Re-organized sub-directory by category
[staging/basesystem.git] / hal / positioning_hal / src / GpsCommon / MDev_Gps_Main.cpp
1 /*
2  * @copyright Copyright (c) 2016-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 MDev_Gps_Main.cpp
18 */
19
20 /*---------------------------------------------------------------------------*/
21 // Include files
22
23 #include "MDev_Gps_Main.h"
24 #include "positioning_common.h"
25 #include "MDev_Gps_Mtrx.h"
26 #include "MDev_Gps_TimerCtrl.h"
27 #include "MDev_Gps_Common.h"
28 #include "MDev_Gps_Nmea.h"
29 #include "MDev_GpsRecv.h"
30 #include "LineSensDrv_Thread.h"
31
32 /*---------------------------------------------------------------------------*/
33 // Global values
34
35 BOOL g_gps_rcv_thread_stop = FALSE;
36
37 /********************************************************************************
38  * Matrix event translation table(Message input)
39  ********************************************************************************/
40 const  TG_GPS_MSGEVTCHNG kGpsMsgchkC[MDEV_GPSMSGCHKC_MAX] = {
41   /* ++ #GPF_60_024 */
42   /* Matrix Event Translation Table for _CWORD82_ NMEA */
43   {CID_GPS__CWORD82__REQUEST,             GPS_EVT_SENDREQ},                   /*  NMEA transmission request    */
44   {CID_INI_INTERNAL_ACCOFF_START,   GPS_EVT_ACC_OFF},                   /*  In-function ACC-OFF instructions    */
45   {CID_GPS_REQRESET,                GPS_EVT_RESETREQ},                  /*  GPS reset request      */
46   {CID_NAVIINFO_DELIVER,            GPS_EVT_NAVI_LOCATIONINFO},         /*  Providing navigation information      */
47   {CID_NAVIINFO_SPEED_DELIVER,      GPS_EVT_NAVI_SPEEDINFO},            /*  Navigation speed information service    */
48   {CID_GPS_TIMESETTING,             GPS_EVT_TIMESETTING},               /*  GPS time setting instruction      */
49   {CID_GPS_BACKUPDATA_LOAD,         GPS_EVT_BACKUPDATA_LOAD},           /*  Backup memory read request */
50   {CID_THREAD_STOP_REQ,             GPS_EVT_STOPREQ},                   /*  Thread stop request           */
51   {CID_GPSWEEKCOR_CNT_NOTIFICATION, GPS_EVT_WEEKCOR_CNT_NOTIFICATIO},   /*  GPS Week Adjustment Counter Notification   */
52   {DAT_END,                         DAT_END}                            /*  Table end      */
53   /* -- #GPF_60_024 */
54 };
55
56 /********************************************************************************
57  * Matrix event translation table(Input Timeout [ Timer ID])
58  ********************************************************************************/
59 const  TG_GPS_TIMEVTCHNG kGpsTimchkC[MDEV_PSTIMCHKC_MAX] = {
60   {GPS_STARTUP_TIMER,       0, GPS_EVT_TIMEOUT_CYCLDAT},        /*  Start confirmation monitoring timer      */
61   {GPS_CYCL_TIMER,          0, GPS_EVT_TIMEOUT_CYCLDAT},        /*  Periodic reception data monitoring timer  */
62   {GPS_RECV_ACK_TIMER,      0, GPS_EVT_TIMEOUT_RSPDAT},         /*  ACK reception monitoring timer      */
63   {GPS_NAVIFST_TIMER,       0, GPS_EVT_TIMEOUT_NAVI},           /*  Initial NAVI monitoring timer    */
64   {GPS_NAVICYCLE_TIMER,     0, GPS_EVT_TIMEOUT_NAVI},           /*  NAVIGATION monitoring timer        */
65   {GPS_NAVIDISRPT_TIMER,    0, GPS_EVT_TIMEOUT_NAVI},           /*  NAVIGATION Disruption Log Output Timer    */
66   {GPS_DIAGCLK_GUARDTIMER,  0, GPS_EVT_TIMEOUT_DIAGCLKGUARD},   /*  Diagnosis provision time guard monitoring timer  */
67   {GPS_NMEADATA_GUARDTIMER, 0, GPS_EVT_TIMEOUT_NMEADATAGUARD},  /*  NMEA data-providing guard monitoring timer  */
68   {GPS_RECOVERY_TIMER,      0, GPS_EVT_TIMEOUT_RECOVERY},       /*  GPS recovery timer              */
69   {GPS_RECEIVERERR_TIMER,   0, GPS_EVT_TIMEOUT_RECEIVERERR},    /*  GPS receiver anomaly detection timer   */
70   {DAT_END,                 0, DAT_END}                         /*  Table end        */
71 };
72
73 /********************************************************************************
74  * Processing Branch Table
75  ********************************************************************************/
76 /********************************************************************************
77  * TAG      : TG_GPS_JMP_TBL
78  * ABSTRACT : GPS jump table
79  * NOTE     : GPS Communication Management Matrix Table
80  ********************************************************************************/
81 const  TG_GPS_JMP_TBL kGpsMatxC[GPS_STS_NUM][GPS_EVT_NUM] = {
82   /* - - - - -  Confirming Start  - - - - - */
83   {
84     {&DevGpsInitStartSendReq},              /* Transmission request                */
85     {&DevGpsInitStartGPSResetReq},          /* GPS reset request          */
86     {&DevGpsInitStartRcvCyclCmd},           /* Cyclic reception command reception          */
87     {&DevGpsNop},                           /* Receive Response Command            */
88     {&DevGpsNop},                           /* Response monitoring timeout          */
89     {&DevGpsInitStartCyclDataTimeOut},      /* Periodic reception data monitoring timeout    */
90     {&DevGpsInitStartNaviDataTimeOut},      /* Navigation providing data monitoring timeout    */
91     {&DevGpsInitStartDiagClkGuardTimeOut},  /* Diagnosis provision time guard monitoring timeout  */
92     {&DevGpsInitStartAccOffStart},          /* In-function ACC-OFF instructions          */
93     {&DevGpsInitStartNaviInfoDeliver},      /* Providing navigation information              */
94     {&DevGpsInitStartNaviSpeedDeliver},     /* Navigation speed information service            */
95     {&DevGpsInitStartSettingTime},          /* GPS time setting instruction            */
96     {&DevGpsInitStartNmeaDataGuardTimeOut}, /* NMEA data-providing guard monitoring timeout */
97     {&DevGpsInitStartBackupDataLoadReq},    /* Backup data read request           */
98     {&DevGpsInitStartStopReq},              /* Thread stop request                      */
99     {&DevGpsInitStartGpsweekcorcntNtf},     /* GPS Week Adjustment Counter Notification              */
100     {&DevGpsInitStartRecoveryTimeOut},      /* GPS error monitoring timer out              */
101     {&DevGpsInitStartGpsReceiverErrTimeOut} /* GPS receiver anomaly detection timeout    */
102   },
103   /* - - - - - -  In operation  - - - - - - */
104   {
105     {&DevGpsNormalSendReq},                 /* Transmission request                */
106     {&DevGpsNormalGPSResetReq},             /* GPS reset request          */
107     {&DevGpsNormalRcvCyclCmd},          /* Cyclic reception command reception          */
108     {&DevGpsNop},                           /* Receive Response Command            */
109     {&DevGpsNop},                           /* Response monitoring timeout          */
110     {&DevGpsNormalCyclDataTimeOut},         /* Periodic reception data monitoring timeout    */
111     {&DevGpsNormalNaviDataTimeOut},         /* Navigation providing data monitoring timeout    */
112     {&DevGpsNormalDiagClkGuardTimeOut},     /* Diagnosis provision time guard monitoring timeout  */
113     {&DevGpsNormalAccOffStart},             /* In-function ACC-OFF instructions          */
114     {&DevGpsNormalNaviInfoDeliver},         /* Providing navigation information              */
115     {&DevGpsNormalNaviSpeedDeliver},        /* Navigation speed information service            */
116     {&DevGpsNormalSettingTime},             /* GPS time setting instruction            */
117     {&DevGpsNormalNmeaDataGuardTimeOut},    /* NMEA data-providing guard monitoring timeout  */
118     {&DevGpsNormalBackupDataLoadReq},       /* Backup data read request           */
119     {&DevGpsNormalStopReq},                 /* Thread stop request                      */
120     {&DevGpsNormalGpsweekcorcntNtf},        /* GPS Week Adjustment Counter Notification              */
121     {&DevGpsNormalRecoveryTimeOut},         /* GPS error monitoring timer out              */
122     {&DevGpsNormalGpsReceiverErrTimeOut}    /* GPS receiver anomaly detection timeout      */
123   },
124   /* - - - - --  Sending  - - - - - - */
125   {
126     {&DevGpsSendSendReq},                   /* Transmission request                */
127     {&DevGpsSendGPSResetReq},               /* GPS reset request          */
128     {&DevGpsSendRcvCyclCmd},                /* Cyclic reception command reception          */
129     {&DevGpsSendRcvRspCmd},                 /* Receive Response Command            */
130     {&DevGpsSendRspDataTimeOut},            /* Response monitoring timeout          */
131     {&DevGpsSendCyclDataTimeOut},           /* Periodic reception data monitoring timeout    */
132     {&DevGpsSendNaviDataTimeOut},           /* Navigation providing data monitoring timeout    */
133     {&DevGpsSendDiagClkGuardTimeOut},       /* Diagnosis provision time guard monitoring timeout  */
134     {&DevGpsSendAccOffStart},               /* In-function ACC-OFF instructions          */
135     {&DevGpsSendNaviInfoDeliver},           /* Providing navigation information              */
136     {&DevGpsSendNaviSpeedDeliver},          /* Navigation speed information service            */
137     {&DevGpsSendSettingTime},               /* GPS time setting instruction            */
138     {&DevGpsSendNmeaDataGuardTimeOut},      /* NMEA data-providing guard monitoring timeout  */
139     {&DevGpsSendBackupDataLoadReq},         /* Backup data read request           */
140     {&DevGpsSendStopReq},                   /* Thread stop request                      */
141     {&DevGpsSendGpsweekcorcntNtf},          /* GPS Week Adjustment Counter Notification              */
142     {&DevGpsSendRecoveryTimeOut},           /* GPS error monitoring timer out              */
143     {&DevGpsSendGpsReceiverErrTimeOut}      /* GPS receiver anomaly detection timeout      */
144   }
145 };
146
147 // Receive command analysis table(NMEA format)
148 const TG_GPS_CMD_ANA_TBL* kGpsCmdAnaTbl = kGpsCmdAnaTblUblox;
149
150 //GPS process pending buffer management table
151 TG_GPS_MSGRCV             g_gps_msg_rcvr;     /* Incoming message            */
152 TG_GPS_MNG                g_gps_mngr;         /* GPS process management information        */
153
154 // Global variable for GPS communication management
155 u_int16                   g_wsend_err;        /* Number of transmission errors          */
156 u_int16                   g_wcnct_err;        /* Number of connection errors          */
157 TG_GPS_OUTPUT_FORMAT      g_rcv_format;       /* Receive Format          */
158
159 // Global variable for the receive thread
160 TG_GPS_RECV_RcvData       g_gps_rcvdata;      /* Serial receive buffer    */
161 TG_GPS_RECV_RcvBuf        g_gps_rcvbuf;       /* Receive data storage buffer  */
162 TG_GPS_RECV_AnaDataBuf    g_gps_ana_databuf;  /* Analysis data buffer    */
163 TG_GPS_RECV_RcvFrameBuf   g_gps_rcv_framebuf; /* Receive frame buffer    */
164 TG_GPS_SAVEBUF            g_gps_save_cmdr;    /* Command pending buffer    */
165 u_int16                   g_wrecv_err;        /* Number of reception errors      */
166
167 /*---------------------------------------------------------------------------*/
168 // Functions
169
170 /********************************************************************************
171  * MODULE      : DEV_Gps_MainThread
172  * ABSTRACT    : Communication Management Thread Domain Processing
173  * FUNCTION    : Receive a request from the host application and send a command to the serial port. 
174  * ARGUMENT    : PVOID     pv....Thread Creation Arguments
175  * NOTE        :
176  * RETURN      : TRUE :Normal completion
177  *               FALSE:ABENDs
178  ********************************************************************************/
179 EFrameworkunifiedStatus DevGpsMainThread(HANDLE h_app) {
180   BOOL ret = FALSE;
181
182   PosSetupThread(h_app, ETID_POS_GPS);
183
184   ret = DevGpsInit();
185
186   if (TRUE == ret) {
187     g_gps_rcv_thread_stop = FALSE;
188
189     /* Start receiving message */
190     DevGpsRcvMsg();
191   } else {
192     /* Initialization failed */
193     POSITIONING_LOG("DevGpsMainThread Init  Fail \n");
194   }
195
196   return eFrameworkunifiedStatusFail;
197 }
198
199
200 /********************************************************************************
201  * MODULE      : DEV_Gps_Init
202  * ABSTRACT    : Communication management thread initialization processing
203  * FUNCTION    : Initialize internal tables and serial ports 
204  * ARGUMENT    : None
205  * NOTE        :
206  * RETURN      : TRUE :Normal completion
207  *               FALSE:ABENDs
208  ********************************************************************************/
209 BOOL DevGpsInit(void) {
210   BOOL ret = FALSE;
211
212   /* Global variable initialization(GPS Communication Management Thread Internal Use Variable) */
213   /* Clear process management information    */
214   memset(&g_gps_mngr, 0x00, sizeof(g_gps_mngr));          /* #05 */
215
216   /* Clear the message receive buffer  */
217   memset(&g_gps_msg_rcvr, 0x00, sizeof(g_gps_msg_rcvr));        /* #05 */
218
219   /* Clear the pending buffer      */
220   memset(&g_gps_save_cmdr, 0x00, sizeof(g_gps_save_cmdr));      /* #05 */
221
222   /* Clear RTC backup-related data  */
223   g_gps_mngr.rcv_cmd      = GPS_FORMAT_MIN;    /* #GPF_60_040 */
224   g_gps_mngr.resp_cmd     = GPS_FORMAT_MIN;    /* #GPF_60_040 */
225   g_gps_mngr.rcv_err_flag = FALSE;
226
227   g_wsend_err   = 0;                /* Initialization of the number of transmission errors        */
228   g_wcnct_err   = 0;                /* Connection error count initialization        */
229   g_rcv_format  = GPS_FORMAT_MIN;   /* Initialize Receive Format #GPF_60_024*/
230
231   /* Initialize the timer function initialization processing management table */
232   DevGpsTimInit();
233
234   /* Start confirmation monitoring timer setting(5Sec) */
235   ret = DevGpsTimeSet(GPS_STARTUP_TIMER);
236
237   /* GPS receiver anomaly detection timer setting(600Sec) */
238   ret = DevGpsTimeSet(GPS_RECEIVERERR_TIMER);
239
240   // Set command table
241   kGpsCmdAnaTbl = kGpsCmdAnaTblUblox;
242
243   /* State transition processing(Start Confirmation Monitor)  */
244   ChangeStatusGpsCommon(GPS_STS_STARTUP);
245
246   return(ret);
247 }
248
249 /******************************************************************************
250 @brief      DEV_Gps_RcvMsg<BR>
251             Message reception processing
252 @outline    Receive a message and distribute processing into a matrix
253 @param[in]  none
254 @param[out] none
255 @return     none
256 @retval     none
257 *******************************************************************************/
258 void DevGpsRcvMsg(void) {
259   RET_API   ret = RET_NORMAL;
260   void*     p_rcv_msg = &g_gps_msg_rcvr;
261
262   while (1) {
263     p_rcv_msg = &g_gps_msg_rcvr;
264
265     // Receive meaasge
266     ret = _pb_RcvMsg(PNO_NAVI_GPS_MAIN, sizeof(g_gps_msg_rcvr), &p_rcv_msg, RM_WAIT);
267
268     if (RET_RCVMSG == ret) {
269       memcpy(&g_gps_msg_rcvr, p_rcv_msg, sizeof(g_gps_msg_rcvr));
270
271       /* Message Event Check Processing  */
272       DevGpsMsgEventCheck();
273
274       if ((u_int32)NG != g_gps_mngr.event) {
275         /* If the event check is successful    */
276         /* Start Processing by Matrix      */
277         (kGpsMatxC[g_gps_mngr.sts][g_gps_mngr.event].func)();
278       }
279     } else {
280       /** MSG reception error */
281       POSITIONING_LOG("DevGpsRcvMsg error ret[%d] \n", ret);
282     }
283   }
284 }
285
286 static inline void SetStopFlag(void) {
287   g_gps_rcv_thread_stop = TRUE;
288 }
289
290 /********************************************************************************
291  * MODULE      : DEV_Gps_MsgEventCheck
292  * ABSTRACT    : Message Event Check Processing
293  * FUNCTION    : Check received messages and convert them to events 
294  * ARGUMENT    : None
295  * NOTE        :
296  * RETURN      : None
297  ********************************************************************************/
298 void DevGpsMsgEventCheck(void) {
299   u_int32 ul_cnt = 0;
300
301   /* Set Error to Event No.      */
302   g_gps_mngr.event = (u_int32)NG;
303
304   /* For timeout notification */
305   if (g_gps_msg_rcvr.header.hdr.cid == CID_TIMER_TOUT) {
306     /* Get event number from sequence number */
307     DevGpsTimEventCheck();
308   } else if (g_gps_msg_rcvr.header.hdr.cid == CID_GPS_RECVDATA) {
309     DevGpsCmdEventCheckNmea();
310   } else {
311     while (kGpsMsgchkC[ul_cnt].cid != DAT_END) {
312       if (g_gps_msg_rcvr.header.hdr.cid == kGpsMsgchkC[ul_cnt].cid) {
313         POSITIONING_LOG("DevGpsMsgEventCheck: cid = %d", g_gps_msg_rcvr.header.hdr.cid);
314
315         /* Get event number */
316         g_gps_mngr.event = kGpsMsgchkC[ul_cnt].event;
317
318         if (GPS_EVT_STOPREQ == g_gps_mngr.event) {
319           SetStopFlag();
320           LineSensDrvThreadStopProcess();
321         }
322
323         break;
324       }
325
326       ul_cnt++;
327     }
328   }
329 }
330
331 /********************************************************************************
332  * MODULE      : DEV_Gps_TimEventCheck
333  * ABSTRACT    : Timeout-Event check processing
334  * FUNCTION    : Check received timeout messages and convert them to events 
335  * ARGUMENT    : None
336  * NOTE        :
337  * RETURN      : None
338  ********************************************************************************/
339 void DevGpsTimEventCheck(void) {
340   u_int8        uc_time_kind = 0;
341   TimerToutMsg  st_rcv_msg;
342   BOOL          ret = FALSE;
343
344   // Set Error to Event No.
345   g_gps_mngr.event = (u_int32)NG;
346
347   // Get message
348   memcpy(&st_rcv_msg, &g_gps_msg_rcvr, sizeof(TimerToutMsg));
349
350   // Time judge kind
351   ret = DevGpsTimeJdgKind((u_int16)st_rcv_msg.TimerSeq);
352
353   if (TRUE == ret) {
354     /* If the sequence numbers match,    */
355     /* Set the conversion code to the event number  */
356     uc_time_kind = (u_int8)((st_rcv_msg.TimerSeq >> 8) & 0x00FF);
357     g_gps_mngr.event = kGpsTimchkC[uc_time_kind].event;
358   }
359 }
360
361 /*---------------------------------------------------------------------------*/
362 /*EOF*/