Skip to content

Commit 826750e

Browse files
authored
Posix build (#1664)
* Changes to support Posix build target The Posix build can serve as a base for automated tests. The vast majority of the changes were extant problems caught by a pickier compiler, including * Use snprintf instead of sprintf for safety * Missing or spurious "override" on virtual methods * A couple of buffer safety issues * Nonportable use of variable length arrays * Case-correct spelling of freertos Task.H and Queue.h * Type mismatches in printf arguments I had to change the way that undefinedPin and errorPin were defined to ensure that they are initialized prior to being used, which showed up with a different compiler. The changes that were directly related to Posix were quite small. A WinConsole.cpp and ExceptionHelper.cpp were moved from capture/ to (new) windows-x86/, and posix/Console.cpp was created to handle keyboard input. * Spurious "private:" * Task.h and Queue.h should be lowercase * Still fixing capitalization * This time for sure * Reversed preliminary commit of Channel.cpp fix * Put errorPin and undefinedPin in the Pins namespace
1 parent 703d1e1 commit 826750e

53 files changed

Lines changed: 323 additions & 169 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

FluidNC/capture/AssertionFailed.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include "AssertionFailed.h"
55

6+
#include "stdio.h"
67
#include <cstdarg>
78
#include <cstring>
89

FluidNC/capture/WString.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
#include "WString.h"
22

3+
#include <cstdlib>
34
#include <iomanip>
45
#include <sstream>
56

67
#pragma warning(disable : 4996) // itoa
78

89
std::string String::ValueToString(int value, int base) {
9-
char buffer[100] = { 0 };
10-
int number_base = 10;
11-
std::string output = itoa(value, buffer, base);
10+
char buffer[100] = { 0 };
11+
std::to_chars_result result = std::to_chars(buffer, buffer + sizeof(buffer), value, base);
12+
std::string output(buffer, result.ptr);
1213
return output;
1314
}
1415

FluidNC/capture/fnc_uart.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ int uart_buflen(uint32_t uart_num) {
2929

3030
extern int inchar();
3131

32-
int uart_read(uint32_t uart_num, uint8_t* buf, size_t len, uint32_t timeout_ms) {
32+
int uart_read(uint32_t uart_num, uint8_t* buf, uint32_t len, uint32_t timeout_ms) {
3333
auto key = uart_key(uart_num);
3434
const auto& val = Inputs::instance().get(key);
3535
auto max = std::min(size_t(len), val.size());

FluidNC/capture/freertos/FreeRTOS.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#pragma once
22

3-
#include "Task.h"
4-
#include "Queue.h"
3+
#include "task.h"
4+
#include "queue.h"
55
#include "FreeRTOSTypes.h"
66
#include <mutex>
77
#include <atomic>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include "Queue.h"
1+
#include "queue.h"
22

33
#include <cstring>
44
#include <atomic>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#pragma once
22

3-
#include "Task.h"
3+
#include "task.h"
44
#include "FreeRTOSTypes.h"
55

66
#include <queue>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include "Task.h"
1+
#include "task.h"
22

33
#include "Capture.h"
44
#include "Arduino.h"

FluidNC/capture/localfs.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111
#include <filesystem>
1212
namespace stdfs = std::filesystem;
1313

14-
bool localfs_format(const char* fsname) {
14+
const char* localfsDir = "./localfs";
15+
const char* sdDir = "./sd";
16+
17+
bool localfs_format(const std::string fsname) {
1518
return false;
1619
}
1720
bool localfs_mount() {

FluidNC/posix/Console.cpp

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
#include <iostream>
2+
3+
#include <sys/ioctl.h>
4+
#include <termios.h>
5+
#include <unistd.h>
6+
#include <stdio.h>
7+
#include "Serial.h" // allChannels
8+
#include "Driver/Console.h"
9+
10+
#include "Channel.h"
11+
#include "lineedit.h"
12+
13+
static struct termios _orig_termios;
14+
15+
class PosixConsole : public Channel {
16+
private:
17+
Lineedit* _lineedit;
18+
19+
public:
20+
PosixConsole(bool addCR = false) : Channel("PosixConsole", addCR) {}
21+
22+
static void editModeOn() {
23+
// stdout changes?
24+
tcsetattr(STDIN_FILENO, TCSAFLUSH, &_orig_termios);
25+
}
26+
27+
static void editModeOff() {
28+
tcgetattr(STDIN_FILENO, &_orig_termios);
29+
struct termios raw = _orig_termios;
30+
31+
// input modes: no break, no CR to NL, no parity check, no strip char,
32+
// no start/stop output control.
33+
raw.c_iflag &= ~(BRKINT | /* ICRNL | */ INPCK | ISTRIP | IXON);
34+
35+
// output modes - disable post processing */
36+
// raw.c_oflag &= ~(OPOST);
37+
raw.c_oflag |= ONLCR;
38+
39+
// control modes - set 8 bit chars */
40+
raw.c_cflag |= (CS8);
41+
42+
// local modes - choing off, canonical off, no extended functions,
43+
// no signal chars (^Z,^C) */
44+
raw.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG);
45+
raw.c_lflag |= ISIG;
46+
47+
// control chars - set return condition: min number of bytes and timer.
48+
// We want read to return every single byte, without timeout. */
49+
raw.c_cc[VINTR] = 3; /* Ctrl-C interrupts process */
50+
// raw.c_cc[VMIN] = 1;
51+
// raw.c_cc[VTIME] = 0; /* 1 byte, no timer */
52+
53+
// // The cfmakeraw function is a standard way to achieve raw mode
54+
// cfmakeraw(&raw);
55+
56+
// // Additional configuration for read() behavior (VMIN and VTIME)
57+
// // VMIN=1, VTIME=0 means read() returns as soon as 1 character is available, blocking indefinitely until then
58+
// raw.c_cc[VMIN] = 1;
59+
// raw.c_cc[VTIME] = 0;
60+
61+
tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw);
62+
63+
atexit(editModeOn); // Ensure terminal mode is restored on exit
64+
}
65+
66+
void init() override {
67+
editModeOff();
68+
_lineedit = new Lineedit(this, _line, Channel::maxLine - 1);
69+
allChannels.registration(this);
70+
};
71+
72+
// Print methods (Stream inherits from Print)
73+
size_t write(uint8_t c) override {
74+
// fflush(STDOUT);
75+
return ::write(STDOUT_FILENO, &c, 1);
76+
}
77+
78+
// Stream methods (Channel inherits from Stream)
79+
80+
int available(void) override {
81+
int n;
82+
return ioctl(STDIN_FILENO, FIONREAD, &n) ? 0 : n;
83+
}
84+
85+
int read() override {
86+
int n;
87+
if ((n = available()) < 1) {
88+
return -1;
89+
}
90+
91+
char c;
92+
auto ret = ::read(STDIN_FILENO, &c, 1);
93+
return ret == 1 ? c : -1;
94+
}
95+
96+
// Channel methods
97+
int rx_buffer_available() override { return 128 - available(); }
98+
99+
bool realtimeOkay(char c) override { return _lineedit->realtime(c); }
100+
101+
bool lineComplete(char* line, char c) override {
102+
if (_lineedit->step(c)) {
103+
_linelen = _lineedit->finish();
104+
_line[_linelen] = '\0';
105+
strcpy(line, _line);
106+
_linelen = 0;
107+
return true;
108+
}
109+
return false;
110+
}
111+
};
112+
113+
PosixConsole posixConsole(true);
114+
Channel& Console = posixConsole;

FluidNC/src/BTConfig.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ namespace WebUI {
4848
char str[18];
4949
str[17] = '\0';
5050
uint8_t* addr = param->srv_open.rem_bda;
51-
sprintf(str, "%02X:%02X:%02X:%02X:%02X:%02X", addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
51+
snprintf(str, 18, "%02X:%02X:%02X:%02X:%02X:%02X", addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
5252
_btclient = str;
5353
log_info("BT Connected with " << str);
5454
} break;
@@ -65,8 +65,8 @@ namespace WebUI {
6565
const uint8_t* point = esp_bt_dev_get_address();
6666
char* str = _deviceAddrBuffer;
6767
str[17] = '\0';
68-
sprintf(
69-
str, "%02X:%02X:%02X:%02X:%02X:%02X", (int)point[0], (int)point[1], (int)point[2], (int)point[3], (int)point[4], (int)point[5]);
68+
snprintf(
69+
str, 17, "%02X:%02X:%02X:%02X:%02X:%02X", (int)point[0], (int)point[1], (int)point[2], (int)point[3], (int)point[4], (int)point[5]);
7070
return str;
7171
}
7272

0 commit comments

Comments
 (0)