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