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 json_t *event_queue = NULL;
41 char *voicecallmanager_path = NULL;
43 #define MAX_POLL_ELEMENTS 100
44 struct pollfd pollfds[100];
45 int count_pollfds = 0;
47 void handle_command(json_t *o);
48 static int callback_http(struct libwebsocket_context *context,
49 struct libwebsocket *wsi,
50 enum libwebsocket_callback_reasons reason, void *user,
51 void *in, size_t len);
52 void send_app_list(struct libwebsocket *socket);
53 static int callback_app_list(struct libwebsocket_context *context,
54 struct libwebsocket *wsi,
55 enum libwebsocket_callback_reasons reason,
56 void *user, void *in, size_t len);
57 static Eina_Bool get_websocket_event(void *data);
58 static int callback_dialer(struct libwebsocket_context *context,
59 struct libwebsocket *wsi,
60 enum libwebsocket_callback_reasons reason,
61 void *user, void *in, size_t len);
66 struct per_session_data__app_list {
69 struct per_session_data__dialer {
73 static struct libwebsocket_protocols protocols[] = {
82 sizeof(struct per_session_data__app_list),
87 sizeof(struct per_session_data__dialer),
94 /* libwebsocket callback for handling normal http request. We use this */
95 /* to implementa a rudamentary webserver. */
97 callback_http(struct libwebsocket_context *context,
98 struct libwebsocket *wsi,
99 enum libwebsocket_callback_reasons reason, void *user,
100 void *in, size_t len)
102 char client_name[128];
109 case LWS_CALLBACK_HTTP:
110 if (in && ((char *)in)[0] == '/' && !strstr(in, "..")) {
111 if (strstr(in, "/icon/") == in) {
112 /* virtual directory used for serving up system icons */
114 char *icon = (char *)(in + 6);
115 const char *path = efreet_icon_path_find(getenv("E_ICON_THEME"),
116 (const char *)icon, 128);
118 path = efreet_icon_path_find("default", icon, 128);
120 path = efreet_icon_path_find("hicolor", icon, 128);
122 if (icon[0] == '/' && !stat(icon, &info) && S_ISREG(info.st_mode))
125 fprintf(stderr, "Can not find icon: %s\n", icon);
132 libwebsockets_serve_http_file(wsi, path,
133 (const char *)efreet_mime_type_get((char *)path));
135 snprintf(filename, 256, "%s/%s", LOCAL_RESOURCE_PATH, (char *)in + 1);
136 if (strlen((char *)in) == 1)
137 /* serve up default content */
138 libwebsockets_serve_http_file(wsi, DEFAULTCONTENT, "text/html");
139 else if (!stat(filename, &info) && S_ISREG(info.st_mode))
140 libwebsockets_serve_http_file(wsi, filename,
141 (const char *)efreet_mime_type_get((char *)in));
143 fprintf(stderr, "Failed to find content: %s\n", (char *)in);
146 fprintf(stderr, "Invalid request \'%s\'\n", (char *)in);
150 case LWS_CALLBACK_ADD_POLL_FD:
151 pollfds[count_pollfds].fd = (int)(long)user;
152 pollfds[count_pollfds].events = (int)len;
153 pollfds[count_pollfds++].revents = 0;
156 case LWS_CALLBACK_DEL_POLL_FD:
157 for (n = 0; n < count_pollfds; n++)
158 if (pollfds[n].fd == (int)(long)user)
159 while (n < count_pollfds) {
160 pollfds[n] = pollfds[n + 1];
166 case LWS_CALLBACK_SET_MODE_POLL_FD:
167 for (n = 0; n < count_pollfds; n++)
168 if (pollfds[n].fd == (int)(long)user)
169 pollfds[n].events |= (int)(long)len;
172 case LWS_CALLBACK_CLEAR_MODE_POLL_FD:
173 for (n = 0; n < count_pollfds; n++)
174 if (pollfds[n].fd == (int)(long)user)
175 pollfds[n].events &= ~(int)(long)len;
186 dial_reply(void *data, DBusMessage *reply, DBusError *error)
188 if (dbus_error_is_set(error)) {
189 fprintf(stderr, "Error: %s - %s\n", error->name, error->message);
194 /* libwebsocket callback for the 'protocol-dialer' protocol */
196 callback_dialer(struct libwebsocket_context *context,
197 struct libwebsocket *wsi,
198 enum libwebsocket_callback_reasons reason,
199 void *user, void *in, size_t len)
205 case LWS_CALLBACK_BROADCAST:
207 * This is triggered on all open websocket connections
208 * when libwebsocket_broadcast is called. For this
209 * protocol we ignore the 'in' paramter and send out
210 * the pending event queue
213 unsigned char *b = malloc(LWS_SEND_BUFFER_PRE_PADDING +
215 LWS_SEND_BUFFER_POST_PADDING);
216 unsigned char *p = b + LWS_SEND_BUFFER_PRE_PADDING;
217 int n = sprintf((char *)p, "%s", (char *)in);
219 libwebsocket_write(wsi, p, n, LWS_WRITE_TEXT);
225 case LWS_CALLBACK_RECEIVE:
227 * The other end of the websocket is sending us text
228 * so attempt to parse that text as a JSON object providing
229 * either a single command object or an array of command
232 if (in && (o = json_loads((char *)in, 0, NULL))) {
233 if (json_is_array(o)) {
234 for (i = 0; i < json_array_size(o); i++)
235 handle_command(json_array_get(o, i));
239 json_object_clear(o);
251 /* Write a serialized JSON object containing */
252 /* a list of objects each describing an application */
253 void send_app_list(struct libwebsocket *socket)
256 * WARNING: libwebsockets requires prefixing and post
257 * fixing the send buffer with enough space
258 * for the library to insert required websocket
259 * protocol information.
260 * Ignoring this will result in segfaults.
262 char *dump = json_dumps(app_list, 0);
263 unsigned char *b = malloc(LWS_SEND_BUFFER_PRE_PADDING +
265 LWS_SEND_BUFFER_POST_PADDING);
266 unsigned char *p = b + LWS_SEND_BUFFER_PRE_PADDING;
267 int n = sprintf((char *)p, "%s", dump);
269 libwebsocket_write(socket, p, n, LWS_WRITE_TEXT);
275 /* command object parser */
276 void handle_command(json_t *o)
278 const char *cmd = json_string_value(json_object_get(o, "cmd"));
283 if (strncmp(cmd, "launch", 6) == 0 && (tmp = json_object_get(o, "index"))) {
284 int index = json_integer_value(tmp);
285 json_t *app = json_array_get(app_list, index);
289 json_t *exec = json_object_get(app, "exec");
293 ecore_exe_run(json_string_value(exec), NULL);
294 } else if (strncmp(cmd, "makeCall", 8) == 0 &&
295 (tmp = json_object_get(o, "remotePartyId"))) {
296 const char *remotePartyId = json_string_value(tmp);
297 const char *hideCallerId = "";
300 msg = dbus_message_new_method_call(
302 voicecallmanager_path,
303 "org.ofono.VoiceCallManager",
306 dbus_message_append_args(msg,
307 DBUS_TYPE_STRING, &remotePartyId,
309 dbus_message_append_args(msg,
310 DBUS_TYPE_STRING, &hideCallerId,
312 e_dbus_message_send(bus, msg, dial_reply, -1, NULL);
313 } else if (strncmp(cmd, "end", 3) == 0) {
315 msg = dbus_message_new_method_call(
317 voicecallmanager_path,
318 "org.ofono.VoiceCallManager",
321 e_dbus_message_send(bus, msg, dial_reply, -1, NULL);
326 /* libwebsocket callback for 'protocol-app-list' based websockets */
328 callback_app_list(struct libwebsocket_context *context,
329 struct libwebsocket *wsi,
330 enum libwebsocket_callback_reasons reason,
331 void *user, void *in, size_t len)
337 case LWS_CALLBACK_BROADCAST:
339 * This is triggered on all open websocket connections
340 * when libwebsocket_broadcast is called. For this
341 * protocol we only do one possible thing on broadcast
342 * so just ignore the incoming 'in' string and just
343 * send a new list of applications.
345 case LWS_CALLBACK_ESTABLISHED:
349 case LWS_CALLBACK_RECEIVE:
351 * The other end of the websocket is sending us text
352 * so attempt to parse that text as a JSON object providing
353 * either a single command object or an array of command
356 if (in && (o = json_loads((char *)in, 0, NULL))) {
357 if (json_is_array(o)) {
358 for (i = 0; i < json_array_size(o); i++)
359 handle_command(json_array_get(o, i));
363 json_object_clear(o);
375 void build_app_list(char *dir) {
377 const char *filename;
378 const Eina_File_Direct_Info *info;
380 json_array_clear(app_list);
383 it = eina_file_ls(dir);
384 EINA_ITERATOR_FOREACH(it, filename)
386 if (!eina_str_has_suffix(filename, ".desktop"))
389 Efreet_Desktop *desktop = efreet_desktop_get(filename);
390 if (!desktop || !desktop->name || !desktop->icon || !desktop->exec)
393 json_t *o = json_object();
394 json_object_set(o, "id", json_integer(index++));
395 json_object_set(o, "version", json_string("0.1"));
396 json_object_set(o, "show", json_boolean(1));
397 json_object_set(o, "name", json_string(desktop->name));
398 json_object_set(o, "iconPath", json_string(desktop->icon));
399 json_object_set(o, "exec", json_string(desktop->exec));
400 json_array_append(app_list, o);
402 eina_iterator_free(it);
405 #define BUFF_SIZE ((sizeof(struct inotify_event)+FILENAME_MAX)*1024)
406 static Eina_Bool poll_descriptors(void *data)
410 char buff[BUFF_SIZE] = {0};
412 n = poll(pollfds, count_pollfds, 100);
414 if (pollfds[0].revents) {
415 /* The first descriptor what inotify returns*/
416 len = read(pollfds[0].fd, buff, BUFF_SIZE);
418 struct inotify_event *pevent = (struct inotify_event *)&buff[i];
419 if (pevent->name && eina_str_has_suffix(pevent->name, ".desktop")) {
420 build_app_list(apps_directory);
422 /* Trigger the broadcast callback for all open connectinos */
423 libwebsockets_broadcast(&protocols[PROTOCOL_APP_LIST], "x", 2);
427 i += sizeof(struct inotify_event) + pevent->len;
430 if (count_pollfds > 1) {
431 for (n = 1; n < count_pollfds; n++)
432 if (pollfds[n].revents)
433 libwebsocket_service_fd(context, &pollfds[n]);
437 return ECORE_CALLBACK_RENEW;
440 void call_properties_changed_cb(void *data, DBusMessage *msg)
442 DBusMessageIter iter, value;
443 const char *property, *v;
448 dbus_message_iter_init(msg, &iter);
449 dbus_message_iter_get_basic(&iter, &property);
451 dbus_message_iter_next(&iter);
452 dbus_message_iter_recurse(&iter, &value);
454 dbus_message_iter_get_basic(&value, &v);
456 object = json_object();
457 json_object_set(object, property, json_string(v));
458 dump = json_dumps(object, 0);
459 libwebsockets_broadcast(&protocols[PROTOCOL_DIALER], dump, strlen(dump));
463 void call_added_cb(void *data, DBusMessage *msg)
465 DBusMessageIter iter, properties;
468 json_t *object = json_object();
470 dbus_message_iter_init(msg, &iter);
471 dbus_message_iter_get_basic(&iter, &path);
473 dbus_message_iter_next(&iter);
474 dbus_message_iter_recurse(&iter, &properties);
476 while (dbus_message_iter_get_arg_type(&properties) == DBUS_TYPE_DICT_ENTRY) {
477 DBusMessageIter entry, value;
480 dbus_message_iter_recurse(&properties, &entry);
481 dbus_message_iter_get_basic(&entry, &key);
483 dbus_message_iter_next(&entry);
484 dbus_message_iter_recurse(&entry, &value);
486 dbus_message_iter_get_basic(&value, &v);
488 json_object_set(object, key, json_string(v));
490 dbus_message_iter_next(&properties);
493 e_dbus_signal_handler_add(bus,
496 "org.ofono.VoiceCall",
498 call_properties_changed_cb,
501 char *dump = json_dumps(object, 0);
502 libwebsockets_broadcast(&protocols[PROTOCOL_DIALER], dump, strlen(dump));
506 void call_removed_cb(void *data, DBusMessage *msg)
508 json_t *object = json_object();
509 json_object_set(object, "type", json_string("CallRemoved"));
510 char *dump = json_dumps(object, 0);
511 libwebsockets_broadcast(&protocols[PROTOCOL_DIALER], dump, strlen(dump));
516 get_modems_reply(void *data, DBusMessage *reply, DBusError *error)
518 DBusMessageIter iter, entry;
519 if (dbus_error_is_set(error)) {
520 fprintf(stderr, "Error: %s - %s\n", error->name, error->message);
524 dbus_message_iter_init(reply, &iter);
525 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY) {
526 fprintf(stderr, "Unexpeced signature from ofono GetModems call\n");
530 dbus_message_iter_recurse(&iter, &entry);
532 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRUCT) {
534 DBusMessageIter item, properties;
536 dbus_message_iter_recurse(&entry, &item);
537 dbus_message_iter_get_basic(&item, &path);
539 dbus_message_iter_next(&item);
540 dbus_message_iter_recurse(&item, &properties);
542 while (dbus_message_iter_get_arg_type(&properties) == DBUS_TYPE_DICT_ENTRY) {
544 DBusMessageIter interfaces, value, entry;
546 dbus_message_iter_recurse(&properties, &entry);
547 dbus_message_iter_get_basic(&entry, &key);
549 dbus_message_iter_next(&entry);
550 dbus_message_iter_recurse(&entry, &value);
552 if (!strcasecmp(key, "Interfaces")) {
553 dbus_message_iter_recurse(&value, &interfaces);
554 while (dbus_message_iter_get_arg_type(&interfaces) ==
558 dbus_message_iter_get_basic(&interfaces, &iface);
560 if (!strcasecmp(iface, "org.ofono.VoiceCallManager")) {
561 if (voicecallmanager_path)
562 free(voicecallmanager_path);
563 voicecallmanager_path = strdup(path);
565 e_dbus_signal_handler_add(bus, "org.ofono",
566 path, iface, "CallAdded",
567 call_added_cb, NULL);
568 e_dbus_signal_handler_add(bus, "org.ofono",
571 call_removed_cb, NULL);
574 dbus_message_iter_next(&interfaces);
578 dbus_message_iter_next(&properties);
581 dbus_message_iter_next(&entry);
585 static struct option options[] = {
586 { "help", no_argument, NULL, 'h' },
587 { "port", required_argument, NULL, 'p' },
588 { "directory", required_argument, NULL, 'd' },
592 int main(int argc, char **argv)
604 ecore_app_args_set(argc, (const char **)argv);
606 /* parse command-line options */
608 n = getopt_long(argc, argv, "hp:d:", options, NULL);
613 apps_directory = strdup(optarg);
619 fprintf(stderr, "Usage: %s [--port=<p>] [--directory=<d>]\n", argv[0]);
624 apps_directory = APPDIR;
626 /* Initialize and build up the application list */
627 app_list = json_array();
628 build_app_list(apps_directory);
630 bus = e_dbus_bus_get(DBUS_BUS_SYSTEM);
632 /* Start listening for incoming phone calls */
634 msg = dbus_message_new_method_call(
640 e_dbus_message_send(bus, msg, get_modems_reply, -1, NULL);
642 /* Listen for updates on application desktop files*/
644 inotify_add_watch(fd, APPDIR, IN_CREATE | IN_DELETE | IN_MODIFY);
646 pollfds[0].events = POLLIN;
647 pollfds[0].revents = 0;
650 /* Start listening for incoming websocket connections */
651 context = libwebsocket_create_context(port, NULL, protocols,
652 libwebsocket_internal_extensions,
653 NULL, NULL, -1, -1, opts);
654 if (context == NULL) {
655 fprintf(stderr, "libwebsocket init failed\n");
659 ecore_idler_add(poll_descriptors, NULL);
661 ecore_main_loop_begin();
664 libwebsocket_context_destroy(context);
669 /* Local Variables: */
671 /* c-basic-offset:4 */
672 /* indent-tabs-mode: nil */