18 #include <arpa/inet.h>
21 #include <netinet/in.h>
25 #include <sys/socket.h>
26 #include <sys/types.h>
33 #define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
36 #define __PRINTF__ printf
37 #define __COUT__ std::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;
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 uint32_t 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"
259 while(handlerIndex < totalNumberOfBytes &&
260 (numberOfBytes = (buff[handlerIndex + 0]
263 handlerIndex + numberOfBytes <= totalNumberOfBytes)
266 <<
"hw: packet byte index " << handlerIndex << std::endl;
268 <<
"hw: packet is " << numberOfBytes <<
" bytes long"
271 for(
int i = 0; i < numberOfBytes; i++)
273 if((i - RX_ADDR_OFFSET) % 8 == 0)
275 __PRINTF__(
"%2.2X", (
unsigned char)buff[handlerIndex + i]);
282 if(numberOfBytes == 10 &&
283 buff[handlerIndex + 0] == 0)
286 memcpy((
void*)&addr, (
void*)&buff[handlerIndex + RX_ADDR_OFFSET], 8);
288 __COUT__ << std::hex <<
":::"
289 <<
"Read address: 0x" << std::hex << addr;
290 __PRINTF__(
" 0x%16.16lX \n", addr);
293 buff[handlerIndex + 0] = 0;
294 buff[handlerIndex + 1] =
300 memcpy((
void*)&buff[handlerIndex + TX_DATA_OFFSET],
301 (
void*)&data_gen_cnt,
303 __COUT__ << std::hex <<
":::"
304 <<
"Read data count: 0x" << data_gen_cnt << std::endl;
307 memcpy((
void*)&buff[handlerIndex + TX_DATA_OFFSET],
308 (
void*)&data_gen_rate,
310 __COUT__ << std::hex <<
":::"
311 <<
"Read data rate: 0x" << data_gen_rate << std::endl;
314 memset((
void*)&buff[handlerIndex + TX_DATA_OFFSET + 1], 0, 7);
315 memcpy((
void*)&buff[handlerIndex + TX_DATA_OFFSET],
316 (
void*)&led_register,
318 __COUT__ << std::hex <<
":::"
319 <<
"Read LED register: 0x" << (
unsigned int)led_register
322 case 0x0000000100000009:
323 memset((
void*)&buff[handlerIndex + TX_DATA_OFFSET + 1], 0, 7);
324 memcpy((
void*)&buff[handlerIndex + TX_DATA_OFFSET],
327 __COUT__ << std::hex <<
":::"
328 <<
"Read data enable: 0x" << dataEnabled << std::endl;
331 __COUT__ << std::hex <<
":::"
332 <<
"Unknown read address received." << std::endl;
335 packetSz = TX_DATA_OFFSET + 8;
336 if((numberOfBytes = sendto(sockfd,
340 (
struct sockaddr*)&their_addr,
341 sizeof(
struct sockaddr_storage))) == -1)
343 perror(
"hw: sendto");
346 __PRINTF__(
"hw: sent %d bytes back. sequence=%d\n",
348 (
unsigned char)buff[handlerIndex + 1]);
350 else if(numberOfBytes >= 18 &&
351 (numberOfBytes - 2) % 8 ==
353 buff[handlerIndex + 0] == 1)
356 memcpy((
void*)&addr, (
void*)&buff[handlerIndex + RX_ADDR_OFFSET], 8);
357 __COUT__ << std::hex <<
":::"
358 <<
"hw: Line " << std::dec << __LINE__ <<
":::"
359 <<
"Write address: 0x" << std::hex << addr;
360 __PRINTF__(
" 0x%16.16lX \n", addr);
365 memcpy((
void*)&data_gen_cnt,
366 (
void*)&buff[handlerIndex + RX_DATA_OFFSET],
368 __COUT__ << std::hex <<
":::"
369 <<
"Write data count: 0x" << data_gen_cnt << std::endl;
373 memcpy((
void*)&data_gen_rate,
374 (
void*)&buff[handlerIndex + RX_DATA_OFFSET],
376 __COUT__ << std::hex <<
":::"
377 <<
"Write data rate: 0x" << data_gen_rate << std::endl;
380 memcpy((
void*)&led_register,
381 (
void*)&buff[handlerIndex + RX_DATA_OFFSET],
383 __COUT__ << std::hex <<
":::"
384 <<
"Write LED register: 0x" << (
unsigned int)led_register
388 for(
int l = 0; l < 8; ++l)
389 __COUT__ << ((led_register & (1 << (7 - l))) ?
'*' :
'-');
392 case 0x0000000100000006:
395 struct sockaddr_in socketAddress;
397 (
void*)&ip, (
void*)&buff[handlerIndex + RX_DATA_OFFSET], 4);
399 memcpy((
void*)&socketAddress.sin_addr, (
void*)&ip, 4);
400 streamToIP = inet_ntoa(socketAddress.sin_addr);
401 __COUT__ << std::hex <<
":::"
402 <<
"Stream destination IP: " << streamToIP << std::endl;
403 __COUT__ << streamToIP << std::endl;
406 case 0x0000000100000007:
407 __COUT__ << std::hex <<
":::"
408 <<
"Destination MAC address ignored!" << std::endl;
410 case 0x0000000100000008:
413 memcpy((
void*)&streamToPort,
414 (
void*)&buff[handlerIndex + RX_DATA_OFFSET],
416 __COUT__ << std::hex <<
":::"
417 <<
"Stream destination port: 0x" << streamToPort << std::dec
418 <<
" " << streamToPort << std::endl;
422 sendSockfd = makeSocket(streamToIP.c_str(), streamToPort, p);
425 __COUT__ <<
"************************************************"
428 __COUT__ <<
"************************************************"
431 __COUT__ << std::hex <<
":::"
432 <<
"Streaming to ip: " << streamToIP <<
" port: 0x"
433 << streamToPort << std::endl;
434 __COUT__ <<
"************************************************"
437 __COUT__ <<
"************************************************"
442 __COUT__ << std::hex <<
":::"
443 <<
"Failed to create streaming socket to ip: "
444 << streamToIP <<
" port: 0x" << streamToPort << std::endl;
448 case 0x0000000100000009:
449 memcpy((
void*)&dataEnabled,
450 (
void*)&buff[handlerIndex + RX_DATA_OFFSET],
452 __COUT__ << std::hex <<
":::"
453 <<
"Write data enable: 0x" << (int)dataEnabled << std::endl;
457 __COUT__ << std::hex <<
":::"
458 <<
"Unknown write address received." << std::endl;
462 __COUT__ << std::hex <<
":::"
463 <<
"ERROR: The formatting of the string received is wrong! "
465 << numberOfBytes <<
" Read/Write " << buff[handlerIndex + 0]
468 handlerIndex += numberOfBytes;
471 __COUT__ << std::hex <<
":::"
473 << addressSpaceSS.str() <<
"\n\n";
487 if(count % data_gen_rate == 0 &&
491 __COUT__ << std::hex <<
":::"
492 <<
"Count: " << count <<
" rate: " << data_gen_rate
493 <<
" packet-counter: " << data_gen_cnt << std::endl;
494 __COUT__ << std::hex <<
":::"
495 <<
"Send Burst at count:" << count << std::endl;
498 wasDataEnable ? 2 : 1;
499 buff[1] = sequence++;
500 memcpy((
void*)&buff[TX_DATA_OFFSET],
506 packetSz = TX_DATA_OFFSET + 8;
515 if((numberOfBytes = sendto(
516 sendSockfd, buff, packetSz, 0, p->ai_addr, p->ai_addrlen)) ==
519 perror(
"Hw: sendto");
522 __PRINTF__(
"hw: sent %d bytes back. sequence=%d\n",
524 (
unsigned char)buff[1]);
526 if(data_gen_cnt != (uint64_t)-1)
530 wasDataEnable =
true;
532 else if(wasDataEnable)
534 wasDataEnable =
false;
535 __COUT__ << std::hex <<
":::"
536 <<
"Send Last in Burst at count:" << count << std::endl;
539 buff[1] = sequence++;
541 (
void*)&buff[TX_DATA_OFFSET], (
void*)&count, 8);
543 packetSz = TX_DATA_OFFSET + 8;
547 if((numberOfBytes = sendto(
548 sendSockfd, buff, packetSz, 0, p->ai_addr, p->ai_addrlen)) ==
551 perror(
"hw: sendto");
554 __PRINTF__(
"hw: sent %d bytes back. sequence=%d\n",
556 (
unsigned char)buff[1]);
559 __COUT__ << std::hex <<
":::"
560 <<
"Send socket not defined." << std::endl;