Microsoft MVP성태의 닷넷 이야기
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
 
(연관된 글이 4개 있습니다.)
(시리즈 글이 7개 있습니다.)
사물인터넷: 5. Raspberry Pi Zero(OTG)를 다른 컴퓨터에 연결해 가상 이더넷 카드로 쓰는 방법
; https://www.sysnet.pe.kr/2/0/11353

사물인터넷: 6. Raspberry Pi Zero(OTG)를 다른 컴퓨터에 연결해 가상 키보드로 쓰는 방법
; https://www.sysnet.pe.kr/2/0/11354

사물인터넷: 7. Raspberry Pi Zero(OTG)를 다른 컴퓨터에 연결해 가상 마우스로 쓰는 방법
; https://www.sysnet.pe.kr/2/0/11355

사물인터넷: 8. Raspberry Pi Zero(OTG)를 다른 컴퓨터에 연결해 가상 마우스 + 키보드로 쓰는 방법
; https://www.sysnet.pe.kr/2/0/11356

사물인터넷: 10. Raspberry Pi Zero(OTG)를 다른 컴퓨터에 연결해 가상 마우스 + 키보드로 쓰는 방법 (두 번째 이야기)
; https://www.sysnet.pe.kr/2/0/11363

사물인터넷: 11. Raspberry Pi Zero(OTG)를 다른 컴퓨터에 연결해 가상 마우스로 쓰는 방법 (절대 좌표)
; https://www.sysnet.pe.kr/2/0/11364

사물인터넷: 12. Raspberry Pi Zero(OTG)를 다른 컴퓨터에 연결해 가상 키보드 및 마우스로 쓰는 방법 (절대 좌표, 상대 좌표, 휠)
; https://www.sysnet.pe.kr/2/0/11369




Raspberry Pi Zero(OTG)를 다른 컴퓨터에 연결해 가상 마우스 + 키보드로 쓰는 방법

지난 글에서 Raspberry Pi Zero의 OTG 기능을 이용해 "유선 USB 이더넷 카드" / "가상 키보드" / "가상 마우스"로 사용하는 방법을 설명했는데요. ^^

Raspberry Pi Zero(OTG)를 다른 컴퓨터에 연결해 가상 이더넷 카드로 쓰는 방법
; https://www.sysnet.pe.kr/2/0/11353

Raspberry Pi Zero(OTG)를 다른 컴퓨터에 연결해 가상 키보드로 쓰는 방법
; https://www.sysnet.pe.kr/2/0/11354

Raspberry Pi Zero(OTG)를 다른 컴퓨터에 연결해 가상 마우스로 쓰는 방법
; https://www.sysnet.pe.kr/2/0/11355

근데, 마우스와 키보드를 모두 에뮬레이션하고 싶은 경우 라즈베리 파이 제로 장치를 2대 구매해야 할까요? 바로 이럴 때, 예전에 봐두었던 책의 내용이 생각났습니다. ^^

윈도우 USB 디바이스 드라이버 
 - WDM부터 최신 KMDF와 UMDF까지 윈도우 드라이버 모델 입문과 실전 
; http://www.yes24.com/24/goods/4825003?scode=032&OzSrank=1

책의 내용중에 기억에 남는 것이 바로 USB Descriptors의 계층 구조였습니다.

[그림 출처: http://www.beyondlogic.org/usbnutshell/usb5.shtml]
otg_dual_1.gif

즉, 하나의 라즈베리 파이 제로 장치에 키보드와 마우스 2개의 기능을 갖는 descriptors를 맞춰 주면 된다는 것입니다.




처음 할 2개의 작업은 역시나 가상 마우스/키보드로 했을 때와 동일합니다.

먼저 "/boot" 디렉터리의 "config.txt" 파일을 열어 다음의 라인을 추가하고,

dtoverlay=dwc2

/etc/modules 파일에 다음의 2가지 설정을 추가합니다.

dwc2
g_hid

그다음, 파이를 마우스로써 동작시키는 스크립트를 실행해야 하는데 전체적인 구조는 키보드/마우스를 구현할 때의 것을 참조하면 됩니다.

#!/bin/bash
# From the README at https://github.com/girst/hardpass

dtoverlay dwc2
modprobe dwc2
modprobe libcomposite
cd /sys/kernel/config/usb_gadget/
mkdir -p g1
cd g1
echo 0x1d6b > idVendor # Linux Foundation
echo 0x0104 > idProduct # Multifunction Composite Gadget
echo 0x0100 > bcdDevice # v1.0.0
echo 0x0200 > bcdUSB # USB2
mkdir -p strings/0x409
echo "fedcba9876543210" > strings/0x409/serialnumber
echo "girst" > strings/0x409/manufacturer
echo "Hardpass" > strings/0x409/product
N="usb0"
mkdir -p functions/hid.$N
echo 1 > functions/hid.usb0/protocol
echo 1 > functions/hid.usb0/subclass
echo 8 > functions/hid.usb0/report_length

# [...omitted for brevity... - report descriptors]

C=1
mkdir -p configs/c.$C/strings/0x409
echo "Config $C: ECM network" > configs/c.$C/strings/0x409/configuration
echo 250 > configs/c.$C/MaxPower
ln -s functions/hid.$N configs/c.$C/
ls /sys/class/udc > UDC

가만 보면 /sys/kernel/config/usb_gadget/ 디렉터리 하위에 g1이라는 서브 디렉터리를 만드는데 이것이 USB descriptors의 첫 번째 "Device descriptors" 정보를 담는 걸로 보입니다. 우리가 구현하는 것은 장치가 하나이므로 "device descriptors"는 변경하지 않아도 됩니다.

그다음, g1 디렉터리 하위에 다시 functions/hid.usb0 디렉터리를 만드는데요. 이름이 functions이긴 하지만 USB descriptors의 용어로 보면 이것이 interface descriptors로 보입니다. 그러니까, 우리의 장치는 키보드와 마우스를 제공할 것이므로 2개의 interface를 정의하면 됩니다.

그리고 마지막에 g1 디렉터리 하위에 configs가 만들어지는데 아마도 configuration descriptors를 의미하는 듯합니다. 계층 구조로 보면 configuration descriptors 안에 interface descriptors를 다중 정의할 수 있으니 그럼... 대충 답이 나온 것 같습니다. 다음과 같이 2개의 functions를 정의하고 configs에 연결해 주면 되는 것입니다.

#!/bin/bash
# From the README at https://github.com/girst/hardpass

dtoverlay dwc2
modprobe dwc2
modprobe libcomposite
cd /sys/kernel/config/usb_gadget/
mkdir -p g1
cd g1
echo 0x1d6b > idVendor # Linux Foundation
echo 0x0104 > idProduct # Multifunction Composite Gadget
echo 0x0100 > bcdDevice # v1.0.0
echo 0x0200 > bcdUSB # USB2
mkdir -p strings/0x409
echo "fedcba9876543210" > strings/0x409/serialnumber
echo "girst" > strings/0x409/manufacturer
echo "Hardpass" > strings/0x409/product

N="usb0"
mkdir -p functions/hid.$N
echo 1 > functions/hid.usb0/protocol
echo 1 > functions/hid.usb0/subclass
echo 8 > functions/hid.usb0/report_length
echo -ne \\x05\\x01\\x09\\x06\\xa1\\x01\\x05\\x07\\x19\\xe0\\x29\\xe7\\x15\\x00\\x25\\x01\\x75\\x01\\x95\\x08\\x81\\x02\\x95\\x01\\x75\\x08\\x81\\x03\\x95\\x05\\x75\\x01\\x05\\x08\\x19\\x01\\x29\\x05\\x91\\x02\\x95\\x01\\x75\\x03\\x91\\x03\\x95\\x06\\x75\\x08\\x15\\x00\\x25\\x65\\x05\\x07\\x19\\x00\\x29\\x65\\x81\\x00\\xc0 > functions/hid.usb0/report_desc

N="usb1"
mkdir -p functions/hid.$N
echo 1 > functions/hid.usb1/protocol
echo 1 > functions/hid.usb1/subclass
echo 8 > functions/hid.usb1/report_length
echo -ne \\x05\\x01\\x09\\x02\\xa1\\x01\\x09\\x01\\xa1\\x00\\x05\\x09\\x19\\x01\\x29\\x03\\x15\\x00\\x25\\x01\\x95\\x03\\x75\\x01\\x81\\x02\\x95\\x01\\x75\\x05\\x81\\x03\\x05\\x01\\x09\\x30\\x09\\x31\\x15\\x81\\x25\\x7f\\x75\\x08\\x95\\x02\\x81\\x06\\xC0\\xC0 > functions/hid.usb1/report_desc

C=1
mkdir -p configs/c.$C/strings/0x409
echo "Config $C: ECM network" > configs/c.$C/strings/0x409/configuration
echo 250 > configs/c.$C/MaxPower
ln -s functions/hid.usb0 configs/c.$C/
ln -s functions/hid.usb1 configs/c.$C/

ls /sys/class/udc > UDC

정말 잘 되는지 테스트해 볼까요? ^^ 파일명을 dual.sh로 만들어 저장하고,

$ nano dual.sh

실행 권한을 부여한 후 실행합니다.

$ chmod +x ./dual.sh
$ sudo ./dual.sh

이렇게 하고 USB to Micro B 케이블로 컴퓨터에 연결하면 다음과 같이 2개의 "USB Input Device"와 키보드 쪽으로 "HID Keyboard Device", 마우스 쪽으로 "HID-compliant mouse"라는 장치가 인식됩니다.

otg_dual_2.PNG

키보드/마우스 제어 역시 지난번 글의 hid_gadget_test 프로그램을 이용하면 됩니다.

라즈베리 파이로 SSH 콘솔 연결 창을 2개 띄우고 키보드와 마우스 각각에 대해 I/O 작업을 수행하도록 다음과 같이 파이에서 실행해 둡니다.

$ sudo ./hid_tester /dev/hidg0 keyboard

$ sudo ./hid_tester /dev/hidg1 mouse

가상 마우스/키보드 입력 테스트를 위해 라즈베리 파이 제로를 연결한 컴퓨터의 마우스 포인터를 중앙 정도에 메모장과 함께 위치해 놓고, hid_tester 입력 화면에서 다음의 내용을 타이핑합니다.

50 60
100 -20
--b1

t
e
s
t
--return

제 경우에는 아~~~~주 잘 됩니다. ^^




이제 남은 것은 파이 부팅 시 자동으로 dual.sh를 실행하는 것입니다. 아래에 쓰인 대로,

Two things at once
; http://andrewnicolaou.co.uk/posts/2016/pi-zero-midi-3-two-things-at-once

/etc/systemd/system/ 디렉터리에 create-dual-usb.service 파일을 만들고,

$ cd /etc/systemd/system/
$ sudo nano create-dual-usb.service

다음의 내용으로 채웠습니다.

[Unit]
Description=Create Mouse/Keyboard USB gadgets

[Service]
Type=oneshot
ExecStart=/share/dual.sh
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target

그리고 이것을 활성화시키고,

$ sudo systemctl daemon-reload
$ sudo systemctl enable create-dual-usb

재부팅을 해서,

$ sudo reboot

부팅과 함께 연결된 컴퓨터의 장치 관리자에 키보드/마우스가 정상적으로 등록되는지 확인하면 됩니다. ^^

참고로, 서비스의 오류 로그는 다음의 명령어로 확인할 수 있습니다.

$ sudo systemctl status create-dual-usb




찾아보니 괜찮은 자료들이 있군요. ^^

USB_Gadget_Configfs_API_0.pdf
; https://www.elinux.org/File:USB_Gadget_Configfs_API_0.pdf

pololu/wixel-sdk 
; https://github.com/pololu/wixel-sdk/blob/master/libraries/src/usb_hid/usb_hid.c



윈도우즈 사용자를 위한 라즈베리 파이 제로 W 모델을 설정하는 방법
; https://www.sysnet.pe.kr/2/0/11372

Raspberry Pi Zero(OTG)를 다른 컴퓨터에 연결해 가상 이더넷 카드로 쓰는 방법
; https://www.sysnet.pe.kr/2/0/11353

Raspberry Pi Zero(OTG)를 다른 컴퓨터에 연결해 가상 키보드로 쓰는 방법
; https://www.sysnet.pe.kr/2/0/11354

Raspberry Pi Zero(OTG)를 다른 컴퓨터에 연결해 가상 마우스로 쓰는 방법
; https://www.sysnet.pe.kr/2/0/11355

Raspberry Pi Zero(OTG)를 다른 컴퓨터에 연결해 가상 마우스 + 키보드로 쓰는 방법
; https://www.sysnet.pe.kr/2/0/11356

Raspberry Pi Zero(OTG)를 다른 컴퓨터에 연결해 가상 마우스로 쓰는 방법 (절대 좌표)
; https://www.sysnet.pe.kr/2/0/11364

Raspberry Pi Zero(OTG)를 다른 컴퓨터에 연결해 가상 키보드 및 마우스로 쓰는 방법 (절대 좌표, 상대 좌표, 휠)
; https://www.sysnet.pe.kr/2/0/11369

라즈베리 파이 용 C++ 프로젝트에 SSL Socket 적용
; https://www.sysnet.pe.kr/2/0/11411

Raspberry Pi/Windows 다중 플랫폼 지원 컴파일 관련 오류 기록
; https://www.sysnet.pe.kr/2/0/11373

Linux 3: 라즈베리 파이 - (윈도우의 NT 서비스처럼) 부팅 시 시작하는 프로그램 설정
; https://www.sysnet.pe.kr/2/0/11374


[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]

[연관 글]






[최초 등록일: ]
[최종 수정일: 5/13/2020]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 



2017-11-23 12시02분
Raspberry Pi Zero(OTG)를 다른 컴퓨터에 연결해 가상 마우스 + 키보드로 쓰는 방법 (두 번째 이야기)
; http://www.sysnet.pe.kr/2/0/11363
정성태
2018-02-19 08시04분
[김홍근] 혹시 무선으로 HID연결이 가능한가요?
[guest]
2018-02-19 09시23분
그건 저도 방법을 모르겠습니다. 혹시 아시게 되면 공유 부탁드립니다. ^^
정성태
2021-06-19 01시44분
아두이노 레오나르도로 키보드와 마우스 제어: 오토마우스
; https://blog.naver.com/specialist0/221159529907
정성태

... [31]  32  33  34  35  36  37  38  39  40  41  42  43  44  45  ...
NoWriterDateCnt.TitleFile(s)
12845정성태10/6/20218095.NET Framework: 1120. C# - BufferBlock<T> 사용 예제 [5]파일 다운로드1
12844정성태10/3/20216128오류 유형: 764. MSI 설치 시 "... is accessible and not read-only." 오류 메시지
12843정성태10/3/20216586스크립트: 29. 파이썬 - fork 시 기존 클라이언트 소켓 및 스레드의 동작파일 다운로드1
12842정성태10/1/202124791오류 유형: 763. 파이썬 오류 - AttributeError: type object '...' has no attribute '...'
12841정성태10/1/20218375스크립트: 28. 모든 파이썬 프로세스에 올라오는 특별한 파일 - sitecustomize.py
12840정성태9/30/20218424.NET Framework: 1119. Entity Framework의 Join 사용 시 다중 칼럼에 대한 OR 조건 쿼리파일 다운로드1
12839정성태9/15/20219490.NET Framework: 1118. C# 11 - 제네릭 타입의 특성 적용파일 다운로드1
12838정성태9/13/20219143.NET Framework: 1117. C# - Task에 전달한 Action, Func 유형에 따라 달라지는 async/await 비동기 처리 [2]파일 다운로드1
12837정성태9/11/20218086VC++: 151. Golang - fmt.Errorf, errors.Is, errors.As 설명
12836정성태9/10/20217667Linux: 45. 리눅스 - 실행 중인 다른 프로그램의 출력을 확인하는 방법
12835정성태9/7/20218946.NET Framework: 1116. C# 10 - (15) CallerArgumentExpression 특성 추가 [2]파일 다운로드1
12834정성태9/7/20217318오류 유형: 762. Visual Studio 2019 Build Tools - 'C:\Program' is not recognized as an internal or external command, operable program or batch file.
12833정성태9/6/20216770VC++: 150. Golang - TCP client/server echo 예제 코드파일 다운로드1
12832정성태9/6/20217607VC++: 149. Golang - 인터페이스 포인터가 의미 있을까요?
12831정성태9/6/20216149VC++: 148. Golang - 채널에 따른 다중 작업 처리파일 다운로드1
12830정성태9/6/20218364오류 유형: 761. Internet Explorer에서 파일 다운로드 시 "Your current security settings do not allow this file to be downloaded." 오류
12829정성태9/5/202110015.NET Framework: 1115. C# 10 - (14) 구조체 타입에 기본 생성자 정의 가능파일 다운로드1
12828정성태9/4/20218144.NET Framework: 1114. C# 10 - (13) 단일 파일 내에 적용되는 namespace 선언파일 다운로드1
12827정성태9/4/20218125스크립트: 27. 파이썬 - 웹 페이지 데이터 수집을 위한 scrapy Crawler 사용법 요약
12826정성태9/3/202110370.NET Framework: 1113. C# 10 - (12) 문자열 보간 성능 개선 [1]파일 다운로드1
12825정성태9/3/20217923개발 환경 구성: 603. GoLand - WSL 환경과 연동
12824정성태9/2/202116998오류 유형: 760. 파이썬 tensorflow - Dst tensor is not initialized. 오류 메시지
12823정성태9/2/20216737스크립트: 26. 파이썬 - PyCharm을 이용한 fork 디버그 방법
12822정성태9/1/202111944오류 유형: 759. 파이썬 tensorflow - ValueError: Shapes (...) and (...) are incompatible [2]
12821정성태9/1/20217494.NET Framework: 1112. C# - .NET 6부터 공개된 ISpanFormattable 사용법
12820정성태9/1/20217795VC++: 147. Golang - try/catch에 대응하는 panic/recover [1]파일 다운로드1
... [31]  32  33  34  35  36  37  38  39  40  41  42  43  44  45  ...