Esempio n. 1
0
        /// <summary>
        /// <P>Asserts that proper account info exists for this SHAiButtonUser
        /// and establishes a buffer for caching account data.</P>
        /// </summary>
        /// <param name="ibc"> the OneWireContainer whose account info is checked </param>
        /// <returns> whether or not the device is initialized properly </returns>
        protected internal virtual bool checkAccountPageInfo(OneWireContainer ibc)
        {
            lock (this)
            {
                //this flag should only be set if there is valid data
                if (accountPageNumber <= 0)
                {
                    try
                    {
                        //create a file object representing service file
                        OWFile owf = new OWFile(ibc, strServiceFilename);

                        //check to see if file exists
                        if (!owf.exists())
                        {
                            return(false);
                        }

                        //get the page number for the file
                        //this.accountPageNumber = owf.getPageList()[0];
                        this.accountPageNumber = owf.StartPage;

                        //close the file
                        owf.close();

                        //mark the cache as dirty
                        this.accountData[0] = 0;

                        //clear the write cycle counter
                        this.writeCycleCounter = -1;
                    }
                    catch (System.Exception e)
                    {
                        this.accountPageNumber = -1;
                        if (DEBUG)
                        {
                            OneWireEventSource.Log.Debug(e.ToString());
                            OneWireEventSource.Log.Debug(e.StackTrace);
                        }
                    }
                }

                return(this.accountPageNumber > 0);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// <P>Create's empty service file for the user with the given filename.
        /// Also, it populates <code>accountPageNumber</code> with the page
        /// that the service file is stored on.</P>
        /// </summary>
        /// <param name="owc"> the 1-wire device where the service file will be created. </param>
        /// <param name="filename"> the filename of the service file. </param>
        /// <param name="formatDevice"> if <code>true</code>, the device is formatted
        ///        before creating the service file. </param>
        protected internal virtual bool createServiceFile(OneWireContainer owc, string filename, bool formatDevice)
        {
            bool   bRetVal = false;
            OWFile owf     = new OWFile(owc, strServiceFilename);

            try
            {
                if (formatDevice)
                {
                    owf.format();
                }

                if (!owf.exists())
                {
                    owf.createNewFile();
                }

                //save reference to page number for service file
                this.accountPageNumber = owf.PageList[0];

                bRetVal = true;
            }
            catch (IOException ioe)
            {
                bRetVal = false;

                OneWireEventSource.Log.Debug(ioe.ToString());
                OneWireEventSource.Log.Debug(ioe.StackTrace);
            }

            try
            {
                owf.close();
                return(bRetVal);
            }
            catch (IOException ioe)
            {
                OneWireEventSource.Log.Debug(ioe.ToString());
                OneWireEventSource.Log.Debug(ioe.StackTrace);

                /*well, at least I tried!*/
                return(false);
            }
        }
Esempio n. 3
0
    /// <summary>
    /// Main for 1-Wire File Shell (OWFish)
    /// </summary>
    public static void Main1(string[] args)
    {
        List <OneWireContainer> owd_vect = new List <OneWireContainer>(5);

        OneWireContainer[] owd = null;
        DSPortAdapter      adapter = null;
        int        selection, len;
        long       start_time, end_time;
        Stopwatch  stopWatch = new Stopwatch();
        FileStream fos;
        Stream     fis;
//	  FileDescriptor fd;
        OWFileOutputStream owfos;
        OWFileInputStream  owfis;
        OWFileDescriptor   owfd;
        OWFile             owfile, new_owfile;

        byte[] block = new byte[32];

        Debug.WriteLine("");
        Debug.WriteLine("1-Wire File Shell (OWFish): Version 0.00");
        Debug.WriteLine("");

        // load the simulated console input stream
        Stream stream = loadResourceFile("OWFish.input.txt");

        dis = new StreamReader(stream);

        stopWatch.Start();

        try
        {
            // get the default adapter
            adapter = OneWireAccessProvider.DefaultAdapter;

            // adapter driver info
            Debug.WriteLine("=========================================================================");
            Debug.WriteLine("== Adapter Name: " + adapter.AdapterName);
            Debug.WriteLine("== Adapter Port description: " + adapter.PortTypeDescription);
            Debug.WriteLine("== Adapter Version: " + adapter.AdapterVersion);
            Debug.WriteLine("== Adapter support overdrive: " + adapter.canOverdrive());
            Debug.WriteLine("== Adapter support hyperdrive: " + adapter.canHyperdrive());
            Debug.WriteLine("== Adapter support EPROM programming: " + adapter.canProgram());
            Debug.WriteLine("== Adapter support power: " + adapter.canDeliverPower());
            Debug.WriteLine("== Adapter support smart power: " + adapter.canDeliverSmartPower());
            Debug.WriteLine("== Adapter Class Version: " + adapter.ClassVersion);

            // get exclusive use of adapter
            adapter.beginExclusive(true);

            // loop to do menu
            selection = MAIN_SELECT_DEVICE;
            do
            {
                start_time = 0;
                try
                {
                    switch (selection)
                    {
                    case MAIN_SELECT_DEVICE:
                        // find all parts
                        owd_vect = findAllDevices(adapter);
                        // select a device
                        owd = selectDevice(owd_vect);
                        // check for quite
                        if (owd == null)
                        {
                            selection = MAIN_QUIT;
                        }
                        else
                        {
                            // display device info
                            Debug.WriteLine("");
                            Debug.WriteLine("  Device(s) selected: ");
                            printDeviceInfo(owd, false);
                        }
                        break;

                    case MAIN_FORMAT:
                        if (menuSelect(verifyMenu) == VERIFY_YES)
                        {
                            // start time of operation
                            start_time = stopWatch.ElapsedMilliseconds;
                            // create a 1-Wire file at root
                            owfile = new OWFile(owd, "");
                            // format Filesystem
                            owfile.format();
                            // get 1-Wire File descriptor to flush to device
                            owfd = owfile.FD;
                            syncFileDescriptor(owfd);
                            // close the 1-Wire file to release
                            owfile.close();
                        }
                        break;

                    case MAIN_LIST:
                        Debug.Write("Enter the directory to list on (/ for root): ");
                        // get the directory and create a file on it
                        owfile = new OWFile(owd, getString(1));
                        Debug.WriteLine("");
                        // start time of operation
                        start_time = stopWatch.ElapsedMilliseconds;
                        // list the files without recursion
                        listDir(owfile, 1, false);
                        // close the 1-Wire file to release
                        owfile.close();
                        break;

                    case MAIN_RLIST:
                        // start time of operation
                        start_time = stopWatch.ElapsedMilliseconds;
                        // get the directory and create a file on it
                        owfile = new OWFile(owd, "");
                        Debug.WriteLine("");
                        // recursive list
                        listDir(owfile, 1, true);
                        // close the 1-Wire file to release
                        owfile.close();
                        break;

                    case MAIN_MKDIR:
                        Debug.Write("Enter the directory to create (from root): ");
                        // get the directory and create a file on it
                        owfile = new OWFile(owd, getString(1));
                        // start time of operation
                        start_time = stopWatch.ElapsedMilliseconds;
                        // make the directories
                        if (owfile.mkdirs())
                        {
                            Debug.WriteLine("Success!");
                        }
                        else
                        {
                            Debug.WriteLine("-----------------------------------------------");
                            Debug.WriteLine("Could not create directories, out of memory or invalid directory/file");
                            Debug.WriteLine("-----------------------------------------------");
                        }
                        // get 1-Wire File descriptor to flush to device
                        owfd = owfile.FD;
                        syncFileDescriptor(owfd);
                        // close the 1-Wire file to release
                        owfile.close();
                        break;

                    case MAIN_COPYTO:
                        // system SOURCE file
                        Debug.Write("Enter the path/file of the SOURCE file on the system: ");
//TODO					 fis = new FileStream(getString(1), FileMode.Open, FileAccess.Read);
                        fis = loadResourceFile(getString(1));
                        // 1-Wire DESTINATION file
                        Debug.Write("Enter the path/file of the DESTINATION on 1-Wire device: ");
                        owfos = new OWFileOutputStream(owd, getString(1));
                        // start time of operation
                        start_time = stopWatch.ElapsedMilliseconds;
                        // loop to copy block from SOURCE to DESTINATION
                        do
                        {
                            len = fis.Read(block, 0, block.Length);
                            if (len > 0)
                            {
                                owfos.write(block, 0, len);
                            }
                        } while (len > 0);
                        // get 1-Wire File descriptor to flush to device
                        owfd = owfos.FD;
                        syncFileDescriptor(owfd);
                        // close the files
                        owfos.Close();
                        fis.Dispose();
                        break;

                    case MAIN_COPYFROM:
                        // 1-Wire SOURCE file
                        Debug.Write("Enter the path/file of the SOURCE file on 1-Wire device: ");
                        owfis = new OWFileInputStream(owd, getString(1));
                        // system DESTINATION file
                        Debug.Write("Enter the path/file of the DESTINATION on system: ");
                        StorageFolder localFolder = ApplicationData.Current.LocalFolder;
                        string        outp        = localFolder.Path + "\\" + getString(1);
                        fos = new FileStream(outp, FileMode.Create, FileAccess.Write);
                        // start time of operation
                        start_time = stopWatch.ElapsedMilliseconds;
                        // loop to copy block from SOURCE to DESTINATION
                        do
                        {
                            len = owfis.read(block);
                            if (len > 0)
                            {
                                fos.Write(block, 0, len);
                            }
                        } while (len > 0);
                        // get 1-Wire File descriptor to flush to device
                        fos.Flush();
                        // close the files
                        owfis.close();
                        fos.Dispose();
                        break;

                    case MAIN_CAT:
                        // 1-Wire file
                        Debug.Write("Enter the path/file of the file to display: ");
                        owfis = new OWFileInputStream(owd, getString(1));
                        // start time of operation
                        start_time = stopWatch.ElapsedMilliseconds;
                        Debug.WriteLine("");
                        Debug.WriteLine("---FILE START---");
                        // loop to read and display file
                        do
                        {
                            len = owfis.read(block);
                            if (len > 0)
                            {
                                Debug.Write(System.Text.Encoding.UTF8.GetString(block));
                                com.dalsemi.onewire.debug.Debug.debug("file cat", block, 0, len);
                            }
                        } while (len > 0);
                        Debug.WriteLine("");
                        Debug.WriteLine("---FILE END---");
                        // close the file
                        owfis.close();
                        break;

                    case MAIN_DELETE:
                        Debug.Write("Enter the directory/file delete: ");
                        // get the directory and create a file on it
                        owfile = new OWFile(owd, getString(1));
                        Debug.WriteLine("");
                        // start time of operation
                        start_time = stopWatch.ElapsedMilliseconds;
                        // delete the directory/file
                        if (owfile.delete())
                        {
                            Debug.WriteLine("Success!");
                        }
                        else
                        {
                            Debug.WriteLine("-----------------------------------------------");
                            Debug.WriteLine("Could not delete, if it is a directory make sure it is empty");
                            Debug.WriteLine("-----------------------------------------------");
                        }
                        // get 1-Wire File descriptor to flush to device
                        owfd = owfile.FD;
                        syncFileDescriptor(owfd);
                        // close the 1-Wire file to release
                        owfile.close();
                        break;

                    case MAIN_RENAME:
                        Debug.Write("Enter the OLD directory/file name: ");
                        // get the directory and create a file on it
                        owfile = new OWFile(owd, getString(1));
                        Debug.WriteLine("");
                        Debug.Write("Enter the NEW directory/file name: ");
                        // get the directory and create a file on it
                        new_owfile = new OWFile(owd, getString(1));
                        Debug.WriteLine("");
                        // start time of operation
                        start_time = stopWatch.ElapsedMilliseconds;
                        // rename the directory/file
                        if (owfile.renameTo(new_owfile))
                        {
                            Debug.WriteLine("Success!");
                        }
                        else
                        {
                            Debug.WriteLine("-----------------------------------------------");
                            Debug.WriteLine("Could not rename, make sure parents of new directory exist");
                            Debug.WriteLine("-----------------------------------------------");
                        }
                        // get 1-Wire File descriptor to flush to device
                        owfd = owfile.FD;
                        syncFileDescriptor(owfd);
                        // close the 1-Wire file to release
                        owfile.close();
                        new_owfile.close();
                        break;

                    case MAIN_DETAILS:
                        Debug.Write("Enter the directory/file to view details: ");
                        // get the directory and create a file on it
                        owfile = new OWFile(owd, getString(1));
                        Debug.WriteLine("");
                        // start time of operation
                        start_time = stopWatch.ElapsedMilliseconds;
                        // show the file details
                        showDetails(owfile);
                        // close the 1-Wire file to release
                        owfile.close();
                        break;

                    case MAIN_FREEMEM:
                        // start time of operation
                        start_time = stopWatch.ElapsedMilliseconds;
                        // create a 1-Wire file at root
                        owfile = new OWFile(owd, "");
                        // get free memory
                        Debug.WriteLine("");
                        Debug.WriteLine("  free memory: " + owfile.FreeMemory + " (bytes)");
                        // get the devices participating
                        owd = owfile.OneWireContainers;
                        Debug.WriteLine("");
                        Debug.WriteLine("  Filesystem consists of: ");
                        printDeviceInfo(owd, true);
                        // close the 1-Wire file to release
                        owfile.close();
                        break;
                    }
                    ;
                }
                catch (IOException e)
                {
                    Debug.WriteLine("");
                    Debug.WriteLine("-----------------------------------------------");
                    Debug.WriteLine(e);
                    Debug.WriteLine("-----------------------------------------------");
                }

                end_time = stopWatch.ElapsedMilliseconds;
                Debug.WriteLine("");
                if (start_time > 0)
                {
                    Debug.WriteLine((end_time - start_time) + "ms");
                }
                Debug.WriteLine("");

                if (selection != MAIN_QUIT)
                {
                    selection = menuSelect(mainMenu);
                }
                Debug.WriteLine("");
            } while (selection != MAIN_QUIT);
        }
        catch (Exception e)
        {
            Debug.WriteLine(e);
        }
        finally
        {
            if (adapter != null)
            {
                // end exclusive use of adapter
                adapter.endExclusive();

                // free the port used by the adapter
                Debug.WriteLine("Releasing adapter port");
                try
                {
                    adapter.freePort();
                }
                catch (OneWireException e)
                {
                    Debug.WriteLine(e);
                }
            }
        }

        Debug.WriteLine("");
        return;
    }