TI's CC3100. A test demo with very little testing done!
Fork of cc3100_Test_Demo2 by
simplelink/cc3100.cpp@3:b89198ac2efe, 2015-02-22 (annotated)
- Committer:
- dflet
- Date:
- Sun Feb 22 18:33:10 2015 +0000
- Revision:
- 3:b89198ac2efe
- Parent:
- 2:b3fd5b3d9860
Removed more debug.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dflet | 0:e89ba455dbcf | 1 | /* |
dflet | 0:e89ba455dbcf | 2 | * device.c - CC31xx/CC32xx Host Driver Implementation |
dflet | 0:e89ba455dbcf | 3 | * |
dflet | 0:e89ba455dbcf | 4 | * Copyright (C) 2014 Texas Instruments Incorporated - http://www.ti.com/ |
dflet | 0:e89ba455dbcf | 5 | * |
dflet | 0:e89ba455dbcf | 6 | * |
dflet | 0:e89ba455dbcf | 7 | * Redistribution and use in source and binary forms, with or without |
dflet | 0:e89ba455dbcf | 8 | * modification, are permitted provided that the following conditions |
dflet | 0:e89ba455dbcf | 9 | * are met: |
dflet | 0:e89ba455dbcf | 10 | * |
dflet | 0:e89ba455dbcf | 11 | * Redistributions of source code must retain the above copyright |
dflet | 0:e89ba455dbcf | 12 | * notice, this list of conditions and the following disclaimer. |
dflet | 0:e89ba455dbcf | 13 | * |
dflet | 0:e89ba455dbcf | 14 | * Redistributions in binary form must reproduce the above copyright |
dflet | 0:e89ba455dbcf | 15 | * notice, this list of conditions and the following disclaimer in the |
dflet | 0:e89ba455dbcf | 16 | * documentation and/or other materials provided with the |
dflet | 0:e89ba455dbcf | 17 | * distribution. |
dflet | 0:e89ba455dbcf | 18 | * |
dflet | 0:e89ba455dbcf | 19 | * Neither the name of Texas Instruments Incorporated nor the names of |
dflet | 0:e89ba455dbcf | 20 | * its contributors may be used to endorse or promote products derived |
dflet | 0:e89ba455dbcf | 21 | * from this software without specific prior written permission. |
dflet | 0:e89ba455dbcf | 22 | * |
dflet | 0:e89ba455dbcf | 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
dflet | 0:e89ba455dbcf | 24 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
dflet | 0:e89ba455dbcf | 25 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
dflet | 0:e89ba455dbcf | 26 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
dflet | 0:e89ba455dbcf | 27 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
dflet | 0:e89ba455dbcf | 28 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
dflet | 0:e89ba455dbcf | 29 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
dflet | 0:e89ba455dbcf | 30 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
dflet | 0:e89ba455dbcf | 31 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
dflet | 0:e89ba455dbcf | 32 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
dflet | 0:e89ba455dbcf | 33 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
dflet | 0:e89ba455dbcf | 34 | * |
dflet | 0:e89ba455dbcf | 35 | */ |
dflet | 0:e89ba455dbcf | 36 | |
dflet | 0:e89ba455dbcf | 37 | |
dflet | 0:e89ba455dbcf | 38 | |
dflet | 0:e89ba455dbcf | 39 | /*****************************************************************************/ |
dflet | 0:e89ba455dbcf | 40 | /* Include files */ |
dflet | 0:e89ba455dbcf | 41 | /*****************************************************************************/ |
dflet | 0:e89ba455dbcf | 42 | #include "cc3100_simplelink.h" |
dflet | 0:e89ba455dbcf | 43 | #include "cc3100_protocol.h" |
dflet | 0:e89ba455dbcf | 44 | #include "cc3100_sl_common.h" |
dflet | 0:e89ba455dbcf | 45 | #include "cc3100.h" |
dflet | 0:e89ba455dbcf | 46 | |
dflet | 0:e89ba455dbcf | 47 | #include "fPtr_func.h" |
dflet | 2:b3fd5b3d9860 | 48 | |
dflet | 0:e89ba455dbcf | 49 | |
dflet | 0:e89ba455dbcf | 50 | namespace mbed_cc3100 { |
dflet | 0:e89ba455dbcf | 51 | |
dflet | 0:e89ba455dbcf | 52 | uint32_t g_PingPacketsRecv; |
dflet | 0:e89ba455dbcf | 53 | uint32_t g_GatewayIP; |
dflet | 0:e89ba455dbcf | 54 | uint32_t g_StationIP; |
dflet | 0:e89ba455dbcf | 55 | uint32_t g_DestinationIP; |
dflet | 0:e89ba455dbcf | 56 | uint32_t g_BytesReceived; // variable to store the file size |
dflet | 0:e89ba455dbcf | 57 | uint32_t g_Status; |
dflet | 0:e89ba455dbcf | 58 | uint8_t g_buff[MAX_BUFF_SIZE+1]; |
dflet | 0:e89ba455dbcf | 59 | int32_t g_SockID; |
dflet | 0:e89ba455dbcf | 60 | |
dflet | 0:e89ba455dbcf | 61 | |
dflet | 0:e89ba455dbcf | 62 | cc3100::cc3100(PinName cc3100_irq, PinName cc3100_nHIB, PinName cc3100_cs, SPI cc3100_spi) |
dflet | 0:e89ba455dbcf | 63 | : _spi(cc3100_irq, cc3100_nHIB, cc3100_cs, cc3100_spi, _driver), |
dflet | 0:e89ba455dbcf | 64 | _driver(_nonos, _netapp, _flowcont, _spi), _nonos(_driver), _wlan(_driver, _wlan_filters), |
dflet | 0:e89ba455dbcf | 65 | _wlan_filters(_driver), _netapp(_driver, _nonos), _fs(_driver), _netcfg(_driver), |
dflet | 0:e89ba455dbcf | 66 | _socket(_driver, _nonos), _flowcont(_driver, _nonos) |
dflet | 0:e89ba455dbcf | 67 | |
dflet | 0:e89ba455dbcf | 68 | |
dflet | 0:e89ba455dbcf | 69 | { |
dflet | 0:e89ba455dbcf | 70 | |
dflet | 0:e89ba455dbcf | 71 | } |
dflet | 0:e89ba455dbcf | 72 | |
dflet | 0:e89ba455dbcf | 73 | cc3100::~cc3100() |
dflet | 0:e89ba455dbcf | 74 | { |
dflet | 0:e89ba455dbcf | 75 | |
dflet | 0:e89ba455dbcf | 76 | } |
dflet | 0:e89ba455dbcf | 77 | |
dflet | 0:e89ba455dbcf | 78 | /*! |
dflet | 0:e89ba455dbcf | 79 | \brief This function initializes the application variables |
dflet | 0:e89ba455dbcf | 80 | |
dflet | 0:e89ba455dbcf | 81 | \param[in] None |
dflet | 0:e89ba455dbcf | 82 | |
dflet | 0:e89ba455dbcf | 83 | \return 0 on success, negative error-code on error |
dflet | 0:e89ba455dbcf | 84 | */ |
dflet | 0:e89ba455dbcf | 85 | int32_t cc3100::initializeAppVariables() |
dflet | 0:e89ba455dbcf | 86 | { |
dflet | 0:e89ba455dbcf | 87 | |
dflet | 0:e89ba455dbcf | 88 | g_Status = 0; |
dflet | 0:e89ba455dbcf | 89 | g_PingPacketsRecv = 0; |
dflet | 0:e89ba455dbcf | 90 | g_StationIP = 0; |
dflet | 0:e89ba455dbcf | 91 | g_GatewayIP = 0; |
dflet | 0:e89ba455dbcf | 92 | g_DestinationIP = 0; |
dflet | 0:e89ba455dbcf | 93 | g_BytesReceived = 0; /* variable to store the file size */ |
dflet | 0:e89ba455dbcf | 94 | g_SockID = 0; |
dflet | 0:e89ba455dbcf | 95 | memset(g_buff, 0, sizeof(g_buff)); |
dflet | 0:e89ba455dbcf | 96 | |
dflet | 0:e89ba455dbcf | 97 | return SUCCESS; |
dflet | 0:e89ba455dbcf | 98 | } |
dflet | 0:e89ba455dbcf | 99 | |
dflet | 0:e89ba455dbcf | 100 | /*! |
dflet | 0:e89ba455dbcf | 101 | \brief Disconnecting from a WLAN Access point |
dflet | 0:e89ba455dbcf | 102 | |
dflet | 0:e89ba455dbcf | 103 | This function disconnects from the connected AP |
dflet | 0:e89ba455dbcf | 104 | |
dflet | 0:e89ba455dbcf | 105 | \param[in] None |
dflet | 0:e89ba455dbcf | 106 | |
dflet | 0:e89ba455dbcf | 107 | \return none |
dflet | 0:e89ba455dbcf | 108 | |
dflet | 0:e89ba455dbcf | 109 | \note |
dflet | 0:e89ba455dbcf | 110 | |
dflet | 0:e89ba455dbcf | 111 | \warning If the WLAN disconnection fails, we will be stuck in this function forever. |
dflet | 0:e89ba455dbcf | 112 | */ |
dflet | 0:e89ba455dbcf | 113 | int32_t cc3100::disconnectFromAP() |
dflet | 0:e89ba455dbcf | 114 | { |
dflet | 0:e89ba455dbcf | 115 | int32_t retVal = -1; |
dflet | 0:e89ba455dbcf | 116 | |
dflet | 0:e89ba455dbcf | 117 | /* |
dflet | 0:e89ba455dbcf | 118 | * The function returns 0 if 'Disconnected done', negative number if already disconnected |
dflet | 0:e89ba455dbcf | 119 | * Wait for 'disconnection' event if 0 is returned, Ignore other return-codes |
dflet | 0:e89ba455dbcf | 120 | */ |
dflet | 0:e89ba455dbcf | 121 | retVal = _wlan.sl_WlanDisconnect(); |
dflet | 0:e89ba455dbcf | 122 | if(0 == retVal) |
dflet | 0:e89ba455dbcf | 123 | { |
dflet | 0:e89ba455dbcf | 124 | /* Wait */ |
dflet | 3:b89198ac2efe | 125 | while(IS_CONNECTED(g_Status,STATUS_BIT_CONNECTION)) { _nonos._SlNonOsMainLoopTask(); } |
dflet | 0:e89ba455dbcf | 126 | } |
dflet | 0:e89ba455dbcf | 127 | |
dflet | 0:e89ba455dbcf | 128 | return SUCCESS; |
dflet | 0:e89ba455dbcf | 129 | } |
dflet | 0:e89ba455dbcf | 130 | |
dflet | 0:e89ba455dbcf | 131 | /*! |
dflet | 0:e89ba455dbcf | 132 | \brief This function configure the SimpleLink device in its default state. It: |
dflet | 0:e89ba455dbcf | 133 | - Sets the mode to STATION |
dflet | 0:e89ba455dbcf | 134 | - Configures connection policy to Auto and AutoSmartConfig |
dflet | 0:e89ba455dbcf | 135 | - Deletes all the stored profiles |
dflet | 0:e89ba455dbcf | 136 | - Enables DHCP |
dflet | 0:e89ba455dbcf | 137 | - Disables Scan policy |
dflet | 0:e89ba455dbcf | 138 | - Sets Tx power to maximum |
dflet | 0:e89ba455dbcf | 139 | - Sets power policy to normal |
dflet | 0:e89ba455dbcf | 140 | - Unregisters mDNS services |
dflet | 0:e89ba455dbcf | 141 | - Remove all filters |
dflet | 0:e89ba455dbcf | 142 | |
dflet | 0:e89ba455dbcf | 143 | \param[in] none |
dflet | 0:e89ba455dbcf | 144 | |
dflet | 0:e89ba455dbcf | 145 | \return On success, zero is returned. On error, negative is returned |
dflet | 0:e89ba455dbcf | 146 | */ |
dflet | 0:e89ba455dbcf | 147 | int32_t cc3100::configureSimpleLinkToDefaultState() |
dflet | 0:e89ba455dbcf | 148 | { |
dflet | 0:e89ba455dbcf | 149 | SlVersionFull ver = {0}; |
dflet | 0:e89ba455dbcf | 150 | _WlanRxFilterOperationCommandBuff_t RxFilterIdMask = {0}; |
dflet | 0:e89ba455dbcf | 151 | |
dflet | 0:e89ba455dbcf | 152 | uint8_t val = 1; |
dflet | 0:e89ba455dbcf | 153 | uint8_t configOpt = 0; |
dflet | 0:e89ba455dbcf | 154 | uint8_t configLen = 0; |
dflet | 0:e89ba455dbcf | 155 | uint8_t power = 0; |
dflet | 0:e89ba455dbcf | 156 | |
dflet | 0:e89ba455dbcf | 157 | int32_t retVal = -1; |
dflet | 0:e89ba455dbcf | 158 | int32_t role = -1; |
dflet | 0:e89ba455dbcf | 159 | |
dflet | 0:e89ba455dbcf | 160 | role = sl_Start(0, 0, 0); |
dflet | 0:e89ba455dbcf | 161 | ASSERT_ON_ERROR(role); |
dflet | 0:e89ba455dbcf | 162 | |
dflet | 0:e89ba455dbcf | 163 | /* If the device is not in station-mode, try configuring it in station-mode */ |
dflet | 0:e89ba455dbcf | 164 | if (ROLE_STA != role) { |
dflet | 0:e89ba455dbcf | 165 | if (ROLE_AP == role) { |
dflet | 0:e89ba455dbcf | 166 | /* If the device is in AP mode, we need to wait for this event before doing anything */ |
dflet | 0:e89ba455dbcf | 167 | while(!IS_IP_ACQUIRED(g_Status,STATUS_BIT_IP_ACQUIRED)) { |
dflet | 0:e89ba455dbcf | 168 | _nonos._SlNonOsMainLoopTask(); |
dflet | 0:e89ba455dbcf | 169 | } |
dflet | 0:e89ba455dbcf | 170 | } |
dflet | 0:e89ba455dbcf | 171 | |
dflet | 0:e89ba455dbcf | 172 | /* Switch to STA role and restart */ |
dflet | 0:e89ba455dbcf | 173 | retVal = _wlan.sl_WlanSetMode(ROLE_STA); |
dflet | 0:e89ba455dbcf | 174 | ASSERT_ON_ERROR(retVal); |
dflet | 0:e89ba455dbcf | 175 | |
dflet | 0:e89ba455dbcf | 176 | retVal = sl_Stop(SL_STOP_TIMEOUT); |
dflet | 0:e89ba455dbcf | 177 | ASSERT_ON_ERROR(retVal); |
dflet | 0:e89ba455dbcf | 178 | |
dflet | 0:e89ba455dbcf | 179 | retVal = sl_Start(0, 0, 0); |
dflet | 0:e89ba455dbcf | 180 | ASSERT_ON_ERROR(retVal); |
dflet | 0:e89ba455dbcf | 181 | |
dflet | 0:e89ba455dbcf | 182 | /* Check if the device is in station again */ |
dflet | 0:e89ba455dbcf | 183 | if (ROLE_STA != retVal) { |
dflet | 0:e89ba455dbcf | 184 | /* We don't want to proceed if the device is not coming up in station-mode */ |
dflet | 0:e89ba455dbcf | 185 | ASSERT_ON_ERROR(DEVICE_NOT_IN_STATION_MODE); |
dflet | 0:e89ba455dbcf | 186 | } |
dflet | 0:e89ba455dbcf | 187 | } |
dflet | 0:e89ba455dbcf | 188 | |
dflet | 0:e89ba455dbcf | 189 | /* Get the device's version-information */ |
dflet | 0:e89ba455dbcf | 190 | configOpt = SL_DEVICE_GENERAL_VERSION; |
dflet | 0:e89ba455dbcf | 191 | configLen = sizeof(ver); |
dflet | 0:e89ba455dbcf | 192 | retVal = sl_DevGet(SL_DEVICE_GENERAL_CONFIGURATION, &configOpt, &configLen, (uint8_t *)(&ver)); |
dflet | 0:e89ba455dbcf | 193 | ASSERT_ON_ERROR(retVal); |
dflet | 0:e89ba455dbcf | 194 | |
dflet | 0:e89ba455dbcf | 195 | /* Set connection policy to Auto + SmartConfig (Device's default connection policy) */ |
dflet | 0:e89ba455dbcf | 196 | retVal = _wlan.sl_WlanPolicySet(SL_POLICY_CONNECTION, SL_CONNECTION_POLICY(1, 0, 0, 0, 1), NULL, 0); |
dflet | 0:e89ba455dbcf | 197 | ASSERT_ON_ERROR(retVal); |
dflet | 0:e89ba455dbcf | 198 | |
dflet | 0:e89ba455dbcf | 199 | /* Remove all profiles */ |
dflet | 0:e89ba455dbcf | 200 | retVal = _wlan.sl_WlanProfileDel(0xFF); |
dflet | 0:e89ba455dbcf | 201 | ASSERT_ON_ERROR(retVal); |
dflet | 0:e89ba455dbcf | 202 | |
dflet | 0:e89ba455dbcf | 203 | /* |
dflet | 0:e89ba455dbcf | 204 | * Device in station-mode. Disconnect previous connection if any |
dflet | 0:e89ba455dbcf | 205 | * The function returns 0 if 'Disconnected done', negative number if already disconnected |
dflet | 0:e89ba455dbcf | 206 | * Wait for 'disconnection' event if 0 is returned, Ignore other return-codes |
dflet | 0:e89ba455dbcf | 207 | */ |
dflet | 0:e89ba455dbcf | 208 | retVal = _wlan.sl_WlanDisconnect(); |
dflet | 0:e89ba455dbcf | 209 | if(0 == retVal) { |
dflet | 0:e89ba455dbcf | 210 | /* Wait */ |
dflet | 0:e89ba455dbcf | 211 | while(IS_CONNECTED(g_Status,STATUS_BIT_CONNECTION)) { |
dflet | 3:b89198ac2efe | 212 | _nonos._SlNonOsMainLoopTask(); |
dflet | 0:e89ba455dbcf | 213 | } |
dflet | 0:e89ba455dbcf | 214 | } |
dflet | 0:e89ba455dbcf | 215 | |
dflet | 0:e89ba455dbcf | 216 | /* Enable DHCP client*/ |
dflet | 0:e89ba455dbcf | 217 | retVal = _netcfg.sl_NetCfgSet(SL_IPV4_STA_P2P_CL_DHCP_ENABLE,1,1,&val); |
dflet | 0:e89ba455dbcf | 218 | ASSERT_ON_ERROR(retVal); |
dflet | 0:e89ba455dbcf | 219 | |
dflet | 0:e89ba455dbcf | 220 | /* Disable scan */ |
dflet | 0:e89ba455dbcf | 221 | configOpt = SL_SCAN_POLICY(0); |
dflet | 0:e89ba455dbcf | 222 | retVal = _wlan.sl_WlanPolicySet(SL_POLICY_SCAN , configOpt, NULL, 0); |
dflet | 0:e89ba455dbcf | 223 | ASSERT_ON_ERROR(retVal); |
dflet | 0:e89ba455dbcf | 224 | |
dflet | 0:e89ba455dbcf | 225 | /* Set Tx power level for station mode |
dflet | 0:e89ba455dbcf | 226 | Number between 0-15, as dB offset from maximum power - 0 will set maximum power */ |
dflet | 0:e89ba455dbcf | 227 | power = 0; |
dflet | 0:e89ba455dbcf | 228 | retVal = _wlan.sl_WlanSet(SL_WLAN_CFG_GENERAL_PARAM_ID, WLAN_GENERAL_PARAM_OPT_STA_TX_POWER, 1, (uint8_t *)&power); |
dflet | 0:e89ba455dbcf | 229 | ASSERT_ON_ERROR(retVal); |
dflet | 0:e89ba455dbcf | 230 | |
dflet | 0:e89ba455dbcf | 231 | /* Set PM policy to normal */ |
dflet | 0:e89ba455dbcf | 232 | retVal = _wlan.sl_WlanPolicySet(SL_POLICY_PM , SL_NORMAL_POLICY, NULL, 0); |
dflet | 0:e89ba455dbcf | 233 | ASSERT_ON_ERROR(retVal); |
dflet | 0:e89ba455dbcf | 234 | |
dflet | 0:e89ba455dbcf | 235 | /* Unregister mDNS services */ |
dflet | 0:e89ba455dbcf | 236 | retVal = _netapp.sl_NetAppMDNSUnRegisterService(0, 0); |
dflet | 0:e89ba455dbcf | 237 | ASSERT_ON_ERROR(retVal); |
dflet | 0:e89ba455dbcf | 238 | |
dflet | 0:e89ba455dbcf | 239 | /* Remove all 64 filters (8*8) */ |
dflet | 0:e89ba455dbcf | 240 | memset(RxFilterIdMask.FilterIdMask, 0xFF, 8); |
dflet | 0:e89ba455dbcf | 241 | retVal = _wlan_filters.sl_WlanRxFilterSet(SL_REMOVE_RX_FILTER, (uint8_t *)&RxFilterIdMask, |
dflet | 0:e89ba455dbcf | 242 | sizeof(_WlanRxFilterOperationCommandBuff_t)); |
dflet | 0:e89ba455dbcf | 243 | ASSERT_ON_ERROR(retVal); |
dflet | 0:e89ba455dbcf | 244 | |
dflet | 0:e89ba455dbcf | 245 | retVal = sl_Stop(SL_STOP_TIMEOUT); |
dflet | 0:e89ba455dbcf | 246 | ASSERT_ON_ERROR(retVal); |
dflet | 0:e89ba455dbcf | 247 | |
dflet | 0:e89ba455dbcf | 248 | retVal = initializeAppVariables(); |
dflet | 0:e89ba455dbcf | 249 | ASSERT_ON_ERROR(retVal); |
dflet | 0:e89ba455dbcf | 250 | |
dflet | 0:e89ba455dbcf | 251 | return retVal; /* Success */ |
dflet | 0:e89ba455dbcf | 252 | } |
dflet | 0:e89ba455dbcf | 253 | |
dflet | 0:e89ba455dbcf | 254 | /*! |
dflet | 0:e89ba455dbcf | 255 | \brief Create UDP socket to communicate with server. |
dflet | 0:e89ba455dbcf | 256 | |
dflet | 0:e89ba455dbcf | 257 | \param[in] none |
dflet | 0:e89ba455dbcf | 258 | |
dflet | 0:e89ba455dbcf | 259 | \return Socket descriptor for success otherwise negative |
dflet | 0:e89ba455dbcf | 260 | |
dflet | 0:e89ba455dbcf | 261 | \warning |
dflet | 0:e89ba455dbcf | 262 | */ |
dflet | 0:e89ba455dbcf | 263 | int32_t cc3100::createUDPConnection() |
dflet | 0:e89ba455dbcf | 264 | { |
dflet | 0:e89ba455dbcf | 265 | int32_t sd = 0; |
dflet | 0:e89ba455dbcf | 266 | |
dflet | 0:e89ba455dbcf | 267 | sd = _socket.sl_Socket(SL_AF_INET, SL_SOCK_DGRAM, IPPROTO_UDP); |
dflet | 0:e89ba455dbcf | 268 | if( sd < 0 ) |
dflet | 0:e89ba455dbcf | 269 | { |
dflet | 0:e89ba455dbcf | 270 | printf("Error creating socket\n\r\n\r"); |
dflet | 0:e89ba455dbcf | 271 | } |
dflet | 0:e89ba455dbcf | 272 | |
dflet | 0:e89ba455dbcf | 273 | return sd; |
dflet | 0:e89ba455dbcf | 274 | } |
dflet | 0:e89ba455dbcf | 275 | |
dflet | 0:e89ba455dbcf | 276 | /*! |
dflet | 0:e89ba455dbcf | 277 | \brief Create connection with server. |
dflet | 0:e89ba455dbcf | 278 | |
dflet | 0:e89ba455dbcf | 279 | This function opens a socket and create the endpoint communication with server |
dflet | 0:e89ba455dbcf | 280 | |
dflet | 0:e89ba455dbcf | 281 | \param[in] DestinationIP - IP address of the server |
dflet | 0:e89ba455dbcf | 282 | |
dflet | 0:e89ba455dbcf | 283 | \return socket id for success and negative for error |
dflet | 0:e89ba455dbcf | 284 | */ |
dflet | 0:e89ba455dbcf | 285 | int32_t cc3100::createConnection(uint32_t DestinationIP) |
dflet | 0:e89ba455dbcf | 286 | { |
dflet | 0:e89ba455dbcf | 287 | SlSockAddrIn_t Addr = {0}; |
dflet | 0:e89ba455dbcf | 288 | int32_t Status = 0; |
dflet | 0:e89ba455dbcf | 289 | int32_t AddrSize = 0; |
dflet | 0:e89ba455dbcf | 290 | int32_t SockID = 0; |
dflet | 0:e89ba455dbcf | 291 | |
dflet | 0:e89ba455dbcf | 292 | Addr.sin_family = SL_AF_INET; |
dflet | 0:e89ba455dbcf | 293 | Addr.sin_port = _socket.sl_Htons(80); |
dflet | 0:e89ba455dbcf | 294 | Addr.sin_addr.s_addr = _socket.sl_Htonl(DestinationIP); |
dflet | 0:e89ba455dbcf | 295 | |
dflet | 0:e89ba455dbcf | 296 | AddrSize = sizeof(SlSockAddrIn_t); |
dflet | 0:e89ba455dbcf | 297 | |
dflet | 0:e89ba455dbcf | 298 | SockID = _socket.sl_Socket(SL_AF_INET,SL_SOCK_STREAM, 0); |
dflet | 0:e89ba455dbcf | 299 | ASSERT_ON_ERROR(SockID); |
dflet | 0:e89ba455dbcf | 300 | |
dflet | 0:e89ba455dbcf | 301 | Status = _socket.sl_Connect(SockID, ( SlSockAddr_t *)&Addr, AddrSize); |
dflet | 0:e89ba455dbcf | 302 | if (Status < 0) |
dflet | 0:e89ba455dbcf | 303 | { |
dflet | 0:e89ba455dbcf | 304 | _socket.sl_Close(SockID); |
dflet | 0:e89ba455dbcf | 305 | ASSERT_ON_ERROR(Status); |
dflet | 0:e89ba455dbcf | 306 | } |
dflet | 0:e89ba455dbcf | 307 | |
dflet | 0:e89ba455dbcf | 308 | return SockID; |
dflet | 0:e89ba455dbcf | 309 | } |
dflet | 0:e89ba455dbcf | 310 | |
dflet | 0:e89ba455dbcf | 311 | /*! |
dflet | 0:e89ba455dbcf | 312 | \brief Convert hex to decimal base |
dflet | 0:e89ba455dbcf | 313 | |
dflet | 0:e89ba455dbcf | 314 | \param[in] ptr - pointer to string containing number in hex |
dflet | 0:e89ba455dbcf | 315 | |
dflet | 0:e89ba455dbcf | 316 | \return number in decimal base |
dflet | 0:e89ba455dbcf | 317 | |
dflet | 0:e89ba455dbcf | 318 | */ |
dflet | 0:e89ba455dbcf | 319 | int32_t cc3100::hexToi(unsigned char *ptr) |
dflet | 0:e89ba455dbcf | 320 | { |
dflet | 0:e89ba455dbcf | 321 | uint32_t result = 0; |
dflet | 0:e89ba455dbcf | 322 | uint32_t len = 0; |
dflet | 0:e89ba455dbcf | 323 | |
dflet | 0:e89ba455dbcf | 324 | int32_t idx = -1; |
dflet | 0:e89ba455dbcf | 325 | |
dflet | 0:e89ba455dbcf | 326 | len = strlen((const char*) ptr); |
dflet | 0:e89ba455dbcf | 327 | |
dflet | 0:e89ba455dbcf | 328 | /* convert characters to upper case */ |
dflet | 0:e89ba455dbcf | 329 | for(idx = 0; ptr[idx] != '\0'; ++idx) |
dflet | 0:e89ba455dbcf | 330 | { |
dflet | 0:e89ba455dbcf | 331 | if( (ptr[idx] >= 'a') && |
dflet | 0:e89ba455dbcf | 332 | (ptr[idx] <= 'f') ) |
dflet | 0:e89ba455dbcf | 333 | { |
dflet | 0:e89ba455dbcf | 334 | ptr[idx] -= 32; /* Change case - ASCII 'a' = 97, 'A' = 65 => 97-65 = 32 */ |
dflet | 0:e89ba455dbcf | 335 | } |
dflet | 0:e89ba455dbcf | 336 | } |
dflet | 0:e89ba455dbcf | 337 | |
dflet | 0:e89ba455dbcf | 338 | for(idx = 0; ptr[idx] != '\0'; ++idx) |
dflet | 0:e89ba455dbcf | 339 | { |
dflet | 0:e89ba455dbcf | 340 | if(ptr[idx] >= '0' && ptr[idx] <= '9') |
dflet | 0:e89ba455dbcf | 341 | { |
dflet | 0:e89ba455dbcf | 342 | /* Converting '0' to '9' to their decimal value */ |
dflet | 0:e89ba455dbcf | 343 | result += (ptr[idx] - '0') * (1 << (4 * (len - 1 - idx))); |
dflet | 0:e89ba455dbcf | 344 | } |
dflet | 0:e89ba455dbcf | 345 | else if(ptr[idx] >= 'A' && ptr[idx] <= 'F') |
dflet | 0:e89ba455dbcf | 346 | { |
dflet | 0:e89ba455dbcf | 347 | /* Converting hex 'A' to 'F' to their decimal value */ |
dflet | 0:e89ba455dbcf | 348 | result += (ptr[idx] - 55) * (1 << (4 * (len -1 - idx))); /* .i.e. 'A' - 55 = 10, 'F' - 55 = 15 */ |
dflet | 0:e89ba455dbcf | 349 | } |
dflet | 0:e89ba455dbcf | 350 | else |
dflet | 0:e89ba455dbcf | 351 | { |
dflet | 0:e89ba455dbcf | 352 | ASSERT_ON_ERROR(INVALID_HEX_STRING); |
dflet | 0:e89ba455dbcf | 353 | } |
dflet | 0:e89ba455dbcf | 354 | } |
dflet | 0:e89ba455dbcf | 355 | |
dflet | 0:e89ba455dbcf | 356 | return result; |
dflet | 0:e89ba455dbcf | 357 | } |
dflet | 0:e89ba455dbcf | 358 | |
dflet | 0:e89ba455dbcf | 359 | /*! |
dflet | 0:e89ba455dbcf | 360 | \brief Calculate the file chunk size |
dflet | 0:e89ba455dbcf | 361 | |
dflet | 0:e89ba455dbcf | 362 | \param[in] len - pointer to length of the data in the buffer |
dflet | 0:e89ba455dbcf | 363 | \param[in] p_Buff - pointer to pointer of buffer containing data |
dflet | 0:e89ba455dbcf | 364 | \param[out] chunk_size - pointer to variable containing chunk size |
dflet | 0:e89ba455dbcf | 365 | |
dflet | 0:e89ba455dbcf | 366 | \return 0 for success, -ve for error |
dflet | 0:e89ba455dbcf | 367 | |
dflet | 0:e89ba455dbcf | 368 | */ |
dflet | 0:e89ba455dbcf | 369 | int32_t cc3100::getChunkSize(int32_t *len, uint8_t **p_Buff, uint32_t *chunk_size) |
dflet | 0:e89ba455dbcf | 370 | { |
dflet | 0:e89ba455dbcf | 371 | int32_t idx = -1; |
dflet | 0:e89ba455dbcf | 372 | unsigned char lenBuff[10]; |
dflet | 0:e89ba455dbcf | 373 | |
dflet | 0:e89ba455dbcf | 374 | idx = 0; |
dflet | 0:e89ba455dbcf | 375 | memset(lenBuff, 0, sizeof(lenBuff)); |
dflet | 0:e89ba455dbcf | 376 | while(*len >= 0 && **p_Buff != 13) /* check for <CR> */ |
dflet | 0:e89ba455dbcf | 377 | { |
dflet | 0:e89ba455dbcf | 378 | if(0 == *len) |
dflet | 0:e89ba455dbcf | 379 | { |
dflet | 0:e89ba455dbcf | 380 | memset(g_buff, 0, sizeof(g_buff)); |
dflet | 0:e89ba455dbcf | 381 | *len = _socket.sl_Recv(g_SockID, &g_buff[0], MAX_BUFF_SIZE, 0); |
dflet | 0:e89ba455dbcf | 382 | if(*len <= 0) |
dflet | 0:e89ba455dbcf | 383 | ASSERT_ON_ERROR(TCP_RECV_ERROR); |
dflet | 0:e89ba455dbcf | 384 | |
dflet | 0:e89ba455dbcf | 385 | *p_Buff = g_buff; |
dflet | 0:e89ba455dbcf | 386 | } |
dflet | 0:e89ba455dbcf | 387 | |
dflet | 0:e89ba455dbcf | 388 | lenBuff[idx] = **p_Buff; |
dflet | 0:e89ba455dbcf | 389 | idx++; |
dflet | 0:e89ba455dbcf | 390 | (*p_Buff)++; |
dflet | 0:e89ba455dbcf | 391 | (*len)--; |
dflet | 0:e89ba455dbcf | 392 | } |
dflet | 0:e89ba455dbcf | 393 | |
dflet | 0:e89ba455dbcf | 394 | (*p_Buff) += 2; /* skip <CR><LF> */ |
dflet | 0:e89ba455dbcf | 395 | (*len) -= 2; |
dflet | 0:e89ba455dbcf | 396 | *chunk_size = hexToi(lenBuff); |
dflet | 0:e89ba455dbcf | 397 | |
dflet | 0:e89ba455dbcf | 398 | return SUCCESS; |
dflet | 0:e89ba455dbcf | 399 | } |
dflet | 0:e89ba455dbcf | 400 | |
dflet | 0:e89ba455dbcf | 401 | /*! |
dflet | 0:e89ba455dbcf | 402 | \brief Obtain the file from the server |
dflet | 0:e89ba455dbcf | 403 | |
dflet | 0:e89ba455dbcf | 404 | This function requests the file from the server and save it on serial flash. |
dflet | 0:e89ba455dbcf | 405 | To request a different file for different user needs to modify the |
dflet | 0:e89ba455dbcf | 406 | PREFIX_BUFFER and POST_BUFFER macros. |
dflet | 0:e89ba455dbcf | 407 | |
dflet | 0:e89ba455dbcf | 408 | \param[in] None |
dflet | 0:e89ba455dbcf | 409 | |
dflet | 0:e89ba455dbcf | 410 | \return 0 for success and negative for error |
dflet | 0:e89ba455dbcf | 411 | |
dflet | 0:e89ba455dbcf | 412 | */ |
dflet | 0:e89ba455dbcf | 413 | /* |
dflet | 0:e89ba455dbcf | 414 | int32_t cc3100::getFile() |
dflet | 0:e89ba455dbcf | 415 | { |
dflet | 0:e89ba455dbcf | 416 | uint32_t Token = 0; |
dflet | 0:e89ba455dbcf | 417 | uint32_t recv_size = 0; |
dflet | 0:e89ba455dbcf | 418 | uint8_t *pBuff = 0; |
dflet | 0:e89ba455dbcf | 419 | uint8_t eof_detected = 0; |
dflet | 0:e89ba455dbcf | 420 | uint8_t isChunked = 0; |
dflet | 0:e89ba455dbcf | 421 | |
dflet | 0:e89ba455dbcf | 422 | int32_t transfer_len = -1; |
dflet | 0:e89ba455dbcf | 423 | int32_t retVal = -1; |
dflet | 0:e89ba455dbcf | 424 | int32_t fileHandle = -1; |
dflet | 0:e89ba455dbcf | 425 | |
dflet | 0:e89ba455dbcf | 426 | memset(g_buff, 0, sizeof(g_buff)); |
dflet | 0:e89ba455dbcf | 427 | |
dflet | 0:e89ba455dbcf | 428 | //Puts together the HTTP GET string. |
dflet | 0:e89ba455dbcf | 429 | strcpy((char*)g_buff, PREFIX_BUFFER); |
dflet | 0:e89ba455dbcf | 430 | strcat((char*)g_buff, POST_BUFFER); |
dflet | 0:e89ba455dbcf | 431 | |
dflet | 0:e89ba455dbcf | 432 | //Send the HTTP GET string to the opened TCP/IP socket. |
dflet | 0:e89ba455dbcf | 433 | transfer_len = _socket.sl_Send(g_SockID, g_buff, strlen((const char*)g_buff), 0); |
dflet | 0:e89ba455dbcf | 434 | |
dflet | 0:e89ba455dbcf | 435 | if (transfer_len < 0) |
dflet | 0:e89ba455dbcf | 436 | { |
dflet | 0:e89ba455dbcf | 437 | // error |
dflet | 0:e89ba455dbcf | 438 | printf(" Socket Send Error\r\n"); |
dflet | 0:e89ba455dbcf | 439 | ASSERT_ON_ERROR(TCP_SEND_ERROR); |
dflet | 0:e89ba455dbcf | 440 | } |
dflet | 0:e89ba455dbcf | 441 | |
dflet | 0:e89ba455dbcf | 442 | memset(g_buff, 0, sizeof(g_buff)); |
dflet | 0:e89ba455dbcf | 443 | |
dflet | 0:e89ba455dbcf | 444 | //get the reply from the server in buffer. |
dflet | 0:e89ba455dbcf | 445 | transfer_len = _socket.sl_Recv(g_SockID, &g_buff[0], MAX_BUFF_SIZE, 0); |
dflet | 0:e89ba455dbcf | 446 | |
dflet | 0:e89ba455dbcf | 447 | if(transfer_len <= 0) |
dflet | 0:e89ba455dbcf | 448 | ASSERT_ON_ERROR(TCP_RECV_ERROR); |
dflet | 0:e89ba455dbcf | 449 | |
dflet | 0:e89ba455dbcf | 450 | // Check for 404 return code |
dflet | 0:e89ba455dbcf | 451 | if(strstr((const char*)g_buff, HTTP_FILE_NOT_FOUND) != 0) |
dflet | 0:e89ba455dbcf | 452 | { |
dflet | 0:e89ba455dbcf | 453 | printf(" File not found, check the file and try again\r\n"); |
dflet | 0:e89ba455dbcf | 454 | ASSERT_ON_ERROR(FILE_NOT_FOUND_ERROR); |
dflet | 0:e89ba455dbcf | 455 | } |
dflet | 0:e89ba455dbcf | 456 | |
dflet | 0:e89ba455dbcf | 457 | // if not "200 OK" return error |
dflet | 0:e89ba455dbcf | 458 | if(strstr((const char*)g_buff, HTTP_STATUS_OK) == 0) |
dflet | 0:e89ba455dbcf | 459 | { |
dflet | 0:e89ba455dbcf | 460 | printf(" Error during downloading the file\r\n"); |
dflet | 0:e89ba455dbcf | 461 | ASSERT_ON_ERROR(INVALID_SERVER_RESPONSE); |
dflet | 0:e89ba455dbcf | 462 | } |
dflet | 0:e89ba455dbcf | 463 | |
dflet | 0:e89ba455dbcf | 464 | // check if content length is transferred with headers |
dflet | 0:e89ba455dbcf | 465 | pBuff = (uint8_t *)strstr((const char*)g_buff, HTTP_CONTENT_LENGTH); |
dflet | 0:e89ba455dbcf | 466 | if(pBuff != 0) |
dflet | 0:e89ba455dbcf | 467 | { |
dflet | 0:e89ba455dbcf | 468 | // not supported |
dflet | 0:e89ba455dbcf | 469 | printf(" Server response format is not supported\r\n"); |
dflet | 0:e89ba455dbcf | 470 | ASSERT_ON_ERROR(FORMAT_NOT_SUPPORTED); |
dflet | 0:e89ba455dbcf | 471 | } |
dflet | 0:e89ba455dbcf | 472 | |
dflet | 0:e89ba455dbcf | 473 | // Check if data is chunked |
dflet | 0:e89ba455dbcf | 474 | pBuff = (uint8_t *)strstr((const char*)g_buff, HTTP_TRANSFER_ENCODING); |
dflet | 0:e89ba455dbcf | 475 | if(pBuff != 0) |
dflet | 0:e89ba455dbcf | 476 | { |
dflet | 0:e89ba455dbcf | 477 | pBuff += strlen(HTTP_TRANSFER_ENCODING); |
dflet | 0:e89ba455dbcf | 478 | while(*pBuff == SPACE) |
dflet | 0:e89ba455dbcf | 479 | pBuff++; |
dflet | 0:e89ba455dbcf | 480 | |
dflet | 0:e89ba455dbcf | 481 | if(memcmp(pBuff, HTTP_ENCODING_CHUNKED, strlen(HTTP_ENCODING_CHUNKED)) == 0) |
dflet | 0:e89ba455dbcf | 482 | { |
dflet | 0:e89ba455dbcf | 483 | recv_size = 0; |
dflet | 0:e89ba455dbcf | 484 | isChunked = 1; |
dflet | 0:e89ba455dbcf | 485 | } |
dflet | 0:e89ba455dbcf | 486 | } |
dflet | 0:e89ba455dbcf | 487 | else |
dflet | 0:e89ba455dbcf | 488 | { |
dflet | 0:e89ba455dbcf | 489 | // Check if connection will be closed by after sending data |
dflet | 0:e89ba455dbcf | 490 | // In this method the content length is not received and end of |
dflet | 0:e89ba455dbcf | 491 | // connection marks the end of data |
dflet | 0:e89ba455dbcf | 492 | pBuff = (uint8_t *)strstr((const char*)g_buff, HTTP_CONNECTION); |
dflet | 0:e89ba455dbcf | 493 | if(pBuff != 0) |
dflet | 0:e89ba455dbcf | 494 | { |
dflet | 0:e89ba455dbcf | 495 | pBuff += strlen(HTTP_CONNECTION); |
dflet | 0:e89ba455dbcf | 496 | while(*pBuff == SPACE) |
dflet | 0:e89ba455dbcf | 497 | pBuff++; |
dflet | 0:e89ba455dbcf | 498 | |
dflet | 0:e89ba455dbcf | 499 | if(memcmp(pBuff, HTTP_ENCODING_CHUNKED, strlen(HTTP_CONNECTION_CLOSE)) == 0) |
dflet | 0:e89ba455dbcf | 500 | { |
dflet | 0:e89ba455dbcf | 501 | // not supported |
dflet | 0:e89ba455dbcf | 502 | printf(" Server response format is not supported\r\n"); |
dflet | 0:e89ba455dbcf | 503 | ASSERT_ON_ERROR(FORMAT_NOT_SUPPORTED); |
dflet | 0:e89ba455dbcf | 504 | } |
dflet | 0:e89ba455dbcf | 505 | } |
dflet | 0:e89ba455dbcf | 506 | } |
dflet | 0:e89ba455dbcf | 507 | |
dflet | 0:e89ba455dbcf | 508 | // "\r\n\r\n" marks the end of headers |
dflet | 0:e89ba455dbcf | 509 | pBuff = (uint8_t *)strstr((const char*)g_buff, HTTP_END_OF_HEADER); |
dflet | 0:e89ba455dbcf | 510 | if(pBuff == 0) |
dflet | 0:e89ba455dbcf | 511 | { |
dflet | 0:e89ba455dbcf | 512 | printf(" Invalid response\r\n"); |
dflet | 0:e89ba455dbcf | 513 | ASSERT_ON_ERROR(INVALID_SERVER_RESPONSE); |
dflet | 0:e89ba455dbcf | 514 | } |
dflet | 0:e89ba455dbcf | 515 | // Increment by 4 to skip "\r\n\r\n" |
dflet | 0:e89ba455dbcf | 516 | pBuff += 4; |
dflet | 0:e89ba455dbcf | 517 | |
dflet | 0:e89ba455dbcf | 518 | // Adjust buffer data length for header size |
dflet | 0:e89ba455dbcf | 519 | transfer_len -= (pBuff - g_buff); |
dflet | 0:e89ba455dbcf | 520 | |
dflet | 0:e89ba455dbcf | 521 | // If data in chunked format, calculate the chunk size |
dflet | 0:e89ba455dbcf | 522 | if(isChunked == 1) |
dflet | 0:e89ba455dbcf | 523 | { |
dflet | 0:e89ba455dbcf | 524 | retVal = getChunkSize(&transfer_len, &pBuff, &recv_size); |
dflet | 0:e89ba455dbcf | 525 | if(retVal < 0) |
dflet | 0:e89ba455dbcf | 526 | { |
dflet | 0:e89ba455dbcf | 527 | // Error |
dflet | 0:e89ba455dbcf | 528 | printf(" Problem with connection to server\r\n"); |
dflet | 0:e89ba455dbcf | 529 | return retVal; |
dflet | 0:e89ba455dbcf | 530 | } |
dflet | 0:e89ba455dbcf | 531 | } |
dflet | 0:e89ba455dbcf | 532 | |
dflet | 0:e89ba455dbcf | 533 | // Open file to save the downloaded file |
dflet | 0:e89ba455dbcf | 534 | retVal = _fs.sl_FsOpen((uint8_t *)FILE_NAME, |
dflet | 0:e89ba455dbcf | 535 | FS_MODE_OPEN_WRITE, &Token, &fileHandle); |
dflet | 0:e89ba455dbcf | 536 | if(retVal < 0) |
dflet | 0:e89ba455dbcf | 537 | { |
dflet | 0:e89ba455dbcf | 538 | // File Doesn't exit create a new of 45 KB file |
dflet | 0:e89ba455dbcf | 539 | retVal = _fs.sl_FsOpen((uint8_t *)FILE_NAME, |
dflet | 0:e89ba455dbcf | 540 | _fs.FS_MODE_OPEN_CREATE(SIZE_45K,_FS_FILE_OPEN_FLAG_COMMIT|_FS_FILE_PUBLIC_WRITE), |
dflet | 0:e89ba455dbcf | 541 | &Token, &fileHandle); |
dflet | 0:e89ba455dbcf | 542 | if(retVal < 0) |
dflet | 0:e89ba455dbcf | 543 | { |
dflet | 0:e89ba455dbcf | 544 | printf(" Error during opening the file\r\n"); |
dflet | 0:e89ba455dbcf | 545 | return retVal; |
dflet | 0:e89ba455dbcf | 546 | } |
dflet | 0:e89ba455dbcf | 547 | } |
dflet | 0:e89ba455dbcf | 548 | |
dflet | 0:e89ba455dbcf | 549 | while (0 < transfer_len) |
dflet | 0:e89ba455dbcf | 550 | { |
dflet | 0:e89ba455dbcf | 551 | // For chunked data recv_size contains the chunk size to be received |
dflet | 0:e89ba455dbcf | 552 | // while the transfer_len contains the data in the buffer |
dflet | 0:e89ba455dbcf | 553 | if(recv_size <= transfer_len) |
dflet | 0:e89ba455dbcf | 554 | { |
dflet | 0:e89ba455dbcf | 555 | // write the recv_size |
dflet | 0:e89ba455dbcf | 556 | retVal = _fs.sl_FsWrite(fileHandle, g_BytesReceived, |
dflet | 0:e89ba455dbcf | 557 | (uint8_t *)pBuff, recv_size); |
dflet | 0:e89ba455dbcf | 558 | if(retVal < recv_size) |
dflet | 0:e89ba455dbcf | 559 | { |
dflet | 0:e89ba455dbcf | 560 | // Close file without saving |
dflet | 0:e89ba455dbcf | 561 | retVal = _fs.sl_FsClose(fileHandle, 0, (unsigned char*)"A", 1); |
dflet | 0:e89ba455dbcf | 562 | printf(" Error during writing the file\r\n"); |
dflet | 0:e89ba455dbcf | 563 | return FILE_WRITE_ERROR; |
dflet | 0:e89ba455dbcf | 564 | } |
dflet | 0:e89ba455dbcf | 565 | transfer_len -= recv_size; |
dflet | 0:e89ba455dbcf | 566 | g_BytesReceived +=recv_size; |
dflet | 0:e89ba455dbcf | 567 | pBuff += recv_size; |
dflet | 0:e89ba455dbcf | 568 | recv_size = 0; |
dflet | 0:e89ba455dbcf | 569 | |
dflet | 0:e89ba455dbcf | 570 | if(isChunked == 1) |
dflet | 0:e89ba455dbcf | 571 | { |
dflet | 0:e89ba455dbcf | 572 | // if data in chunked format calculate next chunk size |
dflet | 0:e89ba455dbcf | 573 | pBuff += 2; // 2 bytes for <CR> <LF> |
dflet | 0:e89ba455dbcf | 574 | transfer_len -= 2; |
dflet | 0:e89ba455dbcf | 575 | |
dflet | 0:e89ba455dbcf | 576 | if(getChunkSize(&transfer_len, &pBuff, &recv_size) < 0) |
dflet | 0:e89ba455dbcf | 577 | { |
dflet | 0:e89ba455dbcf | 578 | // Error |
dflet | 0:e89ba455dbcf | 579 | break; |
dflet | 0:e89ba455dbcf | 580 | } |
dflet | 0:e89ba455dbcf | 581 | |
dflet | 0:e89ba455dbcf | 582 | // if next chunk size is zero we have received the complete file |
dflet | 0:e89ba455dbcf | 583 | if(recv_size == 0) |
dflet | 0:e89ba455dbcf | 584 | { |
dflet | 0:e89ba455dbcf | 585 | eof_detected = 1; |
dflet | 0:e89ba455dbcf | 586 | break; |
dflet | 0:e89ba455dbcf | 587 | } |
dflet | 0:e89ba455dbcf | 588 | |
dflet | 0:e89ba455dbcf | 589 | if(recv_size < transfer_len) |
dflet | 0:e89ba455dbcf | 590 | { |
dflet | 0:e89ba455dbcf | 591 | // Code will enter this section if the new chunk size is less then |
dflet | 0:e89ba455dbcf | 592 | // then the transfer size. This will the last chunk of file received |
dflet | 0:e89ba455dbcf | 593 | |
dflet | 0:e89ba455dbcf | 594 | retVal = _fs.sl_FsWrite(fileHandle, g_BytesReceived, |
dflet | 0:e89ba455dbcf | 595 | (uint8_t *)pBuff, recv_size); |
dflet | 0:e89ba455dbcf | 596 | if(retVal < recv_size) |
dflet | 0:e89ba455dbcf | 597 | { |
dflet | 0:e89ba455dbcf | 598 | // Close file without saving |
dflet | 0:e89ba455dbcf | 599 | retVal = _fs.sl_FsClose(fileHandle, 0, (unsigned char*)"A", 1); |
dflet | 0:e89ba455dbcf | 600 | printf(" Error during writing the file\r\n"); |
dflet | 0:e89ba455dbcf | 601 | return FILE_WRITE_ERROR; |
dflet | 0:e89ba455dbcf | 602 | } |
dflet | 0:e89ba455dbcf | 603 | transfer_len -= recv_size; |
dflet | 0:e89ba455dbcf | 604 | g_BytesReceived +=recv_size; |
dflet | 0:e89ba455dbcf | 605 | pBuff += recv_size; |
dflet | 0:e89ba455dbcf | 606 | recv_size = 0; |
dflet | 0:e89ba455dbcf | 607 | |
dflet | 0:e89ba455dbcf | 608 | pBuff += 2; // 2bytes for <CR> <LF> |
dflet | 0:e89ba455dbcf | 609 | transfer_len -= 2; |
dflet | 0:e89ba455dbcf | 610 | |
dflet | 0:e89ba455dbcf | 611 | // Calculate the next chunk size, should be zero |
dflet | 0:e89ba455dbcf | 612 | if(getChunkSize(&transfer_len, &pBuff, &recv_size) < 0) |
dflet | 0:e89ba455dbcf | 613 | { |
dflet | 0:e89ba455dbcf | 614 | // Error |
dflet | 0:e89ba455dbcf | 615 | break; |
dflet | 0:e89ba455dbcf | 616 | } |
dflet | 0:e89ba455dbcf | 617 | |
dflet | 0:e89ba455dbcf | 618 | // if next chunk size is non zero error |
dflet | 0:e89ba455dbcf | 619 | if(recv_size != 0) |
dflet | 0:e89ba455dbcf | 620 | { |
dflet | 0:e89ba455dbcf | 621 | // Error |
dflet | 0:e89ba455dbcf | 622 | break; |
dflet | 0:e89ba455dbcf | 623 | } |
dflet | 0:e89ba455dbcf | 624 | eof_detected = 1; |
dflet | 0:e89ba455dbcf | 625 | break; |
dflet | 0:e89ba455dbcf | 626 | } |
dflet | 0:e89ba455dbcf | 627 | else |
dflet | 0:e89ba455dbcf | 628 | { |
dflet | 0:e89ba455dbcf | 629 | // write data on the file |
dflet | 0:e89ba455dbcf | 630 | retVal = _fs.sl_FsWrite(fileHandle, g_BytesReceived, |
dflet | 0:e89ba455dbcf | 631 | (uint8_t *)pBuff, transfer_len); |
dflet | 0:e89ba455dbcf | 632 | if(retVal < transfer_len) |
dflet | 0:e89ba455dbcf | 633 | { |
dflet | 0:e89ba455dbcf | 634 | // Close file without saving |
dflet | 0:e89ba455dbcf | 635 | retVal = _fs.sl_FsClose(fileHandle, 0, (unsigned char*)"A", 1); |
dflet | 0:e89ba455dbcf | 636 | printf(" Error during writing the file\r\n"); |
dflet | 0:e89ba455dbcf | 637 | ASSERT_ON_ERROR(FILE_WRITE_ERROR); |
dflet | 0:e89ba455dbcf | 638 | } |
dflet | 0:e89ba455dbcf | 639 | recv_size -= transfer_len; |
dflet | 0:e89ba455dbcf | 640 | g_BytesReceived +=transfer_len; |
dflet | 0:e89ba455dbcf | 641 | } |
dflet | 0:e89ba455dbcf | 642 | } |
dflet | 0:e89ba455dbcf | 643 | // complete file received exit |
dflet | 0:e89ba455dbcf | 644 | if(recv_size == 0) |
dflet | 0:e89ba455dbcf | 645 | { |
dflet | 0:e89ba455dbcf | 646 | eof_detected = 1; |
dflet | 0:e89ba455dbcf | 647 | break; |
dflet | 0:e89ba455dbcf | 648 | } |
dflet | 0:e89ba455dbcf | 649 | } |
dflet | 0:e89ba455dbcf | 650 | else |
dflet | 0:e89ba455dbcf | 651 | { |
dflet | 0:e89ba455dbcf | 652 | // write data on the file |
dflet | 0:e89ba455dbcf | 653 | retVal = _fs.sl_FsWrite(fileHandle, g_BytesReceived, |
dflet | 0:e89ba455dbcf | 654 | (uint8_t *)pBuff, transfer_len); |
dflet | 0:e89ba455dbcf | 655 | if (retVal < 0) |
dflet | 0:e89ba455dbcf | 656 | { |
dflet | 0:e89ba455dbcf | 657 | // Close file without saving |
dflet | 0:e89ba455dbcf | 658 | retVal = _fs.sl_FsClose(fileHandle, 0, (unsigned char*)"A", 1); |
dflet | 0:e89ba455dbcf | 659 | printf(" Error during writing the file\r\n"); |
dflet | 0:e89ba455dbcf | 660 | ASSERT_ON_ERROR(FILE_WRITE_ERROR); |
dflet | 0:e89ba455dbcf | 661 | } |
dflet | 0:e89ba455dbcf | 662 | g_BytesReceived +=transfer_len; |
dflet | 0:e89ba455dbcf | 663 | recv_size -= transfer_len; |
dflet | 0:e89ba455dbcf | 664 | } |
dflet | 0:e89ba455dbcf | 665 | |
dflet | 0:e89ba455dbcf | 666 | memset(g_buff, 0, sizeof(g_buff)); |
dflet | 0:e89ba455dbcf | 667 | |
dflet | 0:e89ba455dbcf | 668 | transfer_len = _socket.sl_Recv(g_SockID, &g_buff[0], MAX_BUFF_SIZE, 0); |
dflet | 0:e89ba455dbcf | 669 | if(transfer_len <= 0) |
dflet | 0:e89ba455dbcf | 670 | ASSERT_ON_ERROR(TCP_RECV_ERROR); |
dflet | 0:e89ba455dbcf | 671 | |
dflet | 0:e89ba455dbcf | 672 | pBuff = g_buff; |
dflet | 0:e89ba455dbcf | 673 | } |
dflet | 0:e89ba455dbcf | 674 | |
dflet | 0:e89ba455dbcf | 675 | // If user file has checksum which can be used to verify the temporary |
dflet | 0:e89ba455dbcf | 676 | // file then file should be verified |
dflet | 0:e89ba455dbcf | 677 | // In case of invalid file (FILE_NAME) should be closed without saving to |
dflet | 0:e89ba455dbcf | 678 | // recover the previous version of file |
dflet | 0:e89ba455dbcf | 679 | if(0 > transfer_len || eof_detected == 0) |
dflet | 0:e89ba455dbcf | 680 | { |
dflet | 0:e89ba455dbcf | 681 | // Close file without saving |
dflet | 0:e89ba455dbcf | 682 | retVal = _fs.sl_FsClose(fileHandle, 0, (unsigned char*)"A", 1); |
dflet | 0:e89ba455dbcf | 683 | printf(" Error While File Download\r\n"); |
dflet | 0:e89ba455dbcf | 684 | ASSERT_ON_ERROR(INVALID_FILE); |
dflet | 0:e89ba455dbcf | 685 | } |
dflet | 0:e89ba455dbcf | 686 | else |
dflet | 0:e89ba455dbcf | 687 | { |
dflet | 0:e89ba455dbcf | 688 | // Save and close file |
dflet | 0:e89ba455dbcf | 689 | retVal = _fs.sl_FsClose(fileHandle, 0, 0, 0); |
dflet | 0:e89ba455dbcf | 690 | ASSERT_ON_ERROR(retVal); |
dflet | 0:e89ba455dbcf | 691 | } |
dflet | 0:e89ba455dbcf | 692 | |
dflet | 0:e89ba455dbcf | 693 | return SUCCESS;; |
dflet | 0:e89ba455dbcf | 694 | } |
dflet | 0:e89ba455dbcf | 695 | */ |
dflet | 0:e89ba455dbcf | 696 | |
dflet | 0:e89ba455dbcf | 697 | /*! |
dflet | 0:e89ba455dbcf | 698 | \brief Connecting to a WLAN Access point |
dflet | 0:e89ba455dbcf | 699 | |
dflet | 0:e89ba455dbcf | 700 | This function connects to the required AP (SSID_NAME). |
dflet | 0:e89ba455dbcf | 701 | The function will return once we are connected and have acquired IP address |
dflet | 0:e89ba455dbcf | 702 | |
dflet | 0:e89ba455dbcf | 703 | \param[in] None |
dflet | 0:e89ba455dbcf | 704 | |
dflet | 0:e89ba455dbcf | 705 | \return 0 on success, negative error-code on error |
dflet | 0:e89ba455dbcf | 706 | |
dflet | 0:e89ba455dbcf | 707 | \note |
dflet | 0:e89ba455dbcf | 708 | |
dflet | 0:e89ba455dbcf | 709 | \warning If the WLAN connection fails or we don't acquire an IP address, |
dflet | 0:e89ba455dbcf | 710 | We will be stuck in this function forever. |
dflet | 0:e89ba455dbcf | 711 | */ |
dflet | 0:e89ba455dbcf | 712 | int32_t cc3100::establishConnectionWithAP() |
dflet | 0:e89ba455dbcf | 713 | { |
dflet | 2:b3fd5b3d9860 | 714 | |
dflet | 0:e89ba455dbcf | 715 | SlSecParams_t secParams = {0}; |
dflet | 0:e89ba455dbcf | 716 | int32_t retVal = 0; |
dflet | 0:e89ba455dbcf | 717 | |
dflet | 0:e89ba455dbcf | 718 | secParams.Key = (signed char *)PASSKEY; |
dflet | 0:e89ba455dbcf | 719 | secParams.KeyLen = strlen(PASSKEY); |
dflet | 0:e89ba455dbcf | 720 | secParams.Type = SEC_TYPE; |
dflet | 0:e89ba455dbcf | 721 | |
dflet | 0:e89ba455dbcf | 722 | retVal = _wlan.sl_WlanConnect((signed char *)SSID_NAME, strlen(SSID_NAME), 0, &secParams, 0); |
dflet | 0:e89ba455dbcf | 723 | ASSERT_ON_ERROR(retVal); |
dflet | 0:e89ba455dbcf | 724 | |
dflet | 0:e89ba455dbcf | 725 | /* Wait */ |
dflet | 0:e89ba455dbcf | 726 | while((!IS_CONNECTED(g_Status,STATUS_BIT_CONNECTION)) || (!IS_IP_ACQUIRED(g_Status,STATUS_BIT_IP_ACQUIRED))) { _nonos._SlNonOsMainLoopTask(); } |
dflet | 0:e89ba455dbcf | 727 | return SUCCESS; |
dflet | 0:e89ba455dbcf | 728 | } |
dflet | 0:e89ba455dbcf | 729 | |
dflet | 0:e89ba455dbcf | 730 | /*! |
dflet | 0:e89ba455dbcf | 731 | \brief This function checks the LAN connection by pinging the AP's gateway |
dflet | 0:e89ba455dbcf | 732 | |
dflet | 0:e89ba455dbcf | 733 | \param[in] None |
dflet | 0:e89ba455dbcf | 734 | |
dflet | 0:e89ba455dbcf | 735 | \return 0 on success, negative error-code on error |
dflet | 0:e89ba455dbcf | 736 | */ |
dflet | 0:e89ba455dbcf | 737 | int32_t cc3100::checkLanConnection() |
dflet | 0:e89ba455dbcf | 738 | { |
dflet | 0:e89ba455dbcf | 739 | SlPingStartCommand_t pingParams = {0}; |
dflet | 0:e89ba455dbcf | 740 | SlPingReport_t pingReport = {0}; |
dflet | 0:e89ba455dbcf | 741 | |
dflet | 0:e89ba455dbcf | 742 | int32_t retVal = -1; |
dflet | 0:e89ba455dbcf | 743 | |
dflet | 0:e89ba455dbcf | 744 | CLR_STATUS_BIT(g_Status, STATUS_BIT_PING_DONE); |
dflet | 0:e89ba455dbcf | 745 | g_PingPacketsRecv = 0; |
dflet | 0:e89ba455dbcf | 746 | |
dflet | 0:e89ba455dbcf | 747 | /* Set the ping parameters */ |
dflet | 0:e89ba455dbcf | 748 | pingParams.PingIntervalTime = PING_INTERVAL; |
dflet | 0:e89ba455dbcf | 749 | pingParams.PingSize = PING_PKT_SIZE; |
dflet | 0:e89ba455dbcf | 750 | pingParams.PingRequestTimeout = PING_TIMEOUT; |
dflet | 0:e89ba455dbcf | 751 | pingParams.TotalNumberOfAttempts = PING_ATTEMPTS; |
dflet | 0:e89ba455dbcf | 752 | pingParams.Flags = 0; |
dflet | 0:e89ba455dbcf | 753 | pingParams.Ip = g_GatewayIP; |
dflet | 0:e89ba455dbcf | 754 | |
dflet | 0:e89ba455dbcf | 755 | /* Check for LAN connection */ |
dflet | 0:e89ba455dbcf | 756 | retVal = _netapp.sl_NetAppPingStart( (SlPingStartCommand_t*)&pingParams, SL_AF_INET, |
dflet | 0:e89ba455dbcf | 757 | (SlPingReport_t*)&pingReport, SimpleLinkPingReport); |
dflet | 0:e89ba455dbcf | 758 | ASSERT_ON_ERROR(retVal); |
dflet | 0:e89ba455dbcf | 759 | |
dflet | 0:e89ba455dbcf | 760 | /* Wait */ |
dflet | 3:b89198ac2efe | 761 | while(!IS_PING_DONE(g_Status,STATUS_BIT_PING_DONE)) { _nonos._SlNonOsMainLoopTask(); } |
dflet | 0:e89ba455dbcf | 762 | |
dflet | 0:e89ba455dbcf | 763 | if(0 == g_PingPacketsRecv) |
dflet | 0:e89ba455dbcf | 764 | { |
dflet | 0:e89ba455dbcf | 765 | /* Problem with LAN connection */ |
dflet | 0:e89ba455dbcf | 766 | ASSERT_ON_ERROR(LAN_CONNECTION_FAILED); |
dflet | 0:e89ba455dbcf | 767 | } |
dflet | 0:e89ba455dbcf | 768 | |
dflet | 0:e89ba455dbcf | 769 | /* LAN connection is successful */ |
dflet | 0:e89ba455dbcf | 770 | return SUCCESS; |
dflet | 0:e89ba455dbcf | 771 | } |
dflet | 0:e89ba455dbcf | 772 | |
dflet | 0:e89ba455dbcf | 773 | /*! |
dflet | 0:e89ba455dbcf | 774 | \brief This function checks the internet connection by pinging |
dflet | 0:e89ba455dbcf | 775 | the external-host (HOST_NAME) |
dflet | 0:e89ba455dbcf | 776 | |
dflet | 0:e89ba455dbcf | 777 | \param[in] None |
dflet | 0:e89ba455dbcf | 778 | |
dflet | 0:e89ba455dbcf | 779 | \return 0 on success, negative error-code on error |
dflet | 0:e89ba455dbcf | 780 | */ |
dflet | 0:e89ba455dbcf | 781 | int32_t cc3100::checkInternetConnection() |
dflet | 0:e89ba455dbcf | 782 | { |
dflet | 0:e89ba455dbcf | 783 | SlPingStartCommand_t pingParams = {0}; |
dflet | 0:e89ba455dbcf | 784 | SlPingReport_t pingReport = {0}; |
dflet | 0:e89ba455dbcf | 785 | |
dflet | 0:e89ba455dbcf | 786 | uint32_t ipAddr = 0; |
dflet | 0:e89ba455dbcf | 787 | |
dflet | 0:e89ba455dbcf | 788 | int32_t retVal = -1; |
dflet | 0:e89ba455dbcf | 789 | |
dflet | 0:e89ba455dbcf | 790 | CLR_STATUS_BIT(g_Status, STATUS_BIT_PING_DONE); |
dflet | 0:e89ba455dbcf | 791 | g_PingPacketsRecv = 0; |
dflet | 0:e89ba455dbcf | 792 | |
dflet | 0:e89ba455dbcf | 793 | /* Set the ping parameters */ |
dflet | 0:e89ba455dbcf | 794 | pingParams.PingIntervalTime = PING_INTERVAL; |
dflet | 0:e89ba455dbcf | 795 | pingParams.PingSize = PING_PKT_SIZE; |
dflet | 0:e89ba455dbcf | 796 | pingParams.PingRequestTimeout = PING_TIMEOUT; |
dflet | 0:e89ba455dbcf | 797 | pingParams.TotalNumberOfAttempts = PING_ATTEMPTS; |
dflet | 0:e89ba455dbcf | 798 | pingParams.Flags = 0; |
dflet | 0:e89ba455dbcf | 799 | pingParams.Ip = g_GatewayIP; |
dflet | 0:e89ba455dbcf | 800 | |
dflet | 0:e89ba455dbcf | 801 | /* Check for Internet connection */ |
dflet | 0:e89ba455dbcf | 802 | retVal = _netapp.sl_NetAppDnsGetHostByName((unsigned char *)HOST_NAME, strlen(HOST_NAME), &ipAddr, SL_AF_INET); |
dflet | 0:e89ba455dbcf | 803 | ASSERT_ON_ERROR(retVal); |
dflet | 0:e89ba455dbcf | 804 | |
dflet | 0:e89ba455dbcf | 805 | /* Replace the ping address to match HOST_NAME's IP address */ |
dflet | 0:e89ba455dbcf | 806 | pingParams.Ip = ipAddr; |
dflet | 0:e89ba455dbcf | 807 | |
dflet | 0:e89ba455dbcf | 808 | /* Try to ping HOST_NAME */ |
dflet | 0:e89ba455dbcf | 809 | retVal = _netapp.sl_NetAppPingStart( (SlPingStartCommand_t*)&pingParams, SL_AF_INET, |
dflet | 0:e89ba455dbcf | 810 | (SlPingReport_t*)&pingReport, SimpleLinkPingReport); |
dflet | 0:e89ba455dbcf | 811 | ASSERT_ON_ERROR(retVal); |
dflet | 0:e89ba455dbcf | 812 | |
dflet | 0:e89ba455dbcf | 813 | /* Wait */ |
dflet | 3:b89198ac2efe | 814 | while(!IS_PING_DONE(g_Status,STATUS_BIT_PING_DONE)) { _nonos._SlNonOsMainLoopTask(); } |
dflet | 0:e89ba455dbcf | 815 | |
dflet | 0:e89ba455dbcf | 816 | if (0 == g_PingPacketsRecv) |
dflet | 0:e89ba455dbcf | 817 | { |
dflet | 0:e89ba455dbcf | 818 | /* Problem with internet connection*/ |
dflet | 0:e89ba455dbcf | 819 | ASSERT_ON_ERROR(INTERNET_CONNECTION_FAILED); |
dflet | 0:e89ba455dbcf | 820 | } |
dflet | 0:e89ba455dbcf | 821 | |
dflet | 0:e89ba455dbcf | 822 | /* Internet connection is successful */ |
dflet | 0:e89ba455dbcf | 823 | return SUCCESS; |
dflet | 0:e89ba455dbcf | 824 | } |
dflet | 0:e89ba455dbcf | 825 | |
dflet | 0:e89ba455dbcf | 826 | /*****************************************************************************/ |
dflet | 0:e89ba455dbcf | 827 | /* Internal functions */ |
dflet | 0:e89ba455dbcf | 828 | /*****************************************************************************/ |
dflet | 0:e89ba455dbcf | 829 | |
dflet | 0:e89ba455dbcf | 830 | int16_t cc3100::_sl_GetStartResponseConvert(uint32_t Status) |
dflet | 0:e89ba455dbcf | 831 | { |
dflet | 0:e89ba455dbcf | 832 | |
dflet | 0:e89ba455dbcf | 833 | switch(Status) { |
dflet | 0:e89ba455dbcf | 834 | case INIT_STA_OK: |
dflet | 0:e89ba455dbcf | 835 | return ROLE_STA; |
dflet | 0:e89ba455dbcf | 836 | case INIT_STA_ERR: |
dflet | 0:e89ba455dbcf | 837 | return ROLE_STA_ERR; |
dflet | 0:e89ba455dbcf | 838 | case INIT_AP_OK: |
dflet | 0:e89ba455dbcf | 839 | return ROLE_AP; |
dflet | 0:e89ba455dbcf | 840 | case INIT_AP_ERR: |
dflet | 0:e89ba455dbcf | 841 | return ROLE_AP_ERR; |
dflet | 0:e89ba455dbcf | 842 | case INIT_P2P_OK: |
dflet | 0:e89ba455dbcf | 843 | return ROLE_P2P; |
dflet | 0:e89ba455dbcf | 844 | case INIT_P2P_ERR: |
dflet | 0:e89ba455dbcf | 845 | return ROLE_P2P_ERR; |
dflet | 0:e89ba455dbcf | 846 | default: |
dflet | 0:e89ba455dbcf | 847 | return (int16_t)Status; |
dflet | 0:e89ba455dbcf | 848 | } |
dflet | 0:e89ba455dbcf | 849 | } |
dflet | 0:e89ba455dbcf | 850 | |
dflet | 0:e89ba455dbcf | 851 | /*****************************************************************************/ |
dflet | 0:e89ba455dbcf | 852 | /* API Functions */ |
dflet | 0:e89ba455dbcf | 853 | /*****************************************************************************/ |
dflet | 0:e89ba455dbcf | 854 | |
dflet | 0:e89ba455dbcf | 855 | bool cc3100::IS_PING_DONE(uint32_t status_variable,const uint32_t bit){ |
dflet | 0:e89ba455dbcf | 856 | |
dflet | 0:e89ba455dbcf | 857 | g_Status = status_variable; |
dflet | 0:e89ba455dbcf | 858 | |
dflet | 0:e89ba455dbcf | 859 | if(0 != (g_Status & ((uint32_t)1L<<(bit)))){ |
dflet | 0:e89ba455dbcf | 860 | return TRUE; |
dflet | 0:e89ba455dbcf | 861 | }else{ |
dflet | 0:e89ba455dbcf | 862 | return FALSE; |
dflet | 0:e89ba455dbcf | 863 | } |
dflet | 0:e89ba455dbcf | 864 | } |
dflet | 0:e89ba455dbcf | 865 | |
dflet | 0:e89ba455dbcf | 866 | bool cc3100::IS_CONNECTED(uint32_t status_variable,const uint32_t bit){ |
dflet | 0:e89ba455dbcf | 867 | |
dflet | 0:e89ba455dbcf | 868 | g_Status = status_variable; |
dflet | 0:e89ba455dbcf | 869 | |
dflet | 0:e89ba455dbcf | 870 | if(0 != (g_Status & ((uint32_t)1L<<(bit)))){ |
dflet | 0:e89ba455dbcf | 871 | return TRUE; |
dflet | 0:e89ba455dbcf | 872 | }else{ |
dflet | 0:e89ba455dbcf | 873 | return FALSE; |
dflet | 0:e89ba455dbcf | 874 | } |
dflet | 0:e89ba455dbcf | 875 | } |
dflet | 0:e89ba455dbcf | 876 | |
dflet | 0:e89ba455dbcf | 877 | bool cc3100::IS_STA_CONNECTED(uint32_t status_variable,const uint32_t bit){ |
dflet | 0:e89ba455dbcf | 878 | |
dflet | 0:e89ba455dbcf | 879 | g_Status = status_variable; |
dflet | 0:e89ba455dbcf | 880 | |
dflet | 0:e89ba455dbcf | 881 | if(0 != (g_Status & ((uint32_t)1L<<(bit)))){ |
dflet | 0:e89ba455dbcf | 882 | return TRUE; |
dflet | 0:e89ba455dbcf | 883 | }else{ |
dflet | 0:e89ba455dbcf | 884 | return FALSE; |
dflet | 0:e89ba455dbcf | 885 | } |
dflet | 0:e89ba455dbcf | 886 | } |
dflet | 0:e89ba455dbcf | 887 | |
dflet | 0:e89ba455dbcf | 888 | bool cc3100::IS_IP_ACQUIRED(uint32_t status_variable,const uint32_t bit){ |
dflet | 0:e89ba455dbcf | 889 | |
dflet | 0:e89ba455dbcf | 890 | g_Status = status_variable; |
dflet | 0:e89ba455dbcf | 891 | |
dflet | 0:e89ba455dbcf | 892 | if(0 != (g_Status & ((uint32_t)1L<<(bit)))){ |
dflet | 0:e89ba455dbcf | 893 | return TRUE; |
dflet | 0:e89ba455dbcf | 894 | }else{ |
dflet | 0:e89ba455dbcf | 895 | return FALSE; |
dflet | 0:e89ba455dbcf | 896 | } |
dflet | 0:e89ba455dbcf | 897 | } |
dflet | 0:e89ba455dbcf | 898 | |
dflet | 0:e89ba455dbcf | 899 | bool cc3100::IS_IP_LEASED(uint32_t status_variable,const uint32_t bit){ |
dflet | 0:e89ba455dbcf | 900 | |
dflet | 0:e89ba455dbcf | 901 | g_Status = status_variable; |
dflet | 0:e89ba455dbcf | 902 | |
dflet | 0:e89ba455dbcf | 903 | if(0 != (g_Status & ((uint32_t)1L<<(bit)))){ |
dflet | 0:e89ba455dbcf | 904 | return TRUE; |
dflet | 0:e89ba455dbcf | 905 | }else{ |
dflet | 0:e89ba455dbcf | 906 | return FALSE; |
dflet | 0:e89ba455dbcf | 907 | } |
dflet | 0:e89ba455dbcf | 908 | } |
dflet | 0:e89ba455dbcf | 909 | |
dflet | 0:e89ba455dbcf | 910 | bool cc3100::IS_CONNECTION_FAILED(uint32_t status_variable,const uint32_t bit){ |
dflet | 0:e89ba455dbcf | 911 | |
dflet | 0:e89ba455dbcf | 912 | g_Status = status_variable; |
dflet | 0:e89ba455dbcf | 913 | |
dflet | 0:e89ba455dbcf | 914 | if(0 != (g_Status & ((uint32_t)1L<<(bit)))){ |
dflet | 0:e89ba455dbcf | 915 | return TRUE; |
dflet | 0:e89ba455dbcf | 916 | }else{ |
dflet | 0:e89ba455dbcf | 917 | return FALSE; |
dflet | 0:e89ba455dbcf | 918 | } |
dflet | 0:e89ba455dbcf | 919 | } |
dflet | 0:e89ba455dbcf | 920 | |
dflet | 0:e89ba455dbcf | 921 | bool cc3100::IS_P2P_NEG_REQ_RECEIVED(uint32_t status_variable,const uint32_t bit){ |
dflet | 0:e89ba455dbcf | 922 | |
dflet | 0:e89ba455dbcf | 923 | g_Status = status_variable; |
dflet | 0:e89ba455dbcf | 924 | |
dflet | 0:e89ba455dbcf | 925 | if(0 != (g_Status & ((uint32_t)1L<<(bit)))){ |
dflet | 0:e89ba455dbcf | 926 | return TRUE; |
dflet | 0:e89ba455dbcf | 927 | }else{ |
dflet | 0:e89ba455dbcf | 928 | return FALSE; |
dflet | 0:e89ba455dbcf | 929 | } |
dflet | 0:e89ba455dbcf | 930 | } |
dflet | 0:e89ba455dbcf | 931 | |
dflet | 0:e89ba455dbcf | 932 | bool cc3100::IS_SMARTCONFIG_DONE(uint32_t status_variable,const uint32_t bit){ |
dflet | 0:e89ba455dbcf | 933 | |
dflet | 0:e89ba455dbcf | 934 | g_Status = status_variable; |
dflet | 0:e89ba455dbcf | 935 | |
dflet | 0:e89ba455dbcf | 936 | if(0 != (g_Status & ((uint32_t)1L<<(bit)))){ |
dflet | 0:e89ba455dbcf | 937 | return TRUE; |
dflet | 0:e89ba455dbcf | 938 | }else{ |
dflet | 0:e89ba455dbcf | 939 | return FALSE; |
dflet | 0:e89ba455dbcf | 940 | } |
dflet | 0:e89ba455dbcf | 941 | } |
dflet | 0:e89ba455dbcf | 942 | |
dflet | 0:e89ba455dbcf | 943 | bool cc3100::IS_SMARTCONFIG_STOPPED(uint32_t status_variable,const uint32_t bit){ |
dflet | 0:e89ba455dbcf | 944 | |
dflet | 0:e89ba455dbcf | 945 | g_Status = status_variable; |
dflet | 0:e89ba455dbcf | 946 | |
dflet | 0:e89ba455dbcf | 947 | if(0 != (g_Status & ((uint32_t)1L<<(bit)))){ |
dflet | 0:e89ba455dbcf | 948 | return TRUE; |
dflet | 0:e89ba455dbcf | 949 | }else{ |
dflet | 0:e89ba455dbcf | 950 | return FALSE; |
dflet | 0:e89ba455dbcf | 951 | } |
dflet | 0:e89ba455dbcf | 952 | } |
dflet | 0:e89ba455dbcf | 953 | |
dflet | 0:e89ba455dbcf | 954 | void cc3100::CLR_STATUS_BIT(uint32_t status_variable, const uint32_t bit){ |
dflet | 0:e89ba455dbcf | 955 | |
dflet | 0:e89ba455dbcf | 956 | g_Status = status_variable; |
dflet | 3:b89198ac2efe | 957 | g_Status &= ~((uint32_t)1L<<(bit)); |
dflet | 3:b89198ac2efe | 958 | |
dflet | 0:e89ba455dbcf | 959 | } |
dflet | 0:e89ba455dbcf | 960 | |
dflet | 0:e89ba455dbcf | 961 | void cc3100::SET_STATUS_BIT(uint32_t status_variable, const uint32_t bit){ |
dflet | 0:e89ba455dbcf | 962 | |
dflet | 0:e89ba455dbcf | 963 | g_Status = status_variable; |
dflet | 3:b89198ac2efe | 964 | g_Status |= ((uint32_t)1L<<(bit)); |
dflet | 3:b89198ac2efe | 965 | |
dflet | 0:e89ba455dbcf | 966 | } |
dflet | 0:e89ba455dbcf | 967 | |
dflet | 0:e89ba455dbcf | 968 | /*****************************************************************************/ |
dflet | 0:e89ba455dbcf | 969 | /* sl_Task */ |
dflet | 0:e89ba455dbcf | 970 | /*****************************************************************************/ |
dflet | 0:e89ba455dbcf | 971 | #if _SL_INCLUDE_FUNC(sl_Task) |
dflet | 0:e89ba455dbcf | 972 | void cc3100::sl_Task(void) |
dflet | 0:e89ba455dbcf | 973 | { |
dflet | 0:e89ba455dbcf | 974 | #ifdef _SlTaskEntry |
dflet | 0:e89ba455dbcf | 975 | _nonos._SlNonOsMainLoopTask; |
dflet | 0:e89ba455dbcf | 976 | |
dflet | 0:e89ba455dbcf | 977 | #endif |
dflet | 0:e89ba455dbcf | 978 | } |
dflet | 0:e89ba455dbcf | 979 | #endif |
dflet | 0:e89ba455dbcf | 980 | |
dflet | 0:e89ba455dbcf | 981 | /*****************************************************************************/ |
dflet | 0:e89ba455dbcf | 982 | /* sl_Start */ |
dflet | 0:e89ba455dbcf | 983 | /*****************************************************************************/ |
dflet | 0:e89ba455dbcf | 984 | #if _SL_INCLUDE_FUNC(sl_Start) |
dflet | 0:e89ba455dbcf | 985 | int16_t cc3100::sl_Start(const void* pIfHdl, int8_t* pDevName, const P_INIT_CALLBACK pInitCallBack) |
dflet | 0:e89ba455dbcf | 986 | { |
dflet | 0:e89ba455dbcf | 987 | int16_t ObjIdx = MAX_CONCURRENT_ACTIONS; |
dflet | 0:e89ba455dbcf | 988 | InitComplete_t AsyncRsp; |
dflet | 0:e89ba455dbcf | 989 | |
dflet | 0:e89ba455dbcf | 990 | /* Perform any preprocessing before enable networking services */ |
dflet | 3:b89198ac2efe | 991 | sl_DeviceEnablePreamble();//stub only |
dflet | 0:e89ba455dbcf | 992 | |
dflet | 0:e89ba455dbcf | 993 | /* ControlBlock init */ |
dflet | 0:e89ba455dbcf | 994 | _driver._SlDrvDriverCBInit(); |
dflet | 0:e89ba455dbcf | 995 | |
dflet | 0:e89ba455dbcf | 996 | /* open the interface: usually SPI or UART */ |
dflet | 0:e89ba455dbcf | 997 | if (NULL == pIfHdl) |
dflet | 0:e89ba455dbcf | 998 | { |
dflet | 0:e89ba455dbcf | 999 | g_pCB->FD = _spi.spi_Open((int8_t *)pDevName, 0); |
dflet | 0:e89ba455dbcf | 1000 | } |
dflet | 0:e89ba455dbcf | 1001 | else |
dflet | 0:e89ba455dbcf | 1002 | { |
dflet | 0:e89ba455dbcf | 1003 | g_pCB->FD = (_SlFd_t)pIfHdl; |
dflet | 0:e89ba455dbcf | 1004 | } |
dflet | 0:e89ba455dbcf | 1005 | /* Use Obj to issue the command, if not available try later */ |
dflet | 0:e89ba455dbcf | 1006 | ObjIdx = _driver._SlDrvWaitForPoolObj(START_STOP_ID,SL_MAX_SOCKETS); |
dflet | 0:e89ba455dbcf | 1007 | if (MAX_CONCURRENT_ACTIONS == ObjIdx) |
dflet | 0:e89ba455dbcf | 1008 | { |
dflet | 0:e89ba455dbcf | 1009 | printf("SL_POOL_IS_EMPTY\r\n"); |
dflet | 0:e89ba455dbcf | 1010 | return SL_POOL_IS_EMPTY; |
dflet | 0:e89ba455dbcf | 1011 | } |
dflet | 3:b89198ac2efe | 1012 | |
dflet | 0:e89ba455dbcf | 1013 | OSI_RET_OK_CHECK(_nonos.sl_LockObjLock(&g_pCB->ProtectionLockObj, NON_OS_LOCK_OBJ_UNLOCK_VALUE, NON_OS_LOCK_OBJ_LOCK_VALUE, SL_OS_WAIT_FOREVER)); |
dflet | 0:e89ba455dbcf | 1014 | g_pCB->ObjPool[ObjIdx].pRespArgs = (uint8_t *)&AsyncRsp; |
dflet | 0:e89ba455dbcf | 1015 | OSI_RET_OK_CHECK(_nonos.sl_LockObjUnlock(&g_pCB->ProtectionLockObj, NON_OS_LOCK_OBJ_UNLOCK_VALUE)); |
dflet | 0:e89ba455dbcf | 1016 | |
dflet | 0:e89ba455dbcf | 1017 | if( g_pCB->FD >= 0) { |
dflet | 0:e89ba455dbcf | 1018 | _spi.CC3100_disable(); |
dflet | 0:e89ba455dbcf | 1019 | |
dflet | 0:e89ba455dbcf | 1020 | if(NULL != pInitCallBack) { |
dflet | 0:e89ba455dbcf | 1021 | g_pCB->pInitCallback = pInitCallBack; |
dflet | 0:e89ba455dbcf | 1022 | } |
dflet | 0:e89ba455dbcf | 1023 | _spi.CC3100_enable(); |
dflet | 0:e89ba455dbcf | 1024 | |
dflet | 0:e89ba455dbcf | 1025 | if (NULL == pInitCallBack) { |
dflet | 0:e89ba455dbcf | 1026 | |
dflet | 0:e89ba455dbcf | 1027 | OSI_RET_OK_CHECK(_nonos.sl_SyncObjWait(&g_pCB->ObjPool[ObjIdx].SyncObj, NON_OS_SYNC_OBJ_SIGNAL_VALUE, NON_OS_SYNC_OBJ_CLEAR_VALUE, SL_OS_WAIT_FOREVER)); |
dflet | 0:e89ba455dbcf | 1028 | /*release Pool Object*/ |
dflet | 0:e89ba455dbcf | 1029 | _driver._SlDrvReleasePoolObj(g_pCB->FunctionParams.AsyncExt.ActionIndex); |
dflet | 0:e89ba455dbcf | 1030 | return _sl_GetStartResponseConvert(AsyncRsp.Status); |
dflet | 0:e89ba455dbcf | 1031 | } |
dflet | 0:e89ba455dbcf | 1032 | } |
dflet | 0:e89ba455dbcf | 1033 | |
dflet | 0:e89ba455dbcf | 1034 | return (int16_t)g_pCB->FD; |
dflet | 0:e89ba455dbcf | 1035 | |
dflet | 0:e89ba455dbcf | 1036 | } |
dflet | 0:e89ba455dbcf | 1037 | #endif |
dflet | 0:e89ba455dbcf | 1038 | |
dflet | 0:e89ba455dbcf | 1039 | /*************************************************************************** |
dflet | 0:e89ba455dbcf | 1040 | _sl_HandleAsync_InitComplete - handles init complete signalling to |
dflet | 0:e89ba455dbcf | 1041 | a waiting object |
dflet | 0:e89ba455dbcf | 1042 | ****************************************************************************/ |
dflet | 0:e89ba455dbcf | 1043 | void cc3100::_sl_HandleAsync_InitComplete(void *pVoidBuf) |
dflet | 0:e89ba455dbcf | 1044 | { |
dflet | 0:e89ba455dbcf | 1045 | |
dflet | 0:e89ba455dbcf | 1046 | InitComplete_t *pMsgArgs = (InitComplete_t *)_SL_RESP_ARGS_START(pVoidBuf); |
dflet | 0:e89ba455dbcf | 1047 | |
dflet | 0:e89ba455dbcf | 1048 | OSI_RET_OK_CHECK(_nonos.sl_LockObjLock(&g_pCB->ProtectionLockObj, NON_OS_LOCK_OBJ_UNLOCK_VALUE, NON_OS_LOCK_OBJ_LOCK_VALUE, SL_OS_WAIT_FOREVER)); |
dflet | 0:e89ba455dbcf | 1049 | |
dflet | 0:e89ba455dbcf | 1050 | if(g_pCB->pInitCallback) { |
dflet | 0:e89ba455dbcf | 1051 | g_pCB->pInitCallback(_sl_GetStartResponseConvert(pMsgArgs->Status)); |
dflet | 0:e89ba455dbcf | 1052 | } else { |
dflet | 0:e89ba455dbcf | 1053 | memcpy(g_pCB->ObjPool[g_pCB->FunctionParams.AsyncExt.ActionIndex].pRespArgs, pMsgArgs, sizeof(InitComplete_t)); |
dflet | 0:e89ba455dbcf | 1054 | OSI_RET_OK_CHECK(_nonos.sl_SyncObjSignal(&g_pCB->ObjPool[g_pCB->FunctionParams.AsyncExt.ActionIndex].SyncObj, NON_OS_SYNC_OBJ_SIGNAL_VALUE)); |
dflet | 0:e89ba455dbcf | 1055 | } |
dflet | 0:e89ba455dbcf | 1056 | OSI_RET_OK_CHECK(_nonos.sl_LockObjUnlock(&g_pCB->ProtectionLockObj, NON_OS_LOCK_OBJ_UNLOCK_VALUE)); |
dflet | 0:e89ba455dbcf | 1057 | |
dflet | 0:e89ba455dbcf | 1058 | if(g_pCB->pInitCallback) { |
dflet | 0:e89ba455dbcf | 1059 | _driver._SlDrvReleasePoolObj(g_pCB->FunctionParams.AsyncExt.ActionIndex); |
dflet | 0:e89ba455dbcf | 1060 | } |
dflet | 0:e89ba455dbcf | 1061 | |
dflet | 0:e89ba455dbcf | 1062 | } |
dflet | 0:e89ba455dbcf | 1063 | |
dflet | 0:e89ba455dbcf | 1064 | /***************************************************************************** |
dflet | 0:e89ba455dbcf | 1065 | sl_stop |
dflet | 0:e89ba455dbcf | 1066 | ******************************************************************************/ |
dflet | 0:e89ba455dbcf | 1067 | typedef union { |
dflet | 0:e89ba455dbcf | 1068 | _DevStopCommand_t Cmd; |
dflet | 0:e89ba455dbcf | 1069 | _BasicResponse_t Rsp; |
dflet | 0:e89ba455dbcf | 1070 | } _SlStopMsg_u; |
dflet | 0:e89ba455dbcf | 1071 | |
dflet | 0:e89ba455dbcf | 1072 | const _SlCmdCtrl_t _SlStopCmdCtrl = { |
dflet | 0:e89ba455dbcf | 1073 | SL_OPCODE_DEVICE_STOP_COMMAND, |
dflet | 0:e89ba455dbcf | 1074 | sizeof(_DevStopCommand_t), |
dflet | 0:e89ba455dbcf | 1075 | sizeof(_BasicResponse_t) |
dflet | 0:e89ba455dbcf | 1076 | }; |
dflet | 0:e89ba455dbcf | 1077 | |
dflet | 0:e89ba455dbcf | 1078 | #if _SL_INCLUDE_FUNC(sl_Stop) |
dflet | 0:e89ba455dbcf | 1079 | int16_t cc3100::sl_Stop(uint16_t timeout) |
dflet | 0:e89ba455dbcf | 1080 | { |
dflet | 0:e89ba455dbcf | 1081 | int16_t RetVal=0; |
dflet | 0:e89ba455dbcf | 1082 | _SlStopMsg_u Msg; |
dflet | 0:e89ba455dbcf | 1083 | _BasicResponse_t AsyncRsp; |
dflet | 0:e89ba455dbcf | 1084 | int16_t ObjIdx = MAX_CONCURRENT_ACTIONS; |
dflet | 0:e89ba455dbcf | 1085 | /* if timeout is 0 the shutdown is forced immediately */ |
dflet | 0:e89ba455dbcf | 1086 | if( 0 == timeout ) { |
dflet | 0:e89ba455dbcf | 1087 | _spi.registerInterruptHandler(NULL, NULL); |
dflet | 0:e89ba455dbcf | 1088 | _spi.CC3100_disable(); |
dflet | 0:e89ba455dbcf | 1089 | RetVal = _spi.spi_Close(g_pCB->FD); |
dflet | 0:e89ba455dbcf | 1090 | |
dflet | 0:e89ba455dbcf | 1091 | } else { |
dflet | 0:e89ba455dbcf | 1092 | /* let the device make the shutdown using the defined timeout */ |
dflet | 0:e89ba455dbcf | 1093 | Msg.Cmd.Timeout = timeout; |
dflet | 0:e89ba455dbcf | 1094 | /* Use Obj to issue the command, if not available try later */ |
dflet | 0:e89ba455dbcf | 1095 | ObjIdx = _driver._SlDrvWaitForPoolObj(START_STOP_ID,SL_MAX_SOCKETS); |
dflet | 0:e89ba455dbcf | 1096 | if (MAX_CONCURRENT_ACTIONS == ObjIdx) { |
dflet | 0:e89ba455dbcf | 1097 | return SL_POOL_IS_EMPTY; |
dflet | 0:e89ba455dbcf | 1098 | } |
dflet | 0:e89ba455dbcf | 1099 | OSI_RET_OK_CHECK(_nonos.sl_LockObjLock(&g_pCB->ProtectionLockObj, NON_OS_LOCK_OBJ_UNLOCK_VALUE, NON_OS_LOCK_OBJ_LOCK_VALUE, SL_OS_WAIT_FOREVER)); |
dflet | 0:e89ba455dbcf | 1100 | |
dflet | 0:e89ba455dbcf | 1101 | g_pCB->ObjPool[ObjIdx].pRespArgs = (uint8_t *)&AsyncRsp; |
dflet | 0:e89ba455dbcf | 1102 | |
dflet | 0:e89ba455dbcf | 1103 | OSI_RET_OK_CHECK(_nonos.sl_LockObjUnlock(&g_pCB->ProtectionLockObj, NON_OS_LOCK_OBJ_UNLOCK_VALUE)); |
dflet | 0:e89ba455dbcf | 1104 | |
dflet | 0:e89ba455dbcf | 1105 | VERIFY_RET_OK(_driver._SlDrvCmdOp((_SlCmdCtrl_t *)&_SlStopCmdCtrl, &Msg, NULL)); |
dflet | 0:e89ba455dbcf | 1106 | |
dflet | 0:e89ba455dbcf | 1107 | if(SL_OS_RET_CODE_OK == (int16_t)Msg.Rsp.status) { |
dflet | 0:e89ba455dbcf | 1108 | OSI_RET_OK_CHECK(_nonos.sl_SyncObjWait(&g_pCB->ObjPool[ObjIdx].SyncObj, NON_OS_SYNC_OBJ_SIGNAL_VALUE, NON_OS_SYNC_OBJ_CLEAR_VALUE, SL_OS_WAIT_FOREVER)); |
dflet | 0:e89ba455dbcf | 1109 | Msg.Rsp.status = AsyncRsp.status; |
dflet | 0:e89ba455dbcf | 1110 | RetVal = Msg.Rsp.status; |
dflet | 0:e89ba455dbcf | 1111 | } |
dflet | 0:e89ba455dbcf | 1112 | |
dflet | 0:e89ba455dbcf | 1113 | _driver._SlDrvReleasePoolObj((uint8_t)ObjIdx); |
dflet | 0:e89ba455dbcf | 1114 | |
dflet | 0:e89ba455dbcf | 1115 | _spi.registerInterruptHandler(NULL, NULL); |
dflet | 0:e89ba455dbcf | 1116 | _spi.CC3100_disable(); |
dflet | 0:e89ba455dbcf | 1117 | _spi.spi_Close(g_pCB->FD); |
dflet | 0:e89ba455dbcf | 1118 | } |
dflet | 0:e89ba455dbcf | 1119 | _driver._SlDrvDriverCBDeinit(); |
dflet | 0:e89ba455dbcf | 1120 | |
dflet | 0:e89ba455dbcf | 1121 | return RetVal; |
dflet | 0:e89ba455dbcf | 1122 | } |
dflet | 0:e89ba455dbcf | 1123 | #endif |
dflet | 0:e89ba455dbcf | 1124 | |
dflet | 0:e89ba455dbcf | 1125 | |
dflet | 0:e89ba455dbcf | 1126 | /***************************************************************************** |
dflet | 0:e89ba455dbcf | 1127 | sl_EventMaskSet |
dflet | 0:e89ba455dbcf | 1128 | *****************************************************************************/ |
dflet | 0:e89ba455dbcf | 1129 | typedef union { |
dflet | 0:e89ba455dbcf | 1130 | _DevMaskEventSetCommand_t Cmd; |
dflet | 0:e89ba455dbcf | 1131 | _BasicResponse_t Rsp; |
dflet | 0:e89ba455dbcf | 1132 | } _SlEventMaskSetMsg_u; |
dflet | 0:e89ba455dbcf | 1133 | |
dflet | 0:e89ba455dbcf | 1134 | const _SlCmdCtrl_t _SlEventMaskSetCmdCtrl = { |
dflet | 0:e89ba455dbcf | 1135 | SL_OPCODE_DEVICE_EVENTMASKSET, |
dflet | 0:e89ba455dbcf | 1136 | sizeof(_DevMaskEventSetCommand_t), |
dflet | 0:e89ba455dbcf | 1137 | sizeof(_BasicResponse_t) |
dflet | 0:e89ba455dbcf | 1138 | }; |
dflet | 0:e89ba455dbcf | 1139 | |
dflet | 0:e89ba455dbcf | 1140 | #if _SL_INCLUDE_FUNC(sl_EventMaskSet) |
dflet | 0:e89ba455dbcf | 1141 | int16_t cc3100::sl_EventMaskSet(uint8_t EventClass , uint32_t Mask) |
dflet | 0:e89ba455dbcf | 1142 | { |
dflet | 0:e89ba455dbcf | 1143 | _SlEventMaskSetMsg_u Msg; |
dflet | 0:e89ba455dbcf | 1144 | Msg.Cmd.group = EventClass; |
dflet | 0:e89ba455dbcf | 1145 | Msg.Cmd.mask = Mask; |
dflet | 0:e89ba455dbcf | 1146 | |
dflet | 0:e89ba455dbcf | 1147 | VERIFY_RET_OK(_driver._SlDrvCmdOp((_SlCmdCtrl_t *)&_SlEventMaskSetCmdCtrl, &Msg, NULL)); |
dflet | 0:e89ba455dbcf | 1148 | |
dflet | 0:e89ba455dbcf | 1149 | return (int16_t)Msg.Rsp.status; |
dflet | 0:e89ba455dbcf | 1150 | } |
dflet | 0:e89ba455dbcf | 1151 | #endif |
dflet | 0:e89ba455dbcf | 1152 | |
dflet | 0:e89ba455dbcf | 1153 | /****************************************************************************** |
dflet | 0:e89ba455dbcf | 1154 | sl_EventMaskGet |
dflet | 0:e89ba455dbcf | 1155 | ******************************************************************************/ |
dflet | 0:e89ba455dbcf | 1156 | typedef union { |
dflet | 0:e89ba455dbcf | 1157 | _DevMaskEventGetCommand_t Cmd; |
dflet | 0:e89ba455dbcf | 1158 | _DevMaskEventGetResponse_t Rsp; |
dflet | 0:e89ba455dbcf | 1159 | } _SlEventMaskGetMsg_u; |
dflet | 0:e89ba455dbcf | 1160 | |
dflet | 0:e89ba455dbcf | 1161 | const _SlCmdCtrl_t _SlEventMaskGetCmdCtrl = { |
dflet | 0:e89ba455dbcf | 1162 | SL_OPCODE_DEVICE_EVENTMASKGET, |
dflet | 0:e89ba455dbcf | 1163 | sizeof(_DevMaskEventGetCommand_t), |
dflet | 0:e89ba455dbcf | 1164 | sizeof(_DevMaskEventGetResponse_t) |
dflet | 0:e89ba455dbcf | 1165 | }; |
dflet | 0:e89ba455dbcf | 1166 | |
dflet | 0:e89ba455dbcf | 1167 | #if _SL_INCLUDE_FUNC(sl_EventMaskGet) |
dflet | 0:e89ba455dbcf | 1168 | int16_t cc3100::sl_EventMaskGet(uint8_t EventClass, uint32_t *pMask) |
dflet | 0:e89ba455dbcf | 1169 | { |
dflet | 0:e89ba455dbcf | 1170 | _SlEventMaskGetMsg_u Msg; |
dflet | 0:e89ba455dbcf | 1171 | |
dflet | 0:e89ba455dbcf | 1172 | Msg.Cmd.group = EventClass; |
dflet | 0:e89ba455dbcf | 1173 | |
dflet | 0:e89ba455dbcf | 1174 | VERIFY_RET_OK(_driver._SlDrvCmdOp((_SlCmdCtrl_t *)&_SlEventMaskGetCmdCtrl, &Msg, NULL)); |
dflet | 0:e89ba455dbcf | 1175 | |
dflet | 0:e89ba455dbcf | 1176 | *pMask = Msg.Rsp.mask; |
dflet | 0:e89ba455dbcf | 1177 | return SL_RET_CODE_OK; |
dflet | 0:e89ba455dbcf | 1178 | } |
dflet | 0:e89ba455dbcf | 1179 | #endif |
dflet | 0:e89ba455dbcf | 1180 | |
dflet | 0:e89ba455dbcf | 1181 | |
dflet | 0:e89ba455dbcf | 1182 | |
dflet | 0:e89ba455dbcf | 1183 | /****************************************************************************** |
dflet | 0:e89ba455dbcf | 1184 | sl_DevGet |
dflet | 0:e89ba455dbcf | 1185 | ******************************************************************************/ |
dflet | 0:e89ba455dbcf | 1186 | |
dflet | 0:e89ba455dbcf | 1187 | typedef union { |
dflet | 0:e89ba455dbcf | 1188 | _DeviceSetGet_t Cmd; |
dflet | 0:e89ba455dbcf | 1189 | _DeviceSetGet_t Rsp; |
dflet | 0:e89ba455dbcf | 1190 | } _SlDeviceMsgGet_u; |
dflet | 0:e89ba455dbcf | 1191 | |
dflet | 0:e89ba455dbcf | 1192 | const _SlCmdCtrl_t _SlDeviceGetCmdCtrl = { |
dflet | 0:e89ba455dbcf | 1193 | SL_OPCODE_DEVICE_DEVICEGET, |
dflet | 0:e89ba455dbcf | 1194 | sizeof(_DeviceSetGet_t), |
dflet | 0:e89ba455dbcf | 1195 | sizeof(_DeviceSetGet_t) |
dflet | 0:e89ba455dbcf | 1196 | }; |
dflet | 0:e89ba455dbcf | 1197 | |
dflet | 0:e89ba455dbcf | 1198 | #if _SL_INCLUDE_FUNC(sl_DevGet) |
dflet | 0:e89ba455dbcf | 1199 | int32_t cc3100::sl_DevGet(uint8_t DeviceGetId, uint8_t *pOption,uint8_t *pConfigLen, uint8_t *pValues) |
dflet | 0:e89ba455dbcf | 1200 | { |
dflet | 0:e89ba455dbcf | 1201 | _SlDeviceMsgGet_u Msg; |
dflet | 0:e89ba455dbcf | 1202 | _SlCmdExt_t CmdExt; |
dflet | 0:e89ba455dbcf | 1203 | |
dflet | 0:e89ba455dbcf | 1204 | if (*pConfigLen == 0) { |
dflet | 0:e89ba455dbcf | 1205 | return SL_EZEROLEN; |
dflet | 0:e89ba455dbcf | 1206 | } |
dflet | 0:e89ba455dbcf | 1207 | |
dflet | 0:e89ba455dbcf | 1208 | if( pOption ) { |
dflet | 0:e89ba455dbcf | 1209 | CmdExt.TxPayloadLen = 0; |
dflet | 0:e89ba455dbcf | 1210 | CmdExt.RxPayloadLen = *pConfigLen; |
dflet | 0:e89ba455dbcf | 1211 | CmdExt.pTxPayload = NULL; |
dflet | 0:e89ba455dbcf | 1212 | CmdExt.pRxPayload = (uint8_t *)pValues; |
dflet | 0:e89ba455dbcf | 1213 | CmdExt.ActualRxPayloadLen = 0; |
dflet | 0:e89ba455dbcf | 1214 | |
dflet | 0:e89ba455dbcf | 1215 | Msg.Cmd.DeviceSetId = DeviceGetId; |
dflet | 0:e89ba455dbcf | 1216 | |
dflet | 0:e89ba455dbcf | 1217 | Msg.Cmd.Option = (uint16_t)*pOption; |
dflet | 0:e89ba455dbcf | 1218 | |
dflet | 0:e89ba455dbcf | 1219 | VERIFY_RET_OK(_driver._SlDrvCmdOp((_SlCmdCtrl_t *)&_SlDeviceGetCmdCtrl, &Msg, &CmdExt)); |
dflet | 0:e89ba455dbcf | 1220 | |
dflet | 0:e89ba455dbcf | 1221 | if( pOption ) { |
dflet | 0:e89ba455dbcf | 1222 | *pOption = (uint8_t)Msg.Rsp.Option; |
dflet | 0:e89ba455dbcf | 1223 | } |
dflet | 0:e89ba455dbcf | 1224 | |
dflet | 0:e89ba455dbcf | 1225 | if (CmdExt.RxPayloadLen < CmdExt.ActualRxPayloadLen) { |
dflet | 0:e89ba455dbcf | 1226 | *pConfigLen = (uint8_t)CmdExt.RxPayloadLen; |
dflet | 0:e89ba455dbcf | 1227 | return SL_ESMALLBUF; |
dflet | 0:e89ba455dbcf | 1228 | } else { |
dflet | 0:e89ba455dbcf | 1229 | *pConfigLen = (uint8_t)CmdExt.ActualRxPayloadLen; |
dflet | 0:e89ba455dbcf | 1230 | } |
dflet | 0:e89ba455dbcf | 1231 | |
dflet | 0:e89ba455dbcf | 1232 | return (int16_t)Msg.Rsp.Status; |
dflet | 0:e89ba455dbcf | 1233 | } else { |
dflet | 0:e89ba455dbcf | 1234 | return -1; |
dflet | 0:e89ba455dbcf | 1235 | } |
dflet | 0:e89ba455dbcf | 1236 | } |
dflet | 0:e89ba455dbcf | 1237 | #endif |
dflet | 0:e89ba455dbcf | 1238 | |
dflet | 0:e89ba455dbcf | 1239 | /****************************************************************************** |
dflet | 0:e89ba455dbcf | 1240 | sl_DevSet |
dflet | 0:e89ba455dbcf | 1241 | ******************************************************************************/ |
dflet | 0:e89ba455dbcf | 1242 | typedef union { |
dflet | 0:e89ba455dbcf | 1243 | _DeviceSetGet_t Cmd; |
dflet | 0:e89ba455dbcf | 1244 | _BasicResponse_t Rsp; |
dflet | 0:e89ba455dbcf | 1245 | } _SlDeviceMsgSet_u; |
dflet | 0:e89ba455dbcf | 1246 | |
dflet | 0:e89ba455dbcf | 1247 | const _SlCmdCtrl_t _SlDeviceSetCmdCtrl = { |
dflet | 0:e89ba455dbcf | 1248 | SL_OPCODE_DEVICE_DEVICESET, |
dflet | 0:e89ba455dbcf | 1249 | sizeof(_DeviceSetGet_t), |
dflet | 0:e89ba455dbcf | 1250 | sizeof(_BasicResponse_t) |
dflet | 0:e89ba455dbcf | 1251 | }; |
dflet | 0:e89ba455dbcf | 1252 | |
dflet | 0:e89ba455dbcf | 1253 | #if _SL_INCLUDE_FUNC(sl_DevSet) |
dflet | 0:e89ba455dbcf | 1254 | int32_t cc3100::sl_DevSet(uint8_t DeviceSetId ,uint8_t Option,uint8_t ConfigLen, uint8_t *pValues) |
dflet | 0:e89ba455dbcf | 1255 | { |
dflet | 0:e89ba455dbcf | 1256 | _SlDeviceMsgSet_u Msg; |
dflet | 0:e89ba455dbcf | 1257 | _SlCmdExt_t CmdExt; |
dflet | 0:e89ba455dbcf | 1258 | |
dflet | 0:e89ba455dbcf | 1259 | CmdExt.TxPayloadLen = (ConfigLen+3) & (~3); |
dflet | 0:e89ba455dbcf | 1260 | CmdExt.RxPayloadLen = 0; |
dflet | 0:e89ba455dbcf | 1261 | CmdExt.pTxPayload = (uint8_t *)pValues; |
dflet | 0:e89ba455dbcf | 1262 | CmdExt.pRxPayload = NULL; |
dflet | 0:e89ba455dbcf | 1263 | |
dflet | 0:e89ba455dbcf | 1264 | |
dflet | 0:e89ba455dbcf | 1265 | Msg.Cmd.DeviceSetId = DeviceSetId; |
dflet | 0:e89ba455dbcf | 1266 | Msg.Cmd.ConfigLen = ConfigLen; |
dflet | 0:e89ba455dbcf | 1267 | Msg.Cmd.Option = Option; |
dflet | 0:e89ba455dbcf | 1268 | |
dflet | 0:e89ba455dbcf | 1269 | VERIFY_RET_OK(_driver._SlDrvCmdOp((_SlCmdCtrl_t *)&_SlDeviceSetCmdCtrl, &Msg, &CmdExt)); |
dflet | 0:e89ba455dbcf | 1270 | |
dflet | 0:e89ba455dbcf | 1271 | return (int16_t)Msg.Rsp.status; |
dflet | 0:e89ba455dbcf | 1272 | } |
dflet | 0:e89ba455dbcf | 1273 | #endif |
dflet | 0:e89ba455dbcf | 1274 | |
dflet | 0:e89ba455dbcf | 1275 | /****************************************************************************** |
dflet | 0:e89ba455dbcf | 1276 | sl_UartSetMode |
dflet | 0:e89ba455dbcf | 1277 | ******************************************************************************/ |
dflet | 0:e89ba455dbcf | 1278 | #ifdef SL_IF_TYPE_UART |
dflet | 0:e89ba455dbcf | 1279 | typedef union { |
dflet | 0:e89ba455dbcf | 1280 | _DevUartSetModeCommand_t Cmd; |
dflet | 0:e89ba455dbcf | 1281 | _DevUartSetModeResponse_t Rsp; |
dflet | 0:e89ba455dbcf | 1282 | } _SlUartSetModeMsg_u; |
dflet | 0:e89ba455dbcf | 1283 | |
dflet | 0:e89ba455dbcf | 1284 | const _SlCmdCtrl_t _SlUartSetModeCmdCtrl = { |
dflet | 0:e89ba455dbcf | 1285 | SL_OPCODE_DEVICE_SETUARTMODECOMMAND, |
dflet | 0:e89ba455dbcf | 1286 | sizeof(_DevUartSetModeCommand_t), |
dflet | 0:e89ba455dbcf | 1287 | sizeof(_DevUartSetModeResponse_t) |
dflet | 0:e89ba455dbcf | 1288 | }; |
dflet | 0:e89ba455dbcf | 1289 | |
dflet | 0:e89ba455dbcf | 1290 | |
dflet | 0:e89ba455dbcf | 1291 | #if _SL_INCLUDE_FUNC(sl_UartSetMode) |
dflet | 0:e89ba455dbcf | 1292 | int16_t cc3100::sl_UartSetMode(const SlUartIfParams_t* pUartParams) |
dflet | 0:e89ba455dbcf | 1293 | { |
dflet | 0:e89ba455dbcf | 1294 | _SlUartSetModeMsg_u Msg; |
dflet | 0:e89ba455dbcf | 1295 | uint32_t magicCode = 0xFFFFFFFF; |
dflet | 0:e89ba455dbcf | 1296 | |
dflet | 0:e89ba455dbcf | 1297 | Msg.Cmd.BaudRate = pUartParams->BaudRate; |
dflet | 0:e89ba455dbcf | 1298 | Msg.Cmd.FlowControlEnable = pUartParams->FlowControlEnable; |
dflet | 0:e89ba455dbcf | 1299 | |
dflet | 0:e89ba455dbcf | 1300 | |
dflet | 0:e89ba455dbcf | 1301 | VERIFY_RET_OK(_SlDrvCmdOp((_SlCmdCtrl_t *)&_SlUartSetModeCmdCtrl, &Msg, NULL)); |
dflet | 0:e89ba455dbcf | 1302 | |
dflet | 0:e89ba455dbcf | 1303 | /* cmd response OK, we can continue with the handshake */ |
dflet | 0:e89ba455dbcf | 1304 | if (SL_RET_CODE_OK == Msg.Rsp.status) { |
dflet | 0:e89ba455dbcf | 1305 | sl_IfMaskIntHdlr(); |
dflet | 0:e89ba455dbcf | 1306 | |
dflet | 0:e89ba455dbcf | 1307 | /* Close the comm port */ |
dflet | 0:e89ba455dbcf | 1308 | sl_IfClose(g_pCB->FD); |
dflet | 0:e89ba455dbcf | 1309 | |
dflet | 0:e89ba455dbcf | 1310 | /* Re-open the comm port */ |
dflet | 0:e89ba455dbcf | 1311 | sl_IfOpen((void * )pUartParams, UART_IF_OPEN_FLAG_RE_OPEN); |
dflet | 0:e89ba455dbcf | 1312 | sl_IfUnMaskIntHdlr(); |
dflet | 0:e89ba455dbcf | 1313 | |
dflet | 0:e89ba455dbcf | 1314 | /* send the magic code and wait for the response */ |
dflet | 0:e89ba455dbcf | 1315 | sl_IfWrite(g_pCB->FD, (uint8_t* )&magicCode, 4); |
dflet | 0:e89ba455dbcf | 1316 | |
dflet | 0:e89ba455dbcf | 1317 | magicCode = UART_SET_MODE_MAGIC_CODE; |
dflet | 0:e89ba455dbcf | 1318 | sl_IfWrite(g_pCB->FD, (uint8_t* )&magicCode, 4); |
dflet | 0:e89ba455dbcf | 1319 | |
dflet | 0:e89ba455dbcf | 1320 | /* clear magic code */ |
dflet | 0:e89ba455dbcf | 1321 | magicCode = 0; |
dflet | 0:e89ba455dbcf | 1322 | |
dflet | 0:e89ba455dbcf | 1323 | /* wait (blocking) till the magic code to be returned from device */ |
dflet | 0:e89ba455dbcf | 1324 | sl_IfRead(g_pCB->FD, (uint8_t* )&magicCode, 4); |
dflet | 0:e89ba455dbcf | 1325 | |
dflet | 0:e89ba455dbcf | 1326 | /* check for the received magic code matching */ |
dflet | 0:e89ba455dbcf | 1327 | if (UART_SET_MODE_MAGIC_CODE != magicCode) { |
dflet | 0:e89ba455dbcf | 1328 | _SL_ASSERT(0); |
dflet | 0:e89ba455dbcf | 1329 | } |
dflet | 0:e89ba455dbcf | 1330 | } |
dflet | 0:e89ba455dbcf | 1331 | |
dflet | 0:e89ba455dbcf | 1332 | return (int16_t)Msg.Rsp.status; |
dflet | 0:e89ba455dbcf | 1333 | } |
dflet | 0:e89ba455dbcf | 1334 | #endif |
dflet | 0:e89ba455dbcf | 1335 | #endif |
dflet | 0:e89ba455dbcf | 1336 | |
dflet | 0:e89ba455dbcf | 1337 | }//namespace |
dflet | 0:e89ba455dbcf | 1338 | |
dflet | 0:e89ba455dbcf | 1339 |