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