示例#1
0
        /// <summary>
        /// Creates a <code>OWFileInputStream</code> by
        /// opening a connection to an actual file,
        /// the file named by the <code>File</code>
        /// object <code>file</code> in the Filesystem.
        /// A new <code>OWFileDescriptor</code> object
        /// is created to represent this file connection.
        /// <para>
        /// If the named file does not exist, is a directory rather than a regular
        /// file, or for some other reason cannot be opened for reading then a
        /// <code>FileNotFoundException</code> is thrown.
        ///
        /// </para>
        /// </summary>
        /// <param name="file">   the file to be opened for reading. </param>
        /// <exception cref="FileNotFoundException">  if the file does not exist,
        ///                   is a directory rather than a regular file,
        ///                   or for some other reason cannot be opened for
        ///                   reading. </exception>
        /// <seealso cref=        com.dalsemi.onewire.application.file.OWFile#getPath() </seealso>
        public OWFileInputStream(OWFile file)
        {
            // get the file descriptor
            try
            {
                fd = file.FD;
            }
            catch (System.IO.IOException e)
            {
                fd.free();
                fd = null;
                throw new OWFileNotFoundException(e.ToString());
            }

            // open the file
            try
            {
                fd.open();
            }
            catch (OWFileNotFoundException e)
            {
                fd.free();
                fd = null;
                throw new OWFileNotFoundException(e.ToString());
            }

            // make sure it is not a directory
            if (!fd.File)
            {
                fd.free();
                fd = null;
                throw new OWFileNotFoundException("Not a file");
            }
        }
示例#2
0
        /// <summary>
        /// Creates a file output stream to write to the file represented by
        /// the specified <code>File</code> object. A new
        /// <code>OWFileDescriptor</code> object is created to represent this
        /// file connection.
        /// <para>
        /// First, if there is a security manager, its <code>checkWrite</code>
        /// method is called with the path represented by the <code>file</code>
        /// argument as its argument.
        /// </para>
        /// <para>
        /// If the file exists but is a directory rather than a regular file, does
        /// not exist but cannot be created, or cannot be opened for any other
        /// reason then a <code>FileNotFoundException</code> is thrown.
        ///
        /// </para>
        /// </summary>
        /// <param name="file">               the file to be opened for writing. </param>
        /// <exception cref="FileNotFoundException">  if the file exists but is a directory
        ///                   rather than a regular file, does not exist but cannot
        ///                   be created, or cannot be opened for any other reason </exception>
        /// <exception cref="SecurityException">  if a security manager exists and its
        ///               <code>checkWrite</code> method denies write access
        ///               to the file. </exception>
        /// <seealso cref=        java.io.File#getPath() </seealso>
        public OWFileOutputStream(OWFile file)
        {
            try
            {
                fd = file.FD;
            }
            catch (System.IO.IOException e)
            {
                fd.free();
                fd = null;
                throw new OWFileNotFoundException(e.ToString());
            }

            fd.open();
        }
示例#3
0
    /// <summary>
    /// Display file details
    /// </summary>
    /// <param name="owfile"> 1-Wire file to show details of </param>
    internal static void showDetails(OWFile owfile)
    {
        int[]           page_list;
        int             local_pg;
        PagedMemoryBank pmb;

        // check if this directory/file exists
        if (!owfile.exists())
        {
            Debug.WriteLine("-----------------------------------------------");
            Debug.WriteLine("Directory/file not found!");
            Debug.WriteLine("-----------------------------------------------");
            return;
        }

        // get the list of pages that make up this directory/file
        Debug.WriteLine("Page allocation of " + (owfile.File ? "file:" : "directory:"));

        try
        {
            page_list = owfile.PageList;
        }
        catch (IOException e)
        {
            Debug.WriteLine(e);
            return;
        }

        // loop to display info and contents of each page
        for (int pg = 0; pg < page_list.Length; pg++)
        {
            local_pg = owfile.getLocalPage(page_list[pg]);
            Debug.WriteLine("Filesystem page=" + page_list[pg] + ", local PagedMemoryBank page=" + owfile.getLocalPage(page_list[pg]));
            pmb = owfile.getMemoryBankForPage(page_list[pg]);
            byte[] read_buf = new byte[pmb.PageLength];
            try
            {
                pmb.readPage(local_pg, false, read_buf, 0);
                hexPrint(read_buf, 0, read_buf.Length);
                Debug.WriteLine("");
            }
            catch (OneWireException e)
            {
                Debug.WriteLine(e);
            }
        }
    }
示例#4
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);
            }
        }
示例#5
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);
            }
        }
示例#6
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;
    }
示例#7
0
    /// <summary>
    /// List the files/directory entries on the provided 1-Wire File.  Format the
    /// output depending on the recursive and depth parameters.
    /// </summary>
    /// <param name="depth"> recursive depth </param>
    /// <param name="recursive"> call this method recursivly if true </param>
    public static void listDir(OWFile owfile, int depth, bool recursive)
    {
        OWFile[] owfile_list;

        // get the list
        owfile_list = owfile.listFiles();
        // check for bad directory
        if (owfile_list == null)
        {
            Debug.WriteLine("Directory given was not valid or error in Filesystem!");
        }
        else
        {
            // display
            for (int i = 0; i < owfile_list.Length; i++)
            {
                if (recursive)
                {
                    Debug.Write("|");
                    if (owfile_list[i].Directory)
                    {
                        for (int j = 0; j < (depth - 1); j++)
                        {
                            Debug.Write("    ");
                        }

                        if (depth == 1)
                        {
                            Debug.Write("----");
                        }
                        else
                        {
                            Debug.Write("|---");
                        }
                    }
                    else
                    {
                        for (int j = 0; j < depth; j++)
                        {
                            Debug.Write("    ");
                        }
                    }
                }

                Debug.Write(owfile_list[i].Name);
                if (owfile_list[i].Directory)
                {
                    if (recursive)
                    {
                        Debug.WriteLine("");
                    }
                    else
                    {
                        Debug.WriteLine("    <dir>");
                    }
                    if (recursive)
                    {
                        listDir(owfile_list[i], depth + 1, true);
                    }
                }
                else
                {
                    Debug.WriteLine("  (" + owfile_list[i].length() + " bytes)");
                }

                owfile_list[i].close();
            }
        }
    }