// read config from uSD int8_t configReadFile(char *fname) { char *fileBuf; int8_t fh; int ret; int p1; if (fname == 0) fname = CONFIG_FILE_NAME; if ((fh = filerGetHandle(fname)) < 0) { AQ_NOTICE("config: cannot get read file handle\n"); return -1; } fileBuf = (char *)aqCalloc(CONFIG_FILE_BUF_SIZE, sizeof(char)); p1 = 0; while ((ret = filerRead(fh, fileBuf, -1, CONFIG_FILE_BUF_SIZE)) > 0) p1 = configParseParams((char *)fileBuf, ret, p1); filerClose(fh); if (fileBuf) aqFree(fileBuf, CONFIG_FILE_BUF_SIZE, sizeof(char)); return ret; }
// read config from uSD int8_t configReadFile(char *fname) { char *fileBuf; int8_t fh; int ret; int p1; if (fname == 0) fname = CONFIG_FILE_NAME; if ((fh = filerGetHandle(fname)) < 0) { AQ_NOTICE("config: cannot get read file handle\n"); return -1; } if (!(fileBuf = (char *)aqCalloc(CONFIG_FILE_BUF_SIZE, sizeof(char)))) { AQ_NOTICE("config: Error reading from file, cannot allocate memory.\n"); filerClose(fh); return -1; } p1 = 0; while ((ret = filerRead(fh, fileBuf, -1, CONFIG_FILE_BUF_SIZE)) > 0) { p1 = configParseParams((char *)fileBuf, ret, p1); if (p1 < 0) { ret = -1; break; } } filerClose(fh); if (fileBuf) aqFree(fileBuf, CONFIG_FILE_BUF_SIZE, sizeof(char)); if (ret > -1) AQ_NOTICE("config: Parameters loaded from local storage file.\n"); else AQ_NOTICE("config: Failed to read parameters from local file."); return ret; }
// read esc32 params from uSD static int8_t esc32ReadFile(char *fname, canNodes_t *canNode) { char *fileBuf; char *lineBuf; char param[24]; int paramId; float value, retValue; int8_t needsConfigWrite = 0; int8_t fh; char c; int ret; int i, p1, p2; int n; if (fname == 0) fname = ESC32_FILE_NAME; if ((fh = filerGetHandle(fname)) < 0) { AQ_NOTICE("ESC32: cannot get read file handle\n"); return -1; } fileBuf = (char *)aqCalloc(ESC32_FILE_BUF_SIZE, sizeof(char)); lineBuf = (char *)aqCalloc(ESC32_LINE_BUF_SIZE, sizeof(char)); needsConfigWrite = 0; if (fileBuf && lineBuf) { p1 = 0; do { ret = filerRead(fh, fileBuf, -1, ESC32_FILE_BUF_SIZE); p2 = 0; for (i = 0; i < ret; i++) { c = fileBuf[p2++]; if (c == '\n' || p1 == (ESC32_LINE_BUF_SIZE-1)) { lineBuf[p1] = 0; n = sscanf(lineBuf, "#define DEFAULT_%23s %f", param, &value); if (n != 2) { n = sscanf(lineBuf, "#define %23s %f", param, &value); if (n != 2) n = sscanf(lineBuf, "%23s %f", param, &value); } if (n == 2) { // lookup parameter id if (canNode) paramId = canGetParamIdByName(canNode->networkId, (uint8_t *)param); else paramId = esc32OwGetParamId(param); // valid id? if (paramId >= 0) { // read if (canNode) retValue = canGetParamById(canNode->networkId, paramId); else retValue = esc32OwReadParamById(paramId); // current value differs from ours? if (retValue != value) { // write if (canNode) canSetParamById(canNode->networkId, paramId, value); else esc32OwWriteParam(paramId, value); // read back if (canNode) retValue = canGetParamById(canNode->networkId, paramId); else retValue = esc32OwReadParamById(paramId); // successful write? if (retValue == value) { needsConfigWrite++; } else { AQ_NOTICE("ESC32: parameter write failed!\n"); } } } } p1 = 0; } else { lineBuf[p1++] = c; } } } while (ret > 0); filerClose(fh); } if (lineBuf) aqFree(lineBuf, ESC32_LINE_BUF_SIZE, sizeof(char)); if (fileBuf) aqFree(fileBuf, ESC32_FILE_BUF_SIZE, sizeof(char)); return needsConfigWrite; }