Applicable Versions
NetSim Standard
NetSim Pro


NetSim allows users to add additional metrics tables to the Simulation Results window in addition to the default set of tables that are available at the end of any simulation. This has to be done via code by editing the source codes of the respective protocol.

Every protocol has the main C file which contains a Metrics() function. For Eg: TCP project will have a TCP.c file, UDP will have an UDP.c file etc. 

For illustration, an example regarding Wireless Sensor Network is provided. In this example, parameters such as Sensor Node Name, Residual Energy, State (On/Off) and turn–off time are tracked and added to a new table in the Simulation Results window.


Applicable Releases
v11
v12
v13.0


After loading the source codes in Visual Studio, perform the following modifications: 

Step 1: Copy the provided code at the top in 802_15_4.h file 


double NetSim_Off_Time[100]; //Supports upto Device ID 100. Array size can be increased for higher number of Devices/Device ID's


Step 2: Add the header file in 802_15_4.c file 


#include "../BatteryModel/BatteryModel.h" 


Step 3: Copy the below code (in red color) in 802_15_4.c file (inside fn_NetSim_Zigbee_Metrics() function) 


/** This function write the metrics in metrics.txt */ 

_declspec(dllexport) int fn_NetSim_Zigbee_Metrics(PMETRICSWRITERmetricsWriter)

 { 

//CUSTOM METRICS  

    ptrIEEE802_15_4_PHY_VAR phy;

    ptrBATTERY battery;

    char radiostate[BUFSIZ];

    NETSIM_ID nDeviceCount = NETWORK->nDeviceCount;

    //Set table name  

    PMETRICSNODE table = init_metrics_node(MetricsNode_Table, "NODE_FAILURE_METRICS", NULL);

    //set table headers  

    add_table_heading(table, "Node Name", true, 0);

    add_table_heading(table, "Status(ON/OFF)", true, 0);

    add_table_heading(table, "Residual_Energy (mJ)", true, 0);

    add_table_heading(table, "Time - Turned OFF (microseconds)", false, 0);

    for (int i = 1; i <= nDeviceCount; i++)

    {

        sprintf(radiostate, "ON");

        phy = WSN_PHY(i);

        if (strcmp(DEVICE(i)->type, "SENSOR"))

            continue;

        if (WSN_MAC(i)->nNodeStatus == 5 || phy->nRadioState==RX_OFF)

            sprintf(radiostate, "OFF");

        //Add table data   

        add_table_row_formatted(false, table, "%s,%s,%.2lf,%.2lf,", DEVICE_NAME(i), radiostate, battery_get_remaining_energy((ptrBATTERY)phy->battery), NetSim_Off_Time[i]);

    }

    PMETRICSNODE menu = init_metrics_node(MetricsNode_Menu, "CUSTOM_METRICS", NULL);

    add_node_to_menu(menu, table);

    write_metrics_node(metricsWriter, WriterPosition_Current, NULL, menu);

    delete_metrics_node(menu);

    //CUSTOM METRICS

return fn_NetSim_Zigbee_Metrics_F(metricsWriter);  


Step 4:Copy the below code (in red colour) at the end of ChangeRadioState.c file  


if(isChange)

  {   

phy->nOldState = nOldState;    

phy->nRadioState = nNewState;    

}  

else  

{   

phy->nRadioState = RX_OFF;   

WSN_MAC(nDeviceId)->nNodeStatus = OFF; 

NetSim_Off_Time[nDeviceId] = ldEventTime;

 return isChange;                          

}

 

Step 5: Build DLL with the modified code and run a Wireless Sensor Network scenario. After Simulation, user will notice a new Performance metrics named “Custom Metrics” is added. The newly added NODE_FAILURE_METRICS table is shown below: 


Applicable Releases
v10.2


After loading the source codes in Visual Studio, perform the following modifications:

Step 1: Copy the provided code at the top in 802_15_4.h file

double NetSim_Off_Time[100];

Step 2:

Copy the below code (in red colour) in 802_15_4.c file (inside      fn_NetSim_Zigbee_Metrics() function)

/** This function write the metrics in metrics.txt */

_declspec(dllexport) int fn_NetSim_Zigbee_Metrics(PMETRICSWRITERmetricsWriter)

{

//CUSTOM METRICS
int i;
char radiostate[BUFSIZ];
NETSIM_ID nDeviceCount = NETWORK->nDeviceCount;

//Set table name
PMETRICSNODE table = init_metrics_node(MetricsNode_Table, "NODE_FAILURE_METRICS", NULL);

//set table headers
add_table_heading(table, "Node Name", true, 0);
add_table_heading(table, "Status(ON/OFF)", true, 0);
add_table_heading(table, "Residual_Energy (mJ)", true, 0);
add_table_heading(table, "Time - Turned OFF (microseconds)", false, 0);

for (i = 1; i <= nDeviceCount; i++)
{
sprintf(radiostate, "ON");
if (strcmp(DEVICE(i)->type, "SENSOR"))
continue;
if (WSN_MAC(i)->nNodeStatus == OFF)
sprintf(radiostate, "OFF");

//Add table data
add_table_row_formatted(false, table, "%s,%s,%.2lf,%.2lf,",DEVICE_NAME(i),radiostate,pstruDevicePower[i-1]->dRemainingPower, NetSim_Off_Time[i - 1]);
}


PMETRICSNODE menu = init_metrics_node(MetricsNode_Menu,"CUSTOM_METRICS", NULL);
add_node_to_menu(menu, table);
write_metrics_node(metricsWriter, WriterPosition_Current, NULL, menu);
delete_metrics_node(menu);

//CUSTOM METRICS

            return fn_NetSim_Zigbee_Metrics_F(metricsWriter);        

}

Step 4: 

Copy the below code (in red colour) at the end of ChangeRadioState.c file


if(nStatus)
{
WSN_PHY(nDeviceId)->nOldState = nOldState;
WSN_PHY(nDeviceId)->nRadioState = nNewState;
return nStatus;
}


else
{
WSN_PHY(nDeviceId)->nRadioState = RX_OFF;
WSN_MAC(nDeviceId)->nNodeStatus = OFF;
 NetSim_Off_Time[nDeviceId - 1] = ldEventTime;
return nStatus;
}

Step 5:

Build DLL with the modified code and run a Wireless Sensor Network scenario. After Simulation, the user will notice a new Performance metrics named “Custom Metrics” is added. The newly added NODE_FAILURE_METRICS table is shown below: