TI's CC3100. A test demo with very little testing done!

Dependencies:   mbed

Fork of cc3100_Test_Demo2 by 高 杨

Committer:
gaoyang
Date:
Sun Jul 30 06:32:08 2017 +0000
Revision:
9:b8da46b97970
Parent:
8:bb9cccbe84d7
DAS

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