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:
parent
9f9e0fc008
commit
c848090f56
@ -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);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
@ -60,28 +60,27 @@
|
||||
#ifndef __MBUS_PROTOCOL_AUX_H__
|
||||
#define __MBUS_PROTOCOL_AUX_H__
|
||||
|
||||
#include <mbus/mbus.h>
|
||||
#include <mbus/mbus-protocol.h>
|
||||
#include <mbus/mbus-serial.h>
|
||||
#include <mbus/mbus-tcp.h>
|
||||
#include "mbus-protocol.h"
|
||||
|
||||
#define MBUS_PROBE_NOTHING 0
|
||||
#define MBUS_PROBE_SINGLE 1
|
||||
#define MBUS_PROBE_COLLISION 2
|
||||
#define MBUS_PROBE_ERROR -1
|
||||
|
||||
|
||||
/**
|
||||
* Unified MBus handle type encapsulating either Serial or TCP gateway.
|
||||
*/
|
||||
typedef struct _mbus_handle {
|
||||
struct _mbus_handle {
|
||||
int fd;
|
||||
char is_serial; /**< _handle type (non zero for serial) */
|
||||
union {
|
||||
mbus_tcp_handle * m_tcp_handle; /**< TCP gateway handle */
|
||||
mbus_serial_handle * m_serial_handle; /**< Serial gateway handle */
|
||||
};
|
||||
} mbus_handle;
|
||||
int (*open) (struct _mbus_handle *handle);
|
||||
int (*close) (struct _mbus_handle *handle);
|
||||
int (*send) (struct _mbus_handle *handle, mbus_frame *frame);
|
||||
int (*recv) (struct _mbus_handle *handle, mbus_frame *frame);
|
||||
void *auxdata;
|
||||
};
|
||||
|
||||
typedef struct _mbus_handle mbus_handle;
|
||||
|
||||
/**
|
||||
* MBus slave address type (primary/secodary address)
|
||||
@ -141,23 +140,32 @@ void mbus_register_scan_progress(void (*event)(mbus_handle * handle, const char
|
||||
void mbus_register_found_event(void (*event)(mbus_handle * handle, mbus_frame *frame));
|
||||
|
||||
/**
|
||||
* Connects to serial gateway and initializes MBus handle
|
||||
* Allocate and initialize M-Bus serial context.
|
||||
*
|
||||
* @param device Serial device (like /dev/ttyUSB0 or /dev/ttyS0)
|
||||
*
|
||||
* @return Initialized "unified" handler when successful, NULL otherwise;
|
||||
*/
|
||||
mbus_handle * mbus_connect_serial(const char * device);
|
||||
mbus_handle * mbus_context_serial(const char *device);
|
||||
|
||||
/**
|
||||
* Connects to TCP gateway and initializes MBus handle
|
||||
* Allocate and initialize M-Bus TCP context.
|
||||
*
|
||||
* @param host Gateway host
|
||||
* @param port Gateway port
|
||||
*
|
||||
* @return Initialized "unified" handler when successful, NULL otherwise;
|
||||
*/
|
||||
mbus_handle * mbus_connect_tcp(const char *host, int port);
|
||||
mbus_handle * mbus_context_tcp(const char *host, int port);
|
||||
|
||||
/**
|
||||
* Connect to serial bus or TCP gateway depending on context.
|
||||
*
|
||||
* @param handle Initialized handle
|
||||
*
|
||||
* @return Zero when successful.
|
||||
*/
|
||||
int mbus_connect(mbus_handle * handle);
|
||||
|
||||
/**
|
||||
* Disconnects the "unified" handle.
|
||||
|
2
mbus/mbus-protocol.c
Normal file → Executable file
2
mbus/mbus-protocol.c
Normal file → Executable file
@ -13,7 +13,7 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <mbus/mbus-protocol.h>
|
||||
#include "mbus-protocol.h"
|
||||
|
||||
static int parse_debug = 0, debug = 0;
|
||||
static char error_str[512];
|
||||
|
0
mbus/mbus-protocol.h
Normal file → Executable file
0
mbus/mbus-protocol.h
Normal file → Executable file
151
mbus/mbus-serial.c
Normal file → Executable file
151
mbus/mbus-serial.c
Normal file → Executable file
@ -20,109 +20,112 @@
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <mbus/mbus.h>
|
||||
#include <mbus/mbus-serial.h>
|
||||
#include "mbus-serial.h"
|
||||
#include "mbus-protocol-aux.h"
|
||||
#include "mbus-protocol.h"
|
||||
|
||||
#define PACKET_BUFF_SIZE 2048
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
/// Set up a serial connection handle.
|
||||
//------------------------------------------------------------------------------
|
||||
mbus_serial_handle *
|
||||
mbus_serial_connect(char *device)
|
||||
int
|
||||
mbus_serial_connect(mbus_handle *handle)
|
||||
{
|
||||
mbus_serial_handle *handle;
|
||||
// mbus_serial_handle *serial_data;
|
||||
//
|
||||
// if (device == NULL)
|
||||
// {
|
||||
// return NULL;
|
||||
// }
|
||||
//
|
||||
// if ((handle = (mbus_serial_handle *)malloc(sizeof(mbus_serial_handle))) == NULL)
|
||||
// {
|
||||
// fprintf(stderr, "%s: failed to allocate memory for handle\n", __PRETTY_FUNCTION__);
|
||||
// return NULL;
|
||||
// }
|
||||
//
|
||||
// handle->device = device; // strdup?
|
||||
//
|
||||
// //
|
||||
// // create the SERIAL connection
|
||||
// //
|
||||
//
|
||||
// // Use blocking read and handle it by serial port VMIN/VTIME setting
|
||||
// if ((handle->fd = open(handle->device, O_RDWR | O_NOCTTY)) < 0)
|
||||
// {
|
||||
// fprintf(stderr, "%s: failed to open tty.", __PRETTY_FUNCTION__);
|
||||
// return NULL;
|
||||
// }
|
||||
//
|
||||
// memset(&(handle->t), 0, sizeof(handle->t));
|
||||
// handle->t.c_cflag |= (CS8|CREAD|CLOCAL);
|
||||
// handle->t.c_cflag |= PARENB;
|
||||
//
|
||||
// // No received data still OK
|
||||
// handle->t.c_cc[VMIN] = 0;
|
||||
//
|
||||
// // Wait at most 0.2 sec.Note that it starts after first received byte!!
|
||||
// // I.e. if CMIN>0 and there are no data we would still wait forever...
|
||||
// //
|
||||
// // The specification mentions link layer response timeout this way:
|
||||
// // The time structure of various link layer communication types is described in EN60870-5-1. The answer time
|
||||
// // between the end of a master send telegram and the beginning of the response telegram of the slave shall be
|
||||
// // between 11 bit times and (330 bit times + 50ms).
|
||||
// //
|
||||
// // For 2400Bd this means (330 + 11) / 2400 + 0.05 = 188.75 ms (added 11 bit periods to receive first byte).
|
||||
// // I.e. timeout of 0.2s seems appropriate for 2400Bd.
|
||||
//
|
||||
// handle->t.c_cc[VTIME] = 2; // Timeout in 1/10 sec
|
||||
//
|
||||
// cfsetispeed(&(handle->t), B2400);
|
||||
// cfsetospeed(&(handle->t), B2400);
|
||||
//
|
||||
//#ifdef MBUS_SERIAL_DEBUG
|
||||
// printf("%s: t.c_cflag = %x\n", __PRETTY_FUNCTION__, handle->t.c_cflag);
|
||||
// printf("%s: t.c_oflag = %x\n", __PRETTY_FUNCTION__, handle->t.c_oflag);
|
||||
// printf("%s: t.c_iflag = %x\n", __PRETTY_FUNCTION__, handle->t.c_iflag);
|
||||
// printf("%s: t.c_lflag = %x\n", __PRETTY_FUNCTION__, handle->t.c_lflag);
|
||||
//#endif
|
||||
//
|
||||
// tcsetattr(handle->fd, TCSANOW, &(handle->t));
|
||||
|
||||
if (device == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if ((handle = (mbus_serial_handle *)malloc(sizeof(mbus_serial_handle))) == NULL)
|
||||
{
|
||||
fprintf(stderr, "%s: failed to allocate memory for handle\n", __PRETTY_FUNCTION__);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
handle->device = device; // strdup?
|
||||
|
||||
//
|
||||
// create the SERIAL connection
|
||||
//
|
||||
|
||||
// Use blocking read and handle it by serial port VMIN/VTIME setting
|
||||
if ((handle->fd = open(handle->device, O_RDWR | O_NOCTTY)) < 0)
|
||||
{
|
||||
fprintf(stderr, "%s: failed to open tty.", __PRETTY_FUNCTION__);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memset(&(handle->t), 0, sizeof(handle->t));
|
||||
handle->t.c_cflag |= (CS8|CREAD|CLOCAL);
|
||||
handle->t.c_cflag |= PARENB;
|
||||
|
||||
// No received data still OK
|
||||
handle->t.c_cc[VMIN] = 0;
|
||||
|
||||
// Wait at most 0.2 sec.Note that it starts after first received byte!!
|
||||
// I.e. if CMIN>0 and there are no data we would still wait forever...
|
||||
//
|
||||
// The specification mentions link layer response timeout this way:
|
||||
// The time structure of various link layer communication types is described in EN60870-5-1. The answer time
|
||||
// between the end of a master send telegram and the beginning of the response telegram of the slave shall be
|
||||
// between 11 bit times and (330 bit times + 50ms).
|
||||
//
|
||||
// For 2400Bd this means (330 + 11) / 2400 + 0.05 = 188.75 ms (added 11 bit periods to receive first byte).
|
||||
// I.e. timeout of 0.2s seems appropriate for 2400Bd.
|
||||
|
||||
handle->t.c_cc[VTIME] = 2; // Timeout in 1/10 sec
|
||||
|
||||
cfsetispeed(&(handle->t), B2400);
|
||||
cfsetospeed(&(handle->t), B2400);
|
||||
|
||||
#ifdef MBUS_SERIAL_DEBUG
|
||||
printf("%s: t.c_cflag = %x\n", __PRETTY_FUNCTION__, handle->t.c_cflag);
|
||||
printf("%s: t.c_oflag = %x\n", __PRETTY_FUNCTION__, handle->t.c_oflag);
|
||||
printf("%s: t.c_iflag = %x\n", __PRETTY_FUNCTION__, handle->t.c_iflag);
|
||||
printf("%s: t.c_lflag = %x\n", __PRETTY_FUNCTION__, handle->t.c_lflag);
|
||||
#endif
|
||||
|
||||
tcsetattr(handle->fd, TCSANOW, &(handle->t));
|
||||
|
||||
return handle;
|
||||
return 0;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Set baud rate for serial connection
|
||||
//------------------------------------------------------------------------------
|
||||
int
|
||||
mbus_serial_set_baudrate(mbus_serial_handle *handle, int baudrate)
|
||||
mbus_serial_set_baudrate(mbus_handle *handle, int baudrate)
|
||||
{
|
||||
speed_t speed;
|
||||
|
||||
if (handle == NULL)
|
||||
return -1;
|
||||
|
||||
mbus_serial_handle *serial_data = (mbus_serial_handle *) handle->auxdata;
|
||||
|
||||
switch (baudrate)
|
||||
{
|
||||
case 300:
|
||||
speed = B300;
|
||||
handle->t.c_cc[VTIME] = 12; // Timeout in 1/10 sec
|
||||
serial_data->t.c_cc[VTIME] = 12; // Timeout in 1/10 sec
|
||||
break;
|
||||
|
||||
case 1200:
|
||||
speed = B1200;
|
||||
handle->t.c_cc[VTIME] = 4; // Timeout in 1/10 sec
|
||||
serial_data->t.c_cc[VTIME] = 4; // Timeout in 1/10 sec
|
||||
break;
|
||||
|
||||
case 2400:
|
||||
speed = B2400;
|
||||
handle->t.c_cc[VTIME] = 2; // Timeout in 1/10 sec
|
||||
serial_data->t.c_cc[VTIME] = 2; // Timeout in 1/10 sec
|
||||
break;
|
||||
|
||||
case 9600:
|
||||
speed = B9600;
|
||||
handle->t.c_cc[VTIME] = 1; // Timeout in 1/10 sec
|
||||
serial_data->t.c_cc[VTIME] = 1; // Timeout in 1/10 sec
|
||||
break;
|
||||
|
||||
default:
|
||||
@ -130,19 +133,19 @@ mbus_serial_set_baudrate(mbus_serial_handle *handle, int baudrate)
|
||||
}
|
||||
|
||||
// Set input baud rate
|
||||
if (cfsetispeed(&(handle->t), speed) != 0)
|
||||
if (cfsetispeed(&(serial_data->t), speed) != 0)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Set output baud rate
|
||||
if (cfsetospeed(&(handle->t), speed) != 0)
|
||||
if (cfsetospeed(&(serial_data->t), speed) != 0)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Change baud rate immediately
|
||||
if (tcsetattr(handle->fd, TCSANOW, &(handle->t)) != 0)
|
||||
if (tcsetattr(handle->fd, TCSANOW, &(serial_data->t)) != 0)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
@ -155,7 +158,7 @@ mbus_serial_set_baudrate(mbus_serial_handle *handle, int baudrate)
|
||||
//
|
||||
//------------------------------------------------------------------------------
|
||||
int
|
||||
mbus_serial_disconnect(mbus_serial_handle *handle)
|
||||
mbus_serial_disconnect(mbus_handle *handle)
|
||||
{
|
||||
if (handle == NULL)
|
||||
{
|
||||
@ -164,8 +167,6 @@ mbus_serial_disconnect(mbus_serial_handle *handle)
|
||||
|
||||
close(handle->fd);
|
||||
|
||||
free(handle);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -173,7 +174,7 @@ mbus_serial_disconnect(mbus_serial_handle *handle)
|
||||
//
|
||||
//------------------------------------------------------------------------------
|
||||
int
|
||||
mbus_serial_send_frame(mbus_serial_handle *handle, mbus_frame *frame)
|
||||
mbus_serial_send_frame(mbus_handle *handle, mbus_frame *frame)
|
||||
{
|
||||
u_char buff[PACKET_BUFF_SIZE];
|
||||
int len, ret;
|
||||
@ -225,7 +226,7 @@ mbus_serial_send_frame(mbus_serial_handle *handle, mbus_frame *frame)
|
||||
//
|
||||
//------------------------------------------------------------------------------
|
||||
int
|
||||
mbus_serial_recv_frame(mbus_serial_handle *handle, mbus_frame *frame)
|
||||
mbus_serial_recv_frame(mbus_handle *handle, mbus_frame *frame)
|
||||
{
|
||||
char buff[PACKET_BUFF_SIZE];
|
||||
int len, remaining, nread, timeouts;
|
||||
|
21
mbus/mbus-serial.h
Normal file → Executable file
21
mbus/mbus-serial.h
Normal file → Executable file
@ -19,23 +19,20 @@
|
||||
#define MBUS_SERIAL_H
|
||||
|
||||
#include <termios.h>
|
||||
#include <mbus/mbus.h>
|
||||
|
||||
typedef struct _mbus_serial_handle {
|
||||
#include "mbus-protocol-aux.h"
|
||||
#include "mbus-protocol.h"
|
||||
|
||||
typedef struct _mbus_serial_handle
|
||||
{
|
||||
char *device;
|
||||
|
||||
int fd;
|
||||
struct termios t;
|
||||
|
||||
} mbus_serial_handle;
|
||||
|
||||
|
||||
mbus_serial_handle *mbus_serial_connect(char *device);
|
||||
int mbus_serial_disconnect(mbus_serial_handle *handle);
|
||||
int mbus_serial_send_frame(mbus_serial_handle *handle, mbus_frame *frame);
|
||||
int mbus_serial_recv_frame(mbus_serial_handle *handle, mbus_frame *frame);
|
||||
int mbus_serial_set_baudrate(mbus_serial_handle *handle, int baudrate);
|
||||
int mbus_serial_connect(mbus_handle *handle);
|
||||
int mbus_serial_disconnect(mbus_handle *handle);
|
||||
int mbus_serial_send_frame(mbus_handle *handle, mbus_frame *frame);
|
||||
int mbus_serial_recv_frame(mbus_handle *handle, mbus_frame *frame);
|
||||
int mbus_serial_set_baudrate(mbus_handle *handle, int baudrate);
|
||||
#endif /* MBUS_SERIAL_H */
|
||||
|
||||
|
||||
|
@ -23,48 +23,41 @@
|
||||
#include <strings.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <mbus/mbus.h>
|
||||
#include <mbus/mbus-tcp.h>
|
||||
#include "mbus-tcp.h"
|
||||
|
||||
#define PACKET_BUFF_SIZE 2048
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
/// Setup a TCP/IP handle.
|
||||
//------------------------------------------------------------------------------
|
||||
mbus_tcp_handle *
|
||||
mbus_tcp_connect(char *host, int port)
|
||||
int
|
||||
mbus_tcp_connect(mbus_handle *handle)
|
||||
{
|
||||
mbus_tcp_handle *handle;
|
||||
|
||||
char error_str[128], *host;
|
||||
struct hostent *host_addr;
|
||||
struct sockaddr_in s;
|
||||
struct timeval time_out;
|
||||
mbus_tcp_handle *tcp_data;
|
||||
int port;
|
||||
|
||||
if (host == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
if (handle == NULL)
|
||||
return 0;
|
||||
|
||||
if ((handle = (mbus_tcp_handle *)malloc(sizeof(mbus_tcp_handle))) == NULL)
|
||||
{
|
||||
char error_str[128];
|
||||
snprintf(error_str, sizeof(error_str), "%s: failed to allocate memory for handle\n", __PRETTY_FUNCTION__);
|
||||
mbus_error_str_set(error_str);
|
||||
return NULL;
|
||||
}
|
||||
tcp_data = (mbus_tcp_handle *) handle->auxdata;
|
||||
if (tcp_data == NULL || tcp_data->host == NULL)
|
||||
return 0;
|
||||
|
||||
handle->host = host; // strdup ?
|
||||
handle->port = port;
|
||||
host = tcp_data->host;
|
||||
port = tcp_data->port;
|
||||
|
||||
//
|
||||
// create the TCP connection
|
||||
//
|
||||
if ((handle->sock = socket(AF_INET,SOCK_STREAM, 0)) < 0)
|
||||
if ((handle->fd = socket(AF_INET,SOCK_STREAM, 0)) < 0)
|
||||
{
|
||||
char error_str[128];
|
||||
snprintf(error_str, sizeof(error_str), "%s: failed to setup a socket.", __PRETTY_FUNCTION__);
|
||||
mbus_error_str_set(error_str);
|
||||
return NULL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
s.sin_family = AF_INET;
|
||||
@ -73,47 +66,41 @@ mbus_tcp_connect(char *host, int port)
|
||||
/* resolve hostname */
|
||||
if ((host_addr = gethostbyname(host)) == NULL)
|
||||
{
|
||||
char error_str[128];
|
||||
snprintf(error_str, sizeof(error_str), "%s: unknown host: %s", __PRETTY_FUNCTION__, host);
|
||||
mbus_error_str_set(error_str);
|
||||
free(handle);
|
||||
return NULL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
memcpy((void *)(&s.sin_addr), (void *)(host_addr->h_addr), host_addr->h_length);
|
||||
|
||||
if (connect(handle->sock, (struct sockaddr *)&s, sizeof(s)) < 0)
|
||||
if (connect(handle->fd, (struct sockaddr *)&s, sizeof(s)) < 0)
|
||||
{
|
||||
char error_str[128];
|
||||
snprintf(error_str, sizeof(error_str), "%s: Failed to establish connection to %s:%d", __PRETTY_FUNCTION__, host, port);
|
||||
mbus_error_str_set(error_str);
|
||||
free(handle);
|
||||
return NULL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Set a timeout
|
||||
time_out.tv_sec = 4; //seconds
|
||||
time_out.tv_usec = 0;
|
||||
setsockopt(handle->sock, SOL_SOCKET, SO_SNDTIMEO, &time_out, sizeof(time_out));
|
||||
setsockopt(handle->sock, SOL_SOCKET, SO_RCVTIMEO, &time_out, sizeof(time_out));
|
||||
time_out.tv_sec = 0; //seconds
|
||||
time_out.tv_usec = 100000;
|
||||
setsockopt(handle->fd, SOL_SOCKET, SO_SNDTIMEO, &time_out, sizeof(time_out));
|
||||
setsockopt(handle->fd, SOL_SOCKET, SO_RCVTIMEO, &time_out, sizeof(time_out));
|
||||
|
||||
return handle;
|
||||
return 1;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
//
|
||||
//------------------------------------------------------------------------------
|
||||
int
|
||||
mbus_tcp_disconnect(mbus_tcp_handle *handle)
|
||||
mbus_tcp_disconnect(mbus_handle *handle)
|
||||
{
|
||||
if (handle == NULL)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
close(handle->sock);
|
||||
|
||||
free(handle);
|
||||
close(handle->fd);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@ -122,7 +109,7 @@ mbus_tcp_disconnect(mbus_tcp_handle *handle)
|
||||
//
|
||||
//------------------------------------------------------------------------------
|
||||
int
|
||||
mbus_tcp_send_frame(mbus_tcp_handle *handle, mbus_frame *frame)
|
||||
mbus_tcp_send_frame(mbus_handle *handle, mbus_frame *frame)
|
||||
{
|
||||
u_char buff[PACKET_BUFF_SIZE];
|
||||
int len, ret;
|
||||
@ -140,7 +127,7 @@ 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->fd, buff, len)) == len)
|
||||
{
|
||||
//
|
||||
// call the send event function, if the callback function is registered
|
||||
@ -162,7 +149,7 @@ mbus_tcp_send_frame(mbus_tcp_handle *handle, mbus_frame *frame)
|
||||
//------------------------------------------------------------------------------
|
||||
//
|
||||
//------------------------------------------------------------------------------
|
||||
int mbus_tcp_recv_frame(mbus_tcp_handle *handle, mbus_frame *frame)
|
||||
int mbus_tcp_recv_frame(mbus_handle *handle, mbus_frame *frame)
|
||||
{
|
||||
char buff[PACKET_BUFF_SIZE];
|
||||
int len, remaining, nread;
|
||||
@ -182,7 +169,7 @@ int mbus_tcp_recv_frame(mbus_tcp_handle *handle, mbus_frame *frame)
|
||||
|
||||
do {
|
||||
retry:
|
||||
nread = read(handle->sock, &buff[len], remaining);
|
||||
nread = read(handle->fd, &buff[len], remaining);
|
||||
switch (nread) {
|
||||
case -1:
|
||||
if (errno == EINTR)
|
||||
|
19
mbus/mbus-tcp.h
Normal file → Executable file
19
mbus/mbus-tcp.h
Normal file → Executable file
@ -18,22 +18,19 @@
|
||||
#ifndef MBUS_TCP_H
|
||||
#define MBUS_TCP_H
|
||||
|
||||
#include <mbus/mbus.h>
|
||||
|
||||
typedef struct _mbus_tcp_handle {
|
||||
#include "mbus-protocol.h"
|
||||
#include "mbus-protocol-aux.h"
|
||||
|
||||
typedef struct _mbus_tcp_handle
|
||||
{
|
||||
char *host;
|
||||
|
||||
int port;
|
||||
int sock;
|
||||
|
||||
} mbus_tcp_handle;
|
||||
|
||||
|
||||
mbus_tcp_handle *mbus_tcp_connect(char *host, int port);
|
||||
int mbus_tcp_disconnect(mbus_tcp_handle *handle);
|
||||
int mbus_tcp_send_frame(mbus_tcp_handle *handle, mbus_frame *frame);
|
||||
int mbus_tcp_recv_frame(mbus_tcp_handle *handle, mbus_frame *frame);
|
||||
int mbus_tcp_connect(mbus_handle *handle);
|
||||
int mbus_tcp_disconnect(mbus_handle *handle);
|
||||
int mbus_tcp_send_frame(mbus_handle *handle, mbus_frame *frame);
|
||||
int mbus_tcp_recv_frame(mbus_handle *handle, mbus_frame *frame);
|
||||
|
||||
#endif /* MBUS_TCP_H */
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user