Path: blob/next/external/cache/sources/hcitools/lib/bluetooth/bluetooth.h
18125 views
/*1*2* BlueZ - Bluetooth protocol stack for Linux3*4* Copyright (C) 2000-2001 Qualcomm Incorporated5* Copyright (C) 2002-2003 Maxim Krasnyansky <[email protected]>6* Copyright (C) 2002-2010 Marcel Holtmann <[email protected]>7*8*9* This program is free software; you can redistribute it and/or modify10* it under the terms of the GNU General Public License as published by11* the Free Software Foundation; either version 2 of the License, or12* (at your option) any later version.13*14* This program is distributed in the hope that it will be useful,15* but WITHOUT ANY WARRANTY; without even the implied warranty of16* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the17* GNU General Public License for more details.18*19* You should have received a copy of the GNU General Public License20* along with this program; if not, write to the Free Software21* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA22*23*/2425#ifndef __BLUETOOTH_H26#define __BLUETOOTH_H2728#ifdef __cplusplus29extern "C" {30#endif3132#include <stdio.h>33#include <stdint.h>34#include <string.h>35#include <endian.h>36#include <byteswap.h>37#include <netinet/in.h>3839#ifndef AF_BLUETOOTH40#define AF_BLUETOOTH 3141#define PF_BLUETOOTH AF_BLUETOOTH42#endif4344#define BTPROTO_L2CAP 045#define BTPROTO_HCI 146#define BTPROTO_SCO 247#define BTPROTO_RFCOMM 348#define BTPROTO_BNEP 449#define BTPROTO_CMTP 550#define BTPROTO_HIDP 651#define BTPROTO_AVDTP 75253#define SOL_HCI 054#define SOL_L2CAP 655#define SOL_SCO 1756#define SOL_RFCOMM 185758#ifndef SOL_BLUETOOTH59#define SOL_BLUETOOTH 27460#endif6162#define BT_SECURITY 463struct bt_security {64uint8_t level;65uint8_t key_size;66};67#define BT_SECURITY_SDP 068#define BT_SECURITY_LOW 169#define BT_SECURITY_MEDIUM 270#define BT_SECURITY_HIGH 37172#define BT_DEFER_SETUP 77374#define BT_FLUSHABLE 87576#define BT_FLUSHABLE_OFF 077#define BT_FLUSHABLE_ON 17879#define BT_CHANNEL_POLICY 108081/* BR/EDR only (default policy)82* AMP controllers cannot be used.83* Channel move requests from the remote device are denied.84* If the L2CAP channel is currently using AMP, move the channel to BR/EDR.85*/86#define BT_CHANNEL_POLICY_BREDR_ONLY 08788/* BR/EDR Preferred89* Allow use of AMP controllers.90* If the L2CAP channel is currently on AMP, move it to BR/EDR.91* Channel move requests from the remote device are allowed.92*/93#define BT_CHANNEL_POLICY_BREDR_PREFERRED 19495/* AMP Preferred96* Allow use of AMP controllers97* If the L2CAP channel is currently on BR/EDR and AMP controller98* resources are available, initiate a channel move to AMP.99* Channel move requests from the remote device are allowed.100* If the L2CAP socket has not been connected yet, try to create101* and configure the channel directly on an AMP controller rather102* than BR/EDR.103*/104#define BT_CHANNEL_POLICY_AMP_PREFERRED 2105106#define BT_VOICE 11107struct bt_voice {108uint16_t setting;109};110111#define BT_VOICE_TRANSPARENT 0x0003112#define BT_VOICE_CVSD_16BIT 0x0060113114/* Connection and socket states */115enum {116BT_CONNECTED = 1, /* Equal to TCP_ESTABLISHED to make net code happy */117BT_OPEN,118BT_BOUND,119BT_LISTEN,120BT_CONNECT,121BT_CONNECT2,122BT_CONFIG,123BT_DISCONN,124BT_CLOSED125};126127/* Byte order conversions */128#if __BYTE_ORDER == __LITTLE_ENDIAN129#define htobs(d) (d)130#define htobl(d) (d)131#define htobll(d) (d)132#define btohs(d) (d)133#define btohl(d) (d)134#define btohll(d) (d)135#elif __BYTE_ORDER == __BIG_ENDIAN136#define htobs(d) bswap_16(d)137#define htobl(d) bswap_32(d)138#define htobll(d) bswap_64(d)139#define btohs(d) bswap_16(d)140#define btohl(d) bswap_32(d)141#define btohll(d) bswap_64(d)142#else143#error "Unknown byte order"144#endif145146/* Bluetooth unaligned access */147#define bt_get_unaligned(ptr) \148({ \149struct __attribute__((packed)) { \150typeof(*(ptr)) __v; \151} *__p = (typeof(__p)) (ptr); \152__p->__v; \153})154155#define bt_put_unaligned(val, ptr) \156do { \157struct __attribute__((packed)) { \158typeof(*(ptr)) __v; \159} *__p = (typeof(__p)) (ptr); \160__p->__v = (val); \161} while(0)162163#if __BYTE_ORDER == __LITTLE_ENDIAN164static inline uint64_t bt_get_le64(const void *ptr)165{166return bt_get_unaligned((const uint64_t *) ptr);167}168169static inline uint64_t bt_get_be64(const void *ptr)170{171return bswap_64(bt_get_unaligned((const uint64_t *) ptr));172}173174static inline uint32_t bt_get_le32(const void *ptr)175{176return bt_get_unaligned((const uint32_t *) ptr);177}178179static inline uint32_t bt_get_be32(const void *ptr)180{181return bswap_32(bt_get_unaligned((const uint32_t *) ptr));182}183184static inline uint16_t bt_get_le16(const void *ptr)185{186return bt_get_unaligned((const uint16_t *) ptr);187}188189static inline uint16_t bt_get_be16(const void *ptr)190{191return bswap_16(bt_get_unaligned((const uint16_t *) ptr));192}193194static inline void bt_put_le64(uint64_t val, const void *ptr)195{196bt_put_unaligned(val, (uint64_t *) ptr);197}198199static inline void bt_put_be64(uint64_t val, const void *ptr)200{201bt_put_unaligned(bswap_64(val), (uint64_t *) ptr);202}203204static inline void bt_put_le32(uint32_t val, const void *ptr)205{206bt_put_unaligned(val, (uint32_t *) ptr);207}208209static inline void bt_put_be32(uint32_t val, const void *ptr)210{211bt_put_unaligned(bswap_32(val), (uint32_t *) ptr);212}213214static inline void bt_put_le16(uint16_t val, const void *ptr)215{216bt_put_unaligned(val, (uint16_t *) ptr);217}218219static inline void bt_put_be16(uint16_t val, const void *ptr)220{221bt_put_unaligned(bswap_16(val), (uint16_t *) ptr);222}223224#elif __BYTE_ORDER == __BIG_ENDIAN225static inline uint64_t bt_get_le64(const void *ptr)226{227return bswap_64(bt_get_unaligned((const uint64_t *) ptr));228}229230static inline uint64_t bt_get_be64(const void *ptr)231{232return bt_get_unaligned((const uint64_t *) ptr);233}234235static inline uint32_t bt_get_le32(const void *ptr)236{237return bswap_32(bt_get_unaligned((const uint32_t *) ptr));238}239240static inline uint32_t bt_get_be32(const void *ptr)241{242return bt_get_unaligned((const uint32_t *) ptr);243}244245static inline uint16_t bt_get_le16(const void *ptr)246{247return bswap_16(bt_get_unaligned((const uint16_t *) ptr));248}249250static inline uint16_t bt_get_be16(const void *ptr)251{252return bt_get_unaligned((const uint16_t *) ptr);253}254255static inline void bt_put_le64(uint64_t val, const void *ptr)256{257bt_put_unaligned(bswap_64(val), (uint64_t *) ptr);258}259260static inline void bt_put_be64(uint64_t val, const void *ptr)261{262bt_put_unaligned(val, (uint64_t *) ptr);263}264265static inline void bt_put_le32(uint32_t val, const void *ptr)266{267bt_put_unaligned(bswap_32(val), (uint32_t *) ptr);268}269270static inline void bt_put_be32(uint32_t val, const void *ptr)271{272bt_put_unaligned(val, (uint32_t *) ptr);273}274275static inline void bt_put_le16(uint16_t val, const void *ptr)276{277bt_put_unaligned(bswap_16(val), (uint16_t *) ptr);278}279280static inline void bt_put_be16(uint16_t val, const void *ptr)281{282bt_put_unaligned(val, (uint16_t *) ptr);283}284#else285#error "Unknown byte order"286#endif287288/* BD Address */289typedef struct {290uint8_t b[6];291} __attribute__((packed)) bdaddr_t;292293/* BD Address type */294#define BDADDR_BREDR 0x00295#define BDADDR_LE_PUBLIC 0x01296#define BDADDR_LE_RANDOM 0x02297298#define BDADDR_ANY (&(bdaddr_t) {{0, 0, 0, 0, 0, 0}})299#define BDADDR_ALL (&(bdaddr_t) {{0xff, 0xff, 0xff, 0xff, 0xff, 0xff}})300#define BDADDR_LOCAL (&(bdaddr_t) {{0, 0, 0, 0xff, 0xff, 0xff}})301302/* Copy, swap, convert BD Address */303static inline int bacmp(const bdaddr_t *ba1, const bdaddr_t *ba2)304{305return memcmp(ba1, ba2, sizeof(bdaddr_t));306}307static inline void bacpy(bdaddr_t *dst, const bdaddr_t *src)308{309memcpy(dst, src, sizeof(bdaddr_t));310}311312void baswap(bdaddr_t *dst, const bdaddr_t *src);313bdaddr_t *strtoba(const char *str);314char *batostr(const bdaddr_t *ba);315int ba2str(const bdaddr_t *ba, char *str);316int str2ba(const char *str, bdaddr_t *ba);317int ba2oui(const bdaddr_t *ba, char *oui);318int bachk(const char *str);319320int baprintf(const char *format, ...);321int bafprintf(FILE *stream, const char *format, ...);322int basprintf(char *str, const char *format, ...);323int basnprintf(char *str, size_t size, const char *format, ...);324325void *bt_malloc(size_t size);326void bt_free(void *ptr);327328int bt_error(uint16_t code);329const char *bt_compidtostr(int id);330331typedef struct {332uint8_t data[16];333} uint128_t;334335#if __BYTE_ORDER == __BIG_ENDIAN336337#define ntoh64(x) (x)338339static inline void ntoh128(const uint128_t *src, uint128_t *dst)340{341memcpy(dst, src, sizeof(uint128_t));342}343344static inline void btoh128(const uint128_t *src, uint128_t *dst)345{346int i;347348for (i = 0; i < 16; i++)349dst->data[15 - i] = src->data[i];350}351352#else353354static inline uint64_t ntoh64(uint64_t n)355{356uint64_t h;357uint64_t tmp = ntohl(n & 0x00000000ffffffff);358359h = ntohl(n >> 32);360h |= tmp << 32;361362return h;363}364365static inline void ntoh128(const uint128_t *src, uint128_t *dst)366{367int i;368369for (i = 0; i < 16; i++)370dst->data[15 - i] = src->data[i];371}372373static inline void btoh128(const uint128_t *src, uint128_t *dst)374{375memcpy(dst, src, sizeof(uint128_t));376}377378#endif379380#define hton64(x) ntoh64(x)381#define hton128(x, y) ntoh128(x, y)382#define htob128(x, y) btoh128(x, y)383384#ifdef __cplusplus385}386#endif387388#endif /* __BLUETOOTH_H */389390391