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