Added mutex for multiple SPI devices on the same SPI bus
Fork of cc3000_hostdriver_mbedsocket by
cc3000.cpp@12:1c2a856c618a, 2013-10-02 (annotated)
- Committer:
- Martin Kojtal
- Date:
- Wed Oct 02 17:02:34 2013 +0200
- Revision:
- 12:1c2a856c618a
- Parent:
- 11:5e3771b29385
- Child:
- 16:f3676ae62f96
Debug messages (\r\n), socket bsd naming
Who changed what in which revision?
User | Revision | Line number | New 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 | |
Kojto | 0:615c697c33b0 | 136 | #if (CC3000_DEBUG == 1) |
Martin Kojtal |
12:1c2a856c618a | 137 | printf("DEBUG: Waiting for smartconfig to be completed. \r\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) |
Martin Kojtal |
12:1c2a856c618a | 146 | printf("DEBUG: Smartconfig finished.\r\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) |
Martin Kojtal |
12:1c2a856c618a | 209 | printf("Connection to AP failed.\r\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) |
Martin Kojtal |
12:1c2a856c618a | 293 | printf("DEBUG: Failed to create new socket (tcp).\r\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) |
Martin Kojtal |
12:1c2a856c618a | 308 | printf("DEBUG: Failed to connect (tcp).\r\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) |
Martin Kojtal |
12:1c2a856c618a | 323 | printf("DEBUG: Failed to create new socket (udp).\r\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) |
Martin Kojtal |
12:1c2a856c618a | 338 | printf("DEBUG: Failed to connect (udp).\r\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) |
Martin Kojtal |
12:1c2a856c618a | 354 | printf("Failed to create new socket.\r\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) |
Martin Kojtal |
12:1c2a856c618a | 369 | printf("DEBUG: Failed to bind the new socket.\r\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) |
Martin Kojtal |
12:1c2a856c618a | 375 | printf("DEBUG: Failed to listen on the new socket.\r\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 | 4:15b58c119a0a | 389 | get_user_file_info((uint8_t *)&user_info, sizeof(user_info)); |
Kojto | 0:615c697c33b0 | 390 | user_info.FTC = 0; |
Kojto | 0:615c697c33b0 | 391 | set_user_file_info((uint8_t *)&user_info, sizeof(user_info)); |
Kojto | 0:615c697c33b0 | 392 | } |
Kojto | 0:615c697c33b0 | 393 | |
Kojto | 0:615c697c33b0 | 394 | void cc3000::set_user_file_info(uint8_t *info_file, size_t size) { |
Kojto | 0:615c697c33b0 | 395 | _nvmem.write( NVMEM_USER_FILE_1_FILEID, size, 0, info_file); |
Kojto | 0:615c697c33b0 | 396 | } |
Kojto | 0:615c697c33b0 | 397 | |
Kojto | 0:615c697c33b0 | 398 | bool cc3000::disconnect(void){ |
Kojto | 0:615c697c33b0 | 399 | if (_wlan.disconnect()) { |
Kojto | 0:615c697c33b0 | 400 | return false; |
Kojto | 0:615c697c33b0 | 401 | } else { |
Kojto | 0:615c697c33b0 | 402 | return true; |
Kojto | 0:615c697c33b0 | 403 | } |
Kojto | 0:615c697c33b0 | 404 | } |
Kojto | 0:615c697c33b0 | 405 | |
Kojto | 0:615c697c33b0 | 406 | uint32_t cc3000::ping(uint32_t ip, uint8_t attempts, uint16_t timeout, uint8_t size) { |
Kojto | 0:615c697c33b0 | 407 | uint32_t reversed_ip = (ip >> 24) | (ip >> 8) & 0xFF00 | (ip << 8) & 0xFF0000 | (ip << 24); |
Kojto | 0:615c697c33b0 | 408 | |
Kojto | 0:615c697c33b0 | 409 | _ping_report.packets_received = 0; |
Kojto | 0:615c697c33b0 | 410 | if (_netapp.ping_send(&reversed_ip, attempts, size, timeout) == -1) { |
Kojto | 0:615c697c33b0 | 411 | #if (CC3000_DEBUG == 1) |
Martin Kojtal |
12:1c2a856c618a | 412 | printf("DEBUG: Failed to send ping.\r\n"); |
Kojto | 0:615c697c33b0 | 413 | #endif |
Kojto | 0:615c697c33b0 | 414 | return 0; |
Kojto | 0:615c697c33b0 | 415 | } |
Kojto | 0:615c697c33b0 | 416 | wait_ms(timeout*attempts*2); |
Kojto | 0:615c697c33b0 | 417 | |
Kojto | 0:615c697c33b0 | 418 | /* known issue of cc3000 - sent number is send + received */ |
Kojto | 0:615c697c33b0 | 419 | #if (CC3000_DEBUG == 1) |
SolderSplashLabs | 11:5e3771b29385 | 420 | printf("DEBUG: Sent: %d \r\n",_ping_report.packets_sent); |
SolderSplashLabs | 11:5e3771b29385 | 421 | printf("DEBUG: Received: %d \r\n",_ping_report.packets_received); |
SolderSplashLabs | 11:5e3771b29385 | 422 | printf("DEBUG: Min time: %d \r\n",_ping_report.min_round_time); |
SolderSplashLabs | 11:5e3771b29385 | 423 | printf("DEBUG: Max time: %d \r\n",_ping_report.max_round_time); |
SolderSplashLabs | 11:5e3771b29385 | 424 | printf("DEBUG: Avg time: %d \r\n",_ping_report.avg_round_time); |
Kojto | 0:615c697c33b0 | 425 | #endif |
Kojto | 0:615c697c33b0 | 426 | |
Kojto | 0:615c697c33b0 | 427 | return _ping_report.packets_received; |
Kojto | 0:615c697c33b0 | 428 | } |
Kojto | 0:615c697c33b0 | 429 | |
Kojto | 0:615c697c33b0 | 430 | /* Conversion between uint types and C strings */ |
Kojto | 0:615c697c33b0 | 431 | uint8_t* UINT32_TO_STREAM_f (uint8_t *p, uint32_t u32) |
Kojto | 0:615c697c33b0 | 432 | { |
Kojto | 0:615c697c33b0 | 433 | *(p)++ = (uint8_t)(u32); |
Kojto | 0:615c697c33b0 | 434 | *(p)++ = (uint8_t)((u32) >> 8); |
Kojto | 0:615c697c33b0 | 435 | *(p)++ = (uint8_t)((u32) >> 16); |
Kojto | 0:615c697c33b0 | 436 | *(p)++ = (uint8_t)((u32) >> 24); |
Kojto | 0:615c697c33b0 | 437 | return p; |
Kojto | 0:615c697c33b0 | 438 | } |
Kojto | 0:615c697c33b0 | 439 | |
Kojto | 0:615c697c33b0 | 440 | |
Kojto | 0:615c697c33b0 | 441 | uint8_t* UINT16_TO_STREAM_f (uint8_t *p, uint16_t u16) |
Kojto | 0:615c697c33b0 | 442 | { |
Kojto | 0:615c697c33b0 | 443 | *(p)++ = (uint8_t)(u16); |
Kojto | 0:615c697c33b0 | 444 | *(p)++ = (uint8_t)((u16) >> 8); |
Kojto | 0:615c697c33b0 | 445 | return p; |
Kojto | 0:615c697c33b0 | 446 | } |
Kojto | 0:615c697c33b0 | 447 | |
Kojto | 0:615c697c33b0 | 448 | |
Kojto | 0:615c697c33b0 | 449 | uint16_t STREAM_TO_UINT16_f(uint8_t *p, uint16_t offset) |
Kojto | 0:615c697c33b0 | 450 | { |
Kojto | 0:615c697c33b0 | 451 | return (uint16_t)((uint16_t)((uint16_t) |
Kojto | 0:615c697c33b0 | 452 | (*(p + offset + 1)) << 8) + (uint16_t)(*(p + offset))); |
Kojto | 0:615c697c33b0 | 453 | } |
Kojto | 0:615c697c33b0 | 454 | |
Kojto | 0:615c697c33b0 | 455 | |
Kojto | 0:615c697c33b0 | 456 | uint32_t STREAM_TO_UINT32_f(uint8_t *p, uint16_t offset) |
Kojto | 0:615c697c33b0 | 457 | { |
Kojto | 0:615c697c33b0 | 458 | return (uint32_t)((uint32_t)((uint32_t) |
Kojto | 0:615c697c33b0 | 459 | (*(p + offset + 3)) << 24) + (uint32_t)((uint32_t) |
Kojto | 0:615c697c33b0 | 460 | (*(p + offset + 2)) << 16) + (uint32_t)((uint32_t) |
Kojto | 0:615c697c33b0 | 461 | (*(p + offset + 1)) << 8) + (uint32_t)(*(p + offset))); |
Kojto | 0:615c697c33b0 | 462 | } |
Kojto | 0:615c697c33b0 | 463 | |
Kojto | 0:615c697c33b0 | 464 | } /* end of mbed_cc3000 namespace */ |
Kojto | 0:615c697c33b0 | 465 |