NetSim Source Code Help v14.4
All 13 Components
 
Loading...
Searching...
No Matches
FileBasedMobility.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* This source code is licensed per the NetSim license agreement. *
12* *
13* No portion of this source code may be used as the basis for a derivative work, *
14* or used, for any purpose other than its intended use per the NetSim license *
15* agreement. *
16* *
17* This source code and the algorithms contained within it are confidential trade *
18* secrets of TETCOS and may not be used as the basis for any other software, *
19* hardware, product or service. *
20* *
21* Author: Shashi Kant Suman *
22* *
23* ----------------------------------------------------------------------------------*/
24#include "main.h"
25#include "Mobility.h"
26#include "NetSim_utility.h"
27
28char* mobilityfile;
30{
31 NETSIM_ID d;
32 double time;
33 double x;
34 double y;
35 double z;
36 struct stru_filebasedmobilityinfo* next;
37}INFO, * ptrINFO;
38ptrINFO* mobilityInfo;
39ptrINFO* lastInfo;
40
41static void add_mobility_info(NETSIM_ID d,
42 double time,
43 double x,
44 double y,
45 double z)
46{
47 if (!mobilityInfo)
48 {
49 mobilityInfo = calloc(NETWORK->nDeviceCount, sizeof * mobilityInfo);
50 lastInfo = calloc(NETWORK->nDeviceCount, sizeof * lastInfo);
51 }
52 ptrINFO info = mobilityInfo[d - 1];
53
54 ptrINFO t = calloc(1, sizeof * t);
55 t->d = d;
56 t->time = time;
57 t->x = x;
58 t->y = y;
59 t->z = z;
60 if (info)
61 {
62 lastInfo[d - 1]->next = t;
63 lastInfo[d - 1] = t;
64 }
65 else
66 {
67 mobilityInfo[d - 1] = t;
68 lastInfo[d - 1] = t;
69 }
70 fprintf(stderr, "%lf,%d,%lf,%lf,%lf\n",
71 time / SECOND, d, x, y, z);
72}
73
74static void add_mobility_event()
75{
76 NETSIM_ID d;
77 if (!mobilityInfo)
78 return;
79 for (d = 0; d < NETWORK->nDeviceCount; d++)
80 {
81 if (!mobilityInfo[d])
82 continue;
83 NetSim_MOBILITY* mob = DEVICE_MOBILITY(d + 1);
84 mob->pstruCurrentPosition = calloc(1, sizeof * mob->pstruCurrentPosition);
85 mob->pstruNextPosition = calloc(1, sizeof * mob->pstruNextPosition);
86 ptrINFO i = mobilityInfo[d];
87 pstruEventDetails->dEventTime = i->time;
88 pstruEventDetails->dPacketSize = 0;
89 pstruEventDetails->nApplicationId = 0;
90 pstruEventDetails->nDeviceId = d + 1;
91 pstruEventDetails->nDeviceType = NETWORK->ppstruDeviceList[d]->nDeviceType;
92 pstruEventDetails->nEventType = TIMER_EVENT;
93 pstruEventDetails->nInterfaceId = 0;
94 pstruEventDetails->nPacketId = 0;
95 pstruEventDetails->nProtocolId = PROTOCOL_MOBILITY;
96 pstruEventDetails->nSubEventType = 0;
97 pstruEventDetails->pPacket = NULL;
98 fnpAddEvent(pstruEventDetails);
99 }
100}
101
102void process_filebased_mobility_event()
103{
104 if (!mobilityInfo)
105 return;
106 NETSIM_ID d = pstruEventDetails->nDeviceId;
107 ptrINFO i = mobilityInfo[d - 1];
108 if (!i)
109 return;
110 NetSim_MOBILITY* mob = DEVICE_MOBILITY(d);
111 NetSim_COORDINATES* c = mob->pstruCurrentPosition;
112 NetSim_COORDINATES* n = mob->pstruNextPosition;
113 NetSim_COORDINATES* dc = DEVICE(d)->pstruDevicePosition;
114 dc->X = i->x;
115 dc->Y = i->y;
116 dc->Z = i->z;
117 c->X = i->x;
118 c->Y = i->y;
119 c->Z = i->z;
120 if (i->next)
121 {
122 n->X = i->next->x;
123 n->Y = i->next->y;
124 n->Z = i->next->z;
125 }
126 mobilityInfo[d - 1] = i->next;
127 free(i);
128 mobility_pass_position_to_animation(pstruEventDetails->nDeviceId,
129 pstruEventDetails->dEventTime,
130 DEVICE_POSITION(pstruEventDetails->nDeviceId));
131 if (mobilityInfo[d - 1])
132 {
133 pstruEventDetails->dEventTime = mobilityInfo[d - 1]->time;
134 fnpAddEvent(pstruEventDetails);
135 }
136}
137
138/** This function is to free the file pointers */
139int FileBasedMobilityPointersFree()
140{
141 NETSIM_ID d;
142 if (mobilityInfo)
143 {
144 for (d = 0; d < NETWORK->nDeviceCount; d++)
145 {
146 ptrINFO i = mobilityInfo[d];
147 while (i)
148 {
149 mobilityInfo[d] = i->next;
150 free(i);
151 i = mobilityInfo[d];
152 }
153 }
154 free(mobilityInfo);
155 }
156 return 0;
157}
158
159/** This function is to open the file, to define position pointers and to set the initial positions for all the nodes */
160int FileBasedMobilityReadingFile()
161{
162 int lineno = 0;
163 double time, x, y, z;
164 NETSIM_ID d;
165 FILE* fp;
166 char str[BUFSIZ];
167 bool istext = false;
168
169 if (!mobilityInfo)
170 {
171 fprintf(stderr, "Reading File Based Mobility Input:\n");
172 sprintf(str, "%s/%s", pszIOPath, mobilityfile);
173 fp = fopen(str, "r");
174 if (!fp)
175 istext = true;
176
177 if (istext)
178 {
179 sprintf(str, "%s/%s", pszIOPath, "mobility.txt");
180 fp = fopen(str, "r");
181 if (!fp)
182 {
183 fnSystemError("Unable to open mobility.csv/mobility.txt file.\n", str);
184 perror(str);
185 return -1;
186 }
187 }
188 }
189 else
190 {
191 return -1;
192 }
193
194 char data[BUFSIZ];
195 bool fileEmpty = true;
196 bool xInKm = false;
197 while (fgets(data, BUFSIZ, fp))
198 {
199 lineno++;
200 lskip(data);
201
202 // Check header for unit in X column (case-insensitive match)
203 if (*data == '#')
204 {
205 char* header = data + 1; // Skip the '#' character
206 const char* delimiters = ", \t\n";
207 char* token = strtok(header, delimiters);
208 while (token)
209 {
210 if (!_stricmp(token, "X(KM)")) // Case-insensitive match
211 {
212 xInKm = true;
213 break;
214 }
215 token = strtok(NULL, delimiters);
216 }
217 continue; // Skip processing of header line
218 }
219
220 if (*data == 0)
221 continue;
222 fileEmpty = false;
223 if (istext)
224 {
225 if (*data != '$')
226 {
227 fprintf(stderr, "In mobility.txt, invalid data at line no %d\n", lineno);
228 continue;
229 }
230 if (*(data + 1) == 'n')
231 continue;
232 if (*(data + 1) != 't')
233 {
234 fprintf(stderr, "In mobility.txt, invalid data at line no %d\n", lineno);
235 continue;
236 }
237
238 if (sscanf(data, "$time %lf \"$node_(%d) %lf %lf %lf\"",
239 &time, &d, &x, &y, &z) == 5)
240 {
241 d = fn_NetSim_GetDeviceIdByConfigId(d + 1);
242 if (d > 0 && DEVICE_MOBILITY(d)->nMobilityType == MobilityModel_FILEBASEDMOBILITY)
243 add_mobility_info(d, time * SECOND, x, y, z);
244 else
245 fprintf(stderr, "In mobility.txt, either the device id is invalid or file based mobility not enabled, at line no %d\n", lineno);
246
247 }
248 else
249 {
250 fprintf(stderr, "In mobility.txt, invalid data at line no %d\n", lineno);
251 continue;
252 }
253 }
254 else
255 {
256 if (sscanf(data, "%lf,%d,%lf,%lf,%lf\"",
257 &time, &d, &x, &y, &z) == 5)
258 {
259 if (xInKm)
260 {
261 x *= 1000; // Convert from KM to meters
262 y *= 1000; // Convert from KM to meters
263 z *= 1000; // Convert from KM to meters
264 }
265 d = fn_NetSim_GetDeviceIdByConfigId(d);
266 if (d > 0 && DEVICE_MOBILITY(d)->nMobilityType == MobilityModel_FILEBASEDMOBILITY)
267 add_mobility_info(d, time * SECOND, x, y, z);
268 else
269 fprintf(stderr, "In mobility.csv, either the device id is invalid or file based mobility not enabled, at line no %d\n", lineno);
270
271 }
272 else
273 {
274 fprintf(stderr, "In mobility.csv, invalid data at line no %d\n", lineno);
275 continue;
276 }
277 }
278 }
279 fclose(fp);
280
281 if (fileEmpty)
282 fnSystemError("Empty mobility input file detected\n");
283
284 add_mobility_event();
285 return 0;
286}