Skip to content

Commit

Permalink
aics: notify suscribers when desired
Browse files Browse the repository at this point in the history
  • Loading branch information
SilverBzH committed Sep 6, 2024
1 parent 022513e commit 8462252
Showing 1 changed file with 30 additions and 21 deletions.
51 changes: 30 additions & 21 deletions bumble/profiles/aics.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,11 @@ def increment_change_counter(self):
if self.change_counter > CHANGE_COUNTER_MAX_VALUE:
self.change_counter = 0x00

async def notify_subscribers(self, connection: Connection) -> None:
await connection.device.notify_subscribers(
attribute=self.value, value=self.__bytes__()
)

def _on_read(self, _connection: Optional[Connection]) -> bytes:
return self.__bytes__()

Expand Down Expand Up @@ -216,15 +221,19 @@ def __init__(

self.audio_input_status_value = audio_input_description_value

value = CharacteristicValue(read=self._on_read, write=self._on_write)
self.value = CharacteristicValue(read=self._on_read, write=self._on_write)

super().__init__(uuid, properties, permissions, value, descriptors)
super().__init__(uuid, properties, permissions, self.value, descriptors)

def _on_read(self, _connection: Optional[Connection]) -> bytes:
return self.audio_input_status_value.encode('utf-8')

def _on_write(self, _connection: Optional[Connection], value: bytes) -> None:
async def _on_write(self, connection: Optional[Connection], value: bytes) -> None:
self.audio_input_status_value = value.decode('utf-8')
if connection is not None:
await connection.device.notify_subscribers(
attribute=self.value, value=value
)


class AudioInputControlPoint(Characteristic):
Expand All @@ -245,14 +254,14 @@ def __init__(
self.audio_input_state = audio_input_state
self.gain_settings_properties = gain_settings_properties

value = CharacteristicValue(write=self._on_write)
self.value = CharacteristicValue(write=self._on_write)

super().__init__(uuid, properties, permissions, value, descriptors)
super().__init__(uuid, properties, permissions, self.value, descriptors)

def _on_write(self, connection: Optional[Connection], value: bytes) -> None:
async def _on_write(self, connection: Optional[Connection], value: bytes) -> None:
assert connection

def _set_gain_settings(gain_settings_operand: int) -> None:
async def _set_gain_settings(gain_settings_operand: int) -> None:
'''Cf. 3.5.2.1 Set Gain Settings Procedure'''

gain_mode = self.audio_input_state.gain_mode
Expand All @@ -274,9 +283,9 @@ def _set_gain_settings(gain_settings_operand: int) -> None:

if self.audio_input_state.gain_settings != gain_settings_operand:
self.audio_input_state.gain_settings = gain_settings_operand
pass # TODO: NOTIFY CLIENT
await self.audio_input_state.notify_subscribers(connection)

def _unmute():
async def _unmute():
'''Cf. 3.5.2.2 Unmute procedure'''

logger.error(f'unmute: {self.audio_input_state.mute}')
Expand All @@ -290,9 +299,9 @@ def _unmute():

self.audio_input_state.mute = Mute.NOT_MUTED
self.audio_input_state.increment_change_counter()
# TODO: NOTIFY CLIENT
await self.audio_input_state.notify_subscribers(connection)

def _mute() -> None:
async def _mute() -> None:
'''Cf. 3.5.5.2 Mute procedure'''

change_counter = self.audio_input_state.change_counter
Expand All @@ -310,9 +319,9 @@ def _mute() -> None:

self.audio_input_state.mute = Mute.MUTED
self.audio_input_state.increment_change_counter()
# TODO: NOTIFY CLIENT
await self.audio_input_state.notify_subscribers(connection)

def _set_manual_gain_mode() -> None:
async def _set_manual_gain_mode() -> None:
'''Cf. 3.5.2.4 Set Manual Gain Mode procedure'''

gain_mode = self.audio_input_state.gain_mode
Expand All @@ -325,9 +334,9 @@ def _set_manual_gain_mode() -> None:

self.audio_input_state.gain_mode = GainMode.MANUAL
self.audio_input_state.increment_change_counter()
# TODO: Notify client
await self.audio_input_state.notify_subscribers(connection)

def _set_automatic_gain_mode() -> None:
async def _set_automatic_gain_mode() -> None:
'''Cf. 3.5.2.5 Set Automatic Gain Mode'''

gain_mode = self.audio_input_state.gain_mode
Expand All @@ -340,23 +349,23 @@ def _set_automatic_gain_mode() -> None:

self.audio_input_state.gain_mode = GainMode.AUTOMATIC
self.audio_input_state.increment_change_counter()
# TODO: NOTIFY CLIENT
await self.audio_input_state.notify_subscribers(connection)

try:
opcode = AudioInputControlPointOpCode(value[0])

opcode = AudioInputControlPointOpCode(value[0])

if opcode == AudioInputControlPointOpCode.SET_GAIN_SETTING:
_set_gain_settings(value[2])
await _set_gain_settings(value[2])
elif opcode == AudioInputControlPointOpCode.UNMUTE:
_unmute()
await _unmute()
elif opcode == AudioInputControlPointOpCode.MUTE:
_mute()
await _mute()
elif opcode == AudioInputControlPointOpCode.SET_MANUAL_GAIN_MODE:
_set_manual_gain_mode()
await _set_manual_gain_mode()
elif opcode == AudioInputControlPointOpCode.SET_AUTOMATIC_GAIN_MODE:
_set_automatic_gain_mode()
await _set_automatic_gain_mode()

except ValueError as e:
logger.error(f"OpCode value is incorrect: {e}")
Expand Down

0 comments on commit 8462252

Please sign in to comment.