-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinfocast_sensors.cpp
147 lines (111 loc) · 3.43 KB
/
infocast_sensors.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
* infocast - Make robots visible
* Copyright 2012-2013 Tobias Kalbitz.
* Subject to the AGPL, version 3.
*/
#include "infocast_sensors.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <linux/wireless.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <limits>
#include <alcore/alerror.h>
#include <alproxies/almemoryproxy.h>
#include "infocast.h"
uint8_t get_cpu_temperature()
{
static char filename[] = "/proc/acpi/thermal_zone/THRM/temperature";
int temp = 0;
FILE* fp = fopen(filename, "rb");
if(fp == NULL) {
perror("Error opening file for temperature");
fprintf(stderr, "File was %s\n", filename);
fprintf(stderr, "");
return 0;
}
const int rc = fscanf(fp, "temperature:%*[ ]%d", &temp);
fclose(fp);
if(rc == EOF || rc == 0) {
fprintf(stderr, "Error parsing temperature. Please review the format.\n");
return 0;
}
return (uint8_t)temp;
}
uint8_t get_wifi_quality()
{
int rc, sock;
struct iwreq iwr;
struct iw_statistics stats;
char ifname[] = "wlan0";
memset(&stats, 0, sizeof(stats));
memset(&iwr, 0, sizeof(iwr));
strncpy(iwr.ifr_name, ifname, sizeof(ifname));
iwr.u.data.pointer = &stats;
iwr.u.data.length = sizeof(stats);
iwr.u.data.flags = 1;
if((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
perror("Couldn't create wireless stat socket");
exit(EXIT_FAILURE);
}
rc = ioctl(sock, SIOCGIWSTATS, &iwr);
close(sock);
if(rc != 0) {
return 0;
}
return stats.qual.qual;
}
void get_ip_address(const char* device, uint32_t& ip, unsigned char* mac)
{
int sock;
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
sock = socket(AF_INET, SOCK_DGRAM, 0);
if(sock < 0) {
perror("Error creating socket");
fprintf(stderr, "Error: creating socket in file %s line %d. Exiting.\n", __FILE__, __LINE__);
exit(EXIT_FAILURE);
}
/* I want to get an IPv4 IP address */
ifr.ifr_addr.sa_family = AF_INET;
/* I want IP address attached to "eth0" */
strncpy(ifr.ifr_name, device, IFNAMSIZ-1);
if(ioctl(sock, SIOCGIFADDR, &ifr) == -1) {
perror("get_ip_address: Error calling ioctl-1");
ip = std::numeric_limits<uint32_t>::max();
mac[0] = mac[1] = mac[2] = mac[3] = mac[4] = mac[5] = 0xff;
close(sock);
return;
}
ip = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr;
if(ioctl(sock, SIOCGIFHWADDR, &ifr) == -1) {
perror("get_ip_address: Error calling ioctl-2");
mac[0] = mac[1] = mac[2] = mac[3] = mac[4] = mac[5] = 0xff;
close(sock);
return;
}
memcpy(mac, ifr.ifr_hwaddr.sa_data, 6);
close(sock);
}
template <typename T> int sgn(T val)
{
return (T(0) < val) - (val < T(0));
}
float get_battery_info()
{
static const char* const batteryCurrent = "Device/SubDeviceList/Battery/Current/Sensor/Value";
static const char* const batteryCharge = "Device/SubDeviceList/Battery/Charge/Sensor/Value";
float bat = 0.f;
try {
AL::ALMemoryProxy memory("localhost", NAOQI_PORT);
bat = (float)memory.getData(batteryCharge) * sgn((float)memory.getData(batteryCurrent));
} catch(const AL::ALError& e) {
std::cerr << "Caught exception: " << e.what() << std::endl;
}
return bat;
}