1#include "main-header.h"
8static uint32_t userconfig_calc_checksum(
struct user_config *cfg) {
9 return crc32(&cfg->userdata, cfg->validbytes);
13 struct user_config *cfg = get_flash_user_config_addr();
16 printf(
"[userconfig] warning - NO USERCONFIG FLASH SECTION (struct too big? %i bytes)!!!\r\n",
sizeof(
_user_config));
20uint32_t userconfig_size() {
26 uint32_t c = userconfig_calc_checksum(cfg);
28 if (c != cfg->checksum) {
29 printf(
"[userconfig] Invalid checksum\r\n");
32 return cfg->validbytes;
34uint32_t userconfig_version() {
40 return cfg->data_version;
43int userconfig_copy(uint8_t *buf, uint16_t bufsize, uint16_t *actual_size) {
49 if (cfg->validbytes > bufsize) {
54 printf(
"[userconfig] copying userdata from userconfig at %p\r\n",cfg);
55 for (i = 0; i < cfg->validbytes; i++) {
56 buf[i] = cfg->userdata[i];
58 *actual_size = cfg->validbytes;
65static int userconfig_receive_init(
struct command *com) {
67 userconfiginram.validbytes = 0;
82static int userconfig_receive_data(
struct command *com) {
90 printf(
"[userconfig]: pid mismatch\r\n");
96 if ((lastpos + 1) != ((uint8_t)pid)) {
97 printf(
"[userconfig]: Pos out of sequence: %i vs %i\r\n", (uint32_t)lastpos, pid);
102 printf(
"[userconfig]: missing size\r\n");
105 const char *data =
namedarg(com,
"data");
108 printf(
"[userconfig]: missing data\r\n");
111 const uint8_t *bytes = (
const uint8_t *)data;
113 uint32_t j = userconfiginram.validbytes;
115 if ((j + pid) >
sizeof(userconfiginram.userdata)) {
116 printf(
"[userconfig: too big (%i bytes does not fit into %i bytes buffer\r\n", (j + pid),
sizeof(userconfiginram.userdata));
119 for (i = 0; i < pid; i++) {
120 userconfiginram.userdata[j] = bytes[i];
123 userconfiginram.validbytes = j;
130 r = write_user_config_to_flash();
137int userconfig_write(uint8_t *buf, uint32_t size) {
138 if (size >
sizeof(userconfiginram.userdata)) {
139 printf(
"[userconfig]: buf too big (%i > %i)\r\n", size,
sizeof(userconfiginram.userdata));
144 for (i = 0; i < size; i++) {
145 userconfiginram.userdata[i] = buf[i];
147 userconfiginram.validbytes = size;
148 return write_user_config_to_flash();
152int write_user_config_to_flash() {
159 if (userconfiginram.validbytes == cfg->validbytes) {
162 for (i = 0; i < cfg->validbytes; i++) {
163 if ((userconfiginram.userdata[i] != cfg->userdata[i])) {
169 printf(
"[userconfig] no change, not flashing\r\n");
175 userconfiginram.checksum = userconfig_calc_checksum(&userconfiginram);
176 userconfiginram.data_version = cfg->data_version + 1;
177 if ((r = mculib_flash_unlock(MAIN_MCULIBHANDLE))) {
178 printf(
"[userconfig]: Unlock flash failed: %i\r\n", r);
179 mculib_flash_lock(MAIN_MCULIBHANDLE);
182 if ((r = mculib_flash_erase(MAIN_MCULIBHANDLE, (
void *)cfg, size + 4))) {
183 printf(
"[userconfig]: Erase config failed: %i\r\n", r);
184 mculib_flash_lock(MAIN_MCULIBHANDLE);
187 printf(
"[userconfig]: writing in-ram copy to flash @ 0x%p (%i bytes)\r\n", (
void *)cfg, size);
188 if ((r = mculib_flash_write(MAIN_MCULIBHANDLE, (
void *)&userconfiginram, (
void *)cfg, size)) != 0) {
189 printf(
"[userconfig]: Write config failed: %i\r\n", r);
190 mculib_flash_lock(MAIN_MCULIBHANDLE);
194 mculib_flash_lock(MAIN_MCULIBHANDLE);
195 printf(
"[userconfig]: Saved %i bytes to flash. Flash result: %i\r\n", size, r);
201static int send_userconfig_data(
struct command *com,
struct user_config *cfg,
int cur,
int offset,
int size) {
209 base = (
const uint8_t *)&cfg->userdata;
210 base = base + offset;
215 reply->
flags = COMFLAGS_DATA;
224static int userconfig_send_data(
struct command *
com) {
236 int tblen = userconfig_size();
237 int tvers = userconfig_version();
239 printf(
"[userconfig] sending %i bytes (version %i)\r\n", tblen, tvers);
245 if (offset >= tblen) {
249 if (offset + size > tblen) {
250 size = tblen - offset;
252 send_userconfig_data(
com, cfg, cur, offset, size);
254 offset = offset + size;
260 reply->
flags = COMFLAGS_ACK;
266 printf(
"[userconfig] sent %i bytes in %i commands\r\n", tblen, tcoms);
282 return userconfig_receive_init(
com);
284 return userconfig_receive_data(
com);
286 return userconfig_send_data(
com);
int namedarg_uint16(struct command *com, const char *name, uint16_t *value)
get a named arg (key-value pair), parsed as integer. result in "value". if return value == 0 ,...
int send_command(struct command *com)
send a command to another module (or broadcast)
void free_command(struct command *com)
free a command
int deliver_command(struct command *com, pkt_callback)
deliver a command to a module
void command_add_arg(struct command *com, const char *format,...)
adds an arg to a partially initialised command structure
const char * namedarg(struct command *com, const char *name)
get a named arg (key-value pair) or NULL
int namedarg_uint32(struct command *com, const char *name, uint32_t *value)
get a named arg (key-value pair), parsed as integer. result in "value". if return value == 0 ,...
struct command * alloc_command()
allocate a free command
int command_add_binary_arg(struct command *com, const int len, const byte *srcbuf)
adds a binary parameter to command returns 0 if ok otherwise errorcode