NetSim Source Code Help
Loading...
Searching...
No Matches
FileBasedMobility.c
Go to the documentation of this file.
1/************************************************************************************
2* Copyright (C) 2021 *
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
29{
31 double time;
32 double x;
33 double y;
34 double z;
39
41 double time,
42 double x,
43 double y,
44 double z)
45{
46 if (!mobilityInfo)
47 {
50 }
51 ptrINFO info = mobilityInfo[d - 1];
52
53 ptrINFO t = calloc(1, sizeof * t);
54 t->d = d;
55 t->time = time;
56 t->x = x;
57 t->y = y;
58 t->z = z;
59 if (info)
60 {
61 lastInfo[d - 1]->next = t;
62 lastInfo[d - 1] = t;
63 }
64 else
65 {
66 mobilityInfo[d - 1] = t;
67 lastInfo[d - 1] = t;
68 }
69 fprintf(stderr, "%d,%lf,%lf,%lf%lf\n",
70 d, time, x, y, z);
71}
72
73static void add_mobility_event()
74{
76 if (!mobilityInfo)
77 return;
78 for (d = 0; d < NETWORK->nDeviceCount; d++)
79 {
80 if (!mobilityInfo[d])
81 continue;
83 mob->pstruCurrentPosition = calloc(1, sizeof * mob->pstruCurrentPosition);
84 mob->pstruNextPosition = calloc(1, sizeof * mob->pstruNextPosition);
98 }
99}
100
102{
103 if (!mobilityInfo)
104 return;
106 ptrINFO i = mobilityInfo[d - 1];
107 if (!i)
108 return;
112 NetSim_COORDINATES* dc = DEVICE(d)->pstruDevicePosition;
113 dc->X = i->x;
114 dc->Y = i->y;
115 dc->Z = i->z;
116 c->X = i->x;
117 c->Y = i->y;
118 c->Z = i->z;
119 if (i->next)
120 {
121 n->X = i->next->x;
122 n->Y = i->next->y;
123 n->Z = i->next->z;
124 }
125 mobilityInfo[d - 1] = i->next;
126 free(i);
130 if (mobilityInfo[d - 1])
131 {
134 }
135}
136
137/** This function is to free the file pointers */
139{
140 NETSIM_ID d;
141 if (mobilityInfo)
142 {
143 for (d = 0; d < NETWORK->nDeviceCount; d++)
144 {
146 while (i)
147 {
148 mobilityInfo[d] = i->next;
149 free(i);
150 i = mobilityInfo[d];
151 }
152 }
154 }
155 return 0;
156}
157
159 NETSIM_ID i;
160 for (i = 0; i < NETWORK->nDeviceCount; i++) {
161 NETSIM_ID r = DEVICE_CONFIGID(i + 1);
162 if (DEVICE_CONFIGID(i + 1) == d)
163 return i;
164 }
165 return 0;
166}
167
168/** This function is to open the file, to define position pointers and to set the initial positions for all the nodes */
170{
171 int lineno = 0;
172 double time, x, y, z;
173 NETSIM_ID d;
174 FILE* fp;
175 char str[BUFSIZ];
176 if (!mobilityInfo)
177 {
178 sprintf(str, "%s/%s", pszIOPath, "mobility.txt");
179 fp = fopen(str, "r");
180 if (!fp)
181 {
182 fnSystemError("Unable to open %s file.\n", str);
183 perror(str);
184 return -1;
185 }
186 }
187 else
188 {
189 return -1;
190 }
191
192 char data[BUFSIZ];
193 while (fgets(data, BUFSIZ, fp))
194 {
195 lineno++;
196 lskip(data);
197 if (*data == '#' ||
198 *data == 0)
199 continue;
200 if (*data != '$')
201 {
202 fprintf(stderr, "In mobility.txt, invalid data at line no %d\n", lineno);
203 continue;
204 }
205 if (*(data + 1) == 'n')
206 continue;
207 if (*(data + 1) != 't')
208 {
209 fprintf(stderr, "In mobility.txt, invalid data at line no %d\n", lineno);
210 continue;
211 }
212
213 if (sscanf(data, "$time %lf \"$node_(%d) %lf %lf %lf\"",
214 &time, &d, &x, &y, &z));
216 add_mobility_info(d + 1, time * SECOND, x, y, z);
217 }
218 fclose(fp);
219
221 return 0;
222}
unsigned int NETSIM_ID
Definition: Animation.h:45
int FileBasedMobilityPointersFree()
NETSIM_ID find_DeviceID_From_ConfigID(NETSIM_ID d)
struct stru_filebasedmobilityinfo INFO
ptrINFO * lastInfo
static void add_mobility_event()
ptrINFO * mobilityInfo
void process_filebased_mobility_event()
struct stru_filebasedmobilityinfo * ptrINFO
static void add_mobility_info(NETSIM_ID d, double time, double x, double y, double z)
int FileBasedMobilityReadingFile()
#define c
#define fnSystemError(x,...)
Definition: Linux.h:55
#define free(p)
Definition: Memory.h:31
#define calloc(c, s)
Definition: Memory.h:29
void mobility_pass_position_to_animation(NETSIM_ID devId, double time, NetSim_COORDINATES *coor)
Definition: Mobility.c:347
char * lskip(const char *s)
#define DEVICE(DeviceId)
Definition: Stack.h:769
EXPORTED char * pszIOPath
Definition: Stack.h:842
#define DEVICE_CONFIGID(DeviceId)
Definition: Stack.h:779
EXPORTED struct stru_NetSim_Network * NETWORK
Definition: Stack.h:742
#define SECOND
Definition: Stack.h:60
#define DEVICE_MOBILITY(DeviceId)
Definition: Stack.h:791
@ TIMER_EVENT
Definition: Stack.h:114
EXPORTED struct stru_NetSim_EventDetails * pstruEventDetails
Definition: Stack.h:837
#define PROTOCOL_MOBILITY
Definition: Stack.h:142
#define DEVICE_POSITION(DeviceId)
Definition: Stack.h:790
#define fnpAddEvent(pstruEvent)
Definition: main.h:191
netsimDEVICE_TYPE nDeviceType
Definition: Stack.h:711
NETSIM_ID nApplicationId
Definition: Stack.h:752
EVENT_TYPE nEventType
Definition: Stack.h:747
NETSIM_ID nProtocolId
Definition: Stack.h:748
struct stru_NetSim_Packet * pPacket
Definition: Stack.h:754
NETSIM_ID nSubEventType
Definition: Stack.h:757
NETSIM_ID nDeviceId
Definition: Stack.h:750
long long int nPacketId
Definition: Stack.h:755
netsimDEVICE_TYPE nDeviceType
Definition: Stack.h:749
NETSIM_ID nInterfaceId
Definition: Stack.h:751
struct stru_NetSim_Coordinates * pstruNextPosition
Definition: Mobility.h:68
struct stru_NetSim_Coordinates * pstruCurrentPosition
Definition: Mobility.h:67
struct stru_NetSim_Device ** ppstruDeviceList
Definition: Stack.h:737
struct stru_filebasedmobilityinfo * next