| 1 | #ifndef DIAG_CFG_H |
| 2 | #define DIAG_CFG_H |
| 3 | |
| 4 | /* freediag |
| 5 | * API for configurable items |
| 6 | * |
| 7 | * (c) fenugrec 2015 |
| 8 | * GPLv3 |
| 9 | * |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #include <stdint.h> |
| 14 | #include <stdbool.h> |
| 15 | |
| 16 | /* This struct describes one configurable param, |
| 17 | * including description,type, *value, etc. |
| 18 | * |
| 19 | * Typically an L0 driver will alloc a linked-list of (struct cfgi) items |
| 20 | */ |
| 21 | struct cfgi { |
| 22 | const char *descr; //description; not mallocd |
| 23 | const char *shortname; //for CLI use, must be unique in any array of cfgi. Not mallocd. |
| 24 | int type; //indicate type of *val |
| 25 | #define CFGT_U8 1 //uint8_t, *val is a static buf |
| 26 | #define CFGT_INT 2 //int, *val is a static buf |
| 27 | #define CFGT_STR 3 //const char *; generic string; re-alloc/manage *val every time |
| 28 | //#define CFGT_TTY 4 //const char *; special string ? |
| 29 | //#define CFGT_ENUM 5 //redundant with ((type==CFGT_INT) && (numopts >0) )? |
| 30 | #define CFGT_BOOL 6 |
| 31 | union uval { |
| 32 | bool b; |
| 33 | uint8_t u8; |
| 34 | int i; |
| 35 | char *str; |
| 36 | } val; //Actual value of parameter |
| 37 | |
| 38 | union udval { |
| 39 | bool b; |
| 40 | uint8_t u8; |
| 41 | int i; |
| 42 | char *str; |
| 43 | } dval; //default value; used for reset() |
| 44 | |
| 45 | /* these flags determine who owns & manages *val.str and *dval.str : |
| 46 | * if set, diag_cfg_* functions take care of alloc / free calls. |
| 47 | * if clear, caller must manage the allocation, diag_cfg_* functions |
| 48 | * will not keep track of val.str/dval.str and may rewrite them. |
| 49 | */ |
| 50 | bool dyn_val; |
| 51 | bool dyn_dval; |
| 52 | |
| 53 | int numopts; //if > 0 : number of predefined string options / enum values. If ==0 : value set directly. |
| 54 | char **opt; //description for each predefined option, i.e. numopts==1 means |
| 55 | // *opt[0]=="option_id 0 descr", etc. |
| 56 | // given { const char opt0_descr[]="option_id 0 descr"; const char *opt0_table[]={opt0_descr, opt1_descr}; } |
| 57 | // use { cfg_param->opt = opt0_table; } |
| 58 | bool dyn_opt; //if *opt[] must be free'd (recursively) |
| 59 | |
| 60 | |
| 61 | struct cfgi *next; //single-linked list |
| 62 | |
| 63 | /* do not call these directly */ |
| 64 | void (*refresh)(struct cfgi *_this); //called by diag_cfg_refresh() |
| 65 | // Possible problem with refresh() if numopts>0; and refresh() makes *val invalid / illegal ! |
| 66 | void (*reset)(struct cfgi *_this); //called by diag_cfg_reset() |
| 67 | }; |
| 68 | |
| 69 | /** Refresh opt[] and numopts (for tty, J2534, etc) |
| 70 | * |
| 71 | * Optional implementation; doesn't change *val |
| 72 | */ |
| 73 | void diag_cfg_refresh(struct cfgi *cfgp); |
| 74 | |
| 75 | /** Reset *val to default; doesn't call refresh() |
| 76 | * |
| 77 | * Optional implementation |
| 78 | */ |
| 79 | void diag_cfg_reset(struct cfgi *cfgp); |
| 80 | |
| 81 | //set config value for a param; caller must use the right func ... not super efficient |
| 82 | /** Set value for a CFGT_STR param |
| 83 | * |
| 84 | * @return 0 if ok |
| 85 | * |
| 86 | * Copies contents of *str to a new string |
| 87 | */ |
| 88 | int diag_cfg_setstr(struct cfgi *cfgp, const char *str); |
| 89 | int diag_cfg_setbool(struct cfgi *cfgp, bool val); |
| 90 | int diag_cfg_setu8(struct cfgi *cfgp, uint8_t val); |
| 91 | int diag_cfg_setint(struct cfgi *cfgp, int val); |
| 92 | |
| 93 | //interpret 'str' correctly and set param accordingly |
| 94 | //int diag_cfg_set(struct cfgi *cfgp, const char *str); |
| 95 | |
| 96 | /** set config value to one of the predefined options. |
| 97 | * @return 0 if ok. |
| 98 | * |
| 99 | * Note: optid is 0-based |
| 100 | */ |
| 101 | int diag_cfg_setopt(struct cfgi *cfgp, int optid); |
| 102 | |
| 103 | //directly set param value (caller knows correct type and handles mem management, etc) BAD |
| 104 | //void diag_cfg_setraw(struct cfgi *cfgp, void *val); |
| 105 | |
| 106 | /** get param value: generates new string to be free'd by caller |
| 107 | */ |
| 108 | char *diag_cfg_getstr(struct cfgi *cfgp); |
| 109 | |
| 110 | /** free contents of *cfgp but not the struct itself |
| 111 | */ |
| 112 | void diag_cfg_clear(struct cfgi *cfgp); |
| 113 | |
| 114 | /****** re-usable, typical configurable params ******/ |
| 115 | /* after alloc'ing a new struct cfgi, calling these funcs will prepare the struct */ |
| 116 | /* Ret 0 if ok. Some members of the struct may need to be filled after calling these. */ |
| 117 | |
| 118 | /** new TTY / serial port config item |
| 119 | * @return 0 if ok |
| 120 | */ |
| 121 | int diag_cfgn_tty(struct cfgi *cfgp); |
| 122 | |
| 123 | /** new ordinary int param |
| 124 | * |
| 125 | * @return 0 if ok |
| 126 | * |
| 127 | * Uses caller's val, and def as default value for reset(). |
| 128 | * Doesn't fill descr and shortname |
| 129 | */ |
| 130 | int diag_cfgn_int(struct cfgi *cfgp, int val, int def); |
| 131 | |
| 132 | /** new ordinary u8 param (copy of _int code) |
| 133 | * |
| 134 | * @return 0 if ok |
| 135 | * |
| 136 | * Uses dval as default value for reset(). |
| 137 | * Doesn't fill descr and shortname |
| 138 | */ |
| 139 | int diag_cfgn_u8(struct cfgi *cfgp, uint8_t val, uint8_t def); |
| 140 | |
| 141 | /** new ordinary string param |
| 142 | * |
| 143 | * @return 0 if ok |
| 144 | * |
| 145 | * Copies *def for its default value; sets descr and shortname ptrs |
| 146 | */ |
| 147 | int diag_cfgn_str(struct cfgi *cfgp, const char *def, const char *descr, const char *sn); |
| 148 | |
| 149 | /** new ordinary bool param |
| 150 | * |
| 151 | * @return 0 if ok |
| 152 | * |
| 153 | * (copy of _int code) |
| 154 | */ |
| 155 | int diag_cfgn_bool(struct cfgi *cfgp, bool val, bool def); |
| 156 | |
| 157 | #endif // DIAG_CFG_H |
| 158 | |