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

#ifndef AQH_AVR_NETWORK_MSG_SENDSTATS_W_ASM
#define AQH_AVR_NETWORK_MSG_SENDSTATS_W_ASM



; ---------------------------------------------------------------------------
; @routine NETMSG_SendStats_Write @global
;
; @param Y pointer to device to write msg for
; @param X pointer to buffer to write to
; @clobbers R16, R18 (R17, R19, R20, R21, Z)

NETMSG_SendStats_Write:
  ldi r16, 0xff
  st X+, r16                          ; dest address
  ldi r16, 13                         ; msg code+src address+11 payload bytes
  st X+, r16                          ; msg len
  ldi r16, NETMSG_CMD_SENDSTATS
  st X+, r16                          ; msg code
  ldd r16, Y+NET_IFACE_OFFS_ADDRESS
  st X+, r16                          ; src address

  ; UID
  bigcall NETMSG_Common_AddUidToBuffer  ; (R16, R18, R19, R20, R21)
  ; interface number
  ldd r16, Y+NET_IFACE_OFFS_IFACENUM
  st X+, r16
  ; packets out
  ldd r16, Y+NET_IFACE_OFFS_PACKETSOUT_LOW
  st X+, r16
  ldd r16, Y+NET_IFACE_OFFS_PACKETSOUT_HIGH
  st X+, r16
  ; collisions
  ldd r16, Y+NET_IFACE_OFFS_ERR_COLLISIONS_LOW
  st X+, r16
  ldd r16, Y+NET_IFACE_OFFS_ERR_COLLISIONS_HIGH
  st X+, r16
  ; busy
  ldd r16, Y+NET_IFACE_OFFS_ERR_BUSY_LOW
  st X+, r16
  ldd r16, Y+NET_IFACE_OFFS_ERR_BUSY_HIGH
  st X+, r16

  sbiw xh:xl, 15 ; go back to beginning of message (1 byte dst addr, 1 byte length, 13 bytes payload)
  bigcall NETMSG_CalcAndAddChecksumByte ; (R16, R17, R18, R19, R20, X)
  sbiw xh:xl, 16 ; go back to beginning of message (1 byte dst addr, 1 byte length, 12 bytes payload, 1 byte crc)
  ret
; @end



#endif

