updated tabs etc

This commit is contained in:
TeunBugwood
2023-12-11 14:03:42 +01:00
parent 5b0666920c
commit d1991d9abc
2 changed files with 171 additions and 194 deletions

View File

@@ -27,8 +27,6 @@
#include "llfs.h" #include "llfs.h"
#include "lcd_api.h" #include "lcd_api.h"
/** /**
* @fn void wbe_init(void) * @fn void wbe_init(void)
* @brief Initialise the http server. It also sets the beginning screen * @brief Initialise the http server. It also sets the beginning screen
@@ -36,7 +34,6 @@
*/ */
void wbe_init(void); void wbe_init(void);
/** /**
* @fn int fs_open_custom(struct fs_file*, const char*) * @fn int fs_open_custom(struct fs_file*, const char*)
* @brief Function is called when the frontend wants to reach/open a file * @brief Function is called when the frontend wants to reach/open a file
@@ -48,7 +45,6 @@ void wbe_init(void);
*/ */
int fs_open_custom(struct fs_file* file, const char* name); int fs_open_custom(struct fs_file* file, const char* name);
/** /**
* @fn void fs_close_custom(struct fs_file*) * @fn void fs_close_custom(struct fs_file*)
* @brief Called when closing files and endpoints * @brief Called when closing files and endpoints
@@ -57,7 +53,6 @@ int fs_open_custom(struct fs_file* file, const char* name);
*/ */
void fs_close_custom(struct fs_file* file); void fs_close_custom(struct fs_file* file);
/** /**
* @fn void httpd_cgi_handler(struct fs_file*, const char*, int, char**, char**) * @fn void httpd_cgi_handler(struct fs_file*, const char*, int, char**, char**)
* @brief When data is send from the frontend to the backend with the CGI * @brief When data is send from the frontend to the backend with the CGI
@@ -70,8 +65,7 @@ void fs_close_custom(struct fs_file* file);
* @param[in] pc_param, contains the name of each parameter * @param[in] pc_param, contains the name of each parameter
* @param[in] pc_value, is the value of each parameter * @param[in] pc_value, is the value of each parameter
*/ */
void httpd_cgi_handler(struct fs_file* file, const char* uri, int num_parm, char** pc_param, char** pc_value ); void httpd_cgi_handler(struct fs_file* file, const char* uri, int num_parm, char** pc_param, char** pc_value);
/** /**
* @fn void wbe_display(const char*, const uint32_t, const uint32_t, const char*) * @fn void wbe_display(const char*, const uint32_t, const uint32_t, const char*)
@@ -85,5 +79,4 @@ void httpd_cgi_handler(struct fs_file* file, const char* uri, int num_parm, char
*/ */
void wbe_display(const char* txt, const uint32_t txt_color, const uint32_t background_color, const char* image); void wbe_display(const char* txt, const uint32_t txt_color, const uint32_t background_color, const char* image);
#endif /* INC_WEBSITE_BACKEND_ */ #endif /* INC_WEBSITE_BACKEND_ */

View File

@@ -13,87 +13,81 @@ static inline void wbe_build_infostring(const llfs_file_t* file_list, char* info
static void wbe_decoding_url(const char* encoded, char* decoded); static void wbe_decoding_url(const char* encoded, char* decoded);
static uint32_t wbe_color_value(const char* rgb); static uint32_t wbe_color_value(const char* rgb);
void wbe_init(void) { void wbe_init(void) {
httpd_init(); httpd_init();
LOG_DEBUG("WBE", "Initialize webserver"); LOG_DEBUG("WBE", "Initialize webserver");
// Set starting LCD screen // Set starting LCD screen
wbe_display("Please Edit Me !", LCD_GREEN, LCD_BLACK, SD_IMG); wbe_display("Please Edit Me !", LCD_GREEN, LCD_BLACK, SD_IMG);
return; return;
} }
int fs_open_custom(struct fs_file* file, const char* name) { int fs_open_custom(struct fs_file* file, const char* name) {
// Variables
llfs_file_t *wanted_file = llfs_file_open(name + 1);
size_t buffer_len = 0;
static char image_stringbuffer[STR_MAX] = "";
// Variables // The wanted file was found in the filesystem
llfs_file_t* wanted_file = llfs_file_open(name + 1); if (wanted_file != NULL) {
size_t buffer_len = 0;
static char image_stringbuffer[STR_MAX] = "";
// Debug info
LOG_DEBUG("WBE", "The file : %s was found", wanted_file->name);
// The wanted file was found in the filesystem // Give the information about the file to the client
if (wanted_file != NULL) { file->data = (const char*)wanted_file->data;
file->len = (int)wanted_file->len;
file->index = (int)wanted_file->len;
file->is_custom_file = 1;
file->pextension = NULL;
file->flags = FS_FILE_FLAGS_HEADER_INCLUDED;
// Debug info return 1;
LOG_DEBUG("WBE", "The file : %s was found", wanted_file->name); }
// Give the information about the file to the client /*
file->data = (const char*)wanted_file->data; * The endpoint '/images.info' is called when the client wants to call for the
file->len = (int)wanted_file->len; * information string that contains the names of the available images.
file->index = (int)wanted_file->len; */
file->is_custom_file = 1; if (strncmp(name, "/images.info", strlen("/images.info")) == 0) {
file->pextension = NULL;
file->flags = FS_FILE_FLAGS_HEADER_INCLUDED;
return 1; // Create the information string for the images in the filesystem
} buffer_len = wbe_get_images(image_stringbuffer, llfs_file_count());
// Give the string back to the client
file->data = image_stringbuffer;
file->len = (int)buffer_len;
file->index = (int)buffer_len;
file->is_custom_file = 1;
file->pextension = NULL;
file->flags = FS_FILE_FLAGS_HEADER_INCLUDED;
/* return 1;
* The endpoint '/images.info' is called when the client wants to call for the }
* information string that contains the names of the available images.
*/
if (strncmp(name, "/images.info", strlen("/images.info")) == 0) {
// Create the information string for the images in the filesystem // Endpoint when client wants to send information to the backend
buffer_len = wbe_get_images(image_stringbuffer, llfs_file_count()); if (strncmp(name, "/cgi", 3) == 0) {
// Give the string back to the client // Give the original index.html back to the client
file->data = image_stringbuffer; wanted_file = llfs_file_open("index.html");
file->len = (int)buffer_len;
file->index = (int)buffer_len;
file->is_custom_file = 1;
file->pextension = NULL;
file->flags = FS_FILE_FLAGS_HEADER_INCLUDED;
return 1; if (wanted_file != NULL) {
} file->data = (const char*)wanted_file->data;
file->len = (int)wanted_file->len;
file->index = (int)wanted_file->len;
file->is_custom_file = 1;
file->pextension = NULL;
file->flags = FS_FILE_FLAGS_HEADER_INCLUDED;
return 1;
}
// Endpoint when client wants to send information to the backend }
if (strncmp(name, "/cgi", 3) == 0) {
// Give the original index.html back to the client return 0;
wanted_file = llfs_file_open("index.html");
if (wanted_file != NULL) {
file->data = (const char*)wanted_file->data;
file->len = (int)wanted_file->len;
file->index = (int)wanted_file->len;
file->is_custom_file = 1;
file->pextension = NULL;
file->flags = FS_FILE_FLAGS_HEADER_INCLUDED;
return 1;
}
}
return 0;
} }
void fs_close_custom(struct fs_file* file) { void fs_close_custom(struct fs_file* file) {
@@ -110,30 +104,29 @@ void fs_close_custom(struct fs_file* file) {
*/ */
static size_t wbe_get_images(char* images_string, size_t file_count_fs) { static size_t wbe_get_images(char* images_string, size_t file_count_fs) {
// Allocate space for the files // Allocate space for the files
llfs_file_t file_list[file_count_fs]; llfs_file_t file_list[file_count_fs];
size_t file_count_bmp = 0; size_t file_count_bmp = 0;
size_t file_count_gif = 0; size_t file_count_gif = 0;
// Reset string // Reset string
strcpy(images_string, ""); strcpy(images_string, "");
// Get all the ".bmp" and ".gif" files // Get all the ".bmp" and ".gif" files
file_count_bmp = llfs_file_list(file_list, file_count_fs, ".bmp"); file_count_bmp = llfs_file_list(file_list, file_count_fs, ".bmp");
file_count_gif = llfs_file_list(file_list + file_count_bmp, file_count_fs, ".gif"); file_count_gif = llfs_file_list(file_list + file_count_bmp, file_count_fs, ".gif");
// Create the info string // Create the info string
wbe_build_infostring(file_list, images_string, file_count_gif + file_count_bmp); wbe_build_infostring(file_list, images_string, file_count_gif + file_count_bmp);
// Debug info // Debug info
LOG_DEBUG("WBE", "Images string : %s ",images_string); LOG_DEBUG("WBE", "Images string : %s ", images_string);
// Return the length of the string // Return the length of the string
return strlen(images_string); return strlen(images_string);
} }
/** /**
* @fn void wbe_build_infostring(const llfs_file_t*, char*, const size_t) * @fn void wbe_build_infostring(const llfs_file_t*, char*, const size_t)
* @brief Function adds the names of the files in "file_list" and adds them in "infoStr". * @brief Function adds the names of the files in "file_list" and adds them in "infoStr".
@@ -145,80 +138,76 @@ static size_t wbe_get_images(char* images_string, size_t file_count_fs) {
*/ */
static inline void wbe_build_infostring(const llfs_file_t* file_list, char* info_string, const size_t cnt) { static inline void wbe_build_infostring(const llfs_file_t* file_list, char* info_string, const size_t cnt) {
// Add the filenames to the info string // Add the filenames to the info string
for (size_t i = 0; i < cnt; ++i) { for (size_t i = 0; i < cnt; ++i) {
strncat(info_string, file_list[i].name, strlen(file_list[i].name)); strncat(info_string, file_list[i].name, strlen(file_list[i].name));
strncat(info_string, "|", 2); strncat(info_string, "|", 2);
} }
//Remove last seperator '|' //Remove last seperator '|'
info_string[strlen(info_string) - 1] = '\0'; info_string[strlen(info_string) - 1] = '\0';
} }
void httpd_cgi_handler(struct fs_file* file, const char* uri, int num_parm, char** pc_param, char** pc_value) { void httpd_cgi_handler(struct fs_file* file, const char* uri, int num_parm, char** pc_param, char** pc_value) {
// Variables // Variables
uint32_t vktxt = LCD_GREEN; uint32_t vktxt = LCD_GREEN;
uint32_t vka = LCD_BLACK; uint32_t vka = LCD_BLACK;
// Allocate space for parameters // Allocate space for parameters
char vtxt[STR_MAX]; // The sentence that we want to print on the LCD. char vtxt[STR_MAX]; // The sentence that we want to print on the LCD.
char vfo[STR_MAX]; // Name of the selected image on the website. char vfo[STR_MAX]; // Name of the selected image on the website.
char s_vktxt[CLR_MAX]; // The color of the sentence. (in #RGB) char s_vktxt[CLR_MAX]; // The color of the sentence. (in #RGB)
char s_vka[CLR_MAX]; // Color of the background. (also in #RGB) char s_vka[CLR_MAX]; // Color of the background. (also in #RGB)
if (strcmp("/cgi", uri) == 0) {
if (strcmp("/cgi", uri) == 0) { for (size_t i = 0; i < num_parm; ++i) {
for (size_t i = 0; i < num_parm; ++i) { // The given sentence
if (strcmp("vtxt", pc_param[i]) == 0) {
wbe_decoding_url(pc_value[i], vtxt);
}
// The given sentence // The color of the sentence
if (strcmp("vtxt", pc_param[i]) == 0) { if (strcmp("vktxt", pc_param[i]) == 0) {
wbe_decoding_url(pc_value[i], vtxt); wbe_decoding_url(pc_value[i], s_vktxt);
}
// The color of the sentence // Convert to hex value
if (strcmp("vktxt", pc_param[i]) == 0) { if (s_vktxt != NULL) {
wbe_decoding_url(pc_value[i], s_vktxt); vktxt = wbe_color_value(s_vktxt);
}
}
// Convert to hex value // Color of the background
if (s_vktxt != NULL) { if (strcmp("vka", pc_param[i]) == 0) {
vktxt = wbe_color_value(s_vktxt); wbe_decoding_url(pc_value[i], s_vka);
}
}
// Color of the background // convert to hex value
if (strcmp("vka", pc_param[i]) == 0) { if (s_vka != NULL) {
wbe_decoding_url(pc_value[i], s_vka); vka = wbe_color_value(s_vka);
}
// convert to hex value }
if (s_vka != NULL) {
vka = wbe_color_value(s_vka);
}
} // Name of the image
if (strcmp("vfo", pc_param[i]) == 0) {
wbe_decoding_url(pc_value[i], vfo);
}
}
// Name of the image // Debug info
if (strcmp("vfo", pc_param[i]) == 0) { LOG_DEBUG("WBE", "CGI DATA : %s , %x , %x , %s", vtxt, vktxt, vka, vfo);
wbe_decoding_url(pc_value[i], vfo);
}
}
// Debug info // Display on LCD
LOG_DEBUG("WBE", "CGI DATA : %s , %x , %x , %s", vtxt, vktxt, vka, vfo); wbe_display(vtxt, vktxt, vka, vfo);
// Display on LCD }
wbe_display(vtxt, vktxt, vka, vfo);
}
} }
/** /**
* @fn void wbe_decoding_url(const char*, char*) * @fn void wbe_decoding_url(const char*, char*)
* @brief The given information from the CGI parameters are URL encoded. * @brief The given information from the CGI parameters are URL encoded.
@@ -231,86 +220,81 @@ void httpd_cgi_handler(struct fs_file* file, const char* uri, int num_parm, char
*/ */
static void wbe_decoding_url(const char* encoded, char* decoded) { static void wbe_decoding_url(const char* encoded, char* decoded) {
// Variables // Variables
char* endptr; char *endptr;
size_t decoded_index = 0; size_t decoded_index = 0;
size_t encoded_length = strlen(encoded); size_t encoded_length = strlen(encoded);
uint32_t hex_val; uint32_t hex_val;
for (size_t i = 0; i < encoded_length; ++i) { for (size_t i = 0; i < encoded_length; ++i) {
// If we encounter a %xx, decode it to ascii // If we encounter a %xx, decode it to ascii
if ((encoded[i] == '%') && (i + 2 < encoded_length)) { if ((encoded[i] == '%') && (i + 2 < encoded_length)) {
// Decode %xx // Decode %xx
hex_val = (uint32_t)strtoul(&encoded[i + 1], &endptr, 16); hex_val = (uint32_t)strtoul(&encoded[i + 1], &endptr, 16);
// Check for conversion errors // Check for conversion errors
if (endptr == &encoded[i + 1] || *endptr != '\0') { if (endptr == &encoded[i + 1] || *endptr != '\0') {
LOG_DEBUG("WBE", "URL text conversion error"); LOG_DEBUG("WBE", "URL text conversion error");
} }
decoded[decoded_index++] = (char)hex_val; decoded[decoded_index++] = (char)hex_val;
// Skip 2 characters // Skip 2 characters
i += 2; i += 2;
// If we encounter a +, add a space character // If we encounter a +, add a space character
} else if (encoded[i] == '+') { } else if (encoded[i] == '+') {
decoded[decoded_index++] = ' '; decoded[decoded_index++] = ' ';
// If no % or +, just put what stands in 'encoded' // If no % or +, just put what stands in 'encoded'
} else { } else {
decoded[decoded_index++] = encoded[i]; decoded[decoded_index++] = encoded[i];
} }
} }
// Finish the string // Finish the string
decoded[decoded_index] = '\0'; decoded[decoded_index] = '\0';
} }
static uint32_t wbe_color_value(const char* rgb) { static uint32_t wbe_color_value(const char* rgb) {
// Variables // Variables
char* endptr; char *endptr;
uint32_t color = LCD_BLACK; uint32_t color = LCD_BLACK;
char argb[11] = "0xff"; char argb[11] = "0xff";
// Create argb string (also, skip the '#' char) // Create argb string (also, skip the '#' char)
strncat(argb, rgb + 1, strlen(rgb + 1)); strncat(argb, rgb + 1, strlen(rgb + 1));
// Get argb value // Get argb value
color = (uint32_t)strtoul(argb, &endptr, 16); color = (uint32_t)strtoul(argb, &endptr, 16);
// Check for conversion errors // Check for conversion errors
if (*endptr != '\0') { if (*endptr != '\0') {
color = LCD_BLACK; color = LCD_BLACK;
LOG_DEBUG("WBE", "URL color conversion error"); LOG_DEBUG("WBE", "URL color conversion error");
} }
return color; return color;
} }
void wbe_display(const char* txt, const uint32_t txt_color, const uint32_t bg_color, const char* image) { void wbe_display(const char* txt, const uint32_t txt_color, const uint32_t bg_color, const char* image) {
// Variables
lcd_gif_t *gif;
char *extension;
// Variables // clear the screen
lcd_gif_t* gif; lcd_clear_text();
char* extension; lcd_clear_images();
// clear the screen // Display the text and the background
lcd_clear_text(); lcd_set_bg_color_layer0(bg_color);
lcd_clear_images();
// Display the text and the background
lcd_set_bg_color_layer0(bg_color);
lcd_display_text(txt, 10, 10, txt_color, bg_color, LCD_FONT16); lcd_display_text(txt, 10, 10, txt_color, bg_color, LCD_FONT16);
// Get the extension of the file // Get the extension of the file
@@ -318,25 +302,25 @@ void wbe_display(const char* txt, const uint32_t txt_color, const uint32_t bg_co
// Check extension // Check extension
if (extension != NULL) { if (extension != NULL) {
// Draw bmp // Draw bmp
if (strcmp(extension, "bmp") == 0) { if (strcmp(extension, "bmp") == 0) {
lcd_draw_img_from_fs(image, IMG_X, IMG_Y); lcd_draw_img_from_fs(image, IMG_X, IMG_Y);
// Draw gif // Draw gif
} else if (strcmp(extension, "gif") == 0) { } else if (strcmp(extension, "gif") == 0) {
gif = lcd_draw_gif_from_fs(image, IMG_X, IMG_Y); gif = lcd_draw_gif_from_fs(image, IMG_X, IMG_Y);
if (gif == NULL) { if (gif == NULL) {
LOG_DEBUG("WBE", "GIF could not be drawn"); LOG_DEBUG("WBE", "GIF could not be drawn");
} }
// If nothing, then draw the standard image // If nothing, then draw the standard image
} else { } else {
lcd_draw_img_from_fs(SD_IMG, IMG_X, IMG_Y); lcd_draw_img_from_fs(SD_IMG, IMG_X, IMG_Y);
} }
// If nothing, then draw the standard image // If nothing, then draw the standard image
} else { } else {
lcd_draw_img_from_fs(SD_IMG, IMG_X, IMG_Y); lcd_draw_img_from_fs(SD_IMG, IMG_X, IMG_Y);
} }
} }