Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 453f03d3 authored by Zach Johnson's avatar Zach Johnson
Browse files

rusty-gd: implement vector payloads for packets

this way, we can get the contents to pass to higher layers from ACL, etc

Bug: 171749953
Tag: #gd-refactor
Test: gd/cert/run --rhost
Change-Id: Ib18512d2dcafa9f8624e19a506634b8f92d1e170
parent 248e9fb6
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -22,7 +22,7 @@
void generate_rust_packet_preamble(std::ostream& s) {
  s <<
      R"(
use bytes::{Bytes, BytesMut};
use bytes::{Bytes, BytesMut, BufMut};
use num_derive::{FromPrimitive, ToPrimitive};
use num_traits::{FromPrimitive, ToPrimitive};
use std::convert::TryInto;
+71 −59
Original line number Diff line number Diff line
@@ -741,7 +741,8 @@ void PacketDef::GenBuilderConstructor(std::ostream& s) const {
}

void PacketDef::GenRustChildEnums(std::ostream& s) const {
  if (!children_.empty()) {
  if (HasChildEnums()) {
    bool payload = fields_.HasPayload();
    s << "#[derive(Debug)] ";
    s << "enum " << name_ << "DataChild {";
    for (const auto& child : children_) {
@@ -750,6 +751,9 @@ void PacketDef::GenRustChildEnums(std::ostream& s) const {
      }
      s << child->name_ << "(Arc<" << child->name_ << "Data>),";
    }
    if (payload) {
      s << "Payload(Arc<Vec<u8>>),";
    }
    s << "None,";
    s << "}\n";
    s << "#[derive(Debug)] ";
@@ -760,6 +764,9 @@ void PacketDef::GenRustChildEnums(std::ostream& s) const {
      }
      s << child->name_ << "(" << child->name_ << "Packet),";
    }
    if (payload) {
      s << "Payload(Arc<Vec<u8>>),";
    }
    s << "None,";
    s << "}\n";
  }
@@ -771,7 +778,7 @@ void PacketDef::GenRustStructDeclarations(std::ostream& s) const {

  // Generate struct fields
  GenRustStructFieldNameAndType(s);
  if (!children_.empty()) {
  if (HasChildEnums()) {
    s << "child: " << name_ << "DataChild,";
  }
  s << "}\n";
@@ -799,6 +806,9 @@ void PacketDef::GenRustStructDeclarations(std::ostream& s) const {
    param->GenRustNameAndType(s);
    s << ", ";
  }
  if (fields_.HasPayload()) {
    s << "pub payload: Option<Vec<u8>>,";
  }
  s << "}\n";
}

@@ -855,21 +865,6 @@ void PacketDef::GenRustStructSizeField(std::ostream& s) const {

void PacketDef::GenRustStructImpls(std::ostream& s) const {
  s << "impl " << name_ << "Data {";
  s << "fn new(";
  bool fields_exist = GenRustStructFieldNameAndType(s);
  if (!children_.empty()) {
    s << "child: " << name_ << "DataChild,";
  }
  s << ") -> Self { ";

  s << "Self { ";
  GenRustStructFieldNames(s);
  if (!children_.empty()) {
    s << "child";
  }

  s << "}";
  s << "}";

  // parse function
  if (parent_constraints_.empty() && !children_.empty() && parent_ != nullptr) {
@@ -913,7 +908,6 @@ void PacketDef::GenRustStructImpls(std::ostream& s) const {

  if (!children_.empty()) {
    s << "let child = match " << constraint_name << " {";
  }

    for (const auto& desc : constrained_descendants) {
      if (desc.first->name_.rfind("LeGetVendorCapabilitiesComplete", 0) == 0) {
@@ -951,8 +945,13 @@ void PacketDef::GenRustStructImpls(std::ostream& s) const {
      s << "_ => panic!(\"unexpected value " << "\"),";
    }

  if (!children_.empty()) {
    s << "};\n";
  } else if (fields_.HasPayload()) {
    s << "let child = if payload.len() > 0 {";
    s << name_ << "DataChild::Payload(Arc::new(payload))";
    s << "} else {";
    s << name_ << "DataChild::None";
    s << "};";
  }

  s << "Ok(Self {";
@@ -966,7 +965,7 @@ void PacketDef::GenRustStructImpls(std::ostream& s) const {
      FixedScalarField::kFieldType,
  });

  if (fields_exist) {
  if (fields.size() > 0) {
    for (int i = 0; i < fields.size(); i++) {
      auto field_type = fields[i]->GetFieldType();
      s << fields[i]->GetName();
@@ -974,7 +973,7 @@ void PacketDef::GenRustStructImpls(std::ostream& s) const {
    }
  }

  if (!children_.empty()) {
  if (HasChildEnums()) {
    s << "child,";
  }
  s << "})\n";
@@ -982,7 +981,7 @@ void PacketDef::GenRustStructImpls(std::ostream& s) const {

  // write_to function
  s << "fn write_to(&self, buffer: &mut BytesMut) {";
  if (fields_exist) {
  if (fields.size() > 0) {
    s << " buffer.resize(buffer.len() + self.get_size(), 0);";
  }

@@ -1007,7 +1006,7 @@ void PacketDef::GenRustStructImpls(std::ostream& s) const {
    field->GenRustWriter(s, start_field_offset, end_field_offset);
  }

  if (!children_.empty()) {
  if (HasChildEnums()) {
    s << "match &self.child {";
    for (const auto& child : children_) {
      if (child->name_.rfind("LeGetVendorCapabilitiesComplete", 0) == 0) {
@@ -1015,13 +1014,16 @@ void PacketDef::GenRustStructImpls(std::ostream& s) const {
      }
      s << name_ << "DataChild::" << child->name_ << "(value) => value.write_to(buffer),";
    }
    if (fields_.HasPayload()) {
      s << name_ << "DataChild::Payload(p) => buffer.put(&p[..]),";
    }
    s << name_ << "DataChild::None => {}";
    s << "}";
  }

  s << "}\n";

  if (fields_exist) {
  if (fields.size() > 0) {
    s << "pub fn get_size(&self) -> usize {";
    GenRustStructSizeField(s);
    s << "}";
@@ -1061,7 +1063,7 @@ void PacketDef::GenRustAccessStructImpls(std::ostream& s) const {
    s << "}";
  }

  if (!children_.empty()) {
  if (HasChildEnums()) {
    s << " pub fn specialize(&self) -> " << name_ << "Child {";
    s << " match &self." << util::CamelCaseToUnderScore(name_) << ".child {";
    for (const auto& child : children_) {
@@ -1071,6 +1073,9 @@ void PacketDef::GenRustAccessStructImpls(std::ostream& s) const {
      s << name_ << "DataChild::" << child->name_ << "(_) => " << name_ << "Child::" << child->name_ << "("
        << child->name_ << "Packet::new(self." << root_accessor << ".clone())),";
    }
    if (fields_.HasPayload()) {
      s << name_ << "DataChild::Payload(p) => " << name_ << "Child::Payload(p.clone()),";
    }
    s << name_ << "DataChild::None => " << name_ << "Child::None,";
    s << "}}";
  }
@@ -1190,9 +1195,16 @@ void PacketDef::GenRustBuilderStructImpls(std::ostream& s) const {
      }
      s << ", ";
    }
    if (!ancestor->children_.empty()) {
    if (ancestor->HasChildEnums()) {
      if (prev == nullptr) {
        if (ancestor->fields_.HasPayload()) {
          s << "child: match self.payload { ";
          s << "None => " << name_ << "DataChild::None,";
          s << "Some(vec) => " << name_ << "DataChild::Payload(Arc::new(vec)),";
          s << "},";
        } else {
          s << "child: " << name_ << "DataChild::None,";
        }
      } else {
        s << "child: " << ancestor->name_ << "DataChild::" << prev->name_ << "("
          << util::CamelCaseToUnderScore(prev->name_) << "),";
+4 −0
Original line number Diff line number Diff line
@@ -565,3 +565,7 @@ std::vector<const ParentDef*> ParentDef::FindPathToDescendant(std::string descen
  }
  return res;
}

bool ParentDef::HasChildEnums() const {
  return !children_.empty() || fields_.HasPayload();
}
+2 −0
Original line number Diff line number Diff line
@@ -85,4 +85,6 @@ class ParentDef : public TypeDef {

  std::map<std::string, std::variant<int64_t, std::string>> parent_constraints_;
  bool is_little_endian_;

  bool HasChildEnums() const;
};
+62 −0
Original line number Diff line number Diff line
//! Classic ACL facade

use crate::classic::AclManager;

module! {
    facade_module,
    providers {
        ClassicAclFacadeService => provide_facade,
    }
}

#[provides]
async fn provide_facade(acl: AclManager) -> ClassicAclFacadeService {
    ClassicAclFacadeService { acl }
}

pub struct ClassicAclFacadeService {
    acl: AclManager,
}

impl AclManagerFacade for ClassicAclFacadeService {
    fn create_connection(&mut self, _ctx: RpcContext<'_>, mut _data: ConnectionMsg, _sink: ServerStreamingSink<ConnectionEvent>) {
        unimplemented!();
    }

    fn cancel_connection(&mut self, _ctx: RpcContext<'_>, mut _data: ConnectionMsg, _sink: UnarySink<Empty>) {
        unimplemented!();
    }

    fn disconnect(&mut self, _ctx: RpcContext<'_>, mut _data: HandleMsg, _sink: UnarySink<Empty>) {
        unimplemented!();
    }

    fn disconnect(&mut self, _ctx: RpcContext<'_>, mut _data: PolicyMsg, _sink: UnarySink<Empty>) {
        unimplemented!();
    }

    fn authentication_requested(&mut self, _ctx: RpcContext<'_>, mut _data: HandleMsg, _sink: UnarySink<Empty>) {
        unimplemented!();
    }

    fn connection_command(&mut self, _ctx: RpcContext<'_>, mut _data: ConnectionCommandMsg, _sink: UnarySink<Empty>) {
        unimplemented!();
    }

    fn switch_role(&mut self, _ctx: RpcContext<'_>, mut _data: RoleMsg, _sink: UnarySink<Empty>) {
        unimplemented!();
    }

    fn send_acl_data(&mut self, _ctx: RpcContext<'_>, mut _data: AclData, _sink: UnarySink<Empty>) {
        unimplemented!();
    }

    fn fetch_acl_data(&mut self, _ctx: RpcContext<'_>, mut _data: HandleMsg, _sink: ServerStreamingSink<AclData>) {
        unimplemented!();
    }

    fn fetch_incoming_connection(&mut self, _ctx: RpcContext<'_>, mut _data: Empty, _sink: ServerStreamingSink<ConnectionEvent>) {
        unimplemented!();
    }
}