Re: ipw srcipts for hal



Bastien Nocera wrote:
On Sun, 2007-06-24 at 22:15 +0200, dragoran wrote:
here is a C implementation of the ipwWirelessCtl
the udi is hardcoded and I only implemented get for now (will add the
rest tomorrow). It also uses all values from the README.
yelo_3 you have a typo in your patch (you used getrfkill instead of
set ;) )
Bastien: the UDI that will get passed to the script would be that of
the killswitch corret?

Yes, and it will be passed as a envvar, as in the other scripts.

ok this one uses the envvar passed to it.
programm is attached... any comments?

As this will live in HAL, it should have a better name (Dan was only
mimicking the name of the Dell rfkill-helper).

ok can you suggest a better name?
ipwkillswitch-helper ?

#include <libhal.h>
#include <stdio.h>
#include <string.h>
#include <glib.h>

static LibHalContext *hal_ctx;

int main(int argc,char** argv) {

	DBusError error;

	char *udi;
	char *parent;
	char *iface;
	int i;
	char kill_status;
	char **udis;
	int num_udis;
	FILE *fd;
	char *path;
	int ret = -1;

	if(argc == 1) return -1;	

	dbus_error_init (&error);
	
	if((udi = getenv ("HAL_PROP_INFO_UDI")) == NULL) return -1;

	if ((hal_ctx = libhal_ctx_new ()) == NULL) {
		fprintf (stderr, "error: libhal_ctx_new\n");
		return -1;
	}

	if (!libhal_ctx_set_dbus_connection (hal_ctx, dbus_bus_get (DBUS_BUS_SYSTEM, &error))) {
		fprintf (stderr, "error: libhal_ctx_set_dbus_connection: %s: %s\n", error.name, error.message);
		LIBHAL_FREE_DBUS_ERROR (&error);
		return -1;
	}

	if (!libhal_ctx_init (hal_ctx, &error)) {
		fprintf (stderr, "error: libhal_ctx_init: %s: %s\n", error.name, error.message);
		LIBHAL_FREE_DBUS_ERROR (&error);
		return -1;
	}


	parent = libhal_device_get_property_string (hal_ctx, udi, "info.parent", &error);
	udis = libhal_manager_find_device_string_match (hal_ctx, "info.parent", parent, &num_udis, &error);
	libhal_free_string (parent);
	
	if(argc==2 && strcmp("getrfkill",argv[1])==0) {

		for (i = 0; i < num_udis; i++) {

			if(strcmp (udis[i], udi) == 0) continue;
			iface = libhal_device_get_property_string (hal_ctx, udis[i], "net.interface", &error);
			path = g_strdup_printf ("/sys/class/net/%s/device/rf_kill", iface);
			if((fd=fopen (path, "r")) == NULL) return -1;
			kill_status = fgetc (fd);
			fclose (fd);
			g_free (path);

			switch(kill_status) {
				case '0':
					ret =  1;
				break;
				case '1':
				case '2':
				case '3':
					ret = 0;
				break;
			}

			libhal_free_string (iface);
		}
	
		libhal_free_string_array (udis);
	}

	if(argc == 3 && strcmp ("setrfkill", argv[1]) == 0 && (atoi (argv[2]) == 0 || atoi(argv[2]) == 1)) {

		for (i = 0; i < num_udis; i++) {

			if(strcmp (udis[i], udi)==0) continue;
			iface = libhal_device_get_property_string (hal_ctx, udis[i], "net.interface", &error);
			path = g_strdup_printf ("/sys/class/net/%s/device/rf_kill", iface);
			if((fd=fopen (path, "w")) == NULL) return -1;
			fputc (argv[2][0], fd);
			fclose (fd);
			g_free (path);
			libhal_free_string (iface);
		}
	
		libhal_free_string_array (udis);	

		ret = 0;
	}
	
	libhal_ctx_free (ctx);	

	if (dbus_error_is_set (&error)) {
		fprintf (stderr, "error: %s: %s\n", error.name, error.message);
		dbus_error_free (&error);
		return -1;
	}

	return ret;
}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]