diff --git a/windows/BUILD b/windows/BUILD index e1a4fc3e..f69c9c11 100644 --- a/windows/BUILD +++ b/windows/BUILD @@ -19,16 +19,16 @@ licenses(["notice"]) cc_windows_dll( name = "core_adapter", srcs = [ + "advertising_options.cc", + "connection_options.cc", "core_adapter.cc", - "request_connection_options.cc", - "start_advertising_options.cc", - "start_discovery_options.cc", + "discovery_options.cc", ], hdrs = [ + "advertising_options.h", + "connection_options.h", "core_adapter.h", - "request_connection_options.h", - "start_advertising_options.h", - "start_discovery_options.h", + "discovery_options.h", ], copts = ["-DCORE_ADAPTER_BUILD_DLL -DCOMPILING_CORE"], defines = ["CORE_ADAPTER_DLL"], @@ -47,18 +47,18 @@ cc_windows_dll( cc_windows_dll( name = "core_adapter_dart", srcs = [ + "advertising_options.cc", + "connection_options.cc", "core_adapter.cc", "dart/core_adapter_dart.cc", - "request_connection_options.cc", - "start_advertising_options.cc", - "start_discovery_options.cc", + "discovery_options.cc", ], hdrs = [ + "advertising_options.h", + "connection_options.h", "core_adapter.h", "dart/core_adapter_dart.h", - "request_connection_options.h", - "start_advertising_options.h", - "start_discovery_options.h", + "discovery_options.h", ], copts = ["-DCORE_ADAPTER_DART_BUILD_DLL -DCORE_ADAPTER_DLL -DCOMPILING_CORE"], defines = ["CORE_ADAPTER_DART_DLL"], diff --git a/windows/start_advertising_options.cc b/windows/advertising_options.cc similarity index 77% rename from windows/start_advertising_options.cc rename to windows/advertising_options.cc index 4813eeea..e4cb45d3 100644 --- a/windows/start_advertising_options.cc +++ b/windows/advertising_options.cc @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "third_party/nearby_connections/windows/start_advertising_options.h" +#include "third_party/nearby_connections/windows/advertising_options.h" #include "core/options.h" #include "core/strategy.h" @@ -22,10 +22,11 @@ namespace location { namespace nearby { namespace connections { +namespace windows { -StartAdvertisingOptions::StartAdvertisingOptions() +AdvertisingOptions::AdvertisingOptions() : impl_(new connections::ConnectionOptions(), - [](connections::ConnectionOptions* impl) { delete impl; }), + [](connections::ConnectionOptions *impl) { delete impl; }), strategy(impl_->strategy), allowed(impl_->allowed), auto_upgrade_bandwidth(impl_->auto_upgrade_bandwidth), @@ -40,33 +41,32 @@ StartAdvertisingOptions::StartAdvertisingOptions() keep_alive_interval_millis(impl_->keep_alive_interval_millis), keep_alive_timeout_millis(impl_->keep_alive_timeout_millis) {} -StartAdvertisingOptions::operator connections::ConnectionOptions() const { +AdvertisingOptions::operator connections::ConnectionOptions() const { return *impl_.get(); } // Verify if ConnectionOptions is in a not-initialized (Empty) state. -bool StartAdvertisingOptions::Empty() const { return impl_->Empty(); } +bool AdvertisingOptions::Empty() const { return impl_->Empty(); } // Bring ConnectionOptions to a not-initialized (Empty) state. -void StartAdvertisingOptions::Clear() {} +void AdvertisingOptions::Clear() {} // Returns a copy and normalizes allowed mediums: // (1) If is_out_of_band_connection is true, verifies that there is only one // medium allowed, defaulting to only Bluetooth if unspecified. // (2) If no mediums are allowed, allow all mediums. -ConnectionOptions StartAdvertisingOptions::CompatibleOptions() const { +connections::ConnectionOptions AdvertisingOptions::CompatibleOptions() const { return impl_->CompatibleOptions(); } -// std::vector GetMediums() const; - // This call follows the standard Microsoft calling pattern of calling first // to get the size of the array. Caller then allocates memory for the array, // and makes this call again to copy the array into the provided location. -void StartAdvertisingOptions::GetMediums( - location::nearby::proto::connections::Medium* mediums, - uint32_t* mediumsSize) {} +void AdvertisingOptions::GetMediums( + location::nearby::proto::connections::Medium *mediums, + uint32_t *mediumsSize) {} +} // namespace windows } // namespace connections } // namespace nearby } // namespace location diff --git a/windows/start_advertising_options.h b/windows/advertising_options.h similarity index 70% rename from windows/start_advertising_options.h rename to windows/advertising_options.h index 6c3a9854..55642699 100644 --- a/windows/start_advertising_options.h +++ b/windows/advertising_options.h @@ -27,10 +27,9 @@ class ByteArray; namespace proto { namespace connections { enum Medium : int; -} +} // namespace connections } // namespace proto namespace connections { - struct ConnectionOptions; class Strategy; template @@ -38,31 +37,33 @@ struct MediumSelector; using BooleanMediumSelector = MediumSelector; -struct DLL_API StartAdvertisingOptions { - StartAdvertisingOptions(); +namespace windows { + +struct DLL_API AdvertisingOptions { + AdvertisingOptions(); operator connections::ConnectionOptions() const; private: std::unique_ptr + void (*)(connections::ConnectionOptions *)> impl_; public: - Strategy& strategy; - BooleanMediumSelector& allowed; + connections::Strategy &strategy; + connections::BooleanMediumSelector &allowed; - bool& auto_upgrade_bandwidth; - bool& enable_bluetooth_listening; - bool& enable_webrtc_listening; - bool& low_power; - bool& enforce_topology_constraints; + bool &auto_upgrade_bandwidth; + bool &enable_bluetooth_listening; + bool &enable_webrtc_listening; + bool &low_power; + bool &enforce_topology_constraints; // Whether this is intended to be used in conjunction with InjectEndpoint(). - bool& is_out_of_band_connection; - ByteArray& remote_bluetooth_mac_address; - const char* fast_advertisement_service_uuid; - int& keep_alive_interval_millis; - int& keep_alive_timeout_millis; + bool &is_out_of_band_connection; + ByteArray &remote_bluetooth_mac_address; + const char *fast_advertisement_service_uuid; + int &keep_alive_interval_millis; + int &keep_alive_timeout_millis; // Verify if ConnectionOptions is in a not-initialized (Empty) state. bool Empty() const; @@ -74,15 +75,16 @@ struct DLL_API StartAdvertisingOptions { // (1) If is_out_of_band_connection is true, verifies that there is only one // medium allowed, defaulting to only Bluetooth if unspecified. // (2) If no mediums are allowed, allow all mediums. - ConnectionOptions CompatibleOptions() const; + connections::ConnectionOptions CompatibleOptions() const; // This call follows the standard Microsoft calling pattern of calling first // to get the size of the array. Caller then allocates memory for the array, // and makes this call again to copy the array into the provided location. - void GetMediums(location::nearby::proto::connections::Medium* mediums, - uint32_t* mediumsSize); + void GetMediums(location::nearby::proto::connections::Medium *mediums, + uint32_t *mediumsSize); }; +} // namespace windows } // namespace connections } // namespace nearby } // namespace location diff --git a/windows/start_discovery_options.cc b/windows/connection_options.cc similarity index 78% rename from windows/start_discovery_options.cc rename to windows/connection_options.cc index bbcb6a5d..79913f18 100644 --- a/windows/start_discovery_options.cc +++ b/windows/connection_options.cc @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "third_party/nearby_connections/windows/start_discovery_options.h" +#include "third_party/nearby_connections/windows/connection_options.h" #include "core/options.h" #include "core/strategy.h" @@ -22,10 +22,11 @@ namespace location { namespace nearby { namespace connections { +namespace windows { -StartDiscoveryOptions::StartDiscoveryOptions() +ConnectionOptions::ConnectionOptions() : impl_(new connections::ConnectionOptions(), - [](connections::ConnectionOptions* impl) { delete impl; }), + [](connections::ConnectionOptions *impl) { delete impl; }), strategy(impl_->strategy), allowed(impl_->allowed), auto_upgrade_bandwidth(impl_->auto_upgrade_bandwidth), @@ -40,21 +41,21 @@ StartDiscoveryOptions::StartDiscoveryOptions() keep_alive_interval_millis(impl_->keep_alive_interval_millis), keep_alive_timeout_millis(impl_->keep_alive_timeout_millis) {} -StartDiscoveryOptions::operator connections::ConnectionOptions() const { +ConnectionOptions::operator connections::ConnectionOptions() const { return *impl_.get(); } // Verify if ConnectionOptions is in a not-initialized (Empty) state. -bool StartDiscoveryOptions::Empty() const { return impl_->Empty(); } +bool ConnectionOptions::Empty() const { return impl_->Empty(); } // Bring ConnectionOptions to a not-initialized (Empty) state. -void StartDiscoveryOptions::Clear() {} +void ConnectionOptions::Clear() {} // Returns a copy and normalizes allowed mediums: // (1) If is_out_of_band_connection is true, verifies that there is only one // medium allowed, defaulting to only Bluetooth if unspecified. // (2) If no mediums are allowed, allow all mediums. -ConnectionOptions StartDiscoveryOptions::CompatibleOptions() const { +connections::ConnectionOptions ConnectionOptions::CompatibleOptions() const { return impl_->CompatibleOptions(); } @@ -63,10 +64,11 @@ ConnectionOptions StartDiscoveryOptions::CompatibleOptions() const { // This call follows the standard Microsoft calling pattern of calling first // to get the size of the array. Caller then allocates memory for the array, // and makes this call again to copy the array into the provided location. -void StartDiscoveryOptions::GetMediums( - location::nearby::proto::connections::Medium* mediums, - uint32_t* mediumsSize) {} +void ConnectionOptions::GetMediums( + location::nearby::proto::connections::Medium *mediums, + uint32_t *mediumsSize) {} +} // namespace windows } // namespace connections } // namespace nearby } // namespace location diff --git a/windows/request_connection_options.h b/windows/connection_options.h similarity index 71% rename from windows/request_connection_options.h rename to windows/connection_options.h index d41b38c5..f49fb431 100644 --- a/windows/request_connection_options.h +++ b/windows/connection_options.h @@ -37,31 +37,32 @@ struct MediumSelector; using BooleanMediumSelector = MediumSelector; -struct DLL_API RequestConnectionOptions { - RequestConnectionOptions(); +namespace windows { +struct DLL_API ConnectionOptions { + ConnectionOptions(); operator connections::ConnectionOptions() const; private: std::unique_ptr + void (*)(connections::ConnectionOptions *)> impl_; public: - Strategy& strategy; - BooleanMediumSelector& allowed; + connections::Strategy &strategy; + connections::BooleanMediumSelector &allowed; - bool& auto_upgrade_bandwidth; - bool& enable_bluetooth_listening; - bool& enable_webrtc_listening; - bool& low_power; - bool& enforce_topology_constraints; + bool &auto_upgrade_bandwidth; + bool &enable_bluetooth_listening; + bool &enable_webrtc_listening; + bool &low_power; + bool &enforce_topology_constraints; // Whether this is intended to be used in conjunction with InjectEndpoint(). - bool& is_out_of_band_connection; - ByteArray& remote_bluetooth_mac_address; - const char* fast_advertisement_service_uuid; - int& keep_alive_interval_millis; - int& keep_alive_timeout_millis; + bool &is_out_of_band_connection; + ByteArray &remote_bluetooth_mac_address; + const char *fast_advertisement_service_uuid; + int &keep_alive_interval_millis; + int &keep_alive_timeout_millis; // Verify if ConnectionOptions is in a not-initialized (Empty) state. bool Empty() const; @@ -73,15 +74,16 @@ struct DLL_API RequestConnectionOptions { // (1) If is_out_of_band_connection is true, verifies that there is only one // medium allowed, defaulting to only Bluetooth if unspecified. // (2) If no mediums are allowed, allow all mediums. - ConnectionOptions CompatibleOptions() const; + connections::ConnectionOptions CompatibleOptions() const; // This call follows the standard Microsoft calling pattern of calling first // to get the size of the array. Caller then allocates memory for the array, // and makes this call again to copy the array into the provided location. - void GetMediums(location::nearby::proto::connections::Medium* mediums, - uint32_t* mediumsSize); + void GetMediums(location::nearby::proto::connections::Medium *mediums, + uint32_t *mediumsSize); }; +} // namespace windows } // namespace connections } // namespace nearby } // namespace location diff --git a/windows/core_adapter.cc b/windows/core_adapter.cc index 0e7cfd9d..8b09502b 100644 --- a/windows/core_adapter.cc +++ b/windows/core_adapter.cc @@ -19,50 +19,47 @@ namespace location { namespace nearby { namespace connections { +namespace windows { -Core* InitCore(ServiceControllerRouter* router) { - return new Core(router); -} +Core *InitCore(ServiceControllerRouter *router) { return new Core(router); } -void CloseCore(Core* pCore) { +void CloseCore(Core *pCore) { if (pCore) { pCore->StopAllEndpoints( - {.result_cb = - std::function{ - [](location::nearby::connections::Status) {}}}); + {.result_cb = std::function{[](Status) {}}}); delete pCore; } } -void StartAdvertising(Core* pCore, const char* service_id, - StartAdvertisingOptions options, - ConnectionRequestInfo info, ResultCallback callback) { +void StartAdvertising(Core *pCore, const char *service_id, + AdvertisingOptions options, ConnectionRequestInfo info, + ResultCallback callback) { if (pCore) { pCore->StartAdvertising(service_id, options, info, callback); } } -void StopAdvertising(Core* pCore, ResultCallback callback) { +void StopAdvertising(Core *pCore, ResultCallback callback) { if (pCore) { pCore->StopAdvertising(callback); } } -void StartDiscovery(Core* pCore, const char* service_id, - StartDiscoveryOptions options, DiscoveryListener listener, +void StartDiscovery(Core *pCore, const char *service_id, + DiscoveryOptions options, DiscoveryListener listener, ResultCallback callback) { if (pCore) { pCore->StartDiscovery(service_id, options, listener, callback); } } -void StopDiscovery(Core* pCore, ResultCallback callback) { +void StopDiscovery(Core *pCore, ResultCallback callback) { if (pCore) { pCore->StopDiscovery(callback); } } -void InjectEndpoint(Core* pCore, char* service_id, +void InjectEndpoint(Core *pCore, char *service_id, OutOfBandConnectionMetadata metadata, ResultCallback callback) { if (pCore) { @@ -70,30 +67,29 @@ void InjectEndpoint(Core* pCore, char* service_id, } } -void RequestConnection(Core* pCore, const char* endpoint_id, - ConnectionRequestInfo info, - RequestConnectionOptions options, +void RequestConnection(Core *pCore, const char *endpoint_id, + ConnectionRequestInfo info, ConnectionOptions options, ResultCallback callback) { if (pCore) { pCore->RequestConnection(endpoint_id, info, options, callback); } } -void AcceptConnection(Core* pCore, const char* endpoint_id, +void AcceptConnection(Core *pCore, const char *endpoint_id, PayloadListener listener, ResultCallback callback) { if (pCore) { pCore->AcceptConnection(endpoint_id, listener, callback); } } -void RejectConnection(Core* pCore, const char* endpoint_id, +void RejectConnection(Core *pCore, const char *endpoint_id, ResultCallback callback) { if (pCore) { pCore->RejectConnection(endpoint_id, callback); } } -void SendPayload(Core* pCore, +void SendPayload(Core *pCore, // todo(jfcarroll) this is being exported, needs to be // refactored to return a plain old c type absl::Span endpoint_ids, Payload payload, @@ -103,53 +99,54 @@ void SendPayload(Core* pCore, } } -void CancelPayload(Core* pCore, std::int64_t payload_id, +void CancelPayload(Core *pCore, std::int64_t payload_id, ResultCallback callback) { if (pCore) { pCore->CancelPayload(payload_id, callback); } } -void DisconnectFromEndpoint(Core* pCore, char* endpoint_id, +void DisconnectFromEndpoint(Core *pCore, char *endpoint_id, ResultCallback callback) { if (pCore) { pCore->DisconnectFromEndpoint(endpoint_id, callback); } } -void StopAllEndpoints(Core* pCore, ResultCallback callback) { +void StopAllEndpoints(Core *pCore, ResultCallback callback) { if (pCore) { pCore->StopAllEndpoints(callback); } } -void InitiateBandwidthUpgrade(Core* pCore, char* endpoint_id, +void InitiateBandwidthUpgrade(Core *pCore, char *endpoint_id, ResultCallback callback) { if (pCore) { pCore->InitiateBandwidthUpgrade(endpoint_id, callback); } } -const char* GetLocalEndpointId(Core* pCore) { +const char *GetLocalEndpointId(Core *pCore) { if (pCore) { std::string endpoint_id = pCore->GetLocalEndpointId(); - char* result = new char[endpoint_id.length() + 1]; + char *result = new char[endpoint_id.length() + 1]; absl::SNPrintF(result, endpoint_id.length() + 1, "%s", endpoint_id); return result; } return "Null-Core"; } -ServiceControllerRouter* InitServiceControllerRouter() { +ServiceControllerRouter *InitServiceControllerRouter() { return new ServiceControllerRouter(); } -void CloseServiceControllerRouter(ServiceControllerRouter* pRouter) { +void CloseServiceControllerRouter(ServiceControllerRouter *pRouter) { if (pRouter) { delete pRouter; } } +} // namespace windows } // namespace connections } // namespace nearby } // namespace location diff --git a/windows/core_adapter.h b/windows/core_adapter.h index 4576df0e..ca2994c0 100644 --- a/windows/core_adapter.h +++ b/windows/core_adapter.h @@ -19,34 +19,35 @@ // todo(jfcarroll) This cannot remain. It exposes stuff the client doesn't need. #include "core/internal/offline_service_controller.h" -#include "third_party/nearby_connections/windows/request_connection_options.h" -#include "third_party/nearby_connections/windows/start_advertising_options.h" -#include "third_party/nearby_connections/windows/start_discovery_options.h" +#include "third_party/nearby_connections/windows/advertising_options.h" +#include "third_party/nearby_connections/windows/connection_options.h" +#include "third_party/nearby_connections/windows/discovery_options.h" #define DLL_EXPORT extern "C" __declspec(dllexport) namespace location { namespace nearby { namespace connections { - class Core; class ServiceControllerRouter; +namespace windows { + // Initizlizes a Core instance, providing the ServiceController factory from // app side. If no factory is provided, it will initialize a new // factory creating OffilineServiceController. // Returns the instance handle to c# client. -DLL_EXPORT Core* __stdcall InitCoreWithServiceControllerFactory( - std::function factory = []() { - return new OfflineServiceController; +DLL_EXPORT connections::Core *__stdcall InitCoreWithServiceControllerFactory( + std::function factory = []() { + return new connections::OfflineServiceController; }); // Initializes a default Core instance. // Returns the instance handle to c# client. -DLL_EXPORT Core* __stdcall InitCore(ServiceControllerRouter* router); +DLL_EXPORT Core *__stdcall InitCore(ServiceControllerRouter *router); // Closes the core with stopping all endpoints, then free the memory. -DLL_EXPORT void __stdcall CloseCore(Core* pCore); +DLL_EXPORT void __stdcall CloseCore(Core *pCore); // Starts advertising an endpoint for a local app. // @@ -66,10 +67,9 @@ DLL_EXPORT void __stdcall CloseCore(Core* pCore); // Status::STATUS_ALREADY_ADVERTISING if the app is already advertising. // Status::STATUS_OUT_OF_ORDER_API_CALL if the app is currently // connected to remote endpoints; call StopAllEndpoints first. -DLL_EXPORT void __stdcall StartAdvertising(Core* pCore, const char* service_id, - StartAdvertisingOptions options, - ConnectionRequestInfo info, - ResultCallback callback); +DLL_EXPORT void __stdcall StartAdvertising( + Core *pCore, const char *service_id, AdvertisingOptions options, + connections::ConnectionRequestInfo info, ResultCallback callback); // Stops advertising a local endpoint. Should be called after calling // StartAdvertising, as soon as the application no longer needs to advertise @@ -79,7 +79,7 @@ DLL_EXPORT void __stdcall StartAdvertising(Core* pCore, const char* service_id, // result_cb - to access the status of the operation when available. // Possible status codes include: // Status::STATUS_OK if none of the above errors occurred. -DLL_EXPORT void __stdcall StopAdvertising(Core* pCore, ResultCallback callback); +DLL_EXPORT void __stdcall StopAdvertising(Core *pCore, ResultCallback callback); // Starts discovery for remote endpoints with the specified service ID. // @@ -94,8 +94,8 @@ DLL_EXPORT void __stdcall StopAdvertising(Core* pCore, ResultCallback callback); // discovering the specified service. // Status::STATUS_OUT_OF_ORDER_API_CALL if the app is currently // connected to remote endpoints; call StopAllEndpoints first. -DLL_EXPORT void __stdcall StartDiscovery(Core* pCore, const char* service_id, - StartDiscoveryOptions options, +DLL_EXPORT void __stdcall StartDiscovery(Core *pCore, const char *service_id, + DiscoveryOptions options, DiscoveryListener listener, ResultCallback callback); @@ -107,7 +107,8 @@ DLL_EXPORT void __stdcall StartDiscovery(Core* pCore, const char* service_id, // result_cb - to access the status of the operation when available. // Possible status codes include: // Status::STATUS_OK if none of the above errors occurred. -DLL_EXPORT void __stdcall StopDiscovery(Core* pCore, ResultCallback callback); +DLL_EXPORT void __stdcall StopDiscovery(connections::Core *pCore, + connections::ResultCallback callback); // Invokes the discovery callback from a previous call to StartDiscovery() // with the given endpoint info. The previous call to StartDiscovery() must @@ -124,9 +125,9 @@ DLL_EXPORT void __stdcall StopDiscovery(Core* pCore, ResultCallback callback); // Status::kError if endpoint_id, endpoint_info, or // remote_bluetooth_mac_address are malformed. // Status::kOutOfOrderApiCall if the app is not discovering. -DLL_EXPORT void __stdcall InjectEndpoint(Core* pCore, char* service_id, - OutOfBandConnectionMetadata metadata, - ResultCallback callback); +DLL_EXPORT void __stdcall InjectEndpoint(Core *pCore, char *service_id, + OutOfBandConnectionMetadata metadata, + ResultCallback callback); // Sends a request to connect to a remote endpoint. // @@ -147,10 +148,10 @@ DLL_EXPORT void __stdcall InjectEndpoint(Core* pCore, char* service_id, // Status::STATUS_RADIO_ERROR if we failed to connect because of an // issue with Bluetooth/WiFi. // Status::STATUS_ERROR if we failed to connect for any other reason. -DLL_EXPORT void __stdcall RequestConnection(Core* pCore, - const char* endpoint_id, +DLL_EXPORT void __stdcall RequestConnection(Core *pCore, + const char *endpoint_id, ConnectionRequestInfo info, - RequestConnectionOptions options, + ConnectionOptions options, ResultCallback callback); // Accepts a connection to a remote endpoint. This method must be called @@ -165,8 +166,7 @@ DLL_EXPORT void __stdcall RequestConnection(Core* pCore, // Status::STATUS_OK if the connection request was accepted. // Status::STATUS_ALREADY_CONNECTED_TO_ENDPOINT if the app already. // has a connection to the specified endpoint. -DLL_EXPORT void __stdcall AcceptConnection(Core* pCore, - const char* endpoint_id, +DLL_EXPORT void __stdcall AcceptConnection(Core *pCore, const char *endpoint_id, PayloadListener listener, ResultCallback callback); @@ -180,8 +180,8 @@ DLL_EXPORT void __stdcall AcceptConnection(Core* pCore, // Status::STATUS_OK} if the connection request was rejected. // Status::STATUS_ALREADY_CONNECTED_TO_ENDPOINT} if the app already // has a connection to the specified endpoint. -DLL_EXPORT void __stdcall RejectConnection(Core* pCore, const char* endpoint_id, - ResultCallback callback); +DLL_EXPORT void __stdcall RejectConnection(Core *pCore, const char *endpoint_id, + ResultCallback callback); // Sends a Payload to a remote endpoint. Payloads can only be sent to remote // endpoints once a notice of connection acceptance has been delivered via @@ -202,9 +202,9 @@ DLL_EXPORT void __stdcall RejectConnection(Core* pCore, const char* endpoint_id, // still occur during transmission (and at different times for // different endpoints), and will be delivered via // PayloadCallback#onPayloadTransferUpdate. -DLL_EXPORT void __stdcall SendPayload(Core* pCore, - absl::Span endpoint_ids, - Payload payload, ResultCallback callback); +DLL_EXPORT void __stdcall SendPayload( + Core *pCore, absl::Span endpoint_ids, Payload payload, + ResultCallback callback); // Cancels a Payload currently in-flight to or from remote endpoint(s). // @@ -212,8 +212,8 @@ DLL_EXPORT void __stdcall SendPayload(Core* pCore, // result_cb - to access the status of the operation when available. // Possible status codes include: // Status::STATUS_OK if none of the above errors occurred. -DLL_EXPORT void __stdcall CancelPayload(Core* pCore, int64_t payload_id, - ResultCallback callback); +DLL_EXPORT void __stdcall CancelPayload(Core *pCore, int64_t payload_id, + ResultCallback callback); // Disconnects from a remote endpoint. {@link Payload}s can no longer be sent // to or received from the endpoint after this method is called. @@ -222,8 +222,8 @@ DLL_EXPORT void __stdcall CancelPayload(Core* pCore, int64_t payload_id, // result_cb - to access the status of the operation when available. // Possible status codes include: // Status::STATUS_OK - finished successfully. -DLL_EXPORT void __stdcall DisconnectFromEndpoint(Core* pCore, char* endpoint_id, - ResultCallback callback); +DLL_EXPORT void __stdcall DisconnectFromEndpoint(Core *pCore, char *endpoint_id, + ResultCallback callback); // Disconnects from, and removes all traces of, all connected and/or // discovered endpoints. This call is expected to be preceded by a call to @@ -234,7 +234,7 @@ DLL_EXPORT void __stdcall DisconnectFromEndpoint(Core* pCore, char* endpoint_id, // result_cb - to access the status of the operation when available. // Possible status codes include: // Status::STATUS_OK - finished successfully. -DLL_EXPORT void __stdcall StopAllEndpoints(Core* pCore, +DLL_EXPORT void __stdcall StopAllEndpoints(Core *pCore, ResultCallback callback); // Sends a request to initiate connection bandwidth upgrade. @@ -246,22 +246,24 @@ DLL_EXPORT void __stdcall StopAllEndpoints(Core* pCore, // result_cb - to access the status of the operation when available. // Possible status codes include: // Status::STATUS_OK - finished successfully. -DLL_EXPORT void __stdcall InitiateBandwidthUpgrade(Core* pCore, - char* endpoint_id, +DLL_EXPORT void __stdcall InitiateBandwidthUpgrade(Core *pCore, + char *endpoint_id, ResultCallback callback); // Gets the local endpoint generated by Nearby Connections. -DLL_EXPORT const char* __stdcall GetLocalEndpointId(Core* pCore); +DLL_EXPORT const char *__stdcall GetLocalEndpointId(Core *pCore); // Initializes a default ServiceControllerRouter instance. // Returns the instance handle to c# client. -DLL_EXPORT ServiceControllerRouter* __stdcall InitServiceControllerRouter(); +DLL_EXPORT ServiceControllerRouter *__stdcall InitServiceControllerRouter(); // Close a ServiceControllerRouter instance. DLL_EXPORT void __stdcall CloseServiceControllerRouter( - ServiceControllerRouter* pRouter); + ServiceControllerRouter *pRouter); +} // namespace windows } // namespace connections } // namespace nearby } // namespace location + #endif // LOCATION_NEARBY_CONNECTIONS_WINDOWS_CORE_ADAPTER_H_ diff --git a/windows/dart/core_adapter_dart.cc b/windows/dart/core_adapter_dart.cc index 8ecd6d9e..72213b9f 100644 --- a/windows/dart/core_adapter_dart.cc +++ b/windows/dart/core_adapter_dart.cc @@ -19,6 +19,7 @@ namespace location { namespace nearby { namespace connections { +namespace windows { Strategy GetStrategy(StrategyDart strategy) { switch (strategy) { @@ -36,145 +37,125 @@ ByteArray ConvertBluetoothMacAddress(absl::string_view address) { return ByteArray(address.data()); } -void SetResultCallback(ResultCallback& callback, Dart_Port& port) { +void SetResultCallback(ResultCallback &callback, Dart_Port &port) { callback.result_cb = [port](Status status) { Dart_CObject dart_object_result_callback; dart_object_result_callback.type = Dart_CObject_kInt64; dart_object_result_callback.value.as_int64 = status.value; - const bool result = Dart_PostCObject_DL(port, - &dart_object_result_callback); + const bool result = Dart_PostCObject_DL(port, &dart_object_result_callback); if (!result) { NEARBY_LOG(INFO, "Posting message to port failed."); } }; } -void PostResult(Dart_Port& result_cb, Status::Value value) { +void PostResult(Dart_Port &result_cb, Status::Value value) { Dart_CObject dart_object_result_callback; dart_object_result_callback.type = Dart_CObject_kInt64; dart_object_result_callback.value.as_int64 = value; - const bool result = Dart_PostCObject_DL(result_cb, - &dart_object_result_callback); + const bool result = + Dart_PostCObject_DL(result_cb, &dart_object_result_callback); if (!result) { NEARBY_LOG(INFO, "Returning error to port failed."); } } -void StartAdvertisingDart(Core* pCore, - const char* service_id, +void StartAdvertisingDart(Core *pCore, const char *service_id, ConnectionOptionsDart options_dart, ConnectionRequestInfoDart info_dart, - Dart_Port result_cb) -{ + Dart_Port result_cb) { if (!pCore) { PostResult(result_cb, Status::Value::kError); return; } - StartAdvertisingOptions options; + windows::AdvertisingOptions options; options.strategy = GetStrategy(options_dart.strategy); - options.auto_upgrade_bandwidth = - options_dart.auto_upgrade_bandwidth; + options.auto_upgrade_bandwidth = options_dart.auto_upgrade_bandwidth; options.enforce_topology_constraints = options_dart.enforce_topology_constraints; options.allowed.bluetooth = options_dart.enable_bluetooth; options.allowed.ble = options_dart.enable_ble; options.low_power = options_dart.use_low_power_mode; options.fast_advertisement_service_uuid = - options_dart.discover_fast_advertisements ? - "0000FE2C-0000-1000-8000-00805F9B34FB" : ""; + options_dart.discover_fast_advertisements + ? "0000FE2C-0000-1000-8000-00805F9B34FB" + : ""; options.allowed.wifi_lan = options_dart.enable_wifi_lan; options.allowed.web_rtc = options_dart.enable_web_rtc; ConnectionRequestInfo info; info.endpoint_info = ByteArray(info_dart.endpoint_info); - info.listener.initiated_cb = [info_dart](const std::string& endpoint_id, - const ConnectionResponseInfo& connection_info) { - NEARBY_LOG(INFO, "Advertising initiated: id=%s", - endpoint_id.c_str()); - Dart_CObject dart_object_initiated; - dart_object_initiated.type = Dart_CObject_kString; - dart_object_initiated.value.as_string = - (char *)connection_info.remote_endpoint_info.data(); - const bool result = - Dart_PostCObject_DL(info_dart.initiated_cb, - &dart_object_initiated); - if (!result) { - NEARBY_LOG(INFO, - "Posting message to port failed."); - } - }; - info.listener.accepted_cb = [info_dart](const std::string& endpoint_id) { - NEARBY_LOG(INFO, "Advertising accepted: id=%s", - endpoint_id.c_str()); - Dart_CObject dart_object_accepted; - dart_object_accepted.type = Dart_CObject_kString; - dart_object_accepted.value.as_string = - (char *)endpoint_id.c_str(); - const bool result = - Dart_PostCObject_DL(info_dart.accepted_cb, - &dart_object_accepted); - if (!result) { - NEARBY_LOG(INFO, - "Posting message to port failed."); - } - }; - info.listener.rejected_cb = [info_dart](const std::string& endpoint_id, - Status status) { - NEARBY_LOG(INFO, "Advertising rejected: id=%s", - endpoint_id.c_str()); - Dart_CObject dart_object_rejected; - dart_object_rejected.type = Dart_CObject_kString; - dart_object_rejected.value.as_string = - (char *)endpoint_id.c_str(); - const bool result = - Dart_PostCObject_DL(info_dart.rejected_cb, - &dart_object_rejected); - if (!result) { - NEARBY_LOG(INFO, - "Posting message to port failed."); - } - }; - info.listener.disconnected_cb = [info_dart]( - const std::string& endpoint_id) { - NEARBY_LOG(INFO, "Advertising disconnected: id=%s", - endpoint_id.c_str()); - Dart_CObject dart_object_disconnected; - dart_object_disconnected.type = Dart_CObject_kString; - dart_object_disconnected.value.as_string = - (char *)endpoint_id.c_str(); - const bool result = - Dart_PostCObject_DL(info_dart.disconnected_cb, - &dart_object_disconnected); - if (!result) { - NEARBY_LOG(INFO, - "Posting message to port failed."); - } - }; - info.listener.bandwidth_changed_cb = [info_dart]( - const std::string& endpoint_id, Medium medium) { - NEARBY_LOG(INFO, "Advertising bandwidth changed: id=%s", - endpoint_id.c_str()); - Dart_CObject dart_object_bandwidth_changed; + info.listener.initiated_cb = + [info_dart](const std::string &endpoint_id, + const ConnectionResponseInfo &connection_info) { + NEARBY_LOG(INFO, "Advertising initiated: id=%s", endpoint_id.c_str()); + Dart_CObject dart_object_initiated; + dart_object_initiated.type = Dart_CObject_kString; + dart_object_initiated.value.as_string = + (char *)connection_info.remote_endpoint_info.data(); + const bool result = + Dart_PostCObject_DL(info_dart.initiated_cb, &dart_object_initiated); + if (!result) { + NEARBY_LOG(INFO, "Posting message to port failed."); + } + }; + info.listener.accepted_cb = [info_dart](const std::string &endpoint_id) { + NEARBY_LOG(INFO, "Advertising accepted: id=%s", endpoint_id.c_str()); + Dart_CObject dart_object_accepted; + dart_object_accepted.type = Dart_CObject_kString; + dart_object_accepted.value.as_string = (char *)endpoint_id.c_str(); + const bool result = + Dart_PostCObject_DL(info_dart.accepted_cb, &dart_object_accepted); + if (!result) { + NEARBY_LOG(INFO, "Posting message to port failed."); + } + }; + info.listener.rejected_cb = [info_dart](const std::string &endpoint_id, + Status status) { + NEARBY_LOG(INFO, "Advertising rejected: id=%s", endpoint_id.c_str()); + Dart_CObject dart_object_rejected; + dart_object_rejected.type = Dart_CObject_kString; + dart_object_rejected.value.as_string = (char *)endpoint_id.c_str(); + const bool result = + Dart_PostCObject_DL(info_dart.rejected_cb, &dart_object_rejected); + if (!result) { + NEARBY_LOG(INFO, "Posting message to port failed."); + } + }; + info.listener.disconnected_cb = [info_dart](const std::string &endpoint_id) { + NEARBY_LOG(INFO, "Advertising disconnected: id=%s", endpoint_id.c_str()); + Dart_CObject dart_object_disconnected; + dart_object_disconnected.type = Dart_CObject_kString; + dart_object_disconnected.value.as_string = (char *)endpoint_id.c_str(); + const bool result = Dart_PostCObject_DL(info_dart.disconnected_cb, + &dart_object_disconnected); + if (!result) { + NEARBY_LOG(INFO, "Posting message to port failed."); + } + }; + info.listener.bandwidth_changed_cb = + [info_dart](const std::string &endpoint_id, Medium medium) { + NEARBY_LOG(INFO, "Advertising bandwidth changed: id=%s", + endpoint_id.c_str()); + Dart_CObject dart_object_bandwidth_changed; - dart_object_bandwidth_changed.type = Dart_CObject_kString; - dart_object_bandwidth_changed.value.as_string = - (char *)endpoint_id.c_str(); - const bool result = - Dart_PostCObject_DL(info_dart.bandwidth_changed_cb, - &dart_object_bandwidth_changed); - if (!result) { - NEARBY_LOG(INFO, - "Posting message to port failed."); - } - }; + dart_object_bandwidth_changed.type = Dart_CObject_kString; + dart_object_bandwidth_changed.value.as_string = + (char *)endpoint_id.c_str(); + const bool result = Dart_PostCObject_DL(info_dart.bandwidth_changed_cb, + &dart_object_bandwidth_changed); + if (!result) { + NEARBY_LOG(INFO, "Posting message to port failed."); + } + }; ResultCallback callback; SetResultCallback(callback, result_cb); StartAdvertising(pCore, service_id, std::move(options), info, callback); } -void StopAdvertisingDart(Core* pCore, Dart_Port result_cb) { +void StopAdvertisingDart(Core *pCore, Dart_Port result_cb) { if (!pCore) { PostResult(result_cb, Status::Value::kError); return; @@ -182,11 +163,10 @@ void StopAdvertisingDart(Core* pCore, Dart_Port result_cb) { ResultCallback callback; SetResultCallback(callback, result_cb); - StopAdvertising(pCore, callback); + windows::StopAdvertising(pCore, callback); } -void StartDiscoveryDart(Core* pCore, - const char* service_id, +void StartDiscoveryDart(Core *pCore, const char *service_id, ConnectionOptionsDart options_dart, DiscoveryListenerDart listener_dart, Dart_Port result_cb) { @@ -195,100 +175,84 @@ void StartDiscoveryDart(Core* pCore, return; } - StartDiscoveryOptions options; + windows::DiscoveryOptions options; options.strategy = GetStrategy(options_dart.strategy); options.allowed.bluetooth = options_dart.enable_bluetooth; options.allowed.ble = options_dart.enable_ble; options.allowed.wifi_lan = options_dart.enable_wifi_lan; options.allowed.web_rtc = false; options.low_power = options_dart.use_low_power_mode; - options.enable_bluetooth_listening = - options_dart.enable_bluetooth; + options.enable_bluetooth_listening = options_dart.enable_bluetooth; options.enforce_topology_constraints = true; options.fast_advertisement_service_uuid = - options_dart.discover_fast_advertisements ? - "0000FE2C-0000-1000-8000-00805F9B34FB" : ""; + options_dart.discover_fast_advertisements + ? "0000FE2C-0000-1000-8000-00805F9B34FB" + : ""; DiscoveryListener listener; - listener.endpoint_found_cb = - [listener_dart](const std::string& endpoint_id, - const ByteArray& endpoint_info, - const std::string& str_service_id) { - NEARBY_LOG(INFO, "Device discovered: id=%s", - endpoint_id.c_str()); - NEARBY_LOG(INFO, "Device discovered: service_id=%s", - str_service_id.c_str()); - NEARBY_LOG(INFO, "Device discovered: info=%s", - ((string)endpoint_info).c_str()); + listener.endpoint_found_cb = [listener_dart]( + const std::string &endpoint_id, + const ByteArray &endpoint_info, + const std::string &str_service_id) { + NEARBY_LOG(INFO, "Device discovered: id=%s", endpoint_id.c_str()); + NEARBY_LOG(INFO, "Device discovered: service_id=%s", + str_service_id.c_str()); + NEARBY_LOG(INFO, "Device discovered: info=%s", + ((string)endpoint_info).c_str()); - Dart_CObject dart_object_endpoint_id; - dart_object_endpoint_id.type = Dart_CObject_kString; - dart_object_endpoint_id.value.as_string = - (char *)endpoint_id.data(); + Dart_CObject dart_object_endpoint_id; + dart_object_endpoint_id.type = Dart_CObject_kString; + dart_object_endpoint_id.value.as_string = (char *)endpoint_id.data(); - Dart_CObject dart_object_endpoint_info; - dart_object_endpoint_info.type = Dart_CObject_kString; - dart_object_endpoint_info.value.as_string = - (char *)endpoint_info.data(); + Dart_CObject dart_object_endpoint_info; + dart_object_endpoint_info.type = Dart_CObject_kString; + dart_object_endpoint_info.value.as_string = (char *)endpoint_info.data(); - Dart_CObject* elements[2]; - elements[0] = &dart_object_endpoint_id; - elements[1] = &dart_object_endpoint_info; + Dart_CObject *elements[2]; + elements[0] = &dart_object_endpoint_id; + elements[1] = &dart_object_endpoint_info; - Dart_CObject dart_object_found; - dart_object_found.type = Dart_CObject_kArray; - dart_object_found.value.as_array.length = 2; - dart_object_found.value.as_array.values = elements; - const bool result = - Dart_PostCObject_DL(listener_dart.found_cb, - &dart_object_found); - if (!result) { - NEARBY_LOG(INFO, - "Posting message to port failed."); - } - }; - listener.endpoint_lost_cb = - [listener_dart](const std::string& endpoint_id) { - NEARBY_LOG(INFO, "Device lost: id=%s", - endpoint_id.c_str()); - Dart_CObject dart_object_lost; - dart_object_lost.type = Dart_CObject_kString; - dart_object_lost.value.as_string = - (char *)endpoint_id.c_str(); - const bool result = - Dart_PostCObject_DL(listener_dart.lost_cb, - &dart_object_lost); - if (!result) { - NEARBY_LOG(INFO, - "Posting message to port failed."); - } - }; + Dart_CObject dart_object_found; + dart_object_found.type = Dart_CObject_kArray; + dart_object_found.value.as_array.length = 2; + dart_object_found.value.as_array.values = elements; + const bool result = + Dart_PostCObject_DL(listener_dart.found_cb, &dart_object_found); + if (!result) { + NEARBY_LOG(INFO, "Posting message to port failed."); + } + }; + listener.endpoint_lost_cb = [listener_dart](const std::string &endpoint_id) { + NEARBY_LOG(INFO, "Device lost: id=%s", endpoint_id.c_str()); + Dart_CObject dart_object_lost; + dart_object_lost.type = Dart_CObject_kString; + dart_object_lost.value.as_string = (char *)endpoint_id.c_str(); + const bool result = + Dart_PostCObject_DL(listener_dart.lost_cb, &dart_object_lost); + if (!result) { + NEARBY_LOG(INFO, "Posting message to port failed."); + } + }; listener.endpoint_distance_changed_cb = - [listener_dart](const std::string& endpoint_id, - DistanceInfo info) { - NEARBY_LOG(INFO, "Device distance changed: id=%s", - endpoint_id.c_str()); - Dart_CObject dart_object_distance_changed; - dart_object_distance_changed.type = - Dart_CObject_kString; - dart_object_distance_changed.value.as_string = - (char *)(endpoint_id.c_str()); - const bool result = - Dart_PostCObject_DL( - listener_dart.distance_changed_cb, - &dart_object_distance_changed); - if (!result) { - NEARBY_LOG(INFO, - "Posting message to port failed."); - } - }; + [listener_dart](const std::string &endpoint_id, DistanceInfo info) { + NEARBY_LOG(INFO, "Device distance changed: id=%s", endpoint_id.c_str()); + Dart_CObject dart_object_distance_changed; + dart_object_distance_changed.type = Dart_CObject_kString; + dart_object_distance_changed.value.as_string = + (char *)(endpoint_id.c_str()); + const bool result = Dart_PostCObject_DL( + listener_dart.distance_changed_cb, &dart_object_distance_changed); + if (!result) { + NEARBY_LOG(INFO, "Posting message to port failed."); + } + }; ResultCallback callback; SetResultCallback(callback, result_cb); StartDiscovery(pCore, service_id, std::move(options), listener, callback); } -void StopDiscoveryDart(Core* pCore, Dart_Port result_cb) { +void StopDiscoveryDart(Core *pCore, Dart_Port result_cb) { if (!pCore) { PostResult(result_cb, Status::Value::kError); return; @@ -296,11 +260,10 @@ void StopDiscoveryDart(Core* pCore, Dart_Port result_cb) { ResultCallback callback; SetResultCallback(callback, result_cb); - StopDiscovery(pCore, callback); + windows::StopDiscovery(pCore, callback); } -void RequestConnectionDart(Core* pCore, - const char* endpoint_id, +void RequestConnectionDart(Core *pCore, const char *endpoint_id, ConnectionOptionsDart options_dart, ConnectionRequestInfoDart info_dart, Dart_Port result_cb) { @@ -309,7 +272,7 @@ void RequestConnectionDart(Core* pCore, return; } - RequestConnectionOptions options; + windows::ConnectionOptions options; options.enforce_topology_constraints = false; options.allowed.bluetooth = options_dart.enable_bluetooth; options.allowed.ble = options_dart.enable_ble; @@ -318,94 +281,76 @@ void RequestConnectionDart(Core* pCore, ConnectionRequestInfo info; info.endpoint_info = ByteArray(info_dart.endpoint_info); - info.listener.initiated_cb = [info_dart](const std::string& endpoint_id, - const ConnectionResponseInfo& connection_info) { - NEARBY_LOG(INFO, "Advertising initiated: id=%s", - endpoint_id.c_str()); - Dart_CObject dart_object_initiated; - dart_object_initiated.type = Dart_CObject_kString; - dart_object_initiated.value.as_string = - (char *)connection_info.remote_endpoint_info.data(); - const bool result = - Dart_PostCObject_DL(info_dart.initiated_cb, - &dart_object_initiated); - if (!result) { - NEARBY_LOG(INFO, - "Posting message to port failed."); - } - }; - info.listener.accepted_cb = [info_dart](const std::string& endpoint_id) { - NEARBY_LOG(INFO, "Advertising accepted: id=%s", - endpoint_id.c_str()); - Dart_CObject dart_object_accepted; - dart_object_accepted.type = Dart_CObject_kString; - dart_object_accepted.value.as_string = - (char *)endpoint_id.c_str(); - const bool result = - Dart_PostCObject_DL(info_dart.accepted_cb, - &dart_object_accepted); - if (!result) { - NEARBY_LOG(INFO, - "Posting message to port failed."); - } - }; - info.listener.rejected_cb = [info_dart](const std::string& endpoint_id, - Status status) { - NEARBY_LOG(INFO, "Advertising rejected: id=%s", - endpoint_id.c_str()); - Dart_CObject dart_object_rejected; - dart_object_rejected.type = Dart_CObject_kString; - dart_object_rejected.value.as_string = - (char *)endpoint_id.c_str(); - const bool result = - Dart_PostCObject_DL(info_dart.rejected_cb, - &dart_object_rejected); - if (!result) { - NEARBY_LOG(INFO, - "Posting message to port failed."); - } - }; - info.listener.disconnected_cb = [info_dart]( - const std::string& endpoint_id) { - NEARBY_LOG(INFO, "Advertising disconnected: id=%s", - endpoint_id.c_str()); - Dart_CObject dart_object_disconnected; - dart_object_disconnected.type = Dart_CObject_kString; - dart_object_disconnected.value.as_string = - (char *)endpoint_id.c_str(); - const bool result = - Dart_PostCObject_DL(info_dart.disconnected_cb, - &dart_object_disconnected); - if (!result) { - NEARBY_LOG(INFO, - "Posting message to port failed."); - } - }; - info.listener.bandwidth_changed_cb = [info_dart]( - const std::string& endpoint_id, Medium medium) { - NEARBY_LOG(INFO, "Advertising bandwidth changed: id=%s", - endpoint_id.c_str()); - Dart_CObject dart_object_bandwidth_changed; + info.listener.initiated_cb = + [info_dart](const std::string &endpoint_id, + const ConnectionResponseInfo &connection_info) { + NEARBY_LOG(INFO, "Advertising initiated: id=%s", endpoint_id.c_str()); + Dart_CObject dart_object_initiated; + dart_object_initiated.type = Dart_CObject_kString; + dart_object_initiated.value.as_string = + (char *)connection_info.remote_endpoint_info.data(); + const bool result = + Dart_PostCObject_DL(info_dart.initiated_cb, &dart_object_initiated); + if (!result) { + NEARBY_LOG(INFO, "Posting message to port failed."); + } + }; + info.listener.accepted_cb = [info_dart](const std::string &endpoint_id) { + NEARBY_LOG(INFO, "Advertising accepted: id=%s", endpoint_id.c_str()); + Dart_CObject dart_object_accepted; + dart_object_accepted.type = Dart_CObject_kString; + dart_object_accepted.value.as_string = (char *)endpoint_id.c_str(); + const bool result = + Dart_PostCObject_DL(info_dart.accepted_cb, &dart_object_accepted); + if (!result) { + NEARBY_LOG(INFO, "Posting message to port failed."); + } + }; + info.listener.rejected_cb = [info_dart](const std::string &endpoint_id, + Status status) { + NEARBY_LOG(INFO, "Advertising rejected: id=%s", endpoint_id.c_str()); + Dart_CObject dart_object_rejected; + dart_object_rejected.type = Dart_CObject_kString; + dart_object_rejected.value.as_string = (char *)endpoint_id.c_str(); + const bool result = + Dart_PostCObject_DL(info_dart.rejected_cb, &dart_object_rejected); + if (!result) { + NEARBY_LOG(INFO, "Posting message to port failed."); + } + }; + info.listener.disconnected_cb = [info_dart](const std::string &endpoint_id) { + NEARBY_LOG(INFO, "Advertising disconnected: id=%s", endpoint_id.c_str()); + Dart_CObject dart_object_disconnected; + dart_object_disconnected.type = Dart_CObject_kString; + dart_object_disconnected.value.as_string = (char *)endpoint_id.c_str(); + const bool result = Dart_PostCObject_DL(info_dart.disconnected_cb, + &dart_object_disconnected); + if (!result) { + NEARBY_LOG(INFO, "Posting message to port failed."); + } + }; + info.listener.bandwidth_changed_cb = + [info_dart](const std::string &endpoint_id, Medium medium) { + NEARBY_LOG(INFO, "Advertising bandwidth changed: id=%s", + endpoint_id.c_str()); + Dart_CObject dart_object_bandwidth_changed; - dart_object_bandwidth_changed.type = Dart_CObject_kString; - dart_object_bandwidth_changed.value.as_string = - (char *)endpoint_id.c_str(); - const bool result = - Dart_PostCObject_DL(info_dart.bandwidth_changed_cb, - &dart_object_bandwidth_changed); - if (!result) { - NEARBY_LOG(INFO, - "Posting message to port failed."); - } - }; + dart_object_bandwidth_changed.type = Dart_CObject_kString; + dart_object_bandwidth_changed.value.as_string = + (char *)endpoint_id.c_str(); + const bool result = Dart_PostCObject_DL(info_dart.bandwidth_changed_cb, + &dart_object_bandwidth_changed); + if (!result) { + NEARBY_LOG(INFO, "Posting message to port failed."); + } + }; ResultCallback callback; SetResultCallback(callback, result_cb); RequestConnection(pCore, endpoint_id, info, std::move(options), callback); } -void AcceptConnectionDart(Core* pCore, - const char* endpoint_id, +void AcceptConnectionDart(Core *pCore, const char *endpoint_id, PayloadListenerDart listener_dart, Dart_Port result_cb) { if (!pCore) { @@ -414,43 +359,38 @@ void AcceptConnectionDart(Core* pCore, } PayloadListener listener; - listener.payload_cb = [listener_dart](const std::string& endpoint_id, - Payload payload) { - // TODO: pass payload in callback - Dart_CObject dart_object_payload; - dart_object_payload.type = Dart_CObject_kString; - dart_object_payload.value.as_string = - (char *)endpoint_id.data(); - const bool result = - Dart_PostCObject_DL(listener_dart.payload_cb, - &dart_object_payload); - if (!result) { - NEARBY_LOG(INFO, - "Posting message to port failed."); - } - }; + listener.payload_cb = [listener_dart](const std::string &endpoint_id, + Payload payload) { + // TODO: pass payload in callback + Dart_CObject dart_object_payload; + dart_object_payload.type = Dart_CObject_kString; + dart_object_payload.value.as_string = (char *)endpoint_id.data(); + const bool result = + Dart_PostCObject_DL(listener_dart.payload_cb, &dart_object_payload); + if (!result) { + NEARBY_LOG(INFO, "Posting message to port failed."); + } + }; listener.payload_progress_cb = [listener_dart]( - const std::string& endpoint_id, - const PayloadProgressInfo& info) { - // TODO: pass payload progress info in callback - Dart_CObject dart_object_payload; - dart_object_payload.type = Dart_CObject_kString; - dart_object_payload.value.as_string = - (char *)endpoint_id.data(); - const bool result = - Dart_PostCObject_DL(listener_dart.payload_cb, - &dart_object_payload); - if (!result) { - NEARBY_LOG(INFO, - "Posting message to port failed."); - } - }; + const std::string &endpoint_id, + const PayloadProgressInfo &info) { + // TODO: pass payload progress info in callback + Dart_CObject dart_object_payload; + dart_object_payload.type = Dart_CObject_kString; + dart_object_payload.value.as_string = (char *)endpoint_id.data(); + const bool result = + Dart_PostCObject_DL(listener_dart.payload_cb, &dart_object_payload); + if (!result) { + NEARBY_LOG(INFO, "Posting message to port failed."); + } + }; ResultCallback callback; SetResultCallback(callback, result_cb); - AcceptConnection(pCore, endpoint_id, listener, callback); + windows::AcceptConnection(pCore, endpoint_id, listener, callback); } +} // namespace windows } // namespace connections } // namespace nearby } // namespace location diff --git a/windows/dart/core_adapter_dart.h b/windows/dart/core_adapter_dart.h index 25a6072f..80acf1ff 100644 --- a/windows/dart/core_adapter_dart.h +++ b/windows/dart/core_adapter_dart.h @@ -20,6 +20,7 @@ namespace location { namespace nearby { namespace connections { +namespace windows { enum StrategyDart { P2P_CLUSTER = 0, @@ -43,7 +44,7 @@ struct ConnectionOptionsDart { }; struct ConnectionRequestInfoDart { - char* endpoint_info; + char *endpoint_info; int64_t initiated_cb; int64_t accepted_cb; int64_t rejected_cb; @@ -77,11 +78,9 @@ struct PayloadListenerDart { // Status::STATUS_ALREADY_ADVERTISING if the app is already advertising. // Status::STATUS_OUT_OF_ORDER_API_CALL if the app is currently // connected to remote endpoints; call StopAllEndpoints first. -DLL_EXPORT void __stdcall StartAdvertisingDart(Core* pCore, - const char* service_id, - ConnectionOptionsDart options_dart, - ConnectionRequestInfoDart info_dart, - Dart_Port result_cb); +DLL_EXPORT void __stdcall StartAdvertisingDart( + Core *pCore, const char *service_id, ConnectionOptionsDart options_dart, + ConnectionRequestInfoDart info_dart, Dart_Port result_cb); // Stops advertising a local endpoint. Should be called after calling // StartAdvertising, as soon as the application no longer needs to advertise @@ -91,8 +90,7 @@ DLL_EXPORT void __stdcall StartAdvertisingDart(Core* pCore, // result_cb - to access the status of the operation when available. // Possible status codes include: // Status::STATUS_OK if none of the above errors occurred. -DLL_EXPORT void __stdcall StopAdvertisingDart(Core* pCore, - Dart_Port result_cb); +DLL_EXPORT void __stdcall StopAdvertisingDart(Core *pCore, Dart_Port result_cb); // Starts discovery for remote endpoints with the specified service ID. // @@ -107,11 +105,9 @@ DLL_EXPORT void __stdcall StopAdvertisingDart(Core* pCore, // discovering the specified service. // Status::STATUS_OUT_OF_ORDER_API_CALL if the app is currently // connected to remote endpoints; call StopAllEndpoints first. -DLL_EXPORT void __stdcall StartDiscoveryDart(Core* pCore, - const char* service_id, - ConnectionOptionsDart options_dart, - DiscoveryListenerDart listener_dart, - Dart_Port result_cb); +DLL_EXPORT void __stdcall StartDiscoveryDart( + Core *pCore, const char *service_id, ConnectionOptionsDart options_dart, + DiscoveryListenerDart listener_dart, Dart_Port result_cb); // Stops discovery for remote endpoints, after a previous call to // StartDiscovery, when the client no longer needs to discover endpoints or @@ -121,8 +117,7 @@ DLL_EXPORT void __stdcall StartDiscoveryDart(Core* pCore, // result_cb - to access the status of the operation when available. // Possible status codes include: // Status::STATUS_OK if none of the above errors occurred. -DLL_EXPORT void __stdcall StopDiscoveryDart(Core* pCore, - Dart_Port result_cb); +DLL_EXPORT void __stdcall StopDiscoveryDart(Core *pCore, Dart_Port result_cb); // Sends a request to connect to a remote endpoint. // @@ -144,11 +139,9 @@ DLL_EXPORT void __stdcall StopDiscoveryDart(Core* pCore, // Status::STATUS_RADIO_ERROR if we failed to connect because of an // issue with Bluetooth/WiFi. // Status::STATUS_ERROR if we failed to connect for any other reason. -DLL_EXPORT void __stdcall RequestConnectionDart(Core* pCore, - const char* endpoint_id, - ConnectionOptionsDart options_dart, - ConnectionRequestInfoDart info_dart, - Dart_Port result_cb); +DLL_EXPORT void __stdcall RequestConnectionDart( + Core *pCore, const char *endpoint_id, ConnectionOptionsDart options_dart, + ConnectionRequestInfoDart info_dart, Dart_Port result_cb); // Accepts a connection to a remote endpoint. This method must be called // before Payloads can be exchanged with the remote endpoint. @@ -162,11 +155,11 @@ DLL_EXPORT void __stdcall RequestConnectionDart(Core* pCore, // Status::STATUS_OK if the connection request was accepted. // Status::STATUS_ALREADY_CONNECTED_TO_ENDPOINT if the app already. // has a connection to the specified endpoint. -DLL_EXPORT void __stdcall AcceptConnectionDart(Core* pCore, - const char* endpoint_id, - PayloadListenerDart listener_dart, - Dart_Port result_cb); +DLL_EXPORT void __stdcall AcceptConnectionDart( + Core *pCore, const char *endpoint_id, PayloadListenerDart listener_dart, + Dart_Port result_cb); +} // namespace windows } // namespace connections } // namespace nearby } // namespace location diff --git a/windows/request_connection_options.cc b/windows/discovery_options.cc similarity index 76% rename from windows/request_connection_options.cc rename to windows/discovery_options.cc index 7ea2a94c..919b3381 100644 --- a/windows/request_connection_options.cc +++ b/windows/discovery_options.cc @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "third_party/nearby_connections/windows/request_connection_options.h" +#include "third_party/nearby_connections/windows/discovery_options.h" #include "core/options.h" #include "core/strategy.h" @@ -22,10 +22,11 @@ namespace location { namespace nearby { namespace connections { +namespace windows { -RequestConnectionOptions::RequestConnectionOptions() +DiscoveryOptions::DiscoveryOptions() : impl_(new connections::ConnectionOptions(), - [](connections::ConnectionOptions* impl) { delete impl; }), + [](connections::ConnectionOptions *impl) { delete impl; }), strategy(impl_->strategy), allowed(impl_->allowed), auto_upgrade_bandwidth(impl_->auto_upgrade_bandwidth), @@ -40,33 +41,32 @@ RequestConnectionOptions::RequestConnectionOptions() keep_alive_interval_millis(impl_->keep_alive_interval_millis), keep_alive_timeout_millis(impl_->keep_alive_timeout_millis) {} -RequestConnectionOptions::operator connections::ConnectionOptions() const { +DiscoveryOptions::operator connections::ConnectionOptions() const { return *impl_.get(); } // Verify if ConnectionOptions is in a not-initialized (Empty) state. -bool RequestConnectionOptions::Empty() const { return impl_->Empty(); } +bool DiscoveryOptions::Empty() const { return impl_->Empty(); } // Bring ConnectionOptions to a not-initialized (Empty) state. -void RequestConnectionOptions::Clear() {} +void DiscoveryOptions::Clear() {} // Returns a copy and normalizes allowed mediums: // (1) If is_out_of_band_connection is true, verifies that there is only one // medium allowed, defaulting to only Bluetooth if unspecified. // (2) If no mediums are allowed, allow all mediums. -ConnectionOptions RequestConnectionOptions::CompatibleOptions() const { +connections::ConnectionOptions DiscoveryOptions::CompatibleOptions() const { return impl_->CompatibleOptions(); } -// std::vector GetMediums() const; - // This call follows the standard Microsoft calling pattern of calling first // to get the size of the array. Caller then allocates memory for the array, // and makes this call again to copy the array into the provided location. -void RequestConnectionOptions::GetMediums( - location::nearby::proto::connections::Medium* mediums, - uint32_t* mediumsSize) {} +void DiscoveryOptions::GetMediums( + location::nearby::proto::connections::Medium *mediums, + uint32_t *mediumsSize) {} +} // namespace windows } // namespace connections } // namespace nearby } // namespace location diff --git a/windows/start_discovery_options.h b/windows/discovery_options.h similarity index 70% rename from windows/start_discovery_options.h rename to windows/discovery_options.h index d1d616e0..8993825f 100644 --- a/windows/start_discovery_options.h +++ b/windows/discovery_options.h @@ -38,31 +38,32 @@ struct MediumSelector; using BooleanMediumSelector = MediumSelector; -struct DLL_API StartDiscoveryOptions { - StartDiscoveryOptions(); +namespace windows { +struct DLL_API DiscoveryOptions { + DiscoveryOptions(); operator connections::ConnectionOptions() const; private: std::unique_ptr + void (*)(connections::ConnectionOptions *)> impl_; public: - Strategy& strategy; - BooleanMediumSelector& allowed; + connections::Strategy &strategy; + connections::BooleanMediumSelector &allowed; - bool& auto_upgrade_bandwidth; - bool& enable_bluetooth_listening; - bool& enable_webrtc_listening; - bool& low_power; - bool& enforce_topology_constraints; + bool &auto_upgrade_bandwidth; + bool &enable_bluetooth_listening; + bool &enable_webrtc_listening; + bool &low_power; + bool &enforce_topology_constraints; // Whether this is intended to be used in conjunction with InjectEndpoint(). - bool& is_out_of_band_connection; - ByteArray& remote_bluetooth_mac_address; - const char* fast_advertisement_service_uuid; - int& keep_alive_interval_millis; - int& keep_alive_timeout_millis; + bool &is_out_of_band_connection; + ByteArray &remote_bluetooth_mac_address; + const char *fast_advertisement_service_uuid; + int &keep_alive_interval_millis; + int &keep_alive_timeout_millis; // Verify if ConnectionOptions is in a not-initialized (Empty) state. bool Empty() const; @@ -74,15 +75,16 @@ struct DLL_API StartDiscoveryOptions { // (1) If is_out_of_band_connection is true, verifies that there is only one // medium allowed, defaulting to only Bluetooth if unspecified. // (2) If no mediums are allowed, allow all mediums. - ConnectionOptions CompatibleOptions() const; + connections::ConnectionOptions CompatibleOptions() const; // This call follows the standard Microsoft calling pattern of calling first // to get the size of the array. Caller then allocates memory for the array, // and makes this call again to copy the array into the provided location. - void GetMediums(location::nearby::proto::connections::Medium* mediums, - uint32_t* mediumsSize); + void GetMediums(location::nearby::proto::connections::Medium *mediums, + uint32_t *mediumsSize); }; +} // namespace windows } // namespace connections } // namespace nearby } // namespace location