mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-16 15:36:12 -04:00
WIP: wifi direct support. merged from upstream
This commit is contained in:
@@ -29,7 +29,7 @@
|
||||
#include "internal/platform/implementation/input_file.h"
|
||||
#include "internal/platform/implementation/linux/atomic_boolean.h"
|
||||
#include "internal/platform/implementation/linux/atomic_uint32.h"
|
||||
//#include "internal/platform/implementation/linux/ble_v2_medium.h"
|
||||
// #include "internal/platform/implementation/linux/ble_v2_medium.h"
|
||||
#include "internal/platform/implementation/linux/bluetooth_adapter.h"
|
||||
#include "internal/platform/implementation/linux/bluetooth_classic_medium.h"
|
||||
#include "internal/platform/implementation/linux/bluez.h"
|
||||
@@ -40,7 +40,7 @@
|
||||
#include "internal/platform/implementation/linux/preferences_manager.h"
|
||||
#include "internal/platform/implementation/linux/submittable_executor.h"
|
||||
#include "internal/platform/implementation/linux/timer.h"
|
||||
// #include "internal/platform/implementation/linux/wifi_direct.h"
|
||||
#include "internal/platform/implementation/linux/wifi_direct.h"
|
||||
// #include "internal/platform/implementation/linux/wifi_hotspot.h"
|
||||
#include "internal/platform/implementation/linux/wifi_lan.h"
|
||||
// #include "internal/platform/implementation/linux/wifi_medium.h"
|
||||
@@ -56,22 +56,22 @@
|
||||
#include "internal/platform/implementation/wifi_lan.h"
|
||||
#include "internal/platform/payload_id.h"
|
||||
#include "scheduled_executor.h"
|
||||
// #include "wifi_direct.h"
|
||||
#include "wifi_direct.h"
|
||||
#include "wifi_hotspot.h"
|
||||
|
||||
namespace nearby {
|
||||
namespace api {
|
||||
std::string ImplementationPlatform::GetCustomSavePath(
|
||||
const std::string &parent_folder, const std::string &file_name) {
|
||||
const std::string& parent_folder, const std::string& file_name) {
|
||||
auto fs = std::filesystem::path(parent_folder);
|
||||
return (fs / file_name).string();
|
||||
}
|
||||
|
||||
std::string ImplementationPlatform::GetDownloadPath(
|
||||
const std::string &parent_folder, const std::string &file_name) {
|
||||
const std::string& parent_folder, const std::string& file_name) {
|
||||
std::filesystem::path downloads;
|
||||
const char* download_dir = getenv("XDG_DOWNLOAD_DIR");
|
||||
|
||||
|
||||
if (download_dir != nullptr) {
|
||||
downloads = std::filesystem::path(download_dir);
|
||||
} else {
|
||||
@@ -83,16 +83,17 @@ std::string ImplementationPlatform::GetDownloadPath(
|
||||
downloads = "/tmp/Downloads";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return (downloads / std::filesystem::path(parent_folder).filename() /
|
||||
std::filesystem::path(file_name).filename()).string();
|
||||
std::filesystem::path(file_name).filename())
|
||||
.string();
|
||||
}
|
||||
|
||||
std::string ImplementationPlatform::GetDownloadPath(
|
||||
const std::string &file_name) {
|
||||
const std::string& file_name) {
|
||||
std::filesystem::path downloads;
|
||||
const char* download_dir = getenv("XDG_DOWNLOAD_DIR");
|
||||
|
||||
|
||||
if (download_dir != nullptr) {
|
||||
downloads = std::filesystem::path(download_dir);
|
||||
} else {
|
||||
@@ -104,15 +105,15 @@ std::string ImplementationPlatform::GetDownloadPath(
|
||||
downloads = "/tmp/Downloads";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return (downloads / std::filesystem::path(file_name).filename()).string();
|
||||
}
|
||||
|
||||
std::string ImplementationPlatform::GetAppDataPath(
|
||||
const std::string &file_name) {
|
||||
const std::string& file_name) {
|
||||
std::filesystem::path state;
|
||||
const char* state_home = getenv("XDG_STATE_HOME");
|
||||
|
||||
|
||||
if (state_home != nullptr) {
|
||||
state = std::filesystem::path(state_home);
|
||||
} else {
|
||||
@@ -124,11 +125,13 @@ std::string ImplementationPlatform::GetAppDataPath(
|
||||
state = "/tmp/state";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return (state / std::filesystem::path(file_name).filename()).string();
|
||||
}
|
||||
|
||||
OSName ImplementationPlatform::GetCurrentOS() { return OSName::kWindows; }
|
||||
OSName ImplementationPlatform::GetCurrentOS() {
|
||||
return OSName::kWindows;
|
||||
}
|
||||
|
||||
std::unique_ptr<api::AtomicBoolean> ImplementationPlatform::CreateAtomicBoolean(
|
||||
bool initial_value) {
|
||||
@@ -154,7 +157,7 @@ std::unique_ptr<api::Mutex> ImplementationPlatform::CreateMutex(
|
||||
#pragma pop_macro("CreateMutex")
|
||||
|
||||
std::unique_ptr<api::ConditionVariable>
|
||||
ImplementationPlatform::CreateConditionVariable(api::Mutex *mutex) {
|
||||
ImplementationPlatform::CreateConditionVariable(api::Mutex* mutex) {
|
||||
return std::make_unique<linux::ConditionVariable>(mutex);
|
||||
}
|
||||
|
||||
@@ -171,7 +174,7 @@ std::unique_ptr<api::InputFile> ImplementationPlatform::CreateInputFile(
|
||||
}
|
||||
|
||||
std::unique_ptr<InputFile> ImplementationPlatform::CreateInputFile(
|
||||
const std::string &file_path) {
|
||||
const std::string& file_path) {
|
||||
return nearby::shared::IOFile::CreateInputFile(file_path);
|
||||
}
|
||||
|
||||
@@ -182,20 +185,20 @@ std::unique_ptr<OutputFile> ImplementationPlatform::CreateOutputFile(
|
||||
}
|
||||
|
||||
std::unique_ptr<OutputFile> ImplementationPlatform::CreateOutputFile(
|
||||
const std::string &file_path) {
|
||||
const std::string& file_path) {
|
||||
std::filesystem::path path(file_path);
|
||||
try {
|
||||
std::filesystem::create_directories(path.parent_path());
|
||||
} catch (std::filesystem::filesystem_error const &err) {
|
||||
} catch (std::filesystem::filesystem_error const& err) {
|
||||
LOG(ERROR) << __func__ << ": error creating directory tree "
|
||||
<< path.parent_path() << ": " << err.what();
|
||||
<< path.parent_path() << ": " << err.what();
|
||||
}
|
||||
|
||||
return nearby::shared::IOFile::CreateOutputFile(path.string());
|
||||
}
|
||||
|
||||
std::unique_ptr<api::LogMessage> ImplementationPlatform::CreateLogMessage(
|
||||
const char *file, int line, LogMessage::Severity severity) {
|
||||
const char* file, int line, LogMessage::Severity severity) {
|
||||
return nullptr;
|
||||
// Disabled LogMessage
|
||||
// return std::make_unique<linux::LogMessage>(file, line, severity);
|
||||
@@ -223,33 +226,34 @@ ImplementationPlatform::CreateBluetoothAdapter() {
|
||||
auto manager = linux::bluez::BluezObjectManager(*system_bus);
|
||||
try {
|
||||
auto interfaces = manager.GetManagedObjects();
|
||||
for (auto &[object, properties] : interfaces) {
|
||||
if (properties.count(sdbus::InterfaceName(org::bluez::Adapter1_proxy::INTERFACE_NAME)) == 1) {
|
||||
for (auto& [object, properties] : interfaces) {
|
||||
if (properties.count(sdbus::InterfaceName(
|
||||
org::bluez::Adapter1_proxy::INTERFACE_NAME)) == 1) {
|
||||
LOG(INFO) << __func__ << ": found bluetooth adapter " << object;
|
||||
return std::make_unique<linux::BluetoothAdapter>(system_bus, object);
|
||||
}
|
||||
}
|
||||
} catch (const sdbus::Error &e) {
|
||||
} catch (const sdbus::Error& e) {
|
||||
DBUS_LOG_METHOD_CALL_ERROR(&manager, "GetManagedObjects", e);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
LOG(ERROR) << __func__
|
||||
<< ": couldn't find a bluetooth adapter on this system";
|
||||
<< ": couldn't find a bluetooth adapter on this system";
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::unique_ptr<api::BluetoothClassicMedium>
|
||||
ImplementationPlatform::CreateBluetoothClassicMedium(
|
||||
BluetoothAdapter &adapter) {
|
||||
BluetoothAdapter& adapter) {
|
||||
return std::make_unique<linux::BluetoothClassicMedium>(
|
||||
dynamic_cast<linux::BluetoothAdapter &>(adapter));
|
||||
dynamic_cast<linux::BluetoothAdapter&>(adapter));
|
||||
}
|
||||
|
||||
std::unique_ptr<api::ble::BleMedium> ImplementationPlatform::CreateBleMedium(
|
||||
BluetoothAdapter &adapter) {
|
||||
BluetoothAdapter& adapter) {
|
||||
return std::make_unique<linux::BleV2Medium>(
|
||||
dynamic_cast<linux::BluetoothAdapter &>(adapter));
|
||||
dynamic_cast<linux::BluetoothAdapter&>(adapter));
|
||||
}
|
||||
|
||||
std::unique_ptr<api::CredentialStorage>
|
||||
@@ -265,7 +269,7 @@ static std::unique_ptr<linux::NetworkManagerWifiMedium> createWifiMedium(
|
||||
|
||||
try {
|
||||
device_paths = nm->GetAllDevices();
|
||||
} catch (const sdbus::Error &e) {
|
||||
} catch (const sdbus::Error& e) {
|
||||
DBUS_LOG_METHOD_CALL_ERROR(nm, "GetAllDevices", e);
|
||||
return nullptr;
|
||||
}
|
||||
@@ -273,38 +277,39 @@ static std::unique_ptr<linux::NetworkManagerWifiMedium> createWifiMedium(
|
||||
auto manager = linux::networkmanager::ObjectManager(nm->GetConnection());
|
||||
|
||||
std::map<sdbus::ObjectPath,
|
||||
std::map<sdbus::InterfaceName, std::map<sdbus::PropertyName, sdbus::Variant>>>
|
||||
std::map<sdbus::InterfaceName,
|
||||
std::map<sdbus::PropertyName, sdbus::Variant>>>
|
||||
objects;
|
||||
try {
|
||||
objects = manager.GetManagedObjects();
|
||||
} catch (const sdbus::Error &e) {
|
||||
} catch (const sdbus::Error& e) {
|
||||
DBUS_LOG_METHOD_CALL_ERROR(nm, "GetManagedObjects", e);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
for (auto &device_path : device_paths) {
|
||||
for (auto& device_path : device_paths) {
|
||||
if (objects.count(device_path) == 1) {
|
||||
auto device = objects[device_path];
|
||||
if (device.count(sdbus::InterfaceName(org::freedesktop::NetworkManager::Device::
|
||||
Wireless_proxy::INTERFACE_NAME)) == 1) {
|
||||
if (device.count(
|
||||
sdbus::InterfaceName(org::freedesktop::NetworkManager::Device::
|
||||
Wireless_proxy::INTERFACE_NAME)) == 1) {
|
||||
LOG(INFO) << __func__
|
||||
<< ": Found a wireless device at :" << device_path;
|
||||
<< ": Found a wireless device at :" << device_path;
|
||||
return std::make_unique<linux::NetworkManagerWifiMedium>(nm,
|
||||
device_path);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
LOG(ERROR) << __func__
|
||||
<< ": couldn't find a wireless device on this system";
|
||||
LOG(ERROR) << __func__ << ": couldn't find a wireless device on this system";
|
||||
return nullptr;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
std::unique_ptr<api::WifiMedium> ImplementationPlatform::CreateWifiMedium() {
|
||||
// return nullptr;
|
||||
auto nm =
|
||||
std::make_shared<linux::networkmanager::NetworkManager>(linux::getSystemBusConnection());
|
||||
auto nm = std::make_shared<linux::networkmanager::NetworkManager>(
|
||||
linux::getSystemBusConnection());
|
||||
return createWifiMedium(nm);
|
||||
}
|
||||
|
||||
@@ -317,8 +322,8 @@ ImplementationPlatform::CreateWifiLanMedium() {
|
||||
|
||||
std::unique_ptr<api::WifiHotspotMedium>
|
||||
ImplementationPlatform::CreateWifiHotspotMedium() {
|
||||
auto nm =
|
||||
std::make_shared<linux::networkmanager::NetworkManager>(linux::getSystemBusConnection());
|
||||
auto nm = std::make_shared<linux::networkmanager::NetworkManager>(
|
||||
linux::getSystemBusConnection());
|
||||
auto wifiMedium = createWifiMedium(nm);
|
||||
|
||||
if (wifiMedium == nullptr) {
|
||||
@@ -332,18 +337,45 @@ ImplementationPlatform::CreateWifiHotspotMedium() {
|
||||
|
||||
std::unique_ptr<api::WifiDirectMedium>
|
||||
ImplementationPlatform::CreateWifiDirectMedium() {
|
||||
auto nm = std::make_shared<linux::networkmanager::NetworkManager>(
|
||||
linux::getSystemBusConnection());
|
||||
auto manager = linux::networkmanager::ObjectManager(nm->GetConnection());
|
||||
|
||||
std::map<sdbus::ObjectPath,
|
||||
std::map<sdbus::InterfaceName,
|
||||
std::map<sdbus::PropertyName, sdbus::Variant>>>
|
||||
objects;
|
||||
try {
|
||||
objects = manager.GetManagedObjects();
|
||||
} catch (const sdbus::Error& error) {
|
||||
DBUS_LOG_METHOD_CALL_ERROR(nm, "GetManagedObjects", error);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
const sdbus::InterfaceName wifi_p2p_interface(
|
||||
org::freedesktop::NetworkManager::Device::WifiP2P_proxy::INTERFACE_NAME);
|
||||
const sdbus::InterfaceName device_interface(
|
||||
"org.freedesktop.NetworkManager.Device");
|
||||
const sdbus::PropertyName managed_property("Managed");
|
||||
for (const auto& [object_path, interfaces] : objects) {
|
||||
if (!interfaces.contains(wifi_p2p_interface)) {
|
||||
continue;
|
||||
}
|
||||
auto device = interfaces.find(device_interface);
|
||||
if (device != interfaces.end()) {
|
||||
auto managed = device->second.find(managed_property);
|
||||
if (managed != device->second.end() && !managed->second.get<bool>()) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
LOG(INFO) << __func__ << ": Found Wi-Fi P2P device at " << object_path;
|
||||
return std::make_unique<linux::NetworkManagerWifiDirectMedium>(
|
||||
nm, object_path);
|
||||
}
|
||||
|
||||
LOG(WARNING) << __func__
|
||||
<< ": NetworkManager exposes no managed Wi-Fi P2P device";
|
||||
return nullptr;
|
||||
// auto nm =
|
||||
// std::make_shared<linux::networkmanager::NetworkManager>(linux::getSystemBusConnection());
|
||||
// auto wifiMedium = createWifiMedium(nm);
|
||||
//
|
||||
// if (wifiMedium == nullptr) {
|
||||
// LOG(ERROR) << __func__ << ": Could not create a WiFi medium";
|
||||
// return nullptr;
|
||||
// }
|
||||
//
|
||||
// return std::make_unique<linux::NetworkManagerWifiDirectMedium>(
|
||||
// nm, std::move(wifiMedium));
|
||||
}
|
||||
|
||||
std::unique_ptr<api::Timer> ImplementationPlatform::CreateTimer() {
|
||||
@@ -354,18 +386,18 @@ std::unique_ptr<api::DeviceInfo> ImplementationPlatform::CreateDeviceInfo() {
|
||||
return std::make_unique<linux::DeviceInfo>(linux::getSystemBusConnection());
|
||||
}
|
||||
|
||||
std::unique_ptr<AwdlMedium> ImplementationPlatform::CreateAwdlMedium() {
|
||||
std::unique_ptr<AwdlMedium> ImplementationPlatform::CreateAwdlMedium() {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
absl::StatusOr<api::WebResponse> ImplementationPlatform::SendRequest(
|
||||
const WebRequest &request) {
|
||||
const WebRequest& request) {
|
||||
if (request.body.size() >= (8 * 1024 * 1024)) {
|
||||
return absl::Status(absl::StatusCode::kResourceExhausted,
|
||||
"request body too large");
|
||||
}
|
||||
|
||||
CURL *handle = curl_easy_init();
|
||||
CURL* handle = curl_easy_init();
|
||||
char errbuf[CURL_ERROR_SIZE];
|
||||
errbuf[0] = '\0';
|
||||
|
||||
@@ -381,9 +413,9 @@ absl::StatusOr<api::WebResponse> ImplementationPlatform::SendRequest(
|
||||
|
||||
curl_easy_setopt(handle, CURLOPT_UPLOAD, request.body.c_str());
|
||||
|
||||
struct curl_slist *headers_slist = nullptr;
|
||||
struct curl_slist* headers_slist = nullptr;
|
||||
|
||||
for (auto &[key, value] : request.headers) {
|
||||
for (auto& [key, value] : request.headers) {
|
||||
auto hdr = absl::StrCat(key, ": ", value);
|
||||
auto temp = curl_slist_append(headers_slist, hdr.c_str());
|
||||
if (temp == nullptr) {
|
||||
@@ -400,27 +432,26 @@ absl::StatusOr<api::WebResponse> ImplementationPlatform::SendRequest(
|
||||
api::WebResponse response;
|
||||
|
||||
if (curl_easy_perform(handle) != CURLE_OK) {
|
||||
LOG(ERROR) << __func__
|
||||
<< ": Error performing HTTP request: " << errbuf;
|
||||
LOG(ERROR) << __func__ << ": Error performing HTTP request: " << errbuf;
|
||||
return absl::Status(absl::StatusCode::kUnknown, errbuf);
|
||||
}
|
||||
|
||||
struct curl_header *prev = nullptr;
|
||||
struct curl_header *h;
|
||||
struct curl_header* prev = nullptr;
|
||||
struct curl_header* h;
|
||||
|
||||
h = curl_easy_nextheader(handle, CURLH_HEADER, 0, prev);
|
||||
while (h != nullptr) {
|
||||
response.headers.emplace(h->name, h->value);
|
||||
}
|
||||
|
||||
auto writefn = [](char *ptr, size_t size, size_t nmemb, void *userdata) {
|
||||
std::string *body = static_cast<std::string *>(userdata);
|
||||
auto writefn = [](char* ptr, size_t size, size_t nmemb, void* userdata) {
|
||||
std::string* body = static_cast<std::string*>(userdata);
|
||||
body->append(ptr, size * nmemb);
|
||||
};
|
||||
|
||||
curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, writefn);
|
||||
curl_easy_setopt(handle, CURLOPT_WRITEDATA,
|
||||
static_cast<void *>(&response.body));
|
||||
static_cast<void*>(&response.body));
|
||||
long status;
|
||||
curl_easy_getinfo(handle, CURLINFO_RESPONSE_CODE, &status);
|
||||
response.status_code = status;
|
||||
|
||||
Reference in New Issue
Block a user