IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Lazarus Pascal Discussion :

Utilisation de la GATTLib avec Lazarus


Sujet :

Lazarus Pascal

  1. #1
    Membre confirmé

    Homme Profil pro
    Développeur informatique
    Inscrit en
    Novembre 2013
    Messages
    343
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Gironde (Aquitaine)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : Administration - Collectivité locale

    Informations forums :
    Inscription : Novembre 2013
    Messages : 343
    Points : 536
    Points
    536
    Billets dans le blog
    2
    Par défaut Utilisation de la GATTLib avec Lazarus
    Bjr à vous,

    J'ai téléchargé la librairie GATTLIB (Bluetooth LE sous protocole GATT) et commencé à convertir les en-têtes (avec h2pas).
    Voici la traduction de gattlib.h après quelques retouches.

    Je recherche des exemples d'utilisation de Gattlib.

    Traduction:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    {$mode objfpc}
    unit gattlib;
    interface
     
     
     
    {
      Automatically converted by H2Pas 1.0.0 from .\gattlib.h
      The following command line parameters were used:
        -p
        -P
        -v
        .\gattlib.h
    }
     
        { Pointers to basic pascal types, inserted by h2pas conversion program.}
        Type
          PLongint  = ^Longint;
          PSmallInt = ^SmallInt;
          PByte     = ^Byte;
          PWord     = ^Word;
          PDWord    = ^DWord;
          PDouble   = ^Double;
     
        Type
        Pchar  = ^char;
        Pgatt_connection_t  = ^gatt_connection_t;
        Pgatt_read_cb_t  = ^gatt_read_cb_t;
        Pgatt_stream_t  = ^gatt_stream_t;
        Pgattlib_advertisement_data_t  = ^gattlib_advertisement_data_t;
        Pgattlib_characteristic_t  = ^gattlib_characteristic_t;
        Pgattlib_descriptor_t  = ^gattlib_descriptor_t;
        Pgattlib_primary_service_t  = ^gattlib_primary_service_t;
        Puint8_t  = ^uint8_t;
        Puuid_t  = ^uuid_t;
    {$IFDEF FPC}
    {$PACKRECORDS C}
    {$ENDIF}
     
     
      {
       * SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0-or-later
       *
       * Copyright (c) 2016-2021, Olivier Martin <olivier@labapart.org>
        }
    {$ifndef __GATTLIB_H__}
    {$define __GATTLIB_H__}  
    { C++ extern C conditionnal removed }
    {$include <stdint.h>}
    {$include <bluetooth/bluetooth.h>}
    {$include <bluetooth/sdp.h>}
    {$include <bluetooth/sdp_lib.h>}
    {$ifndef BDADDR_BREDR}
      { 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;    
    {$endif}
    {$if BLUEZ_VERSION_MAJOR == 5}
     
      const
        ATT_MAX_MTU = ATT_MAX_VALUE_LEN;    
    {$endif}
      {*
       * @name Gattlib errors
        }
      {@ }
     
      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
        }
      {@ }
        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;    
      {@ }
      {*
       * Helper function to create UUID16 from a 16bit integer
        }
     
     
    (* error 
    #define CREATE_UUID16(value16) { .type=SDP_UUID16, .value.uuid16=(value16) }
    in define line 64 *)
        {*
         * @name Options for gattlib_connect()
         *
         * @note Options with the prefix `GATTLIB_CONNECTION_OPTIONS_LEGACY_`
         *       is for Bluez prior to v5.42 (before Bluez) support
          }
        {@ }
          GATTLIB_CONNECTION_OPTIONS_LEGACY_BDADDR_LE_PUBLIC   = 1 shl 0;      
          GATTLIB_CONNECTION_OPTIONS_LEGACY_BDADDR_LE_RANDOM   = 1 shl 1;      
          GATTLIB_CONNECTION_OPTIONS_LEGACY_BT_SEC_LOW         = 1 shl 2;      
          GATTLIB_CONNECTION_OPTIONS_LEGACY_BT_SEC_MEDIUM 	   = 1 shl 3;      
          GATTLIB_CONNECTION_OPTIONS_LEGACY_BT_SEC_HIGH        = 1 shl 4;      
        { was #define dname(params) para_def_expr }
        { argument types are unknown }
        { return type might be wrong }   
     
        function GATTLIB_CONNECTION_OPTIONS_LEGACY_PSM(value : longint) : longint;    {< We encode PSM on 10 bits (up to 1023) }
     
      { was #define dname(params) para_def_expr }
      { argument types are unknown }
      { return type might be wrong }   
      function GATTLIB_CONNECTION_OPTIONS_LEGACY_MTU(value : longint) : longint;  {< We encode MTU on 10 bits (up to 1023) }
     
      { was #define dname(params) para_def_expr }
      { argument types are unknown }
      { return type might be wrong }   
      function GATTLIB_CONNECTION_OPTIONS_LEGACY_GET_PSM(options : longint) : longint;  
     
      { was #define dname(params) para_def_expr }
      { argument types are unknown }
      { return type might be wrong }   
      function GATTLIB_CONNECTION_OPTIONS_LEGACY_GET_MTU(options : longint) : longint;  
    (*
    #define GATTLIB_CONNECTION_OPTIONS_LEGACY_DEFAULT \
    		GATTLIB_CONNECTION_OPTIONS_LEGACY_BDADDR_LE_PUBLIC | \
    		GATTLIB_CONNECTION_OPTIONS_LEGACY_BDADDR_LE_RANDOM | \
    		GATTLIB_CONNECTION_OPTIONS_LEGACY_BT_SEC_LOW
     
    //*)
    const GATTLIB_CONNECTION_OPTIONS_LEGACY_DEFAULT = GATTLIB_CONNECTION_OPTIONS_LEGACY_BDADDR_LE_PUBLIC OR 
                                                      GATTLIB_CONNECTION_OPTIONS_LEGACY_BDADDR_LE_RANDOM OR
    												  GATTLIB_CONNECTION_OPTIONS_LEGACY_BT_SEC_LOW;
     
        {@ }
        {*
         * @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)")
     
    function GATTLIB_LOG(const level: integer; const args: array of const);
    begin
     if (level <= GATTLIB_LOG_LEVEL) then gattlib_log(level, args);
    end;
     
        type
          _gatt_stream_t = gatt_stream_t;
        {*
         * Structure to represent a GATT Service and its data in the BLE advertisement packet
          }
        {*< UUID of the GATT Service  }
        {*< Data attached to the GATT Service  }
        {*< Length of data attached to the GATT Service  }
     
          Pgattlib_advertisement_data_t = ^gattlib_advertisement_data_t;
          gattlib_advertisement_data_t = record
              uuid : uuid_t;
              data : Puint8_t;
              data_length : size_t;
            end;
    (* Const before type ignored *)
    (* Const before type ignored *)
     
          gattlib_event_handler_t = procedure (var uuid:uuid_t; var data:uint8_t; data_length:size_t; user_data:pointer);cdecl;
        {*
         * @brief Handler called on disconnection
         *
         * @param connection Connection that is disconnecting
         * @param user_data  Data defined when calling `gattlib_register_on_disconnect()`
          }
     
          gattlib_disconnection_handler_t = procedure (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()`
          }
    (* Const before type ignored *)
    (* Const before type ignored *)
     
          gattlib_discovered_device_t = procedure (adapter:pointer; addr:Pchar; name:Pchar; 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()`
          }
    (* Const before type ignored *)
    (* Const before type ignored *)
     
          gattlib_discovered_device_with_data_t = procedure (adapter:pointer; addr:Pchar; name:Pchar; var advertisement_data:gattlib_advertisement_data_t; advertisement_data_count:size_t; 
                        manufacturer_id:uint16_t; var manufacturer_data:uint8_t; manufacturer_data_size:size_t; user_data:pointer);cdecl;
        {*
         * @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()`
          }
     
          gatt_connect_cb_t = procedure (var connection:gatt_connection_t; user_data:pointer);cdecl;
        {*
         * @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
         *
          }
    (* Const before type ignored *)
     
          Pgatt_read_cb_t = ^gatt_read_cb_t;
          gatt_read_cb_t = function (buffer:pointer; buffer_len:size_t):pointer;cdecl;
        {*
         * @brief Constant defining Eddystone common data UID in Advertisement data
          }
    (* Const before type ignored *)
     
          var
            gattlib_eddystone_common_data_uuid : uuid_t;cvar;external;
        {*
         * @brief List of prefix for Eddystone URL Scheme
          }
    (* Const before type ignored *)
            gattlib_eddystone_url_scheme_prefix : ^Pchar;cvar;external;
        {*
         * @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
          }
    (* Const before type ignored *)
          gattlib_adapter_open : function(adapter_name:Pchar; adapter:Ppointer):longint;
        {*
         * @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
          }
          gattlib_adapter_scan_enable : function(adapter:pointer; discovered_device_cb:gattlib_discovered_device_t; timeout:size_t; user_data:pointer):longint;
        {*
         * @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
          }
          gattlib_adapter_scan_enable_with_filter : function(adapter:pointer; var uuid_list:Puuid_t; rssi_threshold:int16_t; enabled_filters:uint32_t; discovered_device_cb:gattlib_discovered_device_t; 
            timeout:size_t; user_data:pointer):longint;
        {*
         * @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
          }
          gattlib_adapter_scan_enable_with_filter_non_blocking : function(adapter:pointer; var uuid_list:Puuid_t; rssi_threshold:int16_t; enabled_filters:uint32_t; discovered_device_cb:gattlib_discovered_device_t; 
            timeout:size_t; user_data:pointer):longint;
        {*
         * @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
          }
          gattlib_adapter_scan_eddystone : function(adapter:pointer; rssi_threshold:int16_t; eddystone_types:uint32_t; discovered_device_cb:gattlib_discovered_device_with_data_t; timeout:size_t; 
            user_data:pointer):longint;
        {*
         * @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
          }
          gattlib_adapter_scan_disable : function(adapter:pointer):longint;
        {*
         * @brief Close Bluetooth adapter context
         *
         * @param adapter is the context of the newly opened adapter
         *
         * @return GATTLIB_SUCCESS on success or GATTLIB_* error code
          }
          gattlib_adapter_close : function(adapter:pointer):longint;
        {*
         * @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_*`
          }
    (* Const before type ignored *)
          gattlib_connect : function(adapter:pointer; dst:Pchar; options:dword):Pgatt_connection_t;
        {*
         * @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
          }
    (* Const before type ignored *)
          gattlib_connect_async : function(adapter:pointer; dst:Pchar; options:dword; connect_cb:gatt_connect_cb_t; user_data:pointer):Pgatt_connection_t;
        {*
         * @brief Function to disconnect the GATT connection
         *
         * @param connection Active GATT connection
         *
         * @return GATTLIB_SUCCESS on success or GATTLIB_* error code
          }
          gattlib_disconnect : function(var connection:gatt_connection_t):longint;
        {*
         * @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
          }
          gattlib_register_on_disconnect : procedure(var connection:gatt_connection_t; handler:gattlib_disconnection_handler_t; user_data:pointer);
        {*
         * Structure to represent GATT Primary Service
          }
        {*< First attribute handle of the GATT Primary Service  }
        {*< Last attibute handle of the GATT Primary Service  }
        {*< UUID of the Primary Service  }
     
        type
          Pgattlib_primary_service_t = ^gattlib_primary_service_t;
          gattlib_primary_service_t = record
              attr_handle_start : uint16_t;
              attr_handle_end : uint16_t;
              uuid : uuid_t;
            end;
        {*
         * Structure to represent GATT Characteristic
          }
        {*< Handle of the GATT characteristic  }
        {*< Property of the GATT characteristic  }
        {*< Handle for the value of the GATT characteristic  }
        {*< UUID of the GATT characteristic  }
     
          Pgattlib_characteristic_t = ^gattlib_characteristic_t;
          gattlib_characteristic_t = record
              handle : uint16_t;
              properties : uint8_t;
              value_handle : uint16_t;
              uuid : uuid_t;
            end;
        {*
         * Structure to represent GATT Descriptor
          }
        {*< Handle of the GATT Descriptor  }
        {*< UUID16 of the GATT Descriptor  }
        {*< UUID of the GATT Descriptor  }
     
          Pgattlib_descriptor_t = ^gattlib_descriptor_t;
          gattlib_descriptor_t = record
              handle : uint16_t;
              uuid16 : uint16_t;
              uuid : uuid_t;
            end;
        {*
         * @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
          }
     
        var
          gattlib_discover_primary : function(var connection:gatt_connection_t; var services:Pgattlib_primary_service_t; var services_count:longint):longint;
        {*
         * @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
          }
          gattlib_discover_char_range : function(var connection:gatt_connection_t; start:longint; end:longint; characteristics:PPgattlib_characteristic_t; var characteristics_count:longint):longint;
        {*
         * @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
          }
          gattlib_discover_char : function(var connection:gatt_connection_t; characteristics:PPgattlib_characteristic_t; var characteristics_count:longint):longint;
        {*
         * @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
          }
          gattlib_discover_desc_range : function(var connection:gatt_connection_t; start:longint; end:longint; var descriptors:Pgattlib_descriptor_t; var descriptors_count:longint):longint;
        {*
         * @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
          }
          gattlib_discover_desc : function(var connection:gatt_connection_t; var descriptors:Pgattlib_descriptor_t; var descriptors_count:longint):longint;
        {*
         * @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
          }
          gattlib_read_char_by_uuid : function(var connection:gatt_connection_t; var uuid:uuid_t; buffer:Ppointer; var buffer_len:size_t):longint;
        {*
         * @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
          }
          gattlib_read_char_by_uuid_async : function(var connection:gatt_connection_t; var uuid:uuid_t; gatt_read_cb:gatt_read_cb_t):longint;
        {*
         * @brief Free buffer allocated by the characteristic reading to store the value
         *
         * @param buffer Buffer to free
          }
          gattlib_characteristic_free_value : procedure(ptr:pointer);
        {*
         * @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
          }
    (* Const before type ignored *)
          gattlib_write_char_by_uuid : function(var connection:gatt_connection_t; var uuid:uuid_t; buffer:pointer; buffer_len:size_t):longint;
        {*
         * @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
          }
    (* Const before type ignored *)
          gattlib_write_char_by_handle : function(var connection:gatt_connection_t; handle:uint16_t; buffer:pointer; buffer_len:size_t):longint;
        {*
         * @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
          }
    (* Const before type ignored *)
          gattlib_write_without_response_char_by_uuid : function(var connection:gatt_connection_t; var uuid:uuid_t; buffer:pointer; buffer_len:size_t):longint;
        {*
         * @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
          }
          gattlib_write_char_by_uuid_stream_open : function(var connection:gatt_connection_t; var uuid:uuid_t; var stream:Pgatt_stream_t; var mtu:uint16_t):longint;
        {*
         * @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
          }
    (* Const before type ignored *)
          gattlib_write_char_stream_write : function(var stream:gatt_stream_t; buffer:pointer; buffer_len:size_t):longint;
        {*
         * @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
          }
          gattlib_write_char_stream_close : function(var stream:gatt_stream_t):longint;
        {*
         * @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
          }
    (* Const before type ignored *)
          gattlib_write_without_response_char_by_handle : function(var connection:gatt_connection_t; handle:uint16_t; buffer:pointer; buffer_len:size_t):longint;
        {
         * @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
          }
    (* Const before type ignored *)
          gattlib_notification_start : function(var connection:gatt_connection_t; var uuid:uuid_t):longint;
        {
         * @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
          }
    (* Const before type ignored *)
          gattlib_notification_stop : function(var connection:gatt_connection_t; var uuid:uuid_t):longint;
        {
         * @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
          }
    (* Const before type ignored *)
          gattlib_indication_start : function(var connection:gatt_connection_t; var uuid:uuid_t):longint;
        {
         * @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
          }
    (* Const before type ignored *)
          gattlib_indication_stop : function(var connection:gatt_connection_t; var uuid:uuid_t):longint;
        {
         * @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
          }
          gattlib_register_notification : procedure(var connection:gatt_connection_t; notification_handler:gattlib_event_handler_t; user_data:pointer);
        {
         * @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
          }
          gattlib_register_indication : procedure(var connection:gatt_connection_t; indication_handler:gattlib_event_handler_t; user_data:pointer);
    {$if 0 // 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
          }
     
        var
          gattlib_get_rssi : function(var connection:gatt_connection_t; var rssi:int16_t):longint;
    {$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
          }
    (* Const before type ignored *)
     
        var
          gattlib_get_rssi_from_mac : function(adapter:pointer; mac_address:Pchar; var rssi:int16_t):longint;
        {*
         * @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
          }
          gattlib_get_advertisement_data : function(var connection:gatt_connection_t; var advertisement_data:Pgattlib_advertisement_data_t; var advertisement_data_count:size_t; var manufacturer_id:uint16_t; var manufacturer_data:Puint8_t; 
            var manufacturer_data_size:size_t):longint;
        {*
         * @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
          }
    (* Const before type ignored *)
          gattlib_get_advertisement_data_from_mac : function(adapter:pointer; mac_address:Pchar; var advertisement_data:Pgattlib_advertisement_data_t; var advertisement_data_count:size_t; var manufacturer_id:uint16_t; 
            var manufacturer_data:Puint8_t; var manufacturer_data_size:size_t):longint;
        {*
         * @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
          }
    (* Const before type ignored *)
          gattlib_uuid_to_string : function(var uuid:uuid_t; str:Pchar; size:size_t):longint;
        {*
         * @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
          }
    (* Const before type ignored *)
          gattlib_string_to_uuid : function(str:Pchar; size:size_t; var uuid:uuid_t):longint;
        {*
         * @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
          }
    (* Const before type ignored *)
    (* Const before type ignored *)
          gattlib_uuid_cmp : function(var uuid1:uuid_t; var uuid2:uuid_t):longint;
    (* Const before type ignored *)
          gattlib_log : procedure(level:longint; format:Pchar; args:array of const);
          gattlib_log : procedure(level:longint; format:Pchar);
    { C++ end of extern C conditionnal removed }
    {$endif}
     
    implementation
     
        { was #define dname(params) para_def_expr }
        { argument types are unknown }
        { return type might be wrong }   
        function GATTLIB_CONNECTION_OPTIONS_LEGACY_PSM(value : longint) : longint;
        begin
          GATTLIB_CONNECTION_OPTIONS_LEGACY_PSM:=(value(@($3FF))) shl 11;
        end;
     
      { was #define dname(params) para_def_expr }
      { argument types are unknown }
      { return type might be wrong }   
      function GATTLIB_CONNECTION_OPTIONS_LEGACY_MTU(value : longint) : longint;
      begin
        GATTLIB_CONNECTION_OPTIONS_LEGACY_MTU:=(value(@($3FF))) shl 21;
      end;
     
      { was #define dname(params) para_def_expr }
      { argument types are unknown }
      { return type might be wrong }   
      function GATTLIB_CONNECTION_OPTIONS_LEGACY_GET_PSM(options : longint) : longint;
      begin
        GATTLIB_CONNECTION_OPTIONS_LEGACY_GET_PSM:=(options shr 11) and (@($3FF));
      end;
     
      { was #define dname(params) para_def_expr }
      { argument types are unknown }
      { return type might be wrong }   
      function GATTLIB_CONNECTION_OPTIONS_LEGACY_GET_MTU(options : longint) : longint;
      begin
        GATTLIB_CONNECTION_OPTIONS_LEGACY_GET_MTU:=(options shr 21) and (@($3FF));
      end;
     
     
      uses
        SysUtils, dynlibs;
     
      var
        hlib : tlibhandle;
     
     
      procedure Freegattlib;
        begin
          FreeLibrary(hlib);
          gattlib_adapter_open:=nil;
          gattlib_adapter_scan_enable:=nil;
          gattlib_adapter_scan_enable_with_filter:=nil;
          gattlib_adapter_scan_enable_with_filter_non_blocking:=nil;
          gattlib_adapter_scan_eddystone:=nil;
          gattlib_adapter_scan_disable:=nil;
          gattlib_adapter_close:=nil;
          gattlib_connect:=nil;
          gattlib_connect_async:=nil;
          gattlib_disconnect:=nil;
          gattlib_register_on_disconnect:=nil;
          gattlib_discover_primary:=nil;
          gattlib_discover_char_range:=nil;
          gattlib_discover_char:=nil;
          gattlib_discover_desc_range:=nil;
          gattlib_discover_desc:=nil;
          gattlib_read_char_by_uuid:=nil;
          gattlib_read_char_by_uuid_async:=nil;
          gattlib_characteristic_free_value:=nil;
          gattlib_write_char_by_uuid:=nil;
          gattlib_write_char_by_handle:=nil;
          gattlib_write_without_response_char_by_uuid:=nil;
          gattlib_write_char_by_uuid_stream_open:=nil;
          gattlib_write_char_stream_write:=nil;
          gattlib_write_char_stream_close:=nil;
          gattlib_write_without_response_char_by_handle:=nil;
          gattlib_notification_start:=nil;
          gattlib_notification_stop:=nil;
          gattlib_indication_start:=nil;
          gattlib_indication_stop:=nil;
          gattlib_register_notification:=nil;
          gattlib_register_indication:=nil;
          gattlib_get_rssi:=nil;
          gattlib_get_rssi_from_mac:=nil;
          gattlib_get_advertisement_data:=nil;
          gattlib_get_advertisement_data_from_mac:=nil;
          gattlib_uuid_to_string:=nil;
          gattlib_string_to_uuid:=nil;
          gattlib_uuid_cmp:=nil;
          gattlib_log:=nil;
          gattlib_log:=nil;
        end;
     
     
      procedure Loadgattlib(lib : pchar);
        begin
          Freegattlib;
          hlib:=LoadLibrary(lib);
          if hlib=0 then
            raise Exception.Create(format('Could not load library: %s',[lib]));
     
          pointer(gattlib_adapter_open):=GetProcAddress(hlib,'gattlib_adapter_open');
          pointer(gattlib_adapter_scan_enable):=GetProcAddress(hlib,'gattlib_adapter_scan_enable');
          pointer(gattlib_adapter_scan_enable_with_filter):=GetProcAddress(hlib,'gattlib_adapter_scan_enable_with_filter');
          pointer(gattlib_adapter_scan_enable_with_filter_non_blocking):=GetProcAddress(hlib,'gattlib_adapter_scan_enable_with_filter_non_blocking');
          pointer(gattlib_adapter_scan_eddystone):=GetProcAddress(hlib,'gattlib_adapter_scan_eddystone');
          pointer(gattlib_adapter_scan_disable):=GetProcAddress(hlib,'gattlib_adapter_scan_disable');
          pointer(gattlib_adapter_close):=GetProcAddress(hlib,'gattlib_adapter_close');
          pointer(gattlib_connect):=GetProcAddress(hlib,'gattlib_connect');
          pointer(gattlib_connect_async):=GetProcAddress(hlib,'gattlib_connect_async');
          pointer(gattlib_disconnect):=GetProcAddress(hlib,'gattlib_disconnect');
          pointer(gattlib_register_on_disconnect):=GetProcAddress(hlib,'gattlib_register_on_disconnect');
          pointer(gattlib_discover_primary):=GetProcAddress(hlib,'gattlib_discover_primary');
          pointer(gattlib_discover_char_range):=GetProcAddress(hlib,'gattlib_discover_char_range');
          pointer(gattlib_discover_char):=GetProcAddress(hlib,'gattlib_discover_char');
          pointer(gattlib_discover_desc_range):=GetProcAddress(hlib,'gattlib_discover_desc_range');
          pointer(gattlib_discover_desc):=GetProcAddress(hlib,'gattlib_discover_desc');
          pointer(gattlib_read_char_by_uuid):=GetProcAddress(hlib,'gattlib_read_char_by_uuid');
          pointer(gattlib_read_char_by_uuid_async):=GetProcAddress(hlib,'gattlib_read_char_by_uuid_async');
          pointer(gattlib_characteristic_free_value):=GetProcAddress(hlib,'gattlib_characteristic_free_value');
          pointer(gattlib_write_char_by_uuid):=GetProcAddress(hlib,'gattlib_write_char_by_uuid');
          pointer(gattlib_write_char_by_handle):=GetProcAddress(hlib,'gattlib_write_char_by_handle');
          pointer(gattlib_write_without_response_char_by_uuid):=GetProcAddress(hlib,'gattlib_write_without_response_char_by_uuid');
          pointer(gattlib_write_char_by_uuid_stream_open):=GetProcAddress(hlib,'gattlib_write_char_by_uuid_stream_open');
          pointer(gattlib_write_char_stream_write):=GetProcAddress(hlib,'gattlib_write_char_stream_write');
          pointer(gattlib_write_char_stream_close):=GetProcAddress(hlib,'gattlib_write_char_stream_close');
          pointer(gattlib_write_without_response_char_by_handle):=GetProcAddress(hlib,'gattlib_write_without_response_char_by_handle');
          pointer(gattlib_notification_start):=GetProcAddress(hlib,'gattlib_notification_start');
          pointer(gattlib_notification_stop):=GetProcAddress(hlib,'gattlib_notification_stop');
          pointer(gattlib_indication_start):=GetProcAddress(hlib,'gattlib_indication_start');
          pointer(gattlib_indication_stop):=GetProcAddress(hlib,'gattlib_indication_stop');
          pointer(gattlib_register_notification):=GetProcAddress(hlib,'gattlib_register_notification');
          pointer(gattlib_register_indication):=GetProcAddress(hlib,'gattlib_register_indication');
          pointer(gattlib_get_rssi):=GetProcAddress(hlib,'gattlib_get_rssi');
          pointer(gattlib_get_rssi_from_mac):=GetProcAddress(hlib,'gattlib_get_rssi_from_mac');
          pointer(gattlib_get_advertisement_data):=GetProcAddress(hlib,'gattlib_get_advertisement_data');
          pointer(gattlib_get_advertisement_data_from_mac):=GetProcAddress(hlib,'gattlib_get_advertisement_data_from_mac');
          pointer(gattlib_uuid_to_string):=GetProcAddress(hlib,'gattlib_uuid_to_string');
          pointer(gattlib_string_to_uuid):=GetProcAddress(hlib,'gattlib_string_to_uuid');
          pointer(gattlib_uuid_cmp):=GetProcAddress(hlib,'gattlib_uuid_cmp');
          pointer(gattlib_log):=GetProcAddress(hlib,'gattlib_log');
          pointer(gattlib_log):=GetProcAddress(hlib,'gattlib_log');
        end;
     
     
    initialization
      Loadgattlib('gattlib');
    finalization
      Freegattlib;
     
    end.
    Header original:
    Code c : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    /*
     * SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0-or-later
     *
     * Copyright (c) 2016-2021, Olivier Martin <olivier@labapart.org>
     */
     
    #ifndef __GATTLIB_H__
    #define __GATTLIB_H__
     
    #ifdef __cplusplus
    extern "C" {
    #endif
     
    #include <stdint.h>
     
    #include <bluetooth/bluetooth.h>
    #include <bluetooth/sdp.h>
    #include <bluetooth/sdp_lib.h>
     
    #ifndef BDADDR_BREDR
      /* 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 */
      #define BDADDR_BREDR           0x00
      #define BDADDR_LE_PUBLIC       0x01
      #define BDADDR_LE_RANDOM       0x02
    #endif
     
    #if BLUEZ_VERSION_MAJOR == 5
      #define ATT_MAX_MTU ATT_MAX_VALUE_LEN
    #endif
     
    /**
     * @name Gattlib errors
     */
    //@{
    #define GATTLIB_SUCCESS             0
    #define GATTLIB_INVALID_PARAMETER   1
    #define GATTLIB_NOT_FOUND           2
    #define GATTLIB_OUT_OF_MEMORY       3
    #define GATTLIB_NOT_SUPPORTED       4
    #define GATTLIB_DEVICE_ERROR        5
    #define GATTLIB_ERROR_DBUS          6
    #define GATTLIB_ERROR_BLUEZ         7
    #define GATTLIB_ERROR_INTERNAL      8
    //@}
     
    /**
     * @name GATT Characteristic Properties Bitfield values
     */
    //@{
    #define GATTLIB_CHARACTERISTIC_BROADCAST			0x01
    #define GATTLIB_CHARACTERISTIC_READ					0x02
    #define GATTLIB_CHARACTERISTIC_WRITE_WITHOUT_RESP	0x04
    #define GATTLIB_CHARACTERISTIC_WRITE				0x08
    #define GATTLIB_CHARACTERISTIC_NOTIFY				0x10
    #define GATTLIB_CHARACTERISTIC_INDICATE				0x20
    //@}
     
    /**
     * Helper function to create UUID16 from a 16bit integer
     */
    #define CREATE_UUID16(value16) { .type=SDP_UUID16, .value.uuid16=(value16) }
     
    /**
     * @name Options for gattlib_connect()
     *
     * @note Options with the prefix `GATTLIB_CONNECTION_OPTIONS_LEGACY_`
     *       is for Bluez prior to v5.42 (before Bluez) support
     */
    //@{
    #define GATTLIB_CONNECTION_OPTIONS_LEGACY_BDADDR_LE_PUBLIC  (1 << 0)
    #define GATTLIB_CONNECTION_OPTIONS_LEGACY_BDADDR_LE_RANDOM  (1 << 1)
    #define GATTLIB_CONNECTION_OPTIONS_LEGACY_BT_SEC_LOW        (1 << 2)
    #define GATTLIB_CONNECTION_OPTIONS_LEGACY_BT_SEC_MEDIUM     (1 << 3)
    #define GATTLIB_CONNECTION_OPTIONS_LEGACY_BT_SEC_HIGH       (1 << 4)
    #define GATTLIB_CONNECTION_OPTIONS_LEGACY_PSM(value)        (((value) & 0x3FF) << 11) //< We encode PSM 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)
     
    #define GATTLIB_CONNECTION_OPTIONS_LEGACY_GET_PSM(options)  (((options) >> 11) && 0x3FF)
    #define GATTLIB_CONNECTION_OPTIONS_LEGACY_GET_MTU(options)  (((options) >> 21) && 0x3FF)
     
    #define GATTLIB_CONNECTION_OPTIONS_LEGACY_DEFAULT \
    		GATTLIB_CONNECTION_OPTIONS_LEGACY_BDADDR_LE_PUBLIC | \
    		GATTLIB_CONNECTION_OPTIONS_LEGACY_BDADDR_LE_RANDOM | \
    		GATTLIB_CONNECTION_OPTIONS_LEGACY_BT_SEC_LOW
    //@}
     
    /**
     * @name Discover filter
     */
    //@{
    #define GATTLIB_DISCOVER_FILTER_USE_NONE                    0
    #define GATTLIB_DISCOVER_FILTER_USE_UUID                    (1 << 0)
    #define GATTLIB_DISCOVER_FILTER_USE_RSSI                    (1 << 1)
    #define GATTLIB_DISCOVER_FILTER_NOTIFY_CHANGE               (1 << 2)
    //@}
     
    /**
     * @name Gattlib Eddystone types
     */
    //@{
    #define GATTLIB_EDDYSTONE_TYPE_UID                          (1 << 0)
    #define GATTLIB_EDDYSTONE_TYPE_URL                          (1 << 1)
    #define GATTLIB_EDDYSTONE_TYPE_TLM                          (1 << 2)
    #define GATTLIB_EDDYSTONE_TYPE_EID                          (1 << 3)
    #define GATTLIB_EDDYSTONE_LIMIT_RSSI                        (1 << 4)
    //@}
     
    /**
     * @name Eddystone ID types defined by its specification: https://github.com/google/eddystone
     */
    //@{
    #define EDDYSTONE_TYPE_UID                                  0x00
    #define EDDYSTONE_TYPE_URL                                  0x10
    #define EDDYSTONE_TYPE_TLM                                  0x20
    #define EDDYSTONE_TYPE_EID                                  0x30
    //@}
     
     
    #define GATTLIB_ERROR           0
    #define GATTLIB_WARNING         1
    #define GATTLIB_INFO            2
    #define GATTLIB_DEBUG           3
     
    #define GATTLIB_LOG(level, args...) if (level <= GATTLIB_LOG_LEVEL) { gattlib_log(level, args); }
     
    typedef struct _gatt_connection_t gatt_connection_t;
    typedef struct _gatt_stream_t gatt_stream_t;
     
    /**
     * Structure to represent a GATT Service and its data in the BLE advertisement packet
     */
    typedef struct {
    	uuid_t   uuid;         /**< UUID of the GATT Service */
    	uint8_t* data;         /**< Data attached to the GATT Service */
    	size_t   data_length;  /**< Length of data attached to the GATT Service */
    } gattlib_advertisement_data_t;
     
    typedef void (*gattlib_event_handler_t)(const uuid_t* uuid, const uint8_t* data, size_t data_length, void* user_data);
     
    /**
     * @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);
     
    /**
     * @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);
     
    /**
     * @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);
     
    /**
     * @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);
     
    /**
     * @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);
     
     
    /**
     * @brief Constant defining Eddystone common data UID in Advertisement data
     */
    extern const uuid_t gattlib_eddystone_common_data_uuid;
     
    /**
     * @brief List of prefix for Eddystone URL Scheme
     */
    extern const char *gattlib_eddystone_url_scheme_prefix[];
     
     
    /**
     * @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);
     
    /**
     * @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);
     
    /**
     * @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);
     
    /**
     * @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);
     
    /**
     * @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);
     
    /**
     * @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);
     
    /**
     * @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);
     
    /**
     * @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);
     
    /**
     * @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);
     
    /**
     * @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);
     
    /**
     * @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);
     
    /**
     * Structure to represent GATT Primary Service
     */
    typedef struct {
    	uint16_t  attr_handle_start; /**< First attribute handle of the GATT Primary Service */
    	uint16_t  attr_handle_end;   /**< Last attibute handle of the GATT Primary Service */
    	uuid_t    uuid;              /**< UUID of the Primary Service */
    } gattlib_primary_service_t;
     
    /**
     * Structure to represent GATT Characteristic
     */
    typedef struct {
    	uint16_t  handle;        /**< Handle of the GATT characteristic */
    	uint8_t   properties;    /**< Property of the GATT characteristic */
    	uint16_t  value_handle;  /**< Handle for the value of the GATT characteristic */
    	uuid_t    uuid;          /**< UUID of the GATT characteristic */
    } gattlib_characteristic_t;
     
    /**
     * Structure to represent GATT Descriptor
     */
    typedef struct {
    	uint16_t handle;        /**< Handle of the GATT Descriptor */
    	uint16_t uuid16;        /**< UUID16 of the GATT Descriptor */
    	uuid_t   uuid;          /**< UUID of the GATT Descriptor */
    } gattlib_descriptor_t;
     
    /**
     * @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);
     
    /**
     * @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);
     
    /**
     * @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);
     
    /**
     * @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);
     
    /**
     * @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);
     
    /**
     * @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);
     
    /**
     * @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);
     
    /**
     * @brief Free buffer allocated by the characteristic reading to store the value
     *
     * @param buffer Buffer to free
     */
    void gattlib_characteristic_free_value(void *ptr);
     
    /**
     * @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);
     
    /**
     * @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);
     
    /**
     * @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);
     
    /**
     * @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);
     
    /**
     * @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);
     
    /**
     * @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);
     
    /**
     * @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);
     
    /*
     * @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);
     
    /*
     * @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);
     
    /*
     * @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);
     
    /*
     * @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);
     
    /*
     * @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);
     
    /*
     * @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);
     
    #if 0 // 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);
    #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);
     
    /**
     * @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);
     
    /**
     * @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);
     
    /**
     * @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);
     
    /**
     * @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);
     
    /**
     * @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);
     
    void gattlib_log(int level, const char *format, ...);
     
    #ifdef __cplusplus
    }
    #endif
     
    #endif

  2. #2
    Expert éminent
    Avatar de jurassic pork
    Homme Profil pro
    Bidouilleur
    Inscrit en
    Décembre 2008
    Messages
    3 951
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Bidouilleur
    Secteur : Industrie

    Informations forums :
    Inscription : Décembre 2008
    Messages : 3 951
    Points : 9 280
    Points
    9 280
    Par défaut
    hello,
    regarde ici .
    Ami calmant, J.P
    Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

  3. #3
    Membre confirmé

    Homme Profil pro
    Développeur informatique
    Inscrit en
    Novembre 2013
    Messages
    343
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Gironde (Aquitaine)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : Administration - Collectivité locale

    Informations forums :
    Inscription : Novembre 2013
    Messages : 343
    Points : 536
    Points
    536
    Billets dans le blog
    2
    Par défaut Premier draft de la traduction de gattlib.h
    Citation Envoyé par jurassic pork Voir le message
    hello,
    regarde ici .
    Ami calmant, J.P
    Bjr,

    J'ai fait une traduction du header. Il y a des points à voir:

    Comment traduire cette déclaration extern const uuid_t gattlib_eddystone_common_data_uuid; ?
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
     
    (*
     * @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[];
    void** adapter se traduit bien par const adapter: PPointer ?

    uuid_t **uuid_list se traduit bien par const uuid_list: array of uuid_t ?

    Code C : Sélectionner tout - Visualiser dans une fenêtre à part
    typedef void (*gattlib_event_handler_t)(const uuid_t* uuid, const uint8_t* data, size_t data_length, void* user_data);
    équivaut à
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    type gattlib_event_handler_t = procedure(const uuid: uuid_t; const data: uint8_t; data_length: size_t; const user_data: Pointer); cdecl;
    ?

    cdlt

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    unit unitLibGattlib;
    // Wrapper pour gattlib
     
    {$mode ObjFPC}{$H+}
    {$IFDEF FPC}
    {$PACKRECORDS C}
    {$ENDIF}
    interface
     
    uses
      Classes, SysUtils;
     
     
    // 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 _gatt_connection_t = 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}
     
    //******************************************************************************
    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;
     
    // *********** 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 *)
    type gattlib_descriptor_t = record
      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;
     
     
    //******** 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; const user_data: Pointer); cdecl;
    (**
     * @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(const 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(const adapter: Pointer; const addr: PChar; const name: PChar; const 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 (const 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;
                                                            const user_data: Pointer);
    (* * @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(const connection: gatt_connection_t; const user_data: Pointer);
    (*
     * @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(const buffer: Pointer; buffer_len: size_t);
     
    //******************************************************************************
    // 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;
    (**
     * @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(const adapter: Pointer; discovered_device_cb: gattlib_discovered_device_t; timeout: size_t; const user_data: Pointer): integer; cdecl; external;
     
    (**
     * @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(const 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;
                                                     const user_data: Pointer): integer; cdecl; external;
     
    (**
     * @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(const 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;
                                                                  const user_data: Pointer): integer; cdecl; external;
     
    (*
     * @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(const adapter: Pointer;
                                             rssi_threshold : int16_t;
                                             eddystone_types: uint32_t;
                                             discovered_device_cb: gattlib_discovered_device_with_data_t;
                                             const user_data: Pointer): integer; cdecl; external;
    (**
     * @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;
    (***
     * @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;
     
    (*
     * @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(const adapter: Pointer;
                             const dst    : PChar;
                             options      : LongWord): gatt_connection_t; cdecl; external;
     
    (**
     * @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(const adapter: Pointer;
                                   const dst    : PChar;
                                   options      : LongWord;
                                   connect_cb   : gatt_connect_cb_t;
                                   const user_data: Pointer): gatt_connection_t; cdecl; external;
    (**
     * @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(const connection: gatt_connection_t): integer; cdecl; external;
    (**
     * @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(const connection: gatt_connection_t;
                                             handler: gattlib_disconnection_handler_t;
                                             const user_data: Pointer); cdecl; external;
    (*
     * @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(const connection: gatt_connection_t;
                                      const services: array of gattlib_primary_service_t;
                                      const services_count: integer): integer; cdecl; external;
    (**
     * @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(const 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;
     
    (*
     * @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(const connection: gatt_connection_t;
                                   const characteristics: array of gattlib_characteristic_t;
                                   const characteristics_count: integer): integer; cdecl; external;
     
    (*
     * @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(const 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;
     
    (*
     * @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(const connection: gatt_connection_t;
                                   const descriptors    : array of gattlib_descriptor_t;
                                   const descriptors_count: integer): integer; cdecl; external;
     
    (*
     * @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(const connection: gatt_connection_t;  const uuid: uuid_t;
                                       const buffer: PPointer;
                                       const buffer_len: size_t): integer; cdecl; external;
     
    (***
     * @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(const connection: gatt_connection_t; const uuid: uuid_t;
                                             gatt_read_cb : gatt_read_cb_t): integer; cdecl; external;
     
     
    (**
     * @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;
     
    (***
     * @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(const connection: gatt_connection_t; const uuid: uuid_t;
                                        const buffer: Pointer; buffer_len: size_t): integer; cdecl; external;
     
    (*
     * @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(const connection: gatt_connection_t;
                                          handle: uint16_t;
                                          const buffer: Pointer; buffer_len: size_t): integer; cdecl; external;
     
    (*
     * @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(const connection: gatt_connection_t; const uuid: uuid_t;
                                                         const buffer: Pointer; buffer_len: size_t): integer; cdecl; external;
     
    (*
     * @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(const 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;
     
    (**
     * @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(const stream: gatt_stream_t;
                                             const buffer: Pointer; buffer_len: size_t): integer; cdecl; external;
     
    (**
     * @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(const stream: gatt_stream_t): integer; cdecl; external;
     
     
     
    (**
     * @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(const connection: gatt_connection_t;
                                                           handle: uint16_t;
                                                           const buffer: Pointer; buffer_len: size_t): Integer; cdecl; external;
     
    (*
     * @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(const connection: gatt_connection_t; const uuid: uuid_t): Integer; cdecl; external;
     
    (*
     * @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(const connection: gatt_connection_t; const uuid: uuid_t): Integer; cdecl; external;
     
    (*
     * @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(const connection: gatt_connection_t; const uuid: uuid_t): Integer; cdecl; external;
     
    (*
     * @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(const connection: gatt_connection_t; const uuid: uuid_t): Integer; cdecl; external;
     
    (*
     * @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(const connection: gatt_connection_t;
                                            notification_handler: gattlib_event_handler_t;
                                            const user_data: pointer); cdecl; external;
    (*
     * @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(const connection: gatt_connection_t;
                                          indication_handler: gattlib_event_handler_t;
                                          const user_data: pointer); cdecl; external;
     
    {$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(const connection: gatt_connection_t;
                              const rssi      : int16_t) : integer; cdecl; external;
     
    {$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(const adapter: Pointer; const mac_address: PChar;
                                       const rssi: int16_t): integer; cdecl; external;
     
    (**
     * @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(const 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;
     
     
    (*
     * @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(const 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;
     
    (*
     * @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;
     
    (**
     * @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;
     
    (**
     * @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;
     
    //  void gattlib_log(int level, const char *format, ...);
    procedure gattlib_log(level: integer; const format: PChar; const Args: array of const); cdecl; external;
     
    //******************************************************************************
    // 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); }
     
     
    //******************************************************************************
     
    const LIB_GATTLIB = 'LibGattlib.dll';
    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.

  4. #4
    Expert confirmé
    Avatar de BeanzMaster
    Homme Profil pro
    Amateur Passionné
    Inscrit en
    Septembre 2015
    Messages
    1 899
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Suisse

    Informations professionnelles :
    Activité : Amateur Passionné
    Secteur : Tourisme - Loisirs

    Informations forums :
    Inscription : Septembre 2015
    Messages : 1 899
    Points : 4 346
    Points
    4 346
    Billets dans le blog
    2
    Par défaut
    Salut tu peux également essayer ce petit outil https://github.com/neslib/Chet qui à l'air mieux que h2pas
    • "L'Homme devrait mettre autant d'ardeur à simplifier sa vie qu'il met à la compliquer" - Henri Bergson
    • "Bien des livres auraient été plus clairs s'ils n'avaient pas voulu être si clairs" - Emmanuel Kant
    • "La simplicité est la sophistication suprême" - Léonard De Vinci
    • "Ce qui est facile à comprendre ou à faire pour toi, ne l'est pas forcément pour l'autre." - Mon pèrei

    Mes projets sur Github - Blog - Site DVP

  5. #5
    Modérateur
    Avatar de tourlourou
    Homme Profil pro
    Biologiste ; Progr(amateur)
    Inscrit en
    Mars 2005
    Messages
    3 858
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 61
    Localisation : France, Yvelines (Île de France)

    Informations professionnelles :
    Activité : Biologiste ; Progr(amateur)

    Informations forums :
    Inscription : Mars 2005
    Messages : 3 858
    Points : 11 301
    Points
    11 301
    Billets dans le blog
    6
    Par défaut
    Bonsoir,

    J'essayerais plutôt :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    type gattlib_event_handler_t = procedure(var uuid: uuid_t; var data: uint8_t; data_length: size_t; const user_data: Pointer); cdecl;
    Delphi 5 Pro - Delphi 11.3 Alexandria Community Edition - CodeTyphon 6.90 sous Windows 10 ; CT 6.40 sous Ubuntu 18.04 (VM)
    . Ignorer la FAQ Delphi et les Cours et Tutoriels Delphi nuit gravement à notre code !

  6. #6
    Membre confirmé

    Homme Profil pro
    Développeur informatique
    Inscrit en
    Novembre 2013
    Messages
    343
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Gironde (Aquitaine)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : Administration - Collectivité locale

    Informations forums :
    Inscription : Novembre 2013
    Messages : 343
    Points : 536
    Points
    536
    Billets dans le blog
    2
    Par défaut Gattlib: Avancement - En attente de conseils et corrections
    Bjr à tous,

    Etat d'avancement du wrapper GATTLIB.

    Pas mal de difficultés à résoudre, sans compter les exemples, qui sont absents.



    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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.

Discussions similaires

  1. Réponses: 0
    Dernier message: 14/08/2017, 09h12
  2. [Lazarus] Utilisation de PascalSCADA avec Lazarus
    Par Darkcrusade64 dans le forum Lazarus
    Réponses: 3
    Dernier message: 01/09/2014, 08h46
  3. Réponses: 7
    Dernier message: 10/03/2013, 15h49
  4. Réponses: 5
    Dernier message: 27/03/2012, 10h43

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo