From 710df01e6c14a4f379add6d07ccadd6ed2996c3f Mon Sep 17 00:00:00 2001 From: Andre Weber Date: Tue, 8 Oct 2024 09:35:49 +0200 Subject: [PATCH] Build Protobuf Files for App The following PR makes sure, that the proto file which is put inside the app/src/main/proto folder correctly generates the corresponding services. --- app/build.gradle.kts | 45 ++++++++++++++ .../java/com/example/service/CarService.kt | 22 +++++++ .../com/example/service/GrpcCarService.kt | 58 +++++++++++++++++++ app/src/{ => main}/proto/door.proto | 4 ++ 4 files changed, 129 insertions(+) create mode 100644 app/src/main/java/com/example/service/CarService.kt create mode 100644 app/src/main/java/com/example/service/GrpcCarService.kt rename app/src/{ => main}/proto/door.proto (93%) diff --git a/app/build.gradle.kts b/app/build.gradle.kts index d5c92b3..ac9466e 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -17,6 +17,7 @@ plugins { alias(libs.plugins.android.application) alias(libs.plugins.kotlin.android) + alias(libs.plugins.protobuf) } android { @@ -51,9 +52,53 @@ android { } } +protobuf { + protoc { + artifact = libs.protoc.asProvider().get().toString() + } + plugins { + create("java") { + artifact = libs.protoc.gen.grpc.java.get().toString() + } + create("grpc") { + artifact = libs.protoc.gen.grpc.java.get().toString() + } + create("grpckt") { + artifact = libs.protoc.gen.grpc.kotlin.get().toString() + ":jdk8@jar" + } + } + generateProtoTasks { + all().forEach { + it.builtins { + create("java") { + option("lite") + } + create("kotlin") { + option("lite") + } + } + it.plugins { + create("grpc") { + option("lite") + } + create("grpckt") { + option("lite") + } + } + } + } +} + dependencies { implementation(project(":sdk")) + implementation(libs.grpc.okhttp) + implementation(libs.grpc.protobuf.lite) + implementation(libs.grpc.stub) + implementation(libs.grpc.kotlin.stub) + + implementation(libs.protobuf.kotlin.lite) + implementation(libs.androidx.fragment.ktx) implementation(libs.androidx.car.app) diff --git a/app/src/main/java/com/example/service/CarService.kt b/app/src/main/java/com/example/service/CarService.kt new file mode 100644 index 0000000..975a5dd --- /dev/null +++ b/app/src/main/java/com/example/service/CarService.kt @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2024 Contributors to the Eclipse Foundation + * + * This program and the accompanying materials are made available under the + * terms of the Apache License, Version 2.0 which is available at + * https://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. + * + * SPDX-License-Identifier: Apache-2.0 + */ + +package com.example.service + +interface CarService { + fun lockDoor(): Boolean + fun unlockDoor(): Boolean +} diff --git a/app/src/main/java/com/example/service/GrpcCarService.kt b/app/src/main/java/com/example/service/GrpcCarService.kt new file mode 100644 index 0000000..8de24a4 --- /dev/null +++ b/app/src/main/java/com/example/service/GrpcCarService.kt @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2024 Contributors to the Eclipse Foundation + * + * This program and the accompanying materials are made available under the + * terms of the Apache License, Version 2.0 which is available at + * https://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. + * + * SPDX-License-Identifier: Apache-2.0 + */ + +package com.example.service + +import android.util.Log +import door.DoorGrpc +import door.DoorGrpc.DoorFutureStub +import door.DoorService +import io.grpc.Grpc +import io.grpc.InsecureChannelCredentials + +private const val TAG = "SampleServiceImpl" + +class GrpcCarService( + host: String, + port: Int, +) : CarService { + + private val doorService: DoorFutureStub + + init { + Log.i(TAG, "Connecting to gRPC service at $host:$port") + + val channelCredentials = InsecureChannelCredentials.create() + val channel = Grpc.newChannelBuilderForAddress(host, port, channelCredentials).build() + + doorService = DoorGrpc.newFutureStub(channel) + } + + // Door service + override fun lockDoor(): Boolean { + val request = DoorService.LockRequest.newBuilder().build() + val response = doorService.lock(request).get() // blocking call + Log.i(TAG, "lockDoor: Got response: " + response.getCode()) + return response.getCode() == DoorService.BCMReturnCode.BCM_RETURN_CODE_SUCCESS + } + + override fun unlockDoor(): Boolean { + val request = DoorService.UnlockRequest.newBuilder().build() + val response = doorService.unlock(request).get() // blocking call + Log.i(TAG, "unlockDoor: Got response: " + response.getCode()) + return response.getCode() == DoorService.BCMReturnCode.BCM_RETURN_CODE_SUCCESS + } +} diff --git a/app/src/proto/door.proto b/app/src/main/proto/door.proto similarity index 93% rename from app/src/proto/door.proto rename to app/src/main/proto/door.proto index ac12817..96e15c8 100644 --- a/app/src/proto/door.proto +++ b/app/src/main/proto/door.proto @@ -2,7 +2,11 @@ * Provides door control and status service, one atomic service for each door. */ syntax = "proto3"; + package door; + +option java_outer_classname = "DoorService"; + service Door { /* Unlock the door. */ rpc Unlock(UnlockRequest) returns (UnlockResponse);