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