diff --git a/machines/agb02/agb/agb.cpp b/machines/agb02/agb/agb.cpp index 008cfba..904badb 100644 --- a/machines/agb02/agb/agb.cpp +++ b/machines/agb02/agb/agb.cpp @@ -1,6 +1,6 @@ -#include -#include -#include +#include +#include +#include #include #include #include @@ -11,8 +11,7 @@ using namespace std::literals::chrono_literals; constexpr std::chrono::microseconds debounce = 40ms; constexpr std::chrono::microseconds poll_period = 5ms; constexpr std::chrono::microseconds server_ratelimit = 50ms; - -constexpr const char* url = "https://agb.hackens.org/api/control-box"; +constexpr std::chrono::microseconds retry_timeout = 500ms; constexpr std::pair joystick_movement(1.0, 1.0); @@ -20,14 +19,14 @@ const gpiod::line::offsets drive_down = { 21, 13, 6 }; const gpiod::line::offsets decoder = { 2, 3, 4, 14, 15, 18, 23, 24 }; // lsbf const gpiod::line::offsets joystick = { 0, 0, 0, 0 }; // x+, y+, x-, y- -const gpiod::line::offset black_button = 0; -const gpiod::line::offset white_button = 0; +const gpiod::line::offset black_button = 16; +const gpiod::line::offset white_button = 20; const gpiod::line_settings input_settings = gpiod::line_settings() .set_direction(gpiod::line::direction::INPUT) .set_bias(gpiod::line::bias::PULL_UP) - .set_active_low(true) + .set_active_low(false) .set_debounce_period(debounce); constexpr std::array decoder_table = @@ -46,8 +45,8 @@ inline void clamp_decoder(uint8_t& decoder, int move){ } int main(const int argc, char const* const* const argv) { - if(argc < 3) { - std::cerr << "usage: agb gpiodevice tokenfile" << std::endl; + if(argc < 2) { + std::cerr << "usage: agb gpiodevice" << std::endl; return 1; } @@ -64,10 +63,11 @@ int main(const int argc, char const* const* const argv) { .set_output_value(gpiod::line::value::INACTIVE) ) .add_line_settings({ black_button, white_button }, input_settings) - .add_line_settings(joystick, input_settings) + .add_line_settings(joystick, + gpiod::line_settings(input_settings) + .set_active_low(true)) .add_line_settings(decoder, gpiod::line_settings(input_settings) - .set_active_low(false) .set_debounce_period(0ms)) .do_request(); @@ -76,15 +76,21 @@ int main(const int argc, char const* const* const argv) { /// init server communication /// - cURLpp::initialize(); - std::string token; + int socket_file_desc = socket(AF_INET, SOCK_STREAM, 0); { - std::ifstream tokenFile(argv[2]); - std::getline(tokenFile, token); + sockaddr_in socket_addr = { + .sin_family = AF_INET, + .sin_port = htons(1235), + .sin_addr = { .s_addr = inet_addr("10.10.10.1") } + }; + int con_ret = connect(socket_file_desc, + reinterpret_cast(&socket_addr), + sizeof(socket_addr)); + if(con_ret < 0) { + std::cerr << "Failed to open tcp socket." << std::endl; + return 1; + } } - std::list header; - header.push_back("Content-Type: application/json"); - header.push_back("Authorization: Bearer " + token); /// internal state and buffers /// @@ -97,7 +103,7 @@ int main(const int argc, char const* const* const argv) { bool white_pressed = false; bool black_pressed = false; - bool has_changed = false; + bool has_changed = true; std::chrono::time_point last_send = std::chrono::system_clock::now(); std::string postData; @@ -150,10 +156,6 @@ int main(const int argc, char const* const* const argv) { /// server notification std::chrono::time_point now = std::chrono::system_clock::now(); if(has_changed && (now - last_send > server_ratelimit)){ - curlpp::Easy request; - request.setOpt(curlpp::options::Url(url)); - request.setOpt(curlpp::options::HttpHeader(header)); - postData.clear(); std::format_to(std::back_inserter(postData), "{{" "\"pan\": {}," @@ -161,22 +163,22 @@ int main(const int argc, char const* const* const argv) { "\"focus\": {}," "\"whiteButton\": {}," "\"blackButton\": {}" - "}}", + "}}\n", spot_pos.first, spot_pos.second, int(decoder_pos), white_pressed, black_pressed ); - request.setOpt(curlpp::options::PostFields(postData)); - request.setOpt(curlpp::options::PostFieldSize(postData.size())); - //request.perform(); -// std::cout << std::chrono::system_clock::now() - now << std::endl; - std::cout << postData << std::endl; - - has_changed = false; - last_send = now; + int wrote = write(socket_file_desc, postData.data(), postData.size()); + if(wrote < postData.size()){ + std::cerr << "Failed to send data, retrying in " << retry_timeout << "." << std::endl; + last_send = now + retry_timeout - server_ratelimit; + } else { + has_changed = false; + last_send = now; + } } } } diff --git a/machines/agb02/agb/default.nix b/machines/agb02/agb/default.nix index 5b83cb3..71a318a 100644 --- a/machines/agb02/agb/default.nix +++ b/machines/agb02/agb/default.nix @@ -1,4 +1,4 @@ -{ stdenv, libgpiod, curlpp, curl }: +{ stdenv, libgpiod }: stdenv.mkDerivation rec { pname = "agb"; version = "oct-24"; @@ -7,8 +7,6 @@ stdenv.mkDerivation rec { buildPhase = '' g++ --std=c++23 agb.cpp -o agb \ -L${libgpiod}/lib -lgpiodcxx -I${libgpiod}/include \ - -L${curl.out}/lib -lcurl -I${curl.dev}/include \ - -L${curlpp}/lib -lcurlpp -I${curlpp}/include ''; installPhase = '' mkdir -p $out/bin diff --git a/machines/agb02/secrets/default.nix b/machines/agb02/secrets/default.nix index 6abb220..14efc4b 100644 --- a/machines/agb02/secrets/default.nix +++ b/machines/agb02/secrets/default.nix @@ -8,8 +8,4 @@ file = ./wg.age; owner = "systemd-network"; }; - age.secrets."token" = { - file = ./token.age; - # owner = "systemd-network"; - }; } diff --git a/machines/agb02/secrets/secrets.nix b/machines/agb02/secrets/secrets.nix index 3ec47cb..c9744b6 100644 --- a/machines/agb02/secrets/secrets.nix +++ b/machines/agb02/secrets/secrets.nix @@ -5,5 +5,4 @@ let (builtins.readFile (../../../pubkeys + "/${user}.keys"))); in { "wg.age".publicKeys = (readPubkeys "catvayor") ++ (readPubkeys "sinavir") ++ (readPubkeys "agb02"); - "token.age".publicKeys = (readPubkeys "catvayor") ++ (readPubkeys "sinavir") ++ (readPubkeys "agb02"); } diff --git a/machines/agb02/secrets/token.age b/machines/agb02/secrets/token.age deleted file mode 100644 index 4365b46..0000000 Binary files a/machines/agb02/secrets/token.age and /dev/null differ diff --git a/meta.nix b/meta.nix index 281ff89..506322d 100644 --- a/meta.nix +++ b/meta.nix @@ -41,7 +41,7 @@ let agb02 = { deployment = { - targetHost = null; + targetHost = "10.10.10.6"; }; arch = "aarch64-linux"; imports = [ agenix ];