Simulated product dispenser

Dependencies:   HTS221

Fork of mbed-cloud-workshop-connect-HTS221 by Jim Carver

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers resource.cpp Source File

resource.cpp

00001 // ----------------------------------------------------------------------------
00002 // Copyright 2016-2018 ARM Ltd.
00003 //
00004 // SPDX-License-Identifier: Apache-2.0
00005 //
00006 // Licensed under the Apache License, Version 2.0 (the "License");
00007 // you may not use this file except in compliance with the License.
00008 // You may obtain a copy of the License at
00009 //
00010 //     http://www.apache.org/licenses/LICENSE-2.0
00011 //
00012 // Unless required by applicable law or agreed to in writing, software
00013 // distributed under the License is distributed on an "AS IS" BASIS,
00014 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015 // See the License for the specific language governing permissions and
00016 // limitations under the License.
00017 // ----------------------------------------------------------------------------
00018 
00019 #include "mbed-cloud-client/MbedCloudClient.h"
00020 #include "m2mresource.h"
00021 #include "mbed-client/m2minterface.h"
00022 #include <stdio.h>
00023 #include <string.h>
00024 #include "mbed.h"
00025 
00026 static void notification_delivery_status_cb_thunk(const M2MBase& base,
00027                                                   const NoticationDeliveryStatus status,
00028                                                   void *client_args) {
00029     ((Callback<void(const M2MBase& base, const NoticationDeliveryStatus status)>*)client_args)->call(base, status);
00030 }
00031 
00032 M2MResource* add_resource(M2MObjectList *list, uint16_t object_id, uint16_t instance_id,
00033                           uint16_t resource_id, const char *resource_type, M2MResourceInstance::ResourceType data_type,
00034                           M2MBase::Operation allowed, const char *value, bool observable, Callback<void(const char*)> *put_cb,
00035                           Callback<void(void*)> *post_cb,
00036                           Callback<void(const M2MBase&, const NoticationDeliveryStatus)> *notification_status_cb)
00037 {
00038     M2MObject *object = NULL;
00039     M2MObjectInstance* object_instance = NULL;
00040     M2MResource* resource = NULL;
00041     char name[6];
00042 
00043     //check if object already exists.
00044     if (!list->empty()) {
00045         M2MObjectList::const_iterator it;
00046         it = list->begin();
00047         for ( ; it != list->end(); it++ ) {
00048             if ((*it)->name_id() == object_id) {
00049                 object = (*it);
00050                 break;
00051             }
00052         }
00053     }
00054     //Create new object if needed.
00055     if (!object) {
00056         snprintf(name, 6, "%d", object_id);
00057         object = M2MInterfaceFactory::create_object(name);
00058         list->push_back(object);
00059     } else {
00060         //check if instance already exists.
00061         object_instance = object->object_instance(instance_id);
00062     }
00063     //Create new instance if needed.
00064     if (!object_instance) {
00065         object_instance = object->create_object_instance(instance_id);
00066     }
00067     //create the recource.
00068     snprintf(name, 6, "%d", resource_id);
00069     resource = object_instance->create_dynamic_resource(name, resource_type, data_type, observable);
00070     //Set value if available.
00071     if (strcmp(value, "") != 0) {
00072         resource->set_value((const unsigned char*)value, strlen(value));
00073     }
00074     //Set allowed operations for accessing the resource.
00075     resource->set_operation(allowed);
00076     if (observable) {
00077         resource->set_notification_delivery_status_cb(notification_delivery_status_cb_thunk, notification_status_cb);
00078     }
00079 
00080     if (put_cb) {
00081         resource->set_value_updated_function(
00082             FP1<void, const char*>(put_cb,
00083                 (void (Callback<void(const char*)>::*)(const char*))
00084                     &Callback<void(const char*)>::call));
00085     }
00086 
00087     if (post_cb) {
00088         resource->set_execute_function(FP1<void, void*>(post_cb,
00089             (void (Callback<void(void*)>::*)(void*))
00090                 &Callback<void(void*)>::call));
00091     }
00092 
00093     return resource;
00094 }