Add profile management to SDK

Commit amended for code style by Adam Honse <calcprogrammer1@gmail.com>
This commit is contained in:
morg
2021-02-02 21:15:05 +01:00
committed by Adam Honse
parent 3026feeebe
commit e2bc1003e6
7 changed files with 241 additions and 0 deletions

View File

@@ -828,3 +828,110 @@ void NetworkClient::SendRequest_RGBController_UpdateMode(unsigned int dev_idx, u
send(client_sock, (char *)&request_hdr, sizeof(NetPacketHeader), MSG_NOSIGNAL);
send(client_sock, (char *)data, size, MSG_NOSIGNAL);
}
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 = profile_name.size();
send(client_sock, (char *)&reply_hdr, sizeof(NetPacketHeader), MSG_NOSIGNAL);
send(client_sock, (char *)profile_name.c_str(), reply_hdr.pkt_size, MSG_NOSIGNAL);
}
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 = profile_name.size();
send(client_sock, (char *)&reply_hdr, sizeof(NetPacketHeader), MSG_NOSIGNAL);
send(client_sock, (char *)profile_name.c_str(), reply_hdr.pkt_size, MSG_NOSIGNAL);
}
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 = profile_name.size();
send(client_sock, (char *)&reply_hdr, sizeof(NetPacketHeader), MSG_NOSIGNAL);
send(client_sock, (char *)profile_name.c_str(), reply_hdr.pkt_size, MSG_NOSIGNAL);
}
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;
send(client_sock, (char *)&reply_hdr, sizeof(NetPacketHeader), MSG_NOSIGNAL);
}
std::vector<std::string> * NetworkClient::ProcessReply_ProfileList(unsigned int data_size, char * data)
{
std::vector<std::string> * profile_list;
if(data_size > 0)
{
profile_list = new std::vector<std::string>(data_size);
// skip 4 first bytes (data length, unused)
unsigned short data_ptr = sizeof(unsigned short);
unsigned short num_profile;
memcpy(&num_profile, data, sizeof(unsigned short));
data_ptr += sizeof(unsigned short);
for(int i = 0; i < num_profile; i++)
{
unsigned short name_len;
memcpy(&name_len, data, sizeof(unsigned short));
data_ptr += sizeof(unsigned short);
char * profile_name = new char[name_len];
memcpy(&profile_name, data, name_len);
profile_list->push_back(profile_name);
data_ptr += name_len;
}
server_controller_count_received = true;
}
else
{
profile_list = new std::vector<std::string>(0);
}
return profile_list;
}

View File

@@ -67,6 +67,14 @@ public:
void SendRequest_RGBController_UpdateMode(unsigned int dev_idx, unsigned char * data, unsigned int size);
std::vector<std::string> * ProcessReply_ProfileList(unsigned int data_size, char * data);
void SendRequest_GetProfileList();
void SendRequest_LoadProfile(std::string profile_name);
void SendRequest_SaveProfile(std::string profile_name);
void SendRequest_DeleteProfile(std::string profile_name);
std::vector<RGBController *> server_controllers;
std::mutex ControllerListMutex;

View File

@@ -43,6 +43,12 @@ enum
NET_PACKET_ID_SET_CLIENT_NAME = 50, /* Send client name string to server */
NET_PACKET_ID_DEVICE_LIST_UPDATED = 100, /* Indicate to clients that device list has updated */
NET_PACKET_ID_REQUEST_PROFILE_LIST = 150, /* Request profile list */
NET_PACKET_ID_REQUEST_SAVE_PROFILE = 151, /* Save current configuration in a new profile */
NET_PACKET_ID_REQUEST_LOAD_PROFILE = 152, /* Load a given profile */
NET_PACKET_ID_REQUEST_DELETE_PROFILE = 153, /* Load a given profile */
/*----------------------------------------------------------------------------------------------------------*\
| RGBController class functions |
\*----------------------------------------------------------------------------------------------------------*/

View File

@@ -7,6 +7,7 @@
\*-----------------------------------------*/
#include "NetworkServer.h"
#include "ResourceManager.h"
#include <cstring>
#ifndef WIN32
@@ -612,6 +613,45 @@ void NetworkServer::ListenThreadFunction(NetworkClientInfo * client_info)
controllers[header.pkt_dev_idx]->UpdateMode();
}
break;
case NET_PACKET_ID_REQUEST_PROFILE_LIST:
SendReply_ProfileList(client_sock);
break;
case NET_PACKET_ID_REQUEST_SAVE_PROFILE:
if(data == NULL)
{
break;
}
ResourceManager::get()->GetProfileManager()->SaveProfile(data);
break;
case NET_PACKET_ID_REQUEST_LOAD_PROFILE:
if(data == NULL)
{
break;
}
ResourceManager::get()->GetProfileManager()->LoadProfile(data);
for(RGBController* controller : ResourceManager::get()->GetRGBControllers())
{
controller->UpdateLEDs();
}
break;
case NET_PACKET_ID_REQUEST_DELETE_PROFILE:
if(data == NULL)
{
break;
}
ResourceManager::get()->GetProfileManager()->DeleteProfile(data);
break;
}
delete[] data;
@@ -772,3 +812,27 @@ void NetworkServer::SendRequest_DeviceListChanged(SOCKET client_sock)
send(client_sock, (char *)&pkt_hdr, sizeof(NetPacketHeader), 0);
}
void NetworkServer::SendReply_ProfileList(SOCKET client_sock)
{
ProfileManager* profile_manager = ResourceManager::get()->GetProfileManager();
NetPacketHeader reply_hdr;
unsigned char *reply_data = profile_manager->GetProfileListDescription();
unsigned int reply_size;
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;
send(client_sock, (const char *)&reply_hdr, sizeof(NetPacketHeader), 0);
send(client_sock, (const char *)reply_data, reply_size, 0);
}

View File

@@ -60,6 +60,7 @@ public:
void SendReply_ProtocolVersion(SOCKET client_sock);
void SendRequest_DeviceListChanged(SOCKET client_sock);
void SendReply_ProfileList(SOCKET client_sock);
protected:
unsigned short port_num;

View File

@@ -381,3 +381,56 @@ void ProfileManager::UpdateProfileList()
}
}
}
unsigned char * ProfileManager::GetProfileListDescription()
{
unsigned int data_ptr = 0;
unsigned int data_size = 0;
/*---------------------------------------------------------*\
| Calculate data size |
\*---------------------------------------------------------*/
unsigned short num_profiles = profile_list.size();
data_size += sizeof(data_size);
data_size += sizeof(num_profiles);
for(unsigned int i = 0; i < num_profiles; i++)
{
data_size += sizeof (unsigned short);
data_size += strlen(profile_list[i].c_str());
}
/*---------------------------------------------------------*\
| Create data buffer |
\*---------------------------------------------------------*/
unsigned char *data_buf = new unsigned char[data_size];
/*---------------------------------------------------------*\
| Copy in data size |
\*---------------------------------------------------------*/
memcpy(&data_buf[data_ptr], &data_size, sizeof(data_size));
data_ptr += sizeof(data_size);
/*---------------------------------------------------------*\
| Copy in num_profiles |
\*---------------------------------------------------------*/
memcpy(&data_buf[data_ptr], &num_profiles, sizeof(num_profiles));
data_ptr += sizeof(num_profiles);
/*---------------------------------------------------------*\
| Copy in profile names (size+data) |
\*---------------------------------------------------------*/
for(unsigned int i = 0; i < num_profiles; i++)
{
unsigned short name_len = strlen(profile_list[i].c_str());
memcpy(&data_buf[data_ptr], &name_len, sizeof(name_len));
data_ptr += sizeof(name_len);
strcpy((char *)&data_buf[data_ptr], profile_list[i].c_str());
data_ptr += name_len;
}
return(data_buf);
}

View File

@@ -9,6 +9,7 @@ public:
virtual bool LoadProfile(std::string profile_name) = 0;
virtual bool LoadSizeFromProfile(std::string profile_name) = 0;
virtual void DeleteProfile(std::string profile_name) = 0;
virtual unsigned char * GetProfileListDescription() = 0;
std::vector<std::string> profile_list;
@@ -38,6 +39,7 @@ public:
bool LoadProfile(std::string profile_name);
bool LoadSizeFromProfile(std::string profile_name);
void DeleteProfile(std::string profile_name);
unsigned char * GetProfileListDescription();
std::vector<std::string> profile_list;