blob: 3725760f91cd26a1c5d742ef8f724331057e1436 [file] [log] [blame]
Pau Espin Pedrol41091232020-10-05 19:23:38 +02001# osmo_gsm_tester: class defining a RF emulation object implemented using SRS ENB stdin interface
2#
3# Copyright (C) 2020 by sysmocom - s.f.m.c. GmbH
4#
5# Author: Pau Espin Pedrol <pespin@sysmocom.de>
6#
7# This program is free software: you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as
9# published by the Free Software Foundation, either version 3 of the
10# License, or (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20import json
21import socket
22
23from ..core import log
24from ..core import util
25from ..core import process
26from ..core import remote
27from ..core.event_loop import MainLoop
28from .rfemu import RFemulation
29
30
31class GrBroker(log.Origin):
32
33 # static fields:
34 refcount = 0
35 instance = None
36
37 def __init__(self):
38 super().__init__(log.C_RUN, 'zmq_gr_broker')
39 self.process = None
40 self.ctrl_port = 5005
41 self.run_dir = None
42 self.rem_host = None
43 self.cfg = None
44 self.enb = None
45 self.addr = None
46 self.ctrl_sk = None
47
48 @staticmethod
49 def ref():
50 if GrBroker.refcount == 0:
51 GrBroker.instance = GrBroker()
52 GrBroker.refcount = GrBroker.refcount + 1
53 return GrBroker.instance
54
55 @staticmethod
56 def unref():
57 GrBroker.refcount = GrBroker.refcount - 1
58 if GrBroker.refcount == 0:
59 GrBroker.instance.cleanup()
60 GrBroker.instance = None
61
62
63 def cleanup(self):
64 if self.ctrl_sk is not None:
65 self.cmd_exit()
66 self.ctrl_sk.close()
67 self.ctrl_sk = None
68 self.enb = None
69 self.testenv = None
70
71 def handle_enb(self, enb):
72 self.enb = enb
73 self.addr = self.enb.addr()
74 self.testenv = self.enb.testenv
75 self.cfg = self.gen_json(enb)
76 # FIXME: we may need to delay this somehow if we want to support several ENBs
77 self.start()
78 self.setup()
79
80 def gen_json_enb(self, enb):
81 res = []
82 cell_list = enb.gen_conf['enb']['cell_list']
83 for cell in cell_list:
84 # TODO: probably add enb_id, cell_id to support several ENB
85 data = {'earfcn': int(cell['dl_earfcn']),
86 'bind_port': int(cell['zmq_enb_peer_port']),
87 'peer_addr': enb.addr(),
88 'peer_port': int(cell['zmq_enb_bind_port']),
89 'use_mimo': True if enb.num_ports() > 1 else False
90 }
91 res.append(data)
92 return res
93
94 def gen_json_ue(self, enb):
95 res = {}
96 res = []
97 earfcns_done = []
98 cell_list = enb.gen_conf['enb']['cell_list']
99 for cell in cell_list:
100 data = {}
101 if int(cell['dl_earfcn']) in earfcns_done:
102 continue
103 earfcns_done.append(int(cell['dl_earfcn']))
104 data = {'earfcn': int(cell['dl_earfcn']),
105 'bind_port': int(cell['zmq_ue_peer_port']),
106 'peer_addr': enb.ue.addr(),
107 'peer_port': int(cell['zmq_ue_bind_port']),
108 'use_mimo': True if enb.num_ports() > 1 else False
109 }
110 res.append(data)
111 return res
112
113 def gen_json(self, enb):
114 res = {'enb': [self.gen_json_enb(enb)],
115 'ue': [self.gen_json_ue(enb)]}
116 return res
117
118 def start(self):
119 self.run_dir = util.Dir(self.testenv.test().get_run_dir().new_dir(self.name()))
120
121 args = ('osmo-gsm-tester_zmq_broker.py',
122 '-c', str(self.ctrl_port),
123 '-b', self.enb.addr())
124
125 if self.enb._run_node.is_local():
126 self.process = process.Process(self.name(), self.run_dir, args)
127 else:
128 self.rem_host = remote.RemoteHost(self.run_dir, self.enb._run_node.ssh_user(), self.enb._run_node.ssh_addr())
129 self.process = self.rem_host.RemoteProcessSafeExit('zmq_gr_broker', util.Dir('/tmp/ogt_%s' % self.name()), args, wait_time_sec=7)
130 self.testenv.remember_to_stop(self.process)
131 self.process.launch()
132
133 def setup(self):
134 self.dbg('waiting for gr script to be available...')
135 MainLoop.sleep(5)
136 self.ctrl_sk = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
137 buf = json.dumps(self.cfg)
138 self.send_cmd(buf)
139
140 def send_cmd(self, str_buf):
141 self.dbg('sending cmd: "%s"' % str_buf)
142 self.ctrl_sk.sendto(str_buf.encode('utf-8'), (self.addr, self.ctrl_port))
143
144 def cmd_set_relative_gain_on_local_port(self, port, rel_gain):
145 d = { 'action': 'set_relative_gain',
146 'port': port,
147 'rel_gain': rel_gain
148 }
149 buf = json.dumps(d)
150 self.send_cmd(buf)
151
152 def cmd_exit(self):
153 d = { 'action': 'exit' }
154 buf = json.dumps(d)
155 self.send_cmd(buf)
156
157class RFemulationGnuradioZmq(RFemulation):
158##############
159# PROTECTED
160##############
161 def __init__(self, conf):
162 super().__init__(conf, 'gnuradio_zmq')
163 self.broker = None
164 self.ctrl_port = 5005
165 self.cell_id = int(conf.get('cell_id'))
166 if self.cell_id is None:
167 raise log.Error('No "cell_id" attribute provided in rfemu conf!')
168 self.enb = conf.get('enb')
169 if self.enb is None:
170 raise log.Error('No "srsenb" attribute provided in rfemu conf!')
171 self.set_name('%s_%s_%d' % (self.name(), self.enb.name(), self.cell_id))
172 self.testenv = self.enb.testenv
173 self.configure()
174
175 def __del__(self):
176 if self.broker:
177 self.broker.unref()
178 self.broker = None
179 self.enb = None
180 self.testenv = None
181
182 def configure(self):
183 self.broker = GrBroker.ref()
184
185#############################
186# PUBLIC (test API included)
187#############################
188 def set_attenuation(self, db):
189 for cell in self.enb.gen_conf['enb']['cell_list']:
190 if int(cell['cell_id']) == self.cell_id:
Andre Puschmannd5579fa2020-10-15 16:40:21 +0200191 # convert negative dB to amplitude
192 amp = pow(10, -db/20.0)
193 self.broker.cmd_set_relative_gain_on_local_port(cell['zmq_enb_peer_port'], amp)
Pau Espin Pedrol41091232020-10-05 19:23:38 +0200194 break
195
196 def get_max_attenuation(self):
Andre Puschmannd5579fa2020-10-15 16:40:21 +0200197 return 200
Pau Espin Pedrol41091232020-10-05 19:23:38 +0200198
199# vim: expandtab tabstop=4 shiftwidth=4