Skip to content

Commit

Permalink
Return supported event types when opening a session (#1000)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmondrik authored Sep 25, 2023
1 parent 1311245 commit 7d4dbb1
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 4 deletions.
1 change: 1 addition & 0 deletions generated/visa/visa.proto
Original file line number Diff line number Diff line change
Expand Up @@ -915,6 +915,7 @@ message OpenResponse {
int32 status = 1;
nidevice_grpc.Session vi = 2;
bool new_session_initialized = 3;
repeated EventType supported_events = 4;
}

message Out16Request {
Expand Down
7 changes: 7 additions & 0 deletions source/codegen/metadata/visa/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1005,6 +1005,13 @@
'name': 'newSessionInitialized',
'proto_only': True,
'type': 'bool'
},
{
'direction': 'out',
'grpc_type': 'repeated EventType',
'name': 'supportedEvents',
'proto_only': True,
'type': 'ViEvent'
}
],
'returns': 'ViStatus'
Expand Down
14 changes: 14 additions & 0 deletions source/custom/visa_service.custom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

// Copied from NiVisaImpl.h
#define VI_ATTR_RSRC_USER_ALIAS (0x3FFF018EUL) /* ViString */
#define VI_ATTR_NUM_SUP_EVENTS (0x3FFF019CUL) /* ViUInt16 */
#define VI_ATTR_SUP_EVENTS (0x3FFF019DUL) /* ViAEventType */

namespace visa_grpc {
using nidevice_grpc::converters::convert_from_grpc;
Expand Down Expand Up @@ -255,6 +257,18 @@ ::grpc::Status VisaService::Open(::grpc::ServerContext* context, const OpenReque
if (!status_ok(status)) {
return ConvertApiErrorStatusForViSession(context, status, rsrc_manager_handle);
}

ViUInt16 numEvents = 0;
ViSession vi = session_repository_->access_session(grpc_device_session_name);
if (library->GetAttribute(vi, VI_ATTR_NUM_SUP_EVENTS, &numEvents) == VI_SUCCESS && numEvents > 0) {
// NI-VISA writes the events followed by an extra "-1" value that we don't need.
std::vector<EventType> 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]);
}
}

response->set_status(status);
response->mutable_vi()->set_name(grpc_device_session_name);
response->set_new_session_initialized(new_session_initialized);
Expand Down
35 changes: 31 additions & 4 deletions source/tests/integration/visa_resource_manager_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace integration {

static const char* test_descriptor = "fake::descriptor";

void call_open(VisaService& service, const char* session_name)
OpenResponse call_open(VisaService& service, const char* session_name)
{
::grpc::ServerContext context;
OpenRequest request;
Expand All @@ -24,6 +24,7 @@ void call_open(VisaService& service, const char* session_name)
auto status = service.Open(&context, &request, &response);
EXPECT_TRUE(status.ok());
EXPECT_EQ(0, response.status());
return response;
}

void call_parse(VisaService& service)
Expand All @@ -44,6 +45,18 @@ ViStatus SetSessionToOne(ViSession* session)
return VI_SUCCESS;
}

ViStatus SetNumberEventsToOne(void* attributeValue)
{
*(ViUInt16*)attributeValue = 1;
return VI_SUCCESS;
}

ViStatus SetEventTypeToServiceRequest(void* attributeValue)
{
*(ViEventType*)attributeValue = VI_EVENT_SERVICE_REQ;
return VI_SUCCESS;
}

TEST(VisaResourceManagerTest, ParsePlusOpen_OpensResourceManagerOnce)
{
auto session_repository = std::make_shared<nidevice_grpc::SessionRepository>();
Expand All @@ -58,9 +71,15 @@ TEST(VisaResourceManagerTest, ParsePlusOpen_OpensResourceManagerOnce)
.Times(1);
EXPECT_CALL(*library, Open)
.Times(1);
EXPECT_CALL(*library, GetAttribute(_, static_cast<ViAttr>(0x3FFF019CUL), _))
.WillOnce(WithArg<2>(Invoke(SetNumberEventsToOne)));
EXPECT_CALL(*library, GetAttribute(_, static_cast<ViAttr>(0x3FFF019DUL), _))
.WillOnce(WithArg<2>(Invoke(SetEventTypeToServiceRequest)));

call_parse(service);
call_open(service, "name2");
auto response = call_open(service, "name2");
EXPECT_EQ(1, response.supported_events_size());
EXPECT_EQ(VI_EVENT_SERVICE_REQ, response.supported_events(0));

EXPECT_CALL(*library, Close)
.Times(2);
Expand All @@ -79,9 +98,14 @@ TEST(VisaResourceManagerTest, OpenTwoSessions_OpensResourceManagerOnce)
.WillOnce(WithArg<0>(Invoke(SetSessionToOne)));
EXPECT_CALL(*library, Open)
.Times(2);
EXPECT_CALL(*library, GetAttribute)
.Times(2)
.WillRepeatedly(Return(VI_ERROR_NSUP_ATTR));

call_open(service, "name1");
call_open(service, "name2");
auto response = call_open(service, "name1");
EXPECT_EQ(0, response.supported_events_size());
response = call_open(service, "name2");
EXPECT_EQ(0, response.supported_events_size());

EXPECT_CALL(*library, Close)
.Times(3);
Expand All @@ -101,6 +125,9 @@ TEST(VisaResourceManagerTest, ResetServerWithOpenSession_OpenNewSession_OpensRes
.WillRepeatedly(WithArg<0>(Invoke(SetSessionToOne)));
EXPECT_CALL(*library, Open)
.Times(2);
EXPECT_CALL(*library, GetAttribute)
.Times(2)
.WillRepeatedly(Return(VI_ERROR_NSUP_ATTR));
EXPECT_CALL(*library, Close)
.Times(4);

Expand Down

0 comments on commit 7d4dbb1

Please sign in to comment.