blob: 6ad656efbc083646bab1370514eaea80e23c7ebc [file] [log] [blame]
Alexander Dorokhinec48276a2016-12-05 19:04:25 -08001#!/usr/bin/env python3.4
2#
3# Copyright 2016 Google Inc.
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
Alexander Dorokhinec48276a2016-12-05 19:04:25 -080016"""Tool to interactively call sl4a methods.
17
18SL4A (Scripting Layer for Android) is an RPC service exposing API calls on
19Android.
20
21Original version: https://github.com/damonkohler/sl4a
22
23Fork in AOSP (can make direct system privileged calls):
24https://android.googlesource.com/platform/external/sl4a/
25
26Also allows access to Event Dispatcher, which allows waiting for asynchronous
27actions. For more information see the Mobly codelab:
28https://github.com/google/mobly#event-dispatcher
29
30Usage:
Alexander Dorokhine6e7a6362016-12-13 15:10:27 -080031$ sl4a_shell
Alexander Dorokhinec48276a2016-12-05 19:04:25 -080032>>> s.getBuildID()
33u'N2F52'
34"""
Alexander Dorokhinec48276a2016-12-05 19:04:25 -080035
36import argparse
Alexander Dorokhineefa488e2017-05-25 18:14:47 -070037import logging
Alexander Dorokhinec48276a2016-12-05 19:04:25 -080038
Alexander Dorokhine401771b2016-12-14 18:26:35 -080039from mobly.controllers.android_device_lib import jsonrpc_shell_base
Ang Li9ffe8822018-10-03 21:35:14 -070040from mobly.controllers.android_device_lib.services import sl4a_service
Alexander Dorokhinec48276a2016-12-05 19:04:25 -080041
42
Alexander Dorokhine401771b2016-12-14 18:26:35 -080043class Sl4aShell(jsonrpc_shell_base.JsonRpcShellBase):
Ang Lib7c23242021-01-20 10:47:27 +080044
Eric Lin (Tzu Hsiang Lin)11d0f082021-01-12 21:21:37 +080045 def _start_services(self, console_env):
46 """Overrides superclass."""
47 self._ad.services.register('sl4a', sl4a_service.Sl4aService)
48 console_env['s'] = self._ad.services.sl4a
49 console_env['sl4a'] = self._ad.sl4a
50 console_env['ed'] = self._ad.ed
Alexander Dorokhinec48276a2016-12-05 19:04:25 -080051
Eric Lin (Tzu Hsiang Lin)11d0f082021-01-12 21:21:37 +080052 def _get_banner(self, serial):
53 lines = [
nkprasad12ebdec3a2023-10-26 17:29:55 -070054 'Connected to %s.' % serial,
55 'Call methods against:',
56 ' ad (android_device.AndroidDevice)',
57 ' sl4a or s (SL4A)',
58 ' ed (EventDispatcher)',
Eric Lin (Tzu Hsiang Lin)11d0f082021-01-12 21:21:37 +080059 ]
60 return '\n'.join(lines)
Alexander Dorokhinec48276a2016-12-05 19:04:25 -080061
62
63if __name__ == '__main__':
Ang Lib7c23242021-01-20 10:47:27 +080064 parser = argparse.ArgumentParser(description='Interactive client for sl4a.')
Eric Lin (Tzu Hsiang Lin)11d0f082021-01-12 21:21:37 +080065 parser.add_argument(
Ang Lib7c23242021-01-20 10:47:27 +080066 '-s',
67 '--serial',
nkprasad12ebdec3a2023-10-26 17:29:55 -070068 help='Device serial to connect to (if more than one device is connected)',
69 )
Eric Lin (Tzu Hsiang Lin)11d0f082021-01-12 21:21:37 +080070 args = parser.parse_args()
71 logging.basicConfig(level=logging.INFO)
72 Sl4aShell().main(args.serial)