rpi: don't try to re-register a logical address when the logical address isn't regist...
[deb_libcec.git] / src / lib / adapter / RPi / RPiCECAdapterCommunication.cpp
1 /*
2 * This file is part of the libCEC(R) library.
3 *
4 * libCEC(R) is Copyright (C) 2011-2012 Pulse-Eight Limited. All rights reserved.
5 * libCEC(R) is an original work, containing original code.
6 *
7 * libCEC(R) is a trademark of Pulse-Eight Limited.
8 *
9 * This program is dual-licensed; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 *
23 *
24 * Alternatively, you can license this library under a commercial license,
25 * please contact Pulse-Eight Licensing for more information.
26 *
27 * For more information contact:
28 * Pulse-Eight Licensing <license@pulse-eight.com>
29 * http://www.pulse-eight.com/
30 * http://www.pulse-eight.net/
31 */
32
33 #include "env.h"
34
35 #if defined(HAVE_RPI_API)
36 #include "RPiCECAdapterCommunication.h"
37
38 extern "C" {
39 #include <bcm_host.h>
40 }
41
42 #include "lib/CECTypeUtils.h"
43 #include "lib/LibCEC.h"
44 #include "lib/platform/util/StdString.h"
45 #include "RPiCECAdapterMessageQueue.h"
46
47 using namespace CEC;
48 using namespace PLATFORM;
49
50 #define LIB_CEC m_callback->GetLib()
51
52 static bool g_bHostInited = false;
53
54 // callback for the RPi CEC service
55 void rpi_cec_callback(void *callback_data, uint32_t p0, uint32_t p1, uint32_t p2, uint32_t p3, uint32_t p4)
56 {
57 if (callback_data)
58 static_cast<CRPiCECAdapterCommunication *>(callback_data)->OnDataReceived(p0, p1, p2, p3, p4);
59 }
60
61 CRPiCECAdapterCommunication::CRPiCECAdapterCommunication(IAdapterCommunicationCallback *callback) :
62 IAdapterCommunication(callback),
63 m_logicalAddress(CECDEVICE_UNKNOWN),
64 m_bLogicalAddressChanged(false),
65 m_previousLogicalAddress(CECDEVICE_FREEUSE),
66 m_bLogicalAddressRegistered(false)
67 {
68 m_queue = new CRPiCECAdapterMessageQueue(this);
69 }
70
71 CRPiCECAdapterCommunication::~CRPiCECAdapterCommunication(void)
72 {
73 delete(m_queue);
74 Close();
75 }
76
77 const char *ToString(const VC_CEC_ERROR_T error)
78 {
79 switch(error)
80 {
81 case VC_CEC_SUCCESS:
82 return "success";
83 case VC_CEC_ERROR_NO_ACK:
84 return "no ack";
85 case VC_CEC_ERROR_SHUTDOWN:
86 return "shutdown";
87 case VC_CEC_ERROR_BUSY:
88 return "device is busy";
89 case VC_CEC_ERROR_NO_LA:
90 return "no logical address";
91 case VC_CEC_ERROR_NO_PA:
92 return "no physical address";
93 case VC_CEC_ERROR_NO_TOPO:
94 return "no topology";
95 case VC_CEC_ERROR_INVALID_FOLLOWER:
96 return "invalid follower";
97 case VC_CEC_ERROR_INVALID_ARGUMENT:
98 return "invalid arg";
99 default:
100 return "unknown";
101 }
102 }
103
104 bool CRPiCECAdapterCommunication::IsInitialised(void)
105 {
106 CLockObject lock(m_mutex);
107 return m_bInitialised;
108 }
109
110 void CRPiCECAdapterCommunication::OnDataReceived(uint32_t header, uint32_t p0, uint32_t p1, uint32_t p2, uint32_t p3)
111 {
112 VC_CEC_NOTIFY_T reason = (VC_CEC_NOTIFY_T)CEC_CB_REASON(header);
113
114 #ifdef CEC_DEBUGGING
115 LIB_CEC->AddLog(CEC_LOG_DEBUG, "received data: header:%08X p0:%08X p1:%08X p2:%08X p3:%08X reason:%x", header, p0, p1, p2, p3, reason);
116 #endif
117
118 switch (reason)
119 {
120 case VC_CEC_RX:
121 // CEC data received
122 {
123 // translate into a VC_CEC_MESSAGE_T
124 VC_CEC_MESSAGE_T message;
125 vc_cec_param2message(header, p0, p1, p2, p3, &message);
126
127 // translate to a cec_command
128 cec_command command;
129 cec_command::Format(command,
130 (cec_logical_address)message.initiator,
131 (cec_logical_address)message.follower,
132 (cec_opcode)CEC_CB_OPCODE(p0));
133
134 // copy parameters
135 for (uint8_t iPtr = 1; iPtr < message.length; iPtr++)
136 command.PushBack(message.payload[iPtr]);
137
138 // send to libCEC
139 m_callback->OnCommandReceived(command);
140 }
141 break;
142 case VC_CEC_TX:
143 {
144 // handle response to a command that was sent earlier
145 m_queue->MessageReceived((cec_opcode)CEC_CB_OPCODE(p0), (cec_logical_address)CEC_CB_INITIATOR(p0), (cec_logical_address)CEC_CB_FOLLOWER(p0), CEC_CB_RC(header));
146 }
147 break;
148 case VC_CEC_BUTTON_PRESSED:
149 case VC_CEC_REMOTE_PRESSED:
150 {
151 // translate into a cec_command
152 cec_command command;
153 cec_command::Format(command,
154 (cec_logical_address)CEC_CB_INITIATOR(p0),
155 (cec_logical_address)CEC_CB_FOLLOWER(p0),
156 reason == VC_CEC_BUTTON_PRESSED ? CEC_OPCODE_USER_CONTROL_PRESSED : CEC_OPCODE_VENDOR_REMOTE_BUTTON_DOWN);
157 command.parameters.PushBack((uint8_t)CEC_CB_OPERAND1(p0));
158
159 // send to libCEC
160 m_callback->OnCommandReceived(command);
161 }
162 break;
163 case VC_CEC_BUTTON_RELEASE:
164 case VC_CEC_REMOTE_RELEASE:
165 {
166 // translate into a cec_command
167 cec_command command;
168 cec_command::Format(command,
169 (cec_logical_address)CEC_CB_INITIATOR(p0),
170 (cec_logical_address)CEC_CB_FOLLOWER(p0),
171 reason == VC_CEC_BUTTON_PRESSED ? CEC_OPCODE_USER_CONTROL_RELEASE : CEC_OPCODE_VENDOR_REMOTE_BUTTON_UP);
172 command.parameters.PushBack((uint8_t)CEC_CB_OPERAND1(p0));
173
174 // send to libCEC
175 m_callback->OnCommandReceived(command);
176 }
177 break;
178 case VC_CEC_LOGICAL_ADDR:
179 {
180 CLockObject lock(m_mutex);
181 m_previousLogicalAddress = m_logicalAddress;
182 if (CEC_CB_RC(header) == VCHIQ_SUCCESS)
183 {
184 m_bLogicalAddressChanged = true;
185 m_logicalAddress = (cec_logical_address)(p0 & 0xF);
186 LIB_CEC->AddLog(CEC_LOG_DEBUG, "logical address changed to %s (%x)", LIB_CEC->ToString(m_logicalAddress), m_logicalAddress);
187 }
188 else
189 {
190 m_logicalAddress = CECDEVICE_FREEUSE;
191 LIB_CEC->AddLog(CEC_LOG_DEBUG, "failed to change the logical address, reset to %s (%x)", LIB_CEC->ToString(m_logicalAddress), m_logicalAddress);
192 }
193 m_logicalAddressCondition.Signal();
194 }
195 break;
196 case VC_CEC_LOGICAL_ADDR_LOST:
197 {
198 // the logical address was taken by another device
199 cec_logical_address previousAddress = m_logicalAddress == CECDEVICE_BROADCAST ? m_previousLogicalAddress : m_logicalAddress;
200 m_logicalAddress = CECDEVICE_UNKNOWN;
201
202 // notify libCEC that we lost our LA when the connection was initialised
203 bool bNotify(false);
204 {
205 CLockObject lock(m_mutex);
206 bNotify = m_bInitialised && m_bLogicalAddressRegistered;
207 }
208 if (bNotify)
209 m_callback->HandleLogicalAddressLost(previousAddress);
210 }
211 break;
212 case VC_CEC_TOPOLOGY:
213 break;
214 default:
215 LIB_CEC->AddLog(CEC_LOG_DEBUG, "ignoring unknown reason %x", reason);
216 break;
217 }
218 }
219
220 int CRPiCECAdapterCommunication::InitHostCEC(void)
221 {
222 VCHIQ_INSTANCE_T vchiq_instance;
223 int iResult;
224
225 if ((iResult = vchiq_initialise(&vchiq_instance)) != VCHIQ_SUCCESS)
226 {
227 LIB_CEC->AddLog(CEC_LOG_ERROR, "%s - vchiq_initialise failed (%d)", __FUNCTION__, iResult);
228 CStdString strError;
229 strError.Format("%s - vchiq_initialise failed (%d)", __FUNCTION__, iResult);
230 m_strError = strError;
231 return iResult;
232 }
233 LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s - vchiq_initialise succeeded", __FUNCTION__);
234
235 if ((iResult = vchi_initialise(&m_vchi_instance)) != VCHIQ_SUCCESS)
236 {
237 LIB_CEC->AddLog(CEC_LOG_ERROR, "%s - vchi_initialise failed (%d)", __FUNCTION__, iResult);
238 CStdString strError;
239 strError.Format("%s - vchi_initialise failed (%d)", __FUNCTION__, iResult);
240 m_strError = strError;
241 return iResult;
242 }
243 LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s - vchi_initialise succeeded", __FUNCTION__);
244
245 vchiq_instance = (VCHIQ_INSTANCE_T)m_vchi_instance;
246
247 m_vchi_connection = vchi_create_connection(single_get_func_table(),
248 vchi_mphi_message_driver_func_table());
249
250 if ((iResult = vchi_connect(&m_vchi_connection, 1, m_vchi_instance)) != VCHIQ_SUCCESS)
251 {
252 LIB_CEC->AddLog(CEC_LOG_ERROR, "%s - vchi_connect failed (%d)", __FUNCTION__, iResult);
253 CStdString strError;
254 strError.Format("%s - vchi_connect failed (%d)", __FUNCTION__, iResult);
255 m_strError = strError;
256 return iResult;
257 }
258 LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s - vchi_connect succeeded", __FUNCTION__);
259
260 return VCHIQ_SUCCESS;
261 }
262
263 bool CRPiCECAdapterCommunication::Open(uint32_t iTimeoutMs /* = CEC_DEFAULT_CONNECT_TIMEOUT */, bool UNUSED(bSkipChecks) /* = false */, bool bStartListening)
264 {
265 Close();
266
267 if (InitHostCEC() != VCHIQ_SUCCESS)
268 return false;
269
270 if (bStartListening)
271 {
272 // enable passive mode
273 vc_cec_set_passive(true);
274
275 // register the callback
276 vc_cec_register_callback(((CECSERVICE_CALLBACK_T)rpi_cec_callback), (void*)this);
277
278 // release previous LA
279 vc_cec_release_logical_address();
280 if (!m_logicalAddressCondition.Wait(m_mutex, m_bLogicalAddressChanged, iTimeoutMs))
281 {
282 LIB_CEC->AddLog(CEC_LOG_ERROR, "failed to release the previous LA");
283 return false;
284 }
285
286 // register LA "freeuse"
287 if (RegisterLogicalAddress(CECDEVICE_FREEUSE))
288 {
289 LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s - vc_cec initialised", __FUNCTION__);
290 CLockObject lock(m_mutex);
291 m_bInitialised = true;
292 }
293 else
294 LIB_CEC->AddLog(CEC_LOG_ERROR, "%s - vc_cec could not be initialised", __FUNCTION__);
295 }
296
297 return true;
298 }
299
300 uint16_t CRPiCECAdapterCommunication::GetPhysicalAddress(void)
301 {
302 uint16_t iPA(CEC_INVALID_PHYSICAL_ADDRESS);
303 if (!IsInitialised())
304 return iPA;
305
306 if (vc_cec_get_physical_address(&iPA) == VCHIQ_SUCCESS)
307 LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s - physical address = %04x", __FUNCTION__, iPA);
308 else
309 {
310 LIB_CEC->AddLog(CEC_LOG_WARNING, "%s - failed to get the physical address", __FUNCTION__);
311 iPA = CEC_INVALID_PHYSICAL_ADDRESS;
312 }
313
314 return iPA;
315 }
316
317 void CRPiCECAdapterCommunication::Close(void)
318 {
319 {
320 CLockObject lock(m_mutex);
321 if (m_bInitialised)
322 m_bInitialised = false;
323 else
324 return;
325 }
326 UnregisterLogicalAddress();
327
328 // disable passive mode
329 vc_cec_set_passive(false);
330
331 if (!g_bHostInited)
332 {
333 g_bHostInited = false;
334 bcm_host_deinit();
335 }
336 }
337
338 std::string CRPiCECAdapterCommunication::GetError(void) const
339 {
340 std::string strError(m_strError);
341 return strError;
342 }
343
344 cec_adapter_message_state CRPiCECAdapterCommunication::Write(const cec_command &data, bool &UNUSED(bRetry), uint8_t UNUSED(iLineTimeout), bool bIsReply)
345 {
346 // ensure that the source LA is registered
347 if (!RegisterLogicalAddress(data.initiator))
348 {
349 LIB_CEC->AddLog(CEC_LOG_DEBUG, "failed to register logical address %s (%X)", CCECTypeUtils::ToString(data.initiator), data.initiator);
350 return (data.initiator == data.destination) ? ADAPTER_MESSAGE_STATE_SENT_NOT_ACKED : ADAPTER_MESSAGE_STATE_ERROR;
351 }
352
353 if (!data.opcode_set && data.initiator == data.destination)
354 {
355 // registration of the logical address would have failed
356 return ADAPTER_MESSAGE_STATE_SENT_NOT_ACKED;
357 }
358
359 return m_queue->Write(data, bIsReply) ? ADAPTER_MESSAGE_STATE_SENT_ACKED : ADAPTER_MESSAGE_STATE_SENT_NOT_ACKED;
360 }
361
362 uint16_t CRPiCECAdapterCommunication::GetFirmwareVersion(void)
363 {
364 return VC_CECSERVICE_VER;
365 }
366
367 cec_logical_address CRPiCECAdapterCommunication::GetLogicalAddress(void)
368 {
369 {
370 CLockObject lock(m_mutex);
371 if (m_logicalAddress != CECDEVICE_UNKNOWN)
372 return m_logicalAddress;
373 }
374
375 CEC_AllDevices_T address;
376 return (vc_cec_get_logical_address(&address) == VCHIQ_SUCCESS) ?
377 (cec_logical_address)address : CECDEVICE_UNKNOWN;
378 }
379
380 bool CRPiCECAdapterCommunication::UnregisterLogicalAddress(void)
381 {
382 CLockObject lock(m_mutex);
383 if (m_logicalAddress == CECDEVICE_UNKNOWN ||
384 m_logicalAddress == CECDEVICE_BROADCAST)
385 return true;
386
387 LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s - releasing previous logical address", __FUNCTION__);
388 {
389 CLockObject lock(m_mutex);
390 m_bLogicalAddressRegistered = false;
391 m_bLogicalAddressChanged = false;
392 }
393
394 vc_cec_release_logical_address();
395
396 return m_logicalAddressCondition.Wait(m_mutex, m_bLogicalAddressChanged);
397 }
398
399 bool CRPiCECAdapterCommunication::RegisterLogicalAddress(const cec_logical_address address)
400 {
401 {
402 CLockObject lock(m_mutex);
403 if (m_logicalAddress == address)
404 return true;
405 }
406
407 if (!UnregisterLogicalAddress())
408 return false;
409
410 LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s - registering address %x", __FUNCTION__, address);
411
412 CLockObject lock(m_mutex);
413 m_bLogicalAddressChanged = false;
414 vc_cec_poll_address((CEC_AllDevices_T)address);
415
416 // register the new LA
417 int iRetval = vc_cec_set_logical_address((CEC_AllDevices_T)address, (CEC_DEVICE_TYPE_T)CCECTypeUtils::GetType(address), CEC_VENDOR_ID_BROADCOM);
418 if (iRetval != VCHIQ_SUCCESS)
419 {
420 LIB_CEC->AddLog(CEC_LOG_ERROR, "%s - vc_cec_set_logical_address(%X) returned %s (%d)", __FUNCTION__, address, ToString((VC_CEC_ERROR_T)iRetval), iRetval);
421 return false;
422 }
423
424 if (m_logicalAddressCondition.Wait(m_mutex, m_bLogicalAddressChanged))
425 {
426 m_bLogicalAddressRegistered = true;
427 return true;
428 }
429 return false;
430 }
431
432 cec_logical_addresses CRPiCECAdapterCommunication::GetLogicalAddresses(void)
433 {
434 cec_logical_addresses addresses; addresses.Clear();
435 cec_logical_address current = GetLogicalAddress();
436 if (current != CECDEVICE_UNKNOWN)
437 addresses.Set(current);
438
439 return addresses;
440 }
441
442 bool CRPiCECAdapterCommunication::SetLogicalAddresses(const cec_logical_addresses &addresses)
443 {
444 // the current generation RPi only supports 1 LA, so just ensure that the primary address is registered
445 return SupportsSourceLogicalAddress(addresses.primary) &&
446 RegisterLogicalAddress(addresses.primary);
447 }
448
449 void CRPiCECAdapterCommunication::InitHost(void)
450 {
451 if (!g_bHostInited)
452 {
453 g_bHostInited = true;
454 bcm_host_init();
455 }
456 }
457
458 #endif