-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperipheral.c
1668 lines (1442 loc) · 49.1 KB
/
peripheral.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/******************************************************************************
@file peripheral.c
@brief GAP Peripheral Role for RTOS Applications
Group: WCS, BTS
Target Device: CC2650, CC2640, CC1350
******************************************************************************
Copyright (c) 2009-2016, Texas Instruments Incorporated
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of Texas Instruments Incorporated nor the names of
its contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
******************************************************************************
Release Name: ble_sdk_2_02_01_18
Release Date: 2016-10-26 15:20:04
*****************************************************************************/
/*********************************************************************
* INCLUDES
*/
#include <string.h>
#include <xdc/std.h>
#include <xdc/runtime/Error.h>
#include <xdc/runtime/System.h>
#include <ti/sysbios/BIOS.h>
#include <ti/sysbios/knl/Task.h>
#include <ti/sysbios/knl/Clock.h>
#ifdef ICALL_EVENTS
#include <ti/sysbios/knl/Event.h>
#else // ICALL_EVENTS
#include <ti/sysbios/knl/Semaphore.h>
#endif //ICALL_EVENTS
#include <ti/sysbios/knl/Queue.h>
#include <driverlib/ioc.h>
#include "gap.h"
#include "gatt.h"
#include "hci_tl.h"
#include "linkdb.h"
#include "util.h"
#include "gattservapp.h"
#include "peripheral.h"
#include "gapbondmgr.h"
#include "osal_snv.h"
#include "icall_apimsg.h"
/*********************************************************************
* MACROS
*/
/*********************************************************************
* CONSTANTS
*/
// Profile Events
#ifdef ICALL_EVENTS
#define GAPROLE_ICALL_EVT ICALL_MSG_EVENT_ID //Event_Id_31
#define START_ADVERTISING_EVT Event_Id_00
#define START_CONN_UPDATE_EVT Event_Id_01
#define CONN_PARAM_TIMEOUT_EVT Event_Id_02
#define GAPROLE_ALL_EVENTS (GAPROLE_ICALL_EVT | START_ADVERTISING_EVT | \
START_CONN_UPDATE_EVT | CONN_PARAM_TIMEOUT_EVT)
#else //!ICALL_EVENTS
#define START_ADVERTISING_EVT 0x0001 // Start Advertising
#define START_CONN_UPDATE_EVT 0x0002 // Start Connection Update Procedure
#define CONN_PARAM_TIMEOUT_EVT 0x0004 // Connection Parameters Update Timeout
#endif //ICALL_EVENTS
#define DEFAULT_ADVERT_OFF_TIME 30000 // 30 seconds
#define DEFAULT_MIN_CONN_INTERVAL 0x0006 // 100 milliseconds
#define DEFAULT_MAX_CONN_INTERVAL 0x0C80 // 4 seconds
#define MIN_CONN_INTERVAL 0x0006
#define MAX_CONN_INTERVAL 0x0C80
#define DEFAULT_TIMEOUT_MULTIPLIER 1000
#define CONN_INTERVAL_MULTIPLIER 6
#define MIN_SLAVE_LATENCY 0
#define MAX_SLAVE_LATENCY 500
#define MIN_TIMEOUT_MULTIPLIER 0x000a
#define MAX_TIMEOUT_MULTIPLIER 0x0c80
#define MAX_TIMEOUT_VALUE 0xFFFF
// Task configuration
#define GAPROLE_TASK_PRIORITY 3
#ifndef GAPROLE_TASK_STACK_SIZE
#define GAPROLE_TASK_STACK_SIZE 440
#endif
/*********************************************************************
* TYPEDEFS
*/
typedef struct
{
uint8_t paramUpdateEnable;
uint16_t minConnInterval;
uint16_t maxConnInterval;
uint16_t slaveLatency;
uint16_t timeoutMultiplier;
} gapRole_updateConnParams_t;
/*********************************************************************
* GLOBAL VARIABLES
*/
// Link DB maximum number of connections
uint8 linkDBNumConns;
/*********************************************************************
* EXTERNAL VARIABLES
*/
/*********************************************************************
* EXTERNAL FUNCTIONS
*/
/*********************************************************************
* LOCAL VARIABLES
*/
// Entity ID globally used to check for source and/or destination of messages
static ICall_EntityID selfEntity;
#ifdef ICALL_EVENTS
static ICall_SyncHandle syncEvent;
#else //!ICALL_EVENTS
// Semaphore globally used to post events to the application thread
static ICall_Semaphore sem;
// Task pending events
static uint16_t events = 0;
#endif //ICALL_EVENTS
// Clock object used to signal timeout
static Clock_Struct startAdvClock;
static Clock_Struct startUpdateClock;
static Clock_Struct updateTimeoutClock;
// Task setup
Task_Struct gapRoleTask;
Char gapRoleTaskStack[GAPROLE_TASK_STACK_SIZE];
static gaprole_States_t gapRole_state;
/*********************************************************************
* Profile Parameters - reference GAPROLE_PROFILE_PARAMETERS for
* descriptions
*/
static uint8_t gapRole_profileRole;
static uint8_t gapRole_IRK[KEYLEN];
static uint8_t gapRole_SRK[KEYLEN];
static uint32_t gapRole_signCounter;
static uint8_t gapRole_bdAddr[B_ADDR_LEN];
static uint8_t gapRole_AdvEnabled = TRUE;
static uint8_t gapRole_AdvNonConnEnabled = FALSE;
static uint16_t gapRole_AdvertOffTime = DEFAULT_ADVERT_OFF_TIME;
static uint8_t gapRole_AdvertDataLen = 3;
static uint8_t gapRole_AdvertData[B_MAX_ADV_LEN] =
{
0x02, // length of this data
GAP_ADTYPE_FLAGS, // AD Type = Flags
// Limited Discoverable & BR/EDR not supported
(GAP_ADTYPE_FLAGS_GENERAL | GAP_ADTYPE_FLAGS_BREDR_NOT_SUPPORTED),
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
};
static uint8_t gapRole_ScanRspDataLen = 0;
static uint8_t gapRole_ScanRspData[B_MAX_ADV_LEN] = {0};
static uint8_t gapRole_AdvEventType;
static uint8_t gapRole_AdvDirectType;
static uint8_t gapRole_AdvDirectAddr[B_ADDR_LEN] = {0};
static uint8_t gapRole_AdvChanMap;
static uint8_t gapRole_AdvFilterPolicy;
static uint16_t gapRole_ConnectionHandle = INVALID_CONNHANDLE;
static uint8_t gapRole_ConnectedDevAddr[B_ADDR_LEN] = {0};
// Connection parameter update parameters.
static gapRole_updateConnParams_t gapRole_updateConnParams =
{
// Default behavior is to accept the remote device's request until application
// changes local parameters.
.paramUpdateEnable = GAPROLE_LINK_PARAM_UPDATE_WAIT_REMOTE_PARAMS,
.minConnInterval = DEFAULT_MIN_CONN_INTERVAL,
.maxConnInterval = DEFAULT_MAX_CONN_INTERVAL,
.slaveLatency = MIN_SLAVE_LATENCY,
.timeoutMultiplier = DEFAULT_TIMEOUT_MULTIPLIER
};
static uint16_t gapRole_ConnInterval = 0;
static uint16_t gapRole_ConnSlaveLatency = 0;
static uint16_t gapRole_ConnTimeout = 0;
static uint8_t gapRole_ConnectedDevAddrType = 0;
static uint8_t gapRole_ConnTermReason = 0;
static uint8_t paramUpdateNoSuccessOption = GAPROLE_NO_ACTION;
// Application callbacks
static gapRolesCBs_t *pGapRoles_AppCGs = NULL;
static gapRolesParamUpdateCB_t *pGapRoles_ParamUpdateCB = NULL;
/*********************************************************************
* Profile Attributes - variables
*/
/*********************************************************************
* Profile Attributes - Table
*/
/*********************************************************************
* LOCAL FUNCTIONS
*/
static void gapRole_init(void);
static void gapRole_taskFxn(UArg a0, UArg a1);
static void gapRole_processStackMsg(ICall_Hdr *pMsg);
static void gapRole_processGAPMsg(gapEventHdr_t *pMsg);
static void gapRole_SetupGAP(void);
static void gapRole_HandleParamUpdateNoSuccess(void);
static bStatus_t gapRole_startConnUpdate(uint8_t handleFailure,
gapRole_updateConnParams_t *pConnParams);
static void gapRole_setEvent(uint32_t event);
/*********************************************************************
* CALLBACKS
*/
void gapRole_clockHandler(UArg a0);
/*********************************************************************
* PUBLIC FUNCTIONS
*/
/*********************************************************************
* @brief Set a GAP Role parameter.
*
* Public function defined in peripheral.h.
*/
bStatus_t GAPRole_SetParameter(uint16_t param, uint8_t len, void *pValue)
{
bStatus_t ret = SUCCESS;
switch (param)
{
case GAPROLE_IRK:
if (len == KEYLEN)
{
VOID memcpy(gapRole_IRK, pValue, KEYLEN) ;
}
else
{
ret = bleInvalidRange;
}
break;
case GAPROLE_SRK:
if (len == KEYLEN)
{
VOID memcpy(gapRole_SRK, pValue, KEYLEN) ;
}
else
{
ret = bleInvalidRange;
}
break;
case GAPROLE_SIGNCOUNTER:
if (len == sizeof (uint32_t))
{
gapRole_signCounter = *((uint32_t*)pValue);
}
else
{
ret = bleInvalidRange;
}
break;
case GAPROLE_ADVERT_ENABLED:
if (len == sizeof(uint8_t))
{
// Non-connectable advertising must be disabled.
if (gapRole_AdvNonConnEnabled != TRUE)
{
uint8_t oldAdvEnabled = gapRole_AdvEnabled;
gapRole_AdvEnabled = *((uint8_t*)pValue);
if ((oldAdvEnabled) && (gapRole_AdvEnabled == FALSE))
{
// Turn off advertising.
if ((gapRole_state == GAPROLE_ADVERTISING)
|| (gapRole_state == GAPROLE_WAITING_AFTER_TIMEOUT))
{
VOID GAP_EndDiscoverable(selfEntity);
}
}
else if ((oldAdvEnabled == FALSE) && (gapRole_AdvEnabled))
{
// Turn on advertising.
if ((gapRole_state == GAPROLE_STARTED)
|| (gapRole_state == GAPROLE_WAITING)
|| (gapRole_state == GAPROLE_WAITING_AFTER_TIMEOUT))
{
gapRole_setEvent(START_ADVERTISING_EVT);
}
}
}
else
{
ret = bleIncorrectMode;
}
}
else
{
ret = bleInvalidRange;
}
break;
case GAPROLE_ADV_NONCONN_ENABLED:
if (len == sizeof(uint8_t))
{
// Connectable advertising must be disabled.
if (gapRole_AdvEnabled != TRUE)
{
uint8_t oldAdvEnabled = gapRole_AdvNonConnEnabled;
gapRole_AdvNonConnEnabled = *((uint8_t*)pValue);
if ((oldAdvEnabled) && (gapRole_AdvNonConnEnabled == FALSE))
{
if ((gapRole_state == GAPROLE_ADVERTISING_NONCONN)
|| (gapRole_state == GAPROLE_CONNECTED_ADV)
|| (gapRole_state == GAPROLE_WAITING_AFTER_TIMEOUT))
{
VOID GAP_EndDiscoverable(selfEntity);
}
}
else if ((oldAdvEnabled == FALSE) && (gapRole_AdvNonConnEnabled))
{
// Turn on advertising.
if ((gapRole_state == GAPROLE_STARTED)
|| (gapRole_state == GAPROLE_WAITING)
|| (gapRole_state == GAPROLE_CONNECTED)
|| (gapRole_state == GAPROLE_WAITING_AFTER_TIMEOUT))
{
gapRole_setEvent(START_ADVERTISING_EVT);
}
}
}
else
{
ret = bleIncorrectMode;
}
}
else
{
ret = bleInvalidRange;
}
break;
case GAPROLE_ADVERT_OFF_TIME:
if (len == sizeof (uint16_t))
{
gapRole_AdvertOffTime = *((uint16_t*)pValue);
}
else
{
ret = bleInvalidRange;
}
break;
case GAPROLE_ADVERT_DATA:
if (len <= B_MAX_ADV_LEN)
{
VOID memset(gapRole_AdvertData, 0, B_MAX_ADV_LEN);
VOID memcpy(gapRole_AdvertData, pValue, len);
gapRole_AdvertDataLen = len;
// Update the advertising data
ret = GAP_UpdateAdvertisingData(selfEntity,
TRUE, gapRole_AdvertDataLen, gapRole_AdvertData);
}
else
{
ret = bleInvalidRange;
}
break;
case GAPROLE_SCAN_RSP_DATA:
if (len <= B_MAX_ADV_LEN)
{
VOID memset(gapRole_ScanRspData, 0, B_MAX_ADV_LEN);
VOID memcpy(gapRole_ScanRspData, pValue, len);
gapRole_ScanRspDataLen = len;
// Update the Response Data
ret = GAP_UpdateAdvertisingData(selfEntity,
FALSE, gapRole_ScanRspDataLen, gapRole_ScanRspData);
}
else
{
ret = bleInvalidRange;
}
break;
case GAPROLE_ADV_EVENT_TYPE:
if ((len == sizeof (uint8_t)) && (*((uint8_t*)pValue) <= GAP_ADTYPE_ADV_LDC_DIRECT_IND))
{
gapRole_AdvEventType = *((uint8_t*)pValue);
}
else
{
ret = bleInvalidRange;
}
break;
case GAPROLE_ADV_DIRECT_TYPE:
if ((len == sizeof (uint8_t)) && (*((uint8_t*)pValue) <= ADDRMODE_PRIVATE_RESOLVE))
{
gapRole_AdvDirectType = *((uint8_t*)pValue);
}
else
{
ret = bleInvalidRange;
}
break;
case GAPROLE_ADV_DIRECT_ADDR:
if (len == B_ADDR_LEN)
{
VOID memcpy(gapRole_AdvDirectAddr, pValue, B_ADDR_LEN) ;
}
else
{
ret = bleInvalidRange;
}
break;
case GAPROLE_ADV_CHANNEL_MAP:
if ((len == sizeof (uint8_t)) && (*((uint8_t*)pValue) <= 0x07))
{
gapRole_AdvChanMap = *((uint8_t*)pValue);
}
else
{
ret = bleInvalidRange;
}
break;
case GAPROLE_ADV_FILTER_POLICY:
if ((len == sizeof (uint8_t)) && (*((uint8_t*)pValue) <= GAP_FILTER_POLICY_WHITE))
{
gapRole_AdvFilterPolicy = *((uint8_t*)pValue);
}
else
{
ret = bleInvalidRange;
}
break;
case GAPROLE_PARAM_UPDATE_ENABLE:
if ((len == sizeof (uint8_t)) &&
(*((uint8_t*)pValue) < GAPROLE_LINK_PARAM_UPDATE_NUM_OPTIONS))
{
gapRole_updateConnParams.paramUpdateEnable = *((uint8_t*)pValue);
}
else
{
ret = bleInvalidRange;
}
break;
case GAPROLE_MIN_CONN_INTERVAL:
{
uint16_t newInterval = *((uint16_t*)pValue);
if ( len == sizeof (uint16_t) &&
(newInterval >= MIN_CONN_INTERVAL) &&
(newInterval <= MAX_CONN_INTERVAL))
{
gapRole_updateConnParams.minConnInterval = newInterval;
}
else
{
ret = bleInvalidRange;
}
}
break;
case GAPROLE_MAX_CONN_INTERVAL:
{
uint16_t newInterval = *((uint16_t*)pValue);
if ( len == sizeof (uint16_t) &&
(newInterval >= MIN_CONN_INTERVAL) &&
(newInterval <= MAX_CONN_INTERVAL))
{
gapRole_updateConnParams.maxConnInterval = newInterval;
}
else
{
ret = bleInvalidRange;
}
}
break;
case GAPROLE_SLAVE_LATENCY:
{
uint16_t latency = *((uint16_t*)pValue);
if (len == sizeof (uint16_t) && (latency < MAX_SLAVE_LATENCY))
{
gapRole_updateConnParams.slaveLatency = latency;
}
else
{
ret = bleInvalidRange;
}
}
break;
case GAPROLE_TIMEOUT_MULTIPLIER:
{
uint16_t newTimeout = *((uint16_t*)pValue);
if (len == sizeof (uint16_t)
&& (newTimeout >= MIN_TIMEOUT_MULTIPLIER) && (newTimeout <= MAX_TIMEOUT_MULTIPLIER))
{
gapRole_updateConnParams.timeoutMultiplier = newTimeout;
}
else
{
ret = bleInvalidRange;
}
}
break;
case GAPROLE_PARAM_UPDATE_REQ:
{
uint8_t req = *((uint8_t*)pValue);
if (len == sizeof (uint8_t) && (req == TRUE))
{
// Make sure we don't send an L2CAP Connection Parameter Update Request
// command within TGAP(conn_param_timeout) of an L2CAP Connection Parameter
// Update Response being received.
if (Util_isActive(&updateTimeoutClock) == FALSE)
{
// Start connection update procedure
ret = gapRole_startConnUpdate(GAPROLE_NO_ACTION, &gapRole_updateConnParams);
if (ret == SUCCESS)
{
// Connection update requested by app, cancel such pending procedure (if active)
Util_stopClock(&startUpdateClock);
}
}
else
{
ret = blePending;
}
}
else
{
ret = bleInvalidRange;
}
}
break;
default:
// The param value isn't part of this profile, try the GAP.
if ((param < TGAP_PARAMID_MAX) && (len == sizeof (uint16_t)))
{
ret = GAP_SetParamValue(param, *((uint16_t*)pValue));
}
else
{
ret = INVALIDPARAMETER;
}
break;
}
return (ret);
}
/*********************************************************************
* @brief Get a GAP Role parameter.
*
* Public function defined in peripheral.h.
*/
bStatus_t GAPRole_GetParameter(uint16_t param, void *pValue)
{
bStatus_t ret = SUCCESS;
switch (param)
{
case GAPROLE_PROFILEROLE:
*((uint8_t*)pValue) = gapRole_profileRole;
break;
case GAPROLE_IRK:
VOID memcpy(pValue, gapRole_IRK, KEYLEN) ;
break;
case GAPROLE_SRK:
VOID memcpy(pValue, gapRole_SRK, KEYLEN) ;
break;
case GAPROLE_SIGNCOUNTER:
*((uint32_t*)pValue) = gapRole_signCounter;
break;
case GAPROLE_BD_ADDR:
VOID memcpy(pValue, gapRole_bdAddr, B_ADDR_LEN) ;
break;
case GAPROLE_ADVERT_ENABLED:
*((uint8_t*)pValue) = gapRole_AdvEnabled;
break;
case GAPROLE_ADV_NONCONN_ENABLED:
*((uint8_t*)pValue) = gapRole_AdvNonConnEnabled;
break;
case GAPROLE_ADVERT_OFF_TIME:
*((uint16_t*)pValue) = gapRole_AdvertOffTime;
break;
case GAPROLE_ADVERT_DATA:
VOID memcpy(pValue , gapRole_AdvertData, gapRole_AdvertDataLen);
break;
case GAPROLE_SCAN_RSP_DATA:
VOID memcpy(pValue, gapRole_ScanRspData, gapRole_ScanRspDataLen) ;
break;
case GAPROLE_ADV_EVENT_TYPE:
*((uint8_t*)pValue) = gapRole_AdvEventType;
break;
case GAPROLE_ADV_DIRECT_TYPE:
*((uint8_t*)pValue) = gapRole_AdvDirectType;
break;
case GAPROLE_ADV_DIRECT_ADDR:
VOID memcpy(pValue, gapRole_AdvDirectAddr, B_ADDR_LEN) ;
break;
case GAPROLE_ADV_CHANNEL_MAP:
*((uint8_t*)pValue) = gapRole_AdvChanMap;
break;
case GAPROLE_ADV_FILTER_POLICY:
*((uint8_t*)pValue) = gapRole_AdvFilterPolicy;
break;
case GAPROLE_CONNHANDLE:
*((uint16_t*)pValue) = gapRole_ConnectionHandle;
break;
case GAPROLE_PARAM_UPDATE_ENABLE:
*((uint16_t*)pValue) = gapRole_updateConnParams.paramUpdateEnable;
break;
case GAPROLE_MIN_CONN_INTERVAL:
*((uint16_t*)pValue) = gapRole_updateConnParams.minConnInterval;
break;
case GAPROLE_MAX_CONN_INTERVAL:
*((uint16_t*)pValue) = gapRole_updateConnParams.maxConnInterval;
break;
case GAPROLE_SLAVE_LATENCY:
*((uint16_t*)pValue) = gapRole_updateConnParams.slaveLatency;
break;
case GAPROLE_TIMEOUT_MULTIPLIER:
*((uint16_t*)pValue) = gapRole_updateConnParams.timeoutMultiplier;
break;
case GAPROLE_CONN_BD_ADDR:
VOID memcpy(pValue, gapRole_ConnectedDevAddr, B_ADDR_LEN) ;
break;
case GAPROLE_CONN_INTERVAL:
*((uint16_t*)pValue) = gapRole_ConnInterval;
break;
case GAPROLE_CONN_LATENCY:
*((uint16_t*)pValue) = gapRole_ConnSlaveLatency;
break;
case GAPROLE_CONN_TIMEOUT:
*((uint16_t*)pValue) = gapRole_ConnTimeout;
break;
case GAPROLE_BD_ADDR_TYPE:
*((uint8_t*)pValue) = gapRole_ConnectedDevAddrType;
break;
case GAPROLE_STATE:
*((uint8_t*)pValue) = gapRole_state;
break;
case GAPROLE_CONN_TERM_REASON:
*((uint8_t*)pValue) = gapRole_ConnTermReason;
break;
default:
// The param value isn't part of this profile, try the GAP.
if (param < TGAP_PARAMID_MAX)
{
*((uint16_t*)pValue) = GAP_GetParamValue(param);
}
else
{
ret = INVALIDPARAMETER;
}
break;
}
return (ret);
}
/*********************************************************************
* @brief Does the device initialization.
*
* Public function defined in peripheral.h.
*/
bStatus_t GAPRole_StartDevice(gapRolesCBs_t *pAppCallbacks)
{
if (gapRole_state == GAPROLE_INIT)
{
// Clear all of the Application callbacks
if (pAppCallbacks)
{
pGapRoles_AppCGs = pAppCallbacks;
}
// Start the GAP
gapRole_SetupGAP();
return (SUCCESS);
}
else
{
return (bleAlreadyInRequestedMode);
}
}
/*********************************************************************
* @brief Register application's callbacks.
*
* Public function defined in peripheral.h.
*/
void GAPRole_RegisterAppCBs(gapRolesParamUpdateCB_t *pParamUpdateCB)
{
if (pParamUpdateCB != NULL)
{
pGapRoles_ParamUpdateCB = pParamUpdateCB;
}
}
/*********************************************************************
* @brief Terminates the existing connection.
*
* Public function defined in peripheral.h.
*/
bStatus_t GAPRole_TerminateConnection(void)
{
if ( (gapRole_state == GAPROLE_CONNECTED) ||
(gapRole_state == GAPROLE_CONNECTED_ADV))
{
return (GAP_TerminateLinkReq(selfEntity, gapRole_ConnectionHandle,
HCI_DISCONNECT_REMOTE_USER_TERM));
}
else
{
return (bleIncorrectMode);
}
}
/*********************************************************************
* @fn GAPRole_createTask
*
* @brief Task creation function for the GAP Peripheral Role.
*
* @param none
*
* @return none
*/
void GAPRole_createTask(void)
{
Task_Params taskParams;
// Configure task
Task_Params_init(&taskParams);
taskParams.stack = gapRoleTaskStack;
taskParams.stackSize = GAPROLE_TASK_STACK_SIZE;
taskParams.priority = GAPROLE_TASK_PRIORITY;
Task_construct(&gapRoleTask, gapRole_taskFxn, &taskParams, NULL);
}
/*********************************************************************
* LOCAL FUNCTION PROTOTYPES
*/
/*********************************************************************
* @fn gapRole_init
*
* @brief Initialization function for the GAP Role Task.
*
* @param none
*
* @return none
*/
static void gapRole_init(void)
{
// Register the current thread as an ICall dispatcher application
// so that the application can send and receive messages.
#ifdef ICALL_EVENTS
ICall_registerApp(&selfEntity, &syncEvent);
#else //!ICALL_EVENTS
ICall_registerApp(&selfEntity, &sem);
#endif //ICALL_EVENTS
gapRole_state = GAPROLE_INIT;
gapRole_ConnectionHandle = INVALID_CONNHANDLE;
// Get link DB maximum number of connections
linkDBNumConns = linkDB_NumConns();
// Setup timers as one-shot timers
Util_constructClock(&startAdvClock, gapRole_clockHandler,
0, 0, false, START_ADVERTISING_EVT);
Util_constructClock(&startUpdateClock, gapRole_clockHandler,
0, 0, false, START_CONN_UPDATE_EVT);
Util_constructClock(&updateTimeoutClock, gapRole_clockHandler,
0, 0, false, CONN_PARAM_TIMEOUT_EVT);
// Initialize the Profile Advertising and Connection Parameters
gapRole_profileRole = GAP_PROFILE_PERIPHERAL;
VOID memset(gapRole_IRK, 0, KEYLEN);
VOID memset(gapRole_SRK, 0, KEYLEN);
gapRole_signCounter = 0;
gapRole_AdvEventType = GAP_ADTYPE_ADV_IND;
gapRole_AdvDirectType = ADDRMODE_PUBLIC;
gapRole_AdvChanMap = GAP_ADVCHAN_ALL;
gapRole_AdvFilterPolicy = GAP_FILTER_POLICY_ALL;
// Restore Items from NV
VOID osal_snv_read(BLE_NVID_IRK, KEYLEN, gapRole_IRK);
VOID osal_snv_read(BLE_NVID_CSRK, KEYLEN, gapRole_SRK);
VOID osal_snv_read(BLE_NVID_SIGNCOUNTER, sizeof(uint32_t),
&gapRole_signCounter);
}
/*********************************************************************
* @fn gapRole_taskFxn
*
* @brief Task entry point for the GAP Peripheral Role.
*
* @param a0 - first argument
* @param a1 - second argument
*
* @return none
*/
static void gapRole_taskFxn(UArg a0, UArg a1)
{
// Initialize profile
gapRole_init();
// Profile main loop
for (;;)
{
#ifdef ICALL_EVENTS
uint32_t events;
// Wait for an event to be posted
events = Event_pend(syncEvent, Event_Id_NONE, GAPROLE_ALL_EVENTS,
ICALL_TIMEOUT_FOREVER);
if (events)
#else //!ICALL_EVENTS
// Waits for a signal to the semaphore associated with the calling thread.
// Note that the semaphore associated with a thread is signaled when a
// message is queued to the message receive queue of the thread or when
// ICall_signal() function is called onto the semaphore.
ICall_Errno errno = ICall_wait(ICALL_TIMEOUT_FOREVER);
if (errno == ICALL_ERRNO_SUCCESS)
#endif //ICALL_EVENTS
{
ICall_EntityID dest;
ICall_ServiceEnum src;
ICall_HciExtEvt *pMsg = NULL;
if (ICall_fetchServiceMsg(&src, &dest,
(void **)&pMsg) == ICALL_ERRNO_SUCCESS)
{
if ((src == ICALL_SERVICE_CLASS_BLE) && (dest == selfEntity))
{
ICall_Stack_Event *pEvt = (ICall_Stack_Event *)pMsg;
// Check for BLE stack events first
if (pEvt->signature == 0xffff)
{
if (pEvt->event_flag & GAP_EVENT_SIGN_COUNTER_CHANGED)
{
// Sign counter changed, save it to NV
VOID osal_snv_write(BLE_NVID_SIGNCOUNTER, sizeof(uint32_t),
&gapRole_signCounter);
}
}
else
{
// Process inter-task message
gapRole_processStackMsg((ICall_Hdr *)pMsg);
}
}
if (pMsg)
{
ICall_freeMsg(pMsg);
}
}
}
if (events & START_ADVERTISING_EVT)
{
#ifndef ICALL_EVENTS
events &= ~START_ADVERTISING_EVT;
#endif //ICALL_EVENTS
if (gapRole_AdvEnabled || gapRole_AdvNonConnEnabled)
{
gapAdvertisingParams_t params;
// Setup advertisement parameters
if (gapRole_AdvNonConnEnabled)
{
// Only advertise non-connectable undirected.
params.eventType = GAP_ADTYPE_ADV_NONCONN_IND;
}
else
{
params.eventType = gapRole_AdvEventType;
params.initiatorAddrType = gapRole_AdvDirectType;
VOID memcpy(params.initiatorAddr, gapRole_AdvDirectAddr, B_ADDR_LEN);
}
params.channelMap = gapRole_AdvChanMap;
params.filterPolicy = gapRole_AdvFilterPolicy;
if (GAP_MakeDiscoverable(selfEntity, ¶ms) != SUCCESS)
{
gapRole_state = GAPROLE_ERROR;
// Notify the application with the new state change
if (pGapRoles_AppCGs && pGapRoles_AppCGs->pfnStateChange)
{
pGapRoles_AppCGs->pfnStateChange(gapRole_state);
}
}
}
}