feat(agb02): protocol change

This commit is contained in:
catvayor 2024-10-13 10:42:30 +02:00
parent 012e5fb772
commit 8de7773129
Signed by: lbailly
GPG key ID: CE3E645251AC63F3
6 changed files with 37 additions and 42 deletions

View file

@ -1,6 +1,6 @@
#include <curlpp/cURLpp.hpp> #include <sys/socket.h>
#include <curlpp/Easy.hpp> #include <sys/types.h>
#include <curlpp/Options.hpp> #include <arpa/inet.h>
#include <gpiod.hpp> #include <gpiod.hpp>
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
@ -11,8 +11,7 @@ using namespace std::literals::chrono_literals;
constexpr std::chrono::microseconds debounce = 40ms; constexpr std::chrono::microseconds debounce = 40ms;
constexpr std::chrono::microseconds poll_period = 5ms; constexpr std::chrono::microseconds poll_period = 5ms;
constexpr std::chrono::microseconds server_ratelimit = 50ms; constexpr std::chrono::microseconds server_ratelimit = 50ms;
constexpr std::chrono::microseconds retry_timeout = 500ms;
constexpr const char* url = "https://agb.hackens.org/api/control-box";
constexpr std::pair<double, double> joystick_movement(1.0, 1.0); constexpr std::pair<double, double> 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 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::offsets joystick = { 0, 0, 0, 0 }; // x+, y+, x-, y-
const gpiod::line::offset black_button = 0; const gpiod::line::offset black_button = 16;
const gpiod::line::offset white_button = 0; const gpiod::line::offset white_button = 20;
const gpiod::line_settings input_settings = const gpiod::line_settings input_settings =
gpiod::line_settings() gpiod::line_settings()
.set_direction(gpiod::line::direction::INPUT) .set_direction(gpiod::line::direction::INPUT)
.set_bias(gpiod::line::bias::PULL_UP) .set_bias(gpiod::line::bias::PULL_UP)
.set_active_low(true) .set_active_low(false)
.set_debounce_period(debounce); .set_debounce_period(debounce);
constexpr std::array<uint8_t, 256> decoder_table = constexpr std::array<uint8_t, 256> 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) { int main(const int argc, char const* const* const argv) {
if(argc < 3) { if(argc < 2) {
std::cerr << "usage: agb gpiodevice tokenfile" << std::endl; std::cerr << "usage: agb gpiodevice" << std::endl;
return 1; return 1;
} }
@ -64,10 +63,11 @@ int main(const int argc, char const* const* const argv) {
.set_output_value(gpiod::line::value::INACTIVE) .set_output_value(gpiod::line::value::INACTIVE)
) )
.add_line_settings({ black_button, white_button }, input_settings) .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, .add_line_settings(decoder,
gpiod::line_settings(input_settings) gpiod::line_settings(input_settings)
.set_active_low(false)
.set_debounce_period(0ms)) .set_debounce_period(0ms))
.do_request(); .do_request();
@ -76,15 +76,21 @@ int main(const int argc, char const* const* const argv) {
/// init server communication /// /// init server communication ///
cURLpp::initialize(); int socket_file_desc = socket(AF_INET, SOCK_STREAM, 0);
std::string token;
{ {
std::ifstream tokenFile(argv[2]); sockaddr_in socket_addr = {
std::getline(tokenFile, token); .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<const sockaddr*>(&socket_addr),
sizeof(socket_addr));
if(con_ret < 0) {
std::cerr << "Failed to open tcp socket." << std::endl;
return 1;
}
} }
std::list<std::string> header;
header.push_back("Content-Type: application/json");
header.push_back("Authorization: Bearer " + token);
/// internal state and buffers /// /// internal state and buffers ///
@ -97,7 +103,7 @@ int main(const int argc, char const* const* const argv) {
bool white_pressed = false; bool white_pressed = false;
bool black_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::chrono::time_point last_send = std::chrono::system_clock::now();
std::string postData; std::string postData;
@ -150,10 +156,6 @@ int main(const int argc, char const* const* const argv) {
/// server notification /// server notification
std::chrono::time_point now = std::chrono::system_clock::now(); std::chrono::time_point now = std::chrono::system_clock::now();
if(has_changed && (now - last_send > server_ratelimit)){ 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(); postData.clear();
std::format_to(std::back_inserter(postData), "{{" std::format_to(std::back_inserter(postData), "{{"
"\"pan\": {}," "\"pan\": {},"
@ -161,22 +163,22 @@ int main(const int argc, char const* const* const argv) {
"\"focus\": {}," "\"focus\": {},"
"\"whiteButton\": {}," "\"whiteButton\": {},"
"\"blackButton\": {}" "\"blackButton\": {}"
"}}", "}}\n",
spot_pos.first, spot_pos.first,
spot_pos.second, spot_pos.second,
int(decoder_pos), int(decoder_pos),
white_pressed, white_pressed,
black_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;
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; has_changed = false;
last_send = now; last_send = now;
} }
} }
} }
}

View file

@ -1,4 +1,4 @@
{ stdenv, libgpiod, curlpp, curl }: { stdenv, libgpiod }:
stdenv.mkDerivation rec { stdenv.mkDerivation rec {
pname = "agb"; pname = "agb";
version = "oct-24"; version = "oct-24";
@ -7,8 +7,6 @@ stdenv.mkDerivation rec {
buildPhase = '' buildPhase = ''
g++ --std=c++23 agb.cpp -o agb \ g++ --std=c++23 agb.cpp -o agb \
-L${libgpiod}/lib -lgpiodcxx -I${libgpiod}/include \ -L${libgpiod}/lib -lgpiodcxx -I${libgpiod}/include \
-L${curl.out}/lib -lcurl -I${curl.dev}/include \
-L${curlpp}/lib -lcurlpp -I${curlpp}/include
''; '';
installPhase = '' installPhase = ''
mkdir -p $out/bin mkdir -p $out/bin

View file

@ -8,8 +8,4 @@
file = ./wg.age; file = ./wg.age;
owner = "systemd-network"; owner = "systemd-network";
}; };
age.secrets."token" = {
file = ./token.age;
# owner = "systemd-network";
};
} }

View file

@ -5,5 +5,4 @@ let
(builtins.readFile (../../../pubkeys + "/${user}.keys"))); (builtins.readFile (../../../pubkeys + "/${user}.keys")));
in { in {
"wg.age".publicKeys = (readPubkeys "catvayor") ++ (readPubkeys "sinavir") ++ (readPubkeys "agb02"); "wg.age".publicKeys = (readPubkeys "catvayor") ++ (readPubkeys "sinavir") ++ (readPubkeys "agb02");
"token.age".publicKeys = (readPubkeys "catvayor") ++ (readPubkeys "sinavir") ++ (readPubkeys "agb02");
} }

Binary file not shown.

View file

@ -41,7 +41,7 @@ let
agb02 = { agb02 = {
deployment = { deployment = {
targetHost = null; targetHost = "10.10.10.6";
}; };
arch = "aarch64-linux"; arch = "aarch64-linux";
imports = [ agenix ]; imports = [ agenix ];