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

Add copy/paste application for PenguinPeak project #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions penguinpeak/Android.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#
# Copyright (C) 2018 The Android Open Source Project
# Copyright (c) 2018 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
LOCAL_PATH := $(my-dir)
include $(call all-subdir-makefiles, $(LOCAL_PATH))
34 changes: 34 additions & 0 deletions penguinpeak/PenguinPeakUtils/Android.bp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//
// Copyright (C) 2008 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

// This makefile shows how to build a shared library and an activity that
// bundles the shared library and calls it using JNI.

android_app {
name: "PenguinPeakUtils",
srcs: ["**/*.java"],
// JNI library built from C++ source code
jni_libs: ["libVsocketClientImpl"],
optimize: {
enabled: false,
},
sdk_version: "current",
dex_preopt: {
enabled: false,
},
// To match the signature
certificate: "platform",
}
32 changes: 32 additions & 0 deletions penguinpeak/PenguinPeakUtils/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.intel.penguinpeakutils"
android:sharedUserId="android.uid.system">

<uses-sdk android:minSdkVersion="17" android:targetSdkVersion="30"/>

<!-- Need internet permission to create socket -->
<uses-permission android:name="android.permission.INTERNET"/>

<!-- TODO: update persistent and allowBackup to be true. -->
<application
android:persistent="false"
android:name=".PenguinPeakUtils"
android:allowBackup="false">
<activity
android:name=".MainActivity"
android:theme="@android:style/Theme.NoDisplay">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>

<service
android:exported="false"
android:name=".ClipboardService">
</service>

</application>

</manifest>
37 changes: 37 additions & 0 deletions penguinpeak/PenguinPeakUtils/jni/Android.bp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//
// Copyright (C) 2008 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

// This makefile supplies the rules for building a library of JNI code for
// use by our example of how to bundle a shared library with an APK.

cc_library_shared {
name: "libVsocketClientImpl",
// All of the source files that we will compile.
srcs: ["VsockClientImpl.cpp"],
// All of the shared libraries we link against.
// liblog is used to print trace log in C plus plus source code.
shared_libs: ["liblog"],
// No static libraries.
static_libs: [],
cflags: [
"-Wall",
"-Werror",
],
// We cannot use stl:"none" here to link libc++ dynamically because
// it caused "'iostream' file not found" build issue.
stl: "c++_static",
sdk_version: "current",
}
176 changes: 176 additions & 0 deletions penguinpeak/PenguinPeakUtils/jni/VsockClientImpl.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
#include <iostream>
#include <cstring>
#include <stdio.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <linux/vm_sockets.h>
#include <unistd.h>
#include <errno.h>

#include <jni.h>
#include <VsockClientImpl.h>

#define JVM_IO_INTR (-2)
#ifndef bufferFER_LEN
#define bufferFER_LEN 65536
#endif
#ifndef min
#define min(a, b) ((a) < (b) ? (a) : (b))
#endif

#define LOG_TAG "vsock"
#include <android/log.h>

#define ALOGV(...) __android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__)
#define ALOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)
#define ALOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
#define ALOGW(...) __android_log_print(ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__)
#define ALOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)


static const char *vsockClientImplPath = "com/intel/penguinpeakutils/VsockClientImpl";
static const char *vsockAddressPath = "com/intel/penguinpeakutils/VsockAddress";
static const char *javaConnException = "java/net/ConnectException";
static const char *javaIntrIOException = "java/io/InterruptedIOException";
static const char *sunConnResetException = "sun/net/ConnectionResetException";

JNIEXPORT void JNICALL Java_com_intel_penguinpeakutils_VsockClientImpl_socketCreate
(JNIEnv *env, jobject thisObject) {
int sock = socket(AF_VSOCK, SOCK_STREAM, 0);

jclass implement = env->FindClass(vsockClientImplPath);
jfieldID fdField = env->GetFieldID(implement, "fd", "I");
env->SetIntField(thisObject, fdField, sock);
}

JNIEXPORT void JNICALL Java_com_intel_penguinpeakutils_VsockClientImpl_connect
(JNIEnv *env, jobject thisObject, jobject addr) {
jclass implement = env->FindClass(vsockClientImplPath);
jfieldID fdField = env->GetFieldID(implement, "fd", "I");
int sock = (int)env->GetIntField(thisObject, fdField);

if (sock == -1) {
env->ThrowNew(env->FindClass(javaConnException), "vsock: Socket is closed");
return;
}

jclass vsockAddress = env->FindClass(vsockAddressPath);
jfieldID cidField = env->GetFieldID(vsockAddress, "cid", "I");
jfieldID portField = env->GetFieldID(vsockAddress, "port", "I");


struct sockaddr_vm sock_addr;
std::memset(&sock_addr, 0, sizeof(struct sockaddr_vm));
sock_addr.svm_family = AF_VSOCK;
sock_addr.svm_port = (int)env->GetIntField(addr, portField);
sock_addr.svm_cid = (int)env->GetIntField(addr, cidField);
int status = connect(sock, (struct sockaddr *) &sock_addr, sizeof(struct sockaddr_vm));
if (status != 0) {
if (errno == EALREADY) {
env->ThrowNew(env->FindClass(javaConnException),
("Connect failed: " + std::to_string(errno)).c_str());
}
}
}

JNIEXPORT void JNICALL Java_com_intel_penguinpeakutils_VsockClientImpl_close
(JNIEnv *env, jobject thisObject) {
jclass implement = env->FindClass(vsockClientImplPath);
jfieldID fdField = env->GetFieldID(implement, "fd", "I");
int s = (int)env->GetIntField(thisObject, fdField);

if (s == -1) {
env->ThrowNew(env->FindClass(javaConnException), "vsock close: Socket is already closed.");
return;
}

int status = close(s);

env->SetIntField(thisObject, fdField, -1);
if (status != 0) {
env->ThrowNew(env->FindClass(javaConnException),
("Close failed: " + std::to_string(errno)).c_str());
}
}

JNIEXPORT void JNICALL Java_com_intel_penguinpeakutils_VsockClientImpl_write
(JNIEnv * env, jobject thisObject, jbyteArray b, jint offset, jint len) {
jclass implement = env->FindClass(vsockClientImplPath);
jfieldID fdField = env->GetFieldID(implement, "fd", "I");
int s = (int)env->GetIntField(thisObject, fdField);

if (s == -1) {
env->ThrowNew(env->FindClass(javaConnException), "vsock write: Socket is already closed.");
return;
}

char buffer[bufferFER_LEN];
while(len > 0) {
int chunk_offset = 0;
int chunkLen = min(bufferFER_LEN, len);
int chunkWorkingLen = chunkLen;

env->GetByteArrayRegion(b, offset, chunkLen, (jbyte *)buffer);

while(chunkWorkingLen > 0) {
int n = (int)send(s, buffer + chunk_offset, len, 0);
if (n > 0) {
chunkWorkingLen -= n;
chunk_offset += n;
continue;
}
if (n == JVM_IO_INTR) {
env->ThrowNew(env->FindClass(javaIntrIOException), 0);
} else {
if (errno == ECONNRESET) {
env->ThrowNew(env->FindClass(sunConnResetException), "vsock write: Connection reset");
} else {
env->ThrowNew(env->FindClass(javaConnException), "vsock write: Write failed");
}
}
return;
}
len -= chunkLen;
offset += chunkLen;
}
}

JNIEXPORT jint JNICALL Java_com_intel_penguinpeakutils_VsockClientImpl_read
(JNIEnv * env, jobject thisObject, jbyteArray b, jint off, jint len) {
jint nread;

jclass implement = env->FindClass(vsockClientImplPath);
jfieldID fdField = env->GetFieldID(implement, "fd", "I");
int s = (int)env->GetIntField(thisObject, fdField);

if (s == -1) {
env->ThrowNew(env->FindClass(javaConnException), "vsock read: Socket is already closed");
return -1;
}

char *bufP = (char *)malloc((size_t)len);

nread = (jint) recv(s, bufP, len, 0);
nread = nread < len - 1 ? nread : len - 1;
/*
* Append EOF here to since Java inputstream read blocks utils:
* 1. Meet EOF
* 2. Data count reaches the count intended to be read.
*/
bufP[nread] = EOF;

if (nread <= 0) {
if (nread < 0) {
env->ThrowNew(env->FindClass(javaConnException),
("vsock read: Read failed with error no: " + std::to_string(errno)).c_str());
} else {
env->ThrowNew(env->FindClass(javaConnException),
("vsock read: Connection is closed by peer."));
}
} else {
env->SetByteArrayRegion(b, off, nread, (jbyte *)bufP);
}

free(bufP);
return nread;
}
53 changes: 53 additions & 0 deletions penguinpeak/PenguinPeakUtils/jni/VsockClientImpl.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading