Tigge / openant

ANT and ANT-FS Python Library
MIT License
174 stars 80 forks source link

ANT-FS Device Scanner #66

Open tobotpyrc opened 3 years ago

tobotpyrc commented 3 years ago

Hi, currenty I try to program an ant-fs device scanner which lists me all available ANT-FS devices in the surrounding area. Important for me is the serial number (the full serial number which is sent in the authentication layer, not the 2 Byte number from link layer) and the friendly name of the devices. The pairing should be excluded. Just a passive scan would be enough. Are there any thoughts how achieving this as soon as possible with this tool? I tried here an there but did not get an proper solution. Can you help me?

Thank You!

Tigge commented 3 years ago

Hi.

This could potentially be based on https://github.com/Tigge/openant/blob/master/examples/continuous_scan.py for example, combined with a pairing attempt, but I'm not sure exactly how that would work, and what information is available without pairing it.

tobotpyrc commented 3 years ago

Hi Tigge , thanks for reply and your great work here. This seems to be a reasonable solution. I can run this script, and I can see the devices : e.g: 1623324609.0035694 RX: 14511 , 1 : [43 2c 00 03 fa 03 01 00 80 af 38 01 05] 1623324625.625098 RX: 58106 , 1 : [43 0c 00 03 fa 03 01 00 80 fa e2 01 05]

Now the only thing I have to do is to get the right value to the RX value (This is the serialnumber in link layer). How do I perform such a pairing request in this script against any of those found devices? (When performing such a paring-request the client device send his corresponding (long) serialnumber and his friendly name to us) And this would be the information iam looking for. I would be so thankful if you can help me at this point, I need this information for my bachelor thesis...

Thank you

Tigge commented 3 years ago

Perhaps the next step would be to combine this with some of the code in https://github.com/Tigge/openant/blob/master/ant/fs/manager.py#L179. An ANT-FS device should periodically send out a beacon. Which are the messages you already see there I think.

The beacon should contain most of the information you seek. But you would need to auth with the device to get additional information. That means pairing with the device, or authenticating if you have previously paired with the device.

The beacon has information if data are available, uploads are enabled, if it is open to pairing and the beacon channel period. Also the device state, authentication type, and a 4 byte serial number, with device/manufacturer parts.

tobotpyrc commented 3 years ago

Thanks for the hint, I did try understand the code but at some points I cant follow. My thoughts: Read the beacon ---> [43 2c 00 03 fa 03 01 00 80 af 38 01 05] is that the beacon anyway? Are that bytes?

if data[0] == 0x43: # checks the first Byte?
    _on_beacon(data[:8]) # check data from the first 8 Bytes?

I have absolutely no idea how that code snippet (From beacon.py) parses data from a beacon...

def parse(data): mark, status_byte_1, status_byte_2, authentication_type = struct.unpack( "<BBBB4x", data )

    assert mark == Beacon.BEACON_ID

    return Beacon(status_byte_1, status_byte_2, authentication_type, data[4:])

I definitely will try to keep it working but I think I need your help here...

Thank you

Tigge commented 3 years ago

You can use the entire class there to parse it. Just call Beacon.parse(data) and then use the methods to read out the data.

[43 2c 00 03 fa 03 01 00 80 af 38 01 05] is the beacon, yes. First byte is 43 which indicates that this is a beacon. 2c (101100) indicates this device has data available, upload disabled, enabled pairing enabled, uses 8hz channel. And so on with the rest of the data according to the ANT-FS standard.

tobotpyrc commented 3 years ago

Ok I think i have managed to read the beacons now https://github.com/tobotpyrc/openant/blob/master/examples/serialNumberFinder.py

Next step is to send the authentication command with a corresponding pairing bit? right? Therefore I import the command.py from your project. But now I have no idea how to proceed... I noticed the function authentication_serial():

How could I appropriately include this function to my script?

If I can really keep that project working please let me know how I can pay you a bit for your help.

thank you

Tigge commented 3 years ago

What you want to do is more or less:

  1. Establish a link with the device: See https://github.com/Tigge/openant/blob/945686be9594d964d7ef0f4897f2ae473ae4e72d/ant/fs/manager.py#L381 - after this you are now in the Authentication Layer.
  2. Request the serial number: https://github.com/Tigge/openant/blob/945686be9594d964d7ef0f4897f2ae473ae4e72d/ant/fs/manager.py#L390

After this you are either done if that's all you wanted to do, or you could also try to authenticate with the device. Some devices might support the Pass-through authentication - but otherwise, you need to pair or know the appropriate passkey.