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: EthernetInterface mbed-rtos mbed nsdl_lib
main.cpp
00001 #include "mbed.h" 00002 #include "EthernetInterface.h" 00003 #include "nsdl_support.h" 00004 #include "dbg.h" 00005 #include "IAP.h" 00006 00007 // Include resources 00008 #include "light.h" 00009 00010 Serial pc(USBTX, USBRX); // tx, rx 00011 00012 // **************************************************************************** 00013 // Configuration section 00014 00015 // Ethernet configuration 00016 /* Define this to enable DHCP, otherwise manual address configuration is used */ 00017 #define DHCP 00018 00019 /* Manual IP configurations, if DHCP not defined */ 00020 #define IP "10.0.0.199" 00021 #define MASK "255.255.255.0" 00022 #define GW "10.0.0.1" 00023 00024 extern "C" void mbed_mac_address(char *mac) 00025 { 00026 static char buf[64] = {0}; 00027 IAP iap; 00028 int32_t *block = iap.read_serial(); 00029 uint32_t serial_number[5] = {0}; 00030 00031 memset(buf, 0, sizeof(buf)); 00032 serial_number[0] = *(block); 00033 serial_number[1] = *(block+1); 00034 // we only want bottom 16 bits of word1 (MAC bits 32-47) 00035 // and bit 9 forced to 1, bit 8 forced to 0 00036 // Locally administered MAC, reduced conflicts 00037 // http://en.wikipedia.org/wiki/MAC_address 00038 //serial_number[0] |= 0x00000200; 00039 //serial_number[0] &= 0x0000FEFF; 00040 memcpy(mac, (uint8_t*) &serial_number[0], 6); 00041 mac[0] |= 0x02; 00042 mac[0] &= 0xFE; 00043 mac[5] |= 0x02; 00044 mac[5] &= 0xFE; 00045 00046 // snprintf(buf, 16, "%4X%08X", serial_number[0], serial_number[1]); 00047 } 00048 00049 00050 // NSP configuration 00051 /* Change this IP address to that of your NanoService Platform installation */ 00052 static const char* NSP_ADDRESS = "23.102.162.118"; // coen296.cloudapp.net 00053 //static const char* NSP_ADDRESS = "192.168.1.200"; // local mDS server 00054 static const int NSP_PORT = 5683; 00055 char endpoint_name[24] = "mbedDEMO-"; 00056 uint8_t ep_type[] = {"DEMO"}; 00057 uint8_t lifetime_ptr[] = {"60"}; 00058 00059 // **************************************************************************** 00060 // Ethernet initialization 00061 00062 EthernetInterface eth; 00063 static void ethernet_init() 00064 { 00065 00066 /* Initialize network */ 00067 #ifdef DHCP 00068 NSDL_DEBUG("DHCP in use\r\n"); 00069 eth.init(); 00070 NSDL_DEBUG("eth.init\r\n"); 00071 #else 00072 eth.init(IP, MASK, GW); 00073 NSDL_DEBUG("eth.init\r\n"); 00074 #endif 00075 if(eth.connect(30000) == 0) 00076 pc.printf("Connect OK\n\r"); 00077 00078 NSDL_DEBUG("IP Address:%s ", eth.getIPAddress()); 00079 } 00080 00081 // **************************************************************************** 00082 // NSP initialization 00083 00084 UDPSocket server; 00085 Endpoint nsp; 00086 00087 char macbytes[6] = {0}; 00088 char MAC[20]; 00089 00090 static void nsp_init() 00091 { 00092 server.init(); 00093 server.bind(NSP_PORT); 00094 00095 nsp.set_address(NSP_ADDRESS, NSP_PORT); 00096 00097 mbed_mac_address(macbytes); 00098 00099 sprintf(MAC, "%02X%02X%02X%02X%02X%02X", macbytes[0], macbytes[1], macbytes[2], macbytes[3], macbytes[4], macbytes[5]); 00100 pc.printf("MAC: %s\r\n", MAC); 00101 00102 strncat(endpoint_name, MAC, 12); 00103 00104 NSDL_DEBUG("name: %s", endpoint_name); 00105 NSDL_DEBUG("NSP=%s - port %d\n", NSP_ADDRESS, NSP_PORT); 00106 00107 } 00108 00109 // **************************************************************************** 00110 // Resource creation 00111 00112 static int create_resources() 00113 { 00114 sn_nsdl_resource_info_s *resource_ptr = NULL; 00115 sn_nsdl_ep_parameters_s *endpoint_ptr = NULL; 00116 00117 NSDL_DEBUG("Creating resources"); 00118 00119 /* Create resources */ 00120 resource_ptr = (sn_nsdl_resource_info_s*)nsdl_alloc(sizeof(sn_nsdl_resource_info_s)); 00121 if(!resource_ptr) 00122 return 0; 00123 memset(resource_ptr, 0, sizeof(sn_nsdl_resource_info_s)); 00124 00125 resource_ptr->resource_parameters_ptr = (sn_nsdl_resource_parameters_s*)nsdl_alloc(sizeof(sn_nsdl_resource_parameters_s)); 00126 if(!resource_ptr->resource_parameters_ptr) 00127 { 00128 nsdl_free(resource_ptr); 00129 return 0; 00130 } 00131 memset(resource_ptr->resource_parameters_ptr, 0, sizeof(sn_nsdl_resource_parameters_s)); 00132 00133 // Static resources 00134 nsdl_create_static_resource(resource_ptr, sizeof("3/0/0")-1, (uint8_t*) "3/0/0", 0, 0, (uint8_t*) "mbedDEMO", sizeof("mbedDEMO")-1); 00135 nsdl_create_static_resource(resource_ptr, sizeof("3/0/1")-1, (uint8_t*) "3/0/1", 0, 0, (uint8_t*) "DEMO", sizeof("DEMO")-1); 00136 00137 // Dynamic resources 00138 create_light_resource(resource_ptr); 00139 00140 /* Register with NSP */ 00141 endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)endpoint_name, ep_type, lifetime_ptr); 00142 if(sn_nsdl_register_endpoint(endpoint_ptr) != 0) 00143 pc.printf("NSP registering failed\r\n"); 00144 else 00145 pc.printf("NSP registering OK\r\n"); 00146 nsdl_clean_register_endpoint(&endpoint_ptr); 00147 00148 nsdl_free(resource_ptr->resource_parameters_ptr); 00149 nsdl_free(resource_ptr); 00150 return 1; 00151 } 00152 00153 // **************************************************************************** 00154 // Program entry point 00155 00156 int main() 00157 { 00158 00159 NSDL_DEBUG("Hello mDS Demo Endpoint Application\n"); 00160 00161 // Initialize Ethernet interface first 00162 ethernet_init(); 00163 00164 // Initialize NSP node 00165 nsp_init(); 00166 00167 // Initialize NSDL stack 00168 nsdl_init(); 00169 00170 // Create NSDL resources 00171 create_resources(); 00172 00173 // Run the NSDL event loop (never returns) 00174 nsdl_event_loop(); 00175 }
Generated on Wed Jul 13 2022 17:02:37 by
1.7.2