-
Notifications
You must be signed in to change notification settings - Fork 23
/
sbn_app.c
1745 lines (1439 loc) · 55.7 KB
/
sbn_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
/******************************************************************************
** \file sbn_app.c
**
** Copyright (c) 2004-2006, United States government as represented by the
** administrator of the National Aeronautics Space Administration.
** All rights reserved. This software(cFE) was created at NASA's Goddard
** Space Flight Center pursuant to government contracts.
**
** This software may be used only pursuant to a United States government
** sponsored project and the United States government may not be charged
** for use thereof.
**
** Purpose:
** This file contains source code for the Software Bus Network
** Application.
**
** Authors: J. Wilmot/GSFC Code582
** R. McGraw/SSI
** C. Knight/ARC Code TI
******************************************************************************/
/*
** Include Files
*/
#include <fcntl.h>
#include "sbn_pack.h"
#include "sbn_app.h"
#include "cfe_sb_events.h" /* For event message IDs */
#include "cfe_es.h" /* PerfLog */
#include "cfe_platform_cfg.h"
#include "cfe_msgids.h"
#include "cfe_version.h"
/** \brief SBN global application data, indexed by AppID. */
SBN_App_t SBN;
#include <string.h>
#include "sbn_app.h"
static SBN_Status_t UnloadNets(void);
static SBN_Status_t UnloadModules(void)
{
SBN_ModuleIdx_t i = 0;
for (i = 0; i < SBN_MAX_MOD_CNT; i++)
{
if (SBN.ProtocolModules[i] == 0)
{
continue; /* this module may have been loaded by ES, so continue in case there are any I loaded. */
} /* end if */
if (OS_ModuleUnload(SBN.ProtocolModules[i]) != OS_SUCCESS)
{
EVSSendCrit(SBN_TBL_EID, "unable to unload protocol module ID %d", i);
return SBN_ERROR;
} /* end if */
} /* end for */
for (i = 0; i < SBN_MAX_MOD_CNT; i++)
{
if (SBN.FilterModules[i] == 0)
{
continue; /* this module may have been loaded by ES, so continue in case there are any I loaded. */
} /* end if */
if (OS_ModuleUnload(SBN.FilterModules[i]) != OS_SUCCESS)
{
EVSSendCrit(SBN_TBL_EID, "unable to unload filter module ID %d", i);
return SBN_ERROR;
} /* end if */
} /* end for */
return SBN_SUCCESS;
} /* end UnloadModules() */
/**
* Packs a CCSDS message with an SBN message header.
*
* \note Ensures the SBN fields (CPU ID, MsgSz) and CCSDS message headers
* are in network (big-endian) byte order.
* \param SBNBuf[out] The buffer to pack into.
* \param MsgSz[in] The size of the payload.
* \param MsgType[in] The SBN message type.
* \param ProcessorID[in] The ProcessorID of the sender (should be CFE_CPU_ID)
* \param SpacecraftID[in] The SpacecraftID of the sender
* \param Msg[in] The payload (CCSDS message or SBN sub/unsub.)
*/
void SBN_PackMsg(void *SBNBuf, SBN_MsgSz_t MsgSz, SBN_MsgType_t MsgType, CFE_ProcessorID_t ProcessorID, CFE_ProcessorID_t SpacecraftID,void *Msg)
{
Pack_t Pack;
Pack_Init(&Pack, SBNBuf, MsgSz + SBN_PACKED_HDR_SZ, false);
Pack_Int16(&Pack, MsgSz);
Pack_UInt8(&Pack, MsgType);
Pack_UInt32(&Pack, ProcessorID);
Pack_UInt32(&Pack, SpacecraftID);
if (!Msg || !MsgSz)
{
/* valid to have a NULL pointer/empty size payload */
return;
} /* end if */
Pack_Data(&Pack, Msg, MsgSz);
} /* end SBN_PackMsg */
/**
* Unpacks a CCSDS message with an SBN message header.
*
* \param SBNBuf[in] The buffer to unpack.
* \param MsgTypePtr[out] The SBN message type.
* \param MsgSzPtr[out] The payload size.
* \param ProcessorID[out] The ProcessorID of the sender.
* \param Msg[out] The payload (a CCSDS message, or SBN sub/unsub).
* \return true if we were able to unpack the message.
*
* \note Ensures the SBN fields (CPU ID, MsgSz) and CCSDS message headers
* are in platform byte order.
* \todo Use a type for SBNBuf.
*/
bool SBN_UnpackMsg(void *SBNBuf, SBN_MsgSz_t *MsgSzPtr, SBN_MsgType_t *MsgTypePtr,
CFE_ProcessorID_t *ProcessorIDPtr, CFE_SpacecraftID_t *SpacecraftIDPtr,
void *Msg)
{
uint8 t = 0;
Pack_t Pack;
Pack_Init(&Pack, SBNBuf, SBN_MAX_PACKED_MSG_SZ, false);
Unpack_Int16(&Pack, MsgSzPtr);
Unpack_UInt8(&Pack, &t);
*MsgTypePtr = t;
Unpack_UInt32(&Pack, ProcessorIDPtr);
Unpack_UInt32(&Pack, SpacecraftIDPtr);
if (!*MsgSzPtr)
{
return true;
} /* end if */
if (*MsgSzPtr < 0 || *MsgSzPtr > CFE_MISSION_SB_MAX_SB_MSG_SIZE)
{
return false;
} /* end if */
Unpack_Data(&Pack, Msg, *MsgSzPtr);
return true;
} /* end SBN_UnpackMsg */
/**
* Called by a protocol module to signal that a peer has been connected.
*
* @param Peer[in] The peer that has been connected.
* @return SBN_SUCCESS or SBN_ERROR if there was an issue.
*/
SBN_Status_t SBN_Connected(SBN_PeerInterface_t *Peer)
{
static const char FAIL_PREFIX[] = "ERROR: could not disconnect peer:";
SBN_Status_t SBN_Status = SBN_SUCCESS;
CFE_Status_t CFE_Status;
if (Peer->Connected != 0)
{
EVSSendErr(SBN_PEER_EID, "%s peer %d:%d already connected", FAIL_PREFIX, Peer->SpacecraftID, (int)(Peer->ProcessorID));
return SBN_ERROR;
} /* end if */
char PipeName[OS_MAX_API_NAME];
/* create a pipe name string similar to SBN_0_Pipe */
snprintf(PipeName, OS_MAX_API_NAME, "SBN_%d_%d_Pipe", (int)(Peer->ProcessorID), (int)(Peer->SpacecraftID));
CFE_Status = CFE_SB_CreatePipe(&(Peer->Pipe), SBN_PEER_PIPE_DEPTH, PipeName);
if (CFE_Status != CFE_SUCCESS)
{
EVSSendErr(SBN_PEER_EID, "%s: could not create peer pipe '%s'", FAIL_PREFIX, PipeName);
return SBN_ERROR;
} /* end if */
EVSSendInfo(SBN_PEER_EID, "Created peer pipe '%s'", PipeName);
CFE_Status = CFE_SB_SetPipeOpts(Peer->Pipe, CFE_SB_PIPEOPTS_IGNOREMINE);
if (CFE_Status != CFE_SUCCESS)
{
EVSSendErr(SBN_PEER_EID, "%s: could not set pipe options '%s'", FAIL_PREFIX, PipeName);
return SBN_ERROR;
} /* end if */
EVSSendInfo(SBN_PEER_EID, "Peer %d:%d connected.", Peer->SpacecraftID, (int)(Peer->ProcessorID));
uint8 ProtocolVer = SBN_PROTO_VER;
SBN_Status = SBN_SendNetMsg(SBN_PROTO_MSG, sizeof(ProtocolVer), &ProtocolVer, Peer);
if (SBN_Status != SBN_SUCCESS)
{
return SBN_Status;
} /* end if */
/* set this to current time so we don't think we've already timed out */
OS_GetLocalTime(&Peer->LastRecv);
SBN_Status = SBN_SendLocalSubsToPeer(Peer);
Peer->Connected = 1;
return SBN_Status;
} /* end SBN_Connected() */
/**
* Called by a protocol module to signal that a peer has been disconnected.
*
* @param Peer[in] The peer that has been disconnected.
* @return SBN_SUCCESS or SBN_ERROR if there was an issue.
*/
SBN_Status_t SBN_Disconnected(SBN_PeerInterface_t *Peer)
{
static const char FAIL_PREFIX[] = "ERROR: could not disconnect peer:";
CFE_Status_t Status;
if (Peer->Connected == 0)
{
EVSSendErr(SBN_PEER_EID, "%s already not connected to peer %d:%d", FAIL_PREFIX, Peer->SpacecraftID, (int)(Peer->ProcessorID));
return SBN_ERROR;
}
Peer->Connected = 0; /**< mark as disconnected before deleting pipe */
if((Status = CFE_SB_DeletePipe(Peer->Pipe)) != CFE_SUCCESS) {
EVSSendErr(SBN_PEER_EID, "%s could not delete pipe when disconnecting peer %d:%d: 0x%08x",
FAIL_PREFIX,
Peer->SpacecraftID,
Peer->ProcessorID,
Status);
}
Peer->Pipe = 0;
Peer->SendCnt = 0;
Peer->RecvCnt = 0;
Peer->SendErrCnt = 0;
Peer->RecvErrCnt = 0;
Peer->SubCnt = 0; /* reset sub count, in case this is a reconnection */
EVSSendInfo(SBN_PEER_EID, "Disconnected from peer %d:%d.", Peer->SpacecraftID, (int)(Peer->ProcessorID));
return SBN_SUCCESS;
} /* end SBN_Disconnected() */
/* Use a struct for all local variables in the task so we can specify exactly
* how large of a stack we need for the task.
*/
typedef struct
{
SBN_Status_t Status;
OS_TaskID_t RecvTaskID;
SBN_PeerIdx_t PeerIdx;
SBN_NetIdx_t NetIdx;
SBN_PeerInterface_t *Peer;
SBN_NetInterface_t * Net;
CFE_ProcessorID_t ProcessorID;
CFE_SpacecraftID_t SpacecraftID;
SBN_MsgType_t MsgType;
SBN_MsgSz_t MsgSz;
uint8 Msg[CFE_MISSION_SB_MAX_SB_MSG_SIZE];
} RecvPeerTaskData_t;
/**
* \brief Receive task created for each direct peer-based connection.
* Spanwed from PeerPoll()
*/
void SBN_RecvPeerTask(void)
{
RecvPeerTaskData_t D;
memset(&D, 0, sizeof(D));
D.RecvTaskID = OS_TaskGetId();
for (D.NetIdx = 0; D.NetIdx < SBN.NetCnt; D.NetIdx++)
{
D.Net = &SBN.Nets[D.NetIdx];
if (!D.Net->Configured)
{
continue;
}
for (D.PeerIdx = 0; D.PeerIdx < D.Net->PeerCnt; D.PeerIdx++)
{
D.Peer = &D.Net->Peers[D.PeerIdx];
if (D.Peer->RecvTaskID == D.RecvTaskID)
{
break;
} /* end if */
} /* end for */
if (D.PeerIdx < D.Net->PeerCnt)
{
break;
} /* end if */
} /* end for */
if (D.NetIdx == SBN.NetCnt)
{
EVSSendErr(SBN_PEERTASK_EID, "unable to connect task to peer struct");
return;
} /* end if */
while (1)
{
D.Status = D.Net->IfOps->RecvFromPeer(D.Net, D.Peer, &D.MsgType, &D.MsgSz, &D.ProcessorID, &D.SpacecraftID, &D.Msg);
if (D.Status == SBN_IF_EMPTY)
{
continue; /* no (more) messages */
} /* end if */
if (D.Status == SBN_SUCCESS)
{
OS_GetLocalTime(&D.Peer->LastRecv);
D.Status = SBN_ProcessNetMsg(D.Net, D.MsgType, D.ProcessorID, D.SpacecraftID, D.MsgSz, &D.Msg);
if (D.Status != SBN_SUCCESS)
{
D.Peer->RecvTaskID = 0;
return;
} /* end if */
}
else
{
EVSSendErr(SBN_PEER_EID, "recv error (%d)", D.Status);
D.Peer->RecvErrCnt++;
D.Peer->RecvTaskID = 0;
return;
} /* end if */
} /* end while */
} /* end SBN_RecvPeerTask() */
typedef struct RecvNetTaskData_s
{
SBN_NetIdx_t NetIdx;
SBN_NetInterface_t * Net;
SBN_PeerInterface_t *Peer;
SBN_Status_t Status;
OS_TaskID_t RecvTaskID;
CFE_ProcessorID_t ProcessorID;
CFE_SpacecraftID_t SpacecraftID;
SBN_MsgType_t MsgType;
SBN_MsgSz_t MsgSz;
uint8 Msg[CFE_MISSION_SB_MAX_SB_MSG_SIZE];
} RecvNetTaskData_t;
/**
* \brief Receive task created for each net-based connection.
* Spanwed from PeerPoll()
*/
void SBN_RecvNetTask(void)
{
static const char FAIL_PREFIX_STARTUP[] = "ERROR: could not start SBN Receive Net Task:";
static const char FAIL_PREFIX_RUNNING[] = "ERROR: during SBN Receive Net Task:";
RecvNetTaskData_t D;
memset(&D, 0, sizeof(D));
D.RecvTaskID = OS_TaskGetId();
for (D.NetIdx = 0; D.NetIdx < SBN.NetCnt; D.NetIdx++)
{
D.Net = &SBN.Nets[D.NetIdx];
if (D.Net->RecvTaskID == D.RecvTaskID)
{
break;
} /* end if */
} /* end for */
if (D.NetIdx == SBN.NetCnt)
{
EVSSendErr(SBN_PEERTASK_EID, "%s unable to connect task to net struct", FAIL_PREFIX_STARTUP);
return;
} /* end if */
while (1)
{
EVSSendDbg(SBN_PEERTASK_EID,"Try to receive from net...");
D.Status = D.Net->IfOps->RecvFromNet(D.Net, &D.MsgType, &D.MsgSz, &D.ProcessorID, &D.SpacecraftID, &D.Msg);
if (D.Status == SBN_IF_EMPTY)
{
continue; /* no (more) messages */
} /* end if */
if (D.Status != SBN_SUCCESS)
{
EVSSendErr(SBN_PEERTASK_EID, "%s RecvFromNet failed for net %d: status=0x%08X", FAIL_PREFIX_RUNNING, D.NetIdx, D.Status);
break;
} /* end if */
D.Peer = SBN_GetPeer(D.Net, D.ProcessorID, D.SpacecraftID);
if (!D.Peer)
{
EVSSendErr(SBN_PEERTASK_EID, "unknown peer (ProcessorID=%d)", (int)D.ProcessorID);
break;
} /* end if */
OS_GetLocalTime(&D.Peer->LastRecv);
D.Status = SBN_ProcessNetMsg(D.Net, D.MsgType, D.ProcessorID, D.SpacecraftID, D.MsgSz, &D.Msg);
if (D.Status != SBN_SUCCESS)
{
EVSSendErr(SBN_PEERTASK_EID, "SBN_ProcessNetMsg failed: 0x%08X", D.Status);
break;
} /* end if */
} /* end while */
/* Unset the task id so that it can be created if necessary */
D.Net->RecvTaskID = 0;
} /* end SBN_RecvNetTask() */
/**
* Checks all interfaces for messages from peers.
* Receive messages from the specified peer, injecting them onto the local
* software bus.
*/
SBN_Status_t SBN_RecvNetMsgs(void)
{
SBN_Status_t SBN_Status = 0;
SBN_NetIdx_t NetIdx = 0;
for (NetIdx = 0; NetIdx < SBN.NetCnt; NetIdx++)
{
SBN_NetInterface_t *Net = &SBN.Nets[NetIdx];
SBN_MsgType_t MsgType;
SBN_MsgSz_t MsgSz;
CFE_ProcessorID_t ProcessorID;
CFE_SpacecraftID_t SpacecraftID;
if (Net->TaskFlags & SBN_TASK_RECV)
{
continue; /* separate task handles receiving from a net */
} /* end if */
if (Net->IfOps->RecvFromNet)
{
int MsgCnt = 0;
// TODO: make configurable
for (MsgCnt = 0; MsgCnt < 100; MsgCnt++) /* read at most 100 messages from the net */
{
/*memset(SBN.MsgBuffer, 0, sizeof(SBN.MsgBuffer));*/
SBN_Status = Net->IfOps->RecvFromNet(Net, &MsgType, &MsgSz, &ProcessorID, &SpacecraftID, SBN.MsgBuffer);
if (SBN_Status == SBN_IF_EMPTY)
{
break; /* no (more) messages for this net, continue to next net */
} /* end if */
/* for UDP, the message received may not be from the peer
* expected.
*/
SBN_PeerInterface_t *Peer = SBN_GetPeer(Net, ProcessorID, SpacecraftID);
if (!Peer)
{
EVSSendInfo(SBN_PEERTASK_EID, "unknown peer (ProcessorID=%d)", (int)ProcessorID);
/* may be a misconfiguration on my part...? continue processing msgs... */
continue;
} /* end if */
OS_GetLocalTime(&Peer->LastRecv);
SBN_ProcessNetMsg(Net, MsgType, ProcessorID, SpacecraftID, MsgSz, SBN.MsgBuffer); /* ignore errors */
} /* end for */
}
else if (Net->IfOps->RecvFromPeer)
{
SBN_PeerIdx_t PeerIdx = 0;
for (PeerIdx = 0; PeerIdx < Net->PeerCnt; PeerIdx++)
{
SBN_PeerInterface_t *Peer = &Net->Peers[PeerIdx];
int MsgCnt = 0;
// TODO: make configurable
for (MsgCnt = 0; MsgCnt < 100; MsgCnt++) /* read at most 100 messages from peer */
{
CFE_ProcessorID_t ProcessorID = 0;
CFE_SpacecraftID_t SpacecraftID = 0;
SBN_MsgType_t MsgType = 0;
SBN_MsgSz_t MsgSz = 0;
memset(SBN.MsgBuffer, 0, sizeof(SBN.MsgBuffer));
SBN_Status = Net->IfOps->RecvFromPeer(Net, Peer, &MsgType, &MsgSz, &ProcessorID, &SpacecraftID, SBN.MsgBuffer);
if (SBN_Status == SBN_IF_EMPTY)
{
break; /* no (more) messages for this peer, continue to next peer */
} /* end if */
OS_GetLocalTime(&Peer->LastRecv);
SBN_Status = SBN_ProcessNetMsg(Net, MsgType, ProcessorID, SpacecraftID, MsgSz, SBN.MsgBuffer);
if (SBN_Status != SBN_SUCCESS)
{
break; /* continue to next peer */
} /* end if */
} /* end for */
} /* end for */
}
else
{
EVSSendErr(SBN_PEER_EID, "neither RecvFromPeer nor RecvFromNet defined for net #%d", (int)NetIdx);
/* meanwhile, continue to next net... */
} /* end if */
} /* end for */
return SBN_SUCCESS;
} /* end SBN_RecvNetMsgs */
/**
* Sends a message to a peer using the module's send API.
*
* @param MsgType SBN type of the message
* @param MsgSz Size of the message
* @param Msg Message to send
* @param Peer The peer to send the message to.
* @return Number of characters sent on success, -1 on error.
*
*/
SBN_Status_t SBN_SendNetMsg(SBN_MsgType_t MsgType, SBN_MsgSz_t MsgSz, void *Msg, SBN_PeerInterface_t *Peer)
{
SBN_NetInterface_t *Net = Peer->Net;
SBN_Status_t SBN_Status = SBN_SUCCESS;
if (Peer->SendTaskID)
{
if (OS_MutSemTake(SBN.SendMutex) != OS_SUCCESS)
{
EVSSendErr(SBN_PEER_EID, "unable to take send mutex");
return SBN_ERROR;
} /* end if */
} /* end if */
SBN_Status = Net->IfOps->Send(Peer, MsgType, MsgSz, Msg);
if (SBN_Status != SBN_SUCCESS)
{
Peer->SendErrCnt++;
} else {
Peer->SendCnt++;
} /* end if */
/* for clients that need a poll or heartbeat, update time even when failing */
OS_GetLocalTime(&Peer->LastSend);
if (Peer->SendTaskID)
{
if (OS_MutSemGive(SBN.SendMutex) != OS_SUCCESS)
{
EVSSendErr(SBN_PEER_EID, "unable to give send mutex");
return SBN_ERROR;
} /* end if */
} /* end if */
return SBN_Status;
} /* end SBN_SendNetMsg */
typedef struct
{
SBN_Status_t Status;
SBN_NetIdx_t NetIdx;
SBN_PeerIdx_t PeerIdx;
OS_TaskID_t SendTaskID;
CFE_MSG_Message_t *MsgPtr;
CFE_SB_MsgId_t MsgID;
SBN_NetInterface_t *Net;
SBN_PeerInterface_t *Peer;
} SendTaskData_t;
/**
* \brief When a peer is connected, a task is created to listen to the relevant
* pipe for messages to send to that peer.
*/
void SBN_SendTask(void)
{
SendTaskData_t D;
SBN_Filter_Ctx_t Filter_Context;
CFE_MSG_Size_t MsgSz = 0;
Filter_Context.MyProcessorID = CFE_PSP_GetProcessorId();
Filter_Context.MySpacecraftID = CFE_PSP_GetSpacecraftId();
memset(&D, 0, sizeof(D));
D.SendTaskID = OS_TaskGetId();
for (D.NetIdx = 0; D.NetIdx < SBN.NetCnt; D.NetIdx++)
{
D.Net = &SBN.Nets[D.NetIdx];
for (D.PeerIdx = 0; D.PeerIdx < D.Net->PeerCnt; D.PeerIdx++)
{
D.Peer = &D.Net->Peers[D.PeerIdx];
if (D.Peer->SendTaskID == D.SendTaskID)
{
break;
} /* end if */
} /* end for */
if (D.PeerIdx < D.Net->PeerCnt)
{
break; /* found a ringer */
} /* end if */
} /* end for */
if (D.NetIdx == SBN.NetCnt)
{
EVSSendErr(SBN_PEER_EID, "error connecting send task");
return;
} /* end if */
while (1)
{
SBN_ModuleIdx_t FilterIdx = 0;
if (!D.Peer->Connected)
{
OS_TaskDelay(SBN_MAIN_LOOP_DELAY);
continue;
} /* end if */
if (CFE_SB_ReceiveBuffer((CFE_SB_Buffer_t **)&D.MsgPtr, D.Peer->Pipe, CFE_SB_PEND_FOREVER) != CFE_SUCCESS)
{
break;
} /* end if */
Filter_Context.PeerProcessorID = D.Peer->ProcessorID;
Filter_Context.PeerSpacecraftID = D.Peer->SpacecraftID;
for (FilterIdx = 0; FilterIdx < D.Peer->FilterCnt; FilterIdx++)
{
if (D.Peer->Filters[FilterIdx]->FilterSend == NULL)
{
continue;
} /* end if */
D.Status = (D.Peer->Filters[FilterIdx]->FilterSend)(D.MsgPtr, &Filter_Context);
if (D.Status == SBN_IF_EMPTY) /* filter requests not sending this msg, see below for loop */
{
break;
} /* end if */
if (D.Status != SBN_SUCCESS)
{
break;
} /* end if */
} /* end for */
if (FilterIdx < D.Peer->FilterCnt)
{
/* one of the above filters suggested rejecting this message */
continue;
} /* end if */
if(CFE_MSG_GetSize(D.MsgPtr, &MsgSz) != CFE_SUCCESS)
{
continue;
} /* end if */
D.Status = SBN_SendNetMsg(SBN_APP_MSG, MsgSz, D.MsgPtr, D.Peer);
if (D.Status == SBN_ERROR)
{
break;
} /* end if */
} /* end while */
/* mark peer as not having a task so that sending will create a new one */
D.Peer->SendTaskID = 0;
} /* end SBN_SendTask() */
/**
* Iterate through all peers, examining the pipe to see if there are messages
* I need to send to that peer.
*/
static SBN_Status_t CheckPeerPipes(void)
{
CFE_Status_t CFE_Status;
int ReceivedFlag = 0, iter = 0;
CFE_MSG_Message_t *MsgPtr = NULL;
CFE_MSG_Size_t MsgSz = 0;
SBN_Filter_Ctx_t Filter_Context;
Filter_Context.MyProcessorID = CFE_PSP_GetProcessorId();
Filter_Context.MySpacecraftID = CFE_PSP_GetSpacecraftId();
/**
* \note This processes one message per peer, then start again until no
* peers have pending messages. At max only process SBN_MAX_MSG_PER_WAKEUP
* per peer per wakeup otherwise I will starve other processing.
*/
for (iter = 0; iter < SBN_MAX_MSG_PER_WAKEUP; iter++)
{
ReceivedFlag = 0;
SBN_NetIdx_t NetIdx = 0;
for (NetIdx = 0; NetIdx < SBN.NetCnt; NetIdx++)
{
SBN_NetInterface_t *Net = &SBN.Nets[NetIdx];
SBN_PeerIdx_t PeerIdx = 0;
for (PeerIdx = 0; PeerIdx < Net->PeerCnt; PeerIdx++)
{
SBN_ModuleIdx_t FilterIdx = 0;
SBN_PeerInterface_t *Peer = &Net->Peers[PeerIdx];
// Poll peer here to detect disconnections and to reconnect
if(Net->IfOps->PollPeer(Peer) != SBN_SUCCESS) {
EVSSendErr(SBN_PEERTASK_EID, "failed to poll peer %d:%d", Peer->SpacecraftID, Peer->ProcessorID);
}
if (Peer->Connected == 0)
{
EVSSendDbg(SBN_PEERTASK_EID, "not connected to peer %d:%d", Peer->SpacecraftID, Peer->ProcessorID);
continue;
} /* end if */
EVSSendDbg(SBN_PEERTASK_EID, "SBN connected to peer %d:%d", Peer->SpacecraftID, Peer->ProcessorID);
if (Peer->TaskFlags & SBN_TASK_SEND)
{
if (!Peer->SendTaskID)
{
/* TODO: logic/controls to prevent hammering? */
char SendTaskName[32];
snprintf(SendTaskName, 32, "sendT_%d_%d_%d", (int)NetIdx, (int)(Peer->ProcessorID), (int)(Peer->SpacecraftID));
CFE_Status = CFE_ES_CreateChildTask(
&(Peer->SendTaskID), SendTaskName, (CFE_ES_ChildTaskMainFuncPtr_t)&SBN_SendTask, NULL,
CFE_PLATFORM_ES_DEFAULT_STACK_SIZE + 2 * sizeof(SendTaskData_t), 0, 0);
if (CFE_Status != CFE_SUCCESS)
{
EVSSendErr(SBN_PEER_EID, "error creating send task for peer %d:%d", Peer->SpacecraftID, Peer->ProcessorID);
return SBN_ERROR;
} /* end if */
} /* end if */
continue;
} /* end if */
/* if peer data is not in use, go to next peer */
if (CFE_SB_ReceiveBuffer((CFE_SB_Buffer_t **)&MsgPtr, Peer->Pipe, CFE_SB_POLL) != CFE_SUCCESS)
{
continue;
} /* end if */
ReceivedFlag = 1;
Filter_Context.PeerProcessorID = Peer->ProcessorID;
Filter_Context.PeerSpacecraftID = Peer->SpacecraftID;
for (FilterIdx = 0; FilterIdx < Peer->FilterCnt; FilterIdx++)
{
SBN_Status_t SBN_Status;
if (Peer->Filters[FilterIdx]->FilterSend == NULL)
{
continue;
} /* end if */
SBN_Status = (Peer->Filters[FilterIdx]->FilterSend)(MsgPtr, &Filter_Context);
if (SBN_Status == SBN_IF_EMPTY) /* filter requests not sending this msg, see below for loop */
{
break;
} /* end if */
if (SBN_Status != SBN_SUCCESS)
{
/* something fatal happened, exit */
return SBN_Status;
} /* end if */
} /* end for */
if (FilterIdx < Peer->FilterCnt)
{
/* one of the above filters suggested rejecting this message */
continue;
} /* end if */
if (CFE_MSG_GetSize(MsgPtr, &MsgSz) != CFE_SUCCESS)
{
continue;
} /* end if */
SBN_SendNetMsg(SBN_APP_MSG, MsgSz, MsgPtr, Peer);
} /* end for */
} /* end for */
if (!ReceivedFlag)
{
break;
} /* end if */
} /* end for */
return SBN_SUCCESS;
} /* end CheckPeerPipes */
/**
* Iterate through all nets and create receive tasks if they do not yet exist.
*/
static SBN_Status_t PeerPoll(void)
{
CFE_Status_t CFE_Status;
SBN_NetIdx_t NetIdx = 0;
for (NetIdx = 0; NetIdx < SBN.NetCnt; NetIdx++)
{
SBN_NetInterface_t *Net = &SBN.Nets[NetIdx];
if (Net->IfOps->RecvFromNet && Net->TaskFlags & SBN_TASK_RECV)
{
if (!Net->RecvTaskID)
{
EVSSendInfo(SBN_PEER_EID, "Creating recv task for net %d", (int)NetIdx);
/* TODO: add logic/controls to prevent hammering */
char RecvTaskName[32];
snprintf(RecvTaskName, OS_MAX_API_NAME, "sbn_rs_%d", (int)NetIdx);
CFE_Status = CFE_ES_CreateChildTask(
&(Net->RecvTaskID), RecvTaskName, (CFE_ES_ChildTaskMainFuncPtr_t)&SBN_RecvNetTask, NULL,
CFE_PLATFORM_ES_DEFAULT_STACK_SIZE + 2 * sizeof(RecvNetTaskData_t), 0, 0);
if (CFE_Status != CFE_SUCCESS)
{
EVSSendErr(SBN_PEER_EID, "error creating task for net %d",(int)NetIdx);
return SBN_ERROR;
} /* end if */
} /* end if */
}
else
{
SBN_PeerIdx_t PeerIdx = 0;
for (PeerIdx = 0; PeerIdx < Net->PeerCnt; PeerIdx++)
{
SBN_PeerInterface_t *Peer = &Net->Peers[PeerIdx];
if (Net->IfOps->RecvFromPeer && Peer->TaskFlags & SBN_TASK_RECV)
{
if (!Peer->RecvTaskID)
{
/* TODO: add logic/controls to prevent hammering */
char RecvTaskName[32];
snprintf(RecvTaskName, OS_MAX_API_NAME, "sbn_recv_%d", (int)PeerIdx);
CFE_Status = CFE_ES_CreateChildTask(
&(Peer->RecvTaskID), RecvTaskName, (CFE_ES_ChildTaskMainFuncPtr_t)&SBN_RecvPeerTask, NULL,
CFE_PLATFORM_ES_DEFAULT_STACK_SIZE + 2 * sizeof(RecvPeerTaskData_t), 0, 0);
/* TODO: more accurate stack size required */
if (CFE_Status != CFE_SUCCESS)
{
EVSSendErr(SBN_PEER_EID, "error creating task for %d", (int)(Peer->ProcessorID));
return SBN_ERROR;
} /* end if */
} /* end if */
}
else
{
Net->IfOps->PollPeer(Peer);
} /* end if */
} /* end for */
} /* end if */
} /* end for */
return SBN_SUCCESS;
} /* end PeerPoll */
/**
* Loops through all hosts and peers, initializing all.
*
* @return SBN_SUCCESS if interface is initialized successfully
* SBN_ERROR otherwise
*/
static SBN_Status_t InitInterfaces(void)
{
if (SBN.NetCnt < 1)
{
EVSSendErr(SBN_PEER_EID, "no networks configured");
return SBN_ERROR;
} /* end if */
SBN_NetIdx_t NetIdx = 0;
for (NetIdx = 0; NetIdx < SBN.NetCnt; NetIdx++)
{
EVSSendInfo(SBN_PEER_EID, "initializing net: %d", (int)NetIdx);
SBN_NetInterface_t *Net = &SBN.Nets[NetIdx];
if (!Net->Configured)
{
EVSSendErr(SBN_PEER_EID, "network #%d not configured", NetIdx);
return SBN_ERROR;
} /* end if */
Net->IfOps->InitNet(Net);
SBN_PeerIdx_t PeerIdx = 0;
EVSSendInfo(SBN_PEER_EID, "Net %d has %d peers", NetIdx, Net->PeerCnt);
for (PeerIdx = 0; PeerIdx < Net->PeerCnt; PeerIdx++)
{
SBN_PeerInterface_t *Peer = &Net->Peers[PeerIdx];
EVSSendInfo(SBN_PEER_EID, "initializing net: %d peer: %d: sc: %d cpu: %d", (int)NetIdx, (int)PeerIdx, Peer->SpacecraftID, Peer->ProcessorID);
Net->IfOps->InitPeer(Peer);
} /* end for */
} /* end for */
EVSSendInfo(SBN_INIT_EID, "configured, %d nets", SBN.NetCnt);
for (NetIdx = 0; NetIdx < SBN.NetCnt; NetIdx++)
{
EVSSendInfo(SBN_INIT_EID, "net %d has %d peers", NetIdx, SBN.Nets[NetIdx].PeerCnt);
} /* end for */
return SBN_SUCCESS;
} /* end InitInterfaces */
/**
* This function waits for the scheduler (SCH) to wake this code up, so that
* nothing transpires until the cFE is fully operational.
*
* @param[in] iTimeOut The time to wait for the scheduler to notify this code.
* @return CFE_SUCCESS on success, otherwise an error value.
*/
static SBN_Status_t WaitForWakeup(int32 iTimeOut)
{
CFE_Status_t CFE_Status = CFE_SUCCESS;
SBN_Status_t SBN_Status = SBN_SUCCESS;
CFE_MSG_Message_t *MsgPtr = 0;
/* Wait for WakeUp messages from scheduler */
CFE_Status = CFE_SB_ReceiveBuffer((CFE_SB_Buffer_t **)&MsgPtr, SBN.CmdPipe, iTimeOut);
switch (CFE_Status)
{
case CFE_SB_NO_MESSAGE:
case CFE_SB_TIME_OUT:
break;
case CFE_SUCCESS:
SBN_HandleCommand(MsgPtr);
break;
default:
return SBN_ERROR;
} /* end switch */
/* For sbn, we still want to perform cyclic processing
** if the WaitForWakeup time out
** cyclic processing at timeout rate
*/
CFE_ES_PerfLogEntry(SBN_PERF_RECV_ID);
SBN_RecvNetMsgs();
SBN_Status = SBN_CheckSubscriptionPipe();
switch(SBN_Status) {
case SBN_IF_EMPTY:
break;
case SBN_ERROR:
EVSSendErr(SBN_PEER_EID, "SBN_CheckSubscriptionPipe failed.");
break;
default:
/* no error */
break;
};
CheckPeerPipes();
PeerPoll();
CFE_ES_PerfLogExit(SBN_PERF_RECV_ID);
return SBN_SUCCESS;
} /* end WaitForWakeup */
/**
* Load Protocol or Filter Module from the table
*
* Cleaned up by UnloadModules()
*/
static cpuaddr LoadConf_Module(SBN_Module_Entry_t *e, CFE_ES_ModuleID_t *ModuleIDPtr)
{
cpuaddr StructAddr;
EVSSendInfo(SBN_TBL_EID, "checking if module (%s) already loded", e->Name);
if (OS_SymbolLookup(&StructAddr, e->LibSymbol) != OS_SUCCESS) /* try loading it if it's not already loaded */
{
EVSSendInfo(SBN_TBL_EID, "symbol not yet loaded (%s)", e->LibSymbol);
if (e->LibFileName[0] == '\0')