Embedded, Kernel, Linux

BPF Internals – I

Recent post by Brendan Gregg inspired me to write my own blog post about my findings of how Berkeley Packet Filter (BPF) evolved, it’s interesting history and the immense powers it holds – the way Brendan calls it ‘brutal’. I came across this while studying interpreters and small process virtual machines like the proposed KTap’s VM. I was looking at some known papers on register vs stack basd VMs, their performances and various code dispatch mechanisms used in these small VMs. The review of state-of-the-art soon moved to native code compilation and a discussion on LWN caught my eye. The benefits of JIT were too good to be overlooked, and BPF’s application in things like filtering, tracing and seccomp (used in Chrome as well) made me interested. I knew that the kernel devs were on to something here. This is when I started digging through the BPF background.

Background

Network packet analysis requires an interesting bunch of tech. Right from the time a packet reaches the embedded controller on the network hardware in your PC (hardware/data link layer) to the point they do someting useful in your system, such as display something in your browser (application layer). For connected systems evolving these days, the amount of data transfer is huge, and the support infrastructure for the network analysis needed a way to filter out things pretty fast. The initial concept of packet filtering developed keeping in mind such needs and there were many stategies discussed with every filter such as CMU/Stanford packet Filter (CSPF), Sun’s NIT filter and so on. For example, some earlier filtering approaches used a tree based model (in CSPF) to represenf filters and filter them out using predicate-tree walking. This earlier approach was also inherited in the Linux kernel’s old filter in the net subsystem.

Consider an engineer’s need to have a probably simple and unrealistic filter on the network packets with the predicates P1, P2, P3 and P4 :

equation

Filtering approach like the one of CSPF would have represented this filter in a expression tree structure as follows:

tree

It is then trivial to walk the tree evaluating each expression and performing operations on each of them. But this would mean there can be extra costs assiciated with evaluating the predicates which may not necessarily have to be evaluated. For example, what if the packet is neither an ARP packet nor an IP packet? Having the knowledge that P1 and P2 predicates are untrue, we may need not have to evaluate other 2 predicates and perform 2 other boolean operation on them to determine the outcome.

In 1992-93, McCanne et al. proposed a BSD Packet Filter with a new CFG-bytecode based filter design. This was an in-kernel approach where a tiny interpreter would evaluate expressions represented as BPF bytecodes. Instead of simple expression trees, they proposed a CFG based filter design. One of the control flow graph representation of the same filter above can be:

cfg

The evaluation can start from P1 and the right edge is for FALSE and left is for TRUE with each predicate being evaluated in this fashion until the evaluation reaches the final result of TRUE or FALSE. The inherent property of ‘remembering’ in the CFG, i.e, if P1 and P2 are false, the path reaches a final FALSE is remembered and P3 and P4 need not be evaluated. This was then easy to represent in bytecode form where a minimal BPF VM can be designed to evaluate these predicates with jumps to TRUE or FALSE targets.

The BPF Machine

A pseudo-instruction representation of the same filter described above for earlier versions of BPF in Linux kernel can be shown as,

l0:    ldh [12]
l1: jeq #0x800, l3, l2
l2:     jeq #0x805, l3, l8
l3: ld [26]
l4: jeq #SRC, l4, l8
l5:     ld len
l6:     jlt 0x400, l7, l8
l7: ret #0xffff
l8: ret #0

To know how to read these BPF instructions, look at the filter documentation in Kernel source and see what each line does. Each of these instructions are actually just bytecodes which the BPF machine interprets. Like all real machines, this requires a definition of how the VM internals would look like. In the Linux kernel’s version of the BPF based in-kernel filtering technique they adopted, there were initially just 2 important registers, A and X with another 16 register ‘scratch space’ M[0-15]. The Instruction format and some sample instructions for this earlier version of BPF are shown below:

/* Instruction format: { OP, JT, JF, K }
 * OP: opcode, 16 bit
 * JT: Jump target for TRUE
 * JF: Jump target for FALSE
 * K: 32 bit constant
 */

/* Sample instructions*/
{ 0x28,  0,  0, 0x0000000c },     /* 0x28 is opcode for ldh */
{ 0x15,  1,  0, 0x00000800 },     /* jump next to next instr if A = 0x800 */
{ 0x15,  0,  5, 0x00000805 },     /* jump to FALSE (offset 5) if A != 0x805 */
..

There were some radical changes done to the BPF infrastructure recently – extensions to its instruction set, registers, addition of things like BPF-maps etc. We shall discuss what those changes in detail, probably in the next post in this series. For now we’ll just see the good ol’ way of how BPF worked.

Interpreter

Each of the instructions seen above are represented as arrays of these 4 values and each program is an array of such instructions. The BPF interpreter sees each opcode and performs the operations on the registers or data accordingly after it goes through a verifier for a sanity check to make sure the filter code is secure and would not cause harm. The program which consists of these instructions, then passes through a dispatch routine. As an example, here is a small snippet from the BPF instruction dispatch for the instruction ‘add’ before it was restructured in Linux kernel v3.15 onwards,

127         u32 A = 0;                      /* Accumulator */
128         u32 X = 0;                      /* Index Register */
129         u32 mem[BPF_MEMWORDS];          /* Scratch Memory Store */
130         u32 tmp;
131         int k;
132
133         /*
134          * Process array of filter instructions.
135          */
136         for (;; fentry++) {
137 #if defined(CONFIG_X86_32)
138 #define K (fentry->k)
139 #else
140                 const u32 K = fentry->k;
141 #endif
142 
143                 switch (fentry->code) {
144                 case BPF_S_ALU_ADD_X:
145                         A += X;
146                         continue;
147                 case BPF_S_ALU_ADD_K:
148                         A += K;
149                         continue;
150 ..

Above snippet is taken from net/core/filter.c in Linux kernel v3.14. Here, fentry is the socket_filter structure and the filter is applied to the sk_buff data element. The dispatch loop (136), runs till all the instructions are exhaused. The dispatch is basically a huge switch-case dispatch with each opcode being tested (143) and necessary action being taken. For example, here an ‘add’ operation on registers would add A+X and store it in A. Yes, this is simple isn’t it? Let us take it a level above.

JIT Compilation

This is nothing new. JIT compilation of bytecodes has been there for a long time. I think it is one of those eventual steps taken once an interpreted language decides to look for optimizing bytecode execution speed. Interpreter dispatches can be a bit costly once the size of the filter/code and the execution time increases. With high frequency packet filtering, we need to save as much time as possible and a good way is to convert the bytecode to native machine code by Just-In-Time compiling it and then executing the native code from the code cache. For BPF, JIT was discussed first in the BPF+ research paper by Begel etc al. in 1999. Along with other optimizations (redundant predicate elimination, peephole optimizations etc,) a JIT assembler for BPF bytecodes was also discussed. They showed improvements from 3.5x to 9x in certain cases. I quickly started seeing if the Linux kernel had done something similar. And behold, here is how the JIT looks like for the ‘add’ instruction we discussed before (Linux kernel v3.14),

288                switch (filter[i].code) {
289                case BPF_S_ALU_ADD_X: /* A += X; */
290                        seen |= SEEN_XREG;
291                        EMIT2(0x01, 0xd8);              /* add %ebx,%eax */
292                        break;
293                case BPF_S_ALU_ADD_K: /* A += K; */
294                        if (!K)
295                                break;
296                        if (is_imm8(K))
297                                EMIT3(0x83, 0xc0, K);   /* add imm8,%eax */
298                        else
299                                EMIT1_off32(0x05, K);   /* add imm32,%eax */
300                        break;

As seen above in arch/x86/net/bpf_jit_comp.c for v3.14, instead of performing operations during the code dispatch directly, the JIT compiler emits the native code to a memory area and keeps it ready for execution.The JITed filter image is built like a function call, so we add some prologue and epilogue to it as well,

/* JIT image prologue */
221                EMIT4(0x55, 0x48, 0x89, 0xe5); /* push %rbp; mov %rsp,%rbp */
222                EMIT4(0x48, 0x83, 0xec, 96);    /* subq  $96,%rsp       */

There are rules to BPF (such as no-loop etc.) which the verifier checks before the image is built as we are now in dangerous waters of executing external machine code inside the linux kernel. In those days, all this would have been done by bpf_jit_compile which upon completion would point the filter function to the filter image,

774                 fp->bpf_func = (void *)image;

Smooooooth… Upon execution of the filter function, instead of interpreting, the filter will now start executing the native code. Even though things have changed a bit recently, this had been indeed a fun way to learn how interpreters and JIT compilers work in general and the kind of optimizations that can be done. In the next part of this post series, I will look into what changes have been done recently, the restructuring and extension efforts to BPF and its evolution to eBPF along with BPF maps and the very recent and ongoing efforts in hist-triggers. I will discuss about my experiemntal userspace eBPF library and it’s use for LTTng’s UST event filtering and its comparison to LTTng’s bytecode interpreter. Brendan’s blog-post is highly recommended and so are the links to ‘More Reading’ in that post.

Thanks to Alexei Starovoitov, Eric Dumazet and all the other kernel contributors to BPF that I may have missed. They are doing awesome work and are the direct source for my learnings as well. It seems, looking at versatility of eBPF, it’s adoption in newer tools like shark, and with Brendan’s views and first experiemnts, this may indeed be the next big thing in tracing.

Advertisement
Standard
Embedded, Kernel, Linux

Jumping the Kernel-Userspace Boundary – Procfs and Ioctl

I recently had a need to have a very fast and scalable way to share moderate chunks of data between my experimental kernel module and the userspace application. Of course, there are many ways already available. Some of them are documented very nicely here. I will be writing in a few blog posts sharing what all mechanisms I have used to transfer data and provide such interfaces.

Procfs

I have used the Procfs before (with the seq_file API) when I needed to read my experimental results back in userspace and perform aggregation and further analysis there only. It usually consisted of a stream of data which I sent to my /proc/foo file. From a userspace perspective, it is essentially a trivial read-only operation in my case,

/* init stuff */
static struct proc_dir_entry *proc_entry;
/* Create procfs entry in module init */
proc_entry = proc_create("foo", 0, NULL, &foo_fops);
/* The operations*/
static const struct file_operations foo_fops = {
    .owner = THIS_MODULE,
    .open = foo_open,
    .read = seq_read,
    .llseek = seq_lseek,
    .release = single_release,
};
/ *Use seq_printf to provide access to some value from module */
static int foo_print(struct seq_file *m, void *v) {
    seq_printf(m, val);
    return 0;
}

static int foo_open(struct inode *inode, struct  file *file) {
    return single_open(file, foo_print, NULL);
}
/* Remove procfs entry in module exit */
remove_proc_entry("foo", NULL);

Ioctl

I also used ioctl before (More importantly, I call them eye-awk-till. *grins*). They are used in situations when the interaction between your userspace applicatoin and the module resembles actual commands on which action from the kernel has to be performed. With each command, the userspace can send a message containing some data which the module can use to take actions. As an example, consider a device driver for a device which measures temperature from 2 sensors in a cold room. The driver can provide certain commands which are executed when the userspace makes ioctls. Each commad is associated with a number called as ioctl number which the device developer chooses. In a smiliar fashion to Procfs interface, file_operations struct can be defined with a new entry and initializations are done in the module,

/* File operations */
static const struct file_operations temp_fops = {  
       .owner = THIS_MODULE,  
       .unlocked_ioctl = temp_ioctl, 
};
/* The ioctl */
int temp_ioctl(struct file *filep, unsigned int cmd, unsigned long arg) {
	switch(cmd) {
	case READ_TEMP_SENSOR_1:	
		copy_to_user((char *)arg, temp_buff, 8);
		break;	
	case READ_TEMP_SENSOR_2:
		copy_to_user((char *)arg, temp_buff, 8);
		break;
        }
}

There are other complexities involved as well, such as using _IO(), _IOR() macros to define safe ioctl numbers. To know more about ioctl() call and how it is used, I suggest you read Chapter 7 from LKMPG. Note that newer kernels have some minor changes in code, hence refer to some device drivers using ioctls inlatest kernel releases. Each ioctl in our case means we have to use copy data from user to kernel or from kernel to user using copy_fom_user() or copy_to_user() functions. There is also no way to avoid the context switch. For small readings done ocassionally, this is an OK mechanism I would say. Consider that in a parallel universe, this sensor system aggregates temperature as well as a high quality thermal image in addition to each measurement. Also, there are thousands of such sensors spread across a lego factory and are being read each second from a common terminal. For such huge chunks of data accessed very frequently this each additional copy is a performance penalty. For such scenarios, I used the mmap() functionalty provided to share a part of memory between the kernel and userspace. I shall discuss more about Mmap in my next post.

Standard
Android, Embedded, Linux, UX/UI

Some Cool Retro Watchfaces

These holidays, I was refraining from spending huge sums on stocking gadgets I don’t need. But (un)fortunately, I ended up buying a Nexus 5, a Moga Hero controller and a cheap smartwatch. So what do you do when you get an Android device? Exactly! Start hacking on it – root, custom ROMs, custom kernel, cool apps! So, my experimentation started with the SmartQ Z Smartwatch I had bought. For the price (CAD $91, including shipping from China) this was an irresistible piece of tiny Android to start tinkering around with. It packs a 1GHz Ingenic processor (MIPS) – JZ4775, 512MB DDR RAM and 4GB flash. All of this with a tiny 1.54 inch screen with a 240×240 resolution. The latest firmware supplied by the vendor is based on Android 4.4 with a custom launcher. It is a very hacker-friendly device and came with ‘su’ out of the box 🙂 Though its a bit old (2013 launch) and the manufacturer seems to have abandoned the development on this, for the price, its a pretty impressive piece of tech on your hand.

I decided to make my own watchfaces with custom features and more inspiring UI than the watchfaces provided in the watch. Also, the hidden agenda was to check out the new Android Studio. As with everything Chinese, the SDK and docs for the Smartwatch were in Chinese! But fortunately, after surfing through XDA Developers forums, I found a link to the English docs on their website to refer to the weather APIs that I needed to use. With moderate efforts, I was able to make the following watch faces :

They are actually developed as widgets and the launcher apparently sees if the AppWidgetProvicer class you use starts with the string “WatchFace”. If this is the case, it simply puts that widget along with the custom watch faces in the menu.

If you own a similar watch/with similar screen dimensions, you can try installing the apps or use the source code for building your own cool watchfaces. I really wish I had an Android Wear watch rather than this so that I could develop on a more useful and up-to-date platform. Thanks for reading. Happy new year! Here is the source code and specs for these watch faces :

LCD Watch Face

Source : https://github.com/tuxology/LCDWatchFace

Download : lcdwatchface.apk

CRT Watch Face

Source : https://github.com/tuxology/CRTWatchFace

Download : crtwatchface.apk

If you find any bugs, report them on Github. Also, if you want me to port them on Android Wear, let me know. I’ll try to do that in emulator.

Standard
Embedded, Linux, Raspberry Pi

Raspberry Pi with HDMI – VGA converter

I have an early 2012 version of Raspberry Pi (generous gift from the Fedora Project) which sometimes tends to give some problem when a lot of current is being sourced from the USB. In addition to current issues with USB as well as HDMI, I faced a basic display problem when I used my HDMI-VGA converter for display which is not powered externally.

First things first, the converter won’t work directly and its almost sure that you will have to make changes to the config.txt (which holds the boot time specifications and is the key file to tell the Pi during boot time about any configurations that it should take care of) Here is what my config.txt looks like

arm_freq=800
force_turbo=1
gpu_mem=128
disable_overscan=1
start_file=start_x.elf
fixup_file=fixup_x.dat
hdmi_force_hotplug=1
hdmi_group=2
hdmi_mode=47
hdmi_drive=2

The values will obviously be different for you according to your Pi and display configuration. Have a look at http://elinux.org/RPiconfig for a very detailed explaination. Some things of interest to us here are hdmi_force_hotplug=1 just pretends that a HDMI device is always attached. The hdmi_group specifies the HDMI type whether its a DMT type output (mostly used in computer monitors) or if its CEA (which is used for TV monitors). The value 2 is for DMT.  The next is hdmi_mode which is very monitor specific. For example 47 corresponds to a monitor supporting a resolution of 1440X900 at 60Hz. You can check the wiki and find the mode for your monitor.  The hdmi_drive just chooses between HDMI and DVI output modes. You can save this as config.txt in your boot partition and ho! the converter works.

But hold on! As expected, it  the other devices connected also draw some amount of current from the USB ports which because of the infamous polyfuses between the USB supply line and ports limit the current to around 140mA. This is annoying as I don’t even know the specifications of my converter (its a cheap one from Amazon which did not come with absolutely any documentation.) The only way is to open it and measure the current draw myself. Being too lazy for that, I found a lot of solutions on the web to get my power supply of pi straightened out and amazingly the simplest one worked for me (/me says ‘he he he!’ with an evil grin) Here’s what you can try (in order) –

  • Try a better power supply – Use the ones which have ratings for 1A and above The usually are faithful. Some phone chargers will work and mostly the USB supply from laptops is not sufficient. I use my Samsung phone’s charger which has a rating of 5V, 850mA It works until I use more devices like Wi-Fi/Bluetooth dongle etc.
  • Buy the externally powered USB hub – If the simple thing above doesn’t work (usually when you are connecting more than two heavy devices – like a non-powered HDMI-VGA converter and a HDD) you need to find an externally powered USB hub. This is the safest bet (recommended) and usually not exciting if you are of the hacker kind. There is a list of good ones on http://elinux.org/RPi_VerifiedPeripherals#Powered_USB_Hubs
  • But this is pretty overkill for daily mundane tasks like a Wi-Fi dongle + some other USB peripheral. So the more interesting way is to short the polyfuses. I have heard that they no longer exist on the Model B rev 2.0 which seems good, but the main power still has polyfuses (shorting them is a bad idea I think) So for older models, you can get most of peripherals working when you short the polyfuses.
  • If you are more adventurous kind, you can get your Pi powered from an external ATX power supply which you can scavenge from some old PC (as I did) I am pretty much planning to power everything including the pi and a small fan from a Pentium 2 processor (slot package) from my ATX supply and I hope it will work.

I managed to get mpd running on my Pi and have setup my ownCloud on it so that I can use it as a small storage cloud. The mpd combined with my Droid MPD client on an Android phone has essentially made it my remote music station. I am waiting for my relay board to come so that I can maybe tweet-a-lightbulb at home sometime 🙂 I shall keep posting updated about small hacks and things I learned with the Pi.

Here’s an old post which I found interesting on polyfuses on Pi and from where I learnt some stuff about it – http://theiopage.blogspot.ca/2012/06/increasing-raspberry-pis-usb-host.html

Standard
Embedded, Linux, Qt

Qt Apps on Android! Part Two : An App(le) a day

No guys, this post is not related to Apple Inc or Steve Jobs but to my previous post 🙂 We now are in a position to have our development setup ready for Qt app development on Android so lets begin with the actual stuff. I shall take an example of the digital clock app you had seen in the previous post (reproduced here for your sake).

For some Qt newbies, its also going to be a tutorial on using Qt Creator effectively. We shall cover UI design and then do some coloring and stuff like that to make it more beautiful. Then we shall code the app so that your clock works.

Requirements

For your reference, I have put up this simple app on my git repo or maybe you can get the tarball from here

Step 1

Start the Necessitas Qt Creator and create a new Qt Gui Application from File > New File or Project > Qt Widget Project > Qt GUI Application

Step 2

Choose the project name and location and after that choose the Qt version as  Qt for Android which we created in the last post.

You can also select the Desktop version to for prototyping your app for the desktop x86 host. Once the project is created you can see the auto generated files under Project as shown below. The file tuxologycloxk.cpp is the one in which all the logic goes.

Step 3

Under Forms, click the .ui file and start making the UI. Its a pretty easy job actually, you have to drag and drop the required widgets and arrange them properly in something called as layouts. Just analyse a bit how I have created the UI for the clock.

You can drag and drop the Widgets from the left panel to the form view and the corresponding Objects will be created in the right top panel as shown above. The property for each project can be set directly from here only. For eg. the initial value (initValue) for the lcdNumber object has been set as 1200 above. You can actually set the widgets background as well as the whole application colour palette by changing properties of the respective objects.

Notes on StyleSheets

You can also apply styleSheets to make your app a bit beautiful too. For example the Exit text that you see in the application is actually a button with some styles applied. You can set styles using the UI editor quite easily. Just right click the corresponding widget and click on Change styleSheet. You will get window as shown below in which you can apply your desired style.

The above stylesheet changes a button from the boring button widget to a sleek black button which mixes well with the application’s look and feel.

Step 4

Look closely and you will understand that writing code is no big deal too. Just refer my digital clock source and browse through the code to understand it. Just a small reminder on creating signals and slots – You can click on the widget directly to create slots for the specific signals they will emit. For eg. right click Exit and select Go to slot.. A dialog box will ask you the signal which will be emitted and when you hit Ok a slot in the code will  be automatically generated. Now you can write whatever code you want to implement in that slot.

Step 5

Assuming that you have created the application, you can do some other settings too. Just click on Projects on the left panel and you will see different targets for you application. We had opted for Android as well as Desktop in the beginning so both shall be shown here. Click on Run and then Details under Package Configuration. You will see some configuration tabs as shown below. You can fine tune some stuff from here of course such as Android Permissions, app name, app icon etc.

You also have an option to either deploy local Qt libs for the device or use device’s libs. If you have installed Ministro from Android market to your device, just leave it to use the devices qt libs. However, if you are going to use the emulator, make sure you get the Ministro apk from here and install it on the emulator by selecting the third option below.

Once all is set its time to connect your device, set platform as Android and hit Run (Ctrl+R) You can see the compile output on the compile output window and the debug messages in Application Output window (hit Alt+3 or Alt+4 to switch) Watch out for any build issues too. I hope the same stuff works fine with an AVD too as I haven’t tried that out actually. I do all my testing on my rooted Sony Xperia mini x10 Pro and the first image in this post is what you should get if you try to build the TuxologyClock project for your device.

Thats all Folks! Happy hacking!

Standard
Embedded, Linux

Qt Apps on Android! Part One : <3 is in the Air :)

Have you loved two tangentially apart technologies at the same time? Its like holding one girl’s hand while you woo another one 😉 Yeah something like that is the case with me. To my girl – “Its OK sweety, I’m just talking about Qt and Android :)”

There must have been a time when you would have thought, “Oh God, I wish I could just port all these apps I run on my desktop to my new android phone.” Or maybe you are one of hose who say, “I wish I could use my Android cell to prototype my new Qt based embedded device that I am making. It’d be something cool to show to those black shoes, red tie morons in the conference room.”

The Necessitas project comes to your aid guys. I shall be writing a short tutorial series on creating small Qt app like these :

in the speediest of ways and port it to your device. This part will consist of setting up the tools necessary for Qt application development on Android

Necessitas

Also known as Android Lighthouse project, this is the individually developed port of Qt for Android. Necessitas comes with a modified Qt Creator IDE for building, deploying and even debugging your applications directly for your Android device. You will be amazed to see the ease with which you can develop and debug your apps. Say thanks to BogDan Vatra and those unsung heroes who have brought this to you. Now lets begin.

Get Necessitas SDK

Get the Necessitas 0.3 online installer from here. I however downloaded the 0.1.1 version available as an offline install which serves the purpose well. Its available in old versions directory. The installation is pretty straight forward. Just run the installer and make sure that you install the SDK in /opt/necessitas. You may have to make your /opt 777 for sometime and then revert back to 755 once the installation is over.  The SDK mainly consists of the cross compiler for android on ARM and lots of cross compiled ARM libs for Qt. I have mentioned in previous posts how to do all that manually but here, its all ready for you 🙂 Once the installation is over, you will get a Necesitas Qt Creator in your applications. This is almost same as your traditional Qt Creator IDE. We shall move on to configure it now.

Configure Qt Creator

Requirements :

  • Install ant if required by yum install ant
  • Check whether you have JDK with java -version
  • Get Android SDK from here
  • Get Android NDK from here

Step 1

Extract the SDK and NDK at some locations and start Necessitas Qt Creator/Qt Creator for Android and go to Tools>Options. Click Qt4 tab and Add a new qmake path. Give this new qmake path from /opt/necessitas/Android/<qtversion>/bin/qmake This qmake will make the projects and makefiles cross-compile ready. Give some name to it – maybe Qt for Android

Step 2

Now that you have the new Qt setup, Click the Android tab on the left and specify the SDK and NDK target and set proper toolchain as shown below. Also set the ant location and hit Apply

If you are not having any Android device, then create a AVD to test your app. Lastly, some configuration is also required on your device.

Step 3

Now, we have almost everything ready for development on our device, however to run a Qt app we need libraries on the target device. For this, there are two options. Either while developing application, an option to use local Qt libs can be selected or a nifty tool called Ministro can be used. Ministro is an android application that can be downloaded from the Android market. This application performs a one time download of Qt libs from the net on the device as required by the application you have created. In a simple application mostly it will do a mostly 8Mb install of QtCore and QtGui modules.

The next post will describe how to create a small digital clock app (as shown above) using the Qt Creator, something about putting Style Sheets in Qt apps and then get it on your device! Keep experimenting.

Source : http://sourceforge.net/p/necessitas/home/necessitas/

Standard
Embedded, Linux

InfoCanvas – A mini440 Based Web-Client

As part of my MTech project here at COEP, I had spent some considerable amount of time developing a ARM SBC based touchscreen web-client which can easily be customized to be used according to the desired application. (I have demonstrated its use as an information-desk and as a terminal which fetched sensor data from another ARM SBC based web-server) Throughout the development I have taken help from many Embedded Linux enthusiasts and used Open Source technologies all the way.  I posted some stuff about the software design on my blog and sometimes helped some guys through mail also but never ever did I think once even to tell the world how I did it until now when I realized that ethically its wrong not to share good stuff with good guys like you. Oh! its not that I have rewritten a better Kernel than Linux, but some bits here and there are always missing when you work on embedded devices. Thats all I have to give back from the project I have done. Its small and humble but still, its for all of you to play with now. Everyone releases software but no-one releases how to build a complete device kind of thing. I guess I did it at last. Now adapt it according to your needs and create new devices. Host a Diaspora pod on the ARM server or just think crazy.

I am going to release the documentation in the form of my M.Tech. Thesis (Its pretty rough but still covers most of the work) in public domain currently and then will put up the related code/scripts etc on git very soon. The thesis contains some images and and data from various sources from the internet, cited wherever possible. Make use of it as it suits you. Only the work done my me in the thesis is under public domain. At any places if you find some data which is copyrighted, don’t use it without proper permissions.

In case you require some incentive to get interested in playing with such a device, some excerpts from the first chapter :

In this report, which supports and explains the work supplied with it, details of development of an ARM9 SBC based network capable LCD touch-screen device is explained. The device is essentially a versatile HMI module which can be customized according to the various need under various conditions with minimum effort. Stress on the human interaction, networking standards and an aesthetic look has also been given during the development of such a device.

System Overview

As a demonstration, the project has been configured and developed as a full fledged product to be used as a student information desk and feedback system. This device thus is aptly named COEP InfoCanvas. In addition to this another small ARM SBC has been configured as a web-server which serves the latest sensor values attached to the it. This sensor is accessed by the COEP InfoCanvas which displays the remote sensor’s value in text as well as a time series graph on the LCD touch-screen. The COEP InfoCanvas is driven by SBC-I which along with another SBC-II and an Info-Server is connected to the LAN/WAN. Refer Figure 1. SBC-I being network capable acts as a web client an can fetch and display on its LCD, information and other data from any server on LAN/WAN in standard web formats such as HTML and Javascript from Info-Server machine and SBC-II. The Info-Server is a Linux server grade machine which runs a web-server and supplies data to SBC-I. The SBC-II present at a remote location, is designed itself as a tiny ARM web-server which gathers sensor data through its ADC and serves it to the SBC-I for display. The system is pretty modular and uses mainly standard TCP/IP protocols to communicate and send data to and fro.

Figure 1

The system has been designed almost completely using FOSS tools and technologies. Right from configuring the OS to developing applications on it, Open Source tools have dominated the development of this device.

Some Images

This slideshow requires JavaScript.

Standard
Embedded, Linux

Developing Qt Applications for Embedded Targets

Yeah, so now that we have all the required libs (read previous post if you are still struggling for that part) so we are embarking on to develop some cute (read Qt) applications for a mini2440 or whatever embedded target you have. Just before we move any further I am about to make some stuff clear. So, your system looks something like this now that you have the Qt power with you

Qt on an Embedded Device

what we are going to do is create an app that uses the Qt APIs which inturn will use some libs that we just ported and therefore do whatever we tell it to. In this post I’ll just make use of a simple adder program that I have created. I’ll demonstrate how to develop for your x86 host machine and then how to get it working on the ARM target. As always, there are some pre-requisites

  • Toolchain
  • Qt SDK – Offline 689MB
  • Target Board  – ARM, PPC or whatever arch. I use mini2440.
  • Ear-muffs, in case you  can still hear your girlfriend’s shouts

You can however do away with the last part only if you have a geek girl or you are deaf 🙂 So we begin our journey.

Setting Up the Host

Install Qt SDK on your system. Its pretty straight forward. Once its done, get some feel of developing application using Qt for just your x86 host. This means going through the tutorials and the usual stuff of creating a hello world application etc. I’ll cover al this in another post if you wish. but for the time being we’ll just create a small project called adder. Now you can either start making your own app or use mine for testing. Get the simple adder utility that I have made. You can download it either as a tarball or just get it from Gitorious

Also, untar the toolchain and set it up in the PATH so that you get ready for cross-compiling.

Application Development on x86 Host

Now, the good thing is that you can first create an applications and test it on your host. the development is done in Qt Creator which comes with the complete installation of Qt SDK. Open the adder project you just got. You can even just browse through the available examples in your system and create your own if you are well versed with Qt Creator. The utility is quite simple to use and I shall explain how to start development in it in the next post. For now, you can build the ‘adder’ project simply for your desktop by selecting the project and pressing the build and execute button on the left panel.

So once the build is over, your application will get executed on your host successfully. SO we have cover now how to build and execute apps for your desktop host.

Application Development on ARM Target

Now you can have nay target architecture. To test the process, I have used my ol’ faithful ARM mini2440 as the target board. Lets begin then. Remember that in the previous post we ported and put the libs on the mini2440? We had our cross compiled Qt libs and other stuff stored in the  /usr/local/qt directory both on the host and the target. Now just browse to that directory and see if you have the qmake binary in usr/local/qt/ Now add this to the PATH variable so that you can run qmake.

No go to the adder directory and do a

qmake adder.pro
make

You will notice that in the same directory, a binary called adder will be created. if you do a file on it, you will see that its for an ARM target. Of course, make will throw some errors if you have not setup the toolchain properly. Now make sure that you have configured and built Qt with the -qt-mouse-tslib -qt-kbd-linuxinput if you are using a touchscreen interface or need to have a USB keyboard interface.

Also you may need to modify the width and height in the following environment variable according to the proper font size display and the screen. The value below works best for a 15″ VGA monitor

export QWS_DISPLAY=LinuxFB:mmWidth=310:mmHeight=190

Now, Its just a matter of transferring the cross-compiled binary to the target system and running it. I have tested running it on a mini2440 connected to a 3.5″ screen, 12.1″ screen and even on a standard LCD with a VGA interface. Upon execution, the aplication looks something like this :

So we have done it at last.  So just keep on developing whatever comes to your mind and port it on to your embedded target. The cool thing is that we can now just get anything made for Qt to work on your target. I have myself tested fully functional browsers (you need Webkit for that, don’t forget!) and image viewers, clock and small games too 🙂 the only limitation you may face of course is the hardware on your target! Feel free to ask for any help and or giving suggestions

Standard
Embedded, Linux

Qt 4.6 on mini2440 – A Definitive Guide

Having Qt on your device is a awesome way to ensure that you and many other developers can enjoy developing applications and having fun in addition to doing rapid application development whenever required. The wealth of support provided by the Qt framework – right from OpenGL to WebKit tempts a embedded developer for sure. So I decided its time for a change. Lets dump the overkiiling Android and take shelter under Qt’s canvas for my device. I kept on roaming the intricately laid out web of information on having Qt 4.6.2 run successfully on my mini2440. But owing to the strange 12.1″ LCD that I have attached to it I was more-or-less stuck on the Touchscreen calibration part. So Now after hits and trials and many cups of tea, I am assembling a guide. I intend to make if definitive but only the willful enough to dare can tell.

What You Need

Qt 4.6.2 

GNU ARM Toolchain

Step 1

Setup the toolchain and modify the PATH variable accordingly. Untar qt-everywhere-opensource-src-4.6.2.tar.gz wherever you like. Mine is /usr/local/qt

Step 2

Replace the whole text in mkspecs/qws/linux-arm-g++/qmake.conf by the following:

#
# qmake configuration for building with arm-linux-g++
#

include(../../common/g++.conf)
include(../../common/linux.conf)
include(../../common/qws.conf)

# modifications to g++.conf
QMAKE_CC                = arm-none-linux-gnueabi-gcc -msoft-float -D_GCC_FLOAT_NOT_NEEDED -march=armv4t -mtune=arm920t -O0 -lts
QMAKE_CXX               = arm-none-linux-gnueabi-g++ -msoft-float -D_GCC_FLOAT_NOT_NEEDED -march=armv4t -mtune=arm920t -O0 -lts
QMAKE_LINK              = arm-none-linux-gnueabi-g++ -msoft-float -D_GCC_FLOAT_NOT_NEEDED -march=armv4t -mtune=arm920t -O0 -lts
QMAKE_LINK_SHLIB        = arm-none-linux-gnueabi-g++ -msoft-float -D_GCC_FLOAT_NOT_NEEDED -march=armv4t -mtune=arm920t -O0 -lts

# modifications to linux.conf
QMAKE_AR                = arm-none-linux-gnueabi-ar cqs
QMAKE_OBJCOPY           = arm-none-linux-gnueabi-objcopy
QMAKE_STRIP             = arm-none-linux-gnueabi-strip
QMAKE_RANLIB            = arm-none-linux-gnueabi-ranlib

load(qt_config)

Remember that the PATH variable should have the location of arm-none-linux-gnueabi-gcc etc.

Step 3

Now turn off compiler optimization by making changes in /mkspecs/common/g++.conf Just change the following line to this :

QMAKE_CFLAGS_RELEASE  += -O0

Step 4

Now we have to configure Qt the way we want it by doing ./configure but we need to specify some options according to our requirements.

./configure -embedded arm -xplatform qws/linux-arm-g++ -prefix \
/usr/local/qt -little-endian -webkit -no-qt3support -no-cups -no-largefile \
-optimized-qmake -no-openssl -nomake tools -qt-mouse-tslib -qt-kbd-linuxinput

Now, this configuration is what I needed, for example for my network enabled device I needed a webkit based application so I provided -webkit option. You can drop in or add whatever you want. Some handy options that you may (or may not require) are :

Enable touchscreen library support : -qt-mouse-tslib

Enable USB keyboard support : -qt-kbd-linuxinput

The above options affect the QtGui library so you need to replace only QtGui.so.x.x file on your root filesystem if you are planning to make changes.

Step 5

So more or less you are done. Its time to do some creative stuff (blah!) just do

make

Wait for a couple of hours or so and find your cross compiled freshly baked libraries in /usr/local/qt/lib/

Setting up Qt 4.6 libs on mini2440

So now, its time to put the libs on your mini machine! You may choose to create a separate filesystem using busybox too. Elaborate tutorials on that are available on the web. Just Google it up. What I will do is, modify the rootfs that came with my mini and remove all unnecessary stuff from it. Create a dir /usr/local/qt on your mini2440’s stock root filesystem and copy the complete lib directory from step 5 to that location using a USB stick/SD Card or something. Remember to do a df on the mini beforehand to see the free NAND you have got. In case the memory is low, delete some stock data – the small video file, mp3 file and some sample images that are present on it.Also remove unnecessary applications viz konqueror and old qt 2.2.0 libraries from the system.

Environment Variables

To make the mini understand the new Qt libs, we’ll add some variables

On your mini2440, edit /etc/inint.d/rcS and add the following line to it

source /etc/qt46profile

Now create the /etc/qt46profile file and the following text to it :

export LD_LIBRARY_PATH=/usr/local/qt/lib
export QTDIR=/usr/local/qt
export QWS_MOUSE_PROTO=IntelliMouse:/dev/input/event0
export QWS_DISPLAY=LinuxFB:mmWidth=310:mmHeight=190

Remember that these variables may change according to your qt libs location and mouse/touchscreen drivers. Go to /dev/input/ and see which file is responsible for which device. For eg. if tslib is configured on your device then the QWS_MOUSE_PROTO  variable will have the value something like

QWS_MOUSE_PROTO=Tslib:/dev/input/event1

So, you are done at last and have your system ready with Qt 4.6.2 libs. Try running a simple application (viz a Analog Clock from the cross-compiled qt examples at /usr/local/qt/examples/widgets/analogclock/analogclock by giving the -qws option on the mini2440 shell as

./analogclock -qws

Thats it! Do tell me if you are able to reach here. In the next two posts I shall be discussing about rapidly developing a GUI application for your mini using Qt Creator and compiling and configuring Tslib for mini2440.

Standard
Embedded, Linux

GNUnify 2011 Days

As usual, I am late with updating my blog on the recent acivities owing to the fact that I have lots of other tasks (a Fringe and Big Bang Theory marathon for the record) But still let me try to recollect what all hapened those days.

Day #1

I remember collecting Hiemanshu from Pune station and then taking him to my room and spending the night having some generic FOSS talks and getting to know each other. We did some initial planning for the first GNUnify day but that didn´t do any good owing to the fact that our arrival led us straight to a chaos. Apparently some miscommunication led to some speaker names being wrongly declared for a couple of talks they had specified for the Fedora Track, but soon things were under control and Neependra and Hiemanshu started with ¨Contributing to Fedora”and ¨Introduction to Virtualization¨ as mentioned on the Fedora GNUnify 2011 Wiki. Soon, the discs and the swag came and it was Fedora all over Room 706. The after lunch session taken by Tanushri was a small demo of the upcoming Gnome3 and issues related to developing GUI apps on it. Soon I met Shakthi Kannan whom I had got in touch on some occassions and our Embedded NIrvana gang (Chaitannya, Amit Karpe, Ksinkar & Co) :p It started feeling like home already 🙂 I interacted with so many interested students and shared ideas with them. Apart from the already heated up Fedora sessions, the air was charged with guys like Ksinkar interested un Embedded Linux. We brainstormed for sometime about the lame Hawkboard and mini2440 and then called it a day. We went for the Firefox dinner at SIMS where I met some other FF contributors.  I returned with Hiemanshu back to my den and we started planning about an awesome idea on Day 2 🙂

Day #2

What we planned last night was somewhat interesting. Actually I designed a PyGTK app PyQrencode sometime back and had been stuck somewhere (I admit I am a lame coder) and Hiemanshu willingly helped me out. Soon we planned to do it all over using PyQt and take the same concept to showcase app development using both platforms. In the process, I learnt some stuff from Hiemanshu too and our sessions the next day were awesomely interesting! I took up an into to Python and PyGTK and HIemanshu showcased the power of PyQt. Sadly, and apparently, Qt was more powerful in terms of ease of development and features and I lost the mini battle we had. By this time I had missed al the interesting stuff that was going on at GNUnify and my Main Session talk was approaching too. I started a quick review of my talk (which was immediateley after Siji Sunny) and started gearing up. After the FOSS.in MiniConf talk, this was somewhat  a not so different experience. The attendence in my talk was moderate but loyal. I could see some familiar and interested faces and knew that question of all sorts would come :-p

My talk Tux Under the Hood was about some personal experiences of mine, gathered while playing around with Linux on embedded devices. I mostly talked about the basics of embedded devices, structure of the Linux OS on them and ended with an elaborate hands-on/demo on QEMU.

Other interesting main session talks were by Neependra and Shakthi on setting up kgdb, GNU Make and Kickstarting C, GCC respectively – which I obviously missed 😦 However, Neependra was kind enough to teach me some stuff during our Embedded NIrvana Sessions at CoEP. I was tired and hungry by this time, and there was a prize ceremony going on for the winners of programming contest. As soon as it was over, I met some other senior PLUG guys and soon me and He-Man-Sue were on our way back pondering what all we did.

It was a nice experience and I got to know a lot about what goes on inside a FOSS contributorś mind. some pics from the event are here :

Standard