Init basesystem source codes.
[staging/basesystem.git] / vehicleservice / positioning / client / src / POS_sensor_API / Vehicle_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 /*******************************************************************************
18  *    File name        :Vehicle_API.cpp
19  *    System name        :GPF
20  *    Subsystem name    :Vehicle I/F library
21  *    Program name    :Vehicle I/F API
22  *    Module configuration    :POS_RegisterListenerSensData()    Vehicle sensor information delivery registration
23  ******************************************************************************/
24 #include <vehicle_service/positioning_base_library.h>
25 #include <stdio.h>
26 #include <vehicle_service/POS_sensor_API.h>
27 #include "Sensor_API_private.h"
28 #include "Vehicle_API_Dummy.h"
29 #include "Vehicle_API_private.h"
30 #include "POS_private.h"
31
32 /*************************************************/
33 /*           Global variable                      */
34 /*************************************************/
35
36 /*******************************************************************************
37  * initialize
38 ******************************************************************************/
39 VEHICLE_RET_API VehicleInitialize(u_int32 (*sighand)()) {  // LCOV_EXCL_START 8:dead code // NOLINT(readability/nolint)
40     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
41     RET_API _CWORD64_api_ret;
42     VEHICLE_RET_API ret;
43
44     _CWORD64_api_ret = _pb_Setup_CWORD64_API(NULL);
45
46     if (_CWORD64_api_ret == RET_NORMAL) {
47         ret = RET_NORMAL;
48     } else {
49         ret = RET_ERROR;
50     }
51     return ret;
52 }
53 // LCOV_EXCL_STOP
54
55 /**
56  * @brief
57  *    Vehicle sensor information delivery registration
58  *  Register delivery of vehicle sensor information
59  *
60  * @param[in]  hApp                    Application handle
61  * @param[in]  notifyName            Destination thread name
62  * @param[in]  ulDid                Pointer to an array of data IDs for vehicle information
63  * @param[in]  ucCtrlFlg            Delivery control<br>
64  *                Delivery registration: SENSOR_DELIVERY_REGIST<br>
65  *                Delivery stop: SENSOR_DELIVERY_STOP        (Note: Not mounted)<br>
66  *                Resume delivery: SENSOR_DELIVERY_RESTART    (Note: Not mounted)
67  * @param[in]  ucDeliveryTiming        Delivery timing<br>
68  *                Updating    : SENSOR_DELIVERY_TIMING_UPDATE<br>
69  *                Changing    : SENSOR_DELIVERY_TIMING_CHANGE
70  *
71  * @return    SENSOR_RET_NORMAL                Successful registration<br>
72  *            SENSOR_RET_ERROR_CREATE_EVENT    Event generation failure<br>
73  *            SENSOR_RET_ERROR_PARAM            Parameter error<br>
74  *            SENSOR_RET_ERROR_DID            Unregistered ID<br>
75  *            SENSOR_RET_ERROR_BUFFULL        FULL of delivery registers<br>
76  *            SENSOR_RET_ERROR_NOSUPPORT        Unsupported environment
77  *
78  */
79 SENSOR_RET_API POS_RegisterListenerSensData(HANDLE hApp,
80                                             PCSTR notifyName, DID ulDid, u_int8 ucCtrlFlg, u_int8 ucDeliveryTiming) {
81     SENSOR_RET_API    ret;     /* Return value */
82     UNIT_TYPE         type;    /* Supported HW Configuration Type    */
83     BOOL              ret_b;
84
85     /* Internal debug log output */
86     FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, "+");
87
88     /* Check Delivery Control Designation */
89     ret = SENSOR_RET_NORMAL;
90     /* Arguments & Support Configuration Check */
91     if ((ucDeliveryTiming != SENSOR_DELIVERY_TIMING_CHANGE) &&
92         (ucDeliveryTiming != SENSOR_DELIVERY_TIMING_UPDATE)) {
93         /* Change delivery timing,Terminate as a parameter error except update */
94         ret = SENSOR_RET_ERROR_PARAM;
95     } else if (SENSOR_DELIVERY_REGIST != ucCtrlFlg) {
96         /* Parameters other than delivery registration terminated abnormally. */
97         ret = SENSOR_RET_ERROR_PARAM;
98     } else if (hApp == NULL) {
99         /* Check Handle */
100         /* NULL terminates with an abnormal parameter */
101         ret = SENSOR_RET_ERROR_PARAM;
102     } else if (notifyName == NULL) {
103         /* Check Thread Name */
104         /* NULL terminates with an abnormal parameter */
105         ret = SENSOR_RET_ERROR_PARAM;
106     } else {
107         /* Positioning Base API initialization */
108         _pb_Setup_CWORD64_API(hApp);
109
110         /* Supported HW Configuration Check */
111         type = GetEnvSupportInfo();
112         if (UNIT_TYPE_GRADE1 == type) {
113             /* GRADE1 */
114             ret = SENSOR_RET_NORMAL;
115         } else if (UNIT_TYPE_GRADE2 == type) {
116           /*
117            *  Note.
118            *  This feature branches processing depending on the unit type.
119            */
120             ret = SENSOR_RET_ERROR_NOSUPPORT;
121         } else {
122             /* Environment error */
123             ret = SENSOR_RET_ERROR_NOSUPPORT;
124         }
125     }
126
127     if (SENSOR_RET_NORMAL == ret) {
128         /* Judge DID */
129         ret_b = SENSOR_DID_JUDGE_REGLIS(ulDid);
130         if (ret_b == FALSE) {
131             /* An unacceptable ID is regarded as a parameter error. */
132             ret = SENSOR_RET_ERROR_PARAM;
133         } else {
134             /* Delivery registration process */
135             ret = PosRegisterListenerProc(notifyName, ulDid, ucCtrlFlg, ucDeliveryTiming);
136         }
137     }
138
139     /* Internal debug log output */
140     FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, "- [ret = %d]", ret);
141
142     return ret;
143 }
144
145 /*******************************************************************************
146  * MODULE    : PosSetShareData
147  * ABSTRACT  : Write processing to shared memory
148  * FUNCTION  : Write shared memory
149  * ARGUMENT  : *share_top    : Start address of shared memory
150  *           : offset        : Offsets to shared memory write destination
151  *           : *data_src    : Data
152  *           : size_src    : Size
153  * NOTE      :
154  * RETURN    : void
155  ******************************************************************************/
156 void PosSetShareData(void *share_top, u_int16 offset, const void *data_src, u_int16 size_src) {
157     VEHICLE_SHARE_BLOCK_DAT *share_dat;
158     /* Calculate Shared Memory Write Address */
159     share_dat = reinterpret_cast<VEHICLE_SHARE_BLOCK_DAT *>(reinterpret_cast<u_int8 *>(share_top) + offset);
160
161     /* _CWORD71_ processing speed(Memset modification) */
162     /* Clear Shared Memory(Unused area) */
163     share_dat->reserve[0] = 0;
164     share_dat->reserve[1] = 0;
165
166     /* Set write size to shared memory */
167     share_dat->size = size_src;
168
169     /* Set specified data in shared memory */
170     memcpy(reinterpret_cast<void *>(share_dat->data), data_src, (size_t)size_src);
171 }
172
173 /*******************************************************************************
174 * MODULE    : VehicleGetDrData
175 * ABSTRACT  : DR information acquisition
176 * FUNCTION  : Retrieves DR information (optional data) by returning to completion.
177 * ARGUMENT  : pno        : Thread ID
178 *           : did        : Data ID for DR information
179 *           : *dest_data    : Pointer to the storage destination of DR information
180 *           : dest_size    : Storage destination size of DR information(byte)
181 * NOTE      :
182 * RETURN    : Zero or more                            : Stored data size
183 *           : VEHICLE_RET_ERROR_CREATE_EVENT    : Event generation failure
184 *           : VEHICLE_RET_ERROR_OUTOF_MEMORY    : Shared memory allocation failed
185 *           : VEHICLE_RET_ERROR_SIZE            : Storage destination size error
186 *           : VEHICLE_RET_ERROR_DID            : Unregistered ID
187 ******************************************************************************/
188 int32 VehicleGetDrData(PNO pno, DID did, void *dest_data, u_int16 dest_size) {  // LCOV_EXCL_START 8:dead code
189     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
190     VEHICLE_RET_API    ret;                            /* Return value */
191     RET_API            ret_api;                        /* System API return value */
192     EventID            event_id;                       /* Event ID */
193     int32              event_val;                      /* Event value */
194     void               *share_top;                     /* Start address of shared memory */
195     u_int32            share_size;                     /* Size of shared memory area */
196     u_int16            offset;                         /* Offset to free shared memory area */
197     VEHICLE_SHARE_BLOCK_DAT             *share_dat;    /* Address of free shared memory area */
198     VEHICLE_MSG_GET_VEHICLE_DATA_DAT    data;          /* Message data */
199
200     /* Initialization */
201     event_id = 0;
202     event_val = 0;
203     memset(reinterpret_cast<void *>(&data), 0, sizeof(VEHICLE_MSG_GET_VEHICLE_DATA_DAT));
204
205     /* Event Generation */
206     event_id = VehicleCreateEvent(pno);
207
208     if (0 != event_id) {
209         /* Successful event generation */
210         /* Allocate shared memory */
211         ret_api = VehicleLinkShareData(reinterpret_cast<void **>(&share_top), &share_size, &offset);
212         if (RET_NORMAL != ret_api) {
213             /* Failed to allocate shared memory */
214             ret = VEHICLE_RET_ERROR_OUTOF_MEMORY;
215         } else {
216             /* When the shared memory is allocated successfully */
217
218             /* Calculate start address of free shared memory area */
219             share_dat = reinterpret_cast<VEHICLE_SHARE_BLOCK_DAT *>(reinterpret_cast<u_int8 *>(share_top) + offset);
220
221             /* Send vehicle sensor information acquisition message */
222             data.did         = did;
223             data.pno         = pno;
224             data.offset      = offset;
225             data.size        = VEHICLE_SHARE_BLOCK_DSIZE;
226             data.event_id    = event_id;
227             /* Messaging */
228
229             ret_api = VehicleSndMsg(pno,
230                                     PNO_VEHICLE_SENSOR,
231                                     CID_VEHICLEIF_GET_DR_DATA,
232                                     sizeof(VEHICLE_MSG_GET_VEHICLE_DATA_DAT),
233                                     (const void *)&data);
234
235             if (RET_NORMAL == ret_api) {
236                 /* Message transmission processing is successful */
237                 /* Wait for completion event from vehicle sensor thread */
238                 ret_api = _pb_WaitEvent(event_id,
239                                         SAPI_EVWAIT_VAL,
240                                         VEHICLE_RET_ERROR_MIN, VEHICLE_RET_NORMAL, &event_val, INFINITE);
241                 if (RET_NORMAL != ret_api) {
242                     /* When not put in event wait state */
243                     /* Return an event generation failure */
244                     ret = VEHICLE_RET_ERROR_CREATE_EVENT;
245                 } else {
246                     /* Return from Event Wait */
247
248                     /* Link to shared memory */
249                     ret_api = _pb_LinkShareData(const_cast<char *>(VEHICLE_SHARE_NAME), &share_top, &share_size);
250
251                     /* Calculate the address of the shared memory storage area. */
252                     share_dat = reinterpret_cast<VEHICLE_SHARE_BLOCK_DAT *>(reinterpret_cast<u_int8 *>(share_top)
253                                 + offset);
254
255                     if (event_val < 0) {
256                         /* Vehicle sensor information acquisition failure */
257                         ret = (VEHICLE_RET_API)event_val;
258                     } else if (RET_NORMAL != ret_api) {
259                         /* Shared memory error */
260                         ret = VEHICLE_RET_ERROR_OUTOF_MEMORY;
261                     } else if (dest_size < share_dat->size) {
262                         /* Storage destination size error */
263                         ret = VEHICLE_RET_ERROR_SIZE;
264                     } else {
265                         /* Vehicle sensor information acquisition success */
266
267                         /* Copy from shared memory to user memory */
268                         memcpy(dest_data, share_dat->data, (size_t)share_dat->size);
269
270                         /* Set Write Size to Return Value */
271                         ret = static_cast<int32>(share_dat->size);
272                     }
273                 }
274             } else {
275                 /* Message transmission processing failed */
276                 /* Return an event generation failure */
277                 ret = VEHICLE_RET_ERROR_CREATE_EVENT;
278             }
279             /* Free shared memory */
280             (void)VehicleUnLinkShareData(reinterpret_cast<VEHICLE_SHARE*>(share_top), offset);
281         }
282
283         /* Event deletion */
284         ret_api = VehicleDeleteEvent(event_id);
285
286     } else {
287         /* Event generation failure */
288         ret = VEHICLE_RET_ERROR_CREATE_EVENT;
289     }
290     return ret;
291 }
292 // LCOV_EXCL_STOP