Move event handling from global variable to handle
- remove global event function pointer - add event function pointer to handle structure - add handle parameter to register functions - move all register functions to mbus-protocol-aux.c
This commit is contained in:
parent
9e23818152
commit
0cf57bfc35
@ -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)
|
||||||
@ -2189,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;
|
||||||
@ -2335,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);
|
||||||
}
|
}
|
||||||
|
@ -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.
|
||||||
|
@ -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.
|
||||||
//------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------
|
||||||
|
@ -513,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
|
||||||
//
|
//
|
||||||
|
@ -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)
|
||||||
{
|
{
|
||||||
|
@ -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.");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user