Add context pointer to all callbacks so that callbacks can be correlated to the function calls.

PiperOrigin-RevId: 690759015
This commit is contained in:
hai007
2024-10-28 14:52:17 -07:00
committed by Copybara-Service
parent b4d11ecbda
commit 9bf3ba52b6
4 changed files with 230 additions and 165 deletions
+70 -62
View File
@@ -27,7 +27,6 @@
#include "absl/base/no_destructor.h"
#include "absl/container/flat_hash_map.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/str_format.h"
#include "absl/strings/string_view.h"
#include "absl/types/span.h"
#include "connections/advertising_options.h"
@@ -100,27 +99,28 @@ NcContext* GetContext(NC_INSTANCE instance) {
::nearby::connections::ConnectionRequestInfo GetCppConnectionRequestInfo(
NC_INSTANCE instance,
const NC_CONNECTION_REQUEST_INFO& connection_request_info) {
const NC_CONNECTION_REQUEST_INFO& connection_request_info,
CALLER_CONTEXT context) {
::nearby::connections::ConnectionRequestInfo cpp_connection_request_info;
cpp_connection_request_info.endpoint_info =
nearby::ByteArray(connection_request_info.endpoint_info.data,
connection_request_info.endpoint_info.size);
::nearby::connections::ConnectionListener cpp_connection_listener;
cpp_connection_listener.accepted_cb = [=](const std::string& endpoint_id) {
connection_request_info.accepted_callback(instance,
convertStringToInt(endpoint_id));
connection_request_info.accepted_callback(
instance, convertStringToInt(endpoint_id), context);
};
cpp_connection_listener.bandwidth_changed_cb =
[=](const std::string& endpoint_id,
::nearby::connections::Medium medium) {
connection_request_info.bandwidth_changed_callback(
instance, convertStringToInt(endpoint_id),
static_cast<NC_MEDIUM>(medium));
static_cast<NC_MEDIUM>(medium), context);
};
cpp_connection_listener.disconnected_cb =
[=](const std::string& endpoint_id) {
connection_request_info.disconnected_callback(
instance, convertStringToInt(endpoint_id));
instance, convertStringToInt(endpoint_id), context);
};
cpp_connection_listener.initiated_cb =
[=](const std::string& endpoint_id,
@@ -145,7 +145,7 @@ NcContext* GetContext(NC_INSTANCE instance) {
connection_request_info.initiated_callback(
instance, convertStringToInt(endpoint_id),
&connection_response_info);
&connection_response_info, context);
};
cpp_connection_listener.rejected_cb =
@@ -153,7 +153,7 @@ NcContext* GetContext(NC_INSTANCE instance) {
::nearby::connections::Status status) {
connection_request_info.rejected_callback(
instance, convertStringToInt(endpoint_id),
static_cast<NC_STATUS>(status.value));
static_cast<NC_STATUS>(status.value), context);
};
cpp_connection_request_info.listener = std::move(cpp_connection_listener);
@@ -190,15 +190,15 @@ void NcStartAdvertising(
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) {
NcCallbackResult result_callback, CALLER_CONTEXT context) {
NcContext* nc_context = GetContext(instance);
if (nc_context == nullptr) {
result_callback(NC_STATUS_ERROR);
result_callback(NC_STATUS_ERROR, context);
return;
}
::nearby::connections::ConnectionRequestInfo cpp_connection_request_info =
GetCppConnectionRequestInfo(instance, *connection_request_info);
GetCppConnectionRequestInfo(instance, *connection_request_info, context);
::nearby::connections::AdvertisingOptions cpp_advertising_options;
cpp_advertising_options.allowed.ble =
@@ -255,29 +255,30 @@ void NcStartAdvertising(
std::move(cpp_advertising_options),
std::move(cpp_connection_request_info),
[=](::nearby::connections::Status status) {
result_callback(static_cast<NC_STATUS>(status.value));
result_callback(static_cast<NC_STATUS>(status.value), context);
});
}
void NcStopAdvertising(NC_INSTANCE instance, NcCallbackResult result_callback) {
void NcStopAdvertising(NC_INSTANCE instance, NcCallbackResult result_callback,
CALLER_CONTEXT context) {
NcContext* nc_context = GetContext(instance);
if (nc_context == nullptr) {
result_callback(NC_STATUS_ERROR);
result_callback(NC_STATUS_ERROR, context);
return;
}
nc_context->core->StopAdvertising([=](::nearby::connections::Status status) {
result_callback(static_cast<NC_STATUS>(status.value));
result_callback(static_cast<NC_STATUS>(status.value), context);
});
}
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) {
NcCallbackResult result_callback, void* context) {
NcContext* nc_context = GetContext(instance);
if (nc_context == nullptr) {
result_callback(NC_STATUS_ERROR);
result_callback(NC_STATUS_ERROR, context);
return;
}
@@ -326,7 +327,7 @@ void NcStartDiscovery(NC_INSTANCE instance, const NC_DATA* service_id,
::nearby::connections::DistanceInfo info) {
discovery_listener_copy.endpoint_distance_changed_callback(
instance, convertStringToInt(endpoint_id),
static_cast<NC_DISTANCE_INFO>(info));
static_cast<NC_DISTANCE_INFO>(info), context);
};
listener.endpoint_found_cb = [=](const std::string& endpoint_id,
const nearby::ByteArray& endpoint_info,
@@ -338,40 +339,42 @@ void NcStartDiscovery(NC_INSTANCE instance, const NC_DATA* service_id,
.data = (char*)service_id.data()};
discovery_listener_copy.endpoint_found_callback(
instance, convertStringToInt(endpoint_id), &endpoint_info_data,
&service_id_data);
&service_id_data, context);
};
listener.endpoint_lost_cb = [=](const std::string& endpoint_id) {
discovery_listener_copy.endpoint_lost_callback(
instance, convertStringToInt(endpoint_id));
instance, convertStringToInt(endpoint_id), context);
};
nc_context->core->StartDiscovery(
std::string(service_id->data, service_id->size),
std::move(cpp_discovery_options), std::move(listener),
[result_callback =
std::move(result_callback)](::nearby::connections::Status status) {
result_callback(static_cast<NC_STATUS>(status.value));
[result_callback = std::move(result_callback),
context](::nearby::connections::Status status) {
result_callback(static_cast<NC_STATUS>(status.value), context);
});
}
void NcStopDiscovery(NC_INSTANCE instance, NcCallbackResult result_callback) {
void NcStopDiscovery(NC_INSTANCE instance, NcCallbackResult result_callback,
CALLER_CONTEXT context) {
NcContext* nc_context = GetContext(instance);
if (nc_context == nullptr) {
result_callback(NC_STATUS_ERROR);
result_callback(NC_STATUS_ERROR, context);
return;
}
nc_context->core->StopDiscovery([=](::nearby::connections::Status status) {
result_callback(static_cast<NC_STATUS>(status.value));
result_callback(static_cast<NC_STATUS>(status.value), context);
});
}
void NcInjectEndpoint(NC_INSTANCE instance, const NC_DATA* service_id,
const NC_OUT_OF_BAND_CONNECTION_METADATA* metadata,
NcCallbackResult result_callback) {
NcCallbackResult result_callback,
CALLER_CONTEXT context) {
NcContext* nc_context = GetContext(instance);
if (nc_context == nullptr) {
result_callback(NC_STATUS_ERROR);
result_callback(NC_STATUS_ERROR, context);
return;
}
@@ -391,7 +394,7 @@ void NcInjectEndpoint(NC_INSTANCE instance, const NC_DATA* service_id,
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));
result_callback(static_cast<NC_STATUS>(status.value), context);
});
}
@@ -399,15 +402,15 @@ void NcRequestConnection(
NC_INSTANCE instance, int endpoint_id,
const NC_CONNECTION_REQUEST_INFO* connection_request_info,
const NC_CONNECTION_OPTIONS* connection_options,
NcCallbackResult result_callback) {
NcCallbackResult result_callback, CALLER_CONTEXT context) {
NcContext* nc_context = GetContext(instance);
if (nc_context == nullptr) {
result_callback(NC_STATUS_ERROR);
result_callback(NC_STATUS_ERROR, context);
return;
}
::nearby::connections::ConnectionRequestInfo cpp_connection_request_info =
GetCppConnectionRequestInfo(instance, *connection_request_info);
GetCppConnectionRequestInfo(instance, *connection_request_info, context);
::nearby::connections::ConnectionOptions cpp_connection_options;
cpp_connection_options.allowed.ble =
@@ -458,16 +461,17 @@ void NcRequestConnection(
convertIntToString(endpoint_id), std::move(cpp_connection_request_info),
std::move(cpp_connection_options),
[=](::nearby::connections::Status status) {
result_callback(static_cast<NC_STATUS>(status.value));
result_callback(static_cast<NC_STATUS>(status.value), context);
});
}
void NcAcceptConnection(NC_INSTANCE instance, int endpoint_id,
NC_PAYLOAD_LISTENER payload_listener,
NcCallbackResult result_callback) {
NcCallbackResult result_callback,
CALLER_CONTEXT context) {
NcContext* nc_context = GetContext(instance);
if (nc_context == nullptr) {
result_callback(NC_STATUS_ERROR);
result_callback(NC_STATUS_ERROR, context);
return;
}
@@ -495,7 +499,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, context);
};
cpp_payload_listener.payload_progress_cb =
@@ -509,37 +513,38 @@ 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, context);
};
nc_context->core->AcceptConnection(
convertIntToString(endpoint_id), std::move(cpp_payload_listener),
[=](::nearby::connections::Status status) {
result_callback(static_cast<NC_STATUS>(status.value));
result_callback(static_cast<NC_STATUS>(status.value), context);
});
}
void NcRejectConnection(NC_INSTANCE instance, int endpoint_id,
NcCallbackResult result_callback) {
NcCallbackResult result_callback,
CALLER_CONTEXT context) {
NcContext* nc_context = GetContext(instance);
if (nc_context == nullptr) {
result_callback(NC_STATUS_ERROR);
result_callback(NC_STATUS_ERROR, context);
return;
}
nc_context->core->RejectConnection(
convertIntToString(endpoint_id),
[=](::nearby::connections::Status status) {
result_callback(static_cast<NC_STATUS>(status.value));
result_callback(static_cast<NC_STATUS>(status.value), context);
});
}
void NcSendPayload(NC_INSTANCE instance, size_t endpoint_ids_size,
const int* endpoint_ids, const NC_PAYLOAD* payload,
NcCallbackResult result_callback) {
NcCallbackResult result_callback, CALLER_CONTEXT context) {
NcContext* nc_context = GetContext(instance);
if (nc_context == nullptr) {
result_callback(NC_STATUS_ERROR);
result_callback(NC_STATUS_ERROR, context);
return;
}
@@ -576,64 +581,66 @@ void NcSendPayload(NC_INSTANCE instance, size_t endpoint_ids_size,
nc_context->core->SendPayload(
endpoint_ids_span, std::move(cpp_payload),
[=](::nearby::connections::Status status) {
result_callback(static_cast<NC_STATUS>(status.value));
result_callback(static_cast<NC_STATUS>(status.value), context);
});
}
void NcCancelPayload(NC_INSTANCE instance, NC_PAYLOAD_ID payload_id,
NcCallbackResult result_callback) {
NcCallbackResult result_callback, CALLER_CONTEXT context) {
NcContext* nc_context = GetContext(instance);
if (nc_context == nullptr) {
result_callback(NC_STATUS_ERROR);
result_callback(NC_STATUS_ERROR, context);
return;
}
nc_context->core->CancelPayload(
payload_id, [=](::nearby::connections::Status status) {
result_callback(static_cast<NC_STATUS>(status.value));
result_callback(static_cast<NC_STATUS>(status.value), context);
});
}
void NcDisconnectFromEndpoint(NC_INSTANCE instance, int endpoint_id,
NcCallbackResult result_callback) {
NcCallbackResult result_callback,
CALLER_CONTEXT context) {
NcContext* nc_context = GetContext(instance);
if (nc_context == nullptr) {
result_callback(NC_STATUS_ERROR);
result_callback(NC_STATUS_ERROR, context);
return;
}
nc_context->core->DisconnectFromEndpoint(
convertIntToString(endpoint_id),
[=](::nearby::connections::Status status) {
result_callback(static_cast<NC_STATUS>(status.value));
result_callback(static_cast<NC_STATUS>(status.value), context);
});
}
void NcStopAllEndpoints(NC_INSTANCE instance,
NcCallbackResult result_callback) {
void NcStopAllEndpoints(NC_INSTANCE instance, NcCallbackResult result_callback,
CALLER_CONTEXT context) {
NcContext* nc_context = GetContext(instance);
if (nc_context == nullptr) {
result_callback(NC_STATUS_ERROR);
result_callback(NC_STATUS_ERROR, context);
return;
}
nc_context->core->StopAllEndpoints([=](::nearby::connections::Status status) {
result_callback(static_cast<NC_STATUS>(status.value));
result_callback(static_cast<NC_STATUS>(status.value), context);
});
}
void NcInitiateBandwidthUpgrade(NC_INSTANCE instance, int endpoint_id,
NcCallbackResult result_callback) {
NcCallbackResult result_callback,
CALLER_CONTEXT context) {
NcContext* nc_context = GetContext(instance);
if (nc_context == nullptr) {
result_callback(NC_STATUS_ERROR);
result_callback(NC_STATUS_ERROR, context);
return;
}
nc_context->core->InitiateBandwidthUpgrade(
convertIntToString(endpoint_id),
[=](::nearby::connections::Status status) {
result_callback(static_cast<NC_STATUS>(status.value));
result_callback(static_cast<NC_STATUS>(status.value), context);
});
}
@@ -648,25 +655,26 @@ int NcGetLocalEndpointId(NC_INSTANCE instance) {
}
void NcEnableBleV2(NC_INSTANCE instance, bool enable,
NcCallbackResult result_callback) {
NcCallbackResult result_callback, CALLER_CONTEXT context) {
nearby::NearbyFlags::GetInstance().OverrideBoolFlagValue(
::nearby::connections::config_package_nearby::nearby_connections_feature::
kEnableBleV2,
enable);
result_callback(NC_STATUS_SUCCESS);
result_callback(NC_STATUS_SUCCESS, context);
}
void NcSetCustomSavePath(NC_INSTANCE instance, const NC_DATA* save_path,
NcCallbackResult result_callback) {
NcCallbackResult result_callback,
CALLER_CONTEXT context) {
NcContext* nc_context = GetContext(instance);
if (nc_context == nullptr) {
result_callback(NC_STATUS_ERROR);
result_callback(NC_STATUS_ERROR, context);
return;
}
nc_context->core->SetCustomSavePath(
std::string(save_path->data, save_path->size),
[=](::nearby::connections::Status status) {
result_callback(static_cast<NC_STATUS>(status.value));
result_callback(static_cast<NC_STATUS>(status.value), context);
});
}
+28 -15
View File
@@ -40,7 +40,7 @@ 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,
NcCallbackResult result_callback);
NcCallbackResult result_callback, CALLER_CONTEXT context);
// Stops advertising a local endpoint. It should be called after calling
// StartAdvertising.
@@ -48,7 +48,8 @@ NC_API void NcStartAdvertising(
// instance - The instance is used to start advertising.
// result_callback - The result of the API operation.
NC_API void NcStopAdvertising(NC_INSTANCE instance,
NcCallbackResult result_callback);
NcCallbackResult result_callback,
CALLER_CONTEXT context);
// Starts to discover for remote endpoints with the specified service ID.
//
@@ -61,14 +62,16 @@ NC_API void NcStopAdvertising(NC_INSTANCE instance,
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);
NcCallbackResult result_callback,
CALLER_CONTEXT context);
// Stops discovering for a running discovery.
//
// instance - The instance is used to start discovery.
// result_callback - The result of the API operation.
NC_API void NcStopDiscovery(NC_INSTANCE instance,
NcCallbackResult result_callback);
NcCallbackResult result_callback,
CALLER_CONTEXT context);
// Invokes the discovery callback from a previous call to NcStartDiscovery()
// with the given endpoint info. The previous call to NcStartDiscovery() must
@@ -81,7 +84,8 @@ NC_API void NcStopDiscovery(NC_INSTANCE instance,
// 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,
NcCallbackResult result_callback);
NcCallbackResult result_callback,
CALLER_CONTEXT context);
// Sends a request to connect to a remote endpoint.
//
@@ -95,7 +99,7 @@ NC_API void NcRequestConnection(
NC_INSTANCE instance, int endpoint_id,
const NC_CONNECTION_REQUEST_INFO* connection_request_info,
const NC_CONNECTION_OPTIONS* connection_options,
NcCallbackResult result_callback);
NcCallbackResult result_callback, CALLER_CONTEXT context);
// Accepts a connection to a remote endpoint.
//
@@ -106,7 +110,8 @@ NC_API void NcRequestConnection(
// result_callback - The result of the API operation.
NC_API void NcAcceptConnection(NC_INSTANCE instance, int endpoint_id,
NC_PAYLOAD_LISTENER payload_listener,
NcCallbackResult result_callback);
NcCallbackResult result_callback,
CALLER_CONTEXT context);
// Rejects a connection from a remote endpoint.
//
@@ -114,7 +119,8 @@ NC_API void NcAcceptConnection(NC_INSTANCE instance, int endpoint_id,
// endpoint_id - The identifier for the remote endpoint.
// result_callback - The result of the API operation.
NC_API void NcRejectConnection(NC_INSTANCE instance, int endpoint_id,
NcCallbackResult result_callback);
NcCallbackResult result_callback,
CALLER_CONTEXT context);
// Sends a Payload to a remote endpoint.
//
@@ -125,7 +131,8 @@ NC_API void NcRejectConnection(NC_INSTANCE instance, int endpoint_id,
// 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,
NcCallbackResult result_callback);
NcCallbackResult result_callback,
CALLER_CONTEXT context);
// Cancels a Payload currently in-flight to or from remote endpoint(s).
//
@@ -133,7 +140,8 @@ NC_API void NcSendPayload(NC_INSTANCE instance, size_t endpoint_ids_size,
// payload_id - The payload ID of payload to cancel.
// result_callback - The result of the API operation.
NC_API void NcCancelPayload(NC_INSTANCE instance, NC_PAYLOAD_ID payload_id,
NcCallbackResult result_callback);
NcCallbackResult result_callback,
CALLER_CONTEXT context);
// Disconnects from a remote endpoint.
//
@@ -141,7 +149,8 @@ NC_API void NcCancelPayload(NC_INSTANCE instance, NC_PAYLOAD_ID payload_id,
// endpoint_id - The endpoint ID of remote device to disconnect.
// result_callback - The result of the API operation.
NC_API void NcDisconnectFromEndpoint(NC_INSTANCE instance, int endpoint_id,
NcCallbackResult result_callback);
NcCallbackResult result_callback,
CALLER_CONTEXT context);
// Disconnects from, and removes all traces of, all connected and/or
// discovered endpoints.
@@ -149,7 +158,8 @@ NC_API void NcDisconnectFromEndpoint(NC_INSTANCE instance, int endpoint_id,
// instance - The returned instance by NcOpenService.
// result_callback - The result of the API operation.
NC_API void NcStopAllEndpoints(NC_INSTANCE instance,
NcCallbackResult result_callback);
NcCallbackResult result_callback,
CALLER_CONTEXT context);
// Sends a request to initiate connection bandwidth upgrade.
//
@@ -157,7 +167,8 @@ NC_API void NcStopAllEndpoints(NC_INSTANCE instance,
// endpoint_id - Requested to upgrade on the remote device with the endpoint ID.
// result_callback - The result of the API operation.
NC_API void NcInitiateBandwidthUpgrade(NC_INSTANCE instance, int endpoint_id,
NcCallbackResult result_callback);
NcCallbackResult result_callback,
CALLER_CONTEXT context);
// Gets the local endpoint generated by Nearby Connections.
NC_API int NcGetLocalEndpointId(NC_INSTANCE instance);
@@ -165,11 +176,13 @@ NC_API int NcGetLocalEndpointId(NC_INSTANCE instance);
// Enable/Disable BLE V2 advertising. The method should be deprecated after
// BLE V1 deprecated.
NC_API void NcEnableBleV2(NC_INSTANCE instance, bool enable,
NcCallbackResult result_callback);
NcCallbackResult result_callback,
CALLER_CONTEXT context);
// Sets the custom save path for Nearby Connections.
NC_API void NcSetCustomSavePath(NC_INSTANCE instance, const NC_DATA* save_path,
NcCallbackResult result_callback);
NcCallbackResult result_callback,
CALLER_CONTEXT context);
#ifdef __cplusplus
} // extern "C"
+27 -15
View File
@@ -26,6 +26,8 @@ typedef void* NC_INSTANCE;
typedef int64_t NC_PAYLOAD_ID;
typedef void* CALLER_CONTEXT;
// NC_DATA is used to define a byte array. Its last byte is not zero.
typedef struct NC_DATA {
int64_t size;
@@ -180,20 +182,24 @@ typedef struct NC_CONNECTION_RESPONSE_INFO {
// Defines callbacks in Nearby Connections.
typedef void (*NcCallbackResult)(NC_STATUS status);
typedef void (*NcCallbackResult)(NC_STATUS status, CALLER_CONTEXT context);
typedef void (*NcCallbackConnectionInitiated)(
NC_INSTANCE instance, int endpoint_id,
const NC_CONNECTION_RESPONSE_INFO* info);
const NC_CONNECTION_RESPONSE_INFO* info, CALLER_CONTEXT context);
typedef void (*NcCallbackConnectionAccepted)(NC_INSTANCE instance,
int endpoint_id);
int endpoint_id,
CALLER_CONTEXT context);
typedef void (*NcCallbackConnectionRejected)(NC_INSTANCE instance,
int endpoint_id, NC_STATUS status);
int endpoint_id, NC_STATUS status,
CALLER_CONTEXT context);
typedef void (*NcCallbackConnectionDisconnected)(NC_INSTANCE instance,
int endpoint_id);
int endpoint_id,
CALLER_CONTEXT context);
typedef void (*NcCallbackConnectionBandwidthChanged)(NC_INSTANCE instance,
int endpoint_id,
NC_MEDIUM medium);
NC_MEDIUM medium,
CALLER_CONTEXT context);
typedef struct NC_CONNECTION_REQUEST_INFO {
NC_DATA endpoint_info;
@@ -207,11 +213,14 @@ 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* service_id,
CALLER_CONTEXT context);
typedef void (*NcCallbackDiscoveryEndpointLost)(NC_INSTANCE instance,
int endpoint_id);
int endpoint_id,
CALLER_CONTEXT context);
typedef void (*NcCallbackDiscoveryEndpointDistanceChanged)(
NC_INSTANCE instance, int endpoint_id, NC_DISTANCE_INFO info);
NC_INSTANCE instance, int endpoint_id, NC_DISTANCE_INFO info,
CALLER_CONTEXT context);
typedef struct NC_DISCOVERY_LISTENER {
NcCallbackDiscoveryEndpointFound endpoint_found_callback;
@@ -224,9 +233,11 @@ typedef struct NC_BYTES_PAYLOAD {
} NC_BYTES_PAYLOAD;
typedef int (*NcCallbackStreamRead)(NC_INSTANCE stream, char* buffer,
int64_t size);
typedef int (*NcCallbackStreamClose)(NC_INSTANCE stream);
typedef int (*NcCallbackStreamSkip)(NC_INSTANCE stream, int64_t skip);
int64_t size, CALLER_CONTEXT context);
typedef int (*NcCallbackStreamClose)(NC_INSTANCE stream,
CALLER_CONTEXT context);
typedef int (*NcCallbackStreamSkip)(NC_INSTANCE stream, int64_t skip,
CALLER_CONTEXT context);
typedef struct NC_STREAM_PAYLOAD {
NC_INSTANCE stream;
@@ -269,10 +280,11 @@ 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,
CALLER_CONTEXT context);
typedef void (*NcCallbackPayloadProgressUpdated)(
NC_INSTANCE instance, int endpoint_id,
const NC_PAYLOAD_PROGRESS_INFO* info);
NC_INSTANCE instance, int endpoint_id, const NC_PAYLOAD_PROGRESS_INFO* info,
CALLER_CONTEXT context);
typedef struct NC_PAYLOAD_LISTENER {
NcCallbackPayloadReceived received_callback;
+105 -73
View File
@@ -91,7 +91,8 @@ 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,
void *context) {
NEARBY_LOGS(INFO) << "Advertising initiated: id="
<< GetEndpointIdString(endpoint_id);
@@ -125,7 +126,7 @@ void ListenerInitiatedCB(
}
}
void ListenerAcceptedCB(NC_INSTANCE instance, int endpoint_id) {
void ListenerAcceptedCB(NC_INSTANCE instance, int endpoint_id, void *context) {
NEARBY_LOGS(INFO) << "Advertising accepted: id="
<< GetEndpointIdString(endpoint_id);
Dart_CObject dart_object_accepted;
@@ -139,8 +140,8 @@ void ListenerAcceptedCB(NC_INSTANCE instance, int endpoint_id) {
}
}
void ListenerRejectedCB(NC_INSTANCE instance, int endpoint_id,
NC_STATUS status) {
void ListenerRejectedCB(NC_INSTANCE instance, int endpoint_id, NC_STATUS status,
void *context) {
NEARBY_LOGS(INFO) << "Advertising rejected: id="
<< GetEndpointIdString(endpoint_id);
Dart_CObject dart_object_rejected;
@@ -154,7 +155,8 @@ void ListenerRejectedCB(NC_INSTANCE instance, int endpoint_id,
}
}
void ListenerDisconnectedCB(NC_INSTANCE instance, int endpoint_id) {
void ListenerDisconnectedCB(NC_INSTANCE instance, int endpoint_id,
void *context) {
NEARBY_LOGS(INFO) << "Advertising disconnected: id="
<< GetEndpointIdString(endpoint_id);
Dart_CObject dart_object_disconnected;
@@ -169,7 +171,7 @@ void ListenerDisconnectedCB(NC_INSTANCE instance, int endpoint_id) {
}
void ListenerBandwidthChangedCB(NC_INSTANCE instance, int endpoint_id,
NC_MEDIUM medium) {
NC_MEDIUM medium, void *context) {
NEARBY_LOGS(INFO) << "Advertising bandwidth changed: id="
<< GetEndpointIdString(endpoint_id);
Dart_CObject dart_object_bandwidth_changed;
@@ -186,7 +188,7 @@ 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 *service_id, void *context) {
NEARBY_LOGS(INFO) << "Device discovered: id="
<< GetEndpointIdString(endpoint_id);
NEARBY_LOGS(INFO) << "Device discovered: service_id="
@@ -223,7 +225,8 @@ void ListenerEndpointFoundCB(NC_INSTANCE instance, int endpoint_id,
}
}
void ListenerEndpointLostCB(NC_INSTANCE instance, int endpoint_id) {
void ListenerEndpointLostCB(NC_INSTANCE instance, int endpoint_id,
void *context) {
NEARBY_LOGS(INFO) << "Device lost: id=" << GetEndpointIdString(endpoint_id);
Dart_CObject dart_object_lost;
dart_object_lost.type = Dart_CObject_kInt32;
@@ -237,7 +240,8 @@ void ListenerEndpointLostCB(NC_INSTANCE instance, int endpoint_id) {
}
void ListenerEndpointDistanceChangedCB(NC_INSTANCE instance, int endpoint_id,
NC_DISTANCE_INFO distance_info) {
NC_DISTANCE_INFO distance_info,
void *context) {
(void)distance_info; // Avoid unused parameter warning
NEARBY_LOGS(INFO) << "Device distance changed: id="
<< GetEndpointIdString(endpoint_id);
@@ -253,7 +257,7 @@ void ListenerEndpointDistanceChangedCB(NC_INSTANCE instance, int endpoint_id,
}
void ListenerPayloadCB(NC_INSTANCE instance, int endpoint_id,
const NC_PAYLOAD *payload) {
const NC_PAYLOAD *payload, void *context) {
NEARBY_LOGS(INFO) << "Payload callback called. id: "
<< GetEndpointIdString(endpoint_id)
<< ", payload_id: " << payload->id
@@ -355,7 +359,7 @@ 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, void *context) {
NEARBY_LOGS(INFO) << "Payload progress callback called. id: "
<< GetEndpointIdString(endpoint_id)
<< ", payload_id: " << payload_progress_info->id
@@ -437,11 +441,14 @@ void EnableBleV2Dart(NC_INSTANCE instance, int64_t enable,
Dart_Port result_cb) {
kClientState->PushNearbyConnectionsApiPort(NearbyConnectionsApi::kEnableBleV2,
result_cb);
NcEnableBleV2(instance, enable, [](NC_STATUS status) {
ResultCB(kClientState->PopNearbyConnectionsApiPort(
NearbyConnectionsApi::kEnableBleV2),
status);
});
NcEnableBleV2(
instance, enable,
[](NC_STATUS status, void *context) {
ResultCB(kClientState->PopNearbyConnectionsApiPort(
NearbyConnectionsApi::kEnableBleV2),
status);
},
nullptr);
NEARBY_LOGS(INFO) << "EnableBleV2Dart callback is called with enable="
<< enable;
}
@@ -497,12 +504,14 @@ void StartAdvertisingDart(NC_INSTANCE instance, DataDart service_id,
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);
});
NcStartAdvertising(
instance, &service_id_data, &advertising_options, &request_info,
[](NC_STATUS status, void *context) {
ResultCB(kClientState->PopNearbyConnectionsApiPort(
NearbyConnectionsApi::kStartAdvertising),
status);
},
nullptr);
}
void StopAdvertisingDart(NC_INSTANCE instance, Dart_Port result_cb) {
@@ -514,12 +523,15 @@ void StopAdvertisingDart(NC_INSTANCE instance, Dart_Port result_cb) {
kClientState->PushNearbyConnectionsApiPort(
NearbyConnectionsApi::kStopAdvertising, result_cb);
NcStopAdvertising(instance, [](NC_STATUS status) {
kClientState->SetConnectionListenerDart(nullptr);
ResultCB(kClientState->PopNearbyConnectionsApiPort(
NearbyConnectionsApi::kStopAdvertising),
status);
});
NcStopAdvertising(
instance,
[](NC_STATUS status, void *context) {
kClientState->SetConnectionListenerDart(nullptr);
ResultCB(kClientState->PopNearbyConnectionsApiPort(
NearbyConnectionsApi::kStopAdvertising),
status);
},
nullptr);
}
void StartDiscoveryDart(NC_INSTANCE instance, DataDart service_id,
@@ -569,12 +581,14 @@ void StartDiscoveryDart(NC_INSTANCE instance, DataDart service_id,
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);
});
NcStartDiscovery(
instance, &service_id_data, &discovery_options, &listener,
[](NC_STATUS status, void *context) {
ResultCB(kClientState->PopNearbyConnectionsApiPort(
NearbyConnectionsApi::kStartDiscovery),
status);
},
nullptr);
}
void StopDiscoveryDart(NC_INSTANCE instance, Dart_Port result_cb) {
@@ -586,12 +600,15 @@ void StopDiscoveryDart(NC_INSTANCE instance, Dart_Port result_cb) {
kClientState->PushNearbyConnectionsApiPort(
NearbyConnectionsApi::kStopDiscovery, result_cb);
NcStopDiscovery(instance, [](NC_STATUS status) {
kClientState->SetDiscoveryListenerDart(nullptr);
ResultCB(kClientState->PopNearbyConnectionsApiPort(
NearbyConnectionsApi::kStopDiscovery),
status);
});
NcStopDiscovery(
instance,
[](NC_STATUS status, void *context) {
kClientState->SetDiscoveryListenerDart(nullptr);
ResultCB(kClientState->PopNearbyConnectionsApiPort(
NearbyConnectionsApi::kStopDiscovery),
status);
},
nullptr);
}
void RequestConnectionDart(NC_INSTANCE instance, int endpoint_id,
@@ -645,12 +662,14 @@ 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,
[](NC_STATUS status) {
ResultCB(kClientState->PopNearbyConnectionsApiPort(
NearbyConnectionsApi::kRequestConnection),
status);
});
NcRequestConnection(
instance, endpoint_id, &request_info, &connection_options,
[](NC_STATUS status, void *context) {
ResultCB(kClientState->PopNearbyConnectionsApiPort(
NearbyConnectionsApi::kRequestConnection),
status);
},
nullptr);
}
void AcceptConnectionDart(NC_INSTANCE instance, int endpoint_id,
@@ -670,11 +689,14 @@ void AcceptConnectionDart(NC_INSTANCE instance, int endpoint_id,
listener.received_callback = &ListenerPayloadCB;
listener.progress_updated_callback = &ListenerPayloadProgressCB;
NcAcceptConnection(instance, endpoint_id, listener, [](NC_STATUS status) {
ResultCB(kClientState->PopNearbyConnectionsApiPort(
NearbyConnectionsApi::kAcceptConnection),
status);
});
NcAcceptConnection(
instance, endpoint_id, listener,
[](NC_STATUS status, void *context) {
ResultCB(kClientState->PopNearbyConnectionsApiPort(
NearbyConnectionsApi::kAcceptConnection),
status);
},
nullptr);
}
void RejectConnectionDart(NC_INSTANCE instance, int endpoint_id,
@@ -687,11 +709,14 @@ void RejectConnectionDart(NC_INSTANCE instance, int endpoint_id,
kClientState->PushNearbyConnectionsApiPort(
NearbyConnectionsApi::kRejectConnection, result_cb);
NcRejectConnection(instance, endpoint_id, [](NC_STATUS status) {
ResultCB(kClientState->PopNearbyConnectionsApiPort(
NearbyConnectionsApi::kRejectConnection),
status);
});
NcRejectConnection(
instance, endpoint_id,
[](NC_STATUS status, void *context) {
ResultCB(kClientState->PopNearbyConnectionsApiPort(
NearbyConnectionsApi::kRejectConnection),
status);
},
nullptr);
}
void DisconnectFromEndpointDart(NC_INSTANCE instance, int endpoint_id,
@@ -704,11 +729,14 @@ void DisconnectFromEndpointDart(NC_INSTANCE instance, int endpoint_id,
kClientState->PushNearbyConnectionsApiPort(
NearbyConnectionsApi::kDisconnectFromEndpoint, result_cb);
NcDisconnectFromEndpoint(instance, endpoint_id, [](NC_STATUS status) {
ResultCB(kClientState->PopNearbyConnectionsApiPort(
NearbyConnectionsApi::kDisconnectFromEndpoint),
status);
});
NcDisconnectFromEndpoint(
instance, endpoint_id,
[](NC_STATUS status, void *context) {
ResultCB(kClientState->PopNearbyConnectionsApiPort(
NearbyConnectionsApi::kDisconnectFromEndpoint),
status);
},
nullptr);
}
void SendPayloadDart(NC_INSTANCE instance, int endpoint_id,
@@ -738,12 +766,14 @@ void SendPayloadDart(NC_INSTANCE instance, int endpoint_id,
payload.content.bytes.content.size = payload_dart.data.size;
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);
});
NcSendPayload(
instance, endpoint_ids.size(), endpoint_ids_ptr, &payload,
[](NC_STATUS status, void *context) {
ResultCB(kClientState->PopNearbyConnectionsApiPort(
NearbyConnectionsApi::kSendPayload),
status);
},
nullptr);
break;
}
case PAYLOAD_TYPE_FILE:
@@ -762,12 +792,14 @@ void SendPayloadDart(NC_INSTANCE instance, int endpoint_id,
payload.content.file.parent_folder = nullptr;
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);
});
NcSendPayload(
instance, endpoint_ids.size(), endpoint_ids_ptr, &moved_payload,
[](NC_STATUS status, void *context) {
ResultCB(kClientState->PopNearbyConnectionsApiPort(
NearbyConnectionsApi::kSendPayload),
status);
},
nullptr);
break;
}