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 | ||
44 | CCECAdapterMessage::CCECAdapterMessage(const cec_command &command, uint8_t iMaxTries /* = 1 */, uint8_t iLineTimeout /* = 3 */, uint8_t iRetryLineTimeout /* = 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 | /* set the number of retries */ | |
92 | if (command.opcode == CEC_OPCODE_NONE) //TODO | |
93 | maxTries = 1; | |
94 | else if (command.initiator != CECDEVICE_BROADCAST) | |
95 | maxTries = iMaxTries; | |
96 | ||
97 | lineTimeout = iLineTimeout; | |
98 | retryTimeout = iRetryLineTimeout; | |
99 | tries = 0; | |
100 | } | |
101 | ||
102 | CStdString CCECAdapterMessage::ToString(void) const | |
103 | { | |
104 | CStdString strMsg; | |
105 | if (Size() == 0) | |
106 | { | |
107 | strMsg = "empty message"; | |
108 | } | |
109 | else | |
110 | { | |
111 | strMsg = ToString(Message()); | |
112 | ||
113 | switch (Message()) | |
114 | { | |
115 | case MSGCODE_TIMEOUT_ERROR: | |
116 | case MSGCODE_HIGH_ERROR: | |
117 | case MSGCODE_LOW_ERROR: | |
118 | { | |
119 | uint32_t iLine = (Size() >= 4) ? (At(2) << 8) | At(3) : 0; | |
120 | uint32_t iTime = (Size() >= 8) ? (At(4) << 24) | (At(5) << 16) | (At(6) << 8) | At(7) : 0; | |
121 | strMsg.AppendFormat(" line:%u", iLine); | |
122 | strMsg.AppendFormat(" time:%u", iTime); | |
123 | } | |
124 | break; | |
125 | case MSGCODE_FRAME_START: | |
126 | if (Size() >= 3) | |
127 | strMsg.AppendFormat(" initiator:%1x destination:%1x ack:%s %s", Initiator(), Destination(), IsACK() ? "high" : "low", IsEOM() ? "eom" : ""); | |
128 | break; | |
129 | case MSGCODE_FRAME_DATA: | |
130 | if (Size() >= 3) | |
131 | strMsg.AppendFormat(" %02x %s", At(2), IsEOM() ? "eom" : ""); | |
132 | break; | |
133 | default: | |
134 | break; | |
135 | } | |
136 | } | |
137 | ||
138 | return strMsg; | |
139 | } | |
140 | ||
141 | const char *CCECAdapterMessage::ToString(cec_adapter_messagecode msgCode) | |
142 | { | |
143 | switch (msgCode) | |
144 | { | |
145 | case MSGCODE_NOTHING: | |
146 | return "NOTHING"; | |
147 | case MSGCODE_PING: | |
148 | return "PING"; | |
149 | case MSGCODE_TIMEOUT_ERROR: | |
150 | return "TIMEOUT"; | |
151 | case MSGCODE_HIGH_ERROR: | |
152 | return "HIGH_ERROR"; | |
153 | case MSGCODE_LOW_ERROR: | |
154 | return "LOW_ERROR"; | |
155 | case MSGCODE_FRAME_START: | |
156 | return "FRAME_START"; | |
157 | case MSGCODE_FRAME_DATA: | |
158 | return "FRAME_DATA"; | |
159 | case MSGCODE_RECEIVE_FAILED: | |
160 | return "RECEIVE_FAILED"; | |
161 | case MSGCODE_COMMAND_ACCEPTED: | |
162 | return "COMMAND_ACCEPTED"; | |
163 | case MSGCODE_COMMAND_REJECTED: | |
164 | return "COMMAND_REJECTED"; | |
165 | case MSGCODE_SET_ACK_MASK: | |
166 | return "SET_ACK_MASK"; | |
167 | case MSGCODE_TRANSMIT: | |
168 | return "TRANSMIT"; | |
169 | case MSGCODE_TRANSMIT_EOM: | |
170 | return "TRANSMIT_EOM"; | |
171 | case MSGCODE_TRANSMIT_IDLETIME: | |
172 | return "TRANSMIT_IDLETIME"; | |
173 | case MSGCODE_TRANSMIT_ACK_POLARITY: | |
174 | return "TRANSMIT_ACK_POLARITY"; | |
175 | case MSGCODE_TRANSMIT_LINE_TIMEOUT: | |
176 | return "TRANSMIT_LINE_TIMEOUT"; | |
177 | case MSGCODE_TRANSMIT_SUCCEEDED: | |
178 | return "TRANSMIT_SUCCEEDED"; | |
179 | case MSGCODE_TRANSMIT_FAILED_LINE: | |
180 | return "TRANSMIT_FAILED_LINE"; | |
181 | case MSGCODE_TRANSMIT_FAILED_ACK: | |
182 | return "TRANSMIT_FAILED_ACK"; | |
183 | case MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA: | |
184 | return "TRANSMIT_FAILED_TIMEOUT_DATA"; | |
185 | case MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE: | |
186 | return "TRANSMIT_FAILED_TIMEOUT_LINE"; | |
187 | case MSGCODE_FIRMWARE_VERSION: | |
188 | return "FIRMWARE_VERSION"; | |
189 | case MSGCODE_START_BOOTLOADER: | |
190 | return "START_BOOTLOADER"; | |
191 | case MSGCODE_FRAME_EOM: | |
192 | return "FRAME_EOM"; | |
193 | case MSGCODE_FRAME_ACK: | |
194 | return "FRAME_ACK"; | |
195 | case MSGCODE_SET_POWERSTATE: | |
196 | return "SET_POWERSTATE"; | |
197 | case MSGCODE_SET_CONTROLLED: | |
198 | return "SET_CONTROLLED"; | |
199 | case MSGCODE_GET_AUTO_ENABLED: | |
200 | return "GET_AUTO_ENABLED"; | |
201 | case MSGCODE_SET_AUTO_ENABLED: | |
202 | return "SET_AUTO_ENABLED"; | |
203 | case MSGCODE_GET_DEFAULT_LOGICAL_ADDRESS: | |
204 | return "GET_DEFAULT_LOGICAL_ADDRESS"; | |
205 | case MSGCODE_SET_DEFAULT_LOGICAL_ADDRESS: | |
206 | return "SET_DEFAULT_LOGICAL_ADDRESS"; | |
207 | case MSGCODE_GET_LOGICAL_ADDRESS_MASK: | |
208 | return "GET_LOGICAL_ADDRESS_MASK"; | |
209 | case MSGCODE_SET_LOGICAL_ADDRESS_MASK: | |
210 | return "SET_LOGICAL_ADDRESS_MASK"; | |
211 | case MSGCODE_GET_PHYSICAL_ADDRESS: | |
212 | return "GET_PHYSICAL_ADDRESS"; | |
213 | case MSGCODE_SET_PHYSICAL_ADDRESS: | |
214 | return "SET_PHYSICAL_ADDRESS"; | |
215 | case MSGCODE_GET_DEVICE_TYPE: | |
216 | return "GET_DEVICE_TYPE"; | |
217 | case MSGCODE_SET_DEVICE_TYPE: | |
218 | return "SET_DEVICE_TYPE"; | |
219 | case MSGCODE_GET_HDMI_VERSION: | |
220 | return "GET_HDMI_VERSION"; | |
221 | case MSGCODE_SET_HDMI_VERSION: | |
222 | return "SET_HDMI_VERSION"; | |
223 | case MSGCODE_GET_OSD_NAME: | |
224 | return "GET_OSD_NAME"; | |
225 | case MSGCODE_SET_OSD_NAME: | |
226 | return "SET_OSD_NAME"; | |
227 | case MSGCODE_WRITE_EEPROM: | |
228 | return "WRITE_EEPROM"; | |
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 | maxTries = CEC_DEFAULT_TRANSMIT_RETRIES + 1; | |
261 | tries = 0; | |
262 | lineTimeout = 3; | |
263 | retryTimeout = 3; | |
264 | bNextByteIsEscaped = false; | |
265 | } | |
266 | ||
267 | void CCECAdapterMessage::Shift(uint8_t iShiftBy) | |
268 | { | |
269 | packet.Shift(iShiftBy); | |
270 | } | |
271 | ||
272 | void CCECAdapterMessage::Append(CCECAdapterMessage &data) | |
273 | { | |
274 | Append(data.packet); | |
275 | } | |
276 | ||
277 | void CCECAdapterMessage::Append(cec_datapacket &data) | |
278 | { | |
279 | for (uint8_t iPtr = 0; iPtr < data.size; iPtr++) | |
280 | PushBack(data[iPtr]); | |
281 | } | |
282 | ||
283 | void CCECAdapterMessage::PushBack(uint8_t byte) | |
284 | { | |
285 | packet.PushBack(byte); | |
286 | } | |
287 | ||
288 | void CCECAdapterMessage::PushEscaped(uint8_t byte) | |
289 | { | |
290 | if (byte >= MSGESC) | |
291 | { | |
292 | PushBack(MSGESC); | |
293 | PushBack(byte - ESCOFFSET); | |
294 | } | |
295 | else | |
296 | { | |
297 | PushBack(byte); | |
298 | } | |
299 | } | |
300 | ||
301 | bool CCECAdapterMessage::PushReceivedByte(uint8_t byte) | |
302 | { | |
303 | if (byte == MSGSTART) | |
304 | { | |
305 | if (HasStartMessage()) | |
306 | { | |
307 | CLibCEC::AddLog(CEC_LOG_WARNING, "received MSGSTART before MSGEND, removing previous buffer contents"); | |
308 | Clear(); | |
309 | } | |
310 | PushBack(byte); | |
311 | } | |
312 | else | |
313 | { | |
314 | if (bNextByteIsEscaped) | |
315 | { | |
316 | PushBack(byte + (uint8_t)ESCOFFSET); | |
317 | bNextByteIsEscaped = false; | |
318 | } | |
319 | else if (byte == MSGESC) | |
320 | bNextByteIsEscaped = true; | |
321 | else | |
322 | PushBack(byte); | |
323 | } | |
324 | ||
325 | return byte == MSGEND; | |
326 | } | |
327 | ||
328 | cec_adapter_messagecode CCECAdapterMessage::Message(void) const | |
329 | { | |
330 | return packet.size >= 2 ? | |
331 | (cec_adapter_messagecode) (packet.At(1) & ~(MSGCODE_FRAME_EOM | MSGCODE_FRAME_ACK)) : | |
332 | MSGCODE_NOTHING; | |
333 | } | |
334 | ||
335 | bool CCECAdapterMessage::IsTranmission(void) const | |
336 | { | |
337 | cec_adapter_messagecode msgCode = Message(); | |
338 | return msgCode == MSGCODE_FRAME_ACK || | |
339 | msgCode == MSGCODE_FRAME_DATA || | |
340 | msgCode == MSGCODE_FRAME_EOM || | |
341 | msgCode == MSGCODE_FRAME_START || | |
342 | msgCode == MSGCODE_HIGH_ERROR || | |
343 | msgCode == MSGCODE_LOW_ERROR || | |
344 | msgCode == MSGCODE_RECEIVE_FAILED || | |
345 | msgCode == MSGCODE_TRANSMIT_ACK_POLARITY || | |
346 | msgCode == MSGCODE_TRANSMIT_EOM || | |
347 | msgCode == MSGCODE_TRANSMIT_FAILED_ACK || | |
348 | msgCode == MSGCODE_TRANSMIT_FAILED_LINE || | |
349 | msgCode == MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA || | |
350 | msgCode == MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE || | |
351 | msgCode == MSGCODE_TRANSMIT_LINE_TIMEOUT || | |
352 | msgCode == MSGCODE_TRANSMIT_SUCCEEDED; | |
353 | } | |
354 | ||
355 | bool CCECAdapterMessage::IsEOM(void) const | |
356 | { | |
357 | return packet.size >= 2 ? | |
358 | (packet.At(1) & MSGCODE_FRAME_EOM) != 0 : | |
359 | false; | |
360 | } | |
361 | ||
362 | bool CCECAdapterMessage::IsACK(void) const | |
363 | { | |
364 | return packet.size >= 2 ? | |
365 | (packet.At(1) & MSGCODE_FRAME_ACK) != 0 : | |
366 | false; | |
367 | } | |
368 | ||
369 | bool CCECAdapterMessage::IsError(void) const | |
370 | { | |
371 | cec_adapter_messagecode code = Message(); | |
372 | return (code == MSGCODE_HIGH_ERROR || | |
373 | code == MSGCODE_LOW_ERROR || | |
374 | code == MSGCODE_RECEIVE_FAILED || | |
375 | code == MSGCODE_COMMAND_REJECTED || | |
376 | code == MSGCODE_TRANSMIT_LINE_TIMEOUT || | |
377 | code == MSGCODE_TRANSMIT_FAILED_LINE || | |
378 | code == MSGCODE_TRANSMIT_FAILED_ACK || | |
379 | code == MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA || | |
380 | code == MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE); | |
381 | } | |
382 | ||
383 | bool CCECAdapterMessage::NeedsRetry(void) const | |
384 | { | |
385 | return Reply() == MSGCODE_NOTHING || | |
386 | Reply() == MSGCODE_RECEIVE_FAILED || | |
387 | Reply() == MSGCODE_TIMEOUT_ERROR || | |
388 | Reply() == MSGCODE_TRANSMIT_FAILED_LINE || | |
389 | Reply() == MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA || | |
390 | Reply() == MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE || | |
391 | Reply() == MSGCODE_TRANSMIT_LINE_TIMEOUT; | |
392 | } | |
393 | ||
394 | cec_logical_address CCECAdapterMessage::Initiator(void) const | |
395 | { | |
396 | return packet.size >= 3 ? | |
397 | (cec_logical_address) (packet.At(2) >> 4) : | |
398 | CECDEVICE_UNKNOWN; | |
399 | } | |
400 | ||
401 | cec_logical_address CCECAdapterMessage::Destination(void) const | |
402 | { | |
403 | return packet.size >= 3 ? | |
404 | (cec_logical_address) (packet.At(2) & 0xF) : | |
405 | CECDEVICE_UNKNOWN; | |
406 | } | |
407 | ||
408 | bool CCECAdapterMessage::HasStartMessage(void) const | |
409 | { | |
410 | return packet.size >= 1 && packet.At(0) == MSGSTART; | |
411 | } | |
412 | ||
413 | bool CCECAdapterMessage::PushToCecCommand(cec_command &command) const | |
414 | { | |
415 | // empty message | |
416 | if (IsEmpty()) | |
417 | return false; | |
418 | ||
419 | cec_adapter_messagecode msgCode = Message(); | |
420 | if (msgCode == MSGCODE_FRAME_START) | |
421 | { | |
422 | command.Clear(); | |
423 | if (Size() >= 3) | |
424 | { | |
425 | command.initiator = Initiator(); | |
426 | command.destination = Destination(); | |
427 | command.ack = IsACK(); | |
428 | command.eom = IsEOM(); | |
429 | } | |
430 | return IsEOM() && !IsError(); | |
431 | } | |
432 | else if (msgCode == MSGCODE_FRAME_DATA) | |
433 | { | |
434 | if (Size() >= 3) | |
435 | { | |
436 | command.PushBack(At(2)); | |
437 | command.eom = IsEOM(); | |
438 | } | |
439 | return IsEOM() && !IsError(); | |
440 | } | |
441 | ||
442 | return false; | |
443 | } | |
444 | ||
445 | cec_adapter_messagecode CCECAdapterMessage::Reply(void) const | |
446 | { | |
447 | return response.size >= 2 ? | |
448 | (cec_adapter_messagecode) (response.At(1) & ~(MSGCODE_FRAME_EOM | MSGCODE_FRAME_ACK)) : | |
449 | MSGCODE_NOTHING; | |
450 | } |