Skip to content

Commit

Permalink
Fix #109, Move variables declared mid-function to the top
Browse files Browse the repository at this point in the history
  • Loading branch information
thnkslprpt committed Oct 13, 2022
1 parent 3fb7ff6 commit a4e5277
Show file tree
Hide file tree
Showing 11 changed files with 327 additions and 381 deletions.
50 changes: 28 additions & 22 deletions fsw/src/cf_cfdp.c
Original file line number Diff line number Diff line change
Expand Up @@ -1011,12 +1011,15 @@ void CF_CFDP_RecvIdle(CF_Transaction_t *t, CF_Logical_PduBuffer_t *ph)
int32 CF_CFDP_InitEngine(void)
{
/* initialize all transaction nodes */
int i;
int j;
int chunk_mem_offset = 0;
CF_History_t * h;
CF_Transaction_t * t = CF_AppData.engine.transactions;
CF_ChunkWrapper_t *c = CF_AppData.engine.chunks;
int32 ret = CFE_SUCCESS;
int chunk_mem_offset = 0;
int i;
int j;
int k;
char nbuf[64];

static const int CF_DIR_MAX_CHUNKS[CF_Direction_NUM][CF_NUM_CHANNELS] = {CF_CHANNEL_NUM_RX_CHUNKS_PER_TRANSACTION,
CF_CHANNEL_NUM_TX_CHUNKS_PER_TRANSACTION};
Expand All @@ -1025,7 +1028,6 @@ int32 CF_CFDP_InitEngine(void)

for (i = 0; i < CF_NUM_CHANNELS; ++i)
{
char nbuf[64];
snprintf(nbuf, sizeof(nbuf) - 1, "%s%d", CF_CHANNEL_PIPE_PREFIX, i);
ret = CFE_SB_CreatePipe(&CF_AppData.engine.channels[i].pipe, CF_AppData.config_table->chan[i].pipe_depth_input,
nbuf);
Expand Down Expand Up @@ -1062,8 +1064,6 @@ int32 CF_CFDP_InitEngine(void)

for (j = 0; j < CF_NUM_TRANSACTIONS_PER_CHANNEL; ++j, ++t)
{
int k;

t->chan_num = i;
CF_FreeTransaction(t);

Expand All @@ -1079,7 +1079,7 @@ int32 CF_CFDP_InitEngine(void)

for (j = 0; j < CF_NUM_HISTORIES_PER_CHANNEL; ++j)
{
CF_History_t *h = &CF_AppData.engine.histories[(i * CF_NUM_HISTORIES_PER_CHANNEL) + j];
h = &CF_AppData.engine.histories[(i * CF_NUM_HISTORIES_PER_CHANNEL) + j];
CF_CList_InitNode(&h->cl_node);
CF_CList_InsertBack_Ex(&CF_AppData.engine.channels[i], CF_QueueIdx_HIST_FREE, &h->cl_node);
}
Expand Down Expand Up @@ -1454,15 +1454,14 @@ int32 CF_CFDP_PlaybackDir(const char *src_filename, const char *dst_filename, CF
*-----------------------------------------------------------------*/
void CF_CFDP_ProcessPlaybackDirectory(CF_Channel_t *c, CF_Playback_t *p)
{
os_dirent_t dirent;
/* either there's no transaction (first one) or the last one was finished, so check for a new one */
os_dirent_t dirent;
CF_Transaction_t *pt;
int32 status; /* either there's no transaction (first one) or the last one was finished, so check for a new one */

memset(&dirent, 0, sizeof(dirent));

while (p->diropen && (p->num_ts < CF_NUM_TRANSACTIONS_PER_PLAYBACK))
{
int32 status;

CFE_ES_PerfLogEntry(CF_PERF_ID_DIRREAD);
status = OS_DirectoryRead(p->dir_id, &dirent);
CFE_ES_PerfLogExit(CF_PERF_ID_DIRREAD);
Expand All @@ -1475,7 +1474,7 @@ void CF_CFDP_ProcessPlaybackDirectory(CF_Channel_t *c, CF_Playback_t *p)
}

{
CF_Transaction_t *pt = CF_FindUnusedTransaction(c);
pt = CF_FindUnusedTransaction(c);
CF_Assert(pt); /* should be impossible not to have one because there are limits on the number of uses of
them */

Expand Down Expand Up @@ -1568,15 +1567,21 @@ static void CF_CFDP_ProcessPlaybackDirectories(CF_Channel_t *c)
*-----------------------------------------------------------------*/
void CF_CFDP_ProcessPollingDirectories(CF_Channel_t *c)
{
int i;
CF_Poll_t * p;
CF_ChannelConfig_t *cc;
CF_PollDir_t * pd;
int i;
int chan_index;
int count_check;
int ret;

for (i = 0; i < CF_MAX_POLLING_DIR_PER_CHAN; ++i)
{
CF_Poll_t * p = &c->poll[i];
int chan_index = (c - CF_AppData.engine.channels);
CF_ChannelConfig_t *cc = &CF_AppData.config_table->chan[chan_index];
CF_PollDir_t * pd = &cc->polldir[i];
int count_check = 0;
p = &c->poll[i];
chan_index = (c - CF_AppData.engine.channels);
cc = &CF_AppData.config_table->chan[chan_index];
pd = &cc->polldir[i];
count_check = 0;

if (pd->enabled && pd->interval_sec)
{
Expand All @@ -1592,8 +1597,8 @@ void CF_CFDP_ProcessPollingDirectories(CF_Channel_t *c)
else if (CF_Timer_Expired(&p->interval_timer))
{
/* the timer has expired */
int ret = CF_CFDP_PlaybackDir_Initiate(&p->pb, pd->src_dir, pd->dst_dir, pd->cfdp_class, 0,
chan_index, pd->priority, pd->dest_eid);
ret = CF_CFDP_PlaybackDir_Initiate(&p->pb, pd->src_dir, pd->dst_dir, pd->cfdp_class, 0, chan_index,
pd->priority, pd->dest_eid);
if (!ret)
{
p->timer_set = 0;
Expand Down Expand Up @@ -1632,13 +1637,14 @@ void CF_CFDP_ProcessPollingDirectories(CF_Channel_t *c)
*-----------------------------------------------------------------*/
void CF_CFDP_CycleEngine(void)
{
int i;
CF_Channel_t *c;
int i;

if (CF_AppData.engine.enabled)
{
for (i = 0; i < CF_NUM_CHANNELS; ++i)
{
CF_Channel_t *c = &CF_AppData.engine.channels[i];
c = &CF_AppData.engine.channels[i];
CF_AppData.engine.outgoing_counter = 0;

/* consume all received messages, even if channel is frozen */
Expand Down
28 changes: 15 additions & 13 deletions fsw/src/cf_cfdp_r.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,9 @@ int CF_CFDP_R_CheckCrc(CF_Transaction_t *t, uint32 expected_crc)
*-----------------------------------------------------------------*/
void CF_CFDP_R2_Complete(CF_Transaction_t *t, int ok_to_send_nak)
{
int send_nak = 0;
int send_fin = 0;
uint32 ret;
int send_nak = 0;
int send_fin = 0;
/* checking if r2 is complete. Check nak list, and send NAK if appropriate */
/* if all data is present, then there will be no gaps in the chunk */

Expand All @@ -139,7 +140,7 @@ void CF_CFDP_R2_Complete(CF_Transaction_t *t, int ok_to_send_nak)
else
{
/* only look for 1 gap, since the goal here is just to know that there are gaps */
uint32 ret = CF_ChunkList_ComputeGaps(&t->chunks->chunks, 1, t->fsize, 0, NULL, NULL);
ret = CF_ChunkList_ComputeGaps(&t->chunks->chunks, 1, t->fsize, 0, NULL, NULL);

if (ret)
{
Expand Down Expand Up @@ -509,8 +510,8 @@ int CF_CFDP_R_SubstateSendNak(CF_Transaction_t *t)
CF_AppData.config_table->local_eid, 1, t->history->seq_num, 1);
CF_Logical_PduNak_t *nak;
CF_SendRet_t sret;

int ret = -1;
uint32 cret;
int ret = -1;

if (ph)
{
Expand All @@ -520,7 +521,6 @@ int CF_CFDP_R_SubstateSendNak(CF_Transaction_t *t)
{
/* we have metadata, so send valid nak */
CF_GapComputeArgs_t args = {t, nak};
uint32 cret;

nak->scope_start = 0;
cret = CF_ChunkList_ComputeGaps(&t->chunks->chunks,
Expand Down Expand Up @@ -808,7 +808,10 @@ void CF_CFDP_R2_Recv_fin_ack(CF_Transaction_t *t, CF_Logical_PduBuffer_t *ph)
*-----------------------------------------------------------------*/
void CF_CFDP_R2_RecvMd(CF_Transaction_t *t, CF_Logical_PduBuffer_t *ph)
{
bool success = true;
char fname[CF_FILENAME_MAX_LEN];
int status;
int32 ret;
bool success = true;

/* it isn't an error to get another MD pdu, right? */
if (!t->flags.rx.md_recv)
Expand All @@ -817,8 +820,6 @@ void CF_CFDP_R2_RecvMd(CF_Transaction_t *t, CF_Logical_PduBuffer_t *ph)
/* parse the md pdu. this will overwrite the transaction's history, which contains our filename. so let's
* save the filename in a local buffer so it can be used with OS_mv upon successful parsing of
* the md pdu */
char fname[CF_FILENAME_MAX_LEN];
int status;

strcpy(
fname,
Expand Down Expand Up @@ -866,7 +867,7 @@ void CF_CFDP_R2_RecvMd(CF_Transaction_t *t, CF_Logical_PduBuffer_t *ph)
}
else
{
int32 ret =
ret =
CF_WrappedOpenCreate(&t->fd, t->history->fnames.dst_filename, OS_FILE_FLAG_NONE, OS_READ_WRITE);
if (ret < 0)
{
Expand Down Expand Up @@ -1000,7 +1001,8 @@ void CF_CFDP_R_Tick(CF_Transaction_t *t, int *cont /* unused */)
* the logic by state so that it isn't a bunch of if statements for different flags
*/

bool success = true;
CF_SendRet_t sret;
bool success = true;

/* at each tick, various timers used by R are checked */
/* first, check inactivity timer */
Expand All @@ -1024,8 +1026,8 @@ void CF_CFDP_R_Tick(CF_Transaction_t *t, int *cont /* unused */)
/* rx maintenance: possibly process send_eof_ack, send_nak or send_fin */
if (t->flags.rx.send_ack)
{
CF_SendRet_t sret = CF_CFDP_SendAck(t, CF_CFDP_AckTxnStatus_ACTIVE, CF_CFDP_FileDirective_EOF,
t->state_data.r.r2.eof_cc, t->history->peer_eid, t->history->seq_num);
sret = CF_CFDP_SendAck(t, CF_CFDP_AckTxnStatus_ACTIVE, CF_CFDP_FileDirective_EOF, t->state_data.r.r2.eof_cc,
t->history->peer_eid, t->history->seq_num);
CF_Assert(sret != CF_SendRet_ERROR);

/* if CF_SendRet_SUCCESS, then move on in the state machine. CF_CFDP_SendAck does not return
Expand Down
15 changes: 8 additions & 7 deletions fsw/src/cf_cfdp_s.c
Original file line number Diff line number Diff line change
Expand Up @@ -277,11 +277,12 @@ void CF_CFDP_S_SubstateSendFileData(CF_Transaction_t *t)
int CF_CFDP_S_CheckAndRespondNak(CF_Transaction_t *t)
{
const CF_Chunk_t *c;
CF_SendRet_t sret;
int ret = 0;

if (t->flags.tx.md_need_send)
{
CF_SendRet_t sret = CF_CFDP_SendMd(t);
sret = CF_CFDP_SendMd(t);
if (sret == CF_SendRet_ERROR)
{
ret = -1; /* error occurred */
Expand Down Expand Up @@ -358,14 +359,13 @@ void CF_CFDP_S2_SubstateSendFileData(CF_Transaction_t *t)
*-----------------------------------------------------------------*/
void CF_CFDP_S_SubstateSendMetadata(CF_Transaction_t *t)
{
bool success = true;
int status = 0;
CF_SendRet_t sret;
int32 ret;
int status = 0;
bool success = true;

if (!OS_ObjectIdDefined(t->fd))
{
int32 ret;

if (OS_FileOpenCheck(t->history->fnames.src_filename) == OS_SUCCESS)
{
CFE_EVS_SendEvent(CF_EID_ERR_CFDP_S_ALREADY_OPEN, CFE_EVS_EventType_ERROR,
Expand Down Expand Up @@ -738,7 +738,8 @@ void CF_CFDP_S_Tick(CF_Transaction_t *t, int *cont /* unused */)
/* Steven is not real happy with this function. There should be a better way to separate out
* the logic by state so that it isn't a bunch of if statements for different flags
*/
bool early_exit = false;
CF_SendRet_t sret;
bool early_exit = false;

/* at each tick, various timers used by S are checked */
/* first, check inactivity timer */
Expand Down Expand Up @@ -779,7 +780,7 @@ void CF_CFDP_S_Tick(CF_Transaction_t *t, int *cont /* unused */)
}
else
{
CF_SendRet_t sret = CF_CFDP_S_SendEof(t);
sret = CF_CFDP_S_SendEof(t);
if (sret == CF_SendRet_NO_MSG)
{
early_exit = true;
Expand Down
3 changes: 1 addition & 2 deletions fsw/src/cf_cfdp_sbintf.c
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ void CF_CFDP_ReceiveMessage(CF_Channel_t *c)
CFE_MSG_Type_t msg_type = CFE_MSG_Type_Invalid;

CF_Logical_PduBuffer_t *ph;
CF_Transaction_t t_finack;

for (; count < CF_AppData.config_table->chan[chan_num].rx_max_messages_per_wakeup; ++count)
{
Expand Down Expand Up @@ -237,8 +238,6 @@ void CF_CFDP_ReceiveMessage(CF_Channel_t *c)
{
if (!CF_CFDP_RecvFin(t, ph))
{
CF_Transaction_t t_finack;

memset(&t_finack, 0, sizeof(t_finack));
CF_CFDP_InitTxnTxFile(&t_finack, CF_CFDP_CLASS_2, 1, chan_num,
0); /* populate transaction with needed fields for CF_CFDP_SendAck() */
Expand Down
14 changes: 9 additions & 5 deletions fsw/src/cf_chunk.c
Original file line number Diff line number Diff line change
Expand Up @@ -252,18 +252,22 @@ CF_ChunkIdx_t CF_Chunks_FindSmallestSize(const CF_ChunkList_t *chunks)
*-----------------------------------------------------------------*/
void CF_Chunks_Insert(CF_ChunkList_t *chunks, CF_ChunkIdx_t i, const CF_Chunk_t *chunk)
{
int n = CF_Chunks_CombineNext(chunks, i, chunk);
CF_ChunkIdx_t smallest_i;
CF_Chunk_t * smallest_c;
int n = CF_Chunks_CombineNext(chunks, i, chunk);
int combined;

if (n)
{
int combined = CF_Chunks_CombinePrevious(chunks, i, &chunks->chunks[i]);
combined = CF_Chunks_CombinePrevious(chunks, i, &chunks->chunks[i]);
if (combined)
{
CF_Chunks_EraseChunk(chunks, i);
}
}
else
{
int combined = CF_Chunks_CombinePrevious(chunks, i, chunk);
combined = CF_Chunks_CombinePrevious(chunks, i, chunk);
if (!combined)
{
if (chunks->count < chunks->max_chunks)
Expand All @@ -272,8 +276,8 @@ void CF_Chunks_Insert(CF_ChunkList_t *chunks, CF_ChunkIdx_t i, const CF_Chunk_t
}
else
{
CF_ChunkIdx_t smallest_i = CF_Chunks_FindSmallestSize(chunks);
CF_Chunk_t * smallest_c = &chunks->chunks[smallest_i];
smallest_i = CF_Chunks_FindSmallestSize(chunks);
smallest_c = &chunks->chunks[smallest_i];
if (smallest_c->size < chunk->size)
{
CF_Chunks_EraseChunk(chunks, smallest_i);
Expand Down
8 changes: 6 additions & 2 deletions fsw/src/cf_clist.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,16 @@ void CF_CList_InitNode(CF_CListNode_t *node)
*-----------------------------------------------------------------*/
void CF_CList_InsertFront(CF_CListNode_t **head, CF_CListNode_t *node)
{
CF_CListNode_t *last;

CF_Assert(head);
CF_Assert(node);
CF_Assert(node->next == node);
CF_Assert(node->prev == node);

if (*head)
{
CF_CListNode_t *last = (*head)->prev;
last = (*head)->prev;

node->next = *head;
node->prev = last;
Expand All @@ -85,6 +87,8 @@ void CF_CList_InsertFront(CF_CListNode_t **head, CF_CListNode_t *node)
*-----------------------------------------------------------------*/
void CF_CList_InsertBack(CF_CListNode_t **head, CF_CListNode_t *node)
{
CF_CListNode_t *last;

CF_Assert(head);
CF_Assert(node);
CF_Assert(node->next == node);
Expand All @@ -96,7 +100,7 @@ void CF_CList_InsertBack(CF_CListNode_t **head, CF_CListNode_t *node)
}
else
{
CF_CListNode_t *last = (*head)->prev;
last = (*head)->prev;

node->next = *head;
(*head)->prev = node;
Expand Down
Loading

0 comments on commit a4e5277

Please sign in to comment.