takawata / FreeBSD-BLE

17 stars 9 forks source link

FreeBSD 12 and BLE #1

Open sparvu opened 5 years ago

sparvu commented 5 years ago

Hi,

Can we use BLE on FreeBSD 12 x64/arm64(raspberry pi) ? If yes is there any howto or README how to get started ?

Im trying to connect a BLE device like Airthings Radon sensor to a raspberry pi 3b+, running fbsd12.

takawata commented 5 years ago

If you use external USB dongle, I think it will work. But Raspberry PI 3 B+ built-in bluetooth interface is not supported because bluetooth serial interface is long broken and I cannot confirm the built-in bluetooth HCI respond through serial interface.

sparvu commented 5 years ago

Right. What models (USB dongles) could I try ? Do you have any ideas, examples ?

takawata commented 5 years ago

In my PC, I use following device. ubt0: <vendor 0x0a12 CSR8510 Nanosira M2272, class 224/1, rev 2.00/75.58, addr 1> on usbus1

sparvu commented 5 years ago

Ok. Do you have any link to the product? From online shop or any other references whats and how much does ur cost?

Thanks a lot for help

sparvu commented 5 years ago

Is that an USB dongle?

takawata commented 5 years ago

https://www.amazon.com/s?k=CSR8510&ref=nb_sb_noss You may want to buy one of these.

sparvu commented 5 years ago

+1. Will try to get one or two for testing. Thanks a lot.

Do I need to have anything else on top of 12.0 release freebsd to make it working ? Im planning to test on Raspberry Pi 3B+

takawata commented 5 years ago

The kernel code was unmodified since 20 month ago, expect for recent iwm wifi bt support. So nothing is needed, I think.

sparvu commented 5 years ago

thanks a lot.

sparvu commented 5 years ago

is this ok for FreeBSD 12 Raspberry PI3B+ ? It is based on CSR8510 A10. https://thepihut.com/products/adafruit-bluetooth-4-0-usb-module-v2-1-back-compatible?ref=isp_rel_prd&isp_ref_pos=1

takawata commented 5 years ago

Probably.

sparvu commented 5 years ago

thanks. ordered. i will report later the results.

sparvu commented 5 years ago

If a RADON sensor says: "REQUIREMENTS One of the latest major versions of iOS or Android supporting Bluetooth 4.2 or later" and my future dongle is based on CSR8510 BLE 4.0 would it matter ?

Is there a big difference between BLE 4.0 and 4.2 ?

takawata commented 5 years ago

Authentication methods and credit based connection( for 6lowpan).

sparvu commented 5 years ago

Finally here more details:

  1. Device: https://thepihut.com/products/adafruit-bluetooth-4-0-usb-module-v2-1-back-compatible?ref=isp_rel_prd&isp_ref_pos=1

  2. FreeBSD 12 RELEASE ARM64 Raspberry PI3B+

ubt0 on uhub1
ubt0: <vendor 0x0a12 product 0x0001, class 224/1, rev 2.00/75.58, addr 5> on usbus0
WARNING: attempt to domain_add(bluetooth) after domainfinalize()
WARNING: attempt to domain_add(netgraph) after domainfinalize()
Security policy loaded: MAC/ntpd (mac_ntpd)
ubt0: ubt_bulk_read_callback:979: bulk-in transfer failed: USB_ERR_IOERROR
  1. kldstat
freebsd@k1iot:~ % kldstat
Id Refs Address                Size Name
 1   21 0xffff000000000000  13d1480 kernel
 2    1 0xffff000053600000    41000 tmpfs.ko
 3    1 0xffff000053641000    41000 ng_ubt.ko
 4    5 0xffff000053682000    41000 netgraph.ko
 5    1 0xffff0000536c3000    31000 ng_hci.ko
 6    3 0xffff0000536f4000    31000 ng_bluetooth.ko
 7    1 0xffff000053725000    31000 ng_l2cap.ko
 8    1 0xffff000053756000    51000 ng_btsocket.ko
 9    1 0xffff0000537a7000    41000 ng_socket.ko
10    1 0xffff000053800000    31000 mac_ntpd.ko
  1. Start bluetooth service
root@k1iot:~ # service bluetooth start ubt0
/etc/rc.d/bluetooth: ERROR: Unable to setup Bluetooth stack for device ubt0

Something is still not okay with the driver or this card. Right ?

sparvu commented 5 years ago

and here added what the netgraph list says:

root@k1iot:~ # ngctl list
There are 6 total nodes:
  Name: ubt0            Type: ubt             ID: 00000001   Num hooks: 0
  Name: btsock_hci_raw  Type: btsock_hci_raw  ID: 00000002   Num hooks: 0
  Name: btsock_l2c_raw  Type: btsock_l2c_raw  ID: 00000003   Num hooks: 0
  Name: btsock_l2c      Type: btsock_l2c      ID: 00000004   Num hooks: 0
  Name: btsock_sco      Type: btsock_sco      ID: 00000005   Num hooks: 0
  Name: ngctl1117       Type: socket          ID: 00000015   Num hooks: 0
sparvu commented 5 years ago

any ideas ? somebody suggested on freebsd-arm that this might be related with missing firmware ?

takawata commented 5 years ago

Sorry for comment too late. This is because usb stack failure. Do you try it in PC with FreeBSD installed?

sparvu commented 5 years ago

this is a Raspberry PI computer. Model 3B+. Im interested to get the BLE working on the RBPI board not on the PC. Do you think this would even work ? What is it missing ?

I have 2 dongles now:

  1. CSR8510 A10 based

  2. Asus USB-BT400 Broadcom BCM20702 based (which I understood the driver must support too)

None work on RBPI hardware.

takawata commented 5 years ago

CSR8510 A10 based chip should work if no USB stack problem,I think. I know you want to get it work in RPI3+, but to be sure it is RPI3+ specific, I propose you to try it in PC.

BCM20702 requires Firmware, I think. https://github.com/winterheart/broadcom-bt-firmware

sparvu commented 5 years ago

ok, so my first dongle should work.

sparvu commented 5 years ago

I see the following:

ubt0 on uhub1
ubt0: <vendor 0x0a12 product 0x0001, class 224/1, rev 2.00/75.58, addr 5> on usbus0
WARNING: attempt to domain_add(bluetooth) after domainfinalize()
WARNING: attempt to domain_add(netgraph) after domainfinalize()
Security policy loaded: MAC/ntpd (mac_ntpd)
ubt0: ubt_bulk_read_callback:979: bulk-in transfer failed: USB_ERR_IOERROR
ubt0: ubt_bulk_read_callback:979: bulk-in transfer failed: USB_ERR_IOERROR

I am able to start the bluetooth service if I have copied: cp /etc/defaults/bluetooth.device.conf /etc/bluetooth/ubt0.conf

root@k1iot:~ # service bluetooth stop ubt0
root@k1iot:~ # 
root@k1iot:~ # service bluetooth start ubt0
root@k1iot:~ # 

but Im not able to see any devices around. For example my iphone.

sparvu commented 5 years ago

ok, I will try the device on a laptop with latest FreeBSD. Lets see how you say if this is ARM specific problem. Will come back later with the results.

sparvu commented 5 years ago

I have tried the CSR8510 A10 based dongle on a laptop running 12.1 RC. Here are the findings:

root@kk10:~ # uname -a
FreeBSD kk10 12.1-BETA1 FreeBSD 12.1-BETA1 r352546 GENERIC  amd64

root@kk10:~ # service bluetooth start ubt0
ng_hci_process_command_timeout: ubt0hci - unable to complete HCI command OGF=0x3, OCF=0x3. Timeout

I have everything loaded correctly.

root@kk10:~ # kldstat
Id Refs Address                Size Name
 1   18 0xffffffff80200000  2448d30 kernel
 2    1 0xffffffff82821000     4260 ng_ubt.ko
 3    6 0xffffffff82826000     9e30 netgraph.ko
 4    2 0xffffffff82830000     91b8 ng_hci.ko
 5    3 0xffffffff8283a000      9c0 ng_bluetooth.ko
 6    1 0xffffffff8283b000     cad0 ng_l2cap.ko
 7    1 0xffffffff82848000    1ba00 ng_btsocket.ko
 8    1 0xffffffff82864000     21c0 ng_socket.ko

Im not seeing such thing on ARM Raspberry PI.

sparvu commented 5 years ago

I did test as well the Asus UBT400 (Broadcom chip) same error. Timeout. The CSR8510 based dongle should have worked ? Do I need to use the firmware ?

takawata commented 5 years ago

Hmmm.. All what I can say is your device is not responding during initialization.

Disable devd and run sh -x /etc/rc.d/bluetooth start ubt0 and see what command make the device stall.

sparvu commented 5 years ago

the bluetooth starts just fine. after disabling devd or with it.

+ bluetooth_setup_stack ubt0 hook
+ dev=ubt0
+ shift
+ hook=hook
+ shift
+ ngctl mkpeer ubt0: hci hook drv
+ ngctl name ubt0:hook ubt0hci
+ ngctl msg ubt0hci: set_debug 3
+ ngctl mkpeer ubt0hci: l2cap acl hci
+ ngctl name ubt0hci:acl ubt0l2cap
+ ngctl msg ubt0l2cap: set_debug 3
+ ngctl connect ubt0hci: btsock_hci_raw: raw ubt0raw
+ ngctl connect ubt0l2cap: btsock_l2c_raw: ctl ubt0ctl
+ ngctl connect ubt0l2cap: btsock_l2c: l2c ubt0l2c
+ /usr/sbin/hccontrol -n ubt0hci reset
+ /usr/sbin/hccontrol -n ubt0hci read_bd_addr
+ /usr/sbin/hccontrol -n ubt0hci read_local_supported_features
+ /usr/sbin/hccontrol -n ubt0hci read_buffer_size
+ checkyesno bluetooth_device_discoverable
+ eval '_value=$bluetooth_device_discoverable'
+ _value=0
+ debug 'checkyesno: bluetooth_device_discoverable is set to 0.'
+ return 1
+ checkyesno bluetooth_device_connectable
+ eval '_value=$bluetooth_device_connectable'
+ _value=1
+ debug 'checkyesno: bluetooth_device_connectable is set to 1.'
+ return 0
+ /usr/sbin/hccontrol -n ubt0hci write_scan_enable 2
+ /usr/sbin/hccontrol -n ubt0hci write_class_of_device ff:01:0c
+ checkyesno bluetooth_device_authentication_enable
+ eval '_value=$bluetooth_device_authentication_enable'
+ _value=0
+ debug 'checkyesno: bluetooth_device_authentication_enable is set to 0.'
+ return 1
+ /usr/sbin/hccontrol -n ubt0hci write_authentication_enable 0
+ /usr/sbin/hccontrol -n ubt0hci write_encryption_mode 0
+ checkyesno bluetooth_device_role_switch
+ eval '_value=$bluetooth_device_role_switch'
+ _value=1
+ debug 'checkyesno: bluetooth_device_role_switch is set to 1.'
+ return 0
+ /usr/sbin/hccontrol -n ubt0hci write_node_role_switch 1
+ /usr/sbin/hccontrol -n ubt0hci change_local_name 'kk10 (ubt0)'
+ /usr/sbin/hccontrol -n ubt0hci initialize
+ return 0
+ return 0
+ _return=0
+ [ 0 -ne 0 ]
+ return 0
+ _run_rc_postcmd
+ [ -n '' ]
+ return 0
+ return 0

Hmmm. But today after a clean boot of the machine ... I see correctly:

root@kk10:~ # hccontrol -n ubt0hci inquiry
Inquiry complete. Status: No error [00]

So it does not see any BLE devices around. Which is true. I have a sensor I need to power on. And I think it wont discover the iphone ? Right ? Since iphone is not an BLE device.

takawata commented 5 years ago

Ok, now your device works correctly. You need to enter some command after this.

# hccontrol le_enable enable
# hccontrol le_set_scan_enable enable
# hccontrol  read_neighbor_cache
# hccontrol le_set_scan_enable disable
sparvu commented 5 years ago

it does work with and without devd. no problems there.

root@kk10:~ # hccontrol -n ubt0hci inquiry
Inquiry complete. Status: No error [00]

then I ran your 'le' commands:

root@kk10:~ # hccontrol le_enable enable
Using HCI node: ubt0hci

root@kk10:~ # hccontrol le_set_scan_enable enable
Using HCI node: ubt0hci

root@kk10:~ # hccontrol  read_neighbor_cache
Using HCI node: ubt0hci
T BD_ADDR           Features                Clock offset Page scan Rep. scan
R 60:72:f3:c1:18:70 00 00 00 00 00 00 00 00            0      0xd5         0
NDflag:1a
Type10:ff 4c 00 10 05 13 1c 96 b6 37 

R 53:19:44:5c:39:57 00 00 00 00 00 00 00 00            0      0xb2         0
NDflag:1a
Type10:ff 4c 00 10 05 11 18 2e ea e1 

R 4d:43:95:1d:75:db 00 00 00 00 00 00 00 00            0      0xc3         0
NDflag:1a
Type10:ff 4c 00 10 05 07 1c 44 fb dc 

R 1c:c4:88:5f:b2:4a 00 00 00 00 00 00 00 00            0      0xd7         0
NDflag:1b
Type11:ff 4c 00 09 06 03 4f c0 a8 01 b1 

P e0:7d:ea:07:1c:c7 00 00 00 00 00 00 00 00            0      0xc2         0
NDflag:6
Type17:06 ba 5c f7 
Type59:12 d3 89 e4 11 e7 ad 08 1c 2e b4 09 ff 34 03 58 94 a4 ae 08 00 

P f4:5c:89:a6:15:98 00 00 00 00 00 00 00 00            0      0xb3         0
NDflag:6
Type10:ff 4c 00 10 
NDflag:1c

P dc:56:e7:38:d6:35 00 00 00 00 00 00 00 00            0      0xd5         0
NDflag:1a
Type10:ff 4c 00 10 05 01 14 0c c9 79 

R 78:e0:8a:24:75:4b 00 00 00 00 00 00 00 00            0      0xb3         0
NDflag:6
Type19:ff 4c 00 0c 
Type8:61 f2 66 5b da 06 cf bb 2d cd b7 be 

and finally disable it

root@kk10:~ # hccontrol le_set_scan_enable disable
Using HCI node: ubt0hci

now i have some bluettoth devices around including a BLE sensor.

sparvu commented 5 years ago

so these are my BT devices ?

root@kk10:~ # hccontrol read_neighbor_cache
Using HCI node: ubt0hci
T BD_ADDR           Features                Clock offset Page scan Rep. scan
P e0:7d:ea:07:1c:c7 00 00 00 00 00 00 00 00            0      0xb6         0
P dc:56:e7:38:d6:35 00 00 00 00 00 00 00 00            0      0xd7         0
R 7e:0f:ab:62:e4:38 00 00 00 00 00 00 00 00            0      0xbf         0
R 1c:c4:88:5f:b2:4a 00 00 00 00 00 00 00 00            0      0xd6         0
R 5b:c3:5c:68:93:df 00 00 00 00 00 00 00 00            0      0xcd         0
R 78:e0:8a:24:75:4b 00 00 00 00 00 00 00 00            0      0xbc         0

can i see more information about these devices ? And until now we should be able to see these on ARM too ? Right ?

takawata commented 5 years ago

To be honestly, there is an bug at the scan result buffer parser in hccontrol.

Apply this patch , rebuild hccontrol and retry.

https://reviews.freebsd.org/D21779

sparvu commented 5 years ago

before any patching: should I try same thing on ARM / RaspberryPI ?

sparvu commented 5 years ago

do we have other issues on ARM / RBPI platforms with hccontrol ?

takawata commented 5 years ago

There are No specific thing on ARM, I think

I think "P e0:7d:ea:07:1c:c7" may be your target device because others are issue Apple related beacon. (begin with ff 00 4c)

sparvu commented 5 years ago

yep. ARM64 now starting to work. If I stop devd this works.

root@k1iot:~ # hccontrol le_enable enable
root@k1iot:~ # hccontrol le_set_scan_enable enable
root@k1iot:~ # hccontrol  read_neighbor_cache
T BD_ADDR           Features                Clock offset Page scan Rep. scan
P e0:7d:ea:07:1c:c7 00 00 00 00 00 00 00 00            0      0xb5         0
NDflag:6
Type17:06 ba 5c f7 
Type59:12 d3 89 e4 11 e7 ad 08 1c 2e b4 09 ff 34 03 58 94 a4 ae 08 00 

R 6f:b5:6f:15:eb:60 00 00 00 00 00 00 00 00            0      0xab         0
NDflag:1a
Type10:ff 4c 00 10 05 03 1c ba 60 a0 

R 7d:ce:55:74:85:92 00 00 00 00 00 00 00 00            0      0xaf         0
NDflag:1a
Type10:ff 4c 00 10 05 13 1c c9 2e 7f 

P dc:56:e7:38:d6:35 00 00 00 00 00 00 00 00            0      0xac         0
NDflag:1a
Type10:ff 4c 00 10 05 01 14 0c c9 79 

R 78:e0:8a:24:75:4b 00 00 00 00 00 00 00 00            0      0xb3         0
NDflag:6
Type19:ff 4c 00 0c 
Type8:51 f3 0e 69 c7 31 e2 c5 4f 7f d2 57 

R 1c:c4:88:5f:b2:4a 00 00 00 00 00 00 00 00            0      0xac         0
NDflag:1b
Type11:ff 4c 00 09 06 03 51 c0 a8 01 b1 

P f4:5c:89:a6:15:98 00 00 00 00 00 00 00 00            0      0xb3         0
NDflag:6
Type10:ff 4c 00 10 
NDflag:1c

root@k1iot:~ # uname -a
FreeBSD k1iot 12.0-RELEASE FreeBSD 12.0-RELEASE r341666 GENERIC  arm64

I will try to enable and start back devd and see if this is still working. But how can I pair one device ... !?

sparvu commented 5 years ago

Can I see more details from one device ?

takawata commented 5 years ago

Now you can use a tool in this repository. Try lehid $bdaddr like

cd lehid
%./lehid  dc:56:e7:38:d6:35
%./lehid -r 7d:ce:55:74:85:92

P is public address R is random address.

takawata commented 5 years ago

And probably power failure may lead your device doesn't work well. You may want to insert pause in /etc/rc.d/bluetooth script to wait USB power stable.

sparvu commented 5 years ago

aha. thanks for all tips. I have to test all these.

My goal is to make a Perl5 script, part of Kronometrix Data Recording [1], which could extract data from a BLE sensor using a RBPI FreeBSD 12 setup. I do not know yet how can I fetch from Perl5 data from BLE devices. I might need first some Perl library which can sit on top of FreeBSD hccontrol or something similar.

[1] https://gitlab.com/kronometrix/recording

sparvu commented 5 years ago

lehid requires sqlite ?

freebsd@k1iot:~/FreeBSD-BLE/lehid % make
make: "/usr/share/mk/bsd.opts.mk" line 101: warning: "NO_MAN is defined, but deprecated. Please use MK_MAN=no instead."
echo lehid.full: /usr/lib/libc.a  >> .depend
Warning: Object directory not changed from original /home/freebsd/FreeBSD-BLE/lehid
cc  -O2 -pipe -I/usr/local/include -g   -g -MD  -MF.depend.main.o -MTmain.o -std=gnu99 -fstack-protector-strong    -Qunused-arguments  -c main.c -o main.o
main.c:55:10: fatal error: 'sqlite3.h' file not found
#include <sqlite3.h>
         ^~~~~~~~~~~
1 error generated.
*** Error code 1

Stop.
make: stopped in /home/freebsd/FreeBSD-BLE/lehid
sparvu commented 5 years ago

ok, i got lehid compiled and tried to run against one bdaddress

root@k1iot:/home/freebsd/FreeBSD-BLE/lehid # ./lehid e0:7d:ea:07:1c:c7
CONNECT
connect: Operation timed out
CONNECTOK
no more rows available
Broken pipe
root@k1iot:/home/freebsd/FreeBSD-BLE/lehid # 
sparvu commented 5 years ago

same thing for

root@k1iot:/home/freebsd/FreeBSD-BLE/lehid # ./lehid -r e0:7d:ea:07:1c:c7
CONNECT
connect: Operation timed out
CONNECTOK
no more rows available
Broken pipe
root@k1iot:/home/freebsd/FreeBSD-BLE/lehid # 
sparvu commented 5 years ago

and sometimes after some time I see no more devices when trying to scan:

root@k1iot:/home/freebsd/FreeBSD-BLE/lehid # hccontrol read_neighbor_cache
T BD_ADDR           Features                Clock offset Page scan Rep. scan
sparvu commented 5 years ago

Another question: what if we try to use BLE devices using a board like this: https://www.adafruit.com/product/2479 ? Would this be easier for us using FreeBSD 12 ARM Raspberry PI such way we can send AT commands to the board bypassing all Bluetooth management via hccontrol ?

takawata commented 5 years ago

That seems to be peripheral side .

sparvu commented 5 years ago

Right. But using such BLE shield will let us fetch and manage via serial port allBLE traffic. Correct?

For example another one: http://blog.blecentral.com/2015/05/05/hm-10-peripheral/

takawata commented 5 years ago

Absolutely no. That is simply serial port emulation service, not BLE agent. To use them in FreeBSD, we should write such like rfcomm_sppd(4) using GATT protocol. What makes you think so? I recommend you to read the Spec. 

sparvu commented 5 years ago

Ok. I might have misunderstood. I had the impression that there are ways to manage via serial port BLE traffic without getting into details about BLE.

Meaning I was thinking there are ways we could execute in a hardware all BLE internals using a simple serial port comunication.

Back to my USB dongle then

sparvu commented 4 years ago

Im re-starting all tests on Raspberry PI 3B+ and FreeBSD 12.1 using 2 BLE 4.2 Radon sensors from Airthings. Here the link to the sensor: https://www.airthings.com/wave-plus

I will report back all my findings.

sparvu commented 4 years ago

Device

https://thepihut.com/products/adafruit-bluetooth-4-0-usb-module-v2-1-back-compatible?ref=isp_rel_prd&isp_ref_pos=1

BLE 4.2 Sensors

https://www.airthings.com/wave-plus

Comments

T BD_ADDR           Features                Clock offset Page scan Rep. scan
R 50:5e:0b:0d:23:0e 00 00 00 00 00 00 00 00            0      0xaf         0
NDflag:1a
Type2:0a 0c 0b ff 4c 00 10 06 13 1e 5c b1 54 b7 

Problems