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