mbus_manufacturer_id: add error checking code

Signed-off-by: Michael Heimpold <mhei@heimpold.de>
This commit is contained in:
Michael Heimpold
2014-01-18 20:43:57 +01:00
parent 0fccc0e337
commit f262138572
2 changed files with 24 additions and 3 deletions

View File

@ -22,18 +22,38 @@ static char error_str[512];
#define NITEMS(x) (sizeof(x)/sizeof(x[0])) #define NITEMS(x) (sizeof(x)/sizeof(x[0]))
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Return the manufacturer ID according to the manufacturer's 3 byte ASCII code // Returns the manufacturer ID according to the manufacturer's 3 byte ASCII code
// or zero when there was an error.
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
unsigned int unsigned int
mbus_manufacturer_id(char *manufacturer) mbus_manufacturer_id(char *manufacturer)
{ {
unsigned int id; unsigned int id;
/*
* manufacturer must consist of at least 3 alphabetic characters,
* additional chars are silently ignored.
*/
if (!manufacturer || strlen(manufacturer) < 3)
return 0;
if (!isalpha(manufacturer[0]) ||
!isalpha(manufacturer[1]) ||
!isalpha(manufacturer[2]))
return 0;
id = (toupper(manufacturer[0]) - 64) * 32 * 32 + id = (toupper(manufacturer[0]) - 64) * 32 * 32 +
(toupper(manufacturer[1]) - 64) * 32 + (toupper(manufacturer[1]) - 64) * 32 +
(toupper(manufacturer[2]) - 64); (toupper(manufacturer[2]) - 64);
return id; /*
* Valid input data should be in the range of 'AAA' to 'ZZZ' according to
* the FLAG Association (http://www.dlms.com/flag/), thus resulting in
* an ID from 0x0421 to 0x6b5a. If the conversion results in anything not
* in this range, simply discard it and return 0 instead.
*/
return (0x0421 <= id && id <= 0x6b5a) ? id : 0;
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------

View File

@ -493,7 +493,8 @@ typedef struct _mbus_data_secondary_address {
#define MBUS_VARIABLE_DATA_MEDIUM_ADC 0x19 #define MBUS_VARIABLE_DATA_MEDIUM_ADC 0x19
// //
// Return manufacturer ID // Returns the manufacturer ID or zero if the given
// string could not be converted into an ID
// //
unsigned int mbus_manufacturer_id(char *manufacturer); unsigned int mbus_manufacturer_id(char *manufacturer);