From a66de6880a0718e855071ad7597a96979b3ec0d5 Mon Sep 17 00:00:00 2001 From: Denis Kenzior Date: Fri, 23 Oct 2009 13:49:14 -0500 Subject: [PATCH] Add initial implementation of MBM gprs context --- Makefile.am | 6 + drivers/mbmmodem/gprs-context.c | 251 ++++++++++++++++++++++++++++++++ drivers/mbmmodem/mbmmodem.c | 49 +++++++ drivers/mbmmodem/mbmmodem.h | 25 ++++ 4 files changed, 331 insertions(+) create mode 100644 drivers/mbmmodem/gprs-context.c create mode 100644 drivers/mbmmodem/mbmmodem.c create mode 100644 drivers/mbmmodem/mbmmodem.h diff --git a/Makefile.am b/Makefile.am index 966a6303..4c159d91 100644 --- a/Makefile.am +++ b/Makefile.am @@ -127,6 +127,12 @@ builtin_sources += drivers/atmodem/atutil.h \ drivers/hfpmodem/hfpmodem.h \ drivers/hfpmodem/hfpmodem.c +builtin_modules += mbmmodem +builtin_sources += drivers/atmodem/atutil.h \ + drivers/mbmmodem/mbmmodem.h \ + drivers/mbmmodem/mbmmodem.c \ + drivers/mbmmodem/gprs-context.c + builtin_modules += modemconf builtin_sources += plugins/modemconf.c diff --git a/drivers/mbmmodem/gprs-context.c b/drivers/mbmmodem/gprs-context.c new file mode 100644 index 00000000..3acaccfc --- /dev/null +++ b/drivers/mbmmodem/gprs-context.c @@ -0,0 +1,251 @@ +/* + * + * oFono - Open Source Telephony + * + * Copyright (C) 2008-2009 Intel Corporation. All rights reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#define _GNU_SOURCE +#include +#include +#include + +#include + +#include +#include +#include + +#include "gatchat.h" +#include "gatresult.h" + +#include "mbmmodem.h" + +#define MBM_E2NAP_DISCONNECTED 0 +#define MBM_E2NAP_CONNECTED 1 +#define MBM_E2NAP_CONNECTING 2 + +static const char *none_prefix[] = { NULL }; + +struct gprs_context_data { + GAtChat *chat; + unsigned active_context; +}; + +static void at_enap_down_cb(gboolean ok, GAtResult *result, gpointer user_data) +{ + struct cb_data *cbd = user_data; + ofono_gprs_context_cb_t cb = cbd->cb; + struct ofono_gprs_context *gc = cbd->user; + struct gprs_context_data *gcd = ofono_gprs_context_get_data(gc); + struct ofono_error error; + + if (ok) + gcd->active_context = 0; + + dump_response("enap_down_cb", ok, result); + decode_at_error(&error, g_at_result_final_response(result)); + + cb(&error, cbd->data); +} + +static void mbm_enap_up_cb(gboolean ok, GAtResult *result, gpointer user_data) +{ + struct cb_data *cbd = user_data; + ofono_gprs_context_cb_t cb = cbd->cb; + struct ofono_error error; + + dump_response("enap_up_cb", ok, result); + decode_at_error(&error, g_at_result_final_response(result)); + + cb(&error, cbd->data); +} + +static void mbm_cgdcont_cb(gboolean ok, GAtResult *result, gpointer user_data) +{ + struct cb_data *cbd = user_data; + ofono_gprs_context_cb_t cb = cbd->cb; + struct ofono_gprs_context *gc = cbd->user; + struct gprs_context_data *gcd = ofono_gprs_context_get_data(gc); + struct cb_data *ncbd; + char buf[64]; + + dump_response("cgdcont_cb", ok, result); + + if (!ok) { + struct ofono_error error; + + gcd->active_context = 0; + + decode_at_error(&error, g_at_result_final_response(result)); + cb(&error, cbd->data); + return; + } + + ncbd = g_memdup(cbd, sizeof(struct cb_data)); + + sprintf(buf, "AT*ENAP=1,%u", gcd->active_context); + + if (g_at_chat_send(gcd->chat, buf, none_prefix, + mbm_enap_up_cb, ncbd, g_free) > 0) + return; + + if (ncbd) + g_free(ncbd); + + gcd->active_context = 0; + + CALLBACK_WITH_FAILURE(cb, cbd->data); +} + +static void mbm_gprs_activate_primary(struct ofono_gprs_context *gc, + const struct ofono_gprs_primary_context *ctx, + ofono_gprs_context_cb_t cb, void *data) +{ + struct gprs_context_data *gcd = ofono_gprs_context_get_data(gc); + struct cb_data *cbd = cb_data_new(cb, data); + char buf[OFONO_GPRS_MAX_APN_LENGTH + 128]; + int len; + + if (!cbd) + goto error; + + gcd->active_context = ctx->cid; + + cbd->user = gc; + + /* TODO: Handle username / password fields */ + len = sprintf(buf, "AT+CGDCONT=%u,\"IP\"", ctx->cid); + + if (ctx->apn) + snprintf(buf + len, sizeof(buf) - len - 3, ",\"%s\"", + ctx->apn); + + if (g_at_chat_send(gcd->chat, buf, none_prefix, + mbm_cgdcont_cb, cbd, g_free) > 0) + return; +error: + if (cbd) + g_free(cbd); + + CALLBACK_WITH_FAILURE(cb, data); +} + +static void mbm_gprs_deactivate_primary(struct ofono_gprs_context *gc, + unsigned int cid, + ofono_gprs_context_cb_t cb, void *data) +{ + struct gprs_context_data *gcd = ofono_gprs_context_get_data(gc); + struct cb_data *cbd = cb_data_new(cb, data); + char buf[64]; + + if (!cbd) + goto error; + + cbd->user = gc; + + sprintf(buf, "AT*ENAP=0,%u", cid); + + if (g_at_chat_send(gcd->chat, buf, none_prefix, + at_enap_down_cb, cbd, g_free) > 0) + return; + +error: + if (cbd) + g_free(cbd); + + CALLBACK_WITH_FAILURE(cb, data); +} + +static void e2nap_notifier(GAtResult *result, gpointer user_data) +{ + struct ofono_gprs_context *gc = user_data; + struct gprs_context_data *gcd = ofono_gprs_context_get_data(gc); + GAtResultIter iter; + int state; + + if (gcd->active_context == 0) + return; + + g_at_result_iter_init(&iter, result); + + if (g_at_result_iter_next(&iter, "*E2NAP:") == FALSE) + return; + + g_at_result_iter_next_number(&iter, &state); + + switch (state) { + case MBM_E2NAP_DISCONNECTED: + ofono_gprs_context_deactivated(gc, gcd->active_context); + gcd->active_context = 0; + break; + case MBM_E2NAP_CONNECTED: + ofono_debug("MBM Context: connected"); + break; + case MBM_E2NAP_CONNECTING: + ofono_debug("MBM Context: connecting"); + break; + default: + break; + }; +} + +static int mbm_gprs_context_probe(struct ofono_gprs_context *gc, + unsigned int vendor, void *data) +{ + GAtChat *chat = data; + struct gprs_context_data *gcd; + + gcd = g_new0(struct gprs_context_data, 1); + gcd->chat = chat; + + g_at_chat_register(chat, "*E2NAP:", e2nap_notifier, FALSE, gc, NULL); + + ofono_gprs_context_set_data(gc, gcd); + + return 0; +} + +static void mbm_gprs_context_remove(struct ofono_gprs_context *gc) +{ + struct gprs_context_data *gcd = ofono_gprs_context_get_data(gc); + + ofono_gprs_context_set_data(gc, NULL); + g_free(gcd); +} + +static struct ofono_gprs_context_driver driver = { + .name = "mbm", + .probe = mbm_gprs_context_probe, + .remove = mbm_gprs_context_remove, + .activate_primary = mbm_gprs_activate_primary, + .deactivate_primary = mbm_gprs_deactivate_primary, +}; + +void mbm_gprs_context_init() +{ + ofono_gprs_context_driver_register(&driver); +} + +void mbm_gprs_context_exit() +{ + ofono_gprs_context_driver_unregister(&driver); +} diff --git a/drivers/mbmmodem/mbmmodem.c b/drivers/mbmmodem/mbmmodem.c new file mode 100644 index 00000000..a85dc12a --- /dev/null +++ b/drivers/mbmmodem/mbmmodem.c @@ -0,0 +1,49 @@ +/* + * + * oFono - Open Source Telephony + * + * Copyright (C) 2008-2009 Intel Corporation. All rights reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include +#include + +#define OFONO_API_SUBJECT_TO_CHANGE +#include +#include + +#include "mbmmodem.h" + +static int mbmmodem_init(void) +{ + mbm_gprs_context_init(); + + return 0; +} + +static void mbmmodem_exit(void) +{ + mbm_gprs_context_exit(); +} + +OFONO_PLUGIN_DEFINE(mbmmodem, "MBM modem driver", VERSION, + OFONO_PLUGIN_PRIORITY_DEFAULT, + mbmmodem_init, mbmmodem_exit) diff --git a/drivers/mbmmodem/mbmmodem.h b/drivers/mbmmodem/mbmmodem.h new file mode 100644 index 00000000..18145e9a --- /dev/null +++ b/drivers/mbmmodem/mbmmodem.h @@ -0,0 +1,25 @@ +/* + * + * oFono - Open Source Telephony + * + * Copyright (C) 2008-2009 Intel Corporation. All rights reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#include + +extern void mbm_gprs_context_init(); +extern void mbm_gprs_context_exit();