static void test_errors(void) { cbor_failure tests[] = { {"81", CN_CBOR_ERR_OUT_OF_DATA}, {"0000", CN_CBOR_ERR_NOT_ALL_DATA_CONSUMED}, {"bf00ff", CN_CBOR_ERR_ODD_SIZE_INDEF_MAP}, {"ff", CN_CBOR_ERR_BREAK_OUTSIDE_INDEF}, {"1f", CN_CBOR_ERR_MT_UNDEF_FOR_INDEF}, {"1c", CN_CBOR_ERR_RESERVED_AI}, {"7f4100", CN_CBOR_ERR_WRONG_NESTING_IN_INDEF_STRING}, }; cn_cbor inv = {CN_CBOR_INVALID, 0, {0}, 0, NULL, NULL, NULL, NULL}; TEST_ASSERT_EQUAL_INT(-1, cn_cbor_encoder_write(ebuf, 0, sizeof(ebuf), &inv)); for (offs = 0; offs < sizeof(tests) / sizeof(cbor_failure); offs++) { unsigned char buf[32] = {0}; TEST_ASSERT((strlen(tests[offs].hex)/2) <= sizeof(buf)); size_t len = fmt_hex_bytes(buf, tests[offs].hex); TEST_ASSERT(len); cn_cbor *cbor = cn_cbor_decode(buf, len, &ct, &errb); TEST_ASSERT_NULL(cbor); TEST_ASSERT_EQUAL_INT(errb.err, tests[offs].err); cn_cbor_free(cbor, &ct); } }
int main(void) { puts("LoRaWAN Class A low-power application"); puts("====================================="); /* Convert identifiers and application key */ fmt_hex_bytes(deveui, DEVEUI); fmt_hex_bytes(appeui, APPEUI); fmt_hex_bytes(appkey, APPKEY); /* Initialize the loramac stack */ semtech_loramac_init(&loramac); semtech_loramac_set_deveui(&loramac, deveui); semtech_loramac_set_appeui(&loramac, appeui); semtech_loramac_set_appkey(&loramac, appkey); /* Use a fast datarate, e.g. BW125/SF7 in EU868 */ semtech_loramac_set_dr(&loramac, LORAMAC_DR_5); /* Start the Over-The-Air Activation (OTAA) procedure to retrieve the * generated device address and to get the network and application session * keys. */ puts("Starting join procedure"); if (semtech_loramac_join(&loramac, LORAMAC_JOIN_OTAA) != SEMTECH_LORAMAC_JOIN_SUCCEEDED) { puts("Join procedure failed"); return 1; } puts("Join procedure succeeded"); /* start the sender thread */ sender_pid = thread_create(sender_stack, sizeof(sender_stack), SENDER_PRIO, 0, sender, NULL, "sender"); /* trigger the first send */ msg_t msg; msg_send(&msg, sender_pid); return 0; }
static void test_fmt_hex_bytes(void) { uint8_t val = 0; uint8_t bytes = fmt_hex_bytes(&val, ""); TEST_ASSERT_EQUAL_INT(0, bytes); TEST_ASSERT_EQUAL_INT(0, val); bytes = fmt_hex_bytes(&val, "A"); TEST_ASSERT_EQUAL_INT(0, val); TEST_ASSERT_EQUAL_INT(0, bytes); char hex2[3] = "00"; uint8_t val1[1] = { 0 }; bytes = fmt_hex_bytes(val1, hex2); TEST_ASSERT_EQUAL_INT(1, bytes); TEST_ASSERT_EQUAL_INT(0, val1[0]); memcpy(hex2, "AB", 2); hex2[2] = '\0'; val1[0] = 0; bytes = fmt_hex_bytes(val1, hex2); TEST_ASSERT_EQUAL_INT(1, bytes); TEST_ASSERT_EQUAL_INT(0xAB, val1[0]); memcpy(hex2, "CD", 2); hex2[2] = '\0'; val1[0] = 0; bytes = fmt_hex_bytes(val1, hex2); TEST_ASSERT_EQUAL_INT(1, bytes); TEST_ASSERT_EQUAL_INT(0xCD, val1[0]); memcpy(hex2, "EF", 2); hex2[2] = '\0'; val1[0] = 0; bytes = fmt_hex_bytes(val1, hex2); TEST_ASSERT_EQUAL_INT(1, bytes); TEST_ASSERT_EQUAL_INT(0xEF, val1[0]); char hex6[] = "0102aF"; uint8_t val3[3]; bytes = fmt_hex_bytes(val3, hex6); TEST_ASSERT_EQUAL_INT(3, bytes); TEST_ASSERT_EQUAL_INT(1, val3[0]); TEST_ASSERT_EQUAL_INT(2, val3[1]); TEST_ASSERT_EQUAL_INT(0xAF, val3[2]); }
static void test_parse(void) { char *tests[] = { "00", // 0 "01", // 1 "17", // 23 "1818", // 24 "190100", // 256 "1a00010000", // 65536 #ifndef CBOR_NO_LL "1b0000000100000000", // 4294967296 #endif /* CBOR_NO_LL */ "20", // -1 "37", // -24 "3818", // -25 "390100", // -257 "3a00010000", // -65537 #ifndef CBOR_NO_LL "3b0000000100000000", // -4294967297 #endif /* CBOR_LL */ "4161", // h"a" "6161", // "a" "80", // [] "8100", // [0] "820102", // [1,2] "818100", // [[0]] "a1616100", // {"a":0} "d8184100", // tag "f4", // false "f5", // true "f6", // null "f7", // undefined "f8ff", // simple(255) #ifndef CBOR_NO_FLOAT "f93c00", // 1.0 "f9bc00", // -1.0 "f903ff", // 6.097555160522461e-05 "f90400", // 6.103515625e-05 "f907ff", // 0.00012201070785522461 "f90800", // 0.0001220703125 "fa47800000", // 65536.0 "fb3ff199999999999a", // 1.1 "f97e00", // NaN #endif /* CBOR_NO_FLOAT */ "5f42010243030405ff", // (_ h'0102', h'030405') "7f61616161ff", // (_ "a", "a") "9fff", // [_ ] "9f9f9fffffff", // [_ [_ [_ ]]] "9f009f00ff00ff", // [_ 0, [_ 0], 0] "bf61610161629f0203ffff", // {_ "a": 1, "b": [_ 2, 3]} }; for (test = 0; test < sizeof(tests) / sizeof(char*); test++) { unsigned char buf[64] = {0}; TEST_ASSERT((strlen(tests[test])/2) <= sizeof(buf)); size_t len = fmt_hex_bytes(buf, tests[test]); TEST_ASSERT(len); errb.err = CN_CBOR_NO_ERROR; cn_cbor *cbor = cn_cbor_decode(buf, len, &ct, &errb); TEST_ASSERT_EQUAL_INT(errb.err, CN_CBOR_NO_ERROR); TEST_ASSERT_NOT_NULL(cbor); cn_cbor_encoder_write(ebuf, 0, sizeof(ebuf), cbor); for (offs = 0; offs < len; offs++) { TEST_ASSERT_EQUAL_INT(buf[offs], ebuf[offs]); } cn_cbor_free(cbor, &ct); } }