75 lines
2.7 KiB
Python
75 lines
2.7 KiB
Python
# -*- coding: utf-8 -*-
|
|
'''
|
|
librfid - RFID EPC Class 1 Gen2 / ISO/IEC 18000-6C compliant library
|
|
|
|
Copyright (C) 2013 Mikael Ates
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU Affero General Public License as
|
|
published by the Free Software Foundation, either version 3 of the
|
|
License, or (at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU Affero General Public License for more details.
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
Functions to parse reader command response content.
|
|
'''
|
|
|
|
|
|
from _exceptions import MalformedDataError, BadParameterException
|
|
from utils import get_integer_on_two_bytes
|
|
|
|
|
|
def get_rfsettings(response):
|
|
'''
|
|
Parse response content of 'Get_RFSettings' command.
|
|
|
|
Return a list of dict describing logic ports of the reader.
|
|
'''
|
|
if not response.data:
|
|
raise MalformedDataError('Data content is malformed.')
|
|
if len(response.data) != 64:
|
|
raise MalformedDataError('Data content is malformed.')
|
|
logic_ports = list()
|
|
for i in range(0, 16):
|
|
logic_port = dict()
|
|
logic_port['ScanDelay'] = int(response.data[i * 4:(i * 4) + 2]
|
|
.encode('hex'), 16) * 10 # 0 ms ≤ ScanDelay ≤ 655350 ms ()
|
|
power_ant_nb = int(response.data[(i * 4) + 2:(i * 4) + 4]
|
|
.encode('hex'), 16)
|
|
logic_port['AntNb'] = power_ant_nb & 0x000f
|
|
logic_port['PowerAnt'] = (power_ant_nb >> 4) / 10
|
|
#[0E6h ≤ PowerAnt ≤ 136h] soit [23 dBm ≤ PowerAnt ≤ 31 dBm] (URF)
|
|
logic_ports.append(logic_port)
|
|
return logic_ports
|
|
|
|
|
|
def set_rfsettings(logic_ports):
|
|
'''
|
|
Parse response content of 'Get_RFSettings' command.
|
|
|
|
Return a string representing the data from a list of dict describing
|
|
logic ports of the reader..
|
|
'''
|
|
data = ''
|
|
i = 0
|
|
for logic_port in logic_ports:
|
|
if logic_port['ScanDelay'] > 655350 or logic_port['PowerAnt'] > 31:
|
|
raise BadParameterException('One bad parameter for logic port '
|
|
'%s: ScanDelay %s PowerAnt %s'
|
|
% (i, logic_port['ScanDelay'], logic_port['PowerAnt']))
|
|
data += get_integer_on_two_bytes(logic_port['ScanDelay'] / 10)
|
|
power_ant_nb = (logic_port['PowerAnt'] * 10) << 4
|
|
power_ant_nb |= logic_port['AntNb']
|
|
data += get_integer_on_two_bytes(power_ant_nb)
|
|
i += 1
|
|
for _ in range(i, 16):
|
|
data += '\x00\x00\x00\x00'
|
|
return data
|