DisplayIpAddress.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005 #include <stdio.h>
00006 #include <string.h>
00007 #include <unistd.h>
00008 #include <fcntl.h>
00009 #include <errno.h>
00010 #include <termios.h>
00011 #include <time.h>
00012 #include <iostream>
00013 #include <string>
00014 #include <sys/types.h>
00015 #include <ifaddrs.h>
00016 #include <netinet/in.h>
00017 #include <arpa/inet.h>
00018 #include <cstdlib>
00019
00020 enum displayline {
00021 line2 = 0x02,
00022 line3 = 0x03
00023 };
00024
00025 enum voltagesource {
00026 battery1 = 0x04,
00027 battery2 = 0x05,
00028 powersupply = 0x0c
00029 };
00030
00031 int open_port(std::string port) {
00032 int fd;
00033
00034 fd = open(port.c_str(), O_RDWR | O_NOCTTY | O_NDELAY);
00035
00036 if (fd == -1)
00037 {
00038 throw ("unable to open port: " + port);
00039 } else {
00040 fcntl(fd, F_SETFL, 0);
00041 printf("port is open.\n");
00042 }
00043
00044 return (fd);
00045 }
00046
00047 int configure_port(int fd)
00048 {
00049 struct termios port_settings;
00050
00051 tcgetattr(fd, &port_settings);
00052
00053 cfsetispeed(&port_settings, B0);
00054 cfsetospeed(&port_settings, B0);
00055
00056
00057 port_settings.c_cflag |= (CLOCAL | CREAD);
00058
00059
00060 port_settings.c_cflag &= ~CRTSCTS;
00061
00062 port_settings.c_cflag &= ~PARENB;
00063 port_settings.c_cflag &= ~CSTOPB;
00064 port_settings.c_cflag &= ~CSIZE;
00065 port_settings.c_cflag |= CS8;
00066
00067 port_settings.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
00068 port_settings.c_iflag &= ~(IXON | IXOFF | IXANY);
00069 port_settings.c_oflag &= ~OPOST;
00070 port_settings.c_cc[VMIN] = 0;
00071 port_settings.c_cc[VTIME] = 10;
00072
00073 tcsetattr(fd, TCSANOW, &port_settings);
00074 return (fd);
00075
00076 }
00077
00078 bool setText(int fd, displayline line, std::string text) {
00079 const int size = 18;
00080 if (text.size() > size - 2) {
00081 printf("The text is to long!\n");
00082 return false;
00083 }
00084
00085 unsigned char send_bytes[size];
00086
00087 send_bytes[0] = line;
00088 for (unsigned int i = 0; i < size - 2; i++) {
00089 if (i < text.size())
00090 send_bytes[i + 1] = text[i];
00091 else
00092 send_bytes[i + 1] = ' ';
00093 }
00094 send_bytes[size - 1] = 0x0D;
00095
00096 write(fd, send_bytes, size);
00097 printf("[");
00098 for (int i = 1; i < size - 1; i++) {
00099 printf("%c", send_bytes[i]);
00100 }
00101 printf("]\n");
00102
00103 return true;
00104 }
00105
00106
00107 double getVoltage(int fd, voltagesource source) {
00108 unsigned char send_bytes[1];
00109 send_bytes[0] = source;
00110
00111 write(fd, send_bytes, 1);
00112
00113 const int readsize = 20;
00114 char read_bytes[readsize] = {0};
00115
00116 int nobytesread = 0;
00117 nobytesread = read(fd, read_bytes, readsize);
00118 read_bytes[nobytesread-1] = 0;
00119 read_bytes[nobytesread-2] = 0;
00120
00121 std::string value(read_bytes);
00122
00123 return (double)atoi(value.c_str())/1000;
00124 }
00125
00126 void getIPAdress(std::string lanname, std::string wlanname, std::string& lanip, std::string& wlanip) {
00127 struct ifaddrs * ifAddrStruct = NULL;
00128 struct ifaddrs * ifa = NULL;
00129 void * tmpAddrPtr = NULL;
00130 lanip = "L";
00131 wlanip = "W";
00132 getifaddrs(&ifAddrStruct);
00133
00134 for (ifa = ifAddrStruct; ifa != NULL; ifa = ifa->ifa_next) {
00135 if (ifa ->ifa_addr->sa_family == AF_INET) {
00136
00137 tmpAddrPtr = &((struct sockaddr_in *) ifa->ifa_addr)->sin_addr;
00138 char addressBuffer[INET_ADDRSTRLEN];
00139 inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN);
00140
00141 std::string interface(ifa->ifa_name);
00142 if (interface == lanname)
00143 lanip.append(addressBuffer);
00144 if (interface == wlanname)
00145 wlanip.append(addressBuffer);
00146 }
00147 }
00148 if (ifAddrStruct != NULL) freeifaddrs(ifAddrStruct);
00149
00150 return;
00151 }
00152
00153 int main(int argc, char* argv[]) {
00154 try {
00155 if (argc != 4) {
00156 std::cout << "invalid arguments \n ./displayipaddress 'serial port' 'ethernet' 'wlan' \n./displayipaddress /dev/ttyACM0 eth1 wlan0" << std::endl;
00157 return 0;
00158 }
00159 int fd = open_port(argv[1]);
00160 configure_port(fd);
00161
00162 std::string lanip;
00163 std::string wlanip;
00164
00165 while (true) {
00166 sleep(2);
00167 getIPAdress(argv[2], argv[3], lanip, wlanip);
00168 setText(fd, line2, lanip);
00169 setText(fd, line3, wlanip);
00170
00171
00172
00173 }
00174
00175 close(fd);
00176 } catch (std::string &e) {
00177 std::cout << "Error: " << e << std::endl;
00178 }
00179 return (0);
00180 }
00181