Define SDK magic string value as a shared constant in NetworkProtocol.cpp, use InitNetPacketHeader function to set up packets

This commit is contained in:
Adam Honse
2024-03-25 18:02:45 -05:00
parent 9215ae0514
commit cf4fd16b09
5 changed files with 53 additions and 187 deletions

View File

@@ -686,14 +686,7 @@ void NetworkClient::SendData_ClientString()
{
NetPacketHeader reply_hdr;
reply_hdr.pkt_magic[0] = 'O';
reply_hdr.pkt_magic[1] = 'R';
reply_hdr.pkt_magic[2] = 'G';
reply_hdr.pkt_magic[3] = 'B';
reply_hdr.pkt_dev_idx = 0;
reply_hdr.pkt_id = NET_PACKET_ID_SET_CLIENT_NAME;
reply_hdr.pkt_size = strlen(client_name.c_str()) + 1;
InitNetPacketHeader(&reply_hdr, 0, NET_PACKET_ID_SET_CLIENT_NAME, strlen(client_name.c_str()) + 1);
send(client_sock, (char *)&reply_hdr, sizeof(NetPacketHeader), MSG_NOSIGNAL);
send(client_sock, (char *)client_name.c_str(), reply_hdr.pkt_size, MSG_NOSIGNAL);
@@ -703,14 +696,7 @@ void NetworkClient::SendRequest_ControllerCount()
{
NetPacketHeader request_hdr;
request_hdr.pkt_magic[0] = 'O';
request_hdr.pkt_magic[1] = 'R';
request_hdr.pkt_magic[2] = 'G';
request_hdr.pkt_magic[3] = 'B';
request_hdr.pkt_dev_idx = 0;
request_hdr.pkt_id = NET_PACKET_ID_REQUEST_CONTROLLER_COUNT;
request_hdr.pkt_size = 0;
InitNetPacketHeader(&request_hdr, 0, NET_PACKET_ID_REQUEST_CONTROLLER_COUNT, 0);
send(client_sock, (char *)&request_hdr, sizeof(NetPacketHeader), MSG_NOSIGNAL);
}
@@ -722,10 +708,7 @@ void NetworkClient::SendRequest_ControllerData(unsigned int dev_idx)
controller_data_received = false;
request_hdr.pkt_magic[0] = 'O';
request_hdr.pkt_magic[1] = 'R';
request_hdr.pkt_magic[2] = 'G';
request_hdr.pkt_magic[3] = 'B';
memcpy(request_hdr.pkt_magic, openrgb_sdk_magic, sizeof(openrgb_sdk_magic));
request_hdr.pkt_dev_idx = dev_idx;
request_hdr.pkt_id = NET_PACKET_ID_REQUEST_CONTROLLER_DATA;
@@ -763,14 +746,7 @@ void NetworkClient::SendRequest_ProtocolVersion()
NetPacketHeader request_hdr;
unsigned int request_data;
request_hdr.pkt_magic[0] = 'O';
request_hdr.pkt_magic[1] = 'R';
request_hdr.pkt_magic[2] = 'G';
request_hdr.pkt_magic[3] = 'B';
request_hdr.pkt_dev_idx = 0;
request_hdr.pkt_id = NET_PACKET_ID_REQUEST_PROTOCOL_VERSION;
request_hdr.pkt_size = sizeof(unsigned int);
InitNetPacketHeader(&request_hdr, 0, NET_PACKET_ID_REQUEST_PROTOCOL_VERSION, sizeof(unsigned int));
request_data = OPENRGB_SDK_PROTOCOL_VERSION;
@@ -788,14 +764,7 @@ void NetworkClient::SendRequest_RGBController_ResizeZone(unsigned int dev_idx, i
NetPacketHeader request_hdr;
int request_data[2];
request_hdr.pkt_magic[0] = 'O';
request_hdr.pkt_magic[1] = 'R';
request_hdr.pkt_magic[2] = 'G';
request_hdr.pkt_magic[3] = 'B';
request_hdr.pkt_dev_idx = dev_idx;
request_hdr.pkt_id = NET_PACKET_ID_RGBCONTROLLER_RESIZEZONE;
request_hdr.pkt_size = sizeof(request_data);
InitNetPacketHeader(&request_hdr, dev_idx, NET_PACKET_ID_RGBCONTROLLER_RESIZEZONE, sizeof(request_data));
request_data[0] = zone;
request_data[1] = new_size;
@@ -813,14 +782,7 @@ void NetworkClient::SendRequest_RGBController_UpdateLEDs(unsigned int dev_idx, u
NetPacketHeader request_hdr;
request_hdr.pkt_magic[0] = 'O';
request_hdr.pkt_magic[1] = 'R';
request_hdr.pkt_magic[2] = 'G';
request_hdr.pkt_magic[3] = 'B';
request_hdr.pkt_dev_idx = dev_idx;
request_hdr.pkt_id = NET_PACKET_ID_RGBCONTROLLER_UPDATELEDS;
request_hdr.pkt_size = size;
InitNetPacketHeader(&request_hdr, dev_idx, NET_PACKET_ID_RGBCONTROLLER_UPDATELEDS, size);
send(client_sock, (char *)&request_hdr, sizeof(NetPacketHeader), MSG_NOSIGNAL);
send(client_sock, (char *)data, size, 0);
@@ -835,14 +797,7 @@ void NetworkClient::SendRequest_RGBController_UpdateZoneLEDs(unsigned int dev_id
NetPacketHeader request_hdr;
request_hdr.pkt_magic[0] = 'O';
request_hdr.pkt_magic[1] = 'R';
request_hdr.pkt_magic[2] = 'G';
request_hdr.pkt_magic[3] = 'B';
request_hdr.pkt_dev_idx = dev_idx;
request_hdr.pkt_id = NET_PACKET_ID_RGBCONTROLLER_UPDATEZONELEDS;
request_hdr.pkt_size = size;
InitNetPacketHeader(&request_hdr, dev_idx, NET_PACKET_ID_RGBCONTROLLER_UPDATEZONELEDS, size);
send(client_sock, (char *)&request_hdr, sizeof(NetPacketHeader), MSG_NOSIGNAL);
send(client_sock, (char *)data, size, MSG_NOSIGNAL);
@@ -857,14 +812,7 @@ void NetworkClient::SendRequest_RGBController_UpdateSingleLED(unsigned int dev_i
NetPacketHeader request_hdr;
request_hdr.pkt_magic[0] = 'O';
request_hdr.pkt_magic[1] = 'R';
request_hdr.pkt_magic[2] = 'G';
request_hdr.pkt_magic[3] = 'B';
request_hdr.pkt_dev_idx = dev_idx;
request_hdr.pkt_id = NET_PACKET_ID_RGBCONTROLLER_UPDATESINGLELED;
request_hdr.pkt_size = size;
InitNetPacketHeader(&request_hdr, dev_idx, NET_PACKET_ID_RGBCONTROLLER_UPDATESINGLELED, size);
send(client_sock, (char *)&request_hdr, sizeof(NetPacketHeader), MSG_NOSIGNAL);
send(client_sock, (char *)data, size, MSG_NOSIGNAL);
@@ -879,14 +827,7 @@ void NetworkClient::SendRequest_RGBController_SetCustomMode(unsigned int dev_idx
NetPacketHeader request_hdr;
request_hdr.pkt_magic[0] = 'O';
request_hdr.pkt_magic[1] = 'R';
request_hdr.pkt_magic[2] = 'G';
request_hdr.pkt_magic[3] = 'B';
request_hdr.pkt_dev_idx = dev_idx;
request_hdr.pkt_id = NET_PACKET_ID_RGBCONTROLLER_SETCUSTOMMODE;
request_hdr.pkt_size = 0;
InitNetPacketHeader(&request_hdr, dev_idx, NET_PACKET_ID_RGBCONTROLLER_SETCUSTOMMODE, 0);
send(client_sock, (char *)&request_hdr, sizeof(NetPacketHeader), MSG_NOSIGNAL);
}
@@ -900,14 +841,7 @@ void NetworkClient::SendRequest_RGBController_UpdateMode(unsigned int dev_idx, u
NetPacketHeader request_hdr;
request_hdr.pkt_magic[0] = 'O';
request_hdr.pkt_magic[1] = 'R';
request_hdr.pkt_magic[2] = 'G';
request_hdr.pkt_magic[3] = 'B';
request_hdr.pkt_dev_idx = dev_idx;
request_hdr.pkt_id = NET_PACKET_ID_RGBCONTROLLER_UPDATEMODE;
request_hdr.pkt_size = size;
InitNetPacketHeader(&request_hdr, dev_idx, NET_PACKET_ID_RGBCONTROLLER_UPDATEMODE, size);
send(client_sock, (char *)&request_hdr, sizeof(NetPacketHeader), MSG_NOSIGNAL);
send(client_sock, (char *)data, size, MSG_NOSIGNAL);
@@ -922,14 +856,7 @@ void NetworkClient::SendRequest_RGBController_SaveMode(unsigned int dev_idx, uns
NetPacketHeader request_hdr;
request_hdr.pkt_magic[0] = 'O';
request_hdr.pkt_magic[1] = 'R';
request_hdr.pkt_magic[2] = 'G';
request_hdr.pkt_magic[3] = 'B';
request_hdr.pkt_dev_idx = dev_idx;
request_hdr.pkt_id = NET_PACKET_ID_RGBCONTROLLER_SAVEMODE;
request_hdr.pkt_size = size;
InitNetPacketHeader(&request_hdr, dev_idx, NET_PACKET_ID_RGBCONTROLLER_SAVEMODE, size);
send(client_sock, (char *)&request_hdr, sizeof(NetPacketHeader), MSG_NOSIGNAL);
send(client_sock, (char *)data, size, MSG_NOSIGNAL);
@@ -939,14 +866,7 @@ void NetworkClient::SendRequest_LoadProfile(std::string profile_name)
{
NetPacketHeader reply_hdr;
reply_hdr.pkt_magic[0] = 'O';
reply_hdr.pkt_magic[1] = 'R';
reply_hdr.pkt_magic[2] = 'G';
reply_hdr.pkt_magic[3] = 'B';
reply_hdr.pkt_dev_idx = 0;
reply_hdr.pkt_id = NET_PACKET_ID_REQUEST_LOAD_PROFILE;
reply_hdr.pkt_size = strlen(profile_name.c_str()) + 1;
InitNetPacketHeader(&reply_hdr, 0, NET_PACKET_ID_REQUEST_LOAD_PROFILE, strlen(profile_name.c_str()) + 1);
send(client_sock, (char *)&reply_hdr, sizeof(NetPacketHeader), MSG_NOSIGNAL);
send(client_sock, (char *)profile_name.c_str(), reply_hdr.pkt_size, MSG_NOSIGNAL);
@@ -956,14 +876,7 @@ void NetworkClient::SendRequest_SaveProfile(std::string profile_name)
{
NetPacketHeader reply_hdr;
reply_hdr.pkt_magic[0] = 'O';
reply_hdr.pkt_magic[1] = 'R';
reply_hdr.pkt_magic[2] = 'G';
reply_hdr.pkt_magic[3] = 'B';
reply_hdr.pkt_dev_idx = 0;
reply_hdr.pkt_id = NET_PACKET_ID_REQUEST_SAVE_PROFILE;
reply_hdr.pkt_size = strlen(profile_name.c_str()) + 1;
InitNetPacketHeader(&reply_hdr, 0, NET_PACKET_ID_REQUEST_SAVE_PROFILE, strlen(profile_name.c_str()) + 1);
send(client_sock, (char *)&reply_hdr, sizeof(NetPacketHeader), MSG_NOSIGNAL);
send(client_sock, (char *)profile_name.c_str(), reply_hdr.pkt_size, MSG_NOSIGNAL);
@@ -973,14 +886,7 @@ void NetworkClient::SendRequest_DeleteProfile(std::string profile_name)
{
NetPacketHeader reply_hdr;
reply_hdr.pkt_magic[0] = 'O';
reply_hdr.pkt_magic[1] = 'R';
reply_hdr.pkt_magic[2] = 'G';
reply_hdr.pkt_magic[3] = 'B';
reply_hdr.pkt_dev_idx = 0;
reply_hdr.pkt_id = NET_PACKET_ID_REQUEST_DELETE_PROFILE;
reply_hdr.pkt_size = strlen(profile_name.c_str()) + 1;
InitNetPacketHeader(&reply_hdr, 0, NET_PACKET_ID_REQUEST_DELETE_PROFILE, strlen(profile_name.c_str()) + 1);
send(client_sock, (char *)&reply_hdr, sizeof(NetPacketHeader), MSG_NOSIGNAL);
send(client_sock, (char *)profile_name.c_str(), reply_hdr.pkt_size, MSG_NOSIGNAL);
@@ -990,14 +896,7 @@ void NetworkClient::SendRequest_GetProfileList()
{
NetPacketHeader reply_hdr;
reply_hdr.pkt_magic[0] = 'O';
reply_hdr.pkt_magic[1] = 'R';
reply_hdr.pkt_magic[2] = 'G';
reply_hdr.pkt_magic[3] = 'B';
reply_hdr.pkt_dev_idx = 0;
reply_hdr.pkt_id = NET_PACKET_ID_REQUEST_PROFILE_LIST;
reply_hdr.pkt_size = 0;
InitNetPacketHeader(&reply_hdr, 0, NET_PACKET_ID_REQUEST_PROFILE_LIST, 0);
send(client_sock, (char *)&reply_hdr, sizeof(NetPacketHeader), MSG_NOSIGNAL);
}

View File

@@ -1,20 +1,22 @@
#include "NetworkProtocol.h"
#include <cstring>
NetPacketHeader * InitNetPacketHeader
/*-----------------------------------------------------*\
| OpenRGB SDK Magic Value "ORGB" |
\*-----------------------------------------------------*/
const char * openrgb_sdk_magic = "ORGB";
void InitNetPacketHeader
(
NetPacketHeader * pkt_hdr,
unsigned int pkt_dev_idx,
unsigned int pkt_id,
unsigned int pkt_size
)
{
NetPacketHeader * new_header = new NetPacketHeader;
memcpy(pkt_hdr->pkt_magic, openrgb_sdk_magic, sizeof(openrgb_sdk_magic));
new_header->pkt_magic[0] = 'O';
new_header->pkt_magic[1] = 'R';
new_header->pkt_magic[2] = 'G';
new_header->pkt_magic[3] = 'B';
new_header->pkt_dev_idx = pkt_dev_idx;
new_header->pkt_id = pkt_id;
new_header->pkt_size = pkt_size;
}
pkt_hdr->pkt_dev_idx = pkt_dev_idx;
pkt_hdr->pkt_id = pkt_id;
pkt_hdr->pkt_size = pkt_size;
}

View File

@@ -30,6 +30,11 @@
\*-----------------------------------------------------*/
#define OPENRGB_SDK_PORT 6742
/*-----------------------------------------------------*\
| OpenRGB SDK Magic Value "ORGB" |
\*-----------------------------------------------------*/
extern const char * openrgb_sdk_magic;
typedef struct NetPacketHeader
{
char pkt_magic[4]; /* Magic value "ORGB" identifies beginning of packet */
@@ -73,3 +78,11 @@ enum
NET_PACKET_ID_RGBCONTROLLER_UPDATEMODE = 1101, /* RGBController::UpdateMode() */
NET_PACKET_ID_RGBCONTROLLER_SAVEMODE = 1102, /* RGBController::SaveMode() */
};
void InitNetPacketHeader
(
NetPacketHeader * pkt_hdr,
unsigned int pkt_dev_idx,
unsigned int pkt_id,
unsigned int pkt_size
);

View File

@@ -966,16 +966,9 @@ void NetworkServer::SendReply_ControllerCount(SOCKET client_sock)
NetPacketHeader reply_hdr;
unsigned int reply_data;
reply_hdr.pkt_magic[0] = 'O';
reply_hdr.pkt_magic[1] = 'R';
reply_hdr.pkt_magic[2] = 'G';
reply_hdr.pkt_magic[3] = 'B';
InitNetPacketHeader(&reply_hdr, 0, NET_PACKET_ID_REQUEST_CONTROLLER_COUNT, sizeof(unsigned int));
reply_hdr.pkt_dev_idx = 0;
reply_hdr.pkt_id = NET_PACKET_ID_REQUEST_CONTROLLER_COUNT;
reply_hdr.pkt_size = sizeof(unsigned int);
reply_data = controllers.size();
reply_data = controllers.size();
send(client_sock, (const char *)&reply_hdr, sizeof(NetPacketHeader), 0);
send(client_sock, (const char *)&reply_data, sizeof(unsigned int), 0);
@@ -991,14 +984,7 @@ void NetworkServer::SendReply_ControllerData(SOCKET client_sock, unsigned int de
memcpy(&reply_size, reply_data, sizeof(reply_size));
reply_hdr.pkt_magic[0] = 'O';
reply_hdr.pkt_magic[1] = 'R';
reply_hdr.pkt_magic[2] = 'G';
reply_hdr.pkt_magic[3] = 'B';
reply_hdr.pkt_dev_idx = dev_idx;
reply_hdr.pkt_id = NET_PACKET_ID_REQUEST_CONTROLLER_DATA;
reply_hdr.pkt_size = reply_size;
InitNetPacketHeader(&reply_hdr, dev_idx, NET_PACKET_ID_REQUEST_CONTROLLER_DATA, reply_size);
send(client_sock, (const char *)&reply_hdr, sizeof(NetPacketHeader), 0);
send(client_sock, (const char *)reply_data, reply_size, 0);
@@ -1012,16 +998,9 @@ void NetworkServer::SendReply_ProtocolVersion(SOCKET client_sock)
NetPacketHeader reply_hdr;
unsigned int reply_data;
reply_hdr.pkt_magic[0] = 'O';
reply_hdr.pkt_magic[1] = 'R';
reply_hdr.pkt_magic[2] = 'G';
reply_hdr.pkt_magic[3] = 'B';
InitNetPacketHeader(&reply_hdr, 0, NET_PACKET_ID_REQUEST_PROTOCOL_VERSION, sizeof(unsigned int));
reply_hdr.pkt_dev_idx = 0;
reply_hdr.pkt_id = NET_PACKET_ID_REQUEST_PROTOCOL_VERSION;
reply_hdr.pkt_size = sizeof(unsigned int);
reply_data = OPENRGB_SDK_PROTOCOL_VERSION;
reply_data = OPENRGB_SDK_PROTOCOL_VERSION;
send(client_sock, (const char *)&reply_hdr, sizeof(NetPacketHeader), 0);
send(client_sock, (const char *)&reply_data, sizeof(unsigned int), 0);
@@ -1031,14 +1010,7 @@ void NetworkServer::SendRequest_DeviceListChanged(SOCKET client_sock)
{
NetPacketHeader pkt_hdr;
pkt_hdr.pkt_magic[0] = 'O';
pkt_hdr.pkt_magic[1] = 'R';
pkt_hdr.pkt_magic[2] = 'G';
pkt_hdr.pkt_magic[3] = 'B';
pkt_hdr.pkt_dev_idx = 0;
pkt_hdr.pkt_id = NET_PACKET_ID_DEVICE_LIST_UPDATED;
pkt_hdr.pkt_size = 0;
InitNetPacketHeader(&pkt_hdr, 0, NET_PACKET_ID_DEVICE_LIST_UPDATED, 0);
send(client_sock, (char *)&pkt_hdr, sizeof(NetPacketHeader), 0);
}
@@ -1056,14 +1028,7 @@ void NetworkServer::SendReply_ProfileList(SOCKET client_sock)
memcpy(&reply_size, reply_data, sizeof(reply_size));
reply_hdr.pkt_magic[0] = 'O';
reply_hdr.pkt_magic[1] = 'R';
reply_hdr.pkt_magic[2] = 'G';
reply_hdr.pkt_magic[3] = 'B';
reply_hdr.pkt_dev_idx = 0;
reply_hdr.pkt_id = NET_PACKET_ID_REQUEST_PROFILE_LIST;
reply_hdr.pkt_size = reply_size;
InitNetPacketHeader(&reply_hdr, 0, NET_PACKET_ID_REQUEST_PROFILE_LIST, reply_size);
send(client_sock, (const char *)&reply_hdr, sizeof(NetPacketHeader), 0);
send(client_sock, (const char *)reply_data, reply_size, 0);
@@ -1161,14 +1126,7 @@ void NetworkServer::SendReply_PluginList(SOCKET client_sock)
memcpy(&reply_size, data_buf, sizeof(reply_size));
reply_hdr.pkt_magic[0] = 'O';
reply_hdr.pkt_magic[1] = 'R';
reply_hdr.pkt_magic[2] = 'G';
reply_hdr.pkt_magic[3] = 'B';
reply_hdr.pkt_dev_idx = 0;
reply_hdr.pkt_id = NET_PACKET_ID_REQUEST_PLUGIN_LIST;
reply_hdr.pkt_size = reply_size;
InitNetPacketHeader(&reply_hdr, 0, NET_PACKET_ID_REQUEST_PLUGIN_LIST, reply_size);
send(client_sock, (const char *)&reply_hdr, sizeof(NetPacketHeader), 0);
send(client_sock, (const char *)data_buf, reply_size, 0);
@@ -1180,14 +1138,7 @@ void NetworkServer::SendReply_PluginSpecific(SOCKET client_sock, unsigned int pk
{
NetPacketHeader reply_hdr;
reply_hdr.pkt_magic[0] = 'O';
reply_hdr.pkt_magic[1] = 'R';
reply_hdr.pkt_magic[2] = 'G';
reply_hdr.pkt_magic[3] = 'B';
reply_hdr.pkt_dev_idx = 0;
reply_hdr.pkt_id = NET_PACKET_ID_PLUGIN_SPECIFIC;
reply_hdr.pkt_size = data_size + sizeof(pkt_type);
InitNetPacketHeader(&reply_hdr, 0, NET_PACKET_ID_PLUGIN_SPECIFIC, data_size + sizeof(pkt_type));
send(client_sock, (const char *)&reply_hdr, sizeof(NetPacketHeader), 0);
send(client_sock, (const char *)&pkt_type, sizeof(pkt_type), 0);

View File

@@ -196,6 +196,7 @@ SOURCES +=
cli.cpp \
LogManager.cpp \
NetworkClient.cpp \
NetworkProtocol.cpp \
NetworkServer.cpp \
PluginManager.cpp \
ProfileManager.cpp \
@@ -681,7 +682,7 @@ macx {
qt/macutils.h \
HEADERS -= $$CONTROLLER_H_WIN
SOURCES += \
dependencies/hueplusplus-1.0.0/src/LinHttpHandler.cpp \
serial_port/find_usb_serial_port_linux.cpp \
@@ -689,7 +690,7 @@ macx {
qt/macutils.mm \
SOURCES -= $$CONTROLLER_CPP_WIN
# Use mbedtls v2 instead of latest
MBEDTLS_PREFIX = $$system(brew --prefix mbedtls@2)