Пример #1
0
void CVersionUnit::clock(unsigned int ms)
{
	m_timer.clock(ms);

	if (m_status == VS_WAIT && m_timer.hasExpired()) {
		m_timer.stop();

		// RPT1 and RPT2 will be filled in later
		CHeaderData header;
		header.setMyCall1(m_callsign);
		header.setMyCall2(wxT("VERS"));
		header.setYourCall(wxT("CQCQCQ  "));
		header.setId(m_id);

		m_handler->process(header, DIR_INCOMING, AS_VERSION);

		m_out    = 0U;
		m_status = VS_TRANSMIT;

		m_time.Start();

		return;
	}

	if (m_status == VS_TRANSMIT) {
		unsigned int needed = m_time.Time() / DSTAR_FRAME_TIME_MS;

		while (m_out < needed) {
			CAMBEData* data = m_data[m_out];
			data->setId(m_id);

			m_out++;

			m_handler->process(*data, DIR_INCOMING, AS_VERSION);

			if (m_out == NUM_FRAMES) {
				m_out    = 0U;
				m_status = VS_IDLE;
				return;
			}
		}

		return;
	}
}
bool CStarNetHandler::process(CHeaderData &header, AUDIO_SOURCE source)
{
	if (m_id != 0x00U)
		return false;

	wxString my = header.getMyCall1();
	m_id = header.getId();

	m_linkTimer.start();

	// Change the Your callsign to CQCQCQ
	header.setCQCQCQ();

	header.setFlag1(0x00);
	header.setFlag2(0x00);
	header.setFlag3(0x00);

	// Build new repeater list
	for (CStarNetUsersHashMap::const_iterator it = m_users.begin(); it != m_users.end(); ++it) {
		CStarNetUser* user = it->second;
		if (user != NULL) {
			// Find the user in the cache
			CUserData* userData = m_cache->findUser(user->getCallsign());

			if (userData != NULL) {
				// Find the users repeater in the repeater list, add it otherwise
				CStarNetRepeater* repeater = m_repeaters[userData->getRepeater()];
				if (repeater == NULL) {
					// Add a new repeater entry
					repeater = new CStarNetRepeater;
					repeater->m_destination = wxT("/") + userData->getRepeater().Left(6U) + userData->getRepeater().Right(1U);
					repeater->m_repeater    = userData->getRepeater();
					repeater->m_gateway     = userData->getGateway();
					repeater->m_address     = userData->getAddress();
					repeater->m_local       = CRepeaterHandler::findDVRepeater(userData->getRepeater());
					m_repeaters[userData->getRepeater()] = repeater;
				}

				delete userData;
				userData = NULL;
			}
		}
	}

	switch (m_callsignSwitch) {
		case SCS_GROUP_CALLSIGN:
			// Change the My Callsign 1 to be that of the StarNet group
			header.setMyCall1(m_groupCallsign);
			header.setMyCall2(wxT("SNET"));
			break;
		case SCS_USER_CALLSIGN:
			// Change the My Callsign 2 to be that of the StarNet group
			header.setMyCall1(my);
			header.setMyCall2(m_shortCallsign);
			break;
		default:
			break;
	}

	sendToRepeaters(header);

	if (m_txMsgSwitch)
		sendFromText(my);

	return true;
}
void CStarNetHandler::process(CHeaderData &header)
{
	wxString my   = header.getMyCall1();
	wxString your = header.getYourCall();

	unsigned int id = header.getId();

	CStarNetUser* user = m_users[my];

	// Ensure that this user is in the cache
	CUserData* userData = m_cache->findUser(my);
	if (userData == NULL)
		m_irc->findUser(my);

	if (your.IsSameAs(m_groupCallsign)) {
		// This is a normal message for logging in/relaying
		if (user == NULL) {
			// This is a new user, add them to the list
			if (m_logFile != NULL) {
				time_t timeNow = ::time(NULL);
				struct tm* tm = ::gmtime(&timeNow);

				wxString text;
				text.Printf(wxT("%04d-%02d-%02d %02d:%02d:%02d: Adding %s to StarNet group %s\n"),
					tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec,
					my.c_str(), m_groupCallsign.c_str());

				m_logFile->Write(text);
				m_logFile->Flush();
			}

			// Start the StarNet group timer if not already running
			if (!m_groupTimer.isRunning())
				m_groupTimer.start();

			user = new CStarNetUser(my, m_userTimeout * 60U);
			m_users[my] = user;

			CStarNetId* tx = new CStarNetId(id, MESSAGE_DELAY, user);
			tx->setLogin();
			m_ids[id] = tx;
		} else {
			user->reset();

			// Check that it isn't a duplicate header
			CStarNetId* tx = m_ids[id];
			if (tx != NULL) {
				delete userData;
				return;
			}

			m_ids[id] = new CStarNetId(id, MESSAGE_DELAY, user);
		}
	} else {
		delete userData;
		userData = NULL;

		// This is a logoff message
		if (user == NULL)				// Not a known user, ignore
			return;

		if (m_logFile != NULL) {
			time_t timeNow = ::time(NULL);
			struct tm* tm = ::gmtime(&timeNow);

			wxString text;
			text.Printf(wxT("%04d-%02d-%02d %02d:%02d:%02d: Removing %s from StarNet group %s, logged off\n"),
				tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec,
				user->getCallsign().c_str(), m_groupCallsign.c_str());

			m_logFile->Write(text);
			m_logFile->Flush();
		}

		// Remove the user from the user list
		m_users.erase(my);

		CStarNetId* tx = new CStarNetId(id, MESSAGE_DELAY, user);
		tx->setLogoff();
		m_ids[id] = tx;

		return;
	}

	m_groupTimer.reset();

	if (m_id != 0x00U) {
		delete userData;
		return;
	}

	m_id = id;

	// Change the Your callsign to CQCQCQ
	header.setCQCQCQ();

	header.setFlag1(0x00);
	header.setFlag2(0x00);
	header.setFlag3(0x00);

#if defined(DEXTRA_LINK)
	header.setRepeaters(m_linkGateway, m_linkReflector);
	CDExtraHandler::writeHeader(this, header, DIR_OUTGOING);
#endif
#if defined(DCS_LINK)
	header.setRepeaters(m_linkGateway, m_linkReflector);
	CDCSHandler::writeHeader(this, header, DIR_OUTGOING);
#endif

	// Get the home repeater of the user
	wxString exclude;
	if (userData != NULL) {
		exclude = userData->getRepeater();
		delete userData;
		userData = NULL;
	}

	// Build new repeater list
	for (CStarNetUsersHashMap::const_iterator it = m_users.begin(); it != m_users.end(); ++it) {
		CStarNetUser* user = it->second;
		if (user != NULL) {
			// Find the user in the cache
			CUserData* userData = m_cache->findUser(user->getCallsign());

			if (userData != NULL) {
				// Check for the excluded repeater
				if (!userData->getRepeater().IsSameAs(exclude)) {
					// Find the users repeater in the repeater list, add it otherwise
					CStarNetRepeater* repeater = m_repeaters[userData->getRepeater()];
					if (repeater == NULL) {
						// Add a new repeater entry
						repeater = new CStarNetRepeater;
						repeater->m_destination = wxT("/") + userData->getRepeater().Left(6U) + userData->getRepeater().Right(1U);
						repeater->m_repeater    = userData->getRepeater();
						repeater->m_gateway     = userData->getGateway();
						repeater->m_address     = userData->getAddress();
						repeater->m_local       = CRepeaterHandler::findDVRepeater(userData->getRepeater());
						m_repeaters[userData->getRepeater()] = repeater;
					}
				}

				delete userData;
				userData = NULL;
			}
		}
	}

	switch (m_callsignSwitch) {
		case SCS_GROUP_CALLSIGN:
			// Change the My Callsign 1 to be that of the StarNet group
			header.setMyCall1(m_groupCallsign);
			header.setMyCall2(wxT("SNET"));
			break;
		case SCS_USER_CALLSIGN:
			// Change the My Callsign 2 to be that of the StarNet group
			header.setMyCall1(my);
			header.setMyCall2(m_shortCallsign);
			break;
		default:
			break;
	}

	sendToRepeaters(header);

	if (m_txMsgSwitch)
		sendFromText(my);
}
bool CTimeServerThread::send(const wxArrayString &words, unsigned int hour, unsigned int min)
{
	unsigned int idA = CHeaderData::createId();
	unsigned int idB = CHeaderData::createId();
	unsigned int idC = CHeaderData::createId();
	unsigned int idD = CHeaderData::createId();

	CHeaderData header;
	header.setMyCall1(m_callsign);
	header.setRptCall1(m_callsignG);
	header.setRptCall2(m_callsign);		// Just for the slow data header
	header.setYourCall(wxT("CQCQCQ  "));
	header.setDestination(m_address, G2_DV_PORT);

	wxString slowData;
	switch (m_language) {
		case LANG_DEUTSCH_1:
		case LANG_DEUTSCH_2:
			header.setMyCall2(wxT("ZEIT"));
			slowData.Printf(wxT("Es ist %02u:%02u Uhr"), hour, min);
			break;
		case LANG_FRANCAIS:
			header.setMyCall2(wxT("TIME"));
			slowData.Printf(wxT("Il est %02u:%02u"), hour, min);
			break;
		case LANG_NEDERLANDS:
			header.setMyCall2(wxT("TIJD"));
			slowData.Printf(wxT("Het is %02u:%02u"), hour, min);
			break;
		case LANG_SVENSKA:
			header.setMyCall2(wxT("TID "));
			slowData.Printf(wxT("Klockan ar %02u:%02u"), hour, min);
			break;
		case LANG_ENGLISH_US_1:
		case LANG_ENGLISH_UK_1:
			header.setMyCall2(wxT("TIME"));
			if (hour == 0U)
				slowData.Printf(wxT("It is 12:%02u AM"), min);
			else if (hour == 12U)
				slowData.Printf(wxT("It is 12:%02u PM"), min);
			else if (hour > 12U)
				slowData.Printf(wxT("It is %02u:%02u PM"), hour - 12U, min);
			else
				slowData.Printf(wxT("It is %02u:%02u AM"), hour, min);
			break;
		default:
			header.setMyCall2(wxT("TIME"));
			slowData.Printf(wxT("It is %02u:%02u"), hour, min);
			break;
	}

	m_encoder.setHeaderData(header);
	m_encoder.setTextData(slowData);

	m_in = 0U;

	if (m_format != FORMAT_TEXT_TIME) {
		wxString text = words.Item(0U);
		for (unsigned int i = 1U; i < words.GetCount(); i++) {
			text.Append(wxT(" "));
			text.Append(words.Item(i));
		}

		text.Replace(wxT("_"), wxT(" "));
		wxLogMessage(wxT("Sending voice \"%s\", sending text \"%s\""), text.c_str(), slowData.c_str());

		m_seqNo = 0U;

		// Build the audio
		lookup(wxT(" "));
		lookup(wxT(" "));
		lookup(wxT(" "));
		lookup(wxT(" "));

		for (unsigned int i = 0U; i < words.GetCount(); i++)
			lookup(words.Item(i));

		lookup(wxT(" "));
		lookup(wxT(" "));
		lookup(wxT(" "));
		lookup(wxT(" "));

		end();
	} else {
		wxLogMessage(wxT("Sending text \"%s\""), slowData.c_str());

		for (unsigned int i = 0U; i < 21U; i++) {
			CAMBEData* dataOut = new CAMBEData;
			dataOut->setDestination(m_address, G2_DV_PORT);
			dataOut->setSeq(i);

			unsigned char buffer[DV_FRAME_LENGTH_BYTES];
			::memcpy(buffer + 0U, NULL_AMBE_DATA_BYTES, VOICE_FRAME_LENGTH_BYTES);

			// Insert sync bytes when the sequence number is zero, slow data otherwise
			if (i == 0U) {
				::memcpy(buffer + VOICE_FRAME_LENGTH_BYTES, DATA_SYNC_BYTES, DATA_FRAME_LENGTH_BYTES);
				m_encoder.sync();
			} else {
				m_encoder.getTextData(buffer + VOICE_FRAME_LENGTH_BYTES);
			}

			dataOut->setData(buffer, DV_FRAME_LENGTH_BYTES);

			m_data[m_in] = dataOut;
			m_in++;
		}

		CAMBEData* dataOut = new CAMBEData;
		dataOut->setData(END_PATTERN_BYTES, DV_FRAME_LENGTH_BYTES);
		dataOut->setDestination(m_address, G2_DV_PORT);
		dataOut->setSeq(0U);
		dataOut->setEnd(true);

		m_data[m_in] = dataOut;
		m_in++;
	}

	if (m_in == 0U) {
		wxLogWarning(wxT("Not sending, no audio files loaded"));
		return false;
	}

	if (!m_callsignA.IsEmpty()) {
		header.setRptCall2(m_callsignA);
		header.setId(idA);
		sendHeader(header);
	}

	if (!m_callsignB.IsEmpty()) {
		header.setRptCall2(m_callsignB);
		header.setId(idB);
		sendHeader(header);
	}

	if (!m_callsignC.IsEmpty()) {
		header.setRptCall2(m_callsignC);
		header.setId(idC);
		sendHeader(header);
	}

	if (!m_callsignD.IsEmpty()) {
		header.setRptCall2(m_callsignD);
		header.setId(idD);
		sendHeader(header);
	}

	unsigned int out = 0U;

	wxStopWatch timer;
	timer.Start();

	for (;;) {
		unsigned int needed = timer.Time() / DSTAR_FRAME_TIME_MS;

		while (out < needed) {
			CAMBEData* data = m_data[out];
			m_data[out] = NULL;
			out++;

			if (!m_callsignA.IsEmpty()) {
				data->setId(idA);
				sendData(*data);
			}

			if (!m_callsignB.IsEmpty()) {
				data->setId(idB);
				sendData(*data);
			}

			if (!m_callsignC.IsEmpty()) {
				data->setId(idC);
				sendData(*data);
			}

			if (!m_callsignD.IsEmpty()) {
				data->setId(idD);
				sendData(*data);
			}

			delete data;

			if (m_in == out)
				return true;
		}

		::wxMilliSleep(10UL);
	}
}
Пример #5
0
bool CAPRSTransmit::run()
{
	//First see if the packet is Icom supported...
	CAPRSPacket aprsPacket;
	if(!CAPRSParser::Parse(m_text, aprsPacket)){
		wxLogWarning(wxT("Unsupported APRS Format, ignoring => ") + m_text.Trim(true));
		return false;
	}

	wxString textWithCRC(aprsPacket.Raw());
	wxLogMessage(wxT("Supported APRS Format => ") + textWithCRC.Trim(true));
	//add nececessary stuff to text, but keep it the original
	textWithCRC.Replace(wxT("\n"), wxEmptyString);
	if(!textWithCRC.EndsWith(wxT("\r"))) textWithCRC.Append(wxT("\r"));
	wxString crc = wxString::Format(wxT("$$CRC%04X,"), calcCRC(textWithCRC));
	textWithCRC.Prepend(crc);

	bool opened = m_socket.open();
	if (!opened)
		return false;

	in_addr address = CUDPReaderWriter::lookup(wxT("127.0.0.1"));

	unsigned int id = CHeaderData::createId();

	wxString callsignG = m_repeaterCallsign.Left(LONG_CALLSIGN_LENGTH - 1U);
	callsignG.Append(wxT("G"));

	CHeaderData header;
	header.setId(id);
	header.setMyCall1(m_APRSCallsign);
	header.setMyCall2(wxT("APRS"));
	header.setRptCall1(callsignG);
	header.setRptCall2(m_repeaterCallsign);
	header.setYourCall(wxT("CQCQCQ  "));
	header.setDestination(address, G2_DV_PORT);

	sendHeader(header);

	CSlowDataEncoder encoder;
	encoder.setHeaderData(header);
	encoder.setGPSData(textWithCRC);
	encoder.setTextData(wxT("APRS to DPRS"));

	CAMBEData data;
	data.setDestination(address, G2_DV_PORT);
	data.setId(id);

	wxStopWatch timer;
	timer.Start();

	unsigned int out = 0U;
        unsigned int dataOut = 0U;
	unsigned int needed = (encoder.getInterleavedDataLength() / (DATA_FRAME_LENGTH_BYTES)) * 2U;

	while (dataOut < needed) {
		data.setSeq(out);

		unsigned char buffer[DV_FRAME_LENGTH_BYTES];
		::memcpy(buffer + 0U, NULL_AMBE_DATA_BYTES, VOICE_FRAME_LENGTH_BYTES);

		// Insert sync bytes when the sequence number is zero, slow data otherwise
		if (out == 0U) {
			::memcpy(buffer + VOICE_FRAME_LENGTH_BYTES, DATA_SYNC_BYTES, DATA_FRAME_LENGTH_BYTES);
		} else {
			encoder.getInterleavedData(buffer + VOICE_FRAME_LENGTH_BYTES);		
			dataOut++;
		}

		data.setData(buffer, DV_FRAME_LENGTH_BYTES);

		sendData(data);
		out++;

		if (out == 21U) out = 0U;
	}

	data.setData(END_PATTERN_BYTES, DV_FRAME_LENGTH_BYTES);
	data.setSeq(out >= 21U ? 0U : out);
	data.setEnd(true);

	sendData(data);

	m_socket.close();

	return true;
}
Пример #6
0
void* CDRATSServer::Entry()
{
	wxLogMessage(wxT("Starting the D-RATS Server thread for %s"), m_callsign.c_str());

	bool sending = false;
	unsigned int id = 0U;

	unsigned char seqNo = 0U;
	unsigned int  sent = 0U;

	wxStopWatch time;

	try {
		while (!m_stopped) {
			serviceSocket();

			if (m_readEnd && !sending) {
				id = CHeaderData::createId();

				// Write header
				CHeaderData header;
				header.setMyCall1(m_callsign);
				header.setMyCall2(wxT("DATA"));
				header.setYourCall(wxT("CQCQCQ  "));
				header.setId(id);

#if defined(LOOPBACK)
				writeHeader(header);
#else
				m_handler->process(header, DIR_INCOMING, AS_DRATS);
#endif

				m_readState = SS_FIRST;
				m_readPos   = 0U;
				sending     = true;
				seqNo       = 0U;
				sent        = 0U;

				time.Start();
			}

			if (m_readEnd && sending) {
				unsigned int needed = time.Time() / DSTAR_FRAME_TIME_MS;

				while (sent < needed && sending) {
					// Write AMBE data
					CAMBEData data;
					data.setId(id);

					unsigned char buffer[DV_FRAME_LENGTH_BYTES];
					::memcpy(buffer + 0U, NULL_AMBE_DATA_BYTES, VOICE_FRAME_LENGTH_BYTES);

					// Insert sync bytes when the sequence number is zero, slow data otherwise
					if (seqNo == 0U) {
						::memcpy(buffer + VOICE_FRAME_LENGTH_BYTES, DATA_SYNC_BYTES, DATA_FRAME_LENGTH_BYTES);
						m_readState = SS_FIRST;
					} else {
						if (m_readState == SS_FIRST) {
							unsigned char readText[3U];
							::memset(readText, 'f', 3U);

							unsigned int length = m_readLength - m_readPos;
							unsigned char bytes = 5U;
							if (length < 5U)
								bytes = length;

							readText[0U] = SLOW_DATA_TYPE_GPS | bytes;

							for (unsigned int i = 0U; i < 2U && m_readPos < m_readLength; i++)
								readText[i + 1U] = m_readBuffer[m_readPos++];

							readText[0U] ^= SCRAMBLER_BYTE1;
							readText[1U] ^= SCRAMBLER_BYTE2;
							readText[2U] ^= SCRAMBLER_BYTE3;

							::memcpy(buffer + VOICE_FRAME_LENGTH_BYTES, readText, DATA_FRAME_LENGTH_BYTES);

							m_readState = SS_SECOND;
						} else {
							unsigned char readText[3U];
							::memset(readText, 'f', 3U);

							for (unsigned int i = 0U; i < 3U && m_readPos < m_readLength; i++)
								readText[i] = m_readBuffer[m_readPos++];

							readText[0U] ^= SCRAMBLER_BYTE1;
							readText[1U] ^= SCRAMBLER_BYTE2;
							readText[2U] ^= SCRAMBLER_BYTE3;

							::memcpy(buffer + VOICE_FRAME_LENGTH_BYTES, readText, DATA_FRAME_LENGTH_BYTES);				

							m_readState = SS_FIRST;
						}
					}

					data.setSeq(seqNo);
					data.setData(buffer, DV_FRAME_LENGTH_BYTES);
					sent++;

#if defined(LOOPBACK)
					writeData(data);
#else
					m_handler->process(data, DIR_INCOMING, AS_DRATS);
#endif

					if (m_readPos == m_readLength) {
						if (m_readState == SS_SECOND) {
							seqNo++;
							if (seqNo == 21U)
								seqNo = 0U;

							unsigned char readText[3U];
							readText[0U] = 'f' ^ SCRAMBLER_BYTE1;
							readText[1U] = 'f' ^ SCRAMBLER_BYTE2;
							readText[2U] = 'f' ^ SCRAMBLER_BYTE3;

							::memcpy(buffer + VOICE_FRAME_LENGTH_BYTES, readText, DATA_FRAME_LENGTH_BYTES);

							data.setSeq(seqNo);
							data.setData(buffer, DV_FRAME_LENGTH_BYTES);
							sent++;

#if defined(LOOPBACK)
							writeData(data);
#else
							m_handler->process(data, DIR_INCOMING, AS_DRATS);
#endif
						}

						seqNo++;
						if (seqNo == 21U)
							seqNo = 0U;

						if (seqNo == 0U)
							::memcpy(buffer + VOICE_FRAME_LENGTH_BYTES, DATA_SYNC_BYTES, DATA_FRAME_LENGTH_BYTES);
						else
							::memcpy(buffer + VOICE_FRAME_LENGTH_BYTES, NULL_SLOW_DATA_BYTES, DATA_FRAME_LENGTH_BYTES);

						data.setData(buffer, DV_FRAME_LENGTH_BYTES);
						data.setSeq(seqNo);
						data.setEnd(true);
						sent++;

#if defined(LOOPBACK)
						writeData(data);
#else
						m_handler->process(data, DIR_INCOMING, AS_DRATS);
#endif

						m_readLength = 0U;
						m_readPos    = 0U;
						m_readEnd    = false;
						sending      = false;
						sent         = 0U;
					}

					seqNo++;
					if (seqNo == 21U)
						seqNo = 0U;
				}
			}

			// 50ms
			Sleep(50UL);
		}

		if (m_socket != NULL)
			m_socket->stop();
	}
	catch (std::exception& e) {
		wxString message(e.what(), wxConvLocal);
		wxLogError(wxT("Exception raised in the D-RATS Server thread - \"%s\""), message.c_str());
	}
	catch (...) {
		wxLogError(wxT("Unknown exception raised in the D-RATS Server thread"));
	}

	wxLogMessage(wxT("Stopping the D-RATS Server thread for %s"), m_callsign.c_str());

	return NULL;
}