diff options
Diffstat (limited to 'libiot/sys')
| -rw-r--r-- | libiot/sys/console.c | 211 | ||||
| -rw-r--r-- | libiot/sys/console.h | 61 | ||||
| -rw-r--r-- | libiot/sys/history.c | 199 | ||||
| -rw-r--r-- | libiot/sys/history.h | 57 | ||||
| -rw-r--r-- | libiot/sys/list.c | 429 | ||||
| -rw-r--r-- | libiot/sys/list.h | 85 | ||||
| -rw-r--r-- | libiot/sys/mkfile | 5 | ||||
| -rw-r--r-- | libiot/sys/mutex.c | 155 | ||||
| -rw-r--r-- | libiot/sys/mutex.h | 69 | ||||
| -rw-r--r-- | libiot/sys/panic.c | 52 | ||||
| -rw-r--r-- | libiot/sys/panic.h | 51 |
11 files changed, 1374 insertions, 0 deletions
diff --git a/libiot/sys/console.c b/libiot/sys/console.c new file mode 100644 index 0000000..0c0c293 --- /dev/null +++ b/libiot/sys/console.c @@ -0,0 +1,211 @@ +/* + * Copyright (C) 2015 - 2018, IBEROXARXA SERVICIOS INTEGRALES, S.L. + * Copyright (C) 2015 - 2018, Jaume Olivé Petrus (jolive@whitecatboard.org) + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the <organization> nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * * The WHITECAT logotype cannot be changed, you can remove it, but you + * cannot change it in any way. The WHITECAT logotype is: + * + * /\ /\ + * / \_____/ \ + * /_____________\ + * W H I T E C A T + * + * * Redistributions in binary form must retain all copyright notices printed + * to any local or remote output device. This include any reference to + * Lua RTOS, whitecatboard.org, Lua, and other copyright notices that may + * appear in the future. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * Lua RTOS console utility functions + * + */ + +#include "sdkconfig.h" + +#if CONFIG_LUA_RTOS_USE_CONSOLE + +#include <unistd.h> +#include <stdio.h> +#include <stdlib.h> + +#include <sys/console.h> +#include <sys/fcntl.h> +#include <sys/time.h> + +void console_clear() { + printf("\033[2J\033[1;1H"); +} + +void console_hide_cursor() { + printf("\033[25h"); +} + +void console_show_cursor() { + printf("\033[25l"); +} + +void console_size(int *rows, int *cols) { + struct timeval start; // Start time + struct timeval now; // Current time + int msectimeout = 100; + char buf[6]; + char *cbuf; + char c; + + // Save cursor position + printf("\033[s"); + + // Set cursor out of the screen + printf("\033[999;999H"); + + // Get cursor position + printf("\033[6n"); + + // Return to saved cursor position + printf("\033[u"); + + int flags = fcntl(fileno(stdin), F_GETFL, 0); + fcntl(fileno(stdin), F_SETFL, flags | O_NONBLOCK); + + // Skip scape sequence + gettimeofday(&start, NULL); + for(;;) { + if (read(fileno(stdin), &c, 1) == 1) { + if (c == '\033') { + break; + } + } + + gettimeofday(&now, NULL); + if ((now.tv_sec - start.tv_sec) * 1000 - (((now.tv_usec - start.tv_usec) + 500) / 1000) >= msectimeout) { + break; + } + } + + gettimeofday(&start, NULL); + for(;;) { + if (read(fileno(stdin), &c, 1) == 1) { + if (c == '[') { + break; + } + } + + gettimeofday(&now, NULL); + if ((now.tv_sec - start.tv_sec) * 1000 - (((now.tv_usec - start.tv_usec) + 500) / 1000) >= msectimeout) { + break; + } + } + + // Read rows + c = '\0'; + cbuf = buf; + + gettimeofday(&start, NULL); + for(;;) { + if (read(fileno(stdin), &c, 1) == 1) { + if (c == ';') { + break; + } + *cbuf++ = c; + } + + gettimeofday(&now, NULL); + if ((now.tv_sec - start.tv_sec) * 1000 - (((now.tv_usec - start.tv_usec) + 500) / 1000) >= msectimeout) { + break; + } + } + + *cbuf = '\0'; + + if (*buf != '\0') { + *rows = atoi(buf); + } + + // Read cols + c = '\0'; + cbuf = buf; + + gettimeofday(&start, NULL); + for(;;) { + if (read(fileno(stdin), &c, 1) == 1) { + if (c == 'R') { + break; + } + *cbuf++ = c; + } + + gettimeofday(&now, NULL); + if ((now.tv_sec - start.tv_sec) * 1000 - (((now.tv_usec - start.tv_usec) + 500) / 1000) >= msectimeout) { + break; + } + } + + fcntl(fileno(stdin), F_SETFL, flags); + + *cbuf = '\0'; + + if (*buf != '\0') { + *cols = atoi(buf); + } +} + +void console_gotoxy(int col, int line) { + printf("\033[%d;%dH", line + 1, col + 1); +} + +void console_statusline(const char *text1, const char *text2) { + int rows = 0; + int cols = 0; + + console_size(&rows, &cols); + + console_gotoxy(0, rows); + printf("\033[1m\033[7m%s%s\033[K\033[0m", text1, text2); +} + +void console_clearstatusline() { + int rows = 0; + int cols = 0; + + console_size(&rows, &cols); + + console_gotoxy(0, rows); + printf("\033[K\033[0m"); +} + +void console_erase_eol() { + printf("\033[K"); +} + +void console_erase_sol() { + printf("\033[1K"); +} + +void console_erase_l() { + printf("\033[2K"); +} + +#endif diff --git a/libiot/sys/console.h b/libiot/sys/console.h new file mode 100644 index 0000000..7348890 --- /dev/null +++ b/libiot/sys/console.h @@ -0,0 +1,61 @@ +/* + * Copyright (C) 2015 - 2018, IBEROXARXA SERVICIOS INTEGRALES, S.L. + * Copyright (C) 2015 - 2018, Jaume Olivé Petrus (jolive@whitecatboard.org) + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the <organization> nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * * The WHITECAT logotype cannot be changed, you can remove it, but you + * cannot change it in any way. The WHITECAT logotype is: + * + * /\ /\ + * / \_____/ \ + * /_____________\ + * W H I T E C A T + * + * * Redistributions in binary form must retain all copyright notices printed + * to any local or remote output device. This include any reference to + * Lua RTOS, whitecatboard.org, Lua, and other copyright notices that may + * appear in the future. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * Lua RTOS console utility functions + * + */ + +#ifndef CONSOLE_H +#define CONSOLE_H + +void console_clear(); +void console_size(int *rows, int *cols); +void console_gotoxy(int col, int line); +void console_statusline(const char *text1, const char *text2); +void console_clearstatusline(); +void console_erase_eol(); +void console_erase_sol(); +void console_erase_l(); +void console_hide_cursor(); +void console_show_cursor(); + +#endif /* CONSOLE_H */ + diff --git a/libiot/sys/history.c b/libiot/sys/history.c new file mode 100644 index 0000000..e01e767 --- /dev/null +++ b/libiot/sys/history.c @@ -0,0 +1,199 @@ +/* + * Copyright (C) 2015 - 2018, IBEROXARXA SERVICIOS INTEGRALES, S.L. + * Copyright (C) 2015 - 2018, Jaume Olivé Petrus (jolive@whitecatboard.org) + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the <organization> nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * * The WHITECAT logotype cannot be changed, you can remove it, but you + * cannot change it in any way. The WHITECAT logotype is: + * + * /\ /\ + * / \_____/ \ + * /_____________\ + * W H I T E C A T + * + * * Redistributions in binary form must retain all copyright notices printed + * to any local or remote output device. This include any reference to + * Lua RTOS, whitecatboard.org, Lua, and other copyright notices that may + * appear in the future. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * Lua RTOS, shell history functions + * + */ + +#include "history.h" + +#include <errno.h> +#include <string.h> +#include <stdio.h> + +#include <sys/tail.h> +#include <sys/mount.h> +#include <sys/status.h> +#include <sys/path.h> + +static void history_add_internal(const char *buf) { + // Get the history file name + char fname[PATH_MAX + 1]; + if (!mount_history_file(fname, sizeof(fname))) { + return; + } + + int retries = 0; + +again: { + // Open the history file + FILE *fp = fopen(fname,"a"); + if (!fp) { + // File not found, try to create it, and open again + mkfile(fname); + fp = fopen(fname,"a"); + if (!fp) { + return; + } + } + + // Get file size + long size; + + if ((size = ftell(fp)) < 0) { + fclose(fp); + return; + } + + // Write to history file + int len = strlen(buf); + + int no_space = 0; + + if (fwrite(buf, 1, len, fp) != len) { + no_space = (errno == ENOSPC); + } + + if (!no_space) { + if (fflush(fp) == EOF) { + no_space = (errno == ENOSPC); + } + } + + if (no_space) { + // Truncate file to the original size + if (ftruncate(fileno(fp), size) < 0) { + fclose(fp); + return; + } + + if (retries == 0) { + // Tail file + fclose(fp); + file_tails(fname, fp->_blksize); + + retries++; + goto again; + } + } + + fclose(fp); + } +} + +void history_add(const char *line) { + history_add_internal("\n"); + history_add_internal(line); +} + +int history_get(int index, int up, char *buf, int buflen) { + // Get the history file name + char fname[PATH_MAX + 1]; + if (!mount_history_file(fname, sizeof(fname))) { + return -2; + } + + // Open history file + FILE *fp = fopen(fname,"r"); + if (!fp) { + // File not found, try to create it, and open again + mkfile(fname); + fp = fopen(fname,"r"); + if (!fp) { + return -2; + } + } + + int pos, len, new_pos; + char c; + + if (index == -1) { + fseek(fp, 0, SEEK_END); + pos = ftell(fp); + } else { + pos = index; + } + + if ((pos == 0) && (up)) { + fclose(fp); + return -3; + } + + new_pos = pos; + + while ((pos >= 0) && !feof(fp)) { + if (up) + fseek(fp, --pos, SEEK_SET); + else + fseek(fp, ++pos, SEEK_SET); + + c = fgetc(fp); + if (c == '\n') { + break; + } + } + + new_pos = pos; + + if ((pos >= 0) && !feof(fp)) { + // Get line + fgets(buf, buflen, fp); + len = strlen(buf); + + // Strip \r \n chars at the end, if present + while ((buf[len - 1] == '\r') || (buf[len - 1] == '\n')) { + len--; + } + + buf[len] = '\0'; + } else { + if (feof(fp)) + new_pos = -1; + else + new_pos = 0; + + buf[0] = 0; + } + + fclose(fp); + + return new_pos; +} diff --git a/libiot/sys/history.h b/libiot/sys/history.h new file mode 100644 index 0000000..56a22fd --- /dev/null +++ b/libiot/sys/history.h @@ -0,0 +1,57 @@ +/* + * Copyright (C) 2015 - 2018, IBEROXARXA SERVICIOS INTEGRALES, S.L. + * Copyright (C) 2015 - 2018, Jaume Olivé Petrus (jolive@whitecatboard.org) + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the <organization> nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * * The WHITECAT logotype cannot be changed, you can remove it, but you + * cannot change it in any way. The WHITECAT logotype is: + * + * /\ /\ + * / \_____/ \ + * /_____________\ + * W H I T E C A T + * + * * Redistributions in binary form must retain all copyright notices printed + * to any local or remote output device. This include any reference to + * Lua RTOS, whitecatboard.org, Lua, and other copyright notices that may + * appear in the future. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * Lua RTOS, shell history functions + * + */ +#ifndef _HISTORY_H_ +#define _HISTORY_H_ + +/** + * @brief Add a line to the history file. If the is not left space on the device + * the history file is tailed from the beginning. + * + * @param buf The line to add. + */ +void history_add(const char *line); +int history_get(int index, int up, char *buf, int buflen); + +#endif /* _HISTORY_H_ */ diff --git a/libiot/sys/list.c b/libiot/sys/list.c new file mode 100644 index 0000000..ef965c8 --- /dev/null +++ b/libiot/sys/list.c @@ -0,0 +1,429 @@ +/* + * Copyright (C) 2015 - 2018, IBEROXARXA SERVICIOS INTEGRALES, S.L. + * Copyright (C) 2015 - 2018, Jaume Olivé Petrus (jolive@whitecatboard.org) + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the <organization> nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * * The WHITECAT logotype cannot be changed, you can remove it, but you + * cannot change it in any way. The WHITECAT logotype is: + * + * /\ /\ + * / \_____/ \ + * /_____________\ + * W H I T E C A T + * + * * Redistributions in binary form must retain all copyright notices printed + * to any local or remote output device. This include any reference to + * Lua RTOS, whitecatboard.org, Lua, and other copyright notices that may + * appear in the future. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * Lua RTOS list data structure + * + */ + +//#include "esp_attr.h" + +#include <errno.h> +#include <string.h> +#include <stdlib.h> + +#include <assert.h> + +#include "list.h" +#include "mutex.h" + +void lstinit(struct list *list, int first_index, uint8_t flags) { + // Create the mutex + mtx_init(&list->mutex, NULL, NULL, 0); + + mtx_lock(&list->mutex); + + list->indexes = 0; + list->free = NULL; + list->index = NULL; + list->last = NULL; + list->first_index = first_index; + list->flags = flags; + list->init = 1; + + mtx_unlock(&list->mutex); +} + +int lstadd(struct list *list, void *item, long *item_index) { + struct lstindex *index = NULL; + struct lstindex *indexa = NULL; + int grow = 0; + + mtx_lock(&list->mutex); + + if (list->flags & LIST_DEFAULT) { + // Get an index + if (list->free) { + // Get first free element + index = list->free; + list->free = index->next; + } else { + // Must grow index array + grow = 1; + } + + if (grow) { + // Increment index count + list->indexes++; + + // Create a new index array for allocate new index + indexa = (struct lstindex *)malloc(sizeof(struct lstindex) * list->indexes); + if (!indexa) { + mtx_unlock(&list->mutex); + return ENOMEM; + } + + if (list->index) { + // Copy current index array to new created + bcopy(list->index, indexa, sizeof(struct lstindex) * (list->indexes - 1)); + + // Free current index array + free(list->index); + } + + // Store new index array + list->index = indexa; + + // Current index + index = list->index + list->indexes - 1; + + // Initialize new index + index->index = list->indexes - 1; + } + + index->next = NULL; + index->item = item; + index->deleted = 0; + + // Return index + if (item_index) { + *item_index = index->index + list->first_index; + } + } else if (list->flags & LIST_NOT_INDEXED) { + // Create a new element + index = (struct lstindex *)calloc(1, sizeof(struct lstindex)); + if (!index) { + return ENOMEM; + } + + index->item = item; + + if ((list->index == NULL) && (list->last == NULL)) { + // First element + list->index = index; + } else { + // Almost there is one element in list + assert(list->last != NULL); + + list->last->next = index; + index->previous = list->last; + } + + list->last = index; + + if (item_index) { + *item_index = (long)item; + } + } + + mtx_unlock(&list->mutex); + + return 0; +} + +int lstget(struct list *list, long index, void **item) { + struct lstindex *cindex = NULL; + long iindex; + + mtx_lock(&list->mutex); + + if (list->flags & LIST_DEFAULT) { + if (!list->indexes) { + mtx_unlock(&list->mutex); + return EINVAL; + } + + // Check index + if (index < list->first_index) { + mtx_unlock(&list->mutex); + return EINVAL; + } + + // Get new internal index + iindex = index - list->first_index; + + // Test for a valid index + if (iindex > list->indexes) { + mtx_unlock(&list->mutex); + return EINVAL; + } + + cindex = list->index + iindex; + + if (cindex->deleted) { + mtx_unlock(&list->mutex); + return EINVAL; + } + } else if (list->flags & LIST_NOT_INDEXED) { + cindex = list->index; + + while (cindex) { + if (cindex->item == (void *)index) { + *item = cindex->item; + break; + } + + cindex = cindex->next; + } + + if (!cindex) { + mtx_unlock(&list->mutex); + return EINVAL; + } + } + + *item = cindex->item; + + mtx_unlock(&list->mutex); + + return 0; +} + +int lstremovec(struct list *list, long index, int destroy, bool compact) { + struct lstindex *cindex = NULL; + long iindex; + + mtx_lock(&list->mutex); + + if (list->flags & LIST_DEFAULT) { + // Check index + if (index < list->first_index) { + mtx_unlock(&list->mutex); + return EINVAL; + } + + // Get new internal index + iindex = index - list->first_index; + + // Test for a valid index + if ((iindex < 0) || (iindex > list->indexes)) { + mtx_unlock(&list->mutex); + return EINVAL; + } + + cindex = &list->index[iindex]; + + if (destroy) { + free(cindex->item); + } + + if (compact) { + bcopy(&list->index[iindex+1], &list->index[iindex], sizeof(struct lstindex) * (list->indexes - iindex - 1)); + iindex = list->indexes-1; + cindex = &list->index[iindex]; + } + + cindex->next = list->free; + cindex->deleted = 1; + list->free = cindex; + } else if (list->flags & LIST_NOT_INDEXED) { + cindex = list->index; + + while (cindex) { + if (cindex->item == (void *)index) { + if (cindex->next) { + cindex->next->previous = cindex->previous; + } else { + list->last = cindex->previous; + } + + if (cindex->previous) { + cindex->previous->next = cindex->next; + } else { + list->index = cindex->next; + } + + if (destroy) { + free(cindex->item); + } + + free(cindex); + + break; + } + + cindex = cindex->next; + } + + } + + mtx_unlock(&list->mutex); + + return 0; +} + +int lstremove(struct list *list, long index, int destroy) { + return lstremovec(list, index, destroy, false); +} + +int lstfirst(struct list *list) { + long index; + long res = -1; + + mtx_lock(&list->mutex); + + if (list->flags & LIST_DEFAULT) { + for(index=0;index < list->indexes;index++) { + if (!list->index[index].deleted) { + res = index + list->first_index; + break; + } + } + } else if (list->flags & LIST_NOT_INDEXED) { + if ((list->index != NULL) && (list->last != NULL)) { + res = (long)list->index; + } + } + + mtx_unlock(&list->mutex); + + return res; +} + +int lstlast(struct list *list) { + long index; + long res = -1; + + mtx_lock(&list->mutex); + + if (list->flags & LIST_DEFAULT) { + for(index = list->indexes - 1;index >= 0;index--) { + if (!list->index[index].deleted) { + res = index + list->first_index; + break; + } + } + } else if (list->flags & LIST_NOT_INDEXED) { + if ((list->index != NULL) && (list->last != NULL)) { + res = (long)list->last; + } + } + + mtx_unlock(&list->mutex); + + return res; +} + +int lstnext(struct list *list, long index) { + long res = -1; + long iindex; + + mtx_lock(&list->mutex); + + if (list->flags & LIST_DEFAULT) { + // Check index + if (index < list->first_index) { + mtx_unlock(&list->mutex); + return -1; + } + + // Get new internal index + iindex = index - list->first_index + 1; + + // Get next non deleted item on list + for(;iindex < list->indexes;iindex++) { + if (!list->index[iindex].deleted) { + res = iindex + list->first_index; + break; + } + } + } else if (list->flags & LIST_NOT_INDEXED) { + struct lstindex *cindex = NULL; + + cindex = list->index; + + while (cindex) { + if (cindex->item == (void *)index) { + if (cindex->next) { + res = (long)cindex->next; + } + + break; + } + + cindex = cindex->next; + } + } + + mtx_unlock(&list->mutex); + + return res; +} + +void lstdestroy(struct list *list, int items) { + long index; + + if (!list->init) return; + + mtx_lock(&list->mutex); + + if (list->flags & LIST_DEFAULT) { + if (items) { + for(index=0;index < list->indexes;index++) { + if (!list->index[index].deleted) { + free(list->index[index].item); + } + } + } + + if (0 != list->index) { + free(list->index); + list->index = 0; + } + + } else if (list->flags & LIST_NOT_INDEXED) { + struct lstindex *cindex = NULL; + struct lstindex *pindex = NULL; + + cindex = list->index; + + while (cindex) { + pindex = cindex; + cindex = cindex->next; + + free(pindex); + } + } + + list->init = 0; + + mtx_unlock(&list->mutex); + mtx_destroy(&list->mutex); +} diff --git a/libiot/sys/list.h b/libiot/sys/list.h new file mode 100644 index 0000000..ea397e0 --- /dev/null +++ b/libiot/sys/list.h @@ -0,0 +1,85 @@ +/* + * Copyright (C) 2015 - 2018, IBEROXARXA SERVICIOS INTEGRALES, S.L. + * Copyright (C) 2015 - 2018, Jaume Olivé Petrus (jolive@whitecatboard.org) + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the <organization> nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * * The WHITECAT logotype cannot be changed, you can remove it, but you + * cannot change it in any way. The WHITECAT logotype is: + * + * /\ /\ + * / \_____/ \ + * /_____________\ + * W H I T E C A T + * + * * Redistributions in binary form must retain all copyright notices printed + * to any local or remote output device. This include any reference to + * Lua RTOS, whitecatboard.org, Lua, and other copyright notices that may + * appear in the future. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * Lua RTOS driver common functions + * + */ + +#ifndef _LIST_H +#define _LIST_H + +#include <stdint.h> +#include <stdbool.h> +#include "mutex.h" + +struct list { + struct mtx mutex; + struct lstindex *index; + struct lstindex *free; + struct lstindex *last; + uint8_t indexes; + uint8_t first_index; + uint8_t flags; + uint8_t init; +}; + +struct lstindex { + void *item; + uint8_t index; + uint8_t deleted; + struct lstindex *next; + struct lstindex *previous; +}; + +#define LIST_DEFAULT (1 << 0) +#define LIST_NOT_INDEXED (1 << 1) + +void lstinit(struct list *list, int first_index, uint8_t flags); +int lstadd(struct list *list, void *item, long *item_index); +int lstget(struct list *list, long index, void **item); +int lstremove(struct list *list, long index, int destroy); +int lstremovec(struct list *list, long index, int destroy, bool compact); +int lstfirst(struct list *list); +int lstlast(struct list *list); +int lstnext(struct list *list, long index); +void lstdestroy(struct list *list, int items); + +#endif /* _LIST_H */ diff --git a/libiot/sys/mkfile b/libiot/sys/mkfile new file mode 100644 index 0000000..6eafd7a --- /dev/null +++ b/libiot/sys/mkfile @@ -0,0 +1,5 @@ +OFILES=\ + $OFILES\ + sys/panic.$O\ + sys/mutex.$O\ + sys/list.$O\ diff --git a/libiot/sys/mutex.c b/libiot/sys/mutex.c new file mode 100644 index 0000000..6c63fe3 --- /dev/null +++ b/libiot/sys/mutex.c @@ -0,0 +1,155 @@ +/* + * Copyright (C) 2015 - 2018, IBEROXARXA SERVICIOS INTEGRALES, S.L. + * Copyright (C) 2015 - 2018, Jaume Olivé Petrus (jolive@whitecatboard.org) + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the <organization> nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * * The WHITECAT logotype cannot be changed, you can remove it, but you + * cannot change it in any way. The WHITECAT logotype is: + * + * /\ /\ + * / \_____/ \ + * /_____________\ + * W H I T E C A T + * + * * Redistributions in binary form must retain all copyright notices printed + * to any local or remote output device. This include any reference to + * Lua RTOS, whitecatboard.org, Lua, and other copyright notices that may + * appear in the future. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * Lua RTOS mutex api implementation over FreeRTOS + * + */ + +//#include "sdkconfig.h" +#include <FreeRTOS.h> +#include "../freertos/adds.h" + +//#include "esp_attr.h" + +#include "mutex.h" +#include "panic.h" + +int mtx_inited(struct mtx *mutex) { + return (mutex->lock != 0); +} + +void mtx_init(struct mtx *mutex, const char *name, const char *type, int opts) { + mutex->opts = opts; + + if (mutex->opts == MTX_DEF) { + mutex->lock = xSemaphoreCreateBinary(); + } else if (opts == MTX_RECURSE) { + mutex->lock = xSemaphoreCreateRecursiveMutex(); + } else { + return; + } + + if (mutex->lock) { +#if 0 //{} + if (xPortInIsrContext()) { + BaseType_t xHigherPriorityTaskWoken = pdFALSE; + xSemaphoreGiveFromISR( mutex->lock, &xHigherPriorityTaskWoken); + portEND_SWITCHING_ISR( xHigherPriorityTaskWoken ); + } else { +#endif + if (mutex->opts == MTX_DEF) { + xSemaphoreGive( mutex->lock ); + } +//{} } + } +} + +void mtx_lock(struct mtx *mutex) { +#if 0 //{} + if (xPortInIsrContext()) { + BaseType_t xHigherPriorityTaskWoken = pdFALSE; + xSemaphoreTakeFromISR( mutex->lock, &xHigherPriorityTaskWoken ); + portEND_SWITCHING_ISR( xHigherPriorityTaskWoken ); + } else { +#endif + if (mutex->opts == MTX_DEF) { + xSemaphoreTake( mutex->lock, portMAX_DELAY ); + } else if (mutex->opts == MTX_RECURSE) { + xSemaphoreTakeRecursive( mutex->lock, portMAX_DELAY ); + } +//{} } +} + +int mtx_trylock(struct mtx *mutex) { + if (mutex->opts == MTX_DEF) { + if (xSemaphoreTake( mutex->lock, 0 ) == pdTRUE) { + return 1; + } else { + return 0; + } + } else if (mutex->opts == MTX_RECURSE) { + if (xSemaphoreTakeRecursive( mutex->lock, 0 ) == pdTRUE) { + return 1; + } else { + return 0; + } + } + + return 0; +} + +void mtx_unlock(struct mtx *mutex) { +#if 0 //{} + if (xPortInIsrContext()) { + BaseType_t xHigherPriorityTaskWoken = pdFALSE; + xSemaphoreGiveFromISR( mutex->lock, &xHigherPriorityTaskWoken ); + portEND_SWITCHING_ISR( xHigherPriorityTaskWoken ); + } else { +#endif + if (mutex->opts == MTX_DEF) { + xSemaphoreGive( mutex->lock ); + } else if (mutex->opts == MTX_RECURSE) { + xSemaphoreGiveRecursive( mutex->lock ); + } +//{} } +} + +void mtx_destroy(struct mtx *mutex) { + if (!mutex->lock) return; + + if (mutex->opts == MTX_DEF) { +#if 0 //{} + if (xPortInIsrContext()) { + BaseType_t xHigherPriorityTaskWoken = pdFALSE; + xSemaphoreGiveFromISR( mutex->lock, &xHigherPriorityTaskWoken ); + portEND_SWITCHING_ISR( xHigherPriorityTaskWoken ); + } else { +#endif + xSemaphoreGive( mutex->lock ); +//{} } + + vSemaphoreDelete( mutex->lock ); + } else if (mutex->opts == MTX_RECURSE) { + vSemaphoreDelete( mutex->lock ); + } + + mutex->lock = 0; +} diff --git a/libiot/sys/mutex.h b/libiot/sys/mutex.h new file mode 100644 index 0000000..c2084e9 --- /dev/null +++ b/libiot/sys/mutex.h @@ -0,0 +1,69 @@ +/* + * Copyright (C) 2015 - 2018, IBEROXARXA SERVICIOS INTEGRALES, S.L. + * Copyright (C) 2015 - 2018, Jaume Olivé Petrus (jolive@whitecatboard.org) + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the <organization> nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * * The WHITECAT logotype cannot be changed, you can remove it, but you + * cannot change it in any way. The WHITECAT logotype is: + * + * /\ /\ + * / \_____/ \ + * /_____________\ + * W H I T E C A T + * + * * Redistributions in binary form must retain all copyright notices printed + * to any local or remote output device. This include any reference to + * Lua RTOS, whitecatboard.org, Lua, and other copyright notices that may + * appear in the future. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * Lua RTOS mutex api implementation over FreeRTOS + * + */ + +#ifndef _MUTEX_H +#define _MUTEX_H + +//#include "sdkconfig.h" + +#include <FreeRTOS.h> +#include <semphr.h> + +struct mtx { + SemaphoreHandle_t lock; + int opts; +}; + +#define MTX_DEF 0 +#define MTX_RECURSE 1 + +int mtx_inited(struct mtx *mutex); +void mtx_init(struct mtx *mutex, const char *name, const char *type, int opts); +void mtx_lock(struct mtx *mutex); +int mtx_trylock(struct mtx *mutex); +void mtx_unlock(struct mtx *mutex); +void mtx_destroy(struct mtx *mutex); + +#endif /* _MUTEX_H */ diff --git a/libiot/sys/panic.c b/libiot/sys/panic.c new file mode 100644 index 0000000..a314455 --- /dev/null +++ b/libiot/sys/panic.c @@ -0,0 +1,52 @@ +/* + * Copyright (C) 2015 - 2018, IBEROXARXA SERVICIOS INTEGRALES, S.L. + * Copyright (C) 2015 - 2018, Jaume Olivé Petrus (jolive@whitecatboard.org) + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the <organization> nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * * The WHITECAT logotype cannot be changed, you can remove it, but you + * cannot change it in any way. The WHITECAT logotype is: + * + * /\ /\ + * / \_____/ \ + * /_____________\ + * W H I T E C A T + * + * * Redistributions in binary form must retain all copyright notices printed + * to any local or remote output device. This include any reference to + * Lua RTOS, whitecatboard.org, Lua, and other copyright notices that may + * appear in the future. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * Lua RTOS panic + * + */ + +#include <stdio.h> + +void panic(char *str) { + printf("%s\n", str); + for(;;) { + } +} diff --git a/libiot/sys/panic.h b/libiot/sys/panic.h new file mode 100644 index 0000000..8791dd1 --- /dev/null +++ b/libiot/sys/panic.h @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2015 - 2018, IBEROXARXA SERVICIOS INTEGRALES, S.L. + * Copyright (C) 2015 - 2018, Jaume Olivé Petrus (jolive@whitecatboard.org) + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the <organization> nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * * The WHITECAT logotype cannot be changed, you can remove it, but you + * cannot change it in any way. The WHITECAT logotype is: + * + * /\ /\ + * / \_____/ \ + * /_____________\ + * W H I T E C A T + * + * * Redistributions in binary form must retain all copyright notices printed + * to any local or remote output device. This include any reference to + * Lua RTOS, whitecatboard.org, Lua, and other copyright notices that may + * appear in the future. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * Lua RTOS panic + * + */ + +#ifndef _SYS_PANIC_H_ +#define _SYS_PANIC_H_ + +void panic(char *str); + +#endif /* !_SYS_PANIC_H_ */ |
