Pau Espin Pedrol | bc1ed88 | 2018-05-17 16:59:58 +0200 | [diff] [blame] | 1 | # 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 | |
| 20 | import os |
| 21 | import tempfile |
| 22 | |
Pau Espin Pedrol | e1a58bd | 2020-04-10 20:46:07 +0200 | [diff] [blame] | 23 | from ..core import log, util, process |
Pau Espin Pedrol | ea8c3d4 | 2020-05-04 12:05:05 +0200 | [diff] [blame] | 24 | from ..core import schema |
Pau Espin Pedrol | e1a58bd | 2020-04-10 20:46:07 +0200 | [diff] [blame] | 25 | from ..core.event_loop import MainLoop |
Pau Espin Pedrol | bc1ed88 | 2018-05-17 16:59:58 +0200 | [diff] [blame] | 26 | |
Pau Espin Pedrol | ea8c3d4 | 2020-05-04 12:05:05 +0200 | [diff] [blame] | 27 | def 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 Pedrol | bc1ed88 | 2018-05-17 16:59:58 +0200 | [diff] [blame] | 34 | class Osmocon(log.Origin): |
Pau Espin Pedrol | bc1ed88 | 2018-05-17 16:59:58 +0200 | [diff] [blame] | 35 | |
| 36 | FIRMWARE_FILE="opt/osmocom-bb/target/firmware/board/compal_e88/layer1.compalram.bin" |
| 37 | |
Pau Espin Pedrol | a442cb8 | 2020-05-05 12:54:37 +0200 | [diff] [blame] | 38 | def __init__(self, testenv, conf): |
Pau Espin Pedrol | bc1ed88 | 2018-05-17 16:59:58 +0200 | [diff] [blame] | 39 | 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 Pedrol | 5860367 | 2018-08-09 13:45:55 +0200 | [diff] [blame] | 44 | self.run_dir = None |
| 45 | self.process = None |
Pau Espin Pedrol | a442cb8 | 2020-05-05 12:54:37 +0200 | [diff] [blame] | 46 | self.testenv = testenv |
Pau Espin Pedrol | bc1ed88 | 2018-05-17 16:59:58 +0200 | [diff] [blame] | 47 | 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 Pedrol | 2a2d846 | 2020-05-11 10:56:52 +0200 | [diff] [blame] | 65 | self.run_dir = util.Dir(self.testenv.test().get_run_dir().new_dir(self.name())) |
Pau Espin Pedrol | bc1ed88 | 2018-05-17 16:59:58 +0200 | [diff] [blame] | 66 | |
Pau Espin Pedrol | a442cb8 | 2020-05-05 12:54:37 +0200 | [diff] [blame] | 67 | inst = util.Dir(os.path.abspath(self.testenv.suite().trial().get_inst('osmocom-bb'))) |
Pau Espin Pedrol | bc1ed88 | 2018-05-17 16:59:58 +0200 | [diff] [blame] | 68 | |
| 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 Pedrol | a442cb8 | 2020-05-05 12:54:37 +0200 | [diff] [blame] | 89 | self.testenv.remember_to_stop(self.process) |
Pau Espin Pedrol | bc1ed88 | 2018-05-17 16:59:58 +0200 | [diff] [blame] | 90 | self.process.launch() |
| 91 | self.log('Waiting for osmocon to be up and running') |
Pau Espin Pedrol | 664e383 | 2020-06-10 19:30:33 +0200 | [diff] [blame] | 92 | MainLoop.wait(os.path.exists, self.l2_socket_path()) |
Pau Espin Pedrol | bc1ed88 | 2018-05-17 16:59:58 +0200 | [diff] [blame] | 93 | |
| 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 |