Пример #1
0
        /// <summary>
        /// Configure the client beacon parameters, and start transmitting/beaconing
        /// </summary>
        /// <param name="antDevice">ANT USB device</param>
        public void ConfigureClient(ANT_Device antDevice)
        {
            // Configure ANT Channel parameters
            antfsClient.SetClientNetworkKey(0, Demo.NetworkKey);
            antfsClient.SetChannelID(Demo.DeviceType, Demo.TransmissionType);
            antfsClient.SetChannelPeriod(Demo.ChannelPeriod);

            // Setup client configuration parameters.
            // ANTFS_ClientParameters is a struct, so make sure to initialize ALL parameters
            ANTFS_ClientParameters clientConfig = new ANTFS_ClientParameters();

            clientConfig.BeaconDeviceType     = Demo.ClientDeviceType;
            clientConfig.BeaconManufacturerID = Demo.ClientManufacturerID;
            clientConfig.BeaconRadioFrequency = Demo.SearchRF;
            clientConfig.LinkPeriod           = Demo.LinkPeriod;
            clientConfig.AuthenticationType   = ClientAuthenticationType;
            clientConfig.IsDataAvailable      = DataAvailable;
            clientConfig.IsPairingEnabled     = PairingEnabled;
            clientConfig.IsUploadEnabled      = UploadEnabled;
            clientConfig.BeaconTimeout        = BeaconTimeout;
            clientConfig.PairingTimeout       = PairingTimeout;
            clientConfig.SerialNumber         = antDevice.getSerialNumber(); // Use the serial number of the USB stick to identify the client

            // Apply configuration
            antfsClient.Configure(clientConfig);

            // Configure friendly name and passkey (optional)
            antfsClient.SetFriendlyName(ClientFriendlyName);
            antfsClient.SetPassKey(PassKey);

            // Create directory, an add a single entry, of the configured size
            dirFS = new ANTFS_Directory();
            ANTFS_Directory.Entry fileEntry;
            fileEntry.FileIndex     = 1;
            fileEntry.FileSize      = TestFileSize;
            fileEntry.FileDataType  = 1;
            fileEntry.FileNumber    = 1;
            fileEntry.FileSubType   = 0;
            fileEntry.GeneralFlags  = (byte)(ANTFS_Directory.GeneralFlags.Read | ANTFS_Directory.GeneralFlags.Write | ANTFS_Directory.GeneralFlags.Erase);
            fileEntry.SpecificFlags = 0;
            fileEntry.TimeStamp     = 0; // TODO: Encode the timestamp properly
            dirFS.AddEntry(fileEntry);

            if (Demo.AntfsBroadcast)
            {
                // If we want to use ANT-FS broadcast mode, and start the channel in broadcast mode,
                // setup callback to handle responses for channel 0 while in broadcast mode
                channel0.channelResponse += new dChannelResponseHandler(HandleChannelResponses);

                // Configure the channel as a master, and look for the request message in the channel callback
                // to switch to ANT-FS mode
                if (!antDevice.setNetworkKey(Demo.NetworkNumber, Demo.NetworkKey, 500))
                {
                    throw new Exception("Error configuring network key");
                }
                if (!channel0.assignChannel(ANT_ReferenceLibrary.ChannelType.BASE_Master_Transmit_0x10, Demo.NetworkNumber, 500))
                {
                    throw new Exception("Error assigning channel");
                }
                if (!channel0.setChannelID((ushort)clientConfig.SerialNumber, false, Demo.DeviceType, Demo.TransmissionType, 500))
                {
                    throw new Exception("Error configuring Channel ID");
                }
                if (!channel0.setChannelFreq(Demo.SearchRF, 500))
                {
                    throw new Exception("Error configuring radio frequency");
                }
                if (!channel0.setChannelPeriod(Demo.ChannelPeriod, 500))
                {
                    throw new Exception("Error configuring channel period");
                }
                if (!channel0.openChannel(500))
                {
                    throw new Exception("Error opening channel");
                }
            }
            else
            {
                // If we want to start in ANT-FS mode, just open the beacon
                antfsClient.OpenBeacon();
            }
        }
Пример #2
0
        /// <summary>
        /// Handle ANT-FS response events
        /// </summary>
        public void HandleHostResponses(ANTFS_HostChannel.Response response)
        {
            Console.WriteLine(Print.AsString(response));   // Display response
            switch (response)
            {
            case ANTFS_HostChannel.Response.ConnectPass:
                // A device matching the search criteria was found
                // Disable the search timeout
                searchTimer.Change(Timeout.Infinite, Timeout.Infinite);
                // Obtain and display the device parameters
                ANTFS_SearchResults foundDevice = antfsHost.GetFoundDeviceParameters();
                if (foundDevice != null)
                {
                    Console.WriteLine(foundDevice.ToString());
                }
                else
                {
                    Console.WriteLine("Error obtaining device parameters");
                }
                break;

            case ANTFS_HostChannel.Response.AuthenticatePass:
                // The authentication request was accepted
                // If using Pairing authentication mode, the client will send a Passkey after accepting the request
                byte[] authString = antfsHost.GetAuthResponse();
                if (authString.Length > 0)
                {
                    Console.WriteLine("Received Passkey: Stored for use in next session");
                    clientPassKey = authString;        // Store Passkey for future use
                }
                break;

            case ANTFS_HostChannel.Response.DownloadPass:
                // Download completed successfully
                // Retrieve downloaded data
                byte[] dlData = antfsHost.GetTransferData();
                if ((dlData.Length > 0))
                {
                    if (currentDownloadIndex == 0)       // Directory
                    {
                        // Parse downloaded directory
                        Console.WriteLine("Received Directory");
                        dirFS = new ANTFS_Directory(dlData);
                        Console.WriteLine(dirFS.ToString());
                    }
                    else
                    {
                        // Store downloaded file
                        Console.WriteLine("Downloaded file " + currentDownloadIndex + ", Download size: " + antfsHost.GetDownloadSize());
                        if (dlData != null)
                        {
                            File.WriteAllBytes("rawdataout.txt", dlData);
                            Console.WriteLine("Saved to: rawdataout.txt");
                        }
                        else
                        {
                            Console.WriteLine("No data available");
                        }
                    }
                }
                else
                {
                    Console.WriteLine("No data available");
                }
                break;

            case ANTFS_HostChannel.Response.DisconnectPass:
            case ANTFS_HostChannel.Response.ConnectionLost:
            case ANTFS_HostChannel.Response.CancelDone:
                Console.WriteLine("Press any key to exit");
                demoDone = true;
                break;

            default:
                break;
            }
        }