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