-
Notifications
You must be signed in to change notification settings - Fork 202
/
cfe_sb_priv.c
562 lines (477 loc) · 16.9 KB
/
cfe_sb_priv.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
/*
** 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_priv.c
**
** Purpose:
** This header file contains prototypes for private functions and type
** definitions for cFE internal use.
**
** Author: R.McGraw/SSI
**
** 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.
**
** Some functions have EXTERNAL SYNC REQUIREMENTS
**
** SB functions marked with "Unsync" in their name are designated
** as functions which are _not_ safe to be called concurrently by multiple
** threads, and also do _not_ implement any locking or protection. These
** functions expect the caller to perform all thread synchronization before
** calling it.
**
** The synchronization requirement is across all functions; i.e. it is not safe
** to call B_Unsync() while A_Unsync() is executing or vice-versa. The external
** lock must wait until A_Unsync() finishes before calling B_Unsync().
**
** The expectation is that the required level of synchronization can be achieved
** using the SB shared data lock.
**
******************************************************************************/
/*
** Include Files
*/
#include "cfe_sb_module_all.h"
#include <string.h>
/******************************************************************************
** Function: CFE_SB_CleanUpApp()
**
** Purpose:
**
** Arguments:
**
** Return:
** None
*/
int32 CFE_SB_CleanUpApp(CFE_ES_AppId_t AppId)
{
uint32 i;
uint32 DelCount;
CFE_SB_PipeD_t *PipeDscPtr;
CFE_SB_PipeId_t DelList[CFE_PLATFORM_SB_MAX_PIPES];
PipeDscPtr = CFE_SB_Global.PipeTbl;
DelCount = 0;
CFE_SB_LockSharedData(__func__, __LINE__);
/* loop through the pipe table looking for pipes owned by AppId */
for (i = 0; i < CFE_PLATFORM_SB_MAX_PIPES; ++i)
{
if (CFE_SB_PipeDescIsUsed(PipeDscPtr) && CFE_RESOURCEID_TEST_EQUAL(PipeDscPtr->AppId, AppId))
{
DelList[DelCount] = CFE_SB_PipeDescGetID(PipeDscPtr);
++DelCount;
}
++PipeDscPtr;
}
CFE_SB_UnlockSharedData(__func__, __LINE__);
for (i = 0; i < DelCount; ++i)
{
CFE_SB_DeletePipeWithAppId(DelList[i], AppId);
}
/* Release any zero copy buffers */
CFE_SB_ZeroCopyReleaseAppId(AppId);
return CFE_SUCCESS;
} /* end CFE_SB_CleanUpApp */
/******************************************************************************
** Function: CFE_SB_LockSharedData()
**
** Purpose:
** SB internal function to handle a semaphore take failure for the Shared
** Data Mutex
**
** Arguments:
** FuncName - the function name containing the code that generated the error.
** LineNumber - the line number in the file of the code that generated the error.
**
** Return:
** None
*/
void CFE_SB_LockSharedData(const char *FuncName, int32 LineNumber)
{
int32 Status;
CFE_ES_AppId_t AppId;
Status = OS_MutSemTake(CFE_SB_Global.SharedDataMutexId);
if (Status != OS_SUCCESS)
{
CFE_ES_GetAppID(&AppId);
CFE_ES_WriteToSysLog("SB SharedData Mutex Take Err Stat=0x%x,App=%lu,Func=%s,Line=%d\n", (unsigned int)Status,
CFE_RESOURCEID_TO_ULONG(AppId), FuncName, (int)LineNumber);
} /* end if */
return;
} /* end CFE_SB_LockSharedData */
/******************************************************************************
** Function: CFE_SB_UnlockSharedData()
**
** Purpose:
** SB internal function to handle a semaphore give failure for the Shared
** Data Mutex
**
** Arguments:
** FuncName - the function name containing the code that generated the error.
** LineNumber - the line number in the file of the code that generated the error.
**
** Return:
** None
*/
void CFE_SB_UnlockSharedData(const char *FuncName, int32 LineNumber)
{
int32 Status;
CFE_ES_AppId_t AppId;
Status = OS_MutSemGive(CFE_SB_Global.SharedDataMutexId);
if (Status != OS_SUCCESS)
{
CFE_ES_GetAppID(&AppId);
CFE_ES_WriteToSysLog("SB SharedData Mutex Give Err Stat=0x%x,App=%lu,Func=%s,Line=%d\n", (unsigned int)Status,
CFE_RESOURCEID_TO_ULONG(AppId), FuncName, (int)LineNumber);
} /* end if */
return;
} /* end CFE_SB_UnlockSharedData */
/******************************************************************************
* SB private function to get destination pointer - see description in header
*/
CFE_SB_DestinationD_t *CFE_SB_GetDestPtr(CFE_SBR_RouteId_t RouteId, CFE_SB_PipeId_t PipeId)
{
CFE_SB_DestinationD_t *destptr;
destptr = CFE_SBR_GetDestListHeadPtr(RouteId);
/* Check all destinations */
while (destptr != NULL)
{
if (CFE_RESOURCEID_TEST_EQUAL(destptr->PipeId, PipeId))
{
break;
}
destptr = destptr->Next;
}
return destptr;
}
/******************************************************************************
** Function: CFE_SB_ValidateMsgId()
**
** Purpose:
** SB internal function to validate a given MsgId.
**
** Arguments:
**
** Return:
** None
*/
int32 CFE_SB_ValidateMsgId(CFE_SB_MsgId_t MsgId)
{
if (!CFE_SB_IsValidMsgId(MsgId))
{
return CFE_SB_FAILED;
}
else
{
return CFE_SUCCESS;
} /* end if */
} /* end CFE_SB_ValidateMsgId */
/*********************************************************************/
/*
* CFE_SB_LocatePipeDescByID
*
* For complete API information, see prototype in header
*/
CFE_SB_PipeD_t *CFE_SB_LocatePipeDescByID(CFE_SB_PipeId_t PipeId)
{
CFE_SB_PipeD_t *PipeDscPtr;
uint32 Idx;
if (CFE_SB_PipeId_ToIndex(PipeId, &Idx) == CFE_SUCCESS)
{
PipeDscPtr = &CFE_SB_Global.PipeTbl[Idx];
}
else
{
PipeDscPtr = NULL;
}
return PipeDscPtr;
}
/*********************************************************************/
/*
* CFE_SB_CheckPipeDescSlotUsed
*
* Checks if a table slot is used or not (helper for allocating IDs)
*/
bool CFE_SB_CheckPipeDescSlotUsed(CFE_ResourceId_t CheckId)
{
CFE_SB_PipeD_t *PipeDscPtr;
/*
* Note - The pointer here should never be NULL because the ID should always be
* within the expected range, but if it ever is NULL, this should return true
* such that the caller will _not_ attempt to use the record.
*/
PipeDscPtr = CFE_SB_LocatePipeDescByID(CFE_SB_PIPEID_C(CheckId));
return (PipeDscPtr == NULL || CFE_SB_PipeDescIsUsed(PipeDscPtr));
}
/******************************************************************************
** Function: CFE_SB_GetAppTskName()
**
** Purpose:
** This function returns a pointer to the app.tsk name string
**
** Arguments:
** TaskId - the task id of the app.task name desired
** FullName - string buffer to store name
**
** Return:
** Pointer to App.Tsk Name
**
** Note: With taskId, Parent App name and Child Task name can be queried from ES
**
*/
char *CFE_SB_GetAppTskName(CFE_ES_TaskId_t TaskId, char *FullName)
{
CFE_ES_TaskInfo_t TaskInfo;
CFE_ES_TaskInfo_t *ptr = &TaskInfo;
char AppName[OS_MAX_API_NAME];
char TskName[OS_MAX_API_NAME];
if (CFE_ES_GetTaskInfo(ptr, TaskId) != CFE_SUCCESS)
{
/* unlikely, but possible if TaskId is bogus */
strncpy(FullName, "Unknown", OS_MAX_API_NAME - 1);
FullName[OS_MAX_API_NAME - 1] = '\0';
}
else if (strncmp((char *)ptr->AppName, (char *)ptr->TaskName, sizeof(ptr->AppName)) == 0)
{
/* if app name and task name are the same */
strncpy(FullName, (char *)ptr->AppName, OS_MAX_API_NAME - 1);
FullName[OS_MAX_API_NAME - 1] = '\0';
}
else
{
/* AppName and TskName buffers and strncpy are needed to limit string sizes */
strncpy(AppName, (char *)ptr->AppName, sizeof(AppName) - 1);
AppName[sizeof(AppName) - 1] = '\0';
strncpy(TskName, (char *)ptr->TaskName, sizeof(TskName) - 1);
TskName[sizeof(TskName) - 1] = '\0';
sprintf(FullName, "%s.%s", AppName, TskName);
} /* end if */
return FullName;
} /* end CFE_SB_GetAppTskName */
/******************************************************************************
** Function: CFE_SB_RequestToSendEvent()
**
** Purpose:
** This function will test the given bit for the given task. If the bit is set
** this function will return CFE_SB_DENIED. If bit is not set, this function set
** the bit and return CFE_SB_GRANTED. This will prevent recursive events from
** occurring.
**
** Arguments:
**
** Return:
** If the bit is set this function will return CFE_SB_DENIED.
** If bit is not set, this function set the bit and return CFE_SB_GRANTED.
*/
uint32 CFE_SB_RequestToSendEvent(CFE_ES_TaskId_t TaskId, uint32 Bit)
{
uint32 Indx;
if (CFE_ES_TaskID_ToIndex(TaskId, &Indx) != CFE_SUCCESS)
{
return CFE_SB_DENIED;
}
/* if bit is set... */
if (CFE_TST(CFE_SB_Global.StopRecurseFlags[Indx], Bit))
{
return CFE_SB_DENIED;
}
else
{
CFE_SET(CFE_SB_Global.StopRecurseFlags[Indx], Bit);
return CFE_SB_GRANTED;
} /* end if */
} /* end CFE_SB_RequestToSendEvent */
/******************************************************************************
** Function: CFE_SB_FinishSendEvent()
**
** Purpose:
** This function will clear the given bit for the given task. Called after
** a successful CFE_SB_RequestToSendEvent()
**
** Arguments:
**
** Return:
** If the bit is set this function will return CFE_SB_DENIED.
** If bit is not set, this function set the bit and return CFE_SB_GRANTED.
*/
void CFE_SB_FinishSendEvent(CFE_ES_TaskId_t TaskId, uint32 Bit)
{
uint32 Indx;
if (CFE_ES_TaskID_ToIndex(TaskId, &Indx) != CFE_SUCCESS)
{
return;
}
/* clear the bit so the task may send this event again */
CFE_CLR(CFE_SB_Global.StopRecurseFlags[Indx], Bit);
} /* end CFE_SB_RequestToSendEvent */
/******************************************************************************
* SB private function to add a destination node - see description in header
*/
int32 CFE_SB_AddDestNode(CFE_SBR_RouteId_t RouteId, CFE_SB_DestinationD_t *NewNode)
{
CFE_SB_DestinationD_t *WBS; /* Will Be Second (WBS) node */
CFE_SB_DestinationD_t *listheadptr;
listheadptr = CFE_SBR_GetDestListHeadPtr(RouteId);
/* if first node in list */
if (listheadptr == NULL)
{
/* initialize the new node */
NewNode->Next = NULL;
NewNode->Prev = NULL;
}
else
{
WBS = listheadptr;
/* initialize the new node */
NewNode->Next = WBS;
NewNode->Prev = NULL;
/* insert the new node */
WBS->Prev = NewNode;
}
/* Update Head */
CFE_SBR_SetDestListHeadPtr(RouteId, NewNode);
return CFE_SUCCESS;
}
/******************************************************************************
* SB private function to remove a destination - see description in header
*/
void CFE_SB_RemoveDest(CFE_SBR_RouteId_t RouteId, CFE_SB_DestinationD_t *DestPtr)
{
CFE_SB_RemoveDestNode(RouteId, DestPtr);
CFE_SB_PutDestinationBlk(DestPtr);
CFE_SB_Global.StatTlmMsg.Payload.SubscriptionsInUse--;
}
/******************************************************************************
* SB private function to remove a destination node - see description in header
*/
void CFE_SB_RemoveDestNode(CFE_SBR_RouteId_t RouteId, CFE_SB_DestinationD_t *NodeToRemove)
{
CFE_SB_DestinationD_t *PrevNode;
CFE_SB_DestinationD_t *NextNode;
if ((NodeToRemove->Prev == NULL) && (NodeToRemove->Next == NULL))
{
/* Clear destinations if this is the only node in the list */
CFE_SBR_SetDestListHeadPtr(RouteId, NULL);
}
else if (NodeToRemove->Prev == NULL)
{
/* First in the list, set the next node to list head */
NextNode = NodeToRemove->Next;
NextNode->Prev = NULL;
CFE_SBR_SetDestListHeadPtr(RouteId, NextNode);
}
else if (NodeToRemove->Next == NULL)
{
/* Last in the list, remove previous pointer */
PrevNode = NodeToRemove->Prev;
PrevNode->Next = NULL;
}
else
{
/* Middle of list, remove */
PrevNode = NodeToRemove->Prev;
NextNode = NodeToRemove->Next;
PrevNode->Next = NextNode;
NextNode->Prev = PrevNode;
}
/* initialize the node before returning it to the heap */
NodeToRemove->Next = NULL;
NodeToRemove->Prev = NULL;
}
/******************************************************************************
** Name: CFE_SB_ZeroCopyReleaseAppId
**
** Purpose: API used for releasing all pointers to a buffers (for zero copy mode
** only) for a specific Application. This function is used for cleaning
** up when an application crashes.
**
** Assumptions, External Events, and Notes:
** None
**
** Date Written:
** 07/23/2009
**
** Input Arguments:
** AppId
**
** Output Arguments:
** None
**
** Return Values:
** Status
**
******************************************************************************/
int32 CFE_SB_ZeroCopyReleaseAppId(CFE_ES_AppId_t AppId)
{
CFE_SB_BufferLink_t *NextLink;
CFE_SB_BufferD_t * DscPtr;
/*
* First go through the "ZeroCopy" tracking list and find all nodes
* with a matching AppID. This needs to be done while locked to
* prevent other tasks from changing the list at the same time.
*/
if (CFE_RESOURCEID_TEST_DEFINED(AppId))
{
CFE_SB_LockSharedData(__func__, __LINE__);
/* Get start of list */
NextLink = CFE_SB_TrackingListGetNext(&CFE_SB_Global.ZeroCopyList);
while (!CFE_SB_TrackingListIsEnd(&CFE_SB_Global.ZeroCopyList, NextLink))
{
/* Get buffer descriptor pointer */
/* NOTE: casting via void* here rather than CFE_SB_BufferD_t* avoids a false
* alignment warning on platforms with strict alignment requirements */
DscPtr = (void *)NextLink;
/* Read the next link now in case this node gets moved */
NextLink = CFE_SB_TrackingListGetNext(NextLink);
/* Check if it is a zero-copy buffer owned by this app */
if (CFE_RESOURCEID_TEST_EQUAL(DscPtr->AppId, AppId))
{
/* If so, decrement the use count as the app has now gone away */
CFE_SB_DecrBufUseCnt(DscPtr);
}
}
CFE_SB_UnlockSharedData(__func__, __LINE__);
}
return CFE_SUCCESS;
} /* end CFE_SB_ZeroCopyReleaseAppId */
/*****************************************************************************/