use ZoneList for zones
[profile/ivi/ico-vic-amb-plugin.git] / src / controlwebsocket.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 CONTROLWEBSOCKET_H_
20 #define CONTROLWEBSOCKET_H_
21
22 #include <poll.h>
23 #include <pthread.h>
24 #include <sys/time.h>
25
26 #include <map>
27 #include <vector>
28
29 #include "ico-util/ico_uws.h"
30
31 #include "eventmessage.h"
32 #include "datamessage.h"
33
34 class MWIF;
35 struct user_datacontainer;
36
37 /**
38  * Class that manages the Websocket.
39  */
40 class ControlWebsocket {
41 public:
42     /**
43      * The protocol used to Websocket communicate.
44      */
45     enum ServerProtocol {
46         DATA_STANDARD = 0, CONTROL_STANDARD, DATA_CUSTOM, CONTROL_CUSTOM
47     };
48
49     struct user_datacontainer {
50         enum ControlWebsocket::ServerProtocol type;
51         ControlWebsocket *ctrlws;
52         MWIF *mwif;
53     };
54     /**
55      * Constructor.
56      */
57     ControlWebsocket();
58     /**
59      * Destructor.
60      */
61     ~ControlWebsocket();
62     /**
63      * Initialization.
64      *
65      * @param port The port number used by websocket server.
66      * @param type Kind of the protocol.
67      * @return Success : true Failure : false
68      */
69     bool
70     initialize(int port, enum ServerProtocol stype, MWIF *mwif_);
71     /**
72      * This function sends a message to the MW.
73      * 
74      * @param commid socket id.
75      * @param keyeventtype The name of vehicle information of MW.
76      * @param time Time of the message.
77      * @param data Data of the message.
78      * @return Success : true Failure : false
79      */
80     bool
81     send(int commid, char *keyeventtype, timeval time, void *data, size_t len);
82     /**
83      * This function analyzes the received message.
84      *
85      * @param commid socket id.
86      * @param keyeventtype The name of vehicle information of MW.
87      * @param time Time of the message.
88      * @param data Data of the message.
89      * @param len Length of the message.
90      * @return Success : true Failure : false
91      */
92     bool
93     receive(int commid, char *keyeventtype, timeval recordtime, void *data,
94             size_t len);
95
96     bool
97     receive(int commid, char *data, size_t len);
98     /**
99      * This function monitors the file descriptor for Websocket Server.
100      */
101     void
102     observation();
103     /**
104      * Registration of the socket. This function retrieves the socket information from the socket ID.
105      * 
106      * @param commid socket id.
107      * @return Success : true Failure : false
108      */
109     bool
110     registSocket(int commid);
111     /**
112      * Delete the socket. This function removes the socket information from the socket ID.
113      *
114      * @param commid socket id.
115      * @return Success : true Failure : false
116      */
117     bool
118     unregistSocket(int commid);
119     /**
120      * Callback function for libwebsockets
121      *
122      * @param context Websockets context
123      * @param wsi Opaque websocket instance pointer
124      * @param reason The reason for the call
125      * @param user Pointer to per-session user data allocated by library.
126      * @param in Pointer used for some callback reasons
127      * @param len Length set for some callback reasons.
128      */
129     static void
130     callback_receive(const struct ico_uws_context *context,
131                      const ico_uws_evt_e event, const void *id,
132                      const ico_uws_detail *detail, void *user_data);
133     /**
134      * Function for multi-threaded execution.
135      */
136     static void *
137     run(void *arg);
138
139     /**
140      * Instance of MWIF.
141      */
142 protected:
143     enum ServerProtocol type;
144 private:
145     ico_uws_context *context;
146     std::map<int, void*> socketmap;
147     EventMessage eventmsg;
148     DataMessage datamsg;
149     pthread_t threadid;
150     pthread_mutex_t mutex;
151     char buf[StandardMessage::BUFSIZE];
152     char iface[128];
153     std::vector<pollfd> pollfds;
154     MWIF *mwif;
155     user_datacontainer container;
156 };
157
158 #endif // #ifndef CONTROLWEBSOCKET_H_