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 mPersistentInterface; ///< Whether persistent the interface 84 bool mDryRun; ///< If 'DryRun' is set, the posix daemon will exit 85 ///< directly after initialization. 86 } otPlatformConfig; 87 88 /** 89 * This structure represents RCP interface metrics. 90 * 91 */ 92 typedef struct otRcpInterfaceMetrics 93 { 94 uint8_t mRcpInterfaceType; ///< The RCP interface type. 95 uint64_t mTransferredFrameCount; ///< The number of transferred frames. 96 uint64_t mTransferredValidFrameCount; ///< The number of transferred valid frames. 97 uint64_t mTransferredGarbageFrameCount; ///< The number of transferred garbage frames. 98 uint64_t mRxFrameCount; ///< The number of received frames. 99 uint64_t mRxFrameByteCount; ///< The number of received bytes. 100 uint64_t mTxFrameCount; ///< The number of transmitted frames. 101 uint64_t mTxFrameByteCount; ///< The number of transmitted bytes. 102 } otRcpInterfaceMetrics; 103 104 /** 105 * This function performs all platform-specific initialization of OpenThread's drivers and initializes the OpenThread 106 * instance. 107 * 108 * @note This function is not called by the OpenThread library. Instead, the system/RTOS should call this function 109 * when initialization of OpenThread's drivers is most appropriate. 110 * 111 * @param[in] aPlatformConfig Platform configuration structure. 112 * 113 * @returns A pointer to the OpenThread instance. 114 * 115 */ 116 otInstance *otSysInit(otPlatformConfig *aPlatformConfig); 117 118 /** 119 * This function finalizes the OpenThread instance and performs all platform-specific deinitialization for OpenThread's 120 * drivers. 121 * 122 * @note This function is not called by the OpenThread library. Instead, the system/RTOS should call this function 123 * when deinitialization of OpenThread's drivers is most appropriate. 124 * 125 */ 126 void otSysDeinit(void); 127 128 /** 129 * This structure represents a context for a select() based mainloop. 130 * 131 */ 132 typedef struct otSysMainloopContext 133 { 134 fd_set mReadFdSet; ///< The read file descriptors. 135 fd_set mWriteFdSet; ///< The write file descriptors. 136 fd_set mErrorFdSet; ///< The error file descriptors. 137 int mMaxFd; ///< The max file descriptor. 138 struct timeval mTimeout; ///< The timeout. 139 } otSysMainloopContext; 140 141 /** 142 * This function updates the file descriptor sets with file descriptors used by OpenThread drivers. 143 * 144 * @param[in] aInstance The OpenThread instance structure. 145 * @param[in,out] aMainloop A pointer to the mainloop context. 146 * 147 */ 148 void otSysMainloopUpdate(otInstance *aInstance, otSysMainloopContext *aMainloop); 149 150 /** 151 * This function polls OpenThread's mainloop. 152 * 153 * @param[in,out] aMainloop A pointer to the mainloop context. 154 * 155 * @returns value returned from select(). 156 * 157 */ 158 int otSysMainloopPoll(otSysMainloopContext *aMainloop); 159 160 /** 161 * This function performs all platform-specific processing for OpenThread's example applications. 162 * 163 * @note This function is not called by the OpenThread library. Instead, the system/RTOS should call this function 164 * in the main loop when processing OpenThread's drivers is most appropriate. 165 * 166 * @param[in] aInstance The OpenThread instance structure. 167 * @param[in] aMainloop A pointer to the mainloop context. 168 * 169 */ 170 void otSysMainloopProcess(otInstance *aInstance, const otSysMainloopContext *aMainloop); 171 172 /** 173 * This method returns the radio url help string. 174 * 175 * @returns the radio url help string. 176 * 177 */ 178 const char *otSysGetRadioUrlHelpString(void); 179 180 extern otPlatResetReason gPlatResetReason; 181 182 /** 183 * This method returns the Thread network interface name. 184 * 185 * @returns The Thread network interface name. 186 * 187 */ 188 const char *otSysGetThreadNetifName(void); 189 190 /** 191 * This method returns the Thread network interface index. 192 * 193 * @returns The Thread network interface index. 194 * 195 */ 196 unsigned int otSysGetThreadNetifIndex(void); 197 198 /** 199 * This method returns the infrastructure network interface name. 200 * 201 * @returns The infrastructure network interface name, or `nullptr` if not specified. 202 * 203 */ 204 const char *otSysGetInfraNetifName(void); 205 206 /** 207 * This method returns the radio spinel metrics. 208 * 209 * @returns The radio spinel metrics. 210 * 211 */ 212 const otRadioSpinelMetrics *otSysGetRadioSpinelMetrics(void); 213 214 /** 215 * This method returns the RCP interface metrics. 216 * 217 * @returns The RCP interface metrics. 218 * 219 */ 220 const otRcpInterfaceMetrics *otSysGetRcpInterfaceMetrics(void); 221 222 /** 223 * This function returns the ifr_flags of the infrastructure network interface. 224 * 225 * @returns The ifr_flags of infrastructure network interface. 226 * 227 */ 228 uint32_t otSysGetInfraNetifFlags(void); 229 230 typedef struct otSysInfraNetIfAddressCounters 231 { 232 uint32_t mLinkLocalAddresses; 233 uint32_t mUniqueLocalAddresses; 234 uint32_t mGlobalUnicastAddresses; 235 } otSysInfraNetIfAddressCounters; 236 237 /** 238 * This functions counts the number of addresses on the infrastructure network interface. 239 * 240 * @param[out] aAddressCounters The counters of addresses on infrastructure network interface. 241 * 242 */ 243 void otSysCountInfraNetifAddresses(otSysInfraNetIfAddressCounters *aAddressCounters); 244 245 #ifdef __cplusplus 246 } // end of extern "C" 247 #endif 248 249 #endif // OPENTHREAD_SYSTEM_H_ 250