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