-
Notifications
You must be signed in to change notification settings - Fork 2
/
file_fifo.c
executable file
·564 lines (474 loc) · 13.3 KB
/
file_fifo.c
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
/***************************************************************************************************
File FIFO
This thread opens files to read input commands, write output responses, and write error outputs for
control via external applications.
***************************************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <stdbool.h>
#include "thermistor.h"
#include "tlc1543.h"
#include "servo.h"
#include "app.h"
#include "main.h"
#include "rev_history.h"
#include "file_fifo.h"
typedef struct
{
char* cmd;
char* description;
} cmd_type;
/* **** Global Data **** */
/* *** Global Variables *** */
typedef enum
{
CMD_GET_VERSION = 0,
CMD_EXIT,
CMD_SET_CAB_TEMP,
CMD_GET_CAB_TEMP,
CMD_SET_PROBE_TARGET_TEMP,
CMD_GET_PROBE_TARGET_TEMP,
CMD_GET_PROBE_TEMP,
CMD_GET_ALL_TEMPS,
CMD_SET_CHANNEL_NAME,
CMD_GET_CHANNEL_NAMES,
CMD_SET_P_GAIN,
CMD_SET_I_GAIN,
CMD_SET_I_LIMIT,
CMD_GET_P_GAIN,
CMD_GET_I_GAIN,
CMD_GET_I_LIMIT,
NBR_OF_CMDS,
CMD_UNKNOWN=-1
} cmd_id_type;
static const cmd_type cmd_list[NBR_OF_CMDS] = {
{ "VER", "Gets the firmware versioninformation\n" },
{ "EXIT", "Closes the program\n" },
{ "SET_CABINET_TARGET", "Set the target cabinet temperature\n" },
{ "GET_CABINET_TARGET", "Gets the target cabinet temperature\n" },
{ "SET_PROBE_TARGET", "Set the target channel temperature\n" },
{ "GET_PROBE_TARGET", "Gets the target channel temperature\n" },
{ "GET_PROBE_TEMP", "Gets the current probe temperature\n" },
{ "GET_ALL_TEMPS", "Gets the temperature of all sensors\n" },
{ "SET_CHANNEL_NAME", "Set the name of the target channel\n" },
{ "GET_CHANNEL_NAMES", "Gets the names of all probe channels\n" },
{ "SET_KP", "Set proportaional gain\n" },
{ "SET_KI", "Set integral gain\n" },
{ "SET_KL", "Set integral limit\n" },
{ "GET_KP", "Gets the proportional gain\n" },
{ "GET_KI", "Gets the integral gain\n" },
{ "GET_KL", "Gets the integral windup limit\n" },
};
/* **** Global Variables *** */
// Pointer to the shared data
shared_data_type* gp_shared_data;
// File handlers for the named pipes
static FILE *g_fifo_output;
static FILE *g_fifo_input;
static FILE *g_fifo_errout;
// Buffer for reading files
static char g_in_buffer[1024]; // Buffer for holding incoming data
static char g_out_buffer[1024]; // Buffer for holding outgoing data
static char g_err_buffer[1024]; // Buffer for holding output error
static int g_write_data_bytes = 0;
static int g_write_error_data_bytes = 0;
static void File_Fifo_Process_Cmd( char* cmd_buff );
static void File_Fifo_Signal_Handler( int signalnum );
// Command processing functions
static void File_Fifo_Set_Cook_Temp( char* tokens );
static void File_Fifo_Set_P_Gain( char* tokens );
static void File_Fifo_Set_I_Gain( char* tokens );
static void File_Fifo_Set_I_Limit( char* tokens );
static void File_Fifo_Get_Probe_Temp( char* tokens );
static void File_Fifo_Get_Cook_Temp( void );
static void File_Fifo_Get_P_Gain( void );
static void File_Fifo_Get_I_Gain( void );
static void File_Fifo_Get_I_Limit( void );
static void File_Fifo_Get_All_Probe_Temps( void );
static void File_Fifo_Get_Probe_Temp( char* tokens );
static void File_Fifo_Set_Channel_Name( char* tokens );
static void File_Fifo_Get_Channel_Names( void );
/***************************************************************************************************
Sets up the named pipes for receiving commands, sending responses, and reporting errors.
Returns -1 on error opening files
1 on successful opening of files
***************************************************************************************************/
int File_Fifo_Init( void )
{
int result = 0;
// attempt to remove the files in case they are already present
remove(SMPI_INPFIFO);
remove(SMPI_OUTFIFO);
remove(SMPI_ERRFIFO);
// Create the fifos
if (mkfifo(SMPI_INPFIFO, 0777) || mkfifo(SMPI_OUTFIFO, 0777) || mkfifo(SMPI_ERRFIFO, 0777))
result = -1;
else
result = 1;
return result;
}
void File_Fifo_Service_Input( void *shared_data_address )
{
int rd_index;
char ch;
gp_shared_data = (shared_data_type*)shared_data_address;
signal(SIGINT, File_Fifo_Signal_Handler);
signal(SIGPIPE, SIG_IGN);
while (1)
{
g_fifo_input = fopen(SMPI_INPFIFO, "r");
rd_index = 0;
g_in_buffer[0] = 0;
do {
ch = fgetc(g_fifo_input);
g_in_buffer[rd_index++] = ch;
} while ((ch != '\n') && (rd_index < sizeof(g_in_buffer)));
File_Fifo_Process_Cmd( g_in_buffer );
fclose(g_fifo_input);
}
}
void File_Fifo_Service_Output( void *shared_data_address )
{
int i;
gp_shared_data = (shared_data_type*)shared_data_address;
signal(SIGINT, File_Fifo_Signal_Handler);
signal(SIGPIPE, SIG_IGN);
while (1)
{
g_fifo_output = fopen(SMPI_OUTFIFO, "w");
while (g_fifo_output)
{
if (g_write_data_bytes > 0)
{
i = 0;
while (i < g_write_data_bytes)
fputc(g_out_buffer[i++], g_fifo_output);
fflush(g_fifo_output);
g_write_data_bytes = 0;
}
else
usleep(1000);
}
}
}
void File_Fifo_Respond( char* pResponse )
{
strcpy(g_out_buffer, pResponse);
g_write_data_bytes = strlen(pResponse);
}
void File_Fifo_Report_Error( char* pError )
{
strcpy( g_err_buffer, pError );
g_write_error_data_bytes = strlen(pError);
}
/***************************************************************************************************
This function accepts a command buffer containing ascii data. It tokenizes the string then
identifies the command passed to it.
***************************************************************************************************/
static void File_Fifo_Process_Cmd( char* cmd_buff )
{
char* cmd;
char* tokens;
int cmd_nbr;
char buffer[50];
cmd = strtok(cmd_buff, " \n"); // cmd will now point to the CMD in the input buffer
if (cmd)
{
tokens = strtok(NULL, " \n");
for (cmd_nbr = 0; cmd_nbr < NBR_OF_CMDS; cmd_nbr++)
{
if (strcasecmp(cmd, cmd_list[cmd_nbr].cmd) == 0)
break;
}
}
else
cmd_nbr = -1;
switch (cmd_nbr)
{
default:
case CMD_UNKNOWN:
File_Fifo_Respond("-1\n");
break;
case CMD_GET_VERSION:
sprintf(buffer, "1\n%d.%d.%d\n", FIRMWARE_MAJOR, FIRMWARE_MINOR, FIRMWARE_REVISION);
File_Fifo_Respond(buffer);
break;
case CMD_EXIT:
case CMD_SET_PROBE_TARGET_TEMP:
case CMD_GET_PROBE_TARGET_TEMP:
File_Fifo_Respond("-1\n");
File_Fifo_Report_Error("Cmd not implemented\n");
break;
case CMD_SET_CHANNEL_NAME:
File_Fifo_Set_Channel_Name( tokens );
break;
case CMD_GET_CHANNEL_NAMES:
File_Fifo_Get_Channel_Names();
break;
case CMD_SET_CAB_TEMP:
File_Fifo_Set_Cook_Temp( tokens );
break;
case CMD_GET_CAB_TEMP:
File_Fifo_Get_Cook_Temp();
break;
case CMD_GET_PROBE_TEMP:
File_Fifo_Get_Probe_Temp( tokens );
break;
case CMD_GET_ALL_TEMPS:
File_Fifo_Get_All_Probe_Temps();
break;
case CMD_SET_P_GAIN:
File_Fifo_Set_P_Gain( tokens );
break;
case CMD_SET_I_GAIN:
File_Fifo_Set_I_Gain( tokens );
break;
case CMD_SET_I_LIMIT:
File_Fifo_Set_I_Limit( tokens );
break;
case CMD_GET_P_GAIN:
File_Fifo_Get_P_Gain();
break;
case CMD_GET_I_GAIN:
File_Fifo_Get_I_Gain();
break;
case CMD_GET_I_LIMIT:
File_Fifo_Get_I_Limit();
break;
}
}
/***************************************************************************************************
This function takes a pointer to an ASCII floating point value and sets the cabinet temperature to
that value.
***************************************************************************************************/
static void File_Fifo_Set_Cook_Temp( char* tokens )
{
float new_temp = 0.0;
char buffer[50];
int response;
if (tokens != NULL)
{
sscanf(tokens, "%f", &new_temp);
App_Set_Cabinet_Setpoint( new_temp );
response = 0;
}
else
{
strcpy(buffer, "Error: Tokens - Set Cook Temp\n");
File_Fifo_Report_Error(buffer);
response = -1;
}
sprintf(buffer, "%d\n", response);
File_Fifo_Respond(buffer);
}
/***************************************************************************************************
This function takes a pointer to an ASCII floating point value and sets the P gain to that value.
***************************************************************************************************/
static void File_Fifo_Set_P_Gain( char* tokens )
{
float new_gain = 0.0;
char buffer[50];
int response;
if (tokens != NULL)
{
sscanf(tokens, "%f", &new_gain);
App_Set_Kp( new_gain );
response = 0;
}
else
{
strcpy(buffer, "Error: Tokens - Set P Gain\n");
File_Fifo_Report_Error(buffer);
response = -1;
}
sprintf(buffer, "%d\n", response);
File_Fifo_Respond(buffer);
}
/***************************************************************************************************
This function takes a pointer to an ASCII floating point value and sets the I gain to that value.
***************************************************************************************************/
static void File_Fifo_Set_I_Gain( char* tokens )
{
float new_gain = 0.0;
char buffer[50];
int response;
if (tokens != NULL)
{
sscanf(tokens, "%f", &new_gain);
App_Set_Ki( new_gain );
response = 0;
}
else
{
strcpy(buffer, "Error: Tokens - Set I Gain\n");
File_Fifo_Report_Error(buffer);
response = -1;
}
sprintf(buffer, "%d\n", response);
File_Fifo_Respond(buffer);
}
/***************************************************************************************************
This function takes a pointer to an ASCII floating point value and sets the I windup limit to that
value.
***************************************************************************************************/
static void File_Fifo_Set_I_Limit( char* tokens )
{
float new_gain = 0.0;
char buffer[50];
int response;
if (tokens != NULL)
{
sscanf(tokens, "%f", &new_gain);
App_Set_Kl( new_gain );
response = 0;
}
else
{
strcpy(buffer, "Error: Tokens - Set I Limit\n");
File_Fifo_Report_Error(buffer);
response = -1;
}
sprintf(buffer, "%d\n", response);
File_Fifo_Respond(buffer);
}
static void File_Fifo_Get_Cook_Temp( void )
{
char buffer[50];
float temperature;
temperature = App_Get_Cabinet_Setpoint();
sprintf(buffer, "1\n%4.2f\n", temperature);
File_Fifo_Respond(buffer);
}
static void File_Fifo_Get_P_Gain( void )
{
char buffer[50];
float gain;
gain = App_Get_Kp();
sprintf(buffer, "1\n%4.2f\n", gain);
File_Fifo_Respond(buffer);
}
static void File_Fifo_Get_I_Gain( void )
{
char buffer[50];
float gain;
gain = App_Get_Ki();
sprintf(buffer, "1\n%4.2f\n", gain);
File_Fifo_Respond(buffer);
}
static void File_Fifo_Get_I_Limit( void )
{
char buffer[50];
float limit;
limit = App_Get_Kl();
sprintf(buffer, "1\n%4.2f\n", limit);
File_Fifo_Respond(buffer);
}
static void File_Fifo_Get_Probe_Temp( char* tokens )
{
char buffer[50];
float temperature;
int response;
int channel;
if (tokens != NULL)
{
sscanf(tokens, "%d", &channel);
if ((channel >= 0) && (channel < NBR_OF_THERMISTORS))
{
pthread_mutex_lock(&mutex);
temperature = gp_shared_data->temp_deg_f[channel];
pthread_mutex_unlock(&mutex);
response = 1;
}
else
{
response = -1;
}
}
else
{
strcpy(buffer, "Error: Tokens - Get Probe Temp\n");
File_Fifo_Report_Error(buffer);
response = -1;
}
if (response == 1)
sprintf(buffer, "%d\n%4.2f\n", response, temperature);
else
sprintf(buffer, "%d\n", response);
File_Fifo_Respond(buffer);
}
static void File_Fifo_Set_Channel_Name( char* tokens )
{
int response;
int channel;
char buffer[128];
char name[128];
if (tokens != NULL)
{
sscanf(tokens, "%d", &channel);
tokens = strtok(NULL, " \n");
if (strlen(tokens) < (sizeof(name)-1))
strcpy(name, tokens);
App_Set_Channel_Name( channel, name );
response = 0;
}
else
{
response = -1;
}
sprintf(buffer, "%d\n", response);
File_Fifo_Respond(buffer);
}
static void File_Fifo_Get_Channel_Names( void )
{
int i;
char buffer[1024] = { 0 };
char* name;
strcpy( buffer, "1\n" );
for (i = 0; i < NBR_OF_THERMISTORS; i++)
{
name = App_Get_Channel_Name( i );
sprintf(buffer, "%s%s,", buffer, name);
}
// convert the ',' at the end of the string to a '\n'
buffer[strlen(buffer)-1] = '\n';
File_Fifo_Respond(buffer);
}
static void File_Fifo_Get_All_Probe_Temps( void )
{
char buffer[150];
int channel;
strcpy(buffer, "1\n");
pthread_mutex_lock(&mutex);
for (channel = 0; channel < NBR_OF_THERMISTORS; channel++)
sprintf(buffer, "%s%4.2f,", buffer, gp_shared_data->temp_deg_f[channel]);
pthread_mutex_unlock(&mutex);
// Change the ',' at the end of the string to a '\n'
buffer[strlen(buffer)-1] = '\n';
File_Fifo_Respond(buffer);
}
/***************************************************************************************************
When Ctrl+C is pressed to end the process, this function will catch the signal and close the file
stream before exiting the thread
***************************************************************************************************/
static void File_Fifo_Signal_Handler( int signalnum )
{
switch (signalnum)
{
case SIGINT:
if (g_fifo_input)
fclose(g_fifo_input);
if (g_fifo_output)
fclose(g_fifo_output);
if (g_fifo_errout)
fclose(g_fifo_errout);
exit(signalnum);
break;
}
}
/* *** End of File *** */