diff --git a/.arduino-ci.yaml b/.arduino-ci.yaml deleted file mode 100644 index d8c57d6..0000000 --- a/.arduino-ci.yaml +++ /dev/null @@ -1,16 +0,0 @@ -compile: - libraries: - - ArduinoLog - - TaskManagerIO - platforms: - - esp32 - -unittest: - libraries: - - ArduinoLog - platforms: - - uno - - due - - mega2560 - - leonardo - - esp32 diff --git a/.github/workflows/arduino_ci.yml b/.github/workflows/arduino_ci.yml deleted file mode 100644 index d6dc2a7..0000000 --- a/.github/workflows/arduino_ci.yml +++ /dev/null @@ -1,11 +0,0 @@ -name: Arduino_CI - -on: [push, pull_request] - -jobs: - arduino_ci: - runs-on: ubuntu-latest - - steps: - - uses: actions/checkout@v3 - - uses: Arduino-CI/action@stable-1.x \ No newline at end of file diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..b87cf0c --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,33 @@ +name: Build +on: [push] + +jobs: + build: + runs-on: ubuntu-latest + strategy: + matrix: + example: + - examples/AsyncDetectTag/AsyncDetectTag.ino + - examples/DetectAvailableReaders/DetectAvailableReaders.ino + - examples/SyncReadCardData/SyncReadCardData.ino + - examples/SyncReadReaderId/SyncReadReaderId.ino + - examples/SyncReadSerialNumber/SyncReadSerialNumber.ino + - examples/SyncReReadCardData/SyncReReadCardData.ino + - examples/SyncSetReaderId/SyncSetReaderId.ino + steps: + - uses: actions/checkout@v4 + - uses: actions/cache@v4 + with: + path: | + ~/.cache/pip + ~/.platformio/.cache + key: ${{ runner.os }}-pio + - uses: actions/setup-python@v5 + with: + python-version: '3.12' + - name: Install PlatformIO Core + run: pip install --upgrade platformio + - name: Build PlatformIO examples + run: pio ci --lib "." --project-conf=platformio.ini --environment=esp32dev + env: + PLATFORMIO_CI_SRC: ${{ matrix.example }} diff --git a/.github/workflows/doxygen.yml b/.github/workflows/doxygen.yml index d6645dd..f0b8f35 100644 --- a/.github/workflows/doxygen.yml +++ b/.github/workflows/doxygen.yml @@ -9,18 +9,20 @@ on: jobs: deploy: runs-on: ubuntu-latest + permissions: + contents: write steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 with: fetch-depth: 0 - name: Build - uses: mattnotmitt/doxygen-action@v1.9.5 + uses: mattnotmitt/doxygen-action@v1.9.8 with: doxyfile-path: 'docs/Doxyfile' - name: Deploy - uses: peaceiris/actions-gh-pages@v3 + uses: peaceiris/actions-gh-pages@v4 with: github_token: ${{ secrets.GITHUB_TOKEN }} publish_dir: ./docs/html diff --git a/README.md b/README.md index cba5417..9cd2403 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,9 @@ # RFID reader -[![Arduino_CI](https://github.com/JSC-electronics/rfid-reader/actions/workflows/arduino_ci.yml/badge.svg)](https://github.com/JSC-electronics/rfid-reader/actions/workflows/arduino_ci.yml) -[![Documentation](https://github.com/JSC-electronics/rfid-reader/actions/workflows/doxygen.yml/badge.svg)](https://github.com/JSC-electronics/rfid-reader/actions/workflows/doxygen.yml) -[![License: Apache 2.0](https://img.shields.io/badge/license-Apache--2.0-green.svg)](https://github.com/JSC-electronics/rfid-reader/blob/main/LICENSE) -[![GitHub release](https://img.shields.io/github/release/JSC-electronics/rfid-reader.svg?maxAge=3600)](https://github.com/JSC-electronics/rfid-reader/releases) -[![JSC electronics](https://img.shields.io/badge/JSC-electronics-green.svg)](https://www.jsce.cz/) +[![Build](https://github.com/JSC-TechMinds/rfid-reader/actions/workflows/build.yml/badge.svg)](https://github.com/JSC-TechMinds/rfid-reader/actions/workflows/build.yml) +[![Documentation](https://github.com/JSC-TechMinds/rfid-reader/actions/workflows/doxygen.yml/badge.svg)](https://github.com/JSC-TechMinds/rfid-reader/actions/workflows/doxygen.yml) +[![License: Apache 2.0](https://img.shields.io/badge/license-Apache--2.0-green.svg)](https://github.com/JSC-TechMinds/rfid-reader/blob/main/LICENSE) +[![GitHub release](https://img.shields.io/github/release/JSC-TechMinds/rfid-reader.svg?maxAge=3600)](https://github.com/JSC-TechMinds/rfid-reader/releases) +[![JSC TechMinds](https://img.shields.io/badge/JSC-TechMinds-green.svg)](https://www.jsctm.cz/) [![Donate](https://img.shields.io/badge/donate-PayPal-blueviolet.svg)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=SESX9ABM7V8KA&source=url) This is an Arduino library to support one specific RS485 RFID reader. There isn't much information about this model, so we include a complete datasheet and communication protocol specifications. You can find them [here](documentation). @@ -11,7 +11,7 @@ This is an Arduino library to support one specific RS485 RFID reader. There isn' ![image](https://user-images.githubusercontent.com/4460766/219572463-b0cf4d4c-910d-45bd-9526-941c661914de.png) ## Documentation -- [Extended Doxygen Documentation](https://jsc-electronics.github.io/rfid-reader) +- [Extended Doxygen Documentation](https://JSC-TechMinds.github.io/rfid-reader) - [Examples](examples) # Sync and async modes @@ -43,7 +43,7 @@ Besides these changes, the API is identical to the synchronous one. All the heav ## License -Copyright (c) JSC electronics. All rights reserved. +Copyright (c) JSC TechMinds. All rights reserved. Licensed under the [Apache-2.0](LICENSE) license. diff --git a/library.json b/library.json index ae4bd4f..1651425 100644 --- a/library.json +++ b/library.json @@ -7,20 +7,15 @@ "repository": { "type": "git", - "url": "https://github.com/JSC-electronics/rfid-reader" + "url": "https://github.com/JSC-TechMinds/rfid-reader" }, "authors": [ { - "name": "Vladimír Záhradník", - "email": "vladimir.zahradnik@jsce.cz", - "url": "https://github.com/vzahradnik", + "name": "JSC TechMinds", + "email": "development@jsctm.cz", + "url": "https://github.com/JSC-TechMinds", "maintainer": true - }, - { - "name": "JSC electronics", - "email": "development@jsce.cz", - "url": "https://github.com/jsc-electronics" } ], "dependencies": [ diff --git a/library.properties b/library.properties index 29c5332..5e9975d 100644 --- a/library.properties +++ b/library.properties @@ -1,11 +1,11 @@ name=RFID Reader version=1.0.0 -author=JSC electronics -maintainer=Vladimír Záhradník , JSC electronics +author=JSC TechMinds +maintainer=JSC TechMinds, development@jsctm.cz sentence=RS485 RFID reader support library. paragraph=This is an Arduino library to support one specific RS485 RFID reader. See README for details. category=Signal Input/Output -url=https://github.com/JSC-electronics/rfid-reader +url=https://github.com/JSC-TechMinds/rfid-reader architectures=* license=Apache-2.0 depends=TaskManagerIO,ArduinoLog diff --git a/platformio.ini b/platformio.ini new file mode 100644 index 0000000..33350f1 --- /dev/null +++ b/platformio.ini @@ -0,0 +1,21 @@ +[env:esp32dev] +platform = espressif32 +framework = arduino +board = esp32dev + +lib_deps = + jsc/ArduinoLog@^1.2.1 + davetcc/TaskManagerIO@^1.4.1 + RFID reader + +platform_packages = + platformio/tool-renode + +;test_testing_command = +; ;${platformio.packages_dir}/tool-renode/renode +; renode +; --disable-xwt +; -e include @scripts/single-node/xtensa.resc +; -e showAnalyzer uart1 +; -e sysbus LoadELF @${platformio.build_dir}/${this.__env__}/firmware.elf +; -e start \ No newline at end of file diff --git a/test/deserialize_from_wire.cpp b/test/deserialize_from_wire.cpp deleted file mode 100644 index 9cb6663..0000000 --- a/test/deserialize_from_wire.cpp +++ /dev/null @@ -1,190 +0,0 @@ -#include -#include "../src/packet.h" - -unittest(no_data) { - uint8_t responseData[] = { }; - RfidResponse response = RfidResponse::fromWire(responseData, 0); - assertFalse(response.isValid()); -} - -unittest(invalid_response_soh_value) { - uint8_t responseData[] = { 0x09, 'A', '1', 'B', '9', '9', '0', '8', '0', '0', '0', '1', '3', '2', 0x0D }; - RfidResponse response = RfidResponse::fromWire(responseData, 15); - assertFalse(response.isValid()); -} - -unittest(invalid_packet_termination_value) { - uint8_t responseData[] = { 0x0A, 'A', '1', 'B', '9', '9', '0', '8', '0', '0', '0', '1', '3', '1', 0x0A }; - RfidResponse response = RfidResponse::fromWire(responseData, 15); - assertFalse(response.isValid()); -} - -unittest(invalid_type) { - uint8_t responseData[] = { 0x0A, 'B', '1', 'B', '9', '9', '0', '8', '0', '0', '0', '1', '3', '2', 0x0D }; - RfidResponse response = RfidResponse::fromWire(responseData, 15); - assertFalse(response.isValid()); -} - -unittest(invalid_checksum) { - uint8_t responseData[] = { 0x0A, 'A', '1', 'B', '9', '9', '0', '8', '0', '0', '0', '1', '2', '1', 0x0D }; - RfidResponse response = RfidResponse::fromWire(responseData, 15); - assertFalse(response.isValid()); -} - -unittest(invalid_operation) { - uint8_t responseData[] = { 0x0A, 'A', '1', 'H', '9', '9', '0', '8', '0', '0', '0', '1', '3', 'B', 0x0D }; - RfidResponse response = RfidResponse::fromWire(responseData, 15); - assertFalse(response.isValid()); -} - -unittest(payload_exceeds_limit) { - uint8_t responseData[] = { 0x0A, 'A', '1', 'B', '9', '9', '0', '8', '0', '0', '0', '1', '1', '0', '9', 0x0D }; - RfidResponse response = RfidResponse::fromWire(responseData, 15); - assertFalse(response.isValid()); -} - -unittest(read_serial_number) { - uint8_t responseData[] = { 0x0A, 'A', '1', 'B', '9', '9', '0', '8', '0', '0', '0', '1', '3', '1', 0x0D }; - - RfidResponse response = RfidResponse::fromWire(responseData, 15); - assertEqual((uint8_t) RfidPacket::Function::READ_SERIAL_NUMBER, (uint8_t) response.getOperation()); - assertEqual(1, response.getReaderId()); - assertEqual("99080001", response.getSerialNumber()); - assertTrue(response.isValid()); -} - -unittest(read_serial_number_from_reader_8) { - uint8_t responseData[] = { 0x0A, 'A', '8', 'B', '9', '9', '0', '8', '0', '0', '0', '1', '3', '8', 0x0D }; - - RfidResponse response = RfidResponse::fromWire(responseData, 15); - assertEqual((uint8_t) RfidPacket::Function::READ_SERIAL_NUMBER, (uint8_t) response.getOperation()); - assertEqual(8, response.getReaderId()); - assertEqual("99080001", response.getSerialNumber()); - assertTrue(response.isValid()); -} - -unittest(read_serial_number_with_invalid_length_shorter) { - uint8_t responseData[] = { 0x0A, 'A', '1', 'B', '9', '0', '8', '0', '0', '0', '1', '0', '8', 0x0D }; - - RfidResponse response = RfidResponse::fromWire(responseData, 14); - assertEqual((uint8_t) RfidPacket::Function::READ_SERIAL_NUMBER, (uint8_t) response.getOperation()); - assertEqual(-1, response.getReaderId()); - assertEqual("", response.getSerialNumber()); - assertFalse(response.isValid()); -} - -unittest(read_serial_number_with_invalid_length_longer) { - uint8_t responseData[] = { 0x0A, 'A', '1', 'B', '9', '9', '9', '0', '8', '0', '0', '0', '1', '0', '8', 0x0D }; - - RfidResponse response = RfidResponse::fromWire(responseData, 16); - assertEqual((uint8_t) RfidPacket::Function::READ_SERIAL_NUMBER, (uint8_t) response.getOperation()); - assertEqual(-1, response.getReaderId()); - assertEqual("", response.getSerialNumber()); - assertFalse(response.isValid()); -} - -unittest(set_reader_id) { - uint8_t responseData[] = { 0x0A, 'A', 'X', 'C', '5', '0', 0x0D }; - - RfidResponse response = RfidResponse::fromWire(responseData, 7); - assertEqual((uint8_t) RfidPacket::Function::SET_READER_ID, (uint8_t) response.getOperation()); - assertEqual(-1, response.getReaderId()); - assertEqual("", response.getSerialNumber()); - assertTrue(response.isValid()); -} - -unittest(set_reader_id_with_payload_is_invalid) { - uint8_t responseData[] = { 0x0A, 'A', 'X', 'C', '1', '6', '1', 0x0D }; - - RfidResponse response = RfidResponse::fromWire(responseData, 8); - assertFalse(response.isValid()); -} - -unittest(read_reader_id) { - uint8_t responseData[] = { 0x0A, 'A', 'X', 'D', '2', '6', '5', 0x0D }; - - RfidResponse response = RfidResponse::fromWire(responseData, 8); - assertEqual((uint8_t) RfidPacket::Function::READ_READER_ID, (uint8_t) response.getOperation()); - assertEqual(2, response.getReaderId()); - assertEqual("", response.getSerialNumber()); - assertTrue(response.isValid()); -} - -unittest(read_card_data_no_card) { - uint8_t responseData[] = { 0x0A, 'A', '1', 'F', '3', 'C', 0x0D }; - - RfidResponse response = RfidResponse::fromWire(responseData, 7); - assertEqual((uint8_t) RfidPacket::Function::READ_CARD_DATA, (uint8_t) response.getOperation()); - assertEqual(1, response.getReaderId()); - assertEqual("", response.getSerialNumber()); - assertEqual("", response.getCardData()); - assertTrue(response.isValid()); -} - -unittest(read_card_data_having_max_size) { - uint8_t responseData[] = { 0x0A, 'A', '2', 'F', '0', '0', '0', '0', '0', 'F', 'F', '1', 'A', '7', 'F', 0x0D }; - - RfidResponse response = RfidResponse::fromWire(responseData, 16); - assertEqual((uint8_t) RfidPacket::Function::READ_CARD_DATA, (uint8_t) response.getOperation()); - assertEqual(2, response.getReaderId()); - assertEqual("", response.getSerialNumber()); - assertEqual("00000FF1A", response.getCardData()); - assertTrue(response.isValid()); -} - -unittest(read_card_data_shorter_than_max_size) { - uint8_t responseData[] = { 0x0A, 'A', '2', 'F', 'F', 'F', '1', 'A', '4', 'F', 0x0D }; - - RfidResponse response = RfidResponse::fromWire(responseData, 11); - assertEqual((uint8_t) RfidPacket::Function::READ_CARD_DATA, (uint8_t) response.getOperation()); - assertEqual(2, response.getReaderId()); - assertEqual("", response.getSerialNumber()); - assertEqual("FF1A", response.getCardData()); - assertTrue(response.isValid()); -} - -unittest(read_card_data_longer_than_max_size) { - uint8_t responseData[] = { 0x0A, 'A', '2', 'F', '0', '0', '0', '0', '0', '0', 'F', 'F', '1', 'A', '4', 'F', 0x0D }; - - RfidResponse response = RfidResponse::fromWire(responseData, 17); - assertEqual((uint8_t) RfidPacket::Function::READ_CARD_DATA, (uint8_t) response.getOperation()); - assertEqual(2, response.getReaderId()); - assertEqual("", response.getSerialNumber()); - assertEqual("000000FF1", response.getCardData()); - assertTrue(response.isValid()); -} - -unittest(re_read_card_data_having_max_size) { - uint8_t responseData[] = { 0x0A, 'A', '2', 'G', '0', '0', '0', '0', '0', 'F', 'F', '1', 'A', '7', 'E', 0x0D }; - - RfidResponse response = RfidResponse::fromWire(responseData, 16); - assertEqual((uint8_t) RfidPacket::Function::RE_READ_CARD_DATA, (uint8_t) response.getOperation()); - assertEqual(2, response.getReaderId()); - assertEqual("", response.getSerialNumber()); - assertEqual("00000FF1A", response.getCardData()); - assertTrue(response.isValid()); -} - -unittest(re_read_card_data_shorter_than_max_size) { - uint8_t responseData[] = { 0x0A, 'A', '2', 'G', 'F', 'F', '1', 'A', '4', 'E', 0x0D }; - - RfidResponse response = RfidResponse::fromWire(responseData, 11); - assertEqual((uint8_t) RfidPacket::Function::RE_READ_CARD_DATA, (uint8_t) response.getOperation()); - assertEqual(2, response.getReaderId()); - assertEqual("", response.getSerialNumber()); - assertEqual("FF1A", response.getCardData()); - assertTrue(response.isValid()); -} - -unittest(re_read_card_data_longer_than_max_size) { - uint8_t responseData[] = { 0x0A, 'A', '2', 'G', '0', '0', '0', '0', '0', '0', 'F', 'F', '1', 'A', '4', 'E', 0x0D }; - - RfidResponse response = RfidResponse::fromWire(responseData, 17); - assertEqual((uint8_t) RfidPacket::Function::RE_READ_CARD_DATA, (uint8_t) response.getOperation()); - assertEqual(2, response.getReaderId()); - assertEqual("", response.getSerialNumber()); - assertEqual("000000FF1", response.getCardData()); - assertTrue(response.isValid()); -} - -unittest_main() \ No newline at end of file diff --git a/test/test_deserialize_from_wire/deserialize_from_wire.cpp b/test/test_deserialize_from_wire/deserialize_from_wire.cpp new file mode 100644 index 0000000..2fd47ba --- /dev/null +++ b/test/test_deserialize_from_wire/deserialize_from_wire.cpp @@ -0,0 +1,221 @@ +#include +#include +#include "../src/packet.h" + +void setUp() {} + +void tearDown() {} + +void test_no_data() { + uint8_t responseData[] = { }; + RfidResponse response = RfidResponse::fromWire(responseData, 0); + TEST_ASSERT_FALSE(response.isValid()); +} + +void test_invalid_response_soh_value() { + uint8_t responseData[] = { 0x09, 'A', '1', 'B', '9', '9', '0', '8', '0', '0', '0', '1', '3', '2', 0x0D }; + RfidResponse response = RfidResponse::fromWire(responseData, 15); + TEST_ASSERT_FALSE(response.isValid()); +} + +void test_invalid_packet_termination_value() { + uint8_t responseData[] = { 0x0A, 'A', '1', 'B', '9', '9', '0', '8', '0', '0', '0', '1', '3', '1', 0x0A }; + RfidResponse response = RfidResponse::fromWire(responseData, 15); + TEST_ASSERT_FALSE(response.isValid()); +} + +void test_invalid_type() { + uint8_t responseData[] = { 0x0A, 'B', '1', 'B', '9', '9', '0', '8', '0', '0', '0', '1', '3', '2', 0x0D }; + RfidResponse response = RfidResponse::fromWire(responseData, 15); + TEST_ASSERT_FALSE(response.isValid()); +} + +void test_invalid_checksum() { + uint8_t responseData[] = { 0x0A, 'A', '1', 'B', '9', '9', '0', '8', '0', '0', '0', '1', '2', '1', 0x0D }; + RfidResponse response = RfidResponse::fromWire(responseData, 15); + TEST_ASSERT_FALSE(response.isValid()); +} + +void test_invalid_operation() { + uint8_t responseData[] = { 0x0A, 'A', '1', 'H', '9', '9', '0', '8', '0', '0', '0', '1', '3', 'B', 0x0D }; + RfidResponse response = RfidResponse::fromWire(responseData, 15); + TEST_ASSERT_FALSE(response.isValid()); +} + +void test_payload_exceeds_limit() { + uint8_t responseData[] = { 0x0A, 'A', '1', 'B', '9', '9', '0', '8', '0', '0', '0', '1', '1', '0', '9', 0x0D }; + RfidResponse response = RfidResponse::fromWire(responseData, 15); + TEST_ASSERT_FALSE(response.isValid()); +} + +void test_read_serial_number() { + uint8_t responseData[] = { 0x0A, 'A', '1', 'B', '9', '9', '0', '8', '0', '0', '0', '1', '3', '1', 0x0D }; + + RfidResponse response = RfidResponse::fromWire(responseData, 15); + TEST_ASSERT_EQUAL_UINT8((uint8_t) RfidPacket::Function::READ_SERIAL_NUMBER, (uint8_t) response.getOperation()); + TEST_ASSERT_EQUAL_UINT8(1, response.getReaderId()); + TEST_ASSERT_EQUAL_STRING("99080001", response.getSerialNumber()); + TEST_ASSERT_TRUE(response.isValid()); +} + +void test_read_serial_number_from_reader_8() { + uint8_t responseData[] = { 0x0A, 'A', '8', 'B', '9', '9', '0', '8', '0', '0', '0', '1', '3', '8', 0x0D }; + + RfidResponse response = RfidResponse::fromWire(responseData, 15); + TEST_ASSERT_EQUAL_UINT8((uint8_t) RfidPacket::Function::READ_SERIAL_NUMBER, (uint8_t) response.getOperation()); + TEST_ASSERT_EQUAL_UINT8(8, response.getReaderId()); + TEST_ASSERT_EQUAL_STRING("99080001", response.getSerialNumber()); + TEST_ASSERT_TRUE(response.isValid()); +} + +void test_read_serial_number_with_invalid_length_shorter() { + uint8_t responseData[] = { 0x0A, 'A', '1', 'B', '9', '0', '8', '0', '0', '0', '1', '0', '8', 0x0D }; + + RfidResponse response = RfidResponse::fromWire(responseData, 14); + TEST_ASSERT_EQUAL_UINT8((uint8_t) RfidPacket::Function::READ_SERIAL_NUMBER, (uint8_t) response.getOperation()); + TEST_ASSERT_EQUAL_INT8(-1, response.getReaderId()); + TEST_ASSERT_EQUAL_STRING("", response.getSerialNumber()); + TEST_ASSERT_FALSE(response.isValid()); +} + +void test_read_serial_number_with_invalid_length_longer() { + uint8_t responseData[] = { 0x0A, 'A', '1', 'B', '9', '9', '9', '0', '8', '0', '0', '0', '1', '0', '8', 0x0D }; + + RfidResponse response = RfidResponse::fromWire(responseData, 16); + TEST_ASSERT_EQUAL_UINT8((uint8_t) RfidPacket::Function::READ_SERIAL_NUMBER, (uint8_t) response.getOperation()); + TEST_ASSERT_EQUAL_INT8(-1, response.getReaderId()); + TEST_ASSERT_EQUAL_STRING("", response.getSerialNumber()); + TEST_ASSERT_FALSE(response.isValid()); +} + +void test_set_reader_id() { + uint8_t responseData[] = { 0x0A, 'A', 'X', 'C', '5', '0', 0x0D }; + + RfidResponse response = RfidResponse::fromWire(responseData, 7); + TEST_ASSERT_EQUAL_UINT8((uint8_t) RfidPacket::Function::SET_READER_ID, (uint8_t) response.getOperation()); + TEST_ASSERT_EQUAL_INT8(-1, response.getReaderId()); + TEST_ASSERT_EQUAL_STRING("", response.getSerialNumber()); + TEST_ASSERT_TRUE(response.isValid()); +} + +void test_set_reader_id_with_payload_is_invalid() { + uint8_t responseData[] = { 0x0A, 'A', 'X', 'C', '1', '6', '1', 0x0D }; + + RfidResponse response = RfidResponse::fromWire(responseData, 8); + TEST_ASSERT_FALSE(response.isValid()); +} + +void test_read_reader_id() { + uint8_t responseData[] = { 0x0A, 'A', 'X', 'D', '2', '6', '5', 0x0D }; + + RfidResponse response = RfidResponse::fromWire(responseData, 8); + TEST_ASSERT_EQUAL_UINT8((uint8_t) RfidPacket::Function::READ_READER_ID, (uint8_t) response.getOperation()); + TEST_ASSERT_EQUAL_UINT8(2, response.getReaderId()); + TEST_ASSERT_EQUAL_STRING("", response.getSerialNumber()); + TEST_ASSERT_TRUE(response.isValid()); +} + +void test_read_card_data_no_card() { + uint8_t responseData[] = { 0x0A, 'A', '1', 'F', '3', 'C', 0x0D }; + + RfidResponse response = RfidResponse::fromWire(responseData, 7); + TEST_ASSERT_EQUAL_UINT8((uint8_t) RfidPacket::Function::READ_CARD_DATA, (uint8_t) response.getOperation()); + TEST_ASSERT_EQUAL_UINT8(1, response.getReaderId()); + TEST_ASSERT_EQUAL_STRING("", response.getSerialNumber()); + TEST_ASSERT_EQUAL_STRING("", response.getCardData()); + TEST_ASSERT_TRUE(response.isValid()); +} + +void test_read_card_data_having_max_size() { + uint8_t responseData[] = { 0x0A, 'A', '2', 'F', '0', '0', '0', '0', '0', 'F', 'F', '1', 'A', '7', 'F', 0x0D }; + + RfidResponse response = RfidResponse::fromWire(responseData, 16); + TEST_ASSERT_EQUAL_UINT8((uint8_t) RfidPacket::Function::READ_CARD_DATA, (uint8_t) response.getOperation()); + TEST_ASSERT_EQUAL_UINT8(2, response.getReaderId()); + TEST_ASSERT_EQUAL_STRING("", response.getSerialNumber()); + TEST_ASSERT_EQUAL_STRING("00000FF1A", response.getCardData()); + TEST_ASSERT_TRUE(response.isValid()); +} + +void test_read_card_data_shorter_than_max_size() { + uint8_t responseData[] = { 0x0A, 'A', '2', 'F', 'F', 'F', '1', 'A', '4', 'F', 0x0D }; + + RfidResponse response = RfidResponse::fromWire(responseData, 11); + TEST_ASSERT_EQUAL_UINT8((uint8_t) RfidPacket::Function::READ_CARD_DATA, (uint8_t) response.getOperation()); + TEST_ASSERT_EQUAL_UINT8(2, response.getReaderId()); + TEST_ASSERT_EQUAL_STRING("", response.getSerialNumber()); + TEST_ASSERT_EQUAL_STRING("FF1A", response.getCardData()); + TEST_ASSERT_TRUE(response.isValid()); +} + +void test_read_card_data_longer_than_max_size() { + uint8_t responseData[] = { 0x0A, 'A', '2', 'F', '0', '0', '0', '0', '0', '0', 'F', 'F', '1', 'A', '4', 'F', 0x0D }; + + RfidResponse response = RfidResponse::fromWire(responseData, 17); + TEST_ASSERT_EQUAL_UINT8((uint8_t) RfidPacket::Function::READ_CARD_DATA, (uint8_t) response.getOperation()); + TEST_ASSERT_EQUAL_UINT8(2, response.getReaderId()); + TEST_ASSERT_EQUAL_STRING("", response.getSerialNumber()); + TEST_ASSERT_EQUAL_STRING("000000FF1", response.getCardData()); + TEST_ASSERT_TRUE(response.isValid()); +} + +void test_re_read_card_data_having_max_size() { + uint8_t responseData[] = { 0x0A, 'A', '2', 'G', '0', '0', '0', '0', '0', 'F', 'F', '1', 'A', '7', 'E', 0x0D }; + + RfidResponse response = RfidResponse::fromWire(responseData, 16); + TEST_ASSERT_EQUAL_UINT8((uint8_t) RfidPacket::Function::RE_READ_CARD_DATA, (uint8_t) response.getOperation()); + TEST_ASSERT_EQUAL_UINT8(2, response.getReaderId()); + TEST_ASSERT_EQUAL_STRING("", response.getSerialNumber()); + TEST_ASSERT_EQUAL_STRING("00000FF1A", response.getCardData()); + TEST_ASSERT_TRUE(response.isValid()); +} + +void test_re_read_card_data_shorter_than_max_size() { + uint8_t responseData[] = { 0x0A, 'A', '2', 'G', 'F', 'F', '1', 'A', '4', 'E', 0x0D }; + + RfidResponse response = RfidResponse::fromWire(responseData, 11); + TEST_ASSERT_EQUAL_UINT8((uint8_t) RfidPacket::Function::RE_READ_CARD_DATA, (uint8_t) response.getOperation()); + TEST_ASSERT_EQUAL_UINT8(2, response.getReaderId()); + TEST_ASSERT_EQUAL_STRING("", response.getSerialNumber()); + TEST_ASSERT_EQUAL_STRING("FF1A", response.getCardData()); + TEST_ASSERT_TRUE(response.isValid()); +} + +void test_re_read_card_data_longer_than_max_size() { + uint8_t responseData[] = { 0x0A, 'A', '2', 'G', '0', '0', '0', '0', '0', '0', 'F', 'F', '1', 'A', '4', 'E', 0x0D }; + + RfidResponse response = RfidResponse::fromWire(responseData, 17); + TEST_ASSERT_EQUAL_UINT8((uint8_t) RfidPacket::Function::RE_READ_CARD_DATA, (uint8_t) response.getOperation()); + TEST_ASSERT_EQUAL_UINT8(2, response.getReaderId()); + TEST_ASSERT_EQUAL_STRING("", response.getSerialNumber()); + TEST_ASSERT_EQUAL_STRING("000000FF1", response.getCardData()); + TEST_ASSERT_TRUE(response.isValid()); +} + +void setup() { + UNITY_BEGIN(); + RUN_TEST(test_no_data); + RUN_TEST(test_invalid_response_soh_value); + RUN_TEST(test_invalid_packet_termination_value); + RUN_TEST(test_invalid_type); + RUN_TEST(test_invalid_checksum); + RUN_TEST(test_invalid_operation); + RUN_TEST(test_payload_exceeds_limit); + RUN_TEST(test_read_serial_number); + RUN_TEST(test_read_serial_number_from_reader_8); + RUN_TEST(test_read_serial_number_with_invalid_length_shorter); + RUN_TEST(test_read_serial_number_with_invalid_length_longer); + RUN_TEST(test_set_reader_id); + RUN_TEST(test_set_reader_id_with_payload_is_invalid); + RUN_TEST(test_read_reader_id); + RUN_TEST(test_read_card_data_no_card); + RUN_TEST(test_read_card_data_having_max_size); + RUN_TEST(test_read_card_data_shorter_than_max_size); + RUN_TEST(test_read_card_data_longer_than_max_size); + RUN_TEST(test_re_read_card_data_having_max_size); + RUN_TEST(test_re_read_card_data_shorter_than_max_size); + RUN_TEST(test_re_read_card_data_longer_than_max_size); + UNITY_END(); +} + +void loop() {} diff --git a/test/serialize_to_wire.cpp b/test/test_serialize_to_wire/serialize_to_wire.cpp similarity index 56% rename from test/serialize_to_wire.cpp rename to test/test_serialize_to_wire/serialize_to_wire.cpp index f173612..05b98cc 100644 --- a/test/serialize_to_wire.cpp +++ b/test/test_serialize_to_wire/serialize_to_wire.cpp @@ -1,9 +1,14 @@ -#include +#include +#include #include "../src/packet.h" +void setUp() {} + +void tearDown() {} + uint8_t buffer[64]; -unittest(read_serial_number_packet_is_valid) { +void test_read_serial_number_packet_is_valid() { size_t length; uint8_t readerId = 1; @@ -14,11 +19,11 @@ unittest(read_serial_number_packet_is_valid) { length = request.toWire(buffer, 64); - assertEqual(expectedLength, length); - assertEqual(0, memcmp(buffer, expectedData, expectedLength)); + TEST_ASSERT_EQUAL_UINT8(expectedLength, length); + TEST_ASSERT_EQUAL_UINT8(0, memcmp(buffer, expectedData, expectedLength)); } -unittest(read_serial_number_from_reader_2_packet_is_valid) { +void test_read_serial_number_from_reader_2_packet_is_valid() { size_t length; uint8_t readerId = 2; @@ -29,11 +34,11 @@ unittest(read_serial_number_from_reader_2_packet_is_valid) { length = request.toWire(buffer, 64); - assertEqual(expectedLength, length); - assertEqual(0, memcmp(buffer, expectedData, expectedLength)); + TEST_ASSERT_EQUAL_UINT8(expectedLength, length); + TEST_ASSERT_EQUAL_UINT8(0, memcmp(buffer, expectedData, expectedLength)); } -unittest(set_reader_id_packet_is_valid) { +void test_set_reader_id_packet_is_valid() { size_t length; uint8_t readerId = 1; @@ -44,11 +49,11 @@ unittest(set_reader_id_packet_is_valid) { length = request.toWire(buffer, 64); - assertEqual(expectedLength, length); - assertEqual(0, memcmp(buffer, expectedData, expectedLength)); + TEST_ASSERT_EQUAL_UINT8(expectedLength, length); + TEST_ASSERT_EQUAL_UINT8(0, memcmp(buffer, expectedData, expectedLength)); } -unittest(set_reader_id_for_reader_8_packet_is_valid) { +void test_set_reader_id_for_reader_8_packet_is_valid() { size_t length; uint8_t readerId = 8; @@ -59,11 +64,11 @@ unittest(set_reader_id_for_reader_8_packet_is_valid) { length = request.toWire(buffer, 64); - assertEqual(expectedLength, length); - assertEqual(0, memcmp(buffer, expectedData, expectedLength)); + TEST_ASSERT_EQUAL_UINT8(expectedLength, length); + TEST_ASSERT_EQUAL_UINT8(0, memcmp(buffer, expectedData, expectedLength)); } -unittest(read_reader_id_packet_is_valid) { +void test_read_reader_id_packet_is_valid() { size_t length; uint8_t readerId = 1; // Ignored @@ -74,11 +79,11 @@ unittest(read_reader_id_packet_is_valid) { length = request.toWire(buffer, 64); - assertEqual(expectedLength, length); - assertEqual(0, memcmp(buffer, expectedData, expectedLength)); + TEST_ASSERT_EQUAL_UINT8(expectedLength, length); + TEST_ASSERT_EQUAL_UINT8(0, memcmp(buffer, expectedData, expectedLength)); } -unittest(read_card_data_packet_is_valid) { +void test_read_card_data_packet_is_valid() { size_t length; uint8_t readerId = 1; @@ -89,11 +94,11 @@ unittest(read_card_data_packet_is_valid) { length = request.toWire(buffer, 64); - assertEqual(expectedLength, length); - assertEqual(0, memcmp(buffer, expectedData, expectedLength)); + TEST_ASSERT_EQUAL_UINT8(expectedLength, length); + TEST_ASSERT_EQUAL_UINT8(0, memcmp(buffer, expectedData, expectedLength)); } -unittest(read_card_data_from_reader_4_packet_is_valid) { +void test_read_card_data_from_reader_4_packet_is_valid() { size_t length; uint8_t readerId = 4; @@ -104,11 +109,11 @@ unittest(read_card_data_from_reader_4_packet_is_valid) { length = request.toWire(buffer, 64); - assertEqual(expectedLength, length); - assertEqual(0, memcmp(buffer, expectedData, expectedLength)); + TEST_ASSERT_EQUAL_UINT8(expectedLength, length); + TEST_ASSERT_EQUAL_UINT8(0, memcmp(buffer, expectedData, expectedLength)); } -unittest(re_read_card_data_packet_is_valid) { +void test_re_read_card_data_packet_is_valid() { size_t length; uint8_t readerId = 1; @@ -119,11 +124,11 @@ unittest(re_read_card_data_packet_is_valid) { length = request.toWire(buffer, 64); - assertEqual(expectedLength, length); - assertEqual(0, memcmp(buffer, expectedData, expectedLength)); + TEST_ASSERT_EQUAL_UINT8(expectedLength, length); + TEST_ASSERT_EQUAL_UINT8(0, memcmp(buffer, expectedData, expectedLength)); } -unittest(reader_id_out_of_range_write_no_data) { +void test_reader_id_out_of_range_write_no_data() { size_t length; uint8_t readerId = 10; @@ -133,10 +138,10 @@ unittest(reader_id_out_of_range_write_no_data) { length = request.toWire(buffer, 64); - assertEqual(expectedLength, length); + TEST_ASSERT_EQUAL_UINT8(expectedLength, length); } -unittest(target_buffer_too_small) { +void test_target_buffer_too_small() { uint8_t smallBuffer[5]; size_t length; uint8_t readerId = 1; @@ -145,10 +150,10 @@ unittest(target_buffer_too_small) { length = request.toWire(smallBuffer, 5); - assertEqual(-1, length); + TEST_ASSERT_EQUAL_INT8(-1, length); } -unittest(target_buffer_too_small_for_payload) { +void test_target_buffer_too_small_for_payload() { uint8_t smallBuffer[9]; size_t length; uint8_t readerId = 1; // Ignored @@ -157,10 +162,10 @@ unittest(target_buffer_too_small_for_payload) { length = request.toWire(smallBuffer, 9); - assertEqual(-1, length); + TEST_ASSERT_EQUAL_INT8(-1, length); } -unittest(read_reader_id_no_id_validation) { +void test_read_reader_id_no_id_validation() { size_t length; uint8_t readerId = -1; // Ignored, out of range ID value @@ -171,8 +176,25 @@ unittest(read_reader_id_no_id_validation) { length = request.toWire(buffer, 64); - assertEqual(expectedLength, length); - assertEqual(0, memcmp(buffer, expectedData, expectedLength)); + TEST_ASSERT_EQUAL_UINT8(expectedLength, length); + TEST_ASSERT_EQUAL_UINT8(0, memcmp(buffer, expectedData, expectedLength)); +} + +void setup() { + UNITY_BEGIN(); + RUN_TEST(test_read_serial_number_packet_is_valid); + RUN_TEST(test_read_serial_number_from_reader_2_packet_is_valid); + RUN_TEST(test_set_reader_id_packet_is_valid); + RUN_TEST(test_set_reader_id_for_reader_8_packet_is_valid); + RUN_TEST(test_read_reader_id_packet_is_valid); + RUN_TEST(test_read_card_data_packet_is_valid); + RUN_TEST(test_read_card_data_from_reader_4_packet_is_valid); + RUN_TEST(test_re_read_card_data_packet_is_valid); + RUN_TEST(test_reader_id_out_of_range_write_no_data); + RUN_TEST(test_target_buffer_too_small); + RUN_TEST(test_target_buffer_too_small_for_payload); + RUN_TEST(test_read_reader_id_no_id_validation); + UNITY_END(); } -unittest_main() \ No newline at end of file +void loop() {} \ No newline at end of file