2 * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <agl_thread.h>
25 #include <sys/epoll.h>
26 #include <sys/eventfd.h>
27 #include <sys/prctl.h>
29 #include <sys/syscall.h>
30 #include <sys/timerfd.h>
31 #include <sys/types.h>
33 #include <native_service/frameworkunified_framework_if.h>
34 #include <system_service/ss_system_manager_if.h>
44 #include "bkup_backupmanagerlog.h"
45 #include "bkup_param.h"
46 #include "bkup_crc32.h"
47 #include "bkup_util.h"
49 #define BKUP_THREAD_NAND "NS_BkupNAND"
50 #define BKUP_THREAD_DELAY "NS_BkupDelay"
51 const char kBkupPrexixEncryption[] = "ENC_";
52 const size_t kBkupPrexixEncryptionSize = 4;
54 typedef void (* signal_handler)(int);
55 extern void SignalHandlerFuncForMsgHandleThread(int signum);
56 extern pthread_t g_delay_thread_id;
57 extern pthread_t g_nand_thread_id;
59 #define MAX_FD_EPOLL 8
69 static bkup_state_t g_bkup_internal_state = BKUP_STATE_INIT;
73 * delay thread command
76 BKUP_DELAY_CMD_TERM = 0,
77 BKUP_DELAY_CMD_PRE_ACCOFF,
78 BKUP_DELAY_CMD_REGIST,
83 * inter thread communication message
100 } bkup_itc_message_t;
103 * reference response function
106 int (*set_response)(void *, void *, int, EFrameworkunifiedStatus);
111 * response MQ map(TLS)
113 typedef std::map<std::string, HANDLE> g_bkup_response_mq_t;
114 static __thread g_bkup_response_mq_t *g_bkup_response_mq;
119 static mqd_t g_bkup_nand_mqd = -1;
122 * Delay thread itc MQ
124 static mqd_t g_bkup_delay_mqd = -1;
126 static int g_bkup_nand_tid;
127 static int g_bkup_delay_tid;
132 static void BkupApilog(bkup_response_t *response, enum NvHalMedia current_media,
133 bkup_protocol_header_t *hdr,
134 bkup_query_result_t *query_result, char *buf,
135 uint32_t crc32, EFrameworkunifiedStatus ret_value);
138 * check status of file spicefied by path
140 static int BkupCheckStatFromNv(enum NvHalMedia media, const char *path,
146 static inline void BkupRwlockUnlock(pthread_rwlock_t *rwlock, const char *path);
151 static inline void BkupRwlockRdlock(pthread_rwlock_t *rwlock, const char *path);
156 static inline void BkupRwlockWrlock(pthread_rwlock_t *rwlock, const char *path);
159 * \~english @par Brief
160 * Check the data stored on the storage device and check the data size.
161 * \~english @param [in] media
162 * enum NvHalMedia - Media type
163 * \~english @param [in] filename
164 * const char* - Concatenated string of "category name/item name"
165 * \~english @param [in] size
166 * uint32_t - Data size to be checked
167 * \~english @retval eFrameworkunifiedStatusOK OK
168 * \~english @retval eFrameworkunifiedStatusFileLoadError The file does not exist.
169 * \~english @retval eFrameworkunifiedStatusAccessError Check size failed
170 * \~english @retval eFrameworkunifiedStatusInvldParam Parameter error
171 * \~english @par Preconditons
173 * \~english @par Conditions of processing failure
174 * - If the media (media) specified in the parameter is smaller than NVHALMEDIA_CACHEDRAM or larger than NVHALMEDIA_NAND.\n
175 * [eFrameworkunifiedStatusInvldParam]
176 * - The identifier specified in the arguments is a NULL.[eFrameworkunifiedStatusInvldParam]
177 * - When the size specified in the arguments is 0.[eFrameworkunifiedStatusInvldParam]
178 * - Internal I/O handling errors (lstat).[eFrameworkunifiedStatusFileLoadError]
179 * - The size specified by the parameter does not correspond to the file size specified by the identifier.[eFrameworkunifiedStatusAccessError]
181 static EFrameworkunifiedStatus CheckNv(enum NvHalMedia media, const char *filename, uint32_t size) {
183 EFrameworkunifiedStatus ret;
184 ret = GetSizeNv(media, filename, &size_); // Get size
185 if (ret != eFrameworkunifiedStatusOK) { // If the GetSizeNv fails, the error is returned unchanged.
188 if (size_ != size) { // Returns eFrameworkunifiedStatusAccessError if the sizes do not match.
189 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "invalid size:%d\n", size);
190 return eFrameworkunifiedStatusAccessError;
192 return eFrameworkunifiedStatusOK; // Returns eFrameworkunifiedStatusOK if size matches
198 * read file spicefied by id
200 * \retval 0 read success
201 * \retval -1 crc error
202 * \retval -2 read failed
203 * \retval -3 invalid param
205 static int32_t BkupBufferReadFromNv(const enum NvHalMedia media, const char* path,
206 char *buf, size_t size, uint32_t *crc32) {
208 EFrameworkunifiedStatus nv_ret = eFrameworkunifiedStatusFail;
209 const uint32_t size_all = static_cast<uint32_t>(size + sizeof(uint32_t));
210 void* buffer = MAP_FAILED;
211 uint8_t* read_buffer = NULL;
212 uint32_t calc_crc32, get_crc32;
214 if (path == NULL) { // LCOV_EXCL_BR_LINE 6:double check
215 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
216 return -1; // LCOV_EXCL_LINE 6:double check
220 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "buf is NULL");
223 if ((buffer = BkupAnonMmap(size_all)) == MAP_FAILED) { // LCOV_EXCL_BR_LINE 5:mmap's error case.
224 // LCOV_EXCL_START 5:mmap's error case.
225 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
226 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "mmap:map failed");
229 // LCOV_EXCL_STOP 5:mmap's error case.
232 read_buffer = reinterpret_cast<uint8_t*>(buffer);
233 nv_ret = ReadNv(media, path, read_buffer, size_all);
234 if (nv_ret == eFrameworkunifiedStatusOK) {
235 const void* const src_buffer = reinterpret_cast<void *>(read_buffer + size);
236 void* dest_buffer = reinterpret_cast<void *>(&get_crc32);
237 memcpy(dest_buffer, src_buffer, sizeof(uint32_t));
238 calc_crc32 = BkupCrc32(read_buffer, size); // LCOV_EXCL_BR_LINE 11:unexpected branch
239 if (get_crc32 != calc_crc32) {
240 // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
241 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "invalid crc(%s):%#x:%#x", path, get_crc32, calc_crc32);
242 // LCOV_EXCL_BR_STOP 15:marco defined in "native_service/ns_logger_if.h"
249 dest_buffer = reinterpret_cast<void *>(buf);
250 memcpy(dest_buffer, buffer, size);
251 } else if (nv_ret == eFrameworkunifiedStatusInvldParam) { // LCOV_EXCL_BR_LINE 6:double check
257 if (buffer != MAP_FAILED) { // LCOV_EXCL_BR_LINE 5:mmap's error case.
258 munmap(buffer, size_all);
265 * buffer -> file write
267 static int32_t BkupBufferWriteToNv(const enum NvHalMedia media, const char* path,
268 char *buf, size_t size, uint32_t *crc32) {
270 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "buf is NULL");
271 return eFrameworkunifiedStatusNullPointer;
274 EFrameworkunifiedStatus nv_ret = eFrameworkunifiedStatusFail;
275 const uint32_t size_all = static_cast<uint32_t>(size + sizeof(uint32_t));
276 char *buffer = reinterpret_cast<char *>(MAP_FAILED);
279 if ((buffer = BkupAnonMmap(size_all)) == MAP_FAILED) { // LCOV_EXCL_BR_LINE 5:mmap's error case.
280 // LCOV_EXCL_START 5:mmap's error case.
281 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
282 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "mmap:%s", strerror(errno));
285 // LCOV_EXCL_STOP 5:mmap's error case.
288 calc_crc32 = BkupCrc32(buf, size); // LCOV_EXCL_BR_LINE 11:unexpected branch
293 memcpy(buffer, buf, size);
294 memcpy(buffer + size, &calc_crc32, sizeof(uint32_t));
296 nv_ret = WriteNv(media, path, reinterpret_cast<uint8_t *>(buffer), size_all);
297 if (nv_ret != eFrameworkunifiedStatusOK) {
298 // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
299 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "write to nv(%s):%s", path, strerror(ret));
300 // LCOV_EXCL_BR_STOP 15:marco defined in "native_service/ns_logger_if.h"
305 if (buffer != MAP_FAILED) { // LCOV_EXCL_BR_LINE 5:mmap's error case.
306 munmap(buffer, size_all);
315 static int BkupMediaCopyFromNv(char *category_name, const char *item_name,
316 enum NvHalMedia src, enum NvHalMedia dst,
317 pthread_rwlock_t *src_rwlock,
318 pthread_rwlock_t *dst_rwlock) {
319 if (category_name == NULL) {
320 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "category_name is NULL");
321 return eFrameworkunifiedStatusNullPointer;
324 if (item_name == NULL) {
325 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "item_name is NULL");
326 return eFrameworkunifiedStatusNullPointer;
330 char id_path[FILENAME_MAX] = {0};
331 char *buffer = reinterpret_cast<char *>(MAP_FAILED);
333 bkup_query_result_t query_result;
334 BkupParamGet(item_name, &query_result); // LCOV_EXCL_BR_LINE 11:unexpected branch
336 char prefix_name[kBkupPrexixEncryptionSize + 1] = {0};
337 if (query_result.encrypt != false) {
338 strncpy(prefix_name, kBkupPrexixEncryption, kBkupPrexixEncryptionSize);
340 snprintf(id_path, FILENAME_MAX, "%s/%s%s", category_name, prefix_name, item_name);
342 if ((buffer = BkupAnonMmap(query_result.size)) == MAP_FAILED) { // LCOV_EXCL_BR_LINE 5:mmap's error case.
343 // LCOV_EXCL_START 5:mmap's error case.
344 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
345 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "mmap:map failed");
346 ret = eFrameworkunifiedStatusFail;
348 // LCOV_EXCL_STOP 5:mmap's error case.
351 BkupRwlockRdlock(src_rwlock, id_path); // LCOV_EXCL_BR_LINE 11:unexpected branch
352 if ((ret = BkupBufferReadFromNv(src, id_path, buffer, query_result.size,
354 // LCOV_EXCL_BR_START 11:unexpected branch
355 BkupRwlockWrlock(dst_rwlock, id_path);
356 ret = BkupBufferWriteToNv(dst, id_path, buffer, query_result.size, &crc32);
357 BkupRwlockUnlock(dst_rwlock, id_path);
358 // LCOV_EXCL_BR_STOP 11:unexpected branch
360 BkupRwlockUnlock(src_rwlock, id_path); // LCOV_EXCL_BR_LINE 11:unexpected branch
363 if (buffer != MAP_FAILED) { // LCOV_EXCL_BR_LINE 5:mmap's error case.
364 munmap(buffer, query_result.size);
373 static int BkupMakeBkupPathToNvId(const char *category_name,
374 const char *item_name, char **path_buf,
377 char prefix_name[kBkupPrexixEncryptionSize + 1] = {0};
379 if ((*path_buf = BkupAnonMmap(buf_size)) == MAP_FAILED) { // LCOV_EXCL_BR_LINE 5:mmap's error case.
380 // LCOV_EXCL_START 5:mmap's error case.
381 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
382 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "mmap:%s", strerror(errno));
384 // LCOV_EXCL_STOP 5:mmap's error case.
386 bkup_query_result_t query_result;
387 if (BkupParamGet(item_name, &query_result) < 0) { // LCOV_EXCL_BR_LINE 6:Excluded due to checking by upper-level function
388 // LCOV_EXCL_START 6:Excluded due to checking by upper-level function
389 AGL_ASSERT_NOT_TESTED();
390 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "Invalid item_name:%s", item_name);
392 // LCOV_EXCL_STOP 6:Excluded due to checking by upper-level function
394 if (query_result.encrypt != false) {
395 strncpy(prefix_name, kBkupPrexixEncryption, kBkupPrexixEncryptionSize);
397 snprintf(*path_buf, static_cast<size_t>(buf_size), "%s/%s%s",
398 category_name, prefix_name, item_name);
408 static int BkupDeleteDataFromNv(const enum NvHalMedia media, const char* const path) {
410 EFrameworkunifiedStatus e_status = eFrameworkunifiedStatusOK;
412 if (path == NULL) { // LCOV_EXCL_BR_LINE 6:Excluded due to checking by upper-level function
413 // LCOV_EXCL_START 6:Excluded due to checking by upper-level function
414 AGL_ASSERT_NOT_TESTED();
415 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "path is NULL");
418 // LCOV_EXCL_STOP 6:Excluded due to checking by upper-level function
420 const size_t path_size = static_cast<size_t>(std::string(path).length());
421 if (path_size > FILENAME_MAX) { // LCOV_EXCL_BR_LINE 6:Excluded due to checking by upper-level function
422 // LCOV_EXCL_START 6:Excluded due to checking by upper-level function
423 AGL_ASSERT_NOT_TESTED();
424 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "path size error");
427 // LCOV_EXCL_STOP 6:Excluded due to checking by upper-level function
431 e_status = GetSizeNv(media, path, &size);
432 if (e_status == eFrameworkunifiedStatusFileLoadError) {
433 // If the file does not exist, deleet it successfully
435 } else if (e_status != eFrameworkunifiedStatusOK) {
436 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "GetSizeNv e_status:%d\n", e_status);
443 e_status = DeleteNv(media, path);
444 if (e_status != eFrameworkunifiedStatusOK) {
446 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "DeleteNv e_status:%d\n", e_status);
454 static pthread_rwlock_t *BkupGetRwlock(const char *item_name,
455 enum NvHalMedia media,
456 bkup_query_result_t *query_result) {
458 pthread_rwlock_t *rwlock, *org_rwlock;
459 pthread_rwlockattr_t attr;
461 if (query_result == NULL) {
462 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "query_result is NULL");
466 if (media != NVHALMEDIA_BACKUPDRAM || query_result->backup_dram == false || query_result->nand == false) {
469 // backupDram && nand
470 if (query_result->opt != NULL) {
471 return reinterpret_cast<pthread_rwlock_t *>(query_result->opt);
474 if ((ret = pthread_rwlockattr_init(&attr)) != 0) { // LCOV_EXCL_BR_LINE 5:pthread_rwlockattr_init's error case.
475 // LCOV_EXCL_START 5:pthread_rwlockattr_init's error case.
476 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
477 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "pthread_rwlockattr_init:%s", strerror(ret));
479 // LCOV_EXCL_STOP 5:pthread_rwlockattr_init's error case.
482 // LCOV_EXCL_BR_START 5:pthread_rwlockattr_setkind_np's error case.
483 if ((ret = pthread_rwlockattr_setkind_np(&attr, PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP)) != 0) {
484 // LCOV_EXCL_BR_STOP 5:pthread_rwlockattr_setkind_np's error case.
485 // LCOV_EXCL_START 5:pthread_rwlockattr_setkind_np's error case.
486 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
487 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "pthread_rwlockattr_setkind_np:%s", strerror(ret));
489 // LCOV_EXCL_STOP 5:pthread_rwlockattr_setkind_np's error case.
492 // LCOV_EXCL_BR_START 5:malloc's error case.
493 if ((rwlock = reinterpret_cast<pthread_rwlock_t *>(malloc(sizeof(pthread_rwlock_t)))) == NULL) {
494 // LCOV_EXCL_BR_STOP 5:malloc's error case.
495 // LCOV_EXCL_START 5:malloc's error case.
496 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
497 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "malloc failed");
499 // LCOV_EXCL_STOP 5:malloc's error case.
502 if ((ret = pthread_rwlock_init(rwlock, &attr)) != 0) { // LCOV_EXCL_BR_LINE 5:pthread_rwlock_init's error case.
503 // LCOV_EXCL_START 5:pthread_rwlock_init's error case.
504 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
505 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "pthread_rwlock_init:%s", strerror(ret));
508 // LCOV_EXCL_STOP 5:pthread_rwlock_init's error case.
511 // LCOV_EXCL_BR_START 11:unexpected branch
512 org_rwlock = reinterpret_cast<pthread_rwlock_t *>(BkupParamSetOpt(item_name, rwlock));
513 // LCOV_EXCL_BR_STOP 11:unexpected branch
514 if (org_rwlock == reinterpret_cast<void *>(-1)) { // LCOV_EXCL_BR_LINE 6:org_rwlock will not be -1
515 // LCOV_EXCL_START 6:org_rwlock will not be -1
516 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
517 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "Invalid item_name:%s", item_name != 0 ? item_name : NULL);
520 // LCOV_EXCL_STOP 6:org_rwlock will not be -1
521 } else if (org_rwlock != NULL) { // LCOV_EXCL_BR_LINE 5:gcc builtin function's error case
523 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
524 free(rwlock); // LCOV_EXCL_LINE 5:gcc builtin function's error case
525 rwlock = org_rwlock; // LCOV_EXCL_LINE 5:gcc builtin function's error case
528 query_result->opt = rwlock;
535 static inline void BkupRwlockRdlock(pthread_rwlock_t *rwlock, const char *path) {
538 if ((ret = pthread_rwlock_rdlock(rwlock)) != 0) { // LCOV_EXCL_BR_LINE 5:pthread_rwlock_rdlock's error case.
539 // LCOV_EXCL_START 5:pthread_rwlock_rdlock's error case.
540 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
541 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "pthread_rwlock_rdlock(%s):%s", path, strerror(ret));
542 // LCOV_EXCL_STOP 5:pthread_rwlock_rdlock's error case.
550 static inline void BkupRwlockWrlock(pthread_rwlock_t *rwlock, const char *path) {
553 if ((ret = pthread_rwlock_wrlock(rwlock)) != 0) { // LCOV_EXCL_BR_LINE 5:pthread_rwlock_wrlock's error case.
554 // LCOV_EXCL_START 5:pthread_rwlock_wrlock's error case.
555 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
556 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "pthread_rwlock_wrlock(%s):%s", path, strerror(ret));
557 // LCOV_EXCL_STOP 5:pthread_rwlock_wrlock's error case.
565 static inline void BkupRwlockUnlock(pthread_rwlock_t *rwlock, const char *path) {
568 if ((ret = pthread_rwlock_unlock(rwlock)) != 0) { // LCOV_EXCL_BR_LINE 5:pthread_rwlock_unlock's error case.
569 // LCOV_EXCL_START 5:pthread_rwlock_unlock's error case.
570 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
571 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "pthread_rwlock_unlock(%s):%s", path, strerror(ret));
572 // LCOV_EXCL_STOP 5:pthread_rwlock_unlock's error case.
578 * inter thread communication MQ open
580 static mqd_t BkupItcMqopen(const char *mq_name, int flags) {
583 if (mq_name == NULL) {
584 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "mq_name is NULL");
589 attr.mq_maxmsg = 256;
590 attr.mq_msgsize = sizeof(bkup_itc_message_t);
592 return mq_open(mq_name, flags, 0660, &attr);
598 static int BkupWriteRename(const char *category_name, const char *item_name,
599 enum NvHalMedia media, char *buf, int size,
600 pthread_rwlock_t *rwlock) {
602 char *full_path = reinterpret_cast<char *>(MAP_FAILED);
604 // LCOV_EXCL_BR_START 5:mmap's error case.
605 if (BkupMakeBkupPathToNvId(category_name, item_name, &full_path, FILENAME_MAX) < 0) {
606 // LCOV_EXCL_BR_STOP 5:mmap's error case.
607 // LCOV_EXCL_START 5:mmap's error case.
608 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
610 // LCOV_EXCL_STOP 5:mmap's error case.
613 BkupRwlockWrlock(rwlock, full_path); // LCOV_EXCL_BR_LINE 11:unexpected branch
614 if (BkupBufferWriteToNv(media, full_path, buf, size, NULL) < 0) {
615 BkupRwlockUnlock(rwlock, full_path); // LCOV_EXCL_BR_LINE 11:unexpected branch
618 BkupRwlockUnlock(rwlock, full_path); // LCOV_EXCL_BR_LINE 11:unexpected branch
623 if (full_path != MAP_FAILED) { // LCOV_EXCL_BR_LINE 5:mmap's error case.
624 munmap(full_path, FILENAME_MAX);
632 * \retval 0 file exists
633 * \retval -1 file not exists
634 * \retval -2 size error
636 static int BkupCheckStatFromNv(enum NvHalMedia media, const char *path,
638 uint32_t size = check_size + static_cast<uint32_t>(sizeof(uint32_t));
639 const EFrameworkunifiedStatus ret = CheckNv(media, path, size);
642 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "path is NULL");
646 if (eFrameworkunifiedStatusOK == ret) {
648 } else if (eFrameworkunifiedStatusAccessError == ret) {
649 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "CheckNv e_status:%d", ret);
652 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "CheckNv e_status:%d", ret);
660 static int BkupMediaStat(const char *category_name, const char *item_name, enum NvHalMedia src, int size) {
662 char *path = reinterpret_cast<char *>(MAP_FAILED);
664 // LCOV_EXCL_BR_START 5:mmap's error case.
665 if (BkupMakeBkupPathToNvId(category_name, item_name, &path, FILENAME_MAX) < 0) {
666 // LCOV_EXCL_BR_STOP 5:mmap's error case.
667 // LCOV_EXCL_START 5:mmap's error case.
668 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
670 // LCOV_EXCL_STOP 5:mmap's error case.
672 ret = BkupCheckStatFromNv(src, path, size); // LCOV_EXCL_BR_LINE 11:unexpected branch
675 if (path != MAP_FAILED) { // LCOV_EXCL_BR_LINE 5:mmap's error case.
676 munmap(path, FILENAME_MAX);
684 * \retval 0 file exists
685 * \retval -1 file not exists
687 static int BkupCheckStatRead(enum NvHalMedia current_media, const char *path,
688 char *buf, int check_size, uint32_t *crc32,
689 pthread_rwlock_t *rwlock) {
692 if ((ret = BkupCheckStatFromNv(current_media, path, check_size)) < 0) {
696 BkupRwlockRdlock(rwlock, path);
698 if (BkupBufferReadFromNv(current_media, path, buf, check_size, crc32) < 0) {
699 BkupRwlockUnlock(rwlock, path);
703 BkupRwlockUnlock(rwlock, path);
710 * \retval 0 file invalid & do nothing
711 * \retval -1 file invalid but create 0 fill file
712 * \retval 1 file valid
714 static int BkupCheckRead(const char *path, enum NvHalMedia current_media,
715 bkup_protocol_command_t command, char *rcv_buf,
716 char *read_buf, uint32_t *crc32,
717 bkup_query_result_t *query_result,
718 pthread_rwlock_t *rwlock) {
721 if (rcv_buf == NULL) {
722 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "rcv_buf is NULL");
725 if (query_result == NULL) {
726 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "query_result is NULL");
730 bkup_protocol_header_t *hdr = reinterpret_cast<bkup_protocol_header_t *>(rcv_buf);
732 if (BkupCheckStatRead(current_media, path, read_buf, query_result->size, crc32, rwlock) < 0) {
734 if (current_media == NVHALMEDIA_NAND || query_result->nand == false) {
737 if (command == BKUP_CMD_READ) {
741 if (BkupCheckStatFromNv(NVHALMEDIA_NAND, path, query_result->size) == 0) {
745 // take preference BackupDRAM or CacheDRAM
752 pthread_rwlock_t *nand_rwlock = BkupGetRwlock(hdr->item_name, NVHALMEDIA_NAND, query_result);
753 if (BkupCheckStatRead(NVHALMEDIA_NAND, path, read_buf, query_result->size, NULL, nand_rwlock) == 0) {
755 if (BkupWriteRename(query_result->category_name, hdr->item_name,
756 current_media, read_buf, query_result->size,
760 // copy error, do nothing
764 // take preference BackupDRAM or CacheDRAM
779 static int BkupCmdRead(bkup_response_t *response, enum NvHalMedia current_media,
780 char *rcv_buf, bkup_query_result_t *query_result) {
782 if (response == NULL) {
783 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "response is NULL");
786 if (rcv_buf == NULL) {
787 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "rcv_buf is NULL");
790 if (query_result == NULL) {
791 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "query_result is NULL");
794 bkup_protocol_header_t *hdr = reinterpret_cast<bkup_protocol_header_t *>(rcv_buf);
795 // LCOV_EXCL_BR_START 11:unexpected branch
796 pthread_rwlock_t *rwlock = BkupGetRwlock(hdr->item_name, current_media, query_result);
797 // LCOV_EXCL_BR_STOP 11:unexpected branch
798 char *full_path = reinterpret_cast<char *>(MAP_FAILED);
799 char *buf = reinterpret_cast<char *>(MAP_FAILED);
802 EFrameworkunifiedStatus e_status = eFrameworkunifiedStatusOK;
804 // LCOV_EXCL_BR_START 5:mmap's error case.
805 if (BkupMakeBkupPathToNvId(query_result->category_name, hdr->item_name, &full_path, FILENAME_MAX) < 0) {
806 // LCOV_EXCL_BR_STOP 5:mmap's error case.
807 // LCOV_EXCL_START 5:mmap's error case.
808 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
809 e_status = eFrameworkunifiedStatusFail;
811 // LCOV_EXCL_STOP 5:mmap's error case.
814 if ((buf = BkupAnonMmap(query_result->size)) == MAP_FAILED) { // LCOV_EXCL_BR_LINE 5:mmap's error case.
815 // LCOV_EXCL_START 5:mmap's error case.
816 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
817 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "mmap:%s", strerror(errno));
818 e_status = eFrameworkunifiedStatusFail;
820 // LCOV_EXCL_STOP 5:mmap's error case.
822 buf_size = query_result->size;
824 ret = BkupCheckRead(full_path, current_media, hdr->command, rcv_buf, buf,
825 &crc32, query_result, rwlock); // LCOV_EXCL_BR_LINE 11:unexpected branch
830 } else if (ret == 0) {
839 if (current_media == NVHALMEDIA_NAND) {
840 if (query_result->cache_dram) {
841 if (BkupMediaStat(query_result->category_name, hdr->item_name,
842 NVHALMEDIA_CACHEDRAM, query_result->size) < 0) {
843 // LCOV_EXCL_BR_START 11:unexpected branch
844 pthread_rwlock_t *dst_rwlock =
845 BkupGetRwlock(hdr->item_name, NVHALMEDIA_CACHEDRAM, query_result);
846 // LCOV_EXCL_BR_STOP 11:unexpected branch
847 BkupWriteRename(query_result->category_name, hdr->item_name,
848 NVHALMEDIA_CACHEDRAM, buf, query_result->size,
849 dst_rwlock); // LCOV_EXCL_BR_LINE 11:unexpected branch
852 if (query_result->backup_dram) {
853 if (BkupMediaStat(query_result->category_name, hdr->item_name,
854 NVHALMEDIA_BACKUPDRAM, query_result->size) < 0) {
855 // LCOV_EXCL_BR_START 11:unexpected branch
856 pthread_rwlock_t *dst_rwlock =
857 BkupGetRwlock(hdr->item_name, NVHALMEDIA_BACKUPDRAM, query_result);
858 // LCOV_EXCL_BR_STOP 11:unexpected branch
859 BkupWriteRename(query_result->category_name, hdr->item_name,
860 NVHALMEDIA_BACKUPDRAM, buf, query_result->size,
861 dst_rwlock); // LCOV_EXCL_BR_LINE 11:unexpected branch
867 if (ret != 0 && buf != MAP_FAILED) { // LCOV_EXCL_BR_LINE 5: mmap's error case.
868 // LCOV_EXCL_BR_START 11:unexpected branch
869 BkupApilog(response, current_media, hdr, query_result, buf, crc32, e_status);
870 // LCOV_EXCL_BR_STOP 11:unexpected branch
871 // LCOV_EXCL_BR_START 4: NSFW error case
872 if ((*response->set_response)(response->opt, buf + hdr->offset, hdr->size, e_status) < 0) {
873 // LCOV_EXCL_BR_STOP 4: NSFW error case
874 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
875 ret = -1; // LCOV_EXCL_LINE 4: NSFW error case
879 if (full_path != MAP_FAILED) { // LCOV_EXCL_BR_LINE 5: mmap's error case.
880 munmap(full_path, FILENAME_MAX);
882 if (buf != MAP_FAILED) { // LCOV_EXCL_BR_LINE 5: mmap's error case.
883 munmap(buf, buf_size);
891 static int BkupCmdWrite(bkup_response_t *response,
892 enum NvHalMedia current_media, char *rcv_buf,
893 bkup_query_result_t *query_result) {
896 if (response == NULL) {
897 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "response is NULL");
900 if (rcv_buf == NULL) {
901 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "rcv_buf is NULL");
904 if (query_result == NULL) {
905 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "query_result is NULL");
908 bkup_protocol_header_t *hdr = reinterpret_cast<bkup_protocol_header_t *>(rcv_buf);
909 // LCOV_EXCL_BR_START 11:unexpected branch
910 pthread_rwlock_t *rwlock = BkupGetRwlock(hdr->item_name, current_media, query_result);
911 // LCOV_EXCL_BR_STOP 11:unexpected branch
912 char *full_path = reinterpret_cast<char *>(MAP_FAILED);
913 char *buf = reinterpret_cast<char *>(MAP_FAILED);
918 // LCOV_EXCL_BR_START 5: mmap's error case.
919 if (BkupMakeBkupPathToNvId(query_result->category_name, hdr->item_name, &full_path, FILENAME_MAX) < 0) {
920 // LCOV_EXCL_BR_STOP 5: mmap's error case.
921 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
922 goto exit; // LCOV_EXCL_LINE 5: mmap's error case.
925 if (hdr->offset != 0 || hdr->size != (uint32_t)query_result->size) {
929 if ((buf = BkupAnonMmap(query_result->size)) == MAP_FAILED) { // LCOV_EXCL_BR_LINE 5: mmap's error case.
930 // LCOV_EXCL_START 5: mmap's error case.
931 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
932 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "mmap:%s", strerror(errno));
934 // LCOV_EXCL_STOP 5: mmap's error case.
936 buf_size = query_result->size;
938 // LCOV_EXCL_BR_START 11:unexpected branch
939 ret = BkupCheckRead(full_path, current_media, hdr->command, rcv_buf, buf, NULL, query_result, rwlock);
940 // LCOV_EXCL_BR_STOP 11:unexpected branch
947 memcpy(buf + hdr->offset, rcv_buf + sizeof(bkup_protocol_header_t), hdr->size);
948 BkupRwlockWrlock(rwlock, full_path); // LCOV_EXCL_BR_LINE 11:unexpected branch
949 if (BkupBufferWriteToNv(current_media, full_path, buf, query_result->size, &crc32) < 0) {
950 BkupRwlockUnlock(rwlock, full_path); // LCOV_EXCL_BR_LINE 11:unexpected branch
953 BkupRwlockUnlock(rwlock, full_path); // LCOV_EXCL_BR_LINE 11:unexpected branch
958 header = rcv_buf + sizeof(bkup_protocol_header_t);
959 BkupRwlockWrlock(rwlock, full_path); // LCOV_EXCL_BR_LINE 11:unexpected branch
960 if (BkupBufferWriteToNv(current_media, full_path,
961 rcv_buf + sizeof(bkup_protocol_header_t), hdr->size, &crc32) < 0) {
962 BkupRwlockUnlock(rwlock, full_path); // LCOV_EXCL_BR_LINE 11:unexpected branch
965 BkupRwlockUnlock(rwlock, full_path); // LCOV_EXCL_BR_LINE 11:unexpected branch
972 EFrameworkunifiedStatus e_status = ret < 0 ? eFrameworkunifiedStatusFail : eFrameworkunifiedStatusOK;
973 if (current_media == NVHALMEDIA_NAND) {
974 if (query_result->cache_dram == false && query_result->backup_dram == false) {
975 // LCOV_EXCL_BR_START 11:unexpected branch
976 BkupApilog(response, current_media, hdr, query_result, header, crc32, e_status);
977 // LCOV_EXCL_BR_STOP 11:unexpected branch
979 if ((*response->set_response)(response->opt, NULL, 0, e_status) < 0) { // LCOV_EXCL_BR_LINE 4: NSFW error case
980 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
981 ret = -1; // LCOV_EXCL_LINE 4: NSFW error case
984 // LCOV_EXCL_BR_START 11:unexpected branch
985 BkupApilog(response, current_media, hdr, query_result, header, crc32, e_status);
986 // LCOV_EXCL_BR_STOP 11:unexpected branch
989 if (buf != MAP_FAILED) { // LCOV_EXCL_BR_LINE 5: mmap's error case.
990 munmap(buf, buf_size);
992 if (full_path != MAP_FAILED) { // LCOV_EXCL_BR_LINE 5: mmap's error case.
993 munmap(full_path, FILENAME_MAX);
1001 static int BkupCmdFill(bkup_response_t *response, enum NvHalMedia current_media,
1002 char *rcv_buf, bkup_query_result_t *query_result) {
1004 bkup_protocol_header_t *hdr = reinterpret_cast<bkup_protocol_header_t *>(rcv_buf);
1005 // LCOV_EXCL_BR_START 11:unexpected branch
1006 pthread_rwlock_t *rwlock = BkupGetRwlock(hdr->item_name, current_media, query_result);
1007 // LCOV_EXCL_BR_STOP 11:unexpected branch
1008 char *full_path = reinterpret_cast<char *>(MAP_FAILED);
1009 char *buf = reinterpret_cast<char *>(MAP_FAILED);
1012 if (response == NULL) {
1013 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "response is NULL");
1016 if (rcv_buf == NULL) {
1017 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "rcv_buf is NULL");
1020 if (query_result == NULL) {
1021 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "query_result is NULL");
1024 // LCOV_EXCL_BR_START 5: mmap's error case.
1025 if (BkupMakeBkupPathToNvId(query_result->category_name, hdr->item_name, &full_path, FILENAME_MAX) < 0) {
1026 // LCOV_EXCL_BR_STOP 5: mmap's error case.
1027 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
1028 goto exit; // LCOV_EXCL_LINE 5: mmap's error case.
1031 // LCOV_EXCL_BR_START 5: mmap's error case.
1032 if ((buf = BkupAnonMmap(query_result->size)) == MAP_FAILED) {
1033 // LCOV_EXCL_BR_STOP 5: mmap's error case.
1034 // LCOV_EXCL_START 5: mmap's error case.
1035 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
1036 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "mmap:%s", strerror(errno));
1038 // LCOV_EXCL_STOP 5: mmap's error case.
1041 if (hdr->offset != 0 || hdr->size != (uint32_t)query_result->size) {
1045 // LCOV_EXCL_BR_START 11:unexpected branch
1046 ret = BkupCheckRead(full_path, current_media, hdr->command, rcv_buf, buf, NULL, query_result, rwlock);
1047 // LCOV_EXCL_BR_STOP 11:unexpected branch
1054 memset(buf + hdr->offset, hdr->fill_patern, hdr->size);
1055 BkupRwlockWrlock(rwlock, full_path); // LCOV_EXCL_BR_LINE 11:unexpected branch
1056 if (BkupBufferWriteToNv(current_media, full_path, buf, query_result->size, &crc32) < 0) {
1057 BkupRwlockUnlock(rwlock, full_path); // LCOV_EXCL_BR_LINE 11:unexpected branch
1060 BkupRwlockUnlock(rwlock, full_path); // LCOV_EXCL_BR_LINE 11:unexpected branch
1066 EFrameworkunifiedStatus e_status = ret < 0 ? eFrameworkunifiedStatusFail : eFrameworkunifiedStatusOK;
1067 if (current_media == NVHALMEDIA_NAND) {
1068 if (query_result->cache_dram == false && query_result->backup_dram == false) {
1069 // LCOV_EXCL_BR_START 11:unexpected branch
1070 BkupApilog(response, current_media, hdr, query_result, buf, crc32, e_status);
1071 // LCOV_EXCL_BR_STOP 11:unexpected branch
1073 if ((*response->set_response)(response->opt, NULL, 0, e_status) < 0) { // LCOV_EXCL_BR_LINE 4: NSFW error case
1074 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
1075 ret = -1; // LCOV_EXCL_LINE 4: NSFW error case
1078 // LCOV_EXCL_BR_START 11:unexpected branch
1079 BkupApilog(response, current_media, hdr, query_result, buf, crc32, e_status);
1080 // LCOV_EXCL_BR_STOP 11:unexpected branch
1083 if (buf != MAP_FAILED) { // LCOV_EXCL_BR_LINE 5: mmap's error case.
1084 munmap(buf, query_result->size);
1086 if (full_path != MAP_FAILED) { // LCOV_EXCL_BR_LINE 5: mmap's error case.
1087 munmap(full_path, FILENAME_MAX);
1095 static int BkupCmdCopy(bkup_response_t *response, enum NvHalMedia current_media,
1096 char *rcv_buf, bkup_query_result_t *query_result) {
1097 if (rcv_buf == NULL) {
1098 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "rcv_buf is NULL");
1101 if (query_result == NULL) {
1102 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "query_result is NULL");
1105 bkup_protocol_header_t *hdr =
1106 reinterpret_cast<bkup_protocol_header_t *>(rcv_buf);
1107 pthread_rwlock_t *src_rwlock =
1108 BkupGetRwlock(hdr->item_name, hdr->src_media, query_result);
1109 pthread_rwlock_t *dst_rwlock =
1110 BkupGetRwlock(hdr->item_name, current_media, query_result);
1112 int ret = BkupMediaCopyFromNv(query_result->category_name, hdr->item_name, hdr->src_media, current_media, src_rwlock,
1115 if (response == NULL) {
1116 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "response is NULL");
1120 if (current_media == NVHALMEDIA_NAND) {
1121 // LCOV_EXCL_BR_START 4: NSFW error case
1122 if ((*response->set_response)(response->opt, NULL, 0, ret < 0 ? eFrameworkunifiedStatusFail : eFrameworkunifiedStatusOK) < 0) {
1123 // LCOV_EXCL_BR_STOP 4: NSFW error case
1124 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
1125 ret = -1; // LCOV_EXCL_LINE 4: NSFW error case
1134 static int BkupCmdCheck(bkup_response_t *response,
1135 enum NvHalMedia current_media, char *rcv_buf,
1136 bkup_query_result_t *query_result) {
1138 bkup_protocol_header_t *hdr = reinterpret_cast<bkup_protocol_header_t *>(rcv_buf);
1139 char *full_path = reinterpret_cast<char *>(MAP_FAILED);
1141 if (rcv_buf == NULL) {
1142 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "rcv_buf is NULL");
1145 if (query_result == NULL) {
1146 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "query_result is NULL");
1149 // LCOV_EXCL_BR_START 5: mmap's error case.
1150 if (BkupMakeBkupPathToNvId(query_result->category_name, hdr->item_name, &full_path, FILENAME_MAX) < 0) {
1151 // LCOV_EXCL_BR_STOP 5: mmap's error case.
1152 // LCOV_EXCL_START 5: mmap's error case.
1153 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
1156 // LCOV_EXCL_STOP 5: mmap's error case.
1158 ret = BkupCheckStatFromNv(current_media, full_path, query_result->size); // LCOV_EXCL_BR_LINE 11:unexpected branch
1161 if (full_path != MAP_FAILED) { // LCOV_EXCL_BR_LINE 5: mmap's error case.
1162 munmap(full_path, FILENAME_MAX);
1170 static int BkupCmdDeleteData(bkup_response_t* const response,
1171 const enum NvHalMedia current_media,
1172 char* const rcv_buf,
1173 bkup_query_result_t* const query_result) {
1175 char *full_path = NULL;
1176 bkup_protocol_header_t* hdr = reinterpret_cast<bkup_protocol_header_t*>(NULL);
1177 pthread_rwlock_t* rwlock = reinterpret_cast<pthread_rwlock_t*>(NULL);
1178 if ((response == NULL) || (rcv_buf == NULL) || (query_result == NULL)) { // LCOV_EXCL_BR_LINE 6:Excluded as no NULL is currently passed
1179 // LCOV_EXCL_START 6:Excluded as no NULL is currently passed
1180 AGL_ASSERT_NOT_TESTED();
1181 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "Resp or Buf or Query is NULL");
1183 // LCOV_EXCL_STOP 6:Excluded as no NULL is currently passed
1185 hdr = reinterpret_cast<bkup_protocol_header_t *>(rcv_buf);
1186 rwlock = BkupGetRwlock(hdr->item_name, current_media, query_result);
1187 if (BkupMakeBkupPathToNvId(query_result->category_name, hdr->item_name, &full_path, FILENAME_MAX) < 0) { // LCOV_EXCL_BR_LINE 6:Excluded due to checking by upper-level function
1188 // LCOV_EXCL_START 6:Excluded due to checking by upper-level function
1189 AGL_ASSERT_NOT_TESTED();
1191 // LCOV_EXCL_STOP 6:Excluded due to checking by upper-level function
1193 BkupRwlockWrlock(rwlock, full_path);
1194 ret = BkupDeleteDataFromNv(current_media, full_path);
1196 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "BkupDeleteDataFromNv Error");
1198 BkupRwlockUnlock(rwlock, full_path);
1201 if ((response != NULL) && (query_result != NULL) && (hdr != NULL)) { // LCOV_EXCL_BR_LINE 6:Excluded as no NULL is currently passed
1202 EFrameworkunifiedStatus e_status = (ret < 0) ? eFrameworkunifiedStatusFail : eFrameworkunifiedStatusOK;
1203 if (current_media == NVHALMEDIA_NAND) {
1204 if ((query_result->cache_dram == false) && (query_result->backup_dram == false)) {
1205 BkupApilog(response, current_media, hdr, query_result, NULL, 0, e_status);
1207 if ((*response->set_response)(response->opt, NULL, 0, e_status) < 0) {
1208 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "Response Error");
1212 BkupApilog(response, current_media, hdr, query_result, NULL, 0, e_status);
1216 if (full_path != NULL) { // LCOV_EXCL_BR_LINE 6:Excluded because checked by BkupMakeBkupPathToNvId()
1217 munmap(full_path, FILENAME_MAX);
1223 * common file handler
1225 static int BkupHandlerFile(bkup_response_t *response,
1226 enum NvHalMedia current_media, char *rcv_buf,
1227 bkup_query_result_t *query_result) {
1229 bkup_protocol_header_t *hdr = reinterpret_cast<bkup_protocol_header_t *>(rcv_buf);
1231 if (rcv_buf == NULL) {
1232 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "rcv_buf is NULL");
1236 switch (hdr->command) { // LCOV_EXCL_BR_LINE 6:double check
1238 ret = BkupCmdRead(response, current_media, rcv_buf, query_result);
1241 case BKUP_CMD_WRITE:
1242 ret = BkupCmdWrite(response, current_media, rcv_buf, query_result);
1246 ret = BkupCmdFill(response, current_media, rcv_buf, query_result);
1249 case BKUP_CMD_COPY_INNER:
1250 ret = BkupCmdCopy(response, current_media, rcv_buf, query_result);
1253 case BKUP_CMD_DELETE:
1254 ret = BkupCmdDeleteData(response, current_media, rcv_buf, query_result);
1258 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
1259 break; // LCOV_EXCL_LINE 6:double check
1267 static int BkupSetResponseNsfw(void *opt, void *buf, int size, EFrameworkunifiedStatus ret_value) {
1268 EFrameworkunifiedStatus e_status;
1270 if (buf != NULL) { // LCOV_EXCL_BR_LINE 6:double check
1271 // LCOV_EXCL_BR_START 4: NSFW error case
1272 if ((e_status = FrameworkunifiedSetSyncResponseData((HANDLE)opt, buf, size)) != eFrameworkunifiedStatusOK) {
1273 // LCOV_EXCL_BR_STOP 4: NSFW error case
1274 // LCOV_EXCL_START 4: NSFW error case
1275 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
1276 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "FrameworkunifiedSetSyncResponseData:%d", e_status);
1278 // LCOV_EXCL_STOP 4: NSFW error case
1287 static void BkupApilog(bkup_response_t *response, enum NvHalMedia current_media,
1288 bkup_protocol_header_t *hdr,
1289 bkup_query_result_t *query_result, char *buf,
1290 uint32_t crc32, EFrameworkunifiedStatus ret_value) {
1293 char header[12] = "-- -- -- --";
1294 const char c[] = "0123456789abcdef";
1296 if (response == NULL) {
1297 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "response is NULL");
1301 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "hdr is NULL");
1304 if (query_result == NULL) {
1305 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "query_result is NULL");
1308 if (response->set_response == BkupSetResponseNsfw) {
1309 src = FrameworkunifiedGetMsgSrc((HANDLE)response->opt); // LCOV_EXCL_BR_LINE 11:unexpected branch
1311 // LCOV_EXCL_BR_START 11:unexpected branch
1312 src = (reinterpret_cast<bkup_itc_message_t *>(response->opt))->nand.client_name;
1313 // LCOV_EXCL_BR_STOP 11:unexpected branch
1316 switch (hdr->command) { // LCOV_EXCL_BR_LINE 6:double check
1320 case BKUP_CMD_WRITE:
1326 case BKUP_CMD_READ_NUM:
1327 // LCOV_EXCL_START 6:double check
1328 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
1331 // LCOV_EXCL_STOP 6:double check
1332 case BKUP_CMD_SIZE_NUM:
1333 // LCOV_EXCL_START 6:double check
1334 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
1337 // LCOV_EXCL_STOP 6:double check
1338 case BKUP_CMD_DELETE:
1342 // LCOV_EXCL_START 6:double check
1343 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
1346 // LCOV_EXCL_STOP 6:double check
1349 if (buf) { // LCOV_EXCL_BR_LINE 6:double check
1351 for (i = 0; i < 4; i++) {
1352 if (query_result->size > i) {
1353 header[i * 3] = c[*(buf + i) >> 4];
1354 header[i * 3 + 1] = c[*(buf + i) & 0xf];
1359 // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
1360 FRAMEWORKUNIFIEDLOG(ZONE_ALSA, "", "%s/%s/%s/o:%d/s:%d/h:%s/c:%x/r:%d", src, cmd, hdr->item_name, hdr->offset, hdr->size,
1361 header, crc32, ret_value);
1362 // LCOV_EXCL_BR_STOP 15:marco defined in "native_service/ns_logger_if.h"
1368 static int BkupTweakCommand(enum NvHalMedia current_media, char **rcv_buf,
1369 int *rcv_size, bkup_query_result_t *query_result) {
1371 bkup_protocol_header_t update_hdr;
1372 bkup_protocol_header_t *hdr = reinterpret_cast<bkup_protocol_header_t *>(*rcv_buf);
1374 if (current_media == NVHALMEDIA_NAND) { // LCOV_EXCL_BR_LINE 6:current_media never will be NVHALMEDIA_NAND
1375 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
1376 ret = 0; // LCOV_EXCL_LINE 6:current_media never will be NVHALMEDIA_NAND
1377 goto exit; // LCOV_EXCL_LINE 6:current_media never will be NVHALMEDIA_NAND
1379 if (rcv_size == NULL) {
1380 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "rcv_size is NULL");
1383 if (query_result == NULL) {
1384 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "query_resul is NULL");
1388 if ((hdr->command == BKUP_CMD_WRITE || hdr->command == BKUP_CMD_FILL) &&
1389 (hdr->offset != 0 || hdr->size != (uint32_t)query_result->size)) {
1391 if ((current_media == NVHALMEDIA_CACHEDRAM &&
1392 (query_result->nand || query_result->backup_dram)) ||
1393 (current_media == NVHALMEDIA_BACKUPDRAM && query_result->nand)) {
1394 memset(&update_hdr, 0, sizeof(update_hdr));
1395 BkupStrlcpy(update_hdr.item_name, hdr->item_name, sizeof(update_hdr.item_name));
1396 update_hdr.command = BKUP_CMD_COPY_INNER;
1397 update_hdr.src_media = current_media;
1398 munmap(*rcv_buf, *rcv_size);
1400 // LCOV_EXCL_BR_START 5: mmap's error case.
1401 if ((*rcv_buf = BkupAnonMmap(sizeof(bkup_protocol_header_t))) == MAP_FAILED) {
1402 // LCOV_EXCL_BR_STOP 5: mmap's error case.
1403 // LCOV_EXCL_START 5: mmap's error case.
1404 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
1405 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "mmap:%s", strerror(errno));
1407 // LCOV_EXCL_STOP 5: mmap's error case.
1409 *rcv_size = sizeof(bkup_protocol_header_t);
1410 memcpy(*rcv_buf, &update_hdr, sizeof(update_hdr));
1420 * Main callback handler
1422 EFrameworkunifiedStatus BkupHandler(HANDLE h_app) {
1423 EFrameworkunifiedStatus e_status = eFrameworkunifiedStatusOK;
1424 uint32_t rcv_size = 0;
1425 char *rcv_buf = NULL;
1426 bkup_protocol_header_t *hdr;
1427 bkup_query_result_t query_result;
1428 bool hold_rcv_buf = false;
1430 bkup_response_t response_nsfw = {BkupSetResponseNsfw, h_app};
1432 if ((rcv_size = FrameworkunifiedGetMsgLength(h_app)) < sizeof(bkup_protocol_header_t)) { // LCOV_EXCL_BR_LINE 4: NSFW error case.
1433 // LCOV_EXCL_START 4: NSFW error case.
1434 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
1435 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "FrameworkunifiedGetMsgLength: size fail:%d", rcv_size);
1436 e_status = eFrameworkunifiedStatusFail;
1438 // LCOV_EXCL_STOP 4: NSFW error case.
1441 rcv_buf = reinterpret_cast<char *>(mmap(NULL, rcv_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0));
1442 if (rcv_buf == MAP_FAILED) { // LCOV_EXCL_BR_LINE 5: mmap's error case.
1443 // LCOV_EXCL_START 5: mmap's error case.
1444 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
1446 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "mmap:%s", strerror(errno));
1447 e_status = eFrameworkunifiedStatusInvldParam;
1449 // LCOV_EXCL_STOP 5: mmap's error case.
1452 // LCOV_EXCL_BR_START 4: NSFW error case.
1453 if ((e_status = FrameworkunifiedGetMsgDataOfSize(h_app, rcv_buf, rcv_size, eSMRRelease)) != eFrameworkunifiedStatusOK) {
1454 // LCOV_EXCL_BR_STOP 4: NSFW error case.
1455 // LCOV_EXCL_START 4: NSFW error case.
1456 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
1457 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "FrameworkunifiedGetMsgDataOfSize:%d", e_status);
1459 // LCOV_EXCL_STOP 4: NSFW error case.
1462 hdr = reinterpret_cast<bkup_protocol_header_t *>(rcv_buf);
1463 if (hdr->command < 0 || hdr->command >= BKUP_CMD_MAX) { // LCOV_EXCL_BR_LINE 6:double check
1464 // LCOV_EXCL_START 6:double check
1465 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
1466 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "Invalid command:%d", hdr->command);
1467 e_status = eFrameworkunifiedStatusInvldParam;
1469 // LCOV_EXCL_STOP 6:double check
1472 if (hdr->command == BKUP_CMD_READ_NUM || hdr->command == BKUP_CMD_SIZE_NUM) {
1473 // LCOV_EXCL_BR_START 11:unexpected branch
1474 if (BkupParamGetNumid(hdr->num_id, &query_result, hdr->item_name, sizeof(hdr->item_name)) < 0) {
1475 // LCOV_EXCL_BR_STOP 11:unexpected branch
1476 // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
1477 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "Invalid num_id:%d", hdr->num_id);
1478 // LCOV_EXCL_BR_STOP 15:marco defined in "native_service/ns_logger_if.h"
1479 e_status = eFrameworkunifiedStatusInvldParam;
1483 if (hdr->command == BKUP_CMD_READ_NUM) {
1484 hdr->command = BKUP_CMD_READ;
1487 if (BkupParamGet(hdr->item_name, &query_result) < 0) { // LCOV_EXCL_BR_LINE 11:unexpected branch
1488 // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
1489 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "Invalid item_name:%s", hdr->item_name != 0 ? hdr->item_name : NULL);
1490 // LCOV_EXCL_BR_STOP 15:marco defined in "native_service/ns_logger_if.h"
1491 e_status = eFrameworkunifiedStatusInvldParam;
1496 if (hdr->command == BKUP_CMD_READ || hdr->command == BKUP_CMD_WRITE || hdr->command == BKUP_CMD_FILL) {
1497 if (hdr->size == 0) {
1501 if (hdr->size + hdr->offset > (uint32_t)query_result.size) {
1502 // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
1503 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "Invalid size+offset:%d+%d", hdr->size, hdr->offset);
1504 // LCOV_EXCL_BR_STOP 15:marco defined in "native_service/ns_logger_if.h"
1505 e_status = eFrameworkunifiedStatusInvldParam;
1510 // LCOV_EXCL_BR_START 6: g_bkup_internal_state will not be BKUP_STATE_INIT
1511 if (g_bkup_internal_state == BKUP_STATE_INIT) {
1512 // LCOV_EXCL_BR_STOP 6: g_bkup_internal_state will not be BKUP_STATE_INIT
1513 // LCOV_EXCL_START 6: same as above
1514 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
1515 e_status = eFrameworkunifiedStatusErrOther;
1517 // LCOV_EXCL_STOP 6: same as above
1519 // LCOV_EXCL_BR_START 6: g_bkup_internal_state will not be BKUP_STATE_TERM
1520 if (g_bkup_internal_state == BKUP_STATE_TERM) {
1521 // LCOV_EXCL_BR_STOP 6: g_bkup_internal_state will not be BKUP_STATE_TERM
1522 // LCOV_EXCL_START 6: same as above
1523 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
1524 e_status = eFrameworkunifiedStatusExit;
1526 // LCOV_EXCL_STOP 6: same as above
1529 if (hdr->command == BKUP_CMD_SIZE || hdr->command == BKUP_CMD_SIZE_NUM) {
1530 // LCOV_EXCL_BR_START 11:unexpected branch
1531 BkupSetResponseNsfw(h_app, &query_result.size, sizeof(query_result.size), eFrameworkunifiedStatusOK);
1532 // LCOV_EXCL_BR_STOP 11:unexpected branch
1533 } else if (hdr->command == BKUP_CMD_CHECK) {
1534 if (query_result.nand) {
1535 // LCOV_EXCL_BR_START 11:unexpected branch
1536 ret = BkupCmdCheck(&response_nsfw, NVHALMEDIA_NAND, rcv_buf, &query_result);
1537 // LCOV_EXCL_BR_STOP 11:unexpected branch
1538 } else if (query_result.backup_dram) {
1539 // LCOV_EXCL_BR_START 11:unexpected branch
1540 ret = BkupCmdCheck(&response_nsfw, NVHALMEDIA_BACKUPDRAM, rcv_buf, &query_result);
1541 // LCOV_EXCL_BR_STOP 11:unexpected branch
1542 } else if (query_result.cache_dram) {
1543 // LCOV_EXCL_BR_START 11:unexpected branch
1544 ret = BkupCmdCheck(&response_nsfw, NVHALMEDIA_CACHEDRAM, rcv_buf, &query_result);
1545 // LCOV_EXCL_BR_STOP 11:unexpected branch
1552 // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
1553 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "Not exists:%s", hdr->item_name != 0 ? hdr->item_name : NULL);
1554 // LCOV_EXCL_BR_STOP 15:marco defined in "native_service/ns_logger_if.h"
1555 e_status = eFrameworkunifiedStatusFileLoadError;
1556 } else if (ret == -2) {
1558 e_status = eFrameworkunifiedStatusAccessError;
1559 } else if (ret == -3) {
1560 e_status = eFrameworkunifiedStatusFail;
1566 if (query_result.cache_dram) {
1567 // LCOV_EXCL_BR_START 11:unexpected branch
1568 ret = BkupHandlerFile(&response_nsfw, NVHALMEDIA_CACHEDRAM, rcv_buf, &query_result);
1569 // LCOV_EXCL_BR_STOP 11:unexpected branch
1571 if (hdr->command == BKUP_CMD_READ) {
1574 // LCOV_EXCL_BR_START 5: mmap's error case.
1575 if (BkupTweakCommand(NVHALMEDIA_CACHEDRAM, &rcv_buf,
1576 reinterpret_cast<int *>(&rcv_size),
1577 &query_result) < 0) {
1578 // LCOV_EXCL_BR_STOP 5: mmap's error case.
1579 // LCOV_EXCL_START 5: mmap's error case.
1580 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
1581 e_status = eFrameworkunifiedStatusFail;
1583 // LCOV_EXCL_STOP 5: mmap's error case.
1585 hdr = reinterpret_cast<bkup_protocol_header_t *>(rcv_buf);
1592 if (query_result.backup_dram) {
1593 // LCOV_EXCL_BR_START 11:unexpected branch
1594 ret = BkupHandlerFile(&response_nsfw, NVHALMEDIA_BACKUPDRAM, rcv_buf, &query_result);
1595 // LCOV_EXCL_BR_STOP 11:unexpected branch
1597 if (hdr->command == BKUP_CMD_READ) {
1600 // LCOV_EXCL_BR_START 5: mmap's error case.
1601 if (BkupTweakCommand(NVHALMEDIA_BACKUPDRAM, &rcv_buf,
1602 reinterpret_cast<int *>(&rcv_size),
1603 &query_result) < 0) {
1604 // LCOV_EXCL_BR_STOP 5: mmap's error case.
1605 // LCOV_EXCL_START 5: mmap's error case.
1606 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
1607 e_status = eFrameworkunifiedStatusFail;
1609 // LCOV_EXCL_STOP 5: mmap's error case.
1611 hdr = reinterpret_cast<bkup_protocol_header_t *>(rcv_buf);
1618 if (query_result.nand) {
1619 bkup_itc_message_t snd_msg;
1621 if ((hdr->command == BKUP_CMD_READ) || (hdr->command == BKUP_CMD_DELETE) || (query_result.sync) || (ret == 0)) {
1625 const char *client_name;
1626 char system_info[MAX_SYS_INFO_SIZE];
1628 if (g_bkup_nand_mqd == -1) {
1629 // LCOV_EXCL_BR_START 5: mq_open's error case.
1630 if ((g_bkup_nand_mqd = BkupItcMqopen("/" BKUP_THREAD_NAND, O_WRONLY | O_CREAT | O_NONBLOCK | O_CLOEXEC)) < 0) {
1631 // LCOV_EXCL_BR_STOP 5: mq_open's error case.
1632 // LCOV_EXCL_START 5: mq_open's error case.
1633 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
1634 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "mq_open:%s", strerror(errno));
1636 // LCOV_EXCL_STOP 5: mq_open's error case.
1640 if ((client_name = FrameworkunifiedGetMsgSrc(h_app)) == NULL) { // LCOV_EXCL_BR_LINE 4: NSFW error case.
1641 // LCOV_EXCL_START 4: NSFW error case.
1642 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
1643 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "invalid client_name");
1644 e_status = eFrameworkunifiedStatusFail;
1646 // LCOV_EXCL_STOP 4: NSFW error case.
1648 FrameworkunifiedGetSystemInfo(h_app, system_info); // LCOV_EXCL_BR_LINE 11:unexpected branch.
1650 memset(&snd_msg, 0, sizeof(snd_msg));
1651 BkupStrlcpy(snd_msg.nand.client_name, client_name, sizeof(snd_msg.nand.client_name));
1652 snd_msg.nand.seq_id = static_cast<uint32_t>(strtoul(system_info, NULL, 16));
1653 snd_msg.nand.rcv_buf = rcv_buf;
1654 snd_msg.nand.rcv_size = rcv_size;
1656 // LCOV_EXCL_BR_START 5: mq_send's error case.
1657 if (mq_send(g_bkup_nand_mqd, reinterpret_cast<char *>(&snd_msg), sizeof(snd_msg), 0) < 0) {
1658 // LCOV_EXCL_BR_STOP 5: mq_send's error case.
1659 // LCOV_EXCL_START 5: mq_send's error case.
1660 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
1661 int save_errno = errno;
1662 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "mq_send:%s", strerror(errno));
1663 e_status = eFrameworkunifiedStatusFail;
1664 if (save_errno == EAGAIN) {
1665 kill(g_bkup_nand_tid, SIGABRT);
1668 // LCOV_EXCL_STOP 5: mq_send's error case.
1670 FrameworkunifiedSetDeferredSyncResponse(h_app); // LCOV_EXCL_BR_LINE 11:unexpected branch.
1671 hold_rcv_buf = true;
1676 if (g_bkup_delay_mqd == -1) {
1677 // LCOV_EXCL_BR_START 5: mq_open's error case.
1678 if ((g_bkup_delay_mqd = BkupItcMqopen("/" BKUP_THREAD_DELAY, O_WRONLY | O_CREAT | O_NONBLOCK | O_CLOEXEC)) < 0) {
1679 // LCOV_EXCL_BR_STOP 5: mq_open's error case.
1680 // LCOV_EXCL_START 5: mq_open's error case.
1681 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
1682 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "mq_open:%s", strerror(errno));
1684 // LCOV_EXCL_STOP 5: mq_open's error case.
1688 memset(&snd_msg, 0, sizeof(snd_msg));
1690 // snd_msg.delay.terminate = false;
1691 snd_msg.delay.cmd = BKUP_DELAY_CMD_REGIST;
1693 BkupStrlcpy(snd_msg.delay.item_name, hdr->item_name, sizeof(snd_msg.delay.item_name));
1695 // LCOV_EXCL_BR_START 5: mq_send's error case.
1696 if (mq_send(g_bkup_delay_mqd, reinterpret_cast<char *>(&snd_msg), sizeof(snd_msg), 0) < 0) {
1697 // LCOV_EXCL_BR_STOP 5: mq_send's error case.
1698 // LCOV_EXCL_START 5: mq_send's error case.
1699 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
1700 int save_errno = errno;
1701 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "mq_send:%s", strerror(errno));
1702 e_status = eFrameworkunifiedStatusFail;
1703 if (save_errno == EAGAIN) {
1704 kill(g_bkup_delay_tid, SIGABRT);
1707 // LCOV_EXCL_STOP 5: mq_send's error case.
1714 if (hold_rcv_buf == false && rcv_buf != MAP_FAILED) { // LCOV_EXCL_BR_LINE 5: mmap's error case
1715 munmap(rcv_buf, rcv_size);
1722 * Power callback handler
1724 EFrameworkunifiedStatus bkup_power_handler(HANDLE hApp) {
1725 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
1726 uint32_t rcv_size = 0;
1727 T_SS_SM_UserModeOnOffNotification_StructType power_status;
1729 if ((rcv_size = FrameworkunifiedGetMsgLength(hApp)) != sizeof(power_status)) { // LCOV_EXCL_BR_START 4: NSFW error case
1730 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
1731 // LCOV_EXCL_START 4: NSFW error case
1732 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "FrameworkunifiedGetMsgLength: size fail:%d", rcv_size);
1733 eStatus = eFrameworkunifiedStatusFail;
1735 // LCOV_EXCL_STOP 4: NSFW error case
1738 // LCOV_EXCL_BR_START 4: NSFW error case
1739 if ((eStatus = FrameworkunifiedGetMsgDataOfSize(hApp, &power_status, rcv_size, eSMRRelease)) != eFrameworkunifiedStatusOK) {
1740 // LCOV_EXCL_BR_STOP 4: NSFW error case
1741 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
1742 // LCOV_EXCL_START 4: NSFW error case
1743 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "FrameworkunifiedGetMsgDataOfSize:%d", eStatus);
1745 // LCOV_EXCL_STOP 4: NSFW error case
1748 if (g_bkup_internal_state != BKUP_STATE_NORMAL) { // LCOV_EXCL_BR_LINE 6:double check
1749 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
1750 // LCOV_EXCL_START 6:double check
1751 FRAMEWORKUNIFIEDLOG(ZONE_WARN, __func__, "Invalid state:%d", g_bkup_internal_state);
1753 // LCOV_EXCL_STOP 6:double check
1756 if (power_status.isUserModeOn == FALSE) {
1757 bkup_itc_message_t snd_msg;
1759 if (g_bkup_delay_mqd == -1) {
1760 // LCOV_EXCL_BR_START 5: mq_open's error case.
1761 if ((g_bkup_delay_mqd = BkupItcMqopen("/" BKUP_THREAD_DELAY, O_WRONLY|O_CREAT|O_NONBLOCK|O_CLOEXEC)) < 0) {
1762 // LCOV_EXCL_BR_STOP 5: mq_open's error case.
1763 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
1764 // LCOV_EXCL_START 5: mq_open's error case.
1765 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "mq_open:%s", strerror(errno));
1766 eStatus = eFrameworkunifiedStatusFail;
1768 // LCOV_EXCL_STOP 5: mq_open's error case.
1772 memset(&snd_msg, 0, sizeof(snd_msg));
1773 snd_msg.delay.cmd = BKUP_DELAY_CMD_PRE_ACCOFF;
1775 // LCOV_EXCL_BR_START 5: mq_send's error case.
1776 if (mq_send(g_bkup_delay_mqd, (char *)&snd_msg, sizeof(snd_msg), 0) < 0) {
1777 // LCOV_EXCL_BR_START 5: mq_send's error case.
1778 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
1779 // LCOV_EXCL_START 5: mq_send's error case.
1780 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "mq_send:%s", strerror(errno));
1781 eStatus = eFrameworkunifiedStatusFail;
1783 // LCOV_EXCL_STOP 5: mq_send's error case.
1795 static int BkupSetResponseThread(void *opt, void *buf, int size, EFrameworkunifiedStatus ret_value) {
1797 char invoker_name[MAX_QUEUE_NAME_SIZE];
1799 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "opt is NULL");
1802 bkup_itc_message_t *msg = reinterpret_cast<bkup_itc_message_t *>(opt);
1803 EFrameworkunifiedStatus e_status;
1804 // LCOV_EXCL_BR_START 11:unexpected branch.
1805 McCreateInvokerName(msg->nand.client_name, 0, invoker_name, sizeof(invoker_name));
1806 // LCOV_EXCL_BR_STOP 11:unexpected branch.
1808 if (g_bkup_response_mq == NULL) {
1809 g_bkup_response_mq = new g_bkup_response_mq_t; // LCOV_EXCL_BR_LINE 11:except,C++ operator
1811 g_bkup_response_mq_t::iterator mq_it = g_bkup_response_mq->find(invoker_name); // LCOV_EXCL_BR_LINE 11:except,C++ STL
1812 if (mq_it != g_bkup_response_mq->end()) { // LCOV_EXCL_BR_LINE 11:except,C++ STL
1813 response_mq = mq_it->second; // LCOV_EXCL_BR_LINE 11:except,C++ STL
1815 if ((response_mq = McOpenSyncSender(invoker_name)) == NULL) { // LCOV_EXCL_BR_LINE 4: NSFW error case.
1816 // LCOV_EXCL_START 4: NSFW error case.
1817 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
1818 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "mcOpenSyncSender");
1820 // LCOV_EXCL_STOP 4: NSFW error case.
1822 g_bkup_response_mq->insert(std::make_pair(invoker_name, response_mq)); // LCOV_EXCL_BR_LINE 11:except,C++ STL
1825 // LCOV_EXCL_BR_START 11:unexpected branch.
1826 e_status = McSendSyncResponse(response_mq, MN_NS_BACKUPMGR, BACKUP_CID, msg->nand.seq_id, ret_value, size, buf);
1827 // LCOV_EXCL_BR_STOP 11:unexpected branch.
1828 return (e_status == eFrameworkunifiedStatusOK) ? 0 : -1; // LCOV_EXCL_BR_LINE 11:unexpected branch
1834 void *BkupNandThread(void *arg) {
1836 struct pollfd fds[1];
1837 bkup_itc_message_t rcv_msg;
1838 bkup_protocol_header_t *hdr;
1839 bkup_query_result_t query_result;
1840 bkup_response_t response_thread = {BkupSetResponseThread, NULL};
1842 signal_handler p_signal = SignalHandlerFuncForMsgHandleThread;
1843 signal(SIGUSR1, p_signal);
1844 g_nand_thread_id = pthread_self();
1846 if (prctl(PR_SET_NAME, BKUP_THREAD_NAND) < 0) { // LCOV_EXCL_BR_LINE 5: prctl's error case.
1847 // LCOV_EXCL_START 5: prctl's error case.
1848 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
1849 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "prctl(PR_SET_NAME):%s", strerror(errno));
1851 // LCOV_EXCL_STOP 5: prctl's error case.
1854 // LCOV_EXCL_BR_START 5: mq_open's error case.
1855 if ((mqd = BkupItcMqopen("/" BKUP_THREAD_NAND, O_RDONLY | O_CREAT | O_CLOEXEC)) < -1) {
1856 // LCOV_EXCL_BR_STOP 5: mq_open's error case.
1857 // LCOV_EXCL_START 5: mq_open's error case.
1858 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
1859 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "mq_open:%s", strerror(errno));
1861 // LCOV_EXCL_STOP 5: mq_open's error case.
1863 g_bkup_nand_tid = static_cast<int>(syscall(__NR_gettid));
1866 fds[0].events = POLLIN;
1869 int ret = poll(fds, sizeof(fds) / sizeof(struct pollfd), -1); // LCOV_EXCL_BR_LINE 11:unexpected branch
1870 if (ret < 0) { // LCOV_EXCL_BR_LINE 5: poll's error case.
1871 // LCOV_EXCL_START 5: poll's error case.
1872 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
1873 if (errno == EINTR) {
1876 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "poll errno:%s", strerror(errno));
1877 // LCOV_EXCL_STOP 5: poll's error case.
1879 if ((fds[0].revents & POLLIN) != 0) {
1880 // LCOV_EXCL_BR_START 5: mq_receive's error case.
1881 if (mq_receive(mqd, reinterpret_cast<char *>(&rcv_msg), sizeof(rcv_msg), NULL) < 0) {
1882 // LCOV_EXCL_BR_STOP 5: mq_receive's error case.
1883 // LCOV_EXCL_START 5: mq_receive's error case.
1884 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
1885 if (errno != EINTR) {
1886 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "mq_receive:%s", strerror(errno));
1889 // LCOV_EXCL_STOP 5: mq_receive's error case.
1891 hdr = reinterpret_cast<bkup_protocol_header_t *>(rcv_msg.nand.rcv_buf);
1892 BkupParamGet(hdr->item_name, &query_result); // LCOV_EXCL_BR_LINE 11:unexpected branch
1893 response_thread.opt = &rcv_msg;
1894 // LCOV_EXCL_BR_START 11:unexpected branch
1895 ret = BkupHandlerFile(&response_thread, NVHALMEDIA_NAND, rcv_msg.nand.rcv_buf, &query_result);
1896 // LCOV_EXCL_BR_STOP 11:unexpected branch
1898 munmap(rcv_msg.nand.rcv_buf, rcv_msg.nand.rcv_size);
1907 * delay backup thread
1912 } bkup_delay_t; // LCOV_EXCL_BR_LINE 11:except,C++ STL
1914 static int BkupDelayTimerAdd(int efd, const char *item_name, time_t backup_cycle) {
1916 struct epoll_event ev;
1917 bkup_delay_t *timer_ev = new bkup_delay_t; // LCOV_EXCL_BR_LINE 11:except,C++ operator
1918 struct itimerspec itime;
1920 if ((timer_ev->fd = timerfd_create(CLOCK_MONOTONIC, 0)) < 0) { // LCOV_EXCL_BR_LINE 5: timerfd_create's error case.
1921 // LCOV_EXCL_START 5: timerfd_create's error case.
1922 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
1923 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "timerfd_create:%s", strerror(errno));
1925 // LCOV_EXCL_STOP 5: timerfd_create's error case.
1927 timer_ev->name = item_name; // LCOV_EXCL_BR_LINE 11:except,C++ STL
1929 ev.events = EPOLLIN;
1930 ev.data.ptr = timer_ev;
1931 if (epoll_ctl(efd, EPOLL_CTL_ADD, timer_ev->fd, &ev) < 0) { // LCOV_EXCL_BR_LINE 5: epoll_ctl's error case.
1932 // LCOV_EXCL_START 5: epoll_ctl's error case.
1933 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
1934 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "epoll_ctl:%s", strerror(errno));
1936 // LCOV_EXCL_STOP 5: epoll_ctl's error case.
1939 memset(&itime, 0, sizeof(itime));
1940 itime.it_value.tv_sec = backup_cycle;
1941 itime.it_interval.tv_sec = backup_cycle;
1943 if (timerfd_settime(timer_ev->fd, 0, &itime, NULL) < 0) { // LCOV_EXCL_BR_LINE 5: timerfd_settime's error case.
1944 // LCOV_EXCL_START 5: timerfd_settime's error case.
1945 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
1946 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "timerfd_settime:%s", strerror(errno));
1947 epoll_ctl(efd, EPOLL_CTL_DEL, timer_ev->fd, &ev);
1949 // LCOV_EXCL_STOP 5: timerfd_settime's error case.
1954 if (ret < 0) { // LCOV_EXCL_BR_LINE 5: C API's error case.
1955 // LCOV_EXCL_START 5: C API's error case.
1956 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
1957 if (timer_ev->fd >= 0) {
1958 if (close(timer_ev->fd) < 0) {
1959 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "timerfd_settime:%s", strerror(errno));
1964 // LCOV_EXCL_STOP 5: C API's error case.
1968 static uint64_t *g_bkup_syncreq;
1969 static pthread_rwlock_t g_syncreq_rwlock = PTHREAD_RWLOCK_INITIALIZER;
1970 #define BKUP_SYNCREQ_SIZE (4096)
1971 #define BKUP_SYNCREQ_ID_MAX (BKUP_SYNCREQ_SIZE * 8 - 1)
1972 #define BKUP_SYNCREQ_NAME "syncreq"
1974 static int BkupSyncreqInit(void) {
1978 map = reinterpret_cast<char *>(mmap(NULL, BKUP_SYNCREQ_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0));
1979 if (map == MAP_FAILED) { // LCOV_EXCL_BR_LINE 5: mmap's error case.
1980 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
1981 // LCOV_EXCL_START 5: mmap's error case.
1982 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "mmap failed");
1984 // LCOV_EXCL_STOP 5: mmap's error case.
1986 g_bkup_syncreq = reinterpret_cast<uint64_t *>(map);
1987 memset(g_bkup_syncreq, 0, BKUP_SYNCREQ_SIZE);
1989 pthread_rwlock_wrlock(&g_syncreq_rwlock);
1990 BkupBufferWriteToNv(NVHALMEDIA_BACKUPDRAM, BKUP_SYNCREQ_NAME,
1991 reinterpret_cast<char *>(g_bkup_syncreq),
1992 BKUP_SYNCREQ_SIZE, NULL);
1993 pthread_rwlock_unlock(&g_syncreq_rwlock);
2000 static void BkupSyncreqTerm(void) {
2001 if (g_bkup_syncreq != NULL) { // LCOV_EXCL_BR_LINE 6:g_bkup_syncreq will not be NULL
2002 munmap(g_bkup_syncreq, BKUP_SYNCREQ_SIZE);
2003 g_bkup_syncreq = NULL;
2007 static int BkupSyncreqSet(int id) {
2008 if (g_bkup_syncreq == NULL) { // LCOV_EXCL_BR_LINE 6:g_bkup_syncreq will not be NULL
2009 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
2010 return -1; // LCOV_EXCL_LINE 5: mmap's error case.
2012 if (id > BKUP_SYNCREQ_ID_MAX) {
2015 if (!(*(g_bkup_syncreq + id / 64) & (1ULL << (id % 64)))) {
2016 *(g_bkup_syncreq + id / 64) |= (1ULL << (id % 64));
2017 pthread_rwlock_wrlock(&g_syncreq_rwlock);
2018 BkupBufferWriteToNv(NVHALMEDIA_BACKUPDRAM, BKUP_SYNCREQ_NAME,
2019 reinterpret_cast<char *>(g_bkup_syncreq),
2020 BKUP_SYNCREQ_SIZE, NULL);
2021 pthread_rwlock_unlock(&g_syncreq_rwlock);
2026 static int BkupSyncreqClear(int id) {
2027 if (g_bkup_syncreq == NULL) { // LCOV_EXCL_BR_LINE 6:g_bkup_syncreq will not be NULL
2028 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
2029 return -1; // LCOV_EXCL_LINE 5: mmap's error case.
2031 if (id > BKUP_SYNCREQ_ID_MAX) {
2034 if (*(g_bkup_syncreq + id / 64) & (1ULL << (id % 64))) {
2035 *(g_bkup_syncreq + id / 64) &= ~(1ULL << (id % 64));
2036 pthread_rwlock_wrlock(&g_syncreq_rwlock);
2037 BkupBufferWriteToNv(NVHALMEDIA_BACKUPDRAM, BKUP_SYNCREQ_NAME,
2038 reinterpret_cast<char *>(g_bkup_syncreq),
2039 BKUP_SYNCREQ_SIZE, NULL);
2040 pthread_rwlock_unlock(&g_syncreq_rwlock);
2046 static inline int BkupPopcnt64(uint64_t x) {
2049 n = (x >> 1) & 0x7777777777777777ULL;
2051 n = (n >> 1) & 0x7777777777777777ULL;
2053 n = (n >> 1) & 0x7777777777777777ULL;
2055 x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
2056 x = x * 0x0101010101010101ULL;
2057 return static_cast<int>(x >> 56);
2060 static int BkupSyncreqSearch(void) {
2063 if (g_bkup_syncreq == NULL) { // LCOV_EXCL_BR_LINE 6:g_bkup_syncreq will not be NULL
2064 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
2065 return -1; // LCOV_EXCL_LINE 5: mmap's error case.
2068 for (i = 0; i < static_cast<int>(BKUP_SYNCREQ_SIZE / sizeof(uint64_t)); i++) {
2069 if (*(g_bkup_syncreq + i) != 0) {
2070 uint64_t bits, mrb1; // most right bit 1
2071 bits = *(g_bkup_syncreq + i);
2072 mrb1 = bits & (-bits);
2073 return i * 64 + BkupPopcnt64(mrb1 - 1);
2080 static void BkupSyncreqDone(bool skip_cache) {
2082 uint64_t start, end;
2084 start = BkupTimerReal();
2086 while ((id = BkupSyncreqSearch()) >= 0) {
2088 bkup_query_result_t query_result;
2090 if (BkupParamGetNumid(id, &query_result, item_name, sizeof(item_name)) < 0) {
2091 // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
2092 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "Invalid num_id:%d", id);
2093 // LCOV_EXCL_BR_STOP 15:marco defined in "native_service/ns_logger_if.h"
2095 if (query_result.cache_dram) {
2097 BkupMediaCopyFromNv(query_result.category_name, item_name,
2098 NVHALMEDIA_CACHEDRAM, NVHALMEDIA_NAND, NULL,
2101 } else if (query_result.backup_dram) {
2102 // LCOV_EXCL_BR_START 11:unexpected branch
2103 BkupMediaCopyFromNv(query_result.category_name, item_name,
2104 NVHALMEDIA_BACKUPDRAM, NVHALMEDIA_NAND, NULL, NULL);
2105 // LCOV_EXCL_BR_STOP 11:unexpected branch
2109 BkupSyncreqClear(id); // LCOV_EXCL_BR_LINE 11:unexpected branch
2113 end = BkupTimerReal();
2114 // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
2115 FRAMEWORKUNIFIEDLOG(ZONE_INFO, __func__, "spent:%" PRIu64 ".%06" PRIu64 "ms",
2116 static_cast<uint64_t>((end - start) / 1000000),
2117 static_cast<uint64_t>((end - start) % 1000000));
2118 // LCOV_EXCL_BR_STOP 15:marco defined in "native_service/ns_logger_if.h"
2121 void BkupSyncreqRestart(void) {
2124 map = reinterpret_cast<char *>(mmap(NULL, BKUP_SYNCREQ_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0));
2125 if (map == MAP_FAILED) { // LCOV_EXCL_BR_LINE 5: mmap's error case.
2126 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
2127 // LCOV_EXCL_START 5: mmap's error case.
2128 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "mmap failed");
2130 // LCOV_EXCL_STOP 5: mmap's error case.
2132 g_bkup_syncreq = reinterpret_cast<uint64_t *>(map);
2134 if (BkupBufferReadFromNv(NVHALMEDIA_BACKUPDRAM, BKUP_SYNCREQ_NAME,
2135 reinterpret_cast<char *>(g_bkup_syncreq),
2136 BKUP_SYNCREQ_SIZE, NULL) < 0) {
2139 BkupSyncreqDone(true);
2146 void *BkupDelayThread(void *arg) {
2149 struct epoll_event ev;
2150 bkup_delay_t *mq_ev;
2151 bkup_itc_message_t rcv_msg;
2152 std::map<std::string, int> delay_req; // LCOV_EXCL_BR_LINE 11:except,C++ STL
2154 signal_handler p_signal = SignalHandlerFuncForMsgHandleThread;
2155 signal(SIGUSR1, p_signal);
2156 g_delay_thread_id = pthread_self();
2159 if (prctl(PR_SET_NAME, BKUP_THREAD_DELAY) < 0) { // LCOV_EXCL_BR_LINE 5: prctl's error case.
2160 // LCOV_EXCL_START 5: prctl's error case.
2161 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
2162 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "prctl(PR_SET_NAME):%s", strerror(errno));
2164 // LCOV_EXCL_STOP 5: prctl's error case.
2167 // LCOV_EXCL_BR_START 5: mq_open's error case.
2168 if ((mqd = BkupItcMqopen("/" BKUP_THREAD_DELAY, O_RDONLY | O_CREAT | O_CLOEXEC)) < 0) {
2169 // LCOV_EXCL_BR_STOP 5: mq_open's error case.
2170 // LCOV_EXCL_START 5: mq_open's error case.
2171 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
2172 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "mq_open:%s", strerror(errno));
2174 // LCOV_EXCL_STOP 5: mq_open's error case.
2177 if ((efd = epoll_create(MAX_FD_EPOLL)) < 0) { // LCOV_EXCL_BR_LINE 5: epoll_create's error case.
2178 // LCOV_EXCL_START 5: epoll_create's error case.
2179 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
2180 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "epoll_create:%s", strerror(errno));
2182 // LCOV_EXCL_STOP 5: epoll_create's error case.
2184 g_bkup_delay_tid = static_cast<int>(syscall(__NR_gettid));
2186 mq_ev = new bkup_delay_t; // LCOV_EXCL_BR_LINE 11:except,C++ operator
2187 mq_ev->fd = static_cast<int>(mqd);
2188 mq_ev->name = "mq"; // LCOV_EXCL_BR_LINE 11:except,C++ STL
2190 ev.events = EPOLLIN;
2191 ev.data.ptr = mq_ev;
2192 // LCOV_EXCL_BR_START 5: epoll_ctl's error case.
2193 if (epoll_ctl(efd, EPOLL_CTL_ADD, static_cast<int>(mqd), &ev) < 0) {
2194 // LCOV_EXCL_BR_STOP 5: epoll_ctl's error case.
2195 // LCOV_EXCL_START 5: epoll_ctl's error case.
2196 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
2197 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "epoll_ctl:%s", strerror(errno));
2199 // LCOV_EXCL_STOP 5: epoll_ctl's error case.
2203 struct epoll_event events[MAX_FD_EPOLL];
2206 nfds = epoll_wait(efd, events, MAX_FD_EPOLL, -1); // LCOV_EXCL_BR_LINE 11:unexpected branch
2207 if (nfds < 0) { // LCOV_EXCL_BR_LINE 5: epoll_wait's error case.
2208 // LCOV_EXCL_START 5: epoll_wait's error case.
2209 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
2210 if (errno != EINTR) {
2211 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "epoll_wait:%s", strerror(errno));
2214 // LCOV_EXCL_STOP 5: epoll_wait's error case.
2217 for (int i = 0; i < nfds; i++) {
2218 bkup_delay_t *cur_ev = reinterpret_cast<bkup_delay_t *>(events[i].data.ptr);
2220 if (cur_ev->fd == mqd) {
2224 // LCOV_EXCL_BR_START 5: mq_receive's error case.
2225 if (mq_receive(mqd, reinterpret_cast<char *>(&rcv_msg), sizeof(rcv_msg), NULL) < 0) {
2226 // LCOV_EXCL_BR_STOP 5: mq_receive's error case.
2227 // LCOV_EXCL_START 5: mq_receive's error case.
2228 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
2229 if (errno != EINTR) {
2230 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "mq_receive:%s", strerror(errno));
2233 // LCOV_EXCL_STOP 5: mq_receive's error case.
2237 if (rcv_msg.delay.terminate) {
2238 // terminate request
2240 BkupSyncreqDone(false); // LCOV_EXCL_BR_LINE 11:unexpected branch
2242 for (std::map<std::string, int>::iterator i = delay_req.begin(); i != delay_req.end(); ++i) {
2243 i->second = 0; // LCOV_EXCL_BR_LINE 11:except,C++ STL
2246 // terminate finish response
2247 int64_t term_event = 1;
2248 // LCOV_EXCL_BR_START 5: write's error case.
2249 if (write(rcv_msg.delay.event_fd, &term_event, sizeof(term_event)) < 0) {
2250 // LCOV_EXCL_BR_STOP 5: write's error case.
2251 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
2252 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "write:%s", strerror(errno)); // LCOV_EXCL_LINE 5: write's error case.
2256 bkup_query_result_t query_result;
2257 BkupParamGet(rcv_msg.delay.item_name, &query_result); // LCOV_EXCL_BR_LINE 11:unexpected branch
2259 if (query_result.backup_cycle > 0) { // LCOV_EXCL_BR_LINE 11:unexpected branch
2260 if (delay_req.find(rcv_msg.delay.item_name) != delay_req.end()) { // LCOV_EXCL_BR_LINE 11:except,C++ STL
2261 delay_req[rcv_msg.delay.item_name]++; // LCOV_EXCL_BR_LINE 11:except,C++ STL
2263 // LCOV_EXCL_BR_START 11:unexpected branch
2264 BkupDelayTimerAdd(efd, rcv_msg.delay.item_name, query_result.backup_cycle);
2265 // LCOV_EXCL_BR_STOP 11:unexpected branch
2266 delay_req[rcv_msg.delay.item_name] = 1; // LCOV_EXCL_BR_LINE 11:unexpected branch
2269 if (BkupSyncreqSet(query_result.id) < 0) {
2270 // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
2271 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "Invalid num_id:%d", query_result.id);
2272 // LCOV_EXCL_BR_STOP 15:marco defined in "native_service/ns_logger_if.h"
2276 bkup_query_result_t query_result;
2277 if (BkupParamGet(rcv_msg.delay.item_name, &query_result) < 0) { // LCOV_EXCL_BR_LINE 11:except,C++ STL
2278 // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
2279 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "Invalid item_name:%s",
2280 rcv_msg.delay.item_name != 0 ? rcv_msg.delay.item_name : NULL);
2281 // LCOV_EXCL_BR_STOP 15:marco defined in "native_service/ns_logger_if.h"
2283 if (BkupSyncreqSet(query_result.id) < 0) {
2284 // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
2285 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "Invalid num_id:%d", query_result.id);
2286 // LCOV_EXCL_BR_STOP 15:marco defined in "native_service/ns_logger_if.h"
2292 switch (rcv_msg.delay.cmd) {
2293 case BKUP_DELAY_CMD_TERM:
2294 case BKUP_DELAY_CMD_PRE_ACCOFF:
2296 * terminate or pre-acc_off request
2299 BkupSyncreqDone(false);
2301 for (std::map<std::string, int>::iterator i = delay_req.begin(); i != delay_req.end(); ++i) {
2305 if (rcv_msg.delay.cmd == BKUP_DELAY_CMD_TERM) {
2308 // terminate finish response
2309 int64_t term_event = 1;
2310 // LCOV_EXCL_BR_START 5: write's error case.
2311 if (write(rcv_msg.delay.event_fd, &term_event, sizeof(term_event)) < 0) {
2312 // LCOV_EXCL_BR_STOP 5: write's error case.
2313 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
2314 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "write:%s", strerror(errno)); // LCOV_EXCL_LINE 5: write's error case.
2319 case BKUP_DELAY_CMD_REGIST:
2320 // delay regist request
2321 bkup_query_result_t query_result;
2322 BkupParamGet(rcv_msg.delay.item_name, &query_result);
2324 if (query_result.backup_cycle > 0) {
2325 if (delay_req.find(rcv_msg.delay.item_name) != delay_req.end()) {
2326 delay_req[rcv_msg.delay.item_name]++;
2328 BkupDelayTimerAdd(efd, rcv_msg.delay.item_name, query_result.backup_cycle);
2329 delay_req[rcv_msg.delay.item_name] = 1;
2332 if (BkupSyncreqSet(query_result.id) < 0) {
2333 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "Invalid num_id:%d", query_result.id);
2337 bkup_query_result_t query_result;
2338 if (BkupParamGet(rcv_msg.delay.item_name, &query_result) < 0) { // LCOV_EXCL_BR_LINE 6:double check
2339 // LCOV_EXCL_START 6:double check
2340 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
2341 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "Invalid item_name:%s", rcv_msg.delay.item_name);
2342 // LCOV_EXCL_STOP 6:double check
2344 if (BkupSyncreqSet(query_result.id) < 0) {
2345 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "Invalid num_id:%d", query_result.id);
2357 if (read(cur_ev->fd, &exp, sizeof(exp)) < 0) { // LCOV_EXCL_BR_LINE 5: read's error case.
2358 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
2359 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "read:%s", strerror(errno)); // LCOV_EXCL_LINE 5: read's error case.
2362 if (delay_req[cur_ev->name] > 0) { // LCOV_EXCL_BR_LINE 11:unexpected branch
2363 bkup_query_result_t query_result;
2364 BkupParamGet(cur_ev->name.c_str(), &query_result); // LCOV_EXCL_BR_LINE 11:unexpected branch
2366 if (query_result.cache_dram) {
2367 // LCOV_EXCL_BR_START 11:unexpected branch
2368 pthread_rwlock_t *src_rwlock = BkupGetRwlock(
2369 cur_ev->name.c_str(), NVHALMEDIA_CACHEDRAM, &query_result);
2370 pthread_rwlock_t *dst_rwlock = BkupGetRwlock(
2371 cur_ev->name.c_str(), NVHALMEDIA_NAND, &query_result);
2372 BkupMediaCopyFromNv(query_result.category_name,
2373 cur_ev->name.c_str(), NVHALMEDIA_CACHEDRAM,
2374 NVHALMEDIA_NAND, src_rwlock, dst_rwlock);
2375 BkupMediaCopyFromNv(query_result.category_name,
2376 cur_ev->name.c_str(), NVHALMEDIA_CACHEDRAM,
2377 NVHALMEDIA_NAND, NULL, NULL);
2378 // LCOV_EXCL_BR_STOP 11:unexpected branch
2379 } else if (query_result.backup_dram) {
2380 // LCOV_EXCL_BR_START 11:unexpected branch
2381 pthread_rwlock_t *src_rwlock = BkupGetRwlock(
2382 cur_ev->name.c_str(), NVHALMEDIA_BACKUPDRAM, &query_result);
2383 pthread_rwlock_t *dst_rwlock = BkupGetRwlock(
2384 cur_ev->name.c_str(), NVHALMEDIA_NAND, &query_result);
2385 BkupMediaCopyFromNv(query_result.category_name,
2386 cur_ev->name.c_str(), NVHALMEDIA_BACKUPDRAM,
2387 NVHALMEDIA_NAND, src_rwlock, dst_rwlock);
2388 // LCOV_EXCL_BR_STOP 11:unexpected branch
2390 delay_req[cur_ev->name] = 0; // LCOV_EXCL_BR_LINE 11:unexpected branch
2391 if (BkupSyncreqClear(query_result.id) < 0) {
2392 // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
2393 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "Invalid num_id:%d", query_result.id);
2394 // LCOV_EXCL_BR_STOP 15:marco defined in "native_service/ns_logger_if.h"
2397 // not update. timer delte
2398 // LCOV_EXCL_BR_START 5: epoll_ctl's error case.
2399 if (epoll_ctl(efd, EPOLL_CTL_DEL, cur_ev->fd, &events[i]) < 0) {
2400 // LCOV_EXCL_BR_STOP 5: epoll_ctl's error case.
2401 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
2402 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "epoll_ctl:%s", strerror(errno)); // LCOV_EXCL_LINE 5: epoll_ctl's error case.
2404 if (close(cur_ev->fd) < 0) { // LCOV_EXCL_BR_LINE 5: close's error case.
2405 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
2406 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "close:%s", strerror(errno)); // LCOV_EXCL_LINE 5: close's error case.
2408 delay_req.erase(cur_ev->name); // LCOV_EXCL_BR_LINE 11:unexpected branch
2409 delete cur_ev; // LCOV_EXCL_BR_LINE 11:except,C++ operator
2421 EFrameworkunifiedStatus BkupTerminateHandler(HANDLE h_app) {
2422 bkup_itc_message_t snd_msg;
2426 g_bkup_internal_state = BKUP_STATE_TERM;
2428 if (g_bkup_delay_mqd == -1) {
2429 // LCOV_EXCL_BR_START 5: mq_open's error case.
2430 if ((g_bkup_delay_mqd = BkupItcMqopen("/" BKUP_THREAD_DELAY, O_WRONLY | O_CREAT | O_NONBLOCK | O_CLOEXEC)) < 0) {
2431 // LCOV_EXCL_BR_STOP 5: mq_open's error case.
2432 // LCOV_EXCL_START 5: mq_open's error case.
2433 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
2434 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "mq_open:%s", strerror(errno));
2435 return eFrameworkunifiedStatusFail;
2436 // LCOV_EXCL_STOP 5: mq_open's error case.
2440 if ((event_fd = eventfd(0, 0)) < 0) { // LCOV_EXCL_BR_LINE 5: eventfd's error case.
2441 // LCOV_EXCL_START 5: eventfd's error case.
2442 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
2443 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "eventfd:%s", strerror(errno));
2444 return eFrameworkunifiedStatusFail;
2445 // LCOV_EXCL_STOP 5: eventfd's error case.
2448 memset(&snd_msg, 0, sizeof(snd_msg));
2450 // snd_msg.delay.terminate = true;
2451 snd_msg.delay.cmd = BKUP_DELAY_CMD_TERM;
2453 snd_msg.delay.event_fd = event_fd;
2455 // LCOV_EXCL_BR_START 5: mq_send's error case.
2456 if (mq_send(g_bkup_delay_mqd, reinterpret_cast<char *>(&snd_msg), sizeof(snd_msg), 0) < 0) {
2457 // LCOV_EXCL_BR_STOP 5: mq_send's error case.
2458 // LCOV_EXCL_START 5: mq_send's error case.
2459 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
2460 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "mq_send:%s", strerror(errno));
2461 return eFrameworkunifiedStatusFail;
2462 // LCOV_EXCL_STOP 5: mq_send's error case.
2466 if (read(event_fd, &term_event, sizeof(term_event)) < 0) { // LCOV_EXCL_BR_LINE 5: read's error case.
2467 // LCOV_EXCL_START 5: read's error case.
2468 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
2469 if (errno == EINTR) {
2472 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "read:%s", strerror(errno));
2473 return eFrameworkunifiedStatusFail;
2474 // LCOV_EXCL_STOP 5: read's error case.
2479 if (close(event_fd) < 0) { // LCOV_EXCL_BR_LINE 5: close's error case.
2480 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
2481 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "close:%s", strerror(errno)); // LCOV_EXCL_LINE 5: close's error case.
2484 return eFrameworkunifiedStatusOK;
2490 EFrameworkunifiedStatus BkupInitHandler(HANDLE h_app) {
2491 BkupSyncreqRestart();
2494 g_bkup_internal_state = BKUP_STATE_NORMAL;
2496 return eFrameworkunifiedStatusOK;