NetSim Source Code Help v14.4
All 13 Components
 
Loading...
Searching...
No Matches
Component 6/ZigBee/Superframe.c
1/************************************************************************************
2 * Copyright (C) 2023 *
3 * TETCOS, Bangalore. India *
4 * *
5 * Tetcos owns the intellectual property rights in the Product and its content. *
6 * The copying, redistribution, reselling or publication of any or all of the *
7 * Product or its content without express prior written consent of Tetcos is *
8 * prohibited. Ownership and / or any other right relating to the software and all *
9 * intellectual property rights therein shall remain at all times with Tetcos. *
10 * *
11 * Author: Shashi Kant Suman *
12 * *
13 * ---------------------------------------------------------------------------------*/
14# include "main.h"
15# include "802_15_4.h"
16/**
17The format of the superframe is defined by the coordinator. The superframe is bounded by network beacons
18sent by the coordinator and is divided into 16 equally sized slots. Optionally, the superframe can have an
19active and an inactive portion.
20During the inactive portion, the coordinator may enter a low-power mode. The beacon frame is transmitted in
21the first slot of each superframe. If a coordinator does not wish to use a superframe structure, it will
22turn off the beacon transmissions.The beacons are used to synchronize the attached devices, to identify
23the PAN, and to describe the structure of the superframes.
24*/
25int fn_NetSim_Zigbee_SuperFrameInitialization(SUPERFRAME** ppstruSuperFrame,NETSIM_ID nGlobalPANCoordinatorId,METRICS** pstruMetrics)
26{
27 SUPERFRAME* pstruSuperFrame=*ppstruSuperFrame;
28 static int nBeaconId;
29 NETSIM_ID nDeviceId;
30 double dTime;
31 NetSim_PACKET *pstruPacket;
32 int nCFPStartingSlot = 16;
33 NETSIM_ID nLink_Id, nConnectionID, nConnectionPortID,nLoop;
34
35 nLink_Id = fn_NetSim_Stack_GetConnectedDevice(pstruEventDetails->nDeviceId,pstruEventDetails->nInterfaceId,&nConnectionID,&nConnectionPortID);
36
37 for(nLoop=1; nLoop<=NETWORK->ppstruNetSimLinks[nLink_Id-1]->puniDevList.pstruMP2MP.nConnectedDeviceCount; nLoop++)
38 {
39 nDeviceId = NETWORK->ppstruNetSimLinks[nLink_Id-1]->puniDevList.pstruMP2MP.anDevIds[nLoop-1];
40 if(WSN_MAC(nDeviceId)->nNodeStatus != OFF)
41 WSN_MAC(nDeviceId)->nNodeStatus = IDLE;
42 if(WSN_PHY(nDeviceId)->nRadioState != RX_OFF)
43 ZIGBEE_CHANGERADIOSTATE(nDeviceId, WSN_PHY(nDeviceId)->nRadioState, RX_ON_IDLE);
44 }
45
46 if(!pstruSuperFrame)
47 {
48 *ppstruSuperFrame = (SUPERFRAME*)fnpAllocateMemory(1,sizeof(SUPERFRAME));
49 pstruSuperFrame=*ppstruSuperFrame;
50 pstruSuperFrame->nSuperFrameId = 1;
51 pstruSuperFrame->dSuperFrameLength = WSN_MAC(nGlobalPANCoordinatorId)->dBaseSuperFrameDuration*pow(2,WSN_MAC(nGlobalPANCoordinatorId)->nMacBeaconOrder)*1000;
52 pstruSuperFrame->dActivePeriodLength = WSN_MAC(nGlobalPANCoordinatorId)->dBaseSuperFrameDuration* pow(2,WSN_MAC(nGlobalPANCoordinatorId)->nMacSuperframeOrder) * 1000;
53 pstruSuperFrame->dInactivePeriodLength = pstruSuperFrame->dSuperFrameLength - pstruSuperFrame->dActivePeriodLength;
54 pstruSuperFrame->dSuperFrameStartTime = 0;
55 pstruSuperFrame->dTimeSlotLength = pstruSuperFrame->dActivePeriodLength/16.0;
56 pstruSuperFrame->nTimeSlotCount = 16;
57 pstruSuperFrame->dBeaconLength = pstruSuperFrame->dTimeSlotLength;
58 pstruSuperFrame->dCAPLength = pstruSuperFrame->dActivePeriodLength - pstruSuperFrame->dBeaconLength;
59 pstruSuperFrame->dCFPLength = 0;
60 }
61 else
62 {
63 pstruSuperFrame->pstruNextSuperFrame = (SUPERFRAME*)fnpAllocateMemory(1,sizeof(SUPERFRAME));
64 pstruSuperFrame->pstruNextSuperFrame->nSuperFrameId = pstruSuperFrame->nSuperFrameId+1;
65 pstruSuperFrame->pstruNextSuperFrame->dSuperFrameLength = pstruSuperFrame->dSuperFrameLength;
66 pstruSuperFrame->pstruNextSuperFrame->dActivePeriodLength = pstruSuperFrame->dActivePeriodLength;
67 pstruSuperFrame->pstruNextSuperFrame->dInactivePeriodLength = pstruSuperFrame->dInactivePeriodLength;
68 pstruSuperFrame->pstruNextSuperFrame->dSuperFrameStartTime = pstruSuperFrame->dSuperFrameStartTime + pstruSuperFrame->dSuperFrameLength;
69 pstruSuperFrame->pstruNextSuperFrame->nTimeSlotCount = 16;
70 pstruSuperFrame->pstruNextSuperFrame->dTimeSlotLength = pstruSuperFrame->dTimeSlotLength;
71 pstruSuperFrame->pstruNextSuperFrame->dBeaconLength = pstruSuperFrame->dBeaconLength;
72 pstruSuperFrame->pstruNextSuperFrame->dCAPLength = pstruSuperFrame->dActivePeriodLength - pstruSuperFrame->dBeaconLength;
73 pstruSuperFrame->pstruNextSuperFrame->dCFPLength = 0.0;
74
75 //Update the CAP length and CFP length
76 pstruSuperFrame->pstruNextSuperFrame->dCAPLength = pstruSuperFrame->pstruNextSuperFrame->dCAPLength - (16-(double)nCFPStartingSlot)*pstruSuperFrame->pstruNextSuperFrame->dTimeSlotLength;
77 pstruSuperFrame->pstruNextSuperFrame->dCFPLength = (16-(double)nCFPStartingSlot)*pstruSuperFrame->pstruNextSuperFrame->dTimeSlotLength;
78 pstruSuperFrame = pstruSuperFrame->pstruNextSuperFrame;
79 *ppstruSuperFrame = pstruSuperFrame;
80 }
81 pstruMetrics[pstruEventDetails->nDeviceId-1]->pstruIEEE802_15_4_Metrics->nBeaconTransmitted++;
82 pstruMetrics[pstruEventDetails->nDeviceId-1]->pstruIEEE802_15_4_Metrics->dBeaconTime += pstruSuperFrame->dBeaconLength;
83 pstruMetrics[pstruEventDetails->nDeviceId-1]->pstruIEEE802_15_4_Metrics->dCAPTime += pstruSuperFrame->dCAPLength;
84 pstruMetrics[pstruEventDetails->nDeviceId-1]->pstruIEEE802_15_4_Metrics->dCFPTime += pstruSuperFrame->dCFPLength;
85 //Set the superframe status = Beacon transmission
86 pstruSuperFrame->nSuperFrameStatus = BEACONTRANSMISSIONMODE;
87
88 /*** Preparing details for beacon ***/
89 pstruSuperFrame->pstruBeacon = (BEACONFRAME*)fnpAllocateMemory(1,sizeof(BEACONFRAME));
90 /*{
91 _declspec(dllimport) int memcheckcount;
92 _declspec(dllimport) void* memcheck[50];
93 memcheck[memcheckcount++]=pstruSuperFrame->pstruBeacon;
94 }*/
95 pstruSuperFrame->pstruBeacon->nBeaconId = ++nBeaconId;
96 pstruSuperFrame->pstruBeacon->nSuperFrameId = pstruSuperFrame->nSuperFrameId;
97 pstruSuperFrame->pstruBeacon->nBeaconTime = (int)(pstruSuperFrame->dSuperFrameStartTime);
98 pstruSuperFrame->pstruBeacon->dPayload = 5;
99 pstruSuperFrame->pstruBeacon->dOverhead = 29;
100 pstruSuperFrame->pstruBeacon->dFrameSize = pstruSuperFrame->pstruBeacon->dOverhead + pstruSuperFrame->pstruBeacon->dPayload;
101
102 pstruPacket = fn_NetSim_Packet_CreatePacket(2);
103 pstruPacket->nPacketType = PacketType_Control;
104 pstruPacket->nControlDataType = BEACON_FRAME;
105 pstruPacket->nSourceId = nGlobalPANCoordinatorId;
106 add_dest_to_packet(pstruPacket, 0);
107 pstruPacket->dEventTime = pstruSuperFrame->dSuperFrameStartTime;
108 pstruPacket->nTransmitterId = nGlobalPANCoordinatorId;
109 pstruPacket->nReceiverId = 0;
110 pstruPacket->pstruMacData->dArrivalTime = pstruSuperFrame->dSuperFrameStartTime;
111 pstruPacket->pstruMacData->dStartTime = pstruSuperFrame->dSuperFrameStartTime;
112 pstruPacket->pstruMacData->dEndTime = pstruSuperFrame->dSuperFrameStartTime;
113 pstruPacket->pstruMacData->dPayload = pstruSuperFrame->pstruBeacon->dPayload;
114 pstruPacket->pstruMacData->dOverhead = pstruSuperFrame->pstruBeacon->dPayload;
115 pstruPacket->pstruMacData->dPacketSize = pstruPacket->pstruMacData->dPayload + pstruPacket->pstruMacData->dOverhead;
116 pstruPacket->pstruMacData->szSourceMac = fn_NetSim_Stack_GetMacAddressFromIP(fn_NetSim_Stack_GetFirstIPAddressAsId(pstruEventDetails->nDeviceId,0));
117 pstruPacket->pstruMacData->szDestMac = BROADCAST_MAC;
118 pstruPacket->pstruMacData->nMACProtocol = MAC_PROTOCOL_IEEE802_15_4;
119 pstruPacket->pstruMacData->Packet_MACProtocol = fnpAllocateMemory(1,sizeof(IEEE802_15_4_HEADER));
120 WSN_MAC_HEADER(pstruPacket)->nAckRequestFlag = 0;
121 WSN_MAC_HEADER(pstruPacket)->pstruBeaconFrame = pstruSuperFrame->pstruBeacon;
122 pstruEventDetails->dPacketSize = pstruPacket->pstruMacData->dPacketSize;
123 pstruEventDetails->nApplicationId = 0;
124 pstruEventDetails->nDeviceType = PANCOORDINATOR;
125 pstruEventDetails->nPacketId = nBeaconId;
126 pstruEventDetails->nInterfaceId = 1;
127 pstruEventDetails->nEventType = PHYSICAL_OUT_EVENT;
128 pstruEventDetails->nSubEventType = 0;
129 pstruEventDetails->nProtocolId = MAC_PROTOCOL_IEEE802_15_4;
130 pstruEventDetails->pPacket = pstruPacket;
131 fnpAddEvent(pstruEventDetails);
132
133 dTime = pstruEventDetails->dEventTime;
134 //To trigger the Next superframe event
135 pstruEventDetails->dPacketSize = pstruPacket->pstruMacData->dPacketSize;
136 pstruEventDetails->nApplicationId = 0;
137 pstruEventDetails->nDeviceType = PANCOORDINATOR;
138 pstruEventDetails->nPacketId = nBeaconId;
139 pstruEventDetails->dEventTime = pstruSuperFrame->dSuperFrameStartTime + pstruSuperFrame->dSuperFrameLength + 1;
140 pstruEventDetails->nEventType = TIMER_EVENT;
141 pstruEventDetails->nDeviceId = nGlobalPANCoordinatorId;
142 pstruEventDetails->nProtocolId = MAC_PROTOCOL_IEEE802_15_4;
143 pstruEventDetails->nSubEventType = SUPERFRAME_EVENT;
144 pstruEventDetails->pPacket = NULL;
145 fnpAddEvent(pstruEventDetails);
146
147 //pstruEventDetails->dPacketSize = pstruPacket->pstruMacData->dPacketSize;
148 pstruEventDetails->nApplicationId = 0;
149 pstruEventDetails->nDeviceType = PANCOORDINATOR;
150 pstruEventDetails->nPacketId = nBeaconId;
151 pstruEventDetails->nDeviceId = nGlobalPANCoordinatorId;
152 pstruEventDetails->dEventTime = dTime + pstruSuperFrame->dTimeSlotLength;
153 pstruEventDetails->nEventType = TIMER_EVENT;
154 pstruEventDetails->nSubEventType = BEACON_TRANSMISSION_END;
155 pstruEventDetails->nProtocolId = MAC_PROTOCOL_IEEE802_15_4;
156 pstruEventDetails->pPacket = NULL;
157 fnpAddEvent(pstruEventDetails);
158 return 0;
159}
160