LED Demo for Tech Con 2014

Dependencies:   EthernetInterface PololuLedStripx mbed-rtos mbed nanoservice_client_1_12_X

Fork of LPC1768_LWM2M_Client by MBED_DEMOS

Revision:
1:09a525977925
Parent:
0:9101343a70cd
Child:
7:59c7b96ace5b
--- a/nsdl_support.cpp	Tue Jul 22 23:58:23 2014 +0000
+++ b/nsdl_support.cpp	Sat Sep 27 21:53:28 2014 +0000
@@ -5,26 +5,47 @@
 #include "mbed.h"
 #include "rtos.h"
 #include "EthernetInterface.h"
+#include "UDPSocket.h"
+#include "Endpoint.h"
+
+/*    __heapvalid((__heapprt)fprintf,stdout, 1);\*/
+#define MEM_VALID(x) \
+    int s##x=0;\
+    int *h##x = new int [1];\
+    std::printf("[stack]0x%08x\t[heap]0x%08x\t[memory avail]%d bytes \tLine: %d %s\r\n", &s##x, h##x, &s##x-h##x, __LINE__, __FILE__);\
+    if (h##x > &s##x)\
+    printf("collision\n");\
+    else\
+    delete [] h##x;\
+    __nop()
 
 extern Serial pc;
-extern EthernetInterface eth;
 extern Endpoint nsp;
 extern UDPSocket server;
-extern char endpoint_name[16];
+extern char endpoint_name[24];
 extern uint8_t ep_type[];
 extern uint8_t lifetime_ptr[];
+char null_ep_name[] = "";
+uint8_t null_ep_type[] = "";
+uint8_t null_lifetime_ptr[] = "";
+bool nsdl_reg_update_needed = false;
 
 /* The number of seconds between NSP registration messages */
-#define RD_UPDATE_PERIOD  60
+#define RD_UPDATE_PERIOD  30
 
 void *nsdl_alloc(uint16_t size)
 {
-    return malloc(size);
+    void *buf = malloc(size);
+//    printf("alloc\r\n");
+//    MEM_VALID(0);
+    return buf;
 }
 
 void nsdl_free(void* ptr_to_free)
 {
     free(ptr_to_free);
+//    printf("de-alloc\r\n");
+//    MEM_VALID(0);
 }
 
 /*
@@ -73,7 +94,7 @@
         endpoint_structure->type_ptr = typename_ptr;
         endpoint_structure->type_len =  strlen((char*)typename_ptr);
         endpoint_structure->lifetime_ptr = lifetime_ptr;
-        endpoint_structure->lifetime_len =  strlen((char*)lifetime_ptr);
+        endpoint_structure->lifetime_len =  strlen((char*)lifetime_ptr); 
     }
     return endpoint_structure;
 }
@@ -100,6 +121,21 @@
 static uint8_t rx_cb(sn_coap_hdr_s *coap_packet_ptr, sn_nsdl_addr_s *address_ptr)
 {
     pc.printf("RX callback!\r\n");
+    pc.printf("msg_code: %d \r\n", coap_packet_ptr->msg_code);
+    pc.printf("Payload length: %d bytes\r\n", coap_packet_ptr->payload_len);
+    int i;
+    pc.printf("Payload:'");
+    for (i=0; i < coap_packet_ptr->payload_len; i++)
+        pc.printf("%c", *(coap_packet_ptr->payload_ptr + i));
+    pc.printf("' \r\n");
+    if (coap_packet_ptr->options_list_ptr && coap_packet_ptr->options_list_ptr->location_path_ptr)
+    {
+        pc.printf("Location: /");
+        int i;
+        for (i=0; i < coap_packet_ptr->options_list_ptr->location_path_len; i++) pc.printf("%c", (char)(coap_packet_ptr->options_list_ptr->location_path_ptr[i]));
+        pc.printf(" \r\n");
+    }
+    //sn_coap_packet_debug(coap_packet_ptr);
     return 0;
 }
 
@@ -110,8 +146,10 @@
     while(true)
     {
         wait(RD_UPDATE_PERIOD);
-        endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)endpoint_name, ep_type, lifetime_ptr);
-        if(sn_nsdl_register_endpoint(endpoint_ptr) != 0)
+        //endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)endpoint_name, ep_type, lifetime_ptr);
+        //if(sn_nsdl_register_endpoint(endpoint_ptr) != 0)
+        endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)null_ep_name, null_ep_type, null_lifetime_ptr);
+        if(sn_nsdl_update_registration(endpoint_ptr) != 0)            
             pc.printf("NSP re-registering failed\r\n");
         else
             pc.printf("NSP re-registering OK\r\n");
@@ -134,7 +172,7 @@
 
     /* Set nsp address for library */
     set_NSP_address(nsp_addr, 5683, SN_NSDL_ADDRESS_TYPE_IPV4);
-}
+}   
 
 void nsdl_event_loop()
 {