af5756b68aa5588815f19344f74d3aa66434b9e8
[deb_libcec.git] / src / lib / adapter / RPi / RPiCECAdapterMessageQueue.h
1 #pragma once
2 /*
3 * This file is part of the libCEC(R) library.
4 *
5 * libCEC(R) is Copyright (C) 2011-2013 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/util/buffer.h"
35 #include <map>
36
37 namespace CEC
38 {
39 class CRPiCECAdapterCommunication;
40 class CRPiCECAdapterMessageQueue;
41
42 class CRPiCECAdapterMessageQueueEntry
43 {
44 public:
45 CRPiCECAdapterMessageQueueEntry(CRPiCECAdapterMessageQueue *queue, const cec_command &command);
46 virtual ~CRPiCECAdapterMessageQueueEntry(void) {}
47
48 /*!
49 * @brief Signal waiting threads
50 */
51 void Broadcast(void);
52
53 bool MessageReceived(cec_opcode opcode, cec_logical_address initiator, cec_logical_address destination, uint32_t response);
54
55 /*!
56 * @brief Wait for a response to this command.
57 * @param iTimeout The timeout to use while waiting.
58 * @return True when a response was received before the timeout passed, false otherwise.
59 */
60 bool Wait(uint32_t iTimeout);
61
62 /*!
63 * @return True while a thread is waiting for a signal or isn't waiting yet, false otherwise.
64 */
65 bool IsWaiting(void);
66
67 /*!
68 * @return The command that was sent in human readable form.
69 */
70 const char *ToString(void) const { return "CEC transmission"; }
71
72 CRPiCECAdapterMessageQueue * m_queue;
73 bool m_bWaiting; /**< true while a thread is waiting or when it hasn't started waiting yet */
74 PLATFORM::CCondition<bool> m_condition; /**< the condition to wait on */
75 PLATFORM::CMutex m_mutex; /**< mutex for changes to this class */
76 cec_command m_command;
77 uint32_t m_retval;
78 bool m_bSucceeded;
79 };
80
81 class CRPiCECAdapterMessageQueue
82 {
83 friend class CRPiCECAdapterMessageQueueEntry;
84
85 public:
86 /*!
87 * @brief Create a new message queue.
88 * @param com The communication handler callback to use.
89 * @param iQueueSize The outgoing message queue size.
90 */
91 CRPiCECAdapterMessageQueue(CRPiCECAdapterCommunication *com) :
92 m_com(com),
93 m_iNextMessage(0)
94 {
95 }
96
97 virtual ~CRPiCECAdapterMessageQueue(void)
98 {
99 Clear();
100 }
101
102 /*!
103 * @brief Signal and delete everything in the queue
104 */
105 void Clear(void);
106
107 void MessageReceived(cec_opcode opcode, cec_logical_address initiator, cec_logical_address destination, uint32_t response);
108
109 bool Write(const cec_command &command, bool bIsReply);
110
111 private:
112 CRPiCECAdapterCommunication * m_com; /**< the communication handler */
113 PLATFORM::CMutex m_mutex; /**< mutex for changes to this class */
114 std::map<uint64_t, CRPiCECAdapterMessageQueueEntry *> m_messages; /**< the outgoing message queue */
115 uint64_t m_iNextMessage; /**< the index of the next message */
116 };
117 };