//-------- //-------- Constructors //-------- /// <summary> /// Constructs a new u packet builder. /// </summary> /// <param name="startUState"> the object that contains the U brick state /// which is reference when creating packets </param> public UPacketBuilder(UAdapterState startUState) { // get a reference to the U state uState = startUState; // create the buffer for the data packet = new RawSendPacket(); // create the vector packetsVector = new List <RawSendPacket>(); // restart the packet to initialize restart(); // no bit banging supported bitsOnly = false; // check for a bits only property string bits = OneWireAccessProvider.getProperty("onewire.serial.forcebitsonly"); if (!string.ReferenceEquals(bits, null)) { if (bits.IndexOf("true", StringComparison.Ordinal) != -1) { bitsOnly = true; } else if (bits.IndexOf("false", StringComparison.Ordinal) != -1) { bitsOnly = false; } } }
private void GetProperties() { string test = OneWireAccessProvider.getProperty("onewire.adapter.default"); if (!string.ReferenceEquals(test, null)) { m_adapterName = test; } test = OneWireAccessProvider.getProperty("onewire.port.default"); if (!string.ReferenceEquals(test, null)) { m_adapterPort = test; } test = OneWireAccessProvider.getProperty("NetAdapter.ListenPort"); if (!string.ReferenceEquals(test, null)) { m_listenPort = test; } test = OneWireAccessProvider.getProperty("NetAdapter.Multithread"); if (!string.ReferenceEquals(test, null)) { m_multithread = Convert.ToBoolean(test); } test = OneWireAccessProvider.getProperty("NetAdapter.Secret"); if (!string.ReferenceEquals(test, null)) { m_secret = test; } test = OneWireAccessProvider.getProperty("NetAdapter.Multicast"); if (!string.ReferenceEquals(test, null)) { m_multicast = Convert.ToBoolean(test); } test = OneWireAccessProvider.getProperty("NetAdapter.MulticastPort"); if (!string.ReferenceEquals(test, null)) { m_mcPort = int.Parse(test); } test = OneWireAccessProvider.getProperty("NetAdapter.MulticastGroup"); if (!string.ReferenceEquals(test, null)) { m_mcGroup = test; } }
private void SetupLogging() { string enable = OneWireAccessProvider.getProperty("onewire.debug"); if (!string.ReferenceEquals(enable, null) && enable.ToLower().Equals("true")) { DEBUG = true; } else { DEBUG = false; } string logFile = OneWireAccessProvider.getProperty("onewire.debug.logfile"); if (!string.ReferenceEquals(logFile, null)) { if (DEBUG) { // ignore any absolute path provided, only use filename string[] strtok = logFile.Split(new char[] { '\\' }); StorageFolder localFolder = ApplicationData.Current.LocalFolder; string logFilePath = localFolder.Path + "\\" + strtok[strtok.Length - 1].Trim(); Debug.WriteLine("Log location: " + ApplicationData.Current.LocalFolder.Path); EventListener listener = new StorageFileEventListener(logFile); listener.EnableEvents(OneWireEventSource.Log, EventLevel.Verbose); } else { Debug.WriteLine("Log location: " + ApplicationData.Current.LocalFolder.Path); EventListener listener = new StorageFileEventListener("log"); listener.EnableEvents(OneWireEventSource.Log, EventLevel.Informational); } } else { Debug.WriteLine("Log location: " + ApplicationData.Current.LocalFolder.Path); EventListener listener = new StorageFileEventListener("log"); listener.EnableEvents(OneWireEventSource.Log, EventLevel.Informational); } }
/// <summary> /// Static constructor. Checks system properties to see if debugging /// is enabled by default. Also, will redirect debug output to a log /// file if specified. /// </summary> static Debug() { string enable = OneWireAccessProvider.getProperty("onewire.debug"); if (!string.ReferenceEquals(enable, null) && enable.ToLower().Equals("true")) { DEBUG = true; } else { DEBUG = false; } if (DEBUG) { string logFile = OneWireAccessProvider.getProperty("onewire.debug.logfile"); if (!string.ReferenceEquals(logFile, null)) { // ignore any absolute path provided, only use filename string[] strtok = logFile.Split(new char[] { '\\' }); StorageFolder localFolder = ApplicationData.Current.LocalFolder; string logFilePath = localFolder.Path + "\\" + strtok[strtok.Length - 1].Trim(); System.Diagnostics.Debug.WriteLine(logFilePath); try { @out = new StreamWriter(new FileStream(logFilePath, FileMode.Create, FileAccess.Write)); @out.AutoFlush = true; } catch (System.Exception e) { @out = null; DEBUG = false; debug("Error opening log file in Debug Static Constructor", e); } } } }
/// <summary> /// Method main /// /// </summary> /// <param name="args"> /// </param> /// <exception cref="OneWireException"> </exception> /// <exception cref="OneWireIOException"> /// </exception> public static void Main2(string[] args) { //coprocessor long coprID = 0; // attempt to open the sha.properties file sha_properties = new Properties(); if (!sha_properties.loadLocalFile("sha.properties")) { Debug.WriteLine("loading default sha.properties!"); Assembly asm = typeof(SHADebitDemo.MainPage).GetTypeInfo().Assembly; sha_properties.loadResourceFile(asm, "SHADebitDemo.sha.properties"); } // ------------------------------------------------------------ // Instantiate coprocessor containers // ------------------------------------------------------------ SHAiButtonCopr copr = null; OneWireContainer18 copr18 = new OneWireContainer18(); //TODO copr18.setSpeed(DSPortAdapter.SPEED_OVERDRIVE, false); copr18.SpeedCheck = false; // ------------------------------------------------------------ // Setup the adapter for the coprocessor // ------------------------------------------------------------ DSPortAdapter coprAdapter = null; string coprAdapterName = null, coprPort = null; try { coprAdapterName = OneWireAccessProvider.getProperty("copr.adapter"); coprPort = OneWireAccessProvider.getProperty("copr.port"); if (string.ReferenceEquals(coprPort, null) || string.ReferenceEquals(coprAdapterName, null)) { coprAdapter = OneWireAccessProvider.DefaultAdapter; } else { coprAdapter = OneWireAccessProvider.getAdapter(coprAdapterName, coprPort); } Debug.WriteLine("Coprocessor adapter loaded, adapter: " + coprAdapter.AdapterName + " port: " + coprAdapter.PortName); coprAdapter.adapterDetected(); coprAdapter.targetFamily(0x18); coprAdapter.beginExclusive(true); coprAdapter.reset(); coprAdapter.setSearchAllDevices(); coprAdapter.reset(); coprAdapter.putByte(0x3c); //TODO coprAdapter.Speed = DSPortAdapter.SPEED_OVERDRIVE; } catch (Exception e) { Debug.WriteLine("Error initializing coprocessor adapter"); Debug.WriteLine(e.ToString()); Debug.Write(e.StackTrace); return; } // ------------------------------------------------------------ // Find the coprocessor // ------------------------------------------------------------ if (sha_properties.getPropertyBoolean("copr.simulated.isSimulated", false)) { string coprVMfilename = sha_properties.getProperty("copr.simulated.filename"); // --------------------------------------------------------- // Load emulated coprocessor // --------------------------------------------------------- try { copr = new SHAiButtonCoprVM(coprVMfilename); } catch (Exception e) { IOHelper.writeLine("Invalid Coprocessor Data File"); Debug.WriteLine(e.ToString()); Debug.Write(e.StackTrace); return; } } else { // --------------------------------------------------------- // Get the name of the coprocessor service file // --------------------------------------------------------- string filename = sha_properties.getProperty("copr.filename", "COPR.0"); // --------------------------------------------------------- // Check for hardcoded coprocessor address // --------------------------------------------------------- byte[] coprAddress = sha_properties.getPropertyBytes("copr.address", null); long lookupID = 0; if (coprAddress != null) { lookupID = Address.toLong(coprAddress); IOHelper.write("Looking for coprocessor: "); IOHelper.writeLineHex(lookupID); } // --------------------------------------------------------- // Find hardware coprocessor // --------------------------------------------------------- try { bool next = coprAdapter.findFirstDevice(); while (copr == null && next) { try { long tmpCoprID = coprAdapter.AddressAsLong; if (coprAddress == null || tmpCoprID == lookupID) { Debug.WriteLine("Loading coprocessor file: " + filename + " from device: " + tmpCoprID.ToString("X")); //IOHelper.writeLineHex(tmpCoprID); copr18.setupContainer(coprAdapter, tmpCoprID); copr = new SHAiButtonCopr(copr18, filename); //save coprocessor ID coprID = tmpCoprID; } } catch (Exception e) { IOHelper.writeLine(e); } next = coprAdapter.findNextDevice(); } } catch (Exception) { ; } } if (copr == null) { IOHelper.writeLine("No Coprocessor found!"); return; } Debug.WriteLine(copr); Debug.WriteLine(""); // ------------------------------------------------------------ // Create the SHADebit transaction types // ------------------------------------------------------------ //stores DS1963S transaction data SHATransaction trans = null; string transType = sha_properties.getProperty("transaction.type", "SignedDebit"); if (transType.ToLower().Equals("unsigneddebit")) { trans = new SHADebitUnsigned(copr, 10000, 50); } else { trans = new SHADebit(copr, 10000, 50); } // ------------------------------------------------------------ // Create the User Buttons objects // ------------------------------------------------------------ //holds DS1963S user buttons OneWireContainer18 owc18 = new OneWireContainer18(); //TODO owc18.setSpeed(DSPortAdapter.SPEED_OVERDRIVE, false); owc18.SpeedCheck = false; // ------------------------------------------------------------ // Get the adapter for the user // ------------------------------------------------------------ DSPortAdapter adapter = null; string userAdapterName = null, userPort = null; try { userAdapterName = OneWireAccessProvider.getProperty("user.adapter"); userPort = OneWireAccessProvider.getProperty("user.port"); if (string.ReferenceEquals(userPort, null) || string.ReferenceEquals(userAdapterName, null)) { if (!string.ReferenceEquals(coprAdapterName, null) && !string.ReferenceEquals(coprPort, null)) { adapter = OneWireAccessProvider.DefaultAdapter; } else { adapter = coprAdapter; } } else if (userAdapterName.Equals(coprAdapterName) && userPort.Equals(coprPort)) { adapter = coprAdapter; } else { adapter = OneWireAccessProvider.getAdapter(userAdapterName, userPort); } Debug.WriteLine("User adapter loaded, adapter: " + adapter.AdapterName + " port: " + adapter.PortName); byte[] families = new byte[] { 0x18 }; adapter.adapterDetected(); adapter.targetFamily(families); adapter.beginExclusive(false); adapter.reset(); adapter.setSearchAllDevices(); adapter.reset(); adapter.putByte(0x3c); //TODO adapter.Speed = DSPortAdapter.SPEED_OVERDRIVE; } catch (Exception e) { IOHelper.writeLine("Error initializing user adapter."); Debug.WriteLine(e.ToString()); Debug.Write(e.StackTrace); return; } // // // try { long tmpID = -1; bool next = adapter.findFirstDevice(); for (; tmpID == -1 && next; next = adapter.findNextDevice()) { tmpID = adapter.AddressAsLong; if (tmpID == coprID) { tmpID = -1; } else { owc18.setupContainer(adapter, tmpID); } } if (tmpID == -1) { IOHelper.writeLine("No buttons found!"); return; } } catch (Exception) { IOHelper.writeLine("Adapter error while searching."); return; } IOHelper.write("Setting up user button: "); IOHelper.writeBytesHex(owc18.Address); IOHelper.writeLine("How would you like to enter the authentication secret (unlimited bytes)? "); byte[] auth_secret = getBytes(0); IOHelper.writeBytes(auth_secret); IOHelper.writeLine(""); if (copr.DS1961Scompatible) { auth_secret = SHAiButtonCopr.reformatFor1961S(auth_secret); IOHelper.writeLine("Reformatted for compatibility with 1961S buttons"); IOHelper.writeBytes(auth_secret); IOHelper.writeLine(""); } IOHelper.writeLine("Initial Balance in Cents? "); int initialBalance = IOHelper.readInt(100); trans.setParameter(SHADebit.INITIAL_AMOUNT, initialBalance); SHAiButtonUser user = new SHAiButtonUser18(copr, owc18, true, auth_secret); if (trans.setupTransactionData(user)) { IOHelper.writeLine("Transaction data installation succeeded"); } else { IOHelper.writeLine("Failed to initialize transaction data"); } IOHelper.writeLine(user.ToString()); }