Skip to content

Commit

Permalink
Improve DevAddr generation
Browse files Browse the repository at this point in the history
  • Loading branch information
gotthardp committed May 20, 2017
1 parent 32a5ffc commit 9bcd665
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
16 changes: 16 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
## Code of Conduct

Be cooperative when you ask for help. We will spend time helping you for free,
so be please ready to spend your time too:
* Read the server documentation and [Troubleshooting](doc/Troubleshooting.md)
guide first to see if you can resolve the issue yourself.
* Describe thoroughly the issue you are having and include (only the) relevant
portions of server logs.
* Be ready to repeat the course of actions against latest server binaries to
generate more debug information or verify the bugfix.
* Be ready to investigate behaviour of your device or gateway. These can be
faulty too.

Be respectful to the maintainers and other contributors. Open source contributors
put long hours into developing projects and doing user support. Those projects
and user support are available for free. We believe this deserves some respect.
16 changes: 12 additions & 4 deletions src/lorawan_mac.erl
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ handle_join(Gateway, RxQ, AppEUI, DevEUI, DevNonce, AppKey) ->
NewAddr = if
D#device.link == undefined;
byte_size(D#device.link) < 4 ->
create_devaddr(NetID);
create_devaddr(NetID, 3);
true ->
D#device.link
end,
Expand Down Expand Up @@ -200,10 +200,18 @@ initial_rxwin({A1, A2, _}, {B1, B2, B3}) ->
apply_default(Value, _Default) when is_number(Value) -> Value;
apply_default(_Else, Default) -> Default.

create_devaddr(NetID) ->
create_devaddr(NetID, Attempts) ->
<<_:17, NwkID:7>> = NetID,
%% FIXME: verify uniqueness
<<NwkID:7, 0:1, (crypto:strong_rand_bytes(3))/binary>>.
<<_:7, NwkAddr:25/bitstring>> = crypto:strong_rand_bytes(4),
DevAddr = <<NwkID:7, NwkAddr:25/bitstring>>,
% assert uniqueness
case mnesia:read(links, DevAddr, read) of
[] ->
DevAddr;
[#link{}] when Attempts > 0 ->
create_devaddr(NetID, Attempts-1)
%% FIXME: do not crash when Attempts == 0
end.

txaccept(TxQ, RX1DROffset, RX2DataRate, AppKey, AppNonce, NetID, DevAddr) ->
MHDR = <<2#001:3, 0:3, 0:2>>,
Expand Down

0 comments on commit 9bcd665

Please sign in to comment.