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