31 #include <sys/socket.h> 33 #include <netinet/in.h> 35 #include <arpa/inet.h> 37 #include <sys/ioctl.h> 71 struct sockaddr_in addr;
78 cout<<
"Connecting to eDrone......\n";
81 sockID = socket(AF_INET, SOCK_STREAM, 0);
85 cout<<
"Cannot connect to DroneNode, please try again1\n";
89 addr.sin_family = AF_INET;
90 addr.sin_port = htons(23);
91 addr.sin_addr.s_addr = inet_addr(
"192.168.4.1");
95 if( (arg = fcntl(sockID, F_GETFL, NULL)) < 0) {
97 cout<<
"Cannot connect to Drone, please try again2\n";
101 if( fcntl(sockID, F_SETFL, arg) < 0) {
103 cout<<
"Cannot connect to Drone, please try again3\n";
108 res = connect(sockID, (
struct sockaddr *)&addr,
sizeof(addr));
112 if (errno == EINPROGRESS) {
119 FD_SET(sockID, &myset);
120 res = select(sockID+1, NULL, &myset, NULL, &tv);
122 if (res < 0 && errno != EINTR) {
124 cout<<
"Cannot connect to Drone, please try again4\n";
130 if (getsockopt(sockID, SOL_SOCKET, SO_ERROR, (
void*)(&valopt), &lon) < 0) {
132 cout<<
"Cannot connect to Drone, please try again5\n";
138 cout<<
"Cannot connect to Drone, please try again6\n";
146 cout<<
"Cannot connect to Drone, please try again7\n";
153 cout<<
"Cannot connect to Drone, please try again8\n";
159 if( (arg = fcntl(sockID, F_GETFL, NULL)) < 0) {
161 cout<<
"Cannot connect to Drone, please try again9\n";
164 arg &= (~O_NONBLOCK);
165 if( fcntl(sockID, F_SETFL, arg) < 0) {
167 cout<<
"Cannot connect to Drone, please try again0\n";
173 if(getsockopt(sockID, SOL_SOCKET, SO_KEEPALIVE, &
optval, &
optlen) < 0) {
175 cout<<
"Cannot connect to Drone, please try again11\n";
183 if(setsockopt(sockID, SOL_SOCKET, SO_KEEPALIVE, &
optval,
optlen) < 0) {
185 cout<<
"Cannot connect to Drone, please try again12\n";
191 if(getsockopt(sockID, SOL_SOCKET, SO_KEEPALIVE, &
optval, &
optlen) < 0) {
193 cout<<
"Cannot connect to Drone, please try again13\n";
200 socklen_t
len =
sizeof (error);
201 int retval = getsockopt (sockID, SOL_SOCKET, SO_ERROR, &error, &len);
206 cout<<
"Cannot connect to Drone, please try again14\n";
214 cout<<
"Cannot connect to Drone, please try again15\n";
218 cout<<
"eDrone Connected\n";
224 int k=write(sockID,buf,count);
230 int k=read(sockID,buf,count);
248 }
else if (
c ==
'!') {
266 cmd = (uint8_t) (
c & 0xFF);
300 struct sockaddr_in addr;
307 cout<<
"Connecting to Drone\n";
310 sockIDList[index] = socket(AF_INET, SOCK_STREAM, 0);
312 if (sockIDList[index] < 0) {
314 cout<<
"Cannot connect to Drone, please try again1\n";
319 addr.sin_family = AF_INET;
320 addr.sin_port = htons(23);
321 addr.sin_addr.s_addr = inet_addr(ip.c_str());
325 if( (arg = fcntl(sockIDList[index], F_GETFL, NULL)) < 0) {
327 cout<<
"Cannot connect to Drone, please try again2\n";
331 if( fcntl(sockIDList[index], F_SETFL, arg) < 0) {
333 cout<<
"Cannot connect to Drone, please try again3\n";
338 res = connect(sockIDList[index], (
struct sockaddr *)&addr,
sizeof(addr));
342 if (errno == EINPROGRESS) {
349 FD_SET(sockIDList[index], &myset);
350 res = select(sockIDList[index]+1, NULL, &myset, NULL, &tv);
353 if (res < 0 && errno != EINTR) {
355 cout<<
"Cannot connect to Drone, please try again4\n";
360 if (getsockopt(sockIDList[index], SOL_SOCKET, SO_ERROR, (
void*)(&valopt), &lon) < 0) {
362 cout<<
"Cannot connect to Drone, please try again5\n";
368 cout<<
"Cannot connect to Drone, please try again6\n";
374 cout<<
"Cannot connect to Drone, please try again7\n";
381 cout<<
"Cannot connect to Drone, please try again8\n";
387 if( (arg = fcntl(sockIDList[index], F_GETFL, NULL)) < 0) {
389 cout<<
"Cannot connect to Drone, please try again9\n";
392 arg &= (~O_NONBLOCK);
393 if( fcntl(sockIDList[index], F_SETFL, arg) < 0) {
395 cout<<
"Cannot connect to Drone, please try again10\n";
400 if(getsockopt(sockIDList[index], SOL_SOCKET, SO_KEEPALIVE, &
optval, &
optlen) < 0) {
402 cout<<
"Cannot connect to Drone, please try again11\n";
403 close(sockIDList[index]);
410 if(setsockopt(sockIDList[index], SOL_SOCKET, SO_KEEPALIVE, &
optval,
optlen) < 0) {
412 cout<<
"Cannot connect to Drone, please try again12\n";
413 close(sockIDList[index]);
418 if(getsockopt(sockIDList[index], SOL_SOCKET, SO_KEEPALIVE, &
optval, &
optlen) < 0) {
420 cout<<
"Cannot connect to Drone, please try again13\n";
421 close(sockIDList[index]);
427 socklen_t
len =
sizeof (error);
428 int retval = getsockopt (sockIDList[index], SOL_SOCKET, SO_ERROR, &error, &len);
433 cout<<
"Cannot connect to Drone, please try again14\n";
441 cout<<
"Cannot connect to Drone, please try again15\n";
445 cout<<
"Drone Connected\n";
457 int k=write(sockIDList[i],buf,count);
466 int k=read(sockIDList[index],buf,count);
477 c = readMulSock(
recbuf,1,index);
489 }
else if (
c ==
'!') {
507 cmd = (uint8_t) (
c & 0xFF);
static const int HEADER_ERR
uint8_t readSock(void *buf, int count)
static const int HEADER_CMD
static const int HEADER_SIZE
uint8_t readMulSock(void *buf, int count, int index)
int writeMulSock(const void *buf, int count, int i)
static const int HEADER_ARROW
int writeSock(const void *buf, int count)
void evaluateCommand(int command)
void readMulFrame(int index)
static const int HEADER_M
bool connectMulSock(const std::string &ip, int index)
static const int HEADER_START