f213c1dda81238bac53ccac8a69aa051a70deee3
[deb_libcec.git] / src / lib / adapter / Pulse-Eight / 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 "env.h"
34 #include "USBCECAdapterMessage.h"
35
36 #include "lib/LibCEC.h"
37 #include "lib/platform/util/StdString.h"
38
39 using namespace CEC;
40 using namespace PLATFORM;
41
42 CCECAdapterMessage::CCECAdapterMessage(void)
43 {
44 Clear();
45 }
46
47 CCECAdapterMessage::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
97 std::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 for (uint8_t iPtr = 2; iPtr < Size(); iPtr++)
130 if (At(iPtr) != MSGEND)
131 strMsg.AppendFormat(" %02x", At(iPtr));
132 break;
133 }
134 }
135
136 return std::string(strMsg.c_str());
137 }
138
139 const char *CCECAdapterMessage::ToString(cec_adapter_messagecode msgCode)
140 {
141 switch (msgCode)
142 {
143 case MSGCODE_NOTHING:
144 return "NOTHING";
145 case MSGCODE_PING:
146 return "PING";
147 case MSGCODE_TIMEOUT_ERROR:
148 return "TIMEOUT";
149 case MSGCODE_HIGH_ERROR:
150 return "HIGH_ERROR";
151 case MSGCODE_LOW_ERROR:
152 return "LOW_ERROR";
153 case MSGCODE_FRAME_START:
154 return "FRAME_START";
155 case MSGCODE_FRAME_DATA:
156 return "FRAME_DATA";
157 case MSGCODE_RECEIVE_FAILED:
158 return "RECEIVE_FAILED";
159 case MSGCODE_COMMAND_ACCEPTED:
160 return "COMMAND_ACCEPTED";
161 case MSGCODE_COMMAND_REJECTED:
162 return "COMMAND_REJECTED";
163 case MSGCODE_SET_ACK_MASK:
164 return "SET_ACK_MASK";
165 case MSGCODE_TRANSMIT:
166 return "TRANSMIT";
167 case MSGCODE_TRANSMIT_EOM:
168 return "TRANSMIT_EOM";
169 case MSGCODE_TRANSMIT_IDLETIME:
170 return "TRANSMIT_IDLETIME";
171 case MSGCODE_TRANSMIT_ACK_POLARITY:
172 return "TRANSMIT_ACK_POLARITY";
173 case MSGCODE_TRANSMIT_LINE_TIMEOUT:
174 return "TRANSMIT_LINE_TIMEOUT";
175 case MSGCODE_TRANSMIT_SUCCEEDED:
176 return "TRANSMIT_SUCCEEDED";
177 case MSGCODE_TRANSMIT_FAILED_LINE:
178 return "TRANSMIT_FAILED_LINE";
179 case MSGCODE_TRANSMIT_FAILED_ACK:
180 return "TRANSMIT_FAILED_ACK";
181 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA:
182 return "TRANSMIT_FAILED_TIMEOUT_DATA";
183 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE:
184 return "TRANSMIT_FAILED_TIMEOUT_LINE";
185 case MSGCODE_FIRMWARE_VERSION:
186 return "FIRMWARE_VERSION";
187 case MSGCODE_START_BOOTLOADER:
188 return "START_BOOTLOADER";
189 case MSGCODE_FRAME_EOM:
190 return "FRAME_EOM";
191 case MSGCODE_FRAME_ACK:
192 return "FRAME_ACK";
193 case MSGCODE_GET_BUILDDATE:
194 return "GET_BUILDDATE";
195 case MSGCODE_SET_CONTROLLED:
196 return "SET_CONTROLLED";
197 case MSGCODE_GET_AUTO_ENABLED:
198 return "GET_AUTO_ENABLED";
199 case MSGCODE_SET_AUTO_ENABLED:
200 return "SET_AUTO_ENABLED";
201 case MSGCODE_GET_DEFAULT_LOGICAL_ADDRESS:
202 return "GET_DEFAULT_LOGICAL_ADDRESS";
203 case MSGCODE_SET_DEFAULT_LOGICAL_ADDRESS:
204 return "SET_DEFAULT_LOGICAL_ADDRESS";
205 case MSGCODE_GET_LOGICAL_ADDRESS_MASK:
206 return "GET_LOGICAL_ADDRESS_MASK";
207 case MSGCODE_SET_LOGICAL_ADDRESS_MASK:
208 return "SET_LOGICAL_ADDRESS_MASK";
209 case MSGCODE_GET_PHYSICAL_ADDRESS:
210 return "GET_PHYSICAL_ADDRESS";
211 case MSGCODE_SET_PHYSICAL_ADDRESS:
212 return "SET_PHYSICAL_ADDRESS";
213 case MSGCODE_GET_DEVICE_TYPE:
214 return "GET_DEVICE_TYPE";
215 case MSGCODE_SET_DEVICE_TYPE:
216 return "SET_DEVICE_TYPE";
217 case MSGCODE_GET_HDMI_VERSION:
218 return "GET_HDMI_VERSION";
219 case MSGCODE_SET_HDMI_VERSION:
220 return "SET_HDMI_VERSION";
221 case MSGCODE_GET_OSD_NAME:
222 return "GET_OSD_NAME";
223 case MSGCODE_SET_OSD_NAME:
224 return "SET_OSD_NAME";
225 case MSGCODE_WRITE_EEPROM:
226 return "WRITE_EEPROM";
227 default:
228 break;
229 }
230
231 return "unknown";
232 }
233
234 uint8_t CCECAdapterMessage::operator[](uint8_t pos) const
235 {
236 return pos < packet.size ? packet[pos] : 0;
237 }
238
239 uint8_t CCECAdapterMessage::At(uint8_t pos) const
240 {
241 return pos < packet.size ? packet[pos] : 0;
242 }
243
244 uint8_t CCECAdapterMessage::Size(void) const
245 {
246 return packet.size;
247 }
248
249 bool CCECAdapterMessage::IsEmpty(void) const
250 {
251 return packet.IsEmpty();
252 }
253
254 void CCECAdapterMessage::Clear(void)
255 {
256 state = ADAPTER_MESSAGE_STATE_UNKNOWN;
257 transmit_timeout = CEC_DEFAULT_TRANSMIT_TIMEOUT;
258 response.Clear();
259 packet.Clear();
260 lineTimeout = 3;
261 bNextByteIsEscaped = false;
262 }
263
264 void CCECAdapterMessage::Shift(uint8_t iShiftBy)
265 {
266 packet.Shift(iShiftBy);
267 }
268
269 void CCECAdapterMessage::Append(CCECAdapterMessage &data)
270 {
271 Append(data.packet);
272 }
273
274 void CCECAdapterMessage::Append(cec_datapacket &data)
275 {
276 for (uint8_t iPtr = 0; iPtr < data.size; iPtr++)
277 PushBack(data[iPtr]);
278 }
279
280 void CCECAdapterMessage::PushBack(uint8_t byte)
281 {
282 packet.PushBack(byte);
283 }
284
285 void CCECAdapterMessage::PushEscaped(uint8_t byte)
286 {
287 if (byte >= MSGESC)
288 {
289 PushBack(MSGESC);
290 PushBack(byte - ESCOFFSET);
291 }
292 else
293 {
294 PushBack(byte);
295 }
296 }
297
298 bool CCECAdapterMessage::PushReceivedByte(uint8_t byte)
299 {
300 if (byte == MSGSTART)
301 {
302 if (HasStartMessage())
303 {
304 //TODO CLibCEC::AddLog(CEC_LOG_WARNING, "received MSGSTART before MSGEND, removing previous buffer contents");
305 Clear();
306 }
307 PushBack(byte);
308 }
309 else
310 {
311 if (bNextByteIsEscaped)
312 {
313 PushBack(byte + (uint8_t)ESCOFFSET);
314 bNextByteIsEscaped = false;
315 }
316 else if (byte == MSGESC)
317 bNextByteIsEscaped = true;
318 else
319 PushBack(byte);
320 }
321
322 return byte == MSGEND;
323 }
324
325 cec_adapter_messagecode CCECAdapterMessage::Message(void) const
326 {
327 return packet.size >= 2 ?
328 (cec_adapter_messagecode) (packet.At(1) & ~(MSGCODE_FRAME_EOM | MSGCODE_FRAME_ACK)) :
329 MSGCODE_NOTHING;
330 }
331
332 bool CCECAdapterMessage::IsTranmission(void) const
333 {
334 cec_adapter_messagecode msgCode = Message();
335 return msgCode == MSGCODE_FRAME_ACK ||
336 msgCode == MSGCODE_FRAME_DATA ||
337 msgCode == MSGCODE_FRAME_EOM ||
338 msgCode == MSGCODE_FRAME_START ||
339 msgCode == MSGCODE_HIGH_ERROR ||
340 msgCode == MSGCODE_LOW_ERROR ||
341 msgCode == MSGCODE_RECEIVE_FAILED ||
342 msgCode == MSGCODE_TRANSMIT_ACK_POLARITY ||
343 msgCode == MSGCODE_TRANSMIT_EOM ||
344 msgCode == MSGCODE_TRANSMIT_FAILED_ACK ||
345 msgCode == MSGCODE_TRANSMIT_FAILED_LINE ||
346 msgCode == MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA ||
347 msgCode == MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE ||
348 msgCode == MSGCODE_TRANSMIT_LINE_TIMEOUT ||
349 msgCode == MSGCODE_TRANSMIT_SUCCEEDED;
350 }
351
352 bool CCECAdapterMessage::IsEOM(void) const
353 {
354 return packet.size >= 2 ?
355 (packet.At(1) & MSGCODE_FRAME_EOM) != 0 :
356 false;
357 }
358
359 bool CCECAdapterMessage::IsACK(void) const
360 {
361 return packet.size >= 2 ?
362 (packet.At(1) & MSGCODE_FRAME_ACK) != 0 :
363 false;
364 }
365
366 bool CCECAdapterMessage::IsError(void) const
367 {
368 cec_adapter_messagecode code = Message();
369 return (code == MSGCODE_HIGH_ERROR ||
370 code == MSGCODE_LOW_ERROR ||
371 code == MSGCODE_RECEIVE_FAILED ||
372 code == MSGCODE_COMMAND_REJECTED ||
373 code == MSGCODE_TRANSMIT_LINE_TIMEOUT ||
374 code == MSGCODE_TRANSMIT_FAILED_LINE ||
375 code == MSGCODE_TRANSMIT_FAILED_ACK ||
376 code == MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA ||
377 code == MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE);
378 }
379
380 bool CCECAdapterMessage::NeedsRetry(void) const
381 {
382 return Reply() == MSGCODE_NOTHING ||
383 Reply() == MSGCODE_RECEIVE_FAILED ||
384 Reply() == MSGCODE_TIMEOUT_ERROR ||
385 Reply() == MSGCODE_TRANSMIT_FAILED_LINE ||
386 Reply() == MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA ||
387 Reply() == MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE ||
388 Reply() == MSGCODE_TRANSMIT_LINE_TIMEOUT;
389 }
390
391 cec_logical_address CCECAdapterMessage::Initiator(void) const
392 {
393 return packet.size >= 3 ?
394 (cec_logical_address) (packet.At(2) >> 4) :
395 CECDEVICE_UNKNOWN;
396 }
397
398 cec_logical_address CCECAdapterMessage::Destination(void) const
399 {
400 return packet.size >= 3 ?
401 (cec_logical_address) (packet.At(2) & 0xF) :
402 CECDEVICE_UNKNOWN;
403 }
404
405 bool CCECAdapterMessage::HasStartMessage(void) const
406 {
407 return packet.size >= 1 && packet.At(0) == MSGSTART;
408 }
409
410 bool CCECAdapterMessage::PushToCecCommand(cec_command &command) const
411 {
412 // empty message
413 if (IsEmpty())
414 return false;
415
416 cec_adapter_messagecode msgCode = Message();
417 if (msgCode == MSGCODE_FRAME_START)
418 {
419 command.Clear();
420 if (Size() >= 3)
421 {
422 command.initiator = Initiator();
423 command.destination = Destination();
424 command.ack = IsACK();
425 command.eom = IsEOM();
426 }
427 return IsEOM() && !IsError();
428 }
429 else if (msgCode == MSGCODE_FRAME_DATA)
430 {
431 if (Size() >= 3)
432 {
433 command.PushBack(At(2));
434 command.eom = IsEOM();
435 }
436 return IsEOM() && !IsError();
437 }
438
439 return false;
440 }
441
442 cec_adapter_messagecode CCECAdapterMessage::Reply(void) const
443 {
444 return response.size >= 2 ?
445 (cec_adapter_messagecode) (response.At(1) & ~(MSGCODE_FRAME_EOM | MSGCODE_FRAME_ACK)) :
446 MSGCODE_NOTHING;
447 }