Android calls from the framework layer to the HAL Layer

Source: Internet
Author: User

Reprinted from: http://www.cnblogs.com/xl19862005

Author: Xandy

Working Background: The debugging is a car bluetooth module called goc-md-102, because the bluetooth module can not be directly controlled by HCI, however, it already has a ready-made AT command set for control, so I directly communicate through serial port reading and writing in HAL, and then establish a connection with the java layer through JNI.

Considering the efficiency, I used the callback function in HAL to exchange data with the java layer through JNI. I checked that the method of GPS data reporting is the same as that used in my application!

1. The first step is to enable, initialize, and read/write the serial port in HAL. These are simple. Let's mainly look at the code in initializing this function, as shown below:

 

/*************************************** ************************
** Fun: init gow.102_init (/dev/ttymxc1 );
** In:
** Out: fd sucess,-1 false;
** Gow.102_init
**************************************** ***********************/
Static int go1_102_init (BluetoothCallback * callBack)
{
Int fd, var;
Btportinfo pPort_info;
Int err;

PReceiveCmdPackage = malloc (sizeof (bluetooth ));

Memset (pReceiveCmdPackage, 0, sizeof (bluetooth ));
Memset (recCmdBuf, 0, RECCMDBUFLEN );
// Clear message buf
Memset (& pPort_info, 0, sizeof (btportinfo ));

Fd = open_javasthport ();

If (fd <0)
{
LOGE ("go%102_init open port error! ");
Return-1;
}

PReceiveCmdPackage-> fd = fd;
FD = fd;

PPort_info.baud_rate = go%102_baud;
PPort_info.data_bits = gow.102_databit;
PPort_info.flow_ctrl = gow.102_ctrl;
PPort_info.stop_bit = go%102_stopbit;
PPort_info.parity = go%102_parity;
PPort_info.port_fd = fd;

// Pthread_mutex_lock (& pPort_info.portlock );
Var = set_btportLocked (& pPort_info );
// Pthread_mutex_unlock (& pPort_info.portlock );

If (var <0)
{
LOGE ("set_portLocked error! ");
Return-1;
}

// The input function struct pointer will be obtained here. This function struct pointer will be used for subsequent data reporting.
If (callBack! = NULL)
PReceiveCmdPackage-> callback = * callBack;
Else
{
LOGE ("descrithcallback struct is empty! ");
Return-1;
}

// Uart receive message thread and analyze it
Sem_init (& pReceiveCmdPackage-> uart_end, 0, 0 );
PReceiveCmdPackage-> uart_inited = true;

// Err = pthread_create (& pReceiveCmdPackage-> thread_id, NULL, & BTuartDownloadData, (void *) pReceiveCmdPackage );

// Here, a thread is created through callback for reading and reporting serial data. This thread is a java thread created in the VM and must be used, pthread_create cannot be used; otherwise, a problem may occur!
PReceiveCmdPackage-> thread_id = callBack-> incluth_thread ("go%102_bluetooth", BTuartDownloadData, pReceiveCmdPackage );

If (! PReceiveCmdPackage-> thread_id)
{
LOGE ("cocould not create bluetooth thread: % s", strerror (errno ));
Return-1;
}

Return fd;
}

 

When Bluetooth is enabled, the upper-layer app calls the init function through JNI to complete the initialization of the string, and passes in the function struct address that calls the java method in JNI. The structure of this function is as follows:

typedef struct
{
size_t size;
void (*callIn_bt)(telephoneIn *callIn);
void (*state_bt)(int state);
void (*getVol_bt)(BtVol *vol);
void (*connect_bt)(matchDev *btDevice);
void (*match_bt)(matchDev *btDevice);
void (*downPHBook_bt)(phoneNumber *phoneNum);
void (*callOut_bt)(phoneNumber *dailNum);
pthread_t (* bluetooth_thread)(const char* name, void (*start)(void *), void* arg);
}BluetoothCallback,*pBluetoothCallback;

The callback function in this function struct is implemented in JNI. Let's take a look at the callback function for reporting the phone number when a call is made:

Static void telephoneIn_callback (telephoneIn * callIn)
{
JNIEnv * env = AndroidRuntime: getJNIEnv ();
Jstring number = env-> NewStringUTF (callIn-> number );

Dbg (DBG_DEBUG, "JNI telephoneIn_callback ");
// Call the java method to report data
Env-> CallVoidMethod (mBTCallbackObj, method_reportCallIn, callIn-> Len, number );

If (number)
Env-> DeleteLocalRef (number );
CheckAndClearExceptionFromCallback (env, _ FUNCTION __);
}

Here, mBTCallbackObj (jobject) is assigned a value when java calls jni initialization. It should be the corresponding java class, while method_reportCallIn (jmethodID) is the corresponding java method ID in the obtained java, as follows:

static void android_location_BlueToothLocationProvider_class_init_native(JNIEnv* env, jclass clazz) 
{
method_reportCallIn = env->GetMethodID(clazz, "telephoneCallIn", "(ILjava/lang/String;)V");
method_reportState = env->GetMethodID(clazz, "bluetoothState", "(I)V");
method_reportVol = env->GetMethodID(clazz, "reportVol", "(II)V");
method_reportConnect = env->GetMethodID(clazz,"reportConnect","(Ljava/lang/String;[I)V");
method_reportMatch = env->GetMethodID(clazz,"reportMatch","(ILjava/lang/String;[I)V");
method_reportPhoneBook = env->GetMethodID(clazz,"reportPhoneBook","(IILjava/lang/String;Ljava/lang/String;)V");
method_reportDailNum = env->GetMethodID(clazz,"reportDailNumber","(Ljava/lang/String;)V");
}

Let's take a look at the callback function for creating a java thread in jni:

static pthread_t bluetooth_thread_callback(const char* name, void (*start)(void *), void* arg)
{
return (pthread_t)AndroidRuntime::createJavaThread(name, start, arg);
}

When calling the corresponding java class, the method for reporting the number is as follows:

    /**
* called from native code to update call in telephone number
*/
private void telephoneCallIn(int numberLen, String number)
{
if(DEBUG)
Log.v(TAG, "telephoneCallIn number: " + number);

if(numberLen <= 0)
Log.e(TAG,"telpphone call in,but the phone number is null");

if(number != null)
{
// send an intent to notify there is a telephone call in.
Intent intent = new Intent(TELEPHONE_CALLIN_ACTION);
intent.putExtra(EXTRA_BT_PHONENUMBER, number);
mContext.sendBroadcast(intent);
}

}

Finally, let's take a look at how HAL uses this callback function to report data. When a phone number is obtained after resolution, the following yellow-labeled code will be reported to the phone number:

static void processCharacterI(pBluetooth bt,const uuint8 *data)
{
const uuint8 *pdata = data;

dbg(DBG_DEBUG,"processCharacterI : %s",pdata);

switch(*pdata)
{
case 'D':
{
telephoneIn callIn;

memset(&callIn,0,sizeof(telephoneIn));

callIn.Len = (*(++pdata)-0x30)*10;
callIn.Len += *(++pdata)-0x30;
callIn.number = ++pdata;
BLUETOOTH_CALLIN_CB(bt->callback,callIn);
}
break;

case 'S':
BLUETOOTH_STATE_CB(bt->callback,uartInitOK);
break;

case 'C':
{
phoneNumber CallOut;
uuint32 tmp=0;

memset(&CallOut,0,sizeof(phoneNumber));

tmp = (*(++pdata)-0x30)*10;
tmp += *(++pdata)-0x30;
CallOut.nameLen = tmp;

CallOut.number = ++pdata;

BLUETOOTH_CALLOUT_CB(bt->callback,CallOut);
}
break;

default:
LOGW(" Unknow command : I%s",pdata);
break;
}
}

BLUETOOTH_CALLIN_CB is defined as follows:

#define BLUETOOTH_CALLIN_CB(_cb,_in)    \
if((_cb).callIn_bt){ \
(_cb).callIn_bt(&(_in)); \
}

This completes the data reporting link. When the corresponding data is sent from the serial port, the data will be reported to the java method through this callback function, finally, broadcast the program to the corresponding listener in the java method!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.