summaryrefslogtreecommitdiff
path: root/src/devreader.cpp
diff options
context:
space:
mode:
authorOlivier Gayot <duskcoder@gmail.com>2014-06-18 15:45:45 +0200
committerOlivier Gayot <duskcoder@gmail.com>2014-06-18 15:49:38 +0200
commit0e03940802cebefdf6b0597a154bd9395e1af4d2 (patch)
tree409a58499128227dd57943515d003074190551f5 /src/devreader.cpp
Add the vanilla version of the project
This version can still be found here: http://www.roland-riegel.de/nload/index.html
Diffstat (limited to 'src/devreader.cpp')
-rw-r--r--src/devreader.cpp106
1 files changed, 106 insertions, 0 deletions
diff --git a/src/devreader.cpp b/src/devreader.cpp
new file mode 100644
index 0000000..5006309
--- /dev/null
+++ b/src/devreader.cpp
@@ -0,0 +1,106 @@
+/***************************************************************************
+ devreader.cpp
+ -------------------
+ begin : Fri Nov 16 2007
+ copyright : (C) 2007 - 2012 by Roland Riegel
+ email : feedback@roland-riegel.de
+ ***************************************************************************/
+
+/***************************************************************************
+ * *
+ * This program is free software; you can redistribute it and/or modify *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation; either version 2 of the License, or *
+ * (at your option) any later version. *
+ * *
+ ***************************************************************************/
+
+#include "devreader.h"
+
+#include <config.h>
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/ioctl.h>
+#include <sys/time.h>
+#include <time.h>
+#include <net/if.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <string.h>
+#include <unistd.h>
+
+#ifdef HAVE_SOLARIS
+ #include <sys/sockio.h>
+#endif
+
+using namespace std;
+
+DevReader::DevReader(const string& deviceName)
+ : m_deviceName(deviceName)
+{
+}
+
+DevReader::~DevReader()
+{
+}
+
+const string& DevReader::getDeviceName() const
+{
+ return m_deviceName;
+}
+
+DataFrame DevReader::getNewDataFrame()
+{
+ DataFrame deviceData;
+
+ struct timeval tv;
+ gettimeofday(&tv, 0);
+
+ readFromDevice(deviceData);
+
+ deviceData.setDeviceName(m_deviceName);
+
+ deviceData.setTimeStampSeconds((unsigned long) tv.tv_sec);
+ deviceData.setTimeStampMicroseconds((unsigned long) tv.tv_usec);
+
+ deviceData.setIpV4(getDeviceIp4Address());
+
+ return deviceData;
+}
+
+string DevReader::getDeviceIp4Address()
+{
+ struct ifreq ifr;
+ int sk;
+
+ string deviceIp = "";
+
+ if(m_deviceName.empty())
+ return deviceIp;
+
+ /* create a temporary socket: ioctl needs one */
+ if((sk = socket(AF_INET, SOCK_STREAM, 0)) < 0)
+ return deviceIp;
+
+ /* copy the device name into the ifreq structure */
+ strncpy(ifr.ifr_name, m_deviceName.c_str(), IFNAMSIZ - 1);
+ ifr.ifr_name[IFNAMSIZ - 1] = 0;
+
+ /* make the request */
+ if(!ioctl(sk, SIOCGIFADDR, &ifr))
+ {
+ struct sockaddr_in sin;
+ memcpy(&sin, &ifr.ifr_addr, sizeof(sin));
+
+ /* only use the IP number if the address family is really IPv4 */
+ if(sin.sin_family == AF_INET)
+ deviceIp = inet_ntoa(sin.sin_addr);
+ }
+
+ /* close the temporary socket */
+ close(sk);
+
+ return deviceIp;
+}
+