This is a basic code to be used for Sequana BLE Lab exercises.

Committer:
lru
Date:
Fri Mar 22 10:11:59 2019 +0000
Revision:
4:44690f4495ef
Parent:
0:ff033dfc838b
Initialized properly device name characteristic and changed type of advertising MAC address used to guarantee uniqueness.

Who changed what in which revision?

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