summaryrefslogtreecommitdiff
path: root/tbl_qcmd.h
blob: 34851173d7d9133539760f7b20206d8a63f04f1d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
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