Add oobe mode for initializing
[platform/core/uifw/capi-ui-sticker.git] / receiver / src / main.cpp
1 #include <app_common.h>
2 #include <service_app.h>
3 #include <stdlib.h>
4 #include <app_alarm.h>
5 #include <device/battery.h>
6
7 #include "main.h"
8 #include "ft.h"
9 #include "log.h"
10 #include "sync_alarm.h"
11
12 #define MINIMUM_BATTERY 15
13
14 static bool app_create(void *data)
15 {
16     /* Hook to take necessary actions before main event loop starts
17        Initialize UI resources and application's data
18        If this function returns true, the main loop of application starts
19        If this function returns false, the application is terminated */
20
21     initialize_sap();
22
23     return true;
24 }
25
26 static void app_control(app_control_h app_control, void *data)
27 {
28     /* Handle the launch request. */
29     char* request = NULL;
30     char* mode = NULL;
31     char* category = NULL;
32     char* type = NULL;
33     char* operation = NULL;
34     char* alarm_data = NULL;
35
36     int res;
37
38     // operation
39     int ret = app_control_get_operation(app_control, &operation);
40     if (ret == APP_CONTROL_ERROR_NONE) {
41         LOGD("operation: %s", operation);
42
43         if (operation && (strncmp(operation, APP_CONTROL_OPERATION_SYNC_ALARM, strlen(APP_CONTROL_OPERATION_SYNC_ALARM)) == 0)) {
44             ret = app_control_get_extra_data(app_control, APP_CONTROL_DATA_ALARM_ID, &alarm_data);
45             if (ret != APP_CONTROL_ERROR_NONE) {
46                 dlog_print(DLOG_ERROR, LOG_TAG, "Function app_control_get_extra_data() failed.");
47                 goto cleanup;
48             }
49
50             LOGD("alarm data : %s", alarm_data);
51
52             int battery_percentage = 0;
53             int ret = device_battery_get_percent(&battery_percentage);
54             if (ret == DEVICE_ERROR_NONE) {
55                 LOGD("battery percent : %d", battery_percentage);
56                 if (battery_percentage >= MINIMUM_BATTERY) {
57                     request_sticker_data("auto", "arsticker", "input");
58                     request_sticker_data("auto", "bitmoji", "input");
59                 }
60                 else {
61                     LOGD("No sync request due to insufficient battery");
62                 }
63             }
64             else {
65                LOGW("Failed to get battery percent. error : %d", ret);
66             }
67
68             goto cleanup;
69         }
70     }
71     else {
72         LOGW("Failed to get operation. error : %d", ret);
73     }
74
75     // sync request
76     res = app_control_get_extra_data(app_control, "request", &request);
77     if (APP_CONTROL_ERROR_NONE == res && NULL != request) {
78         if (strcmp(request, "sync") == 0) {
79             bool param_error = false;
80             if (app_control_get_extra_data(app_control, "mode", &mode) != APP_CONTROL_ERROR_NONE) {
81                 LOGE("No given mode");
82                 param_error = true;
83             }
84
85             if (app_control_get_extra_data(app_control, "category", &category) != APP_CONTROL_ERROR_NONE) {
86                 LOGE("No given category");
87                 param_error = true;
88             }
89
90             if (app_control_get_extra_data(app_control, "type", &type) != APP_CONTROL_ERROR_NONE) {
91                 LOGE("No given type");
92                 param_error = true;
93             }
94
95             LOGI("[sync request] mode : %s, category : %s, type : %s", mode, category, type);
96             if (param_error)
97                 goto cleanup;
98
99             if (mode && category && type)
100                 request_sticker_data(mode, category, type);
101         }
102         else if (strcmp(request, "oobe") == 0) {
103             LOGI("[OOBE] register sync alarm");
104             sync_alarm_register(APP_CONTROL_OPERATION_SYNC_ALARM, SYNC_ALARM_DELAY, SYNC_ALARM_INTERVAL);
105             service_app_exit();
106         }
107         else
108         {
109             LOGW("Unknown command : %s", request);
110         }
111     }
112
113 cleanup:
114     if (NULL != operation)
115         free(operation);
116
117     if (NULL != request)
118         free(request);
119
120     if (NULL != mode)
121         free(mode);
122
123     if (NULL != category)
124         free(category);
125
126     if (NULL != type)
127         free(type);
128 }
129
130 static void app_terminate(void *data)
131 {
132     /* Release all resources. */
133     deinitialize_sap();
134 }
135
136 int main(int argc, char *argv[])
137 {
138     int ret = 0;
139
140     service_app_lifecycle_callback_s event_callback;
141     memset(&event_callback, 0x0, sizeof(service_app_lifecycle_callback_s));
142
143     event_callback.create = (service_app_create_cb)app_create;
144     event_callback.terminate = (service_app_terminate_cb)app_terminate;
145     event_callback.app_control = (service_app_control_cb)app_control;
146
147     ret = service_app_main(argc, argv, &event_callback, NULL);
148     if (ret != APP_ERROR_NONE) {
149         LOGE("app_main() is failed. err = %d", ret);
150     }
151     return ret;
152 }