// File: /workspace/github_cpp_makefile_star2_O3/rellla[P]E3DC-Rscp/RscpMain.cpp
// Function: handleResponseValue(RscpProtocol*, SRscpValue*, int*)
int handleResponseValue(RscpProtocol * protocol, SRscpValue * response,
			int *isAuthRequest)
{
    // check if any of the response has the error flag set and react accordingly
    if (response->dataType == RSCP::eTypeError) {
	// handle error for example access denied errors
	uint32_t uiErrorCode = protocol->getValueAsUInt32(response);
	printf("Tag 0x%08X received error code %u.\n",
	       response->tag, uiErrorCode);
	return -1;
    }
    // check the SRscpValue TAG to detect which response it is
    switch (response->tag) {
    case TAG_RSCP_AUTHENTICATION:{
	    // It is possible to check the response->dataType value to detect correct data type
	    // and call the correct function. If data type is known,
	    // the correct function can be called directly like in this case.
	    uint8_t ucAccessLevel = protocol->getValueAsUChar8(response);
	    if (ucAccessLevel > 0) {
		iAuthenticated = 1;
		*isAuthRequest = 1;
	    }
	    printf("RSCP authentitication level %i\n", ucAccessLevel);
	    break;
	}
    case TAG_EMS_POWER_PV:{
	    // response for TAG_EMS_REQ_POWER_PV
	    int32_t iPower = protocol->getValueAsInt32(response);
	    printf("EMS PV power is %i W\n", iPower);
	    break;
	}
    case TAG_EMS_POWER_BAT:{
	    // response for TAG_EMS_REQ_POWER_BAT
	    int32_t iPower = protocol->getValueAsInt32(response);
	    printf("EMS BAT power is %i W\n", iPower);
	    break;
	}
    case TAG_EMS_POWER_HOME:{
	    // response for TAG_EMS_REQ_POWER_HOME
	    int32_t iPower = protocol->getValueAsInt32(response);
	    printf("EMS house power is %i W\n", iPower);
	    break;
	}
    case TAG_EMS_POWER_GRID:{
	    // response for TAG_EMS_REQ_POWER_GRID
	    int32_t iPower = protocol->getValueAsInt32(response);
	    printf("EMS grid power is %i W\n", iPower);
	    break;
	}
    case TAG_EMS_POWER_ADD:{
	    // response for TAG_EMS_REQ_POWER_ADD
	    int32_t iPower = protocol->getValueAsInt32(response);
	    printf("EMS add power meter power is %i W\n", iPower);
	    break;
	}
    case TAG_BAT_DATA:{
	    // response for TAG_REQ_BAT_DATA
	    std::vector < SRscpValue > batteryData =
		protocol->getValueAsContainer(response);
	    for (size_t i = 0; i < batteryData.size(); ++i) {
		if (batteryData[i].dataType == RSCP::eTypeError) {
		    // handle error for example access denied errors
		    uint32_t uiErrorCode =
			protocol->getValueAsUInt32(&batteryData[i]);
		    printf("Tag 0x%08X received error code %u.\n",
			   batteryData[i].tag, uiErrorCode);
		    return -1;
		}
		handleResponseBatData(protocol, &batteryData[i]);
	    }
	    protocol->destroyValueData(batteryData);
	    break;
	}
    case TAG_EMS_GET_POWER_SETTINGS:{
	    // response for TAG_EMS_REQ_GET_POWER_SETTINGS
	    std::vector < SRscpValue > emsData =
		protocol->getValueAsContainer(response);
	    for (size_t i = 0; i < emsData.size(); ++i) {
		if (emsData[i].dataType == RSCP::eTypeError) {
		    // handle error for example access denied errors
		    uint32_t uiErrorCode =
			protocol->getValueAsUInt32(&emsData[i]);
		    printf("Tag 0x%08X received error code %u.\n",
			   emsData[i].tag, uiErrorCode);
		    return -1;
		}
		// check each ems power settings sub tag
		switch (emsData[i].tag) {
		case TAG_EMS_POWER_LIMITS_USED:{
			int8_t weather_en =
			    protocol->getValueAsInt32(&emsData[i]);
			printf("EMS power limits used is %i.\n",
			       weather_en);
			break;
		    }
		case TAG_EMS_MAX_CHARGE_POWER:{
			int8_t weather_en =
			    protocol->getValueAsInt32(&emsData[i]);
			printf("EMS max charge power is %i.\n",
			       weather_en);
			break;
		    }
		case TAG_EMS_MAX_DISCHARGE_POWER:{
			int8_t weather_en =
			    protocol->getValueAsInt32(&emsData[i]);
			printf("EMS max discharge power is %i.\n",
			       weather_en);
			break;
		    }
		case TAG_EMS_DISCHARGE_START_POWER:{
			int8_t weather_en =
			    protocol->getValueAsInt32(&emsData[i]);
			printf("EMS discharge start power is %i.\n",
			       weather_en);
			break;
		    }
		case TAG_EMS_POWERSAVE_ENABLED:{
			int8_t weather_en =
			    protocol->getValueAsInt32(&emsData[i]);
			printf("EMS powersave enabled is %i.\n",
			       weather_en);
			break;
		    }
		case TAG_EMS_WEATHER_REGULATED_CHARGE_ENABLED:{
			int8_t weather_en =
			    protocol->getValueAsInt32(&emsData[i]);
			printf
			    ("EMS weather regulated charge enabled is %i.\n",
			     weather_en);
			break;
		    }
		case TAG_EMS_UNKNOWN:{
			int8_t weather_en =
			    protocol->getValueAsInt32(&emsData[i]);
			printf("EMS unknown is %i.\n", weather_en);
			break;
		    }
		    // ...
		default:
		    // default behaviour
		    printf("Unknown ems tag %08X\n", emsData[i].tag);
		    break;
		}
	    }
	    protocol->destroyValueData(emsData);
	    break;
	}
    case TAG_EMS_SET_POWER_SETTINGS:{
	    // resposne for TAG_EMS_REQ_SET_POWER_SETTINGS
	    std::vector < SRscpValue > emsData =
		protocol->getValueAsContainer(response);
	    for (size_t i = 0; i < emsData.size(); ++i) {
		if (emsData[i].dataType == RSCP::eTypeError) {
		    // handle error for example access denied errors
		    uint32_t uiErrorCode =
			protocol->getValueAsUInt32(&emsData[i]);
		    printf("Tag 0x%08X received error code %u.\n",
			   emsData[i].tag, uiErrorCode);
		    return -1;
		}
		// check each battery sub tag
		switch (emsData[i].tag) {
		case TAG_EMS_RES_WEATHER_REGULATED_CHARGE_ENABLED:{
			int8_t weather_en =
			    protocol->getValueAsInt32(&emsData[i]);
			printf
			    ("Weather regulated charge response: %i\n",
			     weather_en);
			break;
		    }
		default:
		    // default behaviour
		    printf("Unknown ems tag %08X\n", emsData[i].tag);
		    break;
		}
	    }
	    protocol->destroyValueData(emsData);
	    break;
	}
    case TAG_EMS_GET_IDLE_PERIODS:{
	    // resposne for TAG_EMS_REQ_GET_IDLE_PERIODS
	    std::vector < SRscpValue > emsData =
		protocol->getValueAsContainer(response);
	    idle_period_t periods[14];
	    for (size_t i = 0; i < emsData.size(); ++i) {
		if (emsData[i].dataType == RSCP::eTypeError) {
		    // handle error for example access denied errors
		    uint32_t uiErrorCode =
			protocol->getValueAsUInt32(&emsData[i]);
		    printf("Tag 0x%08X received error code %u.\n",
			   emsData[i].tag, uiErrorCode);
		    return -1;
		}
		handleResponseEMSGetIdlePeriods(protocol, &emsData[i], &periods[i]);
	    }
	    break;
	}
    default:
	// default behavior
	uint8_t unknown = protocol->getValueAsUChar8(response);
	printf("Unknown tag %08X -> %i.\n", response->tag, unknown);
	break;
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rsi, %r14
cmpb $-0x1, 0x4(%rsi)
je 0x3415
movq %rdi, %rbx
movl (%r14), %esi
cmpl $0x180008a, %esi        # imm = 0x180008A
jg 0x3434
cmpl $0x1800080, %esi        # imm = 0x1800080
jne 0x397a
movq %rsp, %r15
movq %r15, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x4774
movq (%r15), %r14
cmpq %r14, 0x8(%r15)
je 0x38d2
leaq 0x20(%rsp), %r15
xorl %r12d, %r12d
movabsq $-0x1111111111111111, %r13 # imm = 0xEEEEEEEEEEEEEEEF
xorl %ebp, %ebp
cmpb $-0x1, 0x4(%r14,%r12)
je 0x3833
addq %r12, %r14
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x2dc5
incq %rbp
movq (%rsp), %r14
movq 0x8(%rsp), %rax
subq %r14, %rax
imulq %r13, %rax
addq $0x7, %r15
addq $0xf, %r12
cmpq %rax, %rbp
jb 0x33d3
jmp 0x38d2
movq 0x7(%r14), %rsi
testq %rsi, %rsi
je 0x34a6
movzwl 0x5(%r14), %edx
cmpq $0x4, %rdx
jb 0x34aa
movl (%rsi), %edx
jmp 0x34bf
cmpl $0x180008b, %esi        # imm = 0x180008B
je 0x34d5
cmpl $0x180008c, %esi        # imm = 0x180008C
je 0x376c
cmpl $0x3840000, %esi        # imm = 0x3840000
jne 0x3998
leaq 0x20(%rsp), %r15
movq %r15, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x4774
movq (%r15), %r14
cmpq %r14, 0x8(%r15)
je 0x384d
cmpb $-0x1, 0x4(%r14)
jne 0x39a0
movq 0x7(%r14), %rsi
testq %rsi, %rsi
je 0x38e8
movzwl 0x5(%r14), %edx
cmpq $0x4, %rdx
jb 0x38ec
movl (%rsi), %edx
jmp 0x38ff
xorl %edx, %edx
jmp 0x34bf
leaq 0x20(%rsp), %rbx
movl $0x0, (%rbx)
movq %rbx, %rdi
callq 0x20f0
movl (%rbx), %edx
movl (%r14), %esi
leaq 0xcbe7(%rip), %rdi      # 0x100b0
xorl %eax, %eax
callq 0x2040
jmp 0x3963
leaq 0x20(%rsp), %r15
movq %r15, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x4774
movq (%r15), %r12
cmpq %r12, 0x8(%r15)
je 0x375a
movl $0x7, %r14d
movabsq $-0x1111111111111111, %r15 # imm = 0xEEEEEEEEEEEEEEEF
leaq 0xcb54(%rip), %r13      # 0x10060
xorl %ebp, %ebp
cmpb $-0x1, -0x3(%r12,%r14)
je 0x3863
movl -0x7(%r12,%r14), %esi
leal -0x1000100(%rsi), %eax
cmpl $0x6, %eax
ja 0x35c8
movslq (%r13,%rax,4), %rax
addq %r13, %rax
jmpq *%rax
movq (%r12,%r14), %rsi
testq %rsi, %rsi
je 0x3640
movzwl -0x2(%r12,%r14), %edx
cmpq $0x4, %rdx
jb 0x36bc
movl (%rsi), %eax
jmp 0x36ce
movq (%r12,%r14), %rsi
testq %rsi, %rsi
je 0x3634
movzwl -0x2(%r12,%r14), %edx
cmpq $0x4, %rdx
jb 0x365c
movl (%rsi), %eax
jmp 0x366e
movq (%r12,%r14), %rsi
testq %rsi, %rsi
je 0x3638
movzwl -0x2(%r12,%r14), %edx
cmpq $0x4, %rdx
jb 0x367d
movl (%rsi), %eax
jmp 0x368f
movq (%r12,%r14), %rsi
testq %rsi, %rsi
je 0x363c
movzwl -0x2(%r12,%r14), %edx
cmpq $0x4, %rdx
jb 0x369e
movl (%rsi), %eax
jmp 0x36b0
leaq 0xce74(%rip), %rdi      # 0x10443
jmp 0x3732
movq (%r12,%r14), %rsi
testq %rsi, %rsi
je 0x3647
movzwl -0x2(%r12,%r14), %edx
cmpq $0x4, %rdx
jb 0x36da
movl (%rsi), %eax
jmp 0x36ec
movq (%r12,%r14), %rsi
testq %rsi, %rsi
je 0x364e
movzwl -0x2(%r12,%r14), %edx
cmpq $0x4, %rdx
jb 0x36f8
movl (%rsi), %eax
jmp 0x370a
movq (%r12,%r14), %rsi
testq %rsi, %rsi
je 0x3655
movzwl -0x2(%r12,%r14), %edx
cmpq $0x4, %rdx
jb 0x3716
movl (%rsi), %eax
jmp 0x3728
xorl %eax, %eax
jmp 0x366e
xorl %eax, %eax
jmp 0x368f
xorl %eax, %eax
jmp 0x36b0
xorl %eax, %eax
jmp 0x36ce
xorl %eax, %eax
jmp 0x36ec
xorl %eax, %eax
jmp 0x370a
xorl %eax, %eax
jmp 0x3728
movl $0x0, (%rsp)
movq %rsp, %rdi
callq 0x20f0
movl (%rsp), %eax
movsbl %al, %esi
leaq 0xcd6c(%rip), %rdi      # 0x103e4
jmp 0x3732
movl $0x0, (%rsp)
movq %rsp, %rdi
callq 0x20f0
movl (%rsp), %eax
movsbl %al, %esi
leaq 0xcd09(%rip), %rdi      # 0x103a2
jmp 0x3732
movl $0x0, (%rsp)
movq %rsp, %rdi
callq 0x20f0
movl (%rsp), %eax
movsbl %al, %esi
leaq 0xcd08(%rip), %rdi      # 0x103c2
jmp 0x3732
movl $0x0, (%rsp)
movq %rsp, %rdi
callq 0x20f0
movl (%rsp), %eax
movsbl %al, %esi
leaq 0xcc8f(%rip), %rdi      # 0x10367
jmp 0x3732
movl $0x0, (%rsp)
movq %rsp, %rdi
callq 0x20f0
movl (%rsp), %eax
movsbl %al, %esi
leaq 0xcc8f(%rip), %rdi      # 0x10385
jmp 0x3732
movl $0x0, (%rsp)
movq %rsp, %rdi
callq 0x20f0
movl (%rsp), %eax
movsbl %al, %esi
leaq 0xccee(%rip), %rdi      # 0x10402
jmp 0x3732
movl $0x0, (%rsp)
movq %rsp, %rdi
callq 0x20f0
movl (%rsp), %eax
movsbl %al, %esi
leaq 0xccfd(%rip), %rdi      # 0x1042f
xorl %eax, %eax
callq 0x2040
incq %rbp
movq 0x20(%rsp), %r12
movq 0x28(%rsp), %rax
subq %r12, %rax
imulq %r15, %rax
addq $0xf, %r14
cmpq %rax, %rbp
jb 0x350e
leaq 0x20(%rsp), %rsi
movq %rbx, %rdi
callq 0x52d8
jmp 0x394c
leaq 0x20(%rsp), %r15
movq %r15, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x4774
movq (%r15), %r14
cmpq %r14, 0x8(%r15)
je 0x3821
movl $0x7, %r13d
movabsq $-0x1111111111111111, %rbp # imm = 0xEEEEEEEEEEEEEEEF
leaq 0xcca0(%rip), %r12      # 0x10443
xorl %r15d, %r15d
cmpb $-0x1, -0x3(%r14,%r13)
je 0x3887
movl -0x7(%r14,%r13), %esi
cmpl $0x1800105, %esi        # imm = 0x1800105
jne 0x37d8
movq (%r14,%r13), %rsi
testq %rsi, %rsi
je 0x37dd
movzwl -0x2(%r14,%r13), %edx
cmpq $0x4, %rdx
jb 0x37e1
movl (%rsi), %eax
jmp 0x37f3
movq %r12, %rdi
jmp 0x37fd
xorl %eax, %eax
jmp 0x37f3
movl $0x0, (%rsp)
movq %rsp, %rdi
callq 0x20f0
movl (%rsp), %eax
movsbl %al, %esi
leaq 0xcc5c(%rip), %rdi      # 0x10459
xorl %eax, %eax
callq 0x2040
incq %r15
movq 0x20(%rsp), %r14
movq 0x28(%rsp), %rax
subq %r14, %rax
imulq %rbp, %rax
addq $0xf, %r13
cmpq %rax, %r15
jb 0x37a6
leaq 0x20(%rsp), %rsi
movq %rbx, %rdi
callq 0x52d8
jmp 0x394c
movq 0x7(%r14,%r12), %rsi
testq %rsi, %rsi
je 0x385f
movzwl 0x5(%r14,%r12), %edx
cmpq $0x4, %rdx
jb 0x38a7
movl (%rsi), %edx
jmp 0x38bc
leaq 0x20(%rsp), %rsi
movq %rbx, %rdi
callq 0x52d8
jmp 0x394c
xorl %edx, %edx
jmp 0x38bc
movq (%r12,%r14), %rsi
testq %rsi, %rsi
je 0x3904
movzwl -0x2(%r12,%r14), %edx
cmpq $0x4, %rdx
jb 0x390c
movl (%rsi), %edx
jmp 0x391f
movq (%r14,%r13), %rsi
testq %rsi, %rsi
je 0x3908
movzwl -0x2(%r14,%r13), %edx
cmpq $0x4, %rdx
jb 0x3926
movl (%rsi), %edx
jmp 0x3939
leaq 0x1c(%rsp), %rbx
movl $0x0, (%rbx)
movq %rbx, %rdi
callq 0x20f0
movl (%rbx), %edx
movl (%r14,%r12), %esi
leaq 0xc7e9(%rip), %rdi      # 0x100b0
xorl %eax, %eax
callq 0x2040
movq (%rsp), %r14
testq %r14, %r14
je 0x3963
movq 0x10(%rsp), %rsi
subq %r14, %rsi
movq %r14, %rdi
jmp 0x395e
xorl %edx, %edx
jmp 0x38ff
movq %rsp, %rbx
movl $0x0, (%rbx)
movq %rbx, %rdi
callq 0x20f0
movl (%rbx), %edx
movl (%r14), %esi
jmp 0x393e
xorl %edx, %edx
jmp 0x391f
xorl %edx, %edx
jmp 0x3939
movq %rsp, %rbx
movl $0x0, (%rbx)
movq %rbx, %rdi
callq 0x20f0
movl (%rbx), %edx
movl -0x7(%r12,%r14), %esi
jmp 0x393e
movq %rsp, %rbx
movl $0x0, (%rbx)
movq %rbx, %rdi
callq 0x20f0
movl (%rbx), %edx
movl -0x7(%r14,%r13), %esi
leaq 0xc76b(%rip), %rdi      # 0x100b0
xorl %eax, %eax
callq 0x2040
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x3963
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x2160
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x800001, %esi         # imm = 0x800001
jne 0x398d
movq %r14, %rdi
movq %rdx, %rsi
callq 0x22c3
leal -0x1800001(%rsi), %eax
cmpl $0x4, %eax
jbe 0x39ab
movq %r14, %rdi
callq 0x22fd
movq %rbx, %rdi
movq %r14, %rsi
callq 0x329f
leaq 0xc69a(%rip), %rcx      # 0x1004c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq %rbx, %rdi
movq %r14, %rsi
callq 0x4856
leaq 0xc919(%rip), %rdi      # 0x102e6
jmp 0x3a1d
movq %rbx, %rdi
movq %r14, %rsi
callq 0x4856
leaq 0xc91b(%rip), %rdi      # 0x102fc
jmp 0x3a1d
movq %rbx, %rdi
movq %r14, %rsi
callq 0x4856
leaq 0xc91e(%rip), %rdi      # 0x10313
jmp 0x3a1d
movq %rbx, %rdi
movq %r14, %rsi
callq 0x4856
leaq 0xc923(%rip), %rdi      # 0x1032c
jmp 0x3a1d
movq %rbx, %rdi
movq %r14, %rsi
callq 0x4856
leaq 0xc927(%rip), %rdi      # 0x10344
movl %eax, %esi
xorl %eax, %eax
callq 0x2040
jmp 0x3a2c
jmp 0x3a2c
jmp 0x3a2c
movq %rax, %rbx
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x3a59
movq 0x30(%rsp), %rsi
jmp 0x3a51
movq %rax, %rbx
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x3a59
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x2160
movq %rbx, %rdi
callq 0x2240