helpers/trig.cpp

Go to the documentation of this file.
00001 #include <netinet/ip.h>
00002 #include <netinet/in.h>
00003 #include <sys/types.h>
00004 #include <sys/socket.h>
00005 #include <arpa/inet.h>
00006 #include <errno.h>
00007 #include <string.h>
00008 #include <unistd.h>
00009 #include <linux/if_ether.h>
00010 #include <sys/ioctl.h>
00011 #include <string.h>
00012 #include <sys/socket.h>
00013 #include <sys/param.h>
00014 #include <unistd.h>
00015 #include <sys/ioctl.h>
00016 #include <netinet/in.h>
00017 #include <net/if.h>
00018 #include <netdb.h>
00019 #include <arpa/inet.h>
00020 #include <linux/if_ether.h>
00021 #include <poll.h>
00022 
00023 #include <map>
00024 #include <set>
00025 #include <iostream>
00026 
00027 using namespace std;
00028 
00029 unsigned long port_out = 40000;
00030 unsigned long port_in = 40001;
00031 
00032 int signal_recv(set<unsigned long> ports) {
00033 //int signal_recv(unsigned long port) {
00034         int nfds = ports.size();
00035         struct pollfd *fds = (struct pollfd*)malloc(sizeof(struct pollfd) * nfds);
00036         int i = 0;
00037         struct sockaddr_in addr;
00038         memset(&addr, 0, sizeof(struct sockaddr_in));
00039         addr.sin_family = AF_INET;
00040         addr.sin_addr.s_addr = INADDR_ANY;
00041         int zu = 1;
00042         unsigned long buf[3];
00043         for(set<unsigned long>::iterator it = ports.begin(); it != ports.end(); it++, i++) {
00044 //              cout << "port: " << *it << endl;
00045                 int s = socket(PF_INET, SOCK_DGRAM, 0);
00046                 fds[i].fd = s;
00047                 fds[i].events = POLLIN;
00048                 fds[i].revents = 0;
00049                 addr.sin_port = htons(*it);
00050                 setsockopt(s, SOL_SOCKET, SO_BROADCAST, &zu, sizeof(zu));
00051                 bind(s, (struct sockaddr *)&addr, sizeof(struct sockaddr_in));
00052         }
00053         int c = nfds;
00054         while(c != 0) {
00055                 poll(fds, nfds, 0);
00056                 for(i = 0; i < nfds; i++) {
00057                         if(fds[i].revents |= POLLIN) {
00058                                 fds[i].events = 0;
00059                                 fds[i].revents = 0;
00060                                 int z = recv(fds[i].fd, (void *)&buf[0], 12, 0);
00061                                 char b[256];
00062                                 sprintf(b, "Magic: 0x%08X, ID: 0x%08X, count: 0x%08X", htonl(buf[0]), htonl(buf[1]), htonl(buf[2]));
00063                                 cerr << "Accept signal with length: " << z << " and data: " << b << endl;
00064                                 c--;
00065                         }
00066                 }
00067         }
00068         free(fds);
00069         return 0;
00070 }
00071 
00072 int signal_send(unsigned long port) {
00073         int s = socket(PF_INET, SOCK_DGRAM, 0);
00074         struct sockaddr_in addr;
00075 
00076         memset(&addr, 0, sizeof(struct sockaddr_in));
00077         addr.sin_family = AF_INET;
00078         addr.sin_port = htons(port);
00079 //      addr.sin_addr.s_addr = INADDR_BROADCAST;
00080         addr.sin_addr.s_addr = inet_addr("192.168.0.15");
00081         int zu = 1;
00082         setsockopt(s, SOL_SOCKET, SO_BROADCAST, &zu, sizeof(zu));
00083         // make bind to real ethernet interface, to send packets to correct iface
00084         bind(s, (struct sockaddr*)&addr, sizeof(struct sockaddr_in));
00085 
00086         memset(&addr, 0, sizeof(struct sockaddr_in));
00087         addr.sin_family = AF_INET;
00088         addr.sin_port = htons(port);
00089         addr.sin_addr.s_addr = INADDR_BROADCAST;
00090 //      addr.sin_addr.s_addr = inet_addr("192.168.0.10");
00091         zu = 1;
00092         setsockopt(s, SOL_SOCKET, SO_BROADCAST, &zu, sizeof(zu));
00093         connect(s, (struct sockaddr*)&addr, sizeof(struct sockaddr_in));
00094 
00095 //      char data[] = {0x01};
00096         unsigned long data[3];
00097 //      data[0] = 0x5452472D;
00098         data[0] = 0x2D475254;
00099         data[1] = htonl(0x00001000);
00100         data[2] = htonl(1);
00101 //      send(s, (void *)&data[0], 12, 0);
00102         send(s, (void *)&data[0], 12, 0);
00103         close(s);
00104         return 0;
00105 }
00106 
00107 int main(int argc, char *argv[]) {
00108         cout << "options: [-in port_1[,port_2[,...]]] [-out port_out]; receive signals READY from -in ports, by default " << port_in
00109                 << " and send broadcast pulse to port -out, by default " << port_out << endl;
00110 //      signal_recv(port_in);
00111 //      usleep(5000); // wait before send trigger pulse, because with 6.3.3 firmware camera can lost responce
00112 
00113         string opt;
00114         map<string, string> args;
00115         for(int i = 1; i < argc; i++) {
00116                 if(argv[i][0] == '-' && argv[i][1] != '\0') {
00117                         if(opt != "")
00118                                 args[opt] = "";
00119                         opt = &argv[i][1];
00120                         continue;
00121                 } else {
00122                         if(opt != "") {
00123                                 args[opt] = argv[i];
00124                                 opt = "";
00125                         }
00126                 }
00127         }
00128         if(opt != "")
00129                 args[opt] = "";
00130 //      for(map<string, string>::iterator it = args.begin(); it != args.end(); it++) {
00131 //              cerr << "|" << (*it).first << "| == |" << (*it).second << "|" << endl;
00132 //      }
00133 
00134         set<unsigned long> ports_in;
00135         string in;
00136         if((in = args["in"]) != "") {
00137                 in += ",";
00138                 const char *c = in.c_str();
00139 //              cout << "in == |" << in << "|" << endl;
00140                 for(int i = 0; c[i]; i++) {
00141                         string n = "";
00142                         for(; c[i] != ','; i++)
00143                                 n += c[i];
00144                         ports_in.insert(atol(n.c_str()));
00145                         
00146                 }
00147         } else
00148                 ports_in.insert(port_in);
00149         if(args["out"] != "")
00150                 port_out = atol(args["out"].c_str());
00151 
00152         signal_recv(ports_in);
00153         signal_send(port_out);
00154         return 0;
00155 }

Generated on Fri Nov 28 00:06:23 2008 for elphel by  doxygen 1.5.1