-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrbcp.cxx
332 lines (270 loc) · 6.66 KB
/
rbcp.cxx
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/*
*
*
*/
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/time.h>
struct bcp_header{
unsigned char type;
unsigned char command;
unsigned char id;
unsigned char length;
unsigned int address;
};
struct rbcp_header {
unsigned char type;
unsigned char command;
unsigned char id;
unsigned char length;
unsigned int address;
};
#define RBCP_VER 0xFF
#define RBCP_CMD_WR 0x80
#define RBCP_CMD_RD 0xC0
const int gMaxContainerSize = 1472;
const int gBufSize = 2048;
class RBCP
{
public:
RBCP() {};
RBCP(char *, int);
virtual ~RBCP() {};
int Open(char *, int);
int Close();
int Send(char *, int);
int Receive(char *);
int Read(char *, unsigned int, int);
int Write(char *, unsigned int, int);
int GetSock() {return fSock;};
protected:
private:
struct sockaddr_in fDestSockAddr;
int fSequence = 0;
int fSock = 0;
};
RBCP::RBCP(char *hostname, int port)
{
Open(hostname, port);
}
int RBCP::Open(char *hostname, int port)
{
struct addrinfo hints, *ai;
int status;
struct timeval timeout={3, 0};
memset((char *)&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
status = getaddrinfo(hostname, NULL, &hints, &ai);
if (status != 0) {
fprintf(stderr, "Unknown host %s (%s)\n",
hostname, gai_strerror(status));
perror("RBCP::Open");
return -1;
}
memset((char *)&fDestSockAddr, 0, sizeof(fDestSockAddr));
memcpy(&fDestSockAddr, ai->ai_addr, ai->ai_addrlen);
//fDestSockAddr.sin_addr.s_addr = ai.ai_addr.sin_addr.s_addr;
//fDestSockAddr.sin_addr.s_addr = inet_addr(dest);
fDestSockAddr.sin_port = htons(port);
fDestSockAddr.sin_family = AF_INET;
freeaddrinfo(ai);
fSock = socket(AF_INET, SOCK_DGRAM, 0);
if (fSock < 0) {
perror("RBCP::Open socket");
return -1;
}
setsockopt(fSock, SOL_SOCKET, SO_RCVTIMEO,
(char *)&timeout, sizeof(struct timeval));
status = connect(fSock,
(struct sockaddr *)&fDestSockAddr, sizeof(fDestSockAddr));
if (status != 0) {
perror("RBCP::Open connect");
close(fSock);
return -1;
}
return fSock;
}
int RBCP::Close()
{
return close(fSock);
}
int RBCP::Send(char *buf, int len)
{
int status;
status = sendto(fSock, buf, len, 0,
(struct sockaddr *)&fDestSockAddr, sizeof(fDestSockAddr));
if (status != len) {
perror("RBCP::Send sendto");
return -1;
}
return 0;
}
int RBCP::Receive(char *buf)
{
int status;
//socklen_t fromlen;
//status = *len = recvfrom(sock, buf, 1024, 0, NULL, &fromlen);
status = recvfrom(fSock, buf, gBufSize, 0, NULL, NULL);
if (status < 0) {
perror("RBCP::Receive recvfrom (Timeout ?)");
return -1;
}
return status;
}
#if 0
int updreg_wait_ans(int sock)
{
return 0;
}
#endif
int RBCP::Read(char *buf, unsigned int addr, int len)
{
struct bcp_header bcp;
struct rbcp_header *rbcp;
char *data;
int status;
int rlen = 0;
bcp.type = RBCP_VER;
bcp.command = RBCP_CMD_RD;
bcp.address = htonl(addr);
bcp.length = len;
bcp.id = fSequence++;
status = Send((char *)&bcp, sizeof(bcp));
if (status < 0) printf("rbcp_read udp_send: error\n");
rlen = Receive(buf);
rbcp = (struct rbcp_header *)buf;
data = buf + sizeof(struct rbcp_header);
#ifdef DEBUG
if (rlen > 0) {
int i;
printf("type: 0x%x, command: 0x%x, id: 0x%x, length: %d, address: %d",
rbcp->type & 0xff, rbcp->command& 0xff,
rbcp->id & 0xff, rbcp->length & 0xff,
ntohl(rbcp->address & 0xffffffff));
for (i = 0 ; i < (int)(rlen - sizeof(struct rbcp_header)) ; i++) {
if ((i % 8) == 0) printf("\n%04x: ", addr + i);
printf("%02x ", data[i] & 0xff);
}
printf("\n");
}
#endif
if (rlen > (int)(sizeof(struct rbcp_header))) {
memmove(buf, data, rlen - sizeof(struct rbcp_header));
} else {
fprintf(stderr, "RBCP::Read: RBCP Read err.\n");
}
return rlen;
}
int RBCP::Write(char *data, unsigned int addr, int len)
{
struct bcp_header *bcp;
struct rbcp_header *rbcp;
char *bcp_body;
static char sbuf[gBufSize];
static char rbuf[gBufSize];
int status;
int rlen = 0;
bcp = (struct bcp_header *)sbuf;
bcp_body = sbuf + sizeof(struct bcp_header);
bcp->type = RBCP_VER;
bcp->command = RBCP_CMD_WR;
bcp->address = htonl(addr);
//bcp->length = len + sizeof(struct bcp_header);
bcp->length = len;
bcp->id = fSequence++;
if (len < gMaxContainerSize - static_cast<int>(sizeof(struct bcp_header))) {
memcpy(bcp_body, data, len);
} else {
fprintf(stderr, "RBCP::Write: Buffer overflow %d\n", len);
return -1;
}
status = Send((char *)&sbuf, sizeof(struct bcp_header) + len);
if (status < 0) printf("RBCP::Write udp_send: error\n");
rlen = Receive(rbuf);
rbcp = (struct rbcp_header *)rbuf;
data = rbuf + sizeof(struct rbcp_header);
#ifdef DEBUG
if (rlen > 0) {
int i;
printf("type: 0x%x, command: 0x%x, id: 0x%x, length: %d, address: %d",
rbcp->type & 0xff, rbcp->command& 0xff,
rbcp->id & 0xff, rbcp->length & 0xff,
ntohl(rbcp->address & 0xffffffff));
for (i = 0 ; i < (int)(rlen - sizeof(struct rbcp_header)) ; i++) {
if ((i % 8) == 0) printf("\n%04x: ", addr + i);
printf("%02x ", data[i] & 0xff);
}
printf("\n");
}
#endif
return rlen;
}
#ifdef TEST_MAIN
void helpline(char *cname)
{
printf("%s <Commads>\n", cname);
printf("Commands \n");
printf("--host=<hotname>\n");
printf("--port=<port nmber>\n");
printf("--read=<address>:<number of reading>\n");
printf("--write=<addres>:<data>\n");
return;
}
int main(int argc, char* argv[])
{
const char *default_host = "192.168.10.16";
int default_port = 4660;
int seq1 = 0;
static char buf[1024];
char hostname[256];
int port;
unsigned int raddress, rlen;
unsigned int waddress;
int wdata;
int status;
int i;
strcpy(hostname, default_host);
port = default_port;
wdata = -1;
rlen = 0;
for (i = 1 ; i < argc ; i++) {
if (sscanf(argv[i], "--host=%s", hostname) == 1) ;
if (sscanf(argv[i], "--port=%d", &port) == 1) ;
if (sscanf(argv[i], "--read=%x:%x", &raddress, &rlen) == 1);
if (sscanf(argv[i], "--write=%x:%x", &waddress, &wdata) == 1);
if (strncmp(argv[i], "--seq1", 6) == 0) seq1 = 1;
}
printf("host: %s\n", hostname);
RBCP rbcp;
rbcp.Open(hostname, port);
if (rlen > 0) {
printf("read: 0x%x : %d\n", raddress, rlen);
rbcp.Read(buf, raddress, rlen);
}
if (wdata >= 0) {
printf("write: 0x%x : %x\n", waddress, wdata);
buf[0] = wdata & 0xff;
buf[1] = 0;
status = rbcp.Write(buf, waddress, 1);
if (status <= 0) printf("Write Error %d\n", status);
}
if (seq1 == 1) {
int j;
for (j = 0 ; j < 128 ; j++) {
for (i = 0 ; i < 128 ; i++) buf[i] = i;
buf[0] = j;
rbcp.Write(buf, 0 + 128*j, 128);
}
}
rbcp.Close();
return 0;
}
#endif