Re: ipw srcipts for hal





On 6/25/07, dragoran <drago01 gmail com> wrote:
here again with some cleanups.
btw the new fdi file seems not to work:
there is no ipw_killswitch under computer for me...
any idea why?

new one the same but fixed 2 memory leaks. (should be leakfree now)
#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 *iface;
	int i,j,k;
	int found=0;
	char kill_status;
	char **udis;
	char **ifaces;
	int num_udis,num_ifaces;
	FILE *fd;
	char *path;
	char ipw[3][8]={"ipw2100","ipw2200","ipw3945"};
	int ret=-1;

	if(argc==1 || argc > 3) {
		fprintf (stderr, "Usage: ipwWirelessCtl [getrfkill] [setrfkill [1|0]]\n");
		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;
	}


	if(argc==2 && strcmp("getrfkill",argv[1])==0) {
		
		for(i = 0; i < 3 ; i++) {

			udis = libhal_manager_find_device_string_match (hal_ctx, "info.linux.driver", ipw[i], &num_udis, &error);

			if(num_udis==0) {
				libhal_free_string_array (udis);
				continue;
			}

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

				ifaces = libhal_manager_find_device_string_match (hal_ctx, "info.parent", udis[j], &num_ifaces, &error);

				if(num_ifaces==0) {
					libhal_free_string_array (ifaces);
					continue;
				}
				else {
					found++;
				}

				for (k = 0; k < num_ifaces; k++) {

					iface = libhal_device_get_property_string (hal_ctx, ifaces[k], "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 '1':
						case '2':
						case '3':
							ret=1;
						break;
					}

					libhal_free_string(iface);
				}
				
				libhal_free_string_array (ifaces);
				
			}
			
			libhal_free_string_array (udis);			
			
		}
		
		if(found && ret!=1) ret=0;
					
	}

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

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

			udis = libhal_manager_find_device_string_match (hal_ctx, "info.linux.driver", ipw[i], &num_udis, &error);

			if(num_udis==0) {
				libhal_free_string_array (udis);
				continue;
			}

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

				ifaces = libhal_manager_find_device_string_match (hal_ctx, "info.parent", udis[j], &num_ifaces, &error);

				if(num_ifaces==0) {
					libhal_free_string_array (ifaces);
					continue;
				}
				else {
					found++;
				}

				for (k = 0; k < num_ifaces; k++) {

					iface = libhal_device_get_property_string (hal_ctx, ifaces[k], "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 (ifaces);				

			}
			
			libhal_free_string_array (udis);

		}
		
		if(found) 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]