Skip to content

Commit

Permalink
Improve floating point conversions.
Browse files Browse the repository at this point in the history
Provides an improved range of conversions and handles scientific
notation.

Updates the tests.

Also includes improvemnets to the tests overall by making file scope
constant variables static const and adding const to casts converting
from text.

Signed-off-by: Scott Bertin <[email protected]>
  • Loading branch information
sbertin-telular authored and sbernard31 committed Nov 9, 2020
1 parent da7afe6 commit e6c9671
Show file tree
Hide file tree
Showing 9 changed files with 494 additions and 89 deletions.
4 changes: 2 additions & 2 deletions core/data.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ static int prv_textSerialize(lwm2m_data_t * dataP,
{
uint8_t floatString[_PRV_STR_LENGTH * 2];

res = utils_floatToText(dataP->value.asFloat, floatString, _PRV_STR_LENGTH * 2);
res = utils_floatToText(dataP->value.asFloat, floatString, _PRV_STR_LENGTH * 2, false);
if (res == 0) return -1;

*bufferP = (uint8_t *)lwm2m_malloc(res);
Expand Down Expand Up @@ -477,7 +477,7 @@ int lwm2m_data_decode_float(const lwm2m_data_t * dataP,
break;

case LWM2M_TYPE_STRING:
result = utils_textToFloat(dataP->value.asBuffer.buffer, dataP->value.asBuffer.length, valueP);
result = utils_textToFloat(dataP->value.asBuffer.buffer, dataP->value.asBuffer.length, valueP, false);
break;

case LWM2M_TYPE_OPAQUE:
Expand Down
6 changes: 3 additions & 3 deletions core/discover.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ static int prv_serializeAttributes(lwm2m_context_t * contextP,
PRV_CONCAT_STR(buffer, bufferLen, head, LINK_ATTR_SEPARATOR, LINK_ATTR_SEPARATOR_SIZE);
PRV_CONCAT_STR(buffer, bufferLen, head, ATTR_GREATER_THAN_STR, ATTR_GREATER_THAN_LEN);

res = utils_floatToText(paramP->greaterThan, buffer + head, bufferLen - head);
res = utils_floatToText(paramP->greaterThan, buffer + head, bufferLen - head, false);
if (res <= 0) return -1;
head += res;
}
Expand All @@ -132,7 +132,7 @@ static int prv_serializeAttributes(lwm2m_context_t * contextP,
PRV_CONCAT_STR(buffer, bufferLen, head, LINK_ATTR_SEPARATOR, LINK_ATTR_SEPARATOR_SIZE);
PRV_CONCAT_STR(buffer, bufferLen, head, ATTR_LESS_THAN_STR, ATTR_LESS_THAN_LEN);

res = utils_floatToText(paramP->lessThan, buffer + head, bufferLen - head);
res = utils_floatToText(paramP->lessThan, buffer + head, bufferLen - head, false);
if (res <= 0) return -1;
head += res;
}
Expand All @@ -141,7 +141,7 @@ static int prv_serializeAttributes(lwm2m_context_t * contextP,
PRV_CONCAT_STR(buffer, bufferLen, head, LINK_ATTR_SEPARATOR, LINK_ATTR_SEPARATOR_SIZE);
PRV_CONCAT_STR(buffer, bufferLen, head, ATTR_STEP_STR, ATTR_STEP_LEN);

res = utils_floatToText(paramP->step, buffer + head, bufferLen - head);
res = utils_floatToText(paramP->step, buffer + head, bufferLen - head, false);
if (res <= 0) return -1;
head += res;
}
Expand Down
4 changes: 2 additions & 2 deletions core/internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -383,14 +383,14 @@ int utils_isAltPathValid(const char * altPath);
int utils_stringCopy(char * buffer, size_t length, const char * str);
size_t utils_intToText(int64_t data, uint8_t * string, size_t length);
size_t utils_uintToText(uint64_t data, uint8_t * string, size_t length);
size_t utils_floatToText(double data, uint8_t * string, size_t length);
size_t utils_floatToText(double data, uint8_t * string, size_t length, bool allowExponential);
size_t utils_objLinkToText(uint16_t objectId,
uint16_t objectInstanceId,
uint8_t * string,
size_t length);
int utils_textToInt(const uint8_t * buffer, int length, int64_t * dataP);
int utils_textToUInt(const uint8_t * buffer, int length, uint64_t * dataP);
int utils_textToFloat(const uint8_t * buffer, int length, double * dataP);
int utils_textToFloat(const uint8_t * buffer, int length, double * dataP, bool allowExponential);
int utils_textToObjLink(const uint8_t * buffer,
int length,
uint16_t * objectId,
Expand Down
20 changes: 12 additions & 8 deletions core/json.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@


#include "internals.h"
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
Expand Down Expand Up @@ -758,8 +759,8 @@ static int prv_serializeValue(lwm2m_data_t * tlvP,
uint8_t * buffer,
size_t bufferLen)
{
int res;
int head;
size_t res;
size_t head;

switch (tlvP->type)
{
Expand Down Expand Up @@ -793,7 +794,7 @@ static int prv_serializeValue(lwm2m_data_t * tlvP,
head = JSON_ITEM_NUM_SIZE;

res = utils_intToText(value, buffer + head, bufferLen - head);
if (res <= 0) return -1;
if (!res) return -1;
head += res;

if (bufferLen - head < JSON_ITEM_NUM_END_SIZE) return -1;
Expand All @@ -813,7 +814,7 @@ static int prv_serializeValue(lwm2m_data_t * tlvP,
head = JSON_ITEM_NUM_SIZE;

res = utils_uintToText(value, buffer + head, bufferLen - head);
if (res <= 0) return -1;
if (!res) return -1;
head += res;

if (bufferLen - head < JSON_ITEM_NUM_END_SIZE) return -1;
Expand All @@ -832,8 +833,11 @@ static int prv_serializeValue(lwm2m_data_t * tlvP,
memcpy(buffer, JSON_ITEM_NUM, JSON_ITEM_NUM_SIZE);
head = JSON_ITEM_NUM_SIZE;

res = utils_floatToText(value, buffer + head, bufferLen - head);
if (res <= 0) return -1;
res = utils_floatToText(value, buffer + head, bufferLen - head, true);
if (!res) return -1;
/* Error if inf or nan */
if (buffer[head] != '-' && !isdigit(buffer[head])) return -1;
if (res > 1 && buffer[head] == '-' && !isdigit(buffer[head+1])) return -1;
head += res;

if (bufferLen - head < JSON_ITEM_NUM_END_SIZE) return -1;
Expand Down Expand Up @@ -886,7 +890,7 @@ static int prv_serializeValue(lwm2m_data_t * tlvP,
tlvP->value.asObjLink.objectInstanceId,
buffer + head,
bufferLen - head);
if (res <= 0) return -1;
if (!res) return -1;
head += res;

if (bufferLen - head < JSON_ITEM_OBJECT_LINK_END_SIZE) return -1;
Expand All @@ -898,7 +902,7 @@ static int prv_serializeValue(lwm2m_data_t * tlvP,
return -1;
}

return head;
return (int)head;
}

static int prv_serializeData(lwm2m_data_t * tlvP,
Expand Down
3 changes: 2 additions & 1 deletion core/json_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@


#include "internals.h"
#include <float.h>

#if defined(LWM2M_SUPPORT_JSON) || defined(LWM2M_SUPPORT_SENML_JSON)

Expand Down Expand Up @@ -320,7 +321,7 @@ int json_convertNumeric(const uint8_t *value,
{
double val;

result = utils_textToFloat(value, valueLen, &val);
result = utils_textToFloat(value, valueLen, &val, true);
if (result)
{
lwm2m_data_encode_float(val, targetP);
Expand Down
12 changes: 6 additions & 6 deletions core/management.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ static int prv_readAttributes(multi_option_t * query,
if (0 != ((attrP->toSet | attrP->toClear) & LWM2M_ATTR_FLAG_GREATER_THAN)) return -1;
if (query->len == ATTR_GREATER_THAN_LEN) return -1;

if (1 != utils_textToFloat(query->data + ATTR_GREATER_THAN_LEN, query->len - ATTR_GREATER_THAN_LEN, &floatValue)) return -1;
if (1 != utils_textToFloat(query->data + ATTR_GREATER_THAN_LEN, query->len - ATTR_GREATER_THAN_LEN, &floatValue, false)) return -1;

attrP->toSet |= LWM2M_ATTR_FLAG_GREATER_THAN;
attrP->greaterThan = floatValue;
Expand All @@ -121,7 +121,7 @@ static int prv_readAttributes(multi_option_t * query,
if (0 != ((attrP->toSet | attrP->toClear) & LWM2M_ATTR_FLAG_LESS_THAN)) return -1;
if (query->len == ATTR_LESS_THAN_LEN) return -1;

if (1 != utils_textToFloat(query->data + ATTR_LESS_THAN_LEN, query->len - ATTR_LESS_THAN_LEN, &floatValue)) return -1;
if (1 != utils_textToFloat(query->data + ATTR_LESS_THAN_LEN, query->len - ATTR_LESS_THAN_LEN, &floatValue, false)) return -1;

attrP->toSet |= LWM2M_ATTR_FLAG_LESS_THAN;
attrP->lessThan = floatValue;
Expand All @@ -138,7 +138,7 @@ static int prv_readAttributes(multi_option_t * query,
if (0 != ((attrP->toSet | attrP->toClear) & LWM2M_ATTR_FLAG_STEP)) return -1;
if (query->len == ATTR_STEP_LEN) return -1;

if (1 != utils_textToFloat(query->data + ATTR_STEP_LEN, query->len - ATTR_STEP_LEN, &floatValue)) return -1;
if (1 != utils_textToFloat(query->data + ATTR_STEP_LEN, query->len - ATTR_STEP_LEN, &floatValue, false)) return -1;
if (floatValue < 0) return -1;

attrP->toSet |= LWM2M_ATTR_FLAG_STEP;
Expand Down Expand Up @@ -678,7 +678,7 @@ int lwm2m_dm_write_attributes(lwm2m_context_t * contextP,
if (attrP->toSet & LWM2M_ATTR_FLAG_GREATER_THAN)
{
memcpy(buffer, ATTR_GREATER_THAN_STR, ATTR_GREATER_THAN_LEN);
length = utils_floatToText(attrP->greaterThan, buffer + ATTR_GREATER_THAN_LEN, _PRV_BUFFER_SIZE - ATTR_GREATER_THAN_LEN);
length = utils_floatToText(attrP->greaterThan, buffer + ATTR_GREATER_THAN_LEN, _PRV_BUFFER_SIZE - ATTR_GREATER_THAN_LEN, false);
if (length == 0)
{
transaction_free(transaction);
Expand All @@ -690,7 +690,7 @@ int lwm2m_dm_write_attributes(lwm2m_context_t * contextP,
if (attrP->toSet & LWM2M_ATTR_FLAG_LESS_THAN)
{
memcpy(buffer, ATTR_LESS_THAN_STR, ATTR_LESS_THAN_LEN);
length = utils_floatToText(attrP->lessThan, buffer + ATTR_LESS_THAN_LEN, _PRV_BUFFER_SIZE - ATTR_LESS_THAN_LEN);
length = utils_floatToText(attrP->lessThan, buffer + ATTR_LESS_THAN_LEN, _PRV_BUFFER_SIZE - ATTR_LESS_THAN_LEN, false);
if (length == 0)
{
transaction_free(transaction);
Expand All @@ -702,7 +702,7 @@ int lwm2m_dm_write_attributes(lwm2m_context_t * contextP,
if (attrP->toSet & LWM2M_ATTR_FLAG_STEP)
{
memcpy(buffer, ATTR_STEP_STR, ATTR_STEP_LEN);
length = utils_floatToText(attrP->step, buffer + ATTR_STEP_LEN, _PRV_BUFFER_SIZE - ATTR_STEP_LEN);
length = utils_floatToText(attrP->step, buffer + ATTR_STEP_LEN, _PRV_BUFFER_SIZE - ATTR_STEP_LEN, false);
if (length == 0)
{
transaction_free(transaction);
Expand Down
6 changes: 5 additions & 1 deletion core/senml_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@


#include "internals.h"
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
Expand Down Expand Up @@ -855,8 +856,11 @@ static int prv_serializeValue(const lwm2m_data_t * tlvP,
memcpy(buffer, JSON_ITEM_NUM, JSON_ITEM_NUM_SIZE);
head = JSON_ITEM_NUM_SIZE;

res = utils_floatToText(value, buffer + head, bufferLen - head);
res = utils_floatToText(value, buffer + head, bufferLen - head, true);
if (!res) return -1;
/* Error if inf or nan */
if (buffer[head] != '-' && !isdigit(buffer[head])) return -1;
if (res > 1 && buffer[head] == '-' && !isdigit(buffer[head+1])) return -1;
head += res;
}
break;
Expand Down
Loading

0 comments on commit e6c9671

Please sign in to comment.