Welcome to mirror list, hosted at ThFree Co, Russian Federation.

p2p_routeur_app.c « App « STM32_WPAN « BLE_p2pRouteur « BLE « Applications « P-NUCLEO-WB55.Nucleo « Projects - github.com/Flipper-Zero/STM32CubeWB.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6414e560a2cb7f285ec6b95204f36891cad39c0d (plain)
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
/* USER CODE BEGIN Header */
/**
 ******************************************************************************
 * @file    p2p_routeur_app.c
 * @author  MCD Application Team
 * @brief   P2P Routeur Application
 ******************************************************************************
 * @attention
 *
 * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
 * All rights reserved.</center></h2>
 *
 * This software component is licensed by ST under Ultimate Liberty license
 * SLA0044, the "License"; You may not use this file except in compliance with
 * the License. You may obtain a copy of the License at:
 *                             www.st.com/SLA0044
 *
 ******************************************************************************
 */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/

#include "main.h"
#include "app_common.h"
#include "dbg_trace.h"
#include "ble.h"
#include "app_ble.h"
#include "p2p_routeur_app.h"
#include "stm32_seq.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */

/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/

typedef struct{
    /**
     * state of the Connection
     */
    APP_BLE_ConnStatus_t state;

    /**
     * connection handle
     */
    uint16_t connHandle;

    /**
     * handle of the Led Button service
     */
    uint16_t P2PServiceHandle;

    /**
     * end handle of the Data Transfer service
     */
    uint16_t P2PServiceEndHandle;

    /**
     * handle of the Tx characteristic
     *
     */
    uint16_t P2PLedCharHdle;

    /**
     * handle of the client configuration
     * descriptor of Tx characteristic
     */
    uint16_t P2PLedDescHandle;

    /**
     * handle of the client configuration
     * descriptor of new alert characteristic
     */

    uint16_t  P2PClientCharHdle;                  /**< Characteristic handle */
    uint16_t  P2PClientDescHandle;                    /**< Characteristic handle */

}P2P_ClientContext_t;

/* USER CODE BEGIN PTD */
typedef struct{
    uint8_t                                     Device_Led_Selection;
    uint8_t                                     Led1Control;
}P2P_LedCharValue_t;

typedef struct{
    uint8_t                                     Device_Button_Selection;
    uint8_t                                     ButtonStatus;
}P2P_ButtonCharValue_t;

typedef struct
{
    /**
     * used to chek if Client (Smart Phone) can receive push button information
     */
    uint8_t       Notification_Button_Status;
    /**
     * used to chek if Client (Smart Phone) can receive end device connection information
     */
    uint8_t       Notification_EndDevice_Status;
    /**
     * provide end device Managment information
     */
    EDS_STM_Status_t EndDeviceStatus;
/**
     * Manage button Status for End Device / Server 
     */
    P2P_ButtonCharValue_t      ButtonStatusEndDevice;
    P2P_LedCharValue_t         LedControlEndDevice;
   

} P2P_Router_App_Context_t;

typedef struct
{
    P2P_Client_Data_t LedData;
    P2P_Client_Data_t ButtonData;

} LB_Routeur_ClientContext_t;
/* USER CODE END PTD */

/* Private defines ------------------------------------------------------------*/
#define UNPACK_2_BYTE_PARAMETER(ptr)  \
        (uint16_t)((uint16_t)(*((uint8_t *)ptr))) |   \
        (uint16_t)((((uint16_t)(*((uint8_t *)ptr + 1))) << 8))
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macros -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
/**
 * START of Section BLE_APP_CONTEXT
 */

PLACE_IN_SECTION("BLE_APP_CONTEXT") static P2P_ClientContext_t aP2PClientContext[BLE_CFG_CLT_MAX_NBR_CB];

/**
 * END of Section BLE_APP_CONTEXT
 */
/* USER CODE BEGIN PV */
PLACE_IN_SECTION("BLE_APP_CONTEXT") static P2P_Router_App_Context_t P2P_Router_App_Context;
/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/

static SVCCTL_EvtAckStatus_t Client_Event_Handler(void *pckt);
static tBleStatus Client_Update_Char(uint16_t UUID, uint8_t Service_Instance, uint8_t *pPayload);
static void Client_Update_Service( void );
void P2P_Router_APP_Init(void);
void P2P_Client_App_Notification(P2P_Client_App_Notification_evt_t *pNotification);
void P2P_Client_Init(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Functions Definition ------------------------------------------------------*/
/**
 * @brief  End Device service Notification 
 * @param  GATT Notification (Opcode & Data)
 * @retval None
 */
void EDS_STM_App_Notification(EDS_STM_App_Notification_evt_t *pNotification)
{
/* USER CODE BEGIN EDS_STM_App_Notification_1 */

/* USER CODE END EDS_STM_App_Notification_1 */
        switch(pNotification->EDS_Evt_Opcode)
    {
        /* USER CODE BEGIN EDS_Evt_Opcode */

        /* USER CODE END EDS_Evt_Opcode */
        case EDS_STM_NOTIFY_ENABLED_EVT:
#if(CFG_DEBUG_APP_TRACE != 0) 
            APP_DBG_MSG("-- APPLICATION ROUTER : ENDDEVICEMGT NOTIFICATION ENABLED\r\n");
#endif
            /* USER CODE BEGIN EDS_STM_NOTIFY_ENABLED_EVT */
            P2P_Router_App_Context.Notification_EndDevice_Status = 1;
            /* USER CODE END EDS_STM_NOTIFY_ENABLED_EVT */
            break;

        case EDS_STM_NOTIFY_DISABLED_EVT:
#if(CFG_DEBUG_APP_TRACE != 0) 
            APP_DBG_MSG("-- APPLICATION ROUTER : ENDDEVICEMGT NOTIFICATION DISABLED\r\n");
#endif
            /* USER CODE BEGIN EDS_STM_NOTIFY_DISABLED_EVT */
            P2P_Router_App_Context.Notification_EndDevice_Status = 0;
            /* USER CODE END EDS_STM_NOTIFY_DISABLED_EVT */
            break;

        default:
            /* USER CODE BEGIN EDS_Evt_Opcode_default */

            /* USER CODE END EDS_Evt_Opcode_default */
            break;
    }
/* USER CODE BEGIN EDS_STM_App_Notification_2 */

/* USER CODE END EDS_STM_App_Notification_2 */
    return;
}
/**
 * @brief  P2P service Notification 
 * @param  GATT Notification (Opcode & Data)
 * @retval None
 */
void P2PS_STM_App_Notification(P2PS_STM_App_Notification_evt_t *pNotification)
{
/* USER CODE BEGIN P2PS_STM_App_Notification_1 */

/* USER CODE END P2PS_STM_App_Notification_1 */
    switch(pNotification->P2P_Evt_Opcode)
    {
        /* USER CODE BEGIN P2P_Evt_Opcode */

        /* USER CODE END P2P_Evt_Opcode */
        case P2PS_STM__NOTIFY_ENABLED_EVT:
#if(CFG_DEBUG_APP_TRACE != 0) 
            APP_DBG_MSG("-- APPLICATION ROUTER : BUTTON NOTIFICATION ENABLED\n\r");
#endif
            /* USER CODE BEGIN P2PS_STM__NOTIFY_ENABLED_EVT */
            P2P_Router_App_Context.Notification_Button_Status = 1;
            /* USER CODE END P2PS_STM__NOTIFY_ENABLED_EVT */
            break;

        case P2PS_STM_NOTIFY_DISABLED_EVT:
#if(CFG_DEBUG_APP_TRACE != 0) 
            APP_DBG_MSG("-- APPLICATION ROUTER : BUTTON NOTIFICATION DISABLED\n\r");
#endif
            /* USER CODE BEGIN P2PS_STM_NOTIFY_DISABLED_EVT */
            P2P_Router_App_Context.Notification_Button_Status = 0;
            /* USER CODE END P2PS_STM_NOTIFY_DISABLED_EVT */
            break;

        case P2PS_STM_READ_EVT:
#if(CFG_DEBUG_APP_TRACE != 0) 
            APP_DBG_MSG("-- APPLICATION ROUTER : LED READ STATUS\n\r");
#endif
            /* USER CODE BEGIN P2PS_STM_READ_EVT */

            /* USER CODE END P2PS_STM_READ_EVT */
            break;

        case P2PS_STM_WRITE_EVT:
#if(CFG_DEBUG_APP_TRACE != 0) 
            APP_DBG_MSG("-- APPLICATION ROUTER : WRITE LED CONFIG TO ALL END DEVICE \n\r");
#endif
            /* USER CODE BEGIN P2PS_STM_WRITE_EVT */
            P2P_Router_App_Context.LedControlEndDevice.Device_Led_Selection=pNotification->DataTransfered.pPayload[0];
            P2P_Router_App_Context.LedControlEndDevice.Led1Control=pNotification->DataTransfered.pPayload[1];
            Client_Update_Char( LED_CHAR_UUID, 0, (uint8_t *)&P2P_Router_App_Context.LedControlEndDevice);
            /* USER CODE END P2PS_STM_WRITE_EVT */
            break;

        default:
            /* USER CODE BEGIN P2P_Evt_Opcode_default */

            /* USER CODE END P2P_Evt_Opcode_default */
        break;
    }
/* USER CODE BEGIN P2PS_STM_App_Notification_2 */

/* USER CODE END P2PS_STM_App_Notification_2 */
    return;
}

/**
 * @brief  Router Application initialisation 
 * @param  None
 * @retval None
 */
void P2P_Router_APP_Init(void)
{
    /* USER CODE BEGIN P2P_Router_APP_Init_1 */
    P2PR_APP_Device_Status_t device_status;
    /* USER CODE END P2P_Router_APP_Init_1 */

    UTIL_SEQ_RegTask( 1<< CFG_TASK_SEARCH_SERVICE_ID, UTIL_SEQ_RFU, Client_Update_Service );

    /* USER CODE BEGIN P2P_Router_APP_Init_2 */
    /**
     * Initialize LedButton Service
     */
    P2P_Router_App_Context.Notification_Button_Status=0;
    P2P_Router_App_Context.Notification_EndDevice_Status=0;

    P2P_Router_App_Context.EndDeviceStatus.Device1_Status=0x00;
    device_status.Device1_Status = 0x80; /* Not connected */
#if (CFG_P2P_DEMO_MULTI != 0 )   
    P2P_Router_App_Context.EndDeviceStatus.Device2_Status=0x00;
    P2P_Router_App_Context.EndDeviceStatus.Device3_Status=0x00;
    P2P_Router_App_Context.EndDeviceStatus.Device4_Status=0x00;
    P2P_Router_App_Context.EndDeviceStatus.Device5_Status=0x00;
    P2P_Router_App_Context.EndDeviceStatus.Device6_Status=0x00;
    device_status.Device2_Status = 0x80; /* Not connected */
    device_status.Device3_Status = 0x80; /* Not connected */
    device_status.Device4_Status = 0x80; /* Not connected */
    device_status.Device5_Status = 0x80; /* Not connected */
    device_status.Device6_Status = 0x80; /* Not connected */
#endif
    /* Update EndDevice Management Service */
    
    
    P2PR_APP_End_Device_Mgt_Connection_Update( &device_status );

    /* USER CODE END P2P_Router_APP_Init_2 */

    return;
}

/**
 * @brief  End Device Managment
 * @param  None
 * @retval None
 */
void P2PR_APP_End_Device_Mgt_Connection_Update( P2PR_APP_Device_Status_t *p_device_status )
{
/* USER CODE BEGIN P2PR_APP_End_Device_Mgt_Connection_Update_1 */

/* USER CODE END P2PR_APP_End_Device_Mgt_Connection_Update_1 */
    if( (p_device_status->Device1_Status) & (0x80) )
    {
    /* USER CODE BEGIN Device1_Status */
      P2P_Router_App_Context.EndDeviceStatus.Device1_Status = (p_device_status->Device1_Status) & (0x7F);
    /* USER CODE END Device1_Status */
    }
#if (CFG_P2P_DEMO_MULTI != 0 )   
    /* USER CODE BEGIN CFG_P2P_DEMO_MULTI */
    if( (p_device_status->Device2_Status) & (0x80) )
    {
        P2P_Router_App_Context.EndDeviceStatus.Device2_Status = (p_device_status->Device2_Status) & (0x7F);
    }

    if( (p_device_status->Device3_Status) & (0x80) )
    {
        P2P_Router_App_Context.EndDeviceStatus.Device3_Status = (p_device_status->Device3_Status) & (0x7F);
    }

    if( (p_device_status->Device4_Status) & (0x80) )
    {
        P2P_Router_App_Context.EndDeviceStatus.Device4_Status = (p_device_status->Device4_Status) & (0x7F);
    }
    if( (p_device_status->Device5_Status) & (0x80) )
    {
        P2P_Router_App_Context.EndDeviceStatus.Device5_Status = (p_device_status->Device5_Status) & (0x7F);
    }

    if( (p_device_status->Device6_Status) & (0x80) )
    {
        P2P_Router_App_Context.EndDeviceStatus.Device6_Status = (p_device_status->Device6_Status) & (0x7F);
    }

    /* USER CODE END CFG_P2P_DEMO_MULTI */
#endif
/* USER CODE BEGIN P2PR_APP_End_Device_Mgt_Connection_Update_2 */
    EDS_STM_Update_Char(END_DEVICE_STATUS_CHAR_UUID, 
            (uint8_t *)&P2P_Router_App_Context.EndDeviceStatus);

/* USER CODE END P2PR_APP_End_Device_Mgt_Connection_Update_2 */
    return;
}

/**
 * @brief  Notification from the Server
 * @param  Client Notification from Server side
 * @retval None
 */
void P2P_Client_App_Notification(P2P_Client_App_Notification_evt_t *pNotification)
{
/* USER CODE BEGIN P2P_Client_App_Notification_1 */

/* USER CODE END P2P_Client_App_Notification_1 */
    switch(pNotification->P2P_Client_Evt_Opcode)
    {
    /* USER CODE BEGIN P2P_Client_Evt_Opcode */
      case P2P_BUTTON_INFO_RECEIVED_EVT:
        {
           
           if(P2P_Router_App_Context.Notification_Button_Status){
            APP_DBG_MSG("-- APPLICATION : INFORM SMART PHONE DEVICE %x BUTTON PUSHED \n\r ",pNotification->DataTransfered.pPayload[0]);
            APP_DBG_MSG("\r\n\r ");
            P2P_Router_App_Context.ButtonStatusEndDevice.ButtonStatus=pNotification->DataTransfered.pPayload[1];
            P2P_Router_App_Context.ButtonStatusEndDevice.Device_Button_Selection=pNotification->DataTransfered.pPayload[0];
            P2PS_STM_App_Update_Char(P2P_NOTIFY_CHAR_UUID, (uint8_t *)&P2P_Router_App_Context.ButtonStatusEndDevice);
        } else {
            APP_DBG_MSG("-- APPLICATION : NOT POSSIBLE TO INFORM THE SMART PHONE - NOTIFICATION DISABLED  \n ");
            APP_DBG_MSG("\r\n\r ");
        }

   

        }
        break;


    /* USER CODE END P2P_Client_Evt_Opcode */
        default:
    /* USER CODE BEGIN P2P_Client_Evt_Opcode_default */

    /* USER CODE END P2P_Client_Evt_Opcode_default */
            break;

    }
/* USER CODE BEGIN P2P_Client_App_Notification_2 */

/* USER CODE END P2P_Client_App_Notification_2 */
    return;
}

/**
 * @brief  Service initialization
 * @param  None
 * @retval None
 */
void P2P_Client_Init(void)
{
    uint8_t index =0;
/* USER CODE BEGIN P2P_Client_Init_1 */

/* USER CODE END P2P_Client_Init_1 */

    for(index = 0; index < BLE_CFG_CLT_MAX_NBR_CB; index++)
        aP2PClientContext[index].state= APP_BLE_IDLE;
    /**
     *    Register the event handler to the BLE controller
     */
    SVCCTL_RegisterCltHandler(Client_Event_Handler);
/* USER CODE BEGIN P2P_Client_Init_2 */
    APP_DBG_MSG("-- LED BUTTON CLIENT INITIALIZED \n");
/* USER CODE END P2P_Client_Init_2 */
    return;
}

/* USER CODE BEGIN FD */

/* USER CODE END FD */
/*************************************************************
 *
 * PUBLIC FUNCTIONS
 *
 *************************************************************/
void P2PR_APP_Init( void )
{
    P2P_Router_APP_Init();
    P2P_Client_Init();
    /* USER CODE BEGIN P2PR_APP_Init */

    /* USER CODE END P2PR_APP_Init */
    return;
}

/* USER CODE BEGIN FD_PUBLIC_FUNCTIONS */

/* USER CODE END FD_PUBLIC_FUNCTIONS */
/*************************************************************
 *
 * LOCAL FUNCTIONS
 *
 *************************************************************/
static void Client_Update_Service( void )
{
    /* USER CODE BEGIN Client_Update_Service_1 */

    /* USER CODE END Client_Update_Service_1 */
    uint16_t enable = 0x0001;

    uint8_t index;

    index = 0;
    while((index < BLE_CFG_CLT_MAX_NBR_CB) &&
            (aP2PClientContext[index].state != APP_BLE_IDLE))
    {

        switch(aP2PClientContext[index].state)
        {
            /* USER CODE BEGIN aP2PClientContext */
            case APP_BLE_DISCOVER_LED_CHAR_DESC: /* Not Used - No decriptor */
                APP_DBG_MSG("* GATT : Discover Descriptor of Led Characteritic\n");
                aci_gatt_disc_all_char_desc(aP2PClientContext[index].connHandle,
                        aP2PClientContext[index].P2PLedCharHdle,
                        aP2PClientContext[index].P2PLedCharHdle+2);

                break;
            case APP_BLE_DISCOVER_BUTTON_CHAR_DESC:
                APP_DBG_MSG("* GATT : Discover Descriptor of Button Characteritic\n");
                aci_gatt_disc_all_char_desc(aP2PClientContext[index].connHandle,
                        aP2PClientContext[index].P2PClientCharHdle,
                        aP2PClientContext[index].P2PClientCharHdle+2);

                break;
            case APP_BLE_ENABLE_NOTIFICATION_BUTTON_DESC:
                APP_DBG_MSG("* GATT : Enable Button Notification\n");
                aci_gatt_write_char_desc(aP2PClientContext[index].connHandle,
                        aP2PClientContext[index].P2PClientDescHandle,
                        2,
                        (uint8_t *)&enable);

                aP2PClientContext[index].state = APP_BLE_CONNECTED;

                break;
            /* USER CODE END aP2PClientContext */
            case APP_BLE_DISCOVER_SERVICES:
#if(CFG_DEBUG_APP_TRACE != 0) 
                APP_DBG_MSG("APP_BLE_DISCOVER_SERVICES\n");
#endif
            /* USER CODE BEGIN APP_BLE_DISCOVER_SERVICES */

            /* USER CODE END APP_BLE_DISCOVER_SERVICES */
            break;
            case APP_BLE_DISCOVER_CHARACS:
#if(CFG_DEBUG_APP_TRACE != 0) 
                APP_DBG_MSG("* GATT : Discover Led Button  Characteristics\n");
#endif
                aci_gatt_disc_all_char_of_service(aP2PClientContext[index].connHandle,
                        aP2PClientContext[index].P2PServiceHandle,
                        aP2PClientContext[index].P2PServiceEndHandle);
            /* USER CODE BEGIN APP_BLE_DISCOVER_CHARACS */

            /* USER CODE END APP_BLE_DISCOVER_CHARACS */
                break;

            case APP_BLE_DISABLE_NOTIFICATION_TX_DESC :
#if(CFG_DEBUG_APP_TRACE != 0) 
                APP_DBG_MSG("* GATT : Disable Button Notification\n");
#endif
                aci_gatt_write_char_desc(aP2PClientContext[index].connHandle,
                        aP2PClientContext[index].P2PClientDescHandle,
                        2,
                        (uint8_t *)&enable);

                aP2PClientContext[index].state = APP_BLE_CONNECTED;
            /* USER CODE BEGIN APP_BLE_DISABLE_NOTIFICATION_TX_DESC */

            /* USER CODE END APP_BLE_DISABLE_NOTIFICATION_TX_DESC */
                break;
            default:
            /* USER CODE BEGIN aP2PClientContext_default */

            /* USER CODE END aP2PClientContext_default */
                break;
        }
        index++;
    }
    /* USER CODE BEGIN Client_Update_Service_2 */

    /* USER CODE END Client_Update_Service_2 */
    return;
}

/**
 * @brief  Feature Characteristic update
 * @param  pFeatureValue: The address of the new value to be written
 * @retval None
 */
static tBleStatus Client_Update_Char(uint16_t UUID, uint8_t Service_Instance, uint8_t *pPayload)
{
    /* USER CODE BEGIN Client_Update_Char_1 */

    /* USER CODE END Client_Update_Char_1 */
    tBleStatus ret = BLE_STATUS_INVALID_PARAMS;
    uint8_t index;

    index = 0;
    while((index < BLE_CFG_CLT_MAX_NBR_CB) &&
            (aP2PClientContext[index].state != APP_BLE_IDLE))
    {
        /* USER CODE BEGIN Client_Update_Char_2 */
        switch(UUID)
        {
            case LED_CHAR_UUID: /* SERVER RX -- so CLIENT TX */
                ret =aci_gatt_write_without_resp(aP2PClientContext[index].connHandle,
                        aP2PClientContext[index].P2PLedCharHdle,
                        2, /* charValueLen */
                        (uint8_t *)  pPayload);

                break;

            default:
                break;
        }
        /* USER CODE END Client_Update_Char_2 */
        index++;
    }
    /* USER CODE BEGIN Client_Update_Char_3 */

    /* USER CODE END Client_Update_Char_3 */
    return ret;
}/* end Client_Update_Char() */

/**
 * @brief  Event handler
 * @param  Event: Address of the buffer holding the Event
 * @retval Ack: Return whether the Event has been managed or not
 */
static SVCCTL_EvtAckStatus_t Client_Event_Handler(void *Event)
{
    /* USER CODE BEGIN Client_Event_Handler_1 */

    /* USER CODE END Client_Event_Handler_1 */
    SVCCTL_EvtAckStatus_t return_value;
    hci_event_pckt *event_pckt;
    evt_blue_aci *blue_evt;

    P2P_Client_App_Notification_evt_t Notification;

    return_value = SVCCTL_EvtNotAck;
    event_pckt = (hci_event_pckt *)(((hci_uart_pckt*)Event)->data);

    switch(event_pckt->evt)
    {
        case EVT_VENDOR:
        {
            /* USER CODE BEGIN EVT_VENDOR */

            /* USER CODE END EVT_VENDOR */
            blue_evt = (evt_blue_aci*)event_pckt->data;
            switch(blue_evt->ecode)
            {
            /* USER CODE BEGIN ecode */

            /* USER CODE END ecode */
                case EVT_BLUE_ATT_READ_BY_GROUP_TYPE_RESP:
                {
                    /* USER CODE BEGIN EVT_BLUE_ATT_READ_BY_GROUP_TYPE_RESP */

                    /* USER CODE END EVT_BLUE_ATT_READ_BY_GROUP_TYPE_RESP */
                    aci_att_read_by_group_type_resp_event_rp0 *pr = (void*)blue_evt->data;
                    uint8_t numServ, i, idx;
                    uint16_t uuid, handle;

                    uint8_t index;
                    handle = pr->Connection_Handle;
                    index = 0;
                    while((index < BLE_CFG_CLT_MAX_NBR_CB) &&
                            (aP2PClientContext[index].state != APP_BLE_IDLE)/* &&
                        (aP2PClientContext[index].connHandle != handle)*/)
                    {
                        APP_BLE_ConnStatus_t status;

                        status = APP_BLE_Get_Client_Connection_Status(aP2PClientContext[index].connHandle);
                        if((aP2PClientContext[index].state == APP_BLE_CONNECTED)&&
                                (status == APP_BLE_IDLE))
                        {
                            /* Handle deconnected */

                            aP2PClientContext[index].state = APP_BLE_IDLE;
                            aP2PClientContext[index].connHandle = 0xFFFF;
                            break;
                        }
                        index++;
                    }

                    if(index < BLE_CFG_CLT_MAX_NBR_CB)
                    {
                        aP2PClientContext[index].connHandle= handle;
                        numServ = (pr->Data_Length) / pr->Attribute_Data_Length;

                        /* the event data will be
                         * 2bytes start handle
                         * 2bytes end handle
                         * 2 or 16 bytes data
                         * we are intersted only if the UUID is 16 bit.
                         * So check if the data length is 6
                         * we are intersted only if the UUID is 128 bit.
                         * So check if the data length is 20
                         */
#if (UUID_128BIT_FORMAT==1)
                        if (pr->Attribute_Data_Length == 20)
                        {
                            idx = 16;
#else
                            if (pr->Attribute_Data_Length == 6)
                            {
                                idx = 4;
#endif
                                for (i=0; i<numServ; i++)
                                {
                                    uuid = UNPACK_2_BYTE_PARAMETER(&pr->Attribute_Data_List[idx]);
                                    if(uuid == P2P_SERVICE_UUID)
                                    {
#if(CFG_DEBUG_APP_TRACE != 0)
                                        APP_DBG_MSG("-- GATT : P2P_SERVICE_UUID FOUND - connection handle 0x%x \n", aP2PClientContext[index].connHandle);
#endif
                                        #if (UUID_128BIT_FORMAT==1)
                                        aP2PClientContext[index].P2PServiceHandle = UNPACK_2_BYTE_PARAMETER(&pr->Attribute_Data_List[idx-16]);
                                        aP2PClientContext[index].P2PServiceEndHandle = UNPACK_2_BYTE_PARAMETER (&pr->Attribute_Data_List[idx-14]);
#else
                                        aP2PClientContext[index].P2PServiceHandle = UNPACK_2_BYTE_PARAMETER(&pr->Attribute_Data_List[idx-4]);
                                        aP2PClientContext[index].P2PServiceEndHandle = UNPACK_2_BYTE_PARAMETER (&pr->Attribute_Data_List[idx-2]);
#endif
                                        aP2PClientContext[index].state = APP_BLE_DISCOVER_CHARACS ;
                                    }
                                    idx += 6;
                                }
                            }
                        }
                    }
                    break;

                case EVT_BLUE_ATT_READ_BY_TYPE_RESP:
                {
                    /* USER CODE BEGIN EVT_BLUE_ATT_READ_BY_TYPE_RESP */

                    /* USER CODE END EVT_BLUE_ATT_READ_BY_TYPE_RESP */
                    aci_att_read_by_type_resp_event_rp0 *pr = (void*)blue_evt->data;

                    uint8_t idx;
                    uint16_t uuid, handle;

                    /* the event data will be
                     * 2 bytes start handle
                     * 1 byte char properties
                     * 2 bytes handle
                     * 2 or 16 bytes data
                     */

                    uint8_t index;

                    index = 0;
                    while((index < BLE_CFG_CLT_MAX_NBR_CB) &&
                            (aP2PClientContext[index].connHandle != pr->Connection_Handle))
                        index++;

                    if(index < BLE_CFG_CLT_MAX_NBR_CB)
                    {

                        /* we are interested in only 16 bit UUIDs */
#if (UUID_128BIT_FORMAT==1)
                        idx = 17;
                        if (pr->Handle_Value_Pair_Length == 21)
#else
                            idx = 5;
                        if (pr->Handle_Value_Pair_Length == 7)
#endif
                        {
                            pr->Data_Length -= 1;
                            while(pr->Data_Length > 0)
                            {
                                uuid = UNPACK_2_BYTE_PARAMETER(&pr->Handle_Value_Pair_Data[idx]);
                                /* store the characteristic handle not the attribute handle */
#if (UUID_128BIT_FORMAT==1)
                                handle = UNPACK_2_BYTE_PARAMETER(&pr->Handle_Value_Pair_Data[idx-14]);
#else
                                handle = UNPACK_2_BYTE_PARAMETER(&pr->Handle_Value_Pair_Data[idx-2]);
#endif
                                if(uuid == P2P_WRITE_CHAR_UUID)
                                {
#if(CFG_DEBUG_APP_TRACE != 0)
                                    APP_DBG_MSG("-- GATT : LED_CHAR_UUID FOUND - connection handle 0x%x\n", aP2PClientContext[index].connHandle);
#endif
                                    aP2PClientContext[index].state = APP_BLE_DISCOVER_LED_CHAR_DESC;
                                    aP2PClientContext[index].P2PLedCharHdle = handle;
                                }

                                else if(uuid == P2P_NOTIFY_CHAR_UUID)
                                {
#if(CFG_DEBUG_APP_TRACE != 0)
                                    APP_DBG_MSG("-- GATT : BUTTON_CHAR_UUID FOUND  - connection handle 0x%x\n", aP2PClientContext[index].connHandle);
#endif
                                    aP2PClientContext[index].state = APP_BLE_DISCOVER_BUTTON_CHAR_DESC;
                                    aP2PClientContext[index].P2PClientCharHdle = handle;
                                }
#if (UUID_128BIT_FORMAT==1)
                                pr->Data_Length -= 21;
                                idx += 21;
#else
                                pr->Data_Length -= 7;;
                                idx += 7;;
#endif
                            }
                        }
                    }
                }
                break;

                case EVT_BLUE_ATT_FIND_INFORMATION_RESP:
                {
                    /* USER CODE BEGIN EVT_BLUE_ATT_FIND_INFORMATION_RESP */

                    /* USER CODE END EVT_BLUE_ATT_FIND_INFORMATION_RESP */
                    aci_att_find_info_resp_event_rp0 *pr = (void*)blue_evt->data;

                    uint8_t numDesc, idx, i;
                    uint16_t uuid, handle;

                    /*
                     * event data will be of the format
                     * 2 bytes handle
                     * 2 bytes UUID
                     */

                    uint8_t index;

                    index = 0;
                    while((index < BLE_CFG_CLT_MAX_NBR_CB) &&
                            (aP2PClientContext[index].connHandle != pr->Connection_Handle))

                        index++;

                    if(index < BLE_CFG_CLT_MAX_NBR_CB)
                    {

                        numDesc = (pr->Event_Data_Length) / 4;
                        /* we are interested only in 16 bit UUIDs */
                        idx = 0;
                        if (pr->Format == UUID_TYPE_16)
                        {
                            for (i=0; i<numDesc; i++)
                            {
                                handle = UNPACK_2_BYTE_PARAMETER(&pr->Handle_UUID_Pair[idx]);
                                uuid = UNPACK_2_BYTE_PARAMETER(&pr->Handle_UUID_Pair[idx+2]);

                                if(uuid == CLIENT_CHAR_CONFIG_DESCRIPTOR_UUID)
                                {
#if(CFG_DEBUG_APP_TRACE != 0)
                                    APP_DBG_MSG("-- GATT : CLIENT_CHAR_CONFIG_DESCRIPTOR_UUID- connection handle 0x%x\n", aP2PClientContext[index].connHandle);
#endif
                                    if( aP2PClientContext[index].state == APP_BLE_DISCOVER_BUTTON_CHAR_DESC)
                                    {

                                        aP2PClientContext[index].P2PClientDescHandle = handle;
                                        aP2PClientContext[index].state = APP_BLE_ENABLE_NOTIFICATION_BUTTON_DESC;

                                    }
                                }
                                idx += 4;
                            }
                        }
                    }
                }
                break; /*EVT_BLUE_ATT_FIND_INFORMATION_RESP*/

                case EVT_BLUE_GATT_NOTIFICATION:
                {
                    /* USER CODE BEGIN EVT_BLUE_GATT_NOTIFICATION */

                    /* USER CODE END EVT_BLUE_GATT_NOTIFICATION */
                    aci_gatt_notification_event_rp0 *pr = (void*)blue_evt->data;
                    uint8_t index;

                    index = 0;
                    while((index < BLE_CFG_CLT_MAX_NBR_CB) &&
                            (aP2PClientContext[index].connHandle != pr->Connection_Handle))
                        index++;

                    if(index < BLE_CFG_CLT_MAX_NBR_CB)
                    {

                        if ( (pr->Attribute_Handle == aP2PClientContext[index].P2PClientCharHdle) &&
                                (pr->Attribute_Value_Length == (2)) )
                        {
#if(CFG_DEBUG_APP_TRACE != 0)
                            APP_DBG_MSG("-- GATT : BUTTON CHARACTERISTICS RECEIVED_EVT - connection handle 0x%x\n", aP2PClientContext[index].connHandle);
#endif
                            Notification.P2P_Client_Evt_Opcode = P2P_BUTTON_INFO_RECEIVED_EVT;
                            Notification.DataTransfered.Length = pr->Attribute_Value_Length;
                            Notification.DataTransfered.pPayload = &pr->Attribute_Value[0];

                            P2P_Client_App_Notification(&Notification);

                            /* INFORM APPLICATION BUTTON IS PUSHED BY END DEVICE */

                        }
                    }
                }
                break;/* end EVT_BLUE_GATT_NOTIFICATION */

                case EVT_BLUE_GATT_PROCEDURE_COMPLETE:
                {
                    /* USER CODE BEGIN EVT_BLUE_GATT_PROCEDURE_COMPLETE */

                    /* USER CODE END EVT_BLUE_GATT_PROCEDURE_COMPLETE */
                    aci_gatt_proc_complete_event_rp0 *pr = (void*)blue_evt->data;
#if(CFG_DEBUG_APP_TRACE != 0) 
                    APP_DBG_MSG("-- GATT : EVT_BLUE_GATT_PROCEDURE_COMPLETE \n");
                    APP_DBG_MSG("\n");
#endif
                    uint8_t index;
                    index = 0;
                    while((index < BLE_CFG_CLT_MAX_NBR_CB) &&
                            (aP2PClientContext[index].connHandle != pr->Connection_Handle))
                        index++;

                    if(index < BLE_CFG_CLT_MAX_NBR_CB)
                    {
                        UTIL_SEQ_SetTask(  1<<CFG_TASK_SEARCH_SERVICE_ID, CFG_SCH_PRIO_0 );
                    }
                }
                break; /*EVT_BLUE_GATT_PROCEDURE_COMPLETE*/
                default:
                    /* USER CODE BEGIN ecode_default */

                    /* USER CODE END ecode_default */
                    break;
                }
            }

            break; /* HCI_EVT_VENDOR_SPECIFIC */

        default:
            /* USER CODE BEGIN evt_default */

            /* USER CODE END evt_default */
            break;
        }
/* USER CODE BEGIN Client_Event_Handler_2 */

/* USER CODE END Client_Event_Handler_2 */
        return(return_value);
    }/* end BLE_CTRL_Event_Acknowledged_Status_t */

/* USER CODE BEGIN FD_LOCAL_FUNCTIONS */

/* USER CODE END FD_LOCAL_FUNCTIONS */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/