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 | |
| 23 | from . import log, util, process |
| 24 | from .event_loop import MainLoop |
| 25 | |
| 26 | class Osmocon(log.Origin): |
Pau Espin Pedrol | bc1ed88 | 2018-05-17 16:59:58 +0200 | [diff] [blame] | 27 | |
| 28 | FIRMWARE_FILE="opt/osmocom-bb/target/firmware/board/compal_e88/layer1.compalram.bin" |
| 29 | |
| 30 | def __init__(self, suite_run, conf): |
| 31 | serial_device = conf.get('serial_device') |
| 32 | if serial_device is None: |
| 33 | raise log.Error('osmocon_phone contains no attr "serial_device"') |
| 34 | self.serial_device = os.path.realpath(serial_device) |
| 35 | 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] | 36 | self.run_dir = None |
| 37 | self.process = None |
Pau Espin Pedrol | bc1ed88 | 2018-05-17 16:59:58 +0200 | [diff] [blame] | 38 | self.suite_run = suite_run |
| 39 | self.conf = conf |
| 40 | self.sk_tmp_dir = tempfile.mkdtemp('', 'ogtosmoconsk') |
| 41 | if len(self.l2_socket_path().encode()) > 107: |
| 42 | raise log.Error('Path for l2 socket is longer than max allowed len for unix socket path (107):', self.l2_socket_path()) |
| 43 | if len(self.loader_socket_path().encode()) > 107: |
| 44 | raise log.Error('Path for loader socket is longer than max allowed len for unix socket path (107):', self.loader_socket_path()) |
| 45 | |
| 46 | def l2_socket_path(self): |
| 47 | return os.path.join(self.sk_tmp_dir, 'osmocom_l2') |
| 48 | |
| 49 | def loader_socket_path(self): |
| 50 | return os.path.join(self.sk_tmp_dir, 'osmocom_loader') |
| 51 | |
| 52 | def start(self): |
| 53 | self.log('Resetting the phone') |
| 54 | # TODO: make sure the pone is powered off before starting osmocon |
| 55 | |
| 56 | self.log('Starting osmocon') |
| 57 | self.run_dir = util.Dir(self.suite_run.get_test_run_dir().new_dir(self.name())) |
| 58 | |
| 59 | inst = util.Dir(os.path.abspath(self.suite_run.trial.get_inst('osmocom-bb'))) |
| 60 | |
| 61 | binary = inst.child('sbin', 'osmocon') |
| 62 | if not os.path.isfile(binary): |
| 63 | raise RuntimeError('Binary missing: %r' % binary) |
| 64 | lib = inst.child('lib') |
| 65 | if not os.path.isdir(lib): |
| 66 | raise RuntimeError('No lib/ in %r' % inst) |
| 67 | |
| 68 | env = { 'LD_LIBRARY_PATH': util.prepend_library_path(lib) } |
| 69 | |
| 70 | firmware_path = os.path.join(str(inst), Osmocon.FIRMWARE_FILE) |
| 71 | if not os.path.isfile(firmware_path): |
| 72 | raise RuntimeError('Binary missing: %r' % firmware_path) |
| 73 | self.dbg(run_dir=self.run_dir, binary=binary, env=env) |
| 74 | self.process = process.Process(self.name(), self.run_dir, |
| 75 | (binary, '-p', self.serial_device, |
| 76 | '-m', 'c123xor', |
| 77 | '-s', self.l2_socket_path(), |
| 78 | '-l', self.loader_socket_path(), |
| 79 | firmware_path), |
| 80 | env=env) |
| 81 | self.suite_run.remember_to_stop(self.process) |
| 82 | self.process.launch() |
| 83 | self.log('Waiting for osmocon to be up and running') |
| 84 | MainLoop.wait(self, os.path.exists, self.l2_socket_path()) |
| 85 | |
| 86 | def running(self): |
| 87 | return not self.process.terminated() |
| 88 | |
| 89 | def cleanup(self): |
| 90 | if self.sk_tmp_dir: |
| 91 | try: |
| 92 | os.remove(self.l2_socket_path()) |
| 93 | except OSError: |
| 94 | pass |
| 95 | try: |
| 96 | os.remove(self.loader_socket_path()) |
| 97 | except OSError: |
| 98 | pass |
| 99 | os.rmdir(self.sk_tmp_dir) |
| 100 | |
| 101 | # vim: expandtab tabstop=4 shiftwidth=4 |