Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
DanyO committed Nov 20, 2012
1 parent 5b15a1a commit a8cbc04
Show file tree
Hide file tree
Showing 20 changed files with 2,178 additions and 1 deletion.
64 changes: 64 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Compiled source #
###################
*.com
*.class
*.dll
*.exe
*.slo
*.lo
*.o
*.so
*.d
*.eep
*.elf
*.hex
*.lss
*.map
*.mk

# Python #
##########
__pycache__
*.py[cod]

# Packages #
############
# it's better to unpack these files and commit the raw source
# git has its own built in compression methods
*.7z
*.dmg
*.gz
*.iso
*.jar
*.rar
*.tar
*.zip

# Logs and databases #
######################
*.log
*.sql
*.sqlite

# OS generated files #
######################
.DS_Stor*
ehthumbs.db
Icon?
Thumbs.db

# EagleCAD #
############
*.l#*
*.b#*
*.s#*
*_PROD.*

# Others #
##########
*.tmp
Debug/
Release/
bin/
*.atsuo
*_PROD.*
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
py532lib
========

This package contains the useful stuff to communicate with a PN532 NFC chip in Python.
This package contains the useful stuff to communicate with a PN532 NFC chip in Python.

@author: DanyO <[email protected]>
@license: The source code within this file is licensed under the BSD 2 Clause license.
See LICENSE file for more information.

Built for Python3, using Quick2Wire's Python APIs for the RaspberryPI.

Losely inspired from Adafruit's PN532 I2C library.

Pull requests are welcomed.
14 changes: 14 additions & 0 deletions __init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"""@package py532lib
This package contains the useful stuff to communicate with a PN532 NFC chip in Python.
@author: DanyO <[email protected]>
@license: The source code within this file is licensed under the BSD 2 Clause license.
See LICENSE file for more information.
Built for Python3, using Quick2Wire's Python APIs for the RaspberryPI.
Losely inspired from Adafruit's PN532 I2C library.
Pull requests are welcomed.
"""
9 changes: 9 additions & 0 deletions py532lib/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Copyright (c) 2012, Dany Ouellette
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

-Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
-Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14 changes: 14 additions & 0 deletions py532lib/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"""@package py532lib
This package contains the useful stuff to communicate with a PN532 NFC chip in Python.
@author: DanyO <[email protected]>
@license: The source code within this file is licensed under the BSD 2 Clause license.
See LICENSE file for more information.
Built for Python3, using Quick2Wire's Python APIs for the RaspberryPI.
Losely inspired from Adafruit's PN532 I2C library.
Pull requests are welcomed.
"""
57 changes: 57 additions & 0 deletions py532lib/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
"""@package py532lib.constants
This module contains the constants related to py532lib.
@author: DanyO <[email protected]>
@license: The source code within this file is licensed under the BSD 2 Clause license.
See LICENSE file for more information.
"""

# RaspberryPI Constants.
RPI_DEFAULT_I2C_NEW = 0x01 # New RPi models, like the 512MB Model.
RPI_DEFAULT_I2C_OLD = 0x00 # Old RPi Models.

# Typical PN532 slave address on RPi.
PN532_I2C_SLAVE_ADDRESS = 0x24

# PN532 Commands
PN532_COMMAND_GETFIRMWAREVERSION = 0x02
PN532_COMMAND_SAMCONFIGURATION = 0x14
PN532_COMMAND_INLISTPASSIVETARGET = 0x4A

# Frame Identifiers
PN532_IDENTIFIER_HOST_TO_PN532 = 0xD4
PN532_IDENTIFIER_PN532_TO_HOST = 0xD5

# Values for PN532's SAMCONFIGURATION function.
PN532_SAMCONFIGURATION_MODE_NORMAL = 0x01
PN532_SAMCONFIGURATION_MODE_VIRTUAL_CARD = 0x02
PN532_SAMCONFIGURATION_MODE_WIRED_CARD = 0x03
PN532_SAMCONFIGURATION_MODE_DUAL_CARD = 0X04

PN532_SAMCONFIGURATION_TIMEOUT_50MS = 0x01

PN532_SAMCONFIGURATION_IRQ_OFF = 0x00
PN532_SAMCONFIGURATION_IRQ_ON = 0x01

# Typical frame values.
PN532_PREAMBLE = 0x00
PN532_START_CODE_1 = 0x00
PN532_START_CODE_2 = 0xFF
PN532_POSTAMBLE = 0x00

# Position of info within the communication's frame.
PN532_FRAME_POSITION_STATUS_CODE = 0
PN532_FRAME_POSITION_PREAMBLE = 1
PN532_FRAME_POSITION_START_CODE_1 = 2
PN532_FRAME_POSITION_START_CODE_2 = 3
PN532_FRAME_POSITION_LENGTH = 4
PN532_FRAME_POSITION_LENGTH_CHECKSUM = 5
PN532_FRAME_POSITION_FRAME_IDENTIFIER = 6
PN532_FRAME_POSITION_DATA_START = 7

# Type of frame.
PN532_FRAME_TYPE_DATA = 0
PN532_FRAME_TYPE_ACK = 1
PN532_FRAME_TYPE_NACK = 2
PN532_FRAME_TYPE_ERROR = 3
167 changes: 167 additions & 0 deletions py532lib/frame.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
"""@package py532lib.frame
This module contains classes and functions related to communication frames for the PN532 NFC Chip.
@author: DanyO <[email protected]>
@license: The source code within this file is licensed under the BSD 2 Clause license.
See LICENSE file for more information.
"""

import os
import sys
lib_path = os.path.abspath('../')
sys.path.append(lib_path)

from py532lib.i2c import *
from py532lib.frame import *
from py532lib.constants import *


class Pn532Frame:
"""Pn532Frame represents a single communication frame for
communication with the PN532 NFC Chip.
"""
def __init__(
self, frame_type=PN532_FRAME_TYPE_DATA,
preamble=PN532_PREAMBLE,
start_code_1=PN532_START_CODE_1,
start_code_2=PN532_START_CODE_2,
frame_identifier=0xD4,
data=bytearray(),
postamble=PN532_POSTAMBLE):
"""Constructor for the Pn532Frame class.
Arguments:
@param[in] frame_type Type of current frame.
(default = PN532_FRAME_TYPE_DATA)
@param[in] preamble Preamble to be used.
(default = PN532_PREAMBLE)
@param[in] start_code_1 First byte of frame's start code.
(default = PN532_START_CODE_1)
@param[in] start_code_2 Last byte of frame's start code.
(default = PN532_START_CODE_2)
@param[in] frame_identifier Frame Identifier.
(default = PN532_IDENTIFIER_HOST_TO_PN532)
@param[in] data Frame's data in a bytearray().
@param[in] postamble Postamble to be used.
(default = PN532_PREAMBLE)
"""
self._frame_type = frame_type
self._preamble = preamble
self._startCode1 = start_code_1
self._startCode2 = start_code_2
self._frameIdentifier = frame_identifier
self._data = data
self._postamble = postamble

def get_length(self):
"""Gets the frame's data length."""
return len(self._data) + 1

def get_length_checksum(self):
"""Gets the checksum of get_length()."""
return (~self.get_length() & 0xFF) + 0x01

def get_data(self):
"""Gets the frame's data."""
return self._data

def get_data_checksum(self):
"""Gets a checksum for the frame's data."""
byte_array = bytearray()

for byte in self._data:
byte_array.append(byte)

byte_array.append(self._frameIdentifier)

inverse = (~sum(byte_array) & 0xFF) + 0x01

if inverse > 255:
inverse = inverse - 255

return inverse

def get_frame_type(self):
"""Gets the frame's type."""
return self._frame_type

def to_tuple(self):
byte_array = bytearray()

if self._frame_type == PN532_FRAME_TYPE_ACK:
byte_array.append(PN532_PREAMBLE)
byte_array.append(PN532_START_CODE_1)
byte_array.append(PN532_START_CODE_2)
byte_array.append(PN532_START_CODE_1)
byte_array.append(PN532_START_CODE_2)
byte_array.append(PN532_POSTAMBLE)

return (byte_array)

byte_array.append(self._preamble)
byte_array.append(self._startCode1)
byte_array.append(self._startCode2)
byte_array.append(self.get_length())
byte_array.append(self.get_length_checksum())
byte_array.append(self._frameIdentifier)

for byte in self._data:
byte_array.append(byte)

byte_array.append(self.get_data_checksum())
byte_array.append(self._postamble)

return (byte_array)

@staticmethod
def from_response(response):
"""Fractory that generates a Pn532Frame from a response from the PN532."""
if Pn532Frame.is_valid_response(response) is not True:
raise RuntimeError("Invalid Response")

if Pn532Frame.is_ack(response):
return Pn532Frame(frame_type=PN532_FRAME_TYPE_ACK,
frame_identifier=0x00)

response_length = response[0][PN532_FRAME_POSITION_LENGTH] + 1
data = bytearray(
response[0][PN532_FRAME_POSITION_DATA_START:PN532_FRAME_POSITION_DATA_START + response_length - 2])

return Pn532Frame(
preamble=response[0][PN532_FRAME_POSITION_PREAMBLE],
start_code_1=response[0][PN532_FRAME_POSITION_START_CODE_1],
start_code_2=response[0][PN532_FRAME_POSITION_START_CODE_2],
frame_identifier=response[0][
PN532_FRAME_POSITION_FRAME_IDENTIFIER],
data=data,
postamble=response[0][PN532_FRAME_POSITION_DATA_START + response_length + 2])

@staticmethod
def is_valid_response(response):
"""Checks if a response from the PN532 is valid."""
if (response[0][0] & 0x01) == 0x01:
if response[0][PN532_FRAME_POSITION_PREAMBLE] == PN532_PREAMBLE:
if response[0][PN532_FRAME_POSITION_START_CODE_1] == PN532_START_CODE_1:
if response[0][PN532_FRAME_POSITION_START_CODE_2] == PN532_START_CODE_2:
return True

return False

@staticmethod
def is_ack(response):
"""Checks if the response is an ACK frame."""
if response[0][PN532_FRAME_POSITION_LENGTH] == 0x00:
if response[0][PN532_FRAME_POSITION_LENGTH_CHECKSUM] == 0xFF:
if response[0][PN532_FRAME_POSITION_FRAME_IDENTIFIER] == 0x00:
return True

return False
Loading

0 comments on commit a8cbc04

Please sign in to comment.