2 * Copyright (c) 2013, TOYOTA MOTOR CORPORATION.
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
10 //==========================================================================
12 * @file CicoOSClient.cpp
14 * @brief This file implementation of CicoOSClient class
16 //==========================================================================
21 #include <boost/property_tree/ptree.hpp>
22 #include <boost/property_tree/json_parser.hpp>
23 #include <boost/foreach.hpp>
24 #include <boost/optional.hpp>
25 #include <sys/types.h>
28 using namespace boost::property_tree;
31 #include "CicoOSClient.h"
34 #include "ico_syc_error.h"
35 #include "ico_syc_msg_cmd_def.h"
37 //==========================================================================
38 // private static variable
39 //==========================================================================
40 CicoOSClient* CicoOSClient::ms_myInstance = NULL;
42 //--------------------------------------------------------------------------
44 * @brief default constructor
46 //--------------------------------------------------------------------------
47 CicoOSClient::CicoOSClient()
48 :m_uwsContext(NULL), m_id(NULL), m_seqNum(seqMin)
52 //--------------------------------------------------------------------------
56 //--------------------------------------------------------------------------
57 CicoOSClient::~CicoOSClient()
59 if (NULL != m_uwsContext) {
60 ico_uws_close(m_uwsContext);
64 //--------------------------------------------------------------------------
66 * @brief get CicoOSClient instance
68 * @return CicoOSClient instance
70 //--------------------------------------------------------------------------
72 CicoOSClient::getInstance()
74 if (NULL == ms_myInstance) {
75 ms_myInstance = new CicoOSClient();
81 //--------------------------------------------------------------------------
85 * @param [in] port websocket port
86 * @param [in] protocol websocket protocol name
88 * @return ICO_SYC_EOK on success, other on error
89 * @retval ICO_SYC_EOK success
90 * @retval ICO_SYC_ENOSYS error(connection fail)
92 //--------------------------------------------------------------------------
94 CicoOSClient::connect()
96 ICO_TRA("CicoOSClient::connect Enter");
99 m_uwsContext = ico_uws_create_context(DEF_WS_CLI_HS_PORT,
100 DEF_WS_CLI_HS_PROTOCOL);
101 if (NULL == m_uwsContext) {
102 ICO_ERR("ico_uws_create_context() failed.");
103 ICO_TRA("CicoOSClient::connect Leave false");
108 int ret = ico_uws_set_event_cb(m_uwsContext, uwsReceiveEventCB,
110 if (ret != ICO_UWS_ERR_NONE) {
111 ICO_ERR("ico_uws_set_event_cb() failed(%d).", ret);
112 ICO_TRA("CicoOSClient::connect Leave false");
116 ecore_timer_add(timerCount, CicoOSClient::ecoreTimerCB, this);
118 ICO_TRA("CicoOSClient::connect Leave ture");
123 //--------------------------------------------------------------------------
125 * @brief control sequence number
127 //--------------------------------------------------------------------------
128 void CicoOSClient::updateSeqNum()
131 if (seqMax < m_seqNum) {
136 //--------------------------------------------------------------------------
138 * @brief get sequence number
140 //--------------------------------------------------------------------------
141 int CicoOSClient::getSeqNum() const
146 //--------------------------------------------------------------------------
148 * @brief send message to application client
150 * @param [in] appid application id of destination
151 * @param [in] msg message
153 * @return ICO_SYC_EOK on success, other on error
155 //--------------------------------------------------------------------------
157 CicoOSClient::sendLaunchMessage(const string& op_app)
159 ICO_TRA("CicoOSClient::sendMessage Enter(%s)", op_app.c_str());
160 if ((NULL == m_uwsContext) && (NULL == m_id)) {
161 ICO_TRA("CicoOSClient::sendMessage Leave(false)");
164 ico_uws_service(m_uwsContext);
167 pt.put("command", MSG_CMD_WIN_CHANGE);
168 pt.put("source.pid", (int)getpid());
169 pt.put("source.seq_no", getSeqNum());
171 pt.put("arg.appid", op_app);
172 pt.put("arg.zone", "Center");
173 pt.put("arg.visible", 1);
175 write_json(ss, pt, false);
178 size_t l = m.length();
180 strcpy(b, m.c_str());
181 ICO_DBG("msg:%d, %s", l, b);
182 ico_uws_send(m_uwsContext, (void*)m_id, (unsigned char*)b, l);
183 ico_uws_service(m_uwsContext);
188 ICO_TRA("CicoOSClient::sendMessage Leave(true)");
192 //--------------------------------------------------------------------------
194 * @brief websocket utility callback function
196 * @param [in] context context
197 * @param [in] event event kinds
198 * @param [in] id client id
199 * @param [in] detail event detail
200 * @param [in] data user data
202 //--------------------------------------------------------------------------
204 CicoOSClient::uwsReceiveEventCB(const struct ico_uws_context* context,
205 const ico_uws_evt_e event,
207 const ico_uws_detail* detail,
210 if (NULL == user_data) {
211 ICO_ERR("user_data is NULL");
215 CicoOSClient* cosc = static_cast<CicoOSClient*>(user_data);
216 cosc->receiveEventCB(context, event, id, detail, user_data);
219 //--------------------------------------------------------------------------
221 * @brief websocket callback function
223 * @param [in] context websocket context
224 * @param [in] event changed event
225 * @param [in] id source applicatin id
226 * @param [in] detail event detail information
227 * @param [in] user_data user data
229 //--------------------------------------------------------------------------
231 CicoOSClient::receiveEventCB(const struct ico_uws_context* context,
232 const ico_uws_evt_e event,
234 const ico_uws_detail* detail,
237 // ICO_TRA("CicoOSClient::receiveEventCB Enter");
240 case ICO_UWS_EVT_CLOSE:
241 ICO_DBG(">>>RECV ICO_UWS_EVT_CLOSE(id=%p)", id);
242 // ICO_TRA("CicoOSClient::receiveEventCB Leave");
244 case ICO_UWS_EVT_ERROR:
245 ICO_DBG(">>>RECV ICO_UWS_EVT_ERROR(id=%p, err=%d)",
246 id, detail->_ico_uws_error.code);
247 // ICO_TRA("CicoOSClient::receiveEventCB Leave");
254 ICO_DBG(">>>CHANGE id %x -> %x", m_id, id);
259 case ICO_UWS_EVT_OPEN:
260 ICO_DBG(">>>RECV ICO_UWS_EVT_OPEN(id=%p)", id);
262 case ICO_UWS_EVT_CLOSE:
263 ICO_DBG(">>>RECV ICO_UWS_EVT_CLOSE(id=%p)", id);
265 case ICO_UWS_EVT_RECEIVE:
267 ICO_DBG(">>>RECV ICO_UWS_EVT_RECEIVE(id=%p, msg=%s, len=%d)",
268 id, (char *)detail->_ico_uws_message.recv_data,
269 detail->_ico_uws_message.recv_len);
272 case ICO_UWS_EVT_ADD_FD:
273 ICO_DBG(">>>RECV ICO_UWS_EVT_ADD_FD(id=%p, fd=%d)",
274 id, detail->_ico_uws_fd.fd);
276 case ICO_UWS_EVT_DEL_FD:
277 ICO_DBG(">>>RECV ICO_UWS_EVT_DEL_FD(id=%p, fd=%d)",
278 id, detail->_ico_uws_fd.fd);
283 // ICO_TRA("CicoOSClient::receiveEventCB Leave");
287 CicoOSClient::ecoreTimerCB(void *data)
289 CicoOSClient* cosc = (CicoOSClient*)data;
291 return ECORE_CALLBACK_RENEW;
295 CicoOSClient::refresh()
297 if (NULL == m_uwsContext) {
298 ICO_TRA("NG refresh");
301 ico_uws_service(m_uwsContext);
305 // vim:set expandtab ts=4 sw=4: