ofono/src/ussd.c

970 lines
22 KiB
C
Raw Normal View History

2009-05-06 04:13:14 +00:00
/*
*
* oFono - Open Source Telephony
*
2011-10-10 20:39:42 +00:00
* Copyright (C) 2008-2011 Intel Corporation. All rights reserved.
2009-05-06 04:13:14 +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
#include <string.h>
#include <stdio.h>
2009-08-17 18:45:55 +00:00
#include <errno.h>
2009-05-06 04:13:14 +00:00
#include <glib.h>
#include <gdbus.h>
#include "ofono.h"
#include "common.h"
#include "smsutil.h"
2009-05-06 04:13:14 +00:00
#define MAX_USSD_LENGTH 160
2009-05-06 04:13:14 +00:00
2009-08-17 18:45:55 +00:00
static GSList *g_drivers = NULL;
2009-05-06 04:13:14 +00:00
enum ussd_state {
USSD_STATE_IDLE = 0,
USSD_STATE_ACTIVE = 1,
USSD_STATE_USER_ACTION = 2,
USSD_STATE_RESPONSE_SENT,
2009-05-06 04:13:14 +00:00
};
struct ussd_request {
ofono_ussd_request_cb_t cb;
void *user_data;
};
2009-08-17 18:45:55 +00:00
struct ofono_ussd {
int state;
DBusMessage *pending;
2010-08-16 15:39:39 +00:00
DBusMessage *cancel;
int flags;
GSList *ss_control_list;
GSList *ss_passwd_list;
2009-08-17 18:45:55 +00:00
const struct ofono_ussd_driver *driver;
void *driver_data;
struct ofono_atom *atom;
struct ussd_request *req;
};
struct ssc_entry {
2009-05-06 04:13:14 +00:00
char *service;
void *cb;
void *user;
ofono_destroy_func destroy;
2009-05-06 04:13:14 +00:00
};
gboolean __ofono_ussd_is_busy(struct ofono_ussd *ussd)
{
if (ussd == NULL)
return FALSE;
if (ussd->pending || ussd->state != USSD_STATE_IDLE || ussd->req)
return TRUE;
return FALSE;
}
static struct ssc_entry *ssc_entry_create(const char *sc, void *cb, void *data,
ofono_destroy_func destroy)
2009-05-06 04:13:14 +00:00
{
struct ssc_entry *r;
2009-05-06 04:13:14 +00:00
r = g_try_new0(struct ssc_entry, 1);
2009-05-06 04:13:14 +00:00
if (r == NULL)
2009-05-06 04:13:14 +00:00
return r;
r->service = g_strdup(sc);
2009-05-06 04:13:14 +00:00
r->cb = cb;
r->user = data;
r->destroy = destroy;
2009-05-06 04:13:14 +00:00
return r;
}
2016-04-22 13:10:07 +00:00
static void ssc_entry_destroy(gpointer pointer)
2009-05-06 04:13:14 +00:00
{
2016-04-22 13:10:07 +00:00
struct ssc_entry *ca = pointer;
if (ca->destroy)
ca->destroy(ca->user);
2009-05-06 04:13:14 +00:00
g_free(ca->service);
g_free(ca);
}
static gint ssc_entry_find_by_service(gconstpointer a, gconstpointer b)
2009-05-06 04:13:14 +00:00
{
const struct ssc_entry *ca = a;
2009-05-06 04:13:14 +00:00
return strcmp(ca->service, b);
}
gboolean __ofono_ussd_ssc_register(struct ofono_ussd *ussd, const char *sc,
ofono_ussd_ssc_cb_t cb, void *data,
ofono_destroy_func destroy)
2009-05-06 04:13:14 +00:00
{
struct ssc_entry *entry;
2009-05-06 04:13:14 +00:00
if (ussd == NULL)
2009-05-06 04:13:14 +00:00
return FALSE;
entry = ssc_entry_create(sc, cb, data, destroy);
if (entry == NULL)
2009-05-06 04:13:14 +00:00
return FALSE;
ussd->ss_control_list = g_slist_prepend(ussd->ss_control_list, entry);
2009-05-06 04:13:14 +00:00
return TRUE;
}
void __ofono_ussd_ssc_unregister(struct ofono_ussd *ussd, const char *sc)
2009-05-06 04:13:14 +00:00
{
GSList *l;
if (ussd == NULL)
2009-05-06 04:13:14 +00:00
return;
l = g_slist_find_custom(ussd->ss_control_list, sc,
ssc_entry_find_by_service);
2009-05-06 04:13:14 +00:00
if (l == NULL)
2009-05-06 04:13:14 +00:00
return;
ssc_entry_destroy(l->data);
ussd->ss_control_list = g_slist_remove(ussd->ss_control_list, l->data);
}
gboolean __ofono_ussd_passwd_register(struct ofono_ussd *ussd, const char *sc,
ofono_ussd_passwd_cb_t cb, void *data,
ofono_destroy_func destroy)
{
struct ssc_entry *entry;
if (ussd == NULL)
return FALSE;
entry = ssc_entry_create(sc, cb, data, destroy);
if (entry == NULL)
return FALSE;
ussd->ss_passwd_list = g_slist_prepend(ussd->ss_passwd_list, entry);
return TRUE;
}
void __ofono_ussd_passwd_unregister(struct ofono_ussd *ussd, const char *sc)
{
GSList *l;
if (ussd == NULL)
return;
l = g_slist_find_custom(ussd->ss_passwd_list, sc,
ssc_entry_find_by_service);
if (l == NULL)
return;
ssc_entry_destroy(l->data);
ussd->ss_passwd_list = g_slist_remove(ussd->ss_passwd_list, l->data);
}
static gboolean recognized_passwd_change_string(struct ofono_ussd *ussd,
int type, char *sc,
char *sia, char *sib,
char *sic, char *sid,
char *dn, DBusMessage *msg)
{
GSList *l = ussd->ss_passwd_list;
switch (type) {
case SS_CONTROL_TYPE_ACTIVATION:
case SS_CONTROL_TYPE_REGISTRATION:
break;
default:
return FALSE;
}
if (strcmp(sc, "03") || strlen(dn))
return FALSE;
/* If SIC & SID don't match, then we just bail out here */
if (strcmp(sic, sid)) {
DBusConnection *conn = ofono_dbus_get_connection();
DBusMessage *reply = __ofono_error_invalid_format(msg);
g_dbus_send_message(conn, reply);
return TRUE;
}
while ((l = g_slist_find_custom(l, sia,
ssc_entry_find_by_service)) != NULL) {
struct ssc_entry *entry = l->data;
ofono_ussd_passwd_cb_t cb = entry->cb;
if (cb(sia, sib, sic, msg, entry->user))
return TRUE;
l = l->next;
}
return FALSE;
}
static gboolean recognized_control_string(struct ofono_ussd *ussd,
2009-05-06 04:13:14 +00:00
const char *ss_str,
DBusMessage *msg)
{
char *str = g_strdup(ss_str);
char *sc, *sia, *sib, *sic, *sid, *dn;
2009-05-06 04:13:14 +00:00
int type;
gboolean ret = FALSE;
DBG("parsing control string");
2009-05-06 04:13:14 +00:00
if (parse_ss_control_string(str, &type, &sc,
&sia, &sib, &sic, &sid, &dn)) {
GSList *l = ussd->ss_control_list;
2009-05-06 04:13:14 +00:00
DBG("Got parse result: %d, %s, %s, %s, %s, %s, %s",
type, sc, sia, sib, sic, sid, dn);
2009-05-06 04:13:14 +00:00
2010-09-21 19:18:15 +00:00
/*
* A password change string needs to be treated separately
2009-06-18 21:39:55 +00:00
* because it uses a fourth SI and is thus not a valid
2010-09-21 19:18:15 +00:00
* control string.
*/
if (recognized_passwd_change_string(ussd, type, sc,
2009-06-18 21:39:55 +00:00
sia, sib, sic, sid, dn, msg)) {
ret = TRUE;
goto out;
}
if (*sid != '\0')
2009-06-18 21:39:55 +00:00
goto out;
2009-05-06 04:13:14 +00:00
while ((l = g_slist_find_custom(l, sc,
ssc_entry_find_by_service)) != NULL) {
struct ssc_entry *entry = l->data;
ofono_ussd_ssc_cb_t cb = entry->cb;
2009-05-06 04:13:14 +00:00
if (cb(type, sc, sia, sib, sic, dn, msg, entry->user)) {
2009-05-06 04:13:14 +00:00
ret = TRUE;
goto out;
}
l = l->next;
}
2009-05-06 04:13:14 +00:00
}
/* TODO: Handle all strings that control voice calls */
2010-09-21 19:18:15 +00:00
/* TODO: Handle Multiple subscriber profile DN*59#SEND and *59#SEND */
2009-05-06 04:13:14 +00:00
2010-09-21 19:18:15 +00:00
/*
* Note: SIM PIN/PIN2 change and unblock and IMEI presentation
2009-05-06 04:13:14 +00:00
* procedures are not handled by the daemon since they are not followed
* by SEND and are not valid USSD requests.
*/
out:
g_free(str);
return ret;
}
static const char *ussd_get_state_string(struct ofono_ussd *ussd)
{
switch (ussd->state) {
case USSD_STATE_IDLE:
return "idle";
case USSD_STATE_ACTIVE:
case USSD_STATE_RESPONSE_SENT:
return "active";
case USSD_STATE_USER_ACTION:
2010-02-16 15:55:47 +00:00
return "user-response";
}
return "";
}
static void ussd_change_state(struct ofono_ussd *ussd, int state)
{
const char *value;
DBusConnection *conn = ofono_dbus_get_connection();
const char *path = __ofono_atom_get_path(ussd->atom);
if (state == ussd->state)
return;
ussd->state = state;
value = ussd_get_state_string(ussd);
ofono_dbus_signal_property_changed(conn, path,
OFONO_SUPPLEMENTARY_SERVICES_INTERFACE,
2010-02-16 15:55:47 +00:00
"State", DBUS_TYPE_STRING, &value);
}
static void ussd_request_finish(struct ofono_ussd *ussd, int error, int dcs,
const unsigned char *pdu, int len)
{
struct ussd_request *req = ussd->req;
if (req && req->cb)
req->cb(error, dcs, pdu, len, req->user_data);
g_free(req);
ussd->req = NULL;
}
static int ussd_status_to_failure_code(int status)
{
switch (status) {
case OFONO_USSD_STATUS_TIMED_OUT:
return -ETIMEDOUT;
case OFONO_USSD_STATUS_NOT_SUPPORTED:
return -ENOSYS;
}
return 0;
}
2010-09-16 17:32:46 +00:00
static char const *ussd_status_name(int status)
{
switch (status) {
case OFONO_USSD_STATUS_NOTIFY:
return "NOTIFY";
case OFONO_USSD_STATUS_ACTION_REQUIRED:
return "ACTION_REQUIRED";
case OFONO_USSD_STATUS_TERMINATED:
return "TERMINATED";
case OFONO_USSD_STATUS_LOCAL_CLIENT_RESPONDED:
return "LOCAL_CLIENT_RESPONDED";
case OFONO_USSD_STATUS_NOT_SUPPORTED:
return "NOT_SUPPORTED";
case OFONO_USSD_STATUS_TIMED_OUT:
return "TIMED_OUT";
}
return "????";
}
static const char *ussd_state_name(enum ussd_state state)
{
switch (state) {
case USSD_STATE_IDLE:
return "IDLE";
case USSD_STATE_ACTIVE:
return "ACTIVE";
case USSD_STATE_RESPONSE_SENT:
return "RESPONSE_SENT";
case USSD_STATE_USER_ACTION:
return "USER_ACTION";
}
return "????";
}
void ofono_ussd_notify(struct ofono_ussd *ussd, int status, int dcs,
const unsigned char *data, int data_len)
2009-05-06 04:13:14 +00:00
{
DBusConnection *conn = ofono_dbus_get_connection();
2009-05-06 04:13:14 +00:00
const char *ussdstr = "USSD";
char *utf8_str = NULL;
const char *str;
2009-05-06 04:13:14 +00:00
const char sig[] = { DBUS_TYPE_STRING, 0 };
DBusMessage *reply;
DBusMessageIter iter;
DBusMessageIter variant;
2010-09-16 17:32:46 +00:00
DBG("status: %d %s, state: %d %s",
status, ussd_status_name(status),
ussd->state, ussd_state_name(ussd->state));
if (ussd->req &&
(status == OFONO_USSD_STATUS_NOTIFY ||
status == OFONO_USSD_STATUS_TERMINATED ||
status == OFONO_USSD_STATUS_TIMED_OUT ||
status == OFONO_USSD_STATUS_NOT_SUPPORTED)) {
ussd_request_finish(ussd, ussd_status_to_failure_code(status),
dcs, data, data_len);
ussd_change_state(ussd, USSD_STATE_IDLE);
return;
}
if (status == OFONO_USSD_STATUS_TERMINATED) {
if (ussd->state == USSD_STATE_ACTIVE && data && data_len > 0) {
/* Interpret that as a Notify */
status = OFONO_USSD_STATUS_NOTIFY;
} else {
ussd_change_state(ussd, USSD_STATE_IDLE);
if (ussd->pending == NULL)
return;
reply = __ofono_error_network_terminated(ussd->pending);
goto out;
}
}
2010-02-09 20:00:46 +00:00
if (status == OFONO_USSD_STATUS_NOT_SUPPORTED) {
ussd_change_state(ussd, USSD_STATE_IDLE);
if (ussd->pending == NULL)
return;
reply = __ofono_error_not_supported(ussd->pending);
2009-05-06 04:13:14 +00:00
goto out;
}
2010-02-09 20:00:46 +00:00
if (status == OFONO_USSD_STATUS_TIMED_OUT) {
ussd_change_state(ussd, USSD_STATE_IDLE);
if (ussd->pending == NULL)
return;
reply = __ofono_error_timed_out(ussd->pending);
2009-05-06 04:13:14 +00:00
goto out;
}
if (data && data_len > 0)
utf8_str = ussd_decode(dcs, data_len, data);
str = utf8_str;
2009-05-06 04:13:14 +00:00
/* TODO: Rework this in the Agent framework */
if (ussd->state == USSD_STATE_ACTIVE) {
reply = dbus_message_new_method_return(ussd->pending);
if (str == NULL)
2009-10-16 22:45:15 +00:00
str = "";
2009-05-06 04:13:14 +00:00
dbus_message_iter_init_append(reply, &iter);
dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING,
&ussdstr);
dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT, sig,
&variant);
dbus_message_iter_append_basic(&variant, DBUS_TYPE_STRING,
&str);
dbus_message_iter_close_container(&iter, &variant);
if (status == OFONO_USSD_STATUS_ACTION_REQUIRED)
ussd_change_state(ussd, USSD_STATE_USER_ACTION);
else
ussd_change_state(ussd, USSD_STATE_IDLE);
} else if (ussd->state == USSD_STATE_RESPONSE_SENT) {
reply = dbus_message_new_method_return(ussd->pending);
if (str == NULL)
str = "";
dbus_message_append_args(reply, DBUS_TYPE_STRING, &str,
DBUS_TYPE_INVALID);
if (status == OFONO_USSD_STATUS_ACTION_REQUIRED)
ussd_change_state(ussd, USSD_STATE_USER_ACTION);
else
ussd_change_state(ussd, USSD_STATE_IDLE);
} else if (ussd->state == USSD_STATE_IDLE) {
const char *signal_name;
const char *path = __ofono_atom_get_path(ussd->atom);
int new_state;
if (status == OFONO_USSD_STATUS_ACTION_REQUIRED) {
new_state = USSD_STATE_USER_ACTION;
signal_name = "RequestReceived";
} else {
new_state = USSD_STATE_IDLE;
signal_name = "NotificationReceived";
}
if (str == NULL)
str = "";
g_dbus_emit_signal(conn, path,
OFONO_SUPPLEMENTARY_SERVICES_INTERFACE, signal_name,
DBUS_TYPE_STRING, &str, DBUS_TYPE_INVALID);
ussd_change_state(ussd, new_state);
goto free;
} else if (ussd->state == USSD_STATE_USER_ACTION &&
status != OFONO_USSD_STATUS_ACTION_REQUIRED) {
ussd_change_state(ussd, USSD_STATE_IDLE);
if (status == OFONO_USSD_STATUS_NOTIFY && str && str[0]) {
const char *path = __ofono_atom_get_path(ussd->atom);
g_dbus_emit_signal(conn, path,
OFONO_SUPPLEMENTARY_SERVICES_INTERFACE,
"NotificationReceived", DBUS_TYPE_STRING,
&str, DBUS_TYPE_INVALID);
}
goto free;
2009-05-06 04:13:14 +00:00
} else {
ofono_error("Received an unsolicited USSD but can't handle.");
DBG("USSD is: status: %d, %s", status, str);
2009-05-06 04:13:14 +00:00
goto free;
2009-05-06 04:13:14 +00:00
}
out:
g_dbus_send_message(conn, reply);
dbus_message_unref(ussd->pending);
ussd->pending = NULL;
free:
g_free(utf8_str);
2009-05-06 04:13:14 +00:00
}
static void ussd_callback(const struct ofono_error *error, void *data)
{
2009-08-17 18:45:55 +00:00
struct ofono_ussd *ussd = data;
2009-05-06 04:13:14 +00:00
DBusMessage *reply;
if (error->type != OFONO_ERROR_TYPE_NO_ERROR)
DBG("ussd request failed with error: %s",
2009-05-06 04:13:14 +00:00
telephony_error_to_str(error));
if (error->type == OFONO_ERROR_TYPE_NO_ERROR) {
ussd_change_state(ussd, USSD_STATE_ACTIVE);
2009-05-06 04:13:14 +00:00
return;
}
if (ussd->pending == NULL)
2010-02-15 16:39:29 +00:00
return;
2009-05-06 04:13:14 +00:00
2010-02-15 16:39:29 +00:00
reply = __ofono_error_failed(ussd->pending);
__ofono_dbus_pending_reply(&ussd->pending, reply);
2009-05-06 04:13:14 +00:00
}
static DBusMessage *ussd_initiate(DBusConnection *conn, DBusMessage *msg,
void *data)
{
2009-08-17 18:45:55 +00:00
struct ofono_ussd *ussd = data;
struct ofono_modem *modem = __ofono_atom_get_modem(ussd->atom);
2012-01-18 17:45:49 +00:00
struct ofono_voicecall *vc;
gboolean call_in_progress;
2009-05-06 04:13:14 +00:00
const char *str;
int dcs = 0x0f;
unsigned char buf[160];
long num_packed;
2009-05-06 04:13:14 +00:00
if (__ofono_ussd_is_busy(ussd))
return __ofono_error_busy(msg);
2009-05-06 04:13:14 +00:00
if (dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &str,
DBUS_TYPE_INVALID) == FALSE)
return __ofono_error_invalid_args(msg);
2009-05-06 04:13:14 +00:00
if (strlen(str) == 0)
return __ofono_error_invalid_format(msg);
2009-05-06 04:13:14 +00:00
DBG("checking if this is a recognized control string");
if (recognized_control_string(ussd, str, msg))
2009-05-06 04:13:14 +00:00
return NULL;
2012-01-18 17:45:49 +00:00
vc = __ofono_atom_find(OFONO_ATOM_TYPE_VOICECALL, modem);
if (vc)
call_in_progress = __ofono_voicecall_is_busy(vc,
OFONO_VOICECALL_INTERACTION_NONE);
else
call_in_progress = FALSE;
DBG("No.., checking if this is a USSD string");
if (!valid_ussd_string(str, call_in_progress))
return __ofono_error_not_recognized(msg);
2009-05-06 04:13:14 +00:00
if (!ussd_encode(str, &num_packed, buf))
return __ofono_error_invalid_format(msg);
2009-05-06 04:13:14 +00:00
if (ussd->driver->request == NULL)
return __ofono_error_not_implemented(msg);
2009-05-06 04:13:14 +00:00
DBG("OK, running USSD request");
2009-05-06 04:13:14 +00:00
ussd->pending = dbus_message_ref(msg);
ussd->driver->request(ussd, dcs, buf, num_packed, ussd_callback, ussd);
2009-05-06 04:13:14 +00:00
return NULL;
}
static void ussd_response_callback(const struct ofono_error *error, void *data)
{
struct ofono_ussd *ussd = data;
DBusMessage *reply;
if (error->type != OFONO_ERROR_TYPE_NO_ERROR)
DBG("ussd response failed with error: %s",
telephony_error_to_str(error));
if (error->type == OFONO_ERROR_TYPE_NO_ERROR) {
ussd_change_state(ussd, USSD_STATE_RESPONSE_SENT);
return;
}
if (ussd->pending == NULL)
return;
reply = __ofono_error_failed(ussd->pending);
__ofono_dbus_pending_reply(&ussd->pending, reply);
}
static DBusMessage *ussd_respond(DBusConnection *conn, DBusMessage *msg,
void *data)
{
struct ofono_ussd *ussd = data;
const char *str;
int dcs = 0x0f;
unsigned char buf[160];
long num_packed;
if (ussd->pending)
return __ofono_error_busy(msg);
if (ussd->state != USSD_STATE_USER_ACTION)
return __ofono_error_not_active(msg);
if (dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &str,
DBUS_TYPE_INVALID) == FALSE)
return __ofono_error_invalid_args(msg);
if (strlen(str) == 0)
return __ofono_error_invalid_format(msg);
if (!ussd_encode(str, &num_packed, buf))
return __ofono_error_invalid_format(msg);
if (ussd->driver->request == NULL)
return __ofono_error_not_implemented(msg);
ussd->pending = dbus_message_ref(msg);
2010-09-15 15:03:53 +00:00
ussd->driver->request(ussd, dcs, buf, num_packed,
ussd_response_callback, ussd);
return NULL;
}
static void ussd_cancel_callback(const struct ofono_error *error, void *data)
2009-05-06 04:13:14 +00:00
{
2009-08-17 18:45:55 +00:00
struct ofono_ussd *ussd = data;
DBusMessage *reply;
2010-08-16 15:39:39 +00:00
if (error->type != OFONO_ERROR_TYPE_NO_ERROR) {
DBG("ussd cancel failed with error: %s",
telephony_error_to_str(error));
2010-08-16 15:39:39 +00:00
reply = __ofono_error_failed(ussd->cancel);
__ofono_dbus_pending_reply(&ussd->cancel, reply);
return;
2010-08-16 15:39:39 +00:00
}
2010-08-16 15:39:39 +00:00
if (ussd->pending) {
reply = __ofono_error_canceled(ussd->pending);
__ofono_dbus_pending_reply(&ussd->pending, reply);
}
2010-08-16 15:39:39 +00:00
reply = dbus_message_new_method_return(ussd->cancel);
__ofono_dbus_pending_reply(&ussd->cancel, reply);
if (ussd->req)
2010-09-15 16:45:18 +00:00
ussd_request_finish(ussd, -ECANCELED, 0, NULL, 0);
2010-08-16 15:39:39 +00:00
ussd_change_state(ussd, USSD_STATE_IDLE);
2009-05-06 04:13:14 +00:00
}
static DBusMessage *ussd_cancel(DBusConnection *conn, DBusMessage *msg,
void *data)
{
2009-08-17 18:45:55 +00:00
struct ofono_ussd *ussd = data;
2009-05-06 04:13:14 +00:00
if (ussd->state == USSD_STATE_IDLE)
return __ofono_error_not_active(msg);
2009-05-06 04:13:14 +00:00
/* We have called Respond() but not returned from its callback yet */
if (ussd->state == USSD_STATE_USER_ACTION && ussd->pending)
return __ofono_error_busy(msg);
if (ussd->cancel)
return __ofono_error_busy(msg);
if (ussd->driver->cancel == NULL)
return __ofono_error_not_implemented(msg);
2009-05-06 04:13:14 +00:00
2010-08-16 15:39:39 +00:00
ussd->cancel = dbus_message_ref(msg);
2009-05-06 04:13:14 +00:00
2009-08-17 18:45:55 +00:00
ussd->driver->cancel(ussd, ussd_cancel_callback, ussd);
2009-05-06 04:13:14 +00:00
return NULL;
}
static DBusMessage *ussd_get_properties(DBusConnection *conn,
DBusMessage *msg, void *data)
{
struct ofono_ussd *ussd = data;
DBusMessage *reply;
DBusMessageIter iter;
DBusMessageIter dict;
const char *value;
reply = dbus_message_new_method_return(msg);
if (reply == NULL)
return NULL;
dbus_message_iter_init_append(reply, &iter);
dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
OFONO_PROPERTIES_ARRAY_SIGNATURE,
&dict);
value = ussd_get_state_string(ussd);
2010-02-16 15:55:47 +00:00
ofono_dbus_dict_append(&dict, "State", DBUS_TYPE_STRING, &value);
dbus_message_iter_close_container(&iter, &dict);
return reply;
}
static const GDBusMethodTable ussd_methods[] = {
{ GDBUS_ASYNC_METHOD("Initiate",
GDBUS_ARGS({ "command", "s" }),
GDBUS_ARGS({ "result_name", "s" }, { "value", "v" }),
ussd_initiate) },
{ GDBUS_ASYNC_METHOD("Respond",
GDBUS_ARGS({ "reply", "s" }),
GDBUS_ARGS({ "result", "s" }),
ussd_respond) },
{ GDBUS_ASYNC_METHOD("Cancel", NULL, NULL, ussd_cancel) },
{ GDBUS_METHOD("GetProperties",
NULL, GDBUS_ARGS({ "properties", "a{sv}" }),
ussd_get_properties) },
2009-05-06 04:13:14 +00:00
{ }
};
static const GDBusSignalTable ussd_signals[] = {
{ GDBUS_SIGNAL("NotificationReceived",
GDBUS_ARGS({ "message", "s" })) },
{ GDBUS_SIGNAL("RequestReceived",
GDBUS_ARGS({ "message", "s" })) },
{ GDBUS_SIGNAL("PropertyChanged",
GDBUS_ARGS({ "name", "s" }, { "value", "v" })) },
2009-05-06 04:13:14 +00:00
{ }
};
2009-08-17 18:45:55 +00:00
int ofono_ussd_driver_register(const struct ofono_ussd_driver *d)
{
DBG("driver: %p, name: %s", d, d->name);
if (d->probe == NULL)
return -EINVAL;
2010-11-19 13:57:22 +00:00
g_drivers = g_slist_prepend(g_drivers, (void *) d);
2009-08-17 18:45:55 +00:00
return 0;
}
void ofono_ussd_driver_unregister(const struct ofono_ussd_driver *d)
2009-05-06 04:13:14 +00:00
{
2009-08-17 18:45:55 +00:00
DBG("driver: %p, name: %s", d, d->name);
2010-11-19 13:57:22 +00:00
g_drivers = g_slist_remove(g_drivers, (void *) d);
2009-08-17 18:45:55 +00:00
}
static void ussd_unregister(struct ofono_atom *atom)
{
struct ofono_ussd *ussd = __ofono_atom_get_data(atom);
DBusConnection *conn = ofono_dbus_get_connection();
2009-08-17 18:45:55 +00:00
struct ofono_modem *modem = __ofono_atom_get_modem(atom);
const char *path = __ofono_atom_get_path(atom);
DBusMessage *reply;
if (ussd->pending) {
reply = __ofono_error_canceled(ussd->pending);
__ofono_dbus_pending_reply(&ussd->pending, reply);
}
if (ussd->cancel) {
reply = dbus_message_new_method_return(ussd->cancel);
__ofono_dbus_pending_reply(&ussd->cancel, reply);
}
if (ussd->req)
ussd_request_finish(ussd, -ECANCELED, 0, NULL, 0);
ussd_change_state(ussd, USSD_STATE_IDLE);
2009-05-06 04:13:14 +00:00
2016-04-22 13:10:07 +00:00
g_slist_free_full(ussd->ss_control_list, ssc_entry_destroy);
ussd->ss_control_list = NULL;
2016-04-22 13:10:07 +00:00
g_slist_free_full(ussd->ss_passwd_list, ssc_entry_destroy);
ussd->ss_passwd_list = NULL;
ofono_modem_remove_interface(modem,
OFONO_SUPPLEMENTARY_SERVICES_INTERFACE);
2009-08-17 18:45:55 +00:00
g_dbus_unregister_interface(conn, path,
OFONO_SUPPLEMENTARY_SERVICES_INTERFACE);
2009-08-17 18:45:55 +00:00
}
2009-05-06 04:13:14 +00:00
2009-08-17 18:45:55 +00:00
static void ussd_remove(struct ofono_atom *atom)
{
struct ofono_ussd *ussd = __ofono_atom_get_data(atom);
2009-05-06 04:13:14 +00:00
2009-08-17 18:45:55 +00:00
DBG("atom: %p", atom);
2009-05-06 04:13:14 +00:00
2009-08-17 18:45:55 +00:00
if (ussd == NULL)
return;
if (ussd->driver && ussd->driver->remove)
ussd->driver->remove(ussd);
2009-05-06 04:13:14 +00:00
2009-08-17 18:45:55 +00:00
g_free(ussd);
}
2009-05-06 04:13:14 +00:00
2009-08-17 18:45:55 +00:00
struct ofono_ussd *ofono_ussd_create(struct ofono_modem *modem,
unsigned int vendor,
2009-08-17 18:45:55 +00:00
const char *driver,
void *data)
{
struct ofono_ussd *ussd;
GSList *l;
if (driver == NULL)
return NULL;
ussd = g_try_new0(struct ofono_ussd, 1);
if (ussd == NULL)
return NULL;
ussd->atom = __ofono_modem_add_atom(modem, OFONO_ATOM_TYPE_USSD,
ussd_remove, ussd);
for (l = g_drivers; l; l = l->next) {
const struct ofono_ussd_driver *drv = l->data;
if (g_strcmp0(drv->name, driver))
continue;
2009-09-01 23:00:58 +00:00
if (drv->probe(ussd, vendor, data) < 0)
2009-08-17 18:45:55 +00:00
continue;
ussd->driver = drv;
break;
}
return ussd;
}
void ofono_ussd_register(struct ofono_ussd *ussd)
{
DBusConnection *conn = ofono_dbus_get_connection();
struct ofono_modem *modem = __ofono_atom_get_modem(ussd->atom);
const char *path = __ofono_atom_get_path(ussd->atom);
if (!g_dbus_register_interface(conn, path,
OFONO_SUPPLEMENTARY_SERVICES_INTERFACE,
2009-05-06 04:13:14 +00:00
ussd_methods, ussd_signals, NULL,
2009-08-17 18:45:55 +00:00
ussd, NULL)) {
2009-05-06 04:13:14 +00:00
ofono_error("Could not create %s interface",
OFONO_SUPPLEMENTARY_SERVICES_INTERFACE);
2009-05-06 04:13:14 +00:00
2009-08-17 18:45:55 +00:00
return;
2009-05-06 04:13:14 +00:00
}
ofono_modem_add_interface(modem,
OFONO_SUPPLEMENTARY_SERVICES_INTERFACE);
2009-05-06 04:13:14 +00:00
2009-08-17 18:45:55 +00:00
__ofono_atom_register(ussd->atom, ussd_unregister);
2009-05-06 04:13:14 +00:00
}
2009-08-17 18:45:55 +00:00
void ofono_ussd_remove(struct ofono_ussd *ussd)
2009-05-06 04:13:14 +00:00
{
2009-08-17 18:45:55 +00:00
__ofono_atom_free(ussd->atom);
}
2009-05-06 04:13:14 +00:00
2009-08-17 18:45:55 +00:00
void ofono_ussd_set_data(struct ofono_ussd *ussd, void *data)
{
ussd->driver_data = data;
}
2009-05-06 04:13:14 +00:00
2009-08-17 18:45:55 +00:00
void *ofono_ussd_get_data(struct ofono_ussd *ussd)
{
return ussd->driver_data;
2009-05-06 04:13:14 +00:00
}
static void ussd_request_callback(const struct ofono_error *error, void *data)
{
struct ofono_ussd *ussd = data;
if (error->type != OFONO_ERROR_TYPE_NO_ERROR)
2010-09-15 16:45:18 +00:00
ussd_request_finish(ussd, -EINVAL, 0, NULL, 0);
else
ussd_change_state(ussd, USSD_STATE_ACTIVE);
}
int __ofono_ussd_initiate(struct ofono_ussd *ussd, int dcs,
const unsigned char *pdu, int len,
ofono_ussd_request_cb_t cb, void *user_data)
{
struct ussd_request *req;
if (ussd->driver->request == NULL)
return -ENOSYS;
if (__ofono_ussd_is_busy(ussd))
return -EBUSY;
req = g_try_new0(struct ussd_request, 1);
2010-10-06 15:01:40 +00:00
if (req == NULL)
return -ENOMEM;
req->cb = cb;
req->user_data = user_data;
ussd->req = req;
ussd->driver->request(ussd, dcs, pdu, len, ussd_request_callback, ussd);
return 0;
}
void __ofono_ussd_initiate_cancel(struct ofono_ussd *ussd)
{
if (ussd->req == NULL || ussd->req->cb == NULL)
return;
ussd->req->cb = NULL;
}