Re: ipw srcipts for hal





On 6/25/07, yelo_3 <yelo_3 yahoo it> wrote:
> ok, but this does not solve the problem of multiple killswitches (will
> show up with multiple cards) because both will have
> /org/freedesktop/Hal/devices/ipw_wlan_switch as uid.
Yes the previous shell script didn't solve the problem... sorry. This might mean that the UDI should contain the interface name as you were saying
The C code misses the setrfkill section, and a !=null check when you fopen the file.

ok here is a new version.
it implements setrfkill too and uses g_strdup_printf instead of sprintf.


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

static LibHalContext *hal_ctx;

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

	DBusError error;

	char *udi="/org/freedesktop/Hal/devices/ipw_wlan_switch";
	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 ((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;
	}
	
	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]