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 06:46:54 2014 +0000
Revision:
18:0872a05cae95
Parent:
17:40ce3d963495
Child:
19:e5c0b6553c11
Update the firmware version to 18

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