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

added restart server on android tv server #13177

Merged
merged 6 commits into from
Jan 7, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import chip.setuppayload.DiscoveryCapability;
import chip.setuppayload.SetupPayload;
import chip.setuppayload.SetupPayloadParser;
import com.tcl.chip.chiptvserver.service.MatterServant;
import java.util.HashSet;

public class MainActivity extends AppCompatActivity {
Expand All @@ -23,6 +25,14 @@ protected void onCreate(Bundle savedInstanceState) {
mQrCodeImg = findViewById(R.id.qrCodeImg);
mQrCodeTxt = findViewById(R.id.qrCodeTxt);
mManualPairingCodeTxt = findViewById(R.id.manualPairingCodeTxt);
findViewById(R.id.resetBtn)
.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View view) {
MatterServant.get().restart();
}
});

// TODO: Get these parameters from PreferencesConfigurationManager
HashSet<DiscoveryCapability> discoveryCapabilities = new HashSet<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

public class MatterServant {

private ChipAppServer chipAppServer;

private MatterServant() {}

private static class SingletonHolder {
Expand Down Expand Up @@ -49,7 +51,12 @@ public void init(@NonNull Context context) {
new NsdManagerServiceResolver(applicationContext),
new ChipMdnsCallbackImpl());

ChipAppServer chipAppServer = new ChipAppServer();
chipAppServer = new ChipAppServer();
chipAppServer.startApp();
}

public void restart() {
chipAppServer.stopApp();
chipAppServer.startApp();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,14 @@
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />

<Button
android:id="@+id/resetBtn"
android:text="Reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/manualPairingCodeTxt"
android:layout_marginTop="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
8 changes: 8 additions & 0 deletions src/app/server/java/AndroidAppServerWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,11 @@ CHIP_ERROR ChipAndroidAppInit(void)
}
return err;
}

CHIP_ERROR ChipAndroidAppShutdown(void)
andy31415 marked this conversation as resolved.
Show resolved Hide resolved
{
chip::Server::GetInstance().Shutdown();
chip::Platform::MemoryShutdown();

return CHIP_NO_ERROR;
}
2 changes: 2 additions & 0 deletions src/app/server/java/AndroidAppServerWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

CHIP_ERROR ChipAndroidAppInit(void);

CHIP_ERROR ChipAndroidAppShutdown(void);

jint AndroidAppServerJNI_OnLoad(JavaVM * jvm, void * reserved);

void AndroidAppServerJNI_OnUnload(JavaVM * jvm, void * reserved);
7 changes: 7 additions & 0 deletions src/app/server/java/CHIPAppServer-JNI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <lib/core/CHIPError.h>
#include <lib/support/CHIPJNIError.h>
#include <lib/support/CHIPMem.h>
#include <lib/support/ChipThreadWork.h>
#include <lib/support/CodeUtils.h>
#include <lib/support/JniReferences.h>
#include <lib/support/JniTypeWrappers.h>
Expand Down Expand Up @@ -130,6 +131,12 @@ JNI_METHOD(jboolean, startApp)(JNIEnv * env, jobject self)
return JNI_TRUE;
}

JNI_METHOD(jboolean, stopApp)(JNIEnv * env, jobject self)
{
chip::ThreadWork::ChipMainThreadScheduleAndWait([&] { ChipAndroidAppShutdown(); });
andy31415 marked this conversation as resolved.
Show resolved Hide resolved
return JNI_TRUE;
}

void * IOThreadAppMain(void * arg)
{
JNIEnv * env;
Expand Down
2 changes: 2 additions & 0 deletions src/app/server/java/src/chip/appserver/ChipAppServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ public class ChipAppServer {
private static final String TAG = ChipAppServer.class.getSimpleName();

public native boolean startApp();

public native boolean stopApp();
}
2 changes: 2 additions & 0 deletions src/lib/support/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ static_library("support") {
if (current_os == "android") {
sources += [
"CHIPJNIError.h",
"ChipThreadWork.cpp",
"ChipThreadWork.h",
"JniReferences.cpp",
"JniReferences.h",
"JniTypeWrappers.h",
Expand Down
73 changes: 73 additions & 0 deletions src/lib/support/ChipThreadWork.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
*
* Copyright (c) 2021 Project CHIP Authors
*
* 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.
*/

#include "ChipThreadWork.h"

#ifdef __APPLE__
#include <dispatch/dispatch.h>
#else
#include <semaphore.h>
#endif

#include <platform/CHIPDeviceLayer.h>

namespace chip {
namespace ThreadWork {
namespace {

struct WorkData
{
WorkCallback callback;
#ifdef __APPLE__
bzbarsky-apple marked this conversation as resolved.
Show resolved Hide resolved
dispatch_semaphore_t done;

WorkData() { done = dispatch_semaphore_create(0); }
~WorkData() { dispatch_release(done); }
void Post() { dispatch_semaphore_signal(done); }
void Wait() { dispatch_semaphore_wait(done, DISPATCH_TIME_FOREVER); }
#else
sem_t done;

WorkData() { sem_init(&done, 0 /* shared */, 0); }
~WorkData() { sem_destroy(&done); }
void Post() { sem_post(&done); }
void Wait() { sem_wait(&done); }
#endif
};

void PerformWork(intptr_t arg)
{
WorkData * work = reinterpret_cast<WorkData *>(arg);

work->callback();
work->Post();
}

} // namespace

void ChipMainThreadScheduleAndWait(WorkCallback callback)
{
WorkData workdata;
workdata.callback = callback;

chip::DeviceLayer::PlatformMgr().ScheduleWork(PerformWork, reinterpret_cast<intptr_t>(&workdata));

workdata.Wait();
}

} // namespace ThreadWork
} // namespace chip
37 changes: 37 additions & 0 deletions src/lib/support/ChipThreadWork.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
*
* Copyright (c) 2021 Project CHIP Authors
*
* 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.
*/

#pragma once

#include <functional>

#include <lib/core/CHIPError.h>

namespace chip {
namespace ThreadWork {

using WorkCallback = std::function<void()>;

/// Schedules a task to be run on the CHIP main thread and waits for that
/// task to be complete.
///
/// Returns only once callback has been completely executed (callback
/// will not be running anymore when this returns).
void ChipMainThreadScheduleAndWait(WorkCallback callback);

} // namespace ThreadWork
} // namespace chip