
Simulated product dispenser
Fork of mbed-cloud-workshop-connect-HTS221 by
simple-mbed-cloud-client/mbed-cloud-client/source/MbedCloudClient.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-2017 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-cloud-client/MbedCloudClientConfig.h" |
JimCarver | 0:6b753f761943 | 20 | #include "mbed-cloud-client/MbedCloudClient.h" |
JimCarver | 0:6b753f761943 | 21 | #include "mbed-cloud-client/SimpleM2MResource.h" |
JimCarver | 0:6b753f761943 | 22 | |
JimCarver | 0:6b753f761943 | 23 | #include "mbed-trace/mbed_trace.h" |
JimCarver | 0:6b753f761943 | 24 | |
JimCarver | 0:6b753f761943 | 25 | #include <assert.h> |
JimCarver | 0:6b753f761943 | 26 | |
JimCarver | 0:6b753f761943 | 27 | #define xstr(s) str(s) |
JimCarver | 0:6b753f761943 | 28 | #define str(s) #s |
JimCarver | 0:6b753f761943 | 29 | |
JimCarver | 0:6b753f761943 | 30 | #define TRACE_GROUP "mClt" |
JimCarver | 0:6b753f761943 | 31 | |
JimCarver | 0:6b753f761943 | 32 | MbedCloudClient::MbedCloudClient() |
JimCarver | 0:6b753f761943 | 33 | :_client(*this), |
JimCarver | 0:6b753f761943 | 34 | _value_callback(NULL), |
JimCarver | 0:6b753f761943 | 35 | _error_description(NULL) |
JimCarver | 0:6b753f761943 | 36 | { |
JimCarver | 0:6b753f761943 | 37 | } |
JimCarver | 0:6b753f761943 | 38 | |
JimCarver | 0:6b753f761943 | 39 | MbedCloudClient::~MbedCloudClient() |
JimCarver | 0:6b753f761943 | 40 | { |
JimCarver | 0:6b753f761943 | 41 | _object_list.clear(); |
JimCarver | 0:6b753f761943 | 42 | } |
JimCarver | 0:6b753f761943 | 43 | |
JimCarver | 0:6b753f761943 | 44 | void MbedCloudClient::add_objects(const M2MObjectList& object_list) |
JimCarver | 0:6b753f761943 | 45 | { |
JimCarver | 0:6b753f761943 | 46 | if(!object_list.empty()) { |
JimCarver | 0:6b753f761943 | 47 | M2MObjectList::const_iterator it; |
JimCarver | 0:6b753f761943 | 48 | it = object_list.begin(); |
JimCarver | 0:6b753f761943 | 49 | for (; it!= object_list.end(); it++) { |
JimCarver | 0:6b753f761943 | 50 | _object_list.push_back((M2MBase*)*it); |
JimCarver | 0:6b753f761943 | 51 | } |
JimCarver | 0:6b753f761943 | 52 | } |
JimCarver | 0:6b753f761943 | 53 | } |
JimCarver | 0:6b753f761943 | 54 | |
JimCarver | 0:6b753f761943 | 55 | void MbedCloudClient::add_objects(const M2MBaseList& base_list) |
JimCarver | 0:6b753f761943 | 56 | { |
JimCarver | 0:6b753f761943 | 57 | if(!base_list.empty()) { |
JimCarver | 0:6b753f761943 | 58 | M2MBaseList::const_iterator it; |
JimCarver | 0:6b753f761943 | 59 | it = base_list.begin(); |
JimCarver | 0:6b753f761943 | 60 | for (; it!= base_list.end(); it++) { |
JimCarver | 0:6b753f761943 | 61 | _object_list.push_back(*it); |
JimCarver | 0:6b753f761943 | 62 | } |
JimCarver | 0:6b753f761943 | 63 | } |
JimCarver | 0:6b753f761943 | 64 | } |
JimCarver | 0:6b753f761943 | 65 | |
JimCarver | 0:6b753f761943 | 66 | void MbedCloudClient::remove_object(M2MBase *object) |
JimCarver | 0:6b753f761943 | 67 | { |
JimCarver | 0:6b753f761943 | 68 | M2MBaseList::const_iterator it; |
JimCarver | 0:6b753f761943 | 69 | int found_index = -1; |
JimCarver | 0:6b753f761943 | 70 | int index; |
JimCarver | 0:6b753f761943 | 71 | tr_debug("MbedCloudClient::remove_object %p", object); |
JimCarver | 0:6b753f761943 | 72 | for (it = _object_list.begin(), index = 0; it != _object_list.end(); it++, index++) { |
JimCarver | 0:6b753f761943 | 73 | if(*it == object) { |
JimCarver | 0:6b753f761943 | 74 | found_index = index; |
JimCarver | 0:6b753f761943 | 75 | break; |
JimCarver | 0:6b753f761943 | 76 | } |
JimCarver | 0:6b753f761943 | 77 | } |
JimCarver | 0:6b753f761943 | 78 | if(found_index != -1) { |
JimCarver | 0:6b753f761943 | 79 | tr_debug(" object found at index %d", found_index); |
JimCarver | 0:6b753f761943 | 80 | _object_list.erase(found_index); |
JimCarver | 0:6b753f761943 | 81 | _client.connector_client().m2m_interface()->remove_object(object); |
JimCarver | 0:6b753f761943 | 82 | } |
JimCarver | 0:6b753f761943 | 83 | } |
JimCarver | 0:6b753f761943 | 84 | |
JimCarver | 0:6b753f761943 | 85 | void MbedCloudClient::set_update_callback(MbedCloudClientCallback *callback) |
JimCarver | 0:6b753f761943 | 86 | { |
JimCarver | 0:6b753f761943 | 87 | _value_callback = callback; |
JimCarver | 0:6b753f761943 | 88 | } |
JimCarver | 0:6b753f761943 | 89 | |
JimCarver | 0:6b753f761943 | 90 | bool MbedCloudClient::setup(void* iface) |
JimCarver | 0:6b753f761943 | 91 | { |
JimCarver | 0:6b753f761943 | 92 | tr_debug("MbedCloudClient setup()"); |
JimCarver | 0:6b753f761943 | 93 | // Add objects to list |
JimCarver | 0:6b753f761943 | 94 | map<string, M2MObject*>::iterator it; |
JimCarver | 0:6b753f761943 | 95 | for (it = _objects.begin(); it != _objects.end(); it++) |
JimCarver | 0:6b753f761943 | 96 | { |
JimCarver | 0:6b753f761943 | 97 | _object_list.push_back((M2MBase*)it->second); |
JimCarver | 0:6b753f761943 | 98 | } |
JimCarver | 0:6b753f761943 | 99 | _client.connector_client().m2m_interface()->set_platform_network_handler(iface); |
JimCarver | 0:6b753f761943 | 100 | |
JimCarver | 0:6b753f761943 | 101 | _client.initialize_and_register(_object_list); |
JimCarver | 0:6b753f761943 | 102 | return true; |
JimCarver | 0:6b753f761943 | 103 | } |
JimCarver | 0:6b753f761943 | 104 | |
JimCarver | 0:6b753f761943 | 105 | void MbedCloudClient::on_registered(void(*fn)(void)) |
JimCarver | 0:6b753f761943 | 106 | { |
JimCarver | 0:6b753f761943 | 107 | FP0<void> fp(fn); |
JimCarver | 0:6b753f761943 | 108 | _on_registered = fp; |
JimCarver | 0:6b753f761943 | 109 | } |
JimCarver | 0:6b753f761943 | 110 | |
JimCarver | 0:6b753f761943 | 111 | |
JimCarver | 0:6b753f761943 | 112 | void MbedCloudClient::on_error(void(*fn)(int)) |
JimCarver | 0:6b753f761943 | 113 | { |
JimCarver | 0:6b753f761943 | 114 | _on_error = fn; |
JimCarver | 0:6b753f761943 | 115 | } |
JimCarver | 0:6b753f761943 | 116 | |
JimCarver | 0:6b753f761943 | 117 | |
JimCarver | 0:6b753f761943 | 118 | void MbedCloudClient::on_unregistered(void(*fn)(void)) |
JimCarver | 0:6b753f761943 | 119 | { |
JimCarver | 0:6b753f761943 | 120 | FP0<void> fp(fn); |
JimCarver | 0:6b753f761943 | 121 | _on_unregistered = fp; |
JimCarver | 0:6b753f761943 | 122 | } |
JimCarver | 0:6b753f761943 | 123 | |
JimCarver | 0:6b753f761943 | 124 | void MbedCloudClient::on_registration_updated(void(*fn)(void)) |
JimCarver | 0:6b753f761943 | 125 | { |
JimCarver | 0:6b753f761943 | 126 | FP0<void> fp(fn); |
JimCarver | 0:6b753f761943 | 127 | _on_registration_updated = fp; |
JimCarver | 0:6b753f761943 | 128 | } |
JimCarver | 0:6b753f761943 | 129 | |
JimCarver | 0:6b753f761943 | 130 | void MbedCloudClient::keep_alive() |
JimCarver | 0:6b753f761943 | 131 | { |
JimCarver | 0:6b753f761943 | 132 | _client.connector_client().update_registration(); |
JimCarver | 0:6b753f761943 | 133 | } |
JimCarver | 0:6b753f761943 | 134 | |
JimCarver | 0:6b753f761943 | 135 | void MbedCloudClient::register_update() |
JimCarver | 0:6b753f761943 | 136 | { |
JimCarver | 0:6b753f761943 | 137 | _client.connector_client().update_registration(); |
JimCarver | 0:6b753f761943 | 138 | } |
JimCarver | 0:6b753f761943 | 139 | |
JimCarver | 0:6b753f761943 | 140 | void MbedCloudClient::close() |
JimCarver | 0:6b753f761943 | 141 | { |
JimCarver | 0:6b753f761943 | 142 | _client.connector_client().m2m_interface()->unregister_object(NULL); |
JimCarver | 0:6b753f761943 | 143 | } |
JimCarver | 0:6b753f761943 | 144 | |
JimCarver | 0:6b753f761943 | 145 | const ConnectorClientEndpointInfo *MbedCloudClient::endpoint_info() const |
JimCarver | 0:6b753f761943 | 146 | { |
JimCarver | 0:6b753f761943 | 147 | return _client.connector_client().endpoint_info(); |
JimCarver | 0:6b753f761943 | 148 | } |
JimCarver | 0:6b753f761943 | 149 | |
JimCarver | 0:6b753f761943 | 150 | void MbedCloudClient::set_queue_sleep_handler(callback_handler handler) |
JimCarver | 0:6b753f761943 | 151 | { |
JimCarver | 0:6b753f761943 | 152 | _client.connector_client().m2m_interface()->set_queue_sleep_handler(handler); |
JimCarver | 0:6b753f761943 | 153 | } |
JimCarver | 0:6b753f761943 | 154 | |
JimCarver | 0:6b753f761943 | 155 | void MbedCloudClient::set_random_number_callback(random_number_cb callback) |
JimCarver | 0:6b753f761943 | 156 | { |
JimCarver | 0:6b753f761943 | 157 | _client.connector_client().m2m_interface()->set_random_number_callback(callback); |
JimCarver | 0:6b753f761943 | 158 | } |
JimCarver | 0:6b753f761943 | 159 | |
JimCarver | 0:6b753f761943 | 160 | void MbedCloudClient::set_entropy_callback(entropy_cb callback) |
JimCarver | 0:6b753f761943 | 161 | { |
JimCarver | 0:6b753f761943 | 162 | _client.connector_client().m2m_interface()->set_entropy_callback(callback); |
JimCarver | 0:6b753f761943 | 163 | } |
JimCarver | 0:6b753f761943 | 164 | |
JimCarver | 0:6b753f761943 | 165 | bool MbedCloudClient::set_device_resource_value(M2MDevice::DeviceResource resource, |
JimCarver | 0:6b753f761943 | 166 | const std::string &value) |
JimCarver | 0:6b753f761943 | 167 | { |
JimCarver | 0:6b753f761943 | 168 | return _client.set_device_resource_value(resource, value); |
JimCarver | 0:6b753f761943 | 169 | } |
JimCarver | 0:6b753f761943 | 170 | |
JimCarver | 0:6b753f761943 | 171 | #ifdef MBED_CLOUD_CLIENT_SUPPORT_UPDATE |
JimCarver | 0:6b753f761943 | 172 | void MbedCloudClient::set_update_authorize_handler(void (*handler)(int32_t request)) |
JimCarver | 0:6b753f761943 | 173 | { |
JimCarver | 0:6b753f761943 | 174 | _client.set_update_authorize_handler(handler); |
JimCarver | 0:6b753f761943 | 175 | } |
JimCarver | 0:6b753f761943 | 176 | |
JimCarver | 0:6b753f761943 | 177 | void MbedCloudClient::set_update_progress_handler(void (*handler)(uint32_t progress, uint32_t total)) |
JimCarver | 0:6b753f761943 | 178 | { |
JimCarver | 0:6b753f761943 | 179 | _client.set_update_progress_handler(handler); |
JimCarver | 0:6b753f761943 | 180 | } |
JimCarver | 0:6b753f761943 | 181 | |
JimCarver | 0:6b753f761943 | 182 | void MbedCloudClient::update_authorize(int32_t request) |
JimCarver | 0:6b753f761943 | 183 | { |
JimCarver | 0:6b753f761943 | 184 | _client.update_authorize(request); |
JimCarver | 0:6b753f761943 | 185 | } |
JimCarver | 0:6b753f761943 | 186 | #endif |
JimCarver | 0:6b753f761943 | 187 | |
JimCarver | 0:6b753f761943 | 188 | const char *MbedCloudClient::error_description() const |
JimCarver | 0:6b753f761943 | 189 | { |
JimCarver | 0:6b753f761943 | 190 | return _error_description; |
JimCarver | 0:6b753f761943 | 191 | } |
JimCarver | 0:6b753f761943 | 192 | |
JimCarver | 0:6b753f761943 | 193 | |
JimCarver | 0:6b753f761943 | 194 | void MbedCloudClient::register_update_callback(string route, |
JimCarver | 0:6b753f761943 | 195 | SimpleM2MResourceBase* resource) |
JimCarver | 0:6b753f761943 | 196 | { |
JimCarver | 0:6b753f761943 | 197 | _update_values[route] = resource; |
JimCarver | 0:6b753f761943 | 198 | } |
JimCarver | 0:6b753f761943 | 199 | |
JimCarver | 0:6b753f761943 | 200 | void MbedCloudClient::complete(ServiceClientCallbackStatus status) |
JimCarver | 0:6b753f761943 | 201 | { |
JimCarver | 0:6b753f761943 | 202 | tr_info("MbedCloudClient::complete status (%d)", status); |
JimCarver | 0:6b753f761943 | 203 | if (status == Service_Client_Status_Registered) { |
JimCarver | 0:6b753f761943 | 204 | _on_registered.call(); |
JimCarver | 0:6b753f761943 | 205 | } else if (status == Service_Client_Status_Unregistered) { |
JimCarver | 0:6b753f761943 | 206 | _object_list.clear(); |
JimCarver | 0:6b753f761943 | 207 | _on_unregistered.call(); |
JimCarver | 0:6b753f761943 | 208 | } else if (status == Service_Client_Status_Register_Updated) { |
JimCarver | 0:6b753f761943 | 209 | _on_registration_updated.call(); |
JimCarver | 0:6b753f761943 | 210 | } |
JimCarver | 0:6b753f761943 | 211 | } |
JimCarver | 0:6b753f761943 | 212 | |
JimCarver | 0:6b753f761943 | 213 | void MbedCloudClient::error(int error, const char *reason) |
JimCarver | 0:6b753f761943 | 214 | { |
JimCarver | 0:6b753f761943 | 215 | tr_error("MbedCloudClient::error code (%d)", error); |
JimCarver | 0:6b753f761943 | 216 | _error_description = reason; |
JimCarver | 0:6b753f761943 | 217 | _on_error(error); |
JimCarver | 0:6b753f761943 | 218 | } |
JimCarver | 0:6b753f761943 | 219 | |
JimCarver | 0:6b753f761943 | 220 | void MbedCloudClient::value_updated(M2MBase *base, M2MBase::BaseType type) |
JimCarver | 0:6b753f761943 | 221 | { |
JimCarver | 0:6b753f761943 | 222 | if (base) { |
JimCarver | 0:6b753f761943 | 223 | tr_info("MbedCloudClient::value_updated path %s", base->uri_path()); |
JimCarver | 0:6b753f761943 | 224 | if (base->uri_path()) { |
JimCarver | 0:6b753f761943 | 225 | if (_update_values.count(base->uri_path()) != 0) { |
JimCarver | 0:6b753f761943 | 226 | tr_debug("MbedCloudClient::value_updated calling update() for %s", base->uri_path()); |
JimCarver | 0:6b753f761943 | 227 | _update_values[base->uri_path()]->update(); |
JimCarver | 0:6b753f761943 | 228 | } else { |
JimCarver | 0:6b753f761943 | 229 | // way to tell application that there is a value update |
JimCarver | 0:6b753f761943 | 230 | if (_value_callback) { |
JimCarver | 0:6b753f761943 | 231 | _value_callback->value_updated(base, type); |
JimCarver | 0:6b753f761943 | 232 | } |
JimCarver | 0:6b753f761943 | 233 | } |
JimCarver | 0:6b753f761943 | 234 | } |
JimCarver | 0:6b753f761943 | 235 | } |
JimCarver | 0:6b753f761943 | 236 | } |
JimCarver | 0:6b753f761943 | 237 | |
JimCarver | 0:6b753f761943 | 238 | void MbedCloudClient::send_get_request(const char *uri, |
JimCarver | 0:6b753f761943 | 239 | const size_t offset, |
JimCarver | 0:6b753f761943 | 240 | get_data_cb data_cb, |
JimCarver | 0:6b753f761943 | 241 | get_data_error_cb error_cb, |
JimCarver | 0:6b753f761943 | 242 | void *context) |
JimCarver | 0:6b753f761943 | 243 | { |
JimCarver | 0:6b753f761943 | 244 | _client.connector_client().m2m_interface()->get_data_request(uri, |
JimCarver | 0:6b753f761943 | 245 | offset, |
JimCarver | 0:6b753f761943 | 246 | true, |
JimCarver | 0:6b753f761943 | 247 | data_cb, |
JimCarver | 0:6b753f761943 | 248 | error_cb, |
JimCarver | 0:6b753f761943 | 249 | context); |
JimCarver | 0:6b753f761943 | 250 | } |