-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.c
346 lines (298 loc) · 9.33 KB
/
main.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#include "registers.h"
#include "config.h"
#include "base64.h"
#include "spi.h"
#include "gpio.h"
#include "time_util.h"
#include "connector.h"
#include "sx1276.h"
#include <protobuf-c.h>
#include <sys/time.h>
#include <signal.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
//uint32_t rx_rcv = 0;
uint32_t rx_ok = 0;
/*uint32_t rx_rcv;
uint32_t rx_ok;
uint32_t rx_bad;
uint32_t rx_nocrc;
uint32_t pkt_fwd;
uint32_t network_byte;
uint32_t payload_byte;
uint32_t dgram_sent;
uint32_t ack_rcv;
uint32_t pull_sent;
uint32_t ack_rcv;
uint32_t dgram_rcv;
uint32_t network_byte;
uint32_t payload_byte;
uint32_t tx_ok;
uint32_t tx_fail;
uint32_t tx_requested = 0;
uint32_t tx_rejected_collision_packet = 0;
uint32_t tx_rejected_collision_beacon = 0;
uint32_t tx_rejected_too_late = 0;
uint32_t tx_rejected_too_early = 0;
uint32_t beacon_queued = 0;
uint32_t beacon_sent = 0;
uint32_t beacon_rejected = 0;*/
//uint32_t cp_up_pkt_fwd = 0;
int irqPin, rstPin, intPin;
const double mhz = (double)freq/1000000;
TTN *ttn;
int running = 1;
//void die(const char *s);
void setup_lora();
void send_status();
void receive_packet(void);
void init(void);
void send_ack(const uint8_t *message);
void print_downlink(Router__DownlinkMessage *msg, void *arg);
void reset_radio(void);
void cleanup(void);
/*void die(const char *s){
perror(s);
exit(1);
}*/
void stop(int sig){
signal(SIGINT, NULL);
running = 0;
}
void reset_radio(){
gpio_write(rstPin, 0);
delay(100);
gpio_write(rstPin, 1);
delay(100);
}
void setup_lora(){
reset_radio();
uint8_t version = get_version();
printf("Transceiver version 0x%02X, ", version);
if(version != SX1276_ID){
puts("Unrecognized transceiver");
exit(1);
}
puts("SX1276 detected\n");
sleep();
set_frequency(freq);
set_sync_word(0x34); //LoRaWAN public sync word
if(sf == 11 || sf == 12){
spi_write_reg(REG_MODEM_CONFIG3, 0x0C);
} else {
spi_write_reg(REG_MODEM_CONFIG3, 0x04);
}
spi_write_reg(REG_MODEM_CONFIG1, 0x72);
spi_write_reg(REG_MODEM_CONFIG2, (sf << 4) | 0x04);
if(sf == 10 || sf == 11 || sf == 12){
spi_write_reg(REG_SYMB_TIMEOUT_LSB, 0x05);
} else {
spi_write_reg(REG_SYMB_TIMEOUT_LSB, 0x08);
}
spi_write_reg(REG_MAX_PAYLOAD_LENGTH, 0x80);
spi_write_reg(REG_PAYLOAD_LENGTH, PAYLOAD_LENGTH);
spi_write_reg(REG_HOP_PERIOD, 0xFF);
spi_write_reg(REG_FIFO_ADDR_PTR, spi_read_reg(REG_FIFO_RX_BASE_AD));
//set Continous Receive Mode
spi_write_reg(REG_LNA, LNA_MAX_GAIN);
spi_write_reg(REG_OPMODE, MODE_RX_CONTINUOUS);
}
void print_downlink(Router__DownlinkMessage *msg, void *arg){
//if(!msg->payload.len){ return; }
printf("down: have %zu bytes downlink\n", msg->payload.len);
if(msg->protocol_configuration->protocol_case == PROTOCOL__TX_CONFIGURATION__PROTOCOL_LORAWAN){
Lorawan__TxConfiguration *lora = msg->protocol_configuration->lorawan;
printf("down: modulation: %d, data rate: %s, bit rate: %d, coding rate: "
"%s, fcnt: %d\n",
lora->modulation, lora->data_rate, lora->bit_rate,
lora->coding_rate, lora->f_cnt);
Gateway__TxConfiguration *gtw = msg->gateway_configuration;
printf("down: timestamp: %d, rf chain: %d, frequency: %lu, power: %d, "
"polarization inversion: %d, frequency deviation: %d\n",
gtw->timestamp, gtw->rf_chain, gtw->frequency, gtw->power,
gtw->polarization_inversion, gtw->frequency_deviation);
} else {
printf("down: invalid protocol %d\n", msg->protocol_configuration->protocol_case);
}
}
void send_status(){
//char stat_timestamp[24];
//time_t t = time(NULL);
//strftime(stat_timestamp, sizeof(stat_timestamp), "%F %T %Z", gmtime(&t));
//printf("stat update: %s", stat_timestamp);
if(rx_ok == 0){
printf(" no packet received yet\n");
} else {
printf(" %u packet%sreceived\n", rx_ok, rx_ok > 1 ? "s " : " ");
}
//int json_strlen = strlen(json_str);
//build and send message
//memcpy(status_pkt + HEADER_SIZE, json_str, json_strlen);
//send_udp(servers[i], status_pkt, HEADER_SIZE + json_strlen);
//free json memory
//json_decref(root);
// Send gateway status
Gateway__Status status = GATEWAY__STATUS__INIT;
status.time = 555;
//status.gateway_trusted
//status.boot_time
//status.ip
status.platform = platform;
status.contact_email = email;
status.description = description;
status.frequency_plan = frequency_plan;
//status.location
//status.rx_in
status.rx_ok = rx_ok;
//status.tx_in
//status.tx_ok
//status.lm_ok
//status.lm_st
//status.lm_nw
//status.l_pps
Gateway__Status__OSMetrics os = GATEWAY__STATUS__OSMETRICS__INIT;
os.cpu_percentage = 0;
os.memory_percentage = 0;
os.temperature = 999;
status.os = &os;
int err = ttngwc_send_status(ttn, &status);
if(err){
printf("status: send failed: %d\n", err);
} else {
printf("status: sent with time %d\n", 555);
}
}
void send_ack(const uint8_t *message){
char pkt[ACK_HEADER_SIZE];
pkt[0] = PROTOCOL_VERSION;
pkt[1] = message[1];
pkt[2] = message[2];
pkt[3] = PKT_PUSH_ACK;
write_data(pkt, ACK_HEADER_SIZE);
}
void receive_packet(void){
//wait_irq();
if(!gpio_read(irqPin)){
return;
}
uint8_t message[MAX_MESSAGE_SIZE];
uint8_t length;
if(!read_data(message, &length)){
return;
}
++rx_ok;
//if confirmed
send_ack(message);
puts("ack sent\n");
long int SNR;
uint8_t value = spi_read_reg(REG_PKT_SNR_VALUE);
//the SNR sign bit is 1
if(value & 0x80){
//invert and divide by 4
value = ((~value + 1) & 0xFF) >> 2;
SNR = -value;
} else {
// Divide by 4
SNR = (value & 0xFF) >> 2;
}
const int rssicorr = 157;
int rssi = spi_read_reg(REG_PKT_RSSI) - rssicorr;
printf("Packet RSSI: %d, ", rssi);
printf("RSSI: %d, ", spi_read_reg(REG_RSSI) - rssicorr);
printf("SNR: %li, ", SNR);
printf("Length: %hhu Message:'", length);
for(int i = 0; i < length; ++i){
printf("%c", isprint(message[i]) ? message[i] : '.');
}
printf("'\n");
//uint32_t start_time = get_time();
//encode payload
char b64[BASE64_MAX_LENGTH];
bin_to_b64(message, length, b64, BASE64_MAX_LENGTH);
//char datr[] = "SFxxBWxxx";
//snprintf(datr, strlen(datr) + 1, "SF%hhuBW%hu", sf, bw);
// Enter the payload
//unsigned char buf[] = {0x1, 0x2, 0x3, 0x4, 0x5};
//uint8_t buf[] = "hello world";
Router__UplinkMessage up = ROUTER__UPLINK_MESSAGE__INIT;
up.payload.len = length;
up.payload.data = message;
// Set protocol metadata
Protocol__RxMetadata protocol = PROTOCOL__RX_METADATA__INIT;
protocol.protocol_case = PROTOCOL__RX_METADATA__PROTOCOL_LORAWAN;
Lorawan__Metadata lorawan = LORAWAN__METADATA__INIT;
lorawan.modulation = LORAWAN__MODULATION__LORA;
lorawan.data_rate = data_rate;
lorawan.coding_rate = coding_rate;
lorawan.f_cnt = 0; //frame count
protocol.lorawan = &lorawan;
up.protocol_metadata = &protocol;
// Set gateway metadata
Gateway__RxMetadata gateway = GATEWAY__RX_METADATA__INIT;
gateway.timestamp = 555;
//RF chain where the gateway received the message
gateway.rf_chain = 0;
gateway.frequency = freq;
up.gateway_metadata = &gateway;
// Send uplink message
int err = ttngwc_send_uplink(ttn, &up);
if(err){
printf("up: send failed: %d\n", err);
} else {
printf("up: sent with timestamp %d\n", 555);
}
}
void init(void){
//set up hardware
////setup_interrupt("rising"); //gpio4, input
irqPin = gpio_init("/sys/class/gpio/gpio4/value", O_RDONLY);//gpio 4, input
rstPin = gpio_init("/sys/class/gpio/gpio3/value", O_WRONLY);//gpio 3, output
spi_init("/dev/spidev0.0", O_RDWR);
//setup LoRa
setup_lora();
signal(SIGINT, stop);
signal(SIGTERM, stop);
// Initialize the TTN gateway
ttngwc_init(&ttn, gateway_id, &print_downlink, NULL);
if(!ttn){
printf("failed to initialize TTN gateway\n");
return;
}
// Connect to the broker
printf("connecting...\n");
int err = ttngwc_connect(ttn, server_hostname, server_port, gateway_key);
if(err != 0){
printf("connect failed: %d\n", err);
ttngwc_cleanup(ttn);
exit(0);
}
printf("connected\n");
printf("Listening at SF%i on %.6lf Mhz.\n", sf, mhz);
printf("-----------------------------------\n");
}
void cleanup(void){
ttngwc_cleanup(ttn);
ttngwc_disconnect(ttn);
puts("\ndisconnecting...");
}
int main(){
init();
send_status();
uint32_t lasttime = seconds();
while(running){
receive_packet();
//int nowseconds = seconds();
//if(nowseconds - lasttime >= update_interval){
// lasttime = nowseconds;
// send_status();
// rx_rcv = 0;
// rx_ok = 0;
// cp_up_pkt_fwd = 0;
//}
}
cleanup();
}