Skip to content

Instantly share code, notes, and snippets.

@updateing
Created April 25, 2016 12:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save updateing/bd1df84b5742a1a72537f6df42a4af9a to your computer and use it in GitHub Desktop.
Save updateing/bd1df84b5742a1a72537f6df42a4af9a to your computer and use it in GitHub Desktop.
Program for reading temperature of Broadcom wireless adapters.
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <net/if.h>
#define WLC_IOCTL_SMLEN 256 /* "small" length ioctl buffer required */
#define WLC_GET_VAR 262 /* get value of named variable */
#define WLC_SET_VAR 263 /* set named variable to value */
typedef unsigned char uint8;
/* Linux network driver ioctl encoding */
typedef struct wl_ioctl {
uint cmd; /* common ioctl definition */
void *buf; /* pointer to user buffer */
uint len; /* length of user buffer */
uint8 set; /* 1=set IOCTL; 0=query IOCTL */
uint used; /* bytes read or written (optional) */
uint needed; /* bytes needed (optional) */
} wl_ioctl_t;
static int wl_ioctl(char *name, int cmd, void *buf, int len)
{
struct ifreq ifr;
wl_ioctl_t ioc;
int ret = 0;
int s;
/* open socket to kernel */
if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
perror("socket");
return errno;
}
/* do it */
ioc.cmd = cmd;
ioc.buf = buf;
ioc.len = len;
/* initializing the remaining fields */
ioc.set = 0;
ioc.used = 0;
ioc.needed = 0;
strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name) - 1);
ifr.ifr_name[sizeof(ifr.ifr_name) - 1] = '\0';
ifr.ifr_data = (caddr_t) &ioc;
if ((ret = ioctl(s, SIOCDEVPRIVATE, &ifr)) < 0)
perror("ioctl failed");
/* cleanup */
close(s);
return ret;
}
static unsigned int get_phy_temperature(char* interface)
{
int ret = 0;
unsigned int *temp;
char buf[WLC_IOCTL_SMLEN];
strcpy(buf, "phy_tempsense");
if ((ret = wl_ioctl(interface, WLC_GET_VAR, buf, sizeof(buf)))) {
return 0;
} else {
temp = (unsigned int *)buf;
return *temp / 2 + 20;
}
}
static void usage(char* self)
{
printf("USAGE: %s IFNAME\nMust be a wireless adapter.\n", self);
}
int main(int argc, char* argv[])
{
if (argc < 2) {
usage(argv[0]);
return EINVAL;
}
printf("%d\n", get_phy_temperature(argv[1]));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment