Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion src/cli/hpr_cli_info.erl
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,15 @@ info_usage() ->
"\n\n",
"info key - Print HPR's Public Key\n"
"info ips - Export all connected hotspots IPs to /tmp/hotspot_ip.json\n"
"info netids - Export net ids stats as json to /tmp/net_ids.json\n"
]
].

info_cmd() ->
[
[["info", "key"], [], [], fun info_key/3],
[["info", "ips"], [], [], fun info_ips/3]
[["info", "ips"], [], [], fun info_ips/3],
[["info", "netids"], [], [], fun info_netids/3]
].

info_key(["info", "key"], [], []) ->
Expand Down Expand Up @@ -77,6 +79,28 @@ info_ips(["info", "ips"], [], []) ->
info_ips(_, _, _) ->
usage.

info_netids(["info", "netids"], [], []) ->
List = lists:map(
fun({NetID, Count}) ->
#{
net_id => NetID,
count => Count
}
end,
hpr_netid_stats:export()
),
Json = jsx:encode(List),
case file:open("/tmp/net_ids.json", [write]) of
{ok, File} ->
file:write(File, Json),
file:close(File),
c_text("Exported to /tmp/net_ids.json");
{error, Reason} ->
c_text("Failed to export ~p", [Reason])
end;
info_netids(_, _, _) ->
usage.

%%--------------------------------------------------------------------
%% Helpers
%%--------------------------------------------------------------------
Expand Down
19 changes: 19 additions & 0 deletions src/grpc/packet_router/hpr_packet_up.erl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
encode/1,
decode/1,
type/1,
net_id/1,
md/1, md/2
]).

Expand Down Expand Up @@ -167,6 +168,24 @@ type(Packet) ->
{undefined, 0}
end.

-spec net_id(Packet :: packet()) -> {ok, lora_subnet:net_id()} | {error, any()}.
net_id(Packet) ->
case ?MODULE:payload(Packet) of
<<?JOIN_REQUEST:3, _:5, _AppEUI:64/integer-unsigned-little,
_DevEUI:64/integer-unsigned-little, _DevNonce:2/binary, _MIC:4/binary>> ->
{error, join};
(<<FType:3, _:5, DevAddr:32/integer-unsigned-little, _ADR:1, _ADRACKReq:1, _ACK:1, _RFU:1,
FOptsLen:4, _FCnt:16/little-unsigned-integer, _FOpts:FOptsLen/binary,
PayloadAndMIC/binary>>) when
(FType == ?UNCONFIRMED_UP orelse FType == ?CONFIRMED_UP) andalso
%% MIC is 4 bytes, so the binary must be at least that long
erlang:byte_size(PayloadAndMIC) >= 4
->
lora_subnet:parse_netid(DevAddr, little);
_ ->
{error, undefined}
end.

-spec md(PacketUp :: packet()) -> ok.
md(PacketUp) ->
?MODULE:md(PacketUp, #{}).
Expand Down
3 changes: 3 additions & 0 deletions src/hpr_routing.erl
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ handle_packet(PacketUp, Opts) ->
hpr_metrics:observe_packet_up(PacketUpType, Error, 0, Timestamp),
Error;
ok ->
ok = hpr_netid_stats:maybe_report_net_id(PacketUp),
do_handle_packet(PacketUp, Timestamp)
end.

Expand Down Expand Up @@ -500,6 +501,7 @@ foreach_setup() ->
ok = hpr_route_ets:init(),
ok = hpr_multi_buy:init(),
ok = hpr_device_stats:init(),
ok = hpr_netid_stats:init(),
meck:new(hpr_gateway_location, [passthrough]),
meck:expect(hpr_gateway_location, get, fun(_) -> {error, not_implemented} end),
ok.
Expand All @@ -509,6 +511,7 @@ foreach_cleanup(ok) ->
true = ets:delete(hpr_route_devaddr_ranges_ets),
true = ets:delete(hpr_route_eui_pairs_ets),
true = ets:delete(hpr_device_stats_ets),
true = ets:delete(hpr_netid_stats_ets),
lists:foreach(
fun(RouteETS) ->
SKFETS = hpr_route_ets:skf_ets(RouteETS),
Expand Down
1 change: 1 addition & 0 deletions src/hpr_sup.erl
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ init([]) ->

ok = timing("packet routing cache", fun() -> hpr_routing_cache:init_ets() end),
ok = timing("device stats", fun() -> hpr_device_stats:init() end),
ok = timing("net_id stats", fun() -> hpr_netid_stats:init() end),
ok = timing("routing throttles", fun() -> hpr_routing:init() end),
ok = timing("multi buy", fun() -> hpr_multi_buy:init() end),
ok = timing("packet_router streams", fun() -> hpr_protocol_router:init() end),
Expand Down
68 changes: 68 additions & 0 deletions src/metrics/hpr_netid_stats.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
-module(hpr_netid_stats).

-export([
init/0,
maybe_report_net_id/1,
export/0
]).

-define(ETS, hpr_netid_stats_ets).

-spec init() -> ok.
init() ->
ets:new(?ETS, [
public,
named_table,
set,
{write_concurrency, true}
]),
ok.

-spec maybe_report_net_id(PacketUp :: hpr_packet_up:packet()) -> ok.
maybe_report_net_id(PacketUp) ->
case hpr_packet_up:net_id(PacketUp) of
{error, _Reason} ->
ok;
{ok, NetId} ->
ets:update_counter(
?ETS, NetId, {2, 1}, {default, 0}
),
ok
end.

-spec export() -> list({non_neg_integer(), non_neg_integer()}).
export() ->
ets:tab2list(?ETS).

%% ------------------------------------------------------------------
%% EUNIT Tests
%% ------------------------------------------------------------------
-ifdef(TEST).

-include_lib("eunit/include/eunit.hrl").

all_test_() ->
{foreach, fun foreach_setup/0, fun foreach_cleanup/1, [
?_test(test_maybe_report_net_id())
]}.

foreach_setup() ->
ok = ?MODULE:init(),
ok.

foreach_cleanup(ok) ->
_ = catch ets:delete(?ETS),
ok.

test_maybe_report_net_id() ->
PacketUp = test_utils:uplink_packet_up(#{}),

?assertEqual(ok, maybe_report_net_id(PacketUp)),
?assertEqual(ok, maybe_report_net_id(PacketUp)),
?assertEqual(ok, maybe_report_net_id(PacketUp)),

?assertEqual([{0, 3}], ets:tab2list(?ETS)),

ok.

-endif.
Loading