common_library: gettid is multiple declaration in cl_error
[staging/basesystem.git] / video_in_hal / vehicleservice / positioning / client / src / POS_gps_API / Gps_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
19  *  Gps_API.cpp
20  * @brief
21  *   Module : POSITIONING
22  *   GPS I/F service functionality
23  */
24 #include <vehicle_service/positioning_base_library.h>
25 #include <vehicle_service/POS_define.h>
26 #include <vehicle_service/POS_gps_API.h>
27 #include <vehicle_service/POS_common_API.h>
28 #include <vehicle_service/POS_sensor_API.h>
29 #include "POS_common_private.h"
30 #include "POS_private.h"
31 #include "Vehicle_API_private.h"
32 #include "Gps_API_private.h"
33
34
35 /**
36  * @brief
37  *  GPS reset request
38  *
39  *  Request a GPS reset
40  *
41  * @param[in] hApp          HANDLE  - Application handle
42  * @param[in] ResName       PCSTR   - Destination thread name
43  * @param[in] mode          uint8_t - Reset mode
44  *
45  * @return  POS_RET_NORMAL           Normal completion(Include illegal)<br>
46  *          POS_RET_ERROR_PARAM      Parameter error<br>
47  *          POS_RET_ERROR_INNER      Internal processing error<br>
48  *          POS_RET_ERROR_BUSY       Busy occurrence<br>
49  *          POS_RET_ERROR_NOSUPPORT  Unsupported environment
50  *
51  */
52 POS_RET_API POS_ReqGPSReset(HANDLE hApp, PCSTR ResName, uint8_t mode) {  // NOLINT(readability/nolint)
53     UNIT_TYPE        type = UNIT_TYPE_NONE;    /* Supported HW Configuration Type */
54     POS_RET_API      ret = POS_RET_NORMAL;     /* Return value of this function */
55     RET_API          ret_api = RET_NORMAL;     /* API return value */
56     POS_RESETINFO    snd_msg;                  /* Reset information */
57     EventID          event_id;                 /* Event ID */
58     int32_t          event_val;                /* Event value */
59     PNO              my_pno;                   /* Caller PNO */
60     PNO              rs_pno;                   /* Destination PNO */
61     uint32_t         pid;                      /* Process ID */
62     uint32_t         tid;                      /* Thread ID */
63     char             name[128];                /* Name buffer */
64
65     FRAMEWORKUNIFIEDLOG(ZONE_26, __FUNCTION__, "+");
66
67     /* Arguments & Support Configuration Check */
68     if ((hApp == NULL) || (ResName == NULL)) {
69         ret = POS_RET_ERROR_PARAM;
70     } else if (GPS_RST_COLDSTART != mode) {
71         /* Parameter error except */
72         ret = POS_RET_ERROR_PARAM;
73     } else {
74         /* Positioning Base API initialization */
75         _pb_Setup_CWORD64_API(hApp);
76
77         /* Supported HW Configuration Check */
78         type = GetEnvSupportInfo();
79         if (UNIT_TYPE_GRADE1 == type) {
80             /* GRADE1 */
81             ret = POS_RET_NORMAL;
82         } else if (UNIT_TYPE_GRADE2 == type) {
83           /*
84            *  Note.
85            *  This feature branches processing depending on the unit type.
86            */
87             ret = POS_RET_ERROR_NOSUPPORT;
88         } else {
89             /* Environment error */
90             ret = POS_RET_ERROR_NOSUPPORT;
91         }
92
93         if (ret == POS_RET_NORMAL) {
94             /* Resource acquisition */
95             if (VehicleGetResource() == TRUE) {
96                 /* Event Generation */
97                 rs_pno = _pb_CnvName2Pno(ResName);
98                 pid = (uint32_t)getpid();
99                 tid = GetTid();
100
101                 snprintf(name, sizeof(name), "PR_p%u_t%u", pid, tid);
102                 my_pno = _pb_CnvName2Pno(name);
103                 event_id = VehicleCreateEvent(my_pno);
104                 if (0 != event_id) {
105                     /* Successful event generation */
106                     memset(&snd_msg, 0x00, sizeof(POS_RESETINFO));
107
108                     /* Message creation */
109                     snd_msg.mode     = mode;      /* Reset mode */
110                     snd_msg.snd_pno  = my_pno;    /* Caller PNo. */
111                     snd_msg.res_pno  = rs_pno;    /* Destination PNo. */
112
113                     /* Message transmission request */
114                     ret_api = _pb_SndMsg_Ext(POS_THREAD_NAME,
115                                              CID_GPS_REQRESET,
116                                              sizeof(POS_RESETINFO),
117                                              reinterpret_cast<void*>(&snd_msg), 0);
118
119                     if (RET_NORMAL == ret_api) {
120                         /* If the data setup process is successful,Wait for a completion event */
121                         ret_api = _pb_WaitEvent(event_id,
122                                                 SAPI_EVWAIT_VAL,
123                                                 VEHICLE_RET_ERROR_MIN, VEHICLE_RET_NORMAL, &event_val, INFINITE);
124                         if (RET_NORMAL != ret_api) {
125                             /* When not put in event wait state */
126                             /* Event generation failure */
127                             ret = POS_RET_ERROR_INNER;
128                         } else {
129                             /* Return from Event Wait */
130                             /* Set event value (processing result) as return value */
131                             ret = (POS_RET_API)event_val;
132                         }
133                     } else {
134                         /* Message transmission processing failed */
135                         ret = POS_RET_ERROR_INNER;
136                     }
137                     /* Event deletion */
138                     ret_api = VehicleDeleteEvent(event_id);
139                 } else {
140                     /* Event generation failure */
141                     ret = POS_RET_ERROR_INNER;
142                 }
143             } else {
144                 /* Insufficient resource */
145                 ret = POS_RET_ERROR_RESOURCE;
146             }
147             /* Resource release */
148             VehicleReleaseResource();
149         }
150     }
151
152     FRAMEWORKUNIFIEDLOG(ZONE_26, __FUNCTION__, "- [ret = %d]", ret);
153     return ret;
154 }
155
156 /**
157  * @brief
158  *  GPS time setting request delivery registration
159  *
160  *  Register delivery of GPS time setting request
161  *
162  * @param[in] hApp          HANDLE  - Application handle
163  * @param[in] notifyName    PCSTR   - Destination thread name
164  * @param[in] ucCtrlFlg     uint8_t - Delivery control(Delivery registration/Delivery stop/Resume delivery)
165  *
166  * @return POS_RET_NORMAL               Normal completion(Include illegal)<br>
167  *         POS_RET_ERROR_BUFFULL        Buffer-full<br>
168  *         POS_RET_ERROR_INNER          Internal error<br>
169  *         POS_RET_ERROR_PARAM          Parameter error<br>
170  *         POS_RET_ERROR_NOSUPPORT      Unsupported environment
171  *
172  */
173 POS_RET_API POS_RegisterListenerGPSTimeSetReq(HANDLE hApp,  // NOLINT(readability/nolint)
174                                             PCSTR notifyName,  // NOLINT(readability/nolint)
175                                             uint8_t ucCtrlFlg) {  // NOLINT(readability/nolint)
176     POS_RET_API       ret = POS_RET_NORMAL;      /* Return value of this function */
177     SENSOR_RET_API    ret_sens = SENSOR_RET_NORMAL;  /* API return value */
178     UNIT_TYPE         type = UNIT_TYPE_NONE;      /* Supported HW Configuration Type */
179
180     /* Internal debug log output */
181     FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, "+");
182
183     /* Arguments & Support Configuration Check */
184     if ((hApp == NULL) || (notifyName == NULL)) {
185         ret = POS_RET_ERROR_PARAM;
186     } else if (SENSOR_DELIVERY_REGIST != ucCtrlFlg) {
187         /* Parameter error other than delivery registration */
188         ret = POS_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 = POS_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 = POS_RET_ERROR_NOSUPPORT;
204         } else {
205             /* Environment error */
206             ret = POS_RET_ERROR_NOSUPPORT;
207         }
208     }
209
210     /* Delivery registration */
211     if (POS_RET_NORMAL == ret) {
212         /* Delivery registry SensorAPI calls */
213         ret_sens = PosRegisterListenerProc(notifyName,                         /* Destination thread name */
214                                            VEHICLE_DID_SETTINGTIME,            /* Data ID */
215                                            ucCtrlFlg,                          /* Delivery control */
216                                            VEHICLE_DELIVERY_TIMING_UPDATE);    /* Delivery timing */
217
218         /* Decision of delivery registration result */
219         if (ret_sens == SENSOR_RET_NORMAL) {
220             ret = POS_RET_NORMAL;
221         } else if (ret_sens == SENSOR_RET_ERROR_PARAM) {
222             ret = POS_RET_ERROR_PARAM;
223         } else if (ret_sens == SENSOR_RET_ERROR_BUFFULL) {
224             ret = POS_RET_ERROR_BUFFULL;
225         } else if (ret_sens == SENSOR_RET_ERROR_RESOURCE) {
226             ret = POS_RET_ERROR_RESOURCE;
227         } else {
228             ret = POS_RET_ERROR_INNER;
229         }
230     }
231     FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, "- [ret = %d]", ret);
232     return ret;
233 }
234
235 /**
236  * @brief
237  *  GPS time setting
238  *
239  *  Set the GPS time
240  *
241  * @param[in] hApp          HANDLE        - Application handle
242  * @param[in] pstDateTime   POS_DATETIME  - Pointer to GPS time information
243  *
244  * @return POS_RET_NORMAL               Normal completion(Include illegal)<br>
245  *         POS_RET_ERROR_INNER          Internal error<br>
246  *         POS_RET_ERROR_TIMEOUT        Timeout error<br>
247  *         POS_RET_ERROR_PARAM          Parameter error<br>
248  *         POS_RET_ERROR_NOSUPPORT      Unsupported environment
249  *
250  */
251 POS_RET_API POS_SetGPStime(HANDLE hApp, POS_DATETIME* pstDateTime) {  // NOLINT(readability/nolint)
252     POS_RET_API    ret = POS_RET_NORMAL;     /* Return value of this function */
253     UNIT_TYPE      type = UNIT_TYPE_NONE;    /* Supported HW Configuration Type */
254
255     FRAMEWORKUNIFIEDLOG(ZONE_26, __FUNCTION__, "+");
256
257     /* Arguments & Support Configuration Check */
258     if ((pstDateTime == NULL) || (hApp == NULL)) {
259         ret = POS_RET_ERROR_PARAM;
260     } else {
261         /* Positioning Base API initialization */
262         _pb_Setup_CWORD64_API(hApp);
263
264         /* Supported HW Configuration Check */
265         type = GetEnvSupportInfo();
266         if (UNIT_TYPE_GRADE1 == type) {
267             /* GRADE1 */
268             ret = POS_RET_NORMAL;
269         } else if (UNIT_TYPE_GRADE2 == type) {
270           /*
271            *  Note.
272            *  This feature branches processing depending on the unit type.
273            */
274             ret = POS_RET_ERROR_NOSUPPORT;
275         } else {
276             /* Environment error */
277             ret = POS_RET_ERROR_NOSUPPORT;
278         }
279     }
280
281     if (ret == POS_RET_NORMAL) {
282         /* Parameter range check */
283         /* Month */
284         if (POS_RET_ERROR == POS_CHKPARAMU8(pstDateTime->month, 1, 12)) {
285             return POS_RET_ERROR_PARAM;
286         }
287         /* Day */
288         if (POS_RET_ERROR == POS_CHKPARAMU8(pstDateTime->date, 1, 31)) {
289             return POS_RET_ERROR_PARAM;
290         }
291         /* Hour */
292         if (POS_RET_ERROR == POS_CHKPARAMU8(pstDateTime->hour, 0, 23)) {
293             return POS_RET_ERROR_PARAM;
294         }
295         /* Minutes */
296         if (POS_RET_ERROR == POS_CHKPARAMU8(pstDateTime->minute, 0, 59)) {
297             return POS_RET_ERROR_PARAM;
298         }
299         /* Second */
300         if (POS_RET_ERROR == POS_CHKPARAMU8(pstDateTime->second, 0, 59)) {
301             return POS_RET_ERROR_PARAM;
302         }
303
304         /* Data setting(Immediate recovery)*/
305         ret = PosSetProc(VEHICLE_DID_SETTINGTIME,
306                          reinterpret_cast<void*>(pstDateTime), sizeof(POS_DATETIME), FALSE);
307     }
308
309     FRAMEWORKUNIFIEDLOG(ZONE_26, __FUNCTION__, "- [ret = %d]", ret);
310     return ret;
311 }
312
313 /**
314  * @brief
315  *  GPS Time Delivery Registration
316  *
317  *  Registering GPS time delivery
318  *
319  * @param[in]  hApp          HANDLE  - Application handle
320  * @param[in]  nofifyName      PCSTR   - Destination thread name
321  * @param[in]  ucCtrlFlg      uint8_t - Delivery control(Delivery registration/Delivery stop/Resume delivery)
322  * @param[in]  ucDeliveryTiming   uint8_t - Delivery timing(Changing/Updating)
323  *
324  * @return  SENSOR_RET_NORMAL               Successful registration<br>
325  *      SENSOR_RET_ERROR_CREATE_EVENT   Event generation failure<br>
326  *      SENSOR_RET_ERROR_PARAM          Parameter error<br>
327  *      SENSOR_RET_ERROR_BUFFULL        FULL of delivery registers<br>
328  *          SENSOR_RET_ERROR_NOSUPPORT      Unsupported environment
329  *
330  */
331 SENSOR_RET_API POS_RegisterListenerGPStime(HANDLE hApp,  // NOLINT(readability/nolint)
332                                            PCSTR notifyName,  // NOLINT(readability/nolint)
333                                            uint8_t ucCtrlFlg,  // NOLINT(readability/nolint)
334                                            uint8_t ucDeliveryTiming) {  // NOLINT(readability/nolint)
335     SENSOR_RET_API  ret = SENSOR_RET_NORMAL;
336     UNIT_TYPE       type = UNIT_TYPE_NONE;
337
338     FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, "+");
339
340     if (hApp == NULL) {
341         /* Parameter error */
342         ret = SENSOR_RET_ERROR_PARAM;
343     } else {
344         /* Positioning Base API initialization */
345         _pb_Setup_CWORD64_API(hApp);
346
347         /* Supported HW Configuration Check */
348         type = GetEnvSupportInfo();
349         if (UNIT_TYPE_GRADE1 == type) {
350             /* GRADE1 */
351             ret = SENSOR_RET_NORMAL;
352         } else if (UNIT_TYPE_GRADE2 == type) {
353           /*
354            *  Note.
355            *  This feature branches processing depending on the unit type.
356            */
357             ret = SENSOR_RET_ERROR_NOSUPPORT;
358         } else {
359             /* fail */
360             ret = SENSOR_RET_ERROR_NOSUPPORT;
361         }
362     }
363
364     if (SENSOR_RET_NORMAL == ret) {
365         if (notifyName == NULL) {
366             /* Parameter error */
367             ret = SENSOR_RET_ERROR_PARAM;
368         }
369         if (ucCtrlFlg != SENSOR_DELIVERY_REGIST) {
370             /* Parameter error */
371             ret = SENSOR_RET_ERROR_PARAM;
372         }
373         if ((ucDeliveryTiming != SENSOR_DELIVERY_TIMING_CHANGE) &&
374             (ucDeliveryTiming != SENSOR_DELIVERY_TIMING_UPDATE)) {
375             /* Parameter error */
376             ret = SENSOR_RET_ERROR_PARAM;
377         }
378     }
379
380     if (SENSOR_RET_NORMAL == ret) {
381         /* Delivery registration process */
382         ret = PosRegisterListenerProc(notifyName, VEHICLE_DID_GPS_TIME, ucCtrlFlg, ucDeliveryTiming);
383     }
384
385     FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, "- [ret = %d]", ret);
386     return ret;
387 }
388
389 /**
390  * @brief
391  *  Get GPS time
392  *
393  *  Get the GPS time
394  *
395  * @param[in] hApp   HANDLE         - Application handle
396  * @param[in] *dat   SENSOR_GPSTIME - Pointer to GPS time information
397  *
398  * @return POS_RET_NORMAL               Normal completion(Include illegal)<br>
399  *         POS_RET_ERROR_INNER          Internal error<br>
400  *         POS_RET_ERROR_PARAM          Parameter error<br>
401  *         POS_RET_ERROR_NOSUPPORT      Unsupported environment
402  *
403  */
404 POS_RET_API POS_GetGPStime(HANDLE hApp, SENSOR_GPSTIME* dat) {  // NOLINT(readability/nolint)
405     POS_RET_API ret = POS_RET_NORMAL;          /* Return value */
406     UNIT_TYPE   type = UNIT_TYPE_NONE;         /* Supported HW Configuration Type */
407     DID         did = VEHICLE_DID_GPS_TIME;    /* DID */
408     int32_t       ret_get_proc;                  /* POS_GetProc Return Values */
409
410     /* Internal debug log output */
411     FRAMEWORKUNIFIEDLOG(ZONE_26, __FUNCTION__, "+");
412
413     /* Arguments & Support Configuration Check */
414     if (hApp == NULL) {
415         /* If the handler is NULL, the process terminates with an error. */
416         ret = POS_RET_ERROR_PARAM;
417     } else if (dat == NULL) {
418         /* If the longitude/latitude data is NULL, it ends with an abnormal parameter. */
419         ret = POS_RET_ERROR_PARAM;
420     } else {
421         /* Positioning Base API initialization */
422         _pb_Setup_CWORD64_API(hApp);
423
424         /* Supported HW Configuration Check */
425         type = GetEnvSupportInfo();
426         if (UNIT_TYPE_GRADE1 == type) {
427             ret = POS_RET_NORMAL;
428         } else {
429           /*
430            *  Note.
431            *  This feature branches processing depending on the unit type.
432            */
433           ret = POS_RET_ERROR_NOSUPPORT;
434         }
435     }
436
437     /* Sensor information acquisition */
438     if (ret == POS_RET_NORMAL) {
439         /* Data acquisition process */
440         ret_get_proc = PosGetProc(did, reinterpret_cast<void*>(dat), sizeof(SENSOR_GPSTIME));
441         if (static_cast<int32_t>(sizeof(SENSOR_GPSTIME)) > ret_get_proc) {
442             /* Failed to acquire */
443             if (ret_get_proc == POS_RET_ERROR_RESOURCE) {
444                 /* Insufficient resource */
445                 ret = POS_RET_ERROR_RESOURCE;
446             } else {
447                 ret = POS_RET_ERROR_INNER;
448             }
449             FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "PosGetProc ERROR [ret = %d]", ret);
450         }
451     }
452
453     FRAMEWORKUNIFIEDLOG(ZONE_26, __FUNCTION__, "- [ret = %d]", ret);
454
455     return ret;
456 }