-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathn_hooks.c
More file actions
1093 lines (1000 loc) · 32.8 KB
/
Copy pathn_hooks.c
File metadata and controls
1093 lines (1000 loc) · 32.8 KB
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 n_hooks.c
*
* Hooks allow libraries dependent on note-c to provide platform- or
* MCU-specific functions for common functions like I2C locking/unlocking,
* memory allocation and freeing, delays, and communicating with the Notecard
* over I2C and Serial. Using these hooks, note-c is able to manage Notecard
* transaction logic, and defer to platform functionality, when needed.
*
* Written by Ray Ozzie and Blues Inc. team.
*
* Copyright (c) 2019 Blues Inc. MIT License. Use of this source code is
* governed by licenses granted by the copyright holder including that found in
* the
* <a href="https://github.com/blues/note-c/blob/master/LICENSE">LICENSE</a>
* file.
*
*/
#include <assert.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include "n_lib.h"
//**************************************************************************/
/*!
@brief Show malloc operations for debugging in very low mem environments.
*/
/**************************************************************************/
#ifndef NOTE_C_SHOW_MALLOC
#define NOTE_C_SHOW_MALLOC false
#endif
#if NOTE_C_SHOW_MALLOC
#include <string.h>
#endif
// Externalized Hooks
//**************************************************************************/
/*!
@brief Hook for the calling platform's debug interface, if any.
*/
/**************************************************************************/
debugOutputFn hookDebugOutput = NULL;
//**************************************************************************/
/*!
@brief Hook for the calling platform's I2C lock function.
*/
/**************************************************************************/
mutexFn hookLockI2C = NULL;
//**************************************************************************/
/*!
@brief Hook for the calling platform's I2C unlock function.
*/
/**************************************************************************/
mutexFn hookUnlockI2C = NULL;
//**************************************************************************/
/*!
@brief Hook for the calling platform's Notecard lock function.
*/
/**************************************************************************/
mutexFn hookLockNote = NULL;
//**************************************************************************/
/*!
@brief Hook for the calling platform's Notecard lock function.
*/
/**************************************************************************/
mutexFn hookUnlockNote = NULL;
//**************************************************************************/
/*!
@brief Hook for the calling platform's transaction initiation function.
*/
/**************************************************************************/
NOTE_C_STATIC txnStartFn hookTransactionStart = NULL;
//**************************************************************************/
/*!
@brief Hook for the calling platform's transaction completion function.
*/
/**************************************************************************/
NOTE_C_STATIC txnStopFn hookTransactionStop = NULL;
//**************************************************************************/
/*!
@brief Hook for the calling platform's memory allocation function.
*/
/**************************************************************************/
NOTE_C_STATIC mallocFn hookMalloc = NULL;
//**************************************************************************/
/*!
@brief Hook for the calling platform's memory free function.
*/
/**************************************************************************/
NOTE_C_STATIC freeFn hookFree = NULL;
//**************************************************************************/
/*!
@brief Hook for the calling platform's delay function.
*/
/**************************************************************************/
NOTE_C_STATIC delayMsFn hookDelayMs = NULL;
//**************************************************************************/
/*!
@brief Hook for the calling platform's millis timing function.
*/
/**************************************************************************/
NOTE_C_STATIC getMsFn hookGetMs = NULL;
//**************************************************************************/
/*!
@brief Hook for the calling platform's current active interface. Value is
one of:
- NOTE_C_INTERFACE_NONE (default)
- NOTE_C_INTERFACE_SERIAL
- NOTE_C_INTERFACE_I2C
*/
/**************************************************************************/
NOTE_C_STATIC volatile int hookActiveInterface = NOTE_C_INTERFACE_NONE;
//**************************************************************************/
/*!
@brief Hook for the calling platform's Serial reset function.
*/
/**************************************************************************/
NOTE_C_STATIC serialResetFn hookSerialReset = NULL;
//**************************************************************************/
/*!
@brief Hook for the calling platform's Serial transmit function.
*/
/**************************************************************************/
NOTE_C_STATIC serialTransmitFn hookSerialTransmit = NULL;
//**************************************************************************/
/*!
@brief Hook for the calling platform's Serial data available function.
*/
/**************************************************************************/
NOTE_C_STATIC serialAvailableFn hookSerialAvailable = NULL;
//**************************************************************************/
/*!
@brief Hook for the calling platform's Serial receive function.
*/
/**************************************************************************/
NOTE_C_STATIC serialReceiveFn hookSerialReceive = NULL;
//**************************************************************************/
/*!
@brief Hook for the calling platform's I2C address.
*/
/**************************************************************************/
NOTE_C_STATIC uint32_t i2cAddress = 0;
//**************************************************************************/
/*!
@brief Hook for the calling platform's I2C maximum segment size, in bytes.
*/
/**************************************************************************/
NOTE_C_STATIC uint32_t i2cMax = 0;
//**************************************************************************/
/*!
@brief Hook for the calling platform's I2C reset function.
*/
/**************************************************************************/
NOTE_C_STATIC i2cResetFn hookI2CReset = NULL;
//**************************************************************************/
/*!
@brief Hook for the calling platform's transmit function.
*/
/**************************************************************************/
NOTE_C_STATIC i2cTransmitFn hookI2CTransmit = NULL;
//**************************************************************************/
/*!
@brief Hook for the calling platform's I2C receive function.
*/
/**************************************************************************/
NOTE_C_STATIC i2cReceiveFn hookI2CReceive = NULL;
#ifdef NOTE_C_HEARTBEAT_CALLBACK
//**************************************************************************/
/*!
@brief Hook for a heartbeat notification
*/
/**************************************************************************/
NOTE_C_STATIC heartbeatFn hookHeartbeat = NULL;
NOTE_C_STATIC void *hookHeartbeatContext = NULL;
#endif
#ifndef NOTE_NODEBUG
//**************************************************************************/
/*!
@brief Variable used to determine the runtime logging level
*/
/**************************************************************************/
NOTE_C_STATIC int noteLogLevel = NOTE_C_LOG_LEVEL;
#endif
// Internal hooks
typedef bool (*nNoteResetFn) (void);
typedef const char * (*nTransactionFn) (const char *, size_t, char **, uint32_t);
typedef const char * (*nReceiveFn) (uint8_t *, uint32_t *, bool, uint32_t, uint32_t *);
typedef const char * (*nTransmitFn) (const uint8_t *, uint32_t, bool);
NOTE_C_STATIC nNoteResetFn notecardReset = NULL;
NOTE_C_STATIC nTransactionFn notecardTransaction = NULL;
NOTE_C_STATIC nReceiveFn notecardChunkedReceive = NULL;
NOTE_C_STATIC nTransmitFn notecardChunkedTransmit = NULL;
//**************************************************************************/
/*!
@brief Get the active, platform-specific communications method
@returns The active interface. One of:
- NOTE_C_INTERFACE_NONE (default)
- NOTE_C_INTERFACE_SERIAL
- NOTE_C_INTERFACE_I2C
*/
/**************************************************************************/
int NoteGetActiveInterface(void)
{
return hookActiveInterface;
}
NOTE_C_STATIC void _noteSetActiveInterface(int interface)
{
hookActiveInterface = interface;
switch (interface) {
case NOTE_C_INTERFACE_SERIAL:
notecardReset = _serialNoteReset;
notecardTransaction = _serialNoteTransaction;
notecardChunkedReceive = _serialChunkedReceive;
notecardChunkedTransmit = _serialChunkedTransmit;
break;
case NOTE_C_INTERFACE_I2C:
notecardReset = _i2cNoteReset;
notecardTransaction = _i2cNoteTransaction;
notecardChunkedReceive = _i2cNoteChunkedReceive;
notecardChunkedTransmit = _i2cNoteChunkedTransmit;
break;
default:
hookActiveInterface = NOTE_C_INTERFACE_NONE; // unrecognized interfaces are disabled
notecardReset = NULL;
notecardTransaction = NULL;
notecardChunkedReceive = NULL;
notecardChunkedTransmit = NULL;
break;
}
}
void NoteSetActiveInterface(int interface)
{
_LockNote();
_noteSetActiveInterface(interface);
_UnlockNote();
}
void NoteSetFnDefault(mallocFn mallocfn, freeFn freefn, delayMsFn delayfn, getMsFn millisfn)
{
_LockNote();
if (hookMalloc == NULL) {
hookMalloc = mallocfn;
}
if (hookFree == NULL) {
hookFree = freefn;
}
if (hookDelayMs == NULL) {
hookDelayMs = delayfn;
}
if (hookGetMs == NULL) {
hookGetMs = millisfn;
}
_UnlockNote();
}
void NoteSetFn(mallocFn mallocHook, freeFn freeHook, delayMsFn delayMsHook,
getMsFn getMsHook)
{
_LockNote();
hookMalloc = mallocHook;
hookFree = freeHook;
hookDelayMs = delayMsHook;
hookGetMs = getMsHook;
_UnlockNote();
}
#ifdef NOTE_C_HEARTBEAT_CALLBACK
//**************************************************************************/
/*!
@brief Set the heartbeat function
@param fn A function pointer to call for heart beat notifications.
@param context User context to pass to the heartbeat function.
*/
/**************************************************************************/
void NoteSetFnHeartbeat(heartbeatFn fn, void *context)
{
_LockNote();
hookHeartbeat = fn;
hookHeartbeatContext = context;
_UnlockNote();
}
#endif
void NoteSetFnDebugOutput(debugOutputFn fn)
{
_LockNote();
hookDebugOutput = fn;
_UnlockNote();
}
//**************************************************************************/
/*!
@brief Determine if a debug output function has been set.
@returns A boolean indicating whether a debug ouput function was
provided.
*/
/**************************************************************************/
bool _noteIsDebugOutputActive(void)
{
return hookDebugOutput != NULL;
}
void NoteSetFnTransaction(txnStartFn startFn, txnStopFn stopFn)
{
_LockNote();
hookTransactionStart = startFn;
hookTransactionStop = stopFn;
_UnlockNote();
}
void NoteSetFnMutex(mutexFn lockI2Cfn, mutexFn unlockI2Cfn, mutexFn lockNotefn, mutexFn unlockNotefn)
{
hookLockI2C = lockI2Cfn;
hookUnlockI2C = unlockI2Cfn;
hookLockNote = lockNotefn;
hookUnlockNote = unlockNotefn;
}
void NoteSetFnI2CMutex(mutexFn lockI2Cfn, mutexFn unlockI2Cfn)
{
hookLockI2C = lockI2Cfn;
hookUnlockI2C = unlockI2Cfn;
}
void NoteSetFnNoteMutex(mutexFn lockFn, mutexFn unlockFn)
{
hookLockNote = lockFn;
hookUnlockNote = unlockFn;
}
void NoteSetFnSerial(serialResetFn resetFn, serialTransmitFn transmitFn,
serialAvailableFn availFn, serialReceiveFn receiveFn)
{
_LockNote();
hookSerialReset = resetFn;
hookSerialTransmit = transmitFn;
hookSerialAvailable = availFn;
hookSerialReceive = receiveFn;
_noteSetActiveInterface(NOTE_C_INTERFACE_SERIAL);
_UnlockNote();
}
void NoteSetFnSerialDefault(serialResetFn resetFn, serialTransmitFn transmitFn,
serialAvailableFn availFn, serialReceiveFn receiveFn)
{
_LockNote();
if (hookSerialReset == NULL) {
hookSerialReset = resetFn;
}
if (hookSerialTransmit == NULL) {
hookSerialTransmit = transmitFn;
}
if (hookSerialAvailable == NULL) {
hookSerialAvailable = availFn;
}
if (hookSerialReceive == NULL) {
hookSerialReceive = receiveFn;
}
if (hookActiveInterface == NOTE_C_INTERFACE_NONE) {
_noteSetActiveInterface(NOTE_C_INTERFACE_SERIAL);
}
_UnlockNote();
}
void NoteSetFnI2C(uint32_t notecardAddr, uint32_t maxTransmitSize,
i2cResetFn resetFn, i2cTransmitFn transmitFn,
i2cReceiveFn receiveFn)
{
_LockNote();
i2cAddress = notecardAddr;
i2cMax = maxTransmitSize;
hookI2CReset = resetFn;
hookI2CTransmit = transmitFn;
hookI2CReceive = receiveFn;
_noteSetActiveInterface(NOTE_C_INTERFACE_I2C);
_UnlockNote();
}
void NoteSetFnI2CDefault(uint32_t notecardAddr, uint32_t maxTransmitSize,
i2cResetFn resetFn, i2cTransmitFn transmitFn,
i2cReceiveFn receiveFn)
{
_LockNote();
if (i2cAddress == 0) {
i2cAddress = notecardAddr;
}
if (i2cMax == 0) {
i2cMax = maxTransmitSize;
}
if (hookI2CReset == NULL) {
hookI2CReset = resetFn;
}
if (hookI2CTransmit == NULL) {
hookI2CTransmit = transmitFn;
}
if (hookI2CReceive == NULL) {
hookI2CReceive = receiveFn;
}
if (hookActiveInterface == NOTE_C_INTERFACE_NONE) {
_noteSetActiveInterface(NOTE_C_INTERFACE_I2C);
}
_UnlockNote();
}
//**************************************************************************/
/*!
@brief Set the platform-specific communications method to be disabled
*/
/**************************************************************************/
void NoteSetFnDisabled(void)
{
_LockNote();
_noteSetActiveInterface(NOTE_C_INTERFACE_NONE);
_UnlockNote();
}
// Runtime hook wrappers
void NoteSetLogLevel(int level)
{
#ifndef NOTE_NODEBUG
noteLogLevel = level;
#else
(void)level;
#endif
}
void NoteDebugIntln(const char *msg, int n)
{
if (msg != NULL) {
_Debug(msg);
}
char str[16];
JItoA(n, str);
_Debug(str);
_Debug(c_newline);
}
void NoteDebugln(const char *msg)
{
#ifndef NOTE_NODEBUG
_Debug(msg);
_Debug(c_newline);
#else
(void)msg;
#endif // !NOTE_NODEBUG
}
void NoteDebug(const char *msg)
{
#ifndef NOTE_NODEBUG
if (_noteIsDebugOutputActive()) {
hookDebugOutput(msg);
}
#else
(void)msg;
#endif // !NOTE_NODEBUG
}
void NoteDebugWithLevel(uint8_t level, const char *msg)
{
#ifndef NOTE_NODEBUG
if (level > noteLogLevel) {
return;
}
_Debug(msg);
#else
(void)level;
(void)msg;
#endif // !NOTE_NODEBUG
}
void NoteDebugWithLevelLn(uint8_t level, const char *msg)
{
#ifndef NOTE_NODEBUG
if (level > noteLogLevel) {
return;
}
_DebugWithLevel(level, msg);
_DebugWithLevel(level, c_newline);
#else
(void)level;
(void)msg;
#endif // !NOTE_NODEBUG
}
//**************************************************************************/
/*!
@brief Get the current milliseconds value from the platform-specific
hook.
@returns The current milliseconds value.
*/
/**************************************************************************/
uint32_t NoteGetMs(void)
{
if (hookGetMs == NULL) {
return 0;
}
return hookGetMs();
}
void NoteDelayMs(uint32_t ms)
{
if (hookDelayMs != NULL) {
hookDelayMs(ms);
}
}
#ifndef NOTE_C_LOW_MEM
//**************************************************************************/
/*!
@brief Convert number to a hex string
@param n the number
@param p the buffer to return it into
*/
/**************************************************************************/
void _n_htoa32(uint32_t n, char *p)
{
for (int i=0; i<8; i++) {
uint32_t nibble = (n >> 28) & 0xff;
n = n << 4;
if (nibble >= 10) {
*p++ = 'A' + (nibble-10);
} else {
*p++ = '0' + nibble;
}
}
*p = '\0';
}
#if NOTE_C_SHOW_MALLOC
static_assert(sizeof(void *) == sizeof(uint32_t), "Pointer size mismatch");
NOTE_C_STATIC void _n_ptoa32(void *ptr, char *str)
{
_n_htoa32((uint32_t)ptr, str);
}
#endif // NOTE_C_SHOW_MALLOC
#endif // !NOTE_C_LOW_MEM
void *NoteMalloc(size_t size)
{
if (hookMalloc == NULL) {
return NULL;
}
void *p = hookMalloc(size);
#if NOTE_C_SHOW_MALLOC && !defined(NOTE_C_LOW_MEM)
if (_noteIsDebugOutputActive()) {
hookDebugOutput("malloc ");
// Convert the size to a string and print
char str[16];
JItoA(size, str);
hookDebugOutput(str);
if (p == NULL) {
hookDebugOutput(" FAIL");
} else {
hookDebugOutput(" ");
// Convert the pointer to a string and print
_n_ptoa32(p, str);
hookDebugOutput(str);
}
}
#endif // NOTE_C_SHOW_MALLOC && !defined(NOTE_C_LOW_MEM)
return p;
}
void NoteFree(void *ptr)
{
if (hookFree != NULL) {
#if NOTE_C_SHOW_MALLOC && !defined(NOTE_C_LOW_MEM)
if (_noteIsDebugOutputActive()) {
hookDebugOutput("free ");
// Convert the pointer to a string and print
char str[16];
_n_ptoa32(ptr, str);
hookDebugOutput(str);
}
#endif // NOTE_C_SHOW_MALLOC && !defined(NOTE_C_LOW_MEM)
hookFree(ptr);
}
}
//**************************************************************************/
/*!
@brief Lock the I2C bus using the platform-specific hook.
*/
/**************************************************************************/
void NoteLockI2C(void)
{
if (hookLockI2C != NULL) {
hookLockI2C();
}
}
//**************************************************************************/
/*!
@brief Unlock the I2C bus using the platform-specific hook.
*/
/**************************************************************************/
void NoteUnlockI2C(void)
{
if (hookUnlockI2C != NULL) {
hookUnlockI2C();
}
}
#ifdef NOTE_C_HEARTBEAT_CALLBACK
//**************************************************************************/
/*!
@brief Call a heartbeat function if registered
@param heartbeatJson Pointer to null-terminated heartbeat JSON string.
@returns `true` if the heartbeat callback wishes to abandon the transaction.
*/
/**************************************************************************/
bool _noteHeartbeat(const char *heartbeatJson)
{
if (hookHeartbeat != NULL) {
return hookHeartbeat(heartbeatJson, hookHeartbeatContext);
}
return false;
}
#endif
//**************************************************************************/
/*!
@brief Lock the Notecard using the platform-specific hook.
*/
/**************************************************************************/
void _noteLockNote(void)
{
if (hookLockNote != NULL) {
hookLockNote();
}
}
//**************************************************************************/
/*!
@brief Unlock the Notecard using the platform-specific hook.
*/
/**************************************************************************/
void _noteUnlockNote(void)
{
if (hookUnlockNote != NULL) {
hookUnlockNote();
}
}
//**************************************************************************/
/*!
@brief Indicate that we're initiating a transaction using the platform-specific hook.
*/
/**************************************************************************/
bool _noteTransactionStart(uint32_t timeoutMs)
{
if (hookTransactionStart != NULL) {
return hookTransactionStart(timeoutMs);
}
return true;
}
//**************************************************************************/
/*!
@brief Indicate that we've completed a transaction using the platform-specific hook.
*/
/**************************************************************************/
void _noteTransactionStop(void)
{
if (hookTransactionStop != NULL) {
hookTransactionStop();
}
}
void NoteGetFnDebugOutput(debugOutputFn *fn)
{
if (fn != NULL) {
*fn = hookDebugOutput;
}
}
#ifdef NOTE_C_HEARTBEAT_CALLBACK
/*!
@brief Get the user-defined heartbeat function.
@param fn Pointer to store the heartbeat function pointer.
@param context Pointer to store the heartbeat function context.
*/
void NoteGetFnHeartbeat(heartbeatFn *fn, void **context)
{
if (fn != NULL) {
*fn = hookHeartbeat;
}
if (context != NULL) {
*context = hookHeartbeatContext;
}
}
#endif
void NoteGetFnTransaction(txnStartFn *startFn, txnStopFn *stopFn)
{
_LockNote();
if (startFn != NULL) {
*startFn = hookTransactionStart;
}
if (stopFn != NULL) {
*stopFn = hookTransactionStop;
}
_UnlockNote();
}
void NoteGetFnMutex(mutexFn *lockI2Cfn, mutexFn *unlockI2Cfn, mutexFn *lockNotefn,
mutexFn *unlockNotefn)
{
if (lockI2Cfn != NULL) {
*lockI2Cfn = hookLockI2C;
}
if (unlockI2Cfn != NULL) {
*unlockI2Cfn = hookUnlockI2C;
}
if (lockNotefn != NULL) {
*lockNotefn = hookLockNote;
}
if (unlockNotefn != NULL) {
*unlockNotefn = hookUnlockNote;
}
}
void NoteGetFnI2CMutex(mutexFn *lockI2Cfn, mutexFn *unlockI2Cfn)
{
if (lockI2Cfn != NULL) {
*lockI2Cfn = hookLockI2C;
}
if (unlockI2Cfn != NULL) {
*unlockI2Cfn = hookUnlockI2C;
}
}
void NoteGetFnNoteMutex(mutexFn *lockFn, mutexFn *unlockFn)
{
if (lockFn != NULL) {
*lockFn = hookLockNote;
}
if (unlockFn != NULL) {
*unlockFn = hookUnlockNote;
}
}
void NoteGetFn(mallocFn *mallocHook, freeFn *freeHook, delayMsFn *delayMsHook,
getMsFn *getMsHook)
{
_LockNote();
if (mallocHook != NULL) {
*mallocHook = hookMalloc;
}
if (freeHook != NULL) {
*freeHook = hookFree;
}
if (delayMsHook != NULL) {
*delayMsHook = hookDelayMs;
}
if (getMsHook != NULL) {
*getMsHook = hookGetMs;
}
_UnlockNote();
}
void NoteGetFnSerial(serialResetFn *resetFn, serialTransmitFn *transmitFn,
serialAvailableFn *availFn, serialReceiveFn *receiveFn)
{
_LockNote();
if (resetFn != NULL) {
*resetFn = hookSerialReset;
}
if (transmitFn != NULL) {
*transmitFn = hookSerialTransmit;
}
if (availFn != NULL) {
*availFn = hookSerialAvailable;
}
if (receiveFn != NULL) {
*receiveFn = hookSerialReceive;
}
_UnlockNote();
}
void NoteGetFnI2C(uint32_t *notecardAddr, uint32_t *maxTransmitSize,
i2cResetFn *resetFn, i2cTransmitFn *transmitFn,
i2cReceiveFn *receiveFn)
{
_LockNote();
if (notecardAddr != NULL) {
*notecardAddr = i2cAddress;
}
if (maxTransmitSize != NULL) {
*maxTransmitSize = i2cMax;
}
if (resetFn != NULL) {
*resetFn = hookI2CReset;
}
if (transmitFn != NULL) {
*transmitFn = hookI2CTransmit;
}
if (receiveFn != NULL) {
*receiveFn = hookI2CReceive;
}
_UnlockNote();
}
void NoteGetI2CAddress(uint32_t *i2cAddr)
{
if (i2cAddr != NULL) {
*i2cAddr = i2cAddress;
}
}
void NoteGetI2CMtu(uint32_t *i2cMtu)
{
if (i2cMtu != NULL) {
*i2cMtu = i2cMax;
}
}
//**************************************************************************/
/*!
@brief Reset the Serial bus using the platform-specific hook.
@returns A boolean indicating whether the Serial bus was reset successfully.
*/
/**************************************************************************/
bool _noteSerialReset(void)
{
if (hookActiveInterface == NOTE_C_INTERFACE_SERIAL && hookSerialReset != NULL) {
return hookSerialReset();
}
return true;
}
//**************************************************************************/
/*!
@brief Transmit bytes over Serial using the platform-specific hook.
@param text The bytes to transmit.
@param len The length of bytes.
@param flush `true` to flush the bytes upon transmit.
*/
/**************************************************************************/
void _noteSerialTransmit(const uint8_t *text, size_t len, bool flush)
{
if (hookActiveInterface == NOTE_C_INTERFACE_SERIAL && hookSerialTransmit != NULL) {
// Cast is intentional: serialTransmitFn is non-const for API compatibility.
// TODO: Remove this cast when serialTransmitFn is updated to const uint8_t *.
hookSerialTransmit((uint8_t *)text, len, flush);
}
}
//**************************************************************************/
/*!
@brief Determine if Serial bus is available using the platform-specific
hook.
@returns A boolean indicating whether the Serial bus is available to read.
*/
/**************************************************************************/
bool _noteSerialAvailable(void)
{
if (hookActiveInterface == NOTE_C_INTERFACE_SERIAL && hookSerialAvailable != NULL) {
return hookSerialAvailable();
}
return false;
}
//**************************************************************************/
/*!
@brief Obtain a character from the Serial bus using the platform-specific
hook.
@returns A character from the Serial bus.
*/
/**************************************************************************/
char _noteSerialReceive(void)
{
if (hookActiveInterface == NOTE_C_INTERFACE_SERIAL && hookSerialReceive != NULL) {
return hookSerialReceive();
}
return '\0';
}
//**************************************************************************/
/*!
@brief Reset the I2C bus using the platform-specific hook.
@returns A boolean indicating whether the I2C bus was reset successfully.
*/
/**************************************************************************/
bool _noteI2CReset(uint16_t DevAddress)
{
if (hookActiveInterface == NOTE_C_INTERFACE_I2C && hookI2CReset != NULL) {
return hookI2CReset(DevAddress);
}
return true;
}
//**************************************************************************/
/*!
@brief Transmit bytes over I2C using the platform-specific hook.
@param DevAddress the I2C address for transmission.
@param pBuffer The bytes to transmit.
@param Size The length of bytes.
@returns A c-string from the platform-specific hook, or an error string
if the bus is not active.
*/
/**************************************************************************/
const char *_noteI2CTransmit(uint16_t DevAddress, const uint8_t* pBuffer, uint16_t Size)
{
if (hookActiveInterface == NOTE_C_INTERFACE_I2C && hookI2CTransmit != NULL) {
// Cast is intentional: i2cTransmitFn is non-const for API compatibility.
// TODO: Remove this cast when i2cTransmitFn is updated to const uint8_t *.
return hookI2CTransmit(DevAddress, (uint8_t *)pBuffer, Size);
}
return "i2c not active";
}
//**************************************************************************/
/*!
@brief Receive bytes from I2C using the platform-specific hook.
@param DevAddress the I2C address for transmission.
@param pBuffer (out) A buffer in which to place received bytes.
@param Size The length of bytes.
@param available (out) The number of bytes left to read.
@returns A c-string from the platform-specific hook, or an error string
if the bus is not active.
*/
/**************************************************************************/
const char *_noteI2CReceive(uint16_t DevAddress, uint8_t* pBuffer, uint16_t Size, uint32_t *available)
{
if (hookActiveInterface == NOTE_C_INTERFACE_I2C && hookI2CReceive != NULL) {
return hookI2CReceive(DevAddress, pBuffer, Size, available);
}
return "i2c not active";
}
//**************************************************************************/
/*!
@brief Get the I2C address of the Notecard.
@returns The current I2C address.
*/
/**************************************************************************/
uint32_t NoteI2CAddress(void)
{
if (i2cAddress == 0) {
return NOTE_I2C_ADDR_DEFAULT;
}
return i2cAddress;
}
void NoteSetI2CAddress(uint32_t i2cAddr)
{
i2cAddress = i2cAddr;
}
void NoteSetI2CMtu(uint32_t i2cMtu)
{
i2cMax = i2cMtu;
}
//**************************************************************************/
/*!
@brief Determine the maximum number of bytes for each segment of
data sent to the Notecard over I2C.
@returns A 32-bit integer of the maximum number of bytes per I2C segment.
*/
/**************************************************************************/
uint32_t NoteI2CMax(void)
{
// Many Arduino libraries (such as ESP32) have a limit less than 32, so if the max isn't specified
// we must assume the worst and segment the I2C messages into very tiny chunks.
if (i2cMax == 0) {
return NOTE_I2C_MTU_DEFAULT;
}