arm-linux

http://armboard.taobao.com/

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

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
posted on 2010-06-22 17:09  arm-linux  阅读(305)  评论(0)    收藏  举报