<s> import logging <EOL> class NullHandler ( logging . Handler ) : <EOL> def emit ( self , record ) : <EOL> pass <EOL> log = logging . getLogger ( '<STR_LIT>' ) <EOL> log . setLevel ( logging . ERROR ) <EOL> log . addHandler ( NullHandler ( ) ) <EOL> import coapOption as o <EOL> import coapUtils as u <EOL> import coapException as e <EOL> import coapDefines as d <EOL> def sortOptions ( options ) : <EOL> return options <EOL> '''<STR_LIT>''' <EOL> def buildMessage ( msgtype , token , code , messageId , options = [ ] , payload = [ ] ) : <EOL> assert msgtype in d . TYPE_ALL <EOL> assert code in d . METHOD_ALL + d . COAP_RC_ALL <EOL> message = [ ] <EOL> TKL = None <EOL> for tokenLen in range ( <NUM_LIT:1> , <NUM_LIT:8> + <NUM_LIT:1> ) : <EOL> if token < ( <NUM_LIT:1> << ( <NUM_LIT:8> * tokenLen ) ) : <EOL> TKL = tokenLen <EOL> break <EOL> if not TKL : <EOL> raise ValueError ( '<STR_LIT>' . format ( token ) ) <EOL> message += [ d . COAP_VERSION << <NUM_LIT:6> | msgtype << <NUM_LIT:4> | TKL ] <EOL> message += [ code ] <EOL> message += u . int2buf ( messageId , <NUM_LIT:2> ) <EOL> message += u . int2buf ( token , TKL ) <EOL> options = sortOptions ( options ) <EOL> lastOptionNum = <NUM_LIT:0> <EOL> for option in options : <EOL> assert option . optionNumber >= lastOptionNum <EOL> message += option . toBytes ( lastOptionNum ) <EOL> lastOptionNum = option . optionNumber <EOL> if payload : <EOL> message += [ d . COAP_PAYLOAD_MARKER ] <EOL> message += payload <EOL> return message <EOL> def parseMessage ( message ) : <EOL> returnVal = { } <EOL> if len ( message ) < <NUM_LIT:4> : <EOL> raise e . messageFormatError ( '<STR_LIT>' . format ( len ( message ) ) ) <EOL> returnVal [ '<STR_LIT:version>' ] = ( message [ <NUM_LIT:0> ] >> <NUM_LIT:6> ) & <NUM_LIT> <EOL> if returnVal [ '<STR_LIT:version>' ] != d . COAP_VERSION : <EOL> raise e . messageFormatError ( '<STR_LIT>' . format ( returnVal [ '<STR_LIT:version>' ] ) ) <EOL> returnVal [ '<STR_LIT:type>' ] = ( message [ <NUM_LIT:0> ] >> <NUM_LIT:4> ) & <NUM_LIT> <EOL> if returnVal [ '<STR_LIT:type>' ] not in d . TYPE_ALL : <EOL> raise e . messageFormatError ( '<STR_LIT>' . format ( returnVal [ '<STR_LIT:type>' ] ) ) <EOL> TKL = message [ <NUM_LIT:0> ] & <NUM_LIT> <EOL> if TKL > <NUM_LIT:8> : <EOL> raise e . messageFormatError ( '<STR_LIT>' . format ( TKL ) ) <EOL> returnVal [ '<STR_LIT:code>' ] = message [ <NUM_LIT:1> ] <EOL> returnVal [ '<STR_LIT>' ] = u . buf2int ( message [ <NUM_LIT:2> : <NUM_LIT:4> ] ) <EOL> message = message [ <NUM_LIT:4> : ] <EOL> if len ( message ) < TKL : <EOL> raise e . messageFormatError ( '<STR_LIT>' . format ( len ( message ) ) ) <EOL> returnVal [ '<STR_LIT>' ] = u . buf2int ( message [ : TKL ] ) <EOL> message = message [ TKL : ] <EOL> returnVal [ '<STR_LIT>' ] = [ ] <EOL> currentOptionNumber = <NUM_LIT:0> <EOL> while True : <EOL> ( option , message ) = o . parseOption ( message , currentOptionNumber ) <EOL> if not option : <EOL> break <EOL> returnVal [ '<STR_LIT>' ] += [ option ] <EOL> currentOptionNumber = option . optionNumber <EOL> returnVal [ '<STR_LIT>' ] = message <EOL> log . debug ( '<STR_LIT>' . format ( returnVal ) ) <EOL> return returnVal </s>
