Friday, November 25, 2016

Streamer

This is some quick notes on cross compiling xawtv. I had to get a webcam working on an embedded device this week and after not getting much success with a simple v4l app, (derived from simplewebcam android app's native library), I decided to cross compile xawtv. The only interesting part was the streamer console application.

I cross compiled this for arm. There was not much support for cross compilation as far as I could see.

XawTv depended on libv4l for which I had to compile v4l utils. This in turn needed libjpeg support. SourceForge gave me version 6 of libjpeg which had issues with libtool while cross compiling. Eventually I built version 9 which was obtained from some other location. To get the library, i had to use --enable-shared flag at the time of configuring. Then I compiled v4l utils. All this was done by setting LDFLAGS, CFLAGS and CXXFLAGS appropriately. As I remember, compiling v4l utils needed -Wl,-rpath-link=path_to_jpeglib in compiled flags.

There was some code that needed x11 libraries and I just commented them using # if 0. Another point to note is that output goes to .lib for these packages.

Now, for xawtv, I set the compiler and linker flags and configured using --host option to indicate that it is a cross compilation. However, I ran into problems as LDFLAGS was not honoured. After a long struggle, I figured out that flag was called DLFLAGS or something similar in Makefile. I set that to my current LDFLAGS value. Things went fine to some extend and I got streamer compiled. However, I was getting many warnings and errors since the build kept looking at /usr/include though I was cross compiling. Using -nostdinc only worsened the pain as I had to then use -I flag for each standard include folder and sub folders. So, I gave up on that and looked at what was causing the error. I could see that among many warnings there was only one error. It was related to FD_XXX macros of select.

Though I first ignored all these and decided to use only streamer (which was already compiled at this stage), my happiness was short lives as I found that the streamer used libng and the plugins present inside that folder.

I then commented many places where select appeared. However, same was appearing in v4l drivers. There alone I replaced select with Poll. My streamer and drivers were now ready. But, while running, streamer was not able to load drivers due to some unresolved symbols in drivers.

After a feel in entire source code, I found that they were present in libng and I only had a static library for libng. At this point, a word about makefiles in xawtv. The top folder has main Makefile. It then had Subdir.mk in each sub-folder which is included by main make file. Quick look at Subdir.mk Will give one a good idea about how to build an application/library. After seeing the objects that go into libng.a, I wrote a simple make file to get a corresponding .so file (arm-xxx-gcc -fPIC -shared needed.o -o libng.so). Then I wrote a similar make file for streamer and used this so to build streamer. (Streamer was using .a earlier). Then I copied the streamer and new libng.so to target.

Viola! "./streamer -o image.ppm -D v4l2" gave me my first image and I ended my day's work after seeing it via ppm2tiff. Well, I was really refreshing to see the same old mundane scenes via that image :)

Wednesday, October 19, 2016

Android logging

I was looking at building a framework for logging which is expected to be platform independent. However, truth is that it cannot be really platform independent. If I say that all processes send the log to the logging framework using IPC and stop there, I will not be doing justice to the job of defining the architecture. I need to see what type of IPC is better for our QAs. (Here the main quality attribute that we consider is performance).

Now comes another problem. If I evaluate the IPC for one of the platform (custom made Linux) against IPC that in Android, (two of the five or six platforms that I need to consider) it might so happen that IPC mechanism chosen for Linux is not available under Android. So, I eventually decided to go bit deeper and look at the details of IPC mechanisms available in Android.

It was not that easy to understand the whole framework in a short duration. However, the following page was really helpful.

http://elinux.org/Android_Logging_System

So, every application uses a library function and writes to some buffers in kernel. I checked the corresponding code. Lets start with Log.java

The file is present in frameworks/base/core/java/android/util/Log.java
No magic seems to be happening there. It just calls corresponding native function println_native
println_native is in frameworks/base/core/jni/android_util_Log.cpp. However, the real work happens in __android_log_buf_write which is inside system/core/liblog/logd_write.c (or logd_write_kern.c depending on compile time flag). It calls write_to_log which is a function pointer which very intelligently implements a singleton.

Let us look at logd_write.c first:
At first write_to_log is set to __write_to_log_init which calls __write_to_log_initialize. Which opens a file "/dev/pmsg0" and a Unix socket /dev/scoket/logdw. The pointer is then modified to point to __write_to_log_daemon and subsequent calls reach that function.

In this function (__write_to_log_daemon), the log is written to both the socket and the device. We will first see what happens to socket.

The listener is in system/core/logd/LogListener.cpp. However, this class extends SocketListener. The real work is done in SocketListener::runListener. The socket is read for each client here. It took me a while to find out how the generic SocketListener class passes data to LogListener. SocketListener class has a virtual function onDataAvailable(SocketClient *c) which is implemented by LogListener. When the SocketListener finds that there is data to be read from the socket, it calls the onDataAvailable function  which maps to corresponding function of LogListener class.

What happens in LogListener::onDataAvailable?
The message is read and it is put into a LogBuffer. It also informs the reader when a new message arrives. All the connected clients can now get the data from the reader.

Apps-->Log-->/dev/socket/logdw-->LogListener-->LogBuffer-->LogReader-->/dev/socket/logdr-->Clients of log reader!

Well, I will update the log what happens to /dev/pmsg0 later. Time to sleep. Anyway, my takeaway from this is that I can safely use unix sockets for IPC in both Android and Linux without bothering much about performance and my code might be portable across these two platforms if I take a little bit of care.

Note: I have looked at Anroid 6.0.1 source to get this information.

Wednesday, September 21, 2016

Getting back to blogging

For a long time I was not blogging anything. However, recently I had to study Lucene for some project and I also started work on a application that is being developed on Linux. This made me think that I should also restart this blog. As part of the new application that I am working on, I have started learning python and I will post more about it later. I have lot of things planned for future. On the Linux front, I am looking at getting a test machine setup and spend sometime on some of the industry standard software (mainly from Apache software foundation).

I will take sometime, but, I will surely restart these blogs!

Sunday, May 11, 2014

Win7 access point

OK, this time, its slightly different. It is not about Linux, but about windows 7. I was looking into building an application for pebble watch and while reading about their cloud IDE, I came to know that I need to have both development PC and the Android phone need to be on same wifi network. However, my wifi router was dead long back and I was managing using ad-hoc networks between my laptops. (Well, I had taken over my wife's old laptop after I bought a new one for her and it was running win-7. Since display was dead for that laptop, I have hooked a LCD monitor to it and it was working as my wifi router in adhoc mode)

Now, it was definite that I need an AP at home and thought about buying one. Each one costed more than 1.5K INR or more. And, I had the bad experience of wifi on my D-Link DSL-2730U dying within 3 months of buying it. Actually, it did not die... but, the its dc adapter did and after a few months from replacing the original adapter with a local made one, I noticed that whole device kept rebooting if wifi was on. When the frequency of rebooting became really unbearable, I decided to move to my ad-hoc network. So, the bottom line is, even if I spent 1.5K, there is nothing that I can do for adapter which is not covered under warranty and so, I decided against buying it.

I then read about having the access point setup on the win7 machine. It was much easier than I thought. After a bit of searching on google, I came up with the following solution.


  • Start the command prompt as admin user (type cmd in search box and when cmd appears on top, right click and select run as)
  • The bellow commands will create an AP
    • netsh wlan set hostednetwork mode=allow "ssid=YourNwkName" "key=0Your1Secret3Key" keyUsage=persistent
    • netsh wlan start hostednetwork
  • Change SSID and KEY as per your choice
  • You can see the new virtual WIFI port and set it properties at the same location where you see the normal network adaptors. (Start -> run -> type "ncpa.cpl" sans quotes -> enter)
  • Right click on the adapter that is connected to internet and select properties
  • Select the sharing tab, enable it and select the home network as the newly created network
  • Windows will move home network to some vague IP address block that is not so common. You may change it by editing the IPv4 properties of that network
  • Install a dhcp server and configure it
    • I used the server from http://www.dhcpserver.de/
    • The setup is explained at http://www.dhcpserver.de/dhcpsetup.htm
    • However, I have briefly explained some points to take care below
    • It comes with a firewall, setup the firewall to allow access

Test your network and be satisfied that things work as expected. Now comes the interesting part. How do I turn on this stuff at start-up? Again came google to the rescue :)

  • Create a batch file to create and start hosted network (I saved it as startap.bat)
  • Add dhcp server startup also to it. Final script will look something like below
netsh wlan set hostednetwork mode=allow "ssid=YourNwkName" "key=0Your1Secret3Key" keyUsage=persistent
netsh wlan start hostednetwork
D:\dhcpserv\dhcpsrv.exe -ini D:\dhcpserv\dhcpsrv.ini -start
  • Download and install nssm and the batch script as a startup service to windows
    • I created a run.bat which contained the following line and executed it as administrator by right clicking it
      • D:\dhcpserv\nssm-2.21.1\nssm-2.21.1\win32\nssm.exe install apsrv
    • The above action will bring up a GUI and you can select bash script created in previous step (startap.bat) as the command to run
  • In your scripts, change the paths as per where you have installed dhcpsrv, 
Now a bit about dhcp configuration
  1. Set dns server and gateway
  2. Do remember to turn on firewall
  3. Save the configuration
(I wanted add a few screenshots... but now I am too sleepy...)

Tuesday, March 4, 2014

The price war...

Vishal Tripathi, an analyst at Gartner reportedly believes that the Windows brand still resonates with people. “If people have an option to get a Windows tablet at price that is similar to an Android tablet, they will likely pick it. A low-cost Windows tablet is not going to shake the market but it will help Microsoft gain some consumers who will otherwise go to Android,” he said.
http://tech.firstpost.com/news-analysis/microsoft-releasing-rs-10000-windows-tablets-mid-year-take-android-219367.html

Well, I do not agree to that. Free is FREE. 15 != 0. But, the fact that is really going to go against them is that, people will not pick windows just because there is an option! If that was the case, they should have already been picking up windows mobiles from Nokia. But, they are not doing it!

Reason? Customers have gone more tech savy and they understand what they are getting. They spent 80% of their time in apps. MS has a store with 10^5 apps or a little more. On Android, they have about 10^6 apps. About 10 times more!

Of course,  people have not forgotten name of MS yet. But, they are more familiar with the name of Android when it comes to phones and tablets. The killer apps on any MS OS are office apps. If they are not going to play that trump card by making office dirt cheap, it will be difficult to make people forget about Android.

Again, in past we have seen Amazon, Nokia etc forking Android. Can it happen in case of any MS OS?

So, the bottom line is, MS cannot win by simply reducing the price of OS. They have to catch up on apps, reduce the cost of office suite and learn from Android about how to force your bitter most enemy to come to you for this survival and then help him to survive :) The last point lets you survive in a fiercely competitive market irrespective of whether you win or your opponent wins eventually. Last at least is something that MS can never do. They are good at closing windows to people, but Android has none to close.