forked from hcrest/bno080-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sh2.c
2083 lines (1708 loc) · 58.7 KB
/
sh2.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 2015-17 Hillcrest Laboratories, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License and
* any applicable agreements you may have with Hillcrest Laboratories, Inc.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* SH-2 API Implementation for Hillcrest BNO080
*/
#include <string.h>
#include <stdio.h>
#include "sh2.h"
#include "shtp.h"
#include "sh2_hal.h"
#include "sh2_err.h"
#include "sh2_util.h"
// Max length of an FRS record, words.
#define MAX_FRS_WORDS (72)
// Max length of sensorhub version string.
#define MAX_VER_LEN (16)
// Max number of report ids supported
#define SH2_MAX_REPORT_IDS (64)
// Tags for sensorhub app advertisements.
#define TAG_SH2_VERSION (0x80)
#define TAG_SH2_REPORT_LENGTHS (0x81)
// executable/device channel responses
#define EXECUTABLE_DEVICE_CMD_RESET (1)
#define EXECUTABLE_DEVICE_CMD_ON (2)
#define EXECUTABLE_DEVICE_CMD_SLEEP (3)
// executable/device channel responses
#define EXECUTABLE_DEVICE_RESP_RESET_COMPLETE (1)
// --- Private Data Types -------------------------------------------------
typedef int (sh2_OpStart_t)(void);
typedef void (sh2_OpTxDone_t)(void);
typedef void (sh2_OpRx_t)(const uint8_t *payload, uint16_t len);
typedef struct sh2_Op_s {
sh2_OpStart_t *start;
sh2_OpTxDone_t *txDone;
sh2_OpRx_t *rx;
} sh2_Op_t;
// Report definitions
// Bit fields for Feature Report flags
#define FEAT_CHANGE_SENSITIVITY_RELATIVE (1)
#define FEAT_CHANGE_SENSITIVITY_ABSOLUTE (0)
#define FEAT_CHANGE_SENSITIVITY_ENABLED (2)
#define FEAT_CHANGE_SENSITIVITY_DISABLED (0)
#define FEAT_WAKE_ENABLED (4)
#define FEAT_WAKE_DISABLED (0)
#define FEAT_ALWAYS_ON_ENABLED (8)
#define FEAT_ALWAYS_ON_DISABLED (0)
#if defined(_MSC_VER)
#define PACKED_STRUCT struct
#pragma pack(push, 1)
#elif defined(__GNUC__)
#define PACKED_STRUCT struct __attribute__((packed))
#else
#define PACKED_STRUCT __packed struct
#endif
// GET_FEATURE_REQ
#define SENSORHUB_GET_FEATURE_REQ (0xFE)
typedef PACKED_STRUCT {
uint8_t reportId;
uint8_t featureReportId;
} GetFeatureReq_t;
// SENSORHUB_SET_FEATURE_CMD
#define SENSORHUB_SET_FEATURE_CMD (0xFD)
typedef PACKED_STRUCT {
uint8_t reportId; // 0xFD
uint8_t featureReportId; // sensor id
uint8_t flags; // FEAT_... values
uint16_t changeSensitivity;
uint32_t reportInterval_uS;
uint32_t batchInterval_uS;
uint32_t sensorSpecific;
} SetFeatureReport_t;
// SENSORHUB_GET_FEATURE_RESP
#define SENSORHUB_GET_FEATURE_RESP (0xFC)
typedef PACKED_STRUCT {
uint8_t reportId;
uint8_t featureReportId; // sensor id
uint8_t flags; // FEAT_... values
uint16_t changeSensitivity;
uint32_t reportInterval_uS;
uint32_t batchInterval_uS;
uint32_t sensorSpecific;
} GetFeatureResp_t;
#define SENSORHUB_BASE_TIMESTAMP_REF (0xFB)
typedef PACKED_STRUCT {
uint8_t reportId;
uint32_t timebase;
} BaseTimestampRef_t;
#define SENSORHUB_TIMESTAMP_REBASE (0xFA)
typedef PACKED_STRUCT {
uint8_t reportId;
int32_t timebase;
} TimestampRebase_t;
// SENSORHUB_PROD_ID_REQ
#define SENSORHUB_PROD_ID_REQ (0xF9)
typedef PACKED_STRUCT {
uint8_t reportId;
uint8_t reserved;
} ProdIdReq_t;
// SENSORHUB_PROD_ID_RESP
#define SENSORHUB_PROD_ID_RESP (0xF8)
typedef PACKED_STRUCT {
uint8_t reportId;
uint8_t resetCause;
uint8_t swVerMajor;
uint8_t swVerMinor;
uint32_t swPartNumber;
uint32_t swBuildNumber;
uint16_t swVerPatch;
uint8_t reserved0;
uint8_t reserved1;
} ProdIdResp_t;
// SENSORHUB_FRS_WRITE_REQ
#define SENSORHUB_FRS_WRITE_REQ (0xF7)
typedef PACKED_STRUCT {
uint8_t reportId;
uint8_t reserved;
uint16_t length;
uint16_t frsType;
} FrsWriteReq_t;
// SENSORHUB_FRS_WRITE_DATA_REQ
#define SENSORHUB_FRS_WRITE_DATA_REQ (0xF6)
typedef PACKED_STRUCT {
uint8_t reportId;
uint8_t reserved;
uint16_t offset;
uint32_t data0;
uint32_t data1;
} FrsWriteDataReq_t;
// FRS write status values
#define FRS_WRITE_STATUS_RECEIVED (0)
#define FRS_WRITE_STATUS_UNRECOGNIZED_FRS_TYPE (1)
#define FRS_WRITE_STATUS_BUSY (2)
#define FRS_WRITE_STATUS_WRITE_COMPLETED (3)
#define FRS_WRITE_STATUS_READY (4)
#define FRS_WRITE_STATUS_FAILED (5)
#define FRS_WRITE_STATUS_NOT_READY (6) // data received when not in write mode
#define FRS_WRITE_STATUS_INVALID_LENGTH (7)
#define FRS_WRITE_STATUS_RECORD_VALID (8)
#define FRS_WRITE_STATUS_INVALID_RECORD (9)
#define FRS_WRITE_STATUS_DEVICE_ERROR (10)
#define FRS_WRITE_STATUS_READ_ONLY (11)
// SENSORHUB_FRS_WRITE_RESP
#define SENSORHUB_FRS_WRITE_RESP (0xF5)
typedef PACKED_STRUCT {
uint8_t reportId;
uint8_t status;
uint16_t wordOffset;
} FrsWriteResp_t;
// RESP_FRS_READ_REQ
#define SENSORHUB_FRS_READ_REQ (0xF4)
typedef PACKED_STRUCT {
uint8_t reportId;
uint8_t reserved;
uint16_t readOffset;
uint16_t frsType;
uint16_t blockSize;
} FrsReadReq_t;
// Get Datalen portion of len_status field
#define FRS_READ_DATALEN(x) ((x >> 4) & 0x0F)
// Get status portion of len_status field
#define FRS_READ_STATUS(x) ((x) & 0x0F)
// Status values
#define FRS_READ_STATUS_NO_ERROR 0
#define FRS_READ_STATUS_UNRECOGNIZED_FRS_TYPE 1
#define FRS_READ_STATUS_BUSY 2
#define FRS_READ_STATUS_READ_RECORD_COMPLETED 3
#define FRS_READ_STATUS_OFFSET_OUT_OF_RANGE 4
#define FRS_READ_STATUS_RECORD_EMPTY 5
#define FRS_READ_STATUS_READ_BLOCK_COMPLETED 6
#define FRS_READ_STATUS_READ_BLOCK_AND_RECORD_COMPLETED 7
#define FRS_READ_STATUS_DEVICE_ERROR 8
// SENSORHUB_FRS_READ_RESP
#define SENSORHUB_FRS_READ_RESP (0xF3)
typedef PACKED_STRUCT {
uint8_t reportId;
uint8_t len_status; // See FRS_READ... macros above
uint16_t wordOffset;
uint32_t data0;
uint32_t data1;
uint16_t frsType;
uint8_t reserved0;
uint8_t reserved1;
} FrsReadResp_t;
// Command and Subcommand values
#define SH2_CMD_ERRORS 1
#define SH2_CMD_COUNTS 2
#define SH2_COUNTS_GET_COUNTS 0
#define SH2_COUNTS_CLEAR_COUNTS 1
#define SH2_CMD_TARE 3
#define SH2_TARE_TARE_NOW 0
#define SH2_TARE_PERSIST_TARE 1
#define SH2_TARE_SET_REORIENTATION 2
#define SH2_CMD_INITIALIZE 4
#define SH2_INIT_SYSTEM 1
#define SH2_INIT_UNSOLICITED 0x80
#define SH2_CMD_FRS 5
#define SH2_CMD_DCD 6
#define SH2_CMD_ME_CAL 7
#define SH2_CMD_SYNC 8 // Obsolete
#define SH2_SYNC_SYNC_NOW 0 // Obsolete
#define SH2_SYNC_ENABLE_EXT_SYNC 1 // Obsolete
#define SH2_SYNC_DISABLE_EXT_SYNC 2 // Obsolete
#define SH2_CMD_DCD_SAVE 9
#define SH2_CMD_GET_OSC_TYPE 10
#define SH2_CMD_CLEAR_DCD_AND_RESET 11
#define SH2_CMD_CAL 12
#define SH2_CAL_START 0
#define SH2_CAL_FINISH 1
// SENSORHUB_COMMAND_REQ
#define SENSORHUB_COMMAND_REQ (0xF2)
typedef PACKED_STRUCT {
uint8_t reportId;
uint8_t seq;
uint8_t command;
uint8_t p[9];
} CommandReq_t;
// SENSORHUB_COMMAND_RESP
#define SENSORHUB_COMMAND_RESP (0xF1)
typedef PACKED_STRUCT {
uint8_t reportId;
uint8_t seq;
uint8_t command;
uint8_t commandSeq;
uint8_t respSeq;
uint8_t r[11];
} CommandResp_t;
// SENSORHUB_FORCE_SENSOR_FLUSH
#define SENSORHUB_FORCE_SENSOR_FLUSH (0xF0)
typedef PACKED_STRUCT {
uint8_t reportId;
uint8_t sensorId;
} ForceFlushReq_t;
// SENSORHUB_FLUSH_COMPLETED
#define SENSORHUB_FLUSH_COMPLETED (0xEF)
typedef PACKED_STRUCT {
uint8_t reportId;
uint8_t sensorId;
} ForceFlushResp_t;
#ifdef _MSC_VER
#pragma pack(pop)
#endif
typedef struct sh2_s {
uint8_t controlChan;
char version[MAX_VER_LEN+1];
struct {
uint8_t id;
uint8_t len;
} report[SH2_MAX_REPORT_IDS];
uint32_t emptyPayloads;
uint32_t unknownReportIds;
uint32_t execBadPayload;
bool advertDone;
bool gotInitResp;
bool calledResetCallback;
sh2_EventCallback_t *eventCallback;
void * eventCallbackCookie;
sh2_SensorCallback_t *sensorCallback;
void * sensorCallbackCookie;
uint8_t nextCmdSeq;
const sh2_Op_t *pOp;
int opStatus;
uint32_t frsData[MAX_FRS_WORDS];
uint16_t frsDataLen;
// Parameters and state information for the operation in progress
union {
struct {
CommandReq_t req;
} sendCmd;
struct {
sh2_ProductIds_t *pProdIds;
uint8_t nextEntry;
uint8_t expectedEntries;
} getProdIds;
struct {
sh2_SensorConfig_t *pConfig;
sh2_SensorId_t sensorId;
} getSensorConfig;
struct {
const sh2_SensorConfig_t *pConfig;
sh2_SensorId_t sensorId;
} setSensorConfig;
struct {
uint16_t frsType;
uint32_t *pData;
uint16_t *pWords;
uint16_t nextOffset;
sh2_SensorMetadata_t *pMetadata;
} getFrs;
struct {
uint16_t frsType;
uint32_t *pData;
uint16_t words;
uint16_t offset;
} setFrs;
struct {
uint8_t seq;
uint8_t severity;
sh2_ErrorRecord_t *pErrors;
uint16_t *pNumErrors;
uint16_t errsRead;
} getErrors;
struct {
uint8_t seq;
sh2_SensorId_t sensorId;
sh2_Counts_t *pCounts;
} getCounts;
struct {
uint8_t seq;
} reinit;
struct {
uint8_t seq;
} saveDcdNow;
struct {
uint8_t sensors;
uint8_t seq;
} calConfig;
struct {
uint8_t *pSensors;
uint8_t seq;
} getCalConfig;
struct {
sh2_SensorId_t sensorId;
} forceFlush;
struct {
uint8_t seq;
sh2_OscType_t *pOscType;
} getOscType;
struct {
uint32_t interval_us;
uint8_t seq;
} startCal;
struct {
sh2_CalStatus_t status;
uint8_t seq;
} finishCal;
} opData;
} sh2_t;
// --- Forward Declarations -----------------------------------------------
static int16_t toQ14(double x);
static void setupCmdParams(uint8_t cmd, uint8_t p[9]);
static void setupCmd0(uint8_t cmd);
static void setupCmd1(uint8_t cmd, uint8_t p0);
static void executableAdvertHdlr(void *cookie, uint8_t tag, uint8_t len, uint8_t *val);
static void executableDeviceHdlr(void *cookie, uint8_t *payload, uint16_t len, uint32_t timestamp);
static void sensorhubAdvertHdlr(void *cookie, uint8_t tag, uint8_t len, uint8_t *val);
static void sensorhubControlHdlr(void *cookie, uint8_t *payload, uint16_t len, uint32_t timestamp);
static void sensorhubInputNormalHdlr(void *cookie, uint8_t *payload, uint16_t len, uint32_t timestamp);
static void sensorhubInputWakeHdlr(void *cookie, uint8_t *payload, uint16_t len, uint32_t timestamp);
static void sensorhubInputGyroRvHdlr(void *cookie, uint8_t *payload, uint16_t len, uint32_t timestamp);
static uint8_t getReportLen(uint8_t reportId);
// SH-2 transaction phases
static int opStart(const sh2_Op_t *pOp);
static void opTxDone(void);
static void opRx(const uint8_t *payload, uint16_t len);
static int opCompleted(int status);
static uint64_t touSTimestamp(uint32_t hostInt, int32_t referenceDelta, uint16_t delay);
// --- Private Data -------------------------------------------------------
static sh2_t sh2;
// SH-2 Transaction handlers
// Operation to Send a Command, No response
static int sendCmdStart(void);
static void sendCmdTxDone(void);
const sh2_Op_t sendCmdOp = {
.start = sendCmdStart,
.txDone = sendCmdTxDone,
};
// Operation to get product id
static int getProdIdStart(void);
static void getProdIdRx(const uint8_t *payload, uint16_t len);
const sh2_Op_t getProdIdOp = {
.start = getProdIdStart,
.rx = getProdIdRx,
};
// getSensorConfig Operation
static int getSensorConfigStart(void);
static void getSensorConfigRx(const uint8_t *payload, uint16_t len);
const sh2_Op_t getSensorConfigOp = {
.start = getSensorConfigStart,
.rx = getSensorConfigRx,
};
// setSensorConfig Operation
static int setSensorConfigStart(void);
static void setSensorConfigTxDone(void);
const sh2_Op_t setSensorConfigOp = {
.start = setSensorConfigStart,
.txDone = setSensorConfigTxDone,
};
// get FRS Operation
static int getFrsStart(void);
static void getFrsRx(const uint8_t *payload, uint16_t len);
const sh2_Op_t getFrsOp = {
.start = getFrsStart,
.rx = getFrsRx,
};
// set FRS Operation
static int setFrsStart(void);
static void setFrsRx(const uint8_t *payload, uint16_t len);
const sh2_Op_t setFrsOp = {
.start = setFrsStart,
.rx = setFrsRx,
};
// get errors operation
static int getErrorsStart(void);
static void getErrorsRx(const uint8_t *payload, uint16_t len);
const sh2_Op_t getErrorsOp = {
.start = getErrorsStart,
.rx = getErrorsRx,
};
// get counts operation
static int getCountsStart(void);
static void getCountsRx(const uint8_t *payload, uint16_t len);
const sh2_Op_t getCountsOp = {
.start = getCountsStart,
.rx = getCountsRx,
};
// reinitialize operation
static int reinitStart(void);
static void reinitRx(const uint8_t *payload, uint16_t len);
const sh2_Op_t reinitOp = {
.start = reinitStart,
.rx = reinitRx,
};
// save dcd now operation
static int saveDcdNowStart(void);
static void saveDcdNowRx(const uint8_t *payload, uint16_t len);
const sh2_Op_t saveDcdNowOp = {
.start = saveDcdNowStart,
.rx = saveDcdNowRx,
};
// cal config operation
static int calConfigStart(void);
static void calConfigRx(const uint8_t *payload, uint16_t len);
const sh2_Op_t calConfigOp = {
.start = calConfigStart,
.rx = calConfigRx,
};
// get cal config operation
static int getCalConfigStart(void);
static void getCalConfigRx(const uint8_t *payload, uint16_t len);
const sh2_Op_t getCalConfigOp = {
.start = getCalConfigStart,
.rx = getCalConfigRx,
};
// force flush operation
static int forceFlushStart(void);
static void forceFlushRx(const uint8_t *payload, uint16_t len);
const sh2_Op_t forceFlushOp = {
.start = forceFlushStart,
.rx = forceFlushRx,
};
// getOscType Operation
static int getOscTypeStart(void);
static void getOscTypeRx(const uint8_t *payload, uint16_t len);
const sh2_Op_t getOscTypeOp = {
.start = getOscTypeStart,
.rx = getOscTypeRx,
};
// startCalibration Operation
static int startCalStart(void);
static void startCalRx(const uint8_t *payload, uint16_t len);
const sh2_Op_t startCalOp = {
.start = startCalStart,
.rx = startCalRx,
};
// finishCalibration Operation
static int finishCalStart(void);
static void finishCalRx(const uint8_t *payload, uint16_t len);
const sh2_Op_t finishCalOp = {
.start = finishCalStart,
.rx = finishCalRx,
};
// --- Public API ---------------------------------------------------------
// sh2_init
int sh2_initialize(sh2_EventCallback_t *eventCallback, void *resetCookie)
{
sh2.controlChan = 0xFF; // An invalid value since we don't know yet.
sh2.emptyPayloads = 0;
sh2.unknownReportIds = 0;
sh2.advertDone = false;
sh2.gotInitResp = false;
sh2.calledResetCallback = false;
sh2.eventCallback = eventCallback;
sh2.eventCallbackCookie = resetCookie;
sh2.sensorCallback = 0;
sh2.sensorCallbackCookie = 0;
sh2.pOp = 0;
for (int n = 0; n < SH2_MAX_REPORT_IDS; n++) {
sh2.report[n].id = 0;
sh2.report[n].len = 0;
}
sh2.nextCmdSeq = 0;
// init SHTP layer
shtp_init();
// Register SH2 handlers
shtp_listenAdvert("sensorhub", sensorhubAdvertHdlr, NULL);
shtp_listenChan("sensorhub", "control", sensorhubControlHdlr, NULL);
shtp_listenChan("sensorhub", "inputNormal", sensorhubInputNormalHdlr, NULL);
shtp_listenChan("sensorhub", "inputWake", sensorhubInputWakeHdlr, NULL);
shtp_listenChan("sensorhub", "inputGyroRv", sensorhubInputGyroRvHdlr, NULL);
sh2.execBadPayload = 0;
// Register EXECUTABLE handlers
shtp_listenAdvert("executable", executableAdvertHdlr, NULL);
shtp_listenChan("executable", "device", executableDeviceHdlr, NULL);
// Start SHTP operations (resets sensor hub in non-dfu mode)
shtp_start(false);
return SH2_OK;
}
int sh2_setSensorCallback(sh2_SensorCallback_t *callback, void *cookie)
{
sh2.sensorCallback = callback;
sh2.sensorCallbackCookie = cookie;
return SH2_OK;
}
int sh2_getProdIds(sh2_ProductIds_t *pProdIds)
{
sh2.opData.getProdIds.pProdIds = pProdIds;
sh2.opData.getProdIds.nextEntry = 0;
sh2.opData.getProdIds.expectedEntries = 4; // Most products supply 4 product ids.
// When the first arrives, we'll know if
// we need to adjust this.
return opStart(&getProdIdOp);
}
int sh2_getSensorConfig(sh2_SensorId_t sensorId, sh2_SensorConfig_t *config)
{
sh2.opData.getSensorConfig.sensorId = sensorId;
sh2.opData.getSensorConfig.pConfig = config;
return opStart(&getSensorConfigOp);
}
int sh2_setSensorConfig(sh2_SensorId_t sensorId, const sh2_SensorConfig_t *pConfig)
{
// Set up operation
sh2.opData.setSensorConfig.sensorId = sensorId;
sh2.opData.setSensorConfig.pConfig = pConfig;
return opStart(&setSensorConfigOp);
}
const static struct {
sh2_SensorId_t sensorId;
uint16_t recordId;
} sensorToRecordMap[] = {
{ SH2_RAW_ACCELEROMETER, FRS_ID_META_RAW_ACCELEROMETER },
{ SH2_ACCELEROMETER, FRS_ID_META_ACCELEROMETER },
{ SH2_LINEAR_ACCELERATION, FRS_ID_META_LINEAR_ACCELERATION },
{ SH2_GRAVITY, FRS_ID_META_GRAVITY },
{ SH2_RAW_GYROSCOPE, FRS_ID_META_RAW_GYROSCOPE },
{ SH2_GYROSCOPE_CALIBRATED, FRS_ID_META_GYROSCOPE_CALIBRATED },
{ SH2_GYROSCOPE_UNCALIBRATED, FRS_ID_META_GYROSCOPE_UNCALIBRATED },
{ SH2_RAW_MAGNETOMETER, FRS_ID_META_RAW_MAGNETOMETER },
{ SH2_MAGNETIC_FIELD_CALIBRATED, FRS_ID_META_MAGNETIC_FIELD_CALIBRATED },
{ SH2_MAGNETIC_FIELD_UNCALIBRATED, FRS_ID_META_MAGNETIC_FIELD_UNCALIBRATED },
{ SH2_ROTATION_VECTOR, FRS_ID_META_ROTATION_VECTOR },
{ SH2_GAME_ROTATION_VECTOR, FRS_ID_META_GAME_ROTATION_VECTOR },
{ SH2_GEOMAGNETIC_ROTATION_VECTOR, FRS_ID_META_GEOMAGNETIC_ROTATION_VECTOR },
{ SH2_PRESSURE, FRS_ID_META_PRESSURE },
{ SH2_AMBIENT_LIGHT, FRS_ID_META_AMBIENT_LIGHT },
{ SH2_HUMIDITY, FRS_ID_META_HUMIDITY },
{ SH2_PROXIMITY, FRS_ID_META_PROXIMITY },
{ SH2_TEMPERATURE, FRS_ID_META_TEMPERATURE },
{ SH2_TAP_DETECTOR, FRS_ID_META_TAP_DETECTOR },
{ SH2_STEP_DETECTOR, FRS_ID_META_STEP_DETECTOR },
{ SH2_STEP_COUNTER, FRS_ID_META_STEP_COUNTER },
{ SH2_SIGNIFICANT_MOTION, FRS_ID_META_SIGNIFICANT_MOTION },
{ SH2_STABILITY_CLASSIFIER, FRS_ID_META_STABILITY_CLASSIFIER },
{ SH2_SHAKE_DETECTOR, FRS_ID_META_SHAKE_DETECTOR },
{ SH2_FLIP_DETECTOR, FRS_ID_META_FLIP_DETECTOR },
{ SH2_PICKUP_DETECTOR, FRS_ID_META_PICKUP_DETECTOR },
{ SH2_STABILITY_DETECTOR, FRS_ID_META_STABILITY_DETECTOR },
{ SH2_PERSONAL_ACTIVITY_CLASSIFIER, FRS_ID_META_PERSONAL_ACTIVITY_CLASSIFIER },
{ SH2_SLEEP_DETECTOR, FRS_ID_META_SLEEP_DETECTOR },
{ SH2_TILT_DETECTOR, FRS_ID_META_TILT_DETECTOR },
{ SH2_POCKET_DETECTOR, FRS_ID_META_POCKET_DETECTOR },
{ SH2_CIRCLE_DETECTOR, FRS_ID_META_CIRCLE_DETECTOR },
};
int sh2_getMetadata(sh2_SensorId_t sensorId, sh2_SensorMetadata_t *pData)
{
// pData must be non-null
if (pData == 0) return SH2_ERR_BAD_PARAM;
// Convert sensorId to metadata recordId
int i;
for (i = 0; i < ARRAY_LEN(sensorToRecordMap); i++) {
if (sensorToRecordMap[i].sensorId == sensorId) {
break;
}
}
if (i >= ARRAY_LEN(sensorToRecordMap)) {
// no match was found
return SH2_ERR_BAD_PARAM;
}
uint16_t recordId = sensorToRecordMap[i].recordId;
// Set up an FRS read operation
sh2.opData.getFrs.frsType = recordId;
sh2.opData.getFrs.pData = sh2.frsData;
sh2.frsDataLen = ARRAY_LEN(sh2.frsData);
sh2.opData.getFrs.pWords = &sh2.frsDataLen;
sh2.opData.getFrs.nextOffset = 0;
sh2.opData.getFrs.pMetadata = pData;
return opStart(&getFrsOp);
}
int sh2_getFrs(uint16_t recordId, uint32_t *pData, uint16_t *words)
{
if ((pData == 0) || (words == 0)) {
return SH2_ERR_BAD_PARAM;
}
// Store params for this op
sh2.opData.getFrs.frsType = recordId;
sh2.opData.getFrs.pData = pData;
sh2.opData.getFrs.pWords = words;
sh2.opData.getFrs.nextOffset = 0;
sh2.opData.getFrs.pMetadata = 0;
return opStart(&getFrsOp);
}
int sh2_setFrs(uint16_t recordId, uint32_t *pData, uint16_t words)
{
if ((pData == 0) && (words != 0)) {
return SH2_ERR_BAD_PARAM;
}
sh2.opData.setFrs.frsType = recordId;
sh2.opData.setFrs.pData = pData;
sh2.opData.setFrs.words = words;
return opStart(&setFrsOp);
}
int sh2_getErrors(uint8_t severity, sh2_ErrorRecord_t *pErrors, uint16_t *numErrors)
{
sh2.opData.getErrors.severity = severity;
sh2.opData.getErrors.pErrors = pErrors;
sh2.opData.getErrors.pNumErrors = numErrors;
return opStart(&getErrorsOp);
}
int sh2_getCounts(sh2_SensorId_t sensorId, sh2_Counts_t *pCounts)
{
sh2.opData.getCounts.sensorId = sensorId;
sh2.opData.getCounts.pCounts = pCounts;
return opStart(&getCountsOp);
}
int sh2_clearCounts(sh2_SensorId_t sensorId)
{
uint8_t p[9];
memset(p, 0, sizeof(p));
p[0] = SH2_COUNTS_CLEAR_COUNTS;
p[1] = sensorId;
setupCmdParams(SH2_CMD_COUNTS, p);
return opStart(&sendCmdOp);
}
int sh2_setTareNow(uint8_t axes, // SH2_TARE_X | SH2_TARE_Y | SH2_TARE_Z
sh2_TareBasis_t basis)
{
uint8_t p[9];
memset(p, 0, sizeof(p));
p[0] = SH2_TARE_TARE_NOW;
p[1] = axes;
p[2] = basis;
setupCmdParams(SH2_CMD_TARE, p);
return opStart(&sendCmdOp);
}
int sh2_clearTare(void)
{
uint8_t p[9];
memset(p, 0, sizeof(p));
p[0] = SH2_TARE_SET_REORIENTATION;
setupCmdParams(SH2_CMD_TARE, p);
return opStart(&sendCmdOp);
}
int sh2_persistTare(void)
{
setupCmd1(SH2_CMD_TARE, SH2_TARE_PERSIST_TARE);
return opStart(&sendCmdOp);
}
int sh2_setReorientation(sh2_Quaternion_t *orientation)
{
uint8_t p[9];
p[0] = SH2_TARE_SET_REORIENTATION;
writeu16(&p[1], toQ14(orientation->x));
writeu16(&p[3], toQ14(orientation->y));
writeu16(&p[5], toQ14(orientation->z));
writeu16(&p[7], toQ14(orientation->w));
setupCmdParams(SH2_CMD_TARE, p);
return opStart(&sendCmdOp);
}
int sh2_reinitialize(void)
{
return opStart(&reinitOp);
}
int sh2_saveDcdNow(void)
{
return opStart(&saveDcdNowOp);
}
int sh2_getOscType(sh2_OscType_t *pOscType)
{
sh2.opData.getOscType.pOscType = pOscType;
return opStart(&getOscTypeOp);
}
int sh2_setCalConfig(uint8_t sensors)
{
sh2.opData.calConfig.sensors = sensors;
return opStart(&calConfigOp);
}
int sh2_getCalConfig(uint8_t *pSensors)
{
if (pSensors == 0) {
return SH2_ERR_BAD_PARAM;
}
sh2.opData.getCalConfig.pSensors = pSensors;
return opStart(&getCalConfigOp);
}
int sh2_setDcdAutoSave(bool enabled)
{
setupCmd1(SH2_CMD_DCD_SAVE, enabled ? 0 : 1);
return opStart(&sendCmdOp);
}
int sh2_flush(sh2_SensorId_t sensorId)
{
// Set up flush operation
sh2.opData.forceFlush.sensorId = sensorId;
return opStart(&forceFlushOp);
}
int sh2_clearDcdAndReset(void)
{
setupCmd0(SH2_CMD_CLEAR_DCD_AND_RESET);
return opStart(&sendCmdOp);
}
int sh2_startCal(uint32_t interval_us)
{
sh2.opData.startCal.interval_us = interval_us;
return opStart(&startCalOp);
}
int sh2_finishCal(sh2_CalStatus_t *status)
{
int retval = opStart(&finishCalOp);
*status = sh2.opData.finishCal.status;
return retval;
}
// --- Private utility functions --------------------------------------------------------------
static int16_t toQ14(double x)
{
int16_t retval = (int16_t)(x * (1<<14));
return retval;
}
// Send a command with parameters from p
static void setupCmdParams(uint8_t cmd, uint8_t p[9])
{
// Set up request
sh2.opData.sendCmd.req.reportId = SENSORHUB_COMMAND_REQ;
sh2.opData.sendCmd.req.seq = sh2.nextCmdSeq++;
sh2.opData.sendCmd.req.command = cmd;
memcpy(&sh2.opData.sendCmd.req.p, p,
sizeof(sh2.opData.sendCmd.req.p));
}
// Set up command with no params
static void setupCmd0(uint8_t cmd)
{
uint8_t p[9];
memset(p, 0, sizeof(p));
setupCmdParams(cmd, p);
}
// Set up command with 1 param
static void setupCmd1(uint8_t cmd, uint8_t p0)
{
uint8_t p[9];
memset(p, 0, sizeof(p));
p[0] = p0;
setupCmdParams(cmd, p);
}
static void sensorhubAdvertHdlr(void *cookie, uint8_t tag, uint8_t len, uint8_t *val)
{
switch (tag) {
case TAG_SH2_VERSION:
strcpy(sh2.version, (const char *)val);
break;
case TAG_SH2_REPORT_LENGTHS:
{
uint8_t reports = len/2;
if (reports > SH2_MAX_REPORT_IDS) {
// Hub gave us more report lengths than we can store!
reports = SH2_MAX_REPORT_IDS;
}
for (int n = 0; n < reports; n++) {
sh2.report[n].id = val[n*2];
sh2.report[n].len = val[n*2 + 1];
}
break;
}
case 0:
{
// 0 tag indicates end of advertisements for this app
// At this time, the SHTP layer can give us our channel number.
sh2.controlChan = shtp_chanNo("sensorhub", "control");
sh2.advertDone = true;
break;
}
default:
break;
}
}
static void sensorhubControlHdlr(void *cookie, uint8_t *payload, uint16_t len, uint32_t timestamp)
{
uint16_t cursor = 0;
uint32_t count = 0;
CommandResp_t * pResp = 0;
sh2_AsyncEvent_t event;
if (len == 0) {
sh2.emptyPayloads++;
return;
}
while (cursor < len) {
// Get next report id
count++;
uint8_t reportId = payload[cursor];
// Determine report length
uint8_t reportLen = 0;
for (int n = 0; n < SH2_MAX_REPORT_IDS; n++) {
if (sh2.report[n].id == reportId) {
reportLen = sh2.report[n].len;
break;
}
}
if (reportLen == 0) {
// An unrecognized report id
sh2.unknownReportIds++;
return;
}
else {
// Check for unsolicited initialize response or FRS change response
if (reportId == SENSORHUB_COMMAND_RESP) {
pResp = (CommandResp_t *)(payload+cursor);
if ((pResp->command == (SH2_CMD_INITIALIZE | SH2_INIT_UNSOLICITED)) &&
(pResp->r[1] == SH2_INIT_SYSTEM)) {
// This is an unsolicited INIT message.
// Is it time to call reset callback?
sh2.gotInitResp = true;
}
if (pResp->command == (SH2_CMD_FRS | SH2_INIT_UNSOLICITED))