예제 #1
0
    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)
예제 #2
0
    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)