mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-14 14:46:12 -04:00
Update C wrapper to use pointers, instead of references, which is not supported by C.
PiperOrigin-RevId: 643505769
This commit is contained in:
+104
-100
@@ -17,6 +17,7 @@
|
||||
#include <stddef.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
@@ -144,7 +145,7 @@ NcContext* GetContext(NC_INSTANCE instance) {
|
||||
|
||||
connection_request_info.initiated_callback(
|
||||
instance, convertStringToInt(endpoint_id),
|
||||
connection_response_info);
|
||||
&connection_response_info);
|
||||
};
|
||||
|
||||
cpp_connection_listener.rejected_cb =
|
||||
@@ -186,9 +187,9 @@ void NcCloseService(NC_INSTANCE instance) {
|
||||
}
|
||||
|
||||
void NcStartAdvertising(
|
||||
NC_INSTANCE instance, const NC_DATA& service_id,
|
||||
const NC_ADVERTISING_OPTIONS& advertising_options,
|
||||
const NC_CONNECTION_REQUEST_INFO& connection_request_info,
|
||||
NC_INSTANCE instance, const NC_DATA* service_id,
|
||||
const NC_ADVERTISING_OPTIONS* advertising_options,
|
||||
const NC_CONNECTION_REQUEST_INFO* connection_request_info,
|
||||
NcCallbackResult result_callback) {
|
||||
NcContext* nc_context = GetContext(instance);
|
||||
if (nc_context == nullptr) {
|
||||
@@ -197,59 +198,60 @@ void NcStartAdvertising(
|
||||
}
|
||||
|
||||
::nearby::connections::ConnectionRequestInfo cpp_connection_request_info =
|
||||
GetCppConnectionRequestInfo(instance, connection_request_info);
|
||||
GetCppConnectionRequestInfo(instance, *connection_request_info);
|
||||
|
||||
::nearby::connections::AdvertisingOptions cpp_advertising_options;
|
||||
cpp_advertising_options.allowed.ble =
|
||||
advertising_options.common_options.allowed_mediums[NC_MEDIUM_BLE];
|
||||
advertising_options->common_options.allowed_mediums[NC_MEDIUM_BLE];
|
||||
cpp_advertising_options.allowed.bluetooth =
|
||||
advertising_options.common_options.allowed_mediums[NC_MEDIUM_BLUETOOTH];
|
||||
advertising_options->common_options.allowed_mediums[NC_MEDIUM_BLUETOOTH];
|
||||
cpp_advertising_options.allowed.wifi_lan =
|
||||
advertising_options.common_options.allowed_mediums[NC_MEDIUM_WIFI_LAN];
|
||||
advertising_options->common_options.allowed_mediums[NC_MEDIUM_WIFI_LAN];
|
||||
cpp_advertising_options.allowed.wifi_direct =
|
||||
advertising_options.common_options.allowed_mediums[NC_MEDIUM_WIFI_DIRECT];
|
||||
advertising_options->common_options
|
||||
.allowed_mediums[NC_MEDIUM_WIFI_DIRECT];
|
||||
cpp_advertising_options.allowed.wifi_hotspot =
|
||||
advertising_options.common_options
|
||||
advertising_options->common_options
|
||||
.allowed_mediums[NC_MEDIUM_WIFI_HOTSPOT];
|
||||
cpp_advertising_options.allowed.web_rtc =
|
||||
advertising_options.common_options.allowed_mediums[NC_MEDIUM_WEB_RTC];
|
||||
advertising_options->common_options.allowed_mediums[NC_MEDIUM_WEB_RTC];
|
||||
cpp_advertising_options.enable_bluetooth_listening =
|
||||
advertising_options.enable_bluetooth_listening;
|
||||
advertising_options->enable_bluetooth_listening;
|
||||
cpp_advertising_options.enable_webrtc_listening =
|
||||
advertising_options.enable_webrtc_listening;
|
||||
advertising_options->enable_webrtc_listening;
|
||||
cpp_advertising_options.auto_upgrade_bandwidth =
|
||||
advertising_options.auto_upgrade_bandwidth;
|
||||
advertising_options->auto_upgrade_bandwidth;
|
||||
cpp_advertising_options.enforce_topology_constraints =
|
||||
advertising_options.enforce_topology_constraints;
|
||||
if (advertising_options.fast_advertisement_service_uuid.size > 0) {
|
||||
advertising_options->enforce_topology_constraints;
|
||||
if (advertising_options->fast_advertisement_service_uuid.size > 0) {
|
||||
cpp_advertising_options.fast_advertisement_service_uuid =
|
||||
std::string(advertising_options.fast_advertisement_service_uuid.data,
|
||||
advertising_options.fast_advertisement_service_uuid.size);
|
||||
std::string(advertising_options->fast_advertisement_service_uuid.data,
|
||||
advertising_options->fast_advertisement_service_uuid.size);
|
||||
}
|
||||
|
||||
cpp_advertising_options.is_out_of_band_connection =
|
||||
advertising_options.is_out_of_band_connection;
|
||||
cpp_advertising_options.low_power = advertising_options.low_power;
|
||||
advertising_options->is_out_of_band_connection;
|
||||
cpp_advertising_options.low_power = advertising_options->low_power;
|
||||
|
||||
if (advertising_options.common_options.strategy.type ==
|
||||
if (advertising_options->common_options.strategy.type ==
|
||||
NC_STRATEGY_TYPE_NONE) {
|
||||
cpp_advertising_options.strategy = ::nearby::connections::Strategy::kNone;
|
||||
}
|
||||
if (advertising_options.common_options.strategy.type ==
|
||||
if (advertising_options->common_options.strategy.type ==
|
||||
NC_STRATEGY_TYPE_P2P_CLUSTER)
|
||||
cpp_advertising_options.strategy =
|
||||
::nearby::connections::Strategy::kP2pCluster;
|
||||
if (advertising_options.common_options.strategy.type ==
|
||||
if (advertising_options->common_options.strategy.type ==
|
||||
NC_STRATEGY_TYPE_P2P_POINT_TO_POINT)
|
||||
cpp_advertising_options.strategy =
|
||||
::nearby::connections::Strategy::kP2pPointToPoint;
|
||||
if (advertising_options.common_options.strategy.type ==
|
||||
if (advertising_options->common_options.strategy.type ==
|
||||
NC_STRATEGY_TYPE_P2P_STAR)
|
||||
cpp_advertising_options.strategy =
|
||||
::nearby::connections::Strategy::kP2pStar;
|
||||
|
||||
nc_context->core->StartAdvertising(
|
||||
std::string(service_id.data, service_id.size),
|
||||
std::string(service_id->data, service_id->size),
|
||||
std::move(cpp_advertising_options),
|
||||
std::move(cpp_connection_request_info),
|
||||
[=](::nearby::connections::Status status) {
|
||||
@@ -269,9 +271,9 @@ void NcStopAdvertising(NC_INSTANCE instance, NcCallbackResult result_callback) {
|
||||
});
|
||||
}
|
||||
|
||||
void NcStartDiscovery(NC_INSTANCE instance, const NC_DATA& service_id,
|
||||
const NC_DISCOVERY_OPTIONS& discovery_options,
|
||||
const NC_DISCOVERY_LISTENER& discovery_listener,
|
||||
void NcStartDiscovery(NC_INSTANCE instance, const NC_DATA* service_id,
|
||||
const NC_DISCOVERY_OPTIONS* discovery_options,
|
||||
const NC_DISCOVERY_LISTENER* discovery_listener,
|
||||
NcCallbackResult result_callback) {
|
||||
NcContext* nc_context = GetContext(instance);
|
||||
if (nc_context == nullptr) {
|
||||
@@ -281,69 +283,71 @@ void NcStartDiscovery(NC_INSTANCE instance, const NC_DATA& service_id,
|
||||
|
||||
::nearby::connections::DiscoveryOptions cpp_discovery_options;
|
||||
|
||||
if (discovery_options.common_options.strategy.type == NC_STRATEGY_TYPE_NONE)
|
||||
if (discovery_options->common_options.strategy.type == NC_STRATEGY_TYPE_NONE)
|
||||
cpp_discovery_options.strategy = ::nearby::connections::Strategy::kNone;
|
||||
if (discovery_options.common_options.strategy.type ==
|
||||
if (discovery_options->common_options.strategy.type ==
|
||||
NC_STRATEGY_TYPE_P2P_CLUSTER)
|
||||
cpp_discovery_options.strategy =
|
||||
::nearby::connections::Strategy::kP2pCluster;
|
||||
if (discovery_options.common_options.strategy.type ==
|
||||
if (discovery_options->common_options.strategy.type ==
|
||||
NC_STRATEGY_TYPE_P2P_POINT_TO_POINT)
|
||||
cpp_discovery_options.strategy =
|
||||
::nearby::connections::Strategy::kP2pPointToPoint;
|
||||
if (discovery_options.common_options.strategy.type ==
|
||||
if (discovery_options->common_options.strategy.type ==
|
||||
NC_STRATEGY_TYPE_P2P_STAR)
|
||||
cpp_discovery_options.strategy = ::nearby::connections::Strategy::kP2pStar;
|
||||
cpp_discovery_options.auto_upgrade_bandwidth =
|
||||
discovery_options.auto_upgrade_bandwidth;
|
||||
discovery_options->auto_upgrade_bandwidth;
|
||||
cpp_discovery_options.enforce_topology_constraints =
|
||||
discovery_options.enforce_topology_constraints;
|
||||
discovery_options->enforce_topology_constraints;
|
||||
cpp_discovery_options.is_out_of_band_connection =
|
||||
discovery_options.is_out_of_band_connection;
|
||||
if (discovery_options.fast_advertisement_service_uuid.size > 0) {
|
||||
discovery_options->is_out_of_band_connection;
|
||||
if (discovery_options->fast_advertisement_service_uuid.size > 0) {
|
||||
cpp_discovery_options.fast_advertisement_service_uuid =
|
||||
std::string(discovery_options.fast_advertisement_service_uuid.data,
|
||||
discovery_options.fast_advertisement_service_uuid.size);
|
||||
std::string(discovery_options->fast_advertisement_service_uuid.data,
|
||||
discovery_options->fast_advertisement_service_uuid.size);
|
||||
}
|
||||
cpp_discovery_options.allowed.bluetooth =
|
||||
discovery_options.common_options.allowed_mediums[NC_MEDIUM_BLUETOOTH];
|
||||
discovery_options->common_options.allowed_mediums[NC_MEDIUM_BLUETOOTH];
|
||||
cpp_discovery_options.allowed.ble =
|
||||
discovery_options.common_options.allowed_mediums[NC_MEDIUM_BLE];
|
||||
discovery_options->common_options.allowed_mediums[NC_MEDIUM_BLE];
|
||||
cpp_discovery_options.allowed.wifi_lan =
|
||||
discovery_options.common_options.allowed_mediums[NC_MEDIUM_WIFI_LAN];
|
||||
discovery_options->common_options.allowed_mediums[NC_MEDIUM_WIFI_LAN];
|
||||
cpp_discovery_options.allowed.wifi_hotspot =
|
||||
discovery_options.common_options.allowed_mediums[NC_MEDIUM_WIFI_HOTSPOT];
|
||||
discovery_options->common_options.allowed_mediums[NC_MEDIUM_WIFI_HOTSPOT];
|
||||
cpp_discovery_options.allowed.web_rtc =
|
||||
discovery_options.common_options.allowed_mediums[NC_MEDIUM_WEB_RTC];
|
||||
discovery_options->common_options.allowed_mediums[NC_MEDIUM_WEB_RTC];
|
||||
|
||||
::nearby::connections::DiscoveryListener listener;
|
||||
listener.endpoint_distance_changed_cb =
|
||||
[=](const std::string& endpoint_id,
|
||||
::nearby::connections::DistanceInfo info) {
|
||||
discovery_listener.endpoint_distance_changed_callback(
|
||||
discovery_listener->endpoint_distance_changed_callback(
|
||||
instance, convertStringToInt(endpoint_id),
|
||||
static_cast<NC_DISTANCE_INFO>(info));
|
||||
};
|
||||
listener.endpoint_found_cb = [=](const std::string& endpoint_id,
|
||||
const nearby::ByteArray& endpoint_info,
|
||||
const std::string& service_id) {
|
||||
discovery_listener.endpoint_found_callback(
|
||||
instance, convertStringToInt(endpoint_id),
|
||||
NC_DATA{.size = static_cast<int64_t>(endpoint_info.size()),
|
||||
.data = (char*)endpoint_info.data()},
|
||||
NC_DATA{.size = static_cast<int64_t>(service_id.size()),
|
||||
.data = (char*)service_id.data()});
|
||||
NC_DATA endpoint_info_data = {
|
||||
.size = static_cast<int64_t>(endpoint_info.size()),
|
||||
.data = (char*)endpoint_info.data()};
|
||||
NC_DATA service_id_data = {.size = static_cast<int64_t>(service_id.size()),
|
||||
.data = (char*)service_id.data()};
|
||||
discovery_listener->endpoint_found_callback(
|
||||
instance, convertStringToInt(endpoint_id), &endpoint_info_data,
|
||||
&service_id_data);
|
||||
};
|
||||
|
||||
listener.endpoint_lost_cb = [=](const std::string& endpoint_id) {
|
||||
discovery_listener.endpoint_lost_callback(instance,
|
||||
convertStringToInt(endpoint_id));
|
||||
discovery_listener->endpoint_lost_callback(instance,
|
||||
convertStringToInt(endpoint_id));
|
||||
};
|
||||
|
||||
nc_context->core->StartDiscovery(
|
||||
std::string(service_id.data, service_id.size),
|
||||
std::string(service_id->data, service_id->size),
|
||||
std::move(cpp_discovery_options), std::move(listener),
|
||||
[=](::nearby::connections::Status status) {
|
||||
[result_callback =
|
||||
std::move(result_callback)](::nearby::connections::Status status) {
|
||||
result_callback(static_cast<NC_STATUS>(status.value));
|
||||
});
|
||||
}
|
||||
@@ -360,8 +364,8 @@ void NcStopDiscovery(NC_INSTANCE instance, NcCallbackResult result_callback) {
|
||||
});
|
||||
}
|
||||
|
||||
void NcInjectEndpoint(NC_INSTANCE instance, const NC_DATA& service_id,
|
||||
const NC_OUT_OF_BAND_CONNECTION_METADATA& metadata,
|
||||
void NcInjectEndpoint(NC_INSTANCE instance, const NC_DATA* service_id,
|
||||
const NC_OUT_OF_BAND_CONNECTION_METADATA* metadata,
|
||||
NcCallbackResult result_callback) {
|
||||
NcContext* nc_context = GetContext(instance);
|
||||
if (nc_context == nullptr) {
|
||||
@@ -371,18 +375,18 @@ void NcInjectEndpoint(NC_INSTANCE instance, const NC_DATA& service_id,
|
||||
|
||||
::nearby::connections::OutOfBandConnectionMetadata
|
||||
cpp_out_of_band_connection_metadata;
|
||||
cpp_out_of_band_connection_metadata.endpoint_id = metadata.endpoint_id;
|
||||
cpp_out_of_band_connection_metadata.endpoint_id = metadata->endpoint_id;
|
||||
cpp_out_of_band_connection_metadata.endpoint_info = {
|
||||
metadata.endpoint_info.data,
|
||||
static_cast<size_t>(metadata.endpoint_info.size)};
|
||||
metadata->endpoint_info.data,
|
||||
static_cast<size_t>(metadata->endpoint_info.size)};
|
||||
cpp_out_of_band_connection_metadata.medium =
|
||||
static_cast<::nearby::connections::Medium>(metadata.medium);
|
||||
static_cast<::nearby::connections::Medium>(metadata->medium);
|
||||
cpp_out_of_band_connection_metadata.remote_bluetooth_mac_address = {
|
||||
metadata.remote_bluetooth_mac_address.data,
|
||||
static_cast<size_t>(metadata.remote_bluetooth_mac_address.size)};
|
||||
metadata->remote_bluetooth_mac_address.data,
|
||||
static_cast<size_t>(metadata->remote_bluetooth_mac_address.size)};
|
||||
|
||||
nc_context->core->InjectEndpoint(
|
||||
std::string(service_id.data, service_id.size),
|
||||
std::string(service_id->data, service_id->size),
|
||||
cpp_out_of_band_connection_metadata,
|
||||
[=](::nearby::connections::Status status) {
|
||||
result_callback(static_cast<NC_STATUS>(status.value));
|
||||
@@ -391,8 +395,8 @@ void NcInjectEndpoint(NC_INSTANCE instance, const NC_DATA& service_id,
|
||||
|
||||
void NcRequestConnection(
|
||||
NC_INSTANCE instance, int endpoint_id,
|
||||
const NC_CONNECTION_REQUEST_INFO& connection_request_info,
|
||||
const NC_CONNECTION_OPTIONS& connection_options,
|
||||
const NC_CONNECTION_REQUEST_INFO* connection_request_info,
|
||||
const NC_CONNECTION_OPTIONS* connection_options,
|
||||
NcCallbackResult result_callback) {
|
||||
NcContext* nc_context = GetContext(instance);
|
||||
if (nc_context == nullptr) {
|
||||
@@ -401,50 +405,50 @@ void NcRequestConnection(
|
||||
}
|
||||
|
||||
::nearby::connections::ConnectionRequestInfo cpp_connection_request_info =
|
||||
GetCppConnectionRequestInfo(instance, connection_request_info);
|
||||
GetCppConnectionRequestInfo(instance, *connection_request_info);
|
||||
|
||||
::nearby::connections::ConnectionOptions cpp_connection_options;
|
||||
cpp_connection_options.allowed.ble =
|
||||
connection_options.common_options.allowed_mediums[NC_MEDIUM_BLE];
|
||||
connection_options->common_options.allowed_mediums[NC_MEDIUM_BLE];
|
||||
cpp_connection_options.allowed.bluetooth =
|
||||
connection_options.common_options.allowed_mediums[NC_MEDIUM_BLUETOOTH];
|
||||
connection_options->common_options.allowed_mediums[NC_MEDIUM_BLUETOOTH];
|
||||
cpp_connection_options.allowed.web_rtc =
|
||||
connection_options.common_options.allowed_mediums[NC_MEDIUM_WEB_RTC];
|
||||
connection_options->common_options.allowed_mediums[NC_MEDIUM_WEB_RTC];
|
||||
cpp_connection_options.allowed.wifi_lan =
|
||||
connection_options.common_options.allowed_mediums[NC_MEDIUM_WIFI_LAN];
|
||||
connection_options->common_options.allowed_mediums[NC_MEDIUM_WIFI_LAN];
|
||||
cpp_connection_options.auto_upgrade_bandwidth =
|
||||
connection_options.auto_upgrade_bandwidth;
|
||||
connection_options->auto_upgrade_bandwidth;
|
||||
cpp_connection_options.enforce_topology_constraints =
|
||||
connection_options.enforce_topology_constraints;
|
||||
if (connection_options.fast_advertisement_service_uuid.size > 0) {
|
||||
connection_options->enforce_topology_constraints;
|
||||
if (connection_options->fast_advertisement_service_uuid.size > 0) {
|
||||
cpp_connection_options.fast_advertisement_service_uuid =
|
||||
std::string(connection_options.fast_advertisement_service_uuid.data,
|
||||
connection_options.fast_advertisement_service_uuid.size);
|
||||
std::string(connection_options->fast_advertisement_service_uuid.data,
|
||||
connection_options->fast_advertisement_service_uuid.size);
|
||||
}
|
||||
cpp_connection_options.is_out_of_band_connection =
|
||||
connection_options.is_out_of_band_connection;
|
||||
connection_options->is_out_of_band_connection;
|
||||
cpp_connection_options.keep_alive_interval_millis =
|
||||
connection_options.keep_alive_interval_millis;
|
||||
connection_options->keep_alive_interval_millis;
|
||||
cpp_connection_options.keep_alive_timeout_millis =
|
||||
connection_options.keep_alive_timeout_millis;
|
||||
cpp_connection_options.low_power = connection_options.low_power;
|
||||
if (connection_options.remote_bluetooth_mac_address.size > 0) {
|
||||
connection_options->keep_alive_timeout_millis;
|
||||
cpp_connection_options.low_power = connection_options->low_power;
|
||||
if (connection_options->remote_bluetooth_mac_address.size > 0) {
|
||||
cpp_connection_options.remote_bluetooth_mac_address =
|
||||
nearby::BluetoothUtils::FromString(
|
||||
std::string(connection_options.remote_bluetooth_mac_address.data,
|
||||
connection_options.remote_bluetooth_mac_address.size));
|
||||
std::string(connection_options->remote_bluetooth_mac_address.data,
|
||||
connection_options->remote_bluetooth_mac_address.size));
|
||||
}
|
||||
if (connection_options.common_options.strategy.type == NC_STRATEGY_TYPE_NONE)
|
||||
if (connection_options->common_options.strategy.type == NC_STRATEGY_TYPE_NONE)
|
||||
cpp_connection_options.strategy = ::nearby::connections::Strategy::kNone;
|
||||
if (connection_options.common_options.strategy.type ==
|
||||
if (connection_options->common_options.strategy.type ==
|
||||
NC_STRATEGY_TYPE_P2P_CLUSTER)
|
||||
cpp_connection_options.strategy =
|
||||
::nearby::connections::Strategy::kP2pCluster;
|
||||
if (connection_options.common_options.strategy.type ==
|
||||
if (connection_options->common_options.strategy.type ==
|
||||
NC_STRATEGY_TYPE_P2P_POINT_TO_POINT)
|
||||
cpp_connection_options.strategy =
|
||||
::nearby::connections::Strategy::kP2pPointToPoint;
|
||||
if (connection_options.common_options.strategy.type ==
|
||||
if (connection_options->common_options.strategy.type ==
|
||||
NC_STRATEGY_TYPE_P2P_STAR)
|
||||
cpp_connection_options.strategy = ::nearby::connections::Strategy::kP2pStar;
|
||||
|
||||
@@ -488,7 +492,7 @@ void NcAcceptConnection(NC_INSTANCE instance, int endpoint_id,
|
||||
}
|
||||
|
||||
payload_listener.received_callback(
|
||||
instance, convertStringToInt(endpoint_id), nc_payload);
|
||||
instance, convertStringToInt(endpoint_id), &nc_payload);
|
||||
};
|
||||
|
||||
cpp_payload_listener.payload_progress_cb =
|
||||
@@ -502,7 +506,7 @@ void NcAcceptConnection(NC_INSTANCE instance, int endpoint_id,
|
||||
static_cast<NC_PAYLOAD_PROGRESS_INFO_STATUS>(progress.status);
|
||||
payload_listener.progress_updated_callback(
|
||||
instance, convertStringToInt(endpoint_id),
|
||||
nc_payload_progress_info);
|
||||
&nc_payload_progress_info);
|
||||
};
|
||||
|
||||
nc_context->core->AcceptConnection(
|
||||
@@ -528,7 +532,7 @@ void NcRejectConnection(NC_INSTANCE instance, int endpoint_id,
|
||||
}
|
||||
|
||||
void NcSendPayload(NC_INSTANCE instance, size_t endpoint_ids_size,
|
||||
const int* endpoint_ids, const NC_PAYLOAD& payload,
|
||||
const int* endpoint_ids, const NC_PAYLOAD* payload,
|
||||
NcCallbackResult result_callback) {
|
||||
NcContext* nc_context = GetContext(instance);
|
||||
if (nc_context == nullptr) {
|
||||
@@ -544,25 +548,25 @@ void NcSendPayload(NC_INSTANCE instance, size_t endpoint_ids_size,
|
||||
absl::Span<const std::string> endpoint_ids_span(endpoint_ids_vector.data(),
|
||||
endpoint_ids_size);
|
||||
::nearby::connections::Payload cpp_payload;
|
||||
if (payload.type == NC_PAYLOAD_TYPE_BYTES) {
|
||||
if (payload->type == NC_PAYLOAD_TYPE_BYTES) {
|
||||
cpp_payload = ::nearby::connections::Payload(
|
||||
payload.id, nearby::ByteArray(payload.content.bytes.content.data,
|
||||
payload.content.bytes.content.size));
|
||||
} else if (payload.type == NC_PAYLOAD_TYPE_FILE) {
|
||||
payload->id, nearby::ByteArray(payload->content.bytes.content.data,
|
||||
payload->content.bytes.content.size));
|
||||
} else if (payload->type == NC_PAYLOAD_TYPE_FILE) {
|
||||
// get file size
|
||||
std::string full_file_name = "";
|
||||
if (payload.content.file.parent_folder == nullptr) {
|
||||
full_file_name = payload.content.file.file_name;
|
||||
if (payload->content.file.parent_folder == nullptr) {
|
||||
full_file_name = payload->content.file.file_name;
|
||||
} else {
|
||||
full_file_name = absl::StrCat(payload.content.file.parent_folder, "/",
|
||||
payload.content.file.file_name);
|
||||
full_file_name = absl::StrCat(payload->content.file.parent_folder, "/",
|
||||
payload->content.file.file_name);
|
||||
}
|
||||
|
||||
nearby::InputFile input_file(full_file_name,
|
||||
getFileSize(full_file_name.c_str()));
|
||||
cpp_payload =
|
||||
::nearby::connections::Payload(payload.id, std::move(input_file));
|
||||
} else if (payload.type == NC_PAYLOAD_TYPE_STREAM) {
|
||||
::nearby::connections::Payload(payload->id, std::move(input_file));
|
||||
} else if (payload->type == NC_PAYLOAD_TYPE_STREAM) {
|
||||
// TODO(guogang): support stream later.
|
||||
}
|
||||
|
||||
|
||||
+11
-11
@@ -37,9 +37,9 @@ NC_API void NcCloseService(NC_INSTANCE instance);
|
||||
// and listeners.
|
||||
// result_callback - The result of the API operation.
|
||||
NC_API void NcStartAdvertising(
|
||||
NC_INSTANCE instance, const NC_DATA& service_id,
|
||||
const NC_ADVERTISING_OPTIONS& advertising_options,
|
||||
const NC_CONNECTION_REQUEST_INFO& connection_request_info,
|
||||
NC_INSTANCE instance, const NC_DATA* service_id,
|
||||
const NC_ADVERTISING_OPTIONS* advertising_options,
|
||||
const NC_CONNECTION_REQUEST_INFO* connection_request_info,
|
||||
NcCallbackResult result_callback);
|
||||
|
||||
// Stops advertising a local endpoint. It should be called after calling
|
||||
@@ -58,9 +58,9 @@ NC_API void NcStopAdvertising(NC_INSTANCE instance,
|
||||
// discovery_listener - The callbacks notified when a remote endpoint is
|
||||
// reported.
|
||||
// result_callback - The result of the API operation.
|
||||
NC_API void NcStartDiscovery(NC_INSTANCE instance, const NC_DATA& service_id,
|
||||
const NC_DISCOVERY_OPTIONS& discovery_options,
|
||||
const NC_DISCOVERY_LISTENER& discovery_listener,
|
||||
NC_API void NcStartDiscovery(NC_INSTANCE instance, const NC_DATA* service_id,
|
||||
const NC_DISCOVERY_OPTIONS* discovery_options,
|
||||
const NC_DISCOVERY_LISTENER* discovery_listener,
|
||||
NcCallbackResult result_callback);
|
||||
|
||||
// Stops discovering for a running discovery.
|
||||
@@ -79,8 +79,8 @@ NC_API void NcStopDiscovery(NC_INSTANCE instance,
|
||||
// corresponding call to NcStartDiscovery().
|
||||
// metadata - Metadata used in order to inject the endpoint.
|
||||
// result_callback - The result of the API operation.
|
||||
NC_API void NcInjectEndpoint(NC_INSTANCE instance, const NC_DATA& service_id,
|
||||
const NC_OUT_OF_BAND_CONNECTION_METADATA& metadata,
|
||||
NC_API void NcInjectEndpoint(NC_INSTANCE instance, const NC_DATA* service_id,
|
||||
const NC_OUT_OF_BAND_CONNECTION_METADATA* metadata,
|
||||
NcCallbackResult result_callback);
|
||||
|
||||
// Sends a request to connect to a remote endpoint.
|
||||
@@ -93,8 +93,8 @@ NC_API void NcInjectEndpoint(NC_INSTANCE instance, const NC_DATA& service_id,
|
||||
// result_callback - The result of the API operation.
|
||||
NC_API void NcRequestConnection(
|
||||
NC_INSTANCE instance, int endpoint_id,
|
||||
const NC_CONNECTION_REQUEST_INFO& connection_request_info,
|
||||
const NC_CONNECTION_OPTIONS& connection_options,
|
||||
const NC_CONNECTION_REQUEST_INFO* connection_request_info,
|
||||
const NC_CONNECTION_OPTIONS* connection_options,
|
||||
NcCallbackResult result_callback);
|
||||
|
||||
// Accepts a connection to a remote endpoint.
|
||||
@@ -124,7 +124,7 @@ NC_API void NcRejectConnection(NC_INSTANCE instance, int endpoint_id,
|
||||
// payload - the payload will be sent.
|
||||
// result_callback - The result of the API operation.
|
||||
NC_API void NcSendPayload(NC_INSTANCE instance, size_t endpoint_ids_size,
|
||||
const int* endpoint_ids, const NC_PAYLOAD& payload,
|
||||
const int* endpoint_ids, const NC_PAYLOAD* payload,
|
||||
NcCallbackResult result_callback);
|
||||
|
||||
// Cancels a Payload currently in-flight to or from remote endpoint(s).
|
||||
|
||||
+13
-16
@@ -18,9 +18,6 @@
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif // __cplusplus
|
||||
@@ -152,23 +149,23 @@ typedef struct NC_ADVERTISING_OPTIONS {
|
||||
|
||||
typedef struct NC_CONNECTION_OPTIONS {
|
||||
NC_COMMON_OPTIONS common_options;
|
||||
bool auto_upgrade_bandwidth = true;
|
||||
bool auto_upgrade_bandwidth;
|
||||
bool enforce_topology_constraints;
|
||||
bool low_power;
|
||||
bool is_out_of_band_connection = false;
|
||||
bool is_out_of_band_connection;
|
||||
NC_DATA remote_bluetooth_mac_address;
|
||||
NC_DATA fast_advertisement_service_uuid;
|
||||
int keep_alive_interval_millis = 0;
|
||||
int keep_alive_timeout_millis = 0;
|
||||
int keep_alive_interval_millis;
|
||||
int keep_alive_timeout_millis;
|
||||
} NC_CONNECTION_OPTIONS, *PNC_CONNECTION_OPTIONS;
|
||||
|
||||
typedef struct NC_DISCOVERY_OPTIONS {
|
||||
NC_COMMON_OPTIONS common_options;
|
||||
bool auto_upgrade_bandwidth = true;
|
||||
bool auto_upgrade_bandwidth;
|
||||
bool enforce_topology_constraints;
|
||||
|
||||
// Whether this is intended to be used in conjunction with InjectEndpoint().
|
||||
bool is_out_of_band_connection = false;
|
||||
bool is_out_of_band_connection;
|
||||
NC_DATA fast_advertisement_service_uuid;
|
||||
} NC_DISCOVERY_OPTIONS, *PNC_DISCOVERY_OPTIONS;
|
||||
|
||||
@@ -176,8 +173,8 @@ typedef struct NC_CONNECTION_RESPONSE_INFO {
|
||||
NC_DATA remote_endpoint_info;
|
||||
NC_DATA authentication_token;
|
||||
NC_DATA raw_authentication_token;
|
||||
bool is_incoming_connection = false;
|
||||
bool is_connection_verified = false;
|
||||
bool is_incoming_connection;
|
||||
bool is_connection_verified;
|
||||
} NC_CONNECTION_RESPONSE_INFO, *PNC_CONNECTION_RESPONSE_INFO;
|
||||
|
||||
// Defines callbacks in Nearby Connections.
|
||||
@@ -186,7 +183,7 @@ typedef void (*NcCallbackResult)(NC_STATUS status);
|
||||
|
||||
typedef void (*NcCallbackConnectionInitiated)(
|
||||
NC_INSTANCE instance, int endpoint_id,
|
||||
const NC_CONNECTION_RESPONSE_INFO& info);
|
||||
const NC_CONNECTION_RESPONSE_INFO* info);
|
||||
typedef void (*NcCallbackConnectionAccepted)(NC_INSTANCE instance,
|
||||
int endpoint_id);
|
||||
typedef void (*NcCallbackConnectionRejected)(NC_INSTANCE instance,
|
||||
@@ -208,8 +205,8 @@ typedef struct NC_CONNECTION_REQUEST_INFO {
|
||||
|
||||
typedef void (*NcCallbackDiscoveryEndpointFound)(NC_INSTANCE instance,
|
||||
int endpoint_id,
|
||||
const NC_DATA& endpoint_info,
|
||||
const NC_DATA& service_id);
|
||||
const NC_DATA* endpoint_info,
|
||||
const NC_DATA* service_id);
|
||||
typedef void (*NcCallbackDiscoveryEndpointLost)(NC_INSTANCE instance,
|
||||
int endpoint_id);
|
||||
typedef void (*NcCallbackDiscoveryEndpointDistanceChanged)(
|
||||
@@ -271,10 +268,10 @@ typedef struct NC_PAYLOAD_PROGRESS_INFO {
|
||||
} NC_PAYLOAD_PROGRESS_INFO;
|
||||
|
||||
typedef void (*NcCallbackPayloadReceived)(NC_INSTANCE instance, int endpoint_id,
|
||||
const NC_PAYLOAD& payload);
|
||||
const NC_PAYLOAD* payload);
|
||||
typedef void (*NcCallbackPayloadProgressUpdated)(
|
||||
NC_INSTANCE instance, int endpoint_id,
|
||||
const NC_PAYLOAD_PROGRESS_INFO& info);
|
||||
const NC_PAYLOAD_PROGRESS_INFO* info);
|
||||
|
||||
typedef struct NC_PAYLOAD_LISTENER {
|
||||
NcCallbackPayloadReceived received_callback;
|
||||
|
||||
@@ -93,7 +93,7 @@ std::string GetEndpointIdString(int endpoint_id) {
|
||||
|
||||
void ListenerInitiatedCB(
|
||||
NC_INSTANCE instance, int endpoint_id,
|
||||
const NC_CONNECTION_RESPONSE_INFO &connection_response_info) {
|
||||
const NC_CONNECTION_RESPONSE_INFO *connection_response_info) {
|
||||
NEARBY_LOG(INFO, "Advertising initiated: id=%s",
|
||||
GetEndpointIdString(endpoint_id).c_str());
|
||||
|
||||
@@ -106,9 +106,9 @@ void ListenerInitiatedCB(
|
||||
.value = {.as_typed_data{
|
||||
.type = Dart_TypedData_Type::Dart_TypedData_kUint8,
|
||||
.length =
|
||||
(intptr_t)connection_response_info.remote_endpoint_info.size,
|
||||
(intptr_t)connection_response_info->remote_endpoint_info.size,
|
||||
.values =
|
||||
(uint8_t *)connection_response_info.remote_endpoint_info.data}}};
|
||||
(uint8_t *)connection_response_info->remote_endpoint_info.data}}};
|
||||
|
||||
Dart_CObject *elements[2];
|
||||
elements[0] = &dart_object_endpoint_id;
|
||||
@@ -187,15 +187,15 @@ void ListenerBandwidthChangedCB(NC_INSTANCE instance, int endpoint_id,
|
||||
}
|
||||
|
||||
void ListenerEndpointFoundCB(NC_INSTANCE instance, int endpoint_id,
|
||||
const NC_DATA &endpoint_info,
|
||||
const NC_DATA &service_id) {
|
||||
const NC_DATA *endpoint_info,
|
||||
const NC_DATA *service_id) {
|
||||
NEARBY_LOG(INFO, "Device discovered: id=%s",
|
||||
GetEndpointIdString(endpoint_id).c_str());
|
||||
NEARBY_LOG(INFO, "Device discovered: service_id=%s",
|
||||
std::string(service_id.data, service_id.size).c_str());
|
||||
std::string(service_id->data, service_id->size).c_str());
|
||||
|
||||
std::string endpoint_info_str = absl::BytesToHexString(
|
||||
absl::string_view(endpoint_info.data, endpoint_info.size));
|
||||
absl::string_view(endpoint_info->data, endpoint_info->size));
|
||||
NEARBY_LOG(INFO, "Device discovered: info=%s", endpoint_info_str.c_str());
|
||||
|
||||
Dart_CObject dart_object_endpoint_id = {
|
||||
@@ -206,8 +206,8 @@ void ListenerEndpointFoundCB(NC_INSTANCE instance, int endpoint_id,
|
||||
.type = Dart_CObject_Type::Dart_CObject_kTypedData,
|
||||
.value = {
|
||||
.as_typed_data{.type = Dart_TypedData_Type::Dart_TypedData_kUint8,
|
||||
.length = (intptr_t)endpoint_info.size,
|
||||
.values = (uint8_t *)endpoint_info.data}}};
|
||||
.length = (intptr_t)endpoint_info->size,
|
||||
.values = (uint8_t *)endpoint_info->data}}};
|
||||
|
||||
Dart_CObject *elements[2];
|
||||
elements[0] = &dart_object_endpoint_id;
|
||||
@@ -256,12 +256,12 @@ void ListenerEndpointDistanceChangedCB(NC_INSTANCE instance, int endpoint_id,
|
||||
}
|
||||
|
||||
void ListenerPayloadCB(NC_INSTANCE instance, int endpoint_id,
|
||||
const NC_PAYLOAD &payload) {
|
||||
const NC_PAYLOAD *payload) {
|
||||
NEARBY_LOG(INFO,
|
||||
"Payload callback called. id: %s, "
|
||||
"payload_id: %d, type: %d",
|
||||
GetEndpointIdString(endpoint_id).c_str(), payload.id,
|
||||
payload.type);
|
||||
GetEndpointIdString(endpoint_id).c_str(), payload->id,
|
||||
payload->type);
|
||||
|
||||
Dart_CObject dart_object_endpoint_id;
|
||||
dart_object_endpoint_id.type = Dart_CObject_kInt32;
|
||||
@@ -269,12 +269,12 @@ void ListenerPayloadCB(NC_INSTANCE instance, int endpoint_id,
|
||||
|
||||
Dart_CObject dart_object_payload_id;
|
||||
dart_object_payload_id.type = Dart_CObject_kInt64;
|
||||
dart_object_payload_id.value.as_int64 = payload.id;
|
||||
dart_object_payload_id.value.as_int64 = payload->id;
|
||||
|
||||
switch (payload.type) {
|
||||
switch (payload->type) {
|
||||
case NC_PAYLOAD_TYPE_BYTES: {
|
||||
const char *bytes = payload.content.bytes.content.data;
|
||||
size_t bytes_size = payload.content.bytes.content.size;
|
||||
const char *bytes = payload->content.bytes.content.data;
|
||||
size_t bytes_size = payload->content.bytes.content.size;
|
||||
|
||||
if (bytes_size == 0) {
|
||||
NEARBY_LOG(INFO, "Failed to get the payload as bytes.");
|
||||
@@ -326,9 +326,9 @@ void ListenerPayloadCB(NC_INSTANCE instance, int endpoint_id,
|
||||
case NC_PAYLOAD_TYPE_FILE: {
|
||||
Dart_CObject dart_object_offset;
|
||||
dart_object_offset.type = Dart_CObject_kInt64;
|
||||
dart_object_offset.value.as_int64 = payload.content.file.offset;
|
||||
dart_object_offset.value.as_int64 = payload->content.file.offset;
|
||||
|
||||
std::string path = payload.content.file.file_name;
|
||||
std::string path = payload->content.file.file_name;
|
||||
Dart_CObject dart_object_path;
|
||||
dart_object_path.type = Dart_CObject_kString;
|
||||
dart_object_path.value.as_string = const_cast<char *>(path.c_str());
|
||||
@@ -359,33 +359,34 @@ void ListenerPayloadCB(NC_INSTANCE instance, int endpoint_id,
|
||||
|
||||
void ListenerPayloadProgressCB(
|
||||
NC_INSTANCE instance, int endpoint_id,
|
||||
const NC_PAYLOAD_PROGRESS_INFO &payload_progress_info) {
|
||||
const NC_PAYLOAD_PROGRESS_INFO *payload_progress_info) {
|
||||
NEARBY_LOG(INFO,
|
||||
"Payload progress callback called. id: %s, "
|
||||
"payload_id: %d, bytes transferred: %d, total: %d, status: %d",
|
||||
GetEndpointIdString(endpoint_id).c_str(), payload_progress_info.id,
|
||||
payload_progress_info.bytes_transferred,
|
||||
payload_progress_info.total_bytes, payload_progress_info.status);
|
||||
GetEndpointIdString(endpoint_id).c_str(),
|
||||
payload_progress_info->id,
|
||||
payload_progress_info->bytes_transferred,
|
||||
payload_progress_info->total_bytes, payload_progress_info->status);
|
||||
Dart_CObject dart_object_endpoint_id;
|
||||
dart_object_endpoint_id.type = Dart_CObject_kInt32;
|
||||
dart_object_endpoint_id.value.as_int32 = endpoint_id;
|
||||
|
||||
Dart_CObject dart_object_payload_id;
|
||||
dart_object_payload_id.type = Dart_CObject_kInt64;
|
||||
dart_object_payload_id.value.as_int64 = payload_progress_info.id;
|
||||
dart_object_payload_id.value.as_int64 = payload_progress_info->id;
|
||||
|
||||
Dart_CObject dart_object_bytes_transferred;
|
||||
dart_object_bytes_transferred.type = Dart_CObject_kInt64;
|
||||
dart_object_bytes_transferred.value.as_int64 =
|
||||
payload_progress_info.bytes_transferred;
|
||||
payload_progress_info->bytes_transferred;
|
||||
|
||||
Dart_CObject dart_object_total_bytes;
|
||||
dart_object_total_bytes.type = Dart_CObject_kInt64;
|
||||
dart_object_total_bytes.value.as_int64 = payload_progress_info.total_bytes;
|
||||
dart_object_total_bytes.value.as_int64 = payload_progress_info->total_bytes;
|
||||
|
||||
Dart_CObject dart_object_status;
|
||||
dart_object_status.type = Dart_CObject_kInt64;
|
||||
dart_object_status.value.as_int64 = (int64_t)payload_progress_info.status;
|
||||
dart_object_status.value.as_int64 = (int64_t)payload_progress_info->status;
|
||||
|
||||
Dart_CObject *elements[5];
|
||||
elements[0] = &dart_object_endpoint_id;
|
||||
@@ -492,15 +493,16 @@ void StartAdvertisingDart(NC_INSTANCE instance, DataDart service_id,
|
||||
|
||||
request_info.endpoint_info.data = info_dart.endpoint_info.data;
|
||||
request_info.endpoint_info.size = info_dart.endpoint_info.size;
|
||||
request_info.initiated_callback = ListenerInitiatedCB;
|
||||
request_info.initiated_callback = &ListenerInitiatedCB;
|
||||
request_info.accepted_callback = ListenerAcceptedCB;
|
||||
request_info.rejected_callback = ListenerRejectedCB;
|
||||
request_info.disconnected_callback = ListenerDisconnectedCB;
|
||||
request_info.bandwidth_changed_callback = ListenerBandwidthChangedCB;
|
||||
|
||||
NcStartAdvertising(instance,
|
||||
NC_DATA{.size = service_id.size, .data = service_id.data},
|
||||
advertising_options, request_info, [](NC_STATUS status) {
|
||||
NC_DATA service_id_data =
|
||||
NC_DATA{.size = service_id.size, .data = service_id.data};
|
||||
NcStartAdvertising(instance, &service_id_data, &advertising_options,
|
||||
&request_info, [](NC_STATUS status) {
|
||||
ResultCB(kClientState->PopNearbyConnectionsApiPort(
|
||||
NearbyConnectionsApi::kStartAdvertising),
|
||||
status);
|
||||
@@ -565,12 +567,13 @@ void StartDiscoveryDart(NC_INSTANCE instance, DataDart service_id,
|
||||
NC_DISCOVERY_LISTENER listener{};
|
||||
listener.endpoint_distance_changed_callback =
|
||||
ListenerEndpointDistanceChangedCB;
|
||||
listener.endpoint_found_callback = ListenerEndpointFoundCB;
|
||||
listener.endpoint_lost_callback = ListenerEndpointLostCB;
|
||||
listener.endpoint_found_callback = &ListenerEndpointFoundCB;
|
||||
listener.endpoint_lost_callback = &ListenerEndpointLostCB;
|
||||
|
||||
NcStartDiscovery(instance,
|
||||
NC_DATA{.size = service_id.size, .data = service_id.data},
|
||||
discovery_options, listener, [](NC_STATUS status) {
|
||||
NC_DATA service_id_data =
|
||||
NC_DATA{.size = service_id.size, .data = service_id.data};
|
||||
NcStartDiscovery(instance, &service_id_data, &discovery_options, &listener,
|
||||
[](NC_STATUS status) {
|
||||
ResultCB(kClientState->PopNearbyConnectionsApiPort(
|
||||
NearbyConnectionsApi::kStartDiscovery),
|
||||
status);
|
||||
@@ -645,7 +648,7 @@ void RequestConnectionDart(NC_INSTANCE instance, int endpoint_id,
|
||||
request_info.disconnected_callback = ListenerDisconnectedCB;
|
||||
request_info.bandwidth_changed_callback = ListenerBandwidthChangedCB;
|
||||
|
||||
NcRequestConnection(instance, endpoint_id, request_info, connection_options,
|
||||
NcRequestConnection(instance, endpoint_id, &request_info, &connection_options,
|
||||
[](NC_STATUS status) {
|
||||
ResultCB(kClientState->PopNearbyConnectionsApiPort(
|
||||
NearbyConnectionsApi::kRequestConnection),
|
||||
@@ -667,8 +670,8 @@ void AcceptConnectionDart(NC_INSTANCE instance, int endpoint_id,
|
||||
std::make_unique<PayloadListenerDart>(listener_dart));
|
||||
|
||||
NC_PAYLOAD_LISTENER listener{};
|
||||
listener.received_callback = ListenerPayloadCB;
|
||||
listener.progress_updated_callback = ListenerPayloadProgressCB;
|
||||
listener.received_callback = &ListenerPayloadCB;
|
||||
listener.progress_updated_callback = &ListenerPayloadProgressCB;
|
||||
|
||||
NcAcceptConnection(instance, endpoint_id, listener, [](NC_STATUS status) {
|
||||
ResultCB(kClientState->PopNearbyConnectionsApiPort(
|
||||
@@ -737,8 +740,9 @@ void SendPayloadDart(NC_INSTANCE instance, int endpoint_id,
|
||||
payload.content.bytes.content.data = payload_dart.data.data;
|
||||
payload.content.bytes.content.size = payload_dart.data.size;
|
||||
|
||||
NcSendPayload(instance, endpoint_ids.size(), endpoint_ids.data(),
|
||||
std::move(payload), [](NC_STATUS status) {
|
||||
const int *endpoint_ids_ptr = endpoint_ids.data();
|
||||
NcSendPayload(instance, endpoint_ids.size(), endpoint_ids_ptr, &payload,
|
||||
[](NC_STATUS status) {
|
||||
ResultCB(kClientState->PopNearbyConnectionsApiPort(
|
||||
NearbyConnectionsApi::kSendPayload),
|
||||
status);
|
||||
@@ -759,9 +763,10 @@ void SendPayloadDart(NC_INSTANCE instance, int endpoint_id,
|
||||
payload.content.file.file_name =
|
||||
const_cast<char *>(file_name_str.c_str());
|
||||
payload.content.file.parent_folder = nullptr;
|
||||
|
||||
NcSendPayload(instance, endpoint_ids.size(), endpoint_ids.data(),
|
||||
std::move(payload), [](NC_STATUS status) {
|
||||
const int *endpoint_ids_ptr = endpoint_ids.data();
|
||||
NC_PAYLOAD moved_payload = std::move(payload);
|
||||
NcSendPayload(instance, endpoint_ids.size(), endpoint_ids_ptr,
|
||||
&moved_payload, [](NC_STATUS status) {
|
||||
ResultCB(kClientState->PopNearbyConnectionsApiPort(
|
||||
NearbyConnectionsApi::kSendPayload),
|
||||
status);
|
||||
|
||||
Reference in New Issue
Block a user