/*
 * ----------------------------------------------------------------------------
 * "THE BEER-WARE LICENSE" (Revision 42):
 * <fredde at shapeshifter dot se> wrote this file. As long as you retain this
 * notice you can do whatever you want with this stuff. If we meet some day,
 * and you think this stuff is worth it, you can buy me a beer in return.
 * Fredrik Lindberg
 * ----------------------------------------------------------------------------
 */

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

#include <unistd.h>

#include <sys/socket.h>
#include <sys/types.h>
#include <sys/ioctl.h>

#include <errno.h>

#include <net/if.h>
#include <net/if_var.h>
#include <net/ethernet.h>

#include <dev/wi/if_wavelan_ieee.h>

int _get_strength(char *, int *, u_int);

/*
 * This is function is called from main_loop to
 * get the card readings.
 * This makes it easy to implement this onto
 * another platform, just make sure get_strength 
 * returns the current wavelan strength.
 */

int get_strength(char *iface, u_int opt)
{
	int retval = 0;
	if (_get_strength(iface, &retval, opt) == -1)
		retval = 0;

	return retval;
}

int _get_strength(char *iface, int *retval, u_int opt)
{
	struct ifreq ifr;
	struct wi_req req;
	int sock;
	int f_ret;

	req.wi_type = WI_RID_COMMS_QUALITY;
	req.wi_len = WI_MAX_DATALEN;
	strlcpy(ifr.ifr_name, iface, sizeof(ifr.ifr_name)); 
	ifr.ifr_data = (caddr_t)&req;

	if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) != -1)
	{
		if ((ioctl(sock, SIOCGWAVELAN, &ifr)) != -1) 
		{
			*retval = req.wi_val[opt]; /* quality/signal/noise */
			f_ret = 0;
		}
		else {
			f_ret = -1;
		}

		close(sock);
	}
	else {
		fprintf(stderr, "socket: %s\n", strerror(errno));
		f_ret = -1;	
	} 

	return f_ret;
} 


syntax highlighted by Code2HTML, v. 0.9.1