-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcy_ble_gatt.c
1633 lines (1471 loc) · 66.4 KB
/
cy_ble_gatt.c
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
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************************//**
* \file cy_ble_gatt.c
* \version 3.60
*
* \brief
* This file contains the source code for the GATT API of the PSoC 6 BLE Middleware.
*
********************************************************************************
* \copyright
* Copyright 2017-2021, Cypress Semiconductor Corporation. All rights reserved.
* You may use this file only in accordance with the license, terms, conditions,
* disclaimers, and limitations in the end user license agreement accompanying
* the software package with which this file was provided.
*******************************************************************************/
#include "cy_ble_event_handler.h"
#include "cy_ble_stack_gatt_db.h"
#if defined(CY_IP_MXBLESS)
#if CY_BLE_LIB_HOST_CORE
/*******************************************************************************
* Global Variables
*******************************************************************************/
/* The pointer to the global BLE GATT server config structure */
const cy_stc_ble_gatts_config_t *cy_ble_gattsConfigPtr = NULL;
/* The pointer to the global BLE GATT client config structure */
const cy_stc_ble_gattc_config_t *cy_ble_gattcConfigPtr = NULL;
/******************************************************************************
* Function Name: Cy_BLE_GATTS_Init
***************************************************************************//**
*
* This function initializes server of the GATT service.
*
* \param config: Configuration structure for the GATT.
*
* \return
* \ref cy_en_ble_api_result_t : Return value indicates whether the function succeeded or
* failed. The following are possible error codes.
*
* Error Codes | Description
* ------------ | -----------
* CY_BLE_SUCCESS | The function completed successfully.
* CY_BLE_ERROR_INVALID_PARAMETER | On specifying NULL as input parameter.
*
******************************************************************************/
cy_en_ble_api_result_t Cy_BLE_GATTS_Init(const cy_stc_ble_gatts_config_t *config)
{
cy_en_ble_api_result_t apiResult;
if(config == NULL)
{
apiResult = CY_BLE_ERROR_INVALID_PARAMETER;
}
else
{
cy_stc_ble_gatts_db_info_t dbInfoParam;
/* Registers a pointer to config structure */
cy_ble_gattsConfigPtr = config;
/* Registers a callback to process Stack server events */
Cy_BLE_ServerEventHandlerCallback = &Cy_BLE_ServerEventHandler;
/* Register GATT DB */
dbInfoParam.gattDbPtr = cy_ble_configPtr->gattDB;
dbInfoParam.gattDbTotalEntries = cy_ble_configPtr->params->gattDbIndexCount;
dbInfoParam.gattDbMaxValue = cy_ble_configPtr->params->gattAttrMaxLen;
apiResult = Cy_BLE_GATTS_DbRegister(&dbInfoParam);
}
return(apiResult);
}
/******************************************************************************
* Function Name: Cy_BLE_GATTC_Init
***************************************************************************//**
*
* This function initializes client of the GATT service.
*
* \param config: Configuration structure for the GATT.
*
* \return
* \ref cy_en_ble_api_result_t : Return value indicates whether the function succeeded or
* failed. The following are possible error codes.
*
* Error Codes | Description
* ------------ | -----------
* CY_BLE_SUCCESS | The function completed successfully.
* CY_BLE_ERROR_INVALID_PARAMETER | On specifying NULL as input parameter.
* CY_BLE_ERROR_MEMORY_ALLOCATION_FAILED | Buffer overflow in the registration callback.
*
******************************************************************************/
cy_en_ble_api_result_t Cy_BLE_GATTC_Init(const cy_stc_ble_gattc_config_t *config)
{
cy_en_ble_api_result_t apiResult = CY_BLE_SUCCESS;
if((config == NULL) || ((cy_ble_configPtr->params->gattRole & CY_BLE_GATT_CLIENT) == 0u))
{
apiResult = CY_BLE_ERROR_INVALID_PARAMETER;
}
else
{
uint32_t idx;
/* Registers a pointer to config structure */
cy_ble_gattcConfigPtr = config;
/* Registers a callback to process Stack server events */
Cy_BLE_ClientEventHandlerCallback = &Cy_BLE_ClientEventHandler;
for(idx = 0u; idx < cy_ble_configPtr->params->maxClientCount; idx++)
{
uint32 servCnt = cy_ble_configPtr->context->discServiCount;
uint32 gattServIdx = cy_ble_gattcConfigPtr->serviceDiscIdx;
/* Check service range before clearing to support partial discovery */
if(cy_ble_configPtr->context->serverInfo[(idx * servCnt) + gattServIdx].range.startHandle ==
CY_BLE_GATT_INVALID_ATTR_HANDLE_VALUE)
{
(void)memset(&cy_ble_gattcConfigPtr->attrInfo[idx], 0, sizeof(cy_stc_ble_gattc_t));
/* Initialize UUID */
cy_ble_configPtr->context->serverInfo[(idx * servCnt) + gattServIdx].uuid = CY_BLE_UUID_GATT_SERVICE;
}
}
}
return(apiResult);
}
/******************************************************************************
* Function Name: Cy_BLE_GATTS_ReInitGattDb
***************************************************************************//**
*
* Reinitializes the GATT database.
*
* \return
* \ref cy_en_ble_api_result_t : A function result states if it succeeded or failed with
* error codes:
*
* Error Codes | Description
* ------------ | -----------
* CY_BLE_SUCCESS | GATT database was reinitialized successfully.
* CY_BLE_ERROR_INVALID_STATE | If the function is called in any state except CY_BLE_STATE_ON.
*
******************************************************************************/
cy_en_ble_api_result_t Cy_BLE_GATTS_ReInitGattDb(void)
{
cy_en_ble_api_result_t apiResult;
if(Cy_BLE_GetState() == CY_BLE_STATE_ON)
{
cy_stc_ble_gatts_db_info_t dbInfoParam;
dbInfoParam.gattDbPtr = cy_ble_configPtr->gattDB;
dbInfoParam.gattDbTotalEntries = cy_ble_configPtr->params->gattDbIndexCount;
dbInfoParam.gattDbMaxValue = cy_ble_configPtr->params->gattAttrMaxLen;
apiResult = Cy_BLE_GATTS_DbRegister(&dbInfoParam);
}
else
{
apiResult = CY_BLE_ERROR_INVALID_STATE;
}
return(apiResult);
}
/******************************************************************************
* Function Name: Cy_BLE_GATTS_WriteEventHandler
***************************************************************************//**
*
* Handles the Write Request event for GATT service.
*
* \param eventParam: The pointer to the data structure specified by the event.
*
* \return
* \ref cy_en_ble_gatt_err_code_t : A function returns one of the following status
* values.
*
******************************************************************************/
cy_en_ble_gatt_err_code_t Cy_BLE_GATTS_WriteEventHandler(cy_stc_ble_gatts_write_cmd_req_param_t *eventParam)
{
cy_en_ble_gatt_err_code_t gattErr = CY_BLE_GATT_ERR_NONE;
/* Client Characteristic Configuration descriptor Write Request */
if(eventParam->handleValPair.attrHandle == cy_ble_gattsConfigPtr->attrInfo->cccdHandle)
{
/* Store value to database */
cy_stc_ble_gatts_db_attr_val_info_t dbAttrValInfo;
dbAttrValInfo.handleValuePair = eventParam->handleValPair;
dbAttrValInfo.connHandle = eventParam->connHandle;
dbAttrValInfo.flags = CY_BLE_GATT_DB_PEER_INITIATED;
dbAttrValInfo.offset = 0u;
gattErr = Cy_BLE_GATTS_WriteAttributeValueCCCD(&dbAttrValInfo);
if(gattErr == CY_BLE_GATT_ERR_NONE)
{
if(CY_BLE_IS_INDICATION_ENABLED_IN_PTR(eventParam->handleValPair.value.val))
{
Cy_BLE_ApplCallback((uint32_t)CY_BLE_EVT_GATTS_INDICATION_ENABLED, eventParam);
}
else
{
Cy_BLE_ApplCallback((uint32_t)CY_BLE_EVT_GATTS_INDICATION_DISABLED, eventParam);
}
}
cy_ble_eventHandlerFlag &= (uint8_t) ~CY_BLE_CALLBACK;
}
return(gattErr);
}
/******************************************************************************
* Function Name: Cy_BLE_GATTS_WriteAttributeValueCCCD
***************************************************************************//**
*
* This function extends Cy_BLE_GATTS_WriteAttributeValue() API to support
* storing of separate CCCD values for each (separate) connections.
*
* This function is used to write to the value field of the specified attribute
* in the GATT database of a GATT Server. If attribute is CCCD, the value is
* stored in specific CCCD storages.
* This is a blocking function. No event is generated on calling this function.
*
* If a peer device connected to the GATT Server initiates a write operation,
* this function is executed on the GATT Server. During such a call, the function
* checks for the attribute permissions (flags) before executing the write
* operation by calling.
*
* \param param: Parameter is of type \ref cy_stc_ble_gatts_db_attr_val_info_t.
*
* \return
* \ref cy_en_ble_gatt_err_code_t : Return value indicates whether the function succeeded or
* failed. The following are possible error codes:
*
* Errors codes | Description
* ------------ | -----------
* CY_BLE_GATT_ERR_NONE | On successful operation.
* CY_BLE_GATT_ERR_INVALID_HANDLE | 'handleValuePair.attrHandle' is not valid.
* CY_BLE_GATT_ERR_WRITE_NOT_PERMITTED | write operation is not permitted on this attribute (Peer Initiated).
* CY_BLE_GATT_ERR_UNLIKELY_ERROR | Invalid arguments passed.
* CY_BLE_GATT_ERR_INSUFFICIENT_ENCRYPTION | Link is not encrypted (Peer Initiated).
* CY_BLE_GATT_ERR_INSUFFICIENT_ENC_KEY_SIZE | Link is encrypted with insufficient key size (Peer Initiated).
* CY_BLE_GATT_ERR_INSUFFICIENT_AUTHENTICATION | Link is un-authenticated (Peer Initiated).
* CY_BLE_GATT_ERR_INSUFFICIENT_AUTHORIZATION | Peer client is not authorized (Peer Initiated).
* CY_BLE_GATT_ERR_INVALID_OFFSET | param->offset is invalid.
* CY_BLE_GATT_ERR_INVALID_ATTRIBUTE_LEN | handleValuePair.value.len is invalid.
*
******************************************************************************/
cy_en_ble_gatt_err_code_t Cy_BLE_GATTS_WriteAttributeValueCCCD(cy_stc_ble_gatts_db_attr_val_info_t *param)
{
cy_en_ble_gatt_err_code_t errCode;
errCode = Cy_BLE_GATTS_WriteAttributeValue(param);
/* Support separate CCCD value for each connection */
if((errCode == CY_BLE_GATT_ERR_NONE) &&
(cy_ble_configPtr != NULL) &&(cy_ble_configPtr->flashStorage->cccdCount != 0u) &&
(CY_BLE_GATT_DB_ATTR_CHECK_PRPTY(param->handleValuePair.attrHandle, CY_BLE_GATT_DB_CCCD_ATTR)))
{
uint32_t cccdBlockSize = cy_ble_configPtr->flashStorage->cccdCount + CY_BLE_CCCD_CRC_BYTE;
uint32_t cccdIndex = CY_BLE_GATT_DB_ATTR_GET_CCCD_IDX(param->handleValuePair.attrHandle);
uint32_t cccdOffset = (param->connHandle.attId * cccdBlockSize) + cccdIndex;
(void)memcpy((void*)&cy_ble_configPtr->flashStorage->cccdRamPtr[cccdOffset],
(void*)param->handleValuePair.value.val, (uint32_t)param->handleValuePair.value.len);
/* Set flag to store bonding data to flash */
if( ((cy_ble_configPtr->params->gapRole & (CY_BLE_GAP_PERIPHERAL | CY_BLE_GAP_CENTRAL)) != 0u) &&
(cy_ble_configPtr->params->isBondingReq == CY_BLE_BONDING_YES) &&
(cy_ble_peerBonding[param->connHandle.attId] == CY_BLE_GAP_BONDING) )
{
cy_ble_pendingFlashWrite |= CY_BLE_PENDING_CCCD_FLASH_WRITE_BIT;
}
}
return(errCode);
}
/******************************************************************************
* Function Name: Cy_BLE_GATTS_ReadAttributeValueCCCD
***************************************************************************//**
*
* This function extends Cy_BLE_GATTS_ReadAttributeValue() API to support
* re-storing of separate CCCD values for each(separate) connections.
*
* This function is used to read the value field of the specified attribute from
* the GATT database in a GATT Server. If attribute is CCCD, the value is
* restored from specific CCCD storages.
* This is a blocking function. No event is generated on calling this function.
*
* \param param: Parameter is of type cy_stc_ble_gatts_db_attr_val_info_t.
* param-> offset: not used, to be ignored
*
* \return
* \ref cy_en_ble_gatt_err_code_t : Return value indicates whether the function succeeded or
* failed. The following are possible error codes:
*
* Errors codes | Description
* ------------ | -----------
* CY_BLE_GATT_ERR_NONE | On successful operation.
* CY_BLE_GATT_ERR_INVALID_HANDLE | 'handleValuePair.attrHandle' is not valid.
* CY_BLE_GATT_ERR_READ_NOT_PERMITTED | Read operation is not permitted on this attribute (Peer Initiated).
* CY_BLE_GATT_ERR_UNLIKELY_ERROR | Invalid arguments passed.
* CY_BLE_GATT_ERR_INSUFFICIENT_ENCRYPTION | Link is not encrypted (Peer Initiated).
* CY_BLE_GATT_ERR_INSUFFICIENT_ENC_KEY_SIZE | Link is encrypted with insufficient key size (Peer Initiated).
* CY_BLE_GATT_ERR_INSUFFICIENT_AUTHENTICATION | Link is un-authenticated (Peer Initiated).
* CY_BLE_GATT_ERR_INSUFFICIENT_AUTHORIZATION | Peer client is not authorized (Peer Initiated).
*
******************************************************************************/
cy_en_ble_gatt_err_code_t Cy_BLE_GATTS_ReadAttributeValueCCCD(cy_stc_ble_gatts_db_attr_val_info_t *param)
{
cy_en_ble_gatt_err_code_t errCode;
if( (param->handleValuePair.attrHandle == 0u) || (cy_ble_configPtr == NULL) ||
(param->handleValuePair.attrHandle > cy_ble_configPtr->params->gattDbIndexCount) ||
(param->connHandle.attId >= cy_ble_configPtr->stackParam->maxConnCount) )
{
errCode = CY_BLE_GATT_ERR_INVALID_HANDLE;
}
else
{
if(CY_BLE_GATT_DB_ATTR_CHECK_PRPTY(param->handleValuePair.attrHandle, CY_BLE_GATT_DB_CCCD_ATTR))
{
uint32_t cccdBlockSize = cy_ble_configPtr->flashStorage->cccdCount + CY_BLE_CCCD_CRC_BYTE;
uint32_t cccdIndex = CY_BLE_GATT_DB_ATTR_GET_CCCD_IDX(param->handleValuePair.attrHandle);
uint32_t cccdOffset = (param->connHandle.attId * cccdBlockSize) + cccdIndex;
CY_BLE_GATT_DB_ATTR_SET_GEN_VALUE(param->handleValuePair.attrHandle,
(void*)&cy_ble_configPtr->flashStorage->cccdRamPtr[cccdOffset],
CY_BLE_CCCD_LEN);
}
errCode = Cy_BLE_GATTS_ReadAttributeValue(param);
}
return(errCode);
}
/******************************************************************************
* Function Name: Cy_BLE_GATTS_ReadAttributeValueCCCDReqHandler
***************************************************************************//**
*
* This function is used to handle #CY_BLE_EVT_GATTS_READ_CHAR_VAL_ACCESS_REQ
* request and updated CCCD value in GATT database in a GATT Server for
* multi-connection support.
*
* \param param: Parameter is of type 'cy_stc_ble_gatts_char_val_read_req_t'.
*
* \return
* \ref cy_en_ble_gatt_err_code_t : Return value indicates whether the function succeeded or
* failed. The following are possible error codes.
*
* Error Codes | Description
* ------------ | -----------
* CY_BLE_GATT_ERR_NONE | On successful operation
* CY_BLE_GATT_ERR_INVALID_HANDLE | 'handleValuePair.attrHandle' is not valid
*
******************************************************************************/
cy_en_ble_gatt_err_code_t Cy_BLE_GATTS_ReadAttributeValueCCCDReqHandler(const cy_stc_ble_gatts_char_val_read_req_t *param)
{
cy_en_ble_gatt_err_code_t errCode = CY_BLE_GATT_ERR_NONE;
if((param->attrHandle == 0u) || (cy_ble_configPtr == NULL) ||
(param->attrHandle > cy_ble_configPtr->params->gattDbIndexCount) ||
(param->connHandle.attId >= cy_ble_configPtr->stackParam->maxConnCount))
{
errCode = CY_BLE_GATT_ERR_INVALID_HANDLE;
}
else
{
if(CY_BLE_GATT_DB_ATTR_CHECK_PRPTY(param->attrHandle, CY_BLE_GATT_DB_CCCD_ATTR))
{
uint32 cccdBlockSize = cy_ble_configPtr->flashStorage->cccdCount + CY_BLE_CCCD_CRC_BYTE;
uint32 cccdIndex = CY_BLE_GATT_DB_ATTR_GET_CCCD_IDX(param->attrHandle);
uint32 cccdOffset = (param->connHandle.attId * cccdBlockSize) + cccdIndex;
CY_BLE_GATT_DB_ATTR_SET_GEN_VALUE(param->attrHandle,
(void*)&cy_ble_configPtr->flashStorage->cccdRamPtr[cccdOffset],
CY_BLE_CCCD_LEN);
}
}
return(errCode);
}
/******************************************************************************
* Function Name: Cy_BLE_GATTS_WriteAttributeValueLocal
***************************************************************************//**
*
* Wrapper API for Cy_BLE_GATTS_WriteAttributeValue to trigger a locally
* initiated attribute value write operation to GATT database
*
* This function is used to write to the value field of the specified attribute
* in the GATT database of a GATT Server.
*
* Note: this function doesn't write CCCD attribute value.
*
* This is a blocking function. No event is generated on calling this function.
*
* \param handleValuePair: The pointer to the cy_stc_ble_gatt_handle_value_pair_t
* structure.
*
* \return
* \ref cy_en_ble_gatt_err_code_t : The return value indicates whether the function succeeded
* or failed. The following are possible error codes.
*
* Errors codes | Description
* ------------ | -----------
* CY_BLE_GATT_ERR_NONE | On successful operation.
* CY_BLE_GATT_ERR_INVALID_HANDLE | handleValuePair.attrHandle is not valid.
* CY_BLE_GATT_ERR_UNLIKELY_ERROR | Invalid arguments passed.
* CY_BLE_GATT_ERR_INVALID_ATTRIBUTE_LEN | handleValuePair.value.len is invalid.
*
******************************************************************************/
cy_en_ble_gatt_err_code_t Cy_BLE_GATTS_WriteAttributeValueLocal(const cy_stc_ble_gatt_handle_value_pair_t *handleValuePair)
{
cy_en_ble_gatt_err_code_t ret;
if(handleValuePair == NULL)
{
ret = CY_BLE_GATT_ERR_UNLIKELY_ERROR;
}
else if(CY_BLE_GATT_DB_ATTR_CHECK_PRPTY(handleValuePair->attrHandle, CY_BLE_GATT_DB_CCCD_ATTR))
{
ret = CY_BLE_GATT_ERR_INVALID_HANDLE;
}
else
{
cy_stc_ble_gatts_db_attr_val_info_t param;
param.connHandle.attId = 0u;
param.connHandle.bdHandle = 0u;
param.handleValuePair = *handleValuePair;
param.flags = CY_BLE_GATT_DB_LOCALLY_INITIATED;
param.offset = 0u;
ret = Cy_BLE_GATTS_WriteAttributeValueCCCD(¶m);
}
return (ret);
}
/******************************************************************************
* Function Name: Cy_BLE_GATTS_WriteAttributeValuePeer
***************************************************************************//**
*
* Wrapper API for Cy_BLE_GATTS_WriteAttributeValue() to trigger a peer initiated
* attribute value write operation to GATT database
*
* This function is used to write to the value field of the specified attribute
* in the GATT database of a GATT Server by a peer device.
* If the attribute is CCCD, the value is stored in specific CCCD storages. Writing
* a CCCD value must be executed in the connected state.
* This is a blocking function. No event is generated on calling this function.
*
* If a peer device connected to the GATT Server initiates a Write operation,
* this function is executed on the GATT Server. During such a call, the function
* checks for the attribute permissions (flags) before executing the Write
* operation by calling.
*
* \param connHandle: The pointer to the connection handle.
* \param handleValuePair: The pointer to the cy_stc_ble_gatt_handle_value_pair_t
* structure.
*
* \return
* \ref cy_en_ble_gatt_err_code_t : The return value indicates whether the function succeeded
* or failed. The following are possible error codes.
*
* Errors codes | Description
* ------------ | -----------
* CY_BLE_GATT_ERR_NONE | On successful operation.
* CY_BLE_GATT_ERR_INVALID_HANDLE | handleValuePair.attrHandle is not valid.
* CY_BLE_GATT_ERR_WRITE_NOT_PERMITTED | The write operation is not permitted on this attribute (Peer Initiated).
* CY_BLE_GATT_ERR_UNLIKELY_ERROR | Invalid arguments passed.
* CY_BLE_GATT_ERR_INSUFFICIENT_ENCRYPTION | The link is not encrypted (Peer Initiated).
* CY_BLE_GATT_ERR_INSUFFICIENT_ENC_KEY_SIZE | The link is encrypted with a insufficient key size (Peer Initiated).
* CY_BLE_GATT_ERR_INSUFFICIENT_AUTHENTICATION | The link is un-authenticated (Peer Initiated).
* CY_BLE_GATT_ERR_INSUFFICIENT_AUTHORIZATION | The peer client is not authorized (Peer Initiated).
* CY_BLE_GATT_ERR_INVALID_ATTRIBUTE_LEN | handleValuePair.value.len is invalid.
*
******************************************************************************/
cy_en_ble_gatt_err_code_t Cy_BLE_GATTS_WriteAttributeValuePeer(cy_stc_ble_conn_handle_t *connHandle,
const cy_stc_ble_gatt_handle_value_pair_t *handleValuePair)
{
cy_en_ble_gatt_err_code_t ret;
if((handleValuePair == NULL) || (connHandle == NULL))
{
ret = CY_BLE_GATT_ERR_UNLIKELY_ERROR;
}
else
{
cy_stc_ble_gatts_db_attr_val_info_t param;
param.connHandle = *connHandle;
param.handleValuePair = *handleValuePair;
param.offset = 0u;
param.flags = CY_BLE_GATT_DB_PEER_INITIATED;
ret = Cy_BLE_GATTS_WriteAttributeValueCCCD(¶m);
}
return (ret);
}
/******************************************************************************
* Function Name: Cy_BLE_GATTS_ReadAttributeValueLocal
***************************************************************************//**
*
* Wrapper API for Cy_BLE_GATTS_ReadAttributeValue() to trigger a local initiated
* attribute value read operation to GATT database
*
* This function is used to read the value field of the specified attribute from
* the GATT database in a GATT Server. If the attribute is CCCD, the value is
* restored from specific CCCD storages. Reading a CCCD value must be executed
* in the connected state.
* This is a blocking function. No event is generated on calling this function.
*
* \param connHandle: The pointer to the connection handle. This parameter
* is required only for the Read CCCD attribute.
* If you read a non-CCCD attribute, the connection handle
* can be NULL.
*
* \param handleValuePair: The pointer to the cy_stc_ble_gatt_handle_value_pair_t
* structure.
*
* \return
* \ref cy_en_ble_gatt_err_code_t : The return value indicates whether the function succeeded
* or failed. The following are possible error codes.
*
* Error Codes | Description
* ------------ | -----------
* CY_BLE_GATT_ERR_NONE | On successful operation
* CY_BLE_GATT_ERR_INVALID_HANDLE | handleValuePair.attrHandle is not valid
* CY_BLE_GATT_ERR_UNLIKELY_ERROR | Invalid arguments passed
*
******************************************************************************/
cy_en_ble_gatt_err_code_t Cy_BLE_GATTS_ReadAttributeValueLocal(cy_stc_ble_conn_handle_t *connHandle,
const cy_stc_ble_gatt_handle_value_pair_t *handleValuePair)
{
cy_en_ble_gatt_err_code_t ret;
cy_stc_ble_gatts_db_attr_val_info_t param;
if( (cy_ble_configPtr == NULL) || (handleValuePair == NULL) || ((connHandle == NULL) &&
(CY_BLE_GATT_DB_ATTR_CHECK_PRPTY(handleValuePair->attrHandle, CY_BLE_GATT_DB_CCCD_ATTR))) )
{
ret = CY_BLE_GATT_ERR_UNLIKELY_ERROR;
}
else
{
if((CY_BLE_GATT_DB_ATTR_CHECK_PRPTY(handleValuePair->attrHandle, CY_BLE_GATT_DB_CCCD_ATTR)) &&
(connHandle != NULL))
{
param.connHandle = *connHandle;
}
else
{
param.connHandle.attId = 0u;
param.connHandle.bdHandle = 0u;
}
param.flags = CY_BLE_GATT_DB_LOCALLY_INITIATED;
param.offset = 0u;
param.handleValuePair = *handleValuePair;
ret = Cy_BLE_GATTS_ReadAttributeValueCCCD(¶m);
}
return (ret);
}
/******************************************************************************
* Function Name: Cy_BLE_GATTS_ReadAttributeValuePeer
***************************************************************************//**
*
* Wrapper API for Cy_BLE_GATTS_ReadAttributeValue() to trigger a peer initiated
* attribute value read operation to GATT database.
*
* This function is used to read the value field of the specified attribute from
* the GATT database in a GATT Server by a peer device.
* If the attribute is CCCD, the value is restored from specific CCCD storages.
* Reading a CCCD value must be executed in the connected state.
* This is a blocking function. No event is generated on calling this function.
*
* If a peer initiates a call to this function, the function checks for changed
* attribute permissions before performing this operation.
*
* \param connHandle: The pointer to the connection handle.
* \param handleValuePair: The pointer to the cy_stc_ble_gatt_handle_value_pair_t
* structure.
*
* \return
* \ref cy_en_ble_gatt_err_code_t : The return value indicates whether the function succeeded
* or failed. The following are possible error codes.
*
* Errors codes | Description
* ------------ | -----------
* CY_BLE_GATT_ERR_NONE | On successful operation.
* CY_BLE_GATT_ERR_INVALID_HANDLE | handleValuePair.attrHandle is not valid.
* CY_BLE_GATT_ERR_READ_NOT_PERMITTED | The Read operation is not permitted on this attribute (Peer Initiated).
* CY_BLE_GATT_ERR_UNLIKELY_ERROR | Invalid arguments passed.
* CY_BLE_GATT_ERR_INSUFFICIENT_ENCRYPTION | The link is not encrypted (Peer Initiated).
* CY_BLE_GATT_ERR_INSUFFICIENT_ENC_KEY_SIZE | The link is encrypted with an insufficient key size (Peer Initiated).
* CY_BLE_GATT_ERR_INSUFFICIENT_AUTHENTICATION | The link is un-authenticated (Peer Initiated).
* CY_BLE_GATT_ERR_INSUFFICIENT_AUTHORIZATION | The peer client is not authorized (Peer Initiated).
*
******************************************************************************/
cy_en_ble_gatt_err_code_t Cy_BLE_GATTS_ReadAttributeValuePeer(cy_stc_ble_conn_handle_t *connHandle,
const cy_stc_ble_gatt_handle_value_pair_t *handleValuePair)
{
cy_en_ble_gatt_err_code_t ret;
if((handleValuePair == NULL) || (connHandle == NULL))
{
ret = CY_BLE_GATT_ERR_UNLIKELY_ERROR;
}
else
{
cy_stc_ble_gatts_db_attr_val_info_t param ;
param.connHandle = *connHandle;
param.handleValuePair = *handleValuePair;
param.flags = CY_BLE_GATT_DB_PEER_INITIATED;
param.offset = 0u;
ret = Cy_BLE_GATTS_ReadAttributeValueCCCD(¶m);
}
return (ret);
}
/** \cond IGNORE */
/******************************************************************************
* Function Name: Cy_BLE_GATTS_ReadCccd
***************************************************************************//**
*
* This function is used to read CCCD value. If the attrHandle is
* the characteristic type, the function loops through all descriptors and try
* to find CCCD.
*
* \param connHandle: The pointer to the connection handle.
* \param attrHandle: The attribute handle of the CCCD descriptor or
* the characteristic which includes the CCCD descriptor.
* \param *value: The pointer to the location where the CCCD descriptor
* value data should be stored.
*
* \return
* \ref cy_en_ble_api_result_t : The return value indicates whether the function
* succeeded or failed. The following are possible error codes.
*
* Errors codes | Description
* ------------ | -----------
* CY_BLE_SUCCESS | The request was handled successfully.
* CY_BLE_ERROR_NO_DEVICE_ENTITY | If there is no connection for
* | corresponding bdHandle.
* CY_BLE_ERROR_INVALID_PARAMETER | Validation of the input parameter failed.
*
******************************************************************************/
cy_en_ble_api_result_t Cy_BLE_GATTS_ReadCccd(cy_stc_ble_conn_handle_t *connHandle,
cy_ble_gatt_db_attr_handle_t attrHandle,
uint16_t *value)
{
cy_en_ble_api_result_t apiResult = CY_BLE_ERROR_INVALID_PARAMETER;
bool isCccd = false;
if((connHandle == NULL) || (cy_ble_configPtr == NULL))
{
apiResult = CY_BLE_ERROR_INVALID_PARAMETER;
}
else if(Cy_BLE_GetConnectionState(*connHandle) < CY_BLE_CONN_STATE_CONNECTED)
{
apiResult = CY_BLE_ERROR_NO_DEVICE_ENTITY;
}
else if( (attrHandle <= cy_ble_configPtr->params->gattDbIndexCount) &&
(connHandle->attId < cy_ble_configPtr->stackParam->maxConnCount) )
{
uint32_t i;
for( i = 0u; (i <= ((uint32_t)CY_BLE_GATT_DB_GET_END_HANDLE(attrHandle) - attrHandle)) && (isCccd == false) ; i++)
{
if((CY_BLE_GATT_DB_ATTR_CHECK_PRPTY(attrHandle + i, CY_BLE_GATT_DB_CCCD_ATTR)) &&
(CY_BLE_GATT_DB_ATTR_GET_CCCD_ATTR_GEN_PTR(connHandle->attId, attrHandle + i) != NULL))
{
/* Read the CCCD value */
*value = *CY_BLE_GATT_DB_ATTR_GET_CCCD_ATTR_GEN_PTR(connHandle->attId, attrHandle + i);
isCccd = true;
}
}
}
else
{
/* Do nothing. */
}
return((isCccd == true) ? CY_BLE_SUCCESS : apiResult);
}
/** \endcond */
/******************************************************************************
* Function Name: Cy_BLE_GATTS_IsNotificationEnabled
***************************************************************************//**
*
* Wrapper API to read CCCD attribute value from the local GATT database and
* check if the notification is enabled by the peer device.
*
* This function is used to be aware of the notification status (enabled/disabled)
*
* \param connHandle: The pointer to the connection handle.
* \param attrHandle: The attribute handle of the CCCD descriptor or
* the characteristic which includes the CCCD descriptor.
*
* \return
* * true - Notification is enabled.
* * false - Notification is disabled or if reading CCCD returned an error.
*
******************************************************************************/
bool Cy_BLE_GATTS_IsNotificationEnabled(cy_stc_ble_conn_handle_t *connHandle,
cy_ble_gatt_db_attr_handle_t attrHandle)
{
bool ret = false;
uint16_t cccdValue;
/* Read CCCD and check the notification bit */
if(Cy_BLE_GATTS_ReadCccd(connHandle, attrHandle, &cccdValue) == CY_BLE_SUCCESS)
{
ret = ((cccdValue & CY_BLE_CCCD_NOTIFICATION )!= 0u) ? true : false;
}
return (ret);
}
/******************************************************************************
* Function Name: Cy_BLE_GATTS_IsIndicationEnabled
***************************************************************************//**
*
* Wrapper API to read CCCD attribute value from the local GATT database and
* check if the indication is enabled by the peer device.
*
* This function is used to be aware of the indication status (enabled/disabled)
*
* \param connHandle: The pointer to the connection handle.
* \param attrHandle: The attribute handle of the CCCD descriptor or
* the characteristic which includes the CCCD descriptor.
*
* \return
* * true - Indication is enabled.
* * false - Indication is disabled or reading CCCD returned an error.
*
******************************************************************************/
bool Cy_BLE_GATTS_IsIndicationEnabled(cy_stc_ble_conn_handle_t *connHandle,
cy_ble_gatt_db_attr_handle_t attrHandle)
{
bool ret = false;
uint16_t cccdValue;
/* Read CCCD and check the indication bit */
if(Cy_BLE_GATTS_ReadCccd(connHandle, attrHandle, &cccdValue) == CY_BLE_SUCCESS)
{
ret = ((cccdValue & CY_BLE_CCCD_INDICATION) != 0u) ? true : false;
}
return (ret);
}
/******************************************************************************
* Function Name: Cy_BLE_GATTS_SendNotification
***************************************************************************//**
*
* Wrapper API to write the attribute value to local GATT database and send
* the updated value to the peer device using notification procedure.
* This API will internally use Cy_BLE_GATTS_WriteAttributeValueLocal() and
* Cy_BLE_GATTS_Notification() APIs
*
* \param connHandle: The pointer to the connection handle.
* \param handleValuePair: The pointer to the cy_stc_ble_gatt_handle_value_pair_t structure.
*
* \return
* \ref cy_en_ble_api_result_t : The return value indicates whether the function succeeded
* or failed. The following are possible error codes.
*
* <table>
* <tr>
* <th>Error Codes</th>
* <th>Description</th>
* </tr>
* <tr>
* <td>CY_BLE_SUCCESS</td>
* <td>On successful operation</td>
* </tr>
* <tr>
* <td>CY_BLE_ERROR_NO_DEVICE_ENTITY</td>
* <td>If there is no connection for corresponding bdHandle.</td>
* </tr>
* <tr>
* <td>CY_BLE_ERROR_INVALID_PARAMETER</td>
* <td>Validation of the input parameter failed.</td>
* </tr>
* <tr>
* <td>CY_BLE_ERROR_INVALID_OPERATION</td>
* <td>This operation is not permitted. Or an error was returned during
* the Write attribute value in the GATT database.
* </td>
* </tr>
* <tr>
* <td>CY_BLE_ERROR_NTF_DISABLED</td>
* <td>Characteristic notifications disabled.</td>
* </tr>
* </table>
*
* \note
* This operation is not permitted when the BLE Stack is busy processing
* previous requests. The #CY_BLE_ERROR_INVALID_OPERATION error code will
* be returned if the stack queue is full or for other reasons, the stack
* cannot process the operation. If the BLE Stack busy event
* #CY_BLE_EVT_STACK_BUSY_STATUS is triggered with the status busy, calling
* this API will trigger this error code. For details, refer to
* the #CY_BLE_EVT_STACK_BUSY_STATUS event.
*
******************************************************************************/
cy_en_ble_api_result_t Cy_BLE_GATTS_SendNotification(cy_stc_ble_conn_handle_t *connHandle,
cy_stc_ble_gatt_handle_value_pair_t *handleValuePair)
{
cy_en_ble_api_result_t apiResult = CY_BLE_SUCCESS;
if((connHandle == NULL) || (handleValuePair == NULL))
{
apiResult = CY_BLE_ERROR_INVALID_PARAMETER;
}
else if (Cy_BLE_GetConnectionState(*connHandle) < CY_BLE_CONN_STATE_CONNECTED)
{
apiResult = CY_BLE_ERROR_NO_DEVICE_ENTITY;
}
else if(Cy_BLE_GATTS_WriteAttributeValueLocal(handleValuePair) != CY_BLE_GATT_ERR_NONE)
{
apiResult = CY_BLE_ERROR_INVALID_OPERATION;
}
else if(Cy_BLE_GATTS_IsNotificationEnabled(connHandle, handleValuePair->attrHandle) == false)
{
apiResult = CY_BLE_ERROR_NTF_DISABLED;
}
else
{
/* Fill all fields of the Write Request structure ... */
cy_stc_ble_gatts_handle_value_ntf_t param;
param.handleValPair = *handleValuePair;
param.connHandle = *connHandle;
/* ... and send notification to the client using a previously filled structure */
apiResult = Cy_BLE_GATTS_Notification(¶m);
}
return (apiResult);
}
/******************************************************************************
* Function Name: Cy_BLE_GATTS_SendIndication
***************************************************************************//**
*
* Wrapper API to write the attribute value to local GATT database and send the
* updated value to the peer device using indication procedure. This API will
* internally use Cy_BLE_GATTS_WriteAttributeValueLocal() and
* Cy_BLE_GATTS_Indication() APIs
*
* \param connHandle: The pointer to the connection handle.
* \param handleValuePair: The pointer to the cy_stc_ble_gatt_handle_value_pair_t
* structure.
*
* \return
* \ref cy_en_ble_api_result_t : The return value indicates whether the function succeeded
* or failed. The following are possible error codes.
*
* <table>
* <tr>
* <th>Error Codes</th>
* <th>Description</th>
* </tr>
* <tr>
* <td>CY_BLE_SUCCESS</td>
* <td>On successful operation</td>
* </tr>
* <tr>
* <td>CY_BLE_ERROR_NO_DEVICE_ENTITY</td>
* <td>If there is no connection for corresponding bdHandle.</td>
* </tr>
* <tr>
* <td>CY_BLE_ERROR_INVALID_PARAMETER</td>
* <td>Validation of the input parameter failed.</td>
* </tr>
* <tr>
* <td>CY_BLE_ERROR_INVALID_OPERATION</td>
* <td>This operation is not permitted. Or an error was returned during
* the Write attribute value in the GATT database.
* </td>
* </tr>
* <tr>
* <td>CY_BLE_ERROR_IND_DISABLED</td>
* <td>Characteristic indications disabled.</td>
* </tr>
* </table>
*
* \note
* This operation is not permitted when the BLE Stack is busy processing
* previous requests. The #CY_BLE_ERROR_INVALID_OPERATION error code will
* be returned if the BLE Stack queue is full or for other reasons, the stack
* cannot process the operation. If the BLE Stack busy event
* #CY_BLE_EVT_STACK_BUSY_STATUS is triggered with the status busy, calling
* this API will trigger this error code. For details, refer to
* the #CY_BLE_EVT_STACK_BUSY_STATUS event.
*
******************************************************************************/
cy_en_ble_api_result_t Cy_BLE_GATTS_SendIndication(cy_stc_ble_conn_handle_t *connHandle,
cy_stc_ble_gatt_handle_value_pair_t *handleValuePair)
{
cy_en_ble_api_result_t apiResult = CY_BLE_SUCCESS;
if((connHandle == NULL) || (handleValuePair == NULL))
{
apiResult = CY_BLE_ERROR_INVALID_PARAMETER;
}
else if (Cy_BLE_GetConnectionState(*connHandle) < CY_BLE_CONN_STATE_CONNECTED)
{
apiResult = CY_BLE_ERROR_NO_DEVICE_ENTITY;
}
else if(Cy_BLE_GATTS_WriteAttributeValueLocal(handleValuePair) != CY_BLE_GATT_ERR_NONE)
{
apiResult = CY_BLE_ERROR_INVALID_OPERATION;
}
else if(Cy_BLE_GATTS_IsIndicationEnabled(connHandle, handleValuePair->attrHandle) == false)
{
apiResult = CY_BLE_ERROR_IND_DISABLED;
}
else
{
/* Fill all fields of the Write Request structure ... */
cy_stc_ble_gatts_handle_value_ind_t param;
param.handleValPair = *handleValuePair;
param.connHandle = *connHandle;
/* ... and send indication to the client using a previously filled structure */
apiResult = Cy_BLE_GATTS_Indication(¶m);
}
return (apiResult);
}
/******************************************************************************
* Function Name: Cy_BLE_GATTS_SendErrorRsp
***************************************************************************//**
*
* Replacement for Cy_BLE_GATTS_ErrorRsp() API.
*
* This function sends an Error Response to the peer device. The Error Response
* is used to state that a given request cannot be performed, and to provide the
* reason as defined in \ref cy_en_ble_gatt_err_code_t. This is a non-blocking
* function.
*
* Deprecate the Cy_BLE_GATTS_ErrorRsp() API in BLE_PDL v2_0
*
* This function sends an Error Response to the peer device. The Error Response
* is used to state that a given request cannot be performed, and to provide the
* reason as defined in \ref cy_en_ble_gatt_err_code_t. This is a non-blocking
* function.
*
* \param connHandle: The pointer to the connection handle.
* \param errRspParam: The pointer to the cy_stc_ble_gatt_err_info_t structure
* where, the following to be set:
* errRspParam->opCode
* errRspParam->attrHandle
* errRspParam->errorCode
*
* \return
* \ref cy_en_ble_api_result_t : the return value indicates whether the function
* succeeded or failed. Following are the possible error codes.
*
* Error codes | Description
* ------------ | -----------
* CY_BLE_SUCCESS | On successful operation.
* CY_BLE_ERROR_INVALID_PARAMETER | If param is NULL or connHandle is invalid.
* CY_BLE_ERROR_INVALID_OPERATION | This operation is not permitted.
* CY_BLE_ERROR_MEMORY_ALLOCATION_FAILED | Memory allocation failed.