仕倫 林 / Mbed OS 12_1

Dependencies:   X_NUCLEO_IKS01A2

Committer:
lsl097
Date:
Thu Apr 30 14:28:59 2020 +0000
Revision:
2:5da515ba10ff
Parent:
0:9c0e0ac79e75
FIN

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jim_lsj 0:9c0e0ac79e75 1 /* mbed Microcontroller Library
jim_lsj 0:9c0e0ac79e75 2 * Copyright (c) 2018 ARM Limited
jim_lsj 0:9c0e0ac79e75 3 *
jim_lsj 0:9c0e0ac79e75 4 * Licensed under the Apache License, Version 2.0 (the "License");
jim_lsj 0:9c0e0ac79e75 5 * you may not use this file except in compliance with the License.
jim_lsj 0:9c0e0ac79e75 6 * You may obtain a copy of the License at
jim_lsj 0:9c0e0ac79e75 7 *
jim_lsj 0:9c0e0ac79e75 8 * http://www.apache.org/licenses/LICENSE-2.0
jim_lsj 0:9c0e0ac79e75 9 *
jim_lsj 0:9c0e0ac79e75 10 * Unless required by applicable law or agreed to in writing, software
jim_lsj 0:9c0e0ac79e75 11 * distributed under the License is distributed on an "AS IS" BASIS,
jim_lsj 0:9c0e0ac79e75 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
jim_lsj 0:9c0e0ac79e75 13 * See the License for the specific language governing permissions and
jim_lsj 0:9c0e0ac79e75 14 * limitations under the License.
jim_lsj 0:9c0e0ac79e75 15 */
jim_lsj 0:9c0e0ac79e75 16
jim_lsj 0:9c0e0ac79e75 17 #include <mbed.h>
jim_lsj 0:9c0e0ac79e75 18 #include "ble/BLE.h"
jim_lsj 0:9c0e0ac79e75 19
jim_lsj 0:9c0e0ac79e75 20 inline void print_error(ble_error_t error, const char* msg)
jim_lsj 0:9c0e0ac79e75 21 {
jim_lsj 0:9c0e0ac79e75 22 printf("%s: ", msg);
jim_lsj 0:9c0e0ac79e75 23 switch(error) {
jim_lsj 0:9c0e0ac79e75 24 case BLE_ERROR_NONE:
jim_lsj 0:9c0e0ac79e75 25 printf("BLE_ERROR_NONE: No error");
jim_lsj 0:9c0e0ac79e75 26 break;
jim_lsj 0:9c0e0ac79e75 27 case BLE_ERROR_BUFFER_OVERFLOW:
jim_lsj 0:9c0e0ac79e75 28 printf("BLE_ERROR_BUFFER_OVERFLOW: The requested action would cause a buffer overflow and has been aborted");
jim_lsj 0:9c0e0ac79e75 29 break;
jim_lsj 0:9c0e0ac79e75 30 case BLE_ERROR_NOT_IMPLEMENTED:
jim_lsj 0:9c0e0ac79e75 31 printf("BLE_ERROR_NOT_IMPLEMENTED: Requested a feature that isn't yet implement or isn't supported by the target HW");
jim_lsj 0:9c0e0ac79e75 32 break;
jim_lsj 0:9c0e0ac79e75 33 case BLE_ERROR_PARAM_OUT_OF_RANGE:
jim_lsj 0:9c0e0ac79e75 34 printf("BLE_ERROR_PARAM_OUT_OF_RANGE: One of the supplied parameters is outside the valid range");
jim_lsj 0:9c0e0ac79e75 35 break;
jim_lsj 0:9c0e0ac79e75 36 case BLE_ERROR_INVALID_PARAM:
jim_lsj 0:9c0e0ac79e75 37 printf("BLE_ERROR_INVALID_PARAM: One of the supplied parameters is invalid");
jim_lsj 0:9c0e0ac79e75 38 break;
jim_lsj 0:9c0e0ac79e75 39 case BLE_STACK_BUSY:
jim_lsj 0:9c0e0ac79e75 40 printf("BLE_STACK_BUSY: The stack is busy");
jim_lsj 0:9c0e0ac79e75 41 break;
jim_lsj 0:9c0e0ac79e75 42 case BLE_ERROR_INVALID_STATE:
jim_lsj 0:9c0e0ac79e75 43 printf("BLE_ERROR_INVALID_STATE: Invalid state");
jim_lsj 0:9c0e0ac79e75 44 break;
jim_lsj 0:9c0e0ac79e75 45 case BLE_ERROR_NO_MEM:
jim_lsj 0:9c0e0ac79e75 46 printf("BLE_ERROR_NO_MEM: Out of Memory");
jim_lsj 0:9c0e0ac79e75 47 break;
jim_lsj 0:9c0e0ac79e75 48 case BLE_ERROR_OPERATION_NOT_PERMITTED:
jim_lsj 0:9c0e0ac79e75 49 printf("BLE_ERROR_OPERATION_NOT_PERMITTED");
jim_lsj 0:9c0e0ac79e75 50 break;
jim_lsj 0:9c0e0ac79e75 51 case BLE_ERROR_INITIALIZATION_INCOMPLETE:
jim_lsj 0:9c0e0ac79e75 52 printf("BLE_ERROR_INITIALIZATION_INCOMPLETE");
jim_lsj 0:9c0e0ac79e75 53 break;
jim_lsj 0:9c0e0ac79e75 54 case BLE_ERROR_ALREADY_INITIALIZED:
jim_lsj 0:9c0e0ac79e75 55 printf("BLE_ERROR_ALREADY_INITIALIZED");
jim_lsj 0:9c0e0ac79e75 56 break;
jim_lsj 0:9c0e0ac79e75 57 case BLE_ERROR_UNSPECIFIED:
jim_lsj 0:9c0e0ac79e75 58 printf("BLE_ERROR_UNSPECIFIED: Unknown error");
jim_lsj 0:9c0e0ac79e75 59 break;
jim_lsj 0:9c0e0ac79e75 60 case BLE_ERROR_INTERNAL_STACK_FAILURE:
jim_lsj 0:9c0e0ac79e75 61 printf("BLE_ERROR_INTERNAL_STACK_FAILURE: internal stack faillure");
jim_lsj 0:9c0e0ac79e75 62 break;
jim_lsj 0:9c0e0ac79e75 63 }
jim_lsj 0:9c0e0ac79e75 64 printf("\r\n");
jim_lsj 0:9c0e0ac79e75 65 }
jim_lsj 0:9c0e0ac79e75 66
jim_lsj 0:9c0e0ac79e75 67 /** print device address to the terminal */
jim_lsj 0:9c0e0ac79e75 68 inline void print_address(const Gap::Address_t &addr)
jim_lsj 0:9c0e0ac79e75 69 {
jim_lsj 0:9c0e0ac79e75 70 printf("%02x:%02x:%02x:%02x:%02x:%02x\r\n",
jim_lsj 0:9c0e0ac79e75 71 addr[5], addr[4], addr[3], addr[2], addr[1], addr[0]);
jim_lsj 0:9c0e0ac79e75 72 }
jim_lsj 0:9c0e0ac79e75 73
jim_lsj 0:9c0e0ac79e75 74 inline void print_mac_address()
jim_lsj 0:9c0e0ac79e75 75 {
jim_lsj 0:9c0e0ac79e75 76 /* Print out device MAC address to the console*/
jim_lsj 0:9c0e0ac79e75 77 Gap::AddressType_t addr_type;
jim_lsj 0:9c0e0ac79e75 78 Gap::Address_t address;
jim_lsj 0:9c0e0ac79e75 79 BLE::Instance().gap().getAddress(&addr_type, address);
jim_lsj 0:9c0e0ac79e75 80 printf("DEVICE MAC ADDRESS: ");
jim_lsj 0:9c0e0ac79e75 81 print_address(address);
jim_lsj 0:9c0e0ac79e75 82 }
jim_lsj 0:9c0e0ac79e75 83
jim_lsj 0:9c0e0ac79e75 84 inline const char* phy_to_string(Gap::Phy_t phy) {
jim_lsj 0:9c0e0ac79e75 85 switch(phy.value()) {
jim_lsj 0:9c0e0ac79e75 86 case Gap::Phy_t::LE_1M:
jim_lsj 0:9c0e0ac79e75 87 return "LE 1M";
jim_lsj 0:9c0e0ac79e75 88 case Gap::Phy_t::LE_2M:
jim_lsj 0:9c0e0ac79e75 89 return "LE 2M";
jim_lsj 0:9c0e0ac79e75 90 case Gap::Phy_t::LE_CODED:
jim_lsj 0:9c0e0ac79e75 91 return "LE coded";
jim_lsj 0:9c0e0ac79e75 92 default:
jim_lsj 0:9c0e0ac79e75 93 return "invalid PHY";
jim_lsj 0:9c0e0ac79e75 94 }
jim_lsj 0:9c0e0ac79e75 95 }