Wake up and send sync request when charging
[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
33 using namespace std;
34
35 static bool app_create(void *data)
36 {
37     /* Hook to take necessary actions before main event loop starts
38        Initialize UI resources and application's data
39        If this function returns true, the main loop of application starts
40        If this function returns false, the application is terminated */
41
42     LOGD("");
43
44     return true;
45 }
46
47 static bool check_battery_condition()
48 {
49     int battery_percentage = 0;
50     int ret;
51
52     // check battery percentage
53     ret = device_battery_get_percent(&battery_percentage);
54     if (ret != DEVICE_ERROR_NONE)
55     {
56         LOGW("No sync. Failed to get battery percent. error : %d", ret);
57         return false;
58     }
59
60     LOGI("battery percent : %d", battery_percentage);
61     if (battery_percentage >= MINIMUM_BATTERY)
62         return true;
63     else
64     {
65         LOGI("No sync due to insufficient battery");
66         return false;
67     }
68 }
69
70 static bool check_sync_time_condition()
71 {
72     double last_sync_time;
73     int feature_flag = 0;
74     bool result = false;
75
76 #ifdef VCONFKEY_STICKER_SUPPORTED_FEATURE
77     // Check whether oobe has been done
78     if (vconf_get_int(VCONFKEY_STICKER_SUPPORTED_FEATURE, &feature_flag) == 0 && feature_flag > 0)
79     {
80         if (preference_get_double(LAST_SYNC_TIME, &last_sync_time) != PREFERENCE_ERROR_NONE)
81         {
82             LOGD("Can't get last sync time.");
83             return true;
84         }
85
86         // compare time
87         LOGD("current time : %f, last_sync_time : %f", ecore_time_get(), last_sync_time);
88         if (ecore_time_get() - last_sync_time > SYNC_INTERVAL)
89             result = true;
90         else
91             result = false;
92     }
93     else
94     {
95         result = false;
96     }
97 #endif /* VCONFKEY_STICKER_SUPPORTED_FEATURE */
98
99     return result;
100 }
101
102 static void app_control(app_control_h app_control, void *data)
103 {
104     /* Handle the launch request. */
105     char* request = NULL;
106     char* mode = NULL;
107     char* type = NULL;
108     char* operation = NULL;
109     char* event_value = NULL;
110     char* uri = NULL;
111     const char *event_uri = "event://tizen.system.event.battery_charger_status";
112     int res;
113
114     // operation
115     int ret = app_control_get_operation(app_control, &operation);
116     if (ret == APP_CONTROL_ERROR_NONE) {
117         LOGD("operation: %s", operation);
118
119         if (!operation) {
120             goto cleanup;
121         }
122
123         if (strcmp(operation, APP_CONTROL_OPERATION_LAUNCH_ON_EVENT) == 0)
124         {
125             ret = app_control_get_uri(app_control, &uri);
126             if (ret == APP_CONTROL_ERROR_NONE && uri)
127             {
128                 if (strncmp(uri, event_uri, strlen(event_uri) + 1) == 0)
129                 {
130                     ret = app_control_get_extra_data(app_control, "battery_charger_status", &event_value);
131                     if (ret == APP_CONTROL_ERROR_NONE && event_value)
132                     {
133                         if (string(event_value) == "connected")
134                         {
135                             if (!is_init_sap()) {
136                                 if (check_sync_time_condition()) {
137                                     if (check_battery_condition()) {
138                                         initialize_sap();
139                                         request_all_sticker_data("auto", "input");
140                                     }
141                                 }
142                                 else {
143                                     if (!get_job_progress())
144                                     {
145                                         LOGD("exit");
146                                         service_app_exit();
147                                     }
148                                 }
149                             }
150                             else {
151                                 LOGD("continue doing current job");
152                             }
153                         }
154                         free(event_value);
155                     }
156                 }
157                 free(uri);
158             }
159         }
160     }
161     else {
162         LOGW("Failed to get operation. error : %d", ret);
163     }
164
165     // sync request
166     res = app_control_get_extra_data(app_control, "request", &request);
167     if (APP_CONTROL_ERROR_NONE == res && NULL != request) {
168         if (strcmp(request, "sync") == 0) {
169             bool param_error = false;
170             if (app_control_get_extra_data(app_control, "mode", &mode) != APP_CONTROL_ERROR_NONE) {
171                 LOGE("No given mode");
172                 param_error = true;
173             }
174
175             if (app_control_get_extra_data(app_control, "type", &type) != APP_CONTROL_ERROR_NONE) {
176                 LOGE("No given type");
177                 param_error = true;
178             }
179
180             LOGI("[sync request] mode : %s, type : %s", mode, type);
181             if (param_error)
182                 goto cleanup;
183
184             if (mode && type) {
185                 if (!is_init_sap()) {
186                     initialize_sap();
187                     request_all_sticker_data(mode, type);
188                 }
189             }
190         }
191         else if (strcmp(request, "oobe") == 0) {
192             initialize_sap();
193             request_sticker_feature();
194         }
195         else
196         {
197             LOGW("Unknown command : %s", request);
198             if (!is_init_sap())
199             {
200                 service_app_exit();
201             }
202         }
203     }
204
205 cleanup:
206     if (NULL != operation)
207         free(operation);
208
209     if (NULL != request)
210         free(request);
211
212     if (NULL != mode)
213         free(mode);
214
215     if (NULL != type)
216         free(type);
217 }
218
219 static void app_terminate(void *data)
220 {
221     /* Release all resources. */
222     LOGD("");
223     deinitialize_sap();
224 }
225
226 int main(int argc, char *argv[])
227 {
228     int ret = 0;
229
230     service_app_lifecycle_callback_s event_callback;
231     memset(&event_callback, 0x0, sizeof(service_app_lifecycle_callback_s));
232
233     event_callback.create = (service_app_create_cb)app_create;
234     event_callback.terminate = (service_app_terminate_cb)app_terminate;
235     event_callback.app_control = (service_app_control_cb)app_control;
236
237     ret = service_app_main(argc, argv, &event_callback, NULL);
238     if (ret != APP_ERROR_NONE) {
239         LOGE("app_main() is failed. err = %d", ret);
240     }
241     return ret;
242 }