from LPC1768_LWM2M_Client

Dependencies:   F7_Ethernet mbed-rtos mbed nsdl_lib

Fork of rtos_basic by Mbed

Revision:
8:5bcb48a04554
Parent:
7:8d9919175929
--- a/main.cpp	Tue Nov 22 18:04:36 2016 +0000
+++ b/main.cpp	Wed Dec 21 03:08:39 2016 +0000
@@ -1,22 +1,230 @@
+//#include "mbed.h"
+//#include "rtos.h"
+// 
+//DigitalOut led1(LED1);
+//Serial pc(USBTX,USBRX);//tx rx
+//Thread thread;
+//Mutex stdio_mutex;
+//uint8_t threadindex[3];
+//long count[3];
+//void led2_thread() {
+//    while (true) {
+//        //led2 = !led2;
+//        pc.printf("Hello World!\n");
+//        Thread::wait(1000);
+//        
+//    }
+//}
+//void printString(void const *args)
+//{
+//    while(true)
+//    {
+//        stdio_mutex.lock();
+//        pc.printf("My Thread Name is %d,Thread ID is %d\n",(uint8_t *)args,Thread::gettid()); 
+//        count[*(uint8_t *)args-1]++;
+//        stdio_mutex.unlock();   
+//        Thread::yield();
+//    } 
+//}
+// 
+////int main() {
+////    pc.baud(115200);
+////    thread.start(led2_thread);
+////    
+////    while (true) {
+////        led1 = !led1;
+////        Thread::wait(500);
+////    }
+////}
+//int main(){
+//    pc.baud(115200);
+//    threadindex[0]=1;
+//    threadindex[1]=2;
+//    threadindex[2]=3;
+//    Thread thread1(printString,(void*)threadindex,osPriorityNormal);
+//    Thread thread2(printString,(void*)threadindex,osPriorityNormal);
+//    Thread thread3(printString,(void*)threadindex,osPriorityNormal);
+//    while(1){
+//        stdio_mutex.lock();
+//        pc.printf("Thread1 count is %ld,Thread2 count is %ld,Thread3 count is %ld.\n",count[0],count[1],count[2]);
+//        stdio_mutex.unlock();
+//        Thread::wait(1000);
+//    }
+//}
+///*************************************************************************/
+////测试ok
+//#include "mbed.h"
+//#include "rtos.h"
+//#include "EthernetInterface.h"
+//DigitalOut led(LED1);
+//int main() {
+//EthernetInterface eth;
+////eth.init(); Use DHCP
+//eth.init("192.168.0.110","255.255.255.0","192.168.0.1");
+//eth.connect();
+//printf("IP Address is %s\n", eth.getIPAddress());
+//printf("MAC Address is %s\n", eth.getMACAddress());
+//printf("GateWay Address is %s\n", eth.getGateway());
+//while(1)
+//{
+//    led=!led;
+//    wait(1);
+//}
+//}
+/*************************************************************************/
+//测试ok
 #include "mbed.h"
 #include "rtos.h"
- 
-DigitalOut led1(LED1);
-DigitalOut led2(LED2);
-Thread thread;
- 
-void led2_thread() {
-    while (true) {
-        led2 = !led2;
-        Thread::wait(1000);
+#include "dbg.h"
+#include "nsdl_support.h"
+#include "EthernetInterface.h"
+#include "temperature.h"
+#include "light.h"
+#include "gps.h"
+#include "relay.h"
+DigitalOut led(LED1);
+Serial pc(USBTX,USBRX);//tx rx
+// ****************************************************************************
+// Configuration section
+
+// Ethernet configuration
+/* Define this to enable DHCP, otherwise manual address configuration is used */
+//#define DHCP
+
+/* Manual IP configurations, if DHCP not defined */
+#define IP      "192.168.1.210"
+#define MASK    "255.255.255.0"
+#define GW      "192.168.1.1"
+// NSP configuration
+/* Change this IP address to that of your NanoService Platform installation */
+//static const char* NSP_ADDRESS = "217.140.101.20"; /* public mbed demo server */ 
+static const char* NSP_ADDRESS = "192.168.1.100"; /* demo NSP, web interface at http://nanoservice-demo.mbed.org*/ 
+//static const char* NSP_ADDRESS = "192.168.1.200"; /* demo NSP, web interface at http://nanoservice-demo.mbed.org*/ 
+
+//static const char* NSP_ADDRESS = "5.39.83.206"; //http://leshan.eclipse.org/#/clients
+static const int NSP_PORT = 5683;
+char endpoint_name[16] = "mbed-thp";
+uint8_t ep_type[] = {"mbed_device"};
+uint8_t lifetime_ptr[] = {"1200"};
+// ****************************************************************************
+// Ethernet initialization
+
+EthernetInterface eth;
+
+static void ethernet_init()
+{
+    char mbed_uid[33]; // for creating unique name for the board
+
+    /* Initialize network */
+#ifdef DHCP
+    NSDL_DEBUG("DHCP in use\r\n");
+    eth.init();
+#else
+    eth.init(IP, MASK, GW);
+#endif
+    if(eth.connect(30000) == 0)
+        pc.printf("Connect OK\n\r");
+
+    //mbed_interface_uid(mbed_uid);
+    mbed_uid[8] = '\0';
+    //strncat(endpoint_name, mbed_uid + 27, 15 - strlen(endpoint_name));
+
+    //lcd.locate(0,11);
+    //lcd.printf("IP:%s", eth.getIPAddress());
+
+    NSDL_DEBUG("IP Address:%s ", eth.getIPAddress());
+}
+// ****************************************************************************
+// NSP initialization
+
+UDPSocket server;
+Endpoint nsp;
+
+static void nsp_init()
+{
+    server.init();
+    server.bind(NSP_PORT);
+
+    nsp.set_address(NSP_ADDRESS, NSP_PORT);
+    
+    NSDL_DEBUG("name: %s", endpoint_name);
+    NSDL_DEBUG("NSP=%s - port %d\n", NSP_ADDRESS, NSP_PORT);
+
+    //lcd.locate(0,22);
+    //lcd.printf("EP name:%s\n", endpoint_name);
+}
+// ****************************************************************************
+// Resource creation
+
+static int create_resources()
+{
+    sn_nsdl_resource_info_s *resource_ptr = NULL;
+    sn_nsdl_ep_parameters_s *endpoint_ptr = NULL;
+    
+    NSDL_DEBUG("Creating resources");
+
+    /* Create resources */
+    resource_ptr = (sn_nsdl_resource_info_s*)nsdl_alloc(sizeof(sn_nsdl_resource_info_s));
+    if(!resource_ptr)
+        return 0;
+    memset(resource_ptr, 0, sizeof(sn_nsdl_resource_info_s));
+
+    resource_ptr->resource_parameters_ptr = (sn_nsdl_resource_parameters_s*)nsdl_alloc(sizeof(sn_nsdl_resource_parameters_s));
+    if(!resource_ptr->resource_parameters_ptr)
+    {
+        nsdl_free(resource_ptr);
+        return 0;
     }
+    memset(resource_ptr->resource_parameters_ptr, 0, sizeof(sn_nsdl_resource_parameters_s));
+
+    // Static resources
+    nsdl_create_static_resource(resource_ptr, sizeof("dev/mfg")-1, (uint8_t*) "dev/mfg", 0, 0,  (uint8_t*) "Sensinode", sizeof("Sensinode")-1);
+    nsdl_create_static_resource(resource_ptr, sizeof("dev/mdl")-1, (uint8_t*) "dev/mdl", 0, 0,  (uint8_t*) "NSDL-C mbed device", sizeof("NSDL-C mbed device")-1);
+
+    // Dynamic resources
+    create_temperature_resource(resource_ptr);
+    create_light_resource(resource_ptr);
+    create_gps_resource(resource_ptr);
+    create_relay_resource(resource_ptr);
+
+        /* Register with NSP */
+    endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)endpoint_name, ep_type, lifetime_ptr);
+    if(sn_nsdl_register_endpoint(endpoint_ptr) != 0)
+        pc.printf("NSP registering failed\r\n");
+    else
+        pc.printf("NSP registering OK\r\n");
+    nsdl_clean_register_endpoint(&endpoint_ptr);
+
+    nsdl_free(resource_ptr->resource_parameters_ptr);
+    nsdl_free(resource_ptr);
+    return 1;
 }
- 
 int main() {
-    thread.start(led2_thread);
+    pc.baud(115200);
     
-    while (true) {
-        led1 = !led1;
-        Thread::wait(500);
-    }
-}
+    // Initialize Ethernet interface first
+    ethernet_init();
+    
+    // Initialize NSP node
+    nsp_init();
+    
+    // Initialize NSDL stack
+    nsdl_init();
+    
+    // Create NSDL resources
+    create_resources();
+    
+    // Run the NSDL event loop (never returns)
+    nsdl_event_loop();
+//    eth.init(); //Use DHCP
+//    eth.init("192.168.0.110","255.255.255.0","192.168.0.1");
+//    eth.connect();
+//    pc.printf("IP Address is %s\n", eth.getIPAddress());
+//    pc.printf("MAC Address is %s\n", eth.getMACAddress());
+//    pc.printf("GateWay Address is %s\n", eth.getGateway());
+//    while(1)
+//    {
+//        led=!led;
+//        wait(1);
+//    }
+}
\ No newline at end of file