3 * This file is part of the libCEC(R) library.
5 * libCEC(R) is Copyright (C) 2011 Pulse-Eight Limited. All rights reserved.
6 * libCEC(R) is an original work, containing original code.
8 * libCEC(R) is a trademark of Pulse-Eight Limited.
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.
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.
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.
25 * Alternatively, you can license this library under a commercial license,
26 * please contact Pulse-Eight Licensing for more information.
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/
35 #include "platform/threads.h"
36 #include "util/buffer.h"
41 class CCECAdapterMessage
44 CCECAdapterMessage(void) {}
45 CCECAdapterMessage(const cec_command
&command
)
49 //set ack polarity to high when transmitting to the broadcast address
50 //set ack polarity low when transmitting to any other address
52 push_escaped(MSGCODE_TRANSMIT_ACK_POLARITY
);
53 if (command
.destination
== CECDEVICE_BROADCAST
)
54 push_escaped(CEC_TRUE
);
56 push_escaped(CEC_FALSE
);
59 // add source and destination
61 push_escaped(MSGCODE_TRANSMIT
);
62 push_back(((uint8_t)command
.initiator
<< 4) + (uint8_t)command
.destination
);
67 push_escaped(command
.parameters
.empty() ? (uint8_t)MSGCODE_TRANSMIT_EOM
: (uint8_t)MSGCODE_TRANSMIT
);
68 push_back((uint8_t) command
.opcode
);
72 for (int8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
76 if (iPtr
== command
.parameters
.size
- 1)
77 push_escaped( MSGCODE_TRANSMIT_EOM
);
79 push_escaped(MSGCODE_TRANSMIT
);
81 push_escaped(command
.parameters
[iPtr
]);
87 CCECAdapterMessage
&operator =(const CCECAdapterMessage
&msg
)
93 bool empty(void) const { return packet
.empty(); }
94 uint8_t operator[](uint8_t pos
) const { return packet
[pos
]; }
95 uint8_t at(uint8_t pos
) const { return packet
[pos
]; }
96 uint8_t size(void) const { return packet
.size
; }
97 void clear(void) { packet
.clear(); }
98 void shift(uint8_t iShiftBy
) { packet
.shift(iShiftBy
); }
99 void push_back(uint8_t add
) { packet
.push_back(add
); }
100 cec_adapter_messagecode
message(void) const { return packet
.size
>= 1 ? (cec_adapter_messagecode
) (packet
.at(0) & ~(MSGCODE_FRAME_EOM
| MSGCODE_FRAME_ACK
)) : MSGCODE_NOTHING
; }
101 bool eom(void) const { return packet
.size
>= 1 ? (packet
.at(0) & MSGCODE_FRAME_EOM
) != 0 : false; }
102 bool ack(void) const { return packet
.size
>= 1 ? (packet
.at(0) & MSGCODE_FRAME_ACK
) != 0 : false; }
103 cec_logical_address
initiator(void) const { return packet
.size
>= 2 ? (cec_logical_address
) (packet
.at(1) >> 4) : CECDEVICE_UNKNOWN
; };
104 cec_logical_address
destination(void) const { return packet
.size
>= 2 ? (cec_logical_address
) (packet
.at(1) & 0xF) : CECDEVICE_UNKNOWN
; };
105 void push_escaped(int16_t byte
)
107 if (byte
>= MSGESC
&& byte
!= MSGSTART
)
110 push_back(byte
- ESCOFFSET
);
116 cec_datapacket packet
;
122 class CAdapterCommunication
: private CThread
125 CAdapterCommunication(CLibCEC
*controller
);
126 virtual ~CAdapterCommunication();
128 bool Open(const char *strPort
, uint16_t iBaudRate
= 38400, uint32_t iTimeoutMs
= 10000);
129 bool Read(CCECAdapterMessage
&msg
, uint32_t iTimeout
= 1000);
130 bool Write(const CCECAdapterMessage
&frame
);
131 bool PingAdapter(void);
133 bool IsOpen(void) const;
134 std::string
GetError(void) const;
138 bool StartBootloader(void);
139 bool SetAckMask(uint16_t iMask
);
142 void WriteNextCommand(void);
143 void AddData(uint8_t *data
, uint8_t iLen
);
144 bool ReadFromDevice(uint32_t iTimeout
);
146 CSerialPort
* m_port
;
147 CLibCEC
* m_controller
;
148 CecBuffer
<uint8_t> m_inBuffer
;
149 CecBuffer
<CCECAdapterMessage
> m_outBuffer
;
150 CMutex m_bufferMutex
;
151 CCondition m_rcvCondition
;