diff options
Diffstat (limited to 'tbl_qcmd.h')
-rw-r--r-- | tbl_qcmd.h | 267 |
1 files changed, 267 insertions, 0 deletions
diff --git a/tbl_qcmd.h b/tbl_qcmd.h new file mode 100644 index 0000000..4052812 --- /dev/null +++ b/tbl_qcmd.h @@ -0,0 +1,267 @@ +#ifndef __TBL_QCMD_H +#define __TBL_QCMD_H + +#include <stdio.h> +#include <stdlib.h> +#include <mqueue.h> +#include <string.h> + +#define EXEC_CURRENT 0 //executes in current thread +#define EXEC_SEPERATE 1 //executes in seperate thread + +/* +Fields: +*/ +typedef struct tble_exec +{ + int id; + char *name; /*module name, can be used just for note*/ + char *cmd; /*command name*/ + int type; +} tble_exec; + +/* +list of registed commands +Fields: +*/ +typedef struct tbl_exec +{ + int size; + int max_size; + tble_exec **reg_cmd; +} tbl_exec; + +#define QCMD_NONE 0 //nothing happening with this command +#define QCMD_TIMEOUT 1 //command running to long without result +#define QCMD_DONE 2 //comand finsihed execution +#define QCMD_RUNNING 3 //command still in execution state +#define QCMD_PREPARE 4 //something need to be done before make it to run + +/* +Fields: +*/ +typedef struct tble_qcmd +{ + int id; + int timestamp; //when command started to execute + int state; //command execution state + int timeout; //timeout time for command + char *cmd; //cmd name + char *param; //params + mqd_t out_q; + mqd_t in_q; + int idx_exec; //table unique id of executor, check if still there ;] +} tble_qcmd; + +/* +table of commands executing +Fields: +*/ +typedef struct tbl_qcmd +{ + int size; + int max_size; + tble_qcmd **cmd; /*list of command executing and waiting for replay*/ +} tbl_qcmd; + +/* +use this structure to give params to command execution thread +Fields: +*/ +typedef struct tble_cmd_param +{ + int id; + mqd_t out_q; + char *cmd; + char *param; +} tble_cmd_param; + +/* +Fields: +*/ +typedef struct tble_cmd_resp +{ + int id; + int type; + int ret_code; + char *resp; +} tble_cmd_resp; + +/* +create exec command +Input: +Output: + !NULL - if everything whent ok + NULL - if there was some kind of mistake +*/ +tble_exec *tbl_exec_c(); + +/* +create array where to put commands +Input: + size - size of array that will be created +Output: + !NULL - if everything whent ok + NULL - if there was some kind of mistake +*/ +tbl_exec *tbl_exec_list_c(int size); + +/* +add new command to array +Input: + tbl - table of executed commands + cmd - command that shoule be added +Output: + 0 - if everything whent ok + -1 - if there was some kind of mistake +*/ +int tbl_exec_add(tbl_exec *tbl, tble_exec *cmd); + +/* +search for command in da list +Input: + tbl - table of executed commands + cmd - command name to search in the list +Output: + >=0 - if everything whent ok + -1 - if there was some kind of mistake +*/ +int tbl_exec_in_s(tbl_exec *tbl, char *cmd); + +/* +if command is found then return pointer to it +if you passs pointer further then if its will be freed +or deleted from the list your programm will segfault +so enjoy ;] +Input: + tbl - table of executed commands + cmd - command name to search in the list +Output: + >=0 - if everything whent ok + -1 - if there was some kind of mistake +*/ +tble_exec *tbl_exec_search_cmd(tbl_exec *tbl, char *cmd); + +/* +print all entries in table +Input: + tbl - table of executed commands +Output: + >=0 - if everything whent ok + -1 - if there was some kind of mistake +*/ +int tbl_exec_print(tbl_exec *tbl); + + +/* +create qcmd commands +Input: +Output: + !NULL - if everything whent ok + NULL - if there was some kind of mistake +*/ +tble_qcmd *tbl_qcmd_c(); + +/* +create array for executed commands +Input: +Output: + !NULL - if everything whent ok + NULL - if there was some kind of mistake +*/ +tbl_qcmd* tbl_qcmd_list_c(int size); + +/* +add info from executor to command +Input: + tbl - array of executed commands + cmd - command to be added to execution list +Output: + 0 - if everything whent ok + -1 - if there was some kind of mistake +*/ +int tbl_qcmd_set_exec(tble_qcmd *qcmd, tble_exec *exec); + +/* +add command to executed array +Input: + tbl - array of executed commands + cmd - command to be added to execution list +Output: + 0 - if everything whent ok + -1 - if there was some kind of mistake +*/ +int tbl_qcmd_add(tbl_qcmd *tbl, tble_qcmd *qcmd); +/* +check if there any event on commands +Input: + +Output: + >=0 - if everything whent ok, returns index in the table (ret-1) + -1 - if there was some kind of mistake, or nothing happened +*/ +int tbl_qcmd_chk(tbl_qcmd *tbl); + +/* +delete command from the list +Input: + tbl - array of executed commands + idx - index in array that should be removed +Output: + 0 - if everything whent ok + -1 - if there was some kind of mistake +*/ +int tbl_qcmd_del(tbl_qcmd *tbl, int idx); + +/* +if there is response then try to match response and return code to +executed command requester +Input: + tbl - array of executed cmds + resp - response recieved, match it to table values +Output: + 0 - if everything whent ok + -1 - if there was some kind of mistake +*/ +int tbl_qcmd_resp(tbl_qcmd *tbl, tble_cmd_resp *resp); + +/* +create cmd param +Input: +Output: + !NULL - if everything whent ok + NULL - if there was some kind of mistake +*/ +tble_cmd_param* tbl_cmd_param_c(); + +/* +create cmd param +Input: +Output: + 0 - if everything whent ok + -1 - if there was some kind of mistake +*/ +int tbl_cmd_param_set(tble_cmd_param *cmd_param, tble_qcmd *cmd); + +char *alloc_new_str_s(char *str, size_t size); +char *alloc_new_str(char *str); + +/*mvar things*/ +//shitty macro +#define MVAR_ALLOC_STRC(VNAME,VTYPE,VRET)\ +VTYPE *VNAME;\ +VNAME=malloc(sizeof(VTYPE));\ +if ((VNAME)==NULL){\ +VRET\ +}\ +memset(VNAME,0,sizeof(VTYPE)); + +#define MVAR_ALLOC_ARR(VNAME,VTYPE,VSZ,VRET)\ +VTYPE *VNAME;\ +VNAME=malloc(sizeof(VTYPE)*(VSZ));\ +if ((VNAME)==NULL){\ +VRET\ +}\ +memset(VNAME,0,sizeof(VTYPE)*(VSZ)); + +#endif |