Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement UsbControlIn #1009

Merged
merged 7 commits into from
Oct 11, 2023
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 26 additions & 23 deletions source/custom/visa_service.custom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ ::grpc::Status VisaService::FindRsrc(::grpc::ServerContext* context, const FindR
response->set_status(status);
return ::grpc::Status::OK;
}
catch (nidevice_grpc::NonDriverException& ex) {
catch (const nidevice_grpc::NonDriverException& ex) {
return ex.GetStatus();
}
catch (const DriverErrorException& ex) {
Expand All @@ -188,7 +188,7 @@ ::grpc::Status VisaService::GetAttribute(::grpc::ServerContext* context, const G
response->set_status(status);
return ::grpc::Status::OK;
}
catch (nidevice_grpc::NonDriverException& ex) {
catch (const nidevice_grpc::NonDriverException& ex) {
return ex.GetStatus();
}
}
Expand All @@ -210,7 +210,7 @@ ::grpc::Status VisaService::GetAttributeEvent(::grpc::ServerContext* context, co
response->set_status(status);
return ::grpc::Status::OK;
}
catch (nidevice_grpc::NonDriverException& ex) {
catch (const nidevice_grpc::NonDriverException& ex) {
return ex.GetStatus();
}
}
Expand Down Expand Up @@ -276,7 +276,7 @@ ::grpc::Status VisaService::Open(::grpc::ServerContext* context, const OpenReque
response->set_new_session_initialized(new_session_initialized);
return ::grpc::Status::OK;
}
catch (nidevice_grpc::NonDriverException& ex) {
catch (const nidevice_grpc::NonDriverException& ex) {
return ex.GetStatus();
}
catch (const DriverErrorException& ex) {
Expand Down Expand Up @@ -321,7 +321,7 @@ ::grpc::Status VisaService::ParseRsrc(::grpc::ServerContext* context, const Pars
nidevice_grpc::converters::trim_trailing_nulls(*(response->mutable_alias_if_exists()));
return ::grpc::Status::OK;
}
catch (nidevice_grpc::NonDriverException& ex) {
catch (const nidevice_grpc::NonDriverException& ex) {
return ex.GetStatus();
}
catch (const DriverErrorException& ex) {
Expand All @@ -341,21 +341,22 @@ ::grpc::Status VisaService::Read(::grpc::ServerContext* context, const ReadReque
auto vi_grpc_session = request->vi();
vi = session_repository_->access_session(vi_grpc_session.name());
ViUInt32 count = request->count();
std::vector<ViByte> buffer(count);
std::string* buffer = response->mutable_buffer();
buffer->resize(count);
ViUInt32 return_count{};
auto status = library_->Read(vi, buffer.data(), count, &return_count);
auto status = library_->Read(vi, (ViByte*)buffer->data(), count, &return_count);
danielhuani marked this conversation as resolved.
Show resolved Hide resolved
if (!status_ok(status) && return_count == 0) {
return ConvertApiErrorStatusForViSession(context, status, vi);
}
response->set_status(status);
response->set_buffer(buffer.data(), return_count);
buffer->resize(return_count);
response->set_return_count(return_count);
return ::grpc::Status::OK;
}
catch (std::bad_alloc&) {
catch (const std::bad_alloc&) {
return ConvertApiErrorStatusForViSession(context, VI_ERROR_ALLOC, vi);
}
catch (nidevice_grpc::NonDriverException& ex) {
catch (const nidevice_grpc::NonDriverException& ex) {
return ex.GetStatus();
}
}
Expand Down Expand Up @@ -384,7 +385,7 @@ ::grpc::Status VisaService::ReadAsync(::grpc::ServerContext* context, const Read
response->set_job_identifier(job_identifier);
return ::grpc::Status::OK;
}
catch (nidevice_grpc::NonDriverException& ex) {
catch (const nidevice_grpc::NonDriverException& ex) {
return ex.GetStatus();
}
#endif
Expand Down Expand Up @@ -435,7 +436,7 @@ ::grpc::Status VisaService::SetAttribute(::grpc::ServerContext* context, const S
response->set_status(status);
return ::grpc::Status::OK;
}
catch (nidevice_grpc::NonDriverException& ex) {
catch (const nidevice_grpc::NonDriverException& ex) {
return ex.GetStatus();
}
}
Expand All @@ -447,32 +448,34 @@ ::grpc::Status VisaService::UsbControlIn(::grpc::ServerContext* context, const U
if (context->IsCancelled()) {
return ::grpc::Status::CANCELLED;
}
#if 1
return ::grpc::Status(grpc::StatusCode::DO_NOT_USE, "Custom code not implemented yet");
#else
ViSession vi = VI_NULL;

try {
auto vi_grpc_session = request->vi();
ViSession vi = session_repository_->access_session(vi_grpc_session.name());
vi = session_repository_->access_session(vi_grpc_session.name());
ViInt16 bm_request_type = (ViInt16)request->bm_request_type();
ViInt16 b_request = (ViInt16)request->b_request();
ViUInt16 w_value = request->w_value();
ViUInt16 w_index = request->w_index();
ViUInt16 w_length = request->w_length();
std::string buffer(return_count, '\0');
std::string* buffer = response->mutable_buffer();
buffer->resize(w_length);
ViUInt16 return_count{};
auto status = library_->UsbControlIn(vi, bm_request_type, b_request, w_value, w_index, w_length, (ViByte*)buffer.data(), &return_count);
if (!status_ok(status)) {
auto status = library_->UsbControlIn(vi, bm_request_type, b_request, w_value, w_index, w_length, (ViByte*)buffer->data(), &return_count);
if (!status_ok(status) && return_count == 0) {
return ConvertApiErrorStatusForViSession(context, status, vi);
}
response->set_status(status);
response->set_buffer(buffer);
buffer->resize(return_count);
response->set_return_count(return_count);
return ::grpc::Status::OK;
}
catch (nidevice_grpc::NonDriverException& ex) {
catch (const std::bad_alloc&) {
return ConvertApiErrorStatusForViSession(context, VI_ERROR_ALLOC, vi);
}
catch (const nidevice_grpc::NonDriverException& ex) {
return ex.GetStatus();
}
#endif
}

//---------------------------------------------------------------------
Expand All @@ -499,7 +502,7 @@ ::grpc::Status VisaService::WriteAsync(::grpc::ServerContext* context, const Wri
response->set_job_identifier(job_identifier);
return ::grpc::Status::OK;
}
catch (nidevice_grpc::NonDriverException& ex) {
catch (const nidevice_grpc::NonDriverException& ex) {
return ex.GetStatus();
}
#endif
Expand Down