Imported Upstream version 2.2.0
[deb_libcec.git] / src / lib / adapter / RPi / RPiCECAdapterMessageQueue.cpp
CommitLineData
cbbe90dd
JB
1/*
2 * This file is part of the libCEC(R) library.
3 *
4 * libCEC(R) is Copyright (C) 2011-2013 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 (!m_command.opcode_set &&
79 m_command.destination == destination))
80 {
81 CLockObject lock(m_mutex);
82 m_retval = response;
83 m_bSucceeded = true;
84 m_condition.Signal();
85 return true;
86 }
87
88 return false;
89}
90
91bool CRPiCECAdapterMessageQueueEntry::Wait(uint32_t iTimeout)
92{
93 bool bReturn(false);
94 /* wait until we receive a signal when the tranmission succeeded */
95 {
96 CLockObject lock(m_mutex);
97 bReturn = m_bSucceeded ? true : m_condition.Wait(m_mutex, m_bSucceeded, iTimeout);
98 m_bWaiting = false;
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: opcode=%x initiator=%x destination=%x response=%x", (int)opcode, (int)initiator, (int)destination, response);
125}
126
127uint32_t CRPiCECAdapterMessageQueueEntry::Result() const
128{
129 return m_retval;
130}
131
132cec_adapter_message_state CRPiCECAdapterMessageQueue::Write(const cec_command &command, bool &bRetry, uint32_t iLineTimeout, bool bIsReply, VC_CEC_ERROR_T &vcReply)
133{
134 CRPiCECAdapterMessageQueueEntry *entry = new CRPiCECAdapterMessageQueueEntry(this, command);
135 uint64_t iEntryId(0);
136 /* add to the wait for ack queue */
137 {
138 CLockObject lock(m_mutex);
139 iEntryId = m_iNextMessage++;
140 m_messages.insert(make_pair(iEntryId, entry));
141 }
142
143#if defined(RPI_USE_SEND_MESSAGE2)
144 VC_CEC_MESSAGE_T message;
145 message.initiator = (CEC_AllDevices_T)command.initiator;
146 message.follower = (CEC_AllDevices_T)command.destination;
147 message.length = 1;
148
149 if (command.opcode_set)
150 {
151 message.length += 1;
152 message.payload[0] = command.opcode;
153
154 message.length += command.parameters.size;
155 for (uint8_t iPtr = 0; iPtr < command.parameters.size; iPtr++)
156 message.payload[iPtr + 1] = command.parameters.At(iPtr);
157 }
158
159#ifdef CEC_DEBUGGING
160 CStdString strDump;
161 strDump.Format("len = %d, payload = %X%X", message.length, (int)message.initiator, (int)message.follower);
162 for (uint8_t iPtr = 0; iPtr < message.length - 1; iPtr++)
163 strDump.AppendFormat(":%02X", message.payload[iPtr]);
164 LIB_CEC->AddLog(CEC_LOG_DEBUG, "sending data: %s", strDump.c_str());
165#endif
166
167 int iReturn = vc_cec_send_message2(&message);
168#else
169 uint8_t payload[32];
170 uint32_t iLength(0);
171
172 if (command.opcode_set)
173 {
174 iLength += 1;
175 payload[0] = command.opcode;
176
177 iLength += command.parameters.size;
178 for (uint8_t iPtr = 0; iPtr < command.parameters.size; iPtr++)
179 payload[iPtr + 1] = command.parameters.At(iPtr);
180 }
181
182#ifdef CEC_DEBUGGING
183 CStdString strDump;
184 strDump.Format("len = %d, payload = %X%X", iLength, (int)command.initiator, (int)command.destination);
185 for (uint8_t iPtr = 0; iPtr < iLength; iPtr++)
186 strDump.AppendFormat(":%02X", payload[iPtr]);
187 LIB_CEC->AddLog(CEC_LOG_DEBUG, "sending data: %s", strDump.c_str());
188#endif
189
190 int iReturn = vc_cec_send_message((uint32_t)command.destination, command.opcode_set ? (uint8_t*)&payload : NULL, iLength, bIsReply);
191#endif
192
193 bRetry = false;
194 if (iReturn != VCHIQ_SUCCESS)
195 {
196 LIB_CEC->AddLog(CEC_LOG_DEBUG, "sending command '%s' failed (%d)", command.opcode_set ? CCECTypeUtils::ToString(command.opcode) : "POLL", iReturn);
197 delete (entry);
198 return ADAPTER_MESSAGE_STATE_ERROR;
199 }
200
201 cec_adapter_message_state bReturn(ADAPTER_MESSAGE_STATE_ERROR);
202 if (entry)
203 {
204 if (entry->Wait(iLineTimeout))
205 {
206 int status = entry->Result();
207
208 if (status == VC_CEC_ERROR_NO_ACK)
209 bReturn = ADAPTER_MESSAGE_STATE_SENT_NOT_ACKED;
210 else if (status == VC_CEC_SUCCESS)
211 bReturn = ADAPTER_MESSAGE_STATE_SENT_ACKED;
212 else
213 bReturn = ADAPTER_MESSAGE_STATE_SENT;
214 }
215 else
216 {
217 if (command.opcode_set)
218 {
219 bRetry = true;
220 LIB_CEC->AddLog(CEC_LOG_DEBUG, "command '%s' timeout", command.opcode_set ? CCECTypeUtils::ToString(command.opcode) : "POLL");
221 sleep(CEC_DEFAULT_TRANSMIT_RETRY_WAIT);
222 }
223 bReturn = ADAPTER_MESSAGE_STATE_WAITING_TO_BE_SENT;
224 }
225
226 vcReply = (VC_CEC_ERROR_T)entry->Result();
227
228 CLockObject lock(m_mutex);
229 m_messages.erase(iEntryId);
230 delete entry;
231 }
232
233 return bReturn;
234}
235
236#endif
237