cc3000 driver with expanded buffers.

Fork of cc3000_hostdriver_mbedsocket by Martin Kojtal

Committer:
SolderSplashLabs
Date:
Thu Oct 03 21:00:00 2013 +0000
Revision:
23:fed7f64dd520
Parent:
16:f3676ae62f96
Child:
26:456f73ed2a75
Added separate debug messages and VT100 formatting

Who changed what in which revision?

UserRevisionLine numberNew contents of line
SolderSplashLabs 23:fed7f64dd520 1 /*****************************************************************************
SolderSplashLabs 23:fed7f64dd520 2 *
SolderSplashLabs 23:fed7f64dd520 3 * C++ interface/implementation created by Martin Kojtal (0xc0170). Thanks to
SolderSplashLabs 23:fed7f64dd520 4 * Jim Carver and Frank Vannieuwkerke for their inital cc3000 mbed port and
SolderSplashLabs 23:fed7f64dd520 5 * provided help.
SolderSplashLabs 23:fed7f64dd520 6 *
SolderSplashLabs 23:fed7f64dd520 7 * This version of "host driver" uses CC3000 Host Driver Implementation. Thus
SolderSplashLabs 23:fed7f64dd520 8 * read the following copyright:
SolderSplashLabs 23:fed7f64dd520 9 *
SolderSplashLabs 23:fed7f64dd520 10 * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/
SolderSplashLabs 23:fed7f64dd520 11 *
SolderSplashLabs 23:fed7f64dd520 12 * Redistribution and use in source and binary forms, with or without
SolderSplashLabs 23:fed7f64dd520 13 * modification, are permitted provided that the following conditions
SolderSplashLabs 23:fed7f64dd520 14 * are met:
SolderSplashLabs 23:fed7f64dd520 15 *
SolderSplashLabs 23:fed7f64dd520 16 * Redistributions of source code must retain the above copyright
SolderSplashLabs 23:fed7f64dd520 17 * notice, this list of conditions and the following disclaimer.
SolderSplashLabs 23:fed7f64dd520 18 *
SolderSplashLabs 23:fed7f64dd520 19 * Redistributions in binary form must reproduce the above copyright
SolderSplashLabs 23:fed7f64dd520 20 * notice, this list of conditions and the following disclaimer in the
SolderSplashLabs 23:fed7f64dd520 21 * documentation and/or other materials provided with the
SolderSplashLabs 23:fed7f64dd520 22 * distribution.
SolderSplashLabs 23:fed7f64dd520 23 *
SolderSplashLabs 23:fed7f64dd520 24 * Neither the name of Texas Instruments Incorporated nor the names of
SolderSplashLabs 23:fed7f64dd520 25 * its contributors may be used to endorse or promote products derived
SolderSplashLabs 23:fed7f64dd520 26 * from this software without specific prior written permission.
SolderSplashLabs 23:fed7f64dd520 27 *
SolderSplashLabs 23:fed7f64dd520 28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
SolderSplashLabs 23:fed7f64dd520 29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
SolderSplashLabs 23:fed7f64dd520 30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
SolderSplashLabs 23:fed7f64dd520 31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
SolderSplashLabs 23:fed7f64dd520 32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SolderSplashLabs 23:fed7f64dd520 33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
SolderSplashLabs 23:fed7f64dd520 34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
SolderSplashLabs 23:fed7f64dd520 35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
SolderSplashLabs 23:fed7f64dd520 36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
SolderSplashLabs 23:fed7f64dd520 37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
SolderSplashLabs 23:fed7f64dd520 38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
SolderSplashLabs 23:fed7f64dd520 39 *
SolderSplashLabs 23:fed7f64dd520 40 *****************************************************************************/
SolderSplashLabs 23:fed7f64dd520 41 #include "cc3000.h"
SolderSplashLabs 23:fed7f64dd520 42 #include "cc3000_event.h"
SolderSplashLabs 23:fed7f64dd520 43
SolderSplashLabs 23:fed7f64dd520 44 namespace mbed_cc3000 {
SolderSplashLabs 23:fed7f64dd520 45
SolderSplashLabs 23:fed7f64dd520 46 /* TODO this prefix remove? verify */
SolderSplashLabs 23:fed7f64dd520 47 static uint8_t cc3000_prefix[] = {'T', 'T', 'T'};
SolderSplashLabs 23:fed7f64dd520 48 cc3000 *cc3000::_inst;
SolderSplashLabs 23:fed7f64dd520 49
SolderSplashLabs 23:fed7f64dd520 50 cc3000::cc3000(PinName cc3000_irq, PinName cc3000_en, PinName cc3000_cs, SPI cc3000_spi, IRQn_Type irq_port)
SolderSplashLabs 23:fed7f64dd520 51 : _event(_simple_link, _hci, _spi, *this), _socket(_simple_link, _hci, _event), _spi(cc3000_irq, cc3000_en, cc3000_cs, cc3000_spi, irq_port, _event, _simple_link), _hci(_spi),
SolderSplashLabs 23:fed7f64dd520 52 _nvmem(_hci, _event, _simple_link), _netapp(_simple_link, _nvmem, _hci, _event), _wlan(_simple_link, _event, _spi, _hci) {
SolderSplashLabs 23:fed7f64dd520 53 /* TODO - pIRQ riorities ?? */
SolderSplashLabs 23:fed7f64dd520 54
SolderSplashLabs 23:fed7f64dd520 55 _simple_link.set_tx_complete_signal(1);
SolderSplashLabs 23:fed7f64dd520 56 _status.dhcp = 0;
SolderSplashLabs 23:fed7f64dd520 57 _status.connected = 0;
SolderSplashLabs 23:fed7f64dd520 58 _status.socket = 0;
SolderSplashLabs 23:fed7f64dd520 59 _status.dhcp_configured = 0;
SolderSplashLabs 23:fed7f64dd520 60 _status.smart_config_complete = 0;
SolderSplashLabs 23:fed7f64dd520 61 _status.stop_smart_config = 0;
SolderSplashLabs 23:fed7f64dd520 62 _status.ok_to_shut_down = 0;
SolderSplashLabs 23:fed7f64dd520 63
SolderSplashLabs 23:fed7f64dd520 64 _inst = this;
SolderSplashLabs 23:fed7f64dd520 65 }
SolderSplashLabs 23:fed7f64dd520 66
SolderSplashLabs 23:fed7f64dd520 67 cc3000::~cc3000() {
SolderSplashLabs 23:fed7f64dd520 68
SolderSplashLabs 23:fed7f64dd520 69 }
SolderSplashLabs 23:fed7f64dd520 70
SolderSplashLabs 23:fed7f64dd520 71 void cc3000::usync_callback(int32_t event_type, uint8_t * data, uint8_t length) {
SolderSplashLabs 23:fed7f64dd520 72 if (event_type == HCI_EVNT_WLAN_ASYNC_SIMPLE_CONFIG_DONE)
SolderSplashLabs 23:fed7f64dd520 73 {
SolderSplashLabs 23:fed7f64dd520 74 _status.smart_config_complete = 1;
SolderSplashLabs 23:fed7f64dd520 75 _status.stop_smart_config = 1;
SolderSplashLabs 23:fed7f64dd520 76 }
SolderSplashLabs 23:fed7f64dd520 77
SolderSplashLabs 23:fed7f64dd520 78 if (event_type == HCI_EVNT_WLAN_UNSOL_CONNECT)
SolderSplashLabs 23:fed7f64dd520 79 {
SolderSplashLabs 23:fed7f64dd520 80 _status.connected = 1;
SolderSplashLabs 23:fed7f64dd520 81 }
SolderSplashLabs 23:fed7f64dd520 82
SolderSplashLabs 23:fed7f64dd520 83 if (event_type == HCI_EVNT_WLAN_UNSOL_DISCONNECT)
SolderSplashLabs 23:fed7f64dd520 84 {
SolderSplashLabs 23:fed7f64dd520 85 _status.connected = 0;
SolderSplashLabs 23:fed7f64dd520 86 _status.dhcp = 0;
SolderSplashLabs 23:fed7f64dd520 87 _status.dhcp_configured = 0;
SolderSplashLabs 23:fed7f64dd520 88 }
SolderSplashLabs 23:fed7f64dd520 89
SolderSplashLabs 23:fed7f64dd520 90 if (event_type == HCI_EVNT_WLAN_UNSOL_DHCP)
SolderSplashLabs 23:fed7f64dd520 91 {
SolderSplashLabs 23:fed7f64dd520 92 if ( *(data + NETAPP_IPCONFIG_MAC_OFFSET) == 0) {
SolderSplashLabs 23:fed7f64dd520 93 _status.dhcp = 1;
SolderSplashLabs 23:fed7f64dd520 94 } else {
SolderSplashLabs 23:fed7f64dd520 95 _status.dhcp = 0;
SolderSplashLabs 23:fed7f64dd520 96 }
SolderSplashLabs 23:fed7f64dd520 97 }
SolderSplashLabs 23:fed7f64dd520 98
SolderSplashLabs 23:fed7f64dd520 99 if (event_type == HCI_EVENT_CC3000_CAN_SHUT_DOWN)
SolderSplashLabs 23:fed7f64dd520 100 {
SolderSplashLabs 23:fed7f64dd520 101 _status.ok_to_shut_down = 1;
SolderSplashLabs 23:fed7f64dd520 102 }
SolderSplashLabs 23:fed7f64dd520 103
SolderSplashLabs 23:fed7f64dd520 104 if (event_type == HCI_EVNT_WLAN_ASYNC_PING_REPORT)
SolderSplashLabs 23:fed7f64dd520 105 {
SolderSplashLabs 23:fed7f64dd520 106 memcpy(&_ping_report, data, length);
SolderSplashLabs 23:fed7f64dd520 107 }
SolderSplashLabs 23:fed7f64dd520 108
SolderSplashLabs 23:fed7f64dd520 109 if (event_type == HCI_EVNT_BSD_TCP_CLOSE_WAIT) {
SolderSplashLabs 23:fed7f64dd520 110 uint8_t socketnum;
SolderSplashLabs 23:fed7f64dd520 111 socketnum = data[0];
SolderSplashLabs 23:fed7f64dd520 112 if (socketnum < MAX_SOCKETS) {
SolderSplashLabs 23:fed7f64dd520 113 _closed_sockets[socketnum] = true; /* clients socket is closed */
SolderSplashLabs 23:fed7f64dd520 114 }
SolderSplashLabs 23:fed7f64dd520 115 }
SolderSplashLabs 23:fed7f64dd520 116 }
SolderSplashLabs 23:fed7f64dd520 117
SolderSplashLabs 23:fed7f64dd520 118 void cc3000::start_smart_config(const uint8_t *smart_config_key) {
SolderSplashLabs 23:fed7f64dd520 119 // Reset all the previous configuration
SolderSplashLabs 23:fed7f64dd520 120 _wlan.ioctl_set_connection_policy(0, 0, 0);
SolderSplashLabs 23:fed7f64dd520 121 _wlan.ioctl_del_profile(255);
SolderSplashLabs 23:fed7f64dd520 122
SolderSplashLabs 23:fed7f64dd520 123 //Wait until CC3000 is disconected
SolderSplashLabs 23:fed7f64dd520 124 while (_status.connected == 1)
SolderSplashLabs 23:fed7f64dd520 125 {
SolderSplashLabs 23:fed7f64dd520 126 wait_us(5);
SolderSplashLabs 23:fed7f64dd520 127 _event.hci_unsolicited_event_handler();
SolderSplashLabs 23:fed7f64dd520 128 }
SolderSplashLabs 23:fed7f64dd520 129
SolderSplashLabs 23:fed7f64dd520 130 // Trigger the Smart Config process
SolderSplashLabs 23:fed7f64dd520 131
SolderSplashLabs 23:fed7f64dd520 132 _wlan.smart_config_set_prefix(cc3000_prefix);
SolderSplashLabs 23:fed7f64dd520 133 // Start the Smart Config process with AES disabled
SolderSplashLabs 23:fed7f64dd520 134 _wlan.smart_config_start(0);
SolderSplashLabs 23:fed7f64dd520 135
SolderSplashLabs 23:fed7f64dd520 136 DBG_CC("Waiting for smartconfig to be completed");
SolderSplashLabs 23:fed7f64dd520 137
SolderSplashLabs 23:fed7f64dd520 138 // Wait for Smart config finished
SolderSplashLabs 23:fed7f64dd520 139 while (_status.smart_config_complete == 0)
SolderSplashLabs 23:fed7f64dd520 140 {
SolderSplashLabs 23:fed7f64dd520 141 wait_ms(100);
SolderSplashLabs 23:fed7f64dd520 142
SolderSplashLabs 23:fed7f64dd520 143 }
SolderSplashLabs 23:fed7f64dd520 144
SolderSplashLabs 23:fed7f64dd520 145 DBG_CC("Smartconfig finished");
SolderSplashLabs 23:fed7f64dd520 146
SolderSplashLabs 23:fed7f64dd520 147 #ifndef CC3000_UNENCRYPTED_SMART_CONFIG
SolderSplashLabs 23:fed7f64dd520 148 // create new entry for AES encryption key
SolderSplashLabs 23:fed7f64dd520 149 _nvmem.create_entry(NVMEM_AES128_KEY_FILEID, 16);
SolderSplashLabs 23:fed7f64dd520 150 // write AES key to NVMEM
SolderSplashLabs 23:fed7f64dd520 151 _security.aes_write_key((uint8_t *)(&smart_config_key[0]));
SolderSplashLabs 23:fed7f64dd520 152 // Decrypt configuration information and add profile
SolderSplashLabs 23:fed7f64dd520 153 _wlan.smart_config_process();
SolderSplashLabs 23:fed7f64dd520 154 #endif
SolderSplashLabs 23:fed7f64dd520 155
SolderSplashLabs 23:fed7f64dd520 156 // Configure to connect automatically to the AP retrieved in the
SolderSplashLabs 23:fed7f64dd520 157 // Smart config process
SolderSplashLabs 23:fed7f64dd520 158 _wlan.ioctl_set_connection_policy(0, 1, 1);
SolderSplashLabs 23:fed7f64dd520 159
SolderSplashLabs 23:fed7f64dd520 160 // reset the CC3000
SolderSplashLabs 23:fed7f64dd520 161 _wlan.stop();
SolderSplashLabs 23:fed7f64dd520 162 wait(2);
SolderSplashLabs 23:fed7f64dd520 163 _wlan.start(0);
SolderSplashLabs 23:fed7f64dd520 164 wait(2);
SolderSplashLabs 23:fed7f64dd520 165
SolderSplashLabs 23:fed7f64dd520 166 // Mask out all non-required events
SolderSplashLabs 23:fed7f64dd520 167 _wlan.set_event_mask(HCI_EVNT_WLAN_KEEPALIVE|HCI_EVNT_WLAN_UNSOL_INIT|HCI_EVNT_WLAN_ASYNC_PING_REPORT);
SolderSplashLabs 23:fed7f64dd520 168 }
SolderSplashLabs 23:fed7f64dd520 169
SolderSplashLabs 23:fed7f64dd520 170 bool cc3000::connect_secure(const uint8_t *ssid, const uint8_t *key, int32_t security_mode) {
SolderSplashLabs 23:fed7f64dd520 171 uint32_t ret;
SolderSplashLabs 23:fed7f64dd520 172
SolderSplashLabs 23:fed7f64dd520 173 _wlan.disconnect();
SolderSplashLabs 23:fed7f64dd520 174 wait_ms(3);
SolderSplashLabs 23:fed7f64dd520 175 ret = _wlan.connect(security_mode, ssid, strlen((const char *)ssid), 0, (uint8_t *)key, strlen((const char *)key));
SolderSplashLabs 23:fed7f64dd520 176 if (ret == 0) { /* TODO static internal cc3000 state 0 to TRUE */
SolderSplashLabs 23:fed7f64dd520 177 ret = true;
SolderSplashLabs 23:fed7f64dd520 178 } else {
SolderSplashLabs 23:fed7f64dd520 179 ret = false;
SolderSplashLabs 23:fed7f64dd520 180 }
SolderSplashLabs 23:fed7f64dd520 181 return ret;
SolderSplashLabs 23:fed7f64dd520 182 }
SolderSplashLabs 23:fed7f64dd520 183
SolderSplashLabs 23:fed7f64dd520 184 bool cc3000::connect_to_AP(const uint8_t *ssid, const uint8_t *key, int32_t security_mode) {
SolderSplashLabs 23:fed7f64dd520 185 Timer t; /* TODO static? */
SolderSplashLabs 23:fed7f64dd520 186 bool ret = true;
SolderSplashLabs 23:fed7f64dd520 187
SolderSplashLabs 23:fed7f64dd520 188 t.start();
SolderSplashLabs 23:fed7f64dd520 189 while (is_connected() == false) {
SolderSplashLabs 23:fed7f64dd520 190 if (key == 0) {
SolderSplashLabs 23:fed7f64dd520 191 if (connect_open(ssid)) {
SolderSplashLabs 23:fed7f64dd520 192 break;
SolderSplashLabs 23:fed7f64dd520 193 }
SolderSplashLabs 23:fed7f64dd520 194 } else {
SolderSplashLabs 23:fed7f64dd520 195 #ifndef CC3000_TINY_DRIVER
SolderSplashLabs 23:fed7f64dd520 196 if (connect_secure(ssid,key,security_mode)) {
SolderSplashLabs 23:fed7f64dd520 197 break;
SolderSplashLabs 23:fed7f64dd520 198 }
SolderSplashLabs 23:fed7f64dd520 199 #else
SolderSplashLabs 23:fed7f64dd520 200 return false; /* secure connection not supported with TINY_DRIVER */
SolderSplashLabs 23:fed7f64dd520 201 #endif
SolderSplashLabs 23:fed7f64dd520 202 }
SolderSplashLabs 23:fed7f64dd520 203
SolderSplashLabs 23:fed7f64dd520 204 /* timeout 10 seconds */
SolderSplashLabs 23:fed7f64dd520 205 if (t.read_ms() > 10000){
SolderSplashLabs 23:fed7f64dd520 206 ret = false;
SolderSplashLabs 23:fed7f64dd520 207
SolderSplashLabs 23:fed7f64dd520 208 DBG_CC("Connection to AP failed");
SolderSplashLabs 23:fed7f64dd520 209
SolderSplashLabs 23:fed7f64dd520 210 break;
SolderSplashLabs 23:fed7f64dd520 211 }
SolderSplashLabs 23:fed7f64dd520 212 }
SolderSplashLabs 23:fed7f64dd520 213
SolderSplashLabs 23:fed7f64dd520 214 return ret;
SolderSplashLabs 23:fed7f64dd520 215 }
SolderSplashLabs 23:fed7f64dd520 216
SolderSplashLabs 23:fed7f64dd520 217 void cc3000::start(uint8_t patch) {
SolderSplashLabs 23:fed7f64dd520 218 _wlan.start(patch);
SolderSplashLabs 23:fed7f64dd520 219 _wlan.set_event_mask(HCI_EVNT_WLAN_UNSOL_INIT | HCI_EVNT_WLAN_KEEPALIVE);
SolderSplashLabs 23:fed7f64dd520 220 }
SolderSplashLabs 23:fed7f64dd520 221
SolderSplashLabs 23:fed7f64dd520 222 void cc3000::stop(void) {
SolderSplashLabs 23:fed7f64dd520 223 _wlan.stop();
SolderSplashLabs 23:fed7f64dd520 224 }
SolderSplashLabs 23:fed7f64dd520 225
SolderSplashLabs 23:fed7f64dd520 226 void cc3000::restart(uint8_t patch) {
SolderSplashLabs 23:fed7f64dd520 227 _wlan.stop();
SolderSplashLabs 23:fed7f64dd520 228 wait_ms(500);
SolderSplashLabs 23:fed7f64dd520 229 _wlan.start(patch);
SolderSplashLabs 23:fed7f64dd520 230 }
SolderSplashLabs 23:fed7f64dd520 231
SolderSplashLabs 23:fed7f64dd520 232 bool cc3000::connect_open(const uint8_t *ssid) {
SolderSplashLabs 23:fed7f64dd520 233 uint32_t ret;
SolderSplashLabs 23:fed7f64dd520 234
SolderSplashLabs 23:fed7f64dd520 235 _wlan.disconnect();
SolderSplashLabs 23:fed7f64dd520 236 wait_ms(3);
SolderSplashLabs 23:fed7f64dd520 237 #ifndef CC3000_TINY_DRIVER
SolderSplashLabs 23:fed7f64dd520 238 ret = _wlan.connect(0,ssid, strlen((const char *)ssid), 0, 0, 0);
SolderSplashLabs 23:fed7f64dd520 239 #else
SolderSplashLabs 23:fed7f64dd520 240 ret = _wlan.connect(ssid, strlen((const char *)ssid));
SolderSplashLabs 23:fed7f64dd520 241 #endif
SolderSplashLabs 23:fed7f64dd520 242 if (ret == 0) {
SolderSplashLabs 23:fed7f64dd520 243 ret = true;
SolderSplashLabs 23:fed7f64dd520 244 } else {
SolderSplashLabs 23:fed7f64dd520 245 ret = false;
SolderSplashLabs 23:fed7f64dd520 246 }
SolderSplashLabs 23:fed7f64dd520 247 return ret;
SolderSplashLabs 23:fed7f64dd520 248 }
SolderSplashLabs 23:fed7f64dd520 249
SolderSplashLabs 23:fed7f64dd520 250 bool cc3000::is_connected() {
SolderSplashLabs 23:fed7f64dd520 251 return _status.connected;
SolderSplashLabs 23:fed7f64dd520 252 }
SolderSplashLabs 23:fed7f64dd520 253
SolderSplashLabs 23:fed7f64dd520 254 bool cc3000::is_dhcp_configured() {
SolderSplashLabs 23:fed7f64dd520 255 return _status.dhcp;
SolderSplashLabs 23:fed7f64dd520 256 }
SolderSplashLabs 23:fed7f64dd520 257
SolderSplashLabs 23:fed7f64dd520 258 bool cc3000::is_smart_confing_completed() {
SolderSplashLabs 23:fed7f64dd520 259 return _status.smart_config_complete;
SolderSplashLabs 23:fed7f64dd520 260 }
SolderSplashLabs 23:fed7f64dd520 261
SolderSplashLabs 23:fed7f64dd520 262 uint8_t cc3000::get_mac_address(uint8_t address[6]) {
SolderSplashLabs 23:fed7f64dd520 263 return _nvmem.get_mac_address(address);
SolderSplashLabs 23:fed7f64dd520 264 }
SolderSplashLabs 23:fed7f64dd520 265
SolderSplashLabs 23:fed7f64dd520 266 uint8_t cc3000::set_mac_address(uint8_t address[6]) {
SolderSplashLabs 23:fed7f64dd520 267 return _nvmem.set_mac_address(address);
SolderSplashLabs 23:fed7f64dd520 268 }
SolderSplashLabs 23:fed7f64dd520 269
SolderSplashLabs 23:fed7f64dd520 270 void cc3000::get_user_file_info(uint8_t *info_file, size_t size) {
SolderSplashLabs 23:fed7f64dd520 271 _nvmem.read( NVMEM_USER_FILE_1_FILEID, size, 0, info_file);
SolderSplashLabs 23:fed7f64dd520 272 }
SolderSplashLabs 23:fed7f64dd520 273
SolderSplashLabs 23:fed7f64dd520 274 #ifndef CC3000_TINY_DRIVER
SolderSplashLabs 23:fed7f64dd520 275 bool cc3000::get_ip_config(tNetappIpconfigRetArgs *ip_config) {
SolderSplashLabs 23:fed7f64dd520 276 if ((_status.dhcp == false) || (_status.connected == false)) {
SolderSplashLabs 23:fed7f64dd520 277 return false;
SolderSplashLabs 23:fed7f64dd520 278 }
SolderSplashLabs 23:fed7f64dd520 279
SolderSplashLabs 23:fed7f64dd520 280 _netapp.ipconfig(ip_config);
SolderSplashLabs 23:fed7f64dd520 281 return true;
SolderSplashLabs 23:fed7f64dd520 282 }
SolderSplashLabs 23:fed7f64dd520 283 #endif
SolderSplashLabs 23:fed7f64dd520 284
SolderSplashLabs 23:fed7f64dd520 285 cc3000_client cc3000::create_tcp_client(uint32_t ip_address, uint16_t port) {
SolderSplashLabs 23:fed7f64dd520 286 sockaddr socket_address = {0};
SolderSplashLabs 23:fed7f64dd520 287 int32_t tcp_socket;
SolderSplashLabs 23:fed7f64dd520 288
SolderSplashLabs 23:fed7f64dd520 289 tcp_socket = _socket.socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
SolderSplashLabs 23:fed7f64dd520 290 if (tcp_socket == -1) {
SolderSplashLabs 23:fed7f64dd520 291 DBG_CC("Failed to create new socket (tcp)");
SolderSplashLabs 23:fed7f64dd520 292 return cc3000_client(*this);
SolderSplashLabs 23:fed7f64dd520 293 }
SolderSplashLabs 23:fed7f64dd520 294
SolderSplashLabs 23:fed7f64dd520 295 socket_address.family = AF_INET;
SolderSplashLabs 23:fed7f64dd520 296 socket_address.data[0] = (port & 0xFF00) >> 8;
SolderSplashLabs 23:fed7f64dd520 297 socket_address.data[1] = (port & 0x00FF);
SolderSplashLabs 23:fed7f64dd520 298 socket_address.data[2] = ip_address >> 24;
SolderSplashLabs 23:fed7f64dd520 299 socket_address.data[3] = ip_address >> 16;
SolderSplashLabs 23:fed7f64dd520 300 socket_address.data[4] = ip_address >> 8;
SolderSplashLabs 23:fed7f64dd520 301 socket_address.data[5] = ip_address;
SolderSplashLabs 23:fed7f64dd520 302
SolderSplashLabs 23:fed7f64dd520 303 if (_socket.connect(tcp_socket, &socket_address, sizeof(socket_address)) == -1) {
SolderSplashLabs 23:fed7f64dd520 304 DBG_CC("Failed to connect (tcp)");
SolderSplashLabs 23:fed7f64dd520 305 _socket.closesocket(tcp_socket);
SolderSplashLabs 23:fed7f64dd520 306 return cc3000_client(*this);
SolderSplashLabs 23:fed7f64dd520 307 }
SolderSplashLabs 23:fed7f64dd520 308
SolderSplashLabs 23:fed7f64dd520 309 return cc3000_client(*this, tcp_socket);
SolderSplashLabs 23:fed7f64dd520 310 }
SolderSplashLabs 23:fed7f64dd520 311 cc3000_client cc3000::create_udp_client(uint32_t ip_address, uint16_t port) {
SolderSplashLabs 23:fed7f64dd520 312 sockaddr socket_address = {0};
SolderSplashLabs 23:fed7f64dd520 313 int32_t udp_socket;
SolderSplashLabs 23:fed7f64dd520 314
SolderSplashLabs 23:fed7f64dd520 315 udp_socket = _socket.socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
SolderSplashLabs 23:fed7f64dd520 316 if (udp_socket == -1) {
SolderSplashLabs 23:fed7f64dd520 317 DBG_CC("Failed to create new socket (udp)");
SolderSplashLabs 23:fed7f64dd520 318 return cc3000_client(*this);
SolderSplashLabs 23:fed7f64dd520 319 }
SolderSplashLabs 23:fed7f64dd520 320
SolderSplashLabs 23:fed7f64dd520 321 socket_address.family = AF_INET;
SolderSplashLabs 23:fed7f64dd520 322 socket_address.data[0] = (port & 0xFF00) >> 8;
SolderSplashLabs 23:fed7f64dd520 323 socket_address.data[1] = (port & 0x00FF);
SolderSplashLabs 23:fed7f64dd520 324 socket_address.data[2] = ip_address >> 24;
SolderSplashLabs 23:fed7f64dd520 325 socket_address.data[3] = ip_address >> 16;
SolderSplashLabs 23:fed7f64dd520 326 socket_address.data[4] = ip_address >> 8;
SolderSplashLabs 23:fed7f64dd520 327 socket_address.data[5] = ip_address;
SolderSplashLabs 23:fed7f64dd520 328
SolderSplashLabs 23:fed7f64dd520 329 if (_socket.connect(udp_socket, &socket_address, sizeof(socket_address)) == -1) {
SolderSplashLabs 23:fed7f64dd520 330 DBG_CC("Failed to connect (udp)");
SolderSplashLabs 23:fed7f64dd520 331 _socket.closesocket(udp_socket);
SolderSplashLabs 23:fed7f64dd520 332 return cc3000_client(*this);
SolderSplashLabs 23:fed7f64dd520 333 }
SolderSplashLabs 23:fed7f64dd520 334
SolderSplashLabs 23:fed7f64dd520 335 return cc3000_client(*this, udp_socket);
SolderSplashLabs 23:fed7f64dd520 336 }
SolderSplashLabs 23:fed7f64dd520 337
SolderSplashLabs 23:fed7f64dd520 338 cc3000_server cc3000::create_tcp_server(uint32_t ip_address, uint16_t port) {
SolderSplashLabs 23:fed7f64dd520 339 sockaddr socket_address = {0};
SolderSplashLabs 23:fed7f64dd520 340 int32_t tcp_socket;
SolderSplashLabs 23:fed7f64dd520 341
SolderSplashLabs 23:fed7f64dd520 342 tcp_socket = _socket.socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
SolderSplashLabs 23:fed7f64dd520 343 if (tcp_socket == -1) {
SolderSplashLabs 23:fed7f64dd520 344 DBG_CC("Failed to create new socket.");
SolderSplashLabs 23:fed7f64dd520 345 return cc3000_server(*this, socket_address);
SolderSplashLabs 23:fed7f64dd520 346 }
SolderSplashLabs 23:fed7f64dd520 347
SolderSplashLabs 23:fed7f64dd520 348 socket_address.family = AF_INET;
SolderSplashLabs 23:fed7f64dd520 349 socket_address.data[0] = (port & 0xFF00) >> 8;
SolderSplashLabs 23:fed7f64dd520 350 socket_address.data[1] = (port & 0x00FF);
SolderSplashLabs 23:fed7f64dd520 351 socket_address.data[2] = ip_address >> 24;
SolderSplashLabs 23:fed7f64dd520 352 socket_address.data[3] = ip_address >> 16;
SolderSplashLabs 23:fed7f64dd520 353 socket_address.data[4] = ip_address >> 8;
SolderSplashLabs 23:fed7f64dd520 354 socket_address.data[5] = ip_address;
SolderSplashLabs 23:fed7f64dd520 355
SolderSplashLabs 23:fed7f64dd520 356 if (_socket.bind(tcp_socket, &socket_address, sizeof(socket_address)) != 0) {
SolderSplashLabs 23:fed7f64dd520 357 DBG_CC("Failed to bind the new socket");
SolderSplashLabs 23:fed7f64dd520 358 return cc3000_server(*this, socket_address);
SolderSplashLabs 23:fed7f64dd520 359 }
SolderSplashLabs 23:fed7f64dd520 360 if (_socket.listen(tcp_socket, 1) != 0) { /* 1 client */
SolderSplashLabs 23:fed7f64dd520 361 DBG_CC("Failed to listen on the new socket");
SolderSplashLabs 23:fed7f64dd520 362 return cc3000_server(*this, socket_address);
SolderSplashLabs 23:fed7f64dd520 363 }
SolderSplashLabs 23:fed7f64dd520 364
SolderSplashLabs 23:fed7f64dd520 365 return cc3000_server(*this, socket_address, tcp_socket);
SolderSplashLabs 23:fed7f64dd520 366 }
SolderSplashLabs 23:fed7f64dd520 367
SolderSplashLabs 23:fed7f64dd520 368 void cc3000::delete_profiles(void) {
SolderSplashLabs 23:fed7f64dd520 369 tUserFS user_info;
SolderSplashLabs 23:fed7f64dd520 370
SolderSplashLabs 23:fed7f64dd520 371 _wlan.ioctl_set_connection_policy(0, 0, 0);
SolderSplashLabs 23:fed7f64dd520 372 _wlan.ioctl_del_profile(255);
SolderSplashLabs 23:fed7f64dd520 373
SolderSplashLabs 23:fed7f64dd520 374 get_user_file_info((uint8_t *)&user_info, sizeof(user_info));
SolderSplashLabs 23:fed7f64dd520 375 user_info.FTC = 0;
SolderSplashLabs 23:fed7f64dd520 376 set_user_file_info((uint8_t *)&user_info, sizeof(user_info));
SolderSplashLabs 23:fed7f64dd520 377 }
SolderSplashLabs 23:fed7f64dd520 378
SolderSplashLabs 23:fed7f64dd520 379 void cc3000::set_user_file_info(uint8_t *info_file, size_t size) {
SolderSplashLabs 23:fed7f64dd520 380 _nvmem.write( NVMEM_USER_FILE_1_FILEID, size, 0, info_file);
SolderSplashLabs 23:fed7f64dd520 381 }
SolderSplashLabs 23:fed7f64dd520 382
SolderSplashLabs 23:fed7f64dd520 383 bool cc3000::disconnect(void){
SolderSplashLabs 23:fed7f64dd520 384 if (_wlan.disconnect()) {
SolderSplashLabs 23:fed7f64dd520 385 return false;
SolderSplashLabs 23:fed7f64dd520 386 } else {
SolderSplashLabs 23:fed7f64dd520 387 return true;
SolderSplashLabs 23:fed7f64dd520 388 }
SolderSplashLabs 23:fed7f64dd520 389 }
SolderSplashLabs 23:fed7f64dd520 390
SolderSplashLabs 23:fed7f64dd520 391 uint32_t cc3000::ping(uint32_t ip, uint8_t attempts, uint16_t timeout, uint8_t size) {
SolderSplashLabs 23:fed7f64dd520 392 uint32_t reversed_ip = (ip >> 24) | (ip >> 8) & 0xFF00 | (ip << 8) & 0xFF0000 | (ip << 24);
SolderSplashLabs 23:fed7f64dd520 393
SolderSplashLabs 23:fed7f64dd520 394 _ping_report.packets_received = 0;
SolderSplashLabs 23:fed7f64dd520 395 if (_netapp.ping_send(&reversed_ip, attempts, size, timeout) == -1) {
SolderSplashLabs 23:fed7f64dd520 396 DBG_CC("Failed to send ping");
SolderSplashLabs 23:fed7f64dd520 397 return 0;
SolderSplashLabs 23:fed7f64dd520 398 }
SolderSplashLabs 23:fed7f64dd520 399 wait_ms(timeout*attempts*2);
SolderSplashLabs 23:fed7f64dd520 400
SolderSplashLabs 23:fed7f64dd520 401 /* known issue of cc3000 - sent number is send + received */
SolderSplashLabs 23:fed7f64dd520 402 // TODO : Remove the Sent/recv'd counts until ti fix the firmware issue?
SolderSplashLabs 23:fed7f64dd520 403 DBG_CC("Sent: %d",_ping_report.packets_sent);
SolderSplashLabs 23:fed7f64dd520 404 DBG_CC("Received: %d",_ping_report.packets_received);
SolderSplashLabs 23:fed7f64dd520 405 DBG_CC("Min time: %d",_ping_report.min_round_time);
SolderSplashLabs 23:fed7f64dd520 406 DBG_CC("Max time: %d",_ping_report.max_round_time);
SolderSplashLabs 23:fed7f64dd520 407 DBG_CC("Avg time: %d",_ping_report.avg_round_time);
SolderSplashLabs 23:fed7f64dd520 408
SolderSplashLabs 23:fed7f64dd520 409 return _ping_report.packets_received;
SolderSplashLabs 23:fed7f64dd520 410 }
SolderSplashLabs 23:fed7f64dd520 411
SolderSplashLabs 23:fed7f64dd520 412 /* Conversion between uint types and C strings */
SolderSplashLabs 23:fed7f64dd520 413 uint8_t* UINT32_TO_STREAM_f (uint8_t *p, uint32_t u32)
SolderSplashLabs 23:fed7f64dd520 414 {
SolderSplashLabs 23:fed7f64dd520 415 *(p)++ = (uint8_t)(u32);
SolderSplashLabs 23:fed7f64dd520 416 *(p)++ = (uint8_t)((u32) >> 8);
SolderSplashLabs 23:fed7f64dd520 417 *(p)++ = (uint8_t)((u32) >> 16);
SolderSplashLabs 23:fed7f64dd520 418 *(p)++ = (uint8_t)((u32) >> 24);
SolderSplashLabs 23:fed7f64dd520 419 return p;
SolderSplashLabs 23:fed7f64dd520 420 }
SolderSplashLabs 23:fed7f64dd520 421
SolderSplashLabs 23:fed7f64dd520 422
SolderSplashLabs 23:fed7f64dd520 423 uint8_t* UINT16_TO_STREAM_f (uint8_t *p, uint16_t u16)
SolderSplashLabs 23:fed7f64dd520 424 {
SolderSplashLabs 23:fed7f64dd520 425 *(p)++ = (uint8_t)(u16);
SolderSplashLabs 23:fed7f64dd520 426 *(p)++ = (uint8_t)((u16) >> 8);
SolderSplashLabs 23:fed7f64dd520 427 return p;
SolderSplashLabs 23:fed7f64dd520 428 }
SolderSplashLabs 23:fed7f64dd520 429
SolderSplashLabs 23:fed7f64dd520 430
SolderSplashLabs 23:fed7f64dd520 431 uint16_t STREAM_TO_UINT16_f(uint8_t *p, uint16_t offset)
SolderSplashLabs 23:fed7f64dd520 432 {
SolderSplashLabs 23:fed7f64dd520 433 return (uint16_t)((uint16_t)((uint16_t)
SolderSplashLabs 23:fed7f64dd520 434 (*(p + offset + 1)) << 8) + (uint16_t)(*(p + offset)));
SolderSplashLabs 23:fed7f64dd520 435 }
SolderSplashLabs 23:fed7f64dd520 436
SolderSplashLabs 23:fed7f64dd520 437
SolderSplashLabs 23:fed7f64dd520 438 uint32_t STREAM_TO_UINT32_f(uint8_t *p, uint16_t offset)
SolderSplashLabs 23:fed7f64dd520 439 {
SolderSplashLabs 23:fed7f64dd520 440 return (uint32_t)((uint32_t)((uint32_t)
SolderSplashLabs 23:fed7f64dd520 441 (*(p + offset + 3)) << 24) + (uint32_t)((uint32_t)
SolderSplashLabs 23:fed7f64dd520 442 (*(p + offset + 2)) << 16) + (uint32_t)((uint32_t)
SolderSplashLabs 23:fed7f64dd520 443 (*(p + offset + 1)) << 8) + (uint32_t)(*(p + offset)));
SolderSplashLabs 23:fed7f64dd520 444 }
SolderSplashLabs 23:fed7f64dd520 445
SolderSplashLabs 23:fed7f64dd520 446 } /* end of mbed_cc3000 namespace */
SolderSplashLabs 23:fed7f64dd520 447