Skip to content

Commit

Permalink
Merge pull request #333 from thnkslprpt/fix-109-variable-declarations
Browse files Browse the repository at this point in the history
Fix #109, Move variables declared mid-function to the top
  • Loading branch information
dzbaker authored Mar 9, 2023
2 parents c44e372 + dec4440 commit 4ce1aa9
Show file tree
Hide file tree
Showing 11 changed files with 325 additions and 378 deletions.
45 changes: 26 additions & 19 deletions fsw/src/cf_cfdp.c
Original file line number Diff line number Diff line change
Expand Up @@ -952,12 +952,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 @@ -966,7 +969,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 @@ -1022,8 +1024,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 @@ -1039,7 +1039,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 @@ -1395,14 +1395,14 @@ void CF_CFDP_ProcessPlaybackDirectory(CF_Channel_t *c, CF_Playback_t *p)
{
CF_Transaction_t *pt;
os_dirent_t dirent;
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 Down Expand Up @@ -1500,15 +1500,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 @@ -1524,8 +1530,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 @@ -1562,13 +1568,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 @@ -114,8 +114,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 @@ -129,7 +130,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 @@ -487,8 +488,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 @@ -498,7 +499,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 @@ -777,7 +777,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 @@ -786,8 +789,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 @@ -835,7 +836,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 @@ -959,7 +960,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 @@ -983,8 +985,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 @@ -263,11 +263,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 @@ -341,14 +342,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 @@ -699,7 +699,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 @@ -743,7 +744,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 @@ -182,6 +182,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 @@ -243,8 +244,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 @@ -235,18 +235,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 @@ -255,8 +259,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 @@ -52,14 +52,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 @@ -79,6 +81,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 @@ -90,7 +94,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
9 changes: 5 additions & 4 deletions fsw/src/cf_cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ void CF_CmdPlaybackDir(CFE_SB_Buffer_t *msg)
*-----------------------------------------------------------------*/
int CF_DoChanAction(CF_UnionArgsCmd_t *cmd, const char *errstr, CF_ChanActionFn_t fn, void *context)
{
int i;
int ret = 0;

/* this function is generic for any ground command that takes a single channel
Expand All @@ -222,7 +223,6 @@ int CF_DoChanAction(CF_UnionArgsCmd_t *cmd, const char *errstr, CF_ChanActionFn_
if (cmd->data.byte[0] == CF_ALL_CHANNELS)
{
/* apply to all channels */
int i;
for (i = 0; i < CF_NUM_CHANNELS; ++i)
ret |= fn(i, context);
}
Expand Down Expand Up @@ -334,13 +334,14 @@ CF_Transaction_t *CF_FindTransactionBySequenceNumberAllChannels(CF_TransactionSe
*-----------------------------------------------------------------*/
int CF_TsnChanAction(CF_TransactionCmd_t *cmd, const char *cmdstr, CF_TsnChanAction_fn_t fn, void *context)
{
int ret = -1;
CF_Transaction_t *t;
int ret = -1;

if (cmd->chan == CF_COMPOUND_KEY)
{
/* special value 254 means to use the compound key (cmd->eid, cmd->ts) to find the transaction
* to act upon */
CF_Transaction_t *t = CF_FindTransactionBySequenceNumberAllChannels(cmd->ts, cmd->eid);
t = CF_FindTransactionBySequenceNumberAllChannels(cmd->ts, cmd->eid);
if (t)
{
fn(t, context);
Expand Down Expand Up @@ -587,12 +588,12 @@ void CF_CmdDisableDequeue(CFE_SB_Buffer_t *msg)
*-----------------------------------------------------------------*/
int CF_DoEnableDisablePolldir(uint8 chan_num, const CF_ChanAction_BoolMsgArg_t *context)
{
int i;
int ret = 0;
/* no need to bounds check chan_num, done in caller */
if (context->msg->data.byte[1] == CF_ALL_POLLDIRS)
{
/* all polldirs in channel */
int i;
for (i = 0; i < CF_MAX_POLLING_DIR_PER_CHAN; ++i)
CF_AppData.config_table->chan[chan_num].polldir[i].enabled = context->barg;
}
Expand Down
Loading

0 comments on commit 4ce1aa9

Please sign in to comment.