Metadata-Version: 2.4
Name: isdttool-pal
Version: 2.0.0
Summary: Tool to retrieve information from ISDT chargers with USB connection.
Home-page: https://github.com/pallemannen/isdttool
Author: Max Ried
Author-email: maxried@posteo.de
Maintainer: Patrik Bodin
Maintainer-email: the@pal.pp.se
License: GPLv3
Classifier: Programming Language :: Python :: 3
Classifier: Topic :: System :: Hardware :: Hardware Drivers
Classifier: License :: OSI Approved :: GNU General Public License v3 (GPLv3)
Classifier: Operating System :: OS Independent
Requires-Python: >=3.6
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: hidapi>=0.10.1
Dynamic: author
Dynamic: author-email
Dynamic: classifier
Dynamic: description
Dynamic: description-content-type
Dynamic: home-page
Dynamic: license
Dynamic: license-file
Dynamic: maintainer
Dynamic: maintainer-email
Dynamic: requires-dist
Dynamic: requires-python
Dynamic: summary

# isdttool

> This is a fork of [maxried/isdttool](https://github.com/maxried/isdttool), published on PyPI as `isdttool-pal`. It adds a network bridge (`serve`, `--host`), selecting chargers by USB serial number, and ISDT N24 support. Everything is offered upstream as pull requests; once they are merged, use `isdttool` instead. The command, and the import name stay `isdttool`.
>
>     pip install isdttool-pal

## Important Notes
This software is not sponsored by ISDT, or anyone else. It was created by observing the protocol between the updater, then the charger, and fuzzing the charger, and observing its behavior. Chargers are dangerous tools. You should never use them unattended. Batteries can leak, catch fire, etc. This tool comes with no warranties whatsoever. If bad things happen, I'm not responsible.

## Summary

`isdttool` is a utility to retrieve information such as the current charging status from ISDT chargers. It can output it as plain text, json, and csv, so it should be suitable for automation. Tested models are
- ISDT C4
- ISDT A4
- DNT Smart PRO (which just is a rebranded ISDT C4 with old firmware)
- ISDT C4EVO
- ISDT Q8 (minimal support)
- ISDT N24 (reports itself as "NXHOSTP"; metrics only, it does not answer `serial`)

It should be compatible with other chargers by ISDT, at least the ones that are not primarily for charging LiPo. E.g. N8 should be compatible, but it's unknown if models like P20, or D1 work. It requires a USB connection. ISDT chargers with firmware upgrade capability should at least be detectable by this tool.

## Installation

The only dependency is `hidapi`, which should be automatically installed if you use `pip` to install `isdttool`.

    pip install isdttool-pal

Not every user is allowed to access human interface devices on every platform. If you're on some Linux, you likely have to add some udev rules if you want to run `isdttool` as an unprivileged user. A quick fix would be to save the following file in `/etc/udev/rules.d/99-isdt.rules`, and run `udev trigger`:

    ATTRS{idVendor}=="28e9", MODE="0666"

## Network bridge

`isdttool serve` exposes a charger on a local USB port to the network, so that `isdttool`, or any other program, can use it from another machine. It runs in the foreground until the charger is unplugged, and is meant to run as a service. `contrib/isdttool@.service` is a systemd template for it.

    # On the machine with the charger. --serial picks the charger, and does not change
    # when it moves to another USB port.
    $ isdttool list
    Model "SMART Device" with serial "00000000011C" at "1-1.3:1.0"
    $ isdttool --serial 00000000011C serve --port 24000

    # Anywhere else
    $ isdttool --host charger-pi version
    $ isdttool -H charger-pi:24000 metrics
    $ isdttool -H [2001:db8::1]:24000 metrics

Each bridge serves one charger on one TCP port. The default port is 24000, so more chargers can use 24001, 24002, and so on. Only one client is served at a time, and a new client replaces the current one.

The wire format is deliberately simple, so that other programs can talk to a bridge without isdttool: in both directions, every message is one packet, preceded by its length as two bytes, big-endian. A packet is a command or a response, starting with its opcode, e.g. `00 01 e0` asks for the version. The link-specific framing, like the USB framing in [protocol.md](protocol.md), stays between the bridge and the charger. There is no handshake, and no other message type.

One exception: some chargers, like the N24, don't answer the `serial` command. For those, the bridge answers it with the charger's USB serial number, so that clients can still tell chargers apart.

## Usage

Most options are self-explanatory, and you should not be able to break your charger with this tool unless you voluntarily use the `raw-command` sub-command. Firmware upgrades are not supported right now, but you can have a look at firmware files, and test if a certain image is flashed to your charger.

Usage examples:

    # The metrics command shows you the status of the channels. If you call it with --output json, the output is much more verbose.
    $ isdttool metrics
    CH 0      charging:    NiMH    AA at  90 %, 27 °C,  1.430 V *  0.999 A,  80 Ohm, 13 s
    CH 1        idling:    auto empty at   0 %, 28 °C,  0.000 V *  0.000 A,   0 Ohm, 0 s
    CH 2        idling:    auto empty at   0 %, 28 °C,  0.000 V *  0.000 A,   0 Ohm, 0 s
    CH 3      charging:    NiMH    AA at  99 %, 27 °C,  1.425 V *  1.003 A,  43 Ohm, 2 s
    
    $ isdttool --output json metrics --channel 0
    [{"_type": "metrics", "_channel exists": true, "channel": 0, "mode id": 3, "mode string": "charging", "chemistry id": 9, "chemistry string": "NiMH", "dimensions id": 1, "dimensions string": "AA", "temperature": 29, "internal_temperature": 0, "progress": 96, "charging voltage": 1383, "charging current": 799, "resistance": 83, "power": 1228, "energy": 31, "capacity or peak voltage": 4985, "time": 62, "_malformed": false, "_measurement": 1}]
    
    # If you happen to run a command that is not supported by the charger in its current mode,
    # you get a message about that. You can disable this check with the `--debug`, `-d` flag.
    $ isdttool sensors                                                                                                                       [12:03:43]
    This command is currently not supported by the model "A4" in app mode.
    The command is supported in the following modes:
    Model "C4" in app mode
    
    
    $ isdttool version
    Model name: C4
    Hardware version 1.0.0.4
    Boot loader version 1.0.0.3
    OS/App version 1.1.0.16
    
    $ isdttool rename Test
    Device renamed, rebooting.
    
    $ isdttool fw-info -f Firmware.fwd
    Firmware Image Summary
    ----------------------
    Embedded Checksum:   0xe063dcf7
    Calculated Checksum: 0xe063dcf7
    Checksum OK
    
    $ isdttool boot-loader
    Rebooting to boot loader.
    $ isdttool verify-fw --file A4.bin
    The checksum matches the checksum of the image in flash.
    
    $ isdttool boot-app
    Rebooting to app.
    
    # This one might not be obvious. It runs the supplied command whenever a change in the charging status happens.
    # It sets some informative environment variables. If you call it without `--command`, `-c` parameter,
    # it defaults to `env` to show you what's going on.
    $ isdttool monitor --command '[ "$_REASON" = "mode id" -o "$_REASON" = "periodic" ] && telegram "$HUMAN_READABLE"'
    [...]
    
    # Serial returns the factory programmed serial number of the processor.
    # This is different from the serial number shown in the GUI of the C4, which is most likely a date code, while the
    # serial number of the processor is supposed to be random.
    $ isdttool serial
    Serial Number: 33c0011816666b0410324d3d
    
    
    # If you have multiple chargers attached you can specify the one you like to query using the `--path` parameter.
    $ isdttool --path "IOService:/AppleACPIPlatformExpert/PCI0@0/[...]" version
    Model name: A4
    Hardware version 1.2.0.0
    Boot loader version 1.0.0.1
    OS/App version 1.0.0.20
