-
Notifications
You must be signed in to change notification settings - Fork 0
/
mag.c
770 lines (714 loc) · 23.4 KB
/
mag.c
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
768
769
770
/** ***************************************************************************
* Description : VBIT: Magazine page to packet converter.
* The Pages folder is scanned for pages belonging to a mag.
* This list of pages is used to sequence packets for this mag.
* There are eight instances of this thread, one per mag.
*
* The main problem with this code is that it doesn't manage
* transmission rules properly and decoders might miss a line or two
* if the header goes out on the same field.
*
* Compiler : GCC
*
* Copyright (C) 2013-2015, Peter Kwan
*
* Permission to use, copy, modify, and distribute this software
* and its documentation for any purpose and without fee is hereby
* granted, provided that the above copyright notice appear in all
* copies and that both that the copyright notice and this
* permission notice and warranty disclaimer appear in supporting
* documentation, and that the name of the author not be used in
* advertising or publicity pertaining to distribution of the
* software without specific, written prior permission.
*
* The author disclaims all warranties with regard to this
* software, including all implied warranties of merchantability
* and fitness. In no event shall the author be liable for any
* special, indirect or consequential damages or any damages
* whatsoever resulting from loss of use, data or profits, whether
* in an action of contract, negligence or other tortious action,
* arising out of or in connection with the use or performance of
* this software.
****************************************************************************/
/** Mag thread
* Each magazine operates in its own thread.
* Each mag knows what mag number it is.
* Each mag has a state. IDLE/HEADER/ROW to help sequence packets
* Each mag buffers about 50 packets ahead.
*/
// #define _DEBUG_
#ifdef WIN32
#include "strcasestr.h"
#endif
#include "mag.h"
// Number of packets in a magazine buffer. 20 is an arbitrary number
#define PACKETCOUNT 20
// MAXCAROUSEL is an arbitrary number, the maximum number of carousels per magazine
// 16 is a good value. Should not have too many carousels as it slows the main service.
// but increase this if you need more.
#define MAXCAROUSEL 32
int r1=0; // Not actually used, just a dummy arg.
// uint8_t thismag;
bufferpacket magBuffer[9]; // One buffer control block for each magazine (plus 1 for out-of-sequence packets like subtitles)
// The actual packet storage
uint8_t magPacket[9][PACKETCOUNT][PACKETSIZE]; // 9 threads, 20 packets, 45 bytes per packet
static pthread_t magThread[8];
static uint8_t magCount=1; // Ensure that each thread has a different mag number
char pagesPath[MAXPATH]="/home/pi/Pages/"; // location of tti files. Set a default of ./pages/
// Carousel stuff
typedef struct _CAROUSEL_
{
PAGE *page; /// Page meta data
time_t time; /// System time of the next transmission
uint32_t subcode; /// Single pages tend to set this 0. Carousels start with 1
} CAROUSEL;
/** addCarousel
* Adds a carousel page p to the carousel list c.
* \param c : A carousel list
* \param p : A page meta data object
* \return 0 OK, 1 Fail
*/
uint8_t addCarousel(CAROUSEL *c,PAGE *p)
{
uint8_t i,found,foundindex; // found an empty cell
found=0;
foundindex=0;
for (i=0;i<MAXCAROUSEL;i++)
{
// Not sure why this doesn't work, or if we actually need it
if (c[i].page==p)
{
// printf("[addCarousel]Oh dear, we found a duplicate. Now what? %ul ",(unsigned int)p); // Ignore it and hope it goes away
return 1;
}
if (c[i].page==NULL && !found) // record the first empty slot found
{
found=1;
foundindex=i;
// printf("[addCarousel] Found = %d\n",foundindex);
}
}
if (!found) // No available slots left
{
// printf("[addCarousel]Can't add carousel. Sorry\n"); // oh no
return 1;
}
// We found an empty slot. Lets go fill it
// printf("[addCarousel]Carousel \"%s\" (%d%02x), SC=%d in %d\n",p->filename,p->mag,p->page,p->subcode,foundindex);
// printf("[addCarousel] p=%u\n",(unsigned int)p);
c[foundindex].page=p;
c[foundindex].subcode=0; // Start from a sensible place
c[foundindex].time=time(NULL);
return 0;
}
/** pageToTransmit - Find the next carousel page update
* \param c : The carousels array
* \param fp : A file pointer. If there is a carousel, the file pointer will be set to the required page
* \param page : The page object for the selected page
* \return Time when the carousel page changes. Or if 0, there is no page.
*/
time_t pageToTransmit(CAROUSEL *c, FILE** fp, PAGE *page)
{
const uint16_t MAXLINE=200;
time_t t;
time_t retval=0;
uint8_t i;
uint8_t timeInterval=0;
uint32_t sc;
char str[MAXLINE]; // Make this longer than the longest possible description line, because if it is bigger it will mute the page.
//printf("K");
PAGE p;
if (!c[0].page) return 0; // If there are no carousels, exit immediately
ClearPage(&p);
//sprintf(p.filename,"no filename found on mag %d",mag);
// Get the current time
t=time(NULL);
// Which page is ready to go?
for (i=0;i<MAXCAROUSEL;i++) // Check all the carousels
{
if ((c[i].page!=NULL) && c[i].time<t) // Is the page due?
{
// This is all about finding the next subcode and
// moving the file pointer ready to transmit the next page
//printf("Seeking subcode %d \n",c[i].subcode);
//printf("Opening %s \n",c[i].page->filename);
strcpy(p.filename,c[i].page->filename);
*fp=fopen(c[i].page->filename,"r");
if (!*fp)
{
// printf("[pageToTransmit] file open Failed: Placeholder SEVEN str=%s\n",str);
*fp=NULL;
return 0;
}
// Parse until we find a bigger subpage
sc=c[i].subcode; // The existing subcode
while (p.subcode<=sc && !feof(*fp)) // Parse the carousel
{
fgets(str,MAXLINE,*fp);
// printf("[pageToTransmit]Parsing %s",str); // Note: The line already has \n in it
if (ParseLine(&p, str)) // The parse failed
{
// printf("[pageToTransmit] Parse Failed: Placeholder FOUR str=%s fn=%s\n",str,c[i].page->filename);
delay(1000);
}
// Note: A carousel takes timing from the first CT command only
if (p.time && !timeInterval)
{
timeInterval=p.time;
// printf("[pageToTransmit] Mag %d, time interval=%d\n",mag, p.time);
}
}
// timeInterval=p.time;
// At this point we either have the next page or we ran off the end
// printf("[pageToTransmit] page %d %d Entered with %d, next found %d\n",p.mag,p.page,sc,p.subcode);
// If we hit the end of file, we should restart with subcode 1
//printf("T\n");
if (feof(*fp)) // Ran off the end
{
//printf("X\n");
// Loop back to the start of the carousel
// Just reposition the text pointer to the start
if (fseek(*fp,0,0))
{
// printf("[pageToTransmit] Seek failed: Placeholder FIVE str=%s\n",str);
}
// And set the subcode to 1 (first page of a carousel)
c[i].subcode=0; // The next subcode to expect would be 1, but 0 should work too.
// printf("[pageToTransmit] Out of data. Placeholder ONE\n");
}
else
{
//printf("Y\n");
// TODO: This works if SC follows CT otherwise we will have to do a bit more parsing
c[i].subcode=p.subcode;
if (p.time>0)
timeInterval=p.time; // Good
else
{
timeInterval=15; // Error, set sensible default
//printf("[pageToTransmit] Missing time interval. Placeholder SIX\n");
}
//printf("Z\n");
}
// Reschedule this carousel
c[i].time=time(NULL)+timeInterval;
break;
} // page is due
} // for
// Now work out when the next carousel page changes.
retval=0;
for (i=0;i<MAXCAROUSEL;i++)
{
if (c[i].page) // if the page exists
{
if (c[i].time>0) // a valid time
{
// Save the time if we don't have one or it is less than what we have
if (c[i].time<retval || retval==0)
{
retval=c[i].time;
}
}
else
{
// printf("[pageToTransmit] Mag %d, carousel %d, Time is 0. This is bad\n",mag,i);
}
}
}
// printf("Mag %d, Next retval=%d\n",mag,(int)retval);
if (!p.filename[0]) // For some reason got here without a page? (If ClearPage sets a null string)
{
// printf("[pageToTransmit] filename bug that needs fixing\n");
retval=0; // Really need to investigate how this fails. It should never happen.
}
*page=p;
// printf("[PageToTransmit] L filename=%s mag=%d page=%02x, subcode=%d\n",p.filename,p.mag,p.page,p.subcode);
// printf("M retval=%d\n",(int)retval);
return retval;
} // pageToTransmit
/** getMag - Allocate a magazine to a thread.
*/
uint8_t getMag(void)
{
uint8_t num;
piLock(0); // Ensure that each mag is unique
num=magCount;
magCount=(magCount+1)%8;
piUnlock(0);
return num;
}
/** getList - Populate a magazine list
* Declare the list in domag so we use auto variables. So each thread gets its own environment.
*/
uint8_t getList(PAGE **txList,uint8_t mag, CAROUSEL *carousel)
{
DIR *d; // Directory handle
PAGE page;
PAGE *p; // Page that we are going to parse
PAGE *newpage;
char filename[MAXPATH];
struct dirent *dir;
uint8_t i;
// Make sure all the carousel entries are NULL
for (i=0;i<MAXCAROUSEL;i++)
{
carousel[i].page=NULL;
carousel[i].time=0;
carousel[i].subcode=0;
}
d = opendir(pagesPath);
p=&page;
if (d)
{
while ((dir = readdir(d)) != NULL)
{
// TODO: Is it a directory?
// Is it a tti page
if (strcasestr(dir->d_name,".tti") || strcasestr(dir->d_name,".ttix"))
{
/* hopefully assemble a filename without a buffer overflow */
strncpy(filename,pagesPath,MAXPATH-1);
i = filename[(strlen(filename)-1)];
if (i != '/' && i != '\\' && strlen(filename) + 1 < MAXPATH)
strcat(filename, "/"); // append missing trailing slash
i = strlen(filename) + strlen(dir->d_name);
if (i < MAXPATH){
strncat(filename,dir->d_name, i);
}
//printf("stream %d, %s\n", mag, filename);
if (ParsePage(p, filename))
{
//printf("Not a valid page %s\n",filename);
}
//printf("Comparing p->mag %d, p->page %d, mag %d\n",p->mag % 8, p->page, mag);
if ((p->mag % 8)==mag)
{
strcpy(p->filename,filename);
//printf("Accepted mag %d page %s\n",mag, p->filename);
// Create a new page object
newpage=calloc(1,sizeof(PAGE));
// Copy the data
*newpage=*p;
// And drop the pointer into the transmission list
// Check that we don't have a duplicate!!!
if (txList[p->page])
{
//printf("[mag:getList] Page already exists. old=%s, new=%s\n",txList[p->page]->filename,p->filename);
}
// If subcode is greater than 1 we want to save that page as a carousel
if (p->subcode>1)
{
// printf("subcode=%d ",p->subcode);
addCarousel(carousel,newpage);
}
else {
txList[p->page]=newpage; // Store as a normal non carouselling page
}
//printf("[getList]Saved page %s mpp=%01d%02d\n",txList[p->page]->filename,txList[p->page]->mag,txList[p->page]->subpage);
}
}
// TODO: Something wonderful with the PAGE object
}
closedir(d);
}
else
return 1;
return 0;
} // getList
/** domag is the thread that manages a single magazine
* Finds and sorts all the pages for a particular magazine.
* Manages adding and removing pages.
* Maintains a state machine: IDLE, HEADER, ROW
*/
void domag(void)
{
const uint16_t MAXLINE=200;
uint16_t i;
uint8_t txListIndex; // The current page
uint8_t txListStart; // Help prevent an endless loop
PAGE* txList[256]; // One pointer per page. There are 256 possible pages in a magazine
uint8_t mag;
uint8_t row;
uint8_t state;
uint8_t packet[PACKETSIZE];
PAGE* page=NULL;
FILE* fil=NULL;
time_t txwait=0;
uint8_t isCarousel;
PAGE carPage;
int triplet;
// Some working space for manipulating strings
char strtemp[100];
char *tmpptr;
CAROUSEL carousel[MAXCAROUSEL]; // Is 16 enough carousels? If not then change this yourself.
char str[MAXLINE];
// Init the transmission list for this magaine
for (i=0;i<256;i++)
txList[i]=NULL;
// Work out which magazine we are
mag=getMag();
// Find the pages for this mag and put them into the transmission list.
piLock(1);
if (getList(txList,mag,carousel))
{
#ifdef _DEBUG_
fprintf(stderr,"Could not find pages on stream %1d \n",mag);
#endif
piUnlock(1);
return;
}
#ifdef _DEBUG_
for (i=0;i<MAXCAROUSEL;i++)
{
if (carousel[i].page)
{
fprintf(stderr,"[domag] Carousels found on mag %d\n",mag);
}
}
#endif
delay(400); // EVIL HACK masking a race condition, root cause unknown...
piUnlock(1);
// printf("Mag thread is initialised: mag=%d\n",mag);
// Initialise the magazine state
state=STATE_BEGIN;
// Start at page 0
txListIndex=0;
// The mag loop has a page counter that steps through all the pages
// There is a state variable which helps step through the file.
while(1)
{
while (bufferIsFull(&magBuffer[mag])) delay(20); // ms
switch (state)
{
case STATE_BEGIN: // First time only
// do stuff the first time
state=STATE_IDLE;
break;
case STATE_IDLE: // Ready to start a new page
// Find the next page to transmit
isCarousel=0;
// Timed carousel pages have priority
if (txwait<time(NULL)) // If we are due to transmit a carousel
{
txwait=pageToTransmit(carousel,&fil,&carPage);
if (txwait==0)
{
// If we are expecting a carousel but only this happens, then PageToTransmit is broken
txwait=time(NULL)+10;
}
else
{
// If we get here then fp has a valid carousel file
isCarousel=1;
}
}
// If we didn't get a page object from pageToTransmit, we get it from the main list
if (!isCarousel)
{
// Find the next page in the main sequence
txListStart=txListIndex; // Avoid infinite loop if we have no pages in mag, (should go to another idle state if this happens)
txListIndex++;
while(!txList[txListIndex] )
{
txListIndex++; // This will automatically wrap, hence no range checking.
if (txListStart==txListIndex) // oops. This magazine has nothing to show
{
state=STATE_BEGIN;
#ifdef _DEBUG_
fprintf(stderr,"[domag] Magazine %d contains no pages\n",mag);
#endif
delay(1000); // Might as well do nothing most of the time
#ifdef _DEBUG_
fprintf(stderr,"[domag] Delay with no pages. mag=%d\n",mag);
#endif
break;
}
}
// Now we have the found the next page we get ready to transmit it.
page=txList[txListIndex]; // Get the page object
}
else
{
page=&carPage; // The page is a carousel page
}
if (page) // If we found a page to transmit
{
str[0]=0;
if (!fil) // Carousel will already be scanned down to the page that we want
{
fil=fopen(page->filename,"r"); // Open the Page file if it is not a carousel
while (fil && !feof(fil)){ // re-parse the page file to update header
fgets(str,MAXLINE,fil);
ParseLine(page, str); // TODO: there should probably be some error checking here!
if (str[0]=='S' && str[1]=='C') // reached the subcode line
break;
}
}
if (!fil){
// don't try to access a null file pointer (if file got deleted etc.)
state=STATE_IDLE;
break;
}
// printf("[mag]Carousel filename=%s\n",page->filename);
while (strncmp(str,"OL,",3) && !feof(fil)) // scan down to the rows
fgets(str,MAXLINE,fil);
if (feof(fil)) // Not found any lines
{
fclose(fil);
fil=NULL;
state=STATE_IDLE;
}
else // This is the first output line. Parse it and process it.
{
// TX the header
//sprintf(header,"P%01d%02x %s",page->mag,page->page,page->filename);
// Create the header. Note that we force parallel transmission by ensuring that C11 is clear
PacketHeader((char*)packet,page->mag,page->page,page->subcode,page->control & ~0x0040);
// The header packet isn't quite finished. stream.c intercepts headers and adds dynamic elements, page, date, network ID etc.
while (bufferPut(&magBuffer[mag],(char*)packet)==BUFFER_FULL) delay(20);
state=STATE_HEADER;
}
}
else
state=STATE_IDLE; // We found nothing to send
// Change to sending.
break;
case STATE_HEADER: // If regional options are set, send the enhancement packet
/** @todo I suspect that the page enhancements are reversed or something.
* Also the RE command appears to be in hex which probably isn't what we want.
*/
if (page->region>0 && page->region<=0x0f) // Only send this packet if the page asks for it with a non zero RE command
{
// printf("Sending page region=%d\n",page->region);
// We need to send the X/28 packet first (maybe???) so here would be a good place to do it.
PageEnhancementDataPacket((char*) packet, page->mag, 28,0);
triplet=0;
// Lets assemble triple 1 ETSI 300706 page 30. Also see section 9.4.2.1
// 1..4 Page function. We set this to 0 as a standard teletext page.
// 5..7 Page coding. Set this to zero for 7 bit coding.
// 8..14 G0/G2/Nat opt. 10,9,8 should be set to C12, C13,C14 TODO. Bits are in 14..11
triplet+=page->region<<10; // Not reversed
//if (page->region & 0x01) triplet|=0x40; // Reverse the bit order and shift up 7
//if (page->region & 0x02) triplet|=0x20;
//if (page->region & 0x04) triplet|=0x10;
//if (page->region & 0x08) triplet|=0x08;
// 15..18 Second G0
SetTriplet((char*) packet, 1, triplet);
// Should we also set triplets 1 to 13?
for (i=2;i<=13;i++)
SetTriplet((char*) packet, i, 0);
Parity((char*)packet,50); // 50 ensures that we only reverse bytes. Parity would mess up Ham24/8
while(bufferPut(&magBuffer[mag],(char*)packet)==BUFFER_FULL) delay(20);
// dumpPacket(packet);
}
// Now we can process the initial row of the page
row=copyOL((char*)packet,str);
if (row) // If this happens to be OL,0 then don't process packet
{
PacketPrefix((uint8_t*)packet, page->mag, row);
if (row < 26){ // don't mess with parity for packets that would be hammed
Parity((char*)packet,5);
}
//dumpPacket(packet);
while (bufferPut(&magBuffer[mag],(char*)packet)==BUFFER_FULL) delay(20);
}
state=STATE_SENDING; // Intentional fall through
case STATE_SENDING: // Transmitting rows
fgets(str,MAXLINE,fil);
if (str[0]=='O' && str[1]=='L') // Double check it is OL. It could be FL.
{
row=copyOL((char*)packet,str);
if (row) // Only insert a valid row
{
if (row < 26){ // don't do anything to packets > 25
// Temperature: %%%T : 58.4
// Day of week: %%a : Sat
// Day of month:
//
// Special case for system temperature. Put %%%T to get temperature in form tt.t
#ifndef WIN32
tmpptr=strstr((char*)packet,"%%%T");
if (tmpptr) {
get_temp(strtemp);
strncpy(tmpptr,strtemp,4);
}
#endif
// Special case for system time. Put %%%%%%%%%%%%timedate to get time and date
tmpptr=strstr((char*) packet,"%%%%%%%%%%%%timedate");
if (tmpptr) {
get_time(strtemp);
strncpy(tmpptr,strtemp,20);
}
// Special case for world time. Put %t<+|-><hh> to get local time HH:MM offset by +/- half hours
for (;;)
{
tmpptr=strstr((char*) packet,"%t+");
if (!tmpptr) {
tmpptr=strstr((char*) packet,"%t-");
}
if (tmpptr) {
get_offset_time(tmpptr);
}
else
break;
}
#ifndef WIN32
// Special case for network address. Put %%%%%%%%%%%%%%n to get network address in form xxx.yyy.zzz.aaa with trailing spaces (15 characters total)
tmpptr=strstr((char*)packet,"%%%%%%%%%%%%%%n");
if (tmpptr) {
// strncpy(tmpptr,"not yet working",15);
get_net(strtemp);
strncpy(tmpptr,strtemp,15);
}
#endif
// ======= VERSION ========
// %%%V version number eg. 1.00
tmpptr=strstr((char*) packet,"%%%V");
if (tmpptr) {
strncpy(tmpptr,"1.00",4); // @todo: Move this value to somewhere more obvious
}
// Finally fix up the parity
Parity((char*)packet,5);
}
PacketPrefix(packet,page->mag,row);
while (bufferPut(&magBuffer[mag],(char*)packet)==BUFFER_FULL) delay(20);
}
}
if (str[0]=='F' && str[1]=='L') // Fastext links?
{
copyFL((char*)packet,str,page->mag);
PacketPrefix(packet,page->mag,27); // X/27/0
Parity((char*)packet,5);
while (bufferPut(&magBuffer[mag],(char*)packet)==BUFFER_FULL) delay(20);
}
// When we run out of rows to send
if (fil)
{
if (feof(fil) || (str[0]=='S' && str[1]=='C'))
{
state=STATE_IDLE;
fclose(fil); // TODO: Don't try to close already closed!
fil=NULL;
}
break;
}
} // state switch
delay(1); // Just something to break up the sequence
// TODO: We should really intercept a shutdown and release all the memory
} // while
} // domag
/** MagInit creates eight domag threads
* It also sets up the buffers that each thread will use to forward packets.
*/
void magInit(void)
{
int i;
const int maxThreads=8; // should be 8
magCount=1;
for (i=0;i<9;i++) // One extra buffer for Newfor
{
// Set up the buffers, one per thread
bufferInit(&magBuffer[i],(char*)&magPacket[i],PACKETCOUNT);
// now got to add the packet data itself
}
for (i=0;i<maxThreads;i++) {
magThread[(i+1)%maxThreads]=0;
pthread_create(&magThread[(i+1)%maxThreads],NULL,(void*)domag,(void*)&r1); // r1 is just a dummy arg.
// printf("magInit %d done\n",i);
}
} // magInit
#ifndef WIN32
/** get_temp
* Pinched from raspi-teletext demo.c
* @return Four character temperature in degrees C eg. "45.7"
*/
bool get_temp(char* str)
{
FILE *fp;
char *pch;
char tmp[100];
fp = popen("/usr/bin/vcgencmd measure_temp", "r");
fgets(tmp, 99, fp);
pclose(fp);
pch = strtok (tmp,"=\n");
pch = strtok (NULL,"=\n");
strncpy(str,pch,5);
return TRUE; // @todo
}
#endif
/** get_time
* Pinched from raspi-teletext demo.c
* @return Time as 20 characters
*/
bool get_time(char* str)
{
time_t rawtime;
struct tm *info;
time( &rawtime );
info = localtime( &rawtime );
strftime(str, 21, "\x02%a %d %b\x03%H:%M/%S", info);
return TRUE; // @todo
}
/** get_offset_time
* Given a parameter of say %t+02
* where str[2] is + or -
* str[4:3] is a two digit of half hour offsets from local time (GMT/BST in our case)
* @return Local time plus offset as 5 characters in the form 21:30
*/
bool get_offset_time(char* str)
{
char strTime[6];
// get the time (UTC I think)
time_t rawtime;
struct tm *info;
time( &rawtime );
// What is our offset in seconds?
int offset=((str[3]-'0')*10+str[4]-'0')*30*60; // @todo We really ought to validate this
// Is it negative (west of us?)
if (str[2]=='-')
offset=-offset;
else
if (str[2]!='+') return false; // Must be + or -
// Add the offset to the time value
rawtime+=offset;
info = localtime( &rawtime );
strftime(strTime, 21, "%H:%M", info);
strncpy(str,strTime,5);
return TRUE; // @todo
}
#ifndef WIN32
/** get_net
* Pinched from raspi-teletext demo.c
* @return network address as 20 characters
* Sample response
* 3: wlan0 inet 192.168.1.14/24 brd 192.168.1.255 scope global wlan0\ valid_lft forever preferred_lft forever
*/
bool get_net(char* str)
{
FILE *fp;
char *pch;
int n;
char temp[100];
fp = popen("/sbin/ip -o -f inet addr show scope global", "r");
fgets(temp, 99, fp);
pclose(fp);
pch = strtok (temp," \n/");
for (n=1; n<4; n++)
{
pch = strtok (NULL, " \n/");
}
// If we don't have a connection established, try not to crash
if (pch==NULL)
{
strcpy(str,"IP address????");
return FALSE;
}
strncpy(str,pch,15);
return TRUE; // @todo
// Can be up to 15 characters long
//for (i=strlen(temp2);i<15;i++)
// temp2[i]=' ';
//temp2[15]=0;
//return TRUE;
}
#endif