initial fork update

This commit is contained in:
2024-06-10 18:23:54 -06:00
parent ffe57fbd30
commit 30691e90ce
17 changed files with 354 additions and 53 deletions
+6 -6
View File
@@ -1,4 +1,4 @@
menu "Application Configuration"
menu "Sillyfilly-CAN Configuration"
config GPIO_RANGE_MAX
int
@@ -73,7 +73,7 @@ menu "Application Configuration"
config ENABLE_PRINT
bool "Output the received CAN FRAME to STDOUT"
default y
default true
help
Output the received CAN FRAME to STDOUT.
@@ -83,13 +83,13 @@ menu "Application Configuration"
config ESP_WIFI_SSID
string "WiFi SSID"
default "myssid"
default "wifi-name"
help
SSID (network name) to connect to.
config ESP_WIFI_PASSWORD
string "WiFi Password"
default "mypassword"
default "wifi-password"
help
WiFi password (WPA or WPA2) to connect to.
@@ -101,7 +101,7 @@ menu "Application Configuration"
config MDNS_HOSTNAME
string "mDNS Hostname"
default "esp32-server"
default "esp32-can-server"
help
The mDNS host name used by the ESP32.
@@ -138,7 +138,7 @@ menu "Application Configuration"
config WEB_SERVER
string "HTTP Server IP or mDNS"
default "http-server.local"
default "esp32-can-server"
help
The host name or IP address of the HTTP server to use.
+1 -1
View File
@@ -91,7 +91,7 @@ esp_err_t _http_event_handler(esp_http_client_event_t *evt)
ESP_LOGI(TAG, "Last esp error code: 0x%x", err);
ESP_LOGI(TAG, "Last mbedtls failure: 0x%x", mbedtls_err);
}
break;
break;
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
case HTTP_EVENT_REDIRECT:
ESP_LOGD(TAG, "HTTP_EVENT_REDIRECT");
+64 -11
View File
@@ -8,6 +8,7 @@
*/
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <string.h>
@@ -23,36 +24,79 @@
#include "esp_chip_info.h"
#include "cJSON.h"
#include "driver/twai.h"
#include "twai.h"
static const char *TAG = "SERVER";
//static SemaphoreHandle_t ctrl_task_sem;
#define SCRATCH_BUFSIZE (1024)
#define TAG "HTTP_SERVER"
int pointer = 0;
static char *base_path = "/spiffs";
extern QueueHandle_t xQueue_twai_tx;
#define SCRATCH_BUFSIZE (1024)
extern char* twai_string_buf;
extern int twai_allocation_size;
extern char* file_read_buf;
extern int allocation_size;
typedef struct rest_server_context {
char base_path[ESP_VFS_PATH_MAX + 1]; // Not used in this project
char scratch[SCRATCH_BUFSIZE];
} rest_server_context_t;
esp_err_t readFile(char *ifile) {
int c;
ESP_LOGI(TAG, "File read llocated memory confirmed!: %d bytes", allocation_size);
char *spiffspath = "/spiffs";
int buflen = strlen(spiffspath)+strlen(ifile)+1;
char buf[buflen];
snprintf(buf, sizeof(buf), "%s%s", spiffspath, ifile);
ESP_LOGI(TAG, "buflen: %d, spiffspath: %s, ifile: %s, buf: %s, base_path: %s",buflen,spiffspath,ifile,buf,base_path);
FILE* f = fopen(buf, "r");
ESP_LOGI(TAG, "Begin reading file");
int i = 0;
while ((c = fgetc(f)) != EOF) {
file_read_buf[i] = c;
i++;
};
file_read_buf[i] = '\0';
fclose(f);
ESP_LOGI(TAG, "Read File - Iterations: %d", i);
return ESP_OK;
}
/* Handler for roor get handler */
static esp_err_t root_get_handler(httpd_req_t *req)
esp_err_t root_get_handler(httpd_req_t *req)
{
ESP_LOGI(TAG, "root_get_handler req->uri=[%s]", req->uri);
readFile("/index.html");
esp_err_t sendcheck = httpd_resp_send(req, file_read_buf, HTTPD_RESP_USE_STRLEN);
//httpd_resp_sendstr_chunk(req, NULL);
/* Send empty chunk to signal HTTP response completion */
httpd_resp_sendstr_chunk(req, NULL);
if(sendcheck == ESP_OK) {
//free(file_read_buf);
memset(twai_string_buf, '\0', allocation_size);
ESP_LOGI(TAG, "malloc freed");
return ESP_OK;
} else {
return ESP_ERR_HTTPD_RESP_SEND;
}
return ESP_OK;
}
static esp_err_t twai_read_handler(httpd_req_t *req)
{
httpd_resp_sendstr(req, twai_string_buf);
// Buffers returned by cJSON_Print must be freed by the caller.
// Please use the proper API (cJSON_free) rather than directly
//twai_string_buf = {0};
memset(twai_string_buf, '\0', twai_allocation_size);
return ESP_OK;
}
/* Handler for getting system information handler */
// curl 'http://esp32-server.local:8000/api/system/info' | python -m json.tool
// curl 'http://esp32-can-server:8000/api/system/info' | python -m json.tool
static esp_err_t system_info_get_handler(httpd_req_t *req)
{
ESP_LOGI(TAG, "system_info_get_handler req->uri=[%s]", req->uri);
@@ -225,7 +269,7 @@ static esp_err_t twai_send_handler(httpd_req_t *req)
if (xQueueSend(xQueue_twai_tx, &tx_msg, portMAX_DELAY) != pdPASS) {
ESP_LOGE(TAG, "xQueueSend Fail");
}
httpd_resp_sendstr(req, "twai send successfully");
httpd_resp_sendstr(req, "CAN tx sent successfully");
} else {
ESP_LOGE(TAG, "Request parameter not correct");
httpd_resp_sendstr(req, "Request parameter not correct");
@@ -284,6 +328,15 @@ esp_err_t start_server(const char *base_path, int port)
};
httpd_register_uri_handler(server, &twai_send_post_uri);
/* URI handler for send twai */
httpd_uri_t twai_read = {
.uri = "/api/twai/read",
.method = HTTP_GET,
.handler = twai_read_handler,
//.user_ctx = rest_context
};
httpd_register_uri_handler(server, &twai_read);
return ESP_OK;
}
+8
View File
@@ -0,0 +1,8 @@
#define MAX_FILE_SIZE_KB 10
#define MAX_TWAI_SIZE_KB 5
#define MALLOCHI_TAG "INIT_MALLOCHI"
char* file_read_buf;
char* twai_string_buf;
int allocation_size;
int twai_allocation_size;
+39 -5
View File
@@ -27,9 +27,8 @@
#include "mdns.h"
#include "lwip/dns.h"
#include "driver/twai.h" // Update from V4.2
#include "twai.h"
#include "init_malloci.h"
#define TAG "MAIN"
static const twai_filter_config_t f_config = TWAI_FILTER_CONFIG_ACCEPT_ALL();
@@ -70,6 +69,7 @@ static EventGroupHandle_t s_wifi_event_group;
#define WIFI_FAIL_BIT BIT1
static int s_retry_num = 0;
static char *base_path = "/spiffs";
QueueHandle_t xQueue_http_client;
QueueHandle_t xQueue_twai_tx;
@@ -77,6 +77,9 @@ QueueHandle_t xQueue_twai_tx;
TOPIC_t *publish;
int16_t npublish;
//extern char* twai_string_buf;
//extern esp_err_t init_twai_read_malloc();
static void event_handler(void* arg, esp_event_base_t event_base,
int32_t event_id, void* event_data)
{
@@ -204,10 +207,9 @@ esp_err_t mountSPIFFS(char * partition_label, char * base_path) {
esp_vfs_spiffs_conf_t conf = {
.base_path = base_path,
.partition_label = partition_label,
.max_files = 5,
.max_files = 4,
.format_if_mount_failed = true
};
// Use settings defined above to initialize and mount SPIFFS filesystem.
// Note: esp_vfs_spiffs_register is an all-in-one convenience function.
esp_err_t ret = esp_vfs_spiffs_register(&conf);
@@ -242,6 +244,7 @@ esp_err_t mountSPIFFS(char * partition_label, char * base_path) {
return ret;
}
esp_err_t build_table(TOPIC_t **topics, char *file, int16_t *ntopic)
{
ESP_LOGI(TAG, "build_table file=%s", file);
@@ -354,6 +357,35 @@ void dump_table(TOPIC_t *topics, int16_t ntopic)
}
esp_err_t init_file_malloc() {
allocation_size = (MAX_FILE_SIZE_KB*1024*sizeof(char));
file_read_buf = (char*)malloc(allocation_size);
if (file_read_buf == NULL) {
ESP_LOGE(MALLOCHI_TAG, "File read memory not allocated.");
return ESP_FAIL;
} else {
ESP_LOGI(MALLOCHI_TAG, "File read malloc succeeded! %d bytes allocated", allocation_size);
return ESP_OK;
}
return ESP_OK;
}
esp_err_t init_twai_read_malloc() {
twai_allocation_size = (sizeof(char)*1024*MAX_TWAI_SIZE_KB);
twai_string_buf = (char*)malloc(twai_allocation_size);
if (twai_string_buf == NULL) {
ESP_LOGE(MALLOCHI_TAG, "TWAI memory not allocated.");
return ESP_FAIL;
} else {
ESP_LOGI(MALLOCHI_TAG, "TWAI malloc succeeded! %d bytes allocated", twai_allocation_size);
return ESP_OK;
}
return ESP_OK;
}
void http_client_task(void *pvParameters);
void http_server_task(void *pvParameters);
void twai_task(void *pvParameters);
@@ -371,6 +403,8 @@ void app_main()
ESP_LOGI(TAG, "ESP_WIFI_MODE_STA");
wifi_init_sta();
initialise_mdns();
init_twai_read_malloc();
init_file_malloc();
// Install and start TWAI driver
ESP_LOGI(TAG, "%s",BITRATE);
@@ -385,7 +419,7 @@ void app_main()
// Mount SPIFFS
char *partition_label = "storage";
char *base_path = "/spiffs";
//char *base_path = "/spiffs";
ret = mountSPIFFS(partition_label, base_path);
if (ret != ESP_OK) {
ESP_LOGE(TAG, "mountSPIFFS fail");
+53 -11
View File
@@ -18,8 +18,9 @@
#include "esp_err.h"
#include "esp_log.h"
#include "driver/twai.h" // Update from V4.2
#include "twai.h"
#include "cJSON.h"
//#include "init_malloci.h"
static const char *TAG = "TWAI";
@@ -28,6 +29,10 @@ extern QueueHandle_t xQueue_twai_tx;
extern TOPIC_t *publish;
extern int16_t npublish;
int str_iterator = 0;
extern char* twai_string_buf;
//extern esp_err_t init_twai_read_malloc();
void dump_table(TOPIC_t *topics, int16_t ntopic);
@@ -35,10 +40,14 @@ void twai_task(void *pvParameters)
{
ESP_LOGI(TAG,"task start");
dump_table(publish, npublish);
twai_message_t rx_msg;
twai_message_t tx_msg;
FRAME_t frameBuf;
char* ftype;
//char* fdata = (char*)malloc(32*sizeof(char));
//int iterator = 0;
char flags[4];
char identifier[4];
while (1) {
esp_err_t ret = twai_receive(&rx_msg, pdMS_TO_TICKS(10));
if (ret == ESP_OK) {
@@ -51,22 +60,55 @@ void twai_task(void *pvParameters)
#if CONFIG_ENABLE_PRINT
if (ext == STANDARD_FRAME) {
printf("Standard ID: 0x%03"PRIx32" ", rx_msg.identifier);
printf("Standard ID: 0x%03"PRIx32, rx_msg.identifier);
} else {
printf("Extended ID: 0x%08"PRIx32, rx_msg.identifier);
}
printf(" DLC: %d Data: ", rx_msg.data_length_code);
if (rtr == 0) {
for (int i = 0; i < rx_msg.data_length_code; i++) {
printf("0x%02x ", rx_msg.data[i]);
}
} else {
printf("REMOTE REQUEST FRAME");
}
printf("\n");
#endif
// JSON fun fun fun output for da api
if (ext == STANDARD_FRAME) {
ftype = "STANDARD_FRAME";
} else {
ftype = "EXTENDED_FRAME";
}
cJSON *twai_root = cJSON_CreateObject();
int charslen_each = 6;
int data_len_chars = rx_msg.data_length_code*charslen_each;
char tmp_buf[charslen_each+2];
char fdata[data_len_chars];
if (rx_msg.data_length_code != 0) {
for (int i = 0; i < rx_msg.data_length_code; i++) {
snprintf(tmp_buf, charslen_each, "0x%02x ",rx_msg.data[i]);
strcat(fdata, tmp_buf);
}
fdata[strlen(fdata)-1] = '\0';
cJSON_AddStringToObject(twai_root, "data", fdata);
}
snprintf(flags, sizeof(rx_msg.flags), "0x%"PRIx32, rx_msg.flags);
snprintf(identifier, sizeof(rx_msg.identifier), "0x%"PRIx32, rx_msg.identifier);
cJSON_AddStringToObject(twai_root, "type", ftype);
cJSON_AddStringToObject(twai_root, "id", identifier);
cJSON_AddStringToObject(twai_root, "flags", flags);
cJSON_AddNumberToObject(twai_root, "ext", ext);
cJSON_AddNumberToObject(twai_root, "rtr", rtr);
cJSON_AddNumberToObject(twai_root, "dlc",rx_msg.data_length_code);
char *string = cJSON_Print(twai_root);
char br[] = "<br>\n";
strcat(twai_string_buf,string);
strcat(twai_string_buf,br);
cJSON_free(string);
cJSON_Delete(twai_root);
// end json buffer funssss
for(int index=0;index<npublish;index++) {
if (publish[index].frame != ext) continue;