Toyomasa Watarai / simple-mbed-cloud-client

Dependents:  

Committer:
MACRUM
Date:
Mon Jul 02 06:30:39 2018 +0000
Revision:
0:276e7a263c35
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MACRUM 0:276e7a263c35 1 /*
MACRUM 0:276e7a263c35 2 * Copyright (c) 2017 ARM Limited. All rights reserved.
MACRUM 0:276e7a263c35 3 * SPDX-License-Identifier: Apache-2.0
MACRUM 0:276e7a263c35 4 * Licensed under the Apache License, Version 2.0 (the License); you may
MACRUM 0:276e7a263c35 5 * not use this file except in compliance with the License.
MACRUM 0:276e7a263c35 6 * You may obtain a copy of the License at
MACRUM 0:276e7a263c35 7 *
MACRUM 0:276e7a263c35 8 * http://www.apache.org/licenses/LICENSE-2.0
MACRUM 0:276e7a263c35 9 *
MACRUM 0:276e7a263c35 10 * Unless required by applicable law or agreed to in writing, software
MACRUM 0:276e7a263c35 11 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
MACRUM 0:276e7a263c35 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
MACRUM 0:276e7a263c35 13 * See the License for the specific language governing permissions and
MACRUM 0:276e7a263c35 14 * limitations under the License.
MACRUM 0:276e7a263c35 15 */
MACRUM 0:276e7a263c35 16 #ifndef __M2M_CALLBACK_STORAGE_H__
MACRUM 0:276e7a263c35 17 #define __M2M_CALLBACK_STORAGE_H__
MACRUM 0:276e7a263c35 18
MACRUM 0:276e7a263c35 19 #include "mbed-client/m2mvector.h"
MACRUM 0:276e7a263c35 20
MACRUM 0:276e7a263c35 21 class M2MBase;
MACRUM 0:276e7a263c35 22 class M2MCallbackAssociation;
MACRUM 0:276e7a263c35 23 class M2MCallbackStorage;
MACRUM 0:276e7a263c35 24
MACRUM 0:276e7a263c35 25 typedef m2m::Vector<M2MCallbackAssociation> M2MCallbackAssociationList;
MACRUM 0:276e7a263c35 26
MACRUM 0:276e7a263c35 27 // XXX: this should not be visible for client code
MACRUM 0:276e7a263c35 28 class M2MCallbackAssociation
MACRUM 0:276e7a263c35 29 {
MACRUM 0:276e7a263c35 30 public:
MACRUM 0:276e7a263c35 31
MACRUM 0:276e7a263c35 32 // Types of callbacks stored as "void*" in the _callback variable.
MACRUM 0:276e7a263c35 33 // Note: the FPn<> -types are actually stored as pointers to object instances,
MACRUM 0:276e7a263c35 34 // which needs to be handled externally when fetching and deleting the callbacks.
MACRUM 0:276e7a263c35 35 enum M2MCallbackType {
MACRUM 0:276e7a263c35 36 // typedef FP1<void, const char*> value_updated_callback;
MACRUM 0:276e7a263c35 37 M2MBaseValueUpdatedCallback,
MACRUM 0:276e7a263c35 38
MACRUM 0:276e7a263c35 39 // typedef void(*value_updated_callback2) (const char* object_name);
MACRUM 0:276e7a263c35 40 M2MBaseValueUpdatedCallback2,
MACRUM 0:276e7a263c35 41
MACRUM 0:276e7a263c35 42 // typedef FP1<void,void*> execute_callback;
MACRUM 0:276e7a263c35 43 M2MResourceInstanceExecuteCallback,
MACRUM 0:276e7a263c35 44
MACRUM 0:276e7a263c35 45 //typedef void(*execute_callback_2) (void *arguments);
MACRUM 0:276e7a263c35 46 M2MResourceInstanceExecuteCallback2,
MACRUM 0:276e7a263c35 47
MACRUM 0:276e7a263c35 48 // typedef FP1<void, M2MBlockMessage *> incoming_block_message_callback;
MACRUM 0:276e7a263c35 49 M2MResourceInstanceIncomingBlockMessageCallback,
MACRUM 0:276e7a263c35 50
MACRUM 0:276e7a263c35 51 // typedef FP3<void, const String &, uint8_t *&, uint32_t &> outgoing_block_message_callback;
MACRUM 0:276e7a263c35 52 M2MResourceInstanceOutgoingBlockMessageCallback,
MACRUM 0:276e7a263c35 53
MACRUM 0:276e7a263c35 54 // class M2MResourceCallback
MACRUM 0:276e7a263c35 55 M2MResourceInstanceM2MResourceCallback,
MACRUM 0:276e7a263c35 56
MACRUM 0:276e7a263c35 57 // typedef FP0<void> notification_sent_callback;
MACRUM 0:276e7a263c35 58 M2MResourceInstanceNotificationSentCallback,
MACRUM 0:276e7a263c35 59
MACRUM 0:276e7a263c35 60 // typedef void(*notification_sent_callback_2) (void);
MACRUM 0:276e7a263c35 61 M2MResourceInstanceNotificationSentCallback2,
MACRUM 0:276e7a263c35 62
MACRUM 0:276e7a263c35 63 // typedef FP2<void, const uint16_t, const M2MResourceBase::NotificationStatus> notification_status_callback;
MACRUM 0:276e7a263c35 64 M2MResourceInstanceNotificationStatusCallback,
MACRUM 0:276e7a263c35 65
MACRUM 0:276e7a263c35 66 // typedef void(*notification_status_callback_2) (const uint16_t msg_id, const M2MResourceBase::NotificationStatus status);
MACRUM 0:276e7a263c35 67 M2MResourceInstanceNotificationStatusCallback2,
MACRUM 0:276e7a263c35 68
MACRUM 0:276e7a263c35 69 // typedef void(*notification_delivery_status_cb) (const M2MBase& base,
MACRUM 0:276e7a263c35 70 // const M2MBase::NotificationDeliveryStatus status, void *client_args);
MACRUM 0:276e7a263c35 71 M2MBaseNotificationDeliveryStatusCallback
MACRUM 0:276e7a263c35 72
MACRUM 0:276e7a263c35 73 };
MACRUM 0:276e7a263c35 74
MACRUM 0:276e7a263c35 75 /**
MACRUM 0:276e7a263c35 76 * Dummy constructor which does not initialize any predictable value to members,
MACRUM 0:276e7a263c35 77 * needed for array instantiation. Please use the parameterized constructor for real work.
MACRUM 0:276e7a263c35 78 */
MACRUM 0:276e7a263c35 79 M2MCallbackAssociation();
MACRUM 0:276e7a263c35 80
MACRUM 0:276e7a263c35 81 M2MCallbackAssociation(const M2MBase *object, void *callback, M2MCallbackType type, void *client_args);
MACRUM 0:276e7a263c35 82
MACRUM 0:276e7a263c35 83 public:
MACRUM 0:276e7a263c35 84 /** Object, where the callback is associated to. This is used as key on searches, must not be null. */
MACRUM 0:276e7a263c35 85 const M2MBase *_object;
MACRUM 0:276e7a263c35 86
MACRUM 0:276e7a263c35 87 /**
MACRUM 0:276e7a263c35 88 * Callback pointer, actual data type of the is dependent of _type.
MACRUM 0:276e7a263c35 89 *
MACRUM 0:276e7a263c35 90 * We could also use eg. a union with specific pointer types to avoid ugly casts, but that would
MACRUM 0:276e7a263c35 91 * then require a type-specific get_callback_<type>() to avoid casts on caller side too - unless
MACRUM 0:276e7a263c35 92 * the get_callback() would return this object and the caller would access the union._callback_<type>
MACRUM 0:276e7a263c35 93 * variable according to the _type.
MACRUM 0:276e7a263c35 94 */
MACRUM 0:276e7a263c35 95 void *_callback;
MACRUM 0:276e7a263c35 96
MACRUM 0:276e7a263c35 97 /** Type of the data _callback points to and needed for interpreting the data in it. */
MACRUM 0:276e7a263c35 98 M2MCallbackType _type;
MACRUM 0:276e7a263c35 99
MACRUM 0:276e7a263c35 100 void *_client_args;
MACRUM 0:276e7a263c35 101 };
MACRUM 0:276e7a263c35 102
MACRUM 0:276e7a263c35 103
MACRUM 0:276e7a263c35 104 class M2MCallbackStorage
MACRUM 0:276e7a263c35 105 {
MACRUM 0:276e7a263c35 106 public:
MACRUM 0:276e7a263c35 107
MACRUM 0:276e7a263c35 108 ~M2MCallbackStorage();
MACRUM 0:276e7a263c35 109
MACRUM 0:276e7a263c35 110 // get the shared instance of the storage.
MACRUM 0:276e7a263c35 111 // TODO: tie this' instance to the nsdlinterface or similar class instead, as it would help
MACRUM 0:276e7a263c35 112 // to manage the lifecycle of the object. This would also remove the need for these static methods.
MACRUM 0:276e7a263c35 113 static M2MCallbackStorage *get_instance();
MACRUM 0:276e7a263c35 114
MACRUM 0:276e7a263c35 115 // utility for deleting the singleton instance, used on unit test and on application exit
MACRUM 0:276e7a263c35 116 static void delete_instance();
MACRUM 0:276e7a263c35 117
MACRUM 0:276e7a263c35 118 static bool add_callback(const M2MBase &object,
MACRUM 0:276e7a263c35 119 void *callback,
MACRUM 0:276e7a263c35 120 M2MCallbackAssociation::M2MCallbackType type,
MACRUM 0:276e7a263c35 121 void *client_args = 0);
MACRUM 0:276e7a263c35 122
MACRUM 0:276e7a263c35 123 // remove all callbacks attached for given object
MACRUM 0:276e7a263c35 124 static void remove_callbacks(const M2MBase &object);
MACRUM 0:276e7a263c35 125
MACRUM 0:276e7a263c35 126 // remove callback if one exists, return old value of it
MACRUM 0:276e7a263c35 127 static void* remove_callback(const M2MBase &object, M2MCallbackAssociation::M2MCallbackType type);
MACRUM 0:276e7a263c35 128
MACRUM 0:276e7a263c35 129 // XXX: needed for protecting the vector during iteration, but that would add a dependency on PAL here
MACRUM 0:276e7a263c35 130 // void lock();
MACRUM 0:276e7a263c35 131 // void release();
MACRUM 0:276e7a263c35 132
MACRUM 0:276e7a263c35 133 // XXX: const_iterator would be nicer
MACRUM 0:276e7a263c35 134 //int get_callback(const M2MBase &object, void &*callback, M2MCallbackType type);
MACRUM 0:276e7a263c35 135
MACRUM 0:276e7a263c35 136 static bool does_callback_exist(const M2MBase &object, M2MCallbackAssociation::M2MCallbackType type);
MACRUM 0:276e7a263c35 137
MACRUM 0:276e7a263c35 138 static void* get_callback(const M2MBase &object, M2MCallbackAssociation::M2MCallbackType type);
MACRUM 0:276e7a263c35 139
MACRUM 0:276e7a263c35 140 static M2MCallbackAssociation* get_association_item(const M2MBase &object, M2MCallbackAssociation::M2MCallbackType type);
MACRUM 0:276e7a263c35 141 // XXX: this is actually not needed unless the client has multiple callbacks per object and type.
MACRUM 0:276e7a263c35 142 inline const M2MCallbackAssociationList& get_callbacks() const;
MACRUM 0:276e7a263c35 143
MACRUM 0:276e7a263c35 144 private:
MACRUM 0:276e7a263c35 145 bool does_callback_exist(const M2MBase &object, void *callback, M2MCallbackAssociation::M2MCallbackType type) const;
MACRUM 0:276e7a263c35 146 void do_remove_callbacks(const M2MBase &object);
MACRUM 0:276e7a263c35 147 void* do_remove_callback(const M2MBase &object, M2MCallbackAssociation::M2MCallbackType type);
MACRUM 0:276e7a263c35 148 bool do_add_callback(const M2MBase &object, void *callback, M2MCallbackAssociation::M2MCallbackType type, void *client_args);
MACRUM 0:276e7a263c35 149 void* do_get_callback(const M2MBase &object, M2MCallbackAssociation::M2MCallbackType type) const;
MACRUM 0:276e7a263c35 150
MACRUM 0:276e7a263c35 151 M2MCallbackAssociation* do_get_association_item(const M2MBase &object, M2MCallbackAssociation::M2MCallbackType type) const;
MACRUM 0:276e7a263c35 152
MACRUM 0:276e7a263c35 153 private:
MACRUM 0:276e7a263c35 154
MACRUM 0:276e7a263c35 155 /**
MACRUM 0:276e7a263c35 156 * Currently there is only one storage object, which is shared between all the interfaces and
MACRUM 0:276e7a263c35 157 * M2M objects.
MACRUM 0:276e7a263c35 158 */
MACRUM 0:276e7a263c35 159 static M2MCallbackStorage *_static_instance;
MACRUM 0:276e7a263c35 160
MACRUM 0:276e7a263c35 161 /**
MACRUM 0:276e7a263c35 162 * Callback objects stored. There combination of <object>+<type> is used on searches and
MACRUM 0:276e7a263c35 163 * the code does not fully support for adding multiple callbacks for <object>+<type> -pair.
MACRUM 0:276e7a263c35 164 * But that should not be too hard if the feature was added to M2M object API too, it just
MACRUM 0:276e7a263c35 165 * requires the M2M objects to iterate through the callbacks associated to it, not just call
MACRUM 0:276e7a263c35 166 * the get_callback(<object>,<type>) and call the first one.
MACRUM 0:276e7a263c35 167 */
MACRUM 0:276e7a263c35 168 M2MCallbackAssociationList _callbacks;
MACRUM 0:276e7a263c35 169 };
MACRUM 0:276e7a263c35 170
MACRUM 0:276e7a263c35 171 inline const M2MCallbackAssociationList& M2MCallbackStorage::get_callbacks() const
MACRUM 0:276e7a263c35 172 {
MACRUM 0:276e7a263c35 173 return _callbacks;
MACRUM 0:276e7a263c35 174 }
MACRUM 0:276e7a263c35 175
MACRUM 0:276e7a263c35 176 #endif // !__M2M_CALLBACK_STORAGE_H__