forked from cgaebel/pipe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpipe_test.c
More file actions
126 lines (88 loc) · 2.17 KB
/
Copy pathpipe_test.c
File metadata and controls
126 lines (88 loc) · 2.17 KB
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
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include "pipe.h"
#define RUNNING 1
#define STOPPED 0
typedef struct {
pipe_consumer_t* c;
int parent_status;
int telem_count;
} thread_context_t;
typedef struct {
short int Channel;
char Telemetry [257];
} telemetry_t;
void hexdump_buffer (const char * title, const char * buffer, const int len_buffer)
{
int i = 0;
for (i=0; i<len_buffer ;i++)
{
printf ("%s>%02x ",title, buffer[i]);
if (i%16 == 15)
{
printf("\n");
}
}
}
void *ProcessPayload ( void *context)
{
thread_context_t *ctx;
ctx = context;
telemetry_t t;
size_t a_cnt =0;
while (ctx->parent_status == RUNNING || a_cnt > 0)
{
a_cnt= pipe_pop(ctx->c, &t, 1);
if (a_cnt)
{
sleep(1);
hexdump_buffer ("C",t.Telemetry,256);
}
else
{
printf("empty\n");
}
}
pipe_consumer_free(ctx->c);
return NULL;
}
void send_data( pipe_producer_t * p){
telemetry_t t;
char fileName_ssdv [20] = "ssdv.bin";
FILE* file_ssdv = fopen(fileName_ssdv, "rb");
unsigned int i = 0;
printf ("Size of tx_packet = %u\n", sizeof(t));
while (fread(t.Telemetry,256,1,file_ssdv) && (i < 100))
{
//sleep(1);
t.Telemetry[256]='\0';
hexdump_buffer ("Producer",t.Telemetry,256);
pipe_push(p, &t, 1);
i++;
}
fclose(file_ssdv);
}
int main (void)
{
pipe_t* pipe = pipe_new(sizeof(telemetry_t), 256);
pipe_producer_t* p = pipe_producer_new(pipe);
pipe_consumer_t* c = pipe_consumer_new(pipe);
pipe_free(pipe);
pthread_t process_thread;
thread_context_t ctx;
ctx.c = c;
ctx.parent_status = RUNNING;
int rc = 0;
rc = pthread_create(&process_thread, NULL, ProcessPayload, (void *)&ctx);
if (rc){
printf("ERROR; return code from pthread_create() is %d\n", rc);
return(-1);
}
send_data(p);
pipe_producer_free(p);
ctx.parent_status = STOPPED;
pthread_join(process_thread, NULL);
return (0);
}