Project with example IPSO resources for LED bar, Gas Sensor, and Light Sensor

Dependencies:   EthernetInterface LED_Bar mbed-rtos mbed nsdl_lib

Files at this revision

API Documentation at this revision

Comitter:
michaeljkoster
Date:
Sat Oct 25 03:16:58 2014 +0000
Parent:
4:69ea903414fe
Child:
6:4fa917ca6aa4
Commit message:
Added observe to presence sensor resource

Changed in this revision

resources/IPSO_presence.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/resources/IPSO_presence.cpp	Sat Oct 25 01:31:08 2014 +0000
+++ b/resources/IPSO_presence.cpp	Sat Oct 25 03:16:58 2014 +0000
@@ -1,6 +1,7 @@
 // IPSO Presence sensor resource implementation
 
 #include "mbed.h"
+#include "rtos.h"
 #include "nsdl_support.h"
 
 #define PRESENCE_RES_ID    "3302/0/5500"
@@ -10,10 +11,35 @@
 uint8_t presence_max_age = 0; 
 uint8_t presence_content_type = 50;
 
+static uint8_t pres_obs_number = 0;
+static uint8_t *pres_obs_token_ptr = NULL;
+static uint8_t pres_obs_token_len = 0;
+
 DigitalIn presenceSensor(D2);
 bool presence;
 char presenceString[2];
 
+static void pres_observe_thread(void const *args)
+{
+    int32_t time = 0;
+    while (true)
+    {
+        wait(1);
+        time++;
+        //sn_nsdl_exec(time);
+        if((!(time % 10)) && pres_obs_number != 0 && pres_obs_token_ptr != NULL)
+        {
+            pres_obs_number++;
+            sprintf(presenceString,"%d", presenceSensor.read());            
+            if(sn_nsdl_send_observation_notification(pres_obs_token_ptr, pres_obs_token_len, (uint8_t*)presenceString, sizeof((uint8_t*)presenceString)-1, &pres_obs_number, 1, COAP_MSG_TYPE_NON_CONFIRMABLE, 0) == 0)
+                pc.printf("Presence observation sending failed\r\n");
+            else
+                pc.printf("Presence observation\r\n");
+        }
+    }
+}
+
+
 /* Only GET method allowed */
 static uint8_t presence_resource_cb(sn_coap_hdr_s *received_coap_ptr, sn_nsdl_addr_s *address, sn_proto_info_s * proto)
 {
@@ -26,7 +52,7 @@
     if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_GET)
     {
         coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CONTENT);
-
+   
         coap_res_ptr->payload_len = strlen(presenceString);
         coap_res_ptr->payload_ptr = (uint8_t*)presenceString;
         
@@ -36,13 +62,38 @@
         coap_res_ptr->options_list_ptr = (sn_coap_options_list_s*)nsdl_alloc(sizeof(sn_coap_options_list_s));
         if(!coap_res_ptr->options_list_ptr)
             {
-            pc.printf("cant alloc option list for max-age\r\n");
+            pc.printf("cant alloc option list\r\n");
             coap_res_ptr->options_list_ptr = NULL; //FIXME report error and recover
             }
         memset(coap_res_ptr->options_list_ptr, 0, sizeof(sn_coap_options_list_s));
         coap_res_ptr->options_list_ptr->max_age_ptr = &presence_max_age;
         coap_res_ptr->options_list_ptr->max_age_len = sizeof(presence_max_age);
+        
+        if(received_coap_ptr->token_ptr)
+            {
+            pc.printf("Token included\r\n");
+            if(pres_obs_token_ptr)
+                {   
+                free(pres_obs_token_ptr);
+                pres_obs_token_ptr = 0;
+                }
+            pres_obs_token_ptr = (uint8_t*)malloc(received_coap_ptr->token_len);
+            if(pres_obs_token_ptr)
+                {
+                memcpy(pres_obs_token_ptr, received_coap_ptr->token_ptr, received_coap_ptr->token_len);
+                pres_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 = &pres_obs_number;
+            coap_res_ptr->options_list_ptr->observe_len = 1;
+            pres_obs_number++;
+            }
+ 
         sn_nsdl_send_coap_message(address, coap_res_ptr);
         nsdl_free(coap_res_ptr->options_list_ptr);
         coap_res_ptr->options_list_ptr = NULL;
@@ -57,6 +108,8 @@
 
 int create_IPSO_presence_resource(sn_nsdl_resource_info_s *resource_ptr)
 {
-    nsdl_create_dynamic_resource(resource_ptr, sizeof(PRESENCE_RES_ID)-1, (uint8_t*)PRESENCE_RES_ID, sizeof(PRESENCE_RES_RT)-1, (uint8_t*)PRESENCE_RES_RT, 0, &presence_resource_cb, (SN_GRS_GET_ALLOWED));
+    static Thread exec_thread(pres_observe_thread);
+
+    nsdl_create_dynamic_resource(resource_ptr, sizeof(PRESENCE_RES_ID)-1, (uint8_t*)PRESENCE_RES_ID, sizeof(PRESENCE_RES_RT)-1, (uint8_t*)PRESENCE_RES_RT, 1, &presence_resource_cb, (SN_GRS_GET_ALLOWED));
     return 0;
 }
\ No newline at end of file