Rev 1225 | Rev 1227 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1225 | giacomo | 1 | /* Event Generator |
2 | * |
||
3 | * Giacomo Guidi |
||
4 | */ |
||
5 | |||
6 | #include <stdio.h> |
||
7 | #include <stdlib.h> |
||
8 | |||
9 | #include "event_gen.h" |
||
10 | |||
1226 | giacomo | 11 | #define ACT_HEADER "act_header.h" |
12 | #define EXEC_HEADER "exec_header.h" |
||
13 | |||
1225 | giacomo | 14 | #define ACT_SINGLE 0 |
15 | #define ACT_PERIODIC 1 |
||
16 | #define ACT_MEAN 2 |
||
17 | #define ACT_INVALID 3 |
||
18 | |||
1226 | giacomo | 19 | #define EXEC_CONST 0 |
20 | #define EXEC_MEAN 1 |
||
21 | #define EXEC_INVALID 2 |
||
22 | |||
1225 | giacomo | 23 | int select_act_type(void) |
24 | { |
||
25 | char act_type[10]; |
||
26 | |||
27 | printf("\nInsert the activation type\n"); |
||
28 | printf(" S - Single\n"); |
||
29 | printf(" P - Periodic\n"); |
||
30 | printf(" M - Sporadic with constant distribution (Mean,Delta)\n"); |
||
31 | printf("> "); |
||
32 | scanf("%s",act_type); |
||
33 | |||
34 | switch (act_type[0]) { |
||
35 | |||
36 | case 's': |
||
37 | case 'S': |
||
38 | |||
39 | return ACT_SINGLE; |
||
40 | break; |
||
41 | |||
42 | case 'p': |
||
43 | case 'P': |
||
44 | |||
45 | return ACT_PERIODIC; |
||
46 | break; |
||
47 | |||
48 | case 'm': |
||
49 | case 'M': |
||
50 | |||
51 | return ACT_MEAN; |
||
52 | break; |
||
53 | |||
54 | default: |
||
55 | |||
56 | return ACT_INVALID; |
||
57 | |||
58 | } |
||
59 | |||
60 | return ACT_INVALID; |
||
61 | |||
62 | } |
||
63 | |||
1226 | giacomo | 64 | int select_exec_type(void) |
65 | { |
||
66 | char exec_type[10]; |
||
67 | |||
68 | printf("\nInsert the execution time\n"); |
||
69 | printf(" C - Const Exec Time\n"); |
||
70 | printf(" M - Mean with constant distribution (Mean,Delta)\n"); |
||
71 | printf("> "); |
||
72 | scanf("%s",exec_type); |
||
73 | |||
74 | switch (exec_type[0]) { |
||
75 | |||
76 | case 'c': |
||
77 | case 'C': |
||
78 | |||
79 | return EXEC_CONST; |
||
80 | break; |
||
81 | |||
82 | case 'm': |
||
83 | case 'M': |
||
84 | |||
85 | return EXEC_MEAN; |
||
86 | break; |
||
87 | |||
88 | default: |
||
89 | |||
90 | return EXEC_INVALID; |
||
91 | |||
92 | } |
||
93 | |||
94 | return EXEC_INVALID; |
||
95 | |||
96 | } |
||
97 | |||
1225 | giacomo | 98 | int write_single_act(char *task_name) |
99 | { |
||
100 | |||
101 | FILE *file_act_header; |
||
102 | int time_usec; |
||
103 | |||
1226 | giacomo | 104 | file_act_header = fopen(ACT_HEADER,"a+"); |
1225 | giacomo | 105 | if (file_act_header == NULL) return 1; |
106 | |||
107 | printf("\nInsert single activation time [us]\n"); |
||
108 | printf("> "); |
||
109 | scanf("%d",&time_usec); |
||
110 | |||
111 | fprintf(file_act_header,"\nstruct timespec %s_act_event[1] = {%d,%d};\n", |
||
112 | task_name,time_usec/1000000,time_usec%1000000); |
||
113 | |||
114 | fclose(file_act_header); |
||
115 | |||
116 | return 0; |
||
117 | |||
118 | } |
||
119 | |||
120 | int write_periodic_act(char *task_name) |
||
121 | { |
||
122 | |||
123 | FILE *file_act_header; |
||
124 | int i,first_time_usec,per_time_usec,act_num; |
||
125 | long long tot_time_usec; |
||
126 | |||
1226 | giacomo | 127 | file_act_header = fopen(ACT_HEADER,"a+"); |
1225 | giacomo | 128 | if (file_act_header == NULL) return 1; |
129 | |||
130 | printf("\nInsert the number of activations\n"); |
||
131 | printf("> "); |
||
132 | scanf("%d",&act_num); |
||
133 | printf("Insert first activation time [us]\n"); |
||
134 | printf("> "); |
||
135 | scanf("%d",&first_time_usec); |
||
136 | printf("Insert periodic activation time [us]\n"); |
||
137 | printf("> "); |
||
138 | scanf("%d",&per_time_usec); |
||
139 | |||
140 | fprintf(file_act_header,"\nstruct timespec %s_act_event[%d] = {{%d,%d},\n", |
||
141 | task_name,act_num,first_time_usec/1000000,first_time_usec%1000000); |
||
142 | |||
143 | tot_time_usec = first_time_usec; |
||
144 | for (i=0;i<act_num-1;i++) { |
||
145 | tot_time_usec += per_time_usec; |
||
146 | fprintf(file_act_header," {%d,%d},\n", |
||
147 | (int)tot_time_usec/1000000,(int)tot_time_usec%1000000); |
||
148 | } |
||
149 | |||
150 | fprintf(file_act_header,"};\n"); |
||
151 | |||
152 | fclose(file_act_header); |
||
153 | |||
154 | return 0; |
||
155 | |||
156 | } |
||
157 | |||
158 | int write_mean_act(char *task_name) |
||
159 | { |
||
160 | |||
161 | FILE *file_act_header; |
||
162 | int i,first_time_usec,mean_time_usec,act_num,delta_time_usec; |
||
163 | long long tot_time_usec; |
||
164 | |||
1226 | giacomo | 165 | file_act_header = fopen(ACT_HEADER,"a+"); |
1225 | giacomo | 166 | if (file_act_header == NULL) return 1; |
167 | |||
168 | printf("\nInsert the number of activations\n"); |
||
169 | printf("> "); |
||
170 | scanf("%d",&act_num); |
||
171 | printf("Insert first activation time [us]\n"); |
||
172 | printf("> "); |
||
173 | scanf("%d",&first_time_usec); |
||
174 | printf("Insert mean peridic time [us]\n"); |
||
175 | printf("> "); |
||
176 | scanf("%d",&mean_time_usec); |
||
177 | printf("Insert delta time [us]\n"); |
||
178 | printf("> "); |
||
179 | scanf("%d",&delta_time_usec); |
||
180 | |||
181 | fprintf(file_act_header,"\nstruct timespec %s_act_event[%d] = {{%d,%d},\n", |
||
182 | task_name,act_num,first_time_usec/1000000,first_time_usec%1000000); |
||
183 | |||
184 | tot_time_usec = first_time_usec; |
||
185 | for (i=0;i<act_num-1;i++) { |
||
186 | tot_time_usec += mean_time_usec + random() % delta_time_usec - delta_time_usec/2; |
||
187 | fprintf(file_act_header," {%d,%d},\n", |
||
188 | (int)tot_time_usec/1000000,(int)tot_time_usec%1000000); |
||
189 | } |
||
190 | |||
191 | fprintf(file_act_header,"};\n"); |
||
192 | |||
193 | fclose(file_act_header); |
||
194 | |||
195 | return 0; |
||
196 | |||
197 | } |
||
198 | |||
1226 | giacomo | 199 | int write_exec_const(char *task_name) |
200 | { |
||
201 | |||
202 | return 0; |
||
203 | |||
204 | } |
||
205 | |||
206 | int write_exec_mean(char *task_name) |
||
207 | { |
||
208 | |||
209 | return 0; |
||
210 | |||
211 | } |
||
212 | |||
1225 | giacomo | 213 | int main(void) { |
214 | |||
215 | char task_name[100]; |
||
1226 | giacomo | 216 | char act_type,exec_type; |
1225 | giacomo | 217 | int err; |
218 | |||
219 | printf("\nEvent Generator\n\n"); |
||
220 | |||
221 | srandom(12354132); |
||
222 | |||
1226 | giacomo | 223 | while(1) { |
1225 | giacomo | 224 | |
1226 | giacomo | 225 | printf("Insert the task name\n"); |
226 | printf("Write \"q\" to quit program\n"); |
||
227 | printf("> "); |
||
228 | scanf("%s",task_name); |
||
1225 | giacomo | 229 | |
1226 | giacomo | 230 | if (strlen(task_name) == 1 && task_name[0] == 'q') exit(0); |
231 | |||
232 | while((act_type = select_act_type()) == ACT_INVALID) { |
||
233 | printf("Error: Invalid Act Type\n"); |
||
234 | } |
||
235 | |||
236 | switch (act_type) { |
||
237 | case ACT_SINGLE: |
||
238 | err = write_single_act(task_name); |
||
239 | if (err != 0) { |
||
240 | printf("Error writing activation header\n"); |
||
241 | exit(1); |
||
242 | } |
||
243 | break; |
||
244 | case ACT_PERIODIC: |
||
245 | err = write_periodic_act(task_name); |
||
246 | if (err != 0) { |
||
247 | printf("Error writing activation header\n"); |
||
248 | exit(1); |
||
249 | } |
||
250 | break; |
||
251 | case ACT_MEAN: |
||
252 | err = write_mean_act(task_name); |
||
253 | if (err != 0) { |
||
254 | printf("Error writing activation header\n"); |
||
255 | exit(1); |
||
256 | } |
||
257 | break; |
||
258 | } |
||
259 | |||
260 | while((exec_type = select_exec_type()) == EXEC_INVALID) { |
||
261 | printf("Error: Invalid Exec Type\n"); |
||
262 | } |
||
263 | |||
264 | switch (exec_type) { |
||
265 | case EXEC_CONST: |
||
266 | err = write_exec_const(task_name); |
||
267 | if (err != 0) { |
||
268 | printf("Error writing exec header\n"); |
||
269 | exit(1); |
||
270 | } |
||
271 | break; |
||
272 | case EXEC_MEAN: |
||
273 | err = write_exec_mean(task_name); |
||
274 | if (err != 0) { |
||
275 | printf("Error writing exec header\n"); |
||
276 | exit(1); |
||
277 | } |
||
278 | break; |
||
279 | } |
||
280 | |||
1225 | giacomo | 281 | } |
282 | |||
283 | return 0; |
||
284 | |||
285 | } |