Skip to content

Commit

Permalink
refactor: adding decoding helper functions
Browse files Browse the repository at this point in the history
Signed-off-by: aw <aw@pionix.de>
  • Loading branch information
SebaLukas committed Jun 19, 2024
1 parent 88590c5 commit 3e503ea
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 48 deletions.
26 changes: 8 additions & 18 deletions tests/ISO20/ac_charge_loop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ SCENARIO("Encode and decode AC charge loop message") {
charge_loop.BPT_Scheduled_AC_CLReqControlMode.EVPresentActivePower = {3, 200};

THEN("It should be encoded succussfully") {
const auto result = encode_iso20_and_test(request, doc_raw, sizeof(doc_raw));
const auto result = encode_iso20_and_compare(request, doc_raw, sizeof(doc_raw));

REQUIRE(result.encoding_successful);
REQUIRE(result.bitstream_match);
Expand Down Expand Up @@ -98,7 +98,7 @@ SCENARIO("Encode and decode AC charge loop message") {
charge_loop.BPT_Scheduled_AC_CLResControlMode = {};

THEN("It should be encoded succussfully") {
const auto result = encode_iso20_and_test(response, doc_raw, sizeof(doc_raw));
const auto result = encode_iso20_and_compare(response, doc_raw, sizeof(doc_raw));

REQUIRE(result.encoding_successful);
REQUIRE(result.bitstream_match);
Expand All @@ -111,16 +111,11 @@ SCENARIO("Encode and decode AC charge loop message") {
uint8_t doc_raw[] = "\x80\x08\x04\x1e\x98\x69\xd6\xa6\x1d\xc1\xef\x89\x5b"
"\x9b\x4a\x80\x62\x83\x24\x18\x64\x00\x96";

exi_bitstream_t exi_stream_in;
size_t pos1 = 0;
int errn = 0;

exi_bitstream_init(&exi_stream_in, reinterpret_cast<uint8_t*>(doc_raw), sizeof(doc_raw), pos1, nullptr);

THEN("It should be decoded succussfully") {
iso20_ac_exiDocument request;
const auto result = decode_iso20<iso20_ac_exiDocument>(doc_raw, sizeof(doc_raw));
REQUIRE(result.decoding_successful);

REQUIRE(decode_iso20_ac_exiDocument(&exi_stream_in, &request) == 0);
const auto& request = result.value;

REQUIRE(request.AC_ChargeLoopReq_isUsed == true);
// Check Header
Expand Down Expand Up @@ -148,16 +143,11 @@ SCENARIO("Encode and decode AC charge loop message") {
uint8_t doc_raw[] = "\x80\x0c\x04\x1e\x98\x69\xd6\xa6\x1d\xc1\xef"
"\x89\x5b\x9b\x4a\x80\x62\x00\x59\x00";

exi_bitstream_t exi_stream_in;
size_t pos1 = 0;
int errn = 0;

exi_bitstream_init(&exi_stream_in, reinterpret_cast<uint8_t*>(doc_raw), sizeof(doc_raw), pos1, nullptr);

THEN("It should be decoded succussfully") {
iso20_ac_exiDocument request;
const auto result = decode_iso20<iso20_ac_exiDocument>(doc_raw, sizeof(doc_raw));
REQUIRE(result.decoding_successful);

REQUIRE(decode_iso20_ac_exiDocument(&exi_stream_in, &request) == 0);
const auto& request = result.value;

REQUIRE(request.AC_ChargeLoopRes_isUsed == true);
// Check Header
Expand Down
40 changes: 31 additions & 9 deletions tests/ISO20/codec_helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@

#include <vector>

#include <cbv2g/iso_20/iso20_AC_Decoder.h>
#include <cbv2g/iso_20/iso20_AC_Encoder.h>
#include <cbv2g/iso_20/iso20_DC_Decoder.h>
#include <cbv2g/iso_20/iso20_DC_Encoder.h>

template <typename DocType>
static EncodingTestResult encode(int (*encode_func)(exi_bitstream_t*, DocType*), DocType& request,
const uint8_t* compare_data, std::size_t length) {
static EncodingResult encode(int (*encode_func)(exi_bitstream_t*, DocType*), const DocType& request,
const uint8_t* compare_data, std::size_t length) {
// FIXME (aw): what general size to take here?
uint8_t stream[256] = {};
exi_bitstream_t exi_stream_in;
size_t pos1 = 0;
int errn = 0;

exi_bitstream_init(&exi_stream_in, stream, sizeof(stream), pos1, nullptr);

if (0 != encode_func(&exi_stream_in, &request)) {
if (0 != encode_func(&exi_stream_in, const_cast<DocType*>(&request))) {
return {false, false};
}

Expand All @@ -29,13 +30,34 @@ static EncodingTestResult encode(int (*encode_func)(exi_bitstream_t*, DocType*),
}

template <>
EncodingTestResult encode_iso20_and_test(iso20_ac_exiDocument& request, const uint8_t* compare_data,
std::size_t length) {
EncodingResult encode_iso20_and_compare(const iso20_ac_exiDocument& request, const uint8_t* compare_data,
std::size_t length) {
return encode(&encode_iso20_ac_exiDocument, request, compare_data, length);
}

template <>
EncodingTestResult encode_iso20_and_test(iso20_dc_exiDocument& request, const uint8_t* compare_data,
std::size_t length) {
EncodingResult encode_iso20_and_compare(const iso20_dc_exiDocument& request, const uint8_t* compare_data,
std::size_t length) {
return encode(&encode_iso20_dc_exiDocument, request, compare_data, length);
}
}

template <typename DocType>
DecodingResult<DocType> decode(int (*decode_func)(exi_bitstream_t*, DocType*), const uint8_t* raw_data,
std::size_t length) {
exi_bitstream_t exi_stream_in;
size_t pos1 = 0;

exi_bitstream_init(&exi_stream_in, const_cast<uint8_t*>(raw_data), length, pos1, nullptr);

DecodingResult<DocType> result;
result.decoding_successful = (decode_func(&exi_stream_in, &result.value) == 0);
return result;
}

template <> DecodingResult<iso20_ac_exiDocument> decode_iso20(const uint8_t* raw_data, std::size_t length) {
return decode(&decode_iso20_ac_exiDocument, raw_data, length);
}

template <> DecodingResult<iso20_dc_exiDocument> decode_iso20(const uint8_t* raw_data, std::size_t length) {
return decode(&decode_iso20_dc_exiDocument, raw_data, length);
}
14 changes: 9 additions & 5 deletions tests/ISO20/codec_helper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@
#include <cstdint>
#include <memory>

#include <cbv2g/iso_20/iso20_AC_Encoder.h>

struct EncodingTestResult {
struct EncodingResult {
bool encoding_successful;

Check notice on line 7 in tests/ISO20/codec_helper.hpp

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

tests/ISO20/codec_helper.hpp#L7

struct member 'EncodingTestResult::encoding_successful' is never used.
bool bitstream_match;

Check notice on line 8 in tests/ISO20/codec_helper.hpp

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

tests/ISO20/codec_helper.hpp#L8

struct member 'EncodingTestResult::bitstream_match' is never used.
};

// FIXME (aw): the first parameter should be const ref, but the API of cbexigen doesn't respect that
template <typename DocType>
EncodingTestResult encode_iso20_and_test(DocType&, const uint8_t* compare_data, std::size_t length);
EncodingResult encode_iso20_and_compare(const DocType&, const uint8_t* compare_data, std::size_t length);

template <typename DocType> struct DecodingResult {
bool decoding_successful;

Check notice on line 15 in tests/ISO20/codec_helper.hpp

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

tests/ISO20/codec_helper.hpp#L15

struct member 'DecodingResult::decoding_successful' is never used.
DocType value;
};

template <typename DocType> DecodingResult<DocType> decode_iso20(const uint8_t* raw_data, std::size_t length);
24 changes: 8 additions & 16 deletions tests/ISO20/dc_charge_loop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ SCENARIO("Encode and decode DC charge loop message") {
charge_loop.BPT_Scheduled_DC_CLReqControlMode.EVTargetVoltage = {0, 400};

THEN("It should be encoded succussfully") {
const auto result = encode_iso20_and_test(request, doc_raw, sizeof(doc_raw));
const auto result = encode_iso20_and_compare(request, doc_raw, sizeof(doc_raw));

REQUIRE(result.encoding_successful);
REQUIRE(result.bitstream_match);
Expand Down Expand Up @@ -127,7 +127,7 @@ SCENARIO("Encode and decode DC charge loop message") {
charge_loop.BPT_Scheduled_DC_CLResControlMode = {};

THEN("It should be encoded succussfully") {
const auto result = encode_iso20_and_test(response, doc_raw, sizeof(doc_raw));
const auto result = encode_iso20_and_compare(response, doc_raw, sizeof(doc_raw));

REQUIRE(result.encoding_successful);
REQUIRE(result.bitstream_match);
Expand All @@ -140,16 +140,12 @@ SCENARIO("Encode and decode DC charge loop message") {
uint8_t doc_raw[] = "\x80\x34\x04\x2d\x16\x6f\x29\xfb\x80\xea\x56\x0a"
"\xeb\xdb\xfb\x30\x62\x81\x00\x12\x00\x61\x64\x00"
"\x0a\x02\x00\x24\x00\xc8\x00";
exi_bitstream_t exi_stream_in;
size_t pos1 = 0;
int errn = 0;

exi_bitstream_init(&exi_stream_in, reinterpret_cast<uint8_t*>(doc_raw), sizeof(doc_raw), pos1, nullptr);

THEN("It should be decoded succussfully") {
iso20_dc_exiDocument request;
const auto result = decode_iso20<iso20_dc_exiDocument>(doc_raw, sizeof(doc_raw));
REQUIRE(result.decoding_successful);

REQUIRE(decode_iso20_dc_exiDocument(&exi_stream_in, &request) == 0);
const auto& request = result.value;

REQUIRE(request.DC_ChargeLoopReq_isUsed == true);
// Check Header
Expand Down Expand Up @@ -180,16 +176,12 @@ SCENARIO("Encode and decode DC charge loop message") {

uint8_t doc_raw[] = "\x80\x38\x04\x2d\x16\x6f\x29\xfb\x80\xea\x56\x0b\x7b\xdb\xfb"
"\x30\x62\x00\x63\xf0\x68\x07\x81\xfc\x28\x07\xc2\x22\x30";
exi_bitstream_t exi_stream_in;
size_t pos1 = 0;
int errn = 0;

exi_bitstream_init(&exi_stream_in, reinterpret_cast<uint8_t*>(doc_raw), sizeof(doc_raw), pos1, nullptr);

THEN("It should be decoded succussfully") {
iso20_dc_exiDocument request;
const auto result = decode_iso20<iso20_dc_exiDocument>(doc_raw, sizeof(doc_raw));
REQUIRE(result.decoding_successful);

REQUIRE(decode_iso20_dc_exiDocument(&exi_stream_in, &request) == 0);
const auto& request = result.value;

REQUIRE(request.DC_ChargeLoopRes_isUsed == true);
// Check Header
Expand Down

0 comments on commit 3e503ea

Please sign in to comment.