version_2.0

Dependents:   cc3000_ping_demo_try_2

Fork of cc3000_hostdriver_mbedsocket by Martin Kojtal

Revision:
40:acb9324640c4
Parent:
37:3332f57b7f1e
Parent:
39:03ac37ab34eb
Child:
41:eb1999bd50fb
--- a/cc3000.cpp	Sat Oct 12 14:27:46 2013 +0200
+++ b/cc3000.cpp	Sat Oct 12 20:51:05 2013 +0000
@@ -60,6 +60,7 @@
     _status.smart_config_complete = 0;
     _status.stop_smart_config = 0;
     _status.ok_to_shut_down = 0;
+    _status.enabled = 0;
 
     _inst = this;
 }
@@ -68,6 +69,67 @@
 
 }
 
+#ifdef CC3000_ETH_COMPAT
+// Ethernet library compatible, functions return strings
+// Caches the ipconfig from the usync callback
+static char mac_addr[19];
+static char ip_addr[17] = "\0";
+static char gateway[17] = "\0";
+static char networkmask[17] = "\0";
+
+char* cc3000::getMACAddress() {
+    return mac_addr;
+}
+
+char* cc3000::getIPAddress() {
+    return ip_addr;
+}
+
+char* cc3000::getGateway() {
+    return gateway;
+}
+
+char* cc3000::getNetworkMask() {
+    return networkmask;
+}
+
+/* Copied from lwip , modified to accept an uint32*/
+static char *inet_ntoa_r(uint32_t s_addr, char *buf, int buflen)
+{
+  char inv[3];
+  char *rp;
+  uint8_t *ap;
+  uint8_t rem;
+  uint8_t n;
+  uint8_t i;
+  int len = 0;
+
+  rp = buf;
+  ap = (uint8_t *)&s_addr;
+  for(n = 0; n < 4; n++) {
+    i = 0;
+    do {
+      rem = *ap % (uint8_t)10;
+      *ap /= (uint8_t)10;
+      inv[i++] = '0' + rem;
+    } while(*ap);
+    while(i--) {
+      if (len++ >= buflen) {
+        return NULL;
+      }
+      *rp++ = inv[i];
+    }
+    if (len++ >= buflen) {
+      return NULL;
+    }
+    *rp++ = '.';
+    ap++;
+  }
+  *--rp = 0;
+  return buf;
+}
+#endif
+
 void cc3000::usync_callback(int32_t event_type, uint8_t * data, uint8_t length) {
     if (event_type == HCI_EVNT_WLAN_ASYNC_SIMPLE_CONFIG_DONE)
     {
@@ -78,6 +140,8 @@
     if (event_type == HCI_EVNT_WLAN_UNSOL_CONNECT)
     {
         _status.connected = 1;
+        // Connect message is always followed by a DHCP message, connection is not useable until then
+        _status.dhcp      = 0;
     }
 
     if (event_type == HCI_EVNT_WLAN_UNSOL_DISCONNECT)
@@ -89,6 +153,15 @@
 
     if (event_type == HCI_EVNT_WLAN_UNSOL_DHCP)
     {
+      #ifdef CC3000_ETH_COMPAT
+    
+      inet_ntoa_r( htonl(*((uint32_t *)(&data[NETAPP_IPCONFIG_IP_OFFSET]))), ip_addr, 17);
+      inet_ntoa_r( htonl(*((uint32_t *)(&data[NETAPP_IPCONFIG_GW_OFFSET]))), gateway, 17);
+      inet_ntoa_r( htonl(*((uint32_t *)(&data[NETAPP_IPCONFIG_SUBNET_OFFSET]))), networkmask, 17);
+      inet_ntoa_r( htonl(*((uint32_t *)(&data[NETAPP_IPCONFIG_MAC_OFFSET]))), mac_addr, 19);
+    
+      #endif
+      
       if ( *(data + NETAPP_IPCONFIG_MAC_OFFSET) == 0) {
           _status.dhcp = 1;
       } else {
@@ -116,6 +189,8 @@
 }
 
 void cc3000::start_smart_config(const uint8_t *smart_config_key) {
+
+    _status.smart_config_complete = 0;
     _wlan.ioctl_set_connection_policy(0, 0, 0);
 
     //Wait until CC3000 is disconected
@@ -151,13 +226,14 @@
 
     // Configure to connect automatically to the AP retrieved in the
     // Smart config process
-    _wlan.ioctl_set_connection_policy(0, 1, 1);
+    _wlan.ioctl_set_connection_policy(0, 0, 1);
 
     // reset the CC3000
     _wlan.stop();
-    wait(2);
+    _status.enabled = 0;
+    wait(5);
     _wlan.start(0);
-    wait(2);
+    _status.enabled = 1;
 
     // Mask out all non-required events
     _wlan.set_event_mask(HCI_EVNT_WLAN_KEEPALIVE | HCI_EVNT_WLAN_UNSOL_INIT | HCI_EVNT_WLAN_ASYNC_PING_REPORT);
@@ -177,6 +253,32 @@
     return ret;
 }
 
+bool cc3000::connect_non_blocking(const uint8_t *ssid, const uint8_t *key, int32_t security_mode) 
+{
+bool ret = false;
+
+    if (key == 0) 
+    {
+        if (connect_open(ssid)) 
+        {
+            ret = true;
+        }
+    }
+    else 
+    {
+    #ifndef CC3000_TINY_DRIVER
+        if (connect_secure(ssid,key,security_mode)) 
+        {
+            ret = true;
+        }
+    #else
+        /* secure connection not supported with TINY_DRIVER */
+    #endif
+    }
+    
+    return ret;
+}
+
 bool cc3000::connect_to_AP(const uint8_t *ssid, const uint8_t *key, int32_t security_mode) {
     Timer t;  /* TODO static? */
     bool ret = true;
@@ -212,17 +314,21 @@
 
 void cc3000::start(uint8_t patch) {
     _wlan.start(patch);
+    _status.enabled = 1;
     _wlan.set_event_mask(HCI_EVNT_WLAN_UNSOL_INIT | HCI_EVNT_WLAN_KEEPALIVE);
 }
 
 void cc3000::stop(void) {
     _wlan.stop();
+    _status.enabled = 0;
 }
 
 void cc3000::restart(uint8_t patch) {
     _wlan.stop();
+    _status.enabled = 0;
     wait_ms(500);
     _wlan.start(patch);
+    _status.enabled = 1;
 }
 
 bool cc3000::connect_open(const uint8_t *ssid) {
@@ -243,6 +349,11 @@
     return ret;
 }
 
+bool cc3000::is_enabled()
+{
+    return _status.enabled;
+}
+
 bool cc3000::is_connected() {
     return _status.connected;
 }