; ***************************************************************************
; copyright   : (C) 2025 by Martin Preuss
; email       : martin@libchipcard.de
;
; ***************************************************************************
; * This file is part of the project "AqHome".                              *
; * Please see toplevel file COPYING of that project for license details.   *
; ***************************************************************************


; ***************************************************************************
; defines

;.equ APP_STATS_INTERVAL_MINS = 30
.equ APP_STATS_INTERVAL_MINS = 10





; ***************************************************************************
; code

.cseg


; ---------------------------------------------------------------------------
; @routine AppStats_Init @global
;
; @clobbers r16

AppStats_Init:
  clr r16
  sts appStatsTimer, r16
  sec
  ret
; @end



; ---------------------------------------------------------------------------
; @routine AppStats_Fini @global
;

AppStats_Fini:
  ; nothing to do
  ret
; @end



; ---------------------------------------------------------------------------
; @routine AppStats_OnEveryMinute @global
;

AppStats_OnEveryMinute:
  push r15
    in r15, SREG
    cli
    rcall AppStats_OnEveryMinute_noIrq
    out SREG, r15
  pop r15
  ret

AppStats_OnEveryMinute_noIrq:
  lds r16, appStatsTimer
  inc r16
  cpi r16, APP_STATS_INTERVAL_MINS
  brcs AppStats_OnEveryMinute_store
  clr r16
AppStats_OnEveryMinute_store:
  sts appStatsTimer, r16

  mov r17, r16
  andi r17, 3
  brne AppStats_OnEveryMinute_sendStats
  ; send device announcement
  push r16
    push yl
      push yh
        ldi yl, LOW(netInterfaceData)
        ldi yh, HIGH(netInterfaceData)
        rcall AppNetwork_SendDevice
#ifdef APP_STATS_NETDEV2
        ldi yl, LOW(netInterfaceData2)
        ldi yh, HIGH(netInterfaceData2)
        rcall AppNetwork_SendDevice
#endif
      pop yh
    pop yl
  pop r16
AppStats_OnEveryMinute_sendStats:
  cpi r16, 2
  breq AppStats_OnEveryMinute_sendMemStats

  cpi r16, 3
  breq AppStats_OnEveryMinute_sendRecvStats1

  cpi r16, 5
  breq AppStats_OnEveryMinute_sendSendStats1

#ifdef APP_STATS_NETDEV2
  cpi r16, 6
  breq AppStats_OnEveryMinute_sendRecvStats2

  cpi r16, 7
  breq AppStats_OnEveryMinute_sendSendStats2
#endif
  ; add more here
  ret
AppStats_OnEveryMinute_sendMemStats:
  ldi yl, LOW(netInterfaceData)
  ldi yh, HIGH(netInterfaceData)
  rjmp AppNetwork_SendMemStats

AppStats_OnEveryMinute_sendRecvStats1:
  ldi yl, LOW(netInterfaceData)
  ldi yh, HIGH(netInterfaceData)
  rjmp AppNetwork_SendRxdStats

AppStats_OnEveryMinute_sendSendStats1:
  ldi yl, LOW(netInterfaceData)
  ldi yh, HIGH(netInterfaceData)
  rjmp AppNetwork_SendTxdStats

#ifdef APP_STATS_NETDEV2
AppStats_OnEveryMinute_sendRecvStats2:
  ldi yl, LOW(netInterfaceData2)
  ldi yh, HIGH(netInterfaceData2)
  rjmp AppNetwork_SendRxdStats

AppStats_OnEveryMinute_sendSendStats2:
  ldi yl, LOW(netInterfaceData2)
  ldi yh, HIGH(netInterfaceData2)
  rjmp AppNetwork_SendTxdStats
#endif
; @end




