Merge pull request #14 from OldManSteve/main
Webserver working with freertos
This commit is contained in:
commit
6fc93eb7bb
|
@ -23,3 +23,10 @@
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
// This section enables HTTPD server with SSI, SGI
|
||||||
|
// and tells server which converted HTML files to use
|
||||||
|
#define LWIP_HTTPD 1
|
||||||
|
#define LWIP_HTTPD_SSI 1
|
||||||
|
#define LWIP_HTTPD_CGI 1
|
||||||
|
#define LWIP_HTTPD_SSI_INCLUDE_TAG 0
|
||||||
|
#define HTTPD_FSDATA_FILE "htmldata.c"
|
|
@ -3,12 +3,6 @@ add_executable(
|
||||||
frontend.c
|
frontend.c
|
||||||
)
|
)
|
||||||
|
|
||||||
#message("Running makefsdata python script")
|
|
||||||
#execute_process(COMMAND
|
|
||||||
# py makefsdata.py
|
|
||||||
# WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}
|
|
||||||
#)
|
|
||||||
|
|
||||||
message("Running makefsdata C script")
|
message("Running makefsdata C script")
|
||||||
execute_process(COMMAND
|
execute_process(COMMAND
|
||||||
gcc makefsdata.c -o makefsdata.exe
|
gcc makefsdata.c -o makefsdata.exe
|
||||||
|
@ -24,15 +18,14 @@ target_link_libraries(
|
||||||
frontend
|
frontend
|
||||||
hardware_adc
|
hardware_adc
|
||||||
pico_stdlib
|
pico_stdlib
|
||||||
#FreeRTOS-Kernel-Heap4 # FreeRTOS kernel and dynamic heap
|
FreeRTOS-Kernel-Heap4 # FreeRTOS kernel and dynamic heap
|
||||||
pico_cyw43_arch_lwip_threadsafe_background
|
pico_cyw43_arch_lwip_sys_freertos
|
||||||
pico_lwip_http
|
pico_lwip_http
|
||||||
)
|
)
|
||||||
|
|
||||||
target_include_directories(frontend PRIVATE
|
target_include_directories(frontend PRIVATE
|
||||||
#../config
|
../config
|
||||||
${CMAKE_CURRENT_LIST_DIR}
|
${CMAKE_CURRENT_LIST_DIR}
|
||||||
${CMAKE_CURRENT_LIST_DIR}../config
|
|
||||||
)
|
)
|
||||||
|
|
||||||
pico_enable_stdio_usb(frontend 1)
|
pico_enable_stdio_usb(frontend 1)
|
||||||
|
|
|
@ -1,143 +0,0 @@
|
||||||
/*
|
|
||||||
* FreeRTOS V202111.00
|
|
||||||
* Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
||||||
* this software and associated documentation files (the "Software"), to deal in
|
|
||||||
* the Software without restriction, including without limitation the rights to
|
|
||||||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
||||||
* the Software, and to permit persons to whom the Software is furnished to do so,
|
|
||||||
* subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be included in all
|
|
||||||
* copies or substantial portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
||||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
||||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
||||||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
||||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
||||||
*
|
|
||||||
* http://www.FreeRTOS.org
|
|
||||||
* http://aws.amazon.com/freertos
|
|
||||||
*
|
|
||||||
* 1 tab == 4 spaces!
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef FREERTOS_CONFIG_H
|
|
||||||
#define FREERTOS_CONFIG_H
|
|
||||||
|
|
||||||
/*-----------------------------------------------------------
|
|
||||||
* Application specific definitions.
|
|
||||||
*
|
|
||||||
* These definitions should be adjusted for your particular hardware and
|
|
||||||
* application requirements.
|
|
||||||
*
|
|
||||||
* THESE PARAMETERS ARE DESCRIBED WITHIN THE 'CONFIGURATION' SECTION OF THE
|
|
||||||
* FreeRTOS API DOCUMENTATION AVAILABLE ON THE FreeRTOS.org WEB SITE.
|
|
||||||
*
|
|
||||||
* See http://www.freertos.org/a00110.html
|
|
||||||
*----------------------------------------------------------*/
|
|
||||||
|
|
||||||
/* Scheduler Related */
|
|
||||||
#define configUSE_PREEMPTION 1
|
|
||||||
#define configUSE_TICKLESS_IDLE 0
|
|
||||||
#define configUSE_IDLE_HOOK 0
|
|
||||||
#define configUSE_TICK_HOOK 0
|
|
||||||
#define configTICK_RATE_HZ ( ( TickType_t ) 1000 )
|
|
||||||
#define configMAX_PRIORITIES 32
|
|
||||||
#define configMINIMAL_STACK_SIZE ( configSTACK_DEPTH_TYPE ) 256
|
|
||||||
#define configUSE_16_BIT_TICKS 0
|
|
||||||
|
|
||||||
#define configIDLE_SHOULD_YIELD 1
|
|
||||||
|
|
||||||
/* Synchronization Related */
|
|
||||||
#define configUSE_MUTEXES 1
|
|
||||||
#define configUSE_RECURSIVE_MUTEXES 1
|
|
||||||
#define configUSE_APPLICATION_TASK_TAG 0
|
|
||||||
#define configUSE_COUNTING_SEMAPHORES 1
|
|
||||||
#define configQUEUE_REGISTRY_SIZE 8
|
|
||||||
#define configUSE_QUEUE_SETS 1
|
|
||||||
#define configUSE_TIME_SLICING 1
|
|
||||||
#define configUSE_NEWLIB_REENTRANT 0
|
|
||||||
// todo need this for lwip FreeRTOS sys_arch to compile
|
|
||||||
#define configENABLE_BACKWARD_COMPATIBILITY 1
|
|
||||||
#define configNUM_THREAD_LOCAL_STORAGE_POINTERS 5
|
|
||||||
|
|
||||||
/* System */
|
|
||||||
#define configSTACK_DEPTH_TYPE uint32_t
|
|
||||||
#define configMESSAGE_BUFFER_LENGTH_TYPE size_t
|
|
||||||
|
|
||||||
/* Memory allocation related definitions. */
|
|
||||||
#define configSUPPORT_STATIC_ALLOCATION 0
|
|
||||||
#define configSUPPORT_DYNAMIC_ALLOCATION 1
|
|
||||||
#define configTOTAL_HEAP_SIZE (128*1024)
|
|
||||||
#define configAPPLICATION_ALLOCATED_HEAP 0
|
|
||||||
|
|
||||||
/* Hook function related definitions. */
|
|
||||||
#define configCHECK_FOR_STACK_OVERFLOW 0
|
|
||||||
#define configUSE_MALLOC_FAILED_HOOK 0
|
|
||||||
#define configUSE_DAEMON_TASK_STARTUP_HOOK 0
|
|
||||||
|
|
||||||
/* Run time and task stats gathering related definitions. */
|
|
||||||
#define configGENERATE_RUN_TIME_STATS 0
|
|
||||||
#define configUSE_TRACE_FACILITY 1
|
|
||||||
#define configUSE_STATS_FORMATTING_FUNCTIONS 0
|
|
||||||
|
|
||||||
/* Co-routine related definitions. */
|
|
||||||
#define configUSE_CO_ROUTINES 0
|
|
||||||
#define configMAX_CO_ROUTINE_PRIORITIES 1
|
|
||||||
|
|
||||||
/* Software timer related definitions. */
|
|
||||||
#define configUSE_TIMERS 1
|
|
||||||
#define configTIMER_TASK_PRIORITY ( configMAX_PRIORITIES - 1 )
|
|
||||||
#define configTIMER_QUEUE_LENGTH 10
|
|
||||||
#define configTIMER_TASK_STACK_DEPTH 1024
|
|
||||||
|
|
||||||
/* Interrupt nesting behaviour configuration. */
|
|
||||||
/*
|
|
||||||
#define configKERNEL_INTERRUPT_PRIORITY [dependent of processor]
|
|
||||||
#define configMAX_SYSCALL_INTERRUPT_PRIORITY [dependent on processor and application]
|
|
||||||
#define configMAX_API_CALL_INTERRUPT_PRIORITY [dependent on processor and application]
|
|
||||||
*/
|
|
||||||
|
|
||||||
#if FREE_RTOS_KERNEL_SMP // set by the RP2040 SMP port of FreeRTOS
|
|
||||||
/* SMP port only */
|
|
||||||
#define configNUM_CORES 1
|
|
||||||
#define configTICK_CORE 0
|
|
||||||
#define configRUN_MULTIPLE_PRIORITIES 1
|
|
||||||
#define configUSE_CORE_AFFINITY 0
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/* RP2040 specific */
|
|
||||||
#define configSUPPORT_PICO_SYNC_INTEROP 1
|
|
||||||
#define configSUPPORT_PICO_TIME_INTEROP 1
|
|
||||||
|
|
||||||
#include <assert.h>
|
|
||||||
/* Define to trap errors during development. */
|
|
||||||
#define configASSERT(x) assert(x)
|
|
||||||
|
|
||||||
/* Set the following definitions to 1 to include the API function, or zero
|
|
||||||
to exclude the API function. */
|
|
||||||
#define INCLUDE_vTaskPrioritySet 1
|
|
||||||
#define INCLUDE_uxTaskPriorityGet 1
|
|
||||||
#define INCLUDE_vTaskDelete 1
|
|
||||||
#define INCLUDE_vTaskSuspend 1
|
|
||||||
#define INCLUDE_vTaskDelayUntil 1
|
|
||||||
#define INCLUDE_vTaskDelay 1
|
|
||||||
#define INCLUDE_xTaskGetSchedulerState 1
|
|
||||||
#define INCLUDE_xTaskGetCurrentTaskHandle 1
|
|
||||||
#define INCLUDE_uxTaskGetStackHighWaterMark 1
|
|
||||||
#define INCLUDE_xTaskGetIdleTaskHandle 1
|
|
||||||
#define INCLUDE_eTaskGetState 1
|
|
||||||
#define INCLUDE_xTimerPendFunctionCall 1
|
|
||||||
#define INCLUDE_xTaskAbortDelay 1
|
|
||||||
#define INCLUDE_xTaskGetHandle 1
|
|
||||||
#define INCLUDE_xTaskResumeFromISR 1
|
|
||||||
#define INCLUDE_xQueueGetMutexHolder 1
|
|
||||||
|
|
||||||
/* A header file that defines trace macro can be included here. */
|
|
||||||
|
|
||||||
#endif /* FREERTOS_CONFIG_H */
|
|
||||||
|
|
|
@ -4,10 +4,8 @@
|
||||||
#include "lwipopts.h"
|
#include "lwipopts.h"
|
||||||
#include "ssi.h"
|
#include "ssi.h"
|
||||||
#include "cgi.h"
|
#include "cgi.h"
|
||||||
#include "lwip/inet.h"
|
#include "FreeRTOS.h"
|
||||||
//#include "FreeRTOS.h"
|
#include "task.h"
|
||||||
//#include "task.h"
|
|
||||||
#include "lwip/sockets.h"
|
|
||||||
|
|
||||||
// WIFI Credentials - take care if pushing to GitHub!
|
// WIFI Credentials - take care if pushing to GitHub!
|
||||||
const char WIFI_SSID[] = "XXX";
|
const char WIFI_SSID[] = "XXX";
|
||||||
|
@ -23,23 +21,7 @@ void print_ip_address() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
static void webserver_run(){
|
||||||
stdio_init_all();
|
|
||||||
|
|
||||||
cyw43_arch_init();
|
|
||||||
|
|
||||||
cyw43_arch_enable_sta_mode();
|
|
||||||
|
|
||||||
// Connect to the WiFI network - loop until connected
|
|
||||||
while(cyw43_arch_wifi_connect_timeout_ms(WIFI_SSID, WIFI_PASSWORD, CYW43_AUTH_WPA2_AES_PSK, 30000) != 0){
|
|
||||||
printf("Attempting to connect...\n");
|
|
||||||
}
|
|
||||||
// Print a success message once connected
|
|
||||||
printf("Connected! \n");
|
|
||||||
|
|
||||||
// Print the assigned IP address
|
|
||||||
print_ip_address();
|
|
||||||
|
|
||||||
// Initialize web server
|
// Initialize web server
|
||||||
httpd_init();
|
httpd_init();
|
||||||
printf("Http server initialized\n");
|
printf("Http server initialized\n");
|
||||||
|
@ -51,6 +33,42 @@ int main() {
|
||||||
printf("CGI Handler initialized\n");
|
printf("CGI Handler initialized\n");
|
||||||
|
|
||||||
// Infinite loop
|
// Infinite loop
|
||||||
while(1);
|
while (1) {
|
||||||
|
// Add any required delay or use other FreeRTOS features here if needed
|
||||||
|
vTaskDelay(pdMS_TO_TICKS(1000));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
static void webserver_task(){
|
||||||
|
if (cyw43_arch_init())
|
||||||
|
{
|
||||||
|
printf("failed to initialise\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
cyw43_arch_enable_sta_mode();
|
||||||
|
|
||||||
|
printf("Connecting to WiFi...\n");
|
||||||
|
|
||||||
|
// Connect to the WiFI network - loop until connected
|
||||||
|
while (cyw43_arch_wifi_connect_timeout_ms(WIFI_SSID, WIFI_PASSWORD, CYW43_AUTH_WPA2_AES_PSK, 30000) != 0) {
|
||||||
|
printf("Attempting to connect...\n");
|
||||||
|
}
|
||||||
|
// Print a success message once connected
|
||||||
|
printf("Connected! \n");
|
||||||
|
print_ip_address();
|
||||||
|
|
||||||
|
webserver_run();
|
||||||
|
cyw43_arch_deinit();
|
||||||
|
}
|
||||||
|
int main() {
|
||||||
|
stdio_init_all();
|
||||||
|
|
||||||
|
// Create the FreeRTOS task for the web server
|
||||||
|
TaskHandle_t task_handle;
|
||||||
|
xTaskCreate(webserver_task, "WebServerTask", configMINIMAL_STACK_SIZE, NULL, 1, &task_handle);
|
||||||
|
|
||||||
|
// Start the FreeRTOS scheduler
|
||||||
|
vTaskStartScheduler();
|
||||||
|
|
||||||
|
while (1);
|
||||||
}
|
}
|
|
@ -1,93 +0,0 @@
|
||||||
// Common settings used in most of the pico_w examples
|
|
||||||
// (see https://www.nongnu.org/lwip/2_1_x/group__lwip__opts.html for details)]
|
|
||||||
|
|
||||||
// allow override in some examples
|
|
||||||
#ifndef NO_SYS
|
|
||||||
#define NO_SYS 1
|
|
||||||
#endif
|
|
||||||
// allow override in some examples
|
|
||||||
#ifndef LWIP_SOCKET
|
|
||||||
#define LWIP_SOCKET 0
|
|
||||||
#endif
|
|
||||||
#if PICO_CYW43_ARCH_POLL
|
|
||||||
#define MEM_LIBC_MALLOC 1
|
|
||||||
#else
|
|
||||||
// MEM_LIBC_MALLOC is incompatible with non polling versions
|
|
||||||
#define MEM_LIBC_MALLOC 0
|
|
||||||
#endif
|
|
||||||
#define MEM_ALIGNMENT 4
|
|
||||||
#define MEM_SIZE 4000
|
|
||||||
#define MEMP_NUM_TCP_SEG 32
|
|
||||||
#define MEMP_NUM_ARP_QUEUE 10
|
|
||||||
#define PBUF_POOL_SIZE 24
|
|
||||||
#define LWIP_ARP 1
|
|
||||||
#define LWIP_ETHERNET 1
|
|
||||||
#define LWIP_ICMP 1
|
|
||||||
#define LWIP_RAW 1
|
|
||||||
#define TCP_WND (8 * TCP_MSS)
|
|
||||||
#define TCP_MSS 1460
|
|
||||||
#define TCP_SND_BUF (8 * TCP_MSS)
|
|
||||||
#define TCP_SND_QUEUELEN ((4 * (TCP_SND_BUF) + (TCP_MSS - 1)) / (TCP_MSS))
|
|
||||||
#define LWIP_NETIF_STATUS_CALLBACK 1
|
|
||||||
#define LWIP_NETIF_LINK_CALLBACK 1
|
|
||||||
#define LWIP_NETIF_HOSTNAME 1
|
|
||||||
#define LWIP_NETCONN 0
|
|
||||||
#define MEM_STATS 0
|
|
||||||
#define SYS_STATS 0
|
|
||||||
#define MEMP_STATS 0
|
|
||||||
#define LINK_STATS 0
|
|
||||||
// #define ETH_PAD_SIZE 2
|
|
||||||
#define LWIP_CHKSUM_ALGORITHM 3
|
|
||||||
#define LWIP_DHCP 1
|
|
||||||
#define LWIP_IPV4 1
|
|
||||||
#define LWIP_TCP 1
|
|
||||||
#define LWIP_UDP 1
|
|
||||||
#define LWIP_DNS 1
|
|
||||||
#define LWIP_TCP_KEEPALIVE 1
|
|
||||||
#define LWIP_NETIF_TX_SINGLE_PBUF 1
|
|
||||||
#define DHCP_DOES_ARP_CHECK 0
|
|
||||||
#define LWIP_DHCP_DOES_ACD_CHECK 0
|
|
||||||
|
|
||||||
#ifndef NDEBUG
|
|
||||||
#define LWIP_DEBUG 1
|
|
||||||
#define LWIP_STATS 1
|
|
||||||
#define LWIP_STATS_DISPLAY 1
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#define ETHARP_DEBUG LWIP_DBG_OFF
|
|
||||||
#define NETIF_DEBUG LWIP_DBG_OFF
|
|
||||||
#define PBUF_DEBUG LWIP_DBG_OFF
|
|
||||||
#define API_LIB_DEBUG LWIP_DBG_OFF
|
|
||||||
#define API_MSG_DEBUG LWIP_DBG_OFF
|
|
||||||
#define SOCKETS_DEBUG LWIP_DBG_OFF
|
|
||||||
#define ICMP_DEBUG LWIP_DBG_OFF
|
|
||||||
#define INET_DEBUG LWIP_DBG_OFF
|
|
||||||
#define IP_DEBUG LWIP_DBG_OFF
|
|
||||||
#define IP_REASS_DEBUG LWIP_DBG_OFF
|
|
||||||
#define RAW_DEBUG LWIP_DBG_OFF
|
|
||||||
#define MEM_DEBUG LWIP_DBG_OFF
|
|
||||||
#define MEMP_DEBUG LWIP_DBG_OFF
|
|
||||||
#define SYS_DEBUG LWIP_DBG_OFF
|
|
||||||
#define TCP_DEBUG LWIP_DBG_OFF
|
|
||||||
#define TCP_INPUT_DEBUG LWIP_DBG_OFF
|
|
||||||
#define TCP_OUTPUT_DEBUG LWIP_DBG_OFF
|
|
||||||
#define TCP_RTO_DEBUG LWIP_DBG_OFF
|
|
||||||
#define TCP_CWND_DEBUG LWIP_DBG_OFF
|
|
||||||
#define TCP_WND_DEBUG LWIP_DBG_OFF
|
|
||||||
#define TCP_FR_DEBUG LWIP_DBG_OFF
|
|
||||||
#define TCP_QLEN_DEBUG LWIP_DBG_OFF
|
|
||||||
#define TCP_RST_DEBUG LWIP_DBG_OFF
|
|
||||||
#define UDP_DEBUG LWIP_DBG_OFF
|
|
||||||
#define TCPIP_DEBUG LWIP_DBG_OFF
|
|
||||||
#define PPP_DEBUG LWIP_DBG_OFF
|
|
||||||
#define SLIP_DEBUG LWIP_DBG_OFF
|
|
||||||
#define DHCP_DEBUG LWIP_DBG_OFF
|
|
||||||
|
|
||||||
// This section enables HTTPD server with SSI, SGI
|
|
||||||
// and tells server which converted HTML files to use
|
|
||||||
#define LWIP_HTTPD 1
|
|
||||||
#define LWIP_HTTPD_SSI 1
|
|
||||||
#define LWIP_HTTPD_CGI 1
|
|
||||||
#define LWIP_HTTPD_SSI_INCLUDE_TAG 0
|
|
||||||
#define HTTPD_FSDATA_FILE "htmldata.c"
|
|
||||||
|
|
Binary file not shown.
|
@ -1,106 +0,0 @@
|
||||||
#!/usr/bin/python3
|
|
||||||
|
|
||||||
# This script is by @rspeir on GitHub:
|
|
||||||
# https://github.com/krzmaz/pico-w-webserver-example/pull/1/files/4b3e78351dd236f213da9bebbb20df690d470476#diff-e675c4a367e382db6f9ba61833a58c62029d8c71c3156a9f238b612b69de279d
|
|
||||||
# Renamed output to avoid linking incorrect file
|
|
||||||
|
|
||||||
import os
|
|
||||||
import binascii
|
|
||||||
|
|
||||||
#Create file to write output into
|
|
||||||
output = open('htmldata.c', 'w')
|
|
||||||
|
|
||||||
#Traverse directory, generate list of files
|
|
||||||
files = list()
|
|
||||||
os.chdir('./html_files')
|
|
||||||
for(dirpath, dirnames, filenames) in os.walk('.'):
|
|
||||||
files += [os.path.join(dirpath, file) for file in filenames]
|
|
||||||
|
|
||||||
filenames = list()
|
|
||||||
varnames = list()
|
|
||||||
|
|
||||||
#Generate appropriate HTTP headers
|
|
||||||
for file in files:
|
|
||||||
|
|
||||||
if '404' in file:
|
|
||||||
header = "HTTP/1.0 404 File not found\r\n"
|
|
||||||
else:
|
|
||||||
header = "HTTP/1.0 200 OK\r\n"
|
|
||||||
|
|
||||||
header += "Server: lwIP/pre-0.6 (http://www.sics.se/~adam/lwip/)\r\n"
|
|
||||||
|
|
||||||
if '.html' in file:
|
|
||||||
header += "Content-type: text/html\r\n"
|
|
||||||
elif '.shtml' in file:
|
|
||||||
header += "Content-type: text/html\r\n"
|
|
||||||
elif '.jpg' in file:
|
|
||||||
header += "Content-type: image/jpeg\r\n"
|
|
||||||
elif '.gif' in file:
|
|
||||||
header += "Content-type: image/gif\r\n"
|
|
||||||
elif '.png' in file:
|
|
||||||
header += "Content-type: image/png\r\n"
|
|
||||||
elif '.class' in file:
|
|
||||||
header += "Content-type: application/octet-stream\r\n"
|
|
||||||
elif '.js' in file:
|
|
||||||
header += "Content-type: text/javascript\r\n"
|
|
||||||
elif '.css' in file:
|
|
||||||
header += "Content-type: text/css\r\n"
|
|
||||||
elif '.svg' in file:
|
|
||||||
header += "Content-type: image/svg+xml\r\n"
|
|
||||||
else:
|
|
||||||
header += "Content-type: text/plain\r\n"
|
|
||||||
|
|
||||||
header += "\r\n"
|
|
||||||
|
|
||||||
fvar = file[1:] #remove leading dot in filename
|
|
||||||
fvar = fvar.replace('/', '_') #replace *nix path separator with underscore
|
|
||||||
fvar = fvar.replace('\\', '_') #replace DOS path separator with underscore
|
|
||||||
fvar = fvar.replace('.', '_') #replace file extension dot with underscore
|
|
||||||
|
|
||||||
output.write("static const unsigned char data{}[] = {{\n".format(fvar))
|
|
||||||
output.write("\t/* {} */\n\t".format(file))
|
|
||||||
|
|
||||||
#first set of hex data encodes the filename
|
|
||||||
b = bytes(file[1:].replace('\\', '/'), 'utf-8') #change DOS path separator to forward slash
|
|
||||||
for byte in binascii.hexlify(b, b' ', 1).split():
|
|
||||||
output.write("0x{}, ".format(byte.decode()))
|
|
||||||
output.write("0,\n\t")
|
|
||||||
|
|
||||||
#second set of hex data is the HTTP header/mime type we generated above
|
|
||||||
b = bytes(header, 'utf-8')
|
|
||||||
count = 0
|
|
||||||
for byte in binascii.hexlify(b, b' ', 1).split():
|
|
||||||
output.write("0x{}, ".format(byte.decode()))
|
|
||||||
count = count + 1
|
|
||||||
if(count == 10):
|
|
||||||
output.write("\n\t")
|
|
||||||
count = 0
|
|
||||||
output.write("\n\t")
|
|
||||||
|
|
||||||
#finally, dump raw hex data from files
|
|
||||||
with open(file, 'rb') as f:
|
|
||||||
count = 0
|
|
||||||
while(byte := f.read(1)):
|
|
||||||
byte = binascii.hexlify(byte)
|
|
||||||
output.write("0x{}, ".format(byte.decode()))
|
|
||||||
count = count + 1
|
|
||||||
if(count == 10):
|
|
||||||
output.write("\n\t")
|
|
||||||
count = 0
|
|
||||||
output.write("};\n\n")
|
|
||||||
|
|
||||||
filenames.append(file[1:])
|
|
||||||
varnames.append(fvar)
|
|
||||||
|
|
||||||
for i in range(len(filenames)):
|
|
||||||
prevfile = "NULL"
|
|
||||||
if(i > 0):
|
|
||||||
prevfile = "file" + varnames[i-1]
|
|
||||||
|
|
||||||
output.write("const struct fsdata_file file{0}[] = {{{{ {1}, data{2}, ".format(varnames[i], prevfile, varnames[i]))
|
|
||||||
output.write("data{} + {}, ".format(varnames[i], len(filenames[i]) + 1))
|
|
||||||
output.write("sizeof(data{}) - {}, ".format(varnames[i], len(filenames[i]) + 1))
|
|
||||||
output.write("FS_FILE_FLAGS_HEADER_INCLUDED | FS_FILE_FLAGS_HEADER_PERSISTENT}};\n")
|
|
||||||
|
|
||||||
output.write("\n#define FS_ROOT file{}\n".format(varnames[-1]))
|
|
||||||
output.write("#define FS_NUMFILES {}\n".format(len(filenames)))
|
|
Loading…
Reference in New Issue