rpi: handle VC_CEC_LOGICAL_ADDR_LOST in the rpi communication class itself, don't...
[deb_libcec.git] / src / lib / adapter / RPi / RPiCECAdapterCommunication.cpp
CommitLineData
29104708
LOK
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
38extern "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
47using namespace CEC;
48using namespace PLATFORM;
49
50#define LIB_CEC m_callback->GetLib()
51
52static bool g_bHostInited = false;
53
54// callback for the RPi CEC service
55void 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
f60ee8b3
LOK
61CRPiCECAdapterFindNewLogicalAddress::CRPiCECAdapterFindNewLogicalAddress(CRPiCECAdapterCommunication* communication, const cec_logical_address address) :
62 m_communication(communication),
63 m_address(address) { }
64
65void *CRPiCECAdapterFindNewLogicalAddress::Process(void)
66{
67 cec_logical_address newAddress(CECDEVICE_UNKNOWN);
68 for (unsigned int iLA = CECDEVICE_RECORDINGDEVICE1; newAddress == CECDEVICE_UNKNOWN && iLA < CECDEVICE_BROADCAST; iLA++)
69 {
70 if (CCECTypeUtils::GetType((cec_logical_address)iLA) == CCECTypeUtils::GetType(m_address) &&
71 m_communication->SupportsSourceLogicalAddress((cec_logical_address)iLA) &&
72 m_communication->RegisterLogicalAddress((cec_logical_address)iLA))
73 newAddress = (cec_logical_address)iLA;
74 }
75
76 m_communication->m_callback->HandleLogicalAddressLost(m_address, newAddress);
77 return NULL;
78}
79
29104708
LOK
80CRPiCECAdapterCommunication::CRPiCECAdapterCommunication(IAdapterCommunicationCallback *callback) :
81 IAdapterCommunication(callback),
82 m_logicalAddress(CECDEVICE_UNKNOWN),
f60ee8b3
LOK
83 m_bLogicalAddressChanged(false),
84 m_laLost(NULL)
29104708
LOK
85{
86 m_queue = new CRPiCECAdapterMessageQueue(this);
87}
88
89CRPiCECAdapterCommunication::~CRPiCECAdapterCommunication(void)
90{
f60ee8b3
LOK
91 if (m_laLost)
92 {
93 m_laLost->StopThread();
94 delete m_laLost;
95 m_laLost = NULL;
96 }
29104708
LOK
97 delete(m_queue);
98 Close();
99}
100
101const char *ToString(const VC_CEC_ERROR_T error)
102{
103 switch(error)
104 {
105 case VC_CEC_SUCCESS:
106 return "success";
107 case VC_CEC_ERROR_NO_ACK:
108 return "no ack";
109 case VC_CEC_ERROR_SHUTDOWN:
110 return "shutdown";
111 case VC_CEC_ERROR_BUSY:
112 return "device is busy";
113 case VC_CEC_ERROR_NO_LA:
114 return "no logical address";
115 case VC_CEC_ERROR_NO_PA:
116 return "no physical address";
117 case VC_CEC_ERROR_NO_TOPO:
118 return "no topology";
119 case VC_CEC_ERROR_INVALID_FOLLOWER:
120 return "invalid follower";
121 case VC_CEC_ERROR_INVALID_ARGUMENT:
122 return "invalid arg";
123 default:
124 return "unknown";
125 }
126}
127
128bool CRPiCECAdapterCommunication::IsInitialised(void)
129{
130 CLockObject lock(m_mutex);
131 return m_bInitialised;
132}
133
134void CRPiCECAdapterCommunication::OnDataReceived(uint32_t header, uint32_t p0, uint32_t p1, uint32_t p2, uint32_t p3)
135{
136 VC_CEC_NOTIFY_T reason = (VC_CEC_NOTIFY_T)CEC_CB_REASON(header);
137
138 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);
139
140 switch (reason)
141 {
142 case VC_CEC_RX:
143 // CEC data received
144 {
145 // translate into a VC_CEC_MESSAGE_T
146 VC_CEC_MESSAGE_T message;
147 vc_cec_param2message(header, p0, p1, p2, p3, &message);
148
149 // translate to a cec_command
150 cec_command command;
151 cec_command::Format(command,
152 (cec_logical_address)message.initiator,
153 (cec_logical_address)message.follower,
154 (cec_opcode)CEC_CB_OPCODE(p0));
155
156 // copy parameters
157 for (uint8_t iPtr = 1; iPtr < message.length; iPtr++)
158 command.PushBack(message.payload[iPtr]);
159
160 // send to libCEC
161 m_callback->OnCommandReceived(command);
162 }
163 break;
164 case VC_CEC_TX:
165 {
166 // handle response to a command that was sent earlier
167 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));
168 }
169 break;
170 case VC_CEC_BUTTON_PRESSED:
171 {
172 // translate into a cec_command
173 cec_command command;
174 cec_command::Format(command, (cec_logical_address)CEC_CB_INITIATOR(p0), (cec_logical_address)CEC_CB_FOLLOWER(p0), CEC_OPCODE_USER_CONTROL_PRESSED);
175 command.parameters.PushBack((uint8_t)CEC_CB_OPERAND1(p0));
176
177 // send to libCEC
178 m_callback->OnCommandReceived(command);
179 }
180 break;
181 case VC_CEC_BUTTON_RELEASE:
182 {
183 // translate into a cec_command
184 cec_command command;
185 cec_command::Format(command, (cec_logical_address)CEC_CB_INITIATOR(p0), (cec_logical_address)CEC_CB_FOLLOWER(p0), CEC_OPCODE_USER_CONTROL_RELEASE);
186 command.parameters.PushBack((uint8_t)CEC_CB_OPERAND1(p0));
187
188 // send to libCEC
189 m_callback->OnCommandReceived(command);
190 }
191 break;
192 case VC_CEC_LOGICAL_ADDR:
193 {
194 CLockObject lock(m_mutex);
195 if (CEC_CB_RC(header) == VCHIQ_SUCCESS)
196 {
197 m_bLogicalAddressChanged = true;
198 m_logicalAddress = (cec_logical_address)(p0 & 0xF);
199 LIB_CEC->AddLog(CEC_LOG_DEBUG, "logical address changed to %s (%x)", LIB_CEC->ToString(m_logicalAddress), m_logicalAddress);
200 }
201 else
202 {
203 m_logicalAddress = CECDEVICE_BROADCAST;
204 LIB_CEC->AddLog(CEC_LOG_DEBUG, "failed to change the logical address, reset to %s (%x)", LIB_CEC->ToString(m_logicalAddress), m_logicalAddress);
205 }
206 m_logicalAddressCondition.Signal();
207 }
208 break;
7d27bafc
LOK
209 case VC_CEC_LOGICAL_ADDR_LOST:
210 {
211 // the logical address was taken by another device
212 cec_logical_address previousAddress = m_logicalAddress;
213 m_logicalAddress = CECDEVICE_UNKNOWN;
f60ee8b3
LOK
214
215 if (m_laLost && !m_laLost->IsRunning())
216 delete m_laLost;
217 m_laLost = new CRPiCECAdapterFindNewLogicalAddress(this, previousAddress);
218 if (m_laLost)
219 m_laLost->CreateThread();
7d27bafc
LOK
220 }
221 break;
29104708
LOK
222 case VC_CEC_TOPOLOGY:
223 case VC_CEC_REMOTE_PRESSED:
224 case VC_CEC_REMOTE_RELEASE:
225 break;
226 default:
227 LIB_CEC->AddLog(CEC_LOG_DEBUG, "ignoring unknown reason %x", reason);
228 break;
229 }
230}
231
232int CRPiCECAdapterCommunication::InitHostCEC(void)
233{
234 VCHIQ_INSTANCE_T vchiq_instance;
235 int iResult;
236
237 if ((iResult = vchiq_initialise(&vchiq_instance)) != VCHIQ_SUCCESS)
238 {
239 LIB_CEC->AddLog(CEC_LOG_ERROR, "%s - vchiq_initialise failed (%d)", __FUNCTION__, iResult);
240 CStdString strError;
241 strError.Format("%s - vchiq_initialise failed (%d)", __FUNCTION__, iResult);
242 m_strError = strError;
243 return iResult;
244 }
245 LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s - vchiq_initialise succeeded", __FUNCTION__);
246
247 if ((iResult = vchi_initialise(&m_vchi_instance)) != VCHIQ_SUCCESS)
248 {
249 LIB_CEC->AddLog(CEC_LOG_ERROR, "%s - vchi_initialise failed (%d)", __FUNCTION__, iResult);
250 CStdString strError;
251 strError.Format("%s - vchi_initialise failed (%d)", __FUNCTION__, iResult);
252 m_strError = strError;
253 return iResult;
254 }
255 LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s - vchi_initialise succeeded", __FUNCTION__);
256
257 vchiq_instance = (VCHIQ_INSTANCE_T)m_vchi_instance;
258
259 m_vchi_connection = vchi_create_connection(single_get_func_table(),
260 vchi_mphi_message_driver_func_table());
261
262 if ((iResult = vchi_connect(&m_vchi_connection, 1, m_vchi_instance)) != VCHIQ_SUCCESS)
263 {
264 LIB_CEC->AddLog(CEC_LOG_ERROR, "%s - vchi_connect failed (%d)", __FUNCTION__, iResult);
265 CStdString strError;
266 strError.Format("%s - vchi_connect failed (%d)", __FUNCTION__, iResult);
267 m_strError = strError;
268 return iResult;
269 }
270 LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s - vchi_connect succeeded", __FUNCTION__);
271
272 return VCHIQ_SUCCESS;
273}
274
275bool CRPiCECAdapterCommunication::Open(uint32_t iTimeoutMs /* = CEC_DEFAULT_CONNECT_TIMEOUT */, bool UNUSED(bSkipChecks) /* = false */, bool bStartListening)
276{
277 Close();
278
279 if (InitHostCEC() != VCHIQ_SUCCESS)
280 return false;
281
282 if (bStartListening)
283 {
284 // enable passive mode
285 vc_cec_set_passive(true);
286
287 // register the callback
288 vc_cec_register_callback(((CECSERVICE_CALLBACK_T)rpi_cec_callback), (void*)this);
289
290 // release previous LA
291 vc_cec_release_logical_address();
292 if (!m_logicalAddressCondition.Wait(m_mutex, m_bLogicalAddressChanged, iTimeoutMs))
293 {
294 LIB_CEC->AddLog(CEC_LOG_ERROR, "failed to release the previous LA");
295 return false;
296 }
297
298 // register LA "freeuse"
299 if (RegisterLogicalAddress(CECDEVICE_FREEUSE))
300 {
301 LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s - vc_cec initialised", __FUNCTION__);
302 CLockObject lock(m_mutex);
303 m_bInitialised = true;
304 }
305 else
306 LIB_CEC->AddLog(CEC_LOG_ERROR, "%s - vc_cec could not be initialised", __FUNCTION__);
307 }
308
309 return true;
310}
311
312uint16_t CRPiCECAdapterCommunication::GetPhysicalAddress(void)
313{
314 uint16_t iPA(CEC_INVALID_PHYSICAL_ADDRESS);
315 if (!IsInitialised())
316 return iPA;
317
318 if (vc_cec_get_physical_address(&iPA) == VCHIQ_SUCCESS)
319 LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s - physical address = %04x", __FUNCTION__, iPA);
320 else
321 {
322 LIB_CEC->AddLog(CEC_LOG_WARNING, "%s - failed to get the physical address", __FUNCTION__);
323 iPA = CEC_INVALID_PHYSICAL_ADDRESS;
324 }
325
326 return iPA;
327}
328
329void CRPiCECAdapterCommunication::Close(void)
330{
331 {
332 CLockObject lock(m_mutex);
333 if (m_bInitialised)
334 m_bInitialised = false;
335 else
336 return;
337 }
338 UnregisterLogicalAddress();
339
340 // disable passive mode
341 vc_cec_set_passive(false);
342
343 if (!g_bHostInited)
344 {
345 g_bHostInited = false;
346 bcm_host_deinit();
347 }
348}
349
350std::string CRPiCECAdapterCommunication::GetError(void) const
351{
352 std::string strError(m_strError);
353 return strError;
354}
355
356cec_adapter_message_state CRPiCECAdapterCommunication::Write(const cec_command &data, bool &UNUSED(bRetry), uint8_t UNUSED(iLineTimeout), bool bIsReply)
357{
358 // ensure that the source LA is registered
359 if (!RegisterLogicalAddress(data.initiator))
360 {
361 LIB_CEC->AddLog(CEC_LOG_DEBUG, "failed to register logical address %s (%X)", CCECTypeUtils::ToString(data.initiator), data.initiator);
362 return (data.initiator == data.destination) ? ADAPTER_MESSAGE_STATE_SENT_NOT_ACKED : ADAPTER_MESSAGE_STATE_ERROR;
363 }
364
365 if (!data.opcode_set && data.initiator == data.destination)
366 {
367 // registration of the logical address would have failed
368 return ADAPTER_MESSAGE_STATE_SENT_NOT_ACKED;
369 }
370
371 return m_queue->Write(data, bIsReply) ? ADAPTER_MESSAGE_STATE_SENT_ACKED : ADAPTER_MESSAGE_STATE_SENT_NOT_ACKED;
372}
373
374uint16_t CRPiCECAdapterCommunication::GetFirmwareVersion(void)
375{
376 return VC_CECSERVICE_VER;
377}
378
379cec_logical_address CRPiCECAdapterCommunication::GetLogicalAddress(void)
380{
381 {
382 CLockObject lock(m_mutex);
383 if (m_logicalAddress != CECDEVICE_UNKNOWN)
384 return m_logicalAddress;
385 }
386
387 CEC_AllDevices_T address;
388 return (vc_cec_get_logical_address(&address) == VCHIQ_SUCCESS) ?
389 (cec_logical_address)address : CECDEVICE_UNKNOWN;
390}
391
392bool CRPiCECAdapterCommunication::UnregisterLogicalAddress(void)
393{
394 CLockObject lock(m_mutex);
395 if (m_logicalAddress == CECDEVICE_UNKNOWN ||
396 m_logicalAddress == CECDEVICE_BROADCAST)
397 return true;
398
399 LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s - releasing previous logical address", __FUNCTION__);
400 m_bLogicalAddressChanged = false;
401
402 vc_cec_release_logical_address();
403
404 return m_logicalAddressCondition.Wait(m_mutex, m_bLogicalAddressChanged);
405}
406
407bool CRPiCECAdapterCommunication::RegisterLogicalAddress(const cec_logical_address address)
408{
409 {
410 CLockObject lock(m_mutex);
411 if (m_logicalAddress == address)
412 return true;
413 }
414
415 if (!UnregisterLogicalAddress())
416 return false;
417
418 LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s - registering address %x", __FUNCTION__, address);
419
420 CLockObject lock(m_mutex);
421 m_bLogicalAddressChanged = false;
422 vc_cec_poll_address((CEC_AllDevices_T)address);
423
424 // register the new LA
425 int iRetval = vc_cec_set_logical_address((CEC_AllDevices_T)address, (CEC_DEVICE_TYPE_T)CCECTypeUtils::GetType(address), CEC_VENDOR_ID_BROADCOM);
426 if (iRetval != VCHIQ_SUCCESS)
427 {
428 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);
429 return false;
430 }
431
432 return m_logicalAddressCondition.Wait(m_mutex, m_bLogicalAddressChanged);
433}
434
435cec_logical_addresses CRPiCECAdapterCommunication::GetLogicalAddresses(void)
436{
437 cec_logical_addresses addresses; addresses.Clear();
438 cec_logical_address current = GetLogicalAddress();
439 if (current != CECDEVICE_UNKNOWN)
440 addresses.Set(current);
441
442 return addresses;
443}
444
445bool CRPiCECAdapterCommunication::SetLogicalAddresses(const cec_logical_addresses &addresses)
446{
447 // the current generation RPi only supports 1 LA, so just ensure that the primary address is registered
448 return SupportsSourceLogicalAddress(addresses.primary) &&
449 RegisterLogicalAddress(addresses.primary);
450}
451
452void CRPiCECAdapterCommunication::InitHost(void)
453{
454 if (!g_bHostInited)
455 {
456 g_bHostInited = true;
457 bcm_host_init();
458 }
459}
460
461#endif