BugFix: A establishment of websockets fails.
[profile/ivi/ico-vic-carsimulator.git] / src / CGtCtrl.h
1 /*
2  * Copyright (c) 2013, TOYOTA MOTOR CORPORATION.
3  *
4  * This program is licensed under the terms and conditions of the 
5  * Apache License, version 2.0.  The full text of the Apache License is at
6  * http://www.apache.org/licenses/LICENSE-2.0
7  *
8  */
9 /**
10  * @brief   Pseudo-driving vehicle
11  * @file    CGtCtrl.h
12  */
13
14 #ifndef CGTCTRL_H_
15 #define CGTCTRL_H_
16
17 #include <stdio.h>
18 #include <signal.h>
19 #include <math.h>
20 #include "CConf.h"
21 #include "CJoyStick.h"
22 #include "CJoyStickEV.h"
23 #include "CJoyStickG25.h"
24 #include "CJoyStickG27.h"
25
26 #include <pthread.h>
27
28 #include <queue>
29 #include <string>
30
31 #include <cstddef>
32 #include <unistd.h>
33 #include <sys/types.h>
34 #include <sys/ipc.h>
35 #include <sys/msg.h>
36 #include <errno.h>
37 #include <vector>
38 #include <list>
39 #include <algorithm>
40 #include <glib-object.h>
41 #include <json-glib/json-glib.h>
42
43
44 #include "AmbpiComm.h"
45 #if 1
46 #define MAX_SPEED   199
47 #else
48 #define MAX_SPEED   280
49 #endif
50
51 const useconds_t g_sleeptime = 10000; // = 10milli sec = 0.01 sec
52 const int g_JoyStickTypeNum = 3;
53 const std::string g_ConfPathG25 = "/etc/ico-vic-carsim/G25.conf";
54 const std::string g_ConfPathG27 = "/etc/ico-vic-carsim/G27.conf";
55 const std::string g_RouteListFile = "/etc/ico-vic-carsim/route.txt";
56 #define D_RUNLOOP_INTERVAL_COUNT  5
57 #define D_RUNLOOP_INTERVAL_COUNT2 50
58
59 #define GEORESET 1000
60 #define PIE 3.14159265
61
62 #ifdef  DEBUG
63 #define NOTRCSIZE(n)    (n | 0x01000000)
64 #else
65 #define NOTRCSIZE(n)    (n)
66 #endif
67
68
69 #define AMB_CONF        "/etc/ambd/config"
70
71 struct geoData
72 {
73     double lat;
74     double lng;
75 };
76
77 void DisconnectClient(int ClientID);
78 int ClientHandShake(int ClientID);
79 int ClientRequest(int ClientID);
80 void *Comm(void *s);
81
82 void MakeFdSet(void);
83 void FullMakeFdSet(void);
84
85 void DaemonTerminate(int SigNo);
86
87
88 struct VehicleInfoNameList
89 {
90     const static int maxlen = 64;
91
92   public:
93     int length;
94     char name[maxlen][64];
95     long priority[maxlen];
96
97     /*--------------------------------------------------------------------------*/
98     /**
99      * @brief   initialize
100      *
101      * @param   none
102      * @return  none
103      */
104     /*--------------------------------------------------------------------------*/
105     void init()
106     {
107         length = 0;
108         memset(&name[0], 0x00, maxlen * 64);
109         memset(&priority[0], 0x00, sizeof(priority));
110     }
111
112     /*--------------------------------------------------------------------------*/
113     /**
114      * @brief   check to exist vehicle information name
115      *
116      * @param[in]   s       name of vehicle information
117      * @return  bool    true:exist
118      */
119     /*--------------------------------------------------------------------------*/
120     bool isContainVehicleName(const char *s)
121     {
122         bool rtn = false;
123
124         if (s == NULL)
125             return false;
126         if (getIdx(s) >= 0) {
127             rtn = true;
128         }
129         return rtn;
130     }
131
132     /*--------------------------------------------------------------------------*/
133     /**
134      * @brief   set priority to send vehicle information
135      *
136      * @param[in]   s       name of vehicle information
137      * @param[in]   p       priority
138      * @return  none
139      */
140     /*--------------------------------------------------------------------------*/
141     void setPriority(const char *s, long p)
142     {
143         int idx = 0;
144
145         if (s == NULL)
146             return;
147         if ((idx = getIdx(s)) >= 0) {
148             priority[idx] = p;
149         }
150     }
151
152     /*--------------------------------------------------------------------------*/
153     /**
154      * @brief   get priority to send vehicle information
155      *
156      * @param[in]   s   name of vehicle information
157      * @return  int     priority. if negative value returned, failure
158      */
159     /*--------------------------------------------------------------------------*/
160     long getPriority(const char *s)
161     {
162         int idx = 0;
163         long rtn = 0;
164
165         if (s == NULL)
166             return 0;
167         if ((idx = getIdx(s)) >= 0) {
168             rtn = priority[idx];
169         }
170
171         return rtn;
172     }
173
174   private:
175     /*--------------------------------------------------------------------------*/
176     /**
177      * @brief   get number of vehicle information list
178      *
179      * @param[in]   s       name of vehicle information
180      * @return  int     number of element
181      */
182     /*--------------------------------------------------------------------------*/
183     int getIdx(const char *s)
184     {
185         int limit = length < maxlen ? length : maxlen;
186         int rtn = -1;
187         for (int i = 0; i < limit; i++) {
188             if (!strcmp(name[i], s)) {
189                 rtn = i;
190                 break;
191             }
192         }
193         return rtn;
194     }
195 };
196
197
198 struct VehicleInfo
199 {
200     int nSteeringAngle;
201     int nShiftPos;
202     int nAirconTemp;
203     int nHeadLightPos;
204     int nVelocity;
205     int nDirection;
206     int nWinkerPos;
207     bool bHazard;
208     bool bWinkR;
209     bool bWinkL;
210     double fLng;
211     double fLat;
212     bool bBrake;
213     int nBrakeHydraulicPressure;
214     double dVelocity;
215     int nAccel;
216     int nBrake;
217     bool bHeadLight; // HEAD LIGHT ON(true)/OFF(false) status
218 };
219
220 enum SHIFT_POS
221 {
222     SHIFT_UNKNOWN = 0,
223     PARKING,
224     REVERSE,
225     NEUTRAL,
226     DRIVE,
227     FIRST,
228     SECOND,
229     THIRD,
230     SPORTS
231 };
232
233 enum WINKER_POS
234 {
235     WINKER_UNKNOWN = 0,
236     WINKER_OFF,
237     WINKER_RIGHT,
238     WINKER_LEFT
239 };
240
241 enum AIRCON_TEMP
242 {
243     MAX_TEMP = 300,
244     MIN_TEMP = 160
245 };
246
247 enum HEAD_LIGHT
248 {
249     HL_UNKNOWN = 0,
250     HL_OFF,
251     HL_SMALL,
252     HL_LOW,
253     HL_HIGH
254 };
255
256 enum ProtocolType
257 {
258     dataport_def,
259     ctrlport_def,
260     dataport_cust,
261     ctrlport_cust
262 };
263
264 class CGtCtrl
265 {
266   public:
267             CGtCtrl();
268     virtual ~CGtCtrl();
269
270     CConf myConf;
271     CJoyStick* myJS;
272     bool m_bUseGps;
273     bool m_bDemoRunning;
274
275     bool Initialize();
276     bool Terminate();
277
278     void Run();
279     void Run2();
280     static void signal_handler(int signo);
281
282   private:
283     int m_nJoyStickID;
284
285     bool m_bFirstOpen;
286
287     std::string m_strConfPath;
288
289     VehicleInfo m_stVehicleInfo;
290
291     int m_ambpicomm_port[4];
292     AmbpiCommIF m_ambpicomm_client[4];
293     KeyEventOptMsg_t m_msgOpt;
294     KeyDataMsg_t m_msgDat;
295
296     VehicleInfoNameList m_viList;
297
298     std::list<std::string> m_sendMsgInfo;
299
300     bool LoadConfigJson(const char *);
301     bool LoadConfigAMBJson(const char *, char *, int);
302     bool LoadConfigJsonCommon(JsonReader *);
303     bool LoadConfigJsonCarSim(JsonReader *);
304     void DelJsonObj(JsonParser *, JsonReader *);
305     bool SendVehicleInfo(ProtocolType type, const char *key, bool data);
306     bool SendVehicleInfo(ProtocolType type, const char *key, int data);
307     bool SendVehicleInfo(ProtocolType type, const char *key, int data[],
308                          int len);
309     bool SendVehicleInfo(ProtocolType type, const char *key, double data[],
310                          int len);
311     bool SendVehicleInfo(ProtocolType type, const char *key, char data[],
312                          int len);
313     bool sendVehicleInfo(ProtocolType type, const char *key, void *data,
314                          unsigned int unit_size, int unit_cnt);
315     bool GetConfigValue(JsonReader *, const char *, char *, int);
316     bool GetConfigValue(JsonReader *, const char *, int *, int);
317     bool GetConfigValue(JsonReader *, const char *, double *, int);
318     bool GetConfigValue(JsonReader *, const char *, bool *, int);
319     bool GetConfigValText(JsonReader *, const char *, char *, int);
320     bool GetConfigValInt(JsonReader *, const char *, int *);
321     bool GetConfigValDouble(JsonReader *, const char *, double *);
322     bool GetConfigValBool(JsonReader *, const char *, bool *);
323     void SetMQKeyData(char *buf, unsigned int bufsize, long &mtype,
324                       const char *key, char status[], unsigned int size);
325     void CheckSendResult(int mqid);
326     void LoadRouteList();
327 };
328
329 #endif /* CGTCTRL_H_ */
330 /**
331  * End of File.(CGtCtrl.h)
332  */