Brian Daniels / Mbed 2 deprecated nespresso_demo

Dependencies:   EthernetInterface mbed-rtos mbed nsdl rgb_sensor_buffer

Fork of mbed_nsdl by Nespresso RGB Sensor

Files at this revision

API Documentation at this revision

Comitter:
GeofferyOmlette
Date:
Wed Jun 18 16:25:55 2014 +0000
Parent:
1:8c765fa735b0
Child:
3:8e1117ec91ba
Commit message:
hello

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
nsdl_support.cpp Show annotated file Show diff for this revision Revisions of this file
resources/nespresso.cpp Show annotated file Show diff for this revision Revisions of this file
resources/nespresso.h Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Thu Jun 05 16:55:49 2014 +0000
+++ b/main.cpp	Wed Jun 18 16:25:55 2014 +0000
@@ -8,6 +8,7 @@
 #include "light.h"
 #include "gps.h"
 #include "relay.h"
+#include "nespresso.h"
 
 static C12832_LCD lcd;
 Serial pc(USBTX, USBRX); // tx, rx
@@ -114,8 +115,9 @@
     create_light_resource(resource_ptr);
     create_gps_resource(resource_ptr);
     create_relay_resource(resource_ptr);
+    create_nespresso_resource(resource_ptr);
 
-        /* Register with NSP */
+    /* 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");
--- a/nsdl_support.cpp	Thu Jun 05 16:55:49 2014 +0000
+++ b/nsdl_support.cpp	Wed Jun 18 16:25:55 2014 +0000
@@ -70,8 +70,8 @@
         memset(endpoint_structure, 0, sizeof(sn_nsdl_ep_parameters_s));
         endpoint_structure->endpoint_name_ptr = name;
         endpoint_structure->endpoint_name_len = strlen((char*)name);            
-        endpoint_structure->domain_name_ptr = (uint8_t*) "id538dec6fb626d4020028e788\0";
-        endpoint_structure->domain_name_len = strlen((char*) "id538dec6fb626d4020028e788\0");
+        endpoint_structure->domain_name_ptr = (uint8_t*) "Robert\0";
+        endpoint_structure->domain_name_len = strlen((char*) "Robert\0");
         endpoint_structure->type_ptr = typename_ptr;
         endpoint_structure->type_len =  strlen((char*)typename_ptr);
         endpoint_structure->lifetime_ptr = lifetime_ptr;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/resources/nespresso.cpp	Wed Jun 18 16:25:55 2014 +0000
@@ -0,0 +1,90 @@
+// GPS resource implementation
+
+#include "mbed.h"
+#include "rtos.h"
+#include "nsdl_support.h"
+#include "nespresso.h"
+
+#define NESPRESSO_RES_ID    "sen/0"
+
+extern Serial pc;
+/* stored data for observable resource */
+static uint8_t obs_number = 0;
+static uint8_t *obs_token_ptr = NULL;
+static uint8_t obs_token_len = 0;
+
+static uint8_t res_nespresso_val[] = {"Decaffeinto Lungo (Orange)"};
+
+/* Thread for calling libNsdl exec function (cleanup, resendings etc..) */
+/* Node updates nespresso value every 10 seconds. Notification sending is done here. */
+static void exec_call_thread(void const *args)
+{
+    int32_t time = 0;
+    while (true)
+    {
+        wait(1);
+        time++;
+        sn_nsdl_exec(time);
+        if((!(time % 10)) && obs_number != 0 && obs_token_ptr != NULL)
+        {
+            obs_number++;
+            if(sn_nsdl_send_observation_notification(obs_token_ptr, obs_token_len, (uint8_t*)res_nespresso_val, (sizeof(res_nespresso_val)-1), &obs_number, 1, COAP_MSG_TYPE_NON_CONFIRMABLE, 0) == 0)
+                pc.printf("Observation sending failed\r\n");
+            else
+                pc.printf("Observation\r\n");
+        }
+    }
+}
+
+/* Only GET method allowed */
+static uint8_t nespresso_resource_cb(sn_coap_hdr_s *received_coap_ptr, sn_nsdl_addr_s *address, sn_proto_info_s * proto)
+{
+    sn_coap_hdr_s *coap_res_ptr = 0;
+
+    pc.printf("nespresso callback\r\n");
+
+    coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CONTENT);
+
+    coap_res_ptr->payload_len = sizeof(res_nespresso_val)-1;
+    coap_res_ptr->payload_ptr = (uint8_t*)res_nespresso_val;
+
+    if(received_coap_ptr->token_ptr)
+    {
+        pc.printf("Token included\r\n");
+        if(obs_token_ptr)
+        {
+            free(obs_token_ptr);
+            obs_token_ptr = 0;
+        }
+        obs_token_ptr = (uint8_t*)malloc(received_coap_ptr->token_len);
+        if(obs_token_ptr)
+        {
+            memcpy(obs_token_ptr, received_coap_ptr->token_ptr, received_coap_ptr->token_len);
+            obs_token_len = received_coap_ptr->token_len;
+        }
+    }
+
+    if(received_coap_ptr->options_list_ptr->observe)
+    {
+        coap_res_ptr->options_list_ptr = (sn_coap_options_list_s*)malloc(sizeof(sn_coap_options_list_s));
+        memset(coap_res_ptr->options_list_ptr, 0, sizeof(sn_coap_options_list_s));
+        coap_res_ptr->options_list_ptr->observe_ptr = &obs_number;
+        coap_res_ptr->options_list_ptr->observe_len = 1;
+        obs_number++;
+    }
+
+    sn_nsdl_send_coap_message(address, coap_res_ptr);
+
+    coap_res_ptr->options_list_ptr->observe_ptr = 0;
+    sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr);
+
+    return 0;
+}
+
+int create_nespresso_resource(sn_nsdl_resource_info_s *resource_ptr)
+{
+    static Thread exec_thread(exec_call_thread);
+    
+    nsdl_create_dynamic_resource(resource_ptr, sizeof(NESPRESSO_RES_ID)-1, (uint8_t*)NESPRESSO_RES_ID, 0, 0, 1, &nespresso_resource_cb, SN_GRS_GET_ALLOWED);
+    return 0;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/resources/nespresso.h	Wed Jun 18 16:25:55 2014 +0000
@@ -0,0 +1,10 @@
+// Nespresso resource implementation
+
+#ifndef NESPRESSO_H
+#define NESPRESSO_H
+
+#include "nsdl_support.h"
+
+int create_nespresso_resource(sn_nsdl_resource_info_s *resource_ptr);
+
+#endif