Skip to content

Commit

Permalink
Build Protobuf Files for App
Browse files Browse the repository at this point in the history
The following PR makes sure, that the proto file which is put inside
the app/src/main/proto folder correctly generates the corresponding
services.
  • Loading branch information
wba2hi committed Oct 8, 2024
1 parent 758c889 commit 710df01
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 0 deletions.
45 changes: 45 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
plugins {
alias(libs.plugins.android.application)
alias(libs.plugins.kotlin.android)
alias(libs.plugins.protobuf)
}

android {
Expand Down Expand Up @@ -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)
Expand Down
22 changes: 22 additions & 0 deletions app/src/main/java/com/example/service/CarService.kt
Original file line number Diff line number Diff line change
@@ -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
}
58 changes: 58 additions & 0 deletions app/src/main/java/com/example/service/GrpcCarService.kt
Original file line number Diff line number Diff line change
@@ -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
}
}
4 changes: 4 additions & 0 deletions app/src/proto/door.proto → app/src/main/proto/door.proto
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 710df01

Please sign in to comment.