Merge pull request #15 from lategoodbye/master

Removed magic numbers and added new test frames
This commit is contained in:
Robert Johansson
2012-07-12 16:59:51 -07:00
21 changed files with 351 additions and 33 deletions

View File

@ -132,7 +132,7 @@ main(int argc, char **argv)
} }
} }
if (mbus_recv_frame(handle, &reply) != 0) if (mbus_recv_frame(handle, &reply) != MBUS_RECV_RESULT_OK)
{ {
fprintf(stderr, "Failed to receive M-Bus response frame.\n"); fprintf(stderr, "Failed to receive M-Bus response frame.\n");
return 1; return 1;

View File

@ -103,7 +103,7 @@ main(int argc, char **argv)
ret = mbus_recv_frame(handle, &reply); ret = mbus_recv_frame(handle, &reply);
if (ret == -3) if (ret == MBUS_RECV_RESULT_TIMEOUT)
{ {
continue; continue;
} }
@ -111,7 +111,7 @@ main(int argc, char **argv)
if (debug) if (debug)
printf("\n"); printf("\n");
if (ret == -2) if (ret == MBUS_RECV_RESULT_INVALID)
{ {
/* check for more data (collision) */ /* check for more data (collision) */
mbus_purge_frames(handle); mbus_purge_frames(handle);

View File

@ -79,13 +79,13 @@ main(int argc, char **argv)
ret = mbus_recv_frame(handle, &reply); ret = mbus_recv_frame(handle, &reply);
if (ret == -3) if (ret == MBUS_RECV_RESULT_TIMEOUT)
{ {
printf("No reply from device with secondary address %s: %s\n", argv[2], mbus_error_str()); printf("No reply from device with secondary address %s: %s\n", argv[2], mbus_error_str());
return 1; return 1;
} }
if (ret == -2) if (ret == MBUS_RECV_RESULT_INVALID)
{ {
printf("Invalid reply from %s: The address address probably match more than one device: %s\n", argv[2], mbus_error_str()); printf("Invalid reply from %s: The address address probably match more than one device: %s\n", argv[2], mbus_error_str());
return 1; return 1;
@ -99,7 +99,7 @@ main(int argc, char **argv)
return 1; return 1;
} }
if (mbus_recv_frame(handle, &reply) != 0) if (mbus_recv_frame(handle, &reply) != MBUS_RECV_RESULT_OK)
{ {
printf("Failed to recieve reply from selected secondary device: %s\n", mbus_error_str()); printf("Failed to recieve reply from selected secondary device: %s\n", mbus_error_str());
return 1; return 1;

View File

@ -78,7 +78,7 @@ main(int argc, char **argv)
ret = mbus_recv_frame(handle, &reply); ret = mbus_recv_frame(handle, &reply);
if (ret == -3) if (ret == MBUS_RECV_RESULT_TIMEOUT)
{ {
printf("No reply from device\n"); printf("No reply from device\n");
return 1; return 1;

View File

@ -117,7 +117,7 @@ main(int argc, char **argv)
} }
} }
if (mbus_recv_frame(handle, &reply) != 0) if (mbus_recv_frame(handle, &reply) != MBUS_RECV_RESULT_OK)
{ {
fprintf(stderr, "Failed to receive M-Bus response frame: %s\n", mbus_error_str()); fprintf(stderr, "Failed to receive M-Bus response frame: %s\n", mbus_error_str());
return 1; return 1;

View File

@ -86,7 +86,7 @@ main(int argc, char **argv)
ret = mbus_recv_frame(handle, &reply); ret = mbus_recv_frame(handle, &reply);
if (ret == -3) if (ret == MBUS_RECV_RESULT_TIMEOUT)
{ {
continue; continue;
} }
@ -94,7 +94,7 @@ main(int argc, char **argv)
if (debug) if (debug)
printf("\n"); printf("\n");
if (ret == -2) if (ret == MBUS_RECV_RESULT_INVALID)
{ {
/* check for more data (collision) */ /* check for more data (collision) */
mbus_purge_frames(handle); mbus_purge_frames(handle);

View File

@ -65,13 +65,13 @@ main(int argc, char **argv)
ret = mbus_recv_frame(handle, &reply); ret = mbus_recv_frame(handle, &reply);
if (ret == -3) if (ret == MBUS_RECV_RESULT_TIMEOUT)
{ {
printf("No reply from device with secondary address %s: %s\n", argv[3], mbus_error_str()); printf("No reply from device with secondary address %s: %s\n", argv[3], mbus_error_str());
return 1; return 1;
} }
if (ret == -2) if (ret == MBUS_RECV_RESULT_INVALID)
{ {
printf("Invalid reply from %s: The address address probably match more than one device: %s\n", argv[3], mbus_error_str()); printf("Invalid reply from %s: The address address probably match more than one device: %s\n", argv[3], mbus_error_str());
return 1; return 1;
@ -85,7 +85,7 @@ main(int argc, char **argv)
return 1; return 1;
} }
if (mbus_recv_frame(handle, &reply) != 0) if (mbus_recv_frame(handle, &reply) != MBUS_RECV_RESULT_OK)
{ {
printf("Failed to recieve reply from selected secondary device: %s\n", mbus_error_str()); printf("Failed to recieve reply from selected secondary device: %s\n", mbus_error_str());
return 1; return 1;

View File

@ -1434,13 +1434,13 @@ mbus_recv_frame(mbus_handle * handle, mbus_frame *frame)
if (handle == NULL) if (handle == NULL)
{ {
MBUS_ERROR("%s: Invalid M-Bus handle for receive.\n", __PRETTY_FUNCTION__); MBUS_ERROR("%s: Invalid M-Bus handle for receive.\n", __PRETTY_FUNCTION__);
return -1; return MBUS_RECV_RESULT_ERROR;
} }
if (frame == NULL) if (frame == NULL)
{ {
MBUS_ERROR("%s: Invalid frame.\n", __PRETTY_FUNCTION__); MBUS_ERROR("%s: Invalid frame.\n", __PRETTY_FUNCTION__);
return -1; return MBUS_RECV_RESULT_ERROR;
} }
result = handle->recv(handle, frame); result = handle->recv(handle, frame);
@ -1463,7 +1463,8 @@ int mbus_purge_frames(mbus_handle *handle)
while (1) while (1)
{ {
err = mbus_recv_frame(handle, &reply); err = mbus_recv_frame(handle, &reply);
if (err != -2 && err != 0) if (err != MBUS_RECV_RESULT_OK &&
err != MBUS_RECV_RESULT_INVALID)
break; break;
received = 1; received = 1;
@ -1660,7 +1661,7 @@ mbus_sendrecv_request(mbus_handle *handle, int address, mbus_frame *reply, int m
if (debug) if (debug)
printf("%s: debug: receiving response frame #%d\n", __PRETTY_FUNCTION__, frame_count); printf("%s: debug: receiving response frame #%d\n", __PRETTY_FUNCTION__, frame_count);
if (mbus_recv_frame(handle, next_frame) != 0) if (mbus_recv_frame(handle, next_frame) != MBUS_RECV_RESULT_OK)
{ {
MBUS_ERROR("%s: Failed to receive M-Bus response frame.\n", __PRETTY_FUNCTION__); MBUS_ERROR("%s: Failed to receive M-Bus response frame.\n", __PRETTY_FUNCTION__);
retval = 1; retval = 1;
@ -1802,12 +1803,12 @@ mbus_select_secondary_address(mbus_handle * handle, const char *mask)
ret = mbus_recv_frame(handle, &reply); ret = mbus_recv_frame(handle, &reply);
if (ret == -3) if (ret == MBUS_RECV_RESULT_TIMEOUT)
{ {
return MBUS_PROBE_NOTHING; return MBUS_PROBE_NOTHING;
} }
if (ret == -2) if (ret == MBUS_RECV_RESULT_INVALID)
{ {
/* check for more data (collision) */ /* check for more data (collision) */
mbus_purge_frames(handle); mbus_purge_frames(handle);
@ -1862,12 +1863,12 @@ mbus_probe_secondary_address(mbus_handle * handle, const char *mask, char *match
ret = mbus_recv_frame(handle, &reply); ret = mbus_recv_frame(handle, &reply);
if (ret == -3) if (ret == MBUS_RECV_RESULT_TIMEOUT)
{ {
return MBUS_PROBE_NOTHING; return MBUS_PROBE_NOTHING;
} }
if (ret == -2) if (ret == MBUS_RECV_RESULT_INVALID)
{ {
return MBUS_PROBE_COLLISION; return MBUS_PROBE_COLLISION;
} }

View File

@ -106,6 +106,15 @@ typedef struct _mbus_slave_data {
#define MBUS_HANDLE_TYPE_TCP 0 #define MBUS_HANDLE_TYPE_TCP 0
#define MBUS_HANDLE_TYPE_SERIAL 1 #define MBUS_HANDLE_TYPE_SERIAL 1
//
// Resultcodes for mbus_recv_frame
//
#define MBUS_RECV_RESULT_OK 0
#define MBUS_RECV_RESULT_ERROR -1
#define MBUS_RECV_RESULT_INVALID -2
#define MBUS_RECV_RESULT_TIMEOUT -3
#define MBUS_RECV_RESULT_RESET -4
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// MBUS FRAME DATA FORMATS // MBUS FRAME DATA FORMATS
// //

View File

@ -245,7 +245,7 @@ mbus_serial_recv_frame(mbus_handle *handle, mbus_frame *frame)
if (handle == NULL || frame == NULL) if (handle == NULL || frame == NULL)
{ {
fprintf(stderr, "%s: Invalid parameter.\n", __PRETTY_FUNCTION__); fprintf(stderr, "%s: Invalid parameter.\n", __PRETTY_FUNCTION__);
return -1; return MBUS_RECV_RESULT_ERROR;
} }
memset((void *)buff, 0, sizeof(buff)); memset((void *)buff, 0, sizeof(buff));
@ -264,7 +264,7 @@ mbus_serial_recv_frame(mbus_handle *handle, mbus_frame *frame)
{ {
// fprintf(stderr, "%s: aborting recv frame (remaining = %d, len = %d, nread = %d)\n", // fprintf(stderr, "%s: aborting recv frame (remaining = %d, len = %d, nread = %d)\n",
// __PRETTY_FUNCTION__, remaining, len, nread); // __PRETTY_FUNCTION__, remaining, len, nread);
return -1; return MBUS_RECV_RESULT_ERROR;
} }
// printf("%s: Got %d byte [remaining %d, len %d]\n", __PRETTY_FUNCTION__, nread, remaining, len); // printf("%s: Got %d byte [remaining %d, len %d]\n", __PRETTY_FUNCTION__, nread, remaining, len);
@ -288,7 +288,7 @@ mbus_serial_recv_frame(mbus_handle *handle, mbus_frame *frame)
if (len == 0) if (len == 0)
{ {
// No data received // No data received
return -1; return MBUS_RECV_RESULT_TIMEOUT;
} }
// //
@ -301,16 +301,16 @@ mbus_serial_recv_frame(mbus_handle *handle, mbus_frame *frame)
{ {
// Would be OK when e.g. scanning the bus, otherwise it is a failure. // Would be OK when e.g. scanning the bus, otherwise it is a failure.
// printf("%s: M-Bus layer failed to receive complete data.\n", __PRETTY_FUNCTION__); // printf("%s: M-Bus layer failed to receive complete data.\n", __PRETTY_FUNCTION__);
return -2; return MBUS_RECV_RESULT_INVALID;
} }
if (len == -1) if (len == -1)
{ {
fprintf(stderr, "%s: M-Bus layer failed to parse data.\n", __PRETTY_FUNCTION__); fprintf(stderr, "%s: M-Bus layer failed to parse data.\n", __PRETTY_FUNCTION__);
return -1; return MBUS_RECV_RESULT_ERROR;
} }
return 0; return MBUS_RECV_RESULT_OK;
} }

View File

@ -169,7 +169,7 @@ int mbus_tcp_recv_frame(mbus_handle *handle, mbus_frame *frame)
if (handle == NULL || frame == NULL) { if (handle == NULL || frame == NULL) {
fprintf(stderr, "%s: Invalid parameter.\n", __PRETTY_FUNCTION__); fprintf(stderr, "%s: Invalid parameter.\n", __PRETTY_FUNCTION__);
return -1; return MBUS_RECV_RESULT_ERROR;
} }
memset((void *) buff, 0, sizeof(buff)); memset((void *) buff, 0, sizeof(buff));
@ -190,14 +190,14 @@ retry:
if (errno == EAGAIN || errno == EWOULDBLOCK) { if (errno == EAGAIN || errno == EWOULDBLOCK) {
mbus_error_str_set("M-Bus tcp transport layer response timeout has been reached."); mbus_error_str_set("M-Bus tcp transport layer response timeout has been reached.");
return -3; return MBUS_RECV_RESULT_TIMEOUT;
} }
mbus_error_str_set("M-Bus tcp transport layer failed to read data."); mbus_error_str_set("M-Bus tcp transport layer failed to read data.");
return -1; return MBUS_RECV_RESULT_ERROR;
case 0: case 0:
mbus_error_str_set("M-Bus tcp transport layer connection closed by remote host."); mbus_error_str_set("M-Bus tcp transport layer connection closed by remote host.");
return -4; return MBUS_RECV_RESULT_RESET;
default: default:
len += nread; len += nread;
} }
@ -211,10 +211,10 @@ retry:
if (remaining < 0) { if (remaining < 0) {
mbus_error_str_set("M-Bus layer failed to parse data."); mbus_error_str_set("M-Bus layer failed to parse data.");
return -2; return MBUS_RECV_RESULT_INVALID;
} }
return 0; return MBUS_RECV_RESULT_OK;
} }

View File

@ -0,0 +1 @@
68 5E 5E 68 08 00 72 90 85 71 26 24 23 28 04 73 50 00 00 0C 05 00 00 00 00 0C 12 42 07 00 00 3C 2A DD B4 EB DD 3B 3A DD B4 EB 0A 5A 04 02 0A 5E 04 02 0A 62 00 00 04 6D 22 10 8D 11 4C 05 00 00 00 00 44 6D 3B 17 7E 14 44 ED 7E 3B 17 9E 14 8C 01 05 00 00 00 00 84 01 6D 3B 17 7F 1C 0B 26 53 65 08 04 16

View File

@ -0,0 +1,97 @@
<MBusData>
<SlaveInformation>
<Id>26718590</Id>
<Manufacturer>HYD</Manufacturer>
<Version>40</Version>
<Medium>Heat: Outlet</Medium>
<AccessNumber>115</AccessNumber>
<Status>50</Status>
<Signature>0000</Signature>
</SlaveInformation>
<DataRecord id="0">
<Function>Instantaneous value</Function>
<Unit>Energy (100 Wh)</Unit>
<Value>0</Value>
</DataRecord>
<DataRecord id="1">
<Function>Instantaneous value</Function>
<Unit>Volume (1e-4 m^3)</Unit>
<Value>742</Value>
</DataRecord>
<DataRecord id="2">
<Function>Value during error state</Function>
<Unit>Power (1e-1 W)</Unit>
<Value>144521543</Value>
</DataRecord>
<DataRecord id="3">
<Function>Value during error state</Function>
<Unit>Volume flow (1e-4 m^3/h)</Unit>
<Value>1521543</Value>
</DataRecord>
<DataRecord id="4">
<Function>Instantaneous value</Function>
<Unit>Flow temperature (1e-1 deg C)</Unit>
<Value>204</Value>
</DataRecord>
<DataRecord id="5">
<Function>Instantaneous value</Function>
<Unit>Return temperature (1e-1 deg C)</Unit>
<Value>204</Value>
</DataRecord>
<DataRecord id="6">
<Function>Instantaneous value</Function>
<Unit>Temperature Difference (1e-1 deg C)</Unit>
<Value>0</Value>
</DataRecord>
<DataRecord id="7">
<Function>Instantaneous value</Function>
<Unit>Time Point (time &amp; date)</Unit>
<Value>2012-01-13T16:34:00</Value>
</DataRecord>
<DataRecord id="8">
<Function>Instantaneous value</Function>
<Unit>Energy (100 Wh)</Unit>
<Value>0</Value>
</DataRecord>
<DataRecord id="9">
<Function>Instantaneous value</Function>
<Unit>Time Point (time &amp; date)</Unit>
<Value>2011-04-30T23:59:00</Value>
</DataRecord>
<DataRecord id="10">
<Function>Instantaneous value</Function>
<Unit>Time Point (time &amp; date)</Unit>
<Value>2012-04-30T23:59:00</Value>
</DataRecord>
<DataRecord id="11">
<Function>Instantaneous value</Function>
<Unit>Energy (100 Wh)</Unit>
<Value>0</Value>
</DataRecord>
<DataRecord id="12">
<Function>Instantaneous value</Function>
<Unit>Time Point (time &amp; date)</Unit>
<Value>2011-12-31T23:59:00</Value>
</DataRecord>
<DataRecord id="13">
<Function>Instantaneous value</Function>
<Unit>Operating time (hours)</Unit>
<Value>86553</Value>
</DataRecord>
</MBusData>

View File

@ -0,0 +1,2 @@
68 3D 3D 68 08 01 72 00 51 20 02 82 4D 02 04 00 88 00 00 04 07 00 00 00 00 0C 15 03 00 00 00 0B 2E 00 00 00 0B 3B 00 00 00 0A 5A 88 12 0A 5E 16 05 0B 61 23 77 00 02 6C 8C 11 02 27 37 0D 0F 60 00 67 16

View File

@ -0,0 +1,72 @@
<MBusData>
<SlaveInformation>
<Id>2205100</Id>
<Manufacturer>SLB</Manufacturer>
<Version>2</Version>
<Medium>Heat: Outlet</Medium>
<AccessNumber>0</AccessNumber>
<Status>88</Status>
<Signature>0000</Signature>
</SlaveInformation>
<DataRecord id="0">
<Function>Instantaneous value</Function>
<Unit>Energy (10 kWh)</Unit>
<Value>0</Value>
</DataRecord>
<DataRecord id="1">
<Function>Instantaneous value</Function>
<Unit>Volume (1e-1 m^3)</Unit>
<Value>3</Value>
</DataRecord>
<DataRecord id="2">
<Function>Instantaneous value</Function>
<Unit>Power (kW)</Unit>
<Value>0</Value>
</DataRecord>
<DataRecord id="3">
<Function>Instantaneous value</Function>
<Unit>Volume flow (m m^3/h)</Unit>
<Value>0</Value>
</DataRecord>
<DataRecord id="4">
<Function>Instantaneous value</Function>
<Unit>Flow temperature (1e-1 deg C)</Unit>
<Value>1288</Value>
</DataRecord>
<DataRecord id="5">
<Function>Instantaneous value</Function>
<Unit>Return temperature (1e-1 deg C)</Unit>
<Value>516</Value>
</DataRecord>
<DataRecord id="6">
<Function>Instantaneous value</Function>
<Unit>Temperature Difference (1e-2 deg C)</Unit>
<Value>7723</Value>
</DataRecord>
<DataRecord id="7">
<Function>Instantaneous value</Function>
<Unit>Time Point (date)</Unit>
<Value>2012-01-12</Value>
</DataRecord>
<DataRecord id="8">
<Function>Instantaneous value</Function>
<Unit>Operating time (days)</Unit>
<Value>3383</Value>
</DataRecord>
<DataRecord id="9">
<Function>Manufacturer specific</Function>
<Value>60 00</Value>
</DataRecord>
</MBusData>

View File

@ -0,0 +1 @@
68 2C 2C 68 08 01 72 45 23 11 70 93 15 02 07 02 00 00 00 0C 13 67 45 23 01 04 6D 3A 0D E6 02 42 6C E1 01 4C 13 51 69 45 00 42 EC 7E 01 11 0F 00 61 16

View File

@ -0,0 +1,48 @@
<MBusData>
<SlaveInformation>
<Id>70112345</Id>
<Manufacturer>ELS</Manufacturer>
<Version>2</Version>
<Medium>Water</Medium>
<AccessNumber>2</AccessNumber>
<Status>00</Status>
<Signature>0000</Signature>
</SlaveInformation>
<DataRecord id="0">
<Function>Instantaneous value</Function>
<Unit>Volume (m m^3)</Unit>
<Value>1234567</Value>
</DataRecord>
<DataRecord id="1">
<Function>Instantaneous value</Function>
<Unit>Time Point (time &amp; date)</Unit>
<Value>2007-02-06T13:58:00</Value>
</DataRecord>
<DataRecord id="2">
<Function>Instantaneous value</Function>
<Unit>Time Point (date)</Unit>
<Value>2007-01-01</Value>
</DataRecord>
<DataRecord id="3">
<Function>Instantaneous value</Function>
<Unit>Volume (m m^3)</Unit>
<Value>456951</Value>
</DataRecord>
<DataRecord id="4">
<Function>Instantaneous value</Function>
<Unit>Time Point (date)</Unit>
<Value>2008-01-01</Value>
</DataRecord>
<DataRecord id="5">
<Function>Manufacturer specific</Function>
<Value>00</Value>
</DataRecord>
</MBusData>

View File

@ -0,0 +1 @@
68 21 21 68 08 01 72 02 37 62 00 A8 15 00 02 07 00 00 00 8C 10 04 09 04 00 00 C4 00 2A 00 00 00 00 01 FD 17 00 8C 16

View File

@ -0,0 +1,31 @@
<MBusData>
<SlaveInformation>
<Id>623702</Id>
<Manufacturer>EMH</Manufacturer>
<Version>0</Version>
<Medium>Electricity</Medium>
<AccessNumber>7</AccessNumber>
<Status>00</Status>
<Signature>0000</Signature>
</SlaveInformation>
<DataRecord id="0">
<Function>Instantaneous value</Function>
<Unit>Energy (10 Wh)</Unit>
<Value>409</Value>
</DataRecord>
<DataRecord id="1">
<Function>Instantaneous value</Function>
<Unit>Power (1e-1 W)</Unit>
<Value>0</Value>
</DataRecord>
<DataRecord id="2">
<Function>Instantaneous value</Function>
<Unit>Error flags</Unit>
<Value>0</Value>
</DataRecord>
</MBusData>

View File

@ -0,0 +1 @@
68 32 32 68 08 05 72 08 06 10 30 52 3B 01 02 01 00 00 00 04 03 FA 04 00 00 04 83 7F FA 04 00 00 02 FD 48 44 09 02 FD 5B 00 00 02 2B 00 00 0C 78 08 06 10 30 0F 0E 71 16

View File

@ -0,0 +1,54 @@
<MBusData>
<SlaveInformation>
<Id>30100608</Id>
<Manufacturer>NZR</Manufacturer>
<Version>1</Version>
<Medium>Electricity</Medium>
<AccessNumber>1</AccessNumber>
<Status>00</Status>
<Signature>0000</Signature>
</SlaveInformation>
<DataRecord id="0">
<Function>Instantaneous value</Function>
<Unit>Energy (Wh)</Unit>
<Value>1274</Value>
</DataRecord>
<DataRecord id="1">
<Function>Instantaneous value</Function>
<Unit>Energy (Wh)</Unit>
<Value>1274</Value>
</DataRecord>
<DataRecord id="2">
<Function>Instantaneous value</Function>
<Unit>1e-1 V</Unit>
<Value>2372</Value>
</DataRecord>
<DataRecord id="3">
<Function>Instantaneous value</Function>
<Unit>1e-1 A</Unit>
<Value>0</Value>
</DataRecord>
<DataRecord id="4">
<Function>Instantaneous value</Function>
<Unit>Power (W)</Unit>
<Value>0</Value>
</DataRecord>
<DataRecord id="5">
<Function>Instantaneous value</Function>
<Unit>Fabrication number</Unit>
<Value>30100608</Value>
</DataRecord>
<DataRecord id="6">
<Function>Manufacturer specific</Function>
<Value>0E</Value>
</DataRecord>
</MBusData>