3 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include <sync_agent.h>
23 #include "plugin_slp_device_dm.h"
24 #include "plugin_devexe_error.h"
25 #include "plugin_slp_device_ctrl.h"
30 #define EXPORT_API __attribute__ ((visibility("default")))
33 #ifndef OMADM_AGENT_LOG
35 #define LOG_TAG "PLUGIN_DEVICE_SLP_DM"
38 #define DELTA_FILE "delta_hash.txt"
40 static sync_agent_execute_dev_cb func_execute_dev_function_common = 0;
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);
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);
60 EXPORT_API int sync_agent_plugin_execute_dev_function(char *function_name, int args_cnt, va_list ap)
64 retvm_if((function_name) == NULL, 0, "function_name is NULL!!");
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"))) {
68 _DEBUG_INFO(" Received %s request !!!", function_name);
70 return _execute_dev_function_common(function_name, args_cnt, ap);
72 } else if (!strncmp(function_name, "fota_flag", strlen("fota_flag"))) {
74 int memory_type = va_arg(ap, int);
76 _DEBUG_INFO(" Received %s request !!!", function_name);
78 return _execute_dev_fota_flag(memory_type);
80 } else if (!strncmp(function_name, "fota_result", strlen("fota_result"))) {
82 int *plugin_err = va_arg(ap, int *);
84 _DEBUG_INFO(" Received %s request !!!", function_name);
86 return _execute_dev_fota_update_result(plugin_err);
88 } else if (!strncmp(function_name, "reboot", strlen("reboot"))) {
90 _DEBUG_INFO(" Received %s request !!!", function_name);
92 return _execute_dev_reboot();
94 } else if (!strncmp(function_name, "full_lock", strlen("full_lock"))) {
96 _DEBUG_INFO(" Received %s request !!!", function_name);
97 return _execute_dev_fully_lock();
99 } else if (!strncmp(function_name, "partial_lock", strlen("partial_lock"))) {
101 _DEBUG_INFO(" Received %s request !!!", function_name);
103 return _execute_dev_partially_lock();
105 } else if (!strncmp(function_name, "unlock", strlen("unlock"))) {
107 _DEBUG_INFO(" Received %s request !!!", function_name);
109 return _execute_dev_unlock();
111 } else if (!strncmp(function_name, "factory_reset", strlen("factory_reset"))) {
113 _DEBUG_INFO(" Received %s request !!!", function_name);
115 return _execute_dev_factory_reset();
117 } else if (!strncmp(function_name, "wipe", strlen("wipe"))) {
119 _DEBUG_INFO(" Received %s request !!!", function_name);
121 return _execute_dev_wipe();
124 _DEBUG_INFO("Device function requested not defined in plug-in !!!");
134 EXPORT_API void sync_agent_plugin_set_common_execute_dev_function(sync_agent_execute_dev_cb func)
138 func_execute_dev_function_common = func;
143 static int _execute_dev_fota_flag(int memory_type)
147 /*slp_device_dm_exe_e err = EXE_SUCCESS; */
150 service_ret = __control_fimware_delta_file(memory_type);
151 if (service_ret != 1) {
152 _DEBUG_TRACE("__control_fimware_delta_file() Fail");
155 _DEBUG_TRACE("control firmware delta file : %d \n", service_ret);
157 service_ret = slp_device_ctrl_set_fota_flag();
158 if (service_ret != 0) {
159 _DEBUG_TRACE("slp_device_ctrl_set_FOTA_flag() Fail");
164 return __convert_service_err_to_common_error(service_ret);
167 static int _execute_dev_fota_update_result(int *plugin_err)
171 *plugin_err = slp_device_ctrl_get_fota_update_result();
174 return __convert_service_err_to_common_error(1);
177 static int _execute_dev_reboot()
181 int service_ret = slp_device_ctrl_reboot();
184 return __convert_service_err_to_common_error(service_ret);
187 static int _execute_dev_fully_lock()
191 int service_ret = slp_device_ctrl_fully_lock();
194 return __convert_service_err_to_common_error(service_ret);
197 static int _execute_dev_partially_lock()
201 int service_ret = slp_device_ctrl_partially_lock();
204 return __convert_service_err_to_common_error(service_ret);
207 static int _execute_dev_unlock()
211 int service_ret = slp_device_ctrl_unlock();
214 return __convert_service_err_to_common_error(service_ret);
217 static int _execute_dev_factory_reset()
221 int service_ret = slp_device_ctrl_factory_reset();
224 return __convert_service_err_to_common_error(service_ret);
227 static int _execute_dev_wipe()
234 return __convert_service_err_to_common_error(service_ret);
238 /*static int __find_file_list(const char *folder_path, GSList **file_list)
240 _DEBUG_VERBOSE(" start");
243 if (folder_path == NULL) {
248 _DEBUG_VERBOSE(" folder_path = %s", folder_path);
250 GSList *fileList = NULL;
251 ret = sync_agent_get_file_list(folder_path, &fileList);
252 if (ret != 1 || fileList == NULL) {
258 char *temp_file_info = NULL;
259 char *temp_file_name = NULL;
260 char *temp_file_path = NULL;
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);
266 temp_file_name = strtok(temp_file_info, ",");
268 (*file_list) = g_slist_append((*file_list), temp_file_name);
271 _DEBUG_VERBOSE(" end");
274 _DEBUG_VERBOSE(" end error : %d", ret);
278 static int ___find_file(const char *folder_path, const char **file_path)
282 retvm_if((folder_path) == NULL, -1, "folder_path is NULL!!");
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) {
292 char *temp_file_info = NULL;
293 char *temp_file_name = NULL;
295 temp_file_info = (char *)(fileList->data);
296 _DEBUG_VERBOSE(" file_name = %s", temp_file_info);
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);
306 _DEBUG_VERBOSE(" end error : %d", ret);
311 static int ___check_filename_md5(char *folder_path, char *file_buffer)
316 retvm_if((folder_path) == NULL, -1, "folder_path is NULL!!");
317 retvm_if((file_buffer) == NULL, -1, "file_buffer is NULL!!");
319 _DEBUG_VERBOSE(" check_file_folder : %s", folder_path);
320 _DEBUG_VERBOSE(" check_file : %s", file_buffer);
323 char *check_segment = NULL;
324 char *find_file_name = NULL;
325 unsigned char *find_md5_word = NULL;
326 char *file_path = NULL;
328 GSList *filename_md5 = NULL;
329 GSList *filename_md5_iter = NULL;
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);
337 while (check_segment != NULL) {
338 /*check_segment = strtok(NULL, ":\t\n "); */
339 check_segment = strtok(NULL, delimiter);
340 if (check_segment == NULL)
342 filename_md5 = g_slist_append(filename_md5, check_segment);
344 _DEBUG_VERBOSE(" check file : %s", check_segment);
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) {
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);
358 file_path = g_strdup_printf("%s/%s", folder_path, find_file_name);
359 ret = sync_agent_is_existing_fs(file_path);
364 ret = sync_agent_file_md5_validation(file_path, (char *)find_md5_word, &isSame);
365 if (ret != 1 || (ret == 1 && isSame == false))
372 _DEBUG_VERBOSE(" end error : %d", ret);
377 static int ___check_fimware_delta_validation(char *save_folder)
381 retvm_if((save_folder) == NULL, -1, "save_folder is NULL!!");
384 char *required_contents = NULL;
385 unsigned long required_length = 0;
386 char *file_path = NULL;
389 file_path = g_strdup_printf("%s/%s", save_folder, DELTA_FILE);
390 _DEBUG_VERBOSE(" file_path : %s", file_path);
392 ret = sync_agent_read_whole_file(file_path, &required_contents, &required_length);
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);
399 ret = ___check_filename_md5(save_folder, required_contents);
403 ret = sync_agent_free_file(&required_contents);
412 _DEBUG_VERBOSE(" end error : %d \n", ret);
417 static int __control_fimware_delta_file(int memory_type)
423 char *download_folder = NULL;
424 char *save_folder = NULL;
426 ret = ___get_download_save_folder(memory_type, &download_folder, &save_folder);
430 const char *file_path = NULL;
431 ret = ___find_file(download_folder, &file_path);
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()");
442 _DEBUG_VERBOSE(" Success to call sync_agent_uncompress()");
445 ret = ___check_fimware_delta_validation(save_folder);
450 fs_error = sync_agent_empty_directory_contents(download_folder);
455 _DEBUG_VERBOSE(" end error : %d", ret);
460 static sync_agent_dev_return_e __convert_service_err_to_common_error(int service_err)
464 slp_device_dm_exe_e ret = EXE_SUCCESS;
466 switch (service_err) {
471 ret = EXE_DEV_OPER_NOT_PERFORMED;
479 static int ___get_download_save_folder(int memory_type, char **download_folder, char **save_folder)
484 switch (memory_type) {
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);
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);
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);
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);
533 int _execute_dev_function_common(char *function_name, int args_cnt, va_list ap)
535 if (function_name == NULL) {
536 _DEBUG_TRACE("KEY name is NULL !!!");
540 sync_agent_dev_return_e ret = SYNC_AGENT_DEV_RETURN_SUCCESS;
542 if (!strncmp(function_name, "pm_lock", strlen("pm_lock"))) {
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);
548 ret = func_execute_dev_function_common(1, function_name, args_cnt, state, flag, timeout);
550 } else if (!strncmp(function_name, "pm_unlock", strlen("pm_unlock"))) {
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);
555 ret = func_execute_dev_function_common(1, function_name, args_cnt, state, flag);
557 } else if (!strncmp(function_name, "pm_change", strlen("pm_change"))) {
559 pm_state_e state = va_arg(ap, pm_state_e);
561 ret = func_execute_dev_function_common(1, function_name, args_cnt, state);
565 if (ret == SYNC_AGENT_DEV_RETURN_SUCCESS) {
566 _DEBUG_TRACE("pFunc_Execute_Dev_Function_Common SUCCESS !!!");
569 _DEBUG_TRACE("pFunc_Execute_Dev_Function_Common FAIL !!!");