e1f6d68517eee2908cc7f1b6906e72db0f29240b
[deb_libcec.git] / CECProcessor.h
1 #pragma once
2 /*
3 * This file is part of the libCEC(R) library.
4 *
5 * libCEC(R) is Copyright (C) 2011-2012 Pulse-Eight Limited. All rights reserved.
6 * libCEC(R) is an original work, containing original code.
7 *
8 * libCEC(R) is a trademark of Pulse-Eight Limited.
9 *
10 * This program is dual-licensed; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 *
24 *
25 * Alternatively, you can license this library under a commercial license,
26 * please contact Pulse-Eight Licensing for more information.
27 *
28 * For more information contact:
29 * Pulse-Eight Licensing <license@pulse-eight.com>
30 * http://www.pulse-eight.com/
31 * http://www.pulse-eight.net/
32 */
33
34 #include <string>
35
36 #include "platform/threads/threads.h"
37 #include "platform/util/buffer.h"
38
39 #include "adapter/AdapterCommunication.h"
40 #include "devices/CECDeviceMap.h"
41 #include "CECInputBuffer.h"
42
43 namespace CEC
44 {
45 class CLibCEC;
46 class IAdapterCommunication;
47 class CCECBusDevice;
48 class CCECAudioSystem;
49 class CCECPlaybackDevice;
50 class CCECRecordingDevice;
51 class CCECTuner;
52 class CCECTV;
53 class CCECClient;
54 class CCECProcessor;
55
56 class CCECAllocateLogicalAddress : public PLATFORM::CThread
57 {
58 public:
59 CCECAllocateLogicalAddress(CCECProcessor* processor, CCECClient* client);
60 void* Process(void);
61
62 private:
63 CCECProcessor* m_processor;
64 CCECClient* m_client;
65 };
66
67 class CCECProcessor : public PLATFORM::CThread, public IAdapterCommunicationCallback
68 {
69 public:
70 CCECProcessor(CLibCEC *libcec);
71 virtual ~CCECProcessor(void);
72
73 bool Start(const char *strPort, uint16_t iBaudRate = CEC_SERIAL_DEFAULT_BAUDRATE, uint32_t iTimeoutMs = CEC_DEFAULT_CONNECT_TIMEOUT);
74 void *Process(void);
75 void Close(void);
76
77 bool RegisterClient(CCECClient *client);
78 bool UnregisterClient(CCECClient *client);
79 void UnregisterClients(void);
80 CCECClient *GetPrimaryClient(void);
81 CCECClient *GetClient(const cec_logical_address address);
82
83 bool OnCommandReceived(const cec_command &command);
84 void HandleLogicalAddressLost(cec_logical_address oldAddress);
85
86 CCECBusDevice * GetDevice(cec_logical_address address) const;
87 CCECAudioSystem * GetAudioSystem(void) const;
88 CCECPlaybackDevice * GetPlaybackDevice(cec_logical_address address) const;
89 CCECRecordingDevice * GetRecordingDevice(cec_logical_address address) const;
90 CCECTuner * GetTuner(cec_logical_address address) const;
91 CCECTV * GetTV(void) const;
92
93 CCECBusDevice * GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress, bool bSuppressUpdate = true);
94 CCECBusDevice * GetPrimaryDevice(void);
95 cec_logical_address GetLogicalAddress(void);
96 cec_logical_addresses GetLogicalAddresses(void);
97 bool IsPresentDevice(cec_logical_address address);
98 bool IsPresentDeviceType(cec_device_type type);
99 uint16_t GetDetectedPhysicalAddress(void) const;
100 uint64_t GetLastTransmission(void) const { return m_iLastTransmission; }
101 cec_logical_address GetActiveSource(bool bRequestActiveSource = true);
102 bool IsActiveSource(cec_logical_address iAddress);
103 bool CECInitialised(void);
104
105 bool StandbyDevices(const cec_logical_address initiator, const CECDEVICEVEC &devices);
106 bool StandbyDevice(const cec_logical_address initiator, cec_logical_address address);
107 bool PowerOnDevices(const cec_logical_address initiator, const CECDEVICEVEC &devices);
108 bool PowerOnDevice(const cec_logical_address initiator, cec_logical_address address);
109
110 bool SetDeckInfo(cec_deck_info info, bool bSendUpdate = true);
111 bool ActivateSource(uint16_t iStreamPath);
112 bool PollDevice(cec_logical_address iAddress);
113 void SetStandardLineTimeout(uint8_t iTimeout);
114 uint8_t GetStandardLineTimeout(void);
115 void SetRetryLineTimeout(uint8_t iTimeout);
116 uint8_t GetRetryLineTimeout(void);
117 bool CanPersistConfiguration(void);
118 bool PersistConfiguration(const libcec_configuration &configuration);
119 void RescanActiveDevices(void);
120
121 bool SetLineTimeout(uint8_t iTimeout);
122
123 bool Transmit(const cec_command &data, bool bIsReply);
124 void TransmitAbort(cec_logical_address source, cec_logical_address destination, cec_opcode opcode, cec_abort_reason reason = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE);
125
126 bool StartBootloader(const char *strPort = NULL);
127 bool PingAdapter(void);
128 void HandlePoll(cec_logical_address initiator, cec_logical_address destination);
129 bool HandleReceiveFailed(cec_logical_address initiator);
130
131 bool GetDeviceInformation(const char *strPort, libcec_configuration *config, uint32_t iTimeoutMs = CEC_DEFAULT_CONNECT_TIMEOUT);
132
133 bool TransmitPendingActiveSourceCommands(void);
134
135 CCECDeviceMap *GetDevices(void) const { return m_busDevices; }
136 CLibCEC *GetLib(void) const { return m_libcec; }
137
138 bool IsHandledByLibCEC(const cec_logical_address address) const;
139
140 bool TryLogicalAddress(cec_logical_address address, cec_version libCECSpecVersion = CEC_VERSION_1_4);
141
142 bool IsRunningLatestFirmware(void);
143 void SwitchMonitoring(bool bSwitchTo);
144
145 bool AllocateLogicalAddresses(CCECClient* client);
146 private:
147 bool OpenConnection(const char *strPort, uint16_t iBaudRate, uint32_t iTimeoutMs, bool bStartListening = true);
148 void SetCECInitialised(bool bSetTo = true);
149
150 void ReplaceHandlers(void);
151 bool PhysicalAddressInUse(uint16_t iPhysicalAddress);
152
153 bool ClearLogicalAddresses(void);
154 bool SetLogicalAddresses(const cec_logical_addresses &addresses);
155
156 void LogOutput(const cec_command &data);
157 void ProcessCommand(const cec_command &command);
158
159 void ResetMembers(void);
160
161 bool m_bInitialised;
162 PLATFORM::CMutex m_mutex;
163 IAdapterCommunication * m_communication;
164 CLibCEC* m_libcec;
165 uint8_t m_iStandardLineTimeout;
166 uint8_t m_iRetryLineTimeout;
167 uint64_t m_iLastTransmission;
168 CCECInputBuffer m_inBuffer;
169 CCECDeviceMap * m_busDevices;
170 std::map<cec_logical_address, CCECClient *> m_clients;
171 bool m_bMonitor;
172 CCECAllocateLogicalAddress* m_addrAllocator;
173 bool m_bStallCommunication;
174 };
175 };