![](/media/cache/group/arm_mbed.PNG.50x50_q85.png)
More advanced NanoService Demo for LPC1768 App Board using OMA Lightweight Objects
Dependencies: Beep C12832_lcd EthernetInterface LM75B MMA7660 mbed-rtos mbed nsdl_lib
Fork of LWM2M_NanoService_Ethernet by
main.cpp@15:db022645bcc1, 2014-03-26 (annotated)
- Committer:
- zdshelby
- Date:
- Wed Mar 26 22:40:37 2014 +0000
- Revision:
- 15:db022645bcc1
- Parent:
- 14:5acd59fec679
- Child:
- 16:12fe2f09fab5
- Small resource fix
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
terohoo | 0:2edbfea18d23 | 1 | #include "mbed.h" |
terohoo | 0:2edbfea18d23 | 2 | #include "EthernetInterface.h" |
terohoo | 0:2edbfea18d23 | 3 | #include "C12832_lcd.h" |
bogdanm | 2:7e489126fe7a | 4 | #include "nsdl_support.h" |
bogdanm | 2:7e489126fe7a | 5 | #include "dbg.h" |
bogdanm | 2:7e489126fe7a | 6 | // Include various resources |
bogdanm | 2:7e489126fe7a | 7 | #include "temperature.h" |
bogdanm | 2:7e489126fe7a | 8 | #include "light.h" |
bogdanm | 2:7e489126fe7a | 9 | #include "gps.h" |
bogdanm | 2:7e489126fe7a | 10 | #include "relay.h" |
terohoo | 0:2edbfea18d23 | 11 | |
bogdanm | 2:7e489126fe7a | 12 | static C12832_LCD lcd; |
bogdanm | 2:7e489126fe7a | 13 | Serial pc(USBTX, USBRX); // tx, rx |
terohoo | 0:2edbfea18d23 | 14 | |
bogdanm | 2:7e489126fe7a | 15 | // **************************************************************************** |
bogdanm | 2:7e489126fe7a | 16 | // Configuration section |
bogdanm | 2:7e489126fe7a | 17 | |
bogdanm | 2:7e489126fe7a | 18 | // Ethernet configuration |
terohoo | 0:2edbfea18d23 | 19 | /* Define this to enable DHCP, otherwise manual address configuration is used */ |
sstark | 12:b695198175ee | 20 | #define DHCP 1 |
terohoo | 0:2edbfea18d23 | 21 | |
terohoo | 0:2edbfea18d23 | 22 | /* Manual IP configurations, if DHCP not defined */ |
terohoo | 0:2edbfea18d23 | 23 | #define IP "10.45.0.206" |
terohoo | 0:2edbfea18d23 | 24 | #define MASK "255.255.255.0" |
terohoo | 0:2edbfea18d23 | 25 | #define GW "10.45.0.1" |
terohoo | 0:2edbfea18d23 | 26 | |
bogdanm | 2:7e489126fe7a | 27 | // NSP configuration |
terohoo | 0:2edbfea18d23 | 28 | /* Change this IP address to that of your NanoService Platform installation */ |
sstark | 12:b695198175ee | 29 | static const char* NSP_ADDRESS = "137.135.13.28"; /* demo NSP, web interface at http://red-hat-summit.cloudapp.net*/ |
bogdanm | 2:7e489126fe7a | 30 | static const int NSP_PORT = 5683; |
zdshelby | 8:f0ecb62bda47 | 31 | char endpoint_name[20] = "mbed-ethernet-"; |
zdshelby | 13:b52d83adc1ab | 32 | char mbed_uid[33]; // for creating unique name for the board |
zdshelby | 8:f0ecb62bda47 | 33 | uint8_t ep_type[] = {"mbed_lpc1768_appboard"}; |
zdshelby | 8:f0ecb62bda47 | 34 | uint8_t lifetime_ptr[] = {"60"}; |
zdshelby | 15:db022645bcc1 | 35 | static const char* FIRMWARE_VER = "15"; // Committed revision number |
zdshelby | 13:b52d83adc1ab | 36 | char* mac; |
zdshelby | 13:b52d83adc1ab | 37 | char* ipAddr; |
zdshelby | 13:b52d83adc1ab | 38 | char* gateway; |
zdshelby | 13:b52d83adc1ab | 39 | char* nmask; |
terohoo | 0:2edbfea18d23 | 40 | |
bogdanm | 2:7e489126fe7a | 41 | // **************************************************************************** |
bogdanm | 2:7e489126fe7a | 42 | // Ethernet initialization |
terohoo | 0:2edbfea18d23 | 43 | |
terohoo | 0:2edbfea18d23 | 44 | EthernetInterface eth; |
terohoo | 0:2edbfea18d23 | 45 | |
bogdanm | 2:7e489126fe7a | 46 | static void ethernet_init() |
terohoo | 0:2edbfea18d23 | 47 | { |
terohoo | 0:2edbfea18d23 | 48 | |
terohoo | 0:2edbfea18d23 | 49 | /* Initialize network */ |
terohoo | 0:2edbfea18d23 | 50 | #ifdef DHCP |
zdshelby | 13:b52d83adc1ab | 51 | NSDL_DEBUG("DHCP in use"); |
terohoo | 0:2edbfea18d23 | 52 | eth.init(); |
terohoo | 0:2edbfea18d23 | 53 | #else |
terohoo | 0:2edbfea18d23 | 54 | eth.init(IP, MASK, GW); |
terohoo | 0:2edbfea18d23 | 55 | #endif |
terohoo | 0:2edbfea18d23 | 56 | if(eth.connect(30000) == 0) |
zdshelby | 13:b52d83adc1ab | 57 | NSDL_DEBUG("Ethernet up"); |
terohoo | 0:2edbfea18d23 | 58 | |
zdshelby | 13:b52d83adc1ab | 59 | mac = eth.getMACAddress(); |
zdshelby | 13:b52d83adc1ab | 60 | ipAddr = eth.getIPAddress(); |
zdshelby | 13:b52d83adc1ab | 61 | gateway = eth.getGateway(); |
zdshelby | 13:b52d83adc1ab | 62 | nmask = eth.getNetworkMask(); |
zdshelby | 13:b52d83adc1ab | 63 | NSDL_DEBUG("Network: mac=%s, ip=%s, gateway=%s, mask=%s", mac, ipAddr, gateway, nmask); |
sstark | 12:b695198175ee | 64 | |
terohoo | 0:2edbfea18d23 | 65 | mbed_interface_uid(mbed_uid); |
terohoo | 0:2edbfea18d23 | 66 | mbed_uid[32] = '\0'; |
zdshelby | 8:f0ecb62bda47 | 67 | strncat(endpoint_name, mbed_uid + 27, 20 - strlen(endpoint_name)); |
terohoo | 0:2edbfea18d23 | 68 | |
terohoo | 0:2edbfea18d23 | 69 | lcd.locate(0,11); |
terohoo | 0:2edbfea18d23 | 70 | lcd.printf("IP:%s", eth.getIPAddress()); |
terohoo | 0:2edbfea18d23 | 71 | |
bogdanm | 2:7e489126fe7a | 72 | } |
bogdanm | 2:7e489126fe7a | 73 | |
bogdanm | 2:7e489126fe7a | 74 | // **************************************************************************** |
bogdanm | 2:7e489126fe7a | 75 | // NSP initialization |
bogdanm | 2:7e489126fe7a | 76 | |
bogdanm | 2:7e489126fe7a | 77 | UDPSocket server; |
bogdanm | 2:7e489126fe7a | 78 | Endpoint nsp; |
bogdanm | 2:7e489126fe7a | 79 | |
bogdanm | 2:7e489126fe7a | 80 | static void nsp_init() |
bogdanm | 2:7e489126fe7a | 81 | { |
terohoo | 0:2edbfea18d23 | 82 | server.init(); |
terohoo | 0:2edbfea18d23 | 83 | server.bind(NSP_PORT); |
terohoo | 0:2edbfea18d23 | 84 | |
bogdanm | 2:7e489126fe7a | 85 | nsp.set_address(NSP_ADDRESS, NSP_PORT); |
bogdanm | 2:7e489126fe7a | 86 | |
bogdanm | 2:7e489126fe7a | 87 | NSDL_DEBUG("name: %s", endpoint_name); |
bogdanm | 2:7e489126fe7a | 88 | NSDL_DEBUG("NSP=%s - port %d\n", NSP_ADDRESS, NSP_PORT); |
terohoo | 0:2edbfea18d23 | 89 | |
terohoo | 0:2edbfea18d23 | 90 | lcd.locate(0,22); |
zdshelby | 8:f0ecb62bda47 | 91 | lcd.printf("EP:%s\n", endpoint_name); |
bogdanm | 2:7e489126fe7a | 92 | } |
terohoo | 0:2edbfea18d23 | 93 | |
bogdanm | 2:7e489126fe7a | 94 | // **************************************************************************** |
bogdanm | 2:7e489126fe7a | 95 | // Resource creation |
terohoo | 0:2edbfea18d23 | 96 | |
bogdanm | 2:7e489126fe7a | 97 | static int create_resources() |
bogdanm | 2:7e489126fe7a | 98 | { |
bogdanm | 2:7e489126fe7a | 99 | sn_nsdl_resource_info_s *resource_ptr = NULL; |
bogdanm | 2:7e489126fe7a | 100 | sn_nsdl_ep_parameters_s *endpoint_ptr = NULL; |
bogdanm | 2:7e489126fe7a | 101 | |
bogdanm | 2:7e489126fe7a | 102 | NSDL_DEBUG("Creating resources"); |
terohoo | 0:2edbfea18d23 | 103 | |
terohoo | 0:2edbfea18d23 | 104 | /* Create resources */ |
bogdanm | 2:7e489126fe7a | 105 | resource_ptr = (sn_nsdl_resource_info_s*)nsdl_alloc(sizeof(sn_nsdl_resource_info_s)); |
terohoo | 0:2edbfea18d23 | 106 | if(!resource_ptr) |
terohoo | 0:2edbfea18d23 | 107 | return 0; |
terohoo | 0:2edbfea18d23 | 108 | memset(resource_ptr, 0, sizeof(sn_nsdl_resource_info_s)); |
terohoo | 0:2edbfea18d23 | 109 | |
bogdanm | 2:7e489126fe7a | 110 | resource_ptr->resource_parameters_ptr = (sn_nsdl_resource_parameters_s*)nsdl_alloc(sizeof(sn_nsdl_resource_parameters_s)); |
terohoo | 0:2edbfea18d23 | 111 | if(!resource_ptr->resource_parameters_ptr) |
terohoo | 0:2edbfea18d23 | 112 | { |
bogdanm | 2:7e489126fe7a | 113 | nsdl_free(resource_ptr); |
terohoo | 0:2edbfea18d23 | 114 | return 0; |
terohoo | 0:2edbfea18d23 | 115 | } |
terohoo | 0:2edbfea18d23 | 116 | memset(resource_ptr->resource_parameters_ptr, 0, sizeof(sn_nsdl_resource_parameters_s)); |
terohoo | 0:2edbfea18d23 | 117 | |
bogdanm | 2:7e489126fe7a | 118 | // Static resources |
zdshelby | 8:f0ecb62bda47 | 119 | nsdl_create_static_resource(resource_ptr, sizeof("3/0/0")-1, (uint8_t*) "3/0/0", 0, 0, (uint8_t*) "ARM", sizeof("ARM")-1); |
zdshelby | 8:f0ecb62bda47 | 120 | nsdl_create_static_resource(resource_ptr, sizeof("3/0/1")-1, (uint8_t*) "3/0/1", 0, 0, (uint8_t*) "LPC1768 App Board", sizeof("LPC1768 App Board")-1); |
zdshelby | 13:b52d83adc1ab | 121 | nsdl_create_static_resource(resource_ptr, sizeof("3/0/2")-1, (uint8_t*) "3/0/2", 0, 0, (uint8_t*) mbed_uid, sizeof(mbed_uid)-1); |
zdshelby | 15:db022645bcc1 | 122 | nsdl_create_static_resource(resource_ptr, sizeof("3/0/3")-1, (uint8_t*) "3/0/3", 0, 0, (uint8_t*) FIRMWARE_VER, strlen(FIRMWARE_VER)); |
zdshelby | 14:5acd59fec679 | 123 | #ifdef DHCP |
zdshelby | 14:5acd59fec679 | 124 | nsdl_create_static_resource(resource_ptr, sizeof("4/0/0")-1, (uint8_t*) "4/0/0", 0, 0, (uint8_t*) "Ethernet DHCP", sizeof("Ethernet DHCP")-1); |
zdshelby | 14:5acd59fec679 | 125 | #else |
zdshelby | 14:5acd59fec679 | 126 | nsdl_create_static_resource(resource_ptr, sizeof("4/0/0")-1, (uint8_t*) "4/0/0", 0, 0, (uint8_t*) "Ethernet Static", sizeof("Ethernet Static")-1); |
zdshelby | 14:5acd59fec679 | 127 | #endif |
zdshelby | 14:5acd59fec679 | 128 | nsdl_create_static_resource(resource_ptr, sizeof("4/0/1")-1, (uint8_t*) "4/0/1", 0, 0, (uint8_t*) "Ethernet (Static, DHCP)", sizeof("Ethernet (Static, DHCP)")-1); |
zdshelby | 14:5acd59fec679 | 129 | nsdl_create_static_resource(resource_ptr, sizeof("4/0/4")-1, (uint8_t*) "4/0/4", 0, 0, (uint8_t*) ipAddr, strlen(ipAddr)); |
zdshelby | 14:5acd59fec679 | 130 | nsdl_create_static_resource(resource_ptr, sizeof("4/0/5")-1, (uint8_t*) "4/0/5", 0, 0, (uint8_t*) gateway, strlen(gateway)); |
zdshelby | 13:b52d83adc1ab | 131 | nsdl_create_static_resource(resource_ptr, sizeof("3/0/16")-1, (uint8_t*) "3/0/16", 0, 0, (uint8_t*) "UDP", sizeof("UDP")-1); |
terohoo | 0:2edbfea18d23 | 132 | |
bogdanm | 2:7e489126fe7a | 133 | // Dynamic resources |
bogdanm | 2:7e489126fe7a | 134 | create_temperature_resource(resource_ptr); |
bogdanm | 2:7e489126fe7a | 135 | create_light_resource(resource_ptr); |
bogdanm | 2:7e489126fe7a | 136 | create_gps_resource(resource_ptr); |
bogdanm | 2:7e489126fe7a | 137 | create_relay_resource(resource_ptr); |
terohoo | 0:2edbfea18d23 | 138 | |
terohoo | 0:2edbfea18d23 | 139 | /* Register with NSP */ |
bogdanm | 2:7e489126fe7a | 140 | endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)endpoint_name, ep_type, lifetime_ptr); |
terohoo | 0:2edbfea18d23 | 141 | if(sn_nsdl_register_endpoint(endpoint_ptr) != 0) |
terohoo | 0:2edbfea18d23 | 142 | pc.printf("NSP registering failed\r\n"); |
terohoo | 0:2edbfea18d23 | 143 | else |
terohoo | 0:2edbfea18d23 | 144 | pc.printf("NSP registering OK\r\n"); |
bogdanm | 2:7e489126fe7a | 145 | nsdl_clean_register_endpoint(&endpoint_ptr); |
terohoo | 0:2edbfea18d23 | 146 | |
bogdanm | 2:7e489126fe7a | 147 | nsdl_free(resource_ptr->resource_parameters_ptr); |
bogdanm | 2:7e489126fe7a | 148 | nsdl_free(resource_ptr); |
terohoo | 0:2edbfea18d23 | 149 | return 1; |
terohoo | 0:2edbfea18d23 | 150 | } |
terohoo | 0:2edbfea18d23 | 151 | |
bogdanm | 2:7e489126fe7a | 152 | // **************************************************************************** |
bogdanm | 2:7e489126fe7a | 153 | // Program entry point |
terohoo | 0:2edbfea18d23 | 154 | |
bogdanm | 2:7e489126fe7a | 155 | int main() |
terohoo | 0:2edbfea18d23 | 156 | { |
bogdanm | 2:7e489126fe7a | 157 | lcd.cls(); |
bogdanm | 2:7e489126fe7a | 158 | lcd.locate(0,0); |
zdshelby | 14:5acd59fec679 | 159 | lcd.printf("NanoService LWM2M r%s", FIRMWARE_VER); |
zdshelby | 11:3e9c8d80891e | 160 | NSDL_DEBUG("NanoService LWM2M Demo for LPC1768 App Board\n"); |
bogdanm | 2:7e489126fe7a | 161 | |
bogdanm | 2:7e489126fe7a | 162 | // Initialize Ethernet interface first |
bogdanm | 2:7e489126fe7a | 163 | ethernet_init(); |
terohoo | 1:e35d7f10999a | 164 | |
bogdanm | 2:7e489126fe7a | 165 | // Initialize NSP node |
bogdanm | 2:7e489126fe7a | 166 | nsp_init(); |
bogdanm | 2:7e489126fe7a | 167 | |
bogdanm | 2:7e489126fe7a | 168 | // Initialize NSDL stack |
bogdanm | 2:7e489126fe7a | 169 | nsdl_init(); |
bogdanm | 2:7e489126fe7a | 170 | |
bogdanm | 2:7e489126fe7a | 171 | // Create NSDL resources |
bogdanm | 2:7e489126fe7a | 172 | create_resources(); |
bogdanm | 2:7e489126fe7a | 173 | |
bogdanm | 2:7e489126fe7a | 174 | // Run the NSDL event loop (never returns) |
bogdanm | 2:7e489126fe7a | 175 | nsdl_event_loop(); |
terohoo | 0:2edbfea18d23 | 176 | } |