; ***************************************************************************
; copyright   : (C) 2023 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.   *
; ***************************************************************************




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

.cseg


; ---------------------------------------------------------------------------
; Enqueue a COM Send stats packet.
;
; IN:
; - R16: destination address
; OUT:
; - CFLAG: set if okay, clear otherwise
; MODIFIED REGS: R16, R17, R20, X (R15, Y)

CPRO_EnqueueComSendStats:
  push r15
    in r15, SREG
    cli
    push r16
      rcall COM_AllocBufferAndGetXY ; (r16, r17, r21) -> Y=buffer, X=msg
    pop r16
    brcc CPRO_EnqueueComSendStats_error

    ldi r17, CPRO_PAYLOAD_FLAGS_UID | (8<<CPRO_PAYLOAD_FLAGS_SHIFT_NUM) ; seconds + 8 bytes payload
    ldi r18, CPRO_CMD_COMSENDSTATS
    push xh
      push xl
        rcall cproBeginMsgWithVariablePayload  ; (R3, R4, R16, R17, R18, R19, R20, R21, X)
        lds r16, comStatsPacketsOut            ; packets out
        st X+, r16
        lds r16, comStatsPacketsOut+1
        st X+, r16
        lds r16, comStatsCollisions            ; collisions
        st X+, r16
        lds r16, comStatsCollisions+1
        st X+, r16
        lds r16, comStatsAborted               ; aborted
        st X+, r16
        lds r16, comStatsAborted+1
        st X+, r16
        lds r16, comStatsSendNoBuffer          ; sendNoBuffer
        st X+, r16
        lds r16, comStatsSendNoBuffer+1
        st X+, r16
      pop xl
    pop xh
    rcall comCalcAndAddChecksumByte            ; (R16, R17, R18, R19, X)
  
    ; mark buffer as enqueued with PRIO "important" (higher retry count)
    ldi r20, COM_BUFFER_PRIO_IMPORTANT
    rcall COM_EnqueuePacket      ; (R15, R16)
    brcc CPRO_EnqueueComSendStats_error
  pop r15
  out SREG, r15
  sec
  ret
CPRO_EnqueueComSendStats_error:
  pop r15
  out SREG, r15
  clc
  ret



; ---------------------------------------------------------------------------
; Enqueue a COM reception stats packet.
;
; IN:
; - R16: destination address
; OUT:
; - CFLAG: set if okay, clear otherwise
; MODIFIED REGS: R16, R17, R20, X (R15, Y)

CPRO_EnqueueComRecvStats:
  push r15
    in r15, SREG
    cli
    push r16
      rcall COM_AllocBufferAndGetXY ; (r16, r17, r21)
    pop r16
    brcc CPRO_EnqueueComRecvStats_error

    ldi r17, CPRO_PAYLOAD_FLAGS_UID | (12<<CPRO_PAYLOAD_FLAGS_SHIFT_NUM) ; seconds + 6 bytes payload
    ldi r18, CPRO_CMD_COMRECVSTATS
    push xh
      push xl
        rcall cproBeginMsgWithVariablePayload   ; (R3, R4, R16, R17, R18, R19, R20, R21, X)
        lds r16, comStatsPacketsIn              ; packets in
        st X+, r16                             
        lds r16, comStatsPacketsIn+1
        st X+, r16
        lds r16, comStatsRecvCrcErrs            ; CRC errors
        st X+, r16
        lds r16, comStatsRecvCrcErrs+1
        st X+, r16
        lds r16, comStatsRecvErrs               ; IO errors
        st X+, r16
        lds r16, comStatsRecvErrs+1
        st X+, r16
        lds r16, comStatsRecvNoBuffer           ; no buffer
        st X+, r16
        lds r16, comStatsRecvNoBuffer+1
        st X+, r16
        lds r16, comStatsHandled                ; handled
        st X+, r16
        lds r16, comStatsHandled+1
        st X+, r16
        lds r16, comStatsMissed                 ; missed
        st X+, r16
        lds r16, comStatsMissed+1
        st X+, r16
      pop xl
    pop xh
    rcall comCalcAndAddChecksumByte             ; (R16, R17, R18, R19, X)
  
    ; mark buffer as enqueued with PRIO "important" (higher retry count)
    ldi r20, COM_BUFFER_PRIO_IMPORTANT
    rcall COM_EnqueuePacket      ; (R15, R16)
    brcc CPRO_EnqueueComRecvStats_error
  pop r15
  out SREG, r15
  sec
  ret
CPRO_EnqueueComRecvStats_error:
  pop r15
  out SREG, r15
  clc
  ret



