added GetAdapterProductId()+GetAdapterVendorId()/cec_get_adapter_product_id()+cec_get...
[deb_libcec.git] / src / lib / adapter / RPi / RPiCECAdapterMessageQueue.cpp
... / ...
CommitLineData
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 "RPiCECAdapterMessageQueue.h"
37
38// use vc_cec_send_message2() if defined and vc_cec_send_message() if not
39//#define RPI_USE_SEND_MESSAGE2
40
41#include "RPiCECAdapterCommunication.h"
42#include "lib/LibCEC.h"
43#include "lib/CECTypeUtils.h"
44#include "lib/platform/util/StdString.h"
45
46extern "C" {
47#include <interface/vmcs_host/vc_cecservice.h>
48#include <interface/vchiq_arm/vchiq.h>
49}
50
51using namespace std;
52using namespace CEC;
53using namespace PLATFORM;
54
55#define LIB_CEC m_com->m_callback->GetLib()
56
57CRPiCECAdapterMessageQueueEntry::CRPiCECAdapterMessageQueueEntry(CRPiCECAdapterMessageQueue *queue, const cec_command &command) :
58 m_queue(queue),
59 m_command(command),
60 m_retval(VC_CEC_ERROR_NO_ACK),
61 m_bSucceeded(false)
62{
63
64}
65
66void CRPiCECAdapterMessageQueueEntry::Broadcast(void)
67{
68 CLockObject lock(m_mutex);
69 m_condition.Broadcast();
70}
71
72bool CRPiCECAdapterMessageQueueEntry::MessageReceived(cec_opcode opcode, cec_logical_address initiator, cec_logical_address destination, uint32_t response)
73{
74 if ((!m_command.opcode_set || m_command.opcode == opcode) &&
75 m_command.initiator == initiator &&
76 m_command.destination == destination)
77 {
78 CLockObject lock(m_mutex);
79 m_retval = response;
80 m_bSucceeded = true;
81 m_condition.Signal();
82 return true;
83 }
84
85 return false;
86}
87
88bool CRPiCECAdapterMessageQueueEntry::Wait(uint32_t iTimeout)
89{
90 bool bReturn(false);
91 /* wait until we receive a signal when the tranmission succeeded */
92 {
93 CLockObject lock(m_mutex);
94 bReturn = m_bSucceeded ? true : m_condition.Wait(m_mutex, m_bSucceeded, iTimeout);
95 m_bWaiting = false;
96
97 if (bReturn)
98 bReturn = m_retval == VCHIQ_SUCCESS;
99 }
100 return bReturn;
101}
102
103bool CRPiCECAdapterMessageQueueEntry::IsWaiting(void)
104{
105 CLockObject lock(m_mutex);
106 return m_bWaiting;
107}
108
109void CRPiCECAdapterMessageQueue::Clear(void)
110{
111 CLockObject lock(m_mutex);
112 m_messages.clear();
113}
114
115void CRPiCECAdapterMessageQueue::MessageReceived(cec_opcode opcode, cec_logical_address initiator, cec_logical_address destination, uint32_t response)
116{
117 bool bHandled(false);
118 CLockObject lock(m_mutex);
119 /* send the received message to each entry in the queue until it is handled */
120 for (map<uint64_t, CRPiCECAdapterMessageQueueEntry *>::iterator it = m_messages.begin(); !bHandled && it != m_messages.end(); it++)
121 bHandled = it->second->MessageReceived(opcode, initiator, destination, response);
122
123 if (!bHandled)
124 LIB_CEC->AddLog(CEC_LOG_WARNING, "unhandled response received");
125}
126
127bool CRPiCECAdapterMessageQueue::Write(const cec_command &command, bool bIsReply)
128{
129 CRPiCECAdapterMessageQueueEntry *entry = new CRPiCECAdapterMessageQueueEntry(this, command);
130 uint64_t iEntryId(0);
131 /* add to the wait for ack queue */
132 {
133 CLockObject lock(m_mutex);
134 iEntryId = m_iNextMessage++;
135 m_messages.insert(make_pair(iEntryId, entry));
136 }
137
138#if defined(RPI_USE_SEND_MESSAGE2)
139 VC_CEC_MESSAGE_T message;
140 message.initiator = (CEC_AllDevices_T)command.initiator;
141 message.follower = (CEC_AllDevices_T)command.destination;
142 message.length = 1;
143
144 if (command.opcode_set)
145 {
146 message.length += 1;
147 message.payload[0] = command.opcode;
148
149 message.length += command.parameters.size;
150 for (uint8_t iPtr = 0; iPtr < command.parameters.size; iPtr++)
151 message.payload[iPtr + 1] = command.parameters.At(iPtr);
152 }
153
154 CStdString strDump;
155 strDump.Format("len = %d, payload = %X%X", message.length, (int)message.initiator, (int)message.follower);
156 for (uint8_t iPtr = 0; iPtr < message.length - 1; iPtr++)
157 strDump.AppendFormat(":%02X", message.payload[iPtr]);
158 LIB_CEC->AddLog(CEC_LOG_DEBUG, "sending data: %s", strDump.c_str());
159
160 int iReturn = vc_cec_send_message2(&message);
161#else
162 uint8_t payload[32];
163 uint32_t iLength(0);
164
165 if (command.opcode_set)
166 {
167 iLength += 1;
168 payload[0] = command.opcode;
169
170 iLength += command.parameters.size;
171 for (uint8_t iPtr = 0; iPtr < command.parameters.size; iPtr++)
172 payload[iPtr + 1] = command.parameters.At(iPtr);
173 }
174
175 CStdString strDump;
176 strDump.Format("len = %d, payload = %X%X", iLength, (int)command.initiator, (int)command.destination);
177 for (uint8_t iPtr = 0; iPtr < iLength; iPtr++)
178 strDump.AppendFormat(":%02X", payload[iPtr]);
179 LIB_CEC->AddLog(CEC_LOG_DEBUG, "sending data: %s", strDump.c_str());
180
181 int iReturn = vc_cec_send_message((uint32_t)command.destination, (uint8_t*)&payload, iLength, bIsReply);
182#endif
183
184 if (iReturn != VCHIQ_SUCCESS)
185 {
186 LIB_CEC->AddLog(CEC_LOG_DEBUG, "sending command '%s' failed (%d)", command.opcode_set ? CCECTypeUtils::ToString(command.opcode) : "POLL", iReturn);
187 delete (entry);
188 return false;
189 }
190
191 bool bReturn(true);
192 if (entry)
193 {
194 if (!entry->Wait(CEC_DEFAULT_TRANSMIT_WAIT))
195 {
196 LIB_CEC->AddLog(CEC_LOG_DEBUG, "command '%s' was not acked by the controller", command.opcode_set ? CCECTypeUtils::ToString(command.opcode) : "POLL");
197 bReturn = false;
198 }
199
200 CLockObject lock(m_mutex);
201 m_messages.erase(iEntryId);
202 delete entry;
203 }
204
205 return bReturn;
206}
207
208#endif
209