-
Notifications
You must be signed in to change notification settings - Fork 0
/
csrmodule_csr_mesh_light_阅读.c
3035 lines (2650 loc) · 105 KB
/
csrmodule_csr_mesh_light_阅读.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 Cambridge Silicon Radio Limited 2014-2015
* CSR Bluetooth Low Energy CSRmesh 1.2 Release
* Application Version 1.2
*
* FILE
* csr_mesh_light.c
*
* DESCRIPTION
* This file implements the CSR Mesh light application.
*
*****************************************************************************/
/*============================================================================*
* SDK Header Files
*============================================================================*/
#include <main.h>
#include <ls_app_if.h>
#include <gatt.h>
#include <timer.h>
#include <uart.h>
#include <pio.h>
#include <nvm.h>
#include <security.h>
#include <gatt_prim.h>
#include <mem.h>
#include <panic.h>
#include <config_store.h>
#include <random.h>
#include <buf_utils.h>
/*============================================================================*
* Local Header Files
*============================================================================*/
#include "user_config.h"
#include "nvm_access.h"
#include "csr_mesh_light.h"
#include "app_debug.h"
#include "app_gatt.h"
#include "csr_mesh_light_hw.h"
#include "gap_service.h"
#include "app_gatt_db.h"
#include "mesh_control_service.h"
#include "csr_mesh_light_gatt.h"
#include "appearance.h"
#if defined (ENABLE_GATT_OTA_SERVICE) || defined (ENABLE_FIRMWARE_MODEL)
#include "csr_ota.h"
#include "csr_ota_service.h"
#include "gatt_service.h"
#endif /* ENABLE_GATT_OTA_SERVICE || ENABLE_FIRMWARE_MODEL */
#ifdef USE_ASSOCIATION_REMOVAL_KEY
#include "iot_hw.h"
#endif /* USE_ASSOCIATION_REMOVAL_KEY */
#include "battery_hw.h"
#include "app_data_stream.h"
/*============================================================================*
* CSR Mesh Header Files
*============================================================================*/
#include <csr_mesh.h>
#include <attention_model.h>
#include <light_model.h>
#include <power_model.h>
#include <bearer_model.h>
#include <ping_model.h>
#include <debug.h>
#ifdef ENABLE_FIRMWARE_MODEL
#include <firmware_model.h>
#endif
#ifdef ENABLE_BATTERY_MODEL
#include <battery_model.h>
#endif
/*============================================================================*
* Private Definitions
*============================================================================*/
/* Association Removal Button Press Duration */
#define LONG_KEYPRESS_TIME (2 * SECOND)
/* CSRmesh Device UUID size */
#define DEVICE_UUID_SIZE_WORDS 8
/* CSRmesh Authorization Code Size in Words */
#define DEVICE_AUTHCODE_SIZE_IN_WORDS (4)
/* CS Key for user flags */
#define CSKEY_INDEX_USER_FLAGS (CSR_MESH_CS_USERKEY_INDEX_ADV_TIME + 1)
/* Used for generating Random UUID */
#define RANDOM_UUID_ENABLE_MASK (0x0001)
/* Used for permanently Enabling/Disabling Relay */
#define RELAY_ENABLE_MASK (0x0002)
/* Used for permanently Enabling/Disabling Bridge */
#define BRIDGE_ENABLE_MASK (0x0004)
/* OTA Reset Defer Duration */
#define OTA_RESET_DEFER_DURATION (500 * MILLISECOND)
/* NVM Data Write defer Duration */
#define NVM_WRITE_DEFER_DURATION (5 * SECOND)
#define MAX_APP_TIMERS (10 + MAX_CSR_MESH_TIMERS)
/* Advertisement Timer for sending device identification */
#define DEVICE_ID_ADVERT_TIME (5 * SECOND)
/* Slave device is not allowed to transmit another Connection Parameter
* Update request till time TGAP(conn_param_timeout). Refer to section 9.3.9.2,
* Vol 3, Part C of the Core 4.0 BT spec. The application should retry the
* 'connection parameter update' procedure after time TGAP(conn_param_timeout)
* which is 30 seconds.
*/
/ *从设备不允许传输另一个连接参数
*更新请求直到时间TGAP(conn_param_timeout)。 参见9.3.9.2节,
*第3卷,Core 4.0 BT规格的C部分。 应用程序应该在30秒的TGAP(conn_param_timeout)
*后'连接参数更新'程序,。
*/
#define GAP_CONN_PARAM_TIMEOUT (30 * SECOND)
/* TGAP(conn_pause_peripheral) defined in Core Specification Addendum 3 Revision
* 2. A Peripheral device should not perform a Connection Parameter Update procedure
* within TGAP(conn_pause_peripheral) after establishing a connection.
*/
/*
TGAP 由核心规范附录3版本修订2版中定义的。一个外设不应该在
它建立一个连接后的TGAP时间内,执行连接参数更新程序
*/
#define TGAP_CPP_PERIOD (1 * SECOND)
/* TGAP(conn_pause_central) defined in Core Specification Addendum 3 Revision 2.
* After the Peripheral device has no further pending actions to perform and the
* Central device has not initiated any other actions within TGAP(conn_pause_ce-
* -ntral), then the Peripheral device may perform a Connection Parameter Update
* procedure.
*/
/*
核心规范附录3修订版2中定义的TGAP.
当外设没有进一步的动作执行,且中心设备在TGAP时间内尚未初始化其它操作,则外设
可以执行 连接参数更新 程序。
*/
#define TGAP_CPC_PERIOD (1 * SECOND)
/* NVM magic version used by the 1.1 application */
#define NVM_SANITY_MAGIC_1_1 (0xAB18)
/* APP NVM version used by the 1.1 light application */
#define APP_NVM_VERSION_1_1 (2)
/* Magic value to check the sanity of NVM region used by the application. This
* value should be unique for each application as the NVM layout changes for
* every application.
*/
/*
应用程序用来检测NVM区域的合理值。
此值在每个应用程序中,都是独一无二的。正如NVM布局因每个不同的应用而变动
*/
#define NVM_SANITY_MAGIC (0xAB81)
/*Number of IRKs that application can store */
#define MAX_NUMBER_IRK_STORED (1)
/* NVM offset for the application NVM version */
#define NVM_OFFSET_SANITY_WORD (0)
/* NVM offset for NVM sanity word */
#define NVM_OFFSET_APP_NVM_VERSION (NVM_OFFSET_SANITY_WORD + 1)
/* NVM offset for CSRmesh device UUID */
#define NVM_OFFSET_DEVICE_UUID (NVM_OFFSET_APP_NVM_VERSION + 1)
/* NVM Offset for Authorization Code */
#define NVM_OFFSET_DEVICE_AUTHCODE (NVM_OFFSET_DEVICE_UUID + \
DEVICE_UUID_SIZE_WORDS)
/* NVM Offset for CSRmesh Library */
#define NVM_OFFSET_CSRMESH_LIB (NVM_OFFSET_DEVICE_AUTHCODE + \
DEVICE_AUTHCODE_SIZE_IN_WORDS)
/* Number of words of NVM used by application. Memory used by supported
* services is not taken into consideration here.
*/
/*
应用程序使用的NVM的字节数。
支撑服务的内存没有考虑在内。--- q_cl 此处 service 指的是什么?
*/
#define NVM_OFFSET_ASSOCIATION_STATE (NVM_OFFSET_CSRMESH_LIB + \
CSR_MESH_NVM_SIZE)
#define NVM_OFFSET_LIGHT_MODEL_GROUPS (NVM_OFFSET_ASSOCIATION_STATE + 1)
#define NVM_OFFSET_POWER_MODEL_GROUPS (NVM_OFFSET_LIGHT_MODEL_GROUPS + \
sizeof(light_model_groups))
#define NVM_OFFSET_ATTENTION_MODEL_GROUPS \
(NVM_OFFSET_POWER_MODEL_GROUPS + \
sizeof(power_model_groups))
#ifdef ENABLE_DATA_MODEL
#define NVM_OFFSET_DATA_MODEL_GROUPS \
(NVM_OFFSET_ATTENTION_MODEL_GROUPS + \
sizeof(attention_model_groups))
/* NVM Offset for RGB data */
#define NVM_RGB_DATA_OFFSET (NVM_OFFSET_DATA_MODEL_GROUPS + \
sizeof(data_model_groups))
#else
/* NVM Offset for RGB data */
#define NVM_RGB_DATA_OFFSET (NVM_OFFSET_ATTENTION_MODEL_GROUPS + \
sizeof(attention_model_groups))
#endif
/* Size of RGB Data in Words */
#define NVM_RGB_DATA_SIZE (2)
/* NVM offset for Bearer Model Data */
#define NVM_BEARER_DATA_OFFSET (NVM_RGB_DATA_OFFSET + \
NVM_RGB_DATA_SIZE)
/* NVM Offset for Application data */
#define NVM_MAX_APP_MEMORY_WORDS (NVM_BEARER_DATA_OFFSET + \
sizeof(BEARER_MODEL_STATE_DATA_T))
/*============================================================================*
* Public Data
*============================================================================*/
/* CSRmesh light application specific data */
/* CSRmesh 灯应用所用的数据 */
CSRMESH_LIGHT_DATA_T g_lightapp_data;
/* Application VID,PID and Version. */
CSR_MESH_VID_PID_VERSION_T vid_pid_info =
{
.vendor_id = APP_VENDOR_ID,
.product_id = APP_PRODUCT_ID,
.version = APP_VERSION,
};
/* Device Apprearance. */
CSR_MESH_APPEARANCE_T device_appearance = {APPEARANCE_ORG_BLUETOOTH_SIG,
APPEARANCE_CSRMESH_LIGHT_VALUE};
/* Device Short name */
/* 设备的名称 */
uint8 short_name[9] = "Light";
/*============================================================================*
* Private Data
*============================================================================*/
/* CSRmesh Device UUID: 128-Bit Device UUID is stored in 8 Words(16-Bits each)
* in Little Endian Format in RAM and NVM. To set a particular UUID, change the
* light_uuid array as shown in example.
* Example:
* ............................MSB.................................LSB.
* Device UUID in Hexadecimal: 0123-4567-89AB-CDEF-FEDC-BA98-7654-3210.
* Array should be set as: {0x3210, 0x7654, 0xBA98, 0xFEDC,
* 0xCDEF, 0x89AB, 0x4567, 0x0123};
* NOTE: The UUID in array below will be used only if, Bit-0 of
* CSKEY_INDEX_USER_FLAGS is Cleared.
*/
/*
CSRmesh设备UUID:一个128比特位的设备UUID存储在8个字当中(每个字16位,128 = 16 * 8)
以小端的形式存储在RAM 和 NVM 中。为了得到正常的UUID,要像下面例子那样更改 light_uuid 数组
注:下面数组中的UUID,只会在 CSKEY_INDEX_USER_FLAGS 的 BIT-0 位被清除。
*/
static uint16 light_uuid[DEVICE_UUID_SIZE_WORDS] =
{0x3210, 0x7654, 0xBA98, 0xFEDC,
0xCDEF, 0x89AB, 0x4567, 0x0123};
#ifdef USE_AUTHORIZATION_CODE
/* CSRmesh Device Authorization Code: 64-Bit Device Authorization Code is
* stored in 4 Words(16-Bits each) in Little Endian Format in RAM and NVM.
* To set a particular Authorization Code, change the
* light_auth_code array as shown in example.
* Example:
* ..........................................MSB.............LSB.
* Device Authorization Code in Hexadecimal: 0123-4567-89AB-CDEF.
* Array should be set as: {0xCDEF, 0x89AB, 0x4567, 0x0123};
*/
/*
CSRmesh设备的授权码:64比特位,4个字,小端表示法存储
*/
static uint16 light_auth_code[DEVICE_AUTHCODE_SIZE_IN_WORDS] =
{0xCDEF, 0x89AB, 0x4567, 0x0123};
#endif /* USE_AUTHORIZATION_CODE */
/* Declare space for application timers. */
static uint16 app_timers[SIZEOF_APP_TIMER * MAX_APP_TIMERS];
/* Declare space for Model Groups */
static uint16 light_model_groups[MAX_MODEL_GROUPS];
static uint16 attention_model_groups[MAX_MODEL_GROUPS];
static uint16 power_model_groups[MAX_MODEL_GROUPS];
#ifdef ENABLE_DATA_MODEL
static uint16 data_model_groups[MAX_MODEL_GROUPS];
#endif /* ENABLE_DATA_MODEL */
#ifdef USE_ASSOCIATION_REMOVAL_KEY
/* Association Button Press Timer */
static timer_id long_keypress_tid;
#endif /* USE_ASSOCIATION_REMOVAL_KEY */
/* Attention timer id */
static timer_id attn_tid = TIMER_INVALID;
#ifdef ENABLE_FIRMWARE_MODEL
/* Firmware Reset Delay Timer Id */
static timer_id ota_rst_tid = TIMER_INVALID;
#endif /* ENABLE_FIRMWARE_MODEL */
/* To send the MASP associate to NW msg and Dev appearance msg alternatively */
static bool send_dev_appearance = FALSE;
/*============================================================================*
* Private Function Prototypes
*============================================================================*/
/* UART Receive callback */
#ifdef DEBUG_ENABLE
static uint16 UartDataRxCallback ( void* p_data, uint16 data_count,
uint16* p_num_additional_words );
#endif /* DEBUG_ENABLE */
#ifdef USE_ASSOCIATION_REMOVAL_KEY
static void handlePIOEvent(pio_changed_data *data);
#endif /* USE_ASSOCIATION_REMOVAL_KEY */
/* Advert time out handler */
static void smLightDeviceIdAdvertTimeoutHandler(timer_id tid);
/*2017-5-9 此处只是申明这个函数,在后面实现*/
/* This function reads the persistent store. */
static void readPersistentStore(void);
/*============================================================================*
* Private Function Definitions
*============================================================================*/
#ifdef USE_STATIC_RANDOM_ADDRESS
/*-----------------------------------------------------------------------------*
* NAME
* generateStaticRandomAddress
*
* DESCRIPTION
* This function generates a static random address.
*
* RETURNS/MODIFIES
* Nothing
*
*----------------------------------------------------------------------------*/
static void generateStaticRandomAddress(BD_ADDR_T *addr)
{
uint16 temp[3];
uint16 idx = 0;
if (!addr) return;
for (idx = 0; idx < 3;)
{
temp[idx] = Random16();
if ((temp[idx] != 0) && (temp[idx] != 0xFFFF))
{
idx++;
}
}
addr->lap = ((uint32)(temp[1]) << 16) | (temp[0]);
addr->lap &= 0x00FFFFFFUL;
addr->uap = (temp[1] >> 8) & 0xFF;
addr->nap = temp[2];
addr->nap &= ~BD_ADDR_NAP_RANDOM_TYPE_MASK;
addr->nap |= BD_ADDR_NAP_RANDOM_TYPE_STATIC;
}
#endif /* USE_STATIC_RANDOM_ADDRESS */
#ifdef ENABLE_FIRMWARE_MODEL
/*-----------------------------------------------------------------------------*
* NAME
* issueOTAReset
*
* DESCRIPTION
* This function issues an OTA Reset.
*
* RETURNS/MODIFIES
* Nothing
*
*----------------------------------------------------------------------------*/
static void issueOTAReset(timer_id tid)
{
if (ota_rst_tid == tid)
{
ota_rst_tid = TIMER_INVALID;
/* Issue OTA Reset. */
OtaReset();
}
}
#endif /* ENABLE_FIRMWARE_MODEL */
/*-----------------------------------------------------------------------------*
* NAME
* smLightDeviceIdAdvertTimeoutHandler
*
* DESCRIPTION
* This function handles the Device ID advertise timer event.
*
* RETURNS/MODIFIES
* Nothing
*
*----------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------*
* 名称
* smLightDeviceIdAdvertTimeoutHandler
*
*说明
*此函数处理 设备ID广播 定时器事件。
*
*返回/修改
* 无
*
*----------------------------------------------------------------------------*/
static void smLightDeviceIdAdvertTimeoutHandler(timer_id tid)
{
if(tid == g_lightapp_data.mesh_device_id_advert_tid)
{
if(g_lightapp_data.assoc_state == app_state_not_associated)
{
// 灯泡未绑定
/* Generate a random delay between 0 to 511 ms */
uint32 random_delay = ((uint32)(Random16() & 0x1FF)) * (MILLISECOND);
// 灯泡不处于快速广播阶段
if (g_lightapp_data.state != app_state_fast_advertising)
{
// 未发送设备表征信息
if(send_dev_appearance == FALSE)
{
/* Send the device ID advertisements */
CsrMeshAssociateToANetwork();
send_dev_appearance = TRUE;
}
else
{
/* Send the device appearance */
// q_cl:发送设备表征信息,可用,发送其它信息?
CsrMeshAdvertiseDeviceAppearance(&device_appearance,
short_name,
sizeof(short_name));
send_dev_appearance = FALSE;
}
}
g_lightapp_data.mesh_device_id_advert_tid = TimerCreate(
(DEVICE_ID_ADVERT_TIME + random_delay),
TRUE,
smLightDeviceIdAdvertTimeoutHandler);
}
else
{
/* Device is now associated so no need to start the timer again */
g_lightapp_data.mesh_device_id_advert_tid = TIMER_INVALID;
}
}
}
/*-----------------------------------------------------------------------------*
* NAME
* smLightDataNVMWriteTimerHandler
*
* DESCRIPTION
* This function handles NVM Write Timer time-out.
* 该函数处理NVM写入事件定时器 (超时事件) --- 定时向 NVM 写入数据
* RETURNS/MODIFIES
* Nothing
*
*----------------------------------------------------------------------------*/
static void smLightDataNVMWriteTimerHandler(timer_id tid)
{
uint32 rd_data = 0;
uint32 wr_data = 0;
if (tid == g_lightapp_data.nvm_tid)
{
g_lightapp_data.nvm_tid = TIMER_INVALID;
/* Read RGB and Power Data from NVM */
Nvm_Read((uint16 *)&rd_data, sizeof(uint32),
NVM_RGB_DATA_OFFSET);
/* Pack Data for writing to NVM */
wr_data = ((uint32) g_lightapp_data.power.power_state << 24) |
((uint32) g_lightapp_data.light_state.blue << 16) |
((uint32) g_lightapp_data.light_state.green << 8) |
g_lightapp_data.light_state.red;
/* If data on NVM is not equal to current state, write current state
* to NVM.
*/
if (rd_data != wr_data)
{
Nvm_Write((uint16 *)&wr_data, sizeof(uint32),NVM_RGB_DATA_OFFSET);
}
}
}
/*-----------------------------------------------------------------------------*
* NAME
* attnTimerHandler
*
* DESCRIPTION
* This function handles Attention time-out.
*
* RETURNS/MODIFIES
* Nothing
*
*----------------------------------------------------------------------------*/
static void attnTimerHandler(timer_id tid)
{
if (attn_tid == tid)
{
attn_tid = TIMER_INVALID;
/* Restore Light State */
LightHardwareSetColor(g_lightapp_data.light_state.red,
g_lightapp_data.light_state.green,
g_lightapp_data.light_state.blue);
LightHardwarePowerControl(g_lightapp_data.power.power_state);
}
}
/*-----------------------------------------------------------------------------*
* NAME
* smAppInitiateAssociation
*
* DESCRIPTION
* This function starts timer to send CSRmesh Association Messages
* and also gives visual indication that light is not associated.
* 启动一个定时器:用来发送 设备绑定信息,并发出该 灯未被绑定的暗示信号 -- 灯光蓝色并闪烁
* RETURNS/MODIFIES
* Nothing
*
*----------------------------------------------------------------------------*/
static void smAppInitiateAssociation(void)
{
/* Generate a random delay between 0 to 511 ms */
uint32 random_delay = ((uint32)(Random16() & 0x1FF)) * (MILLISECOND);
/* Blink light to indicate that it is not associated 未绑定时的闪烁的颜色*/
LightHardwareSetBlink(0, 0, 127, 32, 32);
/* Start a timer to send Device ID messages periodically to get
* associated to a network
*/
/*
启动一个定时器,用来周期发送设备ID信息,以便跟网络绑定上
q_cl : device ID 怎么由设备自己确定? a_cl : 从前面 line:426 可知,应该不
包含 deviceID 信息,只是自己的表征
*/
g_lightapp_data.mesh_device_id_advert_tid =
TimerCreate((random_delay + DEVICE_ID_ADVERT_TIME),
TRUE,
smLightDeviceIdAdvertTimeoutHandler);
}
/*-----------------------------------------------------------------------------*
* NAME
* togglePowerState
*
* DESCRIPTION
* This function toggles the power state.
*
* RETURNS/MODIFIES
* Nothing
*
*----------------------------------------------------------------------------*/
static void togglePowerState(void)
{
POWER_STATE_T curr_state = g_lightapp_data.power.power_state;
switch (curr_state)
{
case POWER_STATE_ON:
g_lightapp_data.power.power_state = POWER_STATE_OFF;
break;
case POWER_STATE_OFF:
g_lightapp_data.power.power_state = POWER_STATE_ON;
break;
case POWER_STATE_ON_FROM_STANDBY:
g_lightapp_data.power.power_state = POWER_STATE_STANDBY;
break;
case POWER_STATE_STANDBY:
g_lightapp_data.power.power_state = POWER_STATE_ON_FROM_STANDBY;
break;
default:
break;
}
}
#ifdef USE_ASSOCIATION_REMOVAL_KEY
/*-----------------------------------------------------------------------------*
* NAME
* longKeyPressTimeoutHandler
*
* DESCRIPTION
* This function handles the long key press timer event.
*
* RETURNS/MODIFIES
* Nothing
*
*----------------------------------------------------------------------------*/
static void longKeyPressTimeoutHandler(timer_id tid)
{
if (long_keypress_tid == tid)
{
long_keypress_tid = TIMER_INVALID;
}
}
/*-----------------------------------------------------------------------------*
* NAME
* handlePIOEvent
*
* DESCRIPTION
* This function handles the PIO Events.
*
* RETURNS/MODIFIES
* Nothing
*
*----------------------------------------------------------------------------*/
void handlePIOEvent(pio_changed_data *data)
{
/* If Switch-2 related event, then process further. Otherwise ignore */
/*如果Switch-2相关事件,则进一步处理。 否则忽略*/
if (data->pio_cause & SW2_MASK)
{
/* Button Pressed */
if (!(data->pio_state & SW2_MASK))
{
TimerDelete(long_keypress_tid);
long_keypress_tid = TimerCreate(LONG_KEYPRESS_TIME, TRUE,
longKeyPressTimeoutHandler);
}
else /* Button Released */
{
/* Button released after long press */
if (TIMER_INVALID == long_keypress_tid)
{
if (app_state_not_associated != g_lightapp_data.assoc_state)
{
/* Reset Association Information */
CsrMeshReset();
/* Set state to un-associated */
g_lightapp_data.assoc_state = app_state_not_associated;
/* Write association state to NVM */
Nvm_Write((uint16 *)&g_lightapp_data.assoc_state,
sizeof(g_lightapp_data.assoc_state),
NVM_OFFSET_ASSOCIATION_STATE);
}
}
else /* Button released after a short press */
{
if (app_state_not_associated == g_lightapp_data.assoc_state)
{
/* Delete Long Key Press Timer */
TimerDelete(long_keypress_tid);
/* Start Association to CSRmesh */
smAppInitiateAssociation();
}
}
}
}
}
#endif /* USE_ASSOCIATION_REMOVAL_KEY */
/*----------------------------------------------------------------------------*
* NAME
* requestConnParamUpdate
*
* DESCRIPTION
* This function is used to send L2CAP_CONNECTION_PARAMETER_UPDATE_REQUEST
* to the remote device when an earlier sent request had failed.
*
该函数用来发送 L2CAP_..._REQUEST 到远程设备上,当之前的请求发送失败
q_cl : --- 指的是哪个?
* RETURNS
* Nothing.
*
*---------------------------------------------------------------------------*/
static void requestConnParamUpdate(timer_id tid)
{
/* Application specific preferred parameters */
ble_con_params app_pref_conn_param;
if(g_lightapp_data.gatt_data.con_param_update_tid == tid)
{
g_lightapp_data.gatt_data.con_param_update_tid = TIMER_INVALID;
g_lightapp_data.gatt_data.cpu_timer_value = 0;
/*Handling signal as per current state */
switch(g_lightapp_data.state)
{
case app_state_connected:
{
/* Increment the count for Connection Parameter Update
* requests
*/
// 每次连接参数更新都自增一次
++ g_lightapp_data.gatt_data.num_conn_update_req;
/* If it is first or second request, preferred connection
* parameters should be request
*/
// 前两次请求,应该请求的首选参数
if(g_lightapp_data.gatt_data.num_conn_update_req == 1 ||
g_lightapp_data.gatt_data.num_conn_update_req == 2)
{
app_pref_conn_param.con_max_interval =
PREFERRED_MAX_CON_INTERVAL;
app_pref_conn_param.con_min_interval =
PREFERRED_MIN_CON_INTERVAL;
app_pref_conn_param.con_slave_latency =
PREFERRED_SLAVE_LATENCY;
app_pref_conn_param.con_super_timeout =
PREFERRED_SUPERVISION_TIMEOUT;
}
/* If it is 3rd or 4th request, APPLE compliant parameters
* should be requested.
*/
// 第三、四次请求时,应该选择与APPLE兼容性参数
else if(g_lightapp_data.gatt_data.num_conn_update_req == 3 ||
g_lightapp_data.gatt_data.num_conn_update_req == 4)
{
app_pref_conn_param.con_max_interval =
APPLE_MAX_CON_INTERVAL;
app_pref_conn_param.con_min_interval =
APPLE_MIN_CON_INTERVAL;
app_pref_conn_param.con_slave_latency =
APPLE_SLAVE_LATENCY;
app_pref_conn_param.con_super_timeout =
APPLE_SUPERVISION_TIMEOUT;
}
/* Send Connection Parameter Update request using application
* specific preferred connection parameters
*/
// 使用应用具体的首选参数
if(LsConnectionParamUpdateReq(
&g_lightapp_data.gatt_data.con_bd_addr,
&app_pref_conn_param) != ls_err_none)
{
ReportPanic(app_panic_con_param_update);
}
}
break;
default:
/* Ignore in other states */
break;
}
} /* Else ignore the timer */
}
/*-----------------------------------------------------------------------------*
* NAME
* handleGapCppTimerExpiry
*
* DESCRIPTION
* This function handles the expiry of TGAP(conn_pause_peripheral) timer.
* It starts the TGAP(conn_pause_central) timer, during which, if no activ-
* -ity is detected from the central device, a connection parameter update
* request is sent.
*
该函数处理TGAP定时器的过期。该函数启动一个TGAP定时器,如果没有检测到主设备
的活动,一个 连接参数更新 请求 就会被发送出去
* RETURNS
* Nothing.
*
*----------------------------------------------------------------------------*/
static void handleGapCppTimerExpiry(timer_id tid)
{
if(g_lightapp_data.gatt_data.con_param_update_tid == tid)
{
g_lightapp_data.gatt_data.con_param_update_tid =
TimerCreate(TGAP_CPC_PERIOD, TRUE,
requestConnParamUpdate);
g_lightapp_data.gatt_data.cpu_timer_value = TGAP_CPC_PERIOD;
}
}
/*----------------------------------------------------------------------------*
* NAME
* smLightDataInit
*
* DESCRIPTION
* This function is called to initialise CSRmesh light application
* data structure.
*
* RETURNS
* Nothing.
*
*---------------------------------------------------------------------------*/
static void smLightDataInit(void)
{
/* Reset/Delete all the timers */
TimerDelete(g_lightapp_data.gatt_data.app_tid);
g_lightapp_data.gatt_data.app_tid = TIMER_INVALID;
TimerDelete(g_lightapp_data.gatt_data.con_param_update_tid);
g_lightapp_data.gatt_data.con_param_update_tid = TIMER_INVALID;
g_lightapp_data.gatt_data.cpu_timer_value = 0;
g_lightapp_data.gatt_data.st_ucid = GATT_INVALID_UCID;
g_lightapp_data.gatt_data.advert_timer_value = 0;
/* Reset the connection parameter variables. */
g_lightapp_data.gatt_data.conn_interval = 0;
g_lightapp_data.gatt_data.conn_latency = 0;
g_lightapp_data.gatt_data.conn_timeout = 0;
/* Initialise GAP Data structure */
GapDataInit();
/* Initialize the Mesh Control Service Data Structure */
// 初始化 Mesh 控制服务数据结构
MeshControlServiceDataInit();
#ifdef ENABLE_GATT_OTA_SERVICE
/* Initialise GATT Data structure */
GattDataInit();
/* Initialise the CSR OTA Service Data */
OtaDataInit();
#endif /* ENABLE_GATT_OTA_SERVICE */
}
/*----------------------------------------------------------------------------*
* NAME
* readPersistentStore
*
* DESCRIPTION
* This function is used to initialize and read NVM data
*
// 初始化并读取 NVM 数据
* RETURNS/MODIFIES
* Nothing.
*
*----------------------------------------------------------------------------*/
static void readPersistentStore(void)
{
/* NVM offset for supported services */
uint16 nvm_offset = 0;
uint16 nvm_sanity = 0xffff;
uint16 app_nvm_version = 0;
uint32 temp = 0;
nvm_offset = NVM_MAX_APP_MEMORY_WORDS;
/* Read the sanity word */
Nvm_Read(&nvm_sanity, sizeof(nvm_sanity),
NVM_OFFSET_SANITY_WORD);
/* Read the Application NVM version */
Nvm_Read(&app_nvm_version, 1, NVM_OFFSET_APP_NVM_VERSION);
if(nvm_sanity == NVM_SANITY_MAGIC &&
app_nvm_version == APP_NVM_VERSION )
{
g_lightapp_data.gatt_data.paired = FALSE;
/* Read RGB and Power Data from NVM */
Nvm_Read((uint16 *)&temp, sizeof(uint32),
NVM_RGB_DATA_OFFSET);
/* Read assigned Group IDs for Light model from NVM */
Nvm_Read((uint16 *)light_model_groups, sizeof(light_model_groups),
NVM_OFFSET_LIGHT_MODEL_GROUPS);
/* Read assigned Group IDs for Power model from NVM */
Nvm_Read((uint16 *)power_model_groups, sizeof(power_model_groups),
NVM_OFFSET_POWER_MODEL_GROUPS);
/* Read assigned Group IDs for Attention model from NVM */
Nvm_Read((uint16 *)attention_model_groups, sizeof(attention_model_groups),
NVM_OFFSET_ATTENTION_MODEL_GROUPS);
#ifdef ENABLE_DATA_MODEL
/* Read assigned Group IDs for data stream model from NVM */
Nvm_Read((uint16 *)data_model_groups, sizeof(data_model_groups),
NVM_OFFSET_DATA_MODEL_GROUPS);
#endif /* ENABLE_DATA_MODEL */
/* Unpack data in to the global variables */
g_lightapp_data.light_state.red = temp & 0xFF;
temp >>= 8;
g_lightapp_data.light_state.green = temp & 0xFF;
temp >>= 8;
g_lightapp_data.light_state.blue = temp & 0xFF;
temp >>= 8;
g_lightapp_data.power.power_state = temp & 0xFF;
g_lightapp_data.light_state.power = g_lightapp_data.power.power_state;
/* Read Bearer Model Data from NVM */
Nvm_Read((uint16 *)&g_lightapp_data.bearer_data,
sizeof(BEARER_MODEL_STATE_DATA_T), NVM_BEARER_DATA_OFFSET);
/* If NVM in use, read device name and length from NVM */
GapReadDataFromNVM(&nvm_offset);
}
else
{
/* Read Configuration flags from User CS Key */
uint16 cskey_flags = CSReadUserKey(CSKEY_INDEX_USER_FLAGS);
/* If the NVM_SANITY word the APP_NVM_VERSION matches the 1.1 version
* retain the association data on the NVM and update the versions to
* 1.2
*/
if(nvm_sanity == NVM_SANITY_MAGIC_1_1 &&
app_nvm_version == APP_NVM_VERSION_1_1)
{
nvm_sanity = NVM_SANITY_MAGIC;
/* Write NVM Sanity word to the NVM */
Nvm_Write(&nvm_sanity, sizeof(nvm_sanity), NVM_OFFSET_SANITY_WORD);
}
if(nvm_sanity != NVM_SANITY_MAGIC)
{
uint16 nvm_invalid_value = 0;
uint8 i;
/* NVM Sanity check failed means either the device is being brought
* up for the first time or memory has got corrupted in which case
* discard the data and start fresh.
*/
nvm_sanity = NVM_SANITY_MAGIC;
/* Write NVM Sanity word to the NVM */
Nvm_Write(&nvm_sanity, sizeof(nvm_sanity),
NVM_OFFSET_SANITY_WORD);
if (cskey_flags & RANDOM_UUID_ENABLE_MASK)
{
/* The flag is set so generate a random UUID and store it on NVM */
for( i = 0 ; i < DEVICE_UUID_SIZE_WORDS ; i++)
{
light_uuid[i] = Random16();
}
/* Write to NVM */
Nvm_Write(light_uuid, DEVICE_UUID_SIZE_WORDS,
NVM_OFFSET_DEVICE_UUID);
}
else
{
/* A mass production tool may be used to program the device
* firmware as well as the device UUID and authorisation code
* in which case the NVM sanity check fails but the NVM will
* have a valid UUID and Authorisation code.
* So write the default UUID and AC only if all the words of the
*
*/
uint16 temp_word;
for( i = 0; i < DEVICE_UUID_SIZE_WORDS ; i++)
{
Nvm_Read(&temp_word, 1, NVM_OFFSET_DEVICE_UUID + i);
if( temp_word != NVM_DEFAULT_ERASED_WORD)
{
/* Atleast one word is not same default erased word
* read the UUID from NVM
*/
Nvm_Read(light_uuid, DEVICE_UUID_SIZE_WORDS,
NVM_OFFSET_DEVICE_UUID);
break;
}
}
if( i == DEVICE_UUID_SIZE_WORDS)