Commit | Line | Data |
---|---|---|
a75e3a5a LOK |
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 | ||
2b44051c LOK |
34 | #include "lib/platform/threads/threads.h" |
35 | #include "lib/platform/util/buffer.h" | |
8cdaa059 | 36 | #include <map> |
cb8feb41 | 37 | #include "USBCECAdapterMessage.h" |
a75e3a5a LOK |
38 | |
39 | namespace CEC | |
40 | { | |
41 | class CUSBCECAdapterCommunication; | |
004b8382 | 42 | class CCECAdapterMessageQueue; |
2b44051c | 43 | class CCECAdapterMessage; |
a75e3a5a LOK |
44 | |
45 | class CCECAdapterMessageQueueEntry | |
46 | { | |
47 | public: | |
004b8382 | 48 | CCECAdapterMessageQueueEntry(CCECAdapterMessageQueue *queue, CCECAdapterMessage *message); |
a75e3a5a LOK |
49 | virtual ~CCECAdapterMessageQueueEntry(void); |
50 | ||
51 | /*! | |
52 | * @brief Signal waiting threads | |
53 | */ | |
54 | void Broadcast(void); | |
55 | ||
56 | /*! | |
57 | * @brief Called when a message was received. | |
58 | * @param message The message that was received. | |
59 | * @return True when this message was handled by this entry, false otherwise. | |
60 | */ | |
61 | bool MessageReceived(const CCECAdapterMessage &message); | |
62 | ||
63 | /*! | |
64 | * @brief Wait for a response to this command. | |
65 | * @param iTimeout The timeout to use while waiting. | |
66 | * @return True when a response was received before the timeout passed, false otherwise. | |
67 | */ | |
68 | bool Wait(uint32_t iTimeout); | |
69 | ||
70 | /*! | |
71 | * @return True while a thread is waiting for a signal or isn't waiting yet, false otherwise. | |
72 | */ | |
73 | bool IsWaiting(void); | |
74 | ||
75 | /*! | |
76 | * @return The msgcode of the command that was sent. | |
77 | */ | |
78 | cec_adapter_messagecode MessageCode(void); | |
79 | ||
80 | /*! | |
81 | * @brief Check whether a message is a response to this command. | |
82 | * @param msg The message to check. | |
83 | * @return True when it's a response, false otherwise. | |
84 | */ | |
85 | bool IsResponse(const CCECAdapterMessage &msg); | |
3ead056c | 86 | bool IsResponseOld(const CCECAdapterMessage &msg); |
a75e3a5a LOK |
87 | |
88 | /*! | |
89 | * @return The command that was sent in human readable form. | |
90 | */ | |
91 | const char *ToString(void) const; | |
92 | ||
a75e3a5a LOK |
93 | /*! |
94 | * @brief Called when a 'command accepted' message was received. | |
95 | * @param message The message that was received. | |
8cdaa059 | 96 | * @return True when the message was handled, false otherwise. |
a75e3a5a LOK |
97 | */ |
98 | bool MessageReceivedCommandAccepted(const CCECAdapterMessage &message); | |
99 | ||
100 | /*! | |
101 | * @brief Called when a 'transmit succeeded' message was received. | |
102 | * @param message The message that was received. | |
8cdaa059 | 103 | * @return True when the message was handled, false otherwise. |
a75e3a5a LOK |
104 | */ |
105 | bool MessageReceivedTransmitSucceeded(const CCECAdapterMessage &message); | |
106 | ||
107 | /*! | |
108 | * @brief Called when a message that's not a 'command accepted' or 'transmit succeeded' message was received. | |
109 | * @param message The message that was received. | |
8cdaa059 | 110 | * @return True when the message was handled, false otherwise. |
a75e3a5a LOK |
111 | */ |
112 | bool MessageReceivedResponse(const CCECAdapterMessage &message); | |
113 | ||
8cdaa059 LOK |
114 | /*! |
115 | * @brief Signals the waiting thread. | |
116 | */ | |
117 | void Signal(void); | |
118 | ||
3ead056c LOK |
119 | bool ProvidesExtendedResponse(void); |
120 | ||
004b8382 | 121 | CCECAdapterMessageQueue * m_queue; |
a75e3a5a LOK |
122 | CCECAdapterMessage * m_message; /**< the message that was sent */ |
123 | uint8_t m_iPacketsLeft; /**< the amount of acks that we're waiting on */ | |
124 | bool m_bSucceeded; /**< true when the command received a response, false otherwise */ | |
125 | bool m_bWaiting; /**< true while a thread is waiting or when it hasn't started waiting yet */ | |
126 | PLATFORM::CCondition<bool> m_condition; /**< the condition to wait on */ | |
127 | PLATFORM::CMutex m_mutex; /**< mutex for changes to this class */ | |
128 | }; | |
129 | ||
a8559e01 | 130 | class CCECAdapterMessageQueue : public PLATFORM::CThread |
a75e3a5a LOK |
131 | { |
132 | friend class CUSBCECAdapterCommunication; | |
004b8382 | 133 | friend class CCECAdapterMessageQueueEntry; |
a75e3a5a LOK |
134 | |
135 | public: | |
136 | /*! | |
137 | * @brief Create a new message queue. | |
138 | * @param com The communication handler callback to use. | |
139 | * @param iQueueSize The outgoing message queue size. | |
140 | */ | |
2b44051c | 141 | CCECAdapterMessageQueue(CUSBCECAdapterCommunication *com); |
a75e3a5a LOK |
142 | virtual ~CCECAdapterMessageQueue(void); |
143 | ||
144 | /*! | |
145 | * @brief Signal and delete everything in the queue | |
146 | */ | |
147 | void Clear(void); | |
148 | ||
149 | /*! | |
150 | * @brief Called when a message was received from the adapter. | |
151 | * @param msg The message that was received. | |
152 | */ | |
153 | void MessageReceived(const CCECAdapterMessage &msg); | |
154 | ||
155 | /*! | |
156 | * @brief Adds received data to the current frame. | |
157 | * @param data The data to add. | |
158 | * @param iLen The length of the data to add. | |
159 | */ | |
160 | void AddData(uint8_t *data, size_t iLen); | |
161 | ||
162 | /*! | |
163 | * @brief Transmit a command to the adapter and wait for a response. | |
164 | * @param msg The command to send. | |
165 | * @return True when written, false otherwise. | |
166 | */ | |
167 | bool Write(CCECAdapterMessage *msg); | |
168 | ||
3ead056c LOK |
169 | bool ProvidesExtendedResponse(void); |
170 | ||
a8559e01 LOK |
171 | virtual void *Process(void); |
172 | ||
a75e3a5a | 173 | private: |
a75e3a5a LOK |
174 | CUSBCECAdapterCommunication * m_com; /**< the communication handler */ |
175 | PLATFORM::CMutex m_mutex; /**< mutex for changes to this class */ | |
8cdaa059 | 176 | std::map<uint64_t, CCECAdapterMessageQueueEntry *> m_messages; /**< the outgoing message queue */ |
a8559e01 | 177 | PLATFORM::SyncedBuffer<CCECAdapterMessageQueueEntry *> m_writeQueue; /**< the queue for messages that are to be written */ |
8cdaa059 | 178 | uint64_t m_iNextMessage; /**< the index of the next message */ |
2b44051c | 179 | CCECAdapterMessage *m_incomingAdapterMessage; /**< the current incoming message that's being assembled */ |
a75e3a5a LOK |
180 | cec_command m_currentCECFrame; /**< the current incoming CEC command that's being assembled */ |
181 | }; | |
182 | } |