Implementation of lockf() for android NDK - android

The Android NDK lacks the lockf() function.
While I was compiling CUPS with Android NDK, the error came of missing lockf().
Hence there is need to create function similar to lockf() for NDK.
Please help me in creating such a function.
Any help will be highly appreciated.
PS:I m a noob

This is how another Google produce handles it
// The lockf() function is not available on Android; we translate to flock().
#define F_LOCK LOCK_EX
#define F_ULOCK LOCK_UN
inline int lockf(int fd, int cmd, off_t ignored_len) {
return flock(fd, cmd);
}
https://src.chromium.org/svn/branches/1312/src/base/os_compat_android.h

Even if you implement the lockf() you'll still have some problems using it, because Android has a restricted permission management. Generally two processes won't both have read/write permission on a same directory. I mean, you have no directory to place this file to be locked.

Related

Can't remove an empty folder in C++

I want to remove an empty folder using remove() in C++ on Windows 7 but I can't. I tried rmdir() instead of remove() then the folder got removed!
Nevertheless, the reason why I don't use rmdir() is due to Android. In a library project for Android, I can't include "direct.h" header so can't use rmdir(), either. Unlike on Windows, the function remove() works well on Android. I don't understand why.
Anybody knows why this is happening?
Or any other functions which will work on both Windows and Android?
This is a pretty common problem when writing cross-platform programs.
Sometimes, a library can provide the abstraction you need. For example, Boost has a filesystem library that can enumerate files, manipulate directories, etc., on multiple platforms using the exact same code.
Also, there are usually symbols defined that allow you to detect which compiler is currently building your code. Even if there isn't one that does what you want, you can define your own.
Let's say you need to build your software for two different fictitional operating systems named FooOS and for BarOS. I'm going to invent two symbols, FOO_OS and BAR_OS. In my code, I can do something like this:
#ifdef FOO_OS
#include <foo_stuff.h>
#elseif BAR_OS
#include <bar_stuff.h>
#endif
void do_something()
{
#ifdef FOO_OS
do_it_this_way();
#elseif BAR_OS
do_it_that_way();
#endif
}
Now, we just need to either define FOO_OS or BAR_OS. This can be done through an IDE's project configuration or on the command line of the compiler. Use Google to find out about your particular situation, since you didn't include those details in your post.
There is a preprocessing step when you compile your code that makes a pass through the source, and applies these conditional statements. A following pass actually compiles the code. Here is some documentation about Visual Studio's preprocessor, for example.

Android C++ IPC using Boost.Interprocess - should it work?

Background:
I have working C++ code on linux that uses Boost IPC to access shared memory, and I want to port it to android. I downloaded and built the Boost-for-Android project found here: https://github.com/MysticTreeGames/Boost-for-Android.
Problem:
However when I try to create a boost named mutex like this:
boost::interprocess::named_mutex named_mtx(boost::interprocess::open_or_create, "my_mutex");
I get an exception saying "no such file or directory" with a native code 2.
Additional information:
When I searched for how to use shared memory on android, it looks ashmem and Binder are popular methods, and I can't find references to them at all in the ported Boost IPC code.
Questions:
What is the reason for the "no such file or directory" error?
Can someone confirm that the Boost-for-Android IPC part works?
By default Boost does not look for a common place where to share data on Android. It was not built this way. To make it work modify the file
/boost/interprocess/detail/os_file_functions.hpp
Find the following line an add /sdcard
const char *names[]={ "/sdcard", ......
After doing that use the library and give to your application Read/Write external storage permission.
You are good to go.
PD: Please be carefull. I have problems using Mutex and conditional variables in Android because the process was taking 100% the CPU.
I followed the way provided by #user3645767 but it didn't work. But I solved it by revision the file 'interprocess/detail/shared_dir_helpers.hpp'
line 109
to change the dir_path in get_shared_dir_root()
#elif defined __ANDROID__
dir_path="/data"
#else
dir_path="/tmp"

How setup libsvm in Android?

Im trying to setup libsvm in android to detect motion from accelerometer. I have no idea how to setup libsvm in android and how to use it. guys can you give clue for this ?
You do not setup libsvm, simply use the library wrapper for the language you are using to develop an app for android (Java I guess?). Wrapper is included in the official release. And it also includes the sample of usage of this particular library. There is nothing special here - if you know how to develop android app, then using additional library should not be a problem. If you do not know how to develop such an app - then starting from motion recognition is a bad idea. The same applies for ability to use SVM for anything. If you haven't ever used SVM it would be better to start with something simplier, like writing the "non mobile" version of the app and getting familiar with this model. Otherwise, probability of failing is quite big.
Sorry for my previous wrong answer format
Since the libsvm is written in C, you can easily wrap the code via JNI interface and use libsvm in Java.
A wrapper can be found in: https://github.com/yctung/AndroidLibSvm
For example, once you import this project in Android studio, you can call
jniSvmTrain(String options);
to make the svm training with the same interface of original libsvm.
If you look at the code, it is simply a wrapper of original "svm-train.c" in libsvm
#include "./libsvm/svm-train.h"
// helper function to be called in Java for making svm-train
extern "C" void Java_edu_umich_eecs_androidlibsvm_MainActivity_jniSvmTrain(JNIEnv *env, jobject obj, jstring cmdIn){
const char *cmd = env->GetStringUTFChars(cmdIn, 0);
debug("jniSvmTrain cmd = %s", cmd);
std::vector<char*> v;
// add dummy head to meet argv/command format
std::string cmdString = std::string("dummy ")+std::string(cmd);
cmdToArgv(cmdString, v);
// make svm train by libsvm
svmtrain::main(v.size(),&v[0]);
// free vector memory
for(int i=0;i<v.size();i++){
free(v[i]);
}
// free java object memory
env->ReleaseStringUTFChars(cmdIn, cmd);
}
"Setuping" I think you are asking to add the jar file the LIBSVM provide as a library to your android studio project. You can take a look here:
Android Studio: Add jar as library?
In the LIBSVM website you can download a zip file with the JAVA jar file inside and examples of usage.

Android native methods called from Java side

How can I get reach to the native methods called from Java side in Android? My problem is specifically related to AudioRecord class in Android Media package. I read the source code of AudioRecord.java. I found out that most of the jobs is performed by native methods, such as:
native_setup(...), native_start(...), native_stop(...), native_read_in_byte_array(...), native_read_in_direct_buffer(...)
I downloaded Android source code but I could not reach these methods. And I don't actually know the way to reach them. I seek for these methods in libraries I found in source code directories, but I couldn't success. If anybody may have an idea, I would be appreciative to hear. Thanks...
I think I found them. After using the Linux command
grep -r "native_read_in_direct_buffer" ./ANDROID_SOURCE/.*
I found the corresponding cpp files.
AudioRecord.cpp is located in: ~/ANDROID_SOURCE/frameworks/av/media/libmedia/ directory,
android_media_AudioRecord.cpp is located in ~/ANDROID_SOURCE/frameworks/base/core/jni directory.
I wanted to share it as a reference to other possible programmers willing to reach the same/similar source files.

Wrapping a c++ library using jni

I've successfully added a c++ library to the android 2.3.6 source tree.
Now i want to wrap my library in order to be used from java
I've searched a lot in the net about the steps to do so but almost all the examples i've found are hello world examples and simple ones...
From these examples i've understood that i must create a java file calling the native functions that i need with the prefix native ,then generate the headers and implement the c++ file.
But i don't see how can i call my library functions with this manner.
what is the connection between these headers and my function..?
i am really confused now and i dont know how to start
So, i would be thankfull if someone can tell me the step to wrap a c++ library(already integrated in the aosp)
This is quite a complex problem, and probably beyond the scope of a short answer here. The best thing to do, then, would probably be to guide you to some better documents than the ones you have found so far.
This series of blog posts seems particularly helpful: http://thebreakfastpost.com/2012/01/21/wrapping-a-c-library-with-jni-introduction/
You may also find a wrapper autogenerator helpful. There is one that I haven't tried here: http://www.teamdev.com/jniwrapper/features/
If I understood your problem:
Let's say you have a c++ library with this method
int cPlusPlusMethod{
// Your c++ implementation
}
Basically what you have to do is:
Create the java methods that you need
native int javaMethod();
Follow the steps that you saw in the links you found in order to create a c++ header, you will have something like:
JNIEXPORT jint JNICALL Java_yourPackageName_yourJavaClass_javaMethod(JNIEnv *);
Now you have a c++ header with the declarations of all the methods that you need. Write a c++ file that implements that header and copy-paste the c++ implementation of the library.
JNIEXPORT jint JNICALL Java_yourPackageName_yourJavaClass_javaMethod(JNIEnv *env){
// Your c++ implementation
}
(You will have to include all the needed imports and put the other files of the lib in the 'jni' folder.)
Execute ndk-build from your android project root folder, and that's it.
Maybe this is not the smartest way to do it but at least for me it worked.
Obviously this works if you have the library source files, if you are speaking about a static lib, then I'm not able to help you.

Categories

Resources