From 77624bc5673da5e3407111c5bd6b1ff4f4221289 Mon Sep 17 00:00:00 2001 From: lupyuen Date: Sun, 5 Jul 2020 11:55:34 +0800 Subject: [PATCH] Output MAC Address and Manufacturer Data --- decode-base64/Cargo.lock | 9 ++- decode-base64/Cargo.toml | 3 +- decode-base64/src/main.rs | 151 ++++++++++++++++++++++---------------- 3 files changed, 98 insertions(+), 65 deletions(-) diff --git a/decode-base64/Cargo.lock b/decode-base64/Cargo.lock index 144478200..499e4885f 100644 --- a/decode-base64/Cargo.lock +++ b/decode-base64/Cargo.lock @@ -7,8 +7,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3441f0f7b02788e948e47f457ca01f1d7e6d92c693bc132c22b087d3141c03ff" [[package]] -name = "json-to-rss" +name = "decode-base64" version = "0.1.0" dependencies = [ "base64", + "hex", ] + +[[package]] +name = "hex" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "644f9158b2f133fd50f5fb3242878846d9eb792e445c893805ff0e3824006e35" diff --git a/decode-base64/Cargo.toml b/decode-base64/Cargo.toml index c8f33adbe..bbabfc8b0 100644 --- a/decode-base64/Cargo.toml +++ b/decode-base64/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "json-to-rss" +name = "decode-base64" version = "0.1.0" authors = ["lupyuen "] edition = "2018" @@ -8,3 +8,4 @@ edition = "2018" [dependencies] base64 = "0.12.3" +hex = "0.4.2" \ No newline at end of file diff --git a/decode-base64/src/main.rs b/decode-base64/src/main.rs index adba5ebf8..4998ff4a5 100644 --- a/decode-base64/src/main.rs +++ b/decode-base64/src/main.rs @@ -1,80 +1,105 @@ fn main() { - let lines = std::fs::read_to_string("../../Downloads/20200704.txt").unwrap(); + let lines = std::fs::read_to_string("../../Downloads/20200705.txt").unwrap(); + let mut mac = Vec::new(); + let mut manu = Vec::new(); for line in lines.split("\n") { // line contains b20200704-125008.txt: Value 7b226964223a2... | "{\"id\":\"nRZAdXp...Tg==\",\"mp\":\"SM-G970F\",\"o\":\"SG_MOH\",\"v\":2}" - // println!("line = {:?}", line); - if line.len() == 0 { continue; } + if line.len() == 0 || !line.contains(":") { continue; } + let datetime = line .split(".") .collect::>() [0] .replace("b", ""); + // println!("datetime = {:?}", datetime); + let line = + line + .splitn(2, ":") + .collect::>() + [1]; + if line.starts_with("Scan") + || line.starts_with("Connect") + { continue; } + + // line contains Value 7b226964223a2... | "{\"id\":\"nRZAdXp...Tg==\",\"mp\":\"SM-G970F\",\"o\":\"SG_MOH\",\"v\":2}" + // or Value 28dc2dfb72... | \"(\\xdc-\\xfb...\\xe8\" + // or [49:b7:00:00:00:00] RSSI -96: + // or Manu: FF03393161 + // println!("line = {:?}", line); + if line.starts_with("[") { + // line contains [49:b7:00:00:00:00] RSSI -96: + let encoded = line + .split(" ") + .collect::>() + [0] + .replace(":", "") + .replace("[", "") + .replace("]", ""); + // encoded contains 49b700000000 + mac = hex::decode(encoded).unwrap(); + continue; + } + if line.starts_with("Manu: ") { + // line contains Manu: FF03393161 + let encoded = line + .split(" ") + .collect::>() + [1]; + // encoded contains FF03393161 + manu = hex::decode(encoded).unwrap(); + continue; + } let encoded = line .split("==") .collect::>() [0] .to_string() + "=="; - let encoded = encoded - .split("|") - .collect::>() - [1]; - let encoded = encoded - .split(":") - .collect::>() - [1] - .replace("\\\"", ""); - let decoded = base64::decode(encoded.clone()); - // println!("datetime = {:?}", datetime); - // println!("encoded = {:?}", encoded); - // println!("decoded = {:?}", decoded); - println!("{:?},{:?}", datetime, decoded); + // Get the Base64 decoded value, or if not Base64 encoded, just dump the value + let mut decoded = Vec::new(); + if encoded.contains("|") { + if encoded.contains("id") { + // encoded contains Value 7b226964223a2... | "{\"id\":\"nRZAdXp...Tg== + let encoded = encoded + .split("|") + .collect::>() + [1]; + // encoded contains "{\"id\":\"nRZAdXp...Tg== + if encoded.contains(":") { + let encoded = encoded + .split(":") + .collect::>() + [1] + .replace("\\\"", ""); + // encoded contains nRZAdXp...Tg== + decoded = base64::decode(encoded.clone()).unwrap(); + } + } else { + // encoded contains Value 28dc2dfb72... | \"(\\xdc-\\xfb...\\xe8\" + let encoded = encoded + .split("|") + .collect::>() + [0]; + // encoded contains Value 28dc2dfb72... + if encoded.contains("Value") { + let encoded = encoded + .splitn(2, "Value") + .collect::>() + [1] + .replace(" ", ""); + // println!("encoded = {:?}", encoded); + decoded = hex::decode(encoded).unwrap(); + } + } + } + if decoded.len() > 0 + && mac.len() > 0 + && manu.len() > 0 + { + println!("{:?},{:?},{:?},{:?}", datetime, mac, manu, decoded); + mac = Vec::new(); + manu = Vec::new(); + } } - /* - let deserialized: Resume = serde_json::from_str(&json).unwrap(); - // println!("deserialized = {:?}", deserialized); - - // Convert each publication. - let mut items = Vec::new(); - for article in deserialized.publications { - // Convert date. - let date = Utc.datetime_from_str( - &(article.releaseDate + " 00:00:00"), - "%Y-%m-%d %H:%M:%S" - ).unwrap(); // "2018-03-20" - // println!("{}", date); - - /* - - Example entry - Here is some text containing an interesting description. - http://www.example.com/blog/post/1 - 7bd204c6-1655-4c27-aeee-53f933c5395f - Sun, 06 Sep 2009 16:20:00 +0000 - - */ - // Compose the item. - let item = ItemBuilder::default() - .title(Some(article.name)) - .description(Some(article.summary)) - .link(Some(article.website)) - .pub_date(Some(date.to_rfc2822())) - .build() - .unwrap(); - items.push(item); - } - // Compose the channel. - let channel = ChannelBuilder::default() - .title("lupyuen") - .link("https://lupyuen.github.io") - .description("IoT Techie and Educator") - .items(items) - .build() - .unwrap(); - - // Write the channel. - let string = channel.to_string(); // convert the channel to a string - println!("{}", string); - */ }