Add support for tizen.account which will be automotically populated
authorRusty Lynch <rusty.lynch@intel.com>
Fri, 17 Aug 2012 01:57:22 +0000 (18:57 -0700)
committerRusty Lynch <rusty.lynch@intel.com>
Fri, 17 Aug 2012 01:57:22 +0000 (18:57 -0700)
with a live list of enabled modems

Tizen.Device.js
homescreen.js
main.c

index 4cf9b8c..782e931 100644 (file)
@@ -79,7 +79,11 @@ var __callmanager = function () {
             else if (ev.State == "initialized")
                 for (var index in callHandlers)
                     callHandlers[index].onInitialized(activeCall);
-        }
+        } else if (ev.ModemAdded) {
+           tizen.account.addAccount(new tizen.Account(ev.ModemAdded, ev));
+       } else if (ev.ModemRemoved) {
+           tizen.account.deleteAccount(ev.ModemRemoved);
+       }
     }
 
     this.history = null;
@@ -201,6 +205,67 @@ var __call = function(manager, callService, wsi, data) {
     }
 }
 
+var __accountmanager = function() {
+    var listeners = new Array();
+    var accounts = new Array();
+
+    this.findServices = function(filter) {
+       /* brain dead matching, patches accepted */
+       var result = new Array();
+       if (accounts.length == 0)
+           return result;
+       try {
+           for (var i in accounts) {
+               var a = accounts[i];
+               for (var f in filter)
+                   if (a[f] && a[f] == filter[f]) {
+                       result.push(a);
+                       continue;
+                   }
+           }
+       } catch (err) {
+           console.log(err);
+       }
+
+       if (result.length == 0)
+           result.push(accounts[0]);
+
+       return result;
+    }
+    this.getAccountById = function(accountid) {
+       for (var i in accounts)
+           if (accounts[i].id == accountid)
+               return accounts[i];
+       return null;
+    }
+    this.deleteAccount = function(accountid) {
+       var l = new Array();
+       for (var i in accounts)
+           if (accountid != accounts[i].id)
+               l.push(accounts[i]);
+       accounts = l;
+
+       for (var i in listeners)
+           listeners[i].onAccountRemoved(accountid);   
+    }
+    this.addAccount = function(account) {
+       accounts.push(account);
+       for (var i in listeners)
+           listeners[i].onAccountAdded(account);
+    }
+    this.addAccountListener = function(observer, errorCallback) {
+       listeners.push(observer);
+       return listeners.length;
+    }
+    this.removeAccountListener = function(handle) {
+       var l = new Array();
+       for (var i in listeners)
+           if (i != handle)
+               l.push(listeners[i]);
+       listeners = l;
+    }
+}
+
 var __tizen = function() {
     var application = null;
     this.__defineGetter__("application", function() {
@@ -216,6 +281,24 @@ var __tizen = function() {
         return call;
     });
 
+    this.Account = function (accountId, accountProperties) {
+       var id = accountId;
+       var properties = accountProperties;
+       this.__defineGetter__("id", function() {
+           return id;
+       });
+       for (var p in properties) {
+           this.__defineGetter__(p, function() {
+               return properties[p];
+           });
+       }
+    }
+    var account = null;
+    this.__defineGetter__("account", function() {
+       if (account == null)
+           account = new __accountmanager();
+       return account;
+    });
 }
 
 var tizen = new __tizen();
index cc83615..f06c1b6 100644 (file)
@@ -18,6 +18,19 @@ function createAppEntry(index) {
 
 var activeCall = null;
 document.body.onload = function() {
+    var listener = { 
+       "onAccountUpdated": function(account) { 
+           console.log("Account updated: " + account.id); 
+       }, 
+       "onAccountAdded": function(account) { 
+           console.log("Account added " + account.id); 
+       }, 
+       "onAccountRemoved": function(id) {
+           console.log("Account Removed " + id); 
+       }
+    }
+    tizen.account.addAccountListener(listener);
+
     var handler = {
         onCallList: function (callList) {
             console.log("onCallList: " + callList);
diff --git a/main.c b/main.c
index 6e3715b..f046ff8 100644 (file)
--- a/main.c
+++ b/main.c
@@ -222,6 +222,24 @@ callback_dialer(struct libwebsocket_context *context,
         }
         break;
 
+    case LWS_CALLBACK_ESTABLISHED:
+        if (voicecallmanager_path) {
+            /* inform any connections about the addition of the modem */
+            json_t *o = json_object();
+            json_object_set(o, "ModemAdded", json_string(voicecallmanager_path));
+            char *dump = json_dumps(o, 0);
+
+            unsigned char *b = malloc(LWS_SEND_BUFFER_PRE_PADDING + 
+                                      strlen(dump) + 
+                                      LWS_SEND_BUFFER_POST_PADDING);
+            unsigned char *p = b + LWS_SEND_BUFFER_PRE_PADDING;
+            int n = sprintf((char *)p, "%s", dump);
+
+            libwebsocket_write(wsi, p, n, LWS_WRITE_TEXT);
+            free(b);
+            free(dump);
+        }
+
     case LWS_CALLBACK_RECEIVE:
         /* 
          * The other end of the websocket is sending us text
@@ -460,6 +478,48 @@ void call_properties_changed_cb(void *data, DBusMessage *msg)
     free(dump);
 }
 
+void modem_properties_changed_cb(void *data, DBusMessage *msg)
+{
+    DBusMessageIter iter, values, entry;
+    const char *property, *v;
+    char *dump;
+    int n;
+    json_t *object;
+
+    dbus_message_iter_init(msg, &iter);
+    dbus_message_iter_get_basic(&iter, &property);
+
+    if (!strcasecmp(property, "Interfaces")) {
+        dbus_message_iter_next(&iter);
+        dbus_message_iter_recurse(&iter, &values);
+
+        while (dbus_message_iter_get_arg_type(&values) == DBUS_TYPE_ARRAY) {
+            DBusMessageIter variant;
+            dbus_message_iter_recurse(&iter, &entry);
+            dbus_message_iter_recurse(&entry, &variant);
+            if (dbus_message_iter_get_arg_type(&variant) != DBUS_TYPE_INVALID) {
+                dbus_message_iter_get_basic(&variant, &v);
+                if (!strcasecmp(v, "org.ofono.VoiceCallManager")) {
+                    object = json_object();
+                    json_object_set(object, "ModemAdded", json_string(dbus_message_get_path(msg)));
+                    dump = json_dumps(object, 0);
+                    libwebsockets_broadcast(&protocols[PROTOCOL_DIALER], dump, strlen(dump));
+                    free(dump);
+                }
+            } else {
+                // If the modem has removed all interfaces then its no longer
+                // available for making calls
+                object = json_object();
+                json_object_set(object, "ModemRemoved", json_string(dbus_message_get_path(msg)));
+                dump = json_dumps(object, 0);
+                libwebsockets_broadcast(&protocols[PROTOCOL_DIALER], dump, strlen(dump));
+                free(dump);                
+            }
+            dbus_message_iter_next(&values);
+        }
+    }
+}
+
 void call_added_cb(void *data, DBusMessage *msg)
 {
     DBusMessageIter iter, properties;
@@ -512,6 +572,36 @@ void call_removed_cb(void *data, DBusMessage *msg)
     free(dump);
 }
 
+void modem_added_cb(void *data, DBusMessage *msg)
+{
+    DBusMessageIter iter;
+    const char *path;
+    
+    dbus_message_iter_init(msg, &iter);
+    dbus_message_iter_get_basic(&iter, &path);
+
+    json_t *object = json_object();
+    json_object_set(object, "ModemAdded", json_string(path));
+    char *dump = json_dumps(object, 0);
+    libwebsockets_broadcast(&protocols[PROTOCOL_DIALER], dump, strlen(dump));
+    free(dump);    
+}
+
+void modem_removed_cb(void *data, DBusMessage *msg)
+{
+    DBusMessageIter iter;
+    const char *path;
+
+    dbus_message_iter_init(msg, &iter);
+    dbus_message_iter_get_basic(&iter, &path);
+
+    json_t *object = json_object();
+    json_object_set(object, "ModemRemoved", json_string(path));
+    char *dump = json_dumps(object, 0);
+    libwebsockets_broadcast(&protocols[PROTOCOL_DIALER], dump, strlen(dump));
+    free(dump);
+}
+
 void
 get_modems_reply(void *data, DBusMessage *reply, DBusError *error)
 {
@@ -536,6 +626,18 @@ get_modems_reply(void *data, DBusMessage *reply, DBusError *error)
         dbus_message_iter_recurse(&entry, &item);
         dbus_message_iter_get_basic(&item, &path);
 
+        printf("registering %s\n", path);
+        e_dbus_signal_handler_add(bus, "org.ofono", 
+                                  path, "org.ofono.Modem", "PropertyChanged", 
+                                  modem_properties_changed_cb, (void *)strdup(path));
+        e_dbus_signal_handler_add(bus, "org.ofono", 
+                                  path, "org.ofono.VoiceCallManager", "CallAdded", 
+                                  call_added_cb, NULL);
+        e_dbus_signal_handler_add(bus, "org.ofono", 
+                                  path, "org.ofono.VoiceCallManager", 
+                                  "CallRemoved", 
+                                  call_removed_cb, NULL);
+
         dbus_message_iter_next(&item);
         dbus_message_iter_recurse(&item, &properties);
 
@@ -562,13 +664,12 @@ get_modems_reply(void *data, DBusMessage *reply, DBusError *error)
                             free(voicecallmanager_path);
                         voicecallmanager_path = strdup(path);
 
-                        e_dbus_signal_handler_add(bus, "org.ofono", 
-                                                  path, iface, "CallAdded", 
-                                                  call_added_cb, NULL);
-                        e_dbus_signal_handler_add(bus, "org.ofono", 
-                                                  path, iface, 
-                                                  "CallRemoved", 
-                                                  call_removed_cb, NULL);
+                        /* inform any connections about the addition of the modem */
+                        json_t *o = json_object();
+                        json_object_set(o, "ModemAdded", json_string(path));
+                        char *dump = json_dumps(o, 0);
+                        libwebsockets_broadcast(&protocols[PROTOCOL_DIALER], dump, strlen(dump));
+                        free(dump);
                     }
 
                     dbus_message_iter_next(&interfaces);
@@ -639,6 +740,11 @@ int main(int argc, char **argv)
                                        );
     e_dbus_message_send(bus, msg, get_modems_reply, -1, NULL);
 
+    e_dbus_signal_handler_add(bus, "org.ofono", "/", "org.ofono.Manager", "ModemAdded", 
+                              modem_added_cb, NULL);
+    e_dbus_signal_handler_add(bus, "org.ofono", "/", "org.ofono.Manager", "ModemRemoved", 
+                              modem_removed_cb, NULL);
+
     /* Listen for updates on application desktop files*/
     fd = inotify_init();
     inotify_add_watch(fd, APPDIR, IN_CREATE | IN_DELETE | IN_MODIFY);