-
Notifications
You must be signed in to change notification settings - Fork 204
/
cfe_sb_api.c
2335 lines (2008 loc) · 79 KB
/
cfe_sb_api.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
/*
** GSC-18128-1, "Core Flight Executive Version 6.7"
**
** Copyright (c) 2006-2019 United States Government as represented by
** the Administrator of the National Aeronautics and Space Administration.
** All Rights Reserved.
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/
/******************************************************************************
** File: cfe_sb_api.c
**
** Purpose:
** This file contains the source code for the SB API's.
**
** Notes: The following 4 terms have been, or are used in the cFS architecture and implementation
**
** StreamId - First 16 bits of CCSDS Space Packet Protocol (SPP) 133.0-B.1c2 Blue Book
** packet primary header. It contains the 3 bit Version Number, 1 bit Packet Type ID,
** 1 bit Secondary Header flag, and 11 bit Application Process ID
** It was used in earlier cFS implementaions and is defined here for historical reference
** It is NOT exposed to user applications.
**
** MsgId - Unique numeric message identifier within a mission namespace. It is used by cFS
** applications to the identify messages for publishing and subscribing
** It is used by the SB API and encoded in a mission defended way in the header of
** all cFS messages.
** It is exposed to all cFS applications
**
** ApId - CCSDS Application Process Id field in the primary header.
** It has default bit mask of 0x07FF and is part of the cFS message Id
** It should not be confused with the cFE Executive Services (ES) term appId which
** identifies the software application/component
** It is NOT exposed to user applications.
**
** MsgIdkey - This is a unique numeric key within a mission namespace that is used with
** cFS software bus internal structures.
** It is algorithmically created in a mission defined way from the MsgId to support
** efficient lookup and mapping implementations
** It is NOT exposed to user applications.
**
** Author: R.McGraw/SSI
** J.Wilmot/NASA
**
******************************************************************************/
/*
** Include Files
*/
#include "common_types.h"
#include "private/cfe_private.h"
#include "cfe_sb_events.h"
#include "cfe_sb_priv.h"
#include "cfe_sb.h"
#include "osapi.h"
#include "cfe_es.h"
#include "cfe_psp.h"
#include "cfe_error.h"
#include <string.h>
#include "private/cfe_core_resourceid_basevalues.h"
/*
* Macro to reflect size of PipeDepthStats Telemetry array -
* this may or may not be the same as CFE_SB_MSG_MAX_PIPES
*/
#define CFE_SB_TLM_PIPEDEPTHSTATS_SIZE (sizeof(CFE_SB_Global.StatTlmMsg.Payload.PipeDepthStats) / sizeof(CFE_SB_Global.StatTlmMsg.Payload.PipeDepthStats[0]))
/* Local structure for remove pipe callbacks */
typedef struct
{
const char *FullName; /* Full name (app.task) for error reporting */
CFE_SB_PipeId_t PipeId; /* Pipe id to remove */
} CFE_SB_RemovePipeCallback_t;
/*
* Function: CFE_SB_PipeId_ToIndex - See API and header file for details
*/
CFE_Status_t CFE_SB_PipeId_ToIndex(CFE_SB_PipeId_t PipeID, uint32 *Idx)
{
return CFE_ResourceId_ToIndex(CFE_RESOURCEID_UNWRAP(PipeID),
CFE_SB_PIPEID_BASE,
CFE_PLATFORM_SB_MAX_PIPES,
Idx);
}
/*
* Function: CFE_SB_CreatePipe - See API and header file for details
*/
int32 CFE_SB_CreatePipe(CFE_SB_PipeId_t *PipeIdPtr, uint16 Depth, const char *PipeName)
{
CFE_ES_AppId_t AppId;
CFE_ES_TaskId_t TskId;
osal_id_t SysQueueId;
int32 Status;
CFE_SB_PipeD_t *PipeDscPtr;
CFE_ResourceId_t PendingPipeId;
uint16 PendingEventId;
char FullName[(OS_MAX_API_NAME * 2)];
Status = CFE_SUCCESS;
SysQueueId = OS_OBJECT_ID_UNDEFINED;
PendingEventId = 0;
PipeDscPtr = NULL;
/*
* Get caller AppId.
*
* This is needed for both success and failure cases,
* as it is stored in the Pipe Descriptor on success,
* and used for events on failure, so get it now.
*/
CFE_ES_GetAppID(&AppId);
/* get callers TaskId */
CFE_ES_GetTaskID(&TskId);
/* check input parameters */
if((PipeIdPtr == NULL) || (Depth > OS_QUEUE_MAX_DEPTH) || (Depth == 0))
{
PendingEventId = CFE_SB_CR_PIPE_BAD_ARG_EID;
Status = CFE_SB_BAD_ARGUMENT;
}
else
{
/* Get an available Pipe Descriptor which must be done while locked */
CFE_SB_LockSharedData(__func__,__LINE__);
/* get first available entry in pipe table */
PendingPipeId = CFE_ResourceId_FindNext(CFE_SB_Global.LastPipeId, CFE_PLATFORM_SB_MAX_PIPES, CFE_SB_CheckPipeDescSlotUsed);
PipeDscPtr = CFE_SB_LocatePipeDescByID(CFE_SB_PIPEID_C(PendingPipeId));
/* if pipe table is full, send event and return error */
if(PipeDscPtr == NULL)
{
PendingEventId = CFE_SB_MAX_PIPES_MET_EID;
Status = CFE_SB_MAX_PIPES_MET;
}
else
{
/* Fully clear the entry, just in case of stale data */
memset(PipeDscPtr, 0, sizeof(*PipeDscPtr));
CFE_SB_PipeDescSetUsed(PipeDscPtr, CFE_RESOURCEID_RESERVED);
CFE_SB_Global.LastPipeId = PendingPipeId;
}
CFE_SB_UnlockSharedData(__func__,__LINE__);
}
if (Status == CFE_SUCCESS)
{
/* create the queue */
Status = OS_QueueCreate(&SysQueueId,PipeName,Depth,sizeof(CFE_SB_BufferD_t *),0);
if (Status == OS_SUCCESS)
{
/* just translate the RC to CFE */
Status = CFE_SUCCESS;
}
else
{
if (Status == OS_ERR_NAME_TAKEN)
{
PendingEventId = CFE_SB_CR_PIPE_NAME_TAKEN_EID;
}
else if (Status == OS_ERR_NO_FREE_IDS)
{
PendingEventId = CFE_SB_CR_PIPE_NO_FREE_EID;
}
else
{
/* some other unexpected error */
PendingEventId = CFE_SB_CR_PIPE_ERR_EID;
}
/* translate OSAL error to CFE error code */
Status = CFE_SB_PIPE_CR_ERR;
}
}
CFE_SB_LockSharedData(__func__,__LINE__);
if (Status == CFE_SUCCESS)
{
/* fill in the pipe table fields */
PipeDscPtr->SysQueueId = SysQueueId;
PipeDscPtr->MaxQueueDepth = Depth;
PipeDscPtr->AppId = AppId;
CFE_SB_PipeDescSetUsed(PipeDscPtr, PendingPipeId);
/* Increment the Pipes in use ctr and if it's > the high water mark,*/
/* adjust the high water mark */
CFE_SB_Global.StatTlmMsg.Payload.PipesInUse++;
if (CFE_SB_Global.StatTlmMsg.Payload.PipesInUse > CFE_SB_Global.StatTlmMsg.Payload.PeakPipesInUse)
{
CFE_SB_Global.StatTlmMsg.Payload.PeakPipesInUse = CFE_SB_Global.StatTlmMsg.Payload.PipesInUse;
}/* end if */
}
else
{
/*
* If a descriptor had been allocated, then free it.
*/
if (PipeDscPtr != NULL)
{
CFE_SB_PipeDescSetFree(PipeDscPtr);
PipeDscPtr = NULL;
}
PendingPipeId = CFE_RESOURCEID_UNDEFINED;
/*
* If there is a relevant error counter, increment it now
* while the global data is locked.
*/
switch(PendingEventId)
{
case CFE_SB_CR_PIPE_BAD_ARG_EID:
++CFE_SB_Global.HKTlmMsg.Payload.CreatePipeErrorCounter;
break;
default:
/* no counter */
break;
}
}
CFE_SB_UnlockSharedData(__func__,__LINE__);
/* Send any pending events now, after final unlock */
if (Status == CFE_SUCCESS)
{
/* send debug event */
CFE_EVS_SendEventWithAppID(CFE_SB_PIPE_ADDED_EID,CFE_EVS_EventType_DEBUG,CFE_SB_Global.AppId,
"Pipe Created:name %s,id %d,app %s",
PipeName, (int)CFE_ResourceId_ToInteger(PendingPipeId),
CFE_SB_GetAppTskName(TskId,FullName));
/* give the pipe handle to the caller */
*PipeIdPtr = CFE_SB_PIPEID_C(PendingPipeId);
}
else
{
switch(PendingEventId)
{
case CFE_SB_CR_PIPE_BAD_ARG_EID:
CFE_EVS_SendEventWithAppID(CFE_SB_CR_PIPE_BAD_ARG_EID,CFE_EVS_EventType_ERROR,CFE_SB_Global.AppId,
"CreatePipeErr:Bad Input Arg:app=%s,ptr=0x%lx,depth=%d,maxdepth=%d",
CFE_SB_GetAppTskName(TskId,FullName),(unsigned long)PipeIdPtr,(int)Depth,OS_QUEUE_MAX_DEPTH);
break;
case CFE_SB_MAX_PIPES_MET_EID:
CFE_EVS_SendEventWithAppID(CFE_SB_MAX_PIPES_MET_EID,CFE_EVS_EventType_ERROR,CFE_SB_Global.AppId,
"CreatePipeErr:Max Pipes(%d)In Use.app %s",
CFE_PLATFORM_SB_MAX_PIPES,CFE_SB_GetAppTskName(TskId,FullName));
break;
case CFE_SB_CR_PIPE_NAME_TAKEN_EID:
CFE_EVS_SendEventWithAppID(CFE_SB_CR_PIPE_NAME_TAKEN_EID,CFE_EVS_EventType_ERROR,CFE_SB_Global.AppId,
"CreatePipeErr:OS_QueueCreate failed, name taken (app=%s, name=%s)",
CFE_SB_GetAppTskName(TskId,FullName), PipeName);
break;
case CFE_SB_CR_PIPE_NO_FREE_EID:
CFE_EVS_SendEventWithAppID(CFE_SB_CR_PIPE_NO_FREE_EID,CFE_EVS_EventType_ERROR,CFE_SB_Global.AppId,
"CreatePipeErr:OS_QueueCreate failed, no free id's (app=%s)",
CFE_SB_GetAppTskName(TskId,FullName));
break;
case CFE_SB_CR_PIPE_ERR_EID:
CFE_EVS_SendEventWithAppID(CFE_SB_CR_PIPE_ERR_EID,CFE_EVS_EventType_ERROR,CFE_SB_Global.AppId,
"CreatePipeErr:OS_QueueCreate returned %d,app %s",
(int)Status,CFE_SB_GetAppTskName(TskId,FullName));
break;
}
}
return Status;
}/* end CFE_SB_CreatePipe */
/*
* Function: CFE_SB_DeletePipe - See API and header file for details
*/
int32 CFE_SB_DeletePipe(CFE_SB_PipeId_t PipeId)
{
CFE_ES_AppId_t CallerId;
int32 Status = 0;
/* get the callers Application Id */
CFE_ES_GetAppID(&CallerId);
Status = CFE_SB_DeletePipeFull(PipeId,CallerId);
return Status;
}/* end CFE_SB_DeletePipe */
/******************************************************************************
** Function: CFE_SB_DeletePipeWithAppId()
**
** Purpose:
**
**
** Arguments:
** PipeId - The ID of the pipe to delete.
**
** Return:
** CFE_SUCCESS or cFE Error Code
*/
int32 CFE_SB_DeletePipeWithAppId(CFE_SB_PipeId_t PipeId, CFE_ES_AppId_t AppId)
{
int32 Status = 0;
Status = CFE_SB_DeletePipeFull(PipeId,AppId);
return Status;
}/* end CFE_SB_DeletePipeWithAppId */
/******************************************************************************
* Local callback helper for deleting a pipe from a route
*/
void CFE_SB_RemovePipeFromRoute(CFE_SBR_RouteId_t RouteId, void *ArgPtr)
{
CFE_SB_DestinationD_t *destptr;
CFE_SB_RemovePipeCallback_t *args;
args = (CFE_SB_RemovePipeCallback_t *)ArgPtr;
destptr = CFE_SB_GetDestPtr(RouteId, args->PipeId);
if (destptr != NULL)
{
CFE_SB_RemoveDest(RouteId, destptr);
}
}
/******************************************************************************
** Function: CFE_SB_DeletePipeFull()
**
** Purpose:
** Will unsubscribe to all routes associated with the given pipe id, then remove
** pipe from the pipe table.
**
** NOTE:This function cannot be called directly, it would not be semaphore protected
**
** Arguments:
** PipeId - The ID of the pipe to delete.
**
** Return:
** CFE_SUCCESS or cFE Error Code
*/
int32 CFE_SB_DeletePipeFull(CFE_SB_PipeId_t PipeId,CFE_ES_AppId_t AppId)
{
CFE_SB_PipeD_t *PipeDscPtr;
int32 Status;
CFE_ES_TaskId_t TskId;
CFE_SB_BufferD_t *BufDscPtr;
osal_id_t SysQueueId;
char FullName[(OS_MAX_API_NAME * 2)];
size_t BufDscSize;
CFE_SB_RemovePipeCallback_t Args;
uint16 PendingEventID;
Status = CFE_SUCCESS;
PendingEventID = 0;
SysQueueId = OS_OBJECT_ID_UNDEFINED;
BufDscPtr = NULL;
/* take semaphore to prevent a task switch during this call */
CFE_SB_LockSharedData(__func__,__LINE__);
/* check input parameter */
PipeDscPtr = CFE_SB_LocatePipeDescByID(PipeId);
if (!CFE_SB_PipeDescIsMatch(PipeDscPtr, PipeId))
{
PendingEventID = CFE_SB_DEL_PIPE_ERR1_EID;
Status = CFE_SB_BAD_ARGUMENT;
}
/* check that the given AppId is the owner of the pipe */
else if (!CFE_RESOURCEID_TEST_EQUAL(AppId, PipeDscPtr->AppId))
{
PendingEventID = CFE_SB_DEL_PIPE_ERR2_EID;
Status = CFE_SB_BAD_ARGUMENT;
}
else
{
/* Remove the pipe from all routes */
Args.PipeId = PipeId;
Args.FullName = FullName;
CFE_SBR_ForEachRouteId(CFE_SB_RemovePipeFromRoute, &Args, NULL);
/*
* With the route removed there should be no new messages written to this pipe,
*
* but the pipe ID itself also needs to be invalidated now (before releasing lock) to make
* sure that no no subscriptions/routes can be added either.
*
* However we must first save certain state data for later deletion.
*/
SysQueueId = PipeDscPtr->SysQueueId;
BufDscPtr = PipeDscPtr->LastBuffer;
/*
* Mark entry as "reserved" so other resources can be deleted
* while the SB global is unlocked. This prevents other tasks
* from trying to use this Pipe Desc slot, and also should prevents
* any task from re-subscribing to this pipe.
*/
CFE_SB_PipeDescSetUsed(PipeDscPtr, CFE_RESOURCEID_RESERVED);
}
CFE_SB_UnlockSharedData(__func__,__LINE__);
/* remove any messages that might be on the pipe */
if (Status == CFE_SUCCESS)
{
while(true)
{
/* decrement refcount of any previous buffer */
if (BufDscPtr != NULL)
{
CFE_SB_LockSharedData(__func__,__LINE__);
CFE_SB_DecrBufUseCnt(BufDscPtr);
CFE_SB_UnlockSharedData(__func__,__LINE__);
BufDscPtr = NULL;
}
if (OS_QueueGet(SysQueueId, &BufDscPtr, sizeof(BufDscPtr), &BufDscSize, OS_CHECK) != OS_SUCCESS)
{
/* no more messages */
break;
}
}
/* Delete the underlying OS queue */
OS_QueueDelete(SysQueueId);
}
/*
* Final cleanup with global data locked
*/
CFE_SB_LockSharedData(__func__,__LINE__);
if (Status == CFE_SUCCESS)
{
CFE_SB_PipeDescSetFree(PipeDscPtr);
--CFE_SB_Global.StatTlmMsg.Payload.PipesInUse;
}
else if (PendingEventID != 0)
{
CFE_SB_Global.HKTlmMsg.Payload.CreatePipeErrorCounter++;
}
CFE_SB_UnlockSharedData(__func__,__LINE__);
if (Status == CFE_SUCCESS)
{
/*
* Get the app name of the actual pipe owner for the event string
* as this may be different than the task doing the deletion.
*
* Note: If this fails (e.g. bad AppID, it returns an empty string
*/
CFE_ES_GetAppName(FullName, AppId, sizeof(FullName));
CFE_EVS_SendEventWithAppID(CFE_SB_PIPE_DELETED_EID,CFE_EVS_EventType_DEBUG,CFE_SB_Global.AppId,
"Pipe Deleted:id %d,owner %s",(int)CFE_RESOURCEID_TO_ULONG(PipeId), FullName);
}
else
{
/* get TaskId and name of caller for events */
CFE_ES_GetTaskID(&TskId);
CFE_SB_GetAppTskName(TskId, FullName);
switch (PendingEventID)
{
case CFE_SB_DEL_PIPE_ERR1_EID:
CFE_EVS_SendEventWithAppID(CFE_SB_DEL_PIPE_ERR1_EID,CFE_EVS_EventType_ERROR,CFE_SB_Global.AppId,
"Pipe Delete Error:Bad Argument,PipedId %ld,Requestor %s",
CFE_RESOURCEID_TO_ULONG(PipeId),FullName);
break;
case CFE_SB_DEL_PIPE_ERR2_EID:
CFE_EVS_SendEventWithAppID(CFE_SB_DEL_PIPE_ERR2_EID,CFE_EVS_EventType_ERROR,CFE_SB_Global.AppId,
"Pipe Delete Error:Caller(%s) is not the owner of pipe %ld",
FullName, CFE_RESOURCEID_TO_ULONG(PipeId));
break;
}
}
return Status;
}/* end CFE_SB_DeletePipeFull */
/*
* Function: CFE_SB_SetPipeOpts - See API and header file for details
*/
int32 CFE_SB_SetPipeOpts(CFE_SB_PipeId_t PipeId, uint8 Opts)
{
CFE_SB_PipeD_t *PipeDscPtr;
CFE_ES_AppId_t AppID;
CFE_ES_TaskId_t TskId;
uint16 PendingEventID;
int32 Status;
char FullName[(OS_MAX_API_NAME * 2)];
PendingEventID = 0;
Status = CFE_ES_GetAppID(&AppID);
if(Status != CFE_SUCCESS)
{
/* shouldn't happen... */
return Status;
}
/* take semaphore to prevent a task switch during this call */
CFE_SB_LockSharedData(__func__,__LINE__);
/* check input parameter */
PipeDscPtr = CFE_SB_LocatePipeDescByID(PipeId);
if(!CFE_SB_PipeDescIsMatch(PipeDscPtr, PipeId))
{
PendingEventID = CFE_SB_SETPIPEOPTS_ID_ERR_EID;
Status = CFE_SB_BAD_ARGUMENT;
}
/* check that the caller AppId is the owner of the pipe */
else if( !CFE_RESOURCEID_TEST_EQUAL(AppID, PipeDscPtr->AppId) )
{
PendingEventID = CFE_SB_SETPIPEOPTS_OWNER_ERR_EID;
Status = CFE_SB_BAD_ARGUMENT;
}
else
{
PipeDscPtr->Opts = Opts;
}
/* If anything went wrong, increment the error counter before unlock */
if (Status != CFE_SUCCESS)
{
CFE_SB_Global.HKTlmMsg.Payload.PipeOptsErrorCounter++;
}
CFE_SB_UnlockSharedData(__func__,__LINE__);
/* Send events after unlocking SB */
if (Status == CFE_SUCCESS)
{
/* get AppID of caller for events */
CFE_ES_GetAppName(FullName, AppID, sizeof(FullName));
CFE_EVS_SendEventWithAppID(CFE_SB_SETPIPEOPTS_EID,CFE_EVS_EventType_DEBUG,CFE_SB_Global.AppId,
"Pipe opts set:id %lu,owner %s, opts=0x%02x",CFE_RESOURCEID_TO_ULONG(PipeId), FullName, (unsigned int)Opts);
}
else
{
/* get TaskId of caller for events */
CFE_ES_GetTaskID(&TskId);
switch (PendingEventID)
{
case CFE_SB_SETPIPEOPTS_ID_ERR_EID:
CFE_EVS_SendEventWithAppID(CFE_SB_SETPIPEOPTS_ID_ERR_EID,CFE_EVS_EventType_ERROR,CFE_SB_Global.AppId,
"Pipe Opts Error:Bad Argument,PipedId %lu,Requestor %s",
CFE_RESOURCEID_TO_ULONG(PipeId),CFE_SB_GetAppTskName(TskId,FullName));
break;
case CFE_SB_SETPIPEOPTS_OWNER_ERR_EID:
CFE_EVS_SendEventWithAppID(CFE_SB_SETPIPEOPTS_OWNER_ERR_EID,CFE_EVS_EventType_ERROR,CFE_SB_Global.AppId,
"Pipe Opts Set Error: Caller(%s) is not the owner of pipe %lu",
CFE_SB_GetAppTskName(TskId,FullName),CFE_RESOURCEID_TO_ULONG(PipeId));
break;
}
}
return Status;
}/* end CFE_SB_SetPipeOpts */
/*
* Function: CFE_SB_GetPipeOpts - See API and header file for details
*/
int32 CFE_SB_GetPipeOpts(CFE_SB_PipeId_t PipeId, uint8 *OptsPtr)
{
int32 Status;
CFE_ES_TaskId_t TskId;
char FullName[(OS_MAX_API_NAME * 2)];
uint16 PendingEventID;
CFE_SB_PipeD_t *PipeDscPtr;
PendingEventID = 0;
Status = CFE_SUCCESS;
/* take semaphore to prevent a task switch during this call */
CFE_SB_LockSharedData(__func__,__LINE__);
/* check input parameter */
PipeDscPtr = CFE_SB_LocatePipeDescByID(PipeId);
if (!CFE_SB_PipeDescIsMatch(PipeDscPtr, PipeId))
{
PendingEventID = CFE_SB_GETPIPEOPTS_ID_ERR_EID;
Status = CFE_SB_BAD_ARGUMENT;
}
else if (OptsPtr == NULL)
{
PendingEventID = CFE_SB_GETPIPEOPTS_PTR_ERR_EID;
Status = CFE_SB_BAD_ARGUMENT;
}
else
{
*OptsPtr = PipeDscPtr->Opts;
}
/* If anything went wrong, increment the error counter before unlock */
if (Status != CFE_SUCCESS)
{
CFE_SB_Global.HKTlmMsg.Payload.PipeOptsErrorCounter++;
}
CFE_SB_UnlockSharedData(__func__,__LINE__);
/* Send events after unlocking SB */
if (Status == CFE_SUCCESS)
{
CFE_EVS_SendEventWithAppID(CFE_SB_GETPIPEOPTS_EID,CFE_EVS_EventType_DEBUG,CFE_SB_Global.AppId,
"Pipe opts get:id %lu, opts=0x%02x",CFE_RESOURCEID_TO_ULONG(PipeId), (unsigned int)*OptsPtr);
}
else
{
/* get TaskId of caller for events */
CFE_ES_GetTaskID(&TskId);
switch(PendingEventID)
{
case CFE_SB_GETPIPEOPTS_PTR_ERR_EID:
CFE_EVS_SendEventWithAppID(CFE_SB_GETPIPEOPTS_PTR_ERR_EID, CFE_EVS_EventType_ERROR, CFE_SB_Global.AppId,
"Pipe Opts Error:Bad Argument,Requestor %s",
CFE_SB_GetAppTskName(TskId,FullName));
break;
case CFE_SB_GETPIPEOPTS_ID_ERR_EID:
CFE_EVS_SendEventWithAppID(CFE_SB_GETPIPEOPTS_ID_ERR_EID, CFE_EVS_EventType_ERROR, CFE_SB_Global.AppId,
"Pipe Opts Error:Bad Argument,PipedId %lu,Requestor %s",
CFE_RESOURCEID_TO_ULONG(PipeId),CFE_SB_GetAppTskName(TskId,FullName));
break;
}
}
return Status;
}/* end CFE_SB_GetPipeOpts */
/*
* Function: CFE_SB_GetPipeName - See API and header file for details
*/
int32 CFE_SB_GetPipeName(char *PipeNameBuf, size_t PipeNameSize, CFE_SB_PipeId_t PipeId)
{
int32 Status;
CFE_ES_TaskId_t TskId;
char FullName[(OS_MAX_API_NAME * 2)];
uint16 PendingEventID;
CFE_SB_PipeD_t *PipeDscPtr;
osal_id_t SysQueueId;
PendingEventID = 0;
Status = CFE_SUCCESS;
SysQueueId = OS_OBJECT_ID_UNDEFINED;
/* take semaphore to prevent a task switch during this call */
CFE_SB_LockSharedData(__func__,__LINE__);
/* check input parameter */
PipeDscPtr = CFE_SB_LocatePipeDescByID(PipeId);
if (!CFE_SB_PipeDescIsMatch(PipeDscPtr, PipeId))
{
PendingEventID = CFE_SB_GETPIPENAME_ID_ERR_EID;
Status = CFE_SB_BAD_ARGUMENT;
}
else
{
SysQueueId = PipeDscPtr->SysQueueId;
}
CFE_SB_UnlockSharedData(__func__,__LINE__);
if (Status == CFE_SUCCESS)
{
if (PipeNameBuf == NULL || PipeNameSize == 0)
{
PendingEventID = CFE_SB_GETPIPENAME_NULL_PTR_EID;
Status = CFE_SB_BAD_ARGUMENT;
}
else
{
Status = OS_GetResourceName(SysQueueId, PipeNameBuf, PipeNameSize);
if (Status == OS_SUCCESS)
{
Status = CFE_SUCCESS;
}
else
{
PendingEventID = CFE_SB_GETPIPENAME_ID_ERR_EID;
Status = CFE_SB_BAD_ARGUMENT;
}
}
}
/* Send Events */
if (Status == CFE_SUCCESS)
{
CFE_EVS_SendEventWithAppID(CFE_SB_GETPIPENAME_EID,
CFE_EVS_EventType_DEBUG,CFE_SB_Global.AppId,
"GetPipeName name=%s id=%lu",
PipeNameBuf, CFE_RESOURCEID_TO_ULONG(PipeId));
}
else
{
CFE_ES_GetTaskID(&TskId);
switch(PendingEventID)
{
case CFE_SB_GETPIPENAME_NULL_PTR_EID:
CFE_EVS_SendEventWithAppID(CFE_SB_GETPIPENAME_NULL_PTR_EID, CFE_EVS_EventType_ERROR,
CFE_SB_Global.AppId, "Pipe Name Error:NullPtr,Requestor %s",
CFE_SB_GetAppTskName(TskId,FullName));
break;
case CFE_SB_GETPIPENAME_ID_ERR_EID:
CFE_EVS_SendEventWithAppID(CFE_SB_GETPIPENAME_ID_ERR_EID, CFE_EVS_EventType_ERROR,
CFE_SB_Global.AppId, "Pipe Id Error:Bad Argument,Id=%lu,Requestor %s",
CFE_RESOURCEID_TO_ULONG(PipeId),CFE_SB_GetAppTskName(TskId,FullName));
break;
}
if (PipeNameBuf != NULL && PipeNameSize > 0)
{
memset(PipeNameBuf, 0, PipeNameSize);
}
}
return Status;
}/* end CFE_SB_GetPipeName */
/*
* Function: CFE_SB_GetPipeIdByName - See API and header file for details
*/
int32 CFE_SB_GetPipeIdByName(CFE_SB_PipeId_t *PipeIdPtr, const char *PipeName)
{
int32 Status;
CFE_ES_TaskId_t TskId;
uint32 Idx;
char FullName[(OS_MAX_API_NAME * 2)];
uint16 PendingEventID;
CFE_SB_PipeD_t *PipeDscPtr;
osal_id_t SysQueueId;
PendingEventID = 0;
Status = CFE_SUCCESS;
SysQueueId = OS_OBJECT_ID_UNDEFINED;
if(PipeName == NULL || PipeIdPtr == NULL)
{
PendingEventID = CFE_SB_GETPIPEIDBYNAME_NULL_ERR_EID;
Status = CFE_SB_BAD_ARGUMENT;
}
else
{
/* Get QueueID from OSAL */
Status = OS_QueueGetIdByName(&SysQueueId, PipeName);
if (Status == OS_SUCCESS)
{
Status = CFE_SUCCESS;
}
else
{
PendingEventID = CFE_SB_GETPIPEIDBYNAME_NAME_ERR_EID;
Status = CFE_SB_BAD_ARGUMENT;
}
}
CFE_SB_LockSharedData(__func__,__LINE__);
if (Status == CFE_SUCCESS)
{
Idx = CFE_PLATFORM_SB_MAX_PIPES;
PipeDscPtr = CFE_SB_Global.PipeTbl;
while(true)
{
if (Idx == 0)
{
PendingEventID = CFE_SB_GETPIPEIDBYNAME_NAME_ERR_EID;
Status = CFE_SB_BAD_ARGUMENT;
break;
}
if (OS_ObjectIdEqual(PipeDscPtr->SysQueueId, SysQueueId))
{
/* grab the ID before we release the lock */
*PipeIdPtr = CFE_SB_PipeDescGetID(PipeDscPtr);
break;
}/* end if */
--Idx;
++PipeDscPtr;
}
}
if (Status != CFE_SUCCESS)
{
++CFE_SB_Global.HKTlmMsg.Payload.GetPipeIdByNameErrorCounter;
}
CFE_SB_UnlockSharedData(__func__,__LINE__);
/* Send Events */
if (Status == CFE_SUCCESS)
{
CFE_EVS_SendEventWithAppID(CFE_SB_GETPIPEIDBYNAME_EID,
CFE_EVS_EventType_DEBUG,CFE_SB_Global.AppId,
"PipeIdByName name=%s id=%lu",
PipeName, CFE_RESOURCEID_TO_ULONG(*PipeIdPtr));
}
else
{
/* get TaskId of caller for events */
CFE_ES_GetTaskID(&TskId);
switch(PendingEventID)
{
case CFE_SB_GETPIPEIDBYNAME_NULL_ERR_EID:
CFE_EVS_SendEventWithAppID(CFE_SB_GETPIPEIDBYNAME_NULL_ERR_EID,
CFE_EVS_EventType_ERROR, CFE_SB_Global.AppId,
"Pipe ID By Name Error:Bad Argument,Requestor %s",
CFE_SB_GetAppTskName(TskId,FullName));
break;
case CFE_SB_GETPIPEIDBYNAME_NAME_ERR_EID:
CFE_EVS_SendEventWithAppID(CFE_SB_GETPIPEIDBYNAME_NAME_ERR_EID,
CFE_EVS_EventType_ERROR, CFE_SB_Global.AppId,
"Pipe ID By Name Error:Bad Argument,Requestor %s",
CFE_SB_GetAppTskName(TskId,FullName));
break;
}
}
return Status;
}/* end CFE_SB_GetPipeIdByName */
/*
* Function: CFE_SB_SubscribeEx - See API and header file for details
*/
int32 CFE_SB_SubscribeEx(CFE_SB_MsgId_t MsgId,
CFE_SB_PipeId_t PipeId,
CFE_SB_Qos_t Quality,
uint16 MsgLim)
{
return CFE_SB_SubscribeFull(MsgId,PipeId,Quality,MsgLim,(uint8)CFE_SB_MSG_GLOBAL);
}/* end CFE_SB_SubscribeEx */
/*
* Function: CFE_SB_SubscribeLocal - See API and header file for details
*/
int32 CFE_SB_SubscribeLocal(CFE_SB_MsgId_t MsgId,
CFE_SB_PipeId_t PipeId,
uint16 MsgLim)
{
return CFE_SB_SubscribeFull(MsgId,PipeId,CFE_SB_DEFAULT_QOS,MsgLim,
(uint8)CFE_SB_MSG_LOCAL);
}/* end CFE_SB_SubscribeLocal */
/*
* Function: CFE_SB_Subscribe - See API and header file for details
*/
int32 CFE_SB_Subscribe(CFE_SB_MsgId_t MsgId,
CFE_SB_PipeId_t PipeId)
{
return CFE_SB_SubscribeFull(MsgId,PipeId,CFE_SB_DEFAULT_QOS,
(uint16)CFE_PLATFORM_SB_DEFAULT_MSG_LIMIT,
(uint8)CFE_SB_MSG_GLOBAL);
}/* end CFE_SB_Subscribe */
/******************************************************************************
** Name: CFE_SB_SubscribeFull
**
** Purpose: CFE Internal API used to subscribe to a message. Contains an input
** parameter for all possible subscription choices. This function is
** called by CFE_SB_SubscribeEx, CFE_SB_Subscribe and
** CFE_SB_SubscribeLocal.
**
** Assumptions, External Events, and Notes:
** Has the same typedef as the message Id
**
** Date Written:
** 04/25/2005
**
** Input Arguments:
** MsgId - Mission unique identifier for the message being requested
** PipeId - The Pipe ID to send the message to
** Quality - Quality of Service (Qos) - priority and reliability
** MsgLim - Max number of messages, with this MsgId, allowed on the
** pipe at any time.
** Scope - Local subscription or broadcasted to peers
**
** Output Arguments:
** None
**
** Return Values:
** Status
**
******************************************************************************/
int32 CFE_SB_SubscribeFull(CFE_SB_MsgId_t MsgId,
CFE_SB_PipeId_t PipeId,
CFE_SB_Qos_t Quality,
uint16 MsgLim,
uint8 Scope)
{
CFE_SBR_RouteId_t RouteId;
CFE_SB_PipeD_t *PipeDscPtr;
int32 Status;
CFE_ES_TaskId_t TskId;
CFE_ES_AppId_t AppId;
CFE_SB_DestinationD_t *DestPtr;
uint32 DestCount;
char FullName[(OS_MAX_API_NAME * 2)];
char PipeName[OS_MAX_API_NAME];
uint32 Collisions;
uint16 PendingEventID;
PendingEventID = 0;
Status = CFE_SUCCESS;
DestPtr = NULL;
Collisions = 0;
/* get the callers Application Id */
CFE_ES_GetAppID(&AppId);
/* get TaskId of caller for events */
CFE_ES_GetTaskID(&TskId);
/* take semaphore to prevent a task switch during this call */
CFE_SB_LockSharedData(__func__,__LINE__);
/* check that the pipe has been created */
PipeDscPtr = CFE_SB_LocatePipeDescByID(PipeId);
if (!CFE_SB_PipeDescIsMatch(PipeDscPtr, PipeId))
{
PendingEventID = CFE_SB_SUB_INV_PIPE_EID;
Status = CFE_SB_BAD_ARGUMENT;
}
else if (!CFE_RESOURCEID_TEST_EQUAL(PipeDscPtr->AppId, AppId))
{
PendingEventID = CFE_SB_SUB_INV_CALLER_EID;
Status = CFE_SB_BAD_ARGUMENT;
}
/* check message id key and scope */
else if (!CFE_SB_IsValidMsgId(MsgId) || (Scope > 1))
{
PendingEventID = CFE_SB_SUB_ARG_ERR_EID;
Status = CFE_SB_BAD_ARGUMENT;
}
else
{
/* Get the route, adding one if it does not exist already */
RouteId = CFE_SBR_GetRouteId(MsgId);
if (!CFE_SBR_IsValidRouteId(RouteId))
{
/* Add the route */
RouteId = CFE_SBR_AddRoute(MsgId, &Collisions);
/* if all routing table elements are used, send event */
if (!CFE_SBR_IsValidRouteId(RouteId))
{
PendingEventID = CFE_SB_MAX_MSGS_MET_EID;
Status = CFE_SB_MAX_MSGS_MET;
}
else
{
/* Increment the MsgIds in use ctr and if it's > the high water mark,*/
/* adjust the high water mark */
CFE_SB_Global.StatTlmMsg.Payload.MsgIdsInUse++;
if(CFE_SB_Global.StatTlmMsg.Payload.MsgIdsInUse > CFE_SB_Global.StatTlmMsg.Payload.PeakMsgIdsInUse)