This repository has been archived by the owner on Jun 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
status.c
177 lines (146 loc) · 3.97 KB
/
status.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
// $Id: status.c,v 1.9 2018/12/04 04:33:18 karn Exp $
// Thread to emit receiver status packets
// Copyright 2018 Phil Karn, KA9Q
#define _GNU_SOURCE 1
#include <assert.h>
#include <stdlib.h>
#include <unistd.h>
#include <limits.h>
#include <pthread.h>
#include <string.h>
#if defined(linux)
#include <bsd/string.h>
#endif
#include <math.h>
#include <complex.h>
#undef I
#include <sys/time.h>
#include <ncurses.h>
#include <ctype.h>
#include <sys/socket.h>
#include <netdb.h>
#include "misc.h"
#include "dsp.h"
#include "radio.h"
#include "filter.h"
#include "multicast.h"
#include "status.h"
// Encode 64-bit integer, byte swapped, leading zeroes suppressed
int encode_int64(unsigned char **buf,enum status_type type,uint64_t x){
unsigned char *cp = *buf;
*cp++ = type;
int len = sizeof(x);
while(len > 0 && (x & 0xff00000000000000LL) == 0){
x <<= 8;
len--;
}
*cp++ = len;
for(int i=0; i<len; i++){
*cp++ = x >> 56;
x <<= 8;
}
*buf = cp;
return 2+len;
}
// Single null type byte means end of list
int encode_eol(unsigned char **buf){
unsigned char *bp = *buf;
*bp++ = EOL;
*buf = bp;
return 1;
}
int encode_byte(unsigned char **buf,enum status_type type,unsigned char x){
unsigned char *cp = *buf;
*cp++ = type;
*cp++ = sizeof(x);
*cp++ = x;
*buf = cp;
return 2+sizeof(x);
}
int encode_int16(unsigned char **buf,enum status_type type,uint16_t x){
return encode_int64(buf,type,(uint64_t)x);
}
int encode_int32(unsigned char **buf,enum status_type type,uint32_t x){
return encode_int64(buf,type,(uint64_t)x);
}
int encode_int(unsigned char **buf,enum status_type type,int x){
return encode_int64(buf,type,(int)x);
}
int encode_float(unsigned char **buf,enum status_type type,float x){
uint32_t data;
memcpy(&data,&x,sizeof(data));
return encode_int32(buf,type,(uint64_t)data);
}
int encode_double(unsigned char **buf,enum status_type type,double x){
uint64_t data;
memcpy(&data,&x,sizeof(data));
return encode_int64(buf,type,data);
}
// Encode byte string without byte swapping
int encode_string(unsigned char **bp,enum status_type type,void *buf,int buflen){
unsigned char *cp = *bp;
*cp++ = type;
if(buflen > 255)
buflen = 255;
*cp++ = buflen;
memcpy(cp,buf,buflen);
*bp = cp + buflen;
return 2+buflen;
}
// Decode byte string without byte swapping
void *decode_string(unsigned char **bp,void *buf,int buflen){
unsigned char *cp = *bp;
int len = *cp++;
memcpy(buf,cp,min(len,buflen));
*bp = cp + len;
return buf;
}
// Decode encoded variable-length UNSIGNED integers
// At entry, *bp -> length field (not type!)
// Works for byte, short, long, long long
uint64_t decode_int(unsigned char *cp,int len){
uint64_t result = 0;
// cp now points to beginning of abbreviated int
// Byte swap as we accumulate
while(len-- > 0)
result = (result << 8) | *cp++;
return result;
}
float decode_float(unsigned char *cp,int len){
if(len == 8)
return (float)decode_double(cp,len);
uint32_t result = decode_int(cp,len);
return *(float *)&result;
}
double decode_double(unsigned char *cp,int len){
if(len == 4)
return (double)decode_float(cp,len);
uint64_t result = decode_int(cp,len);
return *(double *)&result;
}
int compact_packet(struct state *s,unsigned char *pkt,int force){
unsigned char *input = pkt;
unsigned char *output = pkt;
*output++ = *input++; // command/response byte (don't really have to copy)
// Read new packet into table, copying elements that have changed to output
while(1){
int type = *input++;
if(type == EOL)
break;
int len = *input++;
assert(type >= 0 && type < 256);
assert(len >= 0 && len < 256);
if(force || s[type].length != len || memcmp(s[type].value,input,len) != 0){
s[type].length = len;
memcpy(s[type].value,input,len);
*output++ = type;
*output++ = len;
assert(output <= input);
memmove(output,input,len);
output += len;
}
input += len;
}
*output++ = EOL;
return output - pkt;
}