-
Notifications
You must be signed in to change notification settings - Fork 0
/
ws_receive.c
157 lines (130 loc) · 3.47 KB
/
ws_receive.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
/*
* Copyright (c) 2009 Sven Bachmann (dev@mcbachmann.de)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <ftdi.h>
#include <fcntl.h>
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
#include "ws.h"
/* variables */
extern struct ftdi_context ftdic;
int thread_exit;
pthread_t ws_recv_thread_data;
/* prototypes */
void* ws_recv_thread(void*);
/*
* ws_recv_init
*
* Initialize the receiving thread.
*/
int ws_recv_init(struct receive_s* recv)
{
int ret;
/* clear thread exit condition */
thread_exit = 0;
/* initialize semaphore */
recv->wait = (sem_t *) malloc(sizeof(sem_t));
sem_init(recv->wait, 0, 0);
check_cond(pthread_create(&ws_recv_thread_data, NULL, ws_recv_thread, recv), != 0, return -1);
return 0;
}
/*
* ws_recv_stop
*
* Stop the receiving thread.
*/
void ws_recv_stop(struct receive_s* recv)
{
int ret;
/* send exit signal to thread */
thread_exit = 1;
/* wait for thread to exit */
check_cond(pthread_join(ws_recv_thread_data, NULL), != 0, return);
/* close semaphore */
sem_close(recv->wait);
}
/*
* ws_recv_thread
*
* Receive data from weatherstation.
*/
void* ws_recv_thread(void* recv_void)
{
int ret;
int data_beg = 0;
int data_end = 0;
int data_last = 0;
u_char data;
struct receive_s* recv = recv_void;
/* check for buffer with zero or less length */
check_cond(recv->length, <= 0, goto bye);
/* set buffer position to zero */
recv->pos = 0;
/* exit condition (pos <= lenght to have one byte extra for record end checking) */
while ((!thread_exit) && (recv->pos <= (recv->length))) {
/* receive one byte */
check_ftdi(ftdi_read_data(&ftdic, &data, 1), break);
/* no byte received? sleep a bit */
if (ret == 0) {
if (recv->tries-- > 0) {
usleep(WS_RECV_TIMEOUT);
continue;
}
break;
}
/* bytestream begin */
if ((data == CMD_BEG) && (data_last != CMD_ESCAPE)) {
recv->pos = 0;
data_last = data;
continue;
}
/* command byte */
if ((data == recv->command) && (data_last == CMD_BEG)) {
data_beg = 1;
data_last = 0;
continue;
}
/* bytestream end */
if ((data == CMD_END) && (data_last != CMD_ESCAPE) && (data_beg)) {
data_end = 1;
break;
}
/* escape character handling
*
* if found and escape is already set, take char as it is,
* otherwise only set escape flag
*/
if ((data == CMD_ESCAPE) && (data_last != CMD_ESCAPE)) {
data_last = data;
continue;
}
/* add character to stream if this was not the "end-of-record" check */
if (recv->pos >= recv->length) {
break;
}
recv->buffer[recv->pos++] = data;
}
/* if record end is not reached, invalidate data */
if (!data_end) {
recv->pos = 0;
}
bye:
/* release waiter */
sem_post(recv->wait);
return NULL;
}