-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCLI_ARM
1248 lines (1132 loc) · 52.6 KB
/
CLI_ARM
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
#include "FrontEnd_Defines.h"
#include "AlarmMngr.h"
#include "CtrlThr.h"
#include "string.h"
#include "stdio.h"
#include "stdlib.h"
//#include "UsAlgo.h"
#include "UsFhr.h"
#include "AudioMngr.h"
#include "Cli_Interpreter.h"
#include "EventHandler.h"
extern AudioMngr* g_pAudioMngrInstance;
extern AlarmMngr* g_pAlarmMngrInstance;
extern EventHandler* g_pEventHandlerInstance;
extern UsFhr* g_pUs1FhrInstance;
CliInterpreter::CliInterpreter(void):CtrlThr(osPriorityLow) {
}
CliInterpreter::~CliInterpreter() {
}
void CliInterpreter::CtrlThread(void) {
char input[20];
char Command[2], Action[10] = {0xFF,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
int Channel = 0xFF, Code = 0xFF;
unsigned int NumInputs = 0;
while(1) {
if(fgets(&input[0],20,stdin)) {
NumInputs = sscanf(&input[0],"%s %d %d %s",&Command[0],&Channel, &Code ,&Action[0]);
}
if(4 == NumInputs) {
//Parse the input
if(!strncmp(&Command[0],"US",2)) {
unsigned int Len = strlen(Action);
switch(Code) {
case 1: //Connect disconnect
if(Len == 1) {
UsStatusType UsTdrStatus = WITHOUT_LENS_CONNECTED;
if(Channel == 1) {
if(Action[0] == '0') { //disconnect
UsTdrStatus = DISCONNECTED;
g_pUs1FhrInstance->SetTransducerInfo(UsTdrStatus);
g_pEventHandlerInstance->LogEvent(US1_DISCONNECTED);
} else if(Action[0] == '1') { //Connect
g_pUs1FhrInstance->SetTransducerInfo(UsTdrStatus);
g_pEventHandlerInstance->LogEvent(US1_CONNECTED);
} else if(Action[0] == '?') {
printf("%d\n",g_pUs1FhrInstance->GetTransducerInfo());
}
}
}
break;
case 2 : //Set / Get FHR
if(Channel == 1) {
UsDataType FhrValue;
if((Len == 1) && (Action[0] == '?')) {
g_pUs1FhrInstance->GetFhr(FhrValue);
printf("%d\n",FhrValue.u16Fhr);
} else if(Len <= 3) {
char* endptr = 0;
int Fhr = strtol(&Action[0],&endptr,10);
if(Fhr) {
if(Fhr > 300) Fhr = 300;
g_pUs1FhrInstance->SetFhr(Fhr);
}
}
}
break;
case 3 : //parameter volume control
if(Len == 1) {
if (Action[0] == '?') {
u8 Us1Vol = 0;
g_pAudioMngrInstance->GetVolume(AudioMngr::AD_US1_AUDIO,Us1Vol);
printf("%d\n",Us1Vol);
} else {
if(Action[0] >= '0' && Action[0] <= '9') {
g_pAudioMngrInstance->SetVolume(AudioMngr::AD_US1_AUDIO,Action[0] - '0');
}
}
}
break;
case 4: //Connect disconnect
if(Len == 1) {
bool Us1EnableStatus = false;
if(Channel == 1) {
if(Action[0] == '0') { //disconnect
g_pUs1FhrInstance->EnableAcq(Us1EnableStatus);
} else if(Action[0] == '1') { //Connect
Us1EnableStatus = true;
g_pUs1FhrInstance->EnableAcq(Us1EnableStatus);
} else if(Action[0] == '?') {
printf("%d\n",g_pUs1FhrInstance->IsEnabled());
}
}
}
break;
case 5: //Connect disconnect
if(Len == 1) {
UsFhr::SignalQuality SigQual = UsFhr::GREEN;
if(Channel == 1) {
if(Action[0] == '0') { //disconnect
g_pUs1FhrInstance->SetSignalQuality(SigQual);
} else if(Action[0] == '1') { //Connect
SigQual = UsFhr::YELLOW;
g_pUs1FhrInstance->SetSignalQuality(SigQual);
} else if(Action[0] == '2') { //Connect
SigQual = UsFhr::RED;
g_pUs1FhrInstance->SetSignalQuality(SigQual);
} else if(Action[0] == '?') {
printf("%d\n",g_pUs1FhrInstance->GetSignalQuality());
}
}
}
break;
case 40:
break;
case 41: //Alarm start / stop
case 42:
case 43:
if(Len == 1) {
AlarmMngr::AlarmPriority Priority = AlarmMngr::PARAM_HP_ALARM;
if(42 == Code) {
Priority = AlarmMngr::PARAM_MP_ALARM;
}
else if(41 == Code) {
Priority = AlarmMngr::PARAM_LP_ALARM;
}
if (Action[0] == '?') {
printf("%d\n",g_pAlarmMngrInstance->GetAlarmStatus(Priority));
} else if((Action[0] >= '0') && (Action[0] <= '2')) {
g_pAlarmMngrInstance->StartAlarm(Priority,(AlarmMngr::AlarmType)(Action[0]-'0'));
}
}
break;
case 45: //Alarm start / stop
case 46:
case 47:
if(Len == 1) {
AlarmMngr::AlarmPriority Priority = AlarmMngr::TECH_LP_ALARM;
if(46 == Code) {
Priority = AlarmMngr::TECH_MP_ALARM;
}
else if(47 == Code) {
Priority = AlarmMngr::TECH_HP_ALARM;
}
if (Action[0] == '?') {
printf("%d\n",g_pAlarmMngrInstance->GetAlarmStatus(Priority));
} else if((Action[0] >= '0') && (Action[0] <= '2')) {
g_pAlarmMngrInstance->StartAlarm(Priority,(AlarmMngr::AlarmType)(Action[0]-'0'));
}
}
break;
case 51 : //alarm volume control
case 52 :
case 53 :
case 54 :
if(Len == 1) {
u8 AlarmVol = 0;
AudioMngr::AudioDevice AudioSource = AudioMngr::AD_PHY_HP_ALARM;
if(52 == Code) {
AudioSource = AudioMngr::AD_PHY_MP_ALARM;
}
else if(51 == Code) {
AudioSource = AudioMngr::AD_PHY_LP_ALARM;
}
if ((Action[0] == '?') && (54 != Code) ) {
g_pAudioMngrInstance->GetVolume(AudioSource,AlarmVol);
printf("%d\n",AlarmVol);
} else {
if(Action[0] >= '1' && Action[0] <= '9') {
AlarmVol = Action[0] - '0';
if(54 == Code) {
g_pAudioMngrInstance->SetVolume(AudioMngr::AD_PHY_HP_ALARM,AlarmVol);
g_pAudioMngrInstance->SetVolume(AudioMngr::AD_PHY_MP_ALARM,AlarmVol);
g_pAudioMngrInstance->SetVolume(AudioMngr::AD_PHY_LP_ALARM,AlarmVol);
} else {
g_pAudioMngrInstance->SetVolume(AudioSource,AlarmVol);
}
}
}
}
break;
case 55 : //alarm volume control
case 56 :
case 57 :
case 58 :
if(Len == 1) {
u8 AlarmVol = 0;
AudioMngr::AudioDevice AudioSource = AudioMngr::AD_TECH_LP_ALARM;
if(56 == Code) {
AudioSource = AudioMngr::AD_TECH_MP_ALARM;
}
else if(57 == Code) {
AudioSource = AudioMngr::AD_TECH_HP_ALARM;
}
if (Action[0] == '?' && (58 != Code)) {
g_pAudioMngrInstance->GetVolume(AudioSource,AlarmVol);
printf("%d\n",AlarmVol);
} else {
if(Action[0] >= '1' && Action[0] <= '9') {
AlarmVol = Action[0] - '0';
if(58 == Code) {
g_pAudioMngrInstance->SetVolume(AudioMngr::AD_TECH_HP_ALARM,AlarmVol);
g_pAudioMngrInstance->SetVolume(AudioMngr::AD_TECH_MP_ALARM,AlarmVol);
g_pAudioMngrInstance->SetVolume(AudioMngr::AD_TECH_LP_ALARM,AlarmVol);
} else {
g_pAudioMngrInstance->SetVolume(AudioSource,AlarmVol);
}
}
}
}
break;
default:
break;
}
}
}
//cleanup to get ready for next input.
memset((void*)&input[0],0,20);
Command[0] = Command[1] = 0x00;
Channel = Code = 0xFF;
memset((void*)&Action[0],0xff,10);
}
}
#include "UsSetTransducerStatusCliCommand.h"
#include "UsFhr.h"
//#include "EventHandler.h"
#include "AcquisitionInterface.h"
#include "ISqdAlgo.h"
extern UsFhr* g_pUs1FhrInstance;
//extern EventHandler* g_pEventHandlerInstance;
void UsSetTransducerStatusCliCommand::executeCommand() {
if( false == isValidValue() )
return;
UsStatusType UsTdrStatus = (( m_u32Value == 0 )? DISCONNECTED : WITHOUT_LENS_CONNECTED );
if( 1 == m_u32Channel ) {
AcquisitionInterface *pAcquisitionInterface = AcquisitionInterface::GetAcqInterfaceInstance();
UsDataType UsDataTypeFhr;
g_pUs1FhrInstance->SetTransducerInfo(UsTdrStatus);
g_pUs1FhrInstance->GetFhr(UsDataTypeFhr);
//g_pEventHandlerInstance->LogEvent(( m_u32Value == 0 )?US1_DISCONNECTED:US1_CONNECTED);
pAcquisitionInterface->OnFhrValueCalculated(IFhrValueObserver::FHR_US1 , UsDataTypeFhr.u16Fhr, 0x0002, UsTdrStatus, ISqdAlgo::GREEN_SQ);
} else if ( 2 == m_u32Channel ) {
}
}
inline bool UsSetTransducerStatusCliCommand::isValidValue() {
return (( m_u32Value == 0 ) || ( 1 == m_u32Value ) );
}
Event.h
#ifndef EVENT_HANDLER_HPP
#define EVENT_HANDLER_HPP
#include "Attendant_Datatypes.h"
#include "CtrlThr.h"
#include "HandleMngr.h"
#include "Common.h"
class EventHandler : public CtrlThr {
private :
static EventHandler *ms_pEventHandler;
static osMutexId ms_EventHandlerMutex; ///< Mutex for the Eventhandler Static functions
/** Since no Api is provided in CMSIS in order to delete the mail Queue. So we cannot create the same Mail Queue
again.So this is made static and created in advance for the module.*/
static osMailQId ms_EventMailBox; ///< MsgBox to store the various events logged by various component.
public:
enum {
LOG_EVENT_FAILED = CTRL_THR_MAX,
MEMORY_ALLOC_FAIL_FOR_LOG_EVENT,
LOG_EVENT_UNSUPPORTED
};
/**
* \Structure :- Structure to hold Event Code i.e source of event and Data associated with that event.
**/
typedef struct {
EventSourceList EventCode;
EventData Data;
} EventLog;
private:
HandleMngr* m_pHandleMngr; // A handle mngr to manage the multiple readers of the different channels
EventCbInfo* m_ArpSubscriberList[MAX_SUPPORTED_EVENTS][MAX_SUBSCRIBERS]; ///< maintains the list of subscribers for every event.
osMutexId m_EventMutex; //Mutex to ensure only one is doing registration for an event at a given time.
public:
static EventHandler* GetInstance(osPriority Priority = osPriorityHigh,u16 u16StackSize = 0);
static bool DeleteInstance(void);
HANDLE Register(const EventSourceList Event,EventCbInfo& rEventCbInfo);
EventHndlrStatus Unregister(HANDLE Handle);
EventHndlrStatus LogEvent(EventSourceList Source,EventData pData = NULL);
i32 Activate(void); //Activate the thread which is reading the event logged in mail queue.
i32 Deactivate(void); //Deactivate the thread i.e disable reading of event from message queue.
private:
EventHandler(osPriority Priority ,u16 u16StackSize); ///< Constructor
~EventHandler(void); ///< destructor.
void CtrlThread(void); //This thread will run whenever an event is logged
};
#endif
#include "EventHandler.h"
#include "cmsis_os.h"
#include "string.h"
#define MAX_QUEUE_SIZE 256
/* Mutex definitions */
osMutexDef(EVENT_HANDLER_CLASS); ///< Mutex Control definition for control on static functions
osMutexDef(EVENT_REGISTER_UNREGISTER); ///< Mutex Control Register and Unregister functions.
osMailQDef(EVENTHANDLER_MBOX, MAX_QUEUE_SIZE, EventHandler::EventLog);
/* Initialize EventHandler class static variables */
EventHandler* EventHandler::ms_pEventHandler = NULL;
osMutexId EventHandler::ms_EventHandlerMutex = osMutexCreate(osMutex(EVENT_HANDLER_CLASS)); /* Create Mutex control */
osMailQId EventHandler:: ms_EventMailBox = osMailCreate(osMailQ(EVENTHANDLER_MBOX),NULL); /*mail queue to log event data*/
/**
* \fn EventHandler::EventHandler(osPriority Priority,u16 u16StackSize)
* \brief Constructor of EventHandler Class.
* \param[in] priority Priority of the thread used for servicing the periodic module. This is optional
parameter
* \param[in] u16StackSize Size of the stack of the thread.
* \return void
* \details This function shall be called on creation of EventHandler class object. On execution of this function
base constructor of CtrlThr is called.Create Mailbox to log the events, initialize subscriber list to NULL.
* Dependencies: NA
* Unusual Features of Implementation: NA
* Reasons why other choices have been avoided: NA
* Step-wise Breakdown of an Algorithm:
- Pass parameters value to base class constructor.
- Create mutex control for Event Registration and unregistration.
- Create Mailbox of "MAX_QUEUE_SIZE" size to log the events.
- Create handler Manger Class object which shall be used to register and Unregister the subscribers.
- Initialize subscriber array list to NULL.
*/
EventHandler :: EventHandler(osPriority Priority,u16 u16StackSize) : CtrlThr(Priority,u16StackSize) {
m_EventMutex = osMutexCreate(osMutex(EVENT_REGISTER_UNREGISTER)); /* Create Mutex control */
m_pHandleMngr = new HandleMngr(MAX_SUPPORTED_EVENTS*MAX_SUBSCRIBERS);
memset( m_ArpSubscriberList,NULL,sizeof(m_ArpSubscriberList));
}
/**
* \fn EventHandler::~EventHandler(void)
* \brief Destructor of EventHandler Class.
* \param - NA
* \return void
* \details This function shall be called on deletion of EventHandler class object. On execution of this function
base destructor of CtrlThr is called which deactivate the thread.Delete the memory created dynamically.
* Dependencies: NA
* Unusual Features of Implementation: NA
* Reasons why other choices have been avoided: NA
* Step-wise Breakdown of an Algorithm:
- Deactivate CtrlThr.
- Delete Handle Manger object.
- Delete Mutex created to control Registration and unregistration.
*/
EventHandler :: ~EventHandler(void) {
/*Deactivate the thread before deleting the class instance*/
Deactivate();
/*Delete Handler manager instance*/
if(NULL != m_pHandleMngr) {
delete m_pHandleMngr;
m_pHandleMngr = NULL;
}
/*Delete Mutex*/
if(NULL != m_EventMutex) {
osMutexDelete(m_EventMutex);
m_EventMutex = NULL;
}
}
/**
* \fn EventHandler::GetInstance(osPriority priority, u16 u16StackSize)
* \brief This function creates the Object of EventHandler Class and returns the pointer
* \param[in] priority Priority of the thread used for servicing the periodic module. This is optional
parameter, and the default value is "osPriorityHigh".
* \param[in] u16StackSize Size of the stack of the thread. This is optional parameter, and default value is 0
meaning default stack size selected in the project settings will be taken.
* \return EventHandler* pointer to the object
* \details This function creates the Object of EventHandler Class and returns the pointer. If the Object is already
created then returns the pointer to the previously created object.
* Dependencies: NA
* Unusual Features of Implementation: NA
* Reasons why other choices have been avoided: NA
* Step-wise Breakdown of an Algorithm:
- Wait for control on static EventHandler Class Mutex
- Check if the EventHandler object e is already created or not
- If it is not created, create object
- if object is created successfully then call "Activate" function to activate the thread
reading events logged in message queue.
- Check if thread is activated successfully or not. Validate all the member variables in
order to ensure that object is created successfully.If invalid then delete the object.
- Update the reference.
- Release control on static EventHandler Class Mutex
- Pass the reference of EventHandler object to the user
*/
EventHandler* EventHandler :: GetInstance(osPriority Priority ,u16 u16StackSize) {
/* Check if ms_EventHandlerMutex is created */
if (NULL != ms_EventHandlerMutex) {
/* Wait for control on static EventHandler Class Mutex */
if (osOK == osMutexWait(ms_EventHandlerMutex,osWaitForever)){
/* Check if the EventHandler object is already created or not */
if (NULL == ms_pEventHandler) {
/* Create EventHandler object and store the reference */
ms_pEventHandler = new EventHandler(Priority, u16StackSize);
/* Check if the object is created with valid values */
if ( NULL != ms_pEventHandler) {
/*Check if Thread which need to read the data logged in mail queue
is activated successfully or not and validate other parameters of class*/
if (((NULL == ms_EventMailBox ) ||( NULL == ms_pEventHandler->m_pHandleMngr)
|| (NULL == ms_pEventHandler->m_EventMutex)) ||
(CTRL_THR_NO_THREAD == ms_pEventHandler->Activate()) ) {
/* Delete the created object */
delete ms_pEventHandler;
/* Remove the reference of the object */
ms_pEventHandler = NULL;
}
}
}
/* Release control on static EventHandler Class Mutex */
osMutexRelease(ms_EventHandlerMutex);
}
}
return ms_pEventHandler;
}
/**
* \fn EventHandler::DeleteInstance(void)
* \brief This function deletes the Object of EventHandler Class
* \param[in] NA
* \return bool returns true if successfully deleted else false
* \details This function deletes the Object of EventHandler Class.
* Dependencies: NA
* Unusual Features of Implementation: NA
* Reasons why other choices have been avoided: NA
* Step-wise Breakdown of an Algorithm:
- Wait for control on static EventHandler Class Mutex
- Check if ms_pEventHandler is not NULL
- Delete the EventHandler Object
- Release control on static EventHandler Class Mutex
- Return the result
*/
bool EventHandler::DeleteInstance(void) {
bool Status = false;
/* Wait for control on static EventHandler Class Mutex */
if (osOK == osMutexWait(ms_EventHandlerMutex,osWaitForever)) {
/* Check if the EventHandler object is created or not */
if (NULL != ms_pEventHandler) {
delete ms_pEventHandler;
ms_pEventHandler = NULL;
Status = true;
}
/* Release control on static EventHandler Class Mutex */
osMutexRelease(ms_EventHandlerMutex);
}
return Status;
}
/**
* \fn EventHandler::Register(const EventSourceList Event,EventCbInfo& rEventCbInfo)
* \brief This function is used by other components to register for particular event.
If the registration is successful then only it will be get the intimation on occurrence of the event.
* \param[in] Event :- Event to be intimated.
* \param[in] rEventCbInfo :- reference to the "EventCbInfo" structure which contain pointer to callback function
and pointer to get the data logged on occurrence of the event and pointer to callback Data. Logged Data and
CbData pointers may be NULL it is application responsibility to do null check before using the data.
* \return HANDLE :- handle to be used for unregistration.
* \details This function is used by other components to register for a particular set of events.
If the registration is successful,then the EventHandler will intimate the subscriber whenever
any registered event occurs.Any events which occurred before registration of the event may be
given to the subscriber only if the read of the events after the registration is done.If the
event handler reads the events before registration,then the event is removed from the event queue.
* Dependencies: NA
* Unusual Features of Implementation: NA
* Reasons why other choices have been avoided: NA
* Step-wise Breakdown of an Algorithm:
- Wait for control on m_EventMutex Mutex to ensure that only one subscriber
is doing registration at a given time.
- validate the event if valid event then only register.
- Check if the free space is there to register new subscriber or not.
- if Available then update subscriber list array and register the same to Handle manager.
- Release Mutex control.
- Return Handle.
*/
HANDLE EventHandler::Register(const EventSourceList Event,EventCbInfo& rEventCbInfo) {
HANDLE Handle = 0;
if( (INVALID_EVENT != Event) && (DEACTIVATE_EVENT_HANDLER != Event)&&
( MAX_SUPPORTED_EVENTS > Event)&& (NULL != rEventCbInfo.EventCbFunc) ) {
//ensure that there is free space for new
if(osOK == osMutexWait(m_EventMutex,osWaitForever)) {
for(u8 u8Index = 0; u8Index < MAX_SUBSCRIBERS; u8Index++) {
if(NULL == m_ArpSubscriberList[Event][u8Index]) {
if( NULL != m_pHandleMngr) {
Handle = m_pHandleMngr->Register(&rEventCbInfo,
(Event*MAX_SUBSCRIBERS + u8Index) );
if( 0 != Handle) {
m_ArpSubscriberList[Event][u8Index] = &rEventCbInfo;
}
break;
}
}
}
osMutexRelease(m_EventMutex);
}
}
return Handle;
}
/**
* \fn EventHandler::Unregister(HANDLE Handle)
* \brief This function is used by subscriber to Unregister for particular event.
If the unregistration is successful then subscriber will not get the intimation on occurrence of that event.
* \param[in] HANDLE :- Handle to be unregistered.
* \return EventHndlrStatus :-
EVENT_HNDLR_OK - if unregistered successfully.
EVENT_HNDLR_INVALID_HANDLE :- if Handle is invalid.
EVENT_HNDLR_INVALID_MUTEX:- if the Mutex is not created properly.
* \details This function unregisters a particular subscriber.
After Unregistering from the Eventhandler,No events will be sent to the unregistered subscriber.
* Dependencies: NA
* Unusual Features of Implementation: NA
* Reasons why other choices have been avoided: NA
* Step-wise Breakdown of an Algorithm:
- Wait for control on m_EventMutex Mutex to ensure that only one subscriber
is doing unregistration at a given time.
- Check if the free space is there to register new subscriber or not.
- if Available then update subscriber list array and register the same to Handle manager.
- Activate the Thread which is waiting on mailbox to read the event.
- Release Mutex control.
- Return Handle.
*/
EventHndlrStatus EventHandler::Unregister(HANDLE Handle) {
u32 u32Index = 0;
u32 u32EventIndex = 0, u32SubscriberIndex = 0;
EventCbInfo *pEventCbInfo;
EventHndlrStatus Status = EVENT_HNDLR_INVALID_MUTEX;
//Wait for Mutex.
if(osOK == osMutexWait(m_EventMutex,osWaitForever)) {
Status = EVENT_HNDLR_INVALID_HANDLE;
/* read the value logged in Handler manager for given handle*/
if (( NULL != m_pHandleMngr) && (true == (m_pHandleMngr->GetValueTagRegForHandle(Handle, \
(void*&)pEventCbInfo, u32Index))) ) {
u32EventIndex = u32Index / MAX_SUBSCRIBERS;
u32SubscriberIndex = u32Index % MAX_SUBSCRIBERS;
/* Check if event is valid and Event Callback info of subscriber list matches with that is saved in Handle manager*/
if(( MAX_SUPPORTED_EVENTS > u32EventIndex) && \
(m_ArpSubscriberList[u32EventIndex][u32SubscriberIndex]->EventCbFunc == \
pEventCbInfo->EventCbFunc) ) {
/* if all info is valid the Unregister the handle*/
if(true == m_pHandleMngr->UnRegister(Handle)) {
/*update subscriber list to Null after successful Unregister*/
m_ArpSubscriberList[u32EventIndex][u32SubscriberIndex] = NULL;
Status = EVENT_HNDLR_OK;
}
}
}
osMutexRelease(m_EventMutex);
}
return Status;
}
/**
* \fn EventHandler::LogEvent(EventSourceList Source,EventData* Data)
* \brief This function shall be used by other class in order to log an event in the mailbox.
* \param[in] Source :- Source or Cause of Event
* \param[in] Data :- pointer to the Data related to that event.It can be null if no data is associated with the event.
* \return EventHndlrStatus :-
EVENT_HNDLR_OK - if event is logged successfully.
EVENT_HNDLR_LOGGING_FAILED :- if Event logging failed.
EVENT_HNDLR_MAIL_BOX_ALLOC_FAIL:- if mail box creation failed.
EVENT_HNDLR_EVENT_NOT_SUPPORTED :- if Event Is invalid
* \details This function shall be used by other class in order to log an event in the mailbox.
* Dependencies: NA
* Unusual Features of Implementation: value of u32Timeout should be 0 if log event is being called in Isr.
* Reasons why other choices have been avoided: NA
* Step-wise Breakdown of an Algorithm:
- Allocate memory for the mailbox.
- if Memory allocated successfully then log the data in allocated mailbox.
*/
EventHndlrStatus EventHandler::LogEvent(EventSourceList Source,EventData Data) {
EventHndlrStatus Status = EVENT_HNDLR_EVENT_NOT_SUPPORTED;
if( (INVALID_EVENT != Source) && ( MAX_SUPPORTED_EVENTS > Source)) {
Status = EVENT_HNDLR_MAIL_BOX_ALLOC_FAIL;
EventLog* pMailBox = (EventLog*)osMailAlloc(ms_EventMailBox, 0); // Allocate memory
if( NULL != pMailBox ) {
pMailBox->EventCode = Source;
pMailBox->Data = Data;
// Log Event and Data in MailBox
if(osOK == osMailPut(ms_EventMailBox, pMailBox)) {
Status = EVENT_HNDLR_OK;
}
else {
Status = EVENT_HNDLR_LOGGING_FAILED;
}
}
}
return Status;
}
/**
* \fn EventHandler::CtrlThread(void)
* \brief This is thread waiting on mailbox for an event.Whenever an event is logged in the mailbox, thread become active and start reading the mailbox.
It also checks for the subscriber list and and call the callback functions of all registered subscribers. After reading it frees the mailbox.
* \param[in] NA
* \return NA
* \details This is thread waiting on mailbox for an event.Whenever an event is logged in the mailbox, thread become active and start reading the mailbox.
It also checks for the subscriber list and and call the callback functions of all registered subscribers. After reading it frees the mailbox.
* Dependencies: CtrlThr
* Unusual Features of Implementation: NA
* Reasons why other choices have been avoided: NA
* Step-wise Breakdown of an Algorithm:
- Wait in mailbox for an event to get logged.
- Check the subscriber list in order to check the registered subscriber and their callback function.
- Copy the Event data in subscriber Event Data structure if reference is provided during registration.
- Call Callback function of all the subscribers who have registered for that event.
- Free respective mail box
*/
void EventHandler :: CtrlThread(void) {
EventLog* pEventLog = NULL;
osEvent Event;
EventSourceList EventSource = INVALID_EVENT;
EventData Data = NULL;
while(1)
{
Event = osMailGet(ms_EventMailBox,osWaitForever);
if( osEventMail == Event.status ) {
pEventLog = (EventLog*)Event.value.p;
if( NULL != pEventLog ) {
EventSource = pEventLog->EventCode;
Data = (pEventLog->Data);
}
if( (INVALID_EVENT != EventSource) && ( MAX_SUPPORTED_EVENTS > EventSource)) {
if(osOK == osMutexWait(m_EventMutex,osWaitForever)) {
for(u8 u8Index = 0; u8Index < MAX_SUBSCRIBERS; u8Index++) {
if( (NULL != m_ArpSubscriberList[EventSource][u8Index]) && \
(NULL != (m_ArpSubscriberList[EventSource][u8Index])->EventCbFunc)) {
if(NULL != Data) {
m_ArpSubscriberList[EventSource][u8Index]->LoggedData = Data;
}
((m_ArpSubscriberList[EventSource][u8Index])->EventCbFunc) \
((m_ArpSubscriberList[EventSource][u8Index]->CbData));
}
}
osMutexRelease(m_EventMutex);
}
}
osMailFree(ms_EventMailBox,pEventLog);
}
if(true == m_bStopThr) {
break;
}
}
}
/**
* \fn EventHandler::Deactivate(void)
* \brief This function deactivates the CtrlThread which is being used to read the event if any logged in mail queue.
* \param[in] NA
* \return i32 :-
CTRL_THR_OPRN_SUCCESS - CtrlThr is successfully deactivated
CTRL_THR_NO_THREAD - Error in deactvating/stoping the CtrlThr thread.
LOG_EVENT_FAILED - if Event Logging failed.
MEMORY_ALLOC_FAIL_FOR_LOG_EVENT - if memory allocation failed while logging event.
LOG_EVENT_UNSUPPORTED - if event to be logged to trigger the thread is unsupported.
* \details This function deactivates the CtrlThread which is being used to read the event if any logged in mail queue.
* Dependencies: CtrlThr
* Unusual Features of Implementation: NA
* Reasons why other choices have been avoided: NA
* Step-wise Breakdown of an Algorithm:
- Check if thread is active or not. if active then log the Deactivate event handler event in mail queue to trigger the thread.
- Call Deactivate function of CtrlThr class to deactivate the thread.
*/
i32 EventHandler :: Deactivate(void) {
EventHndlrStatus Status;
i32 iDeactivateStatus = CTRL_THR_NO_THREAD;
/*Check if thread is active*/
if (true == m_bActive) {
m_bStopThr = true;
/*Trigger CtrlThread in order to log the Deactivate Event Handler event in mail box.*/
Status = LogEvent(DEACTIVATE_EVENT_HANDLER,NULL);
/* Deactivate CtrlThr also*/
if(Status == EVENT_HNDLR_OK) {
iDeactivateStatus = CtrlThr::Deactivate();
}
else if(Status == EVENT_HNDLR_LOGGING_FAILED) {
iDeactivateStatus = LOG_EVENT_FAILED;
}
else if(Status == EVENT_HNDLR_MAIL_BOX_ALLOC_FAIL) {
iDeactivateStatus = MEMORY_ALLOC_FAIL_FOR_LOG_EVENT;
}
else {
iDeactivateStatus = LOG_EVENT_UNSUPPORTED;
}
}
return iDeactivateStatus;
}
/**
* \fn EventHandler::Activate(void)
* \brief This function activates the CtrlThread which is being used to read the event if any logged in mail queue.
* \param[in] NA
* \return i32
CTRL_THR_OPRN_SUCCESS - CntrlThr is successfully activated
CTRL_THR_ALREADY_ACTIVE - CntrlThr is already active
CTRL_THR_NO_THREAD - Error in activating/starting the CntrlThr thread
* \details This function activates the CtrlThread which is being used to read the event if any logged in mail queue.
* Dependencies: CtrlThr
* Unusual Features of Implementation: NA
* Reasons why other choices have been avoided: NA
* Step-wise Breakdown of an Algorithm:
- Call activate function of CtrlThr class to activate the thread.
*/
i32 EventHandler ::Activate(void){
return CtrlThr::Activate();
}
#include "SqdAlgo.h"
#include "stdio.h"
/**
* \brief this method is the signal quality class constructor.
* \param[in] void
* \return void
* \details
* Dependencies : NA
* Unusual Features of Implementation : os_clockrate is equal to OS_TICK (currently 1000us). this variable
is used for time tracking in alarm_hyst function.
* Reasons why other choices have been avoided : NA
* Step-wise Breakdown of an Algorithm :
- Initialize the member variables AND call Init function.
**/
SqdAlgo::SqdAlgo(void) :
OS_TICKS_PER_SEC(1000000 / os_clockrate),
ONSET_PERIOD(SIXTY_SECONDS * OS_TICKS_PER_SEC),RECOVERY_PERIOD(SIXTY_SECONDS * OS_TICKS_PER_SEC),
FhrMode(IFhrAlgo::INOP) {
Init();
}
/**
* \brief this method is the used to initialize member variables. Currently called in constructor and whenever
connector is disconnected.
* \param[in] void
* \return void
* \details
* Dependencies : NA
* Unusual Features of Implementation : NA
* Reasons why other choices have been avoided : NA
* Step-wise Breakdown of an Algorithm :
- Initialize the member variables
**/
void SqdAlgo::Init(void) {
sq_mode = ONE_MIN_SQ;
sq_hyst_cnt = 0;
sq_hyst_tim = os_time;
m_CurrTimeMilliSec = os_time;
sq=false;
m_SigQual = GREEN_SQ;
}
/**
* \brief this method is the main method for Signal quality algorithm.
* \param[in] i16 - current_value - current FHR value
* \return eSignalQuality - returns signal loss level (RED, GREEN, YELLOW) .
* \details
* Dependencies : NA
* Unusual Features of Implementation : This function needs to run every 250ms.
* Reasons why other choices have been avoided : NA
* Step-wise Breakdown of an Algorithm :
- Run alarm hysteresis algorithm initially when the transducer is connected. if signal is >30% in first minute,
change state to TWO_MIN_SQ
- in the second minute onwards, run sq_level function, which updates sq_hyst_cnt
**/
ISqdAlgo::eSignalQuality SqdAlgo::fhr_sig_qual(i16 i16Fhr) {
//eSignalQuality SigQual = RESERVED_SQ;
if(IFhrAlgo::CONNECTED == FhrMode) {
switch (sq_mode) {
case ONE_MIN_SQ: { // > 70% PENLIFT FOR 1 MIN. MEANS NO // SIGNAL ACQUIRED
alarm_hyst(i16Fhr);
if (sq_hyst_cnt >= (SIXTY_SECONDS * OS_TICKS_PER_SEC * 3 / 10)){ // IF FHR non zero MORE THAN 30% of 60s i.e. 18s// OF TIME.
sq_mode = TWO_MIN_SQ;
sq_hyst_cnt = 0; // RESET HYSTERISIS COUNTER
sq_hyst_tim = 0;
m_CurrTimeMilliSec = os_time;
//m_SigQual = YELLOW_SQ;
m_SigQual = GREEN_SQ;
//sq = true;
}
else {
//what should be the SQ now ?
//currently returning RESERVED_SQ
//m_SigQual = RED_SQ;
}
}
break;
case TWO_MIN_SQ: {
//case FIVE_MIN_SQ: // 61-100% PENLIFT DATA FOR 30 min.-75 sec.- POOR SIGNAL
sq_level(i16Fhr); // DETERMINE SQ LEVELS.
if (sq == false && sq_hyst_cnt >= SQ_PLIFT_MAX) {
sq = true;
//SigQual = GetSignalQuality(); //calculate signal quality once sq_hyst_cnt exceeds SQ_PLIFT_MAX
m_SigQual = RED_SQ;
}
else if(sq == true && sq_hyst_cnt <= SQ_PLIFT_MIN){
sq = false;
m_SigQual = GREEN_SQ;
m_CurrTimeMilliSec = os_time;
}
// else {
// printf("%d %d\r\n",SQ_PLIFT_MAX, sq_hyst_cnt);
// }
}
break;
default :
//should never come here. Do some error handling.
break;
}
}
else if(IFhrAlgo::INOP == FhrMode) {
Init(); //what should be the SQ now ?
}
return m_SigQual;
}
/**
* \brief this method is the signal quality quantifier function.
* \param[in] i16 - i16Fhr - current FHR value
* \return void
* \details
* Dependencies : NA
* Unusual Features of Implementation : NA
* Reasons why other choices have been avoided : NA
* Step-wise Breakdown of an Algorithm :
- increment sq_hyst_cnt by PLIFT_WGT if FHR value is 0 (PENLIFT)
- decrement sq_hyst_cnt by HR_WGT if FHR value is non zero
**/
void SqdAlgo::sq_level(i16 i16Fhr) {
// sq_hyst_tim += ((os_time - m_CurrTimeMilliSec) * os_clockrate); //in uS
//
// m_CurrTimeMilliSec = os_time;
//
// sq_hyst_tim /= 1000; //convert from us to ms
if ( 0 == i16Fhr ) { //PENLIFT
sq_hyst_cnt += PLIFT_WGT;
if (sq_hyst_cnt >= SQ_PLIFT_MAX) {
sq_hyst_cnt = SQ_PLIFT_MAX; // CLAMP AT MAX
}
}
else {
if (sq_hyst_cnt >= HR_WGT) {
sq_hyst_cnt -= HR_WGT;
}
}
}
/**
* \brief this method is the alarm hysterisis algorithm (although currently used only for SQD algorithm in Lotus FE).
Currently sq_hyst_cnt is incremented by the number of seconds that the HR increased above the upper limit (1) and decremented
by the number of seconds that the HR falls below upper limit (1 . i,e, when it is INOP).
* \param[in] i16 - current_value - current FHR value
* \return void
* \details
* Dependencies : NA
* Unusual Features of Implementation : NA
* Reasons why other choices have been avoided : NA
* Step-wise Breakdown of an Algorithm :
- increment sq_hyst_cnt by the number of seconds that the HR increased above the upper limit (1)
- decrement sq_hyst_cnt by the number of seconds that the HR falls below upper limit (1 . i,e, when it is INOP).
**/
void SqdAlgo::alarm_hyst(const i16 current_value){
i32 localMilliSeconds = os_time; // Read time once
/* This code is taken from Coro 250cx where this function is used for alarms as well as SQD algorithm. Since, in
Lotus, this is required only for SQD (as alarms processing happens in BE), retaining only whats needed for SQD
Algorithm. In this case, we are interesed in checking the percentage of time, valid HR was received, no need of
low HR detection as well as disabling of limit check
*/
if ( true ) {
//if ( ( (current_value > u16HiLimit) /*&& (u16HiLimit != OFF)*/ ) // CHECK FOR OUT OF LIMITS CONDITION, MAKING SURE LIMIT ISN'T TURNED OFF
// || ( (current_value < lo_limit) && (lo_limit != OFF) )
//) {
if (sq_hyst_cnt == 0) { // UPDATE THE HYSTERISYS UP/DOWN COUNTER ON A ONE SECOND BASIS
sq_hyst_tim = localMilliSeconds;
sq_hyst_cnt = 1;
}
else {
sq_hyst_cnt += (localMilliSeconds - sq_hyst_tim);
}
if (sq_hyst_cnt > ONSET_PERIOD) { // RANGE CHECK HYSTERISYS COUNTER
sq_hyst_cnt = ONSET_PERIOD;
}
}
else {
sq_hyst_cnt -= (localMilliSeconds - sq_hyst_tim);
if (sq_hyst_cnt < (ONSET_PERIOD - RECOVERY_PERIOD)) { // RANGE CHECK HYSTERISYS COUNTER
sq_hyst_cnt = 0;
}
}
sq_hyst_tim = localMilliSeconds; // RECORD THE CURRENT TIME AS THE HYSTERISYS TIME
}
/**
* \brief this method is used to set the FHR mode (to be used in SQD algorithm).
* \param[in] IFhrAlgo::TransducerState - mode transducer connection status (CONNECTED or INOP)
* \return void
* \details
* Dependencies : NA
* Unusual Features of Implementation : NA
* Reasons why other choices have been avoided : NA
* Step-wise Breakdown of an Algorithm :
- Store the input parameter into member variable.
**/
void SqdAlgo::SetMode(IFhrAlgo::TransducerState mode) {
if((IFhrAlgo::INOP == mode) && (IFhrAlgo::INOP != FhrMode)) {
Init();
}
FhrMode = mode;
}
/**
* \brief this method is used to query the FHR mode (to be used in SQD algorithm).
* \param[in] void
* \return IFhrAlgo::TransducerState - transducer connection status (CONNECTED or INOP)
* \details
* Dependencies : NA
* Unusual Features of Implementation : NA
* Reasons why other choices have been avoided : NA
* Step-wise Breakdown of an Algorithm :
- return the member variable used to store the fhr mode.
**/
IFhrAlgo::TransducerState SqdAlgo::GetMode(void){
return FhrMode;
}
/**
* \brief this method is used to calculate the signal loss level (RED, GREEN, YELLOW) depending on the signal loss.
* \param[in] void
* \return eSignalQuality
returns signal loss level (RED, GREEN, YELLOW) .
* \details
* Dependencies : NA