BTstack Bluetooth stack

Dependencies:   mbed USBHost

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers utils.h Source File

utils.h

00001 /*
00002  * Copyright (C) 2009 by Matthias Ringwald
00003  *
00004  * Redistribution and use in source and binary forms, with or without
00005  * modification, are permitted provided that the following conditions
00006  * are met:
00007  *
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the copyright holders nor the names of
00014  *    contributors may be used to endorse or promote products derived
00015  *    from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY MATTHIAS RINGWALD AND CONTRIBUTORS
00018  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00019  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00020  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
00021  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00022  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00023  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
00024  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
00025  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00026  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
00027  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00028  * SUCH DAMAGE.
00029  *
00030  */
00031 
00032 /*
00033  *  utils.h
00034  *
00035  *  General utility functions
00036  *
00037  *  Created by Matthias Ringwald on 7/23/09.
00038  */
00039 
00040 #pragma once
00041 
00042 
00043 #if defined __cplusplus
00044 extern "C" {
00045 #endif
00046 
00047 #include <stdint.h>
00048 
00049 /**
00050  * @brief hci connection handle type
00051  */
00052 typedef uint16_t hci_con_handle_t;
00053 
00054 /**
00055  * @brief Length of a bluetooth device address.
00056  */
00057 #define BD_ADDR_LEN 6
00058 typedef uint8_t bd_addr_t[BD_ADDR_LEN];
00059 
00060 /**
00061  * @brief The link key type
00062  */
00063 #define LINK_KEY_LEN 16
00064 typedef uint8_t link_key_t[LINK_KEY_LEN]; 
00065 
00066 /**
00067  * @brief The device name type
00068  */
00069 #define DEVICE_NAME_LEN 248
00070 typedef uint8_t device_name_t[DEVICE_NAME_LEN+1]; 
00071     
00072     
00073 // helper for BT little endian format
00074 #define READ_BT_16( buffer, pos) ( ((uint16_t) buffer[pos]) | (((uint16_t)buffer[pos+1]) << 8))
00075 #define READ_BT_24( buffer, pos) ( ((uint32_t) buffer[pos]) | (((uint32_t)buffer[pos+1]) << 8) | (((uint32_t)buffer[pos+2]) << 16))
00076 #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)
00077 
00078 // helper for SDP big endian format
00079 #define READ_NET_16( buffer, pos) ( ((uint16_t) buffer[pos+1]) | (((uint16_t)buffer[pos  ]) << 8))
00080 #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)
00081 
00082 // HCI CMD OGF/OCF
00083 #define READ_CMD_OGF(buffer) (buffer[1] >> 2)
00084 #define READ_CMD_OCF(buffer) ((buffer[1] & 0x03) << 8 | buffer[0])
00085 
00086 // check if command complete event for given command
00087 #define COMMAND_COMPLETE_EVENT(event,cmd) ( event[0] == HCI_EVENT_COMMAND_COMPLETE && READ_BT_16(event,3) == cmd.opcode)
00088 #define COMMAND_STATUS_EVENT(event,cmd) ( event[0] == HCI_EVENT_COMMAND_STATUS && READ_BT_16(event,4) == cmd.opcode)
00089 
00090 // Code+Len=2, Pkts+Opcode=3; total=5
00091 #define OFFSET_OF_DATA_IN_COMMAND_COMPLETE 5
00092 
00093 // ACL Packet
00094 #define READ_ACL_CONNECTION_HANDLE( buffer ) ( READ_BT_16(buffer,0) & 0x0fff)
00095 #define READ_ACL_FLAGS( buffer )      ( buffer[1] >> 4 )
00096 #define READ_ACL_LENGTH( buffer )     (READ_BT_16(buffer, 2))
00097 
00098 // L2CAP Packet
00099 #define READ_L2CAP_LENGTH(buffer)     ( READ_BT_16(buffer, 4))
00100 #define READ_L2CAP_CHANNEL_ID(buffer) ( READ_BT_16(buffer, 6))
00101 
00102 void bt_store_16(uint8_t *buffer, uint16_t pos, uint16_t value);
00103 void bt_store_32(uint8_t *buffer, uint16_t pos, uint32_t value);
00104 void bt_flip_addr(bd_addr_t dest, bd_addr_t src);
00105 
00106 void net_store_16(uint8_t *buffer, uint16_t pos, uint16_t value);
00107 void net_store_32(uint8_t *buffer, uint16_t pos, uint32_t value);
00108 
00109 void hexdump(void *data, int size);
00110 void printUUID(uint8_t *uuid);
00111 
00112 // @deprecated please use more convenient bd_addr_to_str
00113 void print_bd_addr( bd_addr_t addr);
00114 char * bd_addr_to_str(bd_addr_t addr);
00115 
00116 int sscan_bd_addr(uint8_t * addr_string, bd_addr_t addr);
00117     
00118 uint8_t crc8_check(uint8_t *data, uint16_t len, uint8_t check_sum);
00119 uint8_t crc8_calc(uint8_t *data, uint16_t len);
00120 
00121 #define BD_ADDR_CMP(a,b) memcmp(a,b, BD_ADDR_LEN)
00122 #define BD_ADDR_COPY(dest,src) memcpy(dest,src,BD_ADDR_LEN)
00123 
00124 #if defined __cplusplus
00125 }
00126 #endif
00127