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:
Fri Mar 21 06:46:10 2014 +0000
Revision:
12:b695198175ee
Parent:
11:3e9c8d80891e
Child:
13:b52d83adc1ab
print mac, ip, gateway and mask

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 8:f0ecb62bda47 32 uint8_t ep_type[] = {"mbed_lpc1768_appboard"};
zdshelby 8:f0ecb62bda47 33 uint8_t lifetime_ptr[] = {"60"};
terohoo 0:2edbfea18d23 34
bogdanm 2:7e489126fe7a 35 // ****************************************************************************
bogdanm 2:7e489126fe7a 36 // Ethernet initialization
terohoo 0:2edbfea18d23 37
terohoo 0:2edbfea18d23 38 EthernetInterface eth;
terohoo 0:2edbfea18d23 39
bogdanm 2:7e489126fe7a 40 static void ethernet_init()
terohoo 0:2edbfea18d23 41 {
bogdanm 2:7e489126fe7a 42 char mbed_uid[33]; // for creating unique name for the board
terohoo 0:2edbfea18d23 43
terohoo 0:2edbfea18d23 44 /* Initialize network */
terohoo 0:2edbfea18d23 45 #ifdef DHCP
bogdanm 2:7e489126fe7a 46 NSDL_DEBUG("DHCP in use\r\n");
terohoo 0:2edbfea18d23 47 eth.init();
terohoo 0:2edbfea18d23 48 #else
terohoo 0:2edbfea18d23 49 eth.init(IP, MASK, GW);
terohoo 0:2edbfea18d23 50 #endif
terohoo 0:2edbfea18d23 51 if(eth.connect(30000) == 0)
terohoo 0:2edbfea18d23 52 pc.printf("Connect OK\n\r");
terohoo 0:2edbfea18d23 53
sstark 12:b695198175ee 54 char* mac = eth.getMACAddress();
sstark 12:b695198175ee 55 char* ipAddr = eth.getIPAddress();
sstark 12:b695198175ee 56 char* gateway = eth.getGateway();
sstark 12:b695198175ee 57 char* nmask = eth.getNetworkMask();
sstark 12:b695198175ee 58 printf("mac=%s, ip=%s, gateway=%s, mask=%s\n", mac, ipAddr, gateway, nmask);
sstark 12:b695198175ee 59
terohoo 0:2edbfea18d23 60 mbed_interface_uid(mbed_uid);
terohoo 0:2edbfea18d23 61 mbed_uid[32] = '\0';
zdshelby 8:f0ecb62bda47 62 strncat(endpoint_name, mbed_uid + 27, 20 - strlen(endpoint_name));
terohoo 0:2edbfea18d23 63
terohoo 0:2edbfea18d23 64 lcd.locate(0,11);
terohoo 0:2edbfea18d23 65 lcd.printf("IP:%s", eth.getIPAddress());
terohoo 0:2edbfea18d23 66
bogdanm 2:7e489126fe7a 67 NSDL_DEBUG("IP Address:%s ", eth.getIPAddress());
bogdanm 2:7e489126fe7a 68 }
bogdanm 2:7e489126fe7a 69
bogdanm 2:7e489126fe7a 70 // ****************************************************************************
bogdanm 2:7e489126fe7a 71 // NSP initialization
bogdanm 2:7e489126fe7a 72
bogdanm 2:7e489126fe7a 73 UDPSocket server;
bogdanm 2:7e489126fe7a 74 Endpoint nsp;
bogdanm 2:7e489126fe7a 75
bogdanm 2:7e489126fe7a 76 static void nsp_init()
bogdanm 2:7e489126fe7a 77 {
terohoo 0:2edbfea18d23 78 server.init();
terohoo 0:2edbfea18d23 79 server.bind(NSP_PORT);
terohoo 0:2edbfea18d23 80
bogdanm 2:7e489126fe7a 81 nsp.set_address(NSP_ADDRESS, NSP_PORT);
bogdanm 2:7e489126fe7a 82
bogdanm 2:7e489126fe7a 83 NSDL_DEBUG("name: %s", endpoint_name);
bogdanm 2:7e489126fe7a 84 NSDL_DEBUG("NSP=%s - port %d\n", NSP_ADDRESS, NSP_PORT);
terohoo 0:2edbfea18d23 85
terohoo 0:2edbfea18d23 86 lcd.locate(0,22);
zdshelby 8:f0ecb62bda47 87 lcd.printf("EP:%s\n", endpoint_name);
bogdanm 2:7e489126fe7a 88 }
terohoo 0:2edbfea18d23 89
bogdanm 2:7e489126fe7a 90 // ****************************************************************************
bogdanm 2:7e489126fe7a 91 // Resource creation
terohoo 0:2edbfea18d23 92
bogdanm 2:7e489126fe7a 93 static int create_resources()
bogdanm 2:7e489126fe7a 94 {
bogdanm 2:7e489126fe7a 95 sn_nsdl_resource_info_s *resource_ptr = NULL;
bogdanm 2:7e489126fe7a 96 sn_nsdl_ep_parameters_s *endpoint_ptr = NULL;
bogdanm 2:7e489126fe7a 97
bogdanm 2:7e489126fe7a 98 NSDL_DEBUG("Creating resources");
terohoo 0:2edbfea18d23 99
terohoo 0:2edbfea18d23 100 /* Create resources */
bogdanm 2:7e489126fe7a 101 resource_ptr = (sn_nsdl_resource_info_s*)nsdl_alloc(sizeof(sn_nsdl_resource_info_s));
terohoo 0:2edbfea18d23 102 if(!resource_ptr)
terohoo 0:2edbfea18d23 103 return 0;
terohoo 0:2edbfea18d23 104 memset(resource_ptr, 0, sizeof(sn_nsdl_resource_info_s));
terohoo 0:2edbfea18d23 105
bogdanm 2:7e489126fe7a 106 resource_ptr->resource_parameters_ptr = (sn_nsdl_resource_parameters_s*)nsdl_alloc(sizeof(sn_nsdl_resource_parameters_s));
terohoo 0:2edbfea18d23 107 if(!resource_ptr->resource_parameters_ptr)
terohoo 0:2edbfea18d23 108 {
bogdanm 2:7e489126fe7a 109 nsdl_free(resource_ptr);
terohoo 0:2edbfea18d23 110 return 0;
terohoo 0:2edbfea18d23 111 }
terohoo 0:2edbfea18d23 112 memset(resource_ptr->resource_parameters_ptr, 0, sizeof(sn_nsdl_resource_parameters_s));
terohoo 0:2edbfea18d23 113
bogdanm 2:7e489126fe7a 114 // Static resources
zdshelby 8:f0ecb62bda47 115 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 116 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 8:f0ecb62bda47 117 nsdl_create_static_resource(resource_ptr, sizeof("3/0/16")-1, (uint8_t*) "3/0/16", 0, 0, (uint8_t*) "U", sizeof("U")-1);
terohoo 0:2edbfea18d23 118
bogdanm 2:7e489126fe7a 119 // Dynamic resources
bogdanm 2:7e489126fe7a 120 create_temperature_resource(resource_ptr);
bogdanm 2:7e489126fe7a 121 create_light_resource(resource_ptr);
bogdanm 2:7e489126fe7a 122 create_gps_resource(resource_ptr);
bogdanm 2:7e489126fe7a 123 create_relay_resource(resource_ptr);
terohoo 0:2edbfea18d23 124
terohoo 0:2edbfea18d23 125 /* Register with NSP */
bogdanm 2:7e489126fe7a 126 endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)endpoint_name, ep_type, lifetime_ptr);
terohoo 0:2edbfea18d23 127 if(sn_nsdl_register_endpoint(endpoint_ptr) != 0)
terohoo 0:2edbfea18d23 128 pc.printf("NSP registering failed\r\n");
terohoo 0:2edbfea18d23 129 else
terohoo 0:2edbfea18d23 130 pc.printf("NSP registering OK\r\n");
bogdanm 2:7e489126fe7a 131 nsdl_clean_register_endpoint(&endpoint_ptr);
terohoo 0:2edbfea18d23 132
bogdanm 2:7e489126fe7a 133 nsdl_free(resource_ptr->resource_parameters_ptr);
bogdanm 2:7e489126fe7a 134 nsdl_free(resource_ptr);
terohoo 0:2edbfea18d23 135 return 1;
terohoo 0:2edbfea18d23 136 }
terohoo 0:2edbfea18d23 137
bogdanm 2:7e489126fe7a 138 // ****************************************************************************
bogdanm 2:7e489126fe7a 139 // Program entry point
terohoo 0:2edbfea18d23 140
bogdanm 2:7e489126fe7a 141 int main()
terohoo 0:2edbfea18d23 142 {
bogdanm 2:7e489126fe7a 143 lcd.cls();
bogdanm 2:7e489126fe7a 144 lcd.locate(0,0);
zdshelby 11:3e9c8d80891e 145 lcd.printf("NanoService LWM2M");
zdshelby 11:3e9c8d80891e 146 NSDL_DEBUG("NanoService LWM2M Demo for LPC1768 App Board\n");
bogdanm 2:7e489126fe7a 147
bogdanm 2:7e489126fe7a 148 // Initialize Ethernet interface first
bogdanm 2:7e489126fe7a 149 ethernet_init();
terohoo 1:e35d7f10999a 150
bogdanm 2:7e489126fe7a 151 // Initialize NSP node
bogdanm 2:7e489126fe7a 152 nsp_init();
bogdanm 2:7e489126fe7a 153
bogdanm 2:7e489126fe7a 154 // Initialize NSDL stack
bogdanm 2:7e489126fe7a 155 nsdl_init();
bogdanm 2:7e489126fe7a 156
bogdanm 2:7e489126fe7a 157 // Create NSDL resources
bogdanm 2:7e489126fe7a 158 create_resources();
bogdanm 2:7e489126fe7a 159
bogdanm 2:7e489126fe7a 160 // Run the NSDL event loop (never returns)
bogdanm 2:7e489126fe7a 161 nsdl_event_loop();
terohoo 0:2edbfea18d23 162 }