-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpsPub.cpp
279 lines (246 loc) · 8.36 KB
/
gpsPub.cpp
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
278
279
/* gpsdLogger: publish GPSD-provided PL/I to shared memory for use w/MGEN et al.
Copyright (C) 2018 Fortian Inc. <[email protected]>
This program is free software; you can redistribute it and/or modify it only
under the terms of Version 2 of the GNU General Public License, as published by
the Free Software Foundation.
This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
Street, Fifth Floor, Boston, MA 02110-1301 USA. */
#include <syslog.h>
#include "gpsPub.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <unistd.h>
static const char *GPS_DEFAULT_KEY_FILE = "/tmp/gpskey";
#ifdef __cplusplus
extern "C" {
#endif
// Upon success, this returns a pointer for storage of published GPS position.
char *GPSMemoryInit(const char *keyFile, unsigned int size) {
char *posPtr;
int id;
FILE *filePtr;
struct shmid_ds ds;
unsigned int holder;
if (keyFile == NULL) {
keyFile = GPS_DEFAULT_KEY_FILE;
}
posPtr = ((char *)-1);
id = -1;
// First read file to see if shared memory already active
// If active, try to use it
filePtr = fopen(keyFile, "r");
if (filePtr != NULL) {
if (1 == fscanf(filePtr, "%d", &id)) {
if (((char *) -1) != (posPtr = (char *)shmat(id, 0, 0))) {
// Make sure pre-existing shared memory is right size
memcpy(&holder, posPtr, sizeof (unsigned int));
if (size != holder) {
GPSPublishShutdown((GPSHandle)posPtr, keyFile);
posPtr = (char *)-1;
}
} else {
syslog(LOG_WARNING, "GPSPublishInit(): shmat(): %m");
}
}
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) {
syslog(LOG_ERR, "GPSPublishInit(): shmget(): %m");
return NULL;
}
if (((char*)-1) ==(posPtr = (char*)shmat(id, 0, 0))) {
syslog(LOG_ERR, "GPSPublishInit(): shmat(): %m");
if (-1 == shmctl(id, IPC_RMID, &ds)) {
syslog(LOG_ERR, "GPSPublishInit(): shmctl(IPC_RMID): %m");
}
return NULL;
}
// Write "id" to "keyFile"
if ((filePtr = fopen(keyFile, "w+"))) {
if (fprintf(filePtr, "%d", id) <= 0) {
syslog(LOG_ERR, "GPSPublishInit() fprintf(): %m");
}
fclose(filePtr);
memset(posPtr + sizeof (unsigned int), 0, size);
memcpy(posPtr, &size, sizeof (unsigned int));
return (posPtr + sizeof (unsigned int));
} else {
syslog(LOG_ERR, "GPSPublishInit() fopen(): %m");
}
if (-1 == shmdt(posPtr)) {
syslog(LOG_WARNING, "GPSPublishInit() shmdt(): %m");
}
if (-1 == shmctl(id, IPC_RMID, &ds)) {
syslog(LOG_WARNING, "GPSPublishInit(): shmctl(IPC_RMID): %m");
}
return NULL;
}
if (((char *)-1) == posPtr) {
return NULL;
}
return (posPtr + sizeof (unsigned int));
}
void GPSPublishShutdown(GPSHandle gpsHandle, const char *keyFile) {
char *ptr;
FILE *filePtr;
int id;
struct shmid_ds ds;
ptr = (char *)gpsHandle - sizeof (unsigned int);
if (keyFile == NULL) {
keyFile = GPS_DEFAULT_KEY_FILE;
}
if (-1 == shmdt((void *)ptr)) {
syslog(LOG_WARNING, "GPSPublishShutdown() shmdt(): %m");
}
filePtr = fopen(keyFile, "r");
if (filePtr != NULL) {
if (1 == fscanf(filePtr, "%d", &id)) {
if (-1 == shmctl(id, IPC_RMID, &ds)) {
syslog(LOG_WARNING,
"GPSPublishShutdown(): shmctl(IPC_RMID): %m");
}
}
fclose(filePtr);
if (keyFile != NULL) { // this shouldn't happen but clang thinks it can
if (unlink(keyFile)) {
syslog(LOG_WARNING, "GPSPublishShutdown(): unlink(): %m");
}
}
} else {
syslog(LOG_WARNING, "GPSPublishShutdown(): fopen(): %m");
}
} // end GPSPublishShutdown;
// Upon success, this returns a pointer for storage of published GPS position.
GPSHandle GPSSubscribe(const char *keyFile) {
char* posPtr;
int id;
FILE *filePtr;
char *rv = NULL;
id = -1;
// First read file to see if shared memory already active. If active, try
// to use it.
if (keyFile == NULL) {
keyFile = GPS_DEFAULT_KEY_FILE;
}
filePtr = fopen(keyFile, "r");
if (filePtr != NULL) {
if (1 == fscanf(filePtr, "%d", &id)) {
if (((char *)-1) == (posPtr = (char *)shmat(id, 0, SHM_RDONLY))) {
syslog(LOG_ERR, "GPSSubscribe(): shmat(): %m");
fclose(filePtr);
} else {
fclose(filePtr);
rv = posPtr + sizeof (unsigned int);
}
} else {
syslog(LOG_ERR, "GPSSubscribe(): fscanf(): %m");
fclose(filePtr);
}
} else {
syslog(LOG_ERR, "GPSSubscribe(): fopen(): %m");
}
return rv;
}
void GPSUnsubscribe(GPSHandle gpsHandle) {
char *ptr;
ptr = (char *)gpsHandle - sizeof (unsigned int);
if (-1 == shmdt((void *)ptr)) {
syslog(LOG_WARNING, "GPSUnsubscribe() shmdt(): %m");
}
}
void GPSPublishPos(GPSHandle gpsHandle, double x, double y, double z) {
GPSPosition pos;
struct timeval t;
pos.x = x;
pos.y = y;
pos.z = z;
pos.xyvalid = 1;
pos.zvalid = 1;
pos.tvalid = 1;
pos.stale = 0;
gettimeofday(&t, NULL);
memcpy(&pos.gps_time, &t, sizeof (struct timeval));
memcpy(&pos.sys_time, &t, sizeof (struct timeval));
memcpy((char *)gpsHandle, &pos, sizeof (GPSPosition));
}
void GPSPublishUpdate(GPSHandle gpsHandle, const GPSPosition* currentPosition) {
memcpy((char *)gpsHandle, (char *)currentPosition, sizeof (GPSPosition));
}
void GPSGetCurrentPosition(GPSHandle gpsHandle, GPSPosition* currentPosition) {
memcpy((char *)currentPosition, (char *)gpsHandle, sizeof (GPSPosition));
}
unsigned int GPSSetMemory(GPSHandle gpsHandle, unsigned int offset,
const char *buffer, unsigned int len) {
char *ptr;
unsigned int size, delta;
ptr = (char *)gpsHandle - sizeof (unsigned int);
memcpy(&size, ptr, sizeof (unsigned int));
// Make sure request fits into available shared memory.
if ((offset + len) > size) {
syslog(LOG_WARNING,
"GPSSetMemory() request exceeds allocated shared memory");
delta = offset + len - size;
if (delta > len) {
return 0;
} else {
len -= delta;
}
}
ptr = (char *)gpsHandle + offset;
memcpy(ptr, buffer, len);
return len;
}
unsigned int GPSGetMemorySize(GPSHandle gpsHandle) {
char *ptr;
unsigned int size;
ptr = (char *)gpsHandle - sizeof (unsigned int);
memcpy(&size, ptr, sizeof (unsigned int));
return size;
}
const char *GPSGetMemoryPtr(GPSHandle gpsHandle, unsigned int offset) {
char *ptr;
unsigned int size;
ptr = (char *)gpsHandle - sizeof (unsigned int);
memcpy(&size, ptr, sizeof (unsigned int));
if (offset < size) {
return (char *)gpsHandle + offset;
} else {
syslog(LOG_ERR, "GPSGetMemoryPtr(): invalid request");
return NULL;
}
}
unsigned int GPSGetMemory(GPSHandle gpsHandle, unsigned int offset,
char *buffer, unsigned int len) {
char *ptr;
unsigned int size, delta;
ptr = (char *)gpsHandle - sizeof (unsigned int);
memcpy(&size, ptr, sizeof (unsigned int));
if (size < (offset + len)) {
delta = offset + len - size;
if (delta > len) {
syslog(LOG_ERR, "GPSGetMemory(): invalid request");
return 0;
} else {
len -= delta;
}
}
ptr = (char *)gpsHandle + offset;
memcpy(buffer, ptr, len);
return len;
}
#ifdef __cplusplus
}
#endif