I have two programs, one for client and one for server. They can talk to each other with a chat using Threads.
After I added two buttons, one for client and one for server to send file from server to client.
When I connect the client to server I start the Thread which waits to receive something with recv() function.
The problem is that when I want to send a file to client because the thread is awake the transfer does not work.
When I do not use threads the transfer works.
My question is: Can I from a button click to stop that Thread?
PS: I am a begginer in Threads
This is my code with Threads:
delegate System::Void PrintStringDel(String^ str);
void PrintString(String^ str) {
txtReceive->Text += str;
txtReceive->SelectionStart = txtReceive->Text->Length;
txtReceive->ScrollToCaret();
}
void ReceiveThread() {
int ByteReceived;
char buff[1024];
while(true) {
ByteReceived = recv(SendingSocket, buff, sizeof(buff), 0);
if(ByteReceived > 0) {
PrintStringDel^ print = gcnew PrintStringDel(this, &Form1::PrintString);
String^ text = gcnew String(buff);
txtReceive->BeginInvoke(print, text);
}
}
}
on Connect() client
...
Threading::ThreadStart^ ts = gcnew Threading::ThreadStart(this,
&Form1::ReceiveThread);
Threading::Thread^ t = gcnew Threading::Thread(ts);
t->Start();
I'm following this tutorial,
http://energia.nu/creating-an-iot-connected-sensor-with-energia-mqtt/
I see the pushed data, but the Node-RED editor constantly prints 'Hello World #XX'. I don't see anything in the code that would suggest where its coming from:
#include <WiFi.h>
#include <PubSubClient.h>
#include <SPI.h> //only required if using an MCU LaunchPad + CC3100 BoosterPack. Not needed for CC3200 LaunchPad
WiFiClient wclient;
byte server[] = { 198, 41, 30, 241 }; // Public MQTT Brokers: http://mqtt.org/wiki/doku.php/public_brokers
byte ip[] = { 172, 16, 0, 100 };
char sensorRead[4];
#define WIFI_SSID "SSID"
#define WIFI_PWD "WIFIPASSWORD"
PubSubClient client(server, 1883, callback, wclient);
void callback(char* inTopic, byte* payload, unsigned int length){
// Handle callback here
}
void setup()
{
//Initialize serial and wait for port to open:
Serial.begin(115200);
Serial.println("Start WiFi");
WiFi.begin(WIFI_SSID, WIFI_PWD);
while(WiFi.localIP() == INADDR_NONE) {
Serial.print(".");
delay(300);
}
Serial.println("");
printWifiStatus();
}
void loop()
{
// read the input on analog pin:
int sensorValue = analogRead(24);
Serial.println(sensorValue);
// convert into to char array
String str = (String)sensorValue;
int str_len = str.length() + 1; // Length (with one extra character for the null terminator)
char char_array[str_len]; // Prepare the character array (the buffer)
str.toCharArray(char_array, str_len); // Copy it over
// publish data to MQTT broker
if (client.connect("LaunchPadClient")) {
client.publish("outTopic", char_array);
//client.subscribe("inTopic");
Serial.println("Publishing successful!");
client.disconnect();
}
}
void printWifiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
}
Is this because this is a free/trial account? Incidentally, it shows I'm using 512MB/2GB, which seems high... does it include the data sent, or is 512MB just the application size?
You are using a MQTT broker that is public to the world, anybody can publish data to any topic on that broker. The messages are probably coming from somebody else doing similar experiments to yourself.
outTopic is the sort of topic name that many people could be using to test, try changing it to some random string in both the publishing code and the MQTT In node in Node-RED.
As for the size in Bluemix, this is how much memory is assigned to your application, it is unlikely to be actually using anything near that amount at the moment.
I'm trying to send BULK data using BULK end point. But each time I submit the URB, it never fires the URB Callback function until I disconnect the device. Once I disconnect, it fires the callback throwing an EPROTO error. Anyone faced similar condition? Am I doing something incorrectly? I'm sure my device and data cable is okay. Here goes my code. Please suggest possible solution
static void urb_received(struct urb *urb)
{
printk("URB Received\n");
switch (urb->status) {
case 0:
printk("case SUCCESS\n");
break;
case -ETIMEDOUT:
printk("case ETIMEDOUT\n");
return;
case -ENOENT:
printk("case ENONET\n");
return;
case -EPROTO:
printk("case EPROTO\n");
return;
case -ECONNRESET:
printk("case ECONNRESET\n");
return;
case -ESHUTDOWN:
printk("case ESHUTDOWN\n");
return;
default:
printk("unknown urb status %i\n", urb->status);
}
printk("URB Received Exit\n");
}
static int myprobe(struct usb_interface *intf,
const struct usb_device_id *id)
{
static int intfcount = 0;
int ret = 0;
int i;
struct device *dev = &intf->dev;
struct mydevice *mydev;
struct usb_device *usb_dev = interface_to_usbdev(intf);
struct usb_host_interface *interface_descriptor;
struct usb_endpoint_descriptor *endpoint;
if (id->idVendor == MY_DEVICE_ID && id->idProduct == MY_PRODUCT_ID){
interface_descriptor = intf->cur_altsetting;
mydev = kzalloc(sizeof(struct mydev), GFP_KERNEL);
mydev->dev = dev;
mydev->udev = usb_get_dev(interface_to_usbdev(intf));
interface_descriptor = intf->cur_altsetting;
for(i=0; i<interface_descriptor->desc.bNumEndpoints; i++){
endpoint = &interface_descriptor->endpoint[i].desc;
if (((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) &&
(endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_BULK
){
printk("MEDEV: Bulk out endpoint");
mydev->bulk_out_endpointAddr = endpoint->bEndpointAddress;
break;
}
}
bulk_urb = usb_alloc_urb(0, GFP_ATOMIC);
bulk_urb->transfer_buffer = kzalloc(8, GFP_ATOMIC);
usb_fill_bulk_urb(bulk_urb, usb_dev, usb_sndbulkpipe(mydev->udev, endpoint->bEndpointAddress),
bulk_urb->transfer_buffer, 8,
urb_received, NULL);
printk("BULK OUT PIPE = %d\n", mydev->bulk_out_pipe);
ret = usb_submit_urb(bulk_urb, GFP_ATOMIC);
return 0;
}
}
Just found out one interesting & same time annoying thing.
If in the above code I declare the usb_fill_bulk_urb with buffer_length=4, then it works perfectly. I can get the data from my android device. Even though I'm creating a buffer size of 8 bytes, I can't use buffer_length=8. I also played with other number but seems it only works for buffer_length=4.
To be sure I checked the endpoint descriptor of my device using lsusb -v command which shows it has a capacity of 512 bytes. Also in my android app, I've taken care off how many bytes I want to read. There is nothing wrong with the android app. I just can't understand why I won't be able to send more than 4 bytes at a time. By the way, if I send less than 4 bytes, that also works.
I'm accessing a video server with Live555 that is streaming to another program. I want to send rtsp PAUSE and PLAY commands to the video server to stop streaming to any other program. Is this possible? My code does not seem to do anything. I can connect to the server and the server verifies that I have received a full PAUSE command:
VideoServer.h
//must make this store session so we can access the session in the static
//callbacks
class MyRTSPClient: public RTSPClient{
protected:
MyRTSPClient(UsageEnvironment& env, char const* rtspURL,
int verbosityLevel, char const* applicationName, portNumBits tunnelOverHTTPPortNum):
RTSPClient(env, rtspURL, verbosityLevel, applicationName, tunnelOverHTTPPortNum)
{
}
public:
MediaSession* session_;
bool sessionStarted_;
static MyRTSPClient* createNew(UsageEnvironment& env, char const* rtspURL,
int verbosityLevel = 0,
char const* applicationName = NULL,
portNumBits tunnelOverHTTPPortNum = 0)
{
return new MyRTSPClient(env, rtspURL, verbosityLevel, applicationName, tunnelOverHTTPPortNum);
}
};
class VideoServer
{
public:
VideoServer();
private:
TaskScheduler* scheduler_;
UsageEnvironment* env_;
MyRTSPClient* rtspClient_;
char eventLoopWatchVariable;
//Asynchronously start the connection
void StartConnection();
static void callbackDESCRIBE(RTSPClient* rtspClient, int resultCode, char* resultString);
static void callbackPAUSEPLAY(RTSPClient* rtspClient, int resultCode, char* resultString);
};
VideoServer.cpp
VideoServer::VideoServer()
{
eventLoopWatchVariable = 0;
scheduler_ = BasicTaskScheduler::createNew();
env_ = BasicUsageEnvironment::createNew(*scheduler_);
//create rtsp client with default params and our url and environment
rtspClient_ = MyRTSPClient::createNew(*env_,
MINI_HARV_AXIS_RTSP_URL, 4, "jtalon5");
//call description to initialize the session
if (rtspClient_ == NULL) {
std::cout << "Failed to create a RTSP client for URL \"" <<
MINI_HARV_AXIS_RTSP_URL << std::endl;
return;
}
std::cout << "made the client!" << std::endl;
// Next, send a RTSP "DESCRIBE" command, to get a SDP description for the stream.
// Note that this command - like all RTSP commands - is sent asynchronously; we do not block, waiting for a response.
// Instead, the following function call returns immediately, and we handle the RTSP response later, from within the event loop:
rtspClient_->sendDescribeCommand(callbackDESCRIBE);
//start doEventLoop in separate thread so it is not blocking
boost::thread thr1(&MiniHarvAxisInterface::StartConnection, this);
}
void VideoServer::StartConnection()
{
env_->taskScheduler().doEventLoop(&eventLoopWatchVariable);
}
void MiniHarvAxisInterface::callbackDESCRIBE(RTSPClient* rtspClient, int resultCode, char* resultString)
{
std::cout << "describe" << resultString << std::endl;
UsageEnvironment& env = rtspClient->envir();
char* const sdpDescription = resultString;
((MyRTSPClient*)rtspClient)->session_ = MediaSession::createNew(env, sdpDescription);
((MyRTSPClient*)rtspClient)->sessionStarted_ = true;
if(((MyRTSPClient*)rtspClient)->session_ == NULL)
std::cout << "did not create session" << std::endl;
else
std::cout << "created session" << std::endl;
rtspClient->sendPauseCommand(*((MyRTSPClient*)rtspClient)->session_, &callbackPAUSEPLAY);
}
void MiniHarvAxisInterface::callbackPAUSEPLAY(RTSPClient* rtspClient, int resultCode, char* resultString)
{
//do nothing
}
It seems as if I can only pause and play a stream that I create in this process. Is this the case using Live555?
Yes this is possible in Live555.
By default Live555 does nothing unless you redefine the pauseStream function (see comment in pauseStream function in Live555 source below):
// default implementation: do nothing
You must create your own session class and you must redefine the pauseStream function as shown in the example below:
Your media session .h file should look something like:
#include <MediaSubsession.hh>
class YOURMediaSubsession: public MediaSubsession {
... //You can leave this empty if you like
private:
...
virtual void pauseStream(unsigned /*clientSessionId*/,void* /*streamToken*/);
};
Your media session .cpp file should look something like:
#include <YOURMediaSubsession.hh>
void YOURMediaSubsession::pauseStream(unsigned /*clientSessionId*/,
void* /*streamToken*/) {
// default implementation: do nothing
}
The you can add whatever you like in this function, whether it may be stopping/terminating all streams, or getting the encoder to keep encoding and the same frame to give the illusion of having the stream paused for a pre-set amount of time is all up to you.
Note that I can see in your code that you are using a default MediaSession class as shown in your code here:
((MyRTSPClient*)rtspClient)->session_ = MediaSession::createNew(env, sdpDescription);
You will need to construct your own YOURMediaSubsession class based on the MediaSession class to redefine the pauseStream function as shown above then YOU do the pausing, not live555. It should look more like:
((MyRTSPClient*)rtspClient)->session_ = YOURMediaSubsession::createNew(env, sdpDescription);
I'm trying to write some code to communicate with wpa_supplicant using DBUS. As I'm working in an embedded system (ARM), I'd like to avoid the use of Python or the GLib. I'm wondering if I'm stupid because I really have the feeling that there is no nice and clear documentation about D-Bus. Even with the official one, I either find the documentation too high level, or the examples shown are using Glib! Documentation I've looked at: http://www.freedesktop.org/wiki/Software/dbus
I found a nice article about using D-Bus in C: http://www.matthew.ath.cx/articles/dbus
However, this article is pretty old and not complete enough! I also found the c++-dbus API but also here, I don't find ANY documentation! I've been digging into wpa_supplicant and NetworkManager source code but it's quite a nightmare! I've been looking into the "low-level D-Bus API" as well but this doesn't tell me how to extract a string parameter from a D-Bus message! http://dbus.freedesktop.org/doc/api/html/index.html
Here is some code I wrote to test a little but I really have trouble to extract string values. Sorry for the long source code but if someone want to try it ... My D-Bus configuration seems fine because it "already" catches "StateChanged" signals from wpa_supplicant but cannot print the state:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <dbus/dbus.h>
//#include "wpa_supp_dbus.h"
/* Content of wpa_supp_dbus.h */
#define WPAS_DBUS_SERVICE "fi.epitest.hostap.WPASupplicant"
#define WPAS_DBUS_PATH "/fi/epitest/hostap/WPASupplicant"
#define WPAS_DBUS_INTERFACE "fi.epitest.hostap.WPASupplicant"
#define WPAS_DBUS_PATH_INTERFACES WPAS_DBUS_PATH "/Interfaces"
#define WPAS_DBUS_IFACE_INTERFACE WPAS_DBUS_INTERFACE ".Interface"
#define WPAS_DBUS_NETWORKS_PART "Networks"
#define WPAS_DBUS_IFACE_NETWORK WPAS_DBUS_INTERFACE ".Network"
#define WPAS_DBUS_BSSIDS_PART "BSSIDs"
#define WPAS_DBUS_IFACE_BSSID WPAS_DBUS_INTERFACE ".BSSID"
int running = 1;
void stopLoop(int sig)
{
running = 0;
}
void sendScan()
{
// TODO !
}
void loop(DBusConnection* conn)
{
DBusMessage* msg;
DBusMessageIter args;
DBusMessageIter subArgs;
int argType;
int i;
int buffSize = 1024;
char strValue[buffSize];
const char* member = 0;
sendScan();
while (running)
{
// non blocking read of the next available message
dbus_connection_read_write(conn, 0);
msg = dbus_connection_pop_message(conn);
// loop again if we haven't read a message
if (!msg)
{
printf("No message received, waiting a little ...\n");
sleep(1);
continue;
}
else printf("Got a message, will analyze it ...\n");
// Print the message member
printf("Got message for interface %s\n",
dbus_message_get_interface(msg));
member = dbus_message_get_member(msg);
if(member) printf("Got message member %s\n", member);
// Check has argument
if (!dbus_message_iter_init(msg, &args))
{
printf("Message has no argument\n");
continue;
}
else
{
// Go through arguments
while(1)
{
argType = dbus_message_iter_get_arg_type(&args);
if (argType == DBUS_TYPE_STRING)
{
printf("Got string argument, extracting ...\n");
/* FIXME : got weird characters
dbus_message_iter_get_basic(&args, &strValue);
*/
/* FIXME : segmentation fault !
dbus_message_iter_get_fixed_array(
&args, &strValue, buffSize);
*/
/* FIXME : segmentation fault !
dbus_message_iter_recurse(&args, &subArgs);
*/
/* FIXME : deprecated!
if(dbus_message_iter_get_array_len(&args) > buffSize)
printf("message content to big for local buffer!");
*/
//printf("String value was %s\n", strValue);
}
else
printf("Arg type not implemented yet !\n");
if(dbus_message_iter_has_next(&args))
dbus_message_iter_next(&args);
else break;
}
printf("No more arguments!\n");
}
// free the message
dbus_message_unref(msg);
}
}
int main(int argc, char* argv[])
{
DBusError err;
DBusConnection* conn;
int ret;
char signalDesc[1024]; // Signal description as string
// Signal handling
signal(SIGKILL, stopLoop);
signal(SIGTERM, stopLoop);
// Initialize err struct
dbus_error_init(&err);
// connect to the bus
conn = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
if (dbus_error_is_set(&err))
{
fprintf(stderr, "Connection Error (%s)\n", err.message);
dbus_error_free(&err);
}
if (!conn)
{
exit(1);
}
// request a name on the bus
ret = dbus_bus_request_name(conn, WPAS_DBUS_SERVICE, 0, &err);
if (dbus_error_is_set(&err))
{
fprintf(stderr, "Name Error (%s)\n", err.message);
dbus_error_free(&err);
}
/* Connect to signal */
// Interface signal ..
sprintf(signalDesc, "type='signal',interface='%s'",
WPAS_DBUS_IFACE_INTERFACE);
dbus_bus_add_match(conn, signalDesc, &err);
dbus_connection_flush(conn);
if (dbus_error_is_set(&err))
{
fprintf(stderr, "Match Error (%s)\n", err.message);
exit(1);
}
// Network signal ..
sprintf(signalDesc, "type='signal',interface='%s'",
WPAS_DBUS_IFACE_NETWORK);
dbus_bus_add_match(conn, signalDesc, &err);
dbus_connection_flush(conn);
if (dbus_error_is_set(&err))
{
fprintf(stderr, "Match Error (%s)\n", err.message);
exit(1);
}
// Bssid signal ..
sprintf(signalDesc, "type='signal',interface='%s'",
WPAS_DBUS_IFACE_BSSID);
dbus_bus_add_match(conn, signalDesc, &err);
dbus_connection_flush(conn);
if (dbus_error_is_set(&err))
{
fprintf(stderr, "Match Error (%s)\n", err.message);
exit(1);
}
// Do main loop
loop(conn);
// Main loop exited
printf("Main loop stopped, exiting ...\n");
dbus_connection_close(conn);
return 0;
}
Any pointer to any nice, complete, low-level C tutorial is strongly appreciated! I'm also planning to do some remote method call, so if the tutorial covers this subject it would be great! Saying I'm not very smart because I don't get it with the official tutorial is also appreciated :-p!
Or is there another way to communicate with wpa_supplicant (except using wpa_cli)?
EDIT 1:
Using 'qdbusviewer' and the introspection capabilty, this helped me a lot discovering what and how wpa_supplicant works using dbus. Hopping that this would help someone else!
Edit 2:
Will probably come when I'll find a way to read string values on D-Bus!
You have given up the tools that would help you to learn D-Bus more easily and are using the low level libdbus implementation, so maybe you deserve to be in pain. BTW, are you talking about ARM, like a cell phone ARM ? With maybe 500 Mhz and 256 MB RAM ? In this case the processor is well suited to using glib, Qt or even python. And D-Bus is most useful when you're writing asynchronous event driven code, with an integrated main loop, for example from glib, even when you're using the low level libdbus (it has functions to connect to the glib main loop, for example).
Since you're using the low level library, then documentation is what you already have:
http://dbus.freedesktop.org/doc/api/html/index.html
Also, libdbus source code is also part of the documentation:
http://dbus.freedesktop.org/doc/api/html/files.html
The main entry point for the documentation is the Modules page (in particular, the public API section):
http://dbus.freedesktop.org/doc/api/html/modules.html
For message handling, the section DBusMessage is the relevant one:
DBusMessage
There you have the documentation for functions that parse item values. In your case, you started with a dbus_message_iter_get_basic. As described in the docs, retrieving the string requires a const char ** variable, since the returned value will point to the pre-allocated string in the received message:
So for int32 it should be a "dbus_int32_t*" and for string a "const char**". The returned value is by reference and should not be freed.
So you can't define an array, because libdbus won't copy the text to your array. If you need to save the string, first get the constant string reference, then strcpy to your own array.
Then you tried to get a fixed array without moving the iterator. You need a call to the next iterator (dbus_message_iter_next) between the basic string and the fixed array. Same right before recursing into the sub iterator.
Finally, you don't call get_array_len to get the number of elements on the array. From the docs, it only returns byte counts. Instead you loop over the sub iterator using iter_next the same way you should have done with the main iterator. After you have iterated past the end of the array, dbus_message_iter_get_arg_type will return DBUS_TYPE_INVALID.
For more info, read the reference manual, don't look for a tutorial. Or just use a reasonable d-bus implementation:
https://developer.gnome.org/gio/2.36/gdbus-codegen.html
GIO's GDBus automatically creates wrappers for your d-bus calls.
http://qt-project.org/doc/qt-4.8/intro-to-dbus.html
http://dbus.freedesktop.org/doc/dbus-python/doc/tutorial.html
etc.
You don't need to use/understand working of dbus If you just need to write a C program to communicate with wpa_supplicant. I reverse engineered the wpa_cli's source code. Went through its implementation and used functions provided in wpa_ctrl.h/c. This implementation takes care of everything. You can use/modify whatever you want, build your executable and you're done!
Here's the official link to wpa_supplicant's ctrl_interface:
http://hostap.epitest.fi/wpa_supplicant/devel/ctrl_iface_page.html
I doubt this answer will still be relevant to the author of this question,
but for anybody who stumbles upon this like I did:
The situation is now better than all those years ago if you don't want to include GTK/QT in your project to access dbus.
There is dbus API in Embedded Linux Library by Intel (weird I remember it being open, maybe it is just for registered users now?)
and systemd sd-bus library now offers public API. You probably run systemd anyway unless you have a really constrained embedded system.
I have worked with GDbus, dbus-cpp and sd-bus and although I wanted a C++ library,
I found sd-bus to be the simplest and the least problematic experience.
I did not try its C++ bindings but they also look nice
#include <stdio.h>
#include <systemd/sd-bus.h>
#include <stdlib.h>
const char* wpa_service = "fi.w1.wpa_supplicant1";
const char* wpa_root_obj_path = "/fi/w1/wpa_supplicant1";
const char* wpa_root_iface = "fi.w1.wpa_supplicant1";
sd_bus_error error = SD_BUS_ERROR_NULL;
sd_bus* system_bus = NULL;
sd_event* loop = NULL;
sd_bus_message* reply = NULL;
void cleanup() {
sd_event_unref(loop);
sd_bus_unref(system_bus);
sd_bus_message_unref(reply);
sd_bus_error_free(&error);
}
void print_error(const char* msg, int code) {
fprintf(stderr, "%s %s\n", msg, strerror(-code));
exit(EXIT_FAILURE);
}
const char* get_interface(const char* iface) {
int res = sd_bus_call_method(system_bus,
wpa_service,
wpa_root_obj_path,
wpa_root_iface,
"GetInterface",
&error,
&reply,
"s",
"Ifname", "s", iface,
"Driver", "s", "nl80211");
if (res < 0) {
fprintf(stderr, "(get) error response: %s\n", error.message);
return NULL;
}
const char* iface_path;
/*
* an object path was returned in reply
* this works like an iterator, if a method returns (osu), you could call message_read_basic in succession
* with arguments SD_BUS_TYPE_OBJECT_PATH, SD_BUS_TYPE_STRING, SD_BUS_TYPE_UINT32 or you could
* call sd_bus_message_read() and provides the signature + arguments in one call
* */
res = sd_bus_message_read_basic(reply, SD_BUS_TYPE_OBJECT_PATH, &iface_path);
if (res < 0) {
print_error("getIface: ", res);
return NULL;
}
return iface_path;
}
const char* create_interface(const char* iface) {
int res = sd_bus_call_method(system_bus,
wpa_service,
wpa_root_obj_path,
wpa_root_iface,
"CreateInterface",
&error,
&reply,
"a{sv}", 2, //pass array of str:variant (dbus dictionary) with 2
//entries to CreateInterface
"Ifname", "s", iface, // "s" variant parameter contains string, then pass the value
"Driver", "s", "nl80211");
if (res < 0) {
fprintf(stderr, "(create) error response: %s\n", error.message);
return NULL;
}
const char* iface_path;
res = sd_bus_message_read_basic(reply, SD_BUS_TYPE_OBJECT_PATH, &iface_path);
if (res < 0) {
print_error("createIface: ", res);
}
return iface_path;
}
int main() {
int res;
const char* iface_path;
//open connection to system bus - default either opens or reuses existing connection as necessary
res = sd_bus_default_system(&system_bus);
if (res < 0) {
print_error("open: ", res);
}
//associate connection with event loop, again default either creates or reuses existing
res = sd_event_default(&loop);
if (res < 0) {
print_error("event: ", res);
}
// get obj. path to the wireless interface on dbus so you can call methods on it
// this is a wireless interface (e.g. your wifi dongle) NOT the dbus interface
// if you don't know the interface name in advance, you will have to read the Interfaces property of
// wpa_supplicants root interface — call Get method on org.freedesktop.DBus properties interface,
// while some libraries expose some kind of get_property convenience function sd-bus does not
const char* ifaceName = "wlp32s0f3u2";
if (!(iface_path = get_interface(ifaceName))) { //substitute your wireless iface here
// sometimes the HW is present and listed in "ip l" but dbus does not reflect that, this fixes it
if (!(iface_path = create_interface(ifaceName))) {
fprintf(stderr, "can't create iface: %s" , ifaceName);
cleanup();
return EXIT_FAILURE;
}
}
/*
call methods with obj. path iface_path and dbus interface of your choice
this will likely be "fi.w1.wpa_supplicant1.Interface", register for signals etc...
you will need the following to receive those signals
*/
int runForUsec = 1000000; //usec, not msec!
sd_event_run(loop, runForUsec); //or sd_event_loop(loop) if you want to loop forever
cleanup();
printf("Finished OK\n");
return 0;
}
I apologize if the example above does not work perfectly. It is an excerpt from an old project I rewrote to C from C++ (I think it's C(-ish), compiler does not protest and you asked for C) but I can't test it as all my dongles refuse to work with my desktop right now. It should give you a general idea though.
Note that you will likely encounter several magical or semi-magical issues.
To ensure smooth developing/testing do the following:
make sure other network management applications are disabled (networkmanager, connman...)
restart the wpa_supplicant service
make sure the wireless interface is UP in ip link
Also, because is not that well-documented right now:
You can access arrays and inner variant values by sd_bus_message_enter_container
and _exit counterpart. sd_bus_message_peek_type might come handy while doing that.
Or sd_bus_message_read_array for a homogenous array.
The below snippet works for me
if (argType == DBUS_TYPE_STRING)
{
printf("Got string argument, extracting ...\n");
char* strBuffer = NULL;
dbus_message_iter_get_basic(&args, &strBuffer);
printf("Received string: \n %s \n",strBuffer);
}