-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgpsPub.cpp
More file actions
277 lines (256 loc) · 8.06 KB
/
gpsPub.cpp
File metadata and controls
277 lines (256 loc) · 8.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#include "gpsPub.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/stat.h> // for permissions flags
#include <unistd.h> // for unlink()
static const char* GPS_DEFAULT_KEY_FILE = "/tmp/gpskey";
/**
* Upon success, this returns a pointer for
* storage of published GPS position
*/
extern "C" char* GPSMemoryInit(const char* keyFile, unsigned int size)
{
if (!keyFile) keyFile = GPS_DEFAULT_KEY_FILE;
char* posPtr = ((char*)-1);
int id = -1;
// First read file to see if shared memory already active
// If active, try to use it
FILE* filePtr = fopen(keyFile, "r");
if (filePtr)
{
if (1 == fscanf(filePtr, "%d", &id))
{
if (((char*)-1) != (posPtr = (char*)shmat(id, 0, 0)))
{
// Make sure pre-existing shared memory is right size
unsigned int theSize;
memcpy(&theSize, posPtr, sizeof(unsigned int));
if (size != theSize)
{
GPSPublishShutdown((GPSHandle)posPtr, keyFile);
posPtr = (char*)-1;
}
}
else
{
perror("GPSPublishInit(): shmat() warning");
}
}
fclose(filePtr);
}
if (((char*)-1) == posPtr)
{
// Create new shared memory segment
// and advertise its "id" in the keyFile
id = shmget(0, (int)(size+sizeof(unsigned int)), IPC_CREAT |
SHM_R| S_IRGRP | S_IROTH | SHM_W);
if (-1 == id)
{
perror("GPSPublishInit(): shmget() error");
return NULL;
}
if (((char*)-1) ==(posPtr = (char*)shmat(id, 0, 0)))
{
perror("GPSPublishInit(): shmat() error");
struct shmid_ds ds;
if (-1 == shmctl(id, IPC_RMID, &ds))
{
perror("GPSPublishInit(): shmctl(IPC_RMID) error");
}
return NULL;
}
// Write "id" to "keyFile"
if ((filePtr = fopen(keyFile, "w+")))
{
if (fprintf(filePtr, "%d", id) <= 0)
perror("GPSPublishInit() fprintf() error");
fclose(filePtr);
memset(posPtr+sizeof(unsigned int), 0, size);
memcpy(posPtr, &size, sizeof(unsigned int));
return (posPtr + sizeof(unsigned int));
}
else
{
perror("GPSPublishInit() fopen() error");
}
if (-1 == shmdt(posPtr))
perror("GPSPublishInit() shmdt() error");
struct shmid_ds ds;
if (-1 == shmctl(id, IPC_RMID, &ds))
perror("GPSPublishInit(): shmctl(IPC_RMID) error");
return NULL;
}
if (((char*)-1) == posPtr)
return NULL;
else
return (posPtr + sizeof(unsigned int));
} // end GPSPublishInit()
extern "C" void GPSPublishShutdown(GPSHandle gpsHandle, const char* keyFile)
{
char* ptr = (char*)gpsHandle - sizeof(unsigned int);
if (!keyFile) keyFile = GPS_DEFAULT_KEY_FILE;
if (-1 == shmdt(ptr))
perror("GPSPublishShutdown() shmdt() error");
FILE* filePtr = fopen(keyFile, "r");
if (filePtr)
{
int id;
if (1 == fscanf(filePtr, "%d", &id))
{
struct shmid_ds ds;
if (-1 == shmctl(id, IPC_RMID, &ds))
perror("GPSPublishShutdown(): shmctl(IPC_RMID) error");
}
fclose(filePtr);
if (unlink(keyFile))
perror("GPSPublishShutdown(): unlink() error");
}
else
{
perror("GPSPublishShutdown(): fopen() error");
}
} // end GPSPublishShutdown();
/**
* Upon success, this returns a pointer for
* storage of published GPS position
*/
extern "C" GPSHandle GPSSubscribe(const char* keyFile)
{
if (!keyFile) keyFile = GPS_DEFAULT_KEY_FILE;
char* posPtr = ((char*)-1);
int id = -1;
// First read file to see if shared memory already active
// If active, try to use it
FILE* filePtr = fopen(keyFile, "r");
if (filePtr)
{
if (1 == fscanf(filePtr, "%d", &id))
{
if (((char*)-1) == (posPtr = (char*)shmat(id, 0, SHM_RDONLY)))
{
perror("GPSSubscribe(): shmat() error");
fclose(filePtr);
return NULL;
}
else
{
fclose(filePtr);
return (posPtr + sizeof(unsigned int));
}
}
else
{
perror("GPSSubscribe(): fscanf() error");
fclose(filePtr);
return NULL;
}
}
else
{
//fprintf(stderr, "GPSSubscribe(): Error opening %s.\n", keyFile);
perror("GPSSubscribe(): fopen() error");
return NULL;
}
} // end GPSSubscribe()
extern "C" void GPSUnsubscribe(GPSHandle gpsHandle)
{
char* ptr = (char*)gpsHandle - sizeof(unsigned int);
if (-1 == shmdt(ptr))
perror("GPSUnsubscribe() shmdt() error");
} // end GPSUnsubscribe()
extern "C" void GPSPublishPos(GPSHandle gpsHandle, double x, double y, double z)
{
GPSPosition pos;
// fprintf(stderr, "GPSPublishPos %f,%f\n",x,y);
pos.x = x;
pos.y = y;
pos.z = z;
pos.xyvalid = true;
pos.zvalid = true;
pos.tvalid = true;
pos.stale = false;
// Update time
struct timeval time;
gettimeofday(&time, 0);
memcpy(&pos.gps_time, &time, sizeof(struct timeval));
memcpy(&pos.sys_time, &time, sizeof(struct timeval));
memcpy((char*)gpsHandle, &pos, sizeof(GPSPosition));
}
extern "C" void GPSPublishUpdate(GPSHandle gpsHandle, const GPSPosition* currentPosition)
{
// fprintf(stderr, "GPSPublishUpdate %f,%f\n",currentPosition->x,currentPosition->y);
memcpy((char*)gpsHandle, (char*)currentPosition, sizeof(GPSPosition));
} // end GPSPublishUpdate()
extern "C" void GPSGetCurrentPosition(GPSHandle gpsHandle, GPSPosition* currentPosition)
{
memcpy((char*)currentPosition, (char*)gpsHandle, sizeof(GPSPosition));
} // end GPSGetCurrentPosition()
extern "C" unsigned int GPSSetMemory(GPSHandle gpsHandle, unsigned int offset,
const char* buffer, unsigned int len)
{
char* ptr = (char*)gpsHandle - sizeof(unsigned int);
unsigned int size;
memcpy(&size, ptr, sizeof(unsigned int));
// Make sure request fits into available shared memory
if ((offset+len) > size)
{
fprintf(stderr, "GPSSetMemory() Request exceeds allocated shared memory!\n");
unsigned int delta = offset + len - size;
if (delta > len)
return 0;
else
len -= delta;
}
ptr = (char*)gpsHandle + offset;
memcpy(ptr, buffer, len);
return len;
} // end GPSSetMemory()
extern "C" unsigned int GPSGetMemorySize(GPSHandle gpsHandle)
{
char* ptr = (char*)gpsHandle - sizeof(unsigned int);
unsigned int size;
memcpy(&size, ptr, sizeof(unsigned int));
return size;
}
extern "C" const char* GPSGetMemoryPtr(GPSHandle gpsHandle,
unsigned int offset)
{
char* ptr = (char*)gpsHandle - sizeof(unsigned int);
unsigned int size;
memcpy(&size, ptr, sizeof(unsigned int));
if (offset < size)
{
return (char*)gpsHandle + offset;
}
else
{
fprintf(stderr, "GPSGetMemory() Invalid request!\n");
return (const char*)NULL;
}
} // end GPSGetMemoryPtr()
extern "C" unsigned int GPSGetMemory(GPSHandle gpsHandle, unsigned int offset,
char* buffer, unsigned int len)
{
char* ptr = (char*)gpsHandle - sizeof(unsigned int);
unsigned int size;
memcpy(&size, ptr, sizeof(unsigned int));
if (size < (offset+len))
{
unsigned int delta = offset + len - size;
if (delta > len)
{
fprintf(stderr, "GPSGetMemory() Invalid request!\n");
return 0;
}
else
{
len -= delta;
}
}
ptr = (char*)gpsHandle + offset;
memcpy(buffer, ptr, len);
return len;
} // end GPSGetMemory()