[network-manager-netbook] Implement mobile provider and access method lookup



commit 8f67ce0bcf8157a4e84e92d6ccc8dee6cea3b2cb
Author: Tambet Ingo <tambet gmail com>
Date:   Wed Dec 23 13:23:58 2009 +0200

    Implement mobile provider and access method lookup

 libnm-gtk/nm-mobile-providers.c |  110 +++++++++++++++++++++++++++++++++++++++
 libnm-gtk/nm-mobile-providers.h |   10 ++++
 2 files changed, 120 insertions(+), 0 deletions(-)
---
diff --git a/libnm-gtk/nm-mobile-providers.c b/libnm-gtk/nm-mobile-providers.c
index 54bead9..df1fd07 100644
--- a/libnm-gtk/nm-mobile-providers.c
+++ b/libnm-gtk/nm-mobile-providers.c
@@ -675,6 +675,116 @@ nm_mobile_providers_parse (GHashTable **out_ccs)
     return parser.table;
 }
 
+static gboolean
+nm_gsm_mcc_mnc_matches (NMGsmMccMnc *mcc_mnc, const char *imsi)
+{
+    size_t mcc_len;
+    size_t mnc_len;
+
+    if (!mcc_mnc->mcc || !mcc_mnc->mnc)
+        return FALSE;
+
+    mcc_len = strlen (mcc_mnc->mcc);
+    mnc_len = strlen (mcc_mnc->mnc);
+
+    if (strlen (imsi) < mcc_len + mnc_len)
+        return FALSE;
+
+    if (strncmp (mcc_mnc->mcc, imsi, mcc_len))
+        return FALSE;
+
+    if (strncmp (mcc_mnc->mnc, imsi + mcc_len, mnc_len))
+        return FALSE;
+
+    return TRUE;
+}
+
+static NMMobileAccessMethod *
+gsm_method_lookup (NMMobileProvider *provider, const char *imsi)
+{
+    GSList *iter;
+    GSList *inner_iter;
+
+    for (iter = provider->methods; iter; iter = iter->next) {
+        NMMobileAccessMethod *method = (NMMobileAccessMethod *) iter->data;
+
+        for (inner_iter = method->gsm_mcc_mnc; inner_iter; inner_iter = inner_iter->next) {
+            NMGsmMccMnc *mcc_mnc = (NMGsmMccMnc *) inner_iter->data;
+
+            if (nm_gsm_mcc_mnc_matches (mcc_mnc, imsi))
+                return method;
+        }
+    }
+
+    return NULL;
+}
+
+static NMMobileAccessMethod *
+cdma_method_lookup (NMMobileProvider *provider, const char *imsi)
+{
+    /* FIXME */
+    return FALSE;
+}
+
+NMMobileAccessMethod *
+nm_mobile_provider_access_method_lookup (NMMobileProvider *provider,
+                                         NMMobileProviderType provider_type,
+                                         const char *imsi)
+{
+    NMMobileAccessMethod *method;
+
+    g_return_val_if_fail (provider != NULL, NULL);
+    g_return_val_if_fail (imsi != NULL, NULL);
+
+    switch (provider_type) {
+    case NM_MOBILE_ACCESS_METHOD_TYPE_GSM:
+        method = gsm_method_lookup (provider, imsi);
+        break;
+    case NM_MOBILE_ACCESS_METHOD_TYPE_CDMA:
+        method = cdma_method_lookup (provider, imsi);
+        break;
+    default:
+        method = NULL;
+        break;
+    }
+
+    return method;
+}
+
+gboolean
+nm_mobile_provider_lookup (GHashTable *providers,
+                           NMMobileProviderType provider_type,
+                           const char *imsi,
+                           NMMobileProvider **provider,
+                           NMMobileAccessMethod **access_method)
+{
+    GHashTableIter iter;
+    gpointer key, value;
+
+    g_return_val_if_fail (providers != NULL, FALSE);
+    g_return_val_if_fail (imsi != NULL, FALSE);
+
+    g_hash_table_iter_init (&iter, providers);
+    while (g_hash_table_iter_next (&iter, &key, &value)) {
+        GSList *country_providers = (GSList *) value;
+        GSList *list_iter;
+
+        for (list_iter = country_providers; list_iter; list_iter = list_iter->next) {
+            NMMobileProvider *p = (NMMobileProvider *) list_iter->data;
+            NMMobileAccessMethod *m;
+
+            m = nm_mobile_provider_access_method_lookup (p, provider_type, imsi);
+            if (m) {
+                *provider = p;
+                *access_method = m;
+                return TRUE;
+            }
+        }
+    }
+
+    return FALSE;
+}
+
 static void
 dump_generic (NMMobileAccessMethod *method)
 {
diff --git a/libnm-gtk/nm-mobile-providers.h b/libnm-gtk/nm-mobile-providers.h
index 405aa6c..3268ea7 100644
--- a/libnm-gtk/nm-mobile-providers.h
+++ b/libnm-gtk/nm-mobile-providers.h
@@ -85,6 +85,16 @@ void                  nm_mobile_access_method_unref (NMMobileAccessMethod *metho
 
 GHashTable *nm_mobile_providers_parse (GHashTable **out_ccs);
 
+NMMobileAccessMethod *nm_mobile_provider_access_method_lookup (NMMobileProvider *provider,
+                                                               NMMobileProviderType provider_type,
+                                                               const char *imsi);
+
+gboolean nm_mobile_provider_lookup (GHashTable *providers,
+                                    NMMobileProviderType provider_type,
+                                    const char *imsi,
+                                    NMMobileProvider **provider,
+                                    NMMobileAccessMethod **access_method);
+
 void nm_mobile_providers_dump (GHashTable *providers);
 
 #endif /* NM_MOBILE_PROVIDERS_H */



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]