ok6410 android driver(5)

  Test the android driver by JNI (Java Native Interface),

  In the third article, we know how to compiler the moduler for localhost, ok6410 and goldfish platform.

http://www.cnblogs.com/plinx/p/3209500.html

  But we didn't try to test the driver using by C/C++ program on ok6410 and goldfish.

  Android was changing from linux, so there must remain some fetures linux working.

  If we want to test a driver using C/C++ program, we should get ready for the following conditions :

  (1) Goldfish emulator/Ok6410/Phone must got the permission to "root".

  (2) The exec file compiled by the cross compiler.

  It's easy to go on within these limits, we just need to compile the C/C++ program in the android source configured by "Android.mk".

  

  Here is the Android.mk :

LOCAL_PATH := $(call my-dir)



include $(CLEAR_VARS)



# files to compiler

LOCAL_SRC_FILES := ftest.c



# obj name - the output execute file name

LOCAL_MODULE := ftest



LOCAL_MODULE_TAGS := optional



include $(BUILD_EXECUTABLE)

  And the ftest.c is :

#include <stdio.h>

#include <fcntl.h>

#include <unistd.h>

#include <stdlib.h>

#include <string.h>



int main(int argc, char *argv[])

{

    int testdev;

    int ret;

    int num = 0;

    unsigned char buf[4];



    testdev = open("/dev/wordcount2", O_RDWR);



    if(testdev == -1) {

        printf("Can not open file.\n");

        return 0;

    }



    if(argc > 1) {

        printf("Strings : %s.\n", argv[1]);

        write(testdev, argv[1], strlen(argv[1]));

    }



    read(testdev, buf, 4);



    num =     ((int)buf[0]) << 24 | \

        ((int)buf[1]) << 16 | \

        ((int)buf[2]) << 8  | \

        ((int)buf[3]);



    printf("Word Byte : 0x%d%d%d%dH\n", buf[0], buf[1], buf[2], buf[3]);

    printf("word count: %d\n", num);



    close(testdev);

    return 0;

}

  now, we should mkdir in the source code :

$ pwd

~/Android/source_code/development/ndk/wordcount

$ ls

Android.mk  ftest.c

  then return to the root directory :

$ cd ~/Android/source_code/

$ source build/envsetup.sh

$ mmm development/ndk/wordcount

...

Install: out/target/product/generic/system/bin/ftest

...

  Then we just need to push the file to emulator and test it.

    

你可能感兴趣的:(android)