From 36e59a1991f075e36e117a08321d5e4c4dc00eac Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Fri, 7 Apr 2017 19:03:05 +0200 Subject: o Splitting capture into just two, capture and send to stdout and transmit from stdin. o Adding formatter that formats the incoming message. --- src/transmitter.cpp | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 src/transmitter.cpp (limited to 'src/transmitter.cpp') 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 +#include "wifi-triangulator/core.h" + +#include +#include +#include + +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(&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(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; +} -- cgit v1.2.3