blob: a22c3492e5055638bb03acd00a1652eab96d9e9e [file] [log] [blame]
Pau Espin Pedrolfbb86112020-10-16 16:55:23 +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
Pau Espin Pedrold6deb282020-10-16 18:49:39 +020022import os
Pau Espin Pedrolfbb86112020-10-16 16:55:23 +020023
24from ..core import log
25from ..core import util
26from ..core import process
27from ..core import remote
28from ..core.event_loop import MainLoop
29
30class GrBroker(log.Origin):
31
32 # static fields:
33 refcount = 0
34 instance = None
35
Pau Espin Pedrold6deb282020-10-16 18:49:39 +020036 REMOTE_DIR = '/osmo-gsm-tester-grbroker'
37 TGT_SCRIPT_NAME = 'gnuradio_zmq_broker_remote.py' # File located in same directory as thine one
38 TGT_SCRIPT_LOCAL_PATH = os.path.join(util.external_dir(), TGT_SCRIPT_NAME)
39
Pau Espin Pedrolfbb86112020-10-16 16:55:23 +020040 def __init__(self):
41 super().__init__(log.C_RUN, 'gr_zmq_broker')
42 self.process = None
43 self.ctrl_port = 5005
44 self.run_dir = None
Pau Espin Pedrol4acb45a2020-10-23 13:51:03 +020045 self._run_node = None
Pau Espin Pedrolfbb86112020-10-16 16:55:23 +020046 self.rem_host = None
Pau Espin Pedrold6deb282020-10-16 18:49:39 +020047 self.remote_run_dir = None
48 self.remote_tgt_script = None
Pau Espin Pedrolfbb86112020-10-16 16:55:23 +020049 self.enb_li = []
Pau Espin Pedrolfbb86112020-10-16 16:55:23 +020050 self.ctrl_sk = None
51 self.num_enb_started = 0
52
53 @staticmethod
54 def ref():
55 if GrBroker.refcount == 0:
56 GrBroker.instance = GrBroker()
57 GrBroker.refcount = GrBroker.refcount + 1
58 return GrBroker.instance
59
60 @staticmethod
61 def unref():
62 GrBroker.refcount = GrBroker.refcount - 1
63 if GrBroker.refcount == 0:
64 GrBroker.instance.cleanup()
65 GrBroker.instance = None
66
Pau Espin Pedrolfbb86112020-10-16 16:55:23 +020067 def cleanup(self):
68 if self.ctrl_sk is not None:
69 self.cmd_exit()
70 self.ctrl_sk.close()
71 self.ctrl_sk = None
72 self.enb_li = []
73 self.testenv = None
74
75 def register_enb(self, enb):
Pau Espin Pedrol4acb45a2020-10-23 13:51:03 +020076 if len(self.enb_li) == 0:
77 # The gnuradio script is run on the first ENB host/addr.
78 self._run_node = enb._run_node
Pau Espin Pedrolfbb86112020-10-16 16:55:23 +020079 self.enb_li.append(enb)
80
81 def unregister_enb(self, enb):
82 self.enb_li.remove(enb)
83
Pau Espin Pedrol4acb45a2020-10-23 13:51:03 +020084 def addr(self):
85 return self._run_node.run_addr()
86
Pau Espin Pedrolfbb86112020-10-16 16:55:23 +020087 def gen_json_enb(self, enb):
88 res = []
89 cell_list = enb.gen_conf['enb']['cell_list']
90 for cell in cell_list:
91 # TODO: probably add enb_id, cell_id to support several ENB
92 data = {'earfcn': int(cell['dl_earfcn']),
93 'bind_port': int(cell['zmq_enb_peer_port']),
94 'peer_addr': enb.addr(),
95 'peer_port': int(cell['zmq_enb_bind_port']),
96 'use_mimo': True if enb.num_ports() > 1 else False
97 }
98 res.append(data)
99 return res
100
101 def gen_json_ue(self, enb):
102 res = {}
103 res = []
104 earfcns_done = []
105 cell_list = enb.gen_conf['enb']['cell_list']
106 for cell in cell_list:
107 data = {}
108 if int(cell['dl_earfcn']) in earfcns_done:
109 continue
110 earfcns_done.append(int(cell['dl_earfcn']))
111 data = {'earfcn': int(cell['dl_earfcn']),
112 'bind_port': int(cell['zmq_ue_peer_port']),
113 'peer_addr': enb.ue.addr(),
114 'peer_port': int(cell['zmq_ue_bind_port']),
115 'use_mimo': True if enb.num_ports() > 1 else False
116 }
117 res.append(data)
118 return res
119
120 def gen_json(self):
121 res = {'enb': [self.gen_json_enb(enb) for enb in self.enb_li],
122 'ue': [self.gen_json_ue(self.enb_li[0])]}
123 return res
124
125 def configure(self):
Pau Espin Pedrolfbb86112020-10-16 16:55:23 +0200126 self.testenv = self.enb_li[0].testenv
127 self.run_dir = util.Dir(self.testenv.test().get_run_dir().new_dir(self.name()))
Pau Espin Pedrol4acb45a2020-10-23 13:51:03 +0200128 if not self._run_node.is_local():
129 self.rem_host = remote.RemoteHost(self.run_dir, self._run_node.ssh_user(), self._run_node.ssh_addr())
Pau Espin Pedrold6deb282020-10-16 18:49:39 +0200130 remote_prefix_dir = util.Dir(GrBroker.REMOTE_DIR)
131 self.remote_run_dir = util.Dir(remote_prefix_dir.child(self.name()))
132 self.remote_tgt_script = os.path.join(str(self.remote_run_dir), GrBroker.TGT_SCRIPT_NAME)
133 self.rem_host.recreate_remote_dir(self.remote_run_dir)
134 self.rem_host.scp('scp-grboker-to-remote', GrBroker.TGT_SCRIPT_LOCAL_PATH, self.remote_tgt_script)
Pau Espin Pedrolfbb86112020-10-16 16:55:23 +0200135
136 def start(self):
137 self.num_enb_started += 1
138 self.dbg('start(%d/%d)' % (self.num_enb_started, len(self.enb_li)))
139 if self.num_enb_started == 1:
140 self.configure()
Pau Espin Pedrol4acb45a2020-10-23 13:51:03 +0200141 if self._run_node.is_local():
Pau Espin Pedrold6deb282020-10-16 18:49:39 +0200142 args = (GrBroker.TGT_SCRIPT_LOCAL_PATH,
143 '-c', str(self.ctrl_port),
Pau Espin Pedrol4acb45a2020-10-23 13:51:03 +0200144 '-b', self.addr())
Pau Espin Pedrolfbb86112020-10-16 16:55:23 +0200145 self.process = process.Process(self.name(), self.run_dir, args)
146 else:
Pau Espin Pedrold6deb282020-10-16 18:49:39 +0200147 args = (self.remote_tgt_script,
148 '-c', str(self.ctrl_port),
Pau Espin Pedrol4acb45a2020-10-23 13:51:03 +0200149 '-b', self.addr())
Pau Espin Pedrold6deb282020-10-16 18:49:39 +0200150 self.process = self.rem_host.RemoteProcessSafeExit(self.name(), self.remote_run_dir, args, wait_time_sec=7)
Pau Espin Pedrolfbb86112020-10-16 16:55:23 +0200151 self.testenv.remember_to_stop(self.process)
152 self.process.launch()
153 # Wait until all ENBs are configured/started:
154 if self.num_enb_started == len(self.enb_li):
155 self.dbg('waiting for gr script to be available...')
156 MainLoop.sleep(5)
157 self.ctrl_sk = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
158 self.cmd_setup()
159
160 def send_cmd(self, str_buf):
161 self.dbg('sending cmd: "%s"' % str_buf)
Pau Espin Pedrol4acb45a2020-10-23 13:51:03 +0200162 self.ctrl_sk.sendto(str_buf.encode('utf-8'), (self.addr(), self.ctrl_port))
Pau Espin Pedrolfbb86112020-10-16 16:55:23 +0200163
164 def cmd_setup(self):
165 cfg = self.gen_json()
166 buf = json.dumps(cfg)
167 self.send_cmd(buf)
168
169 def cmd_set_relative_gain_on_local_port(self, port, rel_gain):
170 d = { 'action': 'set_relative_gain',
171 'port': port,
172 'rel_gain': rel_gain
173 }
174 buf = json.dumps(d)
175 self.send_cmd(buf)
176
177 def cmd_exit(self):
178 d = { 'action': 'exit' }
179 buf = json.dumps(d)
180 self.send_cmd(buf)