Skip to content

Commit 856e23d

Browse files
committed
Update configServer for 2023
1 parent 0fb5710 commit 856e23d

17 files changed

+229
-217
lines changed

deps/tools/configServer/Makefile

+17-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
11
IMG_VERSION?=$(shell git describe)
2-
DEPS_CFLAGS?=$(shell pkg-config --cflags cscore wpiutil) -std=c++17
3-
DEPS_LIBS?=$(shell pkg-config --libs --static cscore wpiutil)
2+
3+
# For a desktop build with the allwpilib source tree:
4+
#ALLWPILIB=/home/peter/project/frc/allwpilib
5+
#DEPS_CFLAGS= \
6+
-I${ALLWPILIB}/wpiutil/src/main/native/include \
7+
-I${ALLWPILIB}/wpinet/src/main/native/include \
8+
-I${ALLWPILIB}/cscore/src/main/native/include \
9+
-I${ALLWPILIB}/wpiutil/src/main/native/thirdparty/llvm/include \
10+
-I${ALLWPILIB}/wpiutil/src/main/native/thirdparty/sigslot/include \
11+
-I${ALLWPILIB}/wpiutil/src/main/native/thirdparty/fmtlib/include \
12+
-I${ALLWPILIB}/wpiutil/src/main/native/thirdparty/json/include \
13+
-I${ALLWPILIB}/wpinet/src/main/native/thirdparty/libuv/include
14+
#DEPS_LIBS=-L${ALLWPILIB}/build-ninja/lib -lcscored -lwpinetd -lwpiutild
15+
16+
DEPS_CFLAGS?=$(shell pkg-config --cflags cscore wpinet wpiutil)
17+
DEPS_LIBS?=$(shell pkg-config --libs --static cscore wpinet wpiutil)
418
EXEC_HOME?=/home/pi
519
FRC_JSON?=/boot/frc.json
620
DHCPCD_CONF?=/boot/dhcpcd.conf
@@ -11,6 +25,7 @@ APP_UID?=1000
1125
APP_GID?=1000
1226
ROMI_JSON?=/boot/romi.json
1327
NODE_HOME?=/home/pi/.nvm/versions/node/v14.15.0/bin
28+
CXXFLAGS?=-std=c++20
1429

1530
.PHONY: all clean install
1631
.SUFFIXES:

deps/tools/configServer/src/Application.cpp

+14-21
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
#include <unistd.h>
1010

1111
#include <fmt/format.h>
12+
#include <wpi/SmallString.h>
1213
#include <wpi/StringExtras.h>
14+
#include <wpi/fmt/raw_ostream.h>
1315
#include <wpi/fs.h>
1416
#include <wpi/json.h>
1517
#include <wpi/raw_istream.h>
@@ -56,11 +58,7 @@ void Application::Set(std::string_view appType,
5658
} else if (appType == "custom") {
5759
return;
5860
} else {
59-
wpi::SmallString<64> msg;
60-
msg = "unrecognized application type '";
61-
msg += appType;
62-
msg += "'";
63-
onFail(msg);
61+
onFail(fmt::format("unrecognized application type '{}'", appType));
6462
return;
6563
}
6664

@@ -72,13 +70,13 @@ void Application::Set(std::string_view appType,
7270
onFail("could not write " EXEC_HOME "/runCamera");
7371
return;
7472
}
75-
os << "#!/bin/sh\n";
76-
os << TYPE_TAG << ' ' << appType << '\n';
77-
os << "echo \"Waiting 5 seconds...\"\n";
78-
os << "sleep 5\n";
79-
if (!appDir.empty()) os << "cd " << appDir << '\n';
80-
if (!appEnv.empty()) os << appEnv << '\n';
81-
os << "exec " << appCommand << '\n';
73+
fmt::print(os, "#!/bin/sh\n");
74+
fmt::print(os, "{} {}\n", TYPE_TAG, appType);
75+
fmt::print(os, "echo \"Waiting 5 seconds...\"\n");
76+
fmt::print(os, "sleep 5\n");
77+
if (!appDir.empty()) fmt::print(os, "cd {}\n", appDir);
78+
if (!appEnv.empty()) fmt::print(os, "{}\n", appEnv);
79+
fmt::print(os, "exec {}\n", appCommand);
8280
}
8381

8482
// terminate vision process so it reloads
@@ -97,18 +95,11 @@ void Application::FinishUpload(std::string_view appType, UploadHelper& helper,
9795
} else if (appType == "upload-python") {
9896
filename = "/uploaded.py";
9997
} else {
100-
wpi::SmallString<64> msg;
101-
msg = "cannot upload application type '";
102-
msg += appType;
103-
msg += "'";
104-
onFail(msg);
98+
onFail(fmt::format("cannot upload application type '{}'", appType));
10599
helper.Close();
106100
return;
107101
}
108102

109-
wpi::SmallString<64> pathname;
110-
pathname = EXEC_HOME;
111-
pathname += filename;
112103
int fd = helper.GetFD();
113104

114105
// change ownership
@@ -126,6 +117,8 @@ void Application::FinishUpload(std::string_view appType, UploadHelper& helper,
126117
// close temporary file
127118
helper.Close();
128119

120+
auto pathname = fmt::format("{}{}", EXEC_HOME, filename);
121+
129122
// remove old file (need to do this as we can't overwrite a running exe)
130123
if (unlink(pathname.c_str()) == -1) {
131124
fmt::print(stderr, "could not remove app executable: {}\n",
@@ -151,7 +144,7 @@ wpi::json Application::GetStatusJson() {
151144
std::error_code ec;
152145
wpi::raw_fd_istream is(EXEC_HOME "/runCamera", ec);
153146
if (ec) {
154-
fmt::print(stderr, "{}", "could not read " EXEC_HOME "/runCamera\n");
147+
fmt::print(stderr, "could not read {}/runCamera\n", EXEC_HOME);
155148
return j;
156149
}
157150

deps/tools/configServer/src/MyHttpConnection.cpp

+13-6
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@
99

1010
#include <fmt/format.h>
1111
#include <wpi/SmallVector.h>
12-
#include <wpi/UrlParser.h>
12+
#include <wpi/StringExtras.h>
1313
#include <wpi/fs.h>
1414
#include <wpi/raw_ostream.h>
15-
#include <wpi/raw_uv_ostream.h>
16-
#include <wpi/uv/Request.h>
15+
#include <wpinet/UrlParser.h>
16+
#include <wpinet/raw_uv_ostream.h>
17+
#include <wpinet/uv/Request.h>
1718

1819
#include "WebSocketHandlers.h"
1920

@@ -40,7 +41,7 @@ MyHttpConnection::MyHttpConnection(std::shared_ptr<wpi::uv::Stream> stream)
4041
: HttpServerConnection(stream), m_websocketHelper(m_request) {
4142
// Handle upgrade event
4243
m_websocketHelper.upgrade.connect([this] {
43-
//fmt::print(stderr, "{}", "got websocket upgrade\n");
44+
//fmt::print(stderr, "got websocket upgrade\n");
4445
// Disconnect HttpServerConnection header reader
4546
m_dataConn.disconnect();
4647
m_messageCompleteConn.disconnect();
@@ -55,7 +56,7 @@ MyHttpConnection::MyHttpConnection(std::shared_ptr<wpi::uv::Stream> stream)
5556
// Connect the websocket open event to our connected event.
5657
// Pass self to delay destruction until this callback happens
5758
ws->open.connect_extended([self, s = ws.get()](auto conn, auto) {
58-
fmt::print(stderr, "{}", "websocket connected\n");
59+
fmt::print(stderr, "websocket connected\n");
5960
InitWs(*s);
6061
conn.disconnect(); // one-shot
6162
});
@@ -132,11 +133,17 @@ void MyHttpConnection::SendFileResponse(int code, std::string_view codeText,
132133
std::string_view extraHeader) {
133134
// open file
134135
std::error_code ec;
135-
int infd = fs::OpenFileForRead(filename, ec);
136+
auto infile = fs::OpenFileForRead(filename, ec);
136137
if (ec) {
137138
SendError(404);
138139
return;
139140
}
141+
int infd = fs::FileToFd(infile, ec, fs::OF_None);
142+
if (ec) {
143+
fs::CloseFile(infile);
144+
SendError(404);
145+
return;
146+
}
140147

141148
// get file size
142149
auto size = fs::file_size(filename, ec);

deps/tools/configServer/src/MyHttpConnection.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
#include <memory>
99
#include <string_view>
1010

11-
#include <wpi/HttpServerConnection.h>
12-
#include <wpi/WebSocketServer.h>
13-
#include <wpi/uv/Stream.h>
11+
#include <wpinet/HttpServerConnection.h>
12+
#include <wpinet/WebSocketServer.h>
13+
#include <wpinet/uv/Stream.h>
1414

1515
class MyHttpConnection : public wpi::HttpServerConnection,
1616
public std::enable_shared_from_this<MyHttpConnection> {

0 commit comments

Comments
 (0)