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