# Copyright (C) 2011 Synapse Wireless, Inc. # Subject to your agreement of the disclaimer set forth below, permission is given by Synapse Wireless, Inc. ("Synapse") to you to freely modify, redistribute or include this SNAPpy code in any program. The purpose of this code is to help you understand and learn about SNAPpy by code examples. # BY USING ALL OR ANY PORTION OF THIS SNAPPY CODE, YOU ACCEPT AND AGREE TO THE BELOW DISCLAIMER. If you do not accept or agree to the below disclaimer, then you may not use, modify, or distribute this SNAPpy code. # THE CODE IS PROVIDED UNDER THIS LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES THAT THE COVERED CODE IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE OR NON-INFRINGING. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE COVERED CODE IS WITH YOU. SHOULD ANY COVERED CODE PROVE DEFECTIVE IN ANY RESPECT, YOU (NOT THE INITIAL DEVELOPER OR ANY OTHER CONTRIBUTOR) ASSUME THE COST OF ANY NECESSARY SERVICING, REPAIR OR CORRECTION. UNDER NO CIRCUMSTANCES WILL SYNAPSE BE LIABLE TO YOU, OR ANY OTHER PERSON OR ENTITY, FOR ANY LOSS OF USE, REVENUE OR PROFIT, LOST OR DAMAGED DATA, OR OTHER COMMERCIAL OR ECONOMIC LOSS OR FOR ANY DAMAGES WHATSOEVER RELATED TO YOUR USE OR RELIANCE UPON THE SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES OR IF SUCH DAMAGES ARE FORESEEABLE. THIS DISCLAIMER OF WARRANTY AND LIABILITY CONSTITUTES AN ESSENTIAL PART OF THIS LICENSE. NO USE OF ANY COVERED CODE IS AUTHORIZED HEREUNDER EXCEPT UNDER THIS DISCLAIMER. """ Common hexadecimal utilities """ def hexNibble(nibble): '''Convert a numeric nibble 0x0-0xF to its ASCII string representation "0"-"F"''' hexStr = "0123456789ABCDEF" return hexStr[nibble & 0xF] def printHex(byte): '''print a byte in hex - input is an integer, not a string''' print hexNibble(byte >> 4), print hexNibble(byte), # no trailing CR/LF def byteToHex(byte): '''print a byte in hex - input is an integer, not a string''' upper = hexNibble(byte >> 4) lower = hexNibble(byte) return upper + lower def printHexWord(word): '''print a word in hex - input is an integer, not a string''' printHex(word >> 8) printHex(word & 0xff) def dumpHex(str): '''dump a string of bytes in hex''' count = len(str) index = 0 while index < count: printHex(ord(str[index])) index += 1 if (index & 15) == 0: print else: print ' ', print def convBinToStr(str): '''Convert a string of bytes in hex Returns "AABBCC"''' count = len(str) index = 0 bigStr = '' while index < count: bigStr += byteToHex(ord(str[index])) index += 1 return bigStr #----------------------------- Reverse Direction ------------------------ def convStrNibble(nibble): '''Convert the nibble to hex value''' numStr = "0123456789" i = 0 found = False while i < len(numStr): if nibble == numStr[i]: found = True break i+=1 if found: return (ord(nibble) - ord('0')) capStr = "ABCDEF" i = 0 found = False while i < len(capStr): if nibble == capStr[i]: found = True break i+=1 if found: return (ord(nibble) - ord('A') + 10) lwrStr = "abcdef" i = 0 found = False while i < len(lwrStr): if nibble == lwrStr[i]: found = True break i+=1 if found: return (ord(nibble) - ord('a') + 10) return None def convStrToByte(byteStr): '''Convert a byte-sized string''' msd = byteStr[0] lsd = byteStr[1] result = convStrNibble(msd) << 4 result += convStrNibble(lsd) return result def convStrToAddr(addrStr): '''Convert a regular string into a SNAP address Expects 'AABBCC' address format as input''' if len(addrStr) >= 6: byte1 = addrStr[0:2] byte2 = addrStr[2:4] byte3 = addrStr[4:6] return chr(convStrToByte(byte1)) + chr(convStrToByte(byte2)) + chr(convStrToByte(byte3)) def convStrToMac(fullMac): if len(fullMac) == 16: convMac = convStrToAddr(fullMac[0:6]) + convStrToAddr(fullMac[6:12]) + chr(convStrToByte(fullMac[12:14])) + chr(convStrToByte(fullMac[14:]))