Add device capability and get authenticated device list
[platform/core/system/edge-orchestration.git] / libedge-orchestration / src / dbus_consumer.c
index b98b54c..b3bc15e 100755 (executable)
@@ -29,6 +29,10 @@ gchar* _name_owner;
 #define _ORCHESTRATION_OBJECT_PATH "/org/tizen/orchestration"
 #define _ORCHESTRATION_INTERFACE "org.tizen.orchestration.agent"
 #define _ORCHESTRATION_REQUEST_SERVICE_METHOD "request_service"
+#define _ORCHESTRATION_REQUEST_SERVICE_ON_DEVICE_METHOD "request_service_on_device"
+#define _ORCHESTRATION_GET_DEVICELIST_METHOD  "get_device_list"
+#define _ORCHESTRATION_READ_CAPABILITY_METHOD "read_capability"
+#define _ORCHESTRATION_WRITE_CAPABILITY_METHOD "write_capability"
 
 /* ---------------------------------------------------------------------------------------------------- */
 
@@ -94,12 +98,222 @@ int request_service_execute(char* app_name, bool self_select, service_info_s ser
 
     if (error)
     {
-               printf("Failed to call remote method - %i : %s\n", error->code, error->message);
-               g_error_free (error);
+        printf("Failed to call remote method - %i : %s\n", error->code, error->message);
+        g_error_free (error);
         error = NULL;
-               g_object_unref (_gdbus_conn);
-               return ORCH_DBUS_ERROR_FAULT;
-       }
+        g_object_unref (_gdbus_conn);
+        return ORCH_DBUS_ERROR_FAULT;
+    }
 
     return ORCH_DBUS_ERROR_NONE;
-}
\ No newline at end of file
+}
+
+int request_service_execute_on_device(char* app_name, bool self_select, service_info_s service_info[], char* ip, int count, int client_pid)
+{
+    int idx;
+    GError *error = NULL;
+    GVariant *services;
+    GVariantBuilder *builder;
+
+    printf("[orchestration dbus_consumer] request_service_execute\n");
+    printf("\t app_name = %s\n", app_name);
+    printf("\t self_select = %s\n", self_select ? "true" : "false");
+    printf("\t count = %d\n", count);
+    printf("\t client_pid = %d\n", client_pid);
+    for (idx = 0; idx < count; idx++) {
+        printf("\t service_info[%d].exec_type = %s\n", idx, service_info[idx].exec_type);
+        printf("\t service_info[%d].exec_parameter = %s\n", idx, service_info[idx].exec_parameter);
+    }
+
+    builder = g_variant_builder_new (G_VARIANT_TYPE ("a(ss)"));
+    for (idx = 0; idx < count; idx++) {
+        g_variant_builder_add (
+            builder,
+            "(ss)",
+            service_info[idx].exec_type,
+            service_info[idx].exec_parameter
+        );
+    }
+
+    services = g_variant_new ("(sia(ss)sii)", app_name, self_select, builder, ip, count, client_pid);
+    g_variant_builder_unref (builder);
+
+    GVariant *result =
+        g_dbus_connection_call_sync (_gdbus_conn,
+                                _ORCHESTRATION_BUS_NAME,
+                                _ORCHESTRATION_OBJECT_PATH,
+                                _ORCHESTRATION_INTERFACE,
+                                _ORCHESTRATION_REQUEST_SERVICE_ON_DEVICE_METHOD,
+                                services,
+                                NULL,
+                                G_DBUS_CALL_FLAGS_NONE,
+                                -1,
+                                NULL,
+                                &error);
+        if (result)
+        g_variant_unref (result);
+
+    if (error)
+    {
+        printf("Failed to call remote method - %i : %s\n", error->code, error->message);
+        g_error_free (error);
+        error = NULL;
+        g_object_unref (_gdbus_conn);
+        return ORCH_DBUS_ERROR_FAULT;
+    }
+
+    return ORCH_DBUS_ERROR_NONE;
+}
+
+int get_device_list(char* service_name, char* exec_type, int client_pid, orchestration_devicelist_s **deviceList)
+{
+    int ret = ORCH_DBUS_ERROR_NONE;
+    GError *error = NULL;
+    GVariant *services;
+    int count;
+    char *message = NULL;
+
+    *deviceList = NULL;
+
+    printf("[orchestration dbus_consumer] get_device_list\n");
+
+    services = g_variant_new ("(ssi)", service_name, exec_type, client_pid);
+
+    GVariant *result =
+        g_dbus_connection_call_sync (_gdbus_conn,
+                                _ORCHESTRATION_BUS_NAME,
+                                _ORCHESTRATION_OBJECT_PATH,
+                                _ORCHESTRATION_INTERFACE,
+                                _ORCHESTRATION_GET_DEVICELIST_METHOD,
+                                services,
+                                NULL,
+                                G_DBUS_CALL_FLAGS_NONE,
+                                -1,
+                                NULL,
+                                &error);
+    if (error)
+    {
+        printf("Failed to call remote method - %i : %s\n", error->code, error->message);
+        g_error_free (error);
+        error = NULL;
+        g_object_unref (_gdbus_conn);
+        ret = ORCH_DBUS_ERROR_FAULT;
+    }
+    else
+    {
+        int idx = 0;
+       GVariantIter *iter;
+       gchar *str;
+        (*deviceList) = (orchestration_devicelist_s*) malloc(sizeof(orchestration_devicelist_s));
+       g_variant_get (result, "((asis))", &iter, &count, &message);
+        (*deviceList)->num_ip = count;
+        (*deviceList)->ipaddr = NULL;
+        (*deviceList)->message = NULL;
+        if (message)
+        {
+            (*deviceList)->message = strdup(message);
+        }
+        if (count > 0)
+        {
+            (*deviceList)->ipaddr = (char**) malloc (sizeof(char*) * count);
+           while (g_variant_iter_loop (iter, "s", &str))
+            {
+                (*deviceList)->ipaddr[idx++] = strdup(str);
+           }
+           g_variant_iter_free (iter);
+    
+        }
+    }
+
+    if (result)
+        g_variant_unref(result);
+
+    return ret;
+}
+
+int read_capability(char *ip, int client_pid, orchestration_device_capability_s **device_capability)
+{
+    int ret = ORCH_DBUS_ERROR_NONE;
+    GError *error = NULL;
+    GVariant *services;
+    char *message = NULL, *capability = NULL;
+
+    *device_capability = NULL;
+
+    printf("[orchestration dbus_consumer] read device capability\n");
+
+    services = g_variant_new ("(si)", ip, client_pid);
+
+    GVariant *result =
+        g_dbus_connection_call_sync (_gdbus_conn,
+                                _ORCHESTRATION_BUS_NAME,
+                                _ORCHESTRATION_OBJECT_PATH,
+                                _ORCHESTRATION_INTERFACE,
+                                _ORCHESTRATION_READ_CAPABILITY_METHOD,
+                                services,
+                                NULL,
+                                G_DBUS_CALL_FLAGS_NONE,
+                                -1,
+                                NULL,
+                                &error);
+
+    if (error)
+    {
+        printf("Failed to call remote method - %i : %s\n", error->code, error->message);
+        g_error_free (error);
+        error = NULL;
+        g_object_unref (_gdbus_conn);
+        ret = ORCH_DBUS_ERROR_FAULT;
+    }
+    else
+    {
+        printf("Read capability success !!!\n");
+        g_variant_get(result, "((ss))", &capability, &message);
+        *device_capability = (orchestration_device_capability_s *) malloc(sizeof(orchestration_device_capability_s));
+        (*device_capability)->message = message;
+        (*device_capability)->capability = capability;        
+    }
+
+    if (result)
+        g_variant_unref(result);
+
+    return ret;
+}
+
+int write_capability(char *capability, int client_pid)
+{
+    int ret = ORCH_DBUS_ERROR_NONE;
+    GError *error = NULL;
+    GVariant *services;
+
+    printf("[orchestration dbus_consumer] write device capability\n");
+
+    services = g_variant_new ("(si)", capability, client_pid);
+
+    GVariant *result =
+        g_dbus_connection_call_sync (_gdbus_conn,
+                                _ORCHESTRATION_BUS_NAME,
+                                _ORCHESTRATION_OBJECT_PATH,
+                                _ORCHESTRATION_INTERFACE,
+                                _ORCHESTRATION_WRITE_CAPABILITY_METHOD,
+                                services,
+                                NULL,
+                                G_DBUS_CALL_FLAGS_NONE,
+                                -1,
+                                NULL,
+                                &error);
+
+    if (result)
+        g_variant_unref(result);
+
+    if (error)
+    {
+        printf("Failed to call remote method - %i : %s\n", error->code, error->message);
+        g_error_free (error);
+        error = NULL;
+        g_object_unref (_gdbus_conn);
+        ret = ORCH_DBUS_ERROR_FAULT;
+    }
+
+    return ret;
+}