| 1 | /* |
| 2 | * Copyright (C) 2002-2010 The DOSBox Team |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | */ |
| 18 | |
| 19 | /* $Id: softmodem.cpp,v 1.12 2009-10-04 20:57:40 h-a-l-9000 Exp $ */ |
| 20 | |
| 21 | #include "dosbox.h" |
| 22 | |
| 23 | #if C_MODEM |
| 24 | |
| 25 | #include <string.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <ctype.h> |
| 28 | |
| 29 | #include "support.h" |
| 30 | #include "serialport.h" |
| 31 | #include "softmodem.h" |
| 32 | #include "misc_util.h" |
| 33 | |
| 34 | //#include "mixer.h" |
| 35 | |
| 36 | |
| 37 | CSerialModem::CSerialModem(Bitu id, CommandLine* cmd):CSerial(id, cmd) { |
| 38 | InstallationSuccessful=false; |
| 39 | connected=false; |
| 40 | |
| 41 | rqueue=new CFifo(MODEM_BUFFER_QUEUE_SIZE); |
| 42 | tqueue=new CFifo(MODEM_BUFFER_QUEUE_SIZE); |
| 43 | |
| 44 | // Default to direct null modem connection. Telnet mode interprets IAC codes |
| 45 | telnetmode = false; |
| 46 | |
| 47 | // Initialize the sockets and setup the listening port |
| 48 | listenport = 23; |
| 49 | waitingclientsocket=0; |
| 50 | clientsocket = 0; |
| 51 | serversocket = 0; |
| 52 | getBituSubstring("listenport:", &listenport, cmd); |
| 53 | |
| 54 | // TODO: Fix dialtones if requested |
| 55 | //mhd.chan=MIXER_AddChannel((MIXER_MixHandler)this->MODEM_CallBack,8000,"MODEM"); |
| 56 | //MIXER_Enable(mhd.chan,false); |
| 57 | //MIXER_SetMode(mhd.chan,MIXER_16MONO); |
| 58 | |
| 59 | CSerial::Init_Registers(); |
| 60 | Reset(); // reset calls EnterIdleState |
| 61 | |
| 62 | setEvent(SERIAL_POLLING_EVENT,1); |
| 63 | InstallationSuccessful=true; |
| 64 | } |
| 65 | |
| 66 | CSerialModem::~CSerialModem() { |
| 67 | if(serversocket) delete serversocket; |
| 68 | if(clientsocket) delete clientsocket; |
| 69 | if(waitingclientsocket) delete waitingclientsocket; |
| 70 | |
| 71 | delete rqueue; |
| 72 | delete tqueue; |
| 73 | |
| 74 | // remove events |
| 75 | for(Bitu i = SERIAL_BASE_EVENT_COUNT+1; i <= SERIAL_MODEM_EVENT_COUNT; i++) |
| 76 | removeEvent(i); |
| 77 | } |
| 78 | |
| 79 | void CSerialModem::handleUpperEvent(Bit16u type) { |
| 80 | switch (type) { |
| 81 | case SERIAL_RX_EVENT: { |
| 82 | // check for bytes to be sent to port |
| 83 | if(CSerial::CanReceiveByte()) |
| 84 | if(rqueue->inuse() && (CSerial::getRTS()||(flowcontrol!=3))) { |
| 85 | Bit8u rbyte = rqueue->getb(); |
| 86 | //LOG_MSG("Modem: sending byte %2x back to UART3",rbyte); |
| 87 | CSerial::receiveByte(rbyte); |
| 88 | } |
| 89 | if(CSerial::CanReceiveByte()) setEvent(SERIAL_RX_EVENT, bytetime*0.98f); |
| 90 | break; |
| 91 | } |
| 92 | case MODEM_TX_EVENT: { |
| 93 | if (tqueue->left()) { |
| 94 | tqueue->addb(waiting_tx_character); |
| 95 | if (tqueue->left() < 2) { |
| 96 | CSerial::setCTS(false); |
| 97 | } |
| 98 | } else { |
| 99 | static Bits lcount=0; |
| 100 | if (lcount<1000) { |
| 101 | lcount++; |
| 102 | LOG_MSG("MODEM: TX Buffer overflow!"); |
| 103 | } |
| 104 | } |
| 105 | ByteTransmitted(); |
| 106 | break; |
| 107 | } |
| 108 | case SERIAL_POLLING_EVENT: { |
| 109 | if (rqueue->inuse()) { |
| 110 | removeEvent(SERIAL_RX_EVENT); |
| 111 | setEvent(SERIAL_RX_EVENT, (float)0.01); |
| 112 | } |
| 113 | Timer2(); |
| 114 | setEvent(SERIAL_POLLING_EVENT,1); |
| 115 | break; |
| 116 | } |
| 117 | |
| 118 | case MODEM_RING_EVENT: { |
| 119 | break; |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | void CSerialModem::SendLine(const char *line) { |
| 125 | rqueue->addb(0xd); |
| 126 | rqueue->addb(0xa); |
| 127 | rqueue->adds((Bit8u *)line,strlen(line)); |
| 128 | rqueue->addb(0xd); |
| 129 | rqueue->addb(0xa); |
| 130 | } |
| 131 | |
| 132 | // only for numbers < 1000... |
| 133 | void CSerialModem::SendNumber(Bitu val) { |
| 134 | rqueue->addb(0xd); |
| 135 | rqueue->addb(0xa); |
| 136 | |
| 137 | rqueue->addb(val/100+'0'); |
| 138 | val = val%100; |
| 139 | rqueue->addb(val/10+'0'); |
| 140 | val = val%10; |
| 141 | rqueue->addb(val+'0'); |
| 142 | |
| 143 | rqueue->addb(0xd); |
| 144 | rqueue->addb(0xa); |
| 145 | } |
| 146 | |
| 147 | void CSerialModem::SendRes(ResTypes response) { |
| 148 | char const * string;Bitu code; |
| 149 | switch (response) |
| 150 | { |
| 151 | case ResNONE: return; |
| 152 | case ResOK: string="OK"; code=0; break; |
| 153 | case ResERROR: string="ERROR"; code=4; break; |
| 154 | case ResRING: string="RING"; code=2; break; |
| 155 | case ResNODIALTONE: string="NO DIALTONE"; code=6; break; |
| 156 | case ResNOCARRIER: string="NO CARRIER" ;code=3; break; |
| 157 | case ResCONNECT: string="CONNECT 57600"; code=1; break; |
| 158 | } |
| 159 | |
| 160 | if(doresponse!=1) { |
| 161 | if(doresponse==2 && (response==ResRING || |
| 162 | response == ResCONNECT || response==ResNOCARRIER)) return; |
| 163 | if(numericresponse) SendNumber(code); |
| 164 | else SendLine(string); |
| 165 | |
| 166 | //if(CSerial::CanReceiveByte()) // very fast response |
| 167 | // if(rqueue->inuse() && CSerial::getRTS()) |
| 168 | // { Bit8u rbyte =rqueue->getb(); |
| 169 | // CSerial::receiveByte(rbyte); |
| 170 | // LOG_MSG("Modem: sending byte %2x back to UART2",rbyte); |
| 171 | // } |
| 172 | |
| 173 | LOG_MSG("Modem response: %s", string); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | bool CSerialModem::Dial(char * host) { |
| 178 | |
| 179 | // Scan host for port |
| 180 | Bit16u port; |
| 181 | char * hasport=strrchr(host,':'); |
| 182 | if (hasport) { |
| 183 | *hasport++=0; |
| 184 | port=(Bit16u)atoi(hasport); |
| 185 | } |
| 186 | else port=MODEM_DEFAULT_PORT; |
| 187 | // Resolve host we're gonna dial |
| 188 | LOG_MSG("Connecting to host %s port %d",host,port); |
| 189 | clientsocket = new TCPClientSocket(host, port); |
| 190 | if(!clientsocket->isopen) { |
| 191 | delete clientsocket; |
| 192 | clientsocket=0; |
| 193 | LOG_MSG("Failed to connect."); |
| 194 | SendRes(ResNOCARRIER); |
| 195 | EnterIdleState(); |
| 196 | return false; |
| 197 | } else { |
| 198 | EnterConnectedState(); |
| 199 | return true; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | void CSerialModem::AcceptIncomingCall(void) { |
| 204 | if(waitingclientsocket) { |
| 205 | clientsocket=waitingclientsocket; |
| 206 | waitingclientsocket=0; |
| 207 | EnterConnectedState(); |
| 208 | } else { |
| 209 | EnterIdleState(); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | Bitu CSerialModem::ScanNumber(char * & scan) { |
| 214 | Bitu ret=0; |
| 215 | while (char c=*scan) { |
| 216 | if (c>='0' && c<='9') { |
| 217 | ret*=10; |
| 218 | ret+=c-'0'; |
| 219 | scan++; |
| 220 | } else break; |
| 221 | } |
| 222 | return ret; |
| 223 | } |
| 224 | |
| 225 | char CSerialModem::GetChar(char * & scan) { |
| 226 | char ch = *scan; |
| 227 | scan++; |
| 228 | return ch; |
| 229 | } |
| 230 | |
| 231 | void CSerialModem::Reset(){ |
| 232 | EnterIdleState(); |
| 233 | cmdpos = 0; |
| 234 | cmdbuf[0]=0; |
| 235 | oldDTRstate = getDTR(); |
| 236 | flowcontrol = 0; |
| 237 | plusinc = 0; |
| 238 | if(clientsocket) { |
| 239 | delete clientsocket; |
| 240 | clientsocket=0; |
| 241 | } |
| 242 | memset(®,0,sizeof(reg)); |
| 243 | reg[MREG_AUTOANSWER_COUNT]=0; // no autoanswer |
| 244 | reg[MREG_RING_COUNT] = 1; |
| 245 | reg[MREG_ESCAPE_CHAR]='+'; |
| 246 | reg[MREG_CR_CHAR]='\r'; |
| 247 | reg[MREG_LF_CHAR]='\n'; |
| 248 | reg[MREG_BACKSPACE_CHAR]='\b'; |
| 249 | |
| 250 | cmdpause = 0; |
| 251 | echo = true; |
| 252 | doresponse = 0; |
| 253 | numericresponse = false; |
| 254 | |
| 255 | /* Default to direct null modem connection. Telnet mode interprets IAC codes */ |
| 256 | telnetmode = false; |
| 257 | } |
| 258 | |
| 259 | void CSerialModem::EnterIdleState(void){ |
| 260 | connected=false; |
| 261 | ringing=false; |
| 262 | |
| 263 | if(clientsocket) { |
| 264 | delete clientsocket; |
| 265 | clientsocket=0; |
| 266 | } |
| 267 | |
| 268 | if(waitingclientsocket) { // clear current incoming socket |
| 269 | delete waitingclientsocket; |
| 270 | waitingclientsocket=0; |
| 271 | } |
| 272 | // get rid of everything |
| 273 | if(serversocket) { |
| 274 | while(waitingclientsocket=serversocket->Accept()) |
| 275 | delete waitingclientsocket; |
| 276 | } else if (listenport) { |
| 277 | |
| 278 | serversocket=new TCPServerSocket(listenport); |
| 279 | if(!serversocket->isopen) { |
| 280 | LOG_MSG("Serial%d: Modem could not open TCP port %d.",COMNUMBER,listenport); |
| 281 | delete serversocket; |
| 282 | serversocket=0; |
| 283 | } else LOG_MSG("Serial%d: Modem listening on port %d...",COMNUMBER,listenport); |
| 284 | } |
| 285 | waitingclientsocket=0; |
| 286 | |
| 287 | commandmode = true; |
| 288 | CSerial::setCD(false); |
| 289 | CSerial::setRI(false); |
| 290 | CSerial::setDSR(true); |
| 291 | CSerial::setCTS(true); |
| 292 | tqueue->clear(); |
| 293 | } |
| 294 | |
| 295 | void CSerialModem::EnterConnectedState(void) { |
| 296 | if(serversocket) { |
| 297 | // we don't accept further calls |
| 298 | delete serversocket; |
| 299 | serversocket=0; |
| 300 | } |
| 301 | SendRes(ResCONNECT); |
| 302 | commandmode = false; |
| 303 | memset(&telClient, 0, sizeof(telClient)); |
| 304 | connected = true; |
| 305 | ringing = false; |
| 306 | CSerial::setCD(true); |
| 307 | CSerial::setRI(false); |
| 308 | } |
| 309 | |
| 310 | void CSerialModem::DoCommand() { |
| 311 | cmdbuf[cmdpos] = 0; |
| 312 | cmdpos = 0; //Reset for next command |
| 313 | upcase(cmdbuf); |
| 314 | LOG_MSG("Command sent to modem: ->%s<-\n", cmdbuf); |
| 315 | /* Check for empty line, stops dialing and autoanswer */ |
| 316 | if (!cmdbuf[0]) { |
| 317 | reg[0]=0; // autoanswer off |
| 318 | return; |
| 319 | } |
| 320 | //else { |
| 321 | //MIXER_Enable(mhd.chan,false); |
| 322 | // dialing = false; |
| 323 | // SendRes(ResNOCARRIER); |
| 324 | // goto ret_none; |
| 325 | //} |
| 326 | /* AT command set interpretation */ |
| 327 | |
| 328 | if ((cmdbuf[0] != 'A') || (cmdbuf[1] != 'T')) { |
| 329 | SendRes(ResERROR); |
| 330 | return; |
| 331 | } |
| 332 | if (strstr(cmdbuf,"NET0")) { |
| 333 | telnetmode = false; |
| 334 | SendRes(ResOK); |
| 335 | return; |
| 336 | } |
| 337 | else if (strstr(cmdbuf,"NET1")) { |
| 338 | telnetmode = true; |
| 339 | SendRes(ResOK); |
| 340 | return; |
| 341 | } |
| 342 | |
| 343 | char * scanbuf = &cmdbuf[2]; |
| 344 | while (1) { |
| 345 | // LOG_MSG("loopstart ->%s<-",scanbuf); |
| 346 | char chr = GetChar(scanbuf); |
| 347 | switch (chr) { |
| 348 | case 'D': { // Dial |
| 349 | char * foundstr=&scanbuf[0]; |
| 350 | if (*foundstr=='T' || *foundstr=='P') foundstr++; |
| 351 | // Small protection against empty line and long string |
| 352 | if ((!foundstr[0]) || (strlen(foundstr)>100)) { |
| 353 | SendRes(ResERROR); |
| 354 | return; |
| 355 | } |
| 356 | char* helper; |
| 357 | // scan for and remove spaces; weird bug: with leading spaces in the string, |
| 358 | // SDLNet_ResolveHost will return no error but not work anyway (win) |
| 359 | while(foundstr[0]==' ') foundstr++; |
| 360 | helper=foundstr; |
| 361 | helper+=strlen(foundstr); |
| 362 | while(helper[0]==' ') { |
| 363 | helper[0]=0; |
| 364 | helper--; |
| 365 | } |
| 366 | if (strlen(foundstr) >= 12) { |
| 367 | // Check if supplied parameter only consists of digits |
| 368 | bool isNum = true; |
| 369 | for (Bitu i=0; i<strlen(foundstr); i++) |
| 370 | if (foundstr[i] < '0' || foundstr[i] > '9') isNum = false; |
| 371 | if (isNum) { |
| 372 | // Parameter is a number with at least 12 digits => this cannot |
| 373 | // be a valid IP/name |
| 374 | // Transform by adding dots |
| 375 | char buffer[128]; |
| 376 | Bitu j = 0; |
| 377 | for (Bitu i=0; i<strlen(foundstr); i++) { |
| 378 | buffer[j++] = foundstr[i]; |
| 379 | // Add a dot after the third, sixth and ninth number |
| 380 | if (i == 2 || i == 5 || i == 8) |
| 381 | buffer[j++] = '.'; |
| 382 | // If the string is longer than 12 digits, |
| 383 | // interpret the rest as port |
| 384 | if (i == 11 && strlen(foundstr)>12) |
| 385 | buffer[j++] = ':'; |
| 386 | } |
| 387 | buffer[j] = 0; |
| 388 | foundstr = buffer; |
| 389 | } |
| 390 | } |
| 391 | Dial(foundstr); |
| 392 | return; |
| 393 | } |
| 394 | case 'I': // Some strings about firmware |
| 395 | switch (ScanNumber(scanbuf)) { |
| 396 | case 3: SendLine("DosBox Emulated Modem Firmware V1.00"); break; |
| 397 | case 4: SendLine("Modem compiled for DosBox version " VERSION); break; |
| 398 | } |
| 399 | break; |
| 400 | case 'E': // Echo on/off |
| 401 | switch (ScanNumber(scanbuf)) { |
| 402 | case 0: echo = false; break; |
| 403 | case 1: echo = true; break; |
| 404 | } |
| 405 | break; |
| 406 | case 'V': |
| 407 | switch (ScanNumber(scanbuf)) { |
| 408 | case 0: numericresponse = true; break; |
| 409 | case 1: numericresponse = false; break; |
| 410 | } |
| 411 | break; |
| 412 | case 'H': // Hang up |
| 413 | switch (ScanNumber(scanbuf)) { |
| 414 | case 0: |
| 415 | if (connected) { |
| 416 | SendRes(ResNOCARRIER); |
| 417 | EnterIdleState(); |
| 418 | return; |
| 419 | } |
| 420 | // else return ok |
| 421 | } |
| 422 | break; |
| 423 | case 'O': // Return to data mode |
| 424 | switch (ScanNumber(scanbuf)) { |
| 425 | case 0: |
| 426 | if (clientsocket) { |
| 427 | commandmode = false; |
| 428 | return; |
| 429 | } else { |
| 430 | SendRes(ResERROR); |
| 431 | return; |
| 432 | } |
| 433 | } |
| 434 | break; |
| 435 | case 'T': // Tone Dial |
| 436 | case 'P': // Pulse Dial |
| 437 | break; |
| 438 | case 'M': // Monitor |
| 439 | case 'L': // Volume |
| 440 | ScanNumber(scanbuf); |
| 441 | break; |
| 442 | case 'A': // Answer call |
| 443 | if (waitingclientsocket) { |
| 444 | AcceptIncomingCall(); |
| 445 | } else { |
| 446 | SendRes(ResERROR); |
| 447 | return; |
| 448 | } |
| 449 | return; |
| 450 | case 'Z': { // Reset and load profiles |
| 451 | // scan the number away, if any |
| 452 | ScanNumber(scanbuf); |
| 453 | if (clientsocket) SendRes(ResNOCARRIER); |
| 454 | Reset(); |
| 455 | break; |
| 456 | } |
| 457 | case ' ': // skip space |
| 458 | break; |
| 459 | case 'Q': { |
| 460 | // Response options |
| 461 | // 0 = all on, 1 = all off, |
| 462 | // 2 = no ring and no connect/carrier in answermode |
| 463 | Bitu val = ScanNumber(scanbuf); |
| 464 | if(!(val>2)) { |
| 465 | doresponse=val; |
| 466 | break; |
| 467 | } else { |
| 468 | SendRes(ResERROR); |
| 469 | return; |
| 470 | } |
| 471 | } |
| 472 | case 'S': { // Registers |
| 473 | Bitu index=ScanNumber(scanbuf); |
| 474 | if(index>=SREGS) { |
| 475 | SendRes(ResERROR); |
| 476 | return; //goto ret_none; |
| 477 | } |
| 478 | |
| 479 | while(scanbuf[0]==' ') scanbuf++; // skip spaces |
| 480 | |
| 481 | if(scanbuf[0]=='=') { // set register |
| 482 | scanbuf++; |
| 483 | while(scanbuf[0]==' ') scanbuf++; // skip spaces |
| 484 | Bitu val = ScanNumber(scanbuf); |
| 485 | reg[index]=val; |
| 486 | break; |
| 487 | } |
| 488 | else if(scanbuf[0]=='?') { // get register |
| 489 | SendNumber(reg[index]); |
| 490 | scanbuf++; |
| 491 | break; |
| 492 | } |
| 493 | //else LOG_MSG("print reg %d with %d",index,reg[index]); |
| 494 | } |
| 495 | break; |
| 496 | case '&': { // & escaped commands |
| 497 | char cmdchar = GetChar(scanbuf); |
| 498 | switch(cmdchar) { |
| 499 | case 'K': { |
| 500 | Bitu val = ScanNumber(scanbuf); |
| 501 | if(val<5) flowcontrol=val; |
| 502 | else { |
| 503 | SendRes(ResERROR); |
| 504 | return; |
| 505 | } |
| 506 | break; |
| 507 | } |
| 508 | case '\0': |
| 509 | // end of string |
| 510 | SendRes(ResERROR); |
| 511 | return; |
| 512 | default: |
| 513 | LOG_MSG("Modem: Unhandled command: &%c%d",cmdchar,ScanNumber(scanbuf)); |
| 514 | break; |
| 515 | } |
| 516 | break; |
| 517 | } |
| 518 | case '\\': { // \ escaped commands |
| 519 | char cmdchar = GetChar(scanbuf); |
| 520 | switch(cmdchar) { |
| 521 | case 'N': |
| 522 | // error correction stuff - not emulated |
| 523 | if (ScanNumber(scanbuf) > 5) { |
| 524 | SendRes(ResERROR); |
| 525 | return; |
| 526 | } |
| 527 | break; |
| 528 | case '\0': |
| 529 | // end of string |
| 530 | SendRes(ResERROR); |
| 531 | return; |
| 532 | default: |
| 533 | LOG_MSG("Modem: Unhandled command: \\%c%d",cmdchar, ScanNumber(scanbuf)); |
| 534 | break; |
| 535 | } |
| 536 | break; |
| 537 | } |
| 538 | case '\0': |
| 539 | SendRes(ResOK); |
| 540 | return; |
| 541 | default: |
| 542 | LOG_MSG("Modem: Unhandled command: %c%d",chr,ScanNumber(scanbuf)); |
| 543 | break; |
| 544 | } |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | void CSerialModem::TelnetEmulation(Bit8u * data, Bitu size) { |
| 549 | Bitu i; |
| 550 | Bit8u c; |
| 551 | for(i=0;i<size;i++) { |
| 552 | c = data[i]; |
| 553 | if(telClient.inIAC) { |
| 554 | if(telClient.recCommand) { |
| 555 | if((c != 0) && (c != 1) && (c != 3)) { |
| 556 | LOG_MSG("MODEM: Unrecognized option %d", c); |
| 557 | if(telClient.command>250) { |
| 558 | /* Reject anything we don't recognize */ |
| 559 | tqueue->addb(0xff); |
| 560 | tqueue->addb(252); |
| 561 | tqueue->addb(c); /* We won't do crap! */ |
| 562 | } |
| 563 | } |
| 564 | switch(telClient.command) { |
| 565 | case 251: /* Will */ |
| 566 | if(c == 0) telClient.binary[TEL_SERVER] = true; |
| 567 | if(c == 1) telClient.echo[TEL_SERVER] = true; |
| 568 | if(c == 3) telClient.supressGA[TEL_SERVER] = true; |
| 569 | break; |
| 570 | case 252: /* Won't */ |
| 571 | if(c == 0) telClient.binary[TEL_SERVER] = false; |
| 572 | if(c == 1) telClient.echo[TEL_SERVER] = false; |
| 573 | if(c == 3) telClient.supressGA[TEL_SERVER] = false; |
| 574 | break; |
| 575 | case 253: /* Do */ |
| 576 | if(c == 0) { |
| 577 | telClient.binary[TEL_CLIENT] = true; |
| 578 | tqueue->addb(0xff); |
| 579 | tqueue->addb(251); |
| 580 | tqueue->addb(0); /* Will do binary transfer */ |
| 581 | } |
| 582 | if(c == 1) { |
| 583 | telClient.echo[TEL_CLIENT] = false; |
| 584 | tqueue->addb(0xff); |
| 585 | tqueue->addb(252); |
| 586 | tqueue->addb(1); /* Won't echo (too lazy) */ |
| 587 | } |
| 588 | if(c == 3) { |
| 589 | telClient.supressGA[TEL_CLIENT] = true; |
| 590 | tqueue->addb(0xff); |
| 591 | tqueue->addb(251); |
| 592 | tqueue->addb(3); /* Will Suppress GA */ |
| 593 | } |
| 594 | break; |
| 595 | case 254: /* Don't */ |
| 596 | if(c == 0) { |
| 597 | telClient.binary[TEL_CLIENT] = false; |
| 598 | tqueue->addb(0xff); |
| 599 | tqueue->addb(252); |
| 600 | tqueue->addb(0); /* Won't do binary transfer */ |
| 601 | } |
| 602 | if(c == 1) { |
| 603 | telClient.echo[TEL_CLIENT] = false; |
| 604 | tqueue->addb(0xff); |
| 605 | tqueue->addb(252); |
| 606 | tqueue->addb(1); /* Won't echo (fine by me) */ |
| 607 | } |
| 608 | if(c == 3) { |
| 609 | telClient.supressGA[TEL_CLIENT] = true; |
| 610 | tqueue->addb(0xff); |
| 611 | tqueue->addb(251); |
| 612 | tqueue->addb(3); /* Will Suppress GA (too lazy) */ |
| 613 | } |
| 614 | break; |
| 615 | default: |
| 616 | LOG_MSG("MODEM: Telnet client sent IAC %d", telClient.command); |
| 617 | break; |
| 618 | } |
| 619 | telClient.inIAC = false; |
| 620 | telClient.recCommand = false; |
| 621 | continue; |
| 622 | } else { |
| 623 | if(c==249) { |
| 624 | /* Go Ahead received */ |
| 625 | telClient.inIAC = false; |
| 626 | continue; |
| 627 | } |
| 628 | telClient.command = c; |
| 629 | telClient.recCommand = true; |
| 630 | |
| 631 | if((telClient.binary[TEL_SERVER]) && (c == 0xff)) { |
| 632 | /* Binary data with value of 255 */ |
| 633 | telClient.inIAC = false; |
| 634 | telClient.recCommand = false; |
| 635 | rqueue->addb(0xff); |
| 636 | continue; |
| 637 | } |
| 638 | } |
| 639 | } else { |
| 640 | if(c == 0xff) { |
| 641 | telClient.inIAC = true; |
| 642 | continue; |
| 643 | } |
| 644 | rqueue->addb(c); |
| 645 | } |
| 646 | } |
| 647 | } |
| 648 | |
| 649 | void CSerialModem::Timer2(void) { |
| 650 | |
| 651 | unsigned long args = 1; |
| 652 | bool sendbyte = true; |
| 653 | Bitu usesize; |
| 654 | Bit8u txval; |
| 655 | Bitu txbuffersize =0; |
| 656 | |
| 657 | // Check for eventual break command |
| 658 | if (!commandmode) cmdpause++; |
| 659 | // Handle incoming data from serial port, read as much as available |
| 660 | CSerial::setCTS(true); // buffer will get 'emptier', new data can be received |
| 661 | while (tqueue->inuse()) { |
| 662 | txval = tqueue->getb(); |
| 663 | if (commandmode) { |
| 664 | if (echo) { |
| 665 | rqueue->addb(txval); |
| 666 | //LOG_MSG("Echo back to queue: %x",txval); |
| 667 | } |
| 668 | if (txval==0xa) continue; //Real modem doesn't seem to skip this? |
| 669 | else if (txval==0x8 && (cmdpos > 0)) --cmdpos; // backspace |
| 670 | else if (txval==0xd) DoCommand(); // return |
| 671 | else if (txval != '+') { |
| 672 | if(cmdpos<99) { |
| 673 | cmdbuf[cmdpos] = txval; |
| 674 | cmdpos++; |
| 675 | } |
| 676 | } |
| 677 | } |
| 678 | else {// + character |
| 679 | // 1000 ticks have passed, can check for pause command |
| 680 | if (cmdpause > 1000) { |
| 681 | if(txval ==reg[MREG_ESCAPE_CHAR]) // + |
| 682 | { |
| 683 | plusinc++; |
| 684 | if(plusinc>=3) { |
| 685 | LOG_MSG("Modem: Entering command mode(escape sequence)"); |
| 686 | commandmode = true; |
| 687 | SendRes(ResOK); |
| 688 | plusinc = 0; |
| 689 | } |
| 690 | sendbyte=false; |
| 691 | } else { |
| 692 | plusinc=0; |
| 693 | } |
| 694 | // If not a special pause command, should go for bigger blocks to send |
| 695 | } |
| 696 | tmpbuf[txbuffersize] = txval; |
| 697 | txbuffersize++; |
| 698 | } |
| 699 | } // while loop |
| 700 | |
| 701 | if (clientsocket && sendbyte && txbuffersize) { |
| 702 | // down here it saves a lot of network traffic |
| 703 | if(!clientsocket->SendArray(tmpbuf,txbuffersize)) { |
| 704 | SendRes(ResNOCARRIER); |
| 705 | EnterIdleState(); |
| 706 | } |
| 707 | } |
| 708 | // Handle incoming to the serial port |
| 709 | if(!commandmode && clientsocket && rqueue->left()) { |
| 710 | usesize = rqueue->left(); |
| 711 | if (usesize>16) usesize=16; |
| 712 | if(!clientsocket->ReceiveArray(tmpbuf, &usesize)) { |
| 713 | SendRes(ResNOCARRIER); |
| 714 | EnterIdleState(); |
| 715 | } else if(usesize) { |
| 716 | // Filter telnet commands |
| 717 | if(telnetmode) TelnetEmulation(tmpbuf, usesize); |
| 718 | else rqueue->adds(tmpbuf,usesize); |
| 719 | cmdpause = 0; |
| 720 | } |
| 721 | } |
| 722 | // Check for incoming calls |
| 723 | if (!connected && !waitingclientsocket && serversocket) { |
| 724 | waitingclientsocket=serversocket->Accept(); |
| 725 | if(waitingclientsocket) { |
| 726 | if(!CSerial::getDTR()) { |
| 727 | // accept no calls with DTR off; TODO: AT &Dn |
| 728 | EnterIdleState(); |
| 729 | } else { |
| 730 | ringing=true; |
| 731 | SendRes(ResRING); |
| 732 | CSerial::setRI(!CSerial::getRI()); |
| 733 | //MIXER_Enable(mhd.chan,true); |
| 734 | ringtimer = 3000; |
| 735 | reg[1] = 0; //Reset ring counter reg |
| 736 | } |
| 737 | } |
| 738 | } |
| 739 | if (ringing) { |
| 740 | if (ringtimer <= 0) { |
| 741 | reg[1]++; |
| 742 | if ((reg[0]>0) && (reg[0]>=reg[1])) { |
| 743 | AcceptIncomingCall(); |
| 744 | return; |
| 745 | } |
| 746 | SendRes(ResRING); |
| 747 | CSerial::setRI(!CSerial::getRI()); |
| 748 | |
| 749 | //MIXER_Enable(mhd.chan,true); |
| 750 | ringtimer = 3000; |
| 751 | } |
| 752 | --ringtimer; |
| 753 | } |
| 754 | } |
| 755 | |
| 756 | |
| 757 | //TODO |
| 758 | void CSerialModem::RXBufferEmpty() { |
| 759 | // see if rqueue has some more bytes |
| 760 | if(rqueue->inuse() && (CSerial::getRTS()||(flowcontrol!=3))){ |
| 761 | Bit8u rbyte = rqueue->getb(); |
| 762 | //LOG_MSG("Modem: sending byte %2x back to UART1",rbyte); |
| 763 | CSerial::receiveByte(rbyte); |
| 764 | } |
| 765 | } |
| 766 | |
| 767 | void CSerialModem::transmitByte(Bit8u val, bool first) { |
| 768 | waiting_tx_character=val; |
| 769 | setEvent(MODEM_TX_EVENT, bytetime); // TX event |
| 770 | if(first) ByteTransmitting(); |
| 771 | //LOG_MSG("MODEM: Byte %x to be transmitted",val); |
| 772 | } |
| 773 | |
| 774 | void CSerialModem::updatePortConfig(Bit16u, Bit8u lcr) { |
| 775 | // nothing to do here right? |
| 776 | } |
| 777 | |
| 778 | void CSerialModem::updateMSR() { |
| 779 | // think it is not needed |
| 780 | } |
| 781 | |
| 782 | void CSerialModem::setBreak(bool) { |
| 783 | // TODO: handle this |
| 784 | } |
| 785 | |
| 786 | void CSerialModem::setRTSDTR(bool rts, bool dtr) { |
| 787 | setDTR(dtr); |
| 788 | } |
| 789 | void CSerialModem::setRTS(bool val) { |
| 790 | |
| 791 | } |
| 792 | void CSerialModem::setDTR(bool val) { |
| 793 | if(!val && connected) { |
| 794 | // If DTR goes low, hang up. |
| 795 | SendRes(ResNOCARRIER); |
| 796 | EnterIdleState(); |
| 797 | LOG_MSG("Modem: Hang up due to dropped DTR."); |
| 798 | } |
| 799 | } |
| 800 | /* |
| 801 | void CSerialModem::updateModemControlLines() { |
| 802 | //bool txrdy=tqueue->left(); |
| 803 | //if(CSerial::getRTS() && txrdy) CSerial::setCTS(true); |
| 804 | //else CSerial::setCTS(tqueue->left()); |
| 805 | |
| 806 | // If DTR goes low, hang up. |
| 807 | if(connected) |
| 808 | if(oldDTRstate) |
| 809 | if(!getDTR()) { |
| 810 | SendRes(ResNOCARRIER); |
| 811 | EnterIdleState(); |
| 812 | LOG_MSG("Modem: Hang up due to dropped DTR."); |
| 813 | } |
| 814 | |
| 815 | oldDTRstate = getDTR(); |
| 816 | } |
| 817 | */ |
| 818 | |
| 819 | #endif |
| 820 |