summaryrefslogtreecommitdiffstats
path: root/positioning/server/src/ServiceInterface/VehicleIf.cpp
blob: 0ef5e15bc2e946583d83c32a3aa1104d50f2cdf4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/*
 * @copyright Copyright (c) 2018-2020 TOYOTA MOTOR CORPORATION.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * @file
 *    VehilceIf.cpp
 * @brief
 *    Vehicle Service Interface
 */

/*---------------------------------------------------------------------------------*
 * Include Files                                                                   *
 *---------------------------------------------------------------------------------*/
#include <vehicle_service/positioning_base_library.h>
#include <stub/Vehicle_Sensor_Common_API.h>
#include <stub/Vehicle_API.h>
#include <stub/vehicle_notifications.h>

#include "VehicleIf.h"

/*---------------------------------------------------------------------------------*
 * Definition                                                                      *
 *---------------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------------*
 * Structre                                                                        *
 *---------------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------------*
 * Local Function Prototype                                                        *
 *---------------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------------*
 * Grobal Value                                                                    *
 *---------------------------------------------------------------------------------*/
static BOOL gb_vehicleAvailability = FALSE;

/*---------------------------------------------------------------------------------*
 * Function                                                                        *
 *---------------------------------------------------------------------------------*/
/**
 * @brief
 *    Registering Vehicle Services IF Callback Functions
 *
 * @param[in]  *p_msg_handler    Callback function table
 * @param[in]  Ui_handler_count Number of callback functions
 * @return     eFrameworkunifiedStatusOK
 * @return     eFrameworkunifiedStatusFail
 */
EFrameworkunifiedStatus VehicleIfAttachCallbacksToDispatcher(    // NOLINT(readability/nolint)
                                                _FrameworkunifiedProtocolCallbackHandler const* p_msg_handler,
                                                unsigned int ui_handler_count) {
  EFrameworkunifiedStatus estatus = eFrameworkunifiedStatusFail;
  HANDLE happ;

  happ = _pb_GetAppHandle();
  if (NULL != happ) {
    estatus = FrameworkunifiedAttachCallbacksToDispatcher(happ, "NS_ANY_SRC", p_msg_handler, ui_handler_count);
    if (eFrameworkunifiedStatusOK != estatus) {  // LCOV_EXCL_BR_LINE 4: nsfw error
      FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, 
                    "FrameworkunifiedAttachCallbacksToDispatcher ERROR!! [estatus=%d]", estatus);
    }
  } else {
    FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "_pb_GetAppHandle ERROR!! [happ=%p]", happ);
  }

  return estatus;
}

/**
 * @brief
 *    Vehicle IF: Set Availability Status
 *
 * @details  This function sets status of Vehicle/Availablity stored in local. <br>
 *           Only when the status is true, Vehicle I/F is called by wrappers.
 *
 * @param[in]  BOOL bIsAvailable : Availability Status
 */
void VehicleIf_SetAvailability(BOOL bIsAvailable)
{
    gb_vehicleAvailability = bIsAvailable;

    FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, "gb_vehicleAvailability=%d", gb_vehicleAvailability);

    return;
}

/**
 * @brief
 *    Remove Vehicle Services IF Callback Functions
 *
 * @param[in]  *p_msg_handler    CID table
 * @param[in]  ui_handler_count  Number of CIDs
 * @return     eFrameworkunifiedStatusOK
 * @return     eFrameworkunifiedStatusFail
 */
EFrameworkunifiedStatus VehicleIfDetachCallbacksFromDispatcher(const PUI_32 pui_cmd_array, UI_32 ui_command_count) {
  EFrameworkunifiedStatus estatus = eFrameworkunifiedStatusFail;
  HANDLE happ;

  happ = _pb_GetAppHandle();
  if (NULL != happ) {
    estatus = FrameworkunifiedDetachCallbacksFromDispatcher(happ, "NS_ANY_SRC", pui_cmd_array, ui_command_count, NULL);
    /* In the event of failure */
    if (eFrameworkunifiedStatusOK != estatus) {
      FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, 
                    "PositioningDetachCallbacksToDispatcher Failed in FrameworkunifiedOnStop [estatus=%d]", estatus);
    }
  } else {
    FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "_pb_GetAppHandle ERROR!! [happ=%p]", happ);
  }

  return estatus;
}

/**
 * @brief
 *    Vehicle IF: Get Type of Transmission
 *
 * @details  This function is wrapper function of Vehicle I/F. <br>
 *           You can get the type of transmission of the vehicle.
 *
 * @param[out] uint8_t* pType     : Type of Transmission
 * @param[out] BOOL* bIsAvailable : Availability Status
 *
 * @return     eFrameworkunifiedStatusOK
 * @return     eFrameworkunifiedStatusFail
 */

//EFrameworkunifiedStatus VehicleIf_GetTypeOfTransmission(uint8_t* pType, BOOL* pbIsAvailable)
EFrameworkunifiedStatus VehicleIf_GetTypeOfTransmission(uint8_t* pType, uint8_t* pPkb, BOOL* pbIsAvailable)
{
    EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusFail;
    VEHICLE_RET_API ret;
    HANDLE hApp;
    uint8_t ucVartrm;

#if 1 /* Plus _CWORD27_ Gear Data Support 180115 */
    uint8_t ucPkb;
#endif /* Plus _CWORD27_ Gear Data Support 180115 */


    if (gb_vehicleAvailability == TRUE)
    {
        hApp = _pb_GetAppHandle();
        if (hApp != NULL)
        {
            ret = Vehicle_GetVehicleData(hApp, VEHICLE_DID_VARTRM1, &ucVartrm, sizeof(ucVartrm));
            if (ret < VEHICLE_RET_NORMAL)
            {
                FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "ERROR: Vehicle_GetVehicleData:%d", ret);
            }
            else
            {
                eStatus = eFrameworkunifiedStatusOK;

                switch (ucVartrm)
                {
                    case VEHICLE_SNS_VARTRM1_AT:
                    case VEHICLE_SNS_VARTRM1_CVT:
                    case VEHICLE_SNS_VARTRM1_HV_AT:
                    {
                        *pType = VEHICLEIF_TRANSMISSION_TYPE_AT;
                        break;
                    }
                    case VEHICLE_SNS_VARTRM1_MT:
                    case VEHICLE_SNS_VARTRM1_MMT:
                    case VEHICLE_SNS_VARTRM1_SMT:
                    {
                        *pType = VEHICLEIF_TRANSMISSION_TYPE_MT;
                        break;
                    }
                    case VEHICLE_SNS_VARTRM1_UNCERTAINTY:
                    {
                        *pType = VEHICLEIF_TRANSMISSION_TYPE_UNKNOWN;
                        break;
                    }
                    default:
                    {
                        FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, 
                        "ERROR: Type of transmission is unknown. (%d)", ucVartrm);

                        *pType = VEHICLEIF_TRANSMISSION_TYPE_UNKNOWN;
                        break;
                    }
                }
            }

#if 1 /* Plus _CWORD27_ Gear Data Support 180115 */
            ret = Vehicle_GetVehicleData(hApp, VEHICLE_DID_PKB, &ucPkb, sizeof(ucPkb));
            if (ret < VEHICLE_RET_NORMAL)
            {
                FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "ERROR: Vehicle_GetVehicleData:%d", ret);
            }
            else
            {
                eStatus = eFrameworkunifiedStatusOK;

                switch (ucPkb)
                {
                    case VEHICLE_SNS_OFF:       // R-state
                    //case VEHICLE_SNS_ANTI:      // Antilock(Vehicle undefined)
                    {
                        *pPkb = VEHICLEIF_PKB_OFF;
                        break;
                    }
                    case VEHICLE_SNS_ON:        // Lock state
                    {
                        *pPkb = VEHICLEIF_PKB_ON;
                        break;
                    }
                    default:
                    {
                        FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, 
                        "ERROR: State of parking brake is unknown. (%d)", ucPkb);

                        *pPkb = VEHICLEIF_PKB_OFF;
                        break;
                    }
                }
            }
#endif /* Plus _CWORD27_ Gear Data Support 180115 */

        }
        else
        {
            FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "ERROR: _pb_GetAppHandle hApp:%p", hApp);
        }
    }
    else
    {
        /* nop */
    }

    *pbIsAvailable = gb_vehicleAvailability;

    return eStatus;
}

/**
 * @brief
 *    Vehicle Services IF Availability Change Notification Registration
 *
 * @param[in]  fp_on_cmd    Callback function
 * @return     eFrameworkunifiedStatusOK
 * @return     eFrameworkunifiedStatusFail
 */
EFrameworkunifiedStatus VehicleIfNotifyOnVehicleAvailability(CbFuncPtr fp_on_cmd) {
  EFrameworkunifiedStatus estatus = eFrameworkunifiedStatusFail;
  HANDLE happ;

  happ = _pb_GetAppHandle();
  if (NULL != happ) {
    /* Vehicle/Availability Changing notification registration */
    estatus = FrameworkunifiedSubscribeNotificationWithCallback(happ, NTFY_Vehicle_Availability, fp_on_cmd);  // LCOV_EXCL_BR_LINE 6:unexpected branch  //NOLINT (whitespace/line_length)
    if (eFrameworkunifiedStatusOK != estatus) {  // LCOV_EXCL_BR_LINE 4: nsfw error
      FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, 
                    "PositioningSubscriveNotificationswithCallback ERROR!! [estatus=%d]", estatus);
    }
  } else {
      FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "_pb_GetAppHandle ERROR!! [happ=%p]", happ);
  }

  return estatus;
}

/**
 * @brief
 *    Vehicle IF: Get Shift Position
 *
 * @details  This function is wrapper function of Vehicle I/F. <br>
 *           You can get the shift position of the vehicle.
 *
 * @param[out] uint8_t* pShift    : Shift Position
 * @param[out] BOOL* bIsAvailable : Availability Status
 *
 * @return     eFrameworkunifiedStatusOK
 * @return     eFrameworkunifiedStatusFail
 */
EFrameworkunifiedStatus VehicleIf_GetShiftPosition(uint8_t* pShift, BOOL* pbIsAvailable)
{
    EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusFail;
    VEHICLE_RET_API ret;
    HANDLE hApp;

    if (gb_vehicleAvailability == TRUE)
    {
        hApp = _pb_GetAppHandle();
        if (hApp != NULL)
        {
            ret = Vehicle_GetVehicleData(hApp, VEHICLE_DID_SHIFT, pShift, sizeof(*pShift));
            if (ret < VEHICLE_RET_NORMAL)
            {
                FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "ERROR: Vehicle_GetVehicleData:%d", ret);
            }
            else
            {
                eStatus = eFrameworkunifiedStatusOK;
            }
        }
        else
        {
            FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "ERROR: _pb_GetAppHandle hApp:%p", hApp);
        }
    }
    else
    {
        /* nop */
    }

    *pbIsAvailable = gb_vehicleAvailability;

    return eStatus;
}


/**
 * @brief
 *    Vehicle services IF Info data-delivery registry
 *
 * @param[in]  ul_did
 * @return     eFrameworkunifiedStatusOK
 * @return     eFrameworkunifiedStatusFail
 */

EFrameworkunifiedStatus VehicleIfDeliveryEntry(uint32_t ul_did) {
  EFrameworkunifiedStatus estatus = eFrameworkunifiedStatusFail;
  HANDLE happ;
  VEHICLE_RET_API iret;

  if (TRUE == gb_vehicleAvailability) {
    happ = _pb_GetAppHandle();
    if (NULL != happ) {
      /* Sensor data delivery registration */
      iret = Vehicle_DeliveryEntry(happ, (PCSTR)POS_THREAD_NAME, ul_did,
                                          VEHICLE_DELIVERY_REGIST, VEHICLE_DELIVERY_TIMING_UPDATE);
      if (VEHICLE_RET_NORMAL != iret) {
        FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, 
                                            "Vehicle_DeliveryEntry ERROR!! [iret=%d]", iret);
      } else {
        estatus = eFrameworkunifiedStatusOK;
      }
    } else {
        FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "_pb_GetAppHandle ERROR!! [happ=%p]", happ);
    }
  } else {
      /* nop */
  }
  return estatus;
}