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
정성태

... [16]  17  18  19  20  21  22  23  24  25  26  27  28  29  30  ...
NoWriterDateCnt.TitleFile(s)
13222정성태1/20/20233932개발 환경 구성: 657. WSL - DockerDesktop.vhdx 파일 위치를 옮기는 방법
13221정성태1/19/20234164Linux: 57. C# - 리눅스 프로세스 메모리 정보파일 다운로드1
13220정성태1/19/20234314오류 유형: 837. NETSDK1045 The current .NET SDK does not support targeting .NET ...
13219정성태1/18/20233872Windows: 220. 네트워크의 인터넷 접속 가능 여부에 대한 판단 기준
13218정성태1/17/20233797VS.NET IDE: 178. Visual Studio 17.5 (Preview 2) - 포트 터널링을 이용한 웹 응용 프로그램의 외부 접근 허용
13217정성태1/13/20234391디버깅 기술: 185. windbg - 64비트 운영체제에서 작업 관리자로 뜬 32비트 프로세스의 덤프를 sos로 디버깅하는 방법
13216정성태1/12/20234655디버깅 기술: 184. windbg - 32비트 프로세스의 메모리 덤프인 경우 !peb 명령어로 나타나지 않는 환경 변수
13215정성태1/11/20236162Linux: 56. 리눅스 - /proc/pid/stat 정보를 이용해 프로세스의 CPU 사용량 구하는 방법 [1]
13214정성태1/10/20235731.NET Framework: 2087. .NET 6부터 SourceGenerator와 통합된 System.Text.Json [1]파일 다운로드1
13213정성태1/9/20235270오류 유형: 836. docker 이미지 빌드 시 "RUN apt install ..." 명령어가 실패하는 이유
13212정성태1/8/20235029기타: 85. 단정도/배정도 부동 소수점의 정밀도(Precision)에 따른 형변환 손실
13211정성태1/6/20235112웹: 42. (https가 아닌) http 다운로드를 막는 웹 브라우저
13210정성태1/5/20234132Windows: 219. 윈도우 x64의 경우 0x00000000`7ffe0000 아래의 주소는 왜 사용하지 않을까요?
13209정성태1/4/20234033Windows: 218. 왜 윈도우에서 가상 메모리 공간은 64KB 정렬이 된 걸까요?
13208정성태1/3/20233964.NET Framework: 2086. C# - Windows 운영체제의 2MB Large 페이지 크기 할당 방법파일 다운로드1
13207정성태12/26/20224270.NET Framework: 2085. C# - gpedit.msc의 "User Rights Assignment" 특권을 코드로 설정/해제하는 방법파일 다운로드1
13206정성태12/24/20224476.NET Framework: 2084. C# - GetTokenInformation으로 사용자 SID(Security identifiers) 구하는 방법 [3]파일 다운로드1
13205정성태12/24/20224872.NET Framework: 2083. C# - C++과의 연동을 위한 구조체의 fixed 배열 필드 사용 (2)파일 다운로드1
13204정성태12/22/20224152.NET Framework: 2082. C# - (LSA_UNICODE_STRING 예제로) CustomMarshaler 사용법파일 다운로드1
13203정성태12/22/20224312.NET Framework: 2081. C# Interop 예제 - (LSA_UNICODE_STRING 예제로) 구조체를 C++에 전달하는 방법파일 다운로드1
13202정성태12/21/20224705기타: 84. 직렬화로 설명하는 Little/Big Endian파일 다운로드1
13201정성태12/20/20225330오류 유형: 835. PyCharm 사용 시 C 드라이브 용량 부족
13200정성태12/19/20224204오류 유형: 834. 이벤트 로그 - SSL Certificate Settings created by an admin process for endpoint
13199정성태12/19/20224492개발 환경 구성: 656. Internal Network 유형의 스위치로 공유한 Hyper-V의 VM과 호스트가 통신이 안 되는 경우
13198정성태12/18/20224369.NET Framework: 2080. C# - Microsoft.XmlSerializer.Generator 처리 없이 XmlSerializer 생성자를 예외 없이 사용하고 싶다면?파일 다운로드1
13197정성태12/17/20224308.NET Framework: 2079. .NET Core/5+ 환경에서 XmlSerializer 사용 시 System.IO.FileNotFoundException 예외 발생하는 경우파일 다운로드1
... [16]  17  18  19  20  21  22  23  24  25  26  27  28  29  30  ...