libmdr-bt 0.1.0__py3-none-win_amd64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- libmdr_bt/__init__.py +8 -0
- libmdr_bt/_dll.py +121 -0
- libmdr_bt/_native/mdr-bt-shared.dll +0 -0
- libmdr_bt/connection.py +138 -0
- libmdr_bt-0.1.0.dist-info/METADATA +13 -0
- libmdr_bt-0.1.0.dist-info/RECORD +9 -0
- libmdr_bt-0.1.0.dist-info/WHEEL +5 -0
- libmdr_bt-0.1.0.dist-info/licenses/LICENSE +21 -0
- libmdr_bt-0.1.0.dist-info/top_level.txt +1 -0
libmdr_bt/__init__.py
ADDED
libmdr_bt/_dll.py
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
"""ctypes loader for mdr-bt-shared (libmdr-bt)."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import os
|
|
6
|
+
import sys
|
|
7
|
+
from ctypes import CDLL
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
|
|
10
|
+
import libmdr._dll as libmdr_dll
|
|
11
|
+
|
|
12
|
+
_LIB: CDLL | None = None
|
|
13
|
+
|
|
14
|
+
_ENV_OVERRIDE = "MDR_BT_DLL"
|
|
15
|
+
|
|
16
|
+
_CANDIDATE_NAMES = (
|
|
17
|
+
"mdr-bt-shared.dll",
|
|
18
|
+
"libmdr-bt-shared.so",
|
|
19
|
+
"libmdr-bt-shared.dylib",
|
|
20
|
+
"mdr-bt-shared.so",
|
|
21
|
+
"mdr-bt-shared.dylib",
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
_CORE_NAMES = (
|
|
25
|
+
"mdr-shared.dll",
|
|
26
|
+
"mdr.dll",
|
|
27
|
+
"libmdr-shared.so",
|
|
28
|
+
"libmdr-shared.dylib",
|
|
29
|
+
"mdr-shared.so",
|
|
30
|
+
"mdr-shared.dylib",
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def _package_native_dir() -> Path:
|
|
35
|
+
return Path(__file__).resolve().parent / "_native"
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def _candidate_paths() -> list[Path]:
|
|
39
|
+
paths: list[Path] = []
|
|
40
|
+
override = os.environ.get(_ENV_OVERRIDE)
|
|
41
|
+
if override:
|
|
42
|
+
paths.append(Path(override))
|
|
43
|
+
|
|
44
|
+
native = _package_native_dir()
|
|
45
|
+
for name in _CANDIDATE_NAMES:
|
|
46
|
+
paths.append(native / name)
|
|
47
|
+
|
|
48
|
+
repo_build = Path(__file__).resolve().parents[4] / "build" / "python"
|
|
49
|
+
if repo_build.is_dir():
|
|
50
|
+
for name in _CANDIDATE_NAMES:
|
|
51
|
+
paths.extend(repo_build.rglob(name))
|
|
52
|
+
|
|
53
|
+
return paths
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def _ensure_core_loaded(platform_dir: Path) -> None:
|
|
57
|
+
if libmdr_dll._LIB is not None: # noqa: SLF001
|
|
58
|
+
return
|
|
59
|
+
|
|
60
|
+
for name in _CORE_NAMES:
|
|
61
|
+
sibling = platform_dir / name
|
|
62
|
+
if sibling.is_file():
|
|
63
|
+
libmdr_dll._LIB = libmdr_dll._load_path(sibling) # noqa: SLF001
|
|
64
|
+
return
|
|
65
|
+
|
|
66
|
+
libmdr_dll.load()
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def _load_path(path: Path) -> CDLL:
|
|
70
|
+
path = path.resolve()
|
|
71
|
+
if not path.is_file():
|
|
72
|
+
raise FileNotFoundError(path)
|
|
73
|
+
|
|
74
|
+
_ensure_core_loaded(path.parent)
|
|
75
|
+
|
|
76
|
+
if sys.platform == "win32":
|
|
77
|
+
os.add_dll_directory(str(path.parent))
|
|
78
|
+
core_dir = libmdr_dll._package_native_dir() # noqa: SLF001
|
|
79
|
+
if core_dir.is_dir():
|
|
80
|
+
os.add_dll_directory(str(core_dir))
|
|
81
|
+
return CDLL(str(path))
|
|
82
|
+
|
|
83
|
+
mode = os.RTLD_GLOBAL if hasattr(os, "RTLD_GLOBAL") else 0
|
|
84
|
+
return CDLL(str(path), mode=mode)
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def load() -> CDLL:
|
|
88
|
+
global _LIB
|
|
89
|
+
if _LIB is not None:
|
|
90
|
+
return _LIB
|
|
91
|
+
|
|
92
|
+
errors: list[str] = []
|
|
93
|
+
seen: set[Path] = set()
|
|
94
|
+
for candidate in _candidate_paths():
|
|
95
|
+
try:
|
|
96
|
+
resolved = candidate.resolve()
|
|
97
|
+
except OSError as exc:
|
|
98
|
+
errors.append(f"{candidate}: {exc}")
|
|
99
|
+
continue
|
|
100
|
+
if resolved in seen:
|
|
101
|
+
continue
|
|
102
|
+
seen.add(resolved)
|
|
103
|
+
try:
|
|
104
|
+
_LIB = _load_path(resolved)
|
|
105
|
+
return _LIB
|
|
106
|
+
except OSError as exc:
|
|
107
|
+
errors.append(f"{resolved}: {exc}")
|
|
108
|
+
|
|
109
|
+
hint = (
|
|
110
|
+
f"Set {_ENV_OVERRIDE} to the full path of mdr-bt-shared.dll, "
|
|
111
|
+
f"or `pip install -e libmdr-bt` to build it."
|
|
112
|
+
)
|
|
113
|
+
raise FileNotFoundError(
|
|
114
|
+
"Unable to load mdr-bt-shared.\n"
|
|
115
|
+
+ "\n".join(errors)
|
|
116
|
+
+ ("\n" + hint if errors else hint)
|
|
117
|
+
)
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
def lib() -> CDLL:
|
|
121
|
+
return load()
|
|
Binary file
|
libmdr_bt/connection.py
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
"""Platform connection factories wrapping mdr-bt-shared (libmdr-bt)."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import sys
|
|
6
|
+
from ctypes import c_void_p
|
|
7
|
+
from typing import Literal
|
|
8
|
+
|
|
9
|
+
from libmdr.connection import Connection
|
|
10
|
+
|
|
11
|
+
from . import _dll
|
|
12
|
+
|
|
13
|
+
Backend = Literal["auto", "classic", "ble"]
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def _bind_factory(create_name: str, get_name: str, destroy_name: str):
|
|
17
|
+
lib = _dll.lib()
|
|
18
|
+
create = getattr(lib, create_name)
|
|
19
|
+
getter = getattr(lib, get_name)
|
|
20
|
+
destroy = getattr(lib, destroy_name)
|
|
21
|
+
|
|
22
|
+
create.restype = c_void_p
|
|
23
|
+
create.argtypes = []
|
|
24
|
+
getter.restype = c_void_p
|
|
25
|
+
getter.argtypes = [c_void_p]
|
|
26
|
+
destroy.restype = None
|
|
27
|
+
destroy.argtypes = [c_void_p]
|
|
28
|
+
return create, getter, destroy
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class PlatformConnection:
|
|
32
|
+
"""Owns a platform-specific connection object and exposes the MDRConnection vtable."""
|
|
33
|
+
|
|
34
|
+
def __init__(self, backend: Backend = "auto") -> None:
|
|
35
|
+
self._backend = "classic" if backend == "auto" else backend
|
|
36
|
+
self._owner: c_void_p | None = None
|
|
37
|
+
self._connection: Connection | None = None
|
|
38
|
+
self._destroy = None
|
|
39
|
+
self._create()
|
|
40
|
+
|
|
41
|
+
def _factories(self):
|
|
42
|
+
platform = sys.platform
|
|
43
|
+
if platform == "win32":
|
|
44
|
+
if self._backend == "ble":
|
|
45
|
+
return _bind_factory(
|
|
46
|
+
"mdrConnectionWindowsBLECreate",
|
|
47
|
+
"mdrConnectionWindowsBLEGet",
|
|
48
|
+
"mdrConnectionWindowsBLEDestroy",
|
|
49
|
+
)
|
|
50
|
+
return _bind_factory(
|
|
51
|
+
"mdrConnectionWindowsCreate",
|
|
52
|
+
"mdrConnectionWindowsGet",
|
|
53
|
+
"mdrConnectionWindowsDestroy",
|
|
54
|
+
)
|
|
55
|
+
if platform == "darwin":
|
|
56
|
+
if self._backend == "ble":
|
|
57
|
+
raise RuntimeError("BLE backend is not available on macOS in mdr-bt-shared")
|
|
58
|
+
return _bind_factory(
|
|
59
|
+
"mdrConnectionMacOSCreate",
|
|
60
|
+
"mdrConnectionMacOSGet",
|
|
61
|
+
"mdrConnectionMacOSDestroy",
|
|
62
|
+
)
|
|
63
|
+
if platform.startswith("linux"):
|
|
64
|
+
if self._backend == "ble":
|
|
65
|
+
raise RuntimeError("BLE backend is not available on Linux in mdr-bt-shared")
|
|
66
|
+
return _bind_factory(
|
|
67
|
+
"mdrConnectionLinuxCreate",
|
|
68
|
+
"mdrConnectionLinuxGet",
|
|
69
|
+
"mdrConnectionLinuxDestroy",
|
|
70
|
+
)
|
|
71
|
+
raise RuntimeError(f"Unsupported platform for libmdr-bt bindings: {platform}")
|
|
72
|
+
|
|
73
|
+
def _create(self) -> None:
|
|
74
|
+
create, getter, destroy = self._factories()
|
|
75
|
+
owner = create()
|
|
76
|
+
if not owner:
|
|
77
|
+
raise RuntimeError(f"Failed to create platform connection ({self._backend})")
|
|
78
|
+
connection = getter(owner)
|
|
79
|
+
if not connection:
|
|
80
|
+
destroy(owner)
|
|
81
|
+
raise RuntimeError("Platform connection Get() returned null")
|
|
82
|
+
|
|
83
|
+
self._destroy = destroy
|
|
84
|
+
self._owner = c_void_p(owner)
|
|
85
|
+
self._connection = Connection(connection)
|
|
86
|
+
|
|
87
|
+
@property
|
|
88
|
+
def backend(self) -> str:
|
|
89
|
+
return self._backend
|
|
90
|
+
|
|
91
|
+
@property
|
|
92
|
+
def connection(self) -> Connection:
|
|
93
|
+
if self._connection is None:
|
|
94
|
+
raise RuntimeError("Platform connection is closed")
|
|
95
|
+
return self._connection
|
|
96
|
+
|
|
97
|
+
def close(self) -> None:
|
|
98
|
+
if self._owner is not None and self._destroy is not None:
|
|
99
|
+
self._destroy(self._owner)
|
|
100
|
+
self._owner = None
|
|
101
|
+
self._connection = None
|
|
102
|
+
|
|
103
|
+
def __enter__(self) -> PlatformConnection:
|
|
104
|
+
return self
|
|
105
|
+
|
|
106
|
+
def __exit__(self, *exc: object) -> None:
|
|
107
|
+
self.close()
|
|
108
|
+
|
|
109
|
+
def __del__(self) -> None:
|
|
110
|
+
try:
|
|
111
|
+
self.close()
|
|
112
|
+
except Exception:
|
|
113
|
+
pass
|
|
114
|
+
|
|
115
|
+
def devices(self):
|
|
116
|
+
return self.connection.devices()
|
|
117
|
+
|
|
118
|
+
def connect(self, mac_address: str, service_uuid: str) -> int:
|
|
119
|
+
return self.connection.connect(mac_address, service_uuid)
|
|
120
|
+
|
|
121
|
+
def disconnect(self) -> None:
|
|
122
|
+
self.connection.disconnect()
|
|
123
|
+
|
|
124
|
+
def poll(self, timeout_ms: int = 0) -> int:
|
|
125
|
+
return self.connection.poll(timeout_ms)
|
|
126
|
+
|
|
127
|
+
def last_error(self) -> str:
|
|
128
|
+
return self.connection.last_error()
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
def create_connection(*, ble: bool = False, backend: Backend | None = None) -> PlatformConnection:
|
|
132
|
+
"""Create the host platform connection.
|
|
133
|
+
|
|
134
|
+
Prefer `ble=True` / `backend="ble"` on Windows when talking over GATT.
|
|
135
|
+
"""
|
|
136
|
+
if backend is None:
|
|
137
|
+
backend = "ble" if ble else "classic"
|
|
138
|
+
return PlatformConnection(backend=backend)
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: libmdr-bt
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Python binding for libmdr-bt (Sony headphones platform connection backend)
|
|
5
|
+
License-Expression: MIT
|
|
6
|
+
Project-URL: Source, https://github.com/mos9527/libmdr-py
|
|
7
|
+
Requires-Python: >=3.10
|
|
8
|
+
Description-Content-Type: text/plain
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Requires-Dist: libmdr
|
|
11
|
+
Dynamic: license-file
|
|
12
|
+
|
|
13
|
+
ctypes bindings for libmdr-bt (mdr-bt-shared).
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
libmdr_bt/__init__.py,sha256=1IjbXTeOBZX4EZ3ELksbFrNIV8P40SPVENseQ07JDEk,190
|
|
2
|
+
libmdr_bt/_dll.py,sha256=XQrJO3za7u3s7OX5L9J5Y9heP9HMuy1hy6a0BjRZGBs,3018
|
|
3
|
+
libmdr_bt/connection.py,sha256=41oC_sFTe4VAUA6gBFHNAAwXT3ZDRVg7n1DXy5yztVE,4557
|
|
4
|
+
libmdr_bt/_native/mdr-bt-shared.dll,sha256=aniLNq-DMwWHc80tGv2vdDRhcFVoPJR0EGyMhD_OtoM,142848
|
|
5
|
+
libmdr_bt-0.1.0.dist-info/licenses/LICENSE,sha256=kV52ZgBjnUFxShyLDgnkngrD6PPJAmTI4I1wGVvEhcE,1120
|
|
6
|
+
libmdr_bt-0.1.0.dist-info/METADATA,sha256=rboDU-ZnhXlUOUmsZVAKOOax6UnaAYwqgNTqsvVbhaw,407
|
|
7
|
+
libmdr_bt-0.1.0.dist-info/WHEEL,sha256=79BCzrXBJOa2YV6ugKrK59dqWifMGjAkgVd_6AQrqbU,98
|
|
8
|
+
libmdr_bt-0.1.0.dist-info/top_level.txt,sha256=u9q-JGv0hsMLpGpu6n5aZ8bhB8h4GjzOdoCemkoB360,10
|
|
9
|
+
libmdr_bt-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 mos9527, Amr Satrio and other contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
libmdr_bt
|