fork/GramFirmware
git clone https://git.julia.blue/fork/GramFirmware
818d962
gram
1/* 2* This file is part of HayBox 3* Copyright (C) 2024 Jonathan Haylett 4* 5* HayBox is free software: you can redistribute it and/or modify 6* it under the terms of the GNU General Public License version 3 as 7* published by the Free Software Foundation. 8* 9* This software is distributed in the hope that it will be useful, 10* but WITHOUT ANY WARRANTY; without even the implied warranty of 11* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12* GNU General Public License for more details. 13* 14* You should have received a copy of the GNU General Public License 15* along with this software. If not, see <http://www.gnu.org/licenses/>. 16*/ 17 18#include "comms/ConfiguratorBackend.hpp" 19 20#include "core/InputSource.hpp" 21#include "core/Persistence.hpp" 22#include "reboot.hpp" 23 24#include <pb_arduino.h> 25#include <pb_decode.h> 26#include <pb_encode.h> 27 28ConfiguratorBackend ::ConfiguratorBackend ( 29InputState & inputs , 30InputSource ** input_sources , 31size_t input_source_count , 32Config & config , 33Stream & stream 34) 35 :CommunicationBackend (inputs ,input_sources ,input_source_count ), 36_in (stream ), 37_out (stream ), 38_base_stream (stream ), 39_config (config ) {} 40 41CommunicationBackendId ConfiguratorBackend ::BackendId () { 42return COMMS_BACKEND_CONFIGURATOR ; 43} 44 45void ConfiguratorBackend ::SendReport () { 46int data = ReadByte (); 47if (data < 0 ) { 48if (data == _in .EOP ) { 49SkipToNextPacket (); 50 } 51return ; 52 } 53 54Command command = (Command )data ; 55switch (command ) { 56case CMD_GET_DEVICE_INFO : 57HandleGetDeviceInfo (); 58break ; 59case CMD_GET_CONFIG : 60HandleGetConfig (); 61break ; 62case CMD_SET_CONFIG : 63HandleSetConfig (); 64break ; 65case CMD_REBOOT_FIRMWARE : 66reboot_firmware (); 67break ; 68case CMD_REBOOT_BOOTLOADER : 69reboot_bootloader (); 70break ; 71case CMD_UNSPECIFIED : 72default : 73HandleUnknownCommand (command ); 74break ; 75 } 76 77SkipToNextPacket (); 78} 79 80size_t ConfiguratorBackend ::ReadPacket (uint8_t * buffer ,size_t max_len ) { 81size_t bytes_read = 0 ; 82while (!_in .available ()) { 83delay (1 ); 84 } 85while (true) { 86int result = _in .read (); 87if (result == _in .EOF ) { 88continue ; 89 } 90if (result == _in .EOP ) { 91break ; 92 } 93buffer [bytes_read ++ ]= result ; 94if (bytes_read >=max_len ) { 95_in .next (); 96return bytes_read ; 97 } 98 } 99_in .next (); 100 101return bytes_read ; 102} 103 104int ConfiguratorBackend ::ReadByte () { 105if (!_base_stream .available ()) { 106return -1 ; 107 } 108return _in .read (); 109} 110 111void ConfiguratorBackend ::SkipToNextPacket () { 112_in .next (); 113} 114 115bool ConfiguratorBackend ::WritePacket (Command command_id ,uint8_t * buffer ,size_t len ) { 116_out .write ((uint8_t )command_id ); 117for (size_t i = 0 ;i < len ;i ++ ) { 118_out .write (buffer [i ]); 119 } 120return _out .end (); 121} 122 123bool ConfiguratorBackend ::HandleGetDeviceInfo () { 124static const DeviceInfo device_info = { 125FIRMWARE_NAME , 126FIRMWARE_VERSION , 127DEVICE_NAME , 128 }; 129 130// Ensure device info encodes correctly. 131size_t size ; 132if (!pb_get_encoded_size (& size ,DeviceInfo_fields ,& device_info )) { 133char errmsg []= "Failed to encode device info" ; 134WritePacket (CMD_ERROR , (uint8_t * )errmsg ,sizeof (errmsg )); 135return false; 136 } 137 138_out .write (CMD_SET_DEVICE_INFO ); 139pb_ostream_t ostream = as_pb_ostream (_out ); 140pb_encode (& ostream ,DeviceInfo_fields ,& device_info ); 141return _out .end (); 142} 143 144bool ConfiguratorBackend ::HandleGetConfig () { 145if (!persistence .CheckSavedConfig ()) { 146char errmsg []= "Config file is invalid" ; 147WritePacket (CMD_ERROR , (uint8_t * )errmsg ,sizeof (errmsg )); 148return false; 149 } 150 151// Write raw config data to output stream. 152_out .write (CMD_SET_CONFIG ); 153persistence .LoadConfigRaw (_out , false); 154return _out .end (); 155} 156 157bool ConfiguratorBackend ::HandleSetConfig () { 158// Reset config defaults first, so config is completely replaced rather than merged. 159_config = Config_init_default ; 160 161pb_istream_t istream = as_pb_istream (_in ); 162if (!pb_decode (& istream ,Config_fields ,& _config )) { 163char errmsg [100 ]; 164size_t errmsg_len = 165snprintf (errmsg ,sizeof (errmsg ),"Failed to decode config: %s" ,istream .errmsg ); 166WritePacket (CMD_ERROR , (uint8_t * )errmsg ,errmsg_len ); 167 168// Restore old config. 169persistence .LoadConfig (_config ); 170return false; 171 } 172 173if (_config .default_backend_config > _config .communication_backend_configs_count ) { 174char errmsg [75 ]; 175size_t errmsg_len = snprintf ( 176errmsg , 177sizeof (errmsg ), 178"Default backend ID is %d but only %d backend configs are defined" , 179 (uint8_t )_config .default_backend_config , 180 (uint8_t )_config .communication_backend_configs_count 181 ); 182WritePacket (CMD_ERROR , (uint8_t * )errmsg ,errmsg_len ); 183return false; 184 } 185 186for (size_t i = 0 ;i < _config .communication_backend_configs_count ;i ++ ) { 187uint8_t default_mode_id = _config .communication_backend_configs [i ].default_mode_config ; 188if (default_mode_id > _config .game_mode_configs_count ) { 189char errmsg [75 ]; 190size_t errmsg_len = snprintf ( 191errmsg , 192sizeof (errmsg ), 193"Default mode ID is %d for backend %d but only %d modes are defined" , 194 (uint8_t )default_mode_id , 195 (uint8_t )i + 1 , 196 (uint8_t )_config .game_mode_configs_count 197 ); 198WritePacket (CMD_ERROR , (uint8_t * )errmsg ,errmsg_len ); 199return false; 200 } 201 } 202 203for (size_t i = 0 ;i < _config .game_mode_configs_count ;i ++ ) { 204const GameModeConfig & gamemode_config = _config .game_mode_configs [i ]; 205uint8_t keyboard_mode_id = gamemode_config .keyboard_mode_config ; 206uint8_t custom_mode_id = gamemode_config .custom_mode_config ; 207 208if (keyboard_mode_id > 0 && gamemode_config .mode_id != MODE_KEYBOARD ) { 209char errmsg [80 ]; 210size_t errmsg_len = snprintf ( 211errmsg , 212sizeof (errmsg ), 213"keyboard_mode_id is set for game mode %d but mode_id is not MODE_KEYBOARD" , 214 (uint8_t )i + 1 215 ); 216WritePacket (CMD_ERROR , (uint8_t * )errmsg ,errmsg_len ); 217return false; 218 } 219 220if (custom_mode_id > 0 && gamemode_config .mode_id != MODE_CUSTOM ) { 221char errmsg [75 ]; 222size_t errmsg_len = snprintf ( 223errmsg , 224sizeof (errmsg ), 225"custom_mode_id is set for game mode %d but mode_id is not MODE_CUSTOM" , 226 (uint8_t )i + 1 227 ); 228WritePacket (CMD_ERROR , (uint8_t * )errmsg ,errmsg_len ); 229return false; 230 } 231 232if (keyboard_mode_id > _config .keyboard_modes_count ) { 233char errmsg [85 ]; 234size_t errmsg_len = snprintf ( 235errmsg , 236sizeof (errmsg ), 237"Keyboard mode ID %d is for game mode %d but only %d keyboard modes are defined" , 238 (uint8_t )keyboard_mode_id , 239 (uint8_t )i + 1 , 240 (uint8_t )_config .keyboard_modes_count 241 ); 242WritePacket (CMD_ERROR , (uint8_t * )errmsg ,errmsg_len ); 243return false; 244 } 245 246if (custom_mode_id > _config .custom_modes_count ) { 247char errmsg [85 ]; 248size_t errmsg_len = snprintf ( 249errmsg , 250sizeof (errmsg ), 251"Custom mode ID %d is for game mode config %d but only %d custom modes are defined" , 252 (uint8_t )custom_mode_id , 253 (uint8_t )i + 1 , 254 (uint8_t )_config .custom_modes_count 255 ); 256WritePacket (CMD_ERROR , (uint8_t * )errmsg ,errmsg_len ); 257return false; 258 } 259 } 260 261if (!persistence .SaveConfig (_config )) { 262char errmsg []= "Failed to save config to memory" ; 263WritePacket (CMD_ERROR , (uint8_t * )errmsg ,sizeof (errmsg )); 264return false; 265 } 266 267WritePacket (CMD_SUCCESS ,nullptr ,0 ); 268return true; 269} 270 271bool ConfiguratorBackend ::HandleUnknownCommand (Command command ) { 272char errmsg [50 ]; 273size_t errmsg_len = 274snprintf (errmsg ,sizeof (errmsg ),"Unknown command ID: %d" , (uint8_t )command ); 275return WritePacket (CMD_ERROR , (uint8_t * )errmsg ,errmsg_len ); 276}