// File: /workspace/github_cpp_makefile_star2_O2/rellla[P]E3DC-Rscp/RscpMain.cpp
// Function: createRequest(SRscpFrameBuffer*, int)
int createRequest(SRscpFrameBuffer * frameBuffer, int requests)
{
    RscpProtocol protocol;
    SRscpValue rootValue;
    // The root container is create with the TAG ID 0 which is not used by any device.
    protocol.createContainerValue(&rootValue, 0);

    //---------------------------------------------------------------------------------------------------------
    // Create a request frame
    //---------------------------------------------------------------------------------------------------------
    printf("\nRequest data:\n");

    // request power data information
    if (requests & TAG_EMS) {
	protocol.appendValue(&rootValue, TAG_EMS_REQ_POWER_PV);
	protocol.appendValue(&rootValue, TAG_EMS_REQ_POWER_BAT);
	protocol.appendValue(&rootValue, TAG_EMS_REQ_POWER_HOME);
	protocol.appendValue(&rootValue, TAG_EMS_REQ_POWER_GRID);
	protocol.appendValue(&rootValue, TAG_EMS_REQ_POWER_ADD);
	protocol.appendValue(&rootValue, TAG_EMS_REQ_GET_POWER_SETTINGS);
	protocol.appendValue(&rootValue, TAG_EMS_REQ_STATUS);
	protocol.appendValue(&rootValue, TAG_EMS_REQ_MODE);
    }

    // request idle periods information
    if (requests & TAG_GET_IDLE_PERIODS) {
	protocol.appendValue(&rootValue, TAG_EMS_REQ_GET_IDLE_PERIODS);
    }

    // request battery information
    if (requests & TAG_BATTERY) {
	SRscpValue batteryContainer;
	protocol.createContainerValue(&batteryContainer, TAG_BAT_REQ_DATA);
	protocol.appendValue(&batteryContainer, TAG_BAT_INDEX, (uint8_t) 0);
	protocol.appendValue(&batteryContainer, TAG_BAT_REQ_RSOC);
	protocol.appendValue(&batteryContainer, TAG_BAT_REQ_MODULE_VOLTAGE);
	protocol.appendValue(&batteryContainer, TAG_BAT_REQ_CURRENT);
	protocol.appendValue(&batteryContainer, TAG_BAT_REQ_STATUS_CODE);
	protocol.appendValue(&batteryContainer, TAG_BAT_REQ_ERROR_CODE);
	// append sub-container to root container
	protocol.appendValue(&rootValue, batteryContainer);
	// free memory of sub-container as it is now copied to rootValue
	protocol.destroyValueData(batteryContainer);
    }

    // request some more power data information
    if (requests & TAG_WEATHER_ENABLE) {
	SRscpValue powerContainer;
	uint8_t enable = !!(requests & TAG_WEATHER_ENABLE_F);
	protocol.createContainerValue(&powerContainer,
				      TAG_EMS_REQ_SET_POWER_SETTINGS);
	protocol.appendValue(&powerContainer,
			     TAG_EMS_WEATHER_REGULATED_CHARGE_ENABLED,
			     enable);
	protocol.appendValue(&rootValue, powerContainer);
	protocol.destroyValueData(powerContainer);
    }

    // request setting idle periods
    // this is WIP and some example values are hardcoded for now
    // this is not useable for productive environments atm
    if (requests & TAG_SET_IDLE_PERIODS) {
	SRscpValue setTimeContainer;
	protocol.createContainerValue(&setTimeContainer,
				      TAG_EMS_REQ_SET_IDLE_PERIODS);

	    SRscpValue setDayContainer;
	    protocol.createContainerValue(&setDayContainer,
					  TAG_EMS_IDLE_PERIOD);
	    protocol.appendValue(&setDayContainer,
				 TAG_EMS_IDLE_PERIOD_TYPE, (uint8_t)LOAD);
	    protocol.appendValue(&setDayContainer,
				 TAG_EMS_IDLE_PERIOD_DAY, (uint8_t)TUESDAY);
	    protocol.appendValue(&setDayContainer,
				 TAG_EMS_IDLE_PERIOD_ACTIVE, (bool)ACTIVE);

		SRscpValue setStartContainer;
		protocol.createContainerValue(&setStartContainer,
					  TAG_EMS_IDLE_PERIOD_START);
		protocol.appendValue(&setStartContainer,
				 TAG_EMS_IDLE_PERIOD_MINUTE, (uint8_t)00);
		protocol.appendValue(&setStartContainer,
				 TAG_EMS_IDLE_PERIOD_HOUR, (uint8_t)11);
		protocol.appendValue(&setDayContainer, setStartContainer);
		protocol.destroyValueData(setStartContainer);

		SRscpValue setStopContainer;
		protocol.createContainerValue(&setStopContainer,
					  TAG_EMS_IDLE_PERIOD_END);
		protocol.appendValue(&setStopContainer,
				 TAG_EMS_IDLE_PERIOD_MINUTE, (uint8_t)42);
		protocol.appendValue(&setStopContainer,
				 TAG_EMS_IDLE_PERIOD_HOUR, (uint8_t)12);
		protocol.appendValue(&setDayContainer, setStopContainer);
		protocol.destroyValueData(setStopContainer);

	    protocol.appendValue(&setTimeContainer, setDayContainer);
	    protocol.destroyValueData(setDayContainer);

	protocol.appendValue(&rootValue, setTimeContainer);
	protocol.destroyValueData(setTimeContainer);
    }

    // create buffer frame to send data to the S10
    protocol.createFrameAsBuffer(frameBuffer, rootValue.data, rootValue.length, true);	// true to calculate CRC on for transfer
    // the root value object should be destroyed after the data is copied into the frameBuffer and is not needed anymore
    protocol.destroyValueData(rootValue);

    return 0;
}

// Assembly:
pushq %rbp
pushq %r14
pushq %rbx
subq $0x60, %rsp
movl %esi, %ebp
movq %rdi, %rbx
movq %rsp, %r14
movq %r14, %rdi
callq 0x4380
leaq 0xc(%rsp), %rdx
andl $0x0, (%rdx)
leaq 0x31(%rsp), %rsi
movq %r14, %rdi
callq 0x3e06
leaq 0xd15b(%rip), %rdi      # 0xf680
callq 0x2210
testb $0x2, %bpl
je 0x25f4
leaq 0xc(%rsp), %rdx
movl $0x1000001, (%rdx)      # imm = 0x1000001
movq %rsp, %rdi
leaq 0x31(%rsp), %rsi
callq 0x3ec2
leaq 0xc(%rsp), %rdx
movl $0x1000002, (%rdx)      # imm = 0x1000002
movq %rsp, %rdi
leaq 0x31(%rsp), %rsi
callq 0x3ec2
leaq 0xc(%rsp), %rdx
movl $0x1000003, (%rdx)      # imm = 0x1000003
movq %rsp, %rdi
leaq 0x31(%rsp), %rsi
callq 0x3ec2
leaq 0xc(%rsp), %rdx
movl $0x1000004, (%rdx)      # imm = 0x1000004
movq %rsp, %rdi
leaq 0x31(%rsp), %rsi
callq 0x3ec2
leaq 0xc(%rsp), %rdx
movl $0x1000005, (%rdx)      # imm = 0x1000005
movq %rsp, %rdi
leaq 0x31(%rsp), %rsi
callq 0x3ec2
leaq 0xc(%rsp), %rdx
movl $0x100008b, (%rdx)      # imm = 0x100008B
movq %rsp, %rdi
leaq 0x31(%rsp), %rsi
callq 0x3ec2
leaq 0xc(%rsp), %rdx
movl $0x1000040, (%rdx)      # imm = 0x1000040
movq %rsp, %rdi
leaq 0x31(%rsp), %rsi
callq 0x3ec2
leaq 0xc(%rsp), %rdx
movl $0x1000011, (%rdx)      # imm = 0x1000011
movq %rsp, %rdi
leaq 0x31(%rsp), %rsi
callq 0x3ec2
testb $0x4, %bpl
je 0x2612
leaq 0xc(%rsp), %rdx
movl $0x1000080, (%rdx)      # imm = 0x1000080
movq %rsp, %rdi
leaq 0x31(%rsp), %rsi
callq 0x3ec2
testb $0x1, %bpl
je 0x26eb
leaq 0x20(%rsp), %rdx
movl $0x3040000, (%rdx)      # imm = 0x3040000
movq %rsp, %rdi
leaq 0xc(%rsp), %rsi
callq 0x3e06
leaq 0x20(%rsp), %rdx
movl $0x3040001, (%rdx)      # imm = 0x3040001
leaq 0x40(%rsp), %rcx
movb $0x0, (%rcx)
movq %rsp, %rdi
leaq 0xc(%rsp), %rsi
callq 0x3ee0
leaq 0x20(%rsp), %rdx
movl $0x3000001, (%rdx)      # imm = 0x3000001
movq %rsp, %rdi
leaq 0xc(%rsp), %rsi
callq 0x3ec2
leaq 0x20(%rsp), %rdx
movl $0x3000002, (%rdx)      # imm = 0x3000002
movq %rsp, %rdi
leaq 0xc(%rsp), %rsi
callq 0x3ec2
leaq 0x20(%rsp), %rdx
movl $0x3000003, (%rdx)      # imm = 0x3000003
movq %rsp, %rdi
leaq 0xc(%rsp), %rsi
callq 0x3ec2
leaq 0x20(%rsp), %rdx
movl $0x300000a, (%rdx)      # imm = 0x300000A
movq %rsp, %rdi
leaq 0xc(%rsp), %rsi
callq 0x3ec2
leaq 0x20(%rsp), %rdx
movl $0x300000b, (%rdx)      # imm = 0x300000B
movq %rsp, %rdi
leaq 0xc(%rsp), %rsi
callq 0x3ec2
movq %rsp, %rdi
leaq 0x31(%rsp), %rsi
leaq 0xc(%rsp), %rdx
callq 0x3e62
movq %rsp, %rdi
leaq 0xc(%rsp), %rsi
callq 0x4fe4
testb $0x8, %bpl
je 0x2750
movl %ebp, %eax
shrb $0x4, %al
andb $0x1, %al
movb %al, 0x40(%rsp)
leaq 0x20(%rsp), %rdx
movl $0x100008c, (%rdx)      # imm = 0x100008C
movq %rsp, %rdi
leaq 0xc(%rsp), %rsi
callq 0x3e06
leaq 0x20(%rsp), %rdx
movl $0x1000105, (%rdx)      # imm = 0x1000105
movq %rsp, %rdi
leaq 0xc(%rsp), %rsi
leaq 0x40(%rsp), %rcx
callq 0x3ee0
movq %rsp, %rdi
leaq 0x31(%rsp), %rsi
leaq 0xc(%rsp), %rdx
callq 0x3e62
movq %rsp, %rdi
leaq 0xc(%rsp), %rsi
callq 0x4fe4
testb $0x20, %bpl
je 0x2916
leaq 0x20(%rsp), %rdx
movl $0x1000081, (%rdx)      # imm = 0x1000081
movq %rsp, %rdi
leaq 0xc(%rsp), %rsi
callq 0x3e06
leaq 0x40(%rsp), %rdx
movl $0x1000082, (%rdx)      # imm = 0x1000082
movq %rsp, %rdi
leaq 0x20(%rsp), %rsi
callq 0x3e06
leaq 0x40(%rsp), %rdx
movl $0x1000083, (%rdx)      # imm = 0x1000083
leaq 0x50(%rsp), %rcx
movb $0x0, (%rcx)
movq %rsp, %rdi
leaq 0x20(%rsp), %rsi
callq 0x3ee0
leaq 0x40(%rsp), %rdx
movl $0x1000084, (%rdx)      # imm = 0x1000084
leaq 0x50(%rsp), %rcx
movb $0x1, (%rcx)
movq %rsp, %rdi
leaq 0x20(%rsp), %rsi
callq 0x3ee0
leaq 0x40(%rsp), %rdx
movl $0x1000089, (%rdx)      # imm = 0x1000089
leaq 0x50(%rsp), %rcx
movb $0x1, (%rcx)
movq %rsp, %rdi
leaq 0x20(%rsp), %rsi
callq 0x3efc
leaq 0x50(%rsp), %rdx
movl $0x1000085, (%rdx)      # imm = 0x1000085
movq %rsp, %rdi
leaq 0x40(%rsp), %rsi
callq 0x3e06
leaq 0x50(%rsp), %rdx
movl $0x1000088, (%rdx)      # imm = 0x1000088
leaq 0x1c(%rsp), %rcx
movb $0x0, (%rcx)
movq %rsp, %rdi
leaq 0x40(%rsp), %rsi
callq 0x3ee0
leaq 0x50(%rsp), %rdx
movl $0x1000087, (%rdx)      # imm = 0x1000087
leaq 0x1c(%rsp), %rcx
movb $0xb, (%rcx)
movq %rsp, %rdi
leaq 0x40(%rsp), %rsi
callq 0x3ee0
movq %rsp, %rdi
leaq 0x20(%rsp), %rsi
leaq 0x40(%rsp), %rdx
callq 0x3e62
movq %rsp, %rdi
leaq 0x40(%rsp), %rsi
callq 0x4fe4
leaq 0x1c(%rsp), %rdx
movl $0x1000086, (%rdx)      # imm = 0x1000086
movq %rsp, %rdi
leaq 0x50(%rsp), %rsi
callq 0x3e06
leaq 0x1c(%rsp), %rdx
movl $0x1000088, (%rdx)      # imm = 0x1000088
leaq 0xb(%rsp), %rcx
movb $0x2a, (%rcx)
movq %rsp, %rdi
leaq 0x50(%rsp), %rsi
callq 0x3ee0
leaq 0x1c(%rsp), %rdx
movl $0x1000087, (%rdx)      # imm = 0x1000087
leaq 0xb(%rsp), %rcx
movb $0xc, (%rcx)
movq %rsp, %rdi
leaq 0x50(%rsp), %rsi
callq 0x3ee0
movq %rsp, %rdi
leaq 0x20(%rsp), %rsi
leaq 0x50(%rsp), %rdx
callq 0x3e62
movq %rsp, %rdi
leaq 0x50(%rsp), %rsi
callq 0x4fe4
movq %rsp, %rdi
leaq 0xc(%rsp), %rsi
leaq 0x20(%rsp), %rdx
callq 0x3e62
movq %rsp, %rdi
leaq 0x20(%rsp), %rsi
callq 0x4fe4
movq %rsp, %rdi
leaq 0x31(%rsp), %rsi
leaq 0xc(%rsp), %rdx
callq 0x3e62
movq %rsp, %rdi
leaq 0xc(%rsp), %rsi
callq 0x4fe4
movq 0x38(%rsp), %rdx
movzwl 0x36(%rsp), %ecx
movq %rsp, %rdi
pushq $0x1
popq %r8
movq %rbx, %rsi
callq 0x445c
movq %rsp, %rdi
leaq 0x31(%rsp), %rsi
callq 0x4fe4
movq %rsp, %rdi
callq 0x438c
xorl %eax, %eax
addq $0x60, %rsp
popq %rbx
popq %r14
popq %rbp
retq
jmp 0x2993
jmp 0x2993
jmp 0x2993
jmp 0x2993
jmp 0x2993
jmp 0x2993
jmp 0x2993
jmp 0x2993
jmp 0x2993
jmp 0x2993
jmp 0x2993
jmp 0x2993
jmp 0x2993
jmp 0x2993
jmp 0x2993
jmp 0x2993
jmp 0x2993
jmp 0x2993
jmp 0x2993
jmp 0x2993
jmp 0x2993
jmp 0x2993
jmp 0x2993
jmp 0x2993
jmp 0x2993
jmp 0x2993
jmp 0x2993
jmp 0x2993
jmp 0x2993
jmp 0x2993
jmp 0x2993
jmp 0x2993
jmp 0x2993
jmp 0x2993
movq %rax, %rbx
movq %rsp, %rdi
callq 0x438c
movq %rbx, %rdi
callq 0x2280