Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
m2mcallbackstorage.h
00001 /* 00002 * Copyright (c) 2017 ARM Limited. All rights reserved. 00003 * SPDX-License-Identifier: Apache-2.0 00004 * Licensed under the Apache License, Version 2.0 (the License); you may 00005 * not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an AS IS BASIS, WITHOUT 00012 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 #ifndef __M2M_CALLBACK_STORAGE_H__ 00017 #define __M2M_CALLBACK_STORAGE_H__ 00018 00019 #include "mbed-client/m2mvector.h" 00020 00021 class M2MBase; 00022 class M2MCallbackAssociation; 00023 class M2MCallbackStorage; 00024 00025 typedef m2m::Vector<M2MCallbackAssociation> M2MCallbackAssociationList; 00026 00027 // XXX: this should not be visible for client code 00028 class M2MCallbackAssociation 00029 { 00030 public: 00031 00032 // Types of callbacks stored as "void*" in the _callback variable. 00033 // Note: the FPn<> -types are actually stored as pointers to object instances, 00034 // which needs to be handled externally when fetching and deleting the callbacks. 00035 enum M2MCallbackType { 00036 // typedef FP1<void, const char*> value_updated_callback; 00037 M2MBaseValueUpdatedCallback, 00038 00039 // typedef void(*value_updated_callback2) (const char* object_name); 00040 M2MBaseValueUpdatedCallback2, 00041 00042 // typedef FP1<void,void*> execute_callback; 00043 M2MResourceInstanceExecuteCallback, 00044 00045 //typedef void(*execute_callback_2) (void *arguments); 00046 M2MResourceInstanceExecuteCallback2, 00047 00048 // typedef FP1<void, M2MBlockMessage *> incoming_block_message_callback; 00049 M2MResourceInstanceIncomingBlockMessageCallback, 00050 00051 // typedef FP3<void, const String &, uint8_t *&, uint32_t &> outgoing_block_message_callback; 00052 M2MResourceInstanceOutgoingBlockMessageCallback, 00053 00054 // class M2MResourceCallback 00055 M2MResourceInstanceM2MResourceCallback, 00056 00057 // typedef FP0<void> notification_sent_callback; 00058 M2MResourceInstanceNotificationSentCallback, 00059 00060 // typedef void(*notification_sent_callback_2) (void); 00061 M2MResourceInstanceNotificationSentCallback2, 00062 00063 // typedef FP2<void, const uint16_t, const M2MResourceBase::NotificationStatus> notification_status_callback; 00064 M2MResourceInstanceNotificationStatusCallback, 00065 00066 // typedef void(*notification_status_callback_2) (const uint16_t msg_id, const M2MResourceBase::NotificationStatus status); 00067 M2MResourceInstanceNotificationStatusCallback2, 00068 00069 // typedef void(*notification_delivery_status_cb) (const M2MBase& base, 00070 // const M2MBase::NotificationDeliveryStatus status, void *client_args); 00071 M2MBaseNotificationDeliveryStatusCallback, 00072 00073 //typedef void(*message_delivery_status_cb) (const M2MBase& base, 00074 // const MessageDeliveryStatus status, 00075 // const MessageType type, 00076 // void *client_args); 00077 M2MBaseMessageDeliveryStatusCallback, 00078 00079 // typedef void(*value_set_callback) (const M2MResourceBase *resource, 00080 // uint8_t *value, const uint32_t value_length); 00081 M2MResourceBaseValueSetCallback, 00082 00083 // typedef size_t(*read_resource_value_callback) (const M2MResourceBase& resource, 00084 // void *buffer, void *client_args); 00085 M2MResourceBaseValueReadCallback, 00086 00087 // typedef bool(*write_resource_value_callback) (const M2MResourceBase& resource, 00088 // const uint8_t *buffer, const size_t buffer_size, void *client_args); 00089 M2MResourceBaseValueWriteCallback 00090 00091 #ifdef ENABLE_ASYNC_REST_RESPONSE 00092 // typedef bool(*handle_async_coap_request_cb) (const M2MBase& base, 00093 // M2MBase::Operation operation, const uint8_t *token, const uint8_t token_len, 00094 // const uint8_t *buffer, size_t buffer_size, void *client_args); 00095 ,M2MBaseAsyncCoapRequestCallback 00096 #endif // ENABLE_ASYNC_REST_RESPONSE 00097 }; 00098 00099 /** 00100 * Dummy constructor which does not initialize any predictable value to members, 00101 * needed for array instantiation. Please use the parameterized constructor for real work. 00102 */ 00103 M2MCallbackAssociation(); 00104 00105 M2MCallbackAssociation(const M2MBase *object, void *callback, M2MCallbackType type, void *client_args); 00106 00107 public: 00108 /** Object, where the callback is associated to. This is used as key on searches, must not be null. */ 00109 const M2MBase *_object; 00110 00111 /** 00112 * Callback pointer, actual data type of the is dependent of _type. 00113 * 00114 * We could also use eg. a union with specific pointer types to avoid ugly casts, but that would 00115 * then require a type-specific get_callback_<type>() to avoid casts on caller side too - unless 00116 * the get_callback() would return this object and the caller would access the union._callback_<type> 00117 * variable according to the _type. 00118 */ 00119 void *_callback; 00120 00121 /** Type of the data _callback points to and needed for interpreting the data in it. */ 00122 M2MCallbackType _type; 00123 00124 void *_client_args; 00125 }; 00126 00127 00128 class M2MCallbackStorage 00129 { 00130 public: 00131 00132 ~M2MCallbackStorage(); 00133 00134 // get the shared instance of the storage. 00135 // TODO: tie this' instance to the nsdlinterface or similar class instead, as it would help 00136 // to manage the lifecycle of the object. This would also remove the need for these static methods. 00137 static M2MCallbackStorage *get_instance(); 00138 00139 // utility for deleting the singleton instance, used on unit test and on application exit 00140 static void delete_instance(); 00141 00142 static bool add_callback(const M2MBase &object, 00143 void *callback, 00144 M2MCallbackAssociation::M2MCallbackType type, 00145 void *client_args = 0); 00146 00147 // remove all callbacks attached for given object 00148 static void remove_callbacks(const M2MBase &object); 00149 00150 // remove callback if one exists, return old value of it 00151 static void* remove_callback(const M2MBase &object, M2MCallbackAssociation::M2MCallbackType type); 00152 00153 // XXX: needed for protecting the vector during iteration, but that would add a dependency on PAL here 00154 // void lock(); 00155 // void release(); 00156 00157 // XXX: const_iterator would be nicer 00158 //int get_callback(const M2MBase &object, void &*callback, M2MCallbackType type); 00159 00160 static bool does_callback_exist(const M2MBase &object, M2MCallbackAssociation::M2MCallbackType type); 00161 00162 static void* get_callback(const M2MBase &object, M2MCallbackAssociation::M2MCallbackType type); 00163 00164 static M2MCallbackAssociation* get_association_item(const M2MBase &object, M2MCallbackAssociation::M2MCallbackType type); 00165 // XXX: this is actually not needed unless the client has multiple callbacks per object and type. 00166 inline const M2MCallbackAssociationList& get_callbacks() const; 00167 00168 private: 00169 bool does_callback_exist(const M2MBase &object, void *callback, M2MCallbackAssociation::M2MCallbackType type) const; 00170 void do_remove_callbacks(const M2MBase &object); 00171 void* do_remove_callback(const M2MBase &object, M2MCallbackAssociation::M2MCallbackType type); 00172 bool do_add_callback(const M2MBase &object, void *callback, M2MCallbackAssociation::M2MCallbackType type, void *client_args); 00173 void* do_get_callback(const M2MBase &object, M2MCallbackAssociation::M2MCallbackType type) const; 00174 00175 M2MCallbackAssociation* do_get_association_item(const M2MBase &object, M2MCallbackAssociation::M2MCallbackType type) const; 00176 00177 private: 00178 00179 /** 00180 * Currently there is only one storage object, which is shared between all the interfaces and 00181 * M2M objects. 00182 */ 00183 static M2MCallbackStorage *_static_instance; 00184 00185 /** 00186 * Callback objects stored. There combination of <object>+<type> is used on searches and 00187 * the code does not fully support for adding multiple callbacks for <object>+<type> -pair. 00188 * But that should not be too hard if the feature was added to M2M object API too, it just 00189 * requires the M2M objects to iterate through the callbacks associated to it, not just call 00190 * the get_callback(<object>,<type>) and call the first one. 00191 */ 00192 M2MCallbackAssociationList _callbacks; 00193 }; 00194 00195 inline const M2MCallbackAssociationList& M2MCallbackStorage::get_callbacks() const 00196 { 00197 return _callbacks; 00198 } 00199 00200 #endif // !__M2M_CALLBACK_STORAGE_H__
Generated on Mon Aug 29 2022 19:53:39 by
