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