1

Committer:
group-STM32F031
Date:
Wed Mar 21 18:13:41 2018 +0000
Revision:
0:d193d40d4fa1
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
group-STM32F031 0:d193d40d4fa1 1 // ----------------------------------------------------------------------------
group-STM32F031 0:d193d40d4fa1 2 // Copyright 2016-2017 ARM Ltd.
group-STM32F031 0:d193d40d4fa1 3 //
group-STM32F031 0:d193d40d4fa1 4 // SPDX-License-Identifier: Apache-2.0
group-STM32F031 0:d193d40d4fa1 5 //
group-STM32F031 0:d193d40d4fa1 6 // Licensed under the Apache License, Version 2.0 (the "License");
group-STM32F031 0:d193d40d4fa1 7 // you may not use this file except in compliance with the License.
group-STM32F031 0:d193d40d4fa1 8 // You may obtain a copy of the License at
group-STM32F031 0:d193d40d4fa1 9 //
group-STM32F031 0:d193d40d4fa1 10 // http://www.apache.org/licenses/LICENSE-2.0
group-STM32F031 0:d193d40d4fa1 11 //
group-STM32F031 0:d193d40d4fa1 12 // Unless required by applicable law or agreed to in writing, software
group-STM32F031 0:d193d40d4fa1 13 // distributed under the License is distributed on an "AS IS" BASIS,
group-STM32F031 0:d193d40d4fa1 14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
group-STM32F031 0:d193d40d4fa1 15 // See the License for the specific language governing permissions and
group-STM32F031 0:d193d40d4fa1 16 // limitations under the License.
group-STM32F031 0:d193d40d4fa1 17 // ----------------------------------------------------------------------------
group-STM32F031 0:d193d40d4fa1 18
group-STM32F031 0:d193d40d4fa1 19 #include "mbed-cloud-client/MbedCloudClient.h"
group-STM32F031 0:d193d40d4fa1 20 #include "m2mresource.h"
group-STM32F031 0:d193d40d4fa1 21 #include "mbed-client/m2minterface.h"
group-STM32F031 0:d193d40d4fa1 22 #include <stdio.h>
group-STM32F031 0:d193d40d4fa1 23 #include <string.h>
group-STM32F031 0:d193d40d4fa1 24
group-STM32F031 0:d193d40d4fa1 25 M2MResource* add_resource(M2MObjectList *list, uint16_t object_id, uint16_t instance_id,
group-STM32F031 0:d193d40d4fa1 26 uint16_t resource_id, const char *resource_type, M2MResourceInstance::ResourceType data_type,
group-STM32F031 0:d193d40d4fa1 27 M2MBase::Operation allowed, const char *value, bool observable, void *cb,
group-STM32F031 0:d193d40d4fa1 28 void *notification_status_cb)
group-STM32F031 0:d193d40d4fa1 29 {
group-STM32F031 0:d193d40d4fa1 30 M2MObject *object = NULL;
group-STM32F031 0:d193d40d4fa1 31 M2MObjectInstance* object_instance = NULL;
group-STM32F031 0:d193d40d4fa1 32 M2MResource* resource = NULL;
group-STM32F031 0:d193d40d4fa1 33 char name[6];
group-STM32F031 0:d193d40d4fa1 34
group-STM32F031 0:d193d40d4fa1 35 //check if object already exists.
group-STM32F031 0:d193d40d4fa1 36 if (!list->empty()) {
group-STM32F031 0:d193d40d4fa1 37 M2MObjectList::const_iterator it;
group-STM32F031 0:d193d40d4fa1 38 it = list->begin();
group-STM32F031 0:d193d40d4fa1 39 for ( ; it != list->end(); it++ ) {
group-STM32F031 0:d193d40d4fa1 40 if ((*it)->name_id() == object_id) {
group-STM32F031 0:d193d40d4fa1 41 object = (*it);
group-STM32F031 0:d193d40d4fa1 42 break;
group-STM32F031 0:d193d40d4fa1 43 }
group-STM32F031 0:d193d40d4fa1 44 }
group-STM32F031 0:d193d40d4fa1 45 }
group-STM32F031 0:d193d40d4fa1 46 //Create new object if needed.
group-STM32F031 0:d193d40d4fa1 47 if (!object) {
group-STM32F031 0:d193d40d4fa1 48 snprintf(name, 6, "%d", object_id);
group-STM32F031 0:d193d40d4fa1 49 object = M2MInterfaceFactory::create_object(name);
group-STM32F031 0:d193d40d4fa1 50 list->push_back(object);
group-STM32F031 0:d193d40d4fa1 51 } else {
group-STM32F031 0:d193d40d4fa1 52 //check if instance already exists.
group-STM32F031 0:d193d40d4fa1 53 object_instance = object->object_instance(instance_id);
group-STM32F031 0:d193d40d4fa1 54 }
group-STM32F031 0:d193d40d4fa1 55 //Create new instance if needed.
group-STM32F031 0:d193d40d4fa1 56 if (!object_instance) {
group-STM32F031 0:d193d40d4fa1 57 object_instance = object->create_object_instance(instance_id);
group-STM32F031 0:d193d40d4fa1 58 }
group-STM32F031 0:d193d40d4fa1 59 //create the recource.
group-STM32F031 0:d193d40d4fa1 60 snprintf(name, 6, "%d", resource_id);
group-STM32F031 0:d193d40d4fa1 61 resource = object_instance->create_dynamic_resource(name, resource_type, data_type, observable);
group-STM32F031 0:d193d40d4fa1 62 //Set value if available.
group-STM32F031 0:d193d40d4fa1 63 if (value) {
group-STM32F031 0:d193d40d4fa1 64 resource->set_value((const unsigned char*)value, strlen(value));
group-STM32F031 0:d193d40d4fa1 65 }
group-STM32F031 0:d193d40d4fa1 66 //Set allowed operations for accessing the resource.
group-STM32F031 0:d193d40d4fa1 67 resource->set_operation(allowed);
group-STM32F031 0:d193d40d4fa1 68 if (observable) {
group-STM32F031 0:d193d40d4fa1 69 resource->set_notification_delivery_status_cb(
group-STM32F031 0:d193d40d4fa1 70 (void(*)(const M2MBase&,
group-STM32F031 0:d193d40d4fa1 71 const NoticationDeliveryStatus,
group-STM32F031 0:d193d40d4fa1 72 void*))notification_status_cb, NULL);
group-STM32F031 0:d193d40d4fa1 73 }
group-STM32F031 0:d193d40d4fa1 74
group-STM32F031 0:d193d40d4fa1 75 //Set callback of PUT or POST operation is enabled.
group-STM32F031 0:d193d40d4fa1 76 //NOTE: This function does not support setting them both.
group-STM32F031 0:d193d40d4fa1 77 if(allowed & M2MResourceInstance::PUT_ALLOWED) {
group-STM32F031 0:d193d40d4fa1 78 resource->set_value_updated_function((void(*)(const char*))cb);
group-STM32F031 0:d193d40d4fa1 79 } else if (allowed & M2MResourceInstance::POST_ALLOWED){
group-STM32F031 0:d193d40d4fa1 80 resource->set_execute_function((void(*)(void*))cb);
group-STM32F031 0:d193d40d4fa1 81 }
group-STM32F031 0:d193d40d4fa1 82
group-STM32F031 0:d193d40d4fa1 83 return resource;
group-STM32F031 0:d193d40d4fa1 84 }