cc3100_Socket_Wifi_Server with Ethernet Interface not working

Dependencies:   EthernetInterface mbed-rtos mbed

Fork of cc3100_Test_Demo by David Fletcher

Committer:
artpes
Date:
Fri May 19 14:45:14 2017 +0000
Revision:
8:f4eb30a34ea7
Parent:
6:778b081f6a13
fix

Who changed what in which revision?

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