hal_agent_new_device fails with d-bus errors
brendan powers
brendan0powers at gmail.com
Tue Dec 28 11:43:05 PST 2004
Hello,
I am attempting to write an agent that detects when a floppy has been
removed. However, i am having a problem with the hal_agent_new_device
function. My program pauses at this function an prints
"libhal.c 1612 : Error sending msg: Message did not receive a reply"
Other functions from libhal work perfectly, such as retrieving
properties of deiveces. So my connection to d-bus should be good.
Atached to this message is the source to my agent. It is not commented
well so here is the general flow of the program
Initialize the hal library context
start a thread to run the glib main loop
retrive the block device of the floppy drive(the hal device is passed
as a command line parameter)
loop forever
Check to see if we can open the floppy device
if we can and last loop we could not then there is a floppy in the drive
create a new temporary hal device
merge the flopy drivers parameters to this device
set the parnet and the RequireEnable property
make the new device available to the gdl
if the floppy was removed
remove the new device
This is just a proof of concept agent, so i really don't know what i'm doing:-p
-------------main.cpp---------------
#include <stdlib.h>
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <unistd.h>
#include <getopt.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#define DBUS_API_SUBJECT_TO_CHANGE
#include <glib.h>
#include <dbus/dbus-glib.h>
#include <dbus/dbus-glib-lowlevel.h>
#include <hal/libhal.h>
using namespace std;
GMainLoop *mainloop; //glib mainloop
static void mainloop_integration(LibHalContext *hal_ctx,DBusConnection *dc)
{
dbus_connection_setup_with_g_main(dc, NULL);
}
void *mainloop_thread(void *ptr)
{
cout << "Starting G Mainloop" << endl;
g_main_loop_run(mainloop);
}
int main(int argc, char *argv[])
{
LibHalFunctions
hal_functions={mainloop_integration,NULL,NULL,NULL,NULL,NULL,NULL};
pthread_t mthread;
static LibHalContext *hal_ctx; //the hal library context
mainloop= g_main_loop_new(NULL, false);
hal_ctx = hal_initialize(&hal_functions, false);
if(hal_ctx == NULL) {
cerr << "Failed to initialize hall library context" << endl;
exit(0);
}
//start the gmainloop thread
pthread_create(&mthread,NULL,mainloop_thread,NULL);
bool disk=false;
bool last=false;
char *device=hal_device_get_property_string(hal_ctx,argv[1],"block.device");
char *udi=new char[strlen(device)+2];
strcat(udi,"-0");
while(1) {
FILE *handle=fopen(device,"r");
last=disk;
if(handle == NULL) disk=false;
else disk=true;
if(disk != last) {
if(disk) {
cout << "Disk Insert" << endl;
cout << "Creating temporarty device" << endl;
char *tmpudi = hal_agent_new_device(hal_ctx);
cout << "Merging properties" << endl;
hal_agent_merge_properties(hal_ctx,tmpudi,argv[1]);
cout << "setting parent and enale state" << endl;
hal_device_set_property_string(hal_ctx,tmpudi,"info.parent",argv[1]);
hal_device_set_property_bool(hal_ctx,tmpudi,"RequireEnable",true);
cout << "Commiting Device" << endl;
hal_agent_commit_to_gdl(hal_ctx,tmpudi,udi);
cout << "Done" << endl;
hal_free_string(tmpudi);
}
else {
cout << "Disk Remove" << endl;
hal_agent_remove_device(hal_ctx,udi);
}
}
sleep(1);
}
hal_free_string(device);
delete udi;
return(0);
}
_______________________________________________
hal mailing list
hal at lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/hal
More information about the Hal
mailing list