Led.h hardware/modules/include/Mokoid

# include < hardware/ hardware. h>
……….
struct led_module_t {
struct hw_module_t common;
} ;

struct led_control_device_t {
struct hw_device_t common;
/* supporting control APIs go here */
int ( * set_on) ( struct led_control_device_t * dev, int32_t led) ;
int ( * set_off) ( struct led_control_device_t * dev, int32_t led) ;
} ;

/*****************************************************************************/

struct led_control_context_t {
struct led_control_device_t device;
} ;
# define LED_HARDWARE_MODULE_ID "led"



Led.c hardware/modules/Led

static int led_set_on( struct led_control_device_t * dev)
{
//FIXME: do system call to control gpio led//可以调用真正的驱动程序,如write,read等

LOGI( "led_set_on" ) ;
return 0;
}
static int led_set_off( struct led_control_device_t * dev)
{
//FIXME: do system call to control gpio led// //可以调用真正的驱动程序,如write,read等

LOGI( "led_set_off" ) ;
return 0;
}
static int led_device_open( const struct hw_module_t* module, const char * name,
struct hw_device_t* * device)
{
struct led_control_device_t * dev;
dev = ( struct led_control_device_t * ) malloc ( sizeof ( * dev) ) ;
memset ( dev, 0, sizeof ( * dev) ) ;
dev- > common. tag = HARDWARE_DEVICE_TAG;
dev- > common. version = 0;
dev- > common. module = module;
dev- > common. close = led_device_close;
dev- > set_on = led_on;
dev- > set_off = led_off;
* device = & dev- > common;
success:
return 0;
}

static struct hw_module_methods_t led_module_methods = {
open : led_device_open
} ;

const struct led_module_t HAL_MODULE_INFO_SYM = {
common: {
tag: HARDWARE_MODULE_TAG,
version_major: 1,
version_minor: 0,
id: LED_HARDWARE_MODULE_ID,
name: "Sample LED Stub" ,
author: "The Mokoid Open Source Project" ,
methods: & led_module_methods,
}
/* supporting APIs go here */
} ;



LedService.java frameworks/base/service/java/com/mokoid/Server

public final class LedService extends……. . {
static {
System . load ( "/system/lib/libmokoid_runtime.so" ) ;
}
public LedService( ) {
Log . i( "LedService" , "Go to get LED Stub..." ) ;
_init( ) ;
}
/*
* Mokoid LED native methods.
*/

public boolean setOn( int led) {
Log . i( "MokoidPlatform" , "LED On" ) ;
return _set_on( led) ;
}
public boolean setOff( int led) {
Log . i( "MokoidPlatform" , "LED Off" ) ;
return _set_off( led) ;
}
private static native boolean _init( ) ;
private static native boolean _set_on( int led) ;
private static native boolean _set_off( int led) ;
}



com_mokoid_server_LedService.cpp frameworks/base/service/Jni

static jboolean mokoid_setOn( JNIEnv* env, jobject thiz, jint led) { ………}
static jboolean mokoid_setOff( JNIEnv* env, jobject thiz, jint led) { ………}
static inline int led_control_open( const struct hw_module_t* module, struct led_control_device_t* * device) { ………. . }
static jbooleanmokoid_init( JNIEnv * env, jclass clazz) { …………. . }
static const JNINativeMethod gMethods[ ] = {
{ "_init" , "()Z" , ( void * ) mokoid_init} ,
{ "_set_on" , "(I)Z" , ( void * ) mokoid_setOn } ,
{ "_set_off" , "(I)Z" , ( void * ) mokoid_setOff } ,
} ;
static int registerMethods( JNIEnv* env) {
static const char * const kClassName = "com/mokoid/server/LedService" ;
jclass clazz;
/* look up the class */
clazz = env- > FindClass( kClassName) ;
if ( clazz = = NULL ) {
LOGE( "Can't find class %s/n" , kClassName) ;
return - 1;
}
/* register all the methods */
if ( env- > RegisterNatives( clazz, gMethods,
sizeof ( gMethods) / sizeof ( gMethods[ 0] ) ) ! = JNI_OK)
{
LOGE( "Failed registering methods for %s/n" , kClassName) ;
return - 1;
}
/* fill out the rest of the ID cache */
return 0;
}

jint JNI_OnLoad( JavaVM* vm, void * reserved) {
JNIEnv* env = NULL ;
jint result = - 1;

if ( vm- > GetEnv ( ( void * * ) & env, JNI_VERSION_1_4) ! = JNI_OK) {
LOGE( "ERROR: GetEnv failed/n" ) ;
goto bail;
}
assert ( env ! = NULL ) ;

if ( registerMethods( env) ! = 0) {
LOGE( "ERROR: PlatformLibrary native registration failed/n" ) ;
goto bail;
}

/* success -- return valid version number */
result = JNI_VERSION_1_4;

bail:
return result;
}



LedManager.java frameworks/base/core/java/mokoid/Hardware

public class LedManager
{
private static final String TAG = "LedManager" ;
private ILedService mLedService;

public LedManager( ) {

mLedService = ILedService. Stub . asInterface(
ServiceManager. getService ( "led" ) ) ;

if ( mLedService ! = null ) {
Log . i( TAG, "The LedManager object is ready." ) ;
}
}

public boolean LedOn( int n) { ……………. . }

public boolean LedOff( int n) { ………………}
}



LedSystemServer.java apps/ledtest/src/com/mokoid/LedTest

public class LedSystemServer extends Service {

@Override
public IBinder onBind( Intent intent) {
return null ;
}

public void onStart( Intent intent, int startId) {
Log . i( "LedSystemServer" , "Start LedService..." ) ;

/* Please also see SystemServer.java for your interests. */
LedService ls = new LedService( ) ;

try {
ServiceManager. addService ( "led" , ls ) ;
} catch ( RuntimeException e) {
Log . e( "LedSystemServer" , "Start LedService failed." ) ;
}
}



LedTest.java apps/ledtest/src/com/mokoid/LedTest

.O {color:black; font-size:149%;} a:link {color:#0083C0 !important;} a:active {color:#D45831 !important;} a:visited {color:#6FA6D6 !important;} <!--.sld {left:0px !important; width:6.0in !important; height:4.5in !important; font-size:103% !important;} -->

public class LedTest extends Activity implements View . OnClickListener {

private LedManager mLedManager = null ;
@Override

public void onCreate( Bundle savedInstanceState) {
super . onCreate( savedInstanceState) ;
// Start LedService in a seperated process.

startService( new Intent( "com.mokoid.ledsystemserver" ) ) ;

Button btn = new Button ( this ) ;
btn. setText ( "Click to turn LED 1 On" ) ;
btn. setOnClickListener( this ) ;

setContentView( btn) ;
}



LedSystemServer.java apps/ledtest/src/com/mokoid/LedTest
LedTest.java apps/ledtest/src/com/mokoid/LedTest
LedManager.java frameworks/base/core/java/mokoid/Hardware
LedService.java frameworks/base/service/java/com/mokoid/Server
com_mokoid_server_LedService.cpp frameworks/base/service/Jni
Led.h hardware/modules/include/Mokoid
Led.c hardware/modules/Led

更多相关文章

  1. Android 驱动之旅: 第三章 硬件抽象层(HAL)增加接口模块访问硬件
  2. 开发可统计单词个数的Android驱动程序(2)
  3. Android 驱动之旅: 第二章 -- 在Android 系统中增加C 可执行程序
  4. 面向 Android* 设备的英特尔® USB 驱动程序
  5. [置顶] Android 从硬件到应用:一步一步向上爬 1 -- 从零编写底层
  6. Android 从硬件到应用:一步一步向上爬 1 -- 从零编写底层硬件驱动
  7. Windows下安装Android SDK与USB驱动程序

随机推荐

  1. ListView之setEmptyView的问题
  2. android中的VersionCode和VersionName到
  3. TextView和EidtText使用技巧
  4. android Handler 介绍
  5. 吹雪花demo,学习android传感器《IT蓝豹》
  6. 创建android文件系统(Root file system)
  7. 【Android(安卓)应用开发】 Android(安卓
  8. Android(安卓)安装卸载程序
  9. Android(安卓)splitActionBarWhenNarrow
  10. Android(安卓)自定义View及其在布局文件