[PATCH 12/16] e_dbus/bluez: add handler to DeviceFound signal
authorbarbieri <barbieri@7cbeb6ba-43b4-40fd-8cce-4c39aea84d33>
Sat, 27 Feb 2010 05:27:43 +0000 (05:27 +0000)
committerbarbieri <barbieri@7cbeb6ba-43b4-40fd-8cce-4c39aea84d33>
Sat, 27 Feb 2010 05:27:43 +0000 (05:27 +0000)
E_Bluez_Device_Found should be free by the user, calling
e_bluez_adapter_device_found_free()

By: Gustavo F. Padovan <padovan@profusion.mobi>

git-svn-id: svn+ssh://svn.enlightenment.org/var/svn/e/trunk/e_dbus@46564 7cbeb6ba-43b4-40fd-8cce-4c39aea84d33

src/bin/e_dbus_bluez_test.c
src/lib/bluez/E_Bluez.h
src/lib/bluez/e_bluez.c
src/lib/bluez/e_bluez_adapter.c
src/lib/bluez/e_bluez_element.c
src/lib/bluez/e_bluez_private.h

index 42dd0b5..47dd29b 100644 (file)
@@ -75,6 +75,20 @@ _on_element_updated(void *data, int type, void *info)
    e_bluez_element_print(stderr, element);
    return 1;
 }
+
+static int
+_on_device_found(void *data, int type, void *info)
+{
+   E_Bluez_Device_Found *device = info;
+   printf("!!! %s\n", device->adapter->path);
+   printf(":::DeviceFound %s\n", device->name);
+   e_bluez_element_array_print(stderr, device->array);
+   printf("\n");
+
+   e_bluez_adapter_device_found_free(device);
+   return 1;
+}
+
 static int
 _on_cmd_quit(char *cmd, char *args)
 {
@@ -579,6 +593,8 @@ main(int argc, char *argv[])
    ecore_event_handler_add(E_BLUEZ_EVENT_ELEMENT_DEL, _on_element_del, NULL);
    ecore_event_handler_add(E_BLUEZ_EVENT_ELEMENT_UPDATED,
                           _on_element_updated, NULL);
+   ecore_event_handler_add(E_BLUEZ_EVENT_DEVICE_FOUND,
+                          _on_device_found, NULL);
 
    ecore_main_fd_handler_add
      (0, ECORE_FD_READ | ECORE_FD_ERROR, _on_input, NULL, NULL, NULL);
index a736795..5de2fd2 100644 (file)
@@ -40,14 +40,18 @@ extern "C" {
   extern int E_BLUEZ_EVENT_ELEMENT_ADD;
   extern int E_BLUEZ_EVENT_ELEMENT_DEL;
   extern int E_BLUEZ_EVENT_ELEMENT_UPDATED;
+  extern int E_BLUEZ_EVENT_DEVICE_FOUND;
 
   typedef struct _E_Bluez_Element E_Bluez_Element;
+  typedef struct _E_Bluez_Array E_Bluez_Array;
+  typedef struct _E_Bluez_Device_Found E_Bluez_Device_Found;
 
   struct _E_Bluez_Element
   {
      const char *path;
      const char *interface;
      E_DBus_Signal_Handler *signal_handler;
+     E_DBus_Signal_Handler *device_found_handler;
      Eina_Inlist *props;
 
      /* private */
@@ -66,6 +70,19 @@ extern "C" {
      int _references;
   };
 
+  struct _E_Bluez_Array
+  {
+     int type;
+     Eina_Array *array;
+  };
+
+  struct _E_Bluez_Device_Found
+  {
+         E_Bluez_Element *adapter;
+         const char *name;
+         E_Bluez_Array *array;
+  };
+
   /* General Public API */
   EAPI unsigned int e_bluez_system_init(E_DBus_Connection *edbus_conn) EINA_ARG_NONNULL(1);
   EAPI unsigned int e_bluez_system_shutdown(void);
@@ -75,6 +92,7 @@ extern "C" {
   EAPI bool e_bluez_manager_default_adapter(E_DBus_Method_Return_Cb cb, void *data) EINA_WARN_UNUSED_RESULT;
 
   /* Adapter Methods */
+  EAPI void e_bluez_adapter_device_found_free(E_Bluez_Device_Found *device) EINA_ARG_NONNULL(1);
   EAPI bool e_bluez_adapter_agent_register(E_Bluez_Element *element, const char *object_path, const char *capability, E_DBus_Method_Return_Cb cb, const void *data) EINA_ARG_NONNULL(1) EINA_WARN_UNUSED_RESULT;
   EAPI bool e_bluez_adapter_agent_unregister(E_Bluez_Element *element, const char *object_path, E_DBus_Method_Return_Cb cb, const void *data) EINA_ARG_NONNULL(1) EINA_WARN_UNUSED_RESULT;
   EAPI bool e_bluez_adapter_address_get(E_Bluez_Element *element, const char **address) EINA_ARG_NONNULL(1) EINA_WARN_UNUSED_RESULT;
@@ -105,7 +123,7 @@ extern "C" {
   EAPI int e_bluez_element_unref(E_Bluez_Element *element) EINA_ARG_NONNULL(1);
 
   EAPI void e_bluez_element_print(FILE *fp, const E_Bluez_Element *element) EINA_ARG_NONNULL(1, 2);
-
+  EAPI void e_bluez_element_array_print(FILE *fp, E_Bluez_Array *array);
 
   EAPI bool e_bluez_element_properties_sync(E_Bluez_Element *element) EINA_ARG_NONNULL(1);
   EAPI bool e_bluez_element_properties_sync_full(E_Bluez_Element *element, E_DBus_Method_Return_Cb cb, const void *data) EINA_ARG_NONNULL(1);
index e51fbbc..76ab0e1 100644 (file)
@@ -19,6 +19,7 @@ EAPI int E_BLUEZ_EVENT_MANAGER_OUT = 0;
 EAPI int E_BLUEZ_EVENT_ELEMENT_ADD = 0;
 EAPI int E_BLUEZ_EVENT_ELEMENT_DEL = 0;
 EAPI int E_BLUEZ_EVENT_ELEMENT_UPDATED = 0;
+EAPI int E_BLUEZ_EVENT_DEVICE_FOUND = 0;
 
 const char *e_bluez_iface_manager = NULL;
 const char *e_bluez_iface_adapter = NULL;
@@ -196,6 +197,8 @@ _e_bluez_get_name_owner(void *data __UNUSED__, DBusMessage *msg, DBusError *err)
  *   - E_BLUEZ_EVENT_ELEMENT_DEL: element was deleted.
  *   - E_BLUEZ_EVENT_ELEMENT_UPDATED: element was updated (properties
  *     or state changed).
+ *   - E_BLUEZ_EVENT_DEVICE_FOUND: a device was found, raised after calling
+ *     Adapter.StartDiscorvery()
  *
  * Manager IN/OUT events do not provide any event information, just
  * tells you that system is usable or not. After manager is out, all
@@ -231,6 +234,8 @@ e_bluez_system_init(E_DBus_Connection *edbus_conn)
                E_BLUEZ_EVENT_ELEMENT_DEL = ecore_event_type_new();
        if (E_BLUEZ_EVENT_ELEMENT_UPDATED == 0)
                E_BLUEZ_EVENT_ELEMENT_UPDATED = ecore_event_type_new();
+       if (E_BLUEZ_EVENT_DEVICE_FOUND == 0)
+               E_BLUEZ_EVENT_DEVICE_FOUND = ecore_event_type_new();
 
        if (e_bluez_iface_manager == NULL)
                e_bluez_iface_manager = eina_stringshare_add("org.bluez.Manager");
index 9ee1b1c..c23bf3d 100644 (file)
@@ -1,5 +1,68 @@
 #include "e_bluez_private.h"
 
+static void
+_device_found_callback(void *data, DBusMessage *msg)
+{
+   E_Bluez_Element *element = (E_Bluez_Element *)data;
+   E_Bluez_Device_Found *device;
+   DBusMessageIter itr;
+   int t;
+   char *name = NULL;
+   void *value = NULL;
+
+   DBG("Device found %s", element->path);
+
+   if (!_dbus_callback_check_and_init(msg, &itr, NULL))
+     return;
+
+   device = calloc(sizeof(E_Bluez_Device_Found), 1);
+   if (!device) {
+       ERR("No memory to alocate E_Bluez_Device_Found");
+       return;
+   }
+
+   t = dbus_message_iter_get_arg_type(&itr);
+   if (!_dbus_iter_type_check(t, DBUS_TYPE_STRING))
+     {
+       ERR("missing device name in DeviceFound");
+       return;
+     }
+   dbus_message_iter_get_basic(&itr, &name);
+
+   dbus_message_iter_next(&itr);
+   t = dbus_message_iter_get_arg_type(&itr);
+   if (!_dbus_iter_type_check(t, DBUS_TYPE_ARRAY))
+     {
+       ERR("missing array in DeviceFound");
+       return;
+     }
+
+   value = e_bluez_element_iter_get_array(&itr, name);
+
+   if (!value)
+      return;
+
+   device->name = eina_stringshare_add(name);
+   device->adapter = element;
+   device->array = value;
+
+   ecore_event_add(E_BLUEZ_EVENT_DEVICE_FOUND, device, NULL, NULL);
+}
+
+/**
+ * Free a E_Bluez_Device_Found struct
+ *
+ * @param device the struct to be freed
+ */
+void
+e_bluez_adapter_device_found_free(E_Bluez_Device_Found *device)
+{
+   EINA_SAFETY_ON_NULL_RETURN(device);
+
+   eina_stringshare_del(device->name);
+   e_bluez_element_array_free(device->array, NULL);
+}
+
 /**
  * Register new agent for handling user requests.
  *
@@ -135,6 +198,12 @@ e_bluez_adapter_start_discovery(E_Bluez_Element *element, E_DBus_Method_Return_C
 
    EINA_SAFETY_ON_NULL_RETURN_VAL(element, 0);
 
+   element->device_found_handler =
+     e_dbus_signal_handler_add
+     (e_bluez_conn, e_bluez_system_bus_name_get(),
+      element->path, element->interface, "DeviceFound",
+      _device_found_callback, element);
+
    return e_bluez_element_call_full(element, name, NULL,
                   &element->_pending.start_discovery, cb, data);
 }
index ac9ef4a..87cc1ac 100644 (file)
@@ -4,19 +4,12 @@
 
 static Eina_Hash *elements = NULL;
 
-typedef struct _E_Bluez_Array E_Bluez_Array;
 typedef struct _E_Bluez_Element_Pending E_Bluez_Element_Pending;
 typedef struct _E_Bluez_Element_Call_Data E_Bluez_Element_Call_Data;
 typedef struct _E_Bluez_Element_Property E_Bluez_Element_Property;
 typedef struct _E_Bluez_Element_Listener E_Bluez_Element_Listener;
 typedef struct _E_Bluez_Element_Dict_Entry E_Bluez_Element_Dict_Entry;
 
-struct _E_Bluez_Array
-{
-   int type;
-   Eina_Array *array;
-};
-
 struct _E_Bluez_Element_Pending
 {
    EINA_INLIST;
@@ -58,6 +51,7 @@ struct _E_Bluez_Element_Dict_Entry
    union {
       bool boolean;
       const char *str;
+      short i16;
       unsigned short u16;
       unsigned int u32;
       unsigned char byte;
@@ -80,7 +74,7 @@ _e_bluez_element_event_no_free(void *data __UNUSED__, void *ev)
    e_bluez_element_unref(element);
 }
 
-static void
+void
 e_bluez_element_event_add(int event_type, E_Bluez_Element *element)
 {
    e_bluez_element_ref(element);
@@ -240,6 +234,7 @@ _e_bluez_element_dict_entry_free(E_Bluez_Element_Dict_Entry *entry)
      {
       case DBUS_TYPE_BOOLEAN:
       case DBUS_TYPE_BYTE:
+      case DBUS_TYPE_INT16:
       case DBUS_TYPE_UINT16:
       case DBUS_TYPE_UINT32:
         break;
@@ -320,6 +315,9 @@ _e_bluez_element_dict_entry_new(DBusMessageIter *itr)
       case DBUS_TYPE_BYTE:
         entry->value.byte = (unsigned char)(long)value;
         break;
+      case DBUS_TYPE_INT16:
+        entry->value.i16 = (short)(long)value;
+        break;
       case DBUS_TYPE_UINT16:
         entry->value.u16 = (unsigned short)(long)value;
         break;
@@ -358,8 +356,8 @@ _e_bluez_element_array_dict_find_stringshared(const E_Bluez_Array *array, const
    return NULL;
 }
 
-static void
-_e_bluez_element_array_free(E_Bluez_Array *array, E_Bluez_Array *new __UNUSED__)
+void
+e_bluez_element_array_free(E_Bluez_Array *array, E_Bluez_Array *new __UNUSED__)
 {
    Eina_Array_Iterator iterator;
    unsigned int i;
@@ -372,6 +370,7 @@ _e_bluez_element_array_free(E_Bluez_Array *array, E_Bluez_Array *new __UNUSED__)
      {
       case DBUS_TYPE_BOOLEAN:
       case DBUS_TYPE_BYTE:
+      case DBUS_TYPE_INT16:
       case DBUS_TYPE_UINT16:
       case DBUS_TYPE_UINT32:
         break;
@@ -415,7 +414,7 @@ _e_bluez_element_property_value_free(E_Bluez_Element_Property *property)
         eina_stringshare_del(property->value.path);
         break;
       case DBUS_TYPE_ARRAY:
-        _e_bluez_element_array_free(property->value.array, NULL);
+        e_bluez_element_array_free(property->value.array, NULL);
         break;
       default:
         ERR("don't know how to free value of property type %c (%d)",
@@ -647,7 +646,7 @@ _e_bluez_element_property_update(E_Bluez_Element_Property *property, int type, v
           if (property->value.array)
             {
                _e_bluez_element_array_match(property->value.array, data, property->name);
-               _e_bluez_element_array_free(property->value.array, data);
+               e_bluez_element_array_free(property->value.array, data);
             }
         property->value.array = data;
         changed = 1;
@@ -850,8 +849,8 @@ e_bluez_element_strings_array_get_stringshared(const E_Bluez_Element *element, c
    return 1;
 }
 
-static void
-_e_bluez_element_array_print(FILE *fp, E_Bluez_Array *array)
+void
+e_bluez_element_array_print(FILE *fp, E_Bluez_Array *array)
 {
    Eina_Array_Iterator iterator;
    unsigned int i;
@@ -899,10 +898,18 @@ _e_bluez_element_array_print(FILE *fp, E_Bluez_Array *array)
                 case DBUS_TYPE_STRING:
                    fprintf(fp, "\"%s\", ", entry->value.str);
                    break;
+                case DBUS_TYPE_BOOLEAN:
+                   fprintf(fp, "%hhu, ",
+                           entry->value.boolean);
+                   break;
                 case DBUS_TYPE_BYTE:
                    fprintf(fp, "%#02hhx (\"%c\"), ",
                            entry->value.byte, entry->value.byte);
                    break;
+                case DBUS_TYPE_INT16:
+                   fprintf(fp, "%#04hx (%hi), ",
+                           entry->value.i16, entry->value.i16);
+                   break;
                 case DBUS_TYPE_UINT16:
                    fprintf(fp, "%#04hx (%hu), ",
                            entry->value.u16, entry->value.u16);
@@ -967,7 +974,7 @@ e_bluez_element_print(FILE *fp, const E_Bluez_Element *element)
              fprintf(fp, "%u", p->value.u32);
              break;
           case DBUS_TYPE_ARRAY:
-             _e_bluez_element_array_print(fp, p->value.array);
+             e_bluez_element_array_print(fp, p->value.array);
              break;
           default:
              fputs("don't know how to print type", fp);
@@ -1170,8 +1177,8 @@ _e_bluez_element_property_value_add(E_Bluez_Element *element, const char *name,
    return 1;
 }
 
-static E_Bluez_Array *
-_e_bluez_element_iter_get_array(DBusMessageIter *itr, const char *key)
+E_Bluez_Array *
+e_bluez_element_iter_get_array(DBusMessageIter *itr, const char *key)
 {
    E_Bluez_Array *array;
    DBusMessageIter e_itr;
@@ -1294,7 +1301,7 @@ _e_bluez_element_get_properties_callback(void *user_data, DBusMessage *msg, DBus
        dbus_message_iter_recurse(&e_itr, &v_itr);
        t = dbus_message_iter_get_arg_type(&v_itr);
        if (t == DBUS_TYPE_ARRAY)
-         value = _e_bluez_element_iter_get_array(&v_itr, key);
+         value = e_bluez_element_iter_get_array(&v_itr, key);
        else if (t != DBUS_TYPE_INVALID) {
          dbus_message_iter_get_basic(&v_itr, &value);
        } else {
@@ -1754,6 +1761,9 @@ e_bluez_element_property_dict_get_stringshared(const E_Bluez_Element *element, c
           case DBUS_TYPE_BYTE:
              *(unsigned char *)value = entry->value.byte;
              return 1;
+          case DBUS_TYPE_INT16:
+             *(short *)value = entry->value.i16;
+             return 1;
           case DBUS_TYPE_UINT16:
              *(unsigned short *)value = entry->value.u16;
              return 1;
@@ -2084,7 +2094,7 @@ _e_bluez_element_property_changed_callback(void *data, DBusMessage *msg)
    t = dbus_message_iter_get_arg_type(&v_itr);
 
    if (t == DBUS_TYPE_ARRAY)
-     value = _e_bluez_element_iter_get_array(&v_itr, name);
+     value = e_bluez_element_iter_get_array(&v_itr, name);
    else if (t != DBUS_TYPE_INVALID)
      dbus_message_iter_get_basic(&v_itr, &value);
    else
index 323541f..6364d06 100644 (file)
@@ -125,6 +125,9 @@ E_Bluez_Element *e_bluez_element_register(const char *path, const char *interfac
 void e_bluez_element_unregister(E_Bluez_Element *element);
 
 bool e_bluez_element_message_send(E_Bluez_Element *element, const char *method_name, E_DBus_Method_Return_Cb cb, DBusMessage *msg, Eina_Inlist **pending, E_DBus_Method_Return_Cb user_cb, const void *user_data);
+E_Bluez_Array *e_bluez_element_iter_get_array(DBusMessageIter *itr, const char *key);
+void e_bluez_element_event_add(int event_type, E_Bluez_Element *element);
+void e_bluez_element_array_free(E_Bluez_Array *array, E_Bluez_Array *new __UNUSED__);
 
 bool e_bluez_element_call_full(E_Bluez_Element *element, const char *method_name, E_DBus_Method_Return_Cb cb, Eina_Inlist **pending, E_DBus_Method_Return_Cb user_cb, const void *user_data);
 bool e_bluez_element_call_with_path(E_Bluez_Element *element, const char *method_name, const char *string, E_DBus_Method_Return_Cb cb, Eina_Inlist **pending, E_DBus_Method_Return_Cb user_cb, const void *user_data);