cec: fixed - atoi returns an int, not an int8_t
[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 event(false)
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;
93 //TODO
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 = ToString(Message());
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 static const char *ToString(cec_adapter_messagecode msgCode)
143 {
144 switch (msgCode)
145 {
146 case MSGCODE_NOTHING:
147 return "NOTHING";
148 case MSGCODE_PING:
149 return "PING";
150 case MSGCODE_TIMEOUT_ERROR:
151 return "TIMEOUT";
152 case MSGCODE_HIGH_ERROR:
153 return "HIGH_ERROR";
154 case MSGCODE_LOW_ERROR:
155 return "LOW_ERROR";
156 case MSGCODE_FRAME_START:
157 return "FRAME_START";
158 case MSGCODE_FRAME_DATA:
159 return "FRAME_DATA";
160 case MSGCODE_RECEIVE_FAILED:
161 return "RECEIVE_FAILED";
162 case MSGCODE_COMMAND_ACCEPTED:
163 return "COMMAND_ACCEPTED";
164 case MSGCODE_COMMAND_REJECTED:
165 return "COMMAND_REJECTED";
166 case MSGCODE_SET_ACK_MASK:
167 return "SET_ACK_MASK";
168 case MSGCODE_TRANSMIT:
169 return "TRANSMIT";
170 case MSGCODE_TRANSMIT_EOM:
171 return "TRANSMIT_EOM";
172 case MSGCODE_TRANSMIT_IDLETIME:
173 return "TRANSMIT_IDLETIME";
174 case MSGCODE_TRANSMIT_ACK_POLARITY:
175 return "TRANSMIT_ACK_POLARITY";
176 case MSGCODE_TRANSMIT_LINE_TIMEOUT:
177 return "TRANSMIT_LINE_TIMEOUT";
178 case MSGCODE_TRANSMIT_SUCCEEDED:
179 return "TRANSMIT_SUCCEEDED";
180 case MSGCODE_TRANSMIT_FAILED_LINE:
181 return "TRANSMIT_FAILED_LINE";
182 case MSGCODE_TRANSMIT_FAILED_ACK:
183 return "TRANSMIT_FAILED_ACK";
184 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA:
185 return "TRANSMIT_FAILED_TIMEOUT_DATA";
186 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE:
187 return "TRANSMIT_FAILED_TIMEOUT_LINE";
188 case MSGCODE_FIRMWARE_VERSION:
189 return "FIRMWARE_VERSION";
190 case MSGCODE_START_BOOTLOADER:
191 return "START_BOOTLOADER";
192 case MSGCODE_FRAME_EOM:
193 return "FRAME_EOM";
194 case MSGCODE_FRAME_ACK:
195 return "FRAME_ACK";
196 case MSGCODE_SET_POWERSTATE:
197 return "SET_POWERSTATE";
198 case MSGCODE_SET_CONTROLLED:
199 return "SET_CONTROLLED";
200 case MSGCODE_GET_AUTO_ENABLED:
201 return "GET_AUTO_ENABLED";
202 case MSGCODE_SET_AUTO_ENABLED:
203 return "SET_AUTO_ENABLED";
204 case MSGCODE_GET_DEFAULT_LOGICAL_ADDRESS:
205 return "GET_DEFAULT_LOGICAL_ADDRESS";
206 case MSGCODE_SET_DEFAULT_LOGICAL_ADDRESS:
207 return "SET_DEFAULT_LOGICAL_ADDRESS";
208 case MSGCODE_GET_LOGICAL_ADDRESS_MASK:
209 return "GET_LOGICAL_ADDRESS_MASK";
210 case MSGCODE_SET_LOGICAL_ADDRESS_MASK:
211 return "SET_LOGICAL_ADDRESS_MASK";
212 case MSGCODE_GET_PHYSICAL_ADDRESS:
213 return "GET_PHYSICAL_ADDRESS";
214 case MSGCODE_SET_PHYSICAL_ADDRESS:
215 return "SET_PHYSICAL_ADDRESS";
216 case MSGCODE_GET_DEVICE_TYPE:
217 return "GET_DEVICE_TYPE";
218 case MSGCODE_SET_DEVICE_TYPE:
219 return "SET_DEVICE_TYPE";
220 case MSGCODE_GET_HDMI_VERSION:
221 return "GET_HDMI_VERSION";
222 case MSGCODE_SET_HDMI_VERSION:
223 return "SET_HDMI_VERSION";
224 case MSGCODE_GET_OSD_NAME:
225 return "GET_OSD_NAME";
226 case MSGCODE_SET_OSD_NAME:
227 return "SET_OSD_NAME";
228 case MSGCODE_WRITE_EEPROM:
229 return "WRITE_EEPROM";
230 }
231
232 return "unknown";
233 }
234
235 uint8_t operator[](uint8_t pos) const
236 {
237 return packet[pos];
238 }
239
240 uint8_t At(uint8_t pos) const
241 {
242 return packet[pos];
243 }
244
245 uint8_t Size(void) const
246 {
247 return packet.size;
248 }
249
250 bool IsEmpty(void) const
251 {
252 return packet.IsEmpty();
253 }
254
255 void Clear(void)
256 {
257 state = ADAPTER_MESSAGE_STATE_UNKNOWN;
258 transmit_timeout = CEC_DEFAULT_TRANSMIT_TIMEOUT;
259 packet.Clear();
260 maxTries = CEC_DEFAULT_TRANSMIT_RETRIES + 1;
261 tries = 0;
262 reply = MSGCODE_NOTHING;
263 isTransmission = true;
264 expectControllerAck = true;
265 lineTimeout = 3;
266 retryTimeout = 3;
267 }
268
269 void Shift(uint8_t iShiftBy)
270 {
271 packet.Shift(iShiftBy);
272 }
273
274 void Append(CCECAdapterMessage &data)
275 {
276 Append(data.packet);
277 }
278
279 void Append(cec_datapacket &data)
280 {
281 for (uint8_t iPtr = 0; iPtr < data.size; iPtr++)
282 PushBack(data[iPtr]);
283 }
284
285 void PushBack(uint8_t add)
286 {
287 packet.PushBack(add);
288 }
289
290 void PushEscaped(uint8_t byte)
291 {
292 if (byte >= MSGESC)
293 {
294 PushBack(MSGESC);
295 PushBack(byte - ESCOFFSET);
296 }
297 else
298 {
299 PushBack(byte);
300 }
301 }
302
303 cec_adapter_messagecode Message(void) const
304 {
305 return packet.size >= 1 ?
306 (cec_adapter_messagecode) (packet.At(0) & ~(MSGCODE_FRAME_EOM | MSGCODE_FRAME_ACK)) :
307 MSGCODE_NOTHING;
308 }
309
310 bool IsEOM(void) const
311 {
312 return packet.size >= 1 ?
313 (packet.At(0) & MSGCODE_FRAME_EOM) != 0 :
314 false;
315 }
316
317 bool IsACK(void) const
318 {
319 return packet.size >= 1 ?
320 (packet.At(0) & MSGCODE_FRAME_ACK) != 0 :
321 false;
322 }
323
324 bool IsError(void) const
325 {
326 cec_adapter_messagecode code = Message();
327 return (code == MSGCODE_HIGH_ERROR ||
328 code == MSGCODE_LOW_ERROR ||
329 code == MSGCODE_RECEIVE_FAILED ||
330 code == MSGCODE_COMMAND_REJECTED ||
331 code == MSGCODE_TRANSMIT_LINE_TIMEOUT ||
332 code == MSGCODE_TRANSMIT_FAILED_LINE ||
333 code == MSGCODE_TRANSMIT_FAILED_ACK ||
334 code == MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA ||
335 code == MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE);
336 }
337
338 bool NeedsRetry(void) const
339 {
340 return reply == MSGCODE_NOTHING ||
341 reply == MSGCODE_RECEIVE_FAILED ||
342 reply == MSGCODE_TIMEOUT_ERROR ||
343 reply == MSGCODE_TRANSMIT_FAILED_LINE ||
344 reply == MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA ||
345 reply == MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE ||
346 reply == MSGCODE_TRANSMIT_LINE_TIMEOUT;
347 }
348
349 cec_logical_address Initiator(void) const
350 {
351 return packet.size >= 2 ?
352 (cec_logical_address) (packet.At(1) >> 4) :
353 CECDEVICE_UNKNOWN;
354 }
355
356 cec_logical_address Destination(void) const
357 {
358 return packet.size >= 2 ?
359 (cec_logical_address) (packet.At(1) & 0xF) :
360 CECDEVICE_UNKNOWN;
361 }
362
363 uint8_t maxTries;
364 uint8_t tries;
365 cec_adapter_messagecode reply;
366 cec_datapacket packet;
367 cec_adapter_message_state state;
368 int32_t transmit_timeout;
369 bool isTransmission;
370 bool expectControllerAck;
371 uint8_t lineTimeout;
372 uint8_t retryTimeout;
373 PLATFORM::CEvent event;
374 };
375 }