Commit | Line | Data |
---|---|---|
5b6d8032 VM |
1 | /* |
2 | * This file is part of the libCEC(R) library. | |
3 | * | |
4 | * libCEC Exynos Code is Copyright (C) 2014 Valentin Manea | |
5 | * libCEC(R) is Copyright (C) 2011-2013 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 "env.h" | |
35 | #include <fcntl.h> | |
36 | #include <sys/ioctl.h> | |
37 | ||
38 | ||
39 | #if defined(HAVE_EXYNOS_API) | |
40 | #include "ExynosCEC.h" | |
41 | #include "ExynosCECAdapterCommunication.h" | |
42 | ||
43 | #include "lib/CECTypeUtils.h" | |
44 | #include "lib/LibCEC.h" | |
45 | #include "lib/platform/util/StdString.h" | |
46 | #include "lib/platform/util/buffer.h" | |
47 | ||
48 | using namespace std; | |
49 | using namespace CEC; | |
50 | using namespace PLATFORM; | |
51 | ||
52 | #define LIB_CEC m_callback->GetLib() | |
53 | ||
54 | ||
55 | CExynosCECAdapterCommunication::CExynosCECAdapterCommunication(IAdapterCommunicationCallback *callback) : | |
56 | IAdapterCommunication(callback), | |
57 | m_bLogicalAddressChanged(false) | |
58 | { | |
59 | CLockObject lock(m_mutex); | |
60 | ||
61 | m_logicalAddresses.Clear(); | |
62 | m_fd = INVALID_SOCKET_VALUE; | |
63 | } | |
64 | ||
65 | ||
66 | CExynosCECAdapterCommunication::~CExynosCECAdapterCommunication(void) | |
67 | { | |
68 | Close(); | |
69 | } | |
70 | ||
71 | ||
72 | bool CExynosCECAdapterCommunication::IsOpen(void) | |
73 | { | |
74 | return IsInitialised() && m_fd != INVALID_SOCKET_VALUE; | |
75 | } | |
76 | ||
77 | ||
78 | bool CExynosCECAdapterCommunication::Open(uint32_t UNUSED(iTimeoutMs), bool UNUSED(bSkipChecks), bool bStartListening) | |
79 | { | |
80 | if (m_fd != INVALID_SOCKET_VALUE) | |
81 | close(m_fd); | |
82 | ||
83 | if ((m_fd = open(CEC_EXYNOS_PATH, O_RDWR)) > 0) | |
84 | { | |
85 | if (!bStartListening || CreateThread()) { | |
86 | return true; | |
87 | } | |
88 | close(m_fd); | |
89 | } | |
90 | return false; | |
91 | } | |
92 | ||
93 | ||
94 | void CExynosCECAdapterCommunication::Close(void) | |
95 | { | |
96 | StopThread(0); | |
97 | ||
98 | close(m_fd); | |
99 | m_fd = INVALID_SOCKET_VALUE; | |
100 | } | |
101 | ||
102 | ||
103 | std::string CExynosCECAdapterCommunication::GetError(void) const | |
104 | { | |
105 | std::string strError(m_strError); | |
106 | return strError; | |
107 | } | |
108 | ||
109 | ||
110 | cec_adapter_message_state CExynosCECAdapterCommunication::Write( | |
111 | const cec_command &data, bool &UNUSED(bRetry), uint8_t UNUSED(iLineTimeout), bool UNUSED(bIsReply)) | |
112 | { | |
113 | uint8_t buffer[CEC_MAX_FRAME_SIZE]; | |
114 | int32_t size = 1; | |
115 | cec_adapter_message_state rc = ADAPTER_MESSAGE_STATE_ERROR; | |
116 | ||
117 | if (!IsOpen()) | |
118 | return rc; | |
119 | ||
120 | if ((size_t)data.parameters.size + data.opcode_set > sizeof(buffer)) | |
121 | { | |
122 | LIB_CEC->AddLog(CEC_LOG_ERROR, "%s: data size too large !", __func__); | |
123 | return ADAPTER_MESSAGE_STATE_ERROR; | |
124 | } | |
125 | ||
126 | buffer[0] = (data.initiator << 4) | (data.destination & 0x0f); | |
127 | ||
128 | if (data.opcode_set) | |
129 | { | |
130 | buffer[1] = data.opcode; | |
131 | size++; | |
132 | ||
133 | memcpy(&buffer[size], data.parameters.data, data.parameters.size); | |
134 | size += data.parameters.size; | |
135 | } | |
136 | ||
137 | if (write(m_fd, (void *)buffer, size) == size) | |
138 | { | |
139 | rc = ADAPTER_MESSAGE_STATE_SENT_ACKED; | |
140 | } | |
141 | else | |
142 | { | |
143 | LIB_CEC->AddLog(CEC_LOG_ERROR, "%s: write failed !", __func__); | |
144 | } | |
145 | ||
146 | return rc; | |
147 | } | |
148 | ||
149 | ||
150 | uint16_t CExynosCECAdapterCommunication::GetFirmwareVersion(void) | |
151 | { | |
152 | return 0; | |
153 | } | |
154 | ||
155 | ||
156 | cec_vendor_id CExynosCECAdapterCommunication::GetVendorId(void) | |
157 | { | |
158 | return cec_vendor_id(CEC_VENDOR_SAMSUNG); | |
159 | } | |
160 | ||
161 | ||
162 | uint16_t CExynosCECAdapterCommunication::GetPhysicalAddress(void) | |
163 | { | |
164 | uint16_t phys_addr = CEC_DEFAULT_PADDR; | |
165 | ||
166 | FILE *f = fopen(CEC_PADDR_NAME, "r"); | |
167 | if(f) { | |
168 | if(fscanf(f, "%hu", &phys_addr) != 1) | |
169 | phys_addr = CEC_DEFAULT_PADDR; | |
170 | ||
171 | fclose(f); | |
172 | } | |
173 | return phys_addr; | |
174 | } | |
175 | ||
176 | ||
177 | cec_logical_addresses CExynosCECAdapterCommunication::GetLogicalAddresses(void) | |
178 | { | |
179 | return m_logicalAddresses; | |
180 | } | |
181 | ||
182 | ||
183 | bool CExynosCECAdapterCommunication::SetLogicalAddresses(const cec_logical_addresses &addresses) | |
184 | { | |
185 | unsigned int log_addr = addresses.primary; | |
186 | CLockObject lock(m_mutex); | |
187 | ||
188 | if (!IsOpen()) | |
189 | return false; | |
190 | ||
191 | if (ioctl(m_fd, CEC_IOC_SETLADDR, &log_addr)) | |
192 | { | |
193 | LIB_CEC->AddLog(CEC_LOG_ERROR, "%s: IOCTL SetLogicalAddr failed !", __func__); | |
194 | return false; | |
195 | } | |
196 | m_logicalAddresses = addresses; | |
197 | m_bLogicalAddressChanged = true; | |
198 | ||
199 | return true; | |
200 | } | |
201 | ||
202 | ||
203 | void CExynosCECAdapterCommunication::HandleLogicalAddressLost(cec_logical_address UNUSED(oldAddress)) | |
204 | { | |
205 | unsigned int log_addr = CECDEVICE_BROADCAST; | |
206 | if (ioctl(m_fd, CEC_IOC_SETLADDR, &log_addr)) | |
207 | { | |
208 | LIB_CEC->AddLog(CEC_LOG_ERROR, "%s: IOCTL SetLogicalAddr failed !", __func__); | |
209 | } | |
210 | } | |
211 | ||
212 | ||
213 | void *CExynosCECAdapterCommunication::Process(void) | |
214 | { | |
215 | uint8_t buffer[CEC_MAX_FRAME_SIZE]; | |
216 | uint32_t size; | |
217 | fd_set rfds; | |
218 | cec_logical_address initiator, destination; | |
219 | ||
220 | if (!IsOpen()) | |
221 | return 0; | |
222 | ||
223 | FD_ZERO(&rfds); | |
224 | FD_SET(m_fd, &rfds); | |
225 | ||
226 | while (!IsStopped()) | |
227 | { | |
228 | if (select(m_fd + 1, &rfds, NULL, NULL, NULL) >= 0 ) | |
229 | { | |
230 | size = read(m_fd, buffer, CEC_MAX_FRAME_SIZE); | |
231 | ||
232 | if (size > 0) | |
233 | { | |
234 | initiator = cec_logical_address(buffer[0] >> 4); | |
235 | destination = cec_logical_address(buffer[0] & 0x0f); | |
236 | ||
237 | cec_command cmd; | |
238 | ||
239 | cec_command::Format( | |
240 | cmd, initiator, destination, | |
241 | ( size > 1 ) ? cec_opcode(buffer[1]) : CEC_OPCODE_NONE); | |
242 | ||
243 | for( uint8_t i = 2; i < size; i++ ) | |
244 | cmd.parameters.PushBack(buffer[i]); | |
245 | ||
246 | if (!IsStopped()) | |
247 | m_callback->OnCommandReceived(cmd); | |
248 | } | |
249 | } | |
250 | ||
251 | } | |
252 | ||
253 | return 0; | |
254 | } | |
255 | ||
256 | #endif // HAVE_EXYNOS_API |