Add device capability and get authenticated device list
[platform/core/system/edge-orchestration.git] / CMain / src / orchestration_service.c
1 /*******************************************************************************
2  * Copyright 2020 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 <orchestration_service.h>
19
20 #include <stdlib.h>
21 #include <stdbool.h>
22 #include <pthread.h>
23 #include <gio/gio.h>
24 #include <app_control.h>
25 #include "orchestration_key.h"
26
27 static int request_service(
28                 char* app_name,
29                 bool self_select,
30                 requestServiceInfo service_info[],
31                 int count,
32                 char* client_pname);
33 static int request_service_on_device(
34                  char* app_name,
35                 bool self_select,
36                 requestServiceInfo service_info[],
37                 char *ip,
38                 int count,
39                 char* client_pname);
40 static int update_key_pair(
41                 char* id,
42                 char* key,
43                 char* client_pname);
44 static DeviceList* get_device_list(
45                 char* service_name,
46                 char* exec_type);
47 static DeviceCapability* read_device_capability(char* ip);
48 static char* write_device_capability(char *ip);
49
50 int app_execute(char *pkg_name, char* args)
51 {
52         app_control_h ac;
53         int ret = 0;;
54         ret = app_control_create(&ac);
55
56         if (ret != APP_CONTROL_ERROR_NONE) {
57                 DEBUG("app_control_create failed : %d\n", ret);
58         }
59
60         ret = app_control_set_app_id(ac, pkg_name);
61         if (ret != APP_CONTROL_ERROR_NONE ) {
62                 DEBUG("app_control_set_app_id error");
63         }
64
65         ret = app_control_send_launch_request(ac, NULL, NULL);
66         if (ret != APP_CONTROL_ERROR_NONE) {
67                 DEBUG("app_control_send_launch_request error :%s", get_error_message(ret));
68         }
69
70         app_control_destroy(ac);
71
72         return ret;
73 }
74
75 void *orchestration_service_thread() {
76         DEBUG("Start OrchestrationInit Thread !!\n");
77         OrchestrationInit(app_execute);
78         SetPSKHandler(identityGetter, keyGetter);
79
80         return NULL;
81 }
82
83 _orchestration_state_e orchestration_service_start() {
84         pthread_t p_thread;
85
86         if (pthread_create(&p_thread, NULL, orchestration_service_thread, NULL) != 0) {
87                 DEBUG("Fail to start OrchestrationInit Thread !!");
88                 return ORCH_ERROR_NOT_READY;
89         }
90
91         return ORCH_ERROR_NONE;
92 }
93
94 dbus_funcs getDbusFuncs() {
95         dbus_funcs funcs;
96         funcs.request_service_f = request_service;
97         funcs.request_service_on_device_f = request_service_on_device;
98         funcs.update_key_pair_f = update_key_pair;
99         funcs.get_device_list_f = get_device_list;
100         funcs.read_device_capability_f = read_device_capability;
101         funcs.write_device_capability_f = write_device_capability;
102         return funcs;
103 }
104
105 static int request_service(char* app_name, bool self_select, requestServiceInfo service_info[], int count, char* client_pname){
106         int ret = ORCH_ERROR_NONE;
107         ResponseService result;
108
109         DEBUG("[orchestration_server]\n")
110         DEBUG("\t app_name : %s\n", app_name);
111         DEBUG("\t self_select : %s\n", self_select ? "true" : "false");
112         DEBUG("\t count : %d\n", count);
113         DEBUG("\t process_name : %s\n", client_pname);
114         for (int ix = 0; ix < count; ix++) {
115                 DEBUG("\t service_info[%d].ExecutionType : %s\n", ix, service_info[ix].ExecutionType);
116                 DEBUG("\t service_info[%d].ExeCmd : %s\n", ix, service_info[ix].ExeCmd);
117         }
118
119         result = OrchestrationRequestService(app_name, self_select, client_pname, service_info, count);
120         DEBUG("result = %s\n", result.Message);
121
122         /* TO DO : parsing API response */
123         return ret;
124 }
125
126 static int request_service_on_device(char* app_name, bool self_select, requestServiceInfo service_info[], char *ip, int count, char* client_pname){
127         int ret = ORCH_ERROR_NONE;
128         ResponseService result;
129
130         DEBUG("[orchestration_server]\n")
131         DEBUG("\t app_name : %s\n", app_name);
132         DEBUG("\t self_select : %s\n", self_select ? "true" : "false");
133         DEBUG("\t Device IP addr : %s \n", ip);
134         DEBUG("\t count : %d\n", count);
135         DEBUG("\t process_name : %s\n", client_pname);
136         for (int ix = 0; ix < count; ix++) {
137                 DEBUG("\t service_info[%d].ExecutionType : %s\n", ix, service_info[ix].ExecutionType);
138                 DEBUG("\t service_info[%d].ExeCmd : %s\n", ix, service_info[ix].ExeCmd);
139         }
140
141         result = OrchestrationRequestServiceOnDevice(app_name, self_select, client_pname, service_info, ip, count);
142         DEBUG("result = %s\n", result.Message);
143
144         /* TO DO : parsing API response */
145         return ret;
146 }
147
148 #define ALLOWED_KEY_UPDATER "homeedge"
149
150 static int update_key_pair(char* id, char* key, char* client_pname) {
151         if (g_strcmp0(client_pname, ALLOWED_KEY_UPDATER) != 0) {
152                 DEBUG("process_name : %s -> is not allowed.\n", client_pname);
153                 return -1;
154         }
155
156         return updateKeyPair(id, key);
157 }
158
159 static DeviceList* get_device_list(char* service_name, char* exec_type) {
160         return OrchestrationGetDeviceList(service_name, exec_type);
161 }
162
163 static DeviceCapability* read_device_capability(char* ip) {
164         return OrchestrationReadCapability(ip);
165 }
166
167 static char* write_device_capability(char* capability) {
168         return OrchestrationWriteCapability(capability);
169 }