xbee stats should be u32
[protos/xbee-avr.git] / eeprom_config.c
1 /*
2  *  Copyright 2013 Olivier Matz <zer0@droids-corp.org>
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  *
18  */
19
20 #include <string.h>
21 #include <stdio.h>
22 #include <stdint.h>
23
24 #include <aversive/pgmspace.h>
25 #include <aversive/eeprom.h>
26 #include <aversive/error.h>
27
28 #include <uart.h>
29
30
31 #include "cmdline.h"
32 #include "eeprom_config.h"
33
34 /* load configuration from eeprom */
35 int8_t eeprom_load_config(void)
36 {
37         struct eeprom_config *e = NULL;
38         struct eeprom_cmd cmd;
39         uint32_t magic;
40         uint8_t i, max;
41
42         eeprom_read_block(&magic, &e->magic, sizeof(magic));
43         if (magic != EEPROM_CONFIG_MAGIC) {
44                 printf_P(PSTR("no EEPROM config\n"));
45                 eeprom_set_ncmds(0);
46                 return 0;
47         }
48
49         max =  eeprom_get_ncmds();
50         for (i = 0; i < max; i++) {
51                 eeprom_get_cmd(&cmd, i);
52                 printf_P(PSTR("%s"), cmd.buf);
53                 cmdline_valid_buffer(cmd.buf, strlen(cmd.buf));
54         }
55
56         return -1;
57 }
58
59 uint8_t eeprom_get_ncmds(void)
60 {
61         struct eeprom_config *e = NULL;
62         uint8_t ncmds;
63
64         eeprom_read_block(&ncmds, &e->ncmds, sizeof(ncmds));
65         return ncmds;
66 }
67
68 void eeprom_set_ncmds(uint8_t ncmds)
69 {
70         struct eeprom_config *e = NULL;
71         uint32_t magic = EEPROM_CONFIG_MAGIC;
72         eeprom_update_block(&ncmds, &e->ncmds, sizeof(ncmds));
73         eeprom_update_block(&magic, &e->magic, sizeof(magic));
74 }
75
76 /* fill cmd struct with the n-th command from eeprom, no check is done
77  * on index or size. The \0 is added at the end of the string. */
78 void eeprom_get_cmd(struct eeprom_cmd *cmd, uint8_t n)
79 {
80         struct eeprom_config *e = NULL;
81
82         eeprom_read_block(cmd, &e->cmds[n], sizeof(*cmd));
83         cmd->buf[EEPROM_CMD_SIZE-1] = '\0';
84 }
85
86 /* fill n-th command of eeprom from struct, no check is done on index
87  * or size */
88 void eeprom_set_cmd(struct eeprom_cmd *cmd, uint8_t n)
89 {
90         struct eeprom_config *e = NULL;
91
92         eeprom_update_block(cmd, &e->cmds[n], sizeof(*cmd));
93 }
94
95 void eeprom_dump_cmds(void)
96 {
97         uint8_t i, max;
98         struct eeprom_cmd cmd;
99
100         printf_P(PSTR("init commands:\n"));
101         max = eeprom_get_ncmds();
102         for (i = 0; i < max; i++) {
103                 eeprom_get_cmd(&cmd, i);
104                 printf_P(PSTR("%.2d: %s"), i, cmd.buf);
105         }
106 }
107
108 int8_t eeprom_insert_cmd_before(const char *str, uint8_t n)
109 {
110         uint8_t i, max;
111         struct eeprom_cmd cmd;
112
113         if (strlen(str) >= EEPROM_CMD_SIZE)
114                 return -1;
115
116         max = eeprom_get_ncmds();
117         if (n > max)
118                 return -1;
119         if (max >= EEPROM_N_CMD_MAX)
120                 return -1;
121
122         for (i = max; i > n; i--) {
123                 eeprom_get_cmd(&cmd, i-1);
124                 eeprom_set_cmd(&cmd, i);
125         }
126
127         snprintf(cmd.buf, sizeof(cmd.buf), "%s", str);
128         eeprom_set_cmd(&cmd, n);
129         eeprom_set_ncmds(max + 1);
130         return 0;
131 }
132
133 int8_t eeprom_append_cmd(const char *str)
134 {
135         uint8_t max;
136
137         max = eeprom_get_ncmds();
138         return eeprom_insert_cmd_before(str, max);
139 }
140
141 int8_t eeprom_delete_cmd(uint8_t n)
142 {
143         uint8_t i, max;
144         struct eeprom_cmd cmd;
145
146         max = eeprom_get_ncmds();
147         if (n >= max)
148                 return -1;
149
150         for (i = n; i < max-1; i++) {
151                 eeprom_get_cmd(&cmd, i+1);
152                 eeprom_set_cmd(&cmd, i);
153         }
154
155         eeprom_set_ncmds(max - 1);
156         return 0;
157 }