1#!/bin/bash
2# Copyright (c) 2021 Huawei Device Co., Ltd.
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15set -e
16
17device="/dev/ttyACM0"
18stty -F "$device" raw speed 115200 min 0 time 5
19port=$(lsusb -t| grep cdc_acm | tail -1 | awk -F'[ :]+' '{print $4}')
20usb_dir="/sys/bus/usb/devices/1-${port}"
21
22while true
23do
24    if [ ! -e "$device" ];then
25        echo "$device not exists"
26        sleep 1
27        continue
28    fi
29    data=$(cat $device)
30    if [ "$data" == "GET_DESCRIPTOR" ];then
31        id_vendor=$(cat ${usb_dir}/idVendor)
32        id_product=$(cat ${usb_dir}/idProduct)
33        bcd_device=$(cat ${usb_dir}/bcdDevice)
34        b_configuration_value=$(cat ${usb_dir}/bConfigurationValue)
35        echo "$id_vendor" "$id_product" "$bcd_device" "$b_configuration_value"
36        echo -n "$id_vendor" "$id_product" "$bcd_device" "$b_configuration_value"> $device
37    fi
38    if [ ! "${data}x" == "x" ];then
39        echo "$data"
40        echo -n "$data" > $device
41    fi
42done