diff --git a/samsung_appliance/registry/identity.py b/samsung_appliance/registry/identity.py new file mode 100644 index 0000000..7c16fe9 --- /dev/null +++ b/samsung_appliance/registry/identity.py @@ -0,0 +1,37 @@ +"""Read device identity from standard OCF resources (/oic/p, /oic/d).""" +from __future__ import annotations + +from dataclasses import dataclass +from typing import Optional + +import cbor2 + + +@dataclass(frozen=True) +class DeviceIdentity: + manufacturer: str + model: str + name: str + serial: Optional[str] + + +def _get(sess, path) -> dict: + try: + code, pl = sess.get(path, timeout=10.0) + if code == 0x45 and pl: + body = cbor2.loads(pl) + return body if isinstance(body, dict) else {} + except Exception: + pass + return {} + + +def read_identity(sess, serial: Optional[str]) -> DeviceIdentity: + p = _get(sess, ['oic', 'p']) + d = _get(sess, ['oic', 'd']) + return DeviceIdentity( + manufacturer=p.get('mnmn') or 'Samsung', + model=p.get('mnmo') or '', + name=d.get('n') or '', + serial=serial, + ) diff --git a/tests/test_identity.py b/tests/test_identity.py new file mode 100644 index 0000000..a60e7fd --- /dev/null +++ b/tests/test_identity.py @@ -0,0 +1,33 @@ +import cbor2 + +from samsung_appliance.registry.identity import DeviceIdentity, read_identity + + +class FakeSession: + def __init__(self, table): + self.table = table # tuple(path) -> rep dict + + def get(self, path, timeout=10.0): + rep = self.table.get(tuple(path)) + if rep is None: + return 0x84, b'' # 4.04 not found + return 0x45, cbor2.dumps(rep) + + +def test_read_identity_from_oic_p_and_d(): + sess = FakeSession({ + ('oic', 'p'): {'mnmn': 'Samsung Electronics', 'mnmo': 'RF9000B'}, + ('oic', 'd'): {'n': 'Family Hub'}, + }) + ident = read_identity(sess, serial='ABC123') + assert ident.manufacturer == 'Samsung Electronics' + assert ident.model == 'RF9000B' + assert ident.name == 'Family Hub' + assert ident.serial == 'ABC123' + + +def test_read_identity_tolerates_missing_resources(): + ident = read_identity(FakeSession({}), serial=None) + assert ident.manufacturer == 'Samsung' + assert ident.model == '' + assert ident.serial is None