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

1  2  3  4  5  6  7  8  9  10  11  12  [13]  14  15  ...
NoWriterDateCnt.TitleFile(s)
13302정성태3/30/20234287Windows: 240. RDP 환경에서 바뀌는 %TEMP% 디렉터리 경로
13301정성태3/29/20234381Windows: 239. C/C++ - Windows 10 Version 1607부터 지원하는 /DEPENDENTLOADFLAG 옵션파일 다운로드1
13300정성태3/28/20234022Windows: 238. Win32 - Modal UI 창에 올바른 Owner(HWND)를 설정해야 하는 이유
13299정성태3/27/20233791Windows: 237. Win32 - 모든 메시지 루프를 탈출하는 WM_QUIT 메시지
13298정성태3/27/20233749Windows: 236. Win32 - MessageBeep 소리가 안 들린다면?
13297정성태3/26/20234410Windows: 235. Win32 - Code Modal과 UI Modal
13296정성태3/25/20233766Windows: 234. IsDialogMessage와 협업하는 WM_GETDLGCODE Win32 메시지 [1]파일 다운로드1
13295정성태3/24/20234041Windows: 233. Win32 - modeless 대화창을 modal처럼 동작하게 만드는 방법파일 다운로드1
13294정성태3/22/20234209.NET Framework: 2105. LargeAddressAware 옵션이 적용된 닷넷 32비트 프로세스의 가용 메모리 - 두 번째
13293정성태3/22/20234252오류 유형: 853. dumpbin - warning LNK4048: Invalid format file; ignored
13292정성태3/21/20234387Windows: 232. C/C++ - 일반 창에도 사용 가능한 IsDialogMessage파일 다운로드1
13291정성태3/20/20234801.NET Framework: 2104. C# Windows Forms - WndProc 재정의와 IMessageFilter 사용 시의 차이점
13290정성태3/19/20234301.NET Framework: 2103. C# - 윈도우에서 기본 제공하는 FindText 대화창 사용법파일 다운로드1
13289정성태3/18/20233481Windows: 231. Win32 - 대화창 템플릿의 2진 리소스를 읽어들여 자식 윈도우를 생성하는 방법파일 다운로드1
13288정성태3/17/20233595Windows: 230. Win32 - 대화창의 DLU 단위를 pixel로 변경하는 방법파일 다운로드1
13287정성태3/16/20233761Windows: 229. Win32 - 대화창 템플릿의 2진 리소스를 읽어들여 윈도우를 직접 띄우는 방법파일 다운로드1
13286정성태3/15/20234199Windows: 228. Win32 - 리소스에 포함된 대화창 Template의 2진 코드 해석 방법
13285정성태3/14/20233790Windows: 227. Win32 C/C++ - Dialog Procedure를 재정의하는 방법파일 다운로드1
13284정성태3/13/20234013Windows: 226. Win32 C/C++ - Dialog에서 값을 반환하는 방법파일 다운로드1
13283정성태3/12/20233553오류 유형: 852. 파이썬 - TypeError: coercing to Unicode: need string or buffer, NoneType found
13282정성태3/12/20233889Linux: 58. WSL - nohup 옵션이 필요한 경우
13281정성태3/12/20233807Windows: 225. 윈도우 바탕화면의 아이콘들이 넓게 퍼지는 경우 [2]
13280정성태3/9/20234568개발 환경 구성: 670. WSL 2에서 호스팅 중인 TCP 서버를 외부에서 접근하는 방법
13279정성태3/9/20234085오류 유형: 851. 파이썬 ModuleNotFoundError: No module named '_cffi_backend'
13278정성태3/8/20234111개발 환경 구성: 669. WSL 2의 (init이 아닌) systemd 지원 [1]
13277정성태3/6/20234733개발 환경 구성: 668. 코드 사인용 인증서 신청 및 적용 방법(예: Digicert)
1  2  3  4  5  6  7  8  9  10  11  12  [13]  14  15  ...