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



; ***************************************************************************
; defs


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

.cseg

Ds3231_OnPacketReceived:
  adiw xh:xl, NETMSG_OFFS_CMD               ; command
  ld r16, X
  sbiw xh:xl, NETMSG_OFFS_CMD
  cpi r16, NETMSG_CMD_TIME_REQSET
  breq Ds3231_OnPacketReceived_set
  clc                                       ; unexpected msg
  ret
Ds3231_OnPacketReceived_set:
  ldi zl, LOW(ds3231RecvBuffer)
  ldi zh, HIGH(ds3231RecvBuffer)
  bigcall netMsgTimeRead                    ; r22=sender, r23=command, r25:r24=msg id
  
  push r22
    push r15
      in r15, SREG
      cli
      clr r16
      ldi r17, 7
      ldi zl, LOW(ds3231RecvBuffer)
      ldi zh, HIGH(ds3231RecvBuffer)
      rcall ds3231SendData                  ; (r16, r17, r18, r19, r20, r22, Z)
      out SREG, r15
    pop r15
  pop r22
  
  lds r16, ds3231Flags
  sbr r16, (1<<DS3231_FLAGS_VALID_BIT)
  sts ds3231Flags, r16
#ifdef MODULES_NETWORK
  rcall Ds3231RespondToSetTime
#endif
  sec
  ret
; @end


