Repository: incubator-mynewt-larva Updated Branches: refs/heads/master 61e8c4733 -> f396cc2bf
Add additional GATT tests and fix UUID endianness. Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/commit/f396cc2b Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/tree/f396cc2b Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/diff/f396cc2b Branch: refs/heads/master Commit: f396cc2bf5e23448aad083305a65180c8cc9216c Parents: 61e8c47 Author: Christopher Collins <ccollins47...@gmail.com> Authored: Fri Dec 4 11:12:53 2015 -0800 Committer: Christopher Collins <ccollins47...@gmail.com> Committed: Fri Dec 4 11:12:53 2015 -0800 ---------------------------------------------------------------------- net/nimble/host/src/ble_hs_uuid.c | 17 ++- net/nimble/host/src/test/ble_gatt_test.c | 122 ++++++++++++++++---- net/nimble/host/src/test/ble_hs_att_svr_test.c | 15 ++- net/nimble/host/src/test/ble_hs_uuid_test.c | 36 +++--- 4 files changed, 134 insertions(+), 56 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/f396cc2b/net/nimble/host/src/ble_hs_uuid.c ---------------------------------------------------------------------- diff --git a/net/nimble/host/src/ble_hs_uuid.c b/net/nimble/host/src/ble_hs_uuid.c index bedadae..e737344 100644 --- a/net/nimble/host/src/ble_hs_uuid.c +++ b/net/nimble/host/src/ble_hs_uuid.c @@ -23,8 +23,8 @@ #include "ble_hs_uuid.h" static uint8_t ble_hs_uuid_base[16] = { - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, - 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB + 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, + 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; /** @@ -44,21 +44,20 @@ ble_hs_uuid_16bit(void *uuid128) u8ptr = uuid128; - /* The UUID can only be converted if its final 96 bits are equal to the - * base UUID. + /* The UUID can only be converted if the final 96 bits of its big endian + * representation are equal to the base UUID. */ - rc = memcmp(u8ptr + 4, ble_hs_uuid_base + 4, - sizeof ble_hs_uuid_base - 4); + rc = memcmp(u8ptr, ble_hs_uuid_base, sizeof ble_hs_uuid_base - 4); if (rc != 0) { return 0; } - if (u8ptr[0] != 0 || u8ptr[1] != 0) { + if (u8ptr[14] != 0 || u8ptr[15] != 0) { /* This UUID has a 32-bit form, but not a 16-bit form. */ return 0; } - uuid16 = (u8ptr[2] << 8) + u8ptr[3]; + uuid16 = le16toh(u8ptr + 12); if (uuid16 == 0) { return 0; } @@ -78,7 +77,7 @@ ble_hs_uuid_from_16bit(uint16_t uuid16, void *uuid128) u8ptr = uuid128; memcpy(u8ptr, ble_hs_uuid_base, 16); - htole16(u8ptr + 2, uuid16); + htole16(u8ptr + 12, uuid16); return 0; } http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/f396cc2b/net/nimble/host/src/test/ble_gatt_test.c ---------------------------------------------------------------------- diff --git a/net/nimble/host/src/test/ble_gatt_test.c b/net/nimble/host/src/test/ble_gatt_test.c index f3c3e2c..f4d23aa 100644 --- a/net/nimble/host/src/test/ble_gatt_test.c +++ b/net/nimble/host/src/test/ble_gatt_test.c @@ -20,18 +20,36 @@ #include "nimble/ble.h" #include "host/ble_hs_test.h" #include "host/ble_gatt.h" +#include "ble_hs_uuid.h" #include "ble_att_cmd.h" #include "ble_hs_conn.h" #include "ble_hs_test_util.h" +struct ble_gatt_test_service { + uint16_t start_handle; + uint16_t end_handle; + uint16_t uuid16; + uint8_t uuid128[16]; +}; + #define BLE_GATT_TEST_MAX_SERVICES 256 static struct ble_gatt_service ble_gatt_test_services[BLE_GATT_TEST_MAX_SERVICES]; static int ble_gatt_test_num_services; -static void -ble_gatt_test_misc_rx_disc_services_rsp(struct ble_hs_conn *conn, - struct ble_gatt_service *services) +int +ble_gatt_test_misc_service_length(struct ble_gatt_test_service *service) +{ + if (service->uuid16 != 0) { + return 6; + } else { + return 20; + } +} + +static int +ble_gatt_test_misc_rx_disc_services_rsp_once( + struct ble_hs_conn *conn, struct ble_gatt_test_service *services) { struct ble_att_read_group_type_rsp rsp; struct ble_l2cap_chan *chan; @@ -40,21 +58,40 @@ ble_gatt_test_misc_rx_disc_services_rsp(struct ble_hs_conn *conn, int rc; int i; - rsp.bhagp_length = 20; + /* Send the pending ATT Read By Group Type Request. */ + ble_gatt_wakeup(); + + rsp.bhagp_length = ble_gatt_test_misc_service_length(services); rc = ble_att_read_group_type_rsp_write( buf, BLE_ATT_READ_GROUP_TYPE_RSP_BASE_SZ, &rsp); TEST_ASSERT_FATAL(rc == 0); off = BLE_ATT_READ_GROUP_TYPE_RSP_BASE_SZ; - for (i = 0; services[i].start_handle != 0; i++) { + for (i = 0; ; i++) { + if (services[i].start_handle == 0) { + /* No more services. */ + break; + } + + rc = ble_gatt_test_misc_service_length(services + i); + if (rc != rsp.bhagp_length) { + /* UUID length is changing; Need a separate response. */ + break; + } + htole16(buf + off, services[i].start_handle); off += 2; htole16(buf + off, services[i].end_handle); off += 2; - memcpy(buf + off, services[i].uuid128, 16); - off += 16; + if (services[i].uuid16 != 0) { + htole16(buf + off, services[i].uuid16); + off += 2; + } else { + memcpy(buf + off, services[i].uuid128, 16); + off += 16; + } } chan = ble_hs_conn_chan_find(conn, BLE_L2CAP_CID_ATT); @@ -62,16 +99,45 @@ ble_gatt_test_misc_rx_disc_services_rsp(struct ble_hs_conn *conn, rc = ble_hs_test_util_l2cap_rx_payload_flat(conn, chan, buf, off); TEST_ASSERT(rc == 0); + + return i; +} + +static void +ble_gatt_test_misc_rx_disc_services_rsp(struct ble_hs_conn *conn, + struct ble_gatt_test_service *services) +{ + int count; + int idx; + + idx = 0; + while (services[idx].start_handle != 0) { + count = ble_gatt_test_misc_rx_disc_services_rsp_once(conn, + services + idx); + idx += count; + } } static void -ble_gatt_test_misc_verify_services(struct ble_gatt_service *services) +ble_gatt_test_misc_verify_services(struct ble_gatt_test_service *services) { + uint16_t uuid16; + uint8_t *uuid128; int i; for (i = 0; services[i].start_handle != 0; i++) { - TEST_ASSERT(memcmp(services + i, ble_gatt_test_services + i, - sizeof *services) == 0); + TEST_ASSERT(services[i].start_handle == + ble_gatt_test_services[i].start_handle); + TEST_ASSERT(services[i].end_handle == + ble_gatt_test_services[i].end_handle); + + uuid128 = ble_gatt_test_services[i].uuid128; + uuid16 = ble_hs_uuid_16bit(uuid128); + if (uuid16 != 0) { + TEST_ASSERT(services[i].uuid16 == uuid16); + } else { + TEST_ASSERT(memcmp(services[i].uuid128, uuid128, 16) == 0); + } } TEST_ASSERT(i == ble_gatt_test_num_services); @@ -92,7 +158,7 @@ ble_gatt_test_misc_disc_cb(uint16_t conn_handle, int status, } static void -ble_gatt_test_misc_successful_disc_services(struct ble_gatt_service *services) +ble_gatt_test_misc_good_disc_services(struct ble_gatt_test_service *services) { struct ble_hs_conn *conn; int rc; @@ -104,7 +170,6 @@ ble_gatt_test_misc_successful_disc_services(struct ble_gatt_service *services) rc = ble_gatt_disc_all_services(2, ble_gatt_test_misc_disc_cb, NULL); TEST_ASSERT(rc == 0); - ble_gatt_wakeup(); ble_gatt_test_misc_rx_disc_services_rsp(conn, services); ble_gatt_test_misc_verify_services(services); @@ -112,24 +177,33 @@ ble_gatt_test_misc_successful_disc_services(struct ble_gatt_service *services) TEST_CASE(ble_gatt_test_1) { - /*** One service. */ - ble_gatt_test_misc_successful_disc_services((struct ble_gatt_service[]) { - { 1, 5, {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, }, + /*** One 128-bit service. */ + ble_gatt_test_misc_good_disc_services((struct ble_gatt_test_service[]) { + { 1, 5, 0, {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, }, + { 0 } + }); + + /*** Two 128-bit services. */ + ble_gatt_test_misc_good_disc_services((struct ble_gatt_test_service[]) { + { 1, 5, 0, {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, }, + { 10, 50, 0, {2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 }, }, { 0 } }); - /*** Two services. */ - ble_gatt_test_misc_successful_disc_services((struct ble_gatt_service[]) { - { 1, 5, {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, }, - { 10, 50, {2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 }, }, + /*** Five 128-bit services. */ + ble_gatt_test_misc_good_disc_services((struct ble_gatt_test_service[]) { + { 1, 5, 0, {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, }, + { 10, 50, 0, {2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 }, }, + { 80, 120, 0, {3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3 }, }, + { 123, 678, 0, {4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4 }, }, + { 751, 999, 0, {5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 }, }, { 0 } }); - /*** Five services. */ - ble_gatt_test_misc_successful_disc_services((struct ble_gatt_service[]) { - { 1, 5, {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, }, - { 10, 50, {2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 }, }, - { 123, 678, {3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3 }, }, + /*** One 128-bit service, one 16-bit-service. */ + ble_gatt_test_misc_good_disc_services((struct ble_gatt_test_service[]) { + { 1, 5, 0, {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, }, + { 6, 7, 0x1234 }, { 0 } }); http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/f396cc2b/net/nimble/host/src/test/ble_hs_att_svr_test.c ---------------------------------------------------------------------- diff --git a/net/nimble/host/src/test/ble_hs_att_svr_test.c b/net/nimble/host/src/test/ble_hs_att_svr_test.c index a1a8479..37f3f11 100644 --- a/net/nimble/host/src/test/ble_hs_att_svr_test.c +++ b/net/nimble/host/src/test/ble_hs_att_svr_test.c @@ -331,8 +331,7 @@ TEST_CASE(ble_att_svr_test_mtu) { /*** MTU too low; should pretend peer sent default value instead. */ ble_att_svr_test_misc_mtu_exchange(BLE_ATT_MTU_DFLT, 5, - BLE_ATT_MTU_DFLT, - BLE_ATT_MTU_DFLT); + BLE_ATT_MTU_DFLT, BLE_ATT_MTU_DFLT); /*** MTUs equal. */ ble_att_svr_test_misc_mtu_exchange(50, 50, 50, 50); @@ -453,8 +452,8 @@ TEST_CASE(ble_att_svr_test_find_info) uint8_t uuid1[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; uint8_t uuid2[16] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}; uint8_t uuid3[16] = { - 0x00, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x10, 0x00, - 0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb + 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, + 0x00, 0x10, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00 }; int rc; @@ -630,13 +629,13 @@ TEST_CASE(ble_att_svr_test_find_type_value) uint16_t handle4; uint16_t handle5; uint8_t uuid1[16] = { - 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x10, 0x00, - 0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb + 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, + 0x00, 0x10, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00 }; uint8_t uuid2[16] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}; uint8_t uuid3[16] = { - 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x10, 0x00, - 0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb + 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, + 0x00, 0x10, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00 }; int rc; http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/f396cc2b/net/nimble/host/src/test/ble_hs_uuid_test.c ---------------------------------------------------------------------- diff --git a/net/nimble/host/src/test/ble_hs_uuid_test.c b/net/nimble/host/src/test/ble_hs_uuid_test.c index 698aa32..ca9c239 100644 --- a/net/nimble/host/src/test/ble_hs_uuid_test.c +++ b/net/nimble/host/src/test/ble_hs_uuid_test.c @@ -26,45 +26,51 @@ TEST_CASE(ble_hs_uuid_test_128_to_16) /*** RFCOMM */ uuid16 = ble_hs_uuid_16bit(((uint8_t[]) { - 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x10, 0x00, - 0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb})); + 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, + 0x00, 0x10, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00 + })); TEST_ASSERT(uuid16 == 0x0003); /*** BNEP */ uuid16 = ble_hs_uuid_16bit(((uint8_t[]) { - 0x00, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x10, 0x00, - 0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb})); + 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, + 0x00, 0x10, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00 + })); TEST_ASSERT(uuid16 == 0x000f); /*** L2CAP */ uuid16 = ble_hs_uuid_16bit(((uint8_t[]) { - 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x10, 0x00, - 0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb})); + 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, + 0x00, 0x10, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00 + })); TEST_ASSERT(uuid16 == 0x0100); /*** ObEXObjectPush */ uuid16 = ble_hs_uuid_16bit(((uint8_t[]) { - 0x00, 0x00, 0x11, 0x05, 0x00, 0x00, 0x10, 0x00, - 0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb})); + 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, + 0x00, 0x10, 0x00, 0x00, 0x05, 0x11, 0x00, 0x00 + })); TEST_ASSERT(uuid16 == 0x1105); /*** Invalid base. */ uuid16 = ble_hs_uuid_16bit(((uint8_t[]) { - 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x10, 0x00, - 0x80, 0x00, 0x00, 0x80, 0x5f, 0x9c, 0x34, 0xfb})); + 0xfb, 0x34, 0x9c, 0x5f, 0x80, 0x00, 0x00, 0x80, + 0x00, 0x10, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00 + })); TEST_ASSERT(uuid16 == 0); /*** Invalid prefix. */ uuid16 = ble_hs_uuid_16bit(((uint8_t[]) { - 0x01, 0x00, 0x00, 0x03, 0x00, 0x00, 0x10, 0x00, - 0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb})); + 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, + 0x00, 0x10, 0x00, 0x00, 0x03, 0x00, 0x00, 0x01 + })); TEST_ASSERT(uuid16 == 0); /*** 16-bit UUID of 0. */ uuid16 = ble_hs_uuid_16bit(((uint8_t[]) { - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, - 0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb})); - TEST_ASSERT(uuid16 == 0); + 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, + 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 + })); } TEST_SUITE(ble_hs_uuid_test_suite)