diff mbox series

[BlueZ] tools/test-runner: add option to start Pipewire inside the VM

Message ID 48701651ef435518ac8432d80dfdc2dfe80f3703.1681569400.git.pav@iki.fi
State New
Headers show
Series [BlueZ] tools/test-runner: add option to start Pipewire inside the VM | expand

Commit Message

Pauli Virtanen April 15, 2023, 2:39 p.m. UTC
Add option for launching Pipewire inside the VM to serve Bluetooth
endpoints, which can be used in tests.

If daemon and emulator were also started, wait for the endpoints to
appear.
---

Notes:
    An example how you can launch Pipewire to serve Bluetooth endpoints.

 tools/test-runner.c | 247 +++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 242 insertions(+), 5 deletions(-)

Comments

bluez.test.bot@gmail.com April 15, 2023, 3:33 p.m. UTC | #1
This is automated email and please do not reply to this email!

Dear submitter,

Thank you for submitting the patches to the linux bluetooth mailing list.
This is a CI test results with your patch series:
PW Link:https://patchwork.kernel.org/project/bluetooth/list/?series=740121

---Test result---

Test Summary:
CheckPatch                    FAIL      0.80 seconds
GitLint                       PASS      0.27 seconds
BuildEll                      PASS      26.33 seconds
BluezMake                     FAIL      55.20 seconds
MakeCheck                     FAIL      180.65 seconds
MakeDistcheck                 PASS      148.92 seconds
CheckValgrind                 FAIL      46.02 seconds
CheckSmatch                   FAIL      101.21 seconds
bluezmakeextell               FAIL      27.79 seconds
IncrementalBuild              FAIL      55.06 seconds
ScanBuild                     FAIL      585.71 seconds

Details
##############################
Test: CheckPatch - FAIL
Desc: Run checkpatch.pl script
Output:
[BlueZ] tools/test-runner: add option to start Pipewire inside the VM
WARNING:RETURN_VOID: void function return statements are not generally useful
#292: FILE: tools/test-runner.c:809:
+	return;
+}

/github/workspace/src/src/13212550.patch total: 0 errors, 1 warnings, 323 lines checked

NOTE: For some of the reported defects, checkpatch may be able to
      mechanically convert to the typical style using --fix or --fix-inplace.

/github/workspace/src/src/13212550.patch has style problems, please review.

NOTE: Ignored message types: COMMIT_MESSAGE COMPLEX_MACRO CONST_STRUCT FILE_PATH_CHANGES MISSING_SIGN_OFF PREFER_PACKED SPDX_LICENSE_TAG SPLIT_STRING SSCANF_TO_KSTRTO

NOTE: If any of the errors are false positives, please report
      them to the maintainer, see CHECKPATCH in MAINTAINERS.


##############################
Test: BluezMake - FAIL
Desc: Build BlueZ
Output:

tools/mgmt-tester.c: In function ‘main’:
tools/mgmt-tester.c:12655:5: note: variable tracking size limit exceeded with ‘-fvar-tracking-assignments’, retrying without
12655 | int main(int argc, char *argv[])
      |     ^~~~
tools/test-runner.c: In function ‘get_command_stdout’:
tools/test-runner.c:626:19: error: comparison of integer expressions of different signedness: ‘ssize_t’ {aka ‘long int’} and ‘size_t’ {aka ‘long unsigned int’} [-Werror=sign-compare]
  626 |   if (nread + 256 > allocated) {
      |                   ^
cc1: all warnings being treated as errors
make[1]: *** [Makefile:7591: tools/test-runner.o] Error 1
make[1]: *** Waiting for unfinished jobs....
make: *** [Makefile:4506: all] Error 2
##############################
Test: MakeCheck - FAIL
Desc: Run Bluez Make Check
Output:

tools/test-runner.c: In function ‘get_command_stdout’:
tools/test-runner.c:626:19: error: comparison of integer expressions of different signedness: ‘ssize_t’ {aka ‘long int’} and ‘size_t’ {aka ‘long unsigned int’} [-Werror=sign-compare]
  626 |   if (nread + 256 > allocated) {
      |                   ^
cc1: all warnings being treated as errors
make[1]: *** [Makefile:7591: tools/test-runner.o] Error 1
make: *** [Makefile:11779: check] Error 2
##############################
Test: CheckValgrind - FAIL
Desc: Run Bluez Make Check with Valgrind
Output:

tools/test-runner.c: In function ‘get_command_stdout’:
tools/test-runner.c:626:19: error: comparison of integer expressions of different signedness: ‘ssize_t’ {aka ‘long int’} and ‘size_t’ {aka ‘long unsigned int’} [-Werror=sign-compare]
  626 |   if (nread + 256 > allocated) {
      |                   ^
cc1: all warnings being treated as errors
make[1]: *** [Makefile:7591: tools/test-runner.o] Error 1
make[1]: *** Waiting for unfinished jobs....
tools/mgmt-tester.c: In function ‘main’:
tools/mgmt-tester.c:12655:5: note: variable tracking size limit exceeded with ‘-fvar-tracking-assignments’, retrying without
12655 | int main(int argc, char *argv[])
      |     ^~~~
make: *** [Makefile:11779: check] Error 2
##############################
Test: CheckSmatch - FAIL
Desc: Run smatch tool with source
Output:

src/shared/crypto.c:271:21: warning: Variable length array is used.
src/shared/crypto.c:272:23: warning: Variable length array is used.
src/shared/gatt-helpers.c:768:31: warning: Variable length array is used.
src/shared/gatt-helpers.c:830:31: warning: Variable length array is used.
src/shared/gatt-helpers.c:1323:31: warning: Variable length array is used.
src/shared/gatt-helpers.c:1354:23: warning: Variable length array is used.
src/shared/gatt-server.c:275:25: warning: Variable length array is used.
src/shared/gatt-server.c:618:25: warning: Variable length array is used.
src/shared/gatt-server.c:717:25: warning: Variable length array is used.
src/shared/shell.c: note: in included file (through /usr/include/readline/readline.h):
/usr/include/readline/rltypedefs.h:35:23: warning: non-ANSI function declaration of function 'Function'
/usr/include/readline/rltypedefs.h:36:25: warning: non-ANSI function declaration of function 'VFunction'
/usr/include/readline/rltypedefs.h:37:27: warning: non-ANSI function declaration of function 'CPFunction'
/usr/include/readline/rltypedefs.h:38:29: warning: non-ANSI function declaration of function 'CPPFunction'
src/shared/shell.c:615:21: warning: non-ANSI function declaration of function 'bt_shell_usage'
src/shared/crypto.c:271:21: warning: Variable length array is used.
src/shared/crypto.c:272:23: warning: Variable length array is used.
src/shared/gatt-helpers.c:768:31: warning: Variable length array is used.
src/shared/gatt-helpers.c:830:31: warning: Variable length array is used.
src/shared/gatt-helpers.c:1323:31: warning: Variable length array is used.
src/shared/gatt-helpers.c:1354:23: warning: Variable length array is used.
src/shared/gatt-server.c:275:25: warning: Variable length array is used.
src/shared/gatt-server.c:618:25: warning: Variable length array is used.
src/shared/gatt-server.c:717:25: warning: Variable length array is used.
src/shared/shell.c: note: in included file (through /usr/include/readline/readline.h):
/usr/include/readline/rltypedefs.h:35:23: warning: non-ANSI function declaration of function 'Function'
/usr/include/readline/rltypedefs.h:36:25: warning: non-ANSI function declaration of function 'VFunction'
/usr/include/readline/rltypedefs.h:37:27: warning: non-ANSI function declaration of function 'CPFunction'
/usr/include/readline/rltypedefs.h:38:29: warning: non-ANSI function declaration of function 'CPPFunction'
src/shared/shell.c:615:21: warning: non-ANSI function declaration of function 'bt_shell_usage'
tools/mesh-cfgtest.c:1453:17: warning: unknown escape sequence: '\%'
tools/sco-tester.c: note: in included file:
./lib/bluetooth.h:206:15: warning: array of flexible structures
./lib/bluetooth.h:211:31: warning: array of flexible structures
tools/bneptest.c:634:39: warning: unknown escape sequence: '\%'
tools/seq2bseq.c:57:26: warning: Variable length array is used.
tools/test-runner.c: In function ‘get_command_stdout’:
tools/test-runner.c:626:19: error: comparison of integer expressions of different signedness: ‘ssize_t’ {aka ‘long int’} and ‘size_t’ {aka ‘long unsigned int’} [-Werror=sign-compare]
  626 |   if (nread + 256 > allocated) {
      |                   ^
cc1: all warnings being treated as errors
make[1]: *** [Makefile:7591: tools/test-runner.o] Error 1
make[1]: *** Waiting for unfinished jobs....
make: *** [Makefile:4506: all] Error 2
##############################
Test: bluezmakeextell - FAIL
Desc: Build Bluez with External ELL
Output:

tools/test-runner.c: In function ‘get_command_stdout’:
tools/test-runner.c:626:19: error: comparison of integer expressions of different signedness: ‘ssize_t’ {aka ‘long int’} and ‘size_t’ {aka ‘long unsigned int’} [-Werror=sign-compare]
  626 |   if (nread + 256 > allocated) {
      |                   ^
cc1: all warnings being treated as errors
make[1]: *** [Makefile:7591: tools/test-runner.o] Error 1
make[1]: *** Waiting for unfinished jobs....
make: *** [Makefile:4506: all] Error 2
##############################
Test: IncrementalBuild - FAIL
Desc: Incremental build with the patches in the series
Output:
[BlueZ] tools/test-runner: add option to start Pipewire inside the VM

tools/mgmt-tester.c: In function ‘main’:
tools/mgmt-tester.c:12655:5: note: variable tracking size limit exceeded with ‘-fvar-tracking-assignments’, retrying without
12655 | int main(int argc, char *argv[])
      |     ^~~~
tools/test-runner.c: In function ‘get_command_stdout’:
tools/test-runner.c:626:19: error: comparison of integer expressions of different signedness: ‘ssize_t’ {aka ‘long int’} and ‘size_t’ {aka ‘long unsigned int’} [-Werror=sign-compare]
  626 |   if (nread + 256 > allocated) {
      |                   ^
cc1: all warnings being treated as errors
make[1]: *** [Makefile:7591: tools/test-runner.o] Error 1
make[1]: *** Waiting for unfinished jobs....
make: *** [Makefile:4506: all] Error 2
##############################
Test: ScanBuild - FAIL
Desc: Run Scan Build
Output:

src/shared/ad.c:369:19: warning: Use of zero-allocated memory
                                buf[(*pos)++] = ad_type;
                                              ^
1 warning generated.
src/shared/gatt-client.c:451:21: warning: Use of memory after it is freed
        gatt_db_unregister(op->client->db, op->db_id);
                           ^~~~~~~~~~
src/shared/gatt-client.c:696:2: warning: Use of memory after it is freed
        discovery_op_complete(op, false, att_ecode);
        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/shared/gatt-client.c:993:2: warning: Use of memory after it is freed
        discovery_op_complete(op, success, att_ecode);
        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/shared/gatt-client.c:1099:2: warning: Use of memory after it is freed
        discovery_op_complete(op, success, att_ecode);
        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/shared/gatt-client.c:1291:2: warning: Use of memory after it is freed
        discovery_op_complete(op, success, att_ecode);
        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/shared/gatt-client.c:1356:2: warning: Use of memory after it is freed
        discovery_op_complete(op, success, att_ecode);
        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/shared/gatt-client.c:1631:6: warning: Use of memory after it is freed
        if (read_db_hash(op)) {
            ^~~~~~~~~~~~~~~~
src/shared/gatt-client.c:1636:2: warning: Use of memory after it is freed
        discover_all(op);
        ^~~~~~~~~~~~~~~~
src/shared/gatt-client.c:2140:6: warning: Use of memory after it is freed
        if (read_db_hash(op)) {
            ^~~~~~~~~~~~~~~~
src/shared/gatt-client.c:2148:8: warning: Use of memory after it is freed
                                                        discovery_op_ref(op),
                                                        ^~~~~~~~~~~~~~~~~~~~
src/shared/gatt-client.c:3236:2: warning: Use of memory after it is freed
        complete_write_long_op(req, success, 0, false);
        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/shared/gatt-client.c:3258:2: warning: Use of memory after it is freed
        request_unref(req);
        ^~~~~~~~~~~~~~~~~~
12 warnings generated.
src/shared/shell.c:1228:13: warning: Access to field 'options' results in a dereference of a null pointer (loaded from variable 'opt')
                        if (c != opt->options[index - offset].val) {
                                 ^~~~~~~~~~~~
1 warning generated.
src/shared/ad.c:369:19: warning: Use of zero-allocated memory
                                buf[(*pos)++] = ad_type;
                                              ^
1 warning generated.
src/shared/gatt-client.c:451:21: warning: Use of memory after it is freed
        gatt_db_unregister(op->client->db, op->db_id);
                           ^~~~~~~~~~
src/shared/gatt-client.c:696:2: warning: Use of memory after it is freed
        discovery_op_complete(op, false, att_ecode);
        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/shared/gatt-client.c:993:2: warning: Use of memory after it is freed
        discovery_op_complete(op, success, att_ecode);
        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/shared/gatt-client.c:1099:2: warning: Use of memory after it is freed
        discovery_op_complete(op, success, att_ecode);
        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/shared/gatt-client.c:1291:2: warning: Use of memory after it is freed
        discovery_op_complete(op, success, att_ecode);
        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/shared/gatt-client.c:1356:2: warning: Use of memory after it is freed
        discovery_op_complete(op, success, att_ecode);
        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/shared/gatt-client.c:1631:6: warning: Use of memory after it is freed
        if (read_db_hash(op)) {
            ^~~~~~~~~~~~~~~~
src/shared/gatt-client.c:1636:2: warning: Use of memory after it is freed
        discover_all(op);
        ^~~~~~~~~~~~~~~~
src/shared/gatt-client.c:2140:6: warning: Use of memory after it is freed
        if (read_db_hash(op)) {
            ^~~~~~~~~~~~~~~~
src/shared/gatt-client.c:2148:8: warning: Use of memory after it is freed
                                                        discovery_op_ref(op),
                                                        ^~~~~~~~~~~~~~~~~~~~
src/shared/gatt-client.c:3236:2: warning: Use of memory after it is freed
        complete_write_long_op(req, success, 0, false);
        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/shared/gatt-client.c:3258:2: warning: Use of memory after it is freed
        request_unref(req);
        ^~~~~~~~~~~~~~~~~~
12 warnings generated.
src/shared/shell.c:1228:13: warning: Access to field 'options' results in a dereference of a null pointer (loaded from variable 'opt')
                        if (c != opt->options[index - offset].val) {
                                 ^~~~~~~~~~~~
1 warning generated.
tools/hciattach.c:816:7: warning: Although the value stored to 'n' is used in the enclosing expression, the value is never actually read from 'n'
        if ((n = read_hci_event(fd, resp, 10)) < 0) {
             ^   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
tools/hciattach.c:864:7: warning: Although the value stored to 'n' is used in the enclosing expression, the value is never actually read from 'n'
        if ((n = read_hci_event(fd, resp, 4)) < 0) {
             ^   ~~~~~~~~~~~~~~~~~~~~~~~~~~~
tools/hciattach.c:886:8: warning: Although the value stored to 'n' is used in the enclosing expression, the value is never actually read from 'n'
                if ((n = read_hci_event(fd, resp, 10)) < 0) {
                     ^   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
tools/hciattach.c:908:7: warning: Although the value stored to 'n' is used in the enclosing expression, the value is never actually read from 'n'
        if ((n = read_hci_event(fd, resp, 4)) < 0) {
             ^   ~~~~~~~~~~~~~~~~~~~~~~~~~~~
tools/hciattach.c:929:7: warning: Although the value stored to 'n' is used in the enclosing expression, the value is never actually read from 'n'
        if ((n = read_hci_event(fd, resp, 4)) < 0) {
             ^   ~~~~~~~~~~~~~~~~~~~~~~~~~~~
tools/hciattach.c:973:7: warning: Although the value stored to 'n' is used in the enclosing expression, the value is never actually read from 'n'
        if ((n = read_hci_event(fd, resp, 6)) < 0) {
             ^   ~~~~~~~~~~~~~~~~~~~~~~~~~~~
6 warnings generated.
src/oui.c:50:2: warning: Value stored to 'hwdb' is never read
        hwdb = udev_hwdb_unref(hwdb);
        ^      ~~~~~~~~~~~~~~~~~~~~~
src/oui.c:53:2: warning: Value stored to 'udev' is never read
        udev = udev_unref(udev);
        ^      ~~~~~~~~~~~~~~~~
2 warnings generated.
tools/hcidump.c:180:9: warning: Potential leak of memory pointed to by 'dp'
                                if (fds[i].fd == sock)
                                    ^~~
tools/hcidump.c:248:17: warning: Assigned value is garbage or undefined
                                dh->ts_sec  = htobl(frm.ts.tv_sec);
                                            ^ ~~~~~~~~~~~~~~~~~~~~
tools/hcidump.c:326:9: warning: 1st function call argument is an uninitialized value
                                if (be32toh(dp.flags) & 0x02) {
                                    ^~~~~~~~~~~~~~~~~
/usr/include/endian.h:46:22: note: expanded from macro 'be32toh'
#  define be32toh(x) __bswap_32 (x)
                     ^~~~~~~~~~~~~~
tools/hcidump.c:341:20: warning: 1st function call argument is an uninitialized value
                                frm.data_len = be32toh(dp.len);
                                               ^~~~~~~~~~~~~~~
/usr/include/endian.h:46:22: note: expanded from macro 'be32toh'
#  define be32toh(x) __bswap_32 (x)
                     ^~~~~~~~~~~~~~
tools/hcidump.c:346:14: warning: 1st function call argument is an uninitialized value
                                opcode = be32toh(dp.flags) & 0xffff;
                                         ^~~~~~~~~~~~~~~~~
/usr/include/endian.h:46:22: note: expanded from macro 'be32toh'
#  define be32toh(x) __bswap_32 (x)
                     ^~~~~~~~~~~~~~
tools/hcidump.c:384:17: warning: Assigned value is garbage or undefined
                        frm.data_len = btohs(dh.len);
                                     ^ ~~~~~~~~~~~~~
tools/hcidump.c:394:11: warning: Assigned value is garbage or undefined
                frm.len = frm.data_len;
                        ^ ~~~~~~~~~~~~
tools/hcidump.c:398:9: warning: 1st function call argument is an uninitialized value
                        ts = be64toh(ph.ts);
                             ^~~~~~~~~~~~~~
/usr/include/endian.h:51:22: note: expanded from macro 'be64toh'
#  define be64toh(x) __bswap_64 (x)
                     ^~~~~~~~~~~~~~
tools/hcidump.c:403:13: warning: 1st function call argument is an uninitialized value
                        frm.in = be32toh(dp.flags) & 0x01;
                                 ^~~~~~~~~~~~~~~~~
/usr/include/endian.h:46:22: note: expanded from macro 'be32toh'
#  define be32toh(x) __bswap_32 (x)
                     ^~~~~~~~~~~~~~
tools/hcidump.c:408:11: warning: Assigned value is garbage or undefined
                        frm.in = dh.in;
                               ^ ~~~~~
tools/hcidump.c:437:7: warning: Null pointer passed to 1st parameter expecting 'nonnull'
        fd = open(file, open_flags, 0644);
             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
11 warnings generated.
tools/rfcomm.c:228:3: warning: Value stored to 'i' is never read
                i = execvp(cmdargv[0], cmdargv);
                ^   ~~~~~~~~~~~~~~~~~~~~~~~~~~~
tools/rfcomm.c:228:7: warning: Null pointer passed to 1st parameter expecting 'nonnull'
                i = execvp(cmdargv[0], cmdargv);
                    ^~~~~~~~~~~~~~~~~~~~~~~~~~~
tools/rfcomm.c:348:8: warning: Although the value stored to 'fd' is used in the enclosing expression, the value is never actually read from 'fd'
                if ((fd = open(devname, O_RDONLY | O_NOCTTY)) < 0) {
                     ^    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
tools/rfcomm.c:491:14: warning: Assigned value is garbage or undefined
        req.channel = raddr.rc_channel;
                    ^ ~~~~~~~~~~~~~~~~
tools/rfcomm.c:509:8: warning: Although the value stored to 'fd' is used in the enclosing expression, the value is never actually read from 'fd'
                if ((fd = open(devname, O_RDONLY | O_NOCTTY)) < 0) {
                     ^    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 warnings generated.
src/sdp-xml.c:126:10: warning: Assigned value is garbage or undefined
                buf[1] = data[i + 1];
                       ^ ~~~~~~~~~~~
src/sdp-xml.c:300:11: warning: Assigned value is garbage or undefined
                        buf[1] = data[i + 1];
                               ^ ~~~~~~~~~~~
src/sdp-xml.c:338:11: warning: Assigned value is garbage or undefined
                        buf[1] = data[i + 1];
                               ^ ~~~~~~~~~~~
3 warnings generated.
tools/ciptool.c:350:7: warning: 5th function call argument is an uninitialized value
        sk = do_connect(ctl, dev_id, &src, &dst, psm, (1 << CMTP_LOOPBACK));
             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 warning generated.
tools/sdptool.c:941:26: warning: Result of 'malloc' is converted to a pointer of type 'uint32_t', which is incompatible with sizeof operand type 'int'
                        uint32_t *value_int = malloc(sizeof(int));
                        ~~~~~~~~~~            ^~~~~~ ~~~~~~~~~~~
tools/sdptool.c:980:4: warning: 1st function call argument is an uninitialized value
                        free(allocArray[i]);
                        ^~~~~~~~~~~~~~~~~~~
tools/sdptool.c:3777:2: warning: Potential leak of memory pointed to by 'si.name'
        return add_service(0, &si);
        ^~~~~~~~~~~~~~~~~~~~~~~~~~
tools/sdptool.c:4112:4: warning: Potential leak of memory pointed to by 'context.svc'
                        return -1;
                        ^~~~~~~~~
4 warnings generated.
tools/avtest.c:224:5: warning: Value stored to 'len' is never read
                                len = write(sk, buf, 3);
                                ^     ~~~~~~~~~~~~~~~~~
tools/avtest.c:234:5: warning: Value stored to 'len' is never read
                                len = write(sk, buf, 4);
                                ^     ~~~~~~~~~~~~~~~~~
tools/avtest.c:243:5: warning: Value stored to 'len' is never read
                                len = write(sk, buf, 3);
                                ^     ~~~~~~~~~~~~~~~~~
tools/avtest.c:257:5: warning: Value stored to 'len' is never read
                                len = write(sk, buf,
                                ^     ~~~~~~~~~~~~~~
tools/avtest.c:264:5: warning: Value stored to 'len' is never read
                                len = write(sk, buf,
                                ^     ~~~~~~~~~~~~~~
tools/avtest.c:271:5: warning: Value stored to 'len' is never read
                                len = write(sk, buf,
                                ^     ~~~~~~~~~~~~~~
tools/avtest.c:278:5: warning: Value stored to 'len' is never read
                                len = write(sk, buf,
                                ^     ~~~~~~~~~~~~~~
tools/avtest.c:289:5: warning: Value stored to 'len' is never read
                                len = write(sk, buf, 4);
                                ^     ~~~~~~~~~~~~~~~~~
tools/avtest.c:293:5: warning: Value stored to 'len' is never read
                                len = write(sk, buf, 2);
                                ^     ~~~~~~~~~~~~~~~~~
tools/avtest.c:302:5: warning: Value stored to 'len' is never read
                                len = write(sk, buf, 3);
                                ^     ~~~~~~~~~~~~~~~~~
tools/avtest.c:306:5: warning: Value stored to 'len' is never read
                                len = write(sk, buf, 2);
                                ^     ~~~~~~~~~~~~~~~~~
tools/avtest.c:315:5: warning: Value stored to 'len' is never read
                                len = write(sk, buf, 3);
                                ^     ~~~~~~~~~~~~~~~~~
tools/avtest.c:322:5: warning: Value stored to 'len' is never read
                                len = write(sk, buf, 2);
                                ^     ~~~~~~~~~~~~~~~~~
tools/avtest.c:344:5: warning: Value stored to 'len' is never read
                                len = write(sk, buf, 4);
                                ^     ~~~~~~~~~~~~~~~~~
tools/avtest.c:348:5: warning: Value stored to 'len' is never read
                                len = write(sk, buf, 2);
                                ^     ~~~~~~~~~~~~~~~~~
tools/avtest.c:357:5: warning: Value stored to 'len' is never read
                                len = write(sk, buf, 3);
                                ^     ~~~~~~~~~~~~~~~~~
tools/avtest.c:361:5: warning: Value stored to 'len' is never read
                                len = write(sk, buf, 2);
                                ^     ~~~~~~~~~~~~~~~~~
tools/avtest.c:374:5: warning: Value stored to 'len' is never read
                                len = write(sk, buf, 4);
                                ^     ~~~~~~~~~~~~~~~~~
tools/avtest.c:378:5: warning: Value stored to 'len' is never read
                                len = write(sk, buf, 2);
                                ^     ~~~~~~~~~~~~~~~~~
tools/avtest.c:385:4: warning: Value stored to 'len' is never read
                        len = write(sk, buf, 2);
                        ^     ~~~~~~~~~~~~~~~~~
tools/avtest.c:395:4: warning: Value stored to 'len' is never read
                        len = write(sk, buf, 2);
                        ^     ~~~~~~~~~~~~~~~~~
tools/avtest.c:559:3: warning: Value stored to 'len' is never read
                len = write(sk, buf, 2);
                ^     ~~~~~~~~~~~~~~~~~
tools/avtest.c:567:3: warning: Value stored to 'len' is never read
                len = write(sk, buf, invalid ? 2 : 3);
                ^     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
tools/avtest.c:581:3: warning: Value stored to 'len' is never read
                len = write(sk, buf, 4 + sizeof(media_transport));
                ^     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
tools/avtest.c:594:3: warning: Value stored to 'len' is never read
                len = write(sk, buf, 3);
                ^     ~~~~~~~~~~~~~~~~~
tools/avtest.c:604:3: warning: Value stored to 'len' is never read
                len = write(sk, buf, 3);
                ^     ~~~~~~~~~~~~~~~~~
tools/avtest.c:616:3: warning: Value stored to 'len' is never read
                len = write(sk, buf, 3);
                ^     ~~~~~~~~~~~~~~~~~
tools/avtest.c:631:3: warning: Value stored to 'len' is never read
                len = write(sk, buf, 3);
                ^     ~~~~~~~~~~~~~~~~~
tools/avtest.c:643:3: warning: Value stored to 'len' is never read
                len = write(sk, buf, 3);
                ^     ~~~~~~~~~~~~~~~~~
tools/avtest.c:652:3: warning: Value stored to 'len' is never read
                len = write(sk, buf, 3);
                ^     ~~~~~~~~~~~~~~~~~
tools/avtest.c:659:3: warning: Value stored to 'len' is never read
                len = write(sk, buf, 2);
                ^     ~~~~~~~~~~~~~~~~~
tools/avtest.c:695:2: warning: Value stored to 'len' is never read
        len = write(sk, buf, AVCTP_HEADER_LENGTH + sizeof(play_pressed));
        ^     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
32 warnings generated.
tools/btproxy.c:836:15: warning: Null pointer passed to 1st parameter expecting 'nonnull'
                        tcp_port = atoi(optarg);
                                   ^~~~~~~~~~~~
tools/btproxy.c:839:8: warning: Null pointer passed to 1st parameter expecting 'nonnull'
                        if (strlen(optarg) > 3 && !strncmp(optarg, "hci", 3))
                            ^~~~~~~~~~~~~~
2 warnings generated.
tools/create-image.c:76:3: warning: Value stored to 'fd' is never read
                fd = -1;
                ^    ~~
tools/create-image.c:84:3: warning: Value stored to 'fd' is never read
                fd = -1;
                ^    ~~
tools/create-image.c:92:3: warning: Value stored to 'fd' is never read
                fd = -1;
                ^    ~~
tools/create-image.c:105:2: warning: Value stored to 'fd' is never read
        fd = -1;
        ^    ~~
4 warnings generated.
tools/btgatt-client.c:1597:2: warning: Value stored to 'argv' is never read
        argv += optind;
        ^       ~~~~~~
1 warning generated.
tools/test-runner.c: In function ‘get_command_stdout’:
tools/test-runner.c:626:19: error: comparison of integer expressions of different signedness: ‘ssize_t’ {aka ‘long int’} and ‘size_t’ {aka ‘long unsigned int’} [-Werror=sign-compare]
  626 |   if (nread + 256 > allocated) {
      |                   ^
tools/btgatt-server.c:1212:2: warning: Value stored to 'argv' is never read
        argv -= optind;
        ^       ~~~~~~
1 warning generated.
cc1: all warnings being treated as errors
make[1]: *** [Makefile:7591: tools/test-runner.o] Error 1
make[1]: *** Waiting for unfinished jobs....
tools/check-selftest.c:42:3: warning: Value stored to 'ptr' is never read
                ptr = fgets(result, sizeof(result), fp);
                ^     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 warning generated.
make: *** [Makefile:4506: all] Error 2


---
Regards,
Linux Bluetooth
Luiz Augusto von Dentz April 20, 2023, 7:49 p.m. UTC | #2
Hi Pauli,

On Sat, Apr 15, 2023 at 7:44 AM Pauli Virtanen <pav@iki.fi> wrote:
>
> Add option for launching Pipewire inside the VM to serve Bluetooth
> endpoints, which can be used in tests.
>
> If daemon and emulator were also started, wait for the endpoints to
> appear.
> ---
>
> Notes:
>     An example how you can launch Pipewire to serve Bluetooth endpoints.
>
>  tools/test-runner.c | 247 +++++++++++++++++++++++++++++++++++++++++++-
>  1 file changed, 242 insertions(+), 5 deletions(-)
>
> diff --git a/tools/test-runner.c b/tools/test-runner.c
> index 6660ea8de..d416f80ed 100644
> --- a/tools/test-runner.c
> +++ b/tools/test-runner.c
> @@ -51,6 +51,7 @@ static bool start_dbus_session;
>  static bool start_daemon = false;
>  static bool start_emulator = false;
>  static bool start_monitor = false;
> +static bool start_pipewire;
>  static int num_devs = 0;
>  static const char *qemu_binary = NULL;
>  static const char *kernel_image = NULL;
> @@ -252,13 +253,13 @@ static void start_qemu(void)
>                                 "acpi=off pci=noacpi noapic quiet ro init=%s "
>                                 "TESTHOME=%s TESTDBUS=%u TESTDAEMON=%u "
>                                 "TESTDBUSSESSION=%u XDG_RUNTIME_DIR=/run/user/0 "
> -                               "TESTAUDIO=%u "
> +                               "TESTAUDIO=%u TESTPIPEWIRE=%u "

I'd just reuse TESTAUDIO instead of introducing yet another
environment variable, that should probably check if it shall run
pulseaudio or pipewire depending on what is available in the system.

>                                 "TESTMONITOR=%u TESTEMULATOR=%u TESTDEVS=%d "
>                                 "TESTAUTO=%u TESTARGS=\'%s\'",
>                                 initcmd, cwd, start_dbus, start_daemon,
>                                 start_dbus_session, audio_support,
> -                               start_monitor, start_emulator, num_devs,
> -                               run_auto, testargs);
> +                               start_pipewire, start_monitor, start_emulator,
> +                               num_devs, run_auto, testargs);
>
>         argv = alloca(sizeof(qemu_argv) +
>                                 (audio_support ? 4 : 0) +
> @@ -606,6 +607,207 @@ static pid_t start_bluetooth_daemon(const char *home)
>         return pid;
>  }
>
> +static char *get_command_stdout(char *command, size_t *size)
> +{
> +       char *buf = NULL;
> +       ssize_t nread = 0;
> +       size_t allocated = 0;
> +       int ret;
> +       FILE *f;
> +
> +       f = popen(command, "re");
> +       if (!f)
> +               return NULL;
> +
> +       while (1) {
> +               size_t res;
> +               void *p;
> +
> +               if (nread + 256 > allocated) {
> +                       allocated += allocated + 256;
> +                       p = realloc(buf, allocated);
> +                       if (!p) {
> +                               nread = -1;
> +                               break;
> +                       }
> +                       buf = p;
> +               }
> +
> +               res = fread(buf + nread, 1, allocated - nread - 1, f);
> +               if (!res)
> +                       break;
> +               nread += res;
> +       }
> +
> +       ret = pclose(f);
> +       if (ret < 0 || !WIFEXITED(ret) || WEXITSTATUS(ret) != 0) {
> +               printf("%s failed\n", command);
> +               nread = -1;
> +       }
> +
> +       if (nread >= 0) {
> +               buf[nread] = 0;
> +               if (size)
> +                       *size = nread;
> +       } else {
> +               free(buf);
> +               buf = NULL;
> +       }
> +
> +       return buf;
> +}
> +
> +static void start_pipewire_daemons(pid_t *pipewire_pid, pid_t *wireplumber_pid)
> +{
> +       static const char *const daemons[2] = {
> +               "/usr/bin/pipewire",
> +               "/usr/bin/wireplumber"
> +       };
> +       static const char *const dirs[] = {
> +               "/run/pw",
> +               "/run/pw/state",
> +               "/run/pw/wireplumber",
> +               "/run/pw/wireplumber/bluetooth.lua.d",
> +               "/run/pw/wireplumber/main.lua.d",
> +               NULL
> +       };
> +       FILE *f;
> +       pid_t *pids[2] = {pipewire_pid, wireplumber_pid};
> +       char *envp[5];
> +       int i;
> +
> +       for (i = 0; dirs[i]; ++i) {
> +               if (mkdir(dirs[i], 0755) < 0) {
> +                       perror("Failed to create directory");
> +                       return;
> +               }
> +       }
> +
> +       /* Enable only Bluetooth part, disable whatever requires user DBus */
> +       f = fopen("/run/pw/wireplumber/main.lua.d/51-custom.lua", "w");
> +       if (!f) {
> +               perror("Failed to create Pipewire main config");
> +               return;
> +       }
> +       fprintf(f, "alsa_monitor.enabled = false\n"
> +               "v4l2_monitor.enabled = false\n"
> +               "libcamera_monitor.enabled = false\n"
> +               "default_access.properties[\"enable-flatpak-portal\"]"
> +               " = false\n");
> +       fclose(f);

I'd put this into its own function to make it clear that this is
setting up the configuration e.g.:

https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/tools/test-runner.c#n450

> +       f = fopen("/run/pw/wireplumber/bluetooth.lua.d/51-custom.lua", "w");
> +       if (!f) {
> +               perror("Failed to create Pipewire bluetooth config");
> +               return;
> +       }
> +       fprintf(f, "bluez_monitor.properties[\"with-logind\"] = false\n"
> +               "bluez_midi_monitor.enabled = false\n");
> +       fclose(f);
> +
> +       /* Launch daemons */
> +       for (i = 0; i < 2; ++i)
> +               *pids[i] = -1;
> +
> +       envp[0] = "DBUS_SYSTEM_BUS_ADDRESS=unix:"
> +                 "path=/run/dbus/system_bus_socket";
> +       envp[1] = "XDG_STATE_HOME=/run/pw/state";
> +       envp[2] = "XDG_CONFIG_HOME=/run/pw";
> +       envp[3] = "XDG_RUNTIME_DIR=/run/pw";
> +       envp[4] = NULL;
> +
> +       for (i = 0; i < 2; ++i) {
> +               const char *daemon = daemons[i];
> +               char *argv[2];
> +               pid_t pid;
> +
> +               printf("Starting Pipewire daemon %s\n", daemon);
> +
> +               argv[0] = (char *) daemon;
> +               argv[1] = NULL;
> +
> +               pid = fork();
> +               if (pid < 0) {
> +                       perror("Failed to fork new process");
> +                       return;
> +               }
> +
> +               if (pid == 0) {
> +                       execve(argv[0], argv, envp);
> +                       exit(EXIT_SUCCESS);
> +               }
> +
> +               *pids[i] = pid;
> +
> +               printf("Pipewire daemon process %d created\n", pid);
> +       }
> +
> +       /* Tell pipewire clients where the socket is */
> +       setenv("PIPEWIRE_RUNTIME_DIR", "/run/pw", 1);
> +
> +       /* Wait until daemons completely started */
> +       for (i = 0; i < 6; ++i) {
> +               char *buf;
> +
> +               if (i > 0) {
> +                       printf("Wait for Pipewire ready...\n");
> +                       usleep(500000);
> +               }
> +
> +               buf = get_command_stdout("/usr/bin/pw-dump", NULL);
> +               if (!buf)
> +                       continue;

Don't we have a file or something similar to
/run/dbus/system_bus_socket that indicates pw is running? Checking
dump file seems a little overkill to me.

> +
> +               if (strstr(buf, "WirePlumber")) {
> +                       printf("Pipewire ready\n");
> +                       free(buf);
> +                       break;
> +               }
> +
> +               free(buf);
> +       }
> +       if (i == 6)
> +               goto fail;
> +
> +       if (!start_emulator || !start_daemon)
> +               return;
> +
> +       /* Wait for Bluetooth endpoints */
> +       for (i = 0; i < 6; ++i) {
> +               char *buf;
> +
> +               if (i > 0) {
> +                       printf("Wait for endpoints...\n");
> +                       usleep(500000);
> +               }
> +
> +               buf = get_command_stdout("/usr/bin/bluetoothctl show", NULL);
> +               if (!buf)
> +                       continue;
> +
> +               if (strstr(buf, "0000110b-0000-1000-8000-00805f9b34fb") ||
> +                   strstr(buf, "00001850-0000-1000-8000-00805f9b34fb")) {
> +                       printf("Pipewire endpoints ready\n");
> +                       free(buf);
> +                       break;
> +               }
> +
> +               free(buf);
> +       }
> +       if (i == 6)
> +               goto fail;

Id skip this part, the endpoints registration depends on how
bluetoothd:main.conf  is configured so we shouldn't really expect
certain UUIDs like above.

> +       return;
> +
> +fail:
> +       for (i = 0; i < 2; ++i)
> +               if (*pids[i] > 0)
> +                       kill(*pids[i], SIGTERM);
> +
> +       printf("Pipewire daemons not running properly\n");
> +       return;
> +}
> +
>  static const char *test_table[] = {
>         "mgmt-tester",
>         "smp-tester",
> @@ -807,7 +1009,7 @@ static void run_command(char *cmdname, char *home)
>         int pos = 0, idx = 0;
>         int serial_fd;
>         pid_t pid, dbus_pid, daemon_pid, monitor_pid, emulator_pid,
> -             dbus_session_pid, udevd_pid;
> +             dbus_session_pid, udevd_pid, pw_pid, wp_pid;
>
>         if (!home) {
>                 perror("Invalid parameter: TESTHOME");
> @@ -860,6 +1062,13 @@ static void run_command(char *cmdname, char *home)
>         else
>                 emulator_pid = -1;
>
> +       if (start_pipewire) {
> +               start_pipewire_daemons(&pw_pid, &wp_pid);
> +       } else {
> +               pw_pid = -1;
> +               wp_pid = -1;
> +       }
> +
>  start_next:
>         if (run_auto) {
>                 if (chdir(home + 5) < 0) {
> @@ -966,6 +1175,16 @@ start_next:
>                         udevd_pid = -1;
>                 }
>
> +               if (corpse == pw_pid) {
> +                       printf("pipewire terminated\n");
> +                       pw_pid = -1;
> +               }
> +
> +               if (corpse == wp_pid) {
> +                       printf("wireplumber terminated\n");
> +                       wp_pid = -1;
> +               }
> +
>                 if (corpse == pid)
>                         break;
>         }
> @@ -975,6 +1194,12 @@ start_next:
>                 goto start_next;
>         }
>
> +       if (wp_pid > 0)
> +               kill(wp_pid, SIGTERM);
> +
> +       if (pw_pid > 0)
> +               kill(pw_pid, SIGTERM);
> +
>         if (daemon_pid > 0)
>                 kill(daemon_pid, SIGTERM);
>
> @@ -1079,6 +1304,12 @@ static void run_tests(void)
>                 audio_support = true;
>         }
>
> +       ptr = strstr(cmdline, "TESTPIPEWIRE=1");
> +       if (ptr) {
> +               printf("Pipewire requested\n");
> +               start_pipewire = true;
> +       }
> +
>         ptr = strstr(cmdline, "TESTHOME=");
>         if (ptr) {
>                 home = ptr + 4;
> @@ -1106,6 +1337,7 @@ static void usage(void)
>                 "\t-q, --qemu <path>      QEMU binary\n"
>                 "\t-k, --kernel <image>   Kernel image (bzImage)\n"
>                 "\t-A, --audio            Add audio support\n"
> +               "\t-P, --pipewire         Start pipewire\n"
>                 "\t-h, --help             Show help options\n");
>  }
>
> @@ -1121,6 +1353,7 @@ static const struct option main_options[] = {
>         { "qemu",    required_argument, NULL, 'q' },
>         { "kernel",  required_argument, NULL, 'k' },
>         { "audio",   no_argument,       NULL, 'A' },
> +       { "pipewire", no_argument,      NULL, 'P' },
>         { "version", no_argument,       NULL, 'v' },
>         { "help",    no_argument,       NULL, 'h' },
>         { }
> @@ -1140,7 +1373,7 @@ int main(int argc, char *argv[])
>         for (;;) {
>                 int opt;
>
> -               opt = getopt_long(argc, argv, "aubdslmq:k:Avh", main_options,
> +               opt = getopt_long(argc, argv, "aubdslmq:k:APvh", main_options,
>                                                                 NULL);
>                 if (opt < 0)
>                         break;
> @@ -1177,6 +1410,10 @@ int main(int argc, char *argv[])
>                 case 'A':
>                         audio_support = true;
>                         break;
> +               case 'P':
> +                       start_dbus = true;
> +                       start_pipewire = true;
> +                       break;
>                 case 'v':
>                         printf("%s\n", VERSION);
>                         return EXIT_SUCCESS;
> --
> 2.39.2
>
Pauli Virtanen April 22, 2023, 12:11 p.m. UTC | #3
Hi Luiz,

to, 2023-04-20 kello 12:49 -0700, Luiz Augusto von Dentz kirjoitti:
> Hi Pauli,
> 
> On Sat, Apr 15, 2023 at 7:44 AM Pauli Virtanen <pav@iki.fi> wrote:
> > 
> > Add option for launching Pipewire inside the VM to serve Bluetooth
> > endpoints, which can be used in tests.
> > 
> > If daemon and emulator were also started, wait for the endpoints to
> > appear.
> > ---
> > 
> > Notes:
> >     An example how you can launch Pipewire to serve Bluetooth endpoints.
> > 
> >  tools/test-runner.c | 247 +++++++++++++++++++++++++++++++++++++++++++-
> >  1 file changed, 242 insertions(+), 5 deletions(-)
> > 
> > diff --git a/tools/test-runner.c b/tools/test-runner.c
> > index 6660ea8de..d416f80ed 100644
> > --- a/tools/test-runner.c
> > +++ b/tools/test-runner.c
> > @@ -51,6 +51,7 @@ static bool start_dbus_session;
> >  static bool start_daemon = false;
> >  static bool start_emulator = false;
> >  static bool start_monitor = false;
> > +static bool start_pipewire;
> >  static int num_devs = 0;
> >  static const char *qemu_binary = NULL;
> >  static const char *kernel_image = NULL;
> > @@ -252,13 +253,13 @@ static void start_qemu(void)
> >                                 "acpi=off pci=noacpi noapic quiet ro init=%s "
> >                                 "TESTHOME=%s TESTDBUS=%u TESTDAEMON=%u "
> >                                 "TESTDBUSSESSION=%u XDG_RUNTIME_DIR=/run/user/0 "
> > -                               "TESTAUDIO=%u "
> > +                               "TESTAUDIO=%u TESTPIPEWIRE=%u "
> 
> I'd just reuse TESTAUDIO instead of introducing yet another
> environment variable, that should probably check if it shall run
> pulseaudio or pipewire depending on what is available in the system.

TESTAUDIO also adds a virtual soundcard to the VM. Is this needed for
something, if tests and the audio daemon runs inside the VM they should
not need such access to soundcards outside the VM?

The tester.config doesn't enable ALSA, so it won't do anything in that
configuration. The VM also fails to boot for me with -A enabled for
that kernel.

IIUC, this and running udevd are not currently used for something, and
if so I'll remove those.

> 
> >                                 "TESTMONITOR=%u TESTEMULATOR=%u TESTDEVS=%d "
> >                                 "TESTAUTO=%u TESTARGS=\'%s\'",
> >                                 initcmd, cwd, start_dbus, start_daemon,
> >                                 start_dbus_session, audio_support,
> > -                               start_monitor, start_emulator, num_devs,
> > -                               run_auto, testargs);
> > +                               start_pipewire, start_monitor, start_emulator,
> > +                               num_devs, run_auto, testargs);
> > 
> >         argv = alloca(sizeof(qemu_argv) +
> >                                 (audio_support ? 4 : 0) +
> > @@ -606,6 +607,207 @@ static pid_t start_bluetooth_daemon(const char *home)
> >         return pid;
> >  }
> > 
> > +static char *get_command_stdout(char *command, size_t *size)
> > +{
> > +       char *buf = NULL;
> > +       ssize_t nread = 0;
> > +       size_t allocated = 0;
> > +       int ret;
> > +       FILE *f;
> > +
> > +       f = popen(command, "re");
> > +       if (!f)
> > +               return NULL;
> > +
> > +       while (1) {
> > +               size_t res;
> > +               void *p;
> > +
> > +               if (nread + 256 > allocated) {
> > +                       allocated += allocated + 256;
> > +                       p = realloc(buf, allocated);
> > +                       if (!p) {
> > +                               nread = -1;
> > +                               break;
> > +                       }
> > +                       buf = p;
> > +               }
> > +
> > +               res = fread(buf + nread, 1, allocated - nread - 1, f);
> > +               if (!res)
> > +                       break;
> > +               nread += res;
> > +       }
> > +
> > +       ret = pclose(f);
> > +       if (ret < 0 || !WIFEXITED(ret) || WEXITSTATUS(ret) != 0) {
> > +               printf("%s failed\n", command);
> > +               nread = -1;
> > +       }
> > +
> > +       if (nread >= 0) {
> > +               buf[nread] = 0;
> > +               if (size)
> > +                       *size = nread;
> > +       } else {
> > +               free(buf);
> > +               buf = NULL;
> > +       }
> > +
> > +       return buf;
> > +}
> > +
> > +static void start_pipewire_daemons(pid_t *pipewire_pid, pid_t *wireplumber_pid)
> > +{
> > +       static const char *const daemons[2] = {
> > +               "/usr/bin/pipewire",
> > +               "/usr/bin/wireplumber"
> > +       };
> > +       static const char *const dirs[] = {
> > +               "/run/pw",
> > +               "/run/pw/state",
> > +               "/run/pw/wireplumber",
> > +               "/run/pw/wireplumber/bluetooth.lua.d",
> > +               "/run/pw/wireplumber/main.lua.d",
> > +               NULL
> > +       };
> > +       FILE *f;
> > +       pid_t *pids[2] = {pipewire_pid, wireplumber_pid};
> > +       char *envp[5];
> > +       int i;
> > +
> > +       for (i = 0; dirs[i]; ++i) {
> > +               if (mkdir(dirs[i], 0755) < 0) {
> > +                       perror("Failed to create directory");
> > +                       return;
> > +               }
> > +       }
> > +
> > +       /* Enable only Bluetooth part, disable whatever requires user DBus */
> > +       f = fopen("/run/pw/wireplumber/main.lua.d/51-custom.lua", "w");
> > +       if (!f) {
> > +               perror("Failed to create Pipewire main config");
> > +               return;
> > +       }
> > +       fprintf(f, "alsa_monitor.enabled = false\n"
> > +               "v4l2_monitor.enabled = false\n"
> > +               "libcamera_monitor.enabled = false\n"
> > +               "default_access.properties[\"enable-flatpak-portal\"]"
> > +               " = false\n");
> > +       fclose(f);
> 
> I'd put this into its own function to make it clear that this is
> setting up the configuration e.g.:
> 
> https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/tools/test-runner.c#n450
> 
> > +       f = fopen("/run/pw/wireplumber/bluetooth.lua.d/51-custom.lua", "w");
> > +       if (!f) {
> > +               perror("Failed to create Pipewire bluetooth config");
> > +               return;
> > +       }
> > +       fprintf(f, "bluez_monitor.properties[\"with-logind\"] = false\n"
> > +               "bluez_midi_monitor.enabled = false\n");
> > +       fclose(f);
> > +
> > +       /* Launch daemons */
> > +       for (i = 0; i < 2; ++i)
> > +               *pids[i] = -1;
> > +
> > +       envp[0] = "DBUS_SYSTEM_BUS_ADDRESS=unix:"
> > +                 "path=/run/dbus/system_bus_socket";
> > +       envp[1] = "XDG_STATE_HOME=/run/pw/state";
> > +       envp[2] = "XDG_CONFIG_HOME=/run/pw";
> > +       envp[3] = "XDG_RUNTIME_DIR=/run/pw";
> > +       envp[4] = NULL;
> > +
> > +       for (i = 0; i < 2; ++i) {
> > +               const char *daemon = daemons[i];
> > +               char *argv[2];
> > +               pid_t pid;
> > +
> > +               printf("Starting Pipewire daemon %s\n", daemon);
> > +
> > +               argv[0] = (char *) daemon;
> > +               argv[1] = NULL;
> > +
> > +               pid = fork();
> > +               if (pid < 0) {
> > +                       perror("Failed to fork new process");
> > +                       return;
> > +               }
> > +
> > +               if (pid == 0) {
> > +                       execve(argv[0], argv, envp);
> > +                       exit(EXIT_SUCCESS);
> > +               }
> > +
> > +               *pids[i] = pid;
> > +
> > +               printf("Pipewire daemon process %d created\n", pid);
> > +       }
> > +
> > +       /* Tell pipewire clients where the socket is */
> > +       setenv("PIPEWIRE_RUNTIME_DIR", "/run/pw", 1);
> > +
> > +       /* Wait until daemons completely started */
> > +       for (i = 0; i < 6; ++i) {
> > +               char *buf;
> > +
> > +               if (i > 0) {
> > +                       printf("Wait for Pipewire ready...\n");
> > +                       usleep(500000);
> > +               }
> > +
> > +               buf = get_command_stdout("/usr/bin/pw-dump", NULL);
> > +               if (!buf)
> > +                       continue;
> 
> Don't we have a file or something similar to
> /run/dbus/system_bus_socket that indicates pw is running? Checking
> dump file seems a little overkill to me.

You can stat for /run/pipewire-0

The daemon running doesn't mean you have a sound devices yet, though,
so if tests need them they need to wait for them themselves.

> 
> > +
> > +               if (strstr(buf, "WirePlumber")) {
> > +                       printf("Pipewire ready\n");
> > +                       free(buf);
> > +                       break;
> > +               }
> > +
> > +               free(buf);
> > +       }
> > +       if (i == 6)
> > +               goto fail;
> > +
> > +       if (!start_emulator || !start_daemon)
> > +               return;
> > +
> > +       /* Wait for Bluetooth endpoints */
> > +       for (i = 0; i < 6; ++i) {
> > +               char *buf;
> > +
> > +               if (i > 0) {
> > +                       printf("Wait for endpoints...\n");
> > +                       usleep(500000);
> > +               }
> > +
> > +               buf = get_command_stdout("/usr/bin/bluetoothctl show", NULL);
> > +               if (!buf)
> > +                       continue;
> > +
> > +               if (strstr(buf, "0000110b-0000-1000-8000-00805f9b34fb") ||
> > +                   strstr(buf, "00001850-0000-1000-8000-00805f9b34fb")) {
> > +                       printf("Pipewire endpoints ready\n");
> > +                       free(buf);
> > +                       break;
> > +               }
> > +
> > +               free(buf);
> > +       }
> > +       if (i == 6)
> > +               goto fail;
> 
> Id skip this part, the endpoints registration depends on how
> bluetoothd:main.conf  is configured so we shouldn't really expect
> certain UUIDs like above.

Ok, in principle the tests that need endpoints can wait for them.

> 
> > +       return;
> > +
> > +fail:
> > +       for (i = 0; i < 2; ++i)
> > +               if (*pids[i] > 0)
> > +                       kill(*pids[i], SIGTERM);
> > +
> > +       printf("Pipewire daemons not running properly\n");
> > +       return;
> > +}
> > +
> >  static const char *test_table[] = {
> >         "mgmt-tester",
> >         "smp-tester",
> > @@ -807,7 +1009,7 @@ static void run_command(char *cmdname, char *home)
> >         int pos = 0, idx = 0;
> >         int serial_fd;
> >         pid_t pid, dbus_pid, daemon_pid, monitor_pid, emulator_pid,
> > -             dbus_session_pid, udevd_pid;
> > +             dbus_session_pid, udevd_pid, pw_pid, wp_pid;
> > 
> >         if (!home) {
> >                 perror("Invalid parameter: TESTHOME");
> > @@ -860,6 +1062,13 @@ static void run_command(char *cmdname, char *home)
> >         else
> >                 emulator_pid = -1;
> > 
> > +       if (start_pipewire) {
> > +               start_pipewire_daemons(&pw_pid, &wp_pid);
> > +       } else {
> > +               pw_pid = -1;
> > +               wp_pid = -1;
> > +       }
> > +
> >  start_next:
> >         if (run_auto) {
> >                 if (chdir(home + 5) < 0) {
> > @@ -966,6 +1175,16 @@ start_next:
> >                         udevd_pid = -1;
> >                 }
> > 
> > +               if (corpse == pw_pid) {
> > +                       printf("pipewire terminated\n");
> > +                       pw_pid = -1;
> > +               }
> > +
> > +               if (corpse == wp_pid) {
> > +                       printf("wireplumber terminated\n");
> > +                       wp_pid = -1;
> > +               }
> > +
> >                 if (corpse == pid)
> >                         break;
> >         }
> > @@ -975,6 +1194,12 @@ start_next:
> >                 goto start_next;
> >         }
> > 
> > +       if (wp_pid > 0)
> > +               kill(wp_pid, SIGTERM);
> > +
> > +       if (pw_pid > 0)
> > +               kill(pw_pid, SIGTERM);
> > +
> >         if (daemon_pid > 0)
> >                 kill(daemon_pid, SIGTERM);
> > 
> > @@ -1079,6 +1304,12 @@ static void run_tests(void)
> >                 audio_support = true;
> >         }
> > 
> > +       ptr = strstr(cmdline, "TESTPIPEWIRE=1");
> > +       if (ptr) {
> > +               printf("Pipewire requested\n");
> > +               start_pipewire = true;
> > +       }
> > +
> >         ptr = strstr(cmdline, "TESTHOME=");
> >         if (ptr) {
> >                 home = ptr + 4;
> > @@ -1106,6 +1337,7 @@ static void usage(void)
> >                 "\t-q, --qemu <path>      QEMU binary\n"
> >                 "\t-k, --kernel <image>   Kernel image (bzImage)\n"
> >                 "\t-A, --audio            Add audio support\n"
> > +               "\t-P, --pipewire         Start pipewire\n"
> >                 "\t-h, --help             Show help options\n");
> >  }
> > 
> > @@ -1121,6 +1353,7 @@ static const struct option main_options[] = {
> >         { "qemu",    required_argument, NULL, 'q' },
> >         { "kernel",  required_argument, NULL, 'k' },
> >         { "audio",   no_argument,       NULL, 'A' },
> > +       { "pipewire", no_argument,      NULL, 'P' },
> >         { "version", no_argument,       NULL, 'v' },
> >         { "help",    no_argument,       NULL, 'h' },
> >         { }
> > @@ -1140,7 +1373,7 @@ int main(int argc, char *argv[])
> >         for (;;) {
> >                 int opt;
> > 
> > -               opt = getopt_long(argc, argv, "aubdslmq:k:Avh", main_options,
> > +               opt = getopt_long(argc, argv, "aubdslmq:k:APvh", main_options,
> >                                                                 NULL);
> >                 if (opt < 0)
> >                         break;
> > @@ -1177,6 +1410,10 @@ int main(int argc, char *argv[])
> >                 case 'A':
> >                         audio_support = true;
> >                         break;
> > +               case 'P':
> > +                       start_dbus = true;
> > +                       start_pipewire = true;
> > +                       break;
> >                 case 'v':
> >                         printf("%s\n", VERSION);
> >                         return EXIT_SUCCESS;
> > --
> > 2.39.2
> > 
> 
>
Luiz Augusto von Dentz May 4, 2023, 7:09 p.m. UTC | #4
Hi Pauli,

On Sat, Apr 22, 2023 at 5:12 AM Pauli Virtanen <pav@iki.fi> wrote:
>
> Hi Luiz,
>
> to, 2023-04-20 kello 12:49 -0700, Luiz Augusto von Dentz kirjoitti:
> > Hi Pauli,
> >
> > On Sat, Apr 15, 2023 at 7:44 AM Pauli Virtanen <pav@iki.fi> wrote:
> > >
> > > Add option for launching Pipewire inside the VM to serve Bluetooth
> > > endpoints, which can be used in tests.
> > >
> > > If daemon and emulator were also started, wait for the endpoints to
> > > appear.
> > > ---
> > >
> > > Notes:
> > >     An example how you can launch Pipewire to serve Bluetooth endpoints.
> > >
> > >  tools/test-runner.c | 247 +++++++++++++++++++++++++++++++++++++++++++-
> > >  1 file changed, 242 insertions(+), 5 deletions(-)
> > >
> > > diff --git a/tools/test-runner.c b/tools/test-runner.c
> > > index 6660ea8de..d416f80ed 100644
> > > --- a/tools/test-runner.c
> > > +++ b/tools/test-runner.c
> > > @@ -51,6 +51,7 @@ static bool start_dbus_session;
> > >  static bool start_daemon = false;
> > >  static bool start_emulator = false;
> > >  static bool start_monitor = false;
> > > +static bool start_pipewire;
> > >  static int num_devs = 0;
> > >  static const char *qemu_binary = NULL;
> > >  static const char *kernel_image = NULL;
> > > @@ -252,13 +253,13 @@ static void start_qemu(void)
> > >                                 "acpi=off pci=noacpi noapic quiet ro init=%s "
> > >                                 "TESTHOME=%s TESTDBUS=%u TESTDAEMON=%u "
> > >                                 "TESTDBUSSESSION=%u XDG_RUNTIME_DIR=/run/user/0 "
> > > -                               "TESTAUDIO=%u "
> > > +                               "TESTAUDIO=%u TESTPIPEWIRE=%u "
> >
> > I'd just reuse TESTAUDIO instead of introducing yet another
> > environment variable, that should probably check if it shall run
> > pulseaudio or pipewire depending on what is available in the system.
>
> TESTAUDIO also adds a virtual soundcard to the VM. Is this needed for
> something, if tests and the audio daemon runs inside the VM they should
> not need such access to soundcards outside the VM?
>
> The tester.config doesn't enable ALSA, so it won't do anything in that
> configuration. The VM also fails to boot for me with -A enabled for
> that kernel.
>
> IIUC, this and running udevd are not currently used for something, and
> if so I'll remove those.

Yep, please remove/replace with something that doesn't depend on ALSA.

> >
> > >                                 "TESTMONITOR=%u TESTEMULATOR=%u TESTDEVS=%d "
> > >                                 "TESTAUTO=%u TESTARGS=\'%s\'",
> > >                                 initcmd, cwd, start_dbus, start_daemon,
> > >                                 start_dbus_session, audio_support,
> > > -                               start_monitor, start_emulator, num_devs,
> > > -                               run_auto, testargs);
> > > +                               start_pipewire, start_monitor, start_emulator,
> > > +                               num_devs, run_auto, testargs);
> > >
> > >         argv = alloca(sizeof(qemu_argv) +
> > >                                 (audio_support ? 4 : 0) +
> > > @@ -606,6 +607,207 @@ static pid_t start_bluetooth_daemon(const char *home)
> > >         return pid;
> > >  }
> > >
> > > +static char *get_command_stdout(char *command, size_t *size)
> > > +{
> > > +       char *buf = NULL;
> > > +       ssize_t nread = 0;
> > > +       size_t allocated = 0;
> > > +       int ret;
> > > +       FILE *f;
> > > +
> > > +       f = popen(command, "re");
> > > +       if (!f)
> > > +               return NULL;
> > > +
> > > +       while (1) {
> > > +               size_t res;
> > > +               void *p;
> > > +
> > > +               if (nread + 256 > allocated) {
> > > +                       allocated += allocated + 256;
> > > +                       p = realloc(buf, allocated);
> > > +                       if (!p) {
> > > +                               nread = -1;
> > > +                               break;
> > > +                       }
> > > +                       buf = p;
> > > +               }
> > > +
> > > +               res = fread(buf + nread, 1, allocated - nread - 1, f);
> > > +               if (!res)
> > > +                       break;
> > > +               nread += res;
> > > +       }
> > > +
> > > +       ret = pclose(f);
> > > +       if (ret < 0 || !WIFEXITED(ret) || WEXITSTATUS(ret) != 0) {
> > > +               printf("%s failed\n", command);
> > > +               nread = -1;
> > > +       }
> > > +
> > > +       if (nread >= 0) {
> > > +               buf[nread] = 0;
> > > +               if (size)
> > > +                       *size = nread;
> > > +       } else {
> > > +               free(buf);
> > > +               buf = NULL;
> > > +       }
> > > +
> > > +       return buf;
> > > +}
> > > +
> > > +static void start_pipewire_daemons(pid_t *pipewire_pid, pid_t *wireplumber_pid)
> > > +{
> > > +       static const char *const daemons[2] = {
> > > +               "/usr/bin/pipewire",
> > > +               "/usr/bin/wireplumber"
> > > +       };
> > > +       static const char *const dirs[] = {
> > > +               "/run/pw",
> > > +               "/run/pw/state",
> > > +               "/run/pw/wireplumber",
> > > +               "/run/pw/wireplumber/bluetooth.lua.d",
> > > +               "/run/pw/wireplumber/main.lua.d",
> > > +               NULL
> > > +       };
> > > +       FILE *f;
> > > +       pid_t *pids[2] = {pipewire_pid, wireplumber_pid};
> > > +       char *envp[5];
> > > +       int i;
> > > +
> > > +       for (i = 0; dirs[i]; ++i) {
> > > +               if (mkdir(dirs[i], 0755) < 0) {
> > > +                       perror("Failed to create directory");
> > > +                       return;
> > > +               }
> > > +       }
> > > +
> > > +       /* Enable only Bluetooth part, disable whatever requires user DBus */
> > > +       f = fopen("/run/pw/wireplumber/main.lua.d/51-custom.lua", "w");
> > > +       if (!f) {
> > > +               perror("Failed to create Pipewire main config");
> > > +               return;
> > > +       }
> > > +       fprintf(f, "alsa_monitor.enabled = false\n"
> > > +               "v4l2_monitor.enabled = false\n"
> > > +               "libcamera_monitor.enabled = false\n"
> > > +               "default_access.properties[\"enable-flatpak-portal\"]"
> > > +               " = false\n");
> > > +       fclose(f);
> >
> > I'd put this into its own function to make it clear that this is
> > setting up the configuration e.g.:
> >
> > https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/tools/test-runner.c#n450
> >
> > > +       f = fopen("/run/pw/wireplumber/bluetooth.lua.d/51-custom.lua", "w");
> > > +       if (!f) {
> > > +               perror("Failed to create Pipewire bluetooth config");
> > > +               return;
> > > +       }
> > > +       fprintf(f, "bluez_monitor.properties[\"with-logind\"] = false\n"
> > > +               "bluez_midi_monitor.enabled = false\n");
> > > +       fclose(f);
> > > +
> > > +       /* Launch daemons */
> > > +       for (i = 0; i < 2; ++i)
> > > +               *pids[i] = -1;
> > > +
> > > +       envp[0] = "DBUS_SYSTEM_BUS_ADDRESS=unix:"
> > > +                 "path=/run/dbus/system_bus_socket";
> > > +       envp[1] = "XDG_STATE_HOME=/run/pw/state";
> > > +       envp[2] = "XDG_CONFIG_HOME=/run/pw";
> > > +       envp[3] = "XDG_RUNTIME_DIR=/run/pw";
> > > +       envp[4] = NULL;
> > > +
> > > +       for (i = 0; i < 2; ++i) {
> > > +               const char *daemon = daemons[i];
> > > +               char *argv[2];
> > > +               pid_t pid;
> > > +
> > > +               printf("Starting Pipewire daemon %s\n", daemon);
> > > +
> > > +               argv[0] = (char *) daemon;
> > > +               argv[1] = NULL;
> > > +
> > > +               pid = fork();
> > > +               if (pid < 0) {
> > > +                       perror("Failed to fork new process");
> > > +                       return;
> > > +               }
> > > +
> > > +               if (pid == 0) {
> > > +                       execve(argv[0], argv, envp);
> > > +                       exit(EXIT_SUCCESS);
> > > +               }
> > > +
> > > +               *pids[i] = pid;
> > > +
> > > +               printf("Pipewire daemon process %d created\n", pid);
> > > +       }
> > > +
> > > +       /* Tell pipewire clients where the socket is */
> > > +       setenv("PIPEWIRE_RUNTIME_DIR", "/run/pw", 1);
> > > +
> > > +       /* Wait until daemons completely started */
> > > +       for (i = 0; i < 6; ++i) {
> > > +               char *buf;
> > > +
> > > +               if (i > 0) {
> > > +                       printf("Wait for Pipewire ready...\n");
> > > +                       usleep(500000);
> > > +               }
> > > +
> > > +               buf = get_command_stdout("/usr/bin/pw-dump", NULL);
> > > +               if (!buf)
> > > +                       continue;
> >
> > Don't we have a file or something similar to
> > /run/dbus/system_bus_socket that indicates pw is running? Checking
> > dump file seems a little overkill to me.
>
> You can stat for /run/pipewire-0
>
> The daemon running doesn't mean you have a sound devices yet, though,
> so if tests need them they need to wait for them themselves.

We could perhaps wait to see if Pipewire name popup on D-Bus, but I'd
leave that for a later stage when we actually have some tests that
depend on Pipewire directly.

> >
> > > +
> > > +               if (strstr(buf, "WirePlumber")) {
> > > +                       printf("Pipewire ready\n");
> > > +                       free(buf);
> > > +                       break;
> > > +               }
> > > +
> > > +               free(buf);
> > > +       }
> > > +       if (i == 6)
> > > +               goto fail;
> > > +
> > > +       if (!start_emulator || !start_daemon)
> > > +               return;
> > > +
> > > +       /* Wait for Bluetooth endpoints */
> > > +       for (i = 0; i < 6; ++i) {
> > > +               char *buf;
> > > +
> > > +               if (i > 0) {
> > > +                       printf("Wait for endpoints...\n");
> > > +                       usleep(500000);
> > > +               }
> > > +
> > > +               buf = get_command_stdout("/usr/bin/bluetoothctl show", NULL);
> > > +               if (!buf)
> > > +                       continue;
> > > +
> > > +               if (strstr(buf, "0000110b-0000-1000-8000-00805f9b34fb") ||
> > > +                   strstr(buf, "00001850-0000-1000-8000-00805f9b34fb")) {
> > > +                       printf("Pipewire endpoints ready\n");
> > > +                       free(buf);
> > > +                       break;
> > > +               }
> > > +
> > > +               free(buf);
> > > +       }
> > > +       if (i == 6)
> > > +               goto fail;
> >
> > Id skip this part, the endpoints registration depends on how
> > bluetoothd:main.conf  is configured so we shouldn't really expect
> > certain UUIDs like above.
>
> Ok, in principle the tests that need endpoints can wait for them.

Let me know if you need more feedback on anything, also what is the
status of coordinated set setup? Did you get it working or there are
still some blockers?

> >
> > > +       return;
> > > +
> > > +fail:
> > > +       for (i = 0; i < 2; ++i)
> > > +               if (*pids[i] > 0)
> > > +                       kill(*pids[i], SIGTERM);
> > > +
> > > +       printf("Pipewire daemons not running properly\n");
> > > +       return;
> > > +}
> > > +
> > >  static const char *test_table[] = {
> > >         "mgmt-tester",
> > >         "smp-tester",
> > > @@ -807,7 +1009,7 @@ static void run_command(char *cmdname, char *home)
> > >         int pos = 0, idx = 0;
> > >         int serial_fd;
> > >         pid_t pid, dbus_pid, daemon_pid, monitor_pid, emulator_pid,
> > > -             dbus_session_pid, udevd_pid;
> > > +             dbus_session_pid, udevd_pid, pw_pid, wp_pid;
> > >
> > >         if (!home) {
> > >                 perror("Invalid parameter: TESTHOME");
> > > @@ -860,6 +1062,13 @@ static void run_command(char *cmdname, char *home)
> > >         else
> > >                 emulator_pid = -1;
> > >
> > > +       if (start_pipewire) {
> > > +               start_pipewire_daemons(&pw_pid, &wp_pid);
> > > +       } else {
> > > +               pw_pid = -1;
> > > +               wp_pid = -1;
> > > +       }
> > > +
> > >  start_next:
> > >         if (run_auto) {
> > >                 if (chdir(home + 5) < 0) {
> > > @@ -966,6 +1175,16 @@ start_next:
> > >                         udevd_pid = -1;
> > >                 }
> > >
> > > +               if (corpse == pw_pid) {
> > > +                       printf("pipewire terminated\n");
> > > +                       pw_pid = -1;
> > > +               }
> > > +
> > > +               if (corpse == wp_pid) {
> > > +                       printf("wireplumber terminated\n");
> > > +                       wp_pid = -1;
> > > +               }
> > > +
> > >                 if (corpse == pid)
> > >                         break;
> > >         }
> > > @@ -975,6 +1194,12 @@ start_next:
> > >                 goto start_next;
> > >         }
> > >
> > > +       if (wp_pid > 0)
> > > +               kill(wp_pid, SIGTERM);
> > > +
> > > +       if (pw_pid > 0)
> > > +               kill(pw_pid, SIGTERM);
> > > +
> > >         if (daemon_pid > 0)
> > >                 kill(daemon_pid, SIGTERM);
> > >
> > > @@ -1079,6 +1304,12 @@ static void run_tests(void)
> > >                 audio_support = true;
> > >         }
> > >
> > > +       ptr = strstr(cmdline, "TESTPIPEWIRE=1");
> > > +       if (ptr) {
> > > +               printf("Pipewire requested\n");
> > > +               start_pipewire = true;
> > > +       }
> > > +
> > >         ptr = strstr(cmdline, "TESTHOME=");
> > >         if (ptr) {
> > >                 home = ptr + 4;
> > > @@ -1106,6 +1337,7 @@ static void usage(void)
> > >                 "\t-q, --qemu <path>      QEMU binary\n"
> > >                 "\t-k, --kernel <image>   Kernel image (bzImage)\n"
> > >                 "\t-A, --audio            Add audio support\n"
> > > +               "\t-P, --pipewire         Start pipewire\n"
> > >                 "\t-h, --help             Show help options\n");
> > >  }
> > >
> > > @@ -1121,6 +1353,7 @@ static const struct option main_options[] = {
> > >         { "qemu",    required_argument, NULL, 'q' },
> > >         { "kernel",  required_argument, NULL, 'k' },
> > >         { "audio",   no_argument,       NULL, 'A' },
> > > +       { "pipewire", no_argument,      NULL, 'P' },
> > >         { "version", no_argument,       NULL, 'v' },
> > >         { "help",    no_argument,       NULL, 'h' },
> > >         { }
> > > @@ -1140,7 +1373,7 @@ int main(int argc, char *argv[])
> > >         for (;;) {
> > >                 int opt;
> > >
> > > -               opt = getopt_long(argc, argv, "aubdslmq:k:Avh", main_options,
> > > +               opt = getopt_long(argc, argv, "aubdslmq:k:APvh", main_options,
> > >                                                                 NULL);
> > >                 if (opt < 0)
> > >                         break;
> > > @@ -1177,6 +1410,10 @@ int main(int argc, char *argv[])
> > >                 case 'A':
> > >                         audio_support = true;
> > >                         break;
> > > +               case 'P':
> > > +                       start_dbus = true;
> > > +                       start_pipewire = true;
> > > +                       break;
> > >                 case 'v':
> > >                         printf("%s\n", VERSION);
> > >                         return EXIT_SUCCESS;
> > > --
> > > 2.39.2
> > >
> >
> >
>
Pauli Virtanen May 8, 2023, 7:08 p.m. UTC | #5
Hi Luiz,

to, 2023-05-04 kello 12:09 -0700, Luiz Augusto von Dentz kirjoitti:
> On Sat, Apr 22, 2023 at 5:12 AM Pauli Virtanen <pav@iki.fi> wrote:
> > 
> > Hi Luiz,
> > 
> > to, 2023-04-20 kello 12:49 -0700, Luiz Augusto von Dentz kirjoitti:
> > > Hi Pauli,
> > > 
> > > On Sat, Apr 15, 2023 at 7:44 AM Pauli Virtanen <pav@iki.fi> wrote:
> > > > 
> > > > Add option for launching Pipewire inside the VM to serve Bluetooth
> > > > endpoints, which can be used in tests.
> > > > 
> > > > If daemon and emulator were also started, wait for the endpoints to
> > > > appear.
> > > > ---
> > > > 
> > > > Notes:
> > > >     An example how you can launch Pipewire to serve Bluetooth endpoints.
> > > > 
> > > >  tools/test-runner.c | 247 +++++++++++++++++++++++++++++++++++++++++++-
> > > >  1 file changed, 242 insertions(+), 5 deletions(-)
> > > > 
> > > > diff --git a/tools/test-runner.c b/tools/test-runner.c
> > > > index 6660ea8de..d416f80ed 100644
> > > > --- a/tools/test-runner.c
> > > > +++ b/tools/test-runner.c
> > > > @@ -51,6 +51,7 @@ static bool start_dbus_session;
> > > >  static bool start_daemon = false;
> > > >  static bool start_emulator = false;
> > > >  static bool start_monitor = false;
> > > > +static bool start_pipewire;
> > > >  static int num_devs = 0;
> > > >  static const char *qemu_binary = NULL;
> > > >  static const char *kernel_image = NULL;
> > > > @@ -252,13 +253,13 @@ static void start_qemu(void)
> > > >                                 "acpi=off pci=noacpi noapic quiet ro init=%s "
> > > >                                 "TESTHOME=%s TESTDBUS=%u TESTDAEMON=%u "
> > > >                                 "TESTDBUSSESSION=%u XDG_RUNTIME_DIR=/run/user/0 "
> > > > -                               "TESTAUDIO=%u "
> > > > +                               "TESTAUDIO=%u TESTPIPEWIRE=%u "
> > > 
> > > I'd just reuse TESTAUDIO instead of introducing yet another
> > > environment variable, that should probably check if it shall run
> > > pulseaudio or pipewire depending on what is available in the system.
> > 
> > TESTAUDIO also adds a virtual soundcard to the VM. Is this needed for
> > something, if tests and the audio daemon runs inside the VM they should
> > not need such access to soundcards outside the VM?
> > 
> > The tester.config doesn't enable ALSA, so it won't do anything in that
> > configuration. The VM also fails to boot for me with -A enabled for
> > that kernel.
> > 
> > IIUC, this and running udevd are not currently used for something, and
> > if so I'll remove those.
> 
> Yep, please remove/replace with something that doesn't depend on ALSA.

Ack.

> > > 
> > > >                                 "TESTMONITOR=%u TESTEMULATOR=%u TESTDEVS=%d "
> > > >                                 "TESTAUTO=%u TESTARGS=\'%s\'",
> > > >                                 initcmd, cwd, start_dbus, start_daemon,
> > > >                                 start_dbus_session, audio_support,
> > > > -                               start_monitor, start_emulator, num_devs,
> > > > -                               run_auto, testargs);
> > > > +                               start_pipewire, start_monitor, start_emulator,
> > > > +                               num_devs, run_auto, testargs);
> > > > 
> > > >         argv = alloca(sizeof(qemu_argv) +
> > > >                                 (audio_support ? 4 : 0) +
> > > > @@ -606,6 +607,207 @@ static pid_t start_bluetooth_daemon(const char *home)
> > > >         return pid;
> > > >  }
> > > > 
> > > > +static char *get_command_stdout(char *command, size_t *size)
> > > > +{
> > > > +       char *buf = NULL;
> > > > +       ssize_t nread = 0;
> > > > +       size_t allocated = 0;
> > > > +       int ret;
> > > > +       FILE *f;
> > > > +
> > > > +       f = popen(command, "re");
> > > > +       if (!f)
> > > > +               return NULL;
> > > > +
> > > > +       while (1) {
> > > > +               size_t res;
> > > > +               void *p;
> > > > +
> > > > +               if (nread + 256 > allocated) {
> > > > +                       allocated += allocated + 256;
> > > > +                       p = realloc(buf, allocated);
> > > > +                       if (!p) {
> > > > +                               nread = -1;
> > > > +                               break;
> > > > +                       }
> > > > +                       buf = p;
> > > > +               }
> > > > +
> > > > +               res = fread(buf + nread, 1, allocated - nread - 1, f);
> > > > +               if (!res)
> > > > +                       break;
> > > > +               nread += res;
> > > > +       }
> > > > +
> > > > +       ret = pclose(f);
> > > > +       if (ret < 0 || !WIFEXITED(ret) || WEXITSTATUS(ret) != 0) {
> > > > +               printf("%s failed\n", command);
> > > > +               nread = -1;
> > > > +       }
> > > > +
> > > > +       if (nread >= 0) {
> > > > +               buf[nread] = 0;
> > > > +               if (size)
> > > > +                       *size = nread;
> > > > +       } else {
> > > > +               free(buf);
> > > > +               buf = NULL;
> > > > +       }
> > > > +
> > > > +       return buf;
> > > > +}
> > > > +
> > > > +static void start_pipewire_daemons(pid_t *pipewire_pid, pid_t *wireplumber_pid)
> > > > +{
> > > > +       static const char *const daemons[2] = {
> > > > +               "/usr/bin/pipewire",
> > > > +               "/usr/bin/wireplumber"
> > > > +       };
> > > > +       static const char *const dirs[] = {
> > > > +               "/run/pw",
> > > > +               "/run/pw/state",
> > > > +               "/run/pw/wireplumber",
> > > > +               "/run/pw/wireplumber/bluetooth.lua.d",
> > > > +               "/run/pw/wireplumber/main.lua.d",
> > > > +               NULL
> > > > +       };
> > > > +       FILE *f;
> > > > +       pid_t *pids[2] = {pipewire_pid, wireplumber_pid};
> > > > +       char *envp[5];
> > > > +       int i;
> > > > +
> > > > +       for (i = 0; dirs[i]; ++i) {
> > > > +               if (mkdir(dirs[i], 0755) < 0) {
> > > > +                       perror("Failed to create directory");
> > > > +                       return;
> > > > +               }
> > > > +       }
> > > > +
> > > > +       /* Enable only Bluetooth part, disable whatever requires user DBus */
> > > > +       f = fopen("/run/pw/wireplumber/main.lua.d/51-custom.lua", "w");
> > > > +       if (!f) {
> > > > +               perror("Failed to create Pipewire main config");
> > > > +               return;
> > > > +       }
> > > > +       fprintf(f, "alsa_monitor.enabled = false\n"
> > > > +               "v4l2_monitor.enabled = false\n"
> > > > +               "libcamera_monitor.enabled = false\n"
> > > > +               "default_access.properties[\"enable-flatpak-portal\"]"
> > > > +               " = false\n");
> > > > +       fclose(f);
> > > 
> > > I'd put this into its own function to make it clear that this is
> > > setting up the configuration e.g.:
> > > 
> > > https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/tools/test-runner.c#n450
> > > 
> > > > +       f = fopen("/run/pw/wireplumber/bluetooth.lua.d/51-custom.lua", "w");
> > > > +       if (!f) {
> > > > +               perror("Failed to create Pipewire bluetooth config");
> > > > +               return;
> > > > +       }
> > > > +       fprintf(f, "bluez_monitor.properties[\"with-logind\"] = false\n"
> > > > +               "bluez_midi_monitor.enabled = false\n");
> > > > +       fclose(f);
> > > > +
> > > > +       /* Launch daemons */
> > > > +       for (i = 0; i < 2; ++i)
> > > > +               *pids[i] = -1;
> > > > +
> > > > +       envp[0] = "DBUS_SYSTEM_BUS_ADDRESS=unix:"
> > > > +                 "path=/run/dbus/system_bus_socket";
> > > > +       envp[1] = "XDG_STATE_HOME=/run/pw/state";
> > > > +       envp[2] = "XDG_CONFIG_HOME=/run/pw";
> > > > +       envp[3] = "XDG_RUNTIME_DIR=/run/pw";
> > > > +       envp[4] = NULL;
> > > > +
> > > > +       for (i = 0; i < 2; ++i) {
> > > > +               const char *daemon = daemons[i];
> > > > +               char *argv[2];
> > > > +               pid_t pid;
> > > > +
> > > > +               printf("Starting Pipewire daemon %s\n", daemon);
> > > > +
> > > > +               argv[0] = (char *) daemon;
> > > > +               argv[1] = NULL;
> > > > +
> > > > +               pid = fork();
> > > > +               if (pid < 0) {
> > > > +                       perror("Failed to fork new process");
> > > > +                       return;
> > > > +               }
> > > > +
> > > > +               if (pid == 0) {
> > > > +                       execve(argv[0], argv, envp);
> > > > +                       exit(EXIT_SUCCESS);
> > > > +               }
> > > > +
> > > > +               *pids[i] = pid;
> > > > +
> > > > +               printf("Pipewire daemon process %d created\n", pid);
> > > > +       }
> > > > +
> > > > +       /* Tell pipewire clients where the socket is */
> > > > +       setenv("PIPEWIRE_RUNTIME_DIR", "/run/pw", 1);
> > > > +
> > > > +       /* Wait until daemons completely started */
> > > > +       for (i = 0; i < 6; ++i) {
> > > > +               char *buf;
> > > > +
> > > > +               if (i > 0) {
> > > > +                       printf("Wait for Pipewire ready...\n");
> > > > +                       usleep(500000);
> > > > +               }
> > > > +
> > > > +               buf = get_command_stdout("/usr/bin/pw-dump", NULL);
> > > > +               if (!buf)
> > > > +                       continue;
> > > 
> > > Don't we have a file or something similar to
> > > /run/dbus/system_bus_socket that indicates pw is running? Checking
> > > dump file seems a little overkill to me.
> > 
> > You can stat for /run/pipewire-0
> > 
> > The daemon running doesn't mean you have a sound devices yet, though,
> > so if tests need them they need to wait for them themselves.
> 
> We could perhaps wait to see if Pipewire name popup on D-Bus, but I'd
> leave that for a later stage when we actually have some tests that
> depend on Pipewire directly.
> 
> > > 
> > > > +
> > > > +               if (strstr(buf, "WirePlumber")) {
> > > > +                       printf("Pipewire ready\n");
> > > > +                       free(buf);
> > > > +                       break;
> > > > +               }
> > > > +
> > > > +               free(buf);
> > > > +       }
> > > > +       if (i == 6)
> > > > +               goto fail;
> > > > +
> > > > +       if (!start_emulator || !start_daemon)
> > > > +               return;
> > > > +
> > > > +       /* Wait for Bluetooth endpoints */
> > > > +       for (i = 0; i < 6; ++i) {
> > > > +               char *buf;
> > > > +
> > > > +               if (i > 0) {
> > > > +                       printf("Wait for endpoints...\n");
> > > > +                       usleep(500000);
> > > > +               }
> > > > +
> > > > +               buf = get_command_stdout("/usr/bin/bluetoothctl show", NULL);
> > > > +               if (!buf)
> > > > +                       continue;
> > > > +
> > > > +               if (strstr(buf, "0000110b-0000-1000-8000-00805f9b34fb") ||
> > > > +                   strstr(buf, "00001850-0000-1000-8000-00805f9b34fb")) {
> > > > +                       printf("Pipewire endpoints ready\n");
> > > > +                       free(buf);
> > > > +                       break;
> > > > +               }
> > > > +
> > > > +               free(buf);
> > > > +       }
> > > > +       if (i == 6)
> > > > +               goto fail;
> > > 
> > > Id skip this part, the endpoints registration depends on how
> > > bluetoothd:main.conf  is configured so we shouldn't really expect
> > > certain UUIDs like above.
> > 
> > Ok, in principle the tests that need endpoints can wait for them.
> 
> Let me know if you need more feedback on anything, also what is the
> status of coordinated set setup? Did you get it working or there are
> still some blockers?

The coordinated set stuff was merged in Pipewire some weeks ago. You'll
need PW 0.3.70 + Wireplumber master branch for it, and it should just
work.

... aside from some problems I mentioned in earlier mails, which may be
at lower level than PW. I uploaded some HCI traces for these:

https://github.com/bluez/bluez/issues/515   (TWS desynchronization)
https://github.com/bluez/bluez/issues/516   (transport reacquire fails)
https://github.com/bluez/bluez/issues/517   (general connection issues)
diff mbox series

Patch

diff --git a/tools/test-runner.c b/tools/test-runner.c
index 6660ea8de..d416f80ed 100644
--- a/tools/test-runner.c
+++ b/tools/test-runner.c
@@ -51,6 +51,7 @@  static bool start_dbus_session;
 static bool start_daemon = false;
 static bool start_emulator = false;
 static bool start_monitor = false;
+static bool start_pipewire;
 static int num_devs = 0;
 static const char *qemu_binary = NULL;
 static const char *kernel_image = NULL;
@@ -252,13 +253,13 @@  static void start_qemu(void)
 				"acpi=off pci=noacpi noapic quiet ro init=%s "
 				"TESTHOME=%s TESTDBUS=%u TESTDAEMON=%u "
 				"TESTDBUSSESSION=%u XDG_RUNTIME_DIR=/run/user/0 "
-				"TESTAUDIO=%u "
+				"TESTAUDIO=%u TESTPIPEWIRE=%u "
 				"TESTMONITOR=%u TESTEMULATOR=%u TESTDEVS=%d "
 				"TESTAUTO=%u TESTARGS=\'%s\'",
 				initcmd, cwd, start_dbus, start_daemon,
 				start_dbus_session, audio_support,
-				start_monitor, start_emulator, num_devs,
-				run_auto, testargs);
+				start_pipewire, start_monitor, start_emulator,
+				num_devs, run_auto, testargs);
 
 	argv = alloca(sizeof(qemu_argv) +
 				(audio_support ? 4 : 0) +
@@ -606,6 +607,207 @@  static pid_t start_bluetooth_daemon(const char *home)
 	return pid;
 }
 
+static char *get_command_stdout(char *command, size_t *size)
+{
+	char *buf = NULL;
+	ssize_t nread = 0;
+	size_t allocated = 0;
+	int ret;
+	FILE *f;
+
+	f = popen(command, "re");
+	if (!f)
+		return NULL;
+
+	while (1) {
+		size_t res;
+		void *p;
+
+		if (nread + 256 > allocated) {
+			allocated += allocated + 256;
+			p = realloc(buf, allocated);
+			if (!p) {
+				nread = -1;
+				break;
+			}
+			buf = p;
+		}
+
+		res = fread(buf + nread, 1, allocated - nread - 1, f);
+		if (!res)
+			break;
+		nread += res;
+	}
+
+	ret = pclose(f);
+	if (ret < 0 || !WIFEXITED(ret) || WEXITSTATUS(ret) != 0) {
+		printf("%s failed\n", command);
+		nread = -1;
+	}
+
+	if (nread >= 0) {
+		buf[nread] = 0;
+		if (size)
+			*size = nread;
+	} else {
+		free(buf);
+		buf = NULL;
+	}
+
+	return buf;
+}
+
+static void start_pipewire_daemons(pid_t *pipewire_pid, pid_t *wireplumber_pid)
+{
+	static const char *const daemons[2] = {
+		"/usr/bin/pipewire",
+		"/usr/bin/wireplumber"
+	};
+	static const char *const dirs[] = {
+		"/run/pw",
+		"/run/pw/state",
+		"/run/pw/wireplumber",
+		"/run/pw/wireplumber/bluetooth.lua.d",
+		"/run/pw/wireplumber/main.lua.d",
+		NULL
+	};
+	FILE *f;
+	pid_t *pids[2] = {pipewire_pid, wireplumber_pid};
+	char *envp[5];
+	int i;
+
+	for (i = 0; dirs[i]; ++i) {
+		if (mkdir(dirs[i], 0755) < 0) {
+			perror("Failed to create directory");
+			return;
+		}
+	}
+
+	/* Enable only Bluetooth part, disable whatever requires user DBus */
+	f = fopen("/run/pw/wireplumber/main.lua.d/51-custom.lua", "w");
+	if (!f) {
+		perror("Failed to create Pipewire main config");
+		return;
+	}
+	fprintf(f, "alsa_monitor.enabled = false\n"
+		"v4l2_monitor.enabled = false\n"
+		"libcamera_monitor.enabled = false\n"
+		"default_access.properties[\"enable-flatpak-portal\"]"
+		" = false\n");
+	fclose(f);
+
+	f = fopen("/run/pw/wireplumber/bluetooth.lua.d/51-custom.lua", "w");
+	if (!f) {
+		perror("Failed to create Pipewire bluetooth config");
+		return;
+	}
+	fprintf(f, "bluez_monitor.properties[\"with-logind\"] = false\n"
+		"bluez_midi_monitor.enabled = false\n");
+	fclose(f);
+
+	/* Launch daemons */
+	for (i = 0; i < 2; ++i)
+		*pids[i] = -1;
+
+	envp[0] = "DBUS_SYSTEM_BUS_ADDRESS=unix:"
+		  "path=/run/dbus/system_bus_socket";
+	envp[1] = "XDG_STATE_HOME=/run/pw/state";
+	envp[2] = "XDG_CONFIG_HOME=/run/pw";
+	envp[3] = "XDG_RUNTIME_DIR=/run/pw";
+	envp[4] = NULL;
+
+	for (i = 0; i < 2; ++i) {
+		const char *daemon = daemons[i];
+		char *argv[2];
+		pid_t pid;
+
+		printf("Starting Pipewire daemon %s\n", daemon);
+
+		argv[0] = (char *) daemon;
+		argv[1] = NULL;
+
+		pid = fork();
+		if (pid < 0) {
+			perror("Failed to fork new process");
+			return;
+		}
+
+		if (pid == 0) {
+			execve(argv[0], argv, envp);
+			exit(EXIT_SUCCESS);
+		}
+
+		*pids[i] = pid;
+
+		printf("Pipewire daemon process %d created\n", pid);
+	}
+
+	/* Tell pipewire clients where the socket is */
+	setenv("PIPEWIRE_RUNTIME_DIR", "/run/pw", 1);
+
+	/* Wait until daemons completely started */
+	for (i = 0; i < 6; ++i) {
+		char *buf;
+
+		if (i > 0) {
+			printf("Wait for Pipewire ready...\n");
+			usleep(500000);
+		}
+
+		buf = get_command_stdout("/usr/bin/pw-dump", NULL);
+		if (!buf)
+			continue;
+
+		if (strstr(buf, "WirePlumber")) {
+			printf("Pipewire ready\n");
+			free(buf);
+			break;
+		}
+
+		free(buf);
+	}
+	if (i == 6)
+		goto fail;
+
+	if (!start_emulator || !start_daemon)
+		return;
+
+	/* Wait for Bluetooth endpoints */
+	for (i = 0; i < 6; ++i) {
+		char *buf;
+
+		if (i > 0) {
+			printf("Wait for endpoints...\n");
+			usleep(500000);
+		}
+
+		buf = get_command_stdout("/usr/bin/bluetoothctl show", NULL);
+		if (!buf)
+			continue;
+
+		if (strstr(buf, "0000110b-0000-1000-8000-00805f9b34fb") ||
+		    strstr(buf, "00001850-0000-1000-8000-00805f9b34fb")) {
+			printf("Pipewire endpoints ready\n");
+			free(buf);
+			break;
+		}
+
+		free(buf);
+	}
+	if (i == 6)
+		goto fail;
+
+	return;
+
+fail:
+	for (i = 0; i < 2; ++i)
+		if (*pids[i] > 0)
+			kill(*pids[i], SIGTERM);
+
+	printf("Pipewire daemons not running properly\n");
+	return;
+}
+
 static const char *test_table[] = {
 	"mgmt-tester",
 	"smp-tester",
@@ -807,7 +1009,7 @@  static void run_command(char *cmdname, char *home)
 	int pos = 0, idx = 0;
 	int serial_fd;
 	pid_t pid, dbus_pid, daemon_pid, monitor_pid, emulator_pid,
-	      dbus_session_pid, udevd_pid;
+	      dbus_session_pid, udevd_pid, pw_pid, wp_pid;
 
 	if (!home) {
 		perror("Invalid parameter: TESTHOME");
@@ -860,6 +1062,13 @@  static void run_command(char *cmdname, char *home)
 	else
 		emulator_pid = -1;
 
+	if (start_pipewire) {
+		start_pipewire_daemons(&pw_pid, &wp_pid);
+	} else {
+		pw_pid = -1;
+		wp_pid = -1;
+	}
+
 start_next:
 	if (run_auto) {
 		if (chdir(home + 5) < 0) {
@@ -966,6 +1175,16 @@  start_next:
 			udevd_pid = -1;
 		}
 
+		if (corpse == pw_pid) {
+			printf("pipewire terminated\n");
+			pw_pid = -1;
+		}
+
+		if (corpse == wp_pid) {
+			printf("wireplumber terminated\n");
+			wp_pid = -1;
+		}
+
 		if (corpse == pid)
 			break;
 	}
@@ -975,6 +1194,12 @@  start_next:
 		goto start_next;
 	}
 
+	if (wp_pid > 0)
+		kill(wp_pid, SIGTERM);
+
+	if (pw_pid > 0)
+		kill(pw_pid, SIGTERM);
+
 	if (daemon_pid > 0)
 		kill(daemon_pid, SIGTERM);
 
@@ -1079,6 +1304,12 @@  static void run_tests(void)
 		audio_support = true;
 	}
 
+	ptr = strstr(cmdline, "TESTPIPEWIRE=1");
+	if (ptr) {
+		printf("Pipewire requested\n");
+		start_pipewire = true;
+	}
+
 	ptr = strstr(cmdline, "TESTHOME=");
 	if (ptr) {
 		home = ptr + 4;
@@ -1106,6 +1337,7 @@  static void usage(void)
 		"\t-q, --qemu <path>      QEMU binary\n"
 		"\t-k, --kernel <image>   Kernel image (bzImage)\n"
 		"\t-A, --audio            Add audio support\n"
+		"\t-P, --pipewire         Start pipewire\n"
 		"\t-h, --help             Show help options\n");
 }
 
@@ -1121,6 +1353,7 @@  static const struct option main_options[] = {
 	{ "qemu",    required_argument, NULL, 'q' },
 	{ "kernel",  required_argument, NULL, 'k' },
 	{ "audio",   no_argument,       NULL, 'A' },
+	{ "pipewire", no_argument,      NULL, 'P' },
 	{ "version", no_argument,       NULL, 'v' },
 	{ "help",    no_argument,       NULL, 'h' },
 	{ }
@@ -1140,7 +1373,7 @@  int main(int argc, char *argv[])
 	for (;;) {
 		int opt;
 
-		opt = getopt_long(argc, argv, "aubdslmq:k:Avh", main_options,
+		opt = getopt_long(argc, argv, "aubdslmq:k:APvh", main_options,
 								NULL);
 		if (opt < 0)
 			break;
@@ -1177,6 +1410,10 @@  int main(int argc, char *argv[])
 		case 'A':
 			audio_support = true;
 			break;
+		case 'P':
+			start_dbus = true;
+			start_pipewire = true;
+			break;
 		case 'v':
 			printf("%s\n", VERSION);
 			return EXIT_SUCCESS;