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

Commit 9ef4b58a authored by JohnLai's avatar JohnLai
Browse files

Floss: Allow set_pin up to 16 digits

The pin code is not limited to 4 digits.
Make it support up to 16 digits.

Add a utility function for converting vector to fixed size
array.

Bug: 269077436
Test: Manually
Tag: #floss
Change-Id: I342088e6da15134366c80cf9002a1a67687fd30f
parent 4c39f403
Loading
Loading
Loading
Loading
+3 −5
Original line number Diff line number Diff line
@@ -772,12 +772,10 @@ impl CommandHandler {
                    name: String::from(""),
                };
                let pin = get_arg(args, 2)?;
                let (accept, pin) = match (&pin[..], String::from(pin).parse::<u32>()) {
                    (_, Ok(p)) => (true, Vec::from(p.to_ne_bytes())),

                let (accept, pin) = match (&pin[..], pin) {
                    ("reject", _) => (false, vec![]),
                    _ => {
                        return Err(format!("Failed to parse '{}'", pin).into());
                    }
                    (_, p) => (true, p.as_bytes().iter().cloned().collect::<Vec<u8>>()),
                };

                self.lock_context().adapter_dbus.as_mut().unwrap().set_pin(
+3 −2
Original line number Diff line number Diff line
@@ -18,11 +18,13 @@ use bt_topshim::{
    topstack,
};

use bt_utils::array_utils;
use btif_macros::{btif_callback, btif_callbacks_dispatcher};

use log::{debug, warn};
use num_traits::cast::ToPrimitive;
use std::collections::HashMap;
use std::convert::TryInto;
use std::fs::File;
use std::hash::Hash;
use std::io::Write;
@@ -1597,8 +1599,7 @@ impl IBluetooth for Bluetooth {
            return false;
        }

        let mut btpin: BtPinCode = BtPinCode { pin: [0; 16] };
        btpin.pin.copy_from_slice(pin_code.as_slice());
        let mut btpin = BtPinCode { pin: array_utils::to_sized_array(&pin_code) };

        self.intf.lock().unwrap().pin_reply(
            &addr.unwrap(),
+2 −8
Original line number Diff line number Diff line
@@ -14,6 +14,7 @@ use bt_topshim::profiles::gatt::{
};
use bt_topshim::topstack;
use bt_utils::adv_parser;
use bt_utils::array_utils;

use crate::async_helper::{AsyncHelper, CallbackSender};
use crate::bluetooth::{Bluetooth, IBluetooth};
@@ -2485,14 +2486,7 @@ impl IBluetoothGatt for BluetoothGatt {
            let handle = self.server_context_map.get_request_handle_from_id(request_id)?;
            let len = value.len() as u16;

            let data: [u8; 600] = value
                .iter()
                .chain(std::iter::repeat(&0))
                .take(600)
                .cloned()
                .collect::<Vec<u8>>()
                .try_into()
                .ok()?;
            let data: [u8; 600] = array_utils::to_sized_array(&value);

            self.gatt.as_ref().unwrap().lock().unwrap().server.send_response(
                conn_id,
+10 −0
Original line number Diff line number Diff line
//! This library provides array utils.

/// Converts a vector of bytes to a fixed sized array.
/// If the vector is longer it will be truncated and if shorter
/// zero padding will be added at the end.
pub fn to_sized_array<const S: usize>(v: &Vec<u8>) -> [u8; S] {
    // Okay to do naked unwrap since we enforce at compile time that
    // the iter length is the same as the destination array length.
    v.iter().chain(std::iter::repeat(&0)).take(S).cloned().collect::<Vec<u8>>().try_into().unwrap()
}
+1 −0
Original line number Diff line number Diff line
//! Utilities

pub mod adv_parser;
pub mod array_utils;
pub mod socket;
pub mod uinput;