| 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 | #include "lib/adapter/AdapterCommunication.h" |
| 37 | |
| 38 | extern "C" { |
| 39 | #include <interface/vmcs_host/vc_cecservice.h> |
| 40 | #include <interface/vchiq_arm/vchiq_if.h> |
| 41 | } |
| 42 | |
| 43 | namespace CEC |
| 44 | { |
| 45 | class CRPiCECAdapterCommunication; |
| 46 | class CRPiCECAdapterMessageQueue; |
| 47 | |
| 48 | class CRPiCECAdapterMessageQueueEntry |
| 49 | { |
| 50 | public: |
| 51 | CRPiCECAdapterMessageQueueEntry(CRPiCECAdapterMessageQueue *queue, const cec_command &command); |
| 52 | virtual ~CRPiCECAdapterMessageQueueEntry(void) {} |
| 53 | |
| 54 | /*! |
| 55 | * @brief Signal waiting threads |
| 56 | */ |
| 57 | void Broadcast(void); |
| 58 | |
| 59 | bool MessageReceived(cec_opcode opcode, cec_logical_address initiator, cec_logical_address destination, uint32_t response); |
| 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 | * @brief Query result from worker thread |
| 75 | */ |
| 76 | uint32_t Result() const; |
| 77 | |
| 78 | /*! |
| 79 | * @return The command that was sent in human readable form. |
| 80 | */ |
| 81 | const char *ToString(void) const { return "CEC transmission"; } |
| 82 | |
| 83 | CRPiCECAdapterMessageQueue * m_queue; |
| 84 | bool m_bWaiting; /**< true while a thread is waiting or when it hasn't started waiting yet */ |
| 85 | PLATFORM::CCondition<bool> m_condition; /**< the condition to wait on */ |
| 86 | PLATFORM::CMutex m_mutex; /**< mutex for changes to this class */ |
| 87 | cec_command m_command; |
| 88 | uint32_t m_retval; |
| 89 | bool m_bSucceeded; |
| 90 | }; |
| 91 | |
| 92 | class CRPiCECAdapterMessageQueue |
| 93 | { |
| 94 | friend class CRPiCECAdapterMessageQueueEntry; |
| 95 | |
| 96 | public: |
| 97 | /*! |
| 98 | * @brief Create a new message queue. |
| 99 | * @param com The communication handler callback to use. |
| 100 | * @param iQueueSize The outgoing message queue size. |
| 101 | */ |
| 102 | CRPiCECAdapterMessageQueue(CRPiCECAdapterCommunication *com) : |
| 103 | m_com(com), |
| 104 | m_iNextMessage(0) |
| 105 | { |
| 106 | } |
| 107 | |
| 108 | virtual ~CRPiCECAdapterMessageQueue(void) |
| 109 | { |
| 110 | Clear(); |
| 111 | } |
| 112 | |
| 113 | /*! |
| 114 | * @brief Signal and delete everything in the queue |
| 115 | */ |
| 116 | void Clear(void); |
| 117 | |
| 118 | void MessageReceived(cec_opcode opcode, cec_logical_address initiator, cec_logical_address destination, uint32_t response); |
| 119 | |
| 120 | cec_adapter_message_state Write(const cec_command &command, bool &bRetry, uint32_t iLineTimeout, bool bIsReply, VC_CEC_ERROR_T &vcReply); |
| 121 | |
| 122 | private: |
| 123 | CRPiCECAdapterCommunication * m_com; /**< the communication handler */ |
| 124 | PLATFORM::CMutex m_mutex; /**< mutex for changes to this class */ |
| 125 | std::map<uint64_t, CRPiCECAdapterMessageQueueEntry *> m_messages; /**< the outgoing message queue */ |
| 126 | uint64_t m_iNextMessage; /**< the index of the next message */ |
| 127 | }; |
| 128 | }; |