コード例 #1
0
ファイル: OWFish1.cs プロジェクト: Agrimeters/winrt-onewire
    /// <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;
    }
コード例 #2
0
    /// <summary>
    /// Search a given path for an XML file.
    /// </summary>
    /// <param name="currentPathIndex"> index into the 'paths' Vector that
    ///        indicates what 1-Wire path to search for XML files </param>
    /// <returns> true if the current path provided has been
    ///         completely checked for XML files.  false if
    ///         if this current path should be searched further </returns>
    public static bool pathXMLSearchComplete(int currentPathIndex)
    {
        OneWireContainer  owd = null, check_owd = null;
        ParseContainer    pc = null, check_pc = null;
        OWFileInputStream file_stream = null;
        bool rslt       = true;
        bool xml_parsed = false;

        try
        {
            main_Renamed.Status = "Waiting for 1-Wire available";

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

            main_Renamed.Status = "Exclusive 1-Wire aquired";

            // open the current path to the device
            OWPath owpath = (OWPath)paths[currentPathIndex];
            owpath.open();

            main_Renamed.Status = "Path opened: " + owpath.ToString();

            // setup search
            adapter.setSearchAllDevices();
            adapter.targetAllFamilies();
            adapter.Speed = DSPortAdapter.SPEED_REGULAR;

            // find all devices, update parseLog and get a device filesystem to check
            for (System.Collections.IEnumerator owd_enum = adapter.AllDeviceContainers; owd_enum.MoveNext();)
            {
                owd = (OneWireContainer)owd_enum.Current;
                long key = owd.AddressAsLong;

                main_Renamed.Status = "Device Found: " + owd.AddressAsString;

                // check to see if this is in the parseLog, add if not there
                pc = null;
                parseLog.TryGetValue(key, out pc);
                if (pc == null)
                {
                    main_Renamed.Status = "Device is new to parse: " + owd.AddressAsString;
                    pc = new ParseContainer(owd);
                    parseLog.Add(key, pc);
                }
                else
                {
                    main_Renamed.Status = "Device " + owd.AddressAsString + " with count " + pc.attemptCount;
                }

                // if attemptCount is low then check it later for XML files
                if (pc.attemptCount < ParseContainer.MAX_ATTEMPT)
                {
                    check_owd = owd;
                    check_pc  = pc;
                }
            }

            // check if there is anything to open
            if (check_owd != null)
            {
                // result is false because found something to try and open
                rslt = false;

                main_Renamed.Status = "Attempt to open file TAGX.000";

                // attempt to open a 'TAGX.000' file, (if fail update parse_log)
                try
                {
                    file_stream         = new OWFileInputStream(check_owd, "TAGX.0");
                    main_Renamed.Status = "Success file TAGX.000 opened";
                }
                catch (OWFileNotFoundException)
                {
                    file_stream = null;
                    check_pc.attemptCount++;
                    main_Renamed.Status = "Could not open TAGX.000 file";
                }
            }

            // try to parse the file, (if fail update parse_log)
            if (file_stream != null)
            {
                // create the tagParser
                // (this should not be necessary but the parser currently holds state)
                parser = new TAGParser(adapter);

                // attempt to parse it, on success max out the attempt
                if (parseStream(parser, file_stream, owpath, true))
                {
                    xml_parsed            = true;
                    check_pc.attemptCount = ParseContainer.MAX_ATTEMPT;
                }
                else
                {
                    check_pc.attemptCount++;
                }

                // close the file
                try
                {
                    file_stream.close();
                }
                catch (IOException)
                {
                    main_Renamed.Status = "Could not close TAGX.000 file";
                }
            }

            // close the path
            owpath.close();
            main_Renamed.Status = "Path closed";

            // update the main paths listbox if XML file found
            if (xml_parsed)
            {
                // add the paths to the main window
                main_Renamed.clearPathList();
                for (int p = 0; p < paths.Count; p++)
                {
                    main_Renamed.addToPathList(((OWPath)paths[p]).ToString());
                }
            }
        }
        catch (OneWireException e)
        {
            Debug.WriteLine(e);
            main_Renamed.Status = e.ToString();
        }
        finally
        {
            // end exclusive use of adapter
            adapter.endExclusive();
            main_Renamed.Status = "Exclusive 1-Wire aquired released";
        }

        return(rslt);
    }