Updated to use external spawn.

Fork of simplelink_V2 by David Fletcher

Committer:
dflet
Date:
Sat Jun 06 13:48:29 2015 +0000
Revision:
1:9b68e650b3f6
Parent:
0:1a07906111ec
Oppps

Who changed what in which revision?

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