tools: Make lookup-apn use mbpi.c
authorDenis Kenzior <denkenz@gmail.com>
Fri, 9 Sep 2011 04:49:49 +0000 (23:49 -0500)
committerDenis Kenzior <denkenz@gmail.com>
Fri, 9 Sep 2011 04:50:19 +0000 (23:50 -0500)
Makefile.am
tools/lookup-apn.c

index 832c8ac..9b0455c 100644 (file)
@@ -631,7 +631,7 @@ tools_auto_enable_LDADD = @GLIB_LIBS@ @DBUS_LIBS@
 tools_get_location_SOURCES = tools/get-location.c
 tools_get_location_LDADD = @GLIB_LIBS@ @DBUS_LIBS@
 
-tools_lookup_apn_SOURCES = tools/lookup-apn.c
+tools_lookup_apn_SOURCES = plugins/mbpi.c plugins/mbpi.h tools/lookup-apn.c
 tools_lookup_apn_LDADD = @GLIB_LIBS@
 endif
 
index 5239b65..2969baf 100644 (file)
 
 #include <glib.h>
 
-#ifndef PROVIDER_DATABASE
-#define PROVIDER_DATABASE  "/usr/share/mobile-broadband-provider-info" \
-                                                       "serviceproviders.xml"
-#endif
-
-static gboolean match_found;
-static const char *current_element;
-
-static const char *match_mcc;
-static const char *match_mnc;
-static const char *match_spn;
+#define OFONO_API_SUBJECT_TO_CHANGE
+#include <ofono/modem.h>
+#include <ofono/gprs-provision.h>
 
-static char *found_apn;
-static char *found_username;
-static char *found_password;
+#include "plugins/mbpi.h"
 
-static void start_element_handler(GMarkupParseContext *context,
-                                       const gchar *element_name,
-                                       const gchar **attribute_names,
-                                       const gchar **attribute_values,
-                                       gpointer user_data, GError **error)
+static void lookup_apn(const char *match_mcc, const char *match_mnc)
 {
-       if (g_str_equal(element_name, "network-id") == TRUE) {
-               const char *mcc = NULL, *mnc = NULL;
-               int i;
-
-               for (i = 0; attribute_names[i]; i++) {
-                       if (g_str_equal(attribute_names[i], "mcc") == TRUE)
-                               mcc = attribute_values[i];
-                       if (g_str_equal(attribute_names[i], "mnc") == TRUE)
-                               mnc = attribute_values[i];
-               }
-
-               if (g_strcmp0(mcc, match_mcc) == 0 &&
-                                       g_strcmp0(mnc, match_mnc) == 0)
-                       match_found = TRUE;
-       }
-
-       if (match_found == FALSE)
-               return;
-
-       if (g_str_equal(element_name, "apn") == TRUE) {
-               const char *apn = NULL;
-               int i;
+       GSList *l;
+       GSList *apns;
+       GError *error = NULL;
 
-               for (i = 0; attribute_names[i]; i++) {
-                       if (g_str_equal(attribute_names[i], "value") == TRUE)
-                               apn = attribute_values[i];
-               }
+       printf("Searching for info for network: %s%s\n", match_mcc, match_mnc);
 
-               if (found_apn == NULL)
-                       found_apn = g_strdup(apn);
-       }
-
-       current_element = element_name;
-}
+       apns = mbpi_lookup(match_mcc, match_mnc, TRUE, &error);
 
-static void end_element_handler(GMarkupParseContext *context,
-                                       const gchar *element_name,
-                                       gpointer user_data, GError **error)
-{
-       if (g_str_equal(element_name, "apn") == TRUE ||
-                               g_str_equal(element_name, "gsm") == TRUE ||
-                               g_str_equal(element_name, "cdma") == TRUE)
-               match_found = FALSE;
-}
-
-static void text_handler(GMarkupParseContext *context,
-                                       const gchar *text, gsize text_len,
-                                       gpointer user_data, GError **error)
-{
-       if (match_found == FALSE || found_apn == NULL)
+       if (apns == NULL) {
+               g_print("Lookup failed: %s\n", error->message);
                return;
-
-       if (g_strcmp0(current_element, "username") == 0)
-               found_username = g_strndup(text, text_len);
-       else if (g_strcmp0(current_element, "password") == 0)
-               found_password = g_strndup(text, text_len);
-}
-
-static void error_handler(GMarkupParseContext *context,
-                                       GError *error, gpointer user_data)
-{
-       printf("error\n");
-}
-
-static const GMarkupParser parser = {
-       start_element_handler,
-       end_element_handler,
-       text_handler,
-       NULL,
-       error_handler,
-};
-
-static void parse_database(const char *data, ssize_t size)
-{
-       GMarkupParseContext *context;
-       gboolean result;
-
-       match_found = FALSE;
-
-       context = g_markup_parse_context_new(&parser,
-                               G_MARKUP_TREAT_CDATA_AS_TEXT, NULL, NULL);
-
-       result = g_markup_parse_context_parse(context, data, size, NULL);
-       if (result == TRUE)
-               result = g_markup_parse_context_end_parse(context, NULL);
-
-       g_markup_parse_context_free(context);
-}
-
-static int lookup_apn(const char *mcc, const char *mnc, const char *spn)
-{
-       struct stat st;
-       char *map;
-       int fd;
-
-       if (mcc == NULL || mnc == NULL)
-               return -1;
-
-       fd = open(PROVIDER_DATABASE, O_RDONLY);
-       if (fd < 0)
-               return -1;
-
-       if (fstat(fd, &st) < 0) {
-               close(fd);
-               return -1;
        }
 
-       map = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
-       if (map == NULL || map == MAP_FAILED) {
-               close(fd);
-               return -1;
-       }
-
-       match_mcc = mcc;
-       match_mnc = mnc;
-       match_spn = spn;
-
-       found_apn = NULL;
-       found_username = NULL;
-       found_password = NULL;
+       for (l = apns; l; l = l->next) {
+               struct ofono_gprs_provision_data *apn = l->data;
 
-       parse_database(map, st.st_size);
+               printf("\n");
+               printf("Name: %s\n", apn->name);
+               printf("APN: %s\n", apn->apn);
+               printf("Username: %s\n", apn->username);
+               printf("Password: %s\n", apn->password);
 
-       munmap(map, st.st_size);
-
-       close(fd);
-
-       printf("Network: %s%s\n", match_mcc, match_mnc);
-       printf("APN: %s\n", found_apn);
-       printf("Username: %s\n", found_username);
-       printf("Password: %s\n", found_password);
-
-       g_free(found_apn);
-       g_free(found_username);
-       g_free(found_password);
+               mbpi_provision_data_free(apn);
+       }
 
-       return 0;
+       g_slist_free(apns);
 }
 
 static gboolean option_version = FALSE;
@@ -226,12 +101,12 @@ int main(int argc, char **argv)
                exit(0);
        }
 
-       if (argc < 3) {
+       if (argc < 2) {
                fprintf(stderr, "Missing parameters\n");
                exit(1);
        }
 
-       lookup_apn(argv[1], argv[2], NULL);
+       lookup_apn(argv[1], argv[2]);
 
        return 0;
 }