Arch Pro mbed Device Server demo exposes LEDs as LWM2M style object
Dependencies: EthernetInterface mbed-rtos mbed nsdl_lib
main.cpp@0:f7f236605c98, 2014-10-11 (annotated)
- Committer:
- michaeljkoster
- Date:
- Sat Oct 11 06:14:44 2014 +0000
- Revision:
- 0:f7f236605c98
Initial Drop for SCU Class Demo
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
michaeljkoster | 0:f7f236605c98 | 1 | #include "mbed.h" |
michaeljkoster | 0:f7f236605c98 | 2 | #include "EthernetInterface.h" |
michaeljkoster | 0:f7f236605c98 | 3 | #include "nsdl_support.h" |
michaeljkoster | 0:f7f236605c98 | 4 | #include "dbg.h" |
michaeljkoster | 0:f7f236605c98 | 5 | #include "IAP.h" |
michaeljkoster | 0:f7f236605c98 | 6 | |
michaeljkoster | 0:f7f236605c98 | 7 | // Include resources |
michaeljkoster | 0:f7f236605c98 | 8 | #include "light.h" |
michaeljkoster | 0:f7f236605c98 | 9 | |
michaeljkoster | 0:f7f236605c98 | 10 | Serial pc(USBTX, USBRX); // tx, rx |
michaeljkoster | 0:f7f236605c98 | 11 | |
michaeljkoster | 0:f7f236605c98 | 12 | // **************************************************************************** |
michaeljkoster | 0:f7f236605c98 | 13 | // Configuration section |
michaeljkoster | 0:f7f236605c98 | 14 | |
michaeljkoster | 0:f7f236605c98 | 15 | // Ethernet configuration |
michaeljkoster | 0:f7f236605c98 | 16 | /* Define this to enable DHCP, otherwise manual address configuration is used */ |
michaeljkoster | 0:f7f236605c98 | 17 | #define DHCP |
michaeljkoster | 0:f7f236605c98 | 18 | |
michaeljkoster | 0:f7f236605c98 | 19 | /* Manual IP configurations, if DHCP not defined */ |
michaeljkoster | 0:f7f236605c98 | 20 | #define IP "10.0.0.199" |
michaeljkoster | 0:f7f236605c98 | 21 | #define MASK "255.255.255.0" |
michaeljkoster | 0:f7f236605c98 | 22 | #define GW "10.0.0.1" |
michaeljkoster | 0:f7f236605c98 | 23 | |
michaeljkoster | 0:f7f236605c98 | 24 | extern "C" void mbed_mac_address(char *mac) |
michaeljkoster | 0:f7f236605c98 | 25 | { |
michaeljkoster | 0:f7f236605c98 | 26 | static char buf[64] = {0}; |
michaeljkoster | 0:f7f236605c98 | 27 | IAP iap; |
michaeljkoster | 0:f7f236605c98 | 28 | int32_t *block = iap.read_serial(); |
michaeljkoster | 0:f7f236605c98 | 29 | uint32_t serial_number[5] = {0}; |
michaeljkoster | 0:f7f236605c98 | 30 | |
michaeljkoster | 0:f7f236605c98 | 31 | memset(buf, 0, sizeof(buf)); |
michaeljkoster | 0:f7f236605c98 | 32 | serial_number[0] = *(block); |
michaeljkoster | 0:f7f236605c98 | 33 | serial_number[1] = *(block+1); |
michaeljkoster | 0:f7f236605c98 | 34 | // we only want bottom 16 bits of word1 (MAC bits 32-47) |
michaeljkoster | 0:f7f236605c98 | 35 | // and bit 9 forced to 1, bit 8 forced to 0 |
michaeljkoster | 0:f7f236605c98 | 36 | // Locally administered MAC, reduced conflicts |
michaeljkoster | 0:f7f236605c98 | 37 | // http://en.wikipedia.org/wiki/MAC_address |
michaeljkoster | 0:f7f236605c98 | 38 | //serial_number[0] |= 0x00000200; |
michaeljkoster | 0:f7f236605c98 | 39 | //serial_number[0] &= 0x0000FEFF; |
michaeljkoster | 0:f7f236605c98 | 40 | memcpy(mac, (uint8_t*) &serial_number[0], 6); |
michaeljkoster | 0:f7f236605c98 | 41 | mac[0] |= 0x02; |
michaeljkoster | 0:f7f236605c98 | 42 | mac[0] &= 0xFE; |
michaeljkoster | 0:f7f236605c98 | 43 | mac[5] |= 0x02; |
michaeljkoster | 0:f7f236605c98 | 44 | mac[5] &= 0xFE; |
michaeljkoster | 0:f7f236605c98 | 45 | |
michaeljkoster | 0:f7f236605c98 | 46 | // snprintf(buf, 16, "%4X%08X", serial_number[0], serial_number[1]); |
michaeljkoster | 0:f7f236605c98 | 47 | } |
michaeljkoster | 0:f7f236605c98 | 48 | |
michaeljkoster | 0:f7f236605c98 | 49 | |
michaeljkoster | 0:f7f236605c98 | 50 | // NSP configuration |
michaeljkoster | 0:f7f236605c98 | 51 | /* Change this IP address to that of your NanoService Platform installation */ |
michaeljkoster | 0:f7f236605c98 | 52 | static const char* NSP_ADDRESS = "23.102.162.118"; // coen296.cloudapp.net |
michaeljkoster | 0:f7f236605c98 | 53 | //static const char* NSP_ADDRESS = "192.168.1.200"; // local mDS server |
michaeljkoster | 0:f7f236605c98 | 54 | static const int NSP_PORT = 5683; |
michaeljkoster | 0:f7f236605c98 | 55 | char endpoint_name[24] = "mbedDEMO-"; |
michaeljkoster | 0:f7f236605c98 | 56 | uint8_t ep_type[] = {"DEMO"}; |
michaeljkoster | 0:f7f236605c98 | 57 | uint8_t lifetime_ptr[] = {"60"}; |
michaeljkoster | 0:f7f236605c98 | 58 | |
michaeljkoster | 0:f7f236605c98 | 59 | // **************************************************************************** |
michaeljkoster | 0:f7f236605c98 | 60 | // Ethernet initialization |
michaeljkoster | 0:f7f236605c98 | 61 | |
michaeljkoster | 0:f7f236605c98 | 62 | EthernetInterface eth; |
michaeljkoster | 0:f7f236605c98 | 63 | static void ethernet_init() |
michaeljkoster | 0:f7f236605c98 | 64 | { |
michaeljkoster | 0:f7f236605c98 | 65 | |
michaeljkoster | 0:f7f236605c98 | 66 | /* Initialize network */ |
michaeljkoster | 0:f7f236605c98 | 67 | #ifdef DHCP |
michaeljkoster | 0:f7f236605c98 | 68 | NSDL_DEBUG("DHCP in use\r\n"); |
michaeljkoster | 0:f7f236605c98 | 69 | eth.init(); |
michaeljkoster | 0:f7f236605c98 | 70 | NSDL_DEBUG("eth.init\r\n"); |
michaeljkoster | 0:f7f236605c98 | 71 | #else |
michaeljkoster | 0:f7f236605c98 | 72 | eth.init(IP, MASK, GW); |
michaeljkoster | 0:f7f236605c98 | 73 | NSDL_DEBUG("eth.init\r\n"); |
michaeljkoster | 0:f7f236605c98 | 74 | #endif |
michaeljkoster | 0:f7f236605c98 | 75 | if(eth.connect(30000) == 0) |
michaeljkoster | 0:f7f236605c98 | 76 | pc.printf("Connect OK\n\r"); |
michaeljkoster | 0:f7f236605c98 | 77 | |
michaeljkoster | 0:f7f236605c98 | 78 | NSDL_DEBUG("IP Address:%s ", eth.getIPAddress()); |
michaeljkoster | 0:f7f236605c98 | 79 | } |
michaeljkoster | 0:f7f236605c98 | 80 | |
michaeljkoster | 0:f7f236605c98 | 81 | // **************************************************************************** |
michaeljkoster | 0:f7f236605c98 | 82 | // NSP initialization |
michaeljkoster | 0:f7f236605c98 | 83 | |
michaeljkoster | 0:f7f236605c98 | 84 | UDPSocket server; |
michaeljkoster | 0:f7f236605c98 | 85 | Endpoint nsp; |
michaeljkoster | 0:f7f236605c98 | 86 | |
michaeljkoster | 0:f7f236605c98 | 87 | char macbytes[6] = {0}; |
michaeljkoster | 0:f7f236605c98 | 88 | char MAC[20]; |
michaeljkoster | 0:f7f236605c98 | 89 | |
michaeljkoster | 0:f7f236605c98 | 90 | static void nsp_init() |
michaeljkoster | 0:f7f236605c98 | 91 | { |
michaeljkoster | 0:f7f236605c98 | 92 | server.init(); |
michaeljkoster | 0:f7f236605c98 | 93 | server.bind(NSP_PORT); |
michaeljkoster | 0:f7f236605c98 | 94 | |
michaeljkoster | 0:f7f236605c98 | 95 | nsp.set_address(NSP_ADDRESS, NSP_PORT); |
michaeljkoster | 0:f7f236605c98 | 96 | |
michaeljkoster | 0:f7f236605c98 | 97 | mbed_mac_address(macbytes); |
michaeljkoster | 0:f7f236605c98 | 98 | |
michaeljkoster | 0:f7f236605c98 | 99 | sprintf(MAC, "%02X%02X%02X%02X%02X%02X", macbytes[0], macbytes[1], macbytes[2], macbytes[3], macbytes[4], macbytes[5]); |
michaeljkoster | 0:f7f236605c98 | 100 | pc.printf("MAC: %s\r\n", MAC); |
michaeljkoster | 0:f7f236605c98 | 101 | |
michaeljkoster | 0:f7f236605c98 | 102 | strncat(endpoint_name, MAC, 12); |
michaeljkoster | 0:f7f236605c98 | 103 | |
michaeljkoster | 0:f7f236605c98 | 104 | NSDL_DEBUG("name: %s", endpoint_name); |
michaeljkoster | 0:f7f236605c98 | 105 | NSDL_DEBUG("NSP=%s - port %d\n", NSP_ADDRESS, NSP_PORT); |
michaeljkoster | 0:f7f236605c98 | 106 | |
michaeljkoster | 0:f7f236605c98 | 107 | } |
michaeljkoster | 0:f7f236605c98 | 108 | |
michaeljkoster | 0:f7f236605c98 | 109 | // **************************************************************************** |
michaeljkoster | 0:f7f236605c98 | 110 | // Resource creation |
michaeljkoster | 0:f7f236605c98 | 111 | |
michaeljkoster | 0:f7f236605c98 | 112 | static int create_resources() |
michaeljkoster | 0:f7f236605c98 | 113 | { |
michaeljkoster | 0:f7f236605c98 | 114 | sn_nsdl_resource_info_s *resource_ptr = NULL; |
michaeljkoster | 0:f7f236605c98 | 115 | sn_nsdl_ep_parameters_s *endpoint_ptr = NULL; |
michaeljkoster | 0:f7f236605c98 | 116 | |
michaeljkoster | 0:f7f236605c98 | 117 | NSDL_DEBUG("Creating resources"); |
michaeljkoster | 0:f7f236605c98 | 118 | |
michaeljkoster | 0:f7f236605c98 | 119 | /* Create resources */ |
michaeljkoster | 0:f7f236605c98 | 120 | resource_ptr = (sn_nsdl_resource_info_s*)nsdl_alloc(sizeof(sn_nsdl_resource_info_s)); |
michaeljkoster | 0:f7f236605c98 | 121 | if(!resource_ptr) |
michaeljkoster | 0:f7f236605c98 | 122 | return 0; |
michaeljkoster | 0:f7f236605c98 | 123 | memset(resource_ptr, 0, sizeof(sn_nsdl_resource_info_s)); |
michaeljkoster | 0:f7f236605c98 | 124 | |
michaeljkoster | 0:f7f236605c98 | 125 | resource_ptr->resource_parameters_ptr = (sn_nsdl_resource_parameters_s*)nsdl_alloc(sizeof(sn_nsdl_resource_parameters_s)); |
michaeljkoster | 0:f7f236605c98 | 126 | if(!resource_ptr->resource_parameters_ptr) |
michaeljkoster | 0:f7f236605c98 | 127 | { |
michaeljkoster | 0:f7f236605c98 | 128 | nsdl_free(resource_ptr); |
michaeljkoster | 0:f7f236605c98 | 129 | return 0; |
michaeljkoster | 0:f7f236605c98 | 130 | } |
michaeljkoster | 0:f7f236605c98 | 131 | memset(resource_ptr->resource_parameters_ptr, 0, sizeof(sn_nsdl_resource_parameters_s)); |
michaeljkoster | 0:f7f236605c98 | 132 | |
michaeljkoster | 0:f7f236605c98 | 133 | // Static resources |
michaeljkoster | 0:f7f236605c98 | 134 | nsdl_create_static_resource(resource_ptr, sizeof("3/0/0")-1, (uint8_t*) "3/0/0", 0, 0, (uint8_t*) "mbedDEMO", sizeof("mbedDEMO")-1); |
michaeljkoster | 0:f7f236605c98 | 135 | nsdl_create_static_resource(resource_ptr, sizeof("3/0/1")-1, (uint8_t*) "3/0/1", 0, 0, (uint8_t*) "DEMO", sizeof("DEMO")-1); |
michaeljkoster | 0:f7f236605c98 | 136 | |
michaeljkoster | 0:f7f236605c98 | 137 | // Dynamic resources |
michaeljkoster | 0:f7f236605c98 | 138 | create_light_resource(resource_ptr); |
michaeljkoster | 0:f7f236605c98 | 139 | |
michaeljkoster | 0:f7f236605c98 | 140 | /* Register with NSP */ |
michaeljkoster | 0:f7f236605c98 | 141 | endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)endpoint_name, ep_type, lifetime_ptr); |
michaeljkoster | 0:f7f236605c98 | 142 | if(sn_nsdl_register_endpoint(endpoint_ptr) != 0) |
michaeljkoster | 0:f7f236605c98 | 143 | pc.printf("NSP registering failed\r\n"); |
michaeljkoster | 0:f7f236605c98 | 144 | else |
michaeljkoster | 0:f7f236605c98 | 145 | pc.printf("NSP registering OK\r\n"); |
michaeljkoster | 0:f7f236605c98 | 146 | nsdl_clean_register_endpoint(&endpoint_ptr); |
michaeljkoster | 0:f7f236605c98 | 147 | |
michaeljkoster | 0:f7f236605c98 | 148 | nsdl_free(resource_ptr->resource_parameters_ptr); |
michaeljkoster | 0:f7f236605c98 | 149 | nsdl_free(resource_ptr); |
michaeljkoster | 0:f7f236605c98 | 150 | return 1; |
michaeljkoster | 0:f7f236605c98 | 151 | } |
michaeljkoster | 0:f7f236605c98 | 152 | |
michaeljkoster | 0:f7f236605c98 | 153 | // **************************************************************************** |
michaeljkoster | 0:f7f236605c98 | 154 | // Program entry point |
michaeljkoster | 0:f7f236605c98 | 155 | |
michaeljkoster | 0:f7f236605c98 | 156 | int main() |
michaeljkoster | 0:f7f236605c98 | 157 | { |
michaeljkoster | 0:f7f236605c98 | 158 | |
michaeljkoster | 0:f7f236605c98 | 159 | NSDL_DEBUG("Hello mDS Demo Endpoint Application\n"); |
michaeljkoster | 0:f7f236605c98 | 160 | |
michaeljkoster | 0:f7f236605c98 | 161 | // Initialize Ethernet interface first |
michaeljkoster | 0:f7f236605c98 | 162 | ethernet_init(); |
michaeljkoster | 0:f7f236605c98 | 163 | |
michaeljkoster | 0:f7f236605c98 | 164 | // Initialize NSP node |
michaeljkoster | 0:f7f236605c98 | 165 | nsp_init(); |
michaeljkoster | 0:f7f236605c98 | 166 | |
michaeljkoster | 0:f7f236605c98 | 167 | // Initialize NSDL stack |
michaeljkoster | 0:f7f236605c98 | 168 | nsdl_init(); |
michaeljkoster | 0:f7f236605c98 | 169 | |
michaeljkoster | 0:f7f236605c98 | 170 | // Create NSDL resources |
michaeljkoster | 0:f7f236605c98 | 171 | create_resources(); |
michaeljkoster | 0:f7f236605c98 | 172 | |
michaeljkoster | 0:f7f236605c98 | 173 | // Run the NSDL event loop (never returns) |
michaeljkoster | 0:f7f236605c98 | 174 | nsdl_event_loop(); |
michaeljkoster | 0:f7f236605c98 | 175 | } |