add new helper functions to check primary and secondary addresses

This commit is contained in:
Stefan Wahren
2012-12-31 13:37:17 +01:00
parent a8cc7b0acd
commit 61fd2aa1c7
3 changed files with 67 additions and 2 deletions

View File

@ -3957,9 +3957,15 @@ mbus_frame_select_secondary_pack(mbus_frame *frame, char *address)
int val, i, j, k;
char tmp[16];
if (frame == NULL || address == NULL || strlen(address) != 16)
if (frame == NULL || address == NULL)
{
snprintf(error_str, sizeof(error_str), "%s: frame or address arguments are NULL or invalid.", __PRETTY_FUNCTION__);
snprintf(error_str, sizeof(error_str), "%s: frame or address arguments are NULL.", __PRETTY_FUNCTION__);
return -1;
}
if (mbus_is_secondary_address(address) == 0)
{
snprintf(error_str, sizeof(error_str), "%s: address is invalid.", __PRETTY_FUNCTION__);
return -1;
}
@ -4011,3 +4017,35 @@ mbus_frame_select_secondary_pack(mbus_frame *frame, char *address)
return 0;
}
//---------------------------------------------------------
// Checks if an integer is a valid primary address.
//---------------------------------------------------------
int
mbus_is_primary_address(int value)
{
return ((value >= 0x00) && (value <= 0xFF));
}
//---------------------------------------------------------
// Checks if an string is a valid secondary address.
//---------------------------------------------------------
int
mbus_is_secondary_address(const char * value)
{
int i;
if (value == NULL)
return 0;
if (strlen(value) != 16)
return 0;
for (i = 0; i < 16; i++)
{
if (!isxdigit(value[i]))
return 0;
}
return 1;
}