Make a struct for connections
This commit is contained in:
@@ -12,4 +12,6 @@ typedef struct Client* Client;
|
|||||||
typedef struct Message* Message;
|
typedef struct Message* Message;
|
||||||
|
|
||||||
Client client_new(void* buffer, size_t size);
|
Client client_new(void* buffer, size_t size);
|
||||||
|
void client_connect(Client client);
|
||||||
|
Message poll_message(Client client);
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include "connection.h"
|
||||||
|
|
||||||
struct Client {
|
struct Client {
|
||||||
|
|
||||||
void* buffer;
|
void* buffer;
|
||||||
@@ -10,6 +12,8 @@ struct Client {
|
|||||||
|
|
||||||
int16_t seq;
|
int16_t seq;
|
||||||
|
|
||||||
|
struct Connection* connection;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
static inline uint8_t host2byte(uint8_t host, uint8_t index) {
|
static inline uint8_t host2byte(uint8_t host, uint8_t index) {
|
||||||
|
|||||||
@@ -0,0 +1,78 @@
|
|||||||
|
|
||||||
|
#include "robomaster.h"
|
||||||
|
#include "client.h"
|
||||||
|
#include "connection.h"
|
||||||
|
#include "message.h"
|
||||||
|
|
||||||
|
static struct Connection* connection_new()
|
||||||
|
{
|
||||||
|
struct Connection* conn = calloc(sizeof(struct Connection));
|
||||||
|
|
||||||
|
// Request a UDP socket
|
||||||
|
conn->sockfd = socket(AF_INET, SOCK_DGRAM, 0);
|
||||||
|
|
||||||
|
// 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
|
||||||
|
conn->addrlen = sizeof(conn->remote_addr);
|
||||||
|
conn->dest_addr.sin_family = AF_INET;
|
||||||
|
conn->dest_addr.sin_port = htons(30030);
|
||||||
|
conn->dest_addr.sin_addr.s_addr = inet_addr("192.168.2.1");
|
||||||
|
|
||||||
|
return conn;
|
||||||
|
}
|
||||||
|
|
||||||
|
void client_connect(Client client) {
|
||||||
|
client->connection = connection_new();
|
||||||
|
|
||||||
|
sendto(client->connection->sockfd, client->buffer, session_size(session), 0, (struct sockaddr*)&client->connection->dest_addr, client->connection->addrlen);
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Use union to make all messages same size
|
||||||
|
void send_message(Client client, Message message, int length) {
|
||||||
|
sendto(client->connection->sockfd, message, length, 0, (struct sockaddr*)&client->connection->dest_addr, client->connection->addrlen);
|
||||||
|
}
|
||||||
|
|
||||||
|
Message poll_message(Client client) {
|
||||||
|
|
||||||
|
// Poll for messages
|
||||||
|
static const struct timeval timeout = {-1, 0};
|
||||||
|
fd_set read_fds;
|
||||||
|
FD_ZERO(&read_fds);
|
||||||
|
FD_SET(client->connection->sockfd, &read_fds);
|
||||||
|
int result = select(client->connection->sockfd + 1, &read_fds, NULL, NULL, &timeout);
|
||||||
|
|
||||||
|
// Check for socket polling errors
|
||||||
|
if(result < 0) {
|
||||||
|
perror("message polling failed");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Skip if nothing was received yet
|
||||||
|
// TODO: Make a static "empty" message or something
|
||||||
|
if (result == 0) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read a message from the socket
|
||||||
|
// TODO: Use union to make all messages same size
|
||||||
|
// NOTE: This is never freed, so it's a memory leak
|
||||||
|
void* buffer = malloc(1024);
|
||||||
|
int recvb = recvfrom(client->connection->sockfd, buffer, sizeof(client->max_size), 0, (struct sockaddr*)&client->connection->dest_addr, &client->connection->addrlen);
|
||||||
|
|
||||||
|
// Check for socket read errors
|
||||||
|
if(recvb < 0) {
|
||||||
|
perror("reading socket failed");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check for message errors
|
||||||
|
if(message_validate(buffer)) {
|
||||||
|
perror("invalid message");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (Message)buffer;
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
struct Connection {
|
||||||
|
int sockfd;
|
||||||
|
socklen_t addrlen;
|
||||||
|
sockaddr_in remote_addr;
|
||||||
|
};
|
||||||
|
|
||||||
@@ -95,3 +95,23 @@ message_new(struct Client* session, uint8_t cmdset, uint8_t cmdid, size_t length
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
enum MESSAGEERR {
|
||||||
|
MESSAGEERR_NONE,
|
||||||
|
MESSAGEERR_HEADERCRC,
|
||||||
|
MESSAGEERR_FOOTERCRC
|
||||||
|
}
|
||||||
|
|
||||||
|
static
|
||||||
|
inline
|
||||||
|
enum MESSAGEERR
|
||||||
|
message_validate(const struct Message* message) {
|
||||||
|
|
||||||
|
uint16_t length = (message->header->length_h[2] & 0x3) * 0xFF + message->header->length_l[1];
|
||||||
|
|
||||||
|
if(message->header.crc != crc8(message, 3))
|
||||||
|
return MESSAGEERR_HEADERCRC;
|
||||||
|
|
||||||
|
if(message->header.crc != crc16(message, length - sizeof(struct Footer)))
|
||||||
|
return MESSAGEERR_FOOTERCRC;
|
||||||
|
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user