do not wake the system when exiting away mode
[deb_libcec.git] / src / lib / adapter / Pulse-Eight / USBCECAdapterMessage.cpp
... / ...
CommitLineData
1/*
2 * This file is part of the libCEC(R) library.
3 *
4 * libCEC(R) is Copyright (C) 2011-2013 Pulse-Eight Limited. All rights reserved.
5 * libCEC(R) is an original work, containing original code.
6 *
7 * libCEC(R) is a trademark of Pulse-Eight Limited.
8 *
9 * This program is dual-licensed; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 *
23 *
24 * Alternatively, you can license this library under a commercial license,
25 * please contact Pulse-Eight Licensing for more information.
26 *
27 * For more information contact:
28 * Pulse-Eight Licensing <license@pulse-eight.com>
29 * http://www.pulse-eight.com/
30 * http://www.pulse-eight.net/
31 */
32
33#include "env.h"
34#include "USBCECAdapterMessage.h"
35
36#include "lib/LibCEC.h"
37#include "lib/platform/util/StdString.h"
38
39using namespace CEC;
40using namespace PLATFORM;
41
42CCECAdapterMessage::CCECAdapterMessage(void)
43{
44 Clear();
45}
46
47CCECAdapterMessage::CCECAdapterMessage(const cec_command &command, uint8_t iLineTimeout /* = 3 */)
48{
49 Clear();
50
51 //set ack polarity to high when transmitting to the broadcast address
52 //set ack polarity low when transmitting to any other address
53 PushBack(MSGSTART);
54 PushEscaped(MSGCODE_TRANSMIT_ACK_POLARITY);
55 if (command.destination == CECDEVICE_BROADCAST)
56 PushEscaped(CEC_TRUE);
57 else
58 PushEscaped(CEC_FALSE);
59 PushBack(MSGEND);
60
61 // add source and destination
62 PushBack(MSGSTART);
63 PushEscaped(command.opcode_set == 0 ? (uint8_t)MSGCODE_TRANSMIT_EOM : (uint8_t)MSGCODE_TRANSMIT);
64 PushBack(((uint8_t)command.initiator << 4) + (uint8_t)command.destination);
65 PushBack(MSGEND);
66
67 // add opcode
68 if (command.opcode_set == 1)
69 {
70 PushBack(MSGSTART);
71 PushEscaped(command.parameters.IsEmpty() ? (uint8_t)MSGCODE_TRANSMIT_EOM : (uint8_t)MSGCODE_TRANSMIT);
72 PushBack((uint8_t) command.opcode);
73 PushBack(MSGEND);
74
75 // add parameters
76 for (int8_t iPtr = 0; iPtr < command.parameters.size; iPtr++)
77 {
78 PushBack(MSGSTART);
79
80 if (iPtr == command.parameters.size - 1)
81 PushEscaped( MSGCODE_TRANSMIT_EOM);
82 else
83 PushEscaped(MSGCODE_TRANSMIT);
84
85 PushEscaped(command.parameters[iPtr]);
86
87 PushBack(MSGEND);
88 }
89 }
90
91 // set timeout
92 transmit_timeout = command.transmit_timeout;
93
94 lineTimeout = iLineTimeout;
95}
96
97std::string CCECAdapterMessage::ToString(void) const
98{
99 CStdString strMsg;
100 if (Size() == 0)
101 {
102 strMsg = "empty message";
103 }
104 else
105 {
106 strMsg = ToString(Message());
107
108 switch (Message())
109 {
110 case MSGCODE_TIMEOUT_ERROR:
111 case MSGCODE_HIGH_ERROR:
112 case MSGCODE_LOW_ERROR:
113 {
114 uint32_t iLine = (Size() >= 4) ? (At(2) << 8) | At(3) : 0;
115 uint32_t iTime = (Size() >= 8) ? (At(4) << 24) | (At(5) << 16) | (At(6) << 8) | At(7) : 0;
116 strMsg.AppendFormat(" line:%u", iLine);
117 strMsg.AppendFormat(" time:%u", iTime);
118 }
119 break;
120 case MSGCODE_FRAME_START:
121 if (Size() >= 3)
122 strMsg.AppendFormat(" initiator:%1x destination:%1x ack:%s %s", Initiator(), Destination(), IsACK() ? "high" : "low", IsEOM() ? "eom" : "");
123 break;
124 case MSGCODE_FRAME_DATA:
125 if (Size() >= 3)
126 strMsg.AppendFormat(" %02x %s", At(2), IsEOM() ? "eom" : "");
127 break;
128 default:
129 if (Size() >= 2 && (Message() == MSGCODE_COMMAND_ACCEPTED || Message() == MSGCODE_COMMAND_REJECTED))
130 strMsg.AppendFormat(": %s", ToString((cec_adapter_messagecode)At(2)));
131 else
132 {
133 for (uint8_t iPtr = 2; iPtr < Size(); iPtr++)
134 if (At(iPtr) != MSGEND)
135 strMsg.AppendFormat(" %02x", At(iPtr));
136 }
137 break;
138 }
139 }
140
141 return std::string(strMsg.c_str());
142}
143
144const char *CCECAdapterMessage::ToString(cec_adapter_messagecode msgCode)
145{
146 switch (msgCode)
147 {
148 case MSGCODE_NOTHING:
149 return "NOTHING";
150 case MSGCODE_PING:
151 return "PING";
152 case MSGCODE_TIMEOUT_ERROR:
153 return "TIMEOUT";
154 case MSGCODE_HIGH_ERROR:
155 return "HIGH_ERROR";
156 case MSGCODE_LOW_ERROR:
157 return "LOW_ERROR";
158 case MSGCODE_FRAME_START:
159 return "FRAME_START";
160 case MSGCODE_FRAME_DATA:
161 return "FRAME_DATA";
162 case MSGCODE_RECEIVE_FAILED:
163 return "RECEIVE_FAILED";
164 case MSGCODE_COMMAND_ACCEPTED:
165 return "COMMAND_ACCEPTED";
166 case MSGCODE_COMMAND_REJECTED:
167 return "COMMAND_REJECTED";
168 case MSGCODE_SET_ACK_MASK:
169 return "SET_ACK_MASK";
170 case MSGCODE_TRANSMIT:
171 return "TRANSMIT";
172 case MSGCODE_TRANSMIT_EOM:
173 return "TRANSMIT_EOM";
174 case MSGCODE_TRANSMIT_IDLETIME:
175 return "TRANSMIT_IDLETIME";
176 case MSGCODE_TRANSMIT_ACK_POLARITY:
177 return "CEC transmission";
178 case MSGCODE_TRANSMIT_LINE_TIMEOUT:
179 return "TRANSMIT_LINE_TIMEOUT";
180 case MSGCODE_TRANSMIT_SUCCEEDED:
181 return "TRANSMIT_SUCCEEDED";
182 case MSGCODE_TRANSMIT_FAILED_LINE:
183 return "TRANSMIT_FAILED_LINE";
184 case MSGCODE_TRANSMIT_FAILED_ACK:
185 return "TRANSMIT_FAILED_ACK";
186 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA:
187 return "TRANSMIT_FAILED_TIMEOUT_DATA";
188 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE:
189 return "TRANSMIT_FAILED_TIMEOUT_LINE";
190 case MSGCODE_FIRMWARE_VERSION:
191 return "FIRMWARE_VERSION";
192 case MSGCODE_START_BOOTLOADER:
193 return "START_BOOTLOADER";
194 case MSGCODE_FRAME_EOM:
195 return "FRAME_EOM";
196 case MSGCODE_FRAME_ACK:
197 return "FRAME_ACK";
198 case MSGCODE_GET_BUILDDATE:
199 return "GET_BUILDDATE";
200 case MSGCODE_SET_CONTROLLED:
201 return "SET_CONTROLLED";
202 case MSGCODE_GET_AUTO_ENABLED:
203 return "GET_AUTO_ENABLED";
204 case MSGCODE_SET_AUTO_ENABLED:
205 return "SET_AUTO_ENABLED";
206 case MSGCODE_GET_DEFAULT_LOGICAL_ADDRESS:
207 return "GET_DEFAULT_LOGICAL_ADDRESS";
208 case MSGCODE_SET_DEFAULT_LOGICAL_ADDRESS:
209 return "SET_DEFAULT_LOGICAL_ADDRESS";
210 case MSGCODE_GET_LOGICAL_ADDRESS_MASK:
211 return "GET_LOGICAL_ADDRESS_MASK";
212 case MSGCODE_SET_LOGICAL_ADDRESS_MASK:
213 return "SET_LOGICAL_ADDRESS_MASK";
214 case MSGCODE_GET_PHYSICAL_ADDRESS:
215 return "GET_PHYSICAL_ADDRESS";
216 case MSGCODE_SET_PHYSICAL_ADDRESS:
217 return "SET_PHYSICAL_ADDRESS";
218 case MSGCODE_GET_DEVICE_TYPE:
219 return "GET_DEVICE_TYPE";
220 case MSGCODE_SET_DEVICE_TYPE:
221 return "SET_DEVICE_TYPE";
222 case MSGCODE_GET_HDMI_VERSION:
223 return "GET_HDMI_VERSION";
224 case MSGCODE_SET_HDMI_VERSION:
225 return "SET_HDMI_VERSION";
226 case MSGCODE_GET_OSD_NAME:
227 return "GET_OSD_NAME";
228 case MSGCODE_SET_OSD_NAME:
229 return "SET_OSD_NAME";
230 case MSGCODE_WRITE_EEPROM:
231 return "WRITE_EEPROM";
232 case MSGCODE_GET_ADAPTER_TYPE:
233 return "GET_ADAPTER_TYPE";
234 default:
235 break;
236 }
237
238 return "unknown";
239}
240
241uint8_t CCECAdapterMessage::operator[](uint8_t pos) const
242{
243 return pos < packet.size ? packet[pos] : 0;
244}
245
246uint8_t CCECAdapterMessage::At(uint8_t pos) const
247{
248 return pos < packet.size ? packet[pos] : 0;
249}
250
251uint8_t CCECAdapterMessage::Size(void) const
252{
253 return packet.size;
254}
255
256bool CCECAdapterMessage::IsEmpty(void) const
257{
258 return packet.IsEmpty();
259}
260
261void CCECAdapterMessage::Clear(void)
262{
263 state = ADAPTER_MESSAGE_STATE_UNKNOWN;
264 transmit_timeout = CEC_DEFAULT_TRANSMIT_TIMEOUT;
265 response.Clear();
266 packet.Clear();
267 lineTimeout = 3;
268 bNextByteIsEscaped = false;
269 bFireAndForget = false;
270}
271
272void CCECAdapterMessage::Shift(uint8_t iShiftBy)
273{
274 packet.Shift(iShiftBy);
275}
276
277void CCECAdapterMessage::Append(CCECAdapterMessage &data)
278{
279 Append(data.packet);
280}
281
282void CCECAdapterMessage::Append(cec_datapacket &data)
283{
284 for (uint8_t iPtr = 0; iPtr < data.size; iPtr++)
285 PushBack(data[iPtr]);
286}
287
288void CCECAdapterMessage::PushBack(uint8_t byte)
289{
290 packet.PushBack(byte);
291}
292
293void CCECAdapterMessage::PushEscaped(uint8_t byte)
294{
295 if (byte >= MSGESC)
296 {
297 PushBack(MSGESC);
298 PushBack(byte - ESCOFFSET);
299 }
300 else
301 {
302 PushBack(byte);
303 }
304}
305
306bool CCECAdapterMessage::PushReceivedByte(uint8_t byte)
307{
308 if (byte == MSGSTART)
309 {
310 if (HasStartMessage())
311 {
312 //TODO CLibCEC::AddLog(CEC_LOG_WARNING, "received MSGSTART before MSGEND, removing previous buffer contents");
313 Clear();
314 }
315 PushBack(byte);
316 }
317 else
318 {
319 if (bNextByteIsEscaped)
320 {
321 PushBack(byte + (uint8_t)ESCOFFSET);
322 bNextByteIsEscaped = false;
323 }
324 else if (byte == MSGESC)
325 bNextByteIsEscaped = true;
326 else
327 PushBack(byte);
328 }
329
330 return byte == MSGEND;
331}
332
333cec_adapter_messagecode CCECAdapterMessage::Message(void) const
334{
335 return packet.size >= 2 ?
336 (cec_adapter_messagecode) (packet.At(1) & ~(MSGCODE_FRAME_EOM | MSGCODE_FRAME_ACK)) :
337 MSGCODE_NOTHING;
338}
339
340cec_adapter_messagecode CCECAdapterMessage::ResponseTo(void) const
341{
342 return packet.size >= 3 ?
343 (cec_adapter_messagecode) (packet.At(2) & ~(MSGCODE_FRAME_EOM | MSGCODE_FRAME_ACK)) :
344 MSGCODE_NOTHING;
345}
346
347bool CCECAdapterMessage::IsTranmission(void) const
348{
349 cec_adapter_messagecode msgCode = Message();
350 return msgCode == MSGCODE_FRAME_ACK ||
351 msgCode == MSGCODE_FRAME_DATA ||
352 msgCode == MSGCODE_FRAME_EOM ||
353 msgCode == MSGCODE_FRAME_START ||
354 msgCode == MSGCODE_HIGH_ERROR ||
355 msgCode == MSGCODE_LOW_ERROR ||
356 msgCode == MSGCODE_RECEIVE_FAILED ||
357 msgCode == MSGCODE_TRANSMIT_ACK_POLARITY ||
358 msgCode == MSGCODE_TRANSMIT_EOM ||
359 msgCode == MSGCODE_TRANSMIT_FAILED_ACK ||
360 msgCode == MSGCODE_TRANSMIT_FAILED_LINE ||
361 msgCode == MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA ||
362 msgCode == MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE ||
363 msgCode == MSGCODE_TRANSMIT_LINE_TIMEOUT ||
364 msgCode == MSGCODE_TRANSMIT_SUCCEEDED;
365}
366
367bool CCECAdapterMessage::IsEOM(void) const
368{
369 return packet.size >= 2 ?
370 (packet.At(1) & MSGCODE_FRAME_EOM) != 0 :
371 false;
372}
373
374bool CCECAdapterMessage::IsACK(void) const
375{
376 return packet.size >= 2 ?
377 (packet.At(1) & MSGCODE_FRAME_ACK) != 0 :
378 false;
379}
380
381bool CCECAdapterMessage::MessageCodeIsError(const cec_adapter_messagecode code)
382{
383 return (code == MSGCODE_HIGH_ERROR ||
384 code == MSGCODE_LOW_ERROR ||
385 code == MSGCODE_RECEIVE_FAILED ||
386 code == MSGCODE_COMMAND_REJECTED ||
387 code == MSGCODE_TRANSMIT_LINE_TIMEOUT ||
388 code == MSGCODE_TRANSMIT_FAILED_LINE ||
389 code == MSGCODE_TRANSMIT_FAILED_ACK ||
390 code == MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA ||
391 code == MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE);
392}
393
394bool CCECAdapterMessage::IsError(void) const
395{
396 return MessageCodeIsError(Message());
397}
398
399bool CCECAdapterMessage::ReplyIsError(void) const
400{
401 return MessageCodeIsError(Reply());
402}
403
404bool CCECAdapterMessage::NeedsRetry(void) const
405{
406 return Reply() == MSGCODE_NOTHING ||
407 Reply() == MSGCODE_RECEIVE_FAILED ||
408 Reply() == MSGCODE_TIMEOUT_ERROR ||
409 Reply() == MSGCODE_TRANSMIT_FAILED_LINE ||
410 Reply() == MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA ||
411 Reply() == MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE ||
412 Reply() == MSGCODE_TRANSMIT_LINE_TIMEOUT;
413}
414
415cec_logical_address CCECAdapterMessage::Initiator(void) const
416{
417 return packet.size >= 3 ?
418 (cec_logical_address) (packet.At(2) >> 4) :
419 CECDEVICE_UNKNOWN;
420}
421
422cec_logical_address CCECAdapterMessage::Destination(void) const
423{
424 return packet.size >= 3 ?
425 (cec_logical_address) (packet.At(2) & 0xF) :
426 CECDEVICE_UNKNOWN;
427}
428
429bool CCECAdapterMessage::HasStartMessage(void) const
430{
431 return packet.size >= 1 && packet.At(0) == MSGSTART;
432}
433
434bool CCECAdapterMessage::PushToCecCommand(cec_command &command) const
435{
436 // empty message
437 if (IsEmpty())
438 return false;
439
440 cec_adapter_messagecode msgCode = Message();
441 if (msgCode == MSGCODE_FRAME_START)
442 {
443 command.Clear();
444 if (Size() >= 3)
445 {
446 command.initiator = Initiator();
447 command.destination = Destination();
448 command.ack = IsACK();
449 command.eom = IsEOM();
450 }
451 return IsEOM() && !IsError();
452 }
453 else if (msgCode == MSGCODE_FRAME_DATA)
454 {
455 if (Size() >= 3)
456 {
457 command.PushBack(At(2));
458 command.eom = IsEOM();
459 }
460 return IsEOM() && !IsError();
461 }
462
463 return false;
464}
465
466cec_adapter_messagecode CCECAdapterMessage::Reply(void) const
467{
468 return response.size >= 2 ?
469 (cec_adapter_messagecode) (response.At(1) & ~(MSGCODE_FRAME_EOM | MSGCODE_FRAME_ACK)) :
470 MSGCODE_NOTHING;
471}