remove hard coded path into CMake files
[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 "ambconfig.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     bool operator==(const MWVehicleInfo& b) {
48         return (this->name == b.name);
49     }
50 };
51
52 /**
53  * This class manages the vehicle information to be notified to MW.
54  */
55 class MWNotifyInfo {
56 public:
57     /**
58      * Conditions of vehicle information changes notification.
59      */
60     struct NotifyOpt {
61         std::string name;
62         int interval;
63         char mask[STATUSSIZE];
64         timeval lastChanged;
65     };
66     /**
67      * Constructor.
68      */
69     MWNotifyInfo();
70     /**
71      * Destructor.
72      */
73     ~MWNotifyInfo();
74     /**
75      * This function registers the change notifiction contitions.
76      *
77      * @param name The name of vehicle information of MW
78      * @param interval Transmission interval change notification.
79      * @param sense Information to detect.
80      * @param mask For change detection mask.
81      * @return Success : true Failure : false
82      */
83     bool
84     insert(std::string name, int interval, int sense, char *mask);
85     /**
86      * This function removes the notification conditions.
87      *
88      * @param name The name of vehicle information of MW.
89      * @param mask For change detection mask.
90      * @return Success : true Failure : false
91      */
92     bool
93     erase(std::string name, char *mask);
94     /**
95      * This function removes the all notification conditions with a matching name.
96      *
97      * @param name The name of vehicle information of MW.
98      * @return Success : true Failure : false
99      */
100     bool
101     eraseAllMask(std::string name);
102     /**
103      * This function will check whether the vehicle information is applicable to the notification conditions.
104      * If applicable, update the internal table.
105      *
106      * @param name The name of vehicle information of MW.
107      * @param olddata Value before the change.
108      * @param newdata Value after the change.
109      * @param newtime Time after the change.
110      * @return Success : true Failure : false
111      */
112     bool
113     checkNotify(std::string name, char *olddata, char *newdata,
114                 timeval newtime);
115 private:
116     enum GetType {
117         NORMAL, ALLNAME, ALLCOMMID, ALLDATA
118     };
119     std::map<std::string, std::vector<NotifyOpt> > notifyMap;
120     char nochangemask[STATUSSIZE];
121     pthread_mutex_t mutex;
122 };
123
124 /**
125  * Interface of MW and Plugin.
126  */
127 class MWIF {
128 public:
129     /**
130      * Constructor.
131      */
132     MWIF();
133     /**
134      * Destructor.
135      */
136     ~MWIF();
137     /**
138      * Initialization.
139      *
140      * @param comm Instance of VICCommunicator.
141      * @param conf Instance of Config.
142      * @return Success : true Failure : false
143      */
144     bool
145     initialize(VICCommunicator *com, AMBConfig *conf);
146     /**
147      * This function issues a request to send vehicle information to the MW.
148      *
149      * @param vehicleinfo Vehicle information of MW.
150      */
151     void
152     send(MWVehicleInfo *vehicleinfo);
153     /**
154      * This function distributes the incoming mesage from the MW.
155      *
156      * @param commid socket id.
157      * @param keyeventtype The name of vehicle information of MW.
158      * @param recordtime Time of the message.
159      * @param data Binary data other than intersection.
160      * @param len Length of the message.
161      */
162     void
163     recvRawdata(int commid, char *keyeventtype, timeval recordtime, void *data,
164                 size_t len);
165     /*
166      * This function is a function of the received vehicle information message.
167      *
168      * @param type Type of the received message.
169      * @param commid socket id.
170      * @param keyeventtype The name of vehicle information of MW.
171      * @param recordtime Time of the message.
172      * @param data Binary data other than intersection.
173      * @param len Length of the message.
174      */
175     void
176     recvMessage(MessageType type, int commid, char *keyeventtype,
177                 timeval recordtime, void *data, size_t len);
178     /**
179      * Mapping the instance and destination socket ID.
180      *
181      * @param commid Socket ID.
182      */
183
184     void
185     registDestination(ControlWebsocket::ServerProtocol type, int commid);
186     /**
187      * Unmapped instances and destination socket ID.
188      * 
189      * @param commid Socket ID.
190      */
191     void
192     unregistDestination(ControlWebsocket::ServerProtocol type, int commid);
193 private:
194
195     void
196     sendMessage(int commid, CommonStatus status, MWVehicleInfo *vehicleinfo);
197     void
198     createThread(PortInfo *portinfo);
199     MWVehicleInfo *
200     find(std::string name);
201     void
202     procSetMessage(int commid, MWVehicleInfo *vi, char *keyeventtype, timeval recordtime,
203                    DataOpt *data, size_t len);
204     void
205     procGetMessage(int commid, char *keyeventtype, timeval recordtime,
206                    EventOpt *data, size_t len);
207     void
208     procCallbackMessage(int commid, char *keyeventtype, timeval recordtime,
209                         EventOpt *data, size_t len);
210
211     static const int SERVERNUM = 4;
212     std::vector<MWVehicleInfo> vehicleinfoArray;
213     ControlWebsocket *websocketserver[SERVERNUM];
214     VICCommunicator *communicator;
215     std::map<int, ControlWebsocket::ServerProtocol> websocketservermap;
216     std::map<int, MWNotifyInfo> mwnotifyinfomap;
217     pthread_mutex_t mutex_socketmap;
218 };
219 #endif // MWINTERFACE_H