forked from machinekoder/mbus-windows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmbus-tcp.c
195 lines (156 loc) · 5.32 KB
/
mbus-tcp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
//------------------------------------------------------------------------------
// Copyright (C) 2011, Robert Johansson, Raditex AB
// All rights reserved.
//
// FreeSCADA
// http://www.FreeSCADA.com
//
//------------------------------------------------------------------------------
#include <unistd.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <strings.h>
#include <mbus/mbus.h>
#include <mbus/mbus-tcp.h>
#define PACKET_BUFF_SIZE 2048
//------------------------------------------------------------------------------
/// Setup a TCP/IP handle.
//------------------------------------------------------------------------------
mbus_tcp_handle *
mbus_tcp_connect(char *host, int port)
{
mbus_tcp_handle *handle;
struct hostent *host_addr;
struct sockaddr_in s;
struct timeval time_out;
if (host == NULL)
{
return NULL;
}
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;
}
handle->host = host; // strdup ?
handle->port = port;
//
// create the TCP connection
//
if ((handle->sock = 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;
}
s.sin_family = AF_INET;
s.sin_port = htons(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;
}
bcopy((void *)(host_addr->h_addr), (void *)(&s.sin_addr), host_addr->h_length);
if (connect(handle->sock, (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;
}
// Set a timeout
time_out.tv_sec = 2; //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));
return handle;
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
int
mbus_tcp_disconnect(mbus_tcp_handle *handle)
{
if (handle == NULL)
{
return -1;
}
close(handle->sock);
free(handle);
return 0;
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
int
mbus_tcp_send_frame(mbus_tcp_handle *handle, mbus_frame *frame)
{
u_char buff[PACKET_BUFF_SIZE];
int len, ret;
if (handle == NULL || frame == NULL)
{
return -1;
}
if ((len = mbus_frame_pack(frame, buff, sizeof(buff))) == -1)
{
char error_str[128];
snprintf(error_str, sizeof(error_str), "%s: mbus_frame_pack failed\n", __PRETTY_FUNCTION__);
mbus_error_str_set(error_str);
return -1;
}
if ((ret = write(handle->sock, buff, len)) != len)
{
char error_str[128];
snprintf(error_str, sizeof(error_str), "%s: Failed to write frame to socket (ret = %d)\n", __PRETTY_FUNCTION__, ret);
mbus_error_str_set(error_str);
return -1;
}
return 0;
}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
int
mbus_tcp_recv_frame(mbus_tcp_handle *handle, mbus_frame *frame)
{
char buff[PACKET_BUFF_SIZE];
int len, remaining, nread;
bzero((void *)buff, sizeof(buff));
//
// read data until a packet is received
//
remaining = 1; // start by reading 1 byte
len = 0;
do {
//printf("%s: Attempt to read %d bytes [len = %d]", __PRETTY_FUNCTION__, remaining, len);
if ((nread = read(handle->sock, &buff[len], remaining)) == -1)
{
mbus_error_str_set("M-Bus tcp transport layer failed to read data.");
//fprintf(stderr, "%s: aborting recv frame (remaining = %d, len = %d, nread = %d)",
// __PRETTY_FUNCTION__, remaining, len, nread);
return -1;
}
//printf("%s: Got %d byte [remaining %d, len %d]", __PRETTY_FUNCTION__, nread, remaining, len);
len += nread;
} while ((remaining = mbus_parse(frame, buff, len)) > 0);
if (remaining < 0)
{
mbus_error_str_set("M-Bus layer failed to parse data.");
return -2;
}
return 0;
}