Skip to main content

Core library for Amfiprot

Project description

Amfiprot is a communication protocol for embedded devices used and developed by Amfitech. The protocol can be extended with plugins for specific devices implementing the Amfiprot protocol (e.g. the AmfiTrack).

Prerequisites

  • Python 3.6 or higher.

Installation

Install (or update) amfiprot with pip:

pip install -U amfiprot

Quick start

The basic workflow when using the library is:

  1. Create a Connection to a device connected directly to the host machine (we call this the "root node").
  2. Search for Nodes on the Connection (i.e. nodes connected through the root node)
  3. Create a Device using one of the discovered Nodes.
  4. Start the Connection.
  5. Communicate with the Device.

Example:

import amfiprot

VENDOR_ID = 0xC17
PRODUCT_ID = 0xD12

if __name__ == "__main__":
    conn = amfiprot.USBConnection(VENDOR_ID, PRODUCT_ID)
    nodes = conn.find_nodes()

    print(f"Found {len(nodes)} node(s).")
    for node in nodes:
        print(f"[{node.tx_id}] {node.name}")

    dev = amfiprot.Device(nodes[0])
    conn.start()
    
    cfg = dev.config.read_all()

    while True:
        if dev.packet_available():
            print(dev.get_packet())

The following sections provide a more in-depth explanation.

Discovering and connecting to a root node

After attaching a device to your host machine, you can scan for connected devices (e.g. connected via USB) with:

phys_devs = amfiprot.USBConnection.discover()

for dev in phys_devs:
    print(dev)

A connection can then be created using a specific physical device:

conn = amfiprot.USBConnection(dev['vid'], dev['pid'], dev['serial_number'])

Using serial_number is optional. If none is given, the first device matching the given vendor and product ID is used.

Finding nodes

After creating a connection, we can search for nodes that are connected to the root node (e.g. via RF or UART):

nodes = conn.find_nodes()

find_nodes() returns a list of Node instances. A Node provides a low-level interface to the physical device and can be used to retrieve the uuid, tx_id and name of the device, and send/receive raw packets. It is not recommended to use the Node directly, but rather attach it to a Device instance.

Creating a device

A Device is an abstraction layer on top of a Node and is created by injecting a Node in the constructor:

dev = amfiprot.Device(node)

The Device provides a higher-level interface allowing the user to easily:

  • Update the firmware
  • Reboot the device
  • Read/write configurations
  • Read decoded packets

We are still able to access the Node through the Device if necessary:

tx_id = dev.node.tx_id

Starting the connection and interacting with the device

Before interacting with the Device, the Connection must be started:

conn.start()

This creates a child process that asynchronously sends and receives packets on the specified interface. We can now interact with the Device in the main process without worrying about blocking:

device_name = dev.name()
print(f"Reading packets from {device_name}...")

while True:
	if dev.packet_available():
		print(dev.get_packet())

We can access the device configuration through a Configurator instance which is automatically created as a member (dev.config) of the Device:

# Read the entire configuration as a JSON-like object (a list of dicts)
cfg = dev.config.read_all()

# Print all parameters
for category in cfg:
    print(f"CATEGORY: {category['name']}")
    for parameter in category['parameters']:
        print(parameter)

The configuration can be easily saved to and loaded from a .json file:

import json

# Write configuration to file
with open("config.json", "w") as outfile:
	json.dump(cfg, outfile, indent=4)

# Read configuration from file and send to device
with open("config.json", "r") as infile:
    new_cfg = json.load(infile)
    dev.config.write_all(new_cfg)

Contributions and bug reporting

The Amfiprot Python package is open-source and the source code can be found on our Github repository. Contributions can be made through pull requests to the development branch. Bug reports and feature requests can be created in the Issues tab.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

amfiprot-0.1.14.tar.gz (22.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

amfiprot-0.1.14-py3-none-any.whl (24.8 kB view details)

Uploaded Python 3

File details

Details for the file amfiprot-0.1.14.tar.gz.

File metadata

  • Download URL: amfiprot-0.1.14.tar.gz
  • Upload date:
  • Size: 22.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.7

File hashes

Hashes for amfiprot-0.1.14.tar.gz
Algorithm Hash digest
SHA256 64d51899f1d80fb75ef1df0d8f4a88e54c04901210f9dd868e9b687130b26e0d
MD5 f574d708a0162d0e1240bf7ec2967a7e
BLAKE2b-256 31e6d304d145479ea9678b7a277b2c2d3f19a23e44c4fb60b1d473e1b3194998

See more details on using hashes here.

File details

Details for the file amfiprot-0.1.14-py3-none-any.whl.

File metadata

  • Download URL: amfiprot-0.1.14-py3-none-any.whl
  • Upload date:
  • Size: 24.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.7

File hashes

Hashes for amfiprot-0.1.14-py3-none-any.whl
Algorithm Hash digest
SHA256 78a9e6572f3202a1c01ea41912db05fe7f7dc174afef925aee71533336fca327
MD5 0c5133e5f15d8c9d8c056e5743227e10
BLAKE2b-256 ef41bf870e26e9a51682ae8d4de5069681da9ee365e3e05f977ad8a2a83f0ec0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page