// Generated by the protocol buffer compiler.  DO NOT EDIT!
// source: dmi/hw_management_service.proto

#include "dmi/hw_management_service.pb.h"

#include <algorithm>

#include <google/protobuf/stubs/common.h>
#include <google/protobuf/io/coded_stream.h>
#include <google/protobuf/extension_set.h>
#include <google/protobuf/wire_format_lite_inl.h>
#include <google/protobuf/descriptor.h>
#include <google/protobuf/generated_message_reflection.h>
#include <google/protobuf/reflection_ops.h>
#include <google/protobuf/wire_format.h>
// @@protoc_insertion_point(includes)
#include <google/protobuf/port_def.inc>

extern PROTOBUF_INTERNAL_EXPORT_dmi_2fhw_2eproto ::google::protobuf::internal::SCCInfo<0> scc_info_Uuid_dmi_2fhw_2eproto;
extern PROTOBUF_INTERNAL_EXPORT_dmi_2fhw_2eproto ::google::protobuf::internal::SCCInfo<2> scc_info_Hardware_dmi_2fhw_2eproto;
extern PROTOBUF_INTERNAL_EXPORT_dmi_2fhw_2eproto ::google::protobuf::internal::SCCInfo<3> scc_info_ModifiableComponent_dmi_2fhw_2eproto;
extern PROTOBUF_INTERNAL_EXPORT_dmi_2fhw_2eproto ::google::protobuf::internal::SCCInfo<9> scc_info_Component_dmi_2fhw_2eproto;
extern PROTOBUF_INTERNAL_EXPORT_dmi_2fhw_5fmanagement_5fservice_2eproto ::google::protobuf::internal::SCCInfo<0> scc_info_EntitiesLogLevel_dmi_2fhw_5fmanagement_5fservice_2eproto;
extern PROTOBUF_INTERNAL_EXPORT_dmi_2fhw_5fmanagement_5fservice_2eproto ::google::protobuf::internal::SCCInfo<2> scc_info_ManagedDeviceInfo_dmi_2fhw_5fmanagement_5fservice_2eproto;
namespace dmi {
class PhysicalInventoryRequestDefaultTypeInternal {
 public:
  ::google::protobuf::internal::ExplicitlyConstructed<PhysicalInventoryRequest> _instance;
} _PhysicalInventoryRequest_default_instance_;
class PhysicalInventoryResponseDefaultTypeInternal {
 public:
  ::google::protobuf::internal::ExplicitlyConstructed<PhysicalInventoryResponse> _instance;
} _PhysicalInventoryResponse_default_instance_;
class HWComponentInfoGetRequestDefaultTypeInternal {
 public:
  ::google::protobuf::internal::ExplicitlyConstructed<HWComponentInfoGetRequest> _instance;
} _HWComponentInfoGetRequest_default_instance_;
class HWComponentInfoGetResponseDefaultTypeInternal {
 public:
  ::google::protobuf::internal::ExplicitlyConstructed<HWComponentInfoGetResponse> _instance;
} _HWComponentInfoGetResponse_default_instance_;
class HWComponentInfoSetRequestDefaultTypeInternal {
 public:
  ::google::protobuf::internal::ExplicitlyConstructed<HWComponentInfoSetRequest> _instance;
} _HWComponentInfoSetRequest_default_instance_;
class HWComponentInfoSetResponseDefaultTypeInternal {
 public:
  ::google::protobuf::internal::ExplicitlyConstructed<HWComponentInfoSetResponse> _instance;
} _HWComponentInfoSetResponse_default_instance_;
class StartManagingDeviceResponseDefaultTypeInternal {
 public:
  ::google::protobuf::internal::ExplicitlyConstructed<StartManagingDeviceResponse> _instance;
} _StartManagingDeviceResponse_default_instance_;
class StopManagingDeviceRequestDefaultTypeInternal {
 public:
  ::google::protobuf::internal::ExplicitlyConstructed<StopManagingDeviceRequest> _instance;
} _StopManagingDeviceRequest_default_instance_;
class StopManagingDeviceResponseDefaultTypeInternal {
 public:
  ::google::protobuf::internal::ExplicitlyConstructed<StopManagingDeviceResponse> _instance;
} _StopManagingDeviceResponse_default_instance_;
class ManagedDeviceInfoDefaultTypeInternal {
 public:
  ::google::protobuf::internal::ExplicitlyConstructed<ManagedDeviceInfo> _instance;
} _ManagedDeviceInfo_default_instance_;
class ManagedDevicesResponseDefaultTypeInternal {
 public:
  ::google::protobuf::internal::ExplicitlyConstructed<ManagedDevicesResponse> _instance;
} _ManagedDevicesResponse_default_instance_;
class SetLoggingEndpointRequestDefaultTypeInternal {
 public:
  ::google::protobuf::internal::ExplicitlyConstructed<SetLoggingEndpointRequest> _instance;
} _SetLoggingEndpointRequest_default_instance_;
class SetRemoteEndpointResponseDefaultTypeInternal {
 public:
  ::google::protobuf::internal::ExplicitlyConstructed<SetRemoteEndpointResponse> _instance;
} _SetRemoteEndpointResponse_default_instance_;
class GetLoggingEndpointResponseDefaultTypeInternal {
 public:
  ::google::protobuf::internal::ExplicitlyConstructed<GetLoggingEndpointResponse> _instance;
} _GetLoggingEndpointResponse_default_instance_;
class SetMsgBusEndpointRequestDefaultTypeInternal {
 public:
  ::google::protobuf::internal::ExplicitlyConstructed<SetMsgBusEndpointRequest> _instance;
} _SetMsgBusEndpointRequest_default_instance_;
class GetMsgBusEndpointResponseDefaultTypeInternal {
 public:
  ::google::protobuf::internal::ExplicitlyConstructed<GetMsgBusEndpointResponse> _instance;
} _GetMsgBusEndpointResponse_default_instance_;
class EntitiesLogLevelDefaultTypeInternal {
 public:
  ::google::protobuf::internal::ExplicitlyConstructed<EntitiesLogLevel> _instance;
} _EntitiesLogLevel_default_instance_;
class SetLogLevelRequestDefaultTypeInternal {
 public:
  ::google::protobuf::internal::ExplicitlyConstructed<SetLogLevelRequest> _instance;
} _SetLogLevelRequest_default_instance_;
class SetLogLevelResponseDefaultTypeInternal {
 public:
  ::google::protobuf::internal::ExplicitlyConstructed<SetLogLevelResponse> _instance;
} _SetLogLevelResponse_default_instance_;
class GetLogLevelRequestDefaultTypeInternal {
 public:
  ::google::protobuf::internal::ExplicitlyConstructed<GetLogLevelRequest> _instance;
} _GetLogLevelRequest_default_instance_;
class GetLogLevelResponseDefaultTypeInternal {
 public:
  ::google::protobuf::internal::ExplicitlyConstructed<GetLogLevelResponse> _instance;
} _GetLogLevelResponse_default_instance_;
class GetLoggableEntitiesRequestDefaultTypeInternal {
 public:
  ::google::protobuf::internal::ExplicitlyConstructed<GetLoggableEntitiesRequest> _instance;
} _GetLoggableEntitiesRequest_default_instance_;
class HeartbeatDefaultTypeInternal {
 public:
  ::google::protobuf::internal::ExplicitlyConstructed<Heartbeat> _instance;
} _Heartbeat_default_instance_;
class RebootDeviceRequestDefaultTypeInternal {
 public:
  ::google::protobuf::internal::ExplicitlyConstructed<RebootDeviceRequest> _instance;
} _RebootDeviceRequest_default_instance_;
class RebootDeviceResponseDefaultTypeInternal {
 public:
  ::google::protobuf::internal::ExplicitlyConstructed<RebootDeviceResponse> _instance;
} _RebootDeviceResponse_default_instance_;
}  // namespace dmi
static void InitDefaultsPhysicalInventoryRequest_dmi_2fhw_5fmanagement_5fservice_2eproto() {
  GOOGLE_PROTOBUF_VERIFY_VERSION;

  {
    void* ptr = &::dmi::_PhysicalInventoryRequest_default_instance_;
    new (ptr) ::dmi::PhysicalInventoryRequest();
    ::google::protobuf::internal::OnShutdownDestroyMessage(ptr);
  }
  ::dmi::PhysicalInventoryRequest::InitAsDefaultInstance();
}

::google::protobuf::internal::SCCInfo<1> scc_info_PhysicalInventoryRequest_dmi_2fhw_5fmanagement_5fservice_2eproto =
    {{ATOMIC_VAR_INIT(::google::protobuf::internal::SCCInfoBase::kUninitialized), 1, InitDefaultsPhysicalInventoryRequest_dmi_2fhw_5fmanagement_5fservice_2eproto}, {
      &scc_info_Uuid_dmi_2fhw_2eproto.base,}};

static void InitDefaultsPhysicalInventoryResponse_dmi_2fhw_5fmanagement_5fservice_2eproto() {
  GOOGLE_PROTOBUF_VERIFY_VERSION;

  {
    void* ptr = &::dmi::_PhysicalInventoryResponse_default_instance_;
    new (ptr) ::dmi::PhysicalInventoryResponse();
    ::google::protobuf::internal::OnShutdownDestroyMessage(ptr);
  }
  ::dmi::PhysicalInventoryResponse::InitAsDefaultInstance();
}

::google::protobuf::internal::SCCInfo<1> scc_info_PhysicalInventoryResponse_dmi_2fhw_5fmanagement_5fservice_2eproto =
    {{ATOMIC_VAR_INIT(::google::protobuf::internal::SCCInfoBase::kUninitialized), 1, InitDefaultsPhysicalInventoryResponse_dmi_2fhw_5fmanagement_5fservice_2eproto}, {
      &scc_info_Hardware_dmi_2fhw_2eproto.base,}};

static void InitDefaultsHWComponentInfoGetRequest_dmi_2fhw_5fmanagement_5fservice_2eproto() {
  GOOGLE_PROTOBUF_VERIFY_VERSION;

  {
    void* ptr = &::dmi::_HWComponentInfoGetRequest_default_instance_;
    new (ptr) ::dmi::HWComponentInfoGetRequest();
    ::google::protobuf::internal::OnShutdownDestroyMessage(ptr);
  }
  ::dmi::HWComponentInfoGetRequest::InitAsDefaultInstance();
}

::google::protobuf::internal::SCCInfo<1> scc_info_HWComponentInfoGetRequest_dmi_2fhw_5fmanagement_5fservice_2eproto =
    {{ATOMIC_VAR_INIT(::google::protobuf::internal::SCCInfoBase::kUninitialized), 1, InitDefaultsHWComponentInfoGetRequest_dmi_2fhw_5fmanagement_5fservice_2eproto}, {
      &scc_info_Uuid_dmi_2fhw_2eproto.base,}};

static void InitDefaultsHWComponentInfoGetResponse_dmi_2fhw_5fmanagement_5fservice_2eproto() {
  GOOGLE_PROTOBUF_VERIFY_VERSION;

  {
    void* ptr = &::dmi::_HWComponentInfoGetResponse_default_instance_;
    new (ptr) ::dmi::HWComponentInfoGetResponse();
    ::google::protobuf::internal::OnShutdownDestroyMessage(ptr);
  }
  ::dmi::HWComponentInfoGetResponse::InitAsDefaultInstance();
}

::google::protobuf::internal::SCCInfo<1> scc_info_HWComponentInfoGetResponse_dmi_2fhw_5fmanagement_5fservice_2eproto =
    {{ATOMIC_VAR_INIT(::google::protobuf::internal::SCCInfoBase::kUninitialized), 1, InitDefaultsHWComponentInfoGetResponse_dmi_2fhw_5fmanagement_5fservice_2eproto}, {
      &scc_info_Component_dmi_2fhw_2eproto.base,}};

static void InitDefaultsHWComponentInfoSetRequest_dmi_2fhw_5fmanagement_5fservice_2eproto() {
  GOOGLE_PROTOBUF_VERIFY_VERSION;

  {
    void* ptr = &::dmi::_HWComponentInfoSetRequest_default_instance_;
    new (ptr) ::dmi::HWComponentInfoSetRequest();
    ::google::protobuf::internal::OnShutdownDestroyMessage(ptr);
  }
  ::dmi::HWComponentInfoSetRequest::InitAsDefaultInstance();
}

::google::protobuf::internal::SCCInfo<2> scc_info_HWComponentInfoSetRequest_dmi_2fhw_5fmanagement_5fservice_2eproto =
    {{ATOMIC_VAR_INIT(::google::protobuf::internal::SCCInfoBase::kUninitialized), 2, InitDefaultsHWComponentInfoSetRequest_dmi_2fhw_5fmanagement_5fservice_2eproto}, {
      &scc_info_Uuid_dmi_2fhw_2eproto.base,
      &scc_info_ModifiableComponent_dmi_2fhw_2eproto.base,}};

static void InitDefaultsHWComponentInfoSetResponse_dmi_2fhw_5fmanagement_5fservice_2eproto() {
  GOOGLE_PROTOBUF_VERIFY_VERSION;

  {
    void* ptr = &::dmi::_HWComponentInfoSetResponse_default_instance_;
    new (ptr) ::dmi::HWComponentInfoSetResponse();
    ::google::protobuf::internal::OnShutdownDestroyMessage(ptr);
  }
  ::dmi::HWComponentInfoSetResponse::InitAsDefaultInstance();
}

::google::protobuf::internal::SCCInfo<0> scc_info_HWComponentInfoSetResponse_dmi_2fhw_5fmanagement_5fservice_2eproto =
    {{ATOMIC_VAR_INIT(::google::protobuf::internal::SCCInfoBase::kUninitialized), 0, InitDefaultsHWComponentInfoSetResponse_dmi_2fhw_5fmanagement_5fservice_2eproto}, {}};

static void InitDefaultsStartManagingDeviceResponse_dmi_2fhw_5fmanagement_5fservice_2eproto() {
  GOOGLE_PROTOBUF_VERIFY_VERSION;

  {
    void* ptr = &::dmi::_StartManagingDeviceResponse_default_instance_;
    new (ptr) ::dmi::StartManagingDeviceResponse();
    ::google::protobuf::internal::OnShutdownDestroyMessage(ptr);
  }
  ::dmi::StartManagingDeviceResponse::InitAsDefaultInstance();
}

::google::protobuf::internal::SCCInfo<1> scc_info_StartManagingDeviceResponse_dmi_2fhw_5fmanagement_5fservice_2eproto =
    {{ATOMIC_VAR_INIT(::google::protobuf::internal::SCCInfoBase::kUninitialized), 1, InitDefaultsStartManagingDeviceResponse_dmi_2fhw_5fmanagement_5fservice_2eproto}, {
      &scc_info_Uuid_dmi_2fhw_2eproto.base,}};

static void InitDefaultsStopManagingDeviceRequest_dmi_2fhw_5fmanagement_5fservice_2eproto() {
  GOOGLE_PROTOBUF_VERIFY_VERSION;

  {
    void* ptr = &::dmi::_StopManagingDeviceRequest_default_instance_;
    new (ptr) ::dmi::StopManagingDeviceRequest();
    ::google::protobuf::internal::OnShutdownDestroyMessage(ptr);
  }
  ::dmi::StopManagingDeviceRequest::InitAsDefaultInstance();
}

::google::protobuf::internal::SCCInfo<0> scc_info_StopManagingDeviceRequest_dmi_2fhw_5fmanagement_5fservice_2eproto =
    {{ATOMIC_VAR_INIT(::google::protobuf::internal::SCCInfoBase::kUninitialized), 0, InitDefaultsStopManagingDeviceRequest_dmi_2fhw_5fmanagement_5fservice_2eproto}, {}};

static void InitDefaultsStopManagingDeviceResponse_dmi_2fhw_5fmanagement_5fservice_2eproto() {
  GOOGLE_PROTOBUF_VERIFY_VERSION;

  {
    void* ptr = &::dmi::_StopManagingDeviceResponse_default_instance_;
    new (ptr) ::dmi::StopManagingDeviceResponse();
    ::google::protobuf::internal::OnShutdownDestroyMessage(ptr);
  }
  ::dmi::StopManagingDeviceResponse::InitAsDefaultInstance();
}

::google::protobuf::internal::SCCInfo<0> scc_info_StopManagingDeviceResponse_dmi_2fhw_5fmanagement_5fservice_2eproto =
    {{ATOMIC_VAR_INIT(::google::protobuf::internal::SCCInfoBase::kUninitialized), 0, InitDefaultsStopManagingDeviceResponse_dmi_2fhw_5fmanagement_5fservice_2eproto}, {}};

static void InitDefaultsManagedDeviceInfo_dmi_2fhw_5fmanagement_5fservice_2eproto() {
  GOOGLE_PROTOBUF_VERIFY_VERSION;

  {
    void* ptr = &::dmi::_ManagedDeviceInfo_default_instance_;
    new (ptr) ::dmi::ManagedDeviceInfo();
    ::google::protobuf::internal::OnShutdownDestroyMessage(ptr);
  }
  ::dmi::ManagedDeviceInfo::InitAsDefaultInstance();
}

::google::protobuf::internal::SCCInfo<2> scc_info_ManagedDeviceInfo_dmi_2fhw_5fmanagement_5fservice_2eproto =
    {{ATOMIC_VAR_INIT(::google::protobuf::internal::SCCInfoBase::kUninitialized), 2, InitDefaultsManagedDeviceInfo_dmi_2fhw_5fmanagement_5fservice_2eproto}, {
      &scc_info_ModifiableComponent_dmi_2fhw_2eproto.base,
      &scc_info_Uuid_dmi_2fhw_2eproto.base,}};

static void InitDefaultsManagedDevicesResponse_dmi_2fhw_5fmanagement_5fservice_2eproto() {
  GOOGLE_PROTOBUF_VERIFY_VERSION;

  {
    void* ptr = &::dmi::_ManagedDevicesResponse_default_instance_;
    new (ptr) ::dmi::ManagedDevicesResponse();
    ::google::protobuf::internal::OnShutdownDestroyMessage(ptr);
  }
  ::dmi::ManagedDevicesResponse::InitAsDefaultInstance();
}

::google::protobuf::internal::SCCInfo<1> scc_info_ManagedDevicesResponse_dmi_2fhw_5fmanagement_5fservice_2eproto =
    {{ATOMIC_VAR_INIT(::google::protobuf::internal::SCCInfoBase::kUninitialized), 1, InitDefaultsManagedDevicesResponse_dmi_2fhw_5fmanagement_5fservice_2eproto}, {
      &scc_info_ManagedDeviceInfo_dmi_2fhw_5fmanagement_5fservice_2eproto.base,}};

static void InitDefaultsSetLoggingEndpointRequest_dmi_2fhw_5fmanagement_5fservice_2eproto() {
  GOOGLE_PROTOBUF_VERIFY_VERSION;

  {
    void* ptr = &::dmi::_SetLoggingEndpointRequest_default_instance_;
    new (ptr) ::dmi::SetLoggingEndpointRequest();
    ::google::protobuf::internal::OnShutdownDestroyMessage(ptr);
  }
  ::dmi::SetLoggingEndpointRequest::InitAsDefaultInstance();
}

::google::protobuf::internal::SCCInfo<1> scc_info_SetLoggingEndpointRequest_dmi_2fhw_5fmanagement_5fservice_2eproto =
    {{ATOMIC_VAR_INIT(::google::protobuf::internal::SCCInfoBase::kUninitialized), 1, InitDefaultsSetLoggingEndpointRequest_dmi_2fhw_5fmanagement_5fservice_2eproto}, {
      &scc_info_Uuid_dmi_2fhw_2eproto.base,}};

static void InitDefaultsSetRemoteEndpointResponse_dmi_2fhw_5fmanagement_5fservice_2eproto() {
  GOOGLE_PROTOBUF_VERIFY_VERSION;

  {
    void* ptr = &::dmi::_SetRemoteEndpointResponse_default_instance_;
    new (ptr) ::dmi::SetRemoteEndpointResponse();
    ::google::protobuf::internal::OnShutdownDestroyMessage(ptr);
  }
  ::dmi::SetRemoteEndpointResponse::InitAsDefaultInstance();
}

::google::protobuf::internal::SCCInfo<0> scc_info_SetRemoteEndpointResponse_dmi_2fhw_5fmanagement_5fservice_2eproto =
    {{ATOMIC_VAR_INIT(::google::protobuf::internal::SCCInfoBase::kUninitialized), 0, InitDefaultsSetRemoteEndpointResponse_dmi_2fhw_5fmanagement_5fservice_2eproto}, {}};

static void InitDefaultsGetLoggingEndpointResponse_dmi_2fhw_5fmanagement_5fservice_2eproto() {
  GOOGLE_PROTOBUF_VERIFY_VERSION;

  {
    void* ptr = &::dmi::_GetLoggingEndpointResponse_default_instance_;
    new (ptr) ::dmi::GetLoggingEndpointResponse();
    ::google::protobuf::internal::OnShutdownDestroyMessage(ptr);
  }
  ::dmi::GetLoggingEndpointResponse::InitAsDefaultInstance();
}

::google::protobuf::internal::SCCInfo<0> scc_info_GetLoggingEndpointResponse_dmi_2fhw_5fmanagement_5fservice_2eproto =
    {{ATOMIC_VAR_INIT(::google::protobuf::internal::SCCInfoBase::kUninitialized), 0, InitDefaultsGetLoggingEndpointResponse_dmi_2fhw_5fmanagement_5fservice_2eproto}, {}};

static void InitDefaultsSetMsgBusEndpointRequest_dmi_2fhw_5fmanagement_5fservice_2eproto() {
  GOOGLE_PROTOBUF_VERIFY_VERSION;

  {
    void* ptr = &::dmi::_SetMsgBusEndpointRequest_default_instance_;
    new (ptr) ::dmi::SetMsgBusEndpointRequest();
    ::google::protobuf::internal::OnShutdownDestroyMessage(ptr);
  }
  ::dmi::SetMsgBusEndpointRequest::InitAsDefaultInstance();
}

::google::protobuf::internal::SCCInfo<0> scc_info_SetMsgBusEndpointRequest_dmi_2fhw_5fmanagement_5fservice_2eproto =
    {{ATOMIC_VAR_INIT(::google::protobuf::internal::SCCInfoBase::kUninitialized), 0, InitDefaultsSetMsgBusEndpointRequest_dmi_2fhw_5fmanagement_5fservice_2eproto}, {}};

static void InitDefaultsGetMsgBusEndpointResponse_dmi_2fhw_5fmanagement_5fservice_2eproto() {
  GOOGLE_PROTOBUF_VERIFY_VERSION;

  {
    void* ptr = &::dmi::_GetMsgBusEndpointResponse_default_instance_;
    new (ptr) ::dmi::GetMsgBusEndpointResponse();
    ::google::protobuf::internal::OnShutdownDestroyMessage(ptr);
  }
  ::dmi::GetMsgBusEndpointResponse::InitAsDefaultInstance();
}

::google::protobuf::internal::SCCInfo<0> scc_info_GetMsgBusEndpointResponse_dmi_2fhw_5fmanagement_5fservice_2eproto =
    {{ATOMIC_VAR_INIT(::google::protobuf::internal::SCCInfoBase::kUninitialized), 0, InitDefaultsGetMsgBusEndpointResponse_dmi_2fhw_5fmanagement_5fservice_2eproto}, {}};

static void InitDefaultsEntitiesLogLevel_dmi_2fhw_5fmanagement_5fservice_2eproto() {
  GOOGLE_PROTOBUF_VERIFY_VERSION;

  {
    void* ptr = &::dmi::_EntitiesLogLevel_default_instance_;
    new (ptr) ::dmi::EntitiesLogLevel();
    ::google::protobuf::internal::OnShutdownDestroyMessage(ptr);
  }
  ::dmi::EntitiesLogLevel::InitAsDefaultInstance();
}

::google::protobuf::internal::SCCInfo<0> scc_info_EntitiesLogLevel_dmi_2fhw_5fmanagement_5fservice_2eproto =
    {{ATOMIC_VAR_INIT(::google::protobuf::internal::SCCInfoBase::kUninitialized), 0, InitDefaultsEntitiesLogLevel_dmi_2fhw_5fmanagement_5fservice_2eproto}, {}};

static void InitDefaultsSetLogLevelRequest_dmi_2fhw_5fmanagement_5fservice_2eproto() {
  GOOGLE_PROTOBUF_VERIFY_VERSION;

  {
    void* ptr = &::dmi::_SetLogLevelRequest_default_instance_;
    new (ptr) ::dmi::SetLogLevelRequest();
    ::google::protobuf::internal::OnShutdownDestroyMessage(ptr);
  }
  ::dmi::SetLogLevelRequest::InitAsDefaultInstance();
}

::google::protobuf::internal::SCCInfo<2> scc_info_SetLogLevelRequest_dmi_2fhw_5fmanagement_5fservice_2eproto =
    {{ATOMIC_VAR_INIT(::google::protobuf::internal::SCCInfoBase::kUninitialized), 2, InitDefaultsSetLogLevelRequest_dmi_2fhw_5fmanagement_5fservice_2eproto}, {
      &scc_info_Uuid_dmi_2fhw_2eproto.base,
      &scc_info_EntitiesLogLevel_dmi_2fhw_5fmanagement_5fservice_2eproto.base,}};

static void InitDefaultsSetLogLevelResponse_dmi_2fhw_5fmanagement_5fservice_2eproto() {
  GOOGLE_PROTOBUF_VERIFY_VERSION;

  {
    void* ptr = &::dmi::_SetLogLevelResponse_default_instance_;
    new (ptr) ::dmi::SetLogLevelResponse();
    ::google::protobuf::internal::OnShutdownDestroyMessage(ptr);
  }
  ::dmi::SetLogLevelResponse::InitAsDefaultInstance();
}

::google::protobuf::internal::SCCInfo<1> scc_info_SetLogLevelResponse_dmi_2fhw_5fmanagement_5fservice_2eproto =
    {{ATOMIC_VAR_INIT(::google::protobuf::internal::SCCInfoBase::kUninitialized), 1, InitDefaultsSetLogLevelResponse_dmi_2fhw_5fmanagement_5fservice_2eproto}, {
      &scc_info_Uuid_dmi_2fhw_2eproto.base,}};

static void InitDefaultsGetLogLevelRequest_dmi_2fhw_5fmanagement_5fservice_2eproto() {
  GOOGLE_PROTOBUF_VERIFY_VERSION;

  {
    void* ptr = &::dmi::_GetLogLevelRequest_default_instance_;
    new (ptr) ::dmi::GetLogLevelRequest();
    ::google::protobuf::internal::OnShutdownDestroyMessage(ptr);
  }
  ::dmi::GetLogLevelRequest::InitAsDefaultInstance();
}

::google::protobuf::internal::SCCInfo<1> scc_info_GetLogLevelRequest_dmi_2fhw_5fmanagement_5fservice_2eproto =
    {{ATOMIC_VAR_INIT(::google::protobuf::internal::SCCInfoBase::kUninitialized), 1, InitDefaultsGetLogLevelRequest_dmi_2fhw_5fmanagement_5fservice_2eproto}, {
      &scc_info_Uuid_dmi_2fhw_2eproto.base,}};

static void InitDefaultsGetLogLevelResponse_dmi_2fhw_5fmanagement_5fservice_2eproto() {
  GOOGLE_PROTOBUF_VERIFY_VERSION;

  {
    void* ptr = &::dmi::_GetLogLevelResponse_default_instance_;
    new (ptr) ::dmi::GetLogLevelResponse();
    ::google::protobuf::internal::OnShutdownDestroyMessage(ptr);
  }
  ::dmi::GetLogLevelResponse::InitAsDefaultInstance();
}

::google::protobuf::internal::SCCInfo<2> scc_info_GetLogLevelResponse_dmi_2fhw_5fmanagement_5fservice_2eproto =
    {{ATOMIC_VAR_INIT(::google::protobuf::internal::SCCInfoBase::kUninitialized), 2, InitDefaultsGetLogLevelResponse_dmi_2fhw_5fmanagement_5fservice_2eproto}, {
      &scc_info_Uuid_dmi_2fhw_2eproto.base,
      &scc_info_EntitiesLogLevel_dmi_2fhw_5fmanagement_5fservice_2eproto.base,}};

static void InitDefaultsGetLoggableEntitiesRequest_dmi_2fhw_5fmanagement_5fservice_2eproto() {
  GOOGLE_PROTOBUF_VERIFY_VERSION;

  {
    void* ptr = &::dmi::_GetLoggableEntitiesRequest_default_instance_;
    new (ptr) ::dmi::GetLoggableEntitiesRequest();
    ::google::protobuf::internal::OnShutdownDestroyMessage(ptr);
  }
  ::dmi::GetLoggableEntitiesRequest::InitAsDefaultInstance();
}

::google::protobuf::internal::SCCInfo<1> scc_info_GetLoggableEntitiesRequest_dmi_2fhw_5fmanagement_5fservice_2eproto =
    {{ATOMIC_VAR_INIT(::google::protobuf::internal::SCCInfoBase::kUninitialized), 1, InitDefaultsGetLoggableEntitiesRequest_dmi_2fhw_5fmanagement_5fservice_2eproto}, {
      &scc_info_Uuid_dmi_2fhw_2eproto.base,}};

static void InitDefaultsHeartbeat_dmi_2fhw_5fmanagement_5fservice_2eproto() {
  GOOGLE_PROTOBUF_VERIFY_VERSION;

  {
    void* ptr = &::dmi::_Heartbeat_default_instance_;
    new (ptr) ::dmi::Heartbeat();
    ::google::protobuf::internal::OnShutdownDestroyMessage(ptr);
  }
  ::dmi::Heartbeat::InitAsDefaultInstance();
}

::google::protobuf::internal::SCCInfo<0> scc_info_Heartbeat_dmi_2fhw_5fmanagement_5fservice_2eproto =
    {{ATOMIC_VAR_INIT(::google::protobuf::internal::SCCInfoBase::kUninitialized), 0, InitDefaultsHeartbeat_dmi_2fhw_5fmanagement_5fservice_2eproto}, {}};

static void InitDefaultsRebootDeviceRequest_dmi_2fhw_5fmanagement_5fservice_2eproto() {
  GOOGLE_PROTOBUF_VERIFY_VERSION;

  {
    void* ptr = &::dmi::_RebootDeviceRequest_default_instance_;
    new (ptr) ::dmi::RebootDeviceRequest();
    ::google::protobuf::internal::OnShutdownDestroyMessage(ptr);
  }
  ::dmi::RebootDeviceRequest::InitAsDefaultInstance();
}

::google::protobuf::internal::SCCInfo<1> scc_info_RebootDeviceRequest_dmi_2fhw_5fmanagement_5fservice_2eproto =
    {{ATOMIC_VAR_INIT(::google::protobuf::internal::SCCInfoBase::kUninitialized), 1, InitDefaultsRebootDeviceRequest_dmi_2fhw_5fmanagement_5fservice_2eproto}, {
      &scc_info_Uuid_dmi_2fhw_2eproto.base,}};

static void InitDefaultsRebootDeviceResponse_dmi_2fhw_5fmanagement_5fservice_2eproto() {
  GOOGLE_PROTOBUF_VERIFY_VERSION;

  {
    void* ptr = &::dmi::_RebootDeviceResponse_default_instance_;
    new (ptr) ::dmi::RebootDeviceResponse();
    ::google::protobuf::internal::OnShutdownDestroyMessage(ptr);
  }
  ::dmi::RebootDeviceResponse::InitAsDefaultInstance();
}

::google::protobuf::internal::SCCInfo<0> scc_info_RebootDeviceResponse_dmi_2fhw_5fmanagement_5fservice_2eproto =
    {{ATOMIC_VAR_INIT(::google::protobuf::internal::SCCInfoBase::kUninitialized), 0, InitDefaultsRebootDeviceResponse_dmi_2fhw_5fmanagement_5fservice_2eproto}, {}};

void InitDefaults_dmi_2fhw_5fmanagement_5fservice_2eproto() {
  ::google::protobuf::internal::InitSCC(&scc_info_PhysicalInventoryRequest_dmi_2fhw_5fmanagement_5fservice_2eproto.base);
  ::google::protobuf::internal::InitSCC(&scc_info_PhysicalInventoryResponse_dmi_2fhw_5fmanagement_5fservice_2eproto.base);
  ::google::protobuf::internal::InitSCC(&scc_info_HWComponentInfoGetRequest_dmi_2fhw_5fmanagement_5fservice_2eproto.base);
  ::google::protobuf::internal::InitSCC(&scc_info_HWComponentInfoGetResponse_dmi_2fhw_5fmanagement_5fservice_2eproto.base);
  ::google::protobuf::internal::InitSCC(&scc_info_HWComponentInfoSetRequest_dmi_2fhw_5fmanagement_5fservice_2eproto.base);
  ::google::protobuf::internal::InitSCC(&scc_info_HWComponentInfoSetResponse_dmi_2fhw_5fmanagement_5fservice_2eproto.base);
  ::google::protobuf::internal::InitSCC(&scc_info_StartManagingDeviceResponse_dmi_2fhw_5fmanagement_5fservice_2eproto.base);
  ::google::protobuf::internal::InitSCC(&scc_info_StopManagingDeviceRequest_dmi_2fhw_5fmanagement_5fservice_2eproto.base);
  ::google::protobuf::internal::InitSCC(&scc_info_StopManagingDeviceResponse_dmi_2fhw_5fmanagement_5fservice_2eproto.base);
  ::google::protobuf::internal::InitSCC(&scc_info_ManagedDeviceInfo_dmi_2fhw_5fmanagement_5fservice_2eproto.base);
  ::google::protobuf::internal::InitSCC(&scc_info_ManagedDevicesResponse_dmi_2fhw_5fmanagement_5fservice_2eproto.base);
  ::google::protobuf::internal::InitSCC(&scc_info_SetLoggingEndpointRequest_dmi_2fhw_5fmanagement_5fservice_2eproto.base);
  ::google::protobuf::internal::InitSCC(&scc_info_SetRemoteEndpointResponse_dmi_2fhw_5fmanagement_5fservice_2eproto.base);
  ::google::protobuf::internal::InitSCC(&scc_info_GetLoggingEndpointResponse_dmi_2fhw_5fmanagement_5fservice_2eproto.base);
  ::google::protobuf::internal::InitSCC(&scc_info_SetMsgBusEndpointRequest_dmi_2fhw_5fmanagement_5fservice_2eproto.base);
  ::google::protobuf::internal::InitSCC(&scc_info_GetMsgBusEndpointResponse_dmi_2fhw_5fmanagement_5fservice_2eproto.base);
  ::google::protobuf::internal::InitSCC(&scc_info_EntitiesLogLevel_dmi_2fhw_5fmanagement_5fservice_2eproto.base);
  ::google::protobuf::internal::InitSCC(&scc_info_SetLogLevelRequest_dmi_2fhw_5fmanagement_5fservice_2eproto.base);
  ::google::protobuf::internal::InitSCC(&scc_info_SetLogLevelResponse_dmi_2fhw_5fmanagement_5fservice_2eproto.base);
  ::google::protobuf::internal::InitSCC(&scc_info_GetLogLevelRequest_dmi_2fhw_5fmanagement_5fservice_2eproto.base);
  ::google::protobuf::internal::InitSCC(&scc_info_GetLogLevelResponse_dmi_2fhw_5fmanagement_5fservice_2eproto.base);
  ::google::protobuf::internal::InitSCC(&scc_info_GetLoggableEntitiesRequest_dmi_2fhw_5fmanagement_5fservice_2eproto.base);
  ::google::protobuf::internal::InitSCC(&scc_info_Heartbeat_dmi_2fhw_5fmanagement_5fservice_2eproto.base);
  ::google::protobuf::internal::InitSCC(&scc_info_RebootDeviceRequest_dmi_2fhw_5fmanagement_5fservice_2eproto.base);
  ::google::protobuf::internal::InitSCC(&scc_info_RebootDeviceResponse_dmi_2fhw_5fmanagement_5fservice_2eproto.base);
}

::google::protobuf::Metadata file_level_metadata_dmi_2fhw_5fmanagement_5fservice_2eproto[25];
const ::google::protobuf::EnumDescriptor* file_level_enum_descriptors_dmi_2fhw_5fmanagement_5fservice_2eproto[12];
constexpr ::google::protobuf::ServiceDescriptor const** file_level_service_descriptors_dmi_2fhw_5fmanagement_5fservice_2eproto = nullptr;

const ::google::protobuf::uint32 TableStruct_dmi_2fhw_5fmanagement_5fservice_2eproto::offsets[] PROTOBUF_SECTION_VARIABLE(protodesc_cold) = {
  ~0u,  // no _has_bits_
  PROTOBUF_FIELD_OFFSET(::dmi::PhysicalInventoryRequest, _internal_metadata_),
  ~0u,  // no _extensions_
  ~0u,  // no _oneof_case_
  ~0u,  // no _weak_field_map_
  PROTOBUF_FIELD_OFFSET(::dmi::PhysicalInventoryRequest, device_uuid_),
  ~0u,  // no _has_bits_
  PROTOBUF_FIELD_OFFSET(::dmi::PhysicalInventoryResponse, _internal_metadata_),
  ~0u,  // no _extensions_
  ~0u,  // no _oneof_case_
  ~0u,  // no _weak_field_map_
  PROTOBUF_FIELD_OFFSET(::dmi::PhysicalInventoryResponse, status_),
  PROTOBUF_FIELD_OFFSET(::dmi::PhysicalInventoryResponse, reason_),
  PROTOBUF_FIELD_OFFSET(::dmi::PhysicalInventoryResponse, inventory_),
  PROTOBUF_FIELD_OFFSET(::dmi::PhysicalInventoryResponse, reason_detail_),
  ~0u,  // no _has_bits_
  PROTOBUF_FIELD_OFFSET(::dmi::HWComponentInfoGetRequest, _internal_metadata_),
  ~0u,  // no _extensions_
  ~0u,  // no _oneof_case_
  ~0u,  // no _weak_field_map_
  PROTOBUF_FIELD_OFFSET(::dmi::HWComponentInfoGetRequest, device_uuid_),
  PROTOBUF_FIELD_OFFSET(::dmi::HWComponentInfoGetRequest, component_uuid_),
  PROTOBUF_FIELD_OFFSET(::dmi::HWComponentInfoGetRequest, component_name_),
  ~0u,  // no _has_bits_
  PROTOBUF_FIELD_OFFSET(::dmi::HWComponentInfoGetResponse, _internal_metadata_),
  ~0u,  // no _extensions_
  ~0u,  // no _oneof_case_
  ~0u,  // no _weak_field_map_
  PROTOBUF_FIELD_OFFSET(::dmi::HWComponentInfoGetResponse, status_),
  PROTOBUF_FIELD_OFFSET(::dmi::HWComponentInfoGetResponse, reason_),
  PROTOBUF_FIELD_OFFSET(::dmi::HWComponentInfoGetResponse, component_),
  PROTOBUF_FIELD_OFFSET(::dmi::HWComponentInfoGetResponse, reason_detail_),
  ~0u,  // no _has_bits_
  PROTOBUF_FIELD_OFFSET(::dmi::HWComponentInfoSetRequest, _internal_metadata_),
  ~0u,  // no _extensions_
  ~0u,  // no _oneof_case_
  ~0u,  // no _weak_field_map_
  PROTOBUF_FIELD_OFFSET(::dmi::HWComponentInfoSetRequest, device_uuid_),
  PROTOBUF_FIELD_OFFSET(::dmi::HWComponentInfoSetRequest, component_uuid_),
  PROTOBUF_FIELD_OFFSET(::dmi::HWComponentInfoSetRequest, component_name_),
  PROTOBUF_FIELD_OFFSET(::dmi::HWComponentInfoSetRequest, changes_),
  ~0u,  // no _has_bits_
  PROTOBUF_FIELD_OFFSET(::dmi::HWComponentInfoSetResponse, _internal_metadata_),
  ~0u,  // no _extensions_
  ~0u,  // no _oneof_case_
  ~0u,  // no _weak_field_map_
  PROTOBUF_FIELD_OFFSET(::dmi::HWComponentInfoSetResponse, status_),
  PROTOBUF_FIELD_OFFSET(::dmi::HWComponentInfoSetResponse, reason_),
  PROTOBUF_FIELD_OFFSET(::dmi::HWComponentInfoSetResponse, reason_detail_),
  ~0u,  // no _has_bits_
  PROTOBUF_FIELD_OFFSET(::dmi::StartManagingDeviceResponse, _internal_metadata_),
  ~0u,  // no _extensions_
  ~0u,  // no _oneof_case_
  ~0u,  // no _weak_field_map_
  PROTOBUF_FIELD_OFFSET(::dmi::StartManagingDeviceResponse, status_),
  PROTOBUF_FIELD_OFFSET(::dmi::StartManagingDeviceResponse, reason_),
  PROTOBUF_FIELD_OFFSET(::dmi::StartManagingDeviceResponse, device_uuid_),
  PROTOBUF_FIELD_OFFSET(::dmi::StartManagingDeviceResponse, reason_detail_),
  ~0u,  // no _has_bits_
  PROTOBUF_FIELD_OFFSET(::dmi::StopManagingDeviceRequest, _internal_metadata_),
  ~0u,  // no _extensions_
  ~0u,  // no _oneof_case_
  ~0u,  // no _weak_field_map_
  PROTOBUF_FIELD_OFFSET(::dmi::StopManagingDeviceRequest, name_),
  ~0u,  // no _has_bits_
  PROTOBUF_FIELD_OFFSET(::dmi::StopManagingDeviceResponse, _internal_metadata_),
  ~0u,  // no _extensions_
  ~0u,  // no _oneof_case_
  ~0u,  // no _weak_field_map_
  PROTOBUF_FIELD_OFFSET(::dmi::StopManagingDeviceResponse, status_),
  PROTOBUF_FIELD_OFFSET(::dmi::StopManagingDeviceResponse, reason_),
  PROTOBUF_FIELD_OFFSET(::dmi::StopManagingDeviceResponse, reason_detail_),
  ~0u,  // no _has_bits_
  PROTOBUF_FIELD_OFFSET(::dmi::ManagedDeviceInfo, _internal_metadata_),
  ~0u,  // no _extensions_
  ~0u,  // no _oneof_case_
  ~0u,  // no _weak_field_map_
  PROTOBUF_FIELD_OFFSET(::dmi::ManagedDeviceInfo, info_),
  PROTOBUF_FIELD_OFFSET(::dmi::ManagedDeviceInfo, device_uuid_),
  ~0u,  // no _has_bits_
  PROTOBUF_FIELD_OFFSET(::dmi::ManagedDevicesResponse, _internal_metadata_),
  ~0u,  // no _extensions_
  ~0u,  // no _oneof_case_
  ~0u,  // no _weak_field_map_
  PROTOBUF_FIELD_OFFSET(::dmi::ManagedDevicesResponse, status_),
  PROTOBUF_FIELD_OFFSET(::dmi::ManagedDevicesResponse, reason_),
  PROTOBUF_FIELD_OFFSET(::dmi::ManagedDevicesResponse, devices_),
  ~0u,  // no _has_bits_
  PROTOBUF_FIELD_OFFSET(::dmi::SetLoggingEndpointRequest, _internal_metadata_),
  ~0u,  // no _extensions_
  ~0u,  // no _oneof_case_
  ~0u,  // no _weak_field_map_
  PROTOBUF_FIELD_OFFSET(::dmi::SetLoggingEndpointRequest, device_uuid_),
  PROTOBUF_FIELD_OFFSET(::dmi::SetLoggingEndpointRequest, logging_endpoint_),
  PROTOBUF_FIELD_OFFSET(::dmi::SetLoggingEndpointRequest, logging_protocol_),
  ~0u,  // no _has_bits_
  PROTOBUF_FIELD_OFFSET(::dmi::SetRemoteEndpointResponse, _internal_metadata_),
  ~0u,  // no _extensions_
  ~0u,  // no _oneof_case_
  ~0u,  // no _weak_field_map_
  PROTOBUF_FIELD_OFFSET(::dmi::SetRemoteEndpointResponse, status_),
  PROTOBUF_FIELD_OFFSET(::dmi::SetRemoteEndpointResponse, reason_),
  PROTOBUF_FIELD_OFFSET(::dmi::SetRemoteEndpointResponse, reason_detail_),
  ~0u,  // no _has_bits_
  PROTOBUF_FIELD_OFFSET(::dmi::GetLoggingEndpointResponse, _internal_metadata_),
  ~0u,  // no _extensions_
  ~0u,  // no _oneof_case_
  ~0u,  // no _weak_field_map_
  PROTOBUF_FIELD_OFFSET(::dmi::GetLoggingEndpointResponse, status_),
  PROTOBUF_FIELD_OFFSET(::dmi::GetLoggingEndpointResponse, reason_),
  PROTOBUF_FIELD_OFFSET(::dmi::GetLoggingEndpointResponse, logging_endpoint_),
  PROTOBUF_FIELD_OFFSET(::dmi::GetLoggingEndpointResponse, logging_protocol_),
  PROTOBUF_FIELD_OFFSET(::dmi::GetLoggingEndpointResponse, reason_detail_),
  ~0u,  // no _has_bits_
  PROTOBUF_FIELD_OFFSET(::dmi::SetMsgBusEndpointRequest, _internal_metadata_),
  ~0u,  // no _extensions_
  ~0u,  // no _oneof_case_
  ~0u,  // no _weak_field_map_
  PROTOBUF_FIELD_OFFSET(::dmi::SetMsgBusEndpointRequest, msgbus_endpoint_),
  ~0u,  // no _has_bits_
  PROTOBUF_FIELD_OFFSET(::dmi::GetMsgBusEndpointResponse, _internal_metadata_),
  ~0u,  // no _extensions_
  ~0u,  // no _oneof_case_
  ~0u,  // no _weak_field_map_
  PROTOBUF_FIELD_OFFSET(::dmi::GetMsgBusEndpointResponse, status_),
  PROTOBUF_FIELD_OFFSET(::dmi::GetMsgBusEndpointResponse, reason_),
  PROTOBUF_FIELD_OFFSET(::dmi::GetMsgBusEndpointResponse, msgbus_endpoint_),
  PROTOBUF_FIELD_OFFSET(::dmi::GetMsgBusEndpointResponse, reason_detail_),
  ~0u,  // no _has_bits_
  PROTOBUF_FIELD_OFFSET(::dmi::EntitiesLogLevel, _internal_metadata_),
  ~0u,  // no _extensions_
  ~0u,  // no _oneof_case_
  ~0u,  // no _weak_field_map_
  PROTOBUF_FIELD_OFFSET(::dmi::EntitiesLogLevel, loglevel_),
  PROTOBUF_FIELD_OFFSET(::dmi::EntitiesLogLevel, entities_),
  ~0u,  // no _has_bits_
  PROTOBUF_FIELD_OFFSET(::dmi::SetLogLevelRequest, _internal_metadata_),
  ~0u,  // no _extensions_
  ~0u,  // no _oneof_case_
  ~0u,  // no _weak_field_map_
  PROTOBUF_FIELD_OFFSET(::dmi::SetLogLevelRequest, device_uuid_),
  PROTOBUF_FIELD_OFFSET(::dmi::SetLogLevelRequest, loglevels_),
  ~0u,  // no _has_bits_
  PROTOBUF_FIELD_OFFSET(::dmi::SetLogLevelResponse, _internal_metadata_),
  ~0u,  // no _extensions_
  ~0u,  // no _oneof_case_
  ~0u,  // no _weak_field_map_
  PROTOBUF_FIELD_OFFSET(::dmi::SetLogLevelResponse, device_uuid_),
  PROTOBUF_FIELD_OFFSET(::dmi::SetLogLevelResponse, status_),
  PROTOBUF_FIELD_OFFSET(::dmi::SetLogLevelResponse, reason_),
  PROTOBUF_FIELD_OFFSET(::dmi::SetLogLevelResponse, reason_detail_),
  ~0u,  // no _has_bits_
  PROTOBUF_FIELD_OFFSET(::dmi::GetLogLevelRequest, _internal_metadata_),
  ~0u,  // no _extensions_
  ~0u,  // no _oneof_case_
  ~0u,  // no _weak_field_map_
  PROTOBUF_FIELD_OFFSET(::dmi::GetLogLevelRequest, device_uuid_),
  PROTOBUF_FIELD_OFFSET(::dmi::GetLogLevelRequest, entities_),
  ~0u,  // no _has_bits_
  PROTOBUF_FIELD_OFFSET(::dmi::GetLogLevelResponse, _internal_metadata_),
  ~0u,  // no _extensions_
  ~0u,  // no _oneof_case_
  ~0u,  // no _weak_field_map_
  PROTOBUF_FIELD_OFFSET(::dmi::GetLogLevelResponse, device_uuid_),
  PROTOBUF_FIELD_OFFSET(::dmi::GetLogLevelResponse, loglevels_),
  PROTOBUF_FIELD_OFFSET(::dmi::GetLogLevelResponse, status_),
  PROTOBUF_FIELD_OFFSET(::dmi::GetLogLevelResponse, reason_),
  PROTOBUF_FIELD_OFFSET(::dmi::GetLogLevelResponse, reason_detail_),
  ~0u,  // no _has_bits_
  PROTOBUF_FIELD_OFFSET(::dmi::GetLoggableEntitiesRequest, _internal_metadata_),
  ~0u,  // no _extensions_
  ~0u,  // no _oneof_case_
  ~0u,  // no _weak_field_map_
  PROTOBUF_FIELD_OFFSET(::dmi::GetLoggableEntitiesRequest, device_uuid_),
  ~0u,  // no _has_bits_
  PROTOBUF_FIELD_OFFSET(::dmi::Heartbeat, _internal_metadata_),
  ~0u,  // no _extensions_
  ~0u,  // no _oneof_case_
  ~0u,  // no _weak_field_map_
  PROTOBUF_FIELD_OFFSET(::dmi::Heartbeat, heartbeat_signature_),
  ~0u,  // no _has_bits_
  PROTOBUF_FIELD_OFFSET(::dmi::RebootDeviceRequest, _internal_metadata_),
  ~0u,  // no _extensions_
  ~0u,  // no _oneof_case_
  ~0u,  // no _weak_field_map_
  PROTOBUF_FIELD_OFFSET(::dmi::RebootDeviceRequest, device_uuid_),
  ~0u,  // no _has_bits_
  PROTOBUF_FIELD_OFFSET(::dmi::RebootDeviceResponse, _internal_metadata_),
  ~0u,  // no _extensions_
  ~0u,  // no _oneof_case_
  ~0u,  // no _weak_field_map_
  PROTOBUF_FIELD_OFFSET(::dmi::RebootDeviceResponse, status_),
  PROTOBUF_FIELD_OFFSET(::dmi::RebootDeviceResponse, reason_),
  PROTOBUF_FIELD_OFFSET(::dmi::RebootDeviceResponse, reason_detail_),
};
static const ::google::protobuf::internal::MigrationSchema schemas[] PROTOBUF_SECTION_VARIABLE(protodesc_cold) = {
  { 0, -1, sizeof(::dmi::PhysicalInventoryRequest)},
  { 6, -1, sizeof(::dmi::PhysicalInventoryResponse)},
  { 15, -1, sizeof(::dmi::HWComponentInfoGetRequest)},
  { 23, -1, sizeof(::dmi::HWComponentInfoGetResponse)},
  { 32, -1, sizeof(::dmi::HWComponentInfoSetRequest)},
  { 41, -1, sizeof(::dmi::HWComponentInfoSetResponse)},
  { 49, -1, sizeof(::dmi::StartManagingDeviceResponse)},
  { 58, -1, sizeof(::dmi::StopManagingDeviceRequest)},
  { 64, -1, sizeof(::dmi::StopManagingDeviceResponse)},
  { 72, -1, sizeof(::dmi::ManagedDeviceInfo)},
  { 79, -1, sizeof(::dmi::ManagedDevicesResponse)},
  { 87, -1, sizeof(::dmi::SetLoggingEndpointRequest)},
  { 95, -1, sizeof(::dmi::SetRemoteEndpointResponse)},
  { 103, -1, sizeof(::dmi::GetLoggingEndpointResponse)},
  { 113, -1, sizeof(::dmi::SetMsgBusEndpointRequest)},
  { 119, -1, sizeof(::dmi::GetMsgBusEndpointResponse)},
  { 128, -1, sizeof(::dmi::EntitiesLogLevel)},
  { 135, -1, sizeof(::dmi::SetLogLevelRequest)},
  { 142, -1, sizeof(::dmi::SetLogLevelResponse)},
  { 151, -1, sizeof(::dmi::GetLogLevelRequest)},
  { 158, -1, sizeof(::dmi::GetLogLevelResponse)},
  { 168, -1, sizeof(::dmi::GetLoggableEntitiesRequest)},
  { 174, -1, sizeof(::dmi::Heartbeat)},
  { 180, -1, sizeof(::dmi::RebootDeviceRequest)},
  { 186, -1, sizeof(::dmi::RebootDeviceResponse)},
};

static ::google::protobuf::Message const * const file_default_instances[] = {
  reinterpret_cast<const ::google::protobuf::Message*>(&::dmi::_PhysicalInventoryRequest_default_instance_),
  reinterpret_cast<const ::google::protobuf::Message*>(&::dmi::_PhysicalInventoryResponse_default_instance_),
  reinterpret_cast<const ::google::protobuf::Message*>(&::dmi::_HWComponentInfoGetRequest_default_instance_),
  reinterpret_cast<const ::google::protobuf::Message*>(&::dmi::_HWComponentInfoGetResponse_default_instance_),
  reinterpret_cast<const ::google::protobuf::Message*>(&::dmi::_HWComponentInfoSetRequest_default_instance_),
  reinterpret_cast<const ::google::protobuf::Message*>(&::dmi::_HWComponentInfoSetResponse_default_instance_),
  reinterpret_cast<const ::google::protobuf::Message*>(&::dmi::_StartManagingDeviceResponse_default_instance_),
  reinterpret_cast<const ::google::protobuf::Message*>(&::dmi::_StopManagingDeviceRequest_default_instance_),
  reinterpret_cast<const ::google::protobuf::Message*>(&::dmi::_StopManagingDeviceResponse_default_instance_),
  reinterpret_cast<const ::google::protobuf::Message*>(&::dmi::_ManagedDeviceInfo_default_instance_),
  reinterpret_cast<const ::google::protobuf::Message*>(&::dmi::_ManagedDevicesResponse_default_instance_),
  reinterpret_cast<const ::google::protobuf::Message*>(&::dmi::_SetLoggingEndpointRequest_default_instance_),
  reinterpret_cast<const ::google::protobuf::Message*>(&::dmi::_SetRemoteEndpointResponse_default_instance_),
  reinterpret_cast<const ::google::protobuf::Message*>(&::dmi::_GetLoggingEndpointResponse_default_instance_),
  reinterpret_cast<const ::google::protobuf::Message*>(&::dmi::_SetMsgBusEndpointRequest_default_instance_),
  reinterpret_cast<const ::google::protobuf::Message*>(&::dmi::_GetMsgBusEndpointResponse_default_instance_),
  reinterpret_cast<const ::google::protobuf::Message*>(&::dmi::_EntitiesLogLevel_default_instance_),
  reinterpret_cast<const ::google::protobuf::Message*>(&::dmi::_SetLogLevelRequest_default_instance_),
  reinterpret_cast<const ::google::protobuf::Message*>(&::dmi::_SetLogLevelResponse_default_instance_),
  reinterpret_cast<const ::google::protobuf::Message*>(&::dmi::_GetLogLevelRequest_default_instance_),
  reinterpret_cast<const ::google::protobuf::Message*>(&::dmi::_GetLogLevelResponse_default_instance_),
  reinterpret_cast<const ::google::protobuf::Message*>(&::dmi::_GetLoggableEntitiesRequest_default_instance_),
  reinterpret_cast<const ::google::protobuf::Message*>(&::dmi::_Heartbeat_default_instance_),
  reinterpret_cast<const ::google::protobuf::Message*>(&::dmi::_RebootDeviceRequest_default_instance_),
  reinterpret_cast<const ::google::protobuf::Message*>(&::dmi::_RebootDeviceResponse_default_instance_),
};

::google::protobuf::internal::AssignDescriptorsTable assign_descriptors_table_dmi_2fhw_5fmanagement_5fservice_2eproto = {
  {}, AddDescriptors_dmi_2fhw_5fmanagement_5fservice_2eproto, "dmi/hw_management_service.proto", schemas,
  file_default_instances, TableStruct_dmi_2fhw_5fmanagement_5fservice_2eproto::offsets,
  file_level_metadata_dmi_2fhw_5fmanagement_5fservice_2eproto, 25, file_level_enum_descriptors_dmi_2fhw_5fmanagement_5fservice_2eproto, file_level_service_descriptors_dmi_2fhw_5fmanagement_5fservice_2eproto,
};

const char descriptor_table_protodef_dmi_2fhw_5fmanagement_5fservice_2eproto[] =
  "\n\037dmi/hw_management_service.proto\022\003dmi\032\021"
  "dmi/commons.proto\032\014dmi/hw.proto\032\033google/"
  "protobuf/empty.proto\":\n\030PhysicalInventor"
  "yRequest\022\036\n\013device_uuid\030\001 \001(\0132\t.dmi.Uuid"
  "\"\210\002\n\031PhysicalInventoryResponse\022\033\n\006status"
  "\030\001 \001(\0162\013.dmi.Status\0225\n\006reason\030\002 \001(\0162%.dm"
  "i.PhysicalInventoryResponse.Reason\022 \n\tin"
  "ventory\030\003 \001(\0132\r.dmi.Hardware\022\025\n\rreason_d"
  "etail\030\004 \001(\t\"^\n\006Reason\022\024\n\020UNDEFINED_REASO"
  "N\020\000\022\022\n\016UNKNOWN_DEVICE\020\001\022\022\n\016INTERNAL_ERRO"
  "R\020\002\022\026\n\022DEVICE_UNREACHABLE\020\003\"v\n\031HWCompone"
  "ntInfoGetRequest\022\036\n\013device_uuid\030\001 \001(\0132\t."
  "dmi.Uuid\022!\n\016component_uuid\030\002 \001(\0132\t.dmi.U"
  "uid\022\026\n\016component_name\030\003 \001(\t\"\242\002\n\032HWCompon"
  "entInfoGetResponse\022\033\n\006status\030\001 \001(\0162\013.dmi"
  ".Status\0226\n\006reason\030\002 \001(\0162&.dmi.HWComponen"
  "tInfoGetResponse.Reason\022!\n\tcomponent\030\003 \001"
  "(\0132\016.dmi.Component\022\025\n\rreason_detail\030\004 \001("
  "\t\"u\n\006Reason\022\024\n\020UNDEFINED_REASON\020\000\022\022\n\016UNK"
  "NOWN_DEVICE\020\001\022\025\n\021UNKNOWN_COMPONENT\020\002\022\022\n\016"
  "INTERNAL_ERROR\020\003\022\026\n\022DEVICE_UNREACHABLE\020\004"
  "\"\241\001\n\031HWComponentInfoSetRequest\022\036\n\013device"
  "_uuid\030\001 \001(\0132\t.dmi.Uuid\022!\n\016component_uuid"
  "\030\002 \001(\0132\t.dmi.Uuid\022\026\n\016component_name\030\003 \001("
  "\t\022)\n\007changes\030\004 \001(\0132\030.dmi.ModifiableCompo"
  "nent\"\224\002\n\032HWComponentInfoSetResponse\022\033\n\006s"
  "tatus\030\001 \001(\0162\013.dmi.Status\0226\n\006reason\030\002 \001(\016"
  "2&.dmi.HWComponentInfoSetResponse.Reason"
  "\022\025\n\rreason_detail\030\003 \001(\t\"\211\001\n\006Reason\022\024\n\020UN"
  "DEFINED_REASON\020\000\022\022\n\016UNKNOWN_DEVICE\020\001\022\025\n\021"
  "UNKNOWN_COMPONENT\020\002\022\022\n\016INVALID_PARAMS\020\003\022"
  "\022\n\016INTERNAL_ERROR\020\004\022\026\n\022DEVICE_UNREACHABL"
  "E\020\005\"\347\002\n\033StartManagingDeviceResponse\022\033\n\006s"
  "tatus\030\001 \001(\0162\013.dmi.Status\0227\n\006reason\030\002 \001(\016"
  "2\'.dmi.StartManagingDeviceResponse.Reaso"
  "n\022\036\n\013device_uuid\030\003 \001(\0132\t.dmi.Uuid\022\025\n\rrea"
  "son_detail\030\004 \001(\t\"\272\001\n\006Reason\022\024\n\020UNDEFINED"
  "_REASON\020\000\022\032\n\026DEVICE_ALREADY_MANAGED\020\001\022!\n"
  "\035OPERATION_ALREADY_IN_PROGRESS\020\002\022\022\n\016INVA"
  "LID_PARAMS\020\003\022\022\n\016INTERNAL_ERROR\020\004\022\032\n\026AUTH"
  "ENTICATION_FAILURE\020\005\022\027\n\023INCOMPATIBLE_DEV"
  "ICE\020\006\")\n\031StopManagingDeviceRequest\022\014\n\004na"
  "me\030\001 \001(\t\"\274\001\n\032StopManagingDeviceResponse\022"
  "\033\n\006status\030\001 \001(\0162\013.dmi.Status\0226\n\006reason\030\002"
  " \001(\0162&.dmi.StopManagingDeviceResponse.Re"
  "ason\022\025\n\rreason_detail\030\003 \001(\t\"2\n\006Reason\022\024\n"
  "\020UNDEFINED_REASON\020\000\022\022\n\016UNKNOWN_DEVICE\020\001\""
  "[\n\021ManagedDeviceInfo\022&\n\004info\030\001 \001(\0132\030.dmi"
  ".ModifiableComponent\022\036\n\013device_uuid\030\002 \001("
  "\0132\t.dmi.Uuid\"\306\001\n\026ManagedDevicesResponse\022"
  "\033\n\006status\030\001 \001(\0162\013.dmi.Status\0222\n\006reason\030\002"
  " \001(\0162\".dmi.ManagedDevicesResponse.Reason"
  "\022\'\n\007devices\030\003 \003(\0132\026.dmi.ManagedDeviceInf"
  "o\"2\n\006Reason\022\024\n\020UNDEFINED_REASON\020\000\022\022\n\016INT"
  "ERNAL_ERROR\020\001\"o\n\031SetLoggingEndpointReque"
  "st\022\036\n\013device_uuid\030\001 \001(\0132\t.dmi.Uuid\022\030\n\020lo"
  "gging_endpoint\030\002 \001(\t\022\030\n\020logging_protocol"
  "\030\003 \001(\t\"\303\002\n\031SetRemoteEndpointResponse\022\033\n\006"
  "status\030\001 \001(\0162\013.dmi.Status\0225\n\006reason\030\002 \001("
  "\0162%.dmi.SetRemoteEndpointResponse.Reason"
  "\022\025\n\rreason_detail\030\003 \001(\t\"\272\001\n\006Reason\022\024\n\020UN"
  "DEFINED_REASON\020\000\022\022\n\016UNKNOWN_DEVICE\020\001\022\022\n\016"
  "INTERNAL_ERROR\020\002\022\032\n\026LOGGING_ENDPOINT_ERR"
  "OR\020\003\022#\n\037LOGGING_ENDPOINT_PROTOCOL_ERROR\020"
  "\004\022\031\n\025MSGBUS_ENDPOINT_ERROR\020\005\022\026\n\022DEVICE_U"
  "NREACHABLE\020\006\"\234\002\n\032GetLoggingEndpointRespo"
  "nse\022\033\n\006status\030\001 \001(\0162\013.dmi.Status\0226\n\006reas"
  "on\030\002 \001(\0162&.dmi.GetLoggingEndpointRespons"
  "e.Reason\022\030\n\020logging_endpoint\030\003 \001(\t\022\030\n\020lo"
  "gging_protocol\030\004 \001(\t\022\025\n\rreason_detail\030\005 "
  "\001(\t\"^\n\006Reason\022\024\n\020UNDEFINED_REASON\020\000\022\022\n\016U"
  "NKNOWN_DEVICE\020\001\022\022\n\016INTERNAL_ERROR\020\002\022\026\n\022D"
  "EVICE_UNREACHABLE\020\003\"3\n\030SetMsgBusEndpoint"
  "Request\022\027\n\017msgbus_endpoint\030\001 \001(\t\"\353\001\n\031Get"
  "MsgBusEndpointResponse\022\033\n\006status\030\001 \001(\0162\013"
  ".dmi.Status\0225\n\006reason\030\002 \001(\0162%.dmi.GetMsg"
  "BusEndpointResponse.Reason\022\027\n\017msgbus_end"
  "point\030\003 \001(\t\022\025\n\rreason_detail\030\004 \001(\t\"J\n\006Re"
  "ason\022\024\n\020UNDEFINED_REASON\020\000\022\022\n\016INTERNAL_E"
  "RROR\020\001\022\026\n\022DEVICE_UNREACHABLE\020\002\"E\n\020Entiti"
  "esLogLevel\022\037\n\010logLevel\030\001 \001(\0162\r.dmi.LogLe"
  "vel\022\020\n\010entities\030\002 \003(\t\"^\n\022SetLogLevelRequ"
  "est\022\036\n\013device_uuid\030\001 \001(\0132\t.dmi.Uuid\022(\n\tl"
  "oglevels\030\002 \003(\0132\025.dmi.EntitiesLogLevel\"\222\002"
  "\n\023SetLogLevelResponse\022\036\n\013device_uuid\030\001 \001"
  "(\0132\t.dmi.Uuid\022\033\n\006status\030\002 \001(\0162\013.dmi.Stat"
  "us\022/\n\006reason\030\003 \001(\0162\037.dmi.SetLogLevelResp"
  "onse.Reason\022\025\n\rreason_detail\030\004 \001(\t\"v\n\006Re"
  "ason\022\024\n\020UNDEFINED_REASON\020\000\022\022\n\016UNKNOWN_DE"
  "VICE\020\001\022\022\n\016INTERNAL_ERROR\020\002\022\026\n\022UNKNOWN_LO"
  "G_ENTITY\020\003\022\026\n\022DEVICE_UNREACHABLE\020\004\"F\n\022Ge"
  "tLogLevelRequest\022\036\n\013device_uuid\030\001 \001(\0132\t."
  "dmi.Uuid\022\020\n\010entities\030\002 \003(\t\"\274\002\n\023GetLogLev"
  "elResponse\022\036\n\013device_uuid\030\001 \001(\0132\t.dmi.Uu"
  "id\022(\n\tlogLevels\030\002 \003(\0132\025.dmi.EntitiesLogL"
  "evel\022\033\n\006status\030\003 \001(\0162\013.dmi.Status\022/\n\006rea"
  "son\030\004 \001(\0162\037.dmi.GetLogLevelResponse.Reas"
  "on\022\025\n\rreason_detail\030\005 \001(\t\"v\n\006Reason\022\024\n\020U"
  "NDEFINED_REASON\020\000\022\022\n\016UNKNOWN_DEVICE\020\001\022\022\n"
  "\016INTERNAL_ERROR\020\002\022\026\n\022UNKNOWN_LOG_ENTITY\020"
  "\003\022\026\n\022DEVICE_UNREACHABLE\020\004\"<\n\032GetLoggable"
  "EntitiesRequest\022\036\n\013device_uuid\030\001 \001(\0132\t.d"
  "mi.Uuid\"(\n\tHeartbeat\022\033\n\023heartbeat_signat"
  "ure\030\001 \001(\007\"5\n\023RebootDeviceRequest\022\036\n\013devi"
  "ce_uuid\030\001 \001(\0132\t.dmi.Uuid\"\334\001\n\024RebootDevic"
  "eResponse\022\033\n\006status\030\003 \001(\0162\013.dmi.Status\0220"
  "\n\006reason\030\004 \001(\0162 .dmi.RebootDeviceRespons"
  "e.Reason\022\025\n\rreason_detail\030\005 \001(\t\"^\n\006Reaso"
  "n\022\024\n\020UNDEFINED_REASON\020\000\022\022\n\016UNKNOWN_DEVIC"
  "E\020\001\022\022\n\016INTERNAL_ERROR\020\002\022\026\n\022DEVICE_UNREAC"
  "HABLE\020\0032\256\t\n\031NativeHWManagementService\022S\n"
  "\023StartManagingDevice\022\030.dmi.ModifiableCom"
  "ponent\032 .dmi.StartManagingDeviceResponse"
  "0\001\022U\n\022StopManagingDevice\022\036.dmi.StopManag"
  "ingDeviceRequest\032\037.dmi.StopManagingDevic"
  "eResponse\022H\n\021GetManagedDevices\022\026.google."
  "protobuf.Empty\032\033.dmi.ManagedDevicesRespo"
  "nse\022W\n\024GetPhysicalInventory\022\035.dmi.Physic"
  "alInventoryRequest\032\036.dmi.PhysicalInvento"
  "ryResponse0\001\022W\n\022GetHWComponentInfo\022\036.dmi"
  ".HWComponentInfoGetRequest\032\037.dmi.HWCompo"
  "nentInfoGetResponse0\001\022U\n\022SetHWComponentI"
  "nfo\022\036.dmi.HWComponentInfoSetRequest\032\037.dm"
  "i.HWComponentInfoSetResponse\022T\n\022SetLoggi"
  "ngEndpoint\022\036.dmi.SetLoggingEndpointReque"
  "st\032\036.dmi.SetRemoteEndpointResponse\022F\n\022Ge"
  "tLoggingEndpoint\022\017.dmi.HardwareID\032\037.dmi."
  "GetLoggingEndpointResponse\022R\n\021SetMsgBusE"
  "ndpoint\022\035.dmi.SetMsgBusEndpointRequest\032\036"
  ".dmi.SetRemoteEndpointResponse\022K\n\021GetMsg"
  "BusEndpoint\022\026.google.protobuf.Empty\032\036.dm"
  "i.GetMsgBusEndpointResponse\022P\n\023GetLoggab"
  "leEntities\022\037.dmi.GetLoggableEntitiesRequ"
  "est\032\030.dmi.GetLogLevelResponse\022@\n\013SetLogL"
  "evel\022\027.dmi.SetLogLevelRequest\032\030.dmi.SetL"
  "ogLevelResponse\022@\n\013GetLogLevel\022\027.dmi.Get"
  "LogLevelRequest\032\030.dmi.GetLogLevelRespons"
  "e\0228\n\016HeartbeatCheck\022\026.google.protobuf.Em"
  "pty\032\016.dmi.Heartbeat\022C\n\014RebootDevice\022\030.dm"
  "i.RebootDeviceRequest\032\031.dmi.RebootDevice"
  "ResponseB;Z9github.com/opencord/device-m"
  "anagement-interface/v3/go/dmib\006proto3"
  ;
::google::protobuf::internal::DescriptorTable descriptor_table_dmi_2fhw_5fmanagement_5fservice_2eproto = {
  false, InitDefaults_dmi_2fhw_5fmanagement_5fservice_2eproto, 
  descriptor_table_protodef_dmi_2fhw_5fmanagement_5fservice_2eproto,
  "dmi/hw_management_service.proto", &assign_descriptors_table_dmi_2fhw_5fmanagement_5fservice_2eproto, 5677,
};

void AddDescriptors_dmi_2fhw_5fmanagement_5fservice_2eproto() {
  static constexpr ::google::protobuf::internal::InitFunc deps[3] =
  {
    ::AddDescriptors_dmi_2fcommons_2eproto,
    ::AddDescriptors_dmi_2fhw_2eproto,
    ::AddDescriptors_google_2fprotobuf_2fempty_2eproto,
  };
 ::google::protobuf::internal::AddDescriptors(&descriptor_table_dmi_2fhw_5fmanagement_5fservice_2eproto, deps, 3);
}

// Force running AddDescriptors() at dynamic initialization time.
static bool dynamic_init_dummy_dmi_2fhw_5fmanagement_5fservice_2eproto = []() { AddDescriptors_dmi_2fhw_5fmanagement_5fservice_2eproto(); return true; }();
namespace dmi {
const ::google::protobuf::EnumDescriptor* PhysicalInventoryResponse_Reason_descriptor() {
  ::google::protobuf::internal::AssignDescriptors(&assign_descriptors_table_dmi_2fhw_5fmanagement_5fservice_2eproto);
  return file_level_enum_descriptors_dmi_2fhw_5fmanagement_5fservice_2eproto[0];
}
bool PhysicalInventoryResponse_Reason_IsValid(int value) {
  switch (value) {
    case 0:
    case 1:
    case 2:
    case 3:
      return true;
    default:
      return false;
  }
}

#if !defined(_MSC_VER) || _MSC_VER >= 1900
const PhysicalInventoryResponse_Reason PhysicalInventoryResponse::UNDEFINED_REASON;
const PhysicalInventoryResponse_Reason PhysicalInventoryResponse::UNKNOWN_DEVICE;
const PhysicalInventoryResponse_Reason PhysicalInventoryResponse::INTERNAL_ERROR;
const PhysicalInventoryResponse_Reason PhysicalInventoryResponse::DEVICE_UNREACHABLE;
const PhysicalInventoryResponse_Reason PhysicalInventoryResponse::Reason_MIN;
const PhysicalInventoryResponse_Reason PhysicalInventoryResponse::Reason_MAX;
const int PhysicalInventoryResponse::Reason_ARRAYSIZE;
#endif  // !defined(_MSC_VER) || _MSC_VER >= 1900
const ::google::protobuf::EnumDescriptor* HWComponentInfoGetResponse_Reason_descriptor() {
  ::google::protobuf::internal::AssignDescriptors(&assign_descriptors_table_dmi_2fhw_5fmanagement_5fservice_2eproto);
  return file_level_enum_descriptors_dmi_2fhw_5fmanagement_5fservice_2eproto[1];
}
bool HWComponentInfoGetResponse_Reason_IsValid(int value) {
  switch (value) {
    case 0:
    case 1:
    case 2:
    case 3:
    case 4:
      return true;
    default:
      return false;
  }
}

#if !defined(_MSC_VER) || _MSC_VER >= 1900
const HWComponentInfoGetResponse_Reason HWComponentInfoGetResponse::UNDEFINED_REASON;
const HWComponentInfoGetResponse_Reason HWComponentInfoGetResponse::UNKNOWN_DEVICE;
const HWComponentInfoGetResponse_Reason HWComponentInfoGetResponse::UNKNOWN_COMPONENT;
const HWComponentInfoGetResponse_Reason HWComponentInfoGetResponse::INTERNAL_ERROR;
const HWComponentInfoGetResponse_Reason HWComponentInfoGetResponse::DEVICE_UNREACHABLE;
const HWComponentInfoGetResponse_Reason HWComponentInfoGetResponse::Reason_MIN;
const HWComponentInfoGetResponse_Reason HWComponentInfoGetResponse::Reason_MAX;
const int HWComponentInfoGetResponse::Reason_ARRAYSIZE;
#endif  // !defined(_MSC_VER) || _MSC_VER >= 1900
const ::google::protobuf::EnumDescriptor* HWComponentInfoSetResponse_Reason_descriptor() {
  ::google::protobuf::internal::AssignDescriptors(&assign_descriptors_table_dmi_2fhw_5fmanagement_5fservice_2eproto);
  return file_level_enum_descriptors_dmi_2fhw_5fmanagement_5fservice_2eproto[2];
}
bool HWComponentInfoSetResponse_Reason_IsValid(int value) {
  switch (value) {
    case 0:
    case 1:
    case 2:
    case 3:
    case 4:
    case 5:
      return true;
    default:
      return false;
  }
}

#if !defined(_MSC_VER) || _MSC_VER >= 1900
const HWComponentInfoSetResponse_Reason HWComponentInfoSetResponse::UNDEFINED_REASON;
const HWComponentInfoSetResponse_Reason HWComponentInfoSetResponse::UNKNOWN_DEVICE;
const HWComponentInfoSetResponse_Reason HWComponentInfoSetResponse::UNKNOWN_COMPONENT;
const HWComponentInfoSetResponse_Reason HWComponentInfoSetResponse::INVALID_PARAMS;
const HWComponentInfoSetResponse_Reason HWComponentInfoSetResponse::INTERNAL_ERROR;
const HWComponentInfoSetResponse_Reason HWComponentInfoSetResponse::DEVICE_UNREACHABLE;
const HWComponentInfoSetResponse_Reason HWComponentInfoSetResponse::Reason_MIN;
const HWComponentInfoSetResponse_Reason HWComponentInfoSetResponse::Reason_MAX;
const int HWComponentInfoSetResponse::Reason_ARRAYSIZE;
#endif  // !defined(_MSC_VER) || _MSC_VER >= 1900
const ::google::protobuf::EnumDescriptor* StartManagingDeviceResponse_Reason_descriptor() {
  ::google::protobuf::internal::AssignDescriptors(&assign_descriptors_table_dmi_2fhw_5fmanagement_5fservice_2eproto);
  return file_level_enum_descriptors_dmi_2fhw_5fmanagement_5fservice_2eproto[3];
}
bool StartManagingDeviceResponse_Reason_IsValid(int value) {
  switch (value) {
    case 0:
    case 1:
    case 2:
    case 3:
    case 4:
    case 5:
    case 6:
      return true;
    default:
      return false;
  }
}

#if !defined(_MSC_VER) || _MSC_VER >= 1900
const StartManagingDeviceResponse_Reason StartManagingDeviceResponse::UNDEFINED_REASON;
const StartManagingDeviceResponse_Reason StartManagingDeviceResponse::DEVICE_ALREADY_MANAGED;
const StartManagingDeviceResponse_Reason StartManagingDeviceResponse::OPERATION_ALREADY_IN_PROGRESS;
const StartManagingDeviceResponse_Reason StartManagingDeviceResponse::INVALID_PARAMS;
const StartManagingDeviceResponse_Reason StartManagingDeviceResponse::INTERNAL_ERROR;
const StartManagingDeviceResponse_Reason StartManagingDeviceResponse::AUTHENTICATION_FAILURE;
const StartManagingDeviceResponse_Reason StartManagingDeviceResponse::INCOMPATIBLE_DEVICE;
const StartManagingDeviceResponse_Reason StartManagingDeviceResponse::Reason_MIN;
const StartManagingDeviceResponse_Reason StartManagingDeviceResponse::Reason_MAX;
const int StartManagingDeviceResponse::Reason_ARRAYSIZE;
#endif  // !defined(_MSC_VER) || _MSC_VER >= 1900
const ::google::protobuf::EnumDescriptor* StopManagingDeviceResponse_Reason_descriptor() {
  ::google::protobuf::internal::AssignDescriptors(&assign_descriptors_table_dmi_2fhw_5fmanagement_5fservice_2eproto);
  return file_level_enum_descriptors_dmi_2fhw_5fmanagement_5fservice_2eproto[4];
}
bool StopManagingDeviceResponse_Reason_IsValid(int value) {
  switch (value) {
    case 0:
    case 1:
      return true;
    default:
      return false;
  }
}

#if !defined(_MSC_VER) || _MSC_VER >= 1900
const StopManagingDeviceResponse_Reason StopManagingDeviceResponse::UNDEFINED_REASON;
const StopManagingDeviceResponse_Reason StopManagingDeviceResponse::UNKNOWN_DEVICE;
const StopManagingDeviceResponse_Reason StopManagingDeviceResponse::Reason_MIN;
const StopManagingDeviceResponse_Reason StopManagingDeviceResponse::Reason_MAX;
const int StopManagingDeviceResponse::Reason_ARRAYSIZE;
#endif  // !defined(_MSC_VER) || _MSC_VER >= 1900
const ::google::protobuf::EnumDescriptor* ManagedDevicesResponse_Reason_descriptor() {
  ::google::protobuf::internal::AssignDescriptors(&assign_descriptors_table_dmi_2fhw_5fmanagement_5fservice_2eproto);
  return file_level_enum_descriptors_dmi_2fhw_5fmanagement_5fservice_2eproto[5];
}
bool ManagedDevicesResponse_Reason_IsValid(int value) {
  switch (value) {
    case 0:
    case 1:
      return true;
    default:
      return false;
  }
}

#if !defined(_MSC_VER) || _MSC_VER >= 1900
const ManagedDevicesResponse_Reason ManagedDevicesResponse::UNDEFINED_REASON;
const ManagedDevicesResponse_Reason ManagedDevicesResponse::INTERNAL_ERROR;
const ManagedDevicesResponse_Reason ManagedDevicesResponse::Reason_MIN;
const ManagedDevicesResponse_Reason ManagedDevicesResponse::Reason_MAX;
const int ManagedDevicesResponse::Reason_ARRAYSIZE;
#endif  // !defined(_MSC_VER) || _MSC_VER >= 1900
const ::google::protobuf::EnumDescriptor* SetRemoteEndpointResponse_Reason_descriptor() {
  ::google::protobuf::internal::AssignDescriptors(&assign_descriptors_table_dmi_2fhw_5fmanagement_5fservice_2eproto);
  return file_level_enum_descriptors_dmi_2fhw_5fmanagement_5fservice_2eproto[6];
}
bool SetRemoteEndpointResponse_Reason_IsValid(int value) {
  switch (value) {
    case 0:
    case 1:
    case 2:
    case 3:
    case 4:
    case 5:
    case 6:
      return true;
    default:
      return false;
  }
}

#if !defined(_MSC_VER) || _MSC_VER >= 1900
const SetRemoteEndpointResponse_Reason SetRemoteEndpointResponse::UNDEFINED_REASON;
const SetRemoteEndpointResponse_Reason SetRemoteEndpointResponse::UNKNOWN_DEVICE;
const SetRemoteEndpointResponse_Reason SetRemoteEndpointResponse::INTERNAL_ERROR;
const SetRemoteEndpointResponse_Reason SetRemoteEndpointResponse::LOGGING_ENDPOINT_ERROR;
const SetRemoteEndpointResponse_Reason SetRemoteEndpointResponse::LOGGING_ENDPOINT_PROTOCOL_ERROR;
const SetRemoteEndpointResponse_Reason SetRemoteEndpointResponse::MSGBUS_ENDPOINT_ERROR;
const SetRemoteEndpointResponse_Reason SetRemoteEndpointResponse::DEVICE_UNREACHABLE;
const SetRemoteEndpointResponse_Reason SetRemoteEndpointResponse::Reason_MIN;
const SetRemoteEndpointResponse_Reason SetRemoteEndpointResponse::Reason_MAX;
const int SetRemoteEndpointResponse::Reason_ARRAYSIZE;
#endif  // !defined(_MSC_VER) || _MSC_VER >= 1900
const ::google::protobuf::EnumDescriptor* GetLoggingEndpointResponse_Reason_descriptor() {
  ::google::protobuf::internal::AssignDescriptors(&assign_descriptors_table_dmi_2fhw_5fmanagement_5fservice_2eproto);
  return file_level_enum_descriptors_dmi_2fhw_5fmanagement_5fservice_2eproto[7];
}
bool GetLoggingEndpointResponse_Reason_IsValid(int value) {
  switch (value) {
    case 0:
    case 1:
    case 2:
    case 3:
      return true;
    default:
      return false;
  }
}

#if !defined(_MSC_VER) || _MSC_VER >= 1900
const GetLoggingEndpointResponse_Reason GetLoggingEndpointResponse::UNDEFINED_REASON;
const GetLoggingEndpointResponse_Reason GetLoggingEndpointResponse::UNKNOWN_DEVICE;
const GetLoggingEndpointResponse_Reason GetLoggingEndpointResponse::INTERNAL_ERROR;
const GetLoggingEndpointResponse_Reason GetLoggingEndpointResponse::DEVICE_UNREACHABLE;
const GetLoggingEndpointResponse_Reason GetLoggingEndpointResponse::Reason_MIN;
const GetLoggingEndpointResponse_Reason GetLoggingEndpointResponse::Reason_MAX;
const int GetLoggingEndpointResponse::Reason_ARRAYSIZE;
#endif  // !defined(_MSC_VER) || _MSC_VER >= 1900
const ::google::protobuf::EnumDescriptor* GetMsgBusEndpointResponse_Reason_descriptor() {
  ::google::protobuf::internal::AssignDescriptors(&assign_descriptors_table_dmi_2fhw_5fmanagement_5fservice_2eproto);
  return file_level_enum_descriptors_dmi_2fhw_5fmanagement_5fservice_2eproto[8];
}
bool GetMsgBusEndpointResponse_Reason_IsValid(int value) {
  switch (value) {
    case 0:
    case 1:
    case 2:
      return true;
    default:
      return false;
  }
}

#if !defined(_MSC_VER) || _MSC_VER >= 1900
const GetMsgBusEndpointResponse_Reason GetMsgBusEndpointResponse::UNDEFINED_REASON;
const GetMsgBusEndpointResponse_Reason GetMsgBusEndpointResponse::INTERNAL_ERROR;
const GetMsgBusEndpointResponse_Reason GetMsgBusEndpointResponse::DEVICE_UNREACHABLE;
const GetMsgBusEndpointResponse_Reason GetMsgBusEndpointResponse::Reason_MIN;
const GetMsgBusEndpointResponse_Reason GetMsgBusEndpointResponse::Reason_MAX;
const int GetMsgBusEndpointResponse::Reason_ARRAYSIZE;
#endif  // !defined(_MSC_VER) || _MSC_VER >= 1900
const ::google::protobuf::EnumDescriptor* SetLogLevelResponse_Reason_descriptor() {
  ::google::protobuf::internal::AssignDescriptors(&assign_descriptors_table_dmi_2fhw_5fmanagement_5fservice_2eproto);
  return file_level_enum_descriptors_dmi_2fhw_5fmanagement_5fservice_2eproto[9];
}
bool SetLogLevelResponse_Reason_IsValid(int value) {
  switch (value) {
    case 0:
    case 1:
    case 2:
    case 3:
    case 4:
      return true;
    default:
      return false;
  }
}

#if !defined(_MSC_VER) || _MSC_VER >= 1900
const SetLogLevelResponse_Reason SetLogLevelResponse::UNDEFINED_REASON;
const SetLogLevelResponse_Reason SetLogLevelResponse::UNKNOWN_DEVICE;
const SetLogLevelResponse_Reason SetLogLevelResponse::INTERNAL_ERROR;
const SetLogLevelResponse_Reason SetLogLevelResponse::UNKNOWN_LOG_ENTITY;
const SetLogLevelResponse_Reason SetLogLevelResponse::DEVICE_UNREACHABLE;
const SetLogLevelResponse_Reason SetLogLevelResponse::Reason_MIN;
const SetLogLevelResponse_Reason SetLogLevelResponse::Reason_MAX;
const int SetLogLevelResponse::Reason_ARRAYSIZE;
#endif  // !defined(_MSC_VER) || _MSC_VER >= 1900
const ::google::protobuf::EnumDescriptor* GetLogLevelResponse_Reason_descriptor() {
  ::google::protobuf::internal::AssignDescriptors(&assign_descriptors_table_dmi_2fhw_5fmanagement_5fservice_2eproto);
  return file_level_enum_descriptors_dmi_2fhw_5fmanagement_5fservice_2eproto[10];
}
bool GetLogLevelResponse_Reason_IsValid(int value) {
  switch (value) {
    case 0:
    case 1:
    case 2:
    case 3:
    case 4:
      return true;
    default:
      return false;
  }
}

#if !defined(_MSC_VER) || _MSC_VER >= 1900
const GetLogLevelResponse_Reason GetLogLevelResponse::UNDEFINED_REASON;
const GetLogLevelResponse_Reason GetLogLevelResponse::UNKNOWN_DEVICE;
const GetLogLevelResponse_Reason GetLogLevelResponse::INTERNAL_ERROR;
const GetLogLevelResponse_Reason GetLogLevelResponse::UNKNOWN_LOG_ENTITY;
const GetLogLevelResponse_Reason GetLogLevelResponse::DEVICE_UNREACHABLE;
const GetLogLevelResponse_Reason GetLogLevelResponse::Reason_MIN;
const GetLogLevelResponse_Reason GetLogLevelResponse::Reason_MAX;
const int GetLogLevelResponse::Reason_ARRAYSIZE;
#endif  // !defined(_MSC_VER) || _MSC_VER >= 1900
const ::google::protobuf::EnumDescriptor* RebootDeviceResponse_Reason_descriptor() {
  ::google::protobuf::internal::AssignDescriptors(&assign_descriptors_table_dmi_2fhw_5fmanagement_5fservice_2eproto);
  return file_level_enum_descriptors_dmi_2fhw_5fmanagement_5fservice_2eproto[11];
}
bool RebootDeviceResponse_Reason_IsValid(int value) {
  switch (value) {
    case 0:
    case 1:
    case 2:
    case 3:
      return true;
    default:
      return false;
  }
}

#if !defined(_MSC_VER) || _MSC_VER >= 1900
const RebootDeviceResponse_Reason RebootDeviceResponse::UNDEFINED_REASON;
const RebootDeviceResponse_Reason RebootDeviceResponse::UNKNOWN_DEVICE;
const RebootDeviceResponse_Reason RebootDeviceResponse::INTERNAL_ERROR;
const RebootDeviceResponse_Reason RebootDeviceResponse::DEVICE_UNREACHABLE;
const RebootDeviceResponse_Reason RebootDeviceResponse::Reason_MIN;
const RebootDeviceResponse_Reason RebootDeviceResponse::Reason_MAX;
const int RebootDeviceResponse::Reason_ARRAYSIZE;
#endif  // !defined(_MSC_VER) || _MSC_VER >= 1900

// ===================================================================

void PhysicalInventoryRequest::InitAsDefaultInstance() {
  ::dmi::_PhysicalInventoryRequest_default_instance_._instance.get_mutable()->device_uuid_ = const_cast< ::dmi::Uuid*>(
      ::dmi::Uuid::internal_default_instance());
}
class PhysicalInventoryRequest::HasBitSetters {
 public:
  static const ::dmi::Uuid& device_uuid(const PhysicalInventoryRequest* msg);
};

const ::dmi::Uuid&
PhysicalInventoryRequest::HasBitSetters::device_uuid(const PhysicalInventoryRequest* msg) {
  return *msg->device_uuid_;
}
void PhysicalInventoryRequest::clear_device_uuid() {
  if (GetArenaNoVirtual() == nullptr && device_uuid_ != nullptr) {
    delete device_uuid_;
  }
  device_uuid_ = nullptr;
}
#if !defined(_MSC_VER) || _MSC_VER >= 1900
const int PhysicalInventoryRequest::kDeviceUuidFieldNumber;
#endif  // !defined(_MSC_VER) || _MSC_VER >= 1900

PhysicalInventoryRequest::PhysicalInventoryRequest()
  : ::google::protobuf::Message(), _internal_metadata_(nullptr) {
  SharedCtor();
  // @@protoc_insertion_point(constructor:dmi.PhysicalInventoryRequest)
}
PhysicalInventoryRequest::PhysicalInventoryRequest(const PhysicalInventoryRequest& from)
  : ::google::protobuf::Message(),
      _internal_metadata_(nullptr) {
  _internal_metadata_.MergeFrom(from._internal_metadata_);
  if (from.has_device_uuid()) {
    device_uuid_ = new ::dmi::Uuid(*from.device_uuid_);
  } else {
    device_uuid_ = nullptr;
  }
  // @@protoc_insertion_point(copy_constructor:dmi.PhysicalInventoryRequest)
}

void PhysicalInventoryRequest::SharedCtor() {
  ::google::protobuf::internal::InitSCC(
      &scc_info_PhysicalInventoryRequest_dmi_2fhw_5fmanagement_5fservice_2eproto.base);
  device_uuid_ = nullptr;
}

PhysicalInventoryRequest::~PhysicalInventoryRequest() {
  // @@protoc_insertion_point(destructor:dmi.PhysicalInventoryRequest)
  SharedDtor();
}

void PhysicalInventoryRequest::SharedDtor() {
  if (this != internal_default_instance()) delete device_uuid_;
}

void PhysicalInventoryRequest::SetCachedSize(int size) const {
  _cached_size_.Set(size);
}
const PhysicalInventoryRequest& PhysicalInventoryRequest::default_instance() {
  ::google::protobuf::internal::InitSCC(&::scc_info_PhysicalInventoryRequest_dmi_2fhw_5fmanagement_5fservice_2eproto.base);
  return *internal_default_instance();
}


void PhysicalInventoryRequest::Clear() {
// @@protoc_insertion_point(message_clear_start:dmi.PhysicalInventoryRequest)
  ::google::protobuf::uint32 cached_has_bits = 0;
  // Prevent compiler warnings about cached_has_bits being unused
  (void) cached_has_bits;

  if (GetArenaNoVirtual() == nullptr && device_uuid_ != nullptr) {
    delete device_uuid_;
  }
  device_uuid_ = nullptr;
  _internal_metadata_.Clear();
}

#if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER
const char* PhysicalInventoryRequest::_InternalParse(const char* begin, const char* end, void* object,
                  ::google::protobuf::internal::ParseContext* ctx) {
  auto msg = static_cast<PhysicalInventoryRequest*>(object);
  ::google::protobuf::int32 size; (void)size;
  int depth; (void)depth;
  ::google::protobuf::uint32 tag;
  ::google::protobuf::internal::ParseFunc parser_till_end; (void)parser_till_end;
  auto ptr = begin;
  while (ptr < end) {
    ptr = ::google::protobuf::io::Parse32(ptr, &tag);
    GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
    switch (tag >> 3) {
      // .dmi.Uuid device_uuid = 1;
      case 1: {
        if (static_cast<::google::protobuf::uint8>(tag) != 10) goto handle_unusual;
        ptr = ::google::protobuf::io::ReadSize(ptr, &size);
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
        parser_till_end = ::dmi::Uuid::_InternalParse;
        object = msg->mutable_device_uuid();
        if (size > end - ptr) goto len_delim_till_end;
        ptr += size;
        GOOGLE_PROTOBUF_PARSER_ASSERT(ctx->ParseExactRange(
            {parser_till_end, object}, ptr - size, ptr));
        break;
      }
      default: {
      handle_unusual:
        if ((tag & 7) == 4 || tag == 0) {
          ctx->EndGroup(tag);
          return ptr;
        }
        auto res = UnknownFieldParse(tag, {_InternalParse, msg},
          ptr, end, msg->_internal_metadata_.mutable_unknown_fields(), ctx);
        ptr = res.first;
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr);
        if (res.second) return ptr;
      }
    }  // switch
  }  // while
  return ptr;
len_delim_till_end:
  return ctx->StoreAndTailCall(ptr, end, {_InternalParse, msg},
                               {parser_till_end, object}, size);
}
#else  // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER
bool PhysicalInventoryRequest::MergePartialFromCodedStream(
    ::google::protobuf::io::CodedInputStream* input) {
#define DO_(EXPRESSION) if (!PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure
  ::google::protobuf::uint32 tag;
  // @@protoc_insertion_point(parse_start:dmi.PhysicalInventoryRequest)
  for (;;) {
    ::std::pair<::google::protobuf::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u);
    tag = p.first;
    if (!p.second) goto handle_unusual;
    switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) {
      // .dmi.Uuid device_uuid = 1;
      case 1: {
        if (static_cast< ::google::protobuf::uint8>(tag) == (10 & 0xFF)) {
          DO_(::google::protobuf::internal::WireFormatLite::ReadMessage(
               input, mutable_device_uuid()));
        } else {
          goto handle_unusual;
        }
        break;
      }

      default: {
      handle_unusual:
        if (tag == 0) {
          goto success;
        }
        DO_(::google::protobuf::internal::WireFormat::SkipField(
              input, tag, _internal_metadata_.mutable_unknown_fields()));
        break;
      }
    }
  }
success:
  // @@protoc_insertion_point(parse_success:dmi.PhysicalInventoryRequest)
  return true;
failure:
  // @@protoc_insertion_point(parse_failure:dmi.PhysicalInventoryRequest)
  return false;
#undef DO_
}
#endif  // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER

void PhysicalInventoryRequest::SerializeWithCachedSizes(
    ::google::protobuf::io::CodedOutputStream* output) const {
  // @@protoc_insertion_point(serialize_start:dmi.PhysicalInventoryRequest)
  ::google::protobuf::uint32 cached_has_bits = 0;
  (void) cached_has_bits;

  // .dmi.Uuid device_uuid = 1;
  if (this->has_device_uuid()) {
    ::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray(
      1, HasBitSetters::device_uuid(this), output);
  }

  if (_internal_metadata_.have_unknown_fields()) {
    ::google::protobuf::internal::WireFormat::SerializeUnknownFields(
        _internal_metadata_.unknown_fields(), output);
  }
  // @@protoc_insertion_point(serialize_end:dmi.PhysicalInventoryRequest)
}

::google::protobuf::uint8* PhysicalInventoryRequest::InternalSerializeWithCachedSizesToArray(
    ::google::protobuf::uint8* target) const {
  // @@protoc_insertion_point(serialize_to_array_start:dmi.PhysicalInventoryRequest)
  ::google::protobuf::uint32 cached_has_bits = 0;
  (void) cached_has_bits;

  // .dmi.Uuid device_uuid = 1;
  if (this->has_device_uuid()) {
    target = ::google::protobuf::internal::WireFormatLite::
      InternalWriteMessageToArray(
        1, HasBitSetters::device_uuid(this), target);
  }

  if (_internal_metadata_.have_unknown_fields()) {
    target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray(
        _internal_metadata_.unknown_fields(), target);
  }
  // @@protoc_insertion_point(serialize_to_array_end:dmi.PhysicalInventoryRequest)
  return target;
}

size_t PhysicalInventoryRequest::ByteSizeLong() const {
// @@protoc_insertion_point(message_byte_size_start:dmi.PhysicalInventoryRequest)
  size_t total_size = 0;

  if (_internal_metadata_.have_unknown_fields()) {
    total_size +=
      ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize(
        _internal_metadata_.unknown_fields());
  }
  ::google::protobuf::uint32 cached_has_bits = 0;
  // Prevent compiler warnings about cached_has_bits being unused
  (void) cached_has_bits;

  // .dmi.Uuid device_uuid = 1;
  if (this->has_device_uuid()) {
    total_size += 1 +
      ::google::protobuf::internal::WireFormatLite::MessageSize(
        *device_uuid_);
  }

  int cached_size = ::google::protobuf::internal::ToCachedSize(total_size);
  SetCachedSize(cached_size);
  return total_size;
}

void PhysicalInventoryRequest::MergeFrom(const ::google::protobuf::Message& from) {
// @@protoc_insertion_point(generalized_merge_from_start:dmi.PhysicalInventoryRequest)
  GOOGLE_DCHECK_NE(&from, this);
  const PhysicalInventoryRequest* source =
      ::google::protobuf::DynamicCastToGenerated<PhysicalInventoryRequest>(
          &from);
  if (source == nullptr) {
  // @@protoc_insertion_point(generalized_merge_from_cast_fail:dmi.PhysicalInventoryRequest)
    ::google::protobuf::internal::ReflectionOps::Merge(from, this);
  } else {
  // @@protoc_insertion_point(generalized_merge_from_cast_success:dmi.PhysicalInventoryRequest)
    MergeFrom(*source);
  }
}

void PhysicalInventoryRequest::MergeFrom(const PhysicalInventoryRequest& from) {
// @@protoc_insertion_point(class_specific_merge_from_start:dmi.PhysicalInventoryRequest)
  GOOGLE_DCHECK_NE(&from, this);
  _internal_metadata_.MergeFrom(from._internal_metadata_);
  ::google::protobuf::uint32 cached_has_bits = 0;
  (void) cached_has_bits;

  if (from.has_device_uuid()) {
    mutable_device_uuid()->::dmi::Uuid::MergeFrom(from.device_uuid());
  }
}

void PhysicalInventoryRequest::CopyFrom(const ::google::protobuf::Message& from) {
// @@protoc_insertion_point(generalized_copy_from_start:dmi.PhysicalInventoryRequest)
  if (&from == this) return;
  Clear();
  MergeFrom(from);
}

void PhysicalInventoryRequest::CopyFrom(const PhysicalInventoryRequest& from) {
// @@protoc_insertion_point(class_specific_copy_from_start:dmi.PhysicalInventoryRequest)
  if (&from == this) return;
  Clear();
  MergeFrom(from);
}

bool PhysicalInventoryRequest::IsInitialized() const {
  return true;
}

void PhysicalInventoryRequest::Swap(PhysicalInventoryRequest* other) {
  if (other == this) return;
  InternalSwap(other);
}
void PhysicalInventoryRequest::InternalSwap(PhysicalInventoryRequest* other) {
  using std::swap;
  _internal_metadata_.Swap(&other->_internal_metadata_);
  swap(device_uuid_, other->device_uuid_);
}

::google::protobuf::Metadata PhysicalInventoryRequest::GetMetadata() const {
  ::google::protobuf::internal::AssignDescriptors(&::assign_descriptors_table_dmi_2fhw_5fmanagement_5fservice_2eproto);
  return ::file_level_metadata_dmi_2fhw_5fmanagement_5fservice_2eproto[kIndexInFileMessages];
}


// ===================================================================

void PhysicalInventoryResponse::InitAsDefaultInstance() {
  ::dmi::_PhysicalInventoryResponse_default_instance_._instance.get_mutable()->inventory_ = const_cast< ::dmi::Hardware*>(
      ::dmi::Hardware::internal_default_instance());
}
class PhysicalInventoryResponse::HasBitSetters {
 public:
  static const ::dmi::Hardware& inventory(const PhysicalInventoryResponse* msg);
};

const ::dmi::Hardware&
PhysicalInventoryResponse::HasBitSetters::inventory(const PhysicalInventoryResponse* msg) {
  return *msg->inventory_;
}
void PhysicalInventoryResponse::clear_inventory() {
  if (GetArenaNoVirtual() == nullptr && inventory_ != nullptr) {
    delete inventory_;
  }
  inventory_ = nullptr;
}
#if !defined(_MSC_VER) || _MSC_VER >= 1900
const int PhysicalInventoryResponse::kStatusFieldNumber;
const int PhysicalInventoryResponse::kReasonFieldNumber;
const int PhysicalInventoryResponse::kInventoryFieldNumber;
const int PhysicalInventoryResponse::kReasonDetailFieldNumber;
#endif  // !defined(_MSC_VER) || _MSC_VER >= 1900

PhysicalInventoryResponse::PhysicalInventoryResponse()
  : ::google::protobuf::Message(), _internal_metadata_(nullptr) {
  SharedCtor();
  // @@protoc_insertion_point(constructor:dmi.PhysicalInventoryResponse)
}
PhysicalInventoryResponse::PhysicalInventoryResponse(const PhysicalInventoryResponse& from)
  : ::google::protobuf::Message(),
      _internal_metadata_(nullptr) {
  _internal_metadata_.MergeFrom(from._internal_metadata_);
  reason_detail_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
  if (from.reason_detail().size() > 0) {
    reason_detail_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.reason_detail_);
  }
  if (from.has_inventory()) {
    inventory_ = new ::dmi::Hardware(*from.inventory_);
  } else {
    inventory_ = nullptr;
  }
  ::memcpy(&status_, &from.status_,
    static_cast<size_t>(reinterpret_cast<char*>(&reason_) -
    reinterpret_cast<char*>(&status_)) + sizeof(reason_));
  // @@protoc_insertion_point(copy_constructor:dmi.PhysicalInventoryResponse)
}

void PhysicalInventoryResponse::SharedCtor() {
  ::google::protobuf::internal::InitSCC(
      &scc_info_PhysicalInventoryResponse_dmi_2fhw_5fmanagement_5fservice_2eproto.base);
  reason_detail_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
  ::memset(&inventory_, 0, static_cast<size_t>(
      reinterpret_cast<char*>(&reason_) -
      reinterpret_cast<char*>(&inventory_)) + sizeof(reason_));
}

PhysicalInventoryResponse::~PhysicalInventoryResponse() {
  // @@protoc_insertion_point(destructor:dmi.PhysicalInventoryResponse)
  SharedDtor();
}

void PhysicalInventoryResponse::SharedDtor() {
  reason_detail_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
  if (this != internal_default_instance()) delete inventory_;
}

void PhysicalInventoryResponse::SetCachedSize(int size) const {
  _cached_size_.Set(size);
}
const PhysicalInventoryResponse& PhysicalInventoryResponse::default_instance() {
  ::google::protobuf::internal::InitSCC(&::scc_info_PhysicalInventoryResponse_dmi_2fhw_5fmanagement_5fservice_2eproto.base);
  return *internal_default_instance();
}


void PhysicalInventoryResponse::Clear() {
// @@protoc_insertion_point(message_clear_start:dmi.PhysicalInventoryResponse)
  ::google::protobuf::uint32 cached_has_bits = 0;
  // Prevent compiler warnings about cached_has_bits being unused
  (void) cached_has_bits;

  reason_detail_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
  if (GetArenaNoVirtual() == nullptr && inventory_ != nullptr) {
    delete inventory_;
  }
  inventory_ = nullptr;
  ::memset(&status_, 0, static_cast<size_t>(
      reinterpret_cast<char*>(&reason_) -
      reinterpret_cast<char*>(&status_)) + sizeof(reason_));
  _internal_metadata_.Clear();
}

#if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER
const char* PhysicalInventoryResponse::_InternalParse(const char* begin, const char* end, void* object,
                  ::google::protobuf::internal::ParseContext* ctx) {
  auto msg = static_cast<PhysicalInventoryResponse*>(object);
  ::google::protobuf::int32 size; (void)size;
  int depth; (void)depth;
  ::google::protobuf::uint32 tag;
  ::google::protobuf::internal::ParseFunc parser_till_end; (void)parser_till_end;
  auto ptr = begin;
  while (ptr < end) {
    ptr = ::google::protobuf::io::Parse32(ptr, &tag);
    GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
    switch (tag >> 3) {
      // .dmi.Status status = 1;
      case 1: {
        if (static_cast<::google::protobuf::uint8>(tag) != 8) goto handle_unusual;
        ::google::protobuf::uint64 val = ::google::protobuf::internal::ReadVarint(&ptr);
        msg->set_status(static_cast<::dmi::Status>(val));
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
        break;
      }
      // .dmi.PhysicalInventoryResponse.Reason reason = 2;
      case 2: {
        if (static_cast<::google::protobuf::uint8>(tag) != 16) goto handle_unusual;
        ::google::protobuf::uint64 val = ::google::protobuf::internal::ReadVarint(&ptr);
        msg->set_reason(static_cast<::dmi::PhysicalInventoryResponse_Reason>(val));
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
        break;
      }
      // .dmi.Hardware inventory = 3;
      case 3: {
        if (static_cast<::google::protobuf::uint8>(tag) != 26) goto handle_unusual;
        ptr = ::google::protobuf::io::ReadSize(ptr, &size);
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
        parser_till_end = ::dmi::Hardware::_InternalParse;
        object = msg->mutable_inventory();
        if (size > end - ptr) goto len_delim_till_end;
        ptr += size;
        GOOGLE_PROTOBUF_PARSER_ASSERT(ctx->ParseExactRange(
            {parser_till_end, object}, ptr - size, ptr));
        break;
      }
      // string reason_detail = 4;
      case 4: {
        if (static_cast<::google::protobuf::uint8>(tag) != 34) goto handle_unusual;
        ptr = ::google::protobuf::io::ReadSize(ptr, &size);
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
        ctx->extra_parse_data().SetFieldName("dmi.PhysicalInventoryResponse.reason_detail");
        object = msg->mutable_reason_detail();
        if (size > end - ptr + ::google::protobuf::internal::ParseContext::kSlopBytes) {
          parser_till_end = ::google::protobuf::internal::GreedyStringParserUTF8;
          goto string_till_end;
        }
        GOOGLE_PROTOBUF_PARSER_ASSERT(::google::protobuf::internal::StringCheckUTF8(ptr, size, ctx));
        ::google::protobuf::internal::InlineGreedyStringParser(object, ptr, size, ctx);
        ptr += size;
        break;
      }
      default: {
      handle_unusual:
        if ((tag & 7) == 4 || tag == 0) {
          ctx->EndGroup(tag);
          return ptr;
        }
        auto res = UnknownFieldParse(tag, {_InternalParse, msg},
          ptr, end, msg->_internal_metadata_.mutable_unknown_fields(), ctx);
        ptr = res.first;
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr);
        if (res.second) return ptr;
      }
    }  // switch
  }  // while
  return ptr;
string_till_end:
  static_cast<::std::string*>(object)->clear();
  static_cast<::std::string*>(object)->reserve(size);
  goto len_delim_till_end;
len_delim_till_end:
  return ctx->StoreAndTailCall(ptr, end, {_InternalParse, msg},
                               {parser_till_end, object}, size);
}
#else  // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER
bool PhysicalInventoryResponse::MergePartialFromCodedStream(
    ::google::protobuf::io::CodedInputStream* input) {
#define DO_(EXPRESSION) if (!PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure
  ::google::protobuf::uint32 tag;
  // @@protoc_insertion_point(parse_start:dmi.PhysicalInventoryResponse)
  for (;;) {
    ::std::pair<::google::protobuf::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u);
    tag = p.first;
    if (!p.second) goto handle_unusual;
    switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) {
      // .dmi.Status status = 1;
      case 1: {
        if (static_cast< ::google::protobuf::uint8>(tag) == (8 & 0xFF)) {
          int value = 0;
          DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive<
                   int, ::google::protobuf::internal::WireFormatLite::TYPE_ENUM>(
                 input, &value)));
          set_status(static_cast< ::dmi::Status >(value));
        } else {
          goto handle_unusual;
        }
        break;
      }

      // .dmi.PhysicalInventoryResponse.Reason reason = 2;
      case 2: {
        if (static_cast< ::google::protobuf::uint8>(tag) == (16 & 0xFF)) {
          int value = 0;
          DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive<
                   int, ::google::protobuf::internal::WireFormatLite::TYPE_ENUM>(
                 input, &value)));
          set_reason(static_cast< ::dmi::PhysicalInventoryResponse_Reason >(value));
        } else {
          goto handle_unusual;
        }
        break;
      }

      // .dmi.Hardware inventory = 3;
      case 3: {
        if (static_cast< ::google::protobuf::uint8>(tag) == (26 & 0xFF)) {
          DO_(::google::protobuf::internal::WireFormatLite::ReadMessage(
               input, mutable_inventory()));
        } else {
          goto handle_unusual;
        }
        break;
      }

      // string reason_detail = 4;
      case 4: {
        if (static_cast< ::google::protobuf::uint8>(tag) == (34 & 0xFF)) {
          DO_(::google::protobuf::internal::WireFormatLite::ReadString(
                input, this->mutable_reason_detail()));
          DO_(::google::protobuf::internal::WireFormatLite::VerifyUtf8String(
            this->reason_detail().data(), static_cast<int>(this->reason_detail().length()),
            ::google::protobuf::internal::WireFormatLite::PARSE,
            "dmi.PhysicalInventoryResponse.reason_detail"));
        } else {
          goto handle_unusual;
        }
        break;
      }

      default: {
      handle_unusual:
        if (tag == 0) {
          goto success;
        }
        DO_(::google::protobuf::internal::WireFormat::SkipField(
              input, tag, _internal_metadata_.mutable_unknown_fields()));
        break;
      }
    }
  }
success:
  // @@protoc_insertion_point(parse_success:dmi.PhysicalInventoryResponse)
  return true;
failure:
  // @@protoc_insertion_point(parse_failure:dmi.PhysicalInventoryResponse)
  return false;
#undef DO_
}
#endif  // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER

void PhysicalInventoryResponse::SerializeWithCachedSizes(
    ::google::protobuf::io::CodedOutputStream* output) const {
  // @@protoc_insertion_point(serialize_start:dmi.PhysicalInventoryResponse)
  ::google::protobuf::uint32 cached_has_bits = 0;
  (void) cached_has_bits;

  // .dmi.Status status = 1;
  if (this->status() != 0) {
    ::google::protobuf::internal::WireFormatLite::WriteEnum(
      1, this->status(), output);
  }

  // .dmi.PhysicalInventoryResponse.Reason reason = 2;
  if (this->reason() != 0) {
    ::google::protobuf::internal::WireFormatLite::WriteEnum(
      2, this->reason(), output);
  }

  // .dmi.Hardware inventory = 3;
  if (this->has_inventory()) {
    ::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray(
      3, HasBitSetters::inventory(this), output);
  }

  // string reason_detail = 4;
  if (this->reason_detail().size() > 0) {
    ::google::protobuf::internal::WireFormatLite::VerifyUtf8String(
      this->reason_detail().data(), static_cast<int>(this->reason_detail().length()),
      ::google::protobuf::internal::WireFormatLite::SERIALIZE,
      "dmi.PhysicalInventoryResponse.reason_detail");
    ::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased(
      4, this->reason_detail(), output);
  }

  if (_internal_metadata_.have_unknown_fields()) {
    ::google::protobuf::internal::WireFormat::SerializeUnknownFields(
        _internal_metadata_.unknown_fields(), output);
  }
  // @@protoc_insertion_point(serialize_end:dmi.PhysicalInventoryResponse)
}

::google::protobuf::uint8* PhysicalInventoryResponse::InternalSerializeWithCachedSizesToArray(
    ::google::protobuf::uint8* target) const {
  // @@protoc_insertion_point(serialize_to_array_start:dmi.PhysicalInventoryResponse)
  ::google::protobuf::uint32 cached_has_bits = 0;
  (void) cached_has_bits;

  // .dmi.Status status = 1;
  if (this->status() != 0) {
    target = ::google::protobuf::internal::WireFormatLite::WriteEnumToArray(
      1, this->status(), target);
  }

  // .dmi.PhysicalInventoryResponse.Reason reason = 2;
  if (this->reason() != 0) {
    target = ::google::protobuf::internal::WireFormatLite::WriteEnumToArray(
      2, this->reason(), target);
  }

  // .dmi.Hardware inventory = 3;
  if (this->has_inventory()) {
    target = ::google::protobuf::internal::WireFormatLite::
      InternalWriteMessageToArray(
        3, HasBitSetters::inventory(this), target);
  }

  // string reason_detail = 4;
  if (this->reason_detail().size() > 0) {
    ::google::protobuf::internal::WireFormatLite::VerifyUtf8String(
      this->reason_detail().data(), static_cast<int>(this->reason_detail().length()),
      ::google::protobuf::internal::WireFormatLite::SERIALIZE,
      "dmi.PhysicalInventoryResponse.reason_detail");
    target =
      ::google::protobuf::internal::WireFormatLite::WriteStringToArray(
        4, this->reason_detail(), target);
  }

  if (_internal_metadata_.have_unknown_fields()) {
    target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray(
        _internal_metadata_.unknown_fields(), target);
  }
  // @@protoc_insertion_point(serialize_to_array_end:dmi.PhysicalInventoryResponse)
  return target;
}

size_t PhysicalInventoryResponse::ByteSizeLong() const {
// @@protoc_insertion_point(message_byte_size_start:dmi.PhysicalInventoryResponse)
  size_t total_size = 0;

  if (_internal_metadata_.have_unknown_fields()) {
    total_size +=
      ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize(
        _internal_metadata_.unknown_fields());
  }
  ::google::protobuf::uint32 cached_has_bits = 0;
  // Prevent compiler warnings about cached_has_bits being unused
  (void) cached_has_bits;

  // string reason_detail = 4;
  if (this->reason_detail().size() > 0) {
    total_size += 1 +
      ::google::protobuf::internal::WireFormatLite::StringSize(
        this->reason_detail());
  }

  // .dmi.Hardware inventory = 3;
  if (this->has_inventory()) {
    total_size += 1 +
      ::google::protobuf::internal::WireFormatLite::MessageSize(
        *inventory_);
  }

  // .dmi.Status status = 1;
  if (this->status() != 0) {
    total_size += 1 +
      ::google::protobuf::internal::WireFormatLite::EnumSize(this->status());
  }

  // .dmi.PhysicalInventoryResponse.Reason reason = 2;
  if (this->reason() != 0) {
    total_size += 1 +
      ::google::protobuf::internal::WireFormatLite::EnumSize(this->reason());
  }

  int cached_size = ::google::protobuf::internal::ToCachedSize(total_size);
  SetCachedSize(cached_size);
  return total_size;
}

void PhysicalInventoryResponse::MergeFrom(const ::google::protobuf::Message& from) {
// @@protoc_insertion_point(generalized_merge_from_start:dmi.PhysicalInventoryResponse)
  GOOGLE_DCHECK_NE(&from, this);
  const PhysicalInventoryResponse* source =
      ::google::protobuf::DynamicCastToGenerated<PhysicalInventoryResponse>(
          &from);
  if (source == nullptr) {
  // @@protoc_insertion_point(generalized_merge_from_cast_fail:dmi.PhysicalInventoryResponse)
    ::google::protobuf::internal::ReflectionOps::Merge(from, this);
  } else {
  // @@protoc_insertion_point(generalized_merge_from_cast_success:dmi.PhysicalInventoryResponse)
    MergeFrom(*source);
  }
}

void PhysicalInventoryResponse::MergeFrom(const PhysicalInventoryResponse& from) {
// @@protoc_insertion_point(class_specific_merge_from_start:dmi.PhysicalInventoryResponse)
  GOOGLE_DCHECK_NE(&from, this);
  _internal_metadata_.MergeFrom(from._internal_metadata_);
  ::google::protobuf::uint32 cached_has_bits = 0;
  (void) cached_has_bits;

  if (from.reason_detail().size() > 0) {

    reason_detail_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.reason_detail_);
  }
  if (from.has_inventory()) {
    mutable_inventory()->::dmi::Hardware::MergeFrom(from.inventory());
  }
  if (from.status() != 0) {
    set_status(from.status());
  }
  if (from.reason() != 0) {
    set_reason(from.reason());
  }
}

void PhysicalInventoryResponse::CopyFrom(const ::google::protobuf::Message& from) {
// @@protoc_insertion_point(generalized_copy_from_start:dmi.PhysicalInventoryResponse)
  if (&from == this) return;
  Clear();
  MergeFrom(from);
}

void PhysicalInventoryResponse::CopyFrom(const PhysicalInventoryResponse& from) {
// @@protoc_insertion_point(class_specific_copy_from_start:dmi.PhysicalInventoryResponse)
  if (&from == this) return;
  Clear();
  MergeFrom(from);
}

bool PhysicalInventoryResponse::IsInitialized() const {
  return true;
}

void PhysicalInventoryResponse::Swap(PhysicalInventoryResponse* other) {
  if (other == this) return;
  InternalSwap(other);
}
void PhysicalInventoryResponse::InternalSwap(PhysicalInventoryResponse* other) {
  using std::swap;
  _internal_metadata_.Swap(&other->_internal_metadata_);
  reason_detail_.Swap(&other->reason_detail_, &::google::protobuf::internal::GetEmptyStringAlreadyInited(),
    GetArenaNoVirtual());
  swap(inventory_, other->inventory_);
  swap(status_, other->status_);
  swap(reason_, other->reason_);
}

::google::protobuf::Metadata PhysicalInventoryResponse::GetMetadata() const {
  ::google::protobuf::internal::AssignDescriptors(&::assign_descriptors_table_dmi_2fhw_5fmanagement_5fservice_2eproto);
  return ::file_level_metadata_dmi_2fhw_5fmanagement_5fservice_2eproto[kIndexInFileMessages];
}


// ===================================================================

void HWComponentInfoGetRequest::InitAsDefaultInstance() {
  ::dmi::_HWComponentInfoGetRequest_default_instance_._instance.get_mutable()->device_uuid_ = const_cast< ::dmi::Uuid*>(
      ::dmi::Uuid::internal_default_instance());
  ::dmi::_HWComponentInfoGetRequest_default_instance_._instance.get_mutable()->component_uuid_ = const_cast< ::dmi::Uuid*>(
      ::dmi::Uuid::internal_default_instance());
}
class HWComponentInfoGetRequest::HasBitSetters {
 public:
  static const ::dmi::Uuid& device_uuid(const HWComponentInfoGetRequest* msg);
  static const ::dmi::Uuid& component_uuid(const HWComponentInfoGetRequest* msg);
};

const ::dmi::Uuid&
HWComponentInfoGetRequest::HasBitSetters::device_uuid(const HWComponentInfoGetRequest* msg) {
  return *msg->device_uuid_;
}
const ::dmi::Uuid&
HWComponentInfoGetRequest::HasBitSetters::component_uuid(const HWComponentInfoGetRequest* msg) {
  return *msg->component_uuid_;
}
void HWComponentInfoGetRequest::clear_device_uuid() {
  if (GetArenaNoVirtual() == nullptr && device_uuid_ != nullptr) {
    delete device_uuid_;
  }
  device_uuid_ = nullptr;
}
void HWComponentInfoGetRequest::clear_component_uuid() {
  if (GetArenaNoVirtual() == nullptr && component_uuid_ != nullptr) {
    delete component_uuid_;
  }
  component_uuid_ = nullptr;
}
#if !defined(_MSC_VER) || _MSC_VER >= 1900
const int HWComponentInfoGetRequest::kDeviceUuidFieldNumber;
const int HWComponentInfoGetRequest::kComponentUuidFieldNumber;
const int HWComponentInfoGetRequest::kComponentNameFieldNumber;
#endif  // !defined(_MSC_VER) || _MSC_VER >= 1900

HWComponentInfoGetRequest::HWComponentInfoGetRequest()
  : ::google::protobuf::Message(), _internal_metadata_(nullptr) {
  SharedCtor();
  // @@protoc_insertion_point(constructor:dmi.HWComponentInfoGetRequest)
}
HWComponentInfoGetRequest::HWComponentInfoGetRequest(const HWComponentInfoGetRequest& from)
  : ::google::protobuf::Message(),
      _internal_metadata_(nullptr) {
  _internal_metadata_.MergeFrom(from._internal_metadata_);
  component_name_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
  if (from.component_name().size() > 0) {
    component_name_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.component_name_);
  }
  if (from.has_device_uuid()) {
    device_uuid_ = new ::dmi::Uuid(*from.device_uuid_);
  } else {
    device_uuid_ = nullptr;
  }
  if (from.has_component_uuid()) {
    component_uuid_ = new ::dmi::Uuid(*from.component_uuid_);
  } else {
    component_uuid_ = nullptr;
  }
  // @@protoc_insertion_point(copy_constructor:dmi.HWComponentInfoGetRequest)
}

void HWComponentInfoGetRequest::SharedCtor() {
  ::google::protobuf::internal::InitSCC(
      &scc_info_HWComponentInfoGetRequest_dmi_2fhw_5fmanagement_5fservice_2eproto.base);
  component_name_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
  ::memset(&device_uuid_, 0, static_cast<size_t>(
      reinterpret_cast<char*>(&component_uuid_) -
      reinterpret_cast<char*>(&device_uuid_)) + sizeof(component_uuid_));
}

HWComponentInfoGetRequest::~HWComponentInfoGetRequest() {
  // @@protoc_insertion_point(destructor:dmi.HWComponentInfoGetRequest)
  SharedDtor();
}

void HWComponentInfoGetRequest::SharedDtor() {
  component_name_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
  if (this != internal_default_instance()) delete device_uuid_;
  if (this != internal_default_instance()) delete component_uuid_;
}

void HWComponentInfoGetRequest::SetCachedSize(int size) const {
  _cached_size_.Set(size);
}
const HWComponentInfoGetRequest& HWComponentInfoGetRequest::default_instance() {
  ::google::protobuf::internal::InitSCC(&::scc_info_HWComponentInfoGetRequest_dmi_2fhw_5fmanagement_5fservice_2eproto.base);
  return *internal_default_instance();
}


void HWComponentInfoGetRequest::Clear() {
// @@protoc_insertion_point(message_clear_start:dmi.HWComponentInfoGetRequest)
  ::google::protobuf::uint32 cached_has_bits = 0;
  // Prevent compiler warnings about cached_has_bits being unused
  (void) cached_has_bits;

  component_name_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
  if (GetArenaNoVirtual() == nullptr && device_uuid_ != nullptr) {
    delete device_uuid_;
  }
  device_uuid_ = nullptr;
  if (GetArenaNoVirtual() == nullptr && component_uuid_ != nullptr) {
    delete component_uuid_;
  }
  component_uuid_ = nullptr;
  _internal_metadata_.Clear();
}

#if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER
const char* HWComponentInfoGetRequest::_InternalParse(const char* begin, const char* end, void* object,
                  ::google::protobuf::internal::ParseContext* ctx) {
  auto msg = static_cast<HWComponentInfoGetRequest*>(object);
  ::google::protobuf::int32 size; (void)size;
  int depth; (void)depth;
  ::google::protobuf::uint32 tag;
  ::google::protobuf::internal::ParseFunc parser_till_end; (void)parser_till_end;
  auto ptr = begin;
  while (ptr < end) {
    ptr = ::google::protobuf::io::Parse32(ptr, &tag);
    GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
    switch (tag >> 3) {
      // .dmi.Uuid device_uuid = 1;
      case 1: {
        if (static_cast<::google::protobuf::uint8>(tag) != 10) goto handle_unusual;
        ptr = ::google::protobuf::io::ReadSize(ptr, &size);
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
        parser_till_end = ::dmi::Uuid::_InternalParse;
        object = msg->mutable_device_uuid();
        if (size > end - ptr) goto len_delim_till_end;
        ptr += size;
        GOOGLE_PROTOBUF_PARSER_ASSERT(ctx->ParseExactRange(
            {parser_till_end, object}, ptr - size, ptr));
        break;
      }
      // .dmi.Uuid component_uuid = 2;
      case 2: {
        if (static_cast<::google::protobuf::uint8>(tag) != 18) goto handle_unusual;
        ptr = ::google::protobuf::io::ReadSize(ptr, &size);
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
        parser_till_end = ::dmi::Uuid::_InternalParse;
        object = msg->mutable_component_uuid();
        if (size > end - ptr) goto len_delim_till_end;
        ptr += size;
        GOOGLE_PROTOBUF_PARSER_ASSERT(ctx->ParseExactRange(
            {parser_till_end, object}, ptr - size, ptr));
        break;
      }
      // string component_name = 3;
      case 3: {
        if (static_cast<::google::protobuf::uint8>(tag) != 26) goto handle_unusual;
        ptr = ::google::protobuf::io::ReadSize(ptr, &size);
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
        ctx->extra_parse_data().SetFieldName("dmi.HWComponentInfoGetRequest.component_name");
        object = msg->mutable_component_name();
        if (size > end - ptr + ::google::protobuf::internal::ParseContext::kSlopBytes) {
          parser_till_end = ::google::protobuf::internal::GreedyStringParserUTF8;
          goto string_till_end;
        }
        GOOGLE_PROTOBUF_PARSER_ASSERT(::google::protobuf::internal::StringCheckUTF8(ptr, size, ctx));
        ::google::protobuf::internal::InlineGreedyStringParser(object, ptr, size, ctx);
        ptr += size;
        break;
      }
      default: {
      handle_unusual:
        if ((tag & 7) == 4 || tag == 0) {
          ctx->EndGroup(tag);
          return ptr;
        }
        auto res = UnknownFieldParse(tag, {_InternalParse, msg},
          ptr, end, msg->_internal_metadata_.mutable_unknown_fields(), ctx);
        ptr = res.first;
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr);
        if (res.second) return ptr;
      }
    }  // switch
  }  // while
  return ptr;
string_till_end:
  static_cast<::std::string*>(object)->clear();
  static_cast<::std::string*>(object)->reserve(size);
  goto len_delim_till_end;
len_delim_till_end:
  return ctx->StoreAndTailCall(ptr, end, {_InternalParse, msg},
                               {parser_till_end, object}, size);
}
#else  // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER
bool HWComponentInfoGetRequest::MergePartialFromCodedStream(
    ::google::protobuf::io::CodedInputStream* input) {
#define DO_(EXPRESSION) if (!PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure
  ::google::protobuf::uint32 tag;
  // @@protoc_insertion_point(parse_start:dmi.HWComponentInfoGetRequest)
  for (;;) {
    ::std::pair<::google::protobuf::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u);
    tag = p.first;
    if (!p.second) goto handle_unusual;
    switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) {
      // .dmi.Uuid device_uuid = 1;
      case 1: {
        if (static_cast< ::google::protobuf::uint8>(tag) == (10 & 0xFF)) {
          DO_(::google::protobuf::internal::WireFormatLite::ReadMessage(
               input, mutable_device_uuid()));
        } else {
          goto handle_unusual;
        }
        break;
      }

      // .dmi.Uuid component_uuid = 2;
      case 2: {
        if (static_cast< ::google::protobuf::uint8>(tag) == (18 & 0xFF)) {
          DO_(::google::protobuf::internal::WireFormatLite::ReadMessage(
               input, mutable_component_uuid()));
        } else {
          goto handle_unusual;
        }
        break;
      }

      // string component_name = 3;
      case 3: {
        if (static_cast< ::google::protobuf::uint8>(tag) == (26 & 0xFF)) {
          DO_(::google::protobuf::internal::WireFormatLite::ReadString(
                input, this->mutable_component_name()));
          DO_(::google::protobuf::internal::WireFormatLite::VerifyUtf8String(
            this->component_name().data(), static_cast<int>(this->component_name().length()),
            ::google::protobuf::internal::WireFormatLite::PARSE,
            "dmi.HWComponentInfoGetRequest.component_name"));
        } else {
          goto handle_unusual;
        }
        break;
      }

      default: {
      handle_unusual:
        if (tag == 0) {
          goto success;
        }
        DO_(::google::protobuf::internal::WireFormat::SkipField(
              input, tag, _internal_metadata_.mutable_unknown_fields()));
        break;
      }
    }
  }
success:
  // @@protoc_insertion_point(parse_success:dmi.HWComponentInfoGetRequest)
  return true;
failure:
  // @@protoc_insertion_point(parse_failure:dmi.HWComponentInfoGetRequest)
  return false;
#undef DO_
}
#endif  // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER

void HWComponentInfoGetRequest::SerializeWithCachedSizes(
    ::google::protobuf::io::CodedOutputStream* output) const {
  // @@protoc_insertion_point(serialize_start:dmi.HWComponentInfoGetRequest)
  ::google::protobuf::uint32 cached_has_bits = 0;
  (void) cached_has_bits;

  // .dmi.Uuid device_uuid = 1;
  if (this->has_device_uuid()) {
    ::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray(
      1, HasBitSetters::device_uuid(this), output);
  }

  // .dmi.Uuid component_uuid = 2;
  if (this->has_component_uuid()) {
    ::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray(
      2, HasBitSetters::component_uuid(this), output);
  }

  // string component_name = 3;
  if (this->component_name().size() > 0) {
    ::google::protobuf::internal::WireFormatLite::VerifyUtf8String(
      this->component_name().data(), static_cast<int>(this->component_name().length()),
      ::google::protobuf::internal::WireFormatLite::SERIALIZE,
      "dmi.HWComponentInfoGetRequest.component_name");
    ::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased(
      3, this->component_name(), output);
  }

  if (_internal_metadata_.have_unknown_fields()) {
    ::google::protobuf::internal::WireFormat::SerializeUnknownFields(
        _internal_metadata_.unknown_fields(), output);
  }
  // @@protoc_insertion_point(serialize_end:dmi.HWComponentInfoGetRequest)
}

::google::protobuf::uint8* HWComponentInfoGetRequest::InternalSerializeWithCachedSizesToArray(
    ::google::protobuf::uint8* target) const {
  // @@protoc_insertion_point(serialize_to_array_start:dmi.HWComponentInfoGetRequest)
  ::google::protobuf::uint32 cached_has_bits = 0;
  (void) cached_has_bits;

  // .dmi.Uuid device_uuid = 1;
  if (this->has_device_uuid()) {
    target = ::google::protobuf::internal::WireFormatLite::
      InternalWriteMessageToArray(
        1, HasBitSetters::device_uuid(this), target);
  }

  // .dmi.Uuid component_uuid = 2;
  if (this->has_component_uuid()) {
    target = ::google::protobuf::internal::WireFormatLite::
      InternalWriteMessageToArray(
        2, HasBitSetters::component_uuid(this), target);
  }

  // string component_name = 3;
  if (this->component_name().size() > 0) {
    ::google::protobuf::internal::WireFormatLite::VerifyUtf8String(
      this->component_name().data(), static_cast<int>(this->component_name().length()),
      ::google::protobuf::internal::WireFormatLite::SERIALIZE,
      "dmi.HWComponentInfoGetRequest.component_name");
    target =
      ::google::protobuf::internal::WireFormatLite::WriteStringToArray(
        3, this->component_name(), target);
  }

  if (_internal_metadata_.have_unknown_fields()) {
    target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray(
        _internal_metadata_.unknown_fields(), target);
  }
  // @@protoc_insertion_point(serialize_to_array_end:dmi.HWComponentInfoGetRequest)
  return target;
}

size_t HWComponentInfoGetRequest::ByteSizeLong() const {
// @@protoc_insertion_point(message_byte_size_start:dmi.HWComponentInfoGetRequest)
  size_t total_size = 0;

  if (_internal_metadata_.have_unknown_fields()) {
    total_size +=
      ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize(
        _internal_metadata_.unknown_fields());
  }
  ::google::protobuf::uint32 cached_has_bits = 0;
  // Prevent compiler warnings about cached_has_bits being unused
  (void) cached_has_bits;

  // string component_name = 3;
  if (this->component_name().size() > 0) {
    total_size += 1 +
      ::google::protobuf::internal::WireFormatLite::StringSize(
        this->component_name());
  }

  // .dmi.Uuid device_uuid = 1;
  if (this->has_device_uuid()) {
    total_size += 1 +
      ::google::protobuf::internal::WireFormatLite::MessageSize(
        *device_uuid_);
  }

  // .dmi.Uuid component_uuid = 2;
  if (this->has_component_uuid()) {
    total_size += 1 +
      ::google::protobuf::internal::WireFormatLite::MessageSize(
        *component_uuid_);
  }

  int cached_size = ::google::protobuf::internal::ToCachedSize(total_size);
  SetCachedSize(cached_size);
  return total_size;
}

void HWComponentInfoGetRequest::MergeFrom(const ::google::protobuf::Message& from) {
// @@protoc_insertion_point(generalized_merge_from_start:dmi.HWComponentInfoGetRequest)
  GOOGLE_DCHECK_NE(&from, this);
  const HWComponentInfoGetRequest* source =
      ::google::protobuf::DynamicCastToGenerated<HWComponentInfoGetRequest>(
          &from);
  if (source == nullptr) {
  // @@protoc_insertion_point(generalized_merge_from_cast_fail:dmi.HWComponentInfoGetRequest)
    ::google::protobuf::internal::ReflectionOps::Merge(from, this);
  } else {
  // @@protoc_insertion_point(generalized_merge_from_cast_success:dmi.HWComponentInfoGetRequest)
    MergeFrom(*source);
  }
}

void HWComponentInfoGetRequest::MergeFrom(const HWComponentInfoGetRequest& from) {
// @@protoc_insertion_point(class_specific_merge_from_start:dmi.HWComponentInfoGetRequest)
  GOOGLE_DCHECK_NE(&from, this);
  _internal_metadata_.MergeFrom(from._internal_metadata_);
  ::google::protobuf::uint32 cached_has_bits = 0;
  (void) cached_has_bits;

  if (from.component_name().size() > 0) {

    component_name_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.component_name_);
  }
  if (from.has_device_uuid()) {
    mutable_device_uuid()->::dmi::Uuid::MergeFrom(from.device_uuid());
  }
  if (from.has_component_uuid()) {
    mutable_component_uuid()->::dmi::Uuid::MergeFrom(from.component_uuid());
  }
}

void HWComponentInfoGetRequest::CopyFrom(const ::google::protobuf::Message& from) {
// @@protoc_insertion_point(generalized_copy_from_start:dmi.HWComponentInfoGetRequest)
  if (&from == this) return;
  Clear();
  MergeFrom(from);
}

void HWComponentInfoGetRequest::CopyFrom(const HWComponentInfoGetRequest& from) {
// @@protoc_insertion_point(class_specific_copy_from_start:dmi.HWComponentInfoGetRequest)
  if (&from == this) return;
  Clear();
  MergeFrom(from);
}

bool HWComponentInfoGetRequest::IsInitialized() const {
  return true;
}

void HWComponentInfoGetRequest::Swap(HWComponentInfoGetRequest* other) {
  if (other == this) return;
  InternalSwap(other);
}
void HWComponentInfoGetRequest::InternalSwap(HWComponentInfoGetRequest* other) {
  using std::swap;
  _internal_metadata_.Swap(&other->_internal_metadata_);
  component_name_.Swap(&other->component_name_, &::google::protobuf::internal::GetEmptyStringAlreadyInited(),
    GetArenaNoVirtual());
  swap(device_uuid_, other->device_uuid_);
  swap(component_uuid_, other->component_uuid_);
}

::google::protobuf::Metadata HWComponentInfoGetRequest::GetMetadata() const {
  ::google::protobuf::internal::AssignDescriptors(&::assign_descriptors_table_dmi_2fhw_5fmanagement_5fservice_2eproto);
  return ::file_level_metadata_dmi_2fhw_5fmanagement_5fservice_2eproto[kIndexInFileMessages];
}


// ===================================================================

void HWComponentInfoGetResponse::InitAsDefaultInstance() {
  ::dmi::_HWComponentInfoGetResponse_default_instance_._instance.get_mutable()->component_ = const_cast< ::dmi::Component*>(
      ::dmi::Component::internal_default_instance());
}
class HWComponentInfoGetResponse::HasBitSetters {
 public:
  static const ::dmi::Component& component(const HWComponentInfoGetResponse* msg);
};

const ::dmi::Component&
HWComponentInfoGetResponse::HasBitSetters::component(const HWComponentInfoGetResponse* msg) {
  return *msg->component_;
}
void HWComponentInfoGetResponse::clear_component() {
  if (GetArenaNoVirtual() == nullptr && component_ != nullptr) {
    delete component_;
  }
  component_ = nullptr;
}
#if !defined(_MSC_VER) || _MSC_VER >= 1900
const int HWComponentInfoGetResponse::kStatusFieldNumber;
const int HWComponentInfoGetResponse::kReasonFieldNumber;
const int HWComponentInfoGetResponse::kComponentFieldNumber;
const int HWComponentInfoGetResponse::kReasonDetailFieldNumber;
#endif  // !defined(_MSC_VER) || _MSC_VER >= 1900

HWComponentInfoGetResponse::HWComponentInfoGetResponse()
  : ::google::protobuf::Message(), _internal_metadata_(nullptr) {
  SharedCtor();
  // @@protoc_insertion_point(constructor:dmi.HWComponentInfoGetResponse)
}
HWComponentInfoGetResponse::HWComponentInfoGetResponse(const HWComponentInfoGetResponse& from)
  : ::google::protobuf::Message(),
      _internal_metadata_(nullptr) {
  _internal_metadata_.MergeFrom(from._internal_metadata_);
  reason_detail_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
  if (from.reason_detail().size() > 0) {
    reason_detail_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.reason_detail_);
  }
  if (from.has_component()) {
    component_ = new ::dmi::Component(*from.component_);
  } else {
    component_ = nullptr;
  }
  ::memcpy(&status_, &from.status_,
    static_cast<size_t>(reinterpret_cast<char*>(&reason_) -
    reinterpret_cast<char*>(&status_)) + sizeof(reason_));
  // @@protoc_insertion_point(copy_constructor:dmi.HWComponentInfoGetResponse)
}

void HWComponentInfoGetResponse::SharedCtor() {
  ::google::protobuf::internal::InitSCC(
      &scc_info_HWComponentInfoGetResponse_dmi_2fhw_5fmanagement_5fservice_2eproto.base);
  reason_detail_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
  ::memset(&component_, 0, static_cast<size_t>(
      reinterpret_cast<char*>(&reason_) -
      reinterpret_cast<char*>(&component_)) + sizeof(reason_));
}

HWComponentInfoGetResponse::~HWComponentInfoGetResponse() {
  // @@protoc_insertion_point(destructor:dmi.HWComponentInfoGetResponse)
  SharedDtor();
}

void HWComponentInfoGetResponse::SharedDtor() {
  reason_detail_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
  if (this != internal_default_instance()) delete component_;
}

void HWComponentInfoGetResponse::SetCachedSize(int size) const {
  _cached_size_.Set(size);
}
const HWComponentInfoGetResponse& HWComponentInfoGetResponse::default_instance() {
  ::google::protobuf::internal::InitSCC(&::scc_info_HWComponentInfoGetResponse_dmi_2fhw_5fmanagement_5fservice_2eproto.base);
  return *internal_default_instance();
}


void HWComponentInfoGetResponse::Clear() {
// @@protoc_insertion_point(message_clear_start:dmi.HWComponentInfoGetResponse)
  ::google::protobuf::uint32 cached_has_bits = 0;
  // Prevent compiler warnings about cached_has_bits being unused
  (void) cached_has_bits;

  reason_detail_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
  if (GetArenaNoVirtual() == nullptr && component_ != nullptr) {
    delete component_;
  }
  component_ = nullptr;
  ::memset(&status_, 0, static_cast<size_t>(
      reinterpret_cast<char*>(&reason_) -
      reinterpret_cast<char*>(&status_)) + sizeof(reason_));
  _internal_metadata_.Clear();
}

#if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER
const char* HWComponentInfoGetResponse::_InternalParse(const char* begin, const char* end, void* object,
                  ::google::protobuf::internal::ParseContext* ctx) {
  auto msg = static_cast<HWComponentInfoGetResponse*>(object);
  ::google::protobuf::int32 size; (void)size;
  int depth; (void)depth;
  ::google::protobuf::uint32 tag;
  ::google::protobuf::internal::ParseFunc parser_till_end; (void)parser_till_end;
  auto ptr = begin;
  while (ptr < end) {
    ptr = ::google::protobuf::io::Parse32(ptr, &tag);
    GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
    switch (tag >> 3) {
      // .dmi.Status status = 1;
      case 1: {
        if (static_cast<::google::protobuf::uint8>(tag) != 8) goto handle_unusual;
        ::google::protobuf::uint64 val = ::google::protobuf::internal::ReadVarint(&ptr);
        msg->set_status(static_cast<::dmi::Status>(val));
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
        break;
      }
      // .dmi.HWComponentInfoGetResponse.Reason reason = 2;
      case 2: {
        if (static_cast<::google::protobuf::uint8>(tag) != 16) goto handle_unusual;
        ::google::protobuf::uint64 val = ::google::protobuf::internal::ReadVarint(&ptr);
        msg->set_reason(static_cast<::dmi::HWComponentInfoGetResponse_Reason>(val));
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
        break;
      }
      // .dmi.Component component = 3;
      case 3: {
        if (static_cast<::google::protobuf::uint8>(tag) != 26) goto handle_unusual;
        ptr = ::google::protobuf::io::ReadSize(ptr, &size);
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
        parser_till_end = ::dmi::Component::_InternalParse;
        object = msg->mutable_component();
        if (size > end - ptr) goto len_delim_till_end;
        ptr += size;
        GOOGLE_PROTOBUF_PARSER_ASSERT(ctx->ParseExactRange(
            {parser_till_end, object}, ptr - size, ptr));
        break;
      }
      // string reason_detail = 4;
      case 4: {
        if (static_cast<::google::protobuf::uint8>(tag) != 34) goto handle_unusual;
        ptr = ::google::protobuf::io::ReadSize(ptr, &size);
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
        ctx->extra_parse_data().SetFieldName("dmi.HWComponentInfoGetResponse.reason_detail");
        object = msg->mutable_reason_detail();
        if (size > end - ptr + ::google::protobuf::internal::ParseContext::kSlopBytes) {
          parser_till_end = ::google::protobuf::internal::GreedyStringParserUTF8;
          goto string_till_end;
        }
        GOOGLE_PROTOBUF_PARSER_ASSERT(::google::protobuf::internal::StringCheckUTF8(ptr, size, ctx));
        ::google::protobuf::internal::InlineGreedyStringParser(object, ptr, size, ctx);
        ptr += size;
        break;
      }
      default: {
      handle_unusual:
        if ((tag & 7) == 4 || tag == 0) {
          ctx->EndGroup(tag);
          return ptr;
        }
        auto res = UnknownFieldParse(tag, {_InternalParse, msg},
          ptr, end, msg->_internal_metadata_.mutable_unknown_fields(), ctx);
        ptr = res.first;
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr);
        if (res.second) return ptr;
      }
    }  // switch
  }  // while
  return ptr;
string_till_end:
  static_cast<::std::string*>(object)->clear();
  static_cast<::std::string*>(object)->reserve(size);
  goto len_delim_till_end;
len_delim_till_end:
  return ctx->StoreAndTailCall(ptr, end, {_InternalParse, msg},
                               {parser_till_end, object}, size);
}
#else  // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER
bool HWComponentInfoGetResponse::MergePartialFromCodedStream(
    ::google::protobuf::io::CodedInputStream* input) {
#define DO_(EXPRESSION) if (!PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure
  ::google::protobuf::uint32 tag;
  // @@protoc_insertion_point(parse_start:dmi.HWComponentInfoGetResponse)
  for (;;) {
    ::std::pair<::google::protobuf::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u);
    tag = p.first;
    if (!p.second) goto handle_unusual;
    switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) {
      // .dmi.Status status = 1;
      case 1: {
        if (static_cast< ::google::protobuf::uint8>(tag) == (8 & 0xFF)) {
          int value = 0;
          DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive<
                   int, ::google::protobuf::internal::WireFormatLite::TYPE_ENUM>(
                 input, &value)));
          set_status(static_cast< ::dmi::Status >(value));
        } else {
          goto handle_unusual;
        }
        break;
      }

      // .dmi.HWComponentInfoGetResponse.Reason reason = 2;
      case 2: {
        if (static_cast< ::google::protobuf::uint8>(tag) == (16 & 0xFF)) {
          int value = 0;
          DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive<
                   int, ::google::protobuf::internal::WireFormatLite::TYPE_ENUM>(
                 input, &value)));
          set_reason(static_cast< ::dmi::HWComponentInfoGetResponse_Reason >(value));
        } else {
          goto handle_unusual;
        }
        break;
      }

      // .dmi.Component component = 3;
      case 3: {
        if (static_cast< ::google::protobuf::uint8>(tag) == (26 & 0xFF)) {
          DO_(::google::protobuf::internal::WireFormatLite::ReadMessage(
               input, mutable_component()));
        } else {
          goto handle_unusual;
        }
        break;
      }

      // string reason_detail = 4;
      case 4: {
        if (static_cast< ::google::protobuf::uint8>(tag) == (34 & 0xFF)) {
          DO_(::google::protobuf::internal::WireFormatLite::ReadString(
                input, this->mutable_reason_detail()));
          DO_(::google::protobuf::internal::WireFormatLite::VerifyUtf8String(
            this->reason_detail().data(), static_cast<int>(this->reason_detail().length()),
            ::google::protobuf::internal::WireFormatLite::PARSE,
            "dmi.HWComponentInfoGetResponse.reason_detail"));
        } else {
          goto handle_unusual;
        }
        break;
      }

      default: {
      handle_unusual:
        if (tag == 0) {
          goto success;
        }
        DO_(::google::protobuf::internal::WireFormat::SkipField(
              input, tag, _internal_metadata_.mutable_unknown_fields()));
        break;
      }
    }
  }
success:
  // @@protoc_insertion_point(parse_success:dmi.HWComponentInfoGetResponse)
  return true;
failure:
  // @@protoc_insertion_point(parse_failure:dmi.HWComponentInfoGetResponse)
  return false;
#undef DO_
}
#endif  // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER

void HWComponentInfoGetResponse::SerializeWithCachedSizes(
    ::google::protobuf::io::CodedOutputStream* output) const {
  // @@protoc_insertion_point(serialize_start:dmi.HWComponentInfoGetResponse)
  ::google::protobuf::uint32 cached_has_bits = 0;
  (void) cached_has_bits;

  // .dmi.Status status = 1;
  if (this->status() != 0) {
    ::google::protobuf::internal::WireFormatLite::WriteEnum(
      1, this->status(), output);
  }

  // .dmi.HWComponentInfoGetResponse.Reason reason = 2;
  if (this->reason() != 0) {
    ::google::protobuf::internal::WireFormatLite::WriteEnum(
      2, this->reason(), output);
  }

  // .dmi.Component component = 3;
  if (this->has_component()) {
    ::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray(
      3, HasBitSetters::component(this), output);
  }

  // string reason_detail = 4;
  if (this->reason_detail().size() > 0) {
    ::google::protobuf::internal::WireFormatLite::VerifyUtf8String(
      this->reason_detail().data(), static_cast<int>(this->reason_detail().length()),
      ::google::protobuf::internal::WireFormatLite::SERIALIZE,
      "dmi.HWComponentInfoGetResponse.reason_detail");
    ::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased(
      4, this->reason_detail(), output);
  }

  if (_internal_metadata_.have_unknown_fields()) {
    ::google::protobuf::internal::WireFormat::SerializeUnknownFields(
        _internal_metadata_.unknown_fields(), output);
  }
  // @@protoc_insertion_point(serialize_end:dmi.HWComponentInfoGetResponse)
}

::google::protobuf::uint8* HWComponentInfoGetResponse::InternalSerializeWithCachedSizesToArray(
    ::google::protobuf::uint8* target) const {
  // @@protoc_insertion_point(serialize_to_array_start:dmi.HWComponentInfoGetResponse)
  ::google::protobuf::uint32 cached_has_bits = 0;
  (void) cached_has_bits;

  // .dmi.Status status = 1;
  if (this->status() != 0) {
    target = ::google::protobuf::internal::WireFormatLite::WriteEnumToArray(
      1, this->status(), target);
  }

  // .dmi.HWComponentInfoGetResponse.Reason reason = 2;
  if (this->reason() != 0) {
    target = ::google::protobuf::internal::WireFormatLite::WriteEnumToArray(
      2, this->reason(), target);
  }

  // .dmi.Component component = 3;
  if (this->has_component()) {
    target = ::google::protobuf::internal::WireFormatLite::
      InternalWriteMessageToArray(
        3, HasBitSetters::component(this), target);
  }

  // string reason_detail = 4;
  if (this->reason_detail().size() > 0) {
    ::google::protobuf::internal::WireFormatLite::VerifyUtf8String(
      this->reason_detail().data(), static_cast<int>(this->reason_detail().length()),
      ::google::protobuf::internal::WireFormatLite::SERIALIZE,
      "dmi.HWComponentInfoGetResponse.reason_detail");
    target =
      ::google::protobuf::internal::WireFormatLite::WriteStringToArray(
        4, this->reason_detail(), target);
  }

  if (_internal_metadata_.have_unknown_fields()) {
    target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray(
        _internal_metadata_.unknown_fields(), target);
  }
  // @@protoc_insertion_point(serialize_to_array_end:dmi.HWComponentInfoGetResponse)
  return target;
}

size_t HWComponentInfoGetResponse::ByteSizeLong() const {
// @@protoc_insertion_point(message_byte_size_start:dmi.HWComponentInfoGetResponse)
  size_t total_size = 0;

  if (_internal_metadata_.have_unknown_fields()) {
    total_size +=
      ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize(
        _internal_metadata_.unknown_fields());
  }
  ::google::protobuf::uint32 cached_has_bits = 0;
  // Prevent compiler warnings about cached_has_bits being unused
  (void) cached_has_bits;

  // string reason_detail = 4;
  if (this->reason_detail().size() > 0) {
    total_size += 1 +
      ::google::protobuf::internal::WireFormatLite::StringSize(
        this->reason_detail());
  }

  // .dmi.Component component = 3;
  if (this->has_component()) {
    total_size += 1 +
      ::google::protobuf::internal::WireFormatLite::MessageSize(
        *component_);
  }

  // .dmi.Status status = 1;
  if (this->status() != 0) {
    total_size += 1 +
      ::google::protobuf::internal::WireFormatLite::EnumSize(this->status());
  }

  // .dmi.HWComponentInfoGetResponse.Reason reason = 2;
  if (this->reason() != 0) {
    total_size += 1 +
      ::google::protobuf::internal::WireFormatLite::EnumSize(this->reason());
  }

  int cached_size = ::google::protobuf::internal::ToCachedSize(total_size);
  SetCachedSize(cached_size);
  return total_size;
}

void HWComponentInfoGetResponse::MergeFrom(const ::google::protobuf::Message& from) {
// @@protoc_insertion_point(generalized_merge_from_start:dmi.HWComponentInfoGetResponse)
  GOOGLE_DCHECK_NE(&from, this);
  const HWComponentInfoGetResponse* source =
      ::google::protobuf::DynamicCastToGenerated<HWComponentInfoGetResponse>(
          &from);
  if (source == nullptr) {
  // @@protoc_insertion_point(generalized_merge_from_cast_fail:dmi.HWComponentInfoGetResponse)
    ::google::protobuf::internal::ReflectionOps::Merge(from, this);
  } else {
  // @@protoc_insertion_point(generalized_merge_from_cast_success:dmi.HWComponentInfoGetResponse)
    MergeFrom(*source);
  }
}

void HWComponentInfoGetResponse::MergeFrom(const HWComponentInfoGetResponse& from) {
// @@protoc_insertion_point(class_specific_merge_from_start:dmi.HWComponentInfoGetResponse)
  GOOGLE_DCHECK_NE(&from, this);
  _internal_metadata_.MergeFrom(from._internal_metadata_);
  ::google::protobuf::uint32 cached_has_bits = 0;
  (void) cached_has_bits;

  if (from.reason_detail().size() > 0) {

    reason_detail_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.reason_detail_);
  }
  if (from.has_component()) {
    mutable_component()->::dmi::Component::MergeFrom(from.component());
  }
  if (from.status() != 0) {
    set_status(from.status());
  }
  if (from.reason() != 0) {
    set_reason(from.reason());
  }
}

void HWComponentInfoGetResponse::CopyFrom(const ::google::protobuf::Message& from) {
// @@protoc_insertion_point(generalized_copy_from_start:dmi.HWComponentInfoGetResponse)
  if (&from == this) return;
  Clear();
  MergeFrom(from);
}

void HWComponentInfoGetResponse::CopyFrom(const HWComponentInfoGetResponse& from) {
// @@protoc_insertion_point(class_specific_copy_from_start:dmi.HWComponentInfoGetResponse)
  if (&from == this) return;
  Clear();
  MergeFrom(from);
}

bool HWComponentInfoGetResponse::IsInitialized() const {
  return true;
}

void HWComponentInfoGetResponse::Swap(HWComponentInfoGetResponse* other) {
  if (other == this) return;
  InternalSwap(other);
}
void HWComponentInfoGetResponse::InternalSwap(HWComponentInfoGetResponse* other) {
  using std::swap;
  _internal_metadata_.Swap(&other->_internal_metadata_);
  reason_detail_.Swap(&other->reason_detail_, &::google::protobuf::internal::GetEmptyStringAlreadyInited(),
    GetArenaNoVirtual());
  swap(component_, other->component_);
  swap(status_, other->status_);
  swap(reason_, other->reason_);
}

::google::protobuf::Metadata HWComponentInfoGetResponse::GetMetadata() const {
  ::google::protobuf::internal::AssignDescriptors(&::assign_descriptors_table_dmi_2fhw_5fmanagement_5fservice_2eproto);
  return ::file_level_metadata_dmi_2fhw_5fmanagement_5fservice_2eproto[kIndexInFileMessages];
}


// ===================================================================

void HWComponentInfoSetRequest::InitAsDefaultInstance() {
  ::dmi::_HWComponentInfoSetRequest_default_instance_._instance.get_mutable()->device_uuid_ = const_cast< ::dmi::Uuid*>(
      ::dmi::Uuid::internal_default_instance());
  ::dmi::_HWComponentInfoSetRequest_default_instance_._instance.get_mutable()->component_uuid_ = const_cast< ::dmi::Uuid*>(
      ::dmi::Uuid::internal_default_instance());
  ::dmi::_HWComponentInfoSetRequest_default_instance_._instance.get_mutable()->changes_ = const_cast< ::dmi::ModifiableComponent*>(
      ::dmi::ModifiableComponent::internal_default_instance());
}
class HWComponentInfoSetRequest::HasBitSetters {
 public:
  static const ::dmi::Uuid& device_uuid(const HWComponentInfoSetRequest* msg);
  static const ::dmi::Uuid& component_uuid(const HWComponentInfoSetRequest* msg);
  static const ::dmi::ModifiableComponent& changes(const HWComponentInfoSetRequest* msg);
};

const ::dmi::Uuid&
HWComponentInfoSetRequest::HasBitSetters::device_uuid(const HWComponentInfoSetRequest* msg) {
  return *msg->device_uuid_;
}
const ::dmi::Uuid&
HWComponentInfoSetRequest::HasBitSetters::component_uuid(const HWComponentInfoSetRequest* msg) {
  return *msg->component_uuid_;
}
const ::dmi::ModifiableComponent&
HWComponentInfoSetRequest::HasBitSetters::changes(const HWComponentInfoSetRequest* msg) {
  return *msg->changes_;
}
void HWComponentInfoSetRequest::clear_device_uuid() {
  if (GetArenaNoVirtual() == nullptr && device_uuid_ != nullptr) {
    delete device_uuid_;
  }
  device_uuid_ = nullptr;
}
void HWComponentInfoSetRequest::clear_component_uuid() {
  if (GetArenaNoVirtual() == nullptr && component_uuid_ != nullptr) {
    delete component_uuid_;
  }
  component_uuid_ = nullptr;
}
void HWComponentInfoSetRequest::clear_changes() {
  if (GetArenaNoVirtual() == nullptr && changes_ != nullptr) {
    delete changes_;
  }
  changes_ = nullptr;
}
#if !defined(_MSC_VER) || _MSC_VER >= 1900
const int HWComponentInfoSetRequest::kDeviceUuidFieldNumber;
const int HWComponentInfoSetRequest::kComponentUuidFieldNumber;
const int HWComponentInfoSetRequest::kComponentNameFieldNumber;
const int HWComponentInfoSetRequest::kChangesFieldNumber;
#endif  // !defined(_MSC_VER) || _MSC_VER >= 1900

HWComponentInfoSetRequest::HWComponentInfoSetRequest()
  : ::google::protobuf::Message(), _internal_metadata_(nullptr) {
  SharedCtor();
  // @@protoc_insertion_point(constructor:dmi.HWComponentInfoSetRequest)
}
HWComponentInfoSetRequest::HWComponentInfoSetRequest(const HWComponentInfoSetRequest& from)
  : ::google::protobuf::Message(),
      _internal_metadata_(nullptr) {
  _internal_metadata_.MergeFrom(from._internal_metadata_);
  component_name_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
  if (from.component_name().size() > 0) {
    component_name_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.component_name_);
  }
  if (from.has_device_uuid()) {
    device_uuid_ = new ::dmi::Uuid(*from.device_uuid_);
  } else {
    device_uuid_ = nullptr;
  }
  if (from.has_component_uuid()) {
    component_uuid_ = new ::dmi::Uuid(*from.component_uuid_);
  } else {
    component_uuid_ = nullptr;
  }
  if (from.has_changes()) {
    changes_ = new ::dmi::ModifiableComponent(*from.changes_);
  } else {
    changes_ = nullptr;
  }
  // @@protoc_insertion_point(copy_constructor:dmi.HWComponentInfoSetRequest)
}

void HWComponentInfoSetRequest::SharedCtor() {
  ::google::protobuf::internal::InitSCC(
      &scc_info_HWComponentInfoSetRequest_dmi_2fhw_5fmanagement_5fservice_2eproto.base);
  component_name_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
  ::memset(&device_uuid_, 0, static_cast<size_t>(
      reinterpret_cast<char*>(&changes_) -
      reinterpret_cast<char*>(&device_uuid_)) + sizeof(changes_));
}

HWComponentInfoSetRequest::~HWComponentInfoSetRequest() {
  // @@protoc_insertion_point(destructor:dmi.HWComponentInfoSetRequest)
  SharedDtor();
}

void HWComponentInfoSetRequest::SharedDtor() {
  component_name_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
  if (this != internal_default_instance()) delete device_uuid_;
  if (this != internal_default_instance()) delete component_uuid_;
  if (this != internal_default_instance()) delete changes_;
}

void HWComponentInfoSetRequest::SetCachedSize(int size) const {
  _cached_size_.Set(size);
}
const HWComponentInfoSetRequest& HWComponentInfoSetRequest::default_instance() {
  ::google::protobuf::internal::InitSCC(&::scc_info_HWComponentInfoSetRequest_dmi_2fhw_5fmanagement_5fservice_2eproto.base);
  return *internal_default_instance();
}


void HWComponentInfoSetRequest::Clear() {
// @@protoc_insertion_point(message_clear_start:dmi.HWComponentInfoSetRequest)
  ::google::protobuf::uint32 cached_has_bits = 0;
  // Prevent compiler warnings about cached_has_bits being unused
  (void) cached_has_bits;

  component_name_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
  if (GetArenaNoVirtual() == nullptr && device_uuid_ != nullptr) {
    delete device_uuid_;
  }
  device_uuid_ = nullptr;
  if (GetArenaNoVirtual() == nullptr && component_uuid_ != nullptr) {
    delete component_uuid_;
  }
  component_uuid_ = nullptr;
  if (GetArenaNoVirtual() == nullptr && changes_ != nullptr) {
    delete changes_;
  }
  changes_ = nullptr;
  _internal_metadata_.Clear();
}

#if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER
const char* HWComponentInfoSetRequest::_InternalParse(const char* begin, const char* end, void* object,
                  ::google::protobuf::internal::ParseContext* ctx) {
  auto msg = static_cast<HWComponentInfoSetRequest*>(object);
  ::google::protobuf::int32 size; (void)size;
  int depth; (void)depth;
  ::google::protobuf::uint32 tag;
  ::google::protobuf::internal::ParseFunc parser_till_end; (void)parser_till_end;
  auto ptr = begin;
  while (ptr < end) {
    ptr = ::google::protobuf::io::Parse32(ptr, &tag);
    GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
    switch (tag >> 3) {
      // .dmi.Uuid device_uuid = 1;
      case 1: {
        if (static_cast<::google::protobuf::uint8>(tag) != 10) goto handle_unusual;
        ptr = ::google::protobuf::io::ReadSize(ptr, &size);
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
        parser_till_end = ::dmi::Uuid::_InternalParse;
        object = msg->mutable_device_uuid();
        if (size > end - ptr) goto len_delim_till_end;
        ptr += size;
        GOOGLE_PROTOBUF_PARSER_ASSERT(ctx->ParseExactRange(
            {parser_till_end, object}, ptr - size, ptr));
        break;
      }
      // .dmi.Uuid component_uuid = 2;
      case 2: {
        if (static_cast<::google::protobuf::uint8>(tag) != 18) goto handle_unusual;
        ptr = ::google::protobuf::io::ReadSize(ptr, &size);
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
        parser_till_end = ::dmi::Uuid::_InternalParse;
        object = msg->mutable_component_uuid();
        if (size > end - ptr) goto len_delim_till_end;
        ptr += size;
        GOOGLE_PROTOBUF_PARSER_ASSERT(ctx->ParseExactRange(
            {parser_till_end, object}, ptr - size, ptr));
        break;
      }
      // string component_name = 3;
      case 3: {
        if (static_cast<::google::protobuf::uint8>(tag) != 26) goto handle_unusual;
        ptr = ::google::protobuf::io::ReadSize(ptr, &size);
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
        ctx->extra_parse_data().SetFieldName("dmi.HWComponentInfoSetRequest.component_name");
        object = msg->mutable_component_name();
        if (size > end - ptr + ::google::protobuf::internal::ParseContext::kSlopBytes) {
          parser_till_end = ::google::protobuf::internal::GreedyStringParserUTF8;
          goto string_till_end;
        }
        GOOGLE_PROTOBUF_PARSER_ASSERT(::google::protobuf::internal::StringCheckUTF8(ptr, size, ctx));
        ::google::protobuf::internal::InlineGreedyStringParser(object, ptr, size, ctx);
        ptr += size;
        break;
      }
      // .dmi.ModifiableComponent changes = 4;
      case 4: {
        if (static_cast<::google::protobuf::uint8>(tag) != 34) goto handle_unusual;
        ptr = ::google::protobuf::io::ReadSize(ptr, &size);
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
        parser_till_end = ::dmi::ModifiableComponent::_InternalParse;
        object = msg->mutable_changes();
        if (size > end - ptr) goto len_delim_till_end;
        ptr += size;
        GOOGLE_PROTOBUF_PARSER_ASSERT(ctx->ParseExactRange(
            {parser_till_end, object}, ptr - size, ptr));
        break;
      }
      default: {
      handle_unusual:
        if ((tag & 7) == 4 || tag == 0) {
          ctx->EndGroup(tag);
          return ptr;
        }
        auto res = UnknownFieldParse(tag, {_InternalParse, msg},
          ptr, end, msg->_internal_metadata_.mutable_unknown_fields(), ctx);
        ptr = res.first;
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr);
        if (res.second) return ptr;
      }
    }  // switch
  }  // while
  return ptr;
string_till_end:
  static_cast<::std::string*>(object)->clear();
  static_cast<::std::string*>(object)->reserve(size);
  goto len_delim_till_end;
len_delim_till_end:
  return ctx->StoreAndTailCall(ptr, end, {_InternalParse, msg},
                               {parser_till_end, object}, size);
}
#else  // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER
bool HWComponentInfoSetRequest::MergePartialFromCodedStream(
    ::google::protobuf::io::CodedInputStream* input) {
#define DO_(EXPRESSION) if (!PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure
  ::google::protobuf::uint32 tag;
  // @@protoc_insertion_point(parse_start:dmi.HWComponentInfoSetRequest)
  for (;;) {
    ::std::pair<::google::protobuf::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u);
    tag = p.first;
    if (!p.second) goto handle_unusual;
    switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) {
      // .dmi.Uuid device_uuid = 1;
      case 1: {
        if (static_cast< ::google::protobuf::uint8>(tag) == (10 & 0xFF)) {
          DO_(::google::protobuf::internal::WireFormatLite::ReadMessage(
               input, mutable_device_uuid()));
        } else {
          goto handle_unusual;
        }
        break;
      }

      // .dmi.Uuid component_uuid = 2;
      case 2: {
        if (static_cast< ::google::protobuf::uint8>(tag) == (18 & 0xFF)) {
          DO_(::google::protobuf::internal::WireFormatLite::ReadMessage(
               input, mutable_component_uuid()));
        } else {
          goto handle_unusual;
        }
        break;
      }

      // string component_name = 3;
      case 3: {
        if (static_cast< ::google::protobuf::uint8>(tag) == (26 & 0xFF)) {
          DO_(::google::protobuf::internal::WireFormatLite::ReadString(
                input, this->mutable_component_name()));
          DO_(::google::protobuf::internal::WireFormatLite::VerifyUtf8String(
            this->component_name().data(), static_cast<int>(this->component_name().length()),
            ::google::protobuf::internal::WireFormatLite::PARSE,
            "dmi.HWComponentInfoSetRequest.component_name"));
        } else {
          goto handle_unusual;
        }
        break;
      }

      // .dmi.ModifiableComponent changes = 4;
      case 4: {
        if (static_cast< ::google::protobuf::uint8>(tag) == (34 & 0xFF)) {
          DO_(::google::protobuf::internal::WireFormatLite::ReadMessage(
               input, mutable_changes()));
        } else {
          goto handle_unusual;
        }
        break;
      }

      default: {
      handle_unusual:
        if (tag == 0) {
          goto success;
        }
        DO_(::google::protobuf::internal::WireFormat::SkipField(
              input, tag, _internal_metadata_.mutable_unknown_fields()));
        break;
      }
    }
  }
success:
  // @@protoc_insertion_point(parse_success:dmi.HWComponentInfoSetRequest)
  return true;
failure:
  // @@protoc_insertion_point(parse_failure:dmi.HWComponentInfoSetRequest)
  return false;
#undef DO_
}
#endif  // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER

void HWComponentInfoSetRequest::SerializeWithCachedSizes(
    ::google::protobuf::io::CodedOutputStream* output) const {
  // @@protoc_insertion_point(serialize_start:dmi.HWComponentInfoSetRequest)
  ::google::protobuf::uint32 cached_has_bits = 0;
  (void) cached_has_bits;

  // .dmi.Uuid device_uuid = 1;
  if (this->has_device_uuid()) {
    ::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray(
      1, HasBitSetters::device_uuid(this), output);
  }

  // .dmi.Uuid component_uuid = 2;
  if (this->has_component_uuid()) {
    ::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray(
      2, HasBitSetters::component_uuid(this), output);
  }

  // string component_name = 3;
  if (this->component_name().size() > 0) {
    ::google::protobuf::internal::WireFormatLite::VerifyUtf8String(
      this->component_name().data(), static_cast<int>(this->component_name().length()),
      ::google::protobuf::internal::WireFormatLite::SERIALIZE,
      "dmi.HWComponentInfoSetRequest.component_name");
    ::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased(
      3, this->component_name(), output);
  }

  // .dmi.ModifiableComponent changes = 4;
  if (this->has_changes()) {
    ::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray(
      4, HasBitSetters::changes(this), output);
  }

  if (_internal_metadata_.have_unknown_fields()) {
    ::google::protobuf::internal::WireFormat::SerializeUnknownFields(
        _internal_metadata_.unknown_fields(), output);
  }
  // @@protoc_insertion_point(serialize_end:dmi.HWComponentInfoSetRequest)
}

::google::protobuf::uint8* HWComponentInfoSetRequest::InternalSerializeWithCachedSizesToArray(
    ::google::protobuf::uint8* target) const {
  // @@protoc_insertion_point(serialize_to_array_start:dmi.HWComponentInfoSetRequest)
  ::google::protobuf::uint32 cached_has_bits = 0;
  (void) cached_has_bits;

  // .dmi.Uuid device_uuid = 1;
  if (this->has_device_uuid()) {
    target = ::google::protobuf::internal::WireFormatLite::
      InternalWriteMessageToArray(
        1, HasBitSetters::device_uuid(this), target);
  }

  // .dmi.Uuid component_uuid = 2;
  if (this->has_component_uuid()) {
    target = ::google::protobuf::internal::WireFormatLite::
      InternalWriteMessageToArray(
        2, HasBitSetters::component_uuid(this), target);
  }

  // string component_name = 3;
  if (this->component_name().size() > 0) {
    ::google::protobuf::internal::WireFormatLite::VerifyUtf8String(
      this->component_name().data(), static_cast<int>(this->component_name().length()),
      ::google::protobuf::internal::WireFormatLite::SERIALIZE,
      "dmi.HWComponentInfoSetRequest.component_name");
    target =
      ::google::protobuf::internal::WireFormatLite::WriteStringToArray(
        3, this->component_name(), target);
  }

  // .dmi.ModifiableComponent changes = 4;
  if (this->has_changes()) {
    target = ::google::protobuf::internal::WireFormatLite::
      InternalWriteMessageToArray(
        4, HasBitSetters::changes(this), target);
  }

  if (_internal_metadata_.have_unknown_fields()) {
    target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray(
        _internal_metadata_.unknown_fields(), target);
  }
  // @@protoc_insertion_point(serialize_to_array_end:dmi.HWComponentInfoSetRequest)
  return target;
}

size_t HWComponentInfoSetRequest::ByteSizeLong() const {
// @@protoc_insertion_point(message_byte_size_start:dmi.HWComponentInfoSetRequest)
  size_t total_size = 0;

  if (_internal_metadata_.have_unknown_fields()) {
    total_size +=
      ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize(
        _internal_metadata_.unknown_fields());
  }
  ::google::protobuf::uint32 cached_has_bits = 0;
  // Prevent compiler warnings about cached_has_bits being unused
  (void) cached_has_bits;

  // string component_name = 3;
  if (this->component_name().size() > 0) {
    total_size += 1 +
      ::google::protobuf::internal::WireFormatLite::StringSize(
        this->component_name());
  }

  // .dmi.Uuid device_uuid = 1;
  if (this->has_device_uuid()) {
    total_size += 1 +
      ::google::protobuf::internal::WireFormatLite::MessageSize(
        *device_uuid_);
  }

  // .dmi.Uuid component_uuid = 2;
  if (this->has_component_uuid()) {
    total_size += 1 +
      ::google::protobuf::internal::WireFormatLite::MessageSize(
        *component_uuid_);
  }

  // .dmi.ModifiableComponent changes = 4;
  if (this->has_changes()) {
    total_size += 1 +
      ::google::protobuf::internal::WireFormatLite::MessageSize(
        *changes_);
  }

  int cached_size = ::google::protobuf::internal::ToCachedSize(total_size);
  SetCachedSize(cached_size);
  return total_size;
}

void HWComponentInfoSetRequest::MergeFrom(const ::google::protobuf::Message& from) {
// @@protoc_insertion_point(generalized_merge_from_start:dmi.HWComponentInfoSetRequest)
  GOOGLE_DCHECK_NE(&from, this);
  const HWComponentInfoSetRequest* source =
      ::google::protobuf::DynamicCastToGenerated<HWComponentInfoSetRequest>(
          &from);
  if (source == nullptr) {
  // @@protoc_insertion_point(generalized_merge_from_cast_fail:dmi.HWComponentInfoSetRequest)
    ::google::protobuf::internal::ReflectionOps::Merge(from, this);
  } else {
  // @@protoc_insertion_point(generalized_merge_from_cast_success:dmi.HWComponentInfoSetRequest)
    MergeFrom(*source);
  }
}

void HWComponentInfoSetRequest::MergeFrom(const HWComponentInfoSetRequest& from) {
// @@protoc_insertion_point(class_specific_merge_from_start:dmi.HWComponentInfoSetRequest)
  GOOGLE_DCHECK_NE(&from, this);
  _internal_metadata_.MergeFrom(from._internal_metadata_);
  ::google::protobuf::uint32 cached_has_bits = 0;
  (void) cached_has_bits;

  if (from.component_name().size() > 0) {

    component_name_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.component_name_);
  }
  if (from.has_device_uuid()) {
    mutable_device_uuid()->::dmi::Uuid::MergeFrom(from.device_uuid());
  }
  if (from.has_component_uuid()) {
    mutable_component_uuid()->::dmi::Uuid::MergeFrom(from.component_uuid());
  }
  if (from.has_changes()) {
    mutable_changes()->::dmi::ModifiableComponent::MergeFrom(from.changes());
  }
}

void HWComponentInfoSetRequest::CopyFrom(const ::google::protobuf::Message& from) {
// @@protoc_insertion_point(generalized_copy_from_start:dmi.HWComponentInfoSetRequest)
  if (&from == this) return;
  Clear();
  MergeFrom(from);
}

void HWComponentInfoSetRequest::CopyFrom(const HWComponentInfoSetRequest& from) {
// @@protoc_insertion_point(class_specific_copy_from_start:dmi.HWComponentInfoSetRequest)
  if (&from == this) return;
  Clear();
  MergeFrom(from);
}

bool HWComponentInfoSetRequest::IsInitialized() const {
  return true;
}

void HWComponentInfoSetRequest::Swap(HWComponentInfoSetRequest* other) {
  if (other == this) return;
  InternalSwap(other);
}
void HWComponentInfoSetRequest::InternalSwap(HWComponentInfoSetRequest* other) {
  using std::swap;
  _internal_metadata_.Swap(&other->_internal_metadata_);
  component_name_.Swap(&other->component_name_, &::google::protobuf::internal::GetEmptyStringAlreadyInited(),
    GetArenaNoVirtual());
  swap(device_uuid_, other->device_uuid_);
  swap(component_uuid_, other->component_uuid_);
  swap(changes_, other->changes_);
}

::google::protobuf::Metadata HWComponentInfoSetRequest::GetMetadata() const {
  ::google::protobuf::internal::AssignDescriptors(&::assign_descriptors_table_dmi_2fhw_5fmanagement_5fservice_2eproto);
  return ::file_level_metadata_dmi_2fhw_5fmanagement_5fservice_2eproto[kIndexInFileMessages];
}


// ===================================================================

void HWComponentInfoSetResponse::InitAsDefaultInstance() {
}
class HWComponentInfoSetResponse::HasBitSetters {
 public:
};

#if !defined(_MSC_VER) || _MSC_VER >= 1900
const int HWComponentInfoSetResponse::kStatusFieldNumber;
const int HWComponentInfoSetResponse::kReasonFieldNumber;
const int HWComponentInfoSetResponse::kReasonDetailFieldNumber;
#endif  // !defined(_MSC_VER) || _MSC_VER >= 1900

HWComponentInfoSetResponse::HWComponentInfoSetResponse()
  : ::google::protobuf::Message(), _internal_metadata_(nullptr) {
  SharedCtor();
  // @@protoc_insertion_point(constructor:dmi.HWComponentInfoSetResponse)
}
HWComponentInfoSetResponse::HWComponentInfoSetResponse(const HWComponentInfoSetResponse& from)
  : ::google::protobuf::Message(),
      _internal_metadata_(nullptr) {
  _internal_metadata_.MergeFrom(from._internal_metadata_);
  reason_detail_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
  if (from.reason_detail().size() > 0) {
    reason_detail_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.reason_detail_);
  }
  ::memcpy(&status_, &from.status_,
    static_cast<size_t>(reinterpret_cast<char*>(&reason_) -
    reinterpret_cast<char*>(&status_)) + sizeof(reason_));
  // @@protoc_insertion_point(copy_constructor:dmi.HWComponentInfoSetResponse)
}

void HWComponentInfoSetResponse::SharedCtor() {
  ::google::protobuf::internal::InitSCC(
      &scc_info_HWComponentInfoSetResponse_dmi_2fhw_5fmanagement_5fservice_2eproto.base);
  reason_detail_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
  ::memset(&status_, 0, static_cast<size_t>(
      reinterpret_cast<char*>(&reason_) -
      reinterpret_cast<char*>(&status_)) + sizeof(reason_));
}

HWComponentInfoSetResponse::~HWComponentInfoSetResponse() {
  // @@protoc_insertion_point(destructor:dmi.HWComponentInfoSetResponse)
  SharedDtor();
}

void HWComponentInfoSetResponse::SharedDtor() {
  reason_detail_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}

void HWComponentInfoSetResponse::SetCachedSize(int size) const {
  _cached_size_.Set(size);
}
const HWComponentInfoSetResponse& HWComponentInfoSetResponse::default_instance() {
  ::google::protobuf::internal::InitSCC(&::scc_info_HWComponentInfoSetResponse_dmi_2fhw_5fmanagement_5fservice_2eproto.base);
  return *internal_default_instance();
}


void HWComponentInfoSetResponse::Clear() {
// @@protoc_insertion_point(message_clear_start:dmi.HWComponentInfoSetResponse)
  ::google::protobuf::uint32 cached_has_bits = 0;
  // Prevent compiler warnings about cached_has_bits being unused
  (void) cached_has_bits;

  reason_detail_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
  ::memset(&status_, 0, static_cast<size_t>(
      reinterpret_cast<char*>(&reason_) -
      reinterpret_cast<char*>(&status_)) + sizeof(reason_));
  _internal_metadata_.Clear();
}

#if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER
const char* HWComponentInfoSetResponse::_InternalParse(const char* begin, const char* end, void* object,
                  ::google::protobuf::internal::ParseContext* ctx) {
  auto msg = static_cast<HWComponentInfoSetResponse*>(object);
  ::google::protobuf::int32 size; (void)size;
  int depth; (void)depth;
  ::google::protobuf::uint32 tag;
  ::google::protobuf::internal::ParseFunc parser_till_end; (void)parser_till_end;
  auto ptr = begin;
  while (ptr < end) {
    ptr = ::google::protobuf::io::Parse32(ptr, &tag);
    GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
    switch (tag >> 3) {
      // .dmi.Status status = 1;
      case 1: {
        if (static_cast<::google::protobuf::uint8>(tag) != 8) goto handle_unusual;
        ::google::protobuf::uint64 val = ::google::protobuf::internal::ReadVarint(&ptr);
        msg->set_status(static_cast<::dmi::Status>(val));
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
        break;
      }
      // .dmi.HWComponentInfoSetResponse.Reason reason = 2;
      case 2: {
        if (static_cast<::google::protobuf::uint8>(tag) != 16) goto handle_unusual;
        ::google::protobuf::uint64 val = ::google::protobuf::internal::ReadVarint(&ptr);
        msg->set_reason(static_cast<::dmi::HWComponentInfoSetResponse_Reason>(val));
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
        break;
      }
      // string reason_detail = 3;
      case 3: {
        if (static_cast<::google::protobuf::uint8>(tag) != 26) goto handle_unusual;
        ptr = ::google::protobuf::io::ReadSize(ptr, &size);
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
        ctx->extra_parse_data().SetFieldName("dmi.HWComponentInfoSetResponse.reason_detail");
        object = msg->mutable_reason_detail();
        if (size > end - ptr + ::google::protobuf::internal::ParseContext::kSlopBytes) {
          parser_till_end = ::google::protobuf::internal::GreedyStringParserUTF8;
          goto string_till_end;
        }
        GOOGLE_PROTOBUF_PARSER_ASSERT(::google::protobuf::internal::StringCheckUTF8(ptr, size, ctx));
        ::google::protobuf::internal::InlineGreedyStringParser(object, ptr, size, ctx);
        ptr += size;
        break;
      }
      default: {
      handle_unusual:
        if ((tag & 7) == 4 || tag == 0) {
          ctx->EndGroup(tag);
          return ptr;
        }
        auto res = UnknownFieldParse(tag, {_InternalParse, msg},
          ptr, end, msg->_internal_metadata_.mutable_unknown_fields(), ctx);
        ptr = res.first;
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr);
        if (res.second) return ptr;
      }
    }  // switch
  }  // while
  return ptr;
string_till_end:
  static_cast<::std::string*>(object)->clear();
  static_cast<::std::string*>(object)->reserve(size);
  goto len_delim_till_end;
len_delim_till_end:
  return ctx->StoreAndTailCall(ptr, end, {_InternalParse, msg},
                               {parser_till_end, object}, size);
}
#else  // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER
bool HWComponentInfoSetResponse::MergePartialFromCodedStream(
    ::google::protobuf::io::CodedInputStream* input) {
#define DO_(EXPRESSION) if (!PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure
  ::google::protobuf::uint32 tag;
  // @@protoc_insertion_point(parse_start:dmi.HWComponentInfoSetResponse)
  for (;;) {
    ::std::pair<::google::protobuf::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u);
    tag = p.first;
    if (!p.second) goto handle_unusual;
    switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) {
      // .dmi.Status status = 1;
      case 1: {
        if (static_cast< ::google::protobuf::uint8>(tag) == (8 & 0xFF)) {
          int value = 0;
          DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive<
                   int, ::google::protobuf::internal::WireFormatLite::TYPE_ENUM>(
                 input, &value)));
          set_status(static_cast< ::dmi::Status >(value));
        } else {
          goto handle_unusual;
        }
        break;
      }

      // .dmi.HWComponentInfoSetResponse.Reason reason = 2;
      case 2: {
        if (static_cast< ::google::protobuf::uint8>(tag) == (16 & 0xFF)) {
          int value = 0;
          DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive<
                   int, ::google::protobuf::internal::WireFormatLite::TYPE_ENUM>(
                 input, &value)));
          set_reason(static_cast< ::dmi::HWComponentInfoSetResponse_Reason >(value));
        } else {
          goto handle_unusual;
        }
        break;
      }

      // string reason_detail = 3;
      case 3: {
        if (static_cast< ::google::protobuf::uint8>(tag) == (26 & 0xFF)) {
          DO_(::google::protobuf::internal::WireFormatLite::ReadString(
                input, this->mutable_reason_detail()));
          DO_(::google::protobuf::internal::WireFormatLite::VerifyUtf8String(
            this->reason_detail().data(), static_cast<int>(this->reason_detail().length()),
            ::google::protobuf::internal::WireFormatLite::PARSE,
            "dmi.HWComponentInfoSetResponse.reason_detail"));
        } else {
          goto handle_unusual;
        }
        break;
      }

      default: {
      handle_unusual:
        if (tag == 0) {
          goto success;
        }
        DO_(::google::protobuf::internal::WireFormat::SkipField(
              input, tag, _internal_metadata_.mutable_unknown_fields()));
        break;
      }
    }
  }
success:
  // @@protoc_insertion_point(parse_success:dmi.HWComponentInfoSetResponse)
  return true;
failure:
  // @@protoc_insertion_point(parse_failure:dmi.HWComponentInfoSetResponse)
  return false;
#undef DO_
}
#endif  // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER

void HWComponentInfoSetResponse::SerializeWithCachedSizes(
    ::google::protobuf::io::CodedOutputStream* output) const {
  // @@protoc_insertion_point(serialize_start:dmi.HWComponentInfoSetResponse)
  ::google::protobuf::uint32 cached_has_bits = 0;
  (void) cached_has_bits;

  // .dmi.Status status = 1;
  if (this->status() != 0) {
    ::google::protobuf::internal::WireFormatLite::WriteEnum(
      1, this->status(), output);
  }

  // .dmi.HWComponentInfoSetResponse.Reason reason = 2;
  if (this->reason() != 0) {
    ::google::protobuf::internal::WireFormatLite::WriteEnum(
      2, this->reason(), output);
  }

  // string reason_detail = 3;
  if (this->reason_detail().size() > 0) {
    ::google::protobuf::internal::WireFormatLite::VerifyUtf8String(
      this->reason_detail().data(), static_cast<int>(this->reason_detail().length()),
      ::google::protobuf::internal::WireFormatLite::SERIALIZE,
      "dmi.HWComponentInfoSetResponse.reason_detail");
    ::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased(
      3, this->reason_detail(), output);
  }

  if (_internal_metadata_.have_unknown_fields()) {
    ::google::protobuf::internal::WireFormat::SerializeUnknownFields(
        _internal_metadata_.unknown_fields(), output);
  }
  // @@protoc_insertion_point(serialize_end:dmi.HWComponentInfoSetResponse)
}

::google::protobuf::uint8* HWComponentInfoSetResponse::InternalSerializeWithCachedSizesToArray(
    ::google::protobuf::uint8* target) const {
  // @@protoc_insertion_point(serialize_to_array_start:dmi.HWComponentInfoSetResponse)
  ::google::protobuf::uint32 cached_has_bits = 0;
  (void) cached_has_bits;

  // .dmi.Status status = 1;
  if (this->status() != 0) {
    target = ::google::protobuf::internal::WireFormatLite::WriteEnumToArray(
      1, this->status(), target);
  }

  // .dmi.HWComponentInfoSetResponse.Reason reason = 2;
  if (this->reason() != 0) {
    target = ::google::protobuf::internal::WireFormatLite::WriteEnumToArray(
      2, this->reason(), target);
  }

  // string reason_detail = 3;
  if (this->reason_detail().size() > 0) {
    ::google::protobuf::internal::WireFormatLite::VerifyUtf8String(
      this->reason_detail().data(), static_cast<int>(this->reason_detail().length()),
      ::google::protobuf::internal::WireFormatLite::SERIALIZE,
      "dmi.HWComponentInfoSetResponse.reason_detail");
    target =
      ::google::protobuf::internal::WireFormatLite::WriteStringToArray(
        3, this->reason_detail(), target);
  }

  if (_internal_metadata_.have_unknown_fields()) {
    target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray(
        _internal_metadata_.unknown_fields(), target);
  }
  // @@protoc_insertion_point(serialize_to_array_end:dmi.HWComponentInfoSetResponse)
  return target;
}

size_t HWComponentInfoSetResponse::ByteSizeLong() const {
// @@protoc_insertion_point(message_byte_size_start:dmi.HWComponentInfoSetResponse)
  size_t total_size = 0;

  if (_internal_metadata_.have_unknown_fields()) {
    total_size +=
      ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize(
        _internal_metadata_.unknown_fields());
  }
  ::google::protobuf::uint32 cached_has_bits = 0;
  // Prevent compiler warnings about cached_has_bits being unused
  (void) cached_has_bits;

  // string reason_detail = 3;
  if (this->reason_detail().size() > 0) {
    total_size += 1 +
      ::google::protobuf::internal::WireFormatLite::StringSize(
        this->reason_detail());
  }

  // .dmi.Status status = 1;
  if (this->status() != 0) {
    total_size += 1 +
      ::google::protobuf::internal::WireFormatLite::EnumSize(this->status());
  }

  // .dmi.HWComponentInfoSetResponse.Reason reason = 2;
  if (this->reason() != 0) {
    total_size += 1 +
      ::google::protobuf::internal::WireFormatLite::EnumSize(this->reason());
  }

  int cached_size = ::google::protobuf::internal::ToCachedSize(total_size);
  SetCachedSize(cached_size);
  return total_size;
}

void HWComponentInfoSetResponse::MergeFrom(const ::google::protobuf::Message& from) {
// @@protoc_insertion_point(generalized_merge_from_start:dmi.HWComponentInfoSetResponse)
  GOOGLE_DCHECK_NE(&from, this);
  const HWComponentInfoSetResponse* source =
      ::google::protobuf::DynamicCastToGenerated<HWComponentInfoSetResponse>(
          &from);
  if (source == nullptr) {
  // @@protoc_insertion_point(generalized_merge_from_cast_fail:dmi.HWComponentInfoSetResponse)
    ::google::protobuf::internal::ReflectionOps::Merge(from, this);
  } else {
  // @@protoc_insertion_point(generalized_merge_from_cast_success:dmi.HWComponentInfoSetResponse)
    MergeFrom(*source);
  }
}

void HWComponentInfoSetResponse::MergeFrom(const HWComponentInfoSetResponse& from) {
// @@protoc_insertion_point(class_specific_merge_from_start:dmi.HWComponentInfoSetResponse)
  GOOGLE_DCHECK_NE(&from, this);
  _internal_metadata_.MergeFrom(from._internal_metadata_);
  ::google::protobuf::uint32 cached_has_bits = 0;
  (void) cached_has_bits;

  if (from.reason_detail().size() > 0) {

    reason_detail_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.reason_detail_);
  }
  if (from.status() != 0) {
    set_status(from.status());
  }
  if (from.reason() != 0) {
    set_reason(from.reason());
  }
}

void HWComponentInfoSetResponse::CopyFrom(const ::google::protobuf::Message& from) {
// @@protoc_insertion_point(generalized_copy_from_start:dmi.HWComponentInfoSetResponse)
  if (&from == this) return;
  Clear();
  MergeFrom(from);
}

void HWComponentInfoSetResponse::CopyFrom(const HWComponentInfoSetResponse& from) {
// @@protoc_insertion_point(class_specific_copy_from_start:dmi.HWComponentInfoSetResponse)
  if (&from == this) return;
  Clear();
  MergeFrom(from);
}

bool HWComponentInfoSetResponse::IsInitialized() const {
  return true;
}

void HWComponentInfoSetResponse::Swap(HWComponentInfoSetResponse* other) {
  if (other == this) return;
  InternalSwap(other);
}
void HWComponentInfoSetResponse::InternalSwap(HWComponentInfoSetResponse* other) {
  using std::swap;
  _internal_metadata_.Swap(&other->_internal_metadata_);
  reason_detail_.Swap(&other->reason_detail_, &::google::protobuf::internal::GetEmptyStringAlreadyInited(),
    GetArenaNoVirtual());
  swap(status_, other->status_);
  swap(reason_, other->reason_);
}

::google::protobuf::Metadata HWComponentInfoSetResponse::GetMetadata() const {
  ::google::protobuf::internal::AssignDescriptors(&::assign_descriptors_table_dmi_2fhw_5fmanagement_5fservice_2eproto);
  return ::file_level_metadata_dmi_2fhw_5fmanagement_5fservice_2eproto[kIndexInFileMessages];
}


// ===================================================================

void StartManagingDeviceResponse::InitAsDefaultInstance() {
  ::dmi::_StartManagingDeviceResponse_default_instance_._instance.get_mutable()->device_uuid_ = const_cast< ::dmi::Uuid*>(
      ::dmi::Uuid::internal_default_instance());
}
class StartManagingDeviceResponse::HasBitSetters {
 public:
  static const ::dmi::Uuid& device_uuid(const StartManagingDeviceResponse* msg);
};

const ::dmi::Uuid&
StartManagingDeviceResponse::HasBitSetters::device_uuid(const StartManagingDeviceResponse* msg) {
  return *msg->device_uuid_;
}
void StartManagingDeviceResponse::clear_device_uuid() {
  if (GetArenaNoVirtual() == nullptr && device_uuid_ != nullptr) {
    delete device_uuid_;
  }
  device_uuid_ = nullptr;
}
#if !defined(_MSC_VER) || _MSC_VER >= 1900
const int StartManagingDeviceResponse::kStatusFieldNumber;
const int StartManagingDeviceResponse::kReasonFieldNumber;
const int StartManagingDeviceResponse::kDeviceUuidFieldNumber;
const int StartManagingDeviceResponse::kReasonDetailFieldNumber;
#endif  // !defined(_MSC_VER) || _MSC_VER >= 1900

StartManagingDeviceResponse::StartManagingDeviceResponse()
  : ::google::protobuf::Message(), _internal_metadata_(nullptr) {
  SharedCtor();
  // @@protoc_insertion_point(constructor:dmi.StartManagingDeviceResponse)
}
StartManagingDeviceResponse::StartManagingDeviceResponse(const StartManagingDeviceResponse& from)
  : ::google::protobuf::Message(),
      _internal_metadata_(nullptr) {
  _internal_metadata_.MergeFrom(from._internal_metadata_);
  reason_detail_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
  if (from.reason_detail().size() > 0) {
    reason_detail_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.reason_detail_);
  }
  if (from.has_device_uuid()) {
    device_uuid_ = new ::dmi::Uuid(*from.device_uuid_);
  } else {
    device_uuid_ = nullptr;
  }
  ::memcpy(&status_, &from.status_,
    static_cast<size_t>(reinterpret_cast<char*>(&reason_) -
    reinterpret_cast<char*>(&status_)) + sizeof(reason_));
  // @@protoc_insertion_point(copy_constructor:dmi.StartManagingDeviceResponse)
}

void StartManagingDeviceResponse::SharedCtor() {
  ::google::protobuf::internal::InitSCC(
      &scc_info_StartManagingDeviceResponse_dmi_2fhw_5fmanagement_5fservice_2eproto.base);
  reason_detail_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
  ::memset(&device_uuid_, 0, static_cast<size_t>(
      reinterpret_cast<char*>(&reason_) -
      reinterpret_cast<char*>(&device_uuid_)) + sizeof(reason_));
}

StartManagingDeviceResponse::~StartManagingDeviceResponse() {
  // @@protoc_insertion_point(destructor:dmi.StartManagingDeviceResponse)
  SharedDtor();
}

void StartManagingDeviceResponse::SharedDtor() {
  reason_detail_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
  if (this != internal_default_instance()) delete device_uuid_;
}

void StartManagingDeviceResponse::SetCachedSize(int size) const {
  _cached_size_.Set(size);
}
const StartManagingDeviceResponse& StartManagingDeviceResponse::default_instance() {
  ::google::protobuf::internal::InitSCC(&::scc_info_StartManagingDeviceResponse_dmi_2fhw_5fmanagement_5fservice_2eproto.base);
  return *internal_default_instance();
}


void StartManagingDeviceResponse::Clear() {
// @@protoc_insertion_point(message_clear_start:dmi.StartManagingDeviceResponse)
  ::google::protobuf::uint32 cached_has_bits = 0;
  // Prevent compiler warnings about cached_has_bits being unused
  (void) cached_has_bits;

  reason_detail_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
  if (GetArenaNoVirtual() == nullptr && device_uuid_ != nullptr) {
    delete device_uuid_;
  }
  device_uuid_ = nullptr;
  ::memset(&status_, 0, static_cast<size_t>(
      reinterpret_cast<char*>(&reason_) -
      reinterpret_cast<char*>(&status_)) + sizeof(reason_));
  _internal_metadata_.Clear();
}

#if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER
const char* StartManagingDeviceResponse::_InternalParse(const char* begin, const char* end, void* object,
                  ::google::protobuf::internal::ParseContext* ctx) {
  auto msg = static_cast<StartManagingDeviceResponse*>(object);
  ::google::protobuf::int32 size; (void)size;
  int depth; (void)depth;
  ::google::protobuf::uint32 tag;
  ::google::protobuf::internal::ParseFunc parser_till_end; (void)parser_till_end;
  auto ptr = begin;
  while (ptr < end) {
    ptr = ::google::protobuf::io::Parse32(ptr, &tag);
    GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
    switch (tag >> 3) {
      // .dmi.Status status = 1;
      case 1: {
        if (static_cast<::google::protobuf::uint8>(tag) != 8) goto handle_unusual;
        ::google::protobuf::uint64 val = ::google::protobuf::internal::ReadVarint(&ptr);
        msg->set_status(static_cast<::dmi::Status>(val));
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
        break;
      }
      // .dmi.StartManagingDeviceResponse.Reason reason = 2;
      case 2: {
        if (static_cast<::google::protobuf::uint8>(tag) != 16) goto handle_unusual;
        ::google::protobuf::uint64 val = ::google::protobuf::internal::ReadVarint(&ptr);
        msg->set_reason(static_cast<::dmi::StartManagingDeviceResponse_Reason>(val));
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
        break;
      }
      // .dmi.Uuid device_uuid = 3;
      case 3: {
        if (static_cast<::google::protobuf::uint8>(tag) != 26) goto handle_unusual;
        ptr = ::google::protobuf::io::ReadSize(ptr, &size);
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
        parser_till_end = ::dmi::Uuid::_InternalParse;
        object = msg->mutable_device_uuid();
        if (size > end - ptr) goto len_delim_till_end;
        ptr += size;
        GOOGLE_PROTOBUF_PARSER_ASSERT(ctx->ParseExactRange(
            {parser_till_end, object}, ptr - size, ptr));
        break;
      }
      // string reason_detail = 4;
      case 4: {
        if (static_cast<::google::protobuf::uint8>(tag) != 34) goto handle_unusual;
        ptr = ::google::protobuf::io::ReadSize(ptr, &size);
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
        ctx->extra_parse_data().SetFieldName("dmi.StartManagingDeviceResponse.reason_detail");
        object = msg->mutable_reason_detail();
        if (size > end - ptr + ::google::protobuf::internal::ParseContext::kSlopBytes) {
          parser_till_end = ::google::protobuf::internal::GreedyStringParserUTF8;
          goto string_till_end;
        }
        GOOGLE_PROTOBUF_PARSER_ASSERT(::google::protobuf::internal::StringCheckUTF8(ptr, size, ctx));
        ::google::protobuf::internal::InlineGreedyStringParser(object, ptr, size, ctx);
        ptr += size;
        break;
      }
      default: {
      handle_unusual:
        if ((tag & 7) == 4 || tag == 0) {
          ctx->EndGroup(tag);
          return ptr;
        }
        auto res = UnknownFieldParse(tag, {_InternalParse, msg},
          ptr, end, msg->_internal_metadata_.mutable_unknown_fields(), ctx);
        ptr = res.first;
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr);
        if (res.second) return ptr;
      }
    }  // switch
  }  // while
  return ptr;
string_till_end:
  static_cast<::std::string*>(object)->clear();
  static_cast<::std::string*>(object)->reserve(size);
  goto len_delim_till_end;
len_delim_till_end:
  return ctx->StoreAndTailCall(ptr, end, {_InternalParse, msg},
                               {parser_till_end, object}, size);
}
#else  // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER
bool StartManagingDeviceResponse::MergePartialFromCodedStream(
    ::google::protobuf::io::CodedInputStream* input) {
#define DO_(EXPRESSION) if (!PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure
  ::google::protobuf::uint32 tag;
  // @@protoc_insertion_point(parse_start:dmi.StartManagingDeviceResponse)
  for (;;) {
    ::std::pair<::google::protobuf::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u);
    tag = p.first;
    if (!p.second) goto handle_unusual;
    switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) {
      // .dmi.Status status = 1;
      case 1: {
        if (static_cast< ::google::protobuf::uint8>(tag) == (8 & 0xFF)) {
          int value = 0;
          DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive<
                   int, ::google::protobuf::internal::WireFormatLite::TYPE_ENUM>(
                 input, &value)));
          set_status(static_cast< ::dmi::Status >(value));
        } else {
          goto handle_unusual;
        }
        break;
      }

      // .dmi.StartManagingDeviceResponse.Reason reason = 2;
      case 2: {
        if (static_cast< ::google::protobuf::uint8>(tag) == (16 & 0xFF)) {
          int value = 0;
          DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive<
                   int, ::google::protobuf::internal::WireFormatLite::TYPE_ENUM>(
                 input, &value)));
          set_reason(static_cast< ::dmi::StartManagingDeviceResponse_Reason >(value));
        } else {
          goto handle_unusual;
        }
        break;
      }

      // .dmi.Uuid device_uuid = 3;
      case 3: {
        if (static_cast< ::google::protobuf::uint8>(tag) == (26 & 0xFF)) {
          DO_(::google::protobuf::internal::WireFormatLite::ReadMessage(
               input, mutable_device_uuid()));
        } else {
          goto handle_unusual;
        }
        break;
      }

      // string reason_detail = 4;
      case 4: {
        if (static_cast< ::google::protobuf::uint8>(tag) == (34 & 0xFF)) {
          DO_(::google::protobuf::internal::WireFormatLite::ReadString(
                input, this->mutable_reason_detail()));
          DO_(::google::protobuf::internal::WireFormatLite::VerifyUtf8String(
            this->reason_detail().data(), static_cast<int>(this->reason_detail().length()),
            ::google::protobuf::internal::WireFormatLite::PARSE,
            "dmi.StartManagingDeviceResponse.reason_detail"));
        } else {
          goto handle_unusual;
        }
        break;
      }

      default: {
      handle_unusual:
        if (tag == 0) {
          goto success;
        }
        DO_(::google::protobuf::internal::WireFormat::SkipField(
              input, tag, _internal_metadata_.mutable_unknown_fields()));
        break;
      }
    }
  }
success:
  // @@protoc_insertion_point(parse_success:dmi.StartManagingDeviceResponse)
  return true;
failure:
  // @@protoc_insertion_point(parse_failure:dmi.StartManagingDeviceResponse)
  return false;
#undef DO_
}
#endif  // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER

void StartManagingDeviceResponse::SerializeWithCachedSizes(
    ::google::protobuf::io::CodedOutputStream* output) const {
  // @@protoc_insertion_point(serialize_start:dmi.StartManagingDeviceResponse)
  ::google::protobuf::uint32 cached_has_bits = 0;
  (void) cached_has_bits;

  // .dmi.Status status = 1;
  if (this->status() != 0) {
    ::google::protobuf::internal::WireFormatLite::WriteEnum(
      1, this->status(), output);
  }

  // .dmi.StartManagingDeviceResponse.Reason reason = 2;
  if (this->reason() != 0) {
    ::google::protobuf::internal::WireFormatLite::WriteEnum(
      2, this->reason(), output);
  }

  // .dmi.Uuid device_uuid = 3;
  if (this->has_device_uuid()) {
    ::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray(
      3, HasBitSetters::device_uuid(this), output);
  }

  // string reason_detail = 4;
  if (this->reason_detail().size() > 0) {
    ::google::protobuf::internal::WireFormatLite::VerifyUtf8String(
      this->reason_detail().data(), static_cast<int>(this->reason_detail().length()),
      ::google::protobuf::internal::WireFormatLite::SERIALIZE,
      "dmi.StartManagingDeviceResponse.reason_detail");
    ::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased(
      4, this->reason_detail(), output);
  }

  if (_internal_metadata_.have_unknown_fields()) {
    ::google::protobuf::internal::WireFormat::SerializeUnknownFields(
        _internal_metadata_.unknown_fields(), output);
  }
  // @@protoc_insertion_point(serialize_end:dmi.StartManagingDeviceResponse)
}

::google::protobuf::uint8* StartManagingDeviceResponse::InternalSerializeWithCachedSizesToArray(
    ::google::protobuf::uint8* target) const {
  // @@protoc_insertion_point(serialize_to_array_start:dmi.StartManagingDeviceResponse)
  ::google::protobuf::uint32 cached_has_bits = 0;
  (void) cached_has_bits;

  // .dmi.Status status = 1;
  if (this->status() != 0) {
    target = ::google::protobuf::internal::WireFormatLite::WriteEnumToArray(
      1, this->status(), target);
  }

  // .dmi.StartManagingDeviceResponse.Reason reason = 2;
  if (this->reason() != 0) {
    target = ::google::protobuf::internal::WireFormatLite::WriteEnumToArray(
      2, this->reason(), target);
  }

  // .dmi.Uuid device_uuid = 3;
  if (this->has_device_uuid()) {
    target = ::google::protobuf::internal::WireFormatLite::
      InternalWriteMessageToArray(
        3, HasBitSetters::device_uuid(this), target);
  }

  // string reason_detail = 4;
  if (this->reason_detail().size() > 0) {
    ::google::protobuf::internal::WireFormatLite::VerifyUtf8String(
      this->reason_detail().data(), static_cast<int>(this->reason_detail().length()),
      ::google::protobuf::internal::WireFormatLite::SERIALIZE,
      "dmi.StartManagingDeviceResponse.reason_detail");
    target =
      ::google::protobuf::internal::WireFormatLite::WriteStringToArray(
        4, this->reason_detail(), target);
  }

  if (_internal_metadata_.have_unknown_fields()) {
    target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray(
        _internal_metadata_.unknown_fields(), target);
  }
  // @@protoc_insertion_point(serialize_to_array_end:dmi.StartManagingDeviceResponse)
  return target;
}

size_t StartManagingDeviceResponse::ByteSizeLong() const {
// @@protoc_insertion_point(message_byte_size_start:dmi.StartManagingDeviceResponse)
  size_t total_size = 0;

  if (_internal_metadata_.have_unknown_fields()) {
    total_size +=
      ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize(
        _internal_metadata_.unknown_fields());
  }
  ::google::protobuf::uint32 cached_has_bits = 0;
  // Prevent compiler warnings about cached_has_bits being unused
  (void) cached_has_bits;

  // string reason_detail = 4;
  if (this->reason_detail().size() > 0) {
    total_size += 1 +
      ::google::protobuf::internal::WireFormatLite::StringSize(
        this->reason_detail());
  }

  // .dmi.Uuid device_uuid = 3;
  if (this->has_device_uuid()) {
    total_size += 1 +
      ::google::protobuf::internal::WireFormatLite::MessageSize(
        *device_uuid_);
  }

  // .dmi.Status status = 1;
  if (this->status() != 0) {
    total_size += 1 +
      ::google::protobuf::internal::WireFormatLite::EnumSize(this->status());
  }

  // .dmi.StartManagingDeviceResponse.Reason reason = 2;
  if (this->reason() != 0) {
    total_size += 1 +
      ::google::protobuf::internal::WireFormatLite::EnumSize(this->reason());
  }

  int cached_size = ::google::protobuf::internal::ToCachedSize(total_size);
  SetCachedSize(cached_size);
  return total_size;
}

void StartManagingDeviceResponse::MergeFrom(const ::google::protobuf::Message& from) {
// @@protoc_insertion_point(generalized_merge_from_start:dmi.StartManagingDeviceResponse)
  GOOGLE_DCHECK_NE(&from, this);
  const StartManagingDeviceResponse* source =
      ::google::protobuf::DynamicCastToGenerated<StartManagingDeviceResponse>(
          &from);
  if (source == nullptr) {
  // @@protoc_insertion_point(generalized_merge_from_cast_fail:dmi.StartManagingDeviceResponse)
    ::google::protobuf::internal::ReflectionOps::Merge(from, this);
  } else {
  // @@protoc_insertion_point(generalized_merge_from_cast_success:dmi.StartManagingDeviceResponse)
    MergeFrom(*source);
  }
}

void StartManagingDeviceResponse::MergeFrom(const StartManagingDeviceResponse& from) {
// @@protoc_insertion_point(class_specific_merge_from_start:dmi.StartManagingDeviceResponse)
  GOOGLE_DCHECK_NE(&from, this);
  _internal_metadata_.MergeFrom(from._internal_metadata_);
  ::google::protobuf::uint32 cached_has_bits = 0;
  (void) cached_has_bits;

  if (from.reason_detail().size() > 0) {

    reason_detail_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.reason_detail_);
  }
  if (from.has_device_uuid()) {
    mutable_device_uuid()->::dmi::Uuid::MergeFrom(from.device_uuid());
  }
  if (from.status() != 0) {
    set_status(from.status());
  }
  if (from.reason() != 0) {
    set_reason(from.reason());
  }
}

void StartManagingDeviceResponse::CopyFrom(const ::google::protobuf::Message& from) {
// @@protoc_insertion_point(generalized_copy_from_start:dmi.StartManagingDeviceResponse)
  if (&from == this) return;
  Clear();
  MergeFrom(from);
}

void StartManagingDeviceResponse::CopyFrom(const StartManagingDeviceResponse& from) {
// @@protoc_insertion_point(class_specific_copy_from_start:dmi.StartManagingDeviceResponse)
  if (&from == this) return;
  Clear();
  MergeFrom(from);
}

bool StartManagingDeviceResponse::IsInitialized() const {
  return true;
}

void StartManagingDeviceResponse::Swap(StartManagingDeviceResponse* other) {
  if (other == this) return;
  InternalSwap(other);
}
void StartManagingDeviceResponse::InternalSwap(StartManagingDeviceResponse* other) {
  using std::swap;
  _internal_metadata_.Swap(&other->_internal_metadata_);
  reason_detail_.Swap(&other->reason_detail_, &::google::protobuf::internal::GetEmptyStringAlreadyInited(),
    GetArenaNoVirtual());
  swap(device_uuid_, other->device_uuid_);
  swap(status_, other->status_);
  swap(reason_, other->reason_);
}

::google::protobuf::Metadata StartManagingDeviceResponse::GetMetadata() const {
  ::google::protobuf::internal::AssignDescriptors(&::assign_descriptors_table_dmi_2fhw_5fmanagement_5fservice_2eproto);
  return ::file_level_metadata_dmi_2fhw_5fmanagement_5fservice_2eproto[kIndexInFileMessages];
}


// ===================================================================

void StopManagingDeviceRequest::InitAsDefaultInstance() {
}
class StopManagingDeviceRequest::HasBitSetters {
 public:
};

#if !defined(_MSC_VER) || _MSC_VER >= 1900
const int StopManagingDeviceRequest::kNameFieldNumber;
#endif  // !defined(_MSC_VER) || _MSC_VER >= 1900

StopManagingDeviceRequest::StopManagingDeviceRequest()
  : ::google::protobuf::Message(), _internal_metadata_(nullptr) {
  SharedCtor();
  // @@protoc_insertion_point(constructor:dmi.StopManagingDeviceRequest)
}
StopManagingDeviceRequest::StopManagingDeviceRequest(const StopManagingDeviceRequest& from)
  : ::google::protobuf::Message(),
      _internal_metadata_(nullptr) {
  _internal_metadata_.MergeFrom(from._internal_metadata_);
  name_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
  if (from.name().size() > 0) {
    name_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.name_);
  }
  // @@protoc_insertion_point(copy_constructor:dmi.StopManagingDeviceRequest)
}

void StopManagingDeviceRequest::SharedCtor() {
  ::google::protobuf::internal::InitSCC(
      &scc_info_StopManagingDeviceRequest_dmi_2fhw_5fmanagement_5fservice_2eproto.base);
  name_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}

StopManagingDeviceRequest::~StopManagingDeviceRequest() {
  // @@protoc_insertion_point(destructor:dmi.StopManagingDeviceRequest)
  SharedDtor();
}

void StopManagingDeviceRequest::SharedDtor() {
  name_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}

void StopManagingDeviceRequest::SetCachedSize(int size) const {
  _cached_size_.Set(size);
}
const StopManagingDeviceRequest& StopManagingDeviceRequest::default_instance() {
  ::google::protobuf::internal::InitSCC(&::scc_info_StopManagingDeviceRequest_dmi_2fhw_5fmanagement_5fservice_2eproto.base);
  return *internal_default_instance();
}


void StopManagingDeviceRequest::Clear() {
// @@protoc_insertion_point(message_clear_start:dmi.StopManagingDeviceRequest)
  ::google::protobuf::uint32 cached_has_bits = 0;
  // Prevent compiler warnings about cached_has_bits being unused
  (void) cached_has_bits;

  name_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
  _internal_metadata_.Clear();
}

#if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER
const char* StopManagingDeviceRequest::_InternalParse(const char* begin, const char* end, void* object,
                  ::google::protobuf::internal::ParseContext* ctx) {
  auto msg = static_cast<StopManagingDeviceRequest*>(object);
  ::google::protobuf::int32 size; (void)size;
  int depth; (void)depth;
  ::google::protobuf::uint32 tag;
  ::google::protobuf::internal::ParseFunc parser_till_end; (void)parser_till_end;
  auto ptr = begin;
  while (ptr < end) {
    ptr = ::google::protobuf::io::Parse32(ptr, &tag);
    GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
    switch (tag >> 3) {
      // string name = 1;
      case 1: {
        if (static_cast<::google::protobuf::uint8>(tag) != 10) goto handle_unusual;
        ptr = ::google::protobuf::io::ReadSize(ptr, &size);
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
        ctx->extra_parse_data().SetFieldName("dmi.StopManagingDeviceRequest.name");
        object = msg->mutable_name();
        if (size > end - ptr + ::google::protobuf::internal::ParseContext::kSlopBytes) {
          parser_till_end = ::google::protobuf::internal::GreedyStringParserUTF8;
          goto string_till_end;
        }
        GOOGLE_PROTOBUF_PARSER_ASSERT(::google::protobuf::internal::StringCheckUTF8(ptr, size, ctx));
        ::google::protobuf::internal::InlineGreedyStringParser(object, ptr, size, ctx);
        ptr += size;
        break;
      }
      default: {
      handle_unusual:
        if ((tag & 7) == 4 || tag == 0) {
          ctx->EndGroup(tag);
          return ptr;
        }
        auto res = UnknownFieldParse(tag, {_InternalParse, msg},
          ptr, end, msg->_internal_metadata_.mutable_unknown_fields(), ctx);
        ptr = res.first;
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr);
        if (res.second) return ptr;
      }
    }  // switch
  }  // while
  return ptr;
string_till_end:
  static_cast<::std::string*>(object)->clear();
  static_cast<::std::string*>(object)->reserve(size);
  goto len_delim_till_end;
len_delim_till_end:
  return ctx->StoreAndTailCall(ptr, end, {_InternalParse, msg},
                               {parser_till_end, object}, size);
}
#else  // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER
bool StopManagingDeviceRequest::MergePartialFromCodedStream(
    ::google::protobuf::io::CodedInputStream* input) {
#define DO_(EXPRESSION) if (!PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure
  ::google::protobuf::uint32 tag;
  // @@protoc_insertion_point(parse_start:dmi.StopManagingDeviceRequest)
  for (;;) {
    ::std::pair<::google::protobuf::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u);
    tag = p.first;
    if (!p.second) goto handle_unusual;
    switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) {
      // string name = 1;
      case 1: {
        if (static_cast< ::google::protobuf::uint8>(tag) == (10 & 0xFF)) {
          DO_(::google::protobuf::internal::WireFormatLite::ReadString(
                input, this->mutable_name()));
          DO_(::google::protobuf::internal::WireFormatLite::VerifyUtf8String(
            this->name().data(), static_cast<int>(this->name().length()),
            ::google::protobuf::internal::WireFormatLite::PARSE,
            "dmi.StopManagingDeviceRequest.name"));
        } else {
          goto handle_unusual;
        }
        break;
      }

      default: {
      handle_unusual:
        if (tag == 0) {
          goto success;
        }
        DO_(::google::protobuf::internal::WireFormat::SkipField(
              input, tag, _internal_metadata_.mutable_unknown_fields()));
        break;
      }
    }
  }
success:
  // @@protoc_insertion_point(parse_success:dmi.StopManagingDeviceRequest)
  return true;
failure:
  // @@protoc_insertion_point(parse_failure:dmi.StopManagingDeviceRequest)
  return false;
#undef DO_
}
#endif  // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER

void StopManagingDeviceRequest::SerializeWithCachedSizes(
    ::google::protobuf::io::CodedOutputStream* output) const {
  // @@protoc_insertion_point(serialize_start:dmi.StopManagingDeviceRequest)
  ::google::protobuf::uint32 cached_has_bits = 0;
  (void) cached_has_bits;

  // string name = 1;
  if (this->name().size() > 0) {
    ::google::protobuf::internal::WireFormatLite::VerifyUtf8String(
      this->name().data(), static_cast<int>(this->name().length()),
      ::google::protobuf::internal::WireFormatLite::SERIALIZE,
      "dmi.StopManagingDeviceRequest.name");
    ::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased(
      1, this->name(), output);
  }

  if (_internal_metadata_.have_unknown_fields()) {
    ::google::protobuf::internal::WireFormat::SerializeUnknownFields(
        _internal_metadata_.unknown_fields(), output);
  }
  // @@protoc_insertion_point(serialize_end:dmi.StopManagingDeviceRequest)
}

::google::protobuf::uint8* StopManagingDeviceRequest::InternalSerializeWithCachedSizesToArray(
    ::google::protobuf::uint8* target) const {
  // @@protoc_insertion_point(serialize_to_array_start:dmi.StopManagingDeviceRequest)
  ::google::protobuf::uint32 cached_has_bits = 0;
  (void) cached_has_bits;

  // string name = 1;
  if (this->name().size() > 0) {
    ::google::protobuf::internal::WireFormatLite::VerifyUtf8String(
      this->name().data(), static_cast<int>(this->name().length()),
      ::google::protobuf::internal::WireFormatLite::SERIALIZE,
      "dmi.StopManagingDeviceRequest.name");
    target =
      ::google::protobuf::internal::WireFormatLite::WriteStringToArray(
        1, this->name(), target);
  }

  if (_internal_metadata_.have_unknown_fields()) {
    target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray(
        _internal_metadata_.unknown_fields(), target);
  }
  // @@protoc_insertion_point(serialize_to_array_end:dmi.StopManagingDeviceRequest)
  return target;
}

size_t StopManagingDeviceRequest::ByteSizeLong() const {
// @@protoc_insertion_point(message_byte_size_start:dmi.StopManagingDeviceRequest)
  size_t total_size = 0;

  if (_internal_metadata_.have_unknown_fields()) {
    total_size +=
      ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize(
        _internal_metadata_.unknown_fields());
  }
  ::google::protobuf::uint32 cached_has_bits = 0;
  // Prevent compiler warnings about cached_has_bits being unused
  (void) cached_has_bits;

  // string name = 1;
  if (this->name().size() > 0) {
    total_size += 1 +
      ::google::protobuf::internal::WireFormatLite::StringSize(
        this->name());
  }

  int cached_size = ::google::protobuf::internal::ToCachedSize(total_size);
  SetCachedSize(cached_size);
  return total_size;
}

void StopManagingDeviceRequest::MergeFrom(const ::google::protobuf::Message& from) {
// @@protoc_insertion_point(generalized_merge_from_start:dmi.StopManagingDeviceRequest)
  GOOGLE_DCHECK_NE(&from, this);
  const StopManagingDeviceRequest* source =
      ::google::protobuf::DynamicCastToGenerated<StopManagingDeviceRequest>(
          &from);
  if (source == nullptr) {
  // @@protoc_insertion_point(generalized_merge_from_cast_fail:dmi.StopManagingDeviceRequest)
    ::google::protobuf::internal::ReflectionOps::Merge(from, this);
  } else {
  // @@protoc_insertion_point(generalized_merge_from_cast_success:dmi.StopManagingDeviceRequest)
    MergeFrom(*source);
  }
}

void StopManagingDeviceRequest::MergeFrom(const StopManagingDeviceRequest& from) {
// @@protoc_insertion_point(class_specific_merge_from_start:dmi.StopManagingDeviceRequest)
  GOOGLE_DCHECK_NE(&from, this);
  _internal_metadata_.MergeFrom(from._internal_metadata_);
  ::google::protobuf::uint32 cached_has_bits = 0;
  (void) cached_has_bits;

  if (from.name().size() > 0) {

    name_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.name_);
  }
}

void StopManagingDeviceRequest::CopyFrom(const ::google::protobuf::Message& from) {
// @@protoc_insertion_point(generalized_copy_from_start:dmi.StopManagingDeviceRequest)
  if (&from == this) return;
  Clear();
  MergeFrom(from);
}

void StopManagingDeviceRequest::CopyFrom(const StopManagingDeviceRequest& from) {
// @@protoc_insertion_point(class_specific_copy_from_start:dmi.StopManagingDeviceRequest)
  if (&from == this) return;
  Clear();
  MergeFrom(from);
}

bool StopManagingDeviceRequest::IsInitialized() const {
  return true;
}

void StopManagingDeviceRequest::Swap(StopManagingDeviceRequest* other) {
  if (other == this) return;
  InternalSwap(other);
}
void StopManagingDeviceRequest::InternalSwap(StopManagingDeviceRequest* other) {
  using std::swap;
  _internal_metadata_.Swap(&other->_internal_metadata_);
  name_.Swap(&other->name_, &::google::protobuf::internal::GetEmptyStringAlreadyInited(),
    GetArenaNoVirtual());
}

::google::protobuf::Metadata StopManagingDeviceRequest::GetMetadata() const {
  ::google::protobuf::internal::AssignDescriptors(&::assign_descriptors_table_dmi_2fhw_5fmanagement_5fservice_2eproto);
  return ::file_level_metadata_dmi_2fhw_5fmanagement_5fservice_2eproto[kIndexInFileMessages];
}


// ===================================================================

void StopManagingDeviceResponse::InitAsDefaultInstance() {
}
class StopManagingDeviceResponse::HasBitSetters {
 public:
};

#if !defined(_MSC_VER) || _MSC_VER >= 1900
const int StopManagingDeviceResponse::kStatusFieldNumber;
const int StopManagingDeviceResponse::kReasonFieldNumber;
const int StopManagingDeviceResponse::kReasonDetailFieldNumber;
#endif  // !defined(_MSC_VER) || _MSC_VER >= 1900

StopManagingDeviceResponse::StopManagingDeviceResponse()
  : ::google::protobuf::Message(), _internal_metadata_(nullptr) {
  SharedCtor();
  // @@protoc_insertion_point(constructor:dmi.StopManagingDeviceResponse)
}
StopManagingDeviceResponse::StopManagingDeviceResponse(const StopManagingDeviceResponse& from)
  : ::google::protobuf::Message(),
      _internal_metadata_(nullptr) {
  _internal_metadata_.MergeFrom(from._internal_metadata_);
  reason_detail_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
  if (from.reason_detail().size() > 0) {
    reason_detail_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.reason_detail_);
  }
  ::memcpy(&status_, &from.status_,
    static_cast<size_t>(reinterpret_cast<char*>(&reason_) -
    reinterpret_cast<char*>(&status_)) + sizeof(reason_));
  // @@protoc_insertion_point(copy_constructor:dmi.StopManagingDeviceResponse)
}

void StopManagingDeviceResponse::SharedCtor() {
  ::google::protobuf::internal::InitSCC(
      &scc_info_StopManagingDeviceResponse_dmi_2fhw_5fmanagement_5fservice_2eproto.base);
  reason_detail_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
  ::memset(&status_, 0, static_cast<size_t>(
      reinterpret_cast<char*>(&reason_) -
      reinterpret_cast<char*>(&status_)) + sizeof(reason_));
}

StopManagingDeviceResponse::~StopManagingDeviceResponse() {
  // @@protoc_insertion_point(destructor:dmi.StopManagingDeviceResponse)
  SharedDtor();
}

void StopManagingDeviceResponse::SharedDtor() {
  reason_detail_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}

void StopManagingDeviceResponse::SetCachedSize(int size) const {
  _cached_size_.Set(size);
}
const StopManagingDeviceResponse& StopManagingDeviceResponse::default_instance() {
  ::google::protobuf::internal::InitSCC(&::scc_info_StopManagingDeviceResponse_dmi_2fhw_5fmanagement_5fservice_2eproto.base);
  return *internal_default_instance();
}


void StopManagingDeviceResponse::Clear() {
// @@protoc_insertion_point(message_clear_start:dmi.StopManagingDeviceResponse)
  ::google::protobuf::uint32 cached_has_bits = 0;
  // Prevent compiler warnings about cached_has_bits being unused
  (void) cached_has_bits;

  reason_detail_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
  ::memset(&status_, 0, static_cast<size_t>(
      reinterpret_cast<char*>(&reason_) -
      reinterpret_cast<char*>(&status_)) + sizeof(reason_));
  _internal_metadata_.Clear();
}

#if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER
const char* StopManagingDeviceResponse::_InternalParse(const char* begin, const char* end, void* object,
                  ::google::protobuf::internal::ParseContext* ctx) {
  auto msg = static_cast<StopManagingDeviceResponse*>(object);
  ::google::protobuf::int32 size; (void)size;
  int depth; (void)depth;
  ::google::protobuf::uint32 tag;
  ::google::protobuf::internal::ParseFunc parser_till_end; (void)parser_till_end;
  auto ptr = begin;
  while (ptr < end) {
    ptr = ::google::protobuf::io::Parse32(ptr, &tag);
    GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
    switch (tag >> 3) {
      // .dmi.Status status = 1;
      case 1: {
        if (static_cast<::google::protobuf::uint8>(tag) != 8) goto handle_unusual;
        ::google::protobuf::uint64 val = ::google::protobuf::internal::ReadVarint(&ptr);
        msg->set_status(static_cast<::dmi::Status>(val));
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
        break;
      }
      // .dmi.StopManagingDeviceResponse.Reason reason = 2;
      case 2: {
        if (static_cast<::google::protobuf::uint8>(tag) != 16) goto handle_unusual;
        ::google::protobuf::uint64 val = ::google::protobuf::internal::ReadVarint(&ptr);
        msg->set_reason(static_cast<::dmi::StopManagingDeviceResponse_Reason>(val));
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
        break;
      }
      // string reason_detail = 3;
      case 3: {
        if (static_cast<::google::protobuf::uint8>(tag) != 26) goto handle_unusual;
        ptr = ::google::protobuf::io::ReadSize(ptr, &size);
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
        ctx->extra_parse_data().SetFieldName("dmi.StopManagingDeviceResponse.reason_detail");
        object = msg->mutable_reason_detail();
        if (size > end - ptr + ::google::protobuf::internal::ParseContext::kSlopBytes) {
          parser_till_end = ::google::protobuf::internal::GreedyStringParserUTF8;
          goto string_till_end;
        }
        GOOGLE_PROTOBUF_PARSER_ASSERT(::google::protobuf::internal::StringCheckUTF8(ptr, size, ctx));
        ::google::protobuf::internal::InlineGreedyStringParser(object, ptr, size, ctx);
        ptr += size;
        break;
      }
      default: {
      handle_unusual:
        if ((tag & 7) == 4 || tag == 0) {
          ctx->EndGroup(tag);
          return ptr;
        }
        auto res = UnknownFieldParse(tag, {_InternalParse, msg},
          ptr, end, msg->_internal_metadata_.mutable_unknown_fields(), ctx);
        ptr = res.first;
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr);
        if (res.second) return ptr;
      }
    }  // switch
  }  // while
  return ptr;
string_till_end:
  static_cast<::std::string*>(object)->clear();
  static_cast<::std::string*>(object)->reserve(size);
  goto len_delim_till_end;
len_delim_till_end:
  return ctx->StoreAndTailCall(ptr, end, {_InternalParse, msg},
                               {parser_till_end, object}, size);
}
#else  // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER
bool StopManagingDeviceResponse::MergePartialFromCodedStream(
    ::google::protobuf::io::CodedInputStream* input) {
#define DO_(EXPRESSION) if (!PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure
  ::google::protobuf::uint32 tag;
  // @@protoc_insertion_point(parse_start:dmi.StopManagingDeviceResponse)
  for (;;) {
    ::std::pair<::google::protobuf::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u);
    tag = p.first;
    if (!p.second) goto handle_unusual;
    switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) {
      // .dmi.Status status = 1;
      case 1: {
        if (static_cast< ::google::protobuf::uint8>(tag) == (8 & 0xFF)) {
          int value = 0;
          DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive<
                   int, ::google::protobuf::internal::WireFormatLite::TYPE_ENUM>(
                 input, &value)));
          set_status(static_cast< ::dmi::Status >(value));
        } else {
          goto handle_unusual;
        }
        break;
      }

      // .dmi.StopManagingDeviceResponse.Reason reason = 2;
      case 2: {
        if (static_cast< ::google::protobuf::uint8>(tag) == (16 & 0xFF)) {
          int value = 0;
          DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive<
                   int, ::google::protobuf::internal::WireFormatLite::TYPE_ENUM>(
                 input, &value)));
          set_reason(static_cast< ::dmi::StopManagingDeviceResponse_Reason >(value));
        } else {
          goto handle_unusual;
        }
        break;
      }

      // string reason_detail = 3;
      case 3: {
        if (static_cast< ::google::protobuf::uint8>(tag) == (26 & 0xFF)) {
          DO_(::google::protobuf::internal::WireFormatLite::ReadString(
                input, this->mutable_reason_detail()));
          DO_(::google::protobuf::internal::WireFormatLite::VerifyUtf8String(
            this->reason_detail().data(), static_cast<int>(this->reason_detail().length()),
            ::google::protobuf::internal::WireFormatLite::PARSE,
            "dmi.StopManagingDeviceResponse.reason_detail"));
        } else {
          goto handle_unusual;
        }
        break;
      }

      default: {
      handle_unusual:
        if (tag == 0) {
          goto success;
        }
        DO_(::google::protobuf::internal::WireFormat::SkipField(
              input, tag, _internal_metadata_.mutable_unknown_fields()));
        break;
      }
    }
  }
success:
  // @@protoc_insertion_point(parse_success:dmi.StopManagingDeviceResponse)
  return true;
failure:
  // @@protoc_insertion_point(parse_failure:dmi.StopManagingDeviceResponse)
  return false;
#undef DO_
}
#endif  // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER

void StopManagingDeviceResponse::SerializeWithCachedSizes(
    ::google::protobuf::io::CodedOutputStream* output) const {
  // @@protoc_insertion_point(serialize_start:dmi.StopManagingDeviceResponse)
  ::google::protobuf::uint32 cached_has_bits = 0;
  (void) cached_has_bits;

  // .dmi.Status status = 1;
  if (this->status() != 0) {
    ::google::protobuf::internal::WireFormatLite::WriteEnum(
      1, this->status(), output);
  }

  // .dmi.StopManagingDeviceResponse.Reason reason = 2;
  if (this->reason() != 0) {
    ::google::protobuf::internal::WireFormatLite::WriteEnum(
      2, this->reason(), output);
  }

  // string reason_detail = 3;
  if (this->reason_detail().size() > 0) {
    ::google::protobuf::internal::WireFormatLite::VerifyUtf8String(
      this->reason_detail().data(), static_cast<int>(this->reason_detail().length()),
      ::google::protobuf::internal::WireFormatLite::SERIALIZE,
      "dmi.StopManagingDeviceResponse.reason_detail");
    ::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased(
      3, this->reason_detail(), output);
  }

  if (_internal_metadata_.have_unknown_fields()) {
    ::google::protobuf::internal::WireFormat::SerializeUnknownFields(
        _internal_metadata_.unknown_fields(), output);
  }
  // @@protoc_insertion_point(serialize_end:dmi.StopManagingDeviceResponse)
}

::google::protobuf::uint8* StopManagingDeviceResponse::InternalSerializeWithCachedSizesToArray(
    ::google::protobuf::uint8* target) const {
  // @@protoc_insertion_point(serialize_to_array_start:dmi.StopManagingDeviceResponse)
  ::google::protobuf::uint32 cached_has_bits = 0;
  (void) cached_has_bits;

  // .dmi.Status status = 1;
  if (this->status() != 0) {
    target = ::google::protobuf::internal::WireFormatLite::WriteEnumToArray(
      1, this->status(), target);
  }

  // .dmi.StopManagingDeviceResponse.Reason reason = 2;
  if (this->reason() != 0) {
    target = ::google::protobuf::internal::WireFormatLite::WriteEnumToArray(
      2, this->reason(), target);
  }

  // string reason_detail = 3;
  if (this->reason_detail().size() > 0) {
    ::google::protobuf::internal::WireFormatLite::VerifyUtf8String(
      this->reason_detail().data(), static_cast<int>(this->reason_detail().length()),
      ::google::protobuf::internal::WireFormatLite::SERIALIZE,
      "dmi.StopManagingDeviceResponse.reason_detail");
    target =
      ::google::protobuf::internal::WireFormatLite::WriteStringToArray(
        3, this->reason_detail(), target);
  }

  if (_internal_metadata_.have_unknown_fields()) {
    target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray(
        _internal_metadata_.unknown_fields(), target);
  }
  // @@protoc_insertion_point(serialize_to_array_end:dmi.StopManagingDeviceResponse)
  return target;
}

size_t StopManagingDeviceResponse::ByteSizeLong() const {
// @@protoc_insertion_point(message_byte_size_start:dmi.StopManagingDeviceResponse)
  size_t total_size = 0;

  if (_internal_metadata_.have_unknown_fields()) {
    total_size +=
      ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize(
        _internal_metadata_.unknown_fields());
  }
  ::google::protobuf::uint32 cached_has_bits = 0;
  // Prevent compiler warnings about cached_has_bits being unused
  (void) cached_has_bits;

  // string reason_detail = 3;
  if (this->reason_detail().size() > 0) {
    total_size += 1 +
      ::google::protobuf::internal::WireFormatLite::StringSize(
        this->reason_detail());
  }

  // .dmi.Status status = 1;
  if (this->status() != 0) {
    total_size += 1 +
      ::google::protobuf::internal::WireFormatLite::EnumSize(this->status());
  }

  // .dmi.StopManagingDeviceResponse.Reason reason = 2;
  if (this->reason() != 0) {
    total_size += 1 +
      ::google::protobuf::internal::WireFormatLite::EnumSize(this->reason());
  }

  int cached_size = ::google::protobuf::internal::ToCachedSize(total_size);
  SetCachedSize(cached_size);
  return total_size;
}

void StopManagingDeviceResponse::MergeFrom(const ::google::protobuf::Message& from) {
// @@protoc_insertion_point(generalized_merge_from_start:dmi.StopManagingDeviceResponse)
  GOOGLE_DCHECK_NE(&from, this);
  const StopManagingDeviceResponse* source =
      ::google::protobuf::DynamicCastToGenerated<StopManagingDeviceResponse>(
          &from);
  if (source == nullptr) {
  // @@protoc_insertion_point(generalized_merge_from_cast_fail:dmi.StopManagingDeviceResponse)
    ::google::protobuf::internal::ReflectionOps::Merge(from, this);
  } else {
  // @@protoc_insertion_point(generalized_merge_from_cast_success:dmi.StopManagingDeviceResponse)
    MergeFrom(*source);
  }
}

void StopManagingDeviceResponse::MergeFrom(const StopManagingDeviceResponse& from) {
// @@protoc_insertion_point(class_specific_merge_from_start:dmi.StopManagingDeviceResponse)
  GOOGLE_DCHECK_NE(&from, this);
  _internal_metadata_.MergeFrom(from._internal_metadata_);
  ::google::protobuf::uint32 cached_has_bits = 0;
  (void) cached_has_bits;

  if (from.reason_detail().size() > 0) {

    reason_detail_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.reason_detail_);
  }
  if (from.status() != 0) {
    set_status(from.status());
  }
  if (from.reason() != 0) {
    set_reason(from.reason());
  }
}

void StopManagingDeviceResponse::CopyFrom(const ::google::protobuf::Message& from) {
// @@protoc_insertion_point(generalized_copy_from_start:dmi.StopManagingDeviceResponse)
  if (&from == this) return;
  Clear();
  MergeFrom(from);
}

void StopManagingDeviceResponse::CopyFrom(const StopManagingDeviceResponse& from) {
// @@protoc_insertion_point(class_specific_copy_from_start:dmi.StopManagingDeviceResponse)
  if (&from == this) return;
  Clear();
  MergeFrom(from);
}

bool StopManagingDeviceResponse::IsInitialized() const {
  return true;
}

void StopManagingDeviceResponse::Swap(StopManagingDeviceResponse* other) {
  if (other == this) return;
  InternalSwap(other);
}
void StopManagingDeviceResponse::InternalSwap(StopManagingDeviceResponse* other) {
  using std::swap;
  _internal_metadata_.Swap(&other->_internal_metadata_);
  reason_detail_.Swap(&other->reason_detail_, &::google::protobuf::internal::GetEmptyStringAlreadyInited(),
    GetArenaNoVirtual());
  swap(status_, other->status_);
  swap(reason_, other->reason_);
}

::google::protobuf::Metadata StopManagingDeviceResponse::GetMetadata() const {
  ::google::protobuf::internal::AssignDescriptors(&::assign_descriptors_table_dmi_2fhw_5fmanagement_5fservice_2eproto);
  return ::file_level_metadata_dmi_2fhw_5fmanagement_5fservice_2eproto[kIndexInFileMessages];
}


// ===================================================================

void ManagedDeviceInfo::InitAsDefaultInstance() {
  ::dmi::_ManagedDeviceInfo_default_instance_._instance.get_mutable()->info_ = const_cast< ::dmi::ModifiableComponent*>(
      ::dmi::ModifiableComponent::internal_default_instance());
  ::dmi::_ManagedDeviceInfo_default_instance_._instance.get_mutable()->device_uuid_ = const_cast< ::dmi::Uuid*>(
      ::dmi::Uuid::internal_default_instance());
}
class ManagedDeviceInfo::HasBitSetters {
 public:
  static const ::dmi::ModifiableComponent& info(const ManagedDeviceInfo* msg);
  static const ::dmi::Uuid& device_uuid(const ManagedDeviceInfo* msg);
};

const ::dmi::ModifiableComponent&
ManagedDeviceInfo::HasBitSetters::info(const ManagedDeviceInfo* msg) {
  return *msg->info_;
}
const ::dmi::Uuid&
ManagedDeviceInfo::HasBitSetters::device_uuid(const ManagedDeviceInfo* msg) {
  return *msg->device_uuid_;
}
void ManagedDeviceInfo::clear_info() {
  if (GetArenaNoVirtual() == nullptr && info_ != nullptr) {
    delete info_;
  }
  info_ = nullptr;
}
void ManagedDeviceInfo::clear_device_uuid() {
  if (GetArenaNoVirtual() == nullptr && device_uuid_ != nullptr) {
    delete device_uuid_;
  }
  device_uuid_ = nullptr;
}
#if !defined(_MSC_VER) || _MSC_VER >= 1900
const int ManagedDeviceInfo::kInfoFieldNumber;
const int ManagedDeviceInfo::kDeviceUuidFieldNumber;
#endif  // !defined(_MSC_VER) || _MSC_VER >= 1900

ManagedDeviceInfo::ManagedDeviceInfo()
  : ::google::protobuf::Message(), _internal_metadata_(nullptr) {
  SharedCtor();
  // @@protoc_insertion_point(constructor:dmi.ManagedDeviceInfo)
}
ManagedDeviceInfo::ManagedDeviceInfo(const ManagedDeviceInfo& from)
  : ::google::protobuf::Message(),
      _internal_metadata_(nullptr) {
  _internal_metadata_.MergeFrom(from._internal_metadata_);
  if (from.has_info()) {
    info_ = new ::dmi::ModifiableComponent(*from.info_);
  } else {
    info_ = nullptr;
  }
  if (from.has_device_uuid()) {
    device_uuid_ = new ::dmi::Uuid(*from.device_uuid_);
  } else {
    device_uuid_ = nullptr;
  }
  // @@protoc_insertion_point(copy_constructor:dmi.ManagedDeviceInfo)
}

void ManagedDeviceInfo::SharedCtor() {
  ::google::protobuf::internal::InitSCC(
      &scc_info_ManagedDeviceInfo_dmi_2fhw_5fmanagement_5fservice_2eproto.base);
  ::memset(&info_, 0, static_cast<size_t>(
      reinterpret_cast<char*>(&device_uuid_) -
      reinterpret_cast<char*>(&info_)) + sizeof(device_uuid_));
}

ManagedDeviceInfo::~ManagedDeviceInfo() {
  // @@protoc_insertion_point(destructor:dmi.ManagedDeviceInfo)
  SharedDtor();
}

void ManagedDeviceInfo::SharedDtor() {
  if (this != internal_default_instance()) delete info_;
  if (this != internal_default_instance()) delete device_uuid_;
}

void ManagedDeviceInfo::SetCachedSize(int size) const {
  _cached_size_.Set(size);
}
const ManagedDeviceInfo& ManagedDeviceInfo::default_instance() {
  ::google::protobuf::internal::InitSCC(&::scc_info_ManagedDeviceInfo_dmi_2fhw_5fmanagement_5fservice_2eproto.base);
  return *internal_default_instance();
}


void ManagedDeviceInfo::Clear() {
// @@protoc_insertion_point(message_clear_start:dmi.ManagedDeviceInfo)
  ::google::protobuf::uint32 cached_has_bits = 0;
  // Prevent compiler warnings about cached_has_bits being unused
  (void) cached_has_bits;

  if (GetArenaNoVirtual() == nullptr && info_ != nullptr) {
    delete info_;
  }
  info_ = nullptr;
  if (GetArenaNoVirtual() == nullptr && device_uuid_ != nullptr) {
    delete device_uuid_;
  }
  device_uuid_ = nullptr;
  _internal_metadata_.Clear();
}

#if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER
const char* ManagedDeviceInfo::_InternalParse(const char* begin, const char* end, void* object,
                  ::google::protobuf::internal::ParseContext* ctx) {
  auto msg = static_cast<ManagedDeviceInfo*>(object);
  ::google::protobuf::int32 size; (void)size;
  int depth; (void)depth;
  ::google::protobuf::uint32 tag;
  ::google::protobuf::internal::ParseFunc parser_till_end; (void)parser_till_end;
  auto ptr = begin;
  while (ptr < end) {
    ptr = ::google::protobuf::io::Parse32(ptr, &tag);
    GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
    switch (tag >> 3) {
      // .dmi.ModifiableComponent info = 1;
      case 1: {
        if (static_cast<::google::protobuf::uint8>(tag) != 10) goto handle_unusual;
        ptr = ::google::protobuf::io::ReadSize(ptr, &size);
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
        parser_till_end = ::dmi::ModifiableComponent::_InternalParse;
        object = msg->mutable_info();
        if (size > end - ptr) goto len_delim_till_end;
        ptr += size;
        GOOGLE_PROTOBUF_PARSER_ASSERT(ctx->ParseExactRange(
            {parser_till_end, object}, ptr - size, ptr));
        break;
      }
      // .dmi.Uuid device_uuid = 2;
      case 2: {
        if (static_cast<::google::protobuf::uint8>(tag) != 18) goto handle_unusual;
        ptr = ::google::protobuf::io::ReadSize(ptr, &size);
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
        parser_till_end = ::dmi::Uuid::_InternalParse;
        object = msg->mutable_device_uuid();
        if (size > end - ptr) goto len_delim_till_end;
        ptr += size;
        GOOGLE_PROTOBUF_PARSER_ASSERT(ctx->ParseExactRange(
            {parser_till_end, object}, ptr - size, ptr));
        break;
      }
      default: {
      handle_unusual:
        if ((tag & 7) == 4 || tag == 0) {
          ctx->EndGroup(tag);
          return ptr;
        }
        auto res = UnknownFieldParse(tag, {_InternalParse, msg},
          ptr, end, msg->_internal_metadata_.mutable_unknown_fields(), ctx);
        ptr = res.first;
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr);
        if (res.second) return ptr;
      }
    }  // switch
  }  // while
  return ptr;
len_delim_till_end:
  return ctx->StoreAndTailCall(ptr, end, {_InternalParse, msg},
                               {parser_till_end, object}, size);
}
#else  // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER
bool ManagedDeviceInfo::MergePartialFromCodedStream(
    ::google::protobuf::io::CodedInputStream* input) {
#define DO_(EXPRESSION) if (!PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure
  ::google::protobuf::uint32 tag;
  // @@protoc_insertion_point(parse_start:dmi.ManagedDeviceInfo)
  for (;;) {
    ::std::pair<::google::protobuf::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u);
    tag = p.first;
    if (!p.second) goto handle_unusual;
    switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) {
      // .dmi.ModifiableComponent info = 1;
      case 1: {
        if (static_cast< ::google::protobuf::uint8>(tag) == (10 & 0xFF)) {
          DO_(::google::protobuf::internal::WireFormatLite::ReadMessage(
               input, mutable_info()));
        } else {
          goto handle_unusual;
        }
        break;
      }

      // .dmi.Uuid device_uuid = 2;
      case 2: {
        if (static_cast< ::google::protobuf::uint8>(tag) == (18 & 0xFF)) {
          DO_(::google::protobuf::internal::WireFormatLite::ReadMessage(
               input, mutable_device_uuid()));
        } else {
          goto handle_unusual;
        }
        break;
      }

      default: {
      handle_unusual:
        if (tag == 0) {
          goto success;
        }
        DO_(::google::protobuf::internal::WireFormat::SkipField(
              input, tag, _internal_metadata_.mutable_unknown_fields()));
        break;
      }
    }
  }
success:
  // @@protoc_insertion_point(parse_success:dmi.ManagedDeviceInfo)
  return true;
failure:
  // @@protoc_insertion_point(parse_failure:dmi.ManagedDeviceInfo)
  return false;
#undef DO_
}
#endif  // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER

void ManagedDeviceInfo::SerializeWithCachedSizes(
    ::google::protobuf::io::CodedOutputStream* output) const {
  // @@protoc_insertion_point(serialize_start:dmi.ManagedDeviceInfo)
  ::google::protobuf::uint32 cached_has_bits = 0;
  (void) cached_has_bits;

  // .dmi.ModifiableComponent info = 1;
  if (this->has_info()) {
    ::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray(
      1, HasBitSetters::info(this), output);
  }

  // .dmi.Uuid device_uuid = 2;
  if (this->has_device_uuid()) {
    ::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray(
      2, HasBitSetters::device_uuid(this), output);
  }

  if (_internal_metadata_.have_unknown_fields()) {
    ::google::protobuf::internal::WireFormat::SerializeUnknownFields(
        _internal_metadata_.unknown_fields(), output);
  }
  // @@protoc_insertion_point(serialize_end:dmi.ManagedDeviceInfo)
}

::google::protobuf::uint8* ManagedDeviceInfo::InternalSerializeWithCachedSizesToArray(
    ::google::protobuf::uint8* target) const {
  // @@protoc_insertion_point(serialize_to_array_start:dmi.ManagedDeviceInfo)
  ::google::protobuf::uint32 cached_has_bits = 0;
  (void) cached_has_bits;

  // .dmi.ModifiableComponent info = 1;
  if (this->has_info()) {
    target = ::google::protobuf::internal::WireFormatLite::
      InternalWriteMessageToArray(
        1, HasBitSetters::info(this), target);
  }

  // .dmi.Uuid device_uuid = 2;
  if (this->has_device_uuid()) {
    target = ::google::protobuf::internal::WireFormatLite::
      InternalWriteMessageToArray(
        2, HasBitSetters::device_uuid(this), target);
  }

  if (_internal_metadata_.have_unknown_fields()) {
    target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray(
        _internal_metadata_.unknown_fields(), target);
  }
  // @@protoc_insertion_point(serialize_to_array_end:dmi.ManagedDeviceInfo)
  return target;
}

size_t ManagedDeviceInfo::ByteSizeLong() const {
// @@protoc_insertion_point(message_byte_size_start:dmi.ManagedDeviceInfo)
  size_t total_size = 0;

  if (_internal_metadata_.have_unknown_fields()) {
    total_size +=
      ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize(
        _internal_metadata_.unknown_fields());
  }
  ::google::protobuf::uint32 cached_has_bits = 0;
  // Prevent compiler warnings about cached_has_bits being unused
  (void) cached_has_bits;

  // .dmi.ModifiableComponent info = 1;
  if (this->has_info()) {
    total_size += 1 +
      ::google::protobuf::internal::WireFormatLite::MessageSize(
        *info_);
  }

  // .dmi.Uuid device_uuid = 2;
  if (this->has_device_uuid()) {
    total_size += 1 +
      ::google::protobuf::internal::WireFormatLite::MessageSize(
        *device_uuid_);
  }

  int cached_size = ::google::protobuf::internal::ToCachedSize(total_size);
  SetCachedSize(cached_size);
  return total_size;
}

void ManagedDeviceInfo::MergeFrom(const ::google::protobuf::Message& from) {
// @@protoc_insertion_point(generalized_merge_from_start:dmi.ManagedDeviceInfo)
  GOOGLE_DCHECK_NE(&from, this);
  const ManagedDeviceInfo* source =
      ::google::protobuf::DynamicCastToGenerated<ManagedDeviceInfo>(
          &from);
  if (source == nullptr) {
  // @@protoc_insertion_point(generalized_merge_from_cast_fail:dmi.ManagedDeviceInfo)
    ::google::protobuf::internal::ReflectionOps::Merge(from, this);
  } else {
  // @@protoc_insertion_point(generalized_merge_from_cast_success:dmi.ManagedDeviceInfo)
    MergeFrom(*source);
  }
}

void ManagedDeviceInfo::MergeFrom(const ManagedDeviceInfo& from) {
// @@protoc_insertion_point(class_specific_merge_from_start:dmi.ManagedDeviceInfo)
  GOOGLE_DCHECK_NE(&from, this);
  _internal_metadata_.MergeFrom(from._internal_metadata_);
  ::google::protobuf::uint32 cached_has_bits = 0;
  (void) cached_has_bits;

  if (from.has_info()) {
    mutable_info()->::dmi::ModifiableComponent::MergeFrom(from.info());
  }
  if (from.has_device_uuid()) {
    mutable_device_uuid()->::dmi::Uuid::MergeFrom(from.device_uuid());
  }
}

void ManagedDeviceInfo::CopyFrom(const ::google::protobuf::Message& from) {
// @@protoc_insertion_point(generalized_copy_from_start:dmi.ManagedDeviceInfo)
  if (&from == this) return;
  Clear();
  MergeFrom(from);
}

void ManagedDeviceInfo::CopyFrom(const ManagedDeviceInfo& from) {
// @@protoc_insertion_point(class_specific_copy_from_start:dmi.ManagedDeviceInfo)
  if (&from == this) return;
  Clear();
  MergeFrom(from);
}

bool ManagedDeviceInfo::IsInitialized() const {
  return true;
}

void ManagedDeviceInfo::Swap(ManagedDeviceInfo* other) {
  if (other == this) return;
  InternalSwap(other);
}
void ManagedDeviceInfo::InternalSwap(ManagedDeviceInfo* other) {
  using std::swap;
  _internal_metadata_.Swap(&other->_internal_metadata_);
  swap(info_, other->info_);
  swap(device_uuid_, other->device_uuid_);
}

::google::protobuf::Metadata ManagedDeviceInfo::GetMetadata() const {
  ::google::protobuf::internal::AssignDescriptors(&::assign_descriptors_table_dmi_2fhw_5fmanagement_5fservice_2eproto);
  return ::file_level_metadata_dmi_2fhw_5fmanagement_5fservice_2eproto[kIndexInFileMessages];
}


// ===================================================================

void ManagedDevicesResponse::InitAsDefaultInstance() {
}
class ManagedDevicesResponse::HasBitSetters {
 public:
};

#if !defined(_MSC_VER) || _MSC_VER >= 1900
const int ManagedDevicesResponse::kStatusFieldNumber;
const int ManagedDevicesResponse::kReasonFieldNumber;
const int ManagedDevicesResponse::kDevicesFieldNumber;
#endif  // !defined(_MSC_VER) || _MSC_VER >= 1900

ManagedDevicesResponse::ManagedDevicesResponse()
  : ::google::protobuf::Message(), _internal_metadata_(nullptr) {
  SharedCtor();
  // @@protoc_insertion_point(constructor:dmi.ManagedDevicesResponse)
}
ManagedDevicesResponse::ManagedDevicesResponse(const ManagedDevicesResponse& from)
  : ::google::protobuf::Message(),
      _internal_metadata_(nullptr),
      devices_(from.devices_) {
  _internal_metadata_.MergeFrom(from._internal_metadata_);
  ::memcpy(&status_, &from.status_,
    static_cast<size_t>(reinterpret_cast<char*>(&reason_) -
    reinterpret_cast<char*>(&status_)) + sizeof(reason_));
  // @@protoc_insertion_point(copy_constructor:dmi.ManagedDevicesResponse)
}

void ManagedDevicesResponse::SharedCtor() {
  ::google::protobuf::internal::InitSCC(
      &scc_info_ManagedDevicesResponse_dmi_2fhw_5fmanagement_5fservice_2eproto.base);
  ::memset(&status_, 0, static_cast<size_t>(
      reinterpret_cast<char*>(&reason_) -
      reinterpret_cast<char*>(&status_)) + sizeof(reason_));
}

ManagedDevicesResponse::~ManagedDevicesResponse() {
  // @@protoc_insertion_point(destructor:dmi.ManagedDevicesResponse)
  SharedDtor();
}

void ManagedDevicesResponse::SharedDtor() {
}

void ManagedDevicesResponse::SetCachedSize(int size) const {
  _cached_size_.Set(size);
}
const ManagedDevicesResponse& ManagedDevicesResponse::default_instance() {
  ::google::protobuf::internal::InitSCC(&::scc_info_ManagedDevicesResponse_dmi_2fhw_5fmanagement_5fservice_2eproto.base);
  return *internal_default_instance();
}


void ManagedDevicesResponse::Clear() {
// @@protoc_insertion_point(message_clear_start:dmi.ManagedDevicesResponse)
  ::google::protobuf::uint32 cached_has_bits = 0;
  // Prevent compiler warnings about cached_has_bits being unused
  (void) cached_has_bits;

  devices_.Clear();
  ::memset(&status_, 0, static_cast<size_t>(
      reinterpret_cast<char*>(&reason_) -
      reinterpret_cast<char*>(&status_)) + sizeof(reason_));
  _internal_metadata_.Clear();
}

#if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER
const char* ManagedDevicesResponse::_InternalParse(const char* begin, const char* end, void* object,
                  ::google::protobuf::internal::ParseContext* ctx) {
  auto msg = static_cast<ManagedDevicesResponse*>(object);
  ::google::protobuf::int32 size; (void)size;
  int depth; (void)depth;
  ::google::protobuf::uint32 tag;
  ::google::protobuf::internal::ParseFunc parser_till_end; (void)parser_till_end;
  auto ptr = begin;
  while (ptr < end) {
    ptr = ::google::protobuf::io::Parse32(ptr, &tag);
    GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
    switch (tag >> 3) {
      // .dmi.Status status = 1;
      case 1: {
        if (static_cast<::google::protobuf::uint8>(tag) != 8) goto handle_unusual;
        ::google::protobuf::uint64 val = ::google::protobuf::internal::ReadVarint(&ptr);
        msg->set_status(static_cast<::dmi::Status>(val));
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
        break;
      }
      // .dmi.ManagedDevicesResponse.Reason reason = 2;
      case 2: {
        if (static_cast<::google::protobuf::uint8>(tag) != 16) goto handle_unusual;
        ::google::protobuf::uint64 val = ::google::protobuf::internal::ReadVarint(&ptr);
        msg->set_reason(static_cast<::dmi::ManagedDevicesResponse_Reason>(val));
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
        break;
      }
      // repeated .dmi.ManagedDeviceInfo devices = 3;
      case 3: {
        if (static_cast<::google::protobuf::uint8>(tag) != 26) goto handle_unusual;
        do {
          ptr = ::google::protobuf::io::ReadSize(ptr, &size);
          GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
          parser_till_end = ::dmi::ManagedDeviceInfo::_InternalParse;
          object = msg->add_devices();
          if (size > end - ptr) goto len_delim_till_end;
          ptr += size;
          GOOGLE_PROTOBUF_PARSER_ASSERT(ctx->ParseExactRange(
              {parser_till_end, object}, ptr - size, ptr));
          if (ptr >= end) break;
        } while ((::google::protobuf::io::UnalignedLoad<::google::protobuf::uint64>(ptr) & 255) == 26 && (ptr += 1));
        break;
      }
      default: {
      handle_unusual:
        if ((tag & 7) == 4 || tag == 0) {
          ctx->EndGroup(tag);
          return ptr;
        }
        auto res = UnknownFieldParse(tag, {_InternalParse, msg},
          ptr, end, msg->_internal_metadata_.mutable_unknown_fields(), ctx);
        ptr = res.first;
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr);
        if (res.second) return ptr;
      }
    }  // switch
  }  // while
  return ptr;
len_delim_till_end:
  return ctx->StoreAndTailCall(ptr, end, {_InternalParse, msg},
                               {parser_till_end, object}, size);
}
#else  // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER
bool ManagedDevicesResponse::MergePartialFromCodedStream(
    ::google::protobuf::io::CodedInputStream* input) {
#define DO_(EXPRESSION) if (!PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure
  ::google::protobuf::uint32 tag;
  // @@protoc_insertion_point(parse_start:dmi.ManagedDevicesResponse)
  for (;;) {
    ::std::pair<::google::protobuf::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u);
    tag = p.first;
    if (!p.second) goto handle_unusual;
    switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) {
      // .dmi.Status status = 1;
      case 1: {
        if (static_cast< ::google::protobuf::uint8>(tag) == (8 & 0xFF)) {
          int value = 0;
          DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive<
                   int, ::google::protobuf::internal::WireFormatLite::TYPE_ENUM>(
                 input, &value)));
          set_status(static_cast< ::dmi::Status >(value));
        } else {
          goto handle_unusual;
        }
        break;
      }

      // .dmi.ManagedDevicesResponse.Reason reason = 2;
      case 2: {
        if (static_cast< ::google::protobuf::uint8>(tag) == (16 & 0xFF)) {
          int value = 0;
          DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive<
                   int, ::google::protobuf::internal::WireFormatLite::TYPE_ENUM>(
                 input, &value)));
          set_reason(static_cast< ::dmi::ManagedDevicesResponse_Reason >(value));
        } else {
          goto handle_unusual;
        }
        break;
      }

      // repeated .dmi.ManagedDeviceInfo devices = 3;
      case 3: {
        if (static_cast< ::google::protobuf::uint8>(tag) == (26 & 0xFF)) {
          DO_(::google::protobuf::internal::WireFormatLite::ReadMessage(
                input, add_devices()));
        } else {
          goto handle_unusual;
        }
        break;
      }

      default: {
      handle_unusual:
        if (tag == 0) {
          goto success;
        }
        DO_(::google::protobuf::internal::WireFormat::SkipField(
              input, tag, _internal_metadata_.mutable_unknown_fields()));
        break;
      }
    }
  }
success:
  // @@protoc_insertion_point(parse_success:dmi.ManagedDevicesResponse)
  return true;
failure:
  // @@protoc_insertion_point(parse_failure:dmi.ManagedDevicesResponse)
  return false;
#undef DO_
}
#endif  // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER

void ManagedDevicesResponse::SerializeWithCachedSizes(
    ::google::protobuf::io::CodedOutputStream* output) const {
  // @@protoc_insertion_point(serialize_start:dmi.ManagedDevicesResponse)
  ::google::protobuf::uint32 cached_has_bits = 0;
  (void) cached_has_bits;

  // .dmi.Status status = 1;
  if (this->status() != 0) {
    ::google::protobuf::internal::WireFormatLite::WriteEnum(
      1, this->status(), output);
  }

  // .dmi.ManagedDevicesResponse.Reason reason = 2;
  if (this->reason() != 0) {
    ::google::protobuf::internal::WireFormatLite::WriteEnum(
      2, this->reason(), output);
  }

  // repeated .dmi.ManagedDeviceInfo devices = 3;
  for (unsigned int i = 0,
      n = static_cast<unsigned int>(this->devices_size()); i < n; i++) {
    ::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray(
      3,
      this->devices(static_cast<int>(i)),
      output);
  }

  if (_internal_metadata_.have_unknown_fields()) {
    ::google::protobuf::internal::WireFormat::SerializeUnknownFields(
        _internal_metadata_.unknown_fields(), output);
  }
  // @@protoc_insertion_point(serialize_end:dmi.ManagedDevicesResponse)
}

::google::protobuf::uint8* ManagedDevicesResponse::InternalSerializeWithCachedSizesToArray(
    ::google::protobuf::uint8* target) const {
  // @@protoc_insertion_point(serialize_to_array_start:dmi.ManagedDevicesResponse)
  ::google::protobuf::uint32 cached_has_bits = 0;
  (void) cached_has_bits;

  // .dmi.Status status = 1;
  if (this->status() != 0) {
    target = ::google::protobuf::internal::WireFormatLite::WriteEnumToArray(
      1, this->status(), target);
  }

  // .dmi.ManagedDevicesResponse.Reason reason = 2;
  if (this->reason() != 0) {
    target = ::google::protobuf::internal::WireFormatLite::WriteEnumToArray(
      2, this->reason(), target);
  }

  // repeated .dmi.ManagedDeviceInfo devices = 3;
  for (unsigned int i = 0,
      n = static_cast<unsigned int>(this->devices_size()); i < n; i++) {
    target = ::google::protobuf::internal::WireFormatLite::
      InternalWriteMessageToArray(
        3, this->devices(static_cast<int>(i)), target);
  }

  if (_internal_metadata_.have_unknown_fields()) {
    target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray(
        _internal_metadata_.unknown_fields(), target);
  }
  // @@protoc_insertion_point(serialize_to_array_end:dmi.ManagedDevicesResponse)
  return target;
}

size_t ManagedDevicesResponse::ByteSizeLong() const {
// @@protoc_insertion_point(message_byte_size_start:dmi.ManagedDevicesResponse)
  size_t total_size = 0;

  if (_internal_metadata_.have_unknown_fields()) {
    total_size +=
      ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize(
        _internal_metadata_.unknown_fields());
  }
  ::google::protobuf::uint32 cached_has_bits = 0;
  // Prevent compiler warnings about cached_has_bits being unused
  (void) cached_has_bits;

  // repeated .dmi.ManagedDeviceInfo devices = 3;
  {
    unsigned int count = static_cast<unsigned int>(this->devices_size());
    total_size += 1UL * count;
    for (unsigned int i = 0; i < count; i++) {
      total_size +=
        ::google::protobuf::internal::WireFormatLite::MessageSize(
          this->devices(static_cast<int>(i)));
    }
  }

  // .dmi.Status status = 1;
  if (this->status() != 0) {
    total_size += 1 +
      ::google::protobuf::internal::WireFormatLite::EnumSize(this->status());
  }

  // .dmi.ManagedDevicesResponse.Reason reason = 2;
  if (this->reason() != 0) {
    total_size += 1 +
      ::google::protobuf::internal::WireFormatLite::EnumSize(this->reason());
  }

  int cached_size = ::google::protobuf::internal::ToCachedSize(total_size);
  SetCachedSize(cached_size);
  return total_size;
}

void ManagedDevicesResponse::MergeFrom(const ::google::protobuf::Message& from) {
// @@protoc_insertion_point(generalized_merge_from_start:dmi.ManagedDevicesResponse)
  GOOGLE_DCHECK_NE(&from, this);
  const ManagedDevicesResponse* source =
      ::google::protobuf::DynamicCastToGenerated<ManagedDevicesResponse>(
          &from);
  if (source == nullptr) {
  // @@protoc_insertion_point(generalized_merge_from_cast_fail:dmi.ManagedDevicesResponse)
    ::google::protobuf::internal::ReflectionOps::Merge(from, this);
  } else {
  // @@protoc_insertion_point(generalized_merge_from_cast_success:dmi.ManagedDevicesResponse)
    MergeFrom(*source);
  }
}

void ManagedDevicesResponse::MergeFrom(const ManagedDevicesResponse& from) {
// @@protoc_insertion_point(class_specific_merge_from_start:dmi.ManagedDevicesResponse)
  GOOGLE_DCHECK_NE(&from, this);
  _internal_metadata_.MergeFrom(from._internal_metadata_);
  ::google::protobuf::uint32 cached_has_bits = 0;
  (void) cached_has_bits;

  devices_.MergeFrom(from.devices_);
  if (from.status() != 0) {
    set_status(from.status());
  }
  if (from.reason() != 0) {
    set_reason(from.reason());
  }
}

void ManagedDevicesResponse::CopyFrom(const ::google::protobuf::Message& from) {
// @@protoc_insertion_point(generalized_copy_from_start:dmi.ManagedDevicesResponse)
  if (&from == this) return;
  Clear();
  MergeFrom(from);
}

void ManagedDevicesResponse::CopyFrom(const ManagedDevicesResponse& from) {
// @@protoc_insertion_point(class_specific_copy_from_start:dmi.ManagedDevicesResponse)
  if (&from == this) return;
  Clear();
  MergeFrom(from);
}

bool ManagedDevicesResponse::IsInitialized() const {
  return true;
}

void ManagedDevicesResponse::Swap(ManagedDevicesResponse* other) {
  if (other == this) return;
  InternalSwap(other);
}
void ManagedDevicesResponse::InternalSwap(ManagedDevicesResponse* other) {
  using std::swap;
  _internal_metadata_.Swap(&other->_internal_metadata_);
  CastToBase(&devices_)->InternalSwap(CastToBase(&other->devices_));
  swap(status_, other->status_);
  swap(reason_, other->reason_);
}

::google::protobuf::Metadata ManagedDevicesResponse::GetMetadata() const {
  ::google::protobuf::internal::AssignDescriptors(&::assign_descriptors_table_dmi_2fhw_5fmanagement_5fservice_2eproto);
  return ::file_level_metadata_dmi_2fhw_5fmanagement_5fservice_2eproto[kIndexInFileMessages];
}


// ===================================================================

void SetLoggingEndpointRequest::InitAsDefaultInstance() {
  ::dmi::_SetLoggingEndpointRequest_default_instance_._instance.get_mutable()->device_uuid_ = const_cast< ::dmi::Uuid*>(
      ::dmi::Uuid::internal_default_instance());
}
class SetLoggingEndpointRequest::HasBitSetters {
 public:
  static const ::dmi::Uuid& device_uuid(const SetLoggingEndpointRequest* msg);
};

const ::dmi::Uuid&
SetLoggingEndpointRequest::HasBitSetters::device_uuid(const SetLoggingEndpointRequest* msg) {
  return *msg->device_uuid_;
}
void SetLoggingEndpointRequest::clear_device_uuid() {
  if (GetArenaNoVirtual() == nullptr && device_uuid_ != nullptr) {
    delete device_uuid_;
  }
  device_uuid_ = nullptr;
}
#if !defined(_MSC_VER) || _MSC_VER >= 1900
const int SetLoggingEndpointRequest::kDeviceUuidFieldNumber;
const int SetLoggingEndpointRequest::kLoggingEndpointFieldNumber;
const int SetLoggingEndpointRequest::kLoggingProtocolFieldNumber;
#endif  // !defined(_MSC_VER) || _MSC_VER >= 1900

SetLoggingEndpointRequest::SetLoggingEndpointRequest()
  : ::google::protobuf::Message(), _internal_metadata_(nullptr) {
  SharedCtor();
  // @@protoc_insertion_point(constructor:dmi.SetLoggingEndpointRequest)
}
SetLoggingEndpointRequest::SetLoggingEndpointRequest(const SetLoggingEndpointRequest& from)
  : ::google::protobuf::Message(),
      _internal_metadata_(nullptr) {
  _internal_metadata_.MergeFrom(from._internal_metadata_);
  logging_endpoint_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
  if (from.logging_endpoint().size() > 0) {
    logging_endpoint_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.logging_endpoint_);
  }
  logging_protocol_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
  if (from.logging_protocol().size() > 0) {
    logging_protocol_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.logging_protocol_);
  }
  if (from.has_device_uuid()) {
    device_uuid_ = new ::dmi::Uuid(*from.device_uuid_);
  } else {
    device_uuid_ = nullptr;
  }
  // @@protoc_insertion_point(copy_constructor:dmi.SetLoggingEndpointRequest)
}

void SetLoggingEndpointRequest::SharedCtor() {
  ::google::protobuf::internal::InitSCC(
      &scc_info_SetLoggingEndpointRequest_dmi_2fhw_5fmanagement_5fservice_2eproto.base);
  logging_endpoint_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
  logging_protocol_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
  device_uuid_ = nullptr;
}

SetLoggingEndpointRequest::~SetLoggingEndpointRequest() {
  // @@protoc_insertion_point(destructor:dmi.SetLoggingEndpointRequest)
  SharedDtor();
}

void SetLoggingEndpointRequest::SharedDtor() {
  logging_endpoint_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
  logging_protocol_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
  if (this != internal_default_instance()) delete device_uuid_;
}

void SetLoggingEndpointRequest::SetCachedSize(int size) const {
  _cached_size_.Set(size);
}
const SetLoggingEndpointRequest& SetLoggingEndpointRequest::default_instance() {
  ::google::protobuf::internal::InitSCC(&::scc_info_SetLoggingEndpointRequest_dmi_2fhw_5fmanagement_5fservice_2eproto.base);
  return *internal_default_instance();
}


void SetLoggingEndpointRequest::Clear() {
// @@protoc_insertion_point(message_clear_start:dmi.SetLoggingEndpointRequest)
  ::google::protobuf::uint32 cached_has_bits = 0;
  // Prevent compiler warnings about cached_has_bits being unused
  (void) cached_has_bits;

  logging_endpoint_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
  logging_protocol_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
  if (GetArenaNoVirtual() == nullptr && device_uuid_ != nullptr) {
    delete device_uuid_;
  }
  device_uuid_ = nullptr;
  _internal_metadata_.Clear();
}

#if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER
const char* SetLoggingEndpointRequest::_InternalParse(const char* begin, const char* end, void* object,
                  ::google::protobuf::internal::ParseContext* ctx) {
  auto msg = static_cast<SetLoggingEndpointRequest*>(object);
  ::google::protobuf::int32 size; (void)size;
  int depth; (void)depth;
  ::google::protobuf::uint32 tag;
  ::google::protobuf::internal::ParseFunc parser_till_end; (void)parser_till_end;
  auto ptr = begin;
  while (ptr < end) {
    ptr = ::google::protobuf::io::Parse32(ptr, &tag);
    GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
    switch (tag >> 3) {
      // .dmi.Uuid device_uuid = 1;
      case 1: {
        if (static_cast<::google::protobuf::uint8>(tag) != 10) goto handle_unusual;
        ptr = ::google::protobuf::io::ReadSize(ptr, &size);
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
        parser_till_end = ::dmi::Uuid::_InternalParse;
        object = msg->mutable_device_uuid();
        if (size > end - ptr) goto len_delim_till_end;
        ptr += size;
        GOOGLE_PROTOBUF_PARSER_ASSERT(ctx->ParseExactRange(
            {parser_till_end, object}, ptr - size, ptr));
        break;
      }
      // string logging_endpoint = 2;
      case 2: {
        if (static_cast<::google::protobuf::uint8>(tag) != 18) goto handle_unusual;
        ptr = ::google::protobuf::io::ReadSize(ptr, &size);
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
        ctx->extra_parse_data().SetFieldName("dmi.SetLoggingEndpointRequest.logging_endpoint");
        object = msg->mutable_logging_endpoint();
        if (size > end - ptr + ::google::protobuf::internal::ParseContext::kSlopBytes) {
          parser_till_end = ::google::protobuf::internal::GreedyStringParserUTF8;
          goto string_till_end;
        }
        GOOGLE_PROTOBUF_PARSER_ASSERT(::google::protobuf::internal::StringCheckUTF8(ptr, size, ctx));
        ::google::protobuf::internal::InlineGreedyStringParser(object, ptr, size, ctx);
        ptr += size;
        break;
      }
      // string logging_protocol = 3;
      case 3: {
        if (static_cast<::google::protobuf::uint8>(tag) != 26) goto handle_unusual;
        ptr = ::google::protobuf::io::ReadSize(ptr, &size);
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
        ctx->extra_parse_data().SetFieldName("dmi.SetLoggingEndpointRequest.logging_protocol");
        object = msg->mutable_logging_protocol();
        if (size > end - ptr + ::google::protobuf::internal::ParseContext::kSlopBytes) {
          parser_till_end = ::google::protobuf::internal::GreedyStringParserUTF8;
          goto string_till_end;
        }
        GOOGLE_PROTOBUF_PARSER_ASSERT(::google::protobuf::internal::StringCheckUTF8(ptr, size, ctx));
        ::google::protobuf::internal::InlineGreedyStringParser(object, ptr, size, ctx);
        ptr += size;
        break;
      }
      default: {
      handle_unusual:
        if ((tag & 7) == 4 || tag == 0) {
          ctx->EndGroup(tag);
          return ptr;
        }
        auto res = UnknownFieldParse(tag, {_InternalParse, msg},
          ptr, end, msg->_internal_metadata_.mutable_unknown_fields(), ctx);
        ptr = res.first;
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr);
        if (res.second) return ptr;
      }
    }  // switch
  }  // while
  return ptr;
string_till_end:
  static_cast<::std::string*>(object)->clear();
  static_cast<::std::string*>(object)->reserve(size);
  goto len_delim_till_end;
len_delim_till_end:
  return ctx->StoreAndTailCall(ptr, end, {_InternalParse, msg},
                               {parser_till_end, object}, size);
}
#else  // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER
bool SetLoggingEndpointRequest::MergePartialFromCodedStream(
    ::google::protobuf::io::CodedInputStream* input) {
#define DO_(EXPRESSION) if (!PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure
  ::google::protobuf::uint32 tag;
  // @@protoc_insertion_point(parse_start:dmi.SetLoggingEndpointRequest)
  for (;;) {
    ::std::pair<::google::protobuf::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u);
    tag = p.first;
    if (!p.second) goto handle_unusual;
    switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) {
      // .dmi.Uuid device_uuid = 1;
      case 1: {
        if (static_cast< ::google::protobuf::uint8>(tag) == (10 & 0xFF)) {
          DO_(::google::protobuf::internal::WireFormatLite::ReadMessage(
               input, mutable_device_uuid()));
        } else {
          goto handle_unusual;
        }
        break;
      }

      // string logging_endpoint = 2;
      case 2: {
        if (static_cast< ::google::protobuf::uint8>(tag) == (18 & 0xFF)) {
          DO_(::google::protobuf::internal::WireFormatLite::ReadString(
                input, this->mutable_logging_endpoint()));
          DO_(::google::protobuf::internal::WireFormatLite::VerifyUtf8String(
            this->logging_endpoint().data(), static_cast<int>(this->logging_endpoint().length()),
            ::google::protobuf::internal::WireFormatLite::PARSE,
            "dmi.SetLoggingEndpointRequest.logging_endpoint"));
        } else {
          goto handle_unusual;
        }
        break;
      }

      // string logging_protocol = 3;
      case 3: {
        if (static_cast< ::google::protobuf::uint8>(tag) == (26 & 0xFF)) {
          DO_(::google::protobuf::internal::WireFormatLite::ReadString(
                input, this->mutable_logging_protocol()));
          DO_(::google::protobuf::internal::WireFormatLite::VerifyUtf8String(
            this->logging_protocol().data(), static_cast<int>(this->logging_protocol().length()),
            ::google::protobuf::internal::WireFormatLite::PARSE,
            "dmi.SetLoggingEndpointRequest.logging_protocol"));
        } else {
          goto handle_unusual;
        }
        break;
      }

      default: {
      handle_unusual:
        if (tag == 0) {
          goto success;
        }
        DO_(::google::protobuf::internal::WireFormat::SkipField(
              input, tag, _internal_metadata_.mutable_unknown_fields()));
        break;
      }
    }
  }
success:
  // @@protoc_insertion_point(parse_success:dmi.SetLoggingEndpointRequest)
  return true;
failure:
  // @@protoc_insertion_point(parse_failure:dmi.SetLoggingEndpointRequest)
  return false;
#undef DO_
}
#endif  // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER

void SetLoggingEndpointRequest::SerializeWithCachedSizes(
    ::google::protobuf::io::CodedOutputStream* output) const {
  // @@protoc_insertion_point(serialize_start:dmi.SetLoggingEndpointRequest)
  ::google::protobuf::uint32 cached_has_bits = 0;
  (void) cached_has_bits;

  // .dmi.Uuid device_uuid = 1;
  if (this->has_device_uuid()) {
    ::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray(
      1, HasBitSetters::device_uuid(this), output);
  }

  // string logging_endpoint = 2;
  if (this->logging_endpoint().size() > 0) {
    ::google::protobuf::internal::WireFormatLite::VerifyUtf8String(
      this->logging_endpoint().data(), static_cast<int>(this->logging_endpoint().length()),
      ::google::protobuf::internal::WireFormatLite::SERIALIZE,
      "dmi.SetLoggingEndpointRequest.logging_endpoint");
    ::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased(
      2, this->logging_endpoint(), output);
  }

  // string logging_protocol = 3;
  if (this->logging_protocol().size() > 0) {
    ::google::protobuf::internal::WireFormatLite::VerifyUtf8String(
      this->logging_protocol().data(), static_cast<int>(this->logging_protocol().length()),
      ::google::protobuf::internal::WireFormatLite::SERIALIZE,
      "dmi.SetLoggingEndpointRequest.logging_protocol");
    ::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased(
      3, this->logging_protocol(), output);
  }

  if (_internal_metadata_.have_unknown_fields()) {
    ::google::protobuf::internal::WireFormat::SerializeUnknownFields(
        _internal_metadata_.unknown_fields(), output);
  }
  // @@protoc_insertion_point(serialize_end:dmi.SetLoggingEndpointRequest)
}

::google::protobuf::uint8* SetLoggingEndpointRequest::InternalSerializeWithCachedSizesToArray(
    ::google::protobuf::uint8* target) const {
  // @@protoc_insertion_point(serialize_to_array_start:dmi.SetLoggingEndpointRequest)
  ::google::protobuf::uint32 cached_has_bits = 0;
  (void) cached_has_bits;

  // .dmi.Uuid device_uuid = 1;
  if (this->has_device_uuid()) {
    target = ::google::protobuf::internal::WireFormatLite::
      InternalWriteMessageToArray(
        1, HasBitSetters::device_uuid(this), target);
  }

  // string logging_endpoint = 2;
  if (this->logging_endpoint().size() > 0) {
    ::google::protobuf::internal::WireFormatLite::VerifyUtf8String(
      this->logging_endpoint().data(), static_cast<int>(this->logging_endpoint().length()),
      ::google::protobuf::internal::WireFormatLite::SERIALIZE,
      "dmi.SetLoggingEndpointRequest.logging_endpoint");
    target =
      ::google::protobuf::internal::WireFormatLite::WriteStringToArray(
        2, this->logging_endpoint(), target);
  }

  // string logging_protocol = 3;
  if (this->logging_protocol().size() > 0) {
    ::google::protobuf::internal::WireFormatLite::VerifyUtf8String(
      this->logging_protocol().data(), static_cast<int>(this->logging_protocol().length()),
      ::google::protobuf::internal::WireFormatLite::SERIALIZE,
      "dmi.SetLoggingEndpointRequest.logging_protocol");
    target =
      ::google::protobuf::internal::WireFormatLite::WriteStringToArray(
        3, this->logging_protocol(), target);
  }

  if (_internal_metadata_.have_unknown_fields()) {
    target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray(
        _internal_metadata_.unknown_fields(), target);
  }
  // @@protoc_insertion_point(serialize_to_array_end:dmi.SetLoggingEndpointRequest)
  return target;
}

size_t SetLoggingEndpointRequest::ByteSizeLong() const {
// @@protoc_insertion_point(message_byte_size_start:dmi.SetLoggingEndpointRequest)
  size_t total_size = 0;

  if (_internal_metadata_.have_unknown_fields()) {
    total_size +=
      ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize(
        _internal_metadata_.unknown_fields());
  }
  ::google::protobuf::uint32 cached_has_bits = 0;
  // Prevent compiler warnings about cached_has_bits being unused
  (void) cached_has_bits;

  // string logging_endpoint = 2;
  if (this->logging_endpoint().size() > 0) {
    total_size += 1 +
      ::google::protobuf::internal::WireFormatLite::StringSize(
        this->logging_endpoint());
  }

  // string logging_protocol = 3;
  if (this->logging_protocol().size() > 0) {
    total_size += 1 +
      ::google::protobuf::internal::WireFormatLite::StringSize(
        this->logging_protocol());
  }

  // .dmi.Uuid device_uuid = 1;
  if (this->has_device_uuid()) {
    total_size += 1 +
      ::google::protobuf::internal::WireFormatLite::MessageSize(
        *device_uuid_);
  }

  int cached_size = ::google::protobuf::internal::ToCachedSize(total_size);
  SetCachedSize(cached_size);
  return total_size;
}

void SetLoggingEndpointRequest::MergeFrom(const ::google::protobuf::Message& from) {
// @@protoc_insertion_point(generalized_merge_from_start:dmi.SetLoggingEndpointRequest)
  GOOGLE_DCHECK_NE(&from, this);
  const SetLoggingEndpointRequest* source =
      ::google::protobuf::DynamicCastToGenerated<SetLoggingEndpointRequest>(
          &from);
  if (source == nullptr) {
  // @@protoc_insertion_point(generalized_merge_from_cast_fail:dmi.SetLoggingEndpointRequest)
    ::google::protobuf::internal::ReflectionOps::Merge(from, this);
  } else {
  // @@protoc_insertion_point(generalized_merge_from_cast_success:dmi.SetLoggingEndpointRequest)
    MergeFrom(*source);
  }
}

void SetLoggingEndpointRequest::MergeFrom(const SetLoggingEndpointRequest& from) {
// @@protoc_insertion_point(class_specific_merge_from_start:dmi.SetLoggingEndpointRequest)
  GOOGLE_DCHECK_NE(&from, this);
  _internal_metadata_.MergeFrom(from._internal_metadata_);
  ::google::protobuf::uint32 cached_has_bits = 0;
  (void) cached_has_bits;

  if (from.logging_endpoint().size() > 0) {

    logging_endpoint_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.logging_endpoint_);
  }
  if (from.logging_protocol().size() > 0) {

    logging_protocol_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.logging_protocol_);
  }
  if (from.has_device_uuid()) {
    mutable_device_uuid()->::dmi::Uuid::MergeFrom(from.device_uuid());
  }
}

void SetLoggingEndpointRequest::CopyFrom(const ::google::protobuf::Message& from) {
// @@protoc_insertion_point(generalized_copy_from_start:dmi.SetLoggingEndpointRequest)
  if (&from == this) return;
  Clear();
  MergeFrom(from);
}

void SetLoggingEndpointRequest::CopyFrom(const SetLoggingEndpointRequest& from) {
// @@protoc_insertion_point(class_specific_copy_from_start:dmi.SetLoggingEndpointRequest)
  if (&from == this) return;
  Clear();
  MergeFrom(from);
}

bool SetLoggingEndpointRequest::IsInitialized() const {
  return true;
}

void SetLoggingEndpointRequest::Swap(SetLoggingEndpointRequest* other) {
  if (other == this) return;
  InternalSwap(other);
}
void SetLoggingEndpointRequest::InternalSwap(SetLoggingEndpointRequest* other) {
  using std::swap;
  _internal_metadata_.Swap(&other->_internal_metadata_);
  logging_endpoint_.Swap(&other->logging_endpoint_, &::google::protobuf::internal::GetEmptyStringAlreadyInited(),
    GetArenaNoVirtual());
  logging_protocol_.Swap(&other->logging_protocol_, &::google::protobuf::internal::GetEmptyStringAlreadyInited(),
    GetArenaNoVirtual());
  swap(device_uuid_, other->device_uuid_);
}

::google::protobuf::Metadata SetLoggingEndpointRequest::GetMetadata() const {
  ::google::protobuf::internal::AssignDescriptors(&::assign_descriptors_table_dmi_2fhw_5fmanagement_5fservice_2eproto);
  return ::file_level_metadata_dmi_2fhw_5fmanagement_5fservice_2eproto[kIndexInFileMessages];
}


// ===================================================================

void SetRemoteEndpointResponse::InitAsDefaultInstance() {
}
class SetRemoteEndpointResponse::HasBitSetters {
 public:
};

#if !defined(_MSC_VER) || _MSC_VER >= 1900
const int SetRemoteEndpointResponse::kStatusFieldNumber;
const int SetRemoteEndpointResponse::kReasonFieldNumber;
const int SetRemoteEndpointResponse::kReasonDetailFieldNumber;
#endif  // !defined(_MSC_VER) || _MSC_VER >= 1900

SetRemoteEndpointResponse::SetRemoteEndpointResponse()
  : ::google::protobuf::Message(), _internal_metadata_(nullptr) {
  SharedCtor();
  // @@protoc_insertion_point(constructor:dmi.SetRemoteEndpointResponse)
}
SetRemoteEndpointResponse::SetRemoteEndpointResponse(const SetRemoteEndpointResponse& from)
  : ::google::protobuf::Message(),
      _internal_metadata_(nullptr) {
  _internal_metadata_.MergeFrom(from._internal_metadata_);
  reason_detail_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
  if (from.reason_detail().size() > 0) {
    reason_detail_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.reason_detail_);
  }
  ::memcpy(&status_, &from.status_,
    static_cast<size_t>(reinterpret_cast<char*>(&reason_) -
    reinterpret_cast<char*>(&status_)) + sizeof(reason_));
  // @@protoc_insertion_point(copy_constructor:dmi.SetRemoteEndpointResponse)
}

void SetRemoteEndpointResponse::SharedCtor() {
  ::google::protobuf::internal::InitSCC(
      &scc_info_SetRemoteEndpointResponse_dmi_2fhw_5fmanagement_5fservice_2eproto.base);
  reason_detail_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
  ::memset(&status_, 0, static_cast<size_t>(
      reinterpret_cast<char*>(&reason_) -
      reinterpret_cast<char*>(&status_)) + sizeof(reason_));
}

SetRemoteEndpointResponse::~SetRemoteEndpointResponse() {
  // @@protoc_insertion_point(destructor:dmi.SetRemoteEndpointResponse)
  SharedDtor();
}

void SetRemoteEndpointResponse::SharedDtor() {
  reason_detail_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}

void SetRemoteEndpointResponse::SetCachedSize(int size) const {
  _cached_size_.Set(size);
}
const SetRemoteEndpointResponse& SetRemoteEndpointResponse::default_instance() {
  ::google::protobuf::internal::InitSCC(&::scc_info_SetRemoteEndpointResponse_dmi_2fhw_5fmanagement_5fservice_2eproto.base);
  return *internal_default_instance();
}


void SetRemoteEndpointResponse::Clear() {
// @@protoc_insertion_point(message_clear_start:dmi.SetRemoteEndpointResponse)
  ::google::protobuf::uint32 cached_has_bits = 0;
  // Prevent compiler warnings about cached_has_bits being unused
  (void) cached_has_bits;

  reason_detail_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
  ::memset(&status_, 0, static_cast<size_t>(
      reinterpret_cast<char*>(&reason_) -
      reinterpret_cast<char*>(&status_)) + sizeof(reason_));
  _internal_metadata_.Clear();
}

#if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER
const char* SetRemoteEndpointResponse::_InternalParse(const char* begin, const char* end, void* object,
                  ::google::protobuf::internal::ParseContext* ctx) {
  auto msg = static_cast<SetRemoteEndpointResponse*>(object);
  ::google::protobuf::int32 size; (void)size;
  int depth; (void)depth;
  ::google::protobuf::uint32 tag;
  ::google::protobuf::internal::ParseFunc parser_till_end; (void)parser_till_end;
  auto ptr = begin;
  while (ptr < end) {
    ptr = ::google::protobuf::io::Parse32(ptr, &tag);
    GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
    switch (tag >> 3) {
      // .dmi.Status status = 1;
      case 1: {
        if (static_cast<::google::protobuf::uint8>(tag) != 8) goto handle_unusual;
        ::google::protobuf::uint64 val = ::google::protobuf::internal::ReadVarint(&ptr);
        msg->set_status(static_cast<::dmi::Status>(val));
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
        break;
      }
      // .dmi.SetRemoteEndpointResponse.Reason reason = 2;
      case 2: {
        if (static_cast<::google::protobuf::uint8>(tag) != 16) goto handle_unusual;
        ::google::protobuf::uint64 val = ::google::protobuf::internal::ReadVarint(&ptr);
        msg->set_reason(static_cast<::dmi::SetRemoteEndpointResponse_Reason>(val));
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
        break;
      }
      // string reason_detail = 3;
      case 3: {
        if (static_cast<::google::protobuf::uint8>(tag) != 26) goto handle_unusual;
        ptr = ::google::protobuf::io::ReadSize(ptr, &size);
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
        ctx->extra_parse_data().SetFieldName("dmi.SetRemoteEndpointResponse.reason_detail");
        object = msg->mutable_reason_detail();
        if (size > end - ptr + ::google::protobuf::internal::ParseContext::kSlopBytes) {
          parser_till_end = ::google::protobuf::internal::GreedyStringParserUTF8;
          goto string_till_end;
        }
        GOOGLE_PROTOBUF_PARSER_ASSERT(::google::protobuf::internal::StringCheckUTF8(ptr, size, ctx));
        ::google::protobuf::internal::InlineGreedyStringParser(object, ptr, size, ctx);
        ptr += size;
        break;
      }
      default: {
      handle_unusual:
        if ((tag & 7) == 4 || tag == 0) {
          ctx->EndGroup(tag);
          return ptr;
        }
        auto res = UnknownFieldParse(tag, {_InternalParse, msg},
          ptr, end, msg->_internal_metadata_.mutable_unknown_fields(), ctx);
        ptr = res.first;
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr);
        if (res.second) return ptr;
      }
    }  // switch
  }  // while
  return ptr;
string_till_end:
  static_cast<::std::string*>(object)->clear();
  static_cast<::std::string*>(object)->reserve(size);
  goto len_delim_till_end;
len_delim_till_end:
  return ctx->StoreAndTailCall(ptr, end, {_InternalParse, msg},
                               {parser_till_end, object}, size);
}
#else  // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER
bool SetRemoteEndpointResponse::MergePartialFromCodedStream(
    ::google::protobuf::io::CodedInputStream* input) {
#define DO_(EXPRESSION) if (!PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure
  ::google::protobuf::uint32 tag;
  // @@protoc_insertion_point(parse_start:dmi.SetRemoteEndpointResponse)
  for (;;) {
    ::std::pair<::google::protobuf::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u);
    tag = p.first;
    if (!p.second) goto handle_unusual;
    switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) {
      // .dmi.Status status = 1;
      case 1: {
        if (static_cast< ::google::protobuf::uint8>(tag) == (8 & 0xFF)) {
          int value = 0;
          DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive<
                   int, ::google::protobuf::internal::WireFormatLite::TYPE_ENUM>(
                 input, &value)));
          set_status(static_cast< ::dmi::Status >(value));
        } else {
          goto handle_unusual;
        }
        break;
      }

      // .dmi.SetRemoteEndpointResponse.Reason reason = 2;
      case 2: {
        if (static_cast< ::google::protobuf::uint8>(tag) == (16 & 0xFF)) {
          int value = 0;
          DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive<
                   int, ::google::protobuf::internal::WireFormatLite::TYPE_ENUM>(
                 input, &value)));
          set_reason(static_cast< ::dmi::SetRemoteEndpointResponse_Reason >(value));
        } else {
          goto handle_unusual;
        }
        break;
      }

      // string reason_detail = 3;
      case 3: {
        if (static_cast< ::google::protobuf::uint8>(tag) == (26 & 0xFF)) {
          DO_(::google::protobuf::internal::WireFormatLite::ReadString(
                input, this->mutable_reason_detail()));
          DO_(::google::protobuf::internal::WireFormatLite::VerifyUtf8String(
            this->reason_detail().data(), static_cast<int>(this->reason_detail().length()),
            ::google::protobuf::internal::WireFormatLite::PARSE,
            "dmi.SetRemoteEndpointResponse.reason_detail"));
        } else {
          goto handle_unusual;
        }
        break;
      }

      default: {
      handle_unusual:
        if (tag == 0) {
          goto success;
        }
        DO_(::google::protobuf::internal::WireFormat::SkipField(
              input, tag, _internal_metadata_.mutable_unknown_fields()));
        break;
      }
    }
  }
success:
  // @@protoc_insertion_point(parse_success:dmi.SetRemoteEndpointResponse)
  return true;
failure:
  // @@protoc_insertion_point(parse_failure:dmi.SetRemoteEndpointResponse)
  return false;
#undef DO_
}
#endif  // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER

void SetRemoteEndpointResponse::SerializeWithCachedSizes(
    ::google::protobuf::io::CodedOutputStream* output) const {
  // @@protoc_insertion_point(serialize_start:dmi.SetRemoteEndpointResponse)
  ::google::protobuf::uint32 cached_has_bits = 0;
  (void) cached_has_bits;

  // .dmi.Status status = 1;
  if (this->status() != 0) {
    ::google::protobuf::internal::WireFormatLite::WriteEnum(
      1, this->status(), output);
  }

  // .dmi.SetRemoteEndpointResponse.Reason reason = 2;
  if (this->reason() != 0) {
    ::google::protobuf::internal::WireFormatLite::WriteEnum(
      2, this->reason(), output);
  }

  // string reason_detail = 3;
  if (this->reason_detail().size() > 0) {
    ::google::protobuf::internal::WireFormatLite::VerifyUtf8String(
      this->reason_detail().data(), static_cast<int>(this->reason_detail().length()),
      ::google::protobuf::internal::WireFormatLite::SERIALIZE,
      "dmi.SetRemoteEndpointResponse.reason_detail");
    ::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased(
      3, this->reason_detail(), output);
  }

  if (_internal_metadata_.have_unknown_fields()) {
    ::google::protobuf::internal::WireFormat::SerializeUnknownFields(
        _internal_metadata_.unknown_fields(), output);
  }
  // @@protoc_insertion_point(serialize_end:dmi.SetRemoteEndpointResponse)
}

::google::protobuf::uint8* SetRemoteEndpointResponse::InternalSerializeWithCachedSizesToArray(
    ::google::protobuf::uint8* target) const {
  // @@protoc_insertion_point(serialize_to_array_start:dmi.SetRemoteEndpointResponse)
  ::google::protobuf::uint32 cached_has_bits = 0;
  (void) cached_has_bits;

  // .dmi.Status status = 1;
  if (this->status() != 0) {
    target = ::google::protobuf::internal::WireFormatLite::WriteEnumToArray(
      1, this->status(), target);
  }

  // .dmi.SetRemoteEndpointResponse.Reason reason = 2;
  if (this->reason() != 0) {
    target = ::google::protobuf::internal::WireFormatLite::WriteEnumToArray(
      2, this->reason(), target);
  }

  // string reason_detail = 3;
  if (this->reason_detail().size() > 0) {
    ::google::protobuf::internal::WireFormatLite::VerifyUtf8String(
      this->reason_detail().data(), static_cast<int>(this->reason_detail().length()),
      ::google::protobuf::internal::WireFormatLite::SERIALIZE,
      "dmi.SetRemoteEndpointResponse.reason_detail");
    target =
      ::google::protobuf::internal::WireFormatLite::WriteStringToArray(
        3, this->reason_detail(), target);
  }

  if (_internal_metadata_.have_unknown_fields()) {
    target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray(
        _internal_metadata_.unknown_fields(), target);
  }
  // @@protoc_insertion_point(serialize_to_array_end:dmi.SetRemoteEndpointResponse)
  return target;
}

size_t SetRemoteEndpointResponse::ByteSizeLong() const {
// @@protoc_insertion_point(message_byte_size_start:dmi.SetRemoteEndpointResponse)
  size_t total_size = 0;

  if (_internal_metadata_.have_unknown_fields()) {
    total_size +=
      ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize(
        _internal_metadata_.unknown_fields());
  }
  ::google::protobuf::uint32 cached_has_bits = 0;
  // Prevent compiler warnings about cached_has_bits being unused
  (void) cached_has_bits;

  // string reason_detail = 3;
  if (this->reason_detail().size() > 0) {
    total_size += 1 +
      ::google::protobuf::internal::WireFormatLite::StringSize(
        this->reason_detail());
  }

  // .dmi.Status status = 1;
  if (this->status() != 0) {
    total_size += 1 +
      ::google::protobuf::internal::WireFormatLite::EnumSize(this->status());
  }

  // .dmi.SetRemoteEndpointResponse.Reason reason = 2;
  if (this->reason() != 0) {
    total_size += 1 +
      ::google::protobuf::internal::WireFormatLite::EnumSize(this->reason());
  }

  int cached_size = ::google::protobuf::internal::ToCachedSize(total_size);
  SetCachedSize(cached_size);
  return total_size;
}

void SetRemoteEndpointResponse::MergeFrom(const ::google::protobuf::Message& from) {
// @@protoc_insertion_point(generalized_merge_from_start:dmi.SetRemoteEndpointResponse)
  GOOGLE_DCHECK_NE(&from, this);
  const SetRemoteEndpointResponse* source =
      ::google::protobuf::DynamicCastToGenerated<SetRemoteEndpointResponse>(
          &from);
  if (source == nullptr) {
  // @@protoc_insertion_point(generalized_merge_from_cast_fail:dmi.SetRemoteEndpointResponse)
    ::google::protobuf::internal::ReflectionOps::Merge(from, this);
  } else {
  // @@protoc_insertion_point(generalized_merge_from_cast_success:dmi.SetRemoteEndpointResponse)
    MergeFrom(*source);
  }
}

void SetRemoteEndpointResponse::MergeFrom(const SetRemoteEndpointResponse& from) {
// @@protoc_insertion_point(class_specific_merge_from_start:dmi.SetRemoteEndpointResponse)
  GOOGLE_DCHECK_NE(&from, this);
  _internal_metadata_.MergeFrom(from._internal_metadata_);
  ::google::protobuf::uint32 cached_has_bits = 0;
  (void) cached_has_bits;

  if (from.reason_detail().size() > 0) {

    reason_detail_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.reason_detail_);
  }
  if (from.status() != 0) {
    set_status(from.status());
  }
  if (from.reason() != 0) {
    set_reason(from.reason());
  }
}

void SetRemoteEndpointResponse::CopyFrom(const ::google::protobuf::Message& from) {
// @@protoc_insertion_point(generalized_copy_from_start:dmi.SetRemoteEndpointResponse)
  if (&from == this) return;
  Clear();
  MergeFrom(from);
}

void SetRemoteEndpointResponse::CopyFrom(const SetRemoteEndpointResponse& from) {
// @@protoc_insertion_point(class_specific_copy_from_start:dmi.SetRemoteEndpointResponse)
  if (&from == this) return;
  Clear();
  MergeFrom(from);
}

bool SetRemoteEndpointResponse::IsInitialized() const {
  return true;
}

void SetRemoteEndpointResponse::Swap(SetRemoteEndpointResponse* other) {
  if (other == this) return;
  InternalSwap(other);
}
void SetRemoteEndpointResponse::InternalSwap(SetRemoteEndpointResponse* other) {
  using std::swap;
  _internal_metadata_.Swap(&other->_internal_metadata_);
  reason_detail_.Swap(&other->reason_detail_, &::google::protobuf::internal::GetEmptyStringAlreadyInited(),
    GetArenaNoVirtual());
  swap(status_, other->status_);
  swap(reason_, other->reason_);
}

::google::protobuf::Metadata SetRemoteEndpointResponse::GetMetadata() const {
  ::google::protobuf::internal::AssignDescriptors(&::assign_descriptors_table_dmi_2fhw_5fmanagement_5fservice_2eproto);
  return ::file_level_metadata_dmi_2fhw_5fmanagement_5fservice_2eproto[kIndexInFileMessages];
}


// ===================================================================

void GetLoggingEndpointResponse::InitAsDefaultInstance() {
}
class GetLoggingEndpointResponse::HasBitSetters {
 public:
};

#if !defined(_MSC_VER) || _MSC_VER >= 1900
const int GetLoggingEndpointResponse::kStatusFieldNumber;
const int GetLoggingEndpointResponse::kReasonFieldNumber;
const int GetLoggingEndpointResponse::kLoggingEndpointFieldNumber;
const int GetLoggingEndpointResponse::kLoggingProtocolFieldNumber;
const int GetLoggingEndpointResponse::kReasonDetailFieldNumber;
#endif  // !defined(_MSC_VER) || _MSC_VER >= 1900

GetLoggingEndpointResponse::GetLoggingEndpointResponse()
  : ::google::protobuf::Message(), _internal_metadata_(nullptr) {
  SharedCtor();
  // @@protoc_insertion_point(constructor:dmi.GetLoggingEndpointResponse)
}
GetLoggingEndpointResponse::GetLoggingEndpointResponse(const GetLoggingEndpointResponse& from)
  : ::google::protobuf::Message(),
      _internal_metadata_(nullptr) {
  _internal_metadata_.MergeFrom(from._internal_metadata_);
  logging_endpoint_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
  if (from.logging_endpoint().size() > 0) {
    logging_endpoint_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.logging_endpoint_);
  }
  logging_protocol_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
  if (from.logging_protocol().size() > 0) {
    logging_protocol_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.logging_protocol_);
  }
  reason_detail_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
  if (from.reason_detail().size() > 0) {
    reason_detail_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.reason_detail_);
  }
  ::memcpy(&status_, &from.status_,
    static_cast<size_t>(reinterpret_cast<char*>(&reason_) -
    reinterpret_cast<char*>(&status_)) + sizeof(reason_));
  // @@protoc_insertion_point(copy_constructor:dmi.GetLoggingEndpointResponse)
}

void GetLoggingEndpointResponse::SharedCtor() {
  ::google::protobuf::internal::InitSCC(
      &scc_info_GetLoggingEndpointResponse_dmi_2fhw_5fmanagement_5fservice_2eproto.base);
  logging_endpoint_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
  logging_protocol_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
  reason_detail_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
  ::memset(&status_, 0, static_cast<size_t>(
      reinterpret_cast<char*>(&reason_) -
      reinterpret_cast<char*>(&status_)) + sizeof(reason_));
}

GetLoggingEndpointResponse::~GetLoggingEndpointResponse() {
  // @@protoc_insertion_point(destructor:dmi.GetLoggingEndpointResponse)
  SharedDtor();
}

void GetLoggingEndpointResponse::SharedDtor() {
  logging_endpoint_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
  logging_protocol_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
  reason_detail_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}

void GetLoggingEndpointResponse::SetCachedSize(int size) const {
  _cached_size_.Set(size);
}
const GetLoggingEndpointResponse& GetLoggingEndpointResponse::default_instance() {
  ::google::protobuf::internal::InitSCC(&::scc_info_GetLoggingEndpointResponse_dmi_2fhw_5fmanagement_5fservice_2eproto.base);
  return *internal_default_instance();
}


void GetLoggingEndpointResponse::Clear() {
// @@protoc_insertion_point(message_clear_start:dmi.GetLoggingEndpointResponse)
  ::google::protobuf::uint32 cached_has_bits = 0;
  // Prevent compiler warnings about cached_has_bits being unused
  (void) cached_has_bits;

  logging_endpoint_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
  logging_protocol_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
  reason_detail_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
  ::memset(&status_, 0, static_cast<size_t>(
      reinterpret_cast<char*>(&reason_) -
      reinterpret_cast<char*>(&status_)) + sizeof(reason_));
  _internal_metadata_.Clear();
}

#if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER
const char* GetLoggingEndpointResponse::_InternalParse(const char* begin, const char* end, void* object,
                  ::google::protobuf::internal::ParseContext* ctx) {
  auto msg = static_cast<GetLoggingEndpointResponse*>(object);
  ::google::protobuf::int32 size; (void)size;
  int depth; (void)depth;
  ::google::protobuf::uint32 tag;
  ::google::protobuf::internal::ParseFunc parser_till_end; (void)parser_till_end;
  auto ptr = begin;
  while (ptr < end) {
    ptr = ::google::protobuf::io::Parse32(ptr, &tag);
    GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
    switch (tag >> 3) {
      // .dmi.Status status = 1;
      case 1: {
        if (static_cast<::google::protobuf::uint8>(tag) != 8) goto handle_unusual;
        ::google::protobuf::uint64 val = ::google::protobuf::internal::ReadVarint(&ptr);
        msg->set_status(static_cast<::dmi::Status>(val));
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
        break;
      }
      // .dmi.GetLoggingEndpointResponse.Reason reason = 2;
      case 2: {
        if (static_cast<::google::protobuf::uint8>(tag) != 16) goto handle_unusual;
        ::google::protobuf::uint64 val = ::google::protobuf::internal::ReadVarint(&ptr);
        msg->set_reason(static_cast<::dmi::GetLoggingEndpointResponse_Reason>(val));
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
        break;
      }
      // string logging_endpoint = 3;
      case 3: {
        if (static_cast<::google::protobuf::uint8>(tag) != 26) goto handle_unusual;
        ptr = ::google::protobuf::io::ReadSize(ptr, &size);
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
        ctx->extra_parse_data().SetFieldName("dmi.GetLoggingEndpointResponse.logging_endpoint");
        object = msg->mutable_logging_endpoint();
        if (size > end - ptr + ::google::protobuf::internal::ParseContext::kSlopBytes) {
          parser_till_end = ::google::protobuf::internal::GreedyStringParserUTF8;
          goto string_till_end;
        }
        GOOGLE_PROTOBUF_PARSER_ASSERT(::google::protobuf::internal::StringCheckUTF8(ptr, size, ctx));
        ::google::protobuf::internal::InlineGreedyStringParser(object, ptr, size, ctx);
        ptr += size;
        break;
      }
      // string logging_protocol = 4;
      case 4: {
        if (static_cast<::google::protobuf::uint8>(tag) != 34) goto handle_unusual;
        ptr = ::google::protobuf::io::ReadSize(ptr, &size);
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
        ctx->extra_parse_data().SetFieldName("dmi.GetLoggingEndpointResponse.logging_protocol");
        object = msg->mutable_logging_protocol();
        if (size > end - ptr + ::google::protobuf::internal::ParseContext::kSlopBytes) {
          parser_till_end = ::google::protobuf::internal::GreedyStringParserUTF8;
          goto string_till_end;
        }
        GOOGLE_PROTOBUF_PARSER_ASSERT(::google::protobuf::internal::StringCheckUTF8(ptr, size, ctx));
        ::google::protobuf::internal::InlineGreedyStringParser(object, ptr, size, ctx);
        ptr += size;
        break;
      }
      // string reason_detail = 5;
      case 5: {
        if (static_cast<::google::protobuf::uint8>(tag) != 42) goto handle_unusual;
        ptr = ::google::protobuf::io::ReadSize(ptr, &size);
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
        ctx->extra_parse_data().SetFieldName("dmi.GetLoggingEndpointResponse.reason_detail");
        object = msg->mutable_reason_detail();
        if (size > end - ptr + ::google::protobuf::internal::ParseContext::kSlopBytes) {
          parser_till_end = ::google::protobuf::internal::GreedyStringParserUTF8;
          goto string_till_end;
        }
        GOOGLE_PROTOBUF_PARSER_ASSERT(::google::protobuf::internal::StringCheckUTF8(ptr, size, ctx));
        ::google::protobuf::internal::InlineGreedyStringParser(object, ptr, size, ctx);
        ptr += size;
        break;
      }
      default: {
      handle_unusual:
        if ((tag & 7) == 4 || tag == 0) {
          ctx->EndGroup(tag);
          return ptr;
        }
        auto res = UnknownFieldParse(tag, {_InternalParse, msg},
          ptr, end, msg->_internal_metadata_.mutable_unknown_fields(), ctx);
        ptr = res.first;
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr);
        if (res.second) return ptr;
      }
    }  // switch
  }  // while
  return ptr;
string_till_end:
  static_cast<::std::string*>(object)->clear();
  static_cast<::std::string*>(object)->reserve(size);
  goto len_delim_till_end;
len_delim_till_end:
  return ctx->StoreAndTailCall(ptr, end, {_InternalParse, msg},
                               {parser_till_end, object}, size);
}
#else  // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER
bool GetLoggingEndpointResponse::MergePartialFromCodedStream(
    ::google::protobuf::io::CodedInputStream* input) {
#define DO_(EXPRESSION) if (!PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure
  ::google::protobuf::uint32 tag;
  // @@protoc_insertion_point(parse_start:dmi.GetLoggingEndpointResponse)
  for (;;) {
    ::std::pair<::google::protobuf::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u);
    tag = p.first;
    if (!p.second) goto handle_unusual;
    switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) {
      // .dmi.Status status = 1;
      case 1: {
        if (static_cast< ::google::protobuf::uint8>(tag) == (8 & 0xFF)) {
          int value = 0;
          DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive<
                   int, ::google::protobuf::internal::WireFormatLite::TYPE_ENUM>(
                 input, &value)));
          set_status(static_cast< ::dmi::Status >(value));
        } else {
          goto handle_unusual;
        }
        break;
      }

      // .dmi.GetLoggingEndpointResponse.Reason reason = 2;
      case 2: {
        if (static_cast< ::google::protobuf::uint8>(tag) == (16 & 0xFF)) {
          int value = 0;
          DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive<
                   int, ::google::protobuf::internal::WireFormatLite::TYPE_ENUM>(
                 input, &value)));
          set_reason(static_cast< ::dmi::GetLoggingEndpointResponse_Reason >(value));
        } else {
          goto handle_unusual;
        }
        break;
      }

      // string logging_endpoint = 3;
      case 3: {
        if (static_cast< ::google::protobuf::uint8>(tag) == (26 & 0xFF)) {
          DO_(::google::protobuf::internal::WireFormatLite::ReadString(
                input, this->mutable_logging_endpoint()));
          DO_(::google::protobuf::internal::WireFormatLite::VerifyUtf8String(
            this->logging_endpoint().data(), static_cast<int>(this->logging_endpoint().length()),
            ::google::protobuf::internal::WireFormatLite::PARSE,
            "dmi.GetLoggingEndpointResponse.logging_endpoint"));
        } else {
          goto handle_unusual;
        }
        break;
      }

      // string logging_protocol = 4;
      case 4: {
        if (static_cast< ::google::protobuf::uint8>(tag) == (34 & 0xFF)) {
          DO_(::google::protobuf::internal::WireFormatLite::ReadString(
                input, this->mutable_logging_protocol()));
          DO_(::google::protobuf::internal::WireFormatLite::VerifyUtf8String(
            this->logging_protocol().data(), static_cast<int>(this->logging_protocol().length()),
            ::google::protobuf::internal::WireFormatLite::PARSE,
            "dmi.GetLoggingEndpointResponse.logging_protocol"));
        } else {
          goto handle_unusual;
        }
        break;
      }

      // string reason_detail = 5;
      case 5: {
        if (static_cast< ::google::protobuf::uint8>(tag) == (42 & 0xFF)) {
          DO_(::google::protobuf::internal::WireFormatLite::ReadString(
                input, this->mutable_reason_detail()));
          DO_(::google::protobuf::internal::WireFormatLite::VerifyUtf8String(
            this->reason_detail().data(), static_cast<int>(this->reason_detail().length()),
            ::google::protobuf::internal::WireFormatLite::PARSE,
            "dmi.GetLoggingEndpointResponse.reason_detail"));
        } else {
          goto handle_unusual;
        }
        break;
      }

      default: {
      handle_unusual:
        if (tag == 0) {
          goto success;
        }
        DO_(::google::protobuf::internal::WireFormat::SkipField(
              input, tag, _internal_metadata_.mutable_unknown_fields()));
        break;
      }
    }
  }
success:
  // @@protoc_insertion_point(parse_success:dmi.GetLoggingEndpointResponse)
  return true;
failure:
  // @@protoc_insertion_point(parse_failure:dmi.GetLoggingEndpointResponse)
  return false;
#undef DO_
}
#endif  // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER

void GetLoggingEndpointResponse::SerializeWithCachedSizes(
    ::google::protobuf::io::CodedOutputStream* output) const {
  // @@protoc_insertion_point(serialize_start:dmi.GetLoggingEndpointResponse)
  ::google::protobuf::uint32 cached_has_bits = 0;
  (void) cached_has_bits;

  // .dmi.Status status = 1;
  if (this->status() != 0) {
    ::google::protobuf::internal::WireFormatLite::WriteEnum(
      1, this->status(), output);
  }

  // .dmi.GetLoggingEndpointResponse.Reason reason = 2;
  if (this->reason() != 0) {
    ::google::protobuf::internal::WireFormatLite::WriteEnum(
      2, this->reason(), output);
  }

  // string logging_endpoint = 3;
  if (this->logging_endpoint().size() > 0) {
    ::google::protobuf::internal::WireFormatLite::VerifyUtf8String(
      this->logging_endpoint().data(), static_cast<int>(this->logging_endpoint().length()),
      ::google::protobuf::internal::WireFormatLite::SERIALIZE,
      "dmi.GetLoggingEndpointResponse.logging_endpoint");
    ::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased(
      3, this->logging_endpoint(), output);
  }

  // string logging_protocol = 4;
  if (this->logging_protocol().size() > 0) {
    ::google::protobuf::internal::WireFormatLite::VerifyUtf8String(
      this->logging_protocol().data(), static_cast<int>(this->logging_protocol().length()),
      ::google::protobuf::internal::WireFormatLite::SERIALIZE,
      "dmi.GetLoggingEndpointResponse.logging_protocol");
    ::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased(
      4, this->logging_protocol(), output);
  }

  // string reason_detail = 5;
  if (this->reason_detail().size() > 0) {
    ::google::protobuf::internal::WireFormatLite::VerifyUtf8String(
      this->reason_detail().data(), static_cast<int>(this->reason_detail().length()),
      ::google::protobuf::internal::WireFormatLite::SERIALIZE,
      "dmi.GetLoggingEndpointResponse.reason_detail");
    ::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased(
      5, this->reason_detail(), output);
  }

  if (_internal_metadata_.have_unknown_fields()) {
    ::google::protobuf::internal::WireFormat::SerializeUnknownFields(
        _internal_metadata_.unknown_fields(), output);
  }
  // @@protoc_insertion_point(serialize_end:dmi.GetLoggingEndpointResponse)
}

::google::protobuf::uint8* GetLoggingEndpointResponse::InternalSerializeWithCachedSizesToArray(
    ::google::protobuf::uint8* target) const {
  // @@protoc_insertion_point(serialize_to_array_start:dmi.GetLoggingEndpointResponse)
  ::google::protobuf::uint32 cached_has_bits = 0;
  (void) cached_has_bits;

  // .dmi.Status status = 1;
  if (this->status() != 0) {
    target = ::google::protobuf::internal::WireFormatLite::WriteEnumToArray(
      1, this->status(), target);
  }

  // .dmi.GetLoggingEndpointResponse.Reason reason = 2;
  if (this->reason() != 0) {
    target = ::google::protobuf::internal::WireFormatLite::WriteEnumToArray(
      2, this->reason(), target);
  }

  // string logging_endpoint = 3;
  if (this->logging_endpoint().size() > 0) {
    ::google::protobuf::internal::WireFormatLite::VerifyUtf8String(
      this->logging_endpoint().data(), static_cast<int>(this->logging_endpoint().length()),
      ::google::protobuf::internal::WireFormatLite::SERIALIZE,
      "dmi.GetLoggingEndpointResponse.logging_endpoint");
    target =
      ::google::protobuf::internal::WireFormatLite::WriteStringToArray(
        3, this->logging_endpoint(), target);
  }

  // string logging_protocol = 4;
  if (this->logging_protocol().size() > 0) {
    ::google::protobuf::internal::WireFormatLite::VerifyUtf8String(
      this->logging_protocol().data(), static_cast<int>(this->logging_protocol().length()),
      ::google::protobuf::internal::WireFormatLite::SERIALIZE,
      "dmi.GetLoggingEndpointResponse.logging_protocol");
    target =
      ::google::protobuf::internal::WireFormatLite::WriteStringToArray(
        4, this->logging_protocol(), target);
  }

  // string reason_detail = 5;
  if (this->reason_detail().size() > 0) {
    ::google::protobuf::internal::WireFormatLite::VerifyUtf8String(
      this->reason_detail().data(), static_cast<int>(this->reason_detail().length()),
      ::google::protobuf::internal::WireFormatLite::SERIALIZE,
      "dmi.GetLoggingEndpointResponse.reason_detail");
    target =
      ::google::protobuf::internal::WireFormatLite::WriteStringToArray(
        5, this->reason_detail(), target);
  }

  if (_internal_metadata_.have_unknown_fields()) {
    target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray(
        _internal_metadata_.unknown_fields(), target);
  }
  // @@protoc_insertion_point(serialize_to_array_end:dmi.GetLoggingEndpointResponse)
  return target;
}

size_t GetLoggingEndpointResponse::ByteSizeLong() const {
// @@protoc_insertion_point(message_byte_size_start:dmi.GetLoggingEndpointResponse)
  size_t total_size = 0;

  if (_internal_metadata_.have_unknown_fields()) {
    total_size +=
      ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize(
        _internal_metadata_.unknown_fields());
  }
  ::google::protobuf::uint32 cached_has_bits = 0;
  // Prevent compiler warnings about cached_has_bits being unused
  (void) cached_has_bits;

  // string logging_endpoint = 3;
  if (this->logging_endpoint().size() > 0) {
    total_size += 1 +
      ::google::protobuf::internal::WireFormatLite::StringSize(
        this->logging_endpoint());
  }

  // string logging_protocol = 4;
  if (this->logging_protocol().size() > 0) {
    total_size += 1 +
      ::google::protobuf::internal::WireFormatLite::StringSize(
        this->logging_protocol());
  }

  // string reason_detail = 5;
  if (this->reason_detail().size() > 0) {
    total_size += 1 +
      ::google::protobuf::internal::WireFormatLite::StringSize(
        this->reason_detail());
  }

  // .dmi.Status status = 1;
  if (this->status() != 0) {
    total_size += 1 +
      ::google::protobuf::internal::WireFormatLite::EnumSize(this->status());
  }

  // .dmi.GetLoggingEndpointResponse.Reason reason = 2;
  if (this->reason() != 0) {
    total_size += 1 +
      ::google::protobuf::internal::WireFormatLite::EnumSize(this->reason());
  }

  int cached_size = ::google::protobuf::internal::ToCachedSize(total_size);
  SetCachedSize(cached_size);
  return total_size;
}

void GetLoggingEndpointResponse::MergeFrom(const ::google::protobuf::Message& from) {
// @@protoc_insertion_point(generalized_merge_from_start:dmi.GetLoggingEndpointResponse)
  GOOGLE_DCHECK_NE(&from, this);
  const GetLoggingEndpointResponse* source =
      ::google::protobuf::DynamicCastToGenerated<GetLoggingEndpointResponse>(
          &from);
  if (source == nullptr) {
  // @@protoc_insertion_point(generalized_merge_from_cast_fail:dmi.GetLoggingEndpointResponse)
    ::google::protobuf::internal::ReflectionOps::Merge(from, this);
  } else {
  // @@protoc_insertion_point(generalized_merge_from_cast_success:dmi.GetLoggingEndpointResponse)
    MergeFrom(*source);
  }
}

void GetLoggingEndpointResponse::MergeFrom(const GetLoggingEndpointResponse& from) {
// @@protoc_insertion_point(class_specific_merge_from_start:dmi.GetLoggingEndpointResponse)
  GOOGLE_DCHECK_NE(&from, this);
  _internal_metadata_.MergeFrom(from._internal_metadata_);
  ::google::protobuf::uint32 cached_has_bits = 0;
  (void) cached_has_bits;

  if (from.logging_endpoint().size() > 0) {

    logging_endpoint_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.logging_endpoint_);
  }
  if (from.logging_protocol().size() > 0) {

    logging_protocol_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.logging_protocol_);
  }
  if (from.reason_detail().size() > 0) {

    reason_detail_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.reason_detail_);
  }
  if (from.status() != 0) {
    set_status(from.status());
  }
  if (from.reason() != 0) {
    set_reason(from.reason());
  }
}

void GetLoggingEndpointResponse::CopyFrom(const ::google::protobuf::Message& from) {
// @@protoc_insertion_point(generalized_copy_from_start:dmi.GetLoggingEndpointResponse)
  if (&from == this) return;
  Clear();
  MergeFrom(from);
}

void GetLoggingEndpointResponse::CopyFrom(const GetLoggingEndpointResponse& from) {
// @@protoc_insertion_point(class_specific_copy_from_start:dmi.GetLoggingEndpointResponse)
  if (&from == this) return;
  Clear();
  MergeFrom(from);
}

bool GetLoggingEndpointResponse::IsInitialized() const {
  return true;
}

void GetLoggingEndpointResponse::Swap(GetLoggingEndpointResponse* other) {
  if (other == this) return;
  InternalSwap(other);
}
void GetLoggingEndpointResponse::InternalSwap(GetLoggingEndpointResponse* other) {
  using std::swap;
  _internal_metadata_.Swap(&other->_internal_metadata_);
  logging_endpoint_.Swap(&other->logging_endpoint_, &::google::protobuf::internal::GetEmptyStringAlreadyInited(),
    GetArenaNoVirtual());
  logging_protocol_.Swap(&other->logging_protocol_, &::google::protobuf::internal::GetEmptyStringAlreadyInited(),
    GetArenaNoVirtual());
  reason_detail_.Swap(&other->reason_detail_, &::google::protobuf::internal::GetEmptyStringAlreadyInited(),
    GetArenaNoVirtual());
  swap(status_, other->status_);
  swap(reason_, other->reason_);
}

::google::protobuf::Metadata GetLoggingEndpointResponse::GetMetadata() const {
  ::google::protobuf::internal::AssignDescriptors(&::assign_descriptors_table_dmi_2fhw_5fmanagement_5fservice_2eproto);
  return ::file_level_metadata_dmi_2fhw_5fmanagement_5fservice_2eproto[kIndexInFileMessages];
}


// ===================================================================

void SetMsgBusEndpointRequest::InitAsDefaultInstance() {
}
class SetMsgBusEndpointRequest::HasBitSetters {
 public:
};

#if !defined(_MSC_VER) || _MSC_VER >= 1900
const int SetMsgBusEndpointRequest::kMsgbusEndpointFieldNumber;
#endif  // !defined(_MSC_VER) || _MSC_VER >= 1900

SetMsgBusEndpointRequest::SetMsgBusEndpointRequest()
  : ::google::protobuf::Message(), _internal_metadata_(nullptr) {
  SharedCtor();
  // @@protoc_insertion_point(constructor:dmi.SetMsgBusEndpointRequest)
}
SetMsgBusEndpointRequest::SetMsgBusEndpointRequest(const SetMsgBusEndpointRequest& from)
  : ::google::protobuf::Message(),
      _internal_metadata_(nullptr) {
  _internal_metadata_.MergeFrom(from._internal_metadata_);
  msgbus_endpoint_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
  if (from.msgbus_endpoint().size() > 0) {
    msgbus_endpoint_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.msgbus_endpoint_);
  }
  // @@protoc_insertion_point(copy_constructor:dmi.SetMsgBusEndpointRequest)
}

void SetMsgBusEndpointRequest::SharedCtor() {
  ::google::protobuf::internal::InitSCC(
      &scc_info_SetMsgBusEndpointRequest_dmi_2fhw_5fmanagement_5fservice_2eproto.base);
  msgbus_endpoint_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}

SetMsgBusEndpointRequest::~SetMsgBusEndpointRequest() {
  // @@protoc_insertion_point(destructor:dmi.SetMsgBusEndpointRequest)
  SharedDtor();
}

void SetMsgBusEndpointRequest::SharedDtor() {
  msgbus_endpoint_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}

void SetMsgBusEndpointRequest::SetCachedSize(int size) const {
  _cached_size_.Set(size);
}
const SetMsgBusEndpointRequest& SetMsgBusEndpointRequest::default_instance() {
  ::google::protobuf::internal::InitSCC(&::scc_info_SetMsgBusEndpointRequest_dmi_2fhw_5fmanagement_5fservice_2eproto.base);
  return *internal_default_instance();
}


void SetMsgBusEndpointRequest::Clear() {
// @@protoc_insertion_point(message_clear_start:dmi.SetMsgBusEndpointRequest)
  ::google::protobuf::uint32 cached_has_bits = 0;
  // Prevent compiler warnings about cached_has_bits being unused
  (void) cached_has_bits;

  msgbus_endpoint_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
  _internal_metadata_.Clear();
}

#if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER
const char* SetMsgBusEndpointRequest::_InternalParse(const char* begin, const char* end, void* object,
                  ::google::protobuf::internal::ParseContext* ctx) {
  auto msg = static_cast<SetMsgBusEndpointRequest*>(object);
  ::google::protobuf::int32 size; (void)size;
  int depth; (void)depth;
  ::google::protobuf::uint32 tag;
  ::google::protobuf::internal::ParseFunc parser_till_end; (void)parser_till_end;
  auto ptr = begin;
  while (ptr < end) {
    ptr = ::google::protobuf::io::Parse32(ptr, &tag);
    GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
    switch (tag >> 3) {
      // string msgbus_endpoint = 1;
      case 1: {
        if (static_cast<::google::protobuf::uint8>(tag) != 10) goto handle_unusual;
        ptr = ::google::protobuf::io::ReadSize(ptr, &size);
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
        ctx->extra_parse_data().SetFieldName("dmi.SetMsgBusEndpointRequest.msgbus_endpoint");
        object = msg->mutable_msgbus_endpoint();
        if (size > end - ptr + ::google::protobuf::internal::ParseContext::kSlopBytes) {
          parser_till_end = ::google::protobuf::internal::GreedyStringParserUTF8;
          goto string_till_end;
        }
        GOOGLE_PROTOBUF_PARSER_ASSERT(::google::protobuf::internal::StringCheckUTF8(ptr, size, ctx));
        ::google::protobuf::internal::InlineGreedyStringParser(object, ptr, size, ctx);
        ptr += size;
        break;
      }
      default: {
      handle_unusual:
        if ((tag & 7) == 4 || tag == 0) {
          ctx->EndGroup(tag);
          return ptr;
        }
        auto res = UnknownFieldParse(tag, {_InternalParse, msg},
          ptr, end, msg->_internal_metadata_.mutable_unknown_fields(), ctx);
        ptr = res.first;
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr);
        if (res.second) return ptr;
      }
    }  // switch
  }  // while
  return ptr;
string_till_end:
  static_cast<::std::string*>(object)->clear();
  static_cast<::std::string*>(object)->reserve(size);
  goto len_delim_till_end;
len_delim_till_end:
  return ctx->StoreAndTailCall(ptr, end, {_InternalParse, msg},
                               {parser_till_end, object}, size);
}
#else  // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER
bool SetMsgBusEndpointRequest::MergePartialFromCodedStream(
    ::google::protobuf::io::CodedInputStream* input) {
#define DO_(EXPRESSION) if (!PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure
  ::google::protobuf::uint32 tag;
  // @@protoc_insertion_point(parse_start:dmi.SetMsgBusEndpointRequest)
  for (;;) {
    ::std::pair<::google::protobuf::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u);
    tag = p.first;
    if (!p.second) goto handle_unusual;
    switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) {
      // string msgbus_endpoint = 1;
      case 1: {
        if (static_cast< ::google::protobuf::uint8>(tag) == (10 & 0xFF)) {
          DO_(::google::protobuf::internal::WireFormatLite::ReadString(
                input, this->mutable_msgbus_endpoint()));
          DO_(::google::protobuf::internal::WireFormatLite::VerifyUtf8String(
            this->msgbus_endpoint().data(), static_cast<int>(this->msgbus_endpoint().length()),
            ::google::protobuf::internal::WireFormatLite::PARSE,
            "dmi.SetMsgBusEndpointRequest.msgbus_endpoint"));
        } else {
          goto handle_unusual;
        }
        break;
      }

      default: {
      handle_unusual:
        if (tag == 0) {
          goto success;
        }
        DO_(::google::protobuf::internal::WireFormat::SkipField(
              input, tag, _internal_metadata_.mutable_unknown_fields()));
        break;
      }
    }
  }
success:
  // @@protoc_insertion_point(parse_success:dmi.SetMsgBusEndpointRequest)
  return true;
failure:
  // @@protoc_insertion_point(parse_failure:dmi.SetMsgBusEndpointRequest)
  return false;
#undef DO_
}
#endif  // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER

void SetMsgBusEndpointRequest::SerializeWithCachedSizes(
    ::google::protobuf::io::CodedOutputStream* output) const {
  // @@protoc_insertion_point(serialize_start:dmi.SetMsgBusEndpointRequest)
  ::google::protobuf::uint32 cached_has_bits = 0;
  (void) cached_has_bits;

  // string msgbus_endpoint = 1;
  if (this->msgbus_endpoint().size() > 0) {
    ::google::protobuf::internal::WireFormatLite::VerifyUtf8String(
      this->msgbus_endpoint().data(), static_cast<int>(this->msgbus_endpoint().length()),
      ::google::protobuf::internal::WireFormatLite::SERIALIZE,
      "dmi.SetMsgBusEndpointRequest.msgbus_endpoint");
    ::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased(
      1, this->msgbus_endpoint(), output);
  }

  if (_internal_metadata_.have_unknown_fields()) {
    ::google::protobuf::internal::WireFormat::SerializeUnknownFields(
        _internal_metadata_.unknown_fields(), output);
  }
  // @@protoc_insertion_point(serialize_end:dmi.SetMsgBusEndpointRequest)
}

::google::protobuf::uint8* SetMsgBusEndpointRequest::InternalSerializeWithCachedSizesToArray(
    ::google::protobuf::uint8* target) const {
  // @@protoc_insertion_point(serialize_to_array_start:dmi.SetMsgBusEndpointRequest)
  ::google::protobuf::uint32 cached_has_bits = 0;
  (void) cached_has_bits;

  // string msgbus_endpoint = 1;
  if (this->msgbus_endpoint().size() > 0) {
    ::google::protobuf::internal::WireFormatLite::VerifyUtf8String(
      this->msgbus_endpoint().data(), static_cast<int>(this->msgbus_endpoint().length()),
      ::google::protobuf::internal::WireFormatLite::SERIALIZE,
      "dmi.SetMsgBusEndpointRequest.msgbus_endpoint");
    target =
      ::google::protobuf::internal::WireFormatLite::WriteStringToArray(
        1, this->msgbus_endpoint(), target);
  }

  if (_internal_metadata_.have_unknown_fields()) {
    target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray(
        _internal_metadata_.unknown_fields(), target);
  }
  // @@protoc_insertion_point(serialize_to_array_end:dmi.SetMsgBusEndpointRequest)
  return target;
}

size_t SetMsgBusEndpointRequest::ByteSizeLong() const {
// @@protoc_insertion_point(message_byte_size_start:dmi.SetMsgBusEndpointRequest)
  size_t total_size = 0;

  if (_internal_metadata_.have_unknown_fields()) {
    total_size +=
      ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize(
        _internal_metadata_.unknown_fields());
  }
  ::google::protobuf::uint32 cached_has_bits = 0;
  // Prevent compiler warnings about cached_has_bits being unused
  (void) cached_has_bits;

  // string msgbus_endpoint = 1;
  if (this->msgbus_endpoint().size() > 0) {
    total_size += 1 +
      ::google::protobuf::internal::WireFormatLite::StringSize(
        this->msgbus_endpoint());
  }

  int cached_size = ::google::protobuf::internal::ToCachedSize(total_size);
  SetCachedSize(cached_size);
  return total_size;
}

void SetMsgBusEndpointRequest::MergeFrom(const ::google::protobuf::Message& from) {
// @@protoc_insertion_point(generalized_merge_from_start:dmi.SetMsgBusEndpointRequest)
  GOOGLE_DCHECK_NE(&from, this);
  const SetMsgBusEndpointRequest* source =
      ::google::protobuf::DynamicCastToGenerated<SetMsgBusEndpointRequest>(
          &from);
  if (source == nullptr) {
  // @@protoc_insertion_point(generalized_merge_from_cast_fail:dmi.SetMsgBusEndpointRequest)
    ::google::protobuf::internal::ReflectionOps::Merge(from, this);
  } else {
  // @@protoc_insertion_point(generalized_merge_from_cast_success:dmi.SetMsgBusEndpointRequest)
    MergeFrom(*source);
  }
}

void SetMsgBusEndpointRequest::MergeFrom(const SetMsgBusEndpointRequest& from) {
// @@protoc_insertion_point(class_specific_merge_from_start:dmi.SetMsgBusEndpointRequest)
  GOOGLE_DCHECK_NE(&from, this);
  _internal_metadata_.MergeFrom(from._internal_metadata_);
  ::google::protobuf::uint32 cached_has_bits = 0;
  (void) cached_has_bits;

  if (from.msgbus_endpoint().size() > 0) {

    msgbus_endpoint_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.msgbus_endpoint_);
  }
}

void SetMsgBusEndpointRequest::CopyFrom(const ::google::protobuf::Message& from) {
// @@protoc_insertion_point(generalized_copy_from_start:dmi.SetMsgBusEndpointRequest)
  if (&from == this) return;
  Clear();
  MergeFrom(from);
}

void SetMsgBusEndpointRequest::CopyFrom(const SetMsgBusEndpointRequest& from) {
// @@protoc_insertion_point(class_specific_copy_from_start:dmi.SetMsgBusEndpointRequest)
  if (&from == this) return;
  Clear();
  MergeFrom(from);
}

bool SetMsgBusEndpointRequest::IsInitialized() const {
  return true;
}

void SetMsgBusEndpointRequest::Swap(SetMsgBusEndpointRequest* other) {
  if (other == this) return;
  InternalSwap(other);
}
void SetMsgBusEndpointRequest::InternalSwap(SetMsgBusEndpointRequest* other) {
  using std::swap;
  _internal_metadata_.Swap(&other->_internal_metadata_);
  msgbus_endpoint_.Swap(&other->msgbus_endpoint_, &::google::protobuf::internal::GetEmptyStringAlreadyInited(),
    GetArenaNoVirtual());
}

::google::protobuf::Metadata SetMsgBusEndpointRequest::GetMetadata() const {
  ::google::protobuf::internal::AssignDescriptors(&::assign_descriptors_table_dmi_2fhw_5fmanagement_5fservice_2eproto);
  return ::file_level_metadata_dmi_2fhw_5fmanagement_5fservice_2eproto[kIndexInFileMessages];
}


// ===================================================================

void GetMsgBusEndpointResponse::InitAsDefaultInstance() {
}
class GetMsgBusEndpointResponse::HasBitSetters {
 public:
};

#if !defined(_MSC_VER) || _MSC_VER >= 1900
const int GetMsgBusEndpointResponse::kStatusFieldNumber;
const int GetMsgBusEndpointResponse::kReasonFieldNumber;
const int GetMsgBusEndpointResponse::kMsgbusEndpointFieldNumber;
const int GetMsgBusEndpointResponse::kReasonDetailFieldNumber;
#endif  // !defined(_MSC_VER) || _MSC_VER >= 1900

GetMsgBusEndpointResponse::GetMsgBusEndpointResponse()
  : ::google::protobuf::Message(), _internal_metadata_(nullptr) {
  SharedCtor();
  // @@protoc_insertion_point(constructor:dmi.GetMsgBusEndpointResponse)
}
GetMsgBusEndpointResponse::GetMsgBusEndpointResponse(const GetMsgBusEndpointResponse& from)
  : ::google::protobuf::Message(),
      _internal_metadata_(nullptr) {
  _internal_metadata_.MergeFrom(from._internal_metadata_);
  msgbus_endpoint_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
  if (from.msgbus_endpoint().size() > 0) {
    msgbus_endpoint_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.msgbus_endpoint_);
  }
  reason_detail_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
  if (from.reason_detail().size() > 0) {
    reason_detail_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.reason_detail_);
  }
  ::memcpy(&status_, &from.status_,
    static_cast<size_t>(reinterpret_cast<char*>(&reason_) -
    reinterpret_cast<char*>(&status_)) + sizeof(reason_));
  // @@protoc_insertion_point(copy_constructor:dmi.GetMsgBusEndpointResponse)
}

void GetMsgBusEndpointResponse::SharedCtor() {
  ::google::protobuf::internal::InitSCC(
      &scc_info_GetMsgBusEndpointResponse_dmi_2fhw_5fmanagement_5fservice_2eproto.base);
  msgbus_endpoint_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
  reason_detail_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
  ::memset(&status_, 0, static_cast<size_t>(
      reinterpret_cast<char*>(&reason_) -
      reinterpret_cast<char*>(&status_)) + sizeof(reason_));
}

GetMsgBusEndpointResponse::~GetMsgBusEndpointResponse() {
  // @@protoc_insertion_point(destructor:dmi.GetMsgBusEndpointResponse)
  SharedDtor();
}

void GetMsgBusEndpointResponse::SharedDtor() {
  msgbus_endpoint_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
  reason_detail_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}

void GetMsgBusEndpointResponse::SetCachedSize(int size) const {
  _cached_size_.Set(size);
}
const GetMsgBusEndpointResponse& GetMsgBusEndpointResponse::default_instance() {
  ::google::protobuf::internal::InitSCC(&::scc_info_GetMsgBusEndpointResponse_dmi_2fhw_5fmanagement_5fservice_2eproto.base);
  return *internal_default_instance();
}


void GetMsgBusEndpointResponse::Clear() {
// @@protoc_insertion_point(message_clear_start:dmi.GetMsgBusEndpointResponse)
  ::google::protobuf::uint32 cached_has_bits = 0;
  // Prevent compiler warnings about cached_has_bits being unused
  (void) cached_has_bits;

  msgbus_endpoint_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
  reason_detail_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
  ::memset(&status_, 0, static_cast<size_t>(
      reinterpret_cast<char*>(&reason_) -
      reinterpret_cast<char*>(&status_)) + sizeof(reason_));
  _internal_metadata_.Clear();
}

#if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER
const char* GetMsgBusEndpointResponse::_InternalParse(const char* begin, const char* end, void* object,
                  ::google::protobuf::internal::ParseContext* ctx) {
  auto msg = static_cast<GetMsgBusEndpointResponse*>(object);
  ::google::protobuf::int32 size; (void)size;
  int depth; (void)depth;
  ::google::protobuf::uint32 tag;
  ::google::protobuf::internal::ParseFunc parser_till_end; (void)parser_till_end;
  auto ptr = begin;
  while (ptr < end) {
    ptr = ::google::protobuf::io::Parse32(ptr, &tag);
    GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
    switch (tag >> 3) {
      // .dmi.Status status = 1;
      case 1: {
        if (static_cast<::google::protobuf::uint8>(tag) != 8) goto handle_unusual;
        ::google::protobuf::uint64 val = ::google::protobuf::internal::ReadVarint(&ptr);
        msg->set_status(static_cast<::dmi::Status>(val));
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
        break;
      }
      // .dmi.GetMsgBusEndpointResponse.Reason reason = 2;
      case 2: {
        if (static_cast<::google::protobuf::uint8>(tag) != 16) goto handle_unusual;
        ::google::protobuf::uint64 val = ::google::protobuf::internal::ReadVarint(&ptr);
        msg->set_reason(static_cast<::dmi::GetMsgBusEndpointResponse_Reason>(val));
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
        break;
      }
      // string msgbus_endpoint = 3;
      case 3: {
        if (static_cast<::google::protobuf::uint8>(tag) != 26) goto handle_unusual;
        ptr = ::google::protobuf::io::ReadSize(ptr, &size);
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
        ctx->extra_parse_data().SetFieldName("dmi.GetMsgBusEndpointResponse.msgbus_endpoint");
        object = msg->mutable_msgbus_endpoint();
        if (size > end - ptr + ::google::protobuf::internal::ParseContext::kSlopBytes) {
          parser_till_end = ::google::protobuf::internal::GreedyStringParserUTF8;
          goto string_till_end;
        }
        GOOGLE_PROTOBUF_PARSER_ASSERT(::google::protobuf::internal::StringCheckUTF8(ptr, size, ctx));
        ::google::protobuf::internal::InlineGreedyStringParser(object, ptr, size, ctx);
        ptr += size;
        break;
      }
      // string reason_detail = 4;
      case 4: {
        if (static_cast<::google::protobuf::uint8>(tag) != 34) goto handle_unusual;
        ptr = ::google::protobuf::io::ReadSize(ptr, &size);
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
        ctx->extra_parse_data().SetFieldName("dmi.GetMsgBusEndpointResponse.reason_detail");
        object = msg->mutable_reason_detail();
        if (size > end - ptr + ::google::protobuf::internal::ParseContext::kSlopBytes) {
          parser_till_end = ::google::protobuf::internal::GreedyStringParserUTF8;
          goto string_till_end;
        }
        GOOGLE_PROTOBUF_PARSER_ASSERT(::google::protobuf::internal::StringCheckUTF8(ptr, size, ctx));
        ::google::protobuf::internal::InlineGreedyStringParser(object, ptr, size, ctx);
        ptr += size;
        break;
      }
      default: {
      handle_unusual:
        if ((tag & 7) == 4 || tag == 0) {
          ctx->EndGroup(tag);
          return ptr;
        }
        auto res = UnknownFieldParse(tag, {_InternalParse, msg},
          ptr, end, msg->_internal_metadata_.mutable_unknown_fields(), ctx);
        ptr = res.first;
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr);
        if (res.second) return ptr;
      }
    }  // switch
  }  // while
  return ptr;
string_till_end:
  static_cast<::std::string*>(object)->clear();
  static_cast<::std::string*>(object)->reserve(size);
  goto len_delim_till_end;
len_delim_till_end:
  return ctx->StoreAndTailCall(ptr, end, {_InternalParse, msg},
                               {parser_till_end, object}, size);
}
#else  // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER
bool GetMsgBusEndpointResponse::MergePartialFromCodedStream(
    ::google::protobuf::io::CodedInputStream* input) {
#define DO_(EXPRESSION) if (!PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure
  ::google::protobuf::uint32 tag;
  // @@protoc_insertion_point(parse_start:dmi.GetMsgBusEndpointResponse)
  for (;;) {
    ::std::pair<::google::protobuf::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u);
    tag = p.first;
    if (!p.second) goto handle_unusual;
    switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) {
      // .dmi.Status status = 1;
      case 1: {
        if (static_cast< ::google::protobuf::uint8>(tag) == (8 & 0xFF)) {
          int value = 0;
          DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive<
                   int, ::google::protobuf::internal::WireFormatLite::TYPE_ENUM>(
                 input, &value)));
          set_status(static_cast< ::dmi::Status >(value));
        } else {
          goto handle_unusual;
        }
        break;
      }

      // .dmi.GetMsgBusEndpointResponse.Reason reason = 2;
      case 2: {
        if (static_cast< ::google::protobuf::uint8>(tag) == (16 & 0xFF)) {
          int value = 0;
          DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive<
                   int, ::google::protobuf::internal::WireFormatLite::TYPE_ENUM>(
                 input, &value)));
          set_reason(static_cast< ::dmi::GetMsgBusEndpointResponse_Reason >(value));
        } else {
          goto handle_unusual;
        }
        break;
      }

      // string msgbus_endpoint = 3;
      case 3: {
        if (static_cast< ::google::protobuf::uint8>(tag) == (26 & 0xFF)) {
          DO_(::google::protobuf::internal::WireFormatLite::ReadString(
                input, this->mutable_msgbus_endpoint()));
          DO_(::google::protobuf::internal::WireFormatLite::VerifyUtf8String(
            this->msgbus_endpoint().data(), static_cast<int>(this->msgbus_endpoint().length()),
            ::google::protobuf::internal::WireFormatLite::PARSE,
            "dmi.GetMsgBusEndpointResponse.msgbus_endpoint"));
        } else {
          goto handle_unusual;
        }
        break;
      }

      // string reason_detail = 4;
      case 4: {
        if (static_cast< ::google::protobuf::uint8>(tag) == (34 & 0xFF)) {
          DO_(::google::protobuf::internal::WireFormatLite::ReadString(
                input, this->mutable_reason_detail()));
          DO_(::google::protobuf::internal::WireFormatLite::VerifyUtf8String(
            this->reason_detail().data(), static_cast<int>(this->reason_detail().length()),
            ::google::protobuf::internal::WireFormatLite::PARSE,
            "dmi.GetMsgBusEndpointResponse.reason_detail"));
        } else {
          goto handle_unusual;
        }
        break;
      }

      default: {
      handle_unusual:
        if (tag == 0) {
          goto success;
        }
        DO_(::google::protobuf::internal::WireFormat::SkipField(
              input, tag, _internal_metadata_.mutable_unknown_fields()));
        break;
      }
    }
  }
success:
  // @@protoc_insertion_point(parse_success:dmi.GetMsgBusEndpointResponse)
  return true;
failure:
  // @@protoc_insertion_point(parse_failure:dmi.GetMsgBusEndpointResponse)
  return false;
#undef DO_
}
#endif  // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER

void GetMsgBusEndpointResponse::SerializeWithCachedSizes(
    ::google::protobuf::io::CodedOutputStream* output) const {
  // @@protoc_insertion_point(serialize_start:dmi.GetMsgBusEndpointResponse)
  ::google::protobuf::uint32 cached_has_bits = 0;
  (void) cached_has_bits;

  // .dmi.Status status = 1;
  if (this->status() != 0) {
    ::google::protobuf::internal::WireFormatLite::WriteEnum(
      1, this->status(), output);
  }

  // .dmi.GetMsgBusEndpointResponse.Reason reason = 2;
  if (this->reason() != 0) {
    ::google::protobuf::internal::WireFormatLite::WriteEnum(
      2, this->reason(), output);
  }

  // string msgbus_endpoint = 3;
  if (this->msgbus_endpoint().size() > 0) {
    ::google::protobuf::internal::WireFormatLite::VerifyUtf8String(
      this->msgbus_endpoint().data(), static_cast<int>(this->msgbus_endpoint().length()),
      ::google::protobuf::internal::WireFormatLite::SERIALIZE,
      "dmi.GetMsgBusEndpointResponse.msgbus_endpoint");
    ::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased(
      3, this->msgbus_endpoint(), output);
  }

  // string reason_detail = 4;
  if (this->reason_detail().size() > 0) {
    ::google::protobuf::internal::WireFormatLite::VerifyUtf8String(
      this->reason_detail().data(), static_cast<int>(this->reason_detail().length()),
      ::google::protobuf::internal::WireFormatLite::SERIALIZE,
      "dmi.GetMsgBusEndpointResponse.reason_detail");
    ::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased(
      4, this->reason_detail(), output);
  }

  if (_internal_metadata_.have_unknown_fields()) {
    ::google::protobuf::internal::WireFormat::SerializeUnknownFields(
        _internal_metadata_.unknown_fields(), output);
  }
  // @@protoc_insertion_point(serialize_end:dmi.GetMsgBusEndpointResponse)
}

::google::protobuf::uint8* GetMsgBusEndpointResponse::InternalSerializeWithCachedSizesToArray(
    ::google::protobuf::uint8* target) const {
  // @@protoc_insertion_point(serialize_to_array_start:dmi.GetMsgBusEndpointResponse)
  ::google::protobuf::uint32 cached_has_bits = 0;
  (void) cached_has_bits;

  // .dmi.Status status = 1;
  if (this->status() != 0) {
    target = ::google::protobuf::internal::WireFormatLite::WriteEnumToArray(
      1, this->status(), target);
  }

  // .dmi.GetMsgBusEndpointResponse.Reason reason = 2;
  if (this->reason() != 0) {
    target = ::google::protobuf::internal::WireFormatLite::WriteEnumToArray(
      2, this->reason(), target);
  }

  // string msgbus_endpoint = 3;
  if (this->msgbus_endpoint().size() > 0) {
    ::google::protobuf::internal::WireFormatLite::VerifyUtf8String(
      this->msgbus_endpoint().data(), static_cast<int>(this->msgbus_endpoint().length()),
      ::google::protobuf::internal::WireFormatLite::SERIALIZE,
      "dmi.GetMsgBusEndpointResponse.msgbus_endpoint");
    target =
      ::google::protobuf::internal::WireFormatLite::WriteStringToArray(
        3, this->msgbus_endpoint(), target);
  }

  // string reason_detail = 4;
  if (this->reason_detail().size() > 0) {
    ::google::protobuf::internal::WireFormatLite::VerifyUtf8String(
      this->reason_detail().data(), static_cast<int>(this->reason_detail().length()),
      ::google::protobuf::internal::WireFormatLite::SERIALIZE,
      "dmi.GetMsgBusEndpointResponse.reason_detail");
    target =
      ::google::protobuf::internal::WireFormatLite::WriteStringToArray(
        4, this->reason_detail(), target);
  }

  if (_internal_metadata_.have_unknown_fields()) {
    target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray(
        _internal_metadata_.unknown_fields(), target);
  }
  // @@protoc_insertion_point(serialize_to_array_end:dmi.GetMsgBusEndpointResponse)
  return target;
}

size_t GetMsgBusEndpointResponse::ByteSizeLong() const {
// @@protoc_insertion_point(message_byte_size_start:dmi.GetMsgBusEndpointResponse)
  size_t total_size = 0;

  if (_internal_metadata_.have_unknown_fields()) {
    total_size +=
      ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize(
        _internal_metadata_.unknown_fields());
  }
  ::google::protobuf::uint32 cached_has_bits = 0;
  // Prevent compiler warnings about cached_has_bits being unused
  (void) cached_has_bits;

  // string msgbus_endpoint = 3;
  if (this->msgbus_endpoint().size() > 0) {
    total_size += 1 +
      ::google::protobuf::internal::WireFormatLite::StringSize(
        this->msgbus_endpoint());
  }

  // string reason_detail = 4;
  if (this->reason_detail().size() > 0) {
    total_size += 1 +
      ::google::protobuf::internal::WireFormatLite::StringSize(
        this->reason_detail());
  }

  // .dmi.Status status = 1;
  if (this->status() != 0) {
    total_size += 1 +
      ::google::protobuf::internal::WireFormatLite::EnumSize(this->status());
  }

  // .dmi.GetMsgBusEndpointResponse.Reason reason = 2;
  if (this->reason() != 0) {
    total_size += 1 +
      ::google::protobuf::internal::WireFormatLite::EnumSize(this->reason());
  }

  int cached_size = ::google::protobuf::internal::ToCachedSize(total_size);
  SetCachedSize(cached_size);
  return total_size;
}

void GetMsgBusEndpointResponse::MergeFrom(const ::google::protobuf::Message& from) {
// @@protoc_insertion_point(generalized_merge_from_start:dmi.GetMsgBusEndpointResponse)
  GOOGLE_DCHECK_NE(&from, this);
  const GetMsgBusEndpointResponse* source =
      ::google::protobuf::DynamicCastToGenerated<GetMsgBusEndpointResponse>(
          &from);
  if (source == nullptr) {
  // @@protoc_insertion_point(generalized_merge_from_cast_fail:dmi.GetMsgBusEndpointResponse)
    ::google::protobuf::internal::ReflectionOps::Merge(from, this);
  } else {
  // @@protoc_insertion_point(generalized_merge_from_cast_success:dmi.GetMsgBusEndpointResponse)
    MergeFrom(*source);
  }
}

void GetMsgBusEndpointResponse::MergeFrom(const GetMsgBusEndpointResponse& from) {
// @@protoc_insertion_point(class_specific_merge_from_start:dmi.GetMsgBusEndpointResponse)
  GOOGLE_DCHECK_NE(&from, this);
  _internal_metadata_.MergeFrom(from._internal_metadata_);
  ::google::protobuf::uint32 cached_has_bits = 0;
  (void) cached_has_bits;

  if (from.msgbus_endpoint().size() > 0) {

    msgbus_endpoint_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.msgbus_endpoint_);
  }
  if (from.reason_detail().size() > 0) {

    reason_detail_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.reason_detail_);
  }
  if (from.status() != 0) {
    set_status(from.status());
  }
  if (from.reason() != 0) {
    set_reason(from.reason());
  }
}

void GetMsgBusEndpointResponse::CopyFrom(const ::google::protobuf::Message& from) {
// @@protoc_insertion_point(generalized_copy_from_start:dmi.GetMsgBusEndpointResponse)
  if (&from == this) return;
  Clear();
  MergeFrom(from);
}

void GetMsgBusEndpointResponse::CopyFrom(const GetMsgBusEndpointResponse& from) {
// @@protoc_insertion_point(class_specific_copy_from_start:dmi.GetMsgBusEndpointResponse)
  if (&from == this) return;
  Clear();
  MergeFrom(from);
}

bool GetMsgBusEndpointResponse::IsInitialized() const {
  return true;
}

void GetMsgBusEndpointResponse::Swap(GetMsgBusEndpointResponse* other) {
  if (other == this) return;
  InternalSwap(other);
}
void GetMsgBusEndpointResponse::InternalSwap(GetMsgBusEndpointResponse* other) {
  using std::swap;
  _internal_metadata_.Swap(&other->_internal_metadata_);
  msgbus_endpoint_.Swap(&other->msgbus_endpoint_, &::google::protobuf::internal::GetEmptyStringAlreadyInited(),
    GetArenaNoVirtual());
  reason_detail_.Swap(&other->reason_detail_, &::google::protobuf::internal::GetEmptyStringAlreadyInited(),
    GetArenaNoVirtual());
  swap(status_, other->status_);
  swap(reason_, other->reason_);
}

::google::protobuf::Metadata GetMsgBusEndpointResponse::GetMetadata() const {
  ::google::protobuf::internal::AssignDescriptors(&::assign_descriptors_table_dmi_2fhw_5fmanagement_5fservice_2eproto);
  return ::file_level_metadata_dmi_2fhw_5fmanagement_5fservice_2eproto[kIndexInFileMessages];
}


// ===================================================================

void EntitiesLogLevel::InitAsDefaultInstance() {
}
class EntitiesLogLevel::HasBitSetters {
 public:
};

#if !defined(_MSC_VER) || _MSC_VER >= 1900
const int EntitiesLogLevel::kLogLevelFieldNumber;
const int EntitiesLogLevel::kEntitiesFieldNumber;
#endif  // !defined(_MSC_VER) || _MSC_VER >= 1900

EntitiesLogLevel::EntitiesLogLevel()
  : ::google::protobuf::Message(), _internal_metadata_(nullptr) {
  SharedCtor();
  // @@protoc_insertion_point(constructor:dmi.EntitiesLogLevel)
}
EntitiesLogLevel::EntitiesLogLevel(const EntitiesLogLevel& from)
  : ::google::protobuf::Message(),
      _internal_metadata_(nullptr),
      entities_(from.entities_) {
  _internal_metadata_.MergeFrom(from._internal_metadata_);
  loglevel_ = from.loglevel_;
  // @@protoc_insertion_point(copy_constructor:dmi.EntitiesLogLevel)
}

void EntitiesLogLevel::SharedCtor() {
  ::google::protobuf::internal::InitSCC(
      &scc_info_EntitiesLogLevel_dmi_2fhw_5fmanagement_5fservice_2eproto.base);
  loglevel_ = 0;
}

EntitiesLogLevel::~EntitiesLogLevel() {
  // @@protoc_insertion_point(destructor:dmi.EntitiesLogLevel)
  SharedDtor();
}

void EntitiesLogLevel::SharedDtor() {
}

void EntitiesLogLevel::SetCachedSize(int size) const {
  _cached_size_.Set(size);
}
const EntitiesLogLevel& EntitiesLogLevel::default_instance() {
  ::google::protobuf::internal::InitSCC(&::scc_info_EntitiesLogLevel_dmi_2fhw_5fmanagement_5fservice_2eproto.base);
  return *internal_default_instance();
}


void EntitiesLogLevel::Clear() {
// @@protoc_insertion_point(message_clear_start:dmi.EntitiesLogLevel)
  ::google::protobuf::uint32 cached_has_bits = 0;
  // Prevent compiler warnings about cached_has_bits being unused
  (void) cached_has_bits;

  entities_.Clear();
  loglevel_ = 0;
  _internal_metadata_.Clear();
}

#if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER
const char* EntitiesLogLevel::_InternalParse(const char* begin, const char* end, void* object,
                  ::google::protobuf::internal::ParseContext* ctx) {
  auto msg = static_cast<EntitiesLogLevel*>(object);
  ::google::protobuf::int32 size; (void)size;
  int depth; (void)depth;
  ::google::protobuf::uint32 tag;
  ::google::protobuf::internal::ParseFunc parser_till_end; (void)parser_till_end;
  auto ptr = begin;
  while (ptr < end) {
    ptr = ::google::protobuf::io::Parse32(ptr, &tag);
    GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
    switch (tag >> 3) {
      // .dmi.LogLevel logLevel = 1;
      case 1: {
        if (static_cast<::google::protobuf::uint8>(tag) != 8) goto handle_unusual;
        ::google::protobuf::uint64 val = ::google::protobuf::internal::ReadVarint(&ptr);
        msg->set_loglevel(static_cast<::dmi::LogLevel>(val));
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
        break;
      }
      // repeated string entities = 2;
      case 2: {
        if (static_cast<::google::protobuf::uint8>(tag) != 18) goto handle_unusual;
        do {
          ptr = ::google::protobuf::io::ReadSize(ptr, &size);
          GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
          ctx->extra_parse_data().SetFieldName("dmi.EntitiesLogLevel.entities");
          object = msg->add_entities();
          if (size > end - ptr + ::google::protobuf::internal::ParseContext::kSlopBytes) {
            parser_till_end = ::google::protobuf::internal::GreedyStringParserUTF8;
            goto string_till_end;
          }
          GOOGLE_PROTOBUF_PARSER_ASSERT(::google::protobuf::internal::StringCheckUTF8(ptr, size, ctx));
          ::google::protobuf::internal::InlineGreedyStringParser(object, ptr, size, ctx);
          ptr += size;
          if (ptr >= end) break;
        } while ((::google::protobuf::io::UnalignedLoad<::google::protobuf::uint64>(ptr) & 255) == 18 && (ptr += 1));
        break;
      }
      default: {
      handle_unusual:
        if ((tag & 7) == 4 || tag == 0) {
          ctx->EndGroup(tag);
          return ptr;
        }
        auto res = UnknownFieldParse(tag, {_InternalParse, msg},
          ptr, end, msg->_internal_metadata_.mutable_unknown_fields(), ctx);
        ptr = res.first;
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr);
        if (res.second) return ptr;
      }
    }  // switch
  }  // while
  return ptr;
string_till_end:
  static_cast<::std::string*>(object)->clear();
  static_cast<::std::string*>(object)->reserve(size);
  goto len_delim_till_end;
len_delim_till_end:
  return ctx->StoreAndTailCall(ptr, end, {_InternalParse, msg},
                               {parser_till_end, object}, size);
}
#else  // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER
bool EntitiesLogLevel::MergePartialFromCodedStream(
    ::google::protobuf::io::CodedInputStream* input) {
#define DO_(EXPRESSION) if (!PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure
  ::google::protobuf::uint32 tag;
  // @@protoc_insertion_point(parse_start:dmi.EntitiesLogLevel)
  for (;;) {
    ::std::pair<::google::protobuf::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u);
    tag = p.first;
    if (!p.second) goto handle_unusual;
    switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) {
      // .dmi.LogLevel logLevel = 1;
      case 1: {
        if (static_cast< ::google::protobuf::uint8>(tag) == (8 & 0xFF)) {
          int value = 0;
          DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive<
                   int, ::google::protobuf::internal::WireFormatLite::TYPE_ENUM>(
                 input, &value)));
          set_loglevel(static_cast< ::dmi::LogLevel >(value));
        } else {
          goto handle_unusual;
        }
        break;
      }

      // repeated string entities = 2;
      case 2: {
        if (static_cast< ::google::protobuf::uint8>(tag) == (18 & 0xFF)) {
          DO_(::google::protobuf::internal::WireFormatLite::ReadString(
                input, this->add_entities()));
          DO_(::google::protobuf::internal::WireFormatLite::VerifyUtf8String(
            this->entities(this->entities_size() - 1).data(),
            static_cast<int>(this->entities(this->entities_size() - 1).length()),
            ::google::protobuf::internal::WireFormatLite::PARSE,
            "dmi.EntitiesLogLevel.entities"));
        } else {
          goto handle_unusual;
        }
        break;
      }

      default: {
      handle_unusual:
        if (tag == 0) {
          goto success;
        }
        DO_(::google::protobuf::internal::WireFormat::SkipField(
              input, tag, _internal_metadata_.mutable_unknown_fields()));
        break;
      }
    }
  }
success:
  // @@protoc_insertion_point(parse_success:dmi.EntitiesLogLevel)
  return true;
failure:
  // @@protoc_insertion_point(parse_failure:dmi.EntitiesLogLevel)
  return false;
#undef DO_
}
#endif  // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER

void EntitiesLogLevel::SerializeWithCachedSizes(
    ::google::protobuf::io::CodedOutputStream* output) const {
  // @@protoc_insertion_point(serialize_start:dmi.EntitiesLogLevel)
  ::google::protobuf::uint32 cached_has_bits = 0;
  (void) cached_has_bits;

  // .dmi.LogLevel logLevel = 1;
  if (this->loglevel() != 0) {
    ::google::protobuf::internal::WireFormatLite::WriteEnum(
      1, this->loglevel(), output);
  }

  // repeated string entities = 2;
  for (int i = 0, n = this->entities_size(); i < n; i++) {
    ::google::protobuf::internal::WireFormatLite::VerifyUtf8String(
      this->entities(i).data(), static_cast<int>(this->entities(i).length()),
      ::google::protobuf::internal::WireFormatLite::SERIALIZE,
      "dmi.EntitiesLogLevel.entities");
    ::google::protobuf::internal::WireFormatLite::WriteString(
      2, this->entities(i), output);
  }

  if (_internal_metadata_.have_unknown_fields()) {
    ::google::protobuf::internal::WireFormat::SerializeUnknownFields(
        _internal_metadata_.unknown_fields(), output);
  }
  // @@protoc_insertion_point(serialize_end:dmi.EntitiesLogLevel)
}

::google::protobuf::uint8* EntitiesLogLevel::InternalSerializeWithCachedSizesToArray(
    ::google::protobuf::uint8* target) const {
  // @@protoc_insertion_point(serialize_to_array_start:dmi.EntitiesLogLevel)
  ::google::protobuf::uint32 cached_has_bits = 0;
  (void) cached_has_bits;

  // .dmi.LogLevel logLevel = 1;
  if (this->loglevel() != 0) {
    target = ::google::protobuf::internal::WireFormatLite::WriteEnumToArray(
      1, this->loglevel(), target);
  }

  // repeated string entities = 2;
  for (int i = 0, n = this->entities_size(); i < n; i++) {
    ::google::protobuf::internal::WireFormatLite::VerifyUtf8String(
      this->entities(i).data(), static_cast<int>(this->entities(i).length()),
      ::google::protobuf::internal::WireFormatLite::SERIALIZE,
      "dmi.EntitiesLogLevel.entities");
    target = ::google::protobuf::internal::WireFormatLite::
      WriteStringToArray(2, this->entities(i), target);
  }

  if (_internal_metadata_.have_unknown_fields()) {
    target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray(
        _internal_metadata_.unknown_fields(), target);
  }
  // @@protoc_insertion_point(serialize_to_array_end:dmi.EntitiesLogLevel)
  return target;
}

size_t EntitiesLogLevel::ByteSizeLong() const {
// @@protoc_insertion_point(message_byte_size_start:dmi.EntitiesLogLevel)
  size_t total_size = 0;

  if (_internal_metadata_.have_unknown_fields()) {
    total_size +=
      ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize(
        _internal_metadata_.unknown_fields());
  }
  ::google::protobuf::uint32 cached_has_bits = 0;
  // Prevent compiler warnings about cached_has_bits being unused
  (void) cached_has_bits;

  // repeated string entities = 2;
  total_size += 1 *
      ::google::protobuf::internal::FromIntSize(this->entities_size());
  for (int i = 0, n = this->entities_size(); i < n; i++) {
    total_size += ::google::protobuf::internal::WireFormatLite::StringSize(
      this->entities(i));
  }

  // .dmi.LogLevel logLevel = 1;
  if (this->loglevel() != 0) {
    total_size += 1 +
      ::google::protobuf::internal::WireFormatLite::EnumSize(this->loglevel());
  }

  int cached_size = ::google::protobuf::internal::ToCachedSize(total_size);
  SetCachedSize(cached_size);
  return total_size;
}

void EntitiesLogLevel::MergeFrom(const ::google::protobuf::Message& from) {
// @@protoc_insertion_point(generalized_merge_from_start:dmi.EntitiesLogLevel)
  GOOGLE_DCHECK_NE(&from, this);
  const EntitiesLogLevel* source =
      ::google::protobuf::DynamicCastToGenerated<EntitiesLogLevel>(
          &from);
  if (source == nullptr) {
  // @@protoc_insertion_point(generalized_merge_from_cast_fail:dmi.EntitiesLogLevel)
    ::google::protobuf::internal::ReflectionOps::Merge(from, this);
  } else {
  // @@protoc_insertion_point(generalized_merge_from_cast_success:dmi.EntitiesLogLevel)
    MergeFrom(*source);
  }
}

void EntitiesLogLevel::MergeFrom(const EntitiesLogLevel& from) {
// @@protoc_insertion_point(class_specific_merge_from_start:dmi.EntitiesLogLevel)
  GOOGLE_DCHECK_NE(&from, this);
  _internal_metadata_.MergeFrom(from._internal_metadata_);
  ::google::protobuf::uint32 cached_has_bits = 0;
  (void) cached_has_bits;

  entities_.MergeFrom(from.entities_);
  if (from.loglevel() != 0) {
    set_loglevel(from.loglevel());
  }
}

void EntitiesLogLevel::CopyFrom(const ::google::protobuf::Message& from) {
// @@protoc_insertion_point(generalized_copy_from_start:dmi.EntitiesLogLevel)
  if (&from == this) return;
  Clear();
  MergeFrom(from);
}

void EntitiesLogLevel::CopyFrom(const EntitiesLogLevel& from) {
// @@protoc_insertion_point(class_specific_copy_from_start:dmi.EntitiesLogLevel)
  if (&from == this) return;
  Clear();
  MergeFrom(from);
}

bool EntitiesLogLevel::IsInitialized() const {
  return true;
}

void EntitiesLogLevel::Swap(EntitiesLogLevel* other) {
  if (other == this) return;
  InternalSwap(other);
}
void EntitiesLogLevel::InternalSwap(EntitiesLogLevel* other) {
  using std::swap;
  _internal_metadata_.Swap(&other->_internal_metadata_);
  entities_.InternalSwap(CastToBase(&other->entities_));
  swap(loglevel_, other->loglevel_);
}

::google::protobuf::Metadata EntitiesLogLevel::GetMetadata() const {
  ::google::protobuf::internal::AssignDescriptors(&::assign_descriptors_table_dmi_2fhw_5fmanagement_5fservice_2eproto);
  return ::file_level_metadata_dmi_2fhw_5fmanagement_5fservice_2eproto[kIndexInFileMessages];
}


// ===================================================================

void SetLogLevelRequest::InitAsDefaultInstance() {
  ::dmi::_SetLogLevelRequest_default_instance_._instance.get_mutable()->device_uuid_ = const_cast< ::dmi::Uuid*>(
      ::dmi::Uuid::internal_default_instance());
}
class SetLogLevelRequest::HasBitSetters {
 public:
  static const ::dmi::Uuid& device_uuid(const SetLogLevelRequest* msg);
};

const ::dmi::Uuid&
SetLogLevelRequest::HasBitSetters::device_uuid(const SetLogLevelRequest* msg) {
  return *msg->device_uuid_;
}
void SetLogLevelRequest::clear_device_uuid() {
  if (GetArenaNoVirtual() == nullptr && device_uuid_ != nullptr) {
    delete device_uuid_;
  }
  device_uuid_ = nullptr;
}
#if !defined(_MSC_VER) || _MSC_VER >= 1900
const int SetLogLevelRequest::kDeviceUuidFieldNumber;
const int SetLogLevelRequest::kLoglevelsFieldNumber;
#endif  // !defined(_MSC_VER) || _MSC_VER >= 1900

SetLogLevelRequest::SetLogLevelRequest()
  : ::google::protobuf::Message(), _internal_metadata_(nullptr) {
  SharedCtor();
  // @@protoc_insertion_point(constructor:dmi.SetLogLevelRequest)
}
SetLogLevelRequest::SetLogLevelRequest(const SetLogLevelRequest& from)
  : ::google::protobuf::Message(),
      _internal_metadata_(nullptr),
      loglevels_(from.loglevels_) {
  _internal_metadata_.MergeFrom(from._internal_metadata_);
  if (from.has_device_uuid()) {
    device_uuid_ = new ::dmi::Uuid(*from.device_uuid_);
  } else {
    device_uuid_ = nullptr;
  }
  // @@protoc_insertion_point(copy_constructor:dmi.SetLogLevelRequest)
}

void SetLogLevelRequest::SharedCtor() {
  ::google::protobuf::internal::InitSCC(
      &scc_info_SetLogLevelRequest_dmi_2fhw_5fmanagement_5fservice_2eproto.base);
  device_uuid_ = nullptr;
}

SetLogLevelRequest::~SetLogLevelRequest() {
  // @@protoc_insertion_point(destructor:dmi.SetLogLevelRequest)
  SharedDtor();
}

void SetLogLevelRequest::SharedDtor() {
  if (this != internal_default_instance()) delete device_uuid_;
}

void SetLogLevelRequest::SetCachedSize(int size) const {
  _cached_size_.Set(size);
}
const SetLogLevelRequest& SetLogLevelRequest::default_instance() {
  ::google::protobuf::internal::InitSCC(&::scc_info_SetLogLevelRequest_dmi_2fhw_5fmanagement_5fservice_2eproto.base);
  return *internal_default_instance();
}


void SetLogLevelRequest::Clear() {
// @@protoc_insertion_point(message_clear_start:dmi.SetLogLevelRequest)
  ::google::protobuf::uint32 cached_has_bits = 0;
  // Prevent compiler warnings about cached_has_bits being unused
  (void) cached_has_bits;

  loglevels_.Clear();
  if (GetArenaNoVirtual() == nullptr && device_uuid_ != nullptr) {
    delete device_uuid_;
  }
  device_uuid_ = nullptr;
  _internal_metadata_.Clear();
}

#if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER
const char* SetLogLevelRequest::_InternalParse(const char* begin, const char* end, void* object,
                  ::google::protobuf::internal::ParseContext* ctx) {
  auto msg = static_cast<SetLogLevelRequest*>(object);
  ::google::protobuf::int32 size; (void)size;
  int depth; (void)depth;
  ::google::protobuf::uint32 tag;
  ::google::protobuf::internal::ParseFunc parser_till_end; (void)parser_till_end;
  auto ptr = begin;
  while (ptr < end) {
    ptr = ::google::protobuf::io::Parse32(ptr, &tag);
    GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
    switch (tag >> 3) {
      // .dmi.Uuid device_uuid = 1;
      case 1: {
        if (static_cast<::google::protobuf::uint8>(tag) != 10) goto handle_unusual;
        ptr = ::google::protobuf::io::ReadSize(ptr, &size);
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
        parser_till_end = ::dmi::Uuid::_InternalParse;
        object = msg->mutable_device_uuid();
        if (size > end - ptr) goto len_delim_till_end;
        ptr += size;
        GOOGLE_PROTOBUF_PARSER_ASSERT(ctx->ParseExactRange(
            {parser_till_end, object}, ptr - size, ptr));
        break;
      }
      // repeated .dmi.EntitiesLogLevel loglevels = 2;
      case 2: {
        if (static_cast<::google::protobuf::uint8>(tag) != 18) goto handle_unusual;
        do {
          ptr = ::google::protobuf::io::ReadSize(ptr, &size);
          GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
          parser_till_end = ::dmi::EntitiesLogLevel::_InternalParse;
          object = msg->add_loglevels();
          if (size > end - ptr) goto len_delim_till_end;
          ptr += size;
          GOOGLE_PROTOBUF_PARSER_ASSERT(ctx->ParseExactRange(
              {parser_till_end, object}, ptr - size, ptr));
          if (ptr >= end) break;
        } while ((::google::protobuf::io::UnalignedLoad<::google::protobuf::uint64>(ptr) & 255) == 18 && (ptr += 1));
        break;
      }
      default: {
      handle_unusual:
        if ((tag & 7) == 4 || tag == 0) {
          ctx->EndGroup(tag);
          return ptr;
        }
        auto res = UnknownFieldParse(tag, {_InternalParse, msg},
          ptr, end, msg->_internal_metadata_.mutable_unknown_fields(), ctx);
        ptr = res.first;
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr);
        if (res.second) return ptr;
      }
    }  // switch
  }  // while
  return ptr;
len_delim_till_end:
  return ctx->StoreAndTailCall(ptr, end, {_InternalParse, msg},
                               {parser_till_end, object}, size);
}
#else  // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER
bool SetLogLevelRequest::MergePartialFromCodedStream(
    ::google::protobuf::io::CodedInputStream* input) {
#define DO_(EXPRESSION) if (!PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure
  ::google::protobuf::uint32 tag;
  // @@protoc_insertion_point(parse_start:dmi.SetLogLevelRequest)
  for (;;) {
    ::std::pair<::google::protobuf::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u);
    tag = p.first;
    if (!p.second) goto handle_unusual;
    switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) {
      // .dmi.Uuid device_uuid = 1;
      case 1: {
        if (static_cast< ::google::protobuf::uint8>(tag) == (10 & 0xFF)) {
          DO_(::google::protobuf::internal::WireFormatLite::ReadMessage(
               input, mutable_device_uuid()));
        } else {
          goto handle_unusual;
        }
        break;
      }

      // repeated .dmi.EntitiesLogLevel loglevels = 2;
      case 2: {
        if (static_cast< ::google::protobuf::uint8>(tag) == (18 & 0xFF)) {
          DO_(::google::protobuf::internal::WireFormatLite::ReadMessage(
                input, add_loglevels()));
        } else {
          goto handle_unusual;
        }
        break;
      }

      default: {
      handle_unusual:
        if (tag == 0) {
          goto success;
        }
        DO_(::google::protobuf::internal::WireFormat::SkipField(
              input, tag, _internal_metadata_.mutable_unknown_fields()));
        break;
      }
    }
  }
success:
  // @@protoc_insertion_point(parse_success:dmi.SetLogLevelRequest)
  return true;
failure:
  // @@protoc_insertion_point(parse_failure:dmi.SetLogLevelRequest)
  return false;
#undef DO_
}
#endif  // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER

void SetLogLevelRequest::SerializeWithCachedSizes(
    ::google::protobuf::io::CodedOutputStream* output) const {
  // @@protoc_insertion_point(serialize_start:dmi.SetLogLevelRequest)
  ::google::protobuf::uint32 cached_has_bits = 0;
  (void) cached_has_bits;

  // .dmi.Uuid device_uuid = 1;
  if (this->has_device_uuid()) {
    ::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray(
      1, HasBitSetters::device_uuid(this), output);
  }

  // repeated .dmi.EntitiesLogLevel loglevels = 2;
  for (unsigned int i = 0,
      n = static_cast<unsigned int>(this->loglevels_size()); i < n; i++) {
    ::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray(
      2,
      this->loglevels(static_cast<int>(i)),
      output);
  }

  if (_internal_metadata_.have_unknown_fields()) {
    ::google::protobuf::internal::WireFormat::SerializeUnknownFields(
        _internal_metadata_.unknown_fields(), output);
  }
  // @@protoc_insertion_point(serialize_end:dmi.SetLogLevelRequest)
}

::google::protobuf::uint8* SetLogLevelRequest::InternalSerializeWithCachedSizesToArray(
    ::google::protobuf::uint8* target) const {
  // @@protoc_insertion_point(serialize_to_array_start:dmi.SetLogLevelRequest)
  ::google::protobuf::uint32 cached_has_bits = 0;
  (void) cached_has_bits;

  // .dmi.Uuid device_uuid = 1;
  if (this->has_device_uuid()) {
    target = ::google::protobuf::internal::WireFormatLite::
      InternalWriteMessageToArray(
        1, HasBitSetters::device_uuid(this), target);
  }

  // repeated .dmi.EntitiesLogLevel loglevels = 2;
  for (unsigned int i = 0,
      n = static_cast<unsigned int>(this->loglevels_size()); i < n; i++) {
    target = ::google::protobuf::internal::WireFormatLite::
      InternalWriteMessageToArray(
        2, this->loglevels(static_cast<int>(i)), target);
  }

  if (_internal_metadata_.have_unknown_fields()) {
    target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray(
        _internal_metadata_.unknown_fields(), target);
  }
  // @@protoc_insertion_point(serialize_to_array_end:dmi.SetLogLevelRequest)
  return target;
}

size_t SetLogLevelRequest::ByteSizeLong() const {
// @@protoc_insertion_point(message_byte_size_start:dmi.SetLogLevelRequest)
  size_t total_size = 0;

  if (_internal_metadata_.have_unknown_fields()) {
    total_size +=
      ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize(
        _internal_metadata_.unknown_fields());
  }
  ::google::protobuf::uint32 cached_has_bits = 0;
  // Prevent compiler warnings about cached_has_bits being unused
  (void) cached_has_bits;

  // repeated .dmi.EntitiesLogLevel loglevels = 2;
  {
    unsigned int count = static_cast<unsigned int>(this->loglevels_size());
    total_size += 1UL * count;
    for (unsigned int i = 0; i < count; i++) {
      total_size +=
        ::google::protobuf::internal::WireFormatLite::MessageSize(
          this->loglevels(static_cast<int>(i)));
    }
  }

  // .dmi.Uuid device_uuid = 1;
  if (this->has_device_uuid()) {
    total_size += 1 +
      ::google::protobuf::internal::WireFormatLite::MessageSize(
        *device_uuid_);
  }

  int cached_size = ::google::protobuf::internal::ToCachedSize(total_size);
  SetCachedSize(cached_size);
  return total_size;
}

void SetLogLevelRequest::MergeFrom(const ::google::protobuf::Message& from) {
// @@protoc_insertion_point(generalized_merge_from_start:dmi.SetLogLevelRequest)
  GOOGLE_DCHECK_NE(&from, this);
  const SetLogLevelRequest* source =
      ::google::protobuf::DynamicCastToGenerated<SetLogLevelRequest>(
          &from);
  if (source == nullptr) {
  // @@protoc_insertion_point(generalized_merge_from_cast_fail:dmi.SetLogLevelRequest)
    ::google::protobuf::internal::ReflectionOps::Merge(from, this);
  } else {
  // @@protoc_insertion_point(generalized_merge_from_cast_success:dmi.SetLogLevelRequest)
    MergeFrom(*source);
  }
}

void SetLogLevelRequest::MergeFrom(const SetLogLevelRequest& from) {
// @@protoc_insertion_point(class_specific_merge_from_start:dmi.SetLogLevelRequest)
  GOOGLE_DCHECK_NE(&from, this);
  _internal_metadata_.MergeFrom(from._internal_metadata_);
  ::google::protobuf::uint32 cached_has_bits = 0;
  (void) cached_has_bits;

  loglevels_.MergeFrom(from.loglevels_);
  if (from.has_device_uuid()) {
    mutable_device_uuid()->::dmi::Uuid::MergeFrom(from.device_uuid());
  }
}

void SetLogLevelRequest::CopyFrom(const ::google::protobuf::Message& from) {
// @@protoc_insertion_point(generalized_copy_from_start:dmi.SetLogLevelRequest)
  if (&from == this) return;
  Clear();
  MergeFrom(from);
}

void SetLogLevelRequest::CopyFrom(const SetLogLevelRequest& from) {
// @@protoc_insertion_point(class_specific_copy_from_start:dmi.SetLogLevelRequest)
  if (&from == this) return;
  Clear();
  MergeFrom(from);
}

bool SetLogLevelRequest::IsInitialized() const {
  return true;
}

void SetLogLevelRequest::Swap(SetLogLevelRequest* other) {
  if (other == this) return;
  InternalSwap(other);
}
void SetLogLevelRequest::InternalSwap(SetLogLevelRequest* other) {
  using std::swap;
  _internal_metadata_.Swap(&other->_internal_metadata_);
  CastToBase(&loglevels_)->InternalSwap(CastToBase(&other->loglevels_));
  swap(device_uuid_, other->device_uuid_);
}

::google::protobuf::Metadata SetLogLevelRequest::GetMetadata() const {
  ::google::protobuf::internal::AssignDescriptors(&::assign_descriptors_table_dmi_2fhw_5fmanagement_5fservice_2eproto);
  return ::file_level_metadata_dmi_2fhw_5fmanagement_5fservice_2eproto[kIndexInFileMessages];
}


// ===================================================================

void SetLogLevelResponse::InitAsDefaultInstance() {
  ::dmi::_SetLogLevelResponse_default_instance_._instance.get_mutable()->device_uuid_ = const_cast< ::dmi::Uuid*>(
      ::dmi::Uuid::internal_default_instance());
}
class SetLogLevelResponse::HasBitSetters {
 public:
  static const ::dmi::Uuid& device_uuid(const SetLogLevelResponse* msg);
};

const ::dmi::Uuid&
SetLogLevelResponse::HasBitSetters::device_uuid(const SetLogLevelResponse* msg) {
  return *msg->device_uuid_;
}
void SetLogLevelResponse::clear_device_uuid() {
  if (GetArenaNoVirtual() == nullptr && device_uuid_ != nullptr) {
    delete device_uuid_;
  }
  device_uuid_ = nullptr;
}
#if !defined(_MSC_VER) || _MSC_VER >= 1900
const int SetLogLevelResponse::kDeviceUuidFieldNumber;
const int SetLogLevelResponse::kStatusFieldNumber;
const int SetLogLevelResponse::kReasonFieldNumber;
const int SetLogLevelResponse::kReasonDetailFieldNumber;
#endif  // !defined(_MSC_VER) || _MSC_VER >= 1900

SetLogLevelResponse::SetLogLevelResponse()
  : ::google::protobuf::Message(), _internal_metadata_(nullptr) {
  SharedCtor();
  // @@protoc_insertion_point(constructor:dmi.SetLogLevelResponse)
}
SetLogLevelResponse::SetLogLevelResponse(const SetLogLevelResponse& from)
  : ::google::protobuf::Message(),
      _internal_metadata_(nullptr) {
  _internal_metadata_.MergeFrom(from._internal_metadata_);
  reason_detail_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
  if (from.reason_detail().size() > 0) {
    reason_detail_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.reason_detail_);
  }
  if (from.has_device_uuid()) {
    device_uuid_ = new ::dmi::Uuid(*from.device_uuid_);
  } else {
    device_uuid_ = nullptr;
  }
  ::memcpy(&status_, &from.status_,
    static_cast<size_t>(reinterpret_cast<char*>(&reason_) -
    reinterpret_cast<char*>(&status_)) + sizeof(reason_));
  // @@protoc_insertion_point(copy_constructor:dmi.SetLogLevelResponse)
}

void SetLogLevelResponse::SharedCtor() {
  ::google::protobuf::internal::InitSCC(
      &scc_info_SetLogLevelResponse_dmi_2fhw_5fmanagement_5fservice_2eproto.base);
  reason_detail_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
  ::memset(&device_uuid_, 0, static_cast<size_t>(
      reinterpret_cast<char*>(&reason_) -
      reinterpret_cast<char*>(&device_uuid_)) + sizeof(reason_));
}

SetLogLevelResponse::~SetLogLevelResponse() {
  // @@protoc_insertion_point(destructor:dmi.SetLogLevelResponse)
  SharedDtor();
}

void SetLogLevelResponse::SharedDtor() {
  reason_detail_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
  if (this != internal_default_instance()) delete device_uuid_;
}

void SetLogLevelResponse::SetCachedSize(int size) const {
  _cached_size_.Set(size);
}
const SetLogLevelResponse& SetLogLevelResponse::default_instance() {
  ::google::protobuf::internal::InitSCC(&::scc_info_SetLogLevelResponse_dmi_2fhw_5fmanagement_5fservice_2eproto.base);
  return *internal_default_instance();
}


void SetLogLevelResponse::Clear() {
// @@protoc_insertion_point(message_clear_start:dmi.SetLogLevelResponse)
  ::google::protobuf::uint32 cached_has_bits = 0;
  // Prevent compiler warnings about cached_has_bits being unused
  (void) cached_has_bits;

  reason_detail_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
  if (GetArenaNoVirtual() == nullptr && device_uuid_ != nullptr) {
    delete device_uuid_;
  }
  device_uuid_ = nullptr;
  ::memset(&status_, 0, static_cast<size_t>(
      reinterpret_cast<char*>(&reason_) -
      reinterpret_cast<char*>(&status_)) + sizeof(reason_));
  _internal_metadata_.Clear();
}

#if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER
const char* SetLogLevelResponse::_InternalParse(const char* begin, const char* end, void* object,
                  ::google::protobuf::internal::ParseContext* ctx) {
  auto msg = static_cast<SetLogLevelResponse*>(object);
  ::google::protobuf::int32 size; (void)size;
  int depth; (void)depth;
  ::google::protobuf::uint32 tag;
  ::google::protobuf::internal::ParseFunc parser_till_end; (void)parser_till_end;
  auto ptr = begin;
  while (ptr < end) {
    ptr = ::google::protobuf::io::Parse32(ptr, &tag);
    GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
    switch (tag >> 3) {
      // .dmi.Uuid device_uuid = 1;
      case 1: {
        if (static_cast<::google::protobuf::uint8>(tag) != 10) goto handle_unusual;
        ptr = ::google::protobuf::io::ReadSize(ptr, &size);
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
        parser_till_end = ::dmi::Uuid::_InternalParse;
        object = msg->mutable_device_uuid();
        if (size > end - ptr) goto len_delim_till_end;
        ptr += size;
        GOOGLE_PROTOBUF_PARSER_ASSERT(ctx->ParseExactRange(
            {parser_till_end, object}, ptr - size, ptr));
        break;
      }
      // .dmi.Status status = 2;
      case 2: {
        if (static_cast<::google::protobuf::uint8>(tag) != 16) goto handle_unusual;
        ::google::protobuf::uint64 val = ::google::protobuf::internal::ReadVarint(&ptr);
        msg->set_status(static_cast<::dmi::Status>(val));
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
        break;
      }
      // .dmi.SetLogLevelResponse.Reason reason = 3;
      case 3: {
        if (static_cast<::google::protobuf::uint8>(tag) != 24) goto handle_unusual;
        ::google::protobuf::uint64 val = ::google::protobuf::internal::ReadVarint(&ptr);
        msg->set_reason(static_cast<::dmi::SetLogLevelResponse_Reason>(val));
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
        break;
      }
      // string reason_detail = 4;
      case 4: {
        if (static_cast<::google::protobuf::uint8>(tag) != 34) goto handle_unusual;
        ptr = ::google::protobuf::io::ReadSize(ptr, &size);
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
        ctx->extra_parse_data().SetFieldName("dmi.SetLogLevelResponse.reason_detail");
        object = msg->mutable_reason_detail();
        if (size > end - ptr + ::google::protobuf::internal::ParseContext::kSlopBytes) {
          parser_till_end = ::google::protobuf::internal::GreedyStringParserUTF8;
          goto string_till_end;
        }
        GOOGLE_PROTOBUF_PARSER_ASSERT(::google::protobuf::internal::StringCheckUTF8(ptr, size, ctx));
        ::google::protobuf::internal::InlineGreedyStringParser(object, ptr, size, ctx);
        ptr += size;
        break;
      }
      default: {
      handle_unusual:
        if ((tag & 7) == 4 || tag == 0) {
          ctx->EndGroup(tag);
          return ptr;
        }
        auto res = UnknownFieldParse(tag, {_InternalParse, msg},
          ptr, end, msg->_internal_metadata_.mutable_unknown_fields(), ctx);
        ptr = res.first;
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr);
        if (res.second) return ptr;
      }
    }  // switch
  }  // while
  return ptr;
string_till_end:
  static_cast<::std::string*>(object)->clear();
  static_cast<::std::string*>(object)->reserve(size);
  goto len_delim_till_end;
len_delim_till_end:
  return ctx->StoreAndTailCall(ptr, end, {_InternalParse, msg},
                               {parser_till_end, object}, size);
}
#else  // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER
bool SetLogLevelResponse::MergePartialFromCodedStream(
    ::google::protobuf::io::CodedInputStream* input) {
#define DO_(EXPRESSION) if (!PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure
  ::google::protobuf::uint32 tag;
  // @@protoc_insertion_point(parse_start:dmi.SetLogLevelResponse)
  for (;;) {
    ::std::pair<::google::protobuf::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u);
    tag = p.first;
    if (!p.second) goto handle_unusual;
    switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) {
      // .dmi.Uuid device_uuid = 1;
      case 1: {
        if (static_cast< ::google::protobuf::uint8>(tag) == (10 & 0xFF)) {
          DO_(::google::protobuf::internal::WireFormatLite::ReadMessage(
               input, mutable_device_uuid()));
        } else {
          goto handle_unusual;
        }
        break;
      }

      // .dmi.Status status = 2;
      case 2: {
        if (static_cast< ::google::protobuf::uint8>(tag) == (16 & 0xFF)) {
          int value = 0;
          DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive<
                   int, ::google::protobuf::internal::WireFormatLite::TYPE_ENUM>(
                 input, &value)));
          set_status(static_cast< ::dmi::Status >(value));
        } else {
          goto handle_unusual;
        }
        break;
      }

      // .dmi.SetLogLevelResponse.Reason reason = 3;
      case 3: {
        if (static_cast< ::google::protobuf::uint8>(tag) == (24 & 0xFF)) {
          int value = 0;
          DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive<
                   int, ::google::protobuf::internal::WireFormatLite::TYPE_ENUM>(
                 input, &value)));
          set_reason(static_cast< ::dmi::SetLogLevelResponse_Reason >(value));
        } else {
          goto handle_unusual;
        }
        break;
      }

      // string reason_detail = 4;
      case 4: {
        if (static_cast< ::google::protobuf::uint8>(tag) == (34 & 0xFF)) {
          DO_(::google::protobuf::internal::WireFormatLite::ReadString(
                input, this->mutable_reason_detail()));
          DO_(::google::protobuf::internal::WireFormatLite::VerifyUtf8String(
            this->reason_detail().data(), static_cast<int>(this->reason_detail().length()),
            ::google::protobuf::internal::WireFormatLite::PARSE,
            "dmi.SetLogLevelResponse.reason_detail"));
        } else {
          goto handle_unusual;
        }
        break;
      }

      default: {
      handle_unusual:
        if (tag == 0) {
          goto success;
        }
        DO_(::google::protobuf::internal::WireFormat::SkipField(
              input, tag, _internal_metadata_.mutable_unknown_fields()));
        break;
      }
    }
  }
success:
  // @@protoc_insertion_point(parse_success:dmi.SetLogLevelResponse)
  return true;
failure:
  // @@protoc_insertion_point(parse_failure:dmi.SetLogLevelResponse)
  return false;
#undef DO_
}
#endif  // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER

void SetLogLevelResponse::SerializeWithCachedSizes(
    ::google::protobuf::io::CodedOutputStream* output) const {
  // @@protoc_insertion_point(serialize_start:dmi.SetLogLevelResponse)
  ::google::protobuf::uint32 cached_has_bits = 0;
  (void) cached_has_bits;

  // .dmi.Uuid device_uuid = 1;
  if (this->has_device_uuid()) {
    ::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray(
      1, HasBitSetters::device_uuid(this), output);
  }

  // .dmi.Status status = 2;
  if (this->status() != 0) {
    ::google::protobuf::internal::WireFormatLite::WriteEnum(
      2, this->status(), output);
  }

  // .dmi.SetLogLevelResponse.Reason reason = 3;
  if (this->reason() != 0) {
    ::google::protobuf::internal::WireFormatLite::WriteEnum(
      3, this->reason(), output);
  }

  // string reason_detail = 4;
  if (this->reason_detail().size() > 0) {
    ::google::protobuf::internal::WireFormatLite::VerifyUtf8String(
      this->reason_detail().data(), static_cast<int>(this->reason_detail().length()),
      ::google::protobuf::internal::WireFormatLite::SERIALIZE,
      "dmi.SetLogLevelResponse.reason_detail");
    ::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased(
      4, this->reason_detail(), output);
  }

  if (_internal_metadata_.have_unknown_fields()) {
    ::google::protobuf::internal::WireFormat::SerializeUnknownFields(
        _internal_metadata_.unknown_fields(), output);
  }
  // @@protoc_insertion_point(serialize_end:dmi.SetLogLevelResponse)
}

::google::protobuf::uint8* SetLogLevelResponse::InternalSerializeWithCachedSizesToArray(
    ::google::protobuf::uint8* target) const {
  // @@protoc_insertion_point(serialize_to_array_start:dmi.SetLogLevelResponse)
  ::google::protobuf::uint32 cached_has_bits = 0;
  (void) cached_has_bits;

  // .dmi.Uuid device_uuid = 1;
  if (this->has_device_uuid()) {
    target = ::google::protobuf::internal::WireFormatLite::
      InternalWriteMessageToArray(
        1, HasBitSetters::device_uuid(this), target);
  }

  // .dmi.Status status = 2;
  if (this->status() != 0) {
    target = ::google::protobuf::internal::WireFormatLite::WriteEnumToArray(
      2, this->status(), target);
  }

  // .dmi.SetLogLevelResponse.Reason reason = 3;
  if (this->reason() != 0) {
    target = ::google::protobuf::internal::WireFormatLite::WriteEnumToArray(
      3, this->reason(), target);
  }

  // string reason_detail = 4;
  if (this->reason_detail().size() > 0) {
    ::google::protobuf::internal::WireFormatLite::VerifyUtf8String(
      this->reason_detail().data(), static_cast<int>(this->reason_detail().length()),
      ::google::protobuf::internal::WireFormatLite::SERIALIZE,
      "dmi.SetLogLevelResponse.reason_detail");
    target =
      ::google::protobuf::internal::WireFormatLite::WriteStringToArray(
        4, this->reason_detail(), target);
  }

  if (_internal_metadata_.have_unknown_fields()) {
    target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray(
        _internal_metadata_.unknown_fields(), target);
  }
  // @@protoc_insertion_point(serialize_to_array_end:dmi.SetLogLevelResponse)
  return target;
}

size_t SetLogLevelResponse::ByteSizeLong() const {
// @@protoc_insertion_point(message_byte_size_start:dmi.SetLogLevelResponse)
  size_t total_size = 0;

  if (_internal_metadata_.have_unknown_fields()) {
    total_size +=
      ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize(
        _internal_metadata_.unknown_fields());
  }
  ::google::protobuf::uint32 cached_has_bits = 0;
  // Prevent compiler warnings about cached_has_bits being unused
  (void) cached_has_bits;

  // string reason_detail = 4;
  if (this->reason_detail().size() > 0) {
    total_size += 1 +
      ::google::protobuf::internal::WireFormatLite::StringSize(
        this->reason_detail());
  }

  // .dmi.Uuid device_uuid = 1;
  if (this->has_device_uuid()) {
    total_size += 1 +
      ::google::protobuf::internal::WireFormatLite::MessageSize(
        *device_uuid_);
  }

  // .dmi.Status status = 2;
  if (this->status() != 0) {
    total_size += 1 +
      ::google::protobuf::internal::WireFormatLite::EnumSize(this->status());
  }

  // .dmi.SetLogLevelResponse.Reason reason = 3;
  if (this->reason() != 0) {
    total_size += 1 +
      ::google::protobuf::internal::WireFormatLite::EnumSize(this->reason());
  }

  int cached_size = ::google::protobuf::internal::ToCachedSize(total_size);
  SetCachedSize(cached_size);
  return total_size;
}

void SetLogLevelResponse::MergeFrom(const ::google::protobuf::Message& from) {
// @@protoc_insertion_point(generalized_merge_from_start:dmi.SetLogLevelResponse)
  GOOGLE_DCHECK_NE(&from, this);
  const SetLogLevelResponse* source =
      ::google::protobuf::DynamicCastToGenerated<SetLogLevelResponse>(
          &from);
  if (source == nullptr) {
  // @@protoc_insertion_point(generalized_merge_from_cast_fail:dmi.SetLogLevelResponse)
    ::google::protobuf::internal::ReflectionOps::Merge(from, this);
  } else {
  // @@protoc_insertion_point(generalized_merge_from_cast_success:dmi.SetLogLevelResponse)
    MergeFrom(*source);
  }
}

void SetLogLevelResponse::MergeFrom(const SetLogLevelResponse& from) {
// @@protoc_insertion_point(class_specific_merge_from_start:dmi.SetLogLevelResponse)
  GOOGLE_DCHECK_NE(&from, this);
  _internal_metadata_.MergeFrom(from._internal_metadata_);
  ::google::protobuf::uint32 cached_has_bits = 0;
  (void) cached_has_bits;

  if (from.reason_detail().size() > 0) {

    reason_detail_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.reason_detail_);
  }
  if (from.has_device_uuid()) {
    mutable_device_uuid()->::dmi::Uuid::MergeFrom(from.device_uuid());
  }
  if (from.status() != 0) {
    set_status(from.status());
  }
  if (from.reason() != 0) {
    set_reason(from.reason());
  }
}

void SetLogLevelResponse::CopyFrom(const ::google::protobuf::Message& from) {
// @@protoc_insertion_point(generalized_copy_from_start:dmi.SetLogLevelResponse)
  if (&from == this) return;
  Clear();
  MergeFrom(from);
}

void SetLogLevelResponse::CopyFrom(const SetLogLevelResponse& from) {
// @@protoc_insertion_point(class_specific_copy_from_start:dmi.SetLogLevelResponse)
  if (&from == this) return;
  Clear();
  MergeFrom(from);
}

bool SetLogLevelResponse::IsInitialized() const {
  return true;
}

void SetLogLevelResponse::Swap(SetLogLevelResponse* other) {
  if (other == this) return;
  InternalSwap(other);
}
void SetLogLevelResponse::InternalSwap(SetLogLevelResponse* other) {
  using std::swap;
  _internal_metadata_.Swap(&other->_internal_metadata_);
  reason_detail_.Swap(&other->reason_detail_, &::google::protobuf::internal::GetEmptyStringAlreadyInited(),
    GetArenaNoVirtual());
  swap(device_uuid_, other->device_uuid_);
  swap(status_, other->status_);
  swap(reason_, other->reason_);
}

::google::protobuf::Metadata SetLogLevelResponse::GetMetadata() const {
  ::google::protobuf::internal::AssignDescriptors(&::assign_descriptors_table_dmi_2fhw_5fmanagement_5fservice_2eproto);
  return ::file_level_metadata_dmi_2fhw_5fmanagement_5fservice_2eproto[kIndexInFileMessages];
}


// ===================================================================

void GetLogLevelRequest::InitAsDefaultInstance() {
  ::dmi::_GetLogLevelRequest_default_instance_._instance.get_mutable()->device_uuid_ = const_cast< ::dmi::Uuid*>(
      ::dmi::Uuid::internal_default_instance());
}
class GetLogLevelRequest::HasBitSetters {
 public:
  static const ::dmi::Uuid& device_uuid(const GetLogLevelRequest* msg);
};

const ::dmi::Uuid&
GetLogLevelRequest::HasBitSetters::device_uuid(const GetLogLevelRequest* msg) {
  return *msg->device_uuid_;
}
void GetLogLevelRequest::clear_device_uuid() {
  if (GetArenaNoVirtual() == nullptr && device_uuid_ != nullptr) {
    delete device_uuid_;
  }
  device_uuid_ = nullptr;
}
#if !defined(_MSC_VER) || _MSC_VER >= 1900
const int GetLogLevelRequest::kDeviceUuidFieldNumber;
const int GetLogLevelRequest::kEntitiesFieldNumber;
#endif  // !defined(_MSC_VER) || _MSC_VER >= 1900

GetLogLevelRequest::GetLogLevelRequest()
  : ::google::protobuf::Message(), _internal_metadata_(nullptr) {
  SharedCtor();
  // @@protoc_insertion_point(constructor:dmi.GetLogLevelRequest)
}
GetLogLevelRequest::GetLogLevelRequest(const GetLogLevelRequest& from)
  : ::google::protobuf::Message(),
      _internal_metadata_(nullptr),
      entities_(from.entities_) {
  _internal_metadata_.MergeFrom(from._internal_metadata_);
  if (from.has_device_uuid()) {
    device_uuid_ = new ::dmi::Uuid(*from.device_uuid_);
  } else {
    device_uuid_ = nullptr;
  }
  // @@protoc_insertion_point(copy_constructor:dmi.GetLogLevelRequest)
}

void GetLogLevelRequest::SharedCtor() {
  ::google::protobuf::internal::InitSCC(
      &scc_info_GetLogLevelRequest_dmi_2fhw_5fmanagement_5fservice_2eproto.base);
  device_uuid_ = nullptr;
}

GetLogLevelRequest::~GetLogLevelRequest() {
  // @@protoc_insertion_point(destructor:dmi.GetLogLevelRequest)
  SharedDtor();
}

void GetLogLevelRequest::SharedDtor() {
  if (this != internal_default_instance()) delete device_uuid_;
}

void GetLogLevelRequest::SetCachedSize(int size) const {
  _cached_size_.Set(size);
}
const GetLogLevelRequest& GetLogLevelRequest::default_instance() {
  ::google::protobuf::internal::InitSCC(&::scc_info_GetLogLevelRequest_dmi_2fhw_5fmanagement_5fservice_2eproto.base);
  return *internal_default_instance();
}


void GetLogLevelRequest::Clear() {
// @@protoc_insertion_point(message_clear_start:dmi.GetLogLevelRequest)
  ::google::protobuf::uint32 cached_has_bits = 0;
  // Prevent compiler warnings about cached_has_bits being unused
  (void) cached_has_bits;

  entities_.Clear();
  if (GetArenaNoVirtual() == nullptr && device_uuid_ != nullptr) {
    delete device_uuid_;
  }
  device_uuid_ = nullptr;
  _internal_metadata_.Clear();
}

#if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER
const char* GetLogLevelRequest::_InternalParse(const char* begin, const char* end, void* object,
                  ::google::protobuf::internal::ParseContext* ctx) {
  auto msg = static_cast<GetLogLevelRequest*>(object);
  ::google::protobuf::int32 size; (void)size;
  int depth; (void)depth;
  ::google::protobuf::uint32 tag;
  ::google::protobuf::internal::ParseFunc parser_till_end; (void)parser_till_end;
  auto ptr = begin;
  while (ptr < end) {
    ptr = ::google::protobuf::io::Parse32(ptr, &tag);
    GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
    switch (tag >> 3) {
      // .dmi.Uuid device_uuid = 1;
      case 1: {
        if (static_cast<::google::protobuf::uint8>(tag) != 10) goto handle_unusual;
        ptr = ::google::protobuf::io::ReadSize(ptr, &size);
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
        parser_till_end = ::dmi::Uuid::_InternalParse;
        object = msg->mutable_device_uuid();
        if (size > end - ptr) goto len_delim_till_end;
        ptr += size;
        GOOGLE_PROTOBUF_PARSER_ASSERT(ctx->ParseExactRange(
            {parser_till_end, object}, ptr - size, ptr));
        break;
      }
      // repeated string entities = 2;
      case 2: {
        if (static_cast<::google::protobuf::uint8>(tag) != 18) goto handle_unusual;
        do {
          ptr = ::google::protobuf::io::ReadSize(ptr, &size);
          GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
          ctx->extra_parse_data().SetFieldName("dmi.GetLogLevelRequest.entities");
          object = msg->add_entities();
          if (size > end - ptr + ::google::protobuf::internal::ParseContext::kSlopBytes) {
            parser_till_end = ::google::protobuf::internal::GreedyStringParserUTF8;
            goto string_till_end;
          }
          GOOGLE_PROTOBUF_PARSER_ASSERT(::google::protobuf::internal::StringCheckUTF8(ptr, size, ctx));
          ::google::protobuf::internal::InlineGreedyStringParser(object, ptr, size, ctx);
          ptr += size;
          if (ptr >= end) break;
        } while ((::google::protobuf::io::UnalignedLoad<::google::protobuf::uint64>(ptr) & 255) == 18 && (ptr += 1));
        break;
      }
      default: {
      handle_unusual:
        if ((tag & 7) == 4 || tag == 0) {
          ctx->EndGroup(tag);
          return ptr;
        }
        auto res = UnknownFieldParse(tag, {_InternalParse, msg},
          ptr, end, msg->_internal_metadata_.mutable_unknown_fields(), ctx);
        ptr = res.first;
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr);
        if (res.second) return ptr;
      }
    }  // switch
  }  // while
  return ptr;
string_till_end:
  static_cast<::std::string*>(object)->clear();
  static_cast<::std::string*>(object)->reserve(size);
  goto len_delim_till_end;
len_delim_till_end:
  return ctx->StoreAndTailCall(ptr, end, {_InternalParse, msg},
                               {parser_till_end, object}, size);
}
#else  // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER
bool GetLogLevelRequest::MergePartialFromCodedStream(
    ::google::protobuf::io::CodedInputStream* input) {
#define DO_(EXPRESSION) if (!PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure
  ::google::protobuf::uint32 tag;
  // @@protoc_insertion_point(parse_start:dmi.GetLogLevelRequest)
  for (;;) {
    ::std::pair<::google::protobuf::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u);
    tag = p.first;
    if (!p.second) goto handle_unusual;
    switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) {
      // .dmi.Uuid device_uuid = 1;
      case 1: {
        if (static_cast< ::google::protobuf::uint8>(tag) == (10 & 0xFF)) {
          DO_(::google::protobuf::internal::WireFormatLite::ReadMessage(
               input, mutable_device_uuid()));
        } else {
          goto handle_unusual;
        }
        break;
      }

      // repeated string entities = 2;
      case 2: {
        if (static_cast< ::google::protobuf::uint8>(tag) == (18 & 0xFF)) {
          DO_(::google::protobuf::internal::WireFormatLite::ReadString(
                input, this->add_entities()));
          DO_(::google::protobuf::internal::WireFormatLite::VerifyUtf8String(
            this->entities(this->entities_size() - 1).data(),
            static_cast<int>(this->entities(this->entities_size() - 1).length()),
            ::google::protobuf::internal::WireFormatLite::PARSE,
            "dmi.GetLogLevelRequest.entities"));
        } else {
          goto handle_unusual;
        }
        break;
      }

      default: {
      handle_unusual:
        if (tag == 0) {
          goto success;
        }
        DO_(::google::protobuf::internal::WireFormat::SkipField(
              input, tag, _internal_metadata_.mutable_unknown_fields()));
        break;
      }
    }
  }
success:
  // @@protoc_insertion_point(parse_success:dmi.GetLogLevelRequest)
  return true;
failure:
  // @@protoc_insertion_point(parse_failure:dmi.GetLogLevelRequest)
  return false;
#undef DO_
}
#endif  // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER

void GetLogLevelRequest::SerializeWithCachedSizes(
    ::google::protobuf::io::CodedOutputStream* output) const {
  // @@protoc_insertion_point(serialize_start:dmi.GetLogLevelRequest)
  ::google::protobuf::uint32 cached_has_bits = 0;
  (void) cached_has_bits;

  // .dmi.Uuid device_uuid = 1;
  if (this->has_device_uuid()) {
    ::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray(
      1, HasBitSetters::device_uuid(this), output);
  }

  // repeated string entities = 2;
  for (int i = 0, n = this->entities_size(); i < n; i++) {
    ::google::protobuf::internal::WireFormatLite::VerifyUtf8String(
      this->entities(i).data(), static_cast<int>(this->entities(i).length()),
      ::google::protobuf::internal::WireFormatLite::SERIALIZE,
      "dmi.GetLogLevelRequest.entities");
    ::google::protobuf::internal::WireFormatLite::WriteString(
      2, this->entities(i), output);
  }

  if (_internal_metadata_.have_unknown_fields()) {
    ::google::protobuf::internal::WireFormat::SerializeUnknownFields(
        _internal_metadata_.unknown_fields(), output);
  }
  // @@protoc_insertion_point(serialize_end:dmi.GetLogLevelRequest)
}

::google::protobuf::uint8* GetLogLevelRequest::InternalSerializeWithCachedSizesToArray(
    ::google::protobuf::uint8* target) const {
  // @@protoc_insertion_point(serialize_to_array_start:dmi.GetLogLevelRequest)
  ::google::protobuf::uint32 cached_has_bits = 0;
  (void) cached_has_bits;

  // .dmi.Uuid device_uuid = 1;
  if (this->has_device_uuid()) {
    target = ::google::protobuf::internal::WireFormatLite::
      InternalWriteMessageToArray(
        1, HasBitSetters::device_uuid(this), target);
  }

  // repeated string entities = 2;
  for (int i = 0, n = this->entities_size(); i < n; i++) {
    ::google::protobuf::internal::WireFormatLite::VerifyUtf8String(
      this->entities(i).data(), static_cast<int>(this->entities(i).length()),
      ::google::protobuf::internal::WireFormatLite::SERIALIZE,
      "dmi.GetLogLevelRequest.entities");
    target = ::google::protobuf::internal::WireFormatLite::
      WriteStringToArray(2, this->entities(i), target);
  }

  if (_internal_metadata_.have_unknown_fields()) {
    target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray(
        _internal_metadata_.unknown_fields(), target);
  }
  // @@protoc_insertion_point(serialize_to_array_end:dmi.GetLogLevelRequest)
  return target;
}

size_t GetLogLevelRequest::ByteSizeLong() const {
// @@protoc_insertion_point(message_byte_size_start:dmi.GetLogLevelRequest)
  size_t total_size = 0;

  if (_internal_metadata_.have_unknown_fields()) {
    total_size +=
      ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize(
        _internal_metadata_.unknown_fields());
  }
  ::google::protobuf::uint32 cached_has_bits = 0;
  // Prevent compiler warnings about cached_has_bits being unused
  (void) cached_has_bits;

  // repeated string entities = 2;
  total_size += 1 *
      ::google::protobuf::internal::FromIntSize(this->entities_size());
  for (int i = 0, n = this->entities_size(); i < n; i++) {
    total_size += ::google::protobuf::internal::WireFormatLite::StringSize(
      this->entities(i));
  }

  // .dmi.Uuid device_uuid = 1;
  if (this->has_device_uuid()) {
    total_size += 1 +
      ::google::protobuf::internal::WireFormatLite::MessageSize(
        *device_uuid_);
  }

  int cached_size = ::google::protobuf::internal::ToCachedSize(total_size);
  SetCachedSize(cached_size);
  return total_size;
}

void GetLogLevelRequest::MergeFrom(const ::google::protobuf::Message& from) {
// @@protoc_insertion_point(generalized_merge_from_start:dmi.GetLogLevelRequest)
  GOOGLE_DCHECK_NE(&from, this);
  const GetLogLevelRequest* source =
      ::google::protobuf::DynamicCastToGenerated<GetLogLevelRequest>(
          &from);
  if (source == nullptr) {
  // @@protoc_insertion_point(generalized_merge_from_cast_fail:dmi.GetLogLevelRequest)
    ::google::protobuf::internal::ReflectionOps::Merge(from, this);
  } else {
  // @@protoc_insertion_point(generalized_merge_from_cast_success:dmi.GetLogLevelRequest)
    MergeFrom(*source);
  }
}

void GetLogLevelRequest::MergeFrom(const GetLogLevelRequest& from) {
// @@protoc_insertion_point(class_specific_merge_from_start:dmi.GetLogLevelRequest)
  GOOGLE_DCHECK_NE(&from, this);
  _internal_metadata_.MergeFrom(from._internal_metadata_);
  ::google::protobuf::uint32 cached_has_bits = 0;
  (void) cached_has_bits;

  entities_.MergeFrom(from.entities_);
  if (from.has_device_uuid()) {
    mutable_device_uuid()->::dmi::Uuid::MergeFrom(from.device_uuid());
  }
}

void GetLogLevelRequest::CopyFrom(const ::google::protobuf::Message& from) {
// @@protoc_insertion_point(generalized_copy_from_start:dmi.GetLogLevelRequest)
  if (&from == this) return;
  Clear();
  MergeFrom(from);
}

void GetLogLevelRequest::CopyFrom(const GetLogLevelRequest& from) {
// @@protoc_insertion_point(class_specific_copy_from_start:dmi.GetLogLevelRequest)
  if (&from == this) return;
  Clear();
  MergeFrom(from);
}

bool GetLogLevelRequest::IsInitialized() const {
  return true;
}

void GetLogLevelRequest::Swap(GetLogLevelRequest* other) {
  if (other == this) return;
  InternalSwap(other);
}
void GetLogLevelRequest::InternalSwap(GetLogLevelRequest* other) {
  using std::swap;
  _internal_metadata_.Swap(&other->_internal_metadata_);
  entities_.InternalSwap(CastToBase(&other->entities_));
  swap(device_uuid_, other->device_uuid_);
}

::google::protobuf::Metadata GetLogLevelRequest::GetMetadata() const {
  ::google::protobuf::internal::AssignDescriptors(&::assign_descriptors_table_dmi_2fhw_5fmanagement_5fservice_2eproto);
  return ::file_level_metadata_dmi_2fhw_5fmanagement_5fservice_2eproto[kIndexInFileMessages];
}


// ===================================================================

void GetLogLevelResponse::InitAsDefaultInstance() {
  ::dmi::_GetLogLevelResponse_default_instance_._instance.get_mutable()->device_uuid_ = const_cast< ::dmi::Uuid*>(
      ::dmi::Uuid::internal_default_instance());
}
class GetLogLevelResponse::HasBitSetters {
 public:
  static const ::dmi::Uuid& device_uuid(const GetLogLevelResponse* msg);
};

const ::dmi::Uuid&
GetLogLevelResponse::HasBitSetters::device_uuid(const GetLogLevelResponse* msg) {
  return *msg->device_uuid_;
}
void GetLogLevelResponse::clear_device_uuid() {
  if (GetArenaNoVirtual() == nullptr && device_uuid_ != nullptr) {
    delete device_uuid_;
  }
  device_uuid_ = nullptr;
}
#if !defined(_MSC_VER) || _MSC_VER >= 1900
const int GetLogLevelResponse::kDeviceUuidFieldNumber;
const int GetLogLevelResponse::kLogLevelsFieldNumber;
const int GetLogLevelResponse::kStatusFieldNumber;
const int GetLogLevelResponse::kReasonFieldNumber;
const int GetLogLevelResponse::kReasonDetailFieldNumber;
#endif  // !defined(_MSC_VER) || _MSC_VER >= 1900

GetLogLevelResponse::GetLogLevelResponse()
  : ::google::protobuf::Message(), _internal_metadata_(nullptr) {
  SharedCtor();
  // @@protoc_insertion_point(constructor:dmi.GetLogLevelResponse)
}
GetLogLevelResponse::GetLogLevelResponse(const GetLogLevelResponse& from)
  : ::google::protobuf::Message(),
      _internal_metadata_(nullptr),
      loglevels_(from.loglevels_) {
  _internal_metadata_.MergeFrom(from._internal_metadata_);
  reason_detail_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
  if (from.reason_detail().size() > 0) {
    reason_detail_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.reason_detail_);
  }
  if (from.has_device_uuid()) {
    device_uuid_ = new ::dmi::Uuid(*from.device_uuid_);
  } else {
    device_uuid_ = nullptr;
  }
  ::memcpy(&status_, &from.status_,
    static_cast<size_t>(reinterpret_cast<char*>(&reason_) -
    reinterpret_cast<char*>(&status_)) + sizeof(reason_));
  // @@protoc_insertion_point(copy_constructor:dmi.GetLogLevelResponse)
}

void GetLogLevelResponse::SharedCtor() {
  ::google::protobuf::internal::InitSCC(
      &scc_info_GetLogLevelResponse_dmi_2fhw_5fmanagement_5fservice_2eproto.base);
  reason_detail_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
  ::memset(&device_uuid_, 0, static_cast<size_t>(
      reinterpret_cast<char*>(&reason_) -
      reinterpret_cast<char*>(&device_uuid_)) + sizeof(reason_));
}

GetLogLevelResponse::~GetLogLevelResponse() {
  // @@protoc_insertion_point(destructor:dmi.GetLogLevelResponse)
  SharedDtor();
}

void GetLogLevelResponse::SharedDtor() {
  reason_detail_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
  if (this != internal_default_instance()) delete device_uuid_;
}

void GetLogLevelResponse::SetCachedSize(int size) const {
  _cached_size_.Set(size);
}
const GetLogLevelResponse& GetLogLevelResponse::default_instance() {
  ::google::protobuf::internal::InitSCC(&::scc_info_GetLogLevelResponse_dmi_2fhw_5fmanagement_5fservice_2eproto.base);
  return *internal_default_instance();
}


void GetLogLevelResponse::Clear() {
// @@protoc_insertion_point(message_clear_start:dmi.GetLogLevelResponse)
  ::google::protobuf::uint32 cached_has_bits = 0;
  // Prevent compiler warnings about cached_has_bits being unused
  (void) cached_has_bits;

  loglevels_.Clear();
  reason_detail_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
  if (GetArenaNoVirtual() == nullptr && device_uuid_ != nullptr) {
    delete device_uuid_;
  }
  device_uuid_ = nullptr;
  ::memset(&status_, 0, static_cast<size_t>(
      reinterpret_cast<char*>(&reason_) -
      reinterpret_cast<char*>(&status_)) + sizeof(reason_));
  _internal_metadata_.Clear();
}

#if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER
const char* GetLogLevelResponse::_InternalParse(const char* begin, const char* end, void* object,
                  ::google::protobuf::internal::ParseContext* ctx) {
  auto msg = static_cast<GetLogLevelResponse*>(object);
  ::google::protobuf::int32 size; (void)size;
  int depth; (void)depth;
  ::google::protobuf::uint32 tag;
  ::google::protobuf::internal::ParseFunc parser_till_end; (void)parser_till_end;
  auto ptr = begin;
  while (ptr < end) {
    ptr = ::google::protobuf::io::Parse32(ptr, &tag);
    GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
    switch (tag >> 3) {
      // .dmi.Uuid device_uuid = 1;
      case 1: {
        if (static_cast<::google::protobuf::uint8>(tag) != 10) goto handle_unusual;
        ptr = ::google::protobuf::io::ReadSize(ptr, &size);
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
        parser_till_end = ::dmi::Uuid::_InternalParse;
        object = msg->mutable_device_uuid();
        if (size > end - ptr) goto len_delim_till_end;
        ptr += size;
        GOOGLE_PROTOBUF_PARSER_ASSERT(ctx->ParseExactRange(
            {parser_till_end, object}, ptr - size, ptr));
        break;
      }
      // repeated .dmi.EntitiesLogLevel logLevels = 2;
      case 2: {
        if (static_cast<::google::protobuf::uint8>(tag) != 18) goto handle_unusual;
        do {
          ptr = ::google::protobuf::io::ReadSize(ptr, &size);
          GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
          parser_till_end = ::dmi::EntitiesLogLevel::_InternalParse;
          object = msg->add_loglevels();
          if (size > end - ptr) goto len_delim_till_end;
          ptr += size;
          GOOGLE_PROTOBUF_PARSER_ASSERT(ctx->ParseExactRange(
              {parser_till_end, object}, ptr - size, ptr));
          if (ptr >= end) break;
        } while ((::google::protobuf::io::UnalignedLoad<::google::protobuf::uint64>(ptr) & 255) == 18 && (ptr += 1));
        break;
      }
      // .dmi.Status status = 3;
      case 3: {
        if (static_cast<::google::protobuf::uint8>(tag) != 24) goto handle_unusual;
        ::google::protobuf::uint64 val = ::google::protobuf::internal::ReadVarint(&ptr);
        msg->set_status(static_cast<::dmi::Status>(val));
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
        break;
      }
      // .dmi.GetLogLevelResponse.Reason reason = 4;
      case 4: {
        if (static_cast<::google::protobuf::uint8>(tag) != 32) goto handle_unusual;
        ::google::protobuf::uint64 val = ::google::protobuf::internal::ReadVarint(&ptr);
        msg->set_reason(static_cast<::dmi::GetLogLevelResponse_Reason>(val));
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
        break;
      }
      // string reason_detail = 5;
      case 5: {
        if (static_cast<::google::protobuf::uint8>(tag) != 42) goto handle_unusual;
        ptr = ::google::protobuf::io::ReadSize(ptr, &size);
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
        ctx->extra_parse_data().SetFieldName("dmi.GetLogLevelResponse.reason_detail");
        object = msg->mutable_reason_detail();
        if (size > end - ptr + ::google::protobuf::internal::ParseContext::kSlopBytes) {
          parser_till_end = ::google::protobuf::internal::GreedyStringParserUTF8;
          goto string_till_end;
        }
        GOOGLE_PROTOBUF_PARSER_ASSERT(::google::protobuf::internal::StringCheckUTF8(ptr, size, ctx));
        ::google::protobuf::internal::InlineGreedyStringParser(object, ptr, size, ctx);
        ptr += size;
        break;
      }
      default: {
      handle_unusual:
        if ((tag & 7) == 4 || tag == 0) {
          ctx->EndGroup(tag);
          return ptr;
        }
        auto res = UnknownFieldParse(tag, {_InternalParse, msg},
          ptr, end, msg->_internal_metadata_.mutable_unknown_fields(), ctx);
        ptr = res.first;
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr);
        if (res.second) return ptr;
      }
    }  // switch
  }  // while
  return ptr;
string_till_end:
  static_cast<::std::string*>(object)->clear();
  static_cast<::std::string*>(object)->reserve(size);
  goto len_delim_till_end;
len_delim_till_end:
  return ctx->StoreAndTailCall(ptr, end, {_InternalParse, msg},
                               {parser_till_end, object}, size);
}
#else  // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER
bool GetLogLevelResponse::MergePartialFromCodedStream(
    ::google::protobuf::io::CodedInputStream* input) {
#define DO_(EXPRESSION) if (!PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure
  ::google::protobuf::uint32 tag;
  // @@protoc_insertion_point(parse_start:dmi.GetLogLevelResponse)
  for (;;) {
    ::std::pair<::google::protobuf::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u);
    tag = p.first;
    if (!p.second) goto handle_unusual;
    switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) {
      // .dmi.Uuid device_uuid = 1;
      case 1: {
        if (static_cast< ::google::protobuf::uint8>(tag) == (10 & 0xFF)) {
          DO_(::google::protobuf::internal::WireFormatLite::ReadMessage(
               input, mutable_device_uuid()));
        } else {
          goto handle_unusual;
        }
        break;
      }

      // repeated .dmi.EntitiesLogLevel logLevels = 2;
      case 2: {
        if (static_cast< ::google::protobuf::uint8>(tag) == (18 & 0xFF)) {
          DO_(::google::protobuf::internal::WireFormatLite::ReadMessage(
                input, add_loglevels()));
        } else {
          goto handle_unusual;
        }
        break;
      }

      // .dmi.Status status = 3;
      case 3: {
        if (static_cast< ::google::protobuf::uint8>(tag) == (24 & 0xFF)) {
          int value = 0;
          DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive<
                   int, ::google::protobuf::internal::WireFormatLite::TYPE_ENUM>(
                 input, &value)));
          set_status(static_cast< ::dmi::Status >(value));
        } else {
          goto handle_unusual;
        }
        break;
      }

      // .dmi.GetLogLevelResponse.Reason reason = 4;
      case 4: {
        if (static_cast< ::google::protobuf::uint8>(tag) == (32 & 0xFF)) {
          int value = 0;
          DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive<
                   int, ::google::protobuf::internal::WireFormatLite::TYPE_ENUM>(
                 input, &value)));
          set_reason(static_cast< ::dmi::GetLogLevelResponse_Reason >(value));
        } else {
          goto handle_unusual;
        }
        break;
      }

      // string reason_detail = 5;
      case 5: {
        if (static_cast< ::google::protobuf::uint8>(tag) == (42 & 0xFF)) {
          DO_(::google::protobuf::internal::WireFormatLite::ReadString(
                input, this->mutable_reason_detail()));
          DO_(::google::protobuf::internal::WireFormatLite::VerifyUtf8String(
            this->reason_detail().data(), static_cast<int>(this->reason_detail().length()),
            ::google::protobuf::internal::WireFormatLite::PARSE,
            "dmi.GetLogLevelResponse.reason_detail"));
        } else {
          goto handle_unusual;
        }
        break;
      }

      default: {
      handle_unusual:
        if (tag == 0) {
          goto success;
        }
        DO_(::google::protobuf::internal::WireFormat::SkipField(
              input, tag, _internal_metadata_.mutable_unknown_fields()));
        break;
      }
    }
  }
success:
  // @@protoc_insertion_point(parse_success:dmi.GetLogLevelResponse)
  return true;
failure:
  // @@protoc_insertion_point(parse_failure:dmi.GetLogLevelResponse)
  return false;
#undef DO_
}
#endif  // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER

void GetLogLevelResponse::SerializeWithCachedSizes(
    ::google::protobuf::io::CodedOutputStream* output) const {
  // @@protoc_insertion_point(serialize_start:dmi.GetLogLevelResponse)
  ::google::protobuf::uint32 cached_has_bits = 0;
  (void) cached_has_bits;

  // .dmi.Uuid device_uuid = 1;
  if (this->has_device_uuid()) {
    ::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray(
      1, HasBitSetters::device_uuid(this), output);
  }

  // repeated .dmi.EntitiesLogLevel logLevels = 2;
  for (unsigned int i = 0,
      n = static_cast<unsigned int>(this->loglevels_size()); i < n; i++) {
    ::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray(
      2,
      this->loglevels(static_cast<int>(i)),
      output);
  }

  // .dmi.Status status = 3;
  if (this->status() != 0) {
    ::google::protobuf::internal::WireFormatLite::WriteEnum(
      3, this->status(), output);
  }

  // .dmi.GetLogLevelResponse.Reason reason = 4;
  if (this->reason() != 0) {
    ::google::protobuf::internal::WireFormatLite::WriteEnum(
      4, this->reason(), output);
  }

  // string reason_detail = 5;
  if (this->reason_detail().size() > 0) {
    ::google::protobuf::internal::WireFormatLite::VerifyUtf8String(
      this->reason_detail().data(), static_cast<int>(this->reason_detail().length()),
      ::google::protobuf::internal::WireFormatLite::SERIALIZE,
      "dmi.GetLogLevelResponse.reason_detail");
    ::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased(
      5, this->reason_detail(), output);
  }

  if (_internal_metadata_.have_unknown_fields()) {
    ::google::protobuf::internal::WireFormat::SerializeUnknownFields(
        _internal_metadata_.unknown_fields(), output);
  }
  // @@protoc_insertion_point(serialize_end:dmi.GetLogLevelResponse)
}

::google::protobuf::uint8* GetLogLevelResponse::InternalSerializeWithCachedSizesToArray(
    ::google::protobuf::uint8* target) const {
  // @@protoc_insertion_point(serialize_to_array_start:dmi.GetLogLevelResponse)
  ::google::protobuf::uint32 cached_has_bits = 0;
  (void) cached_has_bits;

  // .dmi.Uuid device_uuid = 1;
  if (this->has_device_uuid()) {
    target = ::google::protobuf::internal::WireFormatLite::
      InternalWriteMessageToArray(
        1, HasBitSetters::device_uuid(this), target);
  }

  // repeated .dmi.EntitiesLogLevel logLevels = 2;
  for (unsigned int i = 0,
      n = static_cast<unsigned int>(this->loglevels_size()); i < n; i++) {
    target = ::google::protobuf::internal::WireFormatLite::
      InternalWriteMessageToArray(
        2, this->loglevels(static_cast<int>(i)), target);
  }

  // .dmi.Status status = 3;
  if (this->status() != 0) {
    target = ::google::protobuf::internal::WireFormatLite::WriteEnumToArray(
      3, this->status(), target);
  }

  // .dmi.GetLogLevelResponse.Reason reason = 4;
  if (this->reason() != 0) {
    target = ::google::protobuf::internal::WireFormatLite::WriteEnumToArray(
      4, this->reason(), target);
  }

  // string reason_detail = 5;
  if (this->reason_detail().size() > 0) {
    ::google::protobuf::internal::WireFormatLite::VerifyUtf8String(
      this->reason_detail().data(), static_cast<int>(this->reason_detail().length()),
      ::google::protobuf::internal::WireFormatLite::SERIALIZE,
      "dmi.GetLogLevelResponse.reason_detail");
    target =
      ::google::protobuf::internal::WireFormatLite::WriteStringToArray(
        5, this->reason_detail(), target);
  }

  if (_internal_metadata_.have_unknown_fields()) {
    target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray(
        _internal_metadata_.unknown_fields(), target);
  }
  // @@protoc_insertion_point(serialize_to_array_end:dmi.GetLogLevelResponse)
  return target;
}

size_t GetLogLevelResponse::ByteSizeLong() const {
// @@protoc_insertion_point(message_byte_size_start:dmi.GetLogLevelResponse)
  size_t total_size = 0;

  if (_internal_metadata_.have_unknown_fields()) {
    total_size +=
      ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize(
        _internal_metadata_.unknown_fields());
  }
  ::google::protobuf::uint32 cached_has_bits = 0;
  // Prevent compiler warnings about cached_has_bits being unused
  (void) cached_has_bits;

  // repeated .dmi.EntitiesLogLevel logLevels = 2;
  {
    unsigned int count = static_cast<unsigned int>(this->loglevels_size());
    total_size += 1UL * count;
    for (unsigned int i = 0; i < count; i++) {
      total_size +=
        ::google::protobuf::internal::WireFormatLite::MessageSize(
          this->loglevels(static_cast<int>(i)));
    }
  }

  // string reason_detail = 5;
  if (this->reason_detail().size() > 0) {
    total_size += 1 +
      ::google::protobuf::internal::WireFormatLite::StringSize(
        this->reason_detail());
  }

  // .dmi.Uuid device_uuid = 1;
  if (this->has_device_uuid()) {
    total_size += 1 +
      ::google::protobuf::internal::WireFormatLite::MessageSize(
        *device_uuid_);
  }

  // .dmi.Status status = 3;
  if (this->status() != 0) {
    total_size += 1 +
      ::google::protobuf::internal::WireFormatLite::EnumSize(this->status());
  }

  // .dmi.GetLogLevelResponse.Reason reason = 4;
  if (this->reason() != 0) {
    total_size += 1 +
      ::google::protobuf::internal::WireFormatLite::EnumSize(this->reason());
  }

  int cached_size = ::google::protobuf::internal::ToCachedSize(total_size);
  SetCachedSize(cached_size);
  return total_size;
}

void GetLogLevelResponse::MergeFrom(const ::google::protobuf::Message& from) {
// @@protoc_insertion_point(generalized_merge_from_start:dmi.GetLogLevelResponse)
  GOOGLE_DCHECK_NE(&from, this);
  const GetLogLevelResponse* source =
      ::google::protobuf::DynamicCastToGenerated<GetLogLevelResponse>(
          &from);
  if (source == nullptr) {
  // @@protoc_insertion_point(generalized_merge_from_cast_fail:dmi.GetLogLevelResponse)
    ::google::protobuf::internal::ReflectionOps::Merge(from, this);
  } else {
  // @@protoc_insertion_point(generalized_merge_from_cast_success:dmi.GetLogLevelResponse)
    MergeFrom(*source);
  }
}

void GetLogLevelResponse::MergeFrom(const GetLogLevelResponse& from) {
// @@protoc_insertion_point(class_specific_merge_from_start:dmi.GetLogLevelResponse)
  GOOGLE_DCHECK_NE(&from, this);
  _internal_metadata_.MergeFrom(from._internal_metadata_);
  ::google::protobuf::uint32 cached_has_bits = 0;
  (void) cached_has_bits;

  loglevels_.MergeFrom(from.loglevels_);
  if (from.reason_detail().size() > 0) {

    reason_detail_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.reason_detail_);
  }
  if (from.has_device_uuid()) {
    mutable_device_uuid()->::dmi::Uuid::MergeFrom(from.device_uuid());
  }
  if (from.status() != 0) {
    set_status(from.status());
  }
  if (from.reason() != 0) {
    set_reason(from.reason());
  }
}

void GetLogLevelResponse::CopyFrom(const ::google::protobuf::Message& from) {
// @@protoc_insertion_point(generalized_copy_from_start:dmi.GetLogLevelResponse)
  if (&from == this) return;
  Clear();
  MergeFrom(from);
}

void GetLogLevelResponse::CopyFrom(const GetLogLevelResponse& from) {
// @@protoc_insertion_point(class_specific_copy_from_start:dmi.GetLogLevelResponse)
  if (&from == this) return;
  Clear();
  MergeFrom(from);
}

bool GetLogLevelResponse::IsInitialized() const {
  return true;
}

void GetLogLevelResponse::Swap(GetLogLevelResponse* other) {
  if (other == this) return;
  InternalSwap(other);
}
void GetLogLevelResponse::InternalSwap(GetLogLevelResponse* other) {
  using std::swap;
  _internal_metadata_.Swap(&other->_internal_metadata_);
  CastToBase(&loglevels_)->InternalSwap(CastToBase(&other->loglevels_));
  reason_detail_.Swap(&other->reason_detail_, &::google::protobuf::internal::GetEmptyStringAlreadyInited(),
    GetArenaNoVirtual());
  swap(device_uuid_, other->device_uuid_);
  swap(status_, other->status_);
  swap(reason_, other->reason_);
}

::google::protobuf::Metadata GetLogLevelResponse::GetMetadata() const {
  ::google::protobuf::internal::AssignDescriptors(&::assign_descriptors_table_dmi_2fhw_5fmanagement_5fservice_2eproto);
  return ::file_level_metadata_dmi_2fhw_5fmanagement_5fservice_2eproto[kIndexInFileMessages];
}


// ===================================================================

void GetLoggableEntitiesRequest::InitAsDefaultInstance() {
  ::dmi::_GetLoggableEntitiesRequest_default_instance_._instance.get_mutable()->device_uuid_ = const_cast< ::dmi::Uuid*>(
      ::dmi::Uuid::internal_default_instance());
}
class GetLoggableEntitiesRequest::HasBitSetters {
 public:
  static const ::dmi::Uuid& device_uuid(const GetLoggableEntitiesRequest* msg);
};

const ::dmi::Uuid&
GetLoggableEntitiesRequest::HasBitSetters::device_uuid(const GetLoggableEntitiesRequest* msg) {
  return *msg->device_uuid_;
}
void GetLoggableEntitiesRequest::clear_device_uuid() {
  if (GetArenaNoVirtual() == nullptr && device_uuid_ != nullptr) {
    delete device_uuid_;
  }
  device_uuid_ = nullptr;
}
#if !defined(_MSC_VER) || _MSC_VER >= 1900
const int GetLoggableEntitiesRequest::kDeviceUuidFieldNumber;
#endif  // !defined(_MSC_VER) || _MSC_VER >= 1900

GetLoggableEntitiesRequest::GetLoggableEntitiesRequest()
  : ::google::protobuf::Message(), _internal_metadata_(nullptr) {
  SharedCtor();
  // @@protoc_insertion_point(constructor:dmi.GetLoggableEntitiesRequest)
}
GetLoggableEntitiesRequest::GetLoggableEntitiesRequest(const GetLoggableEntitiesRequest& from)
  : ::google::protobuf::Message(),
      _internal_metadata_(nullptr) {
  _internal_metadata_.MergeFrom(from._internal_metadata_);
  if (from.has_device_uuid()) {
    device_uuid_ = new ::dmi::Uuid(*from.device_uuid_);
  } else {
    device_uuid_ = nullptr;
  }
  // @@protoc_insertion_point(copy_constructor:dmi.GetLoggableEntitiesRequest)
}

void GetLoggableEntitiesRequest::SharedCtor() {
  ::google::protobuf::internal::InitSCC(
      &scc_info_GetLoggableEntitiesRequest_dmi_2fhw_5fmanagement_5fservice_2eproto.base);
  device_uuid_ = nullptr;
}

GetLoggableEntitiesRequest::~GetLoggableEntitiesRequest() {
  // @@protoc_insertion_point(destructor:dmi.GetLoggableEntitiesRequest)
  SharedDtor();
}

void GetLoggableEntitiesRequest::SharedDtor() {
  if (this != internal_default_instance()) delete device_uuid_;
}

void GetLoggableEntitiesRequest::SetCachedSize(int size) const {
  _cached_size_.Set(size);
}
const GetLoggableEntitiesRequest& GetLoggableEntitiesRequest::default_instance() {
  ::google::protobuf::internal::InitSCC(&::scc_info_GetLoggableEntitiesRequest_dmi_2fhw_5fmanagement_5fservice_2eproto.base);
  return *internal_default_instance();
}


void GetLoggableEntitiesRequest::Clear() {
// @@protoc_insertion_point(message_clear_start:dmi.GetLoggableEntitiesRequest)
  ::google::protobuf::uint32 cached_has_bits = 0;
  // Prevent compiler warnings about cached_has_bits being unused
  (void) cached_has_bits;

  if (GetArenaNoVirtual() == nullptr && device_uuid_ != nullptr) {
    delete device_uuid_;
  }
  device_uuid_ = nullptr;
  _internal_metadata_.Clear();
}

#if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER
const char* GetLoggableEntitiesRequest::_InternalParse(const char* begin, const char* end, void* object,
                  ::google::protobuf::internal::ParseContext* ctx) {
  auto msg = static_cast<GetLoggableEntitiesRequest*>(object);
  ::google::protobuf::int32 size; (void)size;
  int depth; (void)depth;
  ::google::protobuf::uint32 tag;
  ::google::protobuf::internal::ParseFunc parser_till_end; (void)parser_till_end;
  auto ptr = begin;
  while (ptr < end) {
    ptr = ::google::protobuf::io::Parse32(ptr, &tag);
    GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
    switch (tag >> 3) {
      // .dmi.Uuid device_uuid = 1;
      case 1: {
        if (static_cast<::google::protobuf::uint8>(tag) != 10) goto handle_unusual;
        ptr = ::google::protobuf::io::ReadSize(ptr, &size);
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
        parser_till_end = ::dmi::Uuid::_InternalParse;
        object = msg->mutable_device_uuid();
        if (size > end - ptr) goto len_delim_till_end;
        ptr += size;
        GOOGLE_PROTOBUF_PARSER_ASSERT(ctx->ParseExactRange(
            {parser_till_end, object}, ptr - size, ptr));
        break;
      }
      default: {
      handle_unusual:
        if ((tag & 7) == 4 || tag == 0) {
          ctx->EndGroup(tag);
          return ptr;
        }
        auto res = UnknownFieldParse(tag, {_InternalParse, msg},
          ptr, end, msg->_internal_metadata_.mutable_unknown_fields(), ctx);
        ptr = res.first;
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr);
        if (res.second) return ptr;
      }
    }  // switch
  }  // while
  return ptr;
len_delim_till_end:
  return ctx->StoreAndTailCall(ptr, end, {_InternalParse, msg},
                               {parser_till_end, object}, size);
}
#else  // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER
bool GetLoggableEntitiesRequest::MergePartialFromCodedStream(
    ::google::protobuf::io::CodedInputStream* input) {
#define DO_(EXPRESSION) if (!PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure
  ::google::protobuf::uint32 tag;
  // @@protoc_insertion_point(parse_start:dmi.GetLoggableEntitiesRequest)
  for (;;) {
    ::std::pair<::google::protobuf::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u);
    tag = p.first;
    if (!p.second) goto handle_unusual;
    switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) {
      // .dmi.Uuid device_uuid = 1;
      case 1: {
        if (static_cast< ::google::protobuf::uint8>(tag) == (10 & 0xFF)) {
          DO_(::google::protobuf::internal::WireFormatLite::ReadMessage(
               input, mutable_device_uuid()));
        } else {
          goto handle_unusual;
        }
        break;
      }

      default: {
      handle_unusual:
        if (tag == 0) {
          goto success;
        }
        DO_(::google::protobuf::internal::WireFormat::SkipField(
              input, tag, _internal_metadata_.mutable_unknown_fields()));
        break;
      }
    }
  }
success:
  // @@protoc_insertion_point(parse_success:dmi.GetLoggableEntitiesRequest)
  return true;
failure:
  // @@protoc_insertion_point(parse_failure:dmi.GetLoggableEntitiesRequest)
  return false;
#undef DO_
}
#endif  // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER

void GetLoggableEntitiesRequest::SerializeWithCachedSizes(
    ::google::protobuf::io::CodedOutputStream* output) const {
  // @@protoc_insertion_point(serialize_start:dmi.GetLoggableEntitiesRequest)
  ::google::protobuf::uint32 cached_has_bits = 0;
  (void) cached_has_bits;

  // .dmi.Uuid device_uuid = 1;
  if (this->has_device_uuid()) {
    ::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray(
      1, HasBitSetters::device_uuid(this), output);
  }

  if (_internal_metadata_.have_unknown_fields()) {
    ::google::protobuf::internal::WireFormat::SerializeUnknownFields(
        _internal_metadata_.unknown_fields(), output);
  }
  // @@protoc_insertion_point(serialize_end:dmi.GetLoggableEntitiesRequest)
}

::google::protobuf::uint8* GetLoggableEntitiesRequest::InternalSerializeWithCachedSizesToArray(
    ::google::protobuf::uint8* target) const {
  // @@protoc_insertion_point(serialize_to_array_start:dmi.GetLoggableEntitiesRequest)
  ::google::protobuf::uint32 cached_has_bits = 0;
  (void) cached_has_bits;

  // .dmi.Uuid device_uuid = 1;
  if (this->has_device_uuid()) {
    target = ::google::protobuf::internal::WireFormatLite::
      InternalWriteMessageToArray(
        1, HasBitSetters::device_uuid(this), target);
  }

  if (_internal_metadata_.have_unknown_fields()) {
    target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray(
        _internal_metadata_.unknown_fields(), target);
  }
  // @@protoc_insertion_point(serialize_to_array_end:dmi.GetLoggableEntitiesRequest)
  return target;
}

size_t GetLoggableEntitiesRequest::ByteSizeLong() const {
// @@protoc_insertion_point(message_byte_size_start:dmi.GetLoggableEntitiesRequest)
  size_t total_size = 0;

  if (_internal_metadata_.have_unknown_fields()) {
    total_size +=
      ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize(
        _internal_metadata_.unknown_fields());
  }
  ::google::protobuf::uint32 cached_has_bits = 0;
  // Prevent compiler warnings about cached_has_bits being unused
  (void) cached_has_bits;

  // .dmi.Uuid device_uuid = 1;
  if (this->has_device_uuid()) {
    total_size += 1 +
      ::google::protobuf::internal::WireFormatLite::MessageSize(
        *device_uuid_);
  }

  int cached_size = ::google::protobuf::internal::ToCachedSize(total_size);
  SetCachedSize(cached_size);
  return total_size;
}

void GetLoggableEntitiesRequest::MergeFrom(const ::google::protobuf::Message& from) {
// @@protoc_insertion_point(generalized_merge_from_start:dmi.GetLoggableEntitiesRequest)
  GOOGLE_DCHECK_NE(&from, this);
  const GetLoggableEntitiesRequest* source =
      ::google::protobuf::DynamicCastToGenerated<GetLoggableEntitiesRequest>(
          &from);
  if (source == nullptr) {
  // @@protoc_insertion_point(generalized_merge_from_cast_fail:dmi.GetLoggableEntitiesRequest)
    ::google::protobuf::internal::ReflectionOps::Merge(from, this);
  } else {
  // @@protoc_insertion_point(generalized_merge_from_cast_success:dmi.GetLoggableEntitiesRequest)
    MergeFrom(*source);
  }
}

void GetLoggableEntitiesRequest::MergeFrom(const GetLoggableEntitiesRequest& from) {
// @@protoc_insertion_point(class_specific_merge_from_start:dmi.GetLoggableEntitiesRequest)
  GOOGLE_DCHECK_NE(&from, this);
  _internal_metadata_.MergeFrom(from._internal_metadata_);
  ::google::protobuf::uint32 cached_has_bits = 0;
  (void) cached_has_bits;

  if (from.has_device_uuid()) {
    mutable_device_uuid()->::dmi::Uuid::MergeFrom(from.device_uuid());
  }
}

void GetLoggableEntitiesRequest::CopyFrom(const ::google::protobuf::Message& from) {
// @@protoc_insertion_point(generalized_copy_from_start:dmi.GetLoggableEntitiesRequest)
  if (&from == this) return;
  Clear();
  MergeFrom(from);
}

void GetLoggableEntitiesRequest::CopyFrom(const GetLoggableEntitiesRequest& from) {
// @@protoc_insertion_point(class_specific_copy_from_start:dmi.GetLoggableEntitiesRequest)
  if (&from == this) return;
  Clear();
  MergeFrom(from);
}

bool GetLoggableEntitiesRequest::IsInitialized() const {
  return true;
}

void GetLoggableEntitiesRequest::Swap(GetLoggableEntitiesRequest* other) {
  if (other == this) return;
  InternalSwap(other);
}
void GetLoggableEntitiesRequest::InternalSwap(GetLoggableEntitiesRequest* other) {
  using std::swap;
  _internal_metadata_.Swap(&other->_internal_metadata_);
  swap(device_uuid_, other->device_uuid_);
}

::google::protobuf::Metadata GetLoggableEntitiesRequest::GetMetadata() const {
  ::google::protobuf::internal::AssignDescriptors(&::assign_descriptors_table_dmi_2fhw_5fmanagement_5fservice_2eproto);
  return ::file_level_metadata_dmi_2fhw_5fmanagement_5fservice_2eproto[kIndexInFileMessages];
}


// ===================================================================

void Heartbeat::InitAsDefaultInstance() {
}
class Heartbeat::HasBitSetters {
 public:
};

#if !defined(_MSC_VER) || _MSC_VER >= 1900
const int Heartbeat::kHeartbeatSignatureFieldNumber;
#endif  // !defined(_MSC_VER) || _MSC_VER >= 1900

Heartbeat::Heartbeat()
  : ::google::protobuf::Message(), _internal_metadata_(nullptr) {
  SharedCtor();
  // @@protoc_insertion_point(constructor:dmi.Heartbeat)
}
Heartbeat::Heartbeat(const Heartbeat& from)
  : ::google::protobuf::Message(),
      _internal_metadata_(nullptr) {
  _internal_metadata_.MergeFrom(from._internal_metadata_);
  heartbeat_signature_ = from.heartbeat_signature_;
  // @@protoc_insertion_point(copy_constructor:dmi.Heartbeat)
}

void Heartbeat::SharedCtor() {
  heartbeat_signature_ = 0u;
}

Heartbeat::~Heartbeat() {
  // @@protoc_insertion_point(destructor:dmi.Heartbeat)
  SharedDtor();
}

void Heartbeat::SharedDtor() {
}

void Heartbeat::SetCachedSize(int size) const {
  _cached_size_.Set(size);
}
const Heartbeat& Heartbeat::default_instance() {
  ::google::protobuf::internal::InitSCC(&::scc_info_Heartbeat_dmi_2fhw_5fmanagement_5fservice_2eproto.base);
  return *internal_default_instance();
}


void Heartbeat::Clear() {
// @@protoc_insertion_point(message_clear_start:dmi.Heartbeat)
  ::google::protobuf::uint32 cached_has_bits = 0;
  // Prevent compiler warnings about cached_has_bits being unused
  (void) cached_has_bits;

  heartbeat_signature_ = 0u;
  _internal_metadata_.Clear();
}

#if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER
const char* Heartbeat::_InternalParse(const char* begin, const char* end, void* object,
                  ::google::protobuf::internal::ParseContext* ctx) {
  auto msg = static_cast<Heartbeat*>(object);
  ::google::protobuf::int32 size; (void)size;
  int depth; (void)depth;
  ::google::protobuf::uint32 tag;
  ::google::protobuf::internal::ParseFunc parser_till_end; (void)parser_till_end;
  auto ptr = begin;
  while (ptr < end) {
    ptr = ::google::protobuf::io::Parse32(ptr, &tag);
    GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
    switch (tag >> 3) {
      // fixed32 heartbeat_signature = 1;
      case 1: {
        if (static_cast<::google::protobuf::uint8>(tag) != 13) goto handle_unusual;
        msg->set_heartbeat_signature(::google::protobuf::io::UnalignedLoad<::google::protobuf::uint32>(ptr));
        ptr += sizeof(::google::protobuf::uint32);
        break;
      }
      default: {
      handle_unusual:
        if ((tag & 7) == 4 || tag == 0) {
          ctx->EndGroup(tag);
          return ptr;
        }
        auto res = UnknownFieldParse(tag, {_InternalParse, msg},
          ptr, end, msg->_internal_metadata_.mutable_unknown_fields(), ctx);
        ptr = res.first;
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr);
        if (res.second) return ptr;
      }
    }  // switch
  }  // while
  return ptr;
}
#else  // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER
bool Heartbeat::MergePartialFromCodedStream(
    ::google::protobuf::io::CodedInputStream* input) {
#define DO_(EXPRESSION) if (!PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure
  ::google::protobuf::uint32 tag;
  // @@protoc_insertion_point(parse_start:dmi.Heartbeat)
  for (;;) {
    ::std::pair<::google::protobuf::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u);
    tag = p.first;
    if (!p.second) goto handle_unusual;
    switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) {
      // fixed32 heartbeat_signature = 1;
      case 1: {
        if (static_cast< ::google::protobuf::uint8>(tag) == (13 & 0xFF)) {

          DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive<
                   ::google::protobuf::uint32, ::google::protobuf::internal::WireFormatLite::TYPE_FIXED32>(
                 input, &heartbeat_signature_)));
        } else {
          goto handle_unusual;
        }
        break;
      }

      default: {
      handle_unusual:
        if (tag == 0) {
          goto success;
        }
        DO_(::google::protobuf::internal::WireFormat::SkipField(
              input, tag, _internal_metadata_.mutable_unknown_fields()));
        break;
      }
    }
  }
success:
  // @@protoc_insertion_point(parse_success:dmi.Heartbeat)
  return true;
failure:
  // @@protoc_insertion_point(parse_failure:dmi.Heartbeat)
  return false;
#undef DO_
}
#endif  // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER

void Heartbeat::SerializeWithCachedSizes(
    ::google::protobuf::io::CodedOutputStream* output) const {
  // @@protoc_insertion_point(serialize_start:dmi.Heartbeat)
  ::google::protobuf::uint32 cached_has_bits = 0;
  (void) cached_has_bits;

  // fixed32 heartbeat_signature = 1;
  if (this->heartbeat_signature() != 0) {
    ::google::protobuf::internal::WireFormatLite::WriteFixed32(1, this->heartbeat_signature(), output);
  }

  if (_internal_metadata_.have_unknown_fields()) {
    ::google::protobuf::internal::WireFormat::SerializeUnknownFields(
        _internal_metadata_.unknown_fields(), output);
  }
  // @@protoc_insertion_point(serialize_end:dmi.Heartbeat)
}

::google::protobuf::uint8* Heartbeat::InternalSerializeWithCachedSizesToArray(
    ::google::protobuf::uint8* target) const {
  // @@protoc_insertion_point(serialize_to_array_start:dmi.Heartbeat)
  ::google::protobuf::uint32 cached_has_bits = 0;
  (void) cached_has_bits;

  // fixed32 heartbeat_signature = 1;
  if (this->heartbeat_signature() != 0) {
    target = ::google::protobuf::internal::WireFormatLite::WriteFixed32ToArray(1, this->heartbeat_signature(), target);
  }

  if (_internal_metadata_.have_unknown_fields()) {
    target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray(
        _internal_metadata_.unknown_fields(), target);
  }
  // @@protoc_insertion_point(serialize_to_array_end:dmi.Heartbeat)
  return target;
}

size_t Heartbeat::ByteSizeLong() const {
// @@protoc_insertion_point(message_byte_size_start:dmi.Heartbeat)
  size_t total_size = 0;

  if (_internal_metadata_.have_unknown_fields()) {
    total_size +=
      ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize(
        _internal_metadata_.unknown_fields());
  }
  ::google::protobuf::uint32 cached_has_bits = 0;
  // Prevent compiler warnings about cached_has_bits being unused
  (void) cached_has_bits;

  // fixed32 heartbeat_signature = 1;
  if (this->heartbeat_signature() != 0) {
    total_size += 1 + 4;
  }

  int cached_size = ::google::protobuf::internal::ToCachedSize(total_size);
  SetCachedSize(cached_size);
  return total_size;
}

void Heartbeat::MergeFrom(const ::google::protobuf::Message& from) {
// @@protoc_insertion_point(generalized_merge_from_start:dmi.Heartbeat)
  GOOGLE_DCHECK_NE(&from, this);
  const Heartbeat* source =
      ::google::protobuf::DynamicCastToGenerated<Heartbeat>(
          &from);
  if (source == nullptr) {
  // @@protoc_insertion_point(generalized_merge_from_cast_fail:dmi.Heartbeat)
    ::google::protobuf::internal::ReflectionOps::Merge(from, this);
  } else {
  // @@protoc_insertion_point(generalized_merge_from_cast_success:dmi.Heartbeat)
    MergeFrom(*source);
  }
}

void Heartbeat::MergeFrom(const Heartbeat& from) {
// @@protoc_insertion_point(class_specific_merge_from_start:dmi.Heartbeat)
  GOOGLE_DCHECK_NE(&from, this);
  _internal_metadata_.MergeFrom(from._internal_metadata_);
  ::google::protobuf::uint32 cached_has_bits = 0;
  (void) cached_has_bits;

  if (from.heartbeat_signature() != 0) {
    set_heartbeat_signature(from.heartbeat_signature());
  }
}

void Heartbeat::CopyFrom(const ::google::protobuf::Message& from) {
// @@protoc_insertion_point(generalized_copy_from_start:dmi.Heartbeat)
  if (&from == this) return;
  Clear();
  MergeFrom(from);
}

void Heartbeat::CopyFrom(const Heartbeat& from) {
// @@protoc_insertion_point(class_specific_copy_from_start:dmi.Heartbeat)
  if (&from == this) return;
  Clear();
  MergeFrom(from);
}

bool Heartbeat::IsInitialized() const {
  return true;
}

void Heartbeat::Swap(Heartbeat* other) {
  if (other == this) return;
  InternalSwap(other);
}
void Heartbeat::InternalSwap(Heartbeat* other) {
  using std::swap;
  _internal_metadata_.Swap(&other->_internal_metadata_);
  swap(heartbeat_signature_, other->heartbeat_signature_);
}

::google::protobuf::Metadata Heartbeat::GetMetadata() const {
  ::google::protobuf::internal::AssignDescriptors(&::assign_descriptors_table_dmi_2fhw_5fmanagement_5fservice_2eproto);
  return ::file_level_metadata_dmi_2fhw_5fmanagement_5fservice_2eproto[kIndexInFileMessages];
}


// ===================================================================

void RebootDeviceRequest::InitAsDefaultInstance() {
  ::dmi::_RebootDeviceRequest_default_instance_._instance.get_mutable()->device_uuid_ = const_cast< ::dmi::Uuid*>(
      ::dmi::Uuid::internal_default_instance());
}
class RebootDeviceRequest::HasBitSetters {
 public:
  static const ::dmi::Uuid& device_uuid(const RebootDeviceRequest* msg);
};

const ::dmi::Uuid&
RebootDeviceRequest::HasBitSetters::device_uuid(const RebootDeviceRequest* msg) {
  return *msg->device_uuid_;
}
void RebootDeviceRequest::clear_device_uuid() {
  if (GetArenaNoVirtual() == nullptr && device_uuid_ != nullptr) {
    delete device_uuid_;
  }
  device_uuid_ = nullptr;
}
#if !defined(_MSC_VER) || _MSC_VER >= 1900
const int RebootDeviceRequest::kDeviceUuidFieldNumber;
#endif  // !defined(_MSC_VER) || _MSC_VER >= 1900

RebootDeviceRequest::RebootDeviceRequest()
  : ::google::protobuf::Message(), _internal_metadata_(nullptr) {
  SharedCtor();
  // @@protoc_insertion_point(constructor:dmi.RebootDeviceRequest)
}
RebootDeviceRequest::RebootDeviceRequest(const RebootDeviceRequest& from)
  : ::google::protobuf::Message(),
      _internal_metadata_(nullptr) {
  _internal_metadata_.MergeFrom(from._internal_metadata_);
  if (from.has_device_uuid()) {
    device_uuid_ = new ::dmi::Uuid(*from.device_uuid_);
  } else {
    device_uuid_ = nullptr;
  }
  // @@protoc_insertion_point(copy_constructor:dmi.RebootDeviceRequest)
}

void RebootDeviceRequest::SharedCtor() {
  ::google::protobuf::internal::InitSCC(
      &scc_info_RebootDeviceRequest_dmi_2fhw_5fmanagement_5fservice_2eproto.base);
  device_uuid_ = nullptr;
}

RebootDeviceRequest::~RebootDeviceRequest() {
  // @@protoc_insertion_point(destructor:dmi.RebootDeviceRequest)
  SharedDtor();
}

void RebootDeviceRequest::SharedDtor() {
  if (this != internal_default_instance()) delete device_uuid_;
}

void RebootDeviceRequest::SetCachedSize(int size) const {
  _cached_size_.Set(size);
}
const RebootDeviceRequest& RebootDeviceRequest::default_instance() {
  ::google::protobuf::internal::InitSCC(&::scc_info_RebootDeviceRequest_dmi_2fhw_5fmanagement_5fservice_2eproto.base);
  return *internal_default_instance();
}


void RebootDeviceRequest::Clear() {
// @@protoc_insertion_point(message_clear_start:dmi.RebootDeviceRequest)
  ::google::protobuf::uint32 cached_has_bits = 0;
  // Prevent compiler warnings about cached_has_bits being unused
  (void) cached_has_bits;

  if (GetArenaNoVirtual() == nullptr && device_uuid_ != nullptr) {
    delete device_uuid_;
  }
  device_uuid_ = nullptr;
  _internal_metadata_.Clear();
}

#if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER
const char* RebootDeviceRequest::_InternalParse(const char* begin, const char* end, void* object,
                  ::google::protobuf::internal::ParseContext* ctx) {
  auto msg = static_cast<RebootDeviceRequest*>(object);
  ::google::protobuf::int32 size; (void)size;
  int depth; (void)depth;
  ::google::protobuf::uint32 tag;
  ::google::protobuf::internal::ParseFunc parser_till_end; (void)parser_till_end;
  auto ptr = begin;
  while (ptr < end) {
    ptr = ::google::protobuf::io::Parse32(ptr, &tag);
    GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
    switch (tag >> 3) {
      // .dmi.Uuid device_uuid = 1;
      case 1: {
        if (static_cast<::google::protobuf::uint8>(tag) != 10) goto handle_unusual;
        ptr = ::google::protobuf::io::ReadSize(ptr, &size);
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
        parser_till_end = ::dmi::Uuid::_InternalParse;
        object = msg->mutable_device_uuid();
        if (size > end - ptr) goto len_delim_till_end;
        ptr += size;
        GOOGLE_PROTOBUF_PARSER_ASSERT(ctx->ParseExactRange(
            {parser_till_end, object}, ptr - size, ptr));
        break;
      }
      default: {
      handle_unusual:
        if ((tag & 7) == 4 || tag == 0) {
          ctx->EndGroup(tag);
          return ptr;
        }
        auto res = UnknownFieldParse(tag, {_InternalParse, msg},
          ptr, end, msg->_internal_metadata_.mutable_unknown_fields(), ctx);
        ptr = res.first;
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr);
        if (res.second) return ptr;
      }
    }  // switch
  }  // while
  return ptr;
len_delim_till_end:
  return ctx->StoreAndTailCall(ptr, end, {_InternalParse, msg},
                               {parser_till_end, object}, size);
}
#else  // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER
bool RebootDeviceRequest::MergePartialFromCodedStream(
    ::google::protobuf::io::CodedInputStream* input) {
#define DO_(EXPRESSION) if (!PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure
  ::google::protobuf::uint32 tag;
  // @@protoc_insertion_point(parse_start:dmi.RebootDeviceRequest)
  for (;;) {
    ::std::pair<::google::protobuf::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u);
    tag = p.first;
    if (!p.second) goto handle_unusual;
    switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) {
      // .dmi.Uuid device_uuid = 1;
      case 1: {
        if (static_cast< ::google::protobuf::uint8>(tag) == (10 & 0xFF)) {
          DO_(::google::protobuf::internal::WireFormatLite::ReadMessage(
               input, mutable_device_uuid()));
        } else {
          goto handle_unusual;
        }
        break;
      }

      default: {
      handle_unusual:
        if (tag == 0) {
          goto success;
        }
        DO_(::google::protobuf::internal::WireFormat::SkipField(
              input, tag, _internal_metadata_.mutable_unknown_fields()));
        break;
      }
    }
  }
success:
  // @@protoc_insertion_point(parse_success:dmi.RebootDeviceRequest)
  return true;
failure:
  // @@protoc_insertion_point(parse_failure:dmi.RebootDeviceRequest)
  return false;
#undef DO_
}
#endif  // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER

void RebootDeviceRequest::SerializeWithCachedSizes(
    ::google::protobuf::io::CodedOutputStream* output) const {
  // @@protoc_insertion_point(serialize_start:dmi.RebootDeviceRequest)
  ::google::protobuf::uint32 cached_has_bits = 0;
  (void) cached_has_bits;

  // .dmi.Uuid device_uuid = 1;
  if (this->has_device_uuid()) {
    ::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray(
      1, HasBitSetters::device_uuid(this), output);
  }

  if (_internal_metadata_.have_unknown_fields()) {
    ::google::protobuf::internal::WireFormat::SerializeUnknownFields(
        _internal_metadata_.unknown_fields(), output);
  }
  // @@protoc_insertion_point(serialize_end:dmi.RebootDeviceRequest)
}

::google::protobuf::uint8* RebootDeviceRequest::InternalSerializeWithCachedSizesToArray(
    ::google::protobuf::uint8* target) const {
  // @@protoc_insertion_point(serialize_to_array_start:dmi.RebootDeviceRequest)
  ::google::protobuf::uint32 cached_has_bits = 0;
  (void) cached_has_bits;

  // .dmi.Uuid device_uuid = 1;
  if (this->has_device_uuid()) {
    target = ::google::protobuf::internal::WireFormatLite::
      InternalWriteMessageToArray(
        1, HasBitSetters::device_uuid(this), target);
  }

  if (_internal_metadata_.have_unknown_fields()) {
    target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray(
        _internal_metadata_.unknown_fields(), target);
  }
  // @@protoc_insertion_point(serialize_to_array_end:dmi.RebootDeviceRequest)
  return target;
}

size_t RebootDeviceRequest::ByteSizeLong() const {
// @@protoc_insertion_point(message_byte_size_start:dmi.RebootDeviceRequest)
  size_t total_size = 0;

  if (_internal_metadata_.have_unknown_fields()) {
    total_size +=
      ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize(
        _internal_metadata_.unknown_fields());
  }
  ::google::protobuf::uint32 cached_has_bits = 0;
  // Prevent compiler warnings about cached_has_bits being unused
  (void) cached_has_bits;

  // .dmi.Uuid device_uuid = 1;
  if (this->has_device_uuid()) {
    total_size += 1 +
      ::google::protobuf::internal::WireFormatLite::MessageSize(
        *device_uuid_);
  }

  int cached_size = ::google::protobuf::internal::ToCachedSize(total_size);
  SetCachedSize(cached_size);
  return total_size;
}

void RebootDeviceRequest::MergeFrom(const ::google::protobuf::Message& from) {
// @@protoc_insertion_point(generalized_merge_from_start:dmi.RebootDeviceRequest)
  GOOGLE_DCHECK_NE(&from, this);
  const RebootDeviceRequest* source =
      ::google::protobuf::DynamicCastToGenerated<RebootDeviceRequest>(
          &from);
  if (source == nullptr) {
  // @@protoc_insertion_point(generalized_merge_from_cast_fail:dmi.RebootDeviceRequest)
    ::google::protobuf::internal::ReflectionOps::Merge(from, this);
  } else {
  // @@protoc_insertion_point(generalized_merge_from_cast_success:dmi.RebootDeviceRequest)
    MergeFrom(*source);
  }
}

void RebootDeviceRequest::MergeFrom(const RebootDeviceRequest& from) {
// @@protoc_insertion_point(class_specific_merge_from_start:dmi.RebootDeviceRequest)
  GOOGLE_DCHECK_NE(&from, this);
  _internal_metadata_.MergeFrom(from._internal_metadata_);
  ::google::protobuf::uint32 cached_has_bits = 0;
  (void) cached_has_bits;

  if (from.has_device_uuid()) {
    mutable_device_uuid()->::dmi::Uuid::MergeFrom(from.device_uuid());
  }
}

void RebootDeviceRequest::CopyFrom(const ::google::protobuf::Message& from) {
// @@protoc_insertion_point(generalized_copy_from_start:dmi.RebootDeviceRequest)
  if (&from == this) return;
  Clear();
  MergeFrom(from);
}

void RebootDeviceRequest::CopyFrom(const RebootDeviceRequest& from) {
// @@protoc_insertion_point(class_specific_copy_from_start:dmi.RebootDeviceRequest)
  if (&from == this) return;
  Clear();
  MergeFrom(from);
}

bool RebootDeviceRequest::IsInitialized() const {
  return true;
}

void RebootDeviceRequest::Swap(RebootDeviceRequest* other) {
  if (other == this) return;
  InternalSwap(other);
}
void RebootDeviceRequest::InternalSwap(RebootDeviceRequest* other) {
  using std::swap;
  _internal_metadata_.Swap(&other->_internal_metadata_);
  swap(device_uuid_, other->device_uuid_);
}

::google::protobuf::Metadata RebootDeviceRequest::GetMetadata() const {
  ::google::protobuf::internal::AssignDescriptors(&::assign_descriptors_table_dmi_2fhw_5fmanagement_5fservice_2eproto);
  return ::file_level_metadata_dmi_2fhw_5fmanagement_5fservice_2eproto[kIndexInFileMessages];
}


// ===================================================================

void RebootDeviceResponse::InitAsDefaultInstance() {
}
class RebootDeviceResponse::HasBitSetters {
 public:
};

#if !defined(_MSC_VER) || _MSC_VER >= 1900
const int RebootDeviceResponse::kStatusFieldNumber;
const int RebootDeviceResponse::kReasonFieldNumber;
const int RebootDeviceResponse::kReasonDetailFieldNumber;
#endif  // !defined(_MSC_VER) || _MSC_VER >= 1900

RebootDeviceResponse::RebootDeviceResponse()
  : ::google::protobuf::Message(), _internal_metadata_(nullptr) {
  SharedCtor();
  // @@protoc_insertion_point(constructor:dmi.RebootDeviceResponse)
}
RebootDeviceResponse::RebootDeviceResponse(const RebootDeviceResponse& from)
  : ::google::protobuf::Message(),
      _internal_metadata_(nullptr) {
  _internal_metadata_.MergeFrom(from._internal_metadata_);
  reason_detail_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
  if (from.reason_detail().size() > 0) {
    reason_detail_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.reason_detail_);
  }
  ::memcpy(&status_, &from.status_,
    static_cast<size_t>(reinterpret_cast<char*>(&reason_) -
    reinterpret_cast<char*>(&status_)) + sizeof(reason_));
  // @@protoc_insertion_point(copy_constructor:dmi.RebootDeviceResponse)
}

void RebootDeviceResponse::SharedCtor() {
  ::google::protobuf::internal::InitSCC(
      &scc_info_RebootDeviceResponse_dmi_2fhw_5fmanagement_5fservice_2eproto.base);
  reason_detail_.UnsafeSetDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
  ::memset(&status_, 0, static_cast<size_t>(
      reinterpret_cast<char*>(&reason_) -
      reinterpret_cast<char*>(&status_)) + sizeof(reason_));
}

RebootDeviceResponse::~RebootDeviceResponse() {
  // @@protoc_insertion_point(destructor:dmi.RebootDeviceResponse)
  SharedDtor();
}

void RebootDeviceResponse::SharedDtor() {
  reason_detail_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
}

void RebootDeviceResponse::SetCachedSize(int size) const {
  _cached_size_.Set(size);
}
const RebootDeviceResponse& RebootDeviceResponse::default_instance() {
  ::google::protobuf::internal::InitSCC(&::scc_info_RebootDeviceResponse_dmi_2fhw_5fmanagement_5fservice_2eproto.base);
  return *internal_default_instance();
}


void RebootDeviceResponse::Clear() {
// @@protoc_insertion_point(message_clear_start:dmi.RebootDeviceResponse)
  ::google::protobuf::uint32 cached_has_bits = 0;
  // Prevent compiler warnings about cached_has_bits being unused
  (void) cached_has_bits;

  reason_detail_.ClearToEmptyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
  ::memset(&status_, 0, static_cast<size_t>(
      reinterpret_cast<char*>(&reason_) -
      reinterpret_cast<char*>(&status_)) + sizeof(reason_));
  _internal_metadata_.Clear();
}

#if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER
const char* RebootDeviceResponse::_InternalParse(const char* begin, const char* end, void* object,
                  ::google::protobuf::internal::ParseContext* ctx) {
  auto msg = static_cast<RebootDeviceResponse*>(object);
  ::google::protobuf::int32 size; (void)size;
  int depth; (void)depth;
  ::google::protobuf::uint32 tag;
  ::google::protobuf::internal::ParseFunc parser_till_end; (void)parser_till_end;
  auto ptr = begin;
  while (ptr < end) {
    ptr = ::google::protobuf::io::Parse32(ptr, &tag);
    GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
    switch (tag >> 3) {
      // .dmi.Status status = 3;
      case 3: {
        if (static_cast<::google::protobuf::uint8>(tag) != 24) goto handle_unusual;
        ::google::protobuf::uint64 val = ::google::protobuf::internal::ReadVarint(&ptr);
        msg->set_status(static_cast<::dmi::Status>(val));
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
        break;
      }
      // .dmi.RebootDeviceResponse.Reason reason = 4;
      case 4: {
        if (static_cast<::google::protobuf::uint8>(tag) != 32) goto handle_unusual;
        ::google::protobuf::uint64 val = ::google::protobuf::internal::ReadVarint(&ptr);
        msg->set_reason(static_cast<::dmi::RebootDeviceResponse_Reason>(val));
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
        break;
      }
      // string reason_detail = 5;
      case 5: {
        if (static_cast<::google::protobuf::uint8>(tag) != 42) goto handle_unusual;
        ptr = ::google::protobuf::io::ReadSize(ptr, &size);
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr);
        ctx->extra_parse_data().SetFieldName("dmi.RebootDeviceResponse.reason_detail");
        object = msg->mutable_reason_detail();
        if (size > end - ptr + ::google::protobuf::internal::ParseContext::kSlopBytes) {
          parser_till_end = ::google::protobuf::internal::GreedyStringParserUTF8;
          goto string_till_end;
        }
        GOOGLE_PROTOBUF_PARSER_ASSERT(::google::protobuf::internal::StringCheckUTF8(ptr, size, ctx));
        ::google::protobuf::internal::InlineGreedyStringParser(object, ptr, size, ctx);
        ptr += size;
        break;
      }
      default: {
      handle_unusual:
        if ((tag & 7) == 4 || tag == 0) {
          ctx->EndGroup(tag);
          return ptr;
        }
        auto res = UnknownFieldParse(tag, {_InternalParse, msg},
          ptr, end, msg->_internal_metadata_.mutable_unknown_fields(), ctx);
        ptr = res.first;
        GOOGLE_PROTOBUF_PARSER_ASSERT(ptr != nullptr);
        if (res.second) return ptr;
      }
    }  // switch
  }  // while
  return ptr;
string_till_end:
  static_cast<::std::string*>(object)->clear();
  static_cast<::std::string*>(object)->reserve(size);
  goto len_delim_till_end;
len_delim_till_end:
  return ctx->StoreAndTailCall(ptr, end, {_InternalParse, msg},
                               {parser_till_end, object}, size);
}
#else  // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER
bool RebootDeviceResponse::MergePartialFromCodedStream(
    ::google::protobuf::io::CodedInputStream* input) {
#define DO_(EXPRESSION) if (!PROTOBUF_PREDICT_TRUE(EXPRESSION)) goto failure
  ::google::protobuf::uint32 tag;
  // @@protoc_insertion_point(parse_start:dmi.RebootDeviceResponse)
  for (;;) {
    ::std::pair<::google::protobuf::uint32, bool> p = input->ReadTagWithCutoffNoLastTag(127u);
    tag = p.first;
    if (!p.second) goto handle_unusual;
    switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) {
      // .dmi.Status status = 3;
      case 3: {
        if (static_cast< ::google::protobuf::uint8>(tag) == (24 & 0xFF)) {
          int value = 0;
          DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive<
                   int, ::google::protobuf::internal::WireFormatLite::TYPE_ENUM>(
                 input, &value)));
          set_status(static_cast< ::dmi::Status >(value));
        } else {
          goto handle_unusual;
        }
        break;
      }

      // .dmi.RebootDeviceResponse.Reason reason = 4;
      case 4: {
        if (static_cast< ::google::protobuf::uint8>(tag) == (32 & 0xFF)) {
          int value = 0;
          DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive<
                   int, ::google::protobuf::internal::WireFormatLite::TYPE_ENUM>(
                 input, &value)));
          set_reason(static_cast< ::dmi::RebootDeviceResponse_Reason >(value));
        } else {
          goto handle_unusual;
        }
        break;
      }

      // string reason_detail = 5;
      case 5: {
        if (static_cast< ::google::protobuf::uint8>(tag) == (42 & 0xFF)) {
          DO_(::google::protobuf::internal::WireFormatLite::ReadString(
                input, this->mutable_reason_detail()));
          DO_(::google::protobuf::internal::WireFormatLite::VerifyUtf8String(
            this->reason_detail().data(), static_cast<int>(this->reason_detail().length()),
            ::google::protobuf::internal::WireFormatLite::PARSE,
            "dmi.RebootDeviceResponse.reason_detail"));
        } else {
          goto handle_unusual;
        }
        break;
      }

      default: {
      handle_unusual:
        if (tag == 0) {
          goto success;
        }
        DO_(::google::protobuf::internal::WireFormat::SkipField(
              input, tag, _internal_metadata_.mutable_unknown_fields()));
        break;
      }
    }
  }
success:
  // @@protoc_insertion_point(parse_success:dmi.RebootDeviceResponse)
  return true;
failure:
  // @@protoc_insertion_point(parse_failure:dmi.RebootDeviceResponse)
  return false;
#undef DO_
}
#endif  // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER

void RebootDeviceResponse::SerializeWithCachedSizes(
    ::google::protobuf::io::CodedOutputStream* output) const {
  // @@protoc_insertion_point(serialize_start:dmi.RebootDeviceResponse)
  ::google::protobuf::uint32 cached_has_bits = 0;
  (void) cached_has_bits;

  // .dmi.Status status = 3;
  if (this->status() != 0) {
    ::google::protobuf::internal::WireFormatLite::WriteEnum(
      3, this->status(), output);
  }

  // .dmi.RebootDeviceResponse.Reason reason = 4;
  if (this->reason() != 0) {
    ::google::protobuf::internal::WireFormatLite::WriteEnum(
      4, this->reason(), output);
  }

  // string reason_detail = 5;
  if (this->reason_detail().size() > 0) {
    ::google::protobuf::internal::WireFormatLite::VerifyUtf8String(
      this->reason_detail().data(), static_cast<int>(this->reason_detail().length()),
      ::google::protobuf::internal::WireFormatLite::SERIALIZE,
      "dmi.RebootDeviceResponse.reason_detail");
    ::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased(
      5, this->reason_detail(), output);
  }

  if (_internal_metadata_.have_unknown_fields()) {
    ::google::protobuf::internal::WireFormat::SerializeUnknownFields(
        _internal_metadata_.unknown_fields(), output);
  }
  // @@protoc_insertion_point(serialize_end:dmi.RebootDeviceResponse)
}

::google::protobuf::uint8* RebootDeviceResponse::InternalSerializeWithCachedSizesToArray(
    ::google::protobuf::uint8* target) const {
  // @@protoc_insertion_point(serialize_to_array_start:dmi.RebootDeviceResponse)
  ::google::protobuf::uint32 cached_has_bits = 0;
  (void) cached_has_bits;

  // .dmi.Status status = 3;
  if (this->status() != 0) {
    target = ::google::protobuf::internal::WireFormatLite::WriteEnumToArray(
      3, this->status(), target);
  }

  // .dmi.RebootDeviceResponse.Reason reason = 4;
  if (this->reason() != 0) {
    target = ::google::protobuf::internal::WireFormatLite::WriteEnumToArray(
      4, this->reason(), target);
  }

  // string reason_detail = 5;
  if (this->reason_detail().size() > 0) {
    ::google::protobuf::internal::WireFormatLite::VerifyUtf8String(
      this->reason_detail().data(), static_cast<int>(this->reason_detail().length()),
      ::google::protobuf::internal::WireFormatLite::SERIALIZE,
      "dmi.RebootDeviceResponse.reason_detail");
    target =
      ::google::protobuf::internal::WireFormatLite::WriteStringToArray(
        5, this->reason_detail(), target);
  }

  if (_internal_metadata_.have_unknown_fields()) {
    target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray(
        _internal_metadata_.unknown_fields(), target);
  }
  // @@protoc_insertion_point(serialize_to_array_end:dmi.RebootDeviceResponse)
  return target;
}

size_t RebootDeviceResponse::ByteSizeLong() const {
// @@protoc_insertion_point(message_byte_size_start:dmi.RebootDeviceResponse)
  size_t total_size = 0;

  if (_internal_metadata_.have_unknown_fields()) {
    total_size +=
      ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize(
        _internal_metadata_.unknown_fields());
  }
  ::google::protobuf::uint32 cached_has_bits = 0;
  // Prevent compiler warnings about cached_has_bits being unused
  (void) cached_has_bits;

  // string reason_detail = 5;
  if (this->reason_detail().size() > 0) {
    total_size += 1 +
      ::google::protobuf::internal::WireFormatLite::StringSize(
        this->reason_detail());
  }

  // .dmi.Status status = 3;
  if (this->status() != 0) {
    total_size += 1 +
      ::google::protobuf::internal::WireFormatLite::EnumSize(this->status());
  }

  // .dmi.RebootDeviceResponse.Reason reason = 4;
  if (this->reason() != 0) {
    total_size += 1 +
      ::google::protobuf::internal::WireFormatLite::EnumSize(this->reason());
  }

  int cached_size = ::google::protobuf::internal::ToCachedSize(total_size);
  SetCachedSize(cached_size);
  return total_size;
}

void RebootDeviceResponse::MergeFrom(const ::google::protobuf::Message& from) {
// @@protoc_insertion_point(generalized_merge_from_start:dmi.RebootDeviceResponse)
  GOOGLE_DCHECK_NE(&from, this);
  const RebootDeviceResponse* source =
      ::google::protobuf::DynamicCastToGenerated<RebootDeviceResponse>(
          &from);
  if (source == nullptr) {
  // @@protoc_insertion_point(generalized_merge_from_cast_fail:dmi.RebootDeviceResponse)
    ::google::protobuf::internal::ReflectionOps::Merge(from, this);
  } else {
  // @@protoc_insertion_point(generalized_merge_from_cast_success:dmi.RebootDeviceResponse)
    MergeFrom(*source);
  }
}

void RebootDeviceResponse::MergeFrom(const RebootDeviceResponse& from) {
// @@protoc_insertion_point(class_specific_merge_from_start:dmi.RebootDeviceResponse)
  GOOGLE_DCHECK_NE(&from, this);
  _internal_metadata_.MergeFrom(from._internal_metadata_);
  ::google::protobuf::uint32 cached_has_bits = 0;
  (void) cached_has_bits;

  if (from.reason_detail().size() > 0) {

    reason_detail_.AssignWithDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited(), from.reason_detail_);
  }
  if (from.status() != 0) {
    set_status(from.status());
  }
  if (from.reason() != 0) {
    set_reason(from.reason());
  }
}

void RebootDeviceResponse::CopyFrom(const ::google::protobuf::Message& from) {
// @@protoc_insertion_point(generalized_copy_from_start:dmi.RebootDeviceResponse)
  if (&from == this) return;
  Clear();
  MergeFrom(from);
}

void RebootDeviceResponse::CopyFrom(const RebootDeviceResponse& from) {
// @@protoc_insertion_point(class_specific_copy_from_start:dmi.RebootDeviceResponse)
  if (&from == this) return;
  Clear();
  MergeFrom(from);
}

bool RebootDeviceResponse::IsInitialized() const {
  return true;
}

void RebootDeviceResponse::Swap(RebootDeviceResponse* other) {
  if (other == this) return;
  InternalSwap(other);
}
void RebootDeviceResponse::InternalSwap(RebootDeviceResponse* other) {
  using std::swap;
  _internal_metadata_.Swap(&other->_internal_metadata_);
  reason_detail_.Swap(&other->reason_detail_, &::google::protobuf::internal::GetEmptyStringAlreadyInited(),
    GetArenaNoVirtual());
  swap(status_, other->status_);
  swap(reason_, other->reason_);
}

::google::protobuf::Metadata RebootDeviceResponse::GetMetadata() const {
  ::google::protobuf::internal::AssignDescriptors(&::assign_descriptors_table_dmi_2fhw_5fmanagement_5fservice_2eproto);
  return ::file_level_metadata_dmi_2fhw_5fmanagement_5fservice_2eproto[kIndexInFileMessages];
}


// @@protoc_insertion_point(namespace_scope)
}  // namespace dmi
namespace google {
namespace protobuf {
template<> PROTOBUF_NOINLINE ::dmi::PhysicalInventoryRequest* Arena::CreateMaybeMessage< ::dmi::PhysicalInventoryRequest >(Arena* arena) {
  return Arena::CreateInternal< ::dmi::PhysicalInventoryRequest >(arena);
}
template<> PROTOBUF_NOINLINE ::dmi::PhysicalInventoryResponse* Arena::CreateMaybeMessage< ::dmi::PhysicalInventoryResponse >(Arena* arena) {
  return Arena::CreateInternal< ::dmi::PhysicalInventoryResponse >(arena);
}
template<> PROTOBUF_NOINLINE ::dmi::HWComponentInfoGetRequest* Arena::CreateMaybeMessage< ::dmi::HWComponentInfoGetRequest >(Arena* arena) {
  return Arena::CreateInternal< ::dmi::HWComponentInfoGetRequest >(arena);
}
template<> PROTOBUF_NOINLINE ::dmi::HWComponentInfoGetResponse* Arena::CreateMaybeMessage< ::dmi::HWComponentInfoGetResponse >(Arena* arena) {
  return Arena::CreateInternal< ::dmi::HWComponentInfoGetResponse >(arena);
}
template<> PROTOBUF_NOINLINE ::dmi::HWComponentInfoSetRequest* Arena::CreateMaybeMessage< ::dmi::HWComponentInfoSetRequest >(Arena* arena) {
  return Arena::CreateInternal< ::dmi::HWComponentInfoSetRequest >(arena);
}
template<> PROTOBUF_NOINLINE ::dmi::HWComponentInfoSetResponse* Arena::CreateMaybeMessage< ::dmi::HWComponentInfoSetResponse >(Arena* arena) {
  return Arena::CreateInternal< ::dmi::HWComponentInfoSetResponse >(arena);
}
template<> PROTOBUF_NOINLINE ::dmi::StartManagingDeviceResponse* Arena::CreateMaybeMessage< ::dmi::StartManagingDeviceResponse >(Arena* arena) {
  return Arena::CreateInternal< ::dmi::StartManagingDeviceResponse >(arena);
}
template<> PROTOBUF_NOINLINE ::dmi::StopManagingDeviceRequest* Arena::CreateMaybeMessage< ::dmi::StopManagingDeviceRequest >(Arena* arena) {
  return Arena::CreateInternal< ::dmi::StopManagingDeviceRequest >(arena);
}
template<> PROTOBUF_NOINLINE ::dmi::StopManagingDeviceResponse* Arena::CreateMaybeMessage< ::dmi::StopManagingDeviceResponse >(Arena* arena) {
  return Arena::CreateInternal< ::dmi::StopManagingDeviceResponse >(arena);
}
template<> PROTOBUF_NOINLINE ::dmi::ManagedDeviceInfo* Arena::CreateMaybeMessage< ::dmi::ManagedDeviceInfo >(Arena* arena) {
  return Arena::CreateInternal< ::dmi::ManagedDeviceInfo >(arena);
}
template<> PROTOBUF_NOINLINE ::dmi::ManagedDevicesResponse* Arena::CreateMaybeMessage< ::dmi::ManagedDevicesResponse >(Arena* arena) {
  return Arena::CreateInternal< ::dmi::ManagedDevicesResponse >(arena);
}
template<> PROTOBUF_NOINLINE ::dmi::SetLoggingEndpointRequest* Arena::CreateMaybeMessage< ::dmi::SetLoggingEndpointRequest >(Arena* arena) {
  return Arena::CreateInternal< ::dmi::SetLoggingEndpointRequest >(arena);
}
template<> PROTOBUF_NOINLINE ::dmi::SetRemoteEndpointResponse* Arena::CreateMaybeMessage< ::dmi::SetRemoteEndpointResponse >(Arena* arena) {
  return Arena::CreateInternal< ::dmi::SetRemoteEndpointResponse >(arena);
}
template<> PROTOBUF_NOINLINE ::dmi::GetLoggingEndpointResponse* Arena::CreateMaybeMessage< ::dmi::GetLoggingEndpointResponse >(Arena* arena) {
  return Arena::CreateInternal< ::dmi::GetLoggingEndpointResponse >(arena);
}
template<> PROTOBUF_NOINLINE ::dmi::SetMsgBusEndpointRequest* Arena::CreateMaybeMessage< ::dmi::SetMsgBusEndpointRequest >(Arena* arena) {
  return Arena::CreateInternal< ::dmi::SetMsgBusEndpointRequest >(arena);
}
template<> PROTOBUF_NOINLINE ::dmi::GetMsgBusEndpointResponse* Arena::CreateMaybeMessage< ::dmi::GetMsgBusEndpointResponse >(Arena* arena) {
  return Arena::CreateInternal< ::dmi::GetMsgBusEndpointResponse >(arena);
}
template<> PROTOBUF_NOINLINE ::dmi::EntitiesLogLevel* Arena::CreateMaybeMessage< ::dmi::EntitiesLogLevel >(Arena* arena) {
  return Arena::CreateInternal< ::dmi::EntitiesLogLevel >(arena);
}
template<> PROTOBUF_NOINLINE ::dmi::SetLogLevelRequest* Arena::CreateMaybeMessage< ::dmi::SetLogLevelRequest >(Arena* arena) {
  return Arena::CreateInternal< ::dmi::SetLogLevelRequest >(arena);
}
template<> PROTOBUF_NOINLINE ::dmi::SetLogLevelResponse* Arena::CreateMaybeMessage< ::dmi::SetLogLevelResponse >(Arena* arena) {
  return Arena::CreateInternal< ::dmi::SetLogLevelResponse >(arena);
}
template<> PROTOBUF_NOINLINE ::dmi::GetLogLevelRequest* Arena::CreateMaybeMessage< ::dmi::GetLogLevelRequest >(Arena* arena) {
  return Arena::CreateInternal< ::dmi::GetLogLevelRequest >(arena);
}
template<> PROTOBUF_NOINLINE ::dmi::GetLogLevelResponse* Arena::CreateMaybeMessage< ::dmi::GetLogLevelResponse >(Arena* arena) {
  return Arena::CreateInternal< ::dmi::GetLogLevelResponse >(arena);
}
template<> PROTOBUF_NOINLINE ::dmi::GetLoggableEntitiesRequest* Arena::CreateMaybeMessage< ::dmi::GetLoggableEntitiesRequest >(Arena* arena) {
  return Arena::CreateInternal< ::dmi::GetLoggableEntitiesRequest >(arena);
}
template<> PROTOBUF_NOINLINE ::dmi::Heartbeat* Arena::CreateMaybeMessage< ::dmi::Heartbeat >(Arena* arena) {
  return Arena::CreateInternal< ::dmi::Heartbeat >(arena);
}
template<> PROTOBUF_NOINLINE ::dmi::RebootDeviceRequest* Arena::CreateMaybeMessage< ::dmi::RebootDeviceRequest >(Arena* arena) {
  return Arena::CreateInternal< ::dmi::RebootDeviceRequest >(arena);
}
template<> PROTOBUF_NOINLINE ::dmi::RebootDeviceResponse* Arena::CreateMaybeMessage< ::dmi::RebootDeviceResponse >(Arena* arena) {
  return Arena::CreateInternal< ::dmi::RebootDeviceResponse >(arena);
}
}  // namespace protobuf
}  // namespace google

// @@protoc_insertion_point(global_scope)
#include <google/protobuf/port_undef.inc>
