#!/usr/bin/python

# Author:  Wayne Keenan
# email:   wayne@thebubbleworks.com
# Twitter: https://twitter.com/wkeenan



HCIPY_HCI_CMD_STRUCT_HEADER =  "<BHB"
HCIPY_HCI_FILTER_STRUCT = "<LLLH"

# HCI ioctl Commands:
HCIDEVUP = 0x400448c9  # 201
HCIDEVDOWN = 0x400448ca  # 202
HCIGETDEVINFO = -2147202861 #0x800448d3L  # _IOR(ord('H'), 211, 4)


# HCI Status

HCI_SUCCESS = 0x00
HCI_OE_USER_ENDED_CONNECTION = 0x13



# HCI Parameters

LE_PUBLIC_ADDRESS = 0x00
LE_RANDOM_ADDRESS = 0x01

# Types of bluetooth scan

SCAN_TYPE_PASSIVE = 0x00
SCAN_TYPE_ACTIVE  = 0x01
SCAN_FILTER_DUPLICATES = 0x01
SCAN_DISABLE = 0x00
SCAN_ENABLE = 0x01


# Advertisement event types
ADV_IND = 0x00
ADV_DIRECT_IND = 0x01
ADV_SCAN_IND = 0x02
ADV_NONCONN_IND = 0x03
ADV_SCAN_RSP = 0x04

FILTER_POLICY_NO_WHITELIST = 0x00  # Allow Scan Request from Any, Connect Request from Any
FILTER_POLICY_SCAN_WHITELIST = 0x01  # Allow Scan Request from White List Only, Connect Request from Any
FILTER_POLICY_CONN_WHITELIST = 0x02  # Allow Scan Request from Any, Connect Request from White List Only
FILTER_POLICY_SCAN_AND_CONN_WHITELIST = 0x03  # Allow Scan Request from White List Only, Connect Request from White List Only


# From bluetootchsocket.cpp

HCI_COMMAND_PKT = 0x01
HCI_ACLDATA_PKT = 0x02
HCI_EVENT_PKT = 0x04

EVT_CMD_COMPLETE = 0x0e
EVT_CMD_STATUS = 0x0f


LE_META_EVENT = 0x3E                # Core_4.2.pdf section: 7.7.65 LE Meta Event


# sub-events of LE_META_EVENT
EVT_LE_CONN_COMPLETE = 0x01

EVT_LE_ADVERTISING_REPORT = 0x02
EVT_LE_CONN_UPDATE_COMPLETE = 0x03
EVT_LE_READ_REMOTE_USED_FEATURES_COMPLETE = 0x04
EVT_DISCONN_COMPLETE = 0x05
EVT_LE_META_EVENT = 0x3e


ATT_CID = 0x0004


ACL_START = 0x02


OGF_LE_CTL = 0x08
OGF_LINK_CTL = 0x01

OCF_LE_SET_SCAN_PARAMETERS = 0x000B
OCF_LE_SET_SCAN_ENABLE = 0x000C
OCF_LE_CREATE_CONN = 0x000D
OCF_LE_SET_ADVERTISING_PARAMETERS = 0x0006
OCF_LE_SET_ADVERTISE_ENABLE = 0x000A
OCF_LE_SET_ADVERTISING_DATA = 0x0008
OCF_LE_SET_SCAN_RESPONSE_DATA = 0x0009
OCF_DISCONNECT = 0x0006

LE_SET_SCAN_PARAMETERS_CMD = OCF_LE_SET_SCAN_PARAMETERS | OGF_LE_CTL << 10
LE_SET_SCAN_ENABLE_CMD = OCF_LE_SET_SCAN_ENABLE | OGF_LE_CTL << 10
LE_SET_ADVERTISING_PARAMETERS_CMD = OCF_LE_SET_ADVERTISING_PARAMETERS | OGF_LE_CTL << 10
LE_SET_ADVERTISING_DATA_CMD = OCF_LE_SET_ADVERTISING_DATA | OGF_LE_CTL << 10
LE_SET_SCAN_RESPONSE_DATA_CMD = OCF_LE_SET_SCAN_RESPONSE_DATA | OGF_LE_CTL << 10
LE_SET_ADVERTISE_ENABLE_CMD = OCF_LE_SET_ADVERTISE_ENABLE | OGF_LE_CTL << 10

LE_CREATE_CONN_CMD = OCF_LE_CREATE_CONN | OGF_LE_CTL << 10
DISCONNECT_CMD = OCF_DISCONNECT | OGF_LINK_CTL << 10