Simulated product dispenser

Dependencies:   HTS221

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

Committer:
JimCarver
Date:
Thu Oct 25 14:00:12 2018 +0000
Revision:
4:e518dde96e59
Parent:
0:6b753f761943
Simulated dispenser

Who changed what in which revision?

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