NetSim Source Code Help v14.4
All 13 Components
 
Loading...
Searching...
No Matches
GroupMobility.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 "Mobility.h"
16#include "Animation.h"
17#include "List.h"
18extern unsigned int nCallBackCount;
19int nGroupCount = 0;
20typedef struct stru_Group_Mobility
21{
22 NETSIM_ID nGroupId;
23 unsigned int nDeviceCount;
24 NETSIM_ID* nDevIds;
25 _ele* ele;
26}GROUP_MOBILITY;
27#define GROUP_ALLOC() (GROUP_MOBILITY*)list_alloc(sizeof(GROUP_MOBILITY),offsetof(GROUP_MOBILITY,ele))
28#define GROUP_NEXT(var) var=(GROUP_MOBILITY*)LIST_NEXT(var)
29
30GROUP_MOBILITY* group = NULL;
31
32GROUP_MOBILITY* get_group_ptr(NETSIM_ID nGroupId)
33{
34 GROUP_MOBILITY* temp = group;
35 while (temp)
36 {
37 if (temp->nGroupId == nGroupId)
38 return temp;
39 GROUP_NEXT(temp);
40 }
41 return NULL;
42}
43
44GROUP_MOBILITY* group_add_new(NETSIM_ID group_id)
45{
46 GROUP_MOBILITY* temp = GROUP_ALLOC();
47 nGroupCount++;
48 temp->nGroupId = group_id;
49 LIST_ADD_LAST((void**)&group, temp);
50 return temp;
51}
52
53int add_to_group(NETSIM_ID group_id, NETSIM_ID dev_id)
54{
55 GROUP_MOBILITY* temp = get_group_ptr(group_id);
56 if (!temp)
57 temp = group_add_new(group_id);
58 temp->nDeviceCount++;
59 temp->nDevIds = (NETSIM_ID*)realloc(temp->nDevIds, temp->nDeviceCount * sizeof(NETSIM_ID));
60 temp->nDevIds[temp->nDeviceCount - 1] = dev_id;
61 return 0;
62}
63
64int fn_NetSim_Mobility_Group_init()
65{
66 GROUP_MOBILITY* temp = group;
67 while (temp)
68 {
69 pstruEventDetails->dEventTime = 0;
70 pstruEventDetails->dPacketSize = 0;
71 pstruEventDetails->nApplicationId = 0;
72 pstruEventDetails->nDeviceId = temp->nGroupId;
73 pstruEventDetails->nDeviceType = Not_Device;
74 pstruEventDetails->nEventType = TIMER_EVENT;
75 pstruEventDetails->nInterfaceId = 0;
76 pstruEventDetails->nPacketId = 0;
77 pstruEventDetails->nProtocolId = PROTOCOL_MOBILITY;
78 pstruEventDetails->nSubEventType = MOVE_GROUP;
79 pstruEventDetails->pPacket = NULL;
80 fnpAddEvent(pstruEventDetails);
81 GROUP_NEXT(temp);
82 }
83 return 0;
84}
85int fnValidateposition(GROUP_MOBILITY* group, double diff_x, double diff_y, bool* flag)
86{
87 unsigned int i;
88 if (DEVICE_MOBILITY(group->nDevIds[0])->pstruCurrentPosition->corrType != CORRTYPE_CARTESIAN)
89 {
90 *flag = false;
91 return 0;
92 }
93
94 for (i = 0; i < group->nDeviceCount; i++)
95 {
96 double x = DEVICE_MOBILITY(group->nDevIds[i])->pstruCurrentPosition->X;
97 double y = DEVICE_MOBILITY(group->nDevIds[i])->pstruCurrentPosition->Y;
98 x += diff_x;
99 y += diff_y;
100 if (x > dGridMax_x || x < dOrigin_X || y < dOrigin_Y || y > dGridMax_y)
101 return -1;
102 }
103 *flag = false;
104 return 0;
105}
106
107int fn_NetSim_MoveGroup()
108{
109 unsigned int nLoop;
110 bool flag = true;
111 double vel;
112 double x, y, diff_x, diff_y;
113 NETSIM_ID group_id = pstruEventDetails->nDeviceId;
114 GROUP_MOBILITY* group = get_group_ptr(group_id);
115 unsigned int i;
116 MOBILITY_VAR* pstruMobilityVar;
117 for (i = 0; i < group->nDeviceCount; i++)
118 {
119 NETSIM_ID dev = group->nDevIds[i];
120 memcpy(DEVICE_MOBILITY(dev)->pstruCurrentPosition,
121 DEVICE_MOBILITY(dev)->pstruNextPosition,
122 sizeof * DEVICE_MOBILITY(dev)->pstruCurrentPosition);
123 }
124 pstruMobilityVar = (MOBILITY_VAR*)DEVICE_MOBILITY(group->nDevIds[0])->pstruMobVar;
125 vel = pstruMobilityVar->dVelocity;
126 do
127 {
128 NetSim_COORDINATES* curr = DEVICE_MOBILITY(group->nDevIds[0])->pstruCurrentPosition;
129 x = curr->X;
130 y = curr->Y;
131 fn_NMo_RandomPoint(&x, &y, vel, pstruMobilityVar->dCalculationInterval, &pstruMobilityVar->ulSeed1, &pstruMobilityVar->ulSeed2);
132 diff_x = x - DEVICE_MOBILITY(group->nDevIds[0])->pstruCurrentPosition->X;
133 diff_y = y - DEVICE_MOBILITY(group->nDevIds[0])->pstruCurrentPosition->Y;
134 fnValidateposition(group, diff_x, diff_y, &flag);
135 } while (flag);
136
137 for (i = 0; i < group->nDeviceCount; i++)
138 {
139 NETSIM_ID dev = group->nDevIds[i];
140 DEVICE_MOBILITY(dev)->pstruNextPosition->X += diff_x;
141 DEVICE_MOBILITY(dev)->pstruNextPosition->Y += diff_y;
142
143 memcpy(DEVICE_POSITION(dev),
144 DEVICE_MOBILITY(dev)->pstruCurrentPosition,
145 sizeof * DEVICE_MOBILITY(dev)->pstruCurrentPosition);
146
147 add_mobility_animation(dev,
148 pstruEventDetails->dEventTime,
149 DEVICE_POSITION(dev)->X,
150 DEVICE_POSITION(dev)->Y,
151 0);
152 }
153 pstruMobilityVar->dLastTime = pstruEventDetails->dEventTime + pstruMobilityVar->dCalculationInterval;
154 //Add event for next point
155 pstruEventDetails->dEventTime += pstruMobilityVar->dCalculationInterval;
156 fnpAddEvent(pstruEventDetails);
157 pstruEventDetails->dEventTime -= pstruMobilityVar->dCalculationInterval;
158
159 //call all the callback function
160 for (nLoop = 0; nLoop < nCallBackCount; nLoop++)
161 {
162 extern _fnMobilityCallBack* fnMobilityCallBack;
163 for (i = 0; i < group->nDeviceCount; i++)
164 fnMobilityCallBack[nLoop](group->nDevIds[i]);
165 }
166 return 0;
167}
168
double dLastTime
Represent the devices last move time.
Definition Mobility.h:84
unsigned long ulSeed1
Used to generate random point.
Definition Mobility.h:82
unsigned long ulSeed2
Used to generate random point.
Definition Mobility.h:83
double dVelocity
To store the velocity.
Definition Mobility.h:81