This is an old revision of the document!


Clarujust API

The following represents the expected methods/functionality provided to Clarujust when it is initialized from an external application. Also, the following shows the functionality provided by Clarujust to the external application. For the rest of this document, the external application will be referred to as ezFIT.

The ezFIT API

It is assumed Clarujust will be initialized with a reference to some kind of object that communicates with the hearing aids. The following shows this interface:

I’m guessing the method names will be similar to what has previously been provided by ezOrange.

It is assumed the hearing devices will already be connected and initialized by ezFIT when this interface is passed to Clarujust. Also, ezFIT should already have the correct memory slots selected in the hearing devices.

Here’s a short description of the methods:

Initialize Hearing Instrument

Boolean GetHAInitialized()

Returns true if the API has initialized the hearing devices; otherwise, false.

Ear Side

Int16 GetEar()
void SetEar(Int16 value)

Gets or sets the current ear. 0 = left, 1 = right. With the exception of the GetHAInitialized method, the SetEar method needs to be called before any other method is called in order to access data in the correct ear.

For example: the following code shows how to get the bump values from the right-ear hearing device:

void GetBumpExample(AudinaAPI api)
{
    if (api != null &&
        api.GetHAInitialized())
    {
        api.SetEar(1);
        short bumpQ = api.GetBumpQ();
        short bumpGain = api.GetBumpGain();
        short bumpCF = api.GetBumpCF();
    }
}

Memory

short GetMemory()

Returns the current memory slot of the current ear.

Circuit Parameters

int[] GetEthosArray()

Returns an array containing all current hearing device parameters in the current ear.

Bump

short GetBumpCF()
void  SetBumpCF(short value)
short GetBumpQ()
void  SetBumpQ(short value)
short GetBumpGain()
void  SetBumpGain(short value)
short GetGain()
void  SetGain(short value)
short GetTiltSlope()
void  SetTiltSlope(short value)

Gets or sets the associated parameter values in the current ear.

NOTE: It is unclear if we need the GetBias() or SetBias() methods, or if we’re going to handle the compression via the SetCompression() method, but those can be added if needed.

Exposed Clarujust Methods/Objects

The following shows what will be exposed to ezFit.

Instantiating Clarujust

The ClarujustApp static class provides one method:

static IClarujustWindow Create(AudinaAPI api)

Returns an instance of a Clarujust window.

Clarujust Window

The IClarujustWindow interface defines the methods that will be exposed to ezFIT while Clarujust is running. We can add/remove/modify methods in this interface depending on what we decide should be provided to ezFIT, but for the time being, here is the interface and its associated methods:

void Show()

Shows the Clarujust window.

void ShowModal()

Shows the Clarujust window as a modal dialog box.

void Hide()

Hides the Clarujust window.

Running Clarujust Proxy

The DLL files needed to run the Clarujust proxy server. The only one that needs to be registered is ClarujustProxyServer.dll.

COM DLLs built in .NET need to use a different app to register (regasm.exe). If you have the latest version of .NET installed on your machine, it should be located in the following directory: C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\regasm.exe

Use that to register the libraries. For example, place the DLLs in C:\Temp. Then create these batch files, one to register and one to unregister:

To register:

path C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\
regasm C:\Temp\ClarujustProxyServer.dll /tlb:ClarujustProxyServer.tlb /codebase
pause

Or:

CD C:\TEMP
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\regasm ClarujustProxyServer.dll /tlb:ClarujustProxyServer.tlb /codebase
pause

To unregister:

path C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\
regasm C:\Temp\ClarujustProxyServer.dll /tlb:ClarujustProxyServer.tlb /unregister
pause

Or:

CD C:\TEMP
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\regasm ClarujustProxyServer.dll /tlb:ClarujustProxyServer.tlb /unregister
pause

The other libraries just need to be placed in the same location as the proxy DLL and it should work.

Sample code (in C#) on how to call it:

static void Main(string[] args)
{
    //Create and initialize the optimizer object however you choose...
    EzFITOptimizerSvr.IEzFITOptimizerClarujust optimizer = ...
    Clarujust.Proxy.IClarujustApp app = new Clarujust.Proxy.ClarujustApp();
    app.Initialize(optimizer);
    app.ShowModal();
}

Sample code in Delphi:

procedure TEzFITOptimizerClarujust.LaunchClarujustApp;
var
//  ClarujustApp: Variant;
  ClarujustApp: IClarujustApp; 
  ThisOptSvr: OleVariant;
begin
  ThisOptSvr := IEzFitOptimizerClarujust(self);
  ClarujustApp := CoClarujustApp.Create();
  ClarujustApp.Initialize(ThisOptSvr);
  ClarujustApp.ShowModal;
 
//  ThisOptSvr := IEzFitOptimizerClarujust(self);
//  ClarujustApp := CreateOleObject('Clarujust.Proxy.ClarujustApp');
//  ClarujustApp.Initialize(ThisOptSvr);
//  ClarujustApp.ShowModal;
end;
Sample ClarujustApp proxy source code
using System;
using System.IO;
using System.Text;
using System.Threading;
using System.Reflection;
using System.Windows.Forms;
using System.Runtime.InteropServices;
 
using ezFITOptimizerSvr;
 
namespace Clarujust.Proxy
{
    /// <summary>
    /// Represents the worker class responsible for launching and managing the Clarujust application.
    /// </summary>
    [Guid("A85C954C-F13F-4323-A846-84222AE79D27"),
    ClassInterface(ClassInterfaceType.None),
    ComVisible(true)]
    public class ClarujustApp : IClarujustApp
    {
        #region Properties
 
        /// <summary>
        /// Gets or sets the worker thread representing the Clarujust application.
        /// </summary>
        /// <value>A <see cref="Thread"/> object representing the Clarujust application thread.</value>
        private static Thread ClarujustThread { get; set; }
 
        #endregion
 
        #region Methods
 
        #region Public
 
        /// <summary>
        /// Initializes the Clarujust application.
        /// </summary>
        /// <param name="framework">The Audina framework object.</param>
        /// <remarks>
        /// This method handles any initialization required by the Clarujust application.
        /// The <paramref name="framework"/> parameter represents the Audina interface responsible
        /// for communicating with the hearing aids and the ezFit application.  This parameter
        /// must be specified, and must be an object of type <see cref="ezFITOptimizerSvr.IEzFITOptimizerClarujust"/>.</remarks>
        [STAThread()]
        public void Initialize(Object framework)
        {
            // Stop any existing threads
            ClarujustApp.JoinThread();
 
            if (framework == null)
                MessageBox.Show("An IEzFITOptimizerClarujust type object must be provided.");
            else
            {
                // Check the framework object
                ClarujustApp.LogMessage(String.Format("Initializing Clarujust proxy server."));
                IEzFITOptimizerClarujust fwk = framework as IEzFITOptimizerClarujust;
                if (fwk == null)
                    MessageBox.Show(String.Format(
                        "An IEzFITOptimizerClarujust type object must be provided.  The specified object is of type {0}.",
                        framework.GetType().ToString()));
                else
                {
                    // Create and start the Clarujust thread
                    ClarujustApp.ClarujustThread = new Thread(new ParameterizedThreadStart(thread_Clarujust));
                    ClarujustApp.ClarujustThread.Name = "Main Clarujust Thread";
                    ClarujustApp.ClarujustThread.SetApartmentState(ApartmentState.STA);
                    ClarujustApp.ClarujustThread.Start(framework);
                }
            }
        }
 
        /// <summary>
        /// Shows the Clarujust application window.
        /// </summary>
        [STAThread()]
        public void Show()
        {
        }
 
        /// <summary>
        /// Shows the Clarujust application window as a modal dialog box.
        /// </summary>
        [STAThread()]
        public void ShowModal()
        {
        }
 
        /// <summary>
        /// Closes the Clarujust application window.
        /// </summary>
        [STAThread()]
        public void Close()
        {
        }
 
        #endregion
 
        #region Private
 
        /// <summary>
        /// Prepares the ezFit optimizer framework interface.
        /// </summary>
        /// <param name="fwk">ezFit optimizer framework interface.</param>
        /// <returns>True if the interface has been prepared and is ready to be used; otherwise, false.</returns>
        private static Boolean PrepareInterface(IEzFITOptimizerClarujust fwk)
        {
            Boolean isInitialized = false;
            String deviceId = null;
            String serialNumber = null;
            Int32 earCount = 0;
            Int32 error = 0;
            String appDirectory = null;
 
            try
            {
                ClarujustApp.LogMessage(String.Format("Getting executing assembly location."));
                appDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                ClarujustApp.LogMessage(String.Format("Executing assembly = {0}.", appDirectory));
            }
            catch (Exception e)
            {
                ClarujustApp.LogException(e);
            }
            catch
            {
                ClarujustApp.LogMessage(String.Format("Some exception occurred while checking initialization."));
            }
 
            try
            {
                ClarujustApp.LogMessage(String.Format("Setting environment directory to executing assembly location."));
                Environment.CurrentDirectory = appDirectory;
                ClarujustApp.LogMessage(String.Format("Environment directory set."));
            }
            catch (Exception e)
            {
                ClarujustApp.LogException(e);
            }
            catch
            {
                ClarujustApp.LogMessage(String.Format("Some exception occurred while checking initialization."));
            }
 
            try
            {
                ClarujustApp.LogMessage(String.Format("Checking initialization."));
                error = fwk.GetHAInitialized(out isInitialized);
                ClarujustApp.LogMessage(String.Format("Is initialized = {0} (return code = {1}).", isInitialized, error));
            }
            catch (Exception e)
            {
                ClarujustApp.LogException(e);
            }
            catch
            {
                ClarujustApp.LogMessage(String.Format("Some exception occurred while checking initialization."));
            }
 
            try
            {
                ClarujustApp.LogMessage(String.Format("Setting up 60 (initial)."));
                error = fwk.Setup60();
                ClarujustApp.LogMessage(String.Format("Setup 60 complete (initial) (return code = {0}).", error));
            }
            catch (Exception e)
            {
                ClarujustApp.LogException(e);
            }
            catch
            {
                ClarujustApp.LogMessage(String.Format("Some exception occurred while setting up 60."));
            }
 
            try
            {
                ClarujustApp.LogMessage(String.Format("Getting ear count."));
                error = fwk.GetEarCount(out earCount);
                ClarujustApp.LogMessage(String.Format("Ear count = {0} (ret code = {1}).", earCount, error));
            }
            catch (Exception e)
            {
                ClarujustApp.LogException(e);
            }
            catch
            {
                ClarujustApp.LogMessage(String.Format("Some exception occurred while getting ear count."));
            }
 
            if (earCount == EAR_COUNT_LEFT ||
                earCount == EAR_COUNT_BOTH)
            {
                try
                {
                    ClarujustApp.LogMessage(String.Format("Getting left device ID."));
                    error = fwk.GetDeviceID(out deviceId, 0);
                    ClarujustApp.LogMessage(String.Format("left device ID = {0} (ret code = {1}).", deviceId, error));
                }
                catch (Exception e)
                {
                    ClarujustApp.LogException(e);
                }
                catch
                {
                    ClarujustApp.LogMessage(String.Format("Some exception occurred while getting left device ID."));
                }
 
                try
                {
                    ClarujustApp.LogMessage(String.Format("Getting left serial number."));
                    error = fwk.GetSerialNumber(out serialNumber, 0);
                    ClarujustApp.LogMessage(String.Format("left serial = {0} (ret code = {1}).", serialNumber, error));
                }
                catch (Exception e)
                {
                    ClarujustApp.LogException(e);
                }
                catch
                {
                    ClarujustApp.LogMessage(String.Format("Some exception occurred while getting left serial."));
                }
            }
            else if (earCount == EAR_COUNT_RIGHT)
            {
                try
                {
                    ClarujustApp.LogMessage(String.Format("Getting right device ID."));
                    error = fwk.GetDeviceID(out deviceId, 1);
                    ClarujustApp.LogMessage(String.Format("right device ID = {0} (ret code = {1}).", deviceId, error));
                }
                catch (Exception e)
                {
                    ClarujustApp.LogException(e);
                }
                catch
                {
                    ClarujustApp.LogMessage(String.Format("Some exception occurred while getting right device ID."));
                }
 
                try
                {
                    ClarujustApp.LogMessage(String.Format("Getting right serial number."));
                    error = fwk.GetSerialNumber(out serialNumber, 1);
                    ClarujustApp.LogMessage(String.Format("right serial = {0} (ret code = {1}).", serialNumber, error));
                }
                catch (Exception e)
                {
                    ClarujustApp.LogException(e);
                }
                catch
                {
                    ClarujustApp.LogMessage(String.Format("Some exception occurred while getting right serial."));
                }
            }
            return true;
        }
 
        /// <summary>
        /// Stops and joins the Clarujust worker thread.
        /// </summary>
        private static void JoinThread()
        {
            if (ClarujustApp.ClarujustThread != null)
            {
                ClarujustApp.ClarujustThread.Abort();
                ClarujustApp.ClarujustThread.Join();
            }
        }
 
        /// <summary>
        /// Logs a message to the log file.
        /// </summary>
        /// <param name="message">An application-defined message.</param>
        private static void LogMessage(String message)
        {
            String text = String.Format(
                "Message: {0} {1}",
                DateTime.Now.ToString("MM/dd/yy HH:mm:ss"),
                message);
            using (StreamWriter writer = new StreamWriter(new FileStream("log.txt", FileMode.OpenOrCreate)))
            {
                writer.BaseStream.Position = writer.BaseStream.Length;
                writer.WriteLine(message);
            }
        }
 
        /// <summary>
        /// Logs the exception message to the log file.
        /// </summary>
        /// <param name="e">The exception to log.</param>
        private static void LogException(Exception e)
        {
            if (e == null)
                return;
 
            StringBuilder text = new StringBuilder();
            text.Append(String.Empty.PadLeft(100, '-') + "\r\n");
            text.Append("Exception Type: " + e.GetType().ToString() + "\r\n");
            text.Append("Exception Msg: " + e.Message + "\r\n");
            text.Append("Stack Trace: \r\n");
            text.Append(e.StackTrace + "\r\n");
            while (e.InnerException != null)
            {
                text.Append(String.Empty.PadLeft(100, '-') + "\r\n");
                text.Append("Inner Exception Type: " + e.InnerException.GetType().ToString() + "\r\n");
                text.Append("Inner Exception Msg: " + e.InnerException.Message + "\r\n");
                text.Append("Stack Trace: \r\n");
                text.Append(e.InnerException.StackTrace + "\r\n");
                e = e.InnerException;
            }
 
            ClarujustApp.LogMessage(text.ToString());
        }
 
        #endregion
 
        #region Threads
 
        /// <summary>
        /// The Clarujust worker thread.
        /// </summary>
        /// <param name="data">The ezFit optimizer framework interface.</param>
        private static void thread_Clarujust(Object data)
        {
            ClarujustApp.PrepareInterface((IEzFITOptimizerClarujust)data);
        }
 
        #endregion
 
        #endregion
 
        #region Data Constants
 
        private static readonly Int32 EAR_COUNT_LEFT = 1;
        private static readonly Int32 EAR_COUNT_RIGHT = 2;
        private static readonly Int32 EAR_COUNT_BOTH = 3;
 
        #endregion
    }
}