8b9569da0f3e06afdf284cb17d22939e9d119c36
[deb_libcec.git] / src / lib / adapter / USBCECAdapterMessage.cpp
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
36 using namespace CEC;
37 using namespace PLATFORM;
38
39 CCECAdapterMessage::CCECAdapterMessage(void)
40 {
41 Clear();
42 }
43
44 CCECAdapterMessage::CCECAdapterMessage(const cec_command &command, uint8_t iLineTimeout /* = 3 */)
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
91 lineTimeout = iLineTimeout;
92 }
93
94 CStdString 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:
126 for (uint8_t iPtr = 2; iPtr < Size(); iPtr++)
127 if (At(iPtr) != MSGEND)
128 strMsg.AppendFormat(" %02x", At(iPtr));
129 break;
130 }
131 }
132
133 return strMsg;
134 }
135
136 const 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";
190 case MSGCODE_SET_POWERSTATE:
191 return "SET_POWERSTATE";
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";
224 }
225
226 return "unknown";
227 }
228
229 uint8_t CCECAdapterMessage::operator[](uint8_t pos) const
230 {
231 return pos < packet.size ? packet[pos] : 0;
232 }
233
234 uint8_t CCECAdapterMessage::At(uint8_t pos) const
235 {
236 return pos < packet.size ? packet[pos] : 0;
237 }
238
239 uint8_t CCECAdapterMessage::Size(void) const
240 {
241 return packet.size;
242 }
243
244 bool CCECAdapterMessage::IsEmpty(void) const
245 {
246 return packet.IsEmpty();
247 }
248
249 void CCECAdapterMessage::Clear(void)
250 {
251 state = ADAPTER_MESSAGE_STATE_UNKNOWN;
252 transmit_timeout = CEC_DEFAULT_TRANSMIT_TIMEOUT;
253 response.Clear();
254 packet.Clear();
255 lineTimeout = 3;
256 bNextByteIsEscaped = false;
257 }
258
259 void CCECAdapterMessage::Shift(uint8_t iShiftBy)
260 {
261 packet.Shift(iShiftBy);
262 }
263
264 void CCECAdapterMessage::Append(CCECAdapterMessage &data)
265 {
266 Append(data.packet);
267 }
268
269 void CCECAdapterMessage::Append(cec_datapacket &data)
270 {
271 for (uint8_t iPtr = 0; iPtr < data.size; iPtr++)
272 PushBack(data[iPtr]);
273 }
274
275 void CCECAdapterMessage::PushBack(uint8_t byte)
276 {
277 packet.PushBack(byte);
278 }
279
280 void CCECAdapterMessage::PushEscaped(uint8_t byte)
281 {
282 if (byte >= MSGESC)
283 {
284 PushBack(MSGESC);
285 PushBack(byte - ESCOFFSET);
286 }
287 else
288 {
289 PushBack(byte);
290 }
291 }
292
293 bool CCECAdapterMessage::PushReceivedByte(uint8_t byte)
294 {
295 if (byte == MSGSTART)
296 {
297 if (HasStartMessage())
298 {
299 CLibCEC::AddLog(CEC_LOG_WARNING, "received MSGSTART before MSGEND, removing previous buffer contents");
300 Clear();
301 }
302 PushBack(byte);
303 }
304 else
305 {
306 if (bNextByteIsEscaped)
307 {
308 PushBack(byte + (uint8_t)ESCOFFSET);
309 bNextByteIsEscaped = false;
310 }
311 else if (byte == MSGESC)
312 bNextByteIsEscaped = true;
313 else
314 PushBack(byte);
315 }
316
317 return byte == MSGEND;
318 }
319
320 cec_adapter_messagecode CCECAdapterMessage::Message(void) const
321 {
322 return packet.size >= 2 ?
323 (cec_adapter_messagecode) (packet.At(1) & ~(MSGCODE_FRAME_EOM | MSGCODE_FRAME_ACK)) :
324 MSGCODE_NOTHING;
325 }
326
327 bool CCECAdapterMessage::IsTranmission(void) const
328 {
329 cec_adapter_messagecode msgCode = Message();
330 return msgCode == MSGCODE_FRAME_ACK ||
331 msgCode == MSGCODE_FRAME_DATA ||
332 msgCode == MSGCODE_FRAME_EOM ||
333 msgCode == MSGCODE_FRAME_START ||
334 msgCode == MSGCODE_HIGH_ERROR ||
335 msgCode == MSGCODE_LOW_ERROR ||
336 msgCode == MSGCODE_RECEIVE_FAILED ||
337 msgCode == MSGCODE_TRANSMIT_ACK_POLARITY ||
338 msgCode == MSGCODE_TRANSMIT_EOM ||
339 msgCode == MSGCODE_TRANSMIT_FAILED_ACK ||
340 msgCode == MSGCODE_TRANSMIT_FAILED_LINE ||
341 msgCode == MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA ||
342 msgCode == MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE ||
343 msgCode == MSGCODE_TRANSMIT_LINE_TIMEOUT ||
344 msgCode == MSGCODE_TRANSMIT_SUCCEEDED;
345 }
346
347 bool CCECAdapterMessage::IsEOM(void) const
348 {
349 return packet.size >= 2 ?
350 (packet.At(1) & MSGCODE_FRAME_EOM) != 0 :
351 false;
352 }
353
354 bool CCECAdapterMessage::IsACK(void) const
355 {
356 return packet.size >= 2 ?
357 (packet.At(1) & MSGCODE_FRAME_ACK) != 0 :
358 false;
359 }
360
361 bool CCECAdapterMessage::IsError(void) const
362 {
363 cec_adapter_messagecode code = Message();
364 return (code == MSGCODE_HIGH_ERROR ||
365 code == MSGCODE_LOW_ERROR ||
366 code == MSGCODE_RECEIVE_FAILED ||
367 code == MSGCODE_COMMAND_REJECTED ||
368 code == MSGCODE_TRANSMIT_LINE_TIMEOUT ||
369 code == MSGCODE_TRANSMIT_FAILED_LINE ||
370 code == MSGCODE_TRANSMIT_FAILED_ACK ||
371 code == MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA ||
372 code == MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE);
373 }
374
375 bool CCECAdapterMessage::NeedsRetry(void) const
376 {
377 return Reply() == MSGCODE_NOTHING ||
378 Reply() == MSGCODE_RECEIVE_FAILED ||
379 Reply() == MSGCODE_TIMEOUT_ERROR ||
380 Reply() == MSGCODE_TRANSMIT_FAILED_LINE ||
381 Reply() == MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA ||
382 Reply() == MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE ||
383 Reply() == MSGCODE_TRANSMIT_LINE_TIMEOUT;
384 }
385
386 cec_logical_address CCECAdapterMessage::Initiator(void) const
387 {
388 return packet.size >= 3 ?
389 (cec_logical_address) (packet.At(2) >> 4) :
390 CECDEVICE_UNKNOWN;
391 }
392
393 cec_logical_address CCECAdapterMessage::Destination(void) const
394 {
395 return packet.size >= 3 ?
396 (cec_logical_address) (packet.At(2) & 0xF) :
397 CECDEVICE_UNKNOWN;
398 }
399
400 bool CCECAdapterMessage::HasStartMessage(void) const
401 {
402 return packet.size >= 1 && packet.At(0) == MSGSTART;
403 }
404
405 bool CCECAdapterMessage::PushToCecCommand(cec_command &command) const
406 {
407 // empty message
408 if (IsEmpty())
409 return false;
410
411 cec_adapter_messagecode msgCode = Message();
412 if (msgCode == MSGCODE_FRAME_START)
413 {
414 command.Clear();
415 if (Size() >= 3)
416 {
417 command.initiator = Initiator();
418 command.destination = Destination();
419 command.ack = IsACK();
420 command.eom = IsEOM();
421 }
422 return IsEOM() && !IsError();
423 }
424 else if (msgCode == MSGCODE_FRAME_DATA)
425 {
426 if (Size() >= 3)
427 {
428 command.PushBack(At(2));
429 command.eom = IsEOM();
430 }
431 return IsEOM() && !IsError();
432 }
433
434 return false;
435 }
436
437 cec_adapter_messagecode CCECAdapterMessage::Reply(void) const
438 {
439 return response.size >= 2 ?
440 (cec_adapter_messagecode) (response.At(1) & ~(MSGCODE_FRAME_EOM | MSGCODE_FRAME_ACK)) :
441 MSGCODE_NOTHING;
442 }