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


; ---------------------------------------------------------------------------
; @routine CPRO_WriteResult @global
; Write a RESULT message.
;
; @return nothing
; @param  R16     destination address
; @param  R17     result
; @param  R19:R18 ref msg id
; @param  X       buffer to write to
; @clobbers       R16 (R17, R18, R19, R20, R21)

CPRO_WriteResult:
  st X+, r16     ; dest address
  ldi r16, 5     ; msg code+src address+3 payload bytes
  st X+, r16     ; msg len
  ldi r16, CPRO_CMD_RESULT
  st X+, r16     ; msg code
  lds r16, com2Address
  st X+, r16     ; src address
  st X+, r18     ; ref msg id (low)
  st X+, r19     ; ref msg id (high)
  st X+, r17     ; result
  sbiw xh:xl, 7  ; go back to beginning of message

  ; calc and add checksum
  rcall com2CalcAndAddChecksumByte           ; (R16, R17, R18, R19, X)
  sbiw xh:xl, 8                              ; go back to beginning of message
  ret
; @end
