NOTE: Procedure may vary for different releases of NetSim. Please follow the steps mentioned under applicable release.


Applicable versions
NetSim StandardNetsim Pro

 

Applicable Releases
v12

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 & event in 802.15.4 Zigbee. The same methodology can be applied to any network/protocol.

 

  1. Click on Open simulation, goto workspace options and select open code to open source code using Microsoft visual studio 2017/2019.
  2. Therefore, go to the ZigBee project and Open 802_15_4.h file and add a subevent called “MY_EVENT” inside enum_IEEE802_15_4_Subevent_Type as shown below:

 

 

3. To add a new packet in NetSim first user has to initialize their new packet name inside 802_15_4.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:

 

4. We assume that MY_PACKET has the same fields as a Zigbee Ack and hence we are adding the following ack frame to 802_15_4.h file:

struct stru_My_Frame

{

            int nBeaconId;

            int nSuperFrameId;

            int nBeaconTime;

            double dPayload;

            double dOverhead;

double dFrameSize;

};

5. Inside 802_15_4.h file, add the following code to typedef the ack frame

typedef struct stru_My_Frame MY_FRAME;

 

6. Open 802_15_4.c file, go to the case TIMER_EVENT and add the following code to the subevent type:-


case CFP_END:

{

--------------------------

}

break;

case MY_EVENT:

{

//my event//

      fprintf(stderr, "My_event");

      pstruEventDetails->dEventTime = pstruEventDetails->dEventTime + 1 * SECOND;

      pstruEventDetails->nDeviceId = nGlobalPANCoordinatorId;

      pstruEventDetails->nInterfaceId = 1;

      pstruEventDetails->nEventType = TIMER_EVENT;

      pstruEventDetails->nSubEventType = MY_EVENT;

      pstruEventDetails->nProtocolId = MAC_PROTOCOL_IEEE802_15_4;

      fnpAddEvent(pstruEventDetails);

      fn_NetSim_WSN_MY_PACKET();

//my event//

}

break;

Here we are adding a new event inside the timer event, and this event will occur every 1 second in the GlobalPANCoordinator. i.e sink node. In this event  fn_NetSim_WSN_MY_PACKET() is called, as explained in step 5.

 

7. Inside 802_15_4.c file, add the following code for sending ack (broadcast):

 

int fn_NetSim_WSN_MY_PACKET()

{

            double dTime;

            NETSIM_ID nDeviceId = pstruEventDetails->nDeviceId;

            NETSIM_ID nInterfaceId = pstruEventDetails->nInterfaceId;

            IEEE802_15_4_MAC_VAR *pstruMacVar = DEVICE_MACVAR(nDeviceId,nInterfaceId);

            IEEE802_15_4_PHY_VAR *pstruPhyVar = DEVICE_PHYVAR(nDeviceId,nInterfaceId);

            NetSim_PACKET *pstruPacket = pstruEventDetails->pPacket;

            NetSim_PACKET *pstruAckPkt;

            MY_FRAME *pstruAck;

            dTime = pstruEventDetails->dEventTime;

            

// Create MY_Frame   

            pstruAckPkt = fn_NetSim_Packet_CreatePacket(MAC_LAYER);

            pstruAckPkt->nPacketType = PacketType_Control;

            pstruAckPkt->nPacketPriority = Priority_High;

            pstruAckPkt->nControlDataType =MY_PACKET;

            pstruAck =  fnpAllocateMemory(1, sizeof(MY_FRAME));

 

            // Update packet fields

            pstruAckPkt->nSourceId = nDeviceId;

            pstruAckPkt->nTransmitterId = nDeviceId;

           (add_dest_to_packet(pstruAckPkt, 0)) ; //broadcast

            pstruAckPkt->pstruMacData->Packet_MACProtocol = pstruAck;

            pstruAckPkt->pstruMacData->dArrivalTime = dTime;

            pstruAckPkt->pstruMacData->dStartTime = dTime;

            pstruAckPkt->pstruMacData->dEndTime = dTime; 

            pstruAckPkt->pstruMacData->dPacketSize = pstruAckPkt->pstruMacData->dOverhead;

            pstruAckPkt->pstruMacData->nMACProtocol = MAC_PROTOCOL_IEEE802_15_4;

            pstruAckPkt->nPacketId = 0;

            strcpy(pstruAckPkt->szPacketType, "MY_PACKET");//to see the packet in animation

 

            // Add SEND ACK subevent

            pstruEventDetails->dEventTime = dTime;

            pstruEventDetails->dPacketSize = pstruAckPkt->pstruMacData->dPacketSize;

            pstruEventDetails->nSubEventType = 0;

            pstruEventDetails->nEventType = PHYSICAL_OUT_EVENT;

            pstruEventDetails->pPacket = pstruAckPkt;

            fnpAddEvent(pstruEventDetails);

            

            //Free the packet

            fn_NetSim_Packet_FreePacket(pstruPacket);

            pstruPacket = NULL;  

            return 0;

}

 

Inside the above function NetSim API fn_NetSim_Packet_CreatePacket(MAC_LAYER); is used. This is the API that creates a new packet in NetSim. Since in this example, a new packet is created in the MAC layer, it is passed as an argument. Users can give the respective Layer name for creating packets in any other layers.

In the above code users can see the following line: 

strcpy(pstruAckPkt->szPacketType, "MY_PACKET"); 

This is used visualize the packet transmission in the packet animation.

 

8. In 802_15_4.c file, goto fn_NetSim_Zigbee_Init() function and add the following code in red color to call the timer_event. i.e MY_EVENT

declspec (dllexport) int fn_NetSim_Zigbee_Init(struct stru_NetSim_Network *NETWORK_Formal,\NetSim_EVENTDETAILS *pstruEventDetails_Formal,char *pszAppPath_Formal,\char *pszWritePath_Formal,int nVersion_Type,void **fnPointer)

{

            pstruEventDetails=pstruEventDetails_Formal;

            NETWORK=NETWORK_Formal;

            pszAppPath =pszAppPath_Formal;

            pszIOPath = pszWritePath_Formal;

            //MY_EVENT

            pstruEventDetails->nDeviceId = nGlobalPANCoordinatorId;

            pstruEventDetails->nInterfaceId = 1;

            pstruEventDetails->dEventTime = pstruEventDetails->dEventTime;

            pstruEventDetails->nEventType = TIMER_EVENT;

            pstruEventDetails->nSubEventType = MY_EVENT;

            pstruEventDetails->nProtocolId = MAC_PROTOCOL_IEEE802_15_4;

            pstruEventDetails->pPacket = NULL;

            fnpAddEvent(pstruEventDetails);

            //MY_EVENT

 

fn_NetSim_Zigbee_Init_F(NETWORK_Formal,pstruEventDetails_Formal,pszAppPath_Formal,\pszWritePath_Formal,nVersion_Type,fnPointer);

            return 0;

}

9.  In the above function, subevent type, “MY_EVENT” is called. So this function calls the MY_EVENT, timer event to execute.

fn_NetSim_Zigbee_Trace() is an API to print the trace details to the event trace. So inside 802_15_4.c file add the following lines of code in red color inside fn_NetSim_Zigbee_Trace(int nSubEvent) as shown below:-

 

_declspec (dllexport) char *fn_NetSim_Zigbee_Trace(int nSubEvent)

{

            if (nSubEvent == MY_EVENT)

            return "MY_EVENT";

            return (fn_NetSim_Zigbee_Trace_F(nSubEvent));

}


10. Save the code and Build Zigbee project. libZigbee.dll will automatically get  in the bin folder of the current workspace.


11. Create a basic scenario in WSN with 2 sensors and 1 sink node.


12. Run simulation for 10 seconds.


13. Play packet animation. Here users can see that Sink node broadcasts “MY_PACKET” to the sensor.


 

 

14. Also open packet trace and users can filter the control packet and see all the packet details of “MY_PACKET” written in the packet trace.

 

 

15. To analyze the “MY_EVENT” users can open event trace and filter the subevent type as “MY_EVENT”. Here users can analyze that the event occurs every 1 second.

 



Applicable versions
NetSim StandardNetsim Pro

 

Applicable Releases
v10.2


Please refer to the attached document www.tetcos.com/File_Exchange/Help/NetSim-v10-help-creating-packet-and-writing-details-to-trace-and-animator.docx