cec: fixed - MSGCODE_START_BOOTLOADER doesn't respond with MSGCODE_COMMAND_ACCEPTED
[deb_libcec.git] / src / lib / adapter / AdapterMessage.h
... / ...
CommitLineData
1#pragma once
2/*
3 * This file is part of the libCEC(R) library.
4 *
5 * libCEC(R) is Copyright (C) 2011 Pulse-Eight Limited. All rights reserved.
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
34namespace CEC
35{
36 typedef enum cec_adapter_message_state
37 {
38 ADAPTER_MESSAGE_STATE_UNKNOWN = 0,
39 ADAPTER_MESSAGE_STATE_WAITING_TO_BE_SENT,
40 ADAPTER_MESSAGE_STATE_SENT,
41 ADAPTER_MESSAGE_STATE_SENT_NOT_ACKED,
42 ADAPTER_MESSAGE_STATE_SENT_ACKED,
43 ADAPTER_MESSAGE_STATE_INCOMING,
44 ADAPTER_MESSAGE_STATE_ERROR
45 } cec_adapter_message_state;
46
47
48 class CCECAdapterMessage
49 {
50 public:
51 CCECAdapterMessage(void)
52 {
53 Clear();
54 }
55
56 CCECAdapterMessage(const cec_command &command)
57 {
58 Clear();
59
60 //set ack polarity to high when transmitting to the broadcast address
61 //set ack polarity low when transmitting to any other address
62 PushBack(MSGSTART);
63 PushEscaped(MSGCODE_TRANSMIT_ACK_POLARITY);
64 if (command.destination == CECDEVICE_BROADCAST)
65 PushEscaped(CEC_TRUE);
66 else
67 PushEscaped(CEC_FALSE);
68 PushBack(MSGEND);
69
70 // add source and destination
71 PushBack(MSGSTART);
72 PushEscaped(command.opcode_set == 0 ? (uint8_t)MSGCODE_TRANSMIT_EOM : (uint8_t)MSGCODE_TRANSMIT);
73 PushBack(((uint8_t)command.initiator << 4) + (uint8_t)command.destination);
74 PushBack(MSGEND);
75
76 // add opcode
77 if (command.opcode_set == 1)
78 {
79 PushBack(MSGSTART);
80 PushEscaped(command.parameters.IsEmpty() ? (uint8_t)MSGCODE_TRANSMIT_EOM : (uint8_t)MSGCODE_TRANSMIT);
81 PushBack((uint8_t) command.opcode);
82 PushBack(MSGEND);
83
84 // add parameters
85 for (int8_t iPtr = 0; iPtr < command.parameters.size; iPtr++)
86 {
87 PushBack(MSGSTART);
88
89 if (iPtr == command.parameters.size - 1)
90 PushEscaped( MSGCODE_TRANSMIT_EOM);
91 else
92 PushEscaped(MSGCODE_TRANSMIT);
93
94 PushEscaped(command.parameters[iPtr]);
95
96 PushBack(MSGEND);
97 }
98 }
99
100 // set timeout
101 transmit_timeout = command.transmit_timeout;
102 //TODO
103 }
104
105 CCECAdapterMessage &operator=(const CCECAdapterMessage &msg)
106 {
107 packet = msg.packet;
108 state = msg.state;
109 return *this;
110 }
111
112 CStdString ToString(void) const
113 {
114 CStdString strMsg;
115 if (Size() == 0)
116 {
117 strMsg = "empty message";
118 }
119 else
120 {
121 strMsg = MessageCodeAsString();
122
123 switch (Message())
124 {
125 case MSGCODE_TIMEOUT_ERROR:
126 case MSGCODE_HIGH_ERROR:
127 case MSGCODE_LOW_ERROR:
128 {
129 uint32_t iLine = (Size() >= 3) ? (At(1) << 8) | At(2) : 0;
130 uint32_t iTime = (Size() >= 7) ? (At(3) << 24) | (At(4) << 16) | (At(5) << 8) | At(6) : 0;
131 strMsg.AppendFormat(" line:%u", iLine);
132 strMsg.AppendFormat(" time:%u", iTime);
133 }
134 break;
135 case MSGCODE_FRAME_START:
136 if (Size() >= 2)
137 strMsg.AppendFormat(" initiator:%1x destination:%1x ack:%s %s", Initiator(), Destination(), IsACK() ? "high" : "low", IsEOM() ? "eom" : "");
138 break;
139 case MSGCODE_FRAME_DATA:
140 if (Size() >= 2)
141 strMsg.AppendFormat(" %02x %s", At(1), IsEOM() ? "eom" : "");
142 break;
143 default:
144 break;
145 }
146 }
147
148 return strMsg;
149 }
150
151 CStdString MessageCodeAsString(void) const
152 {
153 CStdString strMsg;
154 switch (Message())
155 {
156 case MSGCODE_NOTHING:
157 strMsg = "NOTHING";
158 break;
159 case MSGCODE_PING:
160 strMsg = "PING";
161 break;
162 case MSGCODE_TIMEOUT_ERROR:
163 strMsg = "TIMEOUT";
164 break;
165 case MSGCODE_HIGH_ERROR:
166 strMsg = "HIGH_ERROR";
167 break;
168 case MSGCODE_LOW_ERROR:
169 strMsg = "LOW_ERROR";
170 break;
171 case MSGCODE_FRAME_START:
172 strMsg = "FRAME_START";
173 break;
174 case MSGCODE_FRAME_DATA:
175 strMsg = "FRAME_DATA";
176 break;
177 case MSGCODE_RECEIVE_FAILED:
178 strMsg = "RECEIVE_FAILED";
179 break;
180 case MSGCODE_COMMAND_ACCEPTED:
181 strMsg = "COMMAND_ACCEPTED";
182 break;
183 case MSGCODE_COMMAND_REJECTED:
184 strMsg = "COMMAND_REJECTED";
185 break;
186 case MSGCODE_SET_ACK_MASK:
187 strMsg = "SET_ACK_MASK";
188 break;
189 case MSGCODE_TRANSMIT:
190 strMsg = "TRANSMIT";
191 break;
192 case MSGCODE_TRANSMIT_EOM:
193 strMsg = "TRANSMIT_EOM";
194 break;
195 case MSGCODE_TRANSMIT_IDLETIME:
196 strMsg = "TRANSMIT_IDLETIME";
197 break;
198 case MSGCODE_TRANSMIT_ACK_POLARITY:
199 strMsg = "TRANSMIT_ACK_POLARITY";
200 break;
201 case MSGCODE_TRANSMIT_LINE_TIMEOUT:
202 strMsg = "TRANSMIT_LINE_TIMEOUT";
203 break;
204 case MSGCODE_TRANSMIT_SUCCEEDED:
205 strMsg = "TRANSMIT_SUCCEEDED";
206 break;
207 case MSGCODE_TRANSMIT_FAILED_LINE:
208 strMsg = "TRANSMIT_FAILED_LINE";
209 break;
210 case MSGCODE_TRANSMIT_FAILED_ACK:
211 strMsg = "TRANSMIT_FAILED_ACK";
212 break;
213 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA:
214 strMsg = "TRANSMIT_FAILED_TIMEOUT_DATA";
215 break;
216 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE:
217 strMsg = "TRANSMIT_FAILED_TIMEOUT_LINE";
218 break;
219 case MSGCODE_FIRMWARE_VERSION:
220 strMsg = "FIRMWARE_VERSION";
221 break;
222 case MSGCODE_START_BOOTLOADER:
223 strMsg = "START_BOOTLOADER";
224 break;
225 case MSGCODE_FRAME_EOM:
226 strMsg = "FRAME_EOM";
227 break;
228 case MSGCODE_FRAME_ACK:
229 strMsg = "FRAME_ACK";
230 break;
231 }
232
233 return strMsg;
234 }
235
236 uint8_t operator[](uint8_t pos) const
237 {
238 return packet[pos];
239 }
240
241 uint8_t At(uint8_t pos) const
242 {
243 return packet[pos];
244 }
245
246 uint8_t Size(void) const
247 {
248 return packet.size;
249 }
250
251 bool IsEmpty(void) const
252 {
253 return packet.IsEmpty();
254 }
255
256 void Clear(void)
257 {
258 state = ADAPTER_MESSAGE_STATE_UNKNOWN;
259 transmit_timeout = 0;
260 packet.Clear();
261 maxTries = CEC_DEFAULT_TRANSMIT_RETRIES + 1;
262 tries = 0;
263 reply = MSGCODE_NOTHING;
264 isTransmission = true;
265 expectControllerAck = true;
266 }
267
268 void Shift(uint8_t iShiftBy)
269 {
270 packet.Shift(iShiftBy);
271 }
272
273 void PushBack(uint8_t add)
274 {
275 packet.PushBack(add);
276 }
277
278 void PushEscaped(uint8_t byte)
279 {
280 if (byte >= MSGESC)
281 {
282 PushBack(MSGESC);
283 PushBack(byte - ESCOFFSET);
284 }
285 else
286 {
287 PushBack(byte);
288 }
289 }
290
291 cec_adapter_messagecode Message(void) const
292 {
293 return packet.size >= 1 ?
294 (cec_adapter_messagecode) (packet.At(0) & ~(MSGCODE_FRAME_EOM | MSGCODE_FRAME_ACK)) :
295 MSGCODE_NOTHING;
296 }
297
298 bool IsEOM(void) const
299 {
300 return packet.size >= 1 ?
301 (packet.At(0) & MSGCODE_FRAME_EOM) != 0 :
302 false;
303 }
304
305 bool IsACK(void) const
306 {
307 return packet.size >= 1 ?
308 (packet.At(0) & MSGCODE_FRAME_ACK) != 0 :
309 false;
310 }
311
312 bool IsError(void) const
313 {
314 cec_adapter_messagecode code = Message();
315 return (code == MSGCODE_HIGH_ERROR ||
316 code == MSGCODE_LOW_ERROR ||
317 code == MSGCODE_RECEIVE_FAILED ||
318 code == MSGCODE_COMMAND_REJECTED ||
319 code == MSGCODE_TRANSMIT_LINE_TIMEOUT ||
320 code == MSGCODE_TRANSMIT_FAILED_LINE ||
321 code == MSGCODE_TRANSMIT_FAILED_ACK ||
322 code == MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA ||
323 code == MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE);
324 }
325
326 bool NeedsRetry(void) const
327 {
328 return reply == MSGCODE_NOTHING ||
329 reply == MSGCODE_RECEIVE_FAILED ||
330 reply == MSGCODE_TIMEOUT_ERROR ||
331 reply == MSGCODE_TRANSMIT_FAILED_LINE ||
332 reply == MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA ||
333 reply == MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE ||
334 reply == MSGCODE_TRANSMIT_LINE_TIMEOUT;
335 }
336
337 cec_logical_address Initiator(void) const
338 {
339 return packet.size >= 2 ?
340 (cec_logical_address) (packet.At(1) >> 4) :
341 CECDEVICE_UNKNOWN;
342 }
343
344 cec_logical_address Destination(void) const
345 {
346 return packet.size >= 2 ?
347 (cec_logical_address) (packet.At(1) & 0xF) :
348 CECDEVICE_UNKNOWN;
349 }
350
351 uint8_t maxTries;
352 uint8_t tries;
353 cec_adapter_messagecode reply;
354 cec_datapacket packet;
355 cec_adapter_message_state state;
356 int32_t transmit_timeout;
357 bool isTransmission;
358 bool expectControllerAck;
359 PLATFORM::CMutex mutex;
360 PLATFORM::CCondition condition;
361 };
362}