custom for >5 resources

Fork of mbedConnectorInterface by Doug Anson

Revision:
21:8487990a3baa
Parent:
20:abfbaf524192
Child:
22:192b598ba389
diff -r abfbaf524192 -r 8487990a3baa api/DynamicResource.cpp
--- a/api/DynamicResource.cpp	Thu Mar 05 18:23:28 2015 +0000
+++ b/api/DynamicResource.cpp	Fri Mar 13 05:46:48 2015 +0000
@@ -31,6 +31,9 @@
     this->m_res_type = string(res_type);
     this->m_observable = observable;
     this->m_res_mask = res_mask;
+    this->m_obs_number = 0;
+    this->m_obs_token_ptr = NULL;
+    this->m_obs_token_len = 0;
 }
 
 // constructor (input initial value)
@@ -39,6 +42,9 @@
     this->m_res_type = string(res_type);
     this->m_observable = observable;
     this->m_res_mask = res_mask;
+    this->m_obs_number = 0;
+    this->m_obs_token_ptr = NULL;
+    this->m_obs_token_len = 0;
 }
 
 // constructor (strings)
@@ -47,6 +53,9 @@
     this->m_res_type = res_type;
     this->m_observable = observable;
     this->m_res_mask = res_mask;
+    this->m_obs_number = 0;
+    this->m_obs_token_ptr = NULL;
+    this->m_obs_token_len = 0;
 }
 
 // copy constructor
@@ -55,11 +64,15 @@
     this->m_res_type = resource.m_res_type;
     this->m_observable = resource.m_observable;
     this->m_res_mask = resource.m_res_mask;
+    this->m_obs_number = resource.m_obs_number;
+    this->m_obs_token_ptr = resource.m_obs_token_ptr;
+    this->m_obs_token_len = resource.m_obs_token_len;
 }
 
 // destructor
 DynamicResource::~DynamicResource()
 {
+    if(this->m_obs_token_ptr) free(this->m_obs_token_ptr);
 }
 
 // bind resource to NSDL
@@ -110,10 +123,32 @@
             // fill in the CoAP response payload
             coap_res_ptr->payload_len = length;
             coap_res_ptr->payload_ptr = (uint8_t *)value_buffer;
+            
+            // Observation handling... 
+            if(received_coap_ptr->token_ptr) {
+                if(this->m_obs_token_ptr) {
+                    free(this->m_obs_token_ptr);
+                    this->m_obs_token_ptr = NULL;
+                }
+                this->m_obs_token_ptr = (uint8_t*)malloc(received_coap_ptr->token_len);
+                if(this->m_obs_token_ptr) {
+                    memcpy(this->m_obs_token_ptr, received_coap_ptr->token_ptr,received_coap_ptr->token_len);
+                    this->m_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 = &this->m_obs_number;
+                coap_res_ptr->options_list_ptr->observe_len = 1;
+                this->m_obs_number++;
+            }
 
             // build out the response and send...
-            sn_nsdl_send_coap_message(address, coap_res_ptr);
-        } else {
+            sn_nsdl_send_coap_message(address,coap_res_ptr);
+        } 
+        else {
             this->logger()->log("ERROR: resource(GET) mask is munged (mask: 0x%x)",this->m_res_mask);
         }
     } else if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_PUT) {
@@ -129,14 +164,56 @@
 
                 // build out the response and send...
                 this->logger()->log("resource(PUT) completed for [%s]...",key.c_str());
-                coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CHANGED);
-                sn_nsdl_send_coap_message(address, coap_res_ptr);
+                coap_res_ptr = sn_coap_build_response(received_coap_ptr,COAP_MSG_CODE_RESPONSE_CHANGED);
+                sn_nsdl_send_coap_message(address,coap_res_ptr);
             } else {
                 this->logger()->log("ERROR: resource(PUT) mask is munged (mask: 0x%x)",this->m_res_mask);
             }
         } else {
             this->logger()->log("ERROR: Binder(PUT) payload is NULL...");
         }
+    } else if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_POST) {
+        if(received_coap_ptr->payload_len > 0) {
+            // process the POST if we have registered a callback for it...
+            if ((this->m_res_mask&SN_GRS_POST_ALLOWED) != 0) {
+                // binder interacts only with strings
+                string value = this->coapDataToString(received_coap_ptr->payload_ptr,received_coap_ptr->payload_len);
+
+                // call the resource post() to set the resource value
+                this->logger()->log("Calling resource(POST) with [%s]=[%s]...",key.c_str(),value.c_str());
+                this->post(value);
+
+                // build out the response and send...
+                this->logger()->log("resource(POST) completed for [%s]...",key.c_str());
+                coap_res_ptr = sn_coap_build_response(received_coap_ptr,COAP_MSG_CODE_RESPONSE_CHANGED);
+                sn_nsdl_send_coap_message(address,coap_res_ptr);
+            } else {
+                this->logger()->log("ERROR: resource(POST) mask is munged (mask: 0x%x)",this->m_res_mask);
+            }
+        } else {
+            this->logger()->log("ERROR: Binder(POST) payload is NULL...");
+        }
+    } else if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_DELETE) {
+        if(received_coap_ptr->payload_len > 0) {
+            // process the DELETE if we have registered a callback for it...
+            if ((this->m_res_mask&SN_GRS_DELETE_ALLOWED) != 0) {
+                // binder interacts only with strings
+                string value = this->coapDataToString(received_coap_ptr->payload_ptr,received_coap_ptr->payload_len);
+
+                // call the resource del() to set the resource value
+                this->logger()->log("Calling resource(DELETE) with [%s]=[%s]...",key.c_str(),value.c_str());
+                this->del(value);
+
+                // build out the response and send...
+                this->logger()->log("resource(DELETE) completed for [%s]...",key.c_str());
+                coap_res_ptr = sn_coap_build_response(received_coap_ptr,COAP_MSG_CODE_RESPONSE_CHANGED);
+                sn_nsdl_send_coap_message(address,coap_res_ptr);
+            } else {
+                this->logger()->log("ERROR: resource(DELETE) mask is munged (mask: 0x%x)",this->m_res_mask);
+            }
+        } else {
+            this->logger()->log("ERROR: Binder(DELETE) payload is NULL...");
+        }
     }
 
     sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr);
@@ -144,6 +221,20 @@
     return 0;
 }
 
+// send the notification
+int DynamicResource::notify(const string data) {
+    return this->notify((uint8_t *)data.c_str(),(int)data.length());
+}
+
+// send the notification
+int DynamicResource::notify(uint8_t *data,int data_length) {
+    int status = sn_nsdl_send_observation_notification(this->m_obs_token_ptr,this->m_obs_token_len,data,data_length, &this->m_obs_number, 1,COAP_MSG_TYPE_NON_CONFIRMABLE,0);
+    if (status == 0) {
+        this->logger()->log("ERROR: resource(NOTIFY) send failed...");
+    }
+    return status;
+}
+
 // default PUT (does nothing)
 void DynamicResource::put(const string value)
 {
@@ -151,6 +242,20 @@
     ;
 }
 
+// default POST (does nothing)
+void DynamicResource::post(const string value)
+{
+    // not used by default
+    ;
+}
+
+// default DELETE (does nothing)
+void DynamicResource::del(const string value)
+{
+    // not used by default
+    ;
+}
+
 // convenience method to get the URI from its buffer field...
 string DynamicResource::coapDataToString(uint8_t *coap_data_ptr,int coap_data_ptr_length)
 {