Applicable versions
NetSim StandardNetsim Pro

 

Applicable Releases
v12
v13

 

Steps to create a new packet and adding a new event and to write it to the packet trace, event trace, and animator in NetSim

 

In this example, we show how users can create their own packet & events in MANET DSR Project. The same methodology can be applied to other networks/protocols.


Step 1: Click on Open simulation, go to workspace options and select open code to open source code using Microsoft visual studio 2017/2019. 


Step 2: Therefore, go to the DSR project and Open DSR.h file and add a subevent called “MY_EVENT” inside enum enum_DSR_Subevent as shown below: 


enum enum_DSR_Subevent
 {
  subevent_RREQ_TIMEOUT=NW_PROTOCOL_DSR*100+1,
  subevent_MAINT_TIMEOUT,
  subevent_PROCESS_RERR,
  //add MY_EVENT
  subevent_MY_EVENT,
 };


Step 3:  To add a new packet in NetSim first user has to initialize their new packet name inside DSR.h file. Let us assume the new packet be “MY_PACKET” and it is a control packet. So the user has to define it inside the following enum as shown below: 


enum enum_DSR_ControlPacket
 {
  ctrlPacket_ROUTE_REQUEST=NW_PROTOCOL_DSR*100+1,
  ctrlPacket_ROUTE_REPLY,
  ctrlPacket_ROUTE_ERROR,
  ctrlPacket_ACK,
  //add MY_PACKET
  MY_PACKET,
 };


Step 4: We assume that MY_PACKET has only a payload and overhead in its pactet. 

Note: Users can add extra fields.

struct stru_MY_PACKET

{

            double dPayload;

            double dOverhead;

};

typedef struct stru_MY_PACKET MY_PACKET_INFO; 


Step 5: Open DSR.c file, go to fn_NetSim_DSR_init and add the following code red in colour.

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

                                            NetSim_EVENTDETAILS *pstruEventDetails_Formal,

                                            char *pszAppPath_Formal,

                                            char *pszWritePath_Formal,

                                            int nVersion_Type,

                                            void **fnPointer)

{

    int i;

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

    {

        if (DEVICE(i)->nDeviceType == SENSOR)

        fn_initialize_MY_PACKET(i);

    }

return fn_NetSim_DSR_Init_F(NETWORK_Formal,pstruEventDetails_Formal,pszAppPath_Formal,pszWritePath_Formal,nVersion_Type,fnPointer);

}


Step 6: Open DSR.c file, go to the case TIMER_EVENT and add the following code to the subevent type: 

case TIMER_EVENT:
  {
   switch(pstruEventDetails->nSubEventType)
   {
    case subevent_RREQ_TIMEOUT:
     DSR_RREQ_TIMEOUT();
    break;
    case subevent_MAINT_TIMEOUT:
     DSR_MAINT_TIMEOUT();
    break;
    // modified
    case subevent_MY_EVENT:
     fn_send_MY_PACKET(pstruEventDetails->nDeviceId, pstruEventDetails);
    break;
   }
  }
  break;
 }
 return 1;


Step 7: Inside DSR.c file, add the following codes


void fn_initialize_MY_PACKET(NETSIM_ID d) 
 pstruEventDetails->nDeviceId = d;
 pstruEventDetails->nInterfaceId = 1;
 pstruEventDetails->dEventTime = pstruEventDetails->dEventTime;
 pstruEventDetails->nEventType = TIMER_EVENT;
 pstruEventDetails->nSubEventType = subevent_MY_EVENT;
 pstruEventDetails->nProtocolId = NW_PROTOCOL_DSR;
 pstruEventDetails->pPacket = NULL;
 fnpAddEvent(pstruEventDetails);
}


void fn_send_Broadcast_Packet(NetSim_EVENTDETAILS* pstruEventDetails)
{
 double dTime;
 NETSIM_ID nDeviceId = pstruEventDetails->nDeviceId;
 NETSIM_ID nInterfaceId = pstruEventDetails->nInterfaceId;
 NetSim_PACKET* pstruPacket = pstruEventDetails->pPacket;
 NetSim_PACKET* pstruAckPkt;
 dTime = pstruEventDetails->dEventTime;

 MY_PACKET_INFO* psBroadCast;


 // Create packet
 pstruAckPkt = fn_NetSim_Packet_CreatePacket(NETWORK_LAYER);
 pstruAckPkt->nPacketType = PacketType_Control;
 pstruAckPkt->nPacketPriority = Priority_Normal;
 pstruAckPkt->nControlDataType = MY_PACKET;

 psBroadCast = fnpAllocateMemory(1, sizeof(MY_PACKET_INFO));

 

 // Update general packet fields
 pstruAckPkt->nSourceId = nDeviceId;
 pstruAckPkt->nTransmitterId = nDeviceId;
 (add_dest_to_packet(pstruAckPkt, 0)); //broadcast

pstruAckPkt->pstruNetworkData->Packet_NetworkProtocol = psBroadCast;

 pstruAckPkt->pstruNetworkData->dArrivalTime = dTime;
 pstruAckPkt->pstruNetworkData->dStartTime = dTime;
 pstruAckPkt->pstruNetworkData->dEndTime = dTime;
 pstruAckPkt->pstruNetworkData->dPacketSize = pstruAckPkt->pstruNetworkData->dOverhead;

 pstruAckPkt->pstruNetworkData->szDestIP = STR_TO_IP("255.255.255.255", 4);

 pstruAckPkt->pstruNetworkData->nTTL = 2;
 pstruAckPkt->pstruNetworkData->nNetworkProtocol = NW_PROTOCOL_DSR;
 pstruAckPkt->nPacketId = 0;
 strcpy(pstruAckPkt->szPacketType, "MY_PACKET");//to see the packet in animation
 

//Fill information to broadcast:

    psBroadCast->dOverhead = 20;

    psBroadCast->dPayload = 20;

 // Add SEND Broadcast subevent
 pstruEventDetails->dEventTime = dTime;
 pstruEventDetails->dPacketSize = pstruAckPkt->pstruNetworkData->dPacketSize;
 pstruEventDetails->nSubEventType = 0;
 pstruEventDetails->nEventType = NETWORK_OUT_EVENT;
 pstruEventDetails->pPacket = pstruAckPkt;
 fnpAddEvent(pstruEventDetails);
 //Free the packet
 fn_NetSim_Packet_FreePacket(pstruPacket);
 pstruPacket = NULL;
 return 0;
}


void fn_send_MY_PACKET(NETSIM_ID d, NetSim_EVENTDETAILS* pstruEventDetails)
 {
 pstruEventDetails->dEventTime = pstruEventDetails->dEventTime + 1 * SECOND;
 pstruEventDetails->nDeviceId = d;
 pstruEventDetails->nInterfaceId = 1;
 pstruEventDetails->nEventType = TIMER_EVENT;
 pstruEventDetails->nSubEventType = subevent_MY_EVENT;
 pstruEventDetails->nProtocolId = NW_PROTOCOL_DSR;
 fnpAddEvent(pstruEventDetails);
  fn_send_Broadcast_Packet(pstruEventDetails);
}


Step 8: As soon as the packet enters the Network Layer at Destination End, the packet processing is NETWORK_IN_EVENT.

add the following codes red in color. Here we are just dropping the packets.


    case ctrlPacket_ACK:

                        DSR_PROCESS_ACK();

                        pstruEventDetails->pPacket=NULL;


                    case MY_PACKET:

                        fn_NetSim_Packet_FreePacket(pstruEventDetails->pPacket);

                        break;


                    }

                }

                break;

            case subevent_PROCESS_RERR:



Related articles:

https://support.tetcos.com/en/support/solutions/articles/14000067054-how-can-i-create-my-own-packet-and-my-own-event-in-netsim-

https://support.tetcos.com/en/support/solutions/articles/14000079638-how-do-i-provide-my-own-label-to-a-packet-that-can-be-seen-in-the-packet-trace-