-
Notifications
You must be signed in to change notification settings - Fork 202
/
cfe_sb_api.c
1960 lines (1613 loc) · 69.4 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>
/*
* 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.StatTlmMsg.Payload.PipeDepthStats) / sizeof(CFE_SB.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_CreatePipe - See API and header file for details
*/
int32 CFE_SB_CreatePipe(CFE_SB_PipeId_t *PipeIdPtr, uint16 Depth, const char *PipeName)
{
CFE_ES_ResourceID_t AppId;
CFE_ES_ResourceID_t TskId;
osal_id_t SysQueueId;
int32 Status;
CFE_SB_PipeId_t OriginalPipeIdParamValue = (PipeIdPtr == NULL) ? 0 : (*PipeIdPtr);
CFE_SB_PipeId_t PipeTblIdx;
char AppName[OS_MAX_API_NAME] = {'\0'};
char FullName[(OS_MAX_API_NAME * 2)];
/* get callers AppId */
CFE_ES_GetAppID(&AppId);
/* get callers TaskId */
CFE_ES_GetTaskID(&TskId);
/* get callers name */
CFE_ES_GetAppName(AppName, AppId, sizeof(AppName));
/* Hardcode a NULL terminator, in case rcvd name was too long */
AppName[OS_MAX_API_NAME-1]= '\0';
/* take semaphore to prevent a task switch during this call */
CFE_SB_LockSharedData(__func__,__LINE__);
/* set user's pipe id value to 'invalid' for error cases below */
if(PipeIdPtr != NULL){
*PipeIdPtr = CFE_SB_INVALID_PIPE;
}/* end if */
/* check input parameters */
if((PipeIdPtr == NULL)||(Depth > CFE_PLATFORM_SB_MAX_PIPE_DEPTH)||(Depth == 0)){
CFE_SB.HKTlmMsg.Payload.CreatePipeErrorCounter++;
CFE_SB_UnlockSharedData(__func__,__LINE__);
CFE_EVS_SendEventWithAppID(CFE_SB_CR_PIPE_BAD_ARG_EID,CFE_EVS_EventType_ERROR,CFE_SB.AppId,
"CreatePipeErr:Bad Input Arg:app=%s,ptr=0x%lx,depth=%d,maxdepth=%d",
CFE_SB_GetAppTskName(TskId,FullName),(unsigned long)PipeIdPtr,(int)Depth,CFE_PLATFORM_SB_MAX_PIPE_DEPTH);
return CFE_SB_BAD_ARGUMENT;
}/*end if*/
/* get first available entry in pipe table */
PipeTblIdx = CFE_SB_GetAvailPipeIdx();
/* if pipe table is full, send event and return error */
if(PipeTblIdx == CFE_SB_INVALID_PIPE){
CFE_SB_UnlockSharedData(__func__,__LINE__);
CFE_EVS_SendEventWithAppID(CFE_SB_MAX_PIPES_MET_EID,CFE_EVS_EventType_ERROR,CFE_SB.AppId,
"CreatePipeErr:Max Pipes(%d)In Use.app %s",
CFE_PLATFORM_SB_MAX_PIPES,CFE_SB_GetAppTskName(TskId,FullName));
return CFE_SB_MAX_PIPES_MET;
}/* end if */
/* create the queue */
Status = OS_QueueCreate(&SysQueueId,PipeName,Depth,sizeof(CFE_SB_BufferD_t *),0);
if (Status != OS_SUCCESS) {
CFE_SB_UnlockSharedData(__func__,__LINE__);
/* if OS_QueueCreate() failed because the pipe name passed in was already in use... */
/* let's make sure we don't alter the user's pipe ID data */
switch(Status) {
case OS_ERR_NAME_TAKEN:
CFE_EVS_SendEventWithAppID(CFE_SB_CR_PIPE_NAME_TAKEN_EID,CFE_EVS_EventType_ERROR,CFE_SB.AppId,
"CreatePipeErr:OS_QueueCreate failed, name taken (app=%s, name=%s)",
CFE_SB_GetAppTskName(TskId,FullName), PipeName);
*PipeIdPtr = OriginalPipeIdParamValue;
break;
case OS_ERR_NO_FREE_IDS:
CFE_EVS_SendEventWithAppID(CFE_SB_CR_PIPE_NO_FREE_EID,CFE_EVS_EventType_ERROR,CFE_SB.AppId,
"CreatePipeErr:OS_QueueCreate failed, no free id's (app=%s)",
CFE_SB_GetAppTskName(TskId,FullName));
break;
default:
CFE_EVS_SendEventWithAppID(CFE_SB_CR_PIPE_ERR_EID,CFE_EVS_EventType_ERROR,CFE_SB.AppId,
"CreatePipeErr:OS_QueueCreate returned %d,app %s",
(int)Status,CFE_SB_GetAppTskName(TskId,FullName));
}/* end switch(Status) */
return CFE_SB_PIPE_CR_ERR;
}/* end if */
/* fill in the pipe table fields */
CFE_SB.PipeTbl[PipeTblIdx].InUse = CFE_SB_IN_USE;
CFE_SB.PipeTbl[PipeTblIdx].SysQueueId = SysQueueId;
CFE_SB.PipeTbl[PipeTblIdx].PipeId = PipeTblIdx;
CFE_SB.PipeTbl[PipeTblIdx].QueueDepth = Depth;
CFE_SB.PipeTbl[PipeTblIdx].AppId = AppId;
CFE_SB.PipeTbl[PipeTblIdx].SendErrors = 0;
CFE_SB.PipeTbl[PipeTblIdx].CurrentBuff = NULL;
CFE_SB.PipeTbl[PipeTblIdx].ToTrashBuff = NULL;
strcpy(&CFE_SB.PipeTbl[PipeTblIdx].AppName[0],&AppName[0]);
/* Increment the Pipes in use ctr and if it's > the high water mark,*/
/* adjust the high water mark */
CFE_SB.StatTlmMsg.Payload.PipesInUse++;
if(CFE_SB.StatTlmMsg.Payload.PipesInUse > CFE_SB.StatTlmMsg.Payload.PeakPipesInUse){
CFE_SB.StatTlmMsg.Payload.PeakPipesInUse = CFE_SB.StatTlmMsg.Payload.PipesInUse;
}/* end if */
/* Reset the pipe depth parameters in the statistics pkt */
if (PipeTblIdx < CFE_SB_TLM_PIPEDEPTHSTATS_SIZE)
{
CFE_SB.StatTlmMsg.Payload.PipeDepthStats[PipeTblIdx].PipeId = PipeTblIdx;
CFE_SB.StatTlmMsg.Payload.PipeDepthStats[PipeTblIdx].Depth = Depth;
CFE_SB.StatTlmMsg.Payload.PipeDepthStats[PipeTblIdx].InUse = 0;
CFE_SB.StatTlmMsg.Payload.PipeDepthStats[PipeTblIdx].PeakInUse = 0;
}
/* give the pipe handle to the caller */
*PipeIdPtr = PipeTblIdx;
CFE_SB_UnlockSharedData(__func__,__LINE__);
/* send debug event */
CFE_EVS_SendEventWithAppID(CFE_SB_PIPE_ADDED_EID,CFE_EVS_EventType_DEBUG,CFE_SB.AppId,
"Pipe Created:name %s,id %d,app %s",
PipeName, (int)CFE_SB.PipeTbl[PipeTblIdx].PipeId,
CFE_SB_GetAppTskName(TskId,FullName));
return CFE_SUCCESS;
}/* 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_ResourceID_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_ResourceID_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_ResourceID_t AppId)
{
uint8 PipeTblIdx;
int32 RtnFromVal;
int32 Stat;
CFE_ES_ResourceID_t Owner;
CFE_ES_ResourceID_t TskId;
CFE_SB_Buffer_t *BufPtr;
char FullName[(OS_MAX_API_NAME * 2)];
CFE_SB_RemovePipeCallback_t Args;
/* get TaskId and name of caller for events */
CFE_ES_GetTaskID(&TskId);
CFE_SB_GetAppTskName(TskId, FullName);
/* take semaphore to prevent a task switch during this call */
CFE_SB_LockSharedData(__func__,__LINE__);
/* check input parameter */
PipeTblIdx = CFE_SB_GetPipeIdx(PipeId);
RtnFromVal = CFE_SB_ValidatePipeId(PipeId);
if((RtnFromVal != CFE_SUCCESS)||(PipeTblIdx == CFE_SB_INVALID_PIPE))
{
CFE_SB.HKTlmMsg.Payload.CreatePipeErrorCounter++;
CFE_SB_UnlockSharedData(__func__,__LINE__);
CFE_EVS_SendEventWithAppID(CFE_SB_DEL_PIPE_ERR1_EID,CFE_EVS_EventType_ERROR,CFE_SB.AppId,
"Pipe Delete Error:Bad Argument,PipedId %d,Requestor %s,Idx %d,Stat %d",
(int)PipeId,FullName,(int)PipeTblIdx,(int)RtnFromVal);
return CFE_SB_BAD_ARGUMENT;
}/* end if */
Owner = CFE_SB.PipeTbl[PipeTblIdx].AppId;
/* check that the given AppId is the owner of the pipe */
if( !CFE_ES_ResourceID_Equal(AppId, Owner) )
{
CFE_SB.HKTlmMsg.Payload.CreatePipeErrorCounter++;
CFE_SB_UnlockSharedData(__func__,__LINE__);
CFE_EVS_SendEventWithAppID(CFE_SB_DEL_PIPE_ERR2_EID,CFE_EVS_EventType_ERROR,CFE_SB.AppId,
"Pipe Delete Error:Caller(%s) is not the owner of pipe %d", FullName, (int)PipeId);
return CFE_SB_BAD_ARGUMENT;
}/* end if */
/* Remove the pipe from all routes */
Args.PipeId = PipeId;
Args.FullName = FullName;
CFE_SBR_ForEachRouteId(CFE_SB_RemovePipeFromRoute, &Args, NULL);
if (CFE_SB.PipeTbl[PipeTblIdx].ToTrashBuff != NULL) {
/* Decrement the Buffer Use Count and Free buffer if cnt=0) */
CFE_SB_DecrBufUseCnt(CFE_SB.PipeTbl[PipeTblIdx].ToTrashBuff);
CFE_SB.PipeTbl[PipeTblIdx].ToTrashBuff = NULL;
}/* end if */
/* remove any messages that might be on the pipe */
/* this step will free the memory used to store the message */
do{
CFE_SB_UnlockSharedData(__func__,__LINE__);
Stat = CFE_SB_ReceiveBuffer(&BufPtr,PipeId,CFE_SB_POLL);
CFE_SB_LockSharedData(__func__,__LINE__);
}while(Stat == CFE_SUCCESS);
/* Delete the underlying OS queue */
OS_QueueDelete(CFE_SB.PipeTbl[PipeTblIdx].SysQueueId);
/* remove the pipe from the pipe table */
CFE_SB.PipeTbl[PipeTblIdx].InUse = CFE_SB_NOT_IN_USE;
CFE_SB.PipeTbl[PipeTblIdx].SysQueueId = CFE_SB_UNUSED_QUEUE;
CFE_SB.PipeTbl[PipeTblIdx].PipeId = CFE_SB_INVALID_PIPE;
CFE_SB.PipeTbl[PipeTblIdx].CurrentBuff = NULL;
/* zero out the pipe depth stats */
if (PipeTblIdx < CFE_SB_TLM_PIPEDEPTHSTATS_SIZE)
{
CFE_SB.StatTlmMsg.Payload.PipeDepthStats[PipeTblIdx].PipeId = 0;
CFE_SB.StatTlmMsg.Payload.PipeDepthStats[PipeTblIdx].Depth = 0;
CFE_SB.StatTlmMsg.Payload.PipeDepthStats[PipeTblIdx].InUse = 0;
CFE_SB.StatTlmMsg.Payload.PipeDepthStats[PipeTblIdx].PeakInUse = 0;
}
CFE_SB.StatTlmMsg.Payload.PipesInUse--;
CFE_SB_UnlockSharedData(__func__,__LINE__);
/*
* 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, Owner, sizeof(FullName));
CFE_EVS_SendEventWithAppID(CFE_SB_PIPE_DELETED_EID,CFE_EVS_EventType_DEBUG,CFE_SB.AppId,
"Pipe Deleted:id %d,owner %s",(int)PipeId, FullName);
return CFE_SUCCESS;
}/* 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)
{
uint8 PipeTblIdx = 0;
int32 RtnFromVal = 0;
CFE_ES_ResourceID_t Owner;
CFE_ES_ResourceID_t AppID;
CFE_ES_ResourceID_t TskId;
int32 Status;
char FullName[(OS_MAX_API_NAME * 2)];
Status = CFE_ES_GetAppID(&AppID);
if(Status != CFE_SUCCESS)
{
/* shouldn't happen... */
return Status;
}
/* get TaskId of caller for events */
Status = CFE_ES_GetTaskID(&TskId);
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 */
PipeTblIdx = CFE_SB_GetPipeIdx(PipeId);
RtnFromVal = CFE_SB_ValidatePipeId(PipeId);
if((RtnFromVal != CFE_SUCCESS)||(PipeTblIdx == CFE_SB_INVALID_PIPE))
{
CFE_SB.HKTlmMsg.Payload.PipeOptsErrorCounter++;
CFE_SB_UnlockSharedData(__func__,__LINE__);
CFE_EVS_SendEventWithAppID(CFE_SB_SETPIPEOPTS_ID_ERR_EID,CFE_EVS_EventType_ERROR,CFE_SB.AppId,
"Pipe Opts Error:Bad Argument,PipedId %d,Requestor %s,Idx %d,Stat %d",
(int)PipeId,CFE_SB_GetAppTskName(TskId,FullName),(int)PipeTblIdx,(int)RtnFromVal);
return CFE_SB_BAD_ARGUMENT;
}/* end if */
/* check that the given AppId is the owner of the pipe */
Owner = CFE_SB.PipeTbl[PipeTblIdx].AppId;
if( !CFE_ES_ResourceID_Equal(AppID, Owner) )
{
CFE_SB.HKTlmMsg.Payload.PipeOptsErrorCounter++;
CFE_SB_UnlockSharedData(__func__,__LINE__);
CFE_EVS_SendEventWithAppID(CFE_SB_SETPIPEOPTS_OWNER_ERR_EID,CFE_EVS_EventType_ERROR,CFE_SB.AppId,
"Pipe Opts Set Error: Caller(%s) is not the owner of pipe %d",
CFE_SB_GetAppTskName(TskId,FullName),(int)PipeId);
return CFE_SB_BAD_ARGUMENT;
}/* end if */
CFE_SB.PipeTbl[PipeTblIdx].Opts = Opts;
CFE_SB_UnlockSharedData(__func__,__LINE__);
/*
* 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, Owner, sizeof(FullName));
CFE_EVS_SendEventWithAppID(CFE_SB_SETPIPEOPTS_EID,CFE_EVS_EventType_DEBUG,CFE_SB.AppId,
"Pipe opts set:id %d,owner %s, opts=0x%02x",(int)PipeId, FullName, (unsigned int)Opts);
return CFE_SUCCESS;
}/* 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)
{
uint8 PipeTblIdx = 0;
int32 RtnFromVal = 0;
CFE_ES_ResourceID_t TskId;
char FullName[(OS_MAX_API_NAME * 2)];
/* get TaskId of caller for events */
CFE_ES_GetTaskID(&TskId);
if(OptsPtr == NULL)
{
CFE_SB.HKTlmMsg.Payload.PipeOptsErrorCounter++;
CFE_EVS_SendEventWithAppID(CFE_SB_GETPIPEOPTS_PTR_ERR_EID, CFE_EVS_EventType_ERROR, CFE_SB.AppId,
"Pipe Opts Error:Bad Argument,Requestor %s",
CFE_SB_GetAppTskName(TskId,FullName));
return CFE_SB_BAD_ARGUMENT;
}
/* take semaphore to prevent a task switch during this call */
CFE_SB_LockSharedData(__func__,__LINE__);
/* check input parameter */
PipeTblIdx = CFE_SB_GetPipeIdx(PipeId);
RtnFromVal = CFE_SB_ValidatePipeId(PipeId);
if((RtnFromVal != CFE_SUCCESS)||(PipeTblIdx == CFE_SB_INVALID_PIPE))
{
CFE_SB.HKTlmMsg.Payload.PipeOptsErrorCounter++;
CFE_SB_UnlockSharedData(__func__,__LINE__);
CFE_EVS_SendEventWithAppID(CFE_SB_GETPIPEOPTS_ID_ERR_EID, CFE_EVS_EventType_ERROR, CFE_SB.AppId,
"Pipe Opts Error:Bad Argument,PipedId %d,Requestor %s,Idx %d,Stat %d",
(int)PipeId,CFE_SB_GetAppTskName(TskId,FullName),(int)PipeTblIdx,(int)RtnFromVal);
return CFE_SB_BAD_ARGUMENT;
}/* end if */
*OptsPtr = CFE_SB.PipeTbl[PipeTblIdx].Opts;
CFE_SB_UnlockSharedData(__func__,__LINE__);
CFE_EVS_SendEventWithAppID(CFE_SB_GETPIPEOPTS_EID,CFE_EVS_EventType_DEBUG,CFE_SB.AppId,
"Pipe opts set:id %d, opts=0x%02x",(int)PipeId, (unsigned int)*OptsPtr);
return CFE_SUCCESS;
}/* 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){
OS_queue_prop_t queue_prop;
int32 Status = CFE_SUCCESS;
CFE_ES_ResourceID_t TskId;
char FullName[(OS_MAX_API_NAME * 2)];
if(PipeNameBuf == NULL || PipeNameSize == 0) {
CFE_ES_GetTaskID(&TskId);
CFE_EVS_SendEventWithAppID(CFE_SB_GETPIPENAME_NULL_PTR_EID, CFE_EVS_EventType_ERROR,
CFE_SB.AppId, "Pipe Name Error:NullPtr,Requestor %s",
CFE_SB_GetAppTskName(TskId,FullName));
Status = CFE_SB_BAD_ARGUMENT;
} else if(PipeId >= CFE_PLATFORM_SB_MAX_PIPES){
CFE_ES_GetTaskID(&TskId);
CFE_EVS_SendEventWithAppID(CFE_SB_GETPIPENAME_ID_ERR_EID, CFE_EVS_EventType_ERROR,
CFE_SB.AppId, "Pipe Id Error:Bad Argument,Id=%d,Requestor %s",
PipeId,CFE_SB_GetAppTskName(TskId,FullName));
memset(PipeNameBuf, 0, PipeNameSize);
Status = CFE_SB_BAD_ARGUMENT;
}else{
if (OS_QueueGetInfo(CFE_SB.PipeTbl[PipeId].SysQueueId, &queue_prop)
== OS_SUCCESS){
strncpy(PipeNameBuf, queue_prop.name, PipeNameSize-1);
PipeNameBuf[PipeNameSize-1] = '\0';
CFE_EVS_SendEventWithAppID(CFE_SB_GETPIPENAME_EID,
CFE_EVS_EventType_DEBUG,CFE_SB.AppId,
"GetPipeName name=%s id=%d",
PipeNameBuf, PipeId);
} else{
CFE_ES_GetTaskID(&TskId);
CFE_EVS_SendEventWithAppID(CFE_SB_GETPIPENAME_ID_ERR_EID, CFE_EVS_EventType_ERROR,
CFE_SB.AppId, "Pipe Id Error:Bad Argument,Id=%d,Requestor %s",
PipeId,CFE_SB_GetAppTskName(TskId,FullName));
memset(PipeNameBuf, 0, PipeNameSize);
Status = CFE_SB_BAD_ARGUMENT;
}/* end if */
}/* end if */
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)
{
uint8 PipeTblIdx = 0;
int32 Status = CFE_SUCCESS;
int32 RtnFromVal = 0;
CFE_ES_ResourceID_t TskId;
osal_id_t QueueId;
char FullName[(OS_MAX_API_NAME * 2)];
/* get TaskId of caller for events */
CFE_ES_GetTaskID(&TskId);
if(PipeName == NULL || PipeIdPtr == NULL)
{
CFE_SB.HKTlmMsg.Payload.GetPipeIdByNameErrorCounter++;
CFE_EVS_SendEventWithAppID(CFE_SB_GETPIPEIDBYNAME_NULL_ERR_EID, CFE_EVS_EventType_ERROR, CFE_SB.AppId,
"Pipe ID By Name Error:Bad Argument,Requestor %s",
CFE_SB_GetAppTskName(TskId,FullName));
Status = CFE_SB_BAD_ARGUMENT;
}
else
{
RtnFromVal = OS_QueueGetIdByName(&QueueId, PipeName);
if(RtnFromVal == OS_SUCCESS)
{
/* take semaphore to prevent a task switch while iterating
* through the PipeTbl.
*/
CFE_SB_LockSharedData(__func__,__LINE__);
for(PipeTblIdx = 0;
PipeTblIdx < CFE_PLATFORM_SB_MAX_PIPES;
PipeTblIdx++)
{
if(CFE_SB.PipeTbl[PipeTblIdx].InUse != 0
&& OS_ObjectIdEqual(CFE_SB.PipeTbl[PipeTblIdx].SysQueueId, QueueId))
{
/* grab the ID before we release the lock */
*PipeIdPtr = CFE_SB.PipeTbl[PipeTblIdx].PipeId;
break;
}/* end if */
}/* end for */
CFE_SB_UnlockSharedData(__func__,__LINE__);
if(PipeTblIdx == CFE_PLATFORM_SB_MAX_PIPES)
{
/* should never get here! */
CFE_SB.HKTlmMsg.Payload.GetPipeIdByNameErrorCounter++;
CFE_EVS_SendEventWithAppID(CFE_SB_GETPIPEIDBYNAME_NAME_ERR_EID,
CFE_EVS_EventType_ERROR, CFE_SB.AppId,
"Pipe ID By Name Error:Bad Argument,Requestor %s",
CFE_SB_GetAppTskName(TskId,FullName));
Status = CFE_SB_BAD_ARGUMENT;
}
else
{
CFE_EVS_SendEventWithAppID(CFE_SB_GETPIPEIDBYNAME_EID,
CFE_EVS_EventType_DEBUG,CFE_SB.AppId,
"PipeIdByName name=%s id=%d",
PipeName, *PipeIdPtr);
Status = CFE_SUCCESS;
}
}
else
{
CFE_SB.HKTlmMsg.Payload.GetPipeIdByNameErrorCounter++;
CFE_EVS_SendEventWithAppID(CFE_SB_GETPIPEIDBYNAME_NAME_ERR_EID,
CFE_EVS_EventType_ERROR, CFE_SB.AppId,
"Pipe ID By Name Error:Bad Argument,Requestor %s",
CFE_SB_GetAppTskName(TskId,FullName));
Status = CFE_SB_BAD_ARGUMENT;
}/* end if */
}/* end if */
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_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_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_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;
int32 Stat;
CFE_ES_ResourceID_t TskId;
CFE_ES_ResourceID_t AppId;
uint8 PipeIdx;
CFE_SB_DestinationD_t *DestPtr = NULL;
uint32 DestCount = 0;
char FullName[(OS_MAX_API_NAME * 2)];
char PipeName[OS_MAX_API_NAME] = {'\0'};
uint32 Collisions = 0;
CFE_SB_GetPipeName(PipeName, sizeof(PipeName), PipeId);
/* 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 */
PipeIdx = CFE_SB_GetPipeIdx(PipeId);
if(PipeIdx==CFE_SB_INVALID_PIPE){
CFE_SB.HKTlmMsg.Payload.SubscribeErrorCounter++;
CFE_SB_UnlockSharedData(__func__,__LINE__);
CFE_EVS_SendEventWithAppID(CFE_SB_SUB_INV_PIPE_EID,CFE_EVS_EventType_ERROR,CFE_SB.AppId,
"Subscribe Err:Invalid Pipe Id,Msg=0x%x,PipeId=%d,App %s",(unsigned int)CFE_SB_MsgIdToValue(MsgId),
(int)PipeId, CFE_SB_GetAppTskName(TskId,FullName));
return CFE_SB_BAD_ARGUMENT;
}/* end if */
/* check that the requestor is the owner of the pipe */
if( !CFE_ES_ResourceID_Equal(CFE_SB.PipeTbl[PipeIdx].AppId, AppId)){
CFE_SB.HKTlmMsg.Payload.SubscribeErrorCounter++;
CFE_SB_UnlockSharedData(__func__,__LINE__);
CFE_EVS_SendEventWithAppID(CFE_SB_SUB_INV_CALLER_EID,CFE_EVS_EventType_ERROR,CFE_SB.AppId,
"Subscribe Err:Caller(%s) is not the owner of pipe %d,Msg=0x%x",
CFE_SB_GetAppTskName(TskId,FullName),(int)PipeId,(unsigned int)CFE_SB_MsgIdToValue(MsgId));
return CFE_SB_BAD_ARGUMENT;
}/* end if */
/* check message id key and scope */
if(!CFE_SB_IsValidMsgId(MsgId) || (Scope > 1))
{
CFE_SB.HKTlmMsg.Payload.SubscribeErrorCounter++;
CFE_SB_UnlockSharedData(__func__,__LINE__);
CFE_EVS_SendEventWithAppID(CFE_SB_SUB_ARG_ERR_EID,CFE_EVS_EventType_ERROR,CFE_SB.AppId,
"Subscribe Err:Bad Arg,MsgId 0x%x,PipeId %d,app %s,scope %d",
(unsigned int)CFE_SB_MsgIdToValue(MsgId),
(int)PipeId,CFE_SB_GetAppTskName(TskId,FullName),Scope);
return CFE_SB_BAD_ARGUMENT;
}/* end if */
RouteId = CFE_SBR_GetRouteId(MsgId);
if (CFE_SBR_IsValidRouteId(RouteId))
{
/* check for duplicate subscription */
if(CFE_SB_GetDestPtr(RouteId, PipeId) != NULL)
{
CFE_SB.HKTlmMsg.Payload.DuplicateSubscriptionsCounter++;
CFE_SB_UnlockSharedData(__func__,__LINE__);
CFE_EVS_SendEventWithAppID(CFE_SB_DUP_SUBSCRIP_EID,CFE_EVS_EventType_INFORMATION,CFE_SB.AppId,
"Duplicate Subscription,MsgId 0x%x on %s pipe,app %s",
(unsigned int)CFE_SB_MsgIdToValue(MsgId),
PipeName,CFE_SB_GetAppTskName(TskId,FullName));
return CFE_SUCCESS;
}
/* Check for destination limit */
for (DestPtr = CFE_SBR_GetDestListHeadPtr(RouteId); DestPtr != NULL; DestPtr = DestPtr->Next)
{
DestCount++;
}
if(DestCount >= CFE_PLATFORM_SB_MAX_DEST_PER_PKT){
CFE_SB_UnlockSharedData(__func__,__LINE__);
CFE_EVS_SendEventWithAppID(CFE_SB_MAX_DESTS_MET_EID,CFE_EVS_EventType_ERROR,CFE_SB.AppId,
"Subscribe Err:Max Dests(%d)In Use For Msg 0x%x,pipe %s,app %s",
CFE_PLATFORM_SB_MAX_DEST_PER_PKT,
(unsigned int)CFE_SB_MsgIdToValue(MsgId),
PipeName, CFE_SB_GetAppTskName(TskId,FullName));
return CFE_SB_MAX_DESTS_MET;
}
}
else
{
/* Add the route */
RouteId = CFE_SBR_AddRoute(MsgId, &Collisions);
/* if all routing table elements are used, send event */
if(!CFE_SBR_IsValidRouteId(RouteId)){
CFE_SB_UnlockSharedData(__func__,__LINE__);
CFE_EVS_SendEventWithAppID(CFE_SB_MAX_MSGS_MET_EID,CFE_EVS_EventType_ERROR,CFE_SB.AppId,
"Subscribe Err:Max Msgs(%d)In Use,MsgId 0x%x,pipe %s,app %s",
CFE_PLATFORM_SB_MAX_MSG_IDS,
(unsigned int)CFE_SB_MsgIdToValue(MsgId),
PipeName,CFE_SB_GetAppTskName(TskId,FullName));
return CFE_SB_MAX_MSGS_MET;
}/* end if */
/* Increment the MsgIds in use ctr and if it's > the high water mark,*/
/* adjust the high water mark */
CFE_SB.StatTlmMsg.Payload.MsgIdsInUse++;
if(CFE_SB.StatTlmMsg.Payload.MsgIdsInUse > CFE_SB.StatTlmMsg.Payload.PeakMsgIdsInUse){
CFE_SB.StatTlmMsg.Payload.PeakMsgIdsInUse = CFE_SB.StatTlmMsg.Payload.MsgIdsInUse;
}/* end if */
}/* end if */
DestPtr = CFE_SB_GetDestinationBlk();
if(DestPtr == NULL){
CFE_SB_UnlockSharedData(__func__,__LINE__);
CFE_EVS_SendEventWithAppID(CFE_SB_DEST_BLK_ERR_EID,CFE_EVS_EventType_ERROR,CFE_SB.AppId,
"Subscribe Err:Request for Destination Blk failed for Msg 0x%x",
(unsigned int)CFE_SB_MsgIdToValue(MsgId));
return CFE_SB_BUF_ALOC_ERR;
}/* end if */
/* initialize destination block */
DestPtr->PipeId = PipeId;
DestPtr->MsgId2PipeLim = (uint16)MsgLim;
DestPtr->Active = CFE_SB_ACTIVE;
DestPtr->BuffCount = 0;
DestPtr->DestCnt = 0;
DestPtr->Scope = Scope;
DestPtr->Prev = NULL;
DestPtr->Next = NULL;
/* add destination node */
CFE_SB_AddDestNode(RouteId, DestPtr);
CFE_SB.StatTlmMsg.Payload.SubscriptionsInUse++;
if(CFE_SB.StatTlmMsg.Payload.SubscriptionsInUse > CFE_SB.StatTlmMsg.Payload.PeakSubscriptionsInUse)
{
CFE_SB.StatTlmMsg.Payload.PeakSubscriptionsInUse = CFE_SB.StatTlmMsg.Payload.SubscriptionsInUse;
}/* end if */
if((CFE_SB.SubscriptionReporting == CFE_SB_ENABLE)&&(Scope==CFE_SB_GLOBAL)){
CFE_SB.SubRprtMsg.Payload.MsgId = MsgId;
CFE_SB.SubRprtMsg.Payload.Pipe = PipeId;
CFE_SB.SubRprtMsg.Payload.Qos.Priority = Quality.Priority;
CFE_SB.SubRprtMsg.Payload.Qos.Reliability = Quality.Reliability;
CFE_SB.SubRprtMsg.Payload.SubType = CFE_SB_SUBSCRIPTION;
CFE_SB_UnlockSharedData(__func__,__LINE__);
Stat = CFE_SB_TransmitMsg(&CFE_SB.SubRprtMsg.Hdr.Msg, true);
CFE_EVS_SendEventWithAppID(CFE_SB_SUBSCRIPTION_RPT_EID,CFE_EVS_EventType_DEBUG,CFE_SB.AppId,
"Sending Subscription Report Msg=0x%x,Pipe=%d,Stat=0x%x",
(unsigned int)CFE_SB_MsgIdToValue(MsgId),
(int)PipeId,(unsigned int)Stat);
CFE_SB_LockSharedData(__func__,__LINE__);/* to prevent back-to-back unlock */
}/* end if */
/* release the semaphore */
CFE_SB_UnlockSharedData(__func__,__LINE__);
CFE_EVS_SendEventWithAppID(CFE_SB_SUBSCRIPTION_RCVD_EID,CFE_EVS_EventType_DEBUG,CFE_SB.AppId,
"Subscription Rcvd:MsgId 0x%x on %s(%d),app %s",
(unsigned int)CFE_SB_MsgIdToValue(MsgId),
PipeName,(int)PipeId,CFE_SB_GetAppTskName(TskId,FullName));
if (Collisions != 0)
{
CFE_EVS_SendEventWithAppID(CFE_SB_HASHCOLLISION_EID, CFE_EVS_EventType_DEBUG, CFE_SB.AppId,
"Msg hash collision: MsgId = 0x%x, collisions = %u",
(unsigned int)CFE_SB_MsgIdToValue(MsgId), (unsigned int)Collisions);
}
return CFE_SUCCESS;
}/* end CFE_SB_SubscribeFull */
/*
* Function: CFE_SB_Unsubscribe - See API and header file for details
*/
int32 CFE_SB_Unsubscribe(CFE_SB_MsgId_t MsgId, CFE_SB_PipeId_t PipeId)
{
CFE_ES_ResourceID_t CallerId;
int32 Status = 0;
/* get the callers Application Id */
CFE_ES_GetAppID(&CallerId);
Status = CFE_SB_UnsubscribeFull(MsgId, PipeId, (uint8)CFE_SB_GLOBAL,CallerId);
return Status;
}/* end CFE_SB_Unsubscribe */
/*
* Function: CFE_SB_UnsubscribeLocal - See API and header file for details
*/
int32 CFE_SB_UnsubscribeLocal(CFE_SB_MsgId_t MsgId, CFE_SB_PipeId_t PipeId)
{
CFE_ES_ResourceID_t CallerId;
int32 Status = 0;
/* get the callers Application Id */
CFE_ES_GetAppID(&CallerId);
Status = CFE_SB_UnsubscribeFull(MsgId, PipeId, (uint8)CFE_SB_LOCAL,CallerId);
return Status;
}/* end CFE_SB_UnsubscribeLocal */
/******************************************************************************
** Name: CFE_SB_UnsubscribeAppId
**
** Purpose: CFE Internal API intented to be called by CFE_ES when an applications
** SB resources need to be freed. The regular unsibscribe api won't work
** because it does a check to ensure the caller is the owner of the pipe.
**
** Date Written:
** 03/15/2007
**
** Input Arguments:
** MsgId
** PipeId
** AppId
**
** Output Arguments:
** None
**
** Return Values:
** Status
**
******************************************************************************/
int32 CFE_SB_UnsubscribeWithAppId(CFE_SB_MsgId_t MsgId,
CFE_SB_PipeId_t PipeId,
CFE_ES_ResourceID_t AppId)
{
int32 Status = 0;
Status = CFE_SB_UnsubscribeFull(MsgId, PipeId, (uint8)CFE_SB_LOCAL, AppId);
return Status;
}/* end CFE_SB_UnsubscribeWithAppId */
/******************************************************************************
** Name: CFE_SB_UnsubscribeFull
**
** Purpose: CFE Internal API used to unsubscribe to a message.
**
** Assumptions, External Events, and Notes:
**
**
** Notes:This function cannot be called directly,it would not be semaphore protected.
** Also,if more than one subscription is found, this function will remove all
** entries that match.
**
** Date Written:
** 04/25/2005
**
** Input Arguments:
** MsgId
** PipeId
** Scope
** AppId
**
** Output Arguments:
** None