diff --git a/coap/coap.py b/coap/coap.py index 311b641..bb7e519 100644 --- a/coap/coap.py +++ b/coap/coap.py @@ -58,7 +58,7 @@ def close(self): #===== client - def GET(self,uri,confirmable=True,options=[]): + def GET(self,uri,confirmable=True,options=None): log.debug('GET {0}'.format(uri)) response = self._transmit( uri = uri, @@ -69,7 +69,7 @@ def GET(self,uri,confirmable=True,options=[]): log.debug('response: {0}'.format(response)) return response['payload'] - def PUT(self,uri,confirmable=True,options=[],payload=None): + def PUT(self,uri,confirmable=True,options=None,payload=None): response = self._transmit( uri = uri, confirmable = confirmable, @@ -80,7 +80,7 @@ def PUT(self,uri,confirmable=True,options=[],payload=None): log.debug('response: {0}'.format(response)) return response['payload'] - def POST(self,uri,confirmable=True,options=[],payload=None): + def POST(self,uri,confirmable=True,options=None,payload=None): response = self._transmit( uri = uri, confirmable = confirmable, @@ -91,7 +91,7 @@ def POST(self,uri,confirmable=True,options=[],payload=None): log.debug('response: {0}'.format(response)) return response['payload'] - def DELETE(self,uri,confirmable=True,options=[]): + def DELETE(self,uri,confirmable=True,options=None): self._transmit( uri = uri, confirmable = confirmable, @@ -113,7 +113,13 @@ def addResource(self,newResource): #===== transmit - def _transmit(self,uri,confirmable,code,options=[],payload=[]): + def _transmit(self,uri,confirmable,code,options=None,payload=None): + if options is None: + options = [] + + if payload is None: + payload = [] + assert code in d.METHOD_ALL if code in [d.METHOD_GET,d.METHOD_DELETE]: assert payload==[] @@ -148,7 +154,7 @@ def _transmit(self,uri,confirmable,code,options=[],payload=[]): def _getMessageID(self,destIp,destPort): ''' - \pre transmittersLock is already acquired. + pre transmittersLock is already acquired. ''' with self.transmittersLock: self._cleanupTransmitter() @@ -166,7 +172,7 @@ def _getMessageID(self,destIp,destPort): def _getToken(self,destIp,destPort): ''' - \pre transmittersLock is already acquired. + pre transmittersLock is already acquired. ''' with self.transmittersLock: self._cleanupTransmitter() @@ -227,9 +233,9 @@ def _receive(self,timestamp,sender,rawbytes): # find resource that matches this path resource = None with self.resourceLock: - for r in self.resources: - if r.matchesPath(path): - resource = r + for _r in self.resources: + if _r.matchesPath(path): + resource = _r break log.debug('resource={0}'.format(resource)) diff --git a/coap/coapMessage.py b/coap/coapMessage.py index 3af9886..417372a 100644 --- a/coap/coapMessage.py +++ b/coap/coapMessage.py @@ -29,7 +29,13 @@ def sortOptions(options): +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ ''' -def buildMessage(msgtype,token,code,messageId,options=[],payload=[]): +def buildMessage(msgtype,token,code,messageId,options=None,payload=None): + if options is None: + options = [] + + if payload is None: + payload = [] + assert msgtype in d.TYPE_ALL assert code in d.METHOD_ALL+d.COAP_RC_ALL diff --git a/coap/coapOption.py b/coap/coapOption.py index 97aae3a..5d99090 100644 --- a/coap/coapOption.py +++ b/coap/coapOption.py @@ -129,8 +129,10 @@ def getPayloadBytes(self): class Block2(coapOption): - def __init__(self,num=None,m=None,szx=None,rawbytes=[]): - + def __init__(self,num=None,m=None,szx=None,rawbytes=None): + if rawbytes is None: + rawbytes = [] + if rawbytes: assert num==None assert m==None @@ -181,7 +183,7 @@ def getPayloadBytes(self): #============================ functions ======================================= def parseOption(message,previousOptionNumber): - ''' + r''' \brief Extract an option from the beginning of a message. \param[in] message A list of bytes. diff --git a/coap/coapResource.py b/coap/coapResource.py index 4d27464..1544dc7 100644 --- a/coap/coapResource.py +++ b/coap/coapResource.py @@ -19,16 +19,16 @@ def __init__(self,path): #======================== abstract methods ================================ - def GET(self,options=[]): + def GET(self,options=None): raise e.coapRcMethodNotAllowed() - def PUT(self,options=[],payload=None): + def PUT(self,options=None,payload=None): raise e.coapRcMethodNotAllowed() - def POST(self,options=[],payload=None): + def POST(self,options=None,payload=None): raise e.coapRcMethodNotAllowed() - def DELETE(self,options=[]): + def DELETE(self,options=None): raise e.coapRcMethodNotAllowed() #======================== public ========================================== diff --git a/coap/coapTransmitter.py b/coap/coapTransmitter.py index 4f04fd5..2d402c7 100644 --- a/coap/coapTransmitter.py +++ b/coap/coapTransmitter.py @@ -16,7 +16,7 @@ def emit(self, record): import coapMessage as m class coapTransmitter(threading.Thread): - ''' + r''' \brief A class which takes care of transmitting a CoAP message. It handles: @@ -48,7 +48,7 @@ class coapTransmitter(threading.Thread): ] def __init__(self,sendFunc,srcIp,srcPort,destIp,destPort,confirmable,messageId,code,token,options,payload,ackTimeout,respTimeout): - ''' + r''' \brief Initilizer function. This function initializes this instance by recording everything about @@ -109,8 +109,8 @@ def __init__(self,sendFunc,srcIp,srcPort,destIp,destPort,confirmable,messageId,c self.endLock = threading.Lock() # released when done communicating self.stateLock = threading.RLock() # busy setting or getting FSM state self.rxMsgEvent = threading.Event() - self.receivedACK = None - self.receivedResp = None + self.receivedACK = (None, None, None, None) + self.receivedResp = (None, None, None, None) self.coapResponse = None self.coapError = None self.state = self.STATE_INIT # current state of the FSM @@ -154,7 +154,7 @@ def __init__(self,sendFunc,srcIp,srcPort,destIp,destPort,confirmable,messageId,c #======================== public ========================================== def transmit(self): - ''' + r''' \brief Start the interaction with the destination, including waiting for transport-level ACK (if needed), waiting for an app-level response, and ACKing that (if needed) diff --git a/coap/coapUri.py b/coap/coapUri.py index e288169..0bf4614 100644 --- a/coap/coapUri.py +++ b/coap/coapUri.py @@ -14,7 +14,7 @@ def emit(self, record): import coapDefines as d def uri2options(uri): - ''' + r''' \brief Converts a coap URI into a list of CoAP options. Examples: @@ -62,13 +62,13 @@ def uri2options(uri): hostPort = uri.split('/')[0] if (not host) or (not port): # try format [aaaa::1]:1244 - m = re.match('\[([0-9a-fA-F:]+)\]:([0-9]+)',hostPort) + m = re.match(r'\[([0-9a-fA-F:]+)\]:([0-9]+)',hostPort) if m: host = m.group(1) port = int(m.group(2)) if (not host) or (not port): # try format [aaaa::1] - m = re.match('\[([0-9a-fA-F:]+)\]',hostPort) + m = re.match(r'\[([0-9a-fA-F:]+)\]',hostPort) if m: host = m.group(1) port = d.DEFAULT_UDP_PORT @@ -76,7 +76,7 @@ def uri2options(uri): # try formats: # 123.123.123.123:1234 # www.example.com:1234 - m = re.match('([0-9a-zA-Z.\-\_]+):([0-9]+)',hostPort) + m = re.match(r'([0-9a-zA-Z.\-_]+):([0-9]+)',hostPort) if m: host = m.group(1) port = int(m.group(2)) @@ -84,7 +84,7 @@ def uri2options(uri): # try formats: # 123.123.123.123 # www.example.com - m = re.match('([0-9a-zA-Z.\-\_]+)',hostPort) + m = re.match(r'([0-9a-zA-Z.\-_]+)',hostPort) if m: host = m.group(1) port = d.DEFAULT_UDP_PORT