thermferm/mqtt.c

changeset 628
32082a19912c
parent 616
e2b82881c13e
child 629
dab1a7450a42
equal deleted inserted replaced
627:6b31403b0662 628:32082a19912c
19 * along with ThermFerm; see the file COPYING. If not, write to the Free 19 * along with ThermFerm; see the file COPYING. If not, write to the Free
20 * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. 20 * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
21 *****************************************************************************/ 21 *****************************************************************************/
22 22
23 #include "thermferm.h" 23 #include "thermferm.h"
24 #include <sys/ioctl.h>
25 #include <linux/wireless.h>
24 #include "rdconfig.h" 26 #include "rdconfig.h"
25 #include "devices.h" 27 #include "devices.h"
26 #include "xutil.h" 28 #include "xutil.h"
27 #include "mqtt.h" 29 #include "mqtt.h"
28 30
1474 * Find our network information 1476 * Find our network information
1475 */ 1477 */
1476 FILE *f; 1478 FILE *f;
1477 char line[100], *ifname, *c, ip[NI_MAXHOST]; 1479 char line[100], *ifname, *c, ip[NI_MAXHOST];
1478 struct ifaddrs *ifaddr, *ifa; 1480 struct ifaddrs *ifaddr, *ifa;
1479 int family, s; 1481 int family, s, sock, rssi;
1480 1482 struct iwreq wrq;
1481 if (/*birth && */(f = fopen("/proc/net/route" , "r"))) { 1483 struct iw_statistics iwstats;
1484 char essid[IW_ESSID_MAX_SIZE+1];
1485
1486 if ((f = fopen("/proc/net/route" , "r"))) {
1482 while (fgets(line, 100, f)) { 1487 while (fgets(line, 100, f)) {
1483 ifname = strtok(line , " \t"); 1488 ifname = strtok(line , " \t");
1484 c = strtok(NULL , " \t"); 1489 c = strtok(NULL , " \t");
1485 1490
1486 // Take the entry with destination '00000000' 1491 // Take the entry with destination '00000000'
1507 } 1512 }
1508 payload = xstrcat(payload, (char *)",\"net\":{\"address\":\""); 1513 payload = xstrcat(payload, (char *)",\"net\":{\"address\":\"");
1509 payload = xstrcat(payload, ip); 1514 payload = xstrcat(payload, ip);
1510 payload = xstrcat(payload, (char *)"\",\"ifname\":\""); 1515 payload = xstrcat(payload, (char *)"\",\"ifname\":\"");
1511 payload = xstrcat(payload, ifname); 1516 payload = xstrcat(payload, ifname);
1512 payload = xstrcat(payload, (char *)"\",\"rssi\":0}"); 1517 payload = xstrcat(payload, (char *)"\"");
1513 // TODO: get rssi if wlan interface. 1518
1519 /*
1520 * Now detect WiFi on this interface.
1521 */
1522 strncpy(wrq.ifr_name, ifa->ifa_name, IFNAMSIZ-1);
1523
1524 if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
1525 syslog(LOG_NOTICE, "Error %d opening INET socket", errno);
1526 } else {
1527 if (ioctl(sock, SIOCGIWNAME, &wrq) == 0) {
1528 syslog(LOG_NOTICE, "IF: %s", wrq.ifr_name);
1529 }
1530
1531 wrq.u.essid.pointer = essid;
1532 if (ioctl(sock, SIOCGIWESSID, &wrq) == -1) {
1533 syslog(LOG_NOTICE, "Can't open socket to obtain essid");
1534 } else {
1535 payload = xstrcat(payload, (char *)",\"ssid\":\"");
1536 payload = xstrcat(payload, wrq.u.essid.pointer);
1537 payload = xstrcat(payload, (char *)"\"");
1538 }
1539
1540 memset(&iwstats, 0, sizeof(iwstats));
1541 wrq.u.data.pointer = &iwstats;
1542 wrq.u.data.length = sizeof(struct iw_statistics);
1543 wrq.u.data.flags = 1;
1544 if (ioctl(sock, SIOCGIWSTATS, &wrq) == -1) {
1545 syslog(LOG_NOTICE, "Can't open socket to obtain iwstats");
1546 } else {
1547 syslog(LOG_NOTICE, "Signal level is %d %d %d", iwstats.qual.updated & IW_QUAL_DBM, iwstats.qual.level, iwstats.qual.noise);
1548 if ((iwstats.qual.updated & (IW_QUAL_QUAL_INVALID | IW_QUAL_LEVEL_INVALID)) == 0) {
1549 /* iwstats.qual.level is __u8 */
1550 if (iwstats.qual.updated & IW_QUAL_DBM) {
1551 rssi = (iwstats.qual.level > 63) ? iwstats.qual.level - 0x100 : iwstats.qual.level;
1552 } else {
1553 /* level seems 0..100% */
1554 if (iwstats.qual.level > 90)
1555 rssi = -10;
1556 else
1557 rssi = (iwstats.qual.level - 100);
1558 }
1559 payload = xstrcat(payload, (char *)",\"rssi\":");
1560 sprintf(buf, "%d", rssi);
1561 payload = xstrcat(payload, buf);
1562 } else {
1563 syslog(LOG_NOTICE, "Signal level is invalid");
1564 }
1565 }
1566 }
1567 payload = xstrcat(payload, (char *)"}");
1514 } 1568 }
1515 } 1569 }
1516 1570
1517 freeifaddrs(ifaddr); 1571 freeifaddrs(ifaddr);
1518 } 1572 }
1520 fclose(f); 1574 fclose(f);
1521 } 1575 }
1522 neterr: 1576 neterr:
1523 1577
1524 payload = xstrcat(payload, (char *)"}}"); 1578 payload = xstrcat(payload, (char *)"}}");
1579 syslog(LOG_NOTICE, payload);
1525 if (birth) { 1580 if (birth) {
1526 topic = topic_base((char *)"NBIRTH"); 1581 topic = topic_base((char *)"NBIRTH");
1527 publisher(mosq, topic, payload, true); 1582 publisher(mosq, topic, payload, true);
1528 } else { 1583 } else {
1529 topic = topic_base((char *)"NDATA"); 1584 topic = topic_base((char *)"NDATA");

mercurial