Merge pull request #34 from lategoodbye/master

Address check functions
This commit is contained in:
Robert Johansson 2013-01-01 06:18:42 -08:00
commit 86a7305f5e
12 changed files with 76 additions and 11 deletions

View File

@ -121,7 +121,7 @@ main(int argc, char **argv)
return 1;
}
if (strlen(addr_str) == 16)
if (mbus_is_secondary_address(addr_str))
{
// secondary addressing

View File

@ -88,7 +88,7 @@ main(int argc, char **argv)
}
if (strlen(addr_str) == 16)
if (mbus_is_secondary_address(addr_str))
{
// secondary addressing

View File

@ -128,7 +128,7 @@ main(int argc, char **argv)
return 1;
}
if (strlen(addr_mask) != 16)
if (mbus_is_secondary_address(addr_mask) == 0)
{
fprintf(stderr, "Misformatted secondary address mask. Must be 16 character HEX number.\n");
free(addr_mask);

View File

@ -49,7 +49,7 @@ main(int argc, char **argv)
return 1;
}
if (strlen(addr) != 16)
if (mbus_is_secondary_address(addr) == 0)
{
printf("Misformatted secondary address. Must be 16 character HEX number.\n");
return 1;

View File

@ -90,7 +90,7 @@ main(int argc, char **argv)
return 1;
}
if (strlen(addr_str) == 16)
if (mbus_is_secondary_address(addr_str))
{
// secondary addressing
int ret;

View File

@ -81,7 +81,7 @@ main(int argc, char **argv)
return 1;
}
if (strlen(addr_str) == 16)
if (mbus_is_secondary_address(addr_str))
{
// secondary addressing

View File

@ -70,7 +70,7 @@ main(int argc, char **argv)
return 1;
}
if (strlen(addr_str) == 16)
if (mbus_is_secondary_address(addr_str))
{
// secondary addressing

View File

@ -53,7 +53,7 @@ main(int argc, char **argv)
return 1;
}
if (strlen(addr_mask) != 16)
if (mbus_is_secondary_address(addr_mask) == 0)
{
fprintf(stderr, "Misformatted secondary address mask. Must be 16 character HEX number.\n");
return 1;

View File

@ -40,7 +40,7 @@ main(int argc, char **argv)
return 1;
}
if (strlen(addr) != 16)
if (mbus_is_secondary_address(addr) == 0)
{
printf("Misformatted secondary address. Must be 16 character HEX number.\n");
return 1;

View File

@ -1629,6 +1629,12 @@ mbus_send_switch_baudrate_frame(mbus_handle * handle, int address, int baudrate)
int control_information = 0;
mbus_frame *frame;
if (mbus_is_primary_address(address) == 0)
{
MBUS_ERROR("%s: invalid address %d\n", __PRETTY_FUNCTION__, address);
return -1;
}
switch (baudrate)
{
case 300:
@ -1691,6 +1697,12 @@ mbus_send_request_frame(mbus_handle * handle, int address)
int retval = 0;
mbus_frame *frame;
if (mbus_is_primary_address(address) == 0)
{
MBUS_ERROR("%s: invalid address %d\n", __PRETTY_FUNCTION__, address);
return -1;
}
frame = mbus_frame_new(MBUS_FRAME_TYPE_SHORT);
if (frame == NULL)
@ -1730,6 +1742,12 @@ mbus_sendrecv_request(mbus_handle *handle, int address, mbus_frame *reply, int m
return 1;
}
if (mbus_is_primary_address(address) == 0)
{
MBUS_ERROR("%s: invalid address %d\n", __PRETTY_FUNCTION__, address);
return 1;
}
frame = mbus_frame_new(MBUS_FRAME_TYPE_SHORT);
if (frame == NULL)
@ -1881,6 +1899,12 @@ mbus_send_ping_frame(mbus_handle *handle, int address, char purge_response)
int retval = 0;
mbus_frame *frame;
if (mbus_is_primary_address(address) == 0)
{
MBUS_ERROR("%s: invalid address %d\n", __PRETTY_FUNCTION__, address);
return 1;
}
frame = mbus_frame_new(MBUS_FRAME_TYPE_SHORT);
if (frame == NULL)

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;
}

View File

@ -636,6 +636,9 @@ u_char mbus_dif_datalength_lookup(u_char dif);
char *mbus_frame_get_secondary_address(mbus_frame *frame);
int mbus_frame_select_secondary_pack(mbus_frame *frame, char *address);
int mbus_is_primary_address(int value);
int mbus_is_secondary_address(const char * value);
#ifdef __cplusplus
}
#endif