Init basesystem source codes.
[staging/basesystem.git] / vehicleservice / positioning / client / src / POS_gps_API / Naviinfo_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       Naviinfo_API.cpp
19 @detail     Naviinfo_API Functions
20 @lib        libNaviinfo_API.so
21 ******************************************************************************/
22
23 /*****************************************************************************
24  *            Include                                                        *
25  *****************************************************************************/
26 #include "Naviinfo_API.h"
27 #include <vehicle_service/POS_define.h>
28 #include <vehicle_service/POS_sensor_API.h>
29 #include <vehicle_service/POS_gps_API.h>
30 #include "Vehicle_API_Dummy.h"
31 #include "POS_private.h"
32 #include <native_service/frameworkunified_types.h>  // NOLINT(build/include_order)
33
34 void PosCnvGpsInfo(NAVIINFO_ALL *navi_loc_info);
35
36
37 /**
38  * @brief
39  *  GPS information setting
40  *
41  *  Set GPS information
42  *
43  * @param[in]  hApp         HANDLE        - Application handle
44  * @param[in]  navilocinfo  NAVIINFO_ALL* - Pointer to GPS information storage area
45  *
46  * @return NAVIINFO_RET_NORMAL              Normal completion<br>
47  *         NAVIINFO_RET_ERROR_PARAM         Parameter error<br>
48  *         NAVIINFO_RET_ERROR_INNER         Internal error<br>
49  *         NAVIINFO_RET_ERROR_NOSUPPORT     Unsupported environment
50  *
51  */
52 NAVIINFO_RET_API POS_SetGPSInfo(HANDLE hApp, NAVIINFO_ALL *navilocinfo)
53 {
54     NAVIINFO_RET_API        ret = NAVIINFO_RET_NORMAL;    /* Return value of this function    */
55     UNIT_TYPE               type = UNIT_TYPE_NONE;        /* Supported HW Configuration Type    */
56     NAVIINFO_ALL            navi_loc_info_tmp;            /* Conversion quantity area */
57     RET_API                 ret_api;
58
59     /** NULL checking */
60     if (navilocinfo == NULL) {
61         /** Parameter error */
62         ret = NAVIINFO_RET_ERROR_PARAM;
63     } else if (hApp == NULL) {
64         /** Parameter error */
65         ret = NAVIINFO_RET_ERROR_PARAM;
66     } else {
67         /* Positioning Base API initialization */
68         _pb_Setup_CWORD64_API(hApp);
69
70         /* Supported HW Configuration Check */
71         type = GetEnvSupportInfo();
72         if (UNIT_TYPE_GRADE1 == type) {
73             /* GRADE1 */
74             ret = NAVIINFO_RET_NORMAL;
75         } else if (UNIT_TYPE_GRADE2 == type) {
76           /*
77            *  Note.
78            *  This feature branches processing depending on the unit type.
79            */
80             ret = NAVIINFO_RET_ERROR_NOSUPPORT;
81         } else {
82             /* Environment error */
83             ret = NAVIINFO_RET_ERROR_NOSUPPORT;
84         }
85     }
86
87     if (ret == NAVIINFO_RET_NORMAL) {
88         /* Parameter range check */
89         if (navilocinfo->stNaviGps.tdsts != 0) { /* Other than ""Time not calibrated after receiver reset"" */
90             /* Positioning status information */
91             if (POS_RET_ERROR == POS_CHKPARAMU8(navilocinfo->stDiagGps.stFix.ucFixSts, 0, 2)) {
92                 return NAVIINFO_RET_ERROR_PARAM;
93             }
94             /* Latitude */
95             if (POS_RET_ERROR == POS_CHKPARAM32(navilocinfo->stDiagGps.stFix.stWgs84.lLat, -82944000, 82944000)) {
96                 return NAVIINFO_RET_ERROR_PARAM;
97             }
98             /* Longitude */
99             if (POS_RET_ERROR == POS_CHKPARAM32(navilocinfo->stDiagGps.stFix.stWgs84.lLon, -165888000, 165888000)) {
100                 return NAVIINFO_RET_ERROR_PARAM;
101             }
102             /* Measurement Azimuth */
103             if (POS_RET_ERROR == POS_CHKPARAMU16(navilocinfo->stNaviGps.heading, 0, 3599)) {
104                 return NAVIINFO_RET_ERROR_PARAM;
105             }
106             /* UTC(Month) */
107             if (POS_RET_ERROR == POS_CHKPARAMU8(navilocinfo->stNaviGps.utc.month, 1, 12)) {
108                 return NAVIINFO_RET_ERROR_PARAM;
109             }
110             /* UTC(Day) */
111             if (POS_RET_ERROR == POS_CHKPARAMU8(navilocinfo->stNaviGps.utc.date, 1, 31)) {
112                 return NAVIINFO_RET_ERROR_PARAM;
113             }
114             /* UTC(Hour) */
115             if (POS_RET_ERROR == POS_CHKPARAMU8(navilocinfo->stNaviGps.utc.hour, 0, 23)) {
116                 return NAVIINFO_RET_ERROR_PARAM;
117             }
118             /* UTC(Minutes) */
119             if (POS_RET_ERROR == POS_CHKPARAMU8(navilocinfo->stNaviGps.utc.minute, 0, 59)) {
120                 return NAVIINFO_RET_ERROR_PARAM;
121             }
122             /* UTC(Second) */
123             if (POS_RET_ERROR == POS_CHKPARAMU8(navilocinfo->stNaviGps.utc.second, 0, 59)) {
124                 return NAVIINFO_RET_ERROR_PARAM;
125             }
126         }
127         /* Date and Time Status */
128         if (POS_RET_ERROR == POS_CHKPARAMU8(navilocinfo->stNaviGps.tdsts, 0, 2)) {
129             return NAVIINFO_RET_ERROR_PARAM;
130         }
131
132         /* Copy to conversion area  */
133         memcpy(&navi_loc_info_tmp, navilocinfo, sizeof(NAVIINFO_ALL));
134         /** Data unit conversion */
135         PosCnvGpsInfo(&navi_loc_info_tmp);
136
137         /* Resource acquisition */
138         if (VehicleGetResource() == TRUE) {
139
140         /** Send navigation information to vehicle sensor */
141             ret_api = _pb_SndMsg_Ext(POS_THREAD_NAME,
142                                      CID_NAVIINFO_DELIVER,
143                                      sizeof(NAVIINFO_ALL),
144                                      reinterpret_cast<void*>(&navi_loc_info_tmp), 0);
145             if (ret_api != RET_NORMAL) {
146                 /** Message transmission failure */
147                 ret = NAVIINFO_RET_ERROR_INNER;
148             }
149         } else {
150             /* When resource shortage occurs, the system terminates with an insufficient resource error. */
151             ret = NAVIINFO_RET_ERROR_RESOURCE;
152         }
153         /* Resource release */
154         VehicleReleaseResource();
155     }
156
157     return ret;
158 }
159
160 /**
161  * @brief
162  *   GPS information acquisition
163  *
164  *   Access GPS information
165  *
166  * @param[in]  hApp         HANDLE             - Application handle
167  * @param[in]  navidiaginfo NAVIINFO_DIAG_GPS* - Pointer to GPS information storage area
168  *
169  * @return  NAVIINFO_RET_NORMAL             Normal completion<br>
170  *          NAVIINFO_RET_ERROR_PARAM        Parameter error<br>
171  *          NAVIINFO_RET_ERROR_INNER        Internal error<br>
172  *          NAVIINFO_RET_ERROR_NOSUPPORT    Unsupported environment
173  *
174  */
175 NAVIINFO_RET_API POS_GetGPSInfo(HANDLE hApp, NAVIINFO_DIAG_GPS *navidiaginfo)
176 {
177     NAVIINFO_RET_API     ret = NAVIINFO_RET_NORMAL;    /* Return value of this function    */
178     UNIT_TYPE            type = UNIT_TYPE_NONE;        /* Supported HW Configuration Type    */
179     int32_t              ret_veh;                      /* VehicleAPI Return Values */
180     NAVIINFO_DIAG_GPS    dest_data;                    /* Data acquisition area */
181
182     /** NULL checking */
183     if (navidiaginfo == NULL) {
184         /** Parameter error */
185         ret = NAVIINFO_RET_ERROR_PARAM;
186     } else if (hApp == NULL) {
187         /** Parameter error */
188         ret = NAVIINFO_RET_ERROR_PARAM;
189     } else {
190         /* Positioning Base API initialization */
191         _pb_Setup_CWORD64_API(hApp);
192
193         /* Supported HW Configuration Check */
194         type = GetEnvSupportInfo();
195         if (UNIT_TYPE_GRADE1 == type) {
196             /* GRADE1 */
197             ret = NAVIINFO_RET_NORMAL;
198         } else if (UNIT_TYPE_GRADE2 == type) {
199           /*
200            *  Note.
201            *  This feature branches processing depending on the unit type.
202            */
203             ret = NAVIINFO_RET_ERROR_NOSUPPORT;
204         } else {
205             /* Environment error */
206             ret = NAVIINFO_RET_ERROR_NOSUPPORT;
207         }
208     }
209
210     if (ret == NAVIINFO_RET_NORMAL) {
211         /** Acquisition of navigation data for Diag provide */
212         ret_veh = PosGetProc(
213                              (DID)VEHICLE_DID_NAVIINFO_DIAG_GPS,
214                              reinterpret_cast<void*>(&dest_data),
215                              (u_int16)sizeof(dest_data));
216
217         if (static_cast<int32_t>(sizeof(NAVIINFO_DIAG_GPS)) > ret_veh) {
218             /** Failed to acquire */
219             if (ret_veh == POS_RET_ERROR_RESOURCE) {
220                 ret = NAVIINFO_RET_ERROR_RESOURCE;
221             } else {
222                 ret = NAVIINFO_RET_ERROR_INNER;
223             }
224         } else {
225             /** Successful acquisition */
226             memcpy( navidiaginfo, &dest_data, sizeof(NAVIINFO_DIAG_GPS));
227         }
228     }
229
230     return ret;
231 }
232
233 /* ++ GPS _CWORD82_ support */
234 /**
235  * @brief
236  *   GPS setting transmission request
237  *
238  *   Requesting GPS Settings with Complete Return
239  *
240  * @param[in]  hApp     HANDLE               - Application handle
241  * @param[in]  p_data   SENSOR_MSG_SEND_DAT* - GPS setting information to be sent
242  *
243  * @return  SENSOR_RET_NORMAL              Normal completion<br>
244  *          SENSOR_RET_ERROR_CREATE_EVENT  Event generation failure<br>
245  *          SENSOR_RET_ERROR_PARAM         Parameter error<br>
246  *          SENSOR_RET_ERROR_DID           Unregistered DID<br>
247  *          SENSOR_RET_ERROR_NOSUPPORT     Unsupported environment
248  *
249  */
250 int32 POS_ReqGPSSetting(HANDLE hApp, SENSOR_MSG_SEND_DAT *p_data) {    /* Ignore->MISRA-C++:2008 Rule 7-1-2 */
251     SENSOR_RET_API    ret = SENSOR_RET_NORMAL;        /* Return value                                */
252     RET_API           ret_api;                        /* System API return value                     */
253     uint16_t          expected_size;                  /* Message size for the specified DID */
254
255     UNIT_TYPE         type = UNIT_TYPE_NONE;          /* Supported HW Configuration Type    */
256
257     if (hApp == NULL) {
258         /* Parameter error */
259         ret = SENSOR_RET_ERROR_PARAM;
260     }
261
262     if (ret == SENSOR_RET_NORMAL) {
263         /* Positioning Base API initialization */
264         _pb_Setup_CWORD64_API(hApp);
265
266         /* Argument check (DID) + Size calculation */
267         if (p_data != reinterpret_cast<SENSOR_MSG_SEND_DAT*>(NULL)) {
268             switch (p_data->did) {
269                 case VEHICLE_DID_GPS__CWORD82__SETINITIAL:
270                 {
271                     expected_size = 71;
272                     break;
273                 }
274                 case VEHICLE_DID_GPS__CWORD82__SETRMODE:
275                 {
276                     expected_size = 50;
277                     break;
278                 }
279                 case VEHICLE_DID_GPS__CWORD82__SETRMODEEX:
280                 {
281                     expected_size = 63;
282                     break;
283                 }
284                 case VEHICLE_DID_GPS__CWORD82__SELSENT:
285                 {
286                     expected_size = 21;
287                     break;
288                 }
289                 case VEHICLE_DID_GPS__CWORD82__SETSBAS:
290                 {
291                     expected_size = 34;
292                     break;
293                 }
294                 case VEHICLE_DID_GPS__CWORD82__SETCONF1:
295                 {
296                     expected_size = 37;
297                     break;
298                 }
299                 case VEHICLE_DID_GPS__CWORD82__SETCONF2:
300                 {
301                     expected_size = 45;
302                     break;
303                 }
304                 default:    /* Ignore->MISRA-C++:2008 Rule 6-3-1, 6-4-1 */
305                     ret = SENSOR_RET_ERROR_DID;
306                     break;
307             }
308         } else {
309             ret = SENSOR_RET_ERROR_PARAM;
310         }
311     }
312
313     /* Supported HW Configuration Check */
314     if (ret == SENSOR_RET_NORMAL) {
315         type = GetEnvSupportInfo();
316         if (UNIT_TYPE_GRADE1 == type) {
317             /* GRADE1 */
318             ret = SENSOR_RET_NORMAL;
319         } else if (UNIT_TYPE_GRADE2 == type) {
320           /*
321            *  Note.
322            *  This feature branches processing depending on the unit type.
323            */
324             ret = SENSOR_RET_ERROR_NOSUPPORT;
325         } else {
326             /* Environment error */
327             ret = SENSOR_RET_ERROR_NOSUPPORT;
328         }
329     }
330
331     if (ret == SENSOR_RET_NORMAL) {
332         /* Argument check (Size)*/
333         if (expected_size != p_data->usSize) {
334             ret = SENSOR_RET_ERROR_PARAM;
335         } else {
336             /* Message buffer initialization */
337
338             /* Create message data */
339
340             /* Resource acquisition */
341             if (VehicleGetResource() == TRUE) {
342             /* External Process Transmission and Reception Messages */
343             FRAMEWORKUNIFIEDLOG(ZONE_27, __FUNCTION__, 
344                           "POSITIONING: POS_ReqGPSSetting() --> cid = %d",
345                           CID_SENSORIF__CWORD82__REQUEST);
346             ret_api = _pb_SndMsg_Ext(POS_THREAD_NAME,
347                                      CID_SENSORIF__CWORD82__REQUEST,
348                                      sizeof(SENSOR_MSG_SEND_DAT),
349                                      reinterpret_cast<void *>(p_data), 0);
350             FRAMEWORKUNIFIEDLOG(ZONE_27, __FUNCTION__, "POSITIONING: POS_ReqGPSSetting() <--");
351
352             if (ret_api != RET_NORMAL) {
353                 ret = SENSOR_RET_ERROR_CREATE_EVENT;
354             }
355             } else {
356                 /* When resource shortage occurs, the system terminates with an insufficient resource error. */
357                 ret = SENSOR_RET_ERROR_RESOURCE;
358             }
359             /* Resource release */
360             VehicleReleaseResource();
361         }
362     }
363
364     return ret;
365 }
366 /* -- GPS _CWORD82_ support */
367
368 /**
369  * @brief
370  *   GPS information conversion process
371  *
372  *   Convert GPS information to a format to be provided to the vehicle sensor
373  *
374  * @param[in]  none
375  * @param[in]  navi_loc_info NAVIINFO_ALL* - GPS information pointer
376  *
377  * @return  none
378  */
379 void PosCnvGpsInfo(NAVIINFO_ALL *navi_loc_info) {
380     int32_t    altitude;
381     int64_t    tmp;
382     uint16_t   heading;
383
384     /* Unit conversion of fix altitude[1m]->[0.01m] */
385     tmp = (int64_t)((int64_t)(navi_loc_info->stNaviGps.altitude) * 100);
386     if (tmp > static_cast<int32_t>(0x7FFFFFFF)) {
387         /* +Overflow of digits */
388         altitude = static_cast<int32_t>(0x7FFFFFFF);
389     } else if (tmp < static_cast<int32_t>(0x80000000)) {    /* Ignore->MISRA-C:2004 Rule 3.1 */
390         /* -Overflow of digits */
391         altitude = static_cast<int32_t>(0x80000000);        /* Ignore->MISRA-C:2004 Rule 3.1 */
392     } else {
393         altitude = static_cast<int32_t>(tmp);
394     }
395     navi_loc_info->stNaviGps.altitude = altitude;
396
397     /* Measurement Azimuth Conversion[0.Once]->[0.01 degree] */
398     heading = navi_loc_info->stNaviGps.heading;
399     heading = static_cast<uint16_t>(heading - ((heading / 3600) * 3600));
400     heading = static_cast<uint16_t>(heading * 10);
401     navi_loc_info->stNaviGps.heading = heading;
402
403     return;
404 }