Compare commits

..

2 Commits

@ -3,14 +3,20 @@
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
#define PACKED __attribute__((__packed__)) #if defined(_MSC_VER) // Check if compiling with Microsoft Visual Studio
#define PACKED(__DECL__) __pragma(pack(push, 1)) __DECL__ __pragma(pack(pop))
#elif defined(__GNUC__) // Check if compiling with GCC or any compiler defining __GNUC__, including Clang
#define PACKED(__DECL__) __DECL__ __attribute__((packed))
#else
#error "Unsupported compiler"
#endif
static const uint8_t CLIENT_HOST = 9; static const uint8_t CLIENT_HOST = 9;
static const uint8_t CLIENT_INDEX = 6; static const uint8_t CLIENT_INDEX = 6;
static const uint8_t PREAMBLE = 0x55; static const uint8_t PREAMBLE = 0x55;
struct PACKED Header { PACKED( struct Header {
// The preamble marks the start of a message and is always 0x55 // The preamble marks the start of a message and is always 0x55
uint8_t preamble; uint8_t preamble;
@ -53,13 +59,13 @@ struct PACKED Header {
}; };
}; };
}; });
struct PACKED Footer { PACKED( struct Footer {
uint16_t crc; uint16_t crc;
}; });
struct PACKED SetSystemLedReq { PACKED( struct SetSystemLedReq {
struct Header header; struct Header header;
@ -88,87 +94,87 @@ struct PACKED SetSystemLedReq {
struct Footer footer; struct Footer footer;
}; });
struct PACKED SetSystemLedResp { PACKED( struct SetSystemLedResp {
struct Header header; struct Header header;
uint8_t retcode; uint8_t retcode;
struct Footer footer; struct Footer footer;
}; });
struct PACKED SubscribeAddNodeReq PACKED( struct SubscribeAddNodeReq
{ {
struct Header header; struct Header header;
uint8_t hostbyte; uint8_t hostbyte;
uint32_t sub_vision; // always 0x03000000 uint32_t sub_vision; // always 0x03000000
struct Footer footer; struct Footer footer;
}; });
struct PACKED SubscribeAddNodeResp PACKED( struct SubscribeAddNodeResp
{ {
struct Header header; struct Header header;
uint8_t retcode; // 0x00 or 0x50 are acceptable here uint8_t retcode; // 0x00 or 0x50 are acceptable here
uint8_t hostbyte; uint8_t hostbyte;
struct Footer footer; struct Footer footer;
}; });
struct PACKED SubNodeResetReq PACKED( struct SubNodeResetReq
{ {
struct Header header; struct Header header;
uint8_t hostbyte; uint8_t hostbyte;
struct Footer footer; struct Footer footer;
}; });
struct PACKED SubNodeResetResp PACKED( struct SubNodeResetResp
{ {
struct Header header; struct Header header;
uint8_t retcode; uint8_t retcode;
struct Footer footer; struct Footer footer;
}; });
struct PACKED SetRobotModeReq PACKED( struct SetRobotModeReq
{ {
struct Header header; struct Header header;
uint8_t mode; uint8_t mode;
struct Footer footer; struct Footer footer;
}; });
struct PACKED SetRobotModeResp PACKED( struct SetRobotModeResp
{ {
struct Header header; struct Header header;
uint8_t retcode; uint8_t retcode;
struct Footer footer; struct Footer footer;
}; });
struct PACKED ChassisSpeedModeReq PACKED( struct ChassisSpeedModeReq
{ {
struct Header header; struct Header header;
float speed[3]; float speed[3];
struct Footer footer; struct Footer footer;
}; });
struct PACKED ChassisSpeedModeResp PACKED( struct ChassisSpeedModeResp
{ {
struct Header header; struct Header header;
int8_t retcode; int8_t retcode;
struct Footer footer; struct Footer footer;
}; });
struct PACKED SetChassisWheelSpeedReq PACKED( struct SetChassisWheelSpeedReq
{ {
struct Header header; struct Header header;
int8_t wheel_speed[4]; int8_t wheel_speed[4];
struct Footer footer; struct Footer footer;
}; });
struct PACKED SetChassisWheelSpeedResp PACKED( struct SetChassisWheelSpeedResp
{ {
struct Header header; struct Header header;
int8_t retcode; int8_t retcode;
struct Footer footer; struct Footer footer;
}; });
struct PACKED GimbalCtrlSpeedReq PACKED( struct GimbalCtrlSpeedReq
{ {
struct Header header; struct Header header;
// Values between -360 and 360 // Values between -360 and 360
@ -183,53 +189,53 @@ struct PACKED GimbalCtrlSpeedReq
// Always 0xDC // Always 0xDC
uint8_t ctrl; uint8_t ctrl;
struct Footer footer; struct Footer footer;
}; });
struct PACKED GimbalCtrlSpeedResp PACKED( struct GimbalCtrlSpeedResp
{ {
struct Header header; struct Header header;
int8_t retcode; int8_t retcode;
struct Footer footer; struct Footer footer;
}; });
struct PACKED SetWheelSpeedReq PACKED( struct SetWheelSpeedReq
{ {
struct Header header; struct Header header;
int16_t wheel_speed[4]; int16_t wheel_speed[4];
struct Footer footer; struct Footer footer;
}; });
struct PACKED SetWheelSpeedResp PACKED( struct SetWheelSpeedResp
{ {
struct Header header; struct Header header;
int8_t retcode; int8_t retcode;
struct Footer footer; struct Footer footer;
}; });
struct PACKED SdkHeartbeatReq { PACKED( struct SdkHeartbeatReq {
struct Header header; struct Header header;
struct Footer footer; struct Footer footer;
}; });
struct PACKED SdkHeartbeatResp { PACKED( struct SdkHeartbeatResp {
struct Header header; struct Header header;
uint8_t retcode; uint8_t retcode;
struct Footer footer; struct Footer footer;
}; });
struct PACKED SetSdkModeReq { PACKED( struct SetSdkModeReq {
struct Header header; struct Header header;
uint8_t enable; uint8_t enable;
struct Footer footer; struct Footer footer;
}; });
struct PACKED SetSdkModeResp { PACKED( struct SetSdkModeResp {
struct Header header; struct Header header;
uint8_t retcode; uint8_t retcode;
struct Footer footer; struct Footer footer;
}; });
struct PACKED SetSdkConnectionReq { PACKED( struct SetSdkConnectionReq {
struct Header header; struct Header header;
uint8_t control; uint8_t control;
uint8_t host; uint8_t host;
@ -238,32 +244,32 @@ struct PACKED SetSdkConnectionReq {
uint32_t ip_address; uint32_t ip_address;
uint16_t port; uint16_t port;
struct Footer footer; struct Footer footer;
}; });
struct PACKED SetSdkConnectionResp { PACKED( struct SetSdkConnectionResp {
struct Header header; struct Header header;
uint8_t retcode; uint8_t retcode;
uint8_t state; uint8_t state;
uint32_t config_ip; uint32_t config_ip;
struct Footer footer; struct Footer footer;
}; });
struct PACKED BlasterFireReq { PACKED( struct BlasterFireReq {
struct Header header; struct Header header;
struct { struct {
uint8_t type : 4; uint8_t type : 4;
uint8_t times : 4; uint8_t times : 4;
}; };
struct Footer footer; struct Footer footer;
}; });
struct PACKED BlasterFireResp { PACKED( struct BlasterFireResp {
struct Header header; struct Header header;
uint8_t retcode; uint8_t retcode;
struct Footer footer; struct Footer footer;
}; });
struct PACKED StreamCtrlReq { PACKED( struct StreamCtrlReq {
struct Header header; struct Header header;
uint8_t ctrl; uint8_t ctrl;
struct { struct {
@ -272,15 +278,15 @@ struct PACKED StreamCtrlReq {
}; };
uint8_t resolution; uint8_t resolution;
struct Footer footer; struct Footer footer;
}; });
struct PACKED StreamCtrlResp { PACKED( struct StreamCtrlResp {
struct Header header; struct Header header;
uint8_t retcode; uint8_t retcode;
struct Footer footer; struct Footer footer;
}; });
struct PACKED AddSubMsgReq { PACKED( struct AddSubMsgReq {
struct Header header; struct Header header;
uint8_t node_id; uint8_t node_id;
uint8_t msg_id; uint8_t msg_id;
@ -290,9 +296,9 @@ struct PACKED AddSubMsgReq {
uint64_t subject_uuid; uint64_t subject_uuid;
uint16_t freq; uint16_t freq;
struct Footer footer; struct Footer footer;
}; });
struct PACKED AddSubMsgResp { PACKED( struct AddSubMsgResp {
struct Header header; struct Header header;
uint8_t retcode; uint8_t retcode;
uint8_t pub_node_id; uint8_t pub_node_id;
@ -300,9 +306,9 @@ struct PACKED AddSubMsgResp {
uint8_t ack_msg_id; uint8_t ack_msg_id;
uint16_t ack_err_uid_data; uint16_t ack_err_uid_data;
struct Footer footer; struct Footer footer;
}; });
struct PACKED PushPeriodMsg { PACKED( struct PushPeriodMsg {
struct Header header; struct Header header;
uint8_t sub_mode; uint8_t sub_mode;
uint8_t msg_id; uint8_t msg_id;
@ -324,7 +330,7 @@ struct PACKED PushPeriodMsg {
} gimbal; } gimbal;
}; };
struct Footer footer; struct Footer footer;
}; });
union Request { union Request {
struct Header header; struct Header header;

@ -4,9 +4,26 @@
#include "connection.h" #include "connection.h"
#include <stdlib.h> #include <stdlib.h>
#ifdef _WIN32
bool StartupWSA() {
static bool wsastarted = false;
if (!wsastarted) {
WORD wVersionRequested;
WSADATA wsaData;
wVersionRequested = MAKEWORD(2, 2);
if (WSAStartup(wVersionRequested, &wsaData) == 0) {
wsastarted = true;
}
}
return wsastarted;
}
#else
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/select.h> #include <sys/select.h>
#include <netinet/in.h> #include <netinet/in.h>
#endif
#include <string.h> #include <string.h>
#include <fcntl.h> #include <fcntl.h>
#include <stdio.h> #include <stdio.h>
@ -106,6 +123,10 @@ connection_new(uint16_t source_port, uint32_t source_ip, uint16_t dest_port, uin
enum connection_error enum connection_error
connection_connect(struct Connection* conn) { connection_connect(struct Connection* conn) {
#ifdef _WIN32
StartupWSA();
#endif
// Request a UDP socket // Request a UDP socket
conn->sockfd = socket(AF_INET, SOCK_DGRAM, 0); conn->sockfd = socket(AF_INET, SOCK_DGRAM, 0);
@ -114,8 +135,13 @@ connection_connect(struct Connection* conn) {
return CONNECTION_LOCAL_BIND; return CONNECTION_LOCAL_BIND;
// Make the socket non-blocking // Make the socket non-blocking
#ifdef _WIN32
static unsigned long sock_opts = 1;
ioctlsocket(conn->sockfd, FIONBIO, &sock_opts); //Non-Blocking
#else
int flags = fcntl(conn->sockfd, F_GETFL); int flags = fcntl(conn->sockfd, F_GETFL);
fcntl(conn->sockfd, F_SETFL, flags | O_NONBLOCK); fcntl(conn->sockfd, F_SETFL, flags | O_NONBLOCK);
#endif
// 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.

Loading…
Cancel
Save