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 mbed_cloud_client_resource.cpp Source File

mbed_cloud_client_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.h"
00020 #include "mbed_cloud_client_resource.h"
00021 #include "simple-mbed-cloud-client.h"
00022 
00023 void path_to_ids(const char* path, unsigned int *object_id,
00024                  unsigned int *instance_id, unsigned int *resource_id) {
00025     int len = strlen(path);
00026     char *buffer = new char[len + 1];
00027     buffer[len] = '\0';
00028     strncpy(buffer, path, len);
00029     unsigned int index = 0;
00030     char * pch = strtok (buffer, "/");
00031 
00032     unsigned int *ptr;
00033     while (pch != NULL && index < 3) {
00034         switch (index) {
00035             case 0:
00036                 ptr = object_id;
00037             break;
00038 
00039             case 1:
00040                 ptr = instance_id;
00041             break;
00042 
00043             case 2:
00044                 ptr = resource_id;
00045             break;
00046         }
00047 
00048         *ptr = atoi(pch);
00049         pch = strtok (NULL, "/");
00050         index++;
00051     }
00052 
00053     delete[] buffer;
00054 }
00055 
00056 MbedCloudClientResource::MbedCloudClientResource(SimpleMbedCloudClient *client, const char *path, const char *name)
00057 : client(client),
00058   resource(NULL),
00059   path(path),
00060   name(name),
00061   putCallback(NULL),
00062   postCallback(NULL),
00063   notificationCallback(NULL),
00064   internalPostCallback(this, &MbedCloudClientResource::internal_post_callback),
00065   internalPutCallback(this, &MbedCloudClientResource::internal_put_callback),
00066   internalNotificationCallback(this, &MbedCloudClientResource::internal_notification_callback)
00067 {
00068 }
00069 
00070 void MbedCloudClientResource::observable(bool observable) {
00071     this->isObservable = observable;
00072 }
00073 
00074 void MbedCloudClientResource::methods(unsigned int methodMask) {
00075     this->methodMask = methodMask;
00076 }
00077 
00078 void MbedCloudClientResource::attach_put_callback(Callback<void(MbedCloudClientResource*, m2m::String)> callback) {
00079     this->putCallback = callback;
00080 }
00081 
00082 void MbedCloudClientResource::attach_post_callback(Callback<void(MbedCloudClientResource*, const uint8_t*, uint16_t)> callback) {
00083     this->postCallback = callback;
00084 }
00085 
00086 void MbedCloudClientResource::attach_notification_callback(Callback<void(MbedCloudClientResource*, const NoticationDeliveryStatus)> callback) {
00087     this->notificationCallback = callback;
00088 }
00089 
00090 void MbedCloudClientResource::detach_put_callback() {
00091     this->putCallback = NULL;
00092 }
00093 
00094 void MbedCloudClientResource::detach_post_callback() {
00095     this->postCallback = NULL;
00096 }
00097 
00098 void MbedCloudClientResource::detach_notification_callback() {
00099     this->notificationCallback = NULL;
00100 }
00101 
00102 void MbedCloudClientResource::set_value(int value) {
00103     this->value = "";
00104     this->value.append_int(value);
00105 
00106     if (this->resource) {
00107         this->resource->set_value((uint8_t*)this->value.c_str(), this->value.size());
00108     }
00109 }
00110 
00111 void MbedCloudClientResource::set_value(const char *value) {
00112     this->value = value;
00113 
00114     if (this->resource) {
00115         this->resource->set_value((uint8_t*)this->value.c_str(), strlen(value));
00116     }
00117 }
00118 
00119 void MbedCloudClientResource::set_value(float value) {
00120     char str[25];
00121     int length = sprintf(str, "%g", value);
00122 
00123     if (this->resource) {
00124         this->resource->set_value((uint8_t*)str, length);
00125     }
00126 }
00127 
00128 m2m::String MbedCloudClientResource::get_value() {
00129     if (this->resource) {
00130         return this->resource->get_value_string();
00131     } else {
00132         return this->value;
00133     }
00134 }
00135 
00136 void MbedCloudClientResource::internal_post_callback(void *params) {
00137     if (!postCallback) return;
00138 
00139     if (params) { // data can be NULL!
00140         M2MResource::M2MExecuteParameter* parameters = static_cast<M2MResource::M2MExecuteParameter*>(params);
00141 
00142         // extract the data that was sent
00143         const uint8_t* buffer = parameters->get_argument_value();
00144         uint16_t length = parameters->get_argument_value_length();
00145 
00146         postCallback(this, buffer, length);
00147     }
00148 }
00149 
00150 void MbedCloudClientResource::internal_put_callback(const char* resource) {
00151     if (!putCallback) return;
00152 
00153     putCallback(this, this->get_value());
00154 }
00155 
00156 void MbedCloudClientResource::internal_notification_callback(const M2MBase& m2mbase, const NoticationDeliveryStatus status) {
00157     if (!notificationCallback) return;
00158 
00159     notificationCallback(this, status);
00160 }
00161 
00162 const char * MbedCloudClientResource::delivery_status_to_string(const NoticationDeliveryStatus status) {
00163     switch(status) {
00164         case NOTIFICATION_STATUS_INIT: return "Init";
00165         case NOTIFICATION_STATUS_BUILD_ERROR: return "Build error";
00166         case NOTIFICATION_STATUS_RESEND_QUEUE_FULL: return "Resend queue full";
00167         case NOTIFICATION_STATUS_SENT: return "Sent";
00168         case NOTIFICATION_STATUS_DELIVERED: return "Delivered";
00169         case NOTIFICATION_STATUS_SEND_FAILED: return "Send failed";
00170         case NOTIFICATION_STATUS_SUBSCRIBED: return "Subscribed";
00171         case NOTIFICATION_STATUS_UNSUBSCRIBED: return "Unsubscribed";
00172         default: return "Unknown";
00173     }
00174 }
00175 
00176 M2MResource *MbedCloudClientResource::get_m2m_resource() {
00177     return resource;
00178 }
00179 
00180 void MbedCloudClientResource::get_data(mcc_resource_def *resourceDef) {
00181     path_to_ids(this->path.c_str(), &(resourceDef->object_id), &(resourceDef->instance_id), &(resourceDef->resource_id));
00182     resourceDef->name = this->name;
00183     resourceDef->method_mask = this->methodMask;
00184     resourceDef->observable = this->isObservable;
00185     resourceDef->value = this->get_value();
00186     resourceDef->put_callback = &(this->internalPutCallback);
00187     resourceDef->post_callback = &(this->internalPostCallback);
00188     resourceDef->notification_callback = &(this->internalNotificationCallback);
00189 }
00190 
00191 void MbedCloudClientResource::set_m2m_resource(M2MResource *res) {
00192     this->resource = res;
00193 }
00194 
00195 int MbedCloudClientResource::get_value_int() {
00196     if (!this->resource) return 0;
00197 
00198     return this->resource->get_value_int();
00199 }
00200 
00201 float MbedCloudClientResource::get_value_float() {
00202     if (!this->resource) return 0.0f;
00203 
00204     return atof(this->get_value().c_str());
00205 }