Fix build error in sticker-receiver
[platform/core/uifw/capi-ui-sticker.git] / receiver / src / main.cpp
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd. 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 #include <app_common.h>
18 #include <service_app.h>
19 #include <stdlib.h>
20 #include <app_event.h>
21 #include <device/battery.h>
22 #include <vconf.h>
23 #include <string>
24 #include <app_preference.h>
25 #include <Ecore.h>
26
27 #include "main.h"
28 #include "ft.h"
29 #include "log.h"
30 #include "config.h"
31 #include "receiver_preference.h"
32 #include "sticker_info.h"
33 #include "sticker_request.h"
34
35 using namespace std;
36
37 static bool check_rw_mount();
38
39 static bool app_create(void *data)
40 {
41     /* Hook to take necessary actions before main event loop starts
42        Initialize UI resources and application's data
43        If this function returns true, the main loop of application starts
44        If this function returns false, the application is terminated */
45
46     if (!check_rw_mount()) {
47         service_app_exit();
48         return true;
49     }
50
51     STLOGD("");
52
53     char log_path[PATH_MAX];
54     char *data_path = NULL;
55     data_path = app_get_shared_data_path();
56     snprintf(log_path, sizeof(log_path), "%s/log", data_path);
57
58     if (data_path)
59         free(data_path);
60
61     if (access(log_path, F_OK) != 0) {
62         if (mkdir(log_path, 0755) == -1) {
63             STLOGE("directory create error");
64         }
65     }
66
67     create_sticker_provider_handle();
68
69     return true;
70 }
71
72 static bool check_battery_condition()
73 {
74     int battery_percentage = 0;
75     int ret;
76
77     // check battery percentage
78     ret = device_battery_get_percent(&battery_percentage);
79     if (ret != DEVICE_ERROR_NONE)
80     {
81         STLOGW("No sync. Failed to get battery percent. error : %d", ret);
82         return false;
83     }
84
85     STLOGI("battery percent : %d", battery_percentage);
86     if (battery_percentage >= MINIMUM_BATTERY)
87         return true;
88     else
89     {
90         STLOGI("No sync due to insufficient battery");
91         return false;
92     }
93 }
94
95 static bool check_sync_time_condition()
96 {
97     double last_sync_time;
98     int feature_flag = 0;
99     bool result = false;
100
101 #ifdef VCONFKEY_STICKER_SUPPORTED_FEATURE
102     // Check whether oobe has been done
103     if (vconf_get_int(VCONFKEY_STICKER_SUPPORTED_FEATURE, &feature_flag) == 0 && feature_flag > 0)
104     {
105         if (preference_get_double(LAST_SYNC_TIME, &last_sync_time) != PREFERENCE_ERROR_NONE)
106         {
107             STLOGD("Can't get last sync time.");
108             return true;
109         }
110
111         // compare time
112         double timediff = ecore_time_unix_get() - last_sync_time;
113         STLOGD("current time : %.3f, last_sync_time : %.3f, diff : %.3f", ecore_time_unix_get(), last_sync_time, timediff);
114
115         if (timediff > MAX_WAIT_TIME) {
116             STLOGD("Starting manual synchronization");
117             initialize_sap();
118             if (false == request_show_sync_notification())
119                 push_sticker_request(REQUEST_TYPE_SHOW_NOTIFICATION, NULL, NULL, NULL);
120             result = false;
121         } else {
122             if (timediff > SYNC_INTERVAL)
123                 result = true;
124             else
125                 result = false;
126         }
127     }
128     else
129     {
130         result = false;
131     }
132 #endif /* VCONFKEY_STICKER_SUPPORTED_FEATURE */
133
134     return result;
135 }
136
137 bool check_rw_mount()
138 {
139     char *data_path = NULL;
140     bool rw_mount = false;
141     data_path = app_get_shared_data_path();
142     if (!data_path) return false;
143
144     if (access(data_path, R_OK) == 0)
145         rw_mount = true;
146
147     free(data_path);
148
149     if (!rw_mount) {
150         SECURE_LOGE("Can't access rw storage\n");
151     }
152
153     return rw_mount;
154 }
155
156 static void process_request(app_control_h app_control, char *request)
157 {
158     char* mode = NULL;
159     char* type = NULL;
160
161     if (strcmp(request, "sync") == 0) {
162         bool param_error = false;
163         if (app_control_get_extra_data(app_control, "mode", &mode) != APP_CONTROL_ERROR_NONE) {
164             STLOGE("No given mode");
165             param_error = true;
166         }
167
168         if (app_control_get_extra_data(app_control, "type", &type) != APP_CONTROL_ERROR_NONE) {
169             STLOGE("No given type");
170             param_error = true;
171         }
172
173         STLOGI("[sync request] mode : %s, type : %s", mode, type);
174         if (param_error)
175             goto cleanup;
176
177         if (mode && type) {
178             if (check_battery_condition()) {
179                 if (check_sync_time_condition()) {
180                     if (preference_set_int(LAST_SYNC_STATUS, LAST_SYNC_STATUS_SYNC_NEEDED) != PREFERENCE_ERROR_NONE)
181                         STLOGE("Failed to set sync status as NEEDED");
182
183                     if (!is_init_sap())
184                         initialize_sap();
185                     request_all_sticker_data(mode, type);
186                 } else { // Under the sync interval time. Need to check whether last sync was succeded or not.
187                     int last_sync_status = 0;
188
189                     if (preference_get_int(LAST_SYNC_STATUS, &last_sync_status) != PREFERENCE_ERROR_NONE) {
190                         STLOGE("Failed to get sync status. Default action is exit.");
191                         goto cleanup;
192                     }
193
194                     if (last_sync_status == LAST_SYNC_STATUS_SYNC_NEEDED) {
195                         STLOGD("Last sync status is NEEDED(code: %d). Retrying to sync.", last_sync_status);
196                         if (!is_init_sap())
197                             initialize_sap();
198                         request_all_sticker_data(mode, type);
199                     } else {
200                         STLOGD("Last sync status is SUCCESS(code: %d). Don't have to retrying sync", last_sync_status);
201                     }
202                 }
203             } else {
204                 STLOGD("Not enough battery.");
205             }
206         }
207     }
208     else if (strcmp(request, "oobe") == 0) {
209         initialize_sap();
210
211         if (false == request_sticker_feature())
212             push_sticker_request(REQUEST_TYPE_FEATURE_REQ, NULL, NULL, NULL);
213     }
214     else {
215         STLOGW("Unknown command : %s", request);
216         if (!is_init_sap()) {
217             service_app_exit();
218         }
219     }
220
221 cleanup:
222     if (NULL != mode)
223         free(mode);
224
225     if (NULL != type)
226         free(type);
227 }
228
229 static void process_auto_sync()
230 {
231     if (is_init_sap()) {
232         STLOGD("continue doing current job");
233         return;
234     }
235
236     if (check_sync_time_condition()) {
237         if (check_battery_condition()) {
238             STLOGD("Starting auto synchronization");
239             if (preference_set_int(LAST_SYNC_STATUS, LAST_SYNC_STATUS_SYNC_NEEDED) != PREFERENCE_ERROR_NONE)
240                 STLOGE("Failed to set sync status as NEEDED");
241             initialize_sap();
242
243             if (false == request_sticker_feature())
244                 push_sticker_request(REQUEST_TYPE_FEATURE_REQ, NULL, NULL, NULL);
245
246             push_sticker_request(REQUEST_TYPE_AUTOSYNC, NULL, NULL, NULL);
247         }
248         else {
249             STLOGI("The status of battery is low");
250             if (!get_job_progress()) {
251                 STLOGD("exit");
252                 service_app_exit();
253             }
254         }
255     }
256     else {
257         if (!get_job_progress()) {
258             int last_sync_status = 0;
259             if (preference_get_int(LAST_SYNC_STATUS, &last_sync_status) != PREFERENCE_ERROR_NONE) {
260                 STLOGE("Failed to get sync status. Default action is exit.");
261                 service_app_exit();
262             }
263
264             if (last_sync_status == LAST_SYNC_STATUS_SYNC_NEEDED) {
265                 STLOGD("Last sync status is NEEDED(code: %d). Retrying to sync.", last_sync_status);
266                 initialize_sap();
267
268                 if (false == request_sticker_feature())
269                     push_sticker_request(REQUEST_TYPE_FEATURE_REQ, NULL, NULL, NULL);
270
271                 push_sticker_request(REQUEST_TYPE_AUTOSYNC, NULL, NULL, NULL);
272             } else {
273                 STLOGD("Last sync status is SUCCESS(code: %d). Don't have to retrying sync", last_sync_status);
274                 service_app_exit();
275             }
276         }
277     }
278 }
279
280 static void get_sticker_feature()
281 {
282 #ifdef VCONFKEY_STICKER_SUPPORTED_FEATURE
283     // Check whether oobe has been done
284     int feature_flag = 0;
285     if (vconf_get_int(VCONFKEY_STICKER_SUPPORTED_FEATURE, &feature_flag) == 0 && feature_flag == 0) {
286         STLOGD("Request to get sticker feature");
287         initialize_sap();
288
289         if (false == request_sticker_feature())
290             push_sticker_request(REQUEST_TYPE_FEATURE_REQ, NULL, NULL, NULL);
291     }
292     else {
293         if (!is_init_sap())
294             service_app_exit();
295     }
296 #endif
297 }
298
299 static void app_control(app_control_h app_control, void *data)
300 {
301     /* Handle the launch request. */
302     char* request = NULL;
303     char* operation = NULL;
304     char* event_value = NULL;
305     char* uri = NULL;
306     const char *event_uri = "event://tizen.system.event.battery_charger_status";
307     int res;
308
309     if (!check_rw_mount())
310         service_app_exit();
311
312     // operation
313     int ret = app_control_get_operation(app_control, &operation);
314     if (ret != APP_CONTROL_ERROR_NONE) {
315         STLOGW("Failed to get operation. error : %d", ret);
316         return;
317     }
318
319     STLOGD("operation: %s", operation);
320
321     if (!operation) {
322         goto cleanup;
323     }
324
325     if (strcmp(operation, APP_CONTROL_OPERATION_LAUNCH_ON_EVENT) == 0)
326     {
327         ret = app_control_get_uri(app_control, &uri);
328         if (ret == APP_CONTROL_ERROR_NONE && uri)
329         {
330             if (strncmp(uri, event_uri, strlen(event_uri) + 1) == 0)
331             {
332                 ret = app_control_get_extra_data(app_control, "battery_charger_status", &event_value);
333                 if (ret == APP_CONTROL_ERROR_NONE && event_value)
334                 {
335                     if (string(event_value) == "connected")
336                     {
337                         STLOGI("The charger state is connected");
338                         process_auto_sync();
339                     }
340                     free(event_value);
341                 }
342             }
343             free(uri);
344         }
345     }
346     else if (strcmp(operation, APP_CONTROL_OPERATION_DEFAULT) == 0) {
347         res = app_control_get_extra_data(app_control, "request", &request);
348         STLOGD("get extra data result : %d, request : %s", res, request);
349         if (APP_CONTROL_ERROR_NONE == res && NULL != request) {
350             process_request(app_control, request);
351         }
352         else {
353             STLOGD("booting");
354             get_sticker_feature();
355         }
356     }
357
358 cleanup:
359     if (NULL != operation)
360         free(operation);
361
362     if (NULL != request)
363         free(request);
364 }
365
366 static void app_terminate(void *data)
367 {
368     /* Release all resources. */
369     STLOGD("");
370     destroy_sticker_provider_handle();
371     deinitialize_sap();
372 }
373
374 int main(int argc, char *argv[])
375 {
376     int ret = 0;
377
378     service_app_lifecycle_callback_s event_callback;
379     memset(&event_callback, 0x0, sizeof(service_app_lifecycle_callback_s));
380
381     event_callback.create = (service_app_create_cb)app_create;
382     event_callback.terminate = (service_app_terminate_cb)app_terminate;
383     event_callback.app_control = (service_app_control_cb)app_control;
384
385     ret = service_app_main(argc, argv, &event_callback, NULL);
386     if (ret != APP_ERROR_NONE) {
387         STLOGE("app_main() is failed. err = %d", ret);
388     }
389     return ret;
390 }