cec: added a vendor command for panasonic that will enable routing of some more butto...
[deb_libcec.git] / src / lib / implementations / VLCommandHandler.cpp
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 "VLCommandHandler.h"
34 #include "../devices/CECBusDevice.h"
35 #include "../CECProcessor.h"
36 #include "../LibCEC.h"
37 #include "../CECClient.h"
38
39 #define VL_POWER_CHANGE 0x20
40 #define VL_POWERED_UP 0x00
41 #define VL_POWERED_DOWN 0x01
42
43 using namespace CEC;
44 using namespace PLATFORM;
45
46 #define LIB_CEC m_busDevice->GetProcessor()->GetLib()
47 #define ToString(p) LIB_CEC->ToString(p)
48
49 CVLCommandHandler::CVLCommandHandler(CCECBusDevice *busDevice) :
50 CCECCommandHandler(busDevice),
51 m_bActiveSourcePending(false),
52 m_bPowerUpEventReceived(false)
53 {
54 m_vendorId = CEC_VENDOR_PANASONIC;
55 }
56
57 bool CVLCommandHandler::InitHandler(void)
58 {
59 CCECBusDevice *primary = m_processor->GetPrimaryDevice();
60 if (primary && primary->GetLogicalAddress() != CECDEVICE_UNREGISTERED)
61 {
62 /* use the VL commandhandler for the primary device that is handled by libCEC */
63 if (m_busDevice->GetLogicalAddress() == CECDEVICE_TV)
64 {
65 if (primary && m_busDevice->GetLogicalAddress() != primary->GetLogicalAddress())
66 {
67 primary->SetVendorId(CEC_VENDOR_PANASONIC);
68 primary->ReplaceHandler(false);
69 }
70 }
71
72 if (primary->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE)
73 return m_processor->GetPrimaryClient()->ChangeDeviceType(CEC_DEVICE_TYPE_RECORDING_DEVICE, CEC_DEVICE_TYPE_PLAYBACK_DEVICE);
74 }
75
76 return CCECCommandHandler::InitHandler();
77 }
78
79 int CVLCommandHandler::HandleDeviceVendorCommandWithId(const cec_command &command)
80 {
81 if (command.parameters[0] != 0x00 ||
82 command.parameters[1] != 0x80 ||
83 command.parameters[2] != 0x45)
84 return CEC_ABORT_REASON_INVALID_OPERAND;
85
86 if (command.initiator == CECDEVICE_TV &&
87 command.destination == CECDEVICE_BROADCAST &&
88 command.parameters.At(3) == VL_POWER_CHANGE)
89 {
90 if (command.parameters.At(4) == VL_POWERED_UP)
91 {
92 LIB_CEC->AddLog(CEC_LOG_DEBUG, "TV powered up");
93 {
94 CLockObject lock(m_mutex);
95 m_bPowerUpEventReceived = true;
96 }
97 m_processor->TransmitPendingActiveSourceCommands();
98 }
99 else if (command.parameters.At(4) == VL_POWERED_DOWN)
100 LIB_CEC->AddLog(CEC_LOG_DEBUG, "TV powered down");
101 else if (command.parameters.At(4) == VL_POWERED_DOWN)
102 LIB_CEC->AddLog(CEC_LOG_DEBUG, "unknown vendor command");
103
104 return COMMAND_HANDLED;
105 }
106
107 return CCECCommandHandler::HandleDeviceVendorCommandWithId(command);
108 }
109
110 bool CVLCommandHandler::TransmitActiveSource(const cec_logical_address iInitiator, uint16_t iPhysicalAddress)
111 {
112 bool bPowerUpEventReceived(false);
113
114 CCECBusDevice *tv = m_processor->GetDevice(CECDEVICE_TV);
115 if (tv && tv->GetCurrentVendorId() == CEC_VENDOR_PANASONIC)
116 {
117 CVLCommandHandler *handler = static_cast<CVLCommandHandler *>(tv->GetHandler());
118 bPowerUpEventReceived = handler ? handler->PowerUpEventReceived() : false;
119 tv->MarkHandlerReady();
120 }
121
122 if (!bPowerUpEventReceived)
123 {
124 CLockObject lock(m_mutex);
125 // wait until we received the event
126 m_bActiveSourcePending = true;
127 return true;
128 }
129 else
130 {
131 // transmit standard active source message
132 return CCECCommandHandler::TransmitActiveSource(iInitiator, iPhysicalAddress);
133 }
134 }
135
136 bool CVLCommandHandler::TransmitPendingActiveSourceCommands(void)
137 {
138 bool bTransmitCommand(false);
139 {
140 CLockObject lock(m_mutex);
141 bTransmitCommand = m_bActiveSourcePending;
142 m_bActiveSourcePending = false;
143 }
144
145 if (bTransmitCommand)
146 {
147 LIB_CEC->AddLog(CEC_LOG_DEBUG, "transmitting delayed activate source command");
148 return CCECCommandHandler::TransmitActiveSource(m_busDevice->GetLogicalAddress(), m_busDevice->GetCurrentPhysicalAddress()) &&
149 TransmitMenuState(m_busDevice->GetLogicalAddress(), CECDEVICE_TV, CEC_MENU_STATE_ACTIVATED);
150 }
151 return true;
152 }
153
154 bool CVLCommandHandler::PowerUpEventReceived(void)
155 {
156 {
157 CLockObject lock(m_mutex);
158 if (m_bPowerUpEventReceived)
159 return true;
160 }
161
162 cec_logical_address sourceLA = m_busDevice->GetLogicalAddress();
163 if (sourceLA == CECDEVICE_TV)
164 sourceLA = m_processor->GetPrimaryDevice()->GetLogicalAddress();
165
166 cec_power_status powerStatus = m_busDevice->GetPowerStatus(sourceLA);
167
168 CLockObject lock(m_mutex);
169 m_bPowerUpEventReceived = (powerStatus == CEC_POWER_STATUS_ON);
170 return m_bPowerUpEventReceived;
171 }
172
173 int CVLCommandHandler::HandleVendorCommand(const cec_command &command)
174 {
175 // some vendor command voodoo that will enable more buttons on the remote
176 if (command.parameters.size == 3 &&
177 command.parameters[0] == 0x10 &&
178 command.parameters[1] == 0x01 &&
179 command.parameters[2] == 0x05)
180 {
181 cec_command response;
182 cec_command::Format(response, command.destination, command.initiator, CEC_OPCODE_VENDOR_COMMAND);
183 uint8_t iResponseData[] = {0x10, 0x02, 0xFF, 0xFF, 0x00, 0x05, 0x05, 0x45, 0x55, 0x5c, 0x58, 0x32};
184 response.PushArray(12, iResponseData);
185
186 Transmit(response, true);
187
188 return COMMAND_HANDLED;
189 }
190
191 return CEC_ABORT_REASON_INVALID_OPERAND;
192 }
193