Init basesystem source codes.
[staging/basesystem.git] / video_in_hal / vehicleservice / positioning_base_library / library / src / MsgBapi.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 /**
18  * @file
19  *    MsgBapi.cpp
20  * @brief system common functions
21   */
22
23 /*---------------------------------------------------------------------------------*
24  * Include Files                                                                                   *
25  *---------------------------------------------------------------------------------*/
26 #include <vehicle_service/positioning_base_library.h>
27
28 #include <fcntl.h>
29 #include <sys/mman.h>
30 #include <dirent.h>
31 #include "_pbInternalProc.h"
32 #include "WPF_STD_private.h"
33
34 /*---------------------------------------------------------------------------------*
35  * Function                                                                                        *
36  *---------------------------------------------------------------------------------*/
37 /**
38  * @brief
39  *   Create Shared Memory
40  *
41  *   Create a file mapping object with the specified shared memory name and size, then <br>
42  *   maps it to the invoking process space. If the shared memory that has already been <br>
43  *   created is specified, an error is returned.<br>
44  *   The shared memory management information allocates a heap area each time it is <br>
45  *   created, and return this address as a shared memory handle.
46  *
47  * @param[in]  TCHAR* name
48  * @param[in]  DWORD size
49  *
50  * @return HANDLE<br>
51  *              Except NULL    Handle of the created shared memory(Management information pointer)<br>
52  *              NULL        ABEND
53  */
54 HANDLE CreateSharedMemory(TCHAR* name, DWORD size) {
55     int32            lret = EOK;
56     RET_API            ret_api = RET_ERROR;
57     int32            fd = POSITIONINGBASELIBRARY_NON_FD;
58     SHARED_MEMORY*    p_shm = NULL;
59     char c_file_path[PATH_MAX + 1] = SHARED_MEMORY_DIRECTORY;
60
61     if ((name != NULL) && (size != 0)) {  // LCOV_EXCL_BR_LINE 6:name can not be NULL, size can not be 0
62         /* Allocate the shared memory management information area in the heap. */
63         p_shm = reinterpret_cast<SHARED_MEMORY *>(malloc(sizeof(SHARED_MEMORY)));
64         if (p_shm == NULL) {  // LCOV_EXCL_BR_LINE 5: malloc's error case
65         } else {
66             p_shm->h_heap = NULL;
67             p_shm->phy_addr = 0;
68             p_shm->h_map = NULL;
69             p_shm->size = size;
70
71             /* Create directory */
72             (void)mkdir(SHARED_MEMORY_DIRECTORY, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
73
74             /* Create shared memory with the specified size and name.(Error if it already exists) */
75             (void)strncat(c_file_path, (const char*)name, 128);
76             fd = open(c_file_path, O_RDWR | O_CREAT | O_EXCL, S_IRWXU | S_IRWXG | S_IRWXO);  // LCOV_EXCL_BR_LINE 5: standard lib error  // NOLINT(whitespace/line_length)
77             if (fd > POSITIONINGBASELIBRARY_NON_FD)   /* Coverity CID: 18775 compliant */ {  // LCOV_EXCL_BR_LINE 5: standard lib error  // NOLINT(whitespace/line_length)
78                 /* Set size */
79                 lret = ftruncate(fd, size);
80                 if (lret == EOK) {  // LCOV_EXCL_BR_LINE 5: ftruncate's error case
81                     /* Map the created shared memory to the invoking process space. */
82                     p_shm->p_memory = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
83                     if (p_shm->p_memory != MAP_FAILED) {  // LCOV_EXCL_BR_LINE 5: standard lib error
84                         ret_api = RET_NORMAL;
85                     }
86                 }
87             }
88         }
89     }
90
91     if (fd > POSITIONINGBASELIBRARY_NON_FD)   /* Coverity CID: 18775 compliant */ {
92         close(fd);
93     }
94
95     if (ret_api == RET_NORMAL) {  // LCOV_EXCL_BR_LINE 200: can not be not normal
96         /* Indicate that it is the creator of the shared memory */
97         p_shm->owner = TRUE;
98     } else {
99         // LCOV_EXCL_START 200: can not be not normal
100         AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
101         /* In case of an error */
102         if (fd != POSITIONINGBASELIBRARY_NON_FD) {
103             /* Delete shared memory object if created */
104             shm_unlink(name);
105         }
106
107         if (p_shm != NULL) {
108             free(p_shm);
109         }
110
111         p_shm = NULL;
112         // LCOV_EXCL_STOP
113     }
114
115     return (HANDLE)p_shm;
116 }
117
118 /**
119  * @brief
120  *   Open Shared Memory
121  *
122  *   Create a file mapping object with the specified shared memory name and size, <br>
123  *   then map it to the invoking process space. Since this is not shared memory <br>
124  *   creation processing, an error is returned if it is not a file mapping object.<br>
125  *   The shared memory management information allocates a heap area each time <br>
126  *   it is created, and return this address as a shared memory handle.
127  *
128  * @param[in]  TCHAR* name
129  * @param[in]  DWORD size
130  *
131  * @return HANDLE<br>
132  *              Except NULL    Handle of the acquired shared memory(Management information pointer)<br>
133  *              NULL        ABEND
134  */
135 HANDLE OpenSharedMemory(TCHAR* name, DWORD size) {
136     RET_API            ret_api = RET_ERROR;
137     int32            fd = POSITIONINGBASELIBRARY_NON_FD;
138     SHARED_MEMORY*    p_shm = NULL;
139     char c_file_path[PATH_MAX + 1] = SHARED_MEMORY_DIRECTORY;
140
141     if ((name != NULL) && (size != 0)) {  // LCOV_EXCL_BR_LINE 6:name can not be NULL, size can not be 0
142         /* Allocate the shared memory management information area in the heap. */
143         p_shm = reinterpret_cast<SHARED_MEMORY *>(malloc(sizeof(SHARED_MEMORY)));
144         if (p_shm == NULL) {  // LCOV_EXCL_BR_LINE 5: malloc's error case
145         } else {
146             p_shm->h_heap = NULL;
147             p_shm->phy_addr = 0;
148             p_shm->h_map = NULL;
149             p_shm->size = size;
150
151             /* Open shared memory with the specified size and name(Error if not exists) */
152             (void)strncat(c_file_path, (const char*)name, 128);
153             fd = open(c_file_path, O_RDWR, S_IRWXU | S_IRWXG | S_IRWXO);  // LCOV_EXCL_BR_LINE 5: standard lib error
154             if (fd > POSITIONINGBASELIBRARY_NON_FD)   /* Coverity CID: 18774 compliant */ {
155                 /* Map open shared memory to invoking process space. */
156                 p_shm->p_memory = reinterpret_cast<SHARED_MEMORY *>(mmap(0, size, PROT_READ | PROT_WRITE, \
157                     MAP_SHARED | POSITIONINGBASELIBRARY_MAP_NON_INIT, fd, 0));
158                 if (p_shm->p_memory != MAP_FAILED) {  // LCOV_EXCL_BR_LINE 5: standard lib error
159                     ret_api = RET_NORMAL;
160                 }
161             }
162         }
163     }
164
165     if (fd > POSITIONINGBASELIBRARY_NON_FD)   /* Coverity CID: 18774 compliant */ {
166         close(fd);
167     }
168
169     if (ret_api == RET_NORMAL) {
170         /* Indicate that the opener of the shared memory not the creator of it. */
171         p_shm->owner = FALSE;
172     } else {
173         /* In case of an error */
174         if (p_shm != NULL) {
175             free(p_shm);
176         }
177
178         p_shm = NULL;
179     }
180
181     return (HANDLE)p_shm;
182 }
183
184 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
185  * MODULE    : DeleteAllSharedMemory()
186  * ABSTRACT  : Delete all shared memory objects
187  * NOTE      : Delete all shared memory objects
188  *           : However, the heap area allocated at the time of shared memory creation or open is not released.
189  * ARGUMENT  : None
190  * RETURN    : None
191  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
192 void
193 DeleteAllSharedMemory(void) {  // LCOV_EXCL_START 8:dead code
194     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
195     int32            lret = EOK;
196     char*            cwd = NULL;
197     char            sz_cwd[PATH_MAX + 1] = {0};
198     DIR*            p_dir = NULL;
199     struct dirent*     p_dir_ent = NULL;
200
201     /* Get path of current directory */
202     cwd = getcwd(sz_cwd, PATH_MAX + 1);
203
204     if (cwd != NULL) {
205         /* Move to shared memory directory */
206         lret = chdir(SHARED_MEMORY_DIRECTORY);
207
208         if (lret == EOK) {
209             /* Open shared memory directory */
210             p_dir = opendir(SHARED_MEMORY_DIRECTORY);
211             if (p_dir != NULL) {
212                 for (;;) {
213                     /* Get directory entry */
214                     p_dir_ent = readdir(p_dir);
215                     if (p_dir_ent == NULL) {
216                         break;
217                     }
218
219                     if ((_pb_strcmp(p_dir_ent->d_name, ".") != 0) &&
220                             (_pb_strcmp(p_dir_ent->d_name, "..") != 0)) {
221                         /* Delete shared memory objects */
222                         shm_unlink(p_dir_ent->d_name);
223                     }
224                 }
225
226                 /* Close shared memory directory */
227                 closedir(p_dir);
228             }
229
230             /* Go to original directory */
231             lret = chdir(cwd);
232             if (lret != EOK) /* Coverity CID: 18816 compliant */ {
233                 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "chdir ERROR [lret:%d]", lret);
234             }
235         }
236     }
237
238     /* Delete shared memory directory */
239     lret = rmdir(SHARED_MEMORY_DIRECTORY);
240
241     return;
242 }
243 // LCOV_EXCL_STOP
244
245 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
246  * MODULE    : GetSharedMemoryPtr()
247  * ABSTRACT  : Shared memory start address acquisition processing
248  * NOTE      : Return the start address of the shared memory that is allocated/mapped at the 
249  *           : time of shared memory creation or open
250  * ARGUMENT  : HANDLE        h_shm        Shared memory handle(Management information pointer)
251  * RETURN    : void*        Except NULL    Pointer to the acquired shared memory
252  *           :                NULL        ABEND
253  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
254 void*
255 GetSharedMemoryPtr(HANDLE h_shm) {
256     void            *p_vret = NULL;
257     SHARED_MEMORY    *p_shm = reinterpret_cast<SHARED_MEMORY *>(h_shm);
258
259     if (p_shm == NULL)      /* If the shared memory handle that is not created or not opened is specified, */ {  // LCOV_EXCL_BR_LINE 200: p_shm can not be NULL  // NOLINT(whitespace/line_length)
260     } else {
261         p_vret = p_shm->p_memory;
262     }
263
264     return p_vret;
265 }
266
267 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
268  * MODULE    : CloseSharedMemory()
269  * ABSTRACT  : Shared memory close processing
270  * NOTE      : Close the shared memory that is allocated/mapped at the time of its creation or open
271  * ARGUMENT  : HANDLE        h_shm        Shared memory handle(Management information pointer)
272  * RETURN    : NONE
273  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
274 void
275 CloseSharedMemory(HANDLE h_shm) {
276     SHARED_MEMORY*     p_shm = reinterpret_cast<SHARED_MEMORY *>(h_shm);
277     int32            lret = EOK;
278
279     if (p_shm == NULL)                  /* If the shared memory handle that is not created or not opened is specified, */ {
280     } else if (p_shm->phy_addr != 0)      /* If the physical memory area address is specified,(Currently never comes here) */ {
281         if (p_shm->p_memory != NULL) {
282             /* Release physical memory area */
283             if (lret != EOK) {
284                 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, \
285                     "_CWORD64_api.dll:%s:LINE %d\r\n munmap_device_memory ERROR In PbFreePhysicalMem\r\n", \
286                 LTEXT(__FILE__), __LINE__);
287                 _pb_Exit();    /* System recovery processing(Exception execution) */
288             }
289         }
290     } else {
291         /* Release the shared memory object mapped to invoking process space */
292         if (p_shm->p_memory != NULL) {
293             lret = munmap(p_shm->p_memory, p_shm->size);
294             p_shm->p_memory = NULL;
295         }
296
297         /* Release the shared memory management information allocated in the heap */
298         free(p_shm);
299     }
300
301     return;
302 }
303
304 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
305  * MODULE    : DeleteSharedMemory()
306  * ABSTRACT  : Shared memory object delete processing
307  * NOTE      : Delete the shared memory object that is specified name
308  * ARGUMENT  : TCHAR*        name        Pointer to the name of the shared memory to be deleted
309  * RETURN    : None
310  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
311 void
312 DeleteSharedMemory(TCHAR* name) {  // LCOV_EXCL_START 8:dead code
313     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
314     int32            lret = EOK;
315     char*            cwd = NULL;
316     char            sz_cwd[PATH_MAX + 1] = {0};
317
318     if (name != NULL) {
319         /* Get path of current directory */
320         cwd = getcwd(sz_cwd, PATH_MAX + 1);
321
322         if (cwd != NULL) {
323             /* Move to shared memory directory */
324             lret = chdir(SHARED_MEMORY_DIRECTORY);
325
326             if (lret == EOK) {
327                 /* Delete shared memory object */
328                 lret = shm_unlink(name);
329
330                 /* Go to original directory */
331                 lret = chdir(cwd);
332             }
333         }
334     }
335
336     return;
337 }
338 // LCOV_EXCL_STOP
339
340
341 HANDLE
342 OpenSharedMemoryAtPhysical(DWORD physical_address, DWORD size, DWORD protect) {  // LCOV_EXCL_START 8:dead code
343     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
344     return NULL;
345 }
346 // LCOV_EXCL_STOP
347