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