ff428865107b0a0560bc0f2fb72ee0f918730352
[profile/ivi/ico-vic-amb-plugin.git] / src / mwinterface.h
1 /**
2  * Copyright (C) 2012  TOYOTA MOTOR CORPORATION.
3  * 
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  * 
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  * 
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17  * 
18  */
19 #ifndef MWINTERFACE_H
20 #define MWINTERFACE_H
21
22 #include <pthread.h>
23 #include <sys/time.h>
24
25 #include <map>
26 #include <string>
27 #include <vector>
28
29 #include <libwebsockets.h>
30
31 #include "config.h"
32 #include "controlwebsocket.h"
33 #include "messageformat.h"
34
35 class VICCommunicator;
36
37 /**
38  * Vehicle information of MW
39  */
40 struct MWVehicleInfo {
41     std::string name;
42     timeval recordtime;
43     char status[STATUSSIZE];
44     int statussize;
45     vector<int> delimeterposition;
46 };
47
48 /**
49  * This class manages the vehicle information to be notified to MW.
50  */
51 class MWNotifyInfo {
52 public:
53     /**
54      * Conditions of vehicle information changes notification.
55      */
56     struct NotifyOpt {
57         std::string name;
58         int interval;
59         char mask[STATUSSIZE];
60         timeval lastChanged;
61     };
62     /**
63      * Constructor.
64      */
65     MWNotifyInfo();
66     /**
67      * Destructor.
68      */
69     ~MWNotifyInfo();
70     /**
71      * This function registers the change notifiction contitions.
72      *
73      * @param name The name of vehicle information of MW
74      * @param interval Transmission interval change notification.
75      * @param sense Information to detect.
76      * @param mask For change detection mask.
77      * @return Success : true Failure : false
78      */
79     bool
80     insert(std::string name, int interval, int sense, char *mask);
81     /**
82      * This function removes the notification conditions.
83      *
84      * @param name The name of vehicle information of MW.
85      * @param mask For change detection mask.
86      * @return Success : true Failure : false
87      */
88     bool
89     erase(std::string name, char *mask);
90     /**
91      * This function removes the all notification conditions with a matching name.
92      *
93      * @param name The name of vehicle information of MW.
94      * @return Success : true Failure : false
95      */
96     bool
97     eraseAllMask(std::string name);
98     /**
99      * This function will check whether the vehicle information is applicable to the notification conditions.
100      * If applicable, update the internal table.
101      *
102      * @param name The name of vehicle information of MW.
103      * @param olddata Value before the change.
104      * @param newdata Value after the change.
105      * @param newtime Time after the change.
106      * @return Success : true Failure : false
107      */
108     bool
109     checkNotify(std::string name, char *olddata, char *newdata,
110                 timeval newtime);
111 private:
112     enum GetType {
113         NORMAL, ALLNAME, ALLCOMMID, ALLDATA
114     };
115     std::map<std::string, std::vector<NotifyOpt> > notifyMap;
116     char nochangemask[STATUSSIZE];
117     pthread_mutex_t mutex;
118 };
119
120 /**
121  * Interface of MW and Plugin.
122  */
123 class MWIF {
124 public:
125     /**
126      * Constructor.
127      */
128     MWIF();
129     /**
130      * Destructor.
131      */
132     ~MWIF();
133     /**
134      * Initialization.
135      *
136      * @param comm Instance of VICCommunicator.
137      * @param conf Instance of Config.
138      * @return Success : true Failure : false
139      */
140     bool
141     initialize(VICCommunicator *com, Config *conf);
142     /**
143      * This function issues a request to send vehicle information to the MW.
144      *
145      * @param vehicleinfo Vehicle information of MW.
146      */
147     void
148     send(MWVehicleInfo *vehicleinfo);
149     /**
150      * This function distributes the incoming mesage from the MW.
151      *
152      * @param commid socket id.
153      * @param keyeventtype The name of vehicle information of MW.
154      * @param recordtime Time of the message.
155      * @param data Binary data other than intersection.
156      * @param len Length of the message.
157      */
158     void
159     recvRawdata(int commid, char *keyeventtype, timeval recordtime, void *data,
160                 size_t len);
161     /*
162      * This function is a function of the received vehicle information message.
163      *
164      * @param type Type of the received message.
165      * @param commid socket id.
166      * @param keyeventtype The name of vehicle information of MW.
167      * @param recordtime Time of the message.
168      * @param data Binary data other than intersection.
169      * @param len Length of the message.
170      */
171     void
172     recvMessage(MessageType type, int commid, char *keyeventtype,
173                 timeval recordtime, void *data, size_t len);
174     /**
175      * Mapping the instance and destination socket ID.
176      *
177      * @param commid Socket ID.
178      */
179
180     void
181     registDestination(int commid);
182     /**
183      * Unmapped instances and destination socket ID.
184      * 
185      * @param commid Socket ID.
186      */
187     void
188     unregistDestination(int commid);
189 private:
190
191     void
192     sendMessage(int commid, CommonStatus status, MWVehicleInfo *vehicleinfo);
193     void
194     createThread(PortInfo *portinfo);
195     MWVehicleInfo *
196     find(std::string name);
197     void
198     procSetMessage(int commid, char *keyeventtype, timeval recordtime,
199                    DataOpt *data, size_t len);
200     void
201     procGetMessage(int commid, char *keyeventtype, timeval recordtime,
202                    EventOpt *data, size_t len);
203     void
204     procCallbackMessage(int commid, char *keyeventtype, timeval recordtime,
205                         EventOpt *data, size_t len);
206
207     static const int SERVERNUM = 4;
208     std::vector<MWVehicleInfo> vehicleinfoArray;
209     ControlWebsocket *websocketserver[SERVERNUM];
210     VICCommunicator *communicator;
211     std::map<int, ControlWebsocket::ServerProtocol> websocketservermap;
212     std::map<int, MWNotifyInfo> mwnotifyinfomap;
213 };
214 #endif // MWINTERFACE_H