Coverage for aiocoap/numbers/optionnumbers.py: 100%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

81 statements  

1# This file is part of the Python aiocoap library project. 

2# 

3# Copyright (c) 2012-2014 Maciej Wasilak <http://sixpinetrees.blogspot.com/>, 

4# 2013-2014 Christian Amsüss <c.amsuess@energyharvesting.at> 

5# 

6# aiocoap is free software, this file is published under the MIT license as 

7# described in the accompanying LICENSE file. 

8 

9"""Known values for CoAP option numbers 

10 

11The values defined in `OptionNumber` correspond to the IANA registry "CoRE 

12Parameters", subregistries "CoAP Method Codes" and "CoAP Response Codes". 

13 

14The option numbers come with methods that can be used to evaluate their 

15properties, see the `OptionNumber` class for details. 

16""" 

17 

18from ..util import ExtensibleIntEnum 

19from .. import optiontypes 

20 

21class OptionNumber(ExtensibleIntEnum): 

22 """A CoAP option number. 

23 

24 As the option number contains information on whether the option is 

25 critical, and whether it is safe-to-forward, those properties can be 

26 queried using the `is_*` group of methods. 

27 

28 Note that whether an option may be repeated or not does not only depend on 

29 the option, but also on the context, and is thus handled in the `Options` 

30 object instead.""" 

31 IF_MATCH = 1 

32 URI_HOST = 3 

33 ETAG = 4 

34 IF_NONE_MATCH = 5 

35 OBSERVE = 6 

36 URI_PORT = 7 

37 LOCATION_PATH = 8 

38 URI_PATH = 11 

39 CONTENT_FORMAT = 12 

40 MAX_AGE = 14 

41 URI_QUERY = 15 

42 ACCEPT = 17 

43 LOCATION_QUERY = 20 

44 BLOCK2 = 23 

45 BLOCK1 = 27 

46 SIZE2 = 28 

47 PROXY_URI = 35 

48 PROXY_SCHEME = 39 

49 SIZE1 = 60 

50 NO_RESPONSE = 258 

51 OBJECT_SECURITY = 9 

52 # picked for draft-ietf-core-echo-request-tag-09 

53 ECHO = 252 

54 REQUEST_TAG = 292 

55 

56 # experimental for draft-amsuess-core-cachable-oscore 

57 # 

58 # Using the number suggested there (rather than a high one) as this is 

59 # going to be used in overhead comparisons. 

60 REQUEST_HASH = 548 

61 

62 def __add__(self, delta): 

63 """Addition makes sense on these due to the delta encoding in CoAP 

64 serialization""" 

65 return type(self)(int(self) + delta) 

66 

67 def is_critical(self): 

68 return self & 0x01 == 0x01 

69 

70 def is_elective(self): 

71 return not self.is_critical() 

72 

73 def is_unsafe(self): 

74 return self & 0x02 == 0x02 

75 

76 def is_safetoforward(self): 

77 return not self.is_unsafe() 

78 

79 def is_nocachekey(self): 

80 if self.is_unsafe(): 

81 raise ValueError("NoCacheKey is only meaningful for safe options") 

82 return self & 0x1e == 0x1c 

83 

84 def is_cachekey(self): 

85 return not self.is_nocachekey() 

86 

87 def _get_format(self): 

88 if hasattr(self, "_format"): 

89 return self._format 

90 else: 

91 return optiontypes.OpaqueOption 

92 

93 def _set_format(self, value): 

94 self._format = value 

95 

96 format = property(_get_format, _set_format) 

97 

98 def create_option(self, decode=None, value=None): 

99 """Return an Option element of the appropriate class from this option 

100 number. 

101 

102 An initial value may be set using the decode or value options, and will 

103 be fed to the resulting object's decode method or value property, 

104 respectively.""" 

105 option = self.format(self) 

106 if decode is not None: 

107 option.decode(decode) 

108 if value is not None: 

109 option.value = value 

110 return option 

111 

112# OpaqueOption is set on formats where it is known to be used even though it is 

113# the default. This allows developers to rely on those interfaces to be stable 

114# (or at least to be notified visibly in the release notes). 

115 

116# RFC 7252 

117 

118OptionNumber.IF_MATCH.format = optiontypes.OpaqueOption 

119OptionNumber.URI_HOST.format = optiontypes.StringOption 

120OptionNumber.ETAG.format = optiontypes.OpaqueOption 

121OptionNumber.URI_PORT.format = optiontypes.UintOption 

122OptionNumber.LOCATION_PATH.format = optiontypes.StringOption 

123OptionNumber.URI_PATH.format = optiontypes.StringOption 

124OptionNumber.CONTENT_FORMAT.format = optiontypes.ContentFormatOption 

125OptionNumber.MAX_AGE.format = optiontypes.UintOption 

126OptionNumber.URI_QUERY.format = optiontypes.StringOption 

127OptionNumber.ACCEPT.format = optiontypes.ContentFormatOption 

128OptionNumber.LOCATION_QUERY.format = optiontypes.StringOption 

129OptionNumber.PROXY_URI.format = optiontypes.StringOption 

130OptionNumber.PROXY_SCHEME.format = optiontypes.StringOption 

131OptionNumber.SIZE1.format = optiontypes.UintOption 

132 

133# RFC 7959 

134 

135OptionNumber.BLOCK2.format = optiontypes.BlockOption 

136OptionNumber.BLOCK1.format = optiontypes.BlockOption 

137OptionNumber.SIZE2.format = optiontypes.UintOption 

138 

139# RFC 7641 

140 

141OptionNumber.OBSERVE.format = optiontypes.UintOption 

142 

143# RFC 7967 

144 

145OptionNumber.NO_RESPONSE.format = optiontypes.UintOption 

146 

147# RFC 8613 

148 

149OptionNumber.OBJECT_SECURITY.format = optiontypes.OpaqueOption 

150 

151# draft-ietf-core-echo-request-tag 

152 

153OptionNumber.ECHO.format = optiontypes.OpaqueOption 

154OptionNumber.REQUEST_TAG.format = optiontypes.OpaqueOption 

155 

156# experimental for draft-amsuess-core-cachable-oscore 

157 

158OptionNumber.REQUEST_HASH.format = optiontypes.OpaqueOption