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