Tizen 2.0 Release
[framework/system/oma-dm-agent.git] / src / agent / common / util / util.c
1 /*
2  * oma-dm-agent
3  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
4  *
5  * Licensed under the Apache License, Version 2.0 (the License);
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 /*lib*/
19 #include <aul.h>
20 #include <unistd.h>
21 #include <appsvc.h>
22 #include <glib.h>
23
24 /*sync-agent*/
25 #include <sync_agent.h>
26
27 /*dm-agent*/
28 #include "common/dm_common.h"
29 #include "common/util/util.h"
30 #include "ipc_agent.h"
31 #include "ipc_common.h"
32
33 #ifndef OMADM_AGENT_LOG
34 #undef LOG_TAG
35 #define LOG_TAG "OMA_DM_UTIL"
36 #endif
37
38 static void _update_csc();
39
40 void str_free(char **str)
41 {
42         _EXTERN_FUNC_ENTER;
43
44         if (*str == NULL) {
45                 _EXTERN_FUNC_EXIT;
46                 return;
47         } else {
48                 free(*str);
49                 *str = NULL;
50         }
51
52         _EXTERN_FUNC_EXIT;
53 }
54
55 int chartoint(char *data, int *value)
56 {
57         _EXTERN_FUNC_ENTER;
58
59         retvm_if(data == NULL, 0, "data is NULL!!");
60
61         if (data == NULL) {
62                 _EXTERN_FUNC_EXIT;
63                 return 0;
64         } else {
65                 (*value) = atoi(data);
66                 _EXTERN_FUNC_EXIT;
67                 return 1;
68         }
69
70         _EXTERN_FUNC_EXIT;
71         return 0;
72 }
73
74 int launch_oma_dm_fumo_ui()
75 {
76         _EXTERN_FUNC_ENTER;
77
78         int result = 0;
79         _DEBUG_INFO("\n---------------------------------------------------: %d\n", result);
80         _DEBUG_INFO("\n is launch oma fumo dm ui : %d ?\n", result);
81         result = aul_app_is_running(OMA_DM_FUMO_UI_PKG);
82         _DEBUG_INFO("\n Is launch oma fumo dm ui , result : %d\n", result);
83         _DEBUG_INFO("\n---------------------------------------------------: %d \n", result);
84
85         if (result == 0) {
86                 bundle *pBundle = NULL;
87                 pBundle = bundle_create();
88                 if (pBundle == NULL) {
89                         _DEBUG_INFO("bundle_create fail\n");
90                         _EXTERN_FUNC_EXIT;
91                         return 0;
92                 }
93                 result = bundle_add(pBundle, "FOTA_UI", "3");
94                 if (result == -1) {
95                         bundle_free(pBundle);
96                         _DEBUG_INFO("bundle_add fail\n");
97                         _EXTERN_FUNC_EXIT;
98                         return 0;
99                 }
100
101                 result = aul_launch_app(OMA_DM_FUMO_UI_PKG, pBundle);
102                 _DEBUG_INFO("result : %d\n", result);
103                 if (result == -1) {
104                         bundle_free(pBundle);
105                         _EXTERN_FUNC_EXIT;
106                         return 0;
107                 }
108                 bundle_free(pBundle);
109                 sleep(2);
110         } else {
111                 _DEBUG_INFO("---------------------------------------------------------------------------------------------------------------------------------");
112                 _DEBUG_INFO("existed oma dm ui\n");
113                 _DEBUG_INFO("---------------------------------------------------------------------------------------------------------------------------------");
114         }
115         _EXTERN_FUNC_EXIT;
116         return 1;
117 }
118
119 int launch_om_dm_fumo_noti_ui(int noti_data, char *session_id, char *server_id)
120 {
121         _EXTERN_FUNC_ENTER;
122
123         int result = 0;
124         _DEBUG_INFO("\n---------------------------------------------------: %d\n", result);
125         _DEBUG_INFO("\n is launch oma noti dm ui ? : %d\n", result);
126         result = aul_app_is_running(OMA_DM_FUMO_NOTI_UI_PKG);
127         _DEBUG_INFO("\n Is launch oma noti dm ui , result : %d\n", result);
128         _DEBUG_INFO("\n---------------------------------------------------: %d\n", result);
129
130         if (result == 0) {
131                 char *noti_type = NULL;
132                 noti_type = g_strdup_printf("%d", noti_data);
133
134                 bundle *pBundle = NULL;
135                 pBundle = bundle_create();
136                 if (pBundle == NULL) {
137                         _DEBUG_INFO("bundle_create fail\n");
138                         _EXTERN_FUNC_EXIT;
139                         return 0;
140                 }
141
142                 result = bundle_add(pBundle, "FOTA_NOTI", noti_type);
143                 result = bundle_add(pBundle, "FOTA_SESSIONID", session_id);
144                 result = bundle_add(pBundle, "FOTA_SERVERID", server_id);
145
146                 if (session_id == NULL) {
147                         /*if engine stoped by battery off or other exceptions */
148                         result = bundle_add(pBundle, "FOTA_SESSIONID", "1111");
149                 }
150                 if (server_id == NULL) {
151                         result = bundle_add(pBundle, "FOTA_SERVERID", "1111");
152                 }
153                 if (result == -1) {
154                         _DEBUG_INFO("bundle_add fail\n");
155                         bundle_free(pBundle);
156                         _EXTERN_FUNC_EXIT;
157                         return 0;
158                 }
159
160                 result = aul_launch_app(OMA_DM_FUMO_NOTI_UI_PKG, pBundle);
161                 _DEBUG_INFO("result : %d\n", result);
162
163                 if (result == -1) {
164                         bundle_free(pBundle);
165                         _EXTERN_FUNC_EXIT;
166                         return 0;
167                 }
168
169                 str_free(&noti_type);
170                 bundle_free(pBundle);
171                 sleep(2);
172         } else {
173                 _DEBUG_INFO("---------------------------------------------------------------------------------------------------------------------------------");
174                 _DEBUG_INFO("existed oma dm noti ui\n");
175                 _DEBUG_INFO("---------------------------------------------------------------------------------------------------------------------------------");
176         }
177         _EXTERN_FUNC_EXIT;
178         return 1;
179 }
180
181 int launch_om_dm_fumo_alert_ui()
182 {
183         _EXTERN_FUNC_ENTER;
184
185         int ret = 0;
186         _DEBUG_INFO("OMA_DM_ALERT_UI_PKG : %s", OMA_DM_ALERT_UI_PKG);
187         ret = aul_app_is_running(OMA_DM_ALERT_UI_PKG);
188         _DEBUG_INFO("aul_app_is_running(%s) [%d] ", OMA_DM_ALERT_UI_PKG, ret);
189         if (ret == 0) {
190                 bundle *b = bundle_create();
191                 //appsvc_set_operation(b, APPSVC_OPERATION_DEFAULT);
192                 //appsvc_set_pkgname(b, OMA_DM_ALERT_UI_PKG);
193
194                 //ret = appsvc_run_service(b, 0, NULL, NULL);
195                 ret = aul_launch_app(OMA_DM_ALERT_UI_PKG, b);
196                 _DEBUG_INFO("result : %d\n", ret);
197
198                 _DEBUG_INFO("oma dm alert ui run : %d\n", ret);
199                 bundle_free(b);
200                 if (ret >= 0) {
201                         sleep(2);
202                         _EXTERN_FUNC_EXIT;
203                         return 1;
204                 } else {
205                         _EXTERN_FUNC_EXIT;
206                         return 0;
207                 }
208         } else {
209                 return 1;
210         }
211         _EXTERN_FUNC_EXIT;
212         return 0;
213 }
214
215 int launch_oma_dm_cp_ui(int sec_type, int ext_id)
216 {
217         _EXTERN_FUNC_ENTER;
218
219         _DEBUG_INFO("Start !!");
220         int result = 0;
221         _DEBUG_INFO("OMA_DM_CP_UI_PKG : %s", OMA_DM_CP_UI_PKG);
222         _DEBUG_INFO("\n---------------------------------------------------\n");
223         _DEBUG_INFO("\n is launch oma dm cp ui ?\n");
224         result = aul_app_is_running(OMA_DM_CP_UI_PKG);
225         _DEBUG_INFO("\n Is launch oma dm cp ui, result : %d\n", result);
226         _DEBUG_INFO("\n---------------------------------------------------\n");
227
228         if (result == 0) {
229                 char *pin_type = NULL;
230                 pin_type = g_strdup_printf("%d", sec_type);
231                 char *extId = NULL;
232                 extId = g_strdup_printf("%d", ext_id);
233
234                 bundle *pBundle = NULL;
235                 pBundle = bundle_create();
236                 if (pBundle == NULL) {
237                         _DEBUG_INFO("bundle_create fail\n");
238                         _EXTERN_FUNC_EXIT;
239                         return 0;
240                 }
241
242                 result = bundle_add(pBundle, "PIN_TYPE", pin_type);
243                 if (result == -1) {
244                         _DEBUG_INFO("bundle_add fail\n");
245                         bundle_free(pBundle);
246                         _EXTERN_FUNC_EXIT;
247                         return 0;
248                 }
249
250                 result = bundle_add(pBundle, "EXT_ID", extId);
251                 if (result == -1) {
252                         _DEBUG_INFO("bundle_add fail\n");
253                         bundle_free(pBundle);
254                         _EXTERN_FUNC_EXIT;
255                         return 0;
256                 }
257
258                 result = aul_launch_app(OMA_DM_CP_UI_PKG, pBundle);
259                 _DEBUG_INFO("result : %d\n", result);
260
261                 if (result == -1) {
262                         _EXTERN_FUNC_EXIT;
263                         bundle_free(pBundle);
264                         return 0;
265                 }
266
267                 str_free(&pin_type);
268                 bundle_free(pBundle);
269                 sleep(2);
270         } else {
271                 _DEBUG_INFO("---------------------------------------------------------------------------------------------------------------------------------");
272                 _DEBUG_INFO("existed oma dm cp ui\n");
273                 _DEBUG_INFO("---------------------------------------------------------------------------------------------------------------------------------");
274         }
275         _EXTERN_FUNC_EXIT;
276         return 1;
277 }
278
279 void terminate_oma_dm_ui(int result_status, ENGINE_ID service_engine_id)
280 {
281         _EXTERN_FUNC_ENTER;
282
283         if (result_status == DM_ERR_REMINDER_INTERVAL || result_status == DM_DOWNLOAD_POPUP || result_status == DM_INSTALL_POPUP || result_status == DM_RESUME_IDLE) {
284                 _DEBUG_INFO("result status : %d", result_status);
285                 return;
286         }
287
288         _DEBUG_INFO("start");
289
290         DM_ERROR ret = DM_OK;
291         int isfumo_engine = 0;
292         engine_status *status = NULL;
293         int result = 0;
294         bool cancel_flag = 0;
295
296         cancel_flag = sync_agent_check_cancel_flag();
297         if (cancel_flag) {
298                 _EXTERN_FUNC_EXIT;
299                 return;
300         }
301         isfumo_engine = IsExist_Engine_id(FUMO_SERVICE_ENGINE);
302
303         if (isfumo_engine == 1) {
304                 if (service_engine_id == FUMO_SERVICE_ENGINE) {
305                         _DEBUG_INFO("existed engine id in db");
306                         ret = Get_Engine_Status(FUMO_SERVICE_ENGINE, &status);
307                         _DEBUG_INFO("get fumo service engine : %d", ret);
308
309                         if (status != NULL && status->ui_mode != NULL) {
310                                 result = aul_app_is_running(OMA_DM_FUMO_NOTI_UI_PKG);
311                                 if (result == 1) {
312                                         terminate_ui_status(status->ui_noti_type, status->ui_mode, result_status);
313                                 }
314                                 result = aul_app_is_running(OMA_DM_FUMO_UI_PKG);
315                                 if (result == 1) {
316                                         terminate_ui_status(status->ui_noti_type, status->ui_mode, result_status);
317                                 }
318                         } else {
319                                 _DEBUG_INFO("fumo service ui mode not existed");
320                         }
321
322                         if (status != NULL)
323                                 Free_Memory_Engine_Status(&status, 1);
324
325                         ret = Delete_Engine_Status(FUMO_SERVICE_ENGINE);
326                         _DEBUG_INFO("delete engine status  : %d", ret);
327                 } else {
328                         _DEBUG_INFO("fumo service not existed");
329                 }
330         } else {
331                 _DEBUG_INFO("fumo service not existed");
332         }
333         _EXTERN_FUNC_EXIT;
334
335 }
336
337 void terminate_ui_status(int noti_type, char *ui_mode, int result_status)
338 {
339         _EXTERN_FUNC_ENTER;
340
341         int noti_res = 0;
342
343         _DEBUG_INFO("noti type : %d", noti_type);
344         /*noti_res = noti_engine_fail(noti_type, fail_type); */
345
346         switch (noti_type) {
347         case NOTI_ENGINE_START:
348                 _DEBUG_INFO("connect to sever ui ");
349                 if (result_status != DM_OK) {
350                         if (result_status == DM_ERR_UNAUTHORIZED || result_status == DM_ERR_AUTHENTICATION_REQUIRED) {
351                                 noti_res = noti_engine_fail(noti_type, CONNECTION_STATUS_FAILURE_AUTHENTICATION);
352                         } else {
353                                 noti_res = noti_engine_fail(noti_type, CONNECTION_STATUS_FAILURE);
354                         }
355                 } else {
356                         noti_res = noti_engine_fail(noti_type, CONNECTION_STATUS_UPTODATA);
357                 }
358                 _DEBUG_INFO("connect to sever ui : %d", noti_res);
359                 break;
360         case NOTI_UNKNOWN:
361         case NOTI_DOWNLOAD:
362         case NOTI_DOWNLOAD_INFO:
363         case NOTI_INSTALL:
364                 //case NOTI_WIFI_ONLY_DOWNLOAD_FAIL:
365                 //case NOTI_MEMORY_FULL:
366                 noti_res = noti_engine_fail(noti_type, 0);
367                 break;
368         case NOTI_LOW_BATTERY:
369         case NOTI_ALERT_DISPLAY:
370         case NOTI_ALERT_CONFIRMATION:
371         case NOTI_ALERT_INPUTTEXT:
372         case NOTI_ALERT_SINGLE_CHOICE:
373         case NOTI_ALERT_MULTIPLE_CHOICE:
374                 break;
375         default:
376                 break;
377         }
378
379         _DEBUG_INFO("cancel ui : %d", noti_res);
380         _EXTERN_FUNC_EXIT;
381
382 }
383
384 int check_csc()
385 {
386         _EXTERN_FUNC_ENTER;
387
388         int exist = 0;
389
390         /*FIXME(temporary do not process csc for ui reason)
391            check csc_check file is existed
392            if false do csc update
393            if true check csc xml file has been changed */
394
395         /*exist = sync_agent_is_existing_fs(CSC_CHECK_PATH); */
396         /*temp value */
397         exist = 1;
398
399 /*for prevent*/
400         /*if(exist == 0 ) {
401            _update_csc();
402            } */
403
404         _update_csc();
405
406         _DEBUG_INFO("update = %d", exist);
407         _EXTERN_FUNC_EXIT;
408         return exist;
409 }
410
411 static void _update_csc()
412 {
413         _INNER_FUNC_ENTER;
414
415         //dmacc mo db update
416         /*sync_agent_write_whole_file(OMA_DS_CSC_CHECK_PATH, contents, strlen(contents), false); */
417
418         _INNER_FUNC_EXIT;
419 }
420
421 int get_wifi_state()
422 {
423         _EXTERN_FUNC_ENTER;
424
425         sync_agent_na_result_e ret = SYNC_AGENT_NA_SUCCESS;
426         sync_agent_na_network_connection_type_e con_type = SYNC_AGENT_NA_NETWORK_CONNECTION_TYPE_UNKNOWN;
427         sync_agent_na_network_connection_state_e con_state = SYNC_AGENT_NA_NETWORK_CONNECTION_STATE_NONE;
428
429         ret = sync_agent_get_connection_type(&con_type);
430         if (ret != SYNC_AGENT_NA_SUCCESS) {
431                 _DEBUG_INFO("get connection type");
432                 ret = SYNC_AGENT_NA_DOWNLOAD_DATA_FAIL;
433                 goto return_part;
434         }
435         _DEBUG_INFO("get connection type : %d", con_type);
436
437         ret = sync_agent_get_connection_state(&con_state);
438         _DEBUG_INFO("get connection state : %d", con_state);
439         if (ret != SYNC_AGENT_NA_SUCCESS) {
440                 _DEBUG_INFO("get connection state");
441                 ret = SYNC_AGENT_NA_NETWORK_UNAVAILABLE;
442                 goto return_part;
443         }
444
445         if (con_type != SYNC_AGENT_NA_NETWORK_CONNECTION_TYPE_WIFI || (con_type == SYNC_AGENT_NA_NETWORK_CONNECTION_TYPE_WIFI && con_state != SYNC_AGENT_NA_NETWORK_CONNECTION_STATE_WIFI_CONNECTED)) {
446                 _DEBUG_INFO("wifi only on mode download fail");
447                 ret = SYNC_AGENT_NA_NETWORK_UNAVAILABLE;
448                 goto return_part;
449         } else {
450                 _DEBUG_INFO("wifi on mode download");
451         }
452
453         _EXTERN_FUNC_EXIT;
454         return ret;
455
456  return_part:
457         _DEBUG_INFO("error end %d : ", ret);
458         _EXTERN_FUNC_EXIT;
459
460         return ret;
461 }
462
463 DM_ERROR nonce_decode(char *nextNonce, unsigned char **nextNonceDecode, unsigned int *out_len)
464 {
465         _EXTERN_FUNC_ENTER;
466
467         unsigned char *tempnextNonceDecode = NULL;
468         tempnextNonceDecode = g_base64_decode(nextNonce, (unsigned int *)(out_len));
469
470         if (tempnextNonceDecode == NULL && *out_len != 0) {
471                 _DEBUG_INFO("tempnextNonceDecode = NULL , out len : %d", *out_len);
472                 int i = 0;
473                 char *zero = "0";
474                 *nextNonceDecode = (unsigned char *)calloc(1, (*out_len) + 1);
475                 for (i = 0; i < (*out_len); ++i) {
476                         g_strlcat((char *)(*nextNonceDecode), zero, *out_len);
477                 }
478
479         } else {
480
481                 _DEBUG_INFO("tempnextNonceDecode = %s, out_len = %d", tempnextNonceDecode, *out_len);
482                 /**nextNonceDecode = (char *)strdup(tempnextNonceDecode);
483                 str_free((char **)(&tempnextNonceDecode));*/
484                 *nextNonceDecode = tempnextNonceDecode;
485         }
486
487         _EXTERN_FUNC_EXIT;
488         return DM_OK;
489
490 }