Mayank Gupta / Mbed OS pelion-example-frdm

Dependencies:   FXAS21002 FXOS8700Q

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers mbed-cloud-client-resource.h Source File

mbed-cloud-client-resource.h

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 #ifndef MBED_CLOUD_CLIENT_RESOURCE_H
00020 #define MBED_CLOUD_CLIENT_RESOURCE_H
00021 
00022 #include "mbed.h"
00023 #include "simple-mbed-cloud-client.h"
00024 #include "mbed-client/m2mstring.h"
00025 
00026 namespace M2MMethod {
00027 
00028 enum M2MMethod {
00029     GET    = 0x01,
00030     PUT    = 0x02,
00031     POST   = 0x04,
00032     DELETE = 0x08
00033 };
00034 
00035 };
00036 
00037 struct mcc_resource_def {
00038     unsigned int object_id;
00039     unsigned int instance_id;
00040     unsigned int resource_id;
00041     String name;
00042     unsigned int method_mask;
00043     String value;
00044     bool observable;
00045     Callback<void(const char*)> *put_callback;
00046     Callback<void(void*)> *post_callback;
00047     Callback<void(const M2MBase&, const NoticationDeliveryStatus)> *notification_callback;
00048 };
00049 
00050 class SimpleMbedCloudClient;
00051 
00052 class MbedCloudClientResource {
00053     public:
00054         /**
00055          * Create a new resource, this function should not be called directly!
00056          *
00057          * Always create a new resource via 'create_resource' on the SimpleMbedCloudClient object.
00058          *
00059          * @param client Instance of SimpleMbedCloudClient
00060          * @param path LwM2M path (in the form of 3200/0/5501)
00061          * @param name Name of the resource (will be shown in the UI)
00062          */
00063         MbedCloudClientResource(SimpleMbedCloudClient *client, const char *path, const char *name);
00064 
00065         /**
00066          * Sets whether the resource can be observed
00067          * When set, Pelion Device Management can subscribe for updates
00068          *
00069          * @param observable
00070          */
00071         void observable(bool observable);
00072 
00073         /**
00074          * Sets the methods that can be applied on this resource
00075          *
00076          * @param methodMask Mask of objects of type M2MMethod, e.g. 'M2MMethod::GET | M2MMethod::POST'
00077          */
00078         void methods(unsigned int methodMask);
00079 
00080         /**
00081          * Set a callback when a PUT action on this resource happens
00082          * Fires whenever someone writes to the resource from Pelion Device Management
00083          *
00084          * @params callback
00085          */
00086         void attach_put_callback(Callback<void(MbedCloudClientResource*, m2m::String)> callback);
00087 
00088         /**
00089          * Set a callback when a POST action on this resource happens
00090          * Fires whenever someone executes the resource from Pelion Device Management
00091          *
00092          * @params callback
00093          */
00094         void attach_post_callback(Callback<void(MbedCloudClientResource*, const uint8_t*, uint16_t)> callback);
00095 
00096         /**
00097          * Set a callback when a POST action on this resource happens
00098          * Fires whenever a notification (e.g. subscribed to resource) was sent
00099          * from Pelion Device Management
00100          *
00101          * @params callback
00102          */
00103         void attach_notification_callback(Callback<void(MbedCloudClientResource*, const NoticationDeliveryStatus)> callback);
00104 
00105         /**
00106          * Clear the PUT callback
00107          */
00108         void detach_put_callback();
00109 
00110         /**
00111          * Clear the POST callback
00112          */
00113         void detach_post_callback();
00114 
00115         /**
00116          * Clear the notification callback
00117          */
00118         void detach_notification_callback();
00119 
00120         /**
00121          * Set the value of the resource to an integer.
00122          * Underneath all values in Pelion Device Management are strings, so this will serialize the value
00123          *
00124          * @param value New value
00125          */
00126         void set_value(int value);
00127 
00128         /**
00129          * Set the value of the resource to a string.
00130          *
00131          * @param value New value
00132          */
00133         void set_value(const char *value);
00134 
00135         /**
00136          * Set the value of the resource to a float.
00137          * Underneath all values in Pelion Device Management are strings, so this will serialize the value
00138          *
00139          * @param value New value
00140          */
00141         void set_value(float value);
00142 
00143         /**
00144          * Get the value of the resource as a string
00145          *
00146          * @returns Current value
00147          */
00148         m2m::String get_value();
00149 
00150         /**
00151          * Get the value of the resource as an integer
00152          * Underneath all values in Pelion Device Management are strings, so this will de-serialize the value
00153          *
00154          * @returns Current value
00155          */
00156         int get_value_int();
00157 
00158         /**
00159          * Get the value of the resource as a float
00160          * Underneath all values in Pelion Device Management are strings, so this will de-serialize the value
00161          *
00162          * @returns Current value
00163          */
00164         float get_value_float();
00165 
00166         /**
00167          * Get a structure with all the data from the resource,
00168          * useful to serialize a resource, or for debugging purposes
00169          *
00170          * @param resourceDef a pointer to a mcc_resource_def structure
00171          */
00172         void get_data(mcc_resource_def *resourceDef);
00173 
00174         /**
00175          * Set the underlying M2MResource
00176          *
00177          * @param res Pointer to an instance of M2MResource
00178          */
00179         void set_m2m_resource(M2MResource *res);
00180 
00181         /**
00182          * Get the underlying M2MResource
00183          *
00184          * @returns Current M2MResource that manages this resource
00185          *          Lifetime will be the same as this object.
00186          */
00187         M2MResource* get_m2m_resource();
00188 
00189         /**
00190          * Convert the delivery status of a notification callback to a string
00191          */
00192         static const char * delivery_status_to_string(const NoticationDeliveryStatus status);
00193 
00194     private:
00195         void internal_post_callback(void* params);
00196         void internal_put_callback(const char* resource);
00197         void internal_notification_callback(const M2MBase& m2mbase, const NoticationDeliveryStatus status);
00198 
00199         SimpleMbedCloudClient *client;
00200         M2MResource *resource;
00201         m2m::String path;
00202         m2m::String name;
00203         m2m::String value;
00204         bool isObservable;
00205         unsigned int methodMask;
00206 
00207         Callback<void(MbedCloudClientResource*, m2m::String)> putCallback;
00208         Callback<void(MbedCloudClientResource*, const uint8_t*, uint16_t)> postCallback;
00209         Callback<void(MbedCloudClientResource*, const NoticationDeliveryStatus)> notificationCallback;
00210         Callback<void(void*)> internalPostCallback;
00211         Callback<void(const char*)> internalPutCallback;
00212         Callback<void(const M2MBase&, const NoticationDeliveryStatus)> internalNotificationCallback;
00213 };
00214 
00215 #endif // MBED_CLOUD_CLIENT_RESOURCE_H