-
Notifications
You must be signed in to change notification settings - Fork 0
/
ble_client.c
1162 lines (1079 loc) · 44.2 KB
/
ble_client.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 Name: ble_client.c
*
* Description: This is the source code for the FreeRTOS: BLE Throughput Client
* Example for ModusToolbox.
*
* Related Document: See README.md
*
*
*******************************************************************************
* Copyright 2021-2024, Cypress Semiconductor Corporation (an Infineon company) or
* an affiliate of Cypress Semiconductor Corporation. All rights reserved.
*
* This software, including source code, documentation and related
* materials ("Software") is owned by Cypress Semiconductor Corporation
* or one of its affiliates ("Cypress") and is protected by and subject to
* worldwide patent protection (United States and foreign),
* United States copyright laws and international treaty provisions.
* Therefore, you may use this Software only as provided in the license
* agreement accompanying the software package from which you
* obtained this Software ("EULA").
* If no EULA applies, Cypress hereby grants you a personal, non-exclusive,
* non-transferable license to copy, modify, and compile the Software
* source code solely for use in connection with Cypress's
* integrated circuit products. Any reproduction, modification, translation,
* compilation, or representation of this Software except as specified
* above is prohibited without the express written permission of Cypress.
*
* Disclaimer: THIS SOFTWARE IS PROVIDED AS-IS, WITH NO WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, NONINFRINGEMENT, IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. Cypress
* reserves the right to make changes to the Software without notice. Cypress
* does not assume any liability arising out of the application or use of the
* Software or any product or circuit described in the Software. Cypress does
* not authorize its products for use in any products where a malfunction or
* failure of the Cypress product may reasonably be expected to result in
* significant property damage, injury or death ("High Risk Product"). By
* including Cypress's product in a High Risk Product, the manufacturer
* of such system or application assumes all risk of such use and in doing
* so agrees to indemnify Cypress against all liability.
******************************************************************************/
/*******************************************************************************
* Header Files
*******************************************************************************/
#include "cyhal.h"
#include <FreeRTOS.h>
#include <task.h>
#include "wiced_memory.h"
#include "cycfg_gap.h"
#include "app_bt_utils.h"
#include "wiced_bt_stack.h"
#include "ble_client.h"
#include "wiced_bt_l2c.h"
/*******************************************************************************
* Macros
*******************************************************************************/
#define GET_THROUGHPUT_TIMER_PERIOD (9999u)
#define APP_MILLISEC_TIMER_PERIOD (9u)
#define WRITE_DATA_SIZE (244)
#define TASK_NOTIFY_1MS_TIMER (1u)
#define TASK_NOTIFY_NO_GATT_CONGESTION (2u)
/*******************************************************************************
* Variable Definitions
*******************************************************************************/
/* Variables to hold GATT notification bytes sent and GATT Write bytes received
* successfully*/
static unsigned long gatt_notif_rx_bytes = 0;
static unsigned long gatt_write_tx_bytes = 0;
/*Variable that stores the data which will be sent as GATT write alternatively*/
uint8_t write_data_seq1[WRITE_DATA_SIZE];
uint8_t write_data_seq2[WRITE_DATA_SIZE];
/* Variable to store packet size decided based on MTU exchanged */
static uint16_t packet_size = 0;
/* PWM object used for Advertising Led*/
static cyhal_pwm_t scan_led_pwm;
/* Variable to store ble advertising state*/
static app_bt_scan_conn_mode_t app_bt_scan_conn_state = APP_BT_SCAN_OFF_CONN_OFF;
static bool tput_service_found = false;
/* Variable to store connection state information*/
static conn_state_info_t conn_state_info;
/* Enable or Disable notification from server */
static bool enable_cccd = true;
/* Flag to enable or disable GATT write */
static bool gatt_write_tx = false;
/* Flag to used to Scan only for first button press */
static bool scan_flag = true;
/* Variable to switch between different data transfer modes */
static tput_mode_t mode_flag = GATT_NOTIFANDWRITE;
static const uint8_t tput_service_uuid[LEN_UUID_128] = TPUT_SERVICE_UUID;
static uint16_t tput_service_handle = 0;
static wiced_bt_gatt_status_t status;
static wiced_bt_gatt_write_hdr_t tput_write_cmd = {0};
/* variables for app buffer + handling */
uint8_t *tput_buffer_ptr;
uint8_t data_flag = 0;
uint8_t value_initialize = 243;
/* For 1 second timer*/
static cyhal_timer_t get_throughput_timer_obj,app_millisec_timer_obj;
const cyhal_timer_cfg_t get_throughput_timer_cfg =
{
.compare_value = 0, /* Timer compare value, not used */
.period = GET_THROUGHPUT_TIMER_PERIOD,/* Defines the timer period */
.direction = CYHAL_TIMER_DIR_UP, /* Timer counts up */
.is_compare = false, /* Don't use compare mode */
.is_continuous = true, /* Run timer indefinitely */
.value = 0 /* Initial value of counter */
};
/* For 1 millisecond timer*/
const cyhal_timer_cfg_t app_millisec_timer_cfg =
{
.compare_value = 0, /* Timer compare value, not used */
.period = APP_MILLISEC_TIMER_PERIOD, /* Defines the timer period */
.direction = CYHAL_TIMER_DIR_UP, /* Timer counts up */
.is_compare = false, /* Don't use compare mode */
.is_continuous = true, /* Run timer indefinitely */
.value = 0 /* Initial value of counter */
};
/*******************************************************************************
* Function Prototypes
*******************************************************************************/
static void tput_scan_led_update (void);
static void tput_ble_app_init (void);
static void tput_button_interrupt_handler (void *handler_arg,
cyhal_gpio_event_t event);
static uint16_t tput_get_write_cmd_pkt_size (uint16_t att_mtu_size);
static wiced_bt_gatt_status_t tput_enable_disable_gatt_notification(bool notify);
static void tput_scan_result_cback (wiced_bt_ble_scan_results_t *p_scan_result,
uint8_t *p_adv_data);
void tput_app_throughput_timer_callb (void *callback_arg,
cyhal_timer_event_t event);
void tput_app_millisec_timer_callb (void *callback_arg,
cyhal_timer_event_t event);
/* GATT Event Callback Functions */
static wiced_bt_gatt_status_t ble_app_connect_callback
(wiced_bt_gatt_connection_status_t *p_conn_status);
static wiced_bt_gatt_status_t ble_app_gatt_event_handler(wiced_bt_gatt_evt_t event,
wiced_bt_gatt_event_data_t *p_event_data);
static cyhal_gpio_callback_data_t cyhal_gpio_callback_data =
{
.callback = tput_button_interrupt_handler,
.pin = CYBSP_USER_BTN,
};
/******************************************************************************
* Function Definitions
******************************************************************************/
/*******************************************************************************
* Function Name: app_bt_free_buffer()
********************************************************************************
* Summary:
* This function frees up the memory buffer
*
* Parameters:
* uint8_t *p_data: Pointer to the buffer to be free
*
* Return:
* None
*
*******************************************************************************/
void app_bt_free_buffer(uint8_t *p_buf)
{
vPortFree(p_buf);
}
/*******************************************************************************
* Function Name: app_bt_alloc_buffer()
********************************************************************************
* Summary:
* This function allocates a memory buffer
*
* Parameters:
* int len : Length to allocate
*
* Return:
* None
*
*******************************************************************************/
void* app_bt_alloc_buffer(int len)
{
return pvPortMalloc(len);
}
/*******************************************************************************
* Function Name: app_bt_management_callback()
********************************************************************************
* Summary:
* This is a Bluetooth stack event handler function to receive management events
* from the BLE stack and process as per the application.
*
* Parameters:
* wiced_bt_management_evt_t : BLE event code of one byte length
* wiced_bt_management_evt_data_t : Pointer to BLE management event structures
*
* Return:
* wiced_result_t: Error code from WICED_RESULT_LIST or BT_RESULT_LIST
*
*******************************************************************************/
wiced_result_t app_bt_management_callback(wiced_bt_management_evt_t event,
wiced_bt_management_evt_data_t *p_event_data)
{
wiced_result_t status = WICED_BT_SUCCESS;
wiced_bt_device_address_t bda = {0};
wiced_bt_ble_scan_type_t p_scan_type ;
switch (event)
{
case BTM_ENABLED_EVT:
/* Bluetooth Controller and Host Stack Enabled */
if(WICED_BT_SUCCESS == p_event_data->enabled.status)
{
/* Bluetooth is enabled */
wiced_bt_dev_read_local_addr(bda);
printf("Local Bluetooth Address: ");
print_bd_address(bda);
/* Perform application-specific initialization */
tput_ble_app_init();
}
else
{
printf("Bluetooth Disabled \n");
}
break;
case BTM_BLE_SCAN_STATE_CHANGED_EVT:
/* Scan State Changed */
p_scan_type = p_event_data->ble_scan_state_changed;
if(BTM_BLE_SCAN_TYPE_NONE == p_scan_type)
{
/* Scan Stopped */
printf("Scanning stopped\n");
/* Check connection status after scanning stops */
if (conn_state_info.conn_id == 0)
{
app_bt_scan_conn_state = APP_BT_SCAN_OFF_CONN_OFF;
}
else
{
app_bt_scan_conn_state = APP_BT_SCAN_OFF_CONN_ON;
}
}
else
{
/* Scan Started */
printf("Scanning.....\n");
app_bt_scan_conn_state = APP_BT_SCAN_ON_CONN_OFF;
}
/* Update Scan LED to reflect the updated state */
tput_scan_led_update();
break;
case BTM_BLE_PHY_UPDATE_EVT:
conn_state_info.rx_phy = p_event_data->ble_phy_update_event.rx_phy;
conn_state_info.tx_phy = p_event_data->ble_phy_update_event.tx_phy;
printf("Selected RX PHY - %dM\nSelected TX PHY - %dM\nPeer address = ",
conn_state_info.rx_phy,conn_state_info.tx_phy);
break;
case BTM_BLE_CONNECTION_PARAM_UPDATE:
/* Connection parameters updated */
if(WICED_BT_SUCCESS == p_event_data->ble_connection_param_update.status)
{
conn_state_info.conn_interval = (double)
((p_event_data->ble_connection_param_update.conn_interval)
* CONN_INTERVAL_MULTIPLIER);
printf("New connection interval: %f ms\n",
conn_state_info.conn_interval);
}
else
{
printf("Connection parameters update failed: %d\n",
p_event_data->ble_connection_param_update.status);
}
break;
default:
printf("Unhandled Bluetooth Management Event: 0x%x %s\n",
event, get_bt_event_name(event));
break;
}
return status;
}
/*******************************************************************************
* Function Name: tput_ble_app_init()
********************************************************************************
* Summary:
* This function handles application level initialization tasks and is called
* from the BT management callback once the BLE stack enabled event
* (BTM_ENABLED_EVT) is triggered. This function is executed in the
* BTM_ENABLED_EVT management callback.
*
* Parameters:
* None
*
* Return:
* None
*
*******************************************************************************/
static void tput_ble_app_init(void)
{
cy_rslt_t rslt = CY_RSLT_SUCCESS;
wiced_bt_gatt_status_t status = WICED_BT_GATT_SUCCESS;
/* Initialize the PWM used for Scanning LED */
rslt = cyhal_pwm_init(&scan_led_pwm, SCAN_LED_GPIO, NULL);
/* PWM init failed. Stop program execution */
if (CY_RSLT_SUCCESS != rslt)
{
printf("Scan LED PWM Initialization has failed! \n");
CY_ASSERT(0);
}
app_bt_scan_conn_state = APP_BT_SCAN_OFF_CONN_OFF;
tput_scan_led_update();
/*Initialize the data packet to be sent as GATT notification to the peer
device */
for(uint8_t index = 0; index < WRITE_DATA_SIZE; index++)
{
write_data_seq1[index] = index;
}
for(uint8_t index = 0; index < WRITE_DATA_SIZE; index++)
{
write_data_seq2[index] = value_initialize;
value_initialize--;
}
/* 1 second Timer initialization */
rslt = cyhal_timer_init(&get_throughput_timer_obj, NC, NULL);
if (CY_RSLT_SUCCESS != rslt)
{
printf("Get throughput timer init failed !\n");
CY_ASSERT(0);
}
cyhal_timer_configure(&get_throughput_timer_obj, &get_throughput_timer_cfg);
rslt = cyhal_timer_set_frequency(&get_throughput_timer_obj,FREQUENCY);
if (CY_RSLT_SUCCESS != rslt)
{
printf("Get throughput timer set freq failed !\n");
CY_ASSERT(0);
}
cyhal_timer_register_callback(&get_throughput_timer_obj,
tput_app_throughput_timer_callb,
NULL);
cyhal_timer_enable_event(&get_throughput_timer_obj,
CYHAL_TIMER_IRQ_TERMINAL_COUNT,
TIMER_INTERRUPT_PRIORITY,
true);
/* 1 millisecond Timer initialization */
rslt = cyhal_timer_init(&app_millisec_timer_obj, NC, NULL);
if (CY_RSLT_SUCCESS != rslt)
{
printf("Get throughput timer init failed !\n");
CY_ASSERT(0);
}
cyhal_timer_configure(&app_millisec_timer_obj, &app_millisec_timer_cfg);
rslt = cyhal_timer_set_frequency(&app_millisec_timer_obj,FREQUENCY);
if (CY_RSLT_SUCCESS != rslt)
{
printf("Get throughput timer set freq failed !\n");
CY_ASSERT(0);
}
cyhal_timer_register_callback(&app_millisec_timer_obj,
tput_app_millisec_timer_callb,
NULL);
cyhal_timer_enable_event(&app_millisec_timer_obj,
CYHAL_TIMER_IRQ_TERMINAL_COUNT,
TIMER_INTERRUPT_PRIORITY,
true);
/* Initialize GPIO for button interrupt*/
rslt = cyhal_gpio_init(CYBSP_USER_BTN,
CYHAL_GPIO_DIR_INPUT,
CYHAL_GPIO_DRIVE_PULLUP,
CYBSP_BTN_OFF);
/* GPIO init failed. Stop program execution */
if (CY_RSLT_SUCCESS != rslt)
{
printf("Button GPIO init failed! \n");
CY_ASSERT(0);
}
/* Configure GPIO interrupt. */
cyhal_gpio_register_callback(CYBSP_USER_BTN,
&cyhal_gpio_callback_data);
cyhal_gpio_enable_event(CYBSP_USER_BTN,
CYHAL_GPIO_IRQ_FALL,
GPIO_INTERRUPT_PRIORITY,
true);
/* Disable pairing for this application */
wiced_bt_set_pairable_mode(WICED_FALSE, 0);
/* Register with BT stack to receive GATT callback */
status = wiced_bt_gatt_register(ble_app_gatt_event_handler);
printf("GATT event Handler registration status: %s \n",
get_bt_gatt_status_name(status));
/* Initialize GATT Database */
status = wiced_bt_gatt_db_init(gatt_database, gatt_database_len, NULL);
printf("GATT database initialization status: %s \n",
get_bt_gatt_status_name(status));
printf("Press User Button on your kit to start scanning.....\n");
}
/*******************************************************************************
* Function Name: ble_app_gatt_event_handler()
********************************************************************************
* Summary:
* This function handles GATT events from the BT stack.
*
* Parameters:
* wiced_bt_gatt_evt_t event :BLE GATT event code of one byte length
* wiced_bt_gatt_event_data_t *p_event_data:Pointer to BLE GATT event structures
*
* Return:
* wiced_bt_gatt_status_t: See possible status codes in wiced_bt_gatt_status_e
* in wiced_bt_gatt.h
*
*******************************************************************************/
static wiced_bt_gatt_status_t ble_app_gatt_event_handler(wiced_bt_gatt_evt_t event,
wiced_bt_gatt_event_data_t *p_event_data)
{
wiced_bt_gatt_status_t status = WICED_BT_GATT_SUCCESS;
/* Call the appropriate callback function based on the GATT event type, and
* pass the relevant event parameters to the callback function */
switch (event)
{
case GATT_CONNECTION_STATUS_EVT:
status = ble_app_connect_callback(&p_event_data->connection_status);
break;
case GATT_DISCOVERY_RESULT_EVT:
/* Check if it is throughput service uuid */
if (!memcmp(&p_event_data->discovery_result.discovery_data.group_value.service_type.uu.uuid128,
&tput_service_uuid, TPUT_SERVICE_UUID_LEN))
{
/* Update the handle to TPUT service uuid, Throughput service
GATT handle : 0x0009 */
tput_service_handle =
p_event_data->discovery_result.discovery_data.group_value.s_handle;
tput_service_found = true;
}
break;
case GATT_DISCOVERY_CPLT_EVT:
if (tput_service_found)
{
printf("Custom throughput service found\n");
}
else
{
printf("Custom throughput service not found\n");
}
break;
case GATT_OPERATION_CPLT_EVT:
switch (p_event_data->operation_complete.op)
{
case GATTC_OPTYPE_WRITE_WITH_RSP:
/*Check if GATT operation of enable/disable notification is success.*/
if ((p_event_data->operation_complete.response_data.handle ==
(tput_service_handle + GATT_CCCD_HANDLE)) &&
(WICED_BT_GATT_SUCCESS == p_event_data->operation_complete.status))
{
printf("Notifications %s\n",(enable_cccd)?"enabled":"disabled");
/* Start msec timer only for GATT writes */
if (gatt_write_tx)
{
/* Clear GATT Tx packets */
gatt_notif_rx_bytes = 0;
if(CY_RSLT_SUCCESS != cyhal_timer_start(&app_millisec_timer_obj))
{
printf("Get millisec timer start failed !\n");
CY_ASSERT(0);
}
}
}
else if (p_event_data->operation_complete.response_data.handle ==
(tput_service_handle + GATT_CCCD_HANDLE) &&
(WICED_BT_GATT_SUCCESS != p_event_data->operation_complete.status))
{
printf("CCCD update failed. Error: %x\n",
p_event_data->operation_complete.status);
}
break;
case GATTC_OPTYPE_WRITE_NO_RSP:
if ((p_event_data->operation_complete.response_data.handle ==
(tput_service_handle + GATT_WRITE_HANDLE)) &&
(WICED_BT_GATT_SUCCESS == p_event_data->operation_complete.status))
{
gatt_write_tx_bytes += packet_size;
}
break;
case GATTC_OPTYPE_NOTIFICATION:
/* Receive GATT Notifications from server */
gatt_notif_rx_bytes += p_event_data->operation_complete.response_data.att_value.len;
break;
case GATTC_OPTYPE_CONFIG_MTU:
conn_state_info.mtu = p_event_data->operation_complete.response_data.mtu;
printf("Negotiated MTU Size: %d\n", conn_state_info.mtu);
packet_size = tput_get_write_cmd_pkt_size(conn_state_info.mtu);
/* Send GATT service discovery request */
wiced_bt_gatt_discovery_param_t gatt_discovery_setup = {0};
gatt_discovery_setup.s_handle = 1;
gatt_discovery_setup.e_handle = 0xFFFF;
gatt_discovery_setup.uuid.len = TPUT_SERVICE_UUID_LEN;
memcpy(gatt_discovery_setup.uuid.uu.uuid128,
tput_service_uuid,
TPUT_SERVICE_UUID_LEN);
status = wiced_bt_gatt_client_send_discover(conn_state_info.conn_id,
GATT_DISCOVER_SERVICES_BY_UUID,
&gatt_discovery_setup);
if (WICED_BT_GATT_SUCCESS != status)
{
printf("GATT Discovery request failed. Error code: %d,Conn id: %d\n",
status, conn_state_info.conn_id);
}
break;
}
break;
case GATT_CONGESTION_EVT:
if(!p_event_data->congestion.congested)
{
xTaskNotifyGiveIndexed(send_gatt_write_task_handle,
TASK_NOTIFY_NO_GATT_CONGESTION);
}
break;
case GATT_GET_RESPONSE_BUFFER_EVT:
if (p_event_data->buffer_request.len_requested != 0)
{
p_event_data->buffer_request.buffer.p_app_rsp_buffer =
app_bt_alloc_buffer(p_event_data->buffer_request.len_requested);
p_event_data->buffer_request.buffer.p_app_ctxt =
(void *)app_bt_free_buffer;
status = WICED_BT_GATT_SUCCESS;
}
break;
case GATT_APP_BUFFER_TRANSMITTED_EVT:
{
break;
}
default:
status = WICED_BT_GATT_SUCCESS;
break;
}
return status;
}
/*******************************************************************************
* Function Name: tput_button_interrupt_handler
*******************************************************************************
* Summary:
* GPIO interrupt service routine. This function detects button presses and
* changes the data transfer mode.
*
* Parameters:
* void *callback_arg : pointer to the variable passed to the ISR
* cyhal_gpio_event_t event : GPIO event type
*
* Return:
* None
*
******************************************************************************/
static void tput_button_interrupt_handler(void *handler_arg,
cyhal_gpio_event_t event)
{
BaseType_t xHigherPriorityTaskWoken;
xHigherPriorityTaskWoken = pdFALSE;
vTaskNotifyGiveFromISR(ble_button_task_handle, &xHigherPriorityTaskWoken);
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
}
/*******************************************************************************
* Function Name: ble_button_task
*******************************************************************************
* Summary:
* This function handles the button event.
*
* Parameters:
* void *pvParam : Unused
*
* Return:
* None
*
******************************************************************************/
void ble_button_task(void *pvParam)
{
wiced_result_t status = WICED_BT_SUCCESS;
wiced_bt_gatt_status_t gatt_status;
while (1)
{
ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
if (!conn_state_info.conn_id)
{
if (scan_flag)
{
/* Start scan */
status = wiced_bt_ble_scan(BTM_BLE_SCAN_TYPE_HIGH_DUTY, true,
tput_scan_result_cback);
if ((WICED_BT_PENDING != status) && (WICED_BT_BUSY != status))
{
printf("Error: Starting scan failed. Error code: %d\n",status);
/* Switch off the scan LED */
app_bt_scan_conn_state = APP_BT_SCAN_OFF_CONN_OFF;
tput_scan_led_update();
}
}
}
else
{
/* After connection pressing the user button will change the
* throughput modes as follows :
* GATT_Notif_StoC -> GATT_Write_CtoS -> GATT_NotifandWrite -> Roll
* back to GATT_Notif_StoC
*/
/* Stop ongoing GATT writes when enabling/disabling server
* notification ,to prevent command failure due to GATT congestion
* that may occur .The timer will be enabled on GATT event callback
* based on the status of the GATT operation.
*/
if (CY_RSLT_SUCCESS != cyhal_timer_stop(&app_millisec_timer_obj))
{
printf("Get millisec timer stop failed !\n");
CY_ASSERT(0);
}
gatt_write_tx_bytes = 0;
/* Change data transfer modes upon interrupt. Based on the current
* mode,set flags to enable/disable notifications and set/clear GATT
* write flag
*/
mode_flag = (mode_flag == GATT_NOTIFANDWRITE) ?
GATT_NOTIF_STOC :
(tput_mode_t)(mode_flag + 1u);
switch (mode_flag)
{
case GATT_NOTIF_STOC:
enable_cccd = true;
gatt_write_tx = false;
break;
case GATT_WRITE_CTOS:
enable_cccd = false;
gatt_write_tx = true;
break;
case GATT_NOTIFANDWRITE:
enable_cccd = true;
gatt_write_tx = true;
break;
default:
printf("Invalid Data Transfer Mode\n");
break;
}
/*Delay added to avoid the failure of notification enable packet*/
vTaskDelay(2000);
gatt_status = tput_enable_disable_gatt_notification(enable_cccd);
if (WICED_BT_GATT_SUCCESS != gatt_status)
{
printf("Enable/Disable notification failed: %d\n\r",gatt_status);
}
}
}
}
/*******************************************************************************
* Function Name: tput_scan_result_cback()
********************************************************************************
* Summary:
* This function is registered as a callback to handle the scan results.
* When the desired device is found, it will try to establish connection with
* that device.
*
* Parameters:
* wiced_bt_ble_scan_results_t *p_scan_result: Details of the new device found.
* uint8_t *p_adv_data : Advertisement data.
*
* Return:
* None
*
*******************************************************************************/
static void tput_scan_result_cback(wiced_bt_ble_scan_results_t *p_scan_result,
uint8_t *p_adv_data)
{
wiced_result_t status = WICED_BT_SUCCESS;
uint8_t length = 0u;
uint8_t *p_data = NULL;
uint8_t server_device_name[5] = {'T', 'P', 'U', 'T', '\0'};
if (p_scan_result)
{
p_data = wiced_bt_ble_check_advertising_data(p_adv_data,
BTM_BLE_ADVERT_TYPE_NAME_COMPLETE,
&length);
if (p_data != NULL)
{
/* Check if the peer device's name is "TPUT" */
if ((length = strlen((const char *)server_device_name)) &&
(memcmp(p_data, (uint8_t *)server_device_name, length) == 0))
{
printf("Scan completed\n Found peer device with BDA:\n");
print_bd_address(p_scan_result->remote_bd_addr);
scan_flag = false;
/* Device found. Stop scan. */
if ((status = wiced_bt_ble_scan(BTM_BLE_SCAN_TYPE_NONE, true,
tput_scan_result_cback)) != 0)
{
printf("Scan off status %d\n", status);
}
/* Initiate the connection */
if (wiced_bt_gatt_le_connect(p_scan_result->remote_bd_addr,
p_scan_result->ble_addr_type,
BLE_CONN_MODE_HIGH_DUTY,
WICED_TRUE) != WICED_TRUE)
{
printf("wiced_bt_gatt_connect failed\n");
}
else
{
printf("gatt connect request sent\n");
}
}
}
}
}
/*******************************************************************************
* Function Name: ble_app_connect_callback()
********************************************************************************
* Summary:
* This callback function handles connection status changes.
*
* Parameters:
* wiced_bt_gatt_connection_status_t *p_conn_status : Pointer to data that has
* connection details
*
* Return:
* wiced_bt_gatt_status_t: See possible status codes in wiced_bt_gatt_status_e
* in wiced_bt_gatt.h
*
*******************************************************************************/
static wiced_bt_gatt_status_t ble_app_connect_callback(
wiced_bt_gatt_connection_status_t *p_conn_status)
{
wiced_bt_gatt_status_t status = WICED_BT_GATT_ERROR;
if (NULL != p_conn_status)
{
if (p_conn_status->connected)
{
/* Device has connected */
printf("Connected : BDA ");
print_bd_address(p_conn_status->bd_addr);
printf("Connection ID '%d'\n", p_conn_status->conn_id);
/* Store the connection ID and remote BDA*/
conn_state_info.conn_id = p_conn_status->conn_id;
printf("connection_id = %d\n", conn_state_info.conn_id);
memcpy(conn_state_info.remote_addr,
p_conn_status->bd_addr,
BD_ADDR_LEN);
/* Update the scan/conn state */
app_bt_scan_conn_state = APP_BT_SCAN_OFF_CONN_ON;
wiced_bt_l2cap_enable_update_ble_conn_params(conn_state_info.remote_addr,
true);
/* Send MTU exchange request */
status = wiced_bt_gatt_client_configure_mtu(conn_state_info.conn_id,
CY_BT_MTU_SIZE);
if (status != WICED_BT_GATT_SUCCESS)
{
printf("GATT MTU configure failed %d\n", status);
}
if (CY_RSLT_SUCCESS != cyhal_timer_start(&get_throughput_timer_obj))
{
printf("Get throughput timer start failed !\n");
CY_ASSERT(0);
}
}
else
{
/* Device has disconnected */
printf("Disconnected : BDA ");
print_bd_address(p_conn_status->bd_addr);
printf("Connection ID '%d', Reason '%s'\n",
p_conn_status->conn_id,
get_bt_gatt_disconn_reason_name(p_conn_status->reason));
/* Fill the structure containing connection info with zero */
memset(&conn_state_info, 0, sizeof(conn_state_info));
/* Reset the flags */
tput_service_found = false;
mode_flag = GATT_NOTIFANDWRITE;
enable_cccd = true;
gatt_write_tx = false;
scan_flag = true;
/* Clear tx and rx packet count */
gatt_notif_rx_bytes = 0;
gatt_write_tx_bytes = 0;
/* Stop the timers */
if (CY_RSLT_SUCCESS != cyhal_timer_stop(&get_throughput_timer_obj))
{
printf("Get throughput timer stop failed !\n");
CY_ASSERT(0);
}
if (CY_RSLT_SUCCESS != cyhal_timer_stop(&app_millisec_timer_obj))
{
printf("Get millisec timer stop failed !\n");
CY_ASSERT(0);
}
/* Update the scan/conn state */
app_bt_scan_conn_state = APP_BT_SCAN_OFF_CONN_OFF;
printf("Press user button on your kit to start scanning.....\n");
}
/* Update Scan LED to reflect the updated state */
tput_scan_led_update();
}
return status;
}
/*******************************************************************************
* Function Name: tput_app_throughput_timer_callb()
********************************************************************************
*
* Summary:
* One second timer callback.
*
* Parameters:
* void *callback_arg : The argument parameter is not used in this callback.
* cyhal_timer_event_t event : The argument parameter is not used in this
* callback.
*
* Return:
* None
*
*******************************************************************************/
void tput_app_throughput_timer_callb(void *callback_arg,cyhal_timer_event_t event)
{
BaseType_t xHigherPriorityTaskWoken;
xHigherPriorityTaskWoken = pdFALSE;
vTaskNotifyGiveFromISR(get_throughput_task_handle,&xHigherPriorityTaskWoken);
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
}
/*******************************************************************************
* Function Name: get_throughput_task()
********************************************************************************
* Summary:
* Send Throughput Values every second .
*
* Parameters:
* void *pvParam : The argument parameter is not used.
*
* Return:
* None
*
*******************************************************************************/
void get_throughput_task(void *pvParam)
{
while (true)
{
ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
if (conn_state_info.conn_id && gatt_notif_rx_bytes)
{
gatt_notif_rx_bytes = (gatt_notif_rx_bytes * 8) / 1000;
printf("GATT NOTIFICATION : Client Throughput (RX) = %lu kbps\n",
gatt_notif_rx_bytes);
gatt_notif_rx_bytes = 0; //Reset the byte counter
}
if ((conn_state_info.conn_id) && gatt_write_tx_bytes)
{
gatt_write_tx_bytes = (gatt_write_tx_bytes * 8) / 1000;
printf("GATT WRITE : Client Throughput (TX) = %lu kbps\n",
gatt_write_tx_bytes);
gatt_write_tx_bytes = 0; //Reset the byte counter
}
}
}
/*******************************************************************************
* Function Name: tput_app_millisec_timer_cb()
********************************************************************************
*
* Summary:
* One millisecond timer callback.
*
* Parameters:
* void *callback_arg : The argument parameter is not used in this callback.
* cyhal_timer_event_t event : The argument parameter is not used in this
* callback.
*
* Return:
* None
*
*******************************************************************************/
void tput_app_millisec_timer_callb(void *callback_arg,cyhal_timer_event_t event)
{
BaseType_t xHigherPriorityTaskWoken;
xHigherPriorityTaskWoken = pdFALSE;
vTaskNotifyGiveIndexedFromISR(send_gatt_write_task_handle,
TASK_NOTIFY_1MS_TIMER,
&xHigherPriorityTaskWoken);
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
}
/*******************************************************************************
* Function Name: send_gatt_write_task()
********************************************************************************
*
* Summary:
* Send GATT Write if enabled by the GATT Client.
*
* Parameters:
* void *pvParam : The argument parameter is not used.
*
* Return:
* None
*
*******************************************************************************/
void send_gatt_write_task(void *pvParam)
{
while(true)
{
ulTaskNotifyTakeIndexed(TASK_NOTIFY_1MS_TIMER,pdTRUE, portMAX_DELAY);
/* Send GATT write(with no response) commands to the server only
* when there is no GATT congestion and no GATT notifications are being
* received. In data transfer mode 3(Both TX and RX), the GATT write
* commands will be sent irrespective of GATT notifications being received
* or not and when it is connected .
*/
if ((conn_state_info.conn_id) && (gatt_write_tx == true))
{
tput_write_cmd.auth_req = GATT_AUTH_REQ_NONE;
tput_write_cmd.handle = (tput_service_handle) + GATT_WRITE_HANDLE;
tput_write_cmd.len = packet_size;
tput_write_cmd.offset = 0;
/*Packets are sending alternatively*/
if(data_flag == 0)
{
status = wiced_bt_gatt_client_send_write(conn_state_info.conn_id,
GATT_CMD_WRITE,
&tput_write_cmd,
write_data_seq1,
(void *)app_bt_free_buffer);
}
else
{
status = wiced_bt_gatt_client_send_write(conn_state_info.conn_id,
GATT_CMD_WRITE,
&tput_write_cmd,
write_data_seq2,
(void *)app_bt_free_buffer);
}
if(WICED_BT_GATT_CONGESTED == status)
{
app_bt_free_buffer((wiced_bt_buffer_t *)tput_buffer_ptr);
ulTaskNotifyTakeIndexed(TASK_NOTIFY_NO_GATT_CONGESTION,
pdTRUE,
portMAX_DELAY);
}
else if (WICED_BT_GATT_SUCCESS == status)
{
data_flag = data_flag == 0 ? 1 : 0 ;