Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: cc3000_hostdriver_mbedsocket mbed-rtos mbed nsdl_lib
main.cpp
00001 #include "mbed.h" 00002 #include "cc3000.h" 00003 #include "nsdl_support.h" 00004 #include "dbg.h" 00005 #include "IAP.h" 00006 #include "UDPSocket.h" 00007 #include "Endpoint.h" 00008 00009 // Include resources 00010 #include "light.h" 00011 00012 using namespace mbed_cc3000; 00013 00014 Serial pc(USBTX, USBRX); // tx, rx 00015 00016 // **************************************************************************** 00017 // Configuration section 00018 00019 00020 // Ethernet configuration 00021 /* Define this to enable DHCP, otherwise manual address configuration is used */ 00022 #define DHCP 00023 00024 // default timeout values for DHCP, ARP, keepalive, disable socket timeout 00025 uint32_t dhcp = 14400; 00026 uint32_t arp = 3600; 00027 uint32_t keep_alive = 10; 00028 uint32_t inactivity = 0; 00029 int32_t to_rtn; 00030 00031 00032 /* Manual IP configurations, if DHCP not defined */ 00033 #define IP "10.0.0.199" 00034 #define MASK "255.255.255.0" 00035 #define GW "10.0.0.1" 00036 00037 extern "C" void mbed_mac_address(char *mac) 00038 { 00039 static char buf[64] = {0}; 00040 IAP iap; 00041 int32_t *block = iap.read_serial(); 00042 uint32_t serial_number[5] = {0}; 00043 00044 memset(buf, 0, sizeof(buf)); 00045 serial_number[0] = *(block); 00046 serial_number[1] = *(block+1); 00047 // we only want bottom 16 bits of word1 (MAC bits 32-47) 00048 // and bit 9 forced to 1, bit 8 forced to 0 00049 // Locally administered MAC, reduced conflicts 00050 // http://en.wikipedia.org/wiki/MAC_address 00051 //serial_number[0] |= 0x00000200; 00052 //serial_number[0] &= 0x0000FEFF; 00053 memcpy(mac, (uint8_t*) &serial_number[0], 6); 00054 mac[0] |= 0x02; 00055 mac[0] &= 0xFE; 00056 mac[5] |= 0x02; 00057 mac[5] &= 0xFE; 00058 00059 // snprintf(buf, 16, "%4X%08X", serial_number[0], serial_number[1]); 00060 } 00061 00062 00063 // NSP configuration 00064 /* Change this IP address to that of your NanoService Platform installation */ 00065 static const char* NSP_ADDRESS = "23.102.162.118"; // coen296.cloudapp.net 00066 //static const char* NSP_ADDRESS = "192.168.1.200"; // local mDS server 00067 static const int NSP_PORT = 5683; 00068 char endpoint_name[24] = "mbedDEMO-"; 00069 uint8_t ep_type[] = {"DEMO"}; 00070 uint8_t lifetime_ptr[] = {"60"}; 00071 00072 // **************************************************************************** 00073 // Ethernet initialization 00074 00075 //EthernetInterface eth; 00076 cc3000 eth(D2, D7, D10, SPI(D11, D12, D13), "ssid", "password", WPA2, false); 00077 00078 static void ethernet_init() 00079 { 00080 00081 // set timeout values to disable socket timeout 00082 /* pc.printf("Setting timeout values\r\n"); 00083 to_rtn = eth._netapp.timeout_values(&dhcp, &arp, &keep_alive, &inactivity); 00084 pc.printf("timeout set: %d\r\n", to_rtn); 00085 */ 00086 /* Initialize network */ 00087 #ifdef DHCP 00088 NSDL_DEBUG("DHCP in use\r\n"); 00089 eth.init(); 00090 NSDL_DEBUG("eth.init\r\n"); 00091 #else 00092 eth.init(IP, MASK, GW); 00093 NSDL_DEBUG("eth.init\r\n"); 00094 #endif 00095 if(eth.connect(30000) == 0) 00096 pc.printf("Connect OK\n\r"); 00097 00098 NSDL_DEBUG("IP Address:%s ", eth.getIPAddress()); 00099 } 00100 00101 // **************************************************************************** 00102 // NSP initialization 00103 00104 UDPSocket server; 00105 Endpoint nsp; 00106 bool UDP_blocking = false; 00107 unsigned int UDP_timeout = 1000; 00108 00109 char macbytes[6] = {0}; 00110 char MAC[20]; 00111 00112 static void nsp_init() 00113 { 00114 server.init(); 00115 server.bind(NSP_PORT); 00116 server.set_blocking(UDP_blocking, UDP_timeout); 00117 00118 nsp.set_address(NSP_ADDRESS, NSP_PORT); 00119 00120 mbed_mac_address(macbytes); 00121 00122 sprintf(MAC, "%02X%02X%02X%02X%02X%02X", macbytes[0], macbytes[1], macbytes[2], macbytes[3], macbytes[4], macbytes[5]); 00123 pc.printf("MAC: %s\r\n", MAC); 00124 00125 strncat(endpoint_name, MAC, 12); 00126 00127 NSDL_DEBUG("name: %s", endpoint_name); 00128 NSDL_DEBUG("NSP=%s - port %d\n", NSP_ADDRESS, NSP_PORT); 00129 00130 } 00131 00132 // **************************************************************************** 00133 // Resource creation 00134 00135 static int create_resources() 00136 { 00137 sn_nsdl_resource_info_s *resource_ptr = NULL; 00138 sn_nsdl_ep_parameters_s *endpoint_ptr = NULL; 00139 00140 NSDL_DEBUG("Creating resources"); 00141 00142 /* Create resources */ 00143 resource_ptr = (sn_nsdl_resource_info_s*)nsdl_alloc(sizeof(sn_nsdl_resource_info_s)); 00144 if(!resource_ptr) 00145 return 0; 00146 memset(resource_ptr, 0, sizeof(sn_nsdl_resource_info_s)); 00147 00148 resource_ptr->resource_parameters_ptr = (sn_nsdl_resource_parameters_s*)nsdl_alloc(sizeof(sn_nsdl_resource_parameters_s)); 00149 if(!resource_ptr->resource_parameters_ptr) 00150 { 00151 nsdl_free(resource_ptr); 00152 return 0; 00153 } 00154 memset(resource_ptr->resource_parameters_ptr, 0, sizeof(sn_nsdl_resource_parameters_s)); 00155 00156 // Static resources 00157 nsdl_create_static_resource(resource_ptr, sizeof("3/0/0")-1, (uint8_t*) "3/0/0", 0, 0, (uint8_t*) "mbedDEMO", sizeof("mbedDEMO")-1); 00158 nsdl_create_static_resource(resource_ptr, sizeof("3/0/1")-1, (uint8_t*) "3/0/1", 0, 0, (uint8_t*) "DEMO", sizeof("DEMO")-1); 00159 00160 // Dynamic resources 00161 create_light_resource(resource_ptr); 00162 00163 /* Register with NSP */ 00164 endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)endpoint_name, ep_type, lifetime_ptr); 00165 if(sn_nsdl_register_endpoint(endpoint_ptr) != 0) 00166 pc.printf("NSP registering failed\r\n"); 00167 else 00168 pc.printf("NSP registering OK\r\n"); 00169 nsdl_clean_register_endpoint(&endpoint_ptr); 00170 00171 nsdl_free(resource_ptr->resource_parameters_ptr); 00172 nsdl_free(resource_ptr); 00173 return 1; 00174 } 00175 00176 // **************************************************************************** 00177 // Program entry point 00178 00179 int main() 00180 { 00181 00182 NSDL_DEBUG("Hello mDS Demo Endpoint Application\n"); 00183 00184 // Initialize Ethernet interface first 00185 ethernet_init(); 00186 00187 // Initialize NSP node 00188 nsp_init(); 00189 00190 // Initialize NSDL stack 00191 nsdl_init(); 00192 00193 // Create NSDL resources 00194 create_resources(); 00195 00196 // Run the NSDL event loop (never returns) 00197 nsdl_event_loop(); 00198 }
Generated on Thu Jul 14 2022 10:47:00 by
1.7.2