Skip to content

Commit

Permalink
working
Browse files Browse the repository at this point in the history
  • Loading branch information
meatpiHQ committed Aug 26, 2023
1 parent bf21213 commit 873a533
Show file tree
Hide file tree
Showing 17 changed files with 1,813 additions and 714 deletions.
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"idf.adapterTargetName": "esp32c3",
"idf.portWin": "COM14",
"idf.flashType": "UART"
}
8 changes: 4 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
cmake_minimum_required(VERSION 3.5)


set(PROJECT_PREF "wican-fw")
set(PROJECT_VER "v180_hv300")
# set(PROJECT_PREF "wican-fw")
# set(PROJECT_VER "v210_hv300")
#set(PROJECT_VER "v155_hv210")

#set(PROJECT_PREF "wican-fw-usb")
#set(PROJECT_VER "v180_hv150")
set(PROJECT_PREF "wican-fw-usb")
set(PROJECT_VER "v210_hv150")

set(PROJECT_VERU ${PROJECT_VER})
set(PROJECT_BIN "${PROJECT_PREF}_${PROJECT_VERU}")
Expand Down
19 changes: 11 additions & 8 deletions main/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# Edit following two lines to set component requirements (see docs)
set(COMPONENT_REQUIRES )
set(COMPONENT_PRIV_REQUIRES )

set(COMPONENT_SRCS "main.c" "comm_server.c" "config_server.c" "realdash.c" "slcan.c" "can.c" "ble.c" "wifi_network.c" "gvret.c" "sleep_mode.c" "wc_uart.c" "elm327.c" "mqtt.c")
set(COMPONENT_ADD_INCLUDEDIRS "")

register_component()
# See the build system documentation in IDF programming guide
# for more information about component CMakeLists.txt files.
set(srcs "main.c" "comm_server.c" "config_server.c" "realdash.c" "slcan.c" "can.c" "ble.c" "wifi_network.c" "gvret.c" "sleep_mode.c" "wc_uart.c" "elm327.c" "mqtt.c" )
set(requires esp_timer esp_wifi nvs_flash fatfs vfs driver esp-tls esp_adc esp_eth log app_update esp_http_server bt spiffs freertos mqtt json)
idf_component_register(
SRCS "${srcs}" # list the source files of this component
INCLUDE_DIRS # optional, add here public include directories
PRIV_INCLUDE_DIRS # optional, add here private include directories
REQUIRES "${requires}" # optional, list the public requirements (component names)
PRIV_REQUIRES # optional, list the private requirements
)
10 changes: 5 additions & 5 deletions main/ble.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include "freertos/task.h"
#include "freertos/queue.h"
#include "freertos/event_groups.h"

#include "esp_timer.h"
#include "esp_bt.h"

#include "esp_gap_ble_api.h"
Expand Down Expand Up @@ -105,7 +105,7 @@ static esp_ble_adv_params_t heart_rate_adv_params = {
.adv_int_min = 0x100,
.adv_int_max = 0x100,
.adv_type = ADV_TYPE_IND,
.own_addr_type = BLE_ADDR_TYPE_RANDOM,
.own_addr_type = BLE_ADDR_TYPE_RPA_PUBLIC,
.channel_map = ADV_CHNL_ALL,
.adv_filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY,
};
Expand Down Expand Up @@ -411,7 +411,7 @@ static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param
/* The app will receive this evt when the IO has DisplayYesNO capability and the peer device IO also has DisplayYesNo capability.
show the passkey number to the user to confirm it with the number displayed by peer device. */
esp_ble_confirm_reply(param->ble_security.ble_req.bd_addr, true);
ESP_LOGI(GATTS_TABLE_TAG, "ESP_GAP_BLE_NC_REQ_EVT, the passkey Notify number:%d", param->ble_security.key_notif.passkey);
ESP_LOGI(GATTS_TABLE_TAG, "ESP_GAP_BLE_NC_REQ_EVT, the passkey Notify number:%" PRIu32, param->ble_security.key_notif.passkey);
break;
case ESP_GAP_BLE_SEC_REQ_EVT:
/* send the positive(true) security response to the peer device to accept the security request.
Expand All @@ -420,7 +420,7 @@ static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param
break;
case ESP_GAP_BLE_PASSKEY_NOTIF_EVT: ///the app will receive this evt when the IO has Output capability and the peer device IO has Input capability.
///show the passkey number to the user to input it in the peer device.
ESP_LOGI(GATTS_TABLE_TAG, "The passkey Notify number:%06d", param->ble_security.key_notif.passkey);
ESP_LOGI(GATTS_TABLE_TAG, "The passkey Notify number:%" PRIu32, param->ble_security.key_notif.passkey);
break;
case ESP_GAP_BLE_KEY_EVT:
//shows the ble key info share with peer device to the user.
Expand Down Expand Up @@ -812,7 +812,7 @@ void ble_init(QueueHandle_t *xTXp_Queue, QueueHandle_t *xRXp_Queue, uint8_t conn
strcpy((char*)dev_name, (char*)uid);
conn_led = connected_led;
ble_pass_key = passkey;
ESP_LOGW(GATTS_TABLE_TAG, "ble passkey: %d", ble_pass_key);
ESP_LOGW(GATTS_TABLE_TAG, "ble passkey: %lu", ble_pass_key);
}

if(xBle_TX_Queue == NULL)
Expand Down
2 changes: 1 addition & 1 deletion main/comm_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ static void tcp_server_task(void *pvParameters)
ESP_LOGE(TAG, "IPPROTO: %d", addr_family);
goto CLEAN_UP;
}
ESP_LOGI(TAG, "Socket bound, port %d", server_port);
ESP_LOGI(TAG, "Socket bound, port %lu", server_port);

if(!udp_enable)
{
Expand Down
12 changes: 6 additions & 6 deletions main/config_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -657,16 +657,16 @@ static esp_err_t upload_post_handler(httpd_req_t *req)
const esp_partition_t *running = esp_ota_get_running_partition();

if (configured != running) {
ESP_LOGW(TAG, "Configured OTA boot partition at offset 0x%08x, but running from offset 0x%08x",
ESP_LOGW(TAG, "Configured OTA boot partition at offset 0x%08lx, but running from offset 0x%08lx",
configured->address, running->address);
ESP_LOGW(TAG, "(This can happen if either the OTA boot data or preferred boot image become corrupted somehow.)");
}
ESP_LOGI(TAG, "Running partition type %d subtype %d (offset 0x%08x)",
ESP_LOGI(TAG, "Running partition type %d subtype %d (offset 0x%08lx)",
running->type, running->subtype, running->address);

update_partition = esp_ota_get_next_update_partition(NULL);
assert(update_partition != NULL);
ESP_LOGI(TAG, "Writing to partition subtype %d at offset 0x%x",
ESP_LOGI(TAG, "Writing to partition subtype %d at offset 0x%lx",
update_partition->subtype, update_partition->address);
err = esp_ota_begin(update_partition, OTA_SIZE_UNKNOWN, &update_handle);
if (err != ESP_OK)
Expand Down Expand Up @@ -720,7 +720,7 @@ static esp_err_t upload_post_handler(httpd_req_t *req)
buf = ret + 3;
remaining -= (buf - boundary_start);
ESP_LOGI(TAG, "Real Remaining size : %d", remaining);
ESP_LOGI(TAG, "Downloading file...", remaining);

received -= (buf - boundary_start);
}
total_size += received;
Expand All @@ -745,7 +745,7 @@ static esp_err_t upload_post_handler(httpd_req_t *req)
}

/* Close file upon upload completion */
ESP_LOGI(TAG, "File reception complete: %u", total_size);
ESP_LOGI(TAG, "File reception complete: %lu", total_size);

if ((received = httpd_req_recv(req, buf, SCRATCH_BUFSIZE)) <= 0)
{
Expand Down Expand Up @@ -1333,7 +1333,7 @@ static httpd_handle_t config_server_init(void)
fseek(f, 0, SEEK_SET);

device_config_file = malloc(filesize+1);
ESP_LOGI(__func__, "File size: %u", filesize);
ESP_LOGI(__func__, "File size: %d", filesize);
fseek(f, 0, SEEK_SET);
fread(device_config_file, sizeof(char), filesize, f);
device_config_file[filesize] = 0;
Expand Down
15 changes: 8 additions & 7 deletions main/elm327.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include <ctype.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "freertos/event_groups.h"
#include "esp_wifi.h"
#include "esp_system.h"
#include "esp_timer.h"
#include "esp_event.h"
#include "nvs_flash.h"
#include "driver/gpio.h"
Expand Down Expand Up @@ -461,7 +462,7 @@ static char* elm327_set_timeout(const char* command_str)
elm327_config.req_timeout = 0x32;
}

ESP_LOGI(TAG, "elm327_config.req_timeout: %d", elm327_config.req_timeout);
ESP_LOGI(TAG, "elm327_config.req_timeout: %lu", elm327_config.req_timeout);

return (char*)ok_str;
}
Expand Down Expand Up @@ -522,7 +523,7 @@ static char* elm327_input_voltage(const char* command_str)
}
else
{
sprintf(volt, "%.1f",0);
sprintf(volt, "%.1f",0.0f);
}
return (char*)volt;
}
Expand Down Expand Up @@ -852,11 +853,11 @@ static int8_t elm327_request(char *cmd, char *rsp, QueueHandle_t *queue)
{
if(rx_frame.extd == 0)
{
sprintf((char*)rsp, "%03X%02X", rx_frame.identifier&0xFFF,rx_frame.data[0]);
sprintf((char*)rsp, "%03lX%02X", rx_frame.identifier&0xFFF,rx_frame.data[0]);
}
else
{
sprintf((char*)rsp, "%08X%02X", rx_frame.identifier&TWAI_EXTD_ID_MASK,rx_frame.data[0]);
sprintf((char*)rsp, "%08lX%02X", rx_frame.identifier&TWAI_EXTD_ID_MASK,rx_frame.data[0]);
}

}
Expand Down Expand Up @@ -891,7 +892,7 @@ static int8_t elm327_request(char *cmd, char *rsp, QueueHandle_t *queue)
else
{
xwait_time -= (((esp_timer_get_time() - txtime)/1000)/portTICK_PERIOD_MS);
ESP_LOGI(TAG, "xwait_time: %d", xwait_time);
ESP_LOGI(TAG, "xwait_time: %lu" , xwait_time);
if(xwait_time > (elm327_config.req_timeout*4.096))
{
xwait_time = 0;
Expand All @@ -905,7 +906,7 @@ static int8_t elm327_request(char *cmd, char *rsp, QueueHandle_t *queue)
}
}

ESP_LOGW(TAG, "Response time: %u", (uint32_t)((esp_timer_get_time() - txtime)/1000));
ESP_LOGW(TAG, "Response time: %" PRIu32, (uint32_t)((esp_timer_get_time() - txtime)/1000));

if(rsp_found == 0)
{
Expand Down
6 changes: 3 additions & 3 deletions main/gvret.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ void gvert_tmr_set_start(void)
xSemaphoreGive(xgvert_tmr_semaphore);
}

uint32_t gvert_tmr_get()
int64_t gvert_tmr_get()
{
int64_t ret = 0;
xSemaphoreTake(xgvert_tmr_semaphore, portMAX_DELAY);
ret = esp_timer_get_time() - gvert_tmr_start_time;
xSemaphoreGive(xgvert_tmr_semaphore);

return (uint32_t)ret;
return (int64_t)ret;
}

static void periodic_timer_callback(void* arg)
Expand All @@ -93,7 +93,7 @@ uint8_t checksumCalc(uint8_t *buffer, int length)
void gvert_setup(EEPROMSettings *settings)
{
can_disable();
ESP_LOGI(__func__, "settings->CAN0Speed: %u", settings->CAN0Speed);
ESP_LOGI(__func__, "settings->CAN0Speed: %lu", settings->CAN0Speed);
for(uint8_t i = 0; i < sizeof(can_speed)/sizeof(uint32_t); i++)
{
if(can_speed[i] == settings->CAN0Speed)
Expand Down
6 changes: 4 additions & 2 deletions main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include <inttypes.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
Expand All @@ -29,6 +29,7 @@
#include "driver/gpio.h"
#include "esp_log.h"
#include <string.h>
#include "esp_timer.h"
#include "comm_server.h"
#include "lwip/sockets.h"
#include "driver/twai.h"
Expand All @@ -49,6 +50,7 @@
#include "wc_uart.h"
#include "elm327.h"
#include "mqtt.h"
#include "esp_mac.h"

#define TAG __func__
#define TX_GPIO_NUM 0
Expand Down Expand Up @@ -220,7 +222,7 @@ static void can_rx_task(void *pvParameters)
{
uint32_t free_heap = heap_caps_get_free_size(HEAP_CAPS);
time_old = esp_timer_get_time();
ESP_LOGI(TAG, "free_heap: %u", free_heap);
ESP_LOGI(TAG, "free_heap: %lu", free_heap);
// ESP_LOGI(TAG, "msg %u/sec", num_msg);
// num_msg = 0;
}
Expand Down
46 changes: 28 additions & 18 deletions main/mqtt.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@
#include "esp_ota_ops.h"
#include "nvs.h"
#include "nvs_flash.h"
#include "driver/adc.h"
#include "esp_adc_cal.h"
#include "sleep_mode.h"
#include "ble.h"
#include "esp_sleep.h"
Expand Down Expand Up @@ -74,7 +72,7 @@ static QueueHandle_t *xmqtt_tx_queue;

static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data)
{
ESP_LOGD(TAG, "Event dispatched from event loop base=%s, event_id=%d", base, event_id);
ESP_LOGD(TAG, "Event dispatched from event loop base=%s, event_id=%ld", base, event_id);
esp_mqtt_event_handle_t event = event_data;
// esp_mqtt_client_handle_t client = event->client;

Expand Down Expand Up @@ -207,7 +205,7 @@ static void mqtt_parse_data(void *handler_args, esp_event_base_t base, int32_t e
can_frame.extd = cJSON_IsTrue(frame_extd)?1:0;
can_frame.rtr = cJSON_IsTrue(frame_rtr)?1:0;;

ESP_LOGI(TAG, " frame_id: %u, frame_dlc: %u ", (uint32_t)frame_id->valuedouble, (uint32_t)frame_dlc->valuedouble);
ESP_LOGI(TAG, " frame_id: %lu, frame_dlc: %lu ", (uint32_t)frame_id->valuedouble, (uint32_t)frame_dlc->valuedouble);

for (int i = 0 ; i < cJSON_GetArraySize(frame_data) ; i++)
{
Expand Down Expand Up @@ -268,15 +266,15 @@ static void mqtt_task(void *pvParameters)
// sprintf(mqtt_data, "{\"bus\":\"0\",\"type\":\"rx\",\"frame\":{\"id\":%u,\"dlc\":%u,\"rtr\":%s,\"extd\":%s,\"data\":[%u,%u,%u,%u,%u,%u,%u,%u]}}",
// message->identifier, message->data_length_code, message->rtr?"true":"false", message->extd?"true":"false",message->data[0], message->data[1], message->data[2], message->data[3],
// message->data[4], message->data[5], message->data[6], message->data[7]);
sprintf(json_buffer, "{\"bus\":\"0\",\"type\":\"rx\",\"ts\":%u,\"frame\":[", (pdTICKS_TO_MS(xTaskGetTickCount())%60000));
sprintf(json_buffer, "{\"bus\":\"0\",\"type\":\"rx\",\"ts\":%lu,\"frame\":[", (pdTICKS_TO_MS(xTaskGetTickCount())%60000));
//pdTICKS_TO_MS(xTaskGetTickCount())%60000

if(strlen(json_buffer) < (sizeof(json_buffer) -128))
{
while(xQueuePeek(*xmqtt_tx_queue, ( void * ) &tx_buffer, 0) == pdTRUE)
{
xQueueReceive(*xmqtt_tx_queue, ( void * ) &tx_buffer, 0);
sprintf(tmp, "{\"id\":%u,\"dlc\":%u,\"rtr\":%s,\"extd\":%s,\"data\":[%u,%u,%u,%u,%u,%u,%u,%u]},",tx_frame.identifier, tx_frame.data_length_code, tx_frame.rtr?"true":"false",
sprintf(tmp, "{\"id\":%lu,\"dlc\":%u,\"rtr\":%s,\"extd\":%s,\"data\":[%u,%u,%u,%u,%u,%u,%u,%u]},",tx_frame.identifier, tx_frame.data_length_code, tx_frame.rtr?"true":"false",
tx_frame.extd?"true":"false",tx_frame.data[0], tx_frame.data[1], tx_frame.data[2], tx_frame.data[3],
tx_frame.data[4], tx_frame.data[5], tx_frame.data[6], tx_frame.data[7]);
strcat((char*)json_buffer, (char*)tmp);
Expand All @@ -302,25 +300,37 @@ static void mqtt_task(void *pvParameters)
void mqtt_init(char* id, uint8_t connected_led, QueueHandle_t *xtx_queue)
{
esp_mqtt_client_config_t mqtt_cfg = {
.uri = config_server_get_mqtt_url(),
.port = config_server_get_mqtt_port(),
.username = config_server_get_mqtt_user(),
.password = config_server_get_mmqtt_pass(),
.disable_auto_reconnect = false,
.reconnect_timeout_ms = 5000,
.out_buffer_size = 1024*5,
.buffer_size = 1024*5,
.lwt_topic = mqtt_status_topic,
.lwt_msg = "{\"status\": \"offline\"}",
.keepalive = 30
// .session.protocol_ver = MQTT_PROTOCOL_V_5,
.broker.address.uri = config_server_get_mqtt_url(),
.broker.address.port = config_server_get_mqtt_port(),
.credentials.username = config_server_get_mqtt_user(),
.credentials.authentication.password = config_server_get_mmqtt_pass(),
.network.disable_auto_reconnect = false,
.session.keepalive = 30,
.session.last_will.topic = mqtt_status_topic,
.session.last_will.msg = "{\"status\": \"offline\"}",
.network.reconnect_timeout_ms = 5000,
.buffer.size = 1024*5,
.buffer.out_size = 1024*5,
// .uri = config_server_get_mqtt_url(),
// .port = config_server_get_mqtt_port(),
// .username = config_server_get_mqtt_user(),
// .password = config_server_get_mmqtt_pass(),
// .disable_auto_reconnect = false,
// .reconnect_timeout_ms = 5000,
// .out_buffer_size = 1024*5,
// .buffer_size = 1024*5,
// .lwt_topic = mqtt_status_topic,
// .lwt_msg = "{\"status\": \"offline\"}",
// .keepalive = 30
};
xmqtt_tx_queue = xtx_queue;
mqtt_led = connected_led;
device_id = id;
sprintf(mqtt_sub_topic, "wican/%s/can/tx", device_id);
sprintf(mqtt_status_topic, "wican/%s/status", device_id);

ESP_LOGI(TAG, "device_id: %s, mqtt_cfg.uri: %s", device_id, mqtt_cfg.uri);
ESP_LOGI(TAG, "device_id: %s, mqtt_cfg.uri: %s", device_id, mqtt_cfg.broker.address.uri);


s_mqtt_event_group = xEventGroupCreate();
Expand Down
4 changes: 2 additions & 2 deletions main/realdash.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include <inttypes.h>
#include <string.h>
#include "esp_log.h"
//#include "esp_log.h"
#include "driver/twai.h"

static uint32_t Crc32(const uint8_t* data, size_t numBytes)
Expand Down
Loading

0 comments on commit 873a533

Please sign in to comment.