High level Bluetooth Low Energy API and radio abstraction layer

Dependencies:   nRF51822

Dependents:   LinkNode_LIS3DH

Fork of BLE_API by Bluetooth Low Energy

Committer:
vcoubard
Date:
Mon Jan 11 08:51:29 2016 +0000
Revision:
1049:d99774db03b4
Parent:
1048:efb29faf12fc
Child:
1052:b55e1ad3e1b3
Synchronized with git rev 4d8829ad
Author: Vincent Coubard
Workaround for mbed os Functionpointer implementation:
mbed os FunctionPointer implementation does not allow, yet, to use a const
member function This kind of workaround is needed to use
FunctionPointerWithContext class in minar

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rgrover1 710:b2e1a2660ec2 1 /* mbed Microcontroller Library
rgrover1 710:b2e1a2660ec2 2 * Copyright (c) 2006-2013 ARM Limited
rgrover1 710:b2e1a2660ec2 3 *
rgrover1 710:b2e1a2660ec2 4 * Licensed under the Apache License, Version 2.0 (the "License");
rgrover1 710:b2e1a2660ec2 5 * you may not use this file except in compliance with the License.
rgrover1 710:b2e1a2660ec2 6 * You may obtain a copy of the License at
rgrover1 710:b2e1a2660ec2 7 *
rgrover1 710:b2e1a2660ec2 8 * http://www.apache.org/licenses/LICENSE-2.0
rgrover1 710:b2e1a2660ec2 9 *
rgrover1 710:b2e1a2660ec2 10 * Unless required by applicable law or agreed to in writing, software
rgrover1 710:b2e1a2660ec2 11 * distributed under the License is distributed on an "AS IS" BASIS,
rgrover1 710:b2e1a2660ec2 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
rgrover1 710:b2e1a2660ec2 13 * See the License for the specific language governing permissions and
rgrover1 710:b2e1a2660ec2 14 * limitations under the License.
rgrover1 710:b2e1a2660ec2 15 */
rgrover1 710:b2e1a2660ec2 16
rgrover1 710:b2e1a2660ec2 17 #ifndef MBED_FUNCTIONPOINTER_WITH_CONTEXT_H
rgrover1 710:b2e1a2660ec2 18 #define MBED_FUNCTIONPOINTER_WITH_CONTEXT_H
rgrover1 710:b2e1a2660ec2 19
rgrover1 710:b2e1a2660ec2 20 #include <string.h>
rgrover1 710:b2e1a2660ec2 21
rgrover1 710:b2e1a2660ec2 22 /** A class for storing and calling a pointer to a static or member void function
vcoubard 1048:efb29faf12fc 23 * that takes a context.
rgrover1 710:b2e1a2660ec2 24 */
rgrover1 710:b2e1a2660ec2 25 template <typename ContextType>
vcoubard 1042:21a86ac7f5b1 26 class FunctionPointerWithContext {
rgrover1 710:b2e1a2660ec2 27 public:
rgrover1 710:b2e1a2660ec2 28 typedef FunctionPointerWithContext<ContextType> *pFunctionPointerWithContext_t;
vcoubard 1045:b9d15970040f 29 typedef const FunctionPointerWithContext<ContextType> *cpFunctionPointerWithContext_t;
rgrover1 710:b2e1a2660ec2 30 typedef void (*pvoidfcontext_t)(ContextType context);
rgrover1 710:b2e1a2660ec2 31
vcoubard 1048:efb29faf12fc 32 /** Create a FunctionPointerWithContext, attaching a static function.
rgrover1 710:b2e1a2660ec2 33 *
vcoubard 1048:efb29faf12fc 34 * @param function The void static function to attach (default is none).
rgrover1 710:b2e1a2660ec2 35 */
rgrover1 710:b2e1a2660ec2 36 FunctionPointerWithContext(void (*function)(ContextType context) = NULL) :
vcoubard 1046:87a2ebe45470 37 _memberFunctionAndPointer(), _caller(NULL), _next(NULL) {
rgrover1 710:b2e1a2660ec2 38 attach(function);
rgrover1 710:b2e1a2660ec2 39 }
rgrover1 710:b2e1a2660ec2 40
vcoubard 1048:efb29faf12fc 41 /** Create a FunctionPointerWithContext, attaching a member function.
rgrover1 710:b2e1a2660ec2 42 *
vcoubard 1048:efb29faf12fc 43 * @param object The object pointer to invoke the member function on (the "this" pointer).
vcoubard 1048:efb29faf12fc 44 * @param function The address of the void member function to attach.
rgrover1 710:b2e1a2660ec2 45 */
rgrover1 710:b2e1a2660ec2 46 template<typename T>
rgrover1 710:b2e1a2660ec2 47 FunctionPointerWithContext(T *object, void (T::*member)(ContextType context)) :
rgrover1 897:838e1375dbaa 48 _memberFunctionAndPointer(), _caller(NULL), _next(NULL) {
rgrover1 710:b2e1a2660ec2 49 attach(object, member);
rgrover1 710:b2e1a2660ec2 50 }
rgrover1 710:b2e1a2660ec2 51
vcoubard 1046:87a2ebe45470 52 FunctionPointerWithContext(const FunctionPointerWithContext& that) :
vcoubard 1046:87a2ebe45470 53 _memberFunctionAndPointer(that._memberFunctionAndPointer), _caller(that._caller), _next(NULL) {
vcoubard 1046:87a2ebe45470 54 }
vcoubard 1046:87a2ebe45470 55
vcoubard 1046:87a2ebe45470 56 FunctionPointerWithContext& operator=(const FunctionPointerWithContext& that) {
vcoubard 1046:87a2ebe45470 57 _memberFunctionAndPointer = that._memberFunctionAndPointer;
vcoubard 1046:87a2ebe45470 58 _caller = that._caller;
vcoubard 1046:87a2ebe45470 59 _next = NULL;
vcoubard 1046:87a2ebe45470 60 return *this;
vcoubard 1046:87a2ebe45470 61 }
vcoubard 1046:87a2ebe45470 62
vcoubard 1048:efb29faf12fc 63 /** Attach a static function.
rgrover1 710:b2e1a2660ec2 64 *
vcoubard 1048:efb29faf12fc 65 * @param function The void static function to attach (default is none).
rgrover1 710:b2e1a2660ec2 66 */
rgrover1 710:b2e1a2660ec2 67 void attach(void (*function)(ContextType context) = NULL) {
rgrover1 710:b2e1a2660ec2 68 _function = function;
rgrover1 897:838e1375dbaa 69 _caller = functioncaller;
rgrover1 710:b2e1a2660ec2 70 }
rgrover1 710:b2e1a2660ec2 71
vcoubard 1048:efb29faf12fc 72 /** Attach a member function.
rgrover1 710:b2e1a2660ec2 73 *
vcoubard 1048:efb29faf12fc 74 * @param object The object pointer to invoke the member function on (the "this" pointer).
vcoubard 1048:efb29faf12fc 75 * @param function The address of the void member function to attach.
rgrover1 710:b2e1a2660ec2 76 */
rgrover1 710:b2e1a2660ec2 77 template<typename T>
rgrover1 710:b2e1a2660ec2 78 void attach(T *object, void (T::*member)(ContextType context)) {
rgrover1 897:838e1375dbaa 79 _memberFunctionAndPointer._object = static_cast<void *>(object);
rgrover1 897:838e1375dbaa 80 memcpy(_memberFunctionAndPointer._memberFunction, (char*) &member, sizeof(member));
rgrover1 897:838e1375dbaa 81 _caller = &FunctionPointerWithContext::membercaller<T>;
rgrover1 710:b2e1a2660ec2 82 }
rgrover1 710:b2e1a2660ec2 83
vcoubard 1048:efb29faf12fc 84 /** Call the attached static or member function; if there are chained
rgrover1 710:b2e1a2660ec2 85 * FunctionPointers their callbacks are invoked as well.
vcoubard 1048:efb29faf12fc 86 * @Note: All chained callbacks stack up, so hopefully there won't be too
rgrover1 710:b2e1a2660ec2 87 * many FunctionPointers in a chain. */
vcoubard 1045:b9d15970040f 88 void call(ContextType context) const {
rgrover1 952:8a6c287de1be 89 _caller(this, context);
vcoubard 1042:21a86ac7f5b1 90
vcoubard 1042:21a86ac7f5b1 91 /* Propagate the call to next in the chain. */
vcoubard 1042:21a86ac7f5b1 92 if (_next) {
vcoubard 1042:21a86ac7f5b1 93 _next->call(context);
vcoubard 1042:21a86ac7f5b1 94 }
rgrover1 990:53ac0ac3aa39 95 }
rgrover1 990:53ac0ac3aa39 96
vcoubard 1049:d99774db03b4 97 /** Same as above, workaround for mbed os FunctionPointer implementation. */
vcoubard 1049:d99774db03b4 98 void call(ContextType context) {
vcoubard 1049:d99774db03b4 99 _caller(this, context);
vcoubard 1049:d99774db03b4 100
vcoubard 1049:d99774db03b4 101 /* Propagate the call to next in the chain. */
vcoubard 1049:d99774db03b4 102 if (_next) {
vcoubard 1049:d99774db03b4 103 _next->call(context);
vcoubard 1049:d99774db03b4 104 }
vcoubard 1049:d99774db03b4 105 }
vcoubard 1049:d99774db03b4 106
rgrover1 990:53ac0ac3aa39 107 /**
vcoubard 1048:efb29faf12fc 108 * Set up an external FunctionPointer as a next in the chain of related
rgrover1 710:b2e1a2660ec2 109 * callbacks. Invoking call() on the head FunctionPointer will invoke all
rgrover1 710:b2e1a2660ec2 110 * chained callbacks.
rgrover1 710:b2e1a2660ec2 111 *
rgrover1 710:b2e1a2660ec2 112 * Refer to 'CallChain' as an alternative.
rgrover1 710:b2e1a2660ec2 113 */
rgrover1 710:b2e1a2660ec2 114 void chainAsNext(pFunctionPointerWithContext_t next) {
rgrover1 710:b2e1a2660ec2 115 _next = next;
rgrover1 710:b2e1a2660ec2 116 }
rgrover1 710:b2e1a2660ec2 117
rgrover1 710:b2e1a2660ec2 118 pFunctionPointerWithContext_t getNext(void) const {
rgrover1 710:b2e1a2660ec2 119 return _next;
rgrover1 710:b2e1a2660ec2 120 }
rgrover1 710:b2e1a2660ec2 121
rgrover1 710:b2e1a2660ec2 122 pvoidfcontext_t get_function() const {
rgrover1 710:b2e1a2660ec2 123 return (pvoidfcontext_t)_function;
rgrover1 710:b2e1a2660ec2 124 }
rgrover1 710:b2e1a2660ec2 125
vcoubard 1045:b9d15970040f 126 friend bool operator==(const FunctionPointerWithContext& lhs, const FunctionPointerWithContext& rhs) {
vcoubard 1045:b9d15970040f 127 return rhs._caller == lhs._caller &&
vcoubard 1045:b9d15970040f 128 memcmp(
vcoubard 1045:b9d15970040f 129 &rhs._memberFunctionAndPointer,
vcoubard 1045:b9d15970040f 130 &lhs._memberFunctionAndPointer,
vcoubard 1045:b9d15970040f 131 sizeof(rhs._memberFunctionAndPointer)
vcoubard 1045:b9d15970040f 132 ) == 0;
vcoubard 1045:b9d15970040f 133 }
vcoubard 1045:b9d15970040f 134
rgrover1 710:b2e1a2660ec2 135 private:
rgrover1 710:b2e1a2660ec2 136 template<typename T>
vcoubard 1045:b9d15970040f 137 static void membercaller(cpFunctionPointerWithContext_t self, ContextType context) {
rgrover1 897:838e1375dbaa 138 if (self->_memberFunctionAndPointer._object) {
rgrover1 897:838e1375dbaa 139 T *o = static_cast<T *>(self->_memberFunctionAndPointer._object);
rgrover1 897:838e1375dbaa 140 void (T::*m)(ContextType);
rgrover1 897:838e1375dbaa 141 memcpy((char*) &m, self->_memberFunctionAndPointer._memberFunction, sizeof(m));
rgrover1 897:838e1375dbaa 142 (o->*m)(context);
rgrover1 897:838e1375dbaa 143 }
rgrover1 897:838e1375dbaa 144 }
rgrover1 897:838e1375dbaa 145
vcoubard 1045:b9d15970040f 146 static void functioncaller(cpFunctionPointerWithContext_t self, ContextType context) {
rgrover1 897:838e1375dbaa 147 if (self->_function) {
rgrover1 897:838e1375dbaa 148 self->_function(context);
rgrover1 897:838e1375dbaa 149 }
rgrover1 710:b2e1a2660ec2 150 }
rgrover1 710:b2e1a2660ec2 151
rgrover1 897:838e1375dbaa 152 struct MemberFunctionAndPtr {
rgrover1 897:838e1375dbaa 153 /*
vcoubard 1048:efb29faf12fc 154 * Forward declaration of a class and a member function to this class.
rgrover1 897:838e1375dbaa 155 * Because the compiler doesn't know anything about the forwarded member
rgrover1 897:838e1375dbaa 156 * function, it will always use the biggest size and the biggest alignment
rgrover1 897:838e1375dbaa 157 * that a member function can take for objects of type UndefinedMemberFunction.
rgrover1 897:838e1375dbaa 158 */
rgrover1 897:838e1375dbaa 159 class UndefinedClass;
rgrover1 897:838e1375dbaa 160 typedef void (UndefinedClass::*UndefinedMemberFunction)(ContextType);
rgrover1 897:838e1375dbaa 161
rgrover1 897:838e1375dbaa 162 void* _object;
rgrover1 897:838e1375dbaa 163 union {
rgrover1 897:838e1375dbaa 164 char _memberFunction[sizeof(UndefinedMemberFunction)];
rgrover1 897:838e1375dbaa 165 UndefinedMemberFunction _alignment;
rgrover1 897:838e1375dbaa 166 };
rgrover1 897:838e1375dbaa 167 };
rgrover1 897:838e1375dbaa 168
rgrover1 897:838e1375dbaa 169 union {
vcoubard 1048:efb29faf12fc 170 pvoidfcontext_t _function; /**< Static function pointer - NULL if none attached */
rgrover1 897:838e1375dbaa 171 /**
rgrover1 897:838e1375dbaa 172 * object this pointer and pointer to member -
rgrover1 897:838e1375dbaa 173 * _memberFunctionAndPointer._object will be NULL if none attached
rgrover1 897:838e1375dbaa 174 */
vcoubard 1045:b9d15970040f 175 mutable MemberFunctionAndPtr _memberFunctionAndPointer;
rgrover1 897:838e1375dbaa 176 };
rgrover1 897:838e1375dbaa 177
vcoubard 1045:b9d15970040f 178 void (*_caller)(const FunctionPointerWithContext*, ContextType);
rgrover1 897:838e1375dbaa 179
vcoubard 1048:efb29faf12fc 180 pFunctionPointerWithContext_t _next; /**< Optional link to make a chain out of functionPointers. This
rgrover1 710:b2e1a2660ec2 181 * allows chaining function pointers without requiring
vcoubard 1048:efb29faf12fc 182 * external memory to manage the chain. Refer to
rgrover1 710:b2e1a2660ec2 183 * 'CallChain' as an alternative. */
rgrover1 710:b2e1a2660ec2 184 };
rgrover1 710:b2e1a2660ec2 185
rgrover1 993:4d62b7967c11 186 /**
rgrover1 993:4d62b7967c11 187 * @brief Create a new FunctionPointerWithContext which bind an instance and a
rgrover1 993:4d62b7967c11 188 * a member function together.
rgrover1 993:4d62b7967c11 189 * @details This little helper is a just here to eliminate the need to write the
rgrover1 993:4d62b7967c11 190 * FunctionPointerWithContext type each time you want to create one by kicking
rgrover1 993:4d62b7967c11 191 * automatic type deduction of function templates. With this function, it is easy
rgrover1 993:4d62b7967c11 192 * to write only one entry point for functions which expect a FunctionPointer
rgrover1 993:4d62b7967c11 193 * in parameters.
rgrover1 993:4d62b7967c11 194 *
rgrover1 993:4d62b7967c11 195 * @param object to bound with member function
rgrover1 993:4d62b7967c11 196 * @param member The member function called
rgrover1 993:4d62b7967c11 197 * @return a new FunctionPointerWithContext
rgrover1 993:4d62b7967c11 198 */
rgrover1 993:4d62b7967c11 199 template<typename T, typename ContextType>
rgrover1 993:4d62b7967c11 200 FunctionPointerWithContext<ContextType> makeFunctionPointer(T *object, void (T::*member)(ContextType context))
rgrover1 993:4d62b7967c11 201 {
rgrover1 993:4d62b7967c11 202 return FunctionPointerWithContext<ContextType>(object, member);
rgrover1 993:4d62b7967c11 203 }
rgrover1 993:4d62b7967c11 204
rgrover1 710:b2e1a2660ec2 205 #endif // ifndef MBED_FUNCTIONPOINTER_WITH_CONTEXT_H