def parseMessage(message):
    
    returnVal = {}
    
    # header
    if len(message)<4:
        raise e.messageFormatError('message to short, {0} bytes: not space for header'.format(len(message)))
    returnVal['version']     = (message[0]>>6)&0x03
    if returnVal['version']!=d.COAP_VERSION:
        raise e.messageFormatError('invalid CoAP version {0}'.format(returnVal['version']))
    returnVal['type']        = (message[0]>>4)&0x03
    if returnVal['type'] not in d.TYPE_ALL:
        raise e.messageFormatError('invalid message type {0}'.format(returnVal['type']))
    TKL  = message[0]&0x0f
    if TKL>8:
        raise e.messageFormatError('TKL too large {0}'.format(TKL))
    returnVal['code']        = message[1]
    returnVal['messageId']   = u.buf2int(message[2:4])
    message = message[4:]
    
    # token
    if len(message)<TKL:
        raise e.messageFormatError('message to short, {0} bytes: not space for token'.format(len(message)))
    returnVal['token']       = u.buf2int(message[:TKL])
    message = message[TKL:]
    
    # options
    returnVal['options']     = []
    currentOptionNumber      = 0
    while True:
        (option,message)     = o.parseOption(message,currentOptionNumber)
        if not option:
            break
        returnVal['options']+= [option]
        currentOptionNumber  = option.optionNumber
    
    # payload
    returnVal['payload']     = message
    
    log.debug('parsed message: {0}'.format(returnVal))
    
    return returnVal
dataset/ETHPy150Open openwsn-berkeley/coap/coap/coapMessage.py/parseMessage/original