mirror of
https://github.com/eddyem/eddys_snippets.git
synced 2026-11-22 22:00:19 +03:00
a lot of fixes
This commit is contained in:
@@ -55,19 +55,31 @@ size_t get_aliasessize(){ return aliasessize; }
|
||||
|
||||
// functions for `qsort` (`needle` and `straw` are pointers to pointers)
|
||||
static int sort_by_code(const void *needle, const void *straw){
|
||||
if(!needle && !straw) return 0;
|
||||
if(!needle) return -1;
|
||||
if(!straw) return 1;
|
||||
const char *c1 = (*(dicentry_t**)needle)->code, *c2 = (*(dicentry_t**)straw)->code;
|
||||
return strcmp(c1, c2);
|
||||
}
|
||||
static int sort_by_reg(const void *needle, const void *straw){
|
||||
if(!needle && !straw) return 0;
|
||||
if(!needle) return -1;
|
||||
if(!straw) return 1;
|
||||
const int r1 = (int)(*(dicentry_t**)needle)->reg, r2 = (int)(*(dicentry_t**)straw)->reg;
|
||||
return r1 - r2;
|
||||
}
|
||||
// functions for `binsearch`
|
||||
static int search_by_code(const void *code, const void *straw){
|
||||
if(!code && !straw) return 0;
|
||||
if(!code) return -1;
|
||||
if(!straw) return 1;
|
||||
const char *c1 = (const char*)code, *c2 = ((dicentry_t*)straw)->code;
|
||||
return strcmp(c1, c2);
|
||||
}
|
||||
static int search_by_reg(const void *reg, const void *straw){
|
||||
if(!reg && !straw) return 0;
|
||||
if(!reg) return -1;
|
||||
if(!straw) return 1;
|
||||
const int r1 = (int)(*(uint16_t*)reg), r2 = (int)((dicentry_t*)straw)->reg;
|
||||
return r1 - r2;
|
||||
}
|
||||
@@ -92,14 +104,14 @@ static char *getcomment(char *str){
|
||||
// all after "#" is comment;
|
||||
// dictionary format: "'code' 'register' 'value' 'readonly flag'\n", e.g.
|
||||
// "F00.09 61444 5000 1"
|
||||
int opendict(const char *dic){
|
||||
if(!dic) return FALSE;
|
||||
bool opendict(const char *dic){
|
||||
if(!dic || !*dic) return false;
|
||||
DBG("Open %s", dic);
|
||||
closedict(); // close early opened dictionary to prevent problems
|
||||
FILE *f = fopen(dic, "r");
|
||||
if(!f){
|
||||
WARN("Can't open %s", dic);
|
||||
return FALSE;
|
||||
return false;
|
||||
}
|
||||
size_t dicsz = 0;
|
||||
size_t linesz = BUFSIZ;
|
||||
@@ -107,7 +119,7 @@ int opendict(const char *dic){
|
||||
dicentry_t curentry;
|
||||
curentry.code = MALLOC(char, BUFSIZ);
|
||||
// `help` field of `curentry` isn't used here
|
||||
int retcode = TRUE;
|
||||
bool retcode = true;
|
||||
char formatbuf[64];
|
||||
snprintf(formatbuf, 64, "%%%ds %%" SCNu16 " %%" SCNu16 " %%" SCNu8, BUFSIZ-1);
|
||||
while(1){
|
||||
@@ -123,21 +135,26 @@ int opendict(const char *dic){
|
||||
WARNX("Can't understand this line: '%s'", line);
|
||||
continue;
|
||||
}
|
||||
DBG("Got line: '%s %" PRIu16 " %" PRIu16 " %" PRIu8, curentry.code, curentry.reg, curentry.value, curentry.readonly);
|
||||
DBG("Got line: '%s %" PRIu16 " %" PRIu16 " %" PRIu8, curentry.code, curentry.reg, curentry.value, curentry.readonly);
|
||||
if(++dictsize >= dicsz){
|
||||
size_t newsize = dicsz + 50;
|
||||
dicentry_t *newdic = realloc(dictionary, sizeof(dicentry_t) * newsize);
|
||||
if(!newdic){
|
||||
WARN("Can't allocate memory for dictionary");
|
||||
retcode = FALSE;
|
||||
retcode = false;
|
||||
--dictsize;
|
||||
goto ret;
|
||||
WARN("Can't allocate memory for dictionary; got only %zd entries", dictsize);
|
||||
break;
|
||||
}
|
||||
dictionary = newdic;
|
||||
dicsz = newsize;
|
||||
}
|
||||
dicentry_t *entry = &dictionary[dictsize-1];
|
||||
entry->code = strdup(curentry.code);
|
||||
if(!entry->code){
|
||||
WARN("strdup()");
|
||||
--dictsize;
|
||||
continue;
|
||||
}
|
||||
entry->reg = curentry.reg;
|
||||
entry->value = curentry.value;
|
||||
entry->readonly = curentry.readonly;
|
||||
@@ -146,13 +163,14 @@ int opendict(const char *dic){
|
||||
//DBG("Add entry; now dictsize is %d", dictsize);
|
||||
}
|
||||
// init dictionaries for sort
|
||||
dictbycode = MALLOC(dicentry_t*, dictsize);
|
||||
dictbyreg = MALLOC(dicentry_t*, dictsize);
|
||||
for(size_t i = 0; i < dictsize; ++i)
|
||||
dictbyreg[i] = dictbycode[i] = &dictionary[i];
|
||||
qsort(dictbycode, dictsize, sizeof(dicentry_t*), sort_by_code);
|
||||
qsort(dictbyreg, dictsize, sizeof(dicentry_t*), sort_by_reg);
|
||||
ret:
|
||||
if(dictsize > 0){
|
||||
dictbycode = MALLOC(dicentry_t*, dictsize);
|
||||
dictbyreg = MALLOC(dicentry_t*, dictsize);
|
||||
for(size_t i = 0; i < dictsize; ++i)
|
||||
dictbyreg[i] = dictbycode[i] = &dictionary[i];
|
||||
qsort(dictbycode, dictsize, sizeof(dicentry_t*), sort_by_code);
|
||||
qsort(dictbyreg, dictsize, sizeof(dicentry_t*), sort_by_reg);
|
||||
}
|
||||
fclose(f);
|
||||
FREE(curentry.code);
|
||||
FREE(line);
|
||||
@@ -163,12 +181,12 @@ ret:
|
||||
* @brief chkdict - check if dictionary is opened
|
||||
* @return TRUE if dictionary opened
|
||||
*/
|
||||
int chkdict(){
|
||||
bool chkdict(){
|
||||
if(dictsize < 1){
|
||||
WARNX("Init dictionary first");
|
||||
return FALSE;
|
||||
return false;
|
||||
}
|
||||
return TRUE;
|
||||
return true;
|
||||
}
|
||||
|
||||
void closedict(){
|
||||
@@ -210,15 +228,14 @@ dicentry_t *findentry_by_code(const char *code){
|
||||
dicentry_t *findentry_by_reg(uint16_t reg){
|
||||
if(!chkdict()) return NULL;
|
||||
return binsearch(dictbyreg, (const void*)(®), search_by_reg);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/** dump functions **/
|
||||
/** dump functions (thread-unsafe!) **/
|
||||
|
||||
// prepare a list with dump parameters (new call will rewrite previous list)
|
||||
int setdumppars(char **pars){
|
||||
if(!pars || !*pars) return FALSE;
|
||||
if(!chkdict()) return FALSE;
|
||||
bool setdumppars(char **pars){
|
||||
if(!pars || !*pars) return false;
|
||||
if(!chkdict()) return false;
|
||||
FNAME();
|
||||
char **p = pars;
|
||||
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
|
||||
@@ -231,7 +248,7 @@ int setdumppars(char **pars){
|
||||
WARNX("Can't find entry with code %s", *p);
|
||||
pthread_mutex_unlock(&mutex);
|
||||
dumpsize = 0;
|
||||
return FALSE;
|
||||
return false;
|
||||
}
|
||||
DBG("found entry do dump, reg=%d", e->reg);
|
||||
if(cursz <= N){
|
||||
@@ -241,7 +258,7 @@ int setdumppars(char **pars){
|
||||
if(!newpars){
|
||||
WARN("realloc()");
|
||||
pthread_mutex_unlock(&mutex);
|
||||
return FALSE;
|
||||
return false;
|
||||
}else{
|
||||
dumppars = newpars;
|
||||
cursz = newsz;
|
||||
@@ -254,45 +271,53 @@ int setdumppars(char **pars){
|
||||
dumppars[N] = *e;
|
||||
dumppars[N].code = strdup(e->code);
|
||||
if(e->help) dumppars[N].help = strdup(e->help);
|
||||
else dumppars[N].help = NULL;
|
||||
DBG("Add %s", e->code);
|
||||
++N; ++p;
|
||||
}
|
||||
dumpsize = N;
|
||||
pthread_mutex_unlock(&mutex);
|
||||
return TRUE;
|
||||
return true;
|
||||
}
|
||||
|
||||
// open dump file and add header; return FALSE if failed
|
||||
int opendumpfile(const char *name){
|
||||
if(!chkdict()) return FALSE;
|
||||
// open dump file (path relative to "/tmp") and add header; return FALSE if failed
|
||||
bool opendumpfile(const char *name){
|
||||
if(!chkdict()) return false;
|
||||
if(dumpsize < 1){
|
||||
WARNX("Set dump parameters first");
|
||||
return FALSE;
|
||||
return false;
|
||||
}
|
||||
if(!name) return FALSE;
|
||||
if(!name || !*name) return false;
|
||||
if(strstr(name, "../")) return false; // don't allow change to upper level
|
||||
closedumpfile();
|
||||
dumpfile = fopen(name, "w+");
|
||||
char path[PATH_MAX];
|
||||
ssize_t L;
|
||||
if(*name == '/') L = snprintf(path, PATH_MAX-1, "/tmp%s", name);
|
||||
else L = snprintf(path, PATH_MAX-1, "/tmp/%s", name);
|
||||
if(L < 1) return false;
|
||||
if(L >= PATH_MAX-1) path[PATH_MAX-1] = 0;
|
||||
dumpfile = fopen(path, "w+");
|
||||
if(!dumpfile){
|
||||
WARN("Can't open %s", name);
|
||||
return FALSE;
|
||||
WARN("Can't open %s", path);
|
||||
return false;
|
||||
}
|
||||
dumpname = strdup(name);
|
||||
dumpname = strdup(path);
|
||||
fprintf(dumpfile, "# time,s ");
|
||||
for(int i = 0; i < dumpsize; ++i){
|
||||
fprintf(dumpfile, "%s ", dumppars[i].code);
|
||||
}
|
||||
fprintf(dumpfile, "\n");
|
||||
return TRUE;
|
||||
return true;
|
||||
}
|
||||
|
||||
char *getdumpname(){ return dumpname;}
|
||||
const char *getdumpname(){ return dumpname;}
|
||||
|
||||
void closedumpfile(){
|
||||
int stpd = atomic_load(&isstopped);
|
||||
if(dumpfile){
|
||||
if(!stpd){
|
||||
atomic_store(&stopdump, 1);
|
||||
while(!atomic_load(&isstopped));
|
||||
while(!atomic_load(&isstopped)) usleep(1000);
|
||||
}
|
||||
fclose(dumpfile);
|
||||
dumpfile = NULL;
|
||||
@@ -320,29 +345,30 @@ static void *dumpthread(void *p){
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int setDumpT(double dT){
|
||||
if(dT < 0.){
|
||||
WARNX("Time interval should be > 0");
|
||||
return FALSE;
|
||||
bool setDumpT(double dT){
|
||||
if(dT < 0. || dT > 60.){
|
||||
WARNX("Time interval should be > 0 and <= 60.");
|
||||
return false;
|
||||
}
|
||||
dumpTime = dT;
|
||||
DBG("user give dT: %g", dT);
|
||||
return TRUE;
|
||||
return true;
|
||||
}
|
||||
|
||||
int rundump(){
|
||||
bool rundump(){
|
||||
if(!dumpfile){
|
||||
WARNX("Open dump file first");
|
||||
return FALSE;
|
||||
return false;
|
||||
}
|
||||
pthread_t thread;
|
||||
if(pthread_create(&thread, NULL, dumpthread, (void*)&dumpTime)){
|
||||
WARN("Can't create dumping thread");
|
||||
return FALSE;
|
||||
return false;
|
||||
}
|
||||
while(atomic_load(&isstopped)) usleep(1000); // wait until thread starts
|
||||
DBG("Thread created, detach");
|
||||
pthread_detach(thread);
|
||||
return TRUE;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -356,7 +382,7 @@ int rundump(){
|
||||
char *dicentry_descr(dicentry_t *entry, char *buf, size_t bufsize){
|
||||
//DBG("descr of %s", entry->code);
|
||||
if(!entry || !buf || !bufsize) return NULL;
|
||||
size_t L;
|
||||
ssize_t L;
|
||||
if(entry->help){
|
||||
L = snprintf(buf, bufsize, "%s %4" PRIu16 " %4" PRIu16 " %" PRIu8 " # %s\n",
|
||||
entry->code, entry->reg, entry->value, entry->readonly, entry->help);
|
||||
@@ -364,7 +390,8 @@ char *dicentry_descr(dicentry_t *entry, char *buf, size_t bufsize){
|
||||
L = snprintf(buf, bufsize, "%s %4" PRIu16 " %4" PRIu16 " %" PRIu8 "\n",
|
||||
entry->code, entry->reg, entry->value, entry->readonly);
|
||||
}
|
||||
if(L >= bufsize) buf[bufsize-1] = 0;
|
||||
if(L < 1) return NULL;
|
||||
else if(L >= (ssize_t)bufsize) buf[bufsize-1] = 0;
|
||||
return buf;
|
||||
}
|
||||
|
||||
@@ -375,7 +402,7 @@ char *dicentry_descrN(size_t N, char *buf, size_t bufsize){
|
||||
|
||||
// dump all registers by input dictionary into output; also modify values of registers in dictionary
|
||||
int read_dict_entries(const char *outdic){
|
||||
if(!chkdict()) return -1;
|
||||
if(!outdic || !chkdict()) return -1;
|
||||
int got = 0;
|
||||
FILE *o = fopen(outdic, "w");
|
||||
if(!o){
|
||||
@@ -388,7 +415,7 @@ int read_dict_entries(const char *outdic){
|
||||
verbose(LOGLEVEL_MSG, "Read register %d, value: %d\n", dictionary[i].reg, dictionary[i].value);
|
||||
if(dicentry_descrN(i, buf, BUFSIZ)){
|
||||
++got;
|
||||
fprintf(o, "%s\n", buf);
|
||||
fprintf(o, "%s", buf);
|
||||
}
|
||||
}else verbose(LOGLEVEL_WARN, "Can't read value of register %d\n", dictionary[i].reg);
|
||||
}
|
||||
@@ -396,6 +423,35 @@ int read_dict_entries(const char *outdic){
|
||||
return got;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief write_dict_entries - write all read-write dictionary entries to device
|
||||
* @param errors - out: number of failed writes (may be NULL)
|
||||
* @return number of successfully written entries, or -1 if dictionary isn't opened
|
||||
*
|
||||
* Entries with `readonly != 0` are skipped silently (not counted as errors).
|
||||
*/
|
||||
int write_dict_entries(int *errors){
|
||||
if(errors) *errors = 0;
|
||||
if(!chkdict()) return -1;
|
||||
int written = 0, failed = 0;
|
||||
for(size_t i = 0; i < dictsize; ++i){
|
||||
dicentry_t *e = &dictionary[i];
|
||||
if(e->readonly) continue; // read-only => skip, not an error
|
||||
if(write_entry(e)){
|
||||
verbose(LOGLEVEL_MSG, "Wrote %u to register %u (%s)\n",
|
||||
e->value, e->reg, e->code);
|
||||
++written;
|
||||
}else{
|
||||
// write_entry() already emitted WARNX with details;
|
||||
// add context with mnemonic code
|
||||
verbose(LOGLEVEL_WARN, "Can't write %u to register %u (%s)\n",
|
||||
e->value, e->reg, e->code);
|
||||
++failed;
|
||||
}
|
||||
}
|
||||
if(errors) *errors = failed;
|
||||
return written;
|
||||
}
|
||||
|
||||
/********** Aliases **********/
|
||||
|
||||
@@ -415,14 +471,15 @@ static int sortalias(const void *l, const void *r){
|
||||
}
|
||||
|
||||
// open file with aliases and fill structure
|
||||
int openaliases(const char *filename){
|
||||
closealiases();
|
||||
bool openaliases(const char *filename){
|
||||
if(!filename) return false;
|
||||
FILE *f = fopen(filename, "r");
|
||||
if(!f){
|
||||
WARN("Can't open %s", filename);
|
||||
return FALSE;
|
||||
return false;
|
||||
}
|
||||
int retcode = TRUE;
|
||||
closealiases();
|
||||
int retcode = true;
|
||||
size_t linesz = BUFSIZ, asz = 0;
|
||||
char *line = MALLOC(char, linesz);
|
||||
while(1){
|
||||
@@ -457,7 +514,7 @@ int openaliases(const char *filename){
|
||||
alias_t *newaliases = realloc(aliases, sizeof(alias_t) * newsize);
|
||||
if(!newaliases){
|
||||
WARNX("Can't allocate memory for aliases list");
|
||||
retcode = FALSE;
|
||||
retcode = false;
|
||||
--aliasessize;
|
||||
goto ret;
|
||||
}
|
||||
@@ -471,7 +528,7 @@ int openaliases(const char *filename){
|
||||
else cur->help = NULL;
|
||||
DBG("Read alias; name='%s', expr='%s', comment='%s'", cur->name, cur->expr, cur->help);
|
||||
}
|
||||
if(aliasessize == 0) retcode = FALSE;
|
||||
if(aliasessize == 0) retcode = false;
|
||||
qsort(aliases, aliasessize, sizeof(alias_t), sortalias);
|
||||
ret:
|
||||
fclose(f);
|
||||
@@ -480,12 +537,12 @@ ret:
|
||||
}
|
||||
|
||||
// return TRUE if aliases list inited
|
||||
int chkaliases(){
|
||||
bool chkaliases(){
|
||||
if(aliasessize < 1){
|
||||
WARNX("Init aliases first");
|
||||
return FALSE;
|
||||
return false;
|
||||
}
|
||||
return TRUE;
|
||||
return true;
|
||||
}
|
||||
// remove aliases
|
||||
void closealiases(){
|
||||
|
||||
Reference in New Issue
Block a user