|
|
@ -7,6 +7,7 @@
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/select.h>
|
|
|
|
#include <sys/select.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdio.h>
|
|
|
@ -40,8 +41,6 @@ message_length(int cmd) {
|
|
|
|
return sizeof(struct BlasterFireReq);
|
|
|
|
return sizeof(struct BlasterFireReq);
|
|
|
|
case STREAM_CTRL_CMD:
|
|
|
|
case STREAM_CTRL_CMD:
|
|
|
|
return sizeof(struct StreamCtrlReq);
|
|
|
|
return sizeof(struct StreamCtrlReq);
|
|
|
|
case ADD_SUB_MSG_CMD:
|
|
|
|
|
|
|
|
return sizeof(struct AddSubMsgReq);
|
|
|
|
|
|
|
|
default:
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -59,7 +58,6 @@ message_module(int cmd) {
|
|
|
|
case SET_ROBOT_MODE_CMD:
|
|
|
|
case SET_ROBOT_MODE_CMD:
|
|
|
|
case SUBNODE_RESET_CMD:
|
|
|
|
case SUBNODE_RESET_CMD:
|
|
|
|
case SUBSCRIBE_ADD_NODE_CMD:
|
|
|
|
case SUBSCRIBE_ADD_NODE_CMD:
|
|
|
|
case ADD_SUB_MSG_CMD:
|
|
|
|
|
|
|
|
return host2byte(SDK_HOST, SDK_INDEX);
|
|
|
|
return host2byte(SDK_HOST, SDK_INDEX);
|
|
|
|
case SET_WHEEL_SPEED_CMD:
|
|
|
|
case SET_WHEEL_SPEED_CMD:
|
|
|
|
case CHASSIS_SPEED_MODE_CMD:
|
|
|
|
case CHASSIS_SPEED_MODE_CMD:
|
|
|
@ -81,49 +79,44 @@ int max_fd = -1;
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: Close the socket and return NULL on error
|
|
|
|
// TODO: Close the socket and return NULL on error
|
|
|
|
struct Connection*
|
|
|
|
struct Connection*
|
|
|
|
connection_new(uint16_t source_port, uint32_t source_ip, uint16_t dest_port, uint32_t dest_ip)
|
|
|
|
connection_new(unsigned int source_port, const char* source_ip, unsigned int dest_port, const char* dest_ip)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
struct Connection* conn = malloc(sizeof(struct Connection));
|
|
|
|
struct Connection* conn = malloc(sizeof(struct Connection));
|
|
|
|
memset(conn, 0, sizeof(struct Connection));
|
|
|
|
memset(conn, 0, sizeof(struct Connection));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Request a UDP socket
|
|
|
|
|
|
|
|
conn->sockfd = socket(AF_INET, SOCK_DGRAM, 0);
|
|
|
|
|
|
|
|
|
|
|
|
// Set the source address and port if they are provided
|
|
|
|
// Set the source address and port if they are provided
|
|
|
|
if(source_port && source_ip) {
|
|
|
|
if(source_port && source_ip) {
|
|
|
|
conn->local_addr.sin_family = AF_INET;
|
|
|
|
struct sockaddr_in loc_addr;
|
|
|
|
conn->local_addr.sin_port = htons(source_port);
|
|
|
|
loc_addr.sin_family = AF_INET;
|
|
|
|
conn->local_addr.sin_addr.s_addr = source_ip;
|
|
|
|
loc_addr.sin_port = htons(source_port);
|
|
|
|
|
|
|
|
loc_addr.sin_addr.s_addr = inet_addr(source_ip);
|
|
|
|
|
|
|
|
if(bind(conn->sockfd, (struct sockaddr*)&loc_addr, sizeof(loc_addr)) < 0)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
perror("unable to bind local port");
|
|
|
|
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Make the socket non-blocking
|
|
|
|
|
|
|
|
int flags = fcntl(conn->sockfd, F_GETFL);
|
|
|
|
|
|
|
|
fcntl(conn->sockfd, F_SETFL, flags | O_NONBLOCK);
|
|
|
|
|
|
|
|
|
|
|
|
// Set the address of the drone
|
|
|
|
// Set the address of the drone
|
|
|
|
memset(&conn->remote_addr, 0, sizeof(conn->remote_addr));
|
|
|
|
memset(&conn->remote_addr, 0, sizeof(conn->remote_addr));
|
|
|
|
conn->addrlen = sizeof(conn->remote_addr);
|
|
|
|
conn->addrlen = sizeof(conn->remote_addr);
|
|
|
|
conn->remote_addr.sin_family = AF_INET;
|
|
|
|
conn->remote_addr.sin_family = AF_INET;
|
|
|
|
conn->remote_addr.sin_port = htons(dest_port);
|
|
|
|
conn->remote_addr.sin_port = htons(dest_port);
|
|
|
|
conn->remote_addr.sin_addr.s_addr = dest_ip;
|
|
|
|
conn->remote_addr.sin_addr.s_addr = inet_addr(dest_ip);
|
|
|
|
|
|
|
|
|
|
|
|
return conn;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
enum connection_error
|
|
|
|
|
|
|
|
connection_connect(struct Connection* conn) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Request a UDP socket
|
|
|
|
|
|
|
|
conn->sockfd = socket(AF_INET, SOCK_DGRAM, 0);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(conn->local_addr.sin_addr.s_addr
|
|
|
|
|
|
|
|
&& bind(conn->sockfd, (struct sockaddr*)&conn->local_addr, sizeof(conn->local_addr)) < 0)
|
|
|
|
|
|
|
|
return CONNECTION_LOCAL_BIND;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Make the socket non-blocking
|
|
|
|
|
|
|
|
int flags = fcntl(conn->sockfd, F_GETFL);
|
|
|
|
|
|
|
|
fcntl(conn->sockfd, F_SETFL, flags | O_NONBLOCK);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// File descriptors are numbers that count up sequentially,
|
|
|
|
// File descriptors are numbers that count up sequentially,
|
|
|
|
// so save the last one as the greatest file descriptor.
|
|
|
|
// so save the last one as the greatest file descriptor.
|
|
|
|
// This is needed for polling the sockets later.
|
|
|
|
// This is needed for polling the sockets later.
|
|
|
|
max_fd = conn->sockfd;
|
|
|
|
max_fd = conn->sockfd;
|
|
|
|
|
|
|
|
|
|
|
|
return CONNECTION_NO_ERROR;
|
|
|
|
return conn;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct Connection*
|
|
|
|
struct Connection*
|
|
|
|