Coverage for aiocoap / transport_params.py: 94%

51 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-01-10 10:42 +0000

1# SPDX-FileCopyrightText: Christian Amsüss and the aiocoap contributors 

2# 

3# SPDX-License-Identifier: MIT 

4 

5from dataclasses import dataclass 

6from typing import Optional, Self 

7 

8from .util.dataclass_data import LoadStoreClass 

9 

10# The concrete per-transport data classes are *not* part of the 

11# aiocoap/transport/ files to avoid eagerly loading them. (And let's see, maybe 

12# they rackup so many commonalities that it doesn't make sense to have them per 

13# tranport anyway). 

14 

15 

16@dataclass 

17class Udp6Parameters(LoadStoreClass): 

18 """Parameters for setting up a ``udp6`` transport (see :mod:`..transport_params` 

19 for context).""" 

20 

21 # Not managing any details yet; those will come as things are being wired up. 

22 

23 

24# ## Address and port to bind to. 

25# ## 

26# ## The practical value when nothing is given explicitly depends on whether 

27# ## a server is run (then it's ``[::]:5683``) or not (then it's effectively 

28# ## ``[::]:0``, which binds to an ephemeral port, although the `bind` 

29# ## syscall may be elided in that case). 

30# bind: Optional[str] = None 

31 

32 

33@dataclass 

34class Simple6Parameters(LoadStoreClass): 

35 """Parameters for setting up a ``simple6`` transport.""" 

36 

37 

38@dataclass 

39class SimpleSocketServerParameters(LoadStoreClass): 

40 """Parameters for setting up a ``simplesocketserver`` transport.""" 

41 

42 

43@dataclass 

44class TinyDTLSParameters(LoadStoreClass): 

45 """Parameters for setting up a ``tinydtls`` transport.""" 

46 

47 

48@dataclass 

49class TinyDTLSServerParameters(LoadStoreClass): 

50 """Parameters for setting up a ``tinydtls_server`` transport.""" 

51 

52 

53@dataclass 

54class TcpClientParameters(LoadStoreClass): 

55 """Parameters for setting up a ``tcpclient`` transport.""" 

56 

57 

58@dataclass 

59class TcpServerParameters(LoadStoreClass): 

60 """Parameters for setting up a ``tcpserver`` transport.""" 

61 

62 

63@dataclass 

64class TlsClientParameters(LoadStoreClass): 

65 """Parameters for setting up a ``tlsclient`` transport.""" 

66 

67 

68@dataclass 

69class TlsServerParameters(LoadStoreClass): 

70 """Parameters for setting up a ``tlsserver`` transport.""" 

71 

72 

73@dataclass 

74class WsParameters(LoadStoreClass): 

75 """Parameters for setting up a ``ws`` transport.""" 

76 

77 

78@dataclass 

79class OscoreParameters(LoadStoreClass): 

80 """Parameters for setting up an ``oscore`` transport.""" 

81 

82 

83@dataclass 

84class TransportParameters(LoadStoreClass): 

85 """Parameters that guide which transports are selected and how they are 

86 configured.""" 

87 

88 @classmethod 

89 def _compat_create(cls, input: Self | None | dict | list[str]) -> Self: 

90 """Used to coerce transports= argument of 

91 ``create_{server,client}_context`` into this type. 

92 

93 It passes on any instance, loads from a JSON/CBOR/TOML style dict if 

94 present, selects the default transports when no data is given, and, in 

95 case of the legacy list-of-strings, sets them up as keys only 

96 (effectively choosing only those transports without any concrete 

97 configuration). 

98 

99 >>> TransportParameters._compat_create(None) == TransportParameters(default_transports=True) 

100 True 

101 """ 

102 

103 if isinstance(input, cls): 

104 return input 

105 elif input is None: 

106 return cls(default_transports=True) 

107 elif isinstance(input, dict): 

108 return cls.load(input) 

109 elif isinstance(input, list): 

110 return cls.load({k: {} for k in input}) 

111 else: 

112 raise ValueError( 

113 "Transports needs to bei either TransportParameters, or a dict that can be loaded as one, or None, or (deprecated) a list of transport names." 

114 ) 

115 

116 ## If True, in any place it applies, parameters for server operation are 

117 ## set. (For example, the UDP and TCP ports bind to the default port rather 

118 ## than an ephemeral port, and the default transports selection may be 

119 ## different). 

120 ## 

121 ## Leaving this unset allows the parameters to be set when creating the 

122 ## context. 

123 is_server: Optional[bool] = None 

124 

125 ## If True, all transports that are on by default (or selected by the 

126 ## environment) are enabled. 

127 ## 

128 ## Note that this is False by default: If TransportParameters are given 

129 ## explicitly (by construction or by loading from JSON/CBOR/TOML style 

130 ## files), all transports are opt-in, and only when not specifying 

131 ## anything (or a legacy format) to the Context constructor, this gets set. 

132 default_transports: bool = False 

133 

134 udp6: Udp6Parameters | None = None 

135 simple6: Simple6Parameters | None = None 

136 simplesocketserver: SimpleSocketServerParameters | None = None 

137 tinydtls: TinyDTLSParameters | None = None 

138 tinydtls_server: TinyDTLSServerParameters | None = None 

139 tcpclient: TcpClientParameters | None = None 

140 tcpserver: TcpServerParameters | None = None 

141 tlsclient: TlsClientParameters | None = None 

142 tlsserver: TlsServerParameters | None = None 

143 ws: WsParameters | None = None 

144 oscore: OscoreParameters | None = None