123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397 |
- using System;
- using System.Text;
- using System.IO;
- using System.Collections.Generic;
- using System.Threading;
- using ANT_Managed_Library;
- using ANT_Managed_Library.ANTFS;
- namespace ANTFS_Demo
- {
-
-
-
-
- public class HostDemo
- {
-
- readonly string HostFriendlyName = "ConsoleHost";
- readonly byte ConnectRF = (byte)ANT_Managed_Library.ANTFS.RadioFrequency.Auto;
- readonly uint ClientDeviceID = 0;
- readonly ushort DeviceNumber = 0;
- readonly byte SearchTimeout = 60;
- ANT_Channel channel0;
- ANTFS_HostChannel antfsHost;
- ANTFS_Directory dirFS;
- byte[] clientPassKey;
- bool demoDone = false;
- int cursorIndex = 0;
- ushort currentDownloadIndex = 0;
- Timer searchTimer;
-
-
-
- public void Start(ANT_Device antDevice)
- {
- PrintMenu();
- try
- {
-
- channel0 = antDevice.getChannel(0);
- antfsHost = new ANTFS_HostChannel(channel0);
-
-
- antfsHost.OnResponse += new Action<ANTFS_HostChannel.Response>(HandleHostResponses);
-
- ConfigureHost(antDevice);
- while (!demoDone)
- {
- string command = Console.ReadLine();
- HandleUserInput(command);
- Thread.Sleep(0);
- }
- }
- catch(ANTFS_Exception antEx)
- {
- Console.WriteLine("ANT-FS Exception: " + antEx.Message);
- }
- catch (Exception ex)
- {
- Console.WriteLine("Demo failed: " + ex.Message);
- }
- finally
- {
- Console.WriteLine("Disconnecting module...");
- antDevice.Dispose();
- antfsHost.Dispose();
- Console.WriteLine("Demo has completed successfully!");
- }
- }
-
-
-
-
- public void ConfigureHost(ANT_Device antDevice)
- {
-
- antfsHost.SetNetworkKey(Demo.NetworkNumber, Demo.NetworkKey);
- antfsHost.SetChannelID(Demo.DeviceType, Demo.TransmissionType);
- antfsHost.SetChannelPeriod(Demo.ChannelPeriod);
-
-
- if (antfsHost.AddSearchDevice(ClientDeviceID, Demo.ClientManufacturerID, Demo.ClientDeviceType) == 0)
- throw new Exception("Error adding search device: ");
- if (Demo.AntfsBroadcast)
- {
-
-
- channel0.channelResponse += new dChannelResponseHandler(HandleChannelResponses);
-
-
- if (!antDevice.setNetworkKey(Demo.NetworkNumber, Demo.NetworkKey, 500))
- throw new Exception("Error configuring network key");
- if (!channel0.assignChannel(ANT_ReferenceLibrary.ChannelType.BASE_Slave_Receive_0x00, Demo.NetworkNumber, 500))
- throw new Exception("Error assigning channel");
- if (!channel0.setChannelID(DeviceNumber, 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
- {
-
-
-
-
- antfsHost.SearchForDevice(Demo.SearchRF, ConnectRF, DeviceNumber);
- }
-
- Console.WriteLine("Searching for devices...");
-
- searchTimer = new Timer(SearchExpired, null, SearchTimeout * 1000, Timeout.Infinite);
- }
-
-
-
-
- public void HandleHostResponses(ANTFS_HostChannel.Response response)
- {
- Console.WriteLine(Print.AsString(response));
- switch (response)
- {
- case ANTFS_HostChannel.Response.ConnectPass:
-
-
- searchTimer.Change(Timeout.Infinite, Timeout.Infinite);
-
- 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:
-
-
- byte[] authString = antfsHost.GetAuthResponse();
- if (authString.Length > 0)
- {
- Console.WriteLine("Received Passkey: Stored for use in next session");
- clientPassKey = authString;
- }
- break;
- case ANTFS_HostChannel.Response.DownloadPass:
-
-
- byte[] dlData = antfsHost.GetTransferData();
- if ((dlData.Length > 0))
- {
- if (currentDownloadIndex == 0)
- {
-
- Console.WriteLine("Received Directory");
- dirFS = new ANTFS_Directory(dlData);
- Console.WriteLine(dirFS.ToString());
- }
- else
- {
-
- 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;
- }
- }
-
-
-
-
- public void HandleChannelResponses(ANT_Response response)
- {
-
- if (antfsHost.GetStatus() != ANTFS_HostChannel.State.Idle)
- return;
- if (response.responseID == (byte) ANT_ReferenceLibrary.ANTMessageID.BROADCAST_DATA_0x4E)
- {
- Console.Write("Rx: " + Demo.CursorStrings[cursorIndex++] + "\r");
- cursorIndex &= 3;
- if (response.getDataPayload()[7] >= 40)
- {
- antfsHost.RequestSession(Demo.SearchRF, ConnectRF);
- }
- }
- }
-
-
-
-
- private void SearchExpired(Object state)
- {
- lock (this)
- {
- ANTFS_HostChannel.State currentState = antfsHost.GetStatus();
- if (currentState == ANTFS_HostChannel.State.Searching)
- {
- Console.WriteLine("No devices found...");
- antfsHost.Cancel();
- }
- }
- }
-
-
-
- public void PrintMenu()
- {
- Console.WriteLine(Environment.NewLine);
- Console.WriteLine("M - Print this menu");
- Console.WriteLine("A - Authenticate");
- Console.WriteLine("D - Download");
- Console.WriteLine("U - Upload");
- Console.WriteLine("E - Erase");
- Console.WriteLine("X - Disconnect");
- Console.WriteLine("C - Cancel");
- Console.WriteLine("P - Print directory");
- Console.WriteLine("V - Request version");
- Console.WriteLine("S - Request status");
- Console.WriteLine("Q - Quit");
- Console.WriteLine(Environment.NewLine);
- }
-
-
-
-
- public void HandleUserInput(string command)
- {
- try
- {
- switch (command)
- {
- case "M":
- case "m":
- PrintMenu();
- break;
- case "A":
- case "a":
-
-
- Console.WriteLine("Select authentication mode:");
- Console.WriteLine("1 - Skip authentication");
- Console.WriteLine("2 - Request pairing");
- Console.WriteLine("3 - Send passkey");
- string authChoice = Console.ReadLine();
- switch (authChoice)
- {
- case "1":
- antfsHost.Authenticate(AuthenticationType.None, 60000);
- break;
- case "2":
- antfsHost.Authenticate(AuthenticationType.Pairing, HostFriendlyName, 60000);
- break;
- case "3":
- if (clientPassKey != null)
- antfsHost.Authenticate(AuthenticationType.PassKey, clientPassKey, 60000);
- else
- Console.WriteLine("Try pairing first");
- break;
- default:
- Console.WriteLine("Invalid authentication type.");
- break;
- }
- break;
- case "D":
- case "d":
-
- Console.WriteLine("Select the file index to download.");
- Console.WriteLine("Choose 0 for the directory");
- string downloadChoice = Console.ReadLine();
- currentDownloadIndex = Demo.ParseInput(downloadChoice);
- antfsHost.Download(currentDownloadIndex, 0, 0);
- break;
- case "U":
- case "u":
-
- Console.WriteLine("Select the file index to upload.");
- string uploadChoice = Console.ReadLine();
- byte[] ulData;
- if (File.Exists("rawdataout.txt"))
- ulData = File.ReadAllBytes("rawdataout.txt");
- else
- ulData = new byte[200];
- antfsHost.Upload(Demo.ParseInput(uploadChoice), ulData);
- Console.WriteLine("Uploading: {0} bytes", ulData.Length);
- break;
- case "E":
- case "e":
-
- Console.WriteLine("Select the file index to erase");
- string eraseChoice = Console.ReadLine();
- antfsHost.EraseData(Demo.ParseInput(eraseChoice));
- break;
- case "X":
- case "x":
-
- antfsHost.Disconnect();
- break;
- case "C":
- case "c":
-
- antfsHost.Cancel();
- break;
- case "P":
- case "p":
- if (dirFS != null)
- Console.WriteLine(dirFS.ToString());
- else
- Console.WriteLine("Try requesting the directory first");
- break;
- case "V":
- case "v":
- Console.WriteLine("ANT-FS Library Version " + antfsHost.GetLibraryVersion());
- break;
- case "S":
- case "s":
- Console.WriteLine("Status: " + Print.AsString(antfsHost.GetStatus()));
- break;
- case "Q":
- case "q":
- demoDone = true;
- break;
- default:
- break;
- }
- }
- catch (ANTFS_RequestFailed_Exception antEx)
- {
-
- Console.WriteLine(antEx.Message);
- }
- catch (System.ArgumentException argEx)
- {
-
- Console.WriteLine(argEx.Message);
- }
- }
- }
- }
|