|
| 1 | +/* mbed Microcontroller Library |
| 2 | + * Copyright (c) 2021 ARM Limited |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +#ifndef BLE_CLIAPP_BLE_TRACE_HELPERS_H |
| 19 | +#define BLE_CLIAPP_BLE_TRACE_HELPERS_H |
| 20 | + |
| 21 | +#include "ble/SecurityManager.h" |
| 22 | +#include "mbed-trace/mbed_trace.h" |
| 23 | + |
| 24 | +namespace ble { |
| 25 | + |
| 26 | +static inline constexpr const char* to_string(bool v) |
| 27 | +{ |
| 28 | + if (v) { |
| 29 | + return "true"; |
| 30 | + } else { |
| 31 | + return "false"; |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +static inline constexpr const char* to_string(ble::SecurityManager::SecurityIOCapabilities_t capabilities) |
| 36 | +{ |
| 37 | + switch(capabilities) { |
| 38 | + case ble::SecurityManager::IO_CAPS_DISPLAY_ONLY: |
| 39 | + return "IO_CAPS_DISPLAY_ONLY"; |
| 40 | + case ble::SecurityManager::IO_CAPS_DISPLAY_YESNO: |
| 41 | + return "IO_CAPS_DISPLAY_YESNO"; |
| 42 | + case ble::SecurityManager::IO_CAPS_KEYBOARD_DISPLAY: |
| 43 | + return "IO_CAPS_KEYBOARD_DISPLAY"; |
| 44 | + case ble::SecurityManager::IO_CAPS_KEYBOARD_ONLY: |
| 45 | + return "IO_CAPS_KEYBOARD_ONLY"; |
| 46 | + case ble::SecurityManager::IO_CAPS_NONE: |
| 47 | + return "IO_CAPS_NONE"; |
| 48 | + default: |
| 49 | + return "unknown"; |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +static inline const char* to_string(ble::io_capability_t capabilities) |
| 54 | +{ |
| 55 | + switch (capabilities.value()) { |
| 56 | + case ble::io_capability_t::DISPLAY_ONLY: |
| 57 | + return "DISPLAY_ONLY"; |
| 58 | + case ble::io_capability_t::DISPLAY_YES_NO: |
| 59 | + return "IO_CAPS_DISPLAY_DISPLAY_YES_NOYESNO"; |
| 60 | + case ble::io_capability_t::KEYBOARD_ONLY: |
| 61 | + return "KEYBOARD_ONLY"; |
| 62 | + case ble::io_capability_t::NO_INPUT_NO_OUTPUT: |
| 63 | + return "NO_INPUT_NO_OUTPUT"; |
| 64 | + case ble::io_capability_t::KEYBOARD_DISPLAY: |
| 65 | + return "KEYBOARD_DISPLAY"; |
| 66 | + default: |
| 67 | + return "unknown"; |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +static inline constexpr const char* to_string(ble::SecurityManager::SecurityMode_t security_mode) |
| 72 | +{ |
| 73 | + switch (security_mode) { |
| 74 | + case ble::SecurityManager::SECURITY_MODE_NO_ACCESS: |
| 75 | + return "SECURITY_MODE_NO_ACCESS"; |
| 76 | + case ble::SecurityManager::SECURITY_MODE_ENCRYPTION_OPEN_LINK: |
| 77 | + return "SECURITY_MODE_ENCRYPTION_OPEN_LINK"; |
| 78 | + case ble::SecurityManager::SECURITY_MODE_ENCRYPTION_NO_MITM: |
| 79 | + return "SECURITY_MODE_ENCRYPTION_NO_MITM"; |
| 80 | + case ble::SecurityManager::SECURITY_MODE_ENCRYPTION_WITH_MITM: |
| 81 | + return "SECURITY_MODE_ENCRYPTION_WITH_MITM"; |
| 82 | + case ble::SecurityManager::SECURITY_MODE_SIGNED_NO_MITM: |
| 83 | + return "SECURITY_MODE_SIGNED_NO_MITM"; |
| 84 | + case ble::SecurityManager::SECURITY_MODE_SIGNED_WITH_MITM: |
| 85 | + return "SECURITY_MODE_SIGNED_WITH_MITM"; |
| 86 | + default: |
| 87 | + return "Unknown"; |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +static inline constexpr const char* passkey_str(const uint8_t* passkey) |
| 92 | +{ |
| 93 | + if (!passkey) { |
| 94 | + return "0"; |
| 95 | + } else { |
| 96 | + return (mbed_trace_array)(passkey, 6); |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +static inline const char* to_string(const ble::address_t& address) |
| 101 | +{ |
| 102 | + return (mbed_trace_array)(address.data(), address.size()); |
| 103 | +} |
| 104 | + |
| 105 | +template<size_t size> |
| 106 | +const char* to_string(const ble::byte_array_t<size> &array) |
| 107 | +{ |
| 108 | + return (mbed_trace_array)(array.data(), array.size()); |
| 109 | +} |
| 110 | + |
| 111 | +static inline const char* to_string(const ble::link_encryption_t encryption) |
| 112 | +{ |
| 113 | + using link_encryption_t = ble::link_encryption_t; |
| 114 | + |
| 115 | + switch (encryption.value()) { |
| 116 | + case link_encryption_t::NOT_ENCRYPTED: |
| 117 | + return "NOT_ENCRYPTED"; |
| 118 | + case link_encryption_t::ENCRYPTION_IN_PROGRESS: |
| 119 | + return "ENCRYPTION_IN_PROGRESS"; |
| 120 | + case link_encryption_t::ENCRYPTED: |
| 121 | + return "ENCRYPTED"; |
| 122 | + case link_encryption_t::ENCRYPTED_WITH_MITM: |
| 123 | + return "ENCRYPTED_WITH_MITM"; |
| 124 | + case link_encryption_t::ENCRYPTED_WITH_SC_AND_MITM: |
| 125 | + return "ENCRYPTED_WITH_SC_AND_MITM"; |
| 126 | + default: |
| 127 | + return "Unknown"; |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +static inline const char* to_string(Keypress_t keypress) |
| 132 | +{ |
| 133 | + switch (keypress) { |
| 134 | + case KEYPRESS_STARTED: |
| 135 | + return "KEYPRESS_STARTED"; |
| 136 | + case KEYPRESS_ENTERED: |
| 137 | + return "KEYPRESS_ENTERED"; |
| 138 | + case KEYPRESS_ERASED: |
| 139 | + return "KEYPRESS_ERASED"; |
| 140 | + case KEYPRESS_CLEARED: |
| 141 | + return "KEYPRESS_CLEARED"; |
| 142 | + case KEYPRESS_COMPLETED: |
| 143 | + return "KEYPRESS_COMPLETED"; |
| 144 | + default: |
| 145 | + return "Unknown"; |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +static inline const char *to_string(ble::pairing_failure_t reason) |
| 150 | +{ |
| 151 | + switch (reason.value()) { |
| 152 | + case ble::pairing_failure_t::PASSKEY_ENTRY_FAILED: |
| 153 | + return "PASSKEY_ENTRY_FAILED"; |
| 154 | + case ble::pairing_failure_t::OOB_NOT_AVAILABLE: |
| 155 | + return "OOB_NOT_AVAILABLE"; |
| 156 | + case ble::pairing_failure_t::AUTHENTICATION_REQUIREMENTS: |
| 157 | + return "AUTHENTICATION_REQUIREMENTS"; |
| 158 | + case ble::pairing_failure_t::CONFIRM_VALUE_FAILED: |
| 159 | + return "CONFIRM_VALUE_FAILED"; |
| 160 | + case ble::pairing_failure_t::PAIRING_NOT_SUPPORTED: |
| 161 | + return "PAIRING_NOT_SUPPORTED"; |
| 162 | + case ble::pairing_failure_t::ENCRYPTION_KEY_SIZE: |
| 163 | + return "ENCRYPTION_KEY_SIZE"; |
| 164 | + case ble::pairing_failure_t::COMMAND_NOT_SUPPORTED: |
| 165 | + return "COMMAND_NOT_SUPPORTED"; |
| 166 | + case ble::pairing_failure_t::UNSPECIFIED_REASON: |
| 167 | + return "UNSPECIFIED_REASON"; |
| 168 | + case ble::pairing_failure_t::REPEATED_ATTEMPTS: |
| 169 | + return "REPEATED_ATTEMPTS"; |
| 170 | + case ble::pairing_failure_t::INVALID_PARAMETERS: |
| 171 | + return "INVALID_PARAMETERS"; |
| 172 | + case ble::pairing_failure_t::DHKEY_CHECK_FAILED: |
| 173 | + return "DHKEY_CHECK_FAILED"; |
| 174 | + case ble::pairing_failure_t::NUMERIC_COMPARISON_FAILED: |
| 175 | + return "NUMERIC_COMPARISON_FAILED"; |
| 176 | + case ble::pairing_failure_t::BR_EDR_PAIRING_IN_PROGRESS: |
| 177 | + return "BR_EDR_PAIRING_IN_PROGRESS"; |
| 178 | + case ble::pairing_failure_t::CROSS_TRANSPORT_KEY_DERIVATION_OR_GENERATION_NOT_ALLOWED: |
| 179 | + return "CROSS_TRANSPORT_KEY_DERIVATION_OR_GENERATION_NOT_ALLOWED"; |
| 180 | + default: |
| 181 | + return "Unknown"; |
| 182 | + } |
| 183 | +} |
| 184 | + |
| 185 | +static inline const char *to_string(target_peer_address_type_t type) |
| 186 | +{ |
| 187 | + if (type == target_peer_address_type_t::PUBLIC) { |
| 188 | + return "PUBLIC"; |
| 189 | + } else { |
| 190 | + return "RANDOM"; |
| 191 | + } |
| 192 | +} |
| 193 | + |
| 194 | +static inline const char *to_string(privacy_mode_t mode) |
| 195 | +{ |
| 196 | + if (mode == privacy_mode_t::NETWORK) { |
| 197 | + return "NETWORK"; |
| 198 | + } else { |
| 199 | + return "DEVICE"; |
| 200 | + } |
| 201 | +} |
| 202 | + |
| 203 | + |
| 204 | +} // namespace ble |
| 205 | + |
| 206 | +#endif //BLE_CLIAPP_BLE_TRACE_HELPERS_H |
0 commit comments