#
#  BluetoothAutomation.py
#
#  Created on Shannon Ma on 2/1/2011.
#  Copyright (c) 2011 Apple Inc. All rights reserved.

import objc, sys, os

from ._bundle import BAM_PATH


try:
    FRAMEWORKNAME = "BluetoothAutomation"
    __bundle__ = objc.initFrameworkWrapper('bluetoothautomation',
           frameworkIdentifier='com.apple.bluetoothautomation',
           frameworkPath=objc.pathForFramework(BAM_PATH),
           globals=globals())
except ImportError as e:
    raise ImportError('{}. {} does not exist. Please Update HomeDiagnostics.'.format(e, BAM_PATH))

bridgesupportfile = __bundle__.pathForResource_ofType_inDirectory_(FRAMEWORKNAME, 'bridgesupport', 'BridgeSupport')
try:
    if os.path.exists(bridgesupportfile):
        f = open(bridgesupportfile, 'r')
        xmldata = f.read()
        f.close()
        objc.parseBridgeSupport(xmldata, globals(), FRAMEWORKNAME)
except:
    sys.stderr.write("Warning: %s looks unparsable or doesn't exist - ignoring.\n" % (FRAMEWORKNAME + '.bridgesupport'))

del objc, sys, os

# Device properties
kBAMDeviceLocation = "kBAMDeviceLocation"
kBAMDeviceName = "kBAMDeviceName"
kBAMDevicePath = "kBAMDevicePath"
kBAMDeviceEntryID = "kBAMDeviceEntryID"
kBAMTestKey = "kBAMTestKey"
kBAMDirty = "kBAMDirty"

# Bluetooth services

kBAMServiceNone =           0x00000000
kBAMServiceHFP =            0x00000001
kBAMServicePBAP =           0x00000002
kBAMServiceAVRCP =          0x00000008
kBAMServiceA2DP =           0x00000010
kBAMServiceHID =            0x00000020
kBAMServiceLE =             0x00008000
kBAMServiceLEA =            0x00010000
kBAMServiceLETime =         0x00020000
kBAMServiceLEMediaControl = 0x00040000
kBAMServiceLEANCS =         0x00080000
kBAMServiceIAP =            0x00100000
kBAMServiceAll =            0xFFFFFFFFFFFFFFFF

# Verbose logging options

kBAMLogNone = 0x00
kBAMLogA2DP = 0x01
kBAMLogAVRCP = 0x02
kBAMLogCore = 0x04
kBAMLogHFP = 0x08
kBAMLogHID = 0x10
kBAMLogLE = 0x20
kBAMLogPBAP = 0x40
kBAMLogAll = 0xFF

# HID channels

kBAMHIDChannelControl = 0x0011
kBAMHIDChannelInterrupt = 0x0013

# HID report types

kBAMHIDReportOther = 0
kBAMHIDReportInput = 1
kBAMHIDReportOutput = 2
kBAMHIDReportFeature = 3
kBAMHIDReportTypeMask = 0x00000001 | 0x00000002
kBAMHIDReportBufferSizePresent = 0x00000008

# v2.1+EDR, SSP IO Capabilities response/reply
# SSP input capability

kBAMIOCapDisplayOnly = 0x00
kBAMIOCapDisplayYesNo = 0x01
kBAMIOCapKeyboardOnly = 0x02
kBAMIOCapNoInNoOut = 0x03

# HID key modifiers
kBAMHIDLeftCtrl = 0x01
kBAMHIDRightCtrl = 0x10
kBAMHIDLeftShift = 0x02
kBAMHIDRightShift = 0x20
kBAMHIDLeftAlt = 0x04
kBAMHIDRightAlt = 0x40
kBAMHIDLeftCmd = 0x08
kBAMHIDRightCmd = 0x80

# AT response keys

kBAMATResponseCommand = "kBAMATResponseCommand"
kBAMATResponseParameters = "kBAMATResponseParameters"

# Device roles

kBAMDeviceRoleMaster, kBAMDeviceRoleSlave = range(2)

# Device modes

kBAMDeviceModeActive, kBAMDeviceModeHold, kBAMDeviceModeSniff, kBAMDeviceModePark = range(4)

# LE addresses

kBAMLEAddressPublic, kBAMLEAddressStatic, kBAMLEAddressPrivateNonResolvable, kBAMLEAddressPrivateResolvable = range(4)

# LE advertising modes

kBAMLEAdModeConnectableUndirected, kBAMLEAdModeConnectableDirected, kBAMLEAdModeDiscoverableUndirected, kBAMLEAdModeNonConnectableUndirected = range(4)

# LE input capabilities

kBAMLEInputCapabilityNone, kBAMLEInputCapabilityYesNo, kBAMLEInputCapabilityKeyboard = range(3)

# LE output capabilities

kBAMLEOutputCapabilityNone, kBAMLEOutputCapabilityDisplay = range(2)

# LE characteristic properties

kBAMLECharacteristicPropertyBroadcast = 0x01
kBAMLECharacteristicPropertyRead = 0x02
kBAMLECharacteristicPropertyWriteNoResponse = 0x04
kBAMLECharacteristicPropertyWrite = 0x08
kBAMLECharacteristicPropertyNotify = 0x10
kBAMLECharacteristicPropertyIndicate = 0x20
kBAMLECharacteristicPropertySignedWrites = 0x40
kBAMLECharacteristicPropertyExtended = 0x80
kBAMLECharacteristicPropertyNotifyEncryptionRequired = 0x100
kBAMLECharacteristicPropertyIndicateEncryptionRequired = 0x200


# LE service characteristic extended properties

kBAMLECharacteristicExPropertyReliableWrite = 0x0001
kBAMLECharacteristicExPropertyWritableAux = 0x0002
kBAMLECharacteristicExPropertyReserved = 0xFFFC

# LE attribute security
kBAMLEAttributeSecurityNone = 0
kBAMLEAttributeSecurityReadable = 1 << 0
kBAMLEAttributeSecurityWritable = 1 << 1
kBAMLEAttributeSecurityReadAuthenticationRequired = 1 << 2
kBAMLEAttributeSecurityWriteAuthenticationRequired = 1 << 3
kBAMLEAttributeSecurityReadAuthorizationRequired = 1 << 4
kBAMLEAttributeSecurityWriteAuthorizationRequired = 1 << 5

#LE characteristic descriptor types

kBAMLEDescriptorExtendedProperties, kBAMLEDescriptorUserDescription, kBAMLEClientDescriptorConfiguration, kBAMLEServerDescriptorConfiguration, kBAMLEDescriptorCharacteristic_Format, kBAMLEDescriptorAggregate_Format, kBAMLEDescriptorCustomConfiguration = range(7)

# PBAP phonebooks

kBAMPBAPPhonebookMain, kBAMPBAPPhonebookIncomingCalls, kBAMPBAPPhonebookOutgoingCalls, kBAMPBAPPhonebookMissedCalls, kBAMPBAPPhonebookCombinedCalls, kBAMPBAPPhonebookInvalid = range(6)

# PBAP repositories

kBAMPBAPRepositoryLocal, kBAMPBAPRepositorySIM, kBAMPBAPRepositoryInvalid = range(3)

# PBAP vCard search keys

kBAMPBAPSearchName, kBAMPBAPSearchNumber, kBAMPBAPSearchSound = range(3)

# vCard properties
kBAMvCardIdentifier = "kBAMvCardIdentifier"
kBAMvCardName = "kBAMvCardName"
kBAMvCardNickname = "kBAMvCardNickname"
kBAMvCardBirthday = "kBAMvCardBirthday"
kBAMvCardAddressHome = "kBAMvCardAddressHome"
kBAMvCardAddressWork = "kBAMvCardAddressWork"
kBAMvCardPhoneHome = "kBAMvCardPhoneHome"
kBAMvCardPhoneWork = "kBAMvCardPhoneWork"
kBAMvCardPhoneOther = "kBAMvCardPhoneOther"
kBAMvCardEmailHome = "kBAMvCardEmailHome"
kBAMvCardEmailWork = "kBAMvCardEmailWork"
kBAMvCardTitle = "kBAMvCardTitle"
kBAMvCardDepartmentName = "kBAMvCardDepartmentName"
kBAMvCardOrganization = "kBAMvCardOrganization"
kBAMvCardNote = "kBAMvCardNote"

# AVRCP Meta Info
kBAMAVRCPMetaTitle = 0x01
kBAMAVRCPMetaArtist = 0x02
kBAMAVRCPMetaAlbum = 0x03
kBAMAVRCPMetaNumber = 0x04
kBAMAVRCPMetaTotal = 0x05
kBAMAVRCPMetaGenre = 0x06
kBAMAVRCPMetaTime = 0x07

# LE Time Profile Client Keys
kBAMLETimeProfileClientKeyCurrentTimeAdjustReason     = "kBAMLETimeProfileClientKeyCurrentTimeAdjustReason"
kBAMLETimeProfileClientKeyCurrentTimeDayOfWeek         ="kBAMLETimeProfileClientKeyCurrentTimeDayOfWeek"
kBAMLETimeProfileClientKeyCurrentTimeYear             ="kBAMLETimeProfileClientKeyCurrentTimeYear"
kBAMLETimeProfileClientKeyCurrentTimeMonth             ="kBAMLETimeProfileClientKeyCurrentTimeMonth"
kBAMLETimeProfileClientKeyCurrentTimeDay             ="kBAMLETimeProfileClientKeyCurrentTimeDay"
kBAMLETimeProfileClientKeyCurrentTimeHours             ="kBAMLETimeProfileClientKeyCurrentTimeHours"
kBAMLETimeProfileClientKeyCurrentTimeMinutes         ="kBAMLETimeProfileClientKeyCurrentTimeMinutes"
kBAMLETimeProfileClientKeyCurrentTimeSeconds         ="kBAMLETimeProfileClientKeyCurrentTimeSeconds"

# LE Service UUIDs
kBAMUUIDTimeService = "1805"
kBAMUUIDMediaControlService = "ACDC"
kBAMUUIDAppleNotificationCenterService = "7905F431-B5CE-4E99-A40F-4B1E122D00D0"

# ANCS
kBAMANCSNotificationSourceKeyEventID = "EventID"
kBAMANCSNotificationSourceKeyEventFlags = "EventFlags"
kBAMANCSNotificationSourceKeyCategoryID = "CategoryID"
kBAMANCSNotificationSourceKeyCategoryCount = "CategoryCount"
kBAMANCSNotificationSourceKeyUID = "UID"
kBAMANCSNotificationAttributeKeyObject = "Object"
kBAMANCSNotificationAttributeKeyAppID = "ApplicationIdentifier"
kBAMANCSNotificationAttributeKeyDate = "Date"
kBAMANCSNotificationAttributeKeySubtitle = "Subtitle"
kBAMANCSNotificationAttributeKeyTitle = "Title"
kBAMANCSNotificationAttributeKeyMessage = "Message"
kBAMANCSNotificationAttributeKeyMessageSize = "MessageSize"
kBAMANCSApplicationAttributeKeyDisplayName = "DisplayName"

# LEA
kBAMLeaCodecInvalid                     = 0
kBAMleaCodecG711                        = 1
kBAMleaCodecG726                        = 2
kBAMleaCodecG722                        = 3
kBAMleaCodecG722Mode3                   = 4
kBAMleaCodecAACELD16                    = 5
kBAMleaCodecAACELD24                    = 6
kBAMleaCodecAACELD24_1                  = 7
kBAMleaCodecSPEEX                       = 8
kBAMleaCodecHEIMDALL1                   = 9
kBAMleaCodecHEIMDALL2                   = 10
kBAMleaCodecHEIMDALL3                   = 11
kBAMleaCodecHEIMDALL4                   = 12
kBAMleaCodecHEIMDALL0                   = 13
kBAMleaCodecG722Mode3_8KHz              = 14
