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