-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhome_automation.c
1199 lines (1012 loc) · 36.5 KB
/
home_automation.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
/******************************************************************************
Copyright (c) 2013-2018, 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.
*****************************************************************************/
/*********************************************************************
* INCLUDES
*/
#include <string.h>
#include <ti/sysbios/knl/Task.h>
#include <ti/sysbios/knl/Clock.h>
#include <ti/sysbios/knl/Semaphore.h>
#include <ti/sysbios/knl/Queue.h>
#include "SensorI2C.h"
#include "hci_tl.h"
#include "gatt.h"
#include "linkdb.h"
#include "gapgattserver.h"
#include "gattservapp.h"
#include "devinfoservice.h"
#include "ble_user_config.h"
#if defined(FEATURE_OAD) || defined(IMAGE_INVALIDATE)
#include "oad_target.h"
#include "oad.h"
#endif //FEATURE_OAD || IMAGE_INVALIDATE
#include "peripheral.h"
#include "gapbondmgr.h"
#include "osal_snv.h"
#include "icall_apimsg.h"
#include "util.h"
#ifdef USE_RCOSC
#include "rcosc_calibration.h"
#endif //USE_RCOSC
#include <ti/mw/display/Display.h>
#include "board_key.h"
#include "board.h"
#include "home_automation.h"
#include "home_automation_batt.h"
#include "home_automation_keys.h"
#include "home_automation_relay.h"
#include "home_automation_env.h"
#include "home_automation_rgb_led.h"
#if defined( USE_FPGA ) || defined( DEBUG_SW_TRACE )
#include <driverlib/ioc.h>
#endif // USE_FPGA | DEBUG_SW_TRACE
/*********************************************************************
* CONSTANTS
*/
// Advertising interval when device is discoverable (units of 625us, 1600=1s)
#define DEFAULT_ADVERTISING_INTERVAL 1600
// Limited discoverable mode advertises for 30.72s, and then stops
// General discoverable mode advertises indefinitely
#define DEFAULT_DISCOVERABLE_MODE GAP_ADTYPE_FLAGS_GENERAL
#ifndef FEATURE_OAD
// Minimum connection interval (units of 1.25ms, 80=100ms) if automatic
// parameter update request is enabled
#define DEFAULT_DESIRED_MIN_CONN_INTERVAL 80
// Maximum connection interval (units of 1.25ms, 200=250ms) if automatic
// parameter update request is enabled
#define DEFAULT_DESIRED_MAX_CONN_INTERVAL 200
#else //!FEATURE_OAD
// Minimum connection interval (units of 1.25ms, 8=10ms) if automatic
// parameter update request is enabled
#define DEFAULT_DESIRED_MIN_CONN_INTERVAL 8
// Maximum connection interval (units of 1.25ms, 8=10ms) if automatic
// parameter update request is enabled
#define DEFAULT_DESIRED_MAX_CONN_INTERVAL 8
#endif // FEATURE_OAD
// Slave latency to use if automatic parameter update request is enabled
#ifndef EXCLUDE_BATT
#define DEFAULT_DESIRED_SLAVE_LATENCY 3
#else
#define DEFAULT_DESIRED_SLAVE_LATENCY 0
#endif
// Supervision timeout value (units of 10ms, 1000=10s) if automatic parameter
// update request is enabled
#define DEFAULT_DESIRED_CONN_TIMEOUT 1000
// Whether to enable automatic parameter update request when a connection is
// formed
#define DEFAULT_ENABLE_UPDATE_REQUEST GAPROLE_LINK_PARAM_UPDATE_INITIATE_BOTH_PARAMS
// Connection Pause Peripheral time value (in seconds)
#define DEFAULT_CONN_PAUSE_PERIPHERAL 6
#ifdef FEATURE_OAD
// The size of an OAD packet.
#define OAD_PACKET_SIZE ((OAD_BLOCK_SIZE) + 2)
#endif // FEATURE_OAD
// Task configuration
#define HA_TASK_PRIORITY 1
#ifndef HA_TASK_STACK_SIZE
#define HA_TASK_STACK_SIZE 800
#endif
// Internal Events for RTOS application
#define HA_STATE_CHANGE_EVT (1 << 0)
#define HA_CONN_EVT_END_EVT (1 << 1)
#define HA_CHAR_CHANGE_EVT (1 << 2)
#ifdef HAS_SWITCH
#define NAME 'S', 'w', 'i', 't', 'c', 'h'
#define NAME_LEN 6
#elif defined(HAS_BUTTON) && Board_RELAY_SET != PIN_UNASSIGNED
#define NAME 'W', 'a', 't', 'e', 'r', 'H', 'e', 'a', 't', 'e', 'r'
#define NAME_LEN 11
#elif defined(HAS_CONTACT)
#define NAME 'C', 'o', 'n', 't', 'a', 'c', 't'
#define NAME_LEN 7
#elif defined(HAS_MOTION)
#define NAME 'M', 'o', 't', 'i', 'o', 'n'
#define NAME_LEN 6
#elif Board_THERMISTOR != PIN_UNASSIGNED
#define NAME 'T', 'e', 'm', 'p', 'e', 'r', 'a', 't', 'u', 'r', 'e'
#define NAME_LEN 11
#else
#error No Name defined for device
#endif
/*********************************************************************
* TYPEDEFS
*/
// App event passed from profiles.
typedef struct
{
uint8_t event; // Which profile's event
uint8_t serviceID; // New status
uint8_t paramID;
} haEvt_t;
/*********************************************************************
* GLOBAL VARIABLES
*/
// Semaphore globally used to post events to the application thread
ICall_Semaphore sem;
// Display Interface
Display_Handle dispHandle = NULL;
// GPIOs
PIN_State haPinState;
PIN_Handle haPinHandle;
/*********************************************************************
* LOCAL VARIABLES
*/
// Entity ID globally used to check for source and/or destination of messages
static ICall_EntityID selfEntity;
// Queue object used for app messages
static Queue_Struct appMsg;
static Queue_Handle appMsgQueue;
#if defined(FEATURE_OAD)
// Event data from OAD profile.
static Queue_Struct oadQ;
static Queue_Handle hOadQ;
#endif //FEATURE_OAD
// Task configuration
Task_Struct haTask;
Char haTaskStack[HA_TASK_STACK_SIZE];
// GAP - SCAN RSP data (max size = 31 bytes)
static uint8_t scanRspData[] =
{
// complete name
NAME_LEN + 6, // length of this data
GAP_ADTYPE_LOCAL_NAME_COMPLETE,
NAME,
'-',
'X',
'X',
'X',
'X',
// connection interval range
5, // length of this data
GAP_ADTYPE_SLAVE_CONN_INTERVAL_RANGE,
LO_UINT16(DEFAULT_DESIRED_MIN_CONN_INTERVAL), // 100ms
HI_UINT16(DEFAULT_DESIRED_MIN_CONN_INTERVAL),
LO_UINT16(DEFAULT_DESIRED_MAX_CONN_INTERVAL), // 250ms
HI_UINT16(DEFAULT_DESIRED_MAX_CONN_INTERVAL),
// Tx power level
2, // length of this data
GAP_ADTYPE_POWER_LEVEL,
0 // 0dBm
};
// GAP - Advertisement data (max size = 31 bytes, though this is
// best kept short to conserve power while advertising)
static uint8_t advertData[] =
{
// Flags; this sets the device to use limited discoverable
// mode (advertises for 30 seconds at a time) instead of general
// discoverable mode (advertises indefinitely)
2, // length of this data
GAP_ADTYPE_FLAGS,
DEFAULT_DISCOVERABLE_MODE | GAP_ADTYPE_FLAGS_BREDR_NOT_SUPPORTED,
// service UUID, to notify central devices what services are included
// in this peripheral
#if defined(FEATURE_OAD)
3, // length of this data
GAP_ADTYPE_16BIT_MORE, // some of the UUID's, but not all
LO_UINT16(OAD_SERVICE_UUID),
HI_UINT16(OAD_SERVICE_UUID),
#endif //FEATURE_OAD
};
// GAP GATT Attributes
static uint8_t attDeviceName[GAP_DEVICE_NAME_LEN] =
{ NAME, '-', 'X', 'X', 'X', 'X' };
// Globals used for ATT Response retransmission
static gattMsgEvent_t *pAttRsp = NULL;
static uint8_t rspTxRetry = 0;
/*********************************************************************
* LOCAL FUNCTIONS
*/
static void HomeAutomation_init( void );
static void HomeAutomation_taskFxn(UArg a0, UArg a1);
static uint8_t HomeAutomation_processStackMsg(ICall_Hdr *pMsg);
static uint8_t HomeAutomation_processGATTMsg(gattMsgEvent_t *pMsg);
static void HomeAutomation_processAppMsg(haEvt_t *pMsg);
static void HomeAutomation_processStateChangeEvt(gaprole_States_t newState);
static void HomeAutomation_processCharValueChangeEvt(uint8_t serviceID,
uint8_t paramID);
static void HomeAutomation_sendAttRsp(void);
static void HomeAutomation_freeAttRsp(uint8_t status);
static void HomeAutomation_stateChangeCB(gaprole_States_t newState);
static void HomeAutomation_enqueueMsg(uint8_t event, uint8_t serviceID,
uint8_t paramID);
static void HomeAutomation_resetAllModules(void);
static void HomeAutomation_intCb(PIN_Handle handle, PIN_Id pinId);
#ifdef FEATURE_OAD
void HomeAutomation_processOadWriteCB(uint8_t event, uint16_t connHandle,
uint8_t *pData);
#endif //FEATURE_OAD
/*********************************************************************
* EXTERN FUNCTIONS
*/
extern void AssertHandler(uint8 assertCause, uint8 assertSubcause);
/*********************************************************************
* PROFILE CALLBACKS
*/
// GAP Role Callbacks
static gapRolesCBs_t HomeAutomation_gapRoleCBs =
{
HomeAutomation_stateChangeCB // Profile State Change Callbacks
};
// GAP Bond Manager Callbacks
static gapBondCBs_t HomeAutomation_BondMgrCBs =
{
NULL, // Passcode callback (not used by application)
NULL // Pairing / Bonding state Callback (not used by application)
};
#ifdef FEATURE_OAD
static oadTargetCBs_t HomeAutomation_oadCBs =
{
HomeAutomation_processOadWriteCB // Write Callback.
};
#endif //FEATURE_OAD
/*********************************************************************
* PUBLIC FUNCTIONS
*/
/*********************************************************************
* @fn HomeAutomation_createTask
*
* @brief Task creation function for the Home Automation task.
*
* @param None.
*
* @return None.
*/
void HomeAutomation_createTask(void)
{
Task_Params taskParams;
// Configure task
Task_Params_init(&taskParams);
taskParams.stack = haTaskStack;
taskParams.stackSize = HA_TASK_STACK_SIZE;
taskParams.priority = HA_TASK_PRIORITY;
Task_construct(&haTask, HomeAutomation_taskFxn, &taskParams, NULL);
}
/*********************************************************************
* @fn HomeAutomation_init
*
* @brief Called during initialization and contains application
* specific initialization (i.e. hardware initialization/setup,
* table initialization, power up notification, etc), and
* profile initialization/setup.
*
* @param None.
*
* @return None.
*/
static void HomeAutomation_init(void)
{
// ******************************************************************
// N0 STACK API CALLS CAN OCCUR BEFORE THIS CALL TO ICall_registerApp
// ******************************************************************
// Register the current thread as an ICall dispatcher application
// so that the application can send and receive messages.
ICall_registerApp(&selfEntity, &sem);
#ifdef USE_RCOSC
RCOSC_enableCalibration();
#endif // USE_RCOSC
#if defined( USE_FPGA )
// configure RF Core SMI Data Link
IOCPortConfigureSet(IOID_12, IOC_PORT_RFC_GPO0, IOC_STD_OUTPUT);
IOCPortConfigureSet(IOID_11, IOC_PORT_RFC_GPI0, IOC_STD_INPUT);
// configure RF Core SMI Command Link
IOCPortConfigureSet(IOID_10, IOC_IOCFG0_PORT_ID_RFC_SMI_CL_OUT, IOC_STD_OUTPUT);
IOCPortConfigureSet(IOID_9, IOC_IOCFG0_PORT_ID_RFC_SMI_CL_IN, IOC_STD_INPUT);
// configure RF Core tracer IO
IOCPortConfigureSet(IOID_8, IOC_PORT_RFC_TRC, IOC_STD_OUTPUT);
#else // !USE_FPGA
#if defined( DEBUG_SW_TRACE )
// configure RF Core tracer IO
IOCPortConfigureSet(IOID_8, IOC_PORT_RFC_TRC, IOC_STD_OUTPUT | IOC_CURRENT_4MA | IOC_SLEW_ENABLE);
#endif // DEBUG_SW_TRACE
#endif // USE_FPGA
// Create an RTOS queue for message from profile to be sent to app.
appMsgQueue = Util_constructQueue(&appMsg);
// Setup I2C for sensors
SensorI2C_open();
// Setup GPIO
haPinHandle = PIN_open(&haPinState, BoardGpioInitTable);
PIN_registerIntCb(haPinHandle, HomeAutomation_intCb);
dispHandle = Display_open(Display_Type_LCD, NULL);
// Setup the GAP
GAP_SetParamValue(TGAP_CONN_PAUSE_PERIPHERAL, DEFAULT_CONN_PAUSE_PERIPHERAL);
// Setup the GAP Peripheral Role Profile
{
// For all hardware platforms, device starts advertising upon initialization
uint8_t initialAdvertEnable = TRUE;
// By setting this to zero, the device will go into the waiting state after
// being discoverable for 30.72 second, and will not being advertising again
// until the enabler is set back to TRUE
uint16_t advertOffTime = 0;
uint8_t enableUpdateRequest = DEFAULT_ENABLE_UPDATE_REQUEST;
uint16_t desiredMinInterval = DEFAULT_DESIRED_MIN_CONN_INTERVAL;
uint16_t desiredMaxInterval = DEFAULT_DESIRED_MAX_CONN_INTERVAL;
uint16_t desiredSlaveLatency = DEFAULT_DESIRED_SLAVE_LATENCY;
uint16_t desiredConnTimeout = DEFAULT_DESIRED_CONN_TIMEOUT;
// Set the GAP Role Parameters
GAPRole_SetParameter(GAPROLE_ADVERT_ENABLED, sizeof(uint8_t),
&initialAdvertEnable);
GAPRole_SetParameter(GAPROLE_ADVERT_OFF_TIME, sizeof(uint16_t),
&advertOffTime);
GAPRole_SetParameter(GAPROLE_ADVERT_DATA, sizeof(advertData), advertData);
GAPRole_SetParameter(GAPROLE_PARAM_UPDATE_ENABLE, sizeof(uint8_t),
&enableUpdateRequest);
GAPRole_SetParameter(GAPROLE_MIN_CONN_INTERVAL, sizeof(uint16_t),
&desiredMinInterval);
GAPRole_SetParameter(GAPROLE_MAX_CONN_INTERVAL, sizeof(uint16_t),
&desiredMaxInterval);
GAPRole_SetParameter(GAPROLE_SLAVE_LATENCY, sizeof(uint16_t),
&desiredSlaveLatency);
GAPRole_SetParameter(GAPROLE_TIMEOUT_MULTIPLIER, sizeof(uint16_t),
&desiredConnTimeout);
}
// Set advertising interval
{
uint16_t advInt = DEFAULT_ADVERTISING_INTERVAL;
GAP_SetParamValue(TGAP_LIM_DISC_ADV_INT_MIN, advInt);
GAP_SetParamValue(TGAP_LIM_DISC_ADV_INT_MAX, advInt);
GAP_SetParamValue(TGAP_GEN_DISC_ADV_INT_MIN, advInt);
GAP_SetParamValue(TGAP_GEN_DISC_ADV_INT_MAX, advInt);
}
// Setup the GAP Bond Manager
{
uint32_t passkey = 0xdeadbeef; // Passkey signature to be replaced by create_image.py
uint8_t pairMode = GAPBOND_PAIRING_MODE_WAIT_FOR_REQ;
uint8_t mitm = TRUE;
uint8_t ioCap = GAPBOND_IO_CAP_DISPLAY_ONLY;
uint8_t bonding = TRUE;
GAPBondMgr_SetParameter(GAPBOND_DEFAULT_PASSCODE, sizeof(uint32_t),
&passkey);
GAPBondMgr_SetParameter(GAPBOND_PAIRING_MODE, sizeof(uint8_t), &pairMode);
GAPBondMgr_SetParameter(GAPBOND_MITM_PROTECTION, sizeof(uint8_t), &mitm);
GAPBondMgr_SetParameter(GAPBOND_IO_CAPABILITIES, sizeof(uint8_t), &ioCap);
GAPBondMgr_SetParameter(GAPBOND_BONDING_ENABLED, sizeof(uint8_t), &bonding);
}
// Initialize GATT attributes
GGS_AddService(GATT_ALL_SERVICES); // GAP
GATTServApp_AddService(GATT_ALL_SERVICES); // GATT attributes
DevInfo_AddService(); // Device Information Service
#ifdef FEATURE_OAD
VOID OAD_addService(); // OAD Profile
OAD_register((oadTargetCBs_t *)&HomeAutomation_oadCBs);
hOadQ = Util_constructQueue(&oadQ);
#endif //FEATURE_OAD
#ifdef IMAGE_INVALIDATE
Reset_addService();
#endif //IMAGE_INVALIDATE
#if defined(EXCLUDE_BATT) || defined(MAX_TX_POWER)
HCI_EXT_SetTxPowerCmd(txPwrTbl.numTxPwrVals - 1);
#endif
// Add battery monitor
HomeAutomationBatt_init();
// Sensor modules
HomeAutomationEnv_init(); // Environmental sensor (light, pressure, humidity and temperature)
// Auxiliary modules
HomeAutomationKeys_init(); // Input keys (buttons, switches, etc.)
HomeAutomationRelay_init(); // Relay
HomeAutomationRGBLED_init(); // RGB LEDs
// Start the Device
VOID GAPRole_StartDevice(&HomeAutomation_gapRoleCBs);
// Start Bond Manager
VOID GAPBondMgr_Register(&HomeAutomation_BondMgrCBs);
// Register with GAP for HCI/Host messages
GAP_RegisterForMsgs(selfEntity);
// Register for GATT local events and ATT Responses pending for transmission
GATT_RegisterForMsgs(selfEntity);
HCI_LE_ReadMaxDataLenCmd();
#if defined FEATURE_OAD
#if defined (HAL_IMAGE_A)
Display_print0(dispHandle, 0, 0, ((char []){ NAME, ' ', 'A', '\0' }));
#else
Display_print0(dispHandle, 0, 0, ((char []){ NAME, ' ', 'B', '\0' }));
#endif // HAL_IMAGE_A
#else
Display_print0(dispHandle, 0, 0, ((char []){ NAME, '\0' }));
#endif // FEATURE_OAD
}
/*********************************************************************
* @fn HomeAutomation_taskFxn
*
* @brief Application task entry point for the Home Automation task.
*
* @param a0, a1 - not used.
*
* @return None.
*/
static void HomeAutomation_taskFxn(UArg a0, UArg a1)
{
// Initialize application
HomeAutomation_init();
// Application main loop
for (;;)
{
// 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)
{
ICall_EntityID dest;
ICall_ServiceEnum src;
ICall_HciExtEvt *pMsg = NULL;
if (ICall_fetchServiceMsg(&src, &dest,
(void **)&pMsg) == ICALL_ERRNO_SUCCESS)
{
uint8 safeToDealloc = TRUE;
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 & HA_CONN_EVT_END_EVT)
{
// Try to retransmit pending ATT Response (if any)
HomeAutomation_sendAttRsp();
}
}
else
{
// Process inter-task message
safeToDealloc = HomeAutomation_processStackMsg((ICall_Hdr *)pMsg);
}
}
if (pMsg && safeToDealloc)
{
ICall_freeMsg(pMsg);
}
}
// If RTOS queue is not empty, process app message.
while (!Queue_empty(appMsgQueue))
{
haEvt_t *pMsg = (haEvt_t *)Util_dequeueMsg(appMsgQueue);
if (pMsg)
{
// Process message.
HomeAutomation_processAppMsg(pMsg);
// Free the space from the message.
ICall_free(pMsg);
}
}
// Process new data if available
HomeAutomationKeys_processEvent();
HomeAutomationRelay_processEvent();
HomeAutomationBatt_processSensorEvent();
HomeAutomationEnv_processEvent();
HomeAutomationRGBLED_processEvent();
}
#ifdef FEATURE_OAD
while (!Queue_empty(hOadQ))
{
oadTargetWrite_t *oadWriteEvt = Queue_get(hOadQ);
// Identify new image.
if (oadWriteEvt->event == OAD_WRITE_IDENTIFY_REQ)
{
OAD_imgIdentifyWrite(oadWriteEvt->connHandle, oadWriteEvt->pData);
}
// Write a next block request.
else if (oadWriteEvt->event == OAD_WRITE_BLOCK_REQ)
{
OAD_imgBlockWrite(oadWriteEvt->connHandle, oadWriteEvt->pData);
}
// Free buffer.
ICall_free(oadWriteEvt);
}
#endif //FEATURE_OAD
}
}
/*********************************************************************
* @fn HomeAutomation_processStackMsg
*
* @brief Process an incoming stack message.
*
* @param pMsg - message to process
*
* @return TRUE if safe to deallocate incoming message, FALSE otherwise.
*/
static uint8_t HomeAutomation_processStackMsg(ICall_Hdr *pMsg)
{
uint8_t safeToDealloc = TRUE;
switch (pMsg->event)
{
case GATT_MSG_EVENT:
// Process GATT message
safeToDealloc = HomeAutomation_processGATTMsg((gattMsgEvent_t *)pMsg);
break;
case HCI_GAP_EVENT_EVENT:
{
// Process HCI message
switch(pMsg->status)
{
case HCI_COMMAND_COMPLETE_EVENT_CODE:
// Process HCI Command Complete Event
break;
case HCI_BLE_HARDWARE_ERROR_EVENT_CODE:
AssertHandler(HAL_ASSERT_CAUSE_HARDWARE_ERROR, 0);
break;
default:
break;
}
}
break;
default:
// do nothing
break;
}
return (safeToDealloc);
}
/*********************************************************************
* @fn HomeAutomation_processGATTMsg
*
* @brief Process GATT messages and events.
*
* @return TRUE if safe to deallocate incoming message, FALSE otherwise.
*/
static uint8_t HomeAutomation_processGATTMsg(gattMsgEvent_t *pMsg)
{
// See if GATT server was unable to transmit an ATT response
if (pMsg->hdr.status == blePending)
{
// No HCI buffer was available. Let's try to retransmit the response
// on the next connection event.
if (HCI_EXT_ConnEventNoticeCmd(pMsg->connHandle, selfEntity,
HA_CONN_EVT_END_EVT) == SUCCESS)
{
// First free any pending response
HomeAutomation_freeAttRsp(FAILURE);
// Hold on to the response message for retransmission
pAttRsp = pMsg;
// Don't free the response message yet
return (FALSE);
}
}
else if (pMsg->method == ATT_FLOW_CTRL_VIOLATED_EVENT)
{
// ATT request-response or indication-confirmation flow control is
// violated. All subsequent ATT requests or indications will be dropped.
// The app is informed in case it wants to drop the connection.
// Display the opcode of the message that caused the violation.
Display_print1(dispHandle, 5, 0, "FC Violated: %d", pMsg->msg.flowCtrlEvt.opcode);
}
else if (pMsg->method == ATT_MTU_UPDATED_EVENT)
{
// MTU size updated
Display_print1(dispHandle, 5, 0, "MTU Size: $d", pMsg->msg.mtuEvt.MTU);
}
// Free message payload. Needed only for ATT Protocol messages
GATT_bm_free(&pMsg->msg, pMsg->method);
// It's safe to free the incoming message
return (TRUE);
}
/*********************************************************************
* @fn HomeAutomation_sendAttRsp
*
* @brief Send a pending ATT response message.
*
* @param none
*
* @return none
*/
static void HomeAutomation_sendAttRsp(void)
{
// See if there's a pending ATT Response to be transmitted
if (pAttRsp != NULL)
{
uint8_t status;
// Increment retransmission count
rspTxRetry++;
// Try to retransmit ATT response till either we're successful or
// the ATT Client times out (after 30s) and drops the connection.
status = GATT_SendRsp(pAttRsp->connHandle, pAttRsp->method, &(pAttRsp->msg));
if ((status != blePending) && (status != MSG_BUFFER_NOT_AVAIL))
{
// Disable connection event end notice
HCI_EXT_ConnEventNoticeCmd(pAttRsp->connHandle, selfEntity, 0);
// We're done with the response message
HomeAutomation_freeAttRsp(status);
}
else
{
// Continue retrying
Display_print1(dispHandle, 5, 0, "Rsp send retry: %d", rspTxRetry);
}
}
}
/*********************************************************************
* @fn HomeAutomation_freeAttRsp
*
* @brief Free ATT response message.
*
* @param status - response transmit status
*
* @return none
*/
static void HomeAutomation_freeAttRsp(uint8_t status)
{
// See if there's a pending ATT response message
if (pAttRsp != NULL)
{
// See if the response was sent out successfully
if (status == SUCCESS)
{
Display_print1(dispHandle, 5, 0, "Rsp sent retry: %d", rspTxRetry);
}
else
{
// Free response payload
GATT_bm_free(&pAttRsp->msg, pAttRsp->method);
Display_print1(dispHandle, 5, 0, "Rsp retry failed: %d", rspTxRetry);
}
// Free response message
ICall_freeMsg(pAttRsp);
// Reset our globals
pAttRsp = NULL;
rspTxRetry = 0;
}
}
/*********************************************************************
* @fn HomeAutomation_processAppMsg
*
* @brief Process an incoming callback from a profile.
*
* @param pMsg - message to process
*
* @return None.
*/
static void HomeAutomation_processAppMsg(haEvt_t *pMsg)
{
switch (pMsg->event)
{
case HA_STATE_CHANGE_EVT:
HomeAutomation_processStateChangeEvt((gaprole_States_t)pMsg->serviceID);
break;
case HA_CHAR_CHANGE_EVT:
HomeAutomation_processCharValueChangeEvt(pMsg->serviceID, pMsg->paramID);
break;
default:
// Do nothing.
break;
}
}
/*********************************************************************
* @fn HomeAutomation_stateChangeCB
*
* @brief Callback from GAP Role indicating a role state change.
*
* @param newState - new state
*
* @return None.
*/
static void HomeAutomation_stateChangeCB(gaprole_States_t newState)
{
HomeAutomation_enqueueMsg(HA_STATE_CHANGE_EVT, newState, NULL);
}
/*********************************************************************
* @fn HomeAutomation_processStateChangeEvt
*
* @brief Process a pending GAP Role state change event.
*
* @param newState - new state
*
* @return None.
*/
static void HomeAutomation_processStateChangeEvt(gaprole_States_t newState)
{
#ifdef PLUS_BROADCASTER
static bool firstConnFlag = false;
#endif // PLUS_BROADCASTER
switch ( newState )
{
case GAPROLE_STARTED:
{
uint8_t ownAddress[B_ADDR_LEN];
uint8_t systemId[DEVINFO_SYSTEM_ID_LEN];
char mac_str[4];
GAPRole_GetParameter(GAPROLE_BD_ADDR, ownAddress);
// use 6 bytes of device address for 8 bytes of system ID value
systemId[0] = ownAddress[0];
systemId[1] = ownAddress[1];
systemId[2] = ownAddress[2];
// set middle bytes to zero
systemId[4] = 0x00;
systemId[3] = 0x00;
// shift three bytes up
systemId[7] = ownAddress[5];
systemId[6] = ownAddress[4];
systemId[5] = ownAddress[3];
DevInfo_SetParameter(DEVINFO_SYSTEM_ID, DEVINFO_SYSTEM_ID_LEN, systemId);
// Add MAC address to device name
#define TO_HEX(i) ("0123456789ABCDEF"[i])
mac_str[0] = TO_HEX((ownAddress[1] >> 4) & 0x0F);
mac_str[1] = TO_HEX(ownAddress[1] & 0x0F);
mac_str[2] = TO_HEX((ownAddress[0] >> 4) & 0x0F);
mac_str[3] = TO_HEX(ownAddress[0] & 0x0F);
memcpy(&scanRspData[2 + NAME_LEN + 1], mac_str, 4);
memcpy(&attDeviceName[NAME_LEN + 1], mac_str, 4);
GAPRole_SetParameter(GAPROLE_SCAN_RSP_DATA, sizeof(scanRspData),
scanRspData);
GGS_SetParameter(GGS_DEVICE_NAME_ATT, GAP_DEVICE_NAME_LEN, attDeviceName);
#undef TO_HEX
// Display device address
Display_print0(dispHandle, 1, 0, Util_convertBdAddr2Str(ownAddress));
Display_print0(dispHandle, 2, 0, "Initialized");
}
break;
case GAPROLE_ADVERTISING:
Display_print0(dispHandle, 2, 0, "Advertising");
break;
#ifdef PLUS_BROADCASTER
/* After a connection is dropped a device in PLUS_BROADCASTER will continue
* sending non-connectable advertisements and shall sending this change of
* state to the application. These are then disabled here so that sending
* connectable advertisements can resume.
*/
case GAPROLE_ADVERTISING_NONCONN:
{
uint8_t advertEnabled = FALSE;
// Disable non-connectable advertising.
GAPRole_SetParameter(GAPROLE_ADV_NONCONN_ENABLED, sizeof(uint8_t),
&advertEnabled);
advertEnabled = TRUE;
// Enabled connectable advertising.
GAPRole_SetParameter(GAPROLE_ADVERT_ENABLED, sizeof(uint8_t),
&advertEnabled);
// Reset flag for next connection.
firstConnFlag = false;
HomeAutomation_freeAttRsp(bleNotConnected);
}
break;
#endif //PLUS_BROADCASTER
case GAPROLE_CONNECTED:
{
linkDBInfo_t linkInfo;
uint8_t numActive = 0;
numActive = linkDB_NumActive();
// Use numActive to determine the connection handle of the last
// connection
if ( linkDB_GetInfo( numActive - 1, &linkInfo ) == SUCCESS )
{
Display_print1(dispHandle, 2, 0, "Num Conns: %d", (uint16_t)numActive);
Display_print0(dispHandle, 3, 0, Util_convertBdAddr2Str(linkInfo.addr));
}
else
{
uint8_t peerAddress[B_ADDR_LEN];
GAPRole_GetParameter(GAPROLE_CONN_BD_ADDR, peerAddress);
Display_print0(dispHandle, 2, 0, "Connected");
Display_print0(dispHandle, 3, 0, Util_convertBdAddr2Str(peerAddress));
}
#ifdef PLUS_BROADCASTER
// Only turn advertising on for this state when we first connect
// otherwise, when we go from connected_advertising back to this state
// we will be turning advertising back on.
if (firstConnFlag == false)
{
uint8_t advertEnabled = FALSE; // Turn on Advertising
// Disable connectable advertising.
GAPRole_SetParameter(GAPROLE_ADVERT_ENABLED, sizeof(uint8_t),
&advertEnabled);
// Set to true for non-connectable advertising.
advertEnabled = TRUE;
// Enable non-connectable advertising.
GAPRole_SetParameter(GAPROLE_ADV_NONCONN_ENABLED, sizeof(uint8_t),
&advertEnabled);
firstConnFlag = true;
}
#endif // PLUS_BROADCASTER
}
break;
case GAPROLE_CONNECTED_ADV:
Display_print0(dispHandle, 2, 0, "Connected Advertising");
break;
case GAPROLE_WAITING:
HomeAutomation_resetAllModules();
HomeAutomation_freeAttRsp(bleNotConnected);
Display_print0(dispHandle, 2, 0, "Disconnected");
// Clear remaining lines
Display_clearLines(dispHandle, 3, 5);
break;