| 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 | |
| 46 | extern "C" { |
| 47 | #include <interface/vmcs_host/vc_cecservice.h> |
| 48 | #include <interface/vchiq_arm/vchiq.h> |
| 49 | } |
| 50 | |
| 51 | using namespace std; |
| 52 | using namespace CEC; |
| 53 | using namespace PLATFORM; |
| 54 | |
| 55 | #define LIB_CEC m_com->m_callback->GetLib() |
| 56 | |
| 57 | CRPiCECAdapterMessageQueueEntry::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 | |
| 66 | void CRPiCECAdapterMessageQueueEntry::Broadcast(void) |
| 67 | { |
| 68 | CLockObject lock(m_mutex); |
| 69 | m_condition.Broadcast(); |
| 70 | } |
| 71 | |
| 72 | bool 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 | |
| 88 | bool 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 | |
| 103 | bool CRPiCECAdapterMessageQueueEntry::IsWaiting(void) |
| 104 | { |
| 105 | CLockObject lock(m_mutex); |
| 106 | return m_bWaiting; |
| 107 | } |
| 108 | |
| 109 | void CRPiCECAdapterMessageQueue::Clear(void) |
| 110 | { |
| 111 | CLockObject lock(m_mutex); |
| 112 | m_messages.clear(); |
| 113 | } |
| 114 | |
| 115 | void 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 | |
| 127 | bool 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 | #ifdef CEC_DEBUGGING |
| 155 | CStdString strDump; |
| 156 | strDump.Format("len = %d, payload = %X%X", message.length, (int)message.initiator, (int)message.follower); |
| 157 | for (uint8_t iPtr = 0; iPtr < message.length - 1; iPtr++) |
| 158 | strDump.AppendFormat(":%02X", message.payload[iPtr]); |
| 159 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "sending data: %s", strDump.c_str()); |
| 160 | #endif |
| 161 | |
| 162 | int iReturn = vc_cec_send_message2(&message); |
| 163 | #else |
| 164 | uint8_t payload[32]; |
| 165 | uint32_t iLength(0); |
| 166 | |
| 167 | if (command.opcode_set) |
| 168 | { |
| 169 | iLength += 1; |
| 170 | payload[0] = command.opcode; |
| 171 | |
| 172 | iLength += command.parameters.size; |
| 173 | for (uint8_t iPtr = 0; iPtr < command.parameters.size; iPtr++) |
| 174 | payload[iPtr + 1] = command.parameters.At(iPtr); |
| 175 | } |
| 176 | |
| 177 | #ifdef CEC_DEBUGGING |
| 178 | CStdString strDump; |
| 179 | strDump.Format("len = %d, payload = %X%X", iLength, (int)command.initiator, (int)command.destination); |
| 180 | for (uint8_t iPtr = 0; iPtr < iLength; iPtr++) |
| 181 | strDump.AppendFormat(":%02X", payload[iPtr]); |
| 182 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "sending data: %s", strDump.c_str()); |
| 183 | #endif |
| 184 | |
| 185 | int iReturn = vc_cec_send_message((uint32_t)command.destination, (uint8_t*)&payload, iLength, bIsReply); |
| 186 | #endif |
| 187 | |
| 188 | if (iReturn != VCHIQ_SUCCESS) |
| 189 | { |
| 190 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "sending command '%s' failed (%d)", command.opcode_set ? CCECTypeUtils::ToString(command.opcode) : "POLL", iReturn); |
| 191 | delete (entry); |
| 192 | return false; |
| 193 | } |
| 194 | |
| 195 | bool bReturn(true); |
| 196 | if (entry) |
| 197 | { |
| 198 | if (!entry->Wait(CEC_DEFAULT_TRANSMIT_WAIT)) |
| 199 | { |
| 200 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "command '%s' was not acked by the controller", command.opcode_set ? CCECTypeUtils::ToString(command.opcode) : "POLL"); |
| 201 | bReturn = false; |
| 202 | } |
| 203 | |
| 204 | CLockObject lock(m_mutex); |
| 205 | m_messages.erase(iEntryId); |
| 206 | delete entry; |
| 207 | } |
| 208 | |
| 209 | return bReturn; |
| 210 | } |
| 211 | |
| 212 | #endif |
| 213 | |