2 * @copyright Copyright (c) 2017-2020 TOYOTA MOTOR CORPORATION.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * @file positioning_common.cpp
20 /*---------------------------------------------------------------------------*/
23 #include "positioning_common.h"
24 #include "MDev_Gps_Common.h"
25 #include "MDev_GpsRecv.h"
26 #include "LineSensDrv_Thread.h"
28 /*---------------------------------------------------------------------------*/
31 #define DATMOD_RETRY (3) /* Number of shared memory generation retries */
32 #define DATMOD_PREINIT (0) /* Shared Memory State [Before initialization] */
33 #define PRIM_NAME_MAX (32) /* Maximum Name Size */
34 #define VEHICLE_SHARE_NAME ("POS_VEHICLE_SHARE_MEMORY") /* Shared memory name */
36 /* Mask for managing various notification reception conditions */
37 #define NTFY_MSK_NONE (0x00)
38 /* Service availability notification */
39 #define NTFY_MSK_COMMUNICATION_AVAILABILITY (0x01)
40 #define NTFY_MSK_PS_COMMUSB_AVAILABILITY (0x02)
41 #define NTFY_MSK_PS_PSMSHADOW_AVAILABILITY (0x04)
42 #define NTFY_MSK_CLOCK_AVAILABILITY (0x08)
43 #define NTFY_MSK_NS_BACKUPMGR_AVAILABILITY (0x10)
44 #define NTFY_MSK_SS_DEVDETSRV_AVAILABILITY (0x20)
46 #define NTFY_MSK_PS_PSMSHADOW_INIT_COMP (0x01) /* PSMShadow startup completion notice */
47 #define NTFY_MSK_PS_LANSERVER_DEVICE_UPDATE (0x02) /* LanServer device configuration change notification */
48 #define NTFY_MSK_PS_LANSERVER_WAKEUP_COMP (0x04) /* LanServer start completion notice */
51 #define THREAD_STS_NOEXIST (0x00)
52 #define THREAD_STS_CREATING (0x01)
53 #define THREAD_STS_CREATED (0x02)
55 /*---------------------------------------------------------------------------*/
58 /*---------------------------------------------------------------------------*/
61 typedef struct StThreadInfo {
62 EnumTID_POS e_id; /**< Thread ID */
63 const int8_t* p_name; /**< Thread name */
64 PNO p_no; /**< Process number */
65 CbFuncPtr cb_routine; /**< Start routine */
66 uint8_t msk_available; /**< Dependent services Availability */
67 uint8_t msk_ntfy; /**< Dependent notification */
68 uint8_t msk_thread; /**< Dependent threads */
69 BOOL b_is_depended; /**< Positioning/Availability->TRUE change dependency */
70 uint8_t uc_status; /**< Thread activation state */
71 uint8_t uc_order; /**< Boot Sequence(Performance) */
73 } ST_THREAD_CREATE_INFO;
76 char share_data_name[PRIM_NAME_MAX]; /**< Shared data name */
77 u_int32 data_size; /**< Shared data size */
80 static const int8_t kThreadNamePosMain[15] = "POS_Main";
81 static const int8_t kThreadNamePosSens[15] = "POS_Sens";
82 static const int8_t kThreadNamePosGps[15] = "POS_Gps";
83 static const int8_t kThreadNamePosGpsRecv[15] = "POS_Gps_Recv";
84 static const int8_t kThreadNamePosGpsRollover[15] = "POS_Gps_Rolovr";
86 static ST_THREAD_CREATE_INFO g_pos_thread_create_info[] = {
88 ETID_POS_MAIN, /* (1) */
92 (NTFY_MSK_NONE), /* (5) */
93 (NTFY_MSK_NONE), /* (6) */
96 THREAD_STS_NOEXIST, /* (9) */
100 ETID_POS_SENS, /* (1) */
101 kThreadNamePosSens, /* (2) */
102 PNO_LINE_SENS_DRV, /* (3) */
103 &LineSensDrvThread, /* (4) */
104 (NTFY_MSK_NONE), /* (5) */
105 (NTFY_MSK_NONE), /* (6) */
108 THREAD_STS_NOEXIST, /* (9) */
112 ETID_POS_GPS, /* (1) */
113 kThreadNamePosGps, /* (2) */
114 PNO_NAVI_GPS_MAIN, /* (3) */
115 &DevGpsMainThread, /* (4) */
116 (NTFY_MSK_NONE), /* (5) */
117 (NTFY_MSK_NONE), /* (6) */
120 THREAD_STS_NOEXIST, /* (9) */
124 ETID_POS_GPS_RECV, /* (1) */
125 kThreadNamePosGpsRecv, /* (2) */
126 PNO_NAVI_GPS_RCV, /* (3) */
127 &DevGpsRecvThread, /* (4) */
128 (NTFY_MSK_NONE), /* (5) */
129 (NTFY_MSK_NONE), /* (6) */
130 THREAD_STS_MSK_POS_GPS, /* (7) */
132 THREAD_STS_NOEXIST, /* (9) */
135 { /* POS_Gps_Rolovr */
136 ETID_POS_GPS_ROLLOVER, /* (1) */
140 (NTFY_MSK_NONE), /* (5) */
141 (NTFY_MSK_NONE), /* (6) */
144 THREAD_STS_NOEXIST, /* (9) */
148 ETID_POS_MAX, /* (1) */
152 NTFY_MSK_NONE, /* (5) */
153 NTFY_MSK_NONE, /* (6) */
156 THREAD_STS_NOEXIST, /* (9) */
161 static ST_SHAREDATA g_sharedata_tbl[] = {
162 /* Shared data name to be generated, Shared data size */
163 { {VEHICLE_SHARE_NAME}, 512 * 11 }, /* Vehicle sensor information acquisition */
164 #if 0 /* Not used in _CWORD71_ below */
165 { {"SENSOR_SHARE_MEMORY"}, 512 * 11 }, /* Vehicle sensor information Pkg acquisition */
166 { {"GPS_INT_SIGNAL_SHARE_MEMORY"}, 4 }, /* GPS interrupt signal acquisition */
167 { {"LOG_SETTING_SHARE_MEMORY"}, 36 }, /* DR feature log acquisition */
168 { {"GYRO_CONNECT_STTS_SHARE_MEMORY"}, 4 }, /* Gyro connection status acquisition */
169 { {"EPHEMERIS_NUM_SHARE_MEMORY"}, 4 }, /* Effective ephemeris count acquisition at shutdown */
170 { {"LOCALTIME_SHARE_MEMORY"}, 12 }, /* Local time acquisition at shutdown */
171 { {"LONLAT_SHARE_MEMORY"}, 8 }, /* Location acquisition at shutdown */
173 { {(int8)NULL}, 0 } /* Termination */
177 /*---------------------------------------------------------------------------*/
180 static EFrameworkunifiedStatus PosStopThreadDummy(HANDLE h_app) {
181 return eFrameworkunifiedStatusOK;
184 static uint32_t PosGetMsg(HANDLE h_app, void** p_buf, uint32_t ul_size) {
185 EFrameworkunifiedStatus e_status = eFrameworkunifiedStatusOK;
186 uint32_t tmp_size = 0;
187 void* p_rev_buf = NULL;
189 if ((h_app == NULL) || (p_buf == NULL)) {
190 POSITIONING_LOG("Argument ERROR!! [h_app = %p, pBuf = %p]", h_app , p_buf);
192 /* Check the size of received data */
193 tmp_size = FrameworkunifiedGetMsgLength(h_app);
194 if (tmp_size > ul_size) {
195 POSITIONING_LOG("Message ul_size ERROR!! [tmp_size = %d, maxsize = %d]", tmp_size, ul_size);
199 e_status = FrameworkunifiedGetDataPointer(h_app, &p_rev_buf);
201 if (e_status == eFrameworkunifiedStatusOK) {
203 } else if (e_status == eFrameworkunifiedStatusInvldBufSize) {
204 e_status = FrameworkunifiedGetMsgDataOfSize(h_app, *p_buf, tmp_size);
206 if (e_status != eFrameworkunifiedStatusOK) {
207 POSITIONING_LOG("FrameworkunifiedGetMsgDataOfSize ERROR [e_status:%d]", e_status);
211 POSITIONING_LOG("FrameworkunifiedGetDataPointer ERROR [e_status:%d]", e_status);
220 static void PosCreateSharedMemory(void) {
221 RET_API ret_api = RET_NORMAL;
222 void *mod_exec_dmy; /* Module data pointer(dummy) */
223 int retry; /* Retry counter */
224 ST_SHAREDATA *p_shm_tbl;
226 /* Configure Shared Data Generating Tables */
227 p_shm_tbl = g_sharedata_tbl;
229 while (*(p_shm_tbl->share_data_name) != (int8)NULL) {
230 for (retry = 0; retry < DATMOD_RETRY; retry++) {
231 /* Shared Memory Generation */
232 ret_api = _pb_CreateShareData(p_shm_tbl->share_data_name, p_shm_tbl->data_size, &mod_exec_dmy);
233 if (ret_api == RET_NORMAL) {
234 /* Set the shared memory status flag to "Before initialization (0)" */
235 *reinterpret_cast<u_int32 *>(mod_exec_dmy) = DATMOD_PREINIT;
239 POSITIONING_LOG("_pb_CreateShareData ERROR [ret_api:%d]", ret_api);
243 if (retry >= DATMOD_RETRY) {
244 POSITIONING_LOG("_pb_CreateShareData failed more %d times.", DATMOD_RETRY);
246 /* don't arrive here. */
249 /* Next shared memory generation */
255 EFrameworkunifiedStatus PosInitialize(HANDLE h_app) {
256 EFrameworkunifiedStatus e_status = eFrameworkunifiedStatusOK;
258 ret_api = _pb_Setup_CWORD64_API(h_app);
259 if (ret_api != RET_NORMAL) {
260 POSITIONING_LOG("_pb_Setup_CWORD64_API ERROR!! [ret_api = %d]", ret_api);
262 e_status = eFrameworkunifiedStatusFail;
264 PosCreateSharedMemory();
271 * Common processing after thread startup
273 * Thread naming, message queue creation, thread startup response
275 * @param[in] h_app Application handle
276 * @param[in] eTid Thread ID
278 * @return Thread startup mode
280 EnumSetupMode_POS PosSetupThread(HANDLE h_app, EnumTID_POS e_tid) {
281 RET_API ret = RET_NORMAL;
282 ST_THREAD_CREATE_INFO* p_thread_info = g_pos_thread_create_info;
283 ST_THREAD_CREATE_INFO* p_info = p_thread_info + e_tid;
284 ST_THREAD_SETUP_INFO st_setup_info;
285 ST_THREAD_SETUP_INFO* pst_setup_info = &st_setup_info;
286 /* Application handle setting */
287 _pb_SetAppHandle(h_app);
288 /* Create Message Queue */
289 _pb_CreateMsg(p_info->p_no);
290 /* Get thread startup information */
291 pst_setup_info->mode = EPOS_SETUP_MODE_NORMAL;
292 (void)PosGetMsg(h_app, reinterpret_cast<void**>(&pst_setup_info), sizeof(ST_THREAD_SETUP_INFO));
294 POSITIONING_LOG("[mode = %d]", pst_setup_info->mode);
296 /* Issue thread creation completion notice */
297 ret = _pb_SndMsg_Ext(POS_THREAD_NAME,
298 CID_THREAD_CREATE_COMP,
300 reinterpret_cast<const void*>(&e_tid),
303 if (ret != RET_NORMAL) {
304 POSITIONING_LOG("_pb_SndMsg_Ext ERROR!! [ret = %d]", ret);
307 return pst_setup_info->mode;
312 * Common processing at thread stop
314 * Thread stop response, thread destruction
316 * @param[in] e_tid Thread ID
318 void PosTeardownThread(EnumTID_POS e_tid) {
319 RET_API e_ret = RET_NORMAL;
320 ST_THREAD_CREATE_INFO* p_thread_info = g_pos_thread_create_info;
322 (p_thread_info + e_tid)->uc_status = THREAD_STS_NOEXIST;
323 /* Issue thread stop completion notice */
324 e_ret = _pb_SndMsg_Ext(POS_THREAD_NAME,
325 CID_THREAD_STOP_COMP,
327 reinterpret_cast<const void*>(&e_tid),
330 if (e_ret != RET_NORMAL) {
331 POSITIONING_LOG("_pb_SndMsg_Ext ERROR!! [e_ret = %d]", e_ret);
334 /* Thread destruction */
335 _pb_ExitThread((DWORD)0);
342 * Positioning in-process thread creation
344 * @param[in] hApp Application handle
346 EFrameworkunifiedStatus PosCreateThread(HANDLE h_app, int8_t index) {
347 HANDLE thread_handle;
348 EFrameworkunifiedStatus e_status = eFrameworkunifiedStatusOK;
349 ST_THREAD_CREATE_INFO* p_thread_info = g_pos_thread_create_info;
351 p_thread_info += index;
353 static EnumSetupMode_POS g_setup_mode;
356 return eFrameworkunifiedStatusInvldHandle;
359 if (index < ETID_POS_MAX) {
360 if ((p_thread_info->uc_status == THREAD_STS_NOEXIST) && (p_thread_info->cb_routine != NULL)) {
361 /* Thread creation */
362 thread_handle = FrameworkunifiedCreateChildThread(h_app,
363 (PCSTR)(p_thread_info->p_name),
364 p_thread_info->cb_routine,
365 &PosStopThreadDummy);
367 if (thread_handle == NULL) {
368 POSITIONING_LOG("FrameworkunifiedCreateChildThread ERROR!! [tHandle=%p]", thread_handle);
370 e_status = FrameworkunifiedStartChildThread(h_app,
372 sizeof(EnumSetupMode_POS),
375 if (e_status != eFrameworkunifiedStatusOK) {
376 POSITIONING_LOG("FrameworkunifiedStartChildThread ERROR!! [e_status=%d, name=%s]", e_status, p_thread_info->p_name);
378 p_thread_info->uc_status = THREAD_STS_CREATING;
379 POSITIONING_LOG("name=%s\n", p_thread_info->p_name);
387 /*---------------------------------------------------------------------------*/