Applicable Versions
NetSim StandardNetSim Pro



In terms of simulation, fog computing essentially translates to having compute capability at the edge (or) at the intermediate devices. 

                                           Image result for fog computing

 

In this example we have a sensor network from which sensed data is being sent to the server. Rather than do the processing at the server some compute operations are carried out at the gateway/router.


Sensors send readings of temperature through the LoWPAN Gateway. The Gateway will forward the packets to the server if the sensed parameter (temperature reading by Sensor) is greater than a specific threshold.  Else the Gateway drops the packet. Appropriately scaled random samples are drawn for the temperature readings

image


This example can be done in NetSim by:

 

1. Modifying the application payload of the data being sent by the sensor (See https://tetcos.freshdesk.com/solution/articles/14000041020-how-do-i-change-the-payload-of-a-packet-in-netsim-for how to change the payload)

2. Writing a function to generate random temperature readings between MIN and MAX

3. Then in the function int fn_NetSim_IP_Run() in IP.c, writing the condition in Network IN Event that if the current device ID matches that of the gateway/router, and then check the payload. 

4. Finally, forwarding the packet if the temperature value is greater than THRESHOLD, or else dropping the packet. 

_________________________________________________________________________________________________


Procedure


1. Configure the random temperature values at the source sensors


The payload of the packet is added in the application layer of the source while generating the packet.

Let's suppose the temperature varies from 0 to 99. We generate a random number in this range and copy that value onto the packet payload. 


To change the payload the following steps can be followed:

Step 1: Go to NetSim Home > Click on Your Work > Workspace Options > Open Code  Go to copy_payload() function present in Application.c file inside Application project.

Step 2: Inside the function copy_payload(), make the modifications as given below (highlighted in red) :


void copy_payload(UINT8 real[], NetSim_PACKET* packet, unsigned int* payload, APP_INFO* info)

{

u_short i;

uint32_t key = 16;

int r = rand() % 100;

char s[3];

s[1] = (char)((r % 10) + '0');

s[0] = (char)((r / 10) % 10 + '0');

s[2] = '\0';

int slength = strlen(s);

if (payload)

{

/*for (i = 0; i < *payload; i++)

{

if (info->encryption == Encryption_XOR)

real[i] = xor_encrypt('a' + i % 26, 16);

else

real[i] = 'a' + i % 26;

}*/

for (i = 0; i < *payload; i++)

{

if(i== slength) break;

if (info->encryption == Encryption_XOR)

real[i] = xor_encrypt(s[i % slength], 16);

else

real[i] = s[i%slength];

if (info->encryption == Encryption_TEA)

encryptBlock(real, payload, &key);

else if (info->encryption == Encryption_AES)

aes256(real, payload);

else if (info->encryption == Encryption_DES)

des(real, payload);

}

}


Step 3: Now make modification highlighted in red in int fn_NetSim_Add_DummyPayload(NetSim_PACKET* packet,APP_INFO* info) in Application.c


while (packet)

{

if (!packet->szPayload &&

packet->pstruAppData &&

packet->pstruAppData->dPacketSize &&

packet->pstruAppData->nAppType != TRAFFIC_EMULATION /* Don't set payload in

emulation */ /*&&

wireshark_flag*/ )

{

unsigned int size = (unsigned int)packet->pstruAppData->dPacketSize;


Step 4: Now make modification highlighted in red in 

_declspec(dllexport) int fn_NetSim_Application_Init(struct stru_NetSim_Network *NETWORK_Formal,                    NetSim_EVENTDETAILS *pstruEventDetails_Formal, char *pszAppPath_Formal, char *pszWritePath_Formal,           int nVersion_Type, void **fnPointer) in Application.c 


_declspec(dllexport) int fn_NetSim_Application_Init(struct stru_NetSim_Network *NETWORK_Formal,

                                                    NetSim_EVENTDETAILS *pstruEventDetails_Formal,

                                                    char *pszAppPath_Formal,

                                                    char *pszWritePath_Formal,

                                                    int nVersion_Type,

                                                    void **fnPointer)

{

    srand(time(0));

    return fn_NetSim_Application_Init_F(NETWORK_Formal,

        pstruEventDetails_Formal,

        pszAppPath_Formal,

        pszWritePath_Formal,

        nVersion_Type,

        fnPointer);

}


2. Find the LoWPAN Gateway ID


In order to ensure that the computing happens only in the Gateway, we use the device type and interface count parameters to differentiate Gateway node from the sensor nodes.

To find the Gateway Id the following steps can be followed:

Step 1: Go to fn_NetSim_IP_Run() function present in IP.c file inside IP project.

Step 2: Inside the function fn_NetSim_IP_Run(),make the modifications as given below (highlighted in red) :


declspec(dllexport) int fn_NetSim_IP_Run()

{

int i = 0; int GatewayDeviceId = -1;

for (i = 0; i < NETWORK->nDeviceCount; i++)

{

    if (DEVICE(i+1)->nDeviceType == 6 && DEVICE(i + 1)->nNumOfInterface == 2 && 

            DEVICE(i + 1)->ppstruInterfaceList[0]->pstruMACLayer->nMacProtocolId == MAC_PROTOCOL_IEEE802_15_4)


            GatewayDeviceId = NETWORK->ppstruDeviceList[i]->nDeviceId;


    }

switch (pstruEventDetails->nEventType)

{

case NETWORK_OUT_EVENT:

 

3. Add condition to forward or drop the packet and Store the details of each packet in a file 

Now whether the packet is forwarded or dropped by the LoWPAN Gateway depends on the payload value (Temperature reading) and threshold value. In this case the threshold value is set to 50. So packet will be forwarded by the Gateway if payload value is greater than equals to 50, else will get dropped. These Modification are added in the IP module. It is also ensured that the payload is verified only for data packets and not for control packets. In NetSim all control packets have the id  0. So if packet Id is zero then there is no check done.


In order to obtain a log file containing details such as PayLoad value, Sender Id, Packet Status(Forwarded or Dropped) and packet Id, the following steps are to be followed:

Step 1: Open fn_NetSim_IP_Init() function present in IP.c file inside IP project.

Step 2: Inside the function fn_NetSim_IP_Init() make the modifications as given below (highlighted in red) :

{

FILE* fp;

char filename[BUFSIZ] ;
sprintf(filename, "%s\\TemplogFile.csv", pszIOLogPath);
 

fp = fopen(filename, "w+");

if (fp)

{

fprintf(fp, "PAYLOAD,STATUS,SENDER_ID,PACKET_ID,"); 

fclose(fp);

}

NETSIM_ID loop;

if (nVersion_Type / 10 != VERSION)

{

printf("IP---Version number mismatch\n Dll Version=%d\n NetSim Version=%d\n FileName=%s\n Line=%d\n", VERSION, nVersion_Type / 10, __FILE__, __LINE__);

exit(0);

}



To add the condition following steps can be followed:

Step 1: Open fn_NetSim_IP_Run() function present in IP.c file inside IP project.

Step 2: Inside the function fn_NetSim_IP_Run(), make the modifications as given below (highlighted in red) :

switch (pstruEventDetails->nEventType)
{
case NETWORK_OUT_EVENT:
{
ptrIP_FORWARD_ROUTE route = NULL;
 NetSim_PACKET* packet = pstruEventDetails->pPacket;

FILE* fp;

char filename[BUFSIZ];

sprintf(filename, "%s\\TemplogFile.csv", pszIOLogPath);

 int reading = 0;

  if (pstruEventDetails->pPacket->nPacketId != 0)

     {

        fp = fopen(filename, "a+");                

        reading = atoi(pstruEventDetails->pPacket->szPayload->packet);

      }

 int thresholdvalue = 50;

 if (pstruEventDetails->nDeviceId == GatewayDeviceId && (pstruEventDetails->pPacket->nPacketId != 0) && reading < thresholdvalue)

        {

              if (fp)

                {

                    fprintf(fp, "\n%d,%s,%u,%lld,", reading, " Dropped ", pstruEventDetails->pPacket->nSourceId,pstruEventDetails->nPacketId);

                    fclose(fp);

                }

                fprintf(stderr, "\nPacket dropped. Payload is %d\n", reading);

                packet->pstruNetworkData->nTTL = 0;

      }

            else if (pstruEventDetails->nDeviceId == GatewayDeviceId && pstruEventDetails->pPacket->nPacketId != 0)

            {

                if (fp)

                {

                    fprintf(fp, "\n%d,%s,%u,%lld,", reading, " Forwarded ", pstruEventDetails->pPacket->nSourceId, pstruEventDetails->nPacketId);

                    fclose(fp);

                }

                fprintf(stderr, "\nPacket forwarded. Payload is %d\n", reading);

            }

 

NETWORK_LAYER_PROTOCOL nLocalNetworkProtcol;

nLocalNetworkProtcol = fnGetLocalNetworkProtocol(pstruEventDetails);

if (nLocalNetworkProtcol)

{

fnCallProtocol(nLocalNetworkProtcol);

return 0;

}

 

After the source code is modified, rebuild the IP module by right-clicking on the IP project and selecting rebuild. Similarly, rebuild the Application project. 


Simulation Results and Analysis:


The steps involved may vary across versions of NetSim. Please refer to the respective section below:


Applicable Releases
v14


Create a simple scenario in the IoT module and configure traffic from sensor nodes to a Wired Node connected externally using a router. 

 

A screenshot of a computer

Description automatically generated


Packets can be captured using Wireshark to analyze the payload. Set the Wireshark to online in the Destination wired node where you can observe the packet payload.


A screenshot of a computer

Description automatically generated


Upon running the simulation Wireshark will pop up automatically as shown below where you can check the packet payload:


A screenshot of a computer

Description automatically generated


NetSim Console will print the payload of those packets that are dropped at the Gateway as shown below:


A screenshot of a computer

Description automatically generated


The TempLogFile.csv can be accessed from NetSim Result Dashboard under Log Section  as shown below:


A screenshot of a computer

Description automatically generated


A screenshot of a data loss

Description automatically generated

 

 


Applicable Releases
v13

 

 

Create a simple scenario in the IoT module and configure traffic from sensor nodes to a Wired Node connected externally using a router. 

 

Packets can be captured using Wireshark to analyze the payload. Set the Wireshark to online in the Destination wired node where you can observe the packet payload.


 

Upon running the simulation Wireshark will pop up automatically as shown below where you can check the packet payload:



NetSim Console will print the payload of those packets that are dropped at the Gateway as shown below:


The TempLogFile.csv can be accessed from NetSim Result Dashboard under Log Section  as shown below: