-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcy_ble_ess.c
3044 lines (2758 loc) · 144 KB
/
cy_ble_ess.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_ess.c
* \version 3.60
*
* \brief
* Contains the source code for the Environmental Sensing service.
*
********************************************************************************
* \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"
#if defined(CY_IP_MXBLESS)
#if CY_BLE_LIB_HOST_CORE
/*******************************************************************************
* Private Function Prototypes
*******************************************************************************/
static cy_en_ble_gatt_err_code_t Cy_BLE_ESS_EventHandler(uint32_t eventCode, void *eventParam);
static cy_en_ble_gatt_err_code_t Cy_BLE_ESSS_EventHandler(uint32_t eventCode, void *eventParam);
static cy_en_ble_gatt_err_code_t Cy_BLE_ESSC_EventHandler(uint32_t eventCode, void *eventParam);
static cy_en_ble_gatt_err_code_t Cy_BLE_ESSS_WriteEventHandler(cy_stc_ble_gatts_write_cmd_req_param_t *eventParam);
static void Cy_BLE_ESSS_PrepareWriteRequestEventHandler(const cy_stc_ble_gatts_prep_write_req_param_t *eventParam);
static void Cy_BLE_ESSS_ExecuteWriteRequestEventHandler(cy_stc_ble_gatts_exec_write_req_t *eventParam);
static void Cy_BLE_ESSS_ConfirmationEventHandler(const cy_stc_ble_conn_handle_t *eventParam);
static void Cy_BLE_ESSC_DiscoverCharacteristicsEventHandler(const cy_stc_ble_disc_char_info_t *discCharInfo);
static void Cy_BLE_ESSC_DiscoverCharDescriptorsEventHandler(cy_stc_ble_disc_descr_info_t *discDescrInfo);
static void Cy_BLE_ESSC_GetCharRange(cy_stc_ble_disc_range_info_t *charRangeInfo);
static void Cy_BLE_ESSC_NotificationEventHandler(cy_stc_ble_gattc_handle_value_ntf_param_t *eventParam);
static void Cy_BLE_ESSC_IndicationEventHandler(cy_stc_ble_gattc_handle_value_ind_param_t *eventParam);
static void Cy_BLE_ESSC_ReadResponseEventHandler(cy_stc_ble_gattc_read_rsp_param_t *eventParam);
static void Cy_BLE_ESSC_ReadLongRespEventHandler(const cy_stc_ble_gattc_read_rsp_param_t *eventParam);
static void Cy_BLE_ESSC_WriteResponseEventHandler(const cy_stc_ble_conn_handle_t *eventParam);
static void Cy_BLE_ESSC_ExecuteWriteResponseEventHandler(const cy_stc_ble_gattc_exec_write_rsp_t *eventParam);
static void Cy_BLE_ESSC_ErrorResponseEventHandler(const cy_stc_ble_gatt_err_param_t *eventParam);
/*******************************************************************************
* Global Variables
*******************************************************************************/
/* The pointers to the callback functions */
static cy_ble_callback_t Cy_BLE_ESS_ApplCallback = NULL;
static cy_ble_event_handler_cb_t Cy_BLE_ESSC_EventHandlerCallback = NULL;
static cy_ble_event_handler_cb_t Cy_BLE_ESSS_EventHandlerCallback = NULL;
/* The internal storage for the last request handle to check response */
static cy_ble_gatt_db_attr_handle_t cy_ble_esscReqHandle[CY_BLE_MAX_CONNECTION_INSTANCES];
/* The internal storage for the last request handle to check response for server */
static cy_ble_gatt_db_attr_handle_t cy_ble_esssReqHandle;
/* The pointer to a global BLE ESS server config structure */
const cy_stc_ble_esss_config_t *cy_ble_esssConfigPtr = NULL;
/* The pointer to a global BLE ESS client config structure */
const cy_stc_ble_essc_config_t *cy_ble_esscConfigPtr = NULL;
/* Read Long Descriptors variables */
static uint8_t *cy_ble_esscRdLongBuffPtr[CY_BLE_MAX_CONNECTION_INSTANCES] = { 0u };
static uint32_t cy_ble_esscRdLongBuffLen[CY_BLE_MAX_CONNECTION_INSTANCES] = { 0u };
static uint8_t cy_ble_esscCurrLen[CY_BLE_MAX_CONNECTION_INSTANCES] = { 0u };
static uint8_t activeCharInstance[CY_BLE_MAX_CONNECTION_INSTANCES] = { 0u };
/******************************************************************************
* Function Name: Cy_BLE_ESSS_Init
***************************************************************************//**
*
* This function initializes server of the Environmental Sensing service.
*
* \param config: Configuration structure for the ESS.
*
* \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_ESSS_Init(const cy_stc_ble_esss_config_t *config)
{
cy_en_ble_api_result_t apiResult;
if(config == NULL)
{
apiResult = CY_BLE_ERROR_INVALID_PARAMETER;
}
else
{
/* Registers a pointer to config structure */
cy_ble_esssConfigPtr = config;
/* Registers event handler for the ESS */
apiResult = Cy_BLE_RegisterServiceEventHandler(&Cy_BLE_ESS_EventHandler);
/* Registers a pointer to server event handler function */
Cy_BLE_ESSS_EventHandlerCallback = &Cy_BLE_ESSS_EventHandler;
/* Sets the value for Aggregate characteristic */
cy_ble_esssReqHandle = CY_BLE_GATT_INVALID_ATTR_HANDLE_VALUE;
}
return(apiResult);
}
/******************************************************************************
* Function Name: Cy_BLE_ESSC_Init
***************************************************************************//**
*
* This function initializes client of the Environmental Sensing service.
*
* \param config: Configuration structure for the ESS.
*
* \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_ESSC_Init(const cy_stc_ble_essc_config_t *config)
{
cy_en_ble_api_result_t apiResult;
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_esscConfigPtr = config;
/* Registers event handler for the ESS */
apiResult = Cy_BLE_RegisterServiceEventHandler(&Cy_BLE_ESS_EventHandler);
/* Registers a pointer to client event handler function */
Cy_BLE_ESSC_EventHandlerCallback = &Cy_BLE_ESSC_EventHandler;
for(idx = 0u; idx < cy_ble_configPtr->params->maxClientCount; idx++)
{
uint32_t j;
uint32 servCnt = cy_ble_configPtr->context->discServiCount;
uint32 essServIdx = cy_ble_esscConfigPtr->serviceDiscIdx;
if(cy_ble_configPtr->context->serverInfo[idx * servCnt].range.startHandle ==
CY_BLE_GATT_INVALID_ATTR_HANDLE_VALUE)
{
uint32_t i;
for(i = 0u; (i < (uint32_t)CY_BLE_AIOS_CHAR_COUNT); i++)
{
/* Get pointer to ESS client structure */
cy_stc_ble_essc_t *esscPtr = (cy_stc_ble_essc_t *)&cy_ble_esscConfigPtr->attrInfo[idx];
for(j = 0u; j < cy_ble_esscConfigPtr->charInstances[i]; j++)
{
(void)memset(&esscPtr->charInfoAddr[i].charInfoPtr[j], 0, sizeof(cy_stc_ble_essc_char_t));
}
}
/* Initialize uuid */
cy_ble_configPtr->context->serverInfo[(idx * servCnt) + essServIdx].uuid =
CY_BLE_UUID_ENVIRONMENTAL_SENSING_SERVICE;
}
cy_ble_esscRdLongBuffPtr[idx] = NULL;
cy_ble_esscRdLongBuffLen[idx] = 0u;
cy_ble_esscCurrLen[idx] = 0u;
cy_ble_esscReqHandle[idx] = CY_BLE_GATT_INVALID_ATTR_HANDLE_VALUE;
activeCharInstance[idx] = 0u;
}
}
return(apiResult);
}
/******************************************************************************
* Function Name: Cy_BLE_ESS_RegisterAttrCallback
***************************************************************************//**
*
* Registers a callback function for service-specific attribute operations.
* Service-specific Write Requests from the peer device will not be handled with
* an unregistered callback function.
*
* \param callbackFunc: An application layer event callback function to receive
* events from the PSoC 6 BLE Middleware. The definition of \ref cy_ble_callback_t
* for ESS service is:<br>
* typedef void (* cy_ble_callback_t) (uint32_t eventCode, void *eventParam),
* where:
* * eventCode: Indicates the event that triggered this callback
* (e.g. #CY_BLE_EVT_ESSS_NOTIFICATION_ENABLED).
* * eventParam: Contains the parameters corresponding to the
* current event; (e.g. pointer to \ref cy_stc_ble_ess_char_value_t
* structure that contains details of the characteristic
* for which the notification enabled event was triggered).
*
******************************************************************************/
void Cy_BLE_ESS_RegisterAttrCallback(cy_ble_callback_t callbackFunc)
{
Cy_BLE_ESS_ApplCallback = callbackFunc;
}
/******************************************************************************
* Function Name: Cy_BLE_ESS_Get24ByPtr
***************************************************************************//**
*
* Returns a three-bytes value by using a pointer to the LSB. The value is
* returned as uint32_t.
*
* \param ptr: The pointer to the LSB of three-byte data (little-endian).
*
* \return
* uint32_t value: Three-byte data.
*
******************************************************************************/
uint32_t Cy_BLE_ESS_Get24ByPtr(const uint8_t ptr[])
{
return(((uint32_t)ptr[0u]) | ((uint32_t)(((uint32_t)ptr[1u]) << 8u)) | ((uint32_t)((uint32_t)ptr[2u]) << 16u));
}
/******************************************************************************
* Function Name: Cy_BLE_ESS_Get32ByPtr
***************************************************************************//**
*
* Returns a four-byte value by using a pointer to the LSB.
*
* \param ptr: The pointer to the LSB of four-byte data (little-endian).
*
* \return
* uint32_t value: Four-byte data.
*
******************************************************************************/
uint32_t Cy_BLE_ESS_Get32ByPtr(const uint8_t ptr[])
{
return(((uint32_t)ptr[0u]) | ((uint32_t)(((uint32_t)ptr[1u]) << 8u)) | ((uint32_t)((uint32_t)ptr[2u]) << 16u) |
((uint32_t)((uint32_t)ptr[3u]) << 24u));
}
/******************************************************************************
* Function Name: Cy_BLE_ESSS_CheckIfInRange
***************************************************************************//**
*
* Checks if the "value" is in the range of "min" and "max". As the ESS
* characteristics operate with signed or unsigned value types and the value in
* the GATT database are stored in unsigned variables. Need to clearly identify
* the signedness of each "value", "min" and "max". After that the values can
* be properly compared. The signedness of each parameter is encoded in "state"
* and has the following meaning:
* State 0: All three are positive (all three are of unsigned type)
* State 1: Min - positive, Max - positive, Value - negative
* State 2: Min - positive, Max - negative, Value - positive
* State 3: Min - positive, Max - negative, Value - negative
* State 4: Min - negative, Max - positive, Value - positive
* State 5: Min - negative, Max - positive, Value - negative
* State 6: Min - negative, Max - negative, Value - positive
* State 7: All three are negative
*
* min - Minimum inclusive limit.
* max - Minimum inclusive limit.
* value - The value to be checked if it is in the range.
* state - The state as shown above.
*
* \return
* A return value of type cy_en_ble_gatt_err_code_t.
* * CY_BLE_GATT_ERR_NONE - "value" is in the range.
* * CY_BLE_GATT_ERR_OUT_OF_RANGE - "value" is not in the range.
*
******************************************************************************/
cy_en_ble_gatt_err_code_t Cy_BLE_ESSS_CheckIfInRange(uint32_t min,
uint32_t max,
uint32_t value,
uint8_t state)
{
cy_en_ble_gatt_err_code_t gattErr = CY_BLE_GATT_ERR_NONE;
switch(state)
{
/* All values are positive */
case CY_BLE_ESS_STATE_0:
if((min > value) || (max < value))
{
gattErr = CY_BLE_GATT_ERR_OUT_OF_RANGE;
}
break;
/* "Not in range" cases. Min and Max are negative and value is positive or
* vice versa.
*/
case CY_BLE_ESS_STATE_1:
case CY_BLE_ESS_STATE_6:
gattErr = CY_BLE_GATT_ERR_OUT_OF_RANGE;
break;
/* Min is negative, Max and value are positive */
case CY_BLE_ESS_STATE_4:
if(max < value)
{
gattErr = CY_BLE_GATT_ERR_OUT_OF_RANGE;
}
break;
/* Max is positive, Min and value are negative */
case CY_BLE_ESS_STATE_5:
if(min > value)
{
gattErr = CY_BLE_GATT_ERR_OUT_OF_RANGE;
}
break;
/* All values are negative */
case CY_BLE_ESS_STATE_7:
if((min > value) || (max < value))
{
gattErr = CY_BLE_GATT_ERR_OUT_OF_RANGE;
}
break;
/* Invalid cases. Handled by customizer. */
case CY_BLE_ESS_STATE_2:
case CY_BLE_ESS_STATE_3:
default:
gattErr = CY_BLE_GATT_ERR_OUT_OF_RANGE;
break;
}
return(gattErr);
}
/******************************************************************************
* Function Name: Cy_BLE_ESS_HandleRangeCheck
***************************************************************************//**
*
* Performs an extraction of the characteristic value ranges then compares it to
* the value received from the client and returns a result of the comparison.
*
* \param connHandle: The connection handle.
* \param charIndex: The index of the service characteristic. Starts with zero.
* \param charInstance: The instance number of the characteristic specified by
* "charIndex".
* \param length: The length of a buffer to store the main and may ranges. Can be 2,4,6
* or 8 bytes.
* \param type: Identifies the type of the value pointed by "pValue" (0 - the value
* is of unsigned type, 1 - the value is of signed type).
* \param pValue: The pointer to the value to be validated if it is in the range.
*
*
* \return
* A return value of type cy_en_ble_gatt_err_code_t.
* * CY_BLE_GATT_ERR_NONE - The value stored in "pValue" is in ranges specified
* by Valid Range descriptor of the characteristic
* addressed by "charIndex" and "charInstance".
* * CY_BLE_GATT_ERR_UNLIKELY_ERROR - Failed to read Valid Range Descriptor
* value.
* * CY_BLE_GATT_ERR_OUT_OF_RANGE - The value stored in "pValue" is not in the
* valid ranges.
*
******************************************************************************/
cy_en_ble_gatt_err_code_t Cy_BLE_ESS_HandleRangeCheck(cy_stc_ble_conn_handle_t connHandle,
cy_en_ble_ess_char_index_t charIndex,
uint8_t charInstance,
uint16_t length,
uint8_t type,
const uint8_t pValue[])
{
uint8_t locState = 0u;
uint8_t skipSignCheck = 0u;
uint8_t tmpBuff[8u] = {0u};
uint32_t maxLimit = 0u;
uint32_t minLimit = 0u;
uint32_t operand = 0u;
uint32_t u32Sign = 0u;
cy_en_ble_gatt_err_code_t apiResult = CY_BLE_GATT_ERR_UNLIKELY_ERROR;
if(Cy_BLE_ESSS_GetCharacteristicDescriptor(connHandle, charIndex, charInstance, CY_BLE_ESS_VRD, length, tmpBuff) ==
CY_BLE_SUCCESS)
{
switch(length)
{
case CY_BLE_ESS_2BYTES_LENGTH:
operand = (uint32_t)(pValue[0u]);
minLimit = (uint32_t)(tmpBuff[0u]);
maxLimit = (uint32_t)(tmpBuff[1u]);
u32Sign = CY_BLE_ESS_U8_SIGN_BIT;
break;
case CY_BLE_ESS_4BYTES_LENGTH:
operand = (uint32_t)Cy_BLE_ESS_Get16ByPtr(&pValue[0u]);
minLimit = (uint32_t)Cy_BLE_ESS_Get16ByPtr(&tmpBuff[0u]);
maxLimit = (uint32_t)Cy_BLE_ESS_Get16ByPtr(&tmpBuff[2u]);
u32Sign = CY_BLE_ESS_U16_SIGN_BIT;
break;
case CY_BLE_ESS_6BYTES_LENGTH:
operand = (uint32_t)Cy_BLE_ESS_Get24ByPtr(&pValue[0u]);
minLimit = (uint32_t)Cy_BLE_ESS_Get24ByPtr(&tmpBuff[0u]);
maxLimit = (uint32_t)Cy_BLE_ESS_Get24ByPtr(&tmpBuff[3u]);
u32Sign = CY_BLE_ESS_U24_SIGN_BIT;
break;
case CY_BLE_ESS_8BYTES_LENGTH:
operand = (uint32_t)Cy_BLE_ESS_Get32ByPtr(&pValue[0u]);
minLimit = (uint32_t)Cy_BLE_ESS_Get32ByPtr(&tmpBuff[0u]);
maxLimit = (uint32_t)Cy_BLE_ESS_Get32ByPtr(&tmpBuff[4u]);
break;
default:
skipSignCheck = 1u;
break;
}
if(skipSignCheck == 0u)
{
if(type != CY_BLE_ESS_UNSIGNED_TYPE)
{
locState = CY_BLE_ESS_IS_NEGATIVE(minLimit, u32Sign) << 1u;
locState = (locState | CY_BLE_ESS_IS_NEGATIVE(maxLimit, u32Sign)) << 1u;
locState |= CY_BLE_ESS_IS_NEGATIVE(operand, u32Sign);
}
else
{
locState = 0u;
}
}
/* Compare the operand to the limits with respect to the state
* and return the results.
*/
apiResult = Cy_BLE_ESSS_CheckIfInRange(minLimit, maxLimit, operand, locState);
}
return(apiResult);
}
/******************************************************************************
* Function Name: Cy_BLE_ESSS_WriteEventHandler
***************************************************************************//**
*
* Handles a Write Request event for Environmental Sensing service.
*
* \param eventParam: The pointer to the data structure specified by an event.
*
* \return
* A return value of type cy_en_ble_gatt_err_code_t.
* * CY_BLE_GATT_ERR_NONE - Write is successful.
* * CY_BLE_GATT_ERR_REQUEST_NOT_SUPPORTED - Request is not supported.
* * 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.
* * CY_BLE_GATT_ERR_INVALID_OFFSET - the offset value is invalid.
* * CY_BLE_GATT_ERR_UNLIKELY_ERROR - Some other error occurred.
* * CY_BLE_GATT_ERR_CONDITION_NOT_SUPPORTED - The condition in ES Trigger Settings
* descriptor is not supported.
* * CY_BLE_GATT_ERR_WRITE_REQ_REJECTED - A Write Request was rejected.
*
******************************************************************************/
static cy_en_ble_gatt_err_code_t Cy_BLE_ESSS_WriteEventHandler(cy_stc_ble_gatts_write_cmd_req_param_t *eventParam)
{
uint32_t event = (uint32_t)CY_BLE_EVT_ESSS_DESCR_WRITE;
uint32_t foundItem = 0u;
uint8_t i;
uint8_t j;
cy_stc_ble_esss_char_t *essCharInfoPtr;
cy_stc_ble_ess_descr_value_t wrDescrReqParam = { .connHandle = eventParam->connHandle };
cy_ble_gatt_db_attr_handle_t tmpHandle;
cy_en_ble_gatt_err_code_t gattErr = CY_BLE_GATT_ERR_NONE;
tmpHandle = eventParam->handleValPair.attrHandle;
if(Cy_BLE_ESS_ApplCallback != NULL)
{
wrDescrReqParam.descrIndex = CY_BLE_ESS_DESCR_COUNT;
/* Go through all possible service characteristics.
* Exit the handler in following conditions:
* 1) No more characteristic left;
* 2) Characteristic or descriptor was successfully written;
* 3) Error occurred while writing characteristic or descriptor.
*/
for(i = 0u; ((i < (uint8_t)CY_BLE_ESS_CHAR_COUNT) && (foundItem == 0u) && (gattErr == CY_BLE_GATT_ERR_NONE)); i++)
{
/* Check whether characteristic is enabled. If the pointer to the characteristic
* is not "NULL", there is at least one instance of the characteristic in
* the ES service.
*/
if(cy_ble_esssConfigPtr->attrInfo->charInfoAddr[i].charInfoPtr != NULL)
{
for(j = 0u; j < cy_ble_esssConfigPtr->charInstances[i]; j++)
{
essCharInfoPtr = (cy_stc_ble_esss_char_t *) &cy_ble_esssConfigPtr->attrInfo->charInfoAddr[i].charInfoPtr[j];
/* Client Characteristic Configuration descriptor Write Request handling */
if(tmpHandle == essCharInfoPtr->descrHandle[CY_BLE_ESS_CCCD])
{
/* Verify that optional notification property is enabled for characteristic */
if(CY_BLE_IS_NOTIFICATION_SUPPORTED(essCharInfoPtr->charHandle))
{
if(CY_BLE_IS_NOTIFICATION_ENABLED_IN_PTR(eventParam->handleValPair.value.val))
{
event = (uint32_t)CY_BLE_EVT_ESSS_NOTIFICATION_ENABLED;
}
else
{
event = (uint32_t)CY_BLE_EVT_ESSS_NOTIFICATION_DISABLED;
}
/* Value is NULL for descriptors */
wrDescrReqParam.value = NULL;
wrDescrReqParam.descrIndex = CY_BLE_ESS_CCCD;
foundItem = CY_BLE_ESS_DESCRIPTOR_ITEM;
}
else if(CY_BLE_IS_INDICATION_SUPPORTED(essCharInfoPtr->charHandle))
{
if(CY_BLE_IS_INDICATION_ENABLED_IN_PTR(eventParam->handleValPair.value.val))
{
event = (uint32_t)CY_BLE_EVT_ESSS_INDICATION_ENABLED;
}
else
{
event = (uint32_t)CY_BLE_EVT_ESSS_INDICATION_DISABLED;
}
/* Value is NULL for descriptors */
wrDescrReqParam.value = NULL;
wrDescrReqParam.descrIndex = CY_BLE_ESS_CCCD;
foundItem = CY_BLE_ESS_DESCRIPTOR_ITEM;
}
else
{
gattErr = (cy_en_ble_gatt_err_code_t)CY_BLE_GATT_ERR_WRITE_REQ_REJECTED;
}
}
else if(tmpHandle == essCharInfoPtr->descrHandle[CY_BLE_ESS_ES_TRIGGER_SETTINGS_DESCR1])
{
wrDescrReqParam.descrIndex = CY_BLE_ESS_ES_TRIGGER_SETTINGS_DESCR1;
foundItem = CY_BLE_ESS_DESCRIPTOR_ITEM;
}
else if(tmpHandle == essCharInfoPtr->descrHandle[CY_BLE_ESS_ES_TRIGGER_SETTINGS_DESCR2])
{
wrDescrReqParam.descrIndex = CY_BLE_ESS_ES_TRIGGER_SETTINGS_DESCR2;
foundItem = CY_BLE_ESS_DESCRIPTOR_ITEM;
}
else if(tmpHandle == essCharInfoPtr->descrHandle[CY_BLE_ESS_ES_TRIGGER_SETTINGS_DESCR3])
{
wrDescrReqParam.descrIndex = CY_BLE_ESS_ES_TRIGGER_SETTINGS_DESCR3;
foundItem = CY_BLE_ESS_DESCRIPTOR_ITEM;
}
else if(tmpHandle == essCharInfoPtr->descrHandle[CY_BLE_ESS_ES_CONFIG_DESCR])
{
wrDescrReqParam.descrIndex = CY_BLE_ESS_ES_CONFIG_DESCR;
foundItem = CY_BLE_ESS_DESCRIPTOR_ITEM;
}
else if(tmpHandle == essCharInfoPtr->descrHandle[CY_BLE_ESS_CHAR_USER_DESCRIPTION_DESCR])
{
wrDescrReqParam.descrIndex = CY_BLE_ESS_CHAR_USER_DESCRIPTION_DESCR;
foundItem = CY_BLE_ESS_DESCRIPTOR_ITEM;
}
else
{
/* No handle match was found */
}
/* Verify if requested handle was found and successfully handled */
if((gattErr == CY_BLE_GATT_ERR_NONE) && (0u != foundItem))
{
switch(wrDescrReqParam.descrIndex)
{
case CY_BLE_ESS_ES_TRIGGER_SETTINGS_DESCR1:
case CY_BLE_ESS_ES_TRIGGER_SETTINGS_DESCR2:
case CY_BLE_ESS_ES_TRIGGER_SETTINGS_DESCR3:
if(eventParam->handleValPair.value.val[0u] > CY_BLE_ESS_TRIG_WHILE_EQUAL_NOT_TO)
{
/* Trigger condition is not supported */
gattErr = (cy_en_ble_gatt_err_code_t)CY_BLE_GATT_ERR_CONDITION_NOT_SUPPORTED;
}
/* If Valid Range descriptor for the characteristic is present, then
* check the characteristic value range. The ranges should be properly
* set in the server or otherwise the server will constantly return an
* "Out of range" error.
*/
else if((CY_BLE_GATT_INVALID_ATTR_HANDLE_VALUE !=
essCharInfoPtr->descrHandle[CY_BLE_ESS_VRD]) &&
(eventParam->handleValPair.value.len > 1u) &&
(eventParam->handleValPair.value.val[0u] >= CY_BLE_ESS_TRIG_WHILE_LESS_THAN))
{
/* The following "switch" groups characteristics are based on their value
* type.
*/
switch((cy_en_ble_ess_char_index_t)i)
{
/* uint8_t */
case CY_BLE_ESS_BAROMETRIC_PRESSURE_TREND:
case CY_BLE_ESS_GUST_FACTOR:
case CY_BLE_ESS_UV_INDEX:
gattErr = Cy_BLE_ESS_HandleRangeCheck(eventParam->connHandle,
(cy_en_ble_ess_char_index_t)i, j,
CY_BLE_ESS_2BYTES_LENGTH,
CY_BLE_ESS_UNSIGNED_TYPE,
&eventParam->handleValPair.value.val[1u]);
break;
/* uint16_t */
case CY_BLE_ESS_APPARENT_WIND_DIR:
case CY_BLE_ESS_APPARENT_WIND_SPEED:
case CY_BLE_ESS_HUMIDITY:
case CY_BLE_ESS_IRRADIANCE:
case CY_BLE_ESS_MAGNETIC_DECLINATION:
case CY_BLE_ESS_RAINFALL:
case CY_BLE_ESS_TRUE_WIND_DIR:
case CY_BLE_ESS_TRUE_WIND_SPEED:
gattErr = Cy_BLE_ESS_HandleRangeCheck(eventParam->connHandle,
(cy_en_ble_ess_char_index_t)i, j,
CY_BLE_ESS_4BYTES_LENGTH,
CY_BLE_ESS_UNSIGNED_TYPE,
&eventParam->handleValPair.value.val[1u]);
break;
/* uint24 */
case CY_BLE_ESS_POLLEN_CONCENTRATION:
gattErr = Cy_BLE_ESS_HandleRangeCheck(eventParam->connHandle,
(cy_en_ble_ess_char_index_t)i, j,
CY_BLE_ESS_6BYTES_LENGTH,
CY_BLE_ESS_UNSIGNED_TYPE,
&eventParam->handleValPair.value.val[1u]);
break;
/* uint32_t */
case CY_BLE_ESS_PRESSURE:
gattErr = Cy_BLE_ESS_HandleRangeCheck(eventParam->connHandle,
(cy_en_ble_ess_char_index_t)i, j,
CY_BLE_ESS_8BYTES_LENGTH,
CY_BLE_ESS_UNSIGNED_TYPE,
&eventParam->handleValPair.value.val[1u]);
break;
/* int8_t */
case CY_BLE_ESS_DEW_POINT:
case CY_BLE_ESS_HEAT_INDEX:
case CY_BLE_ESS_WIND_CHILL:
gattErr = Cy_BLE_ESS_HandleRangeCheck(eventParam->connHandle,
(cy_en_ble_ess_char_index_t)i, j,
CY_BLE_ESS_2BYTES_LENGTH,
CY_BLE_ESS_SIGNED_TYPE,
&eventParam->handleValPair.value.val[1u]);
break;
/* int16_t */
case CY_BLE_ESS_TEMPERATURE:
gattErr = Cy_BLE_ESS_HandleRangeCheck(eventParam->connHandle,
(cy_en_ble_ess_char_index_t)i, j,
CY_BLE_ESS_4BYTES_LENGTH,
CY_BLE_ESS_SIGNED_TYPE,
&eventParam->handleValPair.value.val[1u]);
break;
/* int24 */
case CY_BLE_ESS_ELEVATION:
gattErr = Cy_BLE_ESS_HandleRangeCheck(eventParam->connHandle,
(cy_en_ble_ess_char_index_t)i, j,
CY_BLE_ESS_6BYTES_LENGTH,
CY_BLE_ESS_SIGNED_TYPE,
&eventParam->handleValPair.value.val[1u]);
break;
case CY_BLE_ESS_MAGNETIC_FLUX_DENSITY_2D:
/* No validation required */
break;
case CY_BLE_ESS_MAGNETIC_FLUX_DENSITY_3D:
/* No validation required */
break;
default:
/* Invalid characteristic */
gattErr = CY_BLE_GATT_ERR_UNLIKELY_ERROR;
break;
}
}
else /* No error */
{
if(CY_BLE_GATT_DB_ATTR_GET_ATTR_GEN_MAX_LEN(
essCharInfoPtr->descrHandle[wrDescrReqParam.descrIndex]) >=
eventParam->handleValPair.value.len)
{
/* Set new length of the descriptor value */
CY_BLE_GATT_DB_ATTR_SET_ATTR_GEN_LEN(
essCharInfoPtr->descrHandle[wrDescrReqParam.descrIndex],
eventParam->handleValPair.value.len);
}
else
{
gattErr = CY_BLE_GATT_ERR_INVALID_ATTRIBUTE_LEN;
}
}
break;
case CY_BLE_ESS_ES_CONFIG_DESCR:
if(eventParam->handleValPair.value.val[0u] > CY_BLE_ESS_CONF_BOOLEAN_OR)
{
/* Trigger Logic value is not supported */
gattErr = (cy_en_ble_gatt_err_code_t)CY_BLE_GATT_ERR_WRITE_REQ_REJECTED;
}
break;
case CY_BLE_ESS_CHAR_USER_DESCRIPTION_DESCR:
/* The ESS spec. states are the following: "The Server may also choose to reject
* a Write Request to the Characteristic User Description if it determines that
* the contents of the new value are unsuitable, such as a string containing
* characters in a language that the implementation does not support."
* This validation is omitted in the current version of the service.
*/
if(CY_BLE_GATT_DB_ATTR_GET_ATTR_GEN_MAX_LEN(
essCharInfoPtr->descrHandle[CY_BLE_ESS_CHAR_USER_DESCRIPTION_DESCR]) >=
eventParam->handleValPair.value.len)
{
/* Set new length of the descriptor value */
CY_BLE_GATT_DB_ATTR_SET_ATTR_GEN_LEN(
essCharInfoPtr->descrHandle[CY_BLE_ESS_CHAR_USER_DESCRIPTION_DESCR],
eventParam->handleValPair.value.len);
}
else
{
gattErr = CY_BLE_GATT_ERR_INVALID_ATTRIBUTE_LEN;
}
break;
default:
break;
}
/* Check is there no error conditions happen yet */
if(gattErr == CY_BLE_GATT_ERR_NONE)
{
gattErr = Cy_BLE_GATT_DbCheckPermission(eventParam->handleValPair.attrHandle,
&eventParam->connHandle, CY_BLE_GATT_DB_WRITE |
CY_BLE_GATT_DB_PEER_INITIATED);
if(gattErr == CY_BLE_GATT_ERR_NONE)
{
/* Fill data and pass it to user */
wrDescrReqParam.charIndex = (cy_en_ble_ess_char_index_t)i;
wrDescrReqParam.charInstance = j;
wrDescrReqParam.gattErrorCode = CY_BLE_GATT_ERR_NONE;
if(wrDescrReqParam.descrIndex != CY_BLE_ESS_CCCD)
{
/* Check whether descriptor index is not CCCD index as "event" and
* "wrDescrReqParam.value" parameters for CCCD were handled
* above.
*/
wrDescrReqParam.value = &eventParam->handleValPair.value;
}
Cy_BLE_ESS_ApplCallback(event, &wrDescrReqParam);
if(wrDescrReqParam.gattErrorCode == CY_BLE_GATT_ERR_NONE)
{
/* Write value to GATT database */
cy_stc_ble_gatts_db_attr_val_info_t dbAttrValInfo;
dbAttrValInfo.handleValuePair = eventParam->handleValPair;
dbAttrValInfo.connHandle = eventParam->connHandle;
dbAttrValInfo.offset = 0u;
dbAttrValInfo.flags = CY_BLE_GATT_DB_PEER_INITIATED;
gattErr = Cy_BLE_GATTS_WriteAttributeValueCCCD(&dbAttrValInfo);
}
}
}
cy_ble_eventHandlerFlag &= (uint8_t) ~CY_BLE_CALLBACK;
break;
}
}
}
}
}
if(CY_BLE_GATT_ERR_NONE != gattErr)
{
/* Indicate that request was handled */
cy_ble_eventHandlerFlag &= (uint8_t) ~CY_BLE_CALLBACK;
}
return(gattErr);
}
/******************************************************************************
* Function Name: Cy_BLE_ESSS_PrepareWriteRequestEventHandler
***************************************************************************//**
*
* Handles the Write Request event for the Environmental Sensing service.
*
* \param eventParam: The pointer to the data that received with a prepare write
* request event for the Environmental Sensing service.
*
******************************************************************************/
static void Cy_BLE_ESSS_PrepareWriteRequestEventHandler(const cy_stc_ble_gatts_prep_write_req_param_t *eventParam)
{
uint32_t i;
uint32_t j;
uint8_t exitLoop = 0u;
cy_stc_ble_esss_char_t *essCharInfoPtr;
if(Cy_BLE_ESS_ApplCallback != NULL)
{
for(i = 0u; ((i < ((uint8_t)CY_BLE_ESS_CHAR_COUNT)) && (exitLoop == 0u)); i++)
{
/* Check whether characteristic is enabled. If the pointer to the characteristic
* is not "NULL", there is at least one instance of the characteristic in
* the ES service.
*/
if(cy_ble_esssConfigPtr->attrInfo->charInfoAddr[i].charInfoPtr != NULL)
{
for(j = 0u; ((j < cy_ble_esssConfigPtr->charInstances[i]) && (exitLoop == 0u)); j++)
{
essCharInfoPtr = (cy_stc_ble_esss_char_t *) &cy_ble_esssConfigPtr->attrInfo->charInfoAddr[i].charInfoPtr[j];
/* Need to check following conditions: 1) if requested handle is the handle of
* Characteristic User Description Descriptor; 2) and if there is no active
* requests; 3) or there is an active request but the requested handle is
* different handle. The third condition means that previous request resulted
* with an error so need to handle that.
*/
if(eventParam->baseAddr[eventParam->currentPrepWriteReqCount - 1u].handleValuePair.attrHandle ==
essCharInfoPtr->descrHandle[CY_BLE_ESS_CHAR_USER_DESCRIPTION_DESCR])
{
if(cy_ble_esssReqHandle == CY_BLE_GATT_INVALID_ATTR_HANDLE_VALUE)
{
/* Send Prepare Write Response which identifies acknowledgment for
* long characteristic value write.
*/
cy_ble_esssReqHandle =
eventParam->baseAddr[eventParam->currentPrepWriteReqCount - 1u].handleValuePair.attrHandle;
}
/* Indicate that request was handled */
cy_ble_eventHandlerFlag &= (uint8_t) ~CY_BLE_CALLBACK;
/* Set the flag to exit the loop */
exitLoop = 1u;
}
}
}
}
}
}
/******************************************************************************
* Function Name: Cy_BLE_ESSS_ExecuteWriteRequestEventHandler
***************************************************************************//**
*
* Handles the Execute Write Request event for the Environmental Sensing service.
*
* \param eventParam: The pointer to the data that came with a Write Request for the
* Environmental Sensing service.
*
******************************************************************************/
static void Cy_BLE_ESSS_ExecuteWriteRequestEventHandler(cy_stc_ble_gatts_exec_write_req_t *eventParam)
{
uint8_t i;
uint8_t j;
uint8_t locCount;
uint8_t exitLoop = 0u;
uint16_t locLength = 0u;
cy_stc_ble_gatt_value_t locDescrValue;
cy_stc_ble_ess_descr_value_t wrDescrReqParam = { .connHandle = eventParam->connHandle };
if(Cy_BLE_ESS_ApplCallback != NULL)
{
for(i = 0u; ((i < ((uint8_t)CY_BLE_ESS_CHAR_COUNT)) && (exitLoop == 0u)); i++)
{
/* Check whether characteristic is enabled. If the pointer to the characteristic
* is not "NULL", there is at least one instance of the characteristic in
* the ES service.
*/
if(cy_ble_esssConfigPtr->attrInfo->charInfoAddr[i].charInfoPtr != NULL)
{
for(j = 0u; ((j < cy_ble_esssConfigPtr->charInstances[i]) && (exitLoop == 0u)); j++)
{
if((eventParam->baseAddr[0u].handleValuePair.attrHandle ==
cy_ble_esssConfigPtr->attrInfo->charInfoAddr[i].charInfoPtr[j].
descrHandle[CY_BLE_ESS_CHAR_USER_DESCRIPTION_DESCR]) &&
(cy_ble_esssReqHandle == eventParam->baseAddr[0u].handleValuePair.attrHandle))
{
/* Check the execWriteFlag before execute or cancel write long operation */
if(eventParam->execWriteFlag == CY_BLE_GATT_EXECUTE_WRITE_EXEC_FLAG)
{
for(locCount = 0u; locCount < eventParam->prepWriteReqCount; locCount++)
{
locLength += eventParam->baseAddr[locCount].handleValuePair.value.len;
}
if(CY_BLE_GATT_DB_ATTR_GET_ATTR_GEN_MAX_LEN(cy_ble_esssReqHandle) >= locLength)
{
locDescrValue = eventParam->baseAddr[0u].handleValuePair.value;
/* Fill data and pass it to user */
wrDescrReqParam.charIndex = (cy_en_ble_ess_char_index_t)i;
wrDescrReqParam.charInstance = j;
wrDescrReqParam.gattErrorCode = CY_BLE_GATT_ERR_NONE;
wrDescrReqParam.value = &locDescrValue;
wrDescrReqParam.value->len = locLength;
wrDescrReqParam.descrIndex = CY_BLE_ESS_CHAR_USER_DESCRIPTION_DESCR;
Cy_BLE_ESS_ApplCallback((uint32_t)CY_BLE_EVT_ESSS_DESCR_WRITE, &wrDescrReqParam);
if(wrDescrReqParam.gattErrorCode == CY_BLE_GATT_ERR_NONE)
{
CY_BLE_GATT_DB_ATTR_SET_ATTR_GEN_LEN(cy_ble_esssReqHandle, locLength);
}
}
else
{
wrDescrReqParam.gattErrorCode = CY_BLE_GATT_ERR_INVALID_ATTRIBUTE_LEN;
}
/* Pass user error code to Stack */
eventParam->gattErrorCode = (uint8_t)wrDescrReqParam.gattErrorCode;
}
/* Set the flag to exit the loop */
exitLoop = 1u;
/* Indicate that request was handled */
cy_ble_eventHandlerFlag &= (uint8_t) ~CY_BLE_CALLBACK;
/* Clear requested handle */
cy_ble_esssReqHandle = CY_BLE_GATT_INVALID_ATTR_HANDLE_VALUE;
}
}
}
}
}
}
/******************************************************************************
* Function Name: Cy_BLE_ESSS_ConfirmationEventHandler
***************************************************************************//**
*
* Handles a Value Confirmation request event from the BLE Stack.
*
* eventParam - The pointer to a structure of type cy_stc_ble_conn_handle_t.
*
******************************************************************************/
static void Cy_BLE_ESSS_ConfirmationEventHandler(const cy_stc_ble_conn_handle_t *eventParam)
{
if((Cy_BLE_ESS_ApplCallback != NULL) && (CY_BLE_GATT_INVALID_ATTR_HANDLE_VALUE != cy_ble_esssReqHandle))
{
/* Only descriptor Value Change Characteristic has Indication property.
* Check whether the requested handle is the handle of descriptor Value Change
* handle.
*/
if(cy_ble_esssReqHandle ==
cy_ble_esssConfigPtr->attrInfo->charInfoAddr[CY_BLE_ESS_DESCRIPTOR_VALUE_CHANGED].charInfoPtr[0u].charHandle)
{
/* Fill in event data and inform application about
* successfully confirmed indication. */
cy_stc_ble_ess_char_value_t locCharValue;
locCharValue.connHandle = *eventParam;
locCharValue.charIndex = CY_BLE_ESS_DESCRIPTOR_VALUE_CHANGED;
locCharValue.value = NULL;
cy_ble_eventHandlerFlag &= (uint8_t) ~CY_BLE_CALLBACK;
cy_ble_esssReqHandle = CY_BLE_GATT_INVALID_ATTR_HANDLE_VALUE;
Cy_BLE_ESS_ApplCallback((uint32_t)CY_BLE_EVT_ESSS_INDICATION_CONFIRMED, &locCharValue);
}
}
}
/******************************************************************************
* Function Name: Cy_BLE_ESSS_SetChangeIndex
***************************************************************************//**