
#include <unity.h>
#include <stdint.h>
#include <Packet.h>

void setUp(void) {
}

void tearDown(void) {
}

/*
 *     uint32_t rolling = 0;
 *     uint64_t fixed = 0;
 *     uint32_t data = 0;
 *     decode_wireline(test_data, &rolling, &fixed, &data);
 *     uint16_t cmd = ((fixed >> 24) & 0xf00) | (data & 0xff);
 *     printf("DOOR_ACTION\n");
 *     printf("rolling %X, fixed %llX, data %X\n", rolling, fixed, data);
 *     printf("cmd %X\n", cmd);
 */

void test_packet_status_recd(void) {
    uint8_t test_data[SECPLUS2_CODE_LEN] = {
        0x55, 0x01, 0x00, 0xA5, 0x2F, 0xB3, 0xDB, 0xCE, 0x8F, 0x5B, 0x0C, 0x40, 0x34, 0xB9, 0x71, 0x96, 0x73, 0xFD, 0xBA };

    Packet pkt = Packet(test_data);
    TEST_ASSERT_EQUAL(PacketCommand::Status, pkt.m_pkt_cmd);
    TEST_ASSERT_EQUAL(PacketDataType::Status, pkt.m_data.type);
    TEST_ASSERT_EQUAL(DoorState::Closed, pkt.m_data.value.status.door);
    TEST_ASSERT_TRUE(pkt.m_data.value.status.obstruction);
    TEST_ASSERT_TRUE(pkt.m_data.value.status.light);
    TEST_ASSERT_FALSE(pkt.m_data.value.status.lock);

    TEST_ASSERT_EQUAL_HEX(0x52402A, pkt.m_remote_id);
    TEST_ASSERT_EQUAL_HEX(0x17702, pkt.m_rolling);
}

void test_packet_door_action_xmit(void) {
    PacketData data;
    data.type = PacketDataType::DoorAction;
    data.value.door_action.action = DoorAction::Toggle;
    data.value.door_action.parity = 0b1000;  // to match command below
    data.value.door_action.pressed = true;
    data.value.door_action.id = 1;

    Packet pkt = Packet(PacketCommand::DoorAction, data, 0x539);

    uint8_t test_data[SECPLUS2_CODE_LEN] = {
        0x55, 0x01, 0x00, 0xA0, 0x37, 0xDF, 0x77, 0xB6, 0xFB, 0xED, 0xB0, 0x88, 0x22, 0x91, 0x05, 0x21, 0x72, 0x4D, 0x2C };

    uint8_t encode_output[SECPLUS2_CODE_LEN];
    TEST_ASSERT_EQUAL(0, pkt.encode(0x48, encode_output));
    TEST_ASSERT_EQUAL_MEMORY(test_data, encode_output, SECPLUS2_CODE_LEN);
}

/*
 * This test includes an OG "sync" packet from the ratgdo code ("reboot1"), which has a mystery data
 * bit set (bit 0 of byte 1, or 0x100). There isn't a way to represent that data bit when building a
 * GetOpenings packet, so this test is disabled (but would otherwise pass).
void test_packet_get_openings(void) {
    uint8_t test_data[SECPLUS2_CODE_LEN] = {
        0x55, 0x01, 0x00, 0x94, 0x3F, 0xFD, 0xE7, 0xDF, 0x7F, 0xBE, 0xFF, 0x52, 0x0C, 0x26, 0xDA, 0x4E, 0xA9, 0x8A, 0x67 };

    Packet pkt = Packet(test_data);
    TEST_ASSERT_EQUAL_HEX(PacketCommand::GetOpenings, pkt.m_pkt_cmd);
    TEST_ASSERT_EQUAL(PacketDataType::NoData, pkt.m_data.type);

    TEST_ASSERT_EQUAL_HEX(0x539, pkt.m_remote_id);
    TEST_ASSERT_EQUAL_HEX(0xDB, pkt.m_rolling);

    uint8_t roundtrip[SECPLUS2_CODE_LEN];
    TEST_ASSERT_EQUAL(0, pkt.encode(roundtrip));
    TEST_ASSERT_EQUAL_MEMORY(test_data, roundtrip, SECPLUS2_CODE_LEN);
}
 */

void print_packet(uint8_t pkt[SECPLUS2_CODE_LEN]) {
    printf("decoded packet: [%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X]\n",
            pkt[0], pkt[1], pkt[2], pkt[3], pkt[4], pkt[5], pkt[6], pkt[7], pkt[8], pkt[9],
            pkt[10], pkt[11], pkt[12], pkt[13], pkt[14], pkt[15], pkt[16], pkt[17], pkt[18]);
}

/*
 * This is just a landing zone for printing a bunch of random packets sourced from where ever. They
 * may or may not illustrate anything at all.
 */
void print_some_packets(void) {
    /*
        uint8_t one[SECPLUS2_CODE_LEN] = {0x55, 0x01, 0x00, 0x99, 0x02, 0x11, 0x40, 0x8E, 0x8D, 0x48, 0x0C, 0x65, 0x29, 0x85, 0xC7, 0x7D, 0xC0, 0xCA, 0x2B};
        print_packet(one);
        Packet p = Packet(one);
        p.print();

        uint8_t one_out[SECPLUS2_CODE_LEN] = {1};
        TEST_ASSERT_EQUAL(0, p.encode(one_out));
        print_packet(one_out);
        Packet q = Packet(one_out);
        q.print();
        TEST_ASSERT_EQUAL_MEMORY(one, one_out, SECPLUS2_CODE_LEN);

        uint8_t two[SECPLUS2_CODE_LEN] = {0x55, 0x01, 0x00, 0x54, 0x17, 0x21, 0xEE, 0xAB, 0xE1, 0xEF, 0xAF, 0x06, 0x19, 0x2F, 0xC6, 0x53, 0xCD, 0xB6, 0x4E };
        Packet(two).print();

        */
        uint8_t pkt1[SECPLUS2_CODE_LEN] = {0x55, 0x01, 0x00, 0x00, 0x36, 0xDB, 0x2D, 0xB6, 0xDB, 0x6D, 0xB6, 0x00, 0x36, 0xFD, 0xBC, 0xB6, 0xE9, 0x6F, 0xBB};
        Packet(pkt1).print();
        uint8_t pkt2[SECPLUS2_CODE_LEN] = {0x55, 0x01, 0x00, 0xAA, 0x2C, 0xB2, 0x59, 0x6D, 0x96, 0x59, 0x61, 0xA0, 0x36, 0x94, 0x0C, 0xB7, 0x3B, 0xAD, 0xB6};
        Packet(pkt2).print();
        uint8_t pkt3[SECPLUS2_CODE_LEN] = {0x55, 0x01, 0x00, 0x55, 0x2D, 0x96, 0xCB, 0x2C, 0xB2, 0xCB, 0x2C, 0x50, 0x37, 0x68, 0x0D, 0x36, 0x1C, 0x5D, 0xB4};
        Packet(pkt3).print();
        uint8_t pkt4[SECPLUS2_CODE_LEN] = {0x55, 0x01, 0x00, 0x54, 0x37, 0xFB, 0xEF, 0xBE, 0xFB, 0xEF, 0xBD, 0x41, 0x02, 0xDB, 0xF4, 0xD0, 0xE1, 0x34, 0x90};
        Packet(pkt4).print();
        uint8_t pkt5[SECPLUS2_CODE_LEN] = {0x55, 0x01, 0x00, 0x22, 0x09, 0x20, 0x99, 0x49, 0x24, 0x12, 0x41, 0x20, 0x3E, 0x94, 0x0C, 0xB6, 0x1B, 0xCD, 0xA6};
        Packet(pkt5).print();
        uint8_t pkt6[SECPLUS2_CODE_LEN] = {0x55, 0x01, 0x00, 0x21, 0x1A, 0x49, 0x2F, 0x92, 0x49, 0xA4, 0x93, 0x11, 0x32, 0xEC, 0x94, 0x16, 0x29, 0x74, 0x9E};
        Packet(pkt6).print();
        uint8_t pkt7[SECPLUS2_CODE_LEN] = {0x55, 0x01, 0x00, 0x99, 0x30, 0x3D, 0x6A, 0x07, 0x80, 0x48, 0x04, 0x42, 0x0D, 0x64, 0x73, 0x43, 0x88, 0x03, 0x5D};
        Packet(pkt7).print();
        uint8_t pkt8[SECPLUS2_CODE_LEN] = {0x55, 0x01, 0x00, 0x44, 0x1D, 0x89, 0xBD, 0xEA, 0xF7, 0xFF, 0x7A, 0xA2, 0x03, 0x26, 0xA2, 0xE9, 0xC4, 0x12, 0x61};
        Packet(pkt8).print();
        uint8_t pkt9[SECPLUS2_CODE_LEN] = {0x55, 0x01, 0x00, 0x44, 0x1D, 0x89, 0xBD, 0xEA, 0xF7, 0xFF, 0x7A, 0xA2, 0x03, 0x26, 0xA2, 0xE9, 0xC4, 0x12, 0x61};
        Packet(pkt9).print();
        uint8_t pkt10[SECPLUS2_CODE_LEN] = {0x55, 0x01, 0x00, 0x44, 0x1D, 0x89, 0xBD, 0xEA, 0xF7, 0xFF, 0x7E, 0xA2, 0x03, 0x26, 0xA2, 0xE9, 0xC4, 0x52, 0x61};
        Packet(pkt10).print();
        uint8_t pkt11[SECPLUS2_CODE_LEN] = {0x55, 0x01, 0x00, 0x09, 0x08, 0xF4, 0x80, 0x71, 0x14, 0x84, 0x22, 0x59, 0x08, 0x01, 0x60, 0x61, 0xCC, 0x32, 0x85};
        Packet(pkt11).print();
        uint8_t pkt12[SECPLUS2_CODE_LEN] = {0x55, 0x01, 0x00, 0x09, 0x08, 0xF4, 0x88, 0x71, 0x00, 0x04, 0x02, 0x59, 0x08, 0x01, 0x60, 0x61, 0xCD, 0x13, 0x01};
        Packet(pkt12).print();
        uint8_t pkt13[SECPLUS2_CODE_LEN] = {0x55, 0x01, 0x00, 0x42, 0x29, 0x1A, 0xD0, 0x5C, 0x2C, 0x92, 0x59, 0x94, 0x1D, 0xEF, 0x1E, 0x73, 0x1B, 0x2E, 0x7D};
        Packet(pkt13).print();
        uint8_t pkt14[SECPLUS2_CODE_LEN] = {0x55, 0x01, 0x00, 0x09, 0x08, 0xF4, 0x88, 0x71, 0x00, 0x04, 0x02, 0x59, 0x08, 0x01, 0x60, 0x61, 0xCD, 0x13, 0x01};
        Packet(pkt14).print();
        uint8_t pkt15[SECPLUS2_CODE_LEN] = {0x55, 0x01, 0x00, 0x54, 0x17, 0x21, 0xEE, 0xAB, 0xE1, 0xEF, 0xAF, 0x06, 0x19, 0x2F, 0xC6, 0x53, 0xCD, 0xB6, 0x4E};
        Packet(pkt15).print();
        uint8_t pkt16[SECPLUS2_CODE_LEN] = {0x55, 0x01, 0x00, 0x42, 0x29, 0x1A, 0xD0, 0x5C, 0x2C, 0x92, 0x59, 0x94, 0x1D, 0xEF, 0x1E, 0x73, 0x1B, 0x2E, 0x7D};
        Packet(pkt16).print();
        uint8_t pkt17[SECPLUS2_CODE_LEN] = {0x55, 0x01, 0x00, 0x42, 0x29, 0x1A, 0xD0, 0x5C, 0x2C, 0x92, 0x59, 0x94, 0x1D, 0xEF, 0x1E, 0x73, 0x1B, 0x2E, 0x7D};
        Packet(pkt17).print();
        uint8_t pkt18[SECPLUS2_CODE_LEN] = {0x55, 0x01, 0x00, 0x42, 0x29, 0x1A, 0xD0, 0x5C, 0x2C, 0x92, 0x59, 0x94, 0x1D, 0xEF, 0x1E, 0x73, 0x1B, 0x2E, 0x7D};
        Packet(pkt18).print();
        uint8_t pkt19[SECPLUS2_CODE_LEN] = {0x55, 0x01, 0x00, 0x42, 0x29, 0x1A, 0xD0, 0x5C, 0x2C, 0x92, 0x59, 0x94, 0x1D, 0xEF, 0x1E, 0x73, 0x1B, 0x2E, 0x7D};
        Packet(pkt19).print();
        uint8_t pkt20[SECPLUS2_CODE_LEN] = {0x55, 0x01, 0x00, 0x89, 0x30, 0x36, 0x62, 0x85, 0x40, 0x04, 0x07, 0x41, 0x06, 0x48, 0xE5, 0x1A, 0xE1, 0x24, 0x98};
        Packet(pkt20).print();
        uint8_t pkt21[SECPLUS2_CODE_LEN] = {0x55, 0x01, 0x00, 0x89, 0x30, 0x36, 0x62, 0x85, 0x40, 0x04, 0x07, 0x41, 0x06, 0x48, 0xE5, 0x1A, 0xE1, 0x24, 0x98};
        Packet(pkt21).print();
        uint8_t pkt22[SECPLUS2_CODE_LEN] = {0x55, 0x01, 0x00, 0x89, 0x30, 0x36, 0x62, 0x85, 0x40, 0x04, 0x03, 0x41, 0x06, 0x48, 0xE5, 0x1A, 0xE1, 0x34, 0x98};
        Packet(pkt22).print();
        uint8_t pkt23[SECPLUS2_CODE_LEN] = {0x55, 0x01, 0x00, 0x88, 0x04, 0xE4, 0x2B, 0xA1, 0xD2, 0x4D, 0x24, 0x22, 0x03, 0x22, 0x30, 0xE8, 0xF6, 0x52, 0xC3};
        Packet(pkt23).print();
        uint8_t pkt24[SECPLUS2_CODE_LEN] = {0x55, 0x01, 0x00, 0x52, 0x28, 0xFE, 0x83, 0x5D, 0x3A, 0x82, 0x51, 0xA8, 0x2C, 0x90, 0x3B, 0x35, 0x62, 0xCA, 0x22};
        Packet(pkt24).print();
        uint8_t pkt25[SECPLUS2_CODE_LEN] = {0x55, 0x01, 0x00, 0x88, 0x04, 0xE4, 0x2B, 0xA1, 0xD2, 0x4D, 0x24, 0x22, 0x03, 0x22, 0x30, 0xE8, 0xF6, 0x52, 0xC3};
        Packet(pkt25).print();
        uint8_t pkt26[SECPLUS2_CODE_LEN] = {0x55, 0x01, 0x00, 0x52, 0x28, 0xFE, 0x87, 0x5D, 0x20, 0x82, 0x41, 0xA8, 0x2C, 0x90, 0x3B, 0x35, 0x60, 0xCB, 0xA4};
        Packet(pkt26).print();
        uint8_t pkt27[SECPLUS2_CODE_LEN] = {0x55, 0x01, 0x00, 0x52, 0x28, 0xFE, 0x87, 0x5D, 0x20, 0x82, 0x41, 0xA8, 0x2C, 0x90, 0x3B, 0x35, 0x60, 0xCB, 0xA4};
        Packet(pkt27).print();
        uint8_t pkt28[SECPLUS2_CODE_LEN] = {0x55, 0x01, 0x00, 0x52, 0x28, 0xFE, 0x87, 0x5D, 0x20, 0x82, 0x41, 0xA8, 0x2C, 0x90, 0x3B, 0x35, 0x60, 0xCB, 0xA4};
        Packet(pkt28).print();
        uint8_t pkt29[SECPLUS2_CODE_LEN] = {0x55, 0x01, 0x00, 0xA9, 0x10, 0xD9, 0x82, 0xAA, 0x39, 0x82, 0x21, 0x56, 0x1B, 0x68, 0xC4, 0xFB, 0xA8, 0x86, 0x87};
        Packet(pkt29).print();
        uint8_t pkt30[SECPLUS2_CODE_LEN] = {0x55, 0x01, 0x00, 0x88, 0x26, 0x93, 0x4B, 0x34, 0xD2, 0x4D, 0x21, 0x80, 0x34, 0x49, 0xBD, 0xF6, 0x3B, 0x6D, 0xBE};
        Packet(pkt30).print();
        uint8_t pkt31[SECPLUS2_CODE_LEN] = {0x55, 0x01, 0x00, 0x88, 0x26, 0x93, 0x4B, 0x34, 0xD2, 0x4D, 0x25, 0x80, 0x34, 0x49, 0xBD, 0xF6, 0x3B, 0x7D, 0xBE};
        Packet(pkt31).print();
}

int main(int argc, char **argv) {
    UNITY_BEGIN();
    RUN_TEST(test_packet_status_recd);
    RUN_TEST(test_packet_door_action_xmit);
    // RUN_TEST(test_packet_get_openings);
    RUN_TEST(print_some_packets);
    UNITY_END();

    return 0;
}
