2009-10-23 17:08:48 +00:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* oFono - Open Source Telephony
|
|
|
|
*
|
2010-01-02 01:00:10 +00:00
|
|
|
* Copyright (C) 2008-2010 Intel Corporation. All rights reserved.
|
2009-10-23 17:08:48 +00:00
|
|
|
*
|
|
|
|
* 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"
|
2010-03-31 23:49:39 +00:00
|
|
|
#include "gatppp.h"
|
2009-10-23 17:08:48 +00:00
|
|
|
|
|
|
|
#include "atmodem.h"
|
|
|
|
|
2010-06-03 07:00:42 +00:00
|
|
|
#define STATIC_IP_NETMASK "255.255.255.255"
|
2010-03-31 23:49:39 +00:00
|
|
|
|
2009-10-23 17:08:48 +00:00
|
|
|
static const char *none_prefix[] = { NULL };
|
|
|
|
|
2010-03-31 23:49:39 +00:00
|
|
|
enum state {
|
|
|
|
STATE_IDLE,
|
|
|
|
STATE_ENABLING,
|
|
|
|
STATE_DISABLING,
|
|
|
|
STATE_ACTIVE,
|
|
|
|
};
|
|
|
|
|
2009-10-23 17:08:48 +00:00
|
|
|
struct gprs_context_data {
|
|
|
|
GAtChat *chat;
|
2009-12-01 18:04:14 +00:00
|
|
|
unsigned int active_context;
|
2010-03-31 23:49:39 +00:00
|
|
|
char username[OFONO_GPRS_MAX_USERNAME_LENGTH + 1];
|
|
|
|
char password[OFONO_GPRS_MAX_PASSWORD_LENGTH + 1];
|
|
|
|
GAtPPP *ppp;
|
|
|
|
enum state state;
|
|
|
|
union {
|
|
|
|
ofono_gprs_context_cb_t down_cb; /* Down callback */
|
|
|
|
ofono_gprs_context_up_cb_t up_cb; /* Up callback */
|
|
|
|
};
|
|
|
|
void *cb_data; /* Callback data */
|
2009-10-23 17:08:48 +00:00
|
|
|
};
|
|
|
|
|
2010-04-30 20:32:54 +00:00
|
|
|
static void ppp_connect(const char *interface, const char *ip,
|
2010-03-31 23:49:39 +00:00
|
|
|
const char *dns1, const char *dns2,
|
|
|
|
gpointer user_data)
|
2009-10-23 17:08:48 +00:00
|
|
|
{
|
2010-03-31 23:49:39 +00:00
|
|
|
struct ofono_gprs_context *gc = user_data;
|
|
|
|
struct gprs_context_data *gcd = ofono_gprs_context_get_data(gc);
|
|
|
|
const char *dns[3];
|
2009-10-23 17:08:48 +00:00
|
|
|
|
2010-03-31 23:49:39 +00:00
|
|
|
dns[0] = dns1;
|
|
|
|
dns[1] = dns2;
|
|
|
|
dns[2] = 0;
|
|
|
|
|
|
|
|
gcd->state = STATE_ACTIVE;
|
|
|
|
CALLBACK_WITH_SUCCESS(gcd->up_cb, interface, TRUE, ip,
|
|
|
|
STATIC_IP_NETMASK, NULL,
|
|
|
|
dns, gcd->cb_data);
|
2009-10-23 17:08:48 +00:00
|
|
|
}
|
|
|
|
|
2010-04-30 20:32:54 +00:00
|
|
|
static void ppp_disconnect(GAtPPPDisconnectReason reason, gpointer user_data)
|
2009-10-23 17:08:48 +00:00
|
|
|
{
|
2010-03-31 23:49:39 +00:00
|
|
|
struct ofono_gprs_context *gc = user_data;
|
|
|
|
struct gprs_context_data *gcd = ofono_gprs_context_get_data(gc);
|
|
|
|
|
2010-05-26 15:36:15 +00:00
|
|
|
DBG("");
|
|
|
|
|
2010-06-11 14:16:25 +00:00
|
|
|
g_at_ppp_unref(gcd->ppp);
|
|
|
|
gcd->ppp = NULL;
|
2010-05-26 15:36:21 +00:00
|
|
|
g_at_chat_resume(gcd->chat);
|
|
|
|
|
2010-05-26 15:36:15 +00:00
|
|
|
switch (gcd->state) {
|
|
|
|
case STATE_ENABLING:
|
2010-04-30 20:32:54 +00:00
|
|
|
CALLBACK_WITH_FAILURE(gcd->up_cb, NULL, FALSE, NULL,
|
|
|
|
NULL, NULL, NULL, gcd->cb_data);
|
2010-05-26 15:36:15 +00:00
|
|
|
break;
|
|
|
|
case STATE_DISABLING:
|
|
|
|
CALLBACK_WITH_SUCCESS(gcd->down_cb, gcd->cb_data);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ofono_gprs_context_deactivated(gc, gcd->active_context);
|
|
|
|
break;
|
2010-04-30 20:32:54 +00:00
|
|
|
}
|
|
|
|
|
2010-03-31 23:49:39 +00:00
|
|
|
gcd->active_context = 0;
|
|
|
|
gcd->state = STATE_IDLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean setup_ppp(struct ofono_gprs_context *gc)
|
|
|
|
{
|
|
|
|
struct gprs_context_data *gcd = ofono_gprs_context_get_data(gc);
|
2010-05-26 15:36:21 +00:00
|
|
|
GAtIO *io;
|
|
|
|
|
|
|
|
io = g_at_chat_get_io(gcd->chat);
|
2010-03-31 23:49:39 +00:00
|
|
|
|
2010-05-26 15:36:21 +00:00
|
|
|
g_at_chat_suspend(gcd->chat);
|
2010-03-31 23:49:39 +00:00
|
|
|
|
|
|
|
/* open ppp */
|
2010-05-26 15:36:21 +00:00
|
|
|
gcd->ppp = g_at_ppp_new_from_io(io);
|
2010-03-31 23:49:39 +00:00
|
|
|
|
2010-05-26 15:36:21 +00:00
|
|
|
if (gcd->ppp == NULL) {
|
|
|
|
g_at_chat_resume(gcd->chat);
|
2010-03-31 23:49:39 +00:00
|
|
|
return FALSE;
|
2010-05-26 15:36:21 +00:00
|
|
|
}
|
2010-03-31 23:49:39 +00:00
|
|
|
|
|
|
|
g_at_ppp_set_credentials(gcd->ppp, gcd->username, gcd->password);
|
|
|
|
|
|
|
|
/* set connect and disconnect callbacks */
|
|
|
|
g_at_ppp_set_connect_function(gcd->ppp, ppp_connect, gc);
|
|
|
|
g_at_ppp_set_disconnect_function(gcd->ppp, ppp_disconnect, gc);
|
|
|
|
|
|
|
|
/* open the ppp connection */
|
|
|
|
g_at_ppp_open(gcd->ppp);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void at_cgdata_cb(gboolean ok, GAtResult *result, gpointer user_data)
|
|
|
|
{
|
|
|
|
struct ofono_gprs_context *gc = user_data;
|
2009-10-23 17:08:48 +00:00
|
|
|
struct gprs_context_data *gcd = ofono_gprs_context_get_data(gc);
|
|
|
|
|
|
|
|
if (!ok) {
|
|
|
|
struct ofono_error error;
|
|
|
|
|
2010-03-31 23:49:39 +00:00
|
|
|
ofono_info("Unable to enter data state");
|
|
|
|
|
2009-10-23 17:08:48 +00:00
|
|
|
gcd->active_context = 0;
|
2010-03-31 23:49:39 +00:00
|
|
|
gcd->state = STATE_IDLE;
|
2009-10-23 17:08:48 +00:00
|
|
|
|
|
|
|
decode_at_error(&error, g_at_result_final_response(result));
|
2010-03-31 23:49:39 +00:00
|
|
|
gcd->up_cb(&error, NULL, 0, NULL, NULL, NULL, NULL,
|
|
|
|
gcd->cb_data);
|
2009-10-23 17:08:48 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-03-31 23:49:39 +00:00
|
|
|
setup_ppp(gc);
|
|
|
|
}
|
2009-10-23 17:08:48 +00:00
|
|
|
|
2010-03-31 23:49:39 +00:00
|
|
|
static void at_cgdcont_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);
|
|
|
|
char buf[64];
|
2009-10-23 17:08:48 +00:00
|
|
|
|
2010-03-31 23:49:39 +00:00
|
|
|
if (!ok) {
|
|
|
|
struct ofono_error error;
|
|
|
|
|
|
|
|
gcd->active_context = 0;
|
|
|
|
gcd->state = STATE_IDLE;
|
|
|
|
|
|
|
|
decode_at_error(&error, g_at_result_final_response(result));
|
|
|
|
gcd->up_cb(&error, NULL, 0, NULL, NULL, NULL, NULL,
|
|
|
|
gcd->cb_data);
|
2009-10-23 17:08:48 +00:00
|
|
|
return;
|
2010-03-31 23:49:39 +00:00
|
|
|
}
|
2009-10-23 17:08:48 +00:00
|
|
|
|
2010-03-31 23:49:39 +00:00
|
|
|
sprintf(buf, "AT+CGDATA=\"PPP\",%u", gcd->active_context);
|
|
|
|
if (g_at_chat_send(gcd->chat, buf, none_prefix,
|
|
|
|
at_cgdata_cb, gc, NULL) > 0)
|
|
|
|
return;
|
2009-10-23 17:08:48 +00:00
|
|
|
|
2009-10-23 17:14:47 +00:00
|
|
|
gcd->active_context = 0;
|
2010-03-31 23:49:39 +00:00
|
|
|
gcd->state = STATE_IDLE;
|
2009-10-23 17:14:47 +00:00
|
|
|
|
2010-03-31 23:49:39 +00:00
|
|
|
CALLBACK_WITH_FAILURE(gcd->up_cb, NULL, 0, NULL, NULL, NULL, NULL,
|
|
|
|
gcd->cb_data);
|
2009-10-23 17:08:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void at_gprs_activate_primary(struct ofono_gprs_context *gc,
|
|
|
|
const struct ofono_gprs_primary_context *ctx,
|
2009-11-18 00:54:46 +00:00
|
|
|
ofono_gprs_context_up_cb_t cb, void *data)
|
2009-10-23 17:08:48 +00:00
|
|
|
{
|
|
|
|
struct gprs_context_data *gcd = ofono_gprs_context_get_data(gc);
|
|
|
|
char buf[OFONO_GPRS_MAX_APN_LENGTH + 128];
|
|
|
|
int len;
|
|
|
|
|
|
|
|
gcd->active_context = ctx->cid;
|
2010-03-31 23:49:39 +00:00
|
|
|
gcd->up_cb = cb;
|
|
|
|
gcd->cb_data = data;
|
|
|
|
memcpy(gcd->username, ctx->username, sizeof(ctx->username));
|
|
|
|
memcpy(gcd->password, ctx->password, sizeof(ctx->password));
|
2009-10-23 17:08:48 +00:00
|
|
|
|
2010-03-31 23:49:39 +00:00
|
|
|
gcd->state = STATE_ENABLING;
|
2009-10-23 17:08:48 +00:00
|
|
|
|
2010-02-05 17:59:24 +00:00
|
|
|
len = snprintf(buf, sizeof(buf), "AT+CGDCONT=%u,\"IP\"", ctx->cid);
|
2009-10-23 17:08:48 +00:00
|
|
|
|
|
|
|
if (ctx->apn)
|
|
|
|
snprintf(buf + len, sizeof(buf) - len - 3, ",\"%s\"",
|
|
|
|
ctx->apn);
|
|
|
|
|
|
|
|
if (g_at_chat_send(gcd->chat, buf, none_prefix,
|
2010-03-31 23:49:39 +00:00
|
|
|
at_cgdcont_cb, gc, NULL) > 0)
|
2009-10-23 17:08:48 +00:00
|
|
|
return;
|
|
|
|
|
2009-11-18 00:54:46 +00:00
|
|
|
CALLBACK_WITH_FAILURE(cb, NULL, 0, NULL, NULL, NULL, NULL, data);
|
2009-10-23 17:08:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2010-05-26 15:36:15 +00:00
|
|
|
DBG("");
|
2009-10-23 17:08:48 +00:00
|
|
|
|
2010-05-26 15:36:15 +00:00
|
|
|
gcd->state = STATE_DISABLING;
|
|
|
|
gcd->down_cb = cb;
|
|
|
|
gcd->cb_data = data;
|
2009-10-23 17:08:48 +00:00
|
|
|
|
2010-05-26 15:36:15 +00:00
|
|
|
g_at_ppp_shutdown(gcd->ppp);
|
2009-10-23 17:08:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2010-06-11 14:17:27 +00:00
|
|
|
DBG("");
|
|
|
|
|
|
|
|
if (gcd->state != STATE_IDLE) {
|
|
|
|
g_at_ppp_unref(gcd->ppp);
|
|
|
|
g_at_chat_resume(gcd->chat);
|
|
|
|
}
|
|
|
|
|
2009-10-23 17:08:48 +00:00
|
|
|
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);
|
|
|
|
}
|