Skip to content

Commit ef8684e

Browse files
committed
Add --ble-adapter support to CHIP REPL and update docs
Add support to select the Bluetooth adapter using the common --ble-adapter command line argument. Update the advanced docs for the Python Controller.
1 parent 7cca7f0 commit ef8684e

File tree

2 files changed

+127
-103
lines changed

2 files changed

+127
-103
lines changed

docs/guides/python_chip_controller_advanced_usage.md

+124-102
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ tool or Matter accessories on Linux.
77

88
<hr>
99

10-
- [Bluetooth LE virtualization on Linux](#bluetooth-le-virtualization-on-linux)
11-
- [Debugging with gdb](#debugging-with-gdb)
10+
- [Using Python CHIP Controller advanced features](#using-python-chip-controller-advanced-features)
11+
- [Bluetooth LE virtualization on Linux](#bluetooth-le-virtualization-on-linux)
12+
- [Debugging with gdb](#debugging-with-gdb)
1213

1314
<hr>
1415

@@ -62,38 +63,38 @@ interfaces working as Bluetooth LE central and peripheral, respectively.
6263
TX bytes:3488 acl:95 sco:0 commands:110 errors:0
6364
```
6465
65-
4. Run the Python CHIP Controller with Bluetooth LE adapter defined from a
66+
4. Run the Python CHIP Controller REPL with Bluetooth LE adapter defined from a
6667
command line:
6768
68-
For example, add `--bluetooth-adapter=hci2` to use the virtual interface
69-
`hci2` listed above.
69+
For example, add `--ble-adapter=2` to use the virtual interface `hci2`
70+
listed above.
7071
7172
```
72-
chip-device-ctrl --bluetooth-adapter=hci2
73+
chip-repl --ble-adapter=2
7374
```
7475
7576
<hr>
7677
7778
## Debugging with gdb
7879
79-
You can run the chip-device-ctrl under GDB for debugging, however, since the
80-
Matter core support library is a dynamic library, you cannot read the symbols
81-
unless it is fully loaded.
80+
You can run the chip-repl under GDB for debugging, however, since the Matter SDK
81+
library is a dynamic library, you cannot read the symbols unless it is fully
82+
loaded.
8283
8384
The following block is a example debug session using GDB:
8485
8586
```
8687
# GDB cannot run scripts directly
87-
# so you need to run Python3 with the path of device controller
88-
# Here, we use the feature from bash to get the path of chip-device-ctrl without typing it.
89-
$ gdb --args python3 `which chip-device-ctrl`
90-
GNU gdb (Ubuntu 10.1-2ubuntu2) 10.1.90.20210411-git
91-
Copyright (C) 2021 Free Software Foundation, Inc.
88+
# so you need to run Python3 with the path of device controller REPL
89+
# Here, we use the feature from bash to get the path of chip-repl without typing it.
90+
$ gdb --args python3 `which chip-repl`
91+
GNU gdb (GDB) 14.2
92+
Copyright (C) 2023 Free Software Foundation, Inc.
9293
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
9394
This is free software: you are free to change and redistribute it.
9495
There is NO WARRANTY, to the extent permitted by law.
9596
Type "show copying" and "show warranty" for details.
96-
This GDB was configured as "aarch64-linux-gnu".
97+
This GDB was configured as "x86_64-pc-linux-gnu".
9798
Type "show configuration" for configuration details.
9899
For bug reporting instructions, please see:
99100
<https://www.gnu.org/software/gdb/bugs/>.
@@ -103,6 +104,12 @@ Find the GDB manual and other documentation resources online at:
103104
For help, type "help".
104105
Type "apropos word" to search for commands related to "word"...
105106
Reading symbols from python3...
107+
108+
This GDB supports auto-downloading debuginfo from the following URLs:
109+
<https://debuginfod.archlinux.org>
110+
Enable debuginfod for this session? (y or [n]) n
111+
Debuginfod has been disabled.
112+
To make this setting permanent, add 'set debuginfod enabled off' to .gdbinit.
106113
(No debugging symbols found in python3)
107114
(gdb)
108115
```
@@ -119,38 +126,68 @@ library, let run the Matter device controller first.
119126
120127
```
121128
(gdb) run
122-
Starting program: /usr/bin/python3 /home/ubuntu/.local/bin/chip-device-ctrl
129+
Starting program: /home/sag/projects/project-chip/connectedhomeip/out/venv/bin/python3 /home/sag/projects/project-chip/connectedhomeip/out/venv/bin/chip-repl
123130
[Thread debugging using libthread_db enabled]
124-
Using host libthread_db library "/lib/aarch64-linux-gnu/libthread_db.so.1".
125-
CHIP:DIS: Init admin pairing table with server storage.
126-
CHIP:IN: local node id is 0x000000000001b669
127-
CHIP:DL: MDNS failed to join multicast group on wpan0 for address type IPv4: Inet Error 1016 (0x000003F8): Address not found
128-
CHIP:ZCL: Using ZAP configuration...
129-
CHIP:ZCL: deactivate report event
130-
CHIP:CTL: Getting operational keys
131-
CHIP:CTL: Generating operational certificate for the controller
132-
CHIP:CTL: Getting root certificate for the controller from the issuer
133-
CHIP:CTL: Generating credentials
134-
CHIP:CTL: Loaded credentials successfully
135-
CHIP:DL: Platform main loop started.
136-
Chip Device Controller Shell
131+
Using host libthread_db library "/usr/lib/libthread_db.so.1".
132+
Python 3.11.9 (main, Apr 29 2024, 11:59:58) [GCC 13.2.1 20240417]
133+
Type 'copyright', 'credits' or 'license' for more information
134+
IPython 8.24.0 -- An enhanced Interactive Python. Type '?' for help.
135+
[1716395111.775747][364405:364405] CHIP:CTL: Setting attestation nonce to random value
136+
[1716395111.776196][364405:364405] CHIP:CTL: Setting CSR nonce to random value
137+
InitBLE 0[1716395111.776809][364405:364405] CHIP:DL: writing settings to file (/tmp/chip_counters.ini-T7hX27)
138+
[1716395111.776854][364405:364405] CHIP:DL: renamed tmp file to file (/tmp/chip_counters.ini)
139+
[1716395111.776860][364405:364405] CHIP:DL: NVS set: chip-counters/reboot-count = 9 (0x9)
140+
[1716395111.777261][364405:364405] CHIP:DL: Got Ethernet interface: eno2
141+
[1716395111.777555][364405:364405] CHIP:DL: Found the primary Ethernet interface:eno2
142+
[1716395111.777868][364405:364405] CHIP:DL: Got WiFi interface: wlp7s0
143+
[1716395111.777877][364405:364405] CHIP:DL: Failed to reset WiFi statistic counts
144+
────────────────────────────────────────────────────────────────────────────────────────────────────────── Matter REPL ──────────────────────────────────────────────────────────────────────────────────────────────────────────
145+
146+
147+
148+
Welcome to the Matter Python REPL!
149+
150+
For help, please type matterhelp()
151+
152+
To get more information on a particular object/class, you can pass
153+
that into matterhelp() as well.
154+
155+
156+
─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
157+
2024-05-22 18:25:11 allenwind PersistentStorage[364405] WARNING Initializing persistent storage from file: /tmp/repl-storage.json
158+
2024-05-22 18:25:11 allenwind PersistentStorage[364405] WARNING Loading configuration from /tmp/repl-storage.json...
159+
2024-05-22 18:25:11 allenwind CertificateAuthorityManager[364405] WARNING Loading certificate authorities from storage...
160+
2024-05-22 18:25:11 allenwind CertificateAuthority[364405] WARNING New CertificateAuthority at index 1
161+
2024-05-22 18:25:11 allenwind CertificateAuthority[364405] WARNING Loading fabric admins from storage...
162+
2024-05-22 18:25:11 allenwind FabricAdmin[364405] WARNING New FabricAdmin: FabricId: 0x0000000000000001, VendorId = 0xFFF1
163+
2024-05-22 18:25:11 allenwind FabricAdmin[364405] WARNING Allocating new controller with CaIndex: 1, FabricId: 0x0000000000000001, NodeId: 0x000000000001B669, CatTags: []
164+
165+
166+
The following objects have been created:
167+
certificateAuthorityManager: Manages a list of CertificateAuthority instances.
168+
caList: The list of CertificateAuthority instances.
169+
caList: A specific FabricAdmin object at index m for the nth CertificateAuthority instance.
137170

138-
chip-device-ctrl >
171+
172+
Default CHIP Device Controller (NodeId: 112233): has been initialized to manage caList[0].adminList[0] (FabricId = 1), and is available as devCtrl
173+
174+
In [1]:
139175
```
140176
141-
The prompt `chip-device-ctrl >` indicates that the Matter core library is loaded
142-
by Python, you can browse the symbols in the Matter core library, setting
143-
breakpoints on functions and many other functions provided by GDB.
177+
The prompt `In [1]: ` indicates that the Matter SDK library has been loaded and
178+
initialized by the Python Controller REPL, you can browse the symbols in the
179+
Matter core library, setting breakpoints on functions and many other functions
180+
provided by GDB.
144181
145-
You can use `Ctrl-C` to send SIGINT to the controller anytime you want so you
146-
can set breakpoints.
182+
You can use `Ctrl-Z` to send SIGSTP to the Python 3 REPL process anytime you
183+
want so you can set breakpoints (unfortunately Ctrl+C seems to be captured by
184+
the REPL).
147185
148-
> (`Ctrl-C` pressed here.)
186+
In [1]: (`Ctrl-Z` pressed here.)
149187
150188
```
151-
Thread 1 "python3" received signal SIGINT, Interrupt.
152-
0x0000fffff7db79ec in __GI___select (nfds=<optimized out>, readfds=0xffffffffe760, writefds=0x0, exceptfds=0x0, timeout=<optimized out>) at ../sysdeps/unix/sysv/linux/select.c:49
153-
49 ../sysdeps/unix/sysv/linux/select.c: No such file or directory.
189+
Thread 1 "python3" received signal SIGTSTP, Stopped (user).
190+
0x00007ffff7650ceb in kill () from /usr/lib/libc.so.6
154191
(gdb)
155192
```
156193
@@ -159,40 +196,27 @@ command in GDB (`b` for short)
159196
160197
```
161198
(gdb) b DeviceCommissioner::PairDevice
162-
Breakpoint 1 at 0xfffff5b0f6b4 (2 locations)
199+
Breakpoint 1 at 0x7fffed453943: DeviceCommissioner::PairDevice. (4 locations)
163200
(gdb)
164201
```
165202
166-
Type `continue` (`c` for short) to continue the device controller, you may need
167-
another hit of `Enter` to see the prompt.
203+
Type `signal SIGCONT` to continue the device controller after stopping it with
204+
signal stop, you may need another hit of `Enter` to see the prompt.
168205
169206
```
170-
(gdb) c
171-
Continuing.
172-
173-
chip-device-ctrl >
207+
(gdb) signal SIGCONT
208+
Continuing with signal SIGCONT.
209+
In [1]:
174210
```
175211
176212
Let do pairing over IP to see the effect of the breakpoint we just set.
177213
178214
```
179-
chip-device-ctrl > connect -ip 192.168.50.5 20202021 1
180-
Device is assigned with nodeid = 1
181-
182-
Thread 1 "python3" hit Breakpoint 1, 0x0000fffff5b0f6b4 in chip::Controller::DeviceCommissioner::PairDevice(unsigned long, chip::RendezvousParameters&)@plt ()
183-
from /home/ubuntu/.local/lib/python3.9/site-packages/chip/_ChipDeviceCtrl.so
184-
(gdb)
185-
```
186-
187-
The `@plt` symbol means it is a symbol used by dynamic library loader, type `c`
188-
(for `continue`) and it will break on the real function.
215+
In [1]: devCtrl.CommissionWithCode("MT:-24J0AFN00KA0648G00", 1234)
189216

190-
```
191-
(gdb) c
192-
Continuing.
193-
194-
Thread 1 "python3" hit Breakpoint 1, chip::Controller::DeviceCommissioner::PairDevice (this=0xd28540, remoteDeviceId=1, params=...) at ../../src/controller/CHIPDeviceController.cpp:827
195-
827 {
217+
Thread 5 "python3" hit Breakpoint 1.1, chip::Controller::DeviceCommissioner::PairDevice (this=0x7fffd8003a90, remoteDeviceId=1234, setUpCode=0x7ffff453d490 "MT:-24J0AFN00KA0648G00", params=...,
218+
discoveryType=chip::Controller::DiscoveryType::kAll, resolutionData=...) at ../../src/controller/CHIPDeviceController.cpp:646
219+
646 {
196220
(gdb)
197221
```
198222
@@ -201,46 +225,44 @@ then you can use `bt` (for `backtrace`) to see the backtrace of the call stack.
201225
202226
```
203227
(gdb) bt
204-
#0 chip::Controller::DeviceCommissioner::PairDevice(unsigned long, chip::RendezvousParameters&) (this=0xd28540, remoteDeviceId=1, params=...)
205-
at ../../src/controller/CHIPDeviceController.cpp:827
206-
#1 0x0000fffff5b3095c in pychip_DeviceController_ConnectIP(chip::Controller::DeviceCommissioner*, char const*, uint32_t, chip::NodeId)
207-
(devCtrl=0xd28540, peerAddrStr=0xfffff467ace0 "192.168.50.5", setupPINCode=20202021, nodeid=1) at ../../src/controller/python/ChipDeviceController-ScriptBinding.cpp:234
208-
#2 0x0000fffff7639148 in () at /lib/aarch64-linux-gnu/libffi.so.8
209-
#3 0x0000fffff7638750 in () at /lib/aarch64-linux-gnu/libffi.so.8
210-
#4 0x0000fffff7665a44 in () at /usr/lib/python3.9/lib-dynload/_ctypes.cpython-39-aarch64-linux-gnu.so
211-
#5 0x0000fffff7664c7c in () at /usr/lib/python3.9/lib-dynload/_ctypes.cpython-39-aarch64-linux-gnu.so
212-
#6 0x00000000004a54f0 in _PyObject_MakeTpCall ()
213-
#7 0x000000000049cb10 in _PyEval_EvalFrameDefault ()
214-
#8 0x0000000000496d1c in ()
215-
#9 0x00000000004b1eb0 in _PyFunction_Vectorcall ()
216-
#10 0x0000000000498264 in _PyEval_EvalFrameDefault ()
217-
#11 0x00000000004b1cb8 in _PyFunction_Vectorcall ()
218-
#12 0x0000000000498418 in _PyEval_EvalFrameDefault ()
219-
#13 0x0000000000496d1c in ()
220-
#14 0x00000000004b1eb0 in _PyFunction_Vectorcall ()
221-
#15 0x0000000000498418 in _PyEval_EvalFrameDefault ()
222-
#16 0x00000000004b1cb8 in _PyFunction_Vectorcall ()
223-
#17 0x00000000004c6bc8 in ()
224-
#18 0x0000000000498264 in _PyEval_EvalFrameDefault ()
225-
#19 0x00000000004b1cb8 in _PyFunction_Vectorcall ()
226-
#20 0x0000000000498418 in _PyEval_EvalFrameDefault ()
227-
#21 0x00000000004966f8 in ()
228-
#22 0x00000000004b1f18 in _PyFunction_Vectorcall ()
229-
#23 0x0000000000498418 in _PyEval_EvalFrameDefault ()
230-
#24 0x00000000004b1cb8 in _PyFunction_Vectorcall ()
231-
#25 0x0000000000498264 in _PyEval_EvalFrameDefault ()
232-
#26 0x00000000004966f8 in ()
233-
#27 0x0000000000496490 in _PyEval_EvalCodeWithName ()
234-
#28 0x0000000000595b7c in PyEval_EvalCode ()
235-
#29 0x00000000005c6a5c in ()
236-
#30 0x00000000005c0a70 in ()
237-
#31 0x00000000005c69a8 in ()
238-
#32 0x00000000005c6148 in PyRun_SimpleFileExFlags ()
239-
#33 0x00000000005b60bc in Py_RunMain ()
240-
#34 0x0000000000585a08 in Py_BytesMain ()
241-
#35 0x0000fffff7d0c9d4 in __libc_start_main (main=
242-
0x5858fc <_start+60>, argc=2, argv=0xfffffffff498, init=<optimized out>, fini=<optimized out>, rtld_fini=<optimized out>, stack_end=<optimized out>) at ../csu/libc-start.c:332
243-
#36 0x00000000005858f8 in _start ()
228+
(gdb) bt
229+
#0 chip::Controller::DeviceCommissioner::PairDevice
230+
(this=0x7fffd8003a90, remoteDeviceId=1234, setUpCode=0x7fffef2555d0 "MT:-24J0AFN00KA0648G00", params=..., discoveryType=chip::Controller::DiscoveryType::kAll, resolutionData=...)
231+
at ../../src/controller/CHIPDeviceController.cpp:646
232+
#1 0x00007fffed040825 in pychip_DeviceController_ConnectWithCode (devCtrl=0x7fffd8003a90, onboardingPayload=0x7fffef2555d0 "MT:-24J0AFN00KA0648G00", nodeid=1234, discoveryType=2 '\002')
233+
at ../../src/controller/python/ChipDeviceController-ScriptBinding.cpp:395
234+
#2 0x00007ffff6ad5596 in ??? () at /usr/lib/libffi.so.8
235+
#3 0x00007ffff6ad200e in ??? () at /usr/lib/libffi.so.8
236+
#4 0x00007ffff6ad4bd3 in ffi_call () at /usr/lib/libffi.so.8
237+
#5 0x00007ffff6aeaffc in ??? () at /usr/lib/python3.11/lib-dynload/_ctypes.cpython-311-x86_64-linux-gnu.so
238+
#6 0x00007ffff6aeb4b4 in ??? () at /usr/lib/python3.11/lib-dynload/_ctypes.cpython-311-x86_64-linux-gnu.so
239+
#7 0x00007ffff794a618 in _PyObject_MakeTpCall () at /usr/lib/libpython3.11.so.1.0
240+
#8 0x00007ffff78f3d03 in _PyEval_EvalFrameDefault () at /usr/lib/libpython3.11.so.1.0
241+
#9 0x00007ffff7adef90 in ??? () at /usr/lib/libpython3.11.so.1.0
242+
#10 0x00007ffff79ebc0b in _PyObject_FastCallDictTstate () at /usr/lib/libpython3.11.so.1.0
243+
#11 0x00007ffff79ebe02 in _PyObject_Call_Prepend () at /usr/lib/libpython3.11.so.1.0
244+
#12 0x00007ffff79ec114 in ??? () at /usr/lib/libpython3.11.so.1.0
245+
#13 0x00007ffff794a618 in _PyObject_MakeTpCall () at /usr/lib/libpython3.11.so.1.0
246+
#14 0x00007ffff78f3d03 in _PyEval_EvalFrameDefault () at /usr/lib/libpython3.11.so.1.0
247+
#15 0x00007ffff7adef90 in ??? () at /usr/lib/libpython3.11.so.1.0
248+
#16 0x00007ffff7955b97 in PyObject_Vectorcall () at /usr/lib/libpython3.11.so.1.0
249+
#17 0x00007ffff6aea174 in ??? () at /usr/lib/python3.11/lib-dynload/_ctypes.cpython-311-x86_64-linux-gnu.so
250+
#18 0x00007ffff6aea28c in ??? () at /usr/lib/python3.11/lib-dynload/_ctypes.cpython-311-x86_64-linux-gnu.so
251+
#19 0x00007ffff6ad5152 in ??? () at /usr/lib/libffi.so.8
252+
#20 0x00007ffff6ad57b8 in ??? () at /usr/lib/libffi.so.8
253+
#21 0x00007fffed5de848 in chip::DeviceLayer::Internal::GenericPlatformManagerImpl<chip::DeviceLayer::PlatformManagerImpl>::_DispatchEvent
254+
(this=0x7fffed88dc90 <chip::DeviceLayer::PlatformManagerImpl::sInstance+16>, event=0x7fffe6fffe30) at ../../src/include/platform/internal/GenericPlatformManagerImpl.ipp:304
255+
#22 0x00007fffed5dd90d in chip::DeviceLayer::PlatformManager::DispatchEvent (this=0x7fffed88dc80 <chip::DeviceLayer::PlatformManagerImpl::sInstance>, event=0x7fffe6fffe30) at ../../src/include/platform/PlatformManager.h:503
256+
#23 0x00007fffed5df45b in chip::DeviceLayer::Internal::GenericPlatformManagerImpl_POSIX<chip::DeviceLayer::PlatformManagerImpl>::ProcessDeviceEvents
257+
(this=0x7fffed88dc90 <chip::DeviceLayer::PlatformManagerImpl::sInstance+16>) at ../../src/include/platform/internal/GenericPlatformManagerImpl_POSIX.ipp:185
258+
#24 0x00007fffed5dee64 in chip::DeviceLayer::Internal::GenericPlatformManagerImpl_POSIX<chip::DeviceLayer::PlatformManagerImpl>::_RunEventLoop (this=0x7fffed88dc90 <chip::DeviceLayer::PlatformManagerImpl::sInstance+16>)
259+
--Type <RET> for more, q to quit, c to continue without paging--
260+
at ../../src/include/platform/internal/GenericPlatformManagerImpl_POSIX.ipp:227
261+
#25 0x00007fffed5dd888 in chip::DeviceLayer::PlatformManager::RunEventLoop (this=0x7fffed88dc80 <chip::DeviceLayer::PlatformManagerImpl::sInstance>) at ../../src/include/platform/PlatformManager.h:403
262+
#26 0x00007fffed5df3fe in chip::DeviceLayer::Internal::GenericPlatformManagerImpl_POSIX<chip::DeviceLayer::PlatformManagerImpl>::EventLoopTaskMain (arg=0x7fffed88dc90 <chip::DeviceLayer::PlatformManagerImpl::sInstance+16>)
263+
at ../../src/include/platform/internal/GenericPlatformManagerImpl_POSIX.ipp:256
264+
#27 0x00007ffff76a6ded in ??? () at /usr/lib/libc.so.6
265+
#28 0x00007ffff772a0dc in ??? () at /usr/lib/libc.so.6
244266
(gdb)
245267
```
246268

src/controller/python/chip/ChipReplStartup.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ def main():
9494
"-d", "--debug", help="Set default logging level to debug.", action="store_true")
9595
parser.add_argument(
9696
"-t", "--trust-store", help="Path to the PAA trust store.", action="store", default="./credentials/development/paa-root-certs")
97+
parser.add_argument(
98+
"-b", "--ble-adapter", help="Set the Bluetooth adapter index.", type=int, default=None)
9799
args = parser.parse_args()
98100

99101
if not os.path.exists(args.trust_store):
@@ -128,7 +130,7 @@ def main():
128130
# nothing we can do ... things will NOT work
129131
return
130132

131-
chip.native.Init()
133+
chip.native.Init(bluetoothAdapter=args.ble_adapter)
132134

133135
global certificateAuthorityManager
134136
global chipStack

0 commit comments

Comments
 (0)