ofono/drivers/atmodem/gprs.c

345 lines
7.6 KiB
C
Raw Normal View History

/*
*
* oFono - Open Source Telephony
*
2010-01-02 01:00:10 +00:00
* Copyright (C) 2008-2010 Intel Corporation. All rights reserved.
* Copyright (C) 2010 ST-Ericsson AB.
*
* 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.h>
#include "gatchat.h"
#include "gatresult.h"
#include "atmodem.h"
#include "vendor.h"
static const char *cgreg_prefix[] = { "+CGREG:", NULL };
static const char *cgdcont_prefix[] = { "+CGDCONT:", NULL };
static const char *none_prefix[] = { NULL };
2009-10-20 18:09:54 +00:00
struct gprs_data {
GAtChat *chat;
unsigned int vendor;
};
static void at_cgatt_cb(gboolean ok, GAtResult *result, gpointer user_data)
{
2009-10-22 22:56:42 +00:00
struct cb_data *cbd = user_data;
ofono_gprs_cb_t cb = cbd->cb;
struct ofono_error error;
decode_at_error(&error, g_at_result_final_response(result));
2009-10-22 22:56:42 +00:00
cb(&error, cbd->data);
}
2009-10-22 22:56:42 +00:00
static void at_gprs_set_attached(struct ofono_gprs *gprs, int attached,
ofono_gprs_cb_t cb, void *data)
{
2009-10-20 18:09:54 +00:00
struct gprs_data *gd = ofono_gprs_get_data(gprs);
2009-10-22 22:56:42 +00:00
struct cb_data *cbd = cb_data_new(cb, data);
char buf[64];
2009-10-22 22:56:42 +00:00
if (!cbd)
goto error;
snprintf(buf, sizeof(buf), "AT+CGATT=%i", attached ? 1 : 0);
2009-10-20 18:09:54 +00:00
if (g_at_chat_send(gd->chat, buf, none_prefix,
2009-10-22 22:56:42 +00:00
at_cgatt_cb, cbd, g_free) > 0)
return;
error:
g_free(cbd);
CALLBACK_WITH_FAILURE(cb, data);
}
static void at_cgreg_cb(gboolean ok, GAtResult *result, gpointer user_data)
{
struct cb_data *cbd = user_data;
ofono_gprs_status_cb_t cb = cbd->cb;
struct ofono_error error;
int status;
struct gprs_data *gd = cbd->user;
decode_at_error(&error, g_at_result_final_response(result));
if (!ok) {
cb(&error, -1, cbd->data);
return;
}
if (at_util_parse_reg(result, "+CGREG:", NULL, &status,
NULL, NULL, NULL, gd->vendor) == FALSE) {
CALLBACK_WITH_FAILURE(cb, -1, cbd->data);
return;
}
cb(&error, status, cbd->data);
}
static void at_gprs_registration_status(struct ofono_gprs *gprs,
ofono_gprs_status_cb_t cb,
void *data)
{
2009-10-20 18:09:54 +00:00
struct gprs_data *gd = ofono_gprs_get_data(gprs);
struct cb_data *cbd = cb_data_new(cb, data);
if (!cbd)
goto error;
cbd->user = gd;
switch (gd->vendor) {
case OFONO_VENDOR_NOVATEL:
/*
* Send $CNTI=0 to find out the current tech, it will be
* intercepted in nw_cnti_notify in network registration
*/
g_at_chat_send(gd->chat, "AT$CNTI=0", none_prefix,
NULL, NULL, NULL);
break;
}
if (g_at_chat_send(gd->chat, "AT+CGREG?", cgreg_prefix,
at_cgreg_cb, cbd, g_free) > 0)
return;
error:
g_free(cbd);
CALLBACK_WITH_FAILURE(cb, -1, data);
}
2009-10-22 23:00:33 +00:00
static void cgreg_notify(GAtResult *result, gpointer user_data)
{
2009-10-20 18:09:54 +00:00
struct ofono_gprs *gprs = user_data;
int status;
struct gprs_data *gd = ofono_gprs_get_data(gprs);
if (at_util_parse_reg_unsolicited(result, "+CGREG:", &status,
NULL, NULL, NULL, gd->vendor) == FALSE)
return;
ofono_gprs_status_notify(gprs, status);
}
static void cgev_notify(GAtResult *result, gpointer user_data)
{
2009-10-20 18:09:54 +00:00
struct ofono_gprs *gprs = user_data;
GAtResultIter iter;
const char *event;
2009-11-10 06:31:07 +00:00
g_at_result_iter_init(&iter, result);
if (!g_at_result_iter_next(&iter, "+CGEV:"))
return;
if (!g_at_result_iter_next_unquoted_string(&iter, &event))
return;
2009-11-10 06:31:07 +00:00
if (g_str_equal(event, "NW DETACH") ||
g_str_equal(event, "ME DETACH")) {
ofono_gprs_detached_notify(gprs);
return;
}
}
2009-10-22 23:00:33 +00:00
static void gprs_initialized(gboolean ok, GAtResult *result, gpointer user_data)
{
2009-10-20 18:09:54 +00:00
struct ofono_gprs *gprs = user_data;
2009-10-22 23:00:33 +00:00
struct gprs_data *gd = ofono_gprs_get_data(gprs);
2009-10-22 23:00:33 +00:00
g_at_chat_register(gd->chat, "+CGEV:", cgev_notify, FALSE, gprs, NULL);
g_at_chat_register(gd->chat, "+CGREG:", cgreg_notify,
FALSE, gprs, NULL);
2009-10-22 23:00:33 +00:00
ofono_gprs_register(gprs);
}
static void at_cgreg_test_cb(gboolean ok, GAtResult *result,
gpointer user_data)
{
struct ofono_gprs *gprs = user_data;
struct gprs_data *gd = ofono_gprs_get_data(gprs);
gint range[2];
GAtResultIter iter;
int cgreg1 = 0;
int cgreg2 = 0;
const char *cmd;
if (!ok)
goto error;
g_at_result_iter_init(&iter, result);
if (!g_at_result_iter_next(&iter, "+CGREG:"))
goto error;
if (!g_at_result_iter_open_list(&iter))
goto error;
while (g_at_result_iter_next_range(&iter, &range[0], &range[1])) {
if (1 >= range[0] && 1 <= range[1])
cgreg1 = 1;
if (2 >= range[0] && 2 <= range[1])
cgreg2 = 1;
}
g_at_result_iter_close_list(&iter);
if (cgreg2)
cmd = "AT+CGREG=2";
else if (cgreg1)
cmd = "AT+CGREG=1";
else
goto error;
g_at_chat_send(gd->chat, cmd, none_prefix, NULL, NULL, NULL);
g_at_chat_send(gd->chat, "AT+CGAUTO=0", none_prefix, NULL, NULL, NULL);
/* Ericsson MBM and ST-E modems do not support AT+CGEREP = 2,1 */
if (gd->vendor == OFONO_VENDOR_MBM)
g_at_chat_send(gd->chat, "AT+CGEREP=1,0", none_prefix,
gprs_initialized, gprs, NULL);
else
g_at_chat_send(gd->chat, "AT+CGEREP=2,1", none_prefix,
gprs_initialized, gprs, NULL);
return;
error:
ofono_info("GPRS not supported on this device");
ofono_gprs_remove(gprs);
}
static void at_cgdcont_test_cb(gboolean ok, GAtResult *result,
gpointer user_data)
{
2009-10-20 18:09:54 +00:00
struct ofono_gprs *gprs = user_data;
struct gprs_data *gd = ofono_gprs_get_data(gprs);
GAtResultIter iter;
2009-10-22 23:00:33 +00:00
int min, max;
const char *pdp_type;
2009-10-22 23:00:33 +00:00
gboolean found = FALSE;
if (!ok)
goto error;
g_at_result_iter_init(&iter, result);
2009-10-22 23:00:33 +00:00
while (!found && g_at_result_iter_next(&iter, "+CGDCONT:")) {
gboolean in_list = FALSE;
if (!g_at_result_iter_open_list(&iter))
2009-10-22 23:00:33 +00:00
continue;
2009-10-22 23:00:33 +00:00
if (g_at_result_iter_next_range(&iter, &min, &max) == FALSE)
continue;
if (!g_at_result_iter_close_list(&iter))
2009-10-22 23:00:33 +00:00
continue;
if (g_at_result_iter_open_list(&iter))
in_list = TRUE;
if (!g_at_result_iter_next_string(&iter, &pdp_type))
2009-10-22 23:00:33 +00:00
continue;
if (in_list && !g_at_result_iter_close_list(&iter))
continue;
/* We look for IP PDPs */
if (g_str_equal(pdp_type, "IP"))
2009-10-22 23:00:33 +00:00
found = TRUE;
}
2009-10-22 23:00:33 +00:00
if (found == FALSE)
goto error;
2009-10-22 23:00:33 +00:00
ofono_gprs_set_cid_range(gprs, min, max);
g_at_chat_send(gd->chat, "AT+CGREG=?", cgreg_prefix,
at_cgreg_test_cb, gprs, NULL);
return;
error:
2009-10-22 23:00:33 +00:00
ofono_info("GPRS not supported on this device");
2009-10-20 18:09:54 +00:00
ofono_gprs_remove(gprs);
}
2009-10-20 18:09:54 +00:00
static int at_gprs_probe(struct ofono_gprs *gprs,
unsigned int vendor, void *data)
{
GAtChat *chat = data;
2009-10-20 18:09:54 +00:00
struct gprs_data *gd;
2009-10-20 18:09:54 +00:00
gd = g_new0(struct gprs_data, 1);
2010-08-12 21:40:40 +00:00
gd->chat = g_at_chat_clone(chat);
gd->vendor = vendor;
2009-10-20 18:09:54 +00:00
ofono_gprs_set_data(gprs, gd);
2010-08-12 21:40:40 +00:00
g_at_chat_send(gd->chat, "AT+CGDCONT=?", cgdcont_prefix,
2009-10-20 18:09:54 +00:00
at_cgdcont_test_cb, gprs, NULL);
2009-10-22 23:00:33 +00:00
return 0;
}
2009-10-20 18:09:54 +00:00
static void at_gprs_remove(struct ofono_gprs *gprs)
{
2009-10-20 18:09:54 +00:00
struct gprs_data *gd = ofono_gprs_get_data(gprs);
2009-10-22 22:56:42 +00:00
ofono_gprs_set_data(gprs, NULL);
2010-08-12 21:40:40 +00:00
g_at_chat_unref(gd->chat);
2009-10-20 18:09:54 +00:00
g_free(gd);
}
2009-10-20 18:09:54 +00:00
static struct ofono_gprs_driver driver = {
.name = "atmodem",
2009-10-20 18:09:54 +00:00
.probe = at_gprs_probe,
.remove = at_gprs_remove,
2009-10-22 22:56:42 +00:00
.set_attached = at_gprs_set_attached,
.attached_status = at_gprs_registration_status,
};
2009-10-20 18:09:54 +00:00
void at_gprs_init()
{
2009-10-20 18:09:54 +00:00
ofono_gprs_driver_register(&driver);
}
2009-10-20 18:09:54 +00:00
void at_gprs_exit()
{
2009-10-20 18:09:54 +00:00
ofono_gprs_driver_unregister(&driver);
}