common_library: gettid is multiple declaration in cl_error
[staging/basesystem.git] / video_in_hal / nsframework / backup_manager / client / src / bkup_api.cpp
1 /*
2  * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION.
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <fcntl.h>
18 #include <sys/mman.h>
19 #include <sys/prctl.h>
20 #include <sys/stat.h>
21 #include <sys/types.h>
22 #include <unistd.h>
23 #include <native_service/ns_backup.h>
24 #include <native_service/ns_message_center_if.h>
25 #include <agl_thread.h>
26 #include <cerrno>
27 #include <cstdio>
28 #include <cstdlib>
29 #include <cstring>
30 #include "bkup_api.h"
31 #include "bkup_backupmanagerlog.h"
32 #include "bkup_util.h"
33
34 #define BKUP_RETRY_MAX (10)
35 #define BKUP_RETRY_TIMER (100 * 1000)
36
37 static __thread HANDLE g_bkup_msg_handle;
38 static __thread HANDLE g_bkup_response_handle;
39
40 static int32_t BkupApiCheckTagid(PCSTR tag_id) {
41   if (tag_id == NULL) {
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;
46   }
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;
52   }
53   return BKUP_RET_NORMAL;
54 }
55
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];
61
62   if (g_bkup_msg_handle == NULL) {
63     char invoker_name[24];
64
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));
69       goto exit;
70       // LCOV_EXCL_STOP 5:prctl's error case.
71     }
72
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");
78       goto exit;
79       // LCOV_EXCL_STOP 4: NSFW error case
80     }
81
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;
90       goto exit;
91       // LCOV_EXCL_STOP 4: NSFW error case
92     }
93
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;
101       goto exit;
102       // LCOV_EXCL_STOP 4: NSFW error case
103     }
104   }
105
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
110       break;
111     }
112     usleep(BKUP_RETRY_TIMER);
113   }
114
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;
129     } else {
130       ret = BKUP_RET_ERROR;
131     }
132     goto exit;
133   }
134   if (rcv_size != real_rcvlen) {
135     goto exit;
136   }
137   ret = BKUP_RET_NORMAL;
138
139 exit:
140   return ret;
141 }
142
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;
146
147   if ((ret = BkupApiCheckTagid(tag_id)) != BKUP_RET_NORMAL) {
148     goto exit;
149   }
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;
155     goto exit;
156   }
157
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;
162   hdr.size = ui_size;
163
164   ret = BkupApiCall(&hdr, sizeof(hdr), pv_buf, ui_size);
165
166 exit:
167   return ret;
168 }
169
170 int32_t Backup_DataWt(PCSTR tag_id, void *pv_buf, uint32_t ui_offset, uint32_t ui_size) {
171   int32_t ret;
172   char *snd_buf = NULL;
173   size_t snd_buf_size = 0;
174   bkup_protocol_header_t *hdr;
175
176   if ((ret = BkupApiCheckTagid(tag_id)) != BKUP_RET_NORMAL) {
177     goto exit;
178   }
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;
184     goto exit;
185   }
186
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;
194     goto exit;
195     // LCOV_EXCL_STOP 5:mmap's error case.
196   }
197   snd_buf_size = sizeof(bkup_protocol_header_t) + ui_size;
198
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;
203   hdr->size = ui_size;
204   memcpy(snd_buf + sizeof(bkup_protocol_header_t), pv_buf, ui_size);
205
206   ret = BkupApiCall(snd_buf, snd_buf_size, NULL, 0);
207
208 exit:
209   if (snd_buf) {
210     munmap(snd_buf, snd_buf_size);
211   }
212   return ret;
213 }
214
215 int32_t Backup_DataFil(PCSTR tag_id, uint32_t ui_offset, uint8_t uc_pat, uint32_t ui_size) {
216   int32_t ret;
217   bkup_protocol_header_t hdr;
218
219   if ((ret = BkupApiCheckTagid(tag_id)) != BKUP_RET_NORMAL) {
220     goto exit;
221   }
222
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;
227   hdr.size = ui_size;
228   hdr.fill_patern = uc_pat;
229
230   ret = BkupApiCall(&hdr, sizeof(hdr), NULL, 0);
231
232 exit:
233   return ret;
234 }
235
236 int32_t Backup_DataSz(PCSTR tag_id, uint32_t *pui_size) {
237   int32_t ret;
238   bkup_protocol_header_t hdr;
239
240   if ((ret = BkupApiCheckTagid(tag_id)) != BKUP_RET_NORMAL) {
241     goto exit;
242   }
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;
248     goto exit;
249   }
250
251   memset(&hdr, 0, sizeof(hdr));
252   hdr.command = BKUP_CMD_SIZE;
253   BkupStrlcpy(hdr.item_name, tag_id, sizeof(hdr.item_name));
254
255   ret = BkupApiCall(&hdr, sizeof(hdr), pui_size, sizeof(uint32_t));
256
257 exit:
258   return ret;
259 }
260
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;
264
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;
270     goto exit;
271   }
272
273   memset(&hdr, 0, sizeof(hdr));
274   hdr.command = BKUP_CMD_READ_NUM;
275   hdr.num_id = num_id;
276   hdr.offset = ui_offset;
277   hdr.size = ui_size;
278
279   ret = BkupApiCall(&hdr, sizeof(hdr), pv_buf, ui_size);
280
281 exit:
282   return ret;
283 }
284
285 int32_t Backup_DataSzByNumID(uint32_t num_id, uint32_t *pui_size) {
286   int32_t ret;
287   bkup_protocol_header_t hdr;
288
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;
294     goto exit;
295   }
296
297   memset(&hdr, 0, sizeof(hdr));
298   hdr.command = BKUP_CMD_SIZE_NUM;
299   hdr.num_id = num_id;
300
301   ret = BkupApiCall(&hdr, sizeof(hdr), pui_size, sizeof(uint32_t));
302
303 exit:
304   return ret;
305 }
306
307 int32_t Backup_DataChk(PCSTR tag_id) {
308   int32_t ret;
309   bkup_protocol_header_t hdr;
310
311   if ((ret = BkupApiCheckTagid(tag_id)) != BKUP_RET_NORMAL) {
312     goto exit;
313   }
314
315   memset(&hdr, 0, sizeof(hdr));
316   hdr.command = BKUP_CMD_CHECK;
317   BkupStrlcpy(hdr.item_name, tag_id, sizeof(hdr.item_name));
318
319   ret = BkupApiCall(&hdr, sizeof(hdr), NULL, 0);
320
321 exit:
322   return ret;
323 }
324
325 int32_t Backup_DataDel(PCSTR tag_id) {
326   int32_t ret;
327   bkup_protocol_header_t hdr;
328
329   ret = BkupApiCheckTagid(tag_id);
330   if (ret != BKUP_RET_NORMAL) {
331     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __func__, "tag_id is invalid");
332     goto exit;
333   }
334
335   memset(&hdr, 0, sizeof(hdr));
336   hdr.command = BKUP_CMD_DELETE;
337   BkupStrlcpy(hdr.item_name, tag_id, sizeof(hdr.item_name));
338
339   ret = BkupApiCall(&hdr, sizeof(hdr), NULL, 0);
340
341 exit:
342   return ret;
343 }