; ***************************************************************************
; 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_VALUE_W_ASM
#define AQH_AVR_NETWORK_MSG_VALUE_W_ASM


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

.cseg


; ---------------------------------------------------------------------------
; @routine NETMSG_ValueWriteReport @global
; Write a REPORT_VALUE packet.
;
; @return nothing
; @param  R16     destination address
; @param  R17     value id
; @param  R19:R18 value
; @param  R21:R20 denom (e.g. 100, meaning value must be divided by 100)
; @param  R22     value type
; @param  X       buffer to write to
; @param  Y       pointer to network interface
; @clobbers       R23, R24, R25 (R16, R17, R18, R19, R20, R21)

NETMSG_ValueWriteReport:
  ldi r23, NETMSG_CMD_VALUE_REPORT
  rcall NET_Interface_GetNextMsgId           ; (get msg id to r25:r24)
  rjmp netMsgValueWrite                      ; (R16, R17, R18, R19, R20, R21)
; @end



; ---------------------------------------------------------------------------
; @routine NETMSG_ValueWriteSet @global
; Write a REPORT_VALUE packet.
;
; @return nothing
; @param  R16     destination address
; @param  R17     value id
; @param  R19:R18 value
; @param  R21:R20 denom (e.g. 100, meaning value must be divided by 100)
; @param  R22     value type
; @param  X       buffer to write to
; @param  Y       pointer to network interface
; @clobbers       R23, R24, R25 (R16, R17, R18, R19, R20, R21)

NETMSG_ValueWriteSet:
  ldi r23, NETMSG_CMD_VALUE_SET
  rcall NET_Interface_GetNextMsgId           ; (get msg id to r25:r24)
  rjmp netMsgValueWrite                      ; (R16, R17, R18, R19, R20, R21)
; @end



; ---------------------------------------------------------------------------
; @routine NETMSG_ValueWriteResponse @global
; Write a REPORT_VALUE packet.
;
; @return nothing
; @param  R16     destination address
; @param  R17     value id
; @param  R19:R18 value
; @param  R21:R20 denom (e.g. 100, meaning value must be divided by 100)
; @param  R22     value type
; @param  R23     msg code
; @param  R25:R24 reference message id
; @param  X       buffer to write to
; @param  Y       pointer to network interface
; @clobbers       (R16, R17, R18, R19, R20, R21)

NETMSG_ValueWriteResponse:
  rjmp netMsgValueWrite                      ; (R16, R17, R18, R19, R20, R21)
; @end



; ---------------------------------------------------------------------------
; @routine netMsgValueWrite @global
; Write a packet (except the checksum byte).
;
; @return nothing
; @param  R16     destination address
; @param  R17     value id
; @param  R19:R18 value
; @param  R21:R20 denom (e.g. 100, meaning value must be divided by 100)
; @param  R22     value type
; @param  R23     msg code
; @param  R25:R24 message id
; @param  X       buffer to write to
; @param  Y       pointer to network interface
; @clobbers       R16 (R17, R18, R19, R20, R21)

netMsgValueWrite:
  st X+, r16     ; dest address                                     0
  ldi r16, 14    ; msg code+src address+12 payload bytes            
  st X+, r16     ; msg len                                          1
  ; begin of payload
  st X+, r23     ; msg code                                         2
  ldd r16, Y+NET_IFACE_OFFS_ADDRESS
  st X+, r16     ; src address                                      3
  adiw xh:xl, 4  ; skip uid (4 bytes)                               4
  st X+, r24     ; msg id (low)                                     8
  st X+, r25     ; msg id (high)                                    9
  st X+, r17     ; value id                                        10
  st X+, r22     ; value type                                      11
  st X+, r18     ; value (low)                                     12
  st X+, r19     ; value (high)                                    13
  st X+, r20     ; denom (low)                                     14
  st X+, r21     ; denom (high)                                    15
                                                                 ; 16
  sbiw xh:xl, 12 ; go back to UID (12=16 back, 4 forward)
  rcall NETMSG_Common_AddUidToBuffer ; (R16, R18, R19, R20, R21)
  sbiw xh:xl, 8  ; go back to beginning of message
  
  ; finish msg
  rcall NETMSG_CalcAndAddChecksumByte        ; (R16, R17, R18, R19, R20, X)
  sbiw xh:xl, 17                             ; go back to beginning of message
  ret
; @end


#endif

