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);
67 void setup_new_modem(DBusMessageIter entry);
72 struct per_session_data__app_list {
75 struct per_session_data__dialer {
79 static struct libwebsocket_protocols protocols[] = {
88 sizeof(struct per_session_data__app_list),
93 sizeof(struct per_session_data__dialer),
100 /* libwebsocket callback for handling normal http request. We use this */
101 /* to implementa a rudamentary webserver. */
103 callback_http(struct libwebsocket_context *context,
104 struct libwebsocket *wsi,
105 enum libwebsocket_callback_reasons reason, void *user,
106 void *in, size_t len)
108 char client_name[128];
115 case LWS_CALLBACK_HTTP:
116 if (in && ((char *)in)[0] == '/' && !strstr(in, "..")) {
117 if (strstr(in, "/icon/") == in) {
118 /* virtual directory used for serving up system icons */
120 char *icon = (char *)(in + 6);
121 const char *path = efreet_icon_path_find(getenv("E_ICON_THEME"),
122 (const char *)icon, 128);
124 path = efreet_icon_path_find("default", icon, 128);
126 path = efreet_icon_path_find("hicolor", icon, 128);
128 if (icon[0] == '/' && !stat(icon, &info) && S_ISREG(info.st_mode))
131 fprintf(stderr, "Can not find icon: %s\n", icon);
138 libwebsockets_serve_http_file(wsi, path,
139 (const char *)efreet_mime_type_get((char *)path));
141 snprintf(filename, 256, "%s/%s", content_directory, (char *)in + 1);
142 if (!stat(filename, &info) && S_ISDIR(info.st_mode))
143 /* try serving up index.html */
144 snprintf(filename, 256, "%s/%s/index.html",
145 content_directory, (char *)in + 1);
147 if (!stat(filename, &info) && S_ISREG(info.st_mode)) {
148 libwebsockets_serve_http_file(wsi, filename,
149 (const char *)efreet_mime_type_get(filename));
151 /* TODO: Serve up error html page */
152 fprintf(stderr, "Unable to find %s\n", filename);
156 fprintf(stderr, "Invalid request \'%s\'\n", (char *)in);
160 case LWS_CALLBACK_ADD_POLL_FD:
161 pollfds[count_pollfds].fd = (int)(long)user;
162 pollfds[count_pollfds].events = (int)len;
163 pollfds[count_pollfds++].revents = 0;
166 case LWS_CALLBACK_DEL_POLL_FD:
167 for (n = 0; n < count_pollfds; n++)
168 if (pollfds[n].fd == (int)(long)user)
169 while (n < count_pollfds) {
170 pollfds[n] = pollfds[n + 1];
176 case LWS_CALLBACK_SET_MODE_POLL_FD:
177 for (n = 0; n < count_pollfds; n++)
178 if (pollfds[n].fd == (int)(long)user)
179 pollfds[n].events |= (int)(long)len;
182 case LWS_CALLBACK_CLEAR_MODE_POLL_FD:
183 for (n = 0; n < count_pollfds; n++)
184 if (pollfds[n].fd == (int)(long)user)
185 pollfds[n].events &= ~(int)(long)len;
196 dial_reply(void *data, DBusMessage *reply, DBusError *error)
198 if (dbus_error_is_set(error)) {
199 fprintf(stderr, "Error: %s - %s\n", error->name, error->message);
204 /* libwebsocket callback for the 'protocol-dialer' protocol */
206 callback_dialer(struct libwebsocket_context *context,
207 struct libwebsocket *wsi,
208 enum libwebsocket_callback_reasons reason,
209 void *user, void *in, size_t len)
215 case LWS_CALLBACK_BROADCAST:
217 * This is triggered on all open websocket connections
218 * when libwebsocket_broadcast is called. For this
219 * protocol we ignore the 'in' paramter and send out
220 * the pending event queue
223 unsigned char *b = malloc(LWS_SEND_BUFFER_PRE_PADDING +
225 LWS_SEND_BUFFER_POST_PADDING);
226 unsigned char *p = b + LWS_SEND_BUFFER_PRE_PADDING;
227 int n = sprintf((char *)p, "%s", (char *)in);
229 libwebsocket_write(wsi, p, n, LWS_WRITE_TEXT);
235 case LWS_CALLBACK_ESTABLISHED:
236 if (voicecallmanager_path) {
237 /* inform any connections about the addition of the modem */
238 json_t *o = json_object();
240 json_object_set(o, "type", json_string("ModemAdded"));
241 json_object_set(o, "id", json_string(voicecallmanager_path));
242 char *dump = json_dumps(o, 0);
244 unsigned char *b = malloc(LWS_SEND_BUFFER_PRE_PADDING +
246 LWS_SEND_BUFFER_POST_PADDING);
247 unsigned char *p = b + LWS_SEND_BUFFER_PRE_PADDING;
248 int n = sprintf((char *)p, "%s", dump);
250 libwebsocket_write(wsi, p, n, LWS_WRITE_TEXT);
255 case LWS_CALLBACK_RECEIVE:
257 * The other end of the websocket is sending us text
258 * so attempt to parse that text as a JSON object providing
259 * either a single command object or an array of command
262 if (in && (o = json_loads((char *)in, 0, NULL))) {
263 if (json_is_array(o)) {
264 for (i = 0; i < json_array_size(o); i++)
265 handle_command(json_array_get(o, i));
269 json_object_clear(o);
281 /* Write a serialized JSON object containing */
282 /* a list of objects each describing an application */
283 void send_app_list(struct libwebsocket *socket)
286 * WARNING: libwebsockets requires prefixing and post
287 * fixing the send buffer with enough space
288 * for the library to insert required websocket
289 * protocol information.
290 * Ignoring this will result in segfaults.
292 char *dump = json_dumps(app_list, 0);
293 unsigned char *b = malloc(LWS_SEND_BUFFER_PRE_PADDING +
295 LWS_SEND_BUFFER_POST_PADDING);
296 unsigned char *p = b + LWS_SEND_BUFFER_PRE_PADDING;
297 int n = sprintf((char *)p, "%s", dump);
299 libwebsocket_write(socket, p, n, LWS_WRITE_TEXT);
305 /* command object parser */
306 void handle_command(json_t *o)
308 const char *cmd = json_string_value(json_object_get(o, "cmd"));
313 if (strncmp(cmd, "launch", 6) == 0 && (tmp = json_object_get(o, "index"))) {
314 int index = json_integer_value(tmp);
315 json_t *app = json_array_get(app_list, index);
319 json_t *exec = json_object_get(app, "exec");
323 ecore_exe_run(json_string_value(exec), NULL);
324 } else if (strncmp(cmd, "makeCall", 8) == 0 &&
325 (tmp = json_object_get(o, "remotePartyId"))) {
326 const char *remotePartyId = json_string_value(tmp);
327 const char *hideCallerId = "";
330 msg = dbus_message_new_method_call(
332 voicecallmanager_path,
333 "org.ofono.VoiceCallManager",
336 dbus_message_append_args(msg,
337 DBUS_TYPE_STRING, &remotePartyId,
339 dbus_message_append_args(msg,
340 DBUS_TYPE_STRING, &hideCallerId,
342 e_dbus_message_send(bus, msg, dial_reply, -1, NULL);
343 } else if (strncmp(cmd, "end", 3) == 0) {
344 const char *path = json_string_value(json_object_get(o, "path"));
347 msg = dbus_message_new_method_call(
350 "org.ofono.VoiceCall",
353 e_dbus_message_send(bus, msg, dial_reply, -1, NULL);
355 } else if (strncmp(cmd, "accept", 6) == 0) {
356 const char *path = json_string_value(json_object_get(o, "path"));
359 msg = dbus_message_new_method_call(
362 "org.ofono.VoiceCall",
365 e_dbus_message_send(bus, msg, dial_reply, -1, NULL);
369 /* libwebsocket callback for 'protocol-app-list' based websockets */
371 callback_app_list(struct libwebsocket_context *context,
372 struct libwebsocket *wsi,
373 enum libwebsocket_callback_reasons reason,
374 void *user, void *in, size_t len)
380 case LWS_CALLBACK_BROADCAST:
382 * This is triggered on all open websocket connections
383 * when libwebsocket_broadcast is called. For this
384 * protocol we only do one possible thing on broadcast
385 * so just ignore the incoming 'in' string and just
386 * send a new list of applications.
388 case LWS_CALLBACK_ESTABLISHED:
392 case LWS_CALLBACK_RECEIVE:
394 * The other end of the websocket is sending us text
395 * so attempt to parse that text as a JSON object providing
396 * either a single command object or an array of command
399 if (in && (o = json_loads((char *)in, 0, NULL))) {
400 if (json_is_array(o)) {
401 for (i = 0; i < json_array_size(o); i++)
402 handle_command(json_array_get(o, i));
406 json_object_clear(o);
418 void build_app_list(char *dir) {
420 const char *filename;
421 const Eina_File_Direct_Info *info;
423 json_array_clear(app_list);
426 it = eina_file_ls(dir);
427 EINA_ITERATOR_FOREACH(it, filename)
429 if (!eina_str_has_suffix(filename, ".desktop"))
432 Efreet_Desktop *desktop = efreet_desktop_get(filename);
433 if (!desktop || !desktop->name || !desktop->icon || !desktop->exec)
436 json_t *o = json_object();
437 json_object_set(o, "id", json_integer(index++));
438 json_object_set(o, "version", json_string("0.1"));
439 json_object_set(o, "show", json_boolean(1));
440 json_object_set(o, "name", json_string(desktop->name));
441 json_object_set(o, "iconPath", json_string(desktop->icon));
442 json_object_set(o, "exec", json_string(desktop->exec));
443 json_array_append(app_list, o);
445 eina_iterator_free(it);
448 #define BUFF_SIZE ((sizeof(struct inotify_event)+FILENAME_MAX)*1024)
449 static Eina_Bool poll_descriptors(void *data)
453 char buff[BUFF_SIZE] = {0};
455 n = poll(pollfds, count_pollfds, 100);
457 if (pollfds[0].revents) {
458 /* The first descriptor what inotify returns*/
459 len = read(pollfds[0].fd, buff, BUFF_SIZE);
461 struct inotify_event *pevent = (struct inotify_event *)&buff[i];
462 if (pevent->name && eina_str_has_suffix(pevent->name, ".desktop")) {
463 build_app_list(apps_directory);
465 /* Trigger the broadcast callback for all open connectinos */
466 libwebsockets_broadcast(&protocols[PROTOCOL_APP_LIST], "x", 2);
470 i += sizeof(struct inotify_event) + pevent->len;
473 if (count_pollfds > 1) {
474 for (n = 1; n < count_pollfds; n++)
475 if (pollfds[n].revents)
476 libwebsocket_service_fd(context, &pollfds[n]);
480 return ECORE_CALLBACK_RENEW;
483 void call_properties_changed_cb(void *data, DBusMessage *msg)
485 DBusMessageIter iter, value;
486 const char *property, *v;
491 dbus_message_iter_init(msg, &iter);
492 dbus_message_iter_get_basic(&iter, &property);
494 dbus_message_iter_next(&iter);
495 dbus_message_iter_recurse(&iter, &value);
497 dbus_message_iter_get_basic(&value, &v);
499 object = json_object();
500 json_object_set(object, "type", json_string("PropertyChanged"));
501 json_object_set(object, "id", json_string(dbus_message_get_path(msg)));
502 json_object_set(object, property, json_string(v));
503 dump = json_dumps(object, 0);
504 libwebsockets_broadcast(&protocols[PROTOCOL_DIALER], dump, strlen(dump));
508 void modem_properties_changed_cb(void *data, DBusMessage *msg)
510 DBusMessageIter iter, values, entry;
511 const char *property, *v;
516 dbus_message_iter_init(msg, &iter);
517 dbus_message_iter_get_basic(&iter, &property);
519 if (!strcasecmp(property, "Interfaces")) {
520 dbus_message_iter_next(&iter);
521 dbus_message_iter_recurse(&iter, &values);
523 while (dbus_message_iter_get_arg_type(&values) == DBUS_TYPE_ARRAY) {
524 DBusMessageIter variant;
525 dbus_message_iter_recurse(&iter, &entry);
526 dbus_message_iter_recurse(&entry, &variant);
527 if (dbus_message_iter_get_arg_type(&variant) != DBUS_TYPE_INVALID) {
528 dbus_message_iter_get_basic(&variant, &v);
529 if (!strcasecmp(v, "org.ofono.VoiceCallManager")) {
530 object = json_object();
531 json_object_set(object, "type", json_string("ModemAdded"));
532 json_object_set(object, "id", json_string(dbus_message_get_path(msg)));
533 dump = json_dumps(object, 0);
534 libwebsockets_broadcast(&protocols[PROTOCOL_DIALER], dump, strlen(dump));
538 const char *path = strdup(dbus_message_get_path(msg));
540 // If the modem has removed all interfaces then its no longer
541 // available for making calls
542 object = json_object();
543 json_object_set(object, "type", json_string("ModemRemoved"));
544 json_object_set(object, "id", json_string(path));
545 dump = json_dumps(object, 0);
546 libwebsockets_broadcast(&protocols[PROTOCOL_DIALER], dump, strlen(dump));
549 // Start polling to re-enable the modem
550 power_on_modem((void *)path);
552 dbus_message_iter_next(&values);
557 void call_added_cb(void *data, DBusMessage *msg)
559 DBusMessageIter iter, properties;
562 dbus_message_iter_init(msg, &iter);
563 dbus_message_iter_get_basic(&iter, &path);
565 dbus_message_iter_next(&iter);
566 dbus_message_iter_recurse(&iter, &properties);
568 json_t *object = json_object();
569 json_object_set(object, "id", json_string(path));
570 json_object_set(object, "type", json_string("CallAdded"));
572 while (dbus_message_iter_get_arg_type(&properties) == DBUS_TYPE_DICT_ENTRY) {
573 DBusMessageIter entry, value;
576 dbus_message_iter_recurse(&properties, &entry);
577 dbus_message_iter_get_basic(&entry, &key);
579 dbus_message_iter_next(&entry);
580 dbus_message_iter_recurse(&entry, &value);
582 dbus_message_iter_get_basic(&value, &v);
584 json_object_set(object, key, json_string(v));
586 dbus_message_iter_next(&properties);
589 e_dbus_signal_handler_add(bus,
592 "org.ofono.VoiceCall",
594 call_properties_changed_cb,
597 char *dump = json_dumps(object, 0);
598 libwebsockets_broadcast(&protocols[PROTOCOL_DIALER], dump, strlen(dump));
602 void call_removed_cb(void *data, DBusMessage *msg)
604 DBusMessageIter iter;
607 dbus_message_iter_init(msg, &iter);
608 dbus_message_iter_get_basic(&iter, &path);
610 json_t *object = json_object();
611 json_object_set(object, "id", json_string(path));
612 json_object_set(object, "type", json_string("CallRemoved"));
613 char *dump = json_dumps(object, 0);
614 libwebsockets_broadcast(&protocols[PROTOCOL_DIALER], dump, strlen(dump));
618 void modem_added_cb(void *data, DBusMessage *msg)
621 * TODO: Verify the modem is powered on before sending a ModemAdded
622 * event, and if the modem is not powered then go ahead and
623 * ask ofono to power on the modem
625 DBusMessageIter iter;
628 dbus_message_iter_init(msg, &iter);
629 dbus_message_iter_get_basic(&iter, &path);
631 json_t *object = json_object();
632 json_object_set(object, "type", json_string("ModemAdded"));
633 json_object_set(object, "id", json_string(path));
634 char *dump = json_dumps(object, 0);
635 libwebsockets_broadcast(&protocols[PROTOCOL_DIALER], dump, strlen(dump));
639 void modem_removed_cb(void *data, DBusMessage *msg)
641 DBusMessageIter entry;
642 dbus_message_iter_init(msg, &entry);
643 setup_new_modem(entry);
648 power_on_modem(void *data)
650 const char *path = (const char *)data;
652 DBusMessageIter iter, sub;
653 msg = dbus_message_new_method_call(
659 const char *property = "Powered";
662 sig[0] = DBUS_TYPE_BOOLEAN;
665 dbus_message_append_args(msg,
666 DBUS_TYPE_STRING, &property,
669 dbus_message_iter_init_append(msg, &iter);
670 if (dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT, sig, &sub)) {
671 dbus_message_iter_append_basic(&sub, DBUS_TYPE_BOOLEAN, &p);
672 dbus_message_iter_close_container(&iter, &sub);
674 e_dbus_message_send(bus, msg, set_powered_cb, -1, (void *)path);
677 return ECORE_CALLBACK_DONE;
681 set_powered_cb(void *data, DBusMessage *reply, DBusError *error)
683 if (dbus_error_is_set(error)) {
685 * Attempting to power on a modem when the associated phone
686 * is out of range will result in this call failing. Since we
687 * expect a users phone to automatically be enabled when the
688 * user comes within range then we start a periodic attempt
691 const char *path = (const char *)data;
692 ecore_poller_add(ECORE_POLLER_CORE, 4, power_on_modem, (void *)path);
697 void setup_new_modem(DBusMessageIter entry)
699 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRUCT) {
701 DBusMessageIter item, properties;
703 dbus_message_iter_recurse(&entry, &item);
704 dbus_message_iter_get_basic(&item, &path);
706 printf("registering %s\n", path);
707 e_dbus_signal_handler_add(bus, "org.ofono",
708 path, "org.ofono.Modem", "PropertyChanged",
709 modem_properties_changed_cb, (void *)strdup(path));
710 e_dbus_signal_handler_add(bus, "org.ofono",
711 path, "org.ofono.VoiceCallManager", "CallAdded",
712 call_added_cb, NULL);
713 e_dbus_signal_handler_add(bus, "org.ofono",
714 path, "org.ofono.VoiceCallManager",
716 call_removed_cb, NULL);
718 dbus_message_iter_next(&item);
719 dbus_message_iter_recurse(&item, &properties);
723 while (dbus_message_iter_get_arg_type(&properties) == DBUS_TYPE_DICT_ENTRY) {
725 DBusMessageIter interfaces, value, entry;
727 dbus_message_iter_recurse(&properties, &entry);
728 dbus_message_iter_get_basic(&entry, &key);
730 dbus_message_iter_next(&entry);
731 dbus_message_iter_recurse(&entry, &value);
733 if (!strcasecmp(key, "Interfaces")) {
734 dbus_message_iter_recurse(&value, &interfaces);
735 while (dbus_message_iter_get_arg_type(&interfaces) ==
739 dbus_message_iter_get_basic(&interfaces, &iface);
741 if (!strcasecmp(iface, "org.ofono.VoiceCallManager")) {
743 if (voicecallmanager_path)
744 free(voicecallmanager_path);
745 voicecallmanager_path = strdup(path);
747 /* inform any connections about the addition of the modem */
748 json_t *o = json_object();
749 json_object_set(o, "type", json_string("ModemAdded"));
750 json_object_set(o, "id", json_string(path));
751 char *dump = json_dumps(o, 0);
752 libwebsockets_broadcast(&protocols[PROTOCOL_DIALER], dump, strlen(dump));
756 dbus_message_iter_next(&interfaces);
760 dbus_message_iter_next(&properties);
764 power_on_modem(strdup(path));
767 dbus_message_iter_next(&entry);
772 get_modems_reply(void *data, DBusMessage *reply, DBusError *error)
774 DBusMessageIter iter, entry;
775 if (dbus_error_is_set(error)) {
776 fprintf(stderr, "Error: %s - %s\n", error->name, error->message);
780 dbus_message_iter_init(reply, &iter);
781 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY) {
782 fprintf(stderr, "Unexpeced signature from ofono GetModems call\n");
786 dbus_message_iter_recurse(&iter, &entry);
788 setup_new_modem(entry);
791 static struct option options[] = {
792 { "help", no_argument, NULL, 'h' },
793 { "port", required_argument, NULL, 'p' },
794 { "appsdir", required_argument, NULL, 'a' },
795 { "contentdir", required_argument, NULL, 'c' },
799 int main(int argc, char **argv)
811 ecore_app_args_set(argc, (const char **)argv);
813 /* parse command-line options */
815 n = getopt_long(argc, argv, "hp:d:", options, NULL);
820 apps_directory = strdup(optarg);
823 content_directory = strdup(optarg);
830 fprintf(stderr, "Usage: %s [--port=<p>] [--appsdir=<a>] [--contentdir=<c>]\n", argv[0]);
835 apps_directory = APPDIR;
836 if (!content_directory)
837 content_directory = LOCAL_RESOURCE_PATH;
839 /* Initialize and build up the application list */
840 app_list = json_array();
841 build_app_list(apps_directory);
843 bus = e_dbus_bus_get(DBUS_BUS_SYSTEM);
845 /* Start listening for incoming phone calls */
847 msg = dbus_message_new_method_call(
853 e_dbus_message_send(bus, msg, get_modems_reply, -1, NULL);
855 e_dbus_signal_handler_add(bus, "org.ofono", "/", "org.ofono.Manager", "ModemAdded",
856 modem_added_cb, NULL);
857 e_dbus_signal_handler_add(bus, "org.ofono", "/", "org.ofono.Manager", "ModemRemoved",
858 modem_removed_cb, NULL);
860 /* Listen for updates on application desktop files*/
862 inotify_add_watch(fd, APPDIR, IN_CREATE | IN_DELETE | IN_MODIFY);
864 pollfds[0].events = POLLIN;
865 pollfds[0].revents = 0;
868 /* Start listening for incoming websocket connections */
869 context = libwebsocket_create_context(port, NULL, protocols,
870 libwebsocket_internal_extensions,
871 NULL, NULL, -1, -1, opts);
872 if (context == NULL) {
873 fprintf(stderr, "libwebsocket init failed\n");
877 ecore_idler_add(poll_descriptors, NULL);
879 /* configure poller for periodically attempting */
880 /* to enabled modems */
881 ecore_poller_poll_interval_set(ECORE_POLLER_CORE, 1.0);
883 ecore_main_loop_begin();
886 libwebsocket_context_destroy(context);
891 /* Local Variables: */
893 /* c-basic-offset:4 */
894 /* indent-tabs-mode: nil */