// Copyright 2025 Google LLC // // 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 // // 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. #ifndef PLATFORM_IMPL_APPLE_WIFI_LAN_H_ #define PLATFORM_IMPL_APPLE_WIFI_LAN_H_ #import #import #include #include #include "absl/strings/string_view.h" #include "internal/platform/implementation/upgrade_address_info.h" #include "internal/platform/implementation/wifi_lan.h" #include "internal/platform/nsd_service_info.h" #include "internal/platform/service_address.h" @class GNCNWFramework; @class GNCNWFrameworkServerSocket; @class GNCNWFrameworkSocket; namespace nearby { namespace apple { /** * InputStream that reads from GNCMConnection. */ class WifiLanInputStream : public InputStream { public: explicit WifiLanInputStream(GNCNWFrameworkSocket* socket); ~WifiLanInputStream() override = default; ExceptionOr Read(std::int64_t size) override; Exception Close() override; private: GNCNWFrameworkSocket* socket_; }; /** * OutputStream that writes to GNCMConnection. */ class WifiLanOutputStream : public OutputStream { public: explicit WifiLanOutputStream(GNCNWFrameworkSocket* socket); ~WifiLanOutputStream() override = default; Exception Write(absl::string_view data) override; Exception Flush() override; Exception Close() override; private: GNCNWFrameworkSocket* socket_; }; /** * Concrete WifiLanSocket implementation. */ class WifiLanSocket : public api::WifiLanSocket { public: explicit WifiLanSocket(GNCNWFrameworkSocket* socket); ~WifiLanSocket() override = default; InputStream& GetInputStream() override; OutputStream& GetOutputStream() override; Exception Close() override; private: GNCNWFrameworkSocket* socket_; std::unique_ptr input_stream_; std::unique_ptr output_stream_; }; /** * Concrete WifiLanServerSocket implementation. */ class WifiLanServerSocket : public api::WifiLanServerSocket { public: explicit WifiLanServerSocket(GNCNWFrameworkServerSocket* server_socket); ~WifiLanServerSocket() override = default; std::string GetIPAddress() const override; int GetPort() const override; std::unique_ptr Accept() override; Exception Close() override; private: GNCNWFrameworkServerSocket* server_socket_; }; /** * Concrete WifiLanMedium implementation. */ class WifiLanMedium : public api::WifiLanMedium { public: WifiLanMedium(); // For testing only. explicit WifiLanMedium(GNCNWFramework* medium); ~WifiLanMedium() override = default; WifiLanMedium(const WifiLanMedium&) = delete; WifiLanMedium& operator=(const WifiLanMedium&) = delete; bool IsNetworkConnected() const override { return true; } absl::optional> GetDynamicPortRange() override { return absl::nullopt; } bool StartAdvertising(const NsdServiceInfo& nsd_service_info) override; bool StopAdvertising(const NsdServiceInfo& nsd_service_info) override; bool StartDiscovery(const std::string& service_type, DiscoveredServiceCallback callback) override; bool StopDiscovery(const std::string& service_type) override; std::unique_ptr ConnectToService( const NsdServiceInfo& remote_service_info, CancellationFlag* cancellation_flag) override; std::unique_ptr ConnectToService( const ServiceAddress& service_address, CancellationFlag* cancellation_flag) override; std::unique_ptr ListenForService(int port) override; api::UpgradeAddressInfo GetUpgradeAddressCandidates(const api::WifiLanServerSocket& server_socket) override; private: GNCNWFramework* medium_; DiscoveredServiceCallback service_callback_; absl::AnyInvocable service_discovered_cb_; absl::AnyInvocable service_lost_cb_; }; } // namespace apple } // namespace nearby #endif // PLATFORM_IMPL_APPLE_WIFI_LAN_H_