summaryrefslogtreecommitdiffstats
path: root/positioning/server/src/Sensor/VehicleSens_SharedMemory.cpp
blob: 94ac520d39d238e2b5634b0d5050f31aff335d7b (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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
/*
 * @copyright Copyright (c) 2016-2019 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 name    :VehicleSens_SharedMemory.cpp
 *  System name    :PastModel002
 *  Subsystem name  :Vehicle sensor process
 *  Program name  :Vehicle sensor shared memory management
 ******************************************************************************/

#include <vehicle_service/positioning_base_library.h>
#include "VehicleSens_SharedMemory.h"
#include "Sensor_API.h"
#include "VehicleSens_DataMaster.h"
#include "Sensor_API_private.h"
#include "SensorLocation_API.h"
#include "SensorLocation_API_private.h"

/********************************************************************************
 *      prototype declalation                          *
 ********************************************************************************/
static void VehicleSensLinkSharedMemory(char *shared_memory_name, void **p_share_addr);
static RET_API VehicleSensWriteDataGpsInterruptSignal(DID ul_did);
static RET_API VehicleSensWriteDataGyroConnectStatus(DID ul_did);
static RET_API VehicleSensWriteDataLocalTime(void);
static RET_API VehicleSensWriteDataLonLat(void);

/********************************************************************************
 *      Definition                              *
 ********************************************************************************/

/*******************************************************************************
* MODULE    : VehicleSensInitSharedMemory
* ABSTRACT  : Shared Memory Initialization
* FUNCTION  : Initialize shared memory
* ARGUMENT  : None
* NOTE      :
* RETURN    : RET_NORMAL  :Normal
*           : RET_ERROR  :Abnormality
******************************************************************************/
RET_API  VehicleSensInitSharedMemory(void) {  // LCOV_EXCL_START 8 : dead code
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
    RET_API  l_ret;  /* Return of the functions */

    /* All shared memory initialization */
    l_ret = VehicleSensWriteDataGpsInterruptSignal(VEHICLE_DID_MAIN_GPS_INTERRUPT_SIGNAL);
    if (l_ret == RET_NORMAL) {
        l_ret = VehicleSensWriteDataGpsInterruptSignal(VEHICLE_DID_SYS_GPS_INTERRUPT_SIGNAL);
    }
    if (l_ret == RET_NORMAL) {
        l_ret = VehicleSensWriteDataGyroConnectStatus(VEHICLE_DID_GYRO_CONNECT_STATUS);
    }

    /* Initializes the effective ephemeris count when the shared memory is shut down. */
    if (l_ret == RET_NORMAL) {
        l_ret = VehicleSensWriteDataValidEphemerisNum(0);  /* Initialized with effective ephemeris number 0 */
    }

    /* Writes the value read from the non-volatile memory to the shared memory. */
    /* This process is executed only at startup.,After that, the shared memory will not be overwritten. */
    if (l_ret == RET_NORMAL) {
        l_ret = VehicleSensWriteDataLocalTime();
    }

    if (l_ret == RET_NORMAL) {
        l_ret = VehicleSensWriteDataLonLat();
    }

    return l_ret;
}

/*******************************************************************************
* MODULE    : VehicleSensLinkSharedMemory
* ABSTRACT  : Shared memory link
* FUNCTION  : Link to shared memory
* ARGUMENT  :
* NOTE      :
* RETURN    : None
******************************************************************************/
static void VehicleSensLinkSharedMemory(char *shared_memory_name, void **p_share_addr) {
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
    RET_API  ret_api;
    void  *pv_share_mem;  /* Store Shared Memory Address */
    u_int32  ul_share_mem_size;  /* Size of the linked shared memory */

    /* Link to the handle storage area */
    ret_api = _pb_LinkShareData(shared_memory_name, &pv_share_mem, &ul_share_mem_size);

    if (ret_api == RET_NORMAL) {
        /* If the link is successful */
        *p_share_addr = pv_share_mem;  /* Set the address */
    } else {
        /* If the link fails */
        *p_share_addr = NULL;
    }
}

/*******************************************************************************
* MODULE    : VehicleSensWriteDataGpsInterruptSignal
* ABSTRACT  : Writing of data
* FUNCTION  : Writing Data to Shared Memory
* ARGUMENT  : DID : Data ID
* NOTE      :
* RETURN    : RET_NORMAL  :Normal
*           : RET_ERROR  :Abnormality
******************************************************************************/
static RET_API VehicleSensWriteDataGpsInterruptSignal(DID ul_did) {
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
    static GPS_INTERRUPT  *gpsInterruptSharedAddr = NULL;  /* Store Shared Memory Address */
    static SemID      sem_id = 0;            /* ID of shared memory exclusive semaphore */

    VEHICLESENS_DATA_MASTER_GPS_INTERRUPT_SIGNAL  pst_data;

    RET_API l_ret = RET_NORMAL;  /* Return of the functions */
    RET_API l_ret_api;       /* Return of the functions */


    /* Get Semaphore ID */
    if (sem_id == 0) {
        sem_id = _pb_CreateSemaphore(const_cast<char *>(GPS_INTERRUPT_SIGNAL_SEMAPHO_NAME));
    }

    if (sem_id != 0) {
        /* Semaphore ID successfully acquired */
        l_ret_api = _pb_SemLock(sem_id);  /* Semaphore Lock */

        if (l_ret_api == RET_NORMAL) {
            /* Semaphore lock successful */

            /* When the shared memory is not linked */
            if (gpsInterruptSharedAddr == NULL) {
                /* Link to shared memory */
                /* #QAC confirmation Rule11.4 Use structure for member reference(Cast according to shared memory link IF) */
                VehicleSensLinkSharedMemory(const_cast<char *>(GPS_INTERRUPT_SIGNAL_SHARE_NAME),
                                              reinterpret_cast<void **>(&gpsInterruptSharedAddr));
            }

            if (gpsInterruptSharedAddr != NULL) {
                /* The link to shared memory is successful. */
                switch (ul_did) {
                    case VEHICLE_DID_MAIN_GPS_INTERRUPT_SIGNAL:
                    {
                        /* Get data from data master */
                        VehicleSensGetDataMasterMainGpsInterruptSignal(ul_did, 0, &pst_data);

                        /* Writing Data to Shared Memory */
                        gpsInterruptSharedAddr->_CWORD102__interrupt = pst_data.uc_data;
                        break;
                    }
                    case VEHICLE_DID_SYS_GPS_INTERRUPT_SIGNAL:
                    {
                        /* Get data from data master */
                        VehicleSensGetDataMasterSysGpsInterruptSignal(ul_did, 0, &pst_data);

                        /* Writing Data to Shared Memory */
                        gpsInterruptSharedAddr->_CWORD56__interrupt = pst_data.uc_data;
                        break;
                    }
                    default:
                        l_ret = RET_ERROR;
                        FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "DID is unknown. \r\n");
                        break;
                }

            } else {
                l_ret = RET_ERROR;
                FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "gpsInterruptSharedAddr == NULL \r\n");
            }

            /* Semaphore unlock */
            (void)_pb_SemUnlock(sem_id);
        } else {
            l_ret = RET_ERROR;
            FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "_pb_SemLock failed");
        }
    } else {
        l_ret = RET_ERROR;
        FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "sem_id == 0");
    }

    return l_ret;
}

/*******************************************************************************
* MODULE    : VehicleSensWriteDataGyroConnectStatus
* ABSTRACT  : Writing of data
* FUNCTION  : Writing Data to Shared Memory
* ARGUMENT  : DID : Data ID
* NOTE      :
* RETURN    : RET_NORMAL  :Normal
*           : RET_ERROR  :Abnormality
******************************************************************************/
static RET_API VehicleSensWriteDataGyroConnectStatus(DID ul_did) {
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
    static u_int8    *gyroConnectSharedAddr = NULL;  /* Store Shared Memory Address */
    static SemID    sem_id = 0;            /* ID of shared memory exclusive semaphore */

    VEHICLESENS_DATA_MASTER_GYRO_CONNECT_STATUS    pst_data;

    RET_API l_ret = RET_NORMAL;  /* Return of the functions */
    RET_API l_ret_api;       /* Return of the functions */

    /* Get Semaphore ID */
    if (sem_id == 0) {
        sem_id = _pb_CreateSemaphore(const_cast<char *>(GYRO_CONNECT_STATUS_SEMAPHO_NAME));
    }

    if (sem_id != 0) {
        /* Semaphore ID successfully acquired */
        l_ret_api = _pb_SemLock(sem_id);  /* Semaphore Lock */

        if (l_ret_api == RET_NORMAL) {
            /* Semaphore lock successful */

            /* When the shared memory is not linked */
            if (gyroConnectSharedAddr == NULL) {
                /* Link to shared memory */
                /* #QAC confirmation Rule11.4 Use structure for member reference(Cast according to shared memory link IF) */
                VehicleSensLinkSharedMemory(const_cast<char *>(GYRO_CONNECT_STATUS_SHARE_NAME),
                                              reinterpret_cast<void **>(&gyroConnectSharedAddr));
            }

            if (gyroConnectSharedAddr != NULL) {
                /* The link to shared memory is successful. */

                /* Get data from data master */
                VehicleSensGetDataMasterGyroConnectStatus(ul_did, 0, &pst_data);

                /* Writing Data to Shared Memory */
                *gyroConnectSharedAddr = pst_data.uc_data;

            } else {
                l_ret = RET_ERROR;
                FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
                    "gyroConnectSharedAddr == NULL \r\n");
            }

            /* Semaphore unlock */
            (void)_pb_SemUnlock(sem_id);
        } else {
            l_ret = RET_ERROR;
            FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "_pb_SemLock failed");
        }
    } else {
        l_ret = RET_ERROR;
        FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "sem_id == 0");
    }

    return l_ret;
}

/*******************************************************************************
* MODULE    : VehicleSensWriteDataValidEphemerisNum
* ABSTRACT  : Write effective ephemeris number at shutdown
* FUNCTION  : Write effective ephemeris number at shutdown to shared memory
* ARGUMENT  : u_int8 valid_ephemer_isnum : Number of effective ephemeris written to shared memory during shutdown
* NOTE      :
* RETURN    : RET_NORMAL  :Normal
*           : RET_ERROR  :Abnormality
******************************************************************************/
RET_API VehicleSensWriteDataValidEphemerisNum(u_int8 valid_ephemer_isnum) {
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
    static u_int8  *shared_addr = NULL;  /* Store Shared Memory Address */
    static SemID  sem_id = 0;      /* ID of shared memory exclusive semaphore */

    RET_API l_ret;    /* Return of the functions */
    RET_API l_ret_api;   /* Return of the functions */

    /* Get Semaphore ID */
    if (sem_id == 0) {
        sem_id = _pb_CreateSemaphore(const_cast<char *>(EPHEMERIS_NUM_SEMAPHO_NAME));
    }

    if (sem_id != 0) {
        /* Semaphore ID successfully acquired */
        l_ret_api = _pb_SemLock(sem_id);  /* Semaphore Lock */

        if (l_ret_api == RET_NORMAL) {
            /* Semaphore lock successful */

            /* When the shared memory is not linked */
            if (shared_addr == NULL) {
                /* Link to shared memory */
                /* #QAC confirmation Rule11.4 Use structure for member reference(Cast according to shared memory link IF) */
                VehicleSensLinkSharedMemory(const_cast<char *>(EPHEMERIS_NUM_SHARE_NAME),
                                             reinterpret_cast<void **>(&shared_addr));
            }

            if (shared_addr != NULL) {
                *shared_addr = valid_ephemer_isnum;
                l_ret = RET_NORMAL;

            } else {
                l_ret = RET_ERROR;
                FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "shared_addr == NULL \r\n");
            }

            /* Semaphore unlock */
            l_ret_api = _pb_SemUnlock(sem_id);
            if (l_ret_api != RET_NORMAL) {
                /* Semaphore unlock failure */
                l_ret = RET_ERROR;
                FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "_pb_SemUnlock failed");
            }
        } else {
            /* Semaphore ID acquisition failure */
            l_ret = RET_ERROR;
            FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "_pb_SemLock failed");
        }
    } else {
        l_ret = RET_ERROR;
        FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "sem_id == 0");
    }

    return l_ret;
}

/*******************************************************************************
* MODULE    : VehicleSensWriteDataLocalTime
* ABSTRACT  : Writing Local Time at Shutdown
* FUNCTION  : Write local time on shutdown to shared memory
* ARGUMENT  : None
* NOTE      :
* RETURN    : RET_NORMAL  :Acquisition normal
*           : RET_ERROR  :Acquisition anomaly
******************************************************************************/
static RET_API VehicleSensWriteDataLocalTime(void) {
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
    static LOCALTIME  *shared_addr = NULL;  /* Store Shared Memory Address */
    static SemID    sem_id = 0;      /* ID of shared memory exclusive semaphore */

    RET_API l_ret;    /* Return of the functions */
    RET_API l_ret_api;   /* Return of the functions */

    LOCALTIME LocalTime;

    /* Get Semaphore ID */
    if (sem_id == 0) {
        sem_id = _pb_CreateSemaphore(const_cast<char *>(LOCALTIME_SEMAPHO_NAME));
    }

    if (sem_id != 0) {
        /* Semaphore ID successfully acquired */
        l_ret_api = _pb_SemLock(sem_id);  /* Semaphore Lock */

        if (l_ret_api == RET_NORMAL) {
            /* Semaphore lock successful */

            /* When the shared memory is not linked */
            if (shared_addr == NULL) {
                /* Link to shared memory */
                /* #QAC confirmation Rule11.4 Use structure for member reference(Cast according to shared memory link IF) */
                VehicleSensLinkSharedMemory(const_cast<char *>(LOCALTIME_SHARE_NAME),
                                              reinterpret_cast<void **>(&shared_addr));
            }

            if (shared_addr != NULL) {
                /* The link to shared memory is successful. */

                /* Acquires data from the non-volatile memory and writes it to the shared memory. */
                l_ret_api = VehicleSensReadNVLocalTime(&LocalTime);
                if (l_ret_api == RET_NORMAL) {
                    *shared_addr = LocalTime;
                    l_ret = RET_NORMAL;
                } else {
                    /* When data acquisition from non-volatile memory fails,Set an invalid value */
                    (*shared_addr).status  = CLOCK_INVALID;
                    (*shared_addr).year  = 0xFFFFU; /* invalid */
                    (*shared_addr).month  = 255U; /* invalid */
                    (*shared_addr).day    = 255U; /* invalid */
                    (*shared_addr).hour  = 255U; /* invalid */
                    (*shared_addr).min    = 255U; /* invalid */
                    (*shared_addr).sec    = 255U; /* invalid */
                    l_ret = RET_NORMAL;
                }

            } else {
                l_ret = RET_ERROR;
                FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "shared_addr == NULL \r\n");
            }

            /* Semaphore unlock */
            l_ret_api = _pb_SemUnlock(sem_id);
            if (l_ret_api != RET_NORMAL) {
                /* Semaphore unlock failure */
                l_ret = RET_ERROR;
                FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "_pb_SemUnlock failed");
            }
        } else {
            /* Semaphore ID acquisition failure */
            l_ret = RET_ERROR;
            FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "_pb_SemLock failed");
        }
    } else {
        l_ret = RET_ERROR;
        FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "sem_id == 0");
    }

    return l_ret;
}

/*******************************************************************************
* MODULE    : VehicleSensWriteDataLonLat
* ABSTRACT  : Write position at shutdown
* FUNCTION  : Write shutdown position to shared memory
* ARGUMENT  : None
* NOTE      :
* RETURN    : RET_NORMAL  :Successful acquisition
*           : RET_ERROR  :Failed to acquire
******************************************************************************/
static RET_API VehicleSensWriteDataLonLat(void) {
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
    static LONLAT  *shared_addr = NULL;  /* Store Shared Memory Address */
    static SemID  sem_id = 0;      /* ID of shared memory exclusive semaphore */

    RET_API l_ret;    /* Return of the functions */
    RET_API l_ret_api;   /* Return of the functions */

    LONLAT lonlat;    /* Position */

    /* Get Semaphore ID */
    if (sem_id == 0) {
        sem_id = _pb_CreateSemaphore(const_cast<char *>(LONLAT_SEMAPHO_NAME));
    }

    if (sem_id != 0) {
        /* Semaphore ID successfully acquired */
        l_ret_api = _pb_SemLock(sem_id);  /* Semaphore Lock */

        if (l_ret_api == RET_NORMAL) {
            /* Semaphore lock successful */

            /* When the shared memory is not linked */
            if (shared_addr == NULL) {
                /* Link to shared memory */
                /* #QAC confirmation Rule11.4 Use structure for member reference(Cast according to shared memory link IF) */
                VehicleSensLinkSharedMemory(const_cast<char *>(LONLAT_SHARE_NAME),
                                              reinterpret_cast<void **>(&shared_addr));
            }

            if (shared_addr != NULL) {
                /* The link to shared memory is successful. */

                /* Acquires data from the non-volatile memory and writes it to the shared memory. */
                l_ret_api = VehicleSensReadNVLonLat(&lonlat);
                if (l_ret_api == RET_NORMAL) {
                    *shared_addr = lonlat;
                    l_ret = RET_NORMAL;
                } else {
                    /* When data acquisition from non-volatile memory fails */
                    (*shared_addr).latitude  = SENSORLOCATION_LATITUDE_INIT_VALUE;
                    (*shared_addr).longitude  = SENSORLOCATION_LONGITUDE_INIT_VALUE;
                    l_ret = RET_NORMAL;
                }

            } else {
                l_ret = RET_ERROR;
                FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "shared_addr == NULL \r\n");
            }

            /* Semaphore unlock */
            l_ret_api = _pb_SemUnlock(sem_id);
            if (l_ret_api != RET_NORMAL) {
                /* Semaphore unlock failure */
                l_ret = RET_ERROR;
                FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "_pb_SemUnlock failed");
            }
        } else {
            /* Semaphore ID acquisition failure */
            l_ret = RET_ERROR;
            FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "_pb_SemLock failed");
        }
    } else {
        l_ret = RET_ERROR;
        FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "sem_id == 0");
    }

    return l_ret;
}

/*******************************************************************************
* MODULE    : VehicleSensWriteSharedMemory
* ABSTRACT  : Write Shared Memory
* FUNCTION  : Write Shared Memory
* ARGUMENT  : DID : Data ID
* RETURN    : None
* NOTE      :
******************************************************************************/
void VehicleSensWriteSharedMemory(DID ul_did) {
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
    switch (ul_did) {
        case VEHICLE_DID_MAIN_GPS_INTERRUPT_SIGNAL:
        case VEHICLE_DID_SYS_GPS_INTERRUPT_SIGNAL:
        {
            (void)VehicleSensWriteDataGpsInterruptSignal(ul_did);
            break;
        }
        case VEHICLE_DID_GYRO_CONNECT_STATUS:
        {
            (void)VehicleSensWriteDataGyroConnectStatus(ul_did);
            break;
        }
        default:
            FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, "DID is unknown. \r\n");
            break;
    }
}
// LCOV_EXCL_STOP