cec: send a feature abort again for all unhandled commands, removed statics, refactor...
[deb_libcec.git] / src / lib / adapter / USBCECAdapterMessage.cpp
CommitLineData
a75e3a5a
LOK
1/*
2 * This file is part of the libCEC(R) library.
3 *
4 * libCEC(R) is Copyright (C) 2011-2012 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 "USBCECAdapterMessage.h"
34#include "../LibCEC.h"
35
36using namespace CEC;
37using namespace PLATFORM;
38
39CCECAdapterMessage::CCECAdapterMessage(void)
40{
41 Clear();
42}
43
33dd87a9 44CCECAdapterMessage::CCECAdapterMessage(const cec_command &command, uint8_t iLineTimeout /* = 3 */)
a75e3a5a
LOK
45{
46 Clear();
47
48 //set ack polarity to high when transmitting to the broadcast address
49 //set ack polarity low when transmitting to any other address
50 PushBack(MSGSTART);
51 PushEscaped(MSGCODE_TRANSMIT_ACK_POLARITY);
52 if (command.destination == CECDEVICE_BROADCAST)
53 PushEscaped(CEC_TRUE);
54 else
55 PushEscaped(CEC_FALSE);
56 PushBack(MSGEND);
57
58 // add source and destination
59 PushBack(MSGSTART);
60 PushEscaped(command.opcode_set == 0 ? (uint8_t)MSGCODE_TRANSMIT_EOM : (uint8_t)MSGCODE_TRANSMIT);
61 PushBack(((uint8_t)command.initiator << 4) + (uint8_t)command.destination);
62 PushBack(MSGEND);
63
64 // add opcode
65 if (command.opcode_set == 1)
66 {
67 PushBack(MSGSTART);
68 PushEscaped(command.parameters.IsEmpty() ? (uint8_t)MSGCODE_TRANSMIT_EOM : (uint8_t)MSGCODE_TRANSMIT);
69 PushBack((uint8_t) command.opcode);
70 PushBack(MSGEND);
71
72 // add parameters
73 for (int8_t iPtr = 0; iPtr < command.parameters.size; iPtr++)
74 {
75 PushBack(MSGSTART);
76
77 if (iPtr == command.parameters.size - 1)
78 PushEscaped( MSGCODE_TRANSMIT_EOM);
79 else
80 PushEscaped(MSGCODE_TRANSMIT);
81
82 PushEscaped(command.parameters[iPtr]);
83
84 PushBack(MSGEND);
85 }
86 }
87
88 // set timeout
89 transmit_timeout = command.transmit_timeout;
90
a75e3a5a 91 lineTimeout = iLineTimeout;
a75e3a5a
LOK
92}
93
94CStdString CCECAdapterMessage::ToString(void) const
95{
96 CStdString strMsg;
97 if (Size() == 0)
98 {
99 strMsg = "empty message";
100 }
101 else
102 {
103 strMsg = ToString(Message());
104
105 switch (Message())
106 {
107 case MSGCODE_TIMEOUT_ERROR:
108 case MSGCODE_HIGH_ERROR:
109 case MSGCODE_LOW_ERROR:
110 {
111 uint32_t iLine = (Size() >= 4) ? (At(2) << 8) | At(3) : 0;
112 uint32_t iTime = (Size() >= 8) ? (At(4) << 24) | (At(5) << 16) | (At(6) << 8) | At(7) : 0;
113 strMsg.AppendFormat(" line:%u", iLine);
114 strMsg.AppendFormat(" time:%u", iTime);
115 }
116 break;
117 case MSGCODE_FRAME_START:
118 if (Size() >= 3)
119 strMsg.AppendFormat(" initiator:%1x destination:%1x ack:%s %s", Initiator(), Destination(), IsACK() ? "high" : "low", IsEOM() ? "eom" : "");
120 break;
121 case MSGCODE_FRAME_DATA:
122 if (Size() >= 3)
123 strMsg.AppendFormat(" %02x %s", At(2), IsEOM() ? "eom" : "");
124 break;
125 default:
95acc41b
LOK
126 for (uint8_t iPtr = 2; iPtr < Size(); iPtr++)
127 if (At(iPtr) != MSGEND)
128 strMsg.AppendFormat(" %02x", At(iPtr));
a75e3a5a
LOK
129 break;
130 }
131 }
132
133 return strMsg;
134}
135
136const char *CCECAdapterMessage::ToString(cec_adapter_messagecode msgCode)
137{
138 switch (msgCode)
139 {
140 case MSGCODE_NOTHING:
141 return "NOTHING";
142 case MSGCODE_PING:
143 return "PING";
144 case MSGCODE_TIMEOUT_ERROR:
145 return "TIMEOUT";
146 case MSGCODE_HIGH_ERROR:
147 return "HIGH_ERROR";
148 case MSGCODE_LOW_ERROR:
149 return "LOW_ERROR";
150 case MSGCODE_FRAME_START:
151 return "FRAME_START";
152 case MSGCODE_FRAME_DATA:
153 return "FRAME_DATA";
154 case MSGCODE_RECEIVE_FAILED:
155 return "RECEIVE_FAILED";
156 case MSGCODE_COMMAND_ACCEPTED:
157 return "COMMAND_ACCEPTED";
158 case MSGCODE_COMMAND_REJECTED:
159 return "COMMAND_REJECTED";
160 case MSGCODE_SET_ACK_MASK:
161 return "SET_ACK_MASK";
162 case MSGCODE_TRANSMIT:
163 return "TRANSMIT";
164 case MSGCODE_TRANSMIT_EOM:
165 return "TRANSMIT_EOM";
166 case MSGCODE_TRANSMIT_IDLETIME:
167 return "TRANSMIT_IDLETIME";
168 case MSGCODE_TRANSMIT_ACK_POLARITY:
169 return "TRANSMIT_ACK_POLARITY";
170 case MSGCODE_TRANSMIT_LINE_TIMEOUT:
171 return "TRANSMIT_LINE_TIMEOUT";
172 case MSGCODE_TRANSMIT_SUCCEEDED:
173 return "TRANSMIT_SUCCEEDED";
174 case MSGCODE_TRANSMIT_FAILED_LINE:
175 return "TRANSMIT_FAILED_LINE";
176 case MSGCODE_TRANSMIT_FAILED_ACK:
177 return "TRANSMIT_FAILED_ACK";
178 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA:
179 return "TRANSMIT_FAILED_TIMEOUT_DATA";
180 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE:
181 return "TRANSMIT_FAILED_TIMEOUT_LINE";
182 case MSGCODE_FIRMWARE_VERSION:
183 return "FIRMWARE_VERSION";
184 case MSGCODE_START_BOOTLOADER:
185 return "START_BOOTLOADER";
186 case MSGCODE_FRAME_EOM:
187 return "FRAME_EOM";
188 case MSGCODE_FRAME_ACK:
189 return "FRAME_ACK";
b2f56d35
LOK
190 case MSGCODE_GET_BUILDDATE:
191 return "GET_BUILDDATE";
a75e3a5a
LOK
192 case MSGCODE_SET_CONTROLLED:
193 return "SET_CONTROLLED";
194 case MSGCODE_GET_AUTO_ENABLED:
195 return "GET_AUTO_ENABLED";
196 case MSGCODE_SET_AUTO_ENABLED:
197 return "SET_AUTO_ENABLED";
198 case MSGCODE_GET_DEFAULT_LOGICAL_ADDRESS:
199 return "GET_DEFAULT_LOGICAL_ADDRESS";
200 case MSGCODE_SET_DEFAULT_LOGICAL_ADDRESS:
201 return "SET_DEFAULT_LOGICAL_ADDRESS";
202 case MSGCODE_GET_LOGICAL_ADDRESS_MASK:
203 return "GET_LOGICAL_ADDRESS_MASK";
204 case MSGCODE_SET_LOGICAL_ADDRESS_MASK:
205 return "SET_LOGICAL_ADDRESS_MASK";
206 case MSGCODE_GET_PHYSICAL_ADDRESS:
207 return "GET_PHYSICAL_ADDRESS";
208 case MSGCODE_SET_PHYSICAL_ADDRESS:
209 return "SET_PHYSICAL_ADDRESS";
210 case MSGCODE_GET_DEVICE_TYPE:
211 return "GET_DEVICE_TYPE";
212 case MSGCODE_SET_DEVICE_TYPE:
213 return "SET_DEVICE_TYPE";
214 case MSGCODE_GET_HDMI_VERSION:
215 return "GET_HDMI_VERSION";
216 case MSGCODE_SET_HDMI_VERSION:
217 return "SET_HDMI_VERSION";
218 case MSGCODE_GET_OSD_NAME:
219 return "GET_OSD_NAME";
220 case MSGCODE_SET_OSD_NAME:
221 return "SET_OSD_NAME";
222 case MSGCODE_WRITE_EEPROM:
223 return "WRITE_EEPROM";
b2f56d35
LOK
224 default:
225 break;
a75e3a5a
LOK
226 }
227
228 return "unknown";
229}
230
231uint8_t CCECAdapterMessage::operator[](uint8_t pos) const
232{
233 return pos < packet.size ? packet[pos] : 0;
234}
235
236uint8_t CCECAdapterMessage::At(uint8_t pos) const
237{
238 return pos < packet.size ? packet[pos] : 0;
239}
240
241uint8_t CCECAdapterMessage::Size(void) const
242{
243 return packet.size;
244}
245
246bool CCECAdapterMessage::IsEmpty(void) const
247{
248 return packet.IsEmpty();
249}
250
251void CCECAdapterMessage::Clear(void)
252{
253 state = ADAPTER_MESSAGE_STATE_UNKNOWN;
254 transmit_timeout = CEC_DEFAULT_TRANSMIT_TIMEOUT;
255 response.Clear();
256 packet.Clear();
a75e3a5a 257 lineTimeout = 3;
a75e3a5a
LOK
258 bNextByteIsEscaped = false;
259}
260
261void CCECAdapterMessage::Shift(uint8_t iShiftBy)
262{
263 packet.Shift(iShiftBy);
264}
265
266void CCECAdapterMessage::Append(CCECAdapterMessage &data)
267{
268 Append(data.packet);
269}
270
271void CCECAdapterMessage::Append(cec_datapacket &data)
272{
273 for (uint8_t iPtr = 0; iPtr < data.size; iPtr++)
274 PushBack(data[iPtr]);
275}
276
277void CCECAdapterMessage::PushBack(uint8_t byte)
278{
279 packet.PushBack(byte);
280}
281
282void CCECAdapterMessage::PushEscaped(uint8_t byte)
283{
284 if (byte >= MSGESC)
285 {
286 PushBack(MSGESC);
287 PushBack(byte - ESCOFFSET);
288 }
289 else
290 {
291 PushBack(byte);
292 }
293}
294
295bool CCECAdapterMessage::PushReceivedByte(uint8_t byte)
296{
297 if (byte == MSGSTART)
298 {
299 if (HasStartMessage())
300 {
004b8382 301 //TODO CLibCEC::AddLog(CEC_LOG_WARNING, "received MSGSTART before MSGEND, removing previous buffer contents");
a75e3a5a
LOK
302 Clear();
303 }
304 PushBack(byte);
305 }
306 else
307 {
308 if (bNextByteIsEscaped)
309 {
310 PushBack(byte + (uint8_t)ESCOFFSET);
311 bNextByteIsEscaped = false;
312 }
313 else if (byte == MSGESC)
314 bNextByteIsEscaped = true;
315 else
316 PushBack(byte);
317 }
318
319 return byte == MSGEND;
320}
321
322cec_adapter_messagecode CCECAdapterMessage::Message(void) const
323{
324 return packet.size >= 2 ?
325 (cec_adapter_messagecode) (packet.At(1) & ~(MSGCODE_FRAME_EOM | MSGCODE_FRAME_ACK)) :
326 MSGCODE_NOTHING;
327}
328
329bool CCECAdapterMessage::IsTranmission(void) const
330{
331 cec_adapter_messagecode msgCode = Message();
332 return msgCode == MSGCODE_FRAME_ACK ||
333 msgCode == MSGCODE_FRAME_DATA ||
334 msgCode == MSGCODE_FRAME_EOM ||
335 msgCode == MSGCODE_FRAME_START ||
336 msgCode == MSGCODE_HIGH_ERROR ||
337 msgCode == MSGCODE_LOW_ERROR ||
338 msgCode == MSGCODE_RECEIVE_FAILED ||
339 msgCode == MSGCODE_TRANSMIT_ACK_POLARITY ||
340 msgCode == MSGCODE_TRANSMIT_EOM ||
341 msgCode == MSGCODE_TRANSMIT_FAILED_ACK ||
342 msgCode == MSGCODE_TRANSMIT_FAILED_LINE ||
343 msgCode == MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA ||
344 msgCode == MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE ||
345 msgCode == MSGCODE_TRANSMIT_LINE_TIMEOUT ||
346 msgCode == MSGCODE_TRANSMIT_SUCCEEDED;
347}
348
349bool CCECAdapterMessage::IsEOM(void) const
350{
351 return packet.size >= 2 ?
352 (packet.At(1) & MSGCODE_FRAME_EOM) != 0 :
353 false;
354}
355
356bool CCECAdapterMessage::IsACK(void) const
357{
358 return packet.size >= 2 ?
359 (packet.At(1) & MSGCODE_FRAME_ACK) != 0 :
360 false;
361}
362
363bool CCECAdapterMessage::IsError(void) const
364{
365 cec_adapter_messagecode code = Message();
366 return (code == MSGCODE_HIGH_ERROR ||
367 code == MSGCODE_LOW_ERROR ||
368 code == MSGCODE_RECEIVE_FAILED ||
369 code == MSGCODE_COMMAND_REJECTED ||
370 code == MSGCODE_TRANSMIT_LINE_TIMEOUT ||
371 code == MSGCODE_TRANSMIT_FAILED_LINE ||
372 code == MSGCODE_TRANSMIT_FAILED_ACK ||
373 code == MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA ||
374 code == MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE);
375}
376
377bool CCECAdapterMessage::NeedsRetry(void) const
378{
379 return Reply() == MSGCODE_NOTHING ||
380 Reply() == MSGCODE_RECEIVE_FAILED ||
381 Reply() == MSGCODE_TIMEOUT_ERROR ||
382 Reply() == MSGCODE_TRANSMIT_FAILED_LINE ||
383 Reply() == MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA ||
384 Reply() == MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE ||
385 Reply() == MSGCODE_TRANSMIT_LINE_TIMEOUT;
386}
387
388cec_logical_address CCECAdapterMessage::Initiator(void) const
389{
390 return packet.size >= 3 ?
391 (cec_logical_address) (packet.At(2) >> 4) :
392 CECDEVICE_UNKNOWN;
393}
394
395cec_logical_address CCECAdapterMessage::Destination(void) const
396{
397 return packet.size >= 3 ?
398 (cec_logical_address) (packet.At(2) & 0xF) :
399 CECDEVICE_UNKNOWN;
400}
401
402bool CCECAdapterMessage::HasStartMessage(void) const
403{
404 return packet.size >= 1 && packet.At(0) == MSGSTART;
405}
406
407bool CCECAdapterMessage::PushToCecCommand(cec_command &command) const
408{
409 // empty message
410 if (IsEmpty())
411 return false;
412
413 cec_adapter_messagecode msgCode = Message();
414 if (msgCode == MSGCODE_FRAME_START)
415 {
416 command.Clear();
417 if (Size() >= 3)
418 {
419 command.initiator = Initiator();
420 command.destination = Destination();
421 command.ack = IsACK();
422 command.eom = IsEOM();
423 }
424 return IsEOM() && !IsError();
425 }
426 else if (msgCode == MSGCODE_FRAME_DATA)
427 {
428 if (Size() >= 3)
429 {
430 command.PushBack(At(2));
431 command.eom = IsEOM();
432 }
433 return IsEOM() && !IsError();
434 }
435
436 return false;
437}
438
439cec_adapter_messagecode CCECAdapterMessage::Reply(void) const
440{
441 return response.size >= 2 ?
442 (cec_adapter_messagecode) (response.At(1) & ~(MSGCODE_FRAME_EOM | MSGCODE_FRAME_ACK)) :
443 MSGCODE_NOTHING;
444}