123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330 |
- using System;
- using System.Text;
- using System.IO;
- using System.Collections.Generic;
- using System.Threading;
- using ANT_Managed_Library.ANTFS;
- using ANT_Managed_Library;
- namespace ANTFS_Demo
- {
-
-
-
-
- class ClientDemo
- {
-
- readonly AuthenticationType ClientAuthenticationType = AuthenticationType.None;
- readonly byte BeaconTimeout = 60;
- readonly byte PairingTimeout = 5;
- readonly bool DataAvailable = true;
- readonly bool PairingEnabled = true;
- readonly bool UploadEnabled = false;
- readonly byte[] PassKey = { 1, 2, 3, 1, 2, 3, 1, 2 };
- readonly string ClientFriendlyName = "ConsoleClient";
- readonly uint TestFileSize = 512;
-
- ANT_Channel channel0;
- ANTFS_ClientChannel antfsClient;
- ANTFS_Directory dirFS;
- bool demoDone = false;
- byte[] txBuffer = {0,0,0,0,0,0,0,0};
- int cursorIndex = 0;
-
-
-
- public void Start(ANT_Device antDevice)
- {
- PrintMenu();
- try
- {
-
- channel0 = antDevice.getChannel(0);
- antfsClient = new ANTFS_ClientChannel(channel0);
-
-
- antfsClient.OnResponse += new Action<ANTFS_ClientChannel.Response>(HandleClientResponses);
-
-
- ConfigureClient(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);
- Console.ReadLine();
- }
- finally
- {
- Console.WriteLine("Disconnecting module...");
- antDevice.Dispose();
- antfsClient.Dispose();
- Console.WriteLine("Demo has completed successfully!");
- }
- }
-
-
-
-
- public void ConfigureClient(ANT_Device antDevice)
- {
-
- antfsClient.SetClientNetworkKey(0, Demo.NetworkKey);
- antfsClient.SetChannelID(Demo.DeviceType, Demo.TransmissionType);
- antfsClient.SetChannelPeriod(Demo.ChannelPeriod);
-
-
- 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();
-
- antfsClient.Configure(clientConfig);
-
- antfsClient.SetFriendlyName(ClientFriendlyName);
- antfsClient.SetPassKey(PassKey);
-
-
- 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;
- dirFS.AddEntry(fileEntry);
- 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_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
- {
-
- antfsClient.OpenBeacon();
- }
- }
-
-
-
-
-
- public byte[] ReadFile(uint fileSize)
- {
- byte[] testFile = new byte[fileSize];
- for (uint i = 0; i < fileSize; i++)
- testFile[i] = (byte)i;
- return testFile;
- }
-
-
-
- public void HandleClientResponses(ANTFS_ClientChannel.Response response)
- {
- Console.WriteLine(Print.AsString(response));
- switch (response)
- {
- case ANTFS_ClientChannel.Response.ConnectionLost:
- case ANTFS_ClientChannel.Response.DisconnectPass:
-
- if (antfsClient.GetDisconnectParameters().CommandType == (byte)DisconnectType.Broadcast)
- {
- channel0.closeChannel();
- }
- else
- {
- antfsClient.CloseBeacon();
- }
- break;
- case ANTFS_ClientChannel.Response.BeaconClosed:
- demoDone = true;
- Console.WriteLine("Press enter to exit");
- break;
- case ANTFS_ClientChannel.Response.PairingRequest:
- Console.WriteLine("Pairing request from " + antfsClient.GetHostName());
- antfsClient.SendPairingResponse(true);
- break;
- case ANTFS_ClientChannel.Response.DownloadRequest:
- try
- {
- ushort index = antfsClient.GetRequestedFileIndex();
- if(index == 0)
- antfsClient.SendDownloadResponse(DownloadResponse.Ok, index, dirFS.ToByteArray());
- else
- antfsClient.SendDownloadResponse(DownloadResponse.Ok, index, ReadFile(dirFS.GetFileSize(index)));
- }
- catch(ANTFS_Exception)
- {
- antfsClient.SendDownloadResponse(DownloadResponse.InvalidIndex, 0, null);
- }
- break;
- default:
- break;
- }
- }
-
-
-
-
- public void HandleChannelResponses(ANT_Response response)
- {
-
- if(antfsClient.GetStatus() != ANTFS_ClientChannel.State.Idle)
- return;
- if ((response.responseID == (byte)ANT_ReferenceLibrary.ANTMessageID.RESPONSE_EVENT_0x40))
- {
- if(response.getChannelEventCode() == ANT_ReferenceLibrary.ANTEventID.EVENT_TX_0x03)
- {
- channel0.sendBroadcastData(txBuffer);
- txBuffer[7]++;
- Console.Write("Tx: " + Demo.CursorStrings[cursorIndex++] + "\r");
- cursorIndex &= 3;
- }
- else if(response.getChannelEventCode() == ANT_ReferenceLibrary.ANTEventID.EVENT_CHANNEL_CLOSED_0x07)
- {
- Console.WriteLine("Channel Closed");
- Console.WriteLine("Unassigning Channel...");
- if (channel0.unassignChannel(500))
- {
- Console.WriteLine("Unassigned Channel");
- Console.WriteLine("Press enter to exit");
- demoDone = true;
- }
- }
- }
- else if (response.responseID == (byte)ANT_ReferenceLibrary.ANTMessageID.ACKNOWLEDGED_DATA_0x4F)
- {
-
- if (response.messageContents[1] == 0x46)
- {
- if (response.messageContents[6] == 0 &&
- response.messageContents[7] == 0x43 &&
- response.messageContents[8] == 0x02)
- Console.WriteLine("Remote device requesting ANT-FS session");
- antfsClient.OpenBeacon();
- }
- }
- }
-
-
-
- public void PrintMenu()
- {
- Console.WriteLine(Environment.NewLine);
- Console.WriteLine("M - Print this menu");
- 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 "V":
- case "v":
- Console.WriteLine("ANT-FS Library Version " + ANTFS_ClientChannel.GetLibraryVersion());
- break;
- case "S":
- case "s":
- Console.WriteLine("Status: " + Print.AsString(antfsClient.GetStatus()));
- break;
- case "Q":
- case "q":
- demoDone = true;
- break;
- default:
- break;
- }
- }
- catch (ANTFS_RequestFailed_Exception antEx)
- {
-
- Console.WriteLine(antEx.Message);
- }
- }
- }
- }
|