Pfp Cybersecurity (Aka Power Fingerprinting, Inc.) / Mbed OS pfp-emon-nxp

Dependencies:   FXAS21002 FXOS8700Q

Committer:
vithyat
Date:
Wed Aug 28 19:24:56 2019 +0000
Revision:
0:977e87915078
init

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vithyat 0:977e87915078 1 /*
vithyat 0:977e87915078 2 * Copyright (c) 2017 ARM Limited. All rights reserved.
vithyat 0:977e87915078 3 * SPDX-License-Identifier: Apache-2.0
vithyat 0:977e87915078 4 * Licensed under the Apache License, Version 2.0 (the License); you may
vithyat 0:977e87915078 5 * not use this file except in compliance with the License.
vithyat 0:977e87915078 6 * You may obtain a copy of the License at
vithyat 0:977e87915078 7 *
vithyat 0:977e87915078 8 * http://www.apache.org/licenses/LICENSE-2.0
vithyat 0:977e87915078 9 *
vithyat 0:977e87915078 10 * Unless required by applicable law or agreed to in writing, software
vithyat 0:977e87915078 11 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
vithyat 0:977e87915078 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
vithyat 0:977e87915078 13 * See the License for the specific language governing permissions and
vithyat 0:977e87915078 14 * limitations under the License.
vithyat 0:977e87915078 15 */
vithyat 0:977e87915078 16
vithyat 0:977e87915078 17 #include "include/m2mcallbackstorage.h"
vithyat 0:977e87915078 18
vithyat 0:977e87915078 19 #include <stddef.h>
vithyat 0:977e87915078 20
vithyat 0:977e87915078 21 // Dummy constructor, which does not init any value to something meaningful but needed for array construction.
vithyat 0:977e87915078 22 // It is better to leave values unintialized, so the Valgrind will point out if the Vector is used without
vithyat 0:977e87915078 23 // setting real values in there.
vithyat 0:977e87915078 24 M2MCallbackAssociation::M2MCallbackAssociation()
vithyat 0:977e87915078 25 {
vithyat 0:977e87915078 26 }
vithyat 0:977e87915078 27
vithyat 0:977e87915078 28 M2MCallbackAssociation::M2MCallbackAssociation(const M2MBase *object, void *callback, M2MCallbackType type, void *client_args)
vithyat 0:977e87915078 29 : _object(object), _callback(callback), _type(type), _client_args(client_args)
vithyat 0:977e87915078 30 {
vithyat 0:977e87915078 31 }
vithyat 0:977e87915078 32
vithyat 0:977e87915078 33 M2MCallbackStorage* M2MCallbackStorage::_static_instance = NULL;
vithyat 0:977e87915078 34
vithyat 0:977e87915078 35 M2MCallbackStorage *M2MCallbackStorage::get_instance()
vithyat 0:977e87915078 36 {
vithyat 0:977e87915078 37 if (M2MCallbackStorage::_static_instance == NULL) {
vithyat 0:977e87915078 38 M2MCallbackStorage::_static_instance = new M2MCallbackStorage();
vithyat 0:977e87915078 39 }
vithyat 0:977e87915078 40 return M2MCallbackStorage::_static_instance;
vithyat 0:977e87915078 41 }
vithyat 0:977e87915078 42
vithyat 0:977e87915078 43 void M2MCallbackStorage::delete_instance()
vithyat 0:977e87915078 44 {
vithyat 0:977e87915078 45 delete M2MCallbackStorage::_static_instance;
vithyat 0:977e87915078 46 M2MCallbackStorage::_static_instance = NULL;
vithyat 0:977e87915078 47 }
vithyat 0:977e87915078 48
vithyat 0:977e87915078 49 M2MCallbackStorage::~M2MCallbackStorage()
vithyat 0:977e87915078 50 {
vithyat 0:977e87915078 51 // TODO: go through the list and delete all the FP<n> objects if there are any.
vithyat 0:977e87915078 52 // On the other hand, if the system is done properly, each m2mobject should actually
vithyat 0:977e87915078 53 // remove its callbacks from its destructor so there is nothing here to do
vithyat 0:977e87915078 54 }
vithyat 0:977e87915078 55
vithyat 0:977e87915078 56 bool M2MCallbackStorage::add_callback(const M2MBase &object,
vithyat 0:977e87915078 57 void *callback,
vithyat 0:977e87915078 58 M2MCallbackAssociation::M2MCallbackType type,
vithyat 0:977e87915078 59 void *client_args)
vithyat 0:977e87915078 60 {
vithyat 0:977e87915078 61 bool add_success = false;
vithyat 0:977e87915078 62 M2MCallbackStorage* instance = get_instance();
vithyat 0:977e87915078 63 if (instance) {
vithyat 0:977e87915078 64
vithyat 0:977e87915078 65 add_success = instance->do_add_callback(object, callback, type, client_args);
vithyat 0:977e87915078 66 }
vithyat 0:977e87915078 67 return add_success;
vithyat 0:977e87915078 68 }
vithyat 0:977e87915078 69
vithyat 0:977e87915078 70 bool M2MCallbackStorage::do_add_callback(const M2MBase &object, void *callback, M2MCallbackAssociation::M2MCallbackType type, void *client_args)
vithyat 0:977e87915078 71 {
vithyat 0:977e87915078 72 bool add_success = false;
vithyat 0:977e87915078 73
vithyat 0:977e87915078 74 // verify that the same callback is not re-added.
vithyat 0:977e87915078 75 if (does_callback_exist(object, callback, type) == false) {
vithyat 0:977e87915078 76
vithyat 0:977e87915078 77 const M2MCallbackAssociation association(&object, callback, type, client_args);
vithyat 0:977e87915078 78 _callbacks.push_back(association);
vithyat 0:977e87915078 79 add_success = true;
vithyat 0:977e87915078 80 }
vithyat 0:977e87915078 81
vithyat 0:977e87915078 82 return add_success;
vithyat 0:977e87915078 83 }
vithyat 0:977e87915078 84
vithyat 0:977e87915078 85 #if 0 // Functions not used currently
vithyat 0:977e87915078 86 void M2MCallbackStorage::remove_callbacks(const M2MBase &object)
vithyat 0:977e87915078 87 {
vithyat 0:977e87915078 88 // do not use the get_instance() here as it might create the instance
vithyat 0:977e87915078 89 M2MCallbackStorage* instance = M2MCallbackStorage::_static_instance;
vithyat 0:977e87915078 90 if (instance) {
vithyat 0:977e87915078 91
vithyat 0:977e87915078 92 instance->do_remove_callbacks(object);
vithyat 0:977e87915078 93 }
vithyat 0:977e87915078 94 }
vithyat 0:977e87915078 95
vithyat 0:977e87915078 96 void M2MCallbackStorage::do_remove_callbacks(const M2MBase &object)
vithyat 0:977e87915078 97 {
vithyat 0:977e87915078 98 // find any association to given object and delete them from the vector
vithyat 0:977e87915078 99 for (int index = 0; index < _callbacks.size();) {
vithyat 0:977e87915078 100 if (_callbacks[index]._object == &object) {
vithyat 0:977e87915078 101 _callbacks.erase(index);
vithyat 0:977e87915078 102 } else {
vithyat 0:977e87915078 103 index++;
vithyat 0:977e87915078 104 }
vithyat 0:977e87915078 105 }
vithyat 0:977e87915078 106 }
vithyat 0:977e87915078 107 #endif
vithyat 0:977e87915078 108
vithyat 0:977e87915078 109 void* M2MCallbackStorage::remove_callback(const M2MBase &object, M2MCallbackAssociation::M2MCallbackType type)
vithyat 0:977e87915078 110 {
vithyat 0:977e87915078 111 void* callback = NULL;
vithyat 0:977e87915078 112
vithyat 0:977e87915078 113 // do not use the get_instance() here as it might create the instance
vithyat 0:977e87915078 114 M2MCallbackStorage* instance = M2MCallbackStorage::_static_instance;
vithyat 0:977e87915078 115 if (instance) {
vithyat 0:977e87915078 116
vithyat 0:977e87915078 117 callback = instance->do_remove_callback(object, type);
vithyat 0:977e87915078 118 }
vithyat 0:977e87915078 119 return callback;
vithyat 0:977e87915078 120 }
vithyat 0:977e87915078 121
vithyat 0:977e87915078 122 void* M2MCallbackStorage::do_remove_callback(const M2MBase &object, M2MCallbackAssociation::M2MCallbackType type)
vithyat 0:977e87915078 123 {
vithyat 0:977e87915078 124 void* callback = NULL;
vithyat 0:977e87915078 125 for (int index = 0; index < _callbacks.size(); index++) {
vithyat 0:977e87915078 126
vithyat 0:977e87915078 127 if ((_callbacks[index]._object == &object) && (_callbacks[index]._type == type)) {
vithyat 0:977e87915078 128 callback = _callbacks[index]._callback;
vithyat 0:977e87915078 129 _callbacks.erase(index);
vithyat 0:977e87915078 130 break;
vithyat 0:977e87915078 131 }
vithyat 0:977e87915078 132 }
vithyat 0:977e87915078 133 return callback;
vithyat 0:977e87915078 134 }
vithyat 0:977e87915078 135
vithyat 0:977e87915078 136 void* M2MCallbackStorage::get_callback(const M2MBase &object, M2MCallbackAssociation::M2MCallbackType type)
vithyat 0:977e87915078 137 {
vithyat 0:977e87915078 138 void* callback = NULL;
vithyat 0:977e87915078 139 const M2MCallbackStorage* instance = get_instance();
vithyat 0:977e87915078 140 if (instance) {
vithyat 0:977e87915078 141
vithyat 0:977e87915078 142 callback = instance->do_get_callback(object, type);
vithyat 0:977e87915078 143 }
vithyat 0:977e87915078 144 return callback;
vithyat 0:977e87915078 145 }
vithyat 0:977e87915078 146
vithyat 0:977e87915078 147 void* M2MCallbackStorage::do_get_callback(const M2MBase &object, M2MCallbackAssociation::M2MCallbackType type) const
vithyat 0:977e87915078 148 {
vithyat 0:977e87915078 149 void* callback = NULL;
vithyat 0:977e87915078 150 if (!_callbacks.empty()) {
vithyat 0:977e87915078 151 M2MCallbackAssociationList::const_iterator it = _callbacks.begin();
vithyat 0:977e87915078 152
vithyat 0:977e87915078 153 for ( ; it != _callbacks.end(); it++ ) {
vithyat 0:977e87915078 154
vithyat 0:977e87915078 155 if ((it->_object == &object) && (it->_type == type)) {
vithyat 0:977e87915078 156 callback = it->_callback;
vithyat 0:977e87915078 157 break;
vithyat 0:977e87915078 158 }
vithyat 0:977e87915078 159 }
vithyat 0:977e87915078 160 }
vithyat 0:977e87915078 161 return callback;
vithyat 0:977e87915078 162 }
vithyat 0:977e87915078 163
vithyat 0:977e87915078 164 M2MCallbackAssociation* M2MCallbackStorage::get_association_item(const M2MBase &object, M2MCallbackAssociation::M2MCallbackType type)
vithyat 0:977e87915078 165 {
vithyat 0:977e87915078 166 M2MCallbackAssociation* callback_association = NULL;
vithyat 0:977e87915078 167 const M2MCallbackStorage* instance = get_instance();
vithyat 0:977e87915078 168 if (instance) {
vithyat 0:977e87915078 169
vithyat 0:977e87915078 170 callback_association = instance->do_get_association_item(object, type);
vithyat 0:977e87915078 171 }
vithyat 0:977e87915078 172 return callback_association;
vithyat 0:977e87915078 173 }
vithyat 0:977e87915078 174
vithyat 0:977e87915078 175 M2MCallbackAssociation* M2MCallbackStorage::do_get_association_item(const M2MBase &object, M2MCallbackAssociation::M2MCallbackType type) const
vithyat 0:977e87915078 176 {
vithyat 0:977e87915078 177 M2MCallbackAssociation* callback_association = NULL;
vithyat 0:977e87915078 178 if (!_callbacks.empty()) {
vithyat 0:977e87915078 179 M2MCallbackAssociationList::const_iterator it = _callbacks.begin();
vithyat 0:977e87915078 180
vithyat 0:977e87915078 181 for ( ; it != _callbacks.end(); it++ ) {
vithyat 0:977e87915078 182
vithyat 0:977e87915078 183 if ((it->_object == &object) && (it->_type == type)) {
vithyat 0:977e87915078 184 callback_association = (M2MCallbackAssociation*)it;
vithyat 0:977e87915078 185 break;
vithyat 0:977e87915078 186 }
vithyat 0:977e87915078 187 }
vithyat 0:977e87915078 188 }
vithyat 0:977e87915078 189 return callback_association;
vithyat 0:977e87915078 190 }
vithyat 0:977e87915078 191
vithyat 0:977e87915078 192 bool M2MCallbackStorage::does_callback_exist(const M2MBase &object, M2MCallbackAssociation::M2MCallbackType type)
vithyat 0:977e87915078 193 {
vithyat 0:977e87915078 194 bool found = false;
vithyat 0:977e87915078 195 if (get_callback(object, type) != NULL) {
vithyat 0:977e87915078 196 found = true;
vithyat 0:977e87915078 197 }
vithyat 0:977e87915078 198 return found;
vithyat 0:977e87915078 199 }
vithyat 0:977e87915078 200
vithyat 0:977e87915078 201 bool M2MCallbackStorage::does_callback_exist(const M2MBase &object, void *callback, M2MCallbackAssociation::M2MCallbackType type) const
vithyat 0:977e87915078 202 {
vithyat 0:977e87915078 203 bool match_found = false;
vithyat 0:977e87915078 204
vithyat 0:977e87915078 205 if (!_callbacks.empty()) {
vithyat 0:977e87915078 206 M2MCallbackAssociationList::const_iterator it = _callbacks.begin();
vithyat 0:977e87915078 207
vithyat 0:977e87915078 208 for ( ; it != _callbacks.end(); it++ ) {
vithyat 0:977e87915078 209
vithyat 0:977e87915078 210 if ((it->_object == &object) && (it->_callback == callback) && (it->_type == type)) {
vithyat 0:977e87915078 211 match_found = true;
vithyat 0:977e87915078 212 break;
vithyat 0:977e87915078 213 }
vithyat 0:977e87915078 214 }
vithyat 0:977e87915078 215 }
vithyat 0:977e87915078 216
vithyat 0:977e87915078 217 return match_found;
vithyat 0:977e87915078 218 }