Applicable Versions
NetSim Standard
NetSim Pro


Applicable Releases
v13.0


New commands to turn off nodes can be added to the command library of NetSim CLI Interpreter and functions can be defined to validate and execute the commands. Consider the following command to turn off a node:

turnoff <DEVICE_NAME>


which takes the device_name as a parameter along with it and uses it to turn a specific node from ON to OFF state.

A new file ZigBee_Commands.c is added to the CLIInterpretor Project, which contains the following source code:


#include "main.h"
#include <signal.h>
#include "CLI.h"
#include "../ZigBee/802_15_4.h"

bool validate_turn_off_device_command(ptrCLIENTINFO info, ptrCOMMANDARRAY command, int index)
{
    if (command->length - index < 2)
    {
        send_message(info, "USAGE: turnoff deviceName\n");
        return false;
    }

    if (_stricmp(command->commands[index], "turnoff"))
        return false;

    if (!isCommandAsDeviceName(command->commands[index + 1]))
    {

        send_message(info, "%s is not a valid device name.\n",
            command->commands[index + 1]);
        return false;

    }
    return true;
}

void execute_turn_off_device_command(ptrCLIENTINFO info, ptrCOMMANDARRAY command, int index, NETSIM_ID d)
{
    NETSIM_ID dest;
    if (isCommandAsDeviceName(command->commands[index + 1]))
    {
        dest = fn_NetSim_Stack_GetDeviceId_asName(command->commands[index + 1]);
        fn_NetSim_ZigBee_turn_off_node(d);
        send_message(info, "OK!\n");
        return;
    }
}


Further modifications are done to the existing validate_comand() function that is part of the SimulationCommand.c file to make calls to the functions newly added in the ZigBee_Commands.c file as shown below:

bool validate_command(ptrCLIENTINFO info, ptrCOMMANDARRAY command)
{
	int index = 0;
	if (!info->promptString)
	{
		if (isCommandAsDeviceName(command->commands[0]))
			index = 1;

		if (!_stricmp(command->commands[index], "route"))
			return validate_route_command(info, command, index);

		if (!_stricmp(command->commands[index], "acl"))
			return validate_acl_command(info, command, index);

		if (!_stricmp(command->commands[index], CMD_ACLCONFIG))
			return true;

		if (!_stricmp(command->commands[index], "ping"))
			return validate_ping_command(info, command, index);

		if (!_stricmp(command->commands[index], "turnoff"))
			return validate_turn_off_device_command(info, command, index);
	}
	else
	{
		if (strstr(info->promptString, CMD_ACLCONFIG))
			return validate_aclconfig_command(info, command, index);
	}

	send_message(info, "%s command is not a valid command.\n",
				 command->commands[index]);
	return false;
}

And also modifications are done to the existing execute_command() function that is part of the SimulationCommand.c file to make calls to the functions newly added in the ZigBee_Commands.c file as shown below:

void execute_command(ptrCLIENTINFO info, ptrCOMMANDARRAY command, NETSIM_ID d)
{
	int index = 0;
	if (info->promptString)
	{ 
		if (strstr(info->promptString, CMD_ACLCONFIG))
			execute_prompt_aclconfig_command(info, command, index, d);
	}
	else
	{
		if (isCommandAsDeviceName(command->commands[0]))
		{
			d = fn_NetSim_Stack_GetDeviceId_asName(command->commands[0]);
			index = 1;
		}

		if (!_stricmp(command->commands[index], "route"))
			execute_route_command(info, command, index, d);
		else if (!_stricmp(command->commands[index], "ACL"))
			execute_acl_command(info, command, index, d);
		else if (!_stricmp(command->commands[index], "ACLCONFIG"))
			execute_aclconfig_command(info, command, index, d);
		else if (!_stricmp(command->commands[index], "ping"))
			execute_ping_command(info, command, index, d);
		else if (!_stricmp(command->commands[index], "turnoff"))
			execute_turn_off_device_command(info, command, index, d);
	}

}


Changes are done in the ZigBee Source code project:

To the ChangeRadioState.c file of ZigBee source code project a new function fn_NetSim_ZigBee_turn_off_node() has been added as shown below:


_declspec(dllexport) int fn_NetSim_ZigBee_turn_off_node(NETSIM_ID nDeviceId)
{
  ptrIEEE802_15_4_PHY_VAR phy = WSN_PHY(nDeviceId);
  phy->nRadioState = RX_OFF;
  WSN_MAC(nDeviceId)->nNodeStatus = OFF;
  return 0;
}


Add the function prototype to the 802_15_4.h file as shown below:


_declspec(dllexport) int fn_NetSim_ZigBee_turn_off_node(NETSIM_ID nDeviceId);



Add a line to import ZigBee library files in the CLI.h file in order to make calls to the functions defined in the ZigBee project, from the CLIInterpretor project as shown below:


#pragma comment(lib, "libZigBee.lib")



Access ZigBee Project properties by right-clicking on ZigBee project and selecting properties. Go to Linker->Advanced and set the Import Library value to 

..\lib_x64\lib$(TargetName).lib

as shown below:



Build ZigBee source code project, followed by the CLIInterpretor project. Upon successful build, NetSim automatically updates the binaries which will be used during subsequent simulation runs.


Consider the following IoT network scenario:



Run the simulation with interactive simulation set to true in the Run time Interaction tab which is part of the Run Simulation window as shown below:



A sample python socket interface file is attached herewith for reference which contains the following code to connect to the NetSim socket interface and pass commands to turn off nodes in the network:


# An example script to send client request to NetSim server using socket programming in Python
import socket # for socket
import sys
import time

#----------------------Socket code-----------------------
try:
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    print ("Socket successfully created.")
except socket.error as err:
    print ("Socket creation failed with error %s" %(err))

# default port for socket
port = 8999

try:
    host_ip = socket.gethostbyname('127.0.0.1')
except socket.gaierror:
    # this means could not resolve the host
    print ("Error resolving host.")
    sys.exit()

# connecting to the server
s.connect((host_ip, port))
print ("Connection established to NetSim.")

#print ('each = ', each)

name = 'Wireless_Sensor_3'
name = name + '\0'
s.send(name.encode())

command = 'turnoff Wireless_Sensor_3'
command = command + '\0'
s.send(command.encode())

resp = s.recv(1024).decode('utf-8')
cont = '__continue__'
while cont not in resp:
    resp = resp + s.recv(1024).decode('utf-8')

print ("Received:", resp)

s.close()

When running the simulation in NetSim, the simulation console halts for a socket client to connect, as shown below:



Run the python socket interface file, with the device name modified as per the node which you want to turn off. 



The impact of this can be seen in the simulation results. If the node turned off was part of any of the applications as source, destination or intermediate relay node then it will affect packet transmissions.