Bluetooth Rubber Ducky (https://forums.hak5.org/topic/41678-bluetooth-rubber-ducky/)
Dependencies: BLE_API mbed nRF51822
Fork of microbit_presenter by
- Backspace = x08
- (F5) = \x86
- (F11) = \x8A
- (F12) = \x8B
- Print Screen = \x8C
- Application Program Command (CTRL+8) \x9F
- (‽)RALT LCTRL RSHIFT LSHIFT F1 LCTRL = \u203d + \u2038 (v.similar)
- Down F1 LCTRL = \u2016
- F1 F1 LCTRL = \u2000
- LALT LSHIFT F2 LCTRL = \u206F
- Insert = \x90
- Home = \x91
- Pageup = \x92
- PageDown = \x93
- Right Shift + Left Control = \uF8FF
- Alt Shift + F (File >) = \x99
- CTRL+B = \x98
- Right Ctrl = \x9a
- Escape + stuff, \x9d
- Test on another PC (rwin) \x9c
- Scroll = \x8d
- Capslock = \x8e
- Numlock = \x8f
- Left = \x95
- Right = \x94
- Up = \x97
- Down = \x96
Full list of special command characters here.
keyboard_stream.cpp@3:dc7a04d715a2, 2017-08-21 (annotated)
- Committer:
- CalvinR
- Date:
- Mon Aug 21 12:24:51 2017 +0000
- Revision:
- 3:dc7a04d715a2
- Parent:
- 1:9e174f8fd9e9
v1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JonnyA | 0:cb1939018833 | 1 | /* mbed Microcontroller Library |
JonnyA | 0:cb1939018833 | 2 | * Copyright (c) 2015 ARM Limited |
JonnyA | 0:cb1939018833 | 3 | * |
JonnyA | 0:cb1939018833 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
JonnyA | 0:cb1939018833 | 5 | * you may not use this file except in compliance with the License. |
JonnyA | 0:cb1939018833 | 6 | * You may obtain a copy of the License at |
JonnyA | 0:cb1939018833 | 7 | * |
JonnyA | 0:cb1939018833 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
JonnyA | 0:cb1939018833 | 9 | * |
JonnyA | 0:cb1939018833 | 10 | * Unless required by applicable law or agreed to in writing, software |
JonnyA | 0:cb1939018833 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
JonnyA | 0:cb1939018833 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
JonnyA | 0:cb1939018833 | 13 | * See the License for the specific language governing permissions and |
JonnyA | 0:cb1939018833 | 14 | * limitations under the License. |
JonnyA | 0:cb1939018833 | 15 | */ |
JonnyA | 0:cb1939018833 | 16 | |
JonnyA | 0:cb1939018833 | 17 | #include "mbed.h" |
JonnyA | 0:cb1939018833 | 18 | |
JonnyA | 0:cb1939018833 | 19 | #include "ble/BLE.h" |
JonnyA | 0:cb1939018833 | 20 | #include "KeyboardService.h" |
JonnyA | 0:cb1939018833 | 21 | |
JonnyA | 0:cb1939018833 | 22 | #include "examples_common.h" |
JonnyA | 0:cb1939018833 | 23 | |
JonnyA | 0:cb1939018833 | 24 | /** |
JonnyA | 0:cb1939018833 | 25 | * This program implements a complete HID-over-Gatt Profile: |
JonnyA | 0:cb1939018833 | 26 | * - HID is provided by KeyboardService |
JonnyA | 0:cb1939018833 | 27 | * - Battery Service |
JonnyA | 0:cb1939018833 | 28 | * - Device Information Service |
JonnyA | 0:cb1939018833 | 29 | * |
JonnyA | 0:cb1939018833 | 30 | * Complete strings can be sent over BLE using printf. Please note, however, than a 12char string |
JonnyA | 0:cb1939018833 | 31 | * will take about 500ms to transmit, principally because of the limited notification rate in BLE. |
JonnyA | 0:cb1939018833 | 32 | * KeyboardService uses a circular buffer to store the strings to send, and calls to putc will fail |
JonnyA | 0:cb1939018833 | 33 | * once this buffer is full. This will result in partial strings being sent to the client. |
JonnyA | 0:cb1939018833 | 34 | */ |
JonnyA | 1:9e174f8fd9e9 | 35 | |
JonnyA | 1:9e174f8fd9e9 | 36 | //The micro:bit has a matrixed display, this is a simple way to use some LEDs on it |
JonnyA | 1:9e174f8fd9e9 | 37 | DigitalOut col9(P0_12, 0); |
JonnyA | 0:cb1939018833 | 38 | |
JonnyA | 1:9e174f8fd9e9 | 39 | DigitalOut waiting_led(P0_13); |
JonnyA | 1:9e174f8fd9e9 | 40 | DigitalOut connected_led(P0_15); |
JonnyA | 0:cb1939018833 | 41 | |
JonnyA | 0:cb1939018833 | 42 | InterruptIn button1(BUTTON_A); |
JonnyA | 0:cb1939018833 | 43 | InterruptIn button2(BUTTON_B); |
JonnyA | 0:cb1939018833 | 44 | |
JonnyA | 0:cb1939018833 | 45 | BLE ble; |
JonnyA | 0:cb1939018833 | 46 | KeyboardService *kbdServicePtr; |
JonnyA | 0:cb1939018833 | 47 | |
CalvinR | 3:dc7a04d715a2 | 48 | static const char DEVICE_NAME[] = "micro:bit Rubber Ducky"; |
CalvinR | 3:dc7a04d715a2 | 49 | static const char SHORT_DEVICE_NAME[] = "ducky"; |
JonnyA | 0:cb1939018833 | 50 | |
JonnyA | 0:cb1939018833 | 51 | static void onDisconnect(const Gap::DisconnectionCallbackParams_t *params) |
JonnyA | 0:cb1939018833 | 52 | { |
JonnyA | 0:cb1939018833 | 53 | HID_DEBUG("disconnected\r\n"); |
JonnyA | 0:cb1939018833 | 54 | connected_led = 0; |
JonnyA | 0:cb1939018833 | 55 | |
JonnyA | 0:cb1939018833 | 56 | ble.gap().startAdvertising(); // restart advertising |
JonnyA | 0:cb1939018833 | 57 | } |
JonnyA | 0:cb1939018833 | 58 | |
JonnyA | 0:cb1939018833 | 59 | static void onConnect(const Gap::ConnectionCallbackParams_t *params) |
JonnyA | 0:cb1939018833 | 60 | { |
JonnyA | 0:cb1939018833 | 61 | HID_DEBUG("connected\r\n"); |
JonnyA | 0:cb1939018833 | 62 | waiting_led = false; |
JonnyA | 0:cb1939018833 | 63 | } |
JonnyA | 0:cb1939018833 | 64 | |
JonnyA | 0:cb1939018833 | 65 | static void waiting() { |
JonnyA | 0:cb1939018833 | 66 | if (!kbdServicePtr->isConnected()) |
JonnyA | 0:cb1939018833 | 67 | waiting_led = !waiting_led; |
JonnyA | 0:cb1939018833 | 68 | else |
JonnyA | 0:cb1939018833 | 69 | connected_led = !connected_led; |
JonnyA | 0:cb1939018833 | 70 | } |
JonnyA | 0:cb1939018833 | 71 | |
JonnyA | 0:cb1939018833 | 72 | void send_string(const char * c) { |
JonnyA | 0:cb1939018833 | 73 | if (!kbdServicePtr) |
JonnyA | 0:cb1939018833 | 74 | return; |
JonnyA | 0:cb1939018833 | 75 | |
JonnyA | 0:cb1939018833 | 76 | if (!kbdServicePtr->isConnected()) { |
JonnyA | 0:cb1939018833 | 77 | HID_DEBUG("we haven't connected yet..."); |
JonnyA | 0:cb1939018833 | 78 | } else { |
JonnyA | 0:cb1939018833 | 79 | int len = strlen(c); |
JonnyA | 0:cb1939018833 | 80 | kbdServicePtr->printf(c); |
JonnyA | 0:cb1939018833 | 81 | HID_DEBUG("sending %d chars\r\n", len); |
JonnyA | 0:cb1939018833 | 82 | } |
JonnyA | 0:cb1939018833 | 83 | } |
JonnyA | 0:cb1939018833 | 84 | |
CalvinR | 3:dc7a04d715a2 | 85 | // Left = \x95 |
CalvinR | 3:dc7a04d715a2 | 86 | // Right = \x94 |
CalvinR | 3:dc7a04d715a2 | 87 | // Up = \x97 |
CalvinR | 3:dc7a04d715a2 | 88 | // Down = \x96 |
CalvinR | 3:dc7a04d715a2 | 89 | |
CalvinR | 3:dc7a04d715a2 | 90 | // Save a file with default name = "\x99,s,\n" |
CalvinR | 3:dc7a04d715a2 | 91 | |
JonnyA | 0:cb1939018833 | 92 | void send_stuff() { |
CalvinR | 3:dc7a04d715a2 | 93 | send_string("\x99,s,\n"); |
JonnyA | 0:cb1939018833 | 94 | } |
JonnyA | 0:cb1939018833 | 95 | |
JonnyA | 0:cb1939018833 | 96 | void send_more_stuff() { |
CalvinR | 3:dc7a04d715a2 | 97 | send_string("\x8C"); |
JonnyA | 0:cb1939018833 | 98 | } |
JonnyA | 0:cb1939018833 | 99 | |
JonnyA | 0:cb1939018833 | 100 | int main() |
JonnyA | 0:cb1939018833 | 101 | { |
JonnyA | 0:cb1939018833 | 102 | Ticker heartbeat; |
JonnyA | 0:cb1939018833 | 103 | |
JonnyA | 0:cb1939018833 | 104 | button1.rise(send_stuff); |
JonnyA | 0:cb1939018833 | 105 | button2.rise(send_more_stuff); |
JonnyA | 0:cb1939018833 | 106 | |
JonnyA | 0:cb1939018833 | 107 | HID_DEBUG("initialising ticker\r\n"); |
JonnyA | 0:cb1939018833 | 108 | |
JonnyA | 0:cb1939018833 | 109 | heartbeat.attach(waiting, 1); |
JonnyA | 0:cb1939018833 | 110 | |
JonnyA | 0:cb1939018833 | 111 | HID_DEBUG("initialising ble\r\n"); |
JonnyA | 0:cb1939018833 | 112 | ble.init(); |
JonnyA | 0:cb1939018833 | 113 | |
JonnyA | 0:cb1939018833 | 114 | ble.gap().onDisconnection(onDisconnect); |
JonnyA | 0:cb1939018833 | 115 | ble.gap().onConnection(onConnect); |
JonnyA | 0:cb1939018833 | 116 | |
JonnyA | 0:cb1939018833 | 117 | initializeSecurity(ble); |
JonnyA | 0:cb1939018833 | 118 | |
JonnyA | 0:cb1939018833 | 119 | HID_DEBUG("adding hid service\r\n"); |
JonnyA | 0:cb1939018833 | 120 | KeyboardService kbdService(ble); |
JonnyA | 0:cb1939018833 | 121 | kbdServicePtr = &kbdService; |
JonnyA | 0:cb1939018833 | 122 | |
JonnyA | 0:cb1939018833 | 123 | HID_DEBUG("adding device info and battery service\r\n"); |
JonnyA | 0:cb1939018833 | 124 | initializeHOGP(ble); |
JonnyA | 0:cb1939018833 | 125 | |
JonnyA | 0:cb1939018833 | 126 | HID_DEBUG("setting up gap\r\n"); |
JonnyA | 0:cb1939018833 | 127 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::KEYBOARD); |
JonnyA | 0:cb1939018833 | 128 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, |
JonnyA | 0:cb1939018833 | 129 | (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
JonnyA | 0:cb1939018833 | 130 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, |
JonnyA | 0:cb1939018833 | 131 | (uint8_t *)SHORT_DEVICE_NAME, sizeof(SHORT_DEVICE_NAME)); |
JonnyA | 1:9e174f8fd9e9 | 132 | ble.gap().setDeviceName((const uint8_t *)DEVICE_NAME); |
JonnyA | 0:cb1939018833 | 133 | |
JonnyA | 0:cb1939018833 | 134 | HID_DEBUG("advertising\r\n"); |
JonnyA | 0:cb1939018833 | 135 | ble.gap().startAdvertising(); |
JonnyA | 0:cb1939018833 | 136 | |
JonnyA | 0:cb1939018833 | 137 | while (true) { |
JonnyA | 0:cb1939018833 | 138 | ble.waitForEvent(); |
JonnyA | 0:cb1939018833 | 139 | } |
JonnyA | 0:cb1939018833 | 140 | } |
JonnyA | 0:cb1939018833 | 141 |