-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathinterfaceServer.cpp
More file actions
986 lines (891 loc) · 62.6 KB
/
interfaceServer.cpp
File metadata and controls
986 lines (891 loc) · 62.6 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
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
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
/** A TCP server for various configuration and teletext input interfaces
A network interface for the injection of databroadcast packets, inserter configuration, and
page configuration, for multiple simultaneous clients.
This is a binary message interface where the first byte of any message holds the message
length, and the second byte contains either a command number (for messages to the server) or an
error code (for server replies). A client should transmit a command and then read the server's
response to get the error state and any returned data.
The command numbers and error codes are defined in interfaceServer.h
The client must select which channel subsequent commands relate to using the SETCHAN command.
Databroadcast packet commands must be sent to any of channels 1-15 (corresponding to the packet
addresses 1/30 to 7/31), and only a single client may select each of these datacast channels at
one time.
Channel 0 is used for non databroadcast commands and may have multiple simultaneous users.
The server currently implements:
* A databroadcast interface, which can encode IDL format A datacast, or inject raw packet data.
* A configuration interface, which can set and retrieve certain vbit2 configuration options.
* A page data API, capable of creating and deleting pages, modifying settings and row data.
*/
#include "interfaceServer.h"
using namespace vbit;
InterfaceServer::InterfaceServer(Configure *configure, Debug *debug, PageList *pageList) :
_configure(configure),
_debug(debug),
_pageList(pageList),
_portNumber(configure->GetInterfaceServerPort()),
_isActive(false)
{
/* initialise sockets */
_serverSock = -1;
_datachannel[0]=nullptr; // do not create PacketDatacast for reserved data channel 0
for (int dc=1; dc<16; dc++)
{
_datachannel[dc] = new PacketDatacast(dc, configure); // initialise remaining 15 datacast channels
}
}
InterfaceServer::~InterfaceServer()
{
}
void InterfaceServer::SocketError(std::string errorMessage)
{
if (_serverSock >= 0)
{
#ifdef WIN32
closesocket(_serverSock);
#else
close(_serverSock);
#endif
}
for (int i = 0; i < MAXCLIENTS; i++)
{
if (_clientState[i].socket >= 0)
CloseClient(&_clientState[i]);
}
_debug->Log(Debug::LogLevels::logERROR,errorMessage);
}
void InterfaceServer::CloseClient(ClientState *client)
{
// close socket
#ifdef WIN32
closesocket(client->socket);
#else
close(client->socket);
#endif
if (client->page)
{
client->page->FreeLock(); // unlock page
client->page = nullptr;
}
client->channel = -1;
client->subpage = nullptr;
client->socket = -1; // mark this slot free for new connections
}
void InterfaceServer::run()
{
_debug->Log(Debug::LogLevels::logDEBUG,"[InterfaceServer::run] Datacast server thread started");
int newSock;
int sock;
struct sockaddr_in address;
char readBuffer[256];
fd_set readfds;
#ifdef WIN32
int addrlen;
WSADATA wsaData;
int iResult;
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != 0)
{
SocketError("[InterfaceServer::run] WSAStartup failed\n");
return;
}
#else
unsigned int addrlen;
#endif
/* Create socket */
if ((_serverSock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
{
SocketError("[InterfaceServer::run] socket() failed\n");
return;
}
int reuse = true;
/* Allow multiple connnections */
if(setsockopt(_serverSock, SOL_SOCKET, SO_REUSEADDR, (const char *)&reuse, sizeof(reuse)) < 0)
{
SocketError("[InterfaceServer::run] setsockopt() SO_REUSEADDR failed\n");
return;
}
address.sin_family = AF_INET;
address.sin_addr.s_addr = htonl(INADDR_ANY);
address.sin_port = htons(_portNumber);
/* bind socked */
if (bind(_serverSock, (struct sockaddr *) &address, sizeof(address)) < 0)
{
SocketError("[InterfaceServer::run] bind() failed\n");
return;
}
/* Listen for incoming connections */
if (listen(_serverSock, MAXPENDING) < 0)
{
SocketError("[InterfaceServer::run] listen() failed\n");
return;
}
addrlen = sizeof(address);
while(true)
{
FD_ZERO(&readfds);
FD_SET(_serverSock, &readfds);
bool active = false;
for (int i = 0; i < MAXCLIENTS; i++)
{
sock = _clientState[i].socket;
if(sock >= 0){
FD_SET(sock , &readfds);
active = true; /* packet server has connections */
}
}
_isActive = active;
/* wait for activity on any socket */
if ((select(FD_SETSIZE, &readfds, NULL, NULL, NULL) < 0) && (errno!=EINTR))
SocketError("[InterfaceServer::run] select() failed");
if (FD_ISSET(_serverSock, &readfds))
{
/* incoming connection to server */
if ((newSock = accept(_serverSock, (struct sockaddr *)&address, &addrlen))<0)
{
SocketError("[InterfaceServer::run] accept() failed");
return;
}
#ifdef WIN32
u_long ul = 1;
if (ioctlsocket(newSock, FIONBIO, &ul) < 0)
{
SocketError("[InterfaceServer::run] ioctlsocket() failed");
return;
}
#else
if (fcntl(newSock, F_SETFL, fcntl(newSock, F_GETFL, 0) | O_NONBLOCK) < 0)
{
SocketError("[InterfaceServer::run] fcntl() failed");
return;
}
#endif
for (int i = 0; i <= MAXCLIENTS; i++)
{
if (i == MAXCLIENTS)
{
/* no more client slots so reject */
#ifdef WIN32
closesocket(newSock);
#else
close(newSock);
#endif
_debug->Log(Debug::LogLevels::logWARN,"[InterfaceServer::run] reject new connection from " + std::string(inet_ntoa(address.sin_addr)) + " (too many connections)");
break;
}
/* find unused slot */
if( _clientState[i].socket < 0 )
{
/* add to active sockets */
_clientState[i].socket = newSock;
_clientState[i].channel = -1; // no channel set
_debug->Log(Debug::LogLevels::logINFO,"[InterfaceServer::run] new connection from " + std::string(inet_ntoa(address.sin_addr)) + ":" + std::to_string(ntohs(address.sin_port)) + " as socket " + std::to_string(newSock));
break;
}
}
}
else
{
/* a client socket has activity */
for (int i = 0; i < MAXCLIENTS; i++)
{
sock = _clientState[i].socket;
if (sock >= 0 && FD_ISSET(sock , &readfds))
{
/* socket has activity */
int n = recv(sock, readBuffer, 1, MSG_PEEK); // peek at first byte of message
if (n == 1)
{
// byte 0 of message is message length
int len = (uint8_t)readBuffer[0];
if (len)
{
n = recv(sock, readBuffer, len, 0); // try to read whole message
if (n == len)
{
std::vector<uint8_t> res = {CMDOK}; // create "OK" response
// byte 1 of message is interface server command number
switch ((uint8_t)readBuffer[1]){
case SETCHAN: // set interface channel
{
int ch = (uint8_t)readBuffer[2];
if (n == 3 && ch >= 0 && ch <= 15)
{
_clientState[i].channel = -1; // release current channel
if (ch) // allows multiple connections for channel 0
{
// check if another client is using desired datachannel
for (int j=0; j<MAXCLIENTS; j++){
if (_clientState[j].channel == ch)
{
res[0] = CMDBUSY;
goto SETCHANError; // jump out to return error
}
}
}
_clientState[i].channel = ch; // use requested channel
std::stringstream ss;
ss << "[InterfaceServer::run] Client " << i << ": SETCHAN " << ch;
_debug->Log(Debug::LogLevels::logDEBUG,ss.str());
break;
}
res[0] = CMDERR;
SETCHANError:
break;
}
case GETAPIVER:
{
/* get API version number */
if (n == 2)
{
res.push_back(APIVERSION[0]); // major version
res.push_back(APIVERSION[1]); // minor version
res.push_back(APIVERSION[2]); // patch
}
else
res[0] = CMDERR;
break;
}
case DBCASTAPI:
{
/* databroadcast API */
if (_clientState[i].channel > 0 && n > 2) // databroadcast commands only on channels 1-15
{
switch((uint8_t)readBuffer[2]) // byte 2 is databroadcast command number
{
case DCRAW: // push raw datacast packet to buffer
{
if (n == 43) // 40 bytes of packet data
{
std::vector<uint8_t> data(readBuffer+3, readBuffer+n);
if(_datachannel[_clientState[i].channel]->PushRaw(&data))
{
res[0] = CMDBUSY; // buffer full
}
}
else
{
res[0] = CMDERR;
}
break;
}
case DCFORMATA: // encode and push a format A datacast payload to buffer
{
/* Does _not_ automate repeats, continuity indicator, etc.
Format is:
byte 3: bits 4-7 IAL, bits 1-3 flags RI,CI,DL
byte 4-6: 24 bit Service Packet Address (little endian)
byte 7: Repeat indicator
byte 8: Continuity indicator
byte 9+: payload data
*/
if (n > 9)
{
uint8_t flags = (uint8_t)readBuffer[3] & 0xe;
uint8_t ial = (uint8_t)readBuffer[3] >> 4;
uint32_t spa = (uint8_t)readBuffer[4] | ((uint8_t)readBuffer[5] << 8) | ((uint8_t)readBuffer[6] << 16);
uint8_t ri = (uint8_t)readBuffer[7];
uint8_t ci = (uint8_t)readBuffer[8];
std::vector<uint8_t> data(readBuffer+9, readBuffer+n);
int bytes = _datachannel[_clientState[i].channel]->PushIDLA(flags, ial, spa, ri, ci, &data);
if (bytes == 0) // buffer full
res[0] = CMDBUSY;
else if (bytes < n-9) // payload didn't fit
res[0] = CMDTRUNC; // warn of truncation
res.push_back(bytes); // return number of bytes written
break;
}
else
{
res[0] = CMDERR;
res.push_back(0); // no bytes written
}
break;
}
default: // unknown datacast command
res[0] = CMDERR;
}
}
else
{
res[0] = CMDERR;
}
break;
}
case CONFIGAPI:
{
/* vbit2 configuration API */
if (_clientState[i].channel == 0 && n > 2) // allow VBIT2 configuration commands on channel 0 only
{
switch((uint8_t)readBuffer[2]){ // byte 2 is configuration command number
case CONFRAFLAG: /* get/set row adaptive flag */
{
if (n == 4)
{
_configure->SetRowAdaptive((uint8_t)readBuffer[3]&1);
std::stringstream ss;
ss << "[InterfaceServer::run] Client " << i << ": CONFRAFLAG " << ((readBuffer[3] & 1)?"ON":"OFF");
_debug->Log(Debug::LogLevels::logDEBUG,ss.str());
}
else if (n != 3)
{
res[0] = CMDERR;
}
res.push_back(_configure->GetRowAdaptive()?1:0);
break;
}
case CONFRBYTES: /* get/set BSDP reserved bytes */
{
if (n == 7) // set new bytes
{
_configure->SetReservedBytes(std::array<uint8_t, 4>({(uint8_t)readBuffer[3],(uint8_t)readBuffer[4],(uint8_t)readBuffer[5],(uint8_t)readBuffer[6]}));
std::stringstream ss;
ss << "[InterfaceServer::run] Client " << i << ": CONFRBYTES set";
_debug->Log(Debug::LogLevels::logDEBUG,ss.str());
}
else if (n != 3)
{
res[0] = CMDERR;
}
std::array<uint8_t, 4> bytes = _configure->GetReservedBytes();
res.push_back(bytes[0]);
res.push_back(bytes[1]);
res.push_back(bytes[2]);
res.push_back(bytes[3]); // read back reserved bytes
break;
}
case CONFSTATUS: /* get/set BSDP status message */
{
if (n == 23) // set new status
{
std::ostringstream tmp;
for (int i = 3; i < 23; i++)
{
tmp << (char)(readBuffer[i] & 0x7f);
}
_configure->SetServiceStatusString(tmp.str());
std::stringstream ss;
ss << "[InterfaceServer::run] Client " << i << ": CONFSTATUS set";
_debug->Log(Debug::LogLevels::logDEBUG,ss.str());
}
else if (n != 3)
{
res[0] = CMDERR;
}
for(char& c : _configure->GetServiceStatusString()) {
res.push_back((uint8_t)c & 0x7f);
}
break;
}
case CONFHEADER: /* get/set header template */
{
if (n == 35 || n == 36) // set new header template
{
std::array<uint8_t, 40> tmp;
for (int i = 0; i < 32; i++)
tmp[8+i] = readBuffer[(n-32)+i];
std::shared_ptr<TTXLine> line(new TTXLine(tmp));
std::stringstream ss;
if (n==35)
{
_configure->SetHeaderTemplate(line);
ss << "[InterfaceServer::run] Client " << i << ": CONFHEADER set";
}
else
{
uint8_t mag = (uint8_t)readBuffer[3] & 0x7;
_pageList->GetMagazines()[mag]->SetCustomHeader(line);
ss << "[InterfaceServer::run] Client " << i << ": CONFHEADER set on magazine " << (int)mag;
}
_debug->Log(Debug::LogLevels::logDEBUG,ss.str());
}
else if (n == 3)
{
for(char& c : _configure->GetHeaderTemplate()) {
res.push_back((uint8_t)c);
}
}
else if (n == 4)
{
uint8_t mag = (uint8_t)readBuffer[3];
if (mag & 0x80)
{
// delete flag
_pageList->GetMagazines()[mag & 0x7]->DeleteCustomHeader();
}
else
{
std::string tmp = _pageList->GetMagazines()[mag & 0x7]->GetCustomHeader();
if (tmp.length() == 32)
{
for(char& c : tmp) {
res.push_back((uint8_t)c);
}
}
else
{
// no custom header set for magazine
res[0] = CMDNOENT;
}
}
}
else
{
res[0] = CMDERR;
}
break;
}
case CONFENHANC:
{
res[0] = CMDERR; // default to an returning an error unless we have success
if (n > 3)
{
int mag = (uint8_t)readBuffer[3] & 0x1F;
int deleteFlag = (uint8_t)readBuffer[3]&0x80;
if (mag < 8)
{
if (!deleteFlag)
{
if (n == 44)
{
// write row data
std::array<uint8_t, 40> tmp;
for (int i = 0; i < 40; i++)
tmp[i] = (uint8_t)readBuffer[4+i];
std::shared_ptr<TTXLine> line(new TTXLine(tmp));
_pageList->GetMagazines()[mag]->SetPacket29(line);
res[0] = CMDOK;
}
else if (n == 5)
{
// read row data
std::shared_ptr<TTXLine> line = _pageList->GetMagazines()[mag]->GetPacket29();
if (line!=nullptr)
line = line->LocateLine((uint8_t)readBuffer[4]&0xF);
if (line != nullptr)
{
std::array<uint8_t, 40> tmp = line->GetLine();
res.insert (res.end(), tmp.data(), tmp.data()+tmp.size());
res[0] = CMDOK;
}
else
res[0] = CMDNOENT; // row doesn't exist
}
}
else
{
if (n==4)
{
// delete all packets
_pageList->GetMagazines()[mag]->DeletePacket29();
}
else if (n==5)
{
// delete dc
_pageList->GetMagazines()[mag]->DeletePacket29((uint8_t)readBuffer[4]&0xF);
}
res[0] = CMDOK; // didn't check if line existed, just returns OK
}
}
}
break;
}
default: // unknown configuration command
res[0] = CMDERR;
}
}
else
{
res[0] = CMDERR;
}
break;
}
case PAGESAPI:
{
/* page data API */
if (_clientState[i].channel == 0 && n > 2) // allow page data commands on channel 0 only
{
int cmd = (uint8_t)readBuffer[2]; // byte 2 is page data API command number
if (cmd <= PAGEDELSUB) // all commands that start with a page/subpage number
{
if (cmd <= PAGEOPEN && _clientState[i].page)
{
// implicitly close page when issuing other page delete/open commands
_clientState[i].page->FreeLock();
_clientState[i].page = nullptr;
_clientState[i].subpage = nullptr;
}
if (n >= 5)
{
int num = ((uint8_t)readBuffer[3] << 8) | (uint8_t)readBuffer[4];
if (cmd == PAGEDELETE)
{
if (n == 5)
{
std::stringstream ss;
ss << "[InterfaceServer::run] Client " << i << ": PAGEDELETE " << std::hex << num;
_debug->Log(Debug::LogLevels::logDEBUG,ss.str());
std::shared_ptr<TTXPageStream> p = _pageList->Locate(num);
if (p != nullptr)
{
p->SetOneShotFlag(false);
p->MarkForDeletion();
}
else
{
res[0] = CMDNOENT;
}
}
else
res[0] = CMDERR;
}
else if (cmd == PAGEOPEN)
{
bool OneShot = false;
if (n > 5)
OneShot = (readBuffer[5] & 1);
if (n < 7)
{
std::stringstream ss;
ss << "[InterfaceServer::run] Client " << i << ": PAGEOPEN " << std::hex << num << (OneShot?" as OneShot":"");
_debug->Log(Debug::LogLevels::logDEBUG,ss.str());
if ((uint8_t)readBuffer[3] > 0 && (uint8_t)readBuffer[3] <= 8 && (uint8_t)readBuffer[4] < 0xff)
{
std::shared_ptr<TTXPageStream> p = _pageList->Locate(num);
if (p == nullptr || p->GetIsMarked())
{
p = std::shared_ptr<TTXPageStream>(new TTXPageStream()); // create new page
std::stringstream ss;
ss << "[InterfaceServer::run] Created new page " << std::hex << num;
_debug->Log(Debug::LogLevels::logINFO,ss.str());
if (p->GetLock()) // if this fails we have a real problem!
{
p->SetPageNumber(num);
p->SetOneShotFlag(OneShot);
_pageList->AddPage(p, true); // put it in the page lists
// at this stage it has no subpages!
_clientState[i].page = p;
}
else
{
res[0] = CMDBUSY;
}
}
else
{
res[0] = CMDBUSY; // overwritten if successful
if (p->GetOneShotFlag() && p->GetUpdatedFlag())
{
// previous oneshot hasn't yet sent
}
else if (p->GetLock()) // try to lock page
{
if (OneShot || (p->GetOneShotFlag() != OneShot)) // oneshot or oneshot changed
{
p->SetOneShotFlag(OneShot);
_pageList->UpdatePageLists(p);
}
_clientState[i].page = p;
res[0] = CMDOK;
}
}
}
}
else
res[0] = CMDERR;
}
else if (cmd == PAGESETSUB || cmd == PAGEDELSUB)
{
_clientState[i].subpage = nullptr; // invalidate previous subpage
if (n == 5 && _clientState[i].page)
{
std::stringstream ss;
ss << "[InterfaceServer::run] Client " << i << ": " << ((cmd==PAGESETSUB)?"PAGESETSUB ":"PAGEDELSUB ") << std::hex << num;
_debug->Log(Debug::LogLevels::logDEBUG,ss.str());
if ((num & 0xc080) || num >= 0x3f7f) // reject invalid subpage numbers
{
res[0] = CMDERR;
}
else
{
_clientState[i].subpage = _clientState[i].page->LocateSubpage(num);
if (_clientState[i].subpage == nullptr) // subpage not found
{
if (cmd == PAGESETSUB)
{
_clientState[i].subpage = std::shared_ptr<Subpage>(new Subpage()); // create new subpage
_clientState[i].subpage->SetSubCode(num); // set subcode first
_clientState[i].page->InsertSubpage(_clientState[i].subpage); // add to page
_pageList->UpdatePageLists(_clientState[i].page);
_clientState[i].subpage->SetSubpageStatus(PAGESTATUS_TRANSMITPAGE);
if (_clientState[i].page->GetOneShotFlag()) // page is a oneshot
_clientState[i].page->SetSubpage(num); // put this subpage on air
}
else // PAGEDELSUB
{
res[0] = CMDNOENT;
}
}
else
{
if (cmd == PAGESETSUB)
{
if (_clientState[i].page->GetOneShotFlag()) // page is a oneshot
_clientState[i].page->SetSubpage(num); // put this subpage on air
}
else // PAGEDELSUB
{
_clientState[i].page->RemoveSubpage(_clientState[i].subpage);
}
}
unsigned int count = _clientState[i].page->GetSubpageCount();
res.push_back((count >> 8) & 0xff);
res.push_back(count & 0xff); // return subpage count (big endian)
}
}
else
res[0] = CMDERR;
}
}
else
res[0] = CMDERR;
}
else if (cmd == PAGECLOSE)
{
std::stringstream ss;
ss << "[InterfaceServer::run] Client " << i << ": PAGECLOSE";
_debug->Log(Debug::LogLevels::logDEBUG,ss.str());
if (n==3)
{
if (_clientState[i].page)
_clientState[i].page->FreeLock();
else
res[0] = CMDNOENT;
_clientState[i].page = nullptr;
_clientState[i].subpage = nullptr;
}
else
{
res[0] = CMDERR;
}
}
else if (cmd == PAGEFANDC)
{
if (_clientState[i].page)
{
std::stringstream ss;
ss << "[InterfaceServer::run] Client " << i << ": PAGEFANDC";
_debug->Log(Debug::LogLevels::logDEBUG,ss.str());
if (n == 5) // write
{
_clientState[i].page->SetPageFunctionInt((uint8_t)readBuffer[3]);
_clientState[i].page->SetPageCodingInt((uint8_t)readBuffer[4]);
_pageList->UpdatePageLists(_clientState[i].page);
}
else if (n != 3)
{
res[0] = CMDERR;
}
res.push_back(_clientState[i].page->GetPageFunction());
res.push_back(_clientState[i].page->GetPageCoding());
}
else
{
res[0] = CMDERR;
}
}
else if (cmd == PAGEOPTNS)
{
if (_clientState[i].subpage)
{
std::stringstream ss;
ss << "[InterfaceServer::run] Client " << i << ": PAGEOPTNS";
_debug->Log(Debug::LogLevels::logDEBUG,ss.str());
if (n == 8) // write
{
_clientState[i].subpage->SetSubpageStatus(((uint8_t)readBuffer[3] << 8) | (uint8_t)readBuffer[4]);
_clientState[i].subpage->SetRegion((uint8_t)readBuffer[5]);
_clientState[i].subpage->SetCycleTime((uint8_t)readBuffer[6]);
_clientState[i].subpage->SetTimedMode((uint8_t)readBuffer[7] & 1);
}
else if (n != 3)
{
res[0] = CMDERR;
}
uint16_t status = _clientState[i].subpage->GetSubpageStatus();
res.push_back((status >> 8) & 0xff);
res.push_back(status & 0xff);
res.push_back(_clientState[i].subpage->GetRegion());
res.push_back(_clientState[i].subpage->GetCycleTime());
res.push_back(_clientState[i].subpage->GetTimedMode()?1:0);
}
else
{
res[0] = CMDERR;
}
}
else if (cmd == PAGEROW)
{
res[0] = CMDERR; // default to an returning an error unless we have success
if (_clientState[i].subpage)
{
std::stringstream ss;
ss << "[InterfaceServer::run] Client " << i << ": PAGEROW";
_debug->Log(Debug::LogLevels::logDEBUG,ss.str());
if (n > 3)
{
int num = (uint8_t)readBuffer[3] & 0x1F;
int deleteFlag = (uint8_t)readBuffer[3]&0x80;
if (num > 0 && num < 29)
{
if (!deleteFlag)
{
if (n == 44)
{
// write row data
std::array<uint8_t, 40> tmp;
for (int i = 0; i < 40; i++)
tmp[i] = (uint8_t)readBuffer[4+i];
std::shared_ptr<TTXLine> line(new TTXLine(tmp));
_clientState[i].subpage->SetRow(num, line);
res[0] = CMDOK;
}
else if ((num < 26 && n==4) || (num > 25 && n==5))
{
// read row data
std::shared_ptr<TTXLine> line = _clientState[i].subpage->GetRow(num);
if (n==5 && line!=nullptr)
line = line->LocateLine((uint8_t)readBuffer[4]&0xF);
if (line != nullptr)
{
std::array<uint8_t, 40> tmp = line->GetLine();
res.insert (res.end(), tmp.data(), tmp.data()+tmp.size());
res[0] = CMDOK;
}
else
res[0] = CMDNOENT; // row doesn't exist
}
}
else
{
if (n==4)
{
// delete row data
_clientState[i].subpage->DeleteRow(num);
}
else if (num > 25 && n==5)
{
// delete dc
_clientState[i].subpage->DeleteRow(num, (uint8_t)readBuffer[4]&0xF);
}
res[0] = CMDOK; // didn't check if line existed, just returns OK
}
}
}
}
}
else if (cmd == PAGELINKS)
{
if (_clientState[i].subpage)
{
std::stringstream ss;
ss << "[InterfaceServer::run] Client " << i << ": PAGELINKS";
_debug->Log(Debug::LogLevels::logDEBUG,ss.str());
std::array<FastextLink, 6> links;
if (n == 15 || n == 27)
{
for (int l=0; l<6; l++)
{
links[l].page = (((uint8_t)readBuffer[3+(l*2)] & 0x7) << 8) | (uint8_t)readBuffer[4+(l*2)];
if (n == 27)
{
links[l].subpage = (((uint8_t)readBuffer[15+(l*2)] << 8) | (uint8_t)readBuffer[16+(l*2)]) & 0x3f7f;
}
else
{
links[l].subpage = 0x3f7f;
}
}
_clientState[i].subpage->SetFastext(links);
}
else if (n != 3)
{
res[0] = CMDERR;
}
if (res[0] == CMDOK)
{
if (_clientState[i].subpage->GetFastext(&links))
{
for (int l = 0; l < 6; l++)
{
res.push_back((links[l].page >> 8) & 7);
res.push_back(links[l].page & 0xff);
}
for (int l = 0; l < 6; l++)
{
res.push_back((links[l].subpage >> 8) & 0x3f);
res.push_back(links[l].subpage & 0x7f);
}
}
else
{
res[0] = CMDNOENT;
}
}
}
else
{
res[0] = CMDERR;
}
}
else if (cmd > PAGELINKS) // last defined command number
{
std::stringstream ss;
ss << "[InterfaceServer::run] Client " << i << ": Unknown PAGESAPI command received " << std::hex << cmd;
_debug->Log(Debug::LogLevels::logDEBUG,ss.str());
}
}
break;
}
default: // unknown command
{
res[0] = CMDERR;
break;
}
}
if (res.size() > 254)
{
_debug->Log(Debug::LogLevels::logERROR,"[InterfaceServer::run] Response too long");
res.resize(254); // truncate!
}
res.insert(res.begin(), res.size()+1); // prepend message size
unsigned int n = send(sock, (char*)res.data(), res.size(), 0); // send response
if (n == res.size()) // fail if only partial response can be sent
continue; // next socket in loop
}
// else fall through to error handling
}
}
// couldn't read/write all bytes
getpeername(sock, (struct sockaddr*)&address, &addrlen);
if (n == 0)
{
/* client disconnected */
_debug->Log(Debug::LogLevels::logINFO,"[InterfaceServer::run] closing connection from " + std::string(inet_ntoa(address.sin_addr)) + ":" + std::to_string(ntohs(address.sin_port)) + " on socket " + std::to_string(sock));
}
else
{
#ifdef WIN32
int e = WSAGetLastError();
#else
int e = errno;
#endif
_debug->Log(Debug::LogLevels::logWARN,"[InterfaceServer::run] closing connection from " + std::string(inet_ntoa(address.sin_addr)) + ":" + std::to_string(ntohs(address.sin_port)) + " recv error " + std::to_string(e) + " on socket " + std::to_string(sock));
}
/* close the socket when any error occurs */
CloseClient(&_clientState[i]);
}
}
}
}
}