Skip to content
This repository has been archived by the owner on Oct 12, 2023. It is now read-only.

Commit

Permalink
Merge pull request #9 from EnAccess/openpaygo-v2-1-1
Browse files Browse the repository at this point in the history
2020-10-23: v2.1.1 release
  • Loading branch information
benmod authored Oct 23, 2020
2 parents 3a4ad34 + 449f970 commit 9d1c6a6
Show file tree
Hide file tree
Showing 9 changed files with 206 additions and 29 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,18 @@ please make sure to update to the latest version before using in production.

## CHANGELOG

2019-11-15: v2.1 release
2020-10-23: v2.1.1 release
- Added tool to generate CSV files with device data; the data can then be used for factory setup and software setup
- Added a tool to flash device data onto devices in factory (from the CSV file); it is compatible with the Arduino examples of the hardware repository

2019-11-15: v2.1.0 release
- Added documentation about how to allow entry of slightly older tokens on device (unordered token entry)
- Added an example of unordered token entry (with test scenario) on the Python implementation
- Ensured compatibility of the Python code with Python v2.7+ (in addition to Python 3+ already supported).

**Note:** This version is fully retro-compatible with the v2.0, the tokens themselves do not change. Changes can be implemented on devices that wish to support unordered token entry but are not required. No change is required on servers.

2019-10-10: v2.0 release
2019-10-10: v2.0.0 release
- Improved the test suite
- Added an extra example in the implementation documentation
- Bugfix in the example server implementation leading to count not always being updated correctly
Expand Down
15 changes: 4 additions & 11 deletions decode_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,13 @@ def _count_is_valid(cls, count, last_count, value, type, used_counts):
if value == OPAYGOShared.COUNTER_SYNC_VALUE:
if count > last_count - 30:
return True
else:
return False
elif count > last_count:
return True
elif cls.MAX_UNUSED_OLDER_TOKENS > 0:
if count > last_count:
return True
elif count > last_count - cls.MAX_UNUSED_OLDER_TOKENS:
if count > last_count - cls.MAX_UNUSED_OLDER_TOKENS:
if count not in used_counts and type == OPAYGOShared.TOKEN_TYPE_ADD_TIME:
return True
return False
else:
if count > last_count:
return True
else:
return False
return False

@classmethod
def update_used_counts(cls, past_used_counts, value, new_count, type):
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
siphash
siphash==0.0.1
17 changes: 17 additions & 0 deletions tools/csv_generator/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# OpenPAYGO Token - CSV Generator

## Installation

Nothing special to do, just make sure you have Python version 3.6 or above installed and setup in your path.

## Usage

1. In a command line window, go to the csv_generator folder and run: **python openpaygo_csv_generator.py**
2. At the first run, you will be prompted to setup some parameters (e.g. manufacturer prefix, hardware model, etc.)
If you wish to change them later you can directly edit the openpaygo_csv_generator.conf file.
3. You will be prompted for the last serial number that was generated in order to avoid two devices having the same serial number.
The latest serial number from the last batch will be shown to you if it was generated on the same computer and the config file was kept.
For your first batch you can use 0.
4. You will then be prompted for the number of devices for which you want to generate the CSV for.
5. You will then find the CSV file generated in the same folder with the name "openpaygo_batch_XXX-YYY.csv"
with XXX being the first serial number in the batch and YYY the last serial number in the batch.
87 changes: 87 additions & 0 deletions tools/csv_generator/openpaygo_csv_generator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import sys
import os
import random
import csv
import codecs
import configparser

CONF_FILE = 'openpaygo_csv_generator.conf'


def raw_input():
return sys.stdin.readline().replace('\n', '')


def number_to_serial(manufacturer_prefix, number):
return manufacturer_prefix + str(number).zfill(9)


def generate_csv(serial_start, number_of_devices, config):
base_config = config['BASE_CONFIG']
# We load the config
manufacturer_prefix = base_config['manufacturer_prefix']
count = 1
time_divider = base_config['time_divider']
restricted_digit_set = base_config['restricted_digit_set']
hardware_model = base_config['hardware_model']
firmware_version = base_config['firmware_version']
filename = 'openpaygo_batch_' + number_to_serial(manufacturer_prefix, serial_start+1) + '-' + \
number_to_serial(manufacturer_prefix, serial_start+number_of_devices) + '.csv'
with open(filename, 'w') as csvfile:
device_list_csv = csv.writer(csvfile, delimiter=',')
headers = ['Serial Number', 'Starting Code', 'Key', 'Count', 'Time Divider', 'Restricted Digit Mode',
'Hardware Model', 'Firmware Version']
device_list_csv.writerow(headers)
for number in range(1, number_of_devices+1):
device_serial = number_to_serial(manufacturer_prefix, number)
starting_code = random.randint(1, 999999999)
key = codecs.encode(os.urandom(16), 'hex').decode('utf-8')
device_data = [device_serial, starting_code, key, count, time_divider, restricted_digit_set,
hardware_model, firmware_version]
device_list_csv.writerow(device_data)


def setup_conf_menu():
config = configparser.ConfigParser()
config.read(CONF_FILE)
if 'BASE_CONFIG' in config:
return # Already configured
print('--- Setup ---')
config['BASE_CONFIG'] = {}
config['BATCH_CONFIG'] = {'last_generated': 0}
base_config = config['BASE_CONFIG']
print('Enter your manufacturer serial number prefix: ')
base_config['manufacturer_prefix'] = raw_input()
print('Enter the time divider setup for your devices (Default: 1): ')
base_config['time_divider'] = raw_input()
print('Are your devices using Restricted Digit set (Y/N)? (Default: N)')
base_config['restricted_digit_set'] = str(1 if raw_input() == 'Y' else 0)
print('Enter your device hardware model: ')
base_config['hardware_model'] = raw_input()
print('Enter your device firmware version (optional): ')
base_config['firmware_version'] = raw_input()
with open(CONF_FILE, 'w') as configfile:
config.write(configfile)


def generate_csv_menu():
config = configparser.ConfigParser()
config.read(CONF_FILE)
last_generated = config['BATCH_CONFIG']['last_generated']
print('--- Batch Generation ---')
print('Enter the last serial number that you generated without prefix (Last Generated: '+str(last_generated)+'): ')
serial_start = int(raw_input())
print('Enter the number of units you want to generate serial numbers for: ')
number_of_devices = int(raw_input())
print('Generating CSV...')
generate_csv(serial_start, number_of_devices, config)
config['BATCH_CONFIG']['last_generated'] = str(serial_start+number_of_devices)
with open(CONF_FILE, 'w') as configfile:
config.write(configfile)
print('CSV generated! ')


if __name__ == '__main__':
print('--- OpenPAYGO Token - CSV Generator ---')
setup_conf_menu()
generate_csv_menu()
19 changes: 19 additions & 0 deletions tools/factory_flasher/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# OpenPAYGO Token - Factory Flashing Tool

## Installation

1. Make sure you have Python version 2.7 or above installed and setup in your path.
2. Run **pip install -r requirements.txt**
3. Make sure you have a Serial interface with the correct drivers installed on your system (e.g. FTDI USB to UART adapter)

## Usage

1. In a command line window, go to the csv_generator folder and run: **python factory_flashing_tool.py**
2. At the first run, you will be prompted to enter the Serial port you want to use (this depends)
3. You will be prompted for the CSV file you want to use for this batch. Enter the path to the CSV file
(you can drag and drop the file to the terminal window and press enter on most system)
4. You will then repeatedly be prompted for the serial numbers of the device you want to flash.
Make sure that the device is connected to the serial interface before continuing.
You can enter this serial number manually or use an HID barcode scanners if the devices have barcodes with the serial number.
Make sure the barcode scanner is configured to enter a carriage return after reading the barcode to save time.
5. Wait for the device to be flashed and continue to the next one or press Ctrl+C to quit.
71 changes: 71 additions & 0 deletions tools/factory_flasher/factory_flashing_tool.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import sys
import csv
import serial
from serial.tools import list_ports
import time
import configparser

CONF_FILE = 'factory_flashing_tool.conf'


def raw_input():
return sys.stdin.readline().replace('\n', '')


def flash_device(serial_number, starting_code, key, port, baud):
clean_key = " ".join(key[i:i+2] for i in range(0, len(key), 2))
print('Flashing device with serial number: '+serial_number+', starting_code: '+str(starting_code)+', key: '+str(clean_key))
flashed = False
with serial.Serial(port, baud, timeout=5) as uart:
uart.write(('#'+str(serial_number)+';'+str(starting_code)+';'+clean_key).encode('ascii'))
time.sleep(0.25)
flashed = True
if flashed:
print('Finished Flashing! Check that the device is correctly setup. \n')
else:
print('ERROR: Could not connect to the device, please try again! \n')


def setup_conf(config):
print('--- Setup ---')
config['BASE_CONFIG'] = {}
base_config = config['BASE_CONFIG']
print('Available UART interfaces: ')
list_ports.main()
time.sleep(1)
print('Enter the name of the UART interface to use (copy paste from list above): ')
port = raw_input()
print('Enter the baud rate to use (e.g. 9600): ')
baud = raw_input()
base_config['port'] = port
base_config['baud'] = baud
with open(CONF_FILE, 'w') as configfile:
config.write(configfile)


if __name__ == '__main__':
print('--- OpenPAYGO Token - Factory Flashing Tool ---')
print('This is meant to be used with the OpenPAYGO Arduino example or a device using the same UART protocol.')
config = configparser.ConfigParser()
config.read(CONF_FILE)
if not 'BASE_CONFIG' in config:
setup_conf(config)
base_config = config['BASE_CONFIG']
port = base_config['port']
baud = int(base_config['baud'])
print('Enter the path to the csv file you want to load: ')
csv_file = raw_input()
with open(csv_file, newline='') as csvfile:
device_list_csv = csv.reader(csvfile)
device_data_dict = {serial_number: (starting_code, key) for
serial_number, starting_code, key, c, t, r, h, f in list(device_list_csv)}
print('CSV file loaded! ')
while(1):
print('To flash a device, make sure the device is connected and enter the serial number of the device you want to flash: ')
print('You can press Ctrl+C to quit. ')
serial_number = raw_input()
this_device_data = device_data_dict.get(serial_number)
if not this_device_data:
print('ERROR: Device is not in list!')
else:
flash_device(serial_number, this_device_data[0], this_device_data[1], port, baud)
1 change: 1 addition & 0 deletions tools/factory_flasher/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pyserial==3.4
15 changes: 0 additions & 15 deletions tools/key_generator.py

This file was deleted.

0 comments on commit 9d1c6a6

Please sign in to comment.