Commit | Line | Data |
---|---|---|
29104708 LOK |
1 | /* |
2 | * This file is part of the libCEC(R) library. | |
3 | * | |
16f47961 | 4 | * libCEC(R) is Copyright (C) 2011-2013 Pulse-Eight Limited. All rights reserved. |
29104708 LOK |
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 | { | |
f9c4a2de MK |
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)) | |
29104708 LOK |
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 | ||
91 | bool 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; | |
29104708 LOK |
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) | |
6d50a26b | 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); |
29104708 LOK |
125 | } |
126 | ||
f9c4a2de MK |
127 | uint32_t CRPiCECAdapterMessageQueueEntry::Result() const |
128 | { | |
129 | return m_retval; | |
130 | } | |
131 | ||
132 | cec_adapter_message_state CRPiCECAdapterMessageQueue::Write(const cec_command &command, bool &bRetry, uint32_t iLineTimeout, bool bIsReply, VC_CEC_ERROR_T &vcReply) | |
29104708 LOK |
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 | ||
b509ba1a | 159 | #ifdef CEC_DEBUGGING |
29104708 LOK |
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()); | |
b509ba1a | 165 | #endif |
29104708 LOK |
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 | ||
b509ba1a | 182 | #ifdef CEC_DEBUGGING |
29104708 LOK |
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()); | |
b509ba1a | 188 | #endif |
29104708 | 189 | |
f9c4a2de | 190 | int iReturn = vc_cec_send_message((uint32_t)command.destination, command.opcode_set ? (uint8_t*)&payload : NULL, iLength, bIsReply); |
29104708 LOK |
191 | #endif |
192 | ||
f9c4a2de | 193 | bRetry = false; |
29104708 LOK |
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); | |
f9c4a2de | 198 | return ADAPTER_MESSAGE_STATE_ERROR; |
29104708 LOK |
199 | } |
200 | ||
f9c4a2de | 201 | cec_adapter_message_state bReturn(ADAPTER_MESSAGE_STATE_ERROR); |
29104708 LOK |
202 | if (entry) |
203 | { | |
f9c4a2de | 204 | if (entry->Wait(iLineTimeout)) |
29104708 | 205 | { |
f9c4a2de MK |
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; | |
29104708 | 214 | } |
f9c4a2de MK |
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(); | |
29104708 LOK |
227 | |
228 | CLockObject lock(m_mutex); | |
229 | m_messages.erase(iEntryId); | |
230 | delete entry; | |
231 | } | |
232 | ||
233 | return bReturn; | |
234 | } | |
235 | ||
236 | #endif | |
237 |