leo hendrickson / Mbed OS example-Ethernet-mbed-Cloud-connect
Committer:
leothedragon
Date:
Tue May 04 08:55:12 2021 +0000
Revision:
0:8f0bb79ddd48
nmn

Who changed what in which revision?

UserRevisionLine numberNew contents of line
leothedragon 0:8f0bb79ddd48 1 // ----------------------------------------------------------------------------
leothedragon 0:8f0bb79ddd48 2 // Copyright 2016-2018 ARM Ltd.
leothedragon 0:8f0bb79ddd48 3 //
leothedragon 0:8f0bb79ddd48 4 // SPDX-License-Identifier: Apache-2.0
leothedragon 0:8f0bb79ddd48 5 //
leothedragon 0:8f0bb79ddd48 6 // Licensed under the Apache License, Version 2.0 (the "License");
leothedragon 0:8f0bb79ddd48 7 // you may not use this file except in compliance with the License.
leothedragon 0:8f0bb79ddd48 8 // You may obtain a copy of the License at
leothedragon 0:8f0bb79ddd48 9 //
leothedragon 0:8f0bb79ddd48 10 // http://www.apache.org/licenses/LICENSE-2.0
leothedragon 0:8f0bb79ddd48 11 //
leothedragon 0:8f0bb79ddd48 12 // Unless required by applicable law or agreed to in writing, software
leothedragon 0:8f0bb79ddd48 13 // distributed under the License is distributed on an "AS IS" BASIS,
leothedragon 0:8f0bb79ddd48 14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
leothedragon 0:8f0bb79ddd48 15 // See the License for the specific language governing permissions and
leothedragon 0:8f0bb79ddd48 16 // limitations under the License.
leothedragon 0:8f0bb79ddd48 17 // ----------------------------------------------------------------------------
leothedragon 0:8f0bb79ddd48 18
leothedragon 0:8f0bb79ddd48 19 #include "mbed.h"
leothedragon 0:8f0bb79ddd48 20 #include "mbed-cloud-client-resource.h"
leothedragon 0:8f0bb79ddd48 21 #include "simple-mbed-cloud-client.h"
leothedragon 0:8f0bb79ddd48 22
leothedragon 0:8f0bb79ddd48 23 void path_to_ids(const char* path, unsigned int *object_id,
leothedragon 0:8f0bb79ddd48 24 unsigned int *instance_id, unsigned int *resource_id) {
leothedragon 0:8f0bb79ddd48 25 int len = strlen(path);
leothedragon 0:8f0bb79ddd48 26 char *buffer = new char[len + 1];
leothedragon 0:8f0bb79ddd48 27 buffer[len] = '\0';
leothedragon 0:8f0bb79ddd48 28 strncpy(buffer, path, len);
leothedragon 0:8f0bb79ddd48 29 unsigned int index = 0;
leothedragon 0:8f0bb79ddd48 30 char * pch = strtok (buffer, "/");
leothedragon 0:8f0bb79ddd48 31
leothedragon 0:8f0bb79ddd48 32 unsigned int *ptr;
leothedragon 0:8f0bb79ddd48 33 while (pch != NULL && index < 3) {
leothedragon 0:8f0bb79ddd48 34 switch (index) {
leothedragon 0:8f0bb79ddd48 35 case 0:
leothedragon 0:8f0bb79ddd48 36 ptr = object_id;
leothedragon 0:8f0bb79ddd48 37 break;
leothedragon 0:8f0bb79ddd48 38
leothedragon 0:8f0bb79ddd48 39 case 1:
leothedragon 0:8f0bb79ddd48 40 ptr = instance_id;
leothedragon 0:8f0bb79ddd48 41 break;
leothedragon 0:8f0bb79ddd48 42
leothedragon 0:8f0bb79ddd48 43 case 2:
leothedragon 0:8f0bb79ddd48 44 ptr = resource_id;
leothedragon 0:8f0bb79ddd48 45 break;
leothedragon 0:8f0bb79ddd48 46 }
leothedragon 0:8f0bb79ddd48 47
leothedragon 0:8f0bb79ddd48 48 *ptr = atoi(pch);
leothedragon 0:8f0bb79ddd48 49 pch = strtok (NULL, "/");
leothedragon 0:8f0bb79ddd48 50 index++;
leothedragon 0:8f0bb79ddd48 51 }
leothedragon 0:8f0bb79ddd48 52
leothedragon 0:8f0bb79ddd48 53 delete[] buffer;
leothedragon 0:8f0bb79ddd48 54 }
leothedragon 0:8f0bb79ddd48 55
leothedragon 0:8f0bb79ddd48 56 MbedCloudClientResource::MbedCloudClientResource(SimpleMbedCloudClient *client, const char *path, const char *name)
leothedragon 0:8f0bb79ddd48 57 : client(client),
leothedragon 0:8f0bb79ddd48 58 resource(NULL),
leothedragon 0:8f0bb79ddd48 59 path(path),
leothedragon 0:8f0bb79ddd48 60 name(name),
leothedragon 0:8f0bb79ddd48 61 putCallback(NULL),
leothedragon 0:8f0bb79ddd48 62 postCallback(NULL),
leothedragon 0:8f0bb79ddd48 63 notificationCallback(NULL),
leothedragon 0:8f0bb79ddd48 64 internalPostCallback(this, &MbedCloudClientResource::internal_post_callback),
leothedragon 0:8f0bb79ddd48 65 internalPutCallback(this, &MbedCloudClientResource::internal_put_callback),
leothedragon 0:8f0bb79ddd48 66 internalNotificationCallback(this, &MbedCloudClientResource::internal_notification_callback)
leothedragon 0:8f0bb79ddd48 67 {
leothedragon 0:8f0bb79ddd48 68 }
leothedragon 0:8f0bb79ddd48 69
leothedragon 0:8f0bb79ddd48 70 void MbedCloudClientResource::observable(bool observable) {
leothedragon 0:8f0bb79ddd48 71 this->isObservable = observable;
leothedragon 0:8f0bb79ddd48 72 }
leothedragon 0:8f0bb79ddd48 73
leothedragon 0:8f0bb79ddd48 74 void MbedCloudClientResource::methods(unsigned int methodMask) {
leothedragon 0:8f0bb79ddd48 75 this->methodMask = methodMask;
leothedragon 0:8f0bb79ddd48 76 }
leothedragon 0:8f0bb79ddd48 77
leothedragon 0:8f0bb79ddd48 78 void MbedCloudClientResource::attach_put_callback(Callback<void(MbedCloudClientResource*, m2m::String)> callback) {
leothedragon 0:8f0bb79ddd48 79 this->putCallback = callback;
leothedragon 0:8f0bb79ddd48 80 }
leothedragon 0:8f0bb79ddd48 81
leothedragon 0:8f0bb79ddd48 82 void MbedCloudClientResource::attach_post_callback(Callback<void(MbedCloudClientResource*, const uint8_t*, uint16_t)> callback) {
leothedragon 0:8f0bb79ddd48 83 this->postCallback = callback;
leothedragon 0:8f0bb79ddd48 84 }
leothedragon 0:8f0bb79ddd48 85
leothedragon 0:8f0bb79ddd48 86 void MbedCloudClientResource::attach_notification_callback(Callback<void(MbedCloudClientResource*, const NoticationDeliveryStatus)> callback) {
leothedragon 0:8f0bb79ddd48 87 this->notificationCallback = callback;
leothedragon 0:8f0bb79ddd48 88 }
leothedragon 0:8f0bb79ddd48 89
leothedragon 0:8f0bb79ddd48 90 void MbedCloudClientResource::detach_put_callback() {
leothedragon 0:8f0bb79ddd48 91 this->putCallback = NULL;
leothedragon 0:8f0bb79ddd48 92 }
leothedragon 0:8f0bb79ddd48 93
leothedragon 0:8f0bb79ddd48 94 void MbedCloudClientResource::detach_post_callback() {
leothedragon 0:8f0bb79ddd48 95 this->postCallback = NULL;
leothedragon 0:8f0bb79ddd48 96 }
leothedragon 0:8f0bb79ddd48 97
leothedragon 0:8f0bb79ddd48 98 void MbedCloudClientResource::detach_notification_callback() {
leothedragon 0:8f0bb79ddd48 99 this->notificationCallback = NULL;
leothedragon 0:8f0bb79ddd48 100 }
leothedragon 0:8f0bb79ddd48 101
leothedragon 0:8f0bb79ddd48 102 void MbedCloudClientResource::set_value(int value) {
leothedragon 0:8f0bb79ddd48 103 this->value = "";
leothedragon 0:8f0bb79ddd48 104 this->value.append_int(value);
leothedragon 0:8f0bb79ddd48 105
leothedragon 0:8f0bb79ddd48 106 if (this->resource) {
leothedragon 0:8f0bb79ddd48 107 this->resource->set_value((uint8_t*)this->value.c_str(), this->value.size());
leothedragon 0:8f0bb79ddd48 108 }
leothedragon 0:8f0bb79ddd48 109 }
leothedragon 0:8f0bb79ddd48 110
leothedragon 0:8f0bb79ddd48 111 void MbedCloudClientResource::set_value(const char *value) {
leothedragon 0:8f0bb79ddd48 112 this->value = value;
leothedragon 0:8f0bb79ddd48 113
leothedragon 0:8f0bb79ddd48 114 if (this->resource) {
leothedragon 0:8f0bb79ddd48 115 this->resource->set_value((uint8_t*)this->value.c_str(), strlen(value));
leothedragon 0:8f0bb79ddd48 116 }
leothedragon 0:8f0bb79ddd48 117 }
leothedragon 0:8f0bb79ddd48 118
leothedragon 0:8f0bb79ddd48 119 void MbedCloudClientResource::set_value(float value) {
leothedragon 0:8f0bb79ddd48 120 char str[25];
leothedragon 0:8f0bb79ddd48 121 int length = sprintf(str, "%g", value);
leothedragon 0:8f0bb79ddd48 122
leothedragon 0:8f0bb79ddd48 123 if (this->resource) {
leothedragon 0:8f0bb79ddd48 124 this->resource->set_value((uint8_t*)str, length);
leothedragon 0:8f0bb79ddd48 125 }
leothedragon 0:8f0bb79ddd48 126 }
leothedragon 0:8f0bb79ddd48 127
leothedragon 0:8f0bb79ddd48 128 m2m::String MbedCloudClientResource::get_value() {
leothedragon 0:8f0bb79ddd48 129 if (this->resource) {
leothedragon 0:8f0bb79ddd48 130 return this->resource->get_value_string();
leothedragon 0:8f0bb79ddd48 131 } else {
leothedragon 0:8f0bb79ddd48 132 return this->value;
leothedragon 0:8f0bb79ddd48 133 }
leothedragon 0:8f0bb79ddd48 134 }
leothedragon 0:8f0bb79ddd48 135
leothedragon 0:8f0bb79ddd48 136 void MbedCloudClientResource::internal_post_callback(void *params) {
leothedragon 0:8f0bb79ddd48 137 if (!postCallback) return;
leothedragon 0:8f0bb79ddd48 138
leothedragon 0:8f0bb79ddd48 139 if (params) { // data can be NULL!
leothedragon 0:8f0bb79ddd48 140 M2MResource::M2MExecuteParameter* parameters = static_cast<M2MResource::M2MExecuteParameter*>(params);
leothedragon 0:8f0bb79ddd48 141
leothedragon 0:8f0bb79ddd48 142 // extract the data that was sent
leothedragon 0:8f0bb79ddd48 143 const uint8_t* buffer = parameters->get_argument_value();
leothedragon 0:8f0bb79ddd48 144 uint16_t length = parameters->get_argument_value_length();
leothedragon 0:8f0bb79ddd48 145
leothedragon 0:8f0bb79ddd48 146 postCallback(this, buffer, length);
leothedragon 0:8f0bb79ddd48 147 }
leothedragon 0:8f0bb79ddd48 148 }
leothedragon 0:8f0bb79ddd48 149
leothedragon 0:8f0bb79ddd48 150 void MbedCloudClientResource::internal_put_callback(const char* resource) {
leothedragon 0:8f0bb79ddd48 151 if (!putCallback) return;
leothedragon 0:8f0bb79ddd48 152
leothedragon 0:8f0bb79ddd48 153 putCallback(this, this->get_value());
leothedragon 0:8f0bb79ddd48 154 }
leothedragon 0:8f0bb79ddd48 155
leothedragon 0:8f0bb79ddd48 156 void MbedCloudClientResource::internal_notification_callback(const M2MBase& m2mbase, const NoticationDeliveryStatus status) {
leothedragon 0:8f0bb79ddd48 157 if (!notificationCallback) return;
leothedragon 0:8f0bb79ddd48 158
leothedragon 0:8f0bb79ddd48 159 notificationCallback(this, status);
leothedragon 0:8f0bb79ddd48 160 }
leothedragon 0:8f0bb79ddd48 161
leothedragon 0:8f0bb79ddd48 162 const char * MbedCloudClientResource::delivery_status_to_string(const NoticationDeliveryStatus status) {
leothedragon 0:8f0bb79ddd48 163 switch(status) {
leothedragon 0:8f0bb79ddd48 164 case NOTIFICATION_STATUS_INIT: return "Init";
leothedragon 0:8f0bb79ddd48 165 case NOTIFICATION_STATUS_BUILD_ERROR: return "Build error";
leothedragon 0:8f0bb79ddd48 166 case NOTIFICATION_STATUS_RESEND_QUEUE_FULL: return "Resend queue full";
leothedragon 0:8f0bb79ddd48 167 case NOTIFICATION_STATUS_SENT: return "Sent";
leothedragon 0:8f0bb79ddd48 168 case NOTIFICATION_STATUS_DELIVERED: return "Delivered";
leothedragon 0:8f0bb79ddd48 169 case NOTIFICATION_STATUS_SEND_FAILED: return "Send failed";
leothedragon 0:8f0bb79ddd48 170 case NOTIFICATION_STATUS_SUBSCRIBED: return "Subscribed";
leothedragon 0:8f0bb79ddd48 171 case NOTIFICATION_STATUS_UNSUBSCRIBED: return "Unsubscribed";
leothedragon 0:8f0bb79ddd48 172 default: return "Unknown";
leothedragon 0:8f0bb79ddd48 173 }
leothedragon 0:8f0bb79ddd48 174 }
leothedragon 0:8f0bb79ddd48 175
leothedragon 0:8f0bb79ddd48 176 M2MResource *MbedCloudClientResource::get_m2m_resource() {
leothedragon 0:8f0bb79ddd48 177 return resource;
leothedragon 0:8f0bb79ddd48 178 }
leothedragon 0:8f0bb79ddd48 179
leothedragon 0:8f0bb79ddd48 180 void MbedCloudClientResource::get_data(mcc_resource_def *resourceDef) {
leothedragon 0:8f0bb79ddd48 181 path_to_ids(this->path.c_str(), &(resourceDef->object_id), &(resourceDef->instance_id), &(resourceDef->resource_id));
leothedragon 0:8f0bb79ddd48 182 resourceDef->name = this->name;
leothedragon 0:8f0bb79ddd48 183 resourceDef->method_mask = this->methodMask;
leothedragon 0:8f0bb79ddd48 184 resourceDef->observable = this->isObservable;
leothedragon 0:8f0bb79ddd48 185 resourceDef->value = this->get_value();
leothedragon 0:8f0bb79ddd48 186 resourceDef->put_callback = &(this->internalPutCallback);
leothedragon 0:8f0bb79ddd48 187 resourceDef->post_callback = &(this->internalPostCallback);
leothedragon 0:8f0bb79ddd48 188 resourceDef->notification_callback = &(this->internalNotificationCallback);
leothedragon 0:8f0bb79ddd48 189 }
leothedragon 0:8f0bb79ddd48 190
leothedragon 0:8f0bb79ddd48 191 void MbedCloudClientResource::set_m2m_resource(M2MResource *res) {
leothedragon 0:8f0bb79ddd48 192 this->resource = res;
leothedragon 0:8f0bb79ddd48 193 }
leothedragon 0:8f0bb79ddd48 194
leothedragon 0:8f0bb79ddd48 195 int MbedCloudClientResource::get_value_int() {
leothedragon 0:8f0bb79ddd48 196 if (!this->resource) return 0;
leothedragon 0:8f0bb79ddd48 197
leothedragon 0:8f0bb79ddd48 198 return this->resource->get_value_int();
leothedragon 0:8f0bb79ddd48 199 }
leothedragon 0:8f0bb79ddd48 200
leothedragon 0:8f0bb79ddd48 201 float MbedCloudClientResource::get_value_float() {
leothedragon 0:8f0bb79ddd48 202 if (!this->resource) return 0.0f;
leothedragon 0:8f0bb79ddd48 203
leothedragon 0:8f0bb79ddd48 204 return atof(this->get_value().c_str());
leothedragon 0:8f0bb79ddd48 205 }