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