summaryrefslogtreecommitdiff
path: root/avr
diff options
context:
space:
mode:
authorLeo C2018-07-25 20:46:08 +0200
committerLeo C2018-07-25 20:46:08 +0200
commit1816a69880b3a19d323b2a158bca4d6656d917c3 (patch)
tree565a4663d2e1d1c5d696dfcbe9ae82700d7a1188 /avr
parent972e6e9bc05f0d3d868357029a6da2354f2cdb36 (diff)
downloadz180-stamp-1816a69880b3a19d323b2a158bca4d6656d917c3.zip
WIP
Diffstat (limited to 'avr')
-rw-r--r--avr/env.c327
1 files changed, 155 insertions, 172 deletions
diff --git a/avr/env.c b/avr/env.c
index b2b4e6f..9fe61ea 100644
--- a/avr/env.c
+++ b/avr/env.c
@@ -12,6 +12,11 @@
#include "crc.h"
#include "getopt-min.h"
+#define DEBUG_ENV 1 /* set to 1 to debug */
+
+#define debug_env(fmt, args...) \
+ debug_cond(DEBUG_ENV, fmt, ##args)
+
#define ENV_SIZE (CONFIG_ENV_SIZE - sizeof(uint16_t) -1)
#define ACTIVE_FLAG 1
@@ -29,7 +34,7 @@
const FLASH char default_env[] = {
ENV_BAUDRATE "=" "115200" DELIM
ENV_BOOTDELAY "=" "3" DELIM
- ENV_BOOTCMD "=" "pin ${pins}; loadcpm3; go ${startaddress}" DELIM
+ ENV_BOOTCMD "=" "pin ${pins};loadcpm3;go ${startaddress}" DELIM
ENV_CPM3_SYSFILE "=" CONFIG_CPM3_SYSFILE DELIM
ENV_PINALIAS "=" "0:PG5,1:PG4,2:PB4,3:PB5,4:PB6,5:PB7,"
"6:PG3,7:PG2,8:PG1,9:PG0,10:PE7" DELIM
@@ -46,7 +51,7 @@ typedef struct environment_s {
char data[ENV_SIZE]; /* Environment data */
} env_t;
-
+#if 0
/* */
typedef struct env_item_s {
char * envvar;
@@ -56,10 +61,13 @@ typedef struct env_item_s {
static uint8_t env_valid;
static env_item_t env_list[CONFIG_ENVVAR_MAX];
static int entrycount;
+#endif
+static uint8_t env_valid;
+static char env_list[ENV_SIZE];
static
-char env_get_char(uint16_t index)
+char env_get_char(uint_fast16_t index)
{
unsigned int off = CONFIG_ENV_OFFSET;
char ret;
@@ -79,92 +87,101 @@ char env_get_char(uint16_t index)
return ret;
}
-
-static const FLASH char *comp_key;
+static void envlist_clear(void)
+{
+ memset(env_list, 0, sizeof env_list);
+}
static
-int comp_env_items(const void *m1, const void *m2)
+int env_item_delete(char *ep)
{
- env_item_t *ep1 = (env_item_t *) m1;
- env_item_t *ep2 = (env_item_t *) m2;
+ char *next = ep + strlen(ep) + 1;
+ char *end = next;
+ while (*end++ != 0);
+ while (*end++ != 0);
- if (ep1 == NULL)
- return - strcmp_P(ep2->envvar, comp_key);
- else
- return strcmp(ep1->envvar, ep2->envvar);
-}
+ memmove(ep, next, end - next);
+ return 0;
+}
-env_item_t *envlist_search(const MEMX char *name)
+static char *envlist_search(const MEMX char *name)
{
-#ifdef __MEMX
- if (__builtin_avr_flash_segment(name) != -1) {
- comp_key = name;
- return bsearch(0, env_list, entrycount,
- sizeof(env_item_t), comp_env_items);
- } else {
+ char key[CONFIG_SYS_ENV_NAMELEN + 1];
+ const MEMX char *np = name;
+ int keylen = 0;
+ char c;
+
+ while (keylen <= CONFIG_SYS_ENV_NAMELEN && (c = *np++) != '\0')
+ key[keylen++] = c;
+ key[keylen] = '\0';
+
+debug_env("### len: %d, key: \"%s\"\n", keylen, key);
+
+ for (char *lp = env_list; *lp != 0; ++lp) {
- env_item_t e;
- e.envvar = (char *) name;
+ if (strncmp(lp, key, keylen) == 0)
+ return lp;
- return bsearch(&e, env_list, entrycount,
- sizeof(env_item_t), comp_env_items);
+ while (*lp != 0)
+ ++lp;
}
-#else
- env_item_t e;
- e.envvar = (char *) name;
- return bsearch(&e, env_list, entrycount,
- sizeof(env_item_t), comp_env_items);
-#endif /* __MEMX */
+ return NULL;
}
-
-static
-env_item_t *envlist_enter(env_item_t *e)
+static char *env_item_insert(char *pos, const char *envstr)
{
- const size_t size = sizeof(env_item_t);
- env_item_t *ep;
+ char *dstp = pos + strlen(envstr) + 1;
+ char *end = pos;
+ while (*end++ != 0);
+ while (*end++ != 0);
- ep = bsearch(e, env_list, entrycount,
- size, comp_env_items);
+ if (end > env_list + ENV_SIZE)
+ return NULL;
- if (ep == NULL) {
- if (entrycount < CONFIG_ENVVAR_MAX) {
+ memmove(dstp, pos, end - pos);
+ strcpy(pos, envstr);
- env_list[entrycount++] = *e;
- qsort(env_list, entrycount, size, comp_env_items);
- ep = bsearch(e, env_list, entrycount,
- size, comp_env_items);
- }
- } else {
- free(ep->envvar);
- ep->envvar = e->envvar;
- }
-
- return ep;
+ return pos;
}
-static
-int env_item_delete(env_item_t *ep)
+static char *envlist_enter(const char *ep)
{
- if (entrycount == 0)
- return -1;
+ char *lp;
+ size_t len = strchr(ep, '=') - ep;
+ int rc;
- free(ep->envvar);
- entrycount--;
- size_t size = sizeof(env_item_t);
- memmove(ep, ep + 1, (env_list - ep)*size + entrycount*size);
+ for (lp = env_list; *lp != 0; ++lp) {
- return 0;
+ rc = strncmp(lp, ep, len);
+ if (rc >= 0)
+ break;
+
+ /* rc < 0 : check next place */
+ while (*lp != 0)
+ ++lp;
+ }
+
+ /* rc == 0 : entry found, replace */
+ if (rc == 0)
+ env_item_delete(lp);
+
+ /* rc > 0 : entry not in list, insert */
+ /* *lp == 0 : entry not in list, insert */
+
+ lp = env_item_insert(lp, ep);
+
+ return lp;
}
+
static
int envlist_delete(const MEMX char *name)
{
- env_item_t *ep = envlist_search(name);
+ char *ep = envlist_search(name);
if (ep != NULL)
return env_item_delete(ep);
@@ -172,21 +189,15 @@ int envlist_delete(const MEMX char *name)
return 1;
}
-
-
static
int envlist_import(uint8_t flags)
{
uint16_t index;
-
int len;
- env_item_t e;
- char *np, c;
- uint_fast8_t ef;
+ char *ep;
if ((flags & ENVLIST_DELETE) != 0)
- while (entrycount != 0)
- env_item_delete(&env_list[entrycount-1]);
+ envlist_clear();
for (index = 0; env_get_char(index) != '\0'; ) {
for (len = 0; env_get_char(index + len) != '\0'; ++len) {
@@ -194,26 +205,18 @@ int envlist_import(uint8_t flags)
return -1;
}
- np = (char *) malloc(len+1);
- if (np == NULL) {
+ ep = (char *) malloc(len+1);
+ if (ep == NULL) {
printf_P(PSTR("## Can't malloc %d bytes\n"), len+1);
return 1;
}
- e.envvar = np;
- ef = 0;
- while ((c = env_get_char(index++)) != '\0') {
- if (!ef && (c == '=')) {
- *np = '\0';
- ef = 1;
- } else
- *np = c;
- np++;
- }
- *np = '\0';
- envlist_enter(&e);
+ char *p = ep;
+ while ((*p++ = env_get_char(index++)) != '\0')
+ ;
+ envlist_enter(ep);
+ free(ep);
}
-
return 0;
}
@@ -315,36 +318,28 @@ int env_init(void)
char *getenv_str(const MEMX char *name)
{
- env_item_t *ep;
- char *ret = NULL;
-
- ep = envlist_search(name);
+ char *ep = envlist_search(name);
if (ep != NULL)
- ret = ep->envvar + strlen(ep->envvar) +1;
- return ret;
+ return strchr(ep, '=') + 1;
+ else
+ return NULL;
}
static
-int env_item_save(env_item_t *ep, uint16_t offset, int space_left)
+int env_item_save(char *ep, uint16_t offset, int space_left)
{
- int nlen, vlen;
- char *var = ep->envvar;
+ int len;
- nlen = strlen(var);
- if (nlen == 0)
+ len = strlen(ep);
+ if (len == 0)
return 0;
- vlen = strlen(var + nlen + 1);
- if (vlen == 0)
- return 0;
- if (nlen + vlen + 2 > space_left)
+ len += 1;
+ if (len > space_left)
return 0;
- eeprom_update_block(var, (uint8_t *) offset, nlen);
- offset += nlen;
- eeprom_update_byte((uint8_t *) offset++, '=');
- eeprom_update_block(var + nlen +1, (uint8_t *) offset, vlen+1);
+ eeprom_update_block(ep, (uint8_t *) offset, len);
- return nlen + vlen + 2;
+ return len;
}
@@ -367,14 +362,18 @@ int saveenv(void)
pos = off + offsetof(env_t, data);
left = ENV_SIZE - 1;
- for (int i = 0 ; i < entrycount; i++) {
- len = env_item_save(&env_list[i], pos, left);
+
+ char *ep = env_list;
+ while (*ep) {
+ len = env_item_save(ep, pos, left);
if (len == 0) {
return 1;
}
pos += len;
left -= len;
+ while (*ep++);
}
+
/* terminate list */
eeprom_update_byte((uint8_t *) pos, 0);
crc = env_crc(off + offsetof(env_t, data));
@@ -393,18 +392,20 @@ int saveenv(void)
static
-int env_item_print(env_item_t *ep, bool mode)
+int env_item_print(char *ep, bool mode)
{
- int len;
- char *ev = ep->envvar;
+ int len = strlen(ep) + 1;
if (mode) {
- len = printf_P(PSTR("setenv %s "), ev) - 7;
- len += printf_P(PSTR("'%s'\n"), ev + len) - 2;
+ my_puts_P(PSTR("setenv "));
+ char ch;
+ while ((ch = *ep++) != '=')
+ putchar(ch);
+ printf_P(PSTR(" '%s'\n"), ep);
} else {
- len = printf_P(PSTR("%s="), ev);
- len += printf_P(PSTR("%s\n"), ev + len);
+ puts(ep);
}
+
return len;
}
@@ -415,20 +416,23 @@ int env_item_print(env_item_t *ep, bool mode)
* Returns -1 in case of error, or length of printed string
*/
static
-int env_print(const MEMX char *name, bool mode)
+int env_print(const char *name, bool mode)
{
int len = -1;
+ char *ep;
- if (name) { /* print a single name */
+ if (name != NULL) { /* print a single name */
- env_item_t *ep = envlist_search(name);
+ ep = envlist_search(name);
if (ep != NULL)
len = env_item_print(ep, mode);
} else { /* print whole list */
len = 0;
- for (int i = 0 ; i < entrycount; i++) {
- len += env_item_print(&env_list[i], mode);
+ ep = env_list;
+ while (*ep) {
+ len += env_item_print(ep, mode);
+ while (*ep++);
}
}
return len;
@@ -450,65 +454,48 @@ int env_print(const MEMX char *name, bool mode)
* @return 0 if ok, 1 on error
*/
static
-command_ret_t _do_env_set(uint_fast8_t flag, int argc, char * const argv[])
+command_ret_t _do_env_set(uint_fast8_t flag UNUSED, int argc, char * const argv[])
{
- int i, len;
- char *name, *value, *valp, *p;
- env_item_t e, *ep;
-
- (void) flag;
-
- name = argv[1];
- value = argv[2];
+ char *name = argv[1];
if (strchr(name, '=')) {
printf_P(PSTR("## Error: illegal character '='"
"in variable name \"%s\"\n"), name);
return CMD_RET_FAILURE;
}
- len = strlen(name);
+ size_t len = strlen(name);
if (len > CONFIG_SYS_ENV_NAMELEN) {
printf_P(PSTR("## Error: Variable name \"%s\" too long. "
"(max %d characters)\n"), name, CONFIG_SYS_ENV_NAMELEN);
return CMD_RET_FAILURE;
}
-/*
- env_id++;
-*/
+
/* Delete only ? */
- if (argc < 3 || argv[2] == NULL) {
+ if (argc < 3) {
int rc = envlist_delete(name);
return rc ? CMD_RET_FAILURE : CMD_RET_SUCCESS;
}
- /*
- * Insert / replace new value
- */
- for (i = 2, len += 1; i < argc; ++i)
+ /* Insert / replace new value */
+ len += 1;
+ for (int_fast8_t i = 2; i < argc; ++i)
len += strlen(argv[i]) + 1;
- value = malloc(len);
- if (value == NULL) {
+ char *envstr = malloc(len);
+ if (envstr == NULL) {
printf_P(PSTR("## Can't malloc %d bytes\n"), len);
return CMD_RET_FAILURE;
}
- strcpy(value, name);
- valp = value + strlen(name) + 1;
- for (i = 2, p = valp; i < argc; ++i) {
- char *v = argv[i];
-
- while ((*p++ = *v++) != '\0')
- ;
- *(p - 1) = ' ';
+ strcpy(envstr, name);
+ strcat_P(envstr, PSTR("="));
+ for (int_fast8_t i = 2; i < argc; ++i) {
+ strcat(envstr, argv[i]);
+ if (i < argc-1)
+ strcat_P(envstr, PSTR(" "));
}
- if (p != valp)
- *--p = '\0';
-
- e.envvar = value;
- ep = envlist_enter(&e);
- if (!ep) {
- printf_P(PSTR("## Error inserting \"%s\" variable.\n"),
- name);
+
+ if (envlist_enter(envstr) == NULL) {
+ printf_P(PSTR("## Error inserting \"%s\" variable.\n"), name);
return CMD_RET_FAILURE;
}
@@ -528,7 +515,7 @@ int setenv(const MEMX char *varname, const char *varvalue)
int rc;
#ifdef __MEMX
- char *tmpname = NULL;
+ char *tmpname;
if (__builtin_avr_flash_segment(varname) != -1) {
tmpname = malloc(strlen_P(varname)+1);
if (tmpname == NULL) {
@@ -538,9 +525,12 @@ int setenv(const MEMX char *varname, const char *varvalue)
strcpy_P(tmpname, varname);
} else
tmpname = (char *) varname;
-#endif
const char * const argv[3] = { NULL, tmpname, varvalue };
+#else
+ const char * const argv[3] = { NULL, varname, varvalue };
+#endif
+
int argc = 3;
if (varvalue == NULL || varvalue[0] == '\0')
@@ -549,7 +539,8 @@ int setenv(const MEMX char *varname, const char *varvalue)
rc = (int) _do_env_set(0, argc, (char * const *)argv);
#ifdef __MEMX
- free(tmpname);
+ if (__builtin_avr_flash_segment(varname) != -1)
+ free(tmpname);
#endif
return rc;
}
@@ -612,10 +603,9 @@ unsigned long getenv_ulong(const MEMX char *name, int base, unsigned long defaul
unsigned long value;
char *vp, *endp;
- env_item_t *ep = envlist_search(name);
+ vp = getenv_str(name);
- if (ep != NULL ) {
- vp = ep->envvar + strlen(ep->envvar) +1;
+ if (vp != NULL ) {
value = strtoul(vp, &endp, base);
if (endp != vp)
return value;
@@ -643,10 +633,9 @@ bool getenv_yesno(const MEMX char *name)
*/
}
-command_ret_t do_env_print(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * const argv[])
+command_ret_t do_env_print(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED,
+ int argc, char * const argv[])
{
- (void) cmdtp; (void) flag;
-
bool mode = 0;
command_ret_t rc = CMD_RET_SUCCESS;
@@ -689,10 +678,8 @@ command_ret_t do_env_print(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char *
}
-command_ret_t do_env_set(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * const argv[])
+command_ret_t do_env_set(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag, int argc, char * const argv[])
{
- (void) cmdtp;
-
if (argc < 2)
return CMD_RET_USAGE;
@@ -700,11 +687,9 @@ command_ret_t do_env_set(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * c
}
-command_ret_t do_env_default(cmd_tbl_t *cmdtp, uint_fast8_t flag,
- int argc, char * const argv[])
+command_ret_t do_env_default(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED,
+ int argc UNUSED, char * const argv[] UNUSED)
{
- (void) cmdtp; (void) flag; (void) argc; (void) argv;
-
uint8_t tmp = env_valid;
env_valid = 0;
@@ -718,10 +703,8 @@ command_ret_t do_env_default(cmd_tbl_t *cmdtp, uint_fast8_t flag,
}
-command_ret_t do_env_save(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * const argv[])
+command_ret_t do_env_save(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc UNUSED, char * const argv[] UNUSED)
{
- (void) cmdtp; (void) flag; (void) argc; (void) argv;
-
printf_P(PSTR("Saving Environment ...\n"));
return saveenv() ? CMD_RET_FAILURE : CMD_RET_SUCCESS;
}