-
Notifications
You must be signed in to change notification settings - Fork 24
/
sch_app.c
1272 lines (1082 loc) · 36.1 KB
/
sch_app.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
/*
** $Id: sch_app.c 1.5 2017/06/21 15:29:02EDT mdeschu Exp $
**
** Copyright (c) 2007-2014 United States Government as represented by the
** Administrator of the National Aeronautics and Space Administration.
** All Other Rights Reserved.
**
** This software was created at NASA's Goddard Space Flight Center.
** This software is governed by the NASA Open Source Agreement and may be
** used, distributed and modified only pursuant to the terms of that
** agreement.
**
** Purpose: Scheduler (SCH) application
**
** Author:
**
** Notes:
**
*/
/*************************************************************************
**
** Include section
**
**************************************************************************/
#include "cfe.h"
#include "sch_msgids.h"
#include "sch_perfids.h"
#include "sch_platform_cfg.h"
#if SCH_LIB_PRESENCE == 1
#include "sch_api.h"
#endif
#include "sch_custom.h"
#include "sch_msg.h"
#include "sch_events.h"
#include "sch_app.h"
#include "sch_cmds.h"
#include "sch_version.h"
#include "cfe_time_msg.h"
#include "sch_verify.h"
#include "cfe_platform_cfg.h" /* for CFE_SB_HIGHEST_VALID_MSGID */
/*************************************************************************
**
** Macro definitions
**
**************************************************************************/
/*
** Time Semaphore Characteristics
*/
#define SCH_SEM_NAME "SCH_TIME_SEM"
#define SCH_SEM_VALUE 0
#define SCH_SEM_OPTIONS 0
/*
** SDT Table Validation Error Codes
*/
#define SCH_SDT_GARBAGE_ENTRY (-1)
#define SCH_SDT_NO_FREQUENCY (-2)
#define SCH_SDT_BAD_REMAINDER (-3)
#define SCH_SDT_BAD_ACTIVITY (-4)
#define SCH_SDT_BAD_MSG_INDEX (-5)
#define SCH_SDT_BAD_ENABLE_STATE (-6)
/*
** MDT Table Validation Error Codes
*/
#define SCH_MDT_GARBAGE_ENTRY (-1)
#define SCH_MDT_INVALID_LENGTH (-2)
#define SCH_MDT_BAD_MSG_ID (-3)
/*************************************************************************
**
** Type definitions
**
**************************************************************************/
/*
** (none)
*/
/*************************************************************************
**
** Imported data
**
**************************************************************************/
/*
** (none)
*/
/*************************************************************************
**
** Exported data
**
**************************************************************************/
/*
** Application global data
*/
SCH_AppData_t SCH_AppData;
/*************************************************************************
**
** Function definitions
**
**************************************************************************/
/*******************************************************************
**
** SCH_AppMain
**
** NOTE: For complete prolog information, see 'sch_app.h'
********************************************************************/
void SCH_AppMain(void)
{
int32 Status = CFE_SUCCESS;
uint32 RunStatus = CFE_ES_APP_RUN;
/*
** Performance Log (start time counter)
*/
CFE_ES_PerfLogEntry(SCH_APPMAIN_PERF_ID);
/*
** Register application
*/
Status = CFE_ES_RegisterApp();
/*
** Perform application specific initialization
*/
if (Status == CFE_SUCCESS)
{
Status = SCH_AppInit();
}
/* If no errors were detected during initialization, then wait for everyone to start */
if (Status == CFE_SUCCESS)
{
Status = SCH_CustomLateInit();
if (Status != CFE_SUCCESS)
{
CFE_EVS_SendEvent(SCH_MAJOR_FRAME_SUB_ERR_EID, CFE_EVS_ERROR,
"Error initializing Timers (RC=0x%08X)",
(unsigned int)Status);
}
}
/*
** Check for start-up error
*/
if (Status != CFE_SUCCESS)
{
/*
** Set request to terminate main loop
*/
RunStatus = CFE_ES_APP_ERROR;
}
/*
** Main process loop
*/
while (CFE_ES_RunLoop(&RunStatus))
{
/*
** Performance Log (stop time counter)
*/
CFE_ES_PerfLogExit(SCH_APPMAIN_PERF_ID);
/*
** Wait for the next slot (Major or Minor Frame)
*/
Status = OS_BinSemTake(SCH_AppData.TimeSemaphore);
/*
** Performance Log (start time counter)
*/
CFE_ES_PerfLogEntry(SCH_APPMAIN_PERF_ID);
/*
** Report if during the previous frame the major has determined to be unstable
*/
if (SCH_AppData.IgnoreMajorFrame == TRUE)
{
if (SCH_AppData.IgnoreMajorFrameMsgSent == FALSE)
{
CFE_EVS_SendEvent(SCH_NOISY_MAJOR_FRAME_ERR_EID, CFE_EVS_ERROR,
"Major Frame Sync too noisy (Slot %d). Disabling synchronization.",
SCH_AppData.MinorFramesSinceTone);
SCH_AppData.IgnoreMajorFrameMsgSent = TRUE;
}
}
else
{
SCH_AppData.IgnoreMajorFrameMsgSent = FALSE;
}
/*
** Process schedule table activities
*/
#if SCH_LIB_PRESENCE == 1
if ((Status == OS_SUCCESS) &&
(SCH_GetProcessingState() == TRUE))
{
Status = SCH_ProcessScheduleTable();
}
#else
if (Status == OS_SUCCESS)
{
Status = SCH_ProcessScheduleTable();
}
#endif
/*
** Note: If there were some reason to exit the task
** normally (without error) then we would set
** RunStatus = CFE_ES_APP_EXIT
*/
if (Status != SCH_SUCCESS)
{
/*
** Set request to terminate main loop
*/
RunStatus = CFE_ES_APP_ERROR;
}
} /* End of while */
/*
** Check for "fatal" process error
*/
if ((Status != OS_SUCCESS) || (Status != SCH_SUCCESS))
{
/*
** Send an event describing the reason for the termination
*/
CFE_EVS_SendEvent(SCH_APP_EXIT_EID, CFE_EVS_CRITICAL,
"SCH App: terminating, err = 0x%08X", (unsigned int)Status);
/*
** In case cFE Event Services is not working
*/
CFE_ES_WriteToSysLog("SCH App terminating, err = 0x%08X\n", (unsigned int)Status);
}
/*
** Perform any custom cleanup
*/
SCH_CustomCleanup();
/*
** Performance Log (stop time counter)
*/
CFE_ES_PerfLogExit(SCH_APPMAIN_PERF_ID);
/*
** Let cFE kill the task (and any child tasks)
*/
CFE_ES_ExitApp(RunStatus);
} /* End of SH_AppMain() */
/*******************************************************************
**
** SCH_AppInit
**
** NOTE: For complete prolog information, see above
********************************************************************/
int32 SCH_AppInit(void)
{
int32 Status = CFE_SUCCESS;
/*
** Initialize Application Processing Counters
*/
SCH_AppData.SlotsProcessedCount = 0;
SCH_AppData.SkippedSlotsCount = 0;
SCH_AppData.MultipleSlotsCount = 0;
SCH_AppData.SameSlotCount = 0;
SCH_AppData.ScheduleActivitySuccessCount = 0;
SCH_AppData.ScheduleActivityFailureCount = 0;
/*
** Initialize Command Counters
*/
SCH_AppData.CmdCounter = 0;
SCH_AppData.ErrCounter = 0;
/*
** Get our assigned Application ID
*/
Status = CFE_ES_GetAppID(&SCH_AppData.AppID);
if (Status != CFE_SUCCESS)
{
CFE_ES_WriteToSysLog("SCH App: Unable to obtain own AppID, RC=0x%08X\n", (unsigned int)Status);
return(Status);
}
/*
** Initialize Event Services Interface
*/
Status = SCH_EvsInit();
if (Status != CFE_SUCCESS)
{
return(Status);
}
Status = SCH_SbInit();
if (Status != CFE_SUCCESS)
{
return(Status);
}
/*
** Initialize application tables
*/
Status = SCH_TblInit();
if (Status != CFE_SUCCESS)
{
return(Status);
}
/*
** Initialize timer interfaces
*/
Status = SCH_TimerInit();
if (Status != CFE_SUCCESS)
{
return(Status);
}
/*
** Application startup event message
*/
Status = CFE_EVS_SendEvent(SCH_INITSTATS_INF_EID,
CFE_EVS_INFORMATION,
"SCH Initialized. Version %d.%d.%d.%d",
SCH_MAJOR_VERSION,
SCH_MINOR_VERSION,
SCH_REVISION,
SCH_MISSION_REV);
return(Status);
} /* End of SCH_AppInit() */
/*******************************************************************
**
** SCH_EvsInit
**
** NOTE: For complete prolog information, see above
********************************************************************/
int32 SCH_EvsInit(void)
{
int32 Status = CFE_SUCCESS;
SCH_AppData.EventFilters[0].EventID = SCH_SAME_SLOT_EID;
SCH_AppData.EventFilters[0].Mask = CFE_EVS_FIRST_ONE_STOP;
SCH_AppData.EventFilters[1].EventID = SCH_MULTI_SLOTS_EID;
SCH_AppData.EventFilters[1].Mask = CFE_EVS_FIRST_ONE_STOP;
SCH_AppData.EventFilters[2].EventID = SCH_SKIPPED_SLOTS_EID;
SCH_AppData.EventFilters[2].Mask = CFE_EVS_FIRST_ONE_STOP;
SCH_AppData.EventFilters[3].EventID = SCH_CORRUPTION_EID;
SCH_AppData.EventFilters[3].Mask = CFE_EVS_FIRST_TWO_STOP;
SCH_AppData.EventFilters[4].EventID = SCH_PACKET_SEND_EID;
SCH_AppData.EventFilters[4].Mask = CFE_EVS_FIRST_ONE_STOP;
/*
** Register for event services
*/
Status = CFE_EVS_Register(SCH_AppData.EventFilters, SCH_FILTER_COUNT, CFE_EVS_BINARY_FILTER);
if (Status != CFE_SUCCESS)
{
CFE_ES_WriteToSysLog("SCH App: Error Registering For Event Services, RC=0x%08X\n", (unsigned int)Status);
}
return(Status);
} /* End of SCH_EvsInit() */
/*******************************************************************
**
** SCH_SbInit
**
** NOTE: For complete prolog information, see above
********************************************************************/
int32 SCH_SbInit(void)
{
int32 Status = CFE_SUCCESS;
SCH_AppData.MsgPtr = (CFE_SB_MsgPtr_t) NULL;
SCH_AppData.CmdPipe = 0;
/*
** Initialize housekeeping packet (clear user data area)
*/
CFE_SB_InitMsg(&SCH_AppData.HkPacket, SCH_HK_TLM_MID, sizeof(SCH_HkPacket_t), TRUE);
/*
** Initialize diagnostic packet (clear user data area)
*/
CFE_SB_InitMsg(&SCH_AppData.DiagPacket, SCH_DIAG_TLM_MID, sizeof(SCH_DiagPacket_t), TRUE);
/*
** Create Software Bus message pipe
*/
Status = CFE_SB_CreatePipe(&SCH_AppData.CmdPipe, SCH_PIPE_DEPTH, SCH_PIPE_NAME);
if (Status != CFE_SUCCESS)
{
CFE_EVS_SendEvent(SCH_CR_PIPE_ERR_EID, CFE_EVS_ERROR,
"Error Creating SB Pipe, RC=0x%08X", (unsigned int)Status);
return(Status);
}
/*
** Subscribe to Housekeeping request commands
*/
Status = CFE_SB_Subscribe(SCH_SEND_HK_MID, SCH_AppData.CmdPipe);
if (Status != CFE_SUCCESS)
{
CFE_EVS_SendEvent(SCH_SUB_HK_REQ_ERR_EID, CFE_EVS_ERROR,
"Error Subscribing to HK Request(MID=0x%04X), RC=0x%08X",
SCH_SEND_HK_MID, (unsigned int)Status);
return(Status);
}
/*
** Subscribe to SCH ground command packets
*/
Status = CFE_SB_Subscribe(SCH_CMD_MID, SCH_AppData.CmdPipe);
if (Status != CFE_SUCCESS)
{
CFE_EVS_SendEvent(SCH_SUB_GND_CMD_ERR_EID, CFE_EVS_ERROR,
"Error Subscribing to GND CMD(MID=0x%04X), RC=0x%08X",
SCH_CMD_MID, (unsigned int)Status);
return(Status);
}
return(Status);
} /* End of SCH_SbInit() */
/*******************************************************************
**
** SCH_TblInit
**
** NOTE: For complete prolog information, see above
********************************************************************/
int32 SCH_TblInit(void)
{
uint32 TableSize = 0;
int32 Status = CFE_SUCCESS;
/*
** Initialize SCH table variables
*/
SCH_AppData.ScheduleTable = (SCH_ScheduleEntry_t *) NULL;
SCH_AppData.MessageTable = (SCH_MessageEntry_t *) NULL;
SCH_AppData.ScheduleTableHandle = CFE_TBL_BAD_TABLE_HANDLE;
SCH_AppData.MessageTableHandle = CFE_TBL_BAD_TABLE_HANDLE;
SCH_AppData.BadTableDataCount = 0;
SCH_AppData.TableVerifySuccessCount = 0;
SCH_AppData.TableVerifyFailureCount = 0;
SCH_AppData.TablePassCount = 0;
/*
** Register schedule definition table
*/
TableSize = SCH_TABLE_ENTRIES * sizeof (SCH_ScheduleEntry_t);
Status = CFE_TBL_Register(&SCH_AppData.ScheduleTableHandle,
SCH_SCHEDULE_TABLE_NAME,
TableSize,
CFE_TBL_OPT_DEFAULT,
SCH_ValidateScheduleData);
if (Status != CFE_SUCCESS)
{
CFE_EVS_SendEvent(SCH_SDT_REG_ERR_EID, CFE_EVS_ERROR,
"Error Registering SDT, RC=0x%08X",
(unsigned int)Status);
return(Status);
}
/*
** Register message definition table
*/
TableSize = SCH_MAX_MESSAGES * sizeof (SCH_MessageEntry_t);
Status = CFE_TBL_Register(&SCH_AppData.MessageTableHandle,
SCH_MESSAGE_TABLE_NAME,
TableSize,
CFE_TBL_OPT_DEFAULT,
SCH_ValidateMessageData);
if (Status != CFE_SUCCESS)
{
CFE_EVS_SendEvent(SCH_MDT_REG_ERR_EID, CFE_EVS_ERROR,
"Error Registering MDT, RC=0x%08X",
(unsigned int)Status);
return(Status);
}
/*
** Load default schedule definition table data
*/
Status = CFE_TBL_Load(SCH_AppData.ScheduleTableHandle,
CFE_TBL_SRC_FILE,
(const void *) SCH_SCHEDULE_FILENAME);
if (Status != CFE_SUCCESS)
{
CFE_EVS_SendEvent(SCH_SDT_LOAD_ERR_EID, CFE_EVS_ERROR,
"Error (RC=0x%08X) Loading SDT with %s",
(unsigned int)Status, SCH_SCHEDULE_FILENAME);
return(Status);
}
/*
** Load default message definition table data
*/
Status = CFE_TBL_Load(SCH_AppData.MessageTableHandle,
CFE_TBL_SRC_FILE,
(const void *) SCH_MESSAGE_FILENAME);
if (Status != CFE_SUCCESS)
{
CFE_EVS_SendEvent(SCH_MDT_LOAD_ERR_EID, CFE_EVS_ERROR,
"Error (RC=0x%08X) Loading MDT with %s",
(unsigned int)Status, SCH_MESSAGE_FILENAME);
return(Status);
}
/*
** Get pointers to table data
*/
Status = SCH_AcquirePointers();
if (Status != CFE_SUCCESS)
{
CFE_EVS_SendEvent(SCH_ACQ_PTR_ERR_EID, CFE_EVS_ERROR,
"Error Acquiring Tbl Ptrs (RC=0x%08X)",
(unsigned int)Status);
return(Status);
}
return(Status);
} /* End of SCH_TblInit() */
/*******************************************************************
**
** SCH_TimerInit
**
** NOTE: For complete prolog information, see above
********************************************************************/
int32 SCH_TimerInit(void)
{
int32 Status = CFE_SUCCESS;
/*
** Start off assuming Major Frame synch is normal
** and should be coming at any moment
*/
SCH_AppData.IgnoreMajorFrame = FALSE;
SCH_AppData.IgnoreMajorFrameMsgSent = FALSE;
SCH_AppData.UnexpectedMajorFrame = FALSE;
SCH_AppData.SyncToMET = SCH_NOT_SYNCHRONIZED;
SCH_AppData.MajorFrameSource = SCH_MAJOR_FS_NONE;
SCH_AppData.NextSlotNumber = 0;
SCH_AppData.MinorFramesSinceTone = SCH_TIME_SYNC_SLOT;
SCH_AppData.LastSyncMETSlot = 0;
SCH_AppData.SyncAttemptsLeft = 0;
SCH_AppData.UnexpectedMajorFrameCount = 0;
SCH_AppData.MissedMajorFrameCount = 0;
SCH_AppData.ValidMajorFrameCount = 0;
SCH_AppData.WorstCaseSlotsPerMinorFrame = 1;
/*
** Configure Major Frame and Minor Frame sources
*/
SCH_AppData.ClockAccuracy = SCH_WORST_CLOCK_ACCURACY;
/*
** Create the timer to be used for minor frames
*/
Status = SCH_CustomEarlyInit();
if (Status != CFE_SUCCESS)
{
CFE_EVS_SendEvent(SCH_MINOR_FRAME_TIMER_CREATE_ERR_EID, CFE_EVS_ERROR,
"Error creating Timer (RC=0x%08X)",
(unsigned int)Status);
return(Status);
}
/*
** Determine if the timer has an acceptable clock accuracy
*/
if (SCH_AppData.ClockAccuracy > SCH_WORST_CLOCK_ACCURACY)
{
CFE_EVS_SendEvent(SCH_MINOR_FRAME_TIMER_ACC_WARN_EID, CFE_EVS_INFORMATION,
"OS Timer Accuracy (%d > reqd %d usec) requires Minor Frame MET sync",
(int)SCH_AppData.ClockAccuracy, SCH_WORST_CLOCK_ACCURACY);
/* Synchronize Minor Frame Timing with Mission Elapsed Time to keep from losing slots */
SCH_AppData.SyncToMET = SCH_MINOR_SYNCHRONIZED;
/* Calculate how many slots we may have to routinely process on each Minor Frame Wakeup */
SCH_AppData.WorstCaseSlotsPerMinorFrame = ((SCH_AppData.ClockAccuracy * 2) / SCH_NORMAL_SLOT_PERIOD) + 1;
}
/*
** Create main task semaphore (given by MajorFrameCallback and MinorFrameCallback)
*/
Status = OS_BinSemCreate(&SCH_AppData.TimeSemaphore, SCH_SEM_NAME, SCH_SEM_VALUE, SCH_SEM_OPTIONS);
if (Status != CFE_SUCCESS)
{
CFE_EVS_SendEvent(SCH_SEM_CREATE_ERR_EID, CFE_EVS_ERROR,
"Error creating Main Loop Timing Semaphore (RC=0x%08X)",
(unsigned int)Status);
return(Status);
}
return(Status);
} /* End of SCH_TimerInit() */
/*******************************************************************
**
** SCH_ProcessScheduleTable
**
** NOTE: For complete prolog information, see above
********************************************************************/
int32 SCH_ProcessScheduleTable(void)
{
uint32 CurrentSlot;
uint32 ProcessCount;
int32 Result = CFE_SUCCESS;
/*
** Get the slot we should be at
*/
CurrentSlot = SCH_CustomGetCurrentSlotNumber();
/*
** Compute the number of slots we need to process (watch for rollover)
*/
if (CurrentSlot < SCH_AppData.NextSlotNumber)
{
ProcessCount = SCH_TOTAL_SLOTS - SCH_AppData.NextSlotNumber;
ProcessCount += (CurrentSlot + 1);
}
else
{
ProcessCount = (CurrentSlot - SCH_AppData.NextSlotNumber) + 1;
}
/*
** Correct for the following conditions observed when minor frame driven
** by a clock with poor accuracy
**
** 1) Wake up a little too late for just 1 slot
** symptom = multi slots event followed by same slot event
**
** 2) Wake up a little too early for just 1 slot
** symptom = same slot event followed by multi slots event
*/
if (ProcessCount == 2)
{
/*
** If we want to do 2 slots but last time was OK then assume we
** are seeing condition #1 above. By doing just 1 slot now,
** there will still be 1 to do when the next wakeup occurs
** and we will avoid both events. But, if we really are in
** a delayed state, we will process both slots when we wake
** up next time because then the last time will NOT be OK.
*/
if (SCH_AppData.LastProcessCount == 1)
{
ProcessCount = 1;
}
SCH_AppData.LastProcessCount = 2;
}
else if (ProcessCount == SCH_TOTAL_SLOTS)
{
/*
** Same as previous comment except in reverse order.
*/
if (SCH_AppData.LastProcessCount != SCH_TOTAL_SLOTS)
{
ProcessCount = 1;
}
SCH_AppData.LastProcessCount = SCH_TOTAL_SLOTS;
}
else
{
SCH_AppData.LastProcessCount = ProcessCount;
}
/*
** If current slot = next slot - 1, assume current slot did not increment
*/
if (ProcessCount == SCH_TOTAL_SLOTS)
{
SCH_AppData.SameSlotCount++;
CFE_EVS_SendEvent(SCH_SAME_SLOT_EID, CFE_EVS_DEBUG,
"Slot did not increment: slot = %d",
(int)CurrentSlot);
ProcessCount = 0;
}
/*
** If we are too far behind, jump forward and do just the current slot
*/
if (ProcessCount > SCH_MAX_LAG_COUNT)
{
SCH_AppData.SkippedSlotsCount++;
CFE_EVS_SendEvent(SCH_SKIPPED_SLOTS_EID, CFE_EVS_ERROR,
"Slots skipped: slot = %d, count = %d",
SCH_AppData.NextSlotNumber, (int)(ProcessCount - 1));
/*
** Update the pass counter if we are skipping the rollover slot
*/
if (CurrentSlot < SCH_AppData.NextSlotNumber)
{
SCH_AppData.TablePassCount++;
}
/*
** Process ground commands if we are skipping the time synch slot
** NOTE: This assumes the Time Synch Slot is the LAST Schedule slot
** (see definition of SCH_TIME_SYNC_SLOT in sch_app.h)
** Ground commands should only be processed at the end of the schedule table
** so that Group Enable/Disable commands do not change the state of entries
** in the middle of a schedule.
*/
if ((SCH_AppData.NextSlotNumber + ProcessCount) > SCH_TIME_SYNC_SLOT)
{
Result = SCH_ProcessCommands();
}
SCH_AppData.NextSlotNumber = CurrentSlot;
ProcessCount = 1;
}
/*
** Don't try to catch up all at once, just do a couple
*/
if (ProcessCount > SCH_MAX_SLOTS_PER_WAKEUP)
{
ProcessCount = SCH_MAX_SLOTS_PER_WAKEUP;
}
/*
** Keep track of multi-slot processing
*/
if (ProcessCount > 1)
{
SCH_AppData.MultipleSlotsCount++;
/* Generate an event message if not syncing to MET or when there is more than two being processed */
if ((ProcessCount > SCH_AppData.WorstCaseSlotsPerMinorFrame) || (SCH_AppData.SyncToMET == SCH_NOT_SYNCHRONIZED))
{
CFE_EVS_SendEvent(SCH_MULTI_SLOTS_EID, CFE_EVS_INFORMATION,
"Multiple slots processed: slot = %d, count = %d",
SCH_AppData.NextSlotNumber, (int)ProcessCount);
}
}
/*
** Process the slots (most often this will be just one)
*/
while ((ProcessCount != 0) && (Result == CFE_SUCCESS))
{
Result = SCH_ProcessNextSlot();
ProcessCount--;
}
return(Result);
} /* End of SCH_ProcessScheduleTable() */
/*******************************************************************
**
** SCH_ProcessNextSlot
**
** NOTE: For complete prolog information, see above
********************************************************************/
int32 SCH_ProcessNextSlot(void)
{
int32 Result = CFE_SUCCESS;
int32 EntryNumber;
int32 SlotIndex;
SCH_ScheduleEntry_t *NextEntry;
SlotIndex = SCH_AppData.NextSlotNumber * SCH_ENTRIES_PER_SLOT;
NextEntry = &SCH_AppData.ScheduleTable[SlotIndex];
/*
** Process each (enabled) entry in the schedule table slot
*/
for (EntryNumber = 0; EntryNumber < SCH_ENTRIES_PER_SLOT; EntryNumber++)
{
if (NextEntry->EnableState == SCH_ENABLED)
{
SCH_ProcessNextEntry(NextEntry, EntryNumber);
}
NextEntry++;
}
/*
** Process ground commands in the slot reserved for time synch
** Ground commands should only be processed at the end of the schedule table
** so that Group Enable/Disable commands do not change the state of entries
** in the middle of a schedule.
*/
if (SCH_AppData.NextSlotNumber == SCH_TIME_SYNC_SLOT)
{
Result = SCH_ProcessCommands();
}
/*
** Maintain "next" schedule table slot index
*/
SCH_AppData.NextSlotNumber++;
if (SCH_AppData.NextSlotNumber == SCH_TOTAL_SLOTS)
{
SCH_AppData.NextSlotNumber = 0;
SCH_AppData.TablePassCount++;
}
/*
** Maintain "total slots processed" counter
*/
SCH_AppData.SlotsProcessedCount++;
return(Result);
} /* End of SCH_ProcessNextSlot() */
/*******************************************************************
**
** SCH_ProcessNextEntry
**
** NOTE: For complete prolog information, see above
********************************************************************/
void SCH_ProcessNextEntry(SCH_ScheduleEntry_t *NextEntry, int32 EntryNumber)
{
int32 Status;
uint32 Remainder;
uint16 *Message;
/*
** Check for invalid table entry
**
** (run time corruption -- data was verified at table load)
*/
if ((NextEntry->MessageIndex >= SCH_MAX_MESSAGES) ||
(NextEntry->Frequency == SCH_UNUSED) ||
(NextEntry->Type != SCH_ACTIVITY_SEND_MSG) ||
(NextEntry->Remainder >= NextEntry->Frequency))
{
SCH_AppData.BadTableDataCount++;
/*
** Too much data for just one event
*/
CFE_EVS_SendEvent(SCH_CORRUPTION_EID, CFE_EVS_ERROR,
"Corrupt data error (1): slot = %d, entry = %d",
SCH_AppData.NextSlotNumber, (int)EntryNumber);
CFE_EVS_SendEvent(SCH_CORRUPTION_EID, CFE_EVS_ERROR,
"Corrupt data error (2): msg = %d, freq = %d, type = %d, rem = %d",
NextEntry->MessageIndex,
NextEntry->Frequency,
NextEntry->Type,
NextEntry->Remainder);
/*
** Disable entry to avoid repeating this error
*/
NextEntry->EnableState = SCH_DISABLED;
CFE_TBL_Modified(SCH_AppData.ScheduleTableHandle);
}
else
{
/*
** Look for entry active on this particular pass through table
*/
Remainder = SCH_AppData.TablePassCount % NextEntry->Frequency;
if (Remainder == NextEntry->Remainder)
{
Message = SCH_AppData.MessageTable[NextEntry->MessageIndex].MessageBuffer;
Status = CFE_SB_SendMsg((CFE_SB_Msg_t *) Message);
/* If additional activity types are added in the future, a switch statement */
/* would be useful, as shown below: */
/* NOTE: The "default" clause should never be able to be executed but is */
/* required by Flight Software Branch Coding Standards */
/*
* switch(NextEntry->Type)
* {
* case SCH_ACTIVITY_SEND_MSG:
* Message = SCH_AppData.MessageTable[NextEntry->MessageIndex].MessageBuffer;
* Status = CFE_SB_SendMsg((CFE_SB_Msg_t *) Message);
* break;
*
* default:
* Status = SCH_UNKNOWN_ACTIVITY;
* break;
* }
*/
if (Status == CFE_SUCCESS)
{
SCH_AppData.ScheduleActivitySuccessCount++;
}
else
{
SCH_AppData.ScheduleActivityFailureCount++;
CFE_EVS_SendEvent(SCH_PACKET_SEND_EID, CFE_EVS_ERROR,
"Activity error: slot = %d, entry = %d, err = 0x%08X",
SCH_AppData.NextSlotNumber, (int)EntryNumber, (unsigned int)Status);
}
}
}
return;
} /* End of SCH_ProcessNextEntry() */
/*******************************************************************
**
** SCH_ProcessCommands
**
** NOTE: For complete prolog information, see above
********************************************************************/
int32 SCH_ProcessCommands(void)
{
int32 Status = CFE_SUCCESS;
while (Status == CFE_SUCCESS)
{
/*
** Process pending Software Bus messages
*/
Status = CFE_SB_RcvMsg(&SCH_AppData.MsgPtr, SCH_AppData.CmdPipe, CFE_SB_POLL);
if (Status == CFE_SUCCESS)
{
Status = SCH_AppPipe(SCH_AppData.MsgPtr);
}
}
if (Status == CFE_SB_NO_MESSAGE)
{
/*
** It's OK to not get a message -- we are polling
*/