From 611ac97f3fcb41036585ac682b468aecfcb279d0 Mon Sep 17 00:00:00 2001 From: Morgan Allen Date: Sat, 28 Jul 2018 13:49:13 -0700 Subject: [PATCH] add test firmware --- test/lora32/Makefile | 8 + test/lora32/components/esp32-lora | 1 + .../components/esp32-uart-cmdr/Makefile | 18 ++ .../components/esp32-uart-cmdr/component.mk | 5 + .../esp32-uart-cmdr/include/esp32-uart-cmdr.h | 19 ++ .../components/esp32-uart-cmdr/main/cmdr.c | 44 +++++ .../esp32-uart-cmdr/main/esp32_uart_cmdr.c | 52 ++++++ .../components/esp32-uart-cmdr/string-util.c | 28 +++ .../components/esp32-uart-cmdr/string-util.h | 7 + .../components/esp32-uart-cmdr/test/test.c | 140 +++++++++++++++ test/lora32/main/component.mk | 4 + test/lora32/main/main.c | 166 ++++++++++++++++++ test/lora32/sdkconfig.defaults | 2 + 13 files changed, 494 insertions(+) create mode 100644 test/lora32/Makefile create mode 120000 test/lora32/components/esp32-lora create mode 100644 test/lora32/components/esp32-uart-cmdr/Makefile create mode 100644 test/lora32/components/esp32-uart-cmdr/component.mk create mode 100644 test/lora32/components/esp32-uart-cmdr/include/esp32-uart-cmdr.h create mode 100644 test/lora32/components/esp32-uart-cmdr/main/cmdr.c create mode 100644 test/lora32/components/esp32-uart-cmdr/main/esp32_uart_cmdr.c create mode 100644 test/lora32/components/esp32-uart-cmdr/string-util.c create mode 100644 test/lora32/components/esp32-uart-cmdr/string-util.h create mode 100644 test/lora32/components/esp32-uart-cmdr/test/test.c create mode 100644 test/lora32/main/component.mk create mode 100644 test/lora32/main/main.c create mode 100644 test/lora32/sdkconfig.defaults diff --git a/test/lora32/Makefile b/test/lora32/Makefile new file mode 100644 index 0000000..1528d1f --- /dev/null +++ b/test/lora32/Makefile @@ -0,0 +1,8 @@ +# +# This is a project Makefile. It is assumed the directory this Makefile resides in is a +# project subdirectory. +# + +PROJECT_NAME := lora32 + +include $(IDF_PATH)/make/project.mk diff --git a/test/lora32/components/esp32-lora b/test/lora32/components/esp32-lora new file mode 120000 index 0000000..1b20c9f --- /dev/null +++ b/test/lora32/components/esp32-lora @@ -0,0 +1 @@ +../../../ \ No newline at end of file diff --git a/test/lora32/components/esp32-uart-cmdr/Makefile b/test/lora32/components/esp32-uart-cmdr/Makefile new file mode 100644 index 0000000..9eed097 --- /dev/null +++ b/test/lora32/components/esp32-uart-cmdr/Makefile @@ -0,0 +1,18 @@ +CC=gcc +src := $(wildcard *.c) test/test.c main/cmdr.c +obj = $(src:.c=.o) + +CFLAGS=-I. -Iinclude -std=gnu99 -Og -ggdb -Wall -Wno-error=unused-function -Wno-error=unused-but-set-variable -Wno-error=unused-variable -Wno-error=deprecated-declarations -Wextra -Wno-unused-parameter -Wno-sign-compare -Wno-old-style-declaration +LDFLAGS = -Wno-error=unused-but-set-variable -lcheck -lsubunit -pthread -pthread -lrt -lm -lsubunit -ggdb -Og + +all: tst + +test.o: + $(CC) -o $@ *.o $(CFLAGS) $(LDFLAGS) + +tst: $(obj) + $(CC) -o $@ $^ $(CFLAGS) $(LDFLAGS) + +.PHONY: clean +clean: + rm -f $(obj) tst diff --git a/test/lora32/components/esp32-uart-cmdr/component.mk b/test/lora32/components/esp32-uart-cmdr/component.mk new file mode 100644 index 0000000..9475402 --- /dev/null +++ b/test/lora32/components/esp32-uart-cmdr/component.mk @@ -0,0 +1,5 @@ +COMPONENT_SRCDIRS := main +COMPONENT_ADD_INCLUDEDIRS := main include +COMPONENT_ADD_LDFLAGS := -lesp32-uart-cmdr + +COMPONENT_EXTRA_CLEAN := diff --git a/test/lora32/components/esp32-uart-cmdr/include/esp32-uart-cmdr.h b/test/lora32/components/esp32-uart-cmdr/include/esp32-uart-cmdr.h new file mode 100644 index 0000000..745ebc6 --- /dev/null +++ b/test/lora32/components/esp32-uart-cmdr/include/esp32-uart-cmdr.h @@ -0,0 +1,19 @@ +#ifndef __UART_CMDR_H__ +#define __UART_CMDR_H__ +#include +#include +#include +#include + +static volatile uint8_t cmd_count; + +typedef struct Cmd { + char *cmd; + void (*callback)(void *p); +}; + +void ucmdr_init(); +uint8_t ucmdr_install(char *cmd, void(*callback)(void *p)); +uint8_t ucmdr_match(char *str, struct Cmd **handler); + +#endif diff --git a/test/lora32/components/esp32-uart-cmdr/main/cmdr.c b/test/lora32/components/esp32-uart-cmdr/main/cmdr.c new file mode 100644 index 0000000..36839c3 --- /dev/null +++ b/test/lora32/components/esp32-uart-cmdr/main/cmdr.c @@ -0,0 +1,44 @@ +#include +#include + +#include "esp32-uart-cmdr.h" + +#define MAX_COMMANDS (10) + +static struct Cmd cmd_list[MAX_COMMANDS]; +static volatile uint8_t cmd_count = 0; + +uint8_t ucmdr_install(char *cmd, void(*callback)(void *p)) { + //printf("installing: %s\n", cmd); + cmd_list[cmd_count].cmd = cmd; + cmd_list[cmd_count].callback = callback; + + //printf("cmd_count: %d\n", cmd_count); + ++cmd_count; + //printf("cmd_count: %d\n", cmd_count); + + return cmd_count; +}; + +uint8_t ucmdr_match(char *str, struct Cmd **handler) { + uint8_t i = 0; + + //printf("Checking against %d handlers\n", cmd_count); + + for(; i < cmd_count; i++) { + //printf("checking %s\n", (const char*)cmd_list[i].cmd); + + if(strstr(str, (const char*)cmd_list[i].cmd) != NULL) { + *handler = &cmd_list[i]; + + return 1; + } + } + + return 0; +}; + +void ucmdr_init() { + bzero(&cmd_list, sizeof(cmd_list)); + cmd_count = 0; +}; diff --git a/test/lora32/components/esp32-uart-cmdr/main/esp32_uart_cmdr.c b/test/lora32/components/esp32-uart-cmdr/main/esp32_uart_cmdr.c new file mode 100644 index 0000000..e6df398 --- /dev/null +++ b/test/lora32/components/esp32-uart-cmdr/main/esp32_uart_cmdr.c @@ -0,0 +1,52 @@ +#include +#include +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "freertos/queue.h" +#include "driver/uart.h" +#include "esp_log.h" + +static const char *TAG = "uart_cmdr"; +static QueueHandle_t uart0_queue; + +#define BUF_SIZE (1024) +#define RD_BUF_SIZE (BUF_SIZE) + +static void uart_event_task(void *pvParameters) { + uart_event_t event; + uint8_t *dtmp = (uint8_t*) malloc(RD_BUF_SIZE); + uint8_t size = 0; + + bzero(dtmp, RD_BUF_SIZE); + + for(;;) { + //Waiting for UART event. + if(xQueueReceive(uart0_queue, (void * )&event, (portTickType)portMAX_DELAY)) { + switch(event.type) { + case UART_DATA: + uart_read_bytes(UART_NUM_0, dtmp + size, event.size, portMAX_DELAY); + //uart_write_bytes(UART_NUM_0, (const char*) dtmp + size, event.size); + + size += event.size; + + if(dtmp[size - 1] == '\n') { + //uart_write_bytes(UART_NUM_0, (const char*) dtmp, size); + + bzero(dtmp, RD_BUF_SIZE); + } + + break; + + case UART_BREAK: + case UART_BUFFER_FULL: + case UART_FIFO_OVF: + case UART_FRAME_ERR: + case UART_PARITY_ERR: + case UART_DATA_BREAK: + case UART_PATTERN_DET: + case UART_EVENT_MAX: + break; + } + } + } +} diff --git a/test/lora32/components/esp32-uart-cmdr/string-util.c b/test/lora32/components/esp32-uart-cmdr/string-util.c new file mode 100644 index 0000000..67ac8a8 --- /dev/null +++ b/test/lora32/components/esp32-uart-cmdr/string-util.c @@ -0,0 +1,28 @@ +void trim_leading(char *str) { + char *i = str; + char *j = str; + + while(*j != 0) + if(*++j != ' ') break; + + while(*j != 0) + *i++ = *j++; + + *i = 0; +} + +void trim_multi(char *str) { + char *i = str; + char *j = str; + + while(*j != 0) { + *i = *j++; + + if( + (*j != ' ') || + (*j == ' ' && *(j - 1) != ' ') + ) i++; + } + + *i = 0; +} diff --git a/test/lora32/components/esp32-uart-cmdr/string-util.h b/test/lora32/components/esp32-uart-cmdr/string-util.h new file mode 100644 index 0000000..d49ded2 --- /dev/null +++ b/test/lora32/components/esp32-uart-cmdr/string-util.h @@ -0,0 +1,7 @@ +#ifndef __STRING_UTIL_H +#define __STRING_UTIL_H + +void trim_leading(char *str); +void trim_multi(char *str); + +#endif diff --git a/test/lora32/components/esp32-uart-cmdr/test/test.c b/test/lora32/components/esp32-uart-cmdr/test/test.c new file mode 100644 index 0000000..070116d --- /dev/null +++ b/test/lora32/components/esp32-uart-cmdr/test/test.c @@ -0,0 +1,140 @@ +#include +#include +#include +#include + +#include "string-util.h" +#include "esp32-uart-cmdr.h" + +START_TEST(leading) +{ + char str[] = " I had a leading space"; + trim_leading(str); + + ck_assert_str_eq(str, "I had a leading space"); +} +END_TEST + +START_TEST(multi) +{ + char str[] = " I had a leading space "; + trim_multi(str); + + ck_assert_str_eq(str, " I had a leading space "); +} +END_TEST + +uint8_t cb_count = 0; +static int8_t count = 0; +void inc(void *p) { + ++count; + ++cb_count; +} + +void set(void *p) { + count = (int8_t)p; +} + +void down(void *p) { + --count; + ++cb_count; +} + +START_TEST(install) { + uint8_t cnt = ucmdr_install("inc", inc); + + ck_assert_uint_eq(cnt, 1); + + cnt = ucmdr_install("down", down); + + ck_assert_uint_eq(cnt, 2); +} +END_TEST + +START_TEST(match) { + struct Cmd *handler; + + bool matched = ucmdr_match("inc", &handler); + ck_assert_uint_eq(matched, 1); + + printf("caling callback\n"); + handler->callback((void*)1); + + return; + ck_assert_uint_eq(cb_count, 1); + ck_assert_uint_eq(count, 1); + + ucmdr_match("down", &handler); + + handler->callback((void*)1); + + ck_assert_uint_eq(cb_count, 2); + ck_assert_uint_eq(count, 0); +} +END_TEST + +START_TEST(args) { + uint8_t cnt = ucmdr_install("set", set); + + ck_assert_uint_eq(cnt, 3); + + struct Cmd *handler; + + ucmdr_match("set", &handler); + + handler->callback((void*)42); + + ck_assert_uint_eq(count, 42); +} +END_TEST + +START_TEST(nomatch) { + struct Cmd *handler; + + uint8_t match = ucmdr_match("nomatch", &handler); + + ck_assert_uint_eq(match, 0); +} +END_TEST + +Suite* suite_string(void) +{ + Suite* s; + TCase* tc_str; + TCase* tc_cmdr; + s = suite_create("CMDR Test Suite"); + + tc_str = tcase_create("String utils"); + tcase_add_test(tc_str, leading); + tcase_add_test(tc_str, multi); + + tc_cmdr = tcase_create("CMDR"); + tcase_add_test(tc_cmdr, install); + tcase_add_test(tc_cmdr, nomatch); + tcase_add_test(tc_cmdr, match); + tcase_add_test(tc_cmdr, args); + + suite_add_tcase(s, tc_str); + suite_add_tcase(s, tc_cmdr); + + return s; +} + +int main(void) +{ + int number_failed; + SRunner* sr; + + ucmdr_init(); + + Suite* s_string = suite_string(); + + sr = srunner_create(s_string); + + srunner_set_fork_status(sr, CK_NOFORK); + srunner_run_all(sr, CK_VERBOSE); + + number_failed = srunner_ntests_failed(sr); + srunner_free(sr); + return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE; +} diff --git a/test/lora32/main/component.mk b/test/lora32/main/component.mk new file mode 100644 index 0000000..a98f634 --- /dev/null +++ b/test/lora32/main/component.mk @@ -0,0 +1,4 @@ +# +# "main" pseudo-component makefile. +# +# (Uses default behaviour of compiling all source files in directory, adding 'include' to include path.) diff --git a/test/lora32/main/main.c b/test/lora32/main/main.c new file mode 100644 index 0000000..5af63ed --- /dev/null +++ b/test/lora32/main/main.c @@ -0,0 +1,166 @@ +// Copyright 2015-2017 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +/**************************************************************************** + * + * This file is for gatt server. It can send adv data, be connected by clent. + * Run the gatt_client demo, the client demo will automatically connect to the gatt_server demo. + * Client demo will enable gatt_server's notify after connection. Then two devices will exchange + * data. + * + ****************************************************************************/ + + +#include +#include +#include +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "freertos/event_groups.h" +#include "driver/uart.h" +#include "esp_system.h" +#include "esp_log.h" +#include "nvs_flash.h" +#include "sdkconfig.h" +#include "esp32-lora.h" +#include "esp32-uart-cmdr.h" + +#define TAG "LORA32" +#define BUF_SIZE (1024) +#define RD_BUF_SIZE (BUF_SIZE) +#define EX_UART_NUM UART_NUM_0 + +uint8_t data_available = 0; +static lora32_cfg_t lora; +static QueueHandle_t uart0_queue; + +static void uart_event_task(void *pvParameters) { + uart_event_t event; + uint8_t *dtmp = (uint8_t*) malloc(RD_BUF_SIZE); + uint8_t size = 0; + + bzero(dtmp, RD_BUF_SIZE); + + for(;;) { + //Waiting for UART event. + if(xQueueReceive(uart0_queue, (void * )&event, (portTickType)portMAX_DELAY)) { + switch(event.type) { + case UART_DATA: + uart_read_bytes(UART_NUM_0, dtmp + size, event.size, portMAX_DELAY); + //uart_write_bytes(UART_NUM_0, (const char*) dtmp + size, event.size); + + size += event.size; + + if(dtmp[size - 1] == '\n') { + uart_write_bytes(UART_NUM_0, (const char*) dtmp, size); + + struct Cmd *handler; + uint8_t match = ucmdr_match((char *)dtmp, &handler); + + if(match) { + handler->callback(dtmp + sizeof(handler->cmd)); + } else { + printf("command not found\n"); + } + + bzero(dtmp, RD_BUF_SIZE); + size = 0; + } + + break; + + case UART_BREAK: + case UART_BUFFER_FULL: + case UART_FIFO_OVF: + case UART_FRAME_ERR: + case UART_PARITY_ERR: + case UART_DATA_BREAK: + case UART_PATTERN_DET: + case UART_EVENT_MAX: + break; + } + } + } +} + +static void handle_lora_receive(uint8_t *data, uint8_t size) { + ESP_LOGI(TAG, "msg: %s", data); +} + +void loop(void *p) { + ESP_LOGI(TAG, "starting main loop"); + + while(true) { + vTaskDelay(100 / portTICK_PERIOD_MS); + } +} + +void dump_mem() { + lora32_dump_regs(&lora); +} + +void set_mode(void *args) { + int mode = atoi(args); + ESP_LOGI(TAG, "mode: %d", mode); + + if(mode == 1) { + } +} + +void send(void *args) { + ESP_LOGI(TAG, "sending: %s\n", (char*)args); + lora32_send(&lora, (uint8_t *)args, 12); +}; + +void app_main() { + esp_err_t ret; + + // Initialize NVS. + ret = nvs_flash_init(); + if (ret == ESP_ERR_NVS_NO_FREE_PAGES) { + ESP_ERROR_CHECK(nvs_flash_erase()); + ret = nvs_flash_init(); + } + ESP_ERROR_CHECK( ret ); + + xTaskCreate(&loop, "loop", 2048, NULL, 6, NULL); + + lora = lora32_create(); + lora.frequency = frequencies[F915]; + lora.receive = &handle_lora_receive; + //lora.poll_rx = true; + + lora32_init(&lora); + //lora32_set_spreadfactor(&lora, 12); + + ucmdr_init(); + ucmdr_install("send", send); + ucmdr_install("dump", dump_mem); + + uart_config_t uart_config = { + .baud_rate = 115200, + .data_bits = UART_DATA_8_BITS, + .parity = UART_PARITY_DISABLE, + .stop_bits = UART_STOP_BITS_1, + .flow_ctrl = UART_HW_FLOWCTRL_DISABLE + }; + uart_param_config(EX_UART_NUM, &uart_config); + + esp_log_level_set(TAG, ESP_LOG_INFO); + uart_set_pin(EX_UART_NUM, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE); + uart_driver_install(EX_UART_NUM, BUF_SIZE * 2, BUF_SIZE * 2, 20, &uart0_queue, 0); + + //Create a task to handler UART event from ISR + xTaskCreate(uart_event_task, "uart_event_task", 2048, NULL, 12, NULL); +}; diff --git a/test/lora32/sdkconfig.defaults b/test/lora32/sdkconfig.defaults new file mode 100644 index 0000000..5cf195f --- /dev/null +++ b/test/lora32/sdkconfig.defaults @@ -0,0 +1,2 @@ +CONFIG_ESP32_XTAL_FREQ_26=y +CONFIG_ESP32_XTAL_FREQ=26