blob: 18ee382fc9ee30f4ca7154275eaa2c4b638a9497 [file] [log] [blame]
Pau Espin Pedrolbc1ed882018-05-17 16:59:58 +02001# osmo_gsm_tester: specifics for running an osmocon
2#
3# Copyright (C) 2018 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 os
21import tempfile
22
Pau Espin Pedrole1a58bd2020-04-10 20:46:07 +020023from ..core import log, util, process
Pau Espin Pedrolea8c3d42020-05-04 12:05:05 +020024from ..core import schema
Pau Espin Pedrole1a58bd2020-04-10 20:46:07 +020025from ..core.event_loop import MainLoop
Pau Espin Pedrolbc1ed882018-05-17 16:59:58 +020026
Pau Espin Pedrolea8c3d42020-05-04 12:05:05 +020027def on_register_schemas():
28 resource_schema = {
29 'serial_device': schema.STR,
30 }
31 schema.register_resource_schema('osmocon_phone', resource_schema)
32
33
Pau Espin Pedrolbc1ed882018-05-17 16:59:58 +020034class Osmocon(log.Origin):
Pau Espin Pedrolbc1ed882018-05-17 16:59:58 +020035
36 FIRMWARE_FILE="opt/osmocom-bb/target/firmware/board/compal_e88/layer1.compalram.bin"
37
Pau Espin Pedrola442cb82020-05-05 12:54:37 +020038 def __init__(self, testenv, conf):
Pau Espin Pedrolbc1ed882018-05-17 16:59:58 +020039 serial_device = conf.get('serial_device')
40 if serial_device is None:
41 raise log.Error('osmocon_phone contains no attr "serial_device"')
42 self.serial_device = os.path.realpath(serial_device)
43 super().__init__(log.C_RUN, 'osmocon_%s' % os.path.basename(self.serial_device))
Pau Espin Pedrol58603672018-08-09 13:45:55 +020044 self.run_dir = None
45 self.process = None
Pau Espin Pedrola442cb82020-05-05 12:54:37 +020046 self.testenv = testenv
Pau Espin Pedrolbc1ed882018-05-17 16:59:58 +020047 self.conf = conf
48 self.sk_tmp_dir = tempfile.mkdtemp('', 'ogtosmoconsk')
49 if len(self.l2_socket_path().encode()) > 107:
50 raise log.Error('Path for l2 socket is longer than max allowed len for unix socket path (107):', self.l2_socket_path())
51 if len(self.loader_socket_path().encode()) > 107:
52 raise log.Error('Path for loader socket is longer than max allowed len for unix socket path (107):', self.loader_socket_path())
53
54 def l2_socket_path(self):
55 return os.path.join(self.sk_tmp_dir, 'osmocom_l2')
56
57 def loader_socket_path(self):
58 return os.path.join(self.sk_tmp_dir, 'osmocom_loader')
59
60 def start(self):
61 self.log('Resetting the phone')
62 # TODO: make sure the pone is powered off before starting osmocon
63
64 self.log('Starting osmocon')
Pau Espin Pedrol2a2d8462020-05-11 10:56:52 +020065 self.run_dir = util.Dir(self.testenv.test().get_run_dir().new_dir(self.name()))
Pau Espin Pedrolbc1ed882018-05-17 16:59:58 +020066
Pau Espin Pedrola442cb82020-05-05 12:54:37 +020067 inst = util.Dir(os.path.abspath(self.testenv.suite().trial().get_inst('osmocom-bb')))
Pau Espin Pedrolbc1ed882018-05-17 16:59:58 +020068
69 binary = inst.child('sbin', 'osmocon')
70 if not os.path.isfile(binary):
71 raise RuntimeError('Binary missing: %r' % binary)
72 lib = inst.child('lib')
73 if not os.path.isdir(lib):
74 raise RuntimeError('No lib/ in %r' % inst)
75
76 env = { 'LD_LIBRARY_PATH': util.prepend_library_path(lib) }
77
78 firmware_path = os.path.join(str(inst), Osmocon.FIRMWARE_FILE)
79 if not os.path.isfile(firmware_path):
80 raise RuntimeError('Binary missing: %r' % firmware_path)
81 self.dbg(run_dir=self.run_dir, binary=binary, env=env)
82 self.process = process.Process(self.name(), self.run_dir,
83 (binary, '-p', self.serial_device,
84 '-m', 'c123xor',
85 '-s', self.l2_socket_path(),
86 '-l', self.loader_socket_path(),
87 firmware_path),
88 env=env)
Pau Espin Pedrola442cb82020-05-05 12:54:37 +020089 self.testenv.remember_to_stop(self.process)
Pau Espin Pedrolbc1ed882018-05-17 16:59:58 +020090 self.process.launch()
91 self.log('Waiting for osmocon to be up and running')
Pau Espin Pedrol664e3832020-06-10 19:30:33 +020092 MainLoop.wait(os.path.exists, self.l2_socket_path())
Pau Espin Pedrolbc1ed882018-05-17 16:59:58 +020093
94 def running(self):
95 return not self.process.terminated()
96
97 def cleanup(self):
98 if self.sk_tmp_dir:
99 try:
100 os.remove(self.l2_socket_path())
101 except OSError:
102 pass
103 try:
104 os.remove(self.loader_socket_path())
105 except OSError:
106 pass
107 os.rmdir(self.sk_tmp_dir)
108
109# vim: expandtab tabstop=4 shiftwidth=4