aboutsummaryrefslogtreecommitdiffstats
path: root/Transceiver52M/ms/ms_rx_upper.h
blob: 09154bf7de2adf1e36dd81bf14056f55d24f3653 (plain)
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
#pragma once

/*
 * (C) 2022 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>
 * All Rights Reserved
 *
 * Author: Eric Wild <ewild@sysmocom.de>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
#include <netdb.h>
#include <sys/socket.h>
#include <arpa/inet.h>

#include "GSMCommon.h"
#include "radioClock.h"
#include "syncthing.h"
#include "ms_state.h"

class upper_trx : public ms_trx {
	int rx_sps, tx_sps;

	ms_TransceiverState mStates;

	bool mOn; ///< flag to indicate that transceiver is powered on
	double mTxFreq; ///< the transmit frequency
	double mRxFreq; ///< the receive frequency
	int mPower; ///< the transmit power in dB
	unsigned mMaxExpectedDelay; ///< maximum TOA offset in GSM symbols
	unsigned long long mRxSlotMask[8]; ///< MS - enabled multiframe slot mask

	int mDataSockets;
	sockaddr_in datadest;
	sockaddr datasrc;
	int mCtrlSockets;
	sockaddr_in ctrldest;
	sockaddr ctrlsrc;

	void openudp(int *mSocketFD, unsigned short localPort, const char *wlocalIP)
	{
		*mSocketFD = socket(AF_INET, SOCK_DGRAM, 0);
		int on = 1;
		setsockopt(*mSocketFD, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));

		struct sockaddr_in address;
		size_t length = sizeof(address);
		bzero(&address, length);
		address.sin_family = AF_INET;
		address.sin_addr.s_addr = inet_addr(wlocalIP);
		address.sin_port = htons(localPort);
		if (bind(*mSocketFD, (struct sockaddr *)&address, length) < 0) {
			std::cerr << "bind fail!" << std::endl;
			exit(0);
		}
	}

	bool resolveAddress(struct sockaddr_in *address, const char *host, unsigned short port)
	{
		struct hostent *hp;
		int h_errno_local;

		struct hostent hostData;
		char tmpBuffer[2048];

		auto rc = gethostbyname2_r(host, AF_INET, &hostData, tmpBuffer, sizeof(tmpBuffer), &hp, &h_errno_local);
		if (hp == NULL || hp->h_addrtype != AF_INET || rc != 0) {
			std::cerr << "WARNING -- gethostbyname() failed for " << host << ", "
				  << hstrerror(h_errno_local);
			exit(0);
			return false;
		}

		address->sin_family = hp->h_addrtype;
		assert(sizeof(address->sin_addr) == hp->h_length);
		memcpy(&(address->sin_addr), hp->h_addr_list[0], hp->h_length);
		address->sin_port = htons(port);
		return true;
	}

	void driveControl();
	void driveReceiveFIFO();
	void driveTx();
	void commandhandler(char *buffer, char *response);
	void writeClockInterface(){};

	SoftVector *pullRadioVector(GSM::Time &wTime, int &RSSI, int &timingOffset);

	bool detectSCH(ms_TransceiverState *state, signalVector &burst, struct estim_burst_params *ebp);

	std::thread thr_control, thr_rx, thr_tx;

    public:
	void start_threads();
	void start_ms();

	upper_trx() : rx_sps(4), tx_sps(4)
	{
		auto c_srcport = 6700 + 2 * 0 + 1;
		auto c_dstport = 6700 + 2 * 0 + 101;
		auto d_srcport = 6700 + 2 * 0 + 2;
		auto d_dstport = 6700 + 2 * 0 + 102;

		openudp(&mCtrlSockets, c_srcport, "127.0.0.1");
		openudp(&mDataSockets, d_srcport, "127.0.0.1");
		resolveAddress(&ctrldest, "127.0.0.1", c_dstport);
		resolveAddress(&datadest, "127.0.0.1", d_dstport);
	};
};