text: 
  //---------------------------------------------------------------------
  // This file is automatically generated. All manual edits will be lost.
  //---------------------------------------------------------------------
  // Service implementation for the NI-SYNC Metadata
  //---------------------------------------------------------------------
  #include "nisync_service.h"
  
  #include <sstream>
  #include <fstream>
  #include <iostream>
  #include <atomic>
  #include <vector>
  #include <server/converters.h>
  
  namespace nisync_grpc {
  
    using nidevice_grpc::converters::calculate_linked_array_size;
    using nidevice_grpc::converters::convert_from_grpc;
    using nidevice_grpc::converters::convert_to_grpc;
    using nidevice_grpc::converters::MatchState;
  
    const auto kErrorReadBufferTooSmall = -200229;
    const auto kWarningCAPIStringTruncatedToFitBuffer = 200026;
  
    NiSyncService::NiSyncService(
        NiSyncLibraryInterface* library,
        ResourceRepositorySharedPtr session_repository, 
        const NiSyncFeatureToggles& feature_toggles)
        : library_(library),
        session_repository_(session_repository),
        feature_toggles_(feature_toggles)
    {
    }
  
    NiSyncService::~NiSyncService()
    {
    }
  
    // Returns true if it's safe to use outputs of a method with the given status.
    inline bool status_ok(int32 status)
    {
      return status >= 0;
    }
  
    //---------------------------------------------------------------------
    //---------------------------------------------------------------------
    ::grpc::Status NiSyncService::Init(::grpc::ServerContext* context, const InitRequest* request, InitResponse* response)
    {
      if (context->IsCancelled()) {
        return ::grpc::Status::CANCELLED;
      }
      try {
        ViRsrc resource_name = (ViRsrc)request->resource_name().c_str();
        ViBoolean id_query = request->id_query();
        ViBoolean reset_device = request->reset_device();
  
        auto init_lambda = [&] () {
          ViSession vi;
          int status = library_->Init(resource_name, id_query, reset_device, &vi);
          return std::make_tuple(status, vi);
        };
        uint32_t session_id = 0;
        const std::string& grpc_device_session_name = request->session_name();
        auto cleanup_lambda = [&] (ViSession id) { library_->Close(id); };
        int status = session_repository_->add_session(grpc_device_session_name, init_lambda, cleanup_lambda, session_id);
        response->set_status(status);
        if (status_ok(status)) {
          response->mutable_vi()->set_id(session_id);
        }
        return ::grpc::Status::OK;
      }
      catch (nidevice_grpc::LibraryLoadException& ex) {
        return ::grpc::Status(::grpc::NOT_FOUND, ex.what());
      }
    }
  
    //---------------------------------------------------------------------
    //---------------------------------------------------------------------
    ::grpc::Status NiSyncService::Close(::grpc::ServerContext* context, const CloseRequest* request, CloseResponse* response)
    {
      if (context->IsCancelled()) {
        return ::grpc::Status::CANCELLED;
      }
      try {
        auto vi_grpc_session = request->vi();
        ViSession vi = session_repository_->access_session(vi_grpc_session.id(), vi_grpc_session.name());
        session_repository_->remove_session(vi);
        auto status = library_->Close(vi);
        response->set_status(status);
        return ::grpc::Status::OK;
      }
      catch (nidevice_grpc::LibraryLoadException& ex) {
        return ::grpc::Status(::grpc::NOT_FOUND, ex.what());
      }
    }
  
    //---------------------------------------------------------------------
    //---------------------------------------------------------------------
    ::grpc::Status NiSyncService::ErrorMessage(::grpc::ServerContext* context, const ErrorMessageRequest* request, ErrorMessageResponse* response)
    {
      if (context->IsCancelled()) {
        return ::grpc::Status::CANCELLED;
      }
      try {
        auto vi_grpc_session = request->vi();
        ViSession vi = session_repository_->access_session(vi_grpc_session.id(), vi_grpc_session.name());
        ViStatus error_code = request->error_code();
        std::string error_message(256 - 1, '\0');
        auto status = library_->ErrorMessage(vi, error_code, (ViChar*)error_message.data());
        response->set_status(status);
        if (status_ok(status)) {
          response->set_error_message(error_message);
          nidevice_grpc::converters::trim_trailing_nulls(*(response->mutable_error_message()));
        }
        return ::grpc::Status::OK;
      }
      catch (nidevice_grpc::LibraryLoadException& ex) {
        return ::grpc::Status(::grpc::NOT_FOUND, ex.what());
      }
    }
  
    //---------------------------------------------------------------------
    //---------------------------------------------------------------------
    ::grpc::Status NiSyncService::Reset(::grpc::ServerContext* context, const ResetRequest* request, ResetResponse* response)
    {
      if (context->IsCancelled()) {
        return ::grpc::Status::CANCELLED;
      }
      try {
        auto vi_grpc_session = request->vi();
        ViSession vi = session_repository_->access_session(vi_grpc_session.id(), vi_grpc_session.name());
        auto status = library_->Reset(vi);
        response->set_status(status);
        return ::grpc::Status::OK;
      }
      catch (nidevice_grpc::LibraryLoadException& ex) {
        return ::grpc::Status(::grpc::NOT_FOUND, ex.what());
      }
    }
  
    //---------------------------------------------------------------------
    //---------------------------------------------------------------------
    ::grpc::Status NiSyncService::PersistConfig(::grpc::ServerContext* context, const PersistConfigRequest* request, PersistConfigResponse* response)
    {
      if (context->IsCancelled()) {
        return ::grpc::Status::CANCELLED;
      }
      try {
        auto vi_grpc_session = request->vi();
        ViSession vi = session_repository_->access_session(vi_grpc_session.id(), vi_grpc_session.name());
        auto status = library_->PersistConfig(vi);
        response->set_status(status);
        return ::grpc::Status::OK;
      }
      catch (nidevice_grpc::LibraryLoadException& ex) {
        return ::grpc::Status(::grpc::NOT_FOUND, ex.what());
      }
    }
  
    //---------------------------------------------------------------------
    //---------------------------------------------------------------------
    ::grpc::Status NiSyncService::SelfTest(::grpc::ServerContext* context, const SelfTestRequest* request, SelfTestResponse* response)
    {
      if (context->IsCancelled()) {
        return ::grpc::Status::CANCELLED;
      }
      try {
        auto vi_grpc_session = request->vi();
        ViSession vi = session_repository_->access_session(vi_grpc_session.id(), vi_grpc_session.name());
        ViInt16 self_test_result {};
        std::string self_test_message(256 - 1, '\0');
        auto status = library_->SelfTest(vi, &self_test_result, (ViChar*)self_test_message.data());
        response->set_status(status);
        if (status_ok(status)) {
          response->set_self_test_result(self_test_result);
          response->set_self_test_message(self_test_message);
          nidevice_grpc::converters::trim_trailing_nulls(*(response->mutable_self_test_message()));
        }
        return ::grpc::Status::OK;
      }
      catch (nidevice_grpc::LibraryLoadException& ex) {
        return ::grpc::Status(::grpc::NOT_FOUND, ex.what());
      }
    }
  
    //---------------------------------------------------------------------
    //---------------------------------------------------------------------
    ::grpc::Status NiSyncService::RevisionQuery(::grpc::ServerContext* context, const RevisionQueryRequest* request, RevisionQueryResponse* response)
    {
      if (context->IsCancelled()) {
        return ::grpc::Status::CANCELLED;
      }
      try {
        auto vi_grpc_session = request->vi();
        ViSession vi = session_repository_->access_session(vi_grpc_session.id(), vi_grpc_session.name());
        std::string driver_revision(256 - 1, '\0');
        std::string firmware_revision(256 - 1, '\0');
        auto status = library_->RevisionQuery(vi, (ViChar*)driver_revision.data(), (ViChar*)firmware_revision.data());
        response->set_status(status);
        if (status_ok(status)) {
          response->set_driver_revision(driver_revision);
          nidevice_grpc::converters::trim_trailing_nulls(*(response->mutable_driver_revision()));
          response->set_firmware_revision(firmware_revision);
          nidevice_grpc::converters::trim_trailing_nulls(*(response->mutable_firmware_revision()));
        }
        return ::grpc::Status::OK;
      }
      catch (nidevice_grpc::LibraryLoadException& ex) {
        return ::grpc::Status(::grpc::NOT_FOUND, ex.what());
      }
    }
  
    //---------------------------------------------------------------------
    //---------------------------------------------------------------------
    ::grpc::Status NiSyncService::ConnectTrigTerminals(::grpc::ServerContext* context, const ConnectTrigTerminalsRequest* request, ConnectTrigTerminalsResponse* response)
    {
      if (context->IsCancelled()) {
        return ::grpc::Status::CANCELLED;
      }
      try {
        auto vi_grpc_session = request->vi();
        ViSession vi = session_repository_->access_session(vi_grpc_session.id(), vi_grpc_session.name());
        auto src_terminal = request->src_terminal().c_str();
        auto dest_terminal = request->dest_terminal().c_str();
        auto sync_clock = request->sync_clock().c_str();
        ViInt32 invert = request->invert();
        ViInt32 update_edge = request->update_edge();
        auto status = library_->ConnectTrigTerminals(vi, src_terminal, dest_terminal, sync_clock, invert, update_edge);
        response->set_status(status);
        return ::grpc::Status::OK;
      }
      catch (nidevice_grpc::LibraryLoadException& ex) {
        return ::grpc::Status(::grpc::NOT_FOUND, ex.what());
      }
    }
  
    //---------------------------------------------------------------------
    //---------------------------------------------------------------------
    ::grpc::Status NiSyncService::DisconnectTrigTerminals(::grpc::ServerContext* context, const DisconnectTrigTerminalsRequest* request, DisconnectTrigTerminalsResponse* response)
    {
      if (context->IsCancelled()) {
        return ::grpc::Status::CANCELLED;
      }
      try {
        auto vi_grpc_session = request->vi();
        ViSession vi = session_repository_->access_session(vi_grpc_session.id(), vi_grpc_session.name());
        auto src_terminal = request->src_terminal().c_str();
        auto dest_terminal = request->dest_terminal().c_str();
        auto status = library_->DisconnectTrigTerminals(vi, src_terminal, dest_terminal);
        response->set_status(status);
        return ::grpc::Status::OK;
      }
      catch (nidevice_grpc::LibraryLoadException& ex) {
        return ::grpc::Status(::grpc::NOT_FOUND, ex.what());
      }
    }
  
    //---------------------------------------------------------------------
    //---------------------------------------------------------------------
    ::grpc::Status NiSyncService::ConnectSWTrigToTerminal(::grpc::ServerContext* context, const ConnectSWTrigToTerminalRequest* request, ConnectSWTrigToTerminalResponse* response)
    {
      if (context->IsCancelled()) {
        return ::grpc::Status::CANCELLED;
      }
      try {
        auto vi_grpc_session = request->vi();
        ViSession vi = session_repository_->access_session(vi_grpc_session.id(), vi_grpc_session.name());
        auto src_terminal = request->src_terminal().c_str();
        auto dest_terminal = request->dest_terminal().c_str();
        auto sync_clock = request->sync_clock().c_str();
        ViInt32 invert = request->invert();
        ViInt32 update_edge = request->update_edge();
        ViReal64 delay = request->delay();
        auto status = library_->ConnectSWTrigToTerminal(vi, src_terminal, dest_terminal, sync_clock, invert, update_edge, delay);
        response->set_status(status);
        return ::grpc::Status::OK;
      }
      catch (nidevice_grpc::LibraryLoadException& ex) {
        return ::grpc::Status(::grpc::NOT_FOUND, ex.what());
      }
    }
  
    //---------------------------------------------------------------------
    //---------------------------------------------------------------------
    ::grpc::Status NiSyncService::DisconnectSWTrigFromTerminal(::grpc::ServerContext* context, const DisconnectSWTrigFromTerminalRequest* request, DisconnectSWTrigFromTerminalResponse* response)
    {
      if (context->IsCancelled()) {
        return ::grpc::Status::CANCELLED;
      }
      try {
        auto vi_grpc_session = request->vi();
        ViSession vi = session_repository_->access_session(vi_grpc_session.id(), vi_grpc_session.name());
        auto src_terminal = request->src_terminal().c_str();
        auto dest_terminal = request->dest_terminal().c_str();
        auto status = library_->DisconnectSWTrigFromTerminal(vi, src_terminal, dest_terminal);
        response->set_status(status);
        return ::grpc::Status::OK;
      }
      catch (nidevice_grpc::LibraryLoadException& ex) {
        return ::grpc::Status(::grpc::NOT_FOUND, ex.what());
      }
    }
  
    //---------------------------------------------------------------------
    //---------------------------------------------------------------------
    ::grpc::Status NiSyncService::SendSoftwareTrigger(::grpc::ServerContext* context, const SendSoftwareTriggerRequest* request, SendSoftwareTriggerResponse* response)
    {
      if (context->IsCancelled()) {
        return ::grpc::Status::CANCELLED;
      }
      try {
        auto vi_grpc_session = request->vi();
        ViSession vi = session_repository_->access_session(vi_grpc_session.id(), vi_grpc_session.name());
        auto src_terminal = request->src_terminal().c_str();
        auto status = library_->SendSoftwareTrigger(vi, src_terminal);
        response->set_status(status);
        return ::grpc::Status::OK;
      }
      catch (nidevice_grpc::LibraryLoadException& ex) {
        return ::grpc::Status(::grpc::NOT_FOUND, ex.what());
      }
    }
  
    //---------------------------------------------------------------------
    //---------------------------------------------------------------------
    ::grpc::Status NiSyncService::ConnectClkTerminals(::grpc::ServerContext* context, const ConnectClkTerminalsRequest* request, ConnectClkTerminalsResponse* response)
    {
      if (context->IsCancelled()) {
        return ::grpc::Status::CANCELLED;
      }
      try {
        auto vi_grpc_session = request->vi();
        ViSession vi = session_repository_->access_session(vi_grpc_session.id(), vi_grpc_session.name());
        auto src_terminal = request->src_terminal().c_str();
        auto dest_terminal = request->dest_terminal().c_str();
        auto status = library_->ConnectClkTerminals(vi, src_terminal, dest_terminal);
        response->set_status(status);
        return ::grpc::Status::OK;
      }
      catch (nidevice_grpc::LibraryLoadException& ex) {
        return ::grpc::Status(::grpc::NOT_FOUND, ex.what());
      }
    }
  
    //---------------------------------------------------------------------
    //---------------------------------------------------------------------
    ::grpc::Status NiSyncService::DisconnectClkTerminals(::grpc::ServerContext* context, const DisconnectClkTerminalsRequest* request, DisconnectClkTerminalsResponse* response)
    {
      if (context->IsCancelled()) {
        return ::grpc::Status::CANCELLED;
      }
      try {
        auto vi_grpc_session = request->vi();
        ViSession vi = session_repository_->access_session(vi_grpc_session.id(), vi_grpc_session.name());
        auto src_terminal = request->src_terminal().c_str();
        auto dest_terminal = request->dest_terminal().c_str();
        auto status = library_->DisconnectClkTerminals(vi, src_terminal, dest_terminal);
        response->set_status(status);
        return ::grpc::Status::OK;
      }
      catch (nidevice_grpc::LibraryLoadException& ex) {
        return ::grpc::Status(::grpc::NOT_FOUND, ex.what());
      }
    }
  
    //---------------------------------------------------------------------
    //---------------------------------------------------------------------
    ::grpc::Status NiSyncService::MeasureFrequency(::grpc::ServerContext* context, const MeasureFrequencyRequest* request, MeasureFrequencyResponse* response)
    {
      if (context->IsCancelled()) {
        return ::grpc::Status::CANCELLED;
      }
      try {
        auto vi_grpc_session = request->vi();
        ViSession vi = session_repository_->access_session(vi_grpc_session.id(), vi_grpc_session.name());
        auto src_terminal = request->src_terminal().c_str();
        ViReal64 duration = request->duration();
        ViReal64 actual_duration {};
        ViReal64 frequency {};
        ViReal64 error {};
        auto status = library_->MeasureFrequency(vi, src_terminal, duration, &actual_duration, &frequency, &error);
        response->set_status(status);
        if (status_ok(status)) {
          response->set_actual_duration(actual_duration);
          response->set_frequency(frequency);
          response->set_error(error);
        }
        return ::grpc::Status::OK;
      }
      catch (nidevice_grpc::LibraryLoadException& ex) {
        return ::grpc::Status(::grpc::NOT_FOUND, ex.what());
      }
    }
  
    //---------------------------------------------------------------------
    //---------------------------------------------------------------------
    ::grpc::Status NiSyncService::MeasureFrequencyEx(::grpc::ServerContext* context, const MeasureFrequencyExRequest* request, MeasureFrequencyExResponse* response)
    {
      if (context->IsCancelled()) {
        return ::grpc::Status::CANCELLED;
      }
      try {
        auto vi_grpc_session = request->vi();
        ViSession vi = session_repository_->access_session(vi_grpc_session.id(), vi_grpc_session.name());
        auto src_terminal = request->src_terminal().c_str();
        ViReal64 duration = request->duration();
        ViUInt32 decimation_count = request->decimation_count();
        ViReal64 actual_duration {};
        ViReal64 frequency {};
        ViReal64 frequency_error {};
        auto status = library_->MeasureFrequencyEx(vi, src_terminal, duration, decimation_count, &actual_duration, &frequency, &frequency_error);
        response->set_status(status);
        if (status_ok(status)) {
          response->set_actual_duration(actual_duration);
          response->set_frequency(frequency);
          response->set_frequency_error(frequency_error);
        }
        return ::grpc::Status::OK;
      }
      catch (nidevice_grpc::LibraryLoadException& ex) {
        return ::grpc::Status(::grpc::NOT_FOUND, ex.what());
      }
    }
  
    //---------------------------------------------------------------------
    //---------------------------------------------------------------------
    ::grpc::Status NiSyncService::Start1588(::grpc::ServerContext* context, const Start1588Request* request, Start1588Response* response)
    {
      if (context->IsCancelled()) {
        return ::grpc::Status::CANCELLED;
      }
      try {
        auto vi_grpc_session = request->vi();
        ViSession vi = session_repository_->access_session(vi_grpc_session.id(), vi_grpc_session.name());
        auto status = library_->Start1588(vi);
        response->set_status(status);
        return ::grpc::Status::OK;
      }
      catch (nidevice_grpc::LibraryLoadException& ex) {
        return ::grpc::Status(::grpc::NOT_FOUND, ex.what());
      }
    }
  
    //---------------------------------------------------------------------
    //---------------------------------------------------------------------
    ::grpc::Status NiSyncService::Stop1588(::grpc::ServerContext* context, const Stop1588Request* request, Stop1588Response* response)
    {
      if (context->IsCancelled()) {
        return ::grpc::Status::CANCELLED;
      }
      try {
        auto vi_grpc_session = request->vi();
        ViSession vi = session_repository_->access_session(vi_grpc_session.id(), vi_grpc_session.name());
        auto status = library_->Stop1588(vi);
        response->set_status(status);
        return ::grpc::Status::OK;
      }
      catch (nidevice_grpc::LibraryLoadException& ex) {
        return ::grpc::Status(::grpc::NOT_FOUND, ex.what());
      }
    }
  
    //---------------------------------------------------------------------
    //---------------------------------------------------------------------
    ::grpc::Status NiSyncService::Start8021AS(::grpc::ServerContext* context, const Start8021ASRequest* request, Start8021ASResponse* response)
    {
      if (context->IsCancelled()) {
        return ::grpc::Status::CANCELLED;
      }
      try {
        auto vi_grpc_session = request->vi();
        ViSession vi = session_repository_->access_session(vi_grpc_session.id(), vi_grpc_session.name());
        auto status = library_->Start8021AS(vi);
        response->set_status(status);
        return ::grpc::Status::OK;
      }
      catch (nidevice_grpc::LibraryLoadException& ex) {
        return ::grpc::Status(::grpc::NOT_FOUND, ex.what());
      }
    }
  
    //---------------------------------------------------------------------
    //---------------------------------------------------------------------
    ::grpc::Status NiSyncService::Stop8021AS(::grpc::ServerContext* context, const Stop8021ASRequest* request, Stop8021ASResponse* response)
    {
      if (context->IsCancelled()) {
        return ::grpc::Status::CANCELLED;
      }
      try {
        auto vi_grpc_session = request->vi();
        ViSession vi = session_repository_->access_session(vi_grpc_session.id(), vi_grpc_session.name());
        auto status = library_->Stop8021AS(vi);
        response->set_status(status);
        return ::grpc::Status::OK;
      }
      catch (nidevice_grpc::LibraryLoadException& ex) {
        return ::grpc::Status(::grpc::NOT_FOUND, ex.what());
      }
    }
  
    //---------------------------------------------------------------------
    //---------------------------------------------------------------------
    ::grpc::Status NiSyncService::SetTime(::grpc::ServerContext* context, const SetTimeRequest* request, SetTimeResponse* response)
    {
      if (context->IsCancelled()) {
        return ::grpc::Status::CANCELLED;
      }
      try {
        auto vi_grpc_session = request->vi();
        ViSession vi = session_repository_->access_session(vi_grpc_session.id(), vi_grpc_session.name());
        ViInt32 time_source = request->time_source();
        ViUInt32 time_seconds = request->time_seconds();
        ViUInt32 time_nanoseconds = request->time_nanoseconds();
        ViUInt16 time_fractional_nanoseconds = request->time_fractional_nanoseconds();
        auto status = library_->SetTime(vi, time_source, time_seconds, time_nanoseconds, time_fractional_nanoseconds);
        response->set_status(status);
        return ::grpc::Status::OK;
      }
      catch (nidevice_grpc::LibraryLoadException& ex) {
        return ::grpc::Status(::grpc::NOT_FOUND, ex.what());
      }
    }
  
    //---------------------------------------------------------------------
    //---------------------------------------------------------------------
    ::grpc::Status NiSyncService::GetTime(::grpc::ServerContext* context, const GetTimeRequest* request, GetTimeResponse* response)
    {
      if (context->IsCancelled()) {
        return ::grpc::Status::CANCELLED;
      }
      try {
        auto vi_grpc_session = request->vi();
        ViSession vi = session_repository_->access_session(vi_grpc_session.id(), vi_grpc_session.name());
        ViUInt32 time_seconds {};
        ViUInt32 time_nanoseconds {};
        ViUInt16 time_fractional_nanoseconds {};
        auto status = library_->GetTime(vi, &time_seconds, &time_nanoseconds, &time_fractional_nanoseconds);
        response->set_status(status);
        if (status_ok(status)) {
          response->set_time_seconds(time_seconds);
          response->set_time_nanoseconds(time_nanoseconds);
          response->set_time_fractional_nanoseconds(time_fractional_nanoseconds);
        }
        return ::grpc::Status::OK;
      }
      catch (nidevice_grpc::LibraryLoadException& ex) {
        return ::grpc::Status(::grpc::NOT_FOUND, ex.what());
      }
    }
  
    //---------------------------------------------------------------------
    //---------------------------------------------------------------------
    ::grpc::Status NiSyncService::ResetFrequency(::grpc::ServerContext* context, const ResetFrequencyRequest* request, ResetFrequencyResponse* response)
    {
      if (context->IsCancelled()) {
        return ::grpc::Status::CANCELLED;
      }
      try {
        auto vi_grpc_session = request->vi();
        ViSession vi = session_repository_->access_session(vi_grpc_session.id(), vi_grpc_session.name());
        auto status = library_->ResetFrequency(vi);
        response->set_status(status);
        return ::grpc::Status::OK;
      }
      catch (nidevice_grpc::LibraryLoadException& ex) {
        return ::grpc::Status(::grpc::NOT_FOUND, ex.what());
      }
    }
  
    //---------------------------------------------------------------------
    //---------------------------------------------------------------------
    ::grpc::Status NiSyncService::CreateFutureTimeEvent(::grpc::ServerContext* context, const CreateFutureTimeEventRequest* request, CreateFutureTimeEventResponse* response)
    {
      if (context->IsCancelled()) {
        return ::grpc::Status::CANCELLED;
      }
      try {
        auto vi_grpc_session = request->vi();
        ViSession vi = session_repository_->access_session(vi_grpc_session.id(), vi_grpc_session.name());
        auto terminal = request->terminal().c_str();
        ViInt32 output_level = request->output_level();
        ViUInt32 time_seconds = request->time_seconds();
        ViUInt32 time_nanoseconds = request->time_nanoseconds();
        ViUInt16 time_fractional_nanoseconds = request->time_fractional_nanoseconds();
        auto status = library_->CreateFutureTimeEvent(vi, terminal, output_level, time_seconds, time_nanoseconds, time_fractional_nanoseconds);
        response->set_status(status);
        return ::grpc::Status::OK;
      }
      catch (nidevice_grpc::LibraryLoadException& ex) {
        return ::grpc::Status(::grpc::NOT_FOUND, ex.what());
      }
    }
  
    //---------------------------------------------------------------------
    //---------------------------------------------------------------------
    ::grpc::Status NiSyncService::ClearFutureTimeEvents(::grpc::ServerContext* context, const ClearFutureTimeEventsRequest* request, ClearFutureTimeEventsResponse* response)
    {
      if (context->IsCancelled()) {
        return ::grpc::Status::CANCELLED;
      }
      try {
        auto vi_grpc_session = request->vi();
        ViSession vi = session_repository_->access_session(vi_grpc_session.id(), vi_grpc_session.name());
        auto terminal = request->terminal().c_str();
        auto status = library_->ClearFutureTimeEvents(vi, terminal);
        response->set_status(status);
        return ::grpc::Status::OK;
      }
      catch (nidevice_grpc::LibraryLoadException& ex) {
        return ::grpc::Status(::grpc::NOT_FOUND, ex.what());
      }
    }
  
    //---------------------------------------------------------------------
    //---------------------------------------------------------------------
    ::grpc::Status NiSyncService::EnableTimeStampTrigger(::grpc::ServerContext* context, const EnableTimeStampTriggerRequest* request, EnableTimeStampTriggerResponse* response)
    {
      if (context->IsCancelled()) {
        return ::grpc::Status::CANCELLED;
      }
      try {
        auto vi_grpc_session = request->vi();
        ViSession vi = session_repository_->access_session(vi_grpc_session.id(), vi_grpc_session.name());
        auto terminal = request->terminal().c_str();
        ViInt32 active_edge = request->active_edge();
        auto status = library_->EnableTimeStampTrigger(vi, terminal, active_edge);
        response->set_status(status);
        return ::grpc::Status::OK;
      }
      catch (nidevice_grpc::LibraryLoadException& ex) {
        return ::grpc::Status(::grpc::NOT_FOUND, ex.what());
      }
    }
  
    //---------------------------------------------------------------------
    //---------------------------------------------------------------------
    ::grpc::Status NiSyncService::EnableTimeStampTriggerWithDecimation(::grpc::ServerContext* context, const EnableTimeStampTriggerWithDecimationRequest* request, EnableTimeStampTriggerWithDecimationResponse* response)
    {
      if (context->IsCancelled()) {
        return ::grpc::Status::CANCELLED;
      }
      try {
        auto vi_grpc_session = request->vi();
        ViSession vi = session_repository_->access_session(vi_grpc_session.id(), vi_grpc_session.name());
        auto terminal = request->terminal().c_str();
        ViInt32 active_edge = request->active_edge();
        ViUInt32 decimation_count = request->decimation_count();
        auto status = library_->EnableTimeStampTriggerWithDecimation(vi, terminal, active_edge, decimation_count);
        response->set_status(status);
        return ::grpc::Status::OK;
      }
      catch (nidevice_grpc::LibraryLoadException& ex) {
        return ::grpc::Status(::grpc::NOT_FOUND, ex.what());
      }
    }
  
    //---------------------------------------------------------------------
    //---------------------------------------------------------------------
    ::grpc::Status NiSyncService::ReadTriggerTimeStamp(::grpc::ServerContext* context, const ReadTriggerTimeStampRequest* request, ReadTriggerTimeStampResponse* response)
    {
      if (context->IsCancelled()) {
        return ::grpc::Status::CANCELLED;
      }
      try {
        auto vi_grpc_session = request->vi();
        ViSession vi = session_repository_->access_session(vi_grpc_session.id(), vi_grpc_session.name());
        auto terminal = request->terminal().c_str();
        ViReal64 timeout = request->timeout();
        ViUInt32 time_seconds {};
        ViUInt32 time_nanoseconds {};
        ViUInt16 time_fractional_nanoseconds {};
        ViInt32 detected_edge {};
        auto status = library_->ReadTriggerTimeStamp(vi, terminal, timeout, &time_seconds, &time_nanoseconds, &time_fractional_nanoseconds, &detected_edge);
        response->set_status(status);
        if (status_ok(status)) {
          response->set_time_seconds(time_seconds);
          response->set_time_nanoseconds(time_nanoseconds);
          response->set_time_fractional_nanoseconds(time_fractional_nanoseconds);
          response->set_detected_edge(detected_edge);
        }
        return ::grpc::Status::OK;
      }
      catch (nidevice_grpc::LibraryLoadException& ex) {
        return ::grpc::Status(::grpc::NOT_FOUND, ex.what());
      }
    }
  
    //---------------------------------------------------------------------
    //---------------------------------------------------------------------
    ::grpc::Status NiSyncService::DisableTimeStampTrigger(::grpc::ServerContext* context, const DisableTimeStampTriggerRequest* request, DisableTimeStampTriggerResponse* response)
    {
      if (context->IsCancelled()) {
        return ::grpc::Status::CANCELLED;
      }
      try {
        auto vi_grpc_session = request->vi();
        ViSession vi = session_repository_->access_session(vi_grpc_session.id(), vi_grpc_session.name());
        auto terminal = request->terminal().c_str();
        auto status = library_->DisableTimeStampTrigger(vi, terminal);
        response->set_status(status);
        return ::grpc::Status::OK;
      }
      catch (nidevice_grpc::LibraryLoadException& ex) {
        return ::grpc::Status(::grpc::NOT_FOUND, ex.what());
      }
    }
  
    //---------------------------------------------------------------------
    //---------------------------------------------------------------------
    ::grpc::Status NiSyncService::CreateClock(::grpc::ServerContext* context, const CreateClockRequest* request, CreateClockResponse* response)
    {
      if (context->IsCancelled()) {
        return ::grpc::Status::CANCELLED;
      }
      try {
        auto vi_grpc_session = request->vi();
        ViSession vi = session_repository_->access_session(vi_grpc_session.id(), vi_grpc_session.name());
        auto terminal = request->terminal().c_str();
        ViUInt32 high_ticks = request->high_ticks();
        ViUInt32 low_ticks = request->low_ticks();
        ViUInt32 start_time_seconds = request->start_time_seconds();
        ViUInt32 start_time_nanoseconds = request->start_time_nanoseconds();
        ViUInt16 start_time_fractional_nanoseconds = request->start_time_fractional_nanoseconds();
        ViUInt32 stop_time_seconds = request->stop_time_seconds();
        ViUInt32 stop_time_nanoseconds = request->stop_time_nanoseconds();
        ViUInt16 stop_time_fractional_nanoseconds = request->stop_time_fractional_nanoseconds();
        auto status = library_->CreateClock(vi, terminal, high_ticks, low_ticks, start_time_seconds, start_time_nanoseconds, start_time_fractional_nanoseconds, stop_time_seconds, stop_time_nanoseconds, stop_time_fractional_nanoseconds);
        response->set_status(status);
        return ::grpc::Status::OK;
      }
      catch (nidevice_grpc::LibraryLoadException& ex) {
        return ::grpc::Status(::grpc::NOT_FOUND, ex.what());
      }
    }
  
    //---------------------------------------------------------------------
    //---------------------------------------------------------------------
    ::grpc::Status NiSyncService::ClearClock(::grpc::ServerContext* context, const ClearClockRequest* request, ClearClockResponse* response)
    {
      if (context->IsCancelled()) {
        return ::grpc::Status::CANCELLED;
      }
      try {
        auto vi_grpc_session = request->vi();
        ViSession vi = session_repository_->access_session(vi_grpc_session.id(), vi_grpc_session.name());
        auto terminal = request->terminal().c_str();
        auto status = library_->ClearClock(vi, terminal);
        response->set_status(status);
        return ::grpc::Status::OK;
      }
      catch (nidevice_grpc::LibraryLoadException& ex) {
        return ::grpc::Status(::grpc::NOT_FOUND, ex.what());
      }
    }
  
    //---------------------------------------------------------------------
    //---------------------------------------------------------------------
    ::grpc::Status NiSyncService::SetTimeReferenceFreeRunning(::grpc::ServerContext* context, const SetTimeReferenceFreeRunningRequest* request, SetTimeReferenceFreeRunningResponse* response)
    {
      if (context->IsCancelled()) {
        return ::grpc::Status::CANCELLED;
      }
      try {
        auto vi_grpc_session = request->vi();
        ViSession vi = session_repository_->access_session(vi_grpc_session.id(), vi_grpc_session.name());
        auto status = library_->SetTimeReferenceFreeRunning(vi);
        response->set_status(status);
        return ::grpc::Status::OK;
      }
      catch (nidevice_grpc::LibraryLoadException& ex) {
        return ::grpc::Status(::grpc::NOT_FOUND, ex.what());
      }
    }
  
    //---------------------------------------------------------------------
    //---------------------------------------------------------------------
    ::grpc::Status NiSyncService::SetTimeReferenceGPS(::grpc::ServerContext* context, const SetTimeReferenceGPSRequest* request, SetTimeReferenceGPSResponse* response)
    {
      if (context->IsCancelled()) {
        return ::grpc::Status::CANCELLED;
      }
      try {
        auto vi_grpc_session = request->vi();
        ViSession vi = session_repository_->access_session(vi_grpc_session.id(), vi_grpc_session.name());
        auto status = library_->SetTimeReferenceGPS(vi);
        response->set_status(status);
        return ::grpc::Status::OK;
      }
      catch (nidevice_grpc::LibraryLoadException& ex) {
        return ::grpc::Status(::grpc::NOT_FOUND, ex.what());
      }
    }
  
    //---------------------------------------------------------------------
    //---------------------------------------------------------------------
    ::grpc::Status NiSyncService::SetTimeReferenceIRIG(::grpc::ServerContext* context, const SetTimeReferenceIRIGRequest* request, SetTimeReferenceIRIGResponse* response)
    {
      if (context->IsCancelled()) {
        return ::grpc::Status::CANCELLED;
      }
      try {
        auto vi_grpc_session = request->vi();
        ViSession vi = session_repository_->access_session(vi_grpc_session.id(), vi_grpc_session.name());
        ViInt32 irig_type = request->irig_type();
        auto terminal_name = request->terminal_name().c_str();
        auto status = library_->SetTimeReferenceIRIG(vi, irig_type, terminal_name);
        response->set_status(status);
        return ::grpc::Status::OK;
      }
      catch (nidevice_grpc::LibraryLoadException& ex) {
        return ::grpc::Status(::grpc::NOT_FOUND, ex.what());
      }
    }
  
    //---------------------------------------------------------------------
    //---------------------------------------------------------------------
    ::grpc::Status NiSyncService::SetTimeReferencePPS(::grpc::ServerContext* context, const SetTimeReferencePPSRequest* request, SetTimeReferencePPSResponse* response)
    {
      if (context->IsCancelled()) {
        return ::grpc::Status::CANCELLED;
      }
      try {
        auto vi_grpc_session = request->vi();
        ViSession vi = session_repository_->access_session(vi_grpc_session.id(), vi_grpc_session.name());
        auto terminal_name = request->terminal_name().c_str();
        ViBoolean use_manual_time = request->use_manual_time();
        ViUInt32 initial_time_seconds = request->initial_time_seconds();
        ViUInt32 initial_time_nanoseconds = request->initial_time_nanoseconds();
        ViUInt16 initial_time_fractional_nanoseconds = request->initial_time_fractional_nanoseconds();
        auto status = library_->SetTimeReferencePPS(vi, terminal_name, use_manual_time, initial_time_seconds, initial_time_nanoseconds, initial_time_fractional_nanoseconds);
        response->set_status(status);
        return ::grpc::Status::OK;
      }
      catch (nidevice_grpc::LibraryLoadException& ex) {
        return ::grpc::Status(::grpc::NOT_FOUND, ex.what());
      }
    }
  
    //---------------------------------------------------------------------
    //---------------------------------------------------------------------
    ::grpc::Status NiSyncService::SetTimeReference1588OrdinaryClock(::grpc::ServerContext* context, const SetTimeReference1588OrdinaryClockRequest* request, SetTimeReference1588OrdinaryClockResponse* response)
    {
      if (context->IsCancelled()) {
        return ::grpc::Status::CANCELLED;
      }
      try {
        auto vi_grpc_session = request->vi();
        ViSession vi = session_repository_->access_session(vi_grpc_session.id(), vi_grpc_session.name());
        auto status = library_->SetTimeReference1588OrdinaryClock(vi);
        response->set_status(status);
        return ::grpc::Status::OK;
      }
      catch (nidevice_grpc::LibraryLoadException& ex) {
        return ::grpc::Status(::grpc::NOT_FOUND, ex.what());
      }
    }
  
    //---------------------------------------------------------------------
    //---------------------------------------------------------------------
    ::grpc::Status NiSyncService::SetTimeReference8021AS(::grpc::ServerContext* context, const SetTimeReference8021ASRequest* request, SetTimeReference8021ASResponse* response)
    {
      if (context->IsCancelled()) {
        return ::grpc::Status::CANCELLED;
      }
      try {
        auto vi_grpc_session = request->vi();
        ViSession vi = session_repository_->access_session(vi_grpc_session.id(), vi_grpc_session.name());
        auto status = library_->SetTimeReference8021AS(vi);
        response->set_status(status);
        return ::grpc::Status::OK;
      }
      catch (nidevice_grpc::LibraryLoadException& ex) {
        return ::grpc::Status(::grpc::NOT_FOUND, ex.what());
      }
    }
  
    //---------------------------------------------------------------------
    //---------------------------------------------------------------------
    ::grpc::Status NiSyncService::EnableGPSTimestamping(::grpc::ServerContext* context, const EnableGPSTimestampingRequest* request, EnableGPSTimestampingResponse* response)
    {
      if (context->IsCancelled()) {
        return ::grpc::Status::CANCELLED;
      }
      try {
        auto vi_grpc_session = request->vi();
        ViSession vi = session_repository_->access_session(vi_grpc_session.id(), vi_grpc_session.name());
        auto status = library_->EnableGPSTimestamping(vi);
        response->set_status(status);
        return ::grpc::Status::OK;
      }
      catch (nidevice_grpc::LibraryLoadException& ex) {
        return ::grpc::Status(::grpc::NOT_FOUND, ex.what());
      }
    }
  
    //---------------------------------------------------------------------
    //---------------------------------------------------------------------
    ::grpc::Status NiSyncService::EnableIRIGTimestamping(::grpc::ServerContext* context, const EnableIRIGTimestampingRequest* request, EnableIRIGTimestampingResponse* response)
    {
      if (context->IsCancelled()) {
        return ::grpc::Status::CANCELLED;
      }
      try {
        auto vi_grpc_session = request->vi();
        ViSession vi = session_repository_->access_session(vi_grpc_session.id(), vi_grpc_session.name());
        ViInt32 irig_type = request->irig_type();
        auto terminal_name = request->terminal_name().c_str();
        auto status = library_->EnableIRIGTimestamping(vi, irig_type, terminal_name);
        response->set_status(status);
        return ::grpc::Status::OK;
      }
      catch (nidevice_grpc::LibraryLoadException& ex) {
        return ::grpc::Status(::grpc::NOT_FOUND, ex.what());
      }
    }
  
    //---------------------------------------------------------------------
    //---------------------------------------------------------------------
    ::grpc::Status NiSyncService::ReadLastGPSTimestamp(::grpc::ServerContext* context, const ReadLastGPSTimestampRequest* request, ReadLastGPSTimestampResponse* response)
    {
      if (context->IsCancelled()) {
        return ::grpc::Status::CANCELLED;
      }
      try {
        auto vi_grpc_session = request->vi();
        ViSession vi = session_repository_->access_session(vi_grpc_session.id(), vi_grpc_session.name());
        ViUInt32 timestamp_seconds {};
        ViUInt32 timestamp_nanoseconds {};
        ViUInt16 timestamp_fractional_nanoseconds {};
        ViUInt32 gps_seconds {};
        ViUInt32 gps_nanoseconds {};
        ViUInt16 gps_fractional_nanoseconds {};
        auto status = library_->ReadLastGPSTimestamp(vi, &timestamp_seconds, &timestamp_nanoseconds, &timestamp_fractional_nanoseconds, &gps_seconds, &gps_nanoseconds, &gps_fractional_nanoseconds);
        response->set_status(status);
        if (status_ok(status)) {
          response->set_timestamp_seconds(timestamp_seconds);
          response->set_timestamp_nanoseconds(timestamp_nanoseconds);
          response->set_timestamp_fractional_nanoseconds(timestamp_fractional_nanoseconds);
          response->set_gps_seconds(gps_seconds);
          response->set_gps_nanoseconds(gps_nanoseconds);
          response->set_gps_fractional_nanoseconds(gps_fractional_nanoseconds);
        }
        return ::grpc::Status::OK;
      }
      catch (nidevice_grpc::LibraryLoadException& ex) {
        return ::grpc::Status(::grpc::NOT_FOUND, ex.what());
      }
    }
  
    //---------------------------------------------------------------------
    //---------------------------------------------------------------------
    ::grpc::Status NiSyncService::ReadLastIRIGTimestamp(::grpc::ServerContext* context, const ReadLastIRIGTimestampRequest* request, ReadLastIRIGTimestampResponse* response)
    {
      if (context->IsCancelled()) {
        return ::grpc::Status::CANCELLED;
      }
      try {
        auto vi_grpc_session = request->vi();
        ViSession vi = session_repository_->access_session(vi_grpc_session.id(), vi_grpc_session.name());
        auto terminal = request->terminal().c_str();
        ViUInt32 timestamp_seconds {};
        ViUInt32 timestamp_nanoseconds {};
        ViUInt16 timestamp_fractional_nanoseconds {};
        ViUInt32 irigb_seconds {};
        ViUInt32 irigb_nanoseconds {};
        ViUInt16 irigb_fractional_nanoseconds {};
        auto status = library_->ReadLastIRIGTimestamp(vi, terminal, &timestamp_seconds, &timestamp_nanoseconds, &timestamp_fractional_nanoseconds, &irigb_seconds, &irigb_nanoseconds, &irigb_fractional_nanoseconds);
        response->set_status(status);
        if (status_ok(status)) {
          response->set_timestamp_seconds(timestamp_seconds);
          response->set_timestamp_nanoseconds(timestamp_nanoseconds);
          response->set_timestamp_fractional_nanoseconds(timestamp_fractional_nanoseconds);
          response->set_irigb_seconds(irigb_seconds);
          response->set_irigb_nanoseconds(irigb_nanoseconds);
          response->set_irigb_fractional_nanoseconds(irigb_fractional_nanoseconds);
        }
        return ::grpc::Status::OK;
      }
      catch (nidevice_grpc::LibraryLoadException& ex) {
        return ::grpc::Status(::grpc::NOT_FOUND, ex.what());
      }
    }
  
    //---------------------------------------------------------------------
    //---------------------------------------------------------------------
    ::grpc::Status NiSyncService::DisableGPSTimestamping(::grpc::ServerContext* context, const DisableGPSTimestampingRequest* request, DisableGPSTimestampingResponse* response)
    {
      if (context->IsCancelled()) {
        return ::grpc::Status::CANCELLED;
      }
      try {
        auto vi_grpc_session = request->vi();
        ViSession vi = session_repository_->access_session(vi_grpc_session.id(), vi_grpc_session.name());
        auto status = library_->DisableGPSTimestamping(vi);
        response->set_status(status);
        return ::grpc::Status::OK;
      }
      catch (nidevice_grpc::LibraryLoadException& ex) {
        return ::grpc::Status(::grpc::NOT_FOUND, ex.what());
      }
    }
  
    //---------------------------------------------------------------------
    //---------------------------------------------------------------------
    ::grpc::Status NiSyncService::DisableIRIGTimestamping(::grpc::ServerContext* context, const DisableIRIGTimestampingRequest* request, DisableIRIGTimestampingResponse* response)
    {
      if (context->IsCancelled()) {
        return ::grpc::Status::CANCELLED;
      }
      try {
        auto vi_grpc_session = request->vi();
        ViSession vi = session_repository_->access_session(vi_grpc_session.id(), vi_grpc_session.name());
        auto terminal_name = request->terminal_name().c_str();
        auto status = library_->DisableIRIGTimestamping(vi, terminal_name);
        response->set_status(status);
        return ::grpc::Status::OK;
      }
      catch (nidevice_grpc::LibraryLoadException& ex) {
        return ::grpc::Status(::grpc::NOT_FOUND, ex.what());
      }
    }
  
    //---------------------------------------------------------------------
    //---------------------------------------------------------------------
    ::grpc::Status NiSyncService::GetVelocity(::grpc::ServerContext* context, const GetVelocityRequest* request, GetVelocityResponse* response)
    {
      if (context->IsCancelled()) {
        return ::grpc::Status::CANCELLED;
      }
      try {
        auto vi_grpc_session = request->vi();
        ViSession vi = session_repository_->access_session(vi_grpc_session.id(), vi_grpc_session.name());
        ViReal64 east_velocity {};
        ViReal64 north_velocity {};
        ViReal64 up_velocity {};
        auto status = library_->GetVelocity(vi, &east_velocity, &north_velocity, &up_velocity);
        response->set_status(status);
        if (status_ok(status)) {
          response->set_east_velocity(east_velocity);
          response->set_north_velocity(north_velocity);
          response->set_up_velocity(up_velocity);
        }
        return ::grpc::Status::OK;
      }
      catch (nidevice_grpc::LibraryLoadException& ex) {
        return ::grpc::Status(::grpc::NOT_FOUND, ex.what());
      }
    }
  
    //---------------------------------------------------------------------
    //---------------------------------------------------------------------
    ::grpc::Status NiSyncService::GetLocation(::grpc::ServerContext* context, const GetLocationRequest* request, GetLocationResponse* response)
    {
      if (context->IsCancelled()) {
        return ::grpc::Status::CANCELLED;
      }
      try {
        auto vi_grpc_session = request->vi();
        ViSession vi = session_repository_->access_session(vi_grpc_session.id(), vi_grpc_session.name());
        ViReal64 latitude {};
        ViReal64 longitude {};
        ViReal64 altitude {};
        auto status = library_->GetLocation(vi, &latitude, &longitude, &altitude);
        response->set_status(status);
        if (status_ok(status)) {
          response->set_latitude(latitude);
          response->set_longitude(longitude);
          response->set_altitude(altitude);
        }
        return ::grpc::Status::OK;
      }
      catch (nidevice_grpc::LibraryLoadException& ex) {
        return ::grpc::Status(::grpc::NOT_FOUND, ex.what());
      }
    }
  
    //---------------------------------------------------------------------
    //---------------------------------------------------------------------
    ::grpc::Status NiSyncService::GetTimeReferenceNames(::grpc::ServerContext* context, const GetTimeReferenceNamesRequest* request, GetTimeReferenceNamesResponse* response)
    {
      if (context->IsCancelled()) {
        return ::grpc::Status::CANCELLED;
      }
      try {
        auto vi_grpc_session = request->vi();
        ViSession vi = session_repository_->access_session(vi_grpc_session.id(), vi_grpc_session.name());
  
        while (true) {
          auto status = library_->GetTimeReferenceNames(vi, 0, nullptr);
          if (status < 0) {
            response->set_status(status);
            return ::grpc::Status::OK;
          }
          ViUInt32 buffer_size = status;
        
          std::string time_reference_names;
          if (buffer_size > 0) {
              time_reference_names.resize(buffer_size - 1);
          }
          status = library_->GetTimeReferenceNames(vi, buffer_size, (ViChar*)time_reference_names.data());
          if (status == kErrorReadBufferTooSmall || status == kWarningCAPIStringTruncatedToFitBuffer || status > static_cast<decltype(status)>(buffer_size)) {
            // buffer is now too small, try again
            continue;
          }
          response->set_status(status);
          if (status_ok(status)) {
            response->set_time_reference_names(time_reference_names);
            nidevice_grpc::converters::trim_trailing_nulls(*(response->mutable_time_reference_names()));
          }
          return ::grpc::Status::OK;
        }
      }
      catch (nidevice_grpc::LibraryLoadException& ex) {
        return ::grpc::Status(::grpc::NOT_FOUND, ex.what());
      }
    }
  
    //---------------------------------------------------------------------
    //---------------------------------------------------------------------
    ::grpc::Status NiSyncService::GetAttributeViInt32(::grpc::ServerContext* context, const GetAttributeViInt32Request* request, GetAttributeViInt32Response* response)
    {
      if (context->IsCancelled()) {
        return ::grpc::Status::CANCELLED;
      }
      try {
        auto vi_grpc_session = request->vi();
        ViSession vi = session_repository_->access_session(vi_grpc_session.id(), vi_grpc_session.name());
        auto active_item = request->active_item().c_str();
        ViAttr attribute = request->attribute();
        ViInt32 value {};
        auto status = library_->GetAttributeViInt32(vi, active_item, attribute, &value);
        response->set_status(status);
        if (status_ok(status)) {
          response->set_value(value);
        }
        return ::grpc::Status::OK;
      }
      catch (nidevice_grpc::LibraryLoadException& ex) {
        return ::grpc::Status(::grpc::NOT_FOUND, ex.what());
      }
    }
  
    //---------------------------------------------------------------------
    //---------------------------------------------------------------------
    ::grpc::Status NiSyncService::GetAttributeViReal64(::grpc::ServerContext* context, const GetAttributeViReal64Request* request, GetAttributeViReal64Response* response)
    {
      if (context->IsCancelled()) {
        return ::grpc::Status::CANCELLED;
      }
      try {
        auto vi_grpc_session = request->vi();
        ViSession vi = session_repository_->access_session(vi_grpc_session.id(), vi_grpc_session.name());
        auto active_item = request->active_item().c_str();
        ViAttr attribute = request->attribute();
        ViReal64 value {};
        auto status = library_->GetAttributeViReal64(vi, active_item, attribute, &value);
        response->set_status(status);
        if (status_ok(status)) {
          response->set_value(value);
        }
        return ::grpc::Status::OK;
      }
      catch (nidevice_grpc::LibraryLoadException& ex) {
        return ::grpc::Status(::grpc::NOT_FOUND, ex.what());
      }
    }
  
    //---------------------------------------------------------------------
    //---------------------------------------------------------------------
    ::grpc::Status NiSyncService::GetAttributeViBoolean(::grpc::ServerContext* context, const GetAttributeViBooleanRequest* request, GetAttributeViBooleanResponse* response)
    {
      if (context->IsCancelled()) {
        return ::grpc::Status::CANCELLED;
      }
      try {
        auto vi_grpc_session = request->vi();
        ViSession vi = session_repository_->access_session(vi_grpc_session.id(), vi_grpc_session.name());
        auto active_item = request->active_item().c_str();
        ViAttr attribute = request->attribute();
        ViBoolean value {};
        auto status = library_->GetAttributeViBoolean(vi, active_item, attribute, &value);
        response->set_status(status);
        if (status_ok(status)) {
          response->set_value(value);
        }
        return ::grpc::Status::OK;
      }
      catch (nidevice_grpc::LibraryLoadException& ex) {
        return ::grpc::Status(::grpc::NOT_FOUND, ex.what());
      }
    }
  
    //---------------------------------------------------------------------
    //---------------------------------------------------------------------
    ::grpc::Status NiSyncService::GetAttributeViString(::grpc::ServerContext* context, const GetAttributeViStringRequest* request, GetAttributeViStringResponse* response)
    {
      if (context->IsCancelled()) {
        return ::grpc::Status::CANCELLED;
      }
      try {
        auto vi_grpc_session = request->vi();
        ViSession vi = session_repository_->access_session(vi_grpc_session.id(), vi_grpc_session.name());
        auto active_item = request->active_item().c_str();
        ViAttr attribute = request->attribute();
  
        while (true) {
          auto status = library_->GetAttributeViString(vi, active_item, attribute, 0, nullptr);
          if (status < 0) {
            response->set_status(status);
            return ::grpc::Status::OK;
          }
          ViInt32 buffer_size = status;
        
          std::string value;
          if (buffer_size > 0) {
              value.resize(buffer_size - 1);
          }
          status = library_->GetAttributeViString(vi, active_item, attribute, buffer_size, (ViChar*)value.data());
          if (status == kErrorReadBufferTooSmall || status == kWarningCAPIStringTruncatedToFitBuffer || status > static_cast<decltype(status)>(buffer_size)) {
            // buffer is now too small, try again
            continue;
          }
          response->set_status(status);
          if (status_ok(status)) {
            response->set_value(value);
            nidevice_grpc::converters::trim_trailing_nulls(*(response->mutable_value()));
          }
          return ::grpc::Status::OK;
        }
      }
      catch (nidevice_grpc::LibraryLoadException& ex) {
        return ::grpc::Status(::grpc::NOT_FOUND, ex.what());
      }
    }
  
    //---------------------------------------------------------------------
    //---------------------------------------------------------------------
    ::grpc::Status NiSyncService::SetAttributeViInt32(::grpc::ServerContext* context, const SetAttributeViInt32Request* request, SetAttributeViInt32Response* response)
    {
      if (context->IsCancelled()) {
        return ::grpc::Status::CANCELLED;
      }
      try {
        auto vi_grpc_session = request->vi();
        ViSession vi = session_repository_->access_session(vi_grpc_session.id(), vi_grpc_session.name());
        auto active_item = request->active_item().c_str();
        ViAttr attribute = request->attribute();
        ViInt32 value = request->value_raw();
        auto status = library_->SetAttributeViInt32(vi, active_item, attribute, value);
        response->set_status(status);
        return ::grpc::Status::OK;
      }
      catch (nidevice_grpc::LibraryLoadException& ex) {
        return ::grpc::Status(::grpc::NOT_FOUND, ex.what());
      }
    }
  
    //---------------------------------------------------------------------
    //---------------------------------------------------------------------
    ::grpc::Status NiSyncService::SetAttributeViReal64(::grpc::ServerContext* context, const SetAttributeViReal64Request* request, SetAttributeViReal64Response* response)
    {
      if (context->IsCancelled()) {
        return ::grpc::Status::CANCELLED;
      }
      try {
        auto vi_grpc_session = request->vi();
        ViSession vi = session_repository_->access_session(vi_grpc_session.id(), vi_grpc_session.name());
        auto active_item = request->active_item().c_str();
        ViAttr attribute = request->attribute();
        ViReal64 value = request->value_raw();
        auto status = library_->SetAttributeViReal64(vi, active_item, attribute, value);
        response->set_status(status);
        return ::grpc::Status::OK;
      }
      catch (nidevice_grpc::LibraryLoadException& ex) {
        return ::grpc::Status(::grpc::NOT_FOUND, ex.what());
      }
    }
  
    //---------------------------------------------------------------------
    //---------------------------------------------------------------------
    ::grpc::Status NiSyncService::SetAttributeViBoolean(::grpc::ServerContext* context, const SetAttributeViBooleanRequest* request, SetAttributeViBooleanResponse* response)
    {
      if (context->IsCancelled()) {
        return ::grpc::Status::CANCELLED;
      }
      try {
        auto vi_grpc_session = request->vi();
        ViSession vi = session_repository_->access_session(vi_grpc_session.id(), vi_grpc_session.name());
        auto active_item = request->active_item().c_str();
        ViAttr attribute = request->attribute();
        ViBoolean value = request->value();
        auto status = library_->SetAttributeViBoolean(vi, active_item, attribute, value);
        response->set_status(status);
        return ::grpc::Status::OK;
      }
      catch (nidevice_grpc::LibraryLoadException& ex) {
        return ::grpc::Status(::grpc::NOT_FOUND, ex.what());
      }
    }
  
    //---------------------------------------------------------------------
    //---------------------------------------------------------------------
    ::grpc::Status NiSyncService::SetAttributeViString(::grpc::ServerContext* context, const SetAttributeViStringRequest* request, SetAttributeViStringResponse* response)
    {
      if (context->IsCancelled()) {
        return ::grpc::Status::CANCELLED;
      }
      try {
        auto vi_grpc_session = request->vi();
        ViSession vi = session_repository_->access_session(vi_grpc_session.id(), vi_grpc_session.name());
        auto active_item = request->active_item().c_str();
        ViAttr attribute = request->attribute();
        auto value = request->value_raw().c_str();
        auto status = library_->SetAttributeViString(vi, active_item, attribute, value);
        response->set_status(status);
        return ::grpc::Status::OK;
      }
      catch (nidevice_grpc::LibraryLoadException& ex) {
        return ::grpc::Status(::grpc::NOT_FOUND, ex.what());
      }
    }
  
    //---------------------------------------------------------------------
    //---------------------------------------------------------------------
    ::grpc::Status NiSyncService::GetExtCalLastDateAndTime(::grpc::ServerContext* context, const GetExtCalLastDateAndTimeRequest* request, GetExtCalLastDateAndTimeResponse* response)
    {
      if (context->IsCancelled()) {
        return ::grpc::Status::CANCELLED;
      }
      try {
        auto vi_grpc_session = request->vi();
        ViSession vi = session_repository_->access_session(vi_grpc_session.id(), vi_grpc_session.name());
        ViInt32 year {};
        ViInt32 month {};
        ViInt32 day {};
        ViInt32 hour {};
        ViInt32 minute {};
        auto status = library_->GetExtCalLastDateAndTime(vi, &year, &month, &day, &hour, &minute);
        response->set_status(status);
        if (status_ok(status)) {
          response->set_year(year);
          response->set_month(month);
          response->set_day(day);
          response->set_hour(hour);
          response->set_minute(minute);
        }
        return ::grpc::Status::OK;
      }
      catch (nidevice_grpc::LibraryLoadException& ex) {
        return ::grpc::Status(::grpc::NOT_FOUND, ex.what());
      }
    }
  
    //---------------------------------------------------------------------
    //---------------------------------------------------------------------
    ::grpc::Status NiSyncService::GetExtCalLastTemp(::grpc::ServerContext* context, const GetExtCalLastTempRequest* request, GetExtCalLastTempResponse* response)
    {
      if (context->IsCancelled()) {
        return ::grpc::Status::CANCELLED;
      }
      try {
        auto vi_grpc_session = request->vi();
        ViSession vi = session_repository_->access_session(vi_grpc_session.id(), vi_grpc_session.name());
        ViReal64 temp {};
        auto status = library_->GetExtCalLastTemp(vi, &temp);
        response->set_status(status);
        if (status_ok(status)) {
          response->set_temp(temp);
        }
        return ::grpc::Status::OK;
      }
      catch (nidevice_grpc::LibraryLoadException& ex) {
        return ::grpc::Status(::grpc::NOT_FOUND, ex.what());
      }
    }
  
    //---------------------------------------------------------------------
    //---------------------------------------------------------------------
    ::grpc::Status NiSyncService::GetExtCalRecommendedInterval(::grpc::ServerContext* context, const GetExtCalRecommendedIntervalRequest* request, GetExtCalRecommendedIntervalResponse* response)
    {
      if (context->IsCancelled()) {
        return ::grpc::Status::CANCELLED;
      }
      try {
        auto vi_grpc_session = request->vi();
        ViSession vi = session_repository_->access_session(vi_grpc_session.id(), vi_grpc_session.name());
        ViInt32 months {};
        auto status = library_->GetExtCalRecommendedInterval(vi, &months);
        response->set_status(status);
        if (status_ok(status)) {
          response->set_months(months);
        }
        return ::grpc::Status::OK;
      }
      catch (nidevice_grpc::LibraryLoadException& ex) {
        return ::grpc::Status(::grpc::NOT_FOUND, ex.what());
      }
    }
  
    //---------------------------------------------------------------------
    //---------------------------------------------------------------------
    ::grpc::Status NiSyncService::ChangeExtCalPassword(::grpc::ServerContext* context, const ChangeExtCalPasswordRequest* request, ChangeExtCalPasswordResponse* response)
    {
      if (context->IsCancelled()) {
        return ::grpc::Status::CANCELLED;
      }
      try {
        auto vi_grpc_session = request->vi();
        ViSession vi = session_repository_->access_session(vi_grpc_session.id(), vi_grpc_session.name());
        auto old_password = request->old_password().c_str();
        auto new_password = request->new_password().c_str();
        auto status = library_->ChangeExtCalPassword(vi, old_password, new_password);
        response->set_status(status);
        return ::grpc::Status::OK;
      }
      catch (nidevice_grpc::LibraryLoadException& ex) {
        return ::grpc::Status(::grpc::NOT_FOUND, ex.what());
      }
    }
  
    //---------------------------------------------------------------------
    //---------------------------------------------------------------------
    ::grpc::Status NiSyncService::ReadCurrentTemperature(::grpc::ServerContext* context, const ReadCurrentTemperatureRequest* request, ReadCurrentTemperatureResponse* response)
    {
      if (context->IsCancelled()) {
        return ::grpc::Status::CANCELLED;
      }
      try {
        auto vi_grpc_session = request->vi();
        ViSession vi = session_repository_->access_session(vi_grpc_session.id(), vi_grpc_session.name());
        ViReal64 temperature {};
        auto status = library_->ReadCurrentTemperature(vi, &temperature);
        response->set_status(status);
        if (status_ok(status)) {
          response->set_temperature(temperature);
        }
        return ::grpc::Status::OK;
      }
      catch (nidevice_grpc::LibraryLoadException& ex) {
        return ::grpc::Status(::grpc::NOT_FOUND, ex.what());
      }
    }
  
    //---------------------------------------------------------------------
    //---------------------------------------------------------------------
    ::grpc::Status NiSyncService::CalGetOscillatorVoltage(::grpc::ServerContext* context, const CalGetOscillatorVoltageRequest* request, CalGetOscillatorVoltageResponse* response)
    {
      if (context->IsCancelled()) {
        return ::grpc::Status::CANCELLED;
      }
      try {
        auto vi_grpc_session = request->vi();
        ViSession vi = session_repository_->access_session(vi_grpc_session.id(), vi_grpc_session.name());
        ViReal64 voltage {};
        auto status = library_->CalGetOscillatorVoltage(vi, &voltage);
        response->set_status(status);
        if (status_ok(status)) {
          response->set_voltage(voltage);
        }
        return ::grpc::Status::OK;
      }
      catch (nidevice_grpc::LibraryLoadException& ex) {
        return ::grpc::Status(::grpc::NOT_FOUND, ex.what());
      }
    }
  
    //---------------------------------------------------------------------
    //---------------------------------------------------------------------
    ::grpc::Status NiSyncService::CalGetClk10PhaseVoltage(::grpc::ServerContext* context, const CalGetClk10PhaseVoltageRequest* request, CalGetClk10PhaseVoltageResponse* response)
    {
      if (context->IsCancelled()) {
        return ::grpc::Status::CANCELLED;
      }
      try {
        auto vi_grpc_session = request->vi();
        ViSession vi = session_repository_->access_session(vi_grpc_session.id(), vi_grpc_session.name());
        ViReal64 voltage {};
        auto status = library_->CalGetClk10PhaseVoltage(vi, &voltage);
        response->set_status(status);
        if (status_ok(status)) {
          response->set_voltage(voltage);
        }
        return ::grpc::Status::OK;
      }
      catch (nidevice_grpc::LibraryLoadException& ex) {
        return ::grpc::Status(::grpc::NOT_FOUND, ex.what());
      }
    }
  
    //---------------------------------------------------------------------
    //---------------------------------------------------------------------
    ::grpc::Status NiSyncService::CalGetDDSStartPulsePhaseVoltage(::grpc::ServerContext* context, const CalGetDDSStartPulsePhaseVoltageRequest* request, CalGetDDSStartPulsePhaseVoltageResponse* response)
    {
      if (context->IsCancelled()) {
        return ::grpc::Status::CANCELLED;
      }
      try {
        auto vi_grpc_session = request->vi();
        ViSession vi = session_repository_->access_session(vi_grpc_session.id(), vi_grpc_session.name());
        ViReal64 voltage {};
        auto status = library_->CalGetDDSStartPulsePhaseVoltage(vi, &voltage);
        response->set_status(status);
        if (status_ok(status)) {
          response->set_voltage(voltage);
        }
        return ::grpc::Status::OK;
      }
      catch (nidevice_grpc::LibraryLoadException& ex) {
        return ::grpc::Status(::grpc::NOT_FOUND, ex.what());
      }
    }
  
    //---------------------------------------------------------------------
    //---------------------------------------------------------------------
    ::grpc::Status NiSyncService::CalGetDDSInitialPhase(::grpc::ServerContext* context, const CalGetDDSInitialPhaseRequest* request, CalGetDDSInitialPhaseResponse* response)
    {
      if (context->IsCancelled()) {
        return ::grpc::Status::CANCELLED;
      }
      try {
        auto vi_grpc_session = request->vi();
        ViSession vi = session_repository_->access_session(vi_grpc_session.id(), vi_grpc_session.name());
        ViReal64 phase {};
        auto status = library_->CalGetDDSInitialPhase(vi, &phase);
        response->set_status(status);
        if (status_ok(status)) {
          response->set_phase(phase);
        }
        return ::grpc::Status::OK;
      }
      catch (nidevice_grpc::LibraryLoadException& ex) {
        return ::grpc::Status(::grpc::NOT_FOUND, ex.what());
      }
    }
  
    //---------------------------------------------------------------------
    //---------------------------------------------------------------------
    ::grpc::Status NiSyncService::InitExtCal(::grpc::ServerContext* context, const InitExtCalRequest* request, InitExtCalResponse* response)
    {
      if (context->IsCancelled()) {
        return ::grpc::Status::CANCELLED;
      }
      try {
        ViRsrc resource_name = (ViRsrc)request->resource_name().c_str();
        auto password = request->password().c_str();
  
        auto init_lambda = [&] () {
          ViSession vi;
          int status = library_->InitExtCal(resource_name, password, &vi);
          return std::make_tuple(status, vi);
        };
        uint32_t session_id = 0;
        const std::string& grpc_device_session_name = request->session_name();
        auto cleanup_lambda = [&] (ViSession id) { library_->Close(id); };
        int status = session_repository_->add_session(grpc_device_session_name, init_lambda, cleanup_lambda, session_id);
        response->set_status(status);
        if (status_ok(status)) {
          response->mutable_vi()->set_id(session_id);
        }
        return ::grpc::Status::OK;
      }
      catch (nidevice_grpc::LibraryLoadException& ex) {
        return ::grpc::Status(::grpc::NOT_FOUND, ex.what());
      }
    }
  
    //---------------------------------------------------------------------
    //---------------------------------------------------------------------
    ::grpc::Status NiSyncService::CloseExtCal(::grpc::ServerContext* context, const CloseExtCalRequest* request, CloseExtCalResponse* response)
    {
      if (context->IsCancelled()) {
        return ::grpc::Status::CANCELLED;
      }
      try {
        auto vi_grpc_session = request->vi();
        ViSession vi = session_repository_->access_session(vi_grpc_session.id(), vi_grpc_session.name());
        ViInt32 action = request->action();
        auto status = library_->CloseExtCal(vi, action);
        response->set_status(status);
        return ::grpc::Status::OK;
      }
      catch (nidevice_grpc::LibraryLoadException& ex) {
        return ::grpc::Status(::grpc::NOT_FOUND, ex.what());
      }
    }
  
    //---------------------------------------------------------------------
    //---------------------------------------------------------------------
    ::grpc::Status NiSyncService::CalAdjustOscillatorVoltage(::grpc::ServerContext* context, const CalAdjustOscillatorVoltageRequest* request, CalAdjustOscillatorVoltageResponse* response)
    {
      if (context->IsCancelled()) {
        return ::grpc::Status::CANCELLED;
      }
      try {
        auto vi_grpc_session = request->vi();
        ViSession vi = session_repository_->access_session(vi_grpc_session.id(), vi_grpc_session.name());
        ViReal64 measured_voltage = request->measured_voltage();
        ViReal64 old_voltage {};
        auto status = library_->CalAdjustOscillatorVoltage(vi, measured_voltage, &old_voltage);
        response->set_status(status);
        if (status_ok(status)) {
          response->set_old_voltage(old_voltage);
        }
        return ::grpc::Status::OK;
      }
      catch (nidevice_grpc::LibraryLoadException& ex) {
        return ::grpc::Status(::grpc::NOT_FOUND, ex.what());
      }
    }
  
    //---------------------------------------------------------------------
    //---------------------------------------------------------------------
    ::grpc::Status NiSyncService::CalAdjustClk10PhaseVoltage(::grpc::ServerContext* context, const CalAdjustClk10PhaseVoltageRequest* request, CalAdjustClk10PhaseVoltageResponse* response)
    {
      if (context->IsCancelled()) {
        return ::grpc::Status::CANCELLED;
      }
      try {
        auto vi_grpc_session = request->vi();
        ViSession vi = session_repository_->access_session(vi_grpc_session.id(), vi_grpc_session.name());
        ViReal64 measured_voltage = request->measured_voltage();
        ViReal64 old_voltage {};
        auto status = library_->CalAdjustClk10PhaseVoltage(vi, measured_voltage, &old_voltage);
        response->set_status(status);
        if (status_ok(status)) {
          response->set_old_voltage(old_voltage);
        }
        return ::grpc::Status::OK;
      }
      catch (nidevice_grpc::LibraryLoadException& ex) {
        return ::grpc::Status(::grpc::NOT_FOUND, ex.what());
      }
    }
  
    //---------------------------------------------------------------------
    //---------------------------------------------------------------------
    ::grpc::Status NiSyncService::CalAdjustDDSStartPulsePhaseVoltage(::grpc::ServerContext* context, const CalAdjustDDSStartPulsePhaseVoltageRequest* request, CalAdjustDDSStartPulsePhaseVoltageResponse* response)
    {
      if (context->IsCancelled()) {
        return ::grpc::Status::CANCELLED;
      }
      try {
        auto vi_grpc_session = request->vi();
        ViSession vi = session_repository_->access_session(vi_grpc_session.id(), vi_grpc_session.name());
        ViReal64 measured_voltage = request->measured_voltage();
        ViReal64 old_voltage {};
        auto status = library_->CalAdjustDDSStartPulsePhaseVoltage(vi, measured_voltage, &old_voltage);
        response->set_status(status);
        if (status_ok(status)) {
          response->set_old_voltage(old_voltage);
        }
        return ::grpc::Status::OK;
      }
      catch (nidevice_grpc::LibraryLoadException& ex) {
        return ::grpc::Status(::grpc::NOT_FOUND, ex.what());
      }
    }
  
    //---------------------------------------------------------------------
    //---------------------------------------------------------------------
    ::grpc::Status NiSyncService::CalAdjustDDSInitialPhase(::grpc::ServerContext* context, const CalAdjustDDSInitialPhaseRequest* request, CalAdjustDDSInitialPhaseResponse* response)
    {
      if (context->IsCancelled()) {
        return ::grpc::Status::CANCELLED;
      }
      try {
        auto vi_grpc_session = request->vi();
        ViSession vi = session_repository_->access_session(vi_grpc_session.id(), vi_grpc_session.name());
        ViReal64 measured_phase = request->measured_phase();
        ViReal64 old_phase {};
        auto status = library_->CalAdjustDDSInitialPhase(vi, measured_phase, &old_phase);
        response->set_status(status);
        if (status_ok(status)) {
          response->set_old_phase(old_phase);
        }
        return ::grpc::Status::OK;
      }
      catch (nidevice_grpc::LibraryLoadException& ex) {
        return ::grpc::Status(::grpc::NOT_FOUND, ex.what());
      }
    }
  
  
    NiSyncFeatureToggles::NiSyncFeatureToggles(
      const nidevice_grpc::FeatureToggles& feature_toggles)
      : is_enabled(
          feature_toggles.is_feature_enabled("nisync", CodeReadiness::kRelease))
    {
    }
  } // namespace nisync_grpc
  
  