From d87468e3c74f98d402602cfcadd45766975b77a8 Mon Sep 17 00:00:00 2001 From: Thorsten von Eicken Date: Sun, 20 Sep 2015 10:23:45 -0700 Subject: [PATCH] streamline CMD function --- cmd/cmd.c | 1 + cmd/cmd.h | 3 ++- cmd/handlers.c | 9 ++++----- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/cmd/cmd.c b/cmd/cmd.c index 520143e..e7ec63f 100644 --- a/cmd/cmd.c +++ b/cmd/cmd.c @@ -2,6 +2,7 @@ // // Adapted from: github.com/tuanpmt/esp_bridge, Created on: Jan 9, 2015, Author: Minh +#include "esp8266.h" #include "cmd.h" #include "crc16.h" #include "uart.h" diff --git a/cmd/cmd.h b/cmd/cmd.h index d8b5550..ecd8140 100644 --- a/cmd/cmd.h +++ b/cmd/cmd.h @@ -66,8 +66,9 @@ typedef struct { cmdfunc_t sc_function; } CmdList; +#define CMD_CBNLEN 16 typedef struct { - char name[16]; + char name[CMD_CBNLEN]; uint32_t callback; } cmdCallback; diff --git a/cmd/handlers.c b/cmd/handlers.c index 3b21aea..9642856 100644 --- a/cmd/handlers.c +++ b/cmd/handlers.c @@ -77,8 +77,9 @@ CMD_AddCb(char* name, uint32_t cb) { //os_printf("CMD_AddCb: index %d name=%s cb=%p\n", i, callbacks[i].name, // (void *)callbacks[i].callback); // find existing callback or add to the end - if (os_strcmp(callbacks[i].name, name) == 0 || callbacks[i].name[0] == '\0') { + if (os_strncmp(callbacks[i].name, name, CMD_CBNLEN) == 0 || callbacks[i].name[0] == '\0') { os_strncpy(callbacks[i].name, name, sizeof(callbacks[i].name)); + callbacks[i].name[CMD_CBNLEN-1] = 0; // strncpy doesn't null terminate callbacks[i].callback = cb; #ifdef CMD_DBG os_printf("CMD_AddCb: cb %s added at index %d\n", callbacks[i].name, i); @@ -91,15 +92,13 @@ CMD_AddCb(char* name, uint32_t cb) { cmdCallback* ICACHE_FLASH_ATTR CMD_GetCbByName(char* name) { - char checkname[16]; - os_strncpy(checkname, name, sizeof(checkname)); for (uint8_t i = 0; i < MAX_CALLBACKS; i++) { //os_printf("CMD_GetCbByName: index %d name=%s cb=%p\n", i, callbacks[i].name, // (void *)callbacks[i].callback); // if callback doesn't exist or it's null - if (os_strcmp(callbacks[i].name, checkname) == 0) { + if (os_strncmp(callbacks[i].name, name, CMD_CBNLEN) == 0) { #ifdef CMD_DBG - os_printf("CMD_GetCbByName: cb %s found at index %d\n", callbacks[i].name, i); + os_printf("CMD_GetCbByName: cb %s found at index %d\n", name, i); #endif return &callbacks[i]; }