Git Product home page Git Product logo

Comments (16)

WayneKeenan avatar WayneKeenan commented on May 26, 2024 2

I think in my next experiment I'm going to Pythonise an example and supporting subset of the NodeJS BLE library BluetoothHCISocket by @sandeepmistry

from python-bluezero.

ukBaz avatar ukBaz commented on May 26, 2024 2

Good work @WayneKeenan!

There are two performance issues that are a concern with DBus

  1. continuously scanning for devices. E.g. if you want to constantly scan for nearby beacons. This was discussed on the Bluez mailing list
    http://marc.info/?l=linux-bluetooth&m=148123125327634&w=2
    There is no way to do this with the DBus API currently
  2. Throughput with read/write/notify of GATT characteristics. This was the issue raised on the Adafruit BLE library that was linked to on the Bluezero retrospective.
    New DBus API of AcquireNotify and AcquireWrite seems to be trying to address this.
    https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/doc/gatt-api.txt#n117

Does that help?

from python-bluezero.

WayneKeenan avatar WayneKeenan commented on May 26, 2024 1

added a noddy class in a gist that the test just simply bounces the hci0 interface.

Needs to be run with sudo.

from python-bluezero.

WayneKeenan avatar WayneKeenan commented on May 26, 2024 1

I have implemented the experiment and have LE Scanning working, please see https://github.com/TheBubbleworks/python-hcipy

from python-bluezero.

WayneKeenan avatar WayneKeenan commented on May 26, 2024 1

There is now a Connection and a PhysicalWeb Beacon example.

Scripts can also can run as a non-root user, please check out the README for more info.

I'm keen to understand the performance benefits of the approach, @ukBaz please could you outline the structure of a specific test scenario you had in mind? (it was beacon related I think)

from python-bluezero.

ukBaz avatar ukBaz commented on May 26, 2024 1

Thanks @kaidokert .

I've started a wiki to track these libraries and make it easier for people to update directly:
https://github.com/ukBaz/python-bluezero/wiki

from python-bluezero.

WayneKeenan avatar WayneKeenan commented on May 26, 2024

think the way to go is to use struct.pack and fcntl.ioctl, e.g.

buffer = struct.pack("I", dev_id)
fcntl.ioctl(sock.fileno(), HCIDEVUP, buffer)

My test case gets past the data format but is hitting now hitting complaint from the kernel:

IOError: [Errno 25] Inappropriate ioctl for device

My experiment is here

I think running as root might be required. To circumvent that requirement Noble/Bleno use an external binary which has a ACL applied to the binary, that is something not possible for a 'script', AFAIK.

aversion to running a python bluezero daemon as root?

from python-bluezero.

WayneKeenan avatar WayneKeenan commented on May 26, 2024

The reason for the failure is because the HCI commands passed to ioctl need to be converted from the 'bare' number to a Linux ioctl encoding, e.g. http://code.activestate.com/recipes/578225-linux-ioctl-numbers-in-python/

I've had a quick try, doesn't work as-is, needs a bit more massaging, will take a look later.

from python-bluezero.

WayneKeenan avatar WayneKeenan commented on May 26, 2024

This is the 'call stack' with links to the functions:

  1. Python enable_le_scan() (example from @colin-guyon)
  2. bt_hci_send_cmd() (the underlying C source for import bluetooth._bluetooth as bluez found in many Python BLE libs)
  3. hci_send_cmd() (BlueZ userland C code)
  4. writev() (POSIX/GLIBC)
  5. The Magic Happens(tm)

So, all that's needs is to compress the bits in-between 1 and 5 into 1 :)

EDIT: I should caveat the above by saying this was a manual trawl thru the filesystem and online repositories, not from a runtime or IDE introspection, so please beware it maybe inaccurate.

from python-bluezero.

WayneKeenan avatar WayneKeenan commented on May 26, 2024

I meant to add that the prototype demonstrates 3 key aspects of using socket io for HCI:

  1. sending HCI Commands (e.g. HCIDEVUP)
  2. decoding HCI responses (e.g. HCIDEVINFO, EVT_LE_META_EVENT)
  3. handling unsolicited HCI events (e.g. EVT_LE_META_EVENT for LE Scanning)

I've also kept in mind the need to do automated testing so the core class has been written with future automated tests in mind, comments in the code.

from python-bluezero.

mr499 avatar mr499 commented on May 26, 2024

That's a great demonstration - I spent quite some time banging my head against a brick wall with a prototype similar to Barry's opener, without knowing about the IOCTL numbers (I was getting a "file descriptor in a bad state" type error from fcntl.ioctl). I'm glad Barry asked the question and that there is a purely pythonic way to do HCI!

from python-bluezero.

WayneKeenan avatar WayneKeenan commented on May 26, 2024

I have just added the port of the LE advertisement test, https://github.com/TheBubbleworks/python-hcipy/blob/master/le_advertisement_test.py

from python-bluezero.

kaidokert avatar kaidokert commented on May 26, 2024

Just as a comment, here is yet another half finished pile of code that kind of speaks HCI ( written for TI BLE embedded configurations )

https://github.com/mzxu/pyble/tree/master/ble

Assumes a serial port, but i've verified it to somewhat work with plain character device too for basic functions. At least the constant tables might be useful for anyone trying direct HCI interface implementation

from python-bluezero.

ukBaz avatar ukBaz commented on May 26, 2024

Thanks @kaidokert .

We should add this to the list of libraries on #126

Maybe we should start a wiki with all these libraries listed.

from python-bluezero.

kaidokert avatar kaidokert commented on May 26, 2024

Not to flood, but i came across this too https://github.com/Jumperr-labs/hci-protocol . Built on top of https://en.wikipedia.org/wiki/Construct_(python_library) which provides nice self-documenting code.

Jumperr-labs also has python-gatt-server and jumper-ble-logger repos that use this implementation.

from python-bluezero.

ukBaz avatar ukBaz commented on May 26, 2024

Closing this and following the Python HCI sockets work happening elsewhere

from python-bluezero.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.