version_2.0

Dependents:   cc3000_ping_demo_try_2

Fork of cc3000_hostdriver_mbedsocket by Martin Kojtal

Committer:
SolderSplashLabs
Date:
Wed Oct 02 16:00:41 2013 +0000
Revision:
16:f3676ae62f96
Parent:
14:28c8bbbea025
Parent:
12:1c2a856c618a
Child:
19:9fdf8b4e41bf
Child:
23:fed7f64dd520
Merged martins changes with mine

Who changed what in which revision?

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