cec: set the physical address in CCECBusDevice
[deb_libcec.git] / src / lib / CECBusDevice.cpp
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 "CECBusDevice.h"
34 #include "CECProcessor.h"
35 #include "implementations/ANCommandHandler.h"
36 #include "implementations/CECCommandHandler.h"
37 #include "implementations/SLCommandHandler.h"
38 #include "platform/timeutils.h"
39
40 using namespace CEC;
41
42 CCECBusDevice::CCECBusDevice(CCECProcessor *processor, cec_logical_address iLogicalAddress, uint16_t iPhysicalAddress) :
43 m_iPhysicalAddress(iPhysicalAddress),
44 m_iLogicalAddress(iLogicalAddress),
45 m_processor(processor),
46 m_iVendorId(0),
47 m_iVendorClass(CEC_VENDOR_UNKNOWN),
48 m_iLastActive(0)
49 {
50 m_handler = new CCECCommandHandler(this);
51 }
52
53 CCECBusDevice::~CCECBusDevice(void)
54 {
55 delete m_handler;
56 }
57
58 cec_logical_address CCECBusDevice::GetMyLogicalAddress(void) const
59 {
60 return m_processor->GetLogicalAddress();
61 }
62
63 uint16_t CCECBusDevice::GetMyPhysicalAddress(void) const
64 {
65 return m_processor->GetPhysicalAddress();
66 }
67
68 void CCECBusDevice::AddLog(cec_log_level level, const CStdString &strMessage)
69 {
70 m_processor->AddLog(level, strMessage);
71 }
72
73 void CCECBusDevice::SetVendorId(uint64_t iVendorId, uint8_t iVendorClass /* = 0 */)
74 {
75 m_iVendorId = iVendorId;
76 m_iVendorClass = iVendorClass;
77
78 switch (iVendorId)
79 {
80 case CEC_VENDOR_SAMSUNG:
81 if (m_handler->GetVendorId() != CEC_VENDOR_SAMSUNG)
82 {
83 delete m_handler;
84 m_handler = new CANCommandHandler(this);
85 }
86 break;
87 case CEC_VENDOR_LG:
88 if (m_handler->GetVendorId() != CEC_VENDOR_LG)
89 {
90 delete m_handler;
91 m_handler = new CSLCommandHandler(this);
92 }
93 break;
94 default:
95 if (m_handler->GetVendorId() != CEC_VENDOR_UNKNOWN)
96 {
97 delete m_handler;
98 m_handler = new CCECCommandHandler(this);
99 }
100 break;
101 }
102
103 CStdString strLog;
104 strLog.Format("device %d: vendor = %s (%04x) class = %2x", m_iLogicalAddress, GetVendorName(), GetVendorId(), GetVendorClass());
105 m_processor->AddLog(CEC_LOG_DEBUG, strLog.c_str());
106 }
107
108 bool CCECBusDevice::HandleCommand(const cec_command &command)
109 {
110 CLockObject lock(&m_mutex);
111 m_iLastActive = GetTimeMs();
112 m_handler->HandleCommand(command);
113 return true;
114 }
115
116 void CCECBusDevice::PollVendorId(void)
117 {
118 CLockObject lock(&m_mutex);
119 if (m_iLastActive > 0 && m_iVendorId == CEC_VENDOR_UNKNOWN &&
120 GetTimeMs() - m_iLastActive > 5000)
121 {
122 m_iLastActive = GetTimeMs();
123
124 cec_command command;
125 cec_command::format(command, GetMyLogicalAddress(), GetLogicalAddress(), CEC_OPCODE_GIVE_DEVICE_VENDOR_ID);
126 m_processor->Transmit(command, false);
127 }
128 }
129
130 void CCECBusDevice::SetPhysicalAddress(uint16_t iNewAddress, uint16_t iOldAddress /* = 0 */)
131 {
132 CStdString strLog;
133 strLog.Format(">> %i changed physical address from %04x to %04x", GetLogicalAddress(), m_iPhysicalAddress, iNewAddress);
134 AddLog(CEC_LOG_DEBUG, strLog.c_str());
135
136 m_iPhysicalAddress = iNewAddress;
137 }
138
139 const char *CCECBusDevice::CECVendorIdToString(const uint64_t iVendorId)
140 {
141 switch (iVendorId)
142 {
143 case CEC_VENDOR_SAMSUNG:
144 return "Samsung";
145 case CEC_VENDOR_LG:
146 return "LG";
147 default:
148 return "Unknown";
149 }
150 }