diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2017-04-07 19:03:05 +0200 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2017-04-07 19:03:05 +0200 |
commit | 36e59a1991f075e36e117a08321d5e4c4dc00eac (patch) | |
tree | 2c89d0b9ad67eb6ebdcd5ed4b1789ebc61ce6723 /src/transmitter.cpp | |
parent | 84939234eb66fe7957eaf39956f18224e3108c25 (diff) | |
download | wifi-triangulator-36e59a1991f075e36e117a08321d5e4c4dc00eac.tar.gz wifi-triangulator-36e59a1991f075e36e117a08321d5e4c4dc00eac.tar.bz2 wifi-triangulator-36e59a1991f075e36e117a08321d5e4c4dc00eac.tar.xz wifi-triangulator-36e59a1991f075e36e117a08321d5e4c4dc00eac.zip |
o Splitting capture into just two, capture and send to stdout and transmit from stdin.
o Adding formatter that formats the incoming message.
Diffstat (limited to 'src/transmitter.cpp')
-rw-r--r-- | src/transmitter.cpp | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/src/transmitter.cpp b/src/transmitter.cpp new file mode 100644 index 0000000..047d37d --- /dev/null +++ b/src/transmitter.cpp @@ -0,0 +1,79 @@ +#include <iostream> +#include "wifi-triangulator/core.h" + +#include <arpa/inet.h> +#include <cstring> +#include <unistd.h> + +using namespace std; +using namespace wifi_triangulator; + +class human_output { +public: + void handle(const data &data) { + printf("timestamp=%lu, rssi=%d, src=%s, dst=%s, type=%s\n", ((data.sec * 1000 * 1000) + data.usec) * 1000, + data.rssi, data.src.to_string().c_str(), data.dst.to_string().c_str(), to_string(data.type).c_str()); + fflush(stdout); + }; +}; + +int main(int argc, char *argv[]) { + if (argc != 2) { + fprintf(stderr, "usage: %s [host]\n", argv[0]); + return EXIT_FAILURE; + } + + string host = argv[1]; + + GOOGLE_PROTOBUF_VERIFY_VERSION; + + int s; + struct sockaddr_in addr_si; + struct sockaddr *addr; + socklen_t addr_len; + + if ((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) { + throw std::runtime_error("socket"); + } + + memset((char *) &addr_si, 0, sizeof(addr_si)); + addr_si.sin_family = AF_INET; + addr_si.sin_port = htons(3333); + if (inet_aton(host.c_str(), &addr_si.sin_addr) == 0) { + throw std::runtime_error("Could not resolve " + host); + } + + addr = reinterpret_cast<struct sockaddr *>(&addr_si); + addr_len = sizeof(addr_si); + + int count = 0; + uint8_t buffer[1 << 16]; + while (!feof(stdin)) { + pb::envelope envelope; + + uint16_t size; + + cin >> size; + cin.read(reinterpret_cast<char *>(buffer), size); + + /* + bool ok = envelope.ParseFromArray(buffer, size); + if (!ok) { + cerr << "bad read" << endl; + continue; + } + */ + + if (sendto(s, buffer, size, 0, addr, addr_len) == -1) { + throw std::runtime_error("sendto failed"); + } + + cerr << "count=" << count++ << endl; + } + + cerr << "transmitter exiting" << endl; + + google::protobuf::ShutdownProtobufLibrary(); + + return EXIT_SUCCESS; +} |