Skip to content

Commit 5eceb7c

Browse files
committed
Add some more docstrings
1 parent 9b33a0d commit 5eceb7c

File tree

2 files changed

+86
-28
lines changed

2 files changed

+86
-28
lines changed

scapy/contrib/automotive/uds.py

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ class UDS_DSCPR(Packet):
152152

153153
def answers(self, other):
154154
return isinstance(other, UDS_DSC) and \
155-
other.diagnosticSessionType == self.diagnosticSessionType
155+
other.diagnosticSessionType == self.diagnosticSessionType
156156

157157

158158
bind_layers(UDS, UDS_DSCPR, service=0x50)
@@ -218,7 +218,7 @@ class UDS_SAPR(Packet):
218218

219219
def answers(self, other):
220220
return isinstance(other, UDS_SA) \
221-
and other.securityAccessType == self.securityAccessType
221+
and other.securityAccessType == self.securityAccessType
222222

223223

224224
bind_layers(UDS, UDS_SAPR, service=0x67)
@@ -273,7 +273,7 @@ class UDS_CCPR(Packet):
273273

274274
def answers(self, other):
275275
return isinstance(other, UDS_CC) \
276-
and other.controlType == self.controlType
276+
and other.controlType == self.controlType
277277

278278

279279
bind_layers(UDS, UDS_CCPR, service=0x68)
@@ -429,7 +429,7 @@ class UDS_AUTHPR(Packet):
429429

430430
def answers(self, other):
431431
return isinstance(other, UDS_AUTH) \
432-
and other.subFunction == self.subFunction
432+
and other.subFunction == self.subFunction
433433

434434

435435
bind_layers(UDS, UDS_AUTHPR, service=0x69)
@@ -491,8 +491,7 @@ class UDS_ATPPR(Packet):
491491

492492
def answers(self, other):
493493
return isinstance(other, UDS_ATP) \
494-
and other.timingParameterAccessType == \
495-
self.timingParameterAccessType
494+
and other.timingParameterAccessType == self.timingParameterAccessType
496495

497496

498497
bind_layers(UDS, UDS_ATPPR, service=0xC3)
@@ -605,7 +604,7 @@ class UDS_ROEPR(Packet):
605604

606605
def answers(self, other):
607606
return isinstance(other, UDS_ROE) \
608-
and other.eventType == self.eventType
607+
and other.eventType == self.eventType
609608

610609

611610
bind_layers(UDS, UDS_ROEPR, service=0xC6)
@@ -644,7 +643,7 @@ class UDS_LCPR(Packet):
644643

645644
def answers(self, other):
646645
return isinstance(other, UDS_LC) \
647-
and other.linkControlType == self.linkControlType
646+
and other.linkControlType == self.linkControlType
648647

649648

650649
bind_layers(UDS, UDS_LCPR, service=0xC7)
@@ -673,7 +672,7 @@ class UDS_RDBIPR(Packet):
673672

674673
def answers(self, other):
675674
return isinstance(other, UDS_RDBI) \
676-
and self.dataIdentifier in other.identifiers
675+
and self.dataIdentifier in other.identifiers
677676

678677

679678
bind_layers(UDS, UDS_RDBIPR, service=0x62)
@@ -743,7 +742,7 @@ class UDS_RSDBIPR(Packet):
743742

744743
def answers(self, other):
745744
return isinstance(other, UDS_RSDBI) \
746-
and other.dataIdentifier == self.dataIdentifier
745+
and other.dataIdentifier == self.dataIdentifier
747746

748747

749748
bind_layers(UDS, UDS_RSDBIPR, service=0x64)
@@ -780,7 +779,7 @@ class UDS_RDBPIPR(Packet):
780779

781780
def answers(self, other):
782781
return isinstance(other, UDS_RDBPI) \
783-
and other.periodicDataIdentifier == self.periodicDataIdentifier
782+
and other.periodicDataIdentifier == self.periodicDataIdentifier
784783

785784

786785
bind_layers(UDS, UDS_RDBPIPR, service=0x6A)
@@ -812,7 +811,7 @@ class UDS_DDDIPR(Packet):
812811

813812
def answers(self, other):
814813
return isinstance(other, UDS_DDDI) \
815-
and other.subFunction == self.subFunction
814+
and other.subFunction == self.subFunction
816815

817816

818817
bind_layers(UDS, UDS_DDDIPR, service=0x6C)
@@ -839,7 +838,7 @@ class UDS_WDBIPR(Packet):
839838

840839
def answers(self, other):
841840
return isinstance(other, UDS_WDBI) \
842-
and other.dataIdentifier == self.dataIdentifier
841+
and other.dataIdentifier == self.dataIdentifier
843842

844843

845844
bind_layers(UDS, UDS_WDBIPR, service=0x6E)
@@ -900,8 +899,8 @@ class UDS_WMBAPR(Packet):
900899

901900
def answers(self, other):
902901
return isinstance(other, UDS_WMBA) \
903-
and other.memorySizeLen == self.memorySizeLen \
904-
and other.memoryAddressLen == self.memoryAddressLen
902+
and other.memorySizeLen == self.memorySizeLen \
903+
and other.memoryAddressLen == self.memoryAddressLen
905904

906905

907906
bind_layers(UDS, UDS_WMBAPR, service=0x7D)
@@ -1013,8 +1012,10 @@ class UDS_RDTCI(Packet):
10131012
class DTC(Packet):
10141013
name = 'DTC and status record'
10151014
fields_desc = [
1016-
BitEnumField("system", 0, 2, {0: "Powertrain", 1: "Chassis", 2: "Body", 3: "Network"}),
1017-
BitEnumField("type", 0, 2, {0: "Generic", 1: "ManufacturerSpecific", 2: "Generic", 3: "Generic"}),
1015+
BitEnumField("system", 0, 2, {
1016+
0: "Powertrain", 1: "Chassis", 2: "Body", 3: "Network"}),
1017+
BitEnumField("type", 0, 2, {
1018+
0: "Generic", 1: "ManufacturerSpecific", 2: "Generic", 3: "Generic"}),
10181019
BitField("numeric_value_code", 0, 12),
10191020
ByteField("additional_information_code", 0),
10201021
FlagsField("status", 0, 8, UDS_RDTCI.dtcStatus)
@@ -1028,7 +1029,8 @@ class UDS_RDTCIPR(Packet):
10281029
name = 'ReadDTCInformationPositiveResponse'
10291030
fields_desc = [
10301031
ByteEnumField('reportType', 0, UDS_RDTCI.reportTypes),
1031-
ConditionalField(FlagsField('DTCStatusAvailabilityMask', 0, 8, UDS_RDTCI.dtcStatus),
1032+
ConditionalField(FlagsField('DTCStatusAvailabilityMask', 0, 8,
1033+
UDS_RDTCI.dtcStatus),
10321034
lambda pkt: pkt.reportType in [0x01, 0x07, 0x11,
10331035
0x12, 0x02, 0x0A,
10341036
0x0B, 0x0C, 0x0D,
@@ -1056,7 +1058,7 @@ class UDS_RDTCIPR(Packet):
10561058

10571059
def answers(self, other):
10581060
return isinstance(other, UDS_RDTCI) \
1059-
and other.reportType == self.reportType
1061+
and other.reportType == self.reportType
10601062

10611063

10621064
bind_layers(UDS, UDS_RDTCIPR, service=0x59)
@@ -1091,8 +1093,8 @@ class UDS_RCPR(Packet):
10911093

10921094
def answers(self, other):
10931095
return isinstance(other, UDS_RC) \
1094-
and other.routineControlType == self.routineControlType \
1095-
and other.routineIdentifier == self.routineIdentifier
1096+
and other.routineControlType == self.routineControlType \
1097+
and other.routineIdentifier == self.routineIdentifier
10961098

10971099

10981100
bind_layers(UDS, UDS_RCPR, service=0x71)
@@ -1211,7 +1213,7 @@ class UDS_TDPR(Packet):
12111213

12121214
def answers(self, other):
12131215
return isinstance(other, UDS_TD) \
1214-
and other.blockSequenceCounter == self.blockSequenceCounter
1216+
and other.blockSequenceCounter == self.blockSequenceCounter
12151217

12161218

12171219
bind_layers(UDS, UDS_TDPR, service=0x76)
@@ -1351,7 +1353,7 @@ class UDS_IOCBIPR(Packet):
13511353

13521354
def answers(self, other):
13531355
return isinstance(other, UDS_IOCBI) \
1354-
and other.dataIdentifier == self.dataIdentifier
1356+
and other.dataIdentifier == self.dataIdentifier
13551357

13561358

13571359
bind_layers(UDS, UDS_IOCBIPR, service=0x6F)
@@ -1416,8 +1418,8 @@ class UDS_NR(Packet):
14161418

14171419
def answers(self, other):
14181420
return self.requestServiceId == other.service and \
1419-
(self.negativeResponseCode != 0x78 or
1420-
conf.contribs['UDS']['treat-response-pending-as-answer'])
1421+
(self.negativeResponseCode != 0x78 or
1422+
conf.contribs['UDS']['treat-response-pending-as-answer'])
14211423

14221424

14231425
bind_layers(UDS, UDS_NR, service=0x7f)

scapy/supersocket.py

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,13 @@ def __init__(self,
102102

103103
def send(self, x):
104104
# type: (Packet) -> int
105+
"""Sends a `Packet` object
106+
107+
:param x: `Packet` to be send
108+
:type x: Packet
109+
:return: Number of bytes that have been sent
110+
:rtype: int
111+
"""
105112
sx = raw(x)
106113
try:
107114
x.sent_time = time.time()
@@ -116,14 +123,29 @@ def send(self, x):
116123
if WINDOWS:
117124
def _recv_raw(self, sock, x):
118125
# type: (socket.socket, int) -> Tuple[bytes, Any, Optional[float]]
119-
"""Internal function to receive a Packet"""
126+
"""Internal function to receive a Packet.
127+
128+
:param sock: Socket object from which data are received
129+
:type sock: socket.socket
130+
:param x: Number of bytes to be received
131+
:type x: int
132+
:return: Received bytes, address information and no timestamp
133+
:rtype: Tuple[bytes, Any, Optional[float]]
134+
"""
120135
pkt, sa_ll = sock.recvfrom(x)
121136
return pkt, sa_ll, None
122137
else:
123138
def _recv_raw(self, sock, x):
124139
# type: (socket.socket, int) -> Tuple[bytes, Any, Optional[float]]
125140
"""Internal function to receive a Packet,
126141
and process ancillary data.
142+
143+
:param sock: Socket object from which data are received
144+
:type sock: socket.socket
145+
:param x: Number of bytes to be received
146+
:type x: int
147+
:return: Received bytes, address information and an optional timestamp
148+
:rtype: Tuple[bytes, Any, Optional[float]]
127149
"""
128150
timestamp = None
129151
if not self.auxdata_available:
@@ -172,11 +194,26 @@ def _recv_raw(self, sock, x):
172194

173195
def recv_raw(self, x=MTU):
174196
# type: (int) -> Tuple[Optional[Type[Packet]], Optional[bytes], Optional[float]] # noqa: E501
175-
"""Returns a tuple containing (cls, pkt_data, time)"""
197+
"""Returns a tuple containing (cls, pkt_data, time)
198+
199+
200+
:param x: Maximum number of bytes to be received, defaults to MTU
201+
:type x: int, optional
202+
:return: A tuple, consisting of a Packet type, the received data,
203+
and a timestamp
204+
:rtype: Tuple[Optional[Type[Packet]], Optional[bytes], Optional[float]]
205+
"""
176206
return conf.raw_layer, self.ins.recv(x), None
177207

178-
def recv(self, x=MTU, **kwargs):
208+
def recv(self, x=MTU, **kwargs):
179209
# type: (int, **Any) -> Optional[Packet]
210+
"""Receive a Packet according to the `basecls` of this socket
211+
212+
:param x: Maximum number of bytes to be received, defaults to MTU
213+
:type x: int, optional
214+
:return: The received `Packet` object, or None
215+
:rtype: Optional[Packet]
216+
"""
180217
cls, val, ts = self.recv_raw(x)
181218
if not val or not cls:
182219
return None
@@ -200,6 +237,8 @@ def fileno(self):
200237

201238
def close(self):
202239
# type: () -> None
240+
"""Gracefully close this socket
241+
"""
203242
if self.closed:
204243
return
205244
self.closed = True
@@ -213,6 +252,23 @@ def close(self):
213252

214253
def sr(self, *args, **kargs):
215254
# type: (Any, Any) -> Tuple[SndRcvList, PacketList]
255+
"""Send and Receive multiple packets
256+
257+
:param pkt: Packet or list of packets to be sent
258+
:type pkt: _PacketIterable
259+
:param timeout: Time in seconds for how long packets are received
260+
:type timeout: Optional[int]
261+
:param inter: Delay, between send of two packets
262+
:type inter: int
263+
:param verbose: 1, to enable verbose output, 0 to disable.
264+
:type verbose: Optional[int]
265+
:param chainCC: Forward KeyboardInterrupt, if received.
266+
:type chainCC: bool
267+
268+
:return: A tuple, consisting of two packet lists, one with
269+
answered packets, the other with unanswered packets
270+
:rtype: Tuple[SndRcvList, PacketList]
271+
"""
216272
from scapy import sendrecv
217273
ans, unans = sendrecv.sndrcv(self, *args, **kargs) # type: SndRcvList, PacketList # noqa: E501
218274
return ans, unans

0 commit comments

Comments
 (0)