version_2.0

Dependents:   cc3000_ping_demo_try_2

Fork of cc3000_hostdriver_mbedsocket by Martin Kojtal

Committer:
Kojto
Date:
Thu Sep 19 07:55:14 2013 +0000
Revision:
0:615c697c33b0
Child:
3:ad95e296bfbf
initial commit

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 0:615c697c33b0 52 _nvmem(_hci, _event, _simple_link), _netapp(_simple_link, _nvmem, _hci, _event), _wlan(_simple_link, _event, _spi, _hci), _buffer(256) {
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
Kojto 0:615c697c33b0 136 #if (CC3000_DEBUG == 1)
Kojto 0:615c697c33b0 137 printf("DEBUG: Waiting for smartconfig to be completed.\n");
Kojto 0:615c697c33b0 138 #endif
Kojto 0:615c697c33b0 139 // Wait for Smart config finished
Kojto 0:615c697c33b0 140 while (_status.smart_config_complete == 0)
Kojto 0:615c697c33b0 141 {
Kojto 0:615c697c33b0 142 wait_ms(100);
Kojto 0:615c697c33b0 143
Kojto 0:615c697c33b0 144 }
Kojto 0:615c697c33b0 145 #if (CC3000_DEBUG == 1)
Kojto 0:615c697c33b0 146 printf("DEBUG: Smartconfig finished.\n");
Kojto 0:615c697c33b0 147 #endif
Kojto 0:615c697c33b0 148 #ifndef CC3000_UNENCRYPTED_SMART_CONFIG
Kojto 0:615c697c33b0 149 // create new entry for AES encryption key
Kojto 0:615c697c33b0 150 _nvmem.create_entry(NVMEM_AES128_KEY_FILEID, 16);
Kojto 0:615c697c33b0 151 // write AES key to NVMEM
Kojto 0:615c697c33b0 152 _security.aes_write_key((uint8_t *)(&smart_config_key[0]));
Kojto 0:615c697c33b0 153 // Decrypt configuration information and add profile
Kojto 0:615c697c33b0 154 _wlan.smart_config_process();
Kojto 0:615c697c33b0 155 #endif
Kojto 0:615c697c33b0 156
Kojto 0:615c697c33b0 157 // Configure to connect automatically to the AP retrieved in the
Kojto 0:615c697c33b0 158 // Smart config process
Kojto 0:615c697c33b0 159 _wlan.ioctl_set_connection_policy(0, 1, 1);
Kojto 0:615c697c33b0 160
Kojto 0:615c697c33b0 161 // reset the CC3000
Kojto 0:615c697c33b0 162 _wlan.stop();
Kojto 0:615c697c33b0 163 wait(2);
Kojto 0:615c697c33b0 164 _wlan.start(0);
Kojto 0:615c697c33b0 165 wait(2);
Kojto 0:615c697c33b0 166
Kojto 0:615c697c33b0 167 // Mask out all non-required events
Kojto 0:615c697c33b0 168 _wlan.set_event_mask(HCI_EVNT_WLAN_KEEPALIVE|HCI_EVNT_WLAN_UNSOL_INIT|HCI_EVNT_WLAN_ASYNC_PING_REPORT);
Kojto 0:615c697c33b0 169 }
Kojto 0:615c697c33b0 170
Kojto 0:615c697c33b0 171 bool cc3000::connect_secure(const uint8_t *ssid, const uint8_t *key, int32_t security_mode) {
Kojto 0:615c697c33b0 172 uint32_t ret;
Kojto 0:615c697c33b0 173
Kojto 0:615c697c33b0 174 _wlan.disconnect();
Kojto 0:615c697c33b0 175 wait_ms(3);
Kojto 0:615c697c33b0 176 ret = _wlan.connect(security_mode, ssid, strlen((const char *)ssid), 0, (uint8_t *)key, strlen((const char *)key));
Kojto 0:615c697c33b0 177 if (ret == 0) { /* TODO static internal cc3000 state 0 to TRUE */
Kojto 0:615c697c33b0 178 ret = true;
Kojto 0:615c697c33b0 179 } else {
Kojto 0:615c697c33b0 180 ret = false;
Kojto 0:615c697c33b0 181 }
Kojto 0:615c697c33b0 182 return ret;
Kojto 0:615c697c33b0 183 }
Kojto 0:615c697c33b0 184
Kojto 0:615c697c33b0 185 bool cc3000::connect_to_AP(const uint8_t *ssid, const uint8_t *key, int32_t security_mode) {
Kojto 0:615c697c33b0 186 Timer t; /* TODO static? */
Kojto 0:615c697c33b0 187 bool ret = true;
Kojto 0:615c697c33b0 188
Kojto 0:615c697c33b0 189 t.start();
Kojto 0:615c697c33b0 190 while (is_connected() == false) {
Kojto 0:615c697c33b0 191 if (key == 0) {
Kojto 0:615c697c33b0 192 if (connect_open(ssid)) {
Kojto 0:615c697c33b0 193 break;
Kojto 0:615c697c33b0 194 }
Kojto 0:615c697c33b0 195 } else {
Kojto 0:615c697c33b0 196 #ifndef CC3000_TINY_DRIVER
Kojto 0:615c697c33b0 197 if (connect_secure(ssid,key,security_mode)) {
Kojto 0:615c697c33b0 198 break;
Kojto 0:615c697c33b0 199 }
Kojto 0:615c697c33b0 200 #else
Kojto 0:615c697c33b0 201 return false; /* secure connection not supported with TINY_DRIVER */
Kojto 0:615c697c33b0 202 #endif
Kojto 0:615c697c33b0 203 }
Kojto 0:615c697c33b0 204
Kojto 0:615c697c33b0 205 /* timeout 10 seconds */
Kojto 0:615c697c33b0 206 if (t.read_ms() > 10000){
Kojto 0:615c697c33b0 207 ret = false;
Kojto 0:615c697c33b0 208 #if (CC3000_DEBUG == 1)
Kojto 0:615c697c33b0 209 printf("Connection to AP failed.\n");
Kojto 0:615c697c33b0 210 #endif
Kojto 0:615c697c33b0 211 break;
Kojto 0:615c697c33b0 212 }
Kojto 0:615c697c33b0 213 }
Kojto 0:615c697c33b0 214
Kojto 0:615c697c33b0 215 return ret;
Kojto 0:615c697c33b0 216 }
Kojto 0:615c697c33b0 217
Kojto 0:615c697c33b0 218 void cc3000::start(uint8_t patch) {
Kojto 0:615c697c33b0 219 _wlan.start(patch);
Kojto 0:615c697c33b0 220 _wlan.set_event_mask(HCI_EVNT_WLAN_UNSOL_INIT | HCI_EVNT_WLAN_KEEPALIVE);
Kojto 0:615c697c33b0 221 }
Kojto 0:615c697c33b0 222
Kojto 0:615c697c33b0 223 void cc3000::stop(void) {
Kojto 0:615c697c33b0 224 _wlan.stop();
Kojto 0:615c697c33b0 225 }
Kojto 0:615c697c33b0 226
Kojto 0:615c697c33b0 227 void cc3000::restart(uint8_t patch) {
Kojto 0:615c697c33b0 228 _wlan.stop();
Kojto 0:615c697c33b0 229 wait_ms(500);
Kojto 0:615c697c33b0 230 _wlan.start(patch);
Kojto 0:615c697c33b0 231 }
Kojto 0:615c697c33b0 232
Kojto 0:615c697c33b0 233 bool cc3000::connect_open(const uint8_t *ssid) {
Kojto 0:615c697c33b0 234 uint32_t ret;
Kojto 0:615c697c33b0 235
Kojto 0:615c697c33b0 236 _wlan.disconnect();
Kojto 0:615c697c33b0 237 wait_ms(3);
Kojto 0:615c697c33b0 238 #ifndef CC3000_TINY_DRIVER
Kojto 0:615c697c33b0 239 ret = _wlan.connect(0,ssid, strlen((const char *)ssid), 0, 0, 0);
Kojto 0:615c697c33b0 240 #else
Kojto 0:615c697c33b0 241 ret = _wlan.connect(ssid, strlen((const char *)ssid));
Kojto 0:615c697c33b0 242 #endif
Kojto 0:615c697c33b0 243 if (ret == 0) {
Kojto 0:615c697c33b0 244 ret = true;
Kojto 0:615c697c33b0 245 } else {
Kojto 0:615c697c33b0 246 ret = false;
Kojto 0:615c697c33b0 247 }
Kojto 0:615c697c33b0 248 return ret;
Kojto 0:615c697c33b0 249 }
Kojto 0:615c697c33b0 250
Kojto 0:615c697c33b0 251 bool cc3000::is_connected() {
Kojto 0:615c697c33b0 252 return _status.connected;
Kojto 0:615c697c33b0 253 }
Kojto 0:615c697c33b0 254
Kojto 0:615c697c33b0 255 bool cc3000::is_dhcp_configured() {
Kojto 0:615c697c33b0 256 return _status.dhcp;
Kojto 0:615c697c33b0 257 }
Kojto 0:615c697c33b0 258
Kojto 0:615c697c33b0 259 bool cc3000::is_smart_confing_completed() {
Kojto 0:615c697c33b0 260 return _status.smart_config_complete;
Kojto 0:615c697c33b0 261 }
Kojto 0:615c697c33b0 262
Kojto 0:615c697c33b0 263 void cc3000::get_mac_address(uint8_t address[6]) {
Kojto 0:615c697c33b0 264 _nvmem.get_mac_address(address);
Kojto 0:615c697c33b0 265 }
Kojto 0:615c697c33b0 266
Kojto 0:615c697c33b0 267 void cc3000::set_mac_address(uint8_t address[6]) {
Kojto 0:615c697c33b0 268 _nvmem.set_mac_address(address);
Kojto 0:615c697c33b0 269 }
Kojto 0:615c697c33b0 270
Kojto 0:615c697c33b0 271 void cc3000::get_user_file_info(uint8_t *info_file, size_t size) {
Kojto 0:615c697c33b0 272 _nvmem.read( NVMEM_USER_FILE_1_FILEID, size, 0, info_file);
Kojto 0:615c697c33b0 273 }
Kojto 0:615c697c33b0 274
Kojto 0:615c697c33b0 275 #ifndef CC3000_TINY_DRIVER
Kojto 0:615c697c33b0 276 bool cc3000::get_ip_config(tNetappIpconfigRetArgs *ip_config) {
Kojto 0:615c697c33b0 277 if ((_status.dhcp == false) || (_status.connected == false)) {
Kojto 0:615c697c33b0 278 return false;
Kojto 0:615c697c33b0 279 }
Kojto 0:615c697c33b0 280
Kojto 0:615c697c33b0 281 _netapp.ipconfig(ip_config);
Kojto 0:615c697c33b0 282 return true;
Kojto 0:615c697c33b0 283 }
Kojto 0:615c697c33b0 284 #endif
Kojto 0:615c697c33b0 285
Kojto 0:615c697c33b0 286 cc3000_client cc3000::create_tcp_client(uint32_t ip_address, uint16_t port) {
Kojto 0:615c697c33b0 287 sockaddr socket_address = {0};
Kojto 0:615c697c33b0 288 int32_t tcp_socket;
Kojto 0:615c697c33b0 289
Kojto 0:615c697c33b0 290 tcp_socket = _socket.socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
Kojto 0:615c697c33b0 291 if (tcp_socket == -1) {
Kojto 0:615c697c33b0 292 #if (CC3000_DEBUG == 1)
Kojto 0:615c697c33b0 293 printf("DEBUG: Failed to create new socket (tcp).\n");
Kojto 0:615c697c33b0 294 #endif
Kojto 0:615c697c33b0 295 return cc3000_client(*this);
Kojto 0:615c697c33b0 296 }
Kojto 0:615c697c33b0 297
Kojto 0:615c697c33b0 298 socket_address.family = AF_INET;
Kojto 0:615c697c33b0 299 socket_address.data[0] = (port & 0xFF00) >> 8;
Kojto 0:615c697c33b0 300 socket_address.data[1] = (port & 0x00FF);
Kojto 0:615c697c33b0 301 socket_address.data[2] = ip_address >> 24;
Kojto 0:615c697c33b0 302 socket_address.data[3] = ip_address >> 16;
Kojto 0:615c697c33b0 303 socket_address.data[4] = ip_address >> 8;
Kojto 0:615c697c33b0 304 socket_address.data[5] = ip_address;
Kojto 0:615c697c33b0 305
Kojto 0:615c697c33b0 306 if (_socket.connect(tcp_socket, &socket_address, sizeof(socket_address)) == -1) {
Kojto 0:615c697c33b0 307 #if (CC3000_DEBUG == 1)
Kojto 0:615c697c33b0 308 printf("DEBUG: Failed to connect (tcp).\n");
Kojto 0:615c697c33b0 309 #endif
Kojto 0:615c697c33b0 310 _socket.closesocket(tcp_socket);
Kojto 0:615c697c33b0 311 return cc3000_client(*this);
Kojto 0:615c697c33b0 312 }
Kojto 0:615c697c33b0 313
Kojto 0:615c697c33b0 314 return cc3000_client(*this, tcp_socket);
Kojto 0:615c697c33b0 315 }
Kojto 0:615c697c33b0 316 cc3000_client cc3000::create_udp_client(uint32_t ip_address, uint16_t port) {
Kojto 0:615c697c33b0 317 sockaddr socket_address = {0};
Kojto 0:615c697c33b0 318 int32_t udp_socket;
Kojto 0:615c697c33b0 319
Kojto 0:615c697c33b0 320 udp_socket = _socket.socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
Kojto 0:615c697c33b0 321 if (udp_socket == -1) {
Kojto 0:615c697c33b0 322 #if (CC3000_DEBUG == 1)
Kojto 0:615c697c33b0 323 printf("DEBUG: Failed to create new socket (udp).\n");
Kojto 0:615c697c33b0 324 #endif
Kojto 0:615c697c33b0 325 return cc3000_client(*this);
Kojto 0:615c697c33b0 326 }
Kojto 0:615c697c33b0 327
Kojto 0:615c697c33b0 328 socket_address.family = AF_INET;
Kojto 0:615c697c33b0 329 socket_address.data[0] = (port & 0xFF00) >> 8;
Kojto 0:615c697c33b0 330 socket_address.data[1] = (port & 0x00FF);
Kojto 0:615c697c33b0 331 socket_address.data[2] = ip_address >> 24;
Kojto 0:615c697c33b0 332 socket_address.data[3] = ip_address >> 16;
Kojto 0:615c697c33b0 333 socket_address.data[4] = ip_address >> 8;
Kojto 0:615c697c33b0 334 socket_address.data[5] = ip_address;
Kojto 0:615c697c33b0 335
Kojto 0:615c697c33b0 336 if (_socket.connect(udp_socket, &socket_address, sizeof(socket_address)) == -1) {
Kojto 0:615c697c33b0 337 #if (CC3000_DEBUG == 1)
Kojto 0:615c697c33b0 338 printf("DEBUG: Failed to connect (udp).\n");
Kojto 0:615c697c33b0 339 #endif
Kojto 0:615c697c33b0 340 _socket.closesocket(udp_socket);
Kojto 0:615c697c33b0 341 return cc3000_client(*this);
Kojto 0:615c697c33b0 342 }
Kojto 0:615c697c33b0 343
Kojto 0:615c697c33b0 344 return cc3000_client(*this, udp_socket);
Kojto 0:615c697c33b0 345 }
Kojto 0:615c697c33b0 346
Kojto 0:615c697c33b0 347 cc3000_server cc3000::create_tcp_server(uint32_t ip_address, uint16_t port) {
Kojto 0:615c697c33b0 348 sockaddr socket_address = {0};
Kojto 0:615c697c33b0 349 int32_t tcp_socket;
Kojto 0:615c697c33b0 350
Kojto 0:615c697c33b0 351 tcp_socket = _socket.socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
Kojto 0:615c697c33b0 352 if (tcp_socket == -1) {
Kojto 0:615c697c33b0 353 #if (CC3000_DEBUG == 1)
Kojto 0:615c697c33b0 354 printf("Failed to create new socket.\n");
Kojto 0:615c697c33b0 355 #endif
Kojto 0:615c697c33b0 356 return cc3000_server(*this, socket_address);
Kojto 0:615c697c33b0 357 }
Kojto 0:615c697c33b0 358
Kojto 0:615c697c33b0 359 socket_address.family = AF_INET;
Kojto 0:615c697c33b0 360 socket_address.data[0] = (port & 0xFF00) >> 8;
Kojto 0:615c697c33b0 361 socket_address.data[1] = (port & 0x00FF);
Kojto 0:615c697c33b0 362 socket_address.data[2] = ip_address >> 24;
Kojto 0:615c697c33b0 363 socket_address.data[3] = ip_address >> 16;
Kojto 0:615c697c33b0 364 socket_address.data[4] = ip_address >> 8;
Kojto 0:615c697c33b0 365 socket_address.data[5] = ip_address;
Kojto 0:615c697c33b0 366
Kojto 0:615c697c33b0 367 if (_socket.bind(tcp_socket, &socket_address, sizeof(socket_address)) != 0) {
Kojto 0:615c697c33b0 368 #if (CC3000_DEBUG == 1)
Kojto 0:615c697c33b0 369 printf("DEBUG: Failed to bind the new socket.\n");
Kojto 0:615c697c33b0 370 #endif
Kojto 0:615c697c33b0 371 return cc3000_server(*this, socket_address);
Kojto 0:615c697c33b0 372 }
Kojto 0:615c697c33b0 373 if (_socket.listen(tcp_socket, 1) != 0) { /* 1 client */
Kojto 0:615c697c33b0 374 #if (CC3000_DEBUG == 1)
Kojto 0:615c697c33b0 375 printf("DEBUG: Failed to listen on the new socket.\n");
Kojto 0:615c697c33b0 376 #endif
Kojto 0:615c697c33b0 377 return cc3000_server(*this, socket_address);
Kojto 0:615c697c33b0 378 }
Kojto 0:615c697c33b0 379
Kojto 0:615c697c33b0 380 return cc3000_server(*this, socket_address, tcp_socket);
Kojto 0:615c697c33b0 381 }
Kojto 0:615c697c33b0 382
Kojto 0:615c697c33b0 383 void cc3000::delete_profiles(void) {
Kojto 0:615c697c33b0 384 tUserFS user_info;
Kojto 0:615c697c33b0 385
Kojto 0:615c697c33b0 386 _wlan.ioctl_set_connection_policy(0, 0, 0);
Kojto 0:615c697c33b0 387 _wlan.ioctl_del_profile(255);
Kojto 0:615c697c33b0 388
Kojto 0:615c697c33b0 389 user_info.FTC = 0;
Kojto 0:615c697c33b0 390 set_user_file_info((uint8_t *)&user_info, sizeof(user_info));
Kojto 0:615c697c33b0 391 }
Kojto 0:615c697c33b0 392
Kojto 0:615c697c33b0 393 void cc3000::set_user_file_info(uint8_t *info_file, size_t size) {
Kojto 0:615c697c33b0 394 _nvmem.write( NVMEM_USER_FILE_1_FILEID, size, 0, info_file);
Kojto 0:615c697c33b0 395 }
Kojto 0:615c697c33b0 396
Kojto 0:615c697c33b0 397 bool cc3000::disconnect(void){
Kojto 0:615c697c33b0 398 if (_wlan.disconnect()) {
Kojto 0:615c697c33b0 399 return false;
Kojto 0:615c697c33b0 400 } else {
Kojto 0:615c697c33b0 401 return true;
Kojto 0:615c697c33b0 402 }
Kojto 0:615c697c33b0 403 }
Kojto 0:615c697c33b0 404
Kojto 0:615c697c33b0 405 uint32_t cc3000::ping(uint32_t ip, uint8_t attempts, uint16_t timeout, uint8_t size) {
Kojto 0:615c697c33b0 406 uint32_t reversed_ip = (ip >> 24) | (ip >> 8) & 0xFF00 | (ip << 8) & 0xFF0000 | (ip << 24);
Kojto 0:615c697c33b0 407
Kojto 0:615c697c33b0 408 _ping_report.packets_received = 0;
Kojto 0:615c697c33b0 409 if (_netapp.ping_send(&reversed_ip, attempts, size, timeout) == -1) {
Kojto 0:615c697c33b0 410 #if (CC3000_DEBUG == 1)
Kojto 0:615c697c33b0 411 printf("DEBUG: Failed to send ping.\n");
Kojto 0:615c697c33b0 412 #endif
Kojto 0:615c697c33b0 413 return 0;
Kojto 0:615c697c33b0 414 }
Kojto 0:615c697c33b0 415 wait_ms(timeout*attempts*2);
Kojto 0:615c697c33b0 416
Kojto 0:615c697c33b0 417 /* known issue of cc3000 - sent number is send + received */
Kojto 0:615c697c33b0 418 #if (CC3000_DEBUG == 1)
Kojto 0:615c697c33b0 419 printf("DEBUG: Sent: %d \n",_ping_report.packets_sent);
Kojto 0:615c697c33b0 420 printf("DEBUG: Received: %d \n",_ping_report.packets_received);
Kojto 0:615c697c33b0 421 printf("DEBUG: Min time: %d \n",_ping_report.min_round_time);
Kojto 0:615c697c33b0 422 printf("DEBUG: Max time: %d \n",_ping_report.max_round_time);
Kojto 0:615c697c33b0 423 printf("DEBUG: Avg time: %d \n",_ping_report.avg_round_time);
Kojto 0:615c697c33b0 424 #endif
Kojto 0:615c697c33b0 425
Kojto 0:615c697c33b0 426 return _ping_report.packets_received;
Kojto 0:615c697c33b0 427 }
Kojto 0:615c697c33b0 428
Kojto 0:615c697c33b0 429 int32_t cc3000::readable(void) {
Kojto 0:615c697c33b0 430 return _buffer.available();
Kojto 0:615c697c33b0 431 }
Kojto 0:615c697c33b0 432
Kojto 0:615c697c33b0 433 uint8_t cc3000::getc(void)
Kojto 0:615c697c33b0 434 {
Kojto 0:615c697c33b0 435 char c;
Kojto 0:615c697c33b0 436
Kojto 0:615c697c33b0 437 while (!_buffer.available());
Kojto 0:615c697c33b0 438 _buffer.dequeue(&c);
Kojto 0:615c697c33b0 439 return (uint8_t)c;
Kojto 0:615c697c33b0 440 }
Kojto 0:615c697c33b0 441
Kojto 0:615c697c33b0 442 /* Conversion between uint types and C strings */
Kojto 0:615c697c33b0 443 uint8_t* UINT32_TO_STREAM_f (uint8_t *p, uint32_t u32)
Kojto 0:615c697c33b0 444 {
Kojto 0:615c697c33b0 445 *(p)++ = (uint8_t)(u32);
Kojto 0:615c697c33b0 446 *(p)++ = (uint8_t)((u32) >> 8);
Kojto 0:615c697c33b0 447 *(p)++ = (uint8_t)((u32) >> 16);
Kojto 0:615c697c33b0 448 *(p)++ = (uint8_t)((u32) >> 24);
Kojto 0:615c697c33b0 449 return p;
Kojto 0:615c697c33b0 450 }
Kojto 0:615c697c33b0 451
Kojto 0:615c697c33b0 452
Kojto 0:615c697c33b0 453 uint8_t* UINT16_TO_STREAM_f (uint8_t *p, uint16_t u16)
Kojto 0:615c697c33b0 454 {
Kojto 0:615c697c33b0 455 *(p)++ = (uint8_t)(u16);
Kojto 0:615c697c33b0 456 *(p)++ = (uint8_t)((u16) >> 8);
Kojto 0:615c697c33b0 457 return p;
Kojto 0:615c697c33b0 458 }
Kojto 0:615c697c33b0 459
Kojto 0:615c697c33b0 460
Kojto 0:615c697c33b0 461 uint16_t STREAM_TO_UINT16_f(uint8_t *p, uint16_t offset)
Kojto 0:615c697c33b0 462 {
Kojto 0:615c697c33b0 463 return (uint16_t)((uint16_t)((uint16_t)
Kojto 0:615c697c33b0 464 (*(p + offset + 1)) << 8) + (uint16_t)(*(p + offset)));
Kojto 0:615c697c33b0 465 }
Kojto 0:615c697c33b0 466
Kojto 0:615c697c33b0 467
Kojto 0:615c697c33b0 468 uint32_t STREAM_TO_UINT32_f(uint8_t *p, uint16_t offset)
Kojto 0:615c697c33b0 469 {
Kojto 0:615c697c33b0 470 return (uint32_t)((uint32_t)((uint32_t)
Kojto 0:615c697c33b0 471 (*(p + offset + 3)) << 24) + (uint32_t)((uint32_t)
Kojto 0:615c697c33b0 472 (*(p + offset + 2)) << 16) + (uint32_t)((uint32_t)
Kojto 0:615c697c33b0 473 (*(p + offset + 1)) << 8) + (uint32_t)(*(p + offset)));
Kojto 0:615c697c33b0 474 }
Kojto 0:615c697c33b0 475
Kojto 0:615c697c33b0 476 } /* end of mbed_cc3000 namespace */
Kojto 0:615c697c33b0 477