How can i receive a pairing request event via dbus, glib? - c

I'm currently making a Bluetooth app with BlueZ
I need to get a Pairing request.
Using BlueZ DBus API, i've made my Agent into NoInputOutput mode, and i succeed the first pairing with my phone.
However, if i delete the linux device(which uses my bluetooth app that i am developing) from my phone and try to re-connect, i can't pair it together.
I searched Python code examples that...
From BlueZ's DBus API, calling RequestAuthorization method to pair and successes it, then deleting the device and re-pairing works fine.
But in case of smartphone's pairing request, i think i should call RequestAuthorization method WHEN SOME EVENT(S) is received. And i have no idea for that.
Conclusion: How can i get a pairing request event using dbus and glib loop?
I've already checked
bluez pairing before exchange
apparently, gdbus is a combination of dbus and glib.
However, i'm using dbus and glib separated since i can sorted some codes that i do not need.
More, now i think it doesn't have any relations with deleting devices from the list.

Related

Read key press on a bluetooth keyboard

In my linux program I need to read the key presses on a bluetooth keyboard.
I tried to follow a set of examples using libbluetooth, but have so far no success.
At the last stage I am totally confused on what API to use.
As I understood, the library provides SDP and HCI interfaces and they both seem to be suitable for my need. I even could read out some information about the keyboard, but I cannot get further how to receive key presses.
So, I need you help to understand:
do I need in general to use the libbluetooth or I have to use native BT sockets?
assuming, that I do need to use the lib, which API to use: SDP or HCI (perhaps another..)?
is the lib only required to initiate data transfer from the BT device, or also to receive the data

dbus: ConnectProfile method: error host is down

Actually I'm using D-Feet (D-Feet can be used to inspect D-Bus interfaces of running programs and invoke methods on those interfaces) to connect to a BLE peripheral that advertises proximity profile.
When I try the Connect() method on the remote object /org/bluez/hci0/dev_88_6B_0F_00_C4_3A every thing is fine and the connection succeed but when I try to connect only the proximity profile using ConnectProfile("0x1802") method an error occurs saying that the host is down:
g-io-error-quark: GDBus.Error:org.bluez.Error.Failed: Host is down
(36)
Can anyone help me solving this problem (I'm blocked for 2 weeks and there still to much to deal with in the project :/)
ConnectProfile("0x1802")
ConnectProfile (and the Bluez API in general) does not deal with handles, only UUIDs. Your input argument does not look like a UUID: I suggest you find the remote service UUID that matches the handle (I'm assuming your current input argument is a handle).
I believe you can find the UUID with d-feet (after Connect() the service objects should be there) or with bluez command line tools.

Sending smtp email from microcontroller

This may not be in the right location, so tell me and I'll move it.
I am a recent EE grad and I was hired to build a system that exists on a SoC with a simple 32-bit processor. The system basically monitors several external devices and performs some DSP on it, and then is supposed to send the results using a WiFi device (in my case I have the ESP8266 using UDP) to an email server for logging/notification.
I have been trying to find a library that I can use, but my uC can only program in C and I have it set up for UDP, and everything is in C++ using some other protocol, or something else completely.
I am great at DSP, decent at SoC's and uC's, but when it come to this email server communication thing I am at a loss.
I have successfully configured everything for the sensors, the datapath, the DSP, and connected the system to my WiFi via UDP, but I have yet to figure out how to send data to any servers.
Could someone help me understand how I should go about this?
I have looked into some simple SMTP commands such as HELO, MAIL, RCPT, DATA, etc. but I cannot understand how I actually should implement them in my code.
When I send out the WiFi data via UDP what type of data do I send and how do I format it? Do I need to send any other kind of flags? How should I expect the response? I also know the data has to be transformed into base 64 which is confusing me further.
I am also not super familiar with UDP to begin with, I have been using libraries that are part of the SoC's default library to connect to my WiFi.
I know these may either seem like obvious or stupid questions but it is were I no longer have any knowledge, and everything I find online doesn't make sense, or doesn't attempt to explain it, just gives a pre-made solution
I have found the RFC2821 but it doesn't get any clearer.
I know that's a lot but any help at all would be a lifesaver!
Since you are asking this question, I'm assuming that you are not booting and running an OS suitable for micro-controllers such as an embedded variant of Linux or such. If you were, you would simply be able to take advantage of possibly built in applications or other existing code.
But you don't mention having written an Ethernet stack, so are you using some other library or operating environment which might have some of the functionality needed for an implementation of SMTP?
If you don't and really do need to write your own SMTP client to run directly on the processor you are using, then you should be able to find plenty of examples of source code for this. A quick google search of How To Write an SMTP client showed a few articles with some example code. One article seems to be an exact hit, but you need to look at it further.
However, I would highly suggest just sitting down with a telnet client and connect to an SMTP server you are allowed to use and try the commands you need to just send a message. If you only need to send text, you don't need to get involved in MIME encoding or anything like that.

Is Bluez (bluetoothd) not sending DBUS method_call SelectConfiguration upon Connection? If so, how do I catch it?

UPDATE: not solved yet, but part of a solution. See below.
I am attempting to connect my iPod or other smart device to my laptop (Kubuntu 13.10) via bluetooth and stream a2dp-encoded music to a bluetooth speaker. Never mind why I don't connect them directly. I had a lot of trouble getting the devices to actually pair then connect at the same time, but now I'm able to do that. There are a million posts out there that describe different ways to connect BT headsets by changing .asoundrc, /etc/bluetooth/audio.conf, and loading a loopback module through PulseAudio's controller. However, I could never get PulseAudio to consistently load the sources and sinks, so I scratched that idea and started following this great post by James B: http://jamesbond3142.no-ip.org/blog/?viewCat=Bluetooth
I got the code compiled. It uses the (newer?) DBus API for BlueZ. Following his instructions, I was able to pipe music into the a2dp "server" or out of it, but there is no sound. In the code, he waits for two DBUS method_calls, SelectConfiguration and SetConfiguration in order to obtain the "transport", which I think is a handle or file descriptor that describes where the audio should go to/from. However, the handlers for these events are never called. I set up dbus-monitor to listen on the system bus for these calls using this guide: https://wiki.ubuntu.com/DebuggingDBus. However, the method calls don't show. Then again, I tried listening to ANY method calls on the system bus and there are some that don't show in dbus-monitor that I know are being called. (i.e. RegisterEndpoint)
I see signals from org.bluez, but no method calls. When running bluetoothd in verbose mode I get something like this when I connect my device:
bluetoothd[12118]: audio/a2dp.c:setup_ref() 0x7f9e8ffbdac0: ref=2
bluetoothd[12118]: audio/avdtp.c:avdtp_set_configuration() 0x7f9e8ffd03f0: int_seid=1, acp_seid=1
bluetoothd[12118]: audio/a2dp.c:setup_unref() 0x7f9e8ffbdac0: ref=1
bluetoothd[12118]: audio/avdtp.c:session_cb()
bluetoothd[12118]: audio/avdtp.c:avdtp_parse_resp() SET_CONFIGURATION request succeeded
bluetoothd[12118]: audio/a2dp.c:setconf_cfm() Source 0x7f9e8ffa4600: Set_Configuration_Cfm
bluetoothd[12118]: audio/avdtp.c:avdtp_sep_set_state() stream state changed: IDLE -> CONFIGURED
bluetoothd[12118]: audio/avdtp.c:session_cb()
bluetoothd[12118]: audio/avdtp.c:avdtp_parse_resp() OPEN request succeeded
but on another post I saw that someone had this:
bluetoothd[8315]: audio/a2dp.c:setup_ref() 0x7f1f8300b000: ref=2
bluetoothd[8315]: audio/avdtp.c:avdtp_set_configuration() 0x7f1f83012250: int_seid=2, acp_seid=1
bluetoothd[8315]: audio/a2dp.c:setup_unref() 0x7f1f8300b000: ref=1
bluetoothd[8315]: audio/avdtp.c:session_cb()
bluetoothd[8315]: audio/avdtp.c:avdtp_parse_resp() SET_CONFIGURATION request succeeded
bluetoothd[8315]: audio/a2dp.c:setconf_cfm() Sink 0x7f1f82fc3210: Set_Configuration_Cfm
bluetoothd[8315]: audio/media.c:media_endpoint_async_call() Calling SetConfiguration: name = :1.65 path = /MediaEndpoint/A2DPSink
bluetoothd[8315]: audio/avdtp.c:avdtp_sep_set_state() stream state changed: IDLE -> CONFIGURED
bluetoothd[8315]: audio/avdtp.c:session_cb()
bluetoothd[8315]: audio/avdtp.c:avdtp_parse_resp() OPEN request succeeded
At this point I have two theories:
1) BlueZ isn't sending this to the bus I'm watching when my device is connected
2) I'm not watching correctly (do method_calls also need to be added with dbus_bus_add_match () ?)
I find it hard to believe the second one, since James B's code worked for him and I didn't modify it. I'm using bluez version 4.101. (same as he, I think)
It's hard to find the exact conditions under which BlueZ calls these methods.
My syslog says something like this when I start the program:
bluetoothd[8519]: Endpoint registered: sender=:1.166 path=/MediaEndpoint/A2DPSource
and unregisters as well when the program exits.
I can't think of other details to add at the moment, but I'm happy to if I haven't provided enough. Thanks in advance.
UPDATE
A little embarrassing, but I racked my brain today about why a LINUX SYSTEM wouldn't get a file handle...hmmm...so I appended sudo to the program call (James B's a2dp-alsa program) and it worked. At least for the input device. Still nothing for the output (BT speaker), but I'll try to figure that out soon and post the result here.
Where do I collect my noob badge? :(
I did not continue with BlueZ 4. I upgraded to BlueZ 5 and PulseAudio 5, which work better together than the previous versions. After, I was able to finally use the pactl loopback module. (See this post for more details.)

Data-driven approach to networking in C. This can't be new?

I'd like to know whether something like this has been done before:
I've recently started work on a networking library in C. The library maintains a set of sockets, each of which is associated with two FIFO byte streams, input and output.
A developer using the library is expected to register some callbacks, consisting of a recognizer function and a handler function. If new data arrives on a socket (i.e. the input stream), every recognizer is called. If one of the recognizers finds a matching portion of data, its associated handler is called, consuming the data and possibly queuing new data on the socket's output stream, scheduled to be transmitted later on.
Here's an example to make clear how the library is used:
// create client socket
client = nc_create(NC_CLIENT);
// register some callback functions that you'll have to supply yourself
nc_register_callback(client, &is_login, &on_login);
nc_register_callback(client, &is_password, &on_password);
// connect to server
nc_dial(client, "www.google.com", "23");
// start main loop (we might as well have more than one connection here)
nc_talk();
To me, this is the most obvious way to write a general purpose networking library in C. I did some research using Google, but I wasn't able to find something similar written in C. But it's hard to believe that I'm the first one that implements this approach.
Are there other data-driven general purpose C networking libraries like this out there?
Would you use them?
Here's a few libraries that provides similar APIs, (at various levels, e.g. libevent provides a general callback driven API for socket/file descriptors)
libesmtp (example)
libevent
libcurl
The Sun/OncRPC APIs have a similar style, in that the library does the heavy lifting for you, dispatching requests to the proper callback handlers.
The Java netty and mina libraries works in a similar manner, although more object oriented.

Resources