Rebuilding M-Bus context structs in progress

- changed local library file includes from <> to ""
- get rid of 'if (is_serial)' conditions
- mbus_context_serial() and mbus_context_tcp() allocates memory and initialize context specific function pointers
- simply mbus_connect() called instead of mbus_connect_tcp() or mbus_connect_serial()
- context specific data can still be accessed via (void* auxdata) pointer
- strdup(3) to copy host IP into TCP context struct
This commit is contained in:
jakubovsky
2012-07-04 17:18:36 +02:00
parent 9f9e0fc008
commit c848090f56
8 changed files with 223 additions and 212 deletions

View File

@ -12,6 +12,8 @@
//------------------------------------------------------------------------------
#include "mbus-protocol-aux.h"
#include "mbus-serial.h"
#include "mbus-tcp.h"
#include <stdio.h>
#include <string.h>
@ -1308,53 +1310,98 @@ mbus_frame_data_xml_normalized(mbus_frame_data *data)
}
mbus_handle *
mbus_connect_serial(const char * device)
mbus_context_serial(const char *device)
{
mbus_serial_handle * serial_handle;
if ((serial_handle = mbus_serial_connect((char*)device)) == NULL)
{
MBUS_ERROR("%s: Failed to setup serial connection to M-bus gateway on %s.\n",
__PRETTY_FUNCTION__,
device);
return NULL;
}
mbus_handle *handle;
mbus_serial_handle *serial_data;
char error_str[128];
mbus_handle * handle;
if ((handle = (mbus_handle * ) malloc(sizeof(mbus_handle))) == NULL)
if ((handle = (mbus_handle *) malloc(sizeof(mbus_handle))) == NULL)
{
MBUS_ERROR("%s: Failed to allocate handle.\n", __PRETTY_FUNCTION__);
return NULL;
}
if ((serial_data = (mbus_serial_handle *)malloc(sizeof(mbus_serial_handle))) == NULL)
{
snprintf(error_str, sizeof(error_str), "%s: failed to allocate memory for handle\n", __PRETTY_FUNCTION__);
mbus_error_str_set(error_str);
free(handle);
return NULL;
}
handle->is_serial = 1;
handle->m_serial_handle = serial_handle;
handle->auxdata = serial_data;
handle->open = mbus_serial_connect;
handle->close = mbus_serial_disconnect;
handle->recv = mbus_serial_recv_frame;
handle->send = mbus_serial_send_frame;
if ((serial_data->device = strdup(device)) == NULL)
{
snprintf(error_str, sizeof(error_str), "%s: failed to allocate memory for device\n", __PRETTY_FUNCTION__);
mbus_error_str_set(error_str);
free(serial_data);
free(handle);
return NULL;
}
return handle;
}
mbus_handle *
mbus_connect_tcp(const char * host, int port)
mbus_context_tcp(const char *host, int port)
{
mbus_tcp_handle * tcp_handle;
if ((tcp_handle = mbus_tcp_connect((char*)host, port)) == NULL)
{
MBUS_ERROR("%s: Failed to setup tcp connection to M-bus gateway on %s, port %d.\n",
__PRETTY_FUNCTION__,
host,
port);
return NULL;
}
mbus_handle *handle;
mbus_tcp_handle *tcp_data;
char error_str[128];
mbus_handle * handle;
if ((handle = (mbus_handle * ) malloc(sizeof(mbus_handle))) == NULL)
if ((handle = (mbus_handle *) malloc(sizeof(mbus_handle))) == NULL)
{
MBUS_ERROR("%s: Failed to allocate handle.\n", __PRETTY_FUNCTION__);
return NULL;
}
if ((tcp_data = (mbus_tcp_handle *)malloc(sizeof(mbus_tcp_handle))) == NULL)
{
snprintf(error_str, sizeof(error_str), "%s: failed to allocate memory for handle\n", __PRETTY_FUNCTION__);
mbus_error_str_set(error_str);
free(handle);
return NULL;
}
handle->is_serial = 0;
handle->m_tcp_handle = tcp_handle;
handle->auxdata = tcp_data;
handle->open = mbus_tcp_connect;
handle->close = mbus_tcp_disconnect;
handle->recv = mbus_tcp_recv_frame;
handle->send = mbus_tcp_send_frame;
tcp_data->port = port;
if ((tcp_data->host = strdup(host)) == NULL)
{
snprintf(error_str, sizeof(error_str), "%s: failed to allocate memory for host\n", __PRETTY_FUNCTION__);
mbus_error_str_set(error_str);
free(tcp_data);
free(handle);
return NULL;
}
return handle;
}
int
modbus_connect(mbus_handle * handle)
{
if (handle == NULL)
{
MBUS_ERROR("%s: Invalid M-Bus handle for disconnect.\n", __PRETTY_FUNCTION__);
return 0;
}
return handle->open(handle);
}
int
mbus_disconnect(mbus_handle * handle)
{
@ -1364,18 +1411,7 @@ mbus_disconnect(mbus_handle * handle)
return 0;
}
if (handle->is_serial)
{
mbus_serial_disconnect(handle->m_serial_handle);
handle->m_serial_handle = NULL;
}
else
{
mbus_tcp_disconnect(handle->m_tcp_handle);
handle->m_tcp_handle = NULL;
}
free(handle);
return 0;
return handle->close(handle);
}
int
@ -1389,14 +1425,7 @@ mbus_recv_frame(mbus_handle * handle, mbus_frame *frame)
return -1;
}
if (handle->is_serial)
{
result = mbus_serial_recv_frame(handle->m_serial_handle, frame);
}
else
{
result = mbus_tcp_recv_frame(handle->m_tcp_handle, frame);
}
result = handle->recv(handle, frame);
if (frame != NULL)
{
@ -1434,15 +1463,7 @@ mbus_send_frame(mbus_handle * handle, mbus_frame *frame)
return 0;
}
if (handle->is_serial)
{
return mbus_serial_send_frame(handle->m_serial_handle, frame);
}
else
{
return mbus_tcp_send_frame(handle->m_tcp_handle, frame);
}
return 0;
return handle->send(handle, frame);
}
//------------------------------------------------------------------------------