cc3100_Socket_Wifi_Server with Ethernet Interface not working

Dependencies:   EthernetInterface mbed-rtos mbed

Fork of cc3100_Test_Demo by David Fletcher

Committer:
dflet
Date:
Thu Mar 19 15:39:08 2015 +0000
Revision:
6:778b081f6a13
Child:
8:f4eb30a34ea7
Updated to CC3100SDK1.1.0

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 /*!
dflet 6:778b081f6a13 277 \brief Create connection with server.
dflet 6:778b081f6a13 278
dflet 6:778b081f6a13 279 This function opens a socket and create the endpoint communication with server
dflet 6:778b081f6a13 280
dflet 6:778b081f6a13 281 \param[in] DestinationIP - IP address of the server
dflet 6:778b081f6a13 282
dflet 6:778b081f6a13 283 \return socket id for success and negative for error
dflet 6:778b081f6a13 284 */
dflet 6:778b081f6a13 285 int32_t cc3100::createConnection(uint32_t DestinationIP)
dflet 6:778b081f6a13 286 {
dflet 6:778b081f6a13 287 SlSockAddrIn_t Addr = {0};
dflet 6:778b081f6a13 288 int32_t Status = 0;
dflet 6:778b081f6a13 289 int32_t AddrSize = 0;
dflet 6:778b081f6a13 290 int32_t SockID = 0;
dflet 6:778b081f6a13 291
dflet 6:778b081f6a13 292 Addr.sin_family = SL_AF_INET;
dflet 6:778b081f6a13 293 Addr.sin_port = _socket.sl_Htons(80);
dflet 6:778b081f6a13 294 Addr.sin_addr.s_addr = _socket.sl_Htonl(DestinationIP);
dflet 6:778b081f6a13 295
dflet 6:778b081f6a13 296 AddrSize = sizeof(SlSockAddrIn_t);
dflet 6:778b081f6a13 297
dflet 6:778b081f6a13 298 SockID = _socket.sl_Socket(SL_AF_INET,SL_SOCK_STREAM, 0);
dflet 6:778b081f6a13 299 ASSERT_ON_ERROR(SockID);
dflet 6:778b081f6a13 300
dflet 6:778b081f6a13 301 Status = _socket.sl_Connect(SockID, ( SlSockAddr_t *)&Addr, AddrSize);
dflet 6:778b081f6a13 302 if (Status < 0)
dflet 6:778b081f6a13 303 {
dflet 6:778b081f6a13 304 _socket.sl_Close(SockID);
dflet 6:778b081f6a13 305 ASSERT_ON_ERROR(Status);
dflet 6:778b081f6a13 306 }
dflet 6:778b081f6a13 307
dflet 6:778b081f6a13 308 return SockID;
dflet 6:778b081f6a13 309 }
dflet 6:778b081f6a13 310
dflet 6:778b081f6a13 311 /*!
dflet 6:778b081f6a13 312 \brief Convert hex to decimal base
dflet 6:778b081f6a13 313
dflet 6:778b081f6a13 314 \param[in] ptr - pointer to string containing number in hex
dflet 6:778b081f6a13 315
dflet 6:778b081f6a13 316 \return number in decimal base
dflet 6:778b081f6a13 317
dflet 6:778b081f6a13 318 */
dflet 6:778b081f6a13 319 int32_t cc3100::hexToi(unsigned char *ptr)
dflet 6:778b081f6a13 320 {
dflet 6:778b081f6a13 321 uint32_t result = 0;
dflet 6:778b081f6a13 322 uint32_t len = 0;
dflet 6:778b081f6a13 323
dflet 6:778b081f6a13 324 int32_t idx = -1;
dflet 6:778b081f6a13 325
dflet 6:778b081f6a13 326 len = strlen((const char*) ptr);
dflet 6:778b081f6a13 327
dflet 6:778b081f6a13 328 /* convert characters to upper case */
dflet 6:778b081f6a13 329 for(idx = 0; ptr[idx] != '\0'; ++idx)
dflet 6:778b081f6a13 330 {
dflet 6:778b081f6a13 331 if( (ptr[idx] >= 'a') &&
dflet 6:778b081f6a13 332 (ptr[idx] <= 'f') )
dflet 6:778b081f6a13 333 {
dflet 6:778b081f6a13 334 ptr[idx] -= 32; /* Change case - ASCII 'a' = 97, 'A' = 65 => 97-65 = 32 */
dflet 6:778b081f6a13 335 }
dflet 6:778b081f6a13 336 }
dflet 6:778b081f6a13 337
dflet 6:778b081f6a13 338 for(idx = 0; ptr[idx] != '\0'; ++idx)
dflet 6:778b081f6a13 339 {
dflet 6:778b081f6a13 340 if(ptr[idx] >= '0' && ptr[idx] <= '9')
dflet 6:778b081f6a13 341 {
dflet 6:778b081f6a13 342 /* Converting '0' to '9' to their decimal value */
dflet 6:778b081f6a13 343 result += (ptr[idx] - '0') * (1 << (4 * (len - 1 - idx)));
dflet 6:778b081f6a13 344 }
dflet 6:778b081f6a13 345 else if(ptr[idx] >= 'A' && ptr[idx] <= 'F')
dflet 6:778b081f6a13 346 {
dflet 6:778b081f6a13 347 /* Converting hex 'A' to 'F' to their decimal value */
dflet 6:778b081f6a13 348 result += (ptr[idx] - 55) * (1 << (4 * (len -1 - idx))); /* .i.e. 'A' - 55 = 10, 'F' - 55 = 15 */
dflet 6:778b081f6a13 349 }
dflet 6:778b081f6a13 350 else
dflet 6:778b081f6a13 351 {
dflet 6:778b081f6a13 352 ASSERT_ON_ERROR(INVALID_HEX_STRING);
dflet 6:778b081f6a13 353 }
dflet 6:778b081f6a13 354 }
dflet 6:778b081f6a13 355
dflet 6:778b081f6a13 356 return result;
dflet 6:778b081f6a13 357 }
dflet 6:778b081f6a13 358
dflet 6:778b081f6a13 359 /*!
dflet 6:778b081f6a13 360 \brief Calculate the file chunk size
dflet 6:778b081f6a13 361
dflet 6:778b081f6a13 362 \param[in] len - pointer to length of the data in the buffer
dflet 6:778b081f6a13 363 \param[in] p_Buff - pointer to pointer of buffer containing data
dflet 6:778b081f6a13 364 \param[out] chunk_size - pointer to variable containing chunk size
dflet 6:778b081f6a13 365
dflet 6:778b081f6a13 366 \return 0 for success, -ve for error
dflet 6:778b081f6a13 367
dflet 6:778b081f6a13 368 */
dflet 6:778b081f6a13 369 int32_t cc3100::getChunkSize(int32_t *len, uint8_t **p_Buff, uint32_t *chunk_size)
dflet 6:778b081f6a13 370 {
dflet 6:778b081f6a13 371 int32_t idx = -1;
dflet 6:778b081f6a13 372 unsigned char lenBuff[10];
dflet 6:778b081f6a13 373
dflet 6:778b081f6a13 374 idx = 0;
dflet 6:778b081f6a13 375 memset(lenBuff, 0, sizeof(lenBuff));
dflet 6:778b081f6a13 376 while(*len >= 0 && **p_Buff != 13) /* check for <CR> */
dflet 6:778b081f6a13 377 {
dflet 6:778b081f6a13 378 if(0 == *len)
dflet 6:778b081f6a13 379 {
dflet 6:778b081f6a13 380 memset(g_buff, 0, sizeof(g_buff));
dflet 6:778b081f6a13 381 *len = _socket.sl_Recv(g_SockID, &g_buff[0], MAX_BUFF_SIZE, 0);
dflet 6:778b081f6a13 382 if(*len <= 0)
dflet 6:778b081f6a13 383 ASSERT_ON_ERROR(TCP_RECV_ERROR);
dflet 6:778b081f6a13 384
dflet 6:778b081f6a13 385 *p_Buff = g_buff;
dflet 6:778b081f6a13 386 }
dflet 6:778b081f6a13 387
dflet 6:778b081f6a13 388 lenBuff[idx] = **p_Buff;
dflet 6:778b081f6a13 389 idx++;
dflet 6:778b081f6a13 390 (*p_Buff)++;
dflet 6:778b081f6a13 391 (*len)--;
dflet 6:778b081f6a13 392 }
dflet 6:778b081f6a13 393
dflet 6:778b081f6a13 394 (*p_Buff) += 2; /* skip <CR><LF> */
dflet 6:778b081f6a13 395 (*len) -= 2;
dflet 6:778b081f6a13 396 *chunk_size = hexToi(lenBuff);
dflet 6:778b081f6a13 397
dflet 6:778b081f6a13 398 return SUCCESS;
dflet 6:778b081f6a13 399 }
dflet 6:778b081f6a13 400
dflet 6:778b081f6a13 401 /*!
dflet 6:778b081f6a13 402 \brief Obtain the file from the server
dflet 6:778b081f6a13 403
dflet 6:778b081f6a13 404 This function requests the file from the server and save it on serial flash.
dflet 6:778b081f6a13 405 To request a different file for different user needs to modify the
dflet 6:778b081f6a13 406 PREFIX_BUFFER and POST_BUFFER macros.
dflet 6:778b081f6a13 407
dflet 6:778b081f6a13 408 \param[in] None
dflet 6:778b081f6a13 409
dflet 6:778b081f6a13 410 \return 0 for success and negative for error
dflet 6:778b081f6a13 411
dflet 6:778b081f6a13 412 */
dflet 6:778b081f6a13 413 /*
dflet 6:778b081f6a13 414 int32_t cc3100::getFile()
dflet 6:778b081f6a13 415 {
dflet 6:778b081f6a13 416 uint32_t Token = 0;
dflet 6:778b081f6a13 417 uint32_t recv_size = 0;
dflet 6:778b081f6a13 418 uint8_t *pBuff = 0;
dflet 6:778b081f6a13 419 uint8_t eof_detected = 0;
dflet 6:778b081f6a13 420 uint8_t isChunked = 0;
dflet 6:778b081f6a13 421
dflet 6:778b081f6a13 422 int32_t transfer_len = -1;
dflet 6:778b081f6a13 423 int32_t retVal = -1;
dflet 6:778b081f6a13 424 int32_t fileHandle = -1;
dflet 6:778b081f6a13 425
dflet 6:778b081f6a13 426 memset(g_buff, 0, sizeof(g_buff));
dflet 6:778b081f6a13 427
dflet 6:778b081f6a13 428 //Puts together the HTTP GET string.
dflet 6:778b081f6a13 429 strcpy((char*)g_buff, PREFIX_BUFFER);
dflet 6:778b081f6a13 430 strcat((char*)g_buff, POST_BUFFER);
dflet 6:778b081f6a13 431
dflet 6:778b081f6a13 432 //Send the HTTP GET string to the opened TCP/IP socket.
dflet 6:778b081f6a13 433 transfer_len = _socket.sl_Send(g_SockID, g_buff, strlen((const char*)g_buff), 0);
dflet 6:778b081f6a13 434
dflet 6:778b081f6a13 435 if (transfer_len < 0)
dflet 6:778b081f6a13 436 {
dflet 6:778b081f6a13 437 // error
dflet 6:778b081f6a13 438 printf(" Socket Send Error\r\n");
dflet 6:778b081f6a13 439 ASSERT_ON_ERROR(TCP_SEND_ERROR);
dflet 6:778b081f6a13 440 }
dflet 6:778b081f6a13 441
dflet 6:778b081f6a13 442 memset(g_buff, 0, sizeof(g_buff));
dflet 6:778b081f6a13 443
dflet 6:778b081f6a13 444 //get the reply from the server in buffer.
dflet 6:778b081f6a13 445 transfer_len = _socket.sl_Recv(g_SockID, &g_buff[0], MAX_BUFF_SIZE, 0);
dflet 6:778b081f6a13 446
dflet 6:778b081f6a13 447 if(transfer_len <= 0)
dflet 6:778b081f6a13 448 ASSERT_ON_ERROR(TCP_RECV_ERROR);
dflet 6:778b081f6a13 449
dflet 6:778b081f6a13 450 // Check for 404 return code
dflet 6:778b081f6a13 451 if(strstr((const char*)g_buff, HTTP_FILE_NOT_FOUND) != 0)
dflet 6:778b081f6a13 452 {
dflet 6:778b081f6a13 453 printf(" File not found, check the file and try again\r\n");
dflet 6:778b081f6a13 454 ASSERT_ON_ERROR(FILE_NOT_FOUND_ERROR);
dflet 6:778b081f6a13 455 }
dflet 6:778b081f6a13 456
dflet 6:778b081f6a13 457 // if not "200 OK" return error
dflet 6:778b081f6a13 458 if(strstr((const char*)g_buff, HTTP_STATUS_OK) == 0)
dflet 6:778b081f6a13 459 {
dflet 6:778b081f6a13 460 printf(" Error during downloading the file\r\n");
dflet 6:778b081f6a13 461 ASSERT_ON_ERROR(INVALID_SERVER_RESPONSE);
dflet 6:778b081f6a13 462 }
dflet 6:778b081f6a13 463
dflet 6:778b081f6a13 464 // check if content length is transferred with headers
dflet 6:778b081f6a13 465 pBuff = (uint8_t *)strstr((const char*)g_buff, HTTP_CONTENT_LENGTH);
dflet 6:778b081f6a13 466 if(pBuff != 0)
dflet 6:778b081f6a13 467 {
dflet 6:778b081f6a13 468 // not supported
dflet 6:778b081f6a13 469 printf(" Server response format is not supported\r\n");
dflet 6:778b081f6a13 470 ASSERT_ON_ERROR(FORMAT_NOT_SUPPORTED);
dflet 6:778b081f6a13 471 }
dflet 6:778b081f6a13 472
dflet 6:778b081f6a13 473 // Check if data is chunked
dflet 6:778b081f6a13 474 pBuff = (uint8_t *)strstr((const char*)g_buff, HTTP_TRANSFER_ENCODING);
dflet 6:778b081f6a13 475 if(pBuff != 0)
dflet 6:778b081f6a13 476 {
dflet 6:778b081f6a13 477 pBuff += strlen(HTTP_TRANSFER_ENCODING);
dflet 6:778b081f6a13 478 while(*pBuff == SPACE)
dflet 6:778b081f6a13 479 pBuff++;
dflet 6:778b081f6a13 480
dflet 6:778b081f6a13 481 if(memcmp(pBuff, HTTP_ENCODING_CHUNKED, strlen(HTTP_ENCODING_CHUNKED)) == 0)
dflet 6:778b081f6a13 482 {
dflet 6:778b081f6a13 483 recv_size = 0;
dflet 6:778b081f6a13 484 isChunked = 1;
dflet 6:778b081f6a13 485 }
dflet 6:778b081f6a13 486 }
dflet 6:778b081f6a13 487 else
dflet 6:778b081f6a13 488 {
dflet 6:778b081f6a13 489 // Check if connection will be closed by after sending data
dflet 6:778b081f6a13 490 // In this method the content length is not received and end of
dflet 6:778b081f6a13 491 // connection marks the end of data
dflet 6:778b081f6a13 492 pBuff = (uint8_t *)strstr((const char*)g_buff, HTTP_CONNECTION);
dflet 6:778b081f6a13 493 if(pBuff != 0)
dflet 6:778b081f6a13 494 {
dflet 6:778b081f6a13 495 pBuff += strlen(HTTP_CONNECTION);
dflet 6:778b081f6a13 496 while(*pBuff == SPACE)
dflet 6:778b081f6a13 497 pBuff++;
dflet 6:778b081f6a13 498
dflet 6:778b081f6a13 499 if(memcmp(pBuff, HTTP_ENCODING_CHUNKED, strlen(HTTP_CONNECTION_CLOSE)) == 0)
dflet 6:778b081f6a13 500 {
dflet 6:778b081f6a13 501 // not supported
dflet 6:778b081f6a13 502 printf(" Server response format is not supported\r\n");
dflet 6:778b081f6a13 503 ASSERT_ON_ERROR(FORMAT_NOT_SUPPORTED);
dflet 6:778b081f6a13 504 }
dflet 6:778b081f6a13 505 }
dflet 6:778b081f6a13 506 }
dflet 6:778b081f6a13 507
dflet 6:778b081f6a13 508 // "\r\n\r\n" marks the end of headers
dflet 6:778b081f6a13 509 pBuff = (uint8_t *)strstr((const char*)g_buff, HTTP_END_OF_HEADER);
dflet 6:778b081f6a13 510 if(pBuff == 0)
dflet 6:778b081f6a13 511 {
dflet 6:778b081f6a13 512 printf(" Invalid response\r\n");
dflet 6:778b081f6a13 513 ASSERT_ON_ERROR(INVALID_SERVER_RESPONSE);
dflet 6:778b081f6a13 514 }
dflet 6:778b081f6a13 515 // Increment by 4 to skip "\r\n\r\n"
dflet 6:778b081f6a13 516 pBuff += 4;
dflet 6:778b081f6a13 517
dflet 6:778b081f6a13 518 // Adjust buffer data length for header size
dflet 6:778b081f6a13 519 transfer_len -= (pBuff - g_buff);
dflet 6:778b081f6a13 520
dflet 6:778b081f6a13 521 // If data in chunked format, calculate the chunk size
dflet 6:778b081f6a13 522 if(isChunked == 1)
dflet 6:778b081f6a13 523 {
dflet 6:778b081f6a13 524 retVal = getChunkSize(&transfer_len, &pBuff, &recv_size);
dflet 6:778b081f6a13 525 if(retVal < 0)
dflet 6:778b081f6a13 526 {
dflet 6:778b081f6a13 527 // Error
dflet 6:778b081f6a13 528 printf(" Problem with connection to server\r\n");
dflet 6:778b081f6a13 529 return retVal;
dflet 6:778b081f6a13 530 }
dflet 6:778b081f6a13 531 }
dflet 6:778b081f6a13 532
dflet 6:778b081f6a13 533 // Open file to save the downloaded file
dflet 6:778b081f6a13 534 retVal = _fs.sl_FsOpen((uint8_t *)FILE_NAME,
dflet 6:778b081f6a13 535 FS_MODE_OPEN_WRITE, &Token, &fileHandle);
dflet 6:778b081f6a13 536 if(retVal < 0)
dflet 6:778b081f6a13 537 {
dflet 6:778b081f6a13 538 // File Doesn't exit create a new of 45 KB file
dflet 6:778b081f6a13 539 retVal = _fs.sl_FsOpen((uint8_t *)FILE_NAME,
dflet 6:778b081f6a13 540 _fs.FS_MODE_OPEN_CREATE(SIZE_45K,_FS_FILE_OPEN_FLAG_COMMIT|_FS_FILE_PUBLIC_WRITE),
dflet 6:778b081f6a13 541 &Token, &fileHandle);
dflet 6:778b081f6a13 542 if(retVal < 0)
dflet 6:778b081f6a13 543 {
dflet 6:778b081f6a13 544 printf(" Error during opening the file\r\n");
dflet 6:778b081f6a13 545 return retVal;
dflet 6:778b081f6a13 546 }
dflet 6:778b081f6a13 547 }
dflet 6:778b081f6a13 548
dflet 6:778b081f6a13 549 while (0 < transfer_len)
dflet 6:778b081f6a13 550 {
dflet 6:778b081f6a13 551 // For chunked data recv_size contains the chunk size to be received
dflet 6:778b081f6a13 552 // while the transfer_len contains the data in the buffer
dflet 6:778b081f6a13 553 if(recv_size <= transfer_len)
dflet 6:778b081f6a13 554 {
dflet 6:778b081f6a13 555 // write the recv_size
dflet 6:778b081f6a13 556 retVal = _fs.sl_FsWrite(fileHandle, g_BytesReceived,
dflet 6:778b081f6a13 557 (uint8_t *)pBuff, recv_size);
dflet 6:778b081f6a13 558 if(retVal < recv_size)
dflet 6:778b081f6a13 559 {
dflet 6:778b081f6a13 560 // Close file without saving
dflet 6:778b081f6a13 561 retVal = _fs.sl_FsClose(fileHandle, 0, (unsigned char*)"A", 1);
dflet 6:778b081f6a13 562 printf(" Error during writing the file\r\n");
dflet 6:778b081f6a13 563 return FILE_WRITE_ERROR;
dflet 6:778b081f6a13 564 }
dflet 6:778b081f6a13 565 transfer_len -= recv_size;
dflet 6:778b081f6a13 566 g_BytesReceived +=recv_size;
dflet 6:778b081f6a13 567 pBuff += recv_size;
dflet 6:778b081f6a13 568 recv_size = 0;
dflet 6:778b081f6a13 569
dflet 6:778b081f6a13 570 if(isChunked == 1)
dflet 6:778b081f6a13 571 {
dflet 6:778b081f6a13 572 // if data in chunked format calculate next chunk size
dflet 6:778b081f6a13 573 pBuff += 2; // 2 bytes for <CR> <LF>
dflet 6:778b081f6a13 574 transfer_len -= 2;
dflet 6:778b081f6a13 575
dflet 6:778b081f6a13 576 if(getChunkSize(&transfer_len, &pBuff, &recv_size) < 0)
dflet 6:778b081f6a13 577 {
dflet 6:778b081f6a13 578 // Error
dflet 6:778b081f6a13 579 break;
dflet 6:778b081f6a13 580 }
dflet 6:778b081f6a13 581
dflet 6:778b081f6a13 582 // if next chunk size is zero we have received the complete file
dflet 6:778b081f6a13 583 if(recv_size == 0)
dflet 6:778b081f6a13 584 {
dflet 6:778b081f6a13 585 eof_detected = 1;
dflet 6:778b081f6a13 586 break;
dflet 6:778b081f6a13 587 }
dflet 6:778b081f6a13 588
dflet 6:778b081f6a13 589 if(recv_size < transfer_len)
dflet 6:778b081f6a13 590 {
dflet 6:778b081f6a13 591 // Code will enter this section if the new chunk size is less then
dflet 6:778b081f6a13 592 // then the transfer size. This will the last chunk of file received
dflet 6:778b081f6a13 593
dflet 6:778b081f6a13 594 retVal = _fs.sl_FsWrite(fileHandle, g_BytesReceived,
dflet 6:778b081f6a13 595 (uint8_t *)pBuff, recv_size);
dflet 6:778b081f6a13 596 if(retVal < recv_size)
dflet 6:778b081f6a13 597 {
dflet 6:778b081f6a13 598 // Close file without saving
dflet 6:778b081f6a13 599 retVal = _fs.sl_FsClose(fileHandle, 0, (unsigned char*)"A", 1);
dflet 6:778b081f6a13 600 printf(" Error during writing the file\r\n");
dflet 6:778b081f6a13 601 return FILE_WRITE_ERROR;
dflet 6:778b081f6a13 602 }
dflet 6:778b081f6a13 603 transfer_len -= recv_size;
dflet 6:778b081f6a13 604 g_BytesReceived +=recv_size;
dflet 6:778b081f6a13 605 pBuff += recv_size;
dflet 6:778b081f6a13 606 recv_size = 0;
dflet 6:778b081f6a13 607
dflet 6:778b081f6a13 608 pBuff += 2; // 2bytes for <CR> <LF>
dflet 6:778b081f6a13 609 transfer_len -= 2;
dflet 6:778b081f6a13 610
dflet 6:778b081f6a13 611 // Calculate the next chunk size, should be zero
dflet 6:778b081f6a13 612 if(getChunkSize(&transfer_len, &pBuff, &recv_size) < 0)
dflet 6:778b081f6a13 613 {
dflet 6:778b081f6a13 614 // Error
dflet 6:778b081f6a13 615 break;
dflet 6:778b081f6a13 616 }
dflet 6:778b081f6a13 617
dflet 6:778b081f6a13 618 // if next chunk size is non zero error
dflet 6:778b081f6a13 619 if(recv_size != 0)
dflet 6:778b081f6a13 620 {
dflet 6:778b081f6a13 621 // Error
dflet 6:778b081f6a13 622 break;
dflet 6:778b081f6a13 623 }
dflet 6:778b081f6a13 624 eof_detected = 1;
dflet 6:778b081f6a13 625 break;
dflet 6:778b081f6a13 626 }
dflet 6:778b081f6a13 627 else
dflet 6:778b081f6a13 628 {
dflet 6:778b081f6a13 629 // write data on the file
dflet 6:778b081f6a13 630 retVal = _fs.sl_FsWrite(fileHandle, g_BytesReceived,
dflet 6:778b081f6a13 631 (uint8_t *)pBuff, transfer_len);
dflet 6:778b081f6a13 632 if(retVal < transfer_len)
dflet 6:778b081f6a13 633 {
dflet 6:778b081f6a13 634 // Close file without saving
dflet 6:778b081f6a13 635 retVal = _fs.sl_FsClose(fileHandle, 0, (unsigned char*)"A", 1);
dflet 6:778b081f6a13 636 printf(" Error during writing the file\r\n");
dflet 6:778b081f6a13 637 ASSERT_ON_ERROR(FILE_WRITE_ERROR);
dflet 6:778b081f6a13 638 }
dflet 6:778b081f6a13 639 recv_size -= transfer_len;
dflet 6:778b081f6a13 640 g_BytesReceived +=transfer_len;
dflet 6:778b081f6a13 641 }
dflet 6:778b081f6a13 642 }
dflet 6:778b081f6a13 643 // complete file received exit
dflet 6:778b081f6a13 644 if(recv_size == 0)
dflet 6:778b081f6a13 645 {
dflet 6:778b081f6a13 646 eof_detected = 1;
dflet 6:778b081f6a13 647 break;
dflet 6:778b081f6a13 648 }
dflet 6:778b081f6a13 649 }
dflet 6:778b081f6a13 650 else
dflet 6:778b081f6a13 651 {
dflet 6:778b081f6a13 652 // write data on the file
dflet 6:778b081f6a13 653 retVal = _fs.sl_FsWrite(fileHandle, g_BytesReceived,
dflet 6:778b081f6a13 654 (uint8_t *)pBuff, transfer_len);
dflet 6:778b081f6a13 655 if (retVal < 0)
dflet 6:778b081f6a13 656 {
dflet 6:778b081f6a13 657 // Close file without saving
dflet 6:778b081f6a13 658 retVal = _fs.sl_FsClose(fileHandle, 0, (unsigned char*)"A", 1);
dflet 6:778b081f6a13 659 printf(" Error during writing the file\r\n");
dflet 6:778b081f6a13 660 ASSERT_ON_ERROR(FILE_WRITE_ERROR);
dflet 6:778b081f6a13 661 }
dflet 6:778b081f6a13 662 g_BytesReceived +=transfer_len;
dflet 6:778b081f6a13 663 recv_size -= transfer_len;
dflet 6:778b081f6a13 664 }
dflet 6:778b081f6a13 665
dflet 6:778b081f6a13 666 memset(g_buff, 0, sizeof(g_buff));
dflet 6:778b081f6a13 667
dflet 6:778b081f6a13 668 transfer_len = _socket.sl_Recv(g_SockID, &g_buff[0], MAX_BUFF_SIZE, 0);
dflet 6:778b081f6a13 669 if(transfer_len <= 0)
dflet 6:778b081f6a13 670 ASSERT_ON_ERROR(TCP_RECV_ERROR);
dflet 6:778b081f6a13 671
dflet 6:778b081f6a13 672 pBuff = g_buff;
dflet 6:778b081f6a13 673 }
dflet 6:778b081f6a13 674
dflet 6:778b081f6a13 675 // If user file has checksum which can be used to verify the temporary
dflet 6:778b081f6a13 676 // file then file should be verified
dflet 6:778b081f6a13 677 // In case of invalid file (FILE_NAME) should be closed without saving to
dflet 6:778b081f6a13 678 // recover the previous version of file
dflet 6:778b081f6a13 679 if(0 > transfer_len || eof_detected == 0)
dflet 6:778b081f6a13 680 {
dflet 6:778b081f6a13 681 // Close file without saving
dflet 6:778b081f6a13 682 retVal = _fs.sl_FsClose(fileHandle, 0, (unsigned char*)"A", 1);
dflet 6:778b081f6a13 683 printf(" Error While File Download\r\n");
dflet 6:778b081f6a13 684 ASSERT_ON_ERROR(INVALID_FILE);
dflet 6:778b081f6a13 685 }
dflet 6:778b081f6a13 686 else
dflet 6:778b081f6a13 687 {
dflet 6:778b081f6a13 688 // Save and close file
dflet 6:778b081f6a13 689 retVal = _fs.sl_FsClose(fileHandle, 0, 0, 0);
dflet 6:778b081f6a13 690 ASSERT_ON_ERROR(retVal);
dflet 6:778b081f6a13 691 }
dflet 6:778b081f6a13 692
dflet 6:778b081f6a13 693 return SUCCESS;;
dflet 6:778b081f6a13 694 }
dflet 6:778b081f6a13 695 */
dflet 6:778b081f6a13 696
dflet 6:778b081f6a13 697 /*!
dflet 6:778b081f6a13 698 \brief Connecting to a WLAN Access point
dflet 6:778b081f6a13 699
dflet 6:778b081f6a13 700 This function connects to the required AP (SSID_NAME).
dflet 6:778b081f6a13 701 The function will return once we are connected and have acquired IP address
dflet 6:778b081f6a13 702
dflet 6:778b081f6a13 703 \param[in] None
dflet 6:778b081f6a13 704
dflet 6:778b081f6a13 705 \return 0 on success, negative error-code on error
dflet 6:778b081f6a13 706
dflet 6:778b081f6a13 707 \note
dflet 6:778b081f6a13 708
dflet 6:778b081f6a13 709 \warning If the WLAN connection fails or we don't acquire an IP address,
dflet 6:778b081f6a13 710 We will be stuck in this function forever.
dflet 6:778b081f6a13 711 */
dflet 6:778b081f6a13 712 int32_t cc3100::establishConnectionWithAP()
dflet 6:778b081f6a13 713 {
dflet 6:778b081f6a13 714
dflet 6:778b081f6a13 715 SlSecParams_t secParams = {0};
dflet 6:778b081f6a13 716 int32_t retVal = 0;
dflet 6:778b081f6a13 717
dflet 6:778b081f6a13 718 secParams.Key = (signed char *)PASSKEY;
dflet 6:778b081f6a13 719 secParams.KeyLen = strlen(PASSKEY);
dflet 6:778b081f6a13 720 secParams.Type = SEC_TYPE;
dflet 6:778b081f6a13 721
dflet 6:778b081f6a13 722 retVal = _wlan.sl_WlanConnect((signed char *)SSID_NAME, strlen(SSID_NAME), 0, &secParams, 0);
dflet 6:778b081f6a13 723 ASSERT_ON_ERROR(retVal);
dflet 6:778b081f6a13 724
dflet 6:778b081f6a13 725 /* Wait */
dflet 6:778b081f6a13 726 while((!IS_CONNECTED(g_Status,STATUS_BIT_CONNECTION)) || (!IS_IP_ACQUIRED(g_Status,STATUS_BIT_IP_ACQUIRED))) { _nonos._SlNonOsMainLoopTask(); }
dflet 6:778b081f6a13 727
dflet 6:778b081f6a13 728 return SUCCESS;
dflet 6:778b081f6a13 729 }
dflet 6:778b081f6a13 730
dflet 6:778b081f6a13 731 /*!
dflet 6:778b081f6a13 732 \brief This function checks the LAN connection by pinging the AP's gateway
dflet 6:778b081f6a13 733
dflet 6:778b081f6a13 734 \param[in] None
dflet 6:778b081f6a13 735
dflet 6:778b081f6a13 736 \return 0 on success, negative error-code on error
dflet 6:778b081f6a13 737 */
dflet 6:778b081f6a13 738 int32_t cc3100::checkLanConnection()
dflet 6:778b081f6a13 739 {
dflet 6:778b081f6a13 740 SlPingStartCommand_t pingParams = {0};
dflet 6:778b081f6a13 741 SlPingReport_t pingReport = {0};
dflet 6:778b081f6a13 742
dflet 6:778b081f6a13 743 int32_t retVal = -1;
dflet 6:778b081f6a13 744
dflet 6:778b081f6a13 745 CLR_STATUS_BIT(g_Status, STATUS_BIT_PING_DONE);
dflet 6:778b081f6a13 746 g_PingPacketsRecv = 0;
dflet 6:778b081f6a13 747
dflet 6:778b081f6a13 748 /* Set the ping parameters */
dflet 6:778b081f6a13 749 pingParams.PingIntervalTime = PING_INTERVAL;
dflet 6:778b081f6a13 750 pingParams.PingSize = PING_PKT_SIZE;
dflet 6:778b081f6a13 751 pingParams.PingRequestTimeout = PING_TIMEOUT;
dflet 6:778b081f6a13 752 pingParams.TotalNumberOfAttempts = PING_ATTEMPTS;
dflet 6:778b081f6a13 753 pingParams.Flags = 0;
dflet 6:778b081f6a13 754 pingParams.Ip = g_GatewayIP;
dflet 6:778b081f6a13 755
dflet 6:778b081f6a13 756 /* Check for LAN connection */
dflet 6:778b081f6a13 757 retVal = _netapp.sl_NetAppPingStart( (SlPingStartCommand_t*)&pingParams, SL_AF_INET,
dflet 6:778b081f6a13 758 (SlPingReport_t*)&pingReport, SimpleLinkPingReport);
dflet 6:778b081f6a13 759 ASSERT_ON_ERROR(retVal);
dflet 6:778b081f6a13 760
dflet 6:778b081f6a13 761 /* Wait */
dflet 6:778b081f6a13 762 while(!IS_PING_DONE(g_Status,STATUS_BIT_PING_DONE)) { _nonos._SlNonOsMainLoopTask(); }
dflet 6:778b081f6a13 763
dflet 6:778b081f6a13 764 if(0 == g_PingPacketsRecv)
dflet 6:778b081f6a13 765 {
dflet 6:778b081f6a13 766 /* Problem with LAN connection */
dflet 6:778b081f6a13 767 ASSERT_ON_ERROR(LAN_CONNECTION_FAILED);
dflet 6:778b081f6a13 768 }
dflet 6:778b081f6a13 769
dflet 6:778b081f6a13 770 /* LAN connection is successful */
dflet 6:778b081f6a13 771 return SUCCESS;
dflet 6:778b081f6a13 772 }
dflet 6:778b081f6a13 773
dflet 6:778b081f6a13 774 /*!
dflet 6:778b081f6a13 775 \brief This function checks the internet connection by pinging
dflet 6:778b081f6a13 776 the external-host (HOST_NAME)
dflet 6:778b081f6a13 777
dflet 6:778b081f6a13 778 \param[in] None
dflet 6:778b081f6a13 779
dflet 6:778b081f6a13 780 \return 0 on success, negative error-code on error
dflet 6:778b081f6a13 781 */
dflet 6:778b081f6a13 782 int32_t cc3100::checkInternetConnection()
dflet 6:778b081f6a13 783 {
dflet 6:778b081f6a13 784 SlPingStartCommand_t pingParams = {0};
dflet 6:778b081f6a13 785 SlPingReport_t pingReport = {0};
dflet 6:778b081f6a13 786
dflet 6:778b081f6a13 787 uint32_t ipAddr = 0;
dflet 6:778b081f6a13 788
dflet 6:778b081f6a13 789 int32_t retVal = -1;
dflet 6:778b081f6a13 790
dflet 6:778b081f6a13 791 CLR_STATUS_BIT(g_Status, STATUS_BIT_PING_DONE);
dflet 6:778b081f6a13 792 g_PingPacketsRecv = 0;
dflet 6:778b081f6a13 793
dflet 6:778b081f6a13 794 /* Set the ping parameters */
dflet 6:778b081f6a13 795 pingParams.PingIntervalTime = PING_INTERVAL;
dflet 6:778b081f6a13 796 pingParams.PingSize = PING_PKT_SIZE;
dflet 6:778b081f6a13 797 pingParams.PingRequestTimeout = PING_TIMEOUT;
dflet 6:778b081f6a13 798 pingParams.TotalNumberOfAttempts = PING_ATTEMPTS;
dflet 6:778b081f6a13 799 pingParams.Flags = 0;
dflet 6:778b081f6a13 800 pingParams.Ip = g_GatewayIP;
dflet 6:778b081f6a13 801
dflet 6:778b081f6a13 802 /* Check for Internet connection */
dflet 6:778b081f6a13 803 retVal = _netapp.sl_NetAppDnsGetHostByName((unsigned char *)HOST_NAME, strlen(HOST_NAME), &ipAddr, SL_AF_INET);
dflet 6:778b081f6a13 804 ASSERT_ON_ERROR(retVal);
dflet 6:778b081f6a13 805
dflet 6:778b081f6a13 806 /* Replace the ping address to match HOST_NAME's IP address */
dflet 6:778b081f6a13 807 pingParams.Ip = ipAddr;
dflet 6:778b081f6a13 808
dflet 6:778b081f6a13 809 /* Try to ping HOST_NAME */
dflet 6:778b081f6a13 810 retVal = _netapp.sl_NetAppPingStart( (SlPingStartCommand_t*)&pingParams, SL_AF_INET,
dflet 6:778b081f6a13 811 (SlPingReport_t*)&pingReport, SimpleLinkPingReport);
dflet 6:778b081f6a13 812 ASSERT_ON_ERROR(retVal);
dflet 6:778b081f6a13 813
dflet 6:778b081f6a13 814 /* Wait */
dflet 6:778b081f6a13 815 while(!IS_PING_DONE(g_Status,STATUS_BIT_PING_DONE)) { _nonos._SlNonOsMainLoopTask(); }
dflet 6:778b081f6a13 816
dflet 6:778b081f6a13 817 if (0 == g_PingPacketsRecv)
dflet 6:778b081f6a13 818 {
dflet 6:778b081f6a13 819 /* Problem with internet connection*/
dflet 6:778b081f6a13 820 ASSERT_ON_ERROR(INTERNET_CONNECTION_FAILED);
dflet 6:778b081f6a13 821 }
dflet 6:778b081f6a13 822
dflet 6:778b081f6a13 823 /* Internet connection is successful */
dflet 6:778b081f6a13 824 return SUCCESS;
dflet 6:778b081f6a13 825 }
dflet 6:778b081f6a13 826
dflet 6:778b081f6a13 827 const int8_t StartResponseLUT[8] =
dflet 6:778b081f6a13 828 {
dflet 6:778b081f6a13 829 ROLE_UNKNOWN_ERR,
dflet 6:778b081f6a13 830 ROLE_STA,
dflet 6:778b081f6a13 831 ROLE_STA_ERR,
dflet 6:778b081f6a13 832 ROLE_AP,
dflet 6:778b081f6a13 833 ROLE_AP_ERR,
dflet 6:778b081f6a13 834 ROLE_P2P,
dflet 6:778b081f6a13 835 ROLE_P2P_ERR,
dflet 6:778b081f6a13 836 ROLE_UNKNOWN_ERR
dflet 6:778b081f6a13 837 };
dflet 6:778b081f6a13 838
dflet 6:778b081f6a13 839 /*****************************************************************************/
dflet 6:778b081f6a13 840 /* Internal functions */
dflet 6:778b081f6a13 841 /*****************************************************************************/
dflet 6:778b081f6a13 842
dflet 6:778b081f6a13 843 int16_t cc3100::_sl_GetStartResponseConvert(uint32_t Status)
dflet 6:778b081f6a13 844 {
dflet 6:778b081f6a13 845 return (int16_t)StartResponseLUT[Status & 0x7];
dflet 6:778b081f6a13 846
dflet 6:778b081f6a13 847 }
dflet 6:778b081f6a13 848
dflet 6:778b081f6a13 849 /*****************************************************************************/
dflet 6:778b081f6a13 850 /* API Functions */
dflet 6:778b081f6a13 851 /*****************************************************************************/
dflet 6:778b081f6a13 852
dflet 6:778b081f6a13 853 bool cc3100::IS_PING_DONE(uint32_t status_variable,const uint32_t bit){
dflet 6:778b081f6a13 854
dflet 6:778b081f6a13 855 g_Status = status_variable;
dflet 6:778b081f6a13 856
dflet 6:778b081f6a13 857 if(0 != (g_Status & ((uint32_t)1L<<(bit)))){
dflet 6:778b081f6a13 858 return TRUE;
dflet 6:778b081f6a13 859 }else{
dflet 6:778b081f6a13 860 return FALSE;
dflet 6:778b081f6a13 861 }
dflet 6:778b081f6a13 862 }
dflet 6:778b081f6a13 863
dflet 6:778b081f6a13 864 bool cc3100::IS_CONNECTED(uint32_t status_variable,const uint32_t bit){
dflet 6:778b081f6a13 865
dflet 6:778b081f6a13 866 g_Status = status_variable;
dflet 6:778b081f6a13 867
dflet 6:778b081f6a13 868 if(0 != (g_Status & ((uint32_t)1L<<(bit)))){
dflet 6:778b081f6a13 869 return TRUE;
dflet 6:778b081f6a13 870 }else{
dflet 6:778b081f6a13 871 return FALSE;
dflet 6:778b081f6a13 872 }
dflet 6:778b081f6a13 873 }
dflet 6:778b081f6a13 874
dflet 6:778b081f6a13 875 bool cc3100::IS_STA_CONNECTED(uint32_t status_variable,const uint32_t bit){
dflet 6:778b081f6a13 876
dflet 6:778b081f6a13 877 g_Status = status_variable;
dflet 6:778b081f6a13 878
dflet 6:778b081f6a13 879 if(0 != (g_Status & ((uint32_t)1L<<(bit)))){
dflet 6:778b081f6a13 880 return TRUE;
dflet 6:778b081f6a13 881 }else{
dflet 6:778b081f6a13 882 return FALSE;
dflet 6:778b081f6a13 883 }
dflet 6:778b081f6a13 884 }
dflet 6:778b081f6a13 885
dflet 6:778b081f6a13 886 bool cc3100::IS_IP_ACQUIRED(uint32_t status_variable,const uint32_t bit){
dflet 6:778b081f6a13 887
dflet 6:778b081f6a13 888 g_Status = status_variable;
dflet 6:778b081f6a13 889
dflet 6:778b081f6a13 890 if(0 != (g_Status & ((uint32_t)1L<<(bit)))){
dflet 6:778b081f6a13 891 return TRUE;
dflet 6:778b081f6a13 892 }else{
dflet 6:778b081f6a13 893 return FALSE;
dflet 6:778b081f6a13 894 }
dflet 6:778b081f6a13 895 }
dflet 6:778b081f6a13 896
dflet 6:778b081f6a13 897 bool cc3100::IS_IP_LEASED(uint32_t status_variable,const uint32_t bit){
dflet 6:778b081f6a13 898
dflet 6:778b081f6a13 899 g_Status = status_variable;
dflet 6:778b081f6a13 900
dflet 6:778b081f6a13 901 if(0 != (g_Status & ((uint32_t)1L<<(bit)))){
dflet 6:778b081f6a13 902 return TRUE;
dflet 6:778b081f6a13 903 }else{
dflet 6:778b081f6a13 904 return FALSE;
dflet 6:778b081f6a13 905 }
dflet 6:778b081f6a13 906 }
dflet 6:778b081f6a13 907
dflet 6:778b081f6a13 908 bool cc3100::IS_CONNECTION_FAILED(uint32_t status_variable,const uint32_t bit){
dflet 6:778b081f6a13 909
dflet 6:778b081f6a13 910 g_Status = status_variable;
dflet 6:778b081f6a13 911
dflet 6:778b081f6a13 912 if(0 != (g_Status & ((uint32_t)1L<<(bit)))){
dflet 6:778b081f6a13 913 return TRUE;
dflet 6:778b081f6a13 914 }else{
dflet 6:778b081f6a13 915 return FALSE;
dflet 6:778b081f6a13 916 }
dflet 6:778b081f6a13 917 }
dflet 6:778b081f6a13 918
dflet 6:778b081f6a13 919 bool cc3100::IS_P2P_NEG_REQ_RECEIVED(uint32_t status_variable,const uint32_t bit){
dflet 6:778b081f6a13 920
dflet 6:778b081f6a13 921 g_Status = status_variable;
dflet 6:778b081f6a13 922
dflet 6:778b081f6a13 923 if(0 != (g_Status & ((uint32_t)1L<<(bit)))){
dflet 6:778b081f6a13 924 return TRUE;
dflet 6:778b081f6a13 925 }else{
dflet 6:778b081f6a13 926 return FALSE;
dflet 6:778b081f6a13 927 }
dflet 6:778b081f6a13 928 }
dflet 6:778b081f6a13 929
dflet 6:778b081f6a13 930 bool cc3100::IS_SMARTCONFIG_DONE(uint32_t status_variable,const uint32_t bit){
dflet 6:778b081f6a13 931
dflet 6:778b081f6a13 932 g_Status = status_variable;
dflet 6:778b081f6a13 933
dflet 6:778b081f6a13 934 if(0 != (g_Status & ((uint32_t)1L<<(bit)))){
dflet 6:778b081f6a13 935 return TRUE;
dflet 6:778b081f6a13 936 }else{
dflet 6:778b081f6a13 937 return FALSE;
dflet 6:778b081f6a13 938 }
dflet 6:778b081f6a13 939 }
dflet 6:778b081f6a13 940
dflet 6:778b081f6a13 941 bool cc3100::IS_SMARTCONFIG_STOPPED(uint32_t status_variable,const uint32_t bit){
dflet 6:778b081f6a13 942
dflet 6:778b081f6a13 943 g_Status = status_variable;
dflet 6:778b081f6a13 944
dflet 6:778b081f6a13 945 if(0 != (g_Status & ((uint32_t)1L<<(bit)))){
dflet 6:778b081f6a13 946 return TRUE;
dflet 6:778b081f6a13 947 }else{
dflet 6:778b081f6a13 948 return FALSE;
dflet 6:778b081f6a13 949 }
dflet 6:778b081f6a13 950 }
dflet 6:778b081f6a13 951
dflet 6:778b081f6a13 952 void cc3100::CLR_STATUS_BIT(uint32_t status_variable, const uint32_t bit){
dflet 6:778b081f6a13 953
dflet 6:778b081f6a13 954 g_Status = status_variable;
dflet 6:778b081f6a13 955 g_Status &= ~((uint32_t)1L<<(bit));
dflet 6:778b081f6a13 956
dflet 6:778b081f6a13 957 }
dflet 6:778b081f6a13 958
dflet 6:778b081f6a13 959 void cc3100::SET_STATUS_BIT(uint32_t status_variable, const uint32_t bit){
dflet 6:778b081f6a13 960
dflet 6:778b081f6a13 961 g_Status = status_variable;
dflet 6:778b081f6a13 962 g_Status |= ((uint32_t)1L<<(bit));
dflet 6:778b081f6a13 963
dflet 6:778b081f6a13 964 }
dflet 6:778b081f6a13 965
dflet 6:778b081f6a13 966 /*****************************************************************************/
dflet 6:778b081f6a13 967 /* sl_Task */
dflet 6:778b081f6a13 968 /*****************************************************************************/
dflet 6:778b081f6a13 969 #if _SL_INCLUDE_FUNC(sl_Task)
dflet 6:778b081f6a13 970 void cc3100::sl_Task(void)
dflet 6:778b081f6a13 971 {
dflet 6:778b081f6a13 972 #ifdef _SlTaskEntry
dflet 6:778b081f6a13 973 _nonos._SlNonOsMainLoopTask;
dflet 6:778b081f6a13 974
dflet 6:778b081f6a13 975 #endif
dflet 6:778b081f6a13 976 }
dflet 6:778b081f6a13 977 #endif
dflet 6:778b081f6a13 978
dflet 6:778b081f6a13 979 /*****************************************************************************/
dflet 6:778b081f6a13 980 /* sl_Start */
dflet 6:778b081f6a13 981 /*****************************************************************************/
dflet 6:778b081f6a13 982 #if _SL_INCLUDE_FUNC(sl_Start)
dflet 6:778b081f6a13 983 int16_t cc3100::sl_Start(const void* pIfHdl, int8_t* pDevName, const P_INIT_CALLBACK pInitCallBack)
dflet 6:778b081f6a13 984 {
dflet 6:778b081f6a13 985 int16_t ObjIdx = MAX_CONCURRENT_ACTIONS;
dflet 6:778b081f6a13 986 InitComplete_t AsyncRsp;
dflet 6:778b081f6a13 987
dflet 6:778b081f6a13 988 /* Perform any preprocessing before enable networking services */
dflet 6:778b081f6a13 989 sl_DeviceEnablePreamble();//stub only
dflet 6:778b081f6a13 990
dflet 6:778b081f6a13 991 /* ControlBlock init */
dflet 6:778b081f6a13 992 _driver._SlDrvDriverCBInit();
dflet 6:778b081f6a13 993
dflet 6:778b081f6a13 994 /* open the interface: usually SPI or UART */
dflet 6:778b081f6a13 995 if (NULL == pIfHdl)
dflet 6:778b081f6a13 996 {
dflet 6:778b081f6a13 997 g_pCB->FD = _spi.spi_Open((int8_t *)pDevName, 0);
dflet 6:778b081f6a13 998 }
dflet 6:778b081f6a13 999 else
dflet 6:778b081f6a13 1000 {
dflet 6:778b081f6a13 1001 g_pCB->FD = (_SlFd_t)pIfHdl;
dflet 6:778b081f6a13 1002 }
dflet 6:778b081f6a13 1003
dflet 6:778b081f6a13 1004 ObjIdx = _driver._SlDrvProtectAsyncRespSetting((uint8_t *)&AsyncRsp, START_STOP_ID, SL_MAX_SOCKETS);
dflet 6:778b081f6a13 1005
dflet 6:778b081f6a13 1006 if (MAX_CONCURRENT_ACTIONS == ObjIdx)
dflet 6:778b081f6a13 1007 {
dflet 6:778b081f6a13 1008 printf("SL_POOL_IS_EMPTY\r\n");
dflet 6:778b081f6a13 1009 return SL_POOL_IS_EMPTY;
dflet 6:778b081f6a13 1010 }
dflet 6:778b081f6a13 1011
dflet 6:778b081f6a13 1012 if( g_pCB->FD >= (_SlFd_t)0) {
dflet 6:778b081f6a13 1013 _spi.CC3100_disable();
dflet 6:778b081f6a13 1014
dflet 6:778b081f6a13 1015 g_pCB->pInitCallback = pInitCallBack;
dflet 6:778b081f6a13 1016
dflet 6:778b081f6a13 1017 _spi.CC3100_enable();
dflet 6:778b081f6a13 1018
dflet 6:778b081f6a13 1019 if (NULL == pInitCallBack) {
dflet 6:778b081f6a13 1020
dflet 6:778b081f6a13 1021 _driver._SlDrvSyncObjWaitForever(&g_pCB->ObjPool[ObjIdx].SyncObj);
dflet 6:778b081f6a13 1022
dflet 6:778b081f6a13 1023 /*release Pool Object*/
dflet 6:778b081f6a13 1024 _driver._SlDrvReleasePoolObj(g_pCB->FunctionParams.AsyncExt.ActionIndex);
dflet 6:778b081f6a13 1025 return _sl_GetStartResponseConvert(AsyncRsp.Status);
dflet 6:778b081f6a13 1026 }
dflet 6:778b081f6a13 1027 else
dflet 6:778b081f6a13 1028 {
dflet 6:778b081f6a13 1029 return SL_RET_CODE_OK;
dflet 6:778b081f6a13 1030 }
dflet 6:778b081f6a13 1031 }
dflet 6:778b081f6a13 1032
dflet 6:778b081f6a13 1033 return SL_BAD_INTERFACE;
dflet 6:778b081f6a13 1034
dflet 6:778b081f6a13 1035
dflet 6:778b081f6a13 1036 }
dflet 6:778b081f6a13 1037 #endif
dflet 6:778b081f6a13 1038
dflet 6:778b081f6a13 1039 /***************************************************************************
dflet 6:778b081f6a13 1040 _sl_HandleAsync_InitComplete - handles init complete signalling to
dflet 6:778b081f6a13 1041 a waiting object
dflet 6:778b081f6a13 1042 ****************************************************************************/
dflet 6:778b081f6a13 1043 void cc3100::_sl_HandleAsync_InitComplete(void *pVoidBuf)
dflet 6:778b081f6a13 1044 {
dflet 6:778b081f6a13 1045
dflet 6:778b081f6a13 1046 InitComplete_t *pMsgArgs = (InitComplete_t *)_SL_RESP_ARGS_START(pVoidBuf);
dflet 6:778b081f6a13 1047
dflet 6:778b081f6a13 1048 _driver._SlDrvProtectionObjLockWaitForever();
dflet 6:778b081f6a13 1049
dflet 6:778b081f6a13 1050 if(g_pCB->pInitCallback) {
dflet 6:778b081f6a13 1051 g_pCB->pInitCallback(_sl_GetStartResponseConvert(pMsgArgs->Status));
dflet 6:778b081f6a13 1052 } else {
dflet 6:778b081f6a13 1053 memcpy(g_pCB->ObjPool[g_pCB->FunctionParams.AsyncExt.ActionIndex].pRespArgs, pMsgArgs, sizeof(InitComplete_t));
dflet 6:778b081f6a13 1054 _driver._SlDrvSyncObjSignal(&g_pCB->ObjPool[g_pCB->FunctionParams.AsyncExt.ActionIndex].SyncObj);
dflet 6:778b081f6a13 1055 }
dflet 6:778b081f6a13 1056 _driver._SlDrvProtectionObjUnLock();
dflet 6:778b081f6a13 1057
dflet 6:778b081f6a13 1058 if(g_pCB->pInitCallback) {
dflet 6:778b081f6a13 1059 _driver._SlDrvReleasePoolObj(g_pCB->FunctionParams.AsyncExt.ActionIndex);
dflet 6:778b081f6a13 1060 }
dflet 6:778b081f6a13 1061
dflet 6:778b081f6a13 1062 }
dflet 6:778b081f6a13 1063
dflet 6:778b081f6a13 1064 /*****************************************************************************
dflet 6:778b081f6a13 1065 sl_stop
dflet 6:778b081f6a13 1066 ******************************************************************************/
dflet 6:778b081f6a13 1067 typedef union {
dflet 6:778b081f6a13 1068 _DevStopCommand_t Cmd;
dflet 6:778b081f6a13 1069 _BasicResponse_t Rsp;
dflet 6:778b081f6a13 1070 } _SlStopMsg_u;
dflet 6:778b081f6a13 1071
dflet 6:778b081f6a13 1072 const _SlCmdCtrl_t _SlStopCmdCtrl = {
dflet 6:778b081f6a13 1073 SL_OPCODE_DEVICE_STOP_COMMAND,
dflet 6:778b081f6a13 1074 sizeof(_DevStopCommand_t),
dflet 6:778b081f6a13 1075 sizeof(_BasicResponse_t)
dflet 6:778b081f6a13 1076 };
dflet 6:778b081f6a13 1077
dflet 6:778b081f6a13 1078 #if _SL_INCLUDE_FUNC(sl_Stop)
dflet 6:778b081f6a13 1079 int16_t cc3100::sl_Stop(const uint16_t timeout)
dflet 6:778b081f6a13 1080 {
dflet 6:778b081f6a13 1081 int16_t RetVal=0;
dflet 6:778b081f6a13 1082 _SlStopMsg_u Msg;
dflet 6:778b081f6a13 1083 _BasicResponse_t AsyncRsp;
dflet 6:778b081f6a13 1084 int8_t ObjIdx = MAX_CONCURRENT_ACTIONS;
dflet 6:778b081f6a13 1085 /* if timeout is 0 the shutdown is forced immediately */
dflet 6:778b081f6a13 1086 if( 0 == timeout ) {
dflet 6:778b081f6a13 1087 _spi.registerInterruptHandler(NULL, NULL);
dflet 6:778b081f6a13 1088 _spi.CC3100_disable();
dflet 6:778b081f6a13 1089 RetVal = _spi.spi_Close(g_pCB->FD);
dflet 6:778b081f6a13 1090
dflet 6:778b081f6a13 1091 } else {
dflet 6:778b081f6a13 1092 /* let the device make the shutdown using the defined timeout */
dflet 6:778b081f6a13 1093 Msg.Cmd.Timeout = timeout;
dflet 6:778b081f6a13 1094
dflet 6:778b081f6a13 1095
dflet 6:778b081f6a13 1096
dflet 6:778b081f6a13 1097
dflet 6:778b081f6a13 1098
dflet 6:778b081f6a13 1099 ObjIdx = _driver._SlDrvProtectAsyncRespSetting((uint8_t *)&AsyncRsp, START_STOP_ID, SL_MAX_SOCKETS);
dflet 6:778b081f6a13 1100 if (MAX_CONCURRENT_ACTIONS == ObjIdx)
dflet 6:778b081f6a13 1101 {
dflet 6:778b081f6a13 1102 return SL_POOL_IS_EMPTY;
dflet 6:778b081f6a13 1103 }
dflet 6:778b081f6a13 1104
dflet 6:778b081f6a13 1105 VERIFY_RET_OK(_driver._SlDrvCmdOp((_SlCmdCtrl_t *)&_SlStopCmdCtrl, &Msg, NULL));
dflet 6:778b081f6a13 1106
dflet 6:778b081f6a13 1107 if(SL_OS_RET_CODE_OK == (int16_t)Msg.Rsp.status)
dflet 6:778b081f6a13 1108 {
dflet 6:778b081f6a13 1109 _driver._SlDrvSyncObjWaitForever(&g_pCB->ObjPool[ObjIdx].SyncObj);
dflet 6:778b081f6a13 1110 Msg.Rsp.status = AsyncRsp.status;
dflet 6:778b081f6a13 1111 RetVal = Msg.Rsp.status;
dflet 6:778b081f6a13 1112 }
dflet 6:778b081f6a13 1113
dflet 6:778b081f6a13 1114 _driver._SlDrvReleasePoolObj((uint8_t)ObjIdx);
dflet 6:778b081f6a13 1115
dflet 6:778b081f6a13 1116 _spi.registerInterruptHandler(NULL, NULL);
dflet 6:778b081f6a13 1117 _spi.CC3100_disable();
dflet 6:778b081f6a13 1118 _spi.spi_Close(g_pCB->FD);
dflet 6:778b081f6a13 1119 }
dflet 6:778b081f6a13 1120 _driver._SlDrvDriverCBDeinit();
dflet 6:778b081f6a13 1121
dflet 6:778b081f6a13 1122 return RetVal;
dflet 6:778b081f6a13 1123 }
dflet 6:778b081f6a13 1124 #endif
dflet 6:778b081f6a13 1125
dflet 6:778b081f6a13 1126
dflet 6:778b081f6a13 1127 /*****************************************************************************
dflet 6:778b081f6a13 1128 sl_EventMaskSet
dflet 6:778b081f6a13 1129 *****************************************************************************/
dflet 6:778b081f6a13 1130 typedef union {
dflet 6:778b081f6a13 1131 _DevMaskEventSetCommand_t Cmd;
dflet 6:778b081f6a13 1132 _BasicResponse_t Rsp;
dflet 6:778b081f6a13 1133 } _SlEventMaskSetMsg_u;
dflet 6:778b081f6a13 1134
dflet 6:778b081f6a13 1135 #if _SL_INCLUDE_FUNC(sl_EventMaskSet)
dflet 6:778b081f6a13 1136 const _SlCmdCtrl_t _SlEventMaskSetCmdCtrl = {
dflet 6:778b081f6a13 1137 SL_OPCODE_DEVICE_EVENTMASKSET,
dflet 6:778b081f6a13 1138 sizeof(_DevMaskEventSetCommand_t),
dflet 6:778b081f6a13 1139 sizeof(_BasicResponse_t)
dflet 6:778b081f6a13 1140 };
dflet 6:778b081f6a13 1141
dflet 6:778b081f6a13 1142 int16_t cc3100::sl_EventMaskSet(uint8_t EventClass , uint32_t Mask)
dflet 6:778b081f6a13 1143 {
dflet 6:778b081f6a13 1144 _SlEventMaskSetMsg_u Msg;
dflet 6:778b081f6a13 1145 Msg.Cmd.group = EventClass;
dflet 6:778b081f6a13 1146 Msg.Cmd.mask = Mask;
dflet 6:778b081f6a13 1147
dflet 6:778b081f6a13 1148 VERIFY_RET_OK(_driver._SlDrvCmdOp((_SlCmdCtrl_t *)&_SlEventMaskSetCmdCtrl, &Msg, NULL));
dflet 6:778b081f6a13 1149
dflet 6:778b081f6a13 1150 return (int16_t)Msg.Rsp.status;
dflet 6:778b081f6a13 1151 }
dflet 6:778b081f6a13 1152 #endif
dflet 6:778b081f6a13 1153
dflet 6:778b081f6a13 1154 /******************************************************************************
dflet 6:778b081f6a13 1155 sl_EventMaskGet
dflet 6:778b081f6a13 1156 ******************************************************************************/
dflet 6:778b081f6a13 1157 typedef union {
dflet 6:778b081f6a13 1158 _DevMaskEventGetCommand_t Cmd;
dflet 6:778b081f6a13 1159 _DevMaskEventGetResponse_t Rsp;
dflet 6:778b081f6a13 1160 } _SlEventMaskGetMsg_u;
dflet 6:778b081f6a13 1161
dflet 6:778b081f6a13 1162 #if _SL_INCLUDE_FUNC(sl_EventMaskGet)
dflet 6:778b081f6a13 1163 const _SlCmdCtrl_t _SlEventMaskGetCmdCtrl = {
dflet 6:778b081f6a13 1164 SL_OPCODE_DEVICE_EVENTMASKGET,
dflet 6:778b081f6a13 1165 sizeof(_DevMaskEventGetCommand_t),
dflet 6:778b081f6a13 1166 sizeof(_DevMaskEventGetResponse_t)
dflet 6:778b081f6a13 1167 };
dflet 6:778b081f6a13 1168
dflet 6:778b081f6a13 1169 int16_t cc3100::sl_EventMaskGet(uint8_t EventClass, uint32_t *pMask)
dflet 6:778b081f6a13 1170 {
dflet 6:778b081f6a13 1171 _SlEventMaskGetMsg_u Msg;
dflet 6:778b081f6a13 1172
dflet 6:778b081f6a13 1173 Msg.Cmd.group = EventClass;
dflet 6:778b081f6a13 1174
dflet 6:778b081f6a13 1175 VERIFY_RET_OK(_driver._SlDrvCmdOp((_SlCmdCtrl_t *)&_SlEventMaskGetCmdCtrl, &Msg, NULL));
dflet 6:778b081f6a13 1176
dflet 6:778b081f6a13 1177 *pMask = Msg.Rsp.mask;
dflet 6:778b081f6a13 1178 return SL_RET_CODE_OK;
dflet 6:778b081f6a13 1179 }
dflet 6:778b081f6a13 1180 #endif
dflet 6:778b081f6a13 1181
dflet 6:778b081f6a13 1182 /******************************************************************************
dflet 6:778b081f6a13 1183 sl_DevGet
dflet 6:778b081f6a13 1184 ******************************************************************************/
dflet 6:778b081f6a13 1185
dflet 6:778b081f6a13 1186 typedef union {
dflet 6:778b081f6a13 1187 _DeviceSetGet_t Cmd;
dflet 6:778b081f6a13 1188 _DeviceSetGet_t Rsp;
dflet 6:778b081f6a13 1189 } _SlDeviceMsgGet_u;
dflet 6:778b081f6a13 1190
dflet 6:778b081f6a13 1191 #if _SL_INCLUDE_FUNC(sl_DevGet)
dflet 6:778b081f6a13 1192 const _SlCmdCtrl_t _SlDeviceGetCmdCtrl = {
dflet 6:778b081f6a13 1193 SL_OPCODE_DEVICE_DEVICEGET,
dflet 6:778b081f6a13 1194 sizeof(_DeviceSetGet_t),
dflet 6:778b081f6a13 1195 sizeof(_DeviceSetGet_t)
dflet 6:778b081f6a13 1196 };
dflet 6:778b081f6a13 1197
dflet 6:778b081f6a13 1198 int32_t cc3100::sl_DevGet(uint8_t DeviceGetId, uint8_t *pOption,uint8_t *pConfigLen, uint8_t *pValues)
dflet 6:778b081f6a13 1199 {
dflet 6:778b081f6a13 1200 _SlDeviceMsgGet_u Msg;
dflet 6:778b081f6a13 1201 _SlCmdExt_t CmdExt;
dflet 6:778b081f6a13 1202
dflet 6:778b081f6a13 1203 if (*pConfigLen == 0) {
dflet 6:778b081f6a13 1204 return SL_EZEROLEN;
dflet 6:778b081f6a13 1205 }
dflet 6:778b081f6a13 1206
dflet 6:778b081f6a13 1207 if( pOption ) {
dflet 6:778b081f6a13 1208 _driver._SlDrvResetCmdExt(&CmdExt);
dflet 6:778b081f6a13 1209 CmdExt.RxPayloadLen = *pConfigLen;
dflet 6:778b081f6a13 1210
dflet 6:778b081f6a13 1211 CmdExt.pRxPayload = (uint8_t *)pValues;
dflet 6:778b081f6a13 1212
dflet 6:778b081f6a13 1213
dflet 6:778b081f6a13 1214 Msg.Cmd.DeviceSetId = DeviceGetId;
dflet 6:778b081f6a13 1215
dflet 6:778b081f6a13 1216 Msg.Cmd.Option = (uint16_t)*pOption;
dflet 6:778b081f6a13 1217
dflet 6:778b081f6a13 1218 VERIFY_RET_OK(_driver._SlDrvCmdOp((_SlCmdCtrl_t *)&_SlDeviceGetCmdCtrl, &Msg, &CmdExt));
dflet 6:778b081f6a13 1219
dflet 6:778b081f6a13 1220 if( pOption ) {
dflet 6:778b081f6a13 1221 *pOption = (uint8_t)Msg.Rsp.Option;
dflet 6:778b081f6a13 1222 }
dflet 6:778b081f6a13 1223
dflet 6:778b081f6a13 1224 if (CmdExt.RxPayloadLen < CmdExt.ActualRxPayloadLen) {
dflet 6:778b081f6a13 1225 *pConfigLen = (uint8_t)CmdExt.RxPayloadLen;
dflet 6:778b081f6a13 1226 return SL_ESMALLBUF;
dflet 6:778b081f6a13 1227 } else {
dflet 6:778b081f6a13 1228 *pConfigLen = (uint8_t)CmdExt.ActualRxPayloadLen;
dflet 6:778b081f6a13 1229 }
dflet 6:778b081f6a13 1230
dflet 6:778b081f6a13 1231 return (int16_t)Msg.Rsp.Status;
dflet 6:778b081f6a13 1232 } else {
dflet 6:778b081f6a13 1233 return -1;
dflet 6:778b081f6a13 1234 }
dflet 6:778b081f6a13 1235 }
dflet 6:778b081f6a13 1236 #endif
dflet 6:778b081f6a13 1237
dflet 6:778b081f6a13 1238 /******************************************************************************
dflet 6:778b081f6a13 1239 sl_DevSet
dflet 6:778b081f6a13 1240 ******************************************************************************/
dflet 6:778b081f6a13 1241 typedef union {
dflet 6:778b081f6a13 1242 _DeviceSetGet_t Cmd;
dflet 6:778b081f6a13 1243 _BasicResponse_t Rsp;
dflet 6:778b081f6a13 1244 } _SlDeviceMsgSet_u;
dflet 6:778b081f6a13 1245
dflet 6:778b081f6a13 1246 #if _SL_INCLUDE_FUNC(sl_DevSet)
dflet 6:778b081f6a13 1247 const _SlCmdCtrl_t _SlDeviceSetCmdCtrl = {
dflet 6:778b081f6a13 1248 SL_OPCODE_DEVICE_DEVICESET,
dflet 6:778b081f6a13 1249 sizeof(_DeviceSetGet_t),
dflet 6:778b081f6a13 1250 sizeof(_BasicResponse_t)
dflet 6:778b081f6a13 1251 };
dflet 6:778b081f6a13 1252
dflet 6:778b081f6a13 1253 int32_t cc3100::sl_DevSet(const uint8_t DeviceSetId, const uint8_t Option, const uint8_t ConfigLen, const uint8_t *pValues)
dflet 6:778b081f6a13 1254 {
dflet 6:778b081f6a13 1255 _SlDeviceMsgSet_u Msg;
dflet 6:778b081f6a13 1256 _SlCmdExt_t CmdExt;
dflet 6:778b081f6a13 1257
dflet 6:778b081f6a13 1258 _driver._SlDrvResetCmdExt(&CmdExt);
dflet 6:778b081f6a13 1259 CmdExt.TxPayloadLen = (ConfigLen+3) & (~3);
dflet 6:778b081f6a13 1260
dflet 6:778b081f6a13 1261 CmdExt.pTxPayload = (uint8_t *)pValues;
dflet 6:778b081f6a13 1262
dflet 6:778b081f6a13 1263
dflet 6:778b081f6a13 1264
dflet 6:778b081f6a13 1265 Msg.Cmd.DeviceSetId = DeviceSetId;
dflet 6:778b081f6a13 1266 Msg.Cmd.ConfigLen = ConfigLen;
dflet 6:778b081f6a13 1267 Msg.Cmd.Option = Option;
dflet 6:778b081f6a13 1268
dflet 6:778b081f6a13 1269 VERIFY_RET_OK(_driver._SlDrvCmdOp((_SlCmdCtrl_t *)&_SlDeviceSetCmdCtrl, &Msg, &CmdExt));
dflet 6:778b081f6a13 1270
dflet 6:778b081f6a13 1271 return (int16_t)Msg.Rsp.status;
dflet 6:778b081f6a13 1272 }
dflet 6:778b081f6a13 1273 #endif
dflet 6:778b081f6a13 1274
dflet 6:778b081f6a13 1275 /******************************************************************************
dflet 6:778b081f6a13 1276 sl_UartSetMode
dflet 6:778b081f6a13 1277 ******************************************************************************/
dflet 6:778b081f6a13 1278 #ifdef SL_IF_TYPE_UART
dflet 6:778b081f6a13 1279 typedef union {
dflet 6:778b081f6a13 1280 _DevUartSetModeCommand_t Cmd;
dflet 6:778b081f6a13 1281 _DevUartSetModeResponse_t Rsp;
dflet 6:778b081f6a13 1282 } _SlUartSetModeMsg_u;
dflet 6:778b081f6a13 1283
dflet 6:778b081f6a13 1284 #if _SL_INCLUDE_FUNC(sl_UartSetMode)
dflet 6:778b081f6a13 1285 const _SlCmdCtrl_t _SlUartSetModeCmdCtrl = {
dflet 6:778b081f6a13 1286 SL_OPCODE_DEVICE_SETUARTMODECOMMAND,
dflet 6:778b081f6a13 1287 sizeof(_DevUartSetModeCommand_t),
dflet 6:778b081f6a13 1288 sizeof(_DevUartSetModeResponse_t)
dflet 6:778b081f6a13 1289 };
dflet 6:778b081f6a13 1290
dflet 6:778b081f6a13 1291 int16_t cc3100::sl_UartSetMode(const SlUartIfParams_t* pUartParams)
dflet 6:778b081f6a13 1292 {
dflet 6:778b081f6a13 1293 _SlUartSetModeMsg_u Msg;
dflet 6:778b081f6a13 1294 uint32_t magicCode = 0xFFFFFFFF;
dflet 6:778b081f6a13 1295
dflet 6:778b081f6a13 1296 Msg.Cmd.BaudRate = pUartParams->BaudRate;
dflet 6:778b081f6a13 1297 Msg.Cmd.FlowControlEnable = pUartParams->FlowControlEnable;
dflet 6:778b081f6a13 1298
dflet 6:778b081f6a13 1299
dflet 6:778b081f6a13 1300 VERIFY_RET_OK(_SlDrvCmdOp((_SlCmdCtrl_t *)&_SlUartSetModeCmdCtrl, &Msg, NULL));
dflet 6:778b081f6a13 1301
dflet 6:778b081f6a13 1302 /* cmd response OK, we can continue with the handshake */
dflet 6:778b081f6a13 1303 if (SL_RET_CODE_OK == Msg.Rsp.status) {
dflet 6:778b081f6a13 1304 sl_IfMaskIntHdlr();
dflet 6:778b081f6a13 1305
dflet 6:778b081f6a13 1306 /* Close the comm port */
dflet 6:778b081f6a13 1307 sl_IfClose(g_pCB->FD);
dflet 6:778b081f6a13 1308
dflet 6:778b081f6a13 1309 /* Re-open the comm port */
dflet 6:778b081f6a13 1310 sl_IfOpen((void * )pUartParams, UART_IF_OPEN_FLAG_RE_OPEN);
dflet 6:778b081f6a13 1311 sl_IfUnMaskIntHdlr();
dflet 6:778b081f6a13 1312
dflet 6:778b081f6a13 1313 /* send the magic code and wait for the response */
dflet 6:778b081f6a13 1314 sl_IfWrite(g_pCB->FD, (uint8_t* )&magicCode, 4);
dflet 6:778b081f6a13 1315
dflet 6:778b081f6a13 1316 magicCode = UART_SET_MODE_MAGIC_CODE;
dflet 6:778b081f6a13 1317 sl_IfWrite(g_pCB->FD, (uint8_t* )&magicCode, 4);
dflet 6:778b081f6a13 1318
dflet 6:778b081f6a13 1319 /* clear magic code */
dflet 6:778b081f6a13 1320 magicCode = 0;
dflet 6:778b081f6a13 1321
dflet 6:778b081f6a13 1322 /* wait (blocking) till the magic code to be returned from device */
dflet 6:778b081f6a13 1323 sl_IfRead(g_pCB->FD, (uint8_t* )&magicCode, 4);
dflet 6:778b081f6a13 1324
dflet 6:778b081f6a13 1325 /* check for the received magic code matching */
dflet 6:778b081f6a13 1326 if (UART_SET_MODE_MAGIC_CODE != magicCode) {
dflet 6:778b081f6a13 1327 _SL_ASSERT(0);
dflet 6:778b081f6a13 1328 }
dflet 6:778b081f6a13 1329 }
dflet 6:778b081f6a13 1330
dflet 6:778b081f6a13 1331 return (int16_t)Msg.Rsp.status;
dflet 6:778b081f6a13 1332 }
dflet 6:778b081f6a13 1333 #endif
dflet 6:778b081f6a13 1334 #endif
dflet 6:778b081f6a13 1335
dflet 6:778b081f6a13 1336 }//namespace
dflet 6:778b081f6a13 1337
dflet 6:778b081f6a13 1338