3 * oFono - Open Source Telephony
5 * Copyright (C) 2010-2011 Nokia Corporation and/or its subsidiary(-ies).
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>
41 static GSList *g_drivers;
43 struct cdma_connman_settings {
52 struct ofono_cdma_connman {
55 struct cdma_connman_settings *settings;
57 const struct ofono_cdma_connman_driver *driver;
59 struct ofono_atom *atom;
60 char username[OFONO_CDMA_CONNMAN_MAX_USERNAME_LENGTH + 1];
61 char password[OFONO_CDMA_CONNMAN_MAX_PASSWORD_LENGTH + 1];
64 static void cdma_connman_settings_free(struct cdma_connman_settings *settings)
68 g_free(settings->interface);
70 g_free(settings->netmask);
71 g_free(settings->gateway);
72 g_strfreev(settings->dns);
77 static void cdma_connman_ifupdown(const char *interface, ofono_bool_t active)
84 if (interface == NULL)
87 sk = socket(PF_INET, SOCK_DGRAM, 0);
91 memset(&ifr, 0, sizeof(ifr));
92 strncpy(ifr.ifr_name, interface, IFNAMSIZ);
94 if (ioctl(sk, SIOCGIFFLAGS, &ifr) < 0)
98 if (ifr.ifr_flags & IFF_UP)
100 ifr.ifr_flags |= IFF_UP;
102 if (!(ifr.ifr_flags & IFF_UP))
104 ifr.ifr_flags &= ~IFF_UP;
107 if (ioctl(sk, SIOCSIFFLAGS, &ifr) < 0)
108 ofono_error("Failed to change interface flags");
114 static void cdma_connman_settings_append_variant(
115 struct cdma_connman_settings *settings,
116 DBusMessageIter *iter)
118 DBusMessageIter variant;
119 DBusMessageIter array;
126 arraysig[0] = DBUS_TYPE_ARRAY;
127 arraysig[1] = typesig[0] = DBUS_DICT_ENTRY_BEGIN_CHAR;
128 arraysig[2] = typesig[1] = DBUS_TYPE_STRING;
129 arraysig[3] = typesig[2] = DBUS_TYPE_VARIANT;
130 arraysig[4] = typesig[3] = DBUS_DICT_ENTRY_END_CHAR;
131 arraysig[5] = typesig[4] = '\0';
133 dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT,
136 dbus_message_iter_open_container(&variant, DBUS_TYPE_ARRAY,
139 if (settings == NULL)
142 ofono_dbus_dict_append(&array, "Interface",
143 DBUS_TYPE_STRING, &settings->interface);
145 if (settings->static_ip == TRUE)
150 ofono_dbus_dict_append(&array, "Method", DBUS_TYPE_STRING, &method);
153 ofono_dbus_dict_append(&array, "Address", DBUS_TYPE_STRING,
156 if (settings->netmask)
157 ofono_dbus_dict_append(&array, "Netmask", DBUS_TYPE_STRING,
160 if (settings->gateway)
161 ofono_dbus_dict_append(&array, "Gateway", DBUS_TYPE_STRING,
165 ofono_dbus_dict_append_array(&array, "DomainNameServers",
170 dbus_message_iter_close_container(&variant, &array);
172 dbus_message_iter_close_container(iter, &variant);
175 static void cdma_connman_settings_signal(struct ofono_cdma_connman *cm)
177 DBusConnection *conn = ofono_dbus_get_connection();
180 DBusMessageIter iter;
181 const char *prop = "Settings";
185 path = __ofono_atom_get_path(cm->atom);
187 signal = dbus_message_new_signal(path,
188 OFONO_CDMA_CONNECTION_MANAGER_INTERFACE,
193 dbus_message_iter_init_append(signal, &iter);
195 dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &prop);
197 cdma_connman_settings_append_variant(cm->settings, &iter);
199 g_dbus_send_message(conn, signal);
202 static void cdma_connman_settings_update(struct ofono_cdma_connman *cm,
203 const char *interface,
204 ofono_bool_t static_ip,
205 const char *ip, const char *netmask,
206 const char *gateway, const char **dns)
211 cdma_connman_settings_free(cm->settings);
213 cm->settings = g_try_new0(struct cdma_connman_settings, 1);
214 if (cm->settings == NULL)
217 cm->settings->interface = g_strdup(interface);
218 cm->settings->static_ip = static_ip;
219 cm->settings->ip = g_strdup(ip);
220 cm->settings->netmask = g_strdup(netmask);
221 cm->settings->gateway = g_strdup(gateway);
222 cm->settings->dns = g_strdupv((char **)dns);
224 cdma_connman_ifupdown(interface, TRUE);
226 cdma_connman_settings_signal(cm);
229 static void cdma_connman_settings_reset(struct ofono_cdma_connman *cm)
235 if (cm->settings == NULL)
238 interface = cm->settings->interface;
239 cm->settings->interface = NULL;
241 cdma_connman_settings_free(cm->settings);
244 cdma_connman_settings_signal(cm);
246 cdma_connman_ifupdown(interface, FALSE);
251 static void activate_callback(const struct ofono_error *error,
252 const char *interface,
253 ofono_bool_t static_ip,
254 const char *ip, const char *netmask,
255 const char *gateway, const char **dns,
258 DBusConnection *conn = ofono_dbus_get_connection();
259 struct ofono_cdma_connman *cm = data;
263 DBG("%p %s", cm, interface);
265 if (error->type != OFONO_ERROR_TYPE_NO_ERROR) {
266 DBG("Activating packet data service failed with error: %s",
267 telephony_error_to_str(error));
268 __ofono_dbus_pending_reply(&cm->pending,
269 __ofono_error_failed(cm->pending));
274 __ofono_dbus_pending_reply(&cm->pending,
275 dbus_message_new_method_return(cm->pending));
278 * If we don't have the interface, don't bother emitting any settings,
279 * as nobody can make use of them
281 if (interface != NULL)
282 cdma_connman_settings_update(cm, interface, static_ip,
283 ip, netmask, gateway, dns);
285 path = __ofono_atom_get_path(cm->atom);
287 ofono_dbus_signal_property_changed(conn, path,
288 OFONO_CDMA_CONNECTION_MANAGER_INTERFACE,
289 "Powered", DBUS_TYPE_BOOLEAN, &value);
292 static void deactivate_callback(const struct ofono_error *error, void *data)
294 DBusConnection *conn = ofono_dbus_get_connection();
295 struct ofono_cdma_connman *cm = data;
301 if (error->type != OFONO_ERROR_TYPE_NO_ERROR) {
302 DBG("Deactivating packet data service failed with error: %s",
303 telephony_error_to_str(error));
304 __ofono_dbus_pending_reply(&cm->pending,
305 __ofono_error_failed(cm->pending));
310 __ofono_dbus_pending_reply(&cm->pending,
311 dbus_message_new_method_return(cm->pending));
313 cdma_connman_settings_reset(cm);
315 path = __ofono_atom_get_path(cm->atom);
317 ofono_dbus_signal_property_changed(conn, path,
318 OFONO_CDMA_CONNECTION_MANAGER_INTERFACE,
319 "Powered", DBUS_TYPE_BOOLEAN, &value);
322 static void cdma_connman_settings_append_properties(
323 struct ofono_cdma_connman *cm,
324 DBusMessageIter *dict)
326 DBusMessageIter entry;
327 const char *key = "Settings";
331 dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY,
334 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
336 cdma_connman_settings_append_variant(cm->settings, &entry);
338 dbus_message_iter_close_container(dict, &entry);
341 static ofono_bool_t network_registered(struct ofono_cdma_connman *cm)
344 struct ofono_modem *modem = __ofono_atom_get_modem(cm->atom);
345 struct ofono_cdma_netreg *cdma_netreg;
347 cdma_netreg = __ofono_atom_find(OFONO_ATOM_TYPE_CDMA_NETREG, modem);
348 if (cdma_netreg == NULL)
351 status = ofono_cdma_netreg_get_status(cdma_netreg);
354 case NETWORK_REGISTRATION_STATUS_REGISTERED:
355 case NETWORK_REGISTRATION_STATUS_ROAMING:
364 static DBusMessage *cdma_connman_get_properties(DBusConnection *conn,
365 DBusMessage *msg, void *data)
367 struct ofono_cdma_connman *cm = data;
369 DBusMessageIter iter;
370 DBusMessageIter dict;
375 reply = dbus_message_new_method_return(msg);
379 dbus_message_iter_init_append(reply, &iter);
381 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
382 OFONO_PROPERTIES_ARRAY_SIGNATURE,
386 ofono_dbus_dict_append(&dict, "Powered", DBUS_TYPE_BOOLEAN, &value);
389 ofono_dbus_dict_append(&dict, "Dormant", DBUS_TYPE_BOOLEAN, &value);
392 cdma_connman_settings_append_properties(cm, &dict);
394 dbus_message_iter_close_container(&iter, &dict);
399 static DBusMessage *cdma_connman_set_username(struct ofono_cdma_connman *cm,
400 DBusConnection *conn, DBusMessage *msg,
401 const char *username)
405 if (strlen(username) > OFONO_CDMA_CONNMAN_MAX_USERNAME_LENGTH)
406 return __ofono_error_invalid_format(msg);
408 if (g_str_equal(username, cm->username))
409 return dbus_message_new_method_return(msg);
411 strcpy(cm->username, username);
413 g_dbus_send_reply(conn, msg, DBUS_TYPE_INVALID);
415 path = __ofono_atom_get_path(cm->atom);
416 ofono_dbus_signal_property_changed(conn, path,
417 OFONO_CDMA_CONNECTION_MANAGER_INTERFACE,
418 "Username", DBUS_TYPE_STRING, &username);
423 static DBusMessage *cdma_connman_set_password(struct ofono_cdma_connman *cm,
424 DBusConnection *conn, DBusMessage *msg,
425 const char *password)
429 if (strlen(password) > OFONO_CDMA_CONNMAN_MAX_PASSWORD_LENGTH)
430 return __ofono_error_invalid_format(msg);
432 if (g_str_equal(password, cm->password))
433 return dbus_message_new_method_return(msg);
435 strcpy(cm->password, password);
437 g_dbus_send_reply(conn, msg, DBUS_TYPE_INVALID);
439 path = __ofono_atom_get_path(cm->atom);
440 ofono_dbus_signal_property_changed(conn, path,
441 OFONO_CDMA_CONNECTION_MANAGER_INTERFACE,
442 "Password", DBUS_TYPE_STRING, &password);
447 static DBusMessage *cdma_connman_set_property(DBusConnection *conn,
448 DBusMessage *msg, void *data)
450 struct ofono_cdma_connman *cm = data;
451 DBusMessageIter iter;
453 const char *property;
460 return __ofono_error_busy(msg);
462 if (!dbus_message_iter_init(msg, &iter))
463 return __ofono_error_invalid_args(msg);
465 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
466 return __ofono_error_invalid_args(msg);
468 dbus_message_iter_get_basic(&iter, &property);
469 dbus_message_iter_next(&iter);
471 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
472 return __ofono_error_invalid_args(msg);
474 dbus_message_iter_recurse(&iter, &var);
476 if (!strcmp(property, "Powered")) {
477 if (dbus_message_iter_get_arg_type(&var) != DBUS_TYPE_BOOLEAN)
478 return __ofono_error_invalid_args(msg);
480 dbus_message_iter_get_basic(&var, &value);
482 if (cm->powered == (ofono_bool_t) value)
483 return dbus_message_new_method_return(msg);
485 if (cm->driver == NULL || cm->driver->activate == NULL ||
486 cm->driver->deactivate == NULL)
487 return __ofono_error_not_implemented(msg);
489 if (network_registered(cm) == FALSE)
490 return __ofono_error_not_registered(msg);
492 cm->pending = dbus_message_ref(msg);
495 cm->driver->activate(cm, cm->username, cm->password,
496 activate_callback, cm);
498 cm->driver->deactivate(cm, deactivate_callback, cm);
501 } else if (!strcmp(property, "Username")) {
502 if (dbus_message_iter_get_arg_type(&var) != DBUS_TYPE_STRING)
503 return __ofono_error_invalid_args(msg);
505 dbus_message_iter_get_basic(&var, &str);
506 return cdma_connman_set_username(cm, conn, msg, str);
507 } else if (!strcmp(property, "Password")) {
508 if (dbus_message_iter_get_arg_type(&var) != DBUS_TYPE_STRING)
509 return __ofono_error_invalid_args(msg);
511 dbus_message_iter_get_basic(&var, &str);
512 return cdma_connman_set_password(cm, conn, msg, str);
515 /* TODO: Dormant property. Not yet supported. */
517 return __ofono_error_invalid_args(msg);
520 static const GDBusMethodTable cdma_connman_methods[] = {
521 { GDBUS_METHOD("GetProperties",
522 NULL, GDBUS_ARGS({ "properties", "a{sv}" }),
523 cdma_connman_get_properties) },
524 { GDBUS_ASYNC_METHOD("SetProperty",
525 GDBUS_ARGS({ "property", "s" }, { "value", "v" }),
526 NULL, cdma_connman_set_property) },
530 static const GDBusSignalTable cdma_connman_signals[] = {
531 { GDBUS_SIGNAL("PropertyChanged",
532 GDBUS_ARGS({ "name", "s" }, { "value", "v" })) },
536 int ofono_cdma_connman_driver_register(
537 const struct ofono_cdma_connman_driver *d)
539 DBG("driver: %p, name: %s", d, d->name);
541 if (d->probe == NULL)
544 g_drivers = g_slist_prepend(g_drivers, (void *) d);
549 void ofono_cdma_connman_driver_unregister(
550 const struct ofono_cdma_connman_driver *d)
552 DBG("driver: %p, name: %s", d, d->name);
554 g_drivers = g_slist_remove(g_drivers, (void *) d);
557 void ofono_cdma_connman_deactivated(struct ofono_cdma_connman *cm)
559 DBusConnection *conn = ofono_dbus_get_connection();
566 cdma_connman_settings_reset(cm);
569 path = __ofono_atom_get_path(cm->atom);
571 ofono_dbus_signal_property_changed(conn, path,
572 OFONO_CDMA_CONNECTION_MANAGER_INTERFACE,
573 "Powered", DBUS_TYPE_BOOLEAN, &value);
576 void ofono_cdma_connman_dormant_notify(struct ofono_cdma_connman *cm,
577 ofono_bool_t dormant)
579 DBusConnection *conn = ofono_dbus_get_connection();
585 cm->dormant = dormant;
586 path = __ofono_atom_get_path(cm->atom);
588 ofono_dbus_signal_property_changed(conn, path,
589 OFONO_CDMA_CONNECTION_MANAGER_INTERFACE,
590 "Dormant", DBUS_TYPE_BOOLEAN, &dormant);
593 static void cdma_connman_unregister(struct ofono_atom *atom)
595 DBusConnection *conn = ofono_dbus_get_connection();
596 struct ofono_modem *modem = __ofono_atom_get_modem(atom);
597 const char *path = __ofono_atom_get_path(atom);
601 g_dbus_unregister_interface(conn, path,
602 OFONO_CDMA_CONNECTION_MANAGER_INTERFACE);
603 ofono_modem_remove_interface(modem,
604 OFONO_CDMA_CONNECTION_MANAGER_INTERFACE);
607 static void cdma_connman_remove(struct ofono_atom *atom)
609 struct ofono_cdma_connman *cm = __ofono_atom_get_data(atom);
611 DBG("atom: %p", atom);
616 if (cm->driver && cm->driver->remove)
617 cm->driver->remove(cm);
622 struct ofono_cdma_connman *ofono_cdma_connman_create(
623 struct ofono_modem *modem,
628 struct ofono_cdma_connman *cm;
636 cm = g_try_new0(struct ofono_cdma_connman, 1);
640 cm->atom = __ofono_modem_add_atom(modem,
641 OFONO_ATOM_TYPE_CDMA_CONNMAN,
642 cdma_connman_remove, cm);
644 for (l = g_drivers; l; l = l->next) {
645 const struct ofono_cdma_connman_driver *drv = l->data;
647 if (g_strcmp0(drv->name, driver))
650 if (drv->probe(cm, vendor, data) < 0)
660 void ofono_cdma_connman_register(struct ofono_cdma_connman *cm)
662 DBusConnection *conn = ofono_dbus_get_connection();
663 struct ofono_modem *modem = __ofono_atom_get_modem(cm->atom);
664 const char *path = __ofono_atom_get_path(cm->atom);
668 if (!g_dbus_register_interface(conn, path,
669 OFONO_CDMA_CONNECTION_MANAGER_INTERFACE,
670 cdma_connman_methods, cdma_connman_signals,
672 ofono_error("Could not create %s interface",
673 OFONO_CDMA_CONNECTION_MANAGER_INTERFACE);
677 ofono_modem_add_interface(modem,
678 OFONO_CDMA_CONNECTION_MANAGER_INTERFACE);
680 __ofono_atom_register(cm->atom, cdma_connman_unregister);
683 void ofono_cdma_connman_remove(struct ofono_cdma_connman *cm)
685 __ofono_atom_free(cm->atom);
688 void ofono_cdma_connman_set_data(struct ofono_cdma_connman *cm, void *data)
690 cm->driver_data = data;
693 void *ofono_cdma_connman_get_data(struct ofono_cdma_connman *cm)
695 return cm->driver_data;