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