cec: moved CECBusDevice.cpp to devices/CECBusDevice.cpp
[deb_libcec.git] / src / lib / implementations / CECCommandHandler.cpp
CommitLineData
e9de9629
LOK
1/*
2 * This file is part of the libCEC(R) library.
3 *
4 * libCEC(R) is Copyright (C) 2011 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 "CECCommandHandler.h"
eafa9d46 34#include "../devices/CECBusDevice.h"
387b6f6f 35#include "../CECProcessor.h"
e9de9629
LOK
36
37using namespace CEC;
38
39CCECCommandHandler::CCECCommandHandler(CCECBusDevice *busDevice)
40{
41 m_busDevice = busDevice;
42}
43
44bool CCECCommandHandler::HandleCommand(const cec_command &command)
45{
46 bool bHandled(true);
47
48 if (command.destination == m_busDevice->GetMyLogicalAddress())
49 {
50 switch(command.opcode)
51 {
52 case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS:
53 HandleGivePhysicalAddress(command);
54 break;
55 case CEC_OPCODE_GIVE_OSD_NAME:
56 HandleGiveOSDName(command);
57 break;
58 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID:
59 HandleGiveDeviceVendorId(command);
60 break;
61 case CEC_OPCODE_DEVICE_VENDOR_ID:
62 HandleDeviceVendorId(command);
63 break;
64 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID:
65 HandleDeviceVendorCommandWithId(command);
66 break;
67 case CEC_OPCODE_GIVE_DECK_STATUS:
68 HandleGiveDeckStatus(command);
69 break;
70 case CEC_OPCODE_MENU_REQUEST:
71 HandleMenuRequest(command);
72 break;
73 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS:
74 HandleGiveDevicePowerStatus(command);
75 break;
76 case CEC_OPCODE_GET_CEC_VERSION:
77 HandleGetCecVersion(command);
78 break;
79 case CEC_OPCODE_USER_CONTROL_PRESSED:
80 HandleUserControlPressed(command);
81 break;
82 case CEC_OPCODE_USER_CONTROL_RELEASE:
83 HandleUserControlRelease(command);
84 break;
85 default:
86 UnhandledCommand(command);
87 bHandled = false;
88 break;
89 }
90 }
91 else if (command.destination == CECDEVICE_BROADCAST)
92 {
93 CStdString strLog;
94 switch (command.opcode)
95 {
96 case CEC_OPCODE_REQUEST_ACTIVE_SOURCE:
97 HandleRequestActiveSource(command);
98 break;
99 case CEC_OPCODE_SET_STREAM_PATH:
100 HandleSetStreamPath(command);
101 break;
102 case CEC_OPCODE_ROUTING_CHANGE:
103 HandleRoutingChange(command);
104 break;
105 case CEC_OPCODE_DEVICE_VENDOR_ID:
106 HandleDeviceVendorId(command);
107 break;
108 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID:
109 HandleDeviceVendorCommandWithId(command);
110 break;
111 default:
112 UnhandledCommand(command);
113 bHandled = false;
114 break;
115 }
116 }
117 else
118 {
119 CStdString strLog;
120 strLog.Format("ignoring frame: destination: %u != %u", command.destination, (uint8_t)m_busDevice->GetMyLogicalAddress());
121 m_busDevice->AddLog(CEC_LOG_DEBUG, strLog.c_str());
122 bHandled = false;
123 }
124
125 return bHandled;
126}
127
128bool CCECCommandHandler::HandleDeviceVendorCommandWithId(const cec_command &command)
129{
130 m_busDevice->GetProcessor()->ParseVendorId(command.initiator, command.parameters);
131 return true;
132}
133
134bool CCECCommandHandler::HandleDeviceVendorId(const cec_command &command)
135{
136 m_busDevice->GetProcessor()->ParseVendorId(command.initiator, command.parameters);
137 return true;
138}
139
140bool CCECCommandHandler::HandleGetCecVersion(const cec_command &command)
141{
142 m_busDevice->GetProcessor()->ReportCECVersion(command.initiator);
143 return true;
144}
145
146bool CCECCommandHandler::HandleGiveDeckStatus(const cec_command &command)
147{
148 // need to support opcodes play and deck control before doing anything with this
149 m_busDevice->GetProcessor()->TransmitAbort(command.initiator, CEC_OPCODE_GIVE_DECK_STATUS);
150 return true;
151}
152
153bool CCECCommandHandler::HandleGiveDevicePowerStatus(const cec_command &command)
154{
155 m_busDevice->GetProcessor()->ReportPowerState(command.initiator);
156 return true;
157}
158
159bool CCECCommandHandler::HandleGiveDeviceVendorId(const cec_command &command)
160{
161 m_busDevice->GetProcessor()->ReportVendorID(command.initiator);
162 return true;
163}
164
165bool CCECCommandHandler::HandleGiveOSDName(const cec_command &command)
166{
167 m_busDevice->GetProcessor()->ReportOSDName(command.initiator);
168 return true;
169}
170
171bool CCECCommandHandler::HandleGivePhysicalAddress(const cec_command &command)
172{
173 m_busDevice->GetProcessor()->ReportPhysicalAddress();
174 return true;
175}
176
177bool CCECCommandHandler::HandleMenuRequest(const cec_command &command)
178{
179 if (command.parameters[0] == CEC_MENU_REQUEST_TYPE_QUERY)
180 m_busDevice->GetProcessor()->ReportMenuState(command.initiator);
181 return true;
182}
183
184bool CCECCommandHandler::HandleRequestActiveSource(const cec_command &command)
185{
186 CStdString strLog;
187 strLog.Format(">> %i requests active source", (uint8_t) command.initiator);
188 m_busDevice->AddLog(CEC_LOG_DEBUG, strLog.c_str());
189 m_busDevice->GetProcessor()->BroadcastActiveSource();
190 return true;
191}
192
193bool CCECCommandHandler::HandleRoutingChange(const cec_command &command)
194{
195 if (command.parameters.size == 4)
196 {
197 uint16_t iOldAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
198 uint16_t iNewAddress = ((uint16_t)command.parameters[2] << 8) | ((uint16_t)command.parameters[3]);
e9de9629 199
4e4be5cf 200 m_busDevice->SetPhysicalAddress(iNewAddress, iOldAddress);
e9de9629
LOK
201 }
202 return true;
203}
204
205bool CCECCommandHandler::HandleSetStreamPath(const cec_command &command)
206{
207 if (command.parameters.size >= 2)
208 {
209 int streamaddr = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
210 CStdString strLog;
211 strLog.Format(">> %i requests stream path from physical address %04x", command.initiator, streamaddr);
212 m_busDevice->AddLog(CEC_LOG_DEBUG, strLog.c_str());
213 if (streamaddr == m_busDevice->GetMyPhysicalAddress())
214 m_busDevice->GetProcessor()->BroadcastActiveSource();
215 }
216 return true;
217}
218
219bool CCECCommandHandler::HandleUserControlPressed(const cec_command &command)
220{
221 if (command.parameters.size > 0)
222 {
223 m_busDevice->GetProcessor()->AddKey();
224
225 if (command.parameters[0] <= CEC_USER_CONTROL_CODE_MAX)
226 {
227 CStdString strLog;
228 strLog.Format("key pressed: %1x", command.parameters[0]);
229 m_busDevice->AddLog(CEC_LOG_DEBUG, strLog.c_str());
230
231 m_busDevice->GetProcessor()->SetCurrentButton((cec_user_control_code) command.parameters[0]);
232 }
233 }
234 return true;
235}
236
237bool CCECCommandHandler::HandleUserControlRelease(const cec_command &command)
238{
239 m_busDevice->GetProcessor()->AddKey();
240 return true;
241}
242
243void CCECCommandHandler::UnhandledCommand(const cec_command &command)
244{
245 m_busDevice->GetProcessor()->AddCommand(command);;
246}