I need to port an automation tool from Android 2.3 to Android 4.2 based target. I am trying to get some touch events from /dev/input/events0 using open() system call and then use a poll() system call to wait for any changes on its file descriptor.
This works just fine if I write the logic in a C file and run the binary by placing it in system/bin directory. poll method returns and I can read the content using read() method on the descriptor.
I call these methods using JNI in Android 2.3 based target. It works fine, but only if the app is a part of the system image and the library(native code) exists in system/lib directory as a .so file.
But now the problem is, the same code is not running in Android 4.2. The poll() system call never returns, even if I give a lower timeout value.
One important point to note here is, this same behavior was observed when the library existed as a part of the APK file in Android 2.3 and was run as an installed application.
Also currently I have only quick boot build working, no normal boot. APK/ODEX and *.so files is what I have at my disposal.
I am suspecting it is an issue with permissions. Internally android is not giving me access to driver files in 4.2, as application seems to be better sandboxed than it was in 2.3. Ironically, open() works just fine, only when I do poll(), it is not working.
I need to overcome this problem. Please help.
Here is code snippet:-
int i, numread, value, ret, readlen;
struct input_event event;
struct timeval now;
struct timespec tm;
while (run) {
/* wait for input from any of the input devices(/dev/input/events0) */
if (poll(input_fds, NUM_DEVICES , -1) < 0) {
return EVENT_RECORDER_ERROR;
}
__android_log_print(ANDROID_LOG_INFO, "EVENT_RECORD",
"Poll completed");
for (i = 0; i < NUM_DEVICES; i++) {
__android_log_print(ANDROID_LOG_INFO, "EVENT_RECORD",
"device no %d", i);
if (input_fds[i].revents & POLLIN) {
if (input_fds[i].fd == sockfd)
readlen = MAX_DATA_LENGTH;
else
readlen = sizeof(event);
/* read the data here and write it to the output file */
numread = read(input_fds[i].fd, buffer, readlen);
}
}
}
Related
I have a library written in C that I'm using from an Android application. I discovered that the application blocks sometimes while executing various functions (what is common is they are all related to networking).
I'm testing on a real Android device running Android 6.
The library is compiled with NDK version 21.
So far I have observed this behavior with 3 functions - getaddrinfo, poll and socket:
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
getaddrinfo(hostname, port, &hints, &result);
////
struct pollfd wait = {
.fd = fd,
.events = event,
.revents = 0
};
status = poll(&wait, 1, 10000);
////
fd = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
These functions are called several times each after the program starts running. The same call can work fine one time and block indefinitely (at least several minutes) the next time. The internet connection on the device seems to be fine.
Any ideas? How can I even approach debugging such a problem?
I didn't actually find the reason for the problem, but I found how to fix it. Part of the code was written by somebody else and I wasn't sure what exactly it was doing. After rewriting this part, everything started to work fine.
Among other things, the code was spawning a new process with fork(). I suspect the problem to be related to that.
OK, this is a simple question.Does android support the PTRACE_SINGLESTEP when I use ptrace systemcall? when I want to ptrace a android apk program, I find that I can't process the SINGLESTEP trace. But the situation changed when I use the PTRACE_SYSCALL, It can work perfectly. Does the android wipe out this function or arm lack some supports in hardware? Any help will be appreciated!thanks.
this is my core program:
int main(int argc, char *argv[])
{
if(argc != 2) {
__android_log_print(ANDROID_LOG_DEBUG,TAG,"please input the pid!");
return -1;
}
if(0 != ptrace(PTRACE_ATTACH, target_pid, NULL, NULL))
{
__android_log_print(ANDROID_LOG_DEBUG,TAG,"ptrace attach error");
return -1;
}
__android_log_print(ANDROID_LOG_DEBUG,TAG,"start monitor process :%d",target_pid);
while(1)
{
wait(&status);
if(WIFEXITED(status))
{
break;
}
if (ptrace(PTRACE_SINGLESTEP, target_pid, 0, 0) != 0)
__android_log_print(ANDROID_LOG_DEBUG,TAG,"PTRACE_SINGLESTEP attach error");
}
ptrace(PTRACE_DETACH, target_pid, NULL, NULL);
__android_log_print(ANDROID_LOG_DEBUG,TAG,"monitor finished");
return 0;
}
I run this program on shell. And I can get the root privilege.
If I change the request to PTRACE_SYSCALL the program will run normally.
But if the request is PTRACE_SINGLESTEP, the program will get an error!
PTRACE_SINGLESTEP has been removed on ARM Linux since 2011, by this commit.
The HW has no support for single-stepping; previous kernel support involved decoding the instruction to figure out which one's next (branches) and temporarily replacing it with a debug-break software breakpoint.
Quoting a mailing list message about the same commit, describing the old situation: http://lists.infradead.org/pipermail/linux-arm-kernel/2011-February/041324.html
PTRACE_SINGLESTEP is a ptrace request designed to offer single-stepping
support to userspace when the underlying architecture has hardware
support for this operation.
On ARM, we set arch_has_single_step() to 1 and attempt to emulate
hardware single-stepping by disassembling the current instruction to
determine the next pc and placing a software breakpoint on that
location.
Unfortunately this has the following problems:
Only a subset of ARMv7 instructions are supported
Thumb-2 is unsupported
The code is not SMP safe
We could try to fix this code, but it turns out that because of the
above issues it is rarely used in practice. GDB, for example, uses
PTRACE_POKETEXT and PTRACE_PEEKTEXT to manage breakpoints itself and
does not require any kernel assistance.
This patch removes the single-step emulation code from ptrace meaning
that the PTRACE_SINGLESTEP request will return -EIO on ARM. Portable
code must check the return value from a ptrace call and handle the
failure gracefully.
Signed-off-by: Will Deacon <will.deacon at arm.com>
---
The comments I received about v1 suggest that:
If emulation is required, it is plausible to do it from userspace
ltrace uses the SINGLESTEP call (conditionally at compile-time since other architectures, such as mips, do not support this
request) but does not check the return value from ptrace. This is a
bug in ltrace.
strace does not use SINGLESTEP
My code listens to the DCIM folder, using a FileObserver.
All Android versions I used, except 4.1.1, sent only 1 event - when the video was finished taken.
I think it's the correct behavior - write continually and close when finished.
In 4.1.1 (Galaxy Nexus and Nexus S) though, the event FileObserver.CLOSE_WRITE is sent
twice - when the video starts and when it ends.
Also the same for photos - the event is sent twice - though it's not that critical.
The problem is that I can't distinguish between the start event and end event of a video.
I could try and check the size of the file, but because the event may have been delayed (slow/busy device), the size may be quite big.
Any idea why was the behavior changed? Do you know where is the camera's app source code? I can try and look at the history to understand that.
As I wrote in one of my comments, the difference between 4.1 and previous Android versions is that in 4.1.1, the file is written and closed twice. Once when an empty video file is created. Then the video is written into a tmp file. Then the rename/copy of the tmp file is the second write_close event.
In previous versions there's not tmp file - only the original - thus only one close_write event.
Please comment if you think it's a bug. I'm not sure.
I have myself an app which monitors the DCIM/Camera directory through a FileObserver. What I noticed, and could be of help to you, is that the first operation is a CLOSE_WRITE, however the final operation is a MOVED_TO from the .tmp to the real file, which means you can recognize when the video is (really) ready.
My real code is more complex due to the requirements of my app, but the general idea is something like this:
/* My FileObserver implementation field */
private HashSet<String> jbCache = new HashSet(...)
...
protected void onEvent(int event, String path) {
boolean isJellyBean = Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLYBEAN;
if ((event & FileObserver.CLOSE_WRITE) > 0) {
if (isJellyBean) {
jbCache.add(path);
} else {
performYourWork(path);
}
} else if ((event & FileObserver.MOVED_TO) > 0 && isJellyBean && jbCache.contains(path)) {
performYourWork(path);
jbCache.remove(path);
}
}
You have to listen to both CLOSE_WRITE and MOVED_TO when you register the events you want to catch, obviously.
Although I starred your bug, I doubt Google will ever acknowledge it, as it looks like there could be some (disagreeable) reasoning behind the change. The Camera app is mostly a no-standard crap anyway (e.g.: fake DCIM standard compliance)
I'm looking for a way to do periodic bluetooth inquiry's from my Android Smartphone (HTC Desire with Android 4.0.1). The Device is rooted and it is a custom Rom installed so that I have full privileges.
I already wrote a shellscript, which uses hcitool, hciconfig and hcidump to do the inquiry's. That works fine so far, but it is a little to slow for me. I guess that's because of my dirty workaround of using an android-app to execute a shellscript from linux.
Although I got problems by killing the executed processes via my App.
So I'd like to find a way to inquire BT Devices from the Android API.
I found out that there is no possibility 'til now to do this, but i read about the bluez API which is possibly able to fit my requests.
Does anyone have any links, tips or advises for me?
I haven't found anything useful :/
Thanks in advance.
Edit (2012-09-28):
Okay, I think I'm a little closer to the solution now.
I downloaded the source of blueZ library from: bluez.org
Then i put the important files (hci.h, bluetooth.h, hcilib.h and their sourcefiles) into the jni folder of my android project and compiled them into my shared library.
I wrote a JNI Wrapper around the function
hci_inquiry(int dev_id, int len, int nrsp, const uint8_t *lap,inquiry_info **ii, long flags)
and followed the advises of this book to do my inquiry. Everything fine 'til here.
But when I start the inquiry, the function
dev_id = hci_get_route(NULL);
always returns -1, and I can't continue.
Bluetooth is activated, and i already tried to grant root access to my application. Not better :(
Anything I did wrong until here? Isn't it allowed to use BlueZ HCI commands from a android app?
I mean JBlueZ doesn't do anything else is it?
My native code looks like this:
inquiry_info *ii = NULL;
int max_rsp, num_rsp;
int dev_id, sock, len, flags;
int i;
char addr[19] = { 0 };
char name[248] = { 0 };
dev_id = hci_get_route(NULL);
if (dev_id < 0 ) {
LOGI("ERROR ON finding Device ID");
return;
}
sock = hci_open_dev( dev_id );
if (sock < 0) {
LOGI("ERROR ON opening socket");
return;
}
len = 8;
max_rsp = 255;
flags = IREQ_CACHE_FLUSH;
ii = (inquiry_info*)malloc(max_rsp * sizeof(inquiry_info));
num_rsp = hci_inquiry(dev_id, len, max_rsp, NULL, &ii, flags);
//##################################################################################
LOGI("INQUIRY Executed!!!");
(*env)->CallVoidMethod(env, obj, callBackID, num_rsp);
free(ii);
close(sock);
Thank you for any help.
I'm currently porting Gingerbread code to ICS. The communication between C and Java happens properly in Gingerbread. But the same thing crashes in ICS. Not able to figure out.
What are the major changes in ICS jni.?
My current problem,
1.Get the Class Instance and convert it into global reference and store it.
jclass myWrapperClass = (*env)->FindClass(env,"com/test/mypackage/Wrapper");
if (voipWrapperClass == NULL) {
// error - handling and returns
}
WrapperClass = (jclass)(*env)->NewGlobalRef(env,myWrapperClass);
2.From a JNI call the flow goes to below stack and returns callback to jni. From JNI to java the below function call
void response(void* ptr, int result){
JNIEnv *envPtr= NULL;
JavaVM* vmPtr= p_pdb->vm;
if ((*vmPtr)->GetEnv(vmPtr,(void**) &envPtr, JNI_VERSION_1_4) == JNI_EDETACHED) {
(*vmPtr)->AttachCurrentThread(vmPtr,(void**)&envPtr,NULL);
}
if (ptr->WrapperClass == NULL) {
// error- handling and return
}
RespMethodId = (*envPtr)->GetMethodID(envPtr,ptr->WrapperClass, "resp","(Z)V");
// this method is always 0 ... prev for gingerbread it returned a valid id..
}
Please give me a solution how to proceed further.?
I have found the solution finally. I used to compile my code in android code-base (2.3.3) but from ICS if you need to build a separate shared library use NDK and build a separate library. Else place the code in frameworks folder. This solves the problem. :)