From f63bca476de52839e64068bd58cdf45787ba4073 Mon Sep 17 00:00:00 2001 From: Andreas Karlsson Date: Thu, 16 Mar 2023 16:44:40 +0100 Subject: [PATCH] Fix esc_coe Wconversion warnings - make "size" variables not reflecting hardware or protocol uint32_t. - change size_t to uint32_t to avoid conversion when compiling on x64 - add excplcit typecaste for uint8_t and uint16_t that need it due to integer promotion - split assignment using macros and bitwise operations to avoid imlicit conversion and need of explicit conversion. - add excplicit typecast to htoe MACROs --- soes/esc.h | 14 ++-- soes/esc_coe.c | 132 ++++++++++++++++++++------------------ soes/esc_coe.h | 4 +- soes/include/sys/gcc/cc.h | 8 +-- 4 files changed, 84 insertions(+), 74 deletions(-) diff --git a/soes/esc.h b/soes/esc.h index f155087..ad1a55f 100644 --- a/soes/esc.h +++ b/soes/esc.h @@ -210,9 +210,9 @@ #define MBXstate_backup 0x05 #define MBXstate_again 0x06 -#define COE_DEFAULTLENGTH 0x0a -#define COE_HEADERSIZE 0x0a -#define COE_SEGMENTHEADERSIZE 0x03 +#define COE_DEFAULTLENGTH 0x0AU +#define COE_HEADERSIZE 0x0AU +#define COE_SEGMENTHEADERSIZE 0x03U #define COE_SDOREQUEST 0x02 #define COE_SDORESPONSE 0x03 #define COE_SDOINFORMATION 0x08 @@ -467,7 +467,7 @@ typedef struct uint16_t (*esc_check_dc_handler) (void); int (*get_device_id) (uint16_t * device_id); uint8_t MBXrun; - size_t activembxsize; + uint32_t activembxsize; sm_cfg_t * activemb0; sm_cfg_t * activemb1; uint16_t ESC_SM2_sml; @@ -701,11 +701,11 @@ typedef struct #define ESC_SM3_smc (SM3_smc) #define ESC_SM3_act (SM3_act) -#define ESC_MBXHSIZE sizeof(_MBXh) +#define ESC_MBXHSIZE ((uint32_t)sizeof(_MBXh)) #define ESC_MBXDSIZE (ESC_MBXSIZE - ESC_MBXHSIZE) -#define ESC_FOEHSIZE sizeof(_FOEh) +#define ESC_FOEHSIZE (uint32_t)sizeof(_FOEh) #define ESC_FOE_DATA_SIZE (ESC_MBXSIZE - (ESC_MBXHSIZE +ESC_FOEHSIZE)) -#define ESC_EOEHSIZE sizeof(_EOEh) +#define ESC_EOEHSIZE ((uint32_t)sizeof(_EOEh)) #define ESC_EOE_DATA_SIZE (ESC_MBXSIZE - (ESC_MBXHSIZE +ESC_EOEHSIZE)) void ESC_config (esc_cfg_t * cfg); diff --git a/soes/esc_coe.c b/soes/esc_coe.c index 9c61724..05ea32f 100644 --- a/soes/esc_coe.c +++ b/soes/esc_coe.c @@ -18,7 +18,7 @@ #include "esc.h" #include "esc_coe.h" -#define BITS2BYTES(b) ((b + 7) >> 3) +#define BITS2BYTES(b) ((b + 7U) >> 3) #define BITSPOS2BYTESOFFSET(b) (b >> 3) /* Fetch value from object dictionary */ @@ -48,7 +48,7 @@ typedef enum { UPLOAD, DOWNLOAD } load_t; * @param[in] subindex = value on sub-index of object we want to locate * @return local array index if we succeed, -1 if we didn't find the index. */ -int16_t SDO_findsubindex (int16_t nidx, uint8_t subindex) +int16_t SDO_findsubindex (int32_t nidx, uint8_t subindex) { const _objd *objd; int16_t n = 0; @@ -118,9 +118,10 @@ int32_t SDO_findobject (uint16_t index) uint16_t sizeOfPDO (uint16_t index, int * nmappings, _SMmap * mappings, int max_mappings) { - uint16_t offset = 0, hobj; + uint32_t offset = 0; + uint16_t hobj; uint8_t si, sic, c; - int16_t nidx; + int32_t nidx; const _objd *objd; const _objd *objd1c1x; int mapIx = 0; @@ -159,7 +160,7 @@ uint16_t sizeOfPDO (uint16_t index, int * nmappings, _SMmap * mappings, if (max_mappings > 0) { - uint16_t index = value >> 16; + uint16_t index = (uint16_t)(value >> 16); uint8_t subindex = (value >> 8) & 0xFF; const _objd * mapping; @@ -230,7 +231,7 @@ uint16_t sizeOfPDO (uint16_t index, int * nmappings, _SMmap * mappings, *nmappings = 0; } - return BITS2BYTES (offset); + return BITS2BYTES (offset) & 0xFFFF; } /** Copy to mailbox. @@ -239,7 +240,7 @@ uint16_t sizeOfPDO (uint16_t index, int * nmappings, _SMmap * mappings, * @param[in] dest = pointer to destination * @param[in] size = Size to copy */ -static void copy2mbx (void *source, void *dest, uint16_t size) +static void copy2mbx (void *source, void *dest, size_t size) { memcpy (dest, source, size); } @@ -297,7 +298,8 @@ static void SDO_upload (void) _COEsdo *coesdo, *coeres; uint16_t index; uint8_t subindex; - int16_t nidx, nsub; + int32_t nidx; + int32_t nsub; uint8_t MBXout; uint32_t size; uint8_t dss; @@ -345,14 +347,14 @@ static void SDO_upload (void) } coeres->index = htoes (index); coeres->subindex = subindex; - coeres->command = COE_COMMAND_UPLOADRESPONSE + + coeres->command = COE_COMMAND_UPLOADRESPONSE | COE_SIZE_INDICATOR; /* convert bits to bytes */ size = BITS2BYTES(size); if (size <= 4) { /* expedited response i.e. length<=4 bytes */ - coeres->command += COE_EXPEDITED_INDICATOR + dss; + coeres->command |= (COE_EXPEDITED_INDICATOR | dss); void *dataptr = ((objd + nsub)->data) ? (objd + nsub)->data : (void *)&((objd + nsub)->value); abort = ESC_upload_pre_objecthandler (index, subindex, @@ -440,7 +442,7 @@ static void SDO_upload (void) } static uint32_t complete_access_get_variables(_COEsdo *coesdo, uint16_t *index, - uint8_t *subindex, int16_t *nidx, + uint8_t *subindex, int32_t *nidx, int16_t *nsub) { *index = etohs (coesdo->index); @@ -468,11 +470,11 @@ static uint32_t complete_access_get_variables(_COEsdo *coesdo, uint16_t *index, } static uint32_t complete_access_subindex_loop(const _objd *objd, - int16_t nidx, + int32_t nidx, int16_t nsub, uint8_t *mbxdata, load_t load_type, - uint16_t max_bytes) + uint32_t max_bytes) { /* Objects with dynamic entries cannot be accessed with Complete Access */ if ((objd->datatype == DTYPE_VISIBLE_STRING) || @@ -504,7 +506,7 @@ static uint32_t complete_access_subindex_loop(const _objd *objd, if (bitoffset != 0) { /* move on to next byte boundary */ - size += (8 - bitoffset); + size += (8U - bitoffset); } if (mbxdata != NULL) { @@ -533,19 +535,21 @@ static uint32_t complete_access_subindex_loop(const _objd *objd, else if ((load_type == UPLOAD) && (mbxdata != NULL)) { /* copy a bit data type into correct position */ - uint8_t bitmask = (1 << bitlen) - 1; + uint32_t bitmask = (1U << bitlen) - 1U; + uint32_t tempmask; if (READ_ACCESS(access, state)) { if (bitoffset == 0) { mbxdata[BITSPOS2BYTESOFFSET(size)] = 0; } - mbxdata[BITSPOS2BYTESOFFSET(size)] |= - (*(uint8_t *)ul_source & bitmask) << bitoffset; + tempmask = (*(uint8_t *)ul_source & bitmask) << bitoffset; + mbxdata[BITSPOS2BYTESOFFSET(size)] |= (uint8_t)tempmask; } else { - mbxdata[BITSPOS2BYTESOFFSET(size)] &= ~(bitmask << bitoffset); + tempmask = ~(bitmask << bitoffset); + mbxdata[BITSPOS2BYTESOFFSET(size)] &= (uint8_t)tempmask; } } @@ -586,7 +590,8 @@ static void SDO_upload_complete_access (void) _COEsdo *coesdo = (_COEsdo *) &MBX[0]; uint16_t index; uint8_t subindex; - int16_t nidx, nsub; + int32_t nidx; + int16_t nsub; uint32_t abortcode = complete_access_get_variables (coesdo, &index, &subindex, &nidx, &nsub); if (abortcode != 0) @@ -611,7 +616,7 @@ static void SDO_upload_complete_access (void) uint32_t size = complete_access_subindex_loop(objd, nidx, nsub, NULL, UPLOAD, 0); /* expedited bits used calculation */ - uint8_t dss = (size > 24) ? 0 : (4 * (3 - ((size - 1) >> 3))); + uint8_t dss = (size > 24) ? 0 : (uint8_t)(4U * (3U - ((size - 1U) >> 3))); /* convert bits to bytes */ size = BITS2BYTES(size); @@ -709,7 +714,7 @@ static void SDO_uploadsegment (void) coeres = (_COEsdo *) &MBX[MBXout * ESC_MBXSIZE]; offset = ESCvar.fragsleft; size = ESCvar.frags - ESCvar.fragsleft; - uint8_t command = COE_COMMAND_UPLOADSEGMENT + + uint8_t command = COE_COMMAND_UPLOADSEGMENT | (coesdo->command & COE_TOGGLEBIT); /* copy toggle bit */ init_coesdo(coeres, COE_SDORESPONSE, command, coesdo->index, coesdo->subindex); @@ -728,14 +733,14 @@ static void SDO_uploadsegment (void) ESCvar.segmented = 0; ESCvar.frags = 0; ESCvar.fragsleft = 0; - coeres->command += COE_COMMAND_LASTSEGMENTBIT; + coeres->command |= COE_COMMAND_LASTSEGMENTBIT; if (size >= 7) { coeres->mbxheader.length = htoes (COE_SEGMENTHEADERSIZE + size); } else { - coeres->command += (7 - size) << 1; + coeres->command |= (uint8_t)((7U - size) << 1); coeres->mbxheader.length = htoes (COE_DEFAULTLENGTH); } } @@ -768,9 +773,10 @@ static void SDO_download (void) _COEsdo *coesdo, *coeres; uint16_t index; uint8_t subindex; - int16_t nidx, nsub; + int32_t nidx; + int16_t nsub; uint8_t MBXout; - uint16_t size, actsize; + uint32_t size, actsize; const _objd *objd; uint32_t *mbxdata; uint32_t abort; @@ -792,7 +798,7 @@ static void SDO_download (void) /* expedited? */ if (coesdo->command & COE_EXPEDITED_INDICATOR) { - size = 4 - ((coesdo->command & 0x0c) >> 2); + size = 4U - ((coesdo->command & 0x0CU) >> 2); mbxdata = &(coesdo->size); } else @@ -914,7 +920,8 @@ static void SDO_download_complete_access (void) _COEsdo *coesdo = (_COEsdo *) &MBX[0]; uint16_t index; uint8_t subindex; - int16_t nidx, nsub; + int32_t nidx; + int16_t nsub; uint32_t abortcode = complete_access_get_variables (coesdo, &index, &subindex, &nidx, &nsub); if (abortcode != 0) @@ -929,7 +936,7 @@ static void SDO_download_complete_access (void) if (coesdo->command & COE_EXPEDITED_INDICATOR) { /* expedited download */ - bytes = 4 - ((coesdo->command & 0x0c) >> 2); + bytes = 4U - ((coesdo->command & 0x0CU) >> 2); } else { @@ -1030,13 +1037,14 @@ static void SDO_downloadsegment (void) if (MBXout) { _COEsdo *coeres = (_COEsdo *) &MBX[MBXout * ESC_MBXSIZE]; - uint32_t size = coesdo->mbxheader.length - 3; + uint32_t size = coesdo->mbxheader.length - 3U; if (size == 7) { size = 7 - ((coesdo->command >> 1) & 7); } - uint8_t command = COE_COMMAND_DOWNLOADSEGRESP + - (coesdo->command & COE_TOGGLEBIT); /* copy toggle bit */ + uint8_t command = COE_COMMAND_DOWNLOADSEGRESP; + uint8_t command2 = (coesdo->command & COE_TOGGLEBIT); /* copy toggle bit */ + command |= command2; init_coesdo(coeres, COE_SDORESPONSE, command, 0, 0); void *mbxdata = &(coesdo->index); /* data pointer */ @@ -1046,7 +1054,8 @@ static void SDO_downloadsegment (void) { if(ESCvar.flags == COMPLETE_ACCESS_FLAG) { - int16_t nidx, nsub; + int32_t nidx; + int16_t nsub; if(ESCvar.frags > ESCvar.fragsleft + size) { @@ -1112,7 +1121,7 @@ static void SDO_infoerror (uint32_t abortcode) if (MBXout) { coeres = (_COEobjdesc *) &MBX[MBXout * ESC_MBXSIZE]; - coeres->mbxheader.length = htoes ((uint16_t) COE_HEADERSIZE); + coeres->mbxheader.length = htoes (COE_HEADERSIZE); coeres->mbxheader.mbxtype = MBXCOE; coeres->coeheader.numberservice = htoes ((0 & 0x01f) | (COE_SDOINFORMATION << 12)); @@ -1129,14 +1138,14 @@ static void SDO_infoerror (uint32_t abortcode) } } -#define ODLISTSIZE ((ESC_MBX1_sml - ESC_MBXHSIZE - sizeof(_COEh) - sizeof(_INFOh) - 2) & 0xfffe) +#define ODLISTSIZE ((uint32_t)(ESC_MBX1_sml - ESC_MBXHSIZE - sizeof(_COEh) - sizeof(_INFOh) - 2U) & 0xfffe) /** Function for handling incoming requested SDO Get OD List, validating the * request and sending an response. On error an SDO Info Error will be sent. */ static void SDO_getodlist (void) { - uint16_t frags; + uint32_t frags; uint8_t MBXout = 0; uint16_t entries = 0; uint16_t i, n; @@ -1148,7 +1157,7 @@ static void SDO_getodlist (void) entries++; } ESCvar.entries = entries; - frags = ((entries << 1) + ODLISTSIZE - 1); + frags = ((uint32_t)(entries << 1) + ODLISTSIZE - 1U); frags /= ODLISTSIZE; coer = (_COEobjdesc *) &MBX[0]; /* check for unsupported opcodes */ @@ -1170,10 +1179,10 @@ static void SDO_getodlist (void) /* number of objects request */ if (etohs (coer->index) == 0x00) { - coel->index = htoes ((uint16_t) 0x00); + coel->index = htoes (0x00); coel->infoheader.incomplete = 0; coel->infoheader.reserved = 0x00; - coel->infoheader.fragmentsleft = htoes ((uint16_t) 0); + coel->infoheader.fragmentsleft = htoes (0); MBXcontrol[0].state = MBXstate_idle; ESCvar.xoe = 0; ESCvar.frags = frags; @@ -1210,7 +1219,7 @@ static void SDO_getodlist (void) ESCvar.frags = frags; ESCvar.fragsleft = frags - 1; coel->infoheader.fragmentsleft = htoes (ESCvar.fragsleft); - coel->index = htoes ((uint16_t) 0x01); + coel->index = htoes (0x01); p = &(coel->datatype); for (i = 0; i < n; i++) @@ -1240,13 +1249,13 @@ static void SDO_getodlistcont (void) coel = (_COEobjdesc *) &MBX[MBXout * ESC_MBXSIZE]; coel->mbxheader.mbxtype = MBXCOE; coel->coeheader.numberservice = - htoes ((0 & 0x01f) | (COE_SDOINFORMATION << 12)); + htoes (COE_SDOINFORMATION << 12); coel->infoheader.opcode = COE_GETODLISTRESPONSE; - s = (ESCvar.frags - ESCvar.fragsleft) * (ODLISTSIZE >> 1); + s = (uint16_t)((ESCvar.frags - ESCvar.fragsleft) * (ODLISTSIZE >> 1)); if (ESCvar.fragsleft > 1) { coel->infoheader.incomplete = 1; - n = s + (ODLISTSIZE >> 1); + n = (uint16_t)(s + (ODLISTSIZE >> 1)); } else { @@ -1257,7 +1266,7 @@ static void SDO_getodlistcont (void) } coel->infoheader.reserved = 0x00; ESCvar.fragsleft--; - coel->infoheader.fragmentsleft = htoes (ESCvar.fragsleft); + coel->infoheader.fragmentsleft = htoes ((uint16_t)ESCvar.fragsleft); /* pointer 2 bytes back to exclude index */ p = &(coel->index); for (i = s; i < n; i++) @@ -1294,7 +1303,7 @@ static void SDO_getod (void) coel = (_COEobjdesc *) &MBX[MBXout * ESC_MBXSIZE]; coel->mbxheader.mbxtype = MBXCOE; coel->coeheader.numberservice = - htoes ((0 & 0x01f) | (COE_SDOINFORMATION << 12)); + htoes (COE_SDOINFORMATION << 12); coel->infoheader.opcode = COE_GETODRESPONSE; coel->infoheader.incomplete = 0; coel->infoheader.reserved = 0x00; @@ -1312,14 +1321,14 @@ static void SDO_getod (void) int32_t nsub = SDO_findsubindex (nidx, 0); const _objd *objd = SDOobjects[nidx].objdesc; coel->datatype = htoes ((objd + nsub)->datatype); - coel->maxsub = SDOobjects[nidx].objdesc->value; + coel->maxsub = (uint8_t)SDOobjects[nidx].objdesc->value; } else { coel->datatype = htoes (0); - coel->maxsub = SDOobjects[nidx].objdesc->value; + coel->maxsub = (uint8_t)SDOobjects[nidx].objdesc->value; } - coel->objectcode = SDOobjects[nidx].objtype; + coel->objectcode = (uint8_t)SDOobjects[nidx].objtype; s = (uint8_t *) SDOobjects[nidx].name; d = (uint8_t *) &(coel->name); while (*s && (n < (ESC_MBXDSIZE - 0x0c))) @@ -1330,7 +1339,7 @@ static void SDO_getod (void) d++; } *d = *s; - coel->mbxheader.length = htoes ((uint16_t) 0x0c + n); + coel->mbxheader.length = htoes (0x0C + n); MBXcontrol[MBXout].state = MBXstate_outreq; MBXcontrol[0].state = MBXstate_idle; ESCvar.xoe = 0; @@ -1350,7 +1359,8 @@ static void SDO_geted (void) { uint8_t MBXout; uint16_t index; - int32_t nidx, nsub; + int32_t nidx; + int16_t nsub; uint8_t subindex; uint8_t *d; const uint8_t *s; @@ -1377,7 +1387,7 @@ static void SDO_geted (void) coel->infoheader.opcode = COE_ENTRYDESCRIPTIONRESPONSE; coel->infoheader.incomplete = 0; coel->infoheader.reserved = 0x00; - coel->infoheader.fragmentsleft = htoes ((uint16_t) 0); + coel->infoheader.fragmentsleft = htoes (0); coel->index = htoes (index); coel->subindex = subindex; coel->valueinfo = COE_VALUEINFO_ACCESS + @@ -1395,7 +1405,7 @@ static void SDO_geted (void) d++; } *d = *s; - coel->mbxheader.length = htoes ((uint16_t) 0x10 + n); + coel->mbxheader.length = htoes (0x10 + n); MBXcontrol[MBXout].state = MBXstate_outreq; MBXcontrol[0].state = MBXstate_idle; ESCvar.xoe = 0; @@ -1421,7 +1431,7 @@ void ESC_coeprocess (void) _MBXh *mbh; _COEsdo *coesdo; _COEobjdesc *coeobjdesc; - uint8_t service; + uint16_t service; if (ESCvar.MBXrun == 0) { return; @@ -1548,10 +1558,10 @@ void ESC_coeprocess (void) * @param[in] length = number of bits to get * @return bitslice value */ -static uint64_t COE_bitsliceGet (uint64_t * bitmap, int offset, int length) +static uint64_t COE_bitsliceGet (uint64_t * bitmap, unsigned int offset, unsigned int length) { - const int word_offset = offset / 64; - const int bit_offset = offset % 64; + const unsigned int word_offset = offset / 64; + const unsigned int bit_offset = offset % 64; const uint64_t mask = (length == 64) ? UINT64_MAX : (1ULL << length) - 1; uint64_t w0; uint64_t w1 = 0; @@ -1581,11 +1591,11 @@ static uint64_t COE_bitsliceGet (uint64_t * bitmap, int offset, int length) * @param[in] length = number of bits to set * @param[in] value = value to set */ -static void COE_bitsliceSet (uint64_t * bitmap, int offset, int length, +static void COE_bitsliceSet (uint64_t * bitmap, unsigned int offset, unsigned int length, uint64_t value) { - const int word_offset = offset / 64; - const int bit_offset = offset % 64; + const unsigned int word_offset = offset / 64; + const unsigned int bit_offset = offset % 64; const uint64_t mask = (length == 64) ? UINT64_MAX : (1ULL << length) - 1; const uint64_t mask0 = mask << bit_offset; uint64_t v0 = value << bit_offset; @@ -1778,7 +1788,7 @@ void COE_pdoPack (uint8_t * buffer, int nmappings, _SMmap * mappings) for (ix = 0; ix < nmappings; ix++) { const _objd * obj = mappings[ix].obj; - uint16_t offset = mappings[ix].offset; + uint32_t offset = mappings[ix].offset; if (obj != NULL) { @@ -1825,7 +1835,7 @@ void COE_pdoUnpack (uint8_t * buffer, int nmappings, _SMmap * mappings) for (ix = 0; ix < nmappings; ix++) { const _objd * obj = mappings[ix].obj; - uint16_t offset = mappings[ix].offset; + uint32_t offset = mappings[ix].offset; if (obj != NULL) { @@ -1860,7 +1870,7 @@ void COE_pdoUnpack (uint8_t * buffer, int nmappings, _SMmap * mappings) */ uint8_t COE_maxSub (uint16_t index) { - int nidx; + int32_t nidx; uint8_t maxsub; nidx = SDO_findobject (index); diff --git a/soes/esc_coe.h b/soes/esc_coe.h index 4ba8b87..6223909 100644 --- a/soes/esc_coe.h +++ b/soes/esc_coe.h @@ -41,7 +41,7 @@ typedef struct { const _objd * obj; const _objectlist * objectlistitem; - uint16_t offset; + uint32_t offset; } _SMmap; #define OBJH_READ 0 @@ -112,7 +112,7 @@ typedef struct #define COMPLETE_ACCESS_FLAG (1 << 15) void ESC_coeprocess (void); -int16_t SDO_findsubindex (int16_t nidx, uint8_t subindex); +int16_t SDO_findsubindex (int32_t nidx, uint8_t subindex); int32_t SDO_findobject (uint16_t index); uint16_t sizeOfPDO (uint16_t index, int * nmappings, _SMmap * sm, int max_mappings); void COE_initDefaultValues (void); diff --git a/soes/include/sys/gcc/cc.h b/soes/include/sys/gcc/cc.h index c4598bd..9761a4d 100644 --- a/soes/include/sys/gcc/cc.h +++ b/soes/include/sys/gcc/cc.h @@ -54,11 +54,11 @@ extern "C" #define CC_ATOMIC_OR(var,val) __atomic_or_fetch(&var,val,__ATOMIC_SEQ_CST) #if BYTE_ORDER == BIG_ENDIAN -#define htoes(x) CC_SWAP16 (x) -#define htoel(x) CC_SWAP32 (x) +#define htoes(x) CC_SWAP16 ((uint16_t)(x)) +#define htoel(x) CC_SWAP32 ((uint32_t)(x)) #else -#define htoes(x) (x) -#define htoel(x) (x) +#define htoes(x) ((uint16_t)(x)) +#define htoel(x) ((uint32_t)(x)) #endif #define etohs(x) htoes (x)