|
| 1 | +/* |
| 2 | + This file is part of the ArduinoBLE library. |
| 3 | + Copyright (c) 2018 Arduino SA. All rights reserved. |
| 4 | +
|
| 5 | + This library is free software; you can redistribute it and/or |
| 6 | + modify it under the terms of the GNU Lesser General Public |
| 7 | + License as published by the Free Software Foundation; either |
| 8 | + version 2.1 of the License, or (at your option) any later version. |
| 9 | +
|
| 10 | + This library is distributed in the hope that it will be useful, |
| 11 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | + Lesser General Public License for more details. |
| 14 | +
|
| 15 | + You should have received a copy of the GNU Lesser General Public |
| 16 | + License along with this library; if not, write to the Free Software |
| 17 | + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | +*/ |
| 19 | + |
| 20 | +#include <catch2/catch_test_macros.hpp> |
| 21 | + |
| 22 | +#define private public |
| 23 | +#define protected public |
| 24 | + |
| 25 | +#include "FakeBLELocalDevice.h" |
| 26 | +#include "BLEAdvertisingData.h" |
| 27 | +#include "BLETypedCharacteristics.h" |
| 28 | +#include "BLELocalCharacteristic.h" |
| 29 | +#include "BLEStringCharacteristic.h" |
| 30 | +#include "BLEProperty.h" |
| 31 | + |
| 32 | + |
| 33 | +TEST_CASE("Test characteristic writeValue", "[ArduinoBLE::BLECharacteristic]") |
| 34 | +{ |
| 35 | + WHEN("Create a bool characteristic") |
| 36 | + { |
| 37 | + BLEBoolCharacteristic boolCharacteristic("Bool", BLEBroadcast| BLEIndicate | BLENotify ); |
| 38 | + bool v = false;; |
| 39 | + int written = boolCharacteristic.writeValue(v); |
| 40 | + REQUIRE( written == sizeof(bool) ); |
| 41 | + |
| 42 | + boolCharacteristic.broadcast(); |
| 43 | + written = boolCharacteristic.writeValue(v); |
| 44 | + REQUIRE( written == sizeof(bool) ); |
| 45 | + |
| 46 | + BLEDevice device; |
| 47 | + boolCharacteristic.local()->writeCccdValue(device, 0x002); |
| 48 | + written = boolCharacteristic.writeValue(v); |
| 49 | + /* No peers connected */ |
| 50 | + REQUIRE( written == 0 ); |
| 51 | + boolCharacteristic.local()->writeCccdValue(device, 0x001); |
| 52 | + written = boolCharacteristic.writeValue(v); |
| 53 | + /* No peers connected */ |
| 54 | + REQUIRE( written == 0 ); |
| 55 | + } |
| 56 | + |
| 57 | + WHEN("Create a boolean characteristic") |
| 58 | + { |
| 59 | + BLEBooleanCharacteristic booleanCharacteristic("Boolean", BLEBroadcast| BLEIndicate | BLENotify); |
| 60 | + bool v = false; |
| 61 | + int written = booleanCharacteristic.writeValue(v); |
| 62 | + REQUIRE( written == sizeof(bool) ); |
| 63 | + |
| 64 | + booleanCharacteristic.broadcast(); |
| 65 | + written = booleanCharacteristic.writeValue(v); |
| 66 | + REQUIRE( written == sizeof(bool) ); |
| 67 | + |
| 68 | + BLEDevice device; |
| 69 | + booleanCharacteristic.local()->writeCccdValue(device, 0x002); |
| 70 | + written = booleanCharacteristic.writeValue(v); |
| 71 | + /* No peers connected */ |
| 72 | + REQUIRE( written == 0 ); |
| 73 | + booleanCharacteristic.local()->writeCccdValue(device, 0x001); |
| 74 | + written = booleanCharacteristic.writeValue(v); |
| 75 | + /* No peers connected */ |
| 76 | + REQUIRE( written == 0 ); |
| 77 | + } |
| 78 | + |
| 79 | + WHEN("Create a char characteristic") |
| 80 | + { |
| 81 | + BLECharCharacteristic charCharacteristic("Char", BLEBroadcast| BLEIndicate | BLENotify); |
| 82 | + char v = 'a'; |
| 83 | + int written = charCharacteristic.writeValue(v); |
| 84 | + REQUIRE( written == sizeof(char) ); |
| 85 | + |
| 86 | + charCharacteristic.broadcast(); |
| 87 | + written = charCharacteristic.writeValue(v); |
| 88 | + REQUIRE( written == sizeof(char) ); |
| 89 | + |
| 90 | + BLEDevice device; |
| 91 | + charCharacteristic.local()->writeCccdValue(device, 0x002); |
| 92 | + written = charCharacteristic.writeValue(v); |
| 93 | + /* No peers connected */ |
| 94 | + REQUIRE( written == 0 ); |
| 95 | + charCharacteristic.local()->writeCccdValue(device, 0x001); |
| 96 | + written = charCharacteristic.writeValue(v); |
| 97 | + /* No peers connected */ |
| 98 | + REQUIRE( written == 0 ); |
| 99 | + } |
| 100 | + |
| 101 | + WHEN("Create a unsigned char characteristic") |
| 102 | + { |
| 103 | + BLEUnsignedCharCharacteristic unsignedCharCharacteristic("UnsignedChar", BLEBroadcast| BLEIndicate | BLENotify); |
| 104 | + unsigned char v = 0x01; |
| 105 | + int written = unsignedCharCharacteristic.writeValue(v); |
| 106 | + REQUIRE( written == sizeof(unsigned char) ); |
| 107 | + |
| 108 | + unsignedCharCharacteristic.broadcast(); |
| 109 | + written = unsignedCharCharacteristic.writeValue(v); |
| 110 | + REQUIRE( written == sizeof(unsigned char) ); |
| 111 | + |
| 112 | + BLEDevice device; |
| 113 | + unsignedCharCharacteristic.local()->writeCccdValue(device, 0x002); |
| 114 | + written = unsignedCharCharacteristic.writeValue(v); |
| 115 | + /* No peers connected */ |
| 116 | + REQUIRE( written == 0 ); |
| 117 | + unsignedCharCharacteristic.local()->writeCccdValue(device, 0x001); |
| 118 | + written = unsignedCharCharacteristic.writeValue(v); |
| 119 | + /* No peers connected */ |
| 120 | + REQUIRE( written == 0 ); |
| 121 | + } |
| 122 | + |
| 123 | + WHEN("Create a byte characteristic") |
| 124 | + { |
| 125 | + BLEByteCharacteristic byteCharacteristic("Byte", BLEBroadcast| BLEIndicate | BLENotify); |
| 126 | + byte v = 0x01; |
| 127 | + int written = byteCharacteristic.writeValue(v); |
| 128 | + REQUIRE( written == sizeof(byte) ); |
| 129 | + |
| 130 | + byteCharacteristic.broadcast(); |
| 131 | + written = byteCharacteristic.writeValue(v); |
| 132 | + REQUIRE( written == sizeof(byte) ); |
| 133 | + |
| 134 | + BLEDevice device; |
| 135 | + byteCharacteristic.local()->writeCccdValue(device, 0x002); |
| 136 | + written = byteCharacteristic.writeValue(v); |
| 137 | + /* No peers connected */ |
| 138 | + REQUIRE( written == 0 ); |
| 139 | + byteCharacteristic.local()->writeCccdValue(device, 0x001); |
| 140 | + written = byteCharacteristic.writeValue(v); |
| 141 | + /* No peers connected */ |
| 142 | + REQUIRE( written == 0 ); |
| 143 | + } |
| 144 | + |
| 145 | + WHEN("Create a short characteristic") |
| 146 | + { |
| 147 | + BLEShortCharacteristic shortCharacteristic("Short", BLEBroadcast| BLEIndicate | BLENotify); |
| 148 | + short v = -1; |
| 149 | + int written = shortCharacteristic.writeValue(v); |
| 150 | + REQUIRE( written == sizeof(short) ); |
| 151 | + |
| 152 | + shortCharacteristic.broadcast(); |
| 153 | + written = shortCharacteristic.writeValue(v); |
| 154 | + REQUIRE( written == sizeof(short) ); |
| 155 | + |
| 156 | + BLEDevice device; |
| 157 | + shortCharacteristic.local()->writeCccdValue(device, 0x002); |
| 158 | + written = shortCharacteristic.writeValue(v); |
| 159 | + /* No peers connected */ |
| 160 | + REQUIRE( written == 0 ); |
| 161 | + shortCharacteristic.local()->writeCccdValue(device, 0x001); |
| 162 | + written = shortCharacteristic.writeValue(v); |
| 163 | + /* No peers connected */ |
| 164 | + REQUIRE( written == 0 ); |
| 165 | + } |
| 166 | + |
| 167 | + WHEN("Create a unsigned short characteristic") |
| 168 | + { |
| 169 | + BLEUnsignedShortCharacteristic unsignedShortCharacteristic("UnsignedShort", BLEBroadcast| BLEIndicate | BLENotify); |
| 170 | + unsigned short v = 1; |
| 171 | + int written = unsignedShortCharacteristic.writeValue(v); |
| 172 | + REQUIRE( written == sizeof(unsigned short) ); |
| 173 | + |
| 174 | + unsignedShortCharacteristic.broadcast(); |
| 175 | + written = unsignedShortCharacteristic.writeValue(v); |
| 176 | + REQUIRE( written == sizeof(unsigned short) ); |
| 177 | + |
| 178 | + BLEDevice device; |
| 179 | + unsignedShortCharacteristic.local()->writeCccdValue(device, 0x002); |
| 180 | + written = unsignedShortCharacteristic.writeValue(v); |
| 181 | + /* No peers connected */ |
| 182 | + REQUIRE( written == 0 ); |
| 183 | + unsignedShortCharacteristic.local()->writeCccdValue(device, 0x001); |
| 184 | + written = unsignedShortCharacteristic.writeValue(v); |
| 185 | + /* No peers connected */ |
| 186 | + REQUIRE( written == 0 ); |
| 187 | + } |
| 188 | + |
| 189 | + WHEN("Create a word characteristic") |
| 190 | + { |
| 191 | + BLEWordCharacteristic wordCharacteristic("Word", BLEBroadcast| BLEIndicate | BLENotify); |
| 192 | + word v = -1; |
| 193 | + int written = wordCharacteristic.writeValue(v); |
| 194 | + REQUIRE( written == sizeof(word) ); |
| 195 | + |
| 196 | + wordCharacteristic.broadcast(); |
| 197 | + written = wordCharacteristic.writeValue(v); |
| 198 | + REQUIRE( written == sizeof(word) ); |
| 199 | + |
| 200 | + BLEDevice device; |
| 201 | + wordCharacteristic.local()->writeCccdValue(device, 0x002); |
| 202 | + written = wordCharacteristic.writeValue(v); |
| 203 | + /* No peers connected */ |
| 204 | + REQUIRE( written == 0 ); |
| 205 | + wordCharacteristic.local()->writeCccdValue(device, 0x001); |
| 206 | + written = wordCharacteristic.writeValue(v); |
| 207 | + /* No peers connected */ |
| 208 | + REQUIRE( written == 0 ); |
| 209 | + } |
| 210 | + |
| 211 | + WHEN("Create a int characteristic") |
| 212 | + { |
| 213 | + BLEIntCharacteristic intCharacteristic("Int", BLEBroadcast| BLEIndicate | BLENotify); |
| 214 | + int v = -1; |
| 215 | + int written = intCharacteristic.writeValue(v); |
| 216 | + REQUIRE( written == sizeof(int) ); |
| 217 | + |
| 218 | + intCharacteristic.broadcast(); |
| 219 | + written = intCharacteristic.writeValue(v); |
| 220 | + REQUIRE( written == sizeof(int) ); |
| 221 | + |
| 222 | + BLEDevice device; |
| 223 | + intCharacteristic.local()->writeCccdValue(device, 0x002); |
| 224 | + written = intCharacteristic.writeValue(v); |
| 225 | + /* No peers connected */ |
| 226 | + REQUIRE( written == 0 ); |
| 227 | + intCharacteristic.local()->writeCccdValue(device, 0x001); |
| 228 | + written = intCharacteristic.writeValue(v); |
| 229 | + /* No peers connected */ |
| 230 | + REQUIRE( written == 0 ); |
| 231 | + } |
| 232 | + |
| 233 | + WHEN("Create a unsigned int characteristic") |
| 234 | + { |
| 235 | + BLEUnsignedIntCharacteristic unsignedIntCharacteristic("UnsignedInt", BLEBroadcast| BLEIndicate | BLENotify); |
| 236 | + unsigned int v = 1; |
| 237 | + int written = unsignedIntCharacteristic.writeValue(v); |
| 238 | + REQUIRE( written == sizeof(unsigned int) ); |
| 239 | + |
| 240 | + unsignedIntCharacteristic.broadcast(); |
| 241 | + written = unsignedIntCharacteristic.writeValue(v); |
| 242 | + REQUIRE( written == sizeof(unsigned int) ); |
| 243 | + |
| 244 | + BLEDevice device; |
| 245 | + unsignedIntCharacteristic.local()->writeCccdValue(device, 0x002); |
| 246 | + written = unsignedIntCharacteristic.writeValue(v); |
| 247 | + /* No peers connected */ |
| 248 | + REQUIRE( written == 0 ); |
| 249 | + unsignedIntCharacteristic.local()->writeCccdValue(device, 0x001); |
| 250 | + written = unsignedIntCharacteristic.writeValue(v); |
| 251 | + /* No peers connected */ |
| 252 | + REQUIRE( written == 0 ); |
| 253 | + } |
| 254 | + |
| 255 | + WHEN("Create a long characteristic") |
| 256 | + { |
| 257 | + BLELongCharacteristic longCharacteristic("Long", BLEBroadcast| BLEIndicate | BLENotify); |
| 258 | + long v = -1; |
| 259 | + int written = longCharacteristic.writeValue(v); |
| 260 | + REQUIRE( written == sizeof(long) ); |
| 261 | + |
| 262 | + longCharacteristic.broadcast(); |
| 263 | + written = longCharacteristic.writeValue(v); |
| 264 | + REQUIRE( written == sizeof(long) ); |
| 265 | + |
| 266 | + BLEDevice device; |
| 267 | + longCharacteristic.local()->writeCccdValue(device, 0x002); |
| 268 | + written = longCharacteristic.writeValue(v); |
| 269 | + /* No peers connected */ |
| 270 | + REQUIRE( written == 0 ); |
| 271 | + longCharacteristic.local()->writeCccdValue(device, 0x001); |
| 272 | + written = longCharacteristic.writeValue(v); |
| 273 | + /* No peers connected */ |
| 274 | + REQUIRE( written == 0 ); |
| 275 | + } |
| 276 | + |
| 277 | + WHEN("Create a unsigned long characteristic") |
| 278 | + { |
| 279 | + BLEUnsignedLongCharacteristic unsignedLongCharacteristic("UnsignedLong", BLEBroadcast| BLEIndicate | BLENotify); |
| 280 | + unsigned long v = 1; |
| 281 | + int written = unsignedLongCharacteristic.writeValue(v); |
| 282 | + REQUIRE( written == sizeof(unsigned long) ); |
| 283 | + |
| 284 | + unsignedLongCharacteristic.broadcast(); |
| 285 | + written = unsignedLongCharacteristic.writeValue(v); |
| 286 | + REQUIRE( written == sizeof(unsigned long) ); |
| 287 | + |
| 288 | + BLEDevice device; |
| 289 | + unsignedLongCharacteristic.local()->writeCccdValue(device, 0x002); |
| 290 | + written = unsignedLongCharacteristic.writeValue(v); |
| 291 | + /* No peers connected */ |
| 292 | + REQUIRE( written == 0 ); |
| 293 | + unsignedLongCharacteristic.local()->writeCccdValue(device, 0x001); |
| 294 | + written = unsignedLongCharacteristic.writeValue(v); |
| 295 | + /* No peers connected */ |
| 296 | + REQUIRE( written == 0 ); |
| 297 | + } |
| 298 | + |
| 299 | + WHEN("Create a float characteristic") |
| 300 | + { |
| 301 | + BLEFloatCharacteristic floatCharacteristic("Float", BLEBroadcast| BLEIndicate | BLENotify); |
| 302 | + float v = -1.0f; |
| 303 | + int written = floatCharacteristic.writeValue(v); |
| 304 | + REQUIRE( written == sizeof(float) ); |
| 305 | + |
| 306 | + floatCharacteristic.broadcast(); |
| 307 | + written = floatCharacteristic.writeValue(v); |
| 308 | + REQUIRE( written == sizeof(float) ); |
| 309 | + |
| 310 | + BLEDevice device; |
| 311 | + floatCharacteristic.local()->writeCccdValue(device, 0x002); |
| 312 | + written = floatCharacteristic.writeValue(v); |
| 313 | + /* No peers connected */ |
| 314 | + REQUIRE( written == 0 ); |
| 315 | + floatCharacteristic.local()->writeCccdValue(device, 0x001); |
| 316 | + written = floatCharacteristic.writeValue(v); |
| 317 | + /* No peers connected */ |
| 318 | + REQUIRE( written == 0 ); |
| 319 | + } |
| 320 | + |
| 321 | + WHEN("Create a double characteristic") |
| 322 | + { |
| 323 | + BLEDoubleCharacteristic doubleCharacteristic("Double", BLEBroadcast| BLEIndicate | BLENotify); |
| 324 | + double v = -1.0; |
| 325 | + int written = doubleCharacteristic.writeValue(v); |
| 326 | + REQUIRE( written == sizeof(double) ); |
| 327 | + |
| 328 | + doubleCharacteristic.broadcast(); |
| 329 | + written = doubleCharacteristic.writeValue(v); |
| 330 | + REQUIRE( written == sizeof(double) ); |
| 331 | + |
| 332 | + BLEDevice device; |
| 333 | + doubleCharacteristic.local()->writeCccdValue(device, 0x002); |
| 334 | + written = doubleCharacteristic.writeValue(v); |
| 335 | + /* No peers connected */ |
| 336 | + REQUIRE( written == 0 ); |
| 337 | + doubleCharacteristic.local()->writeCccdValue(device, 0x001); |
| 338 | + written = doubleCharacteristic.writeValue(v); |
| 339 | + /* No peers connected */ |
| 340 | + REQUIRE( written == 0 ); |
| 341 | + } |
| 342 | + |
| 343 | + WHEN("Create a string characteristic") |
| 344 | + { |
| 345 | + const int maxStringLength = 64; |
| 346 | + BLEStringCharacteristic stringCharacteristic("String", BLEBroadcast| BLEIndicate | BLENotify, maxStringLength); |
| 347 | + const char* v = "Hello"; |
| 348 | + int written = stringCharacteristic.writeValue(v); |
| 349 | + REQUIRE( written == min(strlen(v), maxStringLength) ); |
| 350 | + |
| 351 | + stringCharacteristic.broadcast(); |
| 352 | + written = stringCharacteristic.writeValue(v); |
| 353 | + REQUIRE( written == min(strlen(v), maxStringLength) ); |
| 354 | + |
| 355 | + BLEDevice device; |
| 356 | + stringCharacteristic.local()->writeCccdValue(device, 0x002); |
| 357 | + written = stringCharacteristic.writeValue(v); |
| 358 | + /* No peers connected */ |
| 359 | + REQUIRE( written == 0 ); |
| 360 | + stringCharacteristic.local()->writeCccdValue(device, 0x001); |
| 361 | + written = stringCharacteristic.writeValue(v); |
| 362 | + /* No peers connected */ |
| 363 | + REQUIRE( written == 0 ); |
| 364 | + } |
| 365 | + |
| 366 | + WHEN("Create a too long string characteristic") |
| 367 | + { |
| 368 | + const int maxStringLength = 4; |
| 369 | + BLEStringCharacteristic stringCharacteristic("String", BLEBroadcast| BLEIndicate | BLENotify, maxStringLength); |
| 370 | + const char* v = "Hello"; |
| 371 | + int written = stringCharacteristic.writeValue(v); |
| 372 | + REQUIRE( written == min(strlen(v), maxStringLength) ); |
| 373 | + |
| 374 | + stringCharacteristic.broadcast(); |
| 375 | + written = stringCharacteristic.writeValue(v); |
| 376 | + REQUIRE( written == min(strlen(v), maxStringLength) ); |
| 377 | + |
| 378 | + BLEDevice device; |
| 379 | + stringCharacteristic.local()->writeCccdValue(device, 0x002); |
| 380 | + written = stringCharacteristic.writeValue(v); |
| 381 | + /* No peers connected */ |
| 382 | + REQUIRE( written == 0 ); |
| 383 | + stringCharacteristic.local()->writeCccdValue(device, 0x001); |
| 384 | + written = stringCharacteristic.writeValue(v); |
| 385 | + /* No peers connected */ |
| 386 | + REQUIRE( written == 0 ); |
| 387 | + } |
| 388 | + |
| 389 | +} |
0 commit comments