Commit | Line | Data |
---|---|---|
ef7696f5 LOK |
1 | #pragma once |
2 | /* | |
3 | * This file is part of the libCEC(R) library. | |
4 | * | |
b492c10e | 5 | * libCEC(R) is Copyright (C) 2011-2012 Pulse-Eight Limited. All rights reserved. |
ef7696f5 LOK |
6 | * libCEC(R) is an original work, containing original code. |
7 | * | |
8 | * libCEC(R) is a trademark of Pulse-Eight Limited. | |
9 | * | |
10 | * This program is dual-licensed; you can redistribute it and/or modify | |
11 | * it under the terms of the GNU General Public License as published by | |
12 | * the Free Software Foundation; either version 2 of the License, or | |
13 | * (at your option) any later version. | |
14 | * | |
15 | * This program is distributed in the hope that it will be useful, | |
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | * GNU General Public License for more details. | |
19 | * | |
20 | * You should have received a copy of the GNU General Public License | |
21 | * along with this program; if not, write to the Free Software | |
22 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
23 | * | |
24 | * | |
25 | * Alternatively, you can license this library under a commercial license, | |
26 | * please contact Pulse-Eight Licensing for more information. | |
27 | * | |
28 | * For more information contact: | |
29 | * Pulse-Eight Licensing <license@pulse-eight.com> | |
30 | * http://www.pulse-eight.com/ | |
31 | * http://www.pulse-eight.net/ | |
32 | */ | |
33 | ||
24048d57 | 34 | #include "../platform/util/StdString.h" |
ba65909d | 35 | |
ef7696f5 LOK |
36 | namespace CEC |
37 | { | |
ef7696f5 LOK |
38 | class CCECAdapterMessage |
39 | { | |
40 | public: | |
de49d80b LOK |
41 | CCECAdapterMessage(void) : |
42 | event(false) | |
ef7696f5 LOK |
43 | { |
44 | Clear(); | |
45 | } | |
46 | ||
47 | CCECAdapterMessage(const cec_command &command) | |
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; | |
5dcf9f25 | 93 | //TODO |
ef7696f5 LOK |
94 | } |
95 | ||
96 | CCECAdapterMessage &operator=(const CCECAdapterMessage &msg) | |
97 | { | |
98 | packet = msg.packet; | |
99 | state = msg.state; | |
100 | return *this; | |
101 | } | |
102 | ||
103 | CStdString ToString(void) const | |
104 | { | |
105 | CStdString strMsg; | |
106 | if (Size() == 0) | |
107 | { | |
108 | strMsg = "empty message"; | |
109 | } | |
110 | else | |
111 | { | |
112 | strMsg = MessageCodeAsString(); | |
113 | ||
114 | switch (Message()) | |
115 | { | |
116 | case MSGCODE_TIMEOUT_ERROR: | |
117 | case MSGCODE_HIGH_ERROR: | |
118 | case MSGCODE_LOW_ERROR: | |
119 | { | |
120 | uint32_t iLine = (Size() >= 3) ? (At(1) << 8) | At(2) : 0; | |
121 | uint32_t iTime = (Size() >= 7) ? (At(3) << 24) | (At(4) << 16) | (At(5) << 8) | At(6) : 0; | |
122 | strMsg.AppendFormat(" line:%u", iLine); | |
123 | strMsg.AppendFormat(" time:%u", iTime); | |
124 | } | |
125 | break; | |
126 | case MSGCODE_FRAME_START: | |
127 | if (Size() >= 2) | |
128 | strMsg.AppendFormat(" initiator:%1x destination:%1x ack:%s %s", Initiator(), Destination(), IsACK() ? "high" : "low", IsEOM() ? "eom" : ""); | |
129 | break; | |
130 | case MSGCODE_FRAME_DATA: | |
131 | if (Size() >= 2) | |
132 | strMsg.AppendFormat(" %02x %s", At(1), IsEOM() ? "eom" : ""); | |
133 | break; | |
134 | default: | |
135 | break; | |
136 | } | |
137 | } | |
138 | ||
139 | return strMsg; | |
140 | } | |
141 | ||
142 | CStdString MessageCodeAsString(void) const | |
143 | { | |
144 | CStdString strMsg; | |
145 | switch (Message()) | |
146 | { | |
147 | case MSGCODE_NOTHING: | |
148 | strMsg = "NOTHING"; | |
149 | break; | |
150 | case MSGCODE_PING: | |
151 | strMsg = "PING"; | |
152 | break; | |
153 | case MSGCODE_TIMEOUT_ERROR: | |
154 | strMsg = "TIMEOUT"; | |
155 | break; | |
156 | case MSGCODE_HIGH_ERROR: | |
157 | strMsg = "HIGH_ERROR"; | |
158 | break; | |
159 | case MSGCODE_LOW_ERROR: | |
160 | strMsg = "LOW_ERROR"; | |
161 | break; | |
162 | case MSGCODE_FRAME_START: | |
163 | strMsg = "FRAME_START"; | |
164 | break; | |
165 | case MSGCODE_FRAME_DATA: | |
166 | strMsg = "FRAME_DATA"; | |
167 | break; | |
168 | case MSGCODE_RECEIVE_FAILED: | |
169 | strMsg = "RECEIVE_FAILED"; | |
170 | break; | |
171 | case MSGCODE_COMMAND_ACCEPTED: | |
172 | strMsg = "COMMAND_ACCEPTED"; | |
173 | break; | |
174 | case MSGCODE_COMMAND_REJECTED: | |
175 | strMsg = "COMMAND_REJECTED"; | |
176 | break; | |
177 | case MSGCODE_SET_ACK_MASK: | |
178 | strMsg = "SET_ACK_MASK"; | |
179 | break; | |
180 | case MSGCODE_TRANSMIT: | |
181 | strMsg = "TRANSMIT"; | |
182 | break; | |
183 | case MSGCODE_TRANSMIT_EOM: | |
184 | strMsg = "TRANSMIT_EOM"; | |
185 | break; | |
186 | case MSGCODE_TRANSMIT_IDLETIME: | |
187 | strMsg = "TRANSMIT_IDLETIME"; | |
188 | break; | |
189 | case MSGCODE_TRANSMIT_ACK_POLARITY: | |
190 | strMsg = "TRANSMIT_ACK_POLARITY"; | |
191 | break; | |
192 | case MSGCODE_TRANSMIT_LINE_TIMEOUT: | |
193 | strMsg = "TRANSMIT_LINE_TIMEOUT"; | |
194 | break; | |
195 | case MSGCODE_TRANSMIT_SUCCEEDED: | |
196 | strMsg = "TRANSMIT_SUCCEEDED"; | |
197 | break; | |
198 | case MSGCODE_TRANSMIT_FAILED_LINE: | |
199 | strMsg = "TRANSMIT_FAILED_LINE"; | |
200 | break; | |
201 | case MSGCODE_TRANSMIT_FAILED_ACK: | |
202 | strMsg = "TRANSMIT_FAILED_ACK"; | |
203 | break; | |
204 | case MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA: | |
205 | strMsg = "TRANSMIT_FAILED_TIMEOUT_DATA"; | |
206 | break; | |
207 | case MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE: | |
208 | strMsg = "TRANSMIT_FAILED_TIMEOUT_LINE"; | |
209 | break; | |
210 | case MSGCODE_FIRMWARE_VERSION: | |
211 | strMsg = "FIRMWARE_VERSION"; | |
212 | break; | |
213 | case MSGCODE_START_BOOTLOADER: | |
214 | strMsg = "START_BOOTLOADER"; | |
215 | break; | |
216 | case MSGCODE_FRAME_EOM: | |
217 | strMsg = "FRAME_EOM"; | |
218 | break; | |
219 | case MSGCODE_FRAME_ACK: | |
220 | strMsg = "FRAME_ACK"; | |
221 | break; | |
dd4efb6d LOK |
222 | case MSGCODE_SET_POWERSTATE: |
223 | strMsg = "SET_POWERSTATE"; | |
224 | break; | |
225 | case MSGCODE_SET_CONTROLLED: | |
226 | strMsg = "SET_CONTROLLED"; | |
227 | break; | |
da59dc5f LOK |
228 | case MSGCODE_GET_AUTO_ENABLED: |
229 | strMsg = "GET_AUTO_ENABLED"; | |
230 | break; | |
231 | case MSGCODE_SET_AUTO_ENABLED: | |
232 | strMsg = "SET_AUTO_ENABLED"; | |
233 | break; | |
234 | case MSGCODE_GET_DEFAULT_LOGICAL_ADDRESS: | |
235 | strMsg = "GET_DEFAULT_LOGICAL_ADDRESS"; | |
236 | break; | |
237 | case MSGCODE_SET_DEFAULT_LOGICAL_ADDRESS: | |
238 | strMsg = "SET_DEFAULT_LOGICAL_ADDRESS"; | |
239 | break; | |
240 | case MSGCODE_GET_LOGICAL_ADDRESS_MASK: | |
241 | strMsg = "GET_LOGICAL_ADDRESS_MASK"; | |
242 | break; | |
243 | case MSGCODE_SET_LOGICAL_ADDRESS_MASK: | |
244 | strMsg = "SET_LOGICAL_ADDRESS_MASK"; | |
245 | break; | |
246 | case MSGCODE_GET_PHYSICAL_ADDRESS: | |
247 | strMsg = "GET_PHYSICAL_ADDRESS"; | |
248 | break; | |
249 | case MSGCODE_SET_PHYSICAL_ADDRESS: | |
250 | strMsg = "SET_PHYSICAL_ADDRESS"; | |
251 | break; | |
252 | case MSGCODE_GET_DEVICE_TYPE: | |
253 | strMsg = "GET_DEVICE_TYPE"; | |
254 | break; | |
255 | case MSGCODE_SET_DEVICE_TYPE: | |
256 | strMsg = "SET_DEVICE_TYPE"; | |
257 | break; | |
258 | case MSGCODE_GET_HDMI_VERSION: | |
259 | strMsg = "GET_HDMI_VERSION"; | |
260 | break; | |
261 | case MSGCODE_SET_HDMI_VERSION: | |
262 | strMsg = "SET_HDMI_VERSION"; | |
263 | break; | |
264 | case MSGCODE_GET_OSD_NAME: | |
265 | strMsg = "GET_OSD_NAME"; | |
266 | break; | |
267 | case MSGCODE_SET_OSD_NAME: | |
268 | strMsg = "SET_OSD_NAME"; | |
269 | break; | |
270 | case MSGCODE_WRITE_EEPROM: | |
271 | strMsg = "WRITE_EEPROM"; | |
272 | break; | |
ef7696f5 LOK |
273 | } |
274 | ||
275 | return strMsg; | |
276 | } | |
277 | ||
278 | uint8_t operator[](uint8_t pos) const | |
279 | { | |
280 | return packet[pos]; | |
281 | } | |
282 | ||
283 | uint8_t At(uint8_t pos) const | |
284 | { | |
285 | return packet[pos]; | |
286 | } | |
287 | ||
288 | uint8_t Size(void) const | |
289 | { | |
290 | return packet.size; | |
291 | } | |
292 | ||
293 | bool IsEmpty(void) const | |
294 | { | |
295 | return packet.IsEmpty(); | |
296 | } | |
297 | ||
298 | void Clear(void) | |
299 | { | |
71c4a2f5 | 300 | state = ADAPTER_MESSAGE_STATE_UNKNOWN; |
c71891a3 | 301 | transmit_timeout = CEC_DEFAULT_TRANSMIT_TIMEOUT; |
ef7696f5 | 302 | packet.Clear(); |
71c4a2f5 LOK |
303 | maxTries = CEC_DEFAULT_TRANSMIT_RETRIES + 1; |
304 | tries = 0; | |
305 | reply = MSGCODE_NOTHING; | |
306 | isTransmission = true; | |
307 | expectControllerAck = true; | |
7bb4ed43 LOK |
308 | lineTimeout = 3; |
309 | retryTimeout = 3; | |
ef7696f5 LOK |
310 | } |
311 | ||
312 | void Shift(uint8_t iShiftBy) | |
313 | { | |
314 | packet.Shift(iShiftBy); | |
315 | } | |
316 | ||
317 | void PushBack(uint8_t add) | |
318 | { | |
319 | packet.PushBack(add); | |
320 | } | |
321 | ||
322 | void PushEscaped(uint8_t byte) | |
323 | { | |
324 | if (byte >= MSGESC) | |
325 | { | |
326 | PushBack(MSGESC); | |
327 | PushBack(byte - ESCOFFSET); | |
328 | } | |
329 | else | |
330 | { | |
331 | PushBack(byte); | |
332 | } | |
333 | } | |
334 | ||
335 | cec_adapter_messagecode Message(void) const | |
336 | { | |
337 | return packet.size >= 1 ? | |
338 | (cec_adapter_messagecode) (packet.At(0) & ~(MSGCODE_FRAME_EOM | MSGCODE_FRAME_ACK)) : | |
339 | MSGCODE_NOTHING; | |
340 | } | |
341 | ||
342 | bool IsEOM(void) const | |
343 | { | |
344 | return packet.size >= 1 ? | |
345 | (packet.At(0) & MSGCODE_FRAME_EOM) != 0 : | |
346 | false; | |
347 | } | |
348 | ||
349 | bool IsACK(void) const | |
350 | { | |
351 | return packet.size >= 1 ? | |
352 | (packet.At(0) & MSGCODE_FRAME_ACK) != 0 : | |
353 | false; | |
354 | } | |
355 | ||
356 | bool IsError(void) const | |
357 | { | |
358 | cec_adapter_messagecode code = Message(); | |
359 | return (code == MSGCODE_HIGH_ERROR || | |
360 | code == MSGCODE_LOW_ERROR || | |
361 | code == MSGCODE_RECEIVE_FAILED || | |
362 | code == MSGCODE_COMMAND_REJECTED || | |
363 | code == MSGCODE_TRANSMIT_LINE_TIMEOUT || | |
364 | code == MSGCODE_TRANSMIT_FAILED_LINE || | |
365 | code == MSGCODE_TRANSMIT_FAILED_ACK || | |
366 | code == MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA || | |
367 | code == MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE); | |
368 | } | |
369 | ||
370 | bool NeedsRetry(void) const | |
371 | { | |
372 | return reply == MSGCODE_NOTHING || | |
373 | reply == MSGCODE_RECEIVE_FAILED || | |
374 | reply == MSGCODE_TIMEOUT_ERROR || | |
375 | reply == MSGCODE_TRANSMIT_FAILED_LINE || | |
376 | reply == MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA || | |
377 | reply == MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE || | |
378 | reply == MSGCODE_TRANSMIT_LINE_TIMEOUT; | |
379 | } | |
380 | ||
381 | cec_logical_address Initiator(void) const | |
382 | { | |
383 | return packet.size >= 2 ? | |
384 | (cec_logical_address) (packet.At(1) >> 4) : | |
385 | CECDEVICE_UNKNOWN; | |
386 | } | |
387 | ||
388 | cec_logical_address Destination(void) const | |
389 | { | |
390 | return packet.size >= 2 ? | |
391 | (cec_logical_address) (packet.At(1) & 0xF) : | |
392 | CECDEVICE_UNKNOWN; | |
393 | } | |
394 | ||
960f33c6 LOK |
395 | uint8_t maxTries; |
396 | uint8_t tries; | |
397 | cec_adapter_messagecode reply; | |
398 | cec_datapacket packet; | |
399 | cec_adapter_message_state state; | |
400 | int32_t transmit_timeout; | |
401 | bool isTransmission; | |
402 | bool expectControllerAck; | |
403 | uint8_t lineTimeout; | |
404 | uint8_t retryTimeout; | |
405 | PLATFORM::CEvent event; | |
ef7696f5 LOK |
406 | }; |
407 | } |