; ***************************************************************************
; 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





; ***************************************************************************
; 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

  rcall appStatsWalkTable

  ; add more here
  ret
; @end



; ---------------------------------------------------------------------------
; @routine appStatsTable
;
; @clobbers r16, r18, r19, r22, Z, (any)

appStatsWalkTable:
  ldi zl, LOW(appStatsTable*2)
  ldi zh, HIGH(appStatsTable*2)
  ldi yl, LOW(netInterfaceData)
  ldi yh, HIGH(netInterfaceData)

  lds r16, appStatsTimer
  bigcall WalkTable
  ret
; @end



#ifdef APP_STATS_NETDEV2
  AppStats_OnEveryMinute_sendDeviceDev2:
    push yl
      push yh
        ldi yl, LOW(netInterfaceData2)
        ldi yh, HIGH(netInterfaceData2)
        rcall AppNetwork_SendDevice
      pop yh
    pop yl
    ret
  
  AppStats_OnEveryMinute_sendRecvStats2:
    push yl
      push yh
        ldi yl, LOW(netInterfaceData2)
        ldi yh, HIGH(netInterfaceData2)
        rcall AppNetwork_SendRxdStats
      pop yh
    pop yl
    ret
  
  AppStats_OnEveryMinute_sendSendStats2:
    push yl
      push yh
        ldi yl, LOW(netInterfaceData2)
        ldi yh, HIGH(netInterfaceData2)
        rcall AppNetwork_SendTxdStats
      pop yh
    pop yl
    ret
#endif
; @end



; ---------------------------------------------------------------------------
; table
; time  routine (word address)
; @TODO use 2nd byte as flags (e.g. set Y=netInterfaceData, set Y=netInterfaceData2)

appStatsTable:
  .dw 1, AppNetwork_SendMemStats
  .dw 2, AppNetwork_SendDevice
  .dw 3, AppNetwork_SendRxdStats
  .dw 4, AppNetwork_SendTxdStats
  .dw 5, AppNetwork_SendDevice
#ifdef APP_STATS_NETDEV2
  .dw 6, AppStats_OnEveryMinute_sendDeviceDev2
  .dw 7, AppStats_OnEveryMinute_sendRecvStats2
  .dw 8, AppStats_OnEveryMinute_sendSendStats2
#endif
  .dw 9, AppNetwork_SendDevice
  .dw 14, AppNetwork_SendDevice
  .dw 19, AppNetwork_SendDevice
  .dw 24, AppNetwork_SendDevice
  .dw 29, AppNetwork_SendDevice
  
  ; end of table
  .dw 0, 0




