tools: Make lookup-apn use mbpi.c

This commit is contained in:
Denis Kenzior 2011-09-08 23:49:49 -05:00
parent cbe83ead62
commit a11b68b604
2 changed files with 24 additions and 149 deletions

View 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

View File

@ -33,165 +33,40 @@
#include <glib.h>
#ifndef PROVIDER_DATABASE
#define PROVIDER_DATABASE "/usr/share/mobile-broadband-provider-info" \
"serviceproviders.xml"
#endif
#define OFONO_API_SUBJECT_TO_CHANGE
#include <ofono/modem.h>
#include <ofono/gprs-provision.h>
static gboolean match_found;
static const char *current_element;
#include "plugins/mbpi.h"
static const char *match_mcc;
static const char *match_mnc;
static const char *match_spn;
static char *found_apn;
static char *found_username;
static char *found_password;
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;
GSList *l;
GSList *apns;
GError *error = NULL;
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];
}
printf("Searching for info for network: %s%s\n", match_mcc, match_mnc);
if (g_strcmp0(mcc, match_mcc) == 0 &&
g_strcmp0(mnc, match_mnc) == 0)
match_found = TRUE;
}
apns = mbpi_lookup(match_mcc, match_mnc, TRUE, &error);
if (match_found == FALSE)
if (apns == NULL) {
g_print("Lookup failed: %s\n", error->message);
return;
if (g_str_equal(element_name, "apn") == TRUE) {
const char *apn = NULL;
int i;
for (i = 0; attribute_names[i]; i++) {
if (g_str_equal(attribute_names[i], "value") == TRUE)
apn = attribute_values[i];
}
if (found_apn == NULL)
found_apn = g_strdup(apn);
}
current_element = element_name;
}
for (l = apns; l; l = l->next) {
struct ofono_gprs_provision_data *apn = l->data;
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;
}
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);
static void text_handler(GMarkupParseContext *context,
const gchar *text, gsize text_len,
gpointer user_data, GError **error)
{
if (match_found == FALSE || found_apn == NULL)
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;
mbpi_provision_data_free(apn);
}
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;
parse_database(map, st.st_size);
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);
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;
}