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

hids.c « Src « svc « ble « STM32_WPAN « ST « Middlewares - github.com/Flipper-Zero/STM32CubeWB.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 60fa34d11c33434c48f9785f98381a1dc65b920e (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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
/**
 ******************************************************************************
 * @file    hids.c
 * @author  MCD Application Team
 * @brief   Human Interface Device Service
 ******************************************************************************
 * @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
 *
 ******************************************************************************
 */



/* Includes ------------------------------------------------------------------*/
#include "common_blesvc.h"


/* Private typedef -----------------------------------------------------------*/
typedef struct
{
  uint16_t HidSvcHdle;                      /**< Service handle        */
#if (BLE_CFG_HIDS_PROTOCOL_MODE_CHAR != 0)
  uint16_t HidProtocolModeCharHdle;         /**< Characteristic handle */
#endif
#if (BLE_CFG_HIDS_REPORT_CHAR != 0)
  uint16_t HidReportCharHdle[BLE_CFG_HIDS_INPUT_REPORT_NB +
                             BLE_CFG_HIDS_OUTPUT_REPORT_NB +
                             BLE_CFG_HIDS_FEATURE_REPORT_NB]; /**< Characteristic handle */
  uint16_t HidReportReferenceDescHdle[BLE_CFG_HIDS_INPUT_REPORT_NB +
                                      BLE_CFG_HIDS_OUTPUT_REPORT_NB +
                                      BLE_CFG_HIDS_FEATURE_REPORT_NB];      /**< Descriptor handle */
#endif                                      
  uint16_t HidReportMapCharHdle;            /**< Characteristic handle */
#if (BLE_CFG_HIDS_EXTERNAL_REPORT_REFERENCE != 0)
  uint16_t HidExternalReferenceDescHdle;    /**< Descriptor handle */
#endif
#if (BLE_CFG_HIDS_KEYBOARD_DEVICE != 0)          
  uint16_t HidKeyboardReportInputCharHdle;  /**< Characteristic handle */
  uint16_t HidKeyboardReportOutputCharHdle; /**< Characteristic handle */
#endif
#if (BLE_CFG_HIDS_MOUSE_DEVICE != 0) 
  uint16_t HidMouseReportInputCharHdle;      /**< Characteristic handle */
#endif
  uint16_t HidInformationCharHdle;          /**< Characteristic handle */
  uint16_t HidControlPointCharHdle;         /**< Characteristic handle */
}HIDS_Context_t;


/* Private defines -----------------------------------------------------------*/
/* Private macros ------------------------------------------------------------*/
/* Store Value into a buffer in Little Endian Format */
#define STORE_LE_16(buf, val)    ( ((buf)[0] =  (uint8_t) (val)    ) , \
                                   ((buf)[1] =  (uint8_t) (val>>8) ) )
#define STORE_LE_32(buf, val)    ( ((buf)[0] =  (uint8_t) (val)     ) , \
                                   ((buf)[1] =  (uint8_t) (val>>8)  ) , \
                                   ((buf)[2] =  (uint8_t) (val>>16) ) , \
                                   ((buf)[3] =  (uint8_t) (val>>24) ) ) 


/* Private variables ---------------------------------------------------------*/

/**
 * START of Section BLE_DRIVER_CONTEXT
 */

PLACE_IN_SECTION("BLE_DRIVER_CONTEXT") static HIDS_Context_t HIDS_Context[BLE_CFG_HIDS_NUMBER];

/**
 * END of Section BLE_DRIVER_CONTEXT
 */


/* Private function prototypes -----------------------------------------------*/
/* Functions Definition ------------------------------------------------------*/
/* Private functions ----------------------------------------------------------*/

/**
 * @brief  HID Handle Control Point write
 * @retval None
 */
static void HIDS_Handle_Control_Point_Write(uint8_t service_instance, uint8_t *attVal)
{
  tBleStatus hciCmdResult;
  uint16_t length;
  uint8_t data;
  
  hciCmdResult = aci_gatt_read_handle_value(HIDS_Context[service_instance].HidControlPointCharHdle + 1,
                                            0,
                                            1,
                                            &length,
                                            &length,
                                            &data);
  if (hciCmdResult == BLE_STATUS_SUCCESS)
  {
    BLE_DBG_HIDS_MSG("Command %d of length %d\n", data, length);
  }
  else
  {
    BLE_DBG_HIDS_MSG ("FAILED to read handle value, Error: %02X !!\n", 
                         hciCmdResult);
  }
}


#if((BLE_CFG_HIDS_MOUSE_DEVICE != 0) && (BLE_CFG_HIDS_MOUSE_INPUT_WRITE != 0))
/**
 * @brief  HID Handle Mouse Input write
 * @retval None
 */
static void HIDS_Handle_Mouse_Input_Write(uint8_t service_instance, uint8_t *attVal)
{
  tBleStatus hciCmdResult;
  uint16_t length;
  uint8_t *data = 0;
  HIDS_App_Notification_evt_t Notification;
  
  hciCmdResult = aci_gatt_read_handle_value(HIDS_Context[service_instance].HidMouseReportInputCharHdle + 1,
                                            0,
                                            BLE_CFG_HIDS_BOOT_MOUSE_INPUT_REPORT_MAX_LEN,
                                            &length,
                                            &length,
                                            data);
  if (hciCmdResult == BLE_STATUS_SUCCESS)
  {
    uint8_t i;
    
    for(i = 0; i < length; i++)
      BLE_DBG_HIDS_MSG("Data[%d] 0x%X \n", i, data[i]);
  }
  else
  {
    BLE_DBG_HIDS_MSG ("FAILED to read handle value, Error: %02X !!\n", 
                         hciCmdResult);
  }
  
  /**
   * Notify the application of notification setting
   */
  Notification.HIDS_Evt_Opcode = HIDS_MOUSE_INPUT_REPORT;
  Notification.Instance = service_instance;
  Notification.Index = 0;
  Notification.pReport = data;
  Notification.ReportLength = length;

  HIDS_Notification(&Notification);
}
#endif

#if((BLE_CFG_HIDS_KEYBOARD_DEVICE != 0) && (BLE_CFG_HIDS_KEYBOARD_INPUT_WRITE != 0))
/**
 * @brief  HID Handle Keyboard Output write
 * @retval None
 */
static void HIDS_Handle_Keyboard_Output_Write(uint8_t service_instance, uint8_t *attVal)
{
  tBleStatus hciCmdResult;
  uint16_t length;
  uint8_t *data = 0;
  HIDS_App_Notification_evt_t Notification;
  
  hciCmdResult = aci_gatt_read_handle_value(HIDS_Context[service_instance].HidKeyboardReportOutputCharHdle + 1, 
                                            0,
                                            BLE_CFG_HIDS_BOOT_KEYBOARD_OUTPUT_REPORT_MAX_LEN,
                                            &length,
                                            &length,
                                            data);
  if (hciCmdResult == BLE_STATUS_SUCCESS)
  {
    uint8_t i;
    
    for(i = 0; i < length; i++)
      BLE_DBG_HIDS_MSG("Data[%d] 0x%X \n", i, data[i]);
  }
  else
  {
    BLE_DBG_HIDS_MSG ("FAILED to read handle value, Error: %02X !!\n", 
                         hciCmdResult);
  }
  
  /**
   * Notify the application of notification setting
   */
  Notification.HIDS_Evt_Opcode = HIDS_KEYBOARD_OUTPUT_REPORT;
  Notification.Instance = service_instance;
  Notification.pReport = data;
  Notification.ReportLength = length;

  HIDS_Notification(&Notification);
}
#endif


#if((BLE_CFG_HIDS_KEYBOARD_DEVICE != 0) && (BLE_CFG_HIDS_KEYBOARD_INPUT_WRITE != 0))
/**
 * @brief  HID Handle Keyboard Input write
 * @retval None
 */
static void HIDS_Handle_Keyboard_Input_Write(uint8_t service_instance, uint8_t *attVal)
{
  tBleStatus hciCmdResult;
  uint8_t *data = 0;
  uint16_t length;
  HIDS_App_Notification_evt_t Notification;
  
  hciCmdResult = aci_gatt_read_handle_value(HIDS_Context[service_instance].HidKeyboardReportInputCharHdle + 1,
                                            0,
                                            BLE_CFG_HIDS_BOOT_KEYBOARD_INPUT_REPORT_MAX_LEN,
                                            &length,
                                            &length,
                                            data);
  if (hciCmdResult == BLE_STATUS_SUCCESS)
  {
    uint8_t i;
    
    for(i = 0; i < length; i++)
      BLE_DBG_HIDS_MSG("Data[%d] 0x%X \n", i, data[i]);
  }
  else
  {
    BLE_DBG_HIDS_MSG ("FAILED to read handle value, Error: %02X !!\n", 
                         hciCmdResult);
  }
  
  /**
   * Notify the application of notification setting
   */
  Notification.HIDS_Evt_Opcode = HIDS_KEYBOARD_INPUT_REPORT;
  Notification.Instance = service_instance;
  Notification.pReport = data;
  Notification.ReportLength = length;

  HIDS_Notification(&Notification);
}
#endif


#if(BLE_CFG_HIDS_REPORT_CHAR != 0)
/**
 * @brief  HID Handle Report
 * @retval None
 */
static void HIDS_Handle_Report(uint8_t service_instance, uint8_t report_index, uint8_t *attVal)
{
  tBleStatus hciCmdResult;
  uint8_t *data = 0;
  uint16_t length;
  HIDS_App_Notification_evt_t Notification;

  /**
   * Category ID update
   */
  hciCmdResult = aci_gatt_read_handle_value(HIDS_Context[service_instance].HidReportCharHdle[report_index] + 1,
                                            0,
                                            BLE_CFG_HIDS_REPORT_MAX_LEN,
                                            &length,
                                            &length,
                                            data);
  if (hciCmdResult == BLE_STATUS_SUCCESS)
  {
    uint8_t i;
    
    for(i = 0; i < length; i++)
      BLE_DBG_HIDS_MSG("Report %d Data[%d] 0x%X \n", report_index, i, data[i]);
  }
  else
  {
    BLE_DBG_HIDS_MSG ("FAILED to read handle value, Error: %02X !!\n", 
                         hciCmdResult);
  }

  /**
   * Notify the application of notification setting
   */
  Notification.HIDS_Evt_Opcode = HIDS_OUTPUT_REPORT;
  Notification.Instance = service_instance;
  Notification.Index = report_index;
  Notification.pReport = data;
  Notification.ReportLength = length;

  HIDS_Notification(&Notification);
}
#endif


#if(BLE_CFG_HIDS_PROTOCOL_MODE_CHAR != 0)
/**
 * @brief  HID Handle Protocol Mode
 * @retval None
 */
static void HIDS_Handle_Protocol_Mode(uint8_t service_instance, uint8_t *attVal)
{
  tBleStatus hciCmdResult;
  uint16_t length;
  uint8_t data;
  
  hciCmdResult = aci_gatt_read_handle_value(HIDS_Context[service_instance].HidProtocolModeCharHdle + 1, 
                                            0,
                                            1,
                                            &length,
                                            &length,
                                            &data);
  if (hciCmdResult == BLE_STATUS_SUCCESS)
  {
    BLE_DBG_HIDS_MSG("Mode %d of length %d\n", data, length);
  }
  else
  {
    BLE_DBG_HIDS_MSG ("FAILED to read handle value, Error: %02X !!\n", 
                         hciCmdResult);
  }
}
#endif


/**
 * @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 HIDS_Event_Handler(void *Event)
{
  SVCCTL_EvtAckStatus_t return_value;
  hci_event_pckt *event_pckt;
  evt_blecore_aci *blecore_evt;
  aci_gatt_attribute_modified_event_rp0    * attribute_modified;
  uint8_t service_instance;
#if (BLE_CFG_HIDS_REPORT_CHAR != 0)
  uint8_t i;
#endif

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

  switch(event_pckt->evt)
  {
    case HCI_VENDOR_SPECIFIC_DEBUG_EVT_CODE:
    {
      blecore_evt = (evt_blecore_aci*)event_pckt->data;
      switch(blecore_evt->ecode)
      {        
#if ((BLE_CFG_HIDS_REPORT_CHAR != 0) || (BLE_CFG_HIDS_KEYBOARD_DEVICE != 0) || (BLE_CFG_HIDS_MOUSE_DEVICE != 0))
        case ACI_GATT_WRITE_PERMIT_REQ_VSEVT_CODE:
        {
          aci_gatt_write_permit_req_event_rp0 * write_perm_req;
          
          write_perm_req = (aci_gatt_write_permit_req_event_rp0*)blecore_evt->data;

#if ((BLE_CFG_HIDS_REPORT_CHAR != 0) || (BLE_CFG_HIDS_KEYBOARD_DEVICE != 0) || ((BLE_CFG_HIDS_MOUSE_DEVICE != 0) && (BLE_CFG_HIDS_MOUSE_INPUT_WRITE != 0)))
          for(service_instance = 0; service_instance < BLE_CFG_HIDS_NUMBER ; service_instance++)
          {
#if (BLE_CFG_HIDS_REPORT_CHAR != 0)
            for(i = 0; i < BLE_CFG_HIDS_INPUT_REPORT_NB + BLE_CFG_HIDS_OUTPUT_REPORT_NB + BLE_CFG_HIDS_FEATURE_REPORT_NB; i++)
            {
              if(write_perm_req->Attribute_Handle == (HIDS_Context[service_instance].HidReportCharHdle[i]) + 1) 
              {
                return_value = SVCCTL_EvtAckFlowEnable;
                /**
                 * received a correct value
                 */

                aci_gatt_write_resp(write_perm_req->Connection_Handle,
                                        write_perm_req->Attribute_Handle,
                                        0x00, /* write_status = 0 (no error))*/
                                        0x00, /* err_code */
                                        write_perm_req->Data_Length,
                                        (uint8_t *)&(write_perm_req->Data[0]));
                BLE_DBG_HIDS_MSG("ACI_GATT_WRITE_PERMIT_REQ_VSEVT_CODE, HidReportCharHdle[%d]: 0x%02X, Len: 0x%02X !!\n", 
                                    i, write_perm_req->Attribute_Handle, write_perm_req->Data_Length); 
                HIDS_Handle_Report(service_instance, i, write_perm_req->Data);
              }
            }
#endif
#if (BLE_CFG_HIDS_KEYBOARD_DEVICE != 0)          
#if (BLE_CFG_HIDS_KEYBOARD_INPUT_WRITE != 0)
            if(write_perm_req->Attribute_Handle == (HIDS_Context[service_instance].HidKeyboardReportInputCharHdle) + 1)
            {
              return_value = SVCCTL_EvtAckFlowEnable;

              aci_gatt_write_resp(write_perm_req->Connection_Handle,
                                      write_perm_req->Attribute_Handle,
                                      0x00, /* write_status = 0 (no error)) */
                                      (uint8_t)0, /* err_code  */
                                      write_perm_req->Data_Length,
                                      (uint8_t *)&(write_perm_req->Data[0]));
              BLE_DBG_HIDS_MSG("ACI_GATT_WRITE_PERMIT_REQ_VSEVT_CODE, HidKeyboardReportInputCharHdle: 0x%02X, Len: 0x%02X !!\n", 
                                  write_perm_req->Attribute_Handle, write_perm_req->Data_Length); 
              HIDS_Handle_Keyboard_Input_Write(service_instance, write_perm_req->Data);
            }
#endif
            if(write_perm_req->Attribute_Handle == (HIDS_Context[service_instance].HidKeyboardReportOutputCharHdle) + 1)
            {
              return_value = SVCCTL_EvtAckFlowEnable;

              aci_gatt_write_resp(write_perm_req->Connection_Handle,
                                      write_perm_req->Attribute_Handle,
                                      0x00, /* write_status = 0 (no error)) */
                                      (uint8_t)0, /* err_code  */
                                      write_perm_req->Data_Length,
                                      (uint8_t *)&(write_perm_req->Data[0]));
              BLE_DBG_HIDS_MSG("ACI_GATT_WRITE_PERMIT_REQ_VSEVT_CODE, HidKeyboardReportOutputCharHdle: 0x%02X, Len: 0x%02X !!\n", 
                                  write_perm_req->Attribute_Handle, write_perm_req->Data_Length); 
              HIDS_Handle_Keyboard_Output_Write(service_instance, write_perm_req->Data);
            }
#endif
#if ((BLE_CFG_HIDS_MOUSE_DEVICE != 0) && (BLE_CFG_HIDS_MOUSE_INPUT_WRITE != 0))
            if(write_perm_req->Attribute_Handle == (HIDS_Context[service_instance].HidMouseReportInputCharHdle) + 1)
            {
              return_value = SVCCTL_EvtAckFlowEnable;

              aci_gatt_write_resp(write_perm_req->Connection_Handle,
                                      write_perm_req->Attribute_Handle,
                                      0x00, /* write_status = 0 (no error)) */
                                      (uint8_t)0, /* err_code */
                                      write_perm_req->Data_Length,
                                      (uint8_t *)&(write_perm_req->Data[0]));
              BLE_DBG_HIDS_MSG("ACI_GATT_WRITE_PERMIT_REQ_VSEVT_CODE, HidMouseReportInputCharHdle: 0x%02X, Len: 0x%02X !!\n", 
                                  write_perm_req->Attribute_Handle, write_perm_req->Data_Length); 
              HIDS_Handle_Mouse_Input_Write(service_instance, write_perm_req->Data);
            }
#endif
          }
#endif
        }
        break;
#endif
        case ACI_GATT_ATTRIBUTE_MODIFIED_VSEVT_CODE:
        {
          attribute_modified = (aci_gatt_attribute_modified_event_rp0*)blecore_evt->data;
          for(service_instance = 0; service_instance < BLE_CFG_HIDS_NUMBER ; service_instance++)
          {
#if (BLE_CFG_HIDS_PROTOCOL_MODE_CHAR != 0)
            /* Report characteristic write */
            if(attribute_modified->Attr_Handle == (HIDS_Context[service_instance].HidProtocolModeCharHdle) + 1)
            {
              return_value = SVCCTL_EvtAckFlowEnable;
              BLE_DBG_HIDS_MSG("ACI_GATT_ATTRIBUTE_MODIFIED_VSEVT_CODE, HidProtocolModeCharHdle: 0x%02X, Len: 0x%02X !!\n", 
                                  attribute_modified->Attr_Handle, attribute_modified->Attr_Data_Length); 
              HIDS_Handle_Protocol_Mode(service_instance, attribute_modified->Attr_Data);
            }
#endif
#if (BLE_CFG_HIDS_REPORT_CHAR != 0)
            for(i = 0; i < BLE_CFG_HIDS_INPUT_REPORT_NB + BLE_CFG_HIDS_OUTPUT_REPORT_NB + BLE_CFG_HIDS_FEATURE_REPORT_NB ; i++)
            {
              /* Report characteristic write */
              if(attribute_modified->Attr_Handle == (HIDS_Context[service_instance].HidReportCharHdle[i]) + 1)
              {
                return_value = SVCCTL_EvtAckFlowEnable;
                if(i < BLE_CFG_HIDS_INPUT_REPORT_NB)
                {
                  aci_gatt_write_resp(attribute_modified->Connection_Handle,
                                          attribute_modified->Attr_Handle,
                                          0x00, /* write_status = 0 (no error))*/
                                          0x00, /* err_code */
                                          attribute_modified->Attr_Data_Length,
                                          (uint8_t *)&(attribute_modified->Attr_Data[0]));
                }
               BLE_DBG_HIDS_MSG("ACI_GATT_ATTRIBUTE_MODIFIED_VSEVT_CODE, HidReportCharHdle[%d]: 0x%02X, Len: 0x%02X !!\n", 
                                   i, attribute_modified->Attr_Handle, attribute_modified->Attr_Data_Length); 
               HIDS_Handle_Report(service_instance, i, attribute_modified->Attr_Data);
              }
              /* Report Client Characteristic Configuration descriptor write */
              if(attribute_modified->Attr_Handle == (HIDS_Context[service_instance].HidReportCharHdle[i]) + 2)
              {
                HIDS_App_Notification_evt_t Notification;
                
                return_value = SVCCTL_EvtAckFlowEnable;
                /**
                 * Notify the application of notification setting
                 */
                Notification.Instance = service_instance;
                Notification.Index = i;
                if(attribute_modified->Attr_Data[0] & COMSVC_Notification)
                {
                  BLE_DBG_HIDS_MSG("ACI_GATT_ATTRIBUTE_MODIFIED_VSEVT_CODE HIDS_REPORT_NOTIFICATION_ENABLED\n");
                  Notification.HIDS_Evt_Opcode = HIDS_REPORT_NOTIFICATION_ENABLED;
                  HIDS_Notification(&Notification);
                }
                else
                {
                  BLE_DBG_HIDS_MSG("ACI_GATT_ATTRIBUTE_MODIFIED_VSEVT_CODE HIDS_REPORT_NOTIFICATION_DISABLED\n");
                  Notification.HIDS_Evt_Opcode = HIDS_REPORT_NOTIFICATION_DISABLED;
                  HIDS_Notification(&Notification);
                }
              }
            } /* End for(i = 0; i < HIDS_INPUT_REPORT_NB + HIDS_OUTPUT_REPORT_NB + BLE_CFG_HIDS_FEATURE_REPORT_NB ; i++) */
#endif
#if (BLE_CFG_HIDS_KEYBOARD_DEVICE != 0)
#if (BLE_CFG_HIDS_KEYBOARD_INPUT_WRITE != 0)
            /* Keyboard Report Input characteristic write */
            if(attribute_modified->Attr_Handle == (HIDS_Context[service_instance].HidKeyboardReportInputCharHdle) + 1)
            {
              return_value = SVCCTL_EvtAckFlowEnable;
              aci_gatt_write_resp(attribute_modified->Connection_Handle,
                                      attribute_modified->Attr_Handle,
                                      0x00, /* write_status = 0 (no error))*/
                                      0x00, /* err_code */
                                      attribute_modified->Attr_Data_Length,
                                      (uint8_t *)&(attribute_modified->Attr_Data[0]));
              BLE_DBG_HIDS_MSG("ACI_GATT_ATTRIBUTE_MODIFIED_VSEVT_CODE, HidKeyboardReportInputCharHdle: 0x%02X, Len: 0x%02X !!\n", 
                                  attribute_modified->Attr_Handle, attribute_modified->Attr_Data_Length); 
              HIDS_Handle_Keyboard_Input_Write(service_instance, attribute_modified->Attr_Data);
            }
#endif
            /*  Keyboard Report Input Client Characteristic Configuration descriptor write */
            if(attribute_modified->Attr_Handle == (HIDS_Context[service_instance].HidKeyboardReportInputCharHdle) + 2)
            {
              HIDS_App_Notification_evt_t Notification;
              
              return_value = SVCCTL_EvtAckFlowEnable;
              /**
               * Notify the application of write setting
               */
              Notification.Instance = service_instance;
              Notification.Index = 0;
              if(attribute_modified->Attr_Data[0] & COMSVC_Notification)
              {
                BLE_DBG_HIDS_MSG("ACI_GATT_ATTRIBUTE_MODIFIED_VSEVT_CODE HIDS_KEYB_INPUT_NOTIFY_ENABLED\n");
                Notification.HIDS_Evt_Opcode = HIDS_KEYB_INPUT_NOTIFY_ENABLED;
                HIDS_Notification(&Notification);
              }
              else
              {
                BLE_DBG_HIDS_MSG("ACI_GATT_ATTRIBUTE_MODIFIED_VSEVT_CODE HIDS_KEYB_INPUT_NOTIFY_DISABLED\n");
                Notification.HIDS_Evt_Opcode = HIDS_KEYB_INPUT_NOTIFY_DISABLED;
                HIDS_Notification(&Notification);
              }
            }
            /* Keyboard Report Output characteristic write */
            if(attribute_modified->Attr_Handle == (HIDS_Context[service_instance].HidKeyboardReportOutputCharHdle) + 1)
            {
              return_value = SVCCTL_EvtAckFlowEnable;
              BLE_DBG_HIDS_MSG("ACI_GATT_ATTRIBUTE_MODIFIED_VSEVT_CODE, HidKeyboardReportOutputCharHdle: 0x%02X, Len: 0x%02X !!\n", 
                                  attribute_modified->Attr_Handle, attribute_modified->Attr_Data_Length); 
              HIDS_Handle_Keyboard_Output_Write(service_instance, attribute_modified->Attr_Data);
            }
#endif
#if (BLE_CFG_HIDS_MOUSE_DEVICE != 0)
#if (BLE_CFG_HIDS_MOUSE_INPUT_WRITE != 0)
            /* Mouse Report Input characteristic write */
            if(attribute_modified->Attr_Handle == (HIDS_Context[service_instance].HidMouseReportInputCharHdle) + 1)
            {
              return_value = SVCCTL_EvtAckFlowEnable;
              aci_gatt_write_resp(attribute_modified->Connection_Handle,
                                      attribute_modified->Attr_Handle,
                                      0x00, /* write_status = 0 (no error))*/
                                      0x00, /* err_code */
                                      attribute_modified->Attr_Data_Length,
                                      (uint8_t *)&(attribute_modified->Attr_Data[0]));
              BLE_DBG_HIDS_MSG("ACI_GATT_ATTRIBUTE_MODIFIED_VSEVT_CODE, HidMouseReportInputCharHdle: 0x%02X, Len: 0x%02X !!\n", 
                                  attribute_modified->Attr_Handle, attribute_modified->Attr_Data_Length); 
              HIDS_Handle_Mouse_Input_Write(service_instance, attribute_modified->Attr_Data);
            }
#endif
            /*  Mouse Report Input Client Characteristic Configuration descriptor write */
            if(attribute_modified->Attr_Handle == (HIDS_Context[service_instance].HidMouseReportInputCharHdle) + 2)
            {
              HIDS_App_Notification_evt_t Notification;

              return_value = SVCCTL_EvtAckFlowEnable;
              /**
               * Notify the application of write setting
               */
              Notification.Instance = service_instance;
              Notification.Index = 0;
              if(attribute_modified->Attr_Data[0] & COMSVC_Notification)
              {
                BLE_DBG_HIDS_MSG("ACI_GATT_ATTRIBUTE_MODIFIED_VSEVT_CODE HIDS_MOUSE_INPUT_NOTIFY_ENABLED\n");
                Notification.HIDS_Evt_Opcode = HIDS_MOUSE_INPUT_NOTIFY_ENABLED;
                HIDS_Notification(&Notification);
              }
              else
              {
                BLE_DBG_HIDS_MSG("ACI_GATT_ATTRIBUTE_MODIFIED_VSEVT_CODE HIDS_MOUSE_INPUT_NOTIFY_DISABLED\n");
                Notification.HIDS_Evt_Opcode = HIDS_MOUSE_INPUT_NOTIFY_DISABLED;
                HIDS_Notification(&Notification);
              }
            }
#endif
            /*  Control Point Characteristic write */
            if(attribute_modified->Attr_Handle == (HIDS_Context[service_instance].HidControlPointCharHdle) + 1)
            {
              return_value = SVCCTL_EvtAckFlowEnable;
              BLE_DBG_HIDS_MSG("ACI_GATT_ATTRIBUTE_MODIFIED_VSEVT_CODE, HidControlPointCharHdle: 0x%02X, Len: 0x%02X !!\n", 
                                  attribute_modified->Attr_Handle, attribute_modified->Attr_Data_Length); 
              HIDS_Handle_Control_Point_Write(0/*service_instance*/, attribute_modified->Attr_Data);
            }
          } /* End for(service_instance = 0; service_instance < BLE_CFG_HIDS_NUMBER ; service_instance++) */
        }
        break;

        default:
          break;
      }
    }
    break; /* HCI_HCI_VENDOR_SPECIFIC_DEBUG_EVT_CODE_SPECIFIC */

    default:
      break;
  }

  return(return_value);
}/* end HIDS_Event_Handler */


/* Public functions ----------------------------------------------------------*/

/**
 * @brief  Service initialization
 * @param  None
 * @retval None
 */
void HIDS_Init(void)
{
  uint16_t uuid;
  uint8_t service_instance;
#if (BLE_CFG_HIDS_REPORT_CHAR != 0)
  uint8_t i;
#endif
  tBleStatus hciCmdResult;
#if (BLE_CFG_HIDS_PROTOCOL_MODE_CHAR != 0)
  uint8_t protocol_mode;
#endif

  /**
   *	Register the event handler to the BLE controller
   */
  SVCCTL_RegisterSvcHandler(HIDS_Event_Handler);

  for(service_instance = 0; service_instance < BLE_CFG_HIDS_NUMBER; service_instance++)
  {
    /**
     *  Add Human Interface Device Service
     *
     * Max_Attribute_Records = 2*no_of_char + 1
     * service_max_attribute_record = 1 for HID service +
     *                                2 for protocol mode characteristic +
     *                                2 report characteristic +
     *                                1 for client char configuration descriptor +
     *                                1 for report reference descriptor +
     *                                2 for report map characteristic +
     *                                2 boot keyboard input report characteristic +
     *                                1 for client char configuration descriptor +
     *                                2 boot keyboard output report characteristic +
     *                                2 boot mouse input report characteristic +
     *                                1 for client char configuration descriptor +
     *                                2 for HID information characteristic +
     *                                2 for HID control point characteristic
     */
    uuid = HUMAN_INTERFACE_DEVICE_SERVICE_UUID;
    hciCmdResult = aci_gatt_add_service(UUID_TYPE_16,
                                        (Service_UUID_t *) &uuid,
                                        PRIMARY_SERVICE,
#if (BLE_CFG_HIDS_PROTOCOL_MODE_CHAR != 0)
                                        2+
#endif
#if (BLE_CFG_HIDS_REPORT_CHAR != 0)
                                        (4*BLE_CFG_HIDS_INPUT_REPORT_NB)+
                                        (3*BLE_CFG_HIDS_OUTPUT_REPORT_NB)+
                                        (3*BLE_CFG_HIDS_FEATURE_REPORT_NB)+
#endif
#if (BLE_CFG_HIDS_EXTERNAL_REPORT_REFERENCE != 0)
                                         1+
#endif
#if (BLE_CFG_HIDS_KEYBOARD_DEVICE != 0)
                                         5+
#endif
#if (BLE_CFG_HIDS_MOUSE_DEVICE != 0)
                                         3+
#endif
                                         1 + 2 + 2 + 2,      /* Service + Report Map + HID Information + HID Control Point */
                                         &(HIDS_Context[service_instance].HidSvcHdle));
    if (hciCmdResult == BLE_STATUS_SUCCESS)
    {
      BLE_DBG_HIDS_MSG ("Human Interface Device Service (HIDS) is added Successfully %04X\n", 
                           HIDS_Context[service_instance].HidSvcHdle);
    }
    else
    {
      BLE_DBG_HIDS_MSG ("FAILED to add Human Interface Device Service (HIDS), Error: %02X !!\n", 
                           hciCmdResult);
    }


#if (BLE_CFG_HIDS_PROTOCOL_MODE_CHAR != 0)
    /**
     *  Add Protocol Mode Characteristic
     */
    uuid = PROTOCOL_MODE_CHAR_UUID;
    hciCmdResult = aci_gatt_add_char(HIDS_Context[service_instance].HidSvcHdle,
                                     UUID_TYPE_16,
                                     (Char_UUID_t *) &uuid ,
                                     1,
                                     CHAR_PROP_READ | CHAR_PROP_WRITE_WITHOUT_RESP,
                                     ATTR_PERMISSION_NONE,
                                     GATT_NOTIFY_ATTRIBUTE_WRITE, /* gattEvtMask */
                                     10, /* encryKeySize */
                                     CHAR_VALUE_LEN_CONSTANT,
                                     &(HIDS_Context[service_instance].HidProtocolModeCharHdle));
    if (hciCmdResult == BLE_STATUS_SUCCESS)
    {
      BLE_DBG_HIDS_MSG ("Protocol Mode Characterisitic is added Successfully %04X\n", 
                           HIDS_Context[service_instance].HidProtocolModeCharHdle);
    }
    else
    {
      BLE_DBG_HIDS_MSG ("FAILED to add Protocol Mode Characterisitic, Error: %02X !!\n", 
                           hciCmdResult);
    }
    protocol_mode = BLE_CFG_HIDS_PROTOCOL_MODE;
    hciCmdResult = aci_gatt_update_char_value(HIDS_Context[service_instance].HidSvcHdle, 
                                              HIDS_Context[service_instance].HidProtocolModeCharHdle, 
                                              (uint8_t)0,
                                              (uint8_t)1, 
                                              (uint8_t *)&protocol_mode);
    
    if (hciCmdResult == BLE_STATUS_SUCCESS)
    {
      BLE_DBG_HIDS_MSG ("Protocol mode charact update is Successfully %04X\n", 
                           HIDS_Context[service_instance].HidProtocolModeCharHdle);
    }
    else 
    {
      BLE_DBG_HIDS_MSG ("FAILED to update Protocol mode charact, Error: %02X !!\n", 
                           hciCmdResult);
    }   
#endif

#if (BLE_CFG_HIDS_REPORT_CHAR != 0)
    /**
     *  Add Report Characteristic
     */
    for(i = 0; 
        i < (BLE_CFG_HIDS_INPUT_REPORT_NB + BLE_CFG_HIDS_OUTPUT_REPORT_NB + BLE_CFG_HIDS_FEATURE_REPORT_NB); 
        i++)
    {      
      if(i < BLE_CFG_HIDS_INPUT_REPORT_NB)
      {        
        uint8_t buf[2] = {0,0};
        
        uuid = REPORT_CHAR_UUID;
        hciCmdResult = aci_gatt_add_char(HIDS_Context[service_instance].HidSvcHdle,
                                         UUID_TYPE_16,
                                         (Char_UUID_t *) &uuid ,
                                         BLE_CFG_HIDS_REPORT_MAX_LEN, /* bytes */
                                         CHAR_PROP_READ | CHAR_PROP_NOTIFY
#if (BLE_CFG_HIDS_INPUT_WRITE != 0)
                                         | CHAR_PROP_WRITE,
#else
                                         ,
#endif
                                         ATTR_PERMISSION_NONE,
#if (BLE_CFG_HIDS_INPUT_WRITE != 0)
                                         GATT_NOTIFY_WRITE_REQ_AND_WAIT_FOR_APPL_RESP,
#else
                                         GATT_DONT_NOTIFY_EVENTS, /* gattEvtMask */
#endif
                                         10, /* encryKeySize */
                                         CHAR_VALUE_LEN_VARIABLE,
                                         &(HIDS_Context[service_instance].HidReportCharHdle[i]));
        if (hciCmdResult == BLE_STATUS_SUCCESS)
        {
          BLE_DBG_HIDS_MSG ("Input Report Characterisitic is added Successfully %04X\n", 
                               HIDS_Context[service_instance].HidReportCharHdle[i]);
        }
        else
        {
          BLE_DBG_HIDS_MSG ("FAILED to add Input Report Characterisitic, Error: %02X !!\n", 
                               hciCmdResult);
        }
        
        uuid = REPORT_REFERENCE_DESCRIPTOR_UUID;
        buf[0] = i;
        buf[1] = 1; /* Input Report */
        /* add the valid descriptor */
        hciCmdResult = aci_gatt_add_char_desc(HIDS_Context[service_instance].HidSvcHdle, 
                                              HIDS_Context[service_instance].HidReportCharHdle[i], 
                                              UUID_TYPE_16,
                                              (Char_Desc_Uuid_t *)&uuid, 
                                              BLE_CFG_HIDS_REPORT_REFERENCE_LEN,
                                              BLE_CFG_HIDS_REPORT_REFERENCE_LEN,
                                              (void *)&buf,
                                              ATTR_PERMISSION_NONE,
                                              ATTR_ACCESS_READ_ONLY,
                                              GATT_DONT_NOTIFY_EVENTS,
                                              MIN_ENCRY_KEY_SIZE, 
                                              CHAR_VALUE_LEN_CONSTANT,
                                              &HIDS_Context[service_instance].HidReportReferenceDescHdle[i]);
        if (hciCmdResult == BLE_STATUS_SUCCESS)
        {
          BLE_DBG_HIDS_MSG ("Input Report Reference Descriptor is added Successfully %04X\n", 
                               HIDS_Context[service_instance].HidReportReferenceDescHdle[i]);
        }
        else
        {
          BLE_DBG_HIDS_MSG ("FAILED to add Input Report Reference Descriptor, Error: %02X !!\n", 
                               hciCmdResult);
        }
      }
      else if((i - BLE_CFG_HIDS_INPUT_REPORT_NB) < BLE_CFG_HIDS_OUTPUT_REPORT_NB)
      {
        uint8_t buf[2] = {0,0};
        
        uuid = REPORT_CHAR_UUID;
        hciCmdResult = aci_gatt_add_char(HIDS_Context[service_instance].HidSvcHdle,
                                         UUID_TYPE_16,
                                         (Char_UUID_t *) &uuid ,
                                         BLE_CFG_HIDS_REPORT_MAX_LEN, /* bytes */
                                         CHAR_PROP_READ | CHAR_PROP_WRITE | CHAR_PROP_WRITE_WITHOUT_RESP,
                                         ATTR_PERMISSION_NONE,
                                         GATT_NOTIFY_WRITE_REQ_AND_WAIT_FOR_APPL_RESP, /* gattEvtMask */
                                         10, /* encryKeySize */
                                         CHAR_VALUE_LEN_VARIABLE,
                                         &(HIDS_Context[service_instance].HidReportCharHdle[i]));
        if (hciCmdResult == BLE_STATUS_SUCCESS)
        {
          BLE_DBG_HIDS_MSG ("Output Report Characterisitic is added Successfully %04X\n", 
                               HIDS_Context[service_instance].HidReportReferenceDescHdle[i]);
        }
        else
        {
          BLE_DBG_HIDS_MSG ("FAILED to add Output Report Characterisitic, Error: %02X !!\n", 
                               hciCmdResult);
        }

        uuid = REPORT_REFERENCE_DESCRIPTOR_UUID;
        buf[0] = i;
        buf[1] = 2; /* Output Report */
        /* add the valid descriptor */
        hciCmdResult = aci_gatt_add_char_desc(HIDS_Context[service_instance].HidSvcHdle, 
                                              HIDS_Context[service_instance].HidReportCharHdle[i], 
                                              UUID_TYPE_16,
                                              (Char_Desc_Uuid_t *)&uuid, 
                                              BLE_CFG_HIDS_REPORT_REFERENCE_LEN,
                                              BLE_CFG_HIDS_REPORT_REFERENCE_LEN,
                                              (void *)&buf,
                                              ATTR_PERMISSION_NONE,
                                              ATTR_ACCESS_READ_ONLY,
                                              GATT_DONT_NOTIFY_EVENTS,
                                              MIN_ENCRY_KEY_SIZE, 
                                              CHAR_VALUE_LEN_CONSTANT,
                                              &HIDS_Context[service_instance].HidReportReferenceDescHdle[i]);
        if (hciCmdResult == BLE_STATUS_SUCCESS)
        {
          BLE_DBG_HIDS_MSG ("Output Report Reference Descriptor is added Successfully %04X\n", 
                               HIDS_Context[service_instance].HidReportReferenceDescHdle[i]);
        }
        else
        {
          BLE_DBG_HIDS_MSG ("FAILED to add Output Report Reference Descriptor, Error: %02X !!\n", 
                               hciCmdResult);
        }
      }
      else if((i - BLE_CFG_HIDS_INPUT_REPORT_NB - BLE_CFG_HIDS_OUTPUT_REPORT_NB) < BLE_CFG_HIDS_FEATURE_REPORT_NB)
      {
        uint8_t buf[2] = {0,0};
        
        uuid = REPORT_CHAR_UUID;
        hciCmdResult = aci_gatt_add_char(HIDS_Context[service_instance].HidSvcHdle,
                                         UUID_TYPE_16,
                                         (Char_UUID_t *) &uuid ,
                                         BLE_CFG_HIDS_REPORT_MAX_LEN, /* bytes */
                                         CHAR_PROP_READ | CHAR_PROP_WRITE,
                                         ATTR_PERMISSION_NONE,
                                         GATT_NOTIFY_WRITE_REQ_AND_WAIT_FOR_APPL_RESP, /* gattEvtMask */
                                         10, /* encryKeySize */
                                         CHAR_VALUE_LEN_VARIABLE,
                                         &(HIDS_Context[service_instance].HidReportCharHdle[i]));
        if (hciCmdResult == BLE_STATUS_SUCCESS)
        {
          BLE_DBG_HIDS_MSG ("Feature Report Characterisitic is added Successfully %04X\n", 
                               HIDS_Context[service_instance].HidReportReferenceDescHdle[i]);
        }
        else
        {
          BLE_DBG_HIDS_MSG ("FAILED to add Feature Report Characterisitic, Error: %02X !!\n", 
                               hciCmdResult);
        }

        uuid = REPORT_REFERENCE_DESCRIPTOR_UUID;
        buf[0] = i;
        buf[1] = 3; /* Input Report */
        /* add the valid descriptor */
        hciCmdResult = aci_gatt_add_char_desc(HIDS_Context[service_instance].HidSvcHdle, 
                                              HIDS_Context[service_instance].HidReportCharHdle[i], 
                                              UUID_TYPE_16,
                                              (Char_Desc_Uuid_t*)&uuid, 
                                              BLE_CFG_HIDS_REPORT_REFERENCE_LEN,
                                              BLE_CFG_HIDS_REPORT_REFERENCE_LEN,
                                              (void *)&buf,
                                              ATTR_PERMISSION_NONE,
                                              ATTR_ACCESS_READ_ONLY,
                                              GATT_DONT_NOTIFY_EVENTS,
                                              MIN_ENCRY_KEY_SIZE, 
                                              CHAR_VALUE_LEN_CONSTANT,
                                              &HIDS_Context[service_instance].HidReportReferenceDescHdle[i]);
        if (hciCmdResult == BLE_STATUS_SUCCESS)
        {
          BLE_DBG_HIDS_MSG ("Feature Report Reference Descriptor is added Successfully %04X\n", 
                               HIDS_Context[service_instance].HidReportReferenceDescHdle[i]);
        }
        else
        {
          BLE_DBG_HIDS_MSG ("FAILED to add Feature Report Reference Descriptor, Error: %02X !!\n", 
                               hciCmdResult);
        }
      }
    }
#endif

    /**
     *  Add Report Map Characteristic
     */
    uuid = REPORT_MAP_CHAR_UUID;
    hciCmdResult = aci_gatt_add_char(HIDS_Context[service_instance].HidSvcHdle,
                                     UUID_TYPE_16,
                                     (Char_UUID_t *) &uuid ,
                                     BLE_CFG_HIDS_REPORT_MAP_MAX_LEN,
                                     CHAR_PROP_READ,
                                     ATTR_PERMISSION_NONE,
                                     GATT_DONT_NOTIFY_EVENTS, /* gattEvtMask */
                                     10, /* encryKeySize */
                                     CHAR_VALUE_LEN_VARIABLE,
                                     &(HIDS_Context[service_instance].HidReportMapCharHdle));
    if (hciCmdResult == BLE_STATUS_SUCCESS)
    {
      BLE_DBG_HIDS_MSG ("Report Map Characterisitic is added Successfully %04X\n", 
                           HIDS_Context[service_instance].HidReportMapCharHdle);
    }
    else
    {
      BLE_DBG_HIDS_MSG ("FAILED to add Report Map Characterisitic, Error: %02X !!\n", 
                           hciCmdResult);
    }

#if (BLE_CFG_HIDS_EXTERNAL_REPORT_REFERENCE != 0)
    {
      uint8_t buf[2] = {0,0};
      
      uuid = EXTERNAL_REPORT_REFERENCE_DESCRIPTOR_UUID;
      STORE_LE_16(buf, 0);    /* Characteristic UUID  */
      /* add the valid descriptor */
      hciCmdResult = aci_gatt_add_char_desc(HIDS_Context[service_instance].HidSvcHdle, 
                                            HIDS_Context[service_instance].HidReportMapCharHdle, 
                                            UUID_TYPE_16,
                                            (Char_Desc_Uuid_t *)&uuid, 
                                            BLE_CFG_HIDS_EXTERNAL_REPORT_REFERENCE_LEN,
                                            BLE_CFG_HIDS_EXTERNAL_REPORT_REFERENCE_LEN,
                                            (void *)&buf,
                                            ATTR_PERMISSION_NONE,
                                            ATTR_ACCESS_READ_ONLY,
                                            GATT_DONT_NOTIFY_EVENTS,
                                            MIN_ENCRY_KEY_SIZE, 
                                            CHAR_VALUE_LEN_CONSTANT,
                                            &HIDS_Context[service_instance].HidExternalReferenceDescHdle);
      if (hciCmdResult == BLE_STATUS_SUCCESS)
      {
        BLE_DBG_HIDS_MSG ("External Report Reference Descriptor is added Successfully %04X\n", 
                             HIDS_Context[service_instance].HidExternalReferenceDescHdle);
      }
      else
      {
        BLE_DBG_HIDS_MSG ("FAILED to add External Report Reference Descriptor, Error: %02X !!\n", 
                             hciCmdResult);
      }
    }
#endif

#if (BLE_CFG_HIDS_KEYBOARD_DEVICE != 0)
    /**
     *  Add Boot Keyboard Input Report Characteristic
     */
    uuid = BOOT_KEYBOARD_INPUT_REPORT_CHAR_UUID;
    hciCmdResult = aci_gatt_add_char(HIDS_Context[service_instance].HidSvcHdle,
                                     UUID_TYPE_16,
                                     (Char_UUID_t *) &uuid ,
                                     BLE_CFG_HIDS_BOOT_KEYBOARD_INPUT_REPORT_MAX_LEN,
                                     CHAR_PROP_READ | CHAR_PROP_NOTIFY
#if (BLE_CFG_HIDS_KEYBOARD_INPUT_WRITE != 0)
                                     | CHAR_PROP_WRITE,
#else
                                     ,
#endif
                                     ATTR_PERMISSION_NONE,
                                     GATT_NOTIFY_WRITE_REQ_AND_WAIT_FOR_APPL_RESP, /* gattEvtMask */
                                     10, /* encryKeySize */
                                     CHAR_VALUE_LEN_VARIABLE,
                                     &(HIDS_Context[service_instance].HidKeyboardReportInputCharHdle));
    if (hciCmdResult == BLE_STATUS_SUCCESS)
    {
      BLE_DBG_HIDS_MSG ("Boot Keyboard Input Report Characterisitic is added Successfully %04X\n", 
                           HIDS_Context[service_instance].HidKeyboardReportInputCharHdle);
    }
    else
    {
      BLE_DBG_HIDS_MSG ("FAILED to add Boot Keyboard Input Report Characterisitic, Error: %02X !!\n", 
                           hciCmdResult);
    }

    /**
     *  Add Boot Keyboard Output Report Characteristic
     */
    uuid = BOOT_KEYBOARD_OUTPUT_REPORT_CHAR_UUID;
    hciCmdResult = aci_gatt_add_char(HIDS_Context[service_instance].HidSvcHdle,
                                     UUID_TYPE_16,
                                     (Char_UUID_t *) &uuid ,
                                     BLE_CFG_HIDS_BOOT_KEYBOARD_OUTPUT_REPORT_MAX_LEN,
                                     CHAR_PROP_READ | CHAR_PROP_WRITE | CHAR_PROP_WRITE_WITHOUT_RESP,
                                     ATTR_PERMISSION_NONE,
                                     GATT_NOTIFY_WRITE_REQ_AND_WAIT_FOR_APPL_RESP, /* gattEvtMask */
                                     10, /* encryKeySize */
                                     CHAR_VALUE_LEN_VARIABLE,
                                     &(HIDS_Context[service_instance].HidKeyboardReportOutputCharHdle));
    if (hciCmdResult == BLE_STATUS_SUCCESS)
    {
      BLE_DBG_HIDS_MSG ("Boot Keyboard Output Report Characterisitic is added Successfully %04X\n", 
                           HIDS_Context[service_instance].HidKeyboardReportOutputCharHdle);
    }
    else
    {
      BLE_DBG_HIDS_MSG ("FAILED to add Boot Keyboard Output Report Characterisitic, Error: %02X !!\n", 
                           hciCmdResult);
    }
#endif
#if (BLE_CFG_HIDS_MOUSE_DEVICE != 0) 
    /**
     *  Add Boot Mouse Input Report Characteristic
     */
    uuid = BOOT_MOUSE_INPUT_REPORT_CHAR_UUID;
    hciCmdResult = aci_gatt_add_char(HIDS_Context[service_instance].HidSvcHdle,
                                     UUID_TYPE_16,
                                     (Char_UUID_t *) &uuid ,
                                     BLE_CFG_HIDS_BOOT_MOUSE_INPUT_REPORT_MAX_LEN,
                                     CHAR_PROP_READ | CHAR_PROP_NOTIFY
#if (BLE_CFG_HIDS_MOUSE_INPUT_WRITE != 0)
                                     | CHAR_PROP_WRITE,
#else
                                     ,
#endif
                                     ATTR_PERMISSION_NONE,
#if (BLE_CFG_HIDS_MOUSE_INPUT_WRITE != 0)
                                     GATT_NOTIFY_WRITE_REQ_AND_WAIT_FOR_APPL_RESP,
#else
                                     GATT_DONT_NOTIFY_EVENTS, /* gattEvtMask */
#endif
                                     10, /* encryKeySize */
                                     CHAR_VALUE_LEN_VARIABLE,
                                     &(HIDS_Context[service_instance].HidMouseReportInputCharHdle));
    if (hciCmdResult == BLE_STATUS_SUCCESS)
    {
      BLE_DBG_HIDS_MSG ("Boot Mouse Input Report Characterisitic is added Successfully %04X\n", 
                           HIDS_Context[service_instance].HidMouseReportInputCharHdle);
    }
    else
    {
      BLE_DBG_HIDS_MSG ("FAILED to add Boot Mouse Input Report Characterisitic, Error: %02X !!\n", 
                           hciCmdResult);
    }
#endif

    uuid = HID_INFORMATION_CHAR_UUID;
    hciCmdResult = aci_gatt_add_char(HIDS_Context[service_instance].HidSvcHdle,
                                     UUID_TYPE_16,
                                     (Char_UUID_t *) &uuid ,
                                     BLE_CFG_HIDS_INFORMATION_LEN, /* bytes */
                                     CHAR_PROP_READ,
                                     ATTR_PERMISSION_NONE,
                                     GATT_DONT_NOTIFY_EVENTS, /* gattEvtMask */
                                     10, /* encryKeySize */
                                     CHAR_VALUE_LEN_CONSTANT,
                                     &(HIDS_Context[service_instance].HidInformationCharHdle));
    if (hciCmdResult == BLE_STATUS_SUCCESS)
    {
      BLE_DBG_HIDS_MSG ("HID Information Characterisitic is added Successfully %04X\n", 
                           HIDS_Context[service_instance].HidInformationCharHdle);
    }
    else
    {
      BLE_DBG_HIDS_MSG ("FAILED to add HID Information Characterisitic, Error: %02X !!\n", 
                           hciCmdResult);
    }

    uuid = HID_CONTROL_POINT_CHAR_UUID;
    hciCmdResult = aci_gatt_add_char(HIDS_Context[service_instance].HidSvcHdle,
                                     UUID_TYPE_16,
                                     (Char_UUID_t *) &uuid ,
                                     BLE_CFG_HIDS_CONTROL_POINT_LEN, /* bytes */
                                     CHAR_PROP_WRITE_WITHOUT_RESP,
                                     ATTR_PERMISSION_NONE,
                                     GATT_NOTIFY_ATTRIBUTE_WRITE, /* gattEvtMask */
                                     10, /* encryKeySize */
                                     CHAR_VALUE_LEN_CONSTANT,
                                     &(HIDS_Context[service_instance].HidControlPointCharHdle));
    if (hciCmdResult == BLE_STATUS_SUCCESS)
    {
      BLE_DBG_HIDS_MSG ("HID Control Point Characterisitic is added Successfully %04X\n", 
                           HIDS_Context[service_instance].HidControlPointCharHdle);
    }
    else
    {
      BLE_DBG_HIDS_MSG ("FAILED to add HID Control Point Characterisitic, Error: %02X !!\n", 
                           hciCmdResult);
    }

  }

  return;
}

/**
 * @brief  Characteristic update
 * @param  UUID: UUID of the characteristic
 * @param  service_instance: Instance of the service to which the characteristic belongs
 * @retval BodySensorLocationValue: The new value to be written
 */
tBleStatus HIDS_Update_Char(uint16_t UUID, 
                                   uint8_t service_instance, 
                                   uint8_t Report_Index, 
                                   uint8_t report_size,
                                   uint8_t *pPayload)
{
  tBleStatus return_value = BLE_STATUS_FAILED;
  switch(UUID)
  {
#if (BLE_CFG_HIDS_REPORT_CHAR != 0)
    case REPORT_CHAR_UUID:
      {
        return_value = aci_gatt_update_char_value(HIDS_Context[service_instance].HidSvcHdle,
                                                  HIDS_Context[service_instance].HidReportCharHdle[Report_Index],
                                                  0, /* charValOffset */
                                                  report_size,
                                                  pPayload);
      }
      break;
#endif
    case REPORT_MAP_CHAR_UUID:
      {
        return_value = aci_gatt_update_char_value(HIDS_Context[service_instance].HidSvcHdle,
                                                  HIDS_Context[service_instance].HidReportMapCharHdle,
                                                  0, /* charValOffset */
                                                  report_size,
                                                  pPayload);
      }
      break;
#if (BLE_CFG_HIDS_KEYBOARD_DEVICE != 0)          
    case BOOT_KEYBOARD_INPUT_REPORT_CHAR_UUID:
      {
        return_value = aci_gatt_update_char_value(HIDS_Context[service_instance].HidSvcHdle,
                                                  HIDS_Context[service_instance].HidKeyboardReportInputCharHdle,
                                                  0, /* charValOffset */
                                                  report_size,
                                                  pPayload);
      }
      break;
#endif
#if (BLE_CFG_HIDS_MOUSE_DEVICE != 0)          
    case BOOT_MOUSE_INPUT_REPORT_CHAR_UUID:
      {
        return_value = aci_gatt_update_char_value(HIDS_Context[service_instance].HidSvcHdle,
                                                  HIDS_Context[service_instance].HidMouseReportInputCharHdle,
                                                  0, /* charValOffset */
                                                  report_size,
                                                  pPayload);
      }
      break;
#endif

    case HID_INFORMATION_CHAR_UUID:
      {
        return_value = aci_gatt_update_char_value(HIDS_Context[service_instance].HidSvcHdle,
                                                  HIDS_Context[service_instance].HidInformationCharHdle,
                                                  0, /* charValOffset */
                                                  report_size,
                                                  pPayload);
      }
      break;
    default:
      break;
  }

  return return_value;
}/* end HIDS_Update_Char() */


/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/