-
Notifications
You must be signed in to change notification settings - Fork 0
/
ws_record.c
120 lines (99 loc) · 3.16 KB
/
ws_record.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
/*
* Copyright (c) 2009 Sven Bachmann ([email protected])
*
* 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 <stdio.h>
#include <semaphore.h>
#include <asm/byteorder.h>
#include "ws.h"
/*
* ws_record_current
*
* Read current record.
*/
int ws_record_current(void *record_void)
{
int cnt;
int ret;
struct receive_s recv;
struct record_s *record = record_void;
/* initialize receiving thread */
recv.tries = 10;
recv.buffer = (u_char*) record;
recv.length = sizeof(struct record_s);
recv.command = CMD_RECORD_CURRENT;
check(ws_recv_init(&recv), goto bye);
/* send current record command */
check(ws_send(CMD_RECORD_CURRENT), goto bye_recv);
/* wait for data and end thread */
sem_wait(recv.wait);
/* check if data is valid */
if (recv.tries < 1) {
ret = WS_ERR;
goto bye_recv;
}
/* convert data to CPU endianess */
INFO("Received %i record bytes.\n", recv.pos);
for (cnt = 0; cnt < 9; cnt++) {
record->sensor[cnt].temp = __be16_to_cpu(record->sensor[cnt].temp);
}
record->rain = __be16_to_cpu(record->rain);
record->wind = __be16_to_cpu(record->wind);
record->station.pressure = __be16_to_cpu(record->station.pressure);
record->station.temp = __be16_to_cpu(record->station.temp);
ret = WS_OK;
bye_recv:
ws_recv_stop(&recv);
bye:
return ret;
}
int ws_record_next(void *record_void)
{
int cnt;
int ret;
struct receive_s recv;
struct record_next_s *record = record_void;
/* initialize receiving thread */
recv.tries = 10;
recv.buffer = (u_char*) record;
recv.length = sizeof(struct record_next_s);
recv.command = CMD_RECORD_NEXT;
check(ws_recv_init(&recv), goto bye);
/* send current record command */
check(ws_send(CMD_RECORD_NEXT), goto bye_recv);
/* wait for data and end thread */
sem_wait(recv.wait);
/* check if data is valid */
if (recv.tries < 1) {
ret = WS_ERR;
goto bye_recv;
}
/* convert data to CPU endianess */
INFO("Received %i record bytes.\n", recv.pos);
for (cnt = 0; cnt < 9; cnt++) {
record->sensor[cnt].temp = __be16_to_cpu(record->sensor[cnt].temp);
}
record->time = __be16_to_cpu(record->time);
record->rain = __be16_to_cpu(record->rain);
record->wind = __be16_to_cpu(record->wind);
record->station.pressure = __be16_to_cpu(record->station.pressure);
record->station.temp = __be16_to_cpu(record->station.temp);
ret = WS_OK;
bye_recv:
ws_recv_stop(&recv);
bye:
return ret;
}