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 MBED_DEMOS

Committer:
sstark
Date:
Sat Apr 05 03:53:40 2014 +0000
Revision:
16:12fe2f09fab5
Parent:
15:db022645bcc1
Child:
17:40ce3d963495
log the full board uid to the debug console

Who changed what in which revision?

UserRevisionLine numberNew 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';
sstark 16:12fe2f09fab5 67 NSDL_DEBUG("Full interface uid=%s", mbed_uid);
zdshelby 8:f0ecb62bda47 68 strncat(endpoint_name, mbed_uid + 27, 20 - strlen(endpoint_name));
terohoo 0:2edbfea18d23 69
terohoo 0:2edbfea18d23 70 lcd.locate(0,11);
terohoo 0:2edbfea18d23 71 lcd.printf("IP:%s", eth.getIPAddress());
terohoo 0:2edbfea18d23 72
bogdanm 2:7e489126fe7a 73 }
bogdanm 2:7e489126fe7a 74
bogdanm 2:7e489126fe7a 75 // ****************************************************************************
bogdanm 2:7e489126fe7a 76 // NSP initialization
bogdanm 2:7e489126fe7a 77
bogdanm 2:7e489126fe7a 78 UDPSocket server;
bogdanm 2:7e489126fe7a 79 Endpoint nsp;
bogdanm 2:7e489126fe7a 80
bogdanm 2:7e489126fe7a 81 static void nsp_init()
bogdanm 2:7e489126fe7a 82 {
terohoo 0:2edbfea18d23 83 server.init();
terohoo 0:2edbfea18d23 84 server.bind(NSP_PORT);
terohoo 0:2edbfea18d23 85
bogdanm 2:7e489126fe7a 86 nsp.set_address(NSP_ADDRESS, NSP_PORT);
bogdanm 2:7e489126fe7a 87
bogdanm 2:7e489126fe7a 88 NSDL_DEBUG("name: %s", endpoint_name);
bogdanm 2:7e489126fe7a 89 NSDL_DEBUG("NSP=%s - port %d\n", NSP_ADDRESS, NSP_PORT);
terohoo 0:2edbfea18d23 90
terohoo 0:2edbfea18d23 91 lcd.locate(0,22);
zdshelby 8:f0ecb62bda47 92 lcd.printf("EP:%s\n", endpoint_name);
bogdanm 2:7e489126fe7a 93 }
terohoo 0:2edbfea18d23 94
bogdanm 2:7e489126fe7a 95 // ****************************************************************************
bogdanm 2:7e489126fe7a 96 // Resource creation
terohoo 0:2edbfea18d23 97
bogdanm 2:7e489126fe7a 98 static int create_resources()
bogdanm 2:7e489126fe7a 99 {
bogdanm 2:7e489126fe7a 100 sn_nsdl_resource_info_s *resource_ptr = NULL;
bogdanm 2:7e489126fe7a 101 sn_nsdl_ep_parameters_s *endpoint_ptr = NULL;
bogdanm 2:7e489126fe7a 102
bogdanm 2:7e489126fe7a 103 NSDL_DEBUG("Creating resources");
terohoo 0:2edbfea18d23 104
terohoo 0:2edbfea18d23 105 /* Create resources */
bogdanm 2:7e489126fe7a 106 resource_ptr = (sn_nsdl_resource_info_s*)nsdl_alloc(sizeof(sn_nsdl_resource_info_s));
terohoo 0:2edbfea18d23 107 if(!resource_ptr)
terohoo 0:2edbfea18d23 108 return 0;
terohoo 0:2edbfea18d23 109 memset(resource_ptr, 0, sizeof(sn_nsdl_resource_info_s));
terohoo 0:2edbfea18d23 110
bogdanm 2:7e489126fe7a 111 resource_ptr->resource_parameters_ptr = (sn_nsdl_resource_parameters_s*)nsdl_alloc(sizeof(sn_nsdl_resource_parameters_s));
terohoo 0:2edbfea18d23 112 if(!resource_ptr->resource_parameters_ptr)
terohoo 0:2edbfea18d23 113 {
bogdanm 2:7e489126fe7a 114 nsdl_free(resource_ptr);
terohoo 0:2edbfea18d23 115 return 0;
terohoo 0:2edbfea18d23 116 }
terohoo 0:2edbfea18d23 117 memset(resource_ptr->resource_parameters_ptr, 0, sizeof(sn_nsdl_resource_parameters_s));
terohoo 0:2edbfea18d23 118
bogdanm 2:7e489126fe7a 119 // Static resources
zdshelby 8:f0ecb62bda47 120 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 121 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 122 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 123 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 124 #ifdef DHCP
zdshelby 14:5acd59fec679 125 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 126 #else
zdshelby 14:5acd59fec679 127 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 128 #endif
zdshelby 14:5acd59fec679 129 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 130 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 131 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 132 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 133
bogdanm 2:7e489126fe7a 134 // Dynamic resources
bogdanm 2:7e489126fe7a 135 create_temperature_resource(resource_ptr);
bogdanm 2:7e489126fe7a 136 create_light_resource(resource_ptr);
bogdanm 2:7e489126fe7a 137 create_gps_resource(resource_ptr);
bogdanm 2:7e489126fe7a 138 create_relay_resource(resource_ptr);
terohoo 0:2edbfea18d23 139
terohoo 0:2edbfea18d23 140 /* Register with NSP */
bogdanm 2:7e489126fe7a 141 endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)endpoint_name, ep_type, lifetime_ptr);
terohoo 0:2edbfea18d23 142 if(sn_nsdl_register_endpoint(endpoint_ptr) != 0)
terohoo 0:2edbfea18d23 143 pc.printf("NSP registering failed\r\n");
terohoo 0:2edbfea18d23 144 else
terohoo 0:2edbfea18d23 145 pc.printf("NSP registering OK\r\n");
bogdanm 2:7e489126fe7a 146 nsdl_clean_register_endpoint(&endpoint_ptr);
terohoo 0:2edbfea18d23 147
bogdanm 2:7e489126fe7a 148 nsdl_free(resource_ptr->resource_parameters_ptr);
bogdanm 2:7e489126fe7a 149 nsdl_free(resource_ptr);
terohoo 0:2edbfea18d23 150 return 1;
terohoo 0:2edbfea18d23 151 }
terohoo 0:2edbfea18d23 152
bogdanm 2:7e489126fe7a 153 // ****************************************************************************
bogdanm 2:7e489126fe7a 154 // Program entry point
terohoo 0:2edbfea18d23 155
bogdanm 2:7e489126fe7a 156 int main()
terohoo 0:2edbfea18d23 157 {
bogdanm 2:7e489126fe7a 158 lcd.cls();
bogdanm 2:7e489126fe7a 159 lcd.locate(0,0);
zdshelby 14:5acd59fec679 160 lcd.printf("NanoService LWM2M r%s", FIRMWARE_VER);
zdshelby 11:3e9c8d80891e 161 NSDL_DEBUG("NanoService LWM2M Demo for LPC1768 App Board\n");
bogdanm 2:7e489126fe7a 162
bogdanm 2:7e489126fe7a 163 // Initialize Ethernet interface first
bogdanm 2:7e489126fe7a 164 ethernet_init();
terohoo 1:e35d7f10999a 165
bogdanm 2:7e489126fe7a 166 // Initialize NSP node
bogdanm 2:7e489126fe7a 167 nsp_init();
bogdanm 2:7e489126fe7a 168
bogdanm 2:7e489126fe7a 169 // Initialize NSDL stack
bogdanm 2:7e489126fe7a 170 nsdl_init();
bogdanm 2:7e489126fe7a 171
bogdanm 2:7e489126fe7a 172 // Create NSDL resources
bogdanm 2:7e489126fe7a 173 create_resources();
bogdanm 2:7e489126fe7a 174
bogdanm 2:7e489126fe7a 175 // Run the NSDL event loop (never returns)
bogdanm 2:7e489126fe7a 176 nsdl_event_loop();
terohoo 0:2edbfea18d23 177 }