Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions backend-process/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ use crate::input_handler::{
use crate::socket::commands::IncomingCommands;
use firmware_api::device;
use log::{debug, error, info};
use messaging::protos::key_config::KeyConfig;
use messaging::protos::server_config::{DisplayImage, ServerConfig};
use std::fs::File;

#[derive(Clone)]
Expand Down Expand Up @@ -160,6 +162,36 @@ fn main() {
dev.clear_all_images().ok();
dev.refresh().ok();
}
IncomingCommands::RequestServerConfig => {
let brightness = db.get_stored_brightness().ok();
let input_mappings = db.get_all_input_mappings().ok();
let display_zone_images = db.get_all_image_mappings().ok();
let mut server_config = ServerConfig {
..ServerConfig::default()
};

if let Some(brightness) = brightness
&& let Some(brightness) = brightness
{
server_config.brightness = brightness.into();
}

if let Some(input_mappings) = input_mappings {
server_config.key_configs = input_mappings
.iter()
.map(|input_mapping| KeyConfig::from(input_mapping.clone()))
.collect();
}

if let Some(display_zone_images) = display_zone_images {
server_config.display_images = display_zone_images
.iter()
.map(|image| DisplayImage::from(image.to_owned()))
.collect()
}

server.send_current_config(server_config).ok();
}
},
Err(e) => {
info!("No known command was handled: {}", e);
Expand Down
704 changes: 448 additions & 256 deletions backend-process/src/protobuf_conversion.rs

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions backend-process/src/socket/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ pub enum IncomingCommands {
ClearAllDisplayZoneImages,
SetBootLogo(String),
SetBrightness(u8),
RequestServerConfig,
}
13 changes: 12 additions & 1 deletion backend-process/src/socket/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ use crate::database::operations::Operations;
use crate::input_handler::InputMapping;
use crate::protobuf_conversion::DisplayZoneWrapper;
use crate::socket::commands::IncomingCommands;
use messaging::protos::server_config::ServerConfig;
use messaging::protos::top_level::TopLevel;
use messaging::protos::top_level::top_level::Command;
use messaging::socket;
use messaging::socket::MessageReceiver;
use messaging::socket::{MessageReceiver, MessageSender};
use protobuf::Message;
use std::io::{Error, ErrorKind};

Expand All @@ -26,6 +27,13 @@ impl<'a> ServerHandler<'a> {
})
}

/// Given a `ServerConfig`, will broadcast the config to the connected clients, which
/// will have to read and parse this
pub fn send_current_config(&mut self, config: ServerConfig) -> Result<(), Error> {
let bytes = config.write_to_bytes()?;
self.server.send_message(bytes.as_slice())
}

/// Checks if there is a message from the connected clients.
///
/// It will either:
Expand Down Expand Up @@ -105,6 +113,9 @@ impl<'a> ServerHandler<'a> {
));
}
}
Command::RequestServerConfig(_) => {
return Ok(IncomingCommands::RequestServerConfig);
}
_ => {}
},
None => {
Expand Down
6 changes: 5 additions & 1 deletion config-frontend/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,11 @@ fn update(application_state: &mut LaunchpadConfigApp, message: Messages) -> Task
},

Messages::RemoveAction(index) => {
if index < application_state.current_input_sequence.len() {
if application_state
.current_input_sequence
.get(index)
.is_some()
{
application_state.current_input_sequence.remove(index);
}
}
Expand Down
2 changes: 2 additions & 0 deletions messaging/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ fn main() {
.includes(["protobufs"])
// Inputs must reside in some of include paths.
.input("protobufs/top_level.proto")
.input("protobufs/server_config.proto")
.input("protobufs/commands/key_config.proto")
.input("protobufs/commands/boot_logo.proto")
.input("protobufs/commands/brightness.proto")
.input("protobufs/commands/display_zone_image.proto")
.input("protobufs/commands/server_request.proto")
.input("protobufs/commands/common/keys.proto")
.input("protobufs/commands/common/inputs.proto")
.input("protobufs/commands/common/display_zones.proto")
Expand Down
6 changes: 6 additions & 0 deletions messaging/examples/client_sending_key_config_to_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ fn main() {
println!("4. Set brightness");
println!("5. Clear all key images");
println!("6. Clear single key image");
println!("6. Request current server config");
io::stdin().read_line(&mut buffer).unwrap();

let mut handler = ClientWrapper::new(client);
Expand Down Expand Up @@ -106,6 +107,11 @@ fn main() {
6 => handler
.clear_display_zone_image(DisplayZone::BUTTON_2)
.unwrap(),
7 => {
handler.request_server_config().unwrap();
let result = handler.check_for_server_config().unwrap();
println!("{:?}", result);
}
_ => {
panic!("Out of range of options!")
}
Expand Down
9 changes: 9 additions & 0 deletions messaging/protobufs/commands/config_state.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
syntax = "proto3";

package config_state;

message RequestConfigState {}

message CurrentConfigState {
uint32 brightness = 1;
}
6 changes: 6 additions & 0 deletions messaging/protobufs/commands/server_request.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
syntax = "proto3";

package server_request;

message RequestServerConfig {}

15 changes: 15 additions & 0 deletions messaging/protobufs/server_config.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
syntax = "proto3";

import "commands/key_config.proto";
import "commands/common/display_zones.proto";

message DisplayImage {
DisplayZone display_zone = 1;
string path = 2;
}

message ServerConfig {
repeated key_config.KeyConfig key_configs = 1;
repeated DisplayImage display_images = 2;
uint32 brightness = 3;
}
2 changes: 2 additions & 0 deletions messaging/protobufs/top_level.proto
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'commands/boot_logo.proto';
import 'commands/display_zone_image.proto';
import 'commands/brightness.proto';
import 'commands/key_config.proto';
import 'commands/server_request.proto';

message TopLevel {
oneof command {
Expand All @@ -13,5 +14,6 @@ message TopLevel {
display_zone_image.ClearAllDisplayZoneImages clear_all_display_zone_images_command = 4;
brightness.SetBrightness set_brightness_command = 5;
boot_logo.SetBootLogo set_boot_logo_command = 6;
server_request.RequestServerConfig request_server_config = 7;
}
}
23 changes: 22 additions & 1 deletion messaging/src/client_wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ use crate::protos::display_zone_image::{
use crate::protos::display_zones::DisplayZone;
use crate::protos::inputs::InputId;
use crate::protos::key_config::{Action, KeyConfig};
use crate::protos::server_config::ServerConfig;
use crate::protos::server_request::RequestServerConfig;
use crate::protos::top_level::TopLevel;
use crate::protos::top_level::top_level::Command;
use crate::socket;
use crate::socket::MessageSender;
use crate::socket::{MessageReceiver, MessageSender};
use protobuf::{EnumOrUnknown, Message};
use std::io::Error;

Expand Down Expand Up @@ -55,6 +57,10 @@ pub trait ClientCommands {
///
/// * `display_zone` - the specific area/zone of the display to clear
fn clear_display_zone_image(&mut self, display_zone: DisplayZone) -> Result<(), Error>;
/// Sends a request for the server to produce a config message
fn request_server_config(&mut self) -> Result<(), Error>;
/// _Blocking_ call for checking if the server has sent the config
fn check_for_server_config(&mut self) -> Result<ServerConfig, Error>;
}

/// To be used by any client that wants to communicate with the server
Expand Down Expand Up @@ -141,6 +147,21 @@ impl ClientCommands for ClientWrapper {
.as_slice(),
)
}

fn request_server_config(&mut self) -> Result<(), Error> {
self.client.send_message(
create_command(Command::RequestServerConfig(RequestServerConfig {
..RequestServerConfig::default()
}))
.write_to_bytes()?
.as_slice(),
)
}

fn check_for_server_config(&mut self) -> Result<ServerConfig, Error> {
let message = self.client.read_message()?;
Ok(ServerConfig::parse_from_bytes(message.as_slice())?)
}
}

fn create_command(command: Command) -> TopLevel {
Expand Down
2 changes: 2 additions & 0 deletions messaging/src/protos/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ pub mod display_zones;
pub mod inputs;
pub mod key_config;
pub mod keys;
pub mod server_config;
pub mod server_request;
pub mod top_level;
Loading
Loading