Skip to content

Commit

Permalink
Add functions to decode storage number, tariff and device from DIF
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Wahren committed Nov 6, 2013
1 parent d79cb1c commit 58778ba
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
72 changes: 72 additions & 0 deletions mbus/mbus-protocol.c
Expand Up @@ -2448,6 +2448,78 @@ mbus_data_record_value(mbus_data_record *record)
return NULL;
}

//------------------------------------------------------------------------------
/// Return the storage number for a variable-length data record
//------------------------------------------------------------------------------
long mbus_data_record_storage_number(mbus_data_record *record)
{
int bit_index;
long result = 0;
int i;

if (record)
{
result |= (record->drh.dib.dif & MBUS_DATA_RECORD_DIF_MASK_STORAGE_NO) >> 6;
bit_index++;

for (i=0; i<record->drh.dib.ndife; i++)
{
result |= (record->drh.dib.dife[i] & MBUS_DATA_RECORD_DIFE_MASK_STORAGE_NO) << bit_index;
bit_index += 4;
}

return result;
}

return 0;
}

//------------------------------------------------------------------------------
/// Return the tariff for a variable-length data record
//------------------------------------------------------------------------------
long mbus_data_record_tariff(mbus_data_record *record)
{
int bit_index;
long result = 0;
int i;

if (record && (record->drh.dib.ndife > 0))
{
for (i=0; i<record->drh.dib.ndife; i++)
{
result |= ((record->drh.dib.dife[i] & MBUS_DATA_RECORD_DIFE_MASK_TARIFF) >> 4) << bit_index;
bit_index += 2;
}

return result;
}

return -1;
}

//------------------------------------------------------------------------------
/// Return device unit for a variable-length data record
//------------------------------------------------------------------------------
int mbus_data_record_device(mbus_data_record *record)
{
int bit_index;
int result = 0;
int i;

if (record && (record->drh.dib.ndife > 0))
{
for (i=0; i<record->drh.dib.ndife; i++)
{
result |= ((record->drh.dib.dife[i] & MBUS_DATA_RECORD_DIFE_MASK_DEVICE) >> 6) << bit_index;
bit_index++;
}

return result;
}

return -1;
}

//------------------------------------------------------------------------------
/// Return a string containing the function description
//------------------------------------------------------------------------------
Expand Down
3 changes: 3 additions & 0 deletions mbus/mbus-protocol.h
Expand Up @@ -566,6 +566,9 @@ int mbus_frame_internal_pack(mbus_frame *frame, mbus_frame_data *frame_data);
//
const char *mbus_data_record_function(mbus_data_record *record);
const char *mbus_data_fixed_function(int status);
long mbus_data_record_storage_number(mbus_data_record *record);
long mbus_data_record_tariff(mbus_data_record *record);
int mbus_data_record_device(mbus_data_record *record);

//
// M-Bus frame data struct access/write functions
Expand Down

0 comments on commit 58778ba

Please sign in to comment.