cec: moved WaitForTransmitSucceeded() to CAdapterCommunication
[deb_libcec.git] / src / lib / adapter / AdapterMessage.h
CommitLineData
ef7696f5
LOK
1#pragma once
2/*
3 * This file is part of the libCEC(R) library.
4 *
5 * libCEC(R) is Copyright (C) 2011 Pulse-Eight Limited. All rights reserved.
6 * libCEC(R) is an original work, containing original code.
7 *
8 * libCEC(R) is a trademark of Pulse-Eight Limited.
9 *
10 * This program is dual-licensed; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 *
24 *
25 * Alternatively, you can license this library under a commercial license,
26 * please contact Pulse-Eight Licensing for more information.
27 *
28 * For more information contact:
29 * Pulse-Eight Licensing <license@pulse-eight.com>
30 * http://www.pulse-eight.com/
31 * http://www.pulse-eight.net/
32 */
33
34namespace CEC
35{
36 typedef enum cec_adapter_message_state
37 {
38 ADAPTER_MESSAGE_STATE_UNKNOWN = 0,
39 ADAPTER_MESSAGE_STATE_WAITING,
40 ADAPTER_MESSAGE_STATE_SENT,
41 ADAPTER_MESSAGE_STATE_RECEIVED,
42 ADAPTER_MESSAGE_STATE_ERROR
43 } cec_adapter_message_state;
44
45
46 class CCECAdapterMessage
47 {
48 public:
49 CCECAdapterMessage(void)
50 {
51 Clear();
52 }
53
54 CCECAdapterMessage(const cec_command &command)
55 {
56 Clear();
57
58 //set ack polarity to high when transmitting to the broadcast address
59 //set ack polarity low when transmitting to any other address
60 PushBack(MSGSTART);
61 PushEscaped(MSGCODE_TRANSMIT_ACK_POLARITY);
62 if (command.destination == CECDEVICE_BROADCAST)
63 PushEscaped(CEC_TRUE);
64 else
65 PushEscaped(CEC_FALSE);
66 PushBack(MSGEND);
67
68 // add source and destination
69 PushBack(MSGSTART);
70 PushEscaped(command.opcode_set == 0 ? (uint8_t)MSGCODE_TRANSMIT_EOM : (uint8_t)MSGCODE_TRANSMIT);
71 PushBack(((uint8_t)command.initiator << 4) + (uint8_t)command.destination);
72 PushBack(MSGEND);
73
74 // add opcode
75 if (command.opcode_set == 1)
76 {
77 PushBack(MSGSTART);
78 PushEscaped(command.parameters.IsEmpty() ? (uint8_t)MSGCODE_TRANSMIT_EOM : (uint8_t)MSGCODE_TRANSMIT);
79 PushBack((uint8_t) command.opcode);
80 PushBack(MSGEND);
81
82 // add parameters
83 for (int8_t iPtr = 0; iPtr < command.parameters.size; iPtr++)
84 {
85 PushBack(MSGSTART);
86
87 if (iPtr == command.parameters.size - 1)
88 PushEscaped( MSGCODE_TRANSMIT_EOM);
89 else
90 PushEscaped(MSGCODE_TRANSMIT);
91
92 PushEscaped(command.parameters[iPtr]);
93
94 PushBack(MSGEND);
95 }
96 }
97
98 // set timeout
99 transmit_timeout = command.transmit_timeout;
100 }
101
102 CCECAdapterMessage &operator=(const CCECAdapterMessage &msg)
103 {
104 packet = msg.packet;
105 state = msg.state;
106 return *this;
107 }
108
109 CStdString ToString(void) const
110 {
111 CStdString strMsg;
112 if (Size() == 0)
113 {
114 strMsg = "empty message";
115 }
116 else
117 {
118 strMsg = MessageCodeAsString();
119
120 switch (Message())
121 {
122 case MSGCODE_TIMEOUT_ERROR:
123 case MSGCODE_HIGH_ERROR:
124 case MSGCODE_LOW_ERROR:
125 {
126 uint32_t iLine = (Size() >= 3) ? (At(1) << 8) | At(2) : 0;
127 uint32_t iTime = (Size() >= 7) ? (At(3) << 24) | (At(4) << 16) | (At(5) << 8) | At(6) : 0;
128 strMsg.AppendFormat(" line:%u", iLine);
129 strMsg.AppendFormat(" time:%u", iTime);
130 }
131 break;
132 case MSGCODE_FRAME_START:
133 if (Size() >= 2)
134 strMsg.AppendFormat(" initiator:%1x destination:%1x ack:%s %s", Initiator(), Destination(), IsACK() ? "high" : "low", IsEOM() ? "eom" : "");
135 break;
136 case MSGCODE_FRAME_DATA:
137 if (Size() >= 2)
138 strMsg.AppendFormat(" %02x %s", At(1), IsEOM() ? "eom" : "");
139 break;
140 default:
141 break;
142 }
143 }
144
145 return strMsg;
146 }
147
148 CStdString MessageCodeAsString(void) const
149 {
150 CStdString strMsg;
151 switch (Message())
152 {
153 case MSGCODE_NOTHING:
154 strMsg = "NOTHING";
155 break;
156 case MSGCODE_PING:
157 strMsg = "PING";
158 break;
159 case MSGCODE_TIMEOUT_ERROR:
160 strMsg = "TIMEOUT";
161 break;
162 case MSGCODE_HIGH_ERROR:
163 strMsg = "HIGH_ERROR";
164 break;
165 case MSGCODE_LOW_ERROR:
166 strMsg = "LOW_ERROR";
167 break;
168 case MSGCODE_FRAME_START:
169 strMsg = "FRAME_START";
170 break;
171 case MSGCODE_FRAME_DATA:
172 strMsg = "FRAME_DATA";
173 break;
174 case MSGCODE_RECEIVE_FAILED:
175 strMsg = "RECEIVE_FAILED";
176 break;
177 case MSGCODE_COMMAND_ACCEPTED:
178 strMsg = "COMMAND_ACCEPTED";
179 break;
180 case MSGCODE_COMMAND_REJECTED:
181 strMsg = "COMMAND_REJECTED";
182 break;
183 case MSGCODE_SET_ACK_MASK:
184 strMsg = "SET_ACK_MASK";
185 break;
186 case MSGCODE_TRANSMIT:
187 strMsg = "TRANSMIT";
188 break;
189 case MSGCODE_TRANSMIT_EOM:
190 strMsg = "TRANSMIT_EOM";
191 break;
192 case MSGCODE_TRANSMIT_IDLETIME:
193 strMsg = "TRANSMIT_IDLETIME";
194 break;
195 case MSGCODE_TRANSMIT_ACK_POLARITY:
196 strMsg = "TRANSMIT_ACK_POLARITY";
197 break;
198 case MSGCODE_TRANSMIT_LINE_TIMEOUT:
199 strMsg = "TRANSMIT_LINE_TIMEOUT";
200 break;
201 case MSGCODE_TRANSMIT_SUCCEEDED:
202 strMsg = "TRANSMIT_SUCCEEDED";
203 break;
204 case MSGCODE_TRANSMIT_FAILED_LINE:
205 strMsg = "TRANSMIT_FAILED_LINE";
206 break;
207 case MSGCODE_TRANSMIT_FAILED_ACK:
208 strMsg = "TRANSMIT_FAILED_ACK";
209 break;
210 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA:
211 strMsg = "TRANSMIT_FAILED_TIMEOUT_DATA";
212 break;
213 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE:
214 strMsg = "TRANSMIT_FAILED_TIMEOUT_LINE";
215 break;
216 case MSGCODE_FIRMWARE_VERSION:
217 strMsg = "FIRMWARE_VERSION";
218 break;
219 case MSGCODE_START_BOOTLOADER:
220 strMsg = "START_BOOTLOADER";
221 break;
222 case MSGCODE_FRAME_EOM:
223 strMsg = "FRAME_EOM";
224 break;
225 case MSGCODE_FRAME_ACK:
226 strMsg = "FRAME_ACK";
227 break;
228 }
229
230 return strMsg;
231 }
232
233 uint8_t operator[](uint8_t pos) const
234 {
235 return packet[pos];
236 }
237
238 uint8_t At(uint8_t pos) const
239 {
240 return packet[pos];
241 }
242
243 uint8_t Size(void) const
244 {
245 return packet.size;
246 }
247
248 bool IsEmpty(void) const
249 {
250 return packet.IsEmpty();
251 }
252
253 void Clear(void)
254 {
255 state = ADAPTER_MESSAGE_STATE_UNKNOWN;
256 transmit_timeout = 0;
257 packet.Clear();
258 maxTries = CEC_DEFAULT_TRANSMIT_RETRIES + 1;
259 tries = 0;
260 reply = MSGCODE_NOTHING;
261 }
262
263 void Shift(uint8_t iShiftBy)
264 {
265 packet.Shift(iShiftBy);
266 }
267
268 void PushBack(uint8_t add)
269 {
270 packet.PushBack(add);
271 }
272
273 void PushEscaped(uint8_t byte)
274 {
275 if (byte >= MSGESC)
276 {
277 PushBack(MSGESC);
278 PushBack(byte - ESCOFFSET);
279 }
280 else
281 {
282 PushBack(byte);
283 }
284 }
285
286 cec_adapter_messagecode Message(void) const
287 {
288 return packet.size >= 1 ?
289 (cec_adapter_messagecode) (packet.At(0) & ~(MSGCODE_FRAME_EOM | MSGCODE_FRAME_ACK)) :
290 MSGCODE_NOTHING;
291 }
292
293 bool IsEOM(void) const
294 {
295 return packet.size >= 1 ?
296 (packet.At(0) & MSGCODE_FRAME_EOM) != 0 :
297 false;
298 }
299
300 bool IsACK(void) const
301 {
302 return packet.size >= 1 ?
303 (packet.At(0) & MSGCODE_FRAME_ACK) != 0 :
304 false;
305 }
306
307 bool IsError(void) const
308 {
309 cec_adapter_messagecode code = Message();
310 return (code == MSGCODE_HIGH_ERROR ||
311 code == MSGCODE_LOW_ERROR ||
312 code == MSGCODE_RECEIVE_FAILED ||
313 code == MSGCODE_COMMAND_REJECTED ||
314 code == MSGCODE_TRANSMIT_LINE_TIMEOUT ||
315 code == MSGCODE_TRANSMIT_FAILED_LINE ||
316 code == MSGCODE_TRANSMIT_FAILED_ACK ||
317 code == MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA ||
318 code == MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE);
319 }
320
321 bool NeedsRetry(void) const
322 {
323 return reply == MSGCODE_NOTHING ||
324 reply == MSGCODE_RECEIVE_FAILED ||
325 reply == MSGCODE_TIMEOUT_ERROR ||
326 reply == MSGCODE_TRANSMIT_FAILED_LINE ||
327 reply == MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA ||
328 reply == MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE ||
329 reply == MSGCODE_TRANSMIT_LINE_TIMEOUT;
330 }
331
332 cec_logical_address Initiator(void) const
333 {
334 return packet.size >= 2 ?
335 (cec_logical_address) (packet.At(1) >> 4) :
336 CECDEVICE_UNKNOWN;
337 }
338
339 cec_logical_address Destination(void) const
340 {
341 return packet.size >= 2 ?
342 (cec_logical_address) (packet.At(1) & 0xF) :
343 CECDEVICE_UNKNOWN;
344 }
345
346 uint8_t maxTries;
347 uint8_t tries;
348 cec_adapter_messagecode reply;
349 cec_datapacket packet;
350 cec_adapter_message_state state;
351 int32_t transmit_timeout;
352 PLATFORM::CMutex mutex;
353 PLATFORM::CCondition condition;
354 };
355}