76 Commits

Author SHA1 Message Date
b4623992ef b cmake
fix minimum version
2026-01-11 14:54:09 +01:00
b8553067d1 b tcp cmd docs 2026-01-11 14:53:50 +01:00
c0b4a014f5 b tcp cmd
fix error only showing when 1 param is provided
2026-01-11 14:49:23 +01:00
fffbf6e8db tcp_cmd
fix the tests
2023-12-27 13:00:21 +01:00
d627da2999 tcp_cnd
fix warning of casting const to non const
2023-12-27 13:00:21 +01:00
1ec27e9ee3 tcp_cmd
0 is alpha ff is opaque
2023-12-27 13:00:21 +01:00
6bc074a033 tcp_cmd
add capability of using ls as well as listfiles
2023-12-27 13:00:21 +01:00
87b6fecfd0 tcp_cmd
Doxygen: Add in, out
Move some stuff around to have a nicer ordering
2023-12-27 13:00:21 +01:00
3a745af9bf tcp_cmd
change the name of the define/ifdef to make it more obvious what it does
2023-12-27 13:00:21 +01:00
2a4de4fe34 tcp_cmd
split common code for colour conversion into a separate function
2023-12-27 13:00:21 +01:00
86f0a3bedc tcp_cmd
fix bug of tcp_cmd_bg_color writing to color_txt when in argb mode
2023-12-27 13:00:21 +01:00
a37213b128 tcp_cmd
add shift_args and use it in the parser function
2023-12-27 13:00:21 +01:00
992b9f3063 tcp_cmd
fix tests
2023-12-27 13:00:21 +01:00
589f56ecbd tcp_cmd
split parser into separate functions that handle specific commands
2023-12-27 13:00:21 +01:00
bd89deceb6 tcp_cmd
- Add putty compatibility
- Fix debug message with garbage cmd string if you press enter twice by zero initialising variables and checking when printing
2023-12-27 13:00:21 +01:00
5c5fda62e6 tcp_cmd
fix second message of unknown command
2023-12-27 13:00:21 +01:00
76f3044d21 tcp_cmd
fix p->len to p->total_len
2023-12-27 13:00:21 +01:00
f3818e805a tcp_cmd
make default color white text on black background
2023-12-27 13:00:21 +01:00
6ee9e305cc tcp_cmd
update casting of lcd_display_text following the standardisation of the lcd_api
2023-12-27 13:00:21 +01:00
f7dc10c7bb tcp_cmd
Remove author because I rewrote this thing from the tcp_echo example
2023-12-27 13:00:21 +01:00
b9a6566267 tcp_cmd
remove tcp_cmd_get_filename_ext and move over to llfs variant
2023-12-27 13:00:05 +01:00
2d526b899a tcp_cmd
fix some log strings
2023-12-27 13:00:05 +01:00
f8251a3e74 tcp_cmd
fix some usage strings
2023-12-27 13:00:05 +01:00
e42cbc31ba tcp_cmd
add text help for x y mode
2023-12-27 13:00:05 +01:00
a1add6b61b tcp_cmd
add argb capability to color and bgcolor
2023-12-27 13:00:05 +01:00
21a0207873 tcp_cmd
use tcp_cmd_remove_leading_space
2023-12-27 13:00:05 +01:00
b1f952ec58 tcp_cmd
fix tcp_cmd_remove_leading_space if str is null
2023-12-27 13:00:05 +01:00
d5495e2e84 tcp_cmd
fix casting issue
2023-12-27 13:00:05 +01:00
414e53e6e9 tcp_cmd
Add tcp_cmd_remove_leading_space + tests
2023-12-27 13:00:05 +01:00
35656af040 tcp_cmd
fix all tests
2023-12-27 13:00:05 +01:00
8a9df4d883 tcp_cmd
add tcp_cmd_ prefix
2023-12-27 13:00:05 +01:00
12a7221a2e mocs
fix moc of lcd_display_text to match function in lcd_api.h
2023-12-27 13:00:05 +01:00
55ea5b31e1 tcp_cmd
add functions that need to be tested to header file
2023-12-27 13:00:05 +01:00
6a0be6e469 tcp_cmd
fix remove_newline function
2023-12-27 12:59:48 +01:00
5268ad0b7a tcp_cmd
remove \r
2023-12-27 12:59:48 +01:00
1072e349c0 tcp_cmd
After testing
2023-12-27 12:59:48 +01:00
2d345b04c3 tcp_cmd
add error checking to init function
2023-12-27 12:59:48 +01:00
3d51fde427 tcp_cmd
Add doxygen documentation to the functions
2023-12-27 12:59:48 +01:00
9f48f4eef1 tcp_cmd
make some functions static
add static function declaration
2023-12-27 12:59:48 +01:00
d3d6cada0a tcp_cmd
Add doxygen headers
2023-12-27 12:59:22 +01:00
40662b33aa tcp_cmd
add testing of stdout both if moc's in DEBUG mode or not
2023-12-27 12:59:07 +01:00
b7a924719e tcp_cmd
move functions from test file to project
2023-12-27 12:59:07 +01:00
5bc3250c15 tcp_cmd
Move next ptr to the top
2023-12-27 12:57:57 +01:00
ed2e7ef650 tcp_cmd
add return parameter to tcp_cmd_parser to keep the connection open or closed
2023-12-27 12:57:57 +01:00
0f1f041cbf tcp_cmd
format
remove dangling else if when every other thing is if
use Lorenz's way of tcp_recv_cb
2023-12-27 12:57:57 +01:00
9aa53db35f modbus_tcp
add lwip_strerr
fix tcp_close
2023-12-27 12:57:57 +01:00
84f5a61ddf tcp_cmd
found out that my atoi converter didn't work, so I switched back to strtol
2023-12-27 12:57:57 +01:00
9885971549 tcp_cmd
Remov some casting warnings
2023-12-27 12:57:57 +01:00
7c86563242 tcp_cmd
add more dprint in the mocs
2023-12-27 12:57:57 +01:00
edc60bafb8 tcp_cmd
add some unused tags back, when in normal mode this gave some warnings
2023-12-27 12:57:57 +01:00
d7ff0010bd tcp_cmd
add pbuf_copy_partial moc so I don't need if TESTING preprocessor directive
2023-12-27 12:57:48 +01:00
f16e92b7d8 tcp_cmd
Move over to internal (small) buffer because pbuf data is not null terminated
2023-12-27 12:57:30 +01:00
16c40ce7fe tcp_cmd
change null termination from realloc to internal array
2023-12-27 12:57:30 +01:00
6ceee577de tcp_cmd
Make sure p->payload is null terminated
2023-12-27 12:57:30 +01:00
56846b7643 tcp_cmd
fix spacing in print help
2023-12-27 12:57:30 +01:00
2508d365c3 tcp_cmd
Add docs for the version in test/
2023-12-27 12:57:30 +01:00
42675a5fe1 testing for tcp_cmd
add testing files
2023-12-27 12:57:30 +01:00
843c234bcf tcp_cmd
add testing capabilities to header file
2023-12-27 12:57:30 +01:00
e9bcf0e712 tcp_cmd
change include from <> to ""
fix some aligning of the tcp_cmd_print_help
remove endptr
cast strtoul
cast lcd_display_text
2023-12-27 12:57:11 +01:00
2baa820683 tcp_cmd
convert text to english
2023-12-27 12:57:11 +01:00
a784f3c33f tcp_cmd
create wrapper for tcp_write function
create print header and help function
2023-12-27 12:57:11 +01:00
9cc94f25f2 tcp_cmd
remove tcp_buffer
2023-12-27 12:56:45 +01:00
Roelandts_Gert
3cfb22f590 Added flags to tcp_write 2023-12-27 12:56:45 +01:00
Roelandts_Gert
e924f2b82d Update tcp_cmd.c 2023-12-27 12:56:23 +01:00
Roelandts_Gert
31e761300a Update tcp_cmd.c 2023-12-27 12:56:23 +01:00
Roelandts_Gert
fb44367da7 Fixed lcd_display_text 2023-12-27 12:56:23 +01:00
Roelandts_Gert
6de8684d89 renamed files from cmd to tcp_cmd 2023-12-27 12:56:23 +01:00
Roelandts_Gert
61d2994e40 Update code to Styleguide 2023-12-27 12:55:52 +01:00
Roelandts_Gert
8be959eefd Clear in exit command
clears screen if application is terminated
2023-12-27 12:55:52 +01:00
Roelandts_Gert
e852d29824 Added clear support and cleaned up code 2023-12-27 12:55:52 +01:00
Roelandts_Gert
f11f71020d includes from cmd.c to cmd.h 2023-12-27 12:55:52 +01:00
Roelandts_Gert
1bcd9f1113 Added clear functionality
Added lcd_clear_text and lcd_clear_images infront of each write function to the screen
2023-12-27 12:55:52 +01:00
Roelandts_Gert
398bb2efde Update to setImage command
Added checks to see if file exist in filesystem and if extension is supported (bmp of gif)
2023-12-27 12:55:52 +01:00
Roelandts_Gert
26f2655e3f Added support for setImage command for .bmp files
Implemented code for displaying .bmp images with the steImage command.
2023-12-27 12:55:52 +01:00
Roelandts_Gert
e34b2855ba Implementation of llfs and display API
Implementation of llfs and display API. Using the functions from the llfs and display API in my code
2023-12-27 12:55:52 +01:00
Roelandts_Gert
9f25995c82 Added cmd.c and cmd.h
Functions in main copied to cmd.c
2023-12-27 12:55:52 +01:00
16 changed files with 1070 additions and 247 deletions

View File

@@ -1,11 +0,0 @@
project/Core/Inc/main.h
project/Core/Inc/stm32f7xx_hal_conf.h
project/Core/Inc/stm32f7xx_it.h
project/Core/Inc/fsdata_custom.c
project/Core/Src/main.c
project/Core/Src/stm32f7xx_hal_msp.c
project/Core/Src/syscalls.c
project/Core/Src/sysmem.c
project/Core/Src/system_stm32f7xx.c
project/Core/Src/stm32f7xx_it.c
mkllfs/build/CMakeFiles/3.27.7/CompilerIdC/CMakeCCompilerId.c

View File

@@ -1,4 +0,0 @@
project/Core/
mkllfs
tests/
Applications/Qt-application_for_UDP_broadcast/

View File

@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
cmake_minimum_required(VERSION 3.10)
project(WSAA_tests LANGUAGES CXX C)

View File

@@ -1,8 +0,0 @@
#!/bin/bash
cd project/
rm -fr build/*
cmake -B build/ -G Ninja
ninja -C build/
cd ../

View File

@@ -1,7 +0,0 @@
#!/bin/bash
rm -fr build/*
cmake -B build/ -G Ninja
ninja -C build/
ctest --test-dir build

View File

@@ -1,8 +0,0 @@
FROM fedora:39
RUN dnf update -y
RUN dnf install -y git
RUN dnf install -y ninja-build make cmake gcc-c++ gcc clang-tools-extra clang-devel clang
RUN dnf install -y gtest-devel gtest
RUN dnf install -y arm-none-eabi-gcc-cs arm-none-eabi-gcc-cs-c++ arm-none-eabi-binutils-cs.x86_64 arm-none-eabi-newlib.noarch
ENTRYPOINT [ "/bin/bash" ]

104
docs/tcp_cmd_interface.md Normal file
View File

@@ -0,0 +1,104 @@
# TCP CMD Interface
## Introduction
The tcp cmd interface is a way to control the device over TCP. It is a simple protocol that allows the user to send commands to the device and receive responses. The protocol looks like bash commands. The commands are separated by a new line character (`\n`). The response is sent after the command is executed. The response is also terminated by a new line character (`\n`).
## Table of contents
* [Introduction](#introduction)
* [Table of contents](#table-of-contents)
* [Usage of TCP CMD Interface](#usage-of-tcp-cmd-interface)
* [Connecting to the device](#connecting-to-the-device)
* [Sending commands](#sending-commands)
* [Available commands](#available-commands)
* [`help`](#help)
* [`text`](#text)
* [`bgcolor`](#bgcolor)
* [`color`](#color)
* [`listImages`](#listimages)
* [`setImage`](#setimage)
* [`setGif`](#setgif)
## Usage of TCP CMD Interface
### Connecting to the device
The device can be connected to using a TCP client. The device will listen on port 23. The device will send a welcome message when a client connects.
On Linux, the device can be connected to using the `nc` command:
```bash
nc <ip-address> 23
```
### Sending commands
Commands can be sent to the device by typing them in the terminal and pressing enter. The device will execute the command and send a response.
### Available commands
#### `help`
The `help` command will print a list of available commands.
```bash
$> help
help : shows a list of commands
text "<text>" : puts text on the lcd
color (<a>) <r> <g> <b> : set the background color of the lcd
textColor (<a>) <r> <g> <b> : set the color of the text
listImages : shows a list with images in the filesystem
setImage <image_name> : put an image on the screen
setGif <image_name> : put a gif on the screen
exit : closes the connection
```
#### `text`
The `text` command will print the given text on the screen. The text can be one or multiple words. one word does'n need quotes, multiple words do. The text will rapp around when it reaches the end of the screen.
```bash
$> text "Hello World!"
```
#### `bgcolor`
The `bgcolor` command will set the background color of the screen. The color can be provided in 2 ways: as three numbers between 0 and 255 and one hex value. The first number is the red value, the second is the green value and the third is the blue value. It also has the possible to set the alpha value as the first parameter, this is optional.
```bash
bgcolor 255 0 0
```
or
```bash
bgcolor 0xFF0000
```
#### `color`
The `color` command will set the color of the text. The color can be provided in 2 ways: as three numbers between 0 and 255 and one hex value. The first number is the red value, the second is the green value and the third is the blue value. It also has the possible to set the alpha value as the first parameter, this is optional.
```bash
color 255 0 0
```
or
```bash
color 0xFF0000
```
#### `listImages`
The `listImages` command will print a list of images that are present in the filesystem.
```bash
$> listImages
image1.bmp
image2.bmp
image3.bmp
gif1.gif
...
```
#### `setImage`
The `setImage` command will put an image on the screen. The image must be present in the filesystem. If the image is not present in the filesystem, the device will send an error message. This function only works for BMP images, if you give any other file type, the device will send an error message. You can also provide the starting x and y coordinates as optional parameters. If no coordinates are provided, the image will be placed at the top left corner of the screen (0,0).
```bash
$> setImage image1.bmp
```
or
```bash
$> setImage image1.bmp 128 64
```
#### `setGif`
The `setGif` command will put a gif on the screen. The gif must be present in the filesystem. If the gif is not present in the filesystem, the device will send an error message. This function only works for GIF images, if you give any other file type, the device will send an error message. You can also provide the starting x and y coordinates as optional parameters. If no coordinates are provided, the gif will be placed at the top left corner of the screen (0,0).
```bash
$> setGif gif1.gif
```
or
```bash
$> setGif gif1.gif 128 64
```

15
lint.sh
View File

@@ -1,15 +0,0 @@
#!/bin/bash
if (( $# < 1 )); then
>&2 echo "Illegal number of parameters"
exit 1
fi
# save unsaved work
git stash --include-untracked
clang-format -i "$@"
git add -A && git commit -m "Lint"
git stash pop

View File

@@ -1,19 +1,30 @@
/**
* @file tcp_cmd.h
* @brief TCP CMD interface
* @author Gert R.
*/
* @file tcp_cmd.h
* @brief This file contains the headers of the tcp command interface
* @author Sander S.
*/
#ifndef INC_TCP_CMD_H_
#define INC_TCP_CMD_H_
#include <stdio.h>
#include <string.h>
#include <tcp.h>
#ifndef TESTING
#include "lcd_api.h"
#else
#include "mocs.h"
#endif
#include "llfs.h"
#include "log.h"
#include <tcp.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <ctype.h>
void tcp_cmd_remove_newline(char* str, size_t len);
char* tcp_cmd_remove_leading_space(char* str, size_t len);
void tcp_cmd_str_tolower(char* str);
char* tcp_cmd_get_next_token(char* input, const char* delimiters, char** next);
void tcp_cmd_init(void);
err_t tcp_cmd_recv(void* arg, struct tcp_pcb* pcb, struct pbuf* p, err_t err);
#endif /* INC_TCP_CMD_H_ */

View File

@@ -120,10 +120,7 @@ int main(void) {
MX_QUADSPI_Init();
/* USER CODE BEGIN 2 */
/* Initialize QSPI */
BSP_QSPI_Init();
BSP_QSPI_MemoryMappedMode();
WRITE_REG(QUADSPI->LPTR, 0xFFF);
/* USER CODE END 2 */
/* Clear terminal */
printf(CLEAR_SCREEN);
@@ -131,8 +128,10 @@ int main(void) {
/* Initialize the LCD */
lcd_init(true);
/* Initialize the filesystem */
llfs_init();
/* Initialize the filesystem */
llfs_init();
tcp_cmd_init();
/* USER CODE END 2 */
/* Initialize the tftp server */
tftp_server_init();

View File

@@ -1,184 +1,592 @@
/**
* @file tcp_cmd.c
* @brief TCP CMD interface
* @author Gert R.
* @brief This file contains the implementation of the tcp command interface
* @author Sander S.
*/
#include "tcp_cmd.h"
static uint32_t result_txt = 0xff000000; // Store text color
static uint32_t result_bg = 0xff000000; // Store background color
#define MAX_TOKENS 10
#define MAX_CMD_LEN 50
static void tcp_cmd_close(struct tcp_pcb* pcb) {
tcp_arg(pcb, NULL);
tcp_sent(pcb, NULL);
tcp_recv(pcb, NULL);
tcp_close(pcb);
#define NCAT
static const char* TAG = "tcp_cmd";
static uint32_t color_txt = 0xffffffff; // Store text color
static uint32_t color_bg = 0xff000000; // Store background color
static void tcp_cmd_write(struct tcp_pcb* pcb, const char* str);
static void tcp_cmd_print_header(struct tcp_pcb* pcb);
static void tcp_cmd_print_help(struct tcp_pcb* pcb);
static void tcp_cmd_clear(struct tcp_pcb* pcb, int argc, char** argv);
static void tcp_cmd_text(struct tcp_pcb* pcb, int argc, char** argv);
static void tcp_cmd_bg_color(struct tcp_pcb* pcb, int argc, char** argv);
static void tcp_cmd_color(struct tcp_pcb* pcb, int argc, char** argv);
static void tcp_cmd_list_files(struct tcp_pcb* pcb, int argc, char** argv);
static void tcp_cmd_set_image(struct tcp_pcb* pcb, int argc, char** argv);
static void tcp_cmd_set_gif(struct tcp_pcb* pcb, int argc, char** argv);
static void tcp_cmd_exit(struct tcp_pcb* pcb, int argc, char** argv);
static void tcp_cmd_unknown_cmd(struct tcp_pcb* pcb, char* cmd);
static bool tcp_cmd_parser(struct tcp_pcb* pcb, int argc, char** argv);
static err_t tcp_cmd_accept(void* arg, struct tcp_pcb* pcb, err_t err);
static void tcp_cmd_close(struct tcp_pcb* pcb);
/**
* @brief This function shifts the arguments in argv and argc
* @param[in,out] argc The argc pointer
* @param[in,out] argv The argv pointer
* @return The shifted argument
*/
static char* shift_args(int* argc, char*** argv) {
if (*argc == 0) {
return NULL;
}
char* arg = (*argv)[0];
(*argv)++;
(*argc)--;
return arg;
}
static err_t tcp_cmd_recv(void* arg, struct tcp_pcb* pcb, struct pbuf* p, err_t err) {
size_t len;
size_t number_of_files;
uint8_t file_in_fs;
uint8_t check = 0;
/**
* @brief This function converts a string to a color
* @param[in] argc The number of arguments
* @param[in] argv The arguments
* @param[out] color The color to write to
* @return true The conversion failed
* @return false The conversion succeeded
*/
static bool str_to_color(int argc, char** argv, uint32_t* color) {
if (argc == 1) {
*color = (uint32_t)strtoul(argv[0], NULL, 16);
return false;
}
if (argc == 3) {
*color = 0xff000000;
*color |= (uint32_t)strtoul(argv[0], NULL, 10) << 16;
*color |= (uint32_t)strtoul(argv[1], NULL, 10) << 8;
*color |= (uint32_t)strtoul(argv[2], NULL, 10);
return false;
}
if (argc == 4) {
*color = (uint32_t)strtoul(argv[0], NULL, 10) << 24;
*color |= (uint32_t)strtoul(argv[1], NULL, 10) << 16;
*color |= (uint32_t)strtoul(argv[2], NULL, 10) << 8;
*color |= (uint32_t)strtoul(argv[3], NULL, 10);
return false;
}
char* pc;
char tcp_buffer[1024];
char text[256];
char color_r[3];
char color_g[3];
char color_b[3];
return true;
}
char text_color_r[3];
char text_color_g[3];
char text_color_b[3];
char extension[4];
char* endptr;
if (err == ERR_OK && p != NULL) {
tcp_recved(pcb, p->tot_len);
pc = (char*)p->payload;
len = p->tot_len;
for (size_t i = 0; i < len; i++) {
tcp_buffer[i] = pc[i];
/**
* @brief This function removes the newline from a string the string can contain multiple lines
* @param[in,out] str The string to remove the newline from
*/
void tcp_cmd_remove_newline(char* str, size_t len) {
size_t i = 0;
size_t j = 0;
while (str[i] != '\0' && j < len) {
if (str[j] != '\n' && str[j] != '\r') {
str[i] = str[j];
i++;
}
j++;
}
str[i] = '\0';
}
if (!strncmp(tcp_buffer, "help", 4)) {
check = 1;
tcp_write(pcb,
"help : laat lijst zien met alle commando's\r\n"
"text : geeft tekst mee die op LCD komt (uw_text)\r\n"
"color : kleur achtergrond van scherm (255 255 255)\r\n"
"textColor : kleur van tekst (255 255 255)\r\n"
"listImages: laat een lijst zien van de mogelijke afbeeldingen\r\n"
"setImage : veranderd te afbeelding (naam_afbeelding)\r\n"
"exit : sluit de verbinding\r\n",
354, TCP_WRITE_FLAG_COPY | TCP_WRITE_FLAG_MORE);
} else if (!strncmp(tcp_buffer, "text ", 5)) {
size_t i;
for (i = 0; i < len - 4; i++) {
text[i] = tcp_buffer[i + 5];
}
text[i - 1] = '\0';
lcd_clear_text();
lcd_display_text(text, 10, 10, result_txt, result_bg, LCD_FONT24);
check = 1;
} else if (!strncmp(tcp_buffer, "color", 5)) {
for (size_t i = 0; i < 3; i++) {
color_r[i] = tcp_buffer[i + 6];
color_g[i] = tcp_buffer[i + 10];
color_b[i] = tcp_buffer[i + 14];
}
result_bg |= strtoul(color_r, &endptr, 10) << 16;
result_bg |= strtoul(color_g, &endptr, 10) << 8;
result_bg |= strtoul(color_b, &endptr, 10);
check = 1;
} else if (!strncmp(tcp_buffer, "textColor", 9)) {
for (size_t i = 0; i < 3; i++) {
text_color_r[i] = tcp_buffer[i + 10];
text_color_g[i] = tcp_buffer[i + 14];
text_color_b[i] = tcp_buffer[i + 18];
}
result_txt |= strtoul(text_color_r, &endptr, 10) << 16;
result_txt |= strtoul(text_color_g, &endptr, 10) << 8;
result_txt |= strtoul(text_color_b, &endptr, 10);
check = 1;
} else if (!strncmp(tcp_buffer, "listImages", 10)) {
number_of_files = llfs_file_count();
if (number_of_files > 0) {
llfs_file_t file_list[number_of_files];
number_of_files = llfs_file_list(file_list, number_of_files, NULL);
for (size_t i = 0; i < number_of_files; i++) {
tcp_write(pcb, file_list[i].name, strlen(file_list[i].name),
TCP_WRITE_FLAG_COPY | TCP_WRITE_FLAG_MORE);
tcp_write(pcb, "\r\n", 2, TCP_WRITE_FLAG_COPY | TCP_WRITE_FLAG_MORE);
}
} else {
tcp_write(pcb, "NO files in filesystem\r\n", 24, TCP_WRITE_FLAG_COPY | TCP_WRITE_FLAG_MORE);
}
check = 1;
} else if (!strncmp(tcp_buffer, "setImage", 8)) {
char filename[len - 8];
for (size_t i = 0; i < len - 9; i++) {
filename[i] = tcp_buffer[i + 9];
}
for (size_t i = 0; i < 3; i++) {
extension[i] = tcp_buffer[i + len - 3];
}
filename[sizeof(filename) - 1] = '\0';
extension[3] = '\0';
number_of_files = llfs_file_count();
if (number_of_files > 0) {
llfs_file_t file_list[number_of_files];
number_of_files = llfs_file_list(file_list, number_of_files, NULL);
file_in_fs = 0;
for (size_t i = 0; i < number_of_files; i++) {
if (!strcmp(filename, file_list[i].name)) {
file_in_fs = 1;
}
}
// Check which file extension is used and call right function
if (!strncmp(extension, "bmp", 3) && file_in_fs) {
lcd_clear_images();
lcd_draw_img_from_fs(filename, 10, 10);
} else if (!strncmp(extension, "gif", 3) && file_in_fs) {
lcd_clear_images();
lcd_draw_gif_from_fs(filename, 10, 10);
} else if (!file_in_fs) {
tcp_write(pcb, "File NOT in filesystem\n\r", 24, TCP_WRITE_FLAG_COPY | TCP_WRITE_FLAG_MORE);
} else {
tcp_write(pcb, "Extension NOT supported\n\r", 25, TCP_WRITE_FLAG_COPY | TCP_WRITE_FLAG_MORE);
}
} else {
tcp_write(pcb, "NO files in filesystem\r\n", 24, TCP_WRITE_FLAG_COPY | TCP_WRITE_FLAG_MORE);
}
check = 1;
} else if (!strncmp(tcp_buffer, "exit", 4)) {
lcd_clear_images();
lcd_clear_text();
tcp_cmd_close(pcb);
check = 1;
/**
* @brief This function 'removes' the leading spaces from a string
* @param[in] str The string to remove the leading spaces from
* @param[in] len The length of the string
* @return char* A pointer to the first non-space character
*/
char* tcp_cmd_remove_leading_space(char* str, size_t len) {
size_t i = 0;
if (str == NULL || str[0] == '\0' || len == 0) {
return NULL;
}
while (str[i] != '\0' && i < len) {
if (isspace((int)str[i]) == 0) {
return &str[i];
}
i++;
}
return NULL;
}
if (!check && (strncmp(tcp_buffer, "\r\n", 2) != 0)) {
tcp_write(pcb, "Onbestaand commando: help voor lijst van commando's\r\n", 53,
TCP_WRITE_FLAG_COPY | TCP_WRITE_FLAG_MORE);
/**
* @brief This function converts a string to lowercase
* @param[in,out] str The string to convert
*/
void tcp_cmd_str_tolower(char* str) {
int i = 0;
while (str[i] != '\0') {
str[i] = (char)tolower((int)str[i]);
i++;
}
}
/**
* @brief This function finds the next token in the input string
* If the token is between quotes, return the whole string between quotes
* @param[in] input The input string
* @param[in] delimiter The delimiters to use
* @param[in,out] next The next token
* @return char* The next token
*/
char* tcp_cmd_get_next_token(char* input, const char* delimiters, char** next) {
if (input == NULL && *next == NULL) {
return NULL;
}
if (input == NULL) {
input = *next;
}
// Skip leading delimiters
input += strspn(input, delimiters);
if (*input == '\0') {
return NULL;
}
// If the token is between quotes, return the whole string between quotes
if (*input == '"') {
char* end = strchr(++input, '"');
if (end == NULL) {
return NULL;
}
*end = '\0';
*next = end + 1;
return input;
}
pbuf_free(p);
if (len > tcp_sndbuf(pcb)) {
len = tcp_sndbuf(pcb);
}
tcp_sent(pcb, NULL);
// Find the end of the token
char* end = input + strcspn(input, delimiters);
if (*end == '\0') {
*next = end;
} else {
pbuf_free(p);
*end = '\0';
*next = end + 1;
}
return input;
}
/**
* @brief This function is a wrapper for tcp_write and tcp_output
* @param[in] pcb The tcp_pcb struct to write to
* @param[in] str The string to write
*/
static void tcp_cmd_write(struct tcp_pcb* pcb, const char* str) {
tcp_write(pcb, str, strlen(str), TCP_WRITE_FLAG_COPY | TCP_WRITE_FLAG_MORE);
tcp_output(pcb);
}
/**
* @brief This function prints the header of the tcp command interface
* @param[in] pcb The tcp_pcb struct to write to
*/
static void tcp_cmd_print_header(struct tcp_pcb* pcb) {
tcp_cmd_write(pcb, " Welcome to the TCP CMD interface\n"
"(Type help for a list of the commands! exit to close)\n"
"============================================================\n"
"$>");
}
/**
* @brief This function prints the help text
* @param[in] pcb The tcp_pcb struct to write to
*/
static void tcp_cmd_print_help(struct tcp_pcb* pcb) {
LOG_INFO(TAG, "Printing help");
tcp_cmd_write(pcb, "help : shows a list of commands\n"
"clear text/images : clears the text or images on the lcd\n"
"text \"<text>\" : puts text on the lcd\n"
"bgColor (<a>) <r> <g> <b> : set the background color of the lcd\n"
"color (<a>) <r> <g> <b> : set the color of the text\n"
"listFiles : shows a list with images in the filesystem\n"
"setImage <image_name> : put an image on the screen\n"
"setGif <image_name> : put a gif on the screen\n"
"exit : closes the connection\n");
}
/**
* @brief This function handles the clear command
*
* @param[in] pcb The tcp_pcb struct to write to
* @param[in] argc The number of arguments
* @param[in] argv The arguments
*/
static void tcp_cmd_clear(struct tcp_pcb* pcb, int argc, char** argv) {
if (argc == 0) {
LOG_INFO(TAG, "Clearing screen");
lcd_clear_text();
lcd_clear_images();
return;
}
if (argc == 1) {
if (strcmp(argv[0], "text") == 0) {
LOG_INFO(TAG, "Clearing text");
lcd_clear_text();
return;
}
if (strcmp(argv[0], "images") == 0) {
LOG_INFO(TAG, "Clearing images");
lcd_clear_images();
return;
}
}
LOG_WARN(TAG, "Bad usage of clear");
tcp_cmd_write(pcb, "Usage: clear\n");
tcp_cmd_write(pcb, "Usage: clear text\n");
tcp_cmd_write(pcb, "Usage: clear images\n");
return;
}
/**
* @brief This function handles the text command
*
* @param[in] pcb The tcp_pcb struct to write to
* @param[in] argc The number of arguments
* @param[in] argv The arguments
*/
static void tcp_cmd_text(struct tcp_pcb* pcb, int argc, char** argv) {
if (argc == 1) {
LOG_INFO(TAG, "Setting text %s @ 10, 10", argv[0]);
lcd_display_text((const char*)argv[0], 10, 10, color_txt, color_bg, LCD_FONT24);
return;
}
if (argc == 3) {
LOG_INFO(TAG, "Setting text %s @ %lu, %lu", argv[0], (uint32_t)strtoul(argv[1], NULL, 10),
(uint32_t)strtoul(argv[2], NULL, 10));
lcd_display_text((const char*)argv[0], (uint32_t)strtoul(argv[1], NULL, 10),
(uint32_t)strtoul(argv[2], NULL, 10), color_txt, color_bg, LCD_FONT24);
return;
}
LOG_WARN(TAG, "Bad usage of text");
tcp_cmd_write(pcb, "Usage: text \"<text>\"\n");
tcp_cmd_write(pcb, "Usage: text <word>\n");
tcp_cmd_write(pcb, "Usage: text \"<text>\" <x> <y>\n");
}
/**
* @brief This function handles the bgcolor command
*
* @param[in] pcb The tcp_pcb struct to write to
* @param[in] argc The number of arguments
* @param[in] argv The arguments
*/
static void tcp_cmd_bg_color(struct tcp_pcb* pcb, int argc, char** argv) {
if (!str_to_color(argc, argv, &color_bg)) {
LOG_INFO(TAG, "Setting background color to %08lX", color_bg);
return;
}
LOG_WARN(TAG, "Bad usage of bgcolor");
tcp_cmd_write(pcb, "Usage: bgcolor 0x<rrggbb>\n");
tcp_cmd_write(pcb, "Usage: bgcolor 0x<aarrggbb>\n");
tcp_cmd_write(pcb, "Usage: bgcolor <r> <g> <b>\n");
tcp_cmd_write(pcb, "Usage: bgcolor <a> <r> <g> <b>\n");
}
/**
* @brief This function handles the color command
*
* @param[in] pcb The tcp_pcb struct to write to
* @param[in] argc The number of arguments
* @param[in] argv The arguments
*/
static void tcp_cmd_color(struct tcp_pcb* pcb, int argc, char** argv) {
if (!str_to_color(argc, argv, &color_txt)) {
LOG_INFO(TAG, "Setting color to %08lX", color_txt);
return;
}
LOG_WARN(TAG, "Bad usage of color");
tcp_cmd_write(pcb, "Usage: color 0x<rrggbb>\n");
tcp_cmd_write(pcb, "Usage: color 0x<aarrggbb>\n");
tcp_cmd_write(pcb, "Usage: color <r> <g> <b>\n");
tcp_cmd_write(pcb, "Usage: color <a> <r> <g> <b>\n");
}
/**
* @brief This function handles the listfiles command
*
* @param[in] pcb The tcp_pcb struct to write to
* @param[in] argc The number of arguments
* @param[in] argv The arguments
*/
static void tcp_cmd_list_files(struct tcp_pcb* pcb, int argc, char** argv) {
UNUSED(argv);
if (argc == 0) {
LOG_INFO(TAG, "Listing files");
void* mem = NULL; // Pointer for internal use by the llfs library
llfs_file_t* file;
while ((file = llfs_next_file(&mem, NULL)) != NULL) {
tcp_cmd_write(pcb, file->name);
tcp_cmd_write(pcb, "\n");
}
return;
}
LOG_WARN(TAG, "Bad usage of listfiles");
tcp_cmd_write(pcb, "Usage: listfiles\n");
tcp_cmd_write(pcb, "Usage: ls\n");
return;
}
/**
* @brief This function handles the setimage command
*
* @param[in] pcb The tcp_pcb struct to write to
* @param[in] argc The number of arguments
* @param[in] argv The arguments
*/
static void tcp_cmd_set_image(struct tcp_pcb* pcb, int argc, char** argv) {
const char* ext = NULL;
if (argc >= 1) {
ext = llfs_get_filename_ext(argv[0]);
if (strcmp(ext, "bmp") != 0) {
LOG_WARN(TAG, "setimage: File is not a bmp");
tcp_cmd_write(pcb, "File is not a bmp\n");
return;
}
}
if (argc == 1) {
LOG_INFO(TAG, "Setting image %s @ 0, 0", argv[0]);
lcd_draw_img_from_fs(argv[0], 0, 0);
return;
}
if (argc == 3) {
LOG_INFO(TAG, "Setting image %s @ %lu, %lu", argv[0], (uint32_t)strtoul(argv[1], NULL, 10),
(uint32_t)strtoul(argv[2], NULL, 10));
lcd_draw_img_from_fs(argv[0], (uint32_t)strtoul(argv[1], NULL, 10), (uint32_t)strtoul(argv[2], NULL, 10));
return;
}
LOG_WARN(TAG, "Bad usage of setimage");
tcp_cmd_write(pcb, "Usage: setimage <filename>\n");
tcp_cmd_write(pcb, "Usage: setimage <filename> <x> <y>\n");
}
/**
* @brief This function handles the setgif command
*
* @param[in] pcb The tcp_pcb struct to write to
* @param[in] argc The number of arguments
* @param[in] argv The arguments
*/
static void tcp_cmd_set_gif(struct tcp_pcb* pcb, int argc, char** argv) {
const char* ext = NULL;
if (argc >= 1) {
ext = llfs_get_filename_ext(argv[0]);
if (strcmp(ext, "gif") != 0) {
LOG_WARN(TAG, "setgif: File is not a gif");
tcp_cmd_write(pcb, "File is not a gif\n");
return;
}
}
if (argc == 1) {
LOG_INFO(TAG, "Setting gif %s @ 0, 0", argv[0]);
lcd_draw_gif_from_fs(argv[0], 0, 0);
return;
}
if (argc == 3) {
LOG_INFO(TAG, "Setting gif %s @ %lu, %lu", argv[0], (uint32_t)strtoul(argv[1], NULL, 10),
(uint32_t)strtoul(argv[2], NULL, 10));
lcd_draw_gif_from_fs(argv[0], (uint32_t)strtoul(argv[1], NULL, 10), (uint32_t)strtoul(argv[2], NULL, 10));
return;
}
if (err == ERR_OK && p == NULL) {
LOG_WARN(TAG, "Bad usage of setgif");
tcp_cmd_write(pcb, "Usage: setgif <filename>\n");
tcp_cmd_write(pcb, "Usage: setgif <filename> <x> <y>\n");
}
/**
* @brief This function handles the exit command
*
* @param[in] pcb The tcp_pcb struct to write to
* @param[in] argc The number of arguments
* @param[in] argv The arguments
*/
static void tcp_cmd_exit(struct tcp_pcb* pcb, int argc, char** argv) {
UNUSED(argv);
if (argc == 0) {
LOG_INFO(TAG, "Closing connection");
tcp_cmd_write(pcb, "Exiting...\n");
lcd_clear_images();
lcd_clear_text();
tcp_close(pcb);
return;
}
LOG_WARN(TAG, "Bad usage of exit");
tcp_cmd_write(pcb, "Usage: exit\n");
}
/**
* @brief This function handles unknown commands
*
* @param[in] pcb The tcp_pcb struct to write to
* @param[in] cmd The command
*/
static void tcp_cmd_unknown_cmd(struct tcp_pcb* pcb, char* cmd) {
LOG_WARN(TAG, "Unknown command: %s", cmd);
tcp_cmd_write(pcb, "Unknown command: ");
tcp_cmd_write(pcb, cmd);
tcp_cmd_write(pcb, "\n");
tcp_cmd_write(pcb, "Type help for list of commands\n");
}
/**
* @brief This function parses the command and calls the appropriate function
*
* @param[in] pcb Pointer to the tcp_pcb struct to write to
* @param[in] argc Count of arguments
* @param[in] argv Array of arguments
* @return true Connection should be closed
* @return false Connection should be kept open
*/
static bool tcp_cmd_parser(struct tcp_pcb* pcb, int argc, char** argv) {
if (argc == 0) {
LOG_WARN(TAG, "No command given");
return false;
}
char* cmd = shift_args(&argc, &argv);
tcp_cmd_str_tolower(cmd);
if (strcmp(cmd, "help") == 0) {
tcp_cmd_print_help(pcb);
return false;
}
if (strcmp(cmd, "clear") == 0) {
tcp_cmd_clear(pcb, argc, argv);
return false;
}
if (strcmp(cmd, "text") == 0) {
tcp_cmd_text(pcb, argc, argv);
return false;
}
if (strcmp(cmd, "bgcolor") == 0) {
tcp_cmd_bg_color(pcb, argc, argv);
return false;
}
if (strcmp(cmd, "color") == 0) {
tcp_cmd_color(pcb, argc, argv);
return false;
}
if (strcmp(cmd, "listfiles") == 0) {
tcp_cmd_list_files(pcb, argc, argv);
return false;
}
if (strcmp(cmd, "ls") == 0) {
tcp_cmd_list_files(pcb, argc, argv);
return false;
}
if (strcmp(cmd, "setimage") == 0) {
tcp_cmd_set_image(pcb, argc, argv);
return false;
}
if (strcmp(cmd, "setgif") == 0) {
tcp_cmd_set_gif(pcb, argc, argv);
return false;
}
if (strcmp(cmd, "exit") == 0) {
tcp_cmd_exit(pcb, argc, argv);
return true;
}
tcp_cmd_unknown_cmd(pcb, cmd);
return false;
}
/**
* @brief This function is called when the tcp connection receives data
* @param[in] arg The argument ptr
* @param[in] pcb The tcp_pcb struct
* @param[in] p The pbuf struct
* @param[in] err The error code from the tcp stack
* @return err_t ERR_OK if successful
*/
err_t tcp_cmd_recv(void* arg, struct tcp_pcb* pcb, struct pbuf* p, err_t err) {
int argc = 0;
char cmd[MAX_CMD_LEN] = {0};
char* cmd_ptr = NULL;
char* argv[MAX_TOKENS] = {0};
bool close_conn = false;
char* next = NULL;
LWIP_UNUSED_ARG(arg);
// Connection closed?
if (p == NULL && err == ERR_OK) {
LOG_INFO(TAG, "Remote closed connection");
return tcp_close(pcb);
}
if (err != ERR_OK) {
LOG_WARN(TAG, "TCP data received with error(%d): %s", err, lwip_strerr(err));
return ERR_OK;
}
if (p->tot_len >= MAX_CMD_LEN) {
LOG_WARN(TAG, "Command too long");
tcp_cmd_write(pcb, "Command too long");
}
uint16_t len = p->tot_len >= MAX_CMD_LEN ? MAX_CMD_LEN : p->tot_len;
// Make sure the string is null terminated
pbuf_copy_partial(p, cmd, len, 0);
cmd[len] = '\0';
// Tell the tcp stack that we have taken the data
tcp_recved(pcb, p->tot_len);
tcp_cmd_remove_newline(cmd, strlen(cmd));
if (cmd[0] != '\0') {
LOG_INFO(TAG, "cmd: %s", cmd);
}
// Split string into tokens by delimiter (space)
cmd_ptr = tcp_cmd_remove_leading_space(cmd, strlen(cmd));
argv[0] = tcp_cmd_get_next_token(cmd_ptr, " ", &next);
if (argv[0] != NULL) {
argc = 1;
}
while (argv[argc - 1] != NULL && argc < MAX_TOKENS) {
// Check if the next token is 0 (end of string) strlen doesn't work here
if (*next == 0) {
argv[argc] = NULL;
break;
}
next = tcp_cmd_remove_leading_space(next, strlen(next));
argv[argc] = tcp_cmd_get_next_token(NULL, " ", &next);
if (argv[argc] == NULL) {
break;
}
argc++;
}
close_conn = tcp_cmd_parser(pcb, argc, argv);
if (close_conn) {
LOG_INFO(TAG, "Closing connection");
tcp_cmd_close(pcb);
#ifdef NCAT
} else {
tcp_cmd_write(pcb, "$>");
}
if (strncmp(tcp_buffer, "\r\n", 2) != 0) {
tcp_write(pcb, "User: ", 6, TCP_WRITE_FLAG_COPY | TCP_WRITE_FLAG_MORE);
#else
} else if (argc > 0) {
tcp_cmd_write(pcb, "$>");
} else {
tcp_cmd_write(pcb, "");
}
#endif
defer:
pbuf_free(p);
return ERR_OK;
}
/**
* @brief This function is called when a new tcp connection is accepted
* @param[in] arg The argument
* @param[in] pcb The tcp_pcb struct
* @param[in] err The error
* @return err_t ERR_OK
*/
static err_t tcp_cmd_accept(void* arg, struct tcp_pcb* pcb, err_t err) {
LWIP_UNUSED_ARG(arg);
LWIP_UNUSED_ARG(err);
@@ -186,21 +594,46 @@ static err_t tcp_cmd_accept(void* arg, struct tcp_pcb* pcb, err_t err) {
tcp_recv(pcb, tcp_cmd_recv);
tcp_err(pcb, NULL);
tcp_poll(pcb, NULL, 4);
tcp_write(pcb,
" Welcom bij de TCP CMD Interface\r\n"
"(Typ help voor een lijst van de commando's! X om te sluiten)\r\n"
"============================================================\r\n"
"User: ",
168, TCP_WRITE_FLAG_COPY | TCP_WRITE_FLAG_MORE);
tcp_sent(pcb, NULL);
LOG_INFO(TAG, "New connection accepted");
tcp_cmd_print_header(pcb);
return ERR_OK;
}
/**
* @brief This function closes the tcp connection
* @param[in] pcb The tcp_pcb struct to close
*/
static void tcp_cmd_close(struct tcp_pcb* pcb) {
tcp_arg(pcb, NULL);
tcp_sent(pcb, NULL);
tcp_recv(pcb, NULL);
tcp_close(pcb);
}
/**
* @brief This function initializes the tcp command interface
*/
void tcp_cmd_init(void) {
struct tcp_pcb* tcp_pcb;
tcp_pcb = tcp_new();
tcp_bind(tcp_pcb, IP_ADDR_ANY, 23);
if (tcp_pcb == NULL) {
LOG_CRIT(TAG, "Failed to allocate pcb");
return;
}
if (tcp_bind(tcp_pcb, IP_ADDR_ANY, 23) != ERR_OK) {
LOG_CRIT(TAG, "Failed to bind pcb");
free(tcp_pcb);
return;
}
tcp_pcb = tcp_listen(tcp_pcb);
if (tcp_pcb == NULL) {
LOG_CRIT(TAG, "Failed to listen");
free(tcp_pcb);
return;
}
tcp_accept(tcp_pcb, tcp_cmd_accept);
}

View File

@@ -12,6 +12,7 @@ add_executable(tests)
target_compile_definitions(tests
PRIVATE
"TESTING"
"DEBUG"
)
target_sources(tests
@@ -21,6 +22,7 @@ target_sources(tests
${CMAKE_SOURCE_DIR}/project/Core/Src/llfs_data.c
${CMAKE_SOURCE_DIR}/project/Core/Src/llfs.c
${CMAKE_SOURCE_DIR}/project/Core/Src/tftp.c
${CMAKE_SOURCE_DIR}/project/Core/Src/tcp_cmd.c
)
target_compile_options(tests PRIVATE $<$<CONFIG:Debug>:

View File

@@ -1,49 +1,68 @@
#include "mocs.h"
#include "tftp.h"
#include "mocs.h"
#ifdef DEBUG
#define dprint(fmt, ...) printf(fmt, ##__VA_ARGS__)
#else
#define dprint(fmt, ...)
#endif
void tftp_cleanup(void) {
dprint("tftp_cleanup\n");
}
uint32_t logger_get_timestamp(void) {
dprint("logger_get_timestamp\n");
return 0;
}
int tftp_init(struct tftp_context* context) {
UNUSED(context);
dprint("tftp_init\n");
return 0;
}
void lcd_display_text(const char* text,
uint16_t x_pos,
uint16_t y_pos,
uint32_t color,
uint32_t bg_color,
sFONT* font) {
void lcd_display_text(const char* text, uint16_t x_pos, uint16_t y_pos, uint32_t color, uint32_t bg_color, sFONT* font) {
UNUSED(text);
UNUSED(x_pos);
UNUSED(y_pos);
UNUSED(color);
UNUSED(bg_color);
UNUSED(font);
printf("lcd_display_text @ %d %d with color 0x%08X bg color 0x%08X\n%s\n", x_pos, y_pos, color, bg_color, text);
dprint("lcd_display_text @ %d %d with color 0x%08X bg color 0x%08X\n%s\n", x_pos, y_pos, color, bg_color, text);
}
void lcd_draw_img_from_fs(char* filename, uint32_t x_pos, uint32_t y_pos) {
printf("lcd_draw_img_from_fs\n%s @%d %d\n", filename, x_pos, y_pos);
UNUSED(filename);
UNUSED(x_pos);
UNUSED(y_pos);
dprint("lcd_draw_img_from_fs\n%s @%d %d\n", filename, x_pos, y_pos);
}
void lcd_draw_gif_from_fs(char* filename, uint32_t x_pos, uint32_t y_pos) {
printf("lcd_draw_gif_from_fs\n%s @%d %d\n", filename, x_pos, y_pos);
UNUSED(filename);
UNUSED(x_pos);
UNUSED(y_pos);
dprint("lcd_draw_gif_from_fs\n%s @%d %d\n", filename, x_pos, y_pos);
}
void lcd_clear_images(void) {
printf("lcd_clear_images\n");
dprint("lcd_clear_images\n");
}
void lcd_clear_text(void) {
printf("lcd_clear_text\n");
dprint("lcd_clear_text\n");
}
void lcd_draw_bmp_img(uint8_t* bmp_buff, uint32_t x_pos, uint32_t y_pos) {
UNUSED(bmp_buff);
UNUSED(x_pos);
UNUSED(y_pos);
printf("lcd_draw_bmp_img @ %d %d\n", x_pos, y_pos);
dprint("lcd_draw_bmp_img @ %d %d\n", x_pos, y_pos);
}
void lcd_draw_gif(uint8_t* gif_buff, size_t len, uint32_t x_pos, uint32_t y_pos) {
@@ -52,10 +71,11 @@ void lcd_draw_gif(uint8_t* gif_buff, size_t len, uint32_t x_pos, uint32_t y_pos)
UNUSED(x_pos);
UNUSED(y_pos);
printf("lcd_draw_gif @ %d %d\n", x_pos, y_pos);
dprint("lcd_draw_gif @ %d %d\n", x_pos, y_pos);
}
struct tcp_pcb* tcp_new(void) {
dprint("tcp_new\n");
return NULL;
}
@@ -63,48 +83,69 @@ err_t tcp_bind(void* pcb, void* ipaddr, uint16_t port) {
UNUSED(pcb);
UNUSED(ipaddr);
UNUSED(port);
dprint("tcp_bind @ %d\n", port);
return ERR_OK;
}
struct tcp_pcb* tcp_listen(void* pcb) {
UNUSED(pcb);
dprint("tcp_listen\n");
return NULL;
}
void tcp_accept(void* pcb, tcp_accept_fn arg) {
UNUSED(pcb);
UNUSED(arg);
dprint("tcp_accept\n");
}
void tcp_arg(void* pcb, void* arg) {
UNUSED(pcb);
UNUSED(arg);
dprint("tcp_arg\n");
}
void tcp_sent(void* pcb, void* arg) {
UNUSED(pcb);
UNUSED(arg);
dprint("tcp_sent\n");
}
void tcp_recv(void* pcb, tcp_recv_fn arg) {
UNUSED(pcb);
UNUSED(arg);
dprint("tcp_recv\n");
}
void tcp_setprio(void* pcb, uint8_t prio) {
UNUSED(pcb);
UNUSED(prio);
dprint("tcp_setprio\n");
}
void tcp_err(void* pcb, void* err) {
UNUSED(pcb);
UNUSED(err);
dprint("tcp_err\n");
}
void tcp_poll(void* pcb, void* poll, uint8_t interval) {
UNUSED(pcb);
UNUSED(poll);
UNUSED(interval);
dprint("tcp_poll\n");
}
void tcp_close(void* pcb) {
err_t tcp_close(void* pcb) {
UNUSED(pcb);
dprint("tcp_close\n");
return ERR_OK;
}
void tcp_write(void* pcb, const char* data, size_t len, uint8_t apiflags) {
@@ -112,23 +153,44 @@ void tcp_write(void* pcb, const char* data, size_t len, uint8_t apiflags) {
UNUSED(apiflags);
UNUSED(len);
printf("tcp_write:\n%s\n", data);
dprint("tcp_write:\n");
printf("%s\n", data);
}
void tcp_output(void* pcb) {
UNUSED(pcb);
dprint("tcp_output\n");
}
void tcp_recved(void* pcb, uint16_t len) {
UNUSED(pcb);
UNUSED(len);
dprint("tcp_recved\n");
}
size_t tcp_sndbuf(void* pcb) {
UNUSED(pcb);
dprint("tcp_sndbuf\n");
return 0;
}
void pbuf_free(struct pbuf* p) {
UNUSED(p);
dprint("pbuf_free\n");
}
uint16_t pbuf_copy_partial(const struct pbuf* buf, void* dataptr, uint16_t len, uint16_t offset) {
memcpy(dataptr, ((uint8_t*)(buf->payload)) + offset, len);
return len;
}
const char* lwip_strerr(err_t err) {
UNUSED(err);
dprint("lwip_sterr\n");
return NULL;
}

View File

@@ -13,6 +13,15 @@ extern "C" {
#define LWIP_UNUSED_ARG(x) UNUSED(x)
typedef void sFONT;
#define LCD_COLOR_BLACK 0
#define LCD_COLOR_WHITE 1
#define LCD_TRANSPARENT 2
#define LCD_FONT16 0
#define LCD_FONT24 (void*)1
typedef int8_t err_t;
/** Definitions for error constants. */
typedef enum {
@@ -111,7 +120,7 @@ void tcp_recv(void* pcb, tcp_recv_fn arg);
void tcp_setprio(void* pcb, uint8_t prio);
void tcp_err(void* pcb, void* err);
void tcp_poll(void* pcb, void* poll, uint8_t interval);
void tcp_close(void* pcb);
err_t tcp_close(void* pcb);
void tcp_write(void* pcb, const char* data, size_t len, uint8_t apiflags);
void tcp_output(void* pcb);
@@ -120,6 +129,10 @@ void pbuf_free(struct pbuf* p);
size_t tcp_sndbuf(void* pcb);
uint16_t pbuf_copy_partial(const struct pbuf* buf, void* dataptr, uint16_t len, uint16_t offset);
const char* lwip_strerr(err_t err);
#ifdef __cplusplus
}
#endif

0
tests/tcp.h Normal file
View File

252
tests/tcp_cmd.cpp Normal file
View File

@@ -0,0 +1,252 @@
#include <gtest/gtest.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "mocs.h"
extern "C" {
#include "tcp_cmd.h"
}
TEST(TCP_CMD, tcp_cmd_remove_newline) {
char* cmd = (char*)calloc(50, 1);
strcpy(cmd, "help\n");
tcp_cmd_remove_newline(cmd, strlen(cmd));
EXPECT_STREQ(cmd, "help");
strcpy(cmd, "help");
tcp_cmd_remove_newline(cmd, strlen(cmd));
EXPECT_STREQ(cmd, "help");
strcpy(cmd, "help\n\n");
tcp_cmd_remove_newline(cmd, strlen(cmd));
EXPECT_STREQ(cmd, "help");
strcpy(cmd, "\nhelp\n\n");
tcp_cmd_remove_newline(cmd, strlen(cmd));
EXPECT_STREQ(cmd, "help");
strcpy(cmd, "\n\nhelp\n\n");
tcp_cmd_remove_newline(cmd, strlen(cmd));
EXPECT_STREQ(cmd, "help");
strcpy(cmd, "\n\nhelp\n\n\n");
tcp_cmd_remove_newline(cmd, strlen(cmd));
EXPECT_STREQ(cmd, "help");
strcpy(cmd, "\n\nhelp\n\n\n\n");
tcp_cmd_remove_newline(cmd, strlen(cmd));
EXPECT_STREQ(cmd, "help");
strcpy(cmd, "\n\nhelp\n\n\n\n\n");
tcp_cmd_remove_newline(cmd, strlen(cmd));
EXPECT_STREQ(cmd, "help");
strcpy(cmd, "\n\nhelp\n\n\n\n\n\n");
tcp_cmd_remove_newline(cmd, strlen(cmd));
EXPECT_STREQ(cmd, "help");
strcpy(cmd, "\n\nhelp\n\n\n\n\n\n\n");
tcp_cmd_remove_newline(cmd, strlen(cmd));
EXPECT_STREQ(cmd, "help");
strcpy(cmd, "\n\nhelp\n\n\n\n\n\n\n\n");
tcp_cmd_remove_newline(cmd, strlen(cmd));
EXPECT_STREQ(cmd, "help");
strcpy(cmd, "\n\nhelp\n\n\n\n\n\n\n\n\n");
tcp_cmd_remove_newline(cmd, strlen(cmd));
EXPECT_STREQ(cmd, "help");
strcpy(cmd, "\n\nhel\np\n");
tcp_cmd_remove_newline(cmd, strlen(cmd));
EXPECT_STREQ(cmd, "help");
free(cmd);
}
TEST(TCP_CMD, tcp_cmd_remove_leading_space) {
char* cmd = (char*)calloc(50, 1);
strcpy(cmd, "help");
EXPECT_STREQ(tcp_cmd_remove_leading_space(cmd, strlen(cmd)), "help");
strcpy(cmd, " help");
EXPECT_STREQ(tcp_cmd_remove_leading_space(cmd, strlen(cmd)), "help");
strcpy(cmd, " help");
EXPECT_STREQ(tcp_cmd_remove_leading_space(cmd, strlen(cmd)), "help");
strcpy(cmd, " help");
EXPECT_STREQ(tcp_cmd_remove_leading_space(cmd, strlen(cmd)), "help");
strcpy(cmd, " help");
EXPECT_STREQ(tcp_cmd_remove_leading_space(cmd, strlen(cmd)), "help");
strcpy(cmd, "\n\t\r\n help");
EXPECT_STREQ(tcp_cmd_remove_leading_space(cmd, strlen(cmd)), "help");
}
TEST(TCP_CMD, tcp_data_cb) {
char* cmd = (char*)calloc(50, 1);
std::string output;
pbuf p = {.next = NULL, .payload = (void*)cmd, .tot_len = 4, .len = 0, .type_internal = 0, .flags = 0, .if_idx = 0};
strcpy(cmd, "help");
p.tot_len = (uint16_t)strlen(cmd);
testing::internal::CaptureStdout();
tcp_cmd_recv(NULL, NULL, &p, ERR_OK);
output = testing::internal::GetCapturedStdout();
#ifndef DEBUG
EXPECT_EQ(output, "help : shows a list of commands\n"
"clear text/images : clears the text or images on the lcd\n"
"text \"<text>\" : puts text on the lcd\n"
"bgColor (<a>) <r> <g> <b> : set the background color of the lcd\n"
"color (<a>) <r> <g> <b> : set the color of the text\n"
"listFiles : shows a list with images in the filesystem\n"
"setImage <image_name> : put an image on the screen\n"
"setGif <image_name> : put a gif on the screen\n"
"exit : closes the connection\n\n$>\n");
#else
EXPECT_EQ(output, "tcp_recved\ntcp_write:\n"
"help : shows a list of commands\n"
"clear text/images : clears the text or images on the lcd\n"
"text \"<text>\" : puts text on the lcd\n"
"bgColor (<a>) <r> <g> <b> : set the background color of the lcd\n"
"color (<a>) <r> <g> <b> : set the color of the text\n"
"listFiles : shows a list with images in the filesystem\n"
"setImage <image_name> : put an image on the screen\n"
"setGif <image_name> : put a gif on the screen\n"
"exit : closes the connection\n"
"\ntcp_output\ntcp_write:\n$>\ntcp_output\npbuf_free\n");
#endif
strcpy(cmd, "text \"This is printed on the display\"");
p.tot_len = (uint16_t)strlen(cmd);
testing::internal::CaptureStdout();
tcp_cmd_recv(NULL, NULL, &p, ERR_OK);
output = testing::internal::GetCapturedStdout();
#ifndef DEBUG
EXPECT_EQ(output, "$>\n");
#else
EXPECT_EQ(output, "tcp_recved\nlcd_display_text @ 10 10 with color 0xFFFFFFFF bg color 0xFF000000\nThis is printed on the display\ntcp_write:\n$>\ntcp_output\npbuf_free\n");
#endif
strcpy(cmd, "color 0x555555");
p.tot_len = (uint16_t)strlen(cmd);
testing::internal::CaptureStdout();
tcp_cmd_recv(NULL, NULL, &p, ERR_OK);
output = testing::internal::GetCapturedStdout();
#ifndef DEBUG
EXPECT_EQ(output, "$>\n");
#else
EXPECT_EQ(output, "tcp_recved\ntcp_write:\n$>\ntcp_output\npbuf_free\n");
#endif
strcpy(cmd, "bgcolor 0xAAAAAA");
p.tot_len = (uint16_t)strlen(cmd);
testing::internal::CaptureStdout();
tcp_cmd_recv(NULL, NULL, &p, ERR_OK);
output = testing::internal::GetCapturedStdout();
#ifndef DEBUG
EXPECT_EQ(output, "$>\n");
#else
EXPECT_EQ(output, "tcp_recved\ntcp_write:\n$>\ntcp_output\npbuf_free\n");
#endif
strcpy(cmd, "text \"This is printed on the display\"");
p.tot_len = (uint16_t)strlen(cmd);
testing::internal::CaptureStdout();
tcp_cmd_recv(NULL, NULL, &p, ERR_OK);
output = testing::internal::GetCapturedStdout();
#ifndef DEBUG
EXPECT_EQ(output, "$>\n");
#else
EXPECT_EQ(output, "tcp_recved\nlcd_display_text @ 10 10 with color 0x00555555 bg color 0x00AAAAAA\nThis is printed on the display\ntcp_write:\n$>\ntcp_output\npbuf_free\n");
#endif
strcpy(cmd, "color 255 255 255");
p.tot_len = (uint16_t)strlen(cmd);
testing::internal::CaptureStdout();
tcp_cmd_recv(NULL, NULL, &p, ERR_OK);
output = testing::internal::GetCapturedStdout();
#ifndef DEBUG
EXPECT_EQ(output, "$>\n");
#else
EXPECT_EQ(output, "tcp_recved\ntcp_write:\n$>\ntcp_output\npbuf_free\n");
#endif
strcpy(cmd, "bgcolor 255 255 255");
p.tot_len = (uint16_t)strlen(cmd);
testing::internal::CaptureStdout();
tcp_cmd_recv(NULL, NULL, &p, ERR_OK);
output = testing::internal::GetCapturedStdout();
#ifndef DEBUG
EXPECT_EQ(output, "$>\n");
#else
EXPECT_EQ(output, "tcp_recved\ntcp_write:\n$>\ntcp_output\npbuf_free\n");
#endif
strcpy(cmd, "text \"This is printed on the display\"");
p.tot_len = (uint16_t)strlen(cmd);
testing::internal::CaptureStdout();
tcp_cmd_recv(NULL, NULL, &p, ERR_OK);
output = testing::internal::GetCapturedStdout();
#ifndef DEBUG
EXPECT_EQ(output, "$>\n");
#else
EXPECT_EQ(output, "tcp_recved\nlcd_display_text @ 10 10 with color 0xFFFFFFFF bg color 0xFFFFFFFF\nThis is printed on the display\ntcp_write:\n$>\ntcp_output\npbuf_free\n");
#endif
strcpy(cmd, "setImage \"test.bmp\"");
p.tot_len = (uint16_t)strlen(cmd);
testing::internal::CaptureStdout();
tcp_cmd_recv(NULL, NULL, &p, ERR_OK);
output = testing::internal::GetCapturedStdout();
#ifndef DEBUG
EXPECT_EQ(output, "$>\n");
#else
EXPECT_EQ(output, "tcp_recved\nlcd_draw_img_from_fs\ntest.bmp @0 0\ntcp_write:\n$>\ntcp_output\npbuf_free\n");
#endif
strcpy(cmd, "setImage \"test.gif\"");
p.tot_len = (uint16_t)strlen(cmd);
testing::internal::CaptureStdout();
tcp_cmd_recv(NULL, NULL, &p, ERR_OK);
output = testing::internal::GetCapturedStdout();
#ifndef DEBUG
EXPECT_EQ(output, "[Warning] ( 0) [tcp_cmd]: setimage: File is not a bmp\r\nFile is not a bmp\n\n$>\n");
#else
EXPECT_EQ(output, "tcp_recved\nlogger_get_timestamp\n[Warning] ( 0) [tcp_cmd]: setimage: File is not a bmp\r\ntcp_write:\nFile is not a bmp\n\ntcp_output\ntcp_write:\n$>\ntcp_output\npbuf_free\n");
#endif
strcpy(cmd, "setGif \"test.gif\"");
p.tot_len = (uint16_t)strlen(cmd);
testing::internal::CaptureStdout();
tcp_cmd_recv(NULL, NULL, &p, ERR_OK);
output = testing::internal::GetCapturedStdout();
#ifndef DEBUG
EXPECT_EQ(output, "$>\n");
#else
EXPECT_EQ(output, "tcp_recved\nlcd_draw_gif_from_fs\ntest.gif @0 0\ntcp_write:\n$>\ntcp_output\npbuf_free\n");
#endif
strcpy(cmd, "setGif \"test.bmp\"");
p.tot_len = (uint16_t)strlen(cmd);
testing::internal::CaptureStdout();
tcp_cmd_recv(NULL, NULL, &p, ERR_OK);
output = testing::internal::GetCapturedStdout();
#ifndef DEBUG
EXPECT_EQ(output, "[Warning] ( 0) [tcp_cmd]: setgif: File is not a gif\r\nFile is not a gif\n\n$>\n");
#else
EXPECT_EQ(output, "tcp_recved\nlogger_get_timestamp\n[Warning] ( 0) [tcp_cmd]: setgif: File is not a gif\r\ntcp_write:\nFile is not a gif\n\ntcp_output\ntcp_write:\n$>\ntcp_output\npbuf_free\n");
#endif
strcpy(cmd, "exit");
p.tot_len = (uint16_t)strlen(cmd);
testing::internal::CaptureStdout();
tcp_cmd_recv(NULL, NULL, &p, ERR_OK);
output = testing::internal::GetCapturedStdout();
#ifndef DEBUG
EXPECT_EQ(output, "Exiting...\n\n");
#else
EXPECT_EQ(output, "tcp_recved\ntcp_write:\nExiting...\n\ntcp_output\nlcd_clear_images\nlcd_clear_text\ntcp_close\ntcp_arg\ntcp_sent\ntcp_recv\ntcp_close\npbuf_free\n");
#endif
free(cmd);
}