Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not get "poweredOn" event in Ubuntu #341

Open
woowu opened this issue Aug 17, 2024 · 8 comments
Open

Not get "poweredOn" event in Ubuntu #341

woowu opened this issue Aug 17, 2024 · 8 comments

Comments

@woowu
Copy link

woowu commented Aug 17, 2024

Hi,

Since a week ago, my small noble application cannot work in my Ubunut, which can scan and connection BLE devices before. But for unknown reason, without any code change in my application, it can no longer work and I found it's because it did not the the "poweredOn" event. But the Bluetooth functionality from the OS point of view is working fine because Ubuntu's bluetooth menu can list and connect to devices as before.

Ubuntu version: 20.04 LTS
Noble version: 1.9.2-25.

What kind problem could it possible be? Where can I find some debug/log information? Thanks.

Kr,
woody

@ollivierv
Copy link

ollivierv commented Aug 17, 2024

I had the same issue with "poweredOn" event that never happen. But I'm on Ubuntu 24.04.
And exactly same code is running well on Ubuntu 22.04 on another machine.
Of course, the two configuration have perfectly operationnal HCI.

I investigated and the "first" problem comes from Hci.prototype.setSocketFilter(), the filter pass seems to be invalid for the HCI : "EINVAL, Invalid argument".
Exact same argument works on the same USB Blutooth Key (CSR8510 0x0a12 0x0001) on another marchine running ubuntu 22.04.

`
BleNordicUart: waiting for poweredOn state (currently unknown)...

hci set all phys supporting - writing: 01312003000505 +0ms

hci setting filter to: 1600000020c10800000000400000 +0ms
hci onSocketError: EINVAL, Invalid argument +0ms

hci set event mask - writing: 01010c08fffffbff07f8bf3d +1ms

hci set le event mask - writing: 010120081fff000000000000 +0ms

hci read local version - writing: 01011000 +0ms

hci write LE host supported - writing: 016d0c020100 +0ms

hci read LE host supported - writing: 016c0c00 +0ms

hci le read buffer size - writing: 01022000 +0ms

hci read bd addr - writing: 01091000 +0ms

hci set scan enabled - writing: 01422006000100000000 +5s

`

@woowu
Copy link
Author

woowu commented Aug 18, 2024

@ollivierv Thanks for your info. I always on same machine and it's always Ubuntu 20.04 LTS. Just stopped working at early this before, before that, it always works. I am afraid it's some dependencies on my system has changed but cannot recall what's that.

Where do you get these HCI level log information? Can you teach me? Thanks.

-woody

@rzr
Copy link

rzr commented Aug 19, 2024

Please try older version to find if it is regression or not ?

@Apollon77
Copy link

Are capabilities set correctly as shown in readme? Try if it works with sudo. If yes then the capabilities should be the reason.

@MrShinyAndNew
Copy link

I'm having the same problem in Fedora 40. The sample program in the quickstart example just doesn't work for me at all.

I've verified with bluetoothctl and scan le that BLE scanning is working and can see my bluetooth device (a Lego PoweredUp remote).

However in node-poweredup nothing happens, and in the noble same program I never receive the poweredOn event or any events at all.

This program prints 'Starting' and then just waits forever.

const noble = require('@abandonware/noble');
console.log('Starting');
noble.on('stateChange', async (state) => {
    console.log('stateChange ', state);
  if (state === 'poweredOn') {
    console.log('Powered on');
    await noble.startScanningAsync(['180f'], false);
  }
});

noble.on('discover', async (peripheral) => {
  await noble.stopScanningAsync();
  await peripheral.connectAsync();
  const {characteristics} = await peripheral.discoverSomeServicesAndCharacteristicsAsync(['180f'], ['2a19']);
  const batteryLevel = (await characteristics[0].readAsync())[0];

  console.log(`${peripheral.address} (${peripheral.advertisement.localName}): ${batteryLevel}%`);

  await peripheral.disconnectAsync();
  console.log('Exiting');
  process.exit(0);
});

@MrShinyAndNew
Copy link

I set export DEBUG="*" and added a console.log and found there's an error setting the HCI filter

hci setting filter to: 1600000020c10800000000400000 +0ms
hci onSocketError: EINVAL, Invalid argument +1ms
Error: EINVAL, Invalid argument
at Hci.setSocketFilter (/home/mark/code/powered_up/powered_up/node_modules/@abandonware/noble/lib/hci-socket/hci.js:262:16)
at Hci.pollIsDevUp (/home/mark/code/powered_up/powered_up/node_modules/@abandonware/noble/lib/hci-socket/hci.js:184:12)
at Hci.init (/home/mark/code/powered_up/powered_up/node_modules/@abandonware/noble/lib/hci-socket/hci.js:165:10)
at NobleBindings.init (/home/mark/code/powered_up/powered_up/node_modules/@abandonware/noble/lib/hci-socket/bindings.js:100:13)
at /home/mark/code/powered_up/powered_up/node_modules/@abandonware/noble/lib/noble.js:62:26
at process.processTicksAndRejections (node:internal/process/task_queues:77:11) {
errno: 22,
code: 'EINVAL',
syscall: 'setsockopt'
}
hci set event mask - writing: 01010c08fffffbff07f8bf3d +6ms
hci set le event mask - writing: 010120081f00000000000000 +1ms
hci read local version - writing: 01011000 +0ms
hci write LE host supported - writing: 016d0c020100 +1ms
hci read LE host supported - writing: 016c0c00 +0ms
hci le read buffer size - writing: 01022000 +0ms
hci read bd addr - writing: 01091000 +0ms

@eminberkayd
Copy link

Same issue is happing with Ubuntu 22.04. Nearly 6 months ago, I was able to get the discovered peripherals in my computer.

@MrShinyAndNew
Copy link

MrShinyAndNew commented Oct 9, 2024

I dug into this a bit further and I think there might be something else going on. Forgive me if I get this all wrong, as I haven't done C programming in years. But I wrote a sample program to try to replicate what Noble + HCI were doing and found that setsockopt would only work if the option passed in was sized as 16 bytes, not 14 as the setSocketFilter was doing.

	uint32_t type_mask;
	uint32_t event_mask[2];
	uint16_t opcode;
};
... 
struct hci_filter hf;
printf("%d", sizeof (hf));

This outputs 16.
I changed hci.js's setFilter method to use a 16 byte buffer instead of 14 and changed the opcode to uint32 since I assume the problem is structure padding to align on word boundaries, and this got rid of the setsockopt error.

Yay! Except that the program still doesn't work. The noble test program still does nothing and my node poweredup program still doesn't work. I noticed that many places in hci.js use non-multiples of 32 for the buffers to mimic the C structures, so I wonder if maybe that is the issue? But I'm just speculating as I'm in over my head here given the last time I programmed in C was in the 90s.

Edited to add: I tried adding __attribute__((packed)) to the structure in my sample C program and the size does return to 14 bytes, but that brings the error back. I suspect the Kernel's definition of this structure isn't defined as packed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants