mirror of
https://github.com/kavishdevar/librepods.git
synced 2026-03-01 20:23:42 +00:00
move everything to linux folder
This commit is contained in:
25
linux/aln/Notifications/ANC.py
Normal file
25
linux/aln/Notifications/ANC.py
Normal file
@@ -0,0 +1,25 @@
|
||||
from ..enums import enums
|
||||
from ..Capabilites import Capabilites
|
||||
class ANCNotification:
|
||||
NOTIFICATION_PREFIX = enums.NOISE_CANCELLATION_PREFIX
|
||||
OFF = Capabilites.NoiseCancellation.OFF
|
||||
ON = Capabilites.NoiseCancellation.ON
|
||||
TRANSPARENCY = Capabilites.NoiseCancellation.TRANSPARENCY
|
||||
ADAPTIVE = Capabilites.NoiseCancellation.ADAPTIVE
|
||||
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
def isANCData(self, data: bytes):
|
||||
# 04 00 04 00 09 00 0D 01 00 00 00
|
||||
if len(data) != 11:
|
||||
return False
|
||||
|
||||
if data.hex().startswith(self.NOTIFICATION_PREFIX.hex()):
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
def setData(self, data: bytes):
|
||||
self.status = data[7]
|
||||
pass
|
||||
64
linux/aln/Notifications/Battery.py
Normal file
64
linux/aln/Notifications/Battery.py
Normal file
@@ -0,0 +1,64 @@
|
||||
class BatteryComponent:
|
||||
LEFT = 4
|
||||
RIGHT = 2
|
||||
CASE = 8
|
||||
pass
|
||||
class BatteryStatus:
|
||||
CHARGING = 1
|
||||
NOT_CHARGING = 2
|
||||
DISCONNECTED = 4
|
||||
pass
|
||||
|
||||
class Battery:
|
||||
|
||||
def get_name(self, cls, value):
|
||||
for key, val in cls.__dict__.items():
|
||||
if val == value:
|
||||
return key
|
||||
return None
|
||||
|
||||
def get_component(self):
|
||||
return self.get_name(BatteryComponent, self.component)
|
||||
|
||||
def get_level(self):
|
||||
return self.level
|
||||
|
||||
def get_status(self):
|
||||
return self.get_name(BatteryStatus, self.status)
|
||||
|
||||
def __init__(self, component: int, level: int, status: int):
|
||||
self.component = component
|
||||
self.level = level
|
||||
self.status = status
|
||||
pass
|
||||
|
||||
class BatteryNotification:
|
||||
def __init__(self):
|
||||
self.first = Battery(BatteryComponent.LEFT, 0, BatteryStatus.DISCONNECTED)
|
||||
self.second = Battery(BatteryComponent.RIGHT, 0, BatteryStatus.DISCONNECTED)
|
||||
self.case = Battery(BatteryComponent.CASE, 0, BatteryStatus.DISCONNECTED)
|
||||
pass
|
||||
|
||||
def isBatteryData(self, data):
|
||||
if len(data) != 22:
|
||||
return False
|
||||
if data[0] == 0x04 and data[1] == 0x00 and data[2] == 0x04 and data[3] == 0x00 and data[4] == 0x04 and data[5] == 0x00:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
def setBattery(self, data):
|
||||
self.count = data[6]
|
||||
self.first = Battery(data[7], data[9], data[10])
|
||||
self.second = Battery(data[12], data[14], data[15])
|
||||
self.case = Battery(data[17], data[19], data[20])
|
||||
pass
|
||||
|
||||
def getBattery(self):
|
||||
if self.first.component == BatteryComponent.LEFT:
|
||||
self.left = self.first
|
||||
self.right = self.second
|
||||
else:
|
||||
self.left = self.second
|
||||
self.right = self.first
|
||||
self.case = self.case
|
||||
return [self.left, self.right, self.case]
|
||||
19
linux/aln/Notifications/ConversationalAwareness.py
Normal file
19
linux/aln/Notifications/ConversationalAwareness.py
Normal file
@@ -0,0 +1,19 @@
|
||||
# 04 00 04 00 4b 00 02 00 01 [level]
|
||||
|
||||
from ..enums import enums
|
||||
|
||||
class ConversationalAwarenessNotification:
|
||||
NOTIFICATION_PREFIX = enums.CONVERSATION_AWARENESS_RECEIVE_PREFIX
|
||||
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
def isConversationalAwarenessData(self, data: bytes):
|
||||
if len(data) != 10:
|
||||
return False
|
||||
if data.hex().startswith(self.NOTIFICATION_PREFIX.hex()):
|
||||
return True
|
||||
|
||||
def setData(self, data: bytes):
|
||||
self.status = data[9]
|
||||
pass
|
||||
26
linux/aln/Notifications/EarDetection.py
Normal file
26
linux/aln/Notifications/EarDetection.py
Normal file
@@ -0,0 +1,26 @@
|
||||
from ..Capabilites import Capabilites
|
||||
from ..enums import enums
|
||||
from typing import Literal
|
||||
|
||||
class EarDetectionNotification:
|
||||
NOTIFICATION_BIT = Capabilites.EAR_DETECTION
|
||||
NOTIFICATION_PREFIX = enums.PREFIX + NOTIFICATION_BIT
|
||||
IN_EAR = 0x00
|
||||
OUT_OF_EAR = 0x01
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
def isEarDetectionData(self, data: bytes):
|
||||
if len(data) != 8:
|
||||
return False
|
||||
if data.hex().startswith(self.NOTIFICATION_PREFIX.hex()):
|
||||
return True
|
||||
|
||||
def setEarDetection(self, data: bytes):
|
||||
self.first = data[6]
|
||||
self.second = data[7]
|
||||
|
||||
def getEarDetection(self):
|
||||
return [self.first, self.second]
|
||||
pass
|
||||
|
||||
58
linux/aln/Notifications/Listener.py
Normal file
58
linux/aln/Notifications/Listener.py
Normal file
@@ -0,0 +1,58 @@
|
||||
from bluetooth import BluetoothSocket
|
||||
import threading
|
||||
from .Battery import BatteryNotification
|
||||
from .EarDetection import EarDetectionNotification
|
||||
from .ConversationalAwareness import ConversationalAwarenessNotification
|
||||
from .ANC import ANCNotification
|
||||
import logging
|
||||
|
||||
logging = logging.getLogger(__name__)
|
||||
|
||||
class NotificationListener:
|
||||
BATTERY_UPDATED = 0x01
|
||||
ANC_UPDATED = 0x02
|
||||
EAR_DETECTION_UPDATED = 0x03
|
||||
CA_UPDATED = 0x04
|
||||
UNKNOWN = 0x00
|
||||
|
||||
def __init__(self, socket: BluetoothSocket, callback: callable):
|
||||
self.socket = socket
|
||||
self.BatteryNotification = BatteryNotification()
|
||||
self.EarDetectionNotification = EarDetectionNotification()
|
||||
self.ANCNotification = ANCNotification()
|
||||
self.ConversationalAwarenessNotification = ConversationalAwarenessNotification()
|
||||
self.callback = callback
|
||||
pass
|
||||
|
||||
def __start(self):
|
||||
while True:
|
||||
data = self.socket.recv(1024)
|
||||
if len(data) == 0:
|
||||
break
|
||||
if self.BatteryNotification.isBatteryData(data):
|
||||
self.BatteryNotification.setBattery(data)
|
||||
self.callback(self.BATTERY_UPDATED, data)
|
||||
pass
|
||||
if self.EarDetectionNotification.isEarDetectionData(data):
|
||||
self.EarDetectionNotification.setEarDetection(data)
|
||||
self.callback(self.EAR_DETECTION_UPDATED, data)
|
||||
if self.ANCNotification.isANCData(data):
|
||||
self.ANCNotification.setData(data)
|
||||
self.callback(self.ANC_UPDATED, data)
|
||||
if self.ConversationalAwarenessNotification.isConversationalAwarenessData(data):
|
||||
self.ConversationalAwarenessNotification.setData(data)
|
||||
self.callback(self.CA_UPDATED, data)
|
||||
else:
|
||||
self.callback(self.UNKNOWN, data)
|
||||
pass
|
||||
pass
|
||||
pass
|
||||
|
||||
def start(self):
|
||||
threading.Thread(target=self.__start).start()
|
||||
pass
|
||||
|
||||
def stop(self):
|
||||
self.socket.close()
|
||||
pass
|
||||
|
||||
40
linux/aln/Notifications/__init__.py
Normal file
40
linux/aln/Notifications/__init__.py
Normal file
@@ -0,0 +1,40 @@
|
||||
from .Listener import NotificationListener
|
||||
from ..enums import enums
|
||||
import bluetooth
|
||||
import logging
|
||||
|
||||
logging = logging.getLogger(__name__)
|
||||
|
||||
enums = enums()
|
||||
|
||||
class Notifications:
|
||||
BATTERY_UPDATED = NotificationListener.BATTERY_UPDATED
|
||||
ANC_UPDATED = NotificationListener.ANC_UPDATED
|
||||
CA_UPDATED = NotificationListener.CA_UPDATED
|
||||
EAR_DETECTION_UPDATED = NotificationListener.EAR_DETECTION_UPDATED
|
||||
UNKNOWN = NotificationListener.UNKNOWN
|
||||
def __init__(self, socket: bluetooth.BluetoothSocket, callback: callable):
|
||||
self.socket = socket
|
||||
self.notificationListener = NotificationListener(self.socket, callback)
|
||||
self.BatteryNotification = self.notificationListener.BatteryNotification
|
||||
self.EarDetectionNotification = self.notificationListener.EarDetectionNotification
|
||||
self.ANCNotification = self.notificationListener.ANCNotification
|
||||
self.ConversationalAwarenessNotification = self.notificationListener.ConversationalAwarenessNotification
|
||||
pass
|
||||
|
||||
def initialize(self):
|
||||
try:
|
||||
self.socket.send(enums.SET_SPECIFIC_FEATURES)
|
||||
self.socket.send(enums.REQUEST_NOTIFICATIONS)
|
||||
self.notificationListener.start()
|
||||
|
||||
except bluetooth.btcommon.BluetoothError as e:
|
||||
logging.error(f'Failed to send data to {self.mac_address}: {e}')
|
||||
return False
|
||||
return True
|
||||
|
||||
def __del__(self):
|
||||
self.notificationListener.stop()
|
||||
self.socket.close()
|
||||
pass
|
||||
pass
|
||||
Reference in New Issue
Block a user