Skip to content

Commit

Permalink
Add VISA metadata to generate proto and services files
Browse files Browse the repository at this point in the history
  • Loading branch information
danielhuani committed Aug 30, 2023
1 parent 53b2513 commit 0c2e46c
Show file tree
Hide file tree
Showing 48 changed files with 11,754 additions and 18 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ set(metadata_dir ${CMAKE_SOURCE_DIR}/source/codegen/metadata)
set(service_output_dir ${CMAKE_SOURCE_DIR}/generated)
set(codegen_dir ${CMAKE_SOURCE_DIR}/source/codegen)
set(custom_dir ${CMAKE_SOURCE_DIR}/source/custom)
set(nidrivers "nidaqmx" "nidcpower" "nidigitalpattern" "nidmm" "nifake" "nifake_extension" "nifake_non_ivi" "nifgen" "nimxlcterminaladaptor_restricted" "nirfmxbluetooth" "nirfmxcdma2k" "nirfmxgsm" "nirfmxinstr" "nirfmxinstr_restricted" "nirfmxlte" "nirfmxnr" "nirfmxspecan" "nirfmxspecan_restricted" "nirfmxtdscdma" "nirfmxwcdma" "nirfmxdemod" "nirfmxwlan" "nirfmxwlan_restricted" "nirfsa" "nirfsg" "niscope" "niscope_restricted" "niswitch" "nisync" "nitclk" "nixnet" "nixnetsocket")
set(nidrivers "nidaqmx" "nidcpower" "nidigitalpattern" "nidmm" "nifake" "nifake_extension" "nifake_non_ivi" "nifgen" "nimxlcterminaladaptor_restricted" "nirfmxbluetooth" "nirfmxcdma2k" "nirfmxgsm" "nirfmxinstr" "nirfmxinstr_restricted" "nirfmxlte" "nirfmxnr" "nirfmxspecan" "nirfmxspecan_restricted" "nirfmxtdscdma" "nirfmxwcdma" "nirfmxdemod" "nirfmxwlan" "nirfmxwlan_restricted" "nirfsa" "nirfsg" "niscope" "niscope_restricted" "niswitch" "nisync" "nitclk" "nixnet" "nixnetsocket" "visa")

#----------------------------------------------------------------------
# Create NI Driver APIs proto and server files
Expand Down
67 changes: 67 additions & 0 deletions examples/visa/find-resources.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
r"""Perform continuous measure record.
The gRPC API is built from the C API. NI-VISA documentation is installed with the driver at:
C:\Program Files (x86)\IVI Foundation\VISA\WinNT\NIvisa\ni-visa.chm
Getting Started:
To run this example, install "NI-VISA" on the server machine:
https://www.ni.com/en/support/downloads/drivers/download.ni-visa.html
For instructions on how to use protoc to generate gRPC client interfaces, see our "Creating a gRPC
Client" wiki page:
https://github.com/ni/grpc-device/wiki/Creating-a-gRPC-Client
Running from command line:
Server machine's IP address, port number, and resource name can be passed as separate command line
arguments.
> python find-resources.py <server_address> <port_number>
If they are not passed in as command line arguments, then by default the server address will be
"localhost:31763".
"""

import sys

import grpc
import visa_pb2 as visa_types
import visa_pb2_grpc as grpc_visa

SERVER_ADDRESS = "localhost"
SERVER_PORT = "31763"

# Read in cmd args
if len(sys.argv) >= 2:
SERVER_ADDRESS = sys.argv[1]
if len(sys.argv) >= 3:
SERVER_PORT = sys.argv[2]


# Create the communication channel for the remote host and a connection to the NI-VISA service.
channel = grpc.insecure_channel(f"{SERVER_ADDRESS}:{SERVER_PORT}")
client = grpc_visa.VisaStub(channel)

try:
# Query for resources.
find_resources_resp = client.FindRsrc(visa_types.FindRsrcRequest(expression="?*"))

# Get information about each resource.
for resource in find_resources_resp.instrument_descriptor:
parse_resp = client.ParseRsrcEx(visa_types.ParseRsrcExRequest(resource_name=resource))
print(
f"Resource: {parse_resp.expanded_unaliased_name}, Class: {parse_resp.resource_class}, Alias: {parse_resp.alias_if_exists}"
)

except grpc.RpcError as rpc_error:
error_message = str(rpc_error.details() or "")
for entry in rpc_error.trailing_metadata() or []:
if entry.key == "ni-error":
value = entry.value if isinstance(entry.value, str) else entry.value.decode("utf-8")
error_message += f"\nError status: {value}"
if rpc_error.code() == grpc.StatusCode.UNAVAILABLE:
error_message = f"Failed to connect to server on {SERVER_ADDRESS}:{SERVER_PORT}"
elif rpc_error.code() == grpc.StatusCode.UNIMPLEMENTED:
error_message = (
"The operation is not implemented or is not supported/enabled in this service"
)
print(f"{error_message}")
13 changes: 13 additions & 0 deletions generated/nifake_extension/nifake_extension.proto
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import "session.proto";

service NiFakeExtension {
rpc AddCoolFunctionality(AddCoolFunctionalityRequest) returns (AddCoolFunctionalityResponse);
rpc TestAddressParameters(TestAddressParametersRequest) returns (TestAddressParametersResponse);
}

message AddCoolFunctionalityRequest {
Expand All @@ -28,3 +29,15 @@ message AddCoolFunctionalityResponse {
int32 status = 1;
}

message TestAddressParametersRequest {
nidevice_grpc.Session vi = 1;
sint32 space = 2;
uint64 offset = 3;
fixed64 suggested = 4;
}

message TestAddressParametersResponse {
int32 status = 1;
fixed64 actual = 2;
}

20 changes: 20 additions & 0 deletions generated/nifake_extension/nifake_extension_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,25 @@ add_cool_functionality(const StubPtr& stub, const nidevice_grpc::Session& vi, co
return response;
}

TestAddressParametersResponse
test_address_parameters(const StubPtr& stub, const nidevice_grpc::Session& vi, const pb::int32& space, const pb::uint64& offset, const pb::uint64& suggested)
{
::grpc::ClientContext context;

auto request = TestAddressParametersRequest{};
request.mutable_vi()->CopyFrom(vi);
request.set_space(space);
request.set_offset(offset);
request.set_suggested(suggested);

auto response = TestAddressParametersResponse{};

raise_if_error(
stub->TestAddressParameters(&context, request, &response),
context);

return response;
}


} // namespace nifake_extension_grpc::experimental::client
1 change: 1 addition & 0 deletions generated/nifake_extension/nifake_extension_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ using namespace nidevice_grpc::experimental::client;


AddCoolFunctionalityResponse add_cool_functionality(const StubPtr& stub, const nidevice_grpc::Session& vi, const pb::int32& param);
TestAddressParametersResponse test_address_parameters(const StubPtr& stub, const nidevice_grpc::Session& vi, const pb::int32& space, const pb::uint64& offset, const pb::uint64& suggested);

} // namespace nifake_extension_grpc::experimental::client

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,9 @@ ViStatus AddCoolFunctionality(ViSession vi, ViInt32 param)
return niFakeExtension_AddCoolFunctionality(vi, param);
}

ViStatus TestAddressParameters(ViSession vi, ViInt16 space, ViUInt64 offset, ViAddr suggested, ViAddr* actual)
{
return niFakeExtension_TestAddressParameters(vi, space, offset, suggested, actual);
}

} // namespace nifake_extension_grpc
9 changes: 9 additions & 0 deletions generated/nifake_extension/nifake_extension_library.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ NiFakeExtensionLibrary::NiFakeExtensionLibrary(std::shared_ptr<nidevice_grpc::Sh
return;
}
function_pointers_.AddCoolFunctionality = reinterpret_cast<AddCoolFunctionalityPtr>(shared_library_->get_function_pointer("niFakeExtension_AddCoolFunctionality"));
function_pointers_.TestAddressParameters = reinterpret_cast<TestAddressParametersPtr>(shared_library_->get_function_pointer("niFakeExtension_TestAddressParameters"));
}

NiFakeExtensionLibrary::~NiFakeExtensionLibrary()
Expand All @@ -49,4 +50,12 @@ ViStatus NiFakeExtensionLibrary::AddCoolFunctionality(ViSession vi, ViInt32 para
return function_pointers_.AddCoolFunctionality(vi, param);
}

ViStatus NiFakeExtensionLibrary::TestAddressParameters(ViSession vi, ViInt16 space, ViUInt64 offset, ViAddr suggested, ViAddr* actual)
{
if (!function_pointers_.TestAddressParameters) {
throw nidevice_grpc::LibraryLoadException("Could not find niFakeExtension_TestAddressParameters.");
}
return function_pointers_.TestAddressParameters(vi, space, offset, suggested, actual);
}

} // namespace nifake_extension_grpc
3 changes: 3 additions & 0 deletions generated/nifake_extension/nifake_extension_library.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,15 @@ class NiFakeExtensionLibrary : public nifake_extension_grpc::NiFakeExtensionLibr

::grpc::Status check_function_exists(std::string functionName);
ViStatus AddCoolFunctionality(ViSession vi, ViInt32 param);
ViStatus TestAddressParameters(ViSession vi, ViInt16 space, ViUInt64 offset, ViAddr suggested, ViAddr* actual);

private:
using AddCoolFunctionalityPtr = decltype(&niFakeExtension_AddCoolFunctionality);
using TestAddressParametersPtr = decltype(&niFakeExtension_TestAddressParameters);

typedef struct FunctionPointers {
AddCoolFunctionalityPtr AddCoolFunctionality;
TestAddressParametersPtr TestAddressParameters;
} FunctionLoadStatus;

std::shared_ptr<nidevice_grpc::SharedLibraryInterface> shared_library_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class NiFakeExtensionLibraryInterface {
virtual ~NiFakeExtensionLibraryInterface() {}

virtual ViStatus AddCoolFunctionality(ViSession vi, ViInt32 param) = 0;
virtual ViStatus TestAddressParameters(ViSession vi, ViInt16 space, ViUInt64 offset, ViAddr suggested, ViAddr* actual) = 0;
};

} // namespace nifake_extension_grpc
Expand Down
1 change: 1 addition & 0 deletions generated/nifake_extension/nifake_extension_mock_library.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace unit {
class NiFakeExtensionMockLibrary : public nifake_extension_grpc::NiFakeExtensionLibraryInterface {
public:
MOCK_METHOD(ViStatus, AddCoolFunctionality, (ViSession vi, ViInt32 param), (override));
MOCK_METHOD(ViStatus, TestAddressParameters, (ViSession vi, ViInt16 space, ViUInt64 offset, ViAddr suggested, ViAddr* actual), (override));
};

} // namespace unit
Expand Down
27 changes: 27 additions & 0 deletions generated/nifake_extension/nifake_extension_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,33 @@ namespace nifake_extension_grpc {
}
}

//---------------------------------------------------------------------
//---------------------------------------------------------------------
::grpc::Status NiFakeExtensionService::TestAddressParameters(::grpc::ServerContext* context, const TestAddressParametersRequest* request, TestAddressParametersResponse* 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());
ViInt16 space = static_cast<ViInt16>(request->space());
ViUInt64 offset = request->offset();
ViAddr suggested = reinterpret_cast<ViAddr>(request->suggested());
ViAddr actual {};
auto status = library_->TestAddressParameters(vi, space, offset, suggested, &actual);
if (!status_ok(status)) {
return ConvertApiErrorStatusForViSession(context, status, vi);
}
response->set_status(status);
response->set_actual(reinterpret_cast<uint64_t>(actual));
return ::grpc::Status::OK;
}
catch (nidevice_grpc::NonDriverException& ex) {
return ex.GetStatus();
}
}


NiFakeExtensionFeatureToggles::NiFakeExtensionFeatureToggles(
const nidevice_grpc::FeatureToggles& feature_toggles)
Expand Down
1 change: 1 addition & 0 deletions generated/nifake_extension/nifake_extension_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class NiFakeExtensionService final : public NiFakeExtension::Service {
virtual ~NiFakeExtensionService();

::grpc::Status AddCoolFunctionality(::grpc::ServerContext* context, const AddCoolFunctionalityRequest* request, AddCoolFunctionalityResponse* response) override;
::grpc::Status TestAddressParameters(::grpc::ServerContext* context, const TestAddressParametersRequest* request, TestAddressParametersResponse* response) override;
private:
LibrarySharedPtr library_;
ResourceRepositorySharedPtr session_repository_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ message UnregisterSpecialClientSnapshotInterestResponse {
}

message GetSFPSessionAccessEnabledRequest {
string selector_string = 1;
string option_string = 1;
}

message GetSFPSessionAccessEnabledResponse {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -640,12 +640,12 @@ unregister_special_client_snapshot_interest(const StubPtr& stub, const std::stri
}

GetSFPSessionAccessEnabledResponse
get_sfp_session_access_enabled(const StubPtr& stub, const std::string& selector_string)
get_sfp_session_access_enabled(const StubPtr& stub, const std::string& option_string)
{
::grpc::ClientContext context;

auto request = GetSFPSessionAccessEnabledRequest{};
request.set_selector_string(selector_string);
request.set_option_string(option_string);

auto response = GetSFPSessionAccessEnabledResponse{};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ SaveConfigurationsToJSONResponse save_configurations_to_json(const StubPtr& stub
SetForceAllTracesEnabledResponse set_force_all_traces_enabled(const StubPtr& stub, const nidevice_grpc::Session& instrument, const std::string& channel_name, const pb::int32& attr_val);
SetIOTraceStatusResponse set_io_trace_status(const StubPtr& stub, const nidevice_grpc::Session& instrument, const pb::int32& io_trace_status);
UnregisterSpecialClientSnapshotInterestResponse unregister_special_client_snapshot_interest(const StubPtr& stub, const std::string& resource_name);
GetSFPSessionAccessEnabledResponse get_sfp_session_access_enabled(const StubPtr& stub, const std::string& selector_string);
GetSFPSessionAccessEnabledResponse get_sfp_session_access_enabled(const StubPtr& stub, const std::string& option_string);

} // namespace nirfmxinstr_restricted_grpc::experimental::client

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -374,12 +374,12 @@ int32 NiRFmxInstrRestrictedLibrary::UnregisterSpecialClientSnapshotInterest(char
return function_pointers_.UnregisterSpecialClientSnapshotInterest(resourceName);
}

int32 NiRFmxInstrRestrictedLibrary::GetSFPSessionAccessEnabled(char selectorString[], int32* isSFPSessionAccessEnabled)
int32 NiRFmxInstrRestrictedLibrary::GetSFPSessionAccessEnabled(char optionString[], int32* isSFPSessionAccessEnabled)
{
if (!function_pointers_.GetSFPSessionAccessEnabled) {
throw nidevice_grpc::LibraryLoadException("Could not find RFmxInstr_GetSFPSessionAccessEnabled.");
}
return function_pointers_.GetSFPSessionAccessEnabled(selectorString, isSFPSessionAccessEnabled);
return function_pointers_.GetSFPSessionAccessEnabled(optionString, isSFPSessionAccessEnabled);
}

} // namespace nirfmxinstr_restricted_grpc
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class NiRFmxInstrRestrictedLibrary : public nirfmxinstr_restricted_grpc::NiRFmxI
int32 SetForceAllTracesEnabled(niRFmxInstrHandle instrumentHandle, char channelName[], int32 attrVal);
int32 SetIOTraceStatus(niRFmxInstrHandle instrumentHandle, int32 IOTraceStatus);
int32 UnregisterSpecialClientSnapshotInterest(char resourceName[]);
int32 GetSFPSessionAccessEnabled(char selectorString[], int32* isSFPSessionAccessEnabled);
int32 GetSFPSessionAccessEnabled(char optionString[], int32* isSFPSessionAccessEnabled);

private:
using ConvertForPowerUnitsUtilityPtr = int32 (*)(niRFmxInstrHandle instrumentHandle, float64 referenceOrTriggerLevelIn, int32 inputPowerUnits, int32 outputPowerUnits, int32 terminalConfiguration, float64 bandwidth, float64* referenceOrTriggerLevelOut);
Expand Down Expand Up @@ -98,7 +98,7 @@ class NiRFmxInstrRestrictedLibrary : public nirfmxinstr_restricted_grpc::NiRFmxI
using SetForceAllTracesEnabledPtr = int32 (*)(niRFmxInstrHandle instrumentHandle, char channelName[], int32 attrVal);
using SetIOTraceStatusPtr = int32 (*)(niRFmxInstrHandle instrumentHandle, int32 IOTraceStatus);
using UnregisterSpecialClientSnapshotInterestPtr = int32 (*)(char resourceName[]);
using GetSFPSessionAccessEnabledPtr = int32 (*)(char selectorString[], int32* isSFPSessionAccessEnabled);
using GetSFPSessionAccessEnabledPtr = int32 (*)(char optionString[], int32* isSFPSessionAccessEnabled);

typedef struct FunctionPointers {
ConvertForPowerUnitsUtilityPtr ConvertForPowerUnitsUtility;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class NiRFmxInstrRestrictedLibraryInterface {
virtual int32 SetForceAllTracesEnabled(niRFmxInstrHandle instrumentHandle, char channelName[], int32 attrVal) = 0;
virtual int32 SetIOTraceStatus(niRFmxInstrHandle instrumentHandle, int32 IOTraceStatus) = 0;
virtual int32 UnregisterSpecialClientSnapshotInterest(char resourceName[]) = 0;
virtual int32 GetSFPSessionAccessEnabled(char selectorString[], int32* isSFPSessionAccessEnabled) = 0;
virtual int32 GetSFPSessionAccessEnabled(char optionString[], int32* isSFPSessionAccessEnabled) = 0;
};

} // namespace nirfmxinstr_restricted_grpc
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class NiRFmxInstrRestrictedMockLibrary : public nirfmxinstr_restricted_grpc::NiR
MOCK_METHOD(int32, SetForceAllTracesEnabled, (niRFmxInstrHandle instrumentHandle, char channelName[], int32 attrVal), (override));
MOCK_METHOD(int32, SetIOTraceStatus, (niRFmxInstrHandle instrumentHandle, int32 IOTraceStatus), (override));
MOCK_METHOD(int32, UnregisterSpecialClientSnapshotInterest, (char resourceName[]), (override));
MOCK_METHOD(int32, GetSFPSessionAccessEnabled, (char selectorString[], int32* isSFPSessionAccessEnabled), (override));
MOCK_METHOD(int32, GetSFPSessionAccessEnabled, (char optionString[], int32* isSFPSessionAccessEnabled), (override));
};

} // namespace unit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1076,10 +1076,10 @@ namespace nirfmxinstr_restricted_grpc {
return ::grpc::Status::CANCELLED;
}
try {
auto selector_string_mbcs = convert_from_grpc<std::string>(request->selector_string());
char* selector_string = (char*)selector_string_mbcs.c_str();
auto option_string_mbcs = convert_from_grpc<std::string>(request->option_string());
char* option_string = (char*)option_string_mbcs.c_str();
int32 is_sfp_session_access_enabled {};
auto status = library_->GetSFPSessionAccessEnabled(selector_string, &is_sfp_session_access_enabled);
auto status = library_->GetSFPSessionAccessEnabled(option_string, &is_sfp_session_access_enabled);
if (!status_ok(status)) {
return ConvertApiErrorStatusForNiRFmxInstrHandle(context, status, 0);
}
Expand Down
8 changes: 8 additions & 0 deletions generated/register_all_services.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
#include "nitclk/nitclk_service_registrar.h"
#include "nixnet/nixnet_service_registrar.h"
#include "nixnetsocket/nixnetsocket_service_registrar.h"
#include "visa/visa_service_registrar.h"

namespace nidevice_grpc {

Expand All @@ -95,6 +96,7 @@ std::shared_ptr<std::vector<std::shared_ptr<void>>> register_all_services(
auto nx_database_ref_t_repository = std::make_shared<nidevice_grpc::SessionResourceRepository<nxDatabaseRef_t>>(session_repository);
auto nx_socket_repository = std::make_shared<nidevice_grpc::SessionResourceRepository<nxSOCKET>>(session_repository);
auto nx_ip_stack_ref_t_repository = std::make_shared<nidevice_grpc::SessionResourceRepository<nxIpStackRef_t>>(session_repository);
auto vi_object_repository = std::make_shared<nidevice_grpc::SessionResourceRepository<ViObject>>(session_repository);

service_vector->push_back(
nidaqmx_grpc::register_service(
Expand Down Expand Up @@ -284,6 +286,12 @@ std::shared_ptr<std::vector<std::shared_ptr<void>>> register_all_services(
nx_socket_repository,
nx_ip_stack_ref_t_repository,
feature_toggles));
service_vector->push_back(
visa_grpc::register_service(
server_builder,
vi_session_repository,
vi_object_repository,
feature_toggles));

return service_vector;
}
Expand Down
Loading

0 comments on commit 0c2e46c

Please sign in to comment.