Skip to content

Commit

Permalink
Merge pull request #106 from imi415/linux
Browse files Browse the repository at this point in the history
POSIX: use libc heap, allow NULL thread_t, UART refactored.
  • Loading branch information
MaJerle committed Mar 29, 2022
2 parents 8dd076a + f079f6b commit d8af85f
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 20 deletions.
7 changes: 4 additions & 3 deletions examples/posix/sntp_rtos/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ set(C_SOURCES
"../../../lwesp/src/lwesp/lwesp_utils.c"
"../../../lwesp/src/lwesp/lwesp.c"
"../../../lwesp/src/system/lwesp_ll_posix.c"
"../../../lwesp/src/system/lwesp_mem_posix.c"
"../../../lwesp/src/system/lwesp_sys_posix.c"
"main.c"
)
Expand All @@ -43,6 +44,6 @@ set(C_LIBRARIES

set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pthread")

add_executable(${CMAKE_PROJECT_NAME} ${C_SOURCES})
target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE ${C_INCLUDES})
target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE ${C_LIBRARIES})
add_executable(${PROJECT_NAME} ${C_SOURCES})
target_include_directories(${PROJECT_NAME} PRIVATE ${C_INCLUDES})
target_link_libraries(${PROJECT_NAME} PRIVATE ${C_LIBRARIES})
2 changes: 2 additions & 0 deletions examples/posix/sntp_rtos/lwesp_opts.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,6 @@

#define LWESP_CFG_SNTP 1

#define LWESP_CFG_MEM_CUSTOM 1

#endif /* LWESP_HDR_OPTS_H */
38 changes: 27 additions & 11 deletions lwesp/src/system/lwesp_ll_posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@
static uint8_t initialized = 0;

static int uart_fd;
static uint8_t data_buffer[0x1000];

#define LOG_BUFFER_LEN 4096

static uint32_t data_buffer_ptr = 0;
static uint8_t data_buffer[LOG_BUFFER_LEN];

static lwesp_sys_thread_t uart_thread_handle;

Expand Down Expand Up @@ -139,22 +143,34 @@ configure_uart(uint32_t baudrate) {
}
}

static void
log_command(uint8_t new_byte) {
/* Log buffer not full */
if (data_buffer_ptr < LOG_BUFFER_LEN - 1) {
data_buffer[data_buffer_ptr] = new_byte;
data_buffer_ptr++;
}

if (data_buffer_ptr > 0 && data_buffer[data_buffer_ptr - 1] == '\n') {
data_buffer[data_buffer_ptr] = '\0';

fprintf(stderr, "[AT <]: \e[32m%s\e[0m", data_buffer);
data_buffer_ptr = 0;
}
}

static void
uart_thread(void* param) {
size_t read_bytes = 0;
uint8_t data_byte;
for (;;) {
read_bytes += read(uart_fd, &data_buffer[read_bytes], 1);
/* If a newline is received or receive buffer full, pass data to the library */
if ((read_bytes >= sizeof(data_buffer) - 1) || (read_bytes > 0 && data_buffer[read_bytes - 1] == '\n')) {
data_buffer[read_bytes] = '\0';
fprintf(stderr, "[AT <]: \e[32m%s\e[0m", data_buffer);
if (read(uart_fd, &data_byte, 1) > 0) {
log_command(data_byte);
/* Send received data to input processing module */
#if LWESP_CFG_INPUT_USE_PROCESS
lwesp_input_process(data_buffer, read_bytes);
#else /* LWESP_CFG_INPUT_USE_PROCESS */
lwesp_input(data_buffer, read_bytes);
lwesp_input_process(&data_byte, 1);
#else /* LWESP_CFG_INPUT_USE_PROCESS */
lwesp_input(data_byte, 1);
#endif /* LWESP_CFG_INPUT_USE_PROCESS */
read_bytes = 0;
}
}
}
Expand Down
70 changes: 70 additions & 0 deletions lwesp/src/system/lwesp_mem_posix.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/**
* \file lwesp_mem_lwmem.c
* \brief Dynamic memory manager implemented with LwMEM
*/

/*
* Copyright (c) 2020 Tilen MAJERLE
*
* 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.
*
* This file is part of LwESP - Lightweight ESP-AT parser library.
*
* Author: Tilen MAJERLE <tilen@majerle.eu>
* Author: imi415 <imi415.public@gmail.com>
* Version: v1.1.1-dev
*/
#include "lwesp/lwesp.h"

/* See lwesp_mem.c file for function documentation on parameters and return values */

#if LWESP_CFG_MEM_CUSTOM && !__DOXYGEN__

/*
* Before this driver can be used, user must:
*
* - Configure LwMEM for thread-safety mode by enabling operating system features
* - Set memory regions during start of application
*/

#include <stdlib.h>

void*
lwesp_mem_malloc(size_t size) {
return malloc(size);
}

void*
lwesp_mem_realloc(void* ptr, size_t size) {
return realloc(ptr, size);
}

void*
lwesp_mem_calloc(size_t num, size_t size) {
return calloc(num, size);
}

void
lwesp_mem_free(void* ptr) {
free(ptr);
}

#endif /* LWESP_CFG_MEM_CUSTOM && !__DOXYGEN__ */
21 changes: 15 additions & 6 deletions lwesp/src/system/lwesp_sys_posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -406,26 +406,35 @@ lwesp_sys_thread_create(lwesp_sys_thread_t* t, const char* name,
lwesp_sys_thread_fn thread_func, void* const arg,
size_t stack_size, lwesp_sys_thread_prio_t prio) {

*t = malloc(sizeof(pthread_t));
if (*t == NULL) {
pthread_t* new_thread = malloc(sizeof(pthread_t));
if (new_thread == NULL) {
return 0;
}

if (pthread_create(*t, NULL, (lwesp_sys_posix_thread_fn)thread_func, arg) != 0) {
if (pthread_create(new_thread, NULL, (lwesp_sys_posix_thread_fn)thread_func, arg) != 0) {
free(*t);
return 0;
};

if (t != NULL) {
*t = new_thread;
} else {
free(new_thread);
}

return 1;
}

uint8_t
lwesp_sys_thread_terminate(lwesp_sys_thread_t* t) {
if (pthread_cancel(**t) != 0) {
return 0;
if (t != NULL && *t != NULL) {
if (pthread_cancel(**t) != 0) {
return 0;
}

free(*t);
}

free(*t);
return 1;
}

Expand Down

0 comments on commit d8af85f

Please sign in to comment.