Skip to content

Commit

Permalink
added restart server on android tv server (#13177)
Browse files Browse the repository at this point in the history
* added restart server on android tv server

* fix restyled-io and ci errors

* ChipThreadWork can only work on android & linux

* remove chip thread work on darwin because there is no "non-main" CHIP thread on darwin

* move ChipThreadWork to server/java folder as bzbarsky-apple's suggestion because is it not suit for all platforms

* change ChipAndroidAppShutdown to void and remove no mean [&]
  • Loading branch information
xylophone21 authored and pull[bot] committed Feb 24, 2022
1 parent 78646df commit 1060100
Show file tree
Hide file tree
Showing 10 changed files with 142 additions and 1 deletion.
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>
6 changes: 6 additions & 0 deletions src/app/server/java/AndroidAppServerWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,9 @@ CHIP_ERROR ChipAndroidAppInit(void)
}
return err;
}

void ChipAndroidAppShutdown(void)
{
chip::Server::GetInstance().Shutdown();
chip::Platform::MemoryShutdown();
}
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);

void ChipAndroidAppShutdown(void);

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

void AndroidAppServerJNI_OnUnload(JavaVM * jvm, void * reserved);
2 changes: 2 additions & 0 deletions src/app/server/java/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ static_library("jni") {
"AndroidAppServerWrapper.cpp",
"AndroidAppServerWrapper.h",
"CHIPAppServer-JNI.cpp",
"ChipThreadWork.cpp",
"ChipThreadWork.h",
]

deps = [
Expand Down
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 @@ -22,6 +22,7 @@
*
*/
#include "AndroidAppServerWrapper.h"
#include "ChipThreadWork.h"
#include <jni.h>
#include <lib/core/CHIPError.h>
#include <lib/support/CHIPJNIError.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(); });
return JNI_TRUE;
}

void * IOThreadAppMain(void * arg)
{
JNIEnv * env;
Expand Down
58 changes: 58 additions & 0 deletions src/app/server/java/ChipThreadWork.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
*
* 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"
#include <platform/CHIPDeviceLayer.h>
#include <semaphore.h>

namespace chip {
namespace ThreadWork {
namespace {

struct WorkData
{
WorkCallback callback;
sem_t done;

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

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/app/server/java/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
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();
}

0 comments on commit 1060100

Please sign in to comment.