NetSim Source Code Help v14.4
All 13 Components
 
Loading...
Searching...
No Matches
Component 6/ZigBee/BackoffCalculation.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 <math.h>
15#include "main.h"
16#include "802_15_4.h"
17
18
19int lf_NP_RandomBackOffTime(int);
20/** The backoff procedure, the STA shall set its Backoff Timer to a random backoff time
21using the equation
22Backoff Time = Random() × aSlotTime.
23All backoff slots occur following a DIFS period during which the medium is determined to be
24idle for the duration of the DIFS period.
25*/
26int fn_NetSim_Zigbee_BackoffTimeCalculation(int nBackoffExponent,double* dBackoffTime,int nUnitBacoffPeriod,METRICS** pstruMetrics)
27{
28 int nBackoffTimes;
29 nBackoffTimes = (int)pow(2,nBackoffExponent);
30 //nBackoffTimes = lf_NP_RandomBackOffTime(nBackoffTimes);
31 nBackoffTimes = (int)(((unsigned long long int)fn_NetSim_Utilities_GenerateRandomNo(&ulBackoffSeed1,&ulBackoffSeed2))%nBackoffTimes);
32 *dBackoffTime = nBackoffTimes*dUnitSymbolTime*nUnitBacoffPeriod;
33 pstruMetrics[pstruEventDetails->nDeviceId-1]->pstruIEEE802_15_4_Metrics->dTotalbackofftime += (*dBackoffTime);
34 pstruMetrics[pstruEventDetails->nDeviceId-1]->pstruIEEE802_15_4_Metrics->nNumberofBackoffCall++;
35 return 1;
36}
37/**
38 This Function is used to generate the Random time based on seed values
39*/
40int lf_NP_RandomBackOffTime(int nMaxRandBackoff)
41{
42 long lx = 0;
43 long double ldy = 0;
44 int ltemp;
45
46 ulBackoffSeed1 = (unsigned long) ((40014 * ulBackoffSeed1) % (unsigned long) (2147483563));
47 ulBackoffSeed2 = (unsigned long) ((40692 * ulBackoffSeed2) % (unsigned long) (2147483399));
48
49 lx = (long) ((ulBackoffSeed1 - ulBackoffSeed2) % (long) (2147483562));
50
51 if (lx != 0)
52 {
53 ldy = (long double) ((long double) (lx) / (long double) (2147483563));
54 }
55 else
56 {
57 ldy = (long double) ((long double) (2147483562) / (long double) (2147483563));
58 }
59
60
61 ltemp = (int)(ldy*100000000);
62 ltemp = (ltemp%nMaxRandBackoff);
63 return ltemp;
64}