BugFix: A establishment of websockets fails.
[profile/ivi/ico-vic-carsimulator.git] / src / AmbpiComm.h
1 /*
2  * Copyright (c) 2013, TOYOTA MOTOR CORPORATION.
3  *
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
7  *
8  */
9 /**
10  * @brief   AMB plug-in communication I/F
11  */
12
13 #ifndef _ICO_VIC_AMBPICOMM_H_
14 #define _ICO_VIC_AMBPICOMM_H_
15
16 #include <sys/time.h>
17 #include <string>
18 #include "ico-util/ico_uws.h"
19
20 const int MsgQueueMaxMsgSize = 128;
21
22 struct KeyEventOptMsg_t
23 {
24     char KeyEventType[64];
25     struct timeval recordtime;
26     struct KeyEventOpt
27     {
28         int common;
29         int sense;
30         int event_mask;
31     } eventopt;
32 };
33
34 struct KeyDataMsg_t
35 {
36     char KeyEventType[64];
37     struct timeval recordtime;
38     struct KeyData
39     {
40         int common_status;
41         char status[];
42     } data;
43 };
44
45 class AmbpiCommRecvQueue
46 {
47   public:
48         AmbpiCommRecvQueue();
49         AmbpiCommRecvQueue(int queuesize);
50         ~AmbpiCommRecvQueue();
51     bool push(char *data, int datasize);
52     bool front(char *buf) const;
53     bool empty() const;
54     void pop();
55     int size() const;
56
57     static const int maxdatasize = sizeof(KeyDataMsg_t) + MsgQueueMaxMsgSize;
58   private:
59     void init();
60
61     int maxqueuesize;
62     int msize;
63     char (*mdata)[maxdatasize];
64     int *mdatasize;
65 };
66
67 class AmbpiCommIF
68 {
69   public:
70     AmbpiCommIF();
71     AmbpiCommIF(const char* uri, const char* protocolName);
72     ~AmbpiCommIF();
73     bool start(const char* uri, const char* protocolName);
74     bool send(const char *msg, const int size);
75     bool recv(char *msg, bool fbolcking);
76     bool poll();
77     static void *loop(void *arg);
78     bool isContextMatch(const ico_uws_context* context) const;
79 //    bool threadCondWait();
80     void event_cb(const ico_uws_evt_e event, const void *id,
81                   const ico_uws_detail *detail, void *user_data);
82     void reset_ercode();
83     char *getM_id()
84     {
85         return (char *)m_id;
86     };
87   private:
88     bool init(const char* uri, const char* protocolName);
89
90     bool isready;
91     ico_uws_context* m_context;
92     pthread_t  m_threadid;
93     pthread_mutex_t m_mutex;
94     pthread_cond_t m_cond;
95     char m_sendbuf[AmbpiCommRecvQueue::maxdatasize];
96     AmbpiCommRecvQueue m_queue;
97     std::string m_uri; // URI
98     std::string m_pNm; // protocol name
99     ico_uws_error_e m_ercode;
100     void* m_id;
101 };
102
103 /**
104  * @brief context compare
105  * @param context 
106  * @return true:agree / false:not agree
107  */
108 inline bool AmbpiCommIF::isContextMatch(const ico_uws_context* context) const
109 {
110     if (context == m_context) {
111         return true;
112     }
113     return false;
114 }
115
116 /**
117  * @brief thread wait
118  * @return true:success / false:fail
119  *//*
120 inline bool AmbpiCommIF::threadCondWait()
121 {
122     if (0 != pthread_cond_wait(&m_cond, &m_mutex)) {
123         return false;
124     }
125     return true;
126 }
127 */
128 /**
129  * @brief reset error code
130  */
131 inline void AmbpiCommIF::reset_ercode()
132 {
133     m_ercode = ICO_UWS_ERR_UNKNOWN;
134 }
135
136 #endif //#ifndef _ICO_VIC_AMBPICOMM_H_