diff --git a/Cargo.lock b/Cargo.lock index 92ccf0781..4dfe06d84 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -110,9 +110,11 @@ dependencies = [ "graphics-server", "locales", "log", + "modals", "num-derive", "num-traits", "rkyv", + "ureq", "xous", "xous-api-log", "xous-api-names", diff --git a/apps/app-loader/Cargo.toml b/apps/app-loader/Cargo.toml index 51f9f3093..62a11388e 100644 --- a/apps/app-loader/Cargo.toml +++ b/apps/app-loader/Cargo.toml @@ -14,6 +14,7 @@ log = "0.4.14" xous-names = { package = "xous-api-names", version = "0.9.37" } gam = { path = "../../services/gam" } graphics-server = { path = "../../services/graphics-server" } +modals = { path = "../../services/modals" } ticktimer-server = { package = "xous-api-ticktimer", version = "0.9.44" } locales = { path = "../../locales" } @@ -21,3 +22,5 @@ num-derive = {version = "0.3.3", default-features = false} num-traits = {version = "0.2.14", default-features = false} rkyv = {version = "0.4.3", features = ["const_generics"], default-features = false} + +ureq = { version = "2.5.0", features = ["json"] } \ No newline at end of file diff --git a/apps/app-loader/src/api.rs b/apps/app-loader/src/api.rs index 083a9834c..84e9fdccd 100644 --- a/apps/app-loader/src/api.rs +++ b/apps/app-loader/src/api.rs @@ -1,15 +1,20 @@ -use gam::{Gam, MenuItem, UxRegistration, menu_matic, APP_NAME_APP_LOADER, APP_MENU_0_APP_LOADER, MenuMatic}; -use xous_ipc::Buffer; +use std::io::Read; +use gam::{Gam, MenuItem, UxRegistration, menu_matic, APP_NAME_APP_LOADER, APP_MENU_0_APP_LOADER, MenuMatic, TextEntryPayload}; +use modals::Modals; +use xous::MemoryRange; use num_traits::ToPrimitive; use crate::SERVER_NAME_APP_LOADER; pub(crate) struct AppLoader { gam: Gam, + modals: Modals, auth: [u32; 4], ticktimer: ticktimer_server::Ticktimer, menu: MenuMatic, conn: xous::CID, apps: Vec>, + possible_apps: Vec>, + server: Option, } impl AppLoader { @@ -30,6 +35,8 @@ impl AppLoader { rawkeys_id: None }).expect("Couldn't register").expect("Didn't get an auth token"); + let modals = Modals::new(xns).expect("Couldn't get modals"); + // a connection to the server let conn = xns.request_connection_blocking(SERVER_NAME_APP_LOADER).expect("Couldn't connect to server"); @@ -37,27 +44,109 @@ impl AppLoader { let ticktimer = ticktimer_server::Ticktimer::new().expect("Couldn't connect to Ticktimer"); // the menu - let menu = menu_matic(Vec::new(), APP_MENU_0_APP_LOADER, Some(xous::create_server().unwrap())).expect("Couldn't create menu"); + let set_server_item = MenuItem{ name: xous_ipc::String::from_str("Set Server"), + action_conn: Some(conn), + action_opcode: Opcode::SetServer.to_u32().unwrap(), + action_payload: gam::MenuPayload::Scalar([0, 0, 0, 0]), + close_on_select: true }; + let menu = menu_matic(vec![set_server_item], APP_MENU_0_APP_LOADER, Some(xous::create_server().unwrap())).expect("Couldn't create menu"); - AppLoader{ gam, auth, conn, ticktimer, menu, apps: Vec::new() } + AppLoader{ gam, modals, auth, conn, ticktimer, menu, apps: Vec::new(), possible_apps: Vec::new(), server: None } } - pub(crate) fn add_app(&mut self, name: &str) { - let buf = Buffer::into_buf(LoadAppRequest { name: xous_ipc::String::from_str(name) }) - .expect("Couldn't create buffer"); - buf.send(self.conn, - Opcode::LoadApp.to_u32().unwrap()).expect("Couldn't send message"); + pub(crate) fn add_app(&mut self, index: usize) { + let name = self.possible_apps[index]; + + // load the app from the server + let response = ureq::get(&format!("{}/{}", self.server.as_ref().expect("AddApp was somehow called without a server!"), name)) + .call().expect("Couldn't make request"); + + let len = response.header("Content-Length").expect("No Content-Length header") + .parse::().expect("Couldn't parse Content-Length header"); + + let mut app_file = Vec::with_capacity(len); + response.into_reader() + .read_to_end(&mut app_file).expect("Couldn't read"); + let app_file_slice = app_file.as_slice(); + + let memory = unsafe { MemoryRange::new((app_file_slice.as_ptr() as usize) & !0xFFF, + len + if len & 0xFFF == 0 { 0 } else { 0x1000 - (len & 0xFFF) }).unwrap() }; + + ////////////////////// + // The loading part // + ////////////////////// + + // create the spawn process + let stub = include_bytes!("spawn.bin"); + let args = xous::ProcessArgs::new(stub, xous::MemoryAddress::new(0x2050_1000).unwrap(), xous::MemoryAddress::new(0x2050_1000).unwrap()); + let spawn = xous::create_process(args).expect("Couldn't create spawn process"); + log::info!("Spawn PID: {}, Spawn CID: {}", spawn.pid, spawn.cid); + + // perform a ping to make sure that spawn is running + let result = xous::send_message( + spawn.cid, + xous::Message::new_blocking_scalar(2, 1, 2, 3, 4), + ) + .unwrap(); + assert_eq!(xous::Result::Scalar1(2), result); + + // load the app from the binary file + xous::send_message(spawn.cid, xous::Message::new_lend(1, memory, None, None)).expect("Couldn't send a message to spawn"); + + ////////////////////// + // back to graphics // + ////////////////////// // add its name to GAM - self.gam.register_name(name, self.auth).expect("Couldn't register name"); + self.gam.register_name(name.to_str(), self.auth).expect("Couldn't register name"); // add it to the menu - self.menu.add_item(MenuItem { name: xous_ipc::String::from_str(name), - action_conn: Some(self.conn), - action_opcode: Opcode::DispatchApp.to_u32().unwrap(), - action_payload: gam::MenuPayload::Scalar([self.apps.len().try_into().unwrap(), 0, 0, 0]), - close_on_select: true }); - self.apps.push(xous_ipc::String::from_str(name)); + self.menu.insert_item(MenuItem { name, + action_conn: Some(self.conn), + action_opcode: Opcode::DispatchApp.to_u32().unwrap(), + action_payload: gam::MenuPayload::Scalar([self.apps.len().try_into().unwrap(), 0, 0, 0]), + close_on_select: true }, 0); + self.apps.push(name); + + log::info!("Added app `{}'!", name); + + self.redraw(); + } + + pub(crate) fn set_server(&mut self) { + let payload = self.modals.alert_builder("Server IP Address:Port") + // .field(None, Some(|payload: TextEntryPayload| if payload.as_str().trim_start_matches("https://").trim_start_matches("http://").chars().filter(|c| *c == ':').count() == 1 { None } else { Some(xous_ipc::String::from_str("Port is not specified")) })) + .field(None, None) + .build() + .ok(); + + if self.server.is_none() && payload.is_some() { + self.menu.insert_item(MenuItem { name: xous_ipc::String::from_str("Reload App List"), + action_conn: Some(self.conn), + action_opcode: Opcode::ReloadAppList.to_u32().unwrap(), + action_payload: gam::MenuPayload::Scalar([0, 0, 0, 0]), + close_on_select: true }, 0); + } + + self.server = payload.and_then(|p| Some(p.first().as_str().to_string())); + } + + pub(crate) fn reload_app_list(&mut self) { + // without a path, the server responds with a JSON list of strings representing the list of app names + self.possible_apps = ureq::get(&self.server.as_ref().expect("ReloadAppList was somehow called without a server!")) + .call().expect("Couldn't make request") + .into_json::>().expect("Couldn't convert into JSON") + .iter() + .map(|s| xous_ipc::String::<64>::from_str(&s)) + .collect(); + + for (i, app) in self.possible_apps.iter().enumerate() { + self.menu.insert_item(MenuItem { name: xous_ipc::String::from_str("Add ".to_owned()+app.to_str()), + action_conn: Some(self.conn), + action_opcode: Opcode::AddApp.to_u32().unwrap(), + action_payload: gam::MenuPayload::Scalar([i.try_into().unwrap(), 0, 0, 0]), + close_on_select: true }, 0); + } } pub(crate) fn dispatch_app(&self, index: usize) { @@ -85,8 +174,14 @@ impl AppLoader { #[derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive)] pub(crate) enum Opcode { - /// to load an app into ram - LoadApp, + /// set the server + SetServer, + + /// get a list of apps from the server + ReloadAppList, + + /// load an app and add it to the menu + AddApp, /// dispatch the app DispatchApp, @@ -94,8 +189,3 @@ pub(crate) enum Opcode { /// Redraw the UI Redraw } - -#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)] -pub(crate) struct LoadAppRequest { - pub name: xous_ipc::String<64> -} diff --git a/apps/app-loader/src/main.rs b/apps/app-loader/src/main.rs index 47c09aac8..f1f02b934 100644 --- a/apps/app-loader/src/main.rs +++ b/apps/app-loader/src/main.rs @@ -1,7 +1,6 @@ mod api; use api::*; use num_traits::FromPrimitive; -use xous_ipc::Buffer; const SERVER_NAME_APP_LOADER: &str = "_App Loader_"; @@ -15,37 +14,19 @@ fn main() -> ! { // start off by adding hello world let mut app_loader = AppLoader::new(&xns, &sid); - app_loader.add_app("Hello"); loop { let msg = xous::receive_message(sid).unwrap(); match FromPrimitive::from_usize(msg.body.id()).expect("Couldn't load message") { - Opcode::LoadApp => { - let buffer = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) }; - let load_app_req = buffer.to_original::().unwrap(); - - // create the spawn process - let stub = include_bytes!("../../../target/riscv32imac-unknown-xous-elf/debug/spawn.bin"); - let args = xous::ProcessArgs::new(stub, xous::MemoryAddress::new(0x2050_1000).unwrap(), xous::MemoryAddress::new(0x2050_1000).unwrap()); - let spawn = xous::create_process(args).expect("Couldn't create spawn process"); - log::info!("Spawn PID: {}, Spawn CID: {}", spawn.pid, spawn.cid); - - // perform a ping to make sure that spawn is running - let result = xous::send_message( - spawn.cid, - xous::Message::new_blocking_scalar(2, 1, 2, 3, 4), - ) - .unwrap(); - assert_eq!(xous::Result::Scalar1(2), result); - - // load the app from the binary file - let bin = include_bytes!("../../../target/riscv32imac-unknown-xous-elf/debug/hello"); - let bin_len = bin.len(); - let bin_loc = bin.as_ptr() as usize; - let buf = unsafe { xous::MemoryRange::new(bin_loc & !0xFFF, bin_len + if bin_len & 0xFFF == 0 { 0 } else {0x1000 - (bin_len & 0xFFF)}).expect("Couldn't create a buffer from the segment") }; - xous::send_message(spawn.cid, xous::Message::new_lend(1, buf, None, None)).expect("Couldn't send a message to spawn"); - - log::info!("Loaded app `{}'!", load_app_req.name); + Opcode::SetServer => { + app_loader.set_server(); + }, + Opcode::ReloadAppList => { + app_loader.reload_app_list(); + }, + Opcode::AddApp => { + let index = msg.body.scalar_message().expect("Not a scalar").arg1; + app_loader.add_app(index); }, Opcode::DispatchApp => { let index = msg.body.scalar_message().expect("Not a scalar").arg1; diff --git a/apps/app-loader/src/spawn.bin b/apps/app-loader/src/spawn.bin new file mode 100644 index 000000000..37b1ad9cb Binary files /dev/null and b/apps/app-loader/src/spawn.bin differ diff --git a/tools/app_server.py b/tools/app_server.py new file mode 100644 index 000000000..df2d96864 --- /dev/null +++ b/tools/app_server.py @@ -0,0 +1,64 @@ +#! /usr/bin/env python3 + +import argparse +import json +from http.server import BaseHTTPRequestHandler, HTTPServer +from urllib.parse import unquote + +class XousAppServer(BaseHTTPRequestHandler): + def __init__(self, profile, target, context_to_app, *args): + self.profile = profile + self.target = target + self.context_to_app = context_to_app + BaseHTTPRequestHandler.__init__(self, *args) + + def do_GET(self): + # an empty path should return a list of apps + path = self.path.split('/')[-1] + if path == '': + self.send_response(200) + self.send_header('Content-Type', 'application/json') + self.end_headers() + + l = json.dumps(list(self.context_to_app.keys())) + + self.wfile.write(l.encode()) + else: + name = self.context_to_app[unquote(path)] + with open('target/{}/{}/{}'.format(self.target, self.profile, name), 'rb') as f: + data = f.read() + self.send_response(200) + self.send_header('Content-Type', 'application/octet-stream') + self.send_header('Content-Length', str(len(data))) + self.end_headers() + + self.wfile.write(data) + +def main(): + parser = argparse.ArgumentParser(description="Xous App Server") + parser.add_argument('apps', metavar='APP', nargs='+', + help='Specifies which apps to serve') + parser.add_argument('-p', '--port', + help='Specifies the port that the server runs on', + required=True, type=int) + parser.add_argument('--profile', default="release", + help="Either debug or release depending on how the apps were compiled. Defaults to release") + parser.add_argument('--target', default="riscv32imac-unknown-xous-elf", + help="The target that the apps were compile to. Defaults to riscv32imac-unknown-xous-elf") + args = parser.parse_args() + + # get the GAM names for each of the apps + with open('apps/manifest.json', 'r') as f: + manifest = json.loads(f.read()) + + context_to_app = {} + + for app in args.apps: + context_name = manifest[app]['context_name'] + context_to_app[context_name] = app + + server = HTTPServer(('0.0.0.0', args.port), lambda *server_args: XousAppServer(args.profile, args.target, context_to_app, *server_args)) + server.serve_forever() + +if __name__ == "__main__": + main() diff --git a/xtask/src/builder.rs b/xtask/src/builder.rs index d517f205f..60b7e6caa 100644 --- a/xtask/src/builder.rs +++ b/xtask/src/builder.rs @@ -130,6 +130,8 @@ pub(crate) struct Builder { locale_stash: String, /// when set to true, hosted mode builds but does not run dry_run: bool, + /// when set to true, user selected packages are compiled but no image is created + no_image: bool, } impl Builder { @@ -154,6 +156,7 @@ impl Builder { locale_override: None, locale_stash: String::new(), dry_run: false, + no_image: false, } } /// Specify an alternate loader key, as a String that can encode a file name @@ -237,6 +240,14 @@ impl Builder { self.kernel = CrateSpec::Local("xous-kernel".to_string(), false); self } + pub fn target_precursor_no_image<'a>(&'a mut self, soc_version: &str) -> &'a mut Builder { + self.target = Some(crate::TARGET_TRIPLE_RISCV32.to_string()); + self.stream = BuildStream::Release; + self.utra_target = format!("precursor-{}", soc_version).to_string(); + self.run_svd2repl = false; + self.no_image = true; + self + } /// Configure for ARM targets pub fn target_arm(&mut self) -> &mut Builder { @@ -597,8 +608,13 @@ impl Builder { false )?; - // ------ either create an image, or launch hosted mode ------ - if self.target.is_none() { // hosted mode doesn't specify a cross-compilation target! + // ------ either stop here, create an image, or launch hosted mode ------ + if self.no_image { + println!("The following apps/services were compiled:"); + for path in services_path { + println!("{}", path); + } + } else if self.target.is_none() { // hosted mode doesn't specify a cross-compilation target! // throw a warning if prebuilts are specified for hosted mode for item in [&self.services[..], &self.apps[..]].concat() { match item { diff --git a/xtask/src/main.rs b/xtask/src/main.rs index 6dc34e398..8acfea17e 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -363,6 +363,10 @@ fn main() -> Result<(), Box> { .add_services(&user_pkgs.into_iter().map(String::from).collect()) .add_feature("avalanchetest"); } + Some("compile-apps") => { + builder.target_precursor_no_image(PRECURSOR_SOC_VERSION) + .add_services(&gfx_base_pkgs.into_iter().map(String::from).collect()); + } // ------ Cramium hardware image configs ------ Some("cramium-fpga") | Some("cramium-soc") => { @@ -504,6 +508,7 @@ Other commands: generate-locales (re)generate the locales include for the language selected in locales/src/locale.rs wycheproof-import generate binary test vectors for engine-25519 from whycheproof-import/x25519.json install-toolkit installs Xous toolkit with no prompt, useful in CI. Specify `--force` to remove existing toolchains + compile-apps Just compiles the apps specified in [cratespecs], for example in order to use app server Note: By default, the `ticktimer` will get rebuilt every time. You can skip this by appending `--no-timestamp` to the command. "