/* csc -o phonebooker.exe -lSystem phonebooker.cs */ using System; using System.IO; using System.Text; using System.IO.Ports; using System.Threading; using System.Collections; using System.Text.RegularExpressions; public class Driver { public static void Main(String[] args) { String rfdev = "RFCOMM1"; if(args.Length != 0) { rfdev = args[0]; } if(rfdev[0] == '/' || rfdev[rfdev.Length - 1] == '0') { Console.WriteLine("if you gave in an /dev/rfcomm0, it should be changed to a RFCOMM1\n coz this is .NET land, yo !"); } Phonebooker p = new Phonebooker(rfdev); p.Initialize(); IDictionary localphonebook = p.ReadPhonebook("ME"); IDictionary simphonebook = p.ReadPhonebook("SM"); p.Dispose(); foreach(DictionaryEntry d in localphonebook) { // cheat using bash term commands :) Console.WriteLine(d.Key + ": \x001b[20G" + d.Value); } foreach(DictionaryEntry d in simphonebook) { Console.WriteLine(d.Key + ": \x001b[20G" + d.Value); } } } public class Phonebooker : IDisposable { private SerialPort port = null; private Encoding encoding = new UTF8Encoding(); private static Regex CPBR_STATUS_PATTERN = new Regex(@"\+CPBR: \(([0-9])+-([0-9]+)\),([0-9]*),([0-9]*)"); private static Regex CPBR_DATA_PATTERN = new Regex("\\+CPBR: ([0-9]*),\"([^\"]*)\",([0-9]*),\"([^\"]*)\""); public Phonebooker(String device) { port = new SerialPort(device, 38400); } public void Dispose() { port.Close(); port = null; } public void Initialize() { port.Open(); ATCommand("ATZ"); ATCommand("AT+CSCS=\"8859-1\""); } public String[] ATCommand(String command) { return ATCommand(command,true); } public String[] ATCommand(String command, bool ok) { String throwaway = port.ReadExisting(); if(throwaway != null && throwaway.Length > 0) { debug(""); // throw away } debug(">"+command); port.Write(command+"\r"); Thread.Sleep(2); // sleep 2 secs int b = port.BytesToRead; ArrayList data = new ArrayList(); while(ok || b > 0) { b = port.BytesToRead; String line = port.ReadLine(); debug("<"+line); data.Add(line); if(ok && line.StartsWith("ERROR")) { break; } if(ok && line[0] == 'O' && line[1] == 'K' && (line[2] == '\r' || line[2] == '\n')) { break; } } return (String[])data.ToArray(typeof(String)); } public IDictionary ReadPhonebook(String type) { int start, end; int threshold = 8; // exit after these many continous blank entries Hashtable book = new Hashtable(); ATCommand("AT+CPBS=\""+type+"\""); String[] data; data = ATCommand("AT+CPBR=?"); // list of data foreach(String s in data) { Match m = CPBR_STATUS_PATTERN.Match(s); if(m.Success) { start = Int32.Parse(m.Groups[1].Value); end = Int32.Parse(m.Groups[2].Value); } } for(; start < end && threshold > 0 ; start++) { Console.Write(type + " ............" + start +" \r"); data = ATCommand("AT+CPBR="+start); String name, number; bool valid = false; foreach(String s in data) { Match m = CPBR_DATA_PATTERN.Match(s); if(m.Success) { number = m.Groups[2].Value; name = m.Groups[4].Value; book[name] = number; valid = true; break; } } if(!valid) { threshold--; } else { threshold = (int)Math.Min((end - start)/20,8); } } Console.WriteLine(); return book; } public static void debug(Object b) { Console.WriteLine(b); } }