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.
19 #include <sys/prctl.h>
21 #include <sys/types.h>
23 #include <native_service/ns_backup.h>
24 #include <native_service/ns_message_center_if.h>
25 #include <agl_thread.h>
31 #include "bkup_backupmanagerlog.h"
32 #include "bkup_util.h"
34 #define BKUP_RETRY_MAX (10)
35 #define BKUP_RETRY_TIMER (100 * 1000)
37 static __thread HANDLE g_bkup_msg_handle;
38 static __thread HANDLE g_bkup_response_handle;
40 static int32_t BkupApiCheckTagid(PCSTR tag_id) {
42 // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
43 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "tag_id is NULL");
44 // LCOV_EXCL_BR_STOP 15:marco defined in "native_service/ns_logger_if.h"
45 return BKUP_RET_ERRPARAM;
47 if (strlen(tag_id) >= BKUP_MAX_ITEM_NAME) {
48 // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
49 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "tag_id is long:%zd", strlen(tag_id));
50 // LCOV_EXCL_BR_STOP 15:marco defined in "native_service/ns_logger_if.h"
51 return BKUP_RET_ERRPARAM;
53 return BKUP_RET_NORMAL;
56 static int32_t BkupApiCall(void *snd_buf, size_t snd_size, void *rcv_buf, uint32_t rcv_size) {
57 int32_t ret = BKUP_RET_ERROR;
58 EFrameworkunifiedStatus e_status = eFrameworkunifiedStatusFail;
59 uint32_t real_rcvlen = 0;
60 static __thread char thread_name[16];
62 if (g_bkup_msg_handle == NULL) {
63 char invoker_name[24];
65 if (prctl(PR_GET_NAME, thread_name) < 0) { // LCOV_EXCL_BR_LINE 5:prctl's error case.
66 // LCOV_EXCL_START 5:prctl's error case.
67 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
68 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "prctl(PR_GET_NAME):%s", strerror(errno));
70 // LCOV_EXCL_STOP 5:prctl's error case.
73 g_bkup_msg_handle = McOpenSender(SERVICE_BACKUP_MANAGER_MSG_HANDLE_THREAD);
74 if (g_bkup_msg_handle == NULL) { // LCOV_EXCL_BR_LINE 4: NSFW error case
75 // LCOV_EXCL_START 4: NSFW error case
76 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
77 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "McOpenSender");
79 // LCOV_EXCL_STOP 4: NSFW error case
82 // LCOV_EXCL_BR_START 4: NSFW error case
83 if ((e_status = McCreateInvokerName(thread_name, 0, invoker_name, sizeof(invoker_name))) != eFrameworkunifiedStatusOK) {
84 // LCOV_EXCL_BR_STOP 4: NSFW error case
85 // LCOV_EXCL_START 4: NSFW error case
86 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
87 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "McCreateInvokerName:%d", e_status);
88 McClose(g_bkup_msg_handle);
89 g_bkup_msg_handle = NULL;
91 // LCOV_EXCL_STOP 4: NSFW error case
94 g_bkup_response_handle = McOpenSyncReceiver(invoker_name);
95 if (g_bkup_response_handle == NULL) { // LCOV_EXCL_BR_LINE 4: NSFW error case
96 // LCOV_EXCL_START 4: NSFW error case
97 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
98 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "McOpenSyncReceiver");
99 McClose(g_bkup_msg_handle);
100 g_bkup_msg_handle = NULL;
102 // LCOV_EXCL_STOP 4: NSFW error case
106 for (int retry_cnt = 0; retry_cnt < BKUP_RETRY_MAX; retry_cnt++) { // LCOV_EXCL_BR_LINE 4: NSFW error case
107 e_status = McInvokeSync(g_bkup_msg_handle, thread_name, BACKUP_CID, static_cast<UI_32>(snd_size), snd_buf, 0,
108 g_bkup_response_handle, rcv_size, rcv_buf, &real_rcvlen);
109 if (e_status != eFrameworkunifiedStatusFail) { // LCOV_EXCL_BR_LINE 4: NSFW error case
112 usleep(BKUP_RETRY_TIMER);
115 if (e_status != eFrameworkunifiedStatusOK) {
116 // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
117 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "McInvokeSync:%d", e_status);
118 // LCOV_EXCL_BR_STOP 15:marco defined in "native_service/ns_logger_if.h"
119 if (e_status == eFrameworkunifiedStatusInvldParam) {
120 ret = BKUP_RET_ERRPARAM;
121 } else if (e_status == eFrameworkunifiedStatusErrOther) {
122 ret = BKUP_RET_ERRINIT;
123 } else if (e_status == eFrameworkunifiedStatusExit) {
124 ret = BKUP_RET_ERRTERM;
125 } else if (e_status == eFrameworkunifiedStatusFileLoadError) {
126 ret = BKUP_RET_ERRNOENT;
127 } else if (e_status == eFrameworkunifiedStatusAccessError) {
128 ret = BKUP_RET_ERRSIZE;
130 ret = BKUP_RET_ERROR;
134 if (rcv_size != real_rcvlen) {
137 ret = BKUP_RET_NORMAL;
143 int32_t Backup_DataRd(PCSTR tag_id, uint32_t ui_offset, void *pv_buf, uint32_t ui_size) {
144 int32_t ret = BKUP_RET_NORMAL;
145 bkup_protocol_header_t hdr;
147 if ((ret = BkupApiCheckTagid(tag_id)) != BKUP_RET_NORMAL) {
150 if (pv_buf == NULL) {
151 // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
152 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "Buf is NULL");
153 // LCOV_EXCL_BR_STOP 15:marco defined in "native_service/ns_logger_if.h"
154 ret = BKUP_RET_ERRPARAM;
158 memset(&hdr, 0, sizeof(hdr));
159 hdr.command = BKUP_CMD_READ;
160 BkupStrlcpy(hdr.item_name, tag_id, sizeof(hdr.item_name));
161 hdr.offset = ui_offset;
164 ret = BkupApiCall(&hdr, sizeof(hdr), pv_buf, ui_size);
170 int32_t Backup_DataWt(PCSTR tag_id, void *pv_buf, uint32_t ui_offset, uint32_t ui_size) {
172 char *snd_buf = NULL;
173 size_t snd_buf_size = 0;
174 bkup_protocol_header_t *hdr;
176 if ((ret = BkupApiCheckTagid(tag_id)) != BKUP_RET_NORMAL) {
179 if (pv_buf == NULL) {
180 // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
181 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "Buf is NULL");
182 // LCOV_EXCL_BR_STOP 15:marco defined in "native_service/ns_logger_if.h"
183 ret = BKUP_RET_ERRPARAM;
187 // LCOV_EXCL_BR_START 5:mmap's error case.
188 if ((snd_buf = BkupAnonMmap(sizeof(bkup_protocol_header_t) + ui_size)) == MAP_FAILED) {
189 // LCOV_EXCL_BR_STOP 5:mmap's error case.
190 // LCOV_EXCL_START 5:mmap's error case.
191 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
192 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "mmap:%s", strerror(errno));
193 ret = BKUP_RET_ERROR;
195 // LCOV_EXCL_STOP 5:mmap's error case.
197 snd_buf_size = sizeof(bkup_protocol_header_t) + ui_size;
199 hdr = reinterpret_cast<bkup_protocol_header_t *>(snd_buf);
200 hdr->command = BKUP_CMD_WRITE;
201 BkupStrlcpy(hdr->item_name, tag_id, sizeof(hdr->item_name));
202 hdr->offset = ui_offset;
204 memcpy(snd_buf + sizeof(bkup_protocol_header_t), pv_buf, ui_size);
206 ret = BkupApiCall(snd_buf, snd_buf_size, NULL, 0);
210 munmap(snd_buf, snd_buf_size);
215 int32_t Backup_DataFil(PCSTR tag_id, uint32_t ui_offset, uint8_t uc_pat, uint32_t ui_size) {
217 bkup_protocol_header_t hdr;
219 if ((ret = BkupApiCheckTagid(tag_id)) != BKUP_RET_NORMAL) {
223 memset(&hdr, 0, sizeof(hdr));
224 hdr.command = BKUP_CMD_FILL;
225 BkupStrlcpy(hdr.item_name, tag_id, sizeof(hdr.item_name));
226 hdr.offset = ui_offset;
228 hdr.fill_patern = uc_pat;
230 ret = BkupApiCall(&hdr, sizeof(hdr), NULL, 0);
236 int32_t Backup_DataSz(PCSTR tag_id, uint32_t *pui_size) {
238 bkup_protocol_header_t hdr;
240 if ((ret = BkupApiCheckTagid(tag_id)) != BKUP_RET_NORMAL) {
243 if (pui_size == NULL) {
244 // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
245 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "Buf is NULL");
246 // LCOV_EXCL_BR_STOP 15:marco defined in "native_service/ns_logger_if.h"
247 ret = BKUP_RET_ERRPARAM;
251 memset(&hdr, 0, sizeof(hdr));
252 hdr.command = BKUP_CMD_SIZE;
253 BkupStrlcpy(hdr.item_name, tag_id, sizeof(hdr.item_name));
255 ret = BkupApiCall(&hdr, sizeof(hdr), pui_size, sizeof(uint32_t));
261 int32_t Backup_DataRdByNumID(uint32_t num_id, uint32_t ui_offset, void *pv_buf, uint32_t ui_size) {
262 int32_t ret = BKUP_RET_NORMAL;
263 bkup_protocol_header_t hdr;
265 if (pv_buf == NULL) {
266 // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
267 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "Buf is NULL");
268 // LCOV_EXCL_BR_STOP 15:marco defined in "native_service/ns_logger_if.h"
269 ret = BKUP_RET_ERRPARAM;
273 memset(&hdr, 0, sizeof(hdr));
274 hdr.command = BKUP_CMD_READ_NUM;
276 hdr.offset = ui_offset;
279 ret = BkupApiCall(&hdr, sizeof(hdr), pv_buf, ui_size);
285 int32_t Backup_DataSzByNumID(uint32_t num_id, uint32_t *pui_size) {
287 bkup_protocol_header_t hdr;
289 if (pui_size == NULL) {
290 // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
291 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "Buf is NULL");
292 // LCOV_EXCL_BR_STOP 15:marco defined in "native_service/ns_logger_if.h"
293 ret = BKUP_RET_ERRPARAM;
297 memset(&hdr, 0, sizeof(hdr));
298 hdr.command = BKUP_CMD_SIZE_NUM;
301 ret = BkupApiCall(&hdr, sizeof(hdr), pui_size, sizeof(uint32_t));
307 int32_t Backup_DataChk(PCSTR tag_id) {
309 bkup_protocol_header_t hdr;
311 if ((ret = BkupApiCheckTagid(tag_id)) != BKUP_RET_NORMAL) {
315 memset(&hdr, 0, sizeof(hdr));
316 hdr.command = BKUP_CMD_CHECK;
317 BkupStrlcpy(hdr.item_name, tag_id, sizeof(hdr.item_name));
319 ret = BkupApiCall(&hdr, sizeof(hdr), NULL, 0);
325 int32_t Backup_DataDel(PCSTR tag_id) {
327 bkup_protocol_header_t hdr;
329 ret = BkupApiCheckTagid(tag_id);
330 if (ret != BKUP_RET_NORMAL) {
331 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "tag_id is invalid");
335 memset(&hdr, 0, sizeof(hdr));
336 hdr.command = BKUP_CMD_DELETE;
337 BkupStrlcpy(hdr.item_name, tag_id, sizeof(hdr.item_name));
339 ret = BkupApiCall(&hdr, sizeof(hdr), NULL, 0);