ban4jp -
/
BTstack_test
https://mbed.org/forum/ja/topic/4890/
Fork of BTstack by
BTstack/btstack/utils.h@0:1ed23ab1345f, 2012-06-26 (annotated)
- Committer:
- va009039
- Date:
- Tue Jun 26 14:27:45 2012 +0000
- Revision:
- 0:1ed23ab1345f
fix overflow spp_service_buffer
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
va009039 | 0:1ed23ab1345f | 1 | /* |
va009039 | 0:1ed23ab1345f | 2 | * Copyright (C) 2009 by Matthias Ringwald |
va009039 | 0:1ed23ab1345f | 3 | * |
va009039 | 0:1ed23ab1345f | 4 | * Redistribution and use in source and binary forms, with or without |
va009039 | 0:1ed23ab1345f | 5 | * modification, are permitted provided that the following conditions |
va009039 | 0:1ed23ab1345f | 6 | * are met: |
va009039 | 0:1ed23ab1345f | 7 | * |
va009039 | 0:1ed23ab1345f | 8 | * 1. Redistributions of source code must retain the above copyright |
va009039 | 0:1ed23ab1345f | 9 | * notice, this list of conditions and the following disclaimer. |
va009039 | 0:1ed23ab1345f | 10 | * 2. Redistributions in binary form must reproduce the above copyright |
va009039 | 0:1ed23ab1345f | 11 | * notice, this list of conditions and the following disclaimer in the |
va009039 | 0:1ed23ab1345f | 12 | * documentation and/or other materials provided with the distribution. |
va009039 | 0:1ed23ab1345f | 13 | * 3. Neither the name of the copyright holders nor the names of |
va009039 | 0:1ed23ab1345f | 14 | * contributors may be used to endorse or promote products derived |
va009039 | 0:1ed23ab1345f | 15 | * from this software without specific prior written permission. |
va009039 | 0:1ed23ab1345f | 16 | * |
va009039 | 0:1ed23ab1345f | 17 | * THIS SOFTWARE IS PROVIDED BY MATTHIAS RINGWALD AND CONTRIBUTORS |
va009039 | 0:1ed23ab1345f | 18 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
va009039 | 0:1ed23ab1345f | 19 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
va009039 | 0:1ed23ab1345f | 20 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS |
va009039 | 0:1ed23ab1345f | 21 | * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
va009039 | 0:1ed23ab1345f | 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
va009039 | 0:1ed23ab1345f | 23 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
va009039 | 0:1ed23ab1345f | 24 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
va009039 | 0:1ed23ab1345f | 25 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
va009039 | 0:1ed23ab1345f | 26 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF |
va009039 | 0:1ed23ab1345f | 27 | * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
va009039 | 0:1ed23ab1345f | 28 | * SUCH DAMAGE. |
va009039 | 0:1ed23ab1345f | 29 | * |
va009039 | 0:1ed23ab1345f | 30 | */ |
va009039 | 0:1ed23ab1345f | 31 | |
va009039 | 0:1ed23ab1345f | 32 | /* |
va009039 | 0:1ed23ab1345f | 33 | * utils.h |
va009039 | 0:1ed23ab1345f | 34 | * |
va009039 | 0:1ed23ab1345f | 35 | * General utility functions |
va009039 | 0:1ed23ab1345f | 36 | * |
va009039 | 0:1ed23ab1345f | 37 | * Created by Matthias Ringwald on 7/23/09. |
va009039 | 0:1ed23ab1345f | 38 | */ |
va009039 | 0:1ed23ab1345f | 39 | |
va009039 | 0:1ed23ab1345f | 40 | #pragma once |
va009039 | 0:1ed23ab1345f | 41 | |
va009039 | 0:1ed23ab1345f | 42 | |
va009039 | 0:1ed23ab1345f | 43 | #if defined __cplusplus |
va009039 | 0:1ed23ab1345f | 44 | extern "C" { |
va009039 | 0:1ed23ab1345f | 45 | #endif |
va009039 | 0:1ed23ab1345f | 46 | |
va009039 | 0:1ed23ab1345f | 47 | #include <stdint.h> |
va009039 | 0:1ed23ab1345f | 48 | |
va009039 | 0:1ed23ab1345f | 49 | /** |
va009039 | 0:1ed23ab1345f | 50 | * @brief hci connection handle type |
va009039 | 0:1ed23ab1345f | 51 | */ |
va009039 | 0:1ed23ab1345f | 52 | typedef uint16_t hci_con_handle_t; |
va009039 | 0:1ed23ab1345f | 53 | |
va009039 | 0:1ed23ab1345f | 54 | /** |
va009039 | 0:1ed23ab1345f | 55 | * @brief Length of a bluetooth device address. |
va009039 | 0:1ed23ab1345f | 56 | */ |
va009039 | 0:1ed23ab1345f | 57 | #define BD_ADDR_LEN 6 |
va009039 | 0:1ed23ab1345f | 58 | typedef uint8_t bd_addr_t[BD_ADDR_LEN]; |
va009039 | 0:1ed23ab1345f | 59 | |
va009039 | 0:1ed23ab1345f | 60 | /** |
va009039 | 0:1ed23ab1345f | 61 | * @brief The link key type |
va009039 | 0:1ed23ab1345f | 62 | */ |
va009039 | 0:1ed23ab1345f | 63 | #define LINK_KEY_LEN 16 |
va009039 | 0:1ed23ab1345f | 64 | typedef uint8_t link_key_t[LINK_KEY_LEN]; |
va009039 | 0:1ed23ab1345f | 65 | |
va009039 | 0:1ed23ab1345f | 66 | /** |
va009039 | 0:1ed23ab1345f | 67 | * @brief The device name type |
va009039 | 0:1ed23ab1345f | 68 | */ |
va009039 | 0:1ed23ab1345f | 69 | #define DEVICE_NAME_LEN 248 |
va009039 | 0:1ed23ab1345f | 70 | typedef uint8_t device_name_t[DEVICE_NAME_LEN+1]; |
va009039 | 0:1ed23ab1345f | 71 | |
va009039 | 0:1ed23ab1345f | 72 | |
va009039 | 0:1ed23ab1345f | 73 | // helper for BT little endian format |
va009039 | 0:1ed23ab1345f | 74 | #define READ_BT_16( buffer, pos) ( ((uint16_t) buffer[pos]) | (((uint16_t)buffer[pos+1]) << 8)) |
va009039 | 0:1ed23ab1345f | 75 | #define READ_BT_24( buffer, pos) ( ((uint32_t) buffer[pos]) | (((uint32_t)buffer[pos+1]) << 8) | (((uint32_t)buffer[pos+2]) << 16)) |
va009039 | 0:1ed23ab1345f | 76 | #define READ_BT_32( buffer, pos) ( ((uint32_t) buffer[pos]) | (((uint32_t)buffer[pos+1]) << 8) | (((uint32_t)buffer[pos+2]) << 16) | (((uint32_t) buffer[pos+3])) << 24) |
va009039 | 0:1ed23ab1345f | 77 | |
va009039 | 0:1ed23ab1345f | 78 | // helper for SDP big endian format |
va009039 | 0:1ed23ab1345f | 79 | #define READ_NET_16( buffer, pos) ( ((uint16_t) buffer[pos+1]) | (((uint16_t)buffer[pos ]) << 8)) |
va009039 | 0:1ed23ab1345f | 80 | #define READ_NET_32( buffer, pos) ( ((uint32_t) buffer[pos+3]) | (((uint32_t)buffer[pos+2]) << 8) | (((uint32_t)buffer[pos+1]) << 16) | (((uint32_t) buffer[pos])) << 24) |
va009039 | 0:1ed23ab1345f | 81 | |
va009039 | 0:1ed23ab1345f | 82 | // HCI CMD OGF/OCF |
va009039 | 0:1ed23ab1345f | 83 | #define READ_CMD_OGF(buffer) (buffer[1] >> 2) |
va009039 | 0:1ed23ab1345f | 84 | #define READ_CMD_OCF(buffer) ((buffer[1] & 0x03) << 8 | buffer[0]) |
va009039 | 0:1ed23ab1345f | 85 | |
va009039 | 0:1ed23ab1345f | 86 | // check if command complete event for given command |
va009039 | 0:1ed23ab1345f | 87 | #define COMMAND_COMPLETE_EVENT(event,cmd) ( event[0] == HCI_EVENT_COMMAND_COMPLETE && READ_BT_16(event,3) == cmd.opcode) |
va009039 | 0:1ed23ab1345f | 88 | #define COMMAND_STATUS_EVENT(event,cmd) ( event[0] == HCI_EVENT_COMMAND_STATUS && READ_BT_16(event,4) == cmd.opcode) |
va009039 | 0:1ed23ab1345f | 89 | |
va009039 | 0:1ed23ab1345f | 90 | // Code+Len=2, Pkts+Opcode=3; total=5 |
va009039 | 0:1ed23ab1345f | 91 | #define OFFSET_OF_DATA_IN_COMMAND_COMPLETE 5 |
va009039 | 0:1ed23ab1345f | 92 | |
va009039 | 0:1ed23ab1345f | 93 | // ACL Packet |
va009039 | 0:1ed23ab1345f | 94 | #define READ_ACL_CONNECTION_HANDLE( buffer ) ( READ_BT_16(buffer,0) & 0x0fff) |
va009039 | 0:1ed23ab1345f | 95 | #define READ_ACL_FLAGS( buffer ) ( buffer[1] >> 4 ) |
va009039 | 0:1ed23ab1345f | 96 | #define READ_ACL_LENGTH( buffer ) (READ_BT_16(buffer, 2)) |
va009039 | 0:1ed23ab1345f | 97 | |
va009039 | 0:1ed23ab1345f | 98 | // L2CAP Packet |
va009039 | 0:1ed23ab1345f | 99 | #define READ_L2CAP_LENGTH(buffer) ( READ_BT_16(buffer, 4)) |
va009039 | 0:1ed23ab1345f | 100 | #define READ_L2CAP_CHANNEL_ID(buffer) ( READ_BT_16(buffer, 6)) |
va009039 | 0:1ed23ab1345f | 101 | |
va009039 | 0:1ed23ab1345f | 102 | void bt_store_16(uint8_t *buffer, uint16_t pos, uint16_t value); |
va009039 | 0:1ed23ab1345f | 103 | void bt_store_32(uint8_t *buffer, uint16_t pos, uint32_t value); |
va009039 | 0:1ed23ab1345f | 104 | void bt_flip_addr(bd_addr_t dest, bd_addr_t src); |
va009039 | 0:1ed23ab1345f | 105 | |
va009039 | 0:1ed23ab1345f | 106 | void net_store_16(uint8_t *buffer, uint16_t pos, uint16_t value); |
va009039 | 0:1ed23ab1345f | 107 | void net_store_32(uint8_t *buffer, uint16_t pos, uint32_t value); |
va009039 | 0:1ed23ab1345f | 108 | |
va009039 | 0:1ed23ab1345f | 109 | void hexdump(void *data, int size); |
va009039 | 0:1ed23ab1345f | 110 | void printUUID(uint8_t *uuid); |
va009039 | 0:1ed23ab1345f | 111 | |
va009039 | 0:1ed23ab1345f | 112 | // @deprecated please use more convenient bd_addr_to_str |
va009039 | 0:1ed23ab1345f | 113 | void print_bd_addr( bd_addr_t addr); |
va009039 | 0:1ed23ab1345f | 114 | char * bd_addr_to_str(bd_addr_t addr); |
va009039 | 0:1ed23ab1345f | 115 | |
va009039 | 0:1ed23ab1345f | 116 | int sscan_bd_addr(uint8_t * addr_string, bd_addr_t addr); |
va009039 | 0:1ed23ab1345f | 117 | |
va009039 | 0:1ed23ab1345f | 118 | uint8_t crc8_check(uint8_t *data, uint16_t len, uint8_t check_sum); |
va009039 | 0:1ed23ab1345f | 119 | uint8_t crc8_calc(uint8_t *data, uint16_t len); |
va009039 | 0:1ed23ab1345f | 120 | |
va009039 | 0:1ed23ab1345f | 121 | #define BD_ADDR_CMP(a,b) memcmp(a,b, BD_ADDR_LEN) |
va009039 | 0:1ed23ab1345f | 122 | #define BD_ADDR_COPY(dest,src) memcpy(dest,src,BD_ADDR_LEN) |
va009039 | 0:1ed23ab1345f | 123 | |
va009039 | 0:1ed23ab1345f | 124 | #if defined __cplusplus |
va009039 | 0:1ed23ab1345f | 125 | } |
va009039 | 0:1ed23ab1345f | 126 | #endif |
va009039 | 0:1ed23ab1345f | 127 |