libmsr  dc2dc9c
Functions
msr206.c File Reference
#include <time.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <strings.h>
#include <err.h>
#include <string.h>
#include "libmsr.h"
Include dependency graph for msr206.c:

Functions

int msr_cmd (int fd, uint8_t c)
 
int msr_zeros (int fd, msr_lz_t *lz)
 Get the MSR device's current leading-zero setting. More...
 
int msr_commtest (int fd)
 Perform a communications test. More...
 
int msr_fwrev (int fd, uint8_t *buf)
 Check the device's firmware revision. More...
 
int msr_model (int fd, uint8_t *buf)
 Check the device's model. More...
 
int msr_flash_led (int fd, uint8_t led)
 Toggle the LEDs on the MSR device. More...
 
int msr_sensor_test (int fd)
 Check the device's sensor. More...
 
int msr_ram_test (int fd)
 Check the device's RAM. More...
 
int msr_get_co (int fd)
 Get the device's coercivity level. More...
 
int msr_set_hi_co (int fd)
 Set the device's coercivity to high. More...
 
int msr_set_lo_co (int fd)
 Set the device's coercivity to low. More...
 
int msr_reset (int fd)
 Reset the MSR device. More...
 
int msr_iso_read (int fd, msr_tracks_t *tracks)
 Read an ISO formatted card. More...
 
int msr_erase (int fd, uint8_t tracks)
 Erase one or more tracks on a card. More...
 
int msr_iso_write (int fd, msr_tracks_t *tracks)
 Write an ISO formatted card. More...
 
int msr_raw_read (int fd, msr_tracks_t *tracks)
 Read raw data from a card. More...
 
int msr_raw_write (int fd, msr_tracks_t *tracks)
 Write raw data to a card. More...
 
int msr_init (int fd)
 Initialize the MSR device. More...
 
int msr_set_bpi (int fd, uint8_t bpi)
 Set the MSR device's BPI value. More...
 
int msr_set_bpc (int fd, uint8_t bpc1, uint8_t bpc2, uint8_t bpc3)
 Set the MSR device's BPC value for each track. More...
 

Function Documentation

◆ msr_cmd()

int msr_cmd ( int  fd,
uint8_t  c 
)

◆ msr_commtest()

int msr_commtest ( int  fd)

Perform a communications test.

This function issues an MSR_CMD_DIAG_COMM command to the device to perform a communications diagnostic test. After issuing this command, the device will respond with an MSR_STS_COMM_OK byte if the test passes.

Parameters
fdThe device's fd.
Returns
LIBMSR_ERR_OK on success.
LIBMSR_ERR_SERIAL on serial I/O failure.
LIBMSR_ERR_DEVICE on device failure.

◆ msr_erase()

int msr_erase ( int  fd,
uint8_t  tracks 
)

Erase one or more tracks on a card.

This routine issues an MSR_CMD_ERASE command to the device to read erase a magstripe card. After the command is issued, the user must swipe a card through the MSR206. The function will block until the device returns a response code indicating that the erase operation completed successfully.

This function can erase various combinations of tracks, or erase all of them, depending on the tracks value specified:

MSR_ERASE_TK1 erase track 1 only

MSR_ERASE_TK2 erase track 2 only

MSR_ERASE_TK3 erase track 3 only

MSR_ERASE_TK1_TK2 erase tracks 1 and 2

MSR_ERASE_TK1_TK3 erase tracks 1 and 3

MSR_ERASE_TK2_TK3 erase tracks 2 and 3

MSR_ERASE_ALL erase all tracks

Parameters
fdThe device's fd.
tracksThe tracks to delete.
Returns
LIBMSR_ERR_OK on success.
LIBMSR_ERR_DEVICE on device failure.

◆ msr_flash_led()

int msr_flash_led ( int  fd,
uint8_t  led 
)

Toggle the LEDs on the MSR device.

This function is used to manually control the LEDs on the MSR206. The device has one green, one yellow and one red LED mounted in it. A given LED is controlled by specifying led as follows:

MSR_CMD_LED_GRN_ON - turn on green LED

MSR_CMD_LED_YLW_ON - turn on yellow LED

MSR_CMD_LED_RED_ON - turn on red LED

MSR_CMD_LED_OFF - turn all LEDs off

After an LED control command is issued to the device, the routine will pause for a tenth of a second to allow time for the command to be processed and the LED to light up.

Parameters
fdThe device's fd.
ledThe LED to control.
Returns
LIBMSR_ERR_OK on success.
LIBMSR_ERR_SERIAL | LIBMSR_ERR_DEVICE on error.

◆ msr_fwrev()

int msr_fwrev ( int  fd,
uint8_t *  buf 
)

Check the device's firmware revision.

This function issues an MSR_CMD_FWREV command to the device to retrieve its firmware revision code. The revision is saved in the supplied buffer, which must be at least 9 bytes.

Parameters
fdThe device's fd.
bufThe buffer to write the revision to.
Returns
LIBMSR_ERR_OK on success.
LIBMSR_ERR_SERIAL on serial I/O failure.

◆ msr_get_co()

int msr_get_co ( int  fd)

Get the device's coercivity level.

This function issues an MSR_CMD_GETCO command to retrieve the device's current coercivity setting, which is either MSR_CO_HI ('H') or MSR_CO_LO ('L').

Parameters
fdThe device's fd.
Returns
MSR_CO_HI if the device is in hi-co mode.
MSR_CO_LO if the device is in lo-co mode.
LIBMSR_ERR_DEVICE on device failure.

◆ msr_init()

int msr_init ( int  fd)

Initialize the MSR device.

This function issues a reset command to the MSR206 device, and then performs a communications diagnostic test. If the test succeeds, another reset is issued to ready the device for a read or write operation. Typically, msr_init() must be called before any significant operation, including reading and writing cards.

Parameters
fdThe device's fd.
Returns
LIBMSR_ERR_OK on success.
LIBMSR_ERR_DEVICE on device failure.

◆ msr_iso_read()

int msr_iso_read ( int  fd,
msr_tracks_t tracks 
)

Read an ISO formatted card.

This routine issues an MSR_CMD_READ command to the device to read an ISO formatted magstripe card. After the command is issued, the user must swipe a card through the MSR206. The function will block until the card is read and the MSR206 is ready to return data from the tracks. The caller must allocate a pointer to an msr_tracks_t structure and supply a pointer to this structure via the tracks argument.

Parameters
fdThe device's fd.
tracksA pointer to the msr_tracks_t to populate.
Returns
LIBMSR_ERR_OK on success.
LIBMSR_ERR_SERIAL on serial I/O failure.

◆ msr_iso_write()

int msr_iso_write ( int  fd,
msr_tracks_t tracks 
)

Write an ISO formatted card.

This routine issues an MSR_CMD_WRITE command to the device to write to an ISO formatted magstripe card. The card does not need to be erased (any existing data is overwritten). The caller must allocate an msr_tracks_t structure and populate it with the data to be written to the card, then supply a pointer to this structure via the tracks argument. The data in the tracks must meet the ISO requirements.

After the write command is issued and the track data is written to the device, the function will block until a status code is read from the device.

Parameters
fdThe device's fd.
tracksA pointer to the msr_tracks_t data to write.
Returns
LIBMSR_ERR_OK on success.
LIBMSR_ERR_DEVICE on device failure.

◆ msr_model()

int msr_model ( int  fd,
uint8_t *  buf 
)

Check the device's model.

This function issues an MSR_CMD_MODEL command to the device to retrieve its model code. The model code is saved in the supplied buffer, which must be at least 10 bytes.

Parameters
fdThe device's fd.
bufThe buffer to write the model to.
Returns
LIBMSR_ERR_OK on success.
LIBMSR_ERR_DEVICE on device failure.

◆ msr_ram_test()

int msr_ram_test ( int  fd)

Check the device's RAM.

This function issues an MSR_CMD_DIAG_RAM command to perform a diagnostic sense on the MSR206's internal RAM. If the RAM checks good, the device will return a status code of MSR_STS_RAM_OK.

Parameters
fdThe device's fd.
Returns
LIBMSR_ERR_OK on success.
LIBMSR_ERR_DEVICE on device failure.

◆ msr_raw_read()

int msr_raw_read ( int  fd,
msr_tracks_t tracks 
)

Read raw data from a card.

This routine issues an MSR_CMD_RAW_READ command to the device to read arbitrary data from 3 tracks on a magstripe card. After the command is issued, the user must swipe a card through the MSR206. The function will block until the card is read and the MSR206 is ready to return data from the tracks. The caller must allocate a pointer to an msr_tracks_t structure and supply a pointer to this structure via the tracks argument.

Unlike the msr_iso_read() function, this routine bypasses the MSR206's internal data parser and returns data representing the raw bit pattern on the magnetic media. It is up to the caller to decode this data into a useful form.

Parameters
fdThe device's fd.
tracksA pointer to the msr_tracks_t to populate.
Returns
LIBMSR_ERR_OK on success.
LIBMSR_ERR_DEVICE on device failure.

◆ msr_raw_write()

int msr_raw_write ( int  fd,
msr_tracks_t tracks 
)

Write raw data to a card.

This routine issues an MSR_CMD_RAW_WRITE command to the device to write arbitrary data to a magstripe card. The card does not need to be erased (any existing data is overwritten). The caller must allocate an msr_tracks_t structure and populate it with the data to be written to the card, then supply a pointer to this structure via the tracks argument. The data can be in any format.

After the write command is issued and the track data is written to the device, the function will block until a status code is read from the device.

Unlike the msr_iso_write() routine, this function bypasses the MSR206's internal parser and writes the bit pattern represented by the track data unmodified to the card. It's up to the caller to format the data in a meaningful way.

Parameters
fdThe device's fd.
tracksA pointer to the msr_tracks_t data to write.
Returns
LIBMSR_ERR_OK on success.
LIBMSR_ERR_DEVICE on device failure.

◆ msr_reset()

int msr_reset ( int  fd)

Reset the MSR device.

This function issues an MSR_CMD_RESET command to reset the device. This command does not return a status code. The routine pauses for a tenth of a second to wait for the reset to complete.

Parameters
fdThe device's fd.
Returns
LIBMSR_ERR_OK.

◆ msr_sensor_test()

int msr_sensor_test ( int  fd)

Check the device's sensor.

This function issues an MSR_CMD_DIAG_SENSOR command to perform a diagnostic sense on the mechanical card sensor in the MSR206. After issuing the command, the user is instructed to slide a card through the reader. No actual read is performed, however the hardware tests to verify that the card sensor registers the presence of the card in the track. If the card registers correctly, the device will return a status code of MSR_STS_SENSOR_OK.

Parameters
fdThe device's fd.
Returns
LIBMSR_ERR_OK on success.
LIBMSR_ERR_DEVICE on device failure.

◆ msr_set_bpc()

int msr_set_bpc ( int  fd,
uint8_t  bpc1,
uint8_t  bpc2,
uint8_t  bpc3 
)

Set the MSR device's BPC value for each track.

This function issues an MSR_CMD_SETBPC command to bits per character configuration for all 3 tracks. (This command has no effect for other tracks.) It is assumed that this command only has meaning when using ISO read or write commands. The MSR206 supports writing data with BPC values from 5 to 8.

Parameters
fdThe device's fd.
bpc1The new BPC value for track 1.
bpc2The new BPC value for track 2.
bpc3The new BPC value for track 3.
Returns
LIBMSR_ERR_OK on success.
LIBMSR_ERR_DEVICE on device failure.

◆ msr_set_bpi()

int msr_set_bpi ( int  fd,
uint8_t  bpi 
)

Set the MSR device's BPI value.

This function issues an MSR_CMD_SETBPI command to set the bits per inch configuration for track 2. (This command has no effect for other tracks.) It is assumed that this command only has meaning when writing data. The MSR206 supports writing data on track 2 with bpi values. Valid options are either 75 or 210 bits per inch.

Parameters
fdThe device's fd.
bpiThe new BPI value.
Returns
LIBMSR_ERR_OK on success.
LIBMSR_ERR_DEVICE on device failure.

◆ msr_set_hi_co()

int msr_set_hi_co ( int  fd)

Set the device's coercivity to high.

This function issues an MSR_CMD_SETCO_HI command to switch the device to high coercivity mode. It is unclear if this affects both the read and write operations, though presumably it only affects writes by channeling more power to the write head so that it can update high-coercivity media.

Parameters
fdThe device's fd.
Returns
LIBMSR_ERR_OK on success.
LIBMSR_ERR_DEVICE on device failure.

◆ msr_set_lo_co()

int msr_set_lo_co ( int  fd)

Set the device's coercivity to low.

This function issues an MSR_CMD_SETCO_LO command to switch the device to low coercivity mode. It is unclear if this affects both the read and write operations, though presumably it only affects writes by channeling less power to the write head so that it can update low-coercivity media.

Parameters
fdThe device's fd.
Returns
LIBMSR_ERR_OK on success.
LIBMSR_ERR_DEVICE on device failure.

◆ msr_zeros()

int msr_zeros ( int  fd,
msr_lz_t lz 
)

Get the MSR device's current leading-zero setting.

The leading-zero setting is used by the device to determine how many leading zeros to write when writing ISO-formatted tracks. There are two values to the setting: one for tracks 1 and 3, and one for track 2.

Parameters
fdThe device's fd.
lzA pointer to the msr_lz_t to populate.
Returns
LIBMSR_ERR_OK