From 99b5d9d5a85633c943101759b3a7f359a3cac5be Mon Sep 17 00:00:00 2001 From: Alex Budilovsky Date: Fri, 10 Oct 2014 22:40:46 -0700 Subject: [PATCH 1/6] COAP 22 some initial code error fixes --- coap/coap.py | 20 +++++++++++++------- coap/coapMessage.py | 8 +++++++- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/coap/coap.py b/coap/coap.py index 311b641..60234c0 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() 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 From 952388d3b52419b9e9e5002b2028fd3e7f951fe0 Mon Sep 17 00:00:00 2001 From: Alex Budilovsky Date: Sat, 11 Oct 2014 12:38:33 -0700 Subject: [PATCH 2/6] COAP 22 fix more errors --- coap/coapOption.py | 6 ++++-- coap/coapResource.py | 8 ++++---- coap/coapTransmitter.py | 4 ++-- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/coap/coapOption.py b/coap/coapOption.py index 97aae3a..db72a4e 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 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..8c145f8 100644 --- a/coap/coapTransmitter.py +++ b/coap/coapTransmitter.py @@ -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 From d1361bdb4820ecb4302b665fb13e3d8b4de2e71a Mon Sep 17 00:00:00 2001 From: Alex Budilovsky Date: Mon, 13 Oct 2014 21:13:23 -0700 Subject: [PATCH 3/6] python regex errors fixed --- coap/coapUri.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/coap/coapUri.py b/coap/coapUri.py index e288169..ab1e396 100644 --- a/coap/coapUri.py +++ b/coap/coapUri.py @@ -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 From 52d8916439932be59dec912974288100f3a040e0 Mon Sep 17 00:00:00 2001 From: Alex Budilovsky Date: Mon, 13 Oct 2014 21:26:01 -0700 Subject: [PATCH 4/6] COAP-22 comment change to raw type string --- coap/coapOption.py | 2 +- coap/coapTransmitter.py | 6 +++--- coap/coapUri.py | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/coap/coapOption.py b/coap/coapOption.py index db72a4e..5d99090 100644 --- a/coap/coapOption.py +++ b/coap/coapOption.py @@ -183,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/coapTransmitter.py b/coap/coapTransmitter.py index 8c145f8..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 @@ -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 ab1e396..e0cbc51 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: From 815c07213123f5174b8f021489a5a156fdea5e63 Mon Sep 17 00:00:00 2001 From: Alex Budilovsky Date: Mon, 13 Oct 2014 23:14:57 -0700 Subject: [PATCH 5/6] COAP-22 inner loop variable rename --- coap/coap.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/coap/coap.py b/coap/coap.py index 60234c0..bb7e519 100644 --- a/coap/coap.py +++ b/coap/coap.py @@ -233,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)) From 30cf81f30cb58e8dd4669ce4f7784810e3c2b412 Mon Sep 17 00:00:00 2001 From: Alex Budilovsky Date: Wed, 15 Oct 2014 23:15:31 -0700 Subject: [PATCH 6/6] unit tests now pass --- coap/coapUri.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/coap/coapUri.py b/coap/coapUri.py index e0cbc51..0bf4614 100644 --- a/coap/coapUri.py +++ b/coap/coapUri.py @@ -76,7 +76,7 @@ def uri2options(uri): # try formats: # 123.123.123.123:1234 # www.example.com:1234 - m = re.match(r'([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(r'([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