Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace types with standard C #250

Merged
merged 1 commit into from
Mar 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/CLR/Include/WireProtocol_HAL_Interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include "WireProtocol_v2.h"

//////////////////////////////////////////
bool WP_ReceiveBytes(uint8_t* ptr, uint16_t* size);
bool WP_ReceiveBytes(unsigned char* ptr, unsigned short* size);
bool WP_TransmitMessage(WP_Message* message);
void WP_CheckAvailableIncomingData();

Expand Down
27 changes: 23 additions & 4 deletions src/CLR/Include/WireProtocol_Message.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,38 @@
////////////////////////////////////////////////////
// function declarations (related with WP_Message)

#ifdef __cplusplus
extern "C" {
#endif

void WP_Message_Initialize(WP_Message* message);
void WP_Message_PrepareReception(WP_Message* message);
void WP_Message_PrepareRequest(WP_Message* message, uint32_t cmd, uint32_t flags, uint32_t payloadSize, uint8_t* payload);
void WP_Message_PrepareReply(WP_Message* message, const WP_Packet* req, uint32_t flags, uint32_t payloadSize, uint8_t* payload);
void WP_Message_SetPayload(WP_Message* message, uint8_t* payload);
void WP_Message_PrepareRequest(WP_Message* message, unsigned int cmd, unsigned int flags, unsigned int payloadSize, unsigned char* payload);
void WP_Message_PrepareReply(WP_Message* message, const WP_Packet* req, unsigned int flags, unsigned int payloadSize, unsigned char* payload);
void WP_Message_SetPayload(WP_Message* message, unsigned char* payload);
void WP_Message_Release(WP_Message* message);
bool WP_Message_VerifyHeader(WP_Message* message);
bool WP_Message_VerifyPayload(WP_Message* message);
void WP_Message_ReplyBadPacket(uint32_t flags);
void WP_Message_ReplyBadPacket(unsigned int flags);
bool WP_Message_Process(WP_Message* message);

#ifdef __cplusplus
}
#endif

//////////////////////////////////////////
// helper functions

#ifdef __cplusplus
extern "C" {
#endif

void WP_ReplyToCommand(WP_Message* message, bool fSuccess, bool fCritical, void* ptr, int size);
void WP_SendProtocolMessage(WP_Message *message);
void WP_PrepareAndSendProtocolMessage(unsigned int cmd, unsigned int payloadSize, unsigned char* payload, unsigned int flags);

#ifdef __cplusplus
}
#endif

#endif // _WIREPROTOCOL_MESSAGE_H_
39 changes: 24 additions & 15 deletions src/CLR/Include/WireProtocol_MonitorCommands.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,21 @@ typedef enum MemoryMap_Options
// backwards compatible with .NETMF
typedef struct Monitor_Ping_Reply
{
uint32_t m_source;
uint32_t m_dbg_flags;
unsigned int m_source;
unsigned int m_dbg_flags;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for hijacking, but I don't think you really want to do this - this structure is part of a public/shared interface, so it is important to use type of exact size here, which is uint32_t. int is platform dependent and the standard does not define its exact size (it only specifies min and max values and general size rules such as sizeof(char) <= sizeof(int) <= sizeof(long) etc.).

Even though majority of platforms for which nF is being compiled most likely have 32-bit int, making the above point somewhat moot, there could be exceptions; the use of well defined types at such places is a matter of a principle (e.g. char may or may not be signed, long may or may not be of same size as int etc.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your comment is most valuable! Many thanks for pointing out this. 👍
Makes me wonder if the same applies to other types that have also been replaced...

}Monitor_Ping_Reply;

// structure for command Monitor Ping
// backwards compatible with .NETMF
typedef struct Monitor_Ping_Command
{
unsigned int m_source;
unsigned int m_dbg_flags;

}Monitor_Ping_Command;


// structure with reply for OEM information command
typedef struct Monitor_OemInfo_Reply
{
Expand All @@ -71,43 +81,43 @@ typedef struct Monitor_OemInfo_Reply

typedef struct CLR_DBG_Commands_Monitor_WriteMemory
{
uint32_t address;
uint32_t length;
uint8_t data[1];
unsigned int address;
unsigned int length;
unsigned char data[1];

}CLR_DBG_Commands_Monitor_WriteMemory;

typedef struct Monitor_Reboot_Command
{
uint32_t m_flags;
unsigned int m_flags;

}Monitor_Reboot_Command;

typedef struct CLR_DBG_Commands_Monitor_EraseMemory
{
uint32_t address;
uint32_t length;
unsigned int address;
unsigned int length;

}CLR_DBG_Commands_Monitor_EraseMemory;

typedef struct CLR_DBG_Commands_Monitor_CheckMemory
{
uint32_t address;
uint32_t length;
unsigned int address;
unsigned int length;

}CLR_DBG_Commands_Monitor_CheckMemory;

typedef struct CLR_DBG_Commands_Monitor_CheckMemory_Reply
{
uint32_t crc;
unsigned int crc;

}CLR_DBG_Commands_Monitor_CheckMemory_Reply;

typedef struct MemoryMap_Range
{
uint32_t m_address;
uint32_t m_length;
uint32_t m_flags;
unsigned int m_address;
unsigned int m_length;
unsigned int m_flags;

}MemoryMap_Range;

Expand Down Expand Up @@ -140,4 +150,3 @@ bool Monitor_FlashSectorMap(WP_Message* message);
#endif

#endif //_WIREPROTOCOL_COMMANDS_H_

2 changes: 1 addition & 1 deletion src/CLR/WireProtocol/WireProtocol_HAL_Interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
/////////////////////////////////////////////////////////////////////////////////////////////////

// provided as weak to be replaced by actual implementation by HAL interface
__nfweak bool WP_ReceiveBytes(uint8_t* ptr, uint16_t* size)
__nfweak bool WP_ReceiveBytes(unsigned char* ptr, unsigned short* size)
{
// default to false
return false;
Expand Down
53 changes: 33 additions & 20 deletions src/CLR/WireProtocol/WireProtocol_Message.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,21 @@
// See LICENSE file in the project root for full license information.
//

#include <nanoHAL_Time.h>
#include <nanoSupport.h>
#include "WireProtocol_Message.h"

uint8_t receptionBuffer[2048];
unsigned char receptionBuffer[2048];
static uint16_t lastOutboundMessage;
uint32_t m_payloadTicks;
static uint8_t* marker;
// FIXME #146 unsigned int m_payloadTicks;
static unsigned char* marker;

//////////////////////////////////////////
// helper functions

void WP_ReplyToCommand(WP_Message* message, bool fSuccess, bool fCritical, void* ptr, int size)
{
WP_Message msgReply;
uint32_t flags = 0;
unsigned int flags = 0;

//
// Make sure we reply only once!
Expand All @@ -44,7 +43,7 @@ void WP_ReplyToCommand(WP_Message* message, bool fSuccess, bool fCritical, void*

WP_Message_Initialize(&msgReply);

WP_Message_PrepareReply(&msgReply, &message->m_header, flags, size, (uint8_t*)ptr );
WP_Message_PrepareReply(&msgReply, &message->m_header, flags, size, (unsigned char*)ptr );

WP_TransmitMessage(&msgReply);
}
Expand All @@ -63,9 +62,9 @@ void WP_Message_PrepareReception(WP_Message* message)
message->m_rxState = ReceiveState_Initialize;
}

void WP_Message_PrepareRequest(WP_Message* message, uint32_t cmd, uint32_t flags, uint32_t payloadSize, uint8_t* payload)
void WP_Message_PrepareRequest(WP_Message* message, unsigned int cmd, unsigned int flags, unsigned int payloadSize, unsigned char* payload)
{
memcpy(&message->m_header.m_signature, marker ? marker : (uint8_t*)MARKER_PACKET_V1, sizeof(message->m_header.m_signature));
memcpy(&message->m_header.m_signature, marker ? marker : (unsigned char*)MARKER_PACKET_V1, sizeof(message->m_header.m_signature));

message->m_header.m_crcData = SUPPORT_ComputeCRC(payload, payloadSize, 0);
message->m_header.m_cmd = cmd;
Expand All @@ -79,13 +78,13 @@ void WP_Message_PrepareRequest(WP_Message* message, uint32_t cmd, uint32_t flags
// The CRC for the header is computed setting the CRC field to zero and then running the CRC algorithm.
//
message->m_header.m_crcHeader = 0;
message->m_header.m_crcHeader = SUPPORT_ComputeCRC((uint8_t*)&message->m_header, sizeof(message->m_header), 0);
message->m_header.m_crcHeader = SUPPORT_ComputeCRC((unsigned char*)&message->m_header, sizeof(message->m_header), 0);
}


void WP_Message_PrepareReply(WP_Message* message, const WP_Packet* req, uint32_t flags, uint32_t payloadSize, uint8_t* payload)
void WP_Message_PrepareReply(WP_Message* message, const WP_Packet* req, unsigned int flags, unsigned int payloadSize, unsigned char* payload)
{
memcpy(&message->m_header.m_signature, marker ? marker : (uint8_t*)MARKER_PACKET_V1, sizeof(message->m_header.m_signature));
memcpy(&message->m_header.m_signature, marker ? marker : (unsigned char*)MARKER_PACKET_V1, sizeof(message->m_header.m_signature));

message->m_header.m_crcData = SUPPORT_ComputeCRC(payload, payloadSize, 0);
message->m_header.m_cmd = req->m_cmd;
Expand All @@ -99,10 +98,10 @@ void WP_Message_PrepareReply(WP_Message* message, const WP_Packet* req, uint32_t
// The CRC for the header is computed setting the CRC field to zero and then running the CRC algorithm.
//
message->m_header.m_crcHeader = 0;
message->m_header.m_crcHeader = SUPPORT_ComputeCRC((uint8_t*)&message->m_header, sizeof(message->m_header), 0);
message->m_header.m_crcHeader = SUPPORT_ComputeCRC((unsigned char*)&message->m_header, sizeof(message->m_header), 0);
}

void WP_Message_SetPayload(WP_Message* message, uint8_t* payload)
void WP_Message_SetPayload(WP_Message* message, unsigned char* payload)
{
message->m_payload = payload;
}
Expand All @@ -117,9 +116,9 @@ void WP_Message_Release(WP_Message* message)

bool WP_Message_VerifyHeader(WP_Message* message)
{
uint32_t crc = message->m_header.m_crcHeader;
unsigned int crc = message->m_header.m_crcHeader;
message->m_header.m_crcHeader = 0;
uint32_t computedCrc = SUPPORT_ComputeCRC((uint8_t*)&message->m_header, sizeof(message->m_header), 0);
unsigned int computedCrc = SUPPORT_ComputeCRC((unsigned char*)&message->m_header, sizeof(message->m_header), 0);
message->m_header.m_crcHeader = crc;

if(computedCrc != crc)
Expand All @@ -136,15 +135,15 @@ bool WP_Message_VerifyPayload(WP_Message* message)
return false;
}

uint32_t computedCrc = SUPPORT_ComputeCRC(message->m_payload, message->m_header.m_size, 0);
unsigned int computedCrc = SUPPORT_ComputeCRC(message->m_payload, message->m_header.m_size, 0);
if(computedCrc != message->m_header.m_crcData)
{
return false;
}
return true;
}

void WP_Message_ReplyBadPacket(uint32_t flags)
void WP_Message_ReplyBadPacket(unsigned int flags)
{
WP_Message message;
WP_Message_Initialize(&message);
Expand All @@ -155,7 +154,7 @@ void WP_Message_ReplyBadPacket(uint32_t flags)

bool WP_Message_Process(WP_Message* message)
{
uint8_t* buf = (uint8_t*)&message->m_header;
unsigned char* buf = (unsigned char*)&message->m_header;
int len;

while(true)
Expand All @@ -169,7 +168,7 @@ bool WP_Message_Process(WP_Message* message)
WP_Message_Release(message);

message->m_rxState = ReceiveState_WaitingForHeader;
message->m_pos = (uint8_t*)&message->m_header;
message->m_pos = (unsigned char*)&message->m_header;
message->m_size = sizeof(message->m_header);
break;

Expand Down Expand Up @@ -244,7 +243,7 @@ bool WP_Message_Process(WP_Message* message)
}
else
{
m_payloadTicks = HAL_Time_CurrentSysTicks();
// FIXME #146 m_payloadTicks = HAL_Time_CurrentSysTicks();
message->m_rxState = ReceiveState_ReadingPayload;
message->m_pos = message->m_payload;
message->m_size = message->m_header.m_size;
Expand Down Expand Up @@ -303,3 +302,17 @@ bool WP_Message_Process(WP_Message* message)
}
}
}

void WP_SendProtocolMessage(WP_Message *message)
{
WP_TransmitMessage(&message);
}

void WP_PrepareAndSendProtocolMessage(unsigned int cmd, unsigned int payloadSize, unsigned char* payload, unsigned int flags)
{
WP_Message message;
WP_Message_Initialize(&message);
WP_Message_PrepareRequest(&message, cmd, flags, payloadSize, payload);

WP_TransmitMessage(&message);
}