Skip to content

Commit

Permalink
Write netd hal implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
erfanoabdi committed May 29, 2020
1 parent a2c7246 commit 8f83e53
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 50 deletions.
36 changes: 23 additions & 13 deletions Android.bp
Original file line number Diff line number Diff line change
@@ -1,21 +1,31 @@
cc_library_shared {
// FIXME: this should only be -impl for a passthrough hal.
// In most cases, to convert this to a binderized implementation, you should:
// - change '-impl' to '-service' here and make it a cc_binary instead of a
// cc_library_shared.
// - add a *.rc file for this module.
// - delete HIDL_FETCH_I* functions.
// - call configureRpcThreadpool and registerAsService on the instance.
// You may also want to append '-impl/-service' with a specific identifier like
// '-vendor' or '-<hardware identifier>' etc to distinguish it.
name: "[email protected]",
owner: "halium",
//
// Copyright 2020 UBports foundation
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation; version 3.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// Author: Erfan Abdi <[email protected]>

cc_binary {
name: "[email protected]",
defaults: ["hidl_defaults"],
relative_install_path: "hw",
proprietary: true,
init_rc: ["[email protected]"],
srcs: [
"Netd.cpp",
"service.cpp"
],
shared_libs: [
"libbase",
"libhidlbase",
"libhidltransport",
"libutils",
Expand Down
66 changes: 45 additions & 21 deletions Netd.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
/*
* Copyright 2020 UBports foundation
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Author: Erfan Abdi <[email protected]>
*/

#define LOG_TAG "[email protected]"

#include <android-base/logging.h>

#include "Netd.h"

namespace android {
Expand All @@ -10,53 +32,55 @@ namespace implementation {
// Methods from ::android::system::net::netd::V1_0::INetd follow.
Return<void> Netd::createOemNetwork(createOemNetwork_cb _hidl_cb) {
// TODO implement
LOG(INFO) << __FUNCTION__ << " Called";
_hidl_cb(1, 0, StatusCode::OK);
return Void();
}

Return<::android::system::net::netd::V1_0::INetd::StatusCode> Netd::destroyOemNetwork(uint64_t networkHandle) {
Return<StatusCode> Netd::destroyOemNetwork(uint64_t) {
// TODO implement
return ::android::system::net::netd::V1_0::INetd::StatusCode {};
LOG(INFO) << __FUNCTION__ << " Called";
return StatusCode::OK;
}


// Methods from ::android::system::net::netd::V1_1::INetd follow.
Return<::android::system::net::netd::V1_0::INetd::StatusCode> Netd::addRouteToOemNetwork(uint64_t networkHandle, const hidl_string& ifname, const hidl_string& destination, const hidl_string& nexthop) {
Return<StatusCode> Netd::addRouteToOemNetwork(uint64_t, const hidl_string&, const hidl_string&, const hidl_string&) {
// TODO implement
return ::android::system::net::netd::V1_0::INetd::StatusCode {};
LOG(INFO) << __FUNCTION__ << " Called";
return StatusCode::OK;
}

Return<::android::system::net::netd::V1_0::INetd::StatusCode> Netd::removeRouteFromOemNetwork(uint64_t networkHandle, const hidl_string& ifname, const hidl_string& destination, const hidl_string& nexthop) {
Return<StatusCode> Netd::removeRouteFromOemNetwork(uint64_t, const hidl_string&, const hidl_string&, const hidl_string&) {
// TODO implement
return ::android::system::net::netd::V1_0::INetd::StatusCode {};
LOG(INFO) << __FUNCTION__ << " Called";
return StatusCode::OK;
}

Return<::android::system::net::netd::V1_0::INetd::StatusCode> Netd::addInterfaceToOemNetwork(uint64_t networkHandle, const hidl_string& ifname) {
Return<StatusCode> Netd::addInterfaceToOemNetwork(uint64_t, const hidl_string&) {
// TODO implement
return ::android::system::net::netd::V1_0::INetd::StatusCode {};
LOG(INFO) << __FUNCTION__ << " Called";
return StatusCode::OK;
}

Return<::android::system::net::netd::V1_0::INetd::StatusCode> Netd::removeInterfaceFromOemNetwork(uint64_t networkHandle, const hidl_string& ifname) {
Return<StatusCode> Netd::removeInterfaceFromOemNetwork(uint64_t, const hidl_string&) {
// TODO implement
return ::android::system::net::netd::V1_0::INetd::StatusCode {};
LOG(INFO) << __FUNCTION__ << " Called";
return StatusCode::OK;
}

Return<::android::system::net::netd::V1_0::INetd::StatusCode> Netd::setIpForwardEnable(bool enable) {
Return<StatusCode> Netd::setIpForwardEnable(bool) {
// TODO implement
return ::android::system::net::netd::V1_0::INetd::StatusCode {};
LOG(INFO) << __FUNCTION__ << " Called";
return StatusCode::OK;
}

Return<::android::system::net::netd::V1_0::INetd::StatusCode> Netd::setForwardingBetweenInterfaces(const hidl_string& inputIfName, const hidl_string& outputIfName, bool enable) {
Return<StatusCode> Netd::setForwardingBetweenInterfaces(const hidl_string&, const hidl_string&, bool) {
// TODO implement
return ::android::system::net::netd::V1_0::INetd::StatusCode {};
LOG(INFO) << __FUNCTION__ << " Called";
return StatusCode::OK;
}


// Methods from ::android::hidl::base::V1_0::IBase follow.

//INetd* HIDL_FETCH_INetd(const char* /* name */) {
//return new Netd();
//}
//
} // namespace implementation
} // namespace V1_1
} // namespace netd
Expand Down
43 changes: 27 additions & 16 deletions Netd.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
/*
* Copyright 2020 UBports foundation
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Author: Erfan Abdi <[email protected]>
*/

#ifndef ANDROID_SYSTEM_NET_NETD_V1_1_NETD_H
#define ANDROID_SYSTEM_NET_NETD_V1_1_NETD_H

#include <android/system/net/netd/1.1/INetd.h>
#include <hidl/MQDescriptor.h>
#include <hidl/Status.h>

namespace android {
Expand All @@ -12,34 +29,28 @@ namespace netd {
namespace V1_1 {
namespace implementation {

using ::android::hardware::hidl_array;
using ::android::hardware::hidl_memory;
using ::android::hardware::hidl_string;
using ::android::hardware::hidl_vec;
using ::android::hardware::Return;
using ::android::hardware::Void;
using ::android::sp;

using StatusCode = android::system::net::netd::V1_1::INetd::StatusCode;

struct Netd : public INetd {
// Methods from ::android::system::net::netd::V1_0::INetd follow.
Return<void> createOemNetwork(createOemNetwork_cb _hidl_cb) override;
Return<::android::system::net::netd::V1_0::INetd::StatusCode> destroyOemNetwork(uint64_t networkHandle) override;
Return<StatusCode> destroyOemNetwork(uint64_t networkHandle) override;

// Methods from ::android::system::net::netd::V1_1::INetd follow.
Return<::android::system::net::netd::V1_0::INetd::StatusCode> addRouteToOemNetwork(uint64_t networkHandle, const hidl_string& ifname, const hidl_string& destination, const hidl_string& nexthop) override;
Return<::android::system::net::netd::V1_0::INetd::StatusCode> removeRouteFromOemNetwork(uint64_t networkHandle, const hidl_string& ifname, const hidl_string& destination, const hidl_string& nexthop) override;
Return<::android::system::net::netd::V1_0::INetd::StatusCode> addInterfaceToOemNetwork(uint64_t networkHandle, const hidl_string& ifname) override;
Return<::android::system::net::netd::V1_0::INetd::StatusCode> removeInterfaceFromOemNetwork(uint64_t networkHandle, const hidl_string& ifname) override;
Return<::android::system::net::netd::V1_0::INetd::StatusCode> setIpForwardEnable(bool enable) override;
Return<::android::system::net::netd::V1_0::INetd::StatusCode> setForwardingBetweenInterfaces(const hidl_string& inputIfName, const hidl_string& outputIfName, bool enable) override;

// Methods from ::android::hidl::base::V1_0::IBase follow.

Return<StatusCode> addRouteToOemNetwork(uint64_t networkHandle, const hidl_string& ifname, const hidl_string& destination, const hidl_string& nexthop) override;
Return<StatusCode> removeRouteFromOemNetwork(uint64_t networkHandle, const hidl_string& ifname, const hidl_string& destination, const hidl_string& nexthop) override;
Return<StatusCode> addInterfaceToOemNetwork(uint64_t networkHandle, const hidl_string& ifname) override;
Return<StatusCode> removeInterfaceFromOemNetwork(uint64_t networkHandle, const hidl_string& ifname) override;
Return<StatusCode> setIpForwardEnable(bool enable) override;
Return<StatusCode> setForwardingBetweenInterfaces(const hidl_string& inputIfName, const hidl_string& outputIfName, bool enable) override;
};

// FIXME: most likely delete, this is only for passthrough implementations
// extern "C" INetd* HIDL_FETCH_INetd(const char* name);

} // namespace implementation
} // namespace V1_1
} // namespace netd
Expand Down
4 changes: 4 additions & 0 deletions [email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
service netd-hal-1-1-stub /system/bin/hw/[email protected]
class hal
user system
group system
65 changes: 65 additions & 0 deletions service.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright 2020 UBports foundation
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Author: Erfan Abdi <[email protected]>
*/

#define LOG_TAG "[email protected]"

#include <android-base/logging.h>
#include <hidl/HidlTransportSupport.h>
#include "Netd.h"

using ::android::OK;
using ::android::status_t;

// libhwbinder:
using ::android::hardware::configureRpcThreadpool;
using ::android::hardware::joinRpcThreadpool;

// Generated HIDL files:
using ::android::system::net::netd::V1_1::INetd;
using ::android::system::net::netd::V1_1::implementation::Netd;

static int shutdown() {
LOG(ERROR) << "Netd Service is shutting down.";
return 1;
}

int main(int /* argc */, char** /* argv */) {
status_t status;
android::sp<INetd> service = nullptr;

LOG(INFO) << "Netd HAL Service Stub 1.1 starting...";

service = new Netd();
if (service == nullptr) {
LOG(ERROR) << "Error creating an instance of Netd HAL. Exiting...";
return shutdown();
}

configureRpcThreadpool(1, true /* callerWillJoin */);

status = service->registerAsService();
if (status != OK) {
LOG(ERROR) << "Could not register service for Netd HAL (" << status << ")";
return shutdown();
}

LOG(INFO) << "Netd Service started successfully.";
joinRpcThreadpool();
// We should not get past the joinRpcThreadpool().
return shutdown();
}

0 comments on commit 8f83e53

Please sign in to comment.