Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Use "D-Bus over TCP/IP over Ethernet over BLE" for communicating with the watch #216

Draft
wants to merge 10 commits into
base: master
Choose a base branch
from
6 changes: 2 additions & 4 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ android {
versionName = "0.29"
ndk.abiFilters.clear()
ndk.abiFilters.add("arm64-v8a")
ndk.abiFilters.add("armeabi-v7a")
ndk.abiFilters.add("x86")
ndk.abiFilters.add("x86_64")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why introduce them in the previous commit to drop them now ?

externalNativeBuild {
cmake {
cppFlags += ""
Expand All @@ -42,7 +39,7 @@ android {
srcDir("src/main/lib/powerampapi/poweramp_api_lib/res/")
}
jniLibs {
srcDir("src/main/cpp/lib")
I-asked marked this conversation as resolved.
Show resolved Hide resolved
srcDir("/work/android-root/lib")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose this is not meant to be kept ?

}
}
}
Expand Down Expand Up @@ -82,4 +79,5 @@ dependencies {
implementation("org.osmdroid:osmdroid-android:6.1.16")
implementation("no.nordicsemi.android.support.v18:scanner:1.6.0")
implementation("no.nordicsemi.android:ble:2.7.2")
implementation("com.google.guava:guava:33.1.0-android")
}
11 changes: 11 additions & 0 deletions app/src/main/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@ project("sync")

add_subdirectory(libslirp)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's ship libslirp as a git submodule so we don't have to copy paste files across repositories and we benefit from libslirp's commit log etc...


find_program(BASH bash)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's also use submodules to integrate this cgi. It would make it a lot easier to spot backdoors hidden in it while adding it :p

set(ENV{ANDROID_NDK_HOME} ${CMAKE_ANDROID_NDK})
set(ENV{ABI} ${CMAKE_ANDROID_ARCH_ABI})
execute_process(COMMAND ${BASH} ${CMAKE_CURRENT_SOURCE_DIR}/build_depends.sh)

link_directories(/tmp/android-root/lib/${CMAKE_ANDROID_ARCH_ABI})
include_directories(/tmp/android-root/include)

set(ENV{PKG_CONFIG_PATH} /tmp/android-root/lib/${CMAKE_ANDROID_ARCH_ABI}/pkgconfig)

# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
Expand Down
133 changes: 133 additions & 0 deletions app/src/main/cpp/build_depends.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
#!/usr/bin/env bash
#
# AsteroidOSSync
# Copyright (c) 2024 AsteroidOS
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
set -Eeo pipefail
ANDROID_NDK_HOME=${ANDROID_NDK_HOME:?please supply a valid \$ANDROID_SDK_HOME}
ABI=${ABI:?please supply a valid android \$ABI}
case "${ABI}" in
arm64-v8a)
LINUX_ABI=aarch64
;;
armeabi-v7a)
LINUX_ABI=arm
;;
x86_64)
LINUX_ABI=x86_64
;;
x86)
LINUX_ABI=i686-pc
;;
*)
exit 1
;;
esac
SYSROOT=${ANDROID_NDK_HOME}/toolchains/llvm/prebuilt/linux-x86_64/sysroot

>&2 echo "Android NDK in ${ANDROID_NDK_HOME}"
export PATH=$PATH:${ANDROID_NDK_HOME}/toolchains/llvm/prebuilt/linux-x86_64/bin

PREFIX=${PREFIX:-/tmp/android-root/}

GLIB_VERSION=${GLIB_VERSION:-2.80.0}
GLIB_URL=https://download.gnome.org/sources/glib/${GLIB_VERSION%.*}/glib-${GLIB_VERSION}.tar.xz
GLIB_CACHE=${XDG_CACHE_DIR:-/tmp}/glib-${GLIB_VERSION}.tar.xz

LIBICONV_VERSION=${LIBICONV_VERSION:-1.17}
LIBICONV_URL=https://ftp.gnu.org/pub/gnu/libiconv/libiconv-${LIBICONV_VERSION}.tar.gz
LIBICONV_CACHE=${XDG_CACHE_DIR:-/tmp}/libiconv-${LIBICONV_VERSION}.tar.gz
export CFLAGS=--sysroot="${SYSROOT}"
export CPPFLAGS=--sysroot="${SYSROOT}"
export CC=${LINUX_ABI}-linux-android21-clang
export CXX=${LINUX_ABI}-linux-android21-clang++
export AR=llvm-ar
export RANLIB=llvm-ranlib

pushd "$(mktemp -d)"

[[ ! -f "${LIBICONV_CACHE}" ]] \
&& wget -O "${LIBICONV_CACHE}" "${LIBICONV_URL}"
bsdtar --strip-components=1 -xf "${LIBICONV_CACHE}"

mkdir -p build
pushd build

../configure --host=${LINUX_ABI}-linux-android --with-sysroot="${SYSROOT}" --prefix="${PREFIX}" --libdir="${PREFIX}/lib/${ABI}"
make -j14
make install

popd # build

popd

pushd "$(mktemp -d)"

[[ ! -f "${GLIB_CACHE}" ]] \
&& wget -O "${GLIB_CACHE}" "${GLIB_URL}"
bsdtar --strip-components=1 -xf "${GLIB_CACHE}"

>&2 echo "Will build GLib"

_CROSS_FILE=$(mktemp)
>&2 echo "Will setup cross"
cat <<EOF. >"${_CROSS_FILE}"
[built-in options]
c_args = ['-I${PREFIX}/include']
c_link_args = ['-L${PREFIX}/lib/${ABI}']

[constants]
arch = '${LINUX_ABI}-linux-android'

[binaries]
ar = 'llvm-ar'
c = '${LINUX_ABI}-linux-android21-clang'
as = [c]
cpp = '${LINUX_ABI}-linux-android21-clang++'
ranlib = 'llvm-ranlib'
strip = 'llvm-strip'
pkgconfig = '/usr/bin/pkg-config'
cmake = '/usr/bin/cmake'

[properties]
sys_root = '${ANDROID_NDK_HOME}/toolchains/llvm/prebuilt/linux-x86_64/sysroot'
pkg_config_libdir = '${PREFIX}/lib/${ABI}/pkgconfig'

[host_machine]
system = 'android'
cpu_family = '${LINUX_ABI}'
cpu = '${LINUX_ABI}'
endian = 'little'
EOF.

patch <<EOF. ||:
--- meson.build 2024-03-07 22:35:05.000000000 +0100
+++ meson.build 2024-04-27 11:44:38.569868768 +0200
@@ -2170 +2170 @@
- libiconv = dependency('iconv')
+ libiconv = [cc.find_library('iconv', required : true, dirs : ['/work/android-root/lib'])]
EOF.


>&2 echo "Will configure in ${PWD}/_builddir/"
>&2 meson setup ./_builddir/ ./ --cross-file="${_CROSS_FILE}" --prefix="${PREFIX}" --libdir="lib/${ABI}"
>&2 echo "Will build"
>&2 ninja -C ./_builddir/
>&2 echo "Will install"
>&2 ninja -C ./_builddir/ install
>&2 echo "All depends ready"

popd
32 changes: 30 additions & 2 deletions app/src/main/cpp/sync.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <jni.h>
#include <stdlib.h>
//#include <arpa/inet.h>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No commented code


#include "libslirp/src/libvdeslirp.h"

Expand Down Expand Up @@ -46,14 +47,14 @@ JNIEXPORT void JNICALL Java_org_asteroidos_sync_connectivity_SlirpService_finali
env->SetLongField(thisObject, fid, 0L);
}

JNIEXPORT long JNICALL Java_org_asteroidos_sync_connectivity_SlirpService_vdeRecv
JNIEXPORT jlong JNICALL Java_org_asteroidos_sync_connectivity_SlirpService_vdeRecv
(JNIEnv* env, jobject thisObject, jobject dbb, jlong offset, jlong count) {

void *buf = reinterpret_cast<char *>(env->GetDirectBufferAddress(dbb)) + offset;
return vdeslirp_recv(GET_MYSLIRP(env, thisObject), buf, count);
}

JNIEXPORT long JNICALL Java_org_asteroidos_sync_connectivity_SlirpService_vdeSend
JNIEXPORT jlong JNICALL Java_org_asteroidos_sync_connectivity_SlirpService_vdeSend
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this was wrong in the previous commit, let's squash it into the previous commit

(JNIEnv* env, jobject thisObject, jobject dbb, jlong offset, jlong count) {

void *buf = reinterpret_cast<char *>(env->GetDirectBufferAddress(dbb)) + offset;
Expand All @@ -75,4 +76,31 @@ JNIEXPORT jobject JNICALL Java_org_asteroidos_sync_connectivity_SlirpService_get
return ret;
}

JNIEXPORT jint JNICALL Java_org_asteroidos_sync_connectivity_SlirpService_vdeAddUnixFwd
(JNIEnv* env, jobject thisObject, jstring path, jstring ip, jint port) {
const char *c_path = env->GetStringUTFChars(path, nullptr);
const char *c_ip = env->GetStringUTFChars(ip, nullptr);

struct in_addr addr{ inet_addr(c_ip) };
int rv = vdeslirp_add_unixfwd(GET_MYSLIRP(env, thisObject), const_cast<char *>(c_path), &addr, port);

env->ReleaseStringUTFChars(path, c_path);
env->ReleaseStringUTFChars(ip, c_ip);

return rv;
}

JNIEXPORT jint JNICALL Java_org_asteroidos_sync_connectivity_SlirpService_vdeAddFwd
(JNIEnv* env, jobject thisObject, jboolean udp, jstring hostip, jint hostport, jstring ip, jint port) {
const char *c_hostip = env->GetStringUTFChars(hostip, nullptr);
const char *c_ip = env->GetStringUTFChars(ip, nullptr);

int rv = vdeslirp_add_fwd(GET_MYSLIRP(env, thisObject), udp, (struct in_addr){inet_addr(c_hostip) }, hostport, (struct in_addr){inet_addr(c_ip) }, port);

env->ReleaseStringUTFChars(hostip, c_hostip);
env->ReleaseStringUTFChars(ip, c_ip);

return rv;
}

}
Loading