From 7145e2390c8c9f34b37143adcb6eaf9e0b97dec3 Mon Sep 17 00:00:00 2001
From: Connor Wood <connorwood71@gmail.com>
Date: Thu, 31 Aug 2017 11:25:08 +0100
Subject: [PATCH] Converted DDB handle to integer and vice versa

---
 src/acpi/aml/namespace.rs | 13 +++++++++++--
 src/acpi/mod.rs           | 27 +++++++++++++++++++++++++++
 2 files changed, 38 insertions(+), 2 deletions(-)

diff --git a/src/acpi/aml/namespace.rs b/src/acpi/aml/namespace.rs
index 6e6dfa9a..18eb7a34 100644
--- a/src/acpi/aml/namespace.rs
+++ b/src/acpi/aml/namespace.rs
@@ -12,7 +12,7 @@ use super::namedobj::{ RegionSpace, FieldFlags };
 use super::parser::{AmlExecutionContext, ExecutionState};
 use super::AmlError;
 
-use acpi::SdtSignature;
+use acpi::{SdtSignature, get_signature_from_index, get_index_from_signature};
 
 #[derive(Clone)]
 pub enum FieldSelector {
@@ -255,9 +255,13 @@ impl AmlValue {
     }
 
     pub fn get_as_ddb_handle(&self) -> Result<(Vec<String>, SdtSignature), AmlError> {
-        // TODO: Integer conversion
         match *self {
             AmlValue::DDBHandle(ref v) => Ok(v.clone()),
+            AmlValue::Integer(i) => if let Some(sig) = get_signature_from_index(i as usize) {
+                Ok((vec!(), sig))
+            } else {
+                Err(AmlError::AmlValueError)
+            },
             _ => Err(AmlError::AmlValueError)
         }
     }
@@ -317,6 +321,11 @@ impl AmlValue {
 
                 Ok(i)
             },
+            AmlValue::DDBHandle(ref v) => if let Some(idx) = get_index_from_signature(v.1.clone()) {
+                Ok(idx as u64)
+            } else {
+                Err(AmlError::AmlValueError)
+            },
             AmlValue::String(ref s) => {
                 let mut s = s.clone()[0..8].to_string().to_uppercase();
                 let mut i: u64 = 0;
diff --git a/src/acpi/mod.rs b/src/acpi/mod.rs
index 5902ebc5..8324f8e0 100644
--- a/src/acpi/mod.rs
+++ b/src/acpi/mod.rs
@@ -227,6 +227,33 @@ pub fn load_table(signature: SdtSignature) {
     }
 }
 
+pub fn get_signature_from_index(index: usize) -> Option<SdtSignature> {
+    if let Some(ref order) = *(SDT_ORDER.read()) {
+        if index < order.len() {
+            Some(order[index].clone())
+        } else {
+            None
+        }
+    } else {
+        None
+    }
+}
+
+pub fn get_index_from_signature(signature: SdtSignature) -> Option<usize> {
+    if let Some(ref order) = *(SDT_ORDER.read()) {
+        let mut i = order.len();
+        while i > 0 {
+            i -= 1;
+
+            if order[i] == signature {
+                return Some(i);
+            }
+        }
+    }
+
+    None
+}
+
 pub struct Acpi {
     pub fadt: RwLock<Option<Fadt>>,
     pub namespace: RwLock<Option<BTreeMap<String, AmlValue>>>,
-- 
GitLab