Skip to content

Commit

Permalink
Move feature flags to model; fix string descriptions, add and update …
Browse files Browse the repository at this point in the history
…tests accordingly
  • Loading branch information
Robert Schulze committed Feb 3, 2022
1 parent bba054c commit 37ccf59
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 45 deletions.
5 changes: 3 additions & 2 deletions homekit/controller/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@
from homekit.exceptions import AccessoryNotFoundError, ConfigLoadingError, UnknownError, \
AuthenticationError, ConfigSavingError, AlreadyPairedError, TransportNotSupportedError, \
MalformedPinError, PairingAuthError
from homekit.protocol import States, Methods, Errors, TlvTypes, FeatureFlags
from homekit.protocol import States, Methods, Errors, TlvTypes
from homekit.http_impl import HomeKitHTTPConnection
from homekit.protocol.statuscodes import HapStatusCodes
from homekit.protocol import perform_pair_setup_part1, perform_pair_setup_part2, create_ip_pair_setup_write
from homekit.model.services.service_types import ServicesTypes
from homekit.model.characteristics.characteristic_types import CharacteristicsTypes
from homekit.model.feature_flags import FeatureFlags
from homekit.protocol.opcodes import HapBleOpCodes
from homekit.tools import IP_TRANSPORT_SUPPORTED, BLE_TRANSPORT_SUPPORTED
from homekit.controller.tools import NotSupportedPairing
Expand Down Expand Up @@ -353,7 +354,7 @@ def _get_pair_method(self, auth_method: PairingAuth, feature_flags: int):
pair_method = Methods.PairSetup

if auth_method == Controller.PairingAuth.Auto:
if feature_flags & FeatureFlags.AppleMFiCoprocessor:
if feature_flags & FeatureFlags.APPLE_MFI_COPROCESSOR:
pair_method = Methods.PairSetupWithAuth
elif auth_method == Controller.PairingAuth.HwAuth:
pair_method = Methods.PairSetupWithAuth
Expand Down
28 changes: 19 additions & 9 deletions homekit/model/feature_flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,33 @@
# limitations under the License.
#


class _FeatureFlags(object):
"""
Data taken form table 5-8 Bonjour TXT Record Feature Flags on page 69.
Data taken from table 5-8 Bonjour TXT Record Feature Flags on page 69.
"""

APPLE_MFI_COPROCESSOR = 0x01
SOFTWARE_MFI_AUTH = 0x02

def __init__(self):
self._data = {
0x00: 'No support for HAP Pairing',
0x01: 'Supports HAP Pairing with Apple authentication coprocessor',
0x02: 'Supports HAP Pairing with Software authentication',
0x00: 'No support for HAP Pairing', # this might also be uncertified
self.APPLE_MFI_COPROCESSOR: 'Apple authentication coprocessor',
self.SOFTWARE_MFI_AUTH: 'Software authentication',
}

def __getitem__(self, item):
bit_value = item & 0x01
if bit_value in self._data:
return self._data[bit_value]
def __getitem__(self, item: int) -> str:
data = []
if 0 != (item & self.APPLE_MFI_COPROCESSOR):
data.append(self._data[self.APPLE_MFI_COPROCESSOR])
if 0 != (item & self.SOFTWARE_MFI_AUTH):
data.append(self._data[self.SOFTWARE_MFI_AUTH])

if data:
return 'Supports HAP Pairing with ' + ' and '.join(data)
elif 0 == item:
# Note: this may change if feature flags will have more flags!
return self._data[0]

raise KeyError('Item {item} not found'.format(item=item))

Expand Down
1 change: 0 additions & 1 deletion homekit/protocol/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
from homekit.protocol.methods import Methods
from homekit.protocol.errors import Errors
from homekit.protocol.tlv_types import TlvTypes
from homekit.protocol.feature_flags import FeatureFlags
from homekit.exceptions import IncorrectPairingIdError, InvalidAuthTagError, InvalidSignatureError, UnavailableError, \
AuthenticationError, InvalidError, BusyError, MaxTriesError, MaxPeersError, BackoffError

Expand Down
20 changes: 0 additions & 20 deletions homekit/protocol/feature_flags.py

This file was deleted.

4 changes: 3 additions & 1 deletion tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
'TestServerData', 'BleCharacteristicFormatsTest', 'BleCharacteristicUnitsTest', 'CharacteristicTypesTest',
'TestBLEController', 'TestChacha20poly1305', 'TestCharacteristicsTypes', 'TestController', 'TestControllerIpPaired',
'TestControllerIpUnpaired', 'TestHttpResponse', 'TestHttpStatusCodes', 'TestMfrData', 'TestSrp',
'TestZeroconf', 'TestBLEPairing', 'TestServiceTypes', 'TestSecureHttp', 'TestHTTPPairing', 'TestSecureSession'
'TestZeroconf', 'TestBLEPairing', 'TestServiceTypes', 'TestSecureHttp', 'TestHTTPPairing', 'TestSecureSession',
'TestFeatureFlags'
]

from tests.bleCharacteristicFormats_test import BleCharacteristicFormatsTest
Expand All @@ -29,6 +30,7 @@
from tests.characteristicTypes_test import CharacteristicTypesTest
from tests.characteristicsTypes_test import TestCharacteristicsTypes
from tests.controller_test import TestControllerIpPaired, TestControllerIpUnpaired, TestController
from tests.feature_flags_test import TestFeatureFlags
from tests.httpStatusCodes_test import TestHttpStatusCodes
from tests.http_response_test import TestHttpResponse
from tests.regression_test import TestHTTPPairing, TestSecureSession
Expand Down
21 changes: 11 additions & 10 deletions tests/ble_controller_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
from homekit.model.characteristics import CharacteristicsTypes
from homekit.model.services import ServicesTypes, AbstractService, LightBulbService
from homekit.model.characteristics import AbstractCharacteristic
from homekit.protocol import States, Methods, TlvTypes, FeatureFlags
from homekit.model.feature_flags import FeatureFlags
from homekit.protocol import States, Methods, TlvTypes
from homekit.protocol.statuscodes import HapStatusCodes
from homekit import accessoryserver
from homekit.model import mixin as model_mixin
Expand Down Expand Up @@ -75,7 +76,7 @@ class Device:

connected = False

def __init__(self, accessory: Accessory, feature_flags: FeatureFlags = FeatureFlags.AppleMFiCoprocessor | FeatureFlags.SoftwareMFiAuth):
def __init__(self, accessory: Accessory, feature_flags: FeatureFlags = FeatureFlags.APPLE_MFI_COPROCESSOR | FeatureFlags.SOFTWARE_MFI_AUTH):
self.accessory = accessory
self.name = 'Test' # FIXME get from accessory
self.mac_address = '00:00:00:00:00'
Expand Down Expand Up @@ -164,7 +165,7 @@ def set_feature_flags(self, feature_flags):
if isinstance(char, PairingFeaturesCharacteristicHandler):
if feature_flags is None:
char.reset_feature_flags()
elif feature_flags > (FeatureFlags.AppleMFiCoprocessor | FeatureFlags.SoftwareMFiAuth):
elif feature_flags > (FeatureFlags.APPLE_MFI_COPROCESSOR | FeatureFlags.SOFTWARE_MFI_AUTH):
raise ValueError('"feature_flags": invalid value')
else:
char.feature_flags = feature_flags
Expand Down Expand Up @@ -572,7 +573,7 @@ def __init__(self, service):
self.values = []

def reset_feature_flags(self):
self.feature_flags = FeatureFlags.AppleMFiCoprocessor | FeatureFlags.SoftwareMFiAuth
self.feature_flags = FeatureFlags.APPLE_MFI_COPROCESSOR | FeatureFlags.SOFTWARE_MFI_AUTH

def write_value(self, value):
assert value[0] == 0
Expand Down Expand Up @@ -801,29 +802,29 @@ def test_pair_supported_auth(self):

# --- hw auth only
with mock.patch('homekit.controller.ble_impl.device.DeviceManager') as m:
manager._devices['00:00:00:00:00'] = Device(a, FeatureFlags.AppleMFiCoprocessor)
manager._devices['00:00:00:00:00'] = Device(a, FeatureFlags.APPLE_MFI_COPROCESSOR)
m.return_value = manager

c = Controller()
c.perform_pairing_ble('test-pairing', '00:00:00:00:00', '111-11-111')

with mock.patch('homekit.controller.ble_impl.device.DeviceManager') as m:
manager._devices['00:00:00:00:00'] = Device(a, FeatureFlags.AppleMFiCoprocessor)
manager._devices['00:00:00:00:00'] = Device(a, FeatureFlags.APPLE_MFI_COPROCESSOR)
m.return_value = manager

c = Controller()
c.perform_pairing_ble('test-pairing', '00:00:00:00:00', '111-11-111', auth_method=Controller.PairingAuth.HwAuth)

# --- sw auth only
with mock.patch('homekit.controller.ble_impl.device.DeviceManager') as m:
manager._devices['00:00:00:00:00'] = Device(a, FeatureFlags.SoftwareMFiAuth)
manager._devices['00:00:00:00:00'] = Device(a, FeatureFlags.SOFTWARE_MFI_AUTH)
m.return_value = manager

c = Controller()
c.perform_pairing_ble('test-pairing', '00:00:00:00:00', '111-11-111')

with mock.patch('homekit.controller.ble_impl.device.DeviceManager') as m:
manager._devices['00:00:00:00:00'] = Device(a, FeatureFlags.SoftwareMFiAuth)
manager._devices['00:00:00:00:00'] = Device(a, FeatureFlags.SOFTWARE_MFI_AUTH)
m.return_value = manager

c = Controller()
Expand Down Expand Up @@ -860,7 +861,7 @@ def test_pair_unsupported_auth(self):

# --- hw auth only
with mock.patch('homekit.controller.ble_impl.device.DeviceManager') as m:
manager._devices['00:00:00:00:00'] = Device(a, FeatureFlags.AppleMFiCoprocessor)
manager._devices['00:00:00:00:00'] = Device(a, FeatureFlags.APPLE_MFI_COPROCESSOR)
m.return_value = manager

c = Controller()
Expand All @@ -869,7 +870,7 @@ def test_pair_unsupported_auth(self):
self.assertRaises(exceptions.PairingAuthError)
# --- sw auth only
with mock.patch('homekit.controller.ble_impl.device.DeviceManager') as m:
manager._devices['00:00:00:00:00'] = Device(a, FeatureFlags.SoftwareMFiAuth)
manager._devices['00:00:00:00:00'] = Device(a, FeatureFlags.SOFTWARE_MFI_AUTH)
m.return_value = manager

c = Controller()
Expand Down
11 changes: 9 additions & 2 deletions tests/feature_flags_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,17 @@ def test_no_support_hap_pairing(self):
self.assertEqual(FeatureFlags[0x00], 'No support for HAP Pairing')

def test_support_hap_pairing_hw(self):
self.assertEqual(0x01, FeatureFlags.APPLE_MFI_COPROCESSOR)
self.assertEqual(FeatureFlags[0x01], 'Supports HAP Pairing with Apple authentication coprocessor')

def test_support_hap_pairing_sw(self):
self.assertEqual(0x02, FeatureFlags.SOFTWARE_MFI_AUTH)
self.assertEqual(FeatureFlags[0x02], 'Supports HAP Pairing with Software authentication')

# def test_unknown_code(self):
# self.assertRaises(KeyError, FeatureFlags.__getitem__, 99)
def test_support_hap_pairing_hw_sw(self):
self.assertEqual(FeatureFlags[FeatureFlags.APPLE_MFI_COPROCESSOR | FeatureFlags.SOFTWARE_MFI_AUTH],
'Supports HAP Pairing with Apple authentication coprocessor and Software authentication')

def test_support_hap_pairing_unknown(self):
with self.assertRaises(KeyError):
FeatureFlags[0x80]

0 comments on commit 37ccf59

Please sign in to comment.