anchor.diff:      end.
   
   fcnt_gap(A, B) ->
  +    A16 = A band 16#FFFF,
       if
  -        A =< B -> B - A;
  -        A > B -> 16#FFFF - A + B
  +        A16 > B -> 16#FFFF - A16 + B;
  +        true  -> B - A16
       end.
   
   store_rxpk(MAC, RxQ, RF, DevAddr, FCnt, Data) ->
  
positive.code: txpk(Time, RF, DevAddr, FPort, Data) ->
      {atomic, L} = mnesia:transaction(fun() ->
          [D] = mnesia:read(links, DevAddr, write),
          FCnt =  case D#link.fcntdown of
                      N when N < 16#FFFF -> N+1;
                      _Else -> 0
                  end,
          NewD = D#link{fcntdown=FCnt},
          mnesia:write(links, NewD, write),
          NewD
  
negative.code:         [L] ->
              case fcnt_gap(L#link.fcntup, FCnt) of
                  N when N < ?MAX_FCNT_GAP ->
                      NewFCnt = (L#link.fcntup + N) band 16#FFFFFFFF,
                      mnesia:dirty_write(links, L#link{fcntup = NewFCnt}),
                      {ok, L};
                  BigN ->
                      {error, {fcnt_gap_too_large, BigN}}
  