3 * oFono - Open Source Telephony
5 * Copyright (C) 2008-2011 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
31 #include <sys/ioctl.h>
32 #include <sys/socket.h>
34 #include <net/route.h>
35 #include <netinet/in.h>
36 #include <arpa/inet.h>
49 #define GPRS_FLAG_ATTACHING 0x1
50 #define GPRS_FLAG_RECHECK 0x2
52 #define SETTINGS_STORE "gprs"
53 #define SETTINGS_GROUP "Settings"
54 #define MAX_CONTEXT_NAME_LENGTH 127
55 #define MAX_MESSAGE_PROXY_LENGTH 255
56 #define MAX_MESSAGE_CENTER_LENGTH 255
57 #define MAX_CONTEXTS 256
58 #define SUSPEND_TIMEOUT 8
60 static GSList *g_drivers = NULL;
61 static GSList *g_context_drivers = NULL;
63 /* 27.007 Section 7.29 */
65 PACKET_BEARER_NONE = 0,
66 PACKET_BEARER_GPRS = 1,
67 PACKET_BEARER_EGPRS = 2,
68 PACKET_BEARER_UMTS = 3,
69 PACKET_BEARER_HSUPA = 4,
70 PACKET_BEARER_HSDPA = 5,
71 PACKET_BEARER_HSUPA_HSDPA = 6,
72 PACKET_BEARER_EPS = 7,
77 ofono_bool_t attached;
78 ofono_bool_t driver_attached;
79 ofono_bool_t roaming_allowed;
81 ofono_bool_t suspended;
85 guint suspend_timeout;
86 struct idmap *pid_map;
87 unsigned int last_context_id;
88 struct idmap *cid_map;
90 struct ofono_netreg *netreg;
91 unsigned int netreg_watch;
92 unsigned int status_watch;
96 GSList *context_drivers;
97 const struct ofono_gprs_driver *driver;
99 struct ofono_atom *atom;
100 struct ofono_sim_context *sim_context;
103 struct ipv4_settings {
112 struct ipv6_settings {
114 unsigned char prefix_len;
119 struct context_settings {
121 struct ipv4_settings *ipv4;
122 struct ipv6_settings *ipv6;
125 struct ofono_gprs_context {
126 struct ofono_gprs *gprs;
127 enum ofono_gprs_context_type type;
129 const struct ofono_gprs_context_driver *driver;
131 struct context_settings *settings;
132 struct ofono_atom *atom;
137 enum ofono_gprs_context_type type;
138 char name[MAX_CONTEXT_NAME_LENGTH + 1];
139 char message_proxy[MAX_MESSAGE_PROXY_LENGTH + 1];
140 char message_center[MAX_MESSAGE_CENTER_LENGTH + 1];
146 DBusMessage *pending;
147 struct ofono_gprs_primary_context context;
148 struct ofono_gprs_context *context_driver;
149 struct ofono_gprs *gprs;
152 static void gprs_netreg_update(struct ofono_gprs *gprs);
153 static void gprs_deactivate_next(struct ofono_gprs *gprs);
155 const char *packet_bearer_to_string(int bearer)
158 case PACKET_BEARER_NONE:
160 case PACKET_BEARER_GPRS:
162 case PACKET_BEARER_EGPRS:
164 case PACKET_BEARER_UMTS:
166 case PACKET_BEARER_HSUPA:
168 case PACKET_BEARER_HSDPA:
170 case PACKET_BEARER_HSUPA_HSDPA:
172 case PACKET_BEARER_EPS:
178 static const char *gprs_context_default_name(enum ofono_gprs_context_type type)
181 case OFONO_GPRS_CONTEXT_TYPE_ANY:
183 case OFONO_GPRS_CONTEXT_TYPE_INTERNET:
185 case OFONO_GPRS_CONTEXT_TYPE_MMS:
187 case OFONO_GPRS_CONTEXT_TYPE_WAP:
189 case OFONO_GPRS_CONTEXT_TYPE_IMS:
196 static const char *gprs_context_type_to_string(
197 enum ofono_gprs_context_type type)
200 case OFONO_GPRS_CONTEXT_TYPE_ANY:
202 case OFONO_GPRS_CONTEXT_TYPE_INTERNET:
204 case OFONO_GPRS_CONTEXT_TYPE_MMS:
206 case OFONO_GPRS_CONTEXT_TYPE_WAP:
208 case OFONO_GPRS_CONTEXT_TYPE_IMS:
215 static gboolean gprs_context_string_to_type(const char *str,
216 enum ofono_gprs_context_type *out)
218 if (g_str_equal(str, "internet")) {
219 *out = OFONO_GPRS_CONTEXT_TYPE_INTERNET;
221 } else if (g_str_equal(str, "wap")) {
222 *out = OFONO_GPRS_CONTEXT_TYPE_WAP;
224 } else if (g_str_equal(str, "mms")) {
225 *out = OFONO_GPRS_CONTEXT_TYPE_MMS;
227 } else if (g_str_equal(str, "ims")) {
228 *out = OFONO_GPRS_CONTEXT_TYPE_IMS;
235 static const char *gprs_proto_to_string(enum ofono_gprs_proto proto)
238 case OFONO_GPRS_PROTO_IP:
240 case OFONO_GPRS_PROTO_IPV6:
242 case OFONO_GPRS_PROTO_IPV4V6:
249 static gboolean gprs_proto_from_string(const char *str,
250 enum ofono_gprs_proto *proto)
252 if (g_str_equal(str, "ip")) {
253 *proto = OFONO_GPRS_PROTO_IP;
255 } else if (g_str_equal(str, "ipv6")) {
256 *proto = OFONO_GPRS_PROTO_IPV6;
258 } else if (g_str_equal(str, "dual")) {
259 *proto = OFONO_GPRS_PROTO_IPV4V6;
266 static unsigned int gprs_cid_alloc(struct ofono_gprs *gprs)
268 return idmap_alloc(gprs->cid_map);
271 static void gprs_cid_release(struct ofono_gprs *gprs, unsigned int id)
273 idmap_put(gprs->cid_map, id);
276 static gboolean assign_context(struct pri_context *ctx)
278 struct idmap *cidmap = ctx->gprs->cid_map;
284 ctx->context.cid = gprs_cid_alloc(ctx->gprs);
285 if (ctx->context.cid == 0)
288 for (l = ctx->gprs->context_drivers; l; l = l->next) {
289 struct ofono_gprs_context *gc = l->data;
291 if (gc->inuse == TRUE)
294 if (gc->driver == NULL)
297 if (gc->driver->activate_primary == NULL ||
298 gc->driver->deactivate_primary == NULL)
301 if (gc->type != OFONO_GPRS_CONTEXT_TYPE_ANY &&
302 gc->type != ctx->type)
305 ctx->context_driver = gc;
306 ctx->context_driver->inuse = TRUE;
308 if (ctx->context.proto == OFONO_GPRS_PROTO_IPV4V6 ||
309 ctx->context.proto == OFONO_GPRS_PROTO_IP)
310 gc->settings->ipv4 = g_new0(struct ipv4_settings, 1);
312 if (ctx->context.proto == OFONO_GPRS_PROTO_IPV4V6 ||
313 ctx->context.proto == OFONO_GPRS_PROTO_IPV6)
314 gc->settings->ipv6 = g_new0(struct ipv6_settings, 1);
322 static void release_context(struct pri_context *ctx)
324 if (ctx == NULL || ctx->gprs == NULL || ctx->context_driver == NULL)
327 gprs_cid_release(ctx->gprs, ctx->context.cid);
328 ctx->context.cid = 0;
329 ctx->context_driver->inuse = FALSE;
330 ctx->context_driver = NULL;
334 static struct pri_context *gprs_context_by_path(struct ofono_gprs *gprs,
335 const char *ctx_path)
339 for (l = gprs->contexts; l; l = l->next) {
340 struct pri_context *ctx = l->data;
342 if (g_str_equal(ctx_path, ctx->path))
349 static void context_settings_free(struct context_settings *settings)
351 if (settings->ipv4) {
352 g_free(settings->ipv4->ip);
353 g_free(settings->ipv4->netmask);
354 g_free(settings->ipv4->gateway);
355 g_strfreev(settings->ipv4->dns);
356 g_free(settings->ipv4->proxy);
358 g_free(settings->ipv4);
359 settings->ipv4 = NULL;
362 if (settings->ipv6) {
363 g_free(settings->ipv6->ip);
364 g_free(settings->ipv6->gateway);
365 g_strfreev(settings->ipv6->dns);
367 g_free(settings->ipv6);
368 settings->ipv6 = NULL;
371 g_free(settings->interface);
372 settings->interface = NULL;
375 static void context_settings_append_ipv4(struct context_settings *settings,
376 DBusMessageIter *iter)
378 DBusMessageIter variant;
379 DBusMessageIter array;
384 arraysig[0] = DBUS_TYPE_ARRAY;
385 arraysig[1] = typesig[0] = DBUS_DICT_ENTRY_BEGIN_CHAR;
386 arraysig[2] = typesig[1] = DBUS_TYPE_STRING;
387 arraysig[3] = typesig[2] = DBUS_TYPE_VARIANT;
388 arraysig[4] = typesig[3] = DBUS_DICT_ENTRY_END_CHAR;
389 arraysig[5] = typesig[4] = '\0';
391 dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT,
394 dbus_message_iter_open_container(&variant, DBUS_TYPE_ARRAY,
396 if (settings == NULL || settings->ipv4 == NULL)
399 ofono_dbus_dict_append(&array, "Interface",
400 DBUS_TYPE_STRING, &settings->interface);
402 /* If we have a Proxy, no other settings are relevant */
403 if (settings->ipv4->proxy) {
404 ofono_dbus_dict_append(&array, "Proxy", DBUS_TYPE_STRING,
405 &settings->ipv4->proxy);
409 if (settings->ipv4->static_ip == TRUE)
414 ofono_dbus_dict_append(&array, "Method", DBUS_TYPE_STRING, &method);
416 if (settings->ipv4->ip)
417 ofono_dbus_dict_append(&array, "Address", DBUS_TYPE_STRING,
418 &settings->ipv4->ip);
420 if (settings->ipv4->netmask)
421 ofono_dbus_dict_append(&array, "Netmask", DBUS_TYPE_STRING,
422 &settings->ipv4->netmask);
424 if (settings->ipv4->gateway)
425 ofono_dbus_dict_append(&array, "Gateway", DBUS_TYPE_STRING,
426 &settings->ipv4->gateway);
428 if (settings->ipv4->dns)
429 ofono_dbus_dict_append_array(&array, "DomainNameServers",
431 &settings->ipv4->dns);
434 dbus_message_iter_close_container(&variant, &array);
436 dbus_message_iter_close_container(iter, &variant);
439 static void context_settings_append_ipv4_dict(struct context_settings *settings,
440 DBusMessageIter *dict)
442 DBusMessageIter entry;
443 const char *key = "Settings";
445 dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY,
448 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
450 context_settings_append_ipv4(settings, &entry);
452 dbus_message_iter_close_container(dict, &entry);
455 static void context_settings_append_ipv6(struct context_settings *settings,
456 DBusMessageIter *iter)
458 DBusMessageIter variant;
459 DBusMessageIter array;
463 arraysig[0] = DBUS_TYPE_ARRAY;
464 arraysig[1] = typesig[0] = DBUS_DICT_ENTRY_BEGIN_CHAR;
465 arraysig[2] = typesig[1] = DBUS_TYPE_STRING;
466 arraysig[3] = typesig[2] = DBUS_TYPE_VARIANT;
467 arraysig[4] = typesig[3] = DBUS_DICT_ENTRY_END_CHAR;
468 arraysig[5] = typesig[4] = '\0';
470 dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT,
473 dbus_message_iter_open_container(&variant, DBUS_TYPE_ARRAY,
475 if (settings == NULL || settings->ipv6 == NULL)
478 ofono_dbus_dict_append(&array, "Interface",
479 DBUS_TYPE_STRING, &settings->interface);
481 if (settings->ipv6->ip)
482 ofono_dbus_dict_append(&array, "Address", DBUS_TYPE_STRING,
483 &settings->ipv6->ip);
485 if (settings->ipv6->prefix_len)
486 ofono_dbus_dict_append(&array, "PrefixLength", DBUS_TYPE_BYTE,
487 &settings->ipv6->prefix_len);
489 if (settings->ipv6->gateway)
490 ofono_dbus_dict_append(&array, "Gateway", DBUS_TYPE_STRING,
491 &settings->ipv6->gateway);
493 if (settings->ipv6->dns)
494 ofono_dbus_dict_append_array(&array, "DomainNameServers",
496 &settings->ipv6->dns);
499 dbus_message_iter_close_container(&variant, &array);
501 dbus_message_iter_close_container(iter, &variant);
504 static void context_settings_append_ipv6_dict(struct context_settings *settings,
505 DBusMessageIter *dict)
507 DBusMessageIter entry;
508 const char *key = "IPv6.Settings";
510 dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY,
513 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
515 context_settings_append_ipv6(settings, &entry);
517 dbus_message_iter_close_container(dict, &entry);
520 static void signal_settings(struct pri_context *ctx, const char *prop,
521 void (*append)(struct context_settings *, DBusMessageIter *))
524 DBusConnection *conn = ofono_dbus_get_connection();
525 const char *path = ctx->path;
527 DBusMessageIter iter;
528 struct context_settings *settings;
530 signal = dbus_message_new_signal(path,
531 OFONO_CONNECTION_CONTEXT_INTERFACE,
537 dbus_message_iter_init_append(signal, &iter);
538 dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &prop);
540 if (ctx->context_driver)
541 settings = ctx->context_driver->settings;
545 append(settings, &iter);
546 g_dbus_send_message(conn, signal);
549 static void pri_context_signal_settings(struct pri_context *ctx,
550 gboolean ipv4, gboolean ipv6)
553 signal_settings(ctx, "Settings",
554 context_settings_append_ipv4);
557 signal_settings(ctx, "IPv6.Settings",
558 context_settings_append_ipv6);
561 static void pri_parse_proxy(struct pri_context *ctx, const char *proxy)
563 char *scheme, *host, *port, *path;
565 scheme = g_strdup(proxy);
569 host = strstr(scheme, "://");
574 if (strcasecmp(scheme, "https") == 0)
575 ctx->proxy_port = 443;
576 else if (strcasecmp(scheme, "http") == 0)
577 ctx->proxy_port = 80;
584 ctx->proxy_port = 80;
587 path = strchr(host, '/');
591 port = strrchr(host, ':');
594 int tmp = strtol(port + 1, &end, 10);
598 ctx->proxy_port = tmp;
602 g_free(ctx->proxy_host);
603 ctx->proxy_host = g_strdup(host);
608 static void pri_ifupdown(const char *interface, ofono_bool_t active)
613 if (interface == NULL)
616 sk = socket(PF_INET, SOCK_DGRAM, 0);
620 memset(&ifr, 0, sizeof(ifr));
621 strncpy(ifr.ifr_name, interface, IFNAMSIZ);
623 if (ioctl(sk, SIOCGIFFLAGS, &ifr) < 0)
626 if (active == TRUE) {
627 if (ifr.ifr_flags & IFF_UP)
629 ifr.ifr_flags |= IFF_UP;
631 if (!(ifr.ifr_flags & IFF_UP))
633 ifr.ifr_flags &= ~IFF_UP;
636 if (ioctl(sk, SIOCSIFFLAGS, &ifr) < 0)
637 ofono_error("Failed to change interface flags");
643 static void pri_set_ipv4_addr(const char *interface, const char *address)
646 struct sockaddr_in addr;
649 if (interface == NULL)
652 sk = socket(PF_INET, SOCK_DGRAM, 0);
656 memset(&ifr, 0, sizeof(ifr));
657 strncpy(ifr.ifr_name, interface, IFNAMSIZ);
659 if (ioctl(sk, SIOCGIFFLAGS, &ifr) < 0)
662 memset(&addr, 0, sizeof(addr));
663 addr.sin_family = AF_INET;
664 addr.sin_addr.s_addr = address ? inet_addr(address) : INADDR_ANY;
665 memcpy(&ifr.ifr_addr, &addr, sizeof(ifr.ifr_addr));
667 if (ioctl(sk, SIOCSIFADDR, &ifr) < 0) {
668 ofono_error("Failed to set interface address");
675 memset(&addr, 0, sizeof(addr));
676 addr.sin_family = AF_INET;
677 addr.sin_addr.s_addr = inet_addr("255.255.255.255");
678 memcpy(&ifr.ifr_netmask, &addr, sizeof(ifr.ifr_netmask));
680 if (ioctl(sk, SIOCSIFNETMASK, &ifr) < 0)
681 ofono_error("Failed to set interface netmask");
687 static void pri_setproxy(const char *interface, const char *proxy)
690 struct sockaddr_in addr;
693 if (interface == NULL)
696 sk = socket(PF_INET, SOCK_DGRAM, 0);
700 memset(&rt, 0, sizeof(rt));
701 rt.rt_flags = RTF_UP | RTF_HOST;
702 rt.rt_dev = (char *) interface;
704 memset(&addr, 0, sizeof(addr));
705 addr.sin_family = AF_INET;
706 addr.sin_addr.s_addr = inet_addr(proxy);
707 memcpy(&rt.rt_dst, &addr, sizeof(rt.rt_dst));
709 memset(&addr, 0, sizeof(addr));
710 addr.sin_family = AF_INET;
711 addr.sin_addr.s_addr = INADDR_ANY;
712 memcpy(&rt.rt_gateway, &addr, sizeof(rt.rt_gateway));
714 memset(&addr, 0, sizeof(addr));
715 addr.sin_family = AF_INET;
716 addr.sin_addr.s_addr = INADDR_ANY;
717 memcpy(&rt.rt_genmask, &addr, sizeof(rt.rt_genmask));
719 if (ioctl(sk, SIOCADDRT, &rt) < 0)
720 ofono_error("Failed to add proxy host route");
725 static void pri_reset_context_settings(struct pri_context *ctx)
727 struct context_settings *settings;
729 gboolean signal_ipv4;
730 gboolean signal_ipv6;
732 if (ctx->context_driver == NULL)
735 settings = ctx->context_driver->settings;
737 interface = settings->interface;
738 settings->interface = NULL;
740 signal_ipv4 = settings->ipv4 != NULL;
741 signal_ipv6 = settings->ipv6 != NULL;
743 context_settings_free(settings);
745 pri_context_signal_settings(ctx, signal_ipv4, signal_ipv6);
747 if (ctx->type == OFONO_GPRS_CONTEXT_TYPE_MMS) {
748 pri_set_ipv4_addr(interface, NULL);
750 g_free(ctx->proxy_host);
751 ctx->proxy_host = NULL;
755 pri_ifupdown(interface, FALSE);
760 static void pri_update_mms_context_settings(struct pri_context *ctx)
762 struct ofono_gprs_context *gc = ctx->context_driver;
763 struct context_settings *settings = gc->settings;
765 if (ctx->message_proxy)
766 settings->ipv4->proxy = g_strdup(ctx->message_proxy);
768 pri_parse_proxy(ctx, ctx->message_proxy);
770 DBG("proxy %s port %u", ctx->proxy_host, ctx->proxy_port);
772 pri_set_ipv4_addr(settings->interface, settings->ipv4->ip);
775 pri_setproxy(settings->interface, ctx->proxy_host);
778 static void append_context_properties(struct pri_context *ctx,
779 DBusMessageIter *dict)
781 const char *type = gprs_context_type_to_string(ctx->type);
782 const char *proto = gprs_proto_to_string(ctx->context.proto);
783 const char *name = ctx->name;
785 const char *strvalue;
786 struct context_settings *settings;
788 ofono_dbus_dict_append(dict, "Name", DBUS_TYPE_STRING, &name);
791 ofono_dbus_dict_append(dict, "Active", DBUS_TYPE_BOOLEAN, &value);
793 ofono_dbus_dict_append(dict, "Type", DBUS_TYPE_STRING, &type);
795 ofono_dbus_dict_append(dict, "Protocol", DBUS_TYPE_STRING, &proto);
797 strvalue = ctx->context.apn;
798 ofono_dbus_dict_append(dict, "AccessPointName", DBUS_TYPE_STRING,
801 strvalue = ctx->context.username;
802 ofono_dbus_dict_append(dict, "Username", DBUS_TYPE_STRING,
805 strvalue = ctx->context.password;
806 ofono_dbus_dict_append(dict, "Password", DBUS_TYPE_STRING,
809 if (ctx->type == OFONO_GPRS_CONTEXT_TYPE_MMS) {
810 strvalue = ctx->message_proxy;
811 ofono_dbus_dict_append(dict, "MessageProxy",
812 DBUS_TYPE_STRING, &strvalue);
814 strvalue = ctx->message_center;
815 ofono_dbus_dict_append(dict, "MessageCenter",
816 DBUS_TYPE_STRING, &strvalue);
819 if (ctx->context_driver)
820 settings = ctx->context_driver->settings;
824 context_settings_append_ipv4_dict(settings, dict);
825 context_settings_append_ipv6_dict(settings, dict);
828 static DBusMessage *pri_get_properties(DBusConnection *conn,
829 DBusMessage *msg, void *data)
831 struct pri_context *ctx = data;
833 DBusMessageIter iter;
834 DBusMessageIter dict;
836 reply = dbus_message_new_method_return(msg);
840 dbus_message_iter_init_append(reply, &iter);
842 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
843 OFONO_PROPERTIES_ARRAY_SIGNATURE,
845 append_context_properties(ctx, &dict);
846 dbus_message_iter_close_container(&iter, &dict);
851 static void pri_activate_callback(const struct ofono_error *error, void *data)
853 struct pri_context *ctx = data;
854 struct ofono_gprs_context *gc = ctx->context_driver;
855 DBusConnection *conn = ofono_dbus_get_connection();
860 if (error->type != OFONO_ERROR_TYPE_NO_ERROR) {
861 DBG("Activating context failed with error: %s",
862 telephony_error_to_str(error));
863 __ofono_dbus_pending_reply(&ctx->pending,
864 __ofono_error_failed(ctx->pending));
865 context_settings_free(ctx->context_driver->settings);
866 release_context(ctx);
871 __ofono_dbus_pending_reply(&ctx->pending,
872 dbus_message_new_method_return(ctx->pending));
874 if (gc->settings->interface != NULL) {
875 pri_ifupdown(gc->settings->interface, TRUE);
877 if (ctx->type == OFONO_GPRS_CONTEXT_TYPE_MMS &&
879 pri_update_mms_context_settings(ctx);
881 pri_context_signal_settings(ctx, gc->settings->ipv4 != NULL,
882 gc->settings->ipv6 != NULL);
886 ofono_dbus_signal_property_changed(conn, ctx->path,
887 OFONO_CONNECTION_CONTEXT_INTERFACE,
888 "Active", DBUS_TYPE_BOOLEAN, &value);
891 static void pri_deactivate_callback(const struct ofono_error *error, void *data)
893 struct pri_context *ctx = data;
894 DBusConnection *conn = ofono_dbus_get_connection();
897 if (error->type != OFONO_ERROR_TYPE_NO_ERROR) {
898 DBG("Deactivating context failed with error: %s",
899 telephony_error_to_str(error));
900 __ofono_dbus_pending_reply(&ctx->pending,
901 __ofono_error_failed(ctx->pending));
905 __ofono_dbus_pending_reply(&ctx->pending,
906 dbus_message_new_method_return(ctx->pending));
908 pri_reset_context_settings(ctx);
909 release_context(ctx);
912 ofono_dbus_signal_property_changed(conn, ctx->path,
913 OFONO_CONNECTION_CONTEXT_INTERFACE,
914 "Active", DBUS_TYPE_BOOLEAN, &value);
917 static DBusMessage *pri_set_apn(struct pri_context *ctx, DBusConnection *conn,
918 DBusMessage *msg, const char *apn)
920 GKeyFile *settings = ctx->gprs->settings;
922 if (strlen(apn) > OFONO_GPRS_MAX_APN_LENGTH)
923 return __ofono_error_invalid_format(msg);
925 if (g_str_equal(apn, ctx->context.apn))
926 return dbus_message_new_method_return(msg);
928 if (is_valid_apn(apn) == FALSE)
929 return __ofono_error_invalid_format(msg);
931 strcpy(ctx->context.apn, apn);
934 g_key_file_set_string(settings, ctx->key,
935 "AccessPointName", apn);
936 storage_sync(ctx->gprs->imsi, SETTINGS_STORE, settings);
939 g_dbus_send_reply(conn, msg, DBUS_TYPE_INVALID);
941 ofono_dbus_signal_property_changed(conn, ctx->path,
942 OFONO_CONNECTION_CONTEXT_INTERFACE,
944 DBUS_TYPE_STRING, &apn);
949 static DBusMessage *pri_set_username(struct pri_context *ctx,
950 DBusConnection *conn, DBusMessage *msg,
951 const char *username)
953 GKeyFile *settings = ctx->gprs->settings;
955 if (strlen(username) > OFONO_GPRS_MAX_USERNAME_LENGTH)
956 return __ofono_error_invalid_format(msg);
958 if (g_str_equal(username, ctx->context.username))
959 return dbus_message_new_method_return(msg);
961 strcpy(ctx->context.username, username);
964 g_key_file_set_string(settings, ctx->key,
965 "Username", username);
966 storage_sync(ctx->gprs->imsi, SETTINGS_STORE, settings);
969 g_dbus_send_reply(conn, msg, DBUS_TYPE_INVALID);
971 ofono_dbus_signal_property_changed(conn, ctx->path,
972 OFONO_CONNECTION_CONTEXT_INTERFACE,
974 DBUS_TYPE_STRING, &username);
979 static DBusMessage *pri_set_password(struct pri_context *ctx,
980 DBusConnection *conn, DBusMessage *msg,
981 const char *password)
983 GKeyFile *settings = ctx->gprs->settings;
985 if (strlen(password) > OFONO_GPRS_MAX_PASSWORD_LENGTH)
986 return __ofono_error_invalid_format(msg);
988 if (g_str_equal(password, ctx->context.password))
989 return dbus_message_new_method_return(msg);
991 strcpy(ctx->context.password, password);
994 g_key_file_set_string(settings, ctx->key,
995 "Password", password);
996 storage_sync(ctx->gprs->imsi, SETTINGS_STORE, settings);
999 g_dbus_send_reply(conn, msg, DBUS_TYPE_INVALID);
1001 ofono_dbus_signal_property_changed(conn, ctx->path,
1002 OFONO_CONNECTION_CONTEXT_INTERFACE,
1004 DBUS_TYPE_STRING, &password);
1009 static DBusMessage *pri_set_type(struct pri_context *ctx, DBusConnection *conn,
1010 DBusMessage *msg, const char *type)
1012 GKeyFile *settings = ctx->gprs->settings;
1013 enum ofono_gprs_context_type context_type;
1015 if (gprs_context_string_to_type(type, &context_type) == FALSE)
1016 return __ofono_error_invalid_format(msg);
1018 if (ctx->type == context_type)
1019 return dbus_message_new_method_return(msg);
1021 ctx->type = context_type;
1024 g_key_file_set_string(settings, ctx->key, "Type", type);
1025 storage_sync(ctx->gprs->imsi, SETTINGS_STORE, settings);
1028 g_dbus_send_reply(conn, msg, DBUS_TYPE_INVALID);
1030 ofono_dbus_signal_property_changed(conn, ctx->path,
1031 OFONO_CONNECTION_CONTEXT_INTERFACE,
1032 "Type", DBUS_TYPE_STRING, &type);
1037 static DBusMessage *pri_set_proto(struct pri_context *ctx,
1038 DBusConnection *conn,
1039 DBusMessage *msg, const char *str)
1041 GKeyFile *settings = ctx->gprs->settings;
1042 enum ofono_gprs_proto proto;
1044 if (gprs_proto_from_string(str, &proto) == FALSE)
1045 return __ofono_error_invalid_format(msg);
1047 if (ctx->context.proto == proto)
1048 return dbus_message_new_method_return(msg);
1050 ctx->context.proto = proto;
1053 g_key_file_set_string(settings, ctx->key, "Protocol", str);
1054 storage_sync(ctx->gprs->imsi, SETTINGS_STORE, settings);
1057 g_dbus_send_reply(conn, msg, DBUS_TYPE_INVALID);
1059 ofono_dbus_signal_property_changed(conn, ctx->path,
1060 OFONO_CONNECTION_CONTEXT_INTERFACE,
1061 "Protocol", DBUS_TYPE_STRING, &str);
1066 static DBusMessage *pri_set_name(struct pri_context *ctx, DBusConnection *conn,
1067 DBusMessage *msg, const char *name)
1069 GKeyFile *settings = ctx->gprs->settings;
1071 if (strlen(name) > MAX_CONTEXT_NAME_LENGTH)
1072 return __ofono_error_invalid_format(msg);
1074 if (ctx->name && g_str_equal(ctx->name, name))
1075 return dbus_message_new_method_return(msg);
1077 strcpy(ctx->name, name);
1080 g_key_file_set_string(settings, ctx->key, "Name", ctx->name);
1081 storage_sync(ctx->gprs->imsi, SETTINGS_STORE, settings);
1084 g_dbus_send_reply(conn, msg, DBUS_TYPE_INVALID);
1086 ofono_dbus_signal_property_changed(conn, ctx->path,
1087 OFONO_CONNECTION_CONTEXT_INTERFACE,
1088 "Name", DBUS_TYPE_STRING, &name);
1093 static DBusMessage *pri_set_message_proxy(struct pri_context *ctx,
1094 DBusConnection *conn,
1095 DBusMessage *msg, const char *proxy)
1097 GKeyFile *settings = ctx->gprs->settings;
1099 if (strlen(proxy) > MAX_MESSAGE_PROXY_LENGTH)
1100 return __ofono_error_invalid_format(msg);
1102 if (ctx->message_proxy && g_str_equal(ctx->message_proxy, proxy))
1103 return dbus_message_new_method_return(msg);
1105 strcpy(ctx->message_proxy, proxy);
1108 g_key_file_set_string(settings, ctx->key, "MessageProxy",
1109 ctx->message_proxy);
1110 storage_sync(ctx->gprs->imsi, SETTINGS_STORE, settings);
1113 g_dbus_send_reply(conn, msg, DBUS_TYPE_INVALID);
1115 ofono_dbus_signal_property_changed(conn, ctx->path,
1116 OFONO_CONNECTION_CONTEXT_INTERFACE,
1117 "MessageProxy", DBUS_TYPE_STRING, &proxy);
1122 static DBusMessage *pri_set_message_center(struct pri_context *ctx,
1123 DBusConnection *conn,
1124 DBusMessage *msg, const char *center)
1126 GKeyFile *settings = ctx->gprs->settings;
1128 if (strlen(center) > MAX_MESSAGE_CENTER_LENGTH)
1129 return __ofono_error_invalid_format(msg);
1131 if (ctx->message_center && g_str_equal(ctx->message_center, center))
1132 return dbus_message_new_method_return(msg);
1134 strcpy(ctx->message_center, center);
1137 g_key_file_set_string(settings, ctx->key, "MessageCenter",
1138 ctx->message_center);
1139 storage_sync(ctx->gprs->imsi, SETTINGS_STORE, settings);
1142 g_dbus_send_reply(conn, msg, DBUS_TYPE_INVALID);
1144 ofono_dbus_signal_property_changed(conn, ctx->path,
1145 OFONO_CONNECTION_CONTEXT_INTERFACE,
1146 "MessageCenter", DBUS_TYPE_STRING, ¢er);
1151 static DBusMessage *pri_set_property(DBusConnection *conn,
1152 DBusMessage *msg, void *data)
1154 struct pri_context *ctx = data;
1155 DBusMessageIter iter;
1156 DBusMessageIter var;
1157 const char *property;
1161 if (!dbus_message_iter_init(msg, &iter))
1162 return __ofono_error_invalid_args(msg);
1164 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
1165 return __ofono_error_invalid_args(msg);
1167 dbus_message_iter_get_basic(&iter, &property);
1168 dbus_message_iter_next(&iter);
1170 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
1171 return __ofono_error_invalid_args(msg);
1173 dbus_message_iter_recurse(&iter, &var);
1175 if (g_str_equal(property, "Active")) {
1176 struct ofono_gprs_context *gc;
1178 if (ctx->gprs->pending)
1179 return __ofono_error_busy(msg);
1182 return __ofono_error_busy(msg);
1184 if (dbus_message_iter_get_arg_type(&var) != DBUS_TYPE_BOOLEAN)
1185 return __ofono_error_invalid_args(msg);
1187 dbus_message_iter_get_basic(&var, &value);
1189 if (ctx->active == (ofono_bool_t) value)
1190 return dbus_message_new_method_return(msg);
1192 if (value && !ctx->gprs->attached)
1193 return __ofono_error_not_attached(msg);
1195 if (ctx->gprs->flags & GPRS_FLAG_ATTACHING)
1196 return __ofono_error_attach_in_progress(msg);
1198 if (value && assign_context(ctx) == FALSE)
1199 return __ofono_error_not_implemented(msg);
1201 gc = ctx->context_driver;
1203 ctx->pending = dbus_message_ref(msg);
1206 gc->driver->activate_primary(gc, &ctx->context,
1207 pri_activate_callback, ctx);
1209 gc->driver->deactivate_primary(gc, ctx->context.cid,
1210 pri_deactivate_callback, ctx);
1215 /* All other properties are read-only when context is active */
1216 if (ctx->active == TRUE)
1217 return __ofono_error_in_use(msg);
1219 if (!strcmp(property, "AccessPointName")) {
1220 if (dbus_message_iter_get_arg_type(&var) != DBUS_TYPE_STRING)
1221 return __ofono_error_invalid_args(msg);
1223 dbus_message_iter_get_basic(&var, &str);
1225 return pri_set_apn(ctx, conn, msg, str);
1226 } else if (!strcmp(property, "Type")) {
1227 if (dbus_message_iter_get_arg_type(&var) != DBUS_TYPE_STRING)
1228 return __ofono_error_invalid_args(msg);
1230 dbus_message_iter_get_basic(&var, &str);
1232 return pri_set_type(ctx, conn, msg, str);
1233 } else if (!strcmp(property, "Protocol")) {
1234 if (dbus_message_iter_get_arg_type(&var) != DBUS_TYPE_STRING)
1235 return __ofono_error_invalid_args(msg);
1237 dbus_message_iter_get_basic(&var, &str);
1239 return pri_set_proto(ctx, conn, msg, str);
1240 } else if (!strcmp(property, "Username")) {
1241 if (dbus_message_iter_get_arg_type(&var) != DBUS_TYPE_STRING)
1242 return __ofono_error_invalid_args(msg);
1244 dbus_message_iter_get_basic(&var, &str);
1246 return pri_set_username(ctx, conn, msg, str);
1247 } else if (!strcmp(property, "Password")) {
1248 if (dbus_message_iter_get_arg_type(&var) != DBUS_TYPE_STRING)
1249 return __ofono_error_invalid_args(msg);
1251 dbus_message_iter_get_basic(&var, &str);
1253 return pri_set_password(ctx, conn, msg, str);
1254 } else if (!strcmp(property, "Name")) {
1255 if (dbus_message_iter_get_arg_type(&var) != DBUS_TYPE_STRING)
1256 return __ofono_error_invalid_args(msg);
1258 dbus_message_iter_get_basic(&var, &str);
1260 return pri_set_name(ctx, conn, msg, str);
1263 if (ctx->type != OFONO_GPRS_CONTEXT_TYPE_MMS)
1264 return __ofono_error_invalid_args(msg);
1266 if (!strcmp(property, "MessageProxy")) {
1267 if (dbus_message_iter_get_arg_type(&var) != DBUS_TYPE_STRING)
1268 return __ofono_error_invalid_args(msg);
1270 dbus_message_iter_get_basic(&var, &str);
1272 return pri_set_message_proxy(ctx, conn, msg, str);
1273 } else if (!strcmp(property, "MessageCenter")) {
1274 if (dbus_message_iter_get_arg_type(&var) != DBUS_TYPE_STRING)
1275 return __ofono_error_invalid_args(msg);
1277 dbus_message_iter_get_basic(&var, &str);
1279 return pri_set_message_center(ctx, conn, msg, str);
1282 return __ofono_error_invalid_args(msg);
1285 static GDBusMethodTable context_methods[] = {
1286 { "GetProperties", "", "a{sv}", pri_get_properties },
1287 { "SetProperty", "sv", "", pri_set_property,
1288 G_DBUS_METHOD_FLAG_ASYNC },
1292 static GDBusSignalTable context_signals[] = {
1293 { "PropertyChanged", "sv" },
1297 static struct pri_context *pri_context_create(struct ofono_gprs *gprs,
1299 enum ofono_gprs_context_type type)
1301 struct pri_context *context = g_try_new0(struct pri_context, 1);
1303 if (context == NULL)
1307 name = gprs_context_default_name(type);
1314 context->gprs = gprs;
1315 strcpy(context->name, name);
1316 context->type = type;
1321 static void pri_context_destroy(gpointer userdata)
1323 struct pri_context *ctx = userdata;
1325 g_free(ctx->proxy_host);
1330 static gboolean context_dbus_register(struct pri_context *ctx)
1332 DBusConnection *conn = ofono_dbus_get_connection();
1334 const char *basepath;
1336 basepath = __ofono_atom_get_path(ctx->gprs->atom);
1338 snprintf(path, sizeof(path), "%s/context%u", basepath, ctx->id);
1340 if (!g_dbus_register_interface(conn, path,
1341 OFONO_CONNECTION_CONTEXT_INTERFACE,
1342 context_methods, context_signals,
1343 NULL, ctx, pri_context_destroy)) {
1344 ofono_error("Could not register PrimaryContext %s", path);
1345 idmap_put(ctx->gprs->pid_map, ctx->id);
1346 pri_context_destroy(ctx);
1351 ctx->path = g_strdup(path);
1352 ctx->key = ctx->path + strlen(basepath) + 1;
1357 static gboolean context_dbus_unregister(struct pri_context *ctx)
1359 DBusConnection *conn = ofono_dbus_get_connection();
1362 if (ctx->active == TRUE) {
1363 const char *interface =
1364 ctx->context_driver->settings->interface;
1366 if (ctx->type == OFONO_GPRS_CONTEXT_TYPE_MMS)
1367 pri_set_ipv4_addr(interface, NULL);
1369 pri_ifupdown(interface, FALSE);
1372 strcpy(path, ctx->path);
1373 idmap_put(ctx->gprs->pid_map, ctx->id);
1375 return g_dbus_unregister_interface(conn, path,
1376 OFONO_CONNECTION_CONTEXT_INTERFACE);
1379 static void update_suspended_property(struct ofono_gprs *gprs,
1380 ofono_bool_t suspended)
1382 DBusConnection *conn = ofono_dbus_get_connection();
1383 const char *path = __ofono_atom_get_path(gprs->atom);
1384 dbus_bool_t value = suspended;
1386 if (gprs->suspend_timeout) {
1387 g_source_remove(gprs->suspend_timeout);
1388 gprs->suspend_timeout = 0;
1391 if (gprs->suspended == suspended)
1394 DBG("%s GPRS service %s", __ofono_atom_get_path(gprs->atom),
1395 suspended ? "suspended" : "resumed");
1397 gprs->suspended = suspended;
1400 ofono_dbus_signal_property_changed(conn, path,
1401 OFONO_CONNECTION_MANAGER_INTERFACE,
1402 "Suspended", DBUS_TYPE_BOOLEAN, &value);
1405 static gboolean suspend_timeout(gpointer data)
1407 struct ofono_gprs *gprs = data;
1409 gprs->suspend_timeout = 0;
1410 update_suspended_property(gprs, TRUE);
1414 void ofono_gprs_suspend_notify(struct ofono_gprs *gprs, int cause)
1417 case GPRS_SUSPENDED_DETACHED:
1418 case GPRS_SUSPENDED_CALL:
1419 case GPRS_SUSPENDED_NO_COVERAGE:
1420 update_suspended_property(gprs, TRUE);
1423 case GPRS_SUSPENDED_SIGNALLING:
1424 case GPRS_SUSPENDED_UNKNOWN_CAUSE:
1425 if (gprs->suspend_timeout)
1426 g_source_remove(gprs->suspend_timeout);
1427 gprs->suspend_timeout = g_timeout_add_seconds(SUSPEND_TIMEOUT,
1434 void ofono_gprs_resume_notify(struct ofono_gprs *gprs)
1436 update_suspended_property(gprs, FALSE);
1439 static void gprs_attached_update(struct ofono_gprs *gprs)
1441 DBusConnection *conn = ofono_dbus_get_connection();
1443 ofono_bool_t attached;
1446 attached = gprs->driver_attached &&
1447 (gprs->status == NETWORK_REGISTRATION_STATUS_REGISTERED ||
1448 gprs->status == NETWORK_REGISTRATION_STATUS_ROAMING);
1450 if (attached == gprs->attached)
1453 gprs->attached = attached;
1455 if (gprs->attached == FALSE) {
1457 struct pri_context *ctx;
1459 for (l = gprs->contexts; l; l = l->next) {
1462 if (ctx->active == FALSE)
1465 pri_reset_context_settings(ctx);
1466 release_context(ctx);
1469 ofono_dbus_signal_property_changed(conn, ctx->path,
1470 OFONO_CONNECTION_CONTEXT_INTERFACE,
1471 "Active", DBUS_TYPE_BOOLEAN, &value);
1477 path = __ofono_atom_get_path(gprs->atom);
1479 ofono_dbus_signal_property_changed(conn, path,
1480 OFONO_CONNECTION_MANAGER_INTERFACE,
1481 "Attached", DBUS_TYPE_BOOLEAN, &value);
1484 static void registration_status_cb(const struct ofono_error *error,
1485 int status, void *data)
1487 struct ofono_gprs *gprs = data;
1489 DBG("%s error %d status %d", __ofono_atom_get_path(gprs->atom),
1490 error->type, status);
1492 gprs->flags &= ~GPRS_FLAG_ATTACHING;
1494 if (error->type == OFONO_ERROR_TYPE_NO_ERROR)
1495 ofono_gprs_status_notify(gprs, status);
1497 gprs_attached_update(gprs);
1499 if (gprs->flags & GPRS_FLAG_RECHECK) {
1500 gprs->flags &= ~GPRS_FLAG_RECHECK;
1501 gprs_netreg_update(gprs);
1505 static void gprs_attach_callback(const struct ofono_error *error, void *data)
1507 struct ofono_gprs *gprs = data;
1509 DBG("%s error = %d", __ofono_atom_get_path(gprs->atom), error->type);
1511 if (error->type != OFONO_ERROR_TYPE_NO_ERROR)
1512 gprs->driver_attached = !gprs->driver_attached;
1514 if (gprs->driver->attached_status == NULL) {
1515 struct ofono_error error;
1517 error.type = OFONO_ERROR_TYPE_FAILURE;
1520 registration_status_cb(&error, -1, gprs);
1524 gprs->driver->attached_status(gprs, registration_status_cb, gprs);
1527 static void gprs_netreg_removed(struct ofono_gprs *gprs)
1529 gprs->netreg = NULL;
1531 gprs->flags &= ~(GPRS_FLAG_RECHECK | GPRS_FLAG_ATTACHING);
1532 gprs->status_watch = 0;
1533 gprs->netreg_status = NETWORK_REGISTRATION_STATUS_NOT_REGISTERED;
1534 gprs->driver_attached = FALSE;
1536 gprs_attached_update(gprs);
1539 static void gprs_netreg_update(struct ofono_gprs *gprs)
1541 ofono_bool_t attach;
1543 attach = gprs->netreg_status == NETWORK_REGISTRATION_STATUS_REGISTERED;
1545 attach = attach || (gprs->roaming_allowed &&
1546 gprs->netreg_status == NETWORK_REGISTRATION_STATUS_ROAMING);
1548 attach = attach && gprs->powered;
1550 if (gprs->driver_attached == attach)
1553 if (gprs->flags & GPRS_FLAG_ATTACHING) {
1554 gprs->flags |= GPRS_FLAG_RECHECK;
1558 gprs->flags |= GPRS_FLAG_ATTACHING;
1560 gprs->driver->set_attached(gprs, attach, gprs_attach_callback, gprs);
1561 gprs->driver_attached = attach;
1564 static void netreg_status_changed(int status, int lac, int ci, int tech,
1565 const char *mcc, const char *mnc,
1568 struct ofono_gprs *gprs = data;
1572 if (gprs->netreg_status == status)
1575 gprs->netreg_status = status;
1577 gprs_netreg_update(gprs);
1580 static DBusMessage *gprs_get_properties(DBusConnection *conn,
1581 DBusMessage *msg, void *data)
1583 struct ofono_gprs *gprs = data;
1585 DBusMessageIter iter;
1586 DBusMessageIter dict;
1589 reply = dbus_message_new_method_return(msg);
1593 dbus_message_iter_init_append(reply, &iter);
1595 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
1596 OFONO_PROPERTIES_ARRAY_SIGNATURE,
1599 value = gprs->attached;
1600 ofono_dbus_dict_append(&dict, "Attached", DBUS_TYPE_BOOLEAN, &value);
1602 if (gprs->bearer != -1) {
1603 const char *bearer = packet_bearer_to_string(gprs->bearer);
1605 ofono_dbus_dict_append(&dict, "Bearer",
1606 DBUS_TYPE_STRING, &bearer);
1609 value = gprs->roaming_allowed;
1610 ofono_dbus_dict_append(&dict, "RoamingAllowed",
1611 DBUS_TYPE_BOOLEAN, &value);
1613 value = gprs->powered;
1614 ofono_dbus_dict_append(&dict, "Powered", DBUS_TYPE_BOOLEAN, &value);
1616 if (gprs->attached) {
1617 value = gprs->suspended;
1618 ofono_dbus_dict_append(&dict, "Suspended",
1619 DBUS_TYPE_BOOLEAN, &value);
1622 dbus_message_iter_close_container(&iter, &dict);
1627 static DBusMessage *gprs_set_property(DBusConnection *conn,
1628 DBusMessage *msg, void *data)
1630 struct ofono_gprs *gprs = data;
1631 DBusMessageIter iter;
1632 DBusMessageIter var;
1633 const char *property;
1638 return __ofono_error_busy(msg);
1640 if (!dbus_message_iter_init(msg, &iter))
1641 return __ofono_error_invalid_args(msg);
1643 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
1644 return __ofono_error_invalid_args(msg);
1646 dbus_message_iter_get_basic(&iter, &property);
1647 dbus_message_iter_next(&iter);
1649 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
1650 return __ofono_error_invalid_args(msg);
1652 dbus_message_iter_recurse(&iter, &var);
1654 if (!strcmp(property, "RoamingAllowed")) {
1655 if (dbus_message_iter_get_arg_type(&var) != DBUS_TYPE_BOOLEAN)
1656 return __ofono_error_invalid_args(msg);
1658 dbus_message_iter_get_basic(&var, &value);
1660 if (gprs->roaming_allowed == (ofono_bool_t) value)
1661 return dbus_message_new_method_return(msg);
1663 gprs->roaming_allowed = value;
1665 if (gprs->settings) {
1666 g_key_file_set_integer(gprs->settings, SETTINGS_GROUP,
1668 gprs->roaming_allowed);
1669 storage_sync(gprs->imsi, SETTINGS_STORE,
1673 gprs_netreg_update(gprs);
1674 } else if (!strcmp(property, "Powered")) {
1675 if (gprs->driver->set_attached == NULL)
1676 return __ofono_error_not_implemented(msg);
1678 if (dbus_message_iter_get_arg_type(&var) != DBUS_TYPE_BOOLEAN)
1679 return __ofono_error_invalid_args(msg);
1681 dbus_message_iter_get_basic(&var, &value);
1683 if (gprs->powered == (ofono_bool_t) value)
1684 return dbus_message_new_method_return(msg);
1686 gprs->powered = value;
1688 if (gprs->settings) {
1689 g_key_file_set_integer(gprs->settings, SETTINGS_GROUP,
1690 "Powered", gprs->powered);
1691 storage_sync(gprs->imsi, SETTINGS_STORE,
1695 gprs_netreg_update(gprs);
1697 return __ofono_error_invalid_args(msg);
1700 path = __ofono_atom_get_path(gprs->atom);
1701 ofono_dbus_signal_property_changed(conn, path,
1702 OFONO_CONNECTION_MANAGER_INTERFACE,
1703 property, DBUS_TYPE_BOOLEAN, &value);
1705 return dbus_message_new_method_return(msg);
1708 static void write_context_settings(struct ofono_gprs *gprs,
1709 struct pri_context *context)
1711 g_key_file_set_string(gprs->settings, context->key,
1712 "Name", context->name);
1713 g_key_file_set_string(gprs->settings, context->key,
1714 "AccessPointName", context->context.apn);
1715 g_key_file_set_string(gprs->settings, context->key,
1716 "Username", context->context.username);
1717 g_key_file_set_string(gprs->settings, context->key,
1718 "Password", context->context.password);
1719 g_key_file_set_string(gprs->settings, context->key, "Type",
1720 gprs_context_type_to_string(context->type));
1721 g_key_file_set_string(gprs->settings, context->key, "Protocol",
1722 gprs_proto_to_string(context->context.proto));
1724 if (context->type == OFONO_GPRS_CONTEXT_TYPE_MMS) {
1725 g_key_file_set_string(gprs->settings, context->key,
1727 context->message_proxy);
1728 g_key_file_set_string(gprs->settings, context->key,
1730 context->message_center);
1734 static struct pri_context *add_context(struct ofono_gprs *gprs,
1736 enum ofono_gprs_context_type type)
1739 struct pri_context *context;
1741 if (gprs->last_context_id)
1742 id = idmap_alloc_next(gprs->pid_map, gprs->last_context_id);
1744 id = idmap_alloc(gprs->pid_map);
1746 if (id > idmap_get_max(gprs->pid_map))
1749 context = pri_context_create(gprs, name, type);
1750 if (context == NULL) {
1751 idmap_put(gprs->pid_map, id);
1752 ofono_error("Unable to allocate context struct");
1758 DBG("Registering new context");
1760 if (!context_dbus_register(context)) {
1761 ofono_error("Unable to register primary context");
1765 gprs->last_context_id = id;
1767 if (gprs->settings) {
1768 write_context_settings(gprs, context);
1769 storage_sync(gprs->imsi, SETTINGS_STORE, gprs->settings);
1772 gprs->contexts = g_slist_append(gprs->contexts, context);
1777 static DBusMessage *gprs_add_context(DBusConnection *conn,
1778 DBusMessage *msg, void *data)
1780 struct ofono_gprs *gprs = data;
1781 struct pri_context *context;
1782 const char *typestr;
1785 enum ofono_gprs_context_type type;
1786 DBusMessage *signal;
1788 if (!dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &typestr,
1790 return __ofono_error_invalid_args(msg);
1792 if (gprs_context_string_to_type(typestr, &type) == FALSE)
1793 return __ofono_error_invalid_format(msg);
1795 name = gprs_context_default_name(type);
1799 context = add_context(gprs, name, type);
1800 if (context == NULL)
1801 return __ofono_error_failed(msg);
1803 path = context->path;
1805 g_dbus_send_reply(conn, msg, DBUS_TYPE_OBJECT_PATH, &path,
1808 path = __ofono_atom_get_path(gprs->atom);
1809 signal = dbus_message_new_signal(path,
1810 OFONO_CONNECTION_MANAGER_INTERFACE,
1814 DBusMessageIter iter;
1815 DBusMessageIter dict;
1817 dbus_message_iter_init_append(signal, &iter);
1819 path = context->path;
1820 dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
1823 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
1824 OFONO_PROPERTIES_ARRAY_SIGNATURE,
1826 append_context_properties(context, &dict);
1827 dbus_message_iter_close_container(&iter, &dict);
1829 g_dbus_send_message(conn, signal);
1835 static void gprs_deactivate_for_remove(const struct ofono_error *error,
1838 struct pri_context *ctx = data;
1839 struct ofono_gprs *gprs = ctx->gprs;
1840 DBusConnection *conn = ofono_dbus_get_connection();
1842 const char *atompath;
1845 if (error->type != OFONO_ERROR_TYPE_NO_ERROR) {
1846 DBG("Removing context failed with error: %s",
1847 telephony_error_to_str(error));
1849 __ofono_dbus_pending_reply(&gprs->pending,
1850 __ofono_error_failed(gprs->pending));
1854 pri_reset_context_settings(ctx);
1855 release_context(ctx);
1858 ofono_dbus_signal_property_changed(conn, ctx->path,
1859 OFONO_CONNECTION_CONTEXT_INTERFACE,
1860 "Active", DBUS_TYPE_BOOLEAN, &value);
1862 if (gprs->settings) {
1863 g_key_file_remove_group(gprs->settings, ctx->key, NULL);
1864 storage_sync(gprs->imsi, SETTINGS_STORE, gprs->settings);
1867 /* Make a backup copy of path for signal emission below */
1868 path = g_strdup(ctx->path);
1870 context_dbus_unregister(ctx);
1871 gprs->contexts = g_slist_remove(gprs->contexts, ctx);
1873 __ofono_dbus_pending_reply(&gprs->pending,
1874 dbus_message_new_method_return(gprs->pending));
1876 atompath = __ofono_atom_get_path(gprs->atom);
1877 g_dbus_emit_signal(conn, atompath, OFONO_CONNECTION_MANAGER_INTERFACE,
1878 "ContextRemoved", DBUS_TYPE_OBJECT_PATH, &path,
1883 static DBusMessage *gprs_remove_context(DBusConnection *conn,
1884 DBusMessage *msg, void *data)
1886 struct ofono_gprs *gprs = data;
1887 struct pri_context *ctx;
1889 const char *atompath;
1892 return __ofono_error_busy(msg);
1894 if (!dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
1896 return __ofono_error_invalid_args(msg);
1898 if (path[0] == '\0')
1899 return __ofono_error_invalid_format(msg);
1901 ctx = gprs_context_by_path(gprs, path);
1903 return __ofono_error_not_found(msg);
1906 struct ofono_gprs_context *gc = ctx->context_driver;
1908 /* This context is already being messed with */
1910 return __ofono_error_busy(msg);
1912 gprs->pending = dbus_message_ref(msg);
1913 gc->driver->deactivate_primary(gc, ctx->context.cid,
1914 gprs_deactivate_for_remove, ctx);
1918 if (gprs->settings) {
1919 g_key_file_remove_group(gprs->settings, ctx->key, NULL);
1920 storage_sync(gprs->imsi, SETTINGS_STORE, gprs->settings);
1923 DBG("Unregistering context: %s", ctx->path);
1924 context_dbus_unregister(ctx);
1925 gprs->contexts = g_slist_remove(gprs->contexts, ctx);
1927 g_dbus_send_reply(conn, msg, DBUS_TYPE_INVALID);
1929 atompath = __ofono_atom_get_path(gprs->atom);
1930 g_dbus_emit_signal(conn, atompath, OFONO_CONNECTION_MANAGER_INTERFACE,
1931 "ContextRemoved", DBUS_TYPE_OBJECT_PATH, &path,
1937 static void gprs_deactivate_for_all(const struct ofono_error *error,
1940 struct pri_context *ctx = data;
1941 struct ofono_gprs *gprs = ctx->gprs;
1942 DBusConnection *conn;
1945 if (error->type != OFONO_ERROR_TYPE_NO_ERROR) {
1946 __ofono_dbus_pending_reply(&gprs->pending,
1947 __ofono_error_failed(gprs->pending));
1951 pri_reset_context_settings(ctx);
1952 release_context(ctx);
1954 value = ctx->active;
1955 conn = ofono_dbus_get_connection();
1956 ofono_dbus_signal_property_changed(conn, ctx->path,
1957 OFONO_CONNECTION_CONTEXT_INTERFACE,
1958 "Active", DBUS_TYPE_BOOLEAN, &value);
1960 gprs_deactivate_next(gprs);
1963 static void gprs_deactivate_next(struct ofono_gprs *gprs)
1966 struct pri_context *ctx;
1967 struct ofono_gprs_context *gc;
1969 for (l = gprs->contexts; l; l = l->next) {
1972 if (ctx->active == FALSE)
1975 gc = ctx->context_driver;
1976 gc->driver->deactivate_primary(gc, ctx->context.cid,
1977 gprs_deactivate_for_all, ctx);
1982 __ofono_dbus_pending_reply(&gprs->pending,
1983 dbus_message_new_method_return(gprs->pending));
1986 static DBusMessage *gprs_deactivate_all(DBusConnection *conn,
1987 DBusMessage *msg, void *data)
1989 struct ofono_gprs *gprs = data;
1991 struct pri_context *ctx;
1994 return __ofono_error_busy(msg);
1996 if (!dbus_message_get_args(msg, NULL, DBUS_TYPE_INVALID))
1997 return __ofono_error_invalid_args(msg);
1999 for (l = gprs->contexts; l; l = l->next) {
2003 return __ofono_error_busy(msg);
2006 gprs->pending = dbus_message_ref(msg);
2008 gprs_deactivate_next(gprs);
2013 static DBusMessage *gprs_get_contexts(DBusConnection *conn,
2014 DBusMessage *msg, void *data)
2016 struct ofono_gprs *gprs = data;
2018 DBusMessageIter iter;
2019 DBusMessageIter array;
2020 DBusMessageIter entry, dict;
2023 struct pri_context *ctx;
2025 reply = dbus_message_new_method_return(msg);
2029 dbus_message_iter_init_append(reply, &iter);
2031 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
2032 DBUS_STRUCT_BEGIN_CHAR_AS_STRING
2033 DBUS_TYPE_OBJECT_PATH_AS_STRING
2034 DBUS_TYPE_ARRAY_AS_STRING
2035 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
2036 DBUS_TYPE_STRING_AS_STRING
2037 DBUS_TYPE_VARIANT_AS_STRING
2038 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
2039 DBUS_STRUCT_END_CHAR_AS_STRING,
2042 for (l = gprs->contexts; l; l = l->next) {
2047 dbus_message_iter_open_container(&array, DBUS_TYPE_STRUCT,
2049 dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
2051 dbus_message_iter_open_container(&entry, DBUS_TYPE_ARRAY,
2052 OFONO_PROPERTIES_ARRAY_SIGNATURE,
2055 append_context_properties(ctx, &dict);
2056 dbus_message_iter_close_container(&entry, &dict);
2057 dbus_message_iter_close_container(&array, &entry);
2060 dbus_message_iter_close_container(&iter, &array);
2065 static GDBusMethodTable manager_methods[] = {
2066 { "GetProperties", "", "a{sv}", gprs_get_properties },
2067 { "SetProperty", "sv", "", gprs_set_property },
2068 { "AddContext", "s", "o", gprs_add_context,
2069 G_DBUS_METHOD_FLAG_ASYNC },
2070 { "RemoveContext", "o", "", gprs_remove_context,
2071 G_DBUS_METHOD_FLAG_ASYNC },
2072 { "DeactivateAll", "", "", gprs_deactivate_all,
2073 G_DBUS_METHOD_FLAG_ASYNC },
2074 { "GetContexts", "", "a(oa{sv})", gprs_get_contexts },
2078 static GDBusSignalTable manager_signals[] = {
2079 { "PropertyChanged", "sv" },
2080 { "ContextAdded", "oa{sv}" },
2081 { "ContextRemoved", "o" },
2085 void ofono_gprs_detached_notify(struct ofono_gprs *gprs)
2087 DBG("%s", __ofono_atom_get_path(gprs->atom));
2089 gprs->driver_attached = FALSE;
2090 gprs_attached_update(gprs);
2093 * TODO: The network forced a detach, we should wait for some time
2094 * and try to re-attach. This might also be related to a suspend
2095 * event while voicecall is active.
2099 void ofono_gprs_status_notify(struct ofono_gprs *gprs, int status)
2101 DBG("%s status %d", __ofono_atom_get_path(gprs->atom), status);
2103 gprs->status = status;
2105 if (status != NETWORK_REGISTRATION_STATUS_REGISTERED &&
2106 status != NETWORK_REGISTRATION_STATUS_ROAMING) {
2107 gprs_attached_update(gprs);
2112 * If we're already taking action, e.g. attaching or detaching, then
2113 * ignore this notification for now, we will take appropriate action
2114 * after the set_attach operation has completed
2116 if (gprs->flags & GPRS_FLAG_ATTACHING)
2119 /* We registered without being powered */
2120 if (gprs->powered == FALSE)
2123 if (gprs->roaming_allowed == FALSE &&
2124 status == NETWORK_REGISTRATION_STATUS_ROAMING)
2127 gprs->driver_attached = TRUE;
2128 gprs_attached_update(gprs);
2133 gprs->flags |= GPRS_FLAG_ATTACHING;
2134 gprs->driver->set_attached(gprs, FALSE, gprs_attach_callback, gprs);
2137 void ofono_gprs_set_cid_range(struct ofono_gprs *gprs,
2138 unsigned int min, unsigned int max)
2144 idmap_free(gprs->cid_map);
2146 gprs->cid_map = idmap_new_from_range(min, max);
2149 static void gprs_context_unregister(struct ofono_atom *atom)
2151 struct ofono_gprs_context *gc = __ofono_atom_get_data(atom);
2152 DBusConnection *conn = ofono_dbus_get_connection();
2154 struct pri_context *ctx;
2157 DBG("%p, %p", gc, gc->gprs);
2159 if (gc->gprs == NULL)
2162 for (l = gc->gprs->contexts; l; l = l->next) {
2165 if (ctx->context_driver != gc)
2168 if (ctx->pending != NULL)
2169 __ofono_dbus_pending_reply(&ctx->pending,
2170 __ofono_error_failed(ctx->pending));
2172 if (ctx->active == FALSE)
2175 pri_reset_context_settings(ctx);
2176 release_context(ctx);
2179 ofono_dbus_signal_property_changed(conn, ctx->path,
2180 OFONO_CONNECTION_CONTEXT_INTERFACE,
2181 "Active", DBUS_TYPE_BOOLEAN, &value);
2184 gc->gprs->context_drivers = g_slist_remove(gc->gprs->context_drivers,
2190 context_settings_free(gc->settings);
2191 g_free(gc->settings);
2192 gc->settings = NULL;
2196 void ofono_gprs_add_context(struct ofono_gprs *gprs,
2197 struct ofono_gprs_context *gc)
2199 if (gc->driver == NULL)
2203 gc->settings = g_new0(struct context_settings, 1);
2205 gprs->context_drivers = g_slist_append(gprs->context_drivers, gc);
2206 __ofono_atom_register(gc->atom, gprs_context_unregister);
2209 void ofono_gprs_bearer_notify(struct ofono_gprs *gprs, int bearer)
2211 DBusConnection *conn = ofono_dbus_get_connection();
2215 if (gprs->bearer == bearer)
2218 gprs->bearer = bearer;
2219 path = __ofono_atom_get_path(gprs->atom);
2220 value = packet_bearer_to_string(bearer);
2221 ofono_dbus_signal_property_changed(conn, path,
2222 OFONO_CONNECTION_CONTEXT_INTERFACE,
2223 "Bearer", DBUS_TYPE_STRING, &value);
2226 void ofono_gprs_context_deactivated(struct ofono_gprs_context *gc,
2229 DBusConnection *conn = ofono_dbus_get_connection();
2231 struct pri_context *ctx;
2234 if (gc->gprs == NULL)
2237 for (l = gc->gprs->contexts; l; l = l->next) {
2240 if (ctx->context.cid != cid)
2243 if (ctx->active == FALSE)
2246 pri_reset_context_settings(ctx);
2247 release_context(ctx);
2250 ofono_dbus_signal_property_changed(conn, ctx->path,
2251 OFONO_CONNECTION_CONTEXT_INTERFACE,
2252 "Active", DBUS_TYPE_BOOLEAN, &value);
2256 int ofono_gprs_context_driver_register(const struct ofono_gprs_context_driver *d)
2258 DBG("driver: %p, name: %s", d, d->name);
2260 if (d->probe == NULL)
2263 g_context_drivers = g_slist_prepend(g_context_drivers, (void *) d);
2268 void ofono_gprs_context_driver_unregister(const struct ofono_gprs_context_driver *d)
2270 DBG("driver: %p, name: %s", d, d->name);
2272 g_context_drivers = g_slist_remove(g_context_drivers, (void *) d);
2275 static void gprs_context_remove(struct ofono_atom *atom)
2277 struct ofono_gprs_context *gc = __ofono_atom_get_data(atom);
2279 DBG("atom: %p", atom);
2284 if (gc->driver && gc->driver->remove)
2285 gc->driver->remove(gc);
2290 struct ofono_gprs_context *ofono_gprs_context_create(struct ofono_modem *modem,
2291 unsigned int vendor,
2292 const char *driver, void *data)
2294 struct ofono_gprs_context *gc;
2300 gc = g_try_new0(struct ofono_gprs_context, 1);
2304 gc->type = OFONO_GPRS_CONTEXT_TYPE_ANY;
2306 gc->atom = __ofono_modem_add_atom(modem, OFONO_ATOM_TYPE_GPRS_CONTEXT,
2307 gprs_context_remove, gc);
2309 for (l = g_context_drivers; l; l = l->next) {
2310 const struct ofono_gprs_context_driver *drv = l->data;
2312 if (g_strcmp0(drv->name, driver))
2315 if (drv->probe(gc, vendor, data) < 0)
2325 void ofono_gprs_context_remove(struct ofono_gprs_context *gc)
2330 __ofono_atom_free(gc->atom);
2333 void ofono_gprs_context_set_data(struct ofono_gprs_context *gc, void *data)
2335 gc->driver_data = data;
2338 void *ofono_gprs_context_get_data(struct ofono_gprs_context *gc)
2340 return gc->driver_data;
2343 struct ofono_modem *ofono_gprs_context_get_modem(struct ofono_gprs_context *gc)
2345 return __ofono_atom_get_modem(gc->atom);
2348 void ofono_gprs_context_set_type(struct ofono_gprs_context *gc,
2349 enum ofono_gprs_context_type type)
2351 DBG("type %d", type);
2356 void ofono_gprs_context_set_interface(struct ofono_gprs_context *gc,
2357 const char *interface)
2359 struct context_settings *settings = gc->settings;
2361 g_free(settings->interface);
2362 settings->interface = g_strdup(interface);
2365 void ofono_gprs_context_set_ipv4_address(struct ofono_gprs_context *gc,
2366 const char *address,
2369 struct context_settings *settings = gc->settings;
2371 if (settings->ipv4 == NULL)
2374 g_free(settings->ipv4->ip);
2375 settings->ipv4->ip = g_strdup(address);
2376 settings->ipv4->static_ip = static_ip;
2379 void ofono_gprs_context_set_ipv4_netmask(struct ofono_gprs_context *gc,
2380 const char *netmask)
2382 struct context_settings *settings = gc->settings;
2384 if (settings->ipv4 == NULL)
2387 g_free(settings->ipv4->netmask);
2388 settings->ipv4->netmask = g_strdup(netmask);
2391 void ofono_gprs_context_set_ipv4_gateway(struct ofono_gprs_context *gc,
2392 const char *gateway)
2394 struct context_settings *settings = gc->settings;
2396 if (settings->ipv4 == NULL)
2399 g_free(settings->ipv4->gateway);
2400 settings->ipv4->gateway = g_strdup(gateway);
2403 void ofono_gprs_context_set_ipv4_dns_servers(struct ofono_gprs_context *gc,
2406 struct context_settings *settings = gc->settings;
2408 if (settings->ipv4 == NULL)
2411 g_strfreev(settings->ipv4->dns);
2412 settings->ipv4->dns = g_strdupv((char **) dns);
2415 void ofono_gprs_context_set_ipv6_address(struct ofono_gprs_context *gc,
2416 const char *address)
2418 struct context_settings *settings = gc->settings;
2420 if (settings->ipv6 == NULL)
2423 g_free(settings->ipv6->ip);
2424 settings->ipv6->ip = g_strdup(address);
2427 void ofono_gprs_context_set_ipv6_prefix_length(struct ofono_gprs_context *gc,
2428 unsigned char length)
2430 struct context_settings *settings = gc->settings;
2432 if (settings->ipv6 == NULL)
2435 settings->ipv6->prefix_len = length;
2438 void ofono_gprs_context_set_ipv6_gateway(struct ofono_gprs_context *gc,
2439 const char *gateway)
2441 struct context_settings *settings = gc->settings;
2443 if (settings->ipv6 == NULL)
2446 g_free(settings->ipv6->gateway);
2447 settings->ipv6->gateway = g_strdup(gateway);
2450 void ofono_gprs_context_set_ipv6_dns_servers(struct ofono_gprs_context *gc,
2453 struct context_settings *settings = gc->settings;
2455 if (settings->ipv6 == NULL)
2458 g_strfreev(settings->ipv6->dns);
2459 settings->ipv6->dns = g_strdupv((char **) dns);
2462 int ofono_gprs_driver_register(const struct ofono_gprs_driver *d)
2464 DBG("driver: %p, name: %s", d, d->name);
2466 if (d->probe == NULL)
2469 g_drivers = g_slist_prepend(g_drivers, (void *)d);
2474 void ofono_gprs_driver_unregister(const struct ofono_gprs_driver *d)
2476 DBG("driver: %p, name: %s", d, d->name);
2478 g_drivers = g_slist_remove(g_drivers, (void *)d);
2481 static void free_contexts(struct ofono_gprs *gprs)
2485 if (gprs->settings) {
2486 storage_close(gprs->imsi, SETTINGS_STORE,
2487 gprs->settings, TRUE);
2491 gprs->settings = NULL;
2494 for (l = gprs->contexts; l; l = l->next) {
2495 struct pri_context *context = l->data;
2497 context_dbus_unregister(context);
2500 g_slist_free(gprs->contexts);
2503 static void gprs_unregister(struct ofono_atom *atom)
2505 DBusConnection *conn = ofono_dbus_get_connection();
2506 struct ofono_gprs *gprs = __ofono_atom_get_data(atom);
2507 struct ofono_modem *modem = __ofono_atom_get_modem(atom);
2508 const char *path = __ofono_atom_get_path(atom);
2512 free_contexts(gprs);
2514 if (gprs->cid_map) {
2515 idmap_free(gprs->cid_map);
2516 gprs->cid_map = NULL;
2519 if (gprs->netreg_watch) {
2520 if (gprs->status_watch) {
2521 __ofono_netreg_remove_status_watch(gprs->netreg,
2522 gprs->status_watch);
2523 gprs->status_watch = 0;
2526 __ofono_modem_remove_atom_watch(modem, gprs->netreg_watch);
2527 gprs->netreg_watch = 0;
2528 gprs->netreg = NULL;
2531 ofono_modem_remove_interface(modem,
2532 OFONO_CONNECTION_MANAGER_INTERFACE);
2533 g_dbus_unregister_interface(conn, path,
2534 OFONO_CONNECTION_MANAGER_INTERFACE);
2537 static void gprs_remove(struct ofono_atom *atom)
2539 struct ofono_gprs *gprs = __ofono_atom_get_data(atom);
2542 DBG("atom: %p", atom);
2547 if (gprs->suspend_timeout)
2548 g_source_remove(gprs->suspend_timeout);
2550 if (gprs->pid_map) {
2551 idmap_free(gprs->pid_map);
2552 gprs->pid_map = NULL;
2555 for (l = gprs->context_drivers; l; l = l->next) {
2556 struct ofono_gprs_context *gc = l->data;
2561 g_slist_free(gprs->context_drivers);
2563 if (gprs->driver && gprs->driver->remove)
2564 gprs->driver->remove(gprs);
2566 if (gprs->sim_context)
2567 ofono_sim_context_free(gprs->sim_context);
2572 struct ofono_gprs *ofono_gprs_create(struct ofono_modem *modem,
2573 unsigned int vendor,
2574 const char *driver, void *data)
2576 struct ofono_gprs *gprs;
2582 gprs = g_try_new0(struct ofono_gprs, 1);
2586 gprs->atom = __ofono_modem_add_atom(modem, OFONO_ATOM_TYPE_GPRS,
2589 for (l = g_drivers; l; l = l->next) {
2590 const struct ofono_gprs_driver *drv = l->data;
2592 if (g_strcmp0(drv->name, driver))
2595 if (drv->probe(gprs, vendor, data) < 0)
2602 gprs->status = NETWORK_REGISTRATION_STATUS_UNKNOWN;
2603 gprs->netreg_status = NETWORK_REGISTRATION_STATUS_UNKNOWN;
2604 gprs->pid_map = idmap_new(MAX_CONTEXTS);
2609 static void netreg_watch(struct ofono_atom *atom,
2610 enum ofono_atom_watch_condition cond,
2613 struct ofono_gprs *gprs = data;
2615 if (cond == OFONO_ATOM_WATCH_CONDITION_UNREGISTERED) {
2616 gprs_netreg_removed(gprs);
2620 gprs->netreg = __ofono_atom_get_data(atom);
2621 gprs->netreg_status = ofono_netreg_get_status(gprs->netreg);
2622 gprs->status_watch = __ofono_netreg_add_status_watch(gprs->netreg,
2623 netreg_status_changed, gprs, NULL);
2625 gprs_netreg_update(gprs);
2628 static gboolean load_context(struct ofono_gprs *gprs, const char *group)
2631 char *typestr = NULL;
2632 char *protostr = NULL;
2633 char *username = NULL;
2634 char *password = NULL;
2636 char *msgproxy = NULL;
2637 char *msgcenter = NULL;
2638 gboolean ret = FALSE;
2639 gboolean legacy = FALSE;
2640 struct pri_context *context;
2641 enum ofono_gprs_context_type type;
2642 enum ofono_gprs_proto proto;
2645 if (sscanf(group, "context%d", &id) != 1) {
2646 if (sscanf(group, "primarycontext%d", &id) != 1)
2652 if (id < 1 || id > MAX_CONTEXTS)
2655 name = g_key_file_get_string(gprs->settings, group, "Name", NULL);
2659 typestr = g_key_file_get_string(gprs->settings, group, "Type", NULL);
2660 if (typestr == NULL)
2663 if (gprs_context_string_to_type(typestr, &type) == FALSE)
2666 protostr = g_key_file_get_string(gprs->settings, group,
2668 if (protostr == NULL)
2669 protostr = g_strdup("ip");
2671 if (gprs_proto_from_string(protostr, &proto) == FALSE)
2674 username = g_key_file_get_string(gprs->settings, group,
2676 if (username == NULL)
2679 if (strlen(username) > OFONO_GPRS_MAX_USERNAME_LENGTH)
2682 password = g_key_file_get_string(gprs->settings, group,
2684 if (password == NULL)
2687 if (strlen(password) > OFONO_GPRS_MAX_PASSWORD_LENGTH)
2690 apn = g_key_file_get_string(gprs->settings, group,
2691 "AccessPointName", NULL);
2695 if (strlen(apn) > OFONO_GPRS_MAX_APN_LENGTH)
2698 if (type == OFONO_GPRS_CONTEXT_TYPE_MMS) {
2699 msgproxy = g_key_file_get_string(gprs->settings, group,
2700 "MessageProxy", NULL);
2702 msgcenter = g_key_file_get_string(gprs->settings, group,
2703 "MessageCenter", NULL);
2707 * Accept empty (just created) APNs, but don't allow other
2710 if (apn[0] != '\0' && is_valid_apn(apn) == FALSE)
2713 context = pri_context_create(gprs, name, type);
2714 if (context == NULL)
2717 idmap_take(gprs->pid_map, id);
2719 strcpy(context->context.username, username);
2720 strcpy(context->context.password, password);
2721 strcpy(context->context.apn, apn);
2722 context->context.proto = proto;
2724 if (msgproxy != NULL)
2725 strcpy(context->message_proxy, msgproxy);
2727 if (msgcenter != NULL)
2728 strcpy(context->message_center, msgcenter);
2730 if (context_dbus_register(context) == FALSE)
2733 gprs->last_context_id = id;
2735 gprs->contexts = g_slist_append(gprs->contexts, context);
2739 write_context_settings(gprs, context);
2740 g_key_file_remove_group(gprs->settings, group, NULL);
2756 static void gprs_load_settings(struct ofono_gprs *gprs, const char *imsi)
2759 gboolean legacy = FALSE;
2763 gprs->settings = storage_open(imsi, SETTINGS_STORE);
2765 if (gprs->settings == NULL)
2768 gprs->imsi = g_strdup(imsi);
2771 gprs->powered = g_key_file_get_boolean(gprs->settings, SETTINGS_GROUP,
2775 * If any error occurs, simply switch to defaults.
2776 * Default to Powered = True
2777 * and RoamingAllowed = False
2780 g_error_free(error);
2781 gprs->powered = TRUE;
2782 g_key_file_set_boolean(gprs->settings, SETTINGS_GROUP,
2783 "Powered", gprs->powered);
2787 gprs->roaming_allowed = g_key_file_get_boolean(gprs->settings,
2793 g_error_free(error);
2794 gprs->roaming_allowed = FALSE;
2795 g_key_file_set_boolean(gprs->settings, SETTINGS_GROUP,
2797 gprs->roaming_allowed);
2800 groups = g_key_file_get_groups(gprs->settings, NULL);
2802 for (i = 0; groups[i]; i++) {
2803 if (g_str_equal(groups[i], SETTINGS_GROUP))
2806 if (!g_str_has_prefix(groups[i], "context")) {
2807 if (!g_str_has_prefix(groups[i], "primarycontext"))
2813 if (load_context(gprs, groups[i]) == TRUE)
2817 g_key_file_remove_group(gprs->settings, groups[i], NULL);
2823 storage_sync(imsi, SETTINGS_STORE, gprs->settings);
2826 static void provision_context(const struct ofono_gprs_provision_data *ap,
2827 struct ofono_gprs *gprs)
2830 struct pri_context *context = NULL;
2836 if (ap->name && strlen(ap->name) > MAX_CONTEXT_NAME_LENGTH)
2839 if (ap->apn == NULL || strlen(ap->apn) > OFONO_GPRS_MAX_APN_LENGTH)
2842 if (is_valid_apn(ap->apn) == FALSE)
2846 strlen(ap->username) > OFONO_GPRS_MAX_USERNAME_LENGTH)
2850 strlen(ap->password) > OFONO_GPRS_MAX_PASSWORD_LENGTH)
2853 if (ap->message_proxy &&
2854 strlen(ap->message_proxy) > MAX_MESSAGE_PROXY_LENGTH)
2857 if (ap->message_center &&
2858 strlen(ap->message_center) > MAX_MESSAGE_CENTER_LENGTH)
2861 if (gprs->last_context_id)
2862 id = idmap_alloc_next(gprs->pid_map, gprs->last_context_id);
2864 id = idmap_alloc(gprs->pid_map);
2866 if (id > idmap_get_max(gprs->pid_map))
2869 context = pri_context_create(gprs, ap->name, ap->type);
2870 if (context == NULL) {
2871 idmap_put(gprs->pid_map, id);
2877 if (ap->username != NULL)
2878 strcpy(context->context.username, ap->username);
2880 if (ap->password != NULL)
2881 strcpy(context->context.password, ap->password);
2883 strcpy(context->context.apn, ap->apn);
2884 context->context.proto = ap->proto;
2886 if (ap->type == OFONO_GPRS_CONTEXT_TYPE_MMS) {
2887 if (ap->message_proxy != NULL)
2888 strcpy(context->message_proxy, ap->message_proxy);
2890 if (ap->message_center != NULL)
2891 strcpy(context->message_center, ap->message_center);
2894 if (context_dbus_register(context) == FALSE)
2897 gprs->last_context_id = id;
2899 if (gprs->settings) {
2900 write_context_settings(gprs, context);
2901 storage_sync(gprs->imsi, SETTINGS_STORE, gprs->settings);
2904 gprs->contexts = g_slist_append(gprs->contexts, context);
2907 static void provision_contexts(struct ofono_gprs *gprs, const char *mcc,
2908 const char *mnc, const char *spn)
2910 struct ofono_gprs_provision_data *settings;
2914 if (__ofono_gprs_provision_get_settings(mcc, mnc, spn,
2915 &settings, &count) == FALSE) {
2916 ofono_warn("Provisioning failed");
2920 for (i = 0; i < count; i++)
2921 provision_context(&settings[i], gprs);
2923 __ofono_gprs_provision_free_settings(settings, count);
2926 static void ofono_gprs_finish_register(struct ofono_gprs *gprs)
2928 DBusConnection *conn = ofono_dbus_get_connection();
2929 struct ofono_modem *modem = __ofono_atom_get_modem(gprs->atom);
2930 const char *path = __ofono_atom_get_path(gprs->atom);
2932 if (gprs->contexts == NULL) /* Automatic provisioning failed */
2933 add_context(gprs, NULL, OFONO_GPRS_CONTEXT_TYPE_INTERNET);
2935 if (!g_dbus_register_interface(conn, path,
2936 OFONO_CONNECTION_MANAGER_INTERFACE,
2937 manager_methods, manager_signals, NULL,
2939 ofono_error("Could not create %s interface",
2940 OFONO_CONNECTION_MANAGER_INTERFACE);
2942 free_contexts(gprs);
2946 ofono_modem_add_interface(modem,
2947 OFONO_CONNECTION_MANAGER_INTERFACE);
2949 gprs->netreg_watch = __ofono_modem_add_atom_watch(modem,
2950 OFONO_ATOM_TYPE_NETREG,
2951 netreg_watch, gprs, NULL);
2953 __ofono_atom_register(gprs->atom, gprs_unregister);
2956 static void sim_spn_read_cb(int ok, int length, int record,
2957 const unsigned char *data,
2958 int record_length, void *userdata)
2960 struct ofono_gprs *gprs = userdata;
2962 struct ofono_atom *sim_atom;
2963 struct ofono_sim *sim = NULL;
2966 spn = sim_string_to_utf8(data + 1, length - 1);
2968 sim_atom = __ofono_modem_find_atom(__ofono_atom_get_modem(gprs->atom),
2969 OFONO_ATOM_TYPE_SIM);
2971 sim = __ofono_atom_get_data(sim_atom);
2972 provision_contexts(gprs, ofono_sim_get_mcc(sim),
2973 ofono_sim_get_mnc(sim), spn);
2977 ofono_gprs_finish_register(gprs);
2980 void ofono_gprs_register(struct ofono_gprs *gprs)
2982 struct ofono_modem *modem = __ofono_atom_get_modem(gprs->atom);
2983 struct ofono_atom *sim_atom;
2984 struct ofono_sim *sim = NULL;
2986 sim_atom = __ofono_modem_find_atom(modem, OFONO_ATOM_TYPE_SIM);
2990 sim = __ofono_atom_get_data(sim_atom);
2992 imsi = ofono_sim_get_imsi(sim);
2993 gprs_load_settings(gprs, imsi);
2996 if (gprs->contexts == NULL && sim != NULL) {
2997 /* Get Service Provider Name from SIM for provisioning */
2998 gprs->sim_context = ofono_sim_context_create(sim);
3000 if (ofono_sim_read(gprs->sim_context, SIM_EFSPN_FILEID,
3001 OFONO_SIM_FILE_STRUCTURE_TRANSPARENT,
3002 sim_spn_read_cb, gprs) >= 0)
3006 ofono_gprs_finish_register(gprs);
3009 void ofono_gprs_remove(struct ofono_gprs *gprs)
3011 __ofono_atom_free(gprs->atom);
3014 void ofono_gprs_set_data(struct ofono_gprs *gprs, void *data)
3016 gprs->driver_data = data;
3019 void *ofono_gprs_get_data(struct ofono_gprs *gprs)
3021 return gprs->driver_data;