NetSim Source Code Help
Loading...
Searching...
No Matches
Sumo_interface.c
Go to the documentation of this file.
1/************************************************************************************
2* Copyright (C) 2020 *
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: Kshitij Singh
12* Date: 7 July 2016
13* *
14* ---------------------------------------------------------------------------------*/
15
16#include "main.h"
17#include "Mobility.h"
18#include "Animation.h"
19
21double step_size;
22
23char gui; //By Default, Set GUI to 0
24HANDLE hPipe; //Create a Handle to pipe
25
26
27
28
29double *corr(char* id);
30void pipes_init();
31
32//This function is mainly used for calling python and passing sumo configuration files to it
34{
35 static int nosumo=1; // Declared as static, since we want it to be declared and changed only once
36 if(nosumo) // This will run only at the 1st time
37 {
38 char command_to_python[BUFSIZ]; // to be passed to vanet.exe
39 sprintf(command_to_python,"start vanet.exe -main \"%s\" ", sumoname); // store it in one variable
40 fprintf(stderr,"Executing command for opening Sumo - %s",command_to_python); // Error statement in standard error
41 system(command_to_python); // Call vanet.exe and subsequently python
42 fprintf(stderr,"....done.\n");
43 printf("Init sumo pipe\n");
44 pipes_init(); // Initiate pipes for connection
45 nosumo=0;
46 }
47}
48
49//This functon will call Pipes to get coordinates from python. Refer to mobility_run in mobility.c to understand the work of other variables
51{
53 double dPresentTime = pstruMobilityVar->dLastTime;
54
58
59 if(pstruMobilityVar->dLastTime+pstruMobilityVar->dPauseTime*1000000<pstruEventDetails->dEventTime+1000000) //Everytime Mobility being called
60 {
61 double* coordinates; // Pointer for array of X and Y coordinates
62 coordinates = corr(DEVICE_NAME(pstruEventDetails->nDeviceId)); //Get coordinates from python
63 if (coordinates!=NULL)
64 {
65 NETWORK->ppstruDeviceList[pstruEventDetails->nDeviceId-1]->pstruDeviceMobility->pstruNextPosition->X = coordinates[0]; // Update the coordinates in Network stack
67 free(coordinates); // Free memory of pointer
68 }
69
70 //store the last time
71 pstruMobilityVar->dLastTime = pstruEventDetails->dEventTime+1000000*step_size; // Update Last time since we want to match timings with SUMO
72 }
73 //update the device position
77
81
82 //Add event for next point
86}
87
88
89// This function Initiates a Pipes connection and sends GUI =1 or 0 based on Animation Status
90
92{
93 BOOL fSuccess; //If reading/writing successfil or not
94 DWORD cbToWrite, cbWritten; // For Pipes
95 DWORD dwMode;
96 char message_to_be_sent[2];
97
98 LPCSTR lpszPipename = "\\\\.\\pipe\\netsim_sumo_pipe"; //Pipename
99 gui='0';
100 fSuccess=FALSE;
101 //getch();
102 fprintf(stderr,"Creating Sumo pipe\n");
103
104 while (1)
105 {
106 hPipe = CreateFileA
107 (
108 lpszPipename, // pipe name
109 GENERIC_READ | // read and write access
110 GENERIC_WRITE,
111 0, // no sharing
112 NULL, // default security attributes
113 OPEN_EXISTING, // opens existing pipe
114 0, // default attributes
115 NULL // no template file
116 );
117
118 if (hPipe != INVALID_HANDLE_VALUE)
119 break;
120
121 }
122
123 fprintf(stderr,"Connecting Sumo and NetSim in real time\n");
124 dwMode = PIPE_READMODE_MESSAGE; // The pipe connected; change to message-read mode.
125 fSuccess = SetNamedPipeHandleState
126 (
127 hPipe, // pipe handle
128 &dwMode, // new pipe mode
129 NULL, // don't set maximum bytes
130 NULL // don't set maximum time
131 );
132
133 if ( ! fSuccess)
134 fnSystemError("Error in connection netsim sump pipe.\n");
135 else
136 fprintf(stderr,"Connection done\n");
137
139 gui = '1';
140 else
141 gui = '0';
142
143
144 message_to_be_sent[0]=gui; //Send GUI enable or disable
145 message_to_be_sent[1]=0;
146
147 cbToWrite=(DWORD)strlen(message_to_be_sent);
148
149 fSuccess = WriteFile
150 (
151 hPipe, // pipe handle
152 message_to_be_sent, // message
153 cbToWrite, // message length
154 &cbWritten, // bytes written
155 NULL // not overlapped
156 );
157}
158//////////////////////////////////////////////////////
159
160
161
162double *corr(char* id)
163{
164 BOOL fSuccess; //If reading/writing successfil or not
165 DWORD cbRead, cbToWrite, cbWritten; // For Pipes
166 CHAR chBuf[BUFSIZ]; //For reading messages from pipes
167
168 double xcor1,ycor1; // x and y coordinates to be received from sumo
169 double* coordinates;
170
171 do
172 {
173 // Read Garbage from the pipe.
174
175 fSuccess = ReadFile
176 (
177 hPipe, // pipe handle
178 chBuf, // buffer to receive reply
179 BUFSIZ*sizeof(CHAR), // size of buffer
180 &cbRead, // number of bytes read
181 NULL // not overlapped
182 );
183
184 if ( ! fSuccess && GetLastError() != ERROR_MORE_DATA )
185 break;
186 chBuf[cbRead]=0;
187 } while (!fSuccess); // repeat loop if ERROR_MORE_DATA
188
189
190 //Send Vehicle Name to Python
191
192 cbToWrite=(DWORD)strlen(id)+1;
193 fSuccess = WriteFile
194 (
195 hPipe, // pipe handle
196 id, // message
197 cbToWrite, // message length
198 &cbWritten, // bytes written
199 NULL // not overlapped
200 );
201
202
203 // Read acknowledgment for vehicle ids
204 do
205 {
206
207 fSuccess = ReadFile
208 (
209 hPipe, // pipe handle
210 chBuf, // buffer to receive reply
211 BUFSIZ*sizeof(CHAR), // size of buffer
212 &cbRead, // number of bytes read
213 NULL // not overlapped
214 );
215
216 if ( ! fSuccess && GetLastError() != ERROR_MORE_DATA )
217 break;
218 chBuf[cbRead]=0;
219
220 } while (!fSuccess); // repeat loop if ERROR_MORE_DATA
221
222
223
224 if(chBuf[0]=='c') // If the vehicle is present ('c' for confirmation)
225 {
226 xcor1=0;ycor1=0;
227 do
228 {
229 // Read X coordinate
230 fSuccess = ReadFile
231 (
232 hPipe, // pipe handle
233 chBuf, // buffer to receive reply
234 BUFSIZ*sizeof(CHAR), // size of buffer
235 &cbRead, // number of bytes read
236 NULL // not overlapped
237 );
238
239 if ( ! fSuccess && GetLastError() != ERROR_MORE_DATA )
240 break;
241
242 } while (!fSuccess); // repeat loop if ERROR_MORE_DATA
243
244 xcor1 = atof(chBuf);
245 if(xcor1<0)
246 xcor1=0;
247
248 do
249 {
250 // Read Y coordinate
251
252 fSuccess = ReadFile
253 (
254 hPipe, // pipe handle
255 chBuf, // buffer to receive reply
256 BUFSIZ*sizeof(CHAR), // size of buffer
257 &cbRead, // number of bytes read
258 NULL // not overlapped
259 );
260
261 if ( ! fSuccess && GetLastError() != ERROR_MORE_DATA )
262 break;
263 chBuf[cbRead]=0;
264
265 } while (!fSuccess); // repeat loop if ERROR_MORE_DATA
266
267 ycor1 = atof(chBuf);
268 if(ycor1<0)
269 ycor1=0;
270
271 coordinates = (double*)malloc(2*sizeof* coordinates);
272 coordinates[0]=xcor1;
273 coordinates[1]=ycor1;
274 return (coordinates); //Return X Y Coordinates
275 }
276
277 else
278 {
279 return(NULL); // If vehicle not found, return NULL
280 }
281
282}
@ ANIMFLAG_ONLINE
Definition: Animation.h:58
ANIM_FLAG anim_get_anim_flag()
BOOL
Definition: Linux.h:62
#define DWORD
Definition: Linux.h:79
#define fnSystemError(x,...)
Definition: Linux.h:55
#define FALSE
Definition: Linux.h:76
#define HANDLE
Definition: Linux.h:73
#define GetLastError()
Definition: Linux.h:42
#define WriteFile(file, str, size, a, b)
Definition: Linux.h:58
#define malloc(s)
Definition: Memory.h:30
#define free(p)
Definition: Memory.h:31
void mobility_pass_position_to_animation(NETSIM_ID devId, double time, NetSim_COORDINATES *coor)
Definition: Mobility.c:347
#define DEVICE_NAME(DeviceId)
Definition: Stack.h:774
EXPORTED struct stru_NetSim_Network * NETWORK
Definition: Stack.h:742
EXPORTED struct stru_NetSim_EventDetails * pstruEventDetails
Definition: Stack.h:837
#define DEVICE_POSITION(DeviceId)
Definition: Stack.h:790
void init_sumo()
void pipes_init()
HANDLE hPipe
double * corr(char *id)
char * sumoname
double step_size
void sumo_run()
char gui
#define fnpAddEvent(pstruEvent)
Definition: main.h:191
struct stru_NetSim_Coordinates * pstruDevicePosition
Definition: Stack.h:722
struct stru_NetSim_Mobility * pstruDeviceMobility
Definition: Stack.h:723
NETSIM_ID nDeviceId
Definition: Stack.h:750
struct stru_NetSim_Coordinates * pstruNextPosition
Definition: Mobility.h:68
struct stru_NetSim_Coordinates * pstruCurrentPosition
Definition: Mobility.h:67
double dPauseTime
To store the pause time.
Definition: Mobility.h:73
double dLastTime
Represent the devices last move time.
Definition: Mobility.h:77
struct stru_NetSim_Device ** ppstruDeviceList
Definition: Stack.h:737