SlideShare une entreprise Scribd logo
1  sur  53
Carlo Pescio @CarloPescio http://eptacom.net
Carlo Pescio
Eco-friendly hardware
hacking with Android
Carlo Pescio @CarloPescio http://eptacom.net
Code in Movies
Carlo Pescio @CarloPescio http://eptacom.net
Super Heroes
- Build this in a cave
- With a bunch of scraps
Carlo Pescio @CarloPescio http://eptacom.net
Reality
Carlo Pescio @CarloPescio http://eptacom.net
It gets worse
Carlo Pescio @CarloPescio http://eptacom.net
Your ____mission
Should you choose to accept it…
- Your phone
(no rooting)
- Bunch of scraps
- No Firmware
12V
Carlo Pescio @CarloPescio http://eptacom.net
Smart > Hero
Don’t do industrial controls
using scraps
It’s OK at home
Great learning opportunity
Carlo Pescio @CarloPescio http://eptacom.net
Just be careful, ok?
Carlo Pescio @CarloPescio http://eptacom.net
Step 1: blinking an LED
Phone
+
Scraps
Carlo Pescio @CarloPescio http://eptacom.net
Choices, choices…
Built-in led[s] Audio
USB
Carlo Pescio @CarloPescio http://eptacom.net
LEDs in common USB scraps
Flash drive
Mouse
Keyboard
Printer
Carlo Pescio @CarloPescio http://eptacom.net
And the winner is…
LEDs are host-controlled
Standard protocol
LEDs outlive the keys
Easy to find
Carlo Pescio @CarloPescio http://eptacom.net
Phone – Keyboard connection
Your phone is normally a device (gadget) for a
computer (host).
A keyboard is a gadget
=> your phone must turn into a host
=> you need an OTG adapter
Carlo Pescio @CarloPescio http://eptacom.net
OTG Adapter (make or buy)
+
=
+ wire 4-5
Carlo Pescio @CarloPescio http://eptacom.net
Device / Interface / Endpoint
Device
Descriptor
Endpoint
Descriptor 1
Endpoint
Descriptor 2
…
Configuration
Descriptor 1
Configuration
Descriptor 2
Interface 0
Descriptor
Interface 1
Descriptor
Interface 0
Descriptor
Interface 1
Descriptor
… …
Kbd + trackball
Solar powered
Keyboard
Interrupt IN
USB powered
Trackball
Carlo Pescio @CarloPescio http://eptacom.net
The software side
Don’t write any code yet
Interface #0
Class: Human Interaction Device (0x3)
Endpoint: #0
Address : 129 (10000001)
Number : 1
Direction : Inbound (0x80)
Type : Intrrupt (0x3)
Poll Interval : 10
Max Packet Size: 8
Attributes : 000000011
Interface #1
Class: Human Interaction Device (0x3)
Endpoint: #0
Address : 130 (10000010)
Number : 2
Direction : Inbound (0x80)
Type : Intrrupt (0x3)
Poll Interval : 10
Max Packet Size: 3
Attributes : 000000011
Carlo Pescio @CarloPescio http://eptacom.net
The missing endpoint
Source: USB Device Class Definition for Human
Interface Devices Firmware Specification, Section 4.4
If no Interrupt Out endpoint is declared then Output
reports are transmitted to a device through the
Control endpoint.
Endpoint 0 is a Control pipe always present in USB
devices. Therefore, only the Interrupt In pipe is
described for the Interface descriptor using an
Endpoint descriptor.
Carlo Pescio @CarloPescio http://eptacom.net
Know thy APIs
You just can’t get hold of endpoint 0. But:
UsbDeviceConnection.
controlTransfer( gazillion parameters )
Performs a control transaction on endpoint zero for
this device. The direction of the transfer is
determined by the request type. If requestType &
USB_ENDPOINT_DIR_MASK is USB_DIR_OUT, then
the transfer is a write […]
Carlo Pescio @CarloPescio http://eptacom.net
7.2.2 SET_REPORT
Part Description
bmRequestType 00100001 (0x21)
bRequest SET_REPORT
wValue Report Type and Report ID
wIndex Interface
wLength Report Length
Data Report
SET_REPORT = 0x09 (Paragraph 7.2)
Carlo Pescio @CarloPescio http://eptacom.net
For LEDs / Keyboards
Report Type: 02 = Output (Paragraph 7.2.1)
Report ID: 0 = Not Used (Paragraph 7.2.1)
Interface = 0 (irrelevant here)
Report Length = 1 (1 byte, Appendix B.1)
Data: bitmask where bit 0 = NUM LOCK
bit 1 = CAPS LOCK
bit 2 = SCROLL LOCK
Carlo Pescio @CarloPescio http://eptacom.net
Sending the report
int controlTransfer(
int requestType,
int request,
int value,
int index,
byte[] buffer,
int length,
int timeout)
byte[] buf = new byte[] { b };
connection.controlTransfer(
0x21, 0x09, 0x0200, 0x0000, buf, 1, 1000);
// data [as array] -> bitmask
// wValue -> 0x0200
// wIndex -> 0x0000
// wLength -> 1
// 1 sec ok -> 1000
// bmRequestType -> 0x21
// bRequest -> 0x09
Sketch of the code
usbManager = (UsbManager)
getSystemService(Context.USB_SERVICE);
// just get the first usb device –
// should get the keyboard instead
UsbDevice device = (UsbDevice)
usbManager.getDeviceList().values().
toArray()[0];
usbManager.requestPermission(
device, permissionIntent);
Carlo Pescio @CarloPescio http://eptacom.net
… build the Keyboard
// … receive intent
UsbDevice device = (UsbDevice)
intent.getParcelableExtra(
UsbManager.EXTRA_DEVICE);
keyboard = new
UsbKeyboard(usbManager, device);
Keyboard
public UsbKeyboard(UsbManager usbManager,
UsbDevice device)
{
requestQueue = new LinkedBlockingQueue<Byte>();
connection = usbManager.openDevice(device);
// WHY AM I DOING THIS? GUESS 
ifc0 = device.getInterface(0);
connection.claimInterface(ifc0, true);
ioThread = new Thread(usbWriteLoop);
ioThread.start();
}
private Runnable usbWriteLoop = new Runnable()
{
// …
while( !stop )
{
Byte b = requestQueue.take();
if( b < 0 )
{
stop = true;
}
else
{
byte[] buf = new byte[] { b };
connection.controlTransfer(
0x21, 0x09, 0x0200,0x0000, buf, 1, 1000);
}
}
// …
};
UI
public void onCheckedChanged(
CompoundButton buttonView, boolean isChecked)
{
byte ledMask = 0;
if( led1.isChecked() )
ledMask += 1;
if( led2.isChecked() )
ledMask += 2;
if( led3.isChecked() )
ledMask += 4;
// just adds ledMask to the requestQueue
keyboard.powerLedsFromMask(ledMask);
}
Carlo Pescio @CarloPescio http://eptacom.net
We did it!
Carlo Pescio @CarloPescio http://eptacom.net
Detour: how fast can we go?
Just use an infinite loop / no sleeping or waiting:
byte[] buf0 = new byte[] { 0 };
byte[] buf1 = new byte[] { 4 };
while( true )
{
connection.controlTransfer(
0x21, 0x09, 0x0200, 0x0000, buf0, 1, 1000);
connection.controlTransfer(
0x21, 0x09, 0x0200, 0x0000, buf1, 1, 1000);
}
Carlo Pescio @CarloPescio http://eptacom.net
Hollywood loves CRTs
Fantastic Four Iron Man
Star Trek Enterprise
The Big Bang Theory
Carlo Pescio @CarloPescio http://eptacom.net
Me too!
Carlo Pescio @CarloPescio http://eptacom.net
Expected
Well, almost
No RT OS -> some (major) jitter expected
Square wave
Carlo Pescio @CarloPescio http://eptacom.net
Surprise!!
Captain America – the Winter Soldier
Carlo Pescio @CarloPescio http://eptacom.net
Kill the noise
Not exactly square but clean 
Guess the frequency!
Carlo Pescio @CarloPescio http://eptacom.net
About 1 KHz (on my device)
Carlo Pescio @CarloPescio http://eptacom.net
Conclusions from detour
Useful: we killed the noise
Things you cannot do:
- “high” frequency / low latency / low jitter stuff
- PWM
- Infrared remotes
- … etc. …
Things you can do:
- Turning on and off some stuff at low freq
Carlo Pescio @CarloPescio http://eptacom.net
No joy yet
- Different voltage (LED powered from your phone USB)
- High current (same as above)
- Generally speaking, no galvanic isolation
(whatever happens there, happens to your phone)
You can’t just put a motor where the LED is 
Carlo Pescio @CarloPescio http://eptacom.net
Step 2: Galvanic Isolation
Carlo Pescio @CarloPescio http://eptacom.net
The ubiquitous optoisolator
Carlo Pescio @CarloPescio http://eptacom.net
A small step forward…
But can’t power a
motor with that
Carlo Pescio @CarloPescio http://eptacom.net
A level of indirection…
… brings new problems
1) Who is providing the current
going through the phototransistor?
2) The phototransistor can’t handle much
current anyway
Phone
[usb]
???
Carlo Pescio @CarloPescio http://eptacom.net
Step 3: ATX is your best friend
Will solve all your problems at once 
Carlo Pescio @CarloPescio http://eptacom.net
ATX starts in stand-by
Carlo Pescio @CarloPescio http://eptacom.net
So that’s it
12 v
12K
SUPPLY
POWER
ATX
PS_ONCOM
1K1K
keyboard
1.2mA
USB
line power
Carlo Pescio @CarloPescio http://eptacom.net
Some assembly required
Carlo Pescio @CarloPescio http://eptacom.net
… and it works
Carlo Pescio @CarloPescio http://eptacom.net
What about inputs??
Well, it’s a keyboard!
you get digital inputs for free (almost)
???
Carlo Pescio @CarloPescio http://eptacom.net
Relevant code
connection = usbManager.openDevice(device);
ifc0 = device.getInterface(0);
connection.claimInterface(ifc0, true);
endPointRead = ifc0.getEndpoint(0);
while( true ) {
final byte[] buffer = new byte[8];
int transfer =
connection.bulkTransfer(endPointRead, buffer, 8, 1000);
if( transfer > 0 ) {
// [modifier,reserved,Key1,Key2,Key3,Key4,Key5,Key6]
byte key = buffer[2]; // dumps Key1 only...
if( key != 0 )
Log.e(“KEY", " " + key);
}
}
Carlo Pescio @CarloPescio http://eptacom.net
… and we’re live
Carlo Pescio @CarloPescio http://eptacom.net
Missing: galvanic isolation!
1K1KDI
ROW / COLUMN
combo
Carlo Pescio @CarloPescio http://eptacom.net
Charging while in host mode
Usually the host (phone) provides power.
OTG specification:
36.5 kΩ between pin 4 and 5
[…] The OTG device is allowed
to charge and enter host mode
Carlo Pescio @CarloPescio http://eptacom.net
May or may not work for you
If it works, ATX might be even more of a friend!
5V here also
in standby
Carlo Pescio @CarloPescio http://eptacom.net
If it doesn’t work…
- Fiddle with resistor values
- Remove battery and provide power from there
- Also useful when battery is dead
- That’s a story for another time 
Carlo Pescio @CarloPescio http://eptacom.net
Get in touch
carlo.pescio@gmail.com
@CarloPescio
http://eptacom.net

Contenu connexe

Similaire à Eco-Friendly Hardware Hacking with Android

Ak12 upgrade
Ak12 upgradeAk12 upgrade
Ak12 upgrade
Accenture
 
Ccna lab manual 640 802
Ccna lab manual 640 802Ccna lab manual 640 802
Ccna lab manual 640 802
manikkan
 
Vista 1600 c epon olt quick start manual(r1.2)
Vista 1600 c epon olt quick start manual(r1.2)Vista 1600 c epon olt quick start manual(r1.2)
Vista 1600 c epon olt quick start manual(r1.2)
Shanxi Cai
 
Troubleshooting linux-kernel-modules-and-device-drivers-1233050713693744-1
Troubleshooting linux-kernel-modules-and-device-drivers-1233050713693744-1Troubleshooting linux-kernel-modules-and-device-drivers-1233050713693744-1
Troubleshooting linux-kernel-modules-and-device-drivers-1233050713693744-1
Jagadisha Maiya
 

Similaire à Eco-Friendly Hardware Hacking with Android (20)

DEF CON 27 - XILING GONG PETER PI - exploiting qualcom wlan and modem over th...
DEF CON 27 - XILING GONG PETER PI - exploiting qualcom wlan and modem over th...DEF CON 27 - XILING GONG PETER PI - exploiting qualcom wlan and modem over th...
DEF CON 27 - XILING GONG PETER PI - exploiting qualcom wlan and modem over th...
 
[HES2013] Hacking apple accessories to pown iDevices – Wake up Neo! Your phon...
[HES2013] Hacking apple accessories to pown iDevices – Wake up Neo! Your phon...[HES2013] Hacking apple accessories to pown iDevices – Wake up Neo! Your phon...
[HES2013] Hacking apple accessories to pown iDevices – Wake up Neo! Your phon...
 
Itsp documentation quadcopter flight controller based on kalman filters
Itsp documentation   quadcopter flight controller based on kalman filtersItsp documentation   quadcopter flight controller based on kalman filters
Itsp documentation quadcopter flight controller based on kalman filters
 
Testing CAN network with help of CANToolz
Testing CAN network with help of CANToolzTesting CAN network with help of CANToolz
Testing CAN network with help of CANToolz
 
BURO Arduino Workshop
BURO Arduino WorkshopBURO Arduino Workshop
BURO Arduino Workshop
 
Ak12 upgrade
Ak12 upgradeAk12 upgrade
Ak12 upgrade
 
JCrete Embedded Java Workshop
JCrete Embedded Java WorkshopJCrete Embedded Java Workshop
JCrete Embedded Java Workshop
 
SSTIC 2019 - V2G injector: Whispering to cars and charging units through the ...
SSTIC 2019 - V2G injector: Whispering to cars and charging units through the ...SSTIC 2019 - V2G injector: Whispering to cars and charging units through the ...
SSTIC 2019 - V2G injector: Whispering to cars and charging units through the ...
 
EC/Bios Interaction Laptop Repair Course
EC/Bios Interaction Laptop Repair CourseEC/Bios Interaction Laptop Repair Course
EC/Bios Interaction Laptop Repair Course
 
Ccna lab manual 640 802
Ccna lab manual 640 802Ccna lab manual 640 802
Ccna lab manual 640 802
 
Full details of implementation of flying internet balloon
Full details of implementation of flying internet balloonFull details of implementation of flying internet balloon
Full details of implementation of flying internet balloon
 
Vista 1600 c epon olt quick start manual(r1.2)
Vista 1600 c epon olt quick start manual(r1.2)Vista 1600 c epon olt quick start manual(r1.2)
Vista 1600 c epon olt quick start manual(r1.2)
 
Such a weird Processor: messing with opcodes (...and a little bit of PE) (Has...
Such a weird Processor: messing with opcodes (...and a little bit of PE) (Has...Such a weird Processor: messing with opcodes (...and a little bit of PE) (Has...
Such a weird Processor: messing with opcodes (...and a little bit of PE) (Has...
 
IoT: Internet of Things with Python
IoT: Internet of Things with PythonIoT: Internet of Things with Python
IoT: Internet of Things with Python
 
Station 1 POD1
Station 1 POD1Station 1 POD1
Station 1 POD1
 
Troubleshooting Linux Kernel Modules And Device Drivers
Troubleshooting Linux Kernel Modules And Device DriversTroubleshooting Linux Kernel Modules And Device Drivers
Troubleshooting Linux Kernel Modules And Device Drivers
 
Troubleshooting linux-kernel-modules-and-device-drivers-1233050713693744-1
Troubleshooting linux-kernel-modules-and-device-drivers-1233050713693744-1Troubleshooting linux-kernel-modules-and-device-drivers-1233050713693744-1
Troubleshooting linux-kernel-modules-and-device-drivers-1233050713693744-1
 
Esp8266 wi fi_module_quick_start_guide_v_1.0.4
Esp8266 wi fi_module_quick_start_guide_v_1.0.4Esp8266 wi fi_module_quick_start_guide_v_1.0.4
Esp8266 wi fi_module_quick_start_guide_v_1.0.4
 
communicate with instrument by using lan
communicate with instrument by using lancommunicate with instrument by using lan
communicate with instrument by using lan
 
CCNA_200-301_June_2023-v1.2.pdf
CCNA_200-301_June_2023-v1.2.pdfCCNA_200-301_June_2023-v1.2.pdf
CCNA_200-301_June_2023-v1.2.pdf
 

Dernier

The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
shinachiaurasa2
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
masabamasaba
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
masabamasaba
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
VictoriaMetrics
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
masabamasaba
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
masabamasaba
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 

Dernier (20)

The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 

Eco-Friendly Hardware Hacking with Android

  • 1. Carlo Pescio @CarloPescio http://eptacom.net Carlo Pescio Eco-friendly hardware hacking with Android
  • 2. Carlo Pescio @CarloPescio http://eptacom.net Code in Movies
  • 3. Carlo Pescio @CarloPescio http://eptacom.net Super Heroes - Build this in a cave - With a bunch of scraps
  • 4. Carlo Pescio @CarloPescio http://eptacom.net Reality
  • 5. Carlo Pescio @CarloPescio http://eptacom.net It gets worse
  • 6. Carlo Pescio @CarloPescio http://eptacom.net Your ____mission Should you choose to accept it… - Your phone (no rooting) - Bunch of scraps - No Firmware 12V
  • 7. Carlo Pescio @CarloPescio http://eptacom.net Smart > Hero Don’t do industrial controls using scraps It’s OK at home Great learning opportunity
  • 8. Carlo Pescio @CarloPescio http://eptacom.net Just be careful, ok?
  • 9. Carlo Pescio @CarloPescio http://eptacom.net Step 1: blinking an LED Phone + Scraps
  • 10. Carlo Pescio @CarloPescio http://eptacom.net Choices, choices… Built-in led[s] Audio USB
  • 11. Carlo Pescio @CarloPescio http://eptacom.net LEDs in common USB scraps Flash drive Mouse Keyboard Printer
  • 12. Carlo Pescio @CarloPescio http://eptacom.net And the winner is… LEDs are host-controlled Standard protocol LEDs outlive the keys Easy to find
  • 13. Carlo Pescio @CarloPescio http://eptacom.net Phone – Keyboard connection Your phone is normally a device (gadget) for a computer (host). A keyboard is a gadget => your phone must turn into a host => you need an OTG adapter
  • 14. Carlo Pescio @CarloPescio http://eptacom.net OTG Adapter (make or buy) + = + wire 4-5
  • 15. Carlo Pescio @CarloPescio http://eptacom.net Device / Interface / Endpoint Device Descriptor Endpoint Descriptor 1 Endpoint Descriptor 2 … Configuration Descriptor 1 Configuration Descriptor 2 Interface 0 Descriptor Interface 1 Descriptor Interface 0 Descriptor Interface 1 Descriptor … … Kbd + trackball Solar powered Keyboard Interrupt IN USB powered Trackball
  • 16. Carlo Pescio @CarloPescio http://eptacom.net The software side Don’t write any code yet Interface #0 Class: Human Interaction Device (0x3) Endpoint: #0 Address : 129 (10000001) Number : 1 Direction : Inbound (0x80) Type : Intrrupt (0x3) Poll Interval : 10 Max Packet Size: 8 Attributes : 000000011 Interface #1 Class: Human Interaction Device (0x3) Endpoint: #0 Address : 130 (10000010) Number : 2 Direction : Inbound (0x80) Type : Intrrupt (0x3) Poll Interval : 10 Max Packet Size: 3 Attributes : 000000011
  • 17. Carlo Pescio @CarloPescio http://eptacom.net The missing endpoint Source: USB Device Class Definition for Human Interface Devices Firmware Specification, Section 4.4 If no Interrupt Out endpoint is declared then Output reports are transmitted to a device through the Control endpoint. Endpoint 0 is a Control pipe always present in USB devices. Therefore, only the Interrupt In pipe is described for the Interface descriptor using an Endpoint descriptor.
  • 18. Carlo Pescio @CarloPescio http://eptacom.net Know thy APIs You just can’t get hold of endpoint 0. But: UsbDeviceConnection. controlTransfer( gazillion parameters ) Performs a control transaction on endpoint zero for this device. The direction of the transfer is determined by the request type. If requestType & USB_ENDPOINT_DIR_MASK is USB_DIR_OUT, then the transfer is a write […]
  • 19. Carlo Pescio @CarloPescio http://eptacom.net 7.2.2 SET_REPORT Part Description bmRequestType 00100001 (0x21) bRequest SET_REPORT wValue Report Type and Report ID wIndex Interface wLength Report Length Data Report SET_REPORT = 0x09 (Paragraph 7.2)
  • 20. Carlo Pescio @CarloPescio http://eptacom.net For LEDs / Keyboards Report Type: 02 = Output (Paragraph 7.2.1) Report ID: 0 = Not Used (Paragraph 7.2.1) Interface = 0 (irrelevant here) Report Length = 1 (1 byte, Appendix B.1) Data: bitmask where bit 0 = NUM LOCK bit 1 = CAPS LOCK bit 2 = SCROLL LOCK
  • 21. Carlo Pescio @CarloPescio http://eptacom.net Sending the report int controlTransfer( int requestType, int request, int value, int index, byte[] buffer, int length, int timeout) byte[] buf = new byte[] { b }; connection.controlTransfer( 0x21, 0x09, 0x0200, 0x0000, buf, 1, 1000); // data [as array] -> bitmask // wValue -> 0x0200 // wIndex -> 0x0000 // wLength -> 1 // 1 sec ok -> 1000 // bmRequestType -> 0x21 // bRequest -> 0x09
  • 22. Sketch of the code usbManager = (UsbManager) getSystemService(Context.USB_SERVICE); // just get the first usb device – // should get the keyboard instead UsbDevice device = (UsbDevice) usbManager.getDeviceList().values(). toArray()[0]; usbManager.requestPermission( device, permissionIntent);
  • 23. Carlo Pescio @CarloPescio http://eptacom.net … build the Keyboard // … receive intent UsbDevice device = (UsbDevice) intent.getParcelableExtra( UsbManager.EXTRA_DEVICE); keyboard = new UsbKeyboard(usbManager, device);
  • 24. Keyboard public UsbKeyboard(UsbManager usbManager, UsbDevice device) { requestQueue = new LinkedBlockingQueue<Byte>(); connection = usbManager.openDevice(device); // WHY AM I DOING THIS? GUESS  ifc0 = device.getInterface(0); connection.claimInterface(ifc0, true); ioThread = new Thread(usbWriteLoop); ioThread.start(); }
  • 25. private Runnable usbWriteLoop = new Runnable() { // … while( !stop ) { Byte b = requestQueue.take(); if( b < 0 ) { stop = true; } else { byte[] buf = new byte[] { b }; connection.controlTransfer( 0x21, 0x09, 0x0200,0x0000, buf, 1, 1000); } } // … };
  • 26. UI public void onCheckedChanged( CompoundButton buttonView, boolean isChecked) { byte ledMask = 0; if( led1.isChecked() ) ledMask += 1; if( led2.isChecked() ) ledMask += 2; if( led3.isChecked() ) ledMask += 4; // just adds ledMask to the requestQueue keyboard.powerLedsFromMask(ledMask); }
  • 27. Carlo Pescio @CarloPescio http://eptacom.net We did it!
  • 28. Carlo Pescio @CarloPescio http://eptacom.net Detour: how fast can we go? Just use an infinite loop / no sleeping or waiting: byte[] buf0 = new byte[] { 0 }; byte[] buf1 = new byte[] { 4 }; while( true ) { connection.controlTransfer( 0x21, 0x09, 0x0200, 0x0000, buf0, 1, 1000); connection.controlTransfer( 0x21, 0x09, 0x0200, 0x0000, buf1, 1, 1000); }
  • 29. Carlo Pescio @CarloPescio http://eptacom.net Hollywood loves CRTs Fantastic Four Iron Man Star Trek Enterprise The Big Bang Theory
  • 30. Carlo Pescio @CarloPescio http://eptacom.net Me too!
  • 31. Carlo Pescio @CarloPescio http://eptacom.net Expected Well, almost No RT OS -> some (major) jitter expected Square wave
  • 32. Carlo Pescio @CarloPescio http://eptacom.net Surprise!! Captain America – the Winter Soldier
  • 33. Carlo Pescio @CarloPescio http://eptacom.net Kill the noise Not exactly square but clean  Guess the frequency!
  • 34. Carlo Pescio @CarloPescio http://eptacom.net About 1 KHz (on my device)
  • 35. Carlo Pescio @CarloPescio http://eptacom.net Conclusions from detour Useful: we killed the noise Things you cannot do: - “high” frequency / low latency / low jitter stuff - PWM - Infrared remotes - … etc. … Things you can do: - Turning on and off some stuff at low freq
  • 36. Carlo Pescio @CarloPescio http://eptacom.net No joy yet - Different voltage (LED powered from your phone USB) - High current (same as above) - Generally speaking, no galvanic isolation (whatever happens there, happens to your phone) You can’t just put a motor where the LED is 
  • 37. Carlo Pescio @CarloPescio http://eptacom.net Step 2: Galvanic Isolation
  • 38. Carlo Pescio @CarloPescio http://eptacom.net The ubiquitous optoisolator
  • 39. Carlo Pescio @CarloPescio http://eptacom.net A small step forward… But can’t power a motor with that
  • 40. Carlo Pescio @CarloPescio http://eptacom.net A level of indirection… … brings new problems 1) Who is providing the current going through the phototransistor? 2) The phototransistor can’t handle much current anyway Phone [usb] ???
  • 41. Carlo Pescio @CarloPescio http://eptacom.net Step 3: ATX is your best friend Will solve all your problems at once 
  • 42. Carlo Pescio @CarloPescio http://eptacom.net ATX starts in stand-by
  • 43. Carlo Pescio @CarloPescio http://eptacom.net So that’s it 12 v 12K SUPPLY POWER ATX PS_ONCOM 1K1K keyboard 1.2mA USB line power
  • 44. Carlo Pescio @CarloPescio http://eptacom.net Some assembly required
  • 45. Carlo Pescio @CarloPescio http://eptacom.net … and it works
  • 46. Carlo Pescio @CarloPescio http://eptacom.net What about inputs?? Well, it’s a keyboard! you get digital inputs for free (almost) ???
  • 47. Carlo Pescio @CarloPescio http://eptacom.net Relevant code connection = usbManager.openDevice(device); ifc0 = device.getInterface(0); connection.claimInterface(ifc0, true); endPointRead = ifc0.getEndpoint(0); while( true ) { final byte[] buffer = new byte[8]; int transfer = connection.bulkTransfer(endPointRead, buffer, 8, 1000); if( transfer > 0 ) { // [modifier,reserved,Key1,Key2,Key3,Key4,Key5,Key6] byte key = buffer[2]; // dumps Key1 only... if( key != 0 ) Log.e(“KEY", " " + key); } }
  • 48. Carlo Pescio @CarloPescio http://eptacom.net … and we’re live
  • 49. Carlo Pescio @CarloPescio http://eptacom.net Missing: galvanic isolation! 1K1KDI ROW / COLUMN combo
  • 50. Carlo Pescio @CarloPescio http://eptacom.net Charging while in host mode Usually the host (phone) provides power. OTG specification: 36.5 kΩ between pin 4 and 5 […] The OTG device is allowed to charge and enter host mode
  • 51. Carlo Pescio @CarloPescio http://eptacom.net May or may not work for you If it works, ATX might be even more of a friend! 5V here also in standby
  • 52. Carlo Pescio @CarloPescio http://eptacom.net If it doesn’t work… - Fiddle with resistor values - Remove battery and provide power from there - Also useful when battery is dead - That’s a story for another time 
  • 53. Carlo Pescio @CarloPescio http://eptacom.net Get in touch carlo.pescio@gmail.com @CarloPescio http://eptacom.net