Init basesystem source codes.
[staging/basesystem.git] / video_in_hal / positioning_hal / src / Common / positioning_common.cpp
1 /*
2  * @copyright Copyright (c) 2017-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 * @file positioning_common.cpp
18 */
19
20 /*---------------------------------------------------------------------------*/
21 // Include files
22
23 #include "positioning_common.h"
24 #include "MDev_Gps_Common.h"
25 #include "MDev_GpsRecv.h"
26 #include "LineSensDrv_Thread.h"
27
28 /*---------------------------------------------------------------------------*/
29 // Value define
30
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 */
35
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)
45 /* Other Notices */
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 */
49
50 /* Thread state */
51 #define THREAD_STS_NOEXIST                  (0x00)
52 #define THREAD_STS_CREATING                 (0x01)
53 #define THREAD_STS_CREATED                  (0x02)
54
55 /*---------------------------------------------------------------------------*/
56 // ENUMERATION
57
58 /*---------------------------------------------------------------------------*/
59 // STRUCTURE
60
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)                             */
72   uint8_t         u_reserve[2];
73 } ST_THREAD_CREATE_INFO;
74
75 typedef struct {
76     char  share_data_name[PRIM_NAME_MAX]; /**< Shared data name   */
77     u_int32 data_size;            /**< Shared data size */
78 } ST_SHAREDATA;
79
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";
85
86 static ST_THREAD_CREATE_INFO g_pos_thread_create_info[] = {
87   { /* POS_Main */
88     ETID_POS_MAIN, /* (1) */
89     NULL, /* (2) */
90     0, /* (3) */
91     NULL, /* (4) */
92     (NTFY_MSK_NONE), /* (5) */
93     (NTFY_MSK_NONE), /* (6) */
94     0, /* (7) */
95     FALSE, /* (8) */
96     THREAD_STS_NOEXIST, /* (9) */
97     0 /* (10) */
98   },
99   { /* POS_sens */
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) */
106     0, /* (7) */
107     FALSE, /* (8) */
108     THREAD_STS_NOEXIST, /* (9) */
109     0 /* (10) */
110   },
111   { /* POS_Gps */
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) */
118     0, /* (7) */
119     TRUE, /* (8) */
120     THREAD_STS_NOEXIST, /* (9) */
121     0 /* (10) */
122   },
123   { /* POS_Gps_Recv */
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) */
131     FALSE, /* (8) */
132     THREAD_STS_NOEXIST, /* (9) */
133     0 /* (10) */
134   },
135   { /* POS_Gps_Rolovr */
136     ETID_POS_GPS_ROLLOVER, /* (1) */
137     NULL, /* (2) */
138     0, /* (3) */
139     NULL, /* (4) */
140     (NTFY_MSK_NONE), /* (5) */
141     (NTFY_MSK_NONE), /* (6) */
142     0, /* (7) */
143     FALSE, /* (8) */
144     THREAD_STS_NOEXIST, /* (9) */
145     0 /* (10) */
146   },
147   { /* Termination */
148     ETID_POS_MAX, /* (1) */
149     NULL, /* (2) */
150     0, /* (3) */
151     NULL, /* (4) */
152     NTFY_MSK_NONE, /* (5) */
153     NTFY_MSK_NONE, /* (6) */
154     0, /* (7) */
155     FALSE, /* (8) */
156     THREAD_STS_NOEXIST, /* (9) */
157     0 /* (10) */
158   },
159 };
160
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               */
172 #endif
173     { {(int8)NULL},             0    }  /* Termination */
174 };
175
176
177 /*---------------------------------------------------------------------------*/
178 // Functions
179
180 static EFrameworkunifiedStatus PosStopThreadDummy(HANDLE h_app) {
181   return eFrameworkunifiedStatusOK;
182 }
183
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;
188
189   if ((h_app == NULL) || (p_buf == NULL)) {
190       POSITIONING_LOG("Argument ERROR!! [h_app = %p, pBuf = %p]", h_app , p_buf);
191   } else {
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);
196       tmp_size = 0;
197     } else {
198       /* Obtain data */
199       e_status = FrameworkunifiedGetDataPointer(h_app, &p_rev_buf);
200
201       if (e_status == eFrameworkunifiedStatusOK) {
202         *p_buf = p_rev_buf;
203       } else if (e_status == eFrameworkunifiedStatusInvldBufSize) {
204         e_status = FrameworkunifiedGetMsgDataOfSize(h_app, *p_buf, tmp_size);
205
206         if (e_status != eFrameworkunifiedStatusOK) {
207           POSITIONING_LOG("FrameworkunifiedGetMsgDataOfSize ERROR [e_status:%d]", e_status);
208           tmp_size = 0;
209         }
210       } else {
211         POSITIONING_LOG("FrameworkunifiedGetDataPointer ERROR [e_status:%d]", e_status);
212         tmp_size = 0;
213       }
214     }
215   }
216
217   return tmp_size;
218 }
219
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;
225
226   /* Configure Shared Data Generating Tables */
227   p_shm_tbl = g_sharedata_tbl;
228
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;
236               break;
237           } else {
238               /* Error Handling */
239               POSITIONING_LOG("_pb_CreateShareData ERROR [ret_api:%d]", ret_api);
240           }
241       }
242
243       if (retry >= DATMOD_RETRY) {
244           POSITIONING_LOG("_pb_CreateShareData failed more %d times.", DATMOD_RETRY);
245           _pb_Exit();
246           /* don't arrive here. */
247       }
248
249       /* Next shared memory generation */
250       p_shm_tbl++;
251   }
252   return;
253 }
254
255 EFrameworkunifiedStatus PosInitialize(HANDLE h_app) {
256   EFrameworkunifiedStatus e_status = eFrameworkunifiedStatusOK;
257   RET_API ret_api;
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);
261
262       e_status = eFrameworkunifiedStatusFail;
263   } else {
264     PosCreateSharedMemory();
265   }
266   return e_status;
267 }
268
269 /**
270  * @brief
271  *   Common processing after thread startup
272  *
273  *   Thread naming, message queue creation, thread startup response
274  *
275  * @param[in]  h_app Application handle
276  * @param[in]  eTid     Thread ID
277  *
278  * @return  Thread startup mode
279  */
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));
293
294   POSITIONING_LOG("[mode = %d]", pst_setup_info->mode);
295
296   /* Issue thread creation completion notice */
297   ret = _pb_SndMsg_Ext(POS_THREAD_NAME,
298                         CID_THREAD_CREATE_COMP,
299                         sizeof(EnumTID_POS),
300                         reinterpret_cast<const void*>(&e_tid),
301                         0);
302
303   if (ret != RET_NORMAL) {
304     POSITIONING_LOG("_pb_SndMsg_Ext ERROR!! [ret = %d]", ret);
305   }
306
307   return pst_setup_info->mode;
308 }
309
310 /**
311  * @brief
312  *   Common processing at thread stop
313  *
314  *   Thread stop response, thread destruction
315  *
316  * @param[in]  e_tid    Thread ID
317  */
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;
321
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,
326                            sizeof(EnumTID_POS),
327                            reinterpret_cast<const void*>(&e_tid),
328                            0);
329
330   if (e_ret != RET_NORMAL) {
331     POSITIONING_LOG("_pb_SndMsg_Ext ERROR!! [e_ret = %d]", e_ret);
332   }
333
334   /* Thread destruction */
335   _pb_ExitThread((DWORD)0);
336
337   return;
338 }
339
340 /**
341  * @brief
342  *   Positioning in-process thread creation
343  *
344  * @param[in]  hApp Application handle
345  */
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;
350
351   p_thread_info += index;
352
353   static EnumSetupMode_POS g_setup_mode;
354
355   if (NULL == h_app) {
356     return eFrameworkunifiedStatusInvldHandle;
357   }
358
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);
366
367       if (thread_handle == NULL) {
368         POSITIONING_LOG("FrameworkunifiedCreateChildThread ERROR!! [tHandle=%p]", thread_handle);
369       } else {
370         e_status = FrameworkunifiedStartChildThread(h_app,
371                                        thread_handle,
372                                        sizeof(EnumSetupMode_POS),
373                                        &g_setup_mode);
374
375         if (e_status != eFrameworkunifiedStatusOK) {
376           POSITIONING_LOG("FrameworkunifiedStartChildThread ERROR!! [e_status=%d, name=%s]", e_status, p_thread_info->p_name);
377         } else {
378           p_thread_info->uc_status = THREAD_STS_CREATING;
379           POSITIONING_LOG("name=%s\n", p_thread_info->p_name);
380         }
381       }
382     }
383   }
384   return e_status;
385 }
386
387 /*---------------------------------------------------------------------------*/
388 /*EOF*/