NetSim Source Code Help v14.4
All 13 Components
 
Loading...
Searching...
No Matches
Sumo_interface.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: Kshitij Singh
12* Date: 7 July 2016
13* *
14* ---------------------------------------------------------------------------------*/
15
16#include "main.h"
17#include "Mobility.h"
18#include "Animation.h"
19
20char* sumoname;
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
33void init_sumo()
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\\ConfigSupport\\%s\" ",pszIOPath,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
50void sumo_run()
51{
52 MOBILITY_VAR* pstruMobilityVar=(MOBILITY_VAR*)NETWORK->ppstruDeviceList[pstruEventDetails->nDeviceId-1]->pstruDeviceMobility->pstruMobVar; //Define Mobility variable
53 double dPresentTime = pstruMobilityVar->dLastTime;
54
55 memcpy(NETWORK->ppstruDeviceList[pstruEventDetails->nDeviceId-1]->pstruDeviceMobility->pstruCurrentPosition,
56 NETWORK->ppstruDeviceList[pstruEventDetails->nDeviceId-1]->pstruDeviceMobility->pstruNextPosition,
57 sizeof* NETWORK->ppstruDeviceList[pstruEventDetails->nDeviceId-1]->pstruDeviceMobility->pstruCurrentPosition);
58
59 if(pstruMobilityVar->dLastTime+pstruMobilityVar->dPauseTime*1000000<pstruEventDetails->dEventTime+1000000) //Everytime Mobility being called
60 {
61 double* coordinates; // Pointer for array of X Y and Z 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
66 NETWORK->ppstruDeviceList[pstruEventDetails->nDeviceId-1]->pstruDeviceMobility->pstruNextPosition->Y = coordinates[1];
67 NETWORK->ppstruDeviceList[pstruEventDetails->nDeviceId - 1]->pstruDeviceMobility->pstruNextPosition->Z = coordinates[2];
68 free(coordinates); // Free memory of pointer
69 }
70
71 //store the last time
72 pstruMobilityVar->dLastTime = pstruEventDetails->dEventTime+1000000*step_size; // Update Last time since we want to match timings with SUMO
73 }
74 //update the device position
75 memcpy(NETWORK->ppstruDeviceList[pstruEventDetails->nDeviceId-1]->pstruDevicePosition,
76 NETWORK->ppstruDeviceList[pstruEventDetails->nDeviceId-1]->pstruDeviceMobility->pstruCurrentPosition,
77 sizeof* NETWORK->ppstruDeviceList[pstruEventDetails->nDeviceId-1]->pstruDevicePosition);
78
79 mobility_pass_position_to_animation(pstruEventDetails->nDeviceId,
80 pstruEventDetails->dEventTime,
81 DEVICE_POSITION(pstruEventDetails->nDeviceId));
82
83 //Add event for next point
84 pstruEventDetails->dEventTime+=1000000*step_size;
85 fnpAddEvent(pstruEventDetails);
86 pstruEventDetails->dEventTime-=1000000*step_size;
87}
88
89
90// This function Initiates a Pipes connection and sends GUI =1 or 0 based on Animation Status
91
92void pipes_init()
93{
94 BOOL fSuccess; //If reading/writing successfil or not
95 DWORD cbToWrite, cbWritten; // For Pipes
96 DWORD dwMode;
97 char message_to_be_sent[2];
98
99 LPCSTR lpszPipename = "\\\\.\\pipe\\netsim_sumo_pipe"; //Pipename
100 gui='0';
101 fSuccess=FALSE;
102 //getch();
103 fprintf(stderr,"Creating Sumo pipe\n");
104
105 while (1)
106 {
107 hPipe = CreateFileA
108 (
109 lpszPipename, // pipe name
110 GENERIC_READ | // read and write access
111 GENERIC_WRITE,
112 0, // no sharing
113 NULL, // default security attributes
114 OPEN_EXISTING, // opens existing pipe
115 0, // default attributes
116 NULL // no template file
117 );
118
119 if (hPipe != INVALID_HANDLE_VALUE)
120 break;
121
122 }
123
124 fprintf(stderr,"Connecting Sumo and NetSim in real time\n");
125 dwMode = PIPE_READMODE_MESSAGE; // The pipe connected; change to message-read mode.
126 fSuccess = SetNamedPipeHandleState
127 (
128 hPipe, // pipe handle
129 &dwMode, // new pipe mode
130 NULL, // don't set maximum bytes
131 NULL // don't set maximum time
132 );
133
134 if ( ! fSuccess)
135 fnSystemError("Error in connection netsim sump pipe.\n");
136 else
137 fprintf(stderr,"Connection done\n");
138
139 if(anim_get_anim_flag() == ANIMFLAG_ONLINE)
140 gui = '1';
141 else
142 gui = '0';
143
144
145 message_to_be_sent[0]=gui; //Send GUI enable or disable
146 message_to_be_sent[1]=0;
147
148 cbToWrite=(DWORD)strlen(message_to_be_sent);
149
150 fSuccess = WriteFile
151 (
152 hPipe, // pipe handle
153 message_to_be_sent, // message
154 cbToWrite, // message length
155 &cbWritten, // bytes written
156 NULL // not overlapped
157 );
158}
159//////////////////////////////////////////////////////
160
161
162
163double *corr(char* id)
164{
165 BOOL fSuccess; //If reading/writing successfil or not
166 DWORD cbRead, cbToWrite, cbWritten; // For Pipes
167 CHAR chBuf[BUFSIZ]; //For reading messages from pipes
168
169 double xcor1,ycor1,zcor1; // x y and z coordinates to be received from sumo
170 double* coordinates;
171
172 do
173 {
174 // Read Garbage from the pipe.
175
176 fSuccess = ReadFile
177 (
178 hPipe, // pipe handle
179 chBuf, // buffer to receive reply
180 BUFSIZ*sizeof(CHAR), // size of buffer
181 &cbRead, // number of bytes read
182 NULL // not overlapped
183 );
184
185 if ( ! fSuccess && GetLastError() != ERROR_MORE_DATA )
186 break;
187 chBuf[cbRead]=0;
188 } while (!fSuccess); // repeat loop if ERROR_MORE_DATA
189
190
191 //Send Vehicle Name to Python
192
193 cbToWrite=(DWORD)strlen(id)+1;
194 fSuccess = WriteFile
195 (
196 hPipe, // pipe handle
197 id, // message
198 cbToWrite, // message length
199 &cbWritten, // bytes written
200 NULL // not overlapped
201 );
202
203
204 // Read acknowledgment for vehicle ids
205 do
206 {
207
208 fSuccess = ReadFile
209 (
210 hPipe, // pipe handle
211 chBuf, // buffer to receive reply
212 BUFSIZ*sizeof(CHAR), // size of buffer
213 &cbRead, // number of bytes read
214 NULL // not overlapped
215 );
216
217 if ( ! fSuccess && GetLastError() != ERROR_MORE_DATA )
218 break;
219 chBuf[cbRead]=0;
220
221 } while (!fSuccess); // repeat loop if ERROR_MORE_DATA
222
223
224
225 if(chBuf[0]=='c') // If the vehicle is present ('c' for confirmation)
226 {
227 xcor1=0;ycor1=0;
228 do
229 {
230 // Read X coordinate
231 fSuccess = ReadFile
232 (
233 hPipe, // pipe handle
234 chBuf, // buffer to receive reply
235 BUFSIZ*sizeof(CHAR), // size of buffer
236 &cbRead, // number of bytes read
237 NULL // not overlapped
238 );
239
240 if ( ! fSuccess && GetLastError() != ERROR_MORE_DATA )
241 break;
242
243 } while (!fSuccess); // repeat loop if ERROR_MORE_DATA
244
245 xcor1 = atof(chBuf);
246
247 do
248 {
249 // Read Y coordinate
250
251 fSuccess = ReadFile
252 (
253 hPipe, // pipe handle
254 chBuf, // buffer to receive reply
255 BUFSIZ*sizeof(CHAR), // size of buffer
256 &cbRead, // number of bytes read
257 NULL // not overlapped
258 );
259
260 if ( ! fSuccess && GetLastError() != ERROR_MORE_DATA )
261 break;
262 chBuf[cbRead]=0;
263
264 } while (!fSuccess); // repeat loop if ERROR_MORE_DATA
265
266 ycor1 = atof(chBuf);
267
268 do
269 {
270 // Read Z coordinate
271
272 fSuccess = ReadFile
273 (
274 hPipe, // pipe handle
275 chBuf, // buffer to receive reply
276 BUFSIZ * sizeof(CHAR), // size of buffer
277 &cbRead, // number of bytes read
278 NULL // not overlapped
279 );
280
281 if (!fSuccess && GetLastError() != ERROR_MORE_DATA)
282 break;
283 chBuf[cbRead] = 0;
284
285 } while (!fSuccess); // repeat loop if ERROR_MORE_DATA
286
287 zcor1 = atof(chBuf);
288 coordinates = (double*)malloc(3*sizeof* coordinates);
289 coordinates[0]=xcor1;
290 coordinates[1]=ycor1;
291 coordinates[2] = zcor1;
292 return (coordinates); //Return X Y Z Coordinates
293 }
294
295 else
296 {
297 return(NULL); // If vehicle not found, return NULL
298 }
299
300}