Red Hat Summit 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:
sam_grove
Date:
Mon Apr 28 23:49:50 2014 +0000
Revision:
30:cab9f0800471
Parent:
27:d898b3a8c769
Updates to mbed house as sent for the show

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"
sstark 17:40ce3d963495 11 #include "rgb.h"
sam_grove 30:cab9f0800471 12 #include "input.h"
terohoo 0:2edbfea18d23 13
bogdanm 2:7e489126fe7a 14 static C12832_LCD lcd;
bogdanm 2:7e489126fe7a 15 Serial pc(USBTX, USBRX); // tx, rx
terohoo 0:2edbfea18d23 16
bogdanm 2:7e489126fe7a 17 // ****************************************************************************
bogdanm 2:7e489126fe7a 18 // Configuration section
bogdanm 2:7e489126fe7a 19
bogdanm 2:7e489126fe7a 20 // Ethernet configuration
terohoo 0:2edbfea18d23 21 /* Define this to enable DHCP, otherwise manual address configuration is used */
sstark 12:b695198175ee 22 #define DHCP 1
terohoo 0:2edbfea18d23 23
sstark 20:84ee332ba360 24 // Define this to enable the RGB LED resource
sstark 20:84ee332ba360 25 //#define USE_RGBLED 1
sstark 19:e5c0b6553c11 26
terohoo 0:2edbfea18d23 27 /* Manual IP configurations, if DHCP not defined */
terohoo 0:2edbfea18d23 28 #define IP "10.45.0.206"
terohoo 0:2edbfea18d23 29 #define MASK "255.255.255.0"
terohoo 0:2edbfea18d23 30 #define GW "10.45.0.1"
terohoo 0:2edbfea18d23 31
bogdanm 2:7e489126fe7a 32 // NSP configuration
terohoo 0:2edbfea18d23 33 /* Change this IP address to that of your NanoService Platform installation */
sam_grove 27:d898b3a8c769 34 static const char* NSP_ADDRESS = "208.111.39.209"; /* demo NSP, web interface at http://208.111.39.209/ */
bogdanm 2:7e489126fe7a 35 static const int NSP_PORT = 5683;
sam_grove 27:d898b3a8c769 36 char endpoint_name[20] = "mbed-house-";
zdshelby 13:b52d83adc1ab 37 char mbed_uid[33]; // for creating unique name for the board
sam_grove 27:d898b3a8c769 38 uint8_t ep_type[] = {"mbed_house"};
zdshelby 26:afcaeb039ef3 39 uint8_t lifetime_ptr[] = {"60"};
zdshelby 26:afcaeb039ef3 40 static const char* FIRMWARE_VER = "25"; // Committed revision number
zdshelby 13:b52d83adc1ab 41 char* mac;
zdshelby 13:b52d83adc1ab 42 char* ipAddr;
zdshelby 13:b52d83adc1ab 43 char* gateway;
zdshelby 13:b52d83adc1ab 44 char* nmask;
terohoo 0:2edbfea18d23 45
bogdanm 2:7e489126fe7a 46 // ****************************************************************************
bogdanm 2:7e489126fe7a 47 // Ethernet initialization
terohoo 0:2edbfea18d23 48
terohoo 0:2edbfea18d23 49 EthernetInterface eth;
terohoo 0:2edbfea18d23 50
bogdanm 2:7e489126fe7a 51 static void ethernet_init()
terohoo 0:2edbfea18d23 52 {
terohoo 0:2edbfea18d23 53
terohoo 0:2edbfea18d23 54 /* Initialize network */
terohoo 0:2edbfea18d23 55 #ifdef DHCP
zdshelby 13:b52d83adc1ab 56 NSDL_DEBUG("DHCP in use");
terohoo 0:2edbfea18d23 57 eth.init();
terohoo 0:2edbfea18d23 58 #else
terohoo 0:2edbfea18d23 59 eth.init(IP, MASK, GW);
terohoo 0:2edbfea18d23 60 #endif
terohoo 0:2edbfea18d23 61 if(eth.connect(30000) == 0)
zdshelby 13:b52d83adc1ab 62 NSDL_DEBUG("Ethernet up");
terohoo 0:2edbfea18d23 63
zdshelby 13:b52d83adc1ab 64 mac = eth.getMACAddress();
zdshelby 13:b52d83adc1ab 65 ipAddr = eth.getIPAddress();
zdshelby 13:b52d83adc1ab 66 gateway = eth.getGateway();
zdshelby 13:b52d83adc1ab 67 nmask = eth.getNetworkMask();
zdshelby 13:b52d83adc1ab 68 NSDL_DEBUG("Network: mac=%s, ip=%s, gateway=%s, mask=%s", mac, ipAddr, gateway, nmask);
sstark 12:b695198175ee 69
terohoo 0:2edbfea18d23 70 mbed_interface_uid(mbed_uid);
terohoo 0:2edbfea18d23 71 mbed_uid[32] = '\0';
sstark 16:12fe2f09fab5 72 NSDL_DEBUG("Full interface uid=%s", mbed_uid);
zdshelby 8:f0ecb62bda47 73 strncat(endpoint_name, mbed_uid + 27, 20 - strlen(endpoint_name));
terohoo 0:2edbfea18d23 74
terohoo 0:2edbfea18d23 75 lcd.locate(0,11);
terohoo 0:2edbfea18d23 76 lcd.printf("IP:%s", eth.getIPAddress());
terohoo 0:2edbfea18d23 77
bogdanm 2:7e489126fe7a 78 }
bogdanm 2:7e489126fe7a 79
bogdanm 2:7e489126fe7a 80 // ****************************************************************************
bogdanm 2:7e489126fe7a 81 // NSP initialization
bogdanm 2:7e489126fe7a 82
bogdanm 2:7e489126fe7a 83 UDPSocket server;
bogdanm 2:7e489126fe7a 84 Endpoint nsp;
bogdanm 2:7e489126fe7a 85
bogdanm 2:7e489126fe7a 86 static void nsp_init()
bogdanm 2:7e489126fe7a 87 {
terohoo 0:2edbfea18d23 88 server.init();
terohoo 0:2edbfea18d23 89 server.bind(NSP_PORT);
terohoo 0:2edbfea18d23 90
bogdanm 2:7e489126fe7a 91 nsp.set_address(NSP_ADDRESS, NSP_PORT);
bogdanm 2:7e489126fe7a 92
bogdanm 2:7e489126fe7a 93 NSDL_DEBUG("name: %s", endpoint_name);
bogdanm 2:7e489126fe7a 94 NSDL_DEBUG("NSP=%s - port %d\n", NSP_ADDRESS, NSP_PORT);
terohoo 0:2edbfea18d23 95
terohoo 0:2edbfea18d23 96 lcd.locate(0,22);
zdshelby 8:f0ecb62bda47 97 lcd.printf("EP:%s\n", endpoint_name);
bogdanm 2:7e489126fe7a 98 }
terohoo 0:2edbfea18d23 99
bogdanm 2:7e489126fe7a 100 // ****************************************************************************
bogdanm 2:7e489126fe7a 101 // Resource creation
terohoo 0:2edbfea18d23 102
bogdanm 2:7e489126fe7a 103 static int create_resources()
bogdanm 2:7e489126fe7a 104 {
bogdanm 2:7e489126fe7a 105 sn_nsdl_resource_info_s *resource_ptr = NULL;
bogdanm 2:7e489126fe7a 106 sn_nsdl_ep_parameters_s *endpoint_ptr = NULL;
bogdanm 2:7e489126fe7a 107
bogdanm 2:7e489126fe7a 108 NSDL_DEBUG("Creating resources");
terohoo 0:2edbfea18d23 109
terohoo 0:2edbfea18d23 110 /* Create resources */
bogdanm 2:7e489126fe7a 111 resource_ptr = (sn_nsdl_resource_info_s*)nsdl_alloc(sizeof(sn_nsdl_resource_info_s));
terohoo 0:2edbfea18d23 112 if(!resource_ptr)
terohoo 0:2edbfea18d23 113 return 0;
terohoo 0:2edbfea18d23 114 memset(resource_ptr, 0, sizeof(sn_nsdl_resource_info_s));
terohoo 0:2edbfea18d23 115
bogdanm 2:7e489126fe7a 116 resource_ptr->resource_parameters_ptr = (sn_nsdl_resource_parameters_s*)nsdl_alloc(sizeof(sn_nsdl_resource_parameters_s));
terohoo 0:2edbfea18d23 117 if(!resource_ptr->resource_parameters_ptr)
terohoo 0:2edbfea18d23 118 {
bogdanm 2:7e489126fe7a 119 nsdl_free(resource_ptr);
terohoo 0:2edbfea18d23 120 return 0;
terohoo 0:2edbfea18d23 121 }
terohoo 0:2edbfea18d23 122 memset(resource_ptr->resource_parameters_ptr, 0, sizeof(sn_nsdl_resource_parameters_s));
terohoo 0:2edbfea18d23 123
bogdanm 2:7e489126fe7a 124 // Static resources
zdshelby 8:f0ecb62bda47 125 nsdl_create_static_resource(resource_ptr, sizeof("3/0/0")-1, (uint8_t*) "3/0/0", 0, 0, (uint8_t*) "ARM", sizeof("ARM")-1);
sam_grove 27:d898b3a8c769 126 nsdl_create_static_resource(resource_ptr, sizeof("3/0/1")-1, (uint8_t*) "3/0/1", 0, 0, (uint8_t*) "LPC1768 mbed house", sizeof("LPC1768 mbed house")-1);
zdshelby 13:b52d83adc1ab 127 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 128 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 129 #ifdef DHCP
zdshelby 14:5acd59fec679 130 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 131 #else
zdshelby 14:5acd59fec679 132 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 133 #endif
zdshelby 14:5acd59fec679 134 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 135 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 136 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 137 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 138
bogdanm 2:7e489126fe7a 139 // Dynamic resources
bogdanm 2:7e489126fe7a 140 create_temperature_resource(resource_ptr);
bogdanm 2:7e489126fe7a 141 create_light_resource(resource_ptr);
bogdanm 2:7e489126fe7a 142 create_gps_resource(resource_ptr);
sam_grove 30:cab9f0800471 143 create_input_resource(resource_ptr);
sam_grove 27:d898b3a8c769 144 //create_relay_resource(resource_ptr);
sstark 20:84ee332ba360 145
sstark 19:e5c0b6553c11 146 #ifdef USE_RGBLED
sstark 20:84ee332ba360 147 NSDL_DEBUG("Enabling RGB LED due to USE_RGBLED=%d\n", USE_RGBLED);
sstark 17:40ce3d963495 148 create_rgb_resource(resource_ptr);
sstark 19:e5c0b6553c11 149 #else
sstark 19:e5c0b6553c11 150 NSDL_DEBUG("Skipped RGB LED resource, change USE_RGBLED to 1 in main.cpp to test");
sstark 19:e5c0b6553c11 151 #endif
terohoo 0:2edbfea18d23 152
sstark 20:84ee332ba360 153 /* Register with NSP */
bogdanm 2:7e489126fe7a 154 endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)endpoint_name, ep_type, lifetime_ptr);
terohoo 0:2edbfea18d23 155 if(sn_nsdl_register_endpoint(endpoint_ptr) != 0)
terohoo 0:2edbfea18d23 156 pc.printf("NSP registering failed\r\n");
terohoo 0:2edbfea18d23 157 else
terohoo 0:2edbfea18d23 158 pc.printf("NSP registering OK\r\n");
bogdanm 2:7e489126fe7a 159 nsdl_clean_register_endpoint(&endpoint_ptr);
terohoo 0:2edbfea18d23 160
bogdanm 2:7e489126fe7a 161 nsdl_free(resource_ptr->resource_parameters_ptr);
bogdanm 2:7e489126fe7a 162 nsdl_free(resource_ptr);
terohoo 0:2edbfea18d23 163 return 1;
terohoo 0:2edbfea18d23 164 }
terohoo 0:2edbfea18d23 165
bogdanm 2:7e489126fe7a 166 // ****************************************************************************
bogdanm 2:7e489126fe7a 167 // Program entry point
terohoo 0:2edbfea18d23 168
bogdanm 2:7e489126fe7a 169 int main()
terohoo 0:2edbfea18d23 170 {
bogdanm 2:7e489126fe7a 171 lcd.cls();
bogdanm 2:7e489126fe7a 172 lcd.locate(0,0);
zdshelby 14:5acd59fec679 173 lcd.printf("NanoService LWM2M r%s", FIRMWARE_VER);
zdshelby 11:3e9c8d80891e 174 NSDL_DEBUG("NanoService LWM2M Demo for LPC1768 App Board\n");
bogdanm 2:7e489126fe7a 175
bogdanm 2:7e489126fe7a 176 // Initialize Ethernet interface first
bogdanm 2:7e489126fe7a 177 ethernet_init();
terohoo 1:e35d7f10999a 178
bogdanm 2:7e489126fe7a 179 // Initialize NSP node
bogdanm 2:7e489126fe7a 180 nsp_init();
bogdanm 2:7e489126fe7a 181
bogdanm 2:7e489126fe7a 182 // Initialize NSDL stack
bogdanm 2:7e489126fe7a 183 nsdl_init();
bogdanm 2:7e489126fe7a 184
bogdanm 2:7e489126fe7a 185 // Create NSDL resources
bogdanm 2:7e489126fe7a 186 create_resources();
bogdanm 2:7e489126fe7a 187
bogdanm 2:7e489126fe7a 188 // Run the NSDL event loop (never returns)
bogdanm 2:7e489126fe7a 189 nsdl_event_loop();
terohoo 0:2edbfea18d23 190 }