.

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers pretty_printer.h Source File

pretty_printer.h

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2018 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include <mbed.h>
00018 #include "ble/BLE.h"
00019 
00020 /* for seeding random number generator */
00021 static bool seeded = false;
00022 
00023 inline void print_error(ble_error_t error, const char* msg)
00024 {
00025     printf("%s: ", msg);
00026     switch(error) {
00027         case BLE_ERROR_NONE:
00028             printf("BLE_ERROR_NONE: No error");
00029             break;
00030         case BLE_ERROR_BUFFER_OVERFLOW:
00031             printf("BLE_ERROR_BUFFER_OVERFLOW: The requested action would cause a buffer overflow and has been aborted");
00032             break;
00033         case BLE_ERROR_NOT_IMPLEMENTED:
00034             printf("BLE_ERROR_NOT_IMPLEMENTED: Requested a feature that isn't yet implement or isn't supported by the target HW");
00035             break;
00036         case BLE_ERROR_PARAM_OUT_OF_RANGE:
00037             printf("BLE_ERROR_PARAM_OUT_OF_RANGE: One of the supplied parameters is outside the valid range");
00038             break;
00039         case BLE_ERROR_INVALID_PARAM:
00040             printf("BLE_ERROR_INVALID_PARAM: One of the supplied parameters is invalid");
00041             break;
00042         case BLE_STACK_BUSY:
00043             printf("BLE_STACK_BUSY: The stack is busy");
00044             break;
00045         case BLE_ERROR_INVALID_STATE:
00046             printf("BLE_ERROR_INVALID_STATE: Invalid state");
00047             break;
00048         case BLE_ERROR_NO_MEM:
00049             printf("BLE_ERROR_NO_MEM: Out of Memory");
00050             break;
00051         case BLE_ERROR_OPERATION_NOT_PERMITTED:
00052             printf("BLE_ERROR_OPERATION_NOT_PERMITTED");
00053             break;
00054         case BLE_ERROR_INITIALIZATION_INCOMPLETE:
00055             printf("BLE_ERROR_INITIALIZATION_INCOMPLETE");
00056             break;
00057         case BLE_ERROR_ALREADY_INITIALIZED:
00058             printf("BLE_ERROR_ALREADY_INITIALIZED");
00059             break;
00060         case BLE_ERROR_UNSPECIFIED:
00061             printf("BLE_ERROR_UNSPECIFIED: Unknown error");
00062             break;
00063         case BLE_ERROR_INTERNAL_STACK_FAILURE:
00064             printf("BLE_ERROR_INTERNAL_STACK_FAILURE: internal stack faillure");
00065             break;
00066     }
00067     printf("\r\n");
00068 }
00069 
00070 /** print device address to the terminal */
00071 inline void print_address(const uint8_t *addr)
00072 {
00073     printf("%02x:%02x:%02x:%02x:%02x:%02x\r\n",
00074            addr[5], addr[4], addr[3], addr[2], addr[1], addr[0]);
00075 }
00076 
00077 inline void print_mac_address()
00078 {
00079     /* Print out device MAC address to the console*/
00080     Gap::AddressType_t addr_type;
00081     Gap::Address_t address;
00082     BLE::Instance().gap().getAddress(&addr_type, address);
00083     printf("DEVICE MAC ADDRESS: ");
00084     print_address(address);
00085 
00086     if (!seeded) {
00087         seeded = true;
00088         /* use the address as a seed */
00089         uint8_t* random_data = address;
00090         srand(*((unsigned int*)random_data));
00091     }
00092 }
00093 
00094 inline const char* phy_to_string(Gap::Phy_t phy) {
00095     switch(phy.value()) {
00096         case Gap::Phy_t::LE_1M:
00097             return "LE 1M";
00098         case Gap::Phy_t::LE_2M:
00099             return "LE 2M";
00100         case Gap::Phy_t::LE_CODED:
00101             return "LE coded";
00102         default:
00103             return "invalid PHY";
00104     }
00105 }