Tizen 2.0 Release
[framework/system/oma-dm-agent.git] / src / plugins / dm-private / slp-device-dm / src / plugin_interface_devexe.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 #include <string.h>
19
20 /*sync-agent*/
21 #include <sync_agent.h>
22
23 #include "plugin_slp_device_dm.h"
24 #include "plugin_devexe_error.h"
25 #include "plugin_slp_device_ctrl.h"
26
27 #include "pmapi.h"
28
29 #ifndef EXPORT_API
30 #define EXPORT_API __attribute__ ((visibility("default")))
31 #endif
32
33 #ifndef OMADM_AGENT_LOG
34 #undef LOG_TAG
35 #define LOG_TAG "PLUGIN_DEVICE_SLP_DM"
36 #endif
37
38 #define DELTA_FILE "delta_hash.txt"
39
40 static sync_agent_execute_dev_cb func_execute_dev_function_common = 0;
41
42 static int _execute_dev_fota_flag(int memory_type);
43 static int _execute_dev_fota_update_result(int *plugin_err);
44 static int _execute_dev_reboot();
45 static int _execute_dev_fully_lock();
46 static int _execute_dev_partially_lock();
47 static int _execute_dev_unlock();
48 static int _execute_dev_factory_reset();
49 static int _execute_dev_wipe();
50 static sync_agent_dev_return_e __convert_service_err_to_common_error(int service_err);
51 /*static int __find_file_list(const char *folder_path, GSList **file_list);*/
52 static int ___find_file(const char *folder_path, const char **file_path);
53 static int ___check_filename_md5(char *folder_path, char *file_buffer);
54 static int ___check_fimware_delta_validation(char *save_folder);
55 static int __control_fimware_delta_file(int memory_type);
56
57 static int ___get_download_save_folder(int memory_type, char **download_folder, char **save_folder);
58 int _execute_dev_function_common(char *function_name, int args_cnt, va_list ap);
59
60 EXPORT_API int sync_agent_plugin_execute_dev_function(char *function_name, int args_cnt, va_list ap)
61 {
62         _EXTERN_FUNC_ENTER;
63
64         retvm_if((function_name) == NULL, 0, "function_name is NULL!!");
65
66         if (!strncmp(function_name, "pm_lock", strlen("pm_lock")) || !strncmp(function_name, "pm_unlock", strlen("pm_unlock")) || !strncmp(function_name, "pm_change", strlen("pm_change"))) {
67
68                 _DEBUG_INFO(" Received %s request !!!", function_name);
69                 _EXTERN_FUNC_EXIT;
70                 return _execute_dev_function_common(function_name, args_cnt, ap);
71
72         } else if (!strncmp(function_name, "fota_flag", strlen("fota_flag"))) {
73
74                 int memory_type = va_arg(ap, int);
75
76                 _DEBUG_INFO(" Received %s request !!!", function_name);
77                 _EXTERN_FUNC_EXIT;
78                 return _execute_dev_fota_flag(memory_type);
79
80         } else if (!strncmp(function_name, "fota_result", strlen("fota_result"))) {
81
82                 int *plugin_err = va_arg(ap, int *);
83
84                 _DEBUG_INFO(" Received %s request !!!", function_name);
85                 _EXTERN_FUNC_EXIT;
86                 return _execute_dev_fota_update_result(plugin_err);
87
88         } else if (!strncmp(function_name, "reboot", strlen("reboot"))) {
89
90                 _DEBUG_INFO(" Received %s request !!!", function_name);
91                 _EXTERN_FUNC_EXIT;
92                 return _execute_dev_reboot();
93
94         } else if (!strncmp(function_name, "full_lock", strlen("full_lock"))) {
95
96                 _DEBUG_INFO(" Received %s request !!!", function_name);
97                 return _execute_dev_fully_lock();
98
99         } else if (!strncmp(function_name, "partial_lock", strlen("partial_lock"))) {
100
101                 _DEBUG_INFO(" Received %s request !!!", function_name);
102                 _EXTERN_FUNC_EXIT;
103                 return _execute_dev_partially_lock();
104
105         } else if (!strncmp(function_name, "unlock", strlen("unlock"))) {
106
107                 _DEBUG_INFO(" Received %s request !!!", function_name);
108                 _EXTERN_FUNC_EXIT;
109                 return _execute_dev_unlock();
110
111         } else if (!strncmp(function_name, "factory_reset", strlen("factory_reset"))) {
112
113                 _DEBUG_INFO(" Received %s request !!!", function_name);
114                 _EXTERN_FUNC_EXIT;
115                 return _execute_dev_factory_reset();
116
117         } else if (!strncmp(function_name, "wipe", strlen("wipe"))) {
118
119                 _DEBUG_INFO(" Received %s request !!!", function_name);
120                 _EXTERN_FUNC_EXIT;
121                 return _execute_dev_wipe();
122
123         } else {
124                 _DEBUG_INFO("Device function requested not defined in plug-in !!!");
125                 goto error_part;
126         }
127
128  error_part:
129
130         _EXTERN_FUNC_EXIT;
131         return 0;
132 }
133
134 EXPORT_API void sync_agent_plugin_set_common_execute_dev_function(sync_agent_execute_dev_cb func)
135 {
136         _EXTERN_FUNC_ENTER;
137
138         func_execute_dev_function_common = func;
139
140         _EXTERN_FUNC_EXIT;
141 }
142
143 static int _execute_dev_fota_flag(int memory_type)
144 {
145         _INNER_FUNC_ENTER;
146
147         /*slp_device_dm_exe_e err = EXE_SUCCESS; */
148
149         int service_ret = 1;
150         service_ret = __control_fimware_delta_file(memory_type);
151         if (service_ret != 1) {
152                 _DEBUG_TRACE("__control_fimware_delta_file() Fail");
153                 goto error;
154         }
155         _DEBUG_TRACE("control firmware delta file : %d \n", service_ret);
156
157         service_ret = slp_device_ctrl_set_fota_flag();
158         if (service_ret != 0) {
159                 _DEBUG_TRACE("slp_device_ctrl_set_FOTA_flag() Fail");
160         }
161
162  error:
163         _INNER_FUNC_EXIT;
164         return __convert_service_err_to_common_error(service_ret);
165 }
166
167 static int _execute_dev_fota_update_result(int *plugin_err)
168 {
169         _INNER_FUNC_ENTER;
170
171         *plugin_err = slp_device_ctrl_get_fota_update_result();
172
173         _INNER_FUNC_EXIT;
174         return __convert_service_err_to_common_error(1);
175 }
176
177 static int _execute_dev_reboot()
178 {
179         _INNER_FUNC_ENTER;
180
181         int service_ret = slp_device_ctrl_reboot();
182
183         _INNER_FUNC_EXIT;
184         return __convert_service_err_to_common_error(service_ret);
185 }
186
187 static int _execute_dev_fully_lock()
188 {
189         _INNER_FUNC_ENTER;
190
191         int service_ret = slp_device_ctrl_fully_lock();
192
193         _INNER_FUNC_EXIT;
194         return __convert_service_err_to_common_error(service_ret);
195 }
196
197 static int _execute_dev_partially_lock()
198 {
199         _INNER_FUNC_ENTER;
200
201         int service_ret = slp_device_ctrl_partially_lock();
202
203         _INNER_FUNC_EXIT;
204         return __convert_service_err_to_common_error(service_ret);
205 }
206
207 static int _execute_dev_unlock()
208 {
209         _INNER_FUNC_ENTER;
210
211         int service_ret = slp_device_ctrl_unlock();
212
213         _INNER_FUNC_EXIT;
214         return __convert_service_err_to_common_error(service_ret);
215 }
216
217 static int _execute_dev_factory_reset()
218 {
219         _INNER_FUNC_ENTER;
220
221         int service_ret = slp_device_ctrl_factory_reset();
222
223         _INNER_FUNC_EXIT;
224         return __convert_service_err_to_common_error(service_ret);
225 }
226
227 static int _execute_dev_wipe()
228 {
229         _INNER_FUNC_ENTER;
230
231         int service_ret = 1;
232
233         _INNER_FUNC_EXIT;
234         return __convert_service_err_to_common_error(service_ret);
235 }
236
237 /* static function*/
238 /*static int __find_file_list(const char *folder_path, GSList **file_list)
239 {
240         _DEBUG_VERBOSE(" start");
241         int ret = 1;
242
243         if (folder_path == NULL) {
244                 ret = -1;
245                 goto error;
246         }
247
248         _DEBUG_VERBOSE(" folder_path = %s", folder_path);
249
250         GSList *fileList = NULL;
251         ret = sync_agent_get_file_list(folder_path, &fileList);
252         if (ret != 1 || fileList == NULL) {
253                 ret = -1;
254                 goto error;
255         }
256
257         GSList *iter = NULL;
258         char *temp_file_info = NULL;
259         char *temp_file_name = NULL;
260         char *temp_file_path = NULL;
261
262         for (iter = fileList; iter != NULL; iter = g_slist_next(iter)) {
263                 temp_file_info = (char *)(iter->data);
264                 _DEBUG_VERBOSE(" file_name = %s", temp_file_info);
265
266                 temp_file_name = strtok(temp_file_info, ",");
267
268                 (*file_list) = g_slist_append((*file_list), temp_file_name);
269         }
270
271         _DEBUG_VERBOSE(" end");
272         return ret;
273 error:
274         _DEBUG_VERBOSE(" end error : %d", ret);
275         return ret;
276 }*/
277
278 static int ___find_file(const char *folder_path, const char **file_path)
279 {
280         _INNER_FUNC_ENTER;
281
282         retvm_if((folder_path) == NULL, -1, "folder_path is NULL!!");
283
284         int ret = 1;
285         GSList *fileList = NULL;
286         ret = sync_agent_get_file_list(folder_path, &fileList);
287         if (ret != 1 || fileList == NULL || g_slist_length(fileList) > 1) {
288                 ret = -1;
289                 goto error;
290         }
291
292         char *temp_file_info = NULL;
293         char *temp_file_name = NULL;
294
295         temp_file_info = (char *)(fileList->data);
296         _DEBUG_VERBOSE(" file_name = %s", temp_file_info);
297
298         const char *delimiter = ",";
299         /*temp_file_name = strtok(temp_file_info, ","); */
300         temp_file_name = strtok(temp_file_info, delimiter);
301         (*file_path) = g_strdup_printf("%s/%s", folder_path, temp_file_name);
302
303         _INNER_FUNC_EXIT;
304         return ret;
305  error:
306         _DEBUG_VERBOSE(" end error : %d", ret);
307         _INNER_FUNC_EXIT;
308         return ret;
309 }
310
311 static int ___check_filename_md5(char *folder_path, char *file_buffer)
312 {
313         _INNER_FUNC_ENTER;
314         int ret = 1;
315
316         retvm_if((folder_path) == NULL, -1, "folder_path is NULL!!");
317         retvm_if((file_buffer) == NULL, -1, "file_buffer is NULL!!");
318
319         _DEBUG_VERBOSE(" check_file_folder : %s", folder_path);
320         _DEBUG_VERBOSE(" check_file : %s", file_buffer);
321
322         bool isSame = false;
323         char *check_segment = NULL;
324         char *find_file_name = NULL;
325         unsigned char *find_md5_word = NULL;
326         char *file_path = NULL;
327
328         GSList *filename_md5 = NULL;
329         GSList *filename_md5_iter = NULL;
330
331         const char *delimiter = ":\t\n ";
332         /*check_segment = strtok(file_buffer, ":\t\n "); */
333         check_segment = strtok(file_buffer, delimiter);
334         _DEBUG_VERBOSE(" check_segment : %s", check_segment);
335         filename_md5 = g_slist_append(filename_md5, check_segment);
336
337         while (check_segment != NULL) {
338                 /*check_segment = strtok(NULL, ":\t\n "); */
339                 check_segment = strtok(NULL, delimiter);
340                 if (check_segment == NULL)
341                         break;
342                 filename_md5 = g_slist_append(filename_md5, check_segment);
343
344                 _DEBUG_VERBOSE(" check file : %s", check_segment);
345         }
346
347         for (filename_md5_iter = filename_md5; filename_md5_iter != NULL; filename_md5_iter = g_slist_next(filename_md5_iter)) {
348                 if ((filename_md5_iter->data) == NULL) {
349                         ret = -1;
350                         goto error;
351                 }
352
353                 find_file_name = (char *)(filename_md5_iter->data);
354                 filename_md5_iter = g_slist_next(filename_md5_iter);
355                 find_md5_word = (unsigned char *)(filename_md5_iter->data);
356                 _DEBUG_VERBOSE(" find_file_name : %s, find_md5 : %s", find_file_name, find_md5_word);
357
358                 file_path = g_strdup_printf("%s/%s", folder_path, find_file_name);
359                 ret = sync_agent_is_existing_fs(file_path);
360                 if (ret != 1) {
361                         goto error;
362                 }
363
364                 ret = sync_agent_file_md5_validation(file_path, (char *)find_md5_word, &isSame);
365                 if (ret != 1 || (ret == 1 && isSame == false))
366                         goto error;
367         }
368
369         _INNER_FUNC_EXIT;
370         return ret;
371  error:
372         _DEBUG_VERBOSE(" end error : %d", ret);
373         _INNER_FUNC_EXIT;
374         return ret;
375 }
376
377 static int ___check_fimware_delta_validation(char *save_folder)
378 {
379         _INNER_FUNC_ENTER;
380
381         retvm_if((save_folder) == NULL, -1, "save_folder is NULL!!");
382
383         int ret = 0;
384         char *required_contents = NULL;
385         unsigned long required_length = 0;
386         char *file_path = NULL;
387         bool isFinal;
388
389         file_path = g_strdup_printf("%s/%s", save_folder, DELTA_FILE);
390         _DEBUG_VERBOSE(" file_path : %s", file_path);
391
392         ret = sync_agent_read_whole_file(file_path, &required_contents, &required_length);
393         if (ret != 1)
394                 goto error;
395         _DEBUG_VERBOSE(" file read is %d, required_length is %d ", ret, required_length);
396         _DEBUG_VERBOSE(" file read is %d, isFinal is %d ", ret, isFinal);
397         _DEBUG_VERBOSE(" file content is %s ", required_contents);
398
399         ret = ___check_filename_md5(save_folder, required_contents);
400         if (ret != 1)
401                 goto error;
402
403         ret = sync_agent_free_file(&required_contents);
404         if (ret != 1)
405                 goto error;
406
407         _INNER_FUNC_EXIT;
408         return ret;
409
410  error:
411
412         _DEBUG_VERBOSE(" end error : %d \n", ret);
413         _INNER_FUNC_EXIT;
414         return ret;
415 }
416
417 static int __control_fimware_delta_file(int memory_type)
418 {
419         _INNER_FUNC_ENTER;
420
421         int ret = 1;
422
423         char *download_folder = NULL;
424         char *save_folder = NULL;
425
426         ret = ___get_download_save_folder(memory_type, &download_folder, &save_folder);
427         if (ret != 1)
428                 goto error;
429
430         const char *file_path = NULL;
431         ret = ___find_file(download_folder, &file_path);
432         if (ret != 1)
433                 goto error;
434
435         /*file_path = g_strdup_printf("%s%s%s", download_folder, "/","0715_0720_FW_delta.tar"); */
436         sync_agent_util_compress_result_e result = sync_agent_uncompress(SYNC_AGENT_UTIL_COMPRESS_TYPE_TAR, file_path, save_folder);
437         if (result != SYNC_AGENT_UTIL_COMPRESS_SUCCESS) {
438                 _DEBUG_VERBOSE(" Failed to call sync_agent_uncompress()");
439                 ret = -1;
440                 goto error;
441         } else {
442                 _DEBUG_VERBOSE(" Success to call sync_agent_uncompress()");
443         }
444
445         ret = ___check_fimware_delta_validation(save_folder);
446         if (ret != 1)
447                 goto error;
448
449         int fs_error;
450         fs_error = sync_agent_empty_directory_contents(download_folder);
451
452         _INNER_FUNC_EXIT;
453         return ret;
454  error:
455         _DEBUG_VERBOSE(" end error : %d", ret);
456         _INNER_FUNC_EXIT;
457         return ret;
458 }
459
460 static sync_agent_dev_return_e __convert_service_err_to_common_error(int service_err)
461 {
462         _INNER_FUNC_ENTER;
463
464         slp_device_dm_exe_e ret = EXE_SUCCESS;
465
466         switch (service_err) {
467         case 1:
468                 ret = EXE_SUCCESS;
469                 break;
470         default:
471                 ret = EXE_DEV_OPER_NOT_PERFORMED;
472                 break;
473         }
474
475         _INNER_FUNC_EXIT;
476         return ret;
477 }
478
479 static int ___get_download_save_folder(int memory_type, char **download_folder, char **save_folder)
480 {
481         _INNER_FUNC_ENTER;
482
483         int ret = 1;
484         switch (memory_type) {
485         case 0:
486                 {
487                         sync_agent_dev_return_e dci_down_ret = sync_agent_get_devinfo(2, "FOTADownDir", download_folder);
488                         if (dci_down_ret == SYNC_AGENT_DEV_RETURN_SUCCESS) {
489                                 _DEBUG_VERBOSE(" download  = %s\n", *download_folder);
490                         } else {
491                                 ret = -1;
492                                 goto error;
493                         }
494                         sync_agent_dev_return_e dci_save_ret = sync_agent_get_devinfo(2, "FOTASaveDir", save_folder);
495                         if (dci_save_ret == SYNC_AGENT_DEV_RETURN_SUCCESS) {
496                                 _DEBUG_VERBOSE(" save  = %s: \n", *save_folder);
497                         } else {
498                                 ret = -1;
499                                 goto error;
500                         }
501                 }
502                 break;
503         case 1:
504                 {
505                         sync_agent_dev_return_e dci_down_ret = sync_agent_get_devinfo(2, "FOTA_SD_DownDir", download_folder);
506                         if (dci_down_ret == SYNC_AGENT_DEV_RETURN_SUCCESS) {
507                                 _DEBUG_VERBOSE(" download  = %s\n", *download_folder);
508                         } else {
509                                 ret = -1;
510                                 goto error;
511                         }
512                         sync_agent_dev_return_e dci_save_ret = sync_agent_get_devinfo(2, "FOTA_SD_SaveDir", save_folder);
513                         if (dci_save_ret == SYNC_AGENT_DEV_RETURN_SUCCESS) {
514                                 _DEBUG_VERBOSE(" save  = %s: \n", *save_folder);
515                         } else {
516                                 ret = -1;
517                                 goto error;
518                         }
519                 }
520                 break;
521         default:
522                 ret = -1;
523                 goto error;
524                 break;
525         }
526         _INNER_FUNC_EXIT;
527         return ret;
528  error:
529         _INNER_FUNC_EXIT;
530         return ret;
531 }
532
533 int _execute_dev_function_common(char *function_name, int args_cnt, va_list ap)
534 {
535         if (function_name == NULL) {
536                 _DEBUG_TRACE("KEY name is NULL !!!");
537                 return 0;
538         }
539
540         sync_agent_dev_return_e ret = SYNC_AGENT_DEV_RETURN_SUCCESS;
541
542         if (!strncmp(function_name, "pm_lock", strlen("pm_lock"))) {
543
544                 pm_state_e state = va_arg(ap, pm_state_e);
545                 pm_lock_state_flag_e flag = va_arg(ap, pm_lock_state_flag_e);
546                 unsigned int timeout = va_arg(ap, unsigned int);
547
548                 ret = func_execute_dev_function_common(1, function_name, args_cnt, state, flag, timeout);
549
550         } else if (!strncmp(function_name, "pm_unlock", strlen("pm_unlock"))) {
551
552                 pm_state_e state = va_arg(ap, pm_state_e);
553                 pm_unlock_state_flag_e flag = va_arg(ap, pm_unlock_state_flag_e);
554
555                 ret = func_execute_dev_function_common(1, function_name, args_cnt, state, flag);
556
557         } else if (!strncmp(function_name, "pm_change", strlen("pm_change"))) {
558
559                 pm_state_e state = va_arg(ap, pm_state_e);
560
561                 ret = func_execute_dev_function_common(1, function_name, args_cnt, state);
562
563         }
564
565         if (ret == SYNC_AGENT_DEV_RETURN_SUCCESS) {
566                 _DEBUG_TRACE("pFunc_Execute_Dev_Function_Common SUCCESS !!!");
567                 return 1;
568         } else {
569                 _DEBUG_TRACE("pFunc_Execute_Dev_Function_Common FAIL !!!");
570                 return 0;
571         }
572 }