From e2bc1003e6097dc79caf085aead257baab9d9c06 Mon Sep 17 00:00:00 2001 From: morg Date: Tue, 2 Feb 2021 21:15:05 +0100 Subject: [PATCH] Add profile management to SDK Commit amended for code style by Adam Honse --- NetworkClient.cpp | 107 +++++++++++++++++++++++++++++++++++++++++++++ NetworkClient.h | 8 ++++ NetworkProtocol.h | 6 +++ NetworkServer.cpp | 64 +++++++++++++++++++++++++++ NetworkServer.h | 1 + ProfileManager.cpp | 53 ++++++++++++++++++++++ ProfileManager.h | 2 + 7 files changed, 241 insertions(+) diff --git a/NetworkClient.cpp b/NetworkClient.cpp index 3547b6ad..268bd08e 100644 --- a/NetworkClient.cpp +++ b/NetworkClient.cpp @@ -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 * NetworkClient::ProcessReply_ProfileList(unsigned int data_size, char * data) +{ + std::vector * profile_list; + + if(data_size > 0) + { + profile_list = new std::vector(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(0); + } + + return profile_list; +} diff --git a/NetworkClient.h b/NetworkClient.h index 5a18ef86..1870d7ed 100644 --- a/NetworkClient.h +++ b/NetworkClient.h @@ -67,6 +67,14 @@ public: void SendRequest_RGBController_UpdateMode(unsigned int dev_idx, unsigned char * data, unsigned int size); + + std::vector * 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 server_controllers; std::mutex ControllerListMutex; diff --git a/NetworkProtocol.h b/NetworkProtocol.h index ce882f76..12b477be 100644 --- a/NetworkProtocol.h +++ b/NetworkProtocol.h @@ -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 | \*----------------------------------------------------------------------------------------------------------*/ diff --git a/NetworkServer.cpp b/NetworkServer.cpp index ff5eb45e..1c779ad5 100644 --- a/NetworkServer.cpp +++ b/NetworkServer.cpp @@ -7,6 +7,7 @@ \*-----------------------------------------*/ #include "NetworkServer.h" +#include "ResourceManager.h" #include #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); +} + diff --git a/NetworkServer.h b/NetworkServer.h index a23d9fa6..abfd6415 100644 --- a/NetworkServer.h +++ b/NetworkServer.h @@ -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; diff --git a/ProfileManager.cpp b/ProfileManager.cpp index 1f578ddc..45aeb908 100644 --- a/ProfileManager.cpp +++ b/ProfileManager.cpp @@ -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); +} diff --git a/ProfileManager.h b/ProfileManager.h index 3769698c..a7d5e80b 100644 --- a/ProfileManager.h +++ b/ProfileManager.h @@ -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 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 profile_list;