From 2798faf9dbda3fbfe112db555893a296c5be756b Mon Sep 17 00:00:00 2001 From: Dan Mondrik Date: Wed, 4 Oct 2023 18:41:24 -0500 Subject: [PATCH 1/3] Fix code generation for 16-bit block moves --- generated/visa/visa_service.cpp | 52 +++--------------- source/codegen/metadata/visa/functions.py | 1 + source/codegen/service_helpers.py | 3 +- source/codegen/templates/service_helpers.mako | 10 ++-- source/custom/visa_service.custom.cpp | 55 ++++++++++++++++++- 5 files changed, 69 insertions(+), 52 deletions(-) diff --git a/generated/visa/visa_service.cpp b/generated/visa/visa_service.cpp index 9c351bfe0..5d55d6432 100644 --- a/generated/visa/visa_service.cpp +++ b/generated/visa/visa_service.cpp @@ -955,48 +955,6 @@ namespace visa_grpc { } } - //--------------------------------------------------------------------- - //--------------------------------------------------------------------- - ::grpc::Status VisaService::MoveIn16(::grpc::ServerContext* context, const MoveIn16Request* request, MoveIn16Response* response) - { - if (context->IsCancelled()) { - return ::grpc::Status::CANCELLED; - } - try { - auto vi_grpc_session = request->vi(); - ViSession vi = session_repository_->access_session(vi_grpc_session.name()); - ViUInt16 address_space; - switch (request->address_space_enum_case()) { - case visa_grpc::MoveIn16Request::AddressSpaceEnumCase::kAddressSpace: { - address_space = static_cast(request->address_space()); - break; - } - case visa_grpc::MoveIn16Request::AddressSpaceEnumCase::kAddressSpaceRaw: { - address_space = static_cast(request->address_space_raw()); - break; - } - case visa_grpc::MoveIn16Request::AddressSpaceEnumCase::ADDRESS_SPACE_ENUM_NOT_SET: { - return ::grpc::Status(::grpc::INVALID_ARGUMENT, "The value for address_space was not specified or out of range"); - break; - } - } - - ViBusAddress64 offset = request->offset(); - ViBusSize count = request->count(); - response->mutable_buffer()->Resize(count, 0); - ViUInt16* buffer = reinterpret_cast(response->mutable_buffer()->mutable_data()); - auto status = library_->MoveIn16(vi, address_space, offset, count, buffer); - if (!status_ok(status)) { - return ConvertApiErrorStatusForViSession(context, status, vi); - } - response->set_status(status); - return ::grpc::Status::OK; - } - catch (nidevice_grpc::NonDriverException& ex) { - return ex.GetStatus(); - } - } - //--------------------------------------------------------------------- //--------------------------------------------------------------------- ::grpc::Status VisaService::MoveIn32(::grpc::ServerContext* context, const MoveIn32Request* request, MoveIn32Response* response) @@ -1151,8 +1109,14 @@ namespace visa_grpc { ViBusAddress64 offset = request->offset(); ViBusSize count = static_cast(request->buffer().size()); - auto buffer = const_cast(reinterpret_cast(request->buffer().data())); - auto status = library_->MoveOut16(vi, address_space, offset, count, buffer); + auto buffer_request = request->buffer(); + std::vector buffer; + std::transform( + buffer_request.begin(), + buffer_request.end(), + std::back_inserter(buffer), + [](auto x) { return (ViUInt16)x; }); + auto status = library_->MoveOut16(vi, address_space, offset, count, buffer.data()); if (!status_ok(status)) { return ConvertApiErrorStatusForViSession(context, status, vi); } diff --git a/source/codegen/metadata/visa/functions.py b/source/codegen/metadata/visa/functions.py index d3317d742..b9bbfbc8a 100644 --- a/source/codegen/metadata/visa/functions.py +++ b/source/codegen/metadata/visa/functions.py @@ -655,6 +655,7 @@ }, 'MoveIn16': { 'cname': 'viMoveIn16Ex', + 'codegen_method': 'CustomCode', 'parameters': [ { 'direction': 'in', diff --git a/source/codegen/service_helpers.py b/source/codegen/service_helpers.py index ecfe9cffd..2fb46bd28 100644 --- a/source/codegen/service_helpers.py +++ b/source/codegen/service_helpers.py @@ -86,8 +86,9 @@ def _is_array_that_requires_conversion(parameter): or get_c_element_type_for_array_that_needs_coercion(parameter) is not None # Sessions are "converted" by accessing the session repository. or parameter["grpc_type"] == "repeated nidevice_grpc.Session" - # ViInt16s have a hardcoded copy convert routine that predates the coerced flag. + # ViInt16s/ViUInt16s have a hardcoded copy convert routine that predates the coerced flag. or parameter["type"] == "ViInt16[]" + or parameter["type"] == "ViUInt16[]" ) return False diff --git a/source/codegen/templates/service_helpers.mako b/source/codegen/templates/service_helpers.mako index c469006d8..aee41c31f 100644 --- a/source/codegen/templates/service_helpers.mako +++ b/source/codegen/templates/service_helpers.mako @@ -599,7 +599,7 @@ ${initialize_standard_input_param(function_name, parameter)} ${parameter_name}_request.end(), std::back_inserter(${parameter_name}), [](auto x) { return x ? VI_TRUE : VI_FALSE; }); -% elif c_type == 'ViInt16[]': +% elif c_type in ['ViInt16[]', 'ViUInt16[]']: auto ${parameter_name}_request = ${request_snippet}; std::vector<${c_type_underlying_type}> ${parameter_name}; std::transform( @@ -616,11 +616,11 @@ ${initialize_standard_input_param(function_name, parameter)} ${c_type} ${parameter_name} = ${service_helpers.session_repository_field_name(parameter, config)}->access_session(${parameter_name}_grpc_session.name());\ % elif is_array and common_helpers.is_driver_typedef_with_same_size_but_different_qualifiers(c_type_underlying_type): auto ${parameter_name} = const_cast<${c_type_pointer}>(reinterpret_cast(${request_snippet}.data()));\ -%elif c_type in ['const int32[]', 'const uInt32[]']: +% elif c_type in ['const int32[]', 'const uInt32[]']: auto ${parameter_name} = reinterpret_cast<${c_type_pointer}>(${request_snippet}.data());\ -%elif grpc_type == 'bytes': +% elif grpc_type == 'bytes': auto ${parameter_name} = reinterpret_cast(${request_snippet}.data());\ -%elif service_helpers.is_scalar_input_that_needs_coercion(parameter): +% elif service_helpers.is_scalar_input_that_needs_coercion(parameter): auto ${parameter_name}_raw = ${request_snippet}; if (${parameter_name}_raw < std::numeric_limits<${c_type}>::min() || ${parameter_name}_raw > std::numeric_limits<${c_type}>::max()) { std::string message("value "); @@ -630,7 +630,7 @@ ${initialize_standard_input_param(function_name, parameter)} throw nidevice_grpc::ValueOutOfRangeException(message); } auto ${parameter_name} = static_cast<${c_type}>(${parameter_name}_raw); -%elif service_helpers.is_input_array_that_needs_coercion(parameter): +% elif service_helpers.is_input_array_that_needs_coercion(parameter): auto ${parameter_name}_raw = ${request_snippet}; auto ${parameter_name} = std::vector<${c_element_type_that_needs_coercion}>(); ${parameter_name}.reserve(${parameter_name}_raw.size()); diff --git a/source/custom/visa_service.custom.cpp b/source/custom/visa_service.custom.cpp index 2a509dac3..a7cb45677 100644 --- a/source/custom/visa_service.custom.cpp +++ b/source/custom/visa_service.custom.cpp @@ -266,8 +266,8 @@ ::grpc::Status VisaService::Open(::grpc::ServerContext* context, const OpenReque // NI-VISA writes the events followed by an extra "-1" value that we don't need. std::vector events(numEvents+1); library->GetAttribute(vi, VI_ATTR_SUP_EVENTS, events.data()); - for (ViUInt16 i = 0; i < numEvents; ++i) { - response->add_supported_events(events[i]); + for (ViUInt16 index = 0; index < numEvents; ++index) { + response->add_supported_events(events[index]); } } @@ -284,6 +284,57 @@ ::grpc::Status VisaService::Open(::grpc::ServerContext* context, const OpenReque } } +//--------------------------------------------------------------------- +//--------------------------------------------------------------------- +::grpc::Status VisaService::MoveIn16(::grpc::ServerContext* context, const MoveIn16Request* request, MoveIn16Response* response) +{ + if (context->IsCancelled()) { + return ::grpc::Status::CANCELLED; + } + ViSession vi = VI_NULL; + try { + auto vi_grpc_session = request->vi(); + vi = session_repository_->access_session(vi_grpc_session.name()); + ViUInt16 address_space; + switch (request->address_space_enum_case()) { + case visa_grpc::MoveIn16Request::AddressSpaceEnumCase::kAddressSpace: { + address_space = static_cast(request->address_space()); + break; + } + case visa_grpc::MoveIn16Request::AddressSpaceEnumCase::kAddressSpaceRaw: { + address_space = static_cast(request->address_space_raw()); + break; + } + case visa_grpc::MoveIn16Request::AddressSpaceEnumCase::ADDRESS_SPACE_ENUM_NOT_SET: { + return ::grpc::Status(::grpc::INVALID_ARGUMENT, "The value for address_space was not specified or out of range"); + break; + } + } + + ViBusAddress64 offset = request->offset(); + ViBusSize count = request->count(); + std::vector driver_buffer(count); + ViUInt16* buffer_pointer = driver_buffer.data(); + auto status = library_->MoveIn16(vi, address_space, offset, count, buffer_pointer); + if (!status_ok(status)) { + return ConvertApiErrorStatusForViSession(context, status, vi); + } + auto response_buffer = response->mutable_buffer(); + response_buffer->Resize(static_cast(count), 0); + for (ViUInt32 index = 0; index < count; ++index) { + response_buffer->Set(index, *buffer_pointer++); + } + response->set_status(status); + return ::grpc::Status::OK; + } + catch (std::bad_alloc&) { + return ConvertApiErrorStatusForViSession(context, VI_ERROR_ALLOC, vi); + } + catch (nidevice_grpc::NonDriverException& ex) { + return ex.GetStatus(); + } +} + //--------------------------------------------------------------------- //--------------------------------------------------------------------- ::grpc::Status VisaService::ParseRsrc(::grpc::ServerContext* context, const ParseRsrcRequest* request, ParseRsrcResponse* response) From 08132af445f972814d45e0f3ac7748b260ecdb6e Mon Sep 17 00:00:00 2001 From: Dan Mondrik Date: Wed, 4 Oct 2023 18:58:07 -0500 Subject: [PATCH 2/3] Shorten line --- source/codegen/service_helpers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/codegen/service_helpers.py b/source/codegen/service_helpers.py index 2fb46bd28..f806e7636 100644 --- a/source/codegen/service_helpers.py +++ b/source/codegen/service_helpers.py @@ -86,7 +86,7 @@ def _is_array_that_requires_conversion(parameter): or get_c_element_type_for_array_that_needs_coercion(parameter) is not None # Sessions are "converted" by accessing the session repository. or parameter["grpc_type"] == "repeated nidevice_grpc.Session" - # ViInt16s/ViUInt16s have a hardcoded copy convert routine that predates the coerced flag. + # Vi*Int16s have a hardcoded copy convert routine that predates the coerced flag. or parameter["type"] == "ViInt16[]" or parameter["type"] == "ViUInt16[]" ) From 7e174c4d30c9e9b2b858beebb8701a74f28b4f9f Mon Sep 17 00:00:00 2001 From: Dan Mondrik Date: Thu, 5 Oct 2023 14:19:56 -0500 Subject: [PATCH 3/3] Catch by const reference --- source/custom/visa_service.custom.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/custom/visa_service.custom.cpp b/source/custom/visa_service.custom.cpp index a7cb45677..d1ae957e2 100644 --- a/source/custom/visa_service.custom.cpp +++ b/source/custom/visa_service.custom.cpp @@ -327,10 +327,10 @@ ::grpc::Status VisaService::MoveIn16(::grpc::ServerContext* context, const MoveI response->set_status(status); 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(); } }