2 * Copyright (C) 2012 TOYOTA MOTOR CORPORATION.
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.
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.
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
19 #ifndef CONTROLWEBSOCKET_H_
20 #define CONTROLWEBSOCKET_H_
29 #include "ico-util/ico_uws.h"
31 #include "eventmessage.h"
32 #include "datamessage.h"
35 struct user_datacontainer;
38 * Class that manages the Websocket.
40 class ControlWebsocket {
43 * The protocol used to Websocket communicate.
46 DATA_STANDARD = 0, CONTROL_STANDARD, DATA_CUSTOM, CONTROL_CUSTOM
49 struct user_datacontainer {
50 enum ControlWebsocket::ServerProtocol type;
51 ControlWebsocket *ctrlws;
65 * @param port The port number used by websocket server.
66 * @param type Kind of the protocol.
67 * @return Success : true Failure : false
70 initialize(int port, enum ServerProtocol stype, MWIF *mwif_);
72 * This function sends a message to the MW.
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
81 send(int commid, char *keyeventtype, timeval time, void *data, size_t len);
83 * This function analyzes the received message.
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
93 receive(int commid, char *keyeventtype, timeval recordtime, void *data,
97 receive(int commid, char *data, size_t len);
99 * This function monitors the file descriptor for Websocket Server.
104 * Registration of the socket. This function retrieves the socket information from the socket ID.
106 * @param commid socket id.
107 * @return Success : true Failure : false
110 registSocket(int commid);
112 * Delete the socket. This function removes the socket information from the socket ID.
114 * @param commid socket id.
115 * @return Success : true Failure : false
118 unregistSocket(int commid);
120 * Callback function for libwebsockets
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.
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);
134 * Function for multi-threaded execution.
143 enum ServerProtocol type;
145 ico_uws_context *context;
146 std::map<int, void*> socketmap;
147 EventMessage eventmsg;
150 pthread_mutex_t mutex;
151 char buf[StandardMessage::BUFSIZE];
153 std::vector<pollfd> pollfds;
155 user_datacontainer container;
158 #endif // #ifndef CONTROLWEBSOCKET_H_