1#!/usr/bin/env python3
2#
3# Copyright (c) 2019 Intel Corporation
4#
5# SPDX-License-Identifier: Apache-2.0
6#
7# Author: Sathish Kuttan <sathish.k.kuttan@intel.com>
8
9# This script sends a command to either start or stop audio streams
10# to an Intel Sue Creek S1000 target connected to a Linux host
11# over USB.
12# The target should be running the sample audio application from the
13# sample project to which this script belongs.
14
15import hid
16import yaml
17import os
18import argparse
19
20class Device:
21    def __init__(self):
22        """
23        Reads configuration file to determine the USB VID/PID of
24        the Sue Creek target.
25        When the script is run using sudo permission, the
26        manufacturer and product strings are printed
27        """
28        config_file = os.path.join(os.path.dirname(__file__), 'config.yml')
29        with open(config_file, 'r') as ymlfile:
30            config = yaml.safe_load(ymlfile)
31        self.name = config['general']['name']
32        self.usb_vid = config['usb']['vid']
33        self.usb_pid = config['usb']['pid']
34        self.hid_dev = hid.device()
35        if self.hid_dev is None:
36            print('Device not found')
37        else:
38            self.hid_dev.open(self.usb_vid, self.usb_pid)
39
40    def start_audio(self):
41        """
42        Sends a command to start the audio transfers
43        in the Sue Creek target.
44        """
45        # space for 1 byte report id, 2 bytes of padding and 1 byte report size
46        command = 'start_audio'.encode('utf-8') + b'\x00'
47        command += b'\x00' * (56 - len(command))
48        cmd_len = len(command) // 4 + 1
49        command = b'\x01\x00' + cmd_len.to_bytes(2, byteorder='little') + \
50                command
51        command = b'\x01\x00\x00\x38' + command
52        print('Starting Audio on %s ...' % self.hid_dev.get_product_string())
53        self.hid_dev.send_feature_report(command)
54        self.hid_dev.read(len(command))
55
56    def stop_audio(self):
57        """
58        Sends a command to stop the running audio transfers
59        in the Sue Creek target.
60        """
61        # space for 1 byte report id, 2 bytes of padding and 1 byte report size
62        command = 'stop_audio'.encode('utf-8') + b'\x00'
63        command += b'\x00' * (56 - len(command))
64        cmd_len = len(command) // 4 + 1
65        command = b'\x02\x00' + cmd_len.to_bytes(2, byteorder='little') + \
66                command
67        command = b'\x01\x00\x00\x38' + command
68        print('Stopping Audio on %s ...' % self.hid_dev.get_product_string())
69        self.hid_dev.send_feature_report(command)
70        self.hid_dev.read(len(command))
71
72def main():
73    parser = argparse.ArgumentParser(epilog='NOTE: use sudo -E %(prog)s to run the script')
74    parser.add_argument('command', choices=['start', 'stop'],
75            help='start or stop audio streams')
76    args = parser.parse_args()
77    sue_creek = Device()
78    if args.command == 'start':
79        sue_creek.start_audio()
80    if args.command == 'stop':
81        sue_creek.stop_audio()
82
83if __name__ == '__main__':
84    main()
85