MBED_DEMOS / Mbed 2 deprecated Hello-mDS-cc3000-WiFi

Dependencies:   cc3000_hostdriver_mbedsocket mbed nsdl_lib

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "cc3000.h"
00003 #include "nsdl_support.h"
00004 #include "dbg.h"
00005 #include "UDPSocket.h"
00006 #include "Endpoint.h"
00007 
00008 // Include resources
00009 #include "light.h"
00010 
00011 using namespace mbed_cc3000;
00012 
00013 Serial pc(USBTX, USBRX); // tx, rx
00014 
00015 // ****************************************************************************
00016 // Configuration section
00017 
00018 
00019 // Ethernet configuration
00020 /* Define this to enable DHCP, otherwise manual address configuration is used */
00021 #define DHCP
00022 
00023 // default timeout values for DHCP, ARP, keepalive, disable socket timeout
00024 uint32_t dhcp = 14400;
00025 uint32_t arp = 3600;
00026 uint32_t keep_alive = 10;
00027 uint32_t inactivity = 0;
00028 int32_t to_rtn;
00029 
00030   
00031 /* Manual IP configurations, if DHCP not defined */
00032 #define IP      "10.0.0.199"
00033 #define MASK    "255.255.255.0"
00034 #define GW      "10.0.0.1"
00035 
00036 
00037 // NSP configuration
00038 /* Change this IP address to that of your NanoService Platform installation */
00039 static const char* NSP_ADDRESS = "23.102.162.118"; // coen296.cloudapp.net
00040 //static const char* NSP_ADDRESS = "192.168.1.200"; // local mDS server
00041 static const int NSP_PORT = 5683;
00042 char endpoint_name[24] = "mbedDEMO-";
00043 uint8_t ep_type[] = {"DEMO"};
00044 uint8_t lifetime_ptr[] = {"60"};
00045 
00046 // ****************************************************************************
00047 // Ethernet initialization
00048 
00049 //EthernetInterface eth;
00050 //cc3000 eth(D2, D7, D10, SPI(D11, D12, D13), "SCUGuest", "", NONE, false);//Sparkfun
00051 //cc3000 eth(D2, D7, D10, SPI(D11, D12, D13), "demo", "ARMDEMO1", WPA2, false);//Sparkfun
00052 cc3000 eth(D3, D5, D10, SPI(D11, D12, D13), "SCUGuest", "", NONE, false);//Adafruit
00053 
00054 static void ethernet_init()
00055 {
00056     
00057 // set timeout values to disable socket timeout  
00058 /*    pc.printf("Setting timeout values\r\n");
00059     to_rtn = eth._netapp.timeout_values(&dhcp, &arp, &keep_alive, &inactivity);
00060     pc.printf("timeout set: %d\r\n", to_rtn);
00061 */
00062     /* Initialize network */
00063 #ifdef DHCP
00064     NSDL_DEBUG("DHCP in use\r\n");
00065     eth.init();
00066     NSDL_DEBUG("eth.init\r\n");
00067 #else
00068     eth.init(IP, MASK, GW);
00069     NSDL_DEBUG("eth.init\r\n");
00070 #endif
00071     if(eth.connect(30000) == 0)
00072         pc.printf("Connect OK\n\r");
00073 
00074     NSDL_DEBUG("IP Address:%s ", eth.getIPAddress());
00075 }
00076 
00077 // ****************************************************************************
00078 // NSP initialization
00079 
00080 UDPSocket server;
00081 Endpoint nsp;
00082 bool UDP_blocking = false;
00083 unsigned int UDP_timeout = 1000;
00084 
00085 uint8_t macbytes[6] = {0};
00086 char MAC[20];
00087 
00088 static void nsp_init()
00089 {
00090     server.init();
00091     //pc.printf("socket init\r\n");
00092     server.bind(NSP_PORT);
00093     //pc.printf("socket bind\r\n");
00094     server.set_blocking(UDP_blocking, UDP_timeout);
00095 
00096     nsp.set_address(NSP_ADDRESS, NSP_PORT);
00097 
00098     eth.get_mac_address(macbytes);
00099     
00100     sprintf(MAC, "%02X%02X%02X%02X%02X%02X", macbytes[0], macbytes[1], macbytes[2], macbytes[3], macbytes[4], macbytes[5]);
00101     pc.printf("MAC: %s\r\n", MAC);
00102     
00103     strncat(endpoint_name, MAC, 12);
00104     
00105     NSDL_DEBUG("name: %s", endpoint_name);
00106     NSDL_DEBUG("NSP=%s - port %d\n", NSP_ADDRESS, NSP_PORT);
00107 
00108 }
00109 
00110 // ****************************************************************************
00111 // Resource creation
00112 
00113 static int create_resources()
00114 {
00115     sn_nsdl_resource_info_s *resource_ptr = NULL;
00116     sn_nsdl_ep_parameters_s *endpoint_ptr = NULL;
00117     
00118     NSDL_DEBUG("Creating resources");
00119 
00120     /* Create resources */
00121     resource_ptr = (sn_nsdl_resource_info_s*)nsdl_alloc(sizeof(sn_nsdl_resource_info_s));
00122     if(!resource_ptr)
00123         return 0;
00124     memset(resource_ptr, 0, sizeof(sn_nsdl_resource_info_s));
00125 
00126     resource_ptr->resource_parameters_ptr = (sn_nsdl_resource_parameters_s*)nsdl_alloc(sizeof(sn_nsdl_resource_parameters_s));
00127     if(!resource_ptr->resource_parameters_ptr)
00128     {
00129         nsdl_free(resource_ptr);
00130         return 0;
00131     }
00132     memset(resource_ptr->resource_parameters_ptr, 0, sizeof(sn_nsdl_resource_parameters_s));
00133 
00134     // Static resources
00135     nsdl_create_static_resource(resource_ptr, sizeof("3/0/0")-1, (uint8_t*) "3/0/0", 0, 0,  (uint8_t*) "mbedDEMO", sizeof("mbedDEMO")-1);
00136     nsdl_create_static_resource(resource_ptr, sizeof("3/0/1")-1, (uint8_t*) "3/0/1", 0, 0,  (uint8_t*) "DEMO", sizeof("DEMO")-1);
00137 
00138     // Dynamic resources
00139     create_light_resource(resource_ptr);
00140 
00141         /* Register with NSP */
00142     endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)endpoint_name, ep_type, lifetime_ptr);
00143     if(sn_nsdl_register_endpoint(endpoint_ptr) != 0)
00144         pc.printf("NSP registering failed\r\n");
00145     else
00146         pc.printf("NSP registering OK\r\n");
00147     nsdl_clean_register_endpoint(&endpoint_ptr);
00148 
00149     nsdl_free(resource_ptr->resource_parameters_ptr);
00150     nsdl_free(resource_ptr);
00151     return 1;
00152 }
00153 
00154 // ****************************************************************************
00155 // Program entry point
00156 
00157 int main()
00158 {
00159 
00160     NSDL_DEBUG("Hello mDS Demo Endpoint Application\n");
00161     
00162     // Initialize Ethernet interface first
00163     ethernet_init();
00164     
00165     // Initialize NSP node
00166     nsp_init();
00167     
00168     // Initialize NSDL stack
00169     nsdl_init();
00170     
00171     // Create NSDL resources
00172     create_resources();
00173     
00174     // Run the NSDL event loop (never returns)
00175     nsdl_event_loop();
00176 }