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:
Mon Oct 20 02:17:21 2014 +0000
Parent:
0:93cc592ffdb5
Child:
2:7afc089e3bf3
Commit message:
Add presence sensor resource

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
resources/IPSO_presence.cpp Show annotated file Show diff for this revision Revisions of this file
resources/IPSO_presence.h Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Mon Oct 20 02:04:20 2014 +0000
+++ b/main.cpp	Mon Oct 20 02:17:21 2014 +0000
@@ -8,6 +8,7 @@
 #include "LEDbar.h"
 #include "IPSO_illuminance.h"
 #include "gas_sensor.h"
+#include "IPSO_presence.h"
 
 Serial pc(USBTX, USBRX); // tx, rx
 
@@ -140,6 +141,7 @@
     create_LEDbar_resource(resource_ptr);
     create_IPSO_illuminance_resource(resource_ptr);
     create_gas_sensor_resource(resource_ptr);
+    create_IPSO_presence_resource(resource_ptr);
 
         /* Register with NSP */
     endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)endpoint_name, ep_type, lifetime_ptr);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/resources/IPSO_presence.cpp	Mon Oct 20 02:17:21 2014 +0000
@@ -0,0 +1,62 @@
+// IPSO Presence sensor resource implementation
+
+#include "mbed.h"
+#include "nsdl_support.h"
+
+#define PRESENCE_RES_ID    "3302/0/5500"
+#define PRESENCE_RES_RT    "urn:X-ipso:presence"
+
+extern Serial pc;
+uint8_t presence_max_age = 0; 
+uint8_t presence_content_type = 50;
+
+DigitalIn presenceSensor(D2);
+bool presence;
+char presenceString[2];
+
+/* 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)
+{
+    sn_coap_hdr_s *coap_res_ptr = 0;
+    presence = presenceSensor.read();
+    sprintf(presenceString,"%d", presence);
+    pc.printf("illum callback\r\n");
+    pc.printf("illum percent %s\r\n", presenceString);
+
+    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;
+        
+        coap_res_ptr->content_type_ptr = &presence_content_type;
+        coap_res_ptr->content_type_len = sizeof(presence_content_type);
+        
+        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");
+            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);
+
+        sn_nsdl_send_coap_message(address, coap_res_ptr);
+        nsdl_free(coap_res_ptr->options_list_ptr);
+        coap_res_ptr->options_list_ptr = NULL;
+        coap_res_ptr->content_type_ptr = NULL;// parser_release below tries to free this memory
+
+    }
+
+    sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr);
+
+    return 0;
+}
+
+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));
+    return 0;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/resources/IPSO_presence.h	Mon Oct 20 02:17:21 2014 +0000
@@ -0,0 +1,10 @@
+// IPSO Presence Sensor implementation
+
+#ifndef IPSO_PRESENCE_H
+#define IPSO_PRESENCE_H
+
+#include "nsdl_support.h"
+
+int create_IPSO_presence_resource(sn_nsdl_resource_info_s *resource_ptr);
+
+#endif