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

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"

Related

Copying with memcpy externally?

Is it possible to copy a function to an external application's shared library in memory? If so, how? I'm trying to achieve external hooking by making a trampoline hook externally.
This require very deep understanding of the binary formats used by the operating system. Not all code is relocatable, your code must be compiled with -fPIC for this to work for sure. You will need also to resolve manually all external symbols. In fact, you will have reimplement parts of the ELF loader. It is possible but definitely not trivial and very machine dependant.
Also you will have to find a way around the new execution restriction - nowadays most OSs have various protections against writing in executable memory regions.

Interact with C++ program from android using parcelable class

Basically, I have a C++ program that finds the sum of two numbers given. I need to provide the two numbers to the C++ program as input using my android app and then display the result in my android app. I guess I need to use parcelable class. Can someone please tell me the steps to be followed?
Edit: I forgot to mention that the C++ program that I intend to communicate with is an executable program (sum.exe)
To run a C++ executable on Android, you can use something like Runtime.exec("sum 1 2"). There are a lot of tutorials, e.g. https://www.mkyong.com/java/how-to-execute-shell-command-from-java/. The output (stdout and stderr) can be parsed, too. A more sophisticated way is to use ProcessBuilder, but the idea is the same.
If you want your executable to keep running in background, and send the numbers to crunch once in a while, you can either use input pipe, or some IPC protocol. Shared memory works well, see e.g. How to use Shared Memory (IPC) in Android.
You can use JNI code, take a look here:
https://github.com/mkowsiak/jnicookbook/tree/master/recipes/recipeNo025
where you can find super simple code with C++ being called via JNI wrapper from Java.

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.

Free Fuzzy Logic Library, can't parse a file on android

I am developing an application for android/iOS/windows using c++ code for the core logic. The application uses the free fuzzy logic library and it works perfectly for windows mobile, iOS and on my local Ubuntu machine, but it doesn't quite work under android.
The application reads a .fcl file from the sd card and then parses it using the free fuzzy logic library parser. The problem is, that the parser gets stuck at random stages of parsing.
Some notes to my project settings:
I enabled the Android read/write permissions for the sdcard in the manifest.xml.
The code I am trying to run is the basic example from the free fuzzy logic library website.
I am using the stlport_static library for stl support and the -frtti compiler flag.
My question is: Am I missing something android specific, like file encoding or some permissions I didn't set?
Some notes I thought about:
File compression should not be an issues, because, to my knowledge, files on the SD card are not compressed and I can parse the file partially.
Using other fuzzy logic libraries is out of the option, because I can't use GPL licenced libraries. The only other library I found didn't hat a manual / how to and couldn't parse the fcl standard.
The free fuzzy logic library uses a lot of wchar_t's whitch could be an issue.
Thank you for your time and hopefully for some help ;)
Ok after plowing through some android manuals and some Google abuse I found the problem. Currently Android doesn't support the wchar_t type. Well you can use it, but the results will not be the same as on any other operating system.
By changing all the wchar_t and wstring types in the free fuzzy logic library to their corresponding char and string types I was able to make the parser work. Well sort of, there are still some sleight inconsistencies, but nothing i can't handle ;).
Conclusion: Don't use wide characters in android c++ Programs.
Thank you for your time & help

Replace string in a function at runtime Android NDK

I have an external compiled static C++ library that I'm using in my android application. This library is reading a file. I want to know if there is a way I can "redirect" the function that's reading the file so that it reads another file.
So if it does:
fopen("myfile.txt", "rb");
I want to intercept it and to do this instead:
fopen("myotherfile.txt", "rb");
In Objective-C I use MethodSwizzling. Is there something similar I can do in C++ or the android NDK?
Short of editing the binary (with uncertain results), your best option is to use a symlink... if you're just doing it for development purposes, you could use adb shell into your test device to create the symlink.
http://developer.android.com/guide/developing/tools/adb.html#issuingcommands
How about contact the author of the library and ask them to introduce a parameter? Having hard-coded file paths is a lousy design anyway, the library will be better off.

Categories

Resources