1 /*******************************************************************************
2 * Copyright 2019 Samsung Electronics All Rights Reserved.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 *******************************************************************************/
22 #include <dbus_consumer.h>
24 /* ---------------------------------------------------------------------------------------------------- */
25 GDBusConnection *_gdbus_conn;
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"
37 /* ---------------------------------------------------------------------------------------------------- */
39 int dbus_consumer_initailze()
43 _gdbus_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
46 printf ("Failed to get bus access - %i : %s\n", error->code, error->message);
49 return ORCH_DBUS_ERROR_FAULT;
51 return ORCH_DBUS_ERROR_NONE;
54 int request_service_execute(char* app_name, bool self_select, service_info_s service_info[], int count, int client_pid)
59 GVariantBuilder *builder;
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);
71 builder = g_variant_builder_new (G_VARIANT_TYPE ("a(ss)"));
72 for (idx = 0; idx < count; idx++) {
73 g_variant_builder_add (
76 service_info[idx].exec_type,
77 service_info[idx].exec_parameter
81 services = g_variant_new ("(sia(ss)ii)", app_name, self_select, builder, count, client_pid);
82 g_variant_builder_unref (builder);
85 g_dbus_connection_call_sync (_gdbus_conn,
86 _ORCHESTRATION_BUS_NAME,
87 _ORCHESTRATION_OBJECT_PATH,
88 _ORCHESTRATION_INTERFACE,
89 _ORCHESTRATION_REQUEST_SERVICE_METHOD,
92 G_DBUS_CALL_FLAGS_NONE,
97 g_variant_unref (result);
101 printf("Failed to call remote method - %i : %s\n", error->code, error->message);
102 g_error_free (error);
104 g_object_unref (_gdbus_conn);
105 return ORCH_DBUS_ERROR_FAULT;
108 return ORCH_DBUS_ERROR_NONE;
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)
114 GError *error = NULL;
116 GVariantBuilder *builder;
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);
128 builder = g_variant_builder_new (G_VARIANT_TYPE ("a(ss)"));
129 for (idx = 0; idx < count; idx++) {
130 g_variant_builder_add (
133 service_info[idx].exec_type,
134 service_info[idx].exec_parameter
138 services = g_variant_new ("(sia(ss)sii)", app_name, self_select, builder, ip, count, client_pid);
139 g_variant_builder_unref (builder);
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,
149 G_DBUS_CALL_FLAGS_NONE,
154 g_variant_unref (result);
158 printf("Failed to call remote method - %i : %s\n", error->code, error->message);
159 g_error_free (error);
161 g_object_unref (_gdbus_conn);
162 return ORCH_DBUS_ERROR_FAULT;
165 return ORCH_DBUS_ERROR_NONE;
168 int get_device_list(char* service_name, char* exec_type, int client_pid, orchestration_devicelist_s **deviceList)
170 int ret = ORCH_DBUS_ERROR_NONE;
171 GError *error = NULL;
174 char *message = NULL;
178 printf("[orchestration dbus_consumer] get_device_list\n");
180 services = g_variant_new ("(ssi)", service_name, exec_type, client_pid);
183 g_dbus_connection_call_sync (_gdbus_conn,
184 _ORCHESTRATION_BUS_NAME,
185 _ORCHESTRATION_OBJECT_PATH,
186 _ORCHESTRATION_INTERFACE,
187 _ORCHESTRATION_GET_DEVICELIST_METHOD,
190 G_DBUS_CALL_FLAGS_NONE,
196 printf("Failed to call remote method - %i : %s\n", error->code, error->message);
197 g_error_free (error);
199 g_object_unref (_gdbus_conn);
200 ret = ORCH_DBUS_ERROR_FAULT;
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;
214 (*deviceList)->message = strdup(message);
218 (*deviceList)->ipaddr = (char**) malloc (sizeof(char*) * count);
219 while (g_variant_iter_loop (iter, "s", &str))
221 (*deviceList)->ipaddr[idx++] = strdup(str);
223 g_variant_iter_free (iter);
229 g_variant_unref(result);
234 int read_capability(char *ip, int client_pid, orchestration_device_capability_s **device_capability)
236 int ret = ORCH_DBUS_ERROR_NONE;
237 GError *error = NULL;
239 char *message = NULL, *capability = NULL;
241 *device_capability = NULL;
243 printf("[orchestration dbus_consumer] read device capability\n");
245 services = g_variant_new ("(si)", ip, client_pid);
248 g_dbus_connection_call_sync (_gdbus_conn,
249 _ORCHESTRATION_BUS_NAME,
250 _ORCHESTRATION_OBJECT_PATH,
251 _ORCHESTRATION_INTERFACE,
252 _ORCHESTRATION_READ_CAPABILITY_METHOD,
255 G_DBUS_CALL_FLAGS_NONE,
262 printf("Failed to call remote method - %i : %s\n", error->code, error->message);
263 g_error_free (error);
265 g_object_unref (_gdbus_conn);
266 ret = ORCH_DBUS_ERROR_FAULT;
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;
278 g_variant_unref(result);
283 int write_capability(char *capability, int client_pid)
285 int ret = ORCH_DBUS_ERROR_NONE;
286 GError *error = NULL;
289 printf("[orchestration dbus_consumer] write device capability\n");
291 services = g_variant_new ("(si)", capability, client_pid);
294 g_dbus_connection_call_sync (_gdbus_conn,
295 _ORCHESTRATION_BUS_NAME,
296 _ORCHESTRATION_OBJECT_PATH,
297 _ORCHESTRATION_INTERFACE,
298 _ORCHESTRATION_WRITE_CAPABILITY_METHOD,
301 G_DBUS_CALL_FLAGS_NONE,
307 g_variant_unref(result);
311 printf("Failed to call remote method - %i : %s\n", error->code, error->message);
312 g_error_free (error);
314 g_object_unref (_gdbus_conn);
315 ret = ORCH_DBUS_ERROR_FAULT;