1 /* 2 * Copyright (c) 2021, The OpenThread Authors. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 3. Neither the name of the copyright holder nor the 13 * names of its contributors may be used to endorse or promote products 14 * derived from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #ifndef OT_POSIX_PLATFORM_RADIO_HPP_ 30 #define OT_POSIX_PLATFORM_RADIO_HPP_ 31 32 #include "hdlc_interface.hpp" 33 #include "logger.hpp" 34 #include "radio_url.hpp" 35 #include "spi_interface.hpp" 36 #include "vendor_interface.hpp" 37 #include "common/code_utils.hpp" 38 #include "lib/spinel/radio_spinel.hpp" 39 #if OPENTHREAD_SPINEL_CONFIG_VENDOR_HOOK_ENABLE 40 #ifdef OPENTHREAD_SPINEL_CONFIG_VENDOR_HOOK_HEADER 41 #include OPENTHREAD_SPINEL_CONFIG_VENDOR_HOOK_HEADER 42 #endif 43 #endif 44 45 namespace ot { 46 namespace Posix { 47 48 /** 49 * Manages Thread radio. 50 * 51 */ 52 class Radio : public Logger<Radio> 53 { 54 public: 55 static const char kLogModuleName[]; ///< Module name used for logging. 56 57 /** 58 * Creates the radio manager. 59 * 60 */ 61 Radio(void); 62 63 /** 64 * Initialize the Thread radio. 65 * 66 * @param[in] aUrl A pointer to the null-terminated URL. 67 * 68 */ 69 void Init(const char *aUrl); 70 71 /** 72 * Acts as an accessor to the spinel interface instance used by the radio. 73 * 74 * @returns A reference to the radio's spinel interface instance. 75 * 76 */ GetSpinelInterface(void)77 Spinel::SpinelInterface &GetSpinelInterface(void) 78 { 79 OT_ASSERT(mSpinelInterface != nullptr); 80 return *mSpinelInterface; 81 } 82 83 /** 84 * Acts as an accessor to the radio spinel instance used by the radio. 85 * 86 * @returns A reference to the radio spinel instance. 87 * 88 */ GetRadioSpinel(void)89 Spinel::RadioSpinel &GetRadioSpinel(void) { return mRadioSpinel; } 90 91 private: 92 #if OPENTHREAD_POSIX_VIRTUAL_TIME 93 void VirtualTimeInit(void); 94 #endif 95 void ProcessRadioUrl(const RadioUrl &aRadioUrl); 96 void ProcessMaxPowerTable(const RadioUrl &aRadioUrl); 97 98 Spinel::SpinelInterface *CreateSpinelInterface(const char *aInterfaceName); 99 void GetIidListFromRadioUrl(spinel_iid_t (&aIidList)[Spinel::kSpinelHeaderMaxNumIid]); 100 101 #if OPENTHREAD_POSIX_CONFIG_SPINEL_HDLC_INTERFACE_ENABLE && OPENTHREAD_POSIX_CONFIG_SPINEL_SPI_INTERFACE_ENABLE 102 static constexpr size_t kSpinelInterfaceRawSize = sizeof(ot::Posix::SpiInterface) > sizeof(ot::Posix::HdlcInterface) 103 ? sizeof(ot::Posix::SpiInterface) 104 : sizeof(ot::Posix::HdlcInterface); 105 #elif OPENTHREAD_POSIX_CONFIG_SPINEL_HDLC_INTERFACE_ENABLE 106 static constexpr size_t kSpinelInterfaceRawSize = sizeof(ot::Posix::HdlcInterface); 107 #elif OPENTHREAD_POSIX_CONFIG_SPINEL_SPI_INTERFACE_ENABLE 108 static constexpr size_t kSpinelInterfaceRawSize = sizeof(ot::Posix::SpiInterface); 109 #elif OPENTHREAD_POSIX_CONFIG_SPINEL_VENDOR_INTERFACE_ENABLE 110 static constexpr size_t kSpinelInterfaceRawSize = sizeof(ot::Posix::VendorInterface); 111 #else 112 #error "No Spinel interface is specified!" 113 #endif 114 115 RadioUrl mRadioUrl; 116 #if OPENTHREAD_SPINEL_CONFIG_VENDOR_HOOK_ENABLE 117 Spinel::VendorRadioSpinel mRadioSpinel; 118 #else 119 Spinel::RadioSpinel mRadioSpinel; 120 #endif 121 Spinel::SpinelInterface *mSpinelInterface; 122 123 OT_DEFINE_ALIGNED_VAR(mSpinelInterfaceRaw, kSpinelInterfaceRawSize, uint64_t); 124 }; 125 126 } // namespace Posix 127 } // namespace ot 128 129 #endif // OT_POSIX_PLATFORM_RADIO_HPP_ 130