-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathhpr_gwmp_worker.erl
565 lines (517 loc) · 19.7 KB
/
hpr_gwmp_worker.erl
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
%%%-------------------------------------------------------------------
%%% @author jonathanruttenberg
%%% @copyright (C) 2022, Nova Labs Inc.
%%% @doc
%%%
%%% @end
%%% Created : 08. Aug 2022 3:24 PM
%%%-------------------------------------------------------------------
-module(hpr_gwmp_worker).
-author("jonathanruttenberg").
-behaviour(gen_server).
-include("semtech_udp.hrl").
-include_lib("kernel/include/inet.hrl").
%% ------------------------------------------------------------------
%% API Function Exports
%% ------------------------------------------------------------------
-export([
start_link/1,
push_data/4
]).
%% ------------------------------------------------------------------
%% gen_server Function Exports
%% ------------------------------------------------------------------
-export([
init/1,
handle_call/3,
handle_cast/2,
handle_info/2,
terminate/2
]).
-define(SERVER, ?MODULE).
-define(CLEANUP, cleanup).
-ifdef(TEST).
-define(CLEANUP_TIME, timer:seconds(1)).
-define(MONITOR_STREAM_TIMEOUT, timer:seconds(1)).
-define(MONITOR_STREAM_MAX_ATTEMPT, 1).
-else.
-define(CLEANUP_TIME, timer:seconds(30)).
-define(MONITOR_STREAM_TIMEOUT, timer:seconds(5)).
-define(MONITOR_STREAM_MAX_ATTEMPT, 10).
-endif.
-type pull_data_map() :: #{
socket_dest() => acknowledged | #{timer_ref := reference(), token := binary()}
}.
-type socket_address() :: inet:socket_address() | inet:hostname().
-type socket_port() :: inet:port_number().
-type socket_dest() :: {socket_address(), socket_port()}.
-record(state, {
pubkeybin :: libp2p_crypto:pubkey_bin(),
socket :: gen_udp:socket(),
push_data = #{} :: #{binary() => {binary(), reference()}},
pull_data = #{} :: pull_data_map(),
pull_data_timer :: non_neg_integer(),
addr_resolutions = #{} :: #{socket_dest() => socket_dest()}
}).
%% ------------------------------------------------------------------
%% API Function Definitions
%% ------------------------------------------------------------------
start_link(Args) ->
gen_server:start_link(?MODULE, Args, []).
-spec push_data(
WorkerPid :: pid(),
PacketUp :: hpr_packet_up:packet(),
SocketDest :: socket_dest(),
GatewayLocation :: hpr_gateway_location:loc()
) -> ok | {error, any()}.
push_data(WorkerPid, PacketUp, SocketDest, GatewayLocation) ->
gen_server:cast(
WorkerPid,
{push_data, PacketUp, SocketDest, erlang:system_time(millisecond), GatewayLocation}
).
%% ------------------------------------------------------------------
%% gen_server Function Definitions
%% ------------------------------------------------------------------
init(#{pubkeybin := PubKeyBin} = Args) ->
process_flag(trap_exit, true),
lager:info("~p init with ~p", [?SERVER, Args]),
PullDataTimer = maps:get(pull_data_timer, Args, ?PULL_DATA_TIMER),
lager:md([
{packet_gateway, hpr_utils:gateway_name(PubKeyBin)},
{gateway_mac, hpr_utils:gateway_mac(PubKeyBin)},
{pubkey, libp2p_crypto:bin_to_b58(PubKeyBin)}
]),
{ok, Socket} = gen_udp:open(0, [binary, {active, true}]),
_ = erlang:send_after(500, self(), {monitor_stream, 0}),
%% NOTE: Pull data is sent at the first push_data to
%% initiate the connection and allow downlinks to start
%% flowing.
{ok, #state{
pubkeybin = PubKeyBin,
socket = Socket,
pull_data_timer = PullDataTimer
}}.
-spec handle_call(Msg, _From, #state{}) -> {stop, {unimplemented_call, Msg}, #state{}}.
handle_call(Msg, _From, State) ->
{stop, {unimplemented_call, Msg}, State}.
handle_cast(
{push_data, PacketUp, SocketDest, Timestamp, GatewayLocation},
#state{
push_data = PushData,
socket = Socket
} =
State0
) ->
ok = hpr_packet_up:md(PacketUp),
{Token, Payload} = packet_up_to_push_data(PacketUp, Timestamp, GatewayLocation),
State = maybe_send_pull_data(SocketDest, State0),
{_Reply, TimerRef} = send_push_data(Token, Payload, Socket, SocketDest),
NewPushData = maps:put(Token, {Payload, TimerRef}, PushData),
{noreply, State#state{
push_data = NewPushData
}};
handle_cast(_Msg, State) ->
{noreply, State}.
handle_info({monitor_stream, AttemptCnt}, #state{pubkeybin = PubKeyBin} = State) ->
MD = [{attempt, AttemptCnt}],
case {AttemptCnt, hpr_packet_router_service:locate(PubKeyBin)} of
{_, {ok, Pid}} ->
lager:info(MD, "monitor stream success"),
_ = erlang:monitor(process, Pid, [{tag, {'DOWN', PubKeyBin}}]),
{noreply, State};
{?MONITOR_STREAM_MAX_ATTEMPT, _} ->
lager:info(MD, "monitor stream failed at max attempt"),
{stop, max_monitor_attempt, State};
{_, {error, not_found}} ->
lager:info(MD, "monitor stream failed"),
erlang:send_after(?MONITOR_STREAM_TIMEOUT, self(), {monitor_stream, AttemptCnt + 1}),
{noreply, State}
end;
handle_info(
{udp, Socket, Address, Port, Data},
#state{socket = Socket} = State
) ->
try handle_udp(Data, {Address, Port}, State) of
{noreply, _} = NoReply -> NoReply
catch
_E:_R ->
lager:error("failed to handle UDP packet ~p/~p", [_E, _R]),
{noreply, State}
end;
handle_info(
{?PUSH_DATA_TICK, Token},
#state{push_data = PushData} = State
) ->
case maps:get(Token, PushData, undefined) of
undefined ->
{noreply, State};
{_Data, _} ->
lager:debug("got push data timeout ~p, ignoring lack of ack", [Token]),
{noreply, State#state{push_data = maps:remove(Token, PushData)}}
end;
handle_info(
{?PULL_DATA_TICK, SocketDest},
#state{
pubkeybin = PubKeyBin,
socket = Socket,
pull_data_timer = PullDataTimer,
pull_data = PullDataMap0
} =
State
) ->
case send_pull_data(PubKeyBin, Socket, SocketDest, PullDataTimer) of
{ok, RefAndToken} ->
PullDataMap1 = maps:put(SocketDest, RefAndToken, PullDataMap0),
{noreply, State#state{pull_data = PullDataMap1}};
{error, Reason} ->
lager:warning(
[{error, Reason}, {lns, SocketDest}],
"could not send pull_data"
),
{noreply, State}
end;
handle_info(
{?PULL_DATA_TIMEOUT_TICK, SocketDest},
#state{pull_data_timer = PullDataTimer} = State
) ->
handle_pull_data_timeout(PullDataTimer, SocketDest),
{noreply, State};
handle_info(
{{'DOWN', PubKeyBin}, _Monitor, process, _Pid, _ExitReason},
#state{pubkeybin = PubKeyBin} = State
) ->
lager:debug("gateway stream ~p went down: ~p waiting ~wms", [_Pid, _ExitReason, ?CLEANUP_TIME]),
_ = erlang:send_after(?CLEANUP_TIME, self(), ?CLEANUP),
{noreply, State};
handle_info(?CLEANUP, #state{pubkeybin = PubKeyBin} = State) ->
case hpr_packet_router_service:locate(PubKeyBin) of
{error, _Reason} ->
lager:debug("connot find a gateway stream: ~p, shutting down", [_Reason]),
{stop, normal, State};
{ok, StreamPid} ->
_ = erlang:monitor(process, StreamPid, [{tag, {'DOWN', PubKeyBin}}]),
lager:debug("found a new gateway stream ~p, monitoring", [StreamPid]),
{noreply, State}
end;
handle_info(_Msg, State) ->
{noreply, State}.
terminate(_Reason, _State = #state{socket = Socket}) ->
ok = gen_udp:close(Socket).
%% ------------------------------------------------------------------
%% Internal Function Definitions
%% ------------------------------------------------------------------
-spec packet_up_to_push_data(
PacketUp :: hpr_packet_up:packet(),
PacketTime :: non_neg_integer(),
GatewayLocation :: hpr_gateway_location:loc()
) ->
{Token :: binary(), Payload :: binary()}.
packet_up_to_push_data(Up, GatewayTime, GatewayLocation) ->
Token = semtech_udp:token(),
PubKeyBin = hpr_packet_up:gateway(Up),
MAC = hpr_utils:pubkeybin_to_mac(PubKeyBin),
B58 = erlang:list_to_binary(libp2p_crypto:bin_to_b58(PubKeyBin)),
Name = erlang:list_to_binary(hpr_utils:gateway_name(PubKeyBin)),
BaseMeta = #{
gateway_id => B58,
gateway_name => Name,
regi => hpr_packet_up:region(Up)
},
%% NOTE: everything in meta needs to be string -> string.
Meta =
case GatewayLocation of
undefined ->
BaseMeta;
{H3Index, Lat, Long} ->
BaseMeta#{
gateway_h3index => erlang:list_to_binary(h3:to_string(H3Index)),
gateway_lat => hpr_utils:format_coord(Lat),
gateway_long => hpr_utils:format_coord(Long)
}
end,
Data = semtech_udp:push_data(
Token,
MAC,
#{
time => iso8601:format(
calendar:system_time_to_universal_time(GatewayTime, millisecond)
),
tmst => hpr_packet_up:timestamp(Up) band 16#FFFF_FFFF,
freq => hpr_packet_up:frequency_mhz(Up),
rfch => 0,
modu => <<"LORA">>,
codr => <<"4/5">>,
stat => 1,
chan => 0,
datr => erlang:atom_to_binary(hpr_packet_up:datarate(Up)),
rssi => hpr_packet_up:rssi(Up),
lsnr => hpr_packet_up:snr(Up),
size => erlang:byte_size(hpr_packet_up:payload(Up)),
data => base64:encode(hpr_packet_up:payload(Up)),
meta => Meta
}
),
{Token, Data}.
-spec txpk_to_packet_down(TxPkBin :: binary()) -> hpr_packet_down:packet().
txpk_to_packet_down(TxPkBin) ->
TxPk = semtech_udp:json_data(TxPkBin),
Map = maps:get(<<"txpk">>, TxPk),
JSONData0 = base64:decode(maps:get(<<"data">>, Map)),
case maps:get(<<"imme">>, Map, false) of
false ->
hpr_packet_down:new_downlink(
JSONData0,
maps:get(<<"tmst">>, Map),
erlang:round(maps:get(<<"freq">>, Map) * 1_000_000),
erlang:binary_to_existing_atom(maps:get(<<"datr">>, Map))
);
true ->
hpr_packet_down:new_imme_downlink(
JSONData0,
erlang:round(maps:get(<<"freq">>, Map) * 1_000_000),
erlang:binary_to_existing_atom(maps:get(<<"datr">>, Map))
)
end.
-spec handle_udp(
Data :: binary(),
DataSrc :: socket_dest(),
State :: #state{}
) -> {noreply, #state{}}.
handle_udp(
Data,
DataSrc,
#state{
push_data = PushData0,
pull_data_timer = PullDataTimer,
pull_data = PullDataMap0,
socket = Socket,
pubkeybin = PubKeyBin
} = State0
) ->
State1 =
case semtech_udp:identifier(Data) of
?PUSH_ACK ->
PushData1 = handle_push_ack(Data, PushData0),
State0#state{push_data = PushData1};
?PULL_ACK ->
PullDataMap1 = handle_pull_ack(Data, DataSrc, PullDataMap0, PullDataTimer),
State0#state{pull_data = PullDataMap1};
?PULL_RESP ->
ok = handle_pull_resp(Data, DataSrc, PubKeyBin, Socket),
State0;
_Id ->
lager:warning("got unknown identifier ~p for ~p", [_Id, Data]),
State0
end,
{noreply, State1}.
-spec schedule_pull_data(non_neg_integer(), socket_dest()) -> reference().
schedule_pull_data(PullDataTimer, SocketDest) ->
_ = erlang:send_after(PullDataTimer, self(), {?PULL_DATA_TICK, SocketDest}).
-spec send_push_data(binary(), binary(), gen_udp:socket(), socket_dest()) ->
{ok | {error, any()}, reference()}.
send_push_data(
Token,
Data,
Socket,
SocketDest
) ->
Reply = udp_send(Socket, SocketDest, Data),
TimerRef = erlang:send_after(?PUSH_DATA_TIMER, self(), {?PUSH_DATA_TICK, Token}),
lager:debug(
[{token, Token}, {dest, SocketDest}, {reply, Reply}],
"sent push_data"
),
{Reply, TimerRef}.
-spec send_pull_data(
PubKeybin :: libp2p_crypto:pubkey_bin(),
Socket :: gen_udp:socket(),
Dest :: socket_dest(),
PullDataTimer :: non_neg_integer()
) -> {ok, #{timer_ref := reference(), token := binary()}} | {error, any()}.
send_pull_data(PubKeyBin, Socket, SocketDest, PullDataTimer) ->
Token = semtech_udp:token(),
Data = semtech_udp:pull_data(Token, hpr_utils:pubkeybin_to_mac(PubKeyBin)),
case udp_send(Socket, SocketDest, Data) of
ok ->
lager:debug("sent pull data keepalive ~p", [Token]),
TimerRef = erlang:send_after(
PullDataTimer, self(), {?PULL_DATA_TIMEOUT_TICK, SocketDest}
),
{ok, #{timer_ref => TimerRef, token => Token}};
Error ->
lager:warning("failed to send pull data keepalive ~p: ~p", [Token, Error]),
Error
end.
handle_pull_data_timeout(PullDataTimer, SocketDest) ->
lager:debug("got a pull data timeout, ignoring missed pull_ack [retry: ~p]", [PullDataTimer]),
_ = schedule_pull_data(PullDataTimer, SocketDest).
handle_push_ack(Data, PushData) ->
Token = semtech_udp:token(Data),
case maps:get(Token, PushData, undefined) of
undefined ->
lager:debug("got unknown push ack ~p", [Token]),
PushData;
{_, TimerRef} ->
lager:debug("got push ack ~p", [Token]),
_ = erlang:cancel_timer(TimerRef),
NewPushData = maps:remove(Token, PushData),
NewPushData
end.
-spec handle_pull_ack(
Data :: binary(),
DataSrc :: socket_dest(),
PullData :: pull_data_map(),
PullDataTime :: non_neg_integer()
) -> pull_data_map().
handle_pull_ack(Data, DataSrc, PullDataMap, PullDataTimer) ->
case {semtech_udp:token(Data), maps:get(DataSrc, PullDataMap, undefined)} of
{Token, #{token := Token, timer_ref := TimerRef}} ->
_ = erlang:cancel_timer(TimerRef),
_ = schedule_pull_data(PullDataTimer, DataSrc),
maps:put(DataSrc, acknowledged, PullDataMap);
{_, undefined} ->
lager:warning("pull_ack for unknown source"),
PullDataMap;
_ ->
lager:warning("pull_ack with unknown token"),
PullDataMap
end.
-spec handle_pull_resp(
Data :: binary(),
DataSrc :: socket_dest(),
PubKeyBin :: libp2p_crypto:pubkey_bin(),
Socket :: gen_udp:socket()
) ->
ok.
handle_pull_resp(Data, DataSrc, PubKeyBin, Socket) ->
%% Send downlink to grpc handler
PacketDown = txpk_to_packet_down(Data),
lager:debug("sending gwmp downlink to ~p", [hpr_utils:gateway_name(PubKeyBin)]),
_ = hpr_packet_router_service:send_packet_down(PubKeyBin, PacketDown),
%% Ack the downlink
Token = semtech_udp:token(Data),
send_tx_ack(Token, PubKeyBin, Socket, DataSrc),
ok.
-spec send_tx_ack(
Token :: binary(),
PubKeyBin :: libp2p_crypto:pubkey_bin(),
Socket :: gen_udp:socket(),
SocketDest :: socket_dest()
) -> ok | {error, any()}.
send_tx_ack(Token, PubKeyBin, Socket, SocketDest) ->
Data = semtech_udp:tx_ack(Token, hpr_utils:pubkeybin_to_mac(PubKeyBin)),
Reply = udp_send(Socket, SocketDest, Data),
lager:debug(
"sent ~p/~p to ~p replied: ~p",
[Token, Data, SocketDest, Reply]
),
Reply.
%%%-------------------------------------------------------------------
%% @doc
%% Only send a PULL_DATA if we haven't seen the destination
%% before. If we have, the lifecycle for sending PULL_DATA
%% is already being handled.
%% @end
%%%-------------------------------------------------------------------
-spec maybe_send_pull_data(
SocketDest :: socket_dest(),
State :: #state{}
) -> #state{}.
maybe_send_pull_data(
SocketDest0,
#state{pull_data = PullDataMap, addr_resolutions = AddrResolutions0} = State0
) ->
{SocketDest, AddrResolutions} = maybe_resolve_addr(SocketDest0, AddrResolutions0),
State = State0#state{addr_resolutions = AddrResolutions},
case maps:get(SocketDest, PullDataMap, undefined) of
undefined ->
#state{
pubkeybin = PubKeyBin,
socket = Socket,
pull_data_timer = PullDataTimer
} = State,
case send_pull_data(PubKeyBin, Socket, SocketDest, PullDataTimer) of
{ok, RefAndToken} ->
State#state{
pull_data = maps:put(
SocketDest,
RefAndToken,
PullDataMap
)
};
{error, Reason} ->
lager:warning(
[{error, Reason}, {lns, SocketDest}],
"could not send pull_data"
),
State
end;
_ ->
State
end.
-spec udp_send(gen_udp:socket(), socket_dest(), binary()) -> ok | {error, any()}.
udp_send(Socket, {Address, Port}, Data) ->
gen_udp:send(Socket, Address, Port, Data).
%%%-------------------------------------------------------------------
%% @doc
%%
%% We get Addresses as strings, but they are handled as `inet:ip_address()'
%% which is a tuple of numbers.
%%
%% So we attempt to clean provided Addresses. If we received a hostname, we will
%% try to resolve it 1 time into the IP Address.
%%
%% Otherwise we carry on with the string form, and there will be warnings in the
%% logs.
%% @end
%%%-------------------------------------------------------------------
-spec maybe_resolve_addr({string(), inet:port_number()}, IpResolutions :: map()) ->
{{string() | inet:ip_address(), inet:port_number()}, map()}.
maybe_resolve_addr({Addr, Port} = Dest, AddrResolutions) ->
case maps:get(Dest, AddrResolutions, undefined) of
undefined ->
New =
case inet:parse_address(Addr) of
{ok, IPAddr} ->
{IPAddr, Port};
{error, _} ->
{resolve_addr(Addr), Port}
end,
{New, AddrResolutions#{Dest => New}};
Resolved ->
{Resolved, AddrResolutions}
end.
resolve_addr(Addr) ->
case inet:gethostbyname(Addr) of
{ok, #hostent{h_addr_list = [IPAddr]}} ->
IPAddr;
{ok, #hostent{h_addr_list = [IPAddr | _]}} ->
lager:info([{addr, Addr}], "multiple IPs for address, using the first"),
IPAddr;
{error, Err} ->
lager:warning([{err, Err}, {addr, Addr}], "could not resolve hostname"),
Addr
end.
%% ------------------------------------------------------------------
%% EUnit tests
%% ------------------------------------------------------------------
-ifdef(TEST).
-include_lib("eunit/include/eunit.hrl").
verify_downlink_test() ->
%% Taken from actual gwmp reply
TxPkBin =
<<2, 211, 238, 3, 123, 34, 116, 120, 112, 107, 34, 58, 123, 34, 105, 109, 109, 101, 34, 58,
102, 97, 108, 115, 101, 44, 34, 114, 102, 99, 104, 34, 58, 48, 44, 34, 112, 111, 119,
101, 34, 58, 50, 48, 44, 34, 97, 110, 116, 34, 58, 48, 44, 34, 98, 114, 100, 34, 58, 48,
44, 34, 116, 109, 115, 116, 34, 58, 53, 54, 54, 53, 53, 56, 51, 44, 34, 102, 114, 101,
113, 34, 58, 57, 50, 55, 46, 53, 44, 34, 109, 111, 100, 117, 34, 58, 34, 76, 79, 82, 65,
34, 44, 34, 100, 97, 116, 114, 34, 58, 34, 83, 70, 49, 48, 66, 87, 53, 48, 48, 34, 44,
34, 99, 111, 100, 114, 34, 58, 34, 52, 47, 53, 34, 44, 34, 105, 112, 111, 108, 34, 58,
116, 114, 117, 101, 44, 34, 115, 105, 122, 101, 34, 58, 51, 51, 44, 34, 100, 97, 116,
97, 34, 58, 34, 73, 77, 97, 69, 53, 90, 98, 71, 121, 65, 79, 75, 117, 110, 68, 101, 49,
50, 85, 85, 48, 89, 100, 57, 54, 72, 78, 106, 85, 115, 114, 121, 115, 82, 55, 86, 107,
69, 118, 75, 70, 86, 88, 79, 34, 125, 125>>,
Downlink = txpk_to_packet_down(TxPkBin),
?assertEqual(ok, packet_router_pb:verify_msg(Downlink)),
ok.
-endif.