Added 2 event callbacks to handle send and receive events outside the
library (now it is possible to trigger a led from an application if m-bus data is received)
This commit is contained in:
parent
d617accea5
commit
8f48d75fb0
@ -25,6 +25,30 @@ static char error_str[512];
|
||||
//------------------------------------------------------------------------------
|
||||
static mbus_slave_data slave_data[MBUS_MAX_PRIMARY_SLAVES];
|
||||
|
||||
//
|
||||
// init event callback
|
||||
//
|
||||
void (*_mbus_recv_event)(u_char src_type) = NULL;
|
||||
void (*_mbus_send_event)(u_char src_type) = NULL;
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
/// Register a function for receive events.
|
||||
//------------------------------------------------------------------------------
|
||||
void
|
||||
mbus_register_recv_event(void (*event)(u_char src_type))
|
||||
{
|
||||
_mbus_recv_event = event;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
/// Register a function for send events.
|
||||
//------------------------------------------------------------------------------
|
||||
void
|
||||
mbus_register_send_event(void (*event)(u_char src_type))
|
||||
{
|
||||
_mbus_send_event = event;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
/// Return a string that contains an the latest error message.
|
||||
//------------------------------------------------------------------------------
|
||||
|
@ -99,6 +99,12 @@ typedef struct _mbus_slave_data {
|
||||
|
||||
#define NITEMS(x) (sizeof(x)/sizeof(x[0]))
|
||||
|
||||
//
|
||||
// Supported handle types
|
||||
//
|
||||
#define MBUS_HANDLE_TYPE_TCP 0
|
||||
#define MBUS_HANDLE_TYPE_SERIAL 1
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// MBUS FRAME DATA FORMATS
|
||||
//
|
||||
@ -453,6 +459,17 @@ typedef struct _mbus_data_secondary_address {
|
||||
#define MBUS_VARIABLE_DATA_MEDIUM_PRESSURE 0x18
|
||||
#define MBUS_VARIABLE_DATA_MEDIUM_ADC 0x19
|
||||
|
||||
//
|
||||
// Event callback functions
|
||||
//
|
||||
extern void (*_mbus_recv_event)(u_char src_type);
|
||||
extern void (*_mbus_send_event)(u_char src_type);
|
||||
|
||||
//
|
||||
// Event register functions
|
||||
//
|
||||
void mbus_register_recv_event(void (*event)(u_char src_type));
|
||||
void mbus_register_send_event(void (*event)(u_char src_type));
|
||||
|
||||
//
|
||||
// variable length records
|
||||
|
@ -167,7 +167,15 @@ mbus_serial_send_frame(mbus_serial_handle *handle, mbus_frame *frame)
|
||||
return -1;
|
||||
}
|
||||
|
||||
if ((ret = write(handle->fd, buff, len)) != len)
|
||||
if ((ret = write(handle->fd, buff, len)) == len)
|
||||
{
|
||||
//
|
||||
// call the send event function, if the callback function is registered
|
||||
//
|
||||
if (_mbus_send_event)
|
||||
_mbus_send_event(MBUS_HANDLE_TYPE_SERIAL);
|
||||
}
|
||||
else
|
||||
{
|
||||
fprintf(stderr, "%s: Failed to write frame to socket (ret = %d: %s)\n", __PRETTY_FUNCTION__, ret, strerror(errno));
|
||||
return -1;
|
||||
@ -196,7 +204,17 @@ mbus_serial_recv_frame(mbus_serial_handle *handle, mbus_frame *frame)
|
||||
do {
|
||||
//printf("%s: Attempt to read %d bytes [len = %d]\n", __PRETTY_FUNCTION__, remaining, len);
|
||||
|
||||
if ((nread = read(handle->fd, &buff[len], remaining)) == -1)
|
||||
nread = read(handle->fd, &buff[len], remaining);
|
||||
|
||||
if (nread > 0)
|
||||
{
|
||||
//
|
||||
// call the receive event function, if the callback function is registered
|
||||
//
|
||||
if (_mbus_recv_event)
|
||||
_mbus_recv_event(MBUS_HANDLE_TYPE_SERIAL);
|
||||
}
|
||||
else if (nread == -1)
|
||||
{
|
||||
// fprintf(stderr, "%s: aborting recv frame (remaining = %d, len = %d, nread = %d)\n",
|
||||
// __PRETTY_FUNCTION__, remaining, len, nread);
|
||||
|
@ -138,7 +138,15 @@ mbus_tcp_send_frame(mbus_tcp_handle *handle, mbus_frame *frame)
|
||||
return -1;
|
||||
}
|
||||
|
||||
if ((ret = write(handle->sock, buff, len)) != len)
|
||||
if ((ret = write(handle->sock, buff, len)) == len)
|
||||
{
|
||||
//
|
||||
// call the send event function, if the callback function is registered
|
||||
//
|
||||
if (_mbus_send_event)
|
||||
_mbus_send_event(MBUS_HANDLE_TYPE_TCP);
|
||||
}
|
||||
else
|
||||
{
|
||||
char error_str[128];
|
||||
snprintf(error_str, sizeof(error_str), "%s: Failed to write frame to socket (ret = %d)\n", __PRETTY_FUNCTION__, ret);
|
||||
@ -167,8 +175,18 @@ mbus_tcp_recv_frame(mbus_tcp_handle *handle, mbus_frame *frame)
|
||||
len = 0;
|
||||
|
||||
do {
|
||||
|
||||
nread = read(handle->sock, &buff[len], remaining);
|
||||
|
||||
if ((nread = read(handle->sock, &buff[len], remaining)) == -1)
|
||||
if (nread > 0)
|
||||
{
|
||||
//
|
||||
// call the receive event function, if the callback function is registered
|
||||
//
|
||||
if (_mbus_recv_event)
|
||||
_mbus_recv_event(MBUS_HANDLE_TYPE_TCP);
|
||||
}
|
||||
else if (nread == -1)
|
||||
{
|
||||
mbus_error_str_set("M-Bus tcp transport layer failed to read data.");
|
||||
return -1;
|
||||
|
Loading…
x
Reference in New Issue
Block a user