Commit | Line | Data |
---|---|---|
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 | ||
2b44051c | 33 | #include "env.h" |
a75e3a5a | 34 | #include "USBCECAdapterMessage.h" |
2b44051c LOK |
35 | |
36 | #include "lib/LibCEC.h" | |
37 | #include "lib/platform/util/StdString.h" | |
a75e3a5a LOK |
38 | |
39 | using namespace CEC; | |
40 | using namespace PLATFORM; | |
41 | ||
42 | CCECAdapterMessage::CCECAdapterMessage(void) | |
43 | { | |
44 | Clear(); | |
45 | } | |
46 | ||
33dd87a9 | 47 | CCECAdapterMessage::CCECAdapterMessage(const cec_command &command, uint8_t iLineTimeout /* = 3 */) |
a75e3a5a LOK |
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 | ||
a75e3a5a | 94 | lineTimeout = iLineTimeout; |
a75e3a5a LOK |
95 | } |
96 | ||
2b44051c | 97 | std::string CCECAdapterMessage::ToString(void) const |
a75e3a5a LOK |
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: | |
3ead056c LOK |
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 | } | |
a75e3a5a LOK |
137 | break; |
138 | } | |
139 | } | |
140 | ||
2b44051c | 141 | return std::string(strMsg.c_str()); |
a75e3a5a LOK |
142 | } |
143 | ||
144 | const 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 "TRANSMIT_ACK_POLARITY"; | |
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"; | |
b2f56d35 LOK |
198 | case MSGCODE_GET_BUILDDATE: |
199 | return "GET_BUILDDATE"; | |
a75e3a5a LOK |
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"; | |
b2f56d35 LOK |
232 | default: |
233 | break; | |
a75e3a5a LOK |
234 | } |
235 | ||
236 | return "unknown"; | |
237 | } | |
238 | ||
239 | uint8_t CCECAdapterMessage::operator[](uint8_t pos) const | |
240 | { | |
241 | return pos < packet.size ? packet[pos] : 0; | |
242 | } | |
243 | ||
244 | uint8_t CCECAdapterMessage::At(uint8_t pos) const | |
245 | { | |
246 | return pos < packet.size ? packet[pos] : 0; | |
247 | } | |
248 | ||
249 | uint8_t CCECAdapterMessage::Size(void) const | |
250 | { | |
251 | return packet.size; | |
252 | } | |
253 | ||
254 | bool CCECAdapterMessage::IsEmpty(void) const | |
255 | { | |
256 | return packet.IsEmpty(); | |
257 | } | |
258 | ||
259 | void CCECAdapterMessage::Clear(void) | |
260 | { | |
261 | state = ADAPTER_MESSAGE_STATE_UNKNOWN; | |
262 | transmit_timeout = CEC_DEFAULT_TRANSMIT_TIMEOUT; | |
263 | response.Clear(); | |
264 | packet.Clear(); | |
a75e3a5a | 265 | lineTimeout = 3; |
a75e3a5a LOK |
266 | bNextByteIsEscaped = false; |
267 | } | |
268 | ||
269 | void CCECAdapterMessage::Shift(uint8_t iShiftBy) | |
270 | { | |
271 | packet.Shift(iShiftBy); | |
272 | } | |
273 | ||
274 | void CCECAdapterMessage::Append(CCECAdapterMessage &data) | |
275 | { | |
276 | Append(data.packet); | |
277 | } | |
278 | ||
279 | void CCECAdapterMessage::Append(cec_datapacket &data) | |
280 | { | |
281 | for (uint8_t iPtr = 0; iPtr < data.size; iPtr++) | |
282 | PushBack(data[iPtr]); | |
283 | } | |
284 | ||
285 | void CCECAdapterMessage::PushBack(uint8_t byte) | |
286 | { | |
287 | packet.PushBack(byte); | |
288 | } | |
289 | ||
290 | void CCECAdapterMessage::PushEscaped(uint8_t byte) | |
291 | { | |
292 | if (byte >= MSGESC) | |
293 | { | |
294 | PushBack(MSGESC); | |
295 | PushBack(byte - ESCOFFSET); | |
296 | } | |
297 | else | |
298 | { | |
299 | PushBack(byte); | |
300 | } | |
301 | } | |
302 | ||
303 | bool CCECAdapterMessage::PushReceivedByte(uint8_t byte) | |
304 | { | |
305 | if (byte == MSGSTART) | |
306 | { | |
307 | if (HasStartMessage()) | |
308 | { | |
004b8382 | 309 | //TODO CLibCEC::AddLog(CEC_LOG_WARNING, "received MSGSTART before MSGEND, removing previous buffer contents"); |
a75e3a5a LOK |
310 | Clear(); |
311 | } | |
312 | PushBack(byte); | |
313 | } | |
314 | else | |
315 | { | |
316 | if (bNextByteIsEscaped) | |
317 | { | |
318 | PushBack(byte + (uint8_t)ESCOFFSET); | |
319 | bNextByteIsEscaped = false; | |
320 | } | |
321 | else if (byte == MSGESC) | |
322 | bNextByteIsEscaped = true; | |
323 | else | |
324 | PushBack(byte); | |
325 | } | |
326 | ||
327 | return byte == MSGEND; | |
328 | } | |
329 | ||
330 | cec_adapter_messagecode CCECAdapterMessage::Message(void) const | |
331 | { | |
332 | return packet.size >= 2 ? | |
333 | (cec_adapter_messagecode) (packet.At(1) & ~(MSGCODE_FRAME_EOM | MSGCODE_FRAME_ACK)) : | |
334 | MSGCODE_NOTHING; | |
335 | } | |
336 | ||
3ead056c LOK |
337 | cec_adapter_messagecode CCECAdapterMessage::ResponseTo(void) const |
338 | { | |
339 | return packet.size >= 3 ? | |
340 | (cec_adapter_messagecode) (packet.At(2) & ~(MSGCODE_FRAME_EOM | MSGCODE_FRAME_ACK)) : | |
341 | MSGCODE_NOTHING; | |
342 | } | |
343 | ||
a75e3a5a LOK |
344 | bool CCECAdapterMessage::IsTranmission(void) const |
345 | { | |
346 | cec_adapter_messagecode msgCode = Message(); | |
347 | return msgCode == MSGCODE_FRAME_ACK || | |
348 | msgCode == MSGCODE_FRAME_DATA || | |
349 | msgCode == MSGCODE_FRAME_EOM || | |
350 | msgCode == MSGCODE_FRAME_START || | |
351 | msgCode == MSGCODE_HIGH_ERROR || | |
352 | msgCode == MSGCODE_LOW_ERROR || | |
353 | msgCode == MSGCODE_RECEIVE_FAILED || | |
354 | msgCode == MSGCODE_TRANSMIT_ACK_POLARITY || | |
355 | msgCode == MSGCODE_TRANSMIT_EOM || | |
356 | msgCode == MSGCODE_TRANSMIT_FAILED_ACK || | |
357 | msgCode == MSGCODE_TRANSMIT_FAILED_LINE || | |
358 | msgCode == MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA || | |
359 | msgCode == MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE || | |
360 | msgCode == MSGCODE_TRANSMIT_LINE_TIMEOUT || | |
361 | msgCode == MSGCODE_TRANSMIT_SUCCEEDED; | |
362 | } | |
363 | ||
364 | bool CCECAdapterMessage::IsEOM(void) const | |
365 | { | |
366 | return packet.size >= 2 ? | |
367 | (packet.At(1) & MSGCODE_FRAME_EOM) != 0 : | |
368 | false; | |
369 | } | |
370 | ||
371 | bool CCECAdapterMessage::IsACK(void) const | |
372 | { | |
373 | return packet.size >= 2 ? | |
374 | (packet.At(1) & MSGCODE_FRAME_ACK) != 0 : | |
375 | false; | |
376 | } | |
377 | ||
378 | bool CCECAdapterMessage::IsError(void) const | |
379 | { | |
380 | cec_adapter_messagecode code = Message(); | |
381 | return (code == MSGCODE_HIGH_ERROR || | |
382 | code == MSGCODE_LOW_ERROR || | |
383 | code == MSGCODE_RECEIVE_FAILED || | |
384 | code == MSGCODE_COMMAND_REJECTED || | |
385 | code == MSGCODE_TRANSMIT_LINE_TIMEOUT || | |
386 | code == MSGCODE_TRANSMIT_FAILED_LINE || | |
387 | code == MSGCODE_TRANSMIT_FAILED_ACK || | |
388 | code == MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA || | |
389 | code == MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE); | |
390 | } | |
391 | ||
392 | bool CCECAdapterMessage::NeedsRetry(void) const | |
393 | { | |
394 | return Reply() == MSGCODE_NOTHING || | |
395 | Reply() == MSGCODE_RECEIVE_FAILED || | |
396 | Reply() == MSGCODE_TIMEOUT_ERROR || | |
397 | Reply() == MSGCODE_TRANSMIT_FAILED_LINE || | |
398 | Reply() == MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA || | |
399 | Reply() == MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE || | |
400 | Reply() == MSGCODE_TRANSMIT_LINE_TIMEOUT; | |
401 | } | |
402 | ||
403 | cec_logical_address CCECAdapterMessage::Initiator(void) const | |
404 | { | |
405 | return packet.size >= 3 ? | |
406 | (cec_logical_address) (packet.At(2) >> 4) : | |
407 | CECDEVICE_UNKNOWN; | |
408 | } | |
409 | ||
410 | cec_logical_address CCECAdapterMessage::Destination(void) const | |
411 | { | |
412 | return packet.size >= 3 ? | |
413 | (cec_logical_address) (packet.At(2) & 0xF) : | |
414 | CECDEVICE_UNKNOWN; | |
415 | } | |
416 | ||
417 | bool CCECAdapterMessage::HasStartMessage(void) const | |
418 | { | |
419 | return packet.size >= 1 && packet.At(0) == MSGSTART; | |
420 | } | |
421 | ||
422 | bool CCECAdapterMessage::PushToCecCommand(cec_command &command) const | |
423 | { | |
424 | // empty message | |
425 | if (IsEmpty()) | |
426 | return false; | |
427 | ||
428 | cec_adapter_messagecode msgCode = Message(); | |
429 | if (msgCode == MSGCODE_FRAME_START) | |
430 | { | |
431 | command.Clear(); | |
432 | if (Size() >= 3) | |
433 | { | |
434 | command.initiator = Initiator(); | |
435 | command.destination = Destination(); | |
436 | command.ack = IsACK(); | |
437 | command.eom = IsEOM(); | |
438 | } | |
439 | return IsEOM() && !IsError(); | |
440 | } | |
441 | else if (msgCode == MSGCODE_FRAME_DATA) | |
442 | { | |
443 | if (Size() >= 3) | |
444 | { | |
445 | command.PushBack(At(2)); | |
446 | command.eom = IsEOM(); | |
447 | } | |
448 | return IsEOM() && !IsError(); | |
449 | } | |
450 | ||
451 | return false; | |
452 | } | |
453 | ||
454 | cec_adapter_messagecode CCECAdapterMessage::Reply(void) const | |
455 | { | |
456 | return response.size >= 2 ? | |
457 | (cec_adapter_messagecode) (response.At(1) & ~(MSGCODE_FRAME_EOM | MSGCODE_FRAME_ACK)) : | |
458 | MSGCODE_NOTHING; | |
459 | } |