您的位置:首页 > 移动开发 > Android开发

Android Sensor详解(9)Sensor ADSP Sensor1 api使用

2017-10-26 10:07 961 查看

1.Initialize the Sensor1 Framework

sensor1_error_e error;
error = sensor1_init();
if(error != SENSOR1_SUCCESS){
ALOGE(" sensor1_init returned %d\n", error);
}


2. Open a new Sensor1 connection

sensor1_open(sensor1_handle_s **hndl, sensor1_notify_data_cb_t data_cbf,

intptr_t cb_data );

返回值:SENSOR1_EWOULDBLOCK表明此时sensor framework无法打开handle参数无效直到SENSOR1_MSG_TYPE_RETRY_OPEN信息回传才有机会开启

3.Allocate the request message through Sensor1

sensor1_error_e sensor1_alloc_msg_buf(sensor1_handle_s *hndl, 5

uint16_t size, 6

void **buffer );

4.Zero out the Sensor1 request

memset(buffe, 0, sizeof(buffe) );

5.填写buffer request

6. Fill in the message header for the Sensor1 request

sensor1_msg_header_s

Field

service number

msg_id

msg_slze

txn id

Type

uint32 t

int32 t

uint16 t

uint8 t

Description

The QMI service number as defined by the service

Message ID as defined by the service

Size, in bytes, of the c-structure representing the message

A transaction ID defined by the client in the write request in

sensorl_write(); it may be used by the client to identify which Request

caused this Response; in the notify data callback, the txn_id field is only

valid for msg_type and

SENSORI MSG TYPE RESP INT ERR; indications use t,xn id = O

7. Send the Sensor1 request message

sensor1_error_e sensor1_write(sensor1_handle_s *hndl, sensor1_msg_header_s *msg_hdr,

void *msg_ptr);

8.wait for most 1 sec for response

9. Close the Sensor1 connection

sensor1_close

我写的demo 代码如下:

#include <stdlib.h>
#include <cutils/log.h>
#include "sensor1.h"
#include "sns_smgr_api_v01.h"

#ifndef TRUE
#define TRUE 1
#endif /* TRUE */
#ifndef FALSE
#define FALSE 0
#endif /* FALSE */
#ifndef NULL
#define NULL ((void*)0)
#endif

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

//sensor1 handle
sensor1_handle_s *handle_ptr;
sns_smgr_single_sensor_test_req_msg_v01 *msg_req;//
sensor1_msg_header_s               msg_hdr;//sensor message header
int sensor1_response = 0;
/*******************
* function
*******************/
void open_debugsensor(sensor1_notify_data_cb_t callbackfunction);
void alloc_request();
void set_request();
void fill_message_header();
void write_request();
void free_request();
void close_debugsensor();
void test_callback(intptr_t data, sensor1_msg_header_s *msg_hdr_ptr, sensor1_msg_type_e msg_type, void *msg_ptr);
/******************/

int main(int argc, char* argv[])
{
sensor1_error_e error;
// Initialize the Sensor1 Framework
printf("[alex]sensor init");
error = sensor1_init();
if(error != SENSOR1_SUCCESS){
ALOGE("[alex]sensor1_init returned %d\n", error);
}
// Open a new Sensor1 connection
open_debugsensor(test_callback);
//Allocate the request message through Sensor1
alloc_request();
//set request message
set_request();
// Fill in the message header for the Sensor1 request
fill_message_header();
// Send the Sensor1 request message
write_request();
// free the request message through Sensor1
free_request();
//Close the Sensor1 connection
close_debugsensor();
return 0;
}
void open_debugsensor(sensor1_notify_data_cb_t callbackfunction)
{
sensor1_error_e error;

error=sensor1_open(&handle_ptr,callbackfunction,(intptr_t)(&handle_ptr));
if(error == SENSOR1_EWOULDBLOCK){
ALOGE("[alex] sensor1_open returned EWOULDBLOCK. Waiting 3 sec for sensor availability\n");
sleep(3);
error = sensor1_open(&handle_ptr,callbackfunction,(intptr_t)(&handle_ptr));
if (error == SENSOR1_SUCCESS) {
ALOGI("[alex] sensor1 is available now\n");
} else {
ALOGE("[alex] sensor1 is still not available\n");
printf("sensor1_open ERROR\n");
exit(-1);
}
}else if(error != SENSOR1_SUCCESS){
printf("sensor1_open ERROR\n");
exit(-1);
}
}
void alloc_request()
{
sensor1_error_e error;
error=sensor1_alloc_msg_buf(handle_ptr,sizeof(sns_smgr_single_sensor_test_req_msg_v01),(void **)&msg_req);
if(error != SENSOR1_SUCCESS){
printf("sensor1_alloc_msg_buf ERROR\n");
exit(-1);
}
// Zero out the Sensor1 request
memset(msg_req, 0, sizeof(sns_smgr_single_sensor_test_req_msg_v01));
}
void free_request()
{
sensor1_error_e error;
error=sensor1_free_msg_buf(handle_ptr,(void **)msg_req);
if(error != SENSOR1_SUCCESS){
printf("sensor1_free_msg_buf ERROR\n");
}
}
void set_request()
{
msg_req->SensorID=SNS_SMGR_ID_ACCEL_V01;
msg_req->DataType=SNS_SMGR_DATA_TYPE_PRIMARY_V01;
msg_req->TestType=SNS_SMGR_TEST_CONNECTIVITY_V01;
}
void fill_message_header()
{
msg_hdr.service_number = SNS_SMGR_SVC_ID_V01;
msg_hdr.msg_id = SNS_SMGR_SINGLE_SENSOR_TEST_RESP_V01;
msg_hdr.msg_size= sizeof(sns_smgr_single_sensor_test_req_msg_v01);
}
void write_request()
{
sensor1_error_e error;
error=sensor1_write(handle_ptr,&msg_hdr,msg_req);
int timeout=0;
if(error != SENSOR1_SUCCESS){
ALOGE("[alex] sensor1_write error, returned %d\n", error);
if (error != SENSOR1_EWOULDBLOCK) {
printf("sensor1_write error, returned %d\n",error);
exit(-1);
}
}
//wait for most 1 sec for response
sensor1_response = 0;
while (sensor1_response != 1 && timeout < 1000) {
usleep(1000);
timeout++;
}

sensor1_response = 0;
if (timeout >= 1000) {
printf("ERROR : timeout for response message\n");
exit(-1);
}
else {
ALOGE("[gsensor_status] sensor1 get response to message\n");
}
}
void close_debugsensor()
{
sensor1_error_e error;
error= sensor1_close(handle_ptr);
if (error != SENSOR1_SUCCESS) {
printf("ERROR: sensor1_close returned %d\n",error);
exit(-1);
}
}

void test_callback(intptr_t data, sensor1_msg_header_s *msg_hdr_ptr, sensor1_msg_type_e msg_type, void *msg_ptr)
{
int status = 0;

if (msg_hdr_ptr == NULL) {
ALOGE("[gsensor_status] receive NULL msg_hdr_ptr!\n");
} else {
fflush(NULL);
}

if (msg_type == SENSOR1_MSG_TYPE_RESP) {
sns_smgr_single_sensor_test_resp_msg_v01* resp_ptr = (sns_smgr_single_sensor_test_resp_msg_v01*) msg_ptr;

ALOGI("[gsensor_status] received a test response\n");
if ((resp_ptr->Resp.sns_result_t == 0) && (resp_ptr->Resp.sns_err_t == SENSOR1_SUCCESS)) {
if (resp_ptr->TestStatus == SNS_SMGR_TEST_STATUS_PENDING_V01) {
ALOGI("[gsensor_status] Received a test response successfully\n");
} else {
ALOGE("[gsensor_status] test error! TestStatus = %u\n", resp_ptr->TestStatus);
ALOGE("ERROR: test error! TestStatus = %u\n", resp_ptr->TestStatus);
ALOGE("FAIL\n");
status = 1;
}
}
else {
ALOGE("[gsensor_status] error found in common resp! sns_result %u sns_err %u\n",
resp_ptr->Resp.sns_result_t, resp_ptr->Resp.sns_err_t);
ALOGE("ERROR: error found in common resp! sns_result %u sns_err %u\n",
resp_ptr->Resp.sns_result_t, resp_ptr->Resp.sns_err_t);
ALOGE("FAIL\n");
status = 1;
}

sensor1_response = 1;
} else if (msg_type == SENSOR1_MSG_TYPE_IND) {
sns_smgr_single_sensor_test_ind_msg_v01 *ind_ptr = (sns_smgr_single_sensor_test_ind_msg_v01*) msg_ptr;

if (ind_ptr->TestResult == SNS_SMGR_TEST_RESULT_PASS_V01) {
ALOGI("[gsensor_status] Received a test indication, test result is PASS\n");
printf("1\n");
}
else {
ALOGE("[gsensor_status] Received a test indication, test result is FAIL\n");
ALOGE("ERROR: Received a test indication, test result is FAIL\n");
ALOGE("FAIL\n");
status = 1;
}
} else {
if (msg_type == SENSOR1_MSG_TYPE_BROKEN_PIPE) {
ALOGE("[gsensor_status] receive BROKEN_PIPE!!!\n");
ALOGE("ERROR: receive BROKEN_PIPE!!!\n");
} else if (msg_type == SENSOR1_MSG_TYPE_RETRY_OPEN) {
ALOGE("[gsensor_status] receive RETRY_OPEN!!!\n");
ALOGE("ERROR: receive RETRY_OPEN!!!\n");
} else {
ALOGE("[gsensor_status] receive INVALID MSG type!!!, error type = %u\n", msg_type);
ALOGE("ERROR: receive INVALID MSG type!!!, error type = %u\n", msg_type);
}
ALOGE("FAIL");
status = 1;
}

if (msg_ptr != NULL) {
sensor1_free_msg_buf(*((sensor1_handle_s**)data), msg_ptr);
}

if (status) {
printf("0\n");
exit(-1);
}
}
}


希望我的开源能够让高通的BSP开发不再是壁垒
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: