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 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935
|
unit unitLibGattlib;
// Wrapper pour gattlib
{$mode ObjFPC}{$H+}
{$IFDEF FPC}
{$PACKRECORDS C}
{$ENDIF}
interface
//{$UNDEF LINUX}
uses
Classes
, SysUtils
{$IFDEF LINUX}
, Bluetooth
, ctypes
, Sockets
, ptypes
;
{$ENDIF}
; /////
{$IFDEF LINUX}
const LIB_GATTLIB = 'LibGattlib.so';
{$ELSE}
const LIB_GATTLIB = 'LibGattlib.dll';
{$ENDIF}
// Constantes GATTLIB
{ GattLib note: BD Address have only been introduced into Bluez v4.100. }
{ Prior to this version, only BDADDR_BREDR can be supported }
{ BD Address type }
const
BDADDR_BREDR = $00;
BDADDR_LE_PUBLIC = $01;
BDADDR_LE_RANDOM = $02;
const
ATT_MAX_VALUE_LEN = 32; // En attente
ATT_MAX_MTU = ATT_MAX_VALUE_LEN;
const
GATTLIB_SUCCESS = 0;
GATTLIB_INVALID_PARAMETER = 1;
GATTLIB_NOT_FOUND = 2;
GATTLIB_OUT_OF_MEMORY = 3;
GATTLIB_NOT_SUPPORTED = 4;
GATTLIB_DEVICE_ERROR = 5;
GATTLIB_ERROR_DBUS = 6;
GATTLIB_ERROR_BLUEZ = 7;
GATTLIB_ERROR_INTERNAL = 8;
{* @name GATT Characteristic Properties Bitfield values }
const
GATTLIB_CHARACTERISTIC_BROADCAST = $01;
GATTLIB_CHARACTERISTIC_READ = $02;
GATTLIB_CHARACTERISTIC_WRITE_WITHOUT_RESP = $04;
GATTLIB_CHARACTERISTIC_WRITE = $08;
GATTLIB_CHARACTERISTIC_NOTIFY = $10;
GATTLIB_CHARACTERISTIC_INDICATE = $20;
{* @name Options for gattlib_connect()
@note Options with the prefix `GATTLIB_CONNECTION_OPTIONS_LEGACY_`
is for Bluez prior to v5.42 (before Bluez) support
}
const
GATTLIB_CONNECTION_OPTIONS_LEGACY_BDADDR_LE_PUBLIC = 1 shl 0; // #define GATTLIB_CONNECTION_OPTIONS_LEGACY_BDADDR_LE_PUBLIC (1 << 0)
GATTLIB_CONNECTION_OPTIONS_LEGACY_BDADDR_LE_RANDOM = 1 shl 1; // #define GATTLIB_CONNECTION_OPTIONS_LEGACY_BDADDR_LE_RANDOM (1 << 1)
GATTLIB_CONNECTION_OPTIONS_LEGACY_BT_SEC_LOW = 1 shl 2; // #define GATTLIB_CONNECTION_OPTIONS_LEGACY_BT_SEC_LOW (1 << 2)
GATTLIB_CONNECTION_OPTIONS_LEGACY_BT_SEC_MEDIUM = 1 shl 3; // #define GATTLIB_CONNECTION_OPTIONS_LEGACY_BT_SEC_MEDIUM (1 << 3)
GATTLIB_CONNECTION_OPTIONS_LEGACY_BT_SEC_HIGH = 1 shl 4; // #define GATTLIB_CONNECTION_OPTIONS_LEGACY_BT_SEC_HIGH (1 << 4)
{ @name Discover filter }
const
GATTLIB_DISCOVER_FILTER_USE_NONE = 0;
GATTLIB_DISCOVER_FILTER_USE_UUID = 1 shl 0;
GATTLIB_DISCOVER_FILTER_USE_RSSI = 1 shl 1;
GATTLIB_DISCOVER_FILTER_NOTIFY_CHANGE = 1 shl 2;
{ @name Gattlib Eddystone types }
GATTLIB_EDDYSTONE_TYPE_UID = 1 shl 0;
GATTLIB_EDDYSTONE_TYPE_URL = 1 shl 1;
GATTLIB_EDDYSTONE_TYPE_TLM = 1 shl 2;
GATTLIB_EDDYSTONE_TYPE_EID = 1 shl 3;
GATTLIB_EDDYSTONE_LIMIT_RSSI = 1 shl 4;
{*@name Eddystone ID types defined by its specification: https://github.com/google/eddystone }
EDDYSTONE_TYPE_UID = $00;
EDDYSTONE_TYPE_URL = $10;
EDDYSTONE_TYPE_TLM = $20;
EDDYSTONE_TYPE_EID = $30;
GATTLIB_ERROR = 0;
GATTLIB_WARNING = 1;
GATTLIB_INFO = 2;
GATTLIB_DEBUG = 3;
GATTLIB_LOG_LEVEL = GATTLIB_DEBUG; // CACHE STRING "Define the minimum logging level for Gattlib (0=error, 1=warning, 2=info, 3=debug)")
const GATTLIB_CONNECTION_OPTIONS_LEGACY_DEFAULT : integer = GATTLIB_CONNECTION_OPTIONS_LEGACY_BDADDR_LE_PUBLIC OR
GATTLIB_CONNECTION_OPTIONS_LEGACY_BDADDR_LE_RANDOM OR
GATTLIB_CONNECTION_OPTIONS_LEGACY_BT_SEC_LOW;
//******************************************************************************
// Types bidon en attente des vrais types
type GIOChannel = integer; // Bidon
type GAttrib = integer; // Bidon
type _gatt_stream_t = integer; // Bidon !!!
{$IFDEF MSWINDOWS}
type uuid_t = array[0..15] of byte;
type uint8_t = PUInt8;
type uint16_t = PUInt16;
type uint32_t = PUInt32;
type int16_t = PInt16;
type size_t = LongInt;
{$ELSE}
// Le type uuid_t est déclaré dans le noyau de Linux
{$ENDIF}
//******************************************************************************
// *********** STRUCTURES ******************************************************
(* Structure to represent a GATT Service and its data in the BLE advertisement packet *)
type gattlib_advertisement_data_t = record // typedef struct {
uuid: uuid_t; // uuid_t uuid; /**< UUID of the GATT Service */
data: uint8_t; // uint8_t* data; /**< Data attached to the GATT Service */
data_length: size_t; // size_t data_length; /**< Length of data attached to the GATT Service */
end; // }
(* Structure to represent GATT Primary Service *)
type gattlib_primary_service_t = record
attr_handle_start : uint16_t; //**< First attribute handle of the GATT Primary Service */
attr_handle_end : uint16_t; //**< Last attibute handle of the GATT Primary Service */
uuid : uuid_t; //**< UUID of the Primary Service */
end;
(* Structure to represent GATT Characteristic *)
type gattlib_characteristic_t = record
handle : uint16_t; //**< Handle of the GATT characteristic */
properties : uint8_t; //**< Property of the GATT characteristic */
value_handle : uint16_t; //**< Handle for the value of the GATT characteristic */
uuid : uuid_t; //**<UUID of the GATT characteristic */
end;
(* Structure to represent GATT Descriptor *)
// TODO: Vérifier l'ordre des champs
type gattlib_descriptor_t = record
// Version dans https://github.com/andrewbarnham/metawearlinuxboilerplate/blob/master/gattlib.h
uuid : uuid_t ; // char uuid[MAX_LEN_UUID_STR + 1];
handle : uint16_t ;
uuid16 : uint16_t ;
//* Version 'officielle:'
//handle : uint16_t ; //**< Handle of the GATT Descriptor */
//uuid16 : uint16_t ; //**< UUID16 of the GATT Descriptor */
//uuid : uuid_t ; //**< UUID of the GATT Descriptor */
end;
(*
Version dans https://github.com/andrewbarnham/metawearlinuxboilerplate/blob/master/gattlib.h
typedef struct {
char uuid[MAX_LEN_UUID_STR + 1];
uint16_t handle;
uint16_t uuid16;
} gattlib_descriptor_t;
//*)
//******** HANDLERS ************************************************************
//typedef void (*gattlib_event_handler_t)(const uuid_t* uuid, const uint8_t* data, size_t data_length, void* user_data);
//type gattlib_event_handler_t = procedure(const uuid: uuid_t; const data: uint8_t; data_length: size_t; var user_data: Pointer); cdecl;
type gattlib_event_handler_t = procedure(var uuid: uuid_t; var data: uint8_t; data_length: size_t; var user_data: Pointer); cdecl;
type _gatt_connection_t = record // Bidon !!!
io : ^GIOChannel; // GIOChannel *io;
attrib : ^GAttrib; // GAttrib *attrib
notification_handler : gattlib_event_handler_t; // gattlib_event_handler_t notification_handler
notification_user_data : procedure; // void* notification_user_data;
indication_handler : gattlib_event_handler_t; // gattlib_event_handler_t indication_handler;
indication_user_data : procedure; // void* indication_user_data;
end;
type gatt_connection_t = type _gatt_connection_t; // typedef struct _gatt_connection_t gatt_connection_t;
type gatt_stream_t = type _gatt_stream_t; // typedef struct _gatt_stream_t gatt_stream_t;
(**
* @brief Handler called on disconnection
*
* @param connection Connection that is disconnecting
* @param user_data Data defined when calling `gattlib_register_on_disconnect()`
*)
//typedef void (*gattlib_disconnection_handler_t)(void* user_data);
type gattlib_disconnection_handler_t = procedure(var user_data: Pointer); cdecl;
(**
* @brief Handler called on new discovered BLE device
*
* @param adapter is the adapter that has found the BLE device
* @param addr is the MAC address of the BLE device
* @param name is the name of BLE device if advertised
* @param user_data Data defined when calling `gattlib_register_on_disconnect()`
*)
//typedef void (*gattlib_discovered_device_t)(void *adapter, const char* addr, const char* name, void *user_data);
type gattlib_discovered_device_t = procedure(var adapter: Pointer; const addr: PChar; const name: PChar; var user_data: Pointer); cdecl;
(*
* @brief Handler called on new discovered BLE device
*
* @param adapter is the adapter that has found the BLE device
* @param addr is the MAC address of the BLE device
* @param name is the name of BLE device if advertised
* @param advertisement_data is an array of Service UUID and their respective data
* @param advertisement_data_count is the number of elements in the advertisement_data array
* @param manufacturer_id is the ID of the Manufacturer ID
* @param manufacturer_data is the data following Manufacturer ID
* @param manufacturer_data_size is the size of manufacturer_data
* @param user_data Data defined when calling `gattlib_register_on_disconnect()`
*)
//typedef void (*gattlib_discovered_device_with_data_t)(void *adapter, const char* addr, const char* name,
// gattlib_advertisement_data_t *advertisement_data, size_t advertisement_data_count,
// uint16_t manufacturer_id, uint8_t *manufacturer_data, size_t manufacturer_data_size,
// void *user_data);
type gattlib_discovered_device_with_data_t = procedure (var adapter: Pointer; const addr: PChar; const name: PChar;
const advertisement_data: gattlib_advertisement_data_t; advertisement_data_count: size_t;
manufacturer_id: uint16_t;
const manufacturer_data: uint8_t;
manufacturer_data_size: size_t;
var user_data: Pointer); cdecl; external LIB_GATTLIB;
(* * @brief Handler called on asynchronous connection when connection is ready
*
* @param connection Connection that is disconnecting
* @param user_data Data defined when calling `gattlib_register_on_disconnect()`
*)
//typedef void (*gatt_connect_cb_t)(gatt_connection_t* connection, void* user_data);
type gatt_connect_cb_t = procedure(var connection: gatt_connection_t; var user_data: Pointer); cdecl; external LIB_GATTLIB;
(*
* @brief Callback called when GATT characteristic read value has been received
*
* @param buffer contains the value to read.
* @param buffer_len Length of the read data
*
*)
//typedef void* (*gatt_read_cb_t)(const void *buffer, size_t buffer_len);
type gatt_read_cb_t = procedure(var buffer: Pointer; buffer_len: size_t); cdecl; external LIB_GATTLIB;
//******************************************************************************
// Comment traduire ces deux déclarations ?
(*
* @brief Constant defining Eddystone common data UID in Advertisement data
*)
//extern const uuid_t gattlib_eddystone_common_data_uuid;
// *** HOWTO ? *** const uuid_t = gattlib_eddystone_common_data_uuid;
(**
* @brief List of prefix for Eddystone URL Scheme
*)
//extern const char *gattlib_eddystone_url_scheme_prefix[];
// *** HOWTO ? *** extern const char *gattlib_eddystone_url_scheme_prefix[];
//********* FONCTIONS **********************************************************
(**
* @brief Open Bluetooth adapter
*
* @param adapter_name With value NULL, the default adapter will be selected.
* @param adapter is the context of the newly opened adapter
*
* @return GATTLIB_SUCCESS on success or GATTLIB_* error code
*)
//int gattlib_adapter_open(const char* adapter_name, void** adapter);
function gattlib_adapter_open(const adapter_name: PChar; const adapter: PPointer): integer; cdecl; external LIB_GATTLIB;
(**
* @brief Enable Bluetooth scanning on a given adapter
*
* @param adapter is the context of the newly opened adapter
* @param discovered_device_cb is the function callback called for each new Bluetooth device discovered
* @param timeout defines the duration of the Bluetooth scanning. When timeout=0, we scan indefinitely.
* @param user_data is the data passed to the callback `discovered_device_cb()`
*
* @return GATTLIB_SUCCESS on success or GATTLIB_* error code
*)
//int gattlib_adapter_scan_enable(void* adapter, gattlib_discovered_device_t discovered_device_cb, size_t timeout, void *user_data);
function gattlib_adapter_scan_enable(var adapter: Pointer; discovered_device_cb: gattlib_discovered_device_t; timeout: size_t; var user_data: Pointer): integer; cdecl; external LIB_GATTLIB;
(**
* @brief Enable Bluetooth scanning on a given adapter
*
* This function will block until either the timeout has expired or gattlib_adapter_scan_disable() has been called.
*
* @param adapter is the context of the newly opened adapter
* @param uuid_list is a NULL-terminated list of UUIDs to filter. The rule only applies to advertised UUID.
* Returned devices would match any of the UUIDs of the list.
* @param rssi_threshold is the imposed RSSI threshold for the returned devices.
* @param enabled_filters defines the parameters to use for filtering. There are selected by using the macros
* GATTLIB_DISCOVER_FILTER_USE_UUID and GATTLIB_DISCOVER_FILTER_USE_RSSI.
* @param discovered_device_cb is the function callback called for each new Bluetooth device discovered
* @param timeout defines the duration of the Bluetooth scanning. When timeout=0, we scan indefinitely.
* @param user_data is the data passed to the callback `discovered_device_cb()`
*
* @return GATTLIB_SUCCESS on success or GATTLIB_* error code
*)
//int gattlib_adapter_scan_enable_with_filter(void *adapter, uuid_t **uuid_list,
// int16_t rssi_threshold, uint32_t enabled_filters,
// gattlib_discovered_device_t discovered_device_cb,
// size_t timeout, void *user_data);
function gattlib_adapter_scan_enable_with_filter(var adapter: Pointer;
const uuid_list: array of uuid_t;
rssi_threshold : int16_t;
enabled_filters: uint32_t;
discovered_device_cb: gattlib_discovered_device_t;
timeout: size_t;
var user_data: Pointer): integer; cdecl; external LIB_GATTLIB;
(**
* @brief Enable Bluetooth scanning on a given adapter (non-blocking)
*
* This function will return as soon as the BLE scan has been started.
*
* @param adapter is the context of the newly opened adapter
* @param uuid_list is a NULL-terminated list of UUIDs to filter. The rule only applies to advertised UUID.
* Returned devices would match any of the UUIDs of the list.
* @param rssi_threshold is the imposed RSSI threshold for the returned devices.
* @param enabled_filters defines the parameters to use for filtering. There are selected by using the macros
* GATTLIB_DISCOVER_FILTER_USE_UUID and GATTLIB_DISCOVER_FILTER_USE_RSSI.
* @param discovered_device_cb is the function callback called for each new Bluetooth device discovered
* @param timeout defines the duration of the Bluetooth scanning. When timeout=0, we scan indefinitely.
* @param user_data is the data passed to the callback `discovered_device_cb()`
*
* @return GATTLIB_SUCCESS on success or GATTLIB_* error code
*)
//int gattlib_adapter_scan_enable_with_filter_non_blocking(void *adapter, uuid_t **uuid_list,
// int16_t rssi_threshold, uint32_t enabled_filters,
// gattlib_discovered_device_t discovered_device_cb,
// size_t timeout, void *user_data);
function gattlib_adapter_scan_enable_with_filter_non_blocking(var adapter: Pointer;
const uuid_list: array of uuid_t;
rssi_threshold : int16_t;
enabled_filters: uint32_t;
discovered_device_cb: gattlib_discovered_device_t;
timeout: size_t;
var user_data: Pointer): integer; cdecl; external LIB_GATTLIB;
(*
* @brief Enable Eddystone Bluetooth Device scanning on a given adapter
*
* @param adapter is the context of the newly opened adapter
* @param rssi_threshold is the imposed RSSI threshold for the returned devices.
* @param eddystone_types defines the type(s) of Eddystone advertisement data type to select.
* The types are defined by the macros `GATTLIB_EDDYSTONE_TYPE_*`. The macro `GATTLIB_EDDYSTONE_LIMIT_RSSI`
* can also be used to limit RSSI with rssi_threshold.
* @param discovered_device_cb is the function callback called for each new Bluetooth device discovered
* @param timeout defines the duration of the Bluetooth scanning. When timeout=0, we scan indefinitely.
* @param user_data is the data passed to the callback `discovered_device_cb()`
*
* @return GATTLIB_SUCCESS on success or GATTLIB_* error code
**)
//int gattlib_adapter_scan_eddystone(void *adapter, int16_t rssi_threshold, uint32_t eddystone_types,
// gattlib_discovered_device_with_data_t discovered_device_cb, size_t timeout, void *user_data);
function gattlib_adapter_scan_eddystone(var adapter: Pointer;
rssi_threshold : int16_t;
eddystone_types: uint32_t;
discovered_device_cb: gattlib_discovered_device_with_data_t;
var user_data: Pointer): integer; cdecl; external LIB_GATTLIB;
(**
* @brief Disable Bluetooth scanning on a given adapter
*
* @param adapter is the context of the newly opened adapter
*
* @return GATTLIB_SUCCESS on success or GATTLIB_* error code
**)
//int gattlib_adapter_scan_disable(void* adapter);
function gattlib_adapter_scan_disable(const adapter: Pointer): integer; cdecl; external LIB_GATTLIB;
(***
* @brief Close Bluetooth adapter context
*
* @param adapter is the context of the newly opened adapter
*
* @return GATTLIB_SUCCESS on success or GATTLIB_* error code
*)
//int gattlib_adapter_close(void* adapter);
function gattlib_adapter_close(const adapter: Pointer): integer; cdecl; external LIB_GATTLIB;
(*
* @brief Function to connect to a BLE device
*
* @param adapter Local Adaptater interface. When passing NULL, we use default adapter.
* @param dst Remote Bluetooth address
* @param options Options to connect to BLE device. See `GATTLIB_CONNECTION_OPTIONS_*`
*)
//gatt_connection_t *gattlib_connect(void *adapter, const char *dst, unsigned long options);
function gattlib_connect(var adapter: Pointer;
const dst : PChar;
options : LongWord): gatt_connection_t; cdecl; external LIB_GATTLIB;
(**
* @brief Function to asynchronously connect to a BLE device
*
* @note This function is mainly used before Bluez v5.42 (prior to D-BUS support)
*
* @param adapter Local Adaptater interface. When passing NULL, we use default adapter.
* @param dst Remote Bluetooth address
* @param options Options to connect to BLE device. See `GATTLIB_CONNECTION_OPTIONS_*`
* @param connect_cb is the callback to call when the connection is established
* @param user_data is the user specific data to pass to the callback
*)
//gatt_connection_t *gattlib_connect_async(void *adapter, const char *dst,
// unsigned long options,
// gatt_connect_cb_t connect_cb, void* user_data);
function gattlib_connect_async(var adapter: Pointer;
const dst : PChar;
options : LongWord;
connect_cb : gatt_connect_cb_t;
var user_data: Pointer): gatt_connection_t; cdecl; external LIB_GATTLIB;
(**
* @brief Function to disconnect the GATT connection
*
* @param connection Active GATT connection
*
* @return GATTLIB_SUCCESS on success or GATTLIB_* error code
**)
//int gattlib_disconnect(gatt_connection_t* connection);
function gattlib_disconnect(var connection: gatt_connection_t): integer; cdecl; external LIB_GATTLIB;
(**
* @brief Function to register a callback on GATT disconnection
*
* @param connection Active GATT connection
* @param handler is the callaback to invoke on disconnection
* @param user_data is user specific data to pass to the callaback
*
* @return GATTLIB_SUCCESS on success or GATTLIB_* error code
*)
//void gattlib_register_on_disconnect(gatt_connection_t *connection, gattlib_disconnection_handler_t handler, void* user_data);
procedure gattlib_register_on_disconnect(var connection: gatt_connection_t;
handler: gattlib_disconnection_handler_t;
var user_data: Pointer); cdecl; external LIB_GATTLIB;
(*
* @brief Function to discover GATT Services
*
* @note This function can be used to force GATT services/characteristic discovery
*
* @param connection Active GATT connection
* @param services array of GATT services allocated by the function. Can be NULL.
* @param services_count Number of GATT services discovered. Can be NULL
*
* @return GATTLIB_SUCCESS on success or GATTLIB_* error code
*)
//int gattlib_discover_primary(gatt_connection_t* connection, gattlib_primary_service_t** services, int* services_count);
function gattlib_discover_primary(var connection: gatt_connection_t;
const services: array of gattlib_primary_service_t;
const services_count: integer): integer; cdecl; external LIB_GATTLIB;
(**
* @brief Function to discover GATT Characteristic
*
* @note This function can be used to force GATT services/characteristic discovery
*
* @param connection Active GATT connection
* @param start is the index of the first handle of the range
* @param end is the index of the last handle of the range
* @param characteristics array of GATT characteristics allocated by the function. Can be NULL.
* @param characteristics_count Number of GATT characteristics discovered. Can be NULL
*
* @return GATTLIB_SUCCESS on success or GATTLIB_* error code
*)
//int gattlib_discover_char_range(gatt_connection_t* connection, int start, int end, gattlib_characteristic_t** characteristics, int* characteristics_count);
function gattlib_discover_char_range(var connection: gatt_connection_t;
start: integer;
&end : integer; // Neutralisation par & du mot clé end , qui est un nom de variable valide en C
const characteristics: array of gattlib_characteristic_t;
const characteristics_count: integer): integer; cdecl; external LIB_GATTLIB;
(*
* @brief Function to discover GATT Characteristic
*
* @note This function can be used to force GATT services/characteristic discovery
*
* @param connection Active GATT connection
* @param characteristics array of GATT characteristics allocated by the function. Can be NULL.
* @param characteristics_count Number of GATT characteristics discovered. Can be NULL
*
* @return GATTLIB_SUCCESS on success or GATTLIB_* error code
**)
//int gattlib_discover_char(gatt_connection_t* connection, gattlib_characteristic_t** characteristics, int* characteristics_count);
function gattlib_discover_char(var connection: gatt_connection_t;
const characteristics: array of gattlib_characteristic_t;
const characteristics_count: integer): integer; cdecl; external LIB_GATTLIB;
(*
* @brief Function to discover GATT Descriptors in a range of handles
*
* @param connection Active GATT connection
* @param start is the index of the first handle of the range
* @param end is the index of the last handle of the range
* @param descriptors array of GATT descriptors allocated by the function. Can be NULL.
* @param descriptors_count Number of GATT descriptors discovered. Can be NULL
*
* @return GATTLIB_SUCCESS on success or GATTLIB_* error code
*)
// int gattlib_discover_desc_range(gatt_connection_t* connection, int start, int end, gattlib_descriptor_t** descriptors, int* descriptors_count);
function gattlib_discover_desc_range(var connection: gatt_connection_t;
start: integer;
&end : integer; // Neutralisation par & du mot clé end , qui est un nom de variable valide en C
const descriptors : array of gattlib_descriptor_t;
const descriptors_count: integer): integer; cdecl; external LIB_GATTLIB;
(*
* @brief Function to discover GATT Descriptor
*
* @param connection Active GATT connection
* @param descriptors array of GATT descriptors allocated by the function. Can be NULL.
* @param descriptors_count Number of GATT descriptors discovered. Can be NULL
*
* @return GATTLIB_SUCCESS on success or GATTLIB_* error code
**)
//int gattlib_discover_desc(gatt_connection_t* connection, gattlib_descriptor_t** descriptors, int* descriptors_count);
function gattlib_discover_desc(var connection: gatt_connection_t;
const descriptors : array of gattlib_descriptor_t;
const descriptors_count: integer): integer; cdecl; external LIB_GATTLIB;
(*
* @brief Function to read GATT characteristic
*
* @note buffer is allocated by the function. It is the responsibility of the caller to free the buffer.
*
* @param connection Active GATT connection
* @param uuid UUID of the GATT characteristic to read
* @param buffer contains the value to read. It is allocated by the function.
* @param buffer_len Length of the read data
*
* @return GATTLIB_SUCCESS on success or GATTLIB_* error code
*)
//int gattlib_read_char_by_uuid(gatt_connection_t* connection, uuid_t* uuid, void** buffer, size_t* buffer_len);
function gattlib_read_char_by_uuid(var connection: gatt_connection_t; const uuid: uuid_t;
const buffer: PPointer;
const buffer_len: size_t): integer; cdecl; external LIB_GATTLIB;
(***
* @brief Function to asynchronously read GATT characteristic
*
* @param connection Active GATT connection
* @param uuid UUID of the GATT characteristic to read
* @param gatt_read_cb is the callback to read when the GATT characteristic is available
*
* @return GATTLIB_SUCCESS on success or GATTLIB_* error code
*)
// int gattlib_read_char_by_uuid_async(gatt_connection_t* connection, uuid_t* uuid, gatt_read_cb_t gatt_read_cb);
function gattlib_read_char_by_uuid_async(var connection: gatt_connection_t; const uuid: uuid_t;
gatt_read_cb : gatt_read_cb_t): integer; cdecl; external LIB_GATTLIB;
(**
* @brief Free buffer allocated by the characteristic reading to store the value
*
* @param buffer Buffer to free
*)
//void gattlib_characteristic_free_value(void *ptr);
procedure gattlib_characteristic_free_value(const ptr: Pointer); cdecl; external LIB_GATTLIB;
(***
* @brief Function to write to the GATT characteristic UUID
*
* @param connection Active GATT connection
* @param uuid UUID of the GATT characteristic to read
* @param buffer contains the values to write to the GATT characteristic
* @param buffer_len is the length of the buffer to write
*
* @return GATTLIB_SUCCESS on success or GATTLIB_* error code
**)
//int gattlib_write_char_by_uuid(gatt_connection_t* connection, uuid_t* uuid, const void* buffer, size_t buffer_len);
function gattlib_write_char_by_uuid(var connection: gatt_connection_t; const uuid: uuid_t;
var buffer: Pointer; buffer_len: size_t): integer; cdecl; external LIB_GATTLIB;
(*
* @brief Function to write to the GATT characteristic handle
*
* @param connection Active GATT connection
* @param handle is the handle of the GATT characteristic
* @param buffer contains the values to write to the GATT characteristic
* @param buffer_len is the length of the buffer to write
*
* @return GATTLIB_SUCCESS on success or GATTLIB_* error code
*)
//int gattlib_write_char_by_handle(gatt_connection_t* connection, uint16_t handle, const void* buffer, size_t buffer_len);
function gattlib_write_char_by_handle(var connection: gatt_connection_t;
handle: uint16_t;
var buffer: Pointer; buffer_len: size_t): integer; cdecl; external LIB_GATTLIB;
(*
* @brief Function to write without response to the GATT characteristic UUID
*
* @param connection Active GATT connection
* @param uuid UUID of the GATT characteristic to read
* @param buffer contains the values to write to the GATT characteristic
* @param buffer_len is the length of the buffer to write
*
* @return GATTLIB_SUCCESS on success or GATTLIB_* error code
**)
//int gattlib_write_without_response_char_by_uuid(gatt_connection_t* connection, uuid_t* uuid, const void* buffer, size_t buffer_len);
function gattlib_write_without_response_char_by_uuid(var connection: gatt_connection_t; const uuid: uuid_t;
var buffer: Pointer; buffer_len: size_t): integer; cdecl; external LIB_GATTLIB;
(*
* @brief Create a stream to a GATT characteristic to write data in continue
*
* @note: The GATT characteristic must support 'Write-Without-Response'
*
* @param connection Active GATT connection
* @param uuid UUID of the GATT characteristic to write
* @param stream is the object that is attached to the GATT characteristic that is used to write data to
* @param mtu is the MTU of the GATT connection to optimise the stream writting
*
* @return GATTLIB_SUCCESS on success or GATTLIB_* error code
*)
//int gattlib_write_char_by_uuid_stream_open(gatt_connection_t* connection, uuid_t* uuid, gatt_stream_t **stream, uint16_t *mtu);
function gattlib_write_char_by_uuid_stream_open(var connection: gatt_connection_t; const uuid: uuid_t;
const stream: gatt_stream_t; // HOWTO ?: gatt_stream_t **stream
const mtu: uint16_t): integer; cdecl; external LIB_GATTLIB;
(**
* @brief Write data to the stream previously created with `gattlib_write_char_by_uuid_stream_open()`
*
* @param stream is the object that is attached to the GATT characteristic that is used to write data to
* @param buffer is the data to write to the stream
* @param buffer_len is the length of the buffer to write
*
* @return GATTLIB_SUCCESS on success or GATTLIB_* error code
*)
//int gattlib_write_char_stream_write(gatt_stream_t *stream, const void *buffer, size_t buffer_len);
function gattlib_write_char_stream_write(var stream: gatt_stream_t;
var buffer: Pointer; buffer_len: size_t): integer; cdecl; external LIB_GATTLIB;
(**
* @brief Close the stream previously created with `gattlib_write_char_by_uuid_stream_open()`
*
* @param stream is the object that is attached to the GATT characteristic that is used to write data to
*
* @return GATTLIB_SUCCESS on success or GATTLIB_* error code
*)
//int gattlib_write_char_stream_close(gatt_stream_t *stream);
function gattlib_write_char_stream_close(var stream: gatt_stream_t): integer; cdecl; external LIB_GATTLIB;
(**
* @brief Function to write without response to the GATT characteristic handle
*
* @param connection Active GATT connection
* @param handle is the handle of the GATT characteristic
* @param buffer contains the values to write to the GATT characteristic
* @param buffer_len is the length of the buffer to write
*
* @return GATTLIB_SUCCESS on success or GATTLIB_* error code
*)
//int gattlib_write_without_response_char_by_handle(gatt_connection_t* connection, uint16_t handle, const void* buffer, size_t buffer_len);
function gattlib_write_without_response_char_by_handle(var connection: gatt_connection_t;
handle: uint16_t;
var buffer: Pointer; buffer_len: size_t): Integer; cdecl; external LIB_GATTLIB;
(*
* @brief Enable notification on GATT characteristic represented by its UUID
*
* @param connection Active GATT connection
* @param uuid UUID of the characteristic that will trigger the notification
*
* @return GATTLIB_SUCCESS on success or GATTLIB_* error code
*)
//int gattlib_notification_start(gatt_connection_t* connection, const uuid_t* uuid);
function gattlib_notification_start(var connection: gatt_connection_t; const uuid: uuid_t): Integer; cdecl; external LIB_GATTLIB;
(*
* @brief Disable notification on GATT characteristic represented by its UUID
*
* @param connection Active GATT connection
* @param uuid UUID of the characteristic that will trigger the notification
*
* @return GATTLIB_SUCCESS on success or GATTLIB_* error code
*)
//int gattlib_notification_stop(gatt_connection_t* connection, const uuid_t* uuid);
function gattlib_notification_stop(var connection: gatt_connection_t; const uuid: uuid_t): Integer; cdecl; external LIB_GATTLIB;
(*
* @brief Enable indication on GATT characteristic represented by its UUID
*
* @param connection Active GATT connection
* @param uuid UUID of the characteristic that will trigger the indication
*
* @return GATTLIB_SUCCESS on success or GATTLIB_* error code
*)
//*int gattlib_indication_start(gatt_connection_t* connection, const uuid_t* uuid);
function gattlib_indication_start(var connection: gatt_connection_t; const uuid: uuid_t): Integer; cdecl; external LIB_GATTLIB;
(*
* @brief Disable indication on GATT characteristic represented by its UUID
*
* @param connection Active GATT connection
* @param uuid UUID of the characteristic that will trigger the indication
*
* @return GATTLIB_SUCCESS on success or GATTLIB_* error code
*)
//int gattlib_indication_stop(gatt_connection_t* connection, const uuid_t* uuid);
function gattlib_indication_stop(var connection: gatt_connection_t; const uuid: uuid_t): Integer; cdecl; external LIB_GATTLIB;
(*
* @brief Register a handle for the GATT notifications
*
* @param connection Active GATT connection
* @param notification_handler is the handler to call on notification
* @param user_data if the user specific data to pass to the handler
*
* @return GATTLIB_SUCCESS on success or GATTLIB_* error code
//*)
//void gattlib_register_notification(gatt_connection_t* connection, gattlib_event_handler_t notification_handler, void* user_data);
procedure gattlib_register_notification(var connection: gatt_connection_t;
notification_handler: gattlib_event_handler_t;
var user_data: Pointer); cdecl; external LIB_GATTLIB;
(*
* @brief Register a handle for the GATT indications
*
* @param connection Active GATT connection
* @param notification_handler is the handler to call on indications
* @param user_data if the user specific data to pass to the handler
*
* @return GATTLIB_SUCCESS on success or GATTLIB_* error code
*)
//void gattlib_register_indication(gatt_connection_t* connection, gattlib_event_handler_t indication_handler, void* user_data);
procedure gattlib_register_indication(var connection: gatt_connection_t;
indication_handler: gattlib_event_handler_t;
var user_data: Pointer); cdecl; external LIB_GATTLIB;
{$DEFINE thurlutte}
{.$UNDEF thurlutte}
{$IFDEF thurlutte} // Disable until https://github.com/labapart/gattlib/issues/75 is resolved
(**
* @brief Function to retrieve RSSI from a GATT connection
*
* @param connection Active GATT connection
* @param rssi is the Received Signal Strength Indicator of the remote device
*
* @return GATTLIB_SUCCESS on success or GATTLIB_* error code
*)
//int gattlib_get_rssi(gatt_connection_t *connection, int16_t *rssi);
function gattlib_get_rssi(var connection: gatt_connection_t;
const rssi : int16_t) : integer; cdecl; external LIB_GATTLIB;
{$endif}
(*
* @brief Function to retrieve RSSI from a MAC Address
*
* @note: This function is mainly used before a connection is established. Once the connection
* established, the function `gattlib_get_rssi()` should be preferred.
*
* @param adapter is the adapter the new device has been seen
* @param mac_address is the MAC address of the device to get the RSSI
* @param rssi is the Received Signal Strength Indicator of the remote device
*
* @return GATTLIB_SUCCESS on success or GATTLIB_* error code
*)
//int gattlib_get_rssi_from_mac(void *adapter, const char *mac_address, int16_t *rssi);
function gattlib_get_rssi_from_mac(var adapter: Pointer; const mac_address: PChar;
const rssi: int16_t): integer; cdecl; external LIB_GATTLIB;
(**
* @brief Function to retrieve Advertisement Data from a MAC Address
*
* @param connection Active GATT connection
* @param advertisement_data is an array of Service UUID and their respective data
* @param advertisement_data_count is the number of elements in the advertisement_data array
* @param manufacturer_id is the ID of the Manufacturer ID
* @param manufacturer_data is the data following Manufacturer ID
* @param manufacturer_data_size is the size of manufacturer_data
*
* @return GATTLIB_SUCCESS on success or GATTLIB_* error code
*)
//int gattlib_get_advertisement_data(gatt_connection_t *connection,
// gattlib_advertisement_data_t **advertisement_data, size_t *advertisement_data_count,
// uint16_t *manufacturer_id, uint8_t **manufacturer_data, size_t *manufacturer_data_size);
function gattlib_get_advertisement_data(var connection: gatt_connection_t;
const advertisement_data: array of gattlib_advertisement_data_t;
const advertisement_data_count: size_t;
const manufacturer_id : uint8_t;
const manufacturer_data: array of uint8_t;
const manufacturer_data_size: size_t) : integer; cdecl; external LIB_GATTLIB;
(*
* @brief Function to retrieve Advertisement Data from a MAC Address
*
* @param adapter is the adapter the new device has been seen
* @param mac_address is the MAC address of the device to get the RSSI
* @param advertisement_data is an array of Service UUID and their respective data
* @param advertisement_data_count is the number of elements in the advertisement_data array
* @param manufacturer_id is the ID of the Manufacturer ID
* @param manufacturer_data is the data following Manufacturer ID
* @param manufacturer_data_size is the size of manufacturer_data
*
* @return GATTLIB_SUCCESS on success or GATTLIB_* error code
**)
//int gattlib_get_advertisement_data_from_mac(void *adapter, const char *mac_address,
// gattlib_advertisement_data_t **advertisement_data, size_t *advertisement_data_count,
// uint16_t *manufacturer_id, uint8_t **manufacturer_data, size_t *manufacturer_data_size);
function gattlib_get_advertisement_data(var adapter: Pointer; const mac_address: PChar;
const advertisement_data: array of gattlib_advertisement_data_t;
const advertisement_data_count: size_t;
const manufacturer_id : uint8_t;
const manufacturer_data: array of uint8_t;
const manufacturer_data_size: size_t) : integer; cdecl; external LIB_GATTLIB;
(*
* @brief Convert a UUID into a string
*
* @param uuid is the UUID to convert
* @param str is the buffer that will contain the string
* @param size is the size of the buffer
*
* @return GATTLIB_SUCCESS on success or GATTLIB_* error code
*)
//int gattlib_uuid_to_string(const uuid_t *uuid, char *str, size_t size);
function gattlib_uuid_to_string(const uuid: uuid_t; const str: PChar; size: size_t): integer; cdecl; external LIB_GATTLIB;
(**
* @brief Convert a string representing a UUID into a UUID structure
*
* @param str is the buffer containing the string
* @param size is the size of the buffer
* @param uuid is the UUID structure that would receive the UUID
*
* @return GATTLIB_SUCCESS on success or GATTLIB_* error code
*)
//int gattlib_string_to_uuid(const char *str, size_t size, uuid_t *uuid);
function gattlib_string_to_uuid(const str: PChar; size: size_t; var uuid: uuid_t): integer; cdecl; external LIB_GATTLIB;
(**
* @brief Compare two UUIDs
*
* @param uuid1 is the one of the UUID to compare with
* @param uuid2 is the other UUID to compare with
*
* @return GATTLIB_SUCCESS on success or GATTLIB_* error code
*)
// int gattlib_uuid_cmp(const uuid_t *uuid1, const uuid_t *uuid2);
function gattlib_uuid_cmp(const uuid1: uuid_t; const uuid2: uuid_t): integer; cdecl; external LIB_GATTLIB;
// void gattlib_log(int level, const char *format, ...);
procedure gattlib_log(level: integer; const format: PChar; const Args: array of const); cdecl; external LIB_GATTLIB;
//void uuid_to_bt_uuid(uuid_t* uuid, bt_uuid_t* bt_uuid);
procedure uuid_to_bt_uuid(const uuid: uuid_t; var bt_uuid: bt_uuid_t); cdecl; external LIB_GATTLIB;
//void bt_uuid_to_uuid(bt_uuid_t* bt_uuid, uuid_t* uuid);
procedure bt_uuid_to_uuid(const bt_uuid: bt_uuid_t; var uuid: uuid_t);
//int get_uuid_from_handle(gatt_connection_t* connection, uint16_t handle, uuid_t* uuid);
function get_uuid_from_handle(var connection: gatt_connection_t; handle: uint16_t; const uuid: uuid_t): integer; cdecl; external LIB_GATTLIB;
//int get_handle_from_uuid(gatt_connection_t* connection, const uuid_t* uuid, uint16_t* handle);
function get_handle_from_uuid(var connection: gatt_connection_t; const uuid: uuid_t; var handle: uint16_t): integer; cdecl; external LIB_GATTLIB;
//******************************************************************************
// Macros C = deviennent des fonctions
// Ce sont des entiers (comparaisons bit à bit avec l'opérateur & (et non &&))
function GATTLIB_CONNECTION_OPTIONS_LEGACY_PSM(const value: integer): integer; inline;
function GATTLIB_CONNECTION_OPTIONS_LEGACY_MTU(const value: integer): integer; inline;
// Ce sont ici des booléens du fait de l'opérateur &&
function GATTLIB_CONNECTION_OPTIONS_LEGACY_GET_PSM(const options: integer): boolean; inline; // #define (options) (((options) >> 11) && 0x3FF)
function GATTLIB_CONNECTION_OPTIONS_LEGACY_GET_MTU(const options: integer): boolean; inline; //
// log
procedure GATTLIB_LOG(const level: integer; const args: array of const); // #define GATTLIB_LOG(level, args...) if (level <= GATTLIB_LOG_LEVEL) { gattlib_log(level, args); }
//******************************************************************************
function LoadLibGattlib(out LH: TLibHandle): boolean;
procedure ReleaseGattLib(const H: TLibHandle);
// fonction de test
function FirstFunction(const IntParam: Longint): PChar; external LIB_GATTLIB;
implementation
// Traduction des macros C
function GATTLIB_CONNECTION_OPTIONS_LEGACY_PSM(const value: integer): integer;
begin
Result := (value AND $3FF) shl 11; //< We encode PSM on 10 bits (up to 1023) // GATTLIB_CONNECTION_OPTIONS_LEGACY_PSM(value)
end;
function GATTLIB_CONNECTION_OPTIONS_LEGACY_MTU(const value: integer): integer;
begin
Result := (value AND $3FF) shl 21; //< We encode MTU on 10 bits (up to 1023) // #define GATTLIB_CONNECTION_OPTIONS_LEGACY_MTU(value) (((value) & 0x3FF) << 21) //< We encode MTU on 10 bits (up to 1023)
end;
// Ce sont des booléens du fait de l'opérateur &&
function GATTLIB_CONNECTION_OPTIONS_LEGACY_GET_PSM(const options: integer): boolean; // #define (options) (((options) >> 11) && 0x3FF)
var
EWE: Integer;
begin
EWE := options shr 11;
Result := (EWE = $3FF); // #define GATTLIB_CONNECTION_OPTIONS_LEGACY_GET_PSM(options) (((options) >> 11) && 0x3FF)
end;
function GATTLIB_CONNECTION_OPTIONS_LEGACY_GET_MTU(const options: integer): boolean; //
var
EWE: Integer;
begin
EWE := options shr 21;
Result := (EWE = $3FF); // #define GATTLIB_CONNECTION_OPTIONS_LEGACY_GET_MTU(options) (((options) >> 21) && 0x3FF)
end;
procedure GATTLIB_LOG(const level: integer; const args: array of const); // #define GATTLIB_LOG(level, args...) if (level <= GATTLIB_LOG_LEVEL) { gattlib_log(level, args); }
begin
;;; //if (level <= GATTLIB_LOG_LEVEL) { gattlib_log(level, args); }
end;
//********* FIN DU WRAPPER ***************************************************
//******************************************************************************
function LoadLibGattlib(out LH: TLibHandle): boolean;
var
P: Pointer;
begin
Result := false;
P := nil;
LH := LoadLibrary(LIB_GATTLIB);
if (LH <> 0) then
begin
P := GetProcAddress(LH, 'FirstFunction');
Result := (P <> nil);
end;
end;
procedure ReleaseGattLib(const H: TLibHandle);
begin
FreeLibrary(H);
end;
end. |
Partager