def _query(self, observeAction): '''Runs the reader's query :param observeAction: reg (register), dereg (deregister), or None; triggers inclusion of Observe option ''' # create message msg = CoapMessage(self._hostTuple) msg.messageType = MessageType.NON msg.codeClass = CodeClass.Request msg.codeDetail = RequestCode.GET msg.messageId = random.randint(0, 65535) msg.tokenLength = 2 msg.token = (0x35, 0x61) if self._queryName == 'core': msg.addOption( CoapOption(OptionType.UriPath, '.well-known') ) msg.addOption( CoapOption(OptionType.UriPath, 'core') ) elif self._queryName == 'stats': msg.addOption( CoapOption(OptionType.UriPath, 'cli') ) msg.addOption( CoapOption(OptionType.UriPath, 'stats') ) if observeAction == 'reg': # register msg.addOption( CoapOption(OptionType.Observe, 0) ) elif observeAction == 'dereg': # deregister msg.addOption( CoapOption(OptionType.Observe, 1) ) # send message log.debug('Sending query') self._client.send(msg)
def _sendNotifResponse(self, notif, responseType): '''Sends an empty ACK or RST response to a notification :param notif: CoapMessage Observe notification from server ''' msg = CoapMessage(notif.address) msg.codeClass = CodeClass.Empty msg.codeDetail = ClientResponseCode.Empty msg.messageId = notif.messageId msg.tokenLength = 0 msg.token = None if responseType == 'reset': msg.messageType = MessageType.RST else: msg.messageType = MessageType.ACK log.debug('Sending {0} for notification response'.format(responseType)) self._client.send(msg)
def _query(self, observeAction, observePath, tokenText=None): '''Runs the reader's query. Uses a randomly generated two byte token, or the provided string encoded bytes. :param observeAction: string -- reg (register), dereg (deregister); triggers inclusion of Observe option :param observePath: string Path for register/deregister :param tokenText: string String encoding of token bytes; must by an even-numbered length of characters like '05' or '05a6' ''' # create message msg = CoapMessage(self._hostTuple) msg.messageType = MessageType.NON msg.codeClass = CodeClass.Request msg.codeDetail = RequestCode.GET msg.messageId = random.randint(0, 65535) if observePath == 'core': msg.addOption(CoapOption(OptionType.UriPath, '.well-known')) msg.addOption(CoapOption(OptionType.UriPath, 'core')) elif observePath == 'stats': msg.addOption(CoapOption(OptionType.UriPath, 'cli')) msg.addOption(CoapOption(OptionType.UriPath, 'stats')) elif observePath == 'stats2': msg.addOption(CoapOption(OptionType.UriPath, 'cli')) msg.addOption(CoapOption(OptionType.UriPath, 'stats2')) if observeAction == 'reg': # register msg.addOption(CoapOption(OptionType.Observe, 0)) if tokenText: msg.tokenLength = len(tokenText) / 2 msg.token = bytearray(msg.tokenLength) for i in range(0, msg.tokenLength): msg.token[i] = int(tokenText[2 * i:2 * (i + 1)], base=16) else: msg.tokenLength = 2 msg.token = bytearray(2) msg.token[0] = random.randint(0, 255) msg.token[1] = random.randint(0, 255) self._registeredPaths[observePath] = msg.token elif observeAction == 'dereg': # deregister msg.addOption(CoapOption(OptionType.Observe, 1)) msg.tokenLength = 2 msg.token = self._registeredPaths[observePath] # assume deregistration will succeed del self._registeredPaths[observePath] # send message log.debug('Sending query') self._client.send(msg)
def _createReplyTemplate(self, request, resource): '''Creates a reply message with common code for any reply :returns: CoapMessage Created reply ''' msg = CoapMessage() msg.address = request.address msg.tokenLength = request.tokenLength msg.token = request.token msg.codeClass = resource.resultClass msg.codeDetail = resource.resultCode if request.messageType == MessageType.CON: msg.messageType = MessageType.ACK msg.messageId = request.messageId elif request.messageType == MessageType.NON: msg.messageType = MessageType.NON msg.messageId = self._popMessageId() else: log.error('Sending Reset due to unexpected messageType: {0}', msg.messageType) msg.messageType = MessageType.RST msg.messageId = request.messageId return msg
def query(self, queryName): """Runs a named query""" # create message msg = CoapMessage(self._hostTuple) msg.messageType = MessageType.NON msg.codeClass = CodeClass.Request msg.codeDetail = RequestCode.GET msg.messageId = random.randint(0, 65535) if queryName == "core": msg.addOption(CoapOption(OptionType.UriPath, ".well-known")) msg.addOption(CoapOption(OptionType.UriPath, "core")) # send message log.debug("Sending query") self._client.send(msg)