Add device capability and get authenticated device list
[platform/core/system/edge-orchestration.git] / libedge-orchestration / src / dbus_consumer.c
1 /*******************************************************************************
2  * Copyright 2019 Samsung Electronics All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  *******************************************************************************/
17
18 #include <gio/gio.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21
22 #include <dbus_consumer.h>
23
24 /* ---------------------------------------------------------------------------------------------------- */
25 GDBusConnection *_gdbus_conn;
26 gchar* _name_owner;
27
28 #define _ORCHESTRATION_BUS_NAME "org.tizen.orchestration"
29 #define _ORCHESTRATION_OBJECT_PATH "/org/tizen/orchestration"
30 #define _ORCHESTRATION_INTERFACE "org.tizen.orchestration.agent"
31 #define _ORCHESTRATION_REQUEST_SERVICE_METHOD "request_service"
32 #define _ORCHESTRATION_REQUEST_SERVICE_ON_DEVICE_METHOD "request_service_on_device"
33 #define _ORCHESTRATION_GET_DEVICELIST_METHOD  "get_device_list"
34 #define _ORCHESTRATION_READ_CAPABILITY_METHOD "read_capability"
35 #define _ORCHESTRATION_WRITE_CAPABILITY_METHOD "write_capability"
36
37 /* ---------------------------------------------------------------------------------------------------- */
38
39 int dbus_consumer_initailze()
40 {
41     GError *error = NULL;
42
43     _gdbus_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
44         if (error)
45     {
46                 printf ("Failed to get bus access - %i : %s\n", error->code, error->message);
47                 g_error_free (error);
48         error = NULL;
49                 return ORCH_DBUS_ERROR_FAULT;
50         }
51     return ORCH_DBUS_ERROR_NONE;
52 }
53
54 int request_service_execute(char* app_name, bool self_select, service_info_s service_info[], int count, int client_pid)
55 {
56     int idx;
57     GError *error = NULL;
58     GVariant *services;
59     GVariantBuilder *builder;
60
61     printf("[orchestration dbus_consumer] request_service_execute\n");
62     printf("\t app_name = %s\n", app_name);
63     printf("\t self_select = %s\n", self_select ? "true" : "false");
64     printf("\t count = %d\n", count);
65     printf("\t client_pid = %d\n", client_pid);
66     for (idx = 0; idx < count; idx++) {
67         printf("\t service_info[%d].exec_type = %s\n", idx, service_info[idx].exec_type);
68         printf("\t service_info[%d].exec_parameter = %s\n", idx, service_info[idx].exec_parameter);
69     }
70
71     builder = g_variant_builder_new (G_VARIANT_TYPE ("a(ss)"));
72     for (idx = 0; idx < count; idx++) {
73         g_variant_builder_add (
74             builder,
75             "(ss)",
76             service_info[idx].exec_type,
77             service_info[idx].exec_parameter
78         );
79     }
80
81     services = g_variant_new ("(sia(ss)ii)", app_name, self_select, builder, count, client_pid);
82     g_variant_builder_unref (builder);
83
84     GVariant *result =
85         g_dbus_connection_call_sync (_gdbus_conn,
86                                 _ORCHESTRATION_BUS_NAME,
87                                 _ORCHESTRATION_OBJECT_PATH,
88                                 _ORCHESTRATION_INTERFACE,
89                                 _ORCHESTRATION_REQUEST_SERVICE_METHOD,
90                                 services,
91                                 NULL,
92                                 G_DBUS_CALL_FLAGS_NONE,
93                                 -1,
94                                 NULL,
95                                 &error);
96         if (result)
97         g_variant_unref (result);
98
99     if (error)
100     {
101         printf("Failed to call remote method - %i : %s\n", error->code, error->message);
102         g_error_free (error);
103         error = NULL;
104         g_object_unref (_gdbus_conn);
105         return ORCH_DBUS_ERROR_FAULT;
106     }
107
108     return ORCH_DBUS_ERROR_NONE;
109 }
110
111 int request_service_execute_on_device(char* app_name, bool self_select, service_info_s service_info[], char* ip, int count, int client_pid)
112 {
113     int idx;
114     GError *error = NULL;
115     GVariant *services;
116     GVariantBuilder *builder;
117
118     printf("[orchestration dbus_consumer] request_service_execute\n");
119     printf("\t app_name = %s\n", app_name);
120     printf("\t self_select = %s\n", self_select ? "true" : "false");
121     printf("\t count = %d\n", count);
122     printf("\t client_pid = %d\n", client_pid);
123     for (idx = 0; idx < count; idx++) {
124         printf("\t service_info[%d].exec_type = %s\n", idx, service_info[idx].exec_type);
125         printf("\t service_info[%d].exec_parameter = %s\n", idx, service_info[idx].exec_parameter);
126     }
127
128     builder = g_variant_builder_new (G_VARIANT_TYPE ("a(ss)"));
129     for (idx = 0; idx < count; idx++) {
130         g_variant_builder_add (
131             builder,
132             "(ss)",
133             service_info[idx].exec_type,
134             service_info[idx].exec_parameter
135         );
136     }
137
138     services = g_variant_new ("(sia(ss)sii)", app_name, self_select, builder, ip, count, client_pid);
139     g_variant_builder_unref (builder);
140
141     GVariant *result =
142         g_dbus_connection_call_sync (_gdbus_conn,
143                                 _ORCHESTRATION_BUS_NAME,
144                                 _ORCHESTRATION_OBJECT_PATH,
145                                 _ORCHESTRATION_INTERFACE,
146                                 _ORCHESTRATION_REQUEST_SERVICE_ON_DEVICE_METHOD,
147                                 services,
148                                 NULL,
149                                 G_DBUS_CALL_FLAGS_NONE,
150                                 -1,
151                                 NULL,
152                                 &error);
153         if (result)
154         g_variant_unref (result);
155
156     if (error)
157     {
158         printf("Failed to call remote method - %i : %s\n", error->code, error->message);
159         g_error_free (error);
160         error = NULL;
161         g_object_unref (_gdbus_conn);
162         return ORCH_DBUS_ERROR_FAULT;
163     }
164
165     return ORCH_DBUS_ERROR_NONE;
166 }
167
168 int get_device_list(char* service_name, char* exec_type, int client_pid, orchestration_devicelist_s **deviceList)
169 {
170     int ret = ORCH_DBUS_ERROR_NONE;
171     GError *error = NULL;
172     GVariant *services;
173     int count;
174     char *message = NULL;
175
176     *deviceList = NULL;
177
178     printf("[orchestration dbus_consumer] get_device_list\n");
179
180     services = g_variant_new ("(ssi)", service_name, exec_type, client_pid);
181
182     GVariant *result =
183         g_dbus_connection_call_sync (_gdbus_conn,
184                                 _ORCHESTRATION_BUS_NAME,
185                                 _ORCHESTRATION_OBJECT_PATH,
186                                 _ORCHESTRATION_INTERFACE,
187                                 _ORCHESTRATION_GET_DEVICELIST_METHOD,
188                                 services,
189                                 NULL,
190                                 G_DBUS_CALL_FLAGS_NONE,
191                                 -1,
192                                 NULL,
193                                 &error);
194     if (error)
195     {
196         printf("Failed to call remote method - %i : %s\n", error->code, error->message);
197         g_error_free (error);
198         error = NULL;
199         g_object_unref (_gdbus_conn);
200         ret = ORCH_DBUS_ERROR_FAULT;
201     }
202     else
203     {
204         int idx = 0;
205         GVariantIter *iter;
206         gchar *str;
207         (*deviceList) = (orchestration_devicelist_s*) malloc(sizeof(orchestration_devicelist_s));
208         g_variant_get (result, "((asis))", &iter, &count, &message);
209         (*deviceList)->num_ip = count;
210         (*deviceList)->ipaddr = NULL;
211         (*deviceList)->message = NULL;
212         if (message)
213         {
214             (*deviceList)->message = strdup(message);
215         }
216         if (count > 0)
217         {
218             (*deviceList)->ipaddr = (char**) malloc (sizeof(char*) * count);
219             while (g_variant_iter_loop (iter, "s", &str))
220             {
221                 (*deviceList)->ipaddr[idx++] = strdup(str);
222             }
223             g_variant_iter_free (iter);
224     
225         }
226     }
227
228     if (result)
229         g_variant_unref(result);
230
231     return ret;
232 }
233
234 int read_capability(char *ip, int client_pid, orchestration_device_capability_s **device_capability)
235 {
236     int ret = ORCH_DBUS_ERROR_NONE;
237     GError *error = NULL;
238     GVariant *services;
239     char *message = NULL, *capability = NULL;
240
241     *device_capability = NULL;
242
243     printf("[orchestration dbus_consumer] read device capability\n");
244
245     services = g_variant_new ("(si)", ip, client_pid);
246
247     GVariant *result =
248         g_dbus_connection_call_sync (_gdbus_conn,
249                                 _ORCHESTRATION_BUS_NAME,
250                                 _ORCHESTRATION_OBJECT_PATH,
251                                 _ORCHESTRATION_INTERFACE,
252                                 _ORCHESTRATION_READ_CAPABILITY_METHOD,
253                                 services,
254                                 NULL,
255                                 G_DBUS_CALL_FLAGS_NONE,
256                                 -1,
257                                 NULL,
258                                 &error);
259
260     if (error)
261     {
262         printf("Failed to call remote method - %i : %s\n", error->code, error->message);
263         g_error_free (error);
264         error = NULL;
265         g_object_unref (_gdbus_conn);
266         ret = ORCH_DBUS_ERROR_FAULT;
267     }
268     else
269     {
270         printf("Read capability success !!!\n");
271         g_variant_get(result, "((ss))", &capability, &message);
272         *device_capability = (orchestration_device_capability_s *) malloc(sizeof(orchestration_device_capability_s));
273         (*device_capability)->message = message;
274         (*device_capability)->capability = capability;        
275     }
276
277     if (result)
278         g_variant_unref(result);
279
280     return ret;
281 }
282
283 int write_capability(char *capability, int client_pid)
284 {
285     int ret = ORCH_DBUS_ERROR_NONE;
286     GError *error = NULL;
287     GVariant *services;
288
289     printf("[orchestration dbus_consumer] write device capability\n");
290
291     services = g_variant_new ("(si)", capability, client_pid);
292
293     GVariant *result =
294         g_dbus_connection_call_sync (_gdbus_conn,
295                                 _ORCHESTRATION_BUS_NAME,
296                                 _ORCHESTRATION_OBJECT_PATH,
297                                 _ORCHESTRATION_INTERFACE,
298                                 _ORCHESTRATION_WRITE_CAPABILITY_METHOD,
299                                 services,
300                                 NULL,
301                                 G_DBUS_CALL_FLAGS_NONE,
302                                 -1,
303                                 NULL,
304                                 &error);
305
306     if (result)
307         g_variant_unref(result);
308
309     if (error)
310     {
311         printf("Failed to call remote method - %i : %s\n", error->code, error->message);
312         g_error_free (error);
313         error = NULL;
314         g_object_unref (_gdbus_conn);
315         ret = ORCH_DBUS_ERROR_FAULT;
316     }
317
318     return ret;
319 }