Metadata-Version: 2.4
Name: pspair
Version: 0.1.0
Summary: Pair PlayStation controllers (DualShock 3 / DualShock 4) with a Bluetooth host over USB
License-Expression: MIT
Keywords: playstation,dualshock,sixaxis,bluetooth,pairing,usb,cli
Classifier: Development Status :: 3 - Alpha
Classifier: Environment :: Console
Classifier: Intended Audience :: End Users/Desktop
Classifier: Operating System :: Microsoft :: Windows
Classifier: Operating System :: POSIX :: Linux
Classifier: Operating System :: MacOS
Classifier: Programming Language :: Python :: 3
Classifier: Topic :: System :: Hardware
Classifier: Topic :: Utilities
Requires-Python: >=3.8
Description-Content-Type: text/markdown
License-File: LICENSE
Provides-Extra: libusb
Requires-Dist: libusb>=1.0.26; extra == "libusb"
Dynamic: license-file

# pspair

A command-line reimplementation of **SixaxisPairTool.exe**'s core function:
pair a PlayStation controller with a Bluetooth host by writing the host's
Bluetooth address into the controller over USB.

No GUI, no driver installer, no third-party Python packages.

## Install

```
pip install pspair
```

On Windows, `pip install "pspair[libusb]"` also installs a libusb-1.0 DLL, so
you do not have to supply one yourself. If the original tool already placed
`libusb0.dll` in `System32`, the plain install is enough.

## Usage

```
pspair list
pspair get
pspair set 00:1A:7D:DA:71:13
```

From a source checkout the same commands work as `python pspair.py ...`.

## Protocol

Recovered from the shipped binary (PE32, x86, 612 864 bytes,
`sha256 97b89949f8c92d698b34e55235f0cd0c7c2075e66f37c3fd38d3912f2f877a35`)
by PE import/resource inspection and a Capstone linear sweep of `.text`; the
only two `usb_control_msg` call sites in the image were decoded instruction by
instruction. Both are USB class requests to interface 0 with a 5000 ms timeout.

| Model | PID | read | write |
|---|---|---|---|
| DualShock 3 / Sixaxis | `0x0268` | `0xA1 0x01 0x03F5`, len 50 → `reply[2:8]` | `0x21 0x09 0x03F5`, len 8 → `01 00 <addr>` |
| DualShock 4 / v2 | `0x05C4`, `0x09CC` | `0xA1 0x01 0x0312`, len 50 → `reply[10:16]`, byte-reversed | `0x21 0x09 0x0313`, len 23 → `13 <addr reversed> <16 bytes>` |

`0xA1` = IN | class | interface, `0x01` = GET_REPORT; `0x21` = OUT | class |
interface, `0x09` = SET_REPORT.

### How the DualShock 4 read layout was established

Report `0x12` answers with 16 bytes:

```
idx   0    1    2    3    4    5    6    7    8    9   10   11   12   13   14   15
val  12   68   37   5c   88   27   d0   08   25   00   01   01   01   3c   2b   1a
                                                  +------- master, reversed -------+
```

The master address is the tail six bytes, byte-reversed: with the host set to
`1A:2B:3C:01:01:01`, the reply ends `01 01 01 3c 2b 1a`. This was confirmed by
reading the controller with both this tool and the original EXE: both report
`1A:2B:3C:01:01:01`.

Bytes `reply[1:7]` (`68 37 5c 88 27 d0`) are a different field - almost
certainly the controller's own BD address, though that is not confirmed.

**The disassembly was misleading here.** The DS4 branch appears to extract
`reply[15:21]`, which cannot exist in a 16-byte reply, and an earlier version of
this tool therefore read the wrong field. Offsets for this controller had to be
settled with a known ground-truth value, not by reading the decompiled code.

### DualShock 4 write payload

The last 16 bytes (`56 e8 81 38 08 06 51 41 c0 7f 12 aa d9 66 3c ce`) are
copied verbatim from the binary (built at `0x24f4`). Their meaning was not
resolved; they are almost certainly the link key. The address is written in
reverse byte order, as the original does (`[esi+0x14]` down to `[esi]` into
`reply[1:6]`).

Not implemented: **PlayStation Move** (`0x03D5`). It is recognised and then
refused before any transfer, because its write payload was only partially
recovered.

## Backends

| Backend | Platforms | Library | Driver required |
|---|---|---|---|
| `libusb1` | Windows, Linux, macOS | `libusb-1.0` (`.dll`/`.so`/`.dylib`) | WinUSB or libusbK |
| `libusb0` | Windows only | `libusb0.dll` (libusb-win32) | `libusb0.sys` |

`libusb0` exists only on Windows; it is offered because the original tool
installs exactly that driver, so on a machine that has run the original the
pairing path already works with no extra install.

`--backend auto` (default) picks a backend by **openability, not
enumeration**: libusb-1.0 lists every USB device including ones bound to a
driver it cannot claim, so a backend that can merely *see* the controller is
useless. `auto` opens and closes each candidate and keeps the first that
succeeds; if none can open it, the first that saw a controller is used so the
real error surfaces.

Library lookup: `--dll PATH`, then the standard names, then
`ctypes.util.find_library`, then the DLLs shipped by the optional `libusb`
PyPI package.

### Driver notes per platform

* **Windows** - libusb0 needs libusb-win32; libusb1 needs WinUSB or libusbK
  (Zadig). A controller can be visible to both backends and openable by only
  one.
* **Linux** - needs access (`sudo`, or a udev rule such as
  `SUBSYSTEM=="usb", ATTR{idVendor}=="054c", MODE="0666"`). The kernel HID
  driver normally owns the interface, so the tool detaches it before claiming
  and re-attaches on close.
* **macOS** - no driver install; IOKit may hold the interface, covered by the
  same detach logic.

## Commands

```
list [--all]          enumerate controllers; --all shows every USB device
get [--index N]       read the paired host address
set ADDR [--index N]  write a new host address
self-test             framing/parsing/struct-layout checks, no hardware
--backend auto|libusb1|libusb0
--dll PATH            explicit library path
--interface N         USB interface to claim (default 0)
--dry-run             print the exact transfers without touching hardware
```

`--dry-run` needs no driver or device; it uses the connected model when one is
present and otherwise labels the output as the DualShock 3 default.

`set` reads the address back after writing and fails if the controller does not
report the requested value, for both models.

## Verification performed

Against real hardware - a DualShock 4 (`054C:05C4`) attached to this host:

* `get` -> `1A:2B:3C:01:01:01`, matching the original EXE reading the same
  controller (the value was previously written by the original tool).
* A report probe confirmed this device answers `0x0312` with 16 bytes,
  `0x0302` with 37 bytes and `0x0311` with 3 bytes, and rejects `0x0313`,
  `0x0304`, `0x0305`, `0x0314`, `0x03F5` with a Win32 error.
* `auto` selects `libusb0` (the driver the device is bound to) rather than
  `libusb1`, which enumerates it but reports `access denied` on open.
* `self-test` covers both payloads, the observed DS4 reply against the known
  host address, model gating for the unsupported PID, and the ctypes struct
  offsets the libusb0 walk depends on (`descriptor`@536, `devnum`@576,
  `devices`@528).

Not verified:

* **`set` has never been run against hardware.** No write has been sent to a
  controller, so whether the DualShock 4 report `0x13` payload actually pairs
  it is untested. Pairing changes controller state that this tool cannot undo.
* **DualShock 3** has not been tested on hardware; its protocol comes from the
  binary only.
* **Linux and macOS** paths are written to the documented libusb-1.0 API but
  have not been executed on those platforms.
* What `reply[1:7]` names.

## Known failure modes seen during development

* Sending the DS3 report `0x03F5` to a DS4 makes the transfer fail with a
  Win32 "device not functioning" error - the model must select the report.
* A rejected transfer can leave the device needing a retry; `read_master`
  retries once on an empty reply, then fails with a message naming the report.
* libusb0's error text is ANSI in the local code page (e.g. GBK), not UTF-8;
  it is decoded with the active code page or it appears as mojibake.
* A partial disassembly is not enough to place a reply field. Two guesses
  (`reply[15:21]`, then `reply[1:7]`) were both wrong before a known
  ground-truth address pinned it to `reply[10:16]`.

## Publishing a release

The package is one module plus `pyproject.toml`; there is no compiled code and
no runtime dependency. The version lives only in `pspair.__version__`, which
`pyproject.toml` reads, so there is nothing else to keep in sync.

```
python -m build                  # dist/pspair-<version>-py3-none-any.whl + .tar.gz
python -m twine check dist/*     # validates metadata and the README rendering
python -m twine upload dist/*    # TWINE_USERNAME=__token__, TWINE_PASSWORD=pypi-...
```

PyPI rejects a version that already exists, so bump `__version__` before every
upload. To rehearse, use
`python -m twine upload --repository testpypi dist/*` and install with
`pip install -i https://test.pypi.org/simple/ pspair`.
