2012/06/15

Linux Device Driver Topics

Tested on Ubuntu 12.04

Where does printk go
The result of the printk wouldn't be displayed on terminal, we can find them at 
tail -f /var/log/kern.log

To load module
use insmod
modprobe may not work
http://stackoverflow.com/questions/3140478/fatal-module-not-found-error-using-modprobe
lsmod can list loaded module


Poll and interrupt
unsigned int (*poll) (struct file *filp, poll_table *wait);

The driver method is called whenever the user-space program performs a poll, select,or epoll system call involving a file descriptor associated with the driver. The device method is in charge of these two steps:
1. Call poll_wait on one or more wait queues that could indicate a change in the poll status. If no file descriptors are currently available for I/O, the kernel causes the process to wait on the wait queues for all file descriptors passed to the system call.
2. Return a bit mask describing the operations (if any) that could be immediately performed without blocking.

Driver entry points such as read() and poll() operate in tandem with interrupt handler roll_interrupt(). For example, when the handler deciphers wheel movement, it wakes up any waiting poll() threads that may have gone to sleep in response to a select() system call issued by an application

Ubuntu 12.04 Https setup with Self Signed Certification

http://charles.lescampeurs.org/2012/01/14/ubuntu-11-10-setting-up-apache2-and-ssl-with-self-signed-certificate

2012/06/08

Ubuntu 12.04 Https Setup with CA

http://www.tc.umn.edu/~brams006/selfsign.html
http://www.tc.umn.edu/~brams006/selfsign_ubuntu.html

Install Software
sudo apt-get install openssl apache2

Create CA
openssl genrsa -des3 -out ca.key 4096
openssl req -new -x509 -days 365 -key ca.key -out ca.crt

Create Server Key
openssl genrsa -des3 -out server.key 4096
openssl req -new -key server.key -out server.csr

Sign Certificate
openssl x509 -req -days 730 -in server.csr -CA ca.crt -CAkey ca.key -set_serial 01 -out server.crt

Verify Result
openssl rsa -noout -text -in server.key
openssl req -noout -text -in server.csr
openssl rsa -noout -text -in ca.key
openssl x509 -noout -text -in ca.crt

Make Apache Quiet
openssl rsa -in server.key -out server.key.insecure
mv server.key server.key.secure
mv server.key.insecure server.key
Results
server.crt: The self-signed server certificate.
server.csr: Server certificate signing request.
server.key: The private server key, does not require a password when starting Apache.
server.key.secure: The private server key, it does require a password when starting Apache.
ca.crt: The Certificate Authority's own certificate.
ca.key: The key which the CA uses to sign server signing requests. 

Move files
sudo mkdir /etc/apache2/ssl
sudo cp server.key /etc/apache2/ssl
sudo  cp server.crt /etc/apache2/ssl 
Setup SSL module
sudo ln -s /etc/apache2/sites-available/default-ssl /etc/apache2/sites-enabled/000-default-ssl 

sudo mkdir /var/www-ssl
sudo su
cd /etc/apache2/sites-available
cp /etc/apache2/sites-available/default default_original
cp /etc/apache2/sites-available/default-ssl default-ssl_original
Make Sure below in the default-ssl configuration file
SSLEngine On
SSLCertificateFile /etc/apache2/ssl/server.crt
SSLCertificateKeyFile /etc/apache2/ssl/server.key 

Search around a certain lantitute and longitude

A great article about lantitute and longitude
http://www.techraza.com/other/search-around-a-particular-latitude-and-longitude-by-a-given-radius/

Copied from above URL
------------
This post is related to search around a particular latitude and longitude for a given radius in Km or Miles.
  • One degree of latitude is equal to about 69 or 70 miles or 111 Km.
  • Suppose you have to search in a circle of 4 miles ,the center of the the circle is lat,long you have.
  • Since 70 miles is equal to 1 degree lat then the latitude radius will be “4/70=0.057″ .
  • now suppose the center point lat, long is:latitude=26.262011,longitude=81.546562 then the range will be in which you have to search will be: minLat=26.262011- 0.057  maxLat=26.262011+ 0.057  minLong=81.546562 – 0.057  maxLong=81.546562 + 0.057
  • Now you can run a sql query to database with between statement like:- where latitude between “this” and “this” and longitude between “this” and “this” .
  • It will give you all the location’s lat ,long that are in that 4 miles circle around given lat long center point.

2012/06/05

Read bits from stream

typedef struct readBitsContext { const muchar_t *buff; muint32_t bitpos; muint32_t nb; } readBitsContext_t; static muint32_t readBits(readBitsContext_t *ctx, muint32_t nbits) { const muchar_t *p = ctx->buff + ctx->bitpos/8; muint32_t pos = ctx->bitpos % 8; mint32_t bits2go = nbits; muint64_t res = 0; const muchar_t mask[8] = {0xff, 0x7f, 0x3f, 0x1f, 0x0f, 0x07, 0x03, 0x01}; if(pos + nbits > ctx->nb * 8) { ctx->bitpos = ctx->nb*8; ELOG(log_domain,"Trying to read past end of buffer, ignoring"); goto done; } while (bits2go > 0) { res = (res << 8) | (*p++ & mask[pos]); bits2go -= (8 - pos); pos = 0; } bits2go = -bits2go; res >>= bits2go; ctx->bitpos += nbits; done: return (muint32_t) res; }

2012/06/04

About HTTP Post

Make a Http Post by CURL

curl -d "username=abc&passowrd=def" http://localhost/~noguxun/test1/post.php


At PHP side, it could be handled like


<?php if(isset($_POST['username'])) { echo $_POST['username']; } else { echo "no data"; } if(isset($_POST['password'])) { echo $_POST['password']; } else { echo "no data"; } ?>

2012/06/01

Build Mplayer on Ubuntu

Build Command
./configure --enable-debug=3 --enable-profile --prefix=/home/xgu/mplayer --yasm=''


A Raw Audio Problem

How to reproduce the problem:

try to play a raw pcm will below command :

mplayer -demuxer rawaudio -rawaudio
samplesize=2:channels=1:rate=44100:format=0x10001
http://res.wordjp.com/mplayer/rawaudio_patch/B-LPCM-1.pcm

From the console output, we can see that the dvdpcm codec is using the
default setting, all the setting on the command line were ignored.
And audio is not played correctly.
===============
Opening audio decoder: [dvdpcm] Uncompressed DVD/VOB LPCM audio decoder
AUDIO: 48000 Hz, 2 ch, s16be, 1536.0 kbit/100.00% (ratio: 192000->192000)
Selected audio codec: [dvdpcm] afm: dvdpcm (Uncompressed DVD/VOB LPCM)
===============

After the fix, we can play the the same content like
./mplayer -demuxer rawaudio -rawaudio
sampleformat=0x8:samplesize=2:channels=1:rate=44100:format=0x10001
http://res.wordjp.com/mplayer/rawaudio_patch/B-LPCM-1.pcm


Description of the patch:
1. a new sampleformat config was added
the value of the sampleformat was defined in af_format.h, the value 8
here means AF_FORMAT_S16_BE.

2. make sure other configsare not ignored, default value will not
overwrite the pre-set values from configuration.



The Patch
----------

Index: libmpcodecs/ad_dvdpcm.c
===================================================================
--- libmpcodecs/ad_dvdpcm.c (revision 34980)
+++ libmpcodecs/ad_dvdpcm.c (working copy)
@@ -67,12 +67,27 @@
     sh->samplesize = 2;
  }
     } else {
- // use defaults:
+      // use defaults only if there is no setting
+      // which may come from the user command line
+      mp_msg(MSGT_DECAUDIO, MSGL_DBG2, "Preset Meta: channels %d, samplerate %d, sampleformat %d, samplesize %d",
+        sh->channels, sh->samplerate, sh->sample_format, sh->samplesize);
+      if(0 == sh->channels){      
  sh->channels=2;
+      }
+      if(0 == sh->samplerate ){
  sh->samplerate=48000;
+      }
+      if(0 == sh->sample_format)
+      {
  sh->sample_format = AF_FORMAT_S16_BE;
+      }
+      if(0 == sh->samplesize)
+      {
  sh->samplesize = 2;
     }
+      mp_msg(MSGT_DECAUDIO, MSGL_DBG2, "Initialized meta Info: channels %d, samplerate %d, sampleformat %d, samplesize %d",
+        sh->channels, sh->samplerate, sh->sample_format, sh->samplesize);
+    }
     if (!sh->i_bps)
     sh->i_bps = sh->samplesize * sh->channels * sh->samplerate;
     return 1;
Index: libmpdemux/demux_rawaudio.c
===================================================================
--- libmpdemux/demux_rawaudio.c (revision 34980)
+++ libmpdemux/demux_rawaudio.c (working copy)
@@ -35,6 +35,7 @@
 static int samplesize = 2;
 static int bitrate = 0;
 static int format = 0x1; // Raw PCM
+static int sampleformat = 0;

 const m_option_t demux_rawaudio_opts[] = {
   { "channels", &channels, CONF_TYPE_INT,CONF_RANGE,1,8, NULL },
@@ -42,6 +43,7 @@
   { "samplesize", &samplesize, CONF_TYPE_INT,CONF_RANGE,1,8, NULL },
   { "bitrate", &bitrate, CONF_TYPE_INT,CONF_MIN,0,0, NULL },
   { "format", &format, CONF_TYPE_INT, CONF_MIN, 0 , 0, NULL },
+  { "sampleformat", &sampleformat, CONF_TYPE_INT, CONF_MIN, 0, 0, NULL},
   {NULL, NULL, 0, 0, 0, 0, NULL}
 };

@@ -55,6 +57,7 @@
   w->wFormatTag = sh_audio->format = format;
   w->nChannels = sh_audio->channels = channels;
   w->nSamplesPerSec = sh_audio->samplerate = samplerate;
+  sh_audio->sample_format = sampleformat;
   if (bitrate > 999)
     w->nAvgBytesPerSec = bitrate/8;
   else if (bitrate > 0)

----------

Post Code on Blogger

Simplest way to post code to blogger for me: <pre style="background: #f0f0f0; border: 1px dashed #CCCCCC; color: black;overflow-x:...