Change private function _sdb_root_on to public function sdb_root_on
[tools/litmus.git] / litmus / device / devicestandalone.py
1 #!/usr/bin/env python3
2 # Copyright 2015-2016 Samsung Electronics Co., Ltd.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 import time
17 import logging
18 from litmus.device.device import device
19 from litmus.core.util import check_output, find_pattern
20 from litmus.core.util import call
21
22
23 class devicestandalone(device):
24     """
25     Litmus device class.
26     User can control device in topology by this class methods.
27     """
28
29     _booting_time = 60
30
31     def __init__(self, *args, **kwargs):
32         self.args = args
33         self.kwargs = kwargs
34         self._name = kwargs['devicename']
35         if 'serialno' in kwargs:
36             self._id = kwargs['serialno']
37         else:
38             self._id = self._find_device_id()
39
40         self._manager = kwargs['manager']
41
42     def _release(self):
43         """docstring for _release"""
44         pass
45
46     def _find_device_id(self):
47         """docstring for _find_device_id"""
48         self.start_sdb_server()
49         outs = check_output(['sdb', 'devices'], timeout=10)
50         pattern = '.*List of devices attached \n([a-zA-Z0-9]*).*device.*'
51         found = find_pattern(pattern, outs, groupindex=1)
52         if found:
53             return found
54
55     # public methods.
56
57     def get_id(self):
58         """
59         Return the id of acquired device.
60         Device instance uses this id for sdb connection.
61
62         Example:
63             >>> dut = mgr.acquire_dut('xu3')
64             >>> dut.get_id()
65             'XU3_001'
66
67         :returns str: device id
68         """
69         return self._id
70
71     def on(self, booting_time=None):
72         """
73         Turn on the device acquired.
74
75         :param float powercut_delay: power-cut delay for cutter
76         """
77         logging.debug('=================Turn on device {}=================='
78                       .format(self.get_name()))
79
80         self.start_sdb_server()
81         if self.is_on():
82             self.sdb_root_on()
83             self.run_cmd('reboot -f', timeout=20)
84         wait_for_boot = booting_time if booting_time else self._booting_time
85         for loop in range(wait_for_boot):
86             logging.debug('Wait {} seconds......'
87                           .format(wait_for_boot - loop))
88             time.sleep(1)
89         self.start_sdb_server()
90         self.sdb_root_on()
91
92     def off(self, powercut_delay=2):
93         """
94         Trun off the device acquired.
95
96         :param float powercut_delay: power-cut delay for cutter
97         """
98         logging.debug('off function is not supported for standalone device')
99
100     def flash(self, filenames, flasher='lthor', waiting=5,
101               partition_bin_mappings={'BOOT': 'zImage',
102                                       'ROOTFS': 'rootfs.img',
103                                       'USER': 'user.img',
104                                       'SYSTEM-DATA': 'system-data.img'}):
105         """
106         Flash binaries to device.
107         This function turn on device and turn off device automatically.
108
109         :param dict filenames: filename string or dict
110         :param string flasher: external flashing tool name
111         :param float waiting: waiting time to acquire cdc_acm device
112         :param dict partition_bin_mappings: partition table for device which use heimdall flasher
113
114         Example:
115             >>> dut.flash(['boot.tar.gz','platform.tar.gz'])
116             >>> or
117             >>> dut.flash('platform.tar.gz')
118
119         """
120         logging.debug('================Flash binaries to device============')
121         logging.debug(filenames)
122
123         self.start_sdb_server()
124
125         if not filenames:
126             raise Exception('There\'s no file to flash.')
127         try:
128             self.sdb_root_on()
129             self._acquire_global_lock()
130             self.run_cmd('reboot -f download', timeout=20)
131             time.sleep(waiting)
132             if flasher == 'lthor':
133                 busid = self._find_usb_busid()
134                 self._release_global_lock()
135                 self._lthor(filenames=filenames, busid=busid)
136             elif flasher == 'heimdall':
137                 (busaddr, devaddr) = self._find_usb_bus_and_device_address()
138                 self._release_global_lock()
139                 self._heimdall(filenames=filenames,
140                                busaddr=busaddr,
141                                devaddr=devaddr,
142                                partition_bin_mappings=partition_bin_mappings)
143         except (Exception, KeyboardInterrupt) as e:
144             self._release_global_lock()
145             logging.debug(e)
146             raise Exception('Can\'t flash files : {}.'.format(filenames))
147
148     def refresh_sdb_server(self):
149         """docstring for refresh_sdb_server"""
150         call('sdb kill-server; sdb start-server', shell=True, timeout=10)
151         time.sleep(1)
152
153     def start_sdb_server(self):
154         """docstring for start_sdb_server"""
155         call('sdb start-server', shell=True, timeout=10)
156         time.sleep(1)