Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of mbed-cloud-workshop-connect-HTS221 by
simple-mbed-cloud-client/mbed_cloud_client_resource.cpp@4:e518dde96e59, 2018-10-25 (annotated)
- 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?
| User | Revision | Line number | New 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.h" |
| JimCarver | 0:6b753f761943 | 20 | #include "mbed_cloud_client_resource.h" |
| JimCarver | 0:6b753f761943 | 21 | #include "simple-mbed-cloud-client.h" |
| JimCarver | 0:6b753f761943 | 22 | |
| JimCarver | 0:6b753f761943 | 23 | void path_to_ids(const char* path, unsigned int *object_id, |
| JimCarver | 0:6b753f761943 | 24 | unsigned int *instance_id, unsigned int *resource_id) { |
| JimCarver | 0:6b753f761943 | 25 | int len = strlen(path); |
| JimCarver | 0:6b753f761943 | 26 | char *buffer = new char[len + 1]; |
| JimCarver | 0:6b753f761943 | 27 | buffer[len] = '\0'; |
| JimCarver | 0:6b753f761943 | 28 | strncpy(buffer, path, len); |
| JimCarver | 0:6b753f761943 | 29 | unsigned int index = 0; |
| JimCarver | 0:6b753f761943 | 30 | char * pch = strtok (buffer, "/"); |
| JimCarver | 0:6b753f761943 | 31 | |
| JimCarver | 0:6b753f761943 | 32 | unsigned int *ptr; |
| JimCarver | 0:6b753f761943 | 33 | while (pch != NULL && index < 3) { |
| JimCarver | 0:6b753f761943 | 34 | switch (index) { |
| JimCarver | 0:6b753f761943 | 35 | case 0: |
| JimCarver | 0:6b753f761943 | 36 | ptr = object_id; |
| JimCarver | 0:6b753f761943 | 37 | break; |
| JimCarver | 0:6b753f761943 | 38 | |
| JimCarver | 0:6b753f761943 | 39 | case 1: |
| JimCarver | 0:6b753f761943 | 40 | ptr = instance_id; |
| JimCarver | 0:6b753f761943 | 41 | break; |
| JimCarver | 0:6b753f761943 | 42 | |
| JimCarver | 0:6b753f761943 | 43 | case 2: |
| JimCarver | 0:6b753f761943 | 44 | ptr = resource_id; |
| JimCarver | 0:6b753f761943 | 45 | break; |
| JimCarver | 0:6b753f761943 | 46 | } |
| JimCarver | 0:6b753f761943 | 47 | |
| JimCarver | 0:6b753f761943 | 48 | *ptr = atoi(pch); |
| JimCarver | 0:6b753f761943 | 49 | pch = strtok (NULL, "/"); |
| JimCarver | 0:6b753f761943 | 50 | index++; |
| JimCarver | 0:6b753f761943 | 51 | } |
| JimCarver | 0:6b753f761943 | 52 | |
| JimCarver | 0:6b753f761943 | 53 | delete[] buffer; |
| JimCarver | 0:6b753f761943 | 54 | } |
| JimCarver | 0:6b753f761943 | 55 | |
| JimCarver | 0:6b753f761943 | 56 | MbedCloudClientResource::MbedCloudClientResource(SimpleMbedCloudClient *client, const char *path, const char *name) |
| JimCarver | 0:6b753f761943 | 57 | : client(client), |
| JimCarver | 0:6b753f761943 | 58 | resource(NULL), |
| JimCarver | 0:6b753f761943 | 59 | path(path), |
| JimCarver | 0:6b753f761943 | 60 | name(name), |
| JimCarver | 0:6b753f761943 | 61 | putCallback(NULL), |
| JimCarver | 0:6b753f761943 | 62 | postCallback(NULL), |
| JimCarver | 0:6b753f761943 | 63 | notificationCallback(NULL), |
| JimCarver | 0:6b753f761943 | 64 | internalPostCallback(this, &MbedCloudClientResource::internal_post_callback), |
| JimCarver | 0:6b753f761943 | 65 | internalPutCallback(this, &MbedCloudClientResource::internal_put_callback), |
| JimCarver | 0:6b753f761943 | 66 | internalNotificationCallback(this, &MbedCloudClientResource::internal_notification_callback) |
| JimCarver | 0:6b753f761943 | 67 | { |
| JimCarver | 0:6b753f761943 | 68 | } |
| JimCarver | 0:6b753f761943 | 69 | |
| JimCarver | 0:6b753f761943 | 70 | void MbedCloudClientResource::observable(bool observable) { |
| JimCarver | 0:6b753f761943 | 71 | this->isObservable = observable; |
| JimCarver | 0:6b753f761943 | 72 | } |
| JimCarver | 0:6b753f761943 | 73 | |
| JimCarver | 0:6b753f761943 | 74 | void MbedCloudClientResource::methods(unsigned int methodMask) { |
| JimCarver | 0:6b753f761943 | 75 | this->methodMask = methodMask; |
| JimCarver | 0:6b753f761943 | 76 | } |
| JimCarver | 0:6b753f761943 | 77 | |
| JimCarver | 0:6b753f761943 | 78 | void MbedCloudClientResource::attach_put_callback(Callback<void(MbedCloudClientResource*, m2m::String)> callback) { |
| JimCarver | 0:6b753f761943 | 79 | this->putCallback = callback; |
| JimCarver | 0:6b753f761943 | 80 | } |
| JimCarver | 0:6b753f761943 | 81 | |
| JimCarver | 0:6b753f761943 | 82 | void MbedCloudClientResource::attach_post_callback(Callback<void(MbedCloudClientResource*, const uint8_t*, uint16_t)> callback) { |
| JimCarver | 0:6b753f761943 | 83 | this->postCallback = callback; |
| JimCarver | 0:6b753f761943 | 84 | } |
| JimCarver | 0:6b753f761943 | 85 | |
| JimCarver | 0:6b753f761943 | 86 | void MbedCloudClientResource::attach_notification_callback(Callback<void(MbedCloudClientResource*, const NoticationDeliveryStatus)> callback) { |
| JimCarver | 0:6b753f761943 | 87 | this->notificationCallback = callback; |
| JimCarver | 0:6b753f761943 | 88 | } |
| JimCarver | 0:6b753f761943 | 89 | |
| JimCarver | 0:6b753f761943 | 90 | void MbedCloudClientResource::detach_put_callback() { |
| JimCarver | 0:6b753f761943 | 91 | this->putCallback = NULL; |
| JimCarver | 0:6b753f761943 | 92 | } |
| JimCarver | 0:6b753f761943 | 93 | |
| JimCarver | 0:6b753f761943 | 94 | void MbedCloudClientResource::detach_post_callback() { |
| JimCarver | 0:6b753f761943 | 95 | this->postCallback = NULL; |
| JimCarver | 0:6b753f761943 | 96 | } |
| JimCarver | 0:6b753f761943 | 97 | |
| JimCarver | 0:6b753f761943 | 98 | void MbedCloudClientResource::detach_notification_callback() { |
| JimCarver | 0:6b753f761943 | 99 | this->notificationCallback = NULL; |
| JimCarver | 0:6b753f761943 | 100 | } |
| JimCarver | 0:6b753f761943 | 101 | |
| JimCarver | 0:6b753f761943 | 102 | void MbedCloudClientResource::set_value(int value) { |
| JimCarver | 0:6b753f761943 | 103 | this->value = ""; |
| JimCarver | 0:6b753f761943 | 104 | this->value.append_int(value); |
| JimCarver | 0:6b753f761943 | 105 | |
| JimCarver | 0:6b753f761943 | 106 | if (this->resource) { |
| JimCarver | 0:6b753f761943 | 107 | this->resource->set_value((uint8_t*)this->value.c_str(), this->value.size()); |
| JimCarver | 0:6b753f761943 | 108 | } |
| JimCarver | 0:6b753f761943 | 109 | } |
| JimCarver | 0:6b753f761943 | 110 | |
| JimCarver | 0:6b753f761943 | 111 | void MbedCloudClientResource::set_value(const char *value) { |
| JimCarver | 0:6b753f761943 | 112 | this->value = value; |
| JimCarver | 0:6b753f761943 | 113 | |
| JimCarver | 0:6b753f761943 | 114 | if (this->resource) { |
| JimCarver | 0:6b753f761943 | 115 | this->resource->set_value((uint8_t*)this->value.c_str(), strlen(value)); |
| JimCarver | 0:6b753f761943 | 116 | } |
| JimCarver | 0:6b753f761943 | 117 | } |
| JimCarver | 0:6b753f761943 | 118 | |
| JimCarver | 0:6b753f761943 | 119 | void MbedCloudClientResource::set_value(float value) { |
| JimCarver | 0:6b753f761943 | 120 | char str[25]; |
| JimCarver | 0:6b753f761943 | 121 | int length = sprintf(str, "%g", value); |
| JimCarver | 0:6b753f761943 | 122 | |
| JimCarver | 0:6b753f761943 | 123 | if (this->resource) { |
| JimCarver | 0:6b753f761943 | 124 | this->resource->set_value((uint8_t*)str, length); |
| JimCarver | 0:6b753f761943 | 125 | } |
| JimCarver | 0:6b753f761943 | 126 | } |
| JimCarver | 0:6b753f761943 | 127 | |
| JimCarver | 0:6b753f761943 | 128 | m2m::String MbedCloudClientResource::get_value() { |
| JimCarver | 0:6b753f761943 | 129 | if (this->resource) { |
| JimCarver | 0:6b753f761943 | 130 | return this->resource->get_value_string(); |
| JimCarver | 0:6b753f761943 | 131 | } else { |
| JimCarver | 0:6b753f761943 | 132 | return this->value; |
| JimCarver | 0:6b753f761943 | 133 | } |
| JimCarver | 0:6b753f761943 | 134 | } |
| JimCarver | 0:6b753f761943 | 135 | |
| JimCarver | 0:6b753f761943 | 136 | void MbedCloudClientResource::internal_post_callback(void *params) { |
| JimCarver | 0:6b753f761943 | 137 | if (!postCallback) return; |
| JimCarver | 0:6b753f761943 | 138 | |
| JimCarver | 0:6b753f761943 | 139 | if (params) { // data can be NULL! |
| JimCarver | 0:6b753f761943 | 140 | M2MResource::M2MExecuteParameter* parameters = static_cast<M2MResource::M2MExecuteParameter*>(params); |
| JimCarver | 0:6b753f761943 | 141 | |
| JimCarver | 0:6b753f761943 | 142 | // extract the data that was sent |
| JimCarver | 0:6b753f761943 | 143 | const uint8_t* buffer = parameters->get_argument_value(); |
| JimCarver | 0:6b753f761943 | 144 | uint16_t length = parameters->get_argument_value_length(); |
| JimCarver | 0:6b753f761943 | 145 | |
| JimCarver | 0:6b753f761943 | 146 | postCallback(this, buffer, length); |
| JimCarver | 0:6b753f761943 | 147 | } |
| JimCarver | 0:6b753f761943 | 148 | } |
| JimCarver | 0:6b753f761943 | 149 | |
| JimCarver | 0:6b753f761943 | 150 | void MbedCloudClientResource::internal_put_callback(const char* resource) { |
| JimCarver | 0:6b753f761943 | 151 | if (!putCallback) return; |
| JimCarver | 0:6b753f761943 | 152 | |
| JimCarver | 0:6b753f761943 | 153 | putCallback(this, this->get_value()); |
| JimCarver | 0:6b753f761943 | 154 | } |
| JimCarver | 0:6b753f761943 | 155 | |
| JimCarver | 0:6b753f761943 | 156 | void MbedCloudClientResource::internal_notification_callback(const M2MBase& m2mbase, const NoticationDeliveryStatus status) { |
| JimCarver | 0:6b753f761943 | 157 | if (!notificationCallback) return; |
| JimCarver | 0:6b753f761943 | 158 | |
| JimCarver | 0:6b753f761943 | 159 | notificationCallback(this, status); |
| JimCarver | 0:6b753f761943 | 160 | } |
| JimCarver | 0:6b753f761943 | 161 | |
| JimCarver | 0:6b753f761943 | 162 | const char * MbedCloudClientResource::delivery_status_to_string(const NoticationDeliveryStatus status) { |
| JimCarver | 0:6b753f761943 | 163 | switch(status) { |
| JimCarver | 0:6b753f761943 | 164 | case NOTIFICATION_STATUS_INIT: return "Init"; |
| JimCarver | 0:6b753f761943 | 165 | case NOTIFICATION_STATUS_BUILD_ERROR: return "Build error"; |
| JimCarver | 0:6b753f761943 | 166 | case NOTIFICATION_STATUS_RESEND_QUEUE_FULL: return "Resend queue full"; |
| JimCarver | 0:6b753f761943 | 167 | case NOTIFICATION_STATUS_SENT: return "Sent"; |
| JimCarver | 0:6b753f761943 | 168 | case NOTIFICATION_STATUS_DELIVERED: return "Delivered"; |
| JimCarver | 0:6b753f761943 | 169 | case NOTIFICATION_STATUS_SEND_FAILED: return "Send failed"; |
| JimCarver | 0:6b753f761943 | 170 | case NOTIFICATION_STATUS_SUBSCRIBED: return "Subscribed"; |
| JimCarver | 0:6b753f761943 | 171 | case NOTIFICATION_STATUS_UNSUBSCRIBED: return "Unsubscribed"; |
| JimCarver | 0:6b753f761943 | 172 | default: return "Unknown"; |
| JimCarver | 0:6b753f761943 | 173 | } |
| JimCarver | 0:6b753f761943 | 174 | } |
| JimCarver | 0:6b753f761943 | 175 | |
| JimCarver | 0:6b753f761943 | 176 | M2MResource *MbedCloudClientResource::get_m2m_resource() { |
| JimCarver | 0:6b753f761943 | 177 | return resource; |
| JimCarver | 0:6b753f761943 | 178 | } |
| JimCarver | 0:6b753f761943 | 179 | |
| JimCarver | 0:6b753f761943 | 180 | void MbedCloudClientResource::get_data(mcc_resource_def *resourceDef) { |
| JimCarver | 0:6b753f761943 | 181 | path_to_ids(this->path.c_str(), &(resourceDef->object_id), &(resourceDef->instance_id), &(resourceDef->resource_id)); |
| JimCarver | 0:6b753f761943 | 182 | resourceDef->name = this->name; |
| JimCarver | 0:6b753f761943 | 183 | resourceDef->method_mask = this->methodMask; |
| JimCarver | 0:6b753f761943 | 184 | resourceDef->observable = this->isObservable; |
| JimCarver | 0:6b753f761943 | 185 | resourceDef->value = this->get_value(); |
| JimCarver | 0:6b753f761943 | 186 | resourceDef->put_callback = &(this->internalPutCallback); |
| JimCarver | 0:6b753f761943 | 187 | resourceDef->post_callback = &(this->internalPostCallback); |
| JimCarver | 0:6b753f761943 | 188 | resourceDef->notification_callback = &(this->internalNotificationCallback); |
| JimCarver | 0:6b753f761943 | 189 | } |
| JimCarver | 0:6b753f761943 | 190 | |
| JimCarver | 0:6b753f761943 | 191 | void MbedCloudClientResource::set_m2m_resource(M2MResource *res) { |
| JimCarver | 0:6b753f761943 | 192 | this->resource = res; |
| JimCarver | 0:6b753f761943 | 193 | } |
| JimCarver | 0:6b753f761943 | 194 | |
| JimCarver | 0:6b753f761943 | 195 | int MbedCloudClientResource::get_value_int() { |
| JimCarver | 0:6b753f761943 | 196 | if (!this->resource) return 0; |
| JimCarver | 0:6b753f761943 | 197 | |
| JimCarver | 0:6b753f761943 | 198 | return this->resource->get_value_int(); |
| JimCarver | 0:6b753f761943 | 199 | } |
| JimCarver | 0:6b753f761943 | 200 | |
| JimCarver | 0:6b753f761943 | 201 | float MbedCloudClientResource::get_value_float() { |
| JimCarver | 0:6b753f761943 | 202 | if (!this->resource) return 0.0f; |
| JimCarver | 0:6b753f761943 | 203 | |
| JimCarver | 0:6b753f761943 | 204 | return atof(this->get_value().c_str()); |
| JimCarver | 0:6b753f761943 | 205 | } |
