Revert "platform: added predicate in CCondition"
[deb_libcec.git] / src / lib / adapter / USBCECAdapterMessage.h
CommitLineData
ef7696f5
LOK
1#pragma once
2/*
3 * This file is part of the libCEC(R) library.
4 *
b492c10e 5 * libCEC(R) is Copyright (C) 2011-2012 Pulse-Eight Limited. All rights reserved.
ef7696f5
LOK
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
24048d57 34#include "../platform/util/StdString.h"
ba65909d 35
ef7696f5
LOK
36namespace CEC
37{
ef7696f5
LOK
38 class CCECAdapterMessage
39 {
40 public:
41 CCECAdapterMessage(void)
42 {
43 Clear();
44 }
45
46 CCECAdapterMessage(const cec_command &command)
47 {
48 Clear();
49
50 //set ack polarity to high when transmitting to the broadcast address
51 //set ack polarity low when transmitting to any other address
52 PushBack(MSGSTART);
53 PushEscaped(MSGCODE_TRANSMIT_ACK_POLARITY);
54 if (command.destination == CECDEVICE_BROADCAST)
55 PushEscaped(CEC_TRUE);
56 else
57 PushEscaped(CEC_FALSE);
58 PushBack(MSGEND);
59
60 // add source and destination
61 PushBack(MSGSTART);
62 PushEscaped(command.opcode_set == 0 ? (uint8_t)MSGCODE_TRANSMIT_EOM : (uint8_t)MSGCODE_TRANSMIT);
63 PushBack(((uint8_t)command.initiator << 4) + (uint8_t)command.destination);
64 PushBack(MSGEND);
65
66 // add opcode
67 if (command.opcode_set == 1)
68 {
69 PushBack(MSGSTART);
70 PushEscaped(command.parameters.IsEmpty() ? (uint8_t)MSGCODE_TRANSMIT_EOM : (uint8_t)MSGCODE_TRANSMIT);
71 PushBack((uint8_t) command.opcode);
72 PushBack(MSGEND);
73
74 // add parameters
75 for (int8_t iPtr = 0; iPtr < command.parameters.size; iPtr++)
76 {
77 PushBack(MSGSTART);
78
79 if (iPtr == command.parameters.size - 1)
80 PushEscaped( MSGCODE_TRANSMIT_EOM);
81 else
82 PushEscaped(MSGCODE_TRANSMIT);
83
84 PushEscaped(command.parameters[iPtr]);
85
86 PushBack(MSGEND);
87 }
88 }
89
90 // set timeout
91 transmit_timeout = command.transmit_timeout;
5dcf9f25 92 //TODO
ef7696f5
LOK
93 }
94
95 CCECAdapterMessage &operator=(const CCECAdapterMessage &msg)
96 {
97 packet = msg.packet;
98 state = msg.state;
99 return *this;
100 }
101
102 CStdString ToString(void) const
103 {
104 CStdString strMsg;
105 if (Size() == 0)
106 {
107 strMsg = "empty message";
108 }
109 else
110 {
111 strMsg = MessageCodeAsString();
112
113 switch (Message())
114 {
115 case MSGCODE_TIMEOUT_ERROR:
116 case MSGCODE_HIGH_ERROR:
117 case MSGCODE_LOW_ERROR:
118 {
119 uint32_t iLine = (Size() >= 3) ? (At(1) << 8) | At(2) : 0;
120 uint32_t iTime = (Size() >= 7) ? (At(3) << 24) | (At(4) << 16) | (At(5) << 8) | At(6) : 0;
121 strMsg.AppendFormat(" line:%u", iLine);
122 strMsg.AppendFormat(" time:%u", iTime);
123 }
124 break;
125 case MSGCODE_FRAME_START:
126 if (Size() >= 2)
127 strMsg.AppendFormat(" initiator:%1x destination:%1x ack:%s %s", Initiator(), Destination(), IsACK() ? "high" : "low", IsEOM() ? "eom" : "");
128 break;
129 case MSGCODE_FRAME_DATA:
130 if (Size() >= 2)
131 strMsg.AppendFormat(" %02x %s", At(1), IsEOM() ? "eom" : "");
132 break;
133 default:
134 break;
135 }
136 }
137
138 return strMsg;
139 }
140
141 CStdString MessageCodeAsString(void) const
142 {
143 CStdString strMsg;
144 switch (Message())
145 {
146 case MSGCODE_NOTHING:
147 strMsg = "NOTHING";
148 break;
149 case MSGCODE_PING:
150 strMsg = "PING";
151 break;
152 case MSGCODE_TIMEOUT_ERROR:
153 strMsg = "TIMEOUT";
154 break;
155 case MSGCODE_HIGH_ERROR:
156 strMsg = "HIGH_ERROR";
157 break;
158 case MSGCODE_LOW_ERROR:
159 strMsg = "LOW_ERROR";
160 break;
161 case MSGCODE_FRAME_START:
162 strMsg = "FRAME_START";
163 break;
164 case MSGCODE_FRAME_DATA:
165 strMsg = "FRAME_DATA";
166 break;
167 case MSGCODE_RECEIVE_FAILED:
168 strMsg = "RECEIVE_FAILED";
169 break;
170 case MSGCODE_COMMAND_ACCEPTED:
171 strMsg = "COMMAND_ACCEPTED";
172 break;
173 case MSGCODE_COMMAND_REJECTED:
174 strMsg = "COMMAND_REJECTED";
175 break;
176 case MSGCODE_SET_ACK_MASK:
177 strMsg = "SET_ACK_MASK";
178 break;
179 case MSGCODE_TRANSMIT:
180 strMsg = "TRANSMIT";
181 break;
182 case MSGCODE_TRANSMIT_EOM:
183 strMsg = "TRANSMIT_EOM";
184 break;
185 case MSGCODE_TRANSMIT_IDLETIME:
186 strMsg = "TRANSMIT_IDLETIME";
187 break;
188 case MSGCODE_TRANSMIT_ACK_POLARITY:
189 strMsg = "TRANSMIT_ACK_POLARITY";
190 break;
191 case MSGCODE_TRANSMIT_LINE_TIMEOUT:
192 strMsg = "TRANSMIT_LINE_TIMEOUT";
193 break;
194 case MSGCODE_TRANSMIT_SUCCEEDED:
195 strMsg = "TRANSMIT_SUCCEEDED";
196 break;
197 case MSGCODE_TRANSMIT_FAILED_LINE:
198 strMsg = "TRANSMIT_FAILED_LINE";
199 break;
200 case MSGCODE_TRANSMIT_FAILED_ACK:
201 strMsg = "TRANSMIT_FAILED_ACK";
202 break;
203 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA:
204 strMsg = "TRANSMIT_FAILED_TIMEOUT_DATA";
205 break;
206 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE:
207 strMsg = "TRANSMIT_FAILED_TIMEOUT_LINE";
208 break;
209 case MSGCODE_FIRMWARE_VERSION:
210 strMsg = "FIRMWARE_VERSION";
211 break;
212 case MSGCODE_START_BOOTLOADER:
213 strMsg = "START_BOOTLOADER";
214 break;
215 case MSGCODE_FRAME_EOM:
216 strMsg = "FRAME_EOM";
217 break;
218 case MSGCODE_FRAME_ACK:
219 strMsg = "FRAME_ACK";
220 break;
dd4efb6d
LOK
221 case MSGCODE_SET_POWERSTATE:
222 strMsg = "SET_POWERSTATE";
223 break;
224 case MSGCODE_SET_CONTROLLED:
225 strMsg = "SET_CONTROLLED";
226 break;
ef7696f5
LOK
227 }
228
229 return strMsg;
230 }
231
232 uint8_t operator[](uint8_t pos) const
233 {
234 return packet[pos];
235 }
236
237 uint8_t At(uint8_t pos) const
238 {
239 return packet[pos];
240 }
241
242 uint8_t Size(void) const
243 {
244 return packet.size;
245 }
246
247 bool IsEmpty(void) const
248 {
249 return packet.IsEmpty();
250 }
251
252 void Clear(void)
253 {
71c4a2f5 254 state = ADAPTER_MESSAGE_STATE_UNKNOWN;
c71891a3 255 transmit_timeout = CEC_DEFAULT_TRANSMIT_TIMEOUT;
ef7696f5 256 packet.Clear();
71c4a2f5
LOK
257 maxTries = CEC_DEFAULT_TRANSMIT_RETRIES + 1;
258 tries = 0;
259 reply = MSGCODE_NOTHING;
260 isTransmission = true;
261 expectControllerAck = true;
7bb4ed43
LOK
262 lineTimeout = 3;
263 retryTimeout = 3;
ef7696f5
LOK
264 }
265
266 void Shift(uint8_t iShiftBy)
267 {
268 packet.Shift(iShiftBy);
269 }
270
271 void PushBack(uint8_t add)
272 {
273 packet.PushBack(add);
274 }
275
276 void PushEscaped(uint8_t byte)
277 {
278 if (byte >= MSGESC)
279 {
280 PushBack(MSGESC);
281 PushBack(byte - ESCOFFSET);
282 }
283 else
284 {
285 PushBack(byte);
286 }
287 }
288
289 cec_adapter_messagecode Message(void) const
290 {
291 return packet.size >= 1 ?
292 (cec_adapter_messagecode) (packet.At(0) & ~(MSGCODE_FRAME_EOM | MSGCODE_FRAME_ACK)) :
293 MSGCODE_NOTHING;
294 }
295
296 bool IsEOM(void) const
297 {
298 return packet.size >= 1 ?
299 (packet.At(0) & MSGCODE_FRAME_EOM) != 0 :
300 false;
301 }
302
303 bool IsACK(void) const
304 {
305 return packet.size >= 1 ?
306 (packet.At(0) & MSGCODE_FRAME_ACK) != 0 :
307 false;
308 }
309
310 bool IsError(void) const
311 {
312 cec_adapter_messagecode code = Message();
313 return (code == MSGCODE_HIGH_ERROR ||
314 code == MSGCODE_LOW_ERROR ||
315 code == MSGCODE_RECEIVE_FAILED ||
316 code == MSGCODE_COMMAND_REJECTED ||
317 code == MSGCODE_TRANSMIT_LINE_TIMEOUT ||
318 code == MSGCODE_TRANSMIT_FAILED_LINE ||
319 code == MSGCODE_TRANSMIT_FAILED_ACK ||
320 code == MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA ||
321 code == MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE);
322 }
323
324 bool NeedsRetry(void) const
325 {
326 return reply == MSGCODE_NOTHING ||
327 reply == MSGCODE_RECEIVE_FAILED ||
328 reply == MSGCODE_TIMEOUT_ERROR ||
329 reply == MSGCODE_TRANSMIT_FAILED_LINE ||
330 reply == MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA ||
331 reply == MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE ||
332 reply == MSGCODE_TRANSMIT_LINE_TIMEOUT;
333 }
334
335 cec_logical_address Initiator(void) const
336 {
337 return packet.size >= 2 ?
338 (cec_logical_address) (packet.At(1) >> 4) :
339 CECDEVICE_UNKNOWN;
340 }
341
342 cec_logical_address Destination(void) const
343 {
344 return packet.size >= 2 ?
345 (cec_logical_address) (packet.At(1) & 0xF) :
346 CECDEVICE_UNKNOWN;
347 }
348
349 uint8_t maxTries;
350 uint8_t tries;
351 cec_adapter_messagecode reply;
352 cec_datapacket packet;
353 cec_adapter_message_state state;
354 int32_t transmit_timeout;
5dcf9f25 355 bool isTransmission;
71c4a2f5 356 bool expectControllerAck;
7bb4ed43
LOK
357 uint8_t lineTimeout;
358 uint8_t retryTimeout;
ef7696f5
LOK
359 PLATFORM::CMutex mutex;
360 PLATFORM::CCondition condition;
361 };
362}