18 #include <arpa/inet.h>
21 #include <netinet/in.h>
25 #include <sys/socket.h>
26 #include <sys/types.h>
32 #define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
35 #define __PRINTF__ printf
36 #define __COUT__ cout << __FILENAME__ << std::dec << " [" << __LINE__ << "]\t"
44 #define MAXBUFLEN 1492
45 #define EMULATOR_PORT "4950" // Can be also passed as first argument
48 void* get_in_addr(
struct sockaddr* sa)
50 if(sa->sa_family == AF_INET)
52 return &(((
struct sockaddr_in*)sa)->sin_addr);
55 return &(((
struct sockaddr_in6*)sa)->sin6_addr);
58 int makeSocket(
const char* ip,
int port,
struct addrinfo*& p)
61 struct addrinfo hints, *servinfo;
64 struct sockaddr_storage their_addr;
66 char s[INET6_ADDRSTRLEN];
68 memset(&hints, 0,
sizeof hints);
69 hints.ai_family = AF_UNSPEC;
70 hints.ai_socktype = SOCK_DGRAM;
72 sprintf(portStr,
"%d", port);
73 if((rv = getaddrinfo(ip, portStr, &hints, &servinfo)) != 0)
75 __PRINTF__(
"getaddrinfo: %s\n", gai_strerror(rv));
80 for(p = servinfo; p != NULL; p = p->ai_next)
82 if((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1)
84 __PRINTF__(
"sw: socket\n");
93 __PRINTF__(
"sw: failed to create socket\n");
97 freeaddrinfo(servinfo);
102 int main(
int argc,
char** argv)
104 std::string emulatorPort(EMULATOR_PORT);
106 emulatorPort = argv[1];
108 __COUT__ << std::hex <<
":::"
109 <<
"\n\nUsing emulatorPort=" << emulatorPort <<
"\n"
112 std::string streamToIP;
113 unsigned short streamToPort;
117 struct addrinfo hints, *servinfo, *p;
120 struct sockaddr_storage their_addr;
121 char buff[MAXBUFLEN];
123 char s[INET6_ADDRSTRLEN];
125 memset(&hints, 0,
sizeof hints);
126 hints.ai_family = AF_UNSPEC;
127 hints.ai_socktype = SOCK_DGRAM;
128 hints.ai_flags = AI_PASSIVE;
130 if((rv = getaddrinfo(NULL, emulatorPort.c_str(), &hints, &servinfo)) != 0)
132 fprintf(stderr,
"getaddrinfo: %s\n", gai_strerror(rv));
137 for(p = servinfo; p != NULL; p = p->ai_next)
139 if((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1)
141 perror(
"listener: socket");
145 if(bind(sockfd, p->ai_addr, p->ai_addrlen) == -1)
148 perror(
"listener: bind");
157 fprintf(stderr,
"listener: failed to bind socket\n");
159 __COUT__ <<
"\n\nYou can try a different port by passing an argument.\n\n";
164 freeaddrinfo(servinfo);
171 std::stringstream addressSpaceSS;
172 addressSpaceSS <<
"Address space:\n";
173 addressSpaceSS <<
"\t 0x0000000000001001 \t W/R \t 'Data count'\n";
174 addressSpaceSS <<
"\t 0x0000000000001002 \t W/R \t 'Data rate'\n";
175 addressSpaceSS <<
"\t 0x0000000000001003 \t W/R \t 'LEDs'\n";
176 addressSpaceSS <<
"\t 0x0000000100000006 \t W \t 'Stream destination IP'\n";
179 addressSpaceSS <<
"\t 0x0000000100000008 \t W \t 'Stream destination port'\n";
180 addressSpaceSS <<
"\t 0x0000000100000009 \t W/R \t 'Burst data enable'\n";
182 __COUT__ << addressSpaceSS.str() <<
"\n\n";
185 uint64_t data_gen_cnt = 0;
186 uint64_t data_gen_rate = 100;
187 uint8_t led_register = 0;
188 uint8_t dataEnabled = 0;
190 const unsigned int RX_ADDR_OFFSET = 2;
191 const unsigned int RX_DATA_OFFSET = 10;
192 const unsigned int TX_DATA_OFFSET = 2;
194 bool wasDataEnable =
false;
195 unsigned char sequence = 0;
196 unsigned int packetSz;
200 fd_set readfds, masterfds;
204 FD_SET(sockfd, &masterfds);
209 int totalNumberOfBytes;
217 select(sockfd + 1, &readfds, NULL, NULL, &tv);
219 if(FD_ISSET(sockfd, &readfds))
222 __COUT__ << std::hex <<
":::"
223 <<
"Packet Received!" << std::endl;
225 addr_len =
sizeof their_addr;
226 if((totalNumberOfBytes = recvfrom(sockfd,
230 (
struct sockaddr*)&their_addr,
238 <<
"hw: got packet from "
239 << inet_ntop(their_addr.ss_family,
240 get_in_addr((
struct sockaddr*)&their_addr),
245 <<
"hw: packet total is " << totalNumberOfBytes <<
" bytes long"
261 while(handlerIndex < totalNumberOfBytes &&
262 (numberOfBytes = (buff[handlerIndex + 0]
265 handlerIndex + numberOfBytes <= totalNumberOfBytes)
268 <<
"hw: packet byte index " << handlerIndex << std::endl;
270 <<
"hw: packet is " << numberOfBytes <<
" bytes long"
273 for(
int i = 0; i < numberOfBytes; i++)
275 if((i - RX_ADDR_OFFSET) % 8 == 0)
277 __PRINTF__(
"%2.2X", (
unsigned char)buff[handlerIndex + i]);
284 if(numberOfBytes == 10 &&
285 buff[handlerIndex + 0] == 0)
288 memcpy((
void*)&addr, (
void*)&buff[handlerIndex + RX_ADDR_OFFSET], 8);
290 __COUT__ << std::hex <<
":::"
291 <<
"Read address: 0x" << hex << addr;
292 __PRINTF__(
" 0x%16.16lX \n", addr);
295 buff[handlerIndex + 0] = 0;
296 buff[handlerIndex + 1] =
302 memcpy((
void*)&buff[handlerIndex + TX_DATA_OFFSET],
303 (
void*)&data_gen_cnt,
305 __COUT__ << std::hex <<
":::"
306 <<
"Read data count: 0x" << data_gen_cnt << endl;
309 memcpy((
void*)&buff[handlerIndex + TX_DATA_OFFSET],
310 (
void*)&data_gen_rate,
312 __COUT__ << std::hex <<
":::"
313 <<
"Read data rate: 0x" << data_gen_rate << endl;
316 memset((
void*)&buff[handlerIndex + TX_DATA_OFFSET + 1], 0, 7);
317 memcpy((
void*)&buff[handlerIndex + TX_DATA_OFFSET],
318 (
void*)&led_register,
320 __COUT__ << std::hex <<
":::"
321 <<
"Read LED register: 0x" << (
unsigned int)led_register
324 case 0x0000000100000009:
325 memset((
void*)&buff[handlerIndex + TX_DATA_OFFSET + 1], 0, 7);
326 memcpy((
void*)&buff[handlerIndex + TX_DATA_OFFSET],
329 __COUT__ << std::hex <<
":::"
330 <<
"Read data enable: 0x" << dataEnabled << endl;
333 __COUT__ << std::hex <<
":::"
334 <<
"Unknown read address received." << endl;
337 packetSz = TX_DATA_OFFSET + 8;
338 if((numberOfBytes = sendto(sockfd,
342 (
struct sockaddr*)&their_addr,
343 sizeof(
struct sockaddr_storage))) == -1)
345 perror(
"hw: sendto");
348 __PRINTF__(
"hw: sent %d bytes back. sequence=%d\n",
350 (
unsigned char)buff[handlerIndex + 1]);
352 else if(numberOfBytes >= 18 &&
353 (numberOfBytes - 2) % 8 ==
355 buff[handlerIndex + 0] == 1)
358 memcpy((
void*)&addr, (
void*)&buff[handlerIndex + RX_ADDR_OFFSET], 8);
359 __COUT__ << std::hex <<
":::"
360 <<
"hw: Line " << std::dec << __LINE__ <<
":::"
361 <<
"Write address: 0x" << std::hex << addr;
362 __PRINTF__(
" 0x%16.16lX \n", addr);
367 memcpy((
void*)&data_gen_cnt,
368 (
void*)&buff[handlerIndex + RX_DATA_OFFSET],
370 __COUT__ << std::hex <<
":::"
371 <<
"Write data count: 0x" << data_gen_cnt << endl;
375 memcpy((
void*)&data_gen_rate,
376 (
void*)&buff[handlerIndex + RX_DATA_OFFSET],
378 __COUT__ << std::hex <<
":::"
379 <<
"Write data rate: 0x" << data_gen_rate << endl;
382 memcpy((
void*)&led_register,
383 (
void*)&buff[handlerIndex + RX_DATA_OFFSET],
385 __COUT__ << std::hex <<
":::"
386 <<
"Write LED register: 0x" << (
unsigned int)led_register
390 for(
int l = 0; l < 8; ++l)
391 __COUT__ << ((led_register & (1 << (7 - l))) ?
'*' :
'-');
394 case 0x0000000100000006:
397 struct sockaddr_in socketAddress;
399 (
void*)&ip, (
void*)&buff[handlerIndex + RX_DATA_OFFSET], 4);
401 memcpy((
void*)&socketAddress.sin_addr, (
void*)&ip, 4);
402 streamToIP = inet_ntoa(socketAddress.sin_addr);
403 __COUT__ << std::hex <<
":::"
404 <<
"Stream destination IP: " << streamToIP << std::endl;
405 __COUT__ << streamToIP << std::endl;
408 case 0x0000000100000007:
409 __COUT__ << std::hex <<
":::"
410 <<
"Destination MAC address ignored!" << std::endl;
412 case 0x0000000100000008:
415 memcpy((
void*)&streamToPort,
416 (
void*)&buff[handlerIndex + RX_DATA_OFFSET],
418 __COUT__ << std::hex <<
":::"
419 <<
"Stream destination port: 0x" << streamToPort << dec
420 <<
" " << streamToPort << endl;
424 sendSockfd = makeSocket(streamToIP.c_str(), streamToPort, p);
427 __COUT__ <<
"************************************************"
430 __COUT__ <<
"************************************************"
433 __COUT__ << std::hex <<
":::"
434 <<
"Streaming to ip: " << streamToIP <<
" port: 0x"
435 << streamToPort << endl;
436 __COUT__ <<
"************************************************"
439 __COUT__ <<
"************************************************"
444 __COUT__ << std::hex <<
":::"
445 <<
"Failed to create streaming socket to ip: "
446 << streamToIP <<
" port: 0x" << streamToPort << endl;
450 case 0x0000000100000009:
451 memcpy((
void*)&dataEnabled,
452 (
void*)&buff[handlerIndex + RX_DATA_OFFSET],
454 __COUT__ << std::hex <<
":::"
455 <<
"Write data enable: 0x" << (int)dataEnabled << endl;
459 __COUT__ << std::hex <<
":::"
460 <<
"Unknown write address received." << endl;
464 __COUT__ << std::hex <<
":::"
465 <<
"ERROR: The formatting of the string received is wrong! "
467 << numberOfBytes <<
" Read/Write " << buff[handlerIndex + 0]
470 handlerIndex += numberOfBytes;
473 __COUT__ << std::hex <<
":::"
475 << addressSpaceSS.str() <<
"\n\n";
489 if(count % data_gen_rate == 0 &&
493 __COUT__ << std::hex <<
":::"
494 <<
"Count: " << count <<
" rate: " << data_gen_rate
495 <<
" packet-counter: " << data_gen_cnt << std::endl;
496 __COUT__ << std::hex <<
":::"
497 <<
"Send Burst at count:" << count << std::endl;
500 wasDataEnable ? 2 : 1;
501 buff[1] = sequence++;
502 memcpy((
void*)&buff[TX_DATA_OFFSET],
508 packetSz = TX_DATA_OFFSET + 8;
517 if((numberOfBytes = sendto(
518 sendSockfd, buff, packetSz, 0, p->ai_addr, p->ai_addrlen)) ==
521 perror(
"Hw: sendto");
524 __PRINTF__(
"hw: sent %d bytes back. sequence=%d\n",
526 (
unsigned char)buff[1]);
528 if(data_gen_cnt != (uint64_t)-1)
532 wasDataEnable =
true;
534 else if(wasDataEnable)
536 wasDataEnable =
false;
537 __COUT__ << std::hex <<
":::"
538 <<
"Send Last in Burst at count:" << count << endl;
541 buff[1] = sequence++;
542 memcpy((
void*)&buff[TX_DATA_OFFSET],
546 packetSz = TX_DATA_OFFSET + 8;
550 if((numberOfBytes = sendto(
551 sendSockfd, buff, packetSz, 0, p->ai_addr, p->ai_addrlen)) ==
554 perror(
"hw: sendto");
557 __PRINTF__(
"hw: sent %d bytes back. sequence=%d\n",
559 (
unsigned char)buff[1]);
562 __COUT__ << std::hex <<
":::"
563 <<
"Send socket not defined." << endl;