Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 | /* main.c - Application main entry point */ /* * Copyright (c) 2019 Intel Corporation * * SPDX-License-Identifier: Apache-2.0 */ #include <zephyr.h> #include <stddef.h> #include <ztest.h> #include <bluetooth/buf.h> #include <bluetooth/bluetooth.h> #include <bluetooth/gatt.h> /* Custom Service Variables */ static struct bt_uuid_128 test_uuid = BT_UUID_INIT_128( 0xf0, 0xde, 0xbc, 0x9a, 0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12); static struct bt_uuid_128 test_chrc_uuid = BT_UUID_INIT_128( 0xf2, 0xde, 0xbc, 0x9a, 0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12); static u8_t test_value[] = { 'T', 'e', 's', 't', '\0' }; static struct bt_uuid_128 test1_uuid = BT_UUID_INIT_128( 0xf4, 0xde, 0xbc, 0x9a, 0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12); static const struct bt_uuid_128 test1_nfy_uuid = BT_UUID_INIT_128( 0xf5, 0xde, 0xbc, 0x9a, 0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12); static u8_t nfy_enabled; static void test1_ccc_cfg_changed(const struct bt_gatt_attr *attr, u16_t value) { nfy_enabled = (value == BT_GATT_CCC_NOTIFY) ? 1 : 0; } static ssize_t read_test(struct bt_conn *conn, const struct bt_gatt_attr *attr, void *buf, u16_t len, u16_t offset) { const char *value = attr->user_data; return bt_gatt_attr_read(conn, attr, buf, len, offset, value, strlen(value)); } static ssize_t write_test(struct bt_conn *conn, const struct bt_gatt_attr *attr, const void *buf, u16_t len, u16_t offset, u8_t flags) { u8_t *value = attr->user_data; if (offset + len > sizeof(test_value)) { return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET); } memcpy(value + offset, buf, len); return len; } static struct bt_gatt_attr test_attrs[] = { /* Vendor Primary Service Declaration */ BT_GATT_PRIMARY_SERVICE(&test_uuid), BT_GATT_CHARACTERISTIC(&test_chrc_uuid.uuid, BT_GATT_CHRC_READ | BT_GATT_CHRC_WRITE, BT_GATT_PERM_READ_AUTHEN | BT_GATT_PERM_WRITE_AUTHEN, read_test, write_test, test_value), }; static struct bt_gatt_service test_svc = BT_GATT_SERVICE(test_attrs); static struct bt_gatt_attr test1_attrs[] = { /* Vendor Primary Service Declaration */ BT_GATT_PRIMARY_SERVICE(&test1_uuid), BT_GATT_CHARACTERISTIC(&test1_nfy_uuid.uuid, BT_GATT_CHRC_NOTIFY, BT_GATT_PERM_NONE, NULL, NULL, &nfy_enabled), BT_GATT_CCC(test1_ccc_cfg_changed), }; static struct bt_gatt_service test1_svc = BT_GATT_SERVICE(test1_attrs); void test_gatt_register(void) { /* Attempt to register services */ zassert_false(bt_gatt_service_register(&test_svc), "Test service registration failed"); zassert_false(bt_gatt_service_register(&test1_svc), "Test service1 registration failed"); /* Attempt to register already registered services */ zassert_true(bt_gatt_service_register(&test_svc), "Test service duplicate succeeded"); zassert_true(bt_gatt_service_register(&test1_svc), "Test service1 duplicate succeeded"); } void test_gatt_unregister(void) { /* Attempt to unregister last */ zassert_false(bt_gatt_service_unregister(&test1_svc), "Test service1 unregister failed"); zassert_false(bt_gatt_service_register(&test1_svc), "Test service1 re-registration failed"); /* Attempt to unregister first/middle */ zassert_false(bt_gatt_service_unregister(&test_svc), "Test service unregister failed"); zassert_false(bt_gatt_service_register(&test_svc), "Test service re-registration failed"); /* Attempt to unregister all reverse order */ zassert_false(bt_gatt_service_unregister(&test1_svc), "Test service1 unregister failed"); zassert_false(bt_gatt_service_unregister(&test_svc), "Test service unregister failed"); zassert_false(bt_gatt_service_register(&test_svc), "Test service registration failed"); zassert_false(bt_gatt_service_register(&test1_svc), "Test service1 registration failed"); /* Attempt to unregister all same order */ zassert_false(bt_gatt_service_unregister(&test_svc), "Test service1 unregister failed"); zassert_false(bt_gatt_service_unregister(&test1_svc), "Test service unregister failed"); } static u8_t count_attr(const struct bt_gatt_attr *attr, void *user_data) { u16_t *count = user_data; (*count)++; return BT_GATT_ITER_CONTINUE; } static u8_t find_attr(const struct bt_gatt_attr *attr, void *user_data) { const struct bt_gatt_attr **tmp = user_data; *tmp = attr; return BT_GATT_ITER_CONTINUE; } void test_gatt_foreach(void) { const struct bt_gatt_attr *attr; u16_t num = 0; /* Attempt to register services */ zassert_false(bt_gatt_service_register(&test_svc), "Test service registration failed"); zassert_false(bt_gatt_service_register(&test1_svc), "Test service1 registration failed"); /* Iterate attributes */ bt_gatt_foreach_attr(test_attrs[0].handle, 0xffff, count_attr, &num); zassert_equal(num, 7, "Number of attributes don't match"); /* Iterate 1 attribute */ num = 0; bt_gatt_foreach_attr_type(test_attrs[0].handle, 0xffff, NULL, NULL, 1, count_attr, &num); zassert_equal(num, 1, "Number of attributes don't match"); /* Find attribute by UUID */ attr = NULL; bt_gatt_foreach_attr_type(test_attrs[0].handle, 0xffff, &test_chrc_uuid.uuid, NULL, 0, find_attr, &attr); zassert_not_null(attr, "Attribute don't match"); if (attr) { zassert_equal(attr->uuid, &test_chrc_uuid.uuid, "Attribute UUID don't match"); } /* Find attribute by DATA */ attr = NULL; bt_gatt_foreach_attr_type(test_attrs[0].handle, 0xffff, NULL, test_value, 0, find_attr, &attr); zassert_not_null(attr, "Attribute don't match"); if (attr) { zassert_equal(attr->user_data, test_value, "Attribute value don't match"); } /* Find all characteristics */ num = 0; bt_gatt_foreach_attr_type(test_attrs[0].handle, 0xffff, BT_UUID_GATT_CHRC, NULL, 0, count_attr, &num); zassert_equal(num, 2, "Number of attributes don't match"); /* Find 1 characteristic */ attr = NULL; bt_gatt_foreach_attr_type(test_attrs[0].handle, 0xffff, BT_UUID_GATT_CHRC, NULL, 1, find_attr, &attr); zassert_not_null(attr, "Attribute don't match"); /* Find attribute by UUID and DATA */ attr = NULL; bt_gatt_foreach_attr_type(test_attrs[0].handle, 0xffff, &test1_nfy_uuid.uuid, &nfy_enabled, 1, find_attr, &attr); zassert_not_null(attr, "Attribute don't match"); if (attr) { zassert_equal(attr->uuid, &test1_nfy_uuid.uuid, "Attribute UUID don't match"); zassert_equal(attr->user_data, &nfy_enabled, "Attribute value don't match"); } } void test_gatt_read(void) { const struct bt_gatt_attr *attr; u8_t buf[256]; ssize_t ret; /* Find attribute by UUID */ attr = NULL; bt_gatt_foreach_attr_type(test_attrs[0].handle, 0xffff, &test_chrc_uuid.uuid, NULL, 0, find_attr, &attr); zassert_not_null(attr, "Attribute don't match"); zassert_equal(attr->uuid, &test_chrc_uuid.uuid, "Attribute UUID don't match"); ret = attr->read(NULL, attr, (void *)buf, sizeof(buf), 0); zassert_equal(ret, strlen(test_value), "Attribute read unexpected return"); zassert_mem_equal(buf, test_value, ret, "Attribute read value don't match"); } void test_gatt_write(void) { const struct bt_gatt_attr *attr; char *value = " "; ssize_t ret; /* Find attribute by UUID */ attr = NULL; bt_gatt_foreach_attr_type(test_attrs[0].handle, 0xffff, &test_chrc_uuid.uuid, NULL, 0, find_attr, &attr); zassert_not_null(attr, "Attribute don't match"); ret = attr->write(NULL, attr, (void *)value, strlen(value), 0, 0); zassert_equal(ret, strlen(value), "Attribute write unexpected return"); zassert_mem_equal(value, test_value, ret, "Attribute write value don't match"); } /*test case main entry*/ void test_main(void) { ztest_test_suite(test_gatt, ztest_unit_test(test_gatt_register), ztest_unit_test(test_gatt_unregister), ztest_unit_test(test_gatt_foreach), ztest_unit_test(test_gatt_read), ztest_unit_test(test_gatt_write)); ztest_run_test_suite(test_gatt); } |