bluez/lib/bluetooth.h

316 lines
7.1 KiB
C
Raw Normal View History

2002-03-09 05:10:06 +08:00
/*
2004-04-03 13:11:38 +08:00
*
* BlueZ - Bluetooth protocol stack for Linux
*
* Copyright (C) 2000-2001 Qualcomm Incorporated
* Copyright (C) 2002-2003 Maxim Krasnyansky <maxk@qualcomm.com>
2010-01-02 09:08:17 +08:00
* Copyright (C) 2002-2010 Marcel Holtmann <marcel@holtmann.org>
2004-04-03 13:11:38 +08:00
*
*
* This program is free software; you can redistribute it and/or modify
2005-10-30 03:25:42 +08:00
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
2004-04-03 13:11:38 +08:00
*
2005-10-30 03:25:42 +08:00
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
2004-04-03 13:11:38 +08:00
*
2005-10-30 03:25:42 +08:00
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2004-04-03 13:11:38 +08:00
*
2002-03-09 05:10:06 +08:00
*/
#ifndef __BLUETOOTH_H
#define __BLUETOOTH_H
#ifdef __cplusplus
extern "C" {
#endif
2005-04-20 20:23:13 +08:00
#include <stdio.h>
2002-03-09 05:10:06 +08:00
#include <stdint.h>
2005-03-27 22:37:59 +08:00
#include <string.h>
2002-03-09 05:10:06 +08:00
#include <endian.h>
2002-03-13 02:58:39 +08:00
#include <byteswap.h>
#include <netinet/in.h>
2003-07-03 08:06:26 +08:00
2002-03-09 05:10:06 +08:00
#ifndef AF_BLUETOOTH
#define AF_BLUETOOTH 31
#define PF_BLUETOOTH AF_BLUETOOTH
#endif
2003-07-03 08:06:26 +08:00
#define BTPROTO_L2CAP 0
#define BTPROTO_HCI 1
#define BTPROTO_SCO 2
2002-03-09 05:10:06 +08:00
#define BTPROTO_RFCOMM 3
2002-05-07 04:54:32 +08:00
#define BTPROTO_BNEP 4
2003-06-05 14:38:18 +08:00
#define BTPROTO_CMTP 5
2004-03-09 23:39:56 +08:00
#define BTPROTO_HIDP 6
2004-07-04 23:38:11 +08:00
#define BTPROTO_AVDTP 7
2002-03-09 05:10:06 +08:00
2003-07-03 08:06:26 +08:00
#define SOL_HCI 0
#define SOL_L2CAP 6
#define SOL_SCO 17
#define SOL_RFCOMM 18
2002-03-09 05:10:06 +08:00
#ifndef SOL_BLUETOOTH
#define SOL_BLUETOOTH 274
#endif
#define BT_SECURITY 4
struct bt_security {
uint8_t level;
uint8_t key_size;
};
#define BT_SECURITY_SDP 0
#define BT_SECURITY_LOW 1
#define BT_SECURITY_MEDIUM 2
#define BT_SECURITY_HIGH 3
#define BT_DEFER_SETUP 7
#define BT_FLUSHABLE 8
#define BT_FLUSHABLE_OFF 0
#define BT_FLUSHABLE_ON 1
#define BT_CHANNEL_POLICY 10
/* BR/EDR only (default policy)
* AMP controllers cannot be used.
* Channel move requests from the remote device are denied.
* If the L2CAP channel is currently using AMP, move the channel to BR/EDR.
*/
#define BT_CHANNEL_POLICY_BREDR_ONLY 0
/* BR/EDR Preferred
* Allow use of AMP controllers.
* If the L2CAP channel is currently on AMP, move it to BR/EDR.
* Channel move requests from the remote device are allowed.
*/
#define BT_CHANNEL_POLICY_BREDR_PREFERRED 1
/* AMP Preferred
* Allow use of AMP controllers
* If the L2CAP channel is currently on BR/EDR and AMP controller
* resources are available, initiate a channel move to AMP.
* Channel move requests from the remote device are allowed.
* If the L2CAP socket has not been connected yet, try to create
* and configure the channel directly on an AMP controller rather
* than BR/EDR.
*/
#define BT_CHANNEL_POLICY_AMP_PREFERRED 2
2002-03-09 05:10:06 +08:00
/* Connection and socket states */
enum {
BT_CONNECTED = 1, /* Equal to TCP_ESTABLISHED to make net code happy */
BT_OPEN,
BT_BOUND,
BT_LISTEN,
BT_CONNECT,
BT_CONNECT2,
BT_CONFIG,
BT_DISCONN,
BT_CLOSED
};
/* Byte order conversions */
#if __BYTE_ORDER == __LITTLE_ENDIAN
#define htobs(d) (d)
#define htobl(d) (d)
#define htobll(d) (d)
2002-03-09 05:10:06 +08:00
#define btohs(d) (d)
#define btohl(d) (d)
#define btohll(d) (d)
2002-03-09 05:10:06 +08:00
#elif __BYTE_ORDER == __BIG_ENDIAN
#define htobs(d) bswap_16(d)
#define htobl(d) bswap_32(d)
#define htobll(d) bswap_64(d)
2002-03-09 05:10:06 +08:00
#define btohs(d) bswap_16(d)
#define btohl(d) bswap_32(d)
#define btohll(d) bswap_64(d)
2002-03-09 05:10:06 +08:00
#else
#error "Unknown byte order"
#endif
2004-04-03 16:25:58 +08:00
/* Bluetooth unaligned access */
2005-01-21 02:14:19 +08:00
#define bt_get_unaligned(ptr) \
({ \
struct __attribute__((packed)) { \
typeof(*(ptr)) __v; \
2005-01-23 22:36:19 +08:00
} *__p = (void *) (ptr); \
2005-01-21 02:14:19 +08:00
__p->__v; \
})
#define bt_put_unaligned(val, ptr) \
do { \
struct __attribute__((packed)) { \
typeof(*(ptr)) __v; \
2005-01-23 22:36:19 +08:00
} *__p = (void *) (ptr); \
2005-01-21 02:14:19 +08:00
__p->__v = (val); \
} while(0)
2004-04-03 16:25:58 +08:00
#if __BYTE_ORDER == __LITTLE_ENDIAN
static inline uint64_t bt_get_le64(void *ptr)
{
return bt_get_unaligned((uint64_t *) ptr);
}
static inline uint64_t bt_get_be64(void *ptr)
{
return bswap_64(bt_get_unaligned((uint64_t *) ptr));
}
static inline uint32_t bt_get_le32(void *ptr)
{
return bt_get_unaligned((uint32_t *) ptr);
}
static inline uint32_t bt_get_be32(void *ptr)
{
return bswap_32(bt_get_unaligned((uint32_t *) ptr));
}
static inline uint16_t bt_get_le16(void *ptr)
{
return bt_get_unaligned((uint16_t *) ptr);
}
static inline uint16_t bt_get_be16(void *ptr)
{
return bswap_16(bt_get_unaligned((uint16_t *) ptr));
}
#elif __BYTE_ORDER == __BIG_ENDIAN
static inline uint64_t bt_get_le64(void *ptr)
{
return bswap_64(bt_get_unaligned((uint64_t *) ptr));
}
static inline uint64_t bt_get_be64(void *ptr)
{
return bt_get_unaligned((uint64_t *) ptr);
}
static inline uint32_t bt_get_le32(void *ptr)
{
return bswap_32(bt_get_unaligned((uint32_t *) ptr));
}
static inline uint32_t bt_get_be32(void *ptr)
{
return bt_get_unaligned((uint32_t *) ptr);
}
static inline uint16_t bt_get_le16(void *ptr)
{
return bswap_16(bt_get_unaligned((uint16_t *) ptr));
}
static inline uint16_t bt_get_be16(void *ptr)
{
return bt_get_unaligned((uint16_t *) ptr);
}
#else
#error "Unknown byte order"
#endif
2002-03-09 05:10:06 +08:00
/* BD Address */
typedef struct {
uint8_t b[6];
} __attribute__((packed)) bdaddr_t;
2002-03-31 13:14:51 +08:00
#define BDADDR_ANY (&(bdaddr_t) {{0, 0, 0, 0, 0, 0}})
2004-05-09 03:52:55 +08:00
#define BDADDR_ALL (&(bdaddr_t) {{0xff, 0xff, 0xff, 0xff, 0xff, 0xff}})
2002-03-31 13:14:51 +08:00
#define BDADDR_LOCAL (&(bdaddr_t) {{0, 0, 0, 0xff, 0xff, 0xff}})
2002-03-09 05:10:06 +08:00
/* Copy, swap, convert BD Address */
static inline int bacmp(const bdaddr_t *ba1, const bdaddr_t *ba2)
2002-03-09 05:10:06 +08:00
{
return memcmp(ba1, ba2, sizeof(bdaddr_t));
}
static inline void bacpy(bdaddr_t *dst, const bdaddr_t *src)
2002-03-09 05:10:06 +08:00
{
memcpy(dst, src, sizeof(bdaddr_t));
}
void baswap(bdaddr_t *dst, const bdaddr_t *src);
bdaddr_t *strtoba(const char *str);
2004-04-03 13:11:38 +08:00
char *batostr(const bdaddr_t *ba);
int ba2str(const bdaddr_t *ba, char *str);
int str2ba(const char *str, bdaddr_t *ba);
2005-03-27 22:43:44 +08:00
int ba2oui(const bdaddr_t *ba, char *oui);
2007-06-22 05:18:00 +08:00
int bachk(const char *str);
2002-03-09 05:10:06 +08:00
2005-03-27 22:37:59 +08:00
int baprintf(const char *format, ...);
int bafprintf(FILE *stream, const char *format, ...);
int basprintf(char *str, const char *format, ...);
int basnprintf(char *str, size_t size, const char *format, ...);
void *bt_malloc(size_t size);
void bt_free(void *ptr);
2004-04-03 13:11:38 +08:00
int bt_error(uint16_t code);
char *bt_compidtostr(int id);
2002-03-09 05:10:06 +08:00
typedef struct {
uint8_t data[16];
} uint128_t;
#if __BYTE_ORDER == __BIG_ENDIAN
#define ntoh64(x) (x)
static inline void ntoh128(const uint128_t *src, uint128_t *dst)
{
memcpy(dst, src, sizeof(uint128_t));
}
static inline void btoh128(const uint128_t *src, uint128_t *dst)
{
int i;
for (i = 0; i < 16; i++)
dst->data[15 - i] = src->data[i];
}
#else
static inline uint64_t ntoh64(uint64_t n)
{
uint64_t h;
uint64_t tmp = ntohl(n & 0x00000000ffffffff);
h = ntohl(n >> 32);
h |= tmp << 32;
return h;
}
static inline void ntoh128(const uint128_t *src, uint128_t *dst)
{
int i;
for (i = 0; i < 16; i++)
dst->data[15 - i] = src->data[i];
}
static inline void btoh128(const uint128_t *src, uint128_t *dst)
{
memcpy(dst, src, sizeof(uint128_t));
}
#endif
#define hton64(x) ntoh64(x)
#define hton128(x, y) ntoh128(x, y)
#define htob128(x, y) btoh128(x, y)
2002-03-09 05:10:06 +08:00
#ifdef __cplusplus
}
#endif
#endif /* __BLUETOOTH_H */