Fixed int serialization bug (affected large values).

This commit is contained in:
Matthias Kovatsch 2011-12-07 19:35:11 +01:00
parent 2168a70398
commit 95af4b88ec
3 changed files with 13 additions and 13 deletions

View file

@ -123,9 +123,9 @@ int
uint32_2_bytes(uint8_t *bytes, uint32_t var) uint32_2_bytes(uint8_t *bytes, uint32_t var)
{ {
int i = 0; int i = 0;
if (0xFF000000 & var) bytes[i++] = var>>24; if (0xFF000000 & var) bytes[i++] = (0xFF & var>>24);
if (0xFF0000 & var) bytes[i++] = var>>16; if (0xFFFF0000 & var) bytes[i++] = (0xFF & var>>16);
if (0xFF00 & var) bytes[i++] = var>>8; if (0xFFFFFF00 & var) bytes[i++] = (0xFF & var>>8);
bytes[i++] = 0xFF & var; bytes[i++] = 0xFF & var;
return i; return i;

View file

@ -136,10 +136,10 @@ serialize_int_option(int number, int current_number, uint8_t *buffer, uint32_t v
uint8_t *option = &buffer[i]; uint8_t *option = &buffer[i];
if (0xFF000000 & value) buffer[++i] = (uint8_t) (value>>24); if (0xFF000000 & value) buffer[++i] = (uint8_t) (0xFF & value>>24);
if (0x00FF0000 & value) buffer[++i] = (uint8_t) (value>>16); if (0xFFFF0000 & value) buffer[++i] = (uint8_t) (0xFF & value>>16);
if (0x0000FF00 & value) buffer[++i] = (uint8_t) (value>>8); if (0xFFFFFF00 & value) buffer[++i] = (uint8_t) (0xFF & value>>8);
if (0x000000FF & value) buffer[++i] = (uint8_t) value; if ( value) buffer[++i] = (uint8_t) (0xFF & value);
i += set_option_header(number - current_number, i-start_i, option); i += set_option_header(number - current_number, i-start_i, option);

View file

@ -137,10 +137,10 @@ serialize_int_option(int number, int current_number, uint8_t *buffer, uint32_t v
uint8_t *option = &buffer[i]; uint8_t *option = &buffer[i];
if (0xFF000000 & value) buffer[++i] = (uint8_t) (value>>24); if (0xFF000000 & value) buffer[++i] = (uint8_t) (0xFF & value>>24);
if (0x00FF0000 & value) buffer[++i] = (uint8_t) (value>>16); if (0xFFFF0000 & value) buffer[++i] = (uint8_t) (0xFF & value>>16);
if (0x0000FF00 & value) buffer[++i] = (uint8_t) (value>>8); if (0xFFFFFF00 & value) buffer[++i] = (uint8_t) (0xFF & value>>8);
if (0x000000FF & value) buffer[++i] = (uint8_t) value; if (0xFFFFFFFF & value) buffer[++i] = (uint8_t) (0xFF & value);
i += set_option_header(number - current_number, i-start_i, option); i += set_option_header(number - current_number, i-start_i, option);
@ -680,7 +680,7 @@ coap_parse_message(void *packet, uint8_t *data, uint16_t data_len)
((coap_packet_t *)packet)->block2_num = parse_int_option(current_option, option_len); ((coap_packet_t *)packet)->block2_num = parse_int_option(current_option, option_len);
((coap_packet_t *)packet)->block2_more = (((coap_packet_t *)packet)->block2_num & 0x08)>>3; ((coap_packet_t *)packet)->block2_more = (((coap_packet_t *)packet)->block2_num & 0x08)>>3;
((coap_packet_t *)packet)->block2_size = 16 << (((coap_packet_t *)packet)->block2_num & 0x07); ((coap_packet_t *)packet)->block2_size = 16 << (((coap_packet_t *)packet)->block2_num & 0x07);
((coap_packet_t *)packet)->block2_offset = (((coap_packet_t *)packet)->block2_num & ~0x0F)<<(((coap_packet_t *)packet)->block2_num & 0x07); ((coap_packet_t *)packet)->block2_offset = (((coap_packet_t *)packet)->block2_num & ~0x0000000F)<<(((coap_packet_t *)packet)->block2_num & 0x07);
((coap_packet_t *)packet)->block2_num >>= 4; ((coap_packet_t *)packet)->block2_num >>= 4;
PRINTF("Block2 [%lu%s (%u B/blk)]\n", ((coap_packet_t *)packet)->block2_num, ((coap_packet_t *)packet)->block2_more ? "+" : "", ((coap_packet_t *)packet)->block2_size); PRINTF("Block2 [%lu%s (%u B/blk)]\n", ((coap_packet_t *)packet)->block2_num, ((coap_packet_t *)packet)->block2_more ? "+" : "", ((coap_packet_t *)packet)->block2_size);
break; break;
@ -1035,7 +1035,7 @@ coap_set_header_block2(void *packet, uint32_t num, uint8_t more, uint16_t size)
if (num>0x0FFFFF) return 0; if (num>0x0FFFFF) return 0;
((coap_packet_t *)packet)->block2_num = num; ((coap_packet_t *)packet)->block2_num = num;
((coap_packet_t *)packet)->block2_more = more; ((coap_packet_t *)packet)->block2_more = more ? 1 : 0;
((coap_packet_t *)packet)->block2_size = size; ((coap_packet_t *)packet)->block2_size = size;
SET_OPTION((coap_packet_t *)packet, COAP_OPTION_BLOCK2); SET_OPTION((coap_packet_t *)packet, COAP_OPTION_BLOCK2);