Goavec-MerouG.
& al.
Objectives Prérequis Cocoa Bluetooth Conclusion
Developing for Apple iPod under GNU/Linux
Gwenhaël Goavec-Merou1,2 and Jean-Michel Friedt1
1 Association Projet Aurore
2ARMadeus Project
slides available at http://www.trabucayre.com
July 6 2010
1 / 19
Goavec-MerouG.
& al.
Objectives Prérequis Cocoa Bluetooth Conclusion
Objectives
• Becoming familiar with embedded devices around us, especially as development platform for educational purposes
• Developing on proprietary platforms using exclusively opensource tools
⇒selection of the iPod Touch for its widespread availability, its computational performances, and an operating system supporting a POSIX layer
⇒challenge of developing on this closed, proprietary platform (Apple) using free tools.
2 / 19
Goavec-MerouG.
& al.
Objectives Prérequis Cocoa Bluetooth Conclusion
Outline
1 free your iPod Touch
2 the cross-compilation toolchain and linking with proprietary libraries
3 POSIX example
4 COCOA example
5 application to the use of an opensource Bluetooth stack
6 practical demonstrations
3 / 19
Goavec-MerouG.
& al.
Objectives Prérequis Cocoa Bluetooth Conclusion
Free your iPod Touch
The default conguration is to only allow the execution of programs downloaded from iTunes (requiring a fee from developers).
• Opensource tool running under GNU/Linux: spirit1,
• requires a clean iPod under iOS≤3.1.3 (warning: current iTune update installs iOS 4),
• reinitializes the iPod (possible data loss) to provide a limitation-free iOS 3.1.3, mainly removing the requirement to only execute programs provided by iTunes,
• provides a Debian-like package manager (Cydia), installed during the upgrade.
Need to start usbmuxd with root rights.
→availability of a ssh server for connecting over the wi link, program transfer and execution either from a text-mode console or a graphical interface (Springboard).
1http://spiritjb.com
4 / 19
Goavec-MerouG.
& al.
Objectives Prérequis Cocoa Bluetooth Conclusion
Opensource cross-compilation toolchain
• iPod Touch is based on a Cortex A8 ARM processor, supported by gcc
• the project code.google.com/p/iphonedevonlinux provides an installation script for the cross-compilation toolchain, using the proprietary but free Apple libraries,
• requires a registration at developer.apple.com to download the XCode archive (includes the libraries)
• consider the script as a guide towards the toolchain generation, but might require some minor modication depending on your particular GNU/Linux installation and the latest XCode version.
• Beware of disk usage (>8.5 GB) !
5 / 19
Goavec-MerouG.
& al.
Objectives Prérequis Cocoa Bluetooth Conclusion
Toolchain validation: POSIX application
POSIX program example:
#i n c l u d e < s t d i o . h>
i n t main (v o i d) {
p r i n t f ("Ipod : Hello World\n") ; r e t u r n 0 ;
}
Compilation:
arm−a p p l e−d a r w i n 9−g c c−c−o h e l l o W o r l d . o h e l l o W o r l d . c arm−a p p l e−d a r w i n 9−g c c−bind_a t_lo ad−w−o h e l l o h e l l o W o r l d . c
Installation:
@scp−r p h e l l o r o o t @ $ ( IP_IPOD ) : / u s r / l o c a l / b i n
@ssh r o o t @ $ ( IP_IPOD ) "cd / usr / l o c a l / bin ; l d i d -S h e l l o ; k i l l a l l SpringBoard"
6 / 19
Goavec-MerouG.
& al.
Objectives Prérequis Cocoa Bluetooth Conclusion
Objective C
• Main language on NeXTSTEP OS,
• imposed by Apple to use the graphical interface
• Object oriented language using a C-like syntax
• supported by GCC: llvm-gcc compiled with objc support The header le (.h):
#i m p o r t <o b j c / o b j c . h>
#i m p o r t <o b j c / O b j e c t . h>
#i m p o r t " protoDelegate . h"
@ i n t e r f a c e my_class : O b j e c t {
i d <p r o t o D e l e g a t e > d e l e g a t e ; i n t nb ;
}
− (v o i d) s a y H e l l o ;
@ p r o p e r t y ( nonatomic , a s s i g n ) i n t nb ;
@end
Implementation le (.m):
#i m p o r t "my_class . h"
@ i m p l e m e n t a t i o n my_class : O b j e c t
@ s y n t h e s i z e nb ;
− (v o i d) s a y H e l l o {
p r i n t f (" Hello , %d ! \ n", nb ) ; }
@end
7 / 19
Goavec-MerouG.
& al.
Objectives Prérequis Cocoa Bluetooth Conclusion
Objective C
Object instanciation and method use:
#i m p o r t <o b j c / o b j c . h>
#i m p o r t <o b j c / O b j e c t . h>
#i m p o r t "my_class . h"
i n t main (v o i d) {
my_class ∗mc = [ [ my_class a l l o c ] i n i t ] ; [ mc setNb : 1 ] ;
[ mc s a y H e l l o ] ; mc . nb =2;
[ mc s a y H e l l o ] ; r e t u r n 0 ; }
Compilation:
l l v m−g c c−o o b j c _ e x e m p l e main .m my_class .m−l o b j c
Result (running on a GNU/Linux computer):
g w e @ l i n u x o bj c _ ex a mp l e $ . / o b j c _e x am p l e H e l l o , 1 !
H e l l o , 2 !
8 / 19
Goavec-MerouG.
& al.
Objectives Prérequis Cocoa Bluetooth Conclusion
Graphical (COCOA) example
@ i m p l e m e n t a t i o n H e l l o C o c o a
− (v o i d) a p p l i c a t i o n D i d F i n i s h L a u n c h i n g : ( U I A p p l i c a t i o n ∗) a p p l i c a t i o n {
window = [ [ [ UIWindow a l l o c ] i n i t W i t h F r a m e : [ [ U I S c r e e n m a i n S c r e e n ] bounds ] ]→
,→ a u t o r e l e a s e ] ;
CGRect windowRect = [ [ U I S c r e e n m a i n S c r e e n ] a p p l i c a t i o n F r a m e ] ; windowRect . o r i g i n . x = windowRect . o r i g i n . y = 0 . 0 f ;
window = [ [ UIWindow a l l o c ] i n i t W i t h F r a m e : [ [ U I S c r e e n m a i n S c r e e n ] bounds ] ] ; window . b a c k g r o u n d C o l o r = [ U I C o l o r w h i t e C o l o r ] ;
CGRect t x t F r a m e = CGRectMake ( 5 0 , 1 5 0 , 1 5 0 , 1 5 0 ) ;
UITextView ∗t x t V i e w = [ [ UITextView a l l o c ] i n i t W i t h F r a m e : t x t F r a m e ] ; t x t V i e w . t e x t = @" Hello Cocoa";
UIFont ∗f o n t = [ UIFont b o l d S y s t e m F o n t O f S i z e : 1 8 . 0 ] ; t x t V i e w . f o n t = f o n t ;
[ window addSubview : t x t V i e w ] ; [ t x t V i e w r e l e a s e ] ;
[ window m a k e K e y A n d V i s i b l e ] ; }
@end
9 / 19
Goavec-MerouG.
& al.
Objectives Prérequis Cocoa Bluetooth Conclusion
Bluetooth communication stack
• The default API is only compatible with Apple Compliant peripherals
• Alternate opensource implementation: BTstack, available at http://code.google.com/p/btstack2.
BTstack is compliant with Cocoa and POSIX applications.
Our contribution:
• extending this library to handle at best a RFCOMM (virtual serial port) communication link
• compilation of BTstack using the opensource cross-compilation toolchain
2owner: Matthias Ringwald
10 / 19
Goavec-MerouG.
& al.
Objectives Prérequis Cocoa Bluetooth Conclusion
BTstack structure
loop event Socket
rfcomm l2cap hci
Bluetooth Module
Cocoa
App1
POSIX
App2 App3
Cocoa
BTDaemon
bluetooth stack
Structure:
• Use the low-level stack, provided by Apple, in charge of fetching the packets at the hardware level
• BTstack provides a daemon, BTDaemon, linking with both low-level stack and applications
• Bluetooth stack handling is performed through the
BTStackManager library (for Cocoa) or by direct implementation (for POSIX)
11 / 19
Goavec-MerouG.
& al.
Objectives Prérequis Cocoa Bluetooth Conclusion
First example: RFCOMM link with a PC
On the iPod Touch side:
• bind the Bluetooth socket: rfcomm MAC_EEEPC
• cat</tmp/rfcomm0
• or launch a dedicated application (in this example, graphical display of transmitted values)
• On the PC side: an USB-Bluetooth adapter
• No authentication
• on the PC:
rfcomm listen 4
puis
echo "val:0.15" > /dev/rfcomm4
12 / 19
Goavec-MerouG.
& al.
Objectives Prérequis Cocoa Bluetooth Conclusion
Details concerning the use of Bluetooth
Bluetooth link initialization:
b t = [ BTstackManager s h a r e d I n s t a n c e ] ; [ b t s e t D e l e g a t e :s e l f] ;
[ b t a d d L i s t e n e r :s e l f] ; [ b t a c t i v a t e ] ;
Sentence reception:
−(v o i d) r f c o m m D a t a R e c e i v e d F o r C o n n e c t i o n I D : ( u i n t 1 6 _ t ) c o n n e c t i o n I D w i t h D a t a : (→
,→u i n t 8 _ t ∗) p a c k e t o f L e n : ( u i n t 1 6 _ t ) s i z e { memcpy ( tmp+nb , p a c k e t , s i z e ) ;
nb+=s i z e ;
i f ( p a c k e t [ s i z e−1] == ' \0 ') {
N S S t r i n g ∗s t r 2 = [ [ N S S t r i n g a l l o c ] i n i t W i t h C S t r i n g : (c h a r∗) tmp+4 e n c o d i n g : → ,→1 ] ;
CGFloat v a l = [ s t r 2 d o u b l e V a l u e ] ; [ a f f S i n a d d P o i n t : v a l ] ;
nb = 0 ; } }
13 / 19
Goavec-MerouG.
& al.
Objectives Prérequis Cocoa Bluetooth Conclusion
Second example: RFCOMM link with a Free2Move adapter
• Any embedded system
providing an asynchronous link (RS232) can communicate through a Bluetooth link (microcontroler)
• In this example, the iPod provides data display and storage.
RS232−BT Free2Move converter
USB−
RS232 curve of the transmitted values transmitted value
USB power supply: 5V
14 / 19
Goavec-MerouG.
& al.
Objectives Prérequis Cocoa Bluetooth Conclusion
Third example: controling a LEGO NXT brick
• Authentication issue: the NXT brick requires the pin 1234 bluetooth-agent "1234" under GNU/Linux
• Simple and well documented protocol3 4
Addition of commands to the BTstackManager on the iPod Touch:
1 creation of the l2cap channel:
bt_send_cmd(&hci_write_authentication_enable, 1);
2 when receiving request for Pin Code:
bt_send_cmd(&hci_pin_code_request_reply, &event_addr, 4,"1234");
3LEGO Mindstorms NXT Direct Commands v1.00 (2006), available at http://www.microframeworkprojects.com/images/d/df/LEGO_
MINDSTORMS_NXT_Direct_commands.pdf
4LEGO Mindstorms NXT Ultrasonic Sensor I2C Communication Protocol v1.00 (2006), available at http://www3.wooster.edu/physics/jacobs/220/Appendix_7_
Ultrasonic_Sensor_I2C_communication_protocol.pdf, or for a more readable version in C: http://stackoverow.com/questions/1967978/
lego-mindstorm-nxt-cocoa-and-hitechnic-sensors
15 / 19
Goavec-MerouG.
& al.
Objectives Prérequis Cocoa Bluetooth Conclusion
Accessing the iPod accelerometers
Two step approach:
1 Initialisation:
/∗ I n i t i a l i s a t i o n ∗/
U I A c c e l e r o m e t e r ∗a c c e l = [ U I A c c e l e r o m e t e r s h a r e d A c c e l e r o m e t e r ] ; /∗ f o r c a l l b a c k f u n c t i o n ∗/
a c c e l . d e l e g a t e = s e l f; /∗ t i m e r f o r e v e n t ∗/
a c c e l . u p d a t e I n t e r v a l = 0 . 0 3 f ;
2 Information retrieval:
− (v o i d) a c c e l e r o m e t e r : ( U I A c c e l e r o m e t e r ∗) a c e l d i d A c c e l e r a t e : (→
,→U I A c c e l e r a t i o n
∗) a c e l e r {
vx = a c e l e r . x ; vy = a c e l e r . y ; v z = a c e l e r . z ; [ . . . ]
}
withacceler.{x,y,z} oating type values providing tilt angle informations, in radians
16 / 19
Goavec-MerouG.
& al.
Objectives Prérequis Cocoa Bluetooth Conclusion
Sending commands from the iPod to NXT
Sentences to be sent:
c h a r msg_fwd_v [30]={0 x0d , 0 x00 , 0 x80 , 0 x04 , 0 x00 , 0 x00 , 1 + 4 , 1 , 0 x00 , 0 x20 , 0 , 0 , 0 , 0 , 0 , 0 x0d , 0 x00 , 0 x80 , 0 x04 , 0 x02 , 0 x00 , 1 + 4 , 1 , 0 x00 , 0 x20 , 0 , 0 , 0 , 0 , 0 } ;
Controling the motors from a PC:
msg_fwd_v [5]= d r o i t e ; msg_fwd_v [20]= gauche ;
// n x t _ s e n d r e c v ( msg_fwd_v , s i z e o f ( msg_fwd_v ) ) ;
s t a t u s f = w r i t e ( bt_socket , msg_fwd_v , s i z e o f( msg_fwd_v ) ) ;
Controling the motors from the iPod:
− (v o i d) a c c e l e r o m e t e r : ( U I A c c e l e r o m e t e r ∗) a c e l d i d A c c e l e r a t e : ( U I A c c e l e r a t i o n → ,→ ∗) a c e l e r
{
c h a r vx = (c h a r) ( a c e l e r . x∗100) ; c h a r vy = (c h a r) ( a c e l e r . y∗100) ; msg_fwd_v [ 5 ] = msg_fwd_v [ 2 0 ] = vx ;
i f ( vy <−0) msg_fwd_v [ 5 ] = vx+vy ; e l s e i f ( vy > 0 ) msg_fwd_v [ 2 0 ] = vx−vy ; [ b t sendRFCOMMPacketForChannelID : 1
p a c k e t : ( u i n t 8 _ t ∗) msg_fwd_v l e n :s i z e o f( msg_fwd_v ) ] ; }
17 / 19
Goavec-MerouG.
& al.
Objectives Prérequis Cocoa Bluetooth Conclusion
Demonstration
Control of the LEGO NXT brick from the iPod accelerometer through a Bluetooth wireless link
18 / 19
Goavec-MerouG.
& al.
Objectives Prérequis Cocoa Bluetooth Conclusion
Conclusion and perspectives
• we have demonstrated the ability to develop on the iPod Touch using opensource tools
• demonstration of applications running in text mode (POSIX) and using the graphical interface (COCOA),
• ssh over wi and Bluetooth wireless communication links,
• representative of some of the aspects of embedded system development (cross-compilation, hardware data access through an operating system)
but, unless the platform is already available, is the eort worth the time spent with respect to developing under Android ?
19 / 19