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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "EthernetInterface.h"
00003 #include "C12832_lcd.h"
00004 #include "nsdl_support.h"
00005 #include "dbg.h"
00006 // Include various resources
00007 #include "temperature.h"
00008 #include "light.h"
00009 #include "gps.h"
00010 #include "relay.h"
00011 #include "rgb.h"
00012 
00013 static C12832_LCD lcd;
00014 Serial pc(USBTX, USBRX); // tx, rx
00015 
00016 // ****************************************************************************
00017 // Configuration section
00018 
00019 // Ethernet configuration
00020 /* Define this to enable DHCP, otherwise manual address configuration is used */
00021 #define DHCP 1
00022 
00023 // Define this to enable the RGB LED resource
00024 //#define USE_RGBLED 1
00025 
00026 /* Manual IP configurations, if DHCP not defined */
00027 #define IP      "10.45.0.206"
00028 #define MASK    "255.255.255.0"
00029 #define GW      "10.45.0.1"
00030 
00031 // NSP configuration
00032 /* Change this IP address to that of your NanoService Platform installation */
00033 static const char* NSP_ADDRESS = "208.111.39.209"; /* demo NSP, web interface at http://208.111.39.209/ */
00034 static const int NSP_PORT = 5683;
00035 char endpoint_name[20] = "mbed-emt-";
00036 char mbed_uid[33]; // for creating unique name for the board
00037 uint8_t ep_type[] = {"mbed_emt"};
00038 uint8_t lifetime_ptr[] = {"60"};
00039 static const char* FIRMWARE_VER = "25"; // Committed revision number
00040 char* mac;
00041 char* ipAddr;
00042 char* gateway;
00043 char* nmask;
00044 
00045 // ****************************************************************************
00046 // Ethernet initialization
00047 
00048 EthernetInterface eth;
00049 
00050 static void ethernet_init()
00051 {
00052 
00053     /* Initialize network */
00054 #ifdef DHCP
00055     NSDL_DEBUG("DHCP in use");
00056     eth.init();
00057 #else
00058     eth.init(IP, MASK, GW);
00059 #endif
00060     if(eth.connect(30000) == 0)
00061         NSDL_DEBUG("Ethernet up");
00062 
00063     mac = eth.getMACAddress();
00064     ipAddr = eth.getIPAddress();
00065     gateway = eth.getGateway();
00066     nmask = eth.getNetworkMask();
00067     NSDL_DEBUG("Network: mac=%s, ip=%s, gateway=%s, mask=%s", mac, ipAddr, gateway, nmask);
00068 
00069     mbed_interface_uid(mbed_uid);
00070     mbed_uid[32] = '\0';
00071     NSDL_DEBUG("Full interface uid=%s", mbed_uid);
00072     strncat(endpoint_name, mbed_uid + 27, 20 - strlen(endpoint_name));
00073 
00074     lcd.locate(0,11);
00075     lcd.printf("IP:%s", eth.getIPAddress());
00076 
00077 }
00078 
00079 // ****************************************************************************
00080 // NSP initialization
00081 
00082 UDPSocket server;
00083 Endpoint nsp;
00084 
00085 static void nsp_init()
00086 {
00087     server.init();
00088     server.bind(NSP_PORT);
00089 
00090     nsp.set_address(NSP_ADDRESS, NSP_PORT);
00091     
00092     NSDL_DEBUG("name: %s", endpoint_name);
00093     NSDL_DEBUG("NSP=%s - port %d\n", NSP_ADDRESS, NSP_PORT);
00094 
00095     lcd.locate(0,22);
00096     lcd.printf("EP:%s\n", endpoint_name);
00097 }
00098 
00099 // ****************************************************************************
00100 // Resource creation
00101 
00102 static int create_resources()
00103 {
00104     sn_nsdl_resource_info_s *resource_ptr = NULL;
00105     sn_nsdl_ep_parameters_s *endpoint_ptr = NULL;
00106     
00107     NSDL_DEBUG("Creating resources");
00108 
00109     /* Create resources */
00110     resource_ptr = (sn_nsdl_resource_info_s*)nsdl_alloc(sizeof(sn_nsdl_resource_info_s));
00111     if(!resource_ptr)
00112         return 0;
00113     memset(resource_ptr, 0, sizeof(sn_nsdl_resource_info_s));
00114 
00115     resource_ptr->resource_parameters_ptr = (sn_nsdl_resource_parameters_s*)nsdl_alloc(sizeof(sn_nsdl_resource_parameters_s));
00116     if(!resource_ptr->resource_parameters_ptr)
00117     {
00118         nsdl_free(resource_ptr);
00119         return 0;
00120     }
00121     memset(resource_ptr->resource_parameters_ptr, 0, sizeof(sn_nsdl_resource_parameters_s));
00122 
00123     // Static resources
00124     nsdl_create_static_resource(resource_ptr, sizeof("3/0/0")-1, (uint8_t*) "3/0/0", 0, 0,  (uint8_t*) "ARM", sizeof("ARM")-1);
00125     nsdl_create_static_resource(resource_ptr, sizeof("3/0/1")-1, (uint8_t*) "3/0/1", 0, 0,  (uint8_t*) "LPC1768 emt", sizeof("LPC1768 emt")-1);
00126     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);
00127     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));
00128    #ifdef DHCP
00129     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);
00130    #else
00131     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);  
00132    #endif
00133     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);
00134     nsdl_create_static_resource(resource_ptr, sizeof("4/0/4")-1, (uint8_t*) "4/0/4", 0, 0,  (uint8_t*) ipAddr, strlen(ipAddr));
00135     nsdl_create_static_resource(resource_ptr, sizeof("4/0/5")-1, (uint8_t*) "4/0/5", 0, 0,  (uint8_t*) gateway, strlen(gateway));
00136     nsdl_create_static_resource(resource_ptr, sizeof("3/0/16")-1, (uint8_t*) "3/0/16", 0, 0,  (uint8_t*) "UDP", sizeof("UDP")-1);
00137 
00138     // Dynamic resources
00139     create_temperature_resource(resource_ptr);
00140     //create_light_resource(resource_ptr);
00141     create_gps_resource(resource_ptr);
00142     create_relay_resource(resource_ptr);
00143 
00144 #ifdef USE_RGBLED
00145     NSDL_DEBUG("Enabling RGB LED due to USE_RGBLED=%d\n", USE_RGBLED);
00146     create_rgb_resource(resource_ptr);
00147 #else
00148     NSDL_DEBUG("Skipped RGB LED resource, change USE_RGBLED to 1 in main.cpp to test");
00149 #endif
00150 
00151     /* Register with NSP */
00152     endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)endpoint_name, ep_type, lifetime_ptr);
00153     if(sn_nsdl_register_endpoint(endpoint_ptr) != 0)
00154         pc.printf("NSP registering failed\r\n");
00155     else
00156         pc.printf("NSP registering OK\r\n");
00157     nsdl_clean_register_endpoint(&endpoint_ptr);
00158 
00159     nsdl_free(resource_ptr->resource_parameters_ptr);
00160     nsdl_free(resource_ptr);
00161     return 1;
00162 }
00163 
00164 // ****************************************************************************
00165 // Program entry point
00166 
00167 int main()
00168 {
00169     lcd.cls();
00170     lcd.locate(0,0);
00171     lcd.printf("NanoService LWM2M r%s", FIRMWARE_VER);
00172     NSDL_DEBUG("NanoService LWM2M Demo for LPC1768 App Board\n");
00173     
00174     // Initialize Ethernet interface first
00175     ethernet_init();
00176     
00177     // Initialize NSP node
00178     nsp_init();
00179     
00180     // Initialize NSDL stack
00181     nsdl_init();
00182     
00183     // Create NSDL resources
00184     create_resources();
00185     
00186     // Run the NSDL event loop (never returns)
00187     nsdl_event_loop();
00188 }