anchor.diff:  
   store_adr(Link, ADR) -> Link#link{adr_flag_use=ADR}.
   
  -store_rxpk(Gateway, Link, RxQ, FCnt, _Data) ->
  +store_rxpk(Gateway, Link, RxQ) ->
       % store #rxframe{frid, mac, rssi, lsnr, region, freq, datr, codr, devaddr, fcnt, devstat}
       mnesia:dirty_write(rxframes, #rxframe{frid= <<(erlang:system_time()):64>>,
           mac=Gateway#gateway.mac, rssi=RxQ#rxq.rssi, lsnr=RxQ#rxq.lsnr,
           region=Link#link.region, freq=RxQ#rxq.freq, datr=RxQ#rxq.datr, codr=RxQ#rxq.codr,
  -        devaddr=Link#link.devaddr, fcnt=FCnt, devstat=Link#link.devstat}),
  +        devaddr=Link#link.devaddr, fcnt=Link#link.fcntup, devstat=Link#link.devstat}),
       ok.
   
  -handle_rxpk(Gateway, RxQ, MType, Link, ADRACKReq, ACK, FOptsOut, Port, Data)
  +handle_rxpk(Gateway, RxQ, MType, Link, Fresh, Frame)
           when MType == 2#010; MType == 2#100 ->
       <<Confirm:1, _:2>> = <<MType:3>>,
  -    handle_uplink(Gateway, RxQ, Confirm, Link, ADRACKReq, ACK, FOptsOut, Port, Data).
  -
  -handle_uplink(Gateway, RxQ, Confirm, Link, ADRACKReq, ACK, FOptsOut, Port, Data) ->
  +    store_rxpk(Gateway, Link, RxQ),
       % will transmit in the RX2 window
       TxQ = lorawan_mac_region:rx2_rf(Link#link.region, RxQ),
  -    % check whether last transmission was lost
  -    {LastLost, LostFrame} = check_lost(ACK, Link#link.devaddr),
  +    case Fresh of
  +        new ->
  +            handle_uplink(Gateway, TxQ, Confirm, Link, Frame);
  +        reset ->
  +            reset_link(Link#link.devaddr),
  +            handle_uplink(Gateway, TxQ, Confirm, Link, Frame);
  +        retransmit ->
  +            case retransmit_downlink(Link#link.devaddr) of
  +                {true, LostFrame} ->
  +                    TxQ = lorawan_mac_region:rx2_rf(Link#link.region, RxQ),
  +                    {send, Gateway, TxQ, LostFrame};
  +                {false, _} ->
  +                    ok
  +            end
  +    end.
  +
  +handle_uplink(Gateway, TxQ, Confirm, Link, #frame{devaddr=DevAddr,
  +        adr_ack_req=ADRACKReq, ack=ACK, fport=FPort, fopts=FOpts, data=RxData}) ->
  +    {ok, L2, FOptsOut} = lorawan_mac_commands:handle(Link, FOpts),
  +    mnesia:dirty_write(links, L2#link{last_rx=calendar:universal_time()}),
  +    % check whether last downlink transmission was lost
  +    {LastLost, LostFrame} = repeat_downlink(DevAddr, ACK),
       % check whether the response is required
       ShallReply = if
           Confirm == 1 ->
  
positive.code:         false -> is_ignored(DevAddr, Rest)
      end.
  
  match(<<DevAddr:32>>, <<MatchAddr:32>>, <<MatchMask:32>>) ->
      (DevAddr band MatchMask) == MatchAddr.
  
  
negative.code:         [] ->
              lager:warning("Unknown DevAddr ~s", [binary_to_hex(DevAddr)]),
              {error, {unknown_devaddr, DevAddr}};
          [L] when FCnt == L#link.fcntup ->
              % retransmission
              {ok, retransmit, L};
          [L] when L#link.fcnt_check == 3 ->
              % checks disabled
              {ok, new, L#link{fcntup = FCnt}};
          [L] when L#link.fcnt_check == 2, FCnt < L#link.fcntup, FCnt < ?MAX_LOST_AFTER_RESET ->
              lager:debug("~w fcnt reset", [DevAddr]),
              % works for 16b only since we cannot distinguish between reset and 32b rollover
              {ok, reset, L#link{fcntup = FCnt, fcntdown=0, devstat_fcnt=undefined}};
          [L] when L#link.fcnt_check == 1 ->
              % strict 32-bit
              case fcnt_gap32(L#link.fcntup, FCnt) of
                  N when N < ?MAX_FCNT_GAP ->
                      % L#link.fcntup is 32b, but the received FCnt may be 16b only
                      LastFCnt = (L#link.fcntup + N) band 16#FFFFFFFF,
                      {ok, new, L#link{fcntup = LastFCnt}};
                  _BigN ->
                      {error, {fcnt_gap_too_large, DevAddr, FCnt}}
              end;
  