Merge pull request #60 from lategoodbye/master

Improve event handling and support to send user data
This commit is contained in:
Robert 2013-10-15 09:37:35 -07:00
commit ef0f05c460
15 changed files with 173 additions and 116 deletions

View File

@ -103,19 +103,18 @@ main(int argc, char **argv)
device = argv[c]; device = argv[c];
addr_str = argv[c+1]; addr_str = argv[c+1];
if (debug)
{
mbus_register_send_event(&mbus_dump_send_event);
mbus_register_recv_event(&mbus_dump_recv_event);
}
if ((handle = mbus_context_serial(device)) == NULL) if ((handle = mbus_context_serial(device)) == NULL)
{ {
fprintf(stderr, "Could not initialize M-Bus context: %s\n", mbus_error_str()); fprintf(stderr, "Could not initialize M-Bus context: %s\n", mbus_error_str());
return 1; return 1;
} }
if (debug)
{
mbus_register_send_event(handle, &mbus_dump_send_event);
mbus_register_recv_event(handle, &mbus_dump_recv_event);
}
if (mbus_connect(handle) == -1) if (mbus_connect(handle) == -1)
{ {
fprintf(stderr,"Failed to setup connection to M-bus gateway\n"); fprintf(stderr,"Failed to setup connection to M-bus gateway\n");

View File

@ -64,18 +64,18 @@ main(int argc, char **argv)
return 0; return 0;
} }
if (debug)
{
mbus_register_send_event(&mbus_dump_send_event);
mbus_register_recv_event(&mbus_dump_recv_event);
}
if ((handle = mbus_context_serial(device)) == NULL) if ((handle = mbus_context_serial(device)) == NULL)
{ {
fprintf(stderr, "Could not initialize M-Bus context: %s\n", mbus_error_str()); fprintf(stderr, "Could not initialize M-Bus context: %s\n", mbus_error_str());
return 1; return 1;
} }
if (debug)
{
mbus_register_send_event(handle, &mbus_dump_send_event);
mbus_register_recv_event(handle, &mbus_dump_recv_event);
}
if (mbus_connect(handle) == -1) if (mbus_connect(handle) == -1)
{ {
fprintf(stderr,"Failed to setup connection to M-bus gateway\n"); fprintf(stderr,"Failed to setup connection to M-bus gateway\n");

View File

@ -116,12 +116,6 @@ main(int argc, char **argv)
return 0; return 0;
} }
if (debug)
{
mbus_register_send_event(&mbus_dump_send_event);
mbus_register_recv_event(&mbus_dump_recv_event);
}
if (addr_mask == NULL) if (addr_mask == NULL)
{ {
fprintf(stderr, "Failed to allocate address mask.\n"); fprintf(stderr, "Failed to allocate address mask.\n");
@ -142,6 +136,12 @@ main(int argc, char **argv)
return 1; return 1;
} }
if (debug)
{
mbus_register_send_event(handle, &mbus_dump_send_event);
mbus_register_recv_event(handle, &mbus_dump_recv_event);
}
if (mbus_connect(handle) == -1) if (mbus_connect(handle) == -1)
{ {
fprintf(stderr,"Failed to setup connection to M-bus gateway\n"); fprintf(stderr,"Failed to setup connection to M-bus gateway\n");

View File

@ -108,18 +108,18 @@ main(int argc, char **argv)
return 0; return 0;
} }
if (debug)
{
mbus_register_send_event(&mbus_dump_send_event);
mbus_register_recv_event(&mbus_dump_recv_event);
}
if ((handle = mbus_context_serial(device)) == NULL) if ((handle = mbus_context_serial(device)) == NULL)
{ {
fprintf(stderr,"Scan failed: Could not initialize M-Bus context: %s\n", mbus_error_str()); fprintf(stderr,"Scan failed: Could not initialize M-Bus context: %s\n", mbus_error_str());
return 1; return 1;
} }
if (debug)
{
mbus_register_send_event(handle, &mbus_dump_send_event);
mbus_register_recv_event(handle, &mbus_dump_recv_event);
}
if (mbus_connect(handle) == -1) if (mbus_connect(handle) == -1)
{ {
fprintf(stderr,"Scan failed: Could not setup connection to M-bus gateway: %s\n", mbus_error_str()); fprintf(stderr,"Scan failed: Could not setup connection to M-bus gateway: %s\n", mbus_error_str());

View File

@ -68,18 +68,18 @@ main(int argc, char **argv)
return 1; return 1;
} }
if (debug)
{
mbus_register_send_event(&mbus_dump_send_event);
mbus_register_recv_event(&mbus_dump_recv_event);
}
if ((handle = mbus_context_tcp(host, port)) == NULL) if ((handle = mbus_context_tcp(host, port)) == NULL)
{ {
fprintf(stderr, "Could not initialize M-Bus context: %s\n", mbus_error_str()); fprintf(stderr, "Could not initialize M-Bus context: %s\n", mbus_error_str());
return 1; return 1;
} }
if (debug)
{
mbus_register_send_event(handle, &mbus_dump_send_event);
mbus_register_recv_event(handle, &mbus_dump_recv_event);
}
if (mbus_connect(handle) == -1) if (mbus_connect(handle) == -1)
{ {
fprintf(stderr, "Failed to setup connection to M-bus gateway\n"); fprintf(stderr, "Failed to setup connection to M-bus gateway\n");

View File

@ -79,18 +79,18 @@ main(int argc, char **argv)
return 1; return 1;
} }
if (debug)
{
mbus_register_send_event(&mbus_dump_send_event);
mbus_register_recv_event(&mbus_dump_recv_event);
}
if ((handle = mbus_context_tcp(host, port)) == NULL) if ((handle = mbus_context_tcp(host, port)) == NULL)
{ {
fprintf(stderr, "Could not initialize M-Bus context: %s\n", mbus_error_str()); fprintf(stderr, "Could not initialize M-Bus context: %s\n", mbus_error_str());
return 1; return 1;
} }
if (debug)
{
mbus_register_send_event(handle, &mbus_dump_send_event);
mbus_register_recv_event(handle, &mbus_dump_recv_event);
}
if (mbus_connect(handle) == -1) if (mbus_connect(handle) == -1)
{ {
fprintf(stderr, "Failed to setup connection to M-bus gateway\n%s\n", mbus_error_str()); fprintf(stderr, "Failed to setup connection to M-bus gateway\n%s\n", mbus_error_str());

View File

@ -105,18 +105,18 @@ main(int argc, char **argv)
return 1; return 1;
} }
if (debug)
{
mbus_register_send_event(&mbus_dump_send_event);
mbus_register_recv_event(&mbus_dump_recv_event);
}
if ((handle = mbus_context_tcp(host, port)) == NULL) if ((handle = mbus_context_tcp(host, port)) == NULL)
{ {
fprintf(stderr, "Could not initialize M-Bus context: %s\n", mbus_error_str()); fprintf(stderr, "Could not initialize M-Bus context: %s\n", mbus_error_str());
return 1; return 1;
} }
if (debug)
{
mbus_register_send_event(handle, &mbus_dump_send_event);
mbus_register_recv_event(handle, &mbus_dump_recv_event);
}
if (mbus_connect(handle) == -1) if (mbus_connect(handle) == -1)
{ {
fprintf(stderr,"Failed to setup connection to M-bus gateway\n"); fprintf(stderr,"Failed to setup connection to M-bus gateway\n");

View File

@ -59,18 +59,18 @@ main(int argc, char **argv)
return 1; return 1;
} }
if (debug)
{
mbus_register_send_event(&mbus_dump_send_event);
mbus_register_recv_event(&mbus_dump_recv_event);
}
if ((handle = mbus_context_tcp(host, port)) == NULL) if ((handle = mbus_context_tcp(host, port)) == NULL)
{ {
fprintf(stderr, "Could not initialize M-Bus context: %s\n", mbus_error_str()); fprintf(stderr, "Could not initialize M-Bus context: %s\n", mbus_error_str());
return 1; return 1;
} }
if (debug)
{
mbus_register_send_event(handle, &mbus_dump_send_event);
mbus_register_recv_event(handle, &mbus_dump_recv_event);
}
if (mbus_connect(handle) == -1) if (mbus_connect(handle) == -1)
{ {
fprintf(stderr, "Failed to setup connection to M-bus gateway\n"); fprintf(stderr, "Failed to setup connection to M-bus gateway\n");

View File

@ -94,18 +94,18 @@ main(int argc, char **argv)
return 1; return 1;
} }
if (debug)
{
mbus_register_send_event(&mbus_dump_send_event);
mbus_register_recv_event(&mbus_dump_recv_event);
}
if ((handle = mbus_context_tcp(host, port)) == NULL) if ((handle = mbus_context_tcp(host, port)) == NULL)
{ {
fprintf(stderr,"Scan failed: Could not initialize M-Bus context: %s\n", mbus_error_str()); fprintf(stderr,"Scan failed: Could not initialize M-Bus context: %s\n", mbus_error_str());
return 1; return 1;
} }
if (debug)
{
mbus_register_send_event(handle, &mbus_dump_send_event);
mbus_register_recv_event(handle, &mbus_dump_recv_event);
}
if (mbus_connect(handle) == -1) if (mbus_connect(handle) == -1)
{ {
fprintf(stderr,"Scan failed: Could not setup connection to M-bus gateway: %s\n", mbus_error_str()); fprintf(stderr,"Scan failed: Could not setup connection to M-bus gateway: %s\n", mbus_error_str());

View File

@ -707,25 +707,40 @@ mbus_variable_vif fixed_table[] = {
{ 0xFFFF, 0.0, "", "" }, { 0xFFFF, 0.0, "", "" },
}; };
void (*_mbus_scan_progress)(mbus_handle * handle, const char *mask) = NULL; //------------------------------------------------------------------------------
void (*_mbus_found_event)(mbus_handle * handle, mbus_frame *frame) = NULL; /// Register a function for receive events.
//------------------------------------------------------------------------------
void
mbus_register_recv_event(mbus_handle * handle, void (*event)(unsigned char src_type, const char *buff, size_t len))
{
handle->recv_event = event;
}
//------------------------------------------------------------------------------
/// Register a function for send events.
//------------------------------------------------------------------------------
void
mbus_register_send_event(mbus_handle * handle, void (*event)(unsigned char src_type, const char *buff, size_t len))
{
handle->send_event = event;
}
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
/// Register a function for the scan progress. /// Register a function for the scan progress.
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
void void
mbus_register_scan_progress(void (*event)(mbus_handle * handle, const char *mask)) mbus_register_scan_progress(mbus_handle * handle, void (*event)(mbus_handle * handle, const char *mask))
{ {
_mbus_scan_progress = event; handle->scan_progress = event;
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
/// Register a function for the found events. /// Register a function for the found events.
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
void void
mbus_register_found_event(void (*event)(mbus_handle * handle, mbus_frame *frame)) mbus_register_found_event(mbus_handle * handle, void (*event)(mbus_handle * handle, mbus_frame *frame))
{ {
_mbus_found_event = event; handle->found_event = event;
} }
int mbus_fixed_normalize(int medium_unit, long medium_value, char **unit_out, double *value_out, char **quantity_out) int mbus_fixed_normalize(int medium_unit, long medium_value, char **unit_out, double *value_out, char **quantity_out)
@ -1424,6 +1439,10 @@ mbus_context_serial(const char *device)
handle->recv = mbus_serial_recv_frame; handle->recv = mbus_serial_recv_frame;
handle->send = mbus_serial_send_frame; handle->send = mbus_serial_send_frame;
handle->free_auxdata = mbus_serial_data_free; handle->free_auxdata = mbus_serial_data_free;
handle->recv_event = NULL;
handle->send_event = NULL;
handle->scan_progress = NULL;
handle->found_event = NULL;
if ((serial_data->device = strdup(device)) == NULL) if ((serial_data->device = strdup(device)) == NULL)
{ {
@ -1467,6 +1486,10 @@ mbus_context_tcp(const char *host, uint16_t port)
handle->recv = mbus_tcp_recv_frame; handle->recv = mbus_tcp_recv_frame;
handle->send = mbus_tcp_send_frame; handle->send = mbus_tcp_send_frame;
handle->free_auxdata = mbus_tcp_data_free; handle->free_auxdata = mbus_tcp_data_free;
handle->recv_event = NULL;
handle->send_event = NULL;
handle->scan_progress = NULL;
handle->found_event = NULL;
tcp_data->port = port; tcp_data->port = port;
if ((tcp_data->host = strdup(host)) == NULL) if ((tcp_data->host = strdup(host)) == NULL)
@ -1811,6 +1834,57 @@ mbus_send_request_frame(mbus_handle * handle, int address)
return retval; return retval;
} }
//------------------------------------------------------------------------------
// send a user data packet from master to slave
//------------------------------------------------------------------------------
int
mbus_send_user_data_frame(mbus_handle * handle, int address, const unsigned char *data, size_t data_size)
{
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;
}
if (data == NULL)
{
MBUS_ERROR("%s: Invalid data\n", __PRETTY_FUNCTION__);
return -1;
}
if ((data_size > MBUS_FRAME_DATA_LENGTH) || (data_size == 0))
{
MBUS_ERROR("%s: illegal data_size %d\n", __PRETTY_FUNCTION__, data_size);
return -1;
}
frame = mbus_frame_new(MBUS_FRAME_TYPE_LONG);
if (frame == NULL)
{
MBUS_ERROR("%s: failed to allocate mbus frame.\n", __PRETTY_FUNCTION__);
return -1;
}
frame->control = MBUS_CONTROL_MASK_SND_UD | MBUS_CONTROL_MASK_DIR_M2S;
frame->address = address;
frame->control_information = MBUS_CONTROL_INFO_DATA_SEND;
frame->data_size = data_size;
memcpy(frame->data, data, data_size);
if (mbus_send_frame(handle, frame) == -1)
{
MBUS_ERROR("%s: failed to send mbus frame.\n", __PRETTY_FUNCTION__);
retval = -1;
}
mbus_frame_free(frame);
return retval;
}
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// send a request from master to slave and collect the reply (replies) // send a request from master to slave and collect the reply (replies)
// from the slave. // from the slave.
@ -2138,9 +2212,9 @@ mbus_probe_secondary_address(mbus_handle *handle, const char *mask, char *matchi
snprintf(matching_addr, 17, "%s", addr); snprintf(matching_addr, 17, "%s", addr);
if (_mbus_found_event) if (handle->found_event)
{ {
_mbus_found_event(handle,&reply); handle->found_event(handle,&reply);
} }
return MBUS_PROBE_SINGLE; return MBUS_PROBE_SINGLE;
@ -2284,14 +2358,14 @@ mbus_scan_2nd_address_range(mbus_handle * handle, int pos, char *addr_mask)
{ {
mask[pos] = '0'+i; mask[pos] = '0'+i;
if (_mbus_scan_progress) if (handle->scan_progress)
_mbus_scan_progress(handle,mask); handle->scan_progress(handle,mask);
probe_ret = mbus_probe_secondary_address(handle, mask, matching_mask); probe_ret = mbus_probe_secondary_address(handle, mask, matching_mask);
if (probe_ret == MBUS_PROBE_SINGLE) if (probe_ret == MBUS_PROBE_SINGLE)
{ {
if (!_mbus_found_event) if (!handle->found_event)
{ {
printf("Found a device on secondary address %s [using address mask %s]\n", matching_mask, mask); printf("Found a device on secondary address %s [using address mask %s]\n", matching_mask, mask);
} }

View File

@ -93,6 +93,10 @@ typedef struct _mbus_handle {
int (*send) (struct _mbus_handle *handle, mbus_frame *frame); int (*send) (struct _mbus_handle *handle, mbus_frame *frame);
int (*recv) (struct _mbus_handle *handle, mbus_frame *frame); int (*recv) (struct _mbus_handle *handle, mbus_frame *frame);
void (*free_auxdata) (struct _mbus_handle *handle); void (*free_auxdata) (struct _mbus_handle *handle);
void (*recv_event) (unsigned char src_type, const char *buff, size_t len);
void (*send_event) (unsigned char src_type, const char *buff, size_t len);
void (*scan_progress) (struct _mbus_handle *handle, const char *mask);
void (*found_event) (struct _mbus_handle *handle, mbus_frame *frame);
void *auxdata; void *auxdata;
} mbus_handle; } mbus_handle;
@ -148,17 +152,16 @@ typedef enum _mbus_context_option {
MBUS_OPTION_PURGE_FIRST_FRAME /**< option controls the echo cancelation for mbus_recv_frame */ MBUS_OPTION_PURGE_FIRST_FRAME /**< option controls the echo cancelation for mbus_recv_frame */
} mbus_context_option; } mbus_context_option;
/**
* Event callback functions
*/
extern void (*_mbus_scan_progress)(mbus_handle * handle, const char *mask);
extern void (*_mbus_found_event)(mbus_handle * handle, mbus_frame *frame);
/** /**
* Event register functions * Event register functions
*/ */
void mbus_register_scan_progress(void (*event)(mbus_handle * handle, const char *mask)); //
void mbus_register_found_event(void (*event)(mbus_handle * handle, mbus_frame *frame)); // Event register functions
//
void mbus_register_recv_event(mbus_handle *handle, void (*event)(unsigned char src_type, const char *buff, size_t len));
void mbus_register_send_event(mbus_handle *handle, void (*event)(unsigned char src_type, const char *buff, size_t len));
void mbus_register_scan_progress(mbus_handle *handle, void (*event)(mbus_handle *handle, const char *mask));
void mbus_register_found_event(mbus_handle *handle, void (*event)(mbus_handle *handle, mbus_frame *frame));
/** /**
* Allocate and initialize M-Bus serial context. * Allocate and initialize M-Bus serial context.
@ -287,6 +290,18 @@ int mbus_send_switch_baudrate_frame(mbus_handle * handle, int address, long baud
*/ */
int mbus_send_request_frame(mbus_handle * handle, int address); int mbus_send_request_frame(mbus_handle * handle, int address);
/**
* Sends user data frame (SND_UD) to given slave using "unified" handle
*
* @param handle Initialized handle
* @param address Address (0-255)
* @param data User data
* @param data_size Byte count of user data
*
* @return Zero when successful.
*/
int mbus_send_user_data_frame(mbus_handle * handle, int address, const unsigned char *data, size_t data_size);
/** /**
* Sends a request and read replies until no more records available * Sends a request and read replies until no more records available
* or limit is reached. * or limit is reached.

View File

@ -26,12 +26,6 @@ static char error_str[512];
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
static mbus_slave_data slave_data[MBUS_MAX_PRIMARY_SLAVES]; static mbus_slave_data slave_data[MBUS_MAX_PRIMARY_SLAVES];
//
// init event callback
//
void (*_mbus_recv_event)(unsigned char src_type, const char *buff, size_t len) = NULL;
void (*_mbus_send_event)(unsigned char src_type, const char *buff, size_t len) = NULL;
// //
// trace callbacks // trace callbacks
// //
@ -47,24 +41,6 @@ mbus_dump_send_event(unsigned char src_type, const char *buff, size_t len)
mbus_hex_dump("SEND", buff, len); mbus_hex_dump("SEND", buff, len);
} }
//------------------------------------------------------------------------------
/// Register a function for receive events.
//------------------------------------------------------------------------------
void
mbus_register_recv_event(void (*event)(unsigned char src_type, const char *buff, size_t len))
{
_mbus_recv_event = event;
}
//------------------------------------------------------------------------------
/// Register a function for send events.
//------------------------------------------------------------------------------
void
mbus_register_send_event(void (*event)(unsigned char src_type, const char *buff, size_t len))
{
_mbus_send_event = event;
}
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
/// Return a string that contains an the latest error message. /// Return a string that contains an the latest error message.
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------

View File

@ -70,6 +70,8 @@ extern "C" {
// //
// //
#define MBUS_FRAME_DATA_LENGTH 252
typedef struct _mbus_frame { typedef struct _mbus_frame {
unsigned char start1; unsigned char start1;
@ -83,7 +85,7 @@ typedef struct _mbus_frame {
unsigned char checksum; unsigned char checksum;
unsigned char stop; unsigned char stop;
unsigned char data[252]; unsigned char data[MBUS_FRAME_DATA_LENGTH];
size_t data_size; size_t data_size;
int type; int type;
@ -511,18 +513,9 @@ typedef struct _mbus_data_secondary_address {
// //
// Event callback functions // Event callback functions
// //
extern void (*_mbus_recv_event)(unsigned char src_type, const char *buff, size_t len);
extern void (*_mbus_send_event)(unsigned char src_type, const char *buff, size_t len);
void mbus_dump_recv_event(unsigned char src_type, const char *buff, size_t len); void mbus_dump_recv_event(unsigned char src_type, const char *buff, size_t len);
void mbus_dump_send_event(unsigned char src_type, const char *buff, size_t len); void mbus_dump_send_event(unsigned char src_type, const char *buff, size_t len);
//
// Event register functions
//
void mbus_register_recv_event(void (*event)(unsigned char src_type, const char *buff, size_t len));
void mbus_register_send_event(void (*event)(unsigned char src_type, const char *buff, size_t len));
// //
// variable length records // variable length records
// //

View File

@ -255,8 +255,8 @@ mbus_serial_send_frame(mbus_handle *handle, mbus_frame *frame)
// //
// call the send event function, if the callback function is registered // call the send event function, if the callback function is registered
// //
if (_mbus_send_event) if (handle->send_event)
_mbus_send_event(MBUS_HANDLE_TYPE_SERIAL, buff, len); handle->send_event(MBUS_HANDLE_TYPE_SERIAL, buff, len);
} }
else else
{ {
@ -353,8 +353,8 @@ mbus_serial_recv_frame(mbus_handle *handle, mbus_frame *frame)
// //
// call the receive event function, if the callback function is registered // call the receive event function, if the callback function is registered
// //
if (_mbus_recv_event) if (handle->recv_event)
_mbus_recv_event(MBUS_HANDLE_TYPE_SERIAL, buff, len); handle->recv_event(MBUS_HANDLE_TYPE_SERIAL, buff, len);
if (remaining != 0) if (remaining != 0)
{ {

View File

@ -159,8 +159,8 @@ mbus_tcp_send_frame(mbus_handle *handle, mbus_frame *frame)
// //
// call the send event function, if the callback function is registered // call the send event function, if the callback function is registered
// //
if (_mbus_send_event) if (handle->send_event)
_mbus_send_event(MBUS_HANDLE_TYPE_TCP, buff, len); handle->send_event(MBUS_HANDLE_TYPE_TCP, buff, len);
} }
else else
{ {
@ -232,8 +232,8 @@ retry:
// //
// call the receive event function, if the callback function is registered // call the receive event function, if the callback function is registered
// //
if (_mbus_recv_event) if (handle->recv_event)
_mbus_recv_event(MBUS_HANDLE_TYPE_TCP, buff, len); handle->recv_event(MBUS_HANDLE_TYPE_TCP, buff, len);
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.");