streamline CMD function

pull/65/head
Thorsten von Eicken 9 years ago
parent 2a47c81999
commit d87468e3c7
  1. 1
      cmd/cmd.c
  2. 3
      cmd/cmd.h
  3. 9
      cmd/handlers.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"

@ -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;

@ -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];
}

Loading…
Cancel
Save