1 /* 2 * Copyright (c) 2016, 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 /** 30 * @file 31 * @brief 32 * This file defines the platform-specific functions needed by OpenThread's example applications. 33 */ 34 35 #ifndef OPENTHREAD_SYSTEM_H_ 36 #define OPENTHREAD_SYSTEM_H_ 37 38 #include <setjmp.h> 39 #include <stdbool.h> 40 #include <stdint.h> 41 #include <stdio.h> 42 #include <sys/select.h> 43 44 #include <openthread/error.h> 45 #include <openthread/instance.h> 46 #include <openthread/platform/misc.h> 47 48 #include "lib/spinel/radio_spinel_metrics.h" 49 50 #ifdef __cplusplus 51 extern "C" { 52 #endif 53 54 /** 55 * This enumeration represents default parameters for the SPI interface. 56 * 57 */ 58 enum 59 { 60 OT_PLATFORM_CONFIG_SPI_DEFAULT_MODE = 0, ///< Default SPI Mode: CPOL=0, CPHA=0. 61 OT_PLATFORM_CONFIG_SPI_DEFAULT_SPEED_HZ = 1000000, ///< Default SPI speed in hertz. 62 OT_PLATFORM_CONFIG_SPI_DEFAULT_CS_DELAY_US = 20, ///< Default delay after SPI C̅S̅ assertion, in µsec. 63 OT_PLATFORM_CONFIG_SPI_DEFAULT_RESET_DELAY_MS = 0, ///< Default delay after R̅E̅S̅E̅T̅ assertion, in miliseconds. 64 OT_PLATFORM_CONFIG_SPI_DEFAULT_ALIGN_ALLOWANCE = 65 16, ///< Default maximum number of 0xFF bytes to clip from start of MISO frame. 66 OT_PLATFORM_CONFIG_SPI_DEFAULT_SMALL_PACKET_SIZE = 67 32, ///< Default smallest SPI packet size we can receive in a single transaction. 68 OT_PLATFORM_CONFIG_MAX_RADIO_URLS = 2, ///< Max number of Radio URLs. 69 }; 70 71 /** 72 * This structure represents platform specific configurations. 73 * 74 */ 75 typedef struct otPlatformConfig 76 { 77 const char *mBackboneInterfaceName; ///< Backbone network interface name. 78 const char *mInterfaceName; ///< Thread network interface name. 79 const char *mRadioUrls[OT_PLATFORM_CONFIG_MAX_RADIO_URLS]; ///< Radio URLs. 80 uint8_t mRadioUrlNum; ///< Number of Radio URLs. 81 int mRealTimeSignal; ///< The real-time signal for microsecond timer. 82 uint32_t mSpeedUpFactor; ///< Speed up factor. 83 bool mDryRun; ///< If 'DryRun' is set, the posix daemon will exit 84 ///< directly after initialization. 85 } otPlatformConfig; 86 87 /** 88 * This structure represents RCP interface metrics. 89 * 90 */ 91 typedef struct otRcpInterfaceMetrics 92 { 93 uint8_t mRcpInterfaceType; ///< The RCP interface type. 94 uint64_t mTransferredFrameCount; ///< The number of transferred frames. 95 uint64_t mTransferredValidFrameCount; ///< The number of transferred valid frames. 96 uint64_t mTransferredGarbageFrameCount; ///< The number of transferred garbage frames. 97 uint64_t mRxFrameCount; ///< The number of received frames. 98 uint64_t mRxFrameByteCount; ///< The number of received bytes. 99 uint64_t mTxFrameCount; ///< The number of transmitted frames. 100 uint64_t mTxFrameByteCount; ///< The number of transmitted bytes. 101 } otRcpInterfaceMetrics; 102 103 /** 104 * This function performs all platform-specific initialization of OpenThread's drivers and initializes the OpenThread 105 * instance. 106 * 107 * @note This function is not called by the OpenThread library. Instead, the system/RTOS should call this function 108 * when initialization of OpenThread's drivers is most appropriate. 109 * 110 * @param[in] aPlatformConfig Platform configuration structure. 111 * 112 * @returns A pointer to the OpenThread instance. 113 * 114 */ 115 otInstance *otSysInit(otPlatformConfig *aPlatformConfig); 116 117 /** 118 * This function finalizes the OpenThread instance and performs all platform-specific deinitialization for OpenThread's 119 * drivers. 120 * 121 * @note This function is not called by the OpenThread library. Instead, the system/RTOS should call this function 122 * when deinitialization of OpenThread's drivers is most appropriate. 123 * 124 */ 125 void otSysDeinit(void); 126 127 /** 128 * This structure represents a context for a select() based mainloop. 129 * 130 */ 131 typedef struct otSysMainloopContext 132 { 133 fd_set mReadFdSet; ///< The read file descriptors. 134 fd_set mWriteFdSet; ///< The write file descriptors. 135 fd_set mErrorFdSet; ///< The error file descriptors. 136 int mMaxFd; ///< The max file descriptor. 137 struct timeval mTimeout; ///< The timeout. 138 } otSysMainloopContext; 139 140 /** 141 * This function updates the file descriptor sets with file descriptors used by OpenThread drivers. 142 * 143 * @param[in] aInstance The OpenThread instance structure. 144 * @param[in,out] aMainloop A pointer to the mainloop context. 145 * 146 */ 147 void otSysMainloopUpdate(otInstance *aInstance, otSysMainloopContext *aMainloop); 148 149 /** 150 * This function polls OpenThread's mainloop. 151 * 152 * @param[in,out] aMainloop A pointer to the mainloop context. 153 * 154 * @returns value returned from select(). 155 * 156 */ 157 int otSysMainloopPoll(otSysMainloopContext *aMainloop); 158 159 /** 160 * This function performs all platform-specific processing for OpenThread's example applications. 161 * 162 * @note This function is not called by the OpenThread library. Instead, the system/RTOS should call this function 163 * in the main loop when processing OpenThread's drivers is most appropriate. 164 * 165 * @param[in] aInstance The OpenThread instance structure. 166 * @param[in] aMainloop A pointer to the mainloop context. 167 * 168 */ 169 void otSysMainloopProcess(otInstance *aInstance, const otSysMainloopContext *aMainloop); 170 171 /** 172 * This method returns the radio url help string. 173 * 174 * @returns the radio url help string. 175 * 176 */ 177 const char *otSysGetRadioUrlHelpString(void); 178 179 extern otPlatResetReason gPlatResetReason; 180 181 /** 182 * This method returns the Thread network interface name. 183 * 184 * @returns The Thread network interface name. 185 * 186 */ 187 const char *otSysGetThreadNetifName(void); 188 189 /** 190 * This method returns the Thread network interface index. 191 * 192 * @returns The Thread network interface index. 193 * 194 */ 195 unsigned int otSysGetThreadNetifIndex(void); 196 197 /** 198 * This method returns the infrastructure network interface name. 199 * 200 * @returns The infrastructure network interface name, or `nullptr` if not specified. 201 * 202 */ 203 const char *otSysGetInfraNetifName(void); 204 205 /** 206 * This method returns the radio spinel metrics. 207 * 208 * @returns The radio spinel metrics. 209 * 210 */ 211 const otRadioSpinelMetrics *otSysGetRadioSpinelMetrics(void); 212 213 /** 214 * This method returns the RCP interface metrics. 215 * 216 * @returns The RCP interface metrics. 217 * 218 */ 219 const otRcpInterfaceMetrics *otSysGetRcpInterfaceMetrics(void); 220 221 #ifdef __cplusplus 222 } // end of extern "C" 223 #endif 224 225 #endif // OPENTHREAD_SYSTEM_H_ 226