Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP - depends on #44 - Global debugging settings atomization #48

Closed
wants to merge 10 commits into from
296 changes: 167 additions & 129 deletions scantool/diag.h

Large diffs are not rendered by default.

156 changes: 84 additions & 72 deletions scantool/diag_cfg.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,67 +7,68 @@
*
*/


#include "diag.h"
#include "diag_cfg.h"
#include "diag_err.h"
#include "diag_tty.h" //for diag_tty_getportlist()
#include "diag_tty.h" //for diag_tty_getportlist()

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static const char tty_descr[]="Serial/tty port, such as \"/dev/ttyS0\" or \"\\\\.\\COM11\"";
static const char tty_sn[]="port"; /** tty cfg shortname */
static const char tty_def[]="/dev/null"; /** last resort fallback */
static const char tty_descr[] =
"Serial/tty port, such as \"/dev/ttyS0\" or \"\\\\.\\COM11\"";
static const char tty_sn[] = "port"; /** tty cfg shortname */
static const char tty_def[] = "/dev/null"; /** last resort fallback */

/* top decls */
void optarray_clear(struct cfgi *cfgp);


//Optional func to refresh opt[] and numopts (for tty, J2534, etc), doesn't change *val
void diag_cfg_refresh(struct cfgi *cfgp) {
// Optional func to refresh opt[] and numopts (for tty, J2534, etc), doesn't change *val
void
diag_cfg_refresh(struct cfgi *cfgp) {
if (cfgp->refresh) {
cfgp->refresh(cfgp);
}
return;
}

//Optional: func to reset *val to default; doesn't call refresh()
void diag_cfg_reset(struct cfgi *cfgp) {
// Optional: func to reset *val to default; doesn't call refresh()
void
diag_cfg_reset(struct cfgi *cfgp) {
if (cfgp->reset) {
cfgp->reset(cfgp);
}
return;
}


int diag_cfg_setstr(struct cfgi *cfgp, const char *str) {
int
diag_cfg_setstr(struct cfgi *cfgp, const char *str) {
size_t slen;
int rv;

if (cfgp->type != CFGT_STR) {
return diag_iseterr(DIAG_ERR_BADCFG);
}

slen=strlen(str);
slen = strlen(str);
if (cfgp->dyn_val && (cfgp->val.str != NULL)) {
free(cfgp->val.str);
cfgp->val.str = NULL;
}
rv = diag_malloc(&cfgp->val.str, slen+1);
rv = diag_malloc(&cfgp->val.str, slen + 1);
if (rv != 0) {
return diag_iseterr(rv);
}
cfgp->dyn_val = 1; //need to free
cfgp->dyn_val = 1; // need to free
strcpy(cfgp->val.str, str);
return 0;

}

//set config value for a BOOL param
int diag_cfg_setbool(struct cfgi *cfgp, bool val) {
// set config value for a BOOL param
int
diag_cfg_setbool(struct cfgi *cfgp, bool val) {
if (cfgp->type == CFGT_BOOL) {
cfgp->val.b = val;
return 0;
Expand All @@ -76,24 +77,27 @@ int diag_cfg_setbool(struct cfgi *cfgp, bool val) {
}

//
int diag_cfg_setu8(struct cfgi *cfgp, uint8_t val) {
int
diag_cfg_setu8(struct cfgi *cfgp, uint8_t val) {
if (cfgp->type == CFGT_U8) {
cfgp->val.u8 = val;
return 0;
}
return diag_iseterr(DIAG_ERR_BADCFG);
}

int diag_cfg_setint(struct cfgi *cfgp, int val) {
int
diag_cfg_setint(struct cfgi *cfgp, int val) {
if (cfgp->type == CFGT_INT) {
cfgp->val.i = val;
return 0;
}
return diag_iseterr(DIAG_ERR_BADCFG);
}

//set config value to one of the predefined options. Ret 0 if ok
int diag_cfg_setopt(struct cfgi *cfgp, int optid) {
// set config value to one of the predefined options. Ret 0 if ok
int
diag_cfg_setopt(struct cfgi *cfgp, int optid) {
if (optid > (cfgp->numopts - 1)) {
return diag_iseterr(DIAG_ERR_BADCFG);
}
Expand All @@ -108,7 +112,7 @@ int diag_cfg_setopt(struct cfgi *cfgp, int optid) {
case CFGT_INT:
cfgp->val.i = optid;
break;
case CFGT_U8: //these don't really make sense
case CFGT_U8: // these don't really make sense
case CFGT_BOOL:
default:
return diag_iseterr(DIAG_ERR_BADCFG);
Expand All @@ -117,29 +121,30 @@ int diag_cfg_setopt(struct cfgi *cfgp, int optid) {
return 0;
}

//directly set param value (caller knows correct type and handles mem management, etc) BAD
//void diag_cfg_setraw(struct cfgi *cfgp, void *val) {}
// directly set param value (caller knows correct type and handles mem management, etc) BAD
// void diag_cfg_setraw(struct cfgi *cfgp, void *val) {}

//get param value, as new string to be free'd by caller.
//for u8 / int types, sprintf with %X and %d formatters respectively
char *diag_cfg_getstr(struct cfgi *cfgp) {
// get param value, as new string to be free'd by caller.
// for u8 / int types, sprintf with %X and %d formatters respectively
char *
diag_cfg_getstr(struct cfgi *cfgp) {
char *str;
const char *fmt;
size_t len;
int rv;
switch (cfgp->type) {
case CFGT_U8:
len=5;
fmt="0x%02X";
len = 5;
fmt = "0x%02X";
break;
case CFGT_INT:
//handle 5 digits.
len=6;
fmt="%5d";
// handle 5 digits.
len = 6;
fmt = "%5d";
break;
case CFGT_STR:
len=strlen(cfgp->val.str)+1;
fmt="%s";
len = strlen(cfgp->val.str) + 1;
fmt = "%s";
break;
default:
return diag_pseterr(DIAG_ERR_BADCFG);
Expand All @@ -155,8 +160,9 @@ char *diag_cfg_getstr(struct cfgi *cfgp) {
return str;
}

//free contents of *cfgp (prior to free'ing the struct itself, for instance)
void diag_cfg_clear(struct cfgi *cfgp) {
// free contents of *cfgp (prior to free'ing the struct itself, for instance)
void
diag_cfg_clear(struct cfgi *cfgp) {
/* For now, handles only CFGT_STR types */
if (cfgp->type != CFGT_STR) {
return;
Expand All @@ -165,33 +171,34 @@ void diag_cfg_clear(struct cfgi *cfgp) {
free(cfgp->val.str);
}
cfgp->dyn_val = 0;
cfgp->val.str=NULL;
cfgp->val.str = NULL;

optarray_clear(cfgp);

if (cfgp->dyn_dval && (cfgp->dval.str != NULL)) {
free(cfgp->dval.str);
}
cfgp->dyn_dval = 0;
cfgp->dval.str=NULL;
cfgp->dval.str = NULL;
}


/*** struct management funcs ***/

//clear / free ->opt[] array
void optarray_clear(struct cfgi *cfgp) {
// clear / free ->opt[] array
void
optarray_clear(struct cfgi *cfgp) {
if (cfgp->dyn_opt && (cfgp->opt != NULL)) {
/* Need to free every string, and the array of string ptrs */
strlist_free(cfgp->opt, cfgp->numopts);
}
cfgp->dyn_opt = 0;
cfgp->opt=NULL;
cfgp->opt = NULL;
cfgp->numopts = 0;
}

//stock reset() function
void std_reset(struct cfgi *cfgp) {
// stock reset() function
void
std_reset(struct cfgi *cfgp) {
switch (cfgp->type) {
case CFGT_U8:
cfgp->val.b = cfgp->dval.b;
Expand All @@ -217,13 +224,13 @@ void std_reset(struct cfgi *cfgp) {
}
}


/** Refresh list of known ports
*
* Keep current port; update default.
* If no ports are found, changes nothing
*/
void tty_refresh(struct cfgi *cfgp) {
void
tty_refresh(struct cfgi *cfgp) {

optarray_clear(cfgp);

Expand All @@ -236,18 +243,20 @@ void tty_refresh(struct cfgi *cfgp) {

/* Update default port */
cfgp->dyn_dval = 1;
if (diag_malloc(&cfgp->dval.str, strlen(cfgp->opt[0])+1)) {
if (diag_malloc(&cfgp->dval.str, strlen(cfgp->opt[0]) + 1)) {
optarray_clear(cfgp);
return;
}
strcpy(cfgp->dval.str, cfgp->opt[0]); //we just used strlen; strcpy is just as dangerous...
strcpy(cfgp->dval.str, cfgp->opt[0]); // we just used strlen; strcpy is just as
// dangerous...
cfgp->dyn_opt = 1;

return;
}

//new TTY / serial port config item
int diag_cfgn_tty(struct cfgi *cfgp) {
// new TTY / serial port config item
int
diag_cfgn_tty(struct cfgi *cfgp) {
int rv = diag_cfgn_str(cfgp, tty_def, tty_descr, tty_sn);
if (rv != 0) {
return rv;
Expand All @@ -261,72 +270,76 @@ int diag_cfgn_tty(struct cfgi *cfgp) {

/** generic types **/

//ordinary int param using caller's val, and def as default value for reset().
//Doesn't fill descr and shortname
int diag_cfgn_int(struct cfgi *cfgp, int val, int def) {
cfgp->dyn_val = 0; //caller-supplied
// ordinary int param using caller's val, and def as default value for reset().
// Doesn't fill descr and shortname
int
diag_cfgn_int(struct cfgi *cfgp, int val, int def) {
cfgp->dyn_val = 0; // caller-supplied
cfgp->dyn_dval = 0;
cfgp->type = CFGT_INT;
cfgp->numopts=0;
cfgp->numopts = 0;
cfgp->dval.i = def;
cfgp->val.i = val;
cfgp->refresh = NULL;
cfgp->reset = &std_reset;
return 0;
}

//ordinary u8 param (copy of _int code) using caller's &val, and *dev as default value for reset().
//Doesn't fill descr and shortname
int diag_cfgn_u8(struct cfgi *cfgp, uint8_t val, uint8_t def) {
cfgp->dyn_val = 0; //managed by caller
// ordinary u8 param (copy of _int code) using caller's &val, and *dev as default value for
// reset(). Doesn't fill descr and shortname
int
diag_cfgn_u8(struct cfgi *cfgp, uint8_t val, uint8_t def) {
cfgp->dyn_val = 0; // managed by caller
cfgp->dyn_dval = 0;
cfgp->type = CFGT_U8;
cfgp->numopts=0;
cfgp->numopts = 0;
cfgp->val.u8 = val;
cfgp->dval.u8 = def;
cfgp->refresh = NULL;
cfgp->reset = &std_reset;
return 0;
}

//ordinary bool (copy of _int code)
int diag_cfgn_bool(struct cfgi *cfgp, bool val, bool def) {
cfgp->dyn_val = 0; //managed by caller
// ordinary bool (copy of _int code)
int
diag_cfgn_bool(struct cfgi *cfgp, bool val, bool def) {
cfgp->dyn_val = 0; // managed by caller
cfgp->dyn_dval = 0;
cfgp->type = CFGT_BOOL;
cfgp->numopts=0;
cfgp->numopts = 0;
cfgp->val.b = val;
cfgp->dval.b = def;
cfgp->refresh = NULL;
cfgp->reset = &std_reset;
return 0;
}

//ordinary string, copies *def for its default value; sets descr and shortname ptrs
int diag_cfgn_str(struct cfgi *cfgp, const char *def, const char *descr, const char *sn) {
// ordinary string, copies *def for its default value; sets descr and shortname ptrs
int
diag_cfgn_str(struct cfgi *cfgp, const char *def, const char *descr, const char *sn) {
char *val, *dval;
int rv;

assert(def && descr && sn);

cfgp->type = CFGT_STR;
rv = diag_malloc(&dval, strlen(def)+1);
rv = diag_malloc(&dval, strlen(def) + 1);
if (rv != 0) {
return diag_iseterr(rv);
}
rv = diag_malloc(&val, strlen(def)+1);
rv = diag_malloc(&val, strlen(def) + 1);
if (rv != 0) {
free(dval);
return diag_iseterr(rv);
}

cfgp->dval.str = dval;
cfgp->dyn_dval = 1;
strcpy(dval, def); //danger
strcpy(dval, def); // danger

cfgp->val.str = val;
cfgp->dyn_val = 1;
strcpy(val, def); //danger
strcpy(val, def); // danger

cfgp->descr = descr;
cfgp->shortname = sn;
Expand All @@ -335,4 +348,3 @@ int diag_cfgn_str(struct cfgi *cfgp, const char *def, const char *descr, const c
cfgp->reset = &std_reset;
return 0;
}

Loading