|
1 | 1 | #include "bad_usb_hid.h"
|
2 | 2 | #include <extra_profiles/hid_profile.h>
|
3 |
| -#include <bt/bt_service/bt.h> |
4 | 3 | #include <storage/storage.h>
|
5 | 4 |
|
6 | 5 | #define TAG "BadUSB HID"
|
7 | 6 |
|
8 |
| -#define HID_BT_KEYS_STORAGE_NAME ".bt_hid.keys" |
9 |
| - |
10 | 7 | void* hid_usb_init(FuriHalUsbHidConfig* hid_cfg) {
|
11 | 8 | furi_check(furi_hal_usb_set_config(&usb_hid, hid_cfg));
|
12 | 9 | return NULL;
|
@@ -72,155 +69,6 @@ static const BadUsbHidApi hid_api_usb = {
|
72 | 69 | .release_all = hid_usb_release_all,
|
73 | 70 | .get_led_state = hid_usb_get_led_state,
|
74 | 71 | };
|
75 |
| - |
76 |
| -typedef struct { |
77 |
| - Bt* bt; |
78 |
| - FuriHalBleProfileBase* profile; |
79 |
| - HidStateCallback state_callback; |
80 |
| - void* callback_context; |
81 |
| - bool is_connected; |
82 |
| -} BleHidInstance; |
83 |
| - |
84 |
| -static const BleProfileHidParams ble_hid_params = { |
85 |
| - .device_name_prefix = "BadUSB", |
86 |
| - .mac_xor = 0x0002, |
87 |
| -}; |
88 |
| - |
89 |
| -static void hid_ble_connection_status_callback(BtStatus status, void* context) { |
90 |
| - furi_assert(context); |
91 |
| - BleHidInstance* ble_hid = context; |
92 |
| - ble_hid->is_connected = (status == BtStatusConnected); |
93 |
| - if(ble_hid->state_callback) { |
94 |
| - ble_hid->state_callback(ble_hid->is_connected, ble_hid->callback_context); |
95 |
| - } |
96 |
| -} |
97 |
| - |
98 |
| -void* hid_ble_init(FuriHalUsbHidConfig* hid_cfg) { |
99 |
| - UNUSED(hid_cfg); |
100 |
| - BleHidInstance* ble_hid = malloc(sizeof(BleHidInstance)); |
101 |
| - ble_hid->bt = furi_record_open(RECORD_BT); |
102 |
| - bt_disconnect(ble_hid->bt); |
103 |
| - |
104 |
| - // Wait 2nd core to update nvm storage |
105 |
| - furi_delay_ms(200); |
106 |
| - |
107 |
| - bt_keys_storage_set_storage_path(ble_hid->bt, APP_DATA_PATH(HID_BT_KEYS_STORAGE_NAME)); |
108 |
| - |
109 |
| - ble_hid->profile = bt_profile_start(ble_hid->bt, ble_profile_hid, (void*)&ble_hid_params); |
110 |
| - furi_check(ble_hid->profile); |
111 |
| - |
112 |
| - furi_hal_bt_start_advertising(); |
113 |
| - |
114 |
| - bt_set_status_changed_callback(ble_hid->bt, hid_ble_connection_status_callback, ble_hid); |
115 |
| - |
116 |
| - return ble_hid; |
117 |
| -} |
118 |
| - |
119 |
| -void hid_ble_deinit(void* inst) { |
120 |
| - BleHidInstance* ble_hid = inst; |
121 |
| - furi_assert(ble_hid); |
122 |
| - |
123 |
| - bt_set_status_changed_callback(ble_hid->bt, NULL, NULL); |
124 |
| - bt_disconnect(ble_hid->bt); |
125 |
| - |
126 |
| - // Wait 2nd core to update nvm storage |
127 |
| - furi_delay_ms(200); |
128 |
| - bt_keys_storage_set_default_path(ble_hid->bt); |
129 |
| - |
130 |
| - furi_check(bt_profile_restore_default(ble_hid->bt)); |
131 |
| - furi_record_close(RECORD_BT); |
132 |
| - free(ble_hid); |
133 |
| -} |
134 |
| - |
135 |
| -void hid_ble_set_state_callback(void* inst, HidStateCallback cb, void* context) { |
136 |
| - BleHidInstance* ble_hid = inst; |
137 |
| - furi_assert(ble_hid); |
138 |
| - ble_hid->state_callback = cb; |
139 |
| - ble_hid->callback_context = context; |
140 |
| -} |
141 |
| - |
142 |
| -bool hid_ble_is_connected(void* inst) { |
143 |
| - BleHidInstance* ble_hid = inst; |
144 |
| - furi_assert(ble_hid); |
145 |
| - return ble_hid->is_connected; |
146 |
| -} |
147 |
| - |
148 |
| -bool hid_ble_kb_press(void* inst, uint16_t button) { |
149 |
| - BleHidInstance* ble_hid = inst; |
150 |
| - furi_assert(ble_hid); |
151 |
| - return ble_profile_hid_kb_press(ble_hid->profile, button); |
152 |
| -} |
153 |
| - |
154 |
| -bool hid_ble_kb_release(void* inst, uint16_t button) { |
155 |
| - BleHidInstance* ble_hid = inst; |
156 |
| - furi_assert(ble_hid); |
157 |
| - return ble_profile_hid_kb_release(ble_hid->profile, button); |
158 |
| -} |
159 |
| - |
160 |
| -bool hid_ble_consumer_press(void* inst, uint16_t button) { |
161 |
| - BleHidInstance* ble_hid = inst; |
162 |
| - furi_assert(ble_hid); |
163 |
| - return ble_profile_hid_consumer_key_press(ble_hid->profile, button); |
164 |
| -} |
165 |
| - |
166 |
| -bool hid_ble_consumer_release(void* inst, uint16_t button) { |
167 |
| - BleHidInstance* ble_hid = inst; |
168 |
| - furi_assert(ble_hid); |
169 |
| - return ble_profile_hid_consumer_key_release(ble_hid->profile, button); |
170 |
| -} |
171 |
| - |
172 |
| -bool hid_ble_release_all(void* inst) { |
173 |
| - BleHidInstance* ble_hid = inst; |
174 |
| - furi_assert(ble_hid); |
175 |
| - bool state = ble_profile_hid_kb_release_all(ble_hid->profile); |
176 |
| - state &= ble_profile_hid_consumer_key_release_all(ble_hid->profile); |
177 |
| - return state; |
178 |
| -} |
179 |
| - |
180 |
| -uint8_t hid_ble_get_led_state(void* inst) { |
181 |
| - UNUSED(inst); |
182 |
| - FURI_LOG_W(TAG, "hid_ble_get_led_state not implemented"); |
183 |
| - return 0; |
184 |
| -} |
185 |
| - |
186 |
| -static const BadUsbHidApi hid_api_ble = { |
187 |
| - .init = hid_ble_init, |
188 |
| - .deinit = hid_ble_deinit, |
189 |
| - .set_state_callback = hid_ble_set_state_callback, |
190 |
| - .is_connected = hid_ble_is_connected, |
191 |
| - |
192 |
| - .kb_press = hid_ble_kb_press, |
193 |
| - .kb_release = hid_ble_kb_release, |
194 |
| - .consumer_press = hid_ble_consumer_press, |
195 |
| - .consumer_release = hid_ble_consumer_release, |
196 |
| - .release_all = hid_ble_release_all, |
197 |
| - .get_led_state = hid_ble_get_led_state, |
198 |
| -}; |
199 |
| - |
200 |
| -const BadUsbHidApi* bad_usb_hid_get_interface(BadUsbHidInterface interface) { |
201 |
| - if(interface == BadUsbHidInterfaceUsb) { |
202 |
| - return &hid_api_usb; |
203 |
| - } else { |
204 |
| - return &hid_api_ble; |
205 |
| - } |
206 |
| -} |
207 |
| - |
208 |
| -void bad_usb_hid_ble_remove_pairing(void) { |
209 |
| - Bt* bt = furi_record_open(RECORD_BT); |
210 |
| - bt_disconnect(bt); |
211 |
| - |
212 |
| - // Wait 2nd core to update nvm storage |
213 |
| - furi_delay_ms(200); |
214 |
| - |
215 |
| - furi_hal_bt_stop_advertising(); |
216 |
| - |
217 |
| - bt_keys_storage_set_storage_path(bt, APP_DATA_PATH(HID_BT_KEYS_STORAGE_NAME)); |
218 |
| - bt_forget_bonded_devices(bt); |
219 |
| - |
220 |
| - // Wait 2nd core to update nvm storage |
221 |
| - furi_delay_ms(200); |
222 |
| - bt_keys_storage_set_default_path(bt); |
223 |
| - |
224 |
| - furi_check(bt_profile_restore_default(bt)); |
225 |
| - furi_record_close(RECORD_BT); |
| 72 | +const BadUsbHidApi* bad_usb_hid_get_interface() { |
| 73 | + return &hid_api_usb; |
226 | 74 | }
|
0 commit comments