From 12df9036d1249466ed632ce50704076a649ca0e4 Mon Sep 17 00:00:00 2001 From: Michael Jeffrey Date: Wed, 28 Jun 2023 12:07:42 -0700 Subject: [PATCH] expose Devaddr Prefix as env setting (#974) * expose Devaddr Prefix as env setting :warning: This will __only__ work with Devaddrs of a Type-0 NetID. * more dangling $H EUnits are left alone so we don't have to introduce bit sizes to simple devaddr construction, and eunit tests should not consider application env. * update disclaimer --- .env-template | 9 ++++++++- config/sys.config.src | 1 + config/test.config | 1 + src/cli/router_cli_migration.erl | 4 ++-- src/device/router_device_devaddr.erl | 4 ++-- test/router_device_devaddr_SUITE.erl | 2 +- test/router_grpc_SUITE.erl | 6 ++++-- test/router_lorawan_SUITE.erl | 2 +- test/router_lorawan_handler_test.erl | 3 ++- test/test_utils.erl | 5 +++-- 10 files changed, 25 insertions(+), 12 deletions(-) diff --git a/.env-template b/.env-template index 8631f1285..b5a1c4751 100644 --- a/.env-template +++ b/.env-template @@ -57,4 +57,11 @@ ROUTER_CHARGE_WHEN_NO_OFFER=true ROUTER_DEVADDR_ALLOCATE_RESOLUTION=3 # Set http port for Prometheus handler to come scrape metrics (Default to port 3000) -ROUTER_METRICS_PORT=3000 \ No newline at end of file +ROUTER_METRICS_PORT=3000 + +# The first 7 bits of a DevAddr as an integer. (default: 72 :: $H) +# __DISCLAIMER__: +# This setting _only_ works for NetIDs of Type-0. +# The integer represents the first 7-bits of a DevAddr. NOT the Network ID. +# Change at your own discretion, this may lead to instability in assigning DevAddrs to devices. +ROUTER_DEVADDR_PREFIX=72 diff --git a/config/sys.config.src b/config/sys.config.src index 002c09eaf..bf8846c87 100644 --- a/config/sys.config.src +++ b/config/sys.config.src @@ -33,6 +33,7 @@ {sc_hook_close_submit, router_sc_worker} ]}, {router, [ + {devaddr_prefix, "${ROUTER_DEVADDR_PREFIX}"}, {is_chain_dead, "${ROUTER_IS_CHAIN_DEAD}"}, {grpc_port, ${GRPC_PORT:-8080}}, {max_v8_context, 1000}, diff --git a/config/test.config b/config/test.config index 4842b0b26..2a34463d1 100644 --- a/config/test.config +++ b/config/test.config @@ -2,6 +2,7 @@ [ {router, [ {oui, 1}, + {devaddr_prefix, "72"}, {metrics_port, 3001}, {max_v8_context, 10}, {router_http_channel_url_check, false}, diff --git a/src/cli/router_cli_migration.erl b/src/cli/router_cli_migration.erl index 19f56ac05..981e16f37 100644 --- a/src/cli/router_cli_migration.erl +++ b/src/cli/router_cli_migration.erl @@ -248,7 +248,7 @@ get_ouis() -> [] -> Owner; [Router1 | _] -> Router1 end, - Prefix = $H, + Prefix = router_utils:get_env_int(devaddr_prefix, $H), DevAddrRanges = lists:map( fun(<> = _Subnet) -> Size = blockchain_ledger_routing_v1:subnet_mask_to_size(Mask), @@ -341,7 +341,7 @@ devaddr_ranges(RoutingEntry) -> lists:foldl( fun(<>, {Acc, _}) -> Size = blockchain_ledger_routing_v1:subnet_mask_to_size(Mask), - Prefix = $H, + Prefix = router_utils:get_env_int(devaddr_prefix, $H), Min = lorawan_utils:reverse(<>), Max = lorawan_utils:reverse(<< (Base + Size - 1):25/integer-unsigned-little, Prefix:7/integer diff --git a/src/device/router_device_devaddr.erl b/src/device/router_device_devaddr.erl index b88e978e7..ad2590f33 100644 --- a/src/device/router_device_devaddr.erl +++ b/src/device/router_device_devaddr.erl @@ -174,7 +174,7 @@ handle_call( CurrentIndex = maps:get(Parent, Keys, 0), DevaddrBase = lists:nth(CurrentIndex + 1, Numbers), NextIndex = (CurrentIndex + 1) rem length(Numbers), - DevAddrPrefix = application:get_env(blockchain, devaddr_prefix, $H), + DevAddrPrefix = router_utils:get_env_int(devaddr_prefix, $H), Reply = {ok, <>}, @@ -348,7 +348,7 @@ get_devaddrs(#state{route_id = RouteID}) -> -spec devaddr_num_to_base_num(non_neg_integer()) -> non_neg_integer(). devaddr_num_to_base_num(DevaddrNum) -> - Prefix = application:get_env(blockchain, devaddr_prefix, $H), + Prefix = router_utils:get_env_int(devaddr_prefix, $H), <> = lorawan_utils:reverse( binary:encode_unsigned(DevaddrNum) ), diff --git a/test/router_device_devaddr_SUITE.erl b/test/router_device_devaddr_SUITE.erl index b1b85f04e..4bf5e725f 100644 --- a/test/router_device_devaddr_SUITE.erl +++ b/test/router_device_devaddr_SUITE.erl @@ -99,7 +99,7 @@ allocate(Config) -> ), ?assertEqual(16, erlang:length(DevAddrs)), - DevAddrPrefix = application:get_env(blockchain, devaddr_prefix, $H), + DevAddrPrefix = router_utils:get_env_int(devaddr_prefix, $H), Expected = [ <<(I - 1):25/integer-unsigned-little, DevAddrPrefix:7/integer>> || I <- lists:seq(1, 8) diff --git a/test/router_grpc_SUITE.erl b/test/router_grpc_SUITE.erl index bc5a77001..4a04bfc5b 100644 --- a/test/router_grpc_SUITE.erl +++ b/test/router_grpc_SUITE.erl @@ -285,7 +285,8 @@ packet_from_known_device_no_downlink_gateway_test(Config) -> {ok, Device} = router_device:get_by_id(DB, CF, WorkerID), %% create an unconfirmed uplink - DevAddr = <<33554431:25/integer-unsigned-little, $H:7/integer>>, + Prefix = router_utils:get_env_int(devaddr_prefix, $H), + DevAddr = <<33554431:25/integer-unsigned-little, Prefix:7/integer>>, DataPacket1 = #packet_router_packet_up_v1_pb{ payload = test_utils:frame_payload( ?UNCONFIRMED_UP, @@ -376,7 +377,8 @@ join_packet(PubKeyBin, SigFun, AppKey, DevNonce, RSSI) -> #blockchain_state_channel_message_v1_pb{msg = {packet, SignedSCPacket}}. data_packet_map(MType, Fcnt, PubKeyBin, Device, SigFun, Options) -> - DevAddr0 = <<33554431:25/integer-unsigned-little, $H:7/integer>>, + Prefix = router_utils:get_env_int(devaddr_prefix, $H), + DevAddr0 = <<33554431:25/integer-unsigned-little, Prefix:7/integer>>, <> = DevAddr0, P = #packet_pb{ oui = 0, diff --git a/test/router_lorawan_SUITE.erl b/test/router_lorawan_SUITE.erl index 34519673c..d69d94629 100644 --- a/test/router_lorawan_SUITE.erl +++ b/test/router_lorawan_SUITE.erl @@ -74,7 +74,7 @@ init_per_testcase(TestCase, Config) -> _ = persistent_term:erase(router_blockchain), meck:new(router_device_devaddr, [passthrough]), meck:expect(router_device_devaddr, allocate, fun(_, _) -> - DevAddrPrefix = application:get_env(blockchain, devaddr_prefix, $H), + DevAddrPrefix = router_utils:get_env_int(devaddr_prefix, $H), {ok, <<33554431:25/integer-unsigned-little, DevAddrPrefix:7/integer>>} end), Region = proplists:get_value(region, Config), diff --git a/test/router_lorawan_handler_test.erl b/test/router_lorawan_handler_test.erl index 59154c800..f5f07a0bc 100644 --- a/test/router_lorawan_handler_test.erl +++ b/test/router_lorawan_handler_test.erl @@ -123,7 +123,8 @@ handle_info( #{<<"rxpk">> := [JSON]} = jsx:decode(JSONBin, [return_maps]), lager:info("got packet ~p", [JSON]), State#state.pid ! rx, - <> = <<33554431:25/integer-unsigned-little, $H:7/integer>>, + Prefix = router_utils:get_env_int(devaddr_prefix, $H), + <> = <<33554431:25/integer-unsigned-little, Prefix:7/integer>>, HeliumPacket = #packet_pb{ type = lorawan, payload = base64:decode(maps:get(<<"data">>, JSON)), diff --git a/test/test_utils.erl b/test/test_utils.erl index 5555a2455..9a7b1ecb0 100644 --- a/test/test_utils.erl +++ b/test/test_utils.erl @@ -62,7 +62,7 @@ init_per_testcase(TestCase, Config) -> _ = persistent_term:erase(router_blockchain), meck:new(router_device_devaddr, [passthrough]), meck:expect(router_device_devaddr, allocate, fun(_, _) -> - DevAddrPrefix = application:get_env(blockchain, devaddr_prefix, $H), + DevAddrPrefix = router_utils:get_env_int(devaddr_prefix, $H), {ok, <<33554431:25/integer-unsigned-little, DevAddrPrefix:7/integer>>} end), @@ -972,7 +972,8 @@ frame_packet(MType, PubKeyBin, NwkSessionKey, AppSessionKey, FCnt) -> frame_packet(MType, PubKeyBin, NwkSessionKey, AppSessionKey, FCnt, #{}). frame_packet(MType, PubKeyBin, NwkSessionKey, AppSessionKey, FCnt, Options) -> - DevAddr = maps:get(devaddr, Options, <<33554431:25/integer-unsigned-little, $H:7/integer>>), + Prefix = router_utils:get_env_int(devaddr_prefix, $H), + DevAddr = maps:get(devaddr, Options, <<33554431:25/integer-unsigned-little, Prefix:7/integer>>), Payload1 = frame_payload(MType, DevAddr, NwkSessionKey, AppSessionKey, FCnt, Options), <> = DevAddr, Routing = blockchain_helium_packet_v1:make_routing_info({devaddr, DevNum}),