Init basesystem source codes.
[staging/basesystem.git] / vehicleservice / positioning / server / src / ServiceInterface / VehicleIf.cpp
1 /*
2  * @copyright Copyright (c) 2018-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  *    VehilceIf.cpp
20  * @brief
21  *    Vehicle Service Interface
22  */
23
24 /*---------------------------------------------------------------------------------*
25  * Include Files                                                                   *
26  *---------------------------------------------------------------------------------*/
27 #include <vehicle_service/positioning_base_library.h>
28 #include <stub/Vehicle_Sensor_Common_API.h>
29 #include <stub/Vehicle_API.h>
30 #include <stub/vehicle_notifications.h>
31
32 #include "VehicleIf.h"
33
34 /*---------------------------------------------------------------------------------*
35  * Definition                                                                      *
36  *---------------------------------------------------------------------------------*/
37
38 /*---------------------------------------------------------------------------------*
39  * Structre                                                                        *
40  *---------------------------------------------------------------------------------*/
41
42 /*---------------------------------------------------------------------------------*
43  * Local Function Prototype                                                        *
44  *---------------------------------------------------------------------------------*/
45
46 /*---------------------------------------------------------------------------------*
47  * Grobal Value                                                                    *
48  *---------------------------------------------------------------------------------*/
49 static BOOL gb_vehicleAvailability = FALSE;
50
51 /*---------------------------------------------------------------------------------*
52  * Function                                                                        *
53  *---------------------------------------------------------------------------------*/
54 /**
55  * @brief
56  *    Registering Vehicle Services IF Callback Functions
57  *
58  * @param[in]  *p_msg_handler    Callback function table
59  * @param[in]  Ui_handler_count Number of callback functions
60  * @return     eFrameworkunifiedStatusOK
61  * @return     eFrameworkunifiedStatusFail
62  */
63 EFrameworkunifiedStatus VehicleIfAttachCallbacksToDispatcher(    // NOLINT(readability/nolint)
64                                                 _FrameworkunifiedProtocolCallbackHandler const* p_msg_handler,
65                                                 unsigned int ui_handler_count) {
66   EFrameworkunifiedStatus estatus = eFrameworkunifiedStatusFail;
67   HANDLE happ;
68
69   happ = _pb_GetAppHandle();
70   if (NULL != happ) {
71     estatus = FrameworkunifiedAttachCallbacksToDispatcher(happ, "NS_ANY_SRC", p_msg_handler, ui_handler_count);
72     if (eFrameworkunifiedStatusOK != estatus) {  // LCOV_EXCL_BR_LINE 4: nsfw error
73       FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, 
74                     "FrameworkunifiedAttachCallbacksToDispatcher ERROR!! [estatus=%d]", estatus);
75     }
76   } else {
77     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "_pb_GetAppHandle ERROR!! [happ=%p]", happ);
78   }
79
80   return estatus;
81 }
82
83 /**
84  * @brief
85  *    Vehicle IF: Set Availability Status
86  *
87  * @details  This function sets status of Vehicle/Availablity stored in local. <br>
88  *           Only when the status is true, Vehicle I/F is called by wrappers.
89  *
90  * @param[in]  BOOL bIsAvailable : Availability Status
91  */
92 void VehicleIf_SetAvailability(BOOL bIsAvailable)
93 {
94     gb_vehicleAvailability = bIsAvailable;
95
96     FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, "gb_vehicleAvailability=%d", gb_vehicleAvailability);
97
98     return;
99 }
100
101 /**
102  * @brief
103  *    Remove Vehicle Services IF Callback Functions
104  *
105  * @param[in]  *p_msg_handler    CID table
106  * @param[in]  ui_handler_count  Number of CIDs
107  * @return     eFrameworkunifiedStatusOK
108  * @return     eFrameworkunifiedStatusFail
109  */
110 EFrameworkunifiedStatus VehicleIfDetachCallbacksFromDispatcher(const PUI_32 pui_cmd_array, UI_32 ui_command_count) {
111   EFrameworkunifiedStatus estatus = eFrameworkunifiedStatusFail;
112   HANDLE happ;
113
114   happ = _pb_GetAppHandle();
115   if (NULL != happ) {
116     estatus = FrameworkunifiedDetachCallbacksFromDispatcher(happ, "NS_ANY_SRC", pui_cmd_array, ui_command_count, NULL);
117     /* In the event of failure */
118     if (eFrameworkunifiedStatusOK != estatus) {
119       FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, 
120                     "PositioningDetachCallbacksToDispatcher Failed in FrameworkunifiedOnStop [estatus=%d]", estatus);
121     }
122   } else {
123     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "_pb_GetAppHandle ERROR!! [happ=%p]", happ);
124   }
125
126   return estatus;
127 }
128
129 /**
130  * @brief
131  *    Vehicle IF: Get Type of Transmission
132  *
133  * @details  This function is wrapper function of Vehicle I/F. <br>
134  *           You can get the type of transmission of the vehicle.
135  *
136  * @param[out] uint8_t* pType     : Type of Transmission
137  * @param[out] BOOL* bIsAvailable : Availability Status
138  *
139  * @return     eFrameworkunifiedStatusOK
140  * @return     eFrameworkunifiedStatusFail
141  */
142
143 //EFrameworkunifiedStatus VehicleIf_GetTypeOfTransmission(uint8_t* pType, BOOL* pbIsAvailable)
144 EFrameworkunifiedStatus VehicleIf_GetTypeOfTransmission(uint8_t* pType, uint8_t* pPkb, BOOL* pbIsAvailable)
145 {
146     EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusFail;
147     VEHICLE_RET_API ret;
148     HANDLE hApp;
149     uint8_t ucVartrm;
150
151 #if 1 /* Plus _CWORD27_ Gear Data Support 180115 */
152     uint8_t ucPkb;
153 #endif /* Plus _CWORD27_ Gear Data Support 180115 */
154
155
156     if (gb_vehicleAvailability == TRUE)
157     {
158         hApp = _pb_GetAppHandle();
159         if (hApp != NULL)
160         {
161             ret = Vehicle_GetVehicleData(hApp, VEHICLE_DID_VARTRM1, &ucVartrm, sizeof(ucVartrm));
162             if (ret < VEHICLE_RET_NORMAL)
163             {
164                 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "ERROR: Vehicle_GetVehicleData:%d", ret);
165             }
166             else
167             {
168                 eStatus = eFrameworkunifiedStatusOK;
169
170                 switch (ucVartrm)
171                 {
172                     case VEHICLE_SNS_VARTRM1_AT:
173                     case VEHICLE_SNS_VARTRM1_CVT:
174                     case VEHICLE_SNS_VARTRM1_HV_AT:
175                     {
176                         *pType = VEHICLEIF_TRANSMISSION_TYPE_AT;
177                         break;
178                     }
179                     case VEHICLE_SNS_VARTRM1_MT:
180                     case VEHICLE_SNS_VARTRM1_MMT:
181                     case VEHICLE_SNS_VARTRM1_SMT:
182                     {
183                         *pType = VEHICLEIF_TRANSMISSION_TYPE_MT;
184                         break;
185                     }
186                     case VEHICLE_SNS_VARTRM1_UNCERTAINTY:
187                     {
188                         *pType = VEHICLEIF_TRANSMISSION_TYPE_UNKNOWN;
189                         break;
190                     }
191                     default:
192                     {
193                         FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, 
194                         "ERROR: Type of transmission is unknown. (%d)", ucVartrm);
195
196                         *pType = VEHICLEIF_TRANSMISSION_TYPE_UNKNOWN;
197                         break;
198                     }
199                 }
200             }
201
202 #if 1 /* Plus _CWORD27_ Gear Data Support 180115 */
203             ret = Vehicle_GetVehicleData(hApp, VEHICLE_DID_PKB, &ucPkb, sizeof(ucPkb));
204             if (ret < VEHICLE_RET_NORMAL)
205             {
206                 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "ERROR: Vehicle_GetVehicleData:%d", ret);
207             }
208             else
209             {
210                 eStatus = eFrameworkunifiedStatusOK;
211
212                 switch (ucPkb)
213                 {
214                     case VEHICLE_SNS_OFF:       // R-state
215                     //case VEHICLE_SNS_ANTI:      // Antilock(Vehicle undefined)
216                     {
217                         *pPkb = VEHICLEIF_PKB_OFF;
218                         break;
219                     }
220                     case VEHICLE_SNS_ON:        // Lock state
221                     {
222                         *pPkb = VEHICLEIF_PKB_ON;
223                         break;
224                     }
225                     default:
226                     {
227                         FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, 
228                         "ERROR: State of parking brake is unknown. (%d)", ucPkb);
229
230                         *pPkb = VEHICLEIF_PKB_OFF;
231                         break;
232                     }
233                 }
234             }
235 #endif /* Plus _CWORD27_ Gear Data Support 180115 */
236
237         }
238         else
239         {
240             FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "ERROR: _pb_GetAppHandle hApp:%p", hApp);
241         }
242     }
243     else
244     {
245         /* nop */
246     }
247
248     *pbIsAvailable = gb_vehicleAvailability;
249
250     return eStatus;
251 }
252
253 /**
254  * @brief
255  *    Vehicle Services IF Availability Change Notification Registration
256  *
257  * @param[in]  fp_on_cmd    Callback function
258  * @return     eFrameworkunifiedStatusOK
259  * @return     eFrameworkunifiedStatusFail
260  */
261 EFrameworkunifiedStatus VehicleIfNotifyOnVehicleAvailability(CbFuncPtr fp_on_cmd) {
262   EFrameworkunifiedStatus estatus = eFrameworkunifiedStatusFail;
263   HANDLE happ;
264
265   happ = _pb_GetAppHandle();
266   if (NULL != happ) {
267     /* Vehicle/Availability Changing notification registration */
268     estatus = FrameworkunifiedSubscribeNotificationWithCallback(happ, NTFY_Vehicle_Availability, fp_on_cmd);  // LCOV_EXCL_BR_LINE 6:unexpected branch  //NOLINT (whitespace/line_length)
269     if (eFrameworkunifiedStatusOK != estatus) {  // LCOV_EXCL_BR_LINE 4: nsfw error
270       FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, 
271                     "PositioningSubscriveNotificationswithCallback ERROR!! [estatus=%d]", estatus);
272     }
273   } else {
274       FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "_pb_GetAppHandle ERROR!! [happ=%p]", happ);
275   }
276
277   return estatus;
278 }
279
280 /**
281  * @brief
282  *    Vehicle IF: Get Shift Position
283  *
284  * @details  This function is wrapper function of Vehicle I/F. <br>
285  *           You can get the shift position of the vehicle.
286  *
287  * @param[out] uint8_t* pShift    : Shift Position
288  * @param[out] BOOL* bIsAvailable : Availability Status
289  *
290  * @return     eFrameworkunifiedStatusOK
291  * @return     eFrameworkunifiedStatusFail
292  */
293 EFrameworkunifiedStatus VehicleIf_GetShiftPosition(uint8_t* pShift, BOOL* pbIsAvailable)
294 {
295     EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusFail;
296     VEHICLE_RET_API ret;
297     HANDLE hApp;
298
299     if (gb_vehicleAvailability == TRUE)
300     {
301         hApp = _pb_GetAppHandle();
302         if (hApp != NULL)
303         {
304             ret = Vehicle_GetVehicleData(hApp, VEHICLE_DID_SHIFT, pShift, sizeof(*pShift));
305             if (ret < VEHICLE_RET_NORMAL)
306             {
307                 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "ERROR: Vehicle_GetVehicleData:%d", ret);
308             }
309             else
310             {
311                 eStatus = eFrameworkunifiedStatusOK;
312             }
313         }
314         else
315         {
316             FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "ERROR: _pb_GetAppHandle hApp:%p", hApp);
317         }
318     }
319     else
320     {
321         /* nop */
322     }
323
324     *pbIsAvailable = gb_vehicleAvailability;
325
326     return eStatus;
327 }
328
329
330 /**
331  * @brief
332  *    Vehicle services IF Info data-delivery registry
333  *
334  * @param[in]  ul_did
335  * @return     eFrameworkunifiedStatusOK
336  * @return     eFrameworkunifiedStatusFail
337  */
338
339 EFrameworkunifiedStatus VehicleIfDeliveryEntry(uint32_t ul_did) {
340   EFrameworkunifiedStatus estatus = eFrameworkunifiedStatusFail;
341   HANDLE happ;
342   VEHICLE_RET_API iret;
343
344   if (TRUE == gb_vehicleAvailability) {
345     happ = _pb_GetAppHandle();
346     if (NULL != happ) {
347       /* Sensor data delivery registration */
348       iret = Vehicle_DeliveryEntry(happ, (PCSTR)POS_THREAD_NAME, ul_did,
349                                           VEHICLE_DELIVERY_REGIST, VEHICLE_DELIVERY_TIMING_UPDATE);
350       if (VEHICLE_RET_NORMAL != iret) {
351         FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, 
352                                             "Vehicle_DeliveryEntry ERROR!! [iret=%d]", iret);
353       } else {
354         estatus = eFrameworkunifiedStatusOK;
355       }
356     } else {
357         FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "_pb_GetAppHandle ERROR!! [happ=%p]", happ);
358     }
359   } else {
360       /* nop */
361   }
362   return estatus;
363 }
364