/
dimse_messages.py
958 lines (813 loc) · 31.2 KB
/
dimse_messages.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
"""Define the DIMSE Message classes."""
from io import BytesIO
import logging
from math import ceil
from pathlib import Path
from tempfile import NamedTemporaryFile
from typing import Iterator, Optional, TYPE_CHECKING, cast, Union, Tuple
from pydicom.dataset import Dataset
from pydicom.filewriter import write_file_meta_info
from pydicom.tag import Tag
from pydicom.uid import UID
from pynetdicom import _config
from pynetdicom.dimse_primitives import (
C_STORE,
C_FIND,
C_GET,
C_MOVE,
C_ECHO,
C_CANCEL,
N_EVENT_REPORT,
N_GET,
N_SET,
N_ACTION,
N_CREATE,
N_DELETE,
DimsePrimitiveType,
)
from pynetdicom.dsutils import encode, decode, create_file_meta
from pynetdicom.pdu_primitives import P_DATA
if TYPE_CHECKING: # pragma: no cover
from pynetdicom.association import Association
from pynetdicom.dimse_primitives import NTF
LOGGER = logging.getLogger("pynetdicom.dimse")
# PS3.7 Section 9.3
_COMMAND_SET_KEYWORDS = {
"C-ECHO-RQ": (
"CommandGroupLength",
"AffectedSOPClassUID",
"CommandField",
"MessageID",
"CommandDataSetType",
),
"C-ECHO-RSP": (
"CommandGroupLength",
"AffectedSOPClassUID",
"CommandField",
"MessageIDBeingRespondedTo",
"CommandDataSetType",
"Status",
"ErrorComment",
),
"C-STORE-RQ": (
"CommandGroupLength",
"AffectedSOPClassUID",
"CommandField",
"MessageID",
"Priority",
"CommandDataSetType",
"AffectedSOPInstanceUID",
"MoveOriginatorApplicationEntityTitle",
"MoveOriginatorMessageID",
),
"C-STORE-RSP": (
"CommandGroupLength",
"AffectedSOPClassUID",
"CommandField",
"MessageIDBeingRespondedTo",
"CommandDataSetType",
"Status",
"AffectedSOPInstanceUID",
"OffendingElement",
"ErrorComment",
),
"C-FIND-RQ": (
"CommandGroupLength",
"AffectedSOPClassUID",
"CommandField",
"MessageID",
"Priority",
"CommandDataSetType",
),
"C-FIND-RSP": (
"CommandGroupLength",
"AffectedSOPClassUID",
"CommandField",
"MessageIDBeingRespondedTo",
"CommandDataSetType",
"Status",
"OffendingElement",
"ErrorComment",
),
"C-CANCEL-RQ": (
"CommandGroupLength",
"CommandField",
"MessageIDBeingRespondedTo",
"CommandDataSetType",
),
"C-GET-RQ": (
"CommandGroupLength",
"AffectedSOPClassUID",
"CommandField",
"MessageID",
"Priority",
"CommandDataSetType",
),
"C-GET-RSP": (
"CommandGroupLength",
"AffectedSOPClassUID",
"CommandField",
"MessageIDBeingRespondedTo",
"CommandDataSetType",
"Status",
"NumberOfRemainingSuboperations",
"NumberOfCompletedSuboperations",
"NumberOfFailedSuboperations",
"NumberOfWarningSuboperations",
"OffendingElement",
"ErrorComment",
),
"C-MOVE-RQ": (
"CommandGroupLength",
"AffectedSOPClassUID",
"CommandField",
"MessageID",
"Priority",
"CommandDataSetType",
"MoveDestination",
),
"C-MOVE-RSP": (
"CommandGroupLength",
"AffectedSOPClassUID",
"CommandField",
"MessageIDBeingRespondedTo",
"CommandDataSetType",
"Status",
"NumberOfRemainingSuboperations",
"NumberOfCompletedSuboperations",
"NumberOfFailedSuboperations",
"NumberOfWarningSuboperations",
"OffendingElement",
"ErrorComment",
),
"N-EVENT-REPORT-RQ": (
"CommandGroupLength",
"AffectedSOPClassUID",
"CommandField",
"MessageID",
"CommandDataSetType",
"AffectedSOPInstanceUID",
"EventTypeID",
),
"N-EVENT-REPORT-RSP": (
"CommandGroupLength",
"AffectedSOPClassUID",
"CommandField",
"MessageIDBeingRespondedTo",
"CommandDataSetType",
"Status",
"AffectedSOPInstanceUID",
"EventTypeID",
"ErrorID",
"ErrorComment",
),
"N-GET-RQ": (
"CommandGroupLength",
"RequestedSOPClassUID",
"CommandField",
"MessageID",
"CommandDataSetType",
"RequestedSOPInstanceUID",
"AttributeIdentifierList",
),
"N-GET-RSP": (
"CommandGroupLength",
"AffectedSOPClassUID",
"CommandField",
"MessageIDBeingRespondedTo",
"CommandDataSetType",
"Status",
"AffectedSOPInstanceUID",
"AttributeIdentifierList",
"ErrorComment",
"ErrorID",
),
"N-SET-RQ": (
"CommandGroupLength",
"RequestedSOPClassUID",
"CommandField",
"MessageID",
"CommandDataSetType",
"RequestedSOPInstanceUID",
),
"N-SET-RSP": (
"CommandGroupLength",
"AffectedSOPClassUID",
"CommandField",
"MessageIDBeingRespondedTo",
"CommandDataSetType",
"Status",
"AffectedSOPInstanceUID",
"AttributeIdentifierList",
"ErrorComment",
"ErrorID",
),
"N-ACTION-RQ": (
"CommandGroupLength",
"RequestedSOPClassUID",
"CommandField",
"MessageID",
"CommandDataSetType",
"RequestedSOPInstanceUID",
"ActionTypeID",
),
"N-ACTION-RSP": (
"CommandGroupLength",
"AffectedSOPClassUID",
"CommandField",
"MessageIDBeingRespondedTo",
"CommandDataSetType",
"Status",
"AffectedSOPInstanceUID",
"ActionTypeID",
"ErrorID",
"ErrorComment",
),
"N-CREATE-RQ": (
"CommandGroupLength",
"AffectedSOPClassUID",
"CommandField",
"MessageID",
"CommandDataSetType",
"AffectedSOPInstanceUID",
),
"N-CREATE-RSP": (
"CommandGroupLength",
"AffectedSOPClassUID",
"CommandField",
"MessageIDBeingRespondedTo",
"CommandDataSetType",
"Status",
"AffectedSOPInstanceUID",
"ErrorID",
"ErrorComment",
),
"N-DELETE-RQ": (
"CommandGroupLength",
"RequestedSOPClassUID",
"CommandField",
"MessageID",
"CommandDataSetType",
"RequestedSOPInstanceUID",
),
"N-DELETE-RSP": (
"CommandGroupLength",
"AffectedSOPClassUID",
"CommandField",
"MessageIDBeingRespondedTo",
"CommandDataSetType",
"Status",
"AffectedSOPInstanceUID",
"ErrorComment",
"ErrorID",
),
}
# Used with DIMSEMessage.message_to_primitive
_MSG_TO_PRIMITVE = {
"C_ECHO": C_ECHO,
"C_STORE": C_STORE,
"C_FIND": C_FIND,
"C_GET": C_GET,
"C_MOVE": C_MOVE,
"C_CANCEL": C_CANCEL,
"N_EVENT_REPORT": N_EVENT_REPORT,
"N_GET": N_GET,
"N_SET": N_SET,
"N_ACTION": N_ACTION,
"N_CREATE": N_CREATE,
"N_DELETE": N_DELETE,
}
_MULTIVALUE_TAGS = [Tag("OffendingElement"), Tag("AttributeIdentifierList")]
class DIMSEMessage:
"""Represents a DIMSE Message.
Information is communicated across the DICOM network interface in a DICOM
Message. A Message is composed of a Command Set followed by a
conditional Data Set. The Command Set is used to indicate the
operations/notifications to be performed on or with the Data Set (see
PS3.7 6.2-3).
::
primitive_to_message() encode_msg()
+-------------+ ---> +-----------+ ---> +-------------+
| DIMSE | | DIMSE | | P-DATA |
| primitive | | Message | | primitive |
+-------------+ <--- +-----------+ <--- +-------------+
message_to_primitive() decode_msg()
**Command Set**
A Command Set is constructed of Command Elements. Command Elements contain
the encoded values for each field of the Command Set per the semantics
specified in the DIMSE protocol (PS3.7 9.2 and 10.2). Each Command Element
is composed of an explicit Tag, Value Length and a Value field. The
encoding of the Command Set shall be *Little Endian Implicit VR*.
**Message Types**
The following message types are available: C_STORE_RQ, C_STORE_RSP,
C_ECHO_RQ, C_ECHO_RSP, C_FIND_RQ, C_FIND_RSP, C_GET_RQ, C_GET_RSP,
C_MOVE_RQ, C_MOVE_RSP, C_CANCEL_RQ, N_EVENT_REPORT_RQ, N_EVENT_REPORT_RSP,
N_SET_RQ, N_SET_RSP, N_GET_RQ, N_GET_RSP, N_ACTION_RQ, N_ACTION_RSP,
N_CREATE_RQ, N_CREATE_RSP, N_DELETE_RQ, N_DELETE_RSP.
Attributes
----------
command_set : pydicom.dataset.Dataset
The message Command Set information (see PS3.7 6.3).
context_id : int
The presentation context ID.
data_set : io.BytesIO
The encoded message Data Set (see PS3.7 6.3).
encoded_command_set : io.BytesIO
During decoding of an incoming P-DATA primitive this stores the
encoded Command Set data from the fragments.
"""
def __init__(self) -> None:
"""Create a new DIMSE Message."""
self.context_id: Optional[int] = None
# Required to save command set data from multiple fragments
self.encoded_command_set = BytesIO()
self.data_set: Optional[BytesIO] = BytesIO()
self.command_set = Dataset()
# If reading the dataset in chunks this will be a tuple:
# (its file path, a byte offset to the start of the dataset)
# If writing the dataset in chunks this will be a Path:
# its file path
self._data_set_path: Optional[Union[Path, Tuple[Path, int]]] = None
# If writing the dataset in chunks this will be a NamedTemporaryFile:
# the file object backing its file path
self._data_set_file: Optional["NTF"] = None
cls_name = self.__class__.__name__
if cls_name == "DIMSEMessage":
return
# Set the command set attributes for the subclasses
for keyword in _COMMAND_SET_KEYWORDS[cls_name.replace("_", "-")]:
setattr(self.command_set, keyword, None)
def decode_msg(
self, primitive: P_DATA, assoc: Optional["Association"] = None
) -> bool:
"""Converts P-DATA primitives into a ``DIMSEMessage`` sub-class.
Decodes the data from the P-DATA service primitive (which
may contain the results of one or more P-DATA-TF PDUs) into the
:attr:`~DIMSEMessage.command_set` and :attr:`~DIMSEMessage.data_set`
attributes. Also sets the :attr:`~DIMSEMessage.context_id` and
:attr:`~DIMSEMessage.encoded_command_set` attributes of the
``DIMSEMessage`` sub-class object.
Parameters
----------
primitive : pdu_primitives.P_DATA
The P-DATA service primitive to be decoded into a DIMSE message.
assoc : association.Association, optional
The association processing the message. This is required when:
* :attr:`~pynetdicom._config.STORE_RECV_CHUNKED_DATASET` is
``True``
* The P-DATA primitive contains part of a C-STORE-RQ message
In this case the association is consulted for its accepted
transfer syntax, which is included in the File Meta Information
of the stored dataset.
Returns
-------
bool
``True`` when the DIMSE message is completely decoded, ``False``
otherwise.
References
----------
* DICOM Standard, Part 8, :dcm:`Annex E<part08/chapter_E.html>`
"""
# Make sure this is a P-DATA primitive
if primitive.__class__ != P_DATA or primitive is None:
return False
for (context_id, data) in primitive.presentation_data_value_list:
# The first byte of the P-DATA is the Message Control Header
# See Part 8, Annex E.2
# The standard says that only the significant bits (ie the last
# two) should be checked
# xxxxxx00 - Message Dataset information, not the last fragment
# xxxxxx01 - Command information, not the last fragment
# xxxxxx10 - Message Dataset information, the last fragment
# xxxxxx11 - Command information, the last fragment
control_header_byte = data[0]
# LOGGER.debug('Control header byte %s', control_header_byte)
# print(f'Control header byte {control_header_byte}')
# COMMAND SET
# P-DATA fragment contains Command Set information
# (control_header_byte is xxxxxx01 or xxxxxx11)
if control_header_byte & 1:
# The command set may be spread out over a number
# of fragments and P-DATA primitives and we need to remember
# the elements from previous fragments, hence the
# encoded_command_set class attribute
# This adds all the command set data to the class object
self.encoded_command_set.write(data[1:])
# The final command set fragment (xxxxxx11) has been added
# so decode the command set
if control_header_byte & 2:
# Presentation Context ID
# Set this now as must only be one final command set
# fragment and command set must always be present
self.context_id = context_id
# Command Set is always encoded Implicit VR Little Endian
# decode(dataset, is_implicit_VR, is_little_endian)
# pylint: disable=attribute-defined-outside-init
self.command_set = decode(self.encoded_command_set, True, True)
# Determine which DIMSE Message class to use
self.__class__ = _MESSAGE_TYPES[
cast(int, self.command_set.CommandField)
][1]
# Determine if a Data Set is present by checking for
# (0000, 0800) CommandDataSetType US 1. If the value is
# 0x0101 no dataset present, otherwise one is.
if self.command_set.CommandDataSetType == 0x0101:
# By returning True we're indicating that the message
# has been completely decoded
return True
# Data Set is present
if _config.STORE_RECV_CHUNKED_DATASET and isinstance(
self, C_STORE_RQ
):
# delete=False is a workaround for Windows
# Setting delete=True prevents us from re-opening
# the file after it is opened by NamedTemporaryFile
# below.
self._data_set_file = cast(
"NTF",
NamedTemporaryFile(delete=False, mode="wb", suffix=".dcm"),
)
self._data_set_path = Path(self._data_set_file.name)
# Write the File Meta
self._data_set_file.write(b"\x00" * 128)
self._data_set_file.write(b"DICM")
cs = self.command_set
cx = cast("Association", assoc)._accepted_cx[context_id]
sop_class = cast(UID, cs.AffectedSOPClassUID)
sop_instance = cast(UID, cs.AffectedSOPInstanceUID)
write_file_meta_info(
self._data_set_file, # type: ignore
create_file_meta(
sop_class_uid=sop_class,
sop_instance_uid=sop_instance,
transfer_syntax=cx.transfer_syntax[0],
),
)
# DATA SET
# P-DATA fragment contains Data Set information
# (control_header_byte is xxxxxx00 or xxxxxx10)
else:
# As with the command set, the data set may be spread over
# a number of fragments in each P-DATA primitive and a
# number of P-DATA primitives.
if self._data_set_file:
self._data_set_file.write(data[1:])
self._data_set_file.file.flush()
else:
cast(BytesIO, self.data_set).write(data[1:])
# The final data set fragment (xxxxxx10) has been added
if control_header_byte & 2 != 0:
# By returning True we're indicating that the message
# has been completely decoded
return True
# We return False to indicate that the message isn't yet fully decoded
return False
def encode_msg(self, context_id: int, max_pdu_length: int) -> Iterator[P_DATA]:
"""Yield P-DATA primitives for the current DIMSE Message.
**Encoding**
The encoding of the Command Set shall be *Little Endian Implicit VR*,
while the *Data Set* will be encoded as per the agreed presentation
context.
A P-DATA request's PDV List parameter shall contain one or more PDVs.
Each PDV is wholly contained in a given P-DATA request and doesn't
span across several P-DATA request primitives.
The fragmentation of any message results in a series of PDVs that shall
be sent, on a given association, by a corresponding series of P-DATA
requests preserving the ordering of the fragments of any message.
No fragments of any other messages shall be sent until all fragments
of the current message have been sent.
Parameters
----------
context_id : int
The *ID* of the agreed presentation context.
max_pdu_length : int
The maximum PDV length (in bytes).
Yields
------
pdu_primitives.P_DATA
The current DIMSE message as one or more P-DATA service
primitives.
References
----------
* DICOM Standard, Part 7,
:dcm:`Section 6.3.1<part07/sect_6.3.html#sect_6.3.1>`
* DICOM Standard, Part 8, :dcm:`Annex E<part08/chapter_E.html>`
"""
self.context_id = context_id
# The Command Set is always Little Endian Implicit VR (PS3.7 6.3.1)
# encode(dataset, is_implicit_VR, is_little_endian)
encoded_command_set = cast(bytes, encode(self.command_set, True, True))
# COMMAND SET (always)
# Split the command set into fragments with maximum size max_pdu_length
if max_pdu_length == 0:
nr_fragments = 1
else:
nr_fragments = ceil(len(encoded_command_set) / (max_pdu_length - 6))
cmd_fragments = self._generate_pdv_fragments(
encoded_command_set, max_pdu_length
)
# First to (n - 1)th command data fragment - bits xxxxxx01
for ii in range(int(nr_fragments - 1)):
pdata = P_DATA()
pdata.presentation_data_value_list.append(
(context_id, b"\x01" + next(cmd_fragments))
)
yield pdata
# Last command data fragment - bits xxxxxx11
pdata = P_DATA()
pdata.presentation_data_value_list.append(
(context_id, b"\x03" + next(cmd_fragments))
)
yield pdata
# DATASET (if available)
# Check that the Data Set is not empty
if self.data_set is not None:
encoded_data_set = self.data_set.getvalue()
if encoded_data_set:
# Split the data set into fragments with maximum
# size max_pdu_length
if max_pdu_length == 0:
nr_fragments = 1
else:
nr_fragments = ceil(len(encoded_data_set) / (max_pdu_length - 6))
ds_fragments = self._generate_pdv_fragments(
encoded_data_set, max_pdu_length
)
# First to (n - 1)th dataset fragment - bits xxxxxx00
for ii in range(int(nr_fragments - 1)):
pdata = P_DATA()
pdata.presentation_data_value_list.append(
(context_id, b"\x00" + next(ds_fragments))
)
yield pdata
# Last dataset fragment - bits xxxxxx10
pdata = P_DATA()
pdata.presentation_data_value_list.append(
(context_id, b"\x02" + next(ds_fragments))
)
yield pdata
elif self._data_set_path is not None:
# Read and send encoded dataset from file
# Buffer size determined by io.DEFAULT_BUFFER_SIZE
self._data_set_path = cast(Tuple[Path, int], self._data_set_path)
with open(self._data_set_path[0], "rb") as f:
end = f.seek(0, 2)
length = end - f.seek(self._data_set_path[1])
if max_pdu_length == 0:
nr_fragments = 1
max_pdu_length = length + 6
else:
nr_fragments = ceil(length / (max_pdu_length - 6))
# First to (n - 1)th dataset fragment - bits xxxxxx00
for ii in range(int(nr_fragments - 1)):
pdata = P_DATA()
pdata.presentation_data_value_list.append(
(context_id, b"\x00" + f.read(max_pdu_length - 6))
)
yield pdata
# Last dataset fragment - bits xxxxxx10
pdata = P_DATA()
pdata.presentation_data_value_list.append(
(context_id, b"\x02" + f.read(max_pdu_length - 6))
)
yield pdata
@staticmethod
def _generate_pdv_fragments(
bytestream: bytes, fragment_length: int
) -> Iterator[bytes]:
"""Fragment `bytestream` into chunks, each `fragment_length` long.
Fragments bytestream data for use in PDVs.
The Maximum Length Negotiation allows receivers to limit the size of
the Presentation Data Values List parameters of each P-DATA indication.
The Association requestor shall specify the maximum length in bytes for
the PDV list parameter it is ready to receive in each P-DATA
indication. The Association acceptor shall ensure in its fragmentation
of the DICOM Messages that the list of PDVs included in each P-DATA
request does not exceed this maximum length.
Parameters
----------
bytestream : bytes
The data to be fragmented.
fragment_length : int
The maximum size of each fragment, a value of 0 is taken to mean
the fragment is infinite. Cannot be between 1 and 7 as
each Presentation Data Value Item is::
1 - 4 | 5 | 6 ->
Item length | Context ID | Presentation data value ->
Yields
------
fragment : bytes
A `bytestream` fragment, with maximum length `fragment_length`, but
may be smaller depending on the size of `bytestream`.
References
----------
DICOM Standard, Part 8, Annex D.1
"""
if fragment_length == 0:
yield bytestream
return
elif 0 < fragment_length < 7:
raise ValueError("'fragment_length' cannot be between 1 and 7.")
# Because the PDV item includes an extra 6 bytes of data at the start
# we need to decrease `fragment_length` by 6 bytes.
fragment_length -= 6
offset = 0
no_pdv = ceil(len(bytestream) / fragment_length)
for ii in range(int(no_pdv)):
yield bytestream[offset : offset + fragment_length]
offset += fragment_length
def message_to_primitive(self) -> DimsePrimitiveType:
"""Convert the ``DIMSEMessage`` class to a DIMSE primitive.
Returns
-------
DIMSEPrimitive sub-class
One of the DIMSE message primitives from
:ref:`pynetdicom.dimse_primitives<api_dimse_primitives>` generated
from the current ``DIMSEMessage`` sub-class object.
"""
cls_type_name = self.__class__.__name__
final_underscore = cls_type_name.rfind("_R")
primitive = cast(
DimsePrimitiveType, _MSG_TO_PRIMITVE[cls_type_name[:final_underscore]]()
)
# Command Set
# For each parameter in the primitive, set the appropriate value
# from the Message's Command Set elements
for elem in self.command_set:
if hasattr(primitive, elem.keyword):
value = elem.value
if elem.VM > 1 and elem.tag not in _MULTIVALUE_TAGS:
LOGGER.warning(
f"Non-conformant VM {elem.VM} for '{elem.keyword}', "
"taking the first value"
)
value = value[0]
setattr(primitive, elem.keyword, value)
# Datasets
# Set the primitive's DataSet/Identifier/etc attribute
try:
dataset_keyword = _DATASET_KEYWORDS[cls_type_name]
setattr(primitive, dataset_keyword, self.data_set)
except KeyError:
pass
# Set the presentation context ID the message was set under
primitive._context_id = self.context_id
primitive._dataset_path = self._data_set_path
primitive._dataset_file = self._data_set_file
return primitive
def primitive_to_message(self, primitive: DimsePrimitiveType) -> None:
"""Convert a DIMSE `primitive` to the current ``DIMSEMessage`` object.
Parameters
----------
DIMSEPrimitive sub-class
A DIMSE message primitive from
:ref:`pynetdicom.dimse_primitives<api_dimse_primitives>`
to convert to the current ``DIMSEMessage`` object.
"""
# pylint: disable=too-many-branches,too-many-statements
cls_type_name = self.__class__.__name__.replace("_", "-")
if cls_type_name not in _COMMAND_SET_KEYWORDS:
raise ValueError(
f"Can't convert primitive to message for unknown "
f"DIMSE message type '{cls_type_name}'"
)
# Command Set
# Convert the message command set to the primitive attributes
for elem in self.command_set:
# Use the element keyword as these should match the parameter
# names in the primitive
if hasattr(primitive, elem.keyword):
# If value hasn't been set for a parameter then delete
# the corresponding element
attr = getattr(primitive, elem.keyword)
if attr is not None:
elem.value = attr
else:
del self.command_set[elem.tag]
# There's a one-to-one relationship in the _MESSAGE_TYPES dict, so
# invert it for convenience
rev_type = {vv[0]: kk for kk, vv in _MESSAGE_TYPES.items()}
self.command_set.CommandField = rev_type[cls_type_name]
# Data Set
# Default to no Data Set
self.data_set = BytesIO()
self.command_set.CommandDataSetType = 0x0101
try:
# These message types *may* have a dataset
dataset_keyword = _DATASET_KEYWORDS[self.__class__.__name__]
self.data_set = getattr(primitive, dataset_keyword)
if self.data_set:
self.command_set.CommandDataSetType = 0x0001
except KeyError:
# The following message types never have a dataset
# 'C_ECHO_RQ', 'C_ECHO_RSP', 'N_DELETE_RQ', 'C_STORE_RSP',
# 'C_CANCEL_RQ', 'N_DELETE_RSP', 'C_FIND_RSP', 'N_GET_RQ'
pass
self._data_set_path = getattr(primitive, "_dataset_path", None)
if self._data_set_path:
self.command_set.CommandDataSetType = 0x0001
# Set the Command Set length
self._set_command_group_length()
def _set_command_group_length(self) -> None:
"""Reset the Command Group Length element value.
Once the `command_set` Dataset has been built and filled with
values, this should be called to set the (Command Group Length* element
value correctly.
"""
# Remove CommandGroupLength to stop it messing up the length calc
del self.command_set.CommandGroupLength
# The Command Set is always Implicit VR Little Endian
self.command_set.CommandGroupLength = len(
cast(bytes, encode(self.command_set, True, True))
)
# Create DIMSEMessage subclasses
class C_STORE_RQ(DIMSEMessage):
pass
class C_STORE_RSP(DIMSEMessage):
pass
class C_FIND_RQ(DIMSEMessage):
pass
class C_FIND_RSP(DIMSEMessage):
pass
class C_GET_RQ(DIMSEMessage):
pass
class C_GET_RSP(DIMSEMessage):
pass
class C_MOVE_RQ(DIMSEMessage):
pass
class C_MOVE_RSP(DIMSEMessage):
pass
class C_ECHO_RQ(DIMSEMessage):
pass
class C_ECHO_RSP(DIMSEMessage):
pass
class C_CANCEL_RQ(DIMSEMessage):
pass
class N_EVENT_REPORT_RQ(DIMSEMessage):
pass
class N_EVENT_REPORT_RSP(DIMSEMessage):
pass
class N_GET_RQ(DIMSEMessage):
pass
class N_GET_RSP(DIMSEMessage):
pass
class N_SET_RQ(DIMSEMessage):
pass
class N_SET_RSP(DIMSEMessage):
pass
class N_ACTION_RQ(DIMSEMessage):
pass
class N_ACTION_RSP(DIMSEMessage):
pass
class N_CREATE_RQ(DIMSEMessage):
pass
class N_CREATE_RSP(DIMSEMessage):
pass
class N_DELETE_RQ(DIMSEMessage):
pass
class N_DELETE_RSP(DIMSEMessage):
pass
# Values from PS3.5
_MESSAGE_TYPES = {
0x0001: ("C-STORE-RQ", C_STORE_RQ),
0x8001: ("C-STORE-RSP", C_STORE_RSP),
0x0020: ("C-FIND-RQ", C_FIND_RQ),
0x8020: ("C-FIND-RSP", C_FIND_RSP),
0x0010: ("C-GET-RQ", C_GET_RQ),
0x8010: ("C-GET-RSP", C_GET_RSP),
0x0021: ("C-MOVE-RQ", C_MOVE_RQ),
0x8021: ("C-MOVE-RSP", C_MOVE_RSP),
0x0030: ("C-ECHO-RQ", C_ECHO_RQ),
0x8030: ("C-ECHO-RSP", C_ECHO_RSP),
0x0FFF: ("C-CANCEL-RQ", C_CANCEL_RQ),
0x0100: ("N-EVENT-REPORT-RQ", N_EVENT_REPORT_RQ),
0x8100: ("N-EVENT-REPORT-RSP", N_EVENT_REPORT_RSP),
0x0110: ("N-GET-RQ", N_GET_RQ),
0x8110: ("N-GET-RSP", N_GET_RSP),
0x0120: ("N-SET-RQ", N_SET_RQ),
0x8120: ("N-SET-RSP", N_SET_RSP),
0x0130: ("N-ACTION-RQ", N_ACTION_RQ),
0x8130: ("N-ACTION-RSP", N_ACTION_RSP),
0x0140: ("N-CREATE-RQ", N_CREATE_RQ),
0x8140: ("N-CREATE-RSP", N_CREATE_RSP),
0x0150: ("N-DELETE-RQ", N_DELETE_RQ),
0x8150: ("N-DELETE-RSP", N_DELETE_RSP),
}
_DATASET_KEYWORDS = {
"C_STORE_RQ": "DataSet",
"C_FIND_RQ": "Identifier",
"C_GET_RQ": "Identifier",
"C_MOVE_RQ": "Identifier",
"C_FIND_RSP": "Identifier",
"C_GET_RSP": "Identifier",
"C_MOVE_RSP": "Identifier",
"N_EVENT_REPORT_RQ": "EventInformation",
"N_EVENT_REPORT_RSP": "EventReply",
"N_GET_RSP": "AttributeList",
"N_SET_RSP": "AttributeList",
"N_CREATE_RQ": "AttributeList",
"N_CREATE_RSP": "AttributeList",
"N_SET_RQ": "ModificationList",
"N_ACTION_RQ": "ActionInformation",
"N_ACTION_RSP": "ActionReply",
}