Wednesday, November 21, 2012

Modem and linux

Most of the modems are available with USB interface. USB interface is a good option as it supports high speed communication (35MBPS by USB 2.0).  There are certain good utilities available to troubleshoot devices connected to USB interface.
"lsusb" command lists USB devices with device id, vendor id and its description.
To see more details of devices such as driver in use for the device, device class, product name and manufacturer name, etc "usb-devices" is another good utility. Same details could be seen at /sys/kernel/debug/usb/devices.

Coming back to our purpose. Here we will be communicating to modem over serial interface. And so we need a USB to serial converter driver. Once serial data is available we will be seeing the network packet and so will be usb_wwan driver. From above command we can find 3G modem device's vendor id and product id. Lets go to make things working.


$modprobe usbserial vendor=0x<vendor id> product=<product id>
$modprobe usb_wwan option


Now driver lets go to network interface part.
Create node using ip link and bring it up with dhcp client to get ip address.


http://antipastohw.blogspot.in/2012/05/how-to-install-3g4g-cellular-modem-on.html
http://freedune.wordpress.com/2011/06/23/success-with-usb-3g-modems-on-android/

USB modem:

https://wiki.archlinux.org/index.php/USB_3G_Modem
http://www.draisberghof.de/usb_modeswitch/
http://www.draisberghof.de/usb_modeswitch/bb/viewtopic.php?t=960

DHCP:

http://www.cyberciti.biz/faq/howto-linux-renew-dhcp-client-ip-address/

Friday, November 16, 2012

Hello JNI

http://marakana.com/forums/android/examples/49.html
I was working on created java native interface for one of my Android app. Its old during Android 2.1 days. Today after couple of years, I tried to create interface again, in my Ubuntu host system, and got some issues. I though better to keep one hello world post for JNI.

Create a project with name of your choice and copy this minimal code in class, lets say in callnative.java


package com.example.ih;

public class callnative {

static {
System.loadLibrary("my_lib");  //to load library
}

public native int add(int x, int y); //calling native function

public native String hello();

}

Go to /workspace/ih/bin path and give below command,

$javah -jni -classpath /home/abhishekkd/workspace/ih/bin/classes com.example.ih.callnative

$cat com_example_ih_callnative.h

You will see a heard file generated something like,


/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_example_ih_callnative */

#ifndef _Included_com_example_ih_callnative
#define _Included_com_example_ih_callnative
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     com_example_ih_callnative
 * Method:    add
 * Signature: (II)I
 */
JNIEXPORT jint JNICALL Java_com_example_ih_callnative_add
  (JNIEnv *, jobject, jint, jint);

/*
 * Class:     com_example_ih_callnative
 * Method:    hello
 * Signature: ()Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_com_example_ih_callnative_hello
  (JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif

Now, implement declared functions,

$gedit hello_lib.c

#include "com_example_ih_callnative.h"

JNIEXPORT jstring JNICALL Java_com_marakana_NativeLib_hello
  (JNIEnv * env, jobject obj) {
return (*env)->NewStringUTF(env, "Hello World!");
}

JNIEXPORT jint JNICALL Java_com_marakana_NativeLib_add
  (JNIEnv * env, jobject obj, jint value1, jint value2) {
return (value1 + value2);
}



Audio System

It is not simple to discuss about audio codec at one page as there are large verity for different purposes and SOC supports. This post is just to trigger one in right direction. Low level understanding is good for architecture such as ARM. For x86, you should directly go to ALSA.

When you are choosing an Audio codec you will be looking for:

Capability -
mono, stereo,  5.1
Number of output lines, number of input lines

Quality -
8kHz to 96kHz ADC audio
8kHz to 96kHz DAC audio
Price -
Depends on above two.

All of above choice depends on the purpose of use such as mobile phone or DOLBI sound system or is it a box for DJ.
You do consider power consumption and signal to noise ratio when you have options.

If we see for media player such as your portable music player or phone, you do consider Capability, you might consider what all are the means of inputs
  • Application/multimedia processor
  • FM
  • Compressed audio codec
  • Bluetooth Modem
Once you have listed your requirement, you are are ready for suitable vendor in market to chose one codec for your self. I will be considering that you are connecting codec direct to application processor and getting raw audio to play.




Have you connected the codec through I2C or SPI (to application processor). You could use UART or I2S, PCI, USB also based on interfaces available through the codec chip or sound card.
You have to write BSP to communicate with the card. Here you will be following datasheet of the product, how to use control lines and passing/getting data to and from the codec to application processor. 
Well, once you have written these codes, now you hare ready to write driver for your kernel. Sound sub-system is already available for you and so you can use API's to develop you application. Alsa good  for Linux and already available in kernel. Just make sure you do proper implementation of your BSP and coupled it with ALSA driver. Now rest is only to play sample app and test it.

Do we need to discuss more on it? There are some good explanation at an introduction to linux audio. Also palying with ALSA utilities, such as alsa mixer, is a good way to learn.

It is good to check official site of driver to get better understanding. 


Links:

Friday, November 9, 2012

D-Bus


to connect to D-bus,
conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, BLUEZ_NAME, &err);
check in      ./external/bluetooth/android-obex-obexd/src/manager.c

Connection failure could be checked in "err".
This will be giving service.

To invoke message in remote object, which must be connected to D-bus,

DBusMessage * dbus_message_new_method_call (const char *destination, const char *path, const char *interface, const char *method)
  Constructs a new message to invoke a method on a remote object.




Check this program:
int main(int argc, char *argv[])
{
struct sigaction sa;
DBusConnection *conn;
DBusError err;

dbus_error_init(&err);
debug("obex-client start to run");
conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, CLIENT_SERVICE, &err);
if (conn == NULL) {
if (dbus_error_is_set(&err) == TRUE) {
fprintf(stderr, "%s\n", err.message);
dbus_error_free(&err);
} else
fprintf(stderr, "Can't register with session bus\n");
exit(EXIT_FAILURE);
}

if (g_dbus_register_interface(conn, CLIENT_PATH, CLIENT_INTERFACE,
client_methods, NULL, NULL,
NULL, NULL) == FALSE) {
fprintf(stderr, "Can't register client interface\n");
dbus_connection_unref(conn);
exit(EXIT_FAILURE);
}

event_loop = g_main_loop_new(NULL, FALSE);

memset(&sa, 0, sizeof(sa));
sa.sa_handler = sig_term;
sigaction(SIGINT, &sa, NULL);
sigaction(SIGTERM, &sa, NULL);

g_main_loop_run(event_loop);

g_dbus_unregister_interface(conn, CLIENT_PATH, CLIENT_INTERFACE);

dbus_connection_unref(conn);

g_main_loop_unref(event_loop);

return 0;
}