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(*value_set_callback) (const M2MResourceBase *resource, 00074 // uint8_t *value, const uint32_t value_length); 00075 M2MResourceBaseValueSetCallback, 00076 00077 // typedef size_t(*read_resource_value_callback) (const M2MResourceBase& resource, 00078 // void *buffer, void *client_args); 00079 M2MResourceBaseValueReadCallback, 00080 00081 // typedef bool(*write_resource_value_callback) (const M2MResourceBase& resource, 00082 // const uint8_t *buffer, const size_t buffer_size, void *client_args); 00083 M2MResourceBaseValueWriteCallback 00084 }; 00085 00086 /** 00087 * Dummy constructor which does not initialize any predictable value to members, 00088 * needed for array instantiation. Please use the parameterized constructor for real work. 00089 */ 00090 M2MCallbackAssociation(); 00091 00092 M2MCallbackAssociation(const M2MBase *object, void *callback, M2MCallbackType type, void *client_args); 00093 00094 public: 00095 /** Object, where the callback is associated to. This is used as key on searches, must not be null. */ 00096 const M2MBase *_object; 00097 00098 /** 00099 * Callback pointer, actual data type of the is dependent of _type. 00100 * 00101 * We could also use eg. a union with specific pointer types to avoid ugly casts, but that would 00102 * then require a type-specific get_callback_<type>() to avoid casts on caller side too - unless 00103 * the get_callback() would return this object and the caller would access the union._callback_<type> 00104 * variable according to the _type. 00105 */ 00106 void *_callback; 00107 00108 /** Type of the data _callback points to and needed for interpreting the data in it. */ 00109 M2MCallbackType _type; 00110 00111 void *_client_args; 00112 }; 00113 00114 00115 class M2MCallbackStorage 00116 { 00117 public: 00118 00119 ~M2MCallbackStorage(); 00120 00121 // get the shared instance of the storage. 00122 // TODO: tie this' instance to the nsdlinterface or similar class instead, as it would help 00123 // to manage the lifecycle of the object. This would also remove the need for these static methods. 00124 static M2MCallbackStorage *get_instance(); 00125 00126 // utility for deleting the singleton instance, used on unit test and on application exit 00127 static void delete_instance(); 00128 00129 static bool add_callback(const M2MBase &object, 00130 void *callback, 00131 M2MCallbackAssociation::M2MCallbackType type, 00132 void *client_args = 0); 00133 00134 // remove all callbacks attached for given object 00135 static void remove_callbacks(const M2MBase &object); 00136 00137 // remove callback if one exists, return old value of it 00138 static void* remove_callback(const M2MBase &object, M2MCallbackAssociation::M2MCallbackType type); 00139 00140 // XXX: needed for protecting the vector during iteration, but that would add a dependency on PAL here 00141 // void lock(); 00142 // void release(); 00143 00144 // XXX: const_iterator would be nicer 00145 //int get_callback(const M2MBase &object, void &*callback, M2MCallbackType type); 00146 00147 static bool does_callback_exist(const M2MBase &object, M2MCallbackAssociation::M2MCallbackType type); 00148 00149 static void* get_callback(const M2MBase &object, M2MCallbackAssociation::M2MCallbackType type); 00150 00151 static M2MCallbackAssociation* get_association_item(const M2MBase &object, M2MCallbackAssociation::M2MCallbackType type); 00152 // XXX: this is actually not needed unless the client has multiple callbacks per object and type. 00153 inline const M2MCallbackAssociationList& get_callbacks() const; 00154 00155 private: 00156 bool does_callback_exist(const M2MBase &object, void *callback, M2MCallbackAssociation::M2MCallbackType type) const; 00157 void do_remove_callbacks(const M2MBase &object); 00158 void* do_remove_callback(const M2MBase &object, M2MCallbackAssociation::M2MCallbackType type); 00159 bool do_add_callback(const M2MBase &object, void *callback, M2MCallbackAssociation::M2MCallbackType type, void *client_args); 00160 void* do_get_callback(const M2MBase &object, M2MCallbackAssociation::M2MCallbackType type) const; 00161 00162 M2MCallbackAssociation* do_get_association_item(const M2MBase &object, M2MCallbackAssociation::M2MCallbackType type) const; 00163 00164 private: 00165 00166 /** 00167 * Currently there is only one storage object, which is shared between all the interfaces and 00168 * M2M objects. 00169 */ 00170 static M2MCallbackStorage *_static_instance; 00171 00172 /** 00173 * Callback objects stored. There combination of <object>+<type> is used on searches and 00174 * the code does not fully support for adding multiple callbacks for <object>+<type> -pair. 00175 * But that should not be too hard if the feature was added to M2M object API too, it just 00176 * requires the M2M objects to iterate through the callbacks associated to it, not just call 00177 * the get_callback(<object>,<type>) and call the first one. 00178 */ 00179 M2MCallbackAssociationList _callbacks; 00180 }; 00181 00182 inline const M2MCallbackAssociationList& M2MCallbackStorage::get_callbacks() const 00183 { 00184 return _callbacks; 00185 } 00186 00187 #endif // !__M2M_CALLBACK_STORAGE_H__
Generated on Tue Jul 12 2022 16:24:13 by
1.7.2