Darin Petkov | 16f6349 | 2012-10-23 11:12:58 +0200 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | // Shim to set Cellular.APN property for a service. This exists because |
| 6 | // dbus-send isn't capable of sending anything with nested containers, such as a |
| 7 | // variant that is a dict. |
| 8 | |
| 9 | #ifdef HAVE_CONFIG_H |
| 10 | #include <config.h> |
| 11 | #endif |
| 12 | |
| 13 | #include <stdio.h> |
| 14 | #include <stdlib.h> |
| 15 | #include <string.h> |
| 16 | |
| 17 | #include <dbus/dbus.h> |
| 18 | |
| 19 | #define CONNMAN_SERVICE "org.chromium.flimflam" |
| 20 | |
| 21 | #define CONNMAN_SERVICE_INTERFACE CONNMAN_SERVICE ".Service" |
| 22 | |
| 23 | static void append(DBusMessageIter *dict, |
| 24 | const char *key, |
| 25 | const char *value) |
| 26 | { |
| 27 | DBusMessageIter entry; |
| 28 | |
| 29 | dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY, |
| 30 | NULL, &entry); |
| 31 | |
| 32 | dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key); |
| 33 | |
| 34 | dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &value); |
| 35 | |
| 36 | dbus_message_iter_close_container(dict, &entry); |
| 37 | } |
| 38 | |
| 39 | int main(int argc, char *argv[]) |
| 40 | { |
| 41 | DBusConnection *conn; |
| 42 | DBusError error; |
| 43 | DBusMessage *msg; |
| 44 | DBusMessageIter iter, value, dict; |
| 45 | const char *property; |
| 46 | char *args; |
| 47 | char *cp; |
| 48 | char *argname; |
| 49 | char *argvalue; |
| 50 | int done; |
| 51 | |
| 52 | |
| 53 | if (argc < 4) { |
| 54 | fprintf(stderr, |
| 55 | "Usage: %s <service-dbus-path> <property-name> <apn-args>\n", |
| 56 | argv[0]); |
| 57 | return 1; |
| 58 | } |
| 59 | |
| 60 | dbus_error_init(&error); |
| 61 | |
| 62 | conn = dbus_bus_get(DBUS_BUS_SYSTEM, &error); |
| 63 | if (conn == NULL) { |
| 64 | if (dbus_error_is_set(&error) == TRUE) { |
| 65 | fprintf(stderr, "%s\n", error.message); |
| 66 | dbus_error_free(&error); |
| 67 | } else |
| 68 | fprintf(stderr, "Failed to get on system bus\n"); |
| 69 | return 1; |
| 70 | } |
| 71 | |
| 72 | msg = dbus_message_new_method_call(CONNMAN_SERVICE, |
| 73 | argv[1], |
| 74 | CONNMAN_SERVICE_INTERFACE, |
| 75 | "SetProperty"); |
| 76 | if (msg == NULL) { |
| 77 | dbus_connection_unref(conn); |
| 78 | fprintf(stderr, "Failed to allocate method call\n"); |
| 79 | return 1; |
| 80 | } |
| 81 | |
| 82 | dbus_message_set_no_reply(msg, TRUE); |
| 83 | |
| 84 | dbus_message_iter_init_append(msg, &iter); |
| 85 | |
| 86 | property = argv[2]; |
| 87 | dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &property); |
| 88 | |
| 89 | dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT, |
| 90 | DBUS_TYPE_ARRAY_AS_STRING |
| 91 | DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING |
| 92 | DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_STRING_AS_STRING |
| 93 | DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &value); |
| 94 | |
| 95 | dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY, |
| 96 | DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING |
| 97 | DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_STRING_AS_STRING |
| 98 | DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict); |
| 99 | |
| 100 | args = argv[3]; |
| 101 | cp = args; |
| 102 | done = 0; |
| 103 | |
| 104 | while (!done) { |
| 105 | argname = cp; |
| 106 | cp = strchr(cp, ','); |
| 107 | if (cp == NULL) { |
| 108 | fprintf(stderr, "Badly formed argument string\n"); |
| 109 | dbus_message_unref(msg); |
| 110 | dbus_connection_unref(conn); |
| 111 | return 1; |
| 112 | } |
| 113 | *cp++ = '\0'; |
| 114 | argvalue = cp; |
| 115 | cp = strchr(cp, ','); |
| 116 | if (cp == NULL) |
| 117 | done = 1; |
| 118 | else |
| 119 | *cp++ = '\0'; |
| 120 | append(&dict, argname, argvalue); |
| 121 | } |
| 122 | |
| 123 | dbus_message_iter_close_container(&iter, &dict); |
| 124 | dbus_message_iter_close_container(&dict, &value); |
| 125 | |
| 126 | dbus_connection_send(conn, msg, NULL); |
| 127 | |
| 128 | dbus_message_unref(msg); |
| 129 | dbus_connection_unref(conn); |
| 130 | |
| 131 | return 0; |
| 132 | } |