-
Notifications
You must be signed in to change notification settings - Fork 0
/
comrecv.c
245 lines (216 loc) · 8.7 KB
/
comrecv.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
/*
* Copyright 2021 Patrik Schindler <[email protected]>.
*
* Licensing terms.
* This 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 3 of the License, or
* (at your option) any later version.
*
* It 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; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* or get it at http://www.gnu.org/licenses/gpl.html
*
* Based on the skeleton "Programming udp sockets in C on Linux
* Silver Moon <[email protected]>
* http://www.binarytides.com/programming-udp-sockets-in-c-on-linux/
*
* Example how to handle Data Queues in C:
* https://www.ibm.com/docs/en/i/7.1?topic=queues-example-in-ile-c-using-data
*/
#include <arpa/inet.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>
#include <sys/signal.h>
#include <time.h>
#include <qp0ztrc.h>
#include <qsnddtaq.h>
#include "comserver.h"
/* -----------------------------------------------------------------------------
* Defines and global vars.
* FIXME: Read this from preferences.
*/
#define PORT 49152 /* The port on which to listen for incoming data */
int exit_flag;
/*------------------------------------------------------------------------------
* Send a message to the job log, and then exit with error.
*/
void die(char *s) {
Qp0zLprintf("%s: %s\n", s, strerror(errno));
exit(1);
}
/*------------------------------------------------------------------------------
* Create Receiver Socket.
*/
int setup_recv_socket(unsigned int port) {
struct sockaddr_in si_me;
int s;
/* create an UDP socket */
if ((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
die("socket");
}
/* zero struct */
memset((char *) &si_me, 0, sizeof(si_me));
/* Setup */
si_me.sin_family = AF_INET;
si_me.sin_port = htons(port);
si_me.sin_addr.s_addr = htonl(INADDR_ANY);
/* bind socket to port */
if ( bind(s, (struct sockaddr*)&si_me, sizeof(si_me) ) == -1) {
die("bind");
}
return(s);
};
/*------------------------------------------------------------------------------
* Comserver gives us data in host byte order (correct for LSB platforms).
*/
unsigned short bswap(unsigned short val) {
return (val << 8) | (val >> 8);
}
/*------------------------------------------------------------------------------
* What to do when we receive a SIGTERM.
*/
void set_exit_flag(int signum) {
exit_flag = 1;
}
/*------------------------------------------------------------------------------
* Main
*/
int main(int argc, char *argv[]) {
struct sigaction termaction;
struct sockaddr_in si_other;
struct timeval stamp;
struct WUT_WRITE_REGISTER pkt, pkt_swp;
char buf[1500], dtaqbuf[33];
int slen = sizeof(si_other), s = 0, r = 0, recv_len;
unsigned int i, sndqbytes, cur_watt;
unsigned int shortlen = sizeof(short), current_regstate = 0;
unsigned long int cur_usecs[12], prev_usecs[12];
unsigned short int prev_state[12];
/* Clean up beforehand. */
for (i=0; i<13; i++) {
prev_state[i] = prev_usecs[i] = prev_state[i] = 0;
}
sndqbytes = sizeof(dtaqbuf) - 1;
/* Setup Receiver Socket. */
s = setup_recv_socket(PORT);
if ( (r=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
die("socket2");
}
/* Create signal handler for SIGTERM. */
memset(&termaction, 0, sizeof(struct sigaction));
termaction.sa_handler = set_exit_flag;
sigaction(SIGTERM, &termaction, NULL);
/* Init variable for timediff first. */
gettimeofday(&stamp, NULL);
/* Keep listening for data */
while( exit_flag == 0 ) {
/* try to receive some data. This is a blocking call. */
/* FIXME: Get rid of GOTO. This is sooo BASIC-like... */
tryagain:
if ((recv_len = recvfrom(s, buf, 1500, 0, (struct sockaddr *) &si_other,
&slen)) == -1) {
if (errno == EINTR) {
goto tryagain;
} else {
die("recvfrom()");
}
}
/* Sort first few bytes into the struct. */
/* FIXME: Do we really need to copy, or can we just set *s? */
memcpy(&pkt.send_sequence, buf, shortlen);
memcpy(&pkt.recv_sequence, buf + shortlen, shortlen);
memcpy(&pkt.payload_type, buf + shortlen * 2, shortlen);
memcpy(&pkt.length, buf + shortlen * 3, shortlen);
memcpy(&pkt.count, buf + shortlen * 4, shortlen);
memcpy(&pkt.data, buf + shortlen * 5, shortlen);
/* Swap bytes and write into secondary buffer */
/* FIXME: Can we unify the above copy with the swapping? */
pkt_swp.send_sequence = bswap(pkt.send_sequence);
pkt_swp.recv_sequence = bswap(pkt.recv_sequence);
pkt_swp.payload_type = bswap(pkt.payload_type);
pkt_swp.length = bswap(pkt.length);
pkt_swp.count = bswap(pkt.count);
pkt_swp.data = bswap(pkt.data);
/* Increment counters in input_ticks. */
for (i=0; i<pkt_swp.count; i++) {
current_regstate = pkt_swp.data + (i * shortlen);
if ( current_regstate > 0 ) {
gettimeofday(&stamp, NULL);
/* Is "our" bit set? */
if ( current_regstate & 2 ) {
/* Only do if previous state was already 0. */
if ( prev_state[1] == 0 ) {
/* Calculate usecs over all from gettimeofday call. */
cur_usecs[1] = stamp.tv_sec * 1000000 + stamp.tv_usec;
/* Calculate current watts from above values . */
cur_watt = 3600000000 / (cur_usecs[1] - prev_usecs[1]);
/* Prevent bogus values. Very arbitrary. */
if ( prev_usecs[1] > 0 && cur_watt < 6000 ) {
/* Put timestamp into buffer. */
strftime(dtaqbuf, 32, "%Y-%m-%d-%H.%M.%S.000000",
localtime(&stamp.tv_sec));
/* Append calculated data to buffer. */
sprintf(dtaqbuf+26, "%02u%04lu", 2, cur_watt);
/* Send buffer to *DTAQ object. */
QSNDDTAQ("COMTMP ", "HAUSAUTO ", sndqbytes,
dtaqbuf);
}
/* Save previous values. */
prev_usecs[1] = cur_usecs[1];
/* Indicate our last state was 1. */
prev_state[1] = 1;
} else {
/* In theory, this should not happen. */
Qp0zLprintf("1 following 1 on Port %d. Lost Packet?\n",
2);
}
} else {
/* Our bit is not set, so reset state. */
prev_state[1] = 0;
}
if ( current_regstate & 1 ) {
if ( prev_state[0] == 0 ) {
cur_usecs[0] = stamp.tv_sec * 1000000 + stamp.tv_usec;
cur_watt = 3600000000 / (cur_usecs[0] - prev_usecs[0]);
if ( prev_usecs[0] > 0 && cur_watt < 6000 ) {
strftime(dtaqbuf, 32, "%Y-%m-%d-%H.%M.%S.000000",
localtime(&stamp.tv_sec));
sprintf(dtaqbuf+26, "%02u%04lu", 1, cur_watt);
QSNDDTAQ("COMTMP ", "HAUSAUTO ", sndqbytes,
dtaqbuf);
}
prev_usecs[0] = cur_usecs[0];
prev_state[0] = 1;
} else {
Qp0zLprintf("1 following 1 on Port %d. Lost Packet?\n",
1);
}
} else {
prev_state[0] = 0;
}
} else {
prev_state[0] = prev_state[1] = 0;
}
}
}
close(s);
return 0;
}
/*------------------------------------------------------------------------------
* vim: ft=c colorcolumn=81 autoindent shiftwidth=4 tabstop=4 expandtab
*/