Bad BT plugin, Submenu locked elements, API updates, etc.

Thanks to WillyJL, ClaraCrazy, and XFW contributors
This commit is contained in:
MX
2023-05-13 00:14:22 +03:00
parent a7691b2d3b
commit 849f14e480
42 changed files with 3211 additions and 44 deletions

View File

@@ -199,26 +199,39 @@ bool furi_hal_bt_start_app(FuriHalBtProfile profile, GapEventCallback event_cb,
FURI_LOG_E(TAG, "Can't start Ble App - unsupported radio stack");
break;
}
// Set mac address
memcpy(
profile_config[profile].config.mac_address,
furi_hal_version_get_ble_mac(),
sizeof(profile_config[profile].config.mac_address));
// Set advertise name
strlcpy(
profile_config[profile].config.adv_name,
furi_hal_version_get_ble_local_device_name_ptr(),
FURI_HAL_VERSION_DEVICE_NAME_LENGTH);
// Configure GAP
GapConfig* config = &profile_config[profile].config;
// Configure GAP
if(profile == FuriHalBtProfileSerial) {
// Set mac address
memcpy(
config->mac_address, furi_hal_version_get_ble_mac(), sizeof(config->mac_address));
// Set advertise name
strlcpy(
config->adv_name,
furi_hal_version_get_ble_local_device_name_ptr(),
FURI_HAL_VERSION_DEVICE_NAME_LENGTH);
config->adv_service_uuid |= furi_hal_version_get_hw_color();
} else if(profile == FuriHalBtProfileHidKeyboard) {
// Change MAC address for HID profile
config->mac_address[2]++;
uint8_t default_mac[sizeof(config->mac_address)] = FURI_HAL_BT_DEFAULT_MAC_ADDR;
const uint8_t* normal_mac = furi_hal_version_get_ble_mac();
if(memcmp(config->mac_address, default_mac, sizeof(config->mac_address)) == 0) {
memcpy(config->mac_address, normal_mac, sizeof(config->mac_address));
}
if(memcmp(config->mac_address, normal_mac, sizeof(config->mac_address)) == 0) {
config->mac_address[2]++;
}
// Change name Flipper -> Control
const char* clicker_str = "Control";
memcpy(&config->adv_name[1], clicker_str, strlen(clicker_str));
if(strnlen(config->adv_name, FURI_HAL_VERSION_DEVICE_NAME_LENGTH) < 2 ||
strnlen(config->adv_name + 1, FURI_HAL_VERSION_DEVICE_NAME_LENGTH) < 1) {
snprintf(
config->adv_name,
FURI_HAL_VERSION_DEVICE_NAME_LENGTH,
"%cControl %s",
*furi_hal_version_get_ble_local_device_name_ptr(),
furi_hal_version_get_ble_local_device_name_ptr() + 9);
}
}
if(!gap_init(config, event_cb, context)) {
gap_thread_stop();
@@ -280,6 +293,10 @@ bool furi_hal_bt_is_active() {
return gap_get_state() > GapStateIdle;
}
bool furi_hal_bt_is_connected() {
return gap_get_state() == GapStateConnected;
}
void furi_hal_bt_start_advertising() {
if(gap_get_state() == GapStateIdle) {
gap_start_advertising();
@@ -418,6 +435,67 @@ float furi_hal_bt_get_rssi() {
return val;
}
/** fill the RSSI of the remote host of the bt connection and returns the last
* time the RSSI was updated
*
*/
uint32_t furi_hal_bt_get_conn_rssi(uint8_t* rssi) {
int8_t ret_rssi = 0;
uint32_t since = gap_get_remote_conn_rssi(&ret_rssi);
if(ret_rssi == 127 || since == 0) return 0;
*rssi = (uint8_t)abs(ret_rssi);
return since;
}
void furi_hal_bt_set_profile_adv_name(
FuriHalBtProfile profile,
const char name[FURI_HAL_BT_ADV_NAME_LENGTH]) {
furi_assert(profile < FuriHalBtProfileNumber);
furi_assert(name);
if(strlen(name) == 0) {
memset(
&(profile_config[profile].config.adv_name[1]),
0,
strlen(&(profile_config[profile].config.adv_name[1])));
} else {
profile_config[profile].config.adv_name[0] = AD_TYPE_COMPLETE_LOCAL_NAME;
memcpy(&(profile_config[profile].config.adv_name[1]), name, FURI_HAL_BT_ADV_NAME_LENGTH);
}
}
const char* furi_hal_bt_get_profile_adv_name(FuriHalBtProfile profile) {
furi_assert(profile < FuriHalBtProfileNumber);
return &(profile_config[profile].config.adv_name[1]);
}
void furi_hal_bt_set_profile_mac_addr(
FuriHalBtProfile profile,
const uint8_t mac_addr[GAP_MAC_ADDR_SIZE]) {
furi_assert(profile < FuriHalBtProfileNumber);
furi_assert(mac_addr);
memcpy(profile_config[profile].config.mac_address, mac_addr, GAP_MAC_ADDR_SIZE);
}
const uint8_t* furi_hal_bt_get_profile_mac_addr(FuriHalBtProfile profile) {
furi_assert(profile < FuriHalBtProfileNumber);
return profile_config[profile].config.mac_address;
}
void furi_hal_bt_set_profile_pairing_method(FuriHalBtProfile profile, GapPairing pairing_method) {
furi_assert(profile < FuriHalBtProfileNumber);
profile_config[profile].config.pairing_method = pairing_method;
}
GapPairing furi_hal_bt_get_profile_pairing_method(FuriHalBtProfile profile) {
furi_assert(profile < FuriHalBtProfileNumber);
return profile_config[profile].config.pairing_method;
}
uint32_t furi_hal_bt_get_transmitted_packets() {
uint32_t packets = 0;
aci_hal_le_tx_test_packet_number(&packets);

View File

@@ -20,6 +20,7 @@ enum HidReportId {
ReportIdKeyboard = 1,
ReportIdMouse = 2,
ReportIdConsumer = 3,
ReportIdLEDState = 4,
};
// Report numbers corresponded to the report id with an offset of 1
enum HidInputNumber {
@@ -77,6 +78,13 @@ static const uint8_t furi_hal_bt_hid_report_map_data[] = {
HID_USAGE_MINIMUM(0),
HID_USAGE_MAXIMUM(101),
HID_INPUT(HID_IOF_DATA | HID_IOF_ARRAY | HID_IOF_ABSOLUTE),
HID_REPORT_ID(ReportIdLEDState),
HID_USAGE_PAGE(HID_PAGE_LED),
HID_REPORT_COUNT(8),
HID_REPORT_SIZE(1),
HID_USAGE_MINIMUM(1),
HID_USAGE_MAXIMUM(8),
HID_OUTPUT(HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE),
HID_END_COLLECTION,
// Mouse Report
HID_USAGE_PAGE(HID_PAGE_DESKTOP),
@@ -125,6 +133,63 @@ FuriHalBtHidKbReport* kb_report = NULL;
FuriHalBtHidMouseReport* mouse_report = NULL;
FuriHalBtHidConsumerReport* consumer_report = NULL;
typedef struct {
// shortcuts
#define s_undefined data.bits.b_undefined
#define s_num_lock data.bits.b_num_lock
#define s_caps_lock data.bits.b_caps_lock
#define s_scroll_lock data.bits.b_scroll_lock
#define s_compose data.bits.b_compose
#define s_kana data.bits.b_kana
#define s_power data.bits.b_power
#define s_shift data.bits.b_shift
#define s_value data.value
union {
struct {
uint8_t b_undefined : 1;
uint8_t b_num_lock : 1;
uint8_t b_caps_lock : 1;
uint8_t b_scroll_lock : 1;
uint8_t b_compose : 1;
uint8_t b_kana : 1;
uint8_t b_power : 1;
uint8_t b_shift : 1;
} bits;
uint8_t value;
} data;
} __attribute__((__packed__)) FuriHalBtHidLedState;
FuriHalBtHidLedState hid_host_led_state = {.s_value = 0};
uint16_t furi_hal_bt_hid_led_state_cb(uint8_t state, void* ctx) {
FuriHalBtHidLedState* led_state = (FuriHalBtHidLedState*)ctx;
//FURI_LOG_D("HalBtHid", "LED state updated !");
led_state->s_value = state;
return 0;
}
uint8_t furi_hal_bt_hid_get_led_state(void) {
/*FURI_LOG_D(
"HalBtHid",
"LED state: RFU=%d NUMLOCK=%d CAPSLOCK=%d SCROLLLOCK=%d COMPOSE=%d KANA=%d POWER=%d SHIFT=%d",
hid_host_led_state.s_undefined,
hid_host_led_state.s_num_lock,
hid_host_led_state.s_caps_lock,
hid_host_led_state.s_scroll_lock,
hid_host_led_state.s_compose,
hid_host_led_state.s_kana,
hid_host_led_state.s_power,
hid_host_led_state.s_shift);
*/
return (hid_host_led_state.s_value >> 1); // bit 0 is undefined (after shift bit location
// match with HID led state bits defines)
// see bad_bt_script.c (ducky_numlock_on function)
}
void furi_hal_bt_hid_start() {
// Start device info
if(!dev_info_svc_is_started()) {
@@ -139,6 +204,8 @@ void furi_hal_bt_hid_start() {
hid_svc_start();
}
// Configure HID Keyboard
hid_svc_register_led_state_callback(furi_hal_bt_hid_led_state_cb, &hid_host_led_state);
kb_report = malloc(sizeof(FuriHalBtHidKbReport));
mouse_report = malloc(sizeof(FuriHalBtHidMouseReport));
consumer_report = malloc(sizeof(FuriHalBtHidConsumerReport));
@@ -160,6 +227,8 @@ void furi_hal_bt_hid_stop() {
furi_assert(kb_report);
furi_assert(mouse_report);
furi_assert(consumer_report);
hid_svc_register_led_state_callback(NULL, NULL);
// Stop all services
if(dev_info_svc_is_started()) {
dev_info_svc_stop();
@@ -180,12 +249,16 @@ void furi_hal_bt_hid_stop() {
bool furi_hal_bt_hid_kb_press(uint16_t button) {
furi_assert(kb_report);
for(uint8_t i = 0; i < FURI_HAL_BT_HID_KB_MAX_KEYS; i++) {
uint8_t i;
for(i = 0; i < FURI_HAL_BT_HID_KB_MAX_KEYS; i++) {
if(kb_report->key[i] == 0) {
kb_report->key[i] = button & 0xFF;
break;
}
}
if(i == FURI_HAL_BT_HID_KB_MAX_KEYS) {
return false;
}
kb_report->mods |= (button >> 8);
return hid_svc_update_input_report(
ReportNumberKeyboard, (uint8_t*)kb_report, sizeof(FuriHalBtHidKbReport));