50 #define STD_REPLY_TIMEOUT SEC_TO_USEC(0.2) // Should be tens of ms at least or flash writes will fail.
51 #define STOP_REPLY_TIMEOUT SEC_TO_USEC(0.001)
96 static const char *badprefix =
"Bad URL prefix, expected name://, serial:// or any://.";
97 static const char *multiaddress =
"Multiple @-prefixed camera addresses found.";
98 static const char *multiinterface =
"Multiple #-prefixed host interfaces found.";
99 static const char *discoverfailed =
"Discover failed. The specified address or interface may be bad.";
100 static const char *badserial =
"serial:// may only be followed by an integer.";
101 static const char *badany =
"Unexpected characters after any://.";
102 static const char *badip =
"@ must be followed by a valid IP address.";
103 static const char *nomatch =
"No cameras matched the URL.";
104 static const char *multimatch =
"More than one camera matched the URL.";
105 static const char *illegalcase =
"Illegal case, this is a bug.";
106 static const char *nomem =
"malloc failed";
107 const char *name =
"name://";
int namelen = strlen(name);
108 const char *serial =
"serial://";
int seriallen = strlen(serial);
109 const char *any =
"any://";
int anylen = strlen(any);
112 char *address = NULL;
113 char *
interface = NULL;
114 char *copy = malloc(strlen(url) + 1);
118 unsigned int serialnum = -1;
125 *errmsg = illegalcase;
135 if (!strncmp(copy, name, namelen))
137 idstart = copy + namelen;
140 else if (!strncmp(copy, serial, seriallen))
142 idstart = copy + seriallen;
145 else if (!strncmp(copy, any, anylen))
147 idstart = copy + anylen;
158 for (curpos = idstart; *curpos; curpos++)
165 *errmsg = multiaddress;
168 address = curpos + 1;
170 else if (*curpos ==
'#')
175 *errmsg = multiinterface;
178 interface = curpos + 1;
186 if (mode == 3 && *idstart)
196 serialnum = strtol(idstart, &endpos, 10);
197 if (*idstart == 0 || *endpos != 0)
211 *errmsg = discoverfailed;
219 if ((mode == 1 && strcmp(idstart, curEntry->
cam_name) == 0) ||
220 (mode == 2 && serialnum == curEntry->
serial) ||
227 *errmsg = multimatch;
230 memcpy(camera, curEntry,
sizeof(
IpCamList));
234 if (inet_aton(address, &ip))
236 uint8_t *ipbytes = (uint8_t *) &ip.s_addr;
237 snprintf(camera->
ip_str,
sizeof(camera->
ip_str),
"%i.%i.%i.%i", ipbytes[0], ipbytes[1], ipbytes[2], ipbytes[3]);
238 camera->
ip = ip.s_addr;
263 *errmsg = illegalcase;
290 if( recvfrom(
s, &sPkt,
sizeof(
PacketStatus), 0, NULL, NULL ) == -1 ) {
291 perror(
"wge100StatusWait unable to receive from socket");
308 static void xormem(uint8_t *dst, uint8_t *src,
size_t w)
320 strncpy(dPkt.
hdr.
hrt,
"DISCOVER",
sizeof(dPkt.
hdr.
hrt));
334 struct in_addr newIP;
337 inet_aton(ipAddress, &newIP);
342 struct in_addr localip;
343 struct in_addr netmask;
346 dPkt.
ip_addr = localip.s_addr ^ netmask.s_addr ^ ~0;
350 perror(
"Unable to send broadcast\n");
378 const char **ifNames = NULL;
379 struct ifaddrs *ifaces = NULL;
380 struct ifaddrs *curif;
386 if (!ifName || !ifName[0])
389 if (getifaddrs(&ifaces))
391 perror(
"getifaddrs failed");
396 for (curif = ifaces; curif; curif = curif->ifa_next)
401 ifNames = calloc(numif,
sizeof(
char *));
404 perror(
"allocating interfaces memory");
412 for (numif = 0, curif = ifaces; curif; curif = curif->ifa_next)
415 if (curif->ifa_addr && curif->ifa_addr->sa_family == AF_INET)
416 ifNames[numif++] = curif->ifa_name;
420 s = calloc(numif,
sizeof(
int));
423 perror(
"allocating socket memory");
427 for (nums = 0; nums < numif; nums++)
435 for (i = nums; i < numif; i++)
437 ifNames[i] = ifNames[i+1];
446 if( curs != -1 && wait_us != 0 ) {
449 struct sockaddr_in fromaddr;
450 fromaddr.sin_family = AF_INET;
451 socklen_t fromlen =
sizeof(fromaddr);
454 packet_len = recvfrom(
s[curs], &aPkt,
sizeof(
PacketAnnounce), 0, (
struct sockaddr *) &fromaddr, &fromlen);
455 if(packet_len == -1 ) {
456 perror(
"wge100Discover unable to receive from socket");
474 perror(
"Malloc failed");
483 uint8_t *ipbytes = (uint8_t *) &aPkt.
camera_ip;
484 snprintf(tmpListItem->
ip_str,
sizeof(tmpListItem->
ip_str),
"%i.%i.%i.%i", ipbytes[0], ipbytes[1], ipbytes[2], ipbytes[3]);
486 memcpy(&tmpListItem->
mac, aPkt.
mac,
sizeof(aPkt.
mac));
489 strncpy(tmpListItem->
ifName, ifNames[curs],
sizeof(tmpListItem->
ifName));
490 tmpListItem->
ifName[
sizeof(tmpListItem->
ifName) - 1] = 0;
492 char pcb_rev = 0x0A + (0x0000000F & ntohl(aPkt.
hw_version));
493 int hdl_rev = 0x00000FFF & (ntohl(aPkt.
hw_version)>>4);
500 wge100_debug(
"MAC Address: %02X:%02X:%02X:%02X:%02X:%02X\n", aPkt.
mac[0], aPkt.
mac[1], aPkt.
mac[2], aPkt.
mac[3], aPkt.
mac[4], aPkt.
mac[5]);
501 wge100_debug(
"Product #%07u : Unit #%04u\n", ntohl(aPkt.
product_id), ntohl(aPkt.
ser_no));
502 wge100_debug(
"%s\n", tmpListItem->
hwinfo);
506 }
while(wait_us > 0);
507 retval = newCamCount;
512 for (i = 0; i < nums; i++)
539 strncpy(cPkt.
hdr.
hrt,
"CONFIGURE",
sizeof(cPkt.
hdr.
hrt));
551 perror(
"wge100Configure socket creation failed");
556 if (!ipAddress || !*ipAddress)
558 wge100_debug(
"No ipAddress, using %x\n", camInfo->
ip);
562 wge100_debug(
"Unable to send packet\n");
569 struct in_addr newIP;
570 inet_aton(ipAddress, &newIP);
572 wge100_debug(
"Using ipAddress %s -> %x iface %s\n", ipAddress, cPkt.
ip_addr, camInfo->
ifName);
574 wge100_debug(
"Sending broadcast discover packet.\n");
576 wge100_debug(
"Unable to send broadcast\n");
584 wge100_debug(
"Connecting to %x.\n", camIP);
586 wge100_debug(
"Unable to connect\n");
596 if( recvfrom(
s, &aPkt,
sizeof(
PacketAnnounce), 0, NULL, NULL ) == -1 ) {
597 perror(
"wge100Discover unable to receive from socket");
614 }
while(wait_us > 0);
620 wge100_debug(
"Timed out.\n");
644 strncpy(vPkt.
hdr.
hrt,
"Start Video",
sizeof(vPkt.
hdr.
hrt));
648 inet_aton(ipAddress, (
struct in_addr*)&vPkt.
receiver.
addr);
683 wge100_debug(
"Error: wge100StatusWait returned status %d, code %d\n", type, code);
706 strncpy(vPkt.
hdr.
hrt,
"Stop Video",
sizeof(vPkt.
hdr.
hrt));
734 wge100_debug(
"Error: wge100StatusWait returned status %d, code %d\n", type, code);
756 strncpy(gPkt.
hdr.
hrt,
"ReconfigureFPGA",
sizeof(gPkt.
hdr.
hrt));
838 strncpy(gPkt.
hdr.
hrt,
"Time Req",
sizeof(gPkt.
hdr.
hrt));
863 if( recvfrom(
s, &tPkt,
sizeof(
PacketTimer), 0, NULL, NULL ) == -1 ) {
864 perror(
"GetTime unable to receive from socket");
869 *time_us = (uint64_t)ntohl(tPkt.
ticks_hi) << 32;
883 }
while(wait_us > 0);
885 wge100_debug(
"Timed out waiting for time value\n");
913 for (; *retries > 0; (*retries)--)
920 wge100_debug(
"Flash read failed.");
951 rPkt.
address = htonl(address<<9);
953 strncpy(rPkt.
hdr.
hrt,
"Flash read",
sizeof(rPkt.
hdr.
hrt));
980 perror(
"GetTime unable to receive from socket");
990 }
while(wait_us > 0);
992 wge100_debug(
"Timed out waiting for flash value\n");
1020 if (retries == NULL)
1026 for (; *retries > 0; (*retries)--)
1032 wge100_debug(
"Failed write, retries left: %i.", *retries);
1088 rPkt.
address = htonl(address<<9);
1089 strncpy(rPkt.
hdr.
hrt,
"Flash write",
sizeof(rPkt.
hdr.
hrt));
1113 uint32_t type, code;
1120 wge100_debug(
"Error: wge100StatusWait returned status %d, code %d\n", type, code);
1144 strncpy(tPkt.
hdr.
hrt,
"Int. Trigger",
sizeof(tPkt.
hdr.
hrt));
1146 strncpy(tPkt.
hdr.
hrt,
"Ext. Trigger",
sizeof(tPkt.
hdr.
hrt));
1169 uint32_t type, code;
1176 wge100_debug(
"Error: wge100StatusWait returned status %d, code %d\n", type, code);
1201 strncpy(sPkt.
hdr.
hrt,
"System Config",
sizeof(sPkt.
hdr.
hrt));
1202 memcpy(&sPkt.
mac, mac, 6);
1203 sPkt.
serial = htonl(serial);
1225 uint32_t type, code;
1232 wge100_debug(
"Error: wge100StatusWait returned status %d, code %d\n", type, code);
1253 uint16_t readbackdata;
1257 if (retries == NULL)
1260 for (; *retries > 0; (*retries)--)
1270 if (readbackdata ==
data)
1298 strncpy(sPkt.
hdr.
hrt,
"Write I2C",
sizeof(sPkt.
hdr.
hrt));
1322 uint32_t type, code;
1329 wge100_debug(
"Error: wge100StatusWait returned status %d, code %d\n", type, code);
1354 if (retries == NULL)
1356 for (; *retries > 0; (*retries)--)
1385 strncpy(rPkt.
hdr.
hrt,
"Read I2C",
sizeof(rPkt.
hdr.
hrt));
1411 perror(
"SensorRead unable to receive from socket");
1420 }
while(wait_us > 0);
1422 wge100_debug(
"Timed out waiting for sensor value\n");
1445 strncpy(sPkt.
hdr.
hrt,
"Select I2C",
sizeof(sPkt.
hdr.
hrt));
1469 uint32_t type, code;
1476 wge100_debug(
"Error: wge100StatusWait returned status %d, code %d\n", type, code);
1498 mPkt.
mode = htonl(mode);
1500 strncpy(mPkt.
hdr.
hrt,
"Set Mode",
sizeof(mPkt.
hdr.
hrt));
1522 uint32_t type, code;
1529 wge100_debug(
"Error: wge100StatusWait returned status %d, code %d\n", type, code);
1558 strncpy(rPkt.
hdr.
hrt,
"Set Res",
sizeof(rPkt.
hdr.
hrt));
1580 uint32_t type, code;
1587 wge100_debug(
"Error: wge100StatusWait returned status %d, code %d\n", type, code);
1592 #define MAX_HORIZ_RESOLUTION 752
1593 #define LINE_NUMBER_MASK 0x3FF
1609 size_t bufsize = 16*1024*1024;
1613 int return_value = 0;
1615 if( setsockopt(
s, SOL_SOCKET,SO_RCVBUF, &bufsize,
sizeof(bufsize)) == -1) {
1616 perror(
"Can't set rcvbuf option");
1621 socklen_t bufsizesize =
sizeof(bufsize);
1622 if( getsockopt(
s, SOL_SOCKET,SO_RCVBUF, &bufsize, &bufsizesize) == 0) {
1623 wge100_debug(
"Receive buffer size is: %i (%i)\n", bufsize, bufsizesize);
1627 perror(
"Can't read receive buffer size");
1633 if(frame_buf == NULL) {
1634 perror(
"Can't malloc frame buffer");
1642 perror(
"Can't malloc line packet buffer");
1648 bool firstPacket =
true;
1654 bool frameStartTimeSet;
1669 struct sockaddr_in fromaddr;
1677 uint8_t xorline[
width];
1681 frameComplete =
false;
1682 frameStartTimeSet =
false;
1693 if( (eof==NULL) && (firstPacket==
false) ) {
1704 struct timeval readtimeout;
1706 socklen_t fromaddrlen =
sizeof(
struct sockaddr_in);
1707 fromaddr.sin_family = AF_INET;
1712 readtimeout.tv_sec = 1;
1713 readtimeout.tv_usec = 0;
1718 if( select(
s+1, &
set, NULL, NULL, &readtimeout) == -1 ) {
1719 perror(
"wge100VidReceive select failed");
1725 if(! FD_ISSET(
s, &
set) && errno != EINTR) {
1726 wge100_debug(
"Select timed out. Calling handler.");
1727 handlerReturn = frameHandler(NULL, userData);
1731 }
while (! FD_ISSET(
s, &
set));
1737 if( recvfrom(
s, vPkt,
sizeof(
HeaderVideoLine)+
width, 0, (
struct sockaddr *) &fromaddr, &fromaddrlen ) == -1 ) {
1738 perror(
"wge100VidReceive unable to receive from socket");
1753 wge100_debug(
"Mismatched line/frame numbers: %02X / %02X\n", temp, (vPkt->
header.
frame_number & 0x003F));
1764 if(firstPacket ==
true) {
1765 firstPacket =
false;
1770 if (!frameStartTimeSet)
1772 gettimeofday(&frameInfo.
startTime, NULL);
1773 frameStartTimeSet =
true;
1786 wge100_debug(
"Video aborted\n");
1790 wge100_debug(
"Frame #%u missing EOF, got %i lines\n", frameInfo.
frame_number, lineCount);
1791 frameComplete =
true;
1799 frameComplete =
true;
1812 eof->
i2c[i] = ntohs(eof->
i2c[i]);
1815 if(lineCount !=
height) {
1816 if ((1 == (
height - lineCount)) && has_xor) {
1822 memcpy(repair, xorline,
width);
1824 for (y = 0; y <
height; y++) {
1844 wge100_debug(
"Too many lines received for frame!\n")
1856 wge100_debug(
"Frame #%i missed %i line(s) starting at %i src port %i\n", vPkt->
header.
frame_number,
1857 missedLines, lineCount + frameInfo.
missingLines, ntohs(fromaddr.sin_port));
1863 }
while(frameComplete ==
false);
1865 if( frameComplete ==
true ) {
1869 handlerReturn = frameHandler(&frameInfo, userData);
1874 uint8_t dummy = 0xff;
1875 if( sendto(
s, &dummy,
sizeof(dummy), 0, (
struct sockaddr *) &fromaddr,
sizeof(fromaddr) ) == -1 ) {
1883 }
while( handlerReturn == 0 );
1886 return return_value;
1890 struct in_addr host_addr;
1893 if( frameHandler == NULL ) {
1894 wge100_debug(
"Invalid frame handler, aborting.\n");
1898 wge100_debug(
"wge100VidReceive ready to receive on %s (%s:%u)\n", ifName, inet_ntoa(host_addr), port);
1909 struct sockaddr localMac;
1910 struct in_addr localIp;
1911 struct sockaddr localPort;
1912 socklen_t localPortLen;
1918 fprintf(stderr,
"Unable to get local IP address for interface %s", camera->
ifName);
1923 fprintf(stderr,
"Unable to get local MAC address for interface %s", camera->
ifName);
1927 if( frameHandler == NULL ) {
1928 wge100_debug(
"Invalid frame handler, aborting.\n");
1937 localPortLen =
sizeof(localPort);
1938 if (getsockname(
s, &localPort, &localPortLen) == -1)
1940 fprintf(stderr,
"Unable to get local port for socket.");
1945 port = ntohs(((
struct sockaddr_in *)&localPort)->sin_port);
1948 wge100_debug(
"wge100VidReceiveAuto ready to receive on %s (%s:%u)\n", camera->
ifName, inet_ntoa(localIp), port);
1950 if (
wge100StartVid( camera, (uint8_t *)&(localMac.sa_data[0]), inet_ntoa(localIp), port) != 0 )
1952 wge100_debug(
"Could not start camera streaming.");