23 std::string msg = std::string(
to_string(err));
24 if (!details.empty()) {
25 msg +=
": " + details;
36 : command_(cmd), message_id_(message_id) {
39 static_cast<uint16_t
>(cmd));
76 return std::ref(*dataset_);
183 std::vector<core::dicom_tag> tags;
185 if (elem ==
nullptr) {
189 auto bytes = elem->raw_data();
195 for (
size_t i = 0; i + 3 < bytes.size(); i += 4) {
196 uint16_t group =
static_cast<uint16_t
>(bytes[i]) |
197 (
static_cast<uint16_t
>(bytes[i + 1]) << 8);
198 uint16_t element =
static_cast<uint16_t
>(bytes[i + 2]) |
199 (
static_cast<uint16_t
>(bytes[i + 3]) << 8);
200 tags.emplace_back(group, element);
206 const std::vector<core::dicom_tag>& tags) {
207 std::vector<uint8_t> bytes;
208 bytes.reserve(tags.size() * 4);
210 for (
const auto& tag : tags) {
212 bytes.push_back(
static_cast<uint8_t
>(tag.group() & 0xFF));
213 bytes.push_back(
static_cast<uint8_t
>((tag.group() >> 8) & 0xFF));
214 bytes.push_back(
static_cast<uint8_t
>(tag.element() & 0xFF));
215 bytes.push_back(
static_cast<uint8_t
>((tag.element() >> 8) & 0xFF));
277 std::vector<uint8_t> dataset_bytes;
279 auto ds_result = copy.
dataset();
280 if (ds_result.is_err()) {
281 return ds_result.error();
290 return encoded_message{std::move(command_bytes), std::move(dataset_bytes)};
294 std::span<const uint8_t> dataset_data,
299 if (cmd_result.is_err()) {
303 auto command_set = std::move(cmd_result.value());
318 uint16_t message_id = msg_id.value_or(0);
324 if (!dataset_data.empty()) {
330 if (ds_result.is_err()) {
333 msg.
dataset_ = std::move(ds_result.value());
376 auto length =
static_cast<uint32_t
>(encoded.size());
403 std::string_view sop_instance_uid, uint16_t priority)
413 std::string_view sop_class_uid,
414 std::string_view sop_instance_uid,
status_code status)
433 std::string_view sop_class_uid,
status_code status)
450 if (!sop_instance_uid.empty()) {
457 std::string_view sop_class_uid,
458 std::string_view sop_instance_uid,
status_code status)
477 std::string_view sop_class_uid,
478 std::string_view sop_instance_uid,
status_code status)
489 std::string_view sop_instance_uid,
490 const std::vector<core::dicom_tag>& attribute_tags)
495 if (!attribute_tags.empty()) {
502 std::string_view sop_class_uid,
503 std::string_view sop_instance_uid,
status_code status)
514 std::string_view sop_instance_uid,
524 std::string_view sop_class_uid,
525 std::string_view sop_instance_uid,
538 std::string_view sop_instance_uid, uint16_t action_type_id)
548 std::string_view sop_class_uid,
549 std::string_view sop_instance_uid, uint16_t action_type_id,
569 std::string_view sop_class_uid,
570 std::string_view sop_instance_uid,
status_code status)
void set_numeric(dicom_tag tag, encoding::vr_type vr, T value)
Set a numeric value for the given tag.
auto get(dicom_tag tag) noexcept -> dicom_element *
Get a pointer to the element with the given tag.
auto remove(dicom_tag tag) -> bool
Remove an element from the dataset.
void insert(dicom_element element)
Insert or replace an element in the dataset.
auto get_numeric(dicom_tag tag) const -> std::optional< T >
Get the numeric value of an element.
void set_string(dicom_tag tag, encoding::vr_type vr, std::string_view value)
Set a string value for the given tag.
auto contains(dicom_tag tag) const noexcept -> bool
Check if the dataset contains an element with the given tag.
auto get_string(dicom_tag tag, std::string_view default_value="") const -> std::string
Get the string value of an element.
static result< core::dicom_dataset > decode(std::span< const uint8_t > data)
Decode bytes to a dataset using Explicit VR Little Endian.
static std::vector< uint8_t > encode(const core::dicom_dataset &dataset)
Encode a dataset to bytes using Explicit VR Little Endian.
static result< core::dicom_dataset > decode(std::span< const uint8_t > data)
Decode bytes to a dataset using Implicit VR Little Endian.
static std::vector< uint8_t > encode(const core::dicom_dataset &dataset)
Encode a dataset to bytes using Implicit VR Little Endian.
Represents a DICOM Transfer Syntax.
auto message_id() const noexcept -> uint16_t
Get the message ID.
auto status() const -> status_code
Get the status code (for response messages)
core::dicom_dataset command_set_
std::optional< core::dicom_dataset > dataset_
static auto encode(const dimse_message &msg, const encoding::transfer_syntax &dataset_ts) -> dimse_result< encoded_message >
Encode the DIMSE message to bytes.
void set_requested_sop_instance_uid(std::string_view uid)
Set the Requested SOP Instance UID.
void set_action_type_id(uint16_t type_id)
Set the Action Type ID.
auto affected_sop_class_uid() const -> std::string
Get the Affected SOP Class UID.
auto attribute_identifier_list() const -> std::vector< core::dicom_tag >
Get the Attribute Identifier List (for N-GET)
void set_priority(uint16_t priority)
Set the priority.
static auto decode(std::span< const uint8_t > command_data, std::span< const uint8_t > dataset_data, const encoding::transfer_syntax &dataset_ts) -> dimse_result< dimse_message >
Decode a DIMSE message from bytes.
void set_attribute_identifier_list(const std::vector< core::dicom_tag > &tags)
Set the Attribute Identifier List.
void set_event_type_id(uint16_t type_id)
Set the Event Type ID.
auto command_set() noexcept -> core::dicom_dataset &
Get mutable reference to the command set.
auto is_valid() const noexcept -> bool
Check if the message is valid.
auto message_id_responded_to() const -> uint16_t
Get the Message ID Being Responded To (for responses)
auto remaining_subops() const -> std::optional< uint16_t >
Get the number of remaining sub-operations.
void clear_dataset() noexcept
Remove the data set from this message.
auto warning_subops() const -> std::optional< uint16_t >
Get the number of warning sub-operations.
auto requested_sop_class_uid() const -> std::string
Get the Requested SOP Class UID (for N-SET, N-GET, N-ACTION, N-DELETE)
void set_completed_subops(uint16_t count)
Set the number of completed sub-operations.
auto is_request() const noexcept -> bool
Check if this is a request message.
void update_command_group_length()
Calculate and set CommandGroupLength.
void set_dataset(core::dicom_dataset ds)
Set the data set for this message.
auto requested_sop_instance_uid() const -> std::string
Get the Requested SOP Instance UID (for N-SET, N-GET, N-ACTION, N-DELETE)
void set_status(status_code status)
Set the status code (for response messages)
void set_affected_sop_class_uid(std::string_view uid)
Set the Affected SOP Class UID.
auto is_response() const noexcept -> bool
Check if this is a response message.
auto event_type_id() const -> std::optional< uint16_t >
Get the Event Type ID (for N-EVENT-REPORT)
auto has_dataset() const noexcept -> bool
Check if the message has an associated data set.
void update_data_set_type()
Update the CommandDataSetType field based on dataset presence.
auto failed_subops() const -> std::optional< uint16_t >
Get the number of failed sub-operations.
void set_remaining_subops(uint16_t count)
Set the number of remaining sub-operations.
auto dataset() -> kcenon::pacs::Result< std::reference_wrapper< core::dicom_dataset > >
Get mutable reference to the data set.
auto priority() const -> uint16_t
Get the priority.
std::pair< std::vector< uint8_t >, std::vector< uint8_t > > encoded_message
Encoded message result type: pair of (command_set_bytes, dataset_bytes)
void set_message_id_responded_to(uint16_t id)
Set the Message ID Being Responded To (for responses)
auto action_type_id() const -> std::optional< uint16_t >
Get the Action Type ID (for N-ACTION)
void set_failed_subops(uint16_t count)
Set the number of failed sub-operations.
auto affected_sop_instance_uid() const -> std::string
Get the Affected SOP Instance UID.
void set_affected_sop_instance_uid(std::string_view uid)
Set the Affected SOP Instance UID.
auto command() const noexcept -> command_field
Get the command field.
dimse_message()=default
Default constructor (creates an invalid message)
void set_requested_sop_class_uid(std::string_view uid)
Set the Requested SOP Class UID.
auto completed_subops() const -> std::optional< uint16_t >
Get the number of completed sub-operations.
void set_warning_subops(uint16_t count)
Set the number of warning sub-operations.
DIMSE message encoding and decoding.
Encoder/decoder for Explicit VR Little Endian transfer syntax.
Encoder/decoder for Implicit VR Little Endian transfer syntax.
@ UL
Unsigned Long (4 bytes)
@ UI
Unique Identifier (64 chars max)
@ US
Unsigned Short (2 bytes)
@ AT
Attribute Tag (4 bytes)
@ implicit
VR determined from data dictionary lookup.
constexpr int dimse_error
constexpr core::dicom_tag tag_action_type_id
Action Type ID (0000,1008) - US.
constexpr core::dicom_tag tag_command_field
Command Field (0000,0100) - US.
constexpr core::dicom_tag tag_number_of_failed_subops
Number of Failed Sub-operations (0000,1022) - US.
constexpr core::dicom_tag tag_message_id
Message ID (0000,0110) - US.
auto make_n_set_rsp(uint16_t message_id_responded_to, std::string_view sop_class_uid, std::string_view sop_instance_uid, status_code status=status_success) -> dimse_message
Create an N-SET response message.
auto make_n_action_rsp(uint16_t message_id_responded_to, std::string_view sop_class_uid, std::string_view sop_instance_uid, uint16_t action_type_id, status_code status=status_success) -> dimse_message
Create an N-ACTION response message.
constexpr core::dicom_tag tag_number_of_remaining_subops
Number of Remaining Sub-operations (0000,1020) - US.
constexpr core::dicom_tag tag_priority
Priority (0000,0700) - US.
constexpr core::dicom_tag tag_command_group_length
Command Group Length (0000,0000) - UL.
constexpr std::string_view to_string(command_field cmd) noexcept
Convert command field to string representation.
auto make_n_create_rq(uint16_t message_id, std::string_view sop_class_uid, std::string_view sop_instance_uid="") -> dimse_message
Create an N-CREATE request message.
auto make_n_get_rq(uint16_t message_id, std::string_view sop_class_uid, std::string_view sop_instance_uid, const std::vector< core::dicom_tag > &attribute_tags={}) -> dimse_message
Create an N-GET request message.
constexpr core::dicom_tag tag_affected_sop_instance_uid
Affected SOP Instance UID (0000,1000) - UI.
constexpr bool is_response(command_field cmd) noexcept
Check if a command field represents a response.
auto make_n_event_report_rsp(uint16_t message_id_responded_to, std::string_view sop_class_uid, std::string_view sop_instance_uid, uint16_t event_type_id, status_code status=status_success) -> dimse_message
Create an N-EVENT-REPORT response message.
constexpr core::dicom_tag tag_number_of_warning_subops
Number of Warning Sub-operations (0000,1023) - US.
constexpr core::dicom_tag tag_attribute_identifier_list
Attribute Identifier List (0000,1005) - AT.
auto make_n_action_rq(uint16_t message_id, std::string_view sop_class_uid, std::string_view sop_instance_uid, uint16_t action_type_id) -> dimse_message
Create an N-ACTION request message.
auto make_n_set_rq(uint16_t message_id, std::string_view sop_class_uid, std::string_view sop_instance_uid) -> dimse_message
Create an N-SET request message.
constexpr uint16_t command_data_set_type_present
Value indicating data set is present (any value other than 0x0101)
auto make_c_echo_rq(uint16_t message_id, std::string_view sop_class_uid="1.2.840.10008.1.1") -> dimse_message
Create a C-ECHO request message.
auto make_c_find_rsp(uint16_t message_id_responded_to, std::string_view sop_class_uid, status_code status) -> dimse_message
Create a C-FIND response message.
constexpr core::dicom_tag tag_message_id_responded_to
Message ID Being Responded To (0000,0120) - US.
auto make_n_delete_rq(uint16_t message_id, std::string_view sop_class_uid, std::string_view sop_instance_uid) -> dimse_message
Create an N-DELETE request message.
auto make_c_find_rq(uint16_t message_id, std::string_view sop_class_uid, uint16_t priority=priority_medium) -> dimse_message
Create a C-FIND request message.
auto make_c_echo_rsp(uint16_t message_id_responded_to, status_code status=status_success, std::string_view sop_class_uid="1.2.840.10008.1.1") -> dimse_message
Create a C-ECHO response message.
constexpr bool is_request(command_field cmd) noexcept
Check if a command field represents a request.
dimse_error
Error codes for DIMSE message operations.
auto make_c_store_rsp(uint16_t message_id_responded_to, std::string_view sop_class_uid, std::string_view sop_instance_uid, status_code status=status_success) -> dimse_message
Create a C-STORE response message.
auto make_n_event_report_rq(uint16_t message_id, std::string_view sop_class_uid, std::string_view sop_instance_uid, uint16_t event_type_id) -> dimse_message
Create an N-EVENT-REPORT request message.
auto make_n_create_rsp(uint16_t message_id_responded_to, std::string_view sop_class_uid, std::string_view sop_instance_uid, status_code status=status_success) -> dimse_message
Create an N-CREATE response message.
command_field
DIMSE command field values.
@ n_get_rq
N-GET Request - Get attribute values.
@ n_delete_rsp
N-DELETE Response.
@ c_echo_rsp
C-ECHO Response.
@ c_store_rq
C-STORE Request - Store composite SOP instance.
@ n_set_rsp
N-SET Response.
@ n_create_rsp
N-CREATE Response.
@ n_get_rsp
N-GET Response.
@ n_create_rq
N-CREATE Request - Create SOP instance.
@ n_set_rq
N-SET Request - Set attribute values.
@ c_echo_rq
C-ECHO Request - Verify DICOM connection.
@ c_store_rsp
C-STORE Response.
@ c_find_rq
C-FIND Request - Query for matching instances.
@ n_delete_rq
N-DELETE Request - Delete SOP instance.
@ n_event_report_rsp
N-EVENT-REPORT Response.
@ n_event_report_rq
N-EVENT-REPORT Request - Report event notification.
@ n_action_rsp
N-ACTION Response.
@ n_action_rq
N-ACTION Request - Request action.
@ c_find_rsp
C-FIND Response.
auto make_c_store_rq(uint16_t message_id, std::string_view sop_class_uid, std::string_view sop_instance_uid, uint16_t priority=priority_medium) -> dimse_message
Create a C-STORE request message.
auto make_n_delete_rsp(uint16_t message_id_responded_to, std::string_view sop_class_uid, std::string_view sop_instance_uid, status_code status=status_success) -> dimse_message
Create an N-DELETE response message.
constexpr uint16_t priority_medium
Medium priority.
constexpr uint16_t command_data_set_type_null
Null value indicating no data set present.
constexpr core::dicom_tag tag_number_of_completed_subops
Number of Completed Sub-operations (0000,1021) - US.
constexpr core::dicom_tag tag_requested_sop_instance_uid
Requested SOP Instance UID (0000,1001) - UI.
constexpr core::dicom_tag tag_event_type_id
Event Type ID (0000,1002) - US.
uint16_t status_code
DIMSE status code type alias.
auto make_n_get_rsp(uint16_t message_id_responded_to, std::string_view sop_class_uid, std::string_view sop_instance_uid, status_code status=status_success) -> dimse_message
Create an N-GET response message.
constexpr core::dicom_tag tag_affected_sop_class_uid
Affected SOP Class UID (0000,0002) - UI.
constexpr core::dicom_tag tag_status
Status (0000,0900) - US.
constexpr core::dicom_tag tag_requested_sop_class_uid
Requested SOP Class UID (0000,0003) - UI.
constexpr core::dicom_tag tag_command_data_set_type
Command Data Set Type (0000,0800) - US.
kcenon::common::error_info error_info
Error information type.