From 450f4385387876e145dd7b6589e21a1dd2d9732c Mon Sep 17 00:00:00 2001 From: Adam Honse Date: Sat, 25 Apr 2020 16:12:19 -0500 Subject: [PATCH] Send mode data block when updating mode --- NetworkClient.cpp | 7 +- NetworkClient.h | 2 +- NetworkServer.cpp | 9 +- RGBController/RGBController.cpp | 245 ++++++++++++++++++++++++ RGBController/RGBController.h | 3 + RGBController/RGBController_Network.cpp | 11 +- 6 files changed, 269 insertions(+), 8 deletions(-) diff --git a/NetworkClient.cpp b/NetworkClient.cpp index 8dab3529..e442c015 100644 --- a/NetworkClient.cpp +++ b/NetworkClient.cpp @@ -192,7 +192,7 @@ void NetworkClient::ListenThread() do { - bytes_read += port.tcp_listen(&data[bytes_read], 128); + bytes_read += port.tcp_listen(&data[bytes_read], header.pkt_size - bytes_read); } while (bytes_read < header.pkt_size); } @@ -361,7 +361,7 @@ void NetworkClient::SendRequest_RGBController_SetCustomMode(unsigned int dev_idx port.tcp_client_write((char *)&reply_hdr, sizeof(NetPacketHeader)); } -void NetworkClient::SendRequest_RGBController_UpdateMode(unsigned int dev_idx) +void NetworkClient::SendRequest_RGBController_UpdateMode(unsigned int dev_idx, unsigned char * data, unsigned int size) { NetPacketHeader reply_hdr; @@ -372,7 +372,8 @@ void NetworkClient::SendRequest_RGBController_UpdateMode(unsigned int dev_idx) reply_hdr.pkt_dev_idx = dev_idx; reply_hdr.pkt_id = NET_PACKET_ID_RGBCONTROLLER_UPDATEMODE; - reply_hdr.pkt_size = 0; + reply_hdr.pkt_size = size; port.tcp_client_write((char *)&reply_hdr, sizeof(NetPacketHeader)); + port.tcp_client_write((char *)data, size); } \ No newline at end of file diff --git a/NetworkClient.h b/NetworkClient.h index e5938303..beb056c1 100644 --- a/NetworkClient.h +++ b/NetworkClient.h @@ -26,7 +26,7 @@ public: void SendRequest_RGBController_SetCustomMode(unsigned int dev_idx); - void SendRequest_RGBController_UpdateMode(unsigned int dev_idx); + void SendRequest_RGBController_UpdateMode(unsigned int dev_idx, unsigned char * data, unsigned int size); protected: std::vector& controllers; diff --git a/NetworkServer.cpp b/NetworkServer.cpp index 240d3c43..af3fdc61 100644 --- a/NetworkServer.cpp +++ b/NetworkServer.cpp @@ -93,8 +93,8 @@ void NetworkServer::ListenThread(SOCKET * client_sock) while(1) { NetPacketHeader header; - char * data = NULL; int bytes_read = 0; + char * data = NULL; //Read first byte of magic do @@ -162,7 +162,7 @@ void NetworkServer::ListenThread(SOCKET * client_sock) do { - bytes_read += read(*client_sock, &data[bytes_read], 128); + bytes_read += read(*client_sock, &data[bytes_read], header.pkt_size - bytes_read); } while (bytes_read < header.pkt_size); } @@ -246,10 +246,13 @@ void NetworkServer::ListenThread(SOCKET * client_sock) if(header.pkt_dev_idx < controllers.size()) { + controllers[header.pkt_dev_idx]->SetModeDescription((unsigned char *)data); controllers[header.pkt_dev_idx]->UpdateMode(); } break; } + + delete[] data; } } @@ -295,4 +298,4 @@ void NetworkServer::SendReply_ControllerData(SOCKET * client_sock, unsigned int write(*client_sock, &reply_hdr, sizeof(NetPacketHeader)); write(*client_sock, reply_data, reply_size); } -} \ No newline at end of file +} diff --git a/RGBController/RGBController.cpp b/RGBController/RGBController.cpp index 0533439c..d2943a1c 100644 --- a/RGBController/RGBController.cpp +++ b/RGBController/RGBController.cpp @@ -623,6 +623,251 @@ void RGBController::ReadDeviceDescription(unsigned char* data_buf) } } +unsigned char * RGBController::GetModeDescription(int mode) +{ + unsigned int data_ptr = 0; + unsigned int data_size = 0; + + unsigned short mode_name_len; + unsigned short mode_num_colors; + + /*---------------------------------------------------------*\ + | Calculate data size | + \*---------------------------------------------------------*/ + mode_name_len = strlen(modes[mode].name.c_str()) + 1; + mode_num_colors = modes[mode].colors.size(); + + data_size += sizeof(data_size); + data_size += sizeof(mode); + data_size += sizeof(mode_name_len); + data_size += mode_name_len; + data_size += sizeof(modes[mode].value); + data_size += sizeof(modes[mode].flags); + data_size += sizeof(modes[mode].speed_min); + data_size += sizeof(modes[mode].speed_max); + data_size += sizeof(modes[mode].colors_min); + data_size += sizeof(modes[mode].colors_max); + data_size += sizeof(modes[mode].speed); + data_size += sizeof(modes[mode].direction); + data_size += sizeof(modes[mode].color_mode); + data_size += sizeof(mode_num_colors); + data_size += (mode_num_colors * sizeof(RGBColor)); + + printf( "Data size: %d \r\n", data_size ); + + /*---------------------------------------------------------*\ + | 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 mode index | + \*---------------------------------------------------------*/ + memcpy(&data_buf[data_ptr], &mode, sizeof(int)); + data_ptr += sizeof(int); + + /*---------------------------------------------------------*\ + | Copy in mode name (size+data) | + \*---------------------------------------------------------*/ + memcpy(&data_buf[data_ptr], &mode_name_len, sizeof(unsigned short)); + data_ptr += sizeof(unsigned short); + + strcpy((char *)&data_buf[data_ptr], modes[mode].name.c_str()); + data_ptr += mode_name_len; + + /*---------------------------------------------------------*\ + | Copy in mode value (data) | + \*---------------------------------------------------------*/ + memcpy(&data_buf[data_ptr], &modes[mode].value, sizeof(modes[mode].value)); + data_ptr += sizeof(modes[mode].value); + + /*---------------------------------------------------------*\ + | Copy in mode flags (data) | + \*---------------------------------------------------------*/ + memcpy(&data_buf[data_ptr], &modes[mode].flags, sizeof(modes[mode].flags)); + data_ptr += sizeof(modes[mode].flags); + + /*---------------------------------------------------------*\ + | Copy in mode speed_min (data) | + \*---------------------------------------------------------*/ + memcpy(&data_buf[data_ptr], &modes[mode].speed_min, sizeof(modes[mode].speed_min)); + data_ptr += sizeof(modes[mode].speed_min); + + /*---------------------------------------------------------*\ + | Copy in mode speed_max (data) | + \*---------------------------------------------------------*/ + memcpy(&data_buf[data_ptr], &modes[mode].speed_max, sizeof(modes[mode].speed_max)); + data_ptr += sizeof(modes[mode].speed_max); + + /*---------------------------------------------------------*\ + | Copy in mode colors_min (data) | + \*---------------------------------------------------------*/ + memcpy(&data_buf[data_ptr], &modes[mode].colors_min, sizeof(modes[mode].colors_min)); + data_ptr += sizeof(modes[mode].colors_min); + + /*---------------------------------------------------------*\ + | Copy in mode colors_max (data) | + \*---------------------------------------------------------*/ + memcpy(&data_buf[data_ptr], &modes[mode].colors_max, sizeof(modes[mode].colors_max)); + data_ptr += sizeof(modes[mode].colors_max); + + /*---------------------------------------------------------*\ + | Copy in mode speed (data) | + \*---------------------------------------------------------*/ + memcpy(&data_buf[data_ptr], &modes[mode].speed, sizeof(modes[mode].speed)); + data_ptr += sizeof(modes[mode].speed); + + /*---------------------------------------------------------*\ + | Copy in mode direction (data) | + \*---------------------------------------------------------*/ + memcpy(&data_buf[data_ptr], &modes[mode].direction, sizeof(modes[mode].direction)); + data_ptr += sizeof(modes[mode].direction); + + /*---------------------------------------------------------*\ + | Copy in mode color_mode (data) | + \*---------------------------------------------------------*/ + memcpy(&data_buf[data_ptr], &modes[mode].color_mode, sizeof(modes[mode].color_mode)); + data_ptr += sizeof(modes[mode].color_mode); + + /*---------------------------------------------------------*\ + | Copy in mode number of colors | + \*---------------------------------------------------------*/ + memcpy(&data_buf[data_ptr], &mode_num_colors, sizeof(unsigned short)); + data_ptr += sizeof(unsigned short); + + /*---------------------------------------------------------*\ + | Copy in mode mode colors | + \*---------------------------------------------------------*/ + for(int color_index = 0; color_index < mode_num_colors; color_index++) + { + /*---------------------------------------------------------*\ + | Copy in color (data) | + \*---------------------------------------------------------*/ + memcpy(&data_buf[data_ptr], &modes[mode].colors[color_index], sizeof(modes[mode].colors[color_index])); + data_ptr += sizeof(modes[mode].colors[color_index]); + } + + printf( "end data_ptr %d\r\n", data_ptr ); + return(data_buf); +} + +void RGBController::SetModeDescription(unsigned char* data_buf) +{ + int mode_idx; + unsigned int data_ptr = sizeof(unsigned int); + + /*---------------------------------------------------------*\ + | Copy in mode index | + \*---------------------------------------------------------*/ + memcpy(&mode_idx, &data_buf[data_ptr], sizeof(int)); + data_ptr += sizeof(int); + + /*---------------------------------------------------------*\ + | Get pointer to target mode | + \*---------------------------------------------------------*/ + mode * new_mode = &modes[mode_idx]; + + /*---------------------------------------------------------*\ + | Set active mode to the new mode | + \*---------------------------------------------------------*/ + active_mode = mode_idx; + + /*---------------------------------------------------------*\ + | Copy in mode name (size+data) | + \*---------------------------------------------------------*/ + unsigned short modename_len; + memcpy(&modename_len, &data_buf[data_ptr], sizeof(unsigned short)); + data_ptr += sizeof(unsigned short); + + new_mode->name = (char *)&data_buf[data_ptr]; + data_ptr += modename_len; + + /*---------------------------------------------------------*\ + | Copy in mode value (data) | + \*---------------------------------------------------------*/ + memcpy(&new_mode->value, &data_buf[data_ptr], sizeof(new_mode->value)); + data_ptr += sizeof(new_mode->value); + + /*---------------------------------------------------------*\ + | Copy in mode flags (data) | + \*---------------------------------------------------------*/ + memcpy(&new_mode->flags, &data_buf[data_ptr], sizeof(new_mode->flags)); + data_ptr += sizeof(new_mode->flags); + + /*---------------------------------------------------------*\ + | Copy in mode speed_min (data) | + \*---------------------------------------------------------*/ + memcpy(&new_mode->speed_min, &data_buf[data_ptr], sizeof(new_mode->speed_min)); + data_ptr += sizeof(new_mode->speed_min); + + /*---------------------------------------------------------*\ + | Copy in mode speed_max (data) | + \*---------------------------------------------------------*/ + memcpy(&new_mode->speed_max, &data_buf[data_ptr], sizeof(new_mode->speed_max)); + data_ptr += sizeof(new_mode->speed_max); + + /*---------------------------------------------------------*\ + | Copy in mode colors_min (data) | + \*---------------------------------------------------------*/ + memcpy(&new_mode->colors_min, &data_buf[data_ptr], sizeof(new_mode->colors_min)); + data_ptr += sizeof(new_mode->colors_min); + + /*---------------------------------------------------------*\ + | Copy in mode colors_max (data) | + \*---------------------------------------------------------*/ + memcpy(&new_mode->colors_max, &data_buf[data_ptr], sizeof(new_mode->colors_max)); + data_ptr += sizeof(new_mode->colors_max); + + /*---------------------------------------------------------*\ + | Copy in mode speed (data) | + \*---------------------------------------------------------*/ + memcpy(&new_mode->speed, &data_buf[data_ptr], sizeof(new_mode->speed)); + data_ptr += sizeof(new_mode->speed); + + /*---------------------------------------------------------*\ + | Copy in mode direction (data) | + \*---------------------------------------------------------*/ + memcpy(&new_mode->direction, &data_buf[data_ptr], sizeof(new_mode->direction)); + data_ptr += sizeof(new_mode->direction); + + /*---------------------------------------------------------*\ + | Copy in mode color_mode (data) | + \*---------------------------------------------------------*/ + memcpy(&new_mode->color_mode, &data_buf[data_ptr], sizeof(new_mode->color_mode)); + data_ptr += sizeof(new_mode->color_mode); + + /*---------------------------------------------------------*\ + | Copy in mode number of colors | + \*---------------------------------------------------------*/ + unsigned short mode_num_colors; + memcpy(&mode_num_colors, &data_buf[data_ptr], sizeof(unsigned short)); + data_ptr += sizeof(unsigned short); + + /*---------------------------------------------------------*\ + | Copy in mode mode colors | + \*---------------------------------------------------------*/ + new_mode->colors.clear(); + for(int color_index = 0; color_index < mode_num_colors; color_index++) + { + /*---------------------------------------------------------*\ + | Copy in color (data) | + \*---------------------------------------------------------*/ + RGBColor new_color; + memcpy(&new_color, &data_buf[data_ptr], sizeof(RGBColor)); + data_ptr += sizeof(RGBColor); + + new_mode->colors.push_back(new_color); + } + + printf("read data ptr %d\r\n", data_ptr); +} + void RGBController::SetupColors() { unsigned int total_led_count; diff --git a/RGBController/RGBController.h b/RGBController/RGBController.h index 5ab939c7..fbcdc036 100644 --- a/RGBController/RGBController.h +++ b/RGBController/RGBController.h @@ -160,6 +160,9 @@ public: unsigned char * GetDeviceDescription(); void ReadDeviceDescription(unsigned char* data_buf); + unsigned char * GetModeDescription(int mode); + void SetModeDescription(unsigned char* data_buf); + /*---------------------------------------------------------*\ | Functions to be implemented in device implementation | \*---------------------------------------------------------*/ diff --git a/RGBController/RGBController_Network.cpp b/RGBController/RGBController_Network.cpp index b3ec8c10..79d3d785 100644 --- a/RGBController/RGBController_Network.cpp +++ b/RGBController/RGBController_Network.cpp @@ -6,6 +6,8 @@ | Adam Honse (CalcProgrammer1) 4/11/2020 | \*-----------------------------------------*/ +#include + #include "RGBController_Network.h" RGBController_Network::RGBController_Network(NetworkClient * client_ptr, unsigned int dev_idx_val) @@ -46,5 +48,12 @@ void RGBController_Network::SetCustomMode() void RGBController_Network::UpdateMode() { - client->SendRequest_RGBController_UpdateMode(dev_idx); + unsigned char * data = GetModeDescription(active_mode); + unsigned int size; + + memcpy(&size, &data[0], sizeof(unsigned int)); + + client->SendRequest_RGBController_UpdateMode(dev_idx, data, size); + + delete[] data; }