/*********************************************************************
*                     SEGGER Microcontroller GmbH                    *
*                        The Embedded Experts                        *
**********************************************************************
*                                                                    *
*       (c) 1995 - 2022 SEGGER Microcontroller GmbH                  *
*                                                                    *
*       Internet: segger.com  Support: support_emnet@segger.com      *
*                                                                    *
**********************************************************************
*                                                                    *
*       MiddleWare plug-in                                           *
*                                                                    *
*       Please note:                                                 *
*                                                                    *
*       Knowledge of this file may under no circumstances            *
*       be used to write a similar product or a real-time            *
*       operating system for in-house use.                           *
*                                                                    *
*       Thank you for your fairness !                                *
*                                                                    *
**********************************************************************
*                                                                    *
*       Script version: V1.0.0 Rev. 2                                *
*                                                                    *
**********************************************************************

-------------------------- END-OF-HEADER -----------------------------
Purpose : Script for middleware windows for SEGGER Ozone
*/

var _PagePathSeparator  = "/";
var _NumPages           = 4;
var _CurrentRowOnPage   = [];

var _Cache_IP_Global                         = {};
var _Cache_IP_Global_IsValid                 = false;
var _Cache_IP_Global_Socket                  = [];
var _Cache_IP_Global_Socket_IsValid          = false;
var _Cache_IP_Global_Copyright               = {};
var _Cache_IP_Global_Copyright_IsValid       = false;
var _Cache_IP_Global_Interface               = [];
var _Cache_IP_Global_Interface_Name          = [];
var _Cache_IP_Global_Interface_IsValid       = false;

var _Feature_EmNetPresent = false;
var _Feature_Socket_Max   = false;

var _MiddlewareMissingMsgPrinted = false;

/*********************************************************************
*
* Common page handling
*
*********************************************************************/

/*********************************************************************
*
*       _ToStrBoolTrueFalse()
*
*  Function description:
*    Translates Value to "True" or "False"
*/
function _ToStrBoolTrueFalse(Value){
  var Result;

  if(Value == 0){
    Result = "False";
  } else {
    Result = "True";
  }
  return Result;
}

/*********************************************************************
*
*       _ToStrBoolYesNo()
*
*  Function description:
*    Translates value to "Yes" or "No"
*/
function _ToStrBoolYesNo(Value){
  var Result;

  if(Value == 0){
    Result = "No";
  } else {
    Result = "Yes";
  }
  return Result;
}

/*********************************************************************
*
*       _ToStrBoolEnabledDisable()
*
*  Function description:
*    Translates Value to "Enabled" or "Disabled"
*/
function _ToStrBoolEnabledDisable(Value){
  var Result;

  if(Value == 0){
    Result = "Disabled";
  } else {
    Result = "Enabled";
  }
  return Result;
}

/*********************************************************************
*
*       _ToStrByteSuffix()
*
*  Function description:
*    Translates Value to string
*/
function _ToStrByteSuffix(Value) {
  var Result;

  Result = Value.toString();
  if (Value == 1) {
    Result += " byte";
  } else {
    Result += " bytes";
  }
  return Result;
}

/*********************************************************************
*
*       _PadStart()
*
*  Functions description:
*    Pads the start of a string with a given character.
*    Width must be greater than string length to pad.
*    Strings which are shorter or equal to the width are returned
*    without being altered.
*/
function _PadStart (String, Width, Padding) {
  var Temp;
  
  if (String.length >= Width) {
    return String;
  }
  Width -= String.length;
  Temp   = "";
  for (var i = 0; i < Width; i++) {
    Temp += Padding;
  }
  return Temp + String;
}

/*********************************************************************
*
*       _TranslateHwAddr()
*
*  Functions description:
*    Converts an array of integers to a 
*    string containing a MAC Address.
*/
function _TranslateHwAddr (Array) {
  var Result;
  var TempStr;
  
  TempStr = "";
  Result = "";
  TempStr = Array[0].toString(16);
  TempStr = _PadStart(TempStr, 2, "0");
  Result += TempStr + ":";
  TempStr = Array[1].toString(16);
  TempStr = _PadStart(TempStr, 2, "0");
  Result += TempStr + ":";
  TempStr = Array[2].toString(16);
  TempStr = _PadStart(TempStr, 2, "0");
  Result += TempStr + ":";
  TempStr = Array[3].toString(16);
  TempStr = _PadStart(TempStr, 2, "0");
  Result += TempStr + ":";
  TempStr = Array[4].toString(16);
  TempStr = _PadStart(TempStr, 2, "0");
  Result += TempStr + ":";
  TempStr = Array[5].toString(16);
  TempStr = _PadStart(TempStr, 2, "0");
  Result += TempStr;
  return Result;
}

/*********************************************************************
*
*       _TranslateIpAddr()
*
*  Functions description:
*    Converts an integer to a 
*    string containing an IPv4 Address.
*/
function _TranslateIpAddr(Integer) {
  var part4 = Integer          & 255;
  var part3 = ((Integer >> 8)  & 255);
  var part2 = ((Integer >> 16) & 255);
  var part1 = ((Integer >> 24) & 255);

  return part4 + "." + part3 + "." + part2 + "." + part1;
}

/*********************************************************************
*
*       _UpdateFeatureFlags()
*
*  Functions description:
*    Updates feature flags.
*/
function _UpdateFeatureFlags() {
  _GetGlobalInfoFromTarget ();
  if (_Cache_IP_Global != undefined) {
    _Feature_EmNetPresent = true;
    if (typeof _Cache_IP_Global["Socket"]["Max"] != "undefined"){
      _Feature_Socket_Max = true;
    } else {
      _Feature_Socket_Max = false;
    }
  }
}

/*********************************************************************
*
*       _MiddlewareDetection()
*
*  Functions description:
*    Detects emNet.
*/
function _MiddlewareDetection() {
  _UpdateFeatureFlags()
  if (!_Feature_EmNetPresent) {
    if (!_MiddlewareMissingMsgPrinted) {
      TargetInterface.message("emNet plugin loaded but emNet middleware not part of current project (symbol IP_Global not found)")
      _MiddlewareMissingMsgPrinted = true;
    }
  }
}

/*********************************************************************
*
*       _ResetCache()
*
*  Functions description:
*    Resets all caches.
*/
function _ResetCache () {
  _Cache_IP_Global                         = {};
  _Cache_IP_Global_IsValid                 = false;
  _Cache_IP_Global_Socket                  = [];
  _Cache_IP_Global_Socket_IsValid          = false;
  _Cache_IP_Global_Copyright               = {};
  _Cache_IP_Global_Copyright_IsValid       = false;
  _Cache_IP_Global_Interface               = [];
  _Cache_IP_Global_Interface_Name          = [];
  _Cache_IP_Global_Interface_Driver        = [];
  _Cache_IP_Global_Interface_IsValid       = false;
}

/*********************************************************************
*
*       _getCurrentRow()
*
*  Functions description:
*    Returns the values of the currently selected row for a given page.
*/
function _getCurrentRow(PageName) {
  var Result;

  switch (PageName) {
    case "Overview":
      Result = _GetPageGlobalRow(_CurrentRowOnPage[PageName]);
      break;
    case "Sockets":
      Result = _GetPageSocketRow(_CurrentRowOnPage[PageName]);
      break;
    case "Interfaces":
      Result = _GetPageInterfaceRow(_CurrentRowOnPage[PageName]);
      break;
    case "Script Info":
      Result = _GetPageScriptInfoRow(_CurrentRowOnPage[PageName]);
      break;
    default:
      Result = null;
      break;
  }
  return Result;
}

/*********************************************************************
*
* Page Script Info
*
*********************************************************************/

/*********************************************************************
*
*       _GetPageScriptInfoColHeader()
*
*  Functions description:
*    Delivers the column headers of the respective page.
*/
function _GetPageScriptInfoColHeader () {
  var Result;

  Result = ["Script Info"];
  return Result;
}

/*********************************************************************
*
*       _GetPageScriptInfoRow()
*
*  Functions description:
*    Returns a row for the respective page.
*/
function _GetPageScriptInfoRow(RowIdx) {
  var Result;

  switch (RowIdx) {
    case 0:
      Result = ["emNet demo plug-in V1.0.0b"];
      break;
    case 1:
      Result = ["Last modified: 2022-09-02"];
      break;
    case 2:
      Result = ["Tested on emNet V3.43.06"];
      break;
    case 3:
      Result = ["Copyright (c) 2022 SEGGER Microcontroller GmbH"];
      break;
    default:
      Result = null;
      break;
  }
  return Result;
}

/*********************************************************************
*
* Page Overview
*
*********************************************************************/

/*********************************************************************
*
*       _UpdateTargetInfoIpGlobalCopyright()
*
*  Functions description:
*    Retrieves the global IP info record from the target
*/
function _UpdateTargetInfoIpGlobalCopyright() {
  if (_Cache_IP_Global_Copyright_IsValid == false) {
    _Cache_IP_Global_Copyright = Debug.evaluate("IP_Global.sCopyright");
    _Cache_IP_Global_Copyright_IsValid = true;
  }
}

/*********************************************************************
*
*       _GetPageGlobalColHeader()
*
*  Functions description:
*    Delivers the column headers of the respective page.
*/
function _GetPageGlobalColHeader () {
  var Result;

  _UpdateFeatureFlags();
  if (!_Feature_EmNetPresent) {
    return ["emNet not present in target software"];
  }
  Result = ["Info", "Value"];
  return Result;
}

/*********************************************************************
*
*       _GetGlobalInfoFromTarget()
*
*  Functions description:
*    Reads IP_Global from the target.
*/
function _GetGlobalInfoFromTarget () {
  var Expression;
  var MaxBufferSize;
  var MaxIFaces;
  var NumIFaces;
  
  if (_Cache_IP_Global_IsValid) {
    return;
  }
  
  MaxBufferSize     = {};
  MaxIFaces         = {};
  NumIFaces         = {};
  _Cache_IP_Global  = [];

  Expression             = "IP_Global";
  _Cache_IP_Global       = Debug.evaluate(Expression);
  _UpdateTargetInfoIpGlobalCopyright();
  _Cache_IP_Global_IsValid = true;
}

/*********************************************************************
*
*       _GetPageGlobalRow()
*
*  Functions description:
*    Returns a row for the respective page.
*/
function _GetPageGlobalRow(RowIdx) {
  var Result;
  var SocketMax;
  
  Result = [];
  _UpdateFeatureFlags();
  if (!_Feature_EmNetPresent) {
    return null;
  }
  TargetInterface.message("_GetPageGlobalRow(): RowIdx=" + RowIdx);
  
  switch(RowIdx){
    case 0:
      _GetGlobalInfoFromTarget();
      _GetInterfaceInfoFromTarget();
      Result = ["Config Completed", _ToStrBoolYesNo(_Cache_IP_Global["ConfigCompleted"])];
      break;
    case 1:
      Result = ["Init Completed",   _ToStrBoolYesNo(_Cache_IP_Global["InitCompleted"])];
      break;
    case 2:
      Result = ["IPv4",             _ToStrBoolEnabledDisable(!_Cache_IP_Global["DisableIPv4"])];
      break;
    case 3:
      Result = ["Low Level Checks", _ToStrBoolEnabledDisable(!_Cache_IP_Global["DoNotAddLowLevelChecks"])];
      break;
    case 4:
      if (_Feature_Socket_Max) {
        SocketMax =  _Cache_IP_Global["Socket"]["Max"];
      } else {
        SocketMax = "<n/a>";
      }
      if (_Cache_IP_Global["Socket"]["Limit"] == 0){
        Result = ["Sockets", "Currently used: " + _Cache_IP_Global["Socket"]["Cnt"] + ", max ever used: " + SocketMax + ", limit: unlimited"];
      } else {
        Result = ["Sockets", "Currently used: " + _Cache_IP_Global["Socket"]["Cnt"] + ", max ever used: " + SocketMax + ", limit: " + _Cache_IP_Global["Socket"]["Limit"]];
      }
      break;
    case 5:
      Result = ["Interfaces", "Max: " + _Cache_IP_Global["MaxIFaces"] + ", configured: " + _Cache_IP_Global["NumIFaces"]];
      break;
    case 6:
      Result = ["Primary Interface", "ID: " + _Cache_IP_Global["PrimaryIFaceId"]];
      for (var i = 0; i <= _Cache_IP_Global_Interface_Name.length; i++) {
        if (i == _Cache_IP_Global["PrimaryIFaceId"]) {
          Result[1] += ", type: " + _Cache_IP_Global_Interface_Name[i];
          break;
        }
      }
      break;
    case 7:
      Result = ["Maximum Buffer Size", _ToStrByteSuffix(_Cache_IP_Global["MaxBufferSize"])];
      break;
    case 8:
      var i;
      
      i = 0;
      Result = ["Buffers", ""];
      while(typeof _Cache_IP_Global["aBufferConfig"][i] != "undefined" && _Cache_IP_Global["aBufferConfig"][i]["NumBuffers"] != 0){
        Result[1] += _Cache_IP_Global["aBufferConfig"][i]["NumBuffers"] + " buffers @ " + _ToStrByteSuffix(_Cache_IP_Global["aBufferConfig"][i]["BufferSize"]) + ", ";
        i++;
      }
      Result[1] = Result[1].slice(0, -2);
      break;
    case 9:
      Result = ["Copyright", _Cache_IP_Global_Copyright];
      break;
    default:
  }
  return Result;
}

/*********************************************************************
*
* Page Sockets
*
*********************************************************************/

/*********************************************************************
*
*       _GetPageSocketColHeader()
*
*  Functions description:
*    Delivers the column headers of the respective page.
*/
function _GetPageSocketColHeader () {
  var Result;
  
  _UpdateFeatureFlags();
  if (!_Feature_EmNetPresent) {
    return ["emNet not present in target software"];
  }
  Result = ["Socket", "Error", "Receive Buffer", "Send Buffer"];
  return Result;
}

/*********************************************************************
*
*       _GetSocketInfoFromTarget()
*
*  Functions description:
*    Reads the linked list of sockets from the target
*/
function _GetSocketInfoFromTarget () {
  var Expression;
  var RootPtr;

  if (_Cache_IP_Global_Socket_IsValid) {
    return;
  }
  _Cache_IP_Global_Socket = [];
  Expression              = "IP_Global.Socket.List.pFirstSock";
  RootPtr                 = Debug.evaluate(Expression);
  TargetInterface.message("_GetSocketInfoFromTarget(): RootPtr=" + RootPtr.toString());
  if (RootPtr != 0) {
    var CurrentItem;
    var NextElem;
    var NextElemAddr;
    var NextMBlock;
    //
    // Loop through sockets
    //
    CurrentItem = {};
    Expression  = "*IP_Global.Socket.List.pFirstSock";
    do {
      var CurrentRxBuf;
      var CurrentTxBuf;
      var CurrentSocket;
  
      CurrentItem = Debug.evaluate(Expression);
      if (typeof CurrentItem != "undefined") {
        CurrentRxBuf               = CurrentItem["SockBufRx"];
        CurrentTxBuf               = CurrentItem["SockBufTx"];
        
        CurrentSocket              = {};
        CurrentSocket["Handle"]    = CurrentItem["Handle"];
        CurrentSocket["Error"]     = CurrentItem["Error"];
        CurrentSocket["RxBuf"]     = CurrentRxBuf["NumBytes"];
        CurrentSocket["RxBufMax"]  = CurrentRxBuf["Limit"];
        CurrentSocket["TxBuf"]     = CurrentTxBuf["NumBytes"];
        CurrentSocket["TxBufMax"]  = CurrentTxBuf["Limit"];
        //
        // RX Buffers
        //
        CurrentSocket["RxMBlocks"] = 0;
        if (CurrentRxBuf["pFirstMBlock"] != 0 ) {
          NextMBlock = CurrentRxBuf["pFirstMBlock"];
          do {
            var CurrentMBlock;
            
            CurrentSocket["RxMBlocks"] += 1;
            CurrentMBlock = {}
            Expression    = "*(MBLOCK*)" + NextMBlock.toString();
            CurrentMBlock = Debug.evaluate(Expression);
            if (typeof CurrentMBlock["pNext"] != "unavailable") {
              NextMBlock = CurrentMBlock["pNext"];
            } else {
              NextMBlock = 0;
            }
          } while (NextMBlock != 0)
        }
        //
        // TX Buffers
        //
        CurrentSocket["TxMBlocks"] = 0;
        if (CurrentTxBuf["pFirstMBlock"] != 0) {
          NextMBlock = CurrentTxBuf["pFirstMBlock"];
          do {
            var CurrentMBlock;
            
            CurrentSocket["TxMBlocks"] += 1;
            CurrentMBlock = {}
            Expression    = "*(MBLOCK*)" + NextMBlock.toString();
            CurrentMBlock = Debug.evaluate(Expression);
            if (typeof CurrentMBlock["pNext"] != "unavailable") {
              NextMBlock = CurrentMBlock["pNext"];
            } else {
              NextMBlock = 0;
            }
          } while (NextMBlock != 0)
        }
        //
        // Add Socket
        //
        _Cache_IP_Global_Socket.push(CurrentSocket);
        NextElem     = CurrentItem["Next"];
        NextElemAddr = NextElem["pSocket"];
        if (typeof NextElemAddr != "undefined") {
          Expression   = "*(SOCKET*)" + NextElemAddr.toString();
        } else {
          NextElemAddr = 0;
        }
      } else {
        NextElemAddr = 0;
      }
    } while (NextElemAddr != 0); 
  }
  _Cache_IP_Global_Socket_IsValid = true;   
}

/*********************************************************************
*
*       _GetPageSocketRow()
*
*  Functions description:
*    Returns a row for the respective page.
*/
function _GetPageSocketRow(RowIdx) {
  var Result;
  var NumSocket;
  
  _UpdateFeatureFlags();
  if (!_Feature_EmNetPresent) {
    return null;
  }
  _GetSocketInfoFromTarget();
  NumSocket = _Cache_IP_Global_Socket.length;
  TargetInterface.message("_GetPageSocketRow(): RowIdx=" + RowIdx.toString() + ", NumSocket=" + NumSocket.toString());
  if (RowIdx < NumSocket) {
    var Handle;
    var Error;
    var RxBuf;
    var RxBufMax;
    var RxMBlocks;
    var TxBuf;
    var TxBufMax;
    var TxMBlocks;

    Result    = [];
    Handle    = _Cache_IP_Global_Socket[RowIdx]["Handle"];
    if (_Cache_IP_Global_Socket[RowIdx]["Error"] == 0) {
      Error = "None";
    } else {
      Error     = _Cache_IP_Global_Socket[RowIdx]["Error"];
    }
    RxBuf     = _Cache_IP_Global_Socket[RowIdx]["RxBuf"];
    RxBufMax  = _Cache_IP_Global_Socket[RowIdx]["RxBufMax"];
    RxMBlocks = _Cache_IP_Global_Socket[RowIdx]["RxMBlocks"];
    TxBuf     = _Cache_IP_Global_Socket[RowIdx]["TxBuf"];
    TxBufMax  = _Cache_IP_Global_Socket[RowIdx]["TxBufMax"];
    TxMBlocks = _Cache_IP_Global_Socket[RowIdx]["TxMBlocks"];
    
    Result.push (Handle.toString());
    Result.push (Error);
    Result.push ("Size: " + _ToStrByteSuffix(TxBufMax) + ", used: " + _ToStrByteSuffix(TxBuf) + ", packets used: " + TxMBlocks.toString());
    Result.push ("Size: " + _ToStrByteSuffix(RxBufMax) + ", used: " + _ToStrByteSuffix(RxBuf) + ", packets used: " + RxMBlocks.toString());
  } else {
    Result = null;
  }
  return Result;
}

/*********************************************************************
*
* Page Interfaces
*
*********************************************************************/

/*********************************************************************
*
*       _GetInterfaceDuplex()
*
*  Functions description:
*
*/
function _GetInterfaceDuplex (Duplex) {
  var Result;
  switch(Duplex) {
    case 0:
      Result = "Down";
      break;
    case 1:
      Result = "Half Duplex";
      break;
    case 2:
      Result = "Full Duplex";
      break;
    default:
      Result = null;
      break;
  }
  return Result;
}

/*********************************************************************
*
*       _GetPageInterfaceColHeader()
*
*  Functions description:
*    Delivers the column headers of the respective page.
*/
function _GetPageInterfaceColHeader () {
  var Result;

  _UpdateFeatureFlags();
  if (!_Feature_EmNetPresent) {
    return ["emNet not present in target software"];
  }
  Result = ["Interface Nr.", "Type", "Link state", "Driver", "MAC-Address", "IP-Address", "Default Gateway", "Primary DNS", "MTU"];
  return Result;
}

/*********************************************************************
*
*       _GetInterfaceInfoFromTarget()
*
*  Functions description:
*    Reads the linked list of Interfaces from the target
*/
function _GetInterfaceInfoFromTarget () {
  var Expression;
  var NumIFaces;
  
  if (_Cache_IP_Global_Interface_IsValid) {
    return;
  }
  
  _Cache_IP_Global_Interface = [];
  Expression                 = "IP_Global.NumIFaces";
  NumIFaces                  = Debug.evaluate(Expression)
  for (var i = 0; i < NumIFaces; i++) {
    var CurrentInterface;
    var CurrentName;
    var CurrentDriver;
    
    Expression        = "IP_Global.paIFace[" + i + "]";
    CurrentInterface  = Debug.evaluate(Expression);
    Expression        = "IP_Global.paIFace[" + i + "].sName";
    CurrentName       = Debug.evaluate(Expression); 
    CurrentDriver     = Debug.getSymbol(CurrentInterface["pDriver"]); 
    _Cache_IP_Global_Interface.push(CurrentInterface);
    _Cache_IP_Global_Interface_Name.push(CurrentName);
    _Cache_IP_Global_Interface_Driver.push(CurrentDriver);
  }
  _Cache_IP_Global_Interface_IsValid = true;
}

/*********************************************************************
*
*       _GetPageInterfaceRow()
*
*  Functions description:
*    Returns a row for the respective page.
*/
function _GetPageInterfaceRow(RowIdx) {
  var Result;
  var NumInterface;
  
  _UpdateFeatureFlags();
  if (!_Feature_EmNetPresent) {
    return null;
  }
  _GetInterfaceInfoFromTarget();
  _GetGlobalInfoFromTarget();
  NumInterface = _Cache_IP_Global_Interface.length;
  TargetInterface.message("_GetPageInterfaceRow(): RowIdx=" + RowIdx.toString() + ", NumInterface=" + NumInterface.toString());
  if (RowIdx < NumInterface) {
    var Unit;
    var Name;
    var LinkState;
    var Driver;
    var HwAddress;
    var IpAddress;
    var Gateway;
    var DNS;
    var MTU;
    
    Result = []
    Unit      = _Cache_IP_Global_Interface[RowIdx]["Unit"];
    if (_Cache_IP_Global_Interface[RowIdx]["Unit"] == _Cache_IP_Global["PrimaryIFaceId"]) {
      Unit += " (primary)";
    }
    Name      = _Cache_IP_Global_Interface_Name[RowIdx];
    if (_Cache_IP_Global_Interface[RowIdx]["LinkSpeedEx"] == 0) {
      if (_Cache_IP_Global_Interface[RowIdx]["LinkSpeed"] == 0) {
        LinkState = "Down";
      } else {
        LinkState = _Cache_IP_Global_Interface[RowIdx]["LinkSpeed"] / 1000000 + " MHz, " + _GetInterfaceDuplex(_Cache_IP_Global_Interface[RowIdx]["LinkDuplex"]);
      }
    } else {
      LinkState = _Cache_IP_Global_Interface[RowIdx]["LinkSpeedEx"] / 1000000 + " MHz, " + _GetInterfaceDuplex(_Cache_IP_Global_Interface[RowIdx]["LinkDuplex"]);
    }
    Driver    = _Cache_IP_Global_Interface_Driver[RowIdx];
    HwAddress = _TranslateHwAddr(_Cache_IP_Global_Interface[RowIdx]["abHWAddr"]);
    IpAddress = _TranslateIpAddr(_Cache_IP_Global_Interface[RowIdx]["IpAddr"]);
    Gateway   = _TranslateIpAddr(_Cache_IP_Global_Interface[RowIdx]["DefGateway"]);
    DNS       = _TranslateIpAddr(_Cache_IP_Global_Interface[RowIdx]["aDNSServer"][0]);
    MTU       = _ToStrByteSuffix(_Cache_IP_Global_Interface[RowIdx]["Mtu"]);
    
    Result.push(Unit.toString());
    Result.push(Name);
    Result.push(LinkState);
    Result.push(Driver);
    Result.push(HwAddress);
    Result.push(IpAddress);
    Result.push(Gateway);
    Result.push(DNS);
    Result.push(MTU);
  } else {
    Result = null;
  }
  return Result;
}

/*********************************************************************
*
*       Exposed functions
*
**********************************************************************
*/

/*********************************************************************
*
*       init()
*
*  Functions description:
*    This function... Not yet sure what it does...
*/
function init() {
  _ResetCache();
  
  _CurrentRowOnPage["Overview"]     = 0;
  _CurrentRowOnPage["Interfaces"]   = 0;
  _CurrentRowOnPage["Sockets"]      = 0;
  _CurrentRowOnPage["Script Info"]  = 0;
}

/*********************************************************************
*
*       getName()
*
*  Functions description:
*    Returns the name of the middleware this script supplies support
*    for.
*/
function getName() {
  return "emNet";
}

/*********************************************************************
*
*       getPages()
*
*  Functions description:
*    Returns the names of the pages this script is capable to display.
*/
function getPages() {
  var PageNames = ["Overview",
                   "Sockets",
                   "Interfaces",
                   "Script Info"];
  return PageNames;
}

/*********************************************************************
*
*       getColHeaders()
*
*  Functions description:
*    Returns the names of the colums for a given page.
*/
function getColHeaders(PageName) {
  var Result;
  
  _MiddlewareDetection();
  switch (PageName) {
    case "Overview":
      Result = _GetPageGlobalColHeader();
      break;
    case "Sockets":
      Result = _GetPageSocketColHeader();
      break;
    case "Interfaces":
      Result = _GetPageInterfaceColHeader();
      break;
    case "Script Info":
      Result = _GetPageScriptInfoColHeader();
      break;
    default:
      Result = null;
      break;
  }
  return Result;
}

/*********************************************************************
*
*       getFirstRow()
*
*  Functions description:
*    Returns the values of the 1st row for a given page.
*/
function getFirstRow(PageName) {
  var Result;

  if (typeof _CurrentRowOnPage[PageName] != "undefined") {
    _CurrentRowOnPage[PageName] = 0;
    Result = _getCurrentRow(PageName);
  } else {
    Result = null;
  }
  return Result;
}

/*********************************************************************
*
*       getNextRow()
*
*  Functions description:
*    Returns the values of the next row for a given page.
*/
function getNextRow(PageName) {
  var Result;
  
  if (typeof _CurrentRowOnPage[PageName] != "undefined") {
    _CurrentRowOnPage[PageName] += 1;
    Result = _getCurrentRow(PageName);
  } else {
    Result = null;
  }
  return Result;
}

/*********************************************************************
*
*       onTargetChanged()
*
*  Functions description:
*    Handles the event of target state having changed.
*/
function onTargetChanged() {
  _ResetCache();
}

/****** End Of File *************************************************/
