Add basic atmodem gprs context implementation
authorDenis Kenzior <denis.kenzior@intel.com>
Fri, 23 Oct 2009 17:08:48 +0000 (12:08 -0500)
committerDenis Kenzior <denkenz@gmail.com>
Fri, 23 Oct 2009 22:05:56 +0000 (17:05 -0500)
Makefile.am
drivers/atmodem/atmodem.c
drivers/atmodem/atmodem.h
drivers/atmodem/gprs-context.c [new file with mode: 0644]

index bed363a..966a630 100644 (file)
@@ -113,7 +113,8 @@ builtin_sources += $(gatchat_sources) \
                                drivers/atmodem/vendor.h \
                                drivers/atmodem/atutil.h \
                                drivers/atmodem/atutil.c \
-                               drivers/atmodem/gprs.c
+                               drivers/atmodem/gprs.c \
+                               drivers/atmodem/gprs-context.c
 
 builtin_modules += calypsomodem
 builtin_sources += drivers/atmodem/atutil.h \
index c34e6bf..71cafcb 100644 (file)
@@ -49,6 +49,7 @@ static int atmodem_init(void)
        at_cbs_init();
        at_call_volume_init();
        at_gprs_init();
+       at_gprs_context_init();
 
        return 0;
 }
@@ -70,6 +71,7 @@ static void atmodem_exit(void)
        at_cbs_exit();
        at_call_volume_exit();
        at_gprs_exit();
+       at_gprs_context_exit();
 }
 
 OFONO_PLUGIN_DEFINE(atmodem, "AT modem driver", VERSION,
index 55b0b68..8d56708 100644 (file)
@@ -65,3 +65,6 @@ extern void at_call_volume_exit();
 
 extern void at_gprs_init();
 extern void at_gprs_exit();
+
+extern void at_gprs_context_init();
+extern void at_gprs_context_exit();
diff --git a/drivers/atmodem/gprs-context.c b/drivers/atmodem/gprs-context.c
new file mode 100644 (file)
index 0000000..23ad3db
--- /dev/null
@@ -0,0 +1,270 @@
+/*
+ *
+ *  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 <config.h>
+#endif
+
+#define _GNU_SOURCE
+#include <string.h>
+#include <stdlib.h>
+#include <stdio.h>
+
+#include <glib.h>
+
+#include <ofono/log.h>
+#include <ofono/modem.h>
+#include <ofono/gprs-context.h>
+
+#include "gatchat.h"
+#include "gatresult.h"
+
+#include "atmodem.h"
+
+static const char *cgdcont_prefix[] = { "+CGDCONT:", NULL };
+static const char *cgact_prefix[] = { "+CGACT:", NULL };
+static const char *none_prefix[] = { NULL };
+
+struct gprs_context_data {
+       GAtChat *chat;
+       unsigned active_context;
+};
+
+static void at_cgact_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("cgact_down_cb", ok, result);
+       decode_at_error(&error, g_at_result_final_response(result));
+
+       cb(&error, cbd->data);
+}
+
+static void at_cgact_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("cgact_up_cb", ok, result);
+       decode_at_error(&error, g_at_result_final_response(result));
+
+       cb(&error, cbd->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];
+
+       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+CGACT=1,%u", gcd->active_context);
+
+       if (g_at_chat_send(gcd->chat, buf, none_prefix,
+                               at_cgact_up_cb, ncbd, g_free) > 0)
+               return;
+
+error:
+       if (ncbd)
+               g_free(ncbd);
+
+       CALLBACK_WITH_FAILURE(cb, cbd->data);
+}
+
+static void at_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,
+                               at_cgdcont_cb, cbd, g_free) > 0)
+               return;
+error:
+       if (cbd)
+               g_free(cbd);
+
+       CALLBACK_WITH_FAILURE(cb, data);
+}
+
+static void at_gprs_deactivate_primary(struct ofono_gprs_context *gc,
+                                       unsigned int id,
+                                       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+CGACT=0,%u", id);
+
+       if (g_at_chat_send(gcd->chat, buf, none_prefix,
+                               at_cgact_down_cb, cbd, g_free) > 0)
+               return;
+
+error:
+       if (cbd)
+               g_free(cbd);
+
+       CALLBACK_WITH_FAILURE(cb, data);
+}
+
+static void at_cgact_read_cb(gboolean ok, GAtResult *result,
+                               gpointer user_data)
+{
+       struct ofono_gprs_context *gc = user_data;
+       struct gprs_context_data *gcd = ofono_gprs_context_get_data(gc);
+       gint cid, state;
+       GAtResultIter iter;
+
+       dump_response("cgact_read_cb", ok, result);
+
+       if (!ok)
+               return;
+
+       while (g_at_result_iter_next(&iter, "+CGACT:")) {
+               if (!g_at_result_iter_next_number(&iter, &cid))
+                       continue;
+
+               if ((unsigned int) cid != gcd->active_context)
+                       continue;
+
+               if (!g_at_result_iter_next_number(&iter, &state))
+                       continue;
+
+               if (state == 1)
+                       continue;
+
+               ofono_gprs_context_deactivated(gc, gcd->active_context);
+               gcd->active_context = 0;
+
+               break;
+       }
+}
+
+static void cgev_notify(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;
+       const char *event;
+
+       if (!g_at_result_iter_next(&iter, "+CGEV:"))
+               return;
+
+       if (!g_at_result_iter_next_unquoted_string(&iter, &event))
+               return;
+
+       if (g_str_has_prefix(event, "NW REACT ") ||
+                       g_str_has_prefix(event, "NW DEACT ") ||
+                       g_str_has_prefix(event, "ME DEACT ")) {
+               /* Ask what primary contexts are active now */
+               g_at_chat_send(gcd->chat, "AT+CGACT?", cgact_prefix,
+                               at_cgact_read_cb, gc, NULL);
+
+               return;
+       }
+}
+
+static int at_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;
+
+       ofono_gprs_context_set_data(gc, gcd);
+
+       return 0;
+}
+
+static void at_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                   = "atmodem",
+       .probe                  = at_gprs_context_probe,
+       .remove                 = at_gprs_context_remove,
+       .activate_primary       = at_gprs_activate_primary,
+       .deactivate_primary     = at_gprs_deactivate_primary,
+};
+
+void at_gprs_context_init()
+{
+       ofono_gprs_context_driver_register(&driver);
+}
+
+void at_gprs_context_exit()
+{
+       ofono_gprs_context_driver_unregister(&driver);
+}