bool CDVRPTRV3Controller::openModem() { bool ret = false; switch (m_connection) { case CT_USB: ret = m_usb->open(); break; case CT_NETWORK: ret = m_network->open(); break; default: wxLogError(wxT("Invalid connection type: %d"), int(m_connection)); break; } if (!ret) return false; ret = readSerial(); if (!ret) { closeModem(); return false; } ret = setConfig(); if (!ret) { closeModem(); return false; } return true; }
bool CDVRPTRControllerV2::findModem() { closeModem(); // Tell the repeater that the signal has gone away if (m_rx) { purgeRX(); m_mutex.Lock(); unsigned char data[2U]; data[0U] = DQT_EOT; data[1U] = 0U; m_rxData.addData(data, 2U); m_mutex.Unlock(); m_rx = false; } unsigned int count = 0U; // Purge the transmit buffer every 500ms to avoid overflow, but only try and reopen the modem every 2s while (!m_stopped) { purgeTX(); count++; if (count >= 4U) { wxLogMessage(wxT("Trying to reopen the modem")); bool ret = findPort(); if (ret) { ret = openModem(); if (ret) return true; } count = 0U; } Sleep(500UL); } return false; }
void* CDVRPTRV3Controller::Entry() { wxLogMessage(wxT("Starting DV-RPTR3 Modem Controller thread")); // Clock every 5ms-ish CTimer pollTimer(200U, 0U, 250U); pollTimer.start(); unsigned int space = 0U; while (!m_stopped) { // Poll the modem status every 250ms if (pollTimer.hasExpired()) { bool ret = readSpace(); if (!ret) { ret = findModem(); if (!ret) { wxLogMessage(wxT("Stopping DV-RPTR3 Modem Controller thread")); return NULL; } } pollTimer.start(); } unsigned int length; RESP_TYPE_V3 type = getResponse(m_buffer, length); switch (type) { case RT3_TIMEOUT: break; case RT3_ERROR: { bool ret = findModem(); if (!ret) { wxLogMessage(wxT("Stopping DV-RPTR3 Modem Controller thread")); return NULL; } } break; case RT3_HEADER: { // CUtils::dump(wxT("RT3_HEADER"), m_buffer, length); wxMutexLocker locker(m_mutex); unsigned char data[2U]; data[0U] = DSMTT_HEADER; data[1U] = RADIO_HEADER_LENGTH_BYTES; m_rxData.addData(data, 2U); m_rxData.addData(m_buffer + 9U, RADIO_HEADER_LENGTH_BYTES - 2U); // Dummy checksum data[0U] = 0xFFU; data[1U] = 0xFFU; m_rxData.addData(data, 2U); data[0U] = DSMTT_DATA; data[1U] = DV_FRAME_LENGTH_BYTES; m_rxData.addData(data, 2U); m_rxData.addData(m_buffer + 51U, DV_FRAME_LENGTH_BYTES); m_rx = true; } break; case RT3_DATA: { // CUtils::dump(wxT("RT3_DATA"), m_buffer, length); wxMutexLocker locker(m_mutex); unsigned char data[2U]; data[0U] = DSMTT_DATA; data[1U] = DV_FRAME_LENGTH_BYTES; m_rxData.addData(data, 2U); m_rxData.addData(m_buffer + 5U, DV_FRAME_LENGTH_BYTES); m_rx = true; // End of transmission? bool end = (m_buffer[19U] & 0x40U) == 0x40U; if (end) { data[0U] = DSMTT_EOT; data[1U] = 0U; m_rxData.addData(data, 2U); m_rx = false; } } break; case RT3_SPACE: space = m_buffer[9U]; // CUtils::dump(wxT("RT3_SPACE"), m_buffer, length); break; // These should not be received in this loop, but don't complain if we do case RT3_QUERY: case RT3_CONFIG: break; default: wxLogMessage(wxT("Unknown DV-RPTR3 message, type")); CUtils::dump(wxT("Buffer dump"), m_buffer, length); break; } if (space > 0U) { if (m_txData.hasData()) { unsigned char len = 0U; unsigned char data[200U]; { wxMutexLocker locker(m_mutex); m_txData.getData(&len, 1U); m_txData.getData(data, len); } // CUtils::dump(wxT("Write"), data, len); bool ret = writeModem(data, len); if (!ret) { bool ret = findModem(); if (!ret) { wxLogMessage(wxT("Stopping DV-RPTR3 Modem Controller thread")); return NULL; } } else { space--; } } } Sleep(5UL); pollTimer.clock(); } wxLogMessage(wxT("Stopping DV-RPTR3 Modem Controller thread")); closeModem(); return NULL; }
void termnetd(portConf *pDevice) { int mfd, mError = 0, sflg = 0, cnt, rv; FILE *tnlin = NULL, *tnlout = NULL, *mfp = NULL; char commbuf[256]; fd_set readFds, excpFds; int nSynch = 0; struct sigaction sigact; if (dbg) syslog(LOG_DEBUG, "telnetd():Enter"); memset(&sigact, 0, sizeof(sigact)); sigact.sa_flags = SA_RESTART; sigact.sa_handler = disablePort; sigaction(SIGUSR1, &sigact, NULL); sigact.sa_handler = enablePort; sigaction(SIGUSR2, &sigact, NULL); sigact.sa_handler = SIG_IGN; sigaction(SIGALRM, &sigact, NULL); sigact.sa_handler = SIG_IGN; sigaction(SIGCHLD, &sigact, NULL); pDevc = pDevice; mfd = openModem(pDevc); pDevc->mfd = mfd; PortEnabled = 1; if ((rv = tnlInit(NULL, NULL, &tnlin, &tnlout, 15)) != -1) { if (dbg) syslog(LOG_DEBUG, "telnetd():libtn initialized"); /* ** Enable the proccessing of a few options & set the ** Baud rate and port settings of the device */ tnlSetCallBack(TNL_ISSUBOPTDATA_CB, handleNewSubOptData); tnlSetCallBack(TNL_SPECIALCHAR_CB, handleSpecialChars); tnlSetCallBack(TNL_ERROR_CB, handleTelNetErrors); tnlEnableOption(TELOPT_BAUDRATE); tnlEnableOption(TELOPT_PORTSET); tnlEnableOption(TELOPT_DEVICE); tnlEnableOption(TELOPT_ECHO); if (dbg) syslog(LOG_DEBUG, "telnetd():Initial Baudrate of |%s|", pDevice->baud); tnlSetSubOption(TELOPT_BAUDRATE, pDevice->baud); if (dbg) syslog(LOG_DEBUG, "telnetd():Initial Port parms of |%s|", pDevice->port); tnlSetSubOption(TELOPT_PORTSET, pDevice->port); tnlSetSubOption(TELOPT_DEVICE, pDevice->devc); tnlOfferOption(TELOPT_SGA, 1); tnlRequestOption(TELOPT_SGA, 1); if (dbg) syslog(LOG_DEBUG, "telnetd():Options offered"); for (;;) { if (mfp == NULL) { if (dbg) syslog(LOG_DEBUG, "telnetd():Opening buffered I/O for device"); if ((mfp = fdopen(mfd, "w")) == NULL) { syslog(LOG_ERR, "Error opening buffer I/O for device %s:%m", pDevice->devc); closeModem(pDevc); close(0); exit(1); } if (dbg) syslog(LOG_DEBUG, "telnetd():Done opening buffered I/O for device"); } FD_ZERO(&readFds); FD_ZERO(&excpFds); FD_SET(fileno(tnlin), &readFds); FD_SET(mfd, &readFds); if (dbg) syslog(LOG_DEBUG, "telnetd():Calling tnlSelect"); if ((sflg = tnlSelect(&readFds, NULL, &excpFds, NULL)) >= 0) { if (dbg > 2) syslog(LOG_DEBUG, "telnetd():Returned from tnlSelect"); if (mfd && FD_ISSET(mfd, &readFds)) { if (dbg > 2) syslog(LOG_DEBUG, "telnetd():Have data from Modem"); if ((cnt = read(mfd, commbuf, sizeof(commbuf) - 1)) > 0) { commbuf[cnt] = '\0'; if (dbg > 2) syslog(LOG_DEBUG, "telnetd():Read %d bytes from modem: |%s|", cnt, commbuf); if (PortEnabled) { if ((cnt = write(fileno(tnlout), commbuf, cnt)) < 0) { syslog(LOG_ERR, "telnetd():Error Writing to network:%m"); syslog(LOG_INFO, "telnetd():Shutting down proccess"); fclose(mfp); close(mfd); close(0); exit(0); } if (dbg > 2) syslog(LOG_DEBUG, "telnetd():Wrote %d bytes to Network", cnt); } else if (dbg > 2) syslog(LOG_DEBUG, "telnetd():Chucked %d bytes from modem", cnt); } else if (cnt < 0 && errno != EAGAIN && errno != EINTR) { syslog(LOG_ERR, "telnetd():Error Reading from modem:%m"); mError = 1; mfd = 0; mfp = NULL; exit(1); } } if (FD_ISSET(fileno(tnlin), &readFds)) { if (dbg > 2) syslog(LOG_DEBUG, "telnetd():Have data from Network!"); if ((cnt = read(fileno(tnlin), commbuf, sizeof(commbuf))) > 0) { commbuf[cnt] = '\0'; if (dbg) syslog(LOG_DEBUG, "telnetd():Read %d bytes from Network: |%s|", cnt, commbuf); if (PortEnabled) { if ((cnt = write(mfd, commbuf, cnt)) < 0) { syslog(LOG_ERR, "telnetd():Error Writing to modem:%m"); syslog(LOG_INFO, "Shutting down proccess"); closeModem(pDevc); fclose(mfp); pDevc->mfd = -1; close(0); exit(0); } if (dbg) syslog(LOG_DEBUG, "telnetd():Wrote %d bytes to modem", cnt); } else if (dbg > 2) syslog(LOG_DEBUG, "telnetd():Chucked %d bytes from network", cnt); } else { if (dbg > 2) syslog(LOG_ERR, "telnetd():Error reading network:%m"); if (dbg > 2) syslog(LOG_DEBUG, "telnetd():Error reading network:%m"); if (dbg > 2) syslog(LOG_INFO, "telnetd():Shutting down proccess"); closeModem(pDevc); fclose(mfp); pDevc->mfd = -1; close(0); exit(0); } } if (FD_ISSET(fileno(tnlin), &excpFds)) { nSynch = 1; } } else syslog(LOG_ERR, "telnetd():Quiting!! Error from tnlSelect:%m"); } } else syslog(LOG_ERR, "telnetd():Error connecting:m"); closeModem(pDevc); fclose(mfp); pDevc->mfd = -1; close(0); }