Add device capability and get authenticated device list
[platform/core/system/edge-orchestration.git] / libedge-orchestration / src / orchestration_client.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 <stdlib.h>
19 #include <stdio.h>
20 #include <string.h>
21 #include <dbus_consumer.h>
22 #include <gio/gio.h>
23
24 /* ---------------------------------------------------------------------------------------------------- */
25 orchestration_changed_service_status_cb _changed_service_status_cb;
26
27 #define _ORCHESTRATION_BUS_NAME "org.tizen.orchestration"
28 #define _ORCHESTRATION_OBJECT_PATH "/org/tizen/orchestration"
29 #define _ORCHESTRATION_INTERFACE "org.tizen.orchestration.agent"
30 #define _ORCHESTRATION_REQUEST_SERVICE_METHOD "request_service"
31
32
33 orchestration_client_state_e orchestration_get_devicelist(char *service_name, char *exec_type, orchestration_devicelist_s **deviceList)
34 {
35     *deviceList = NULL;
36
37     int client_pid = getpid();
38     int result = 0;
39
40     result = dbus_consumer_initailze();
41     if (result != ORCH_DBUS_ERROR_NONE)
42     {
43         printf("dbus_consumer_initailze failed\n");
44         return ORCH_CLIENT_ERROR_FAULT;
45     }
46
47     result = get_device_list(service_name, exec_type, client_pid, deviceList);
48     if (result != ORCH_DBUS_ERROR_NONE)
49     {
50         printf("get device list failed\n");
51         return ORCH_CLIENT_ERROR_FAULT;
52     }
53     return ORCH_CLIENT_ERROR_NONE;
54 }
55
56 orchestration_client_state_e orchestration_read_capability(char *ip, orchestration_device_capability_s **device_capability)
57 {
58     *device_capability = NULL;
59     if (!ip)
60     {
61         printf("[read cappability] ip cannot be empty!!\n");
62         return ORCH_CLIENT_ERROR_INVALID_PARAMETER;
63     }
64
65     int client_pid = getpid();
66     int result = 0;
67
68     result = dbus_consumer_initailze();
69     if (result != ORCH_DBUS_ERROR_NONE)
70     {
71         printf("dbus_consumer_initialize failed\n");
72         return ORCH_CLIENT_ERROR_FAULT;
73     }
74
75     result = read_capability(ip, client_pid, device_capability);
76     if (result != ORCH_DBUS_ERROR_NONE)
77     {
78         printf("read capability failed\n");
79         return ORCH_CLIENT_ERROR_FAULT;
80     }
81     return ORCH_CLIENT_ERROR_NONE;
82 }
83
84 orchestration_client_state_e orchestration_write_capability(char *capability)
85 {
86     int client_pid = getpid();
87     int result = 0;
88
89     result = dbus_consumer_initailze();
90     if (result != ORCH_DBUS_ERROR_NONE)
91     {
92         printf("dbus_consumer_initialize failed\n");
93         return ORCH_CLIENT_ERROR_FAULT;
94     }
95
96     result = write_capability(capability, client_pid);
97     if (result != ORCH_DBUS_ERROR_NONE)
98     {
99         printf("write capability failed\n");
100         return ORCH_CLIENT_ERROR_FAULT;
101     }
102     return ORCH_CLIENT_ERROR_NONE;
103 }
104
105 orchestration_client_state_e orchestration_request_service(char *app_name,
106                                                         bool self_select,
107                                                         orchestration_service_info_s service_info,
108                                                         orchestration_changed_service_status_cb cb,
109                                                         void *user_data)
110 {
111     int result = 0;
112     int client_pid = getpid();
113
114     if (strcmp(app_name, "") == 0)
115     {
116         printf("app_name is null\n");
117         return ORCH_CLIENT_ERROR_INVALID_PARAMETER;
118     }
119     if (cb == NULL)
120     {
121         printf("orchestration_changed_service_status_cb is null\n");
122         return ORCH_CLIENT_ERROR_INVALID_PARAMETER;
123     }
124     _changed_service_status_cb = cb;
125
126     result = dbus_consumer_initailze();
127     if (result != ORCH_DBUS_ERROR_NONE)
128     {
129         printf("dbus_consumer_initailze failed\n");
130         return ORCH_CLIENT_ERROR_FAULT;
131     }
132
133     printf("[orchestration_client]\n");
134     printf("\t client_pid : %d\n", client_pid);
135     printf("\t app_name : %s\n", app_name);
136     printf("\t self_select : %s\n", self_select ? "true" : "false");
137     printf("\t count : %d\n", service_info.count);
138     for (int i = 0; i < service_info.count; i++) {
139         printf("\t service_info[%d]->exec_type : %s\n", i, service_info.services[i].exec_type);
140         printf("\t service_info[%d]->exec_parameter : %s\n", i, service_info.services[i].exec_parameter);
141     }
142
143     result = request_service_execute(app_name, self_select, service_info.services, service_info.count, client_pid);
144     if (result != ORCH_DBUS_ERROR_NONE)
145     {
146         printf("request_service_execute failed\n");
147         return ORCH_CLIENT_ERROR_FAULT;
148     }
149
150     return ORCH_CLIENT_ERROR_NONE;
151 }
152
153 orchestration_client_state_e orchestration_request_service_on_device(char *app_name,
154                                                         bool self_select,
155                                                         orchestration_service_info_s service_info,
156                                                         char *ip,
157                                                         orchestration_changed_service_status_cb cb,
158                                                         void *user_data)
159 {
160     int result = 0;
161     int client_pid = getpid();
162
163     if (strcmp(app_name, "") == 0)
164     {
165         printf("app_name is null\n");
166         return ORCH_CLIENT_ERROR_INVALID_PARAMETER;
167     }
168     if (cb == NULL)
169     {
170         printf("orchestration_changed_service_status_cb is null\n");
171         return ORCH_CLIENT_ERROR_INVALID_PARAMETER;
172     }
173     _changed_service_status_cb = cb;
174
175     result = dbus_consumer_initailze();
176     if (result != ORCH_DBUS_ERROR_NONE)
177     {
178         printf("dbus_consumer_initailze failed\n");
179         return ORCH_CLIENT_ERROR_FAULT;
180     }
181
182     printf("[orchestration_client]\n");
183     printf("\t client_pid : %d\n", client_pid);
184     printf("\t app_name : %s\n", app_name);
185     printf("\t self_select : %s\n", self_select ? "true" : "false");
186     printf("\t count : %d\n", service_info.count);
187     for (int i = 0; i < service_info.count; i++) {
188         printf("\t service_info[%d]->exec_type : %s\n", i, service_info.services[i].exec_type);
189         printf("\t service_info[%d]->exec_parameter : %s\n", i, service_info.services[i].exec_parameter);
190     }
191
192     result = request_service_execute_on_device(app_name, self_select, service_info.services, ip, service_info.count, client_pid);
193     if (result != ORCH_DBUS_ERROR_NONE)
194     {
195         printf("request_service_execute failed\n");
196         return ORCH_CLIENT_ERROR_FAULT;
197     }
198
199     return ORCH_CLIENT_ERROR_NONE;
200 }