-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultiMag.c
More file actions
332 lines (285 loc) · 9.06 KB
/
multiMag.c
File metadata and controls
332 lines (285 loc) · 9.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
//=========================================================================
// multiMag.c
//
// An interface for the RM3100 3-axis magnetometer from PNI Sensor Corp.
//
// Author: David M. Witten II, KD0EAG
// Date: Jan 30, 2021
// Date: Aug 27, 2021
// License: GPL 3.0
//=========================================================================
#include "main.h"
#include "logFiles.h"
#include "config.h"
#include "cmdline.h"
#include "sensorRoutines.h"
//int gflag = 1;
// #define _DEBUG0 1
// #define READ_I2C 0
// Global struct for parsing JSON
struct pStruct *jsparams = NULL;
//------------------------------------------
// Global variables
//------------------------------------------
//char version[MAXVERSIONLEN] = MULTIMAG_VERSION;
// variables to hold metadata read from files
char version[] = MULTIMAG_VERSION;
char cityState[MAXPATHBUFLEN] = "Columbia, MO, USA";
char callSign[MAXPATHBUFLEN] = "KD0EAG";
char latLonElv[MAXPATHBUFLEN] = "38.92241,-92.29776,228.90m";
char freqStd[MAXPATHBUFLEN] = "";
char nodeNum[MAXPATHBUFLEN] = "";
char sysTem[MAXPATHBUFLEN] = "";
char metadata[MAXPATHBUFLEN] = "";
char dataTyp[MAXPATHBUFLEN] = "";
char rollOverTime[UTCBUFLEN] = "00:00";
char sitePrefix[SITEPREFIXLEN] = "SITEPREFIX";
char gridSqr[GRIDSQRLEN] = "EM38uw";
char baseFileFolder[FOLDERNAMELEN] = "/PSWS/Srawdata";
char baseFilePath[MAXPATHBUFLEN] = "/home/dave";
char outFilePath[MAXPATHBUFLEN] = "";
char outFileName[MAXPATHBUFLEN] = "";
char configFilePath[MAXPATHBUFLEN] = "./";
char configFileName[MAXPATHBUFLEN] = "config/config.json";
//volatile int alarm_fired;
char outputPipeName[MAXPATHBUFLEN] = "/tmp/PSWS-pipein.fifo";
char inputPipeName[MAXPATHBUFLEN] = "/tmp/PSWS-pipeout.fifo";
#define SIGTERM_MSG "\nSIGTERM received.\n"
#define SIGINT_MSG "\nSIGINT received.\n"
//------------------------------------------
// sig_term_handler()
//------------------------------------------
void sig_term_handler(int signum, siginfo_t *info, void *ptr)
{
//saveConfigToFile(pList *p);
write(STDERR_FILENO, SIGTERM_MSG, sizeof(SIGTERM_MSG));
exit(0);
}
//------------------------------------------
// catch_sigterm()
//------------------------------------------
void catch_sigterm()
{
static struct sigaction _sigact;
memset(&_sigact, 0, sizeof(_sigact));
_sigact.sa_sigaction = sig_term_handler;
_sigact.sa_flags = SA_SIGINFO;
// closeI2CBus(&p);
sigaction(SIGTERM, &_sigact, NULL);
}
//------------------------------------------
// sig_int_handler()
//------------------------------------------
void sig_int_handler(int signum, siginfo_t *info, void *ptr)
{
write(STDERR_FILENO, SIGINT_MSG, sizeof(SIGINT_MSG));
exit(0);
}
//------------------------------------------
// catch_sigint()
//------------------------------------------
void catch_sigint()
{
static struct sigaction _sigact;
memset(&_sigact, 0, sizeof(_sigact));
_sigact.sa_sigaction = sig_int_handler;
_sigact.sa_flags = SA_SIGINFO;
//closeI2CBus(&p);
sigaction(SIGINT, &_sigact, NULL);
}
//------------------------------------------
// setupDefaults()
// All Default values should be set here.
//------------------------------------------
int setupDefaults(pList *p)
{
int rv = 0;
if(p != NULL)
{
// from runMag - investigate
p->magnetometerAddr = 0x20;
p->localTempAddr = 0x18;
p->remoteTempAddr = 0x19;
p->magRevId = 0;
p->doBistMask = 0; // ?
p->outDelay = 0;
p->singleRead = FALSE; // Is this useful?
p->tsMilliseconds = FALSE; // ?
p->showTotal = FALSE; // ?
p->logOutput = FALSE;
p->useOutputPipe = FALSE;
p->usePPS = FALSE;
p->writeWorkingCFG = FALSE;
p->TMRCRate = 0;
p->CMMSampleRate = 0;
p->samplingMode = 0;
p->NOSRegValue = 0;
p->cc_x = 0;
p->cc_y = 0;
p->cc_z = 0;
p->x_gain = 0;
p->y_gain = 0;
p->z_gain = 0;
// multiMag from here on...
p->Version = MULTIMAG_VERSION;
p->printParamFlg = FALSE;
p->numThreads = 1;
p->threadCadenceUS = 1000000;
p->threadOffsetUS = 150000;
p->i2cBusNumber = 1;
p->useI2CMUX = FALSE;
p->i2cMUXAddr = FALSE;
p->i2c_fd = 0;
p->outfp = NULL;
p->fdPipeIn = NOT_USED;
p->fdPipeOut = NOT_USED;
p->modeOutputFlag = 0;
p->logOutputTime = "00:00:00";
p->baseFilePath = "/PSWS/Srawdata/";
p->outputFilePath = ""; //"/Srawdata";
p->outputFileName = "";
p->pipeInPath = "/tmp/multiMag_pipeout.fifo";
p->pipeOutPath = "/tmp/multiMag_pipein.fifo";
p->rollOverTime = "00:00";
p->gridSqr = "EM38uw";
p->sitePrefix = "N0000023";
p->city = "Columbia";
p->state = "Missouri";
p->country = "USA";
p->postalcode = "65201";
p->lattitude = "38.92241";
p->longitude = "-92.29776";
p->elevation = "228.90m";
p->system = "";
}
};
#if( USE_SEMAPHORE )
// Semaphore mutex
sem_t mutexp[MAXTHREADS] = {};
#endif
//------------------------------------------
// i2cReader()
//------------------------------------------
void *i2cReaderThread(void *thread_id)
{
int *id = (int *) thread_id;
char utcStr[UTCBUFLEN] = "";
struct tm *utcTime = getUTC();
struct timeval tv;
while(1)
{
//struct timeval tv;
#if( USE_SEMAPHORE )
// Wait for Semaphore.
sem_wait(&mutexp[*id]);
usleep(1000 * 1000); // 1 second
gettimeofday(&tv, NULL);
printf("Task %u: S: %lu, uS: %lu\n", *id, tv.tv_sec, tv.tv_usec);
// Reset Semaphore.
sem_post(&mutexp[*id]);
#else
//catch_sigterm();
usleep(1000 * 1000); // 1 second
// sched_yield();
// clock_nanosleep()
gettimeofday(&tv, NULL);
printf("Task %u: S: %lu, uS: %lu\n", *id, tv.tv_sec, tv.tv_usec);
#endif
}
pthread_exit(NULL);
}
//------------------------------------------
// main()
//
//------------------------------------------
int main(int argc, char** argv)
{
pthread_t tids[MAXTHREADS];
int ids[MAXTHREADS] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int i;
int idd;
pList p;
int rv = 0;
catch_sigterm();
catch_sigint();
// Get default runtime parameters .
setupDefaults(&p);
// Get cunfiguration file overrides of runtime parameters .
readConfig(&p);
// Get command line overrides of runtime parameters .
getCommandLine(argc, argv, &p);
// Open files for logging.
openLogs(&p);
// Open pipes for realtime presentation.
openUIPipes(&p);
// Write out configuration to ./config/config.json
if(p.writeWorkingCFG)
{
strncat(configFilePath, configFileName, MAXPATHBUFLEN);
saveConfigToFile(&p, configFilePath);
}
// Display running parameters.
if(p.printParamFlg)
{
fprintf(stdout, "#==============================================================\n\n");
printParams(&p);
showSettings(&p);
fprintf(stdout, "#==============================================================\n\n");
}
// Open up I2C
openI2CBus(&p);
// Create Threads.
for(i = 0; i < p.numThreads; i++)
{
#if( USE_SEMAPHORE )
// Create Semaphore.
sem_init(&mutexp[i], 0, 1);
#endif
printf("Creating i2cReader thread %u\n", i);
rv = pthread_create(&tids[i], NULL, i2cReaderThread, &ids[i]);
if(rv)
{
printf("unable to create thread! \n");
exit(-1);
}
usleep(p.threadOffsetUS); // Default 150 uS
}
// 'Join' Threads.
for(i = 0; i < p.numThreads; i++)
{
pthread_join(tids[i], NULL);
}
#if( USE_SEMAPHORE )
idd = p.numThreads;
while(1)
{
if(idd > 0)
{
printf("Posting flag for semaphore %u\n", idd);
sem_post(&mutexp[idd]);
i--;
if (idd < 1)
{
idd = p.numThreads;
}
}
usleep(p.threadCadenceUS);
}
#endif
#if( USE_SEMAPHORE )
// Destroy Semaphore
idd = p.numThreads;
while(idd)
{
sem_destroy(&mutexp[idd]);
idd--;
}
printf("Semaphore destroyed. Shutting down...\n");
#endif
pthread_exit(NULL);
hashDeleteAll();
closeLogs(&p);
closeUIPipes(&p);
closeI2CBus(&p);
return 0;
}