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