From 81ad5ea8e0f4ddc8f515bf32603968e723455512 Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Wed, 21 Mar 2012 21:13:13 -0700 Subject: [PATCH] swmodem: Add Sierra Wireless DirectIP support --- Makefile.am | 3 +- drivers/swmodem/gprs-context.c | 248 +++++++++++++++++++++++++++++++++ drivers/swmodem/swmodem.c | 3 + drivers/swmodem/swmodem.h | 3 + 4 files changed, 256 insertions(+), 1 deletion(-) create mode 100644 drivers/swmodem/gprs-context.c diff --git a/Makefile.am b/Makefile.am index 1681b824..760b62b1 100644 --- a/Makefile.am +++ b/Makefile.am @@ -188,7 +188,8 @@ builtin_sources += drivers/atmodem/atutil.h \ builtin_modules += swmodem builtin_sources += drivers/atmodem/atutil.h \ drivers/swmodem/swmodem.h \ - drivers/swmodem/swmodem.c + drivers/swmodem/swmodem.c \ + drivers/swmodem/gprs-context.c builtin_modules += huaweimodem builtin_sources += drivers/atmodem/atutil.h \ diff --git a/drivers/swmodem/gprs-context.c b/drivers/swmodem/gprs-context.c new file mode 100644 index 00000000..d9b494de --- /dev/null +++ b/drivers/swmodem/gprs-context.c @@ -0,0 +1,248 @@ +/* + * + * oFono - Open Source Telephony + * + * Copyright (C) 2008-2011 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 +#include + +#include "gatchat.h" +#include "gatresult.h" +#include "gattty.h" + +#include "swmodem.h" + +static const char *none_prefix[] = { NULL }; + +struct gprs_context_data { + GAtChat *chat; + unsigned int active_context; + ofono_gprs_context_cb_t cb; + void *cb_data; +}; + +static void at_scact_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; + + DBG("ok %d", ok); + + if (ok) { + gcd->cb = cb; + gcd->cb_data = cbd->data; + } + + decode_at_error(&error, g_at_result_final_response(result)); + cb(&error, cbd->data); +} + +static void at_scact_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_gprs_context *gc = cbd->user; + struct gprs_context_data *gcd = ofono_gprs_context_get_data(gc); + struct ofono_modem *modem; + const char *interface; + char buf[64]; + + DBG("ok %d", ok); + + 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; + } + + gcd->cb = cb; + gcd->cb_data = cbd->data; + + snprintf(buf, sizeof(buf), "AT!SCPADDR=%u", gcd->active_context); + g_at_chat_send(gcd->chat, buf, none_prefix, NULL, NULL, NULL); + + modem = ofono_gprs_context_get_modem(gc); + interface = ofono_modem_get_string(modem, "NetworkInterface"); + + ofono_gprs_context_set_interface(gc, interface); + ofono_gprs_context_set_ipv4_address(gc, NULL, FALSE); + + CALLBACK_WITH_SUCCESS(gcd->cb, gcd->cb_data); +} + +static void at_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]; + + DBG("ok %d", ok); + + 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)); + + snprintf(buf, sizeof(buf), "AT!SCACT=1,%u", gcd->active_context); + + if (g_at_chat_send(gcd->chat, buf, none_prefix, + at_scact_up_cb, ncbd, g_free) > 0) + return; + + g_free(ncbd); + + gcd->active_context = 0; + + CALLBACK_WITH_FAILURE(cb, cbd->data); +} + +static void sw_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[64]; + int len; + + DBG("cid %u", ctx->cid); + + gcd->active_context = ctx->cid; + + cbd->user = gc; + + len = snprintf(buf, sizeof(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, + at_cgdcont_cb, cbd, g_free) > 0) + return; + + g_free(cbd); + + CALLBACK_WITH_FAILURE(cb, data); +} + +static void sw_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[128]; + + DBG("cid %u", cid); + + cbd->user = gc; + + snprintf(buf, sizeof(buf), "AT!SCACT=0,%u", cid); + + if (g_at_chat_send(gcd->chat, buf, none_prefix, + at_scact_down_cb, cbd, g_free) > 0) + return; + + g_free(cbd); + + CALLBACK_WITH_FAILURE(cb, data); +} + +static int sw_gprs_context_probe(struct ofono_gprs_context *gc, + unsigned int vendor, void *data) +{ + GAtChat *chat = data; + struct gprs_context_data *gcd; + + DBG(""); + + gcd = g_try_new0(struct gprs_context_data, 1); + if (gcd == NULL) + return -ENOMEM; + + gcd->chat = g_at_chat_clone(chat); + + ofono_gprs_context_set_data(gc, gcd); + + return 0; +} + +static void sw_gprs_context_remove(struct ofono_gprs_context *gc) +{ + struct gprs_context_data *gcd = ofono_gprs_context_get_data(gc); + + DBG(""); + + ofono_gprs_context_set_data(gc, NULL); + + g_at_chat_unref(gcd->chat); + g_free(gcd); +} + +static struct ofono_gprs_context_driver driver = { + .name = "swmodem", + .probe = sw_gprs_context_probe, + .remove = sw_gprs_context_remove, + .activate_primary = sw_gprs_activate_primary, + .deactivate_primary = sw_gprs_deactivate_primary, +}; + +void sw_gprs_context_init(void) +{ + ofono_gprs_context_driver_register(&driver); +} + +void sw_gprs_context_exit(void) +{ + ofono_gprs_context_driver_unregister(&driver); +} diff --git a/drivers/swmodem/swmodem.c b/drivers/swmodem/swmodem.c index 789467c5..81a143ff 100644 --- a/drivers/swmodem/swmodem.c +++ b/drivers/swmodem/swmodem.c @@ -34,11 +34,14 @@ static int swmodem_init(void) { + sw_gprs_context_init(); + return 0; } static void swmodem_exit(void) { + sw_gprs_context_exit(); } OFONO_PLUGIN_DEFINE(swmodem, "Sierra modem driver", VERSION, diff --git a/drivers/swmodem/swmodem.h b/drivers/swmodem/swmodem.h index 90deb948..c501a499 100644 --- a/drivers/swmodem/swmodem.h +++ b/drivers/swmodem/swmodem.h @@ -20,3 +20,6 @@ */ #include + +extern void sw_gprs_context_init(void); +extern void sw_gprs_context_exit(void);