-
Notifications
You must be signed in to change notification settings - Fork 2
/
MDM.h
767 lines (672 loc) · 19.1 KB
/
MDM.h
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
// Prevent Visual Studio Intellisense from defining _WIN32 and _MSC_VER when we use
// Visual Studio to edit Linux or Borland C++ code.
#ifdef __linux__
# undef _WIN32
#endif // __linux__
#if defined(__GNUC__) || defined(__BORLANDC__)
# undef _MSC_VER
#endif // defined(__GNUC__) || defined(__BORLANDC__)
#ifndef MDM_H
#define MDM_H
#include "OSMisc.h"
#include "RS232Port.h"
#ifndef DISABLE_MDMTHREAD
#include "OSThread.h"
#endif // !DISABLE_MDMTHREAD
#define TIMEOUT_MESSAGE_MDM 4.0 // In s.
// Should be at least 2 * number of bytes to be sure to contain entirely the biggest desired message (or group of messages) + 1.
#define MAX_NB_BYTES_MDM 1024
#pragma region MDM-SPECIFIC DEFINITIONS
struct uint8bits_MDM
{
unsigned int bit0 : 1;
unsigned int bit1 : 1;
unsigned int bit2 : 1;
unsigned int bit3 : 1;
unsigned int bit4 : 1;
unsigned int bit5 : 1;
unsigned int bit6 : 1;
unsigned int bit7 : 1;
};
struct uint16bits_MDM
{
unsigned int bit0 : 1;
unsigned int bit1 : 1;
unsigned int bit2 : 1;
unsigned int bit3 : 1;
unsigned int bit4 : 1;
unsigned int bit5 : 1;
unsigned int bit6 : 1;
unsigned int bit7 : 1;
unsigned int bit8 : 1;
unsigned int bit9 : 1;
unsigned int bit10 : 1;
unsigned int bit11 : 1;
unsigned int bit12 : 1;
unsigned int bit13 : 1;
unsigned int bit14 : 1;
unsigned int bit15 : 1;
};
struct uint32bits_MDM
{
unsigned int bit0 : 1;
unsigned int bit1 : 1;
unsigned int bit2 : 1;
unsigned int bit3 : 1;
unsigned int bit4 : 1;
unsigned int bit5 : 1;
unsigned int bit6 : 1;
unsigned int bit7 : 1;
unsigned int bit8 : 1;
unsigned int bit9 : 1;
unsigned int bit10 : 1;
unsigned int bit11 : 1;
unsigned int bit12 : 1;
unsigned int bit13 : 1;
unsigned int bit14 : 1;
unsigned int bit15 : 1;
unsigned int bit16 : 1;
unsigned int bit17 : 1;
unsigned int bit18 : 1;
unsigned int bit19 : 1;
unsigned int bit20 : 1;
unsigned int bit21 : 1;
unsigned int bit22 : 1;
unsigned int bit23 : 1;
unsigned int bit24 : 1;
unsigned int bit25 : 1;
unsigned int bit26 : 1;
unsigned int bit27 : 1;
unsigned int bit28 : 1;
unsigned int bit29 : 1;
unsigned int bit30 : 1;
unsigned int bit31 : 1;
};
struct uint64bits_MDM
{
unsigned int bit0 : 1;
unsigned int bit1 : 1;
unsigned int bit2 : 1;
unsigned int bit3 : 1;
unsigned int bit4 : 1;
unsigned int bit5 : 1;
unsigned int bit6 : 1;
unsigned int bit7 : 1;
unsigned int bit8 : 1;
unsigned int bit9 : 1;
unsigned int bit10 : 1;
unsigned int bit11 : 1;
unsigned int bit12 : 1;
unsigned int bit13 : 1;
unsigned int bit14 : 1;
unsigned int bit15 : 1;
unsigned int bit16 : 1;
unsigned int bit17 : 1;
unsigned int bit18 : 1;
unsigned int bit19 : 1;
unsigned int bit20 : 1;
unsigned int bit21 : 1;
unsigned int bit22 : 1;
unsigned int bit23 : 1;
unsigned int bit24 : 1;
unsigned int bit25 : 1;
unsigned int bit26 : 1;
unsigned int bit27 : 1;
unsigned int bit28 : 1;
unsigned int bit29 : 1;
unsigned int bit30 : 1;
unsigned int bit31 : 1;
unsigned int bit32 : 1;
unsigned int bit33 : 1;
unsigned int bit34 : 1;
unsigned int bit35 : 1;
unsigned int bit36 : 1;
unsigned int bit37 : 1;
unsigned int bit38 : 1;
unsigned int bit39 : 1;
unsigned int bit40 : 1;
unsigned int bit41 : 1;
unsigned int bit42 : 1;
unsigned int bit43 : 1;
unsigned int bit44 : 1;
unsigned int bit45 : 1;
unsigned int bit46 : 1;
unsigned int bit47 : 1;
unsigned int bit48 : 1;
unsigned int bit49 : 1;
unsigned int bit50 : 1;
unsigned int bit51 : 1;
unsigned int bit52 : 1;
unsigned int bit53 : 1;
unsigned int bit54 : 1;
unsigned int bit55 : 1;
unsigned int bit56 : 1;
unsigned int bit57 : 1;
unsigned int bit58 : 1;
unsigned int bit59 : 1;
unsigned int bit60 : 1;
unsigned int bit61 : 1;
unsigned int bit62 : 1;
unsigned int bit63 : 1;
};
union usInt8_MDM
{
char u;
struct uint8bits_MDM bits;
};
typedef union usInt8_MDM usInt8_MDM;
union uInt8_MDM
{
unsigned char uc;
struct uint8bits_MDM bits;
};
typedef union uInt8_MDM uInt8_MDM;
union u2sShortMSG_MDM
{
short s[2];
unsigned char uc[4];
unsigned int u;
struct uint32bits_MDM bits;
};
typedef union u2sShortMSG_MDM u2sShortMSG_MDM;
union u4sShortMSG_MDM
{
short s[4];
unsigned char uc[8];
double d;
struct uint64bits_MDM bits;
};
typedef union u4sShortMSG_MDM u4sShortMSG_MDM;
#pragma endregion
struct MDM
{
RS232PORT RS232Port;
FILE* pfSaveFile; // Used to save raw data, should be handled specifically...
char szCfgFilePath[256];
// Parameters.
char szDevPath[256];
int BaudRate;
int timeout;
int threadperiod;
BOOL bSaveRawData;
int MessageLen;
int InternalBufferSize;
int ModemBitRate;
int DelayReadWriteEchoByte;
};
typedef struct MDM MDM;
// A simplistic example of forward error correction (FEC) is to transmit each data bit 3 times,
// which is known as a (3,1) repetition code. This allows an error in any one of the three samples
// to be corrected by "majority vote" or "democratic voting" (Wikipedia).
/*
Send data with a Tritech Micron data modem.
Should be used in combination with RecvDataMDM() or RecvAllDataMDM().
uint8* buf : (IN) Valid pointer to the data to send.
int buflen : (IN) Number of bytes to send.
Return : EXIT_SUCCESS or EXIT_FAILURE if there is an error.
*/
inline int SendDataMDM(MDM* pMDM, uint8* buf, int buflen, int* pSentBytes)
{
#ifdef _DEBUG_MESSAGES_MDM
int i = 0;
#endif // _DEBUG_MESSAGES_MDM
if (buflen > pMDM->InternalBufferSize)
{
printf("SendDataMDM error (%s) : %s"
"(pMDM=%#x)\n",
strtime_m(),
"Too many data to send at once. ",
(unsigned int)(intptr_t)pMDM);
return EXIT_FAILURE;
}
if (WriteRS232Port(&pMDM->RS232Port, buf, buflen, pSentBytes) != EXIT_SUCCESS)
{
printf("SendDataMDM error (%s) : %s"
"(pMDM=%#x)\n",
strtime_m(),
"Error sending data with a MDM. ",
(unsigned int)(intptr_t)pMDM);
return EXIT_FAILURE;
}
#ifdef _DEBUG_MESSAGES_MDM
for (i = 0; i < *pSentBytes; i++)
{
printf("%.2x ", (int)buf[i]);
}
printf("\n");
#endif // _DEBUG_MESSAGES_MDM
#ifdef ENABLE_DEFAULT_SAVE_RAW_DATA_MDM
if ((pMDM->bSaveRawData)&&(pMDM->pfSaveFile))
{
fwrite(buf, *pSentBytes, 1, pMDM->pfSaveFile);
fflush(pMDM->pfSaveFile);
}
#endif // ENABLE_DEFAULT_SAVE_RAW_DATA_MDM
return EXIT_SUCCESS;
}
/*
Receive data with a Tritech Micron data modem.
Should be used in combination with SendDataMDM() or SendAllDataMDM().
uint8* buf : (INOUT) Valid pointer that will receive the data received.
int buflen : (IN) Number of bytes to receive.
Return : EXIT_SUCCESS or EXIT_FAILURE if there is an error.
*/
inline int RecvDataMDM(MDM* pMDM, uint8* buf, int buflen, int* pReceivedBytes)
{
#ifdef _DEBUG_MESSAGES_MDM
int i = 0;
#endif // _DEBUG_MESSAGES_MDM
if (buflen > pMDM->InternalBufferSize)
{
printf("RecvDataMDM error (%s) : %s"
"(pMDM=%#x)\n",
strtime_m(),
"Too many data to receive at once. ",
(unsigned int)(intptr_t)pMDM);
return EXIT_FAILURE;
}
if (ReadRS232Port(&pMDM->RS232Port, buf, buflen, pReceivedBytes) != EXIT_SUCCESS)
{
printf("RecvDataMDM error (%s) : %s"
"(pMDM=%#x)\n",
strtime_m(),
"Error receiving data with a MDM. ",
(unsigned int)(intptr_t)pMDM);
return EXIT_FAILURE;
}
#ifdef _DEBUG_MESSAGES_MDM
for (i = 0; i < *pReceivedBytes; i++)
{
printf("%.2x ", (int)buf[i]);
}
printf("\n");
#endif // _DEBUG_MESSAGES_MDM
#ifdef ENABLE_DEFAULT_SAVE_RAW_DATA_MDM
if ((pMDM->bSaveRawData)&&(pMDM->pfSaveFile))
{
fwrite(buf, *pReceivedBytes, 1, pMDM->pfSaveFile);
fflush(pMDM->pfSaveFile);
}
#endif // ENABLE_DEFAULT_SAVE_RAW_DATA_MDM
return EXIT_SUCCESS;
}
/*
Purge data from a Tritech Micron data modem.
Return : EXIT_SUCCESS or EXIT_FAILURE if there is an error.
*/
inline int PurgeDataMDM(MDM* pMDM)
{
if (PurgeRS232Port(&pMDM->RS232Port) != EXIT_SUCCESS)
{
printf("PurgeDataMDM error (%s) : %s"
"(pMDM=%#x)\n",
strtime_m(),
"Error purging data from a MDM. ",
(unsigned int)(intptr_t)pMDM);
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
/*
Send data with a Tritech Micron data modem.
Retry automatically if all the bytes were not written.
Should be used in combination with RecvDataMDM() or RecvAllDataMDM().
uint8* buf : (IN) Valid pointer to the data to send.
int buflen : (IN) Number of bytes to send.
Return : EXIT_SUCCESS or EXIT_FAILURE if there is an error.
*/
inline int SendAllDataMDM(MDM* pMDM, uint8* buf, int buflen)
{
#ifdef _DEBUG_MESSAGES_MDM
int i = 0;
#endif // _DEBUG_MESSAGES_MDM
if (buflen > pMDM->InternalBufferSize)
{
printf("SendAllDataMDM error (%s) : %s"
"(pMDM=%#x)\n",
strtime_m(),
"Too many data to send at once. ",
(unsigned int)(intptr_t)pMDM);
return EXIT_FAILURE;
}
if (WriteAllRS232Port(&pMDM->RS232Port, buf, buflen) != EXIT_SUCCESS)
{
printf("SendAllDataMDM error (%s) : %s"
"(pMDM=%#x)\n",
strtime_m(),
"Error sending data with a MDM. ",
(unsigned int)(intptr_t)pMDM);
return EXIT_FAILURE;
}
#ifdef _DEBUG_MESSAGES_MDM
for (i = 0; i < buflen; i++)
{
printf("%.2x ", (int)buf[i]);
}
printf("\n");
#endif // _DEBUG_MESSAGES_MDM
#ifdef ENABLE_DEFAULT_SAVE_RAW_DATA_MDM
if ((pMDM->bSaveRawData)&&(pMDM->pfSaveFile))
{
fwrite(buf, buflen, 1, pMDM->pfSaveFile);
fflush(pMDM->pfSaveFile);
}
#endif // ENABLE_DEFAULT_SAVE_RAW_DATA_MDM
return EXIT_SUCCESS;
}
/*
Receive data with a Tritech Micron data modem.
Retry automatically if all the bytes were not read.
Should be used in combination with SendDataMDM() or SendAllDataMDM().
uint8* buf : (INOUT) Valid pointer that will receive the data received.
int buflen : (IN) Number of bytes to receive.
Return : EXIT_SUCCESS or EXIT_FAILURE if there is an error.
*/
inline int RecvAllDataMDM(MDM* pMDM, uint8* buf, int buflen)
{
#ifdef _DEBUG_MESSAGES_MDM
int i = 0;
#endif // _DEBUG_MESSAGES_MDM
if (buflen > pMDM->InternalBufferSize)
{
printf("RecvAllDataMDM error (%s) : %s"
"(pMDM=%#x)\n",
strtime_m(),
"Too many data to receive at once. ",
(unsigned int)(intptr_t)pMDM);
return EXIT_FAILURE;
}
if (ReadAllRS232Port(&pMDM->RS232Port, buf, buflen) != EXIT_SUCCESS)
{
printf("RecvAllDataMDM error (%s) : %s"
"(pMDM=%#x)\n",
strtime_m(),
"Error receiving data with a MDM. ",
(unsigned int)(intptr_t)pMDM);
return EXIT_FAILURE;
}
#ifdef _DEBUG_MESSAGES_MDM
for (i = 0; i < buflen; i++)
{
printf("%.2x ", (int)buf[i]);
}
printf("\n");
#endif // _DEBUG_MESSAGES_MDM
#ifdef ENABLE_DEFAULT_SAVE_RAW_DATA_MDM
if ((pMDM->bSaveRawData)&&(pMDM->pfSaveFile))
{
fwrite(buf, buflen, 1, pMDM->pfSaveFile);
fflush(pMDM->pfSaveFile);
}
#endif // ENABLE_DEFAULT_SAVE_RAW_DATA_MDM
return EXIT_SUCCESS;
}
/*
Send data with a Tritech Micron data modem.
Append automatically a CRC-16 at the end of the data to enable error checking.
Should be used in combination with RecvDataCRC16MDM().
uint8* buf : (IN) Valid pointer to the data to send.
int buflen : (IN) Number of bytes to send.
Return : EXIT_SUCCESS or EXIT_FAILURE if there is an error.
*/
inline int SendDataCRC16MDM(MDM* pMDM, uint8* buf, int buflen)
{
uint8* writebuf = NULL;
#ifdef _DEBUG_MESSAGES_MDM
int i = 0;
#endif // _DEBUG_MESSAGES_MDM
writebuf = (uint8*)calloc(pMDM->InternalBufferSize, sizeof(uint8));
if (!writebuf)
{
printf("SendDataCRC16MDM error (%s) : %s"
"(pMDM=%#x)\n",
strtime_m(),
szOSUtilsErrMsgs[EXIT_OUT_OF_MEMORY],
(unsigned int)(intptr_t)pMDM);
return EXIT_FAILURE;
}
if (buflen+2 > pMDM->InternalBufferSize)
{
printf("SendDataCRC16MDM error (%s) : %s"
"(pMDM=%#x)\n",
strtime_m(),
"Too many data to send at once. ",
(unsigned int)(intptr_t)pMDM);
free(writebuf);
return EXIT_FAILURE;
}
// Copy desired data.
memcpy(writebuf, buf, buflen);
// Append CRC-16.
CalcCRC16(buf, buflen, &(writebuf[buflen]), &(writebuf[buflen+1]));
if (WriteAllRS232Port(&pMDM->RS232Port, writebuf, buflen+2) != EXIT_SUCCESS)
{
printf("SendDataCRC16MDM error (%s) : %s"
"(pMDM=%#x)\n",
strtime_m(),
"Error sending data with a MDM. ",
(unsigned int)(intptr_t)pMDM);
free(writebuf);
return EXIT_FAILURE;
}
#ifdef _DEBUG_MESSAGES_MDM
for (i = 0; i < buflen+2; i++)
{
printf("%.2x ", (int)writebuf[i]);
}
printf("\n");
#endif // _DEBUG_MESSAGES_MDM
#ifdef ENABLE_DEFAULT_SAVE_RAW_DATA_MDM
if ((pMDM->bSaveRawData)&&(pMDM->pfSaveFile))
{
fwrite(writebuf, buflen+2, 1, pMDM->pfSaveFile);
fflush(pMDM->pfSaveFile);
}
#endif // ENABLE_DEFAULT_SAVE_RAW_DATA_MDM
free(writebuf);
return EXIT_SUCCESS;
}
/*
Receive data with a Tritech Micron data modem.
Check the CRC-16 at the end of the data.
Should be used in combination with SendDataCRC16MDM().
uint8* buf : (INOUT) Valid pointer that will receive the data received.
int buflen : (IN) Number of bytes to receive.
Return : EXIT_SUCCESS, EXIT_INVALID_DATA if bad CRC-16 or EXIT_FAILURE if there is an error.
*/
inline int RecvDataCRC16MDM(MDM* pMDM, uint8* buf, int buflen)
{
uint8* readbuf = NULL;
uint8 crc_h = 0;
uint8 crc_l = 0;
#ifdef _DEBUG_MESSAGES_MDM
int i = 0;
#endif // _DEBUG_MESSAGES_MDM
readbuf = (uint8*)calloc(pMDM->InternalBufferSize, sizeof(uint8));
if (!readbuf)
{
printf("RecvDataCRC16MDM error (%s) : %s"
"(pMDM=%#x)\n",
strtime_m(),
szOSUtilsErrMsgs[EXIT_OUT_OF_MEMORY],
(unsigned int)(intptr_t)pMDM);
return EXIT_FAILURE;
}
if (buflen+2 > pMDM->InternalBufferSize)
{
printf("RecvDataCRC16MDM error (%s) : %s"
"(pMDM=%#x)\n",
strtime_m(),
"Too many data to receive at once. ",
(unsigned int)(intptr_t)pMDM);
free(readbuf);
return EXIT_FAILURE;
}
if (ReadAllRS232Port(&pMDM->RS232Port, readbuf, buflen+2) != EXIT_SUCCESS)
{
printf("RecvDataCRC16MDM error (%s) : %s"
"(pMDM=%#x)\n",
strtime_m(),
"Error receiving data with a MDM. ",
(unsigned int)(intptr_t)pMDM);
free(readbuf);
return EXIT_FAILURE;
}
#ifdef _DEBUG_MESSAGES_MDM
for (i = 0; i < buflen+2; i++)
{
printf("%.2x ", (int)readbuf[i]);
}
printf("\n");
#endif // _DEBUG_MESSAGES_MDM
#ifdef ENABLE_DEFAULT_SAVE_RAW_DATA_MDM
if ((pMDM->bSaveRawData)&&(pMDM->pfSaveFile))
{
fwrite(readbuf, buflen+2, 1, pMDM->pfSaveFile);
fflush(pMDM->pfSaveFile);
}
#endif // ENABLE_DEFAULT_SAVE_RAW_DATA_MDM
// Copy desired data.
memcpy(buf, readbuf, buflen);
// Compute CRC-16.
CalcCRC16(buf, buflen, &crc_h, &crc_l);
// Compare received CRC-16 with computed one.
if ((readbuf[buflen] != crc_h)||(readbuf[buflen+1] != crc_l))
{
printf("RecvDataCRC16MDM error (%s) : %s"
"(pMDM=%#x)\n",
strtime_m(),
"Bad CRC-16. ",
(unsigned int)(intptr_t)pMDM);
free(readbuf);
return EXIT_INVALID_DATA;
}
free(readbuf);
return EXIT_SUCCESS;
}
inline int EchoByteMDM(MDM* pMDM, uint8* pb)
{
int n = 0;
if (ReadRS232Port(&pMDM->RS232Port, pb, 1, &n) == EXIT_SUCCESS)
{
#ifdef _DEBUG_MESSAGES_MDM
if (n == 1) printf("%.2x \n", (int)*pb);
#endif // _DEBUG_MESSAGES_MDM
#ifdef ENABLE_DEFAULT_SAVE_RAW_DATA_MDM
if ((pMDM->bSaveRawData)&&(pMDM->pfSaveFile))
{
fwrite(pb, n, 1, pMDM->pfSaveFile);
fflush(pMDM->pfSaveFile);
}
#endif // ENABLE_DEFAULT_SAVE_RAW_DATA_MDM
mSleep(pMDM->DelayReadWriteEchoByte);
if (WriteRS232Port(&pMDM->RS232Port, pb, 1, &n) != EXIT_SUCCESS)
{
printf("EchoMDM error (%s) : %s"
"(pMDM=%#x)\n",
strtime_m(),
"Error sending data with a MDM. ",
(unsigned int)(intptr_t)pMDM);
return EXIT_FAILURE;
}
#ifdef _DEBUG_MESSAGES_MDM
if (n == 1) printf("%.2x \n", (int)*pb);
#endif // _DEBUG_MESSAGES_MDM
#ifdef ENABLE_DEFAULT_SAVE_RAW_DATA_MDM
if ((pMDM->bSaveRawData)&&(pMDM->pfSaveFile))
{
fwrite(pb, n, 1, pMDM->pfSaveFile);
fflush(pMDM->pfSaveFile);
}
#endif // ENABLE_DEFAULT_SAVE_RAW_DATA_MDM
}
return EXIT_SUCCESS;
}
// MDM must be initialized to 0 before (e.g. MDM mdm; memset(&mdm, 0, sizeof(MDM));)!
inline int ConnectMDM(MDM* pMDM, char* szCfgFilePath)
{
FILE* file = NULL;
char line[256];
memset(pMDM->szCfgFilePath, 0, sizeof(pMDM->szCfgFilePath));
sprintf(pMDM->szCfgFilePath, "%.255s", szCfgFilePath);
// If szCfgFilePath starts with "hardcoded://", parameters are assumed to be already set in the structure,
// otherwise it should be loaded from a configuration file.
if (strncmp(szCfgFilePath, "hardcoded://", strlen("hardcoded://")) != 0)
{
memset(line, 0, sizeof(line));
// Default values.
memset(pMDM->szDevPath, 0, sizeof(pMDM->szDevPath));
sprintf(pMDM->szDevPath, "COM1");
pMDM->BaudRate = 9600;
pMDM->timeout = 8000;
pMDM->threadperiod = 100;
pMDM->bSaveRawData = 1;
pMDM->MessageLen = 4;
pMDM->InternalBufferSize = 256;
pMDM->ModemBitRate = 40;
pMDM->DelayReadWriteEchoByte = 20;
// Load data from a file.
file = fopen(szCfgFilePath, "r");
if (file != NULL)
{
if (fgets3(file, line, sizeof(line)) == NULL) printf("Invalid configuration file.\n");
if (sscanf(line, "%255s", pMDM->szDevPath) != 1) printf("Invalid configuration file.\n");
if (fgets3(file, line, sizeof(line)) == NULL) printf("Invalid configuration file.\n");
if (sscanf(line, "%d", &pMDM->BaudRate) != 1) printf("Invalid configuration file.\n");
if (fgets3(file, line, sizeof(line)) == NULL) printf("Invalid configuration file.\n");
if (sscanf(line, "%d", &pMDM->timeout) != 1) printf("Invalid configuration file.\n");
if (fgets3(file, line, sizeof(line)) == NULL) printf("Invalid configuration file.\n");
if (sscanf(line, "%d", &pMDM->threadperiod) != 1) printf("Invalid configuration file.\n");
if (fgets3(file, line, sizeof(line)) == NULL) printf("Invalid configuration file.\n");
if (sscanf(line, "%d", &pMDM->bSaveRawData) != 1) printf("Invalid configuration file.\n");
if (fgets3(file, line, sizeof(line)) == NULL) printf("Invalid configuration file.\n");
if (sscanf(line, "%d", &pMDM->MessageLen) != 1) printf("Invalid configuration file.\n");
if (fgets3(file, line, sizeof(line)) == NULL) printf("Invalid configuration file.\n");
if (sscanf(line, "%d", &pMDM->InternalBufferSize) != 1) printf("Invalid configuration file.\n");
if (fgets3(file, line, sizeof(line)) == NULL) printf("Invalid configuration file.\n");
if (sscanf(line, "%d", &pMDM->ModemBitRate) != 1) printf("Invalid configuration file.\n");
if (fgets3(file, line, sizeof(line)) == NULL) printf("Invalid configuration file.\n");
if (sscanf(line, "%d", &pMDM->DelayReadWriteEchoByte) != 1) printf("Invalid configuration file.\n");
if (fclose(file) != EXIT_SUCCESS) printf("fclose() failed.\n");
}
else
{
printf("Configuration file not found.\n");
}
}
if (pMDM->threadperiod < 0)
{
printf("Invalid parameter : threadperiod.\n");
pMDM->threadperiod = 100;
}
// Used to save raw data, should be handled specifically...
//pMDM->pfSaveFile = NULL;
if (OpenRS232Port(&pMDM->RS232Port, pMDM->szDevPath) != EXIT_SUCCESS)
{
printf("Unable to connect to a MDM.\n");
return EXIT_FAILURE;
}
if (SetOptionsRS232Port(&pMDM->RS232Port, pMDM->BaudRate, NOPARITY, FALSE, 8,
ONESTOPBIT, (UINT)pMDM->timeout) != EXIT_SUCCESS)
{
printf("Unable to connect to a MDM.\n");
CloseRS232Port(&pMDM->RS232Port);
return EXIT_FAILURE;
}
printf("MDM connected.\n");
return EXIT_SUCCESS;
}
inline int DisconnectMDM(MDM* pMDM)
{
if (CloseRS232Port(&pMDM->RS232Port) != EXIT_SUCCESS)
{
printf("MDM disconnection failed.\n");
return EXIT_FAILURE;
}
printf("MDM disconnected.\n");
return EXIT_SUCCESS;
}
#ifndef DISABLE_MDMTHREAD
THREAD_PROC_RETURN_VALUE MDMThread(void* pParam);
#endif // !DISABLE_MDMTHREAD
#endif // !MDM_H