7 #include <libwebsockets.h>
10 #include <sys/inotify.h>
11 #include <sys/types.h>
14 enum sockdrawer_protocols {
18 SOCKDRAWER_PROTOCOL_COUNT
21 #ifdef INSTALL_DATADIR
22 #define LOCAL_RESOURCE_PATH INSTALL_DATADIR"/sockdrawer"
24 #define LOCAL_RESOURCE_PATH "."
27 #define DEFAULTCONTENT LOCAL_RESOURCE_PATH"/index.html"
28 #define DEFAULTICON LOCAL_RESOURCE_PATH"/default.png"
30 #define APPDIR "/usr/share/applications"
32 struct libwebsocket_context *context;
34 E_DBus_Connection *bus;
36 json_t *app_list = NULL;
37 char *apps_directory = NULL;
39 char *content_directory = NULL;
41 json_t *event_queue = NULL;
43 char *voicecallmanager_path = NULL;
45 #define MAX_POLL_ELEMENTS 100
46 struct pollfd pollfds[100];
47 int count_pollfds = 0;
49 void handle_command(json_t *o);
50 static int callback_http(struct libwebsocket_context *context,
51 struct libwebsocket *wsi,
52 enum libwebsocket_callback_reasons reason, void *user,
53 void *in, size_t len);
54 void send_app_list(struct libwebsocket *socket);
55 static int callback_app_list(struct libwebsocket_context *context,
56 struct libwebsocket *wsi,
57 enum libwebsocket_callback_reasons reason,
58 void *user, void *in, size_t len);
59 static Eina_Bool get_websocket_event(void *data);
60 static int callback_dialer(struct libwebsocket_context *context,
61 struct libwebsocket *wsi,
62 enum libwebsocket_callback_reasons reason,
63 void *user, void *in, size_t len);
65 void set_powered_cb(void *data, DBusMessage *reply, DBusError *error);
66 static Eina_Bool power_on_modem(void *data);
71 struct per_session_data__app_list {
74 struct per_session_data__dialer {
78 static struct libwebsocket_protocols protocols[] = {
87 sizeof(struct per_session_data__app_list),
92 sizeof(struct per_session_data__dialer),
99 /* libwebsocket callback for handling normal http request. We use this */
100 /* to implementa a rudamentary webserver. */
102 callback_http(struct libwebsocket_context *context,
103 struct libwebsocket *wsi,
104 enum libwebsocket_callback_reasons reason, void *user,
105 void *in, size_t len)
107 char client_name[128];
114 case LWS_CALLBACK_HTTP:
115 if (in && ((char *)in)[0] == '/' && !strstr(in, "..")) {
116 if (strstr(in, "/icon/") == in) {
117 /* virtual directory used for serving up system icons */
119 char *icon = (char *)(in + 6);
120 const char *path = efreet_icon_path_find(getenv("E_ICON_THEME"),
121 (const char *)icon, 128);
123 path = efreet_icon_path_find("default", icon, 128);
125 path = efreet_icon_path_find("hicolor", icon, 128);
127 if (icon[0] == '/' && !stat(icon, &info) && S_ISREG(info.st_mode))
130 fprintf(stderr, "Can not find icon: %s\n", icon);
137 libwebsockets_serve_http_file(wsi, path,
138 (const char *)efreet_mime_type_get((char *)path));
140 snprintf(filename, 256, "%s/%s", content_directory, (char *)in + 1);
141 if (!stat(filename, &info) && S_ISDIR(info.st_mode))
142 /* try serving up index.html */
143 snprintf(filename, 256, "%s/%s/index.html",
144 content_directory, (char *)in + 1);
146 if (!stat(filename, &info) && S_ISREG(info.st_mode)) {
147 libwebsockets_serve_http_file(wsi, filename,
148 (const char *)efreet_mime_type_get(filename));
150 /* TODO: Serve up error html page */
151 fprintf(stderr, "Unable to find %s\n", filename);
155 fprintf(stderr, "Invalid request \'%s\'\n", (char *)in);
159 case LWS_CALLBACK_ADD_POLL_FD:
160 pollfds[count_pollfds].fd = (int)(long)user;
161 pollfds[count_pollfds].events = (int)len;
162 pollfds[count_pollfds++].revents = 0;
165 case LWS_CALLBACK_DEL_POLL_FD:
166 for (n = 0; n < count_pollfds; n++)
167 if (pollfds[n].fd == (int)(long)user)
168 while (n < count_pollfds) {
169 pollfds[n] = pollfds[n + 1];
175 case LWS_CALLBACK_SET_MODE_POLL_FD:
176 for (n = 0; n < count_pollfds; n++)
177 if (pollfds[n].fd == (int)(long)user)
178 pollfds[n].events |= (int)(long)len;
181 case LWS_CALLBACK_CLEAR_MODE_POLL_FD:
182 for (n = 0; n < count_pollfds; n++)
183 if (pollfds[n].fd == (int)(long)user)
184 pollfds[n].events &= ~(int)(long)len;
195 dial_reply(void *data, DBusMessage *reply, DBusError *error)
197 if (dbus_error_is_set(error)) {
198 fprintf(stderr, "Error: %s - %s\n", error->name, error->message);
203 /* libwebsocket callback for the 'protocol-dialer' protocol */
205 callback_dialer(struct libwebsocket_context *context,
206 struct libwebsocket *wsi,
207 enum libwebsocket_callback_reasons reason,
208 void *user, void *in, size_t len)
214 case LWS_CALLBACK_BROADCAST:
216 * This is triggered on all open websocket connections
217 * when libwebsocket_broadcast is called. For this
218 * protocol we ignore the 'in' paramter and send out
219 * the pending event queue
222 unsigned char *b = malloc(LWS_SEND_BUFFER_PRE_PADDING +
224 LWS_SEND_BUFFER_POST_PADDING);
225 unsigned char *p = b + LWS_SEND_BUFFER_PRE_PADDING;
226 int n = sprintf((char *)p, "%s", (char *)in);
228 libwebsocket_write(wsi, p, n, LWS_WRITE_TEXT);
234 case LWS_CALLBACK_ESTABLISHED:
235 if (voicecallmanager_path) {
236 /* inform any connections about the addition of the modem */
237 json_t *o = json_object();
239 json_object_set(o, "type", json_string("ModemAdded"));
240 json_object_set(o, "id", json_string(voicecallmanager_path));
241 char *dump = json_dumps(o, 0);
243 unsigned char *b = malloc(LWS_SEND_BUFFER_PRE_PADDING +
245 LWS_SEND_BUFFER_POST_PADDING);
246 unsigned char *p = b + LWS_SEND_BUFFER_PRE_PADDING;
247 int n = sprintf((char *)p, "%s", dump);
249 libwebsocket_write(wsi, p, n, LWS_WRITE_TEXT);
254 case LWS_CALLBACK_RECEIVE:
256 * The other end of the websocket is sending us text
257 * so attempt to parse that text as a JSON object providing
258 * either a single command object or an array of command
261 if (in && (o = json_loads((char *)in, 0, NULL))) {
262 if (json_is_array(o)) {
263 for (i = 0; i < json_array_size(o); i++)
264 handle_command(json_array_get(o, i));
268 json_object_clear(o);
280 /* Write a serialized JSON object containing */
281 /* a list of objects each describing an application */
282 void send_app_list(struct libwebsocket *socket)
285 * WARNING: libwebsockets requires prefixing and post
286 * fixing the send buffer with enough space
287 * for the library to insert required websocket
288 * protocol information.
289 * Ignoring this will result in segfaults.
291 char *dump = json_dumps(app_list, 0);
292 unsigned char *b = malloc(LWS_SEND_BUFFER_PRE_PADDING +
294 LWS_SEND_BUFFER_POST_PADDING);
295 unsigned char *p = b + LWS_SEND_BUFFER_PRE_PADDING;
296 int n = sprintf((char *)p, "%s", dump);
298 libwebsocket_write(socket, p, n, LWS_WRITE_TEXT);
304 /* command object parser */
305 void handle_command(json_t *o)
307 const char *cmd = json_string_value(json_object_get(o, "cmd"));
312 if (strncmp(cmd, "launch", 6) == 0 && (tmp = json_object_get(o, "index"))) {
313 int index = json_integer_value(tmp);
314 json_t *app = json_array_get(app_list, index);
318 json_t *exec = json_object_get(app, "exec");
322 ecore_exe_run(json_string_value(exec), NULL);
323 } else if (strncmp(cmd, "makeCall", 8) == 0 &&
324 (tmp = json_object_get(o, "remotePartyId"))) {
325 const char *remotePartyId = json_string_value(tmp);
326 const char *hideCallerId = "";
329 msg = dbus_message_new_method_call(
331 voicecallmanager_path,
332 "org.ofono.VoiceCallManager",
335 dbus_message_append_args(msg,
336 DBUS_TYPE_STRING, &remotePartyId,
338 dbus_message_append_args(msg,
339 DBUS_TYPE_STRING, &hideCallerId,
341 e_dbus_message_send(bus, msg, dial_reply, -1, NULL);
342 } else if (strncmp(cmd, "end", 3) == 0) {
343 const char *path = json_string_value(json_object_get(o, "path"));
346 msg = dbus_message_new_method_call(
349 "org.ofono.VoiceCall",
352 e_dbus_message_send(bus, msg, dial_reply, -1, NULL);
354 } else if (strncmp(cmd, "accept", 6) == 0) {
355 const char *path = json_string_value(json_object_get(o, "path"));
358 msg = dbus_message_new_method_call(
361 "org.ofono.VoiceCall",
364 e_dbus_message_send(bus, msg, dial_reply, -1, NULL);
368 /* libwebsocket callback for 'protocol-app-list' based websockets */
370 callback_app_list(struct libwebsocket_context *context,
371 struct libwebsocket *wsi,
372 enum libwebsocket_callback_reasons reason,
373 void *user, void *in, size_t len)
379 case LWS_CALLBACK_BROADCAST:
381 * This is triggered on all open websocket connections
382 * when libwebsocket_broadcast is called. For this
383 * protocol we only do one possible thing on broadcast
384 * so just ignore the incoming 'in' string and just
385 * send a new list of applications.
387 case LWS_CALLBACK_ESTABLISHED:
391 case LWS_CALLBACK_RECEIVE:
393 * The other end of the websocket is sending us text
394 * so attempt to parse that text as a JSON object providing
395 * either a single command object or an array of command
398 if (in && (o = json_loads((char *)in, 0, NULL))) {
399 if (json_is_array(o)) {
400 for (i = 0; i < json_array_size(o); i++)
401 handle_command(json_array_get(o, i));
405 json_object_clear(o);
417 void build_app_list(char *dir) {
419 const char *filename;
420 const Eina_File_Direct_Info *info;
422 json_array_clear(app_list);
425 it = eina_file_ls(dir);
426 EINA_ITERATOR_FOREACH(it, filename)
428 if (!eina_str_has_suffix(filename, ".desktop"))
431 Efreet_Desktop *desktop = efreet_desktop_get(filename);
432 if (!desktop || !desktop->name || !desktop->icon || !desktop->exec)
435 json_t *o = json_object();
436 json_object_set(o, "id", json_integer(index++));
437 json_object_set(o, "version", json_string("0.1"));
438 json_object_set(o, "show", json_boolean(1));
439 json_object_set(o, "name", json_string(desktop->name));
440 json_object_set(o, "iconPath", json_string(desktop->icon));
441 json_object_set(o, "exec", json_string(desktop->exec));
442 json_array_append(app_list, o);
444 eina_iterator_free(it);
447 #define BUFF_SIZE ((sizeof(struct inotify_event)+FILENAME_MAX)*1024)
448 static Eina_Bool poll_descriptors(void *data)
452 char buff[BUFF_SIZE] = {0};
454 n = poll(pollfds, count_pollfds, 100);
456 if (pollfds[0].revents) {
457 /* The first descriptor what inotify returns*/
458 len = read(pollfds[0].fd, buff, BUFF_SIZE);
460 struct inotify_event *pevent = (struct inotify_event *)&buff[i];
461 if (pevent->name && eina_str_has_suffix(pevent->name, ".desktop")) {
462 build_app_list(apps_directory);
464 /* Trigger the broadcast callback for all open connectinos */
465 libwebsockets_broadcast(&protocols[PROTOCOL_APP_LIST], "x", 2);
469 i += sizeof(struct inotify_event) + pevent->len;
472 if (count_pollfds > 1) {
473 for (n = 1; n < count_pollfds; n++)
474 if (pollfds[n].revents)
475 libwebsocket_service_fd(context, &pollfds[n]);
479 return ECORE_CALLBACK_RENEW;
482 void call_properties_changed_cb(void *data, DBusMessage *msg)
484 DBusMessageIter iter, value;
485 const char *property, *v;
490 dbus_message_iter_init(msg, &iter);
491 dbus_message_iter_get_basic(&iter, &property);
493 dbus_message_iter_next(&iter);
494 dbus_message_iter_recurse(&iter, &value);
496 dbus_message_iter_get_basic(&value, &v);
498 object = json_object();
499 json_object_set(object, "type", json_string("PropertyChanged"));
500 json_object_set(object, "id", json_string(dbus_message_get_path(msg)));
501 json_object_set(object, property, json_string(v));
502 dump = json_dumps(object, 0);
503 libwebsockets_broadcast(&protocols[PROTOCOL_DIALER], dump, strlen(dump));
507 void modem_properties_changed_cb(void *data, DBusMessage *msg)
509 DBusMessageIter iter, values, entry;
510 const char *property, *v;
515 dbus_message_iter_init(msg, &iter);
516 dbus_message_iter_get_basic(&iter, &property);
518 if (!strcasecmp(property, "Interfaces")) {
519 dbus_message_iter_next(&iter);
520 dbus_message_iter_recurse(&iter, &values);
522 while (dbus_message_iter_get_arg_type(&values) == DBUS_TYPE_ARRAY) {
523 DBusMessageIter variant;
524 dbus_message_iter_recurse(&iter, &entry);
525 dbus_message_iter_recurse(&entry, &variant);
526 if (dbus_message_iter_get_arg_type(&variant) != DBUS_TYPE_INVALID) {
527 dbus_message_iter_get_basic(&variant, &v);
528 if (!strcasecmp(v, "org.ofono.VoiceCallManager")) {
529 object = json_object();
530 json_object_set(object, "type", json_string("ModemAdded"));
531 json_object_set(object, "id", json_string(dbus_message_get_path(msg)));
532 dump = json_dumps(object, 0);
533 libwebsockets_broadcast(&protocols[PROTOCOL_DIALER], dump, strlen(dump));
537 const char *path = strdup(dbus_message_get_path(msg));
539 // If the modem has removed all interfaces then its no longer
540 // available for making calls
541 object = json_object();
542 json_object_set(object, "type", json_string("ModemRemoved"));
543 json_object_set(object, "id", json_string(path));
544 dump = json_dumps(object, 0);
545 libwebsockets_broadcast(&protocols[PROTOCOL_DIALER], dump, strlen(dump));
548 // Start polling to re-enable the modem
549 power_on_modem((void *)path);
551 dbus_message_iter_next(&values);
556 void call_added_cb(void *data, DBusMessage *msg)
558 DBusMessageIter iter, properties;
561 dbus_message_iter_init(msg, &iter);
562 dbus_message_iter_get_basic(&iter, &path);
564 dbus_message_iter_next(&iter);
565 dbus_message_iter_recurse(&iter, &properties);
567 json_t *object = json_object();
568 json_object_set(object, "id", json_string(path));
569 json_object_set(object, "type", json_string("CallAdded"));
571 while (dbus_message_iter_get_arg_type(&properties) == DBUS_TYPE_DICT_ENTRY) {
572 DBusMessageIter entry, value;
575 dbus_message_iter_recurse(&properties, &entry);
576 dbus_message_iter_get_basic(&entry, &key);
578 dbus_message_iter_next(&entry);
579 dbus_message_iter_recurse(&entry, &value);
581 dbus_message_iter_get_basic(&value, &v);
583 json_object_set(object, key, json_string(v));
585 dbus_message_iter_next(&properties);
588 e_dbus_signal_handler_add(bus,
591 "org.ofono.VoiceCall",
593 call_properties_changed_cb,
596 char *dump = json_dumps(object, 0);
597 libwebsockets_broadcast(&protocols[PROTOCOL_DIALER], dump, strlen(dump));
601 void call_removed_cb(void *data, DBusMessage *msg)
603 DBusMessageIter iter;
606 dbus_message_iter_init(msg, &iter);
607 dbus_message_iter_get_basic(&iter, &path);
609 json_t *object = json_object();
610 json_object_set(object, "id", json_string(path));
611 json_object_set(object, "type", json_string("CallRemoved"));
612 char *dump = json_dumps(object, 0);
613 libwebsockets_broadcast(&protocols[PROTOCOL_DIALER], dump, strlen(dump));
617 void modem_added_cb(void *data, DBusMessage *msg)
620 * TODO: Verify the modem is powered on before sending a ModemAdded
621 * event, and if the modem is not powered then go ahead and
622 * ask ofono to power on the modem
624 DBusMessageIter iter;
627 dbus_message_iter_init(msg, &iter);
628 dbus_message_iter_get_basic(&iter, &path);
630 json_t *object = json_object();
631 json_object_set(object, "type", json_string("ModemAdded"));
632 json_object_set(object, "id", json_string(path));
633 char *dump = json_dumps(object, 0);
634 libwebsockets_broadcast(&protocols[PROTOCOL_DIALER], dump, strlen(dump));
638 void modem_removed_cb(void *data, DBusMessage *msg)
640 DBusMessageIter iter;
643 dbus_message_iter_init(msg, &iter);
644 dbus_message_iter_get_basic(&iter, &path);
646 json_t *object = json_object();
647 json_object_set(object, "type", json_string("ModemRemoved"));
648 json_object_set(object, "id", json_string(path));
649 char *dump = json_dumps(object, 0);
650 libwebsockets_broadcast(&protocols[PROTOCOL_DIALER], dump, strlen(dump));
656 power_on_modem(void *data)
658 const char *path = (const char *)data;
660 DBusMessageIter iter, sub;
661 msg = dbus_message_new_method_call(
667 const char *property = "Powered";
670 sig[0] = DBUS_TYPE_BOOLEAN;
673 dbus_message_append_args(msg,
674 DBUS_TYPE_STRING, &property,
677 dbus_message_iter_init_append(msg, &iter);
678 if (dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT, sig, &sub)) {
679 dbus_message_iter_append_basic(&sub, DBUS_TYPE_BOOLEAN, &p);
680 dbus_message_iter_close_container(&iter, &sub);
682 e_dbus_message_send(bus, msg, set_powered_cb, -1, (void *)path);
685 return ECORE_CALLBACK_DONE;
689 set_powered_cb(void *data, DBusMessage *reply, DBusError *error)
691 if (dbus_error_is_set(error)) {
693 * Attempting to power on a modem when the associated phone
694 * is out of range will result in this call failing. Since we
695 * expect a users phone to automatically be enabled when the
696 * user comes within range then we start a periodic attempt
699 const char *path = (const char *)data;
700 ecore_poller_add(ECORE_POLLER_CORE, 4, power_on_modem, (void *)path);
706 get_modems_reply(void *data, DBusMessage *reply, DBusError *error)
708 DBusMessageIter iter, entry;
709 if (dbus_error_is_set(error)) {
710 fprintf(stderr, "Error: %s - %s\n", error->name, error->message);
714 dbus_message_iter_init(reply, &iter);
715 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY) {
716 fprintf(stderr, "Unexpeced signature from ofono GetModems call\n");
720 dbus_message_iter_recurse(&iter, &entry);
722 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRUCT) {
724 DBusMessageIter item, properties;
726 dbus_message_iter_recurse(&entry, &item);
727 dbus_message_iter_get_basic(&item, &path);
729 printf("registering %s\n", path);
730 e_dbus_signal_handler_add(bus, "org.ofono",
731 path, "org.ofono.Modem", "PropertyChanged",
732 modem_properties_changed_cb, (void *)strdup(path));
733 e_dbus_signal_handler_add(bus, "org.ofono",
734 path, "org.ofono.VoiceCallManager", "CallAdded",
735 call_added_cb, NULL);
736 e_dbus_signal_handler_add(bus, "org.ofono",
737 path, "org.ofono.VoiceCallManager",
739 call_removed_cb, NULL);
741 dbus_message_iter_next(&item);
742 dbus_message_iter_recurse(&item, &properties);
746 while (dbus_message_iter_get_arg_type(&properties) == DBUS_TYPE_DICT_ENTRY) {
748 DBusMessageIter interfaces, value, entry;
750 dbus_message_iter_recurse(&properties, &entry);
751 dbus_message_iter_get_basic(&entry, &key);
753 dbus_message_iter_next(&entry);
754 dbus_message_iter_recurse(&entry, &value);
756 if (!strcasecmp(key, "Interfaces")) {
757 dbus_message_iter_recurse(&value, &interfaces);
758 while (dbus_message_iter_get_arg_type(&interfaces) ==
762 dbus_message_iter_get_basic(&interfaces, &iface);
764 if (!strcasecmp(iface, "org.ofono.VoiceCallManager")) {
766 if (voicecallmanager_path)
767 free(voicecallmanager_path);
768 voicecallmanager_path = strdup(path);
770 /* inform any connections about the addition of the modem */
771 json_t *o = json_object();
772 json_object_set(o, "type", json_string("ModemAdded"));
773 json_object_set(o, "id", json_string(path));
774 char *dump = json_dumps(o, 0);
775 libwebsockets_broadcast(&protocols[PROTOCOL_DIALER], dump, strlen(dump));
779 dbus_message_iter_next(&interfaces);
783 dbus_message_iter_next(&properties);
787 power_on_modem(strdup(path));
790 dbus_message_iter_next(&entry);
794 static struct option options[] = {
795 { "help", no_argument, NULL, 'h' },
796 { "port", required_argument, NULL, 'p' },
797 { "appsdir", required_argument, NULL, 'a' },
798 { "contentdir", required_argument, NULL, 'c' },
802 int main(int argc, char **argv)
814 ecore_app_args_set(argc, (const char **)argv);
816 /* parse command-line options */
818 n = getopt_long(argc, argv, "hp:d:", options, NULL);
823 apps_directory = strdup(optarg);
826 content_directory = strdup(optarg);
833 fprintf(stderr, "Usage: %s [--port=<p>] [--appsdir=<a>] [--contentdir=<c>]\n", argv[0]);
838 apps_directory = APPDIR;
839 if (!content_directory)
840 content_directory = LOCAL_RESOURCE_PATH;
842 /* Initialize and build up the application list */
843 app_list = json_array();
844 build_app_list(apps_directory);
846 bus = e_dbus_bus_get(DBUS_BUS_SYSTEM);
848 /* Start listening for incoming phone calls */
850 msg = dbus_message_new_method_call(
856 e_dbus_message_send(bus, msg, get_modems_reply, -1, NULL);
858 e_dbus_signal_handler_add(bus, "org.ofono", "/", "org.ofono.Manager", "ModemAdded",
859 modem_added_cb, NULL);
860 e_dbus_signal_handler_add(bus, "org.ofono", "/", "org.ofono.Manager", "ModemRemoved",
861 modem_removed_cb, NULL);
863 /* Listen for updates on application desktop files*/
865 inotify_add_watch(fd, APPDIR, IN_CREATE | IN_DELETE | IN_MODIFY);
867 pollfds[0].events = POLLIN;
868 pollfds[0].revents = 0;
871 /* Start listening for incoming websocket connections */
872 context = libwebsocket_create_context(port, NULL, protocols,
873 libwebsocket_internal_extensions,
874 NULL, NULL, -1, -1, opts);
875 if (context == NULL) {
876 fprintf(stderr, "libwebsocket init failed\n");
880 ecore_idler_add(poll_descriptors, NULL);
882 /* configure poller for periodically attempting */
883 /* to enabled modems */
884 ecore_poller_poll_interval_set(ECORE_POLLER_CORE, 1.0);
886 ecore_main_loop_begin();
889 libwebsocket_context_destroy(context);
894 /* Local Variables: */
896 /* c-basic-offset:4 */
897 /* indent-tabs-mode: nil */