sandbox / mbed-client

Fork of mbed-client by Christopher Haster

Revision:
4:ae5178938864
Parent:
1:79b6cc67d8b4
--- a/source/m2mdevice.cpp	Fri Feb 19 17:44:50 2016 +0000
+++ b/source/m2mdevice.cpp	Sat Apr 02 00:31:13 2016 +0300
@@ -13,13 +13,16 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-#include <cstdio>
+
 #include "mbed-client/m2mdevice.h"
 #include "mbed-client/m2mconstants.h"
 #include "mbed-client/m2mobject.h"
 #include "mbed-client/m2mobjectinstance.h"
 #include "mbed-client/m2mresource.h"
-#include "ns_trace.h"
+#include "mbed-trace/mbed_trace.h"
+
+#define BUFFER_SIZE 21
+#define TRACE_GROUP "mClt"
 
 M2MDevice* M2MDevice::_instance = NULL;
 
@@ -42,36 +45,51 @@
 M2MDevice::M2MDevice()
 : M2MObject(M2M_DEVICE_ID)
 {
+    M2MBase::set_register_uri(false);
+    M2MBase::set_operation(M2MBase::GET_ALLOWED);
+
     _device_instance = M2MObject::create_object_instance();
-
+    _device_instance->set_operation(M2MBase::GET_ALLOWED);
+    _device_instance->set_register_uri(true);
     if(_device_instance) {
-        _device_instance->set_coap_content_type(COAP_CONTENT_OMA_TLV_TYPE);
-
+        _device_instance->set_coap_content_type(COAP_CONTENT_OMA_TLV_TYPE);        
         M2MResource* res = _device_instance->create_dynamic_resource(DEVICE_REBOOT,
                                                                      OMA_RESOURCE_TYPE,
                                                                      M2MResourceInstance::OPAQUE,
                                                                      false);
         if(res) {
             res->set_operation(M2MBase::POST_ALLOWED);
+            res->set_register_uri(false);
         }
 
         M2MResourceInstance* instance = _device_instance->create_dynamic_resource_instance(DEVICE_ERROR_CODE,
                                                                  OMA_RESOURCE_TYPE,
                                                                  M2MResourceInstance::INTEGER,
-                                                                 false,0);
+                                                                 true,0);
         if(instance) {
+            M2MResource * dev_res = _device_instance->resource(DEVICE_ERROR_CODE);
+            dev_res->set_register_uri(false);
             instance->set_operation(M2MBase::GET_ALLOWED);
-            instance->set_value((const uint8_t*)ERROR_CODE_VALUE.c_str(),
-                           (uint32_t)ERROR_CODE_VALUE.length());
+            uint32_t size = 0;
+            char *buffer = (char*)malloc(BUFFER_SIZE);
+            if(buffer) {
+                size = m2m::itoa_c(0, buffer);
+                if (size <= BUFFER_SIZE)
+                    instance->set_value((const uint8_t*)buffer, size);
+
+                free(buffer);
+            }
+            instance->set_register_uri(false);
         }
         res = _device_instance->create_dynamic_resource(DEVICE_SUPPORTED_BINDING_MODE,
                                                         OMA_RESOURCE_TYPE,
                                                         M2MResourceInstance::STRING,
-                                                        false);
+                                                        true);
         if(res) {
             res->set_operation(M2MBase::GET_ALLOWED);
             res->set_value((const uint8_t*)BINDING_MODE_UDP.c_str(),
                            (uint32_t)BINDING_MODE_UDP.length());
+            res->set_register_uri(false);
         }
     }
 }
@@ -125,7 +143,7 @@
             res = _device_instance->create_dynamic_resource(device_id,
                                                             OMA_RESOURCE_TYPE,
                                                             M2MResourceInstance::STRING,
-                                                            false);
+                                                            true);
 
             if(res ) {
                 res->set_operation(operation);
@@ -135,6 +153,7 @@
                     res->set_value((const uint8_t*)value.c_str(),
                                    (uint32_t)value.length());
                 }
+                res->set_register_uri(false);
             }
         }
     }
@@ -177,17 +196,19 @@
             res = _device_instance->create_dynamic_resource(device_id,
                                                             OMA_RESOURCE_TYPE,
                                                             M2MResourceInstance::INTEGER,
-                                                            false);
+                                                            true);
 
             if(res) {
-                char *buffer = (char*)memory_alloc(20);
+                char *buffer = (char*)malloc(BUFFER_SIZE);
                 if(buffer) {
-                    int size = snprintf(buffer, 20,"%lld", (long long int)value);
-                    res->set_operation(operation);
-                    res->set_value((const uint8_t*)buffer,
-                                   (uint32_t)size);
-                    memory_free(buffer);
+                    uint32_t size = m2m::itoa_c(value, buffer);
+                    if (size <= BUFFER_SIZE) {
+                        res->set_operation(operation);
+                        res->set_value((const uint8_t*)buffer, size);
+                    }
+                    free(buffer);
                 }
+                res->set_register_uri(false);
             }
         }
     }
@@ -218,18 +239,24 @@
         if(_device_instance) {
             res = _device_instance->create_dynamic_resource_instance(device_id,OMA_RESOURCE_TYPE,
                                                                      M2MResourceInstance::INTEGER,
-                                                                     false, instance_id);
+                                                                     true, instance_id);
 
+            M2MResource *resource = _device_instance->resource(device_id);
+            if(resource) {
+                resource->set_register_uri(false);
+            }
             if(res) {
-                char *buffer = (char*)memory_alloc(20);
+                char *buffer = (char*)malloc(BUFFER_SIZE);
                 if(buffer) {
-                    int size = snprintf(buffer, 20,"%lld", (long long int)value);
-                    // Only read operation is allowed for above resources
-                    res->set_operation(M2MBase::GET_ALLOWED);
-                    res->set_value((const uint8_t*)buffer,
-                                   (uint32_t)size);
-                    memory_free(buffer);
+                    uint32_t size = m2m::itoa_c(value, buffer);
+                    if (size <= BUFFER_SIZE) {
+                        res->set_value((const uint8_t*)buffer, size);
+                        // Only read operation is allowed for above resources
+                        res->set_operation(M2MBase::GET_ALLOWED);
+                    }
+                    free(buffer);
                 }
+                res->set_register_uri(false);
             }
         }
     }
@@ -249,9 +276,14 @@
             res = _device_instance->create_dynamic_resource(device_Id,
                                                             OMA_RESOURCE_TYPE,
                                                             M2MResourceInstance::OPAQUE,
-                                                            false);
+                                                            true);
+            M2MResource *resource = _device_instance->resource(device_Id);
+            if(resource) {
+                resource->set_register_uri(false);
+            }
             if(res) {
                 res->set_operation(M2MBase::POST_ALLOWED);
+                res->set_register_uri(false);
             }
         }
     }
@@ -331,12 +363,13 @@
             // If it is any of the above resource
             // set the value of the resource.
             if (check_value_range(resource, value)) {
-                char *buffer = (char*)memory_alloc(20);
+                char *buffer = (char*)malloc(BUFFER_SIZE);
                 if(buffer) {
-                    int size = snprintf(buffer, 20,"%lld",(long long int)value);
-                    success = res->set_value((const uint8_t*)buffer,
-                                             (uint32_t)size);
-                    memory_free(buffer);
+                    uint32_t size = m2m::itoa_c(value, buffer);
+                    if (size <= BUFFER_SIZE)
+                        success = res->set_value((const uint8_t*)buffer, size);
+
+                    free(buffer);
                 }
             }
         }
@@ -561,3 +594,4 @@
     }
     return success;
 }
+