Skip to main content

C API Reference

ABI-stable C interface defined in Reader/Reader_C.h. Provides extern "C" linkage for FFI from C, C#, Python, Rust, and other languages.

note

The C API uses extern "C" linkage for ABI stability but is typically consumed from C++ source files. The examples and type signatures below use C++ syntax for namespace-qualified types (e.g. RikCommon::ReaderDefinition).

#include "Reader/Reader_C.h"

Handle Types

Opaque pointer types used to reference reader instances.

typedef struct Reader_Handle* ReaderPtr;

The ReaderPtr handle is used for all common methods and reader-specific functions.

Error Handling

Every function takes RikResult& rikResult as its first parameter. Check rikResult.HasException after each call.

RikResult

typedef struct {
bool HasException;
char ExceptionType[256];
char Message[2048];
char FileName[2048];
int LineNumber;
char FunctionName[256];

bool HasProtocolException;
char ProtocolExceptionType[256];
char ProtocolMessage[2048];
char ProtocolFileName[2048];
int ProtocolLineNumber;
char ProtocolFunctionName[256];
} RikResult;
FieldDescription
HasExceptiontrue if the call failed
ExceptionTypeException class name
MessageHuman-readable error description
FileNameSource file where the error originated
LineNumberLine number of the error
FunctionNameFunction that raised the error
HasProtocolExceptiontrue if a protocol-level error also occurred
Protocol*Matching fields for the inner protocol exception

Usage pattern:

RikResult result = {0};

Rik_Init(result, handle);

if (result.HasException) {
fprintf(stderr, "Error: %s\n", result.Message);
fprintf(stderr, " at %s:%d in %s\n",
result.FileName, result.LineNumber, result.FunctionName);

if (result.HasProtocolException) {
fprintf(stderr, "Protocol error: %s\n", result.ProtocolMessage);
}
}

Factory Functions

RikReader_Open

Opens a connection to a reader.

ReaderPtr RikReader_Open(
RikResult& rikResult,
const RikCommon::ReaderDefinition& readerDefinition,
int retryCount);
ParameterTypeDescription
rikResultRikResult&Error output
readerDefinitionconst ReaderDefinition&Reader connection parameters
retryCountintNumber of connection retry attempts

Returns: Handle to the reader instance, or NULL on failure.

Common Methods

These functions accept any ReaderPtr.

Rik_Close

Closes the reader connection and destroys the handle.

void Rik_Close(RikResult& rikResult, ReaderPtr handle);
warning

Always call Rik_Close when finished, even if earlier operations failed.

Rik_Init

Initializes the reader after opening. Must be called before any other operations.

void Rik_Init(RikResult& rikResult, ReaderPtr handle);

Rik_RefreshMetadata

Forces a refresh of cached reader metadata from the device.

void Rik_RefreshMetadata(RikResult& rikResult, ReaderPtr handle);

Rik_GetMetadataStruct

Retrieves reader metadata (part number, serial, firmware version, etc.).

void Rik_GetMetadataStruct(
RikResult& rikResult,
ReaderPtr handle,
ReaderMetadataStruct* outStruct,
bool forceRefresh = false);
ParameterTypeDescription
rikResultRikResult&Error output
handleReaderPtrReader handle
outStructReaderMetadataStruct*Output buffer for metadata
forceRefreshboolBypass cache and query device (default: false)

Reader Methods

All reader methods require a ReaderPtr handle.

RikReader_Beep

Causes the reader to beep.

void RikReader_Beep(
RikResult& rikResult,
ReaderPtr handle,
uint8_t beepCount,
uint8_t duration);
ParameterTypeDescription
beepCountuint8_tNumber of beeps
durationuint8_tBeep duration enum value

RikReader_GetCardData

Reads raw card data from the reader.

void RikReader_GetCardData(
RikResult& rikResult,
ReaderPtr handle,
uint8_t* cardDataOut,
size_t bufferLength,
uint32_t& bitCount);
ParameterTypeDescription
cardDataOutuint8_t*Output buffer for card bytes
bufferLengthsize_tSize of the output buffer
bitCountuint32_t&Output: number of valid bits read

RikReader_EnableKeystroking

Enables or disables keystroking.

void RikReader_EnableKeystroking(
RikResult& rikResult,
ReaderPtr handle,
bool enable);

RikReader_GetBeeperVolume / SetBeeperVolume

void RikReader_GetBeeperVolume(
RikResult& rikResult,
ReaderPtr handle,
uint8_t& volume);

void RikReader_SetBeeperVolume(
RikResult& rikResult,
ReaderPtr handle,
uint8_t volume);

RikReader_GetReaderConfiguration / SetReaderConfiguration

void RikReader_GetReaderConfiguration(
RikResult& rikResult,
ReaderPtr handle,
const uint8_t configNum,
ReaderConfigurationStruct& config,
ExtendedConfiguration& extConfig);

void RikReader_SetReaderConfiguration(
RikResult& rikResult,
ReaderPtr handle,
const uint8_t configNum,
const ReaderConfigurationStruct& config,
const ExtendedConfiguration& extConfig,
const HashData* hashData = nullptr);
ParameterTypeDescription
configNumuint8_tConfiguration slot number
configReaderConfigurationStruct&Reader configuration data
extConfigExtendedConfiguration&Extended configuration data
hashDataconst HashData*Optional hash data (Set only, default: nullptr)

RikReader_GetModuleState / SetModuleState

void RikReader_GetModuleState(
RikResult& rikResult,
ReaderPtr handle,
uint8_t moduleId,
uint32_t& state);

void RikReader_SetModuleState(
RikResult& rikResult,
ReaderPtr handle,
uint8_t moduleId,
uint32_t state);

RikReader_GetLedConfiguration / SetLedConfiguration

void RikReader_GetLedConfiguration(
RikResult& rikResult,
ReaderPtr handle,
uint8_t configNum,
LedConfiguration& ledConfig);

void RikReader_SetLedConfiguration(
RikResult& rikResult,
ReaderPtr handle,
uint8_t configNum,
const LedConfiguration& ledConfig);

RikReader_GetLuid / SetLuid

void RikReader_GetLuid(
RikResult& rikResult,
ReaderPtr handle,
LuidResponseInformation& luidInfo);

void RikReader_SetLuid(
RikResult& rikResult,
ReaderPtr handle,
uint16_t luid);

RikReader_GetSupportedCardTypes

void RikReader_GetSupportedCardTypes(
RikResult& rikResult,
ReaderPtr handle,
SupportedCardTypesResult* outResult);

RikReader_OnCredentialPresented

Registers a callback that fires when a card is presented to the reader. The library polls on a background thread and invokes the callback on the leading edge of card presence.

typedef void (*CredentialCallback)(const uint8_t* cardData, uint32_t bitCount);

uint32_t RikReader_OnCredentialPresented(
RikResult& rikResult,
ReaderPtr handle,
CredentialCallback callback);
ParameterTypeDescription
rikResultRikResult&Error output
handleReaderPtrReader handle
callbackCredentialCallbackFunction pointer called with card data and bit count

Returns: Subscription ID (0 on error).

RikReader_UnsubscribeCredentialCallback

Removes a previously registered credential callback.

void RikReader_UnsubscribeCredentialCallback(
RikResult& rikResult,
ReaderPtr handle,
uint32_t subscriptionId);
ParameterTypeDescription
rikResultRikResult&Error output
handleReaderPtrReader handle
subscriptionIduint32_tID returned by RikReader_OnCredentialPresented

RikReader_ReadBleConfigurationFromReader / WriteBleConfigurationToReader

void RikReader_ReadBleConfigurationFromReader(
RikResult& rikResult,
ReaderPtr handle,
uint8_t dataType,
const char* fileName);

void RikReader_WriteBleConfigurationToReader(
RikResult& rikResult,
ReaderPtr handle,
uint8_t dataType,
const char* fileName);
ParameterTypeDescription
dataTypeuint8_tBLE data type identifier
fileNameconst char*Path to the BLE configuration file

Library Info

Standalone function -- no handle required.

LibraryInfo BuildLibraryInfo();

Returns: LibraryInfo struct containing version and build information.

LibraryInfo info = BuildLibraryInfo();
printf("RIK %s\n", info.VersionString);

Examples

Open, Init, Use, Close

#include <stdio.h>
#include "Reader/Reader_C.h"

int main() {
RikResult result = {0};

// 1. Open
RikCommon::ReaderDefinition readerDef = {0};
readerDef.DeviceId.VendorId = 0x0C27;
readerDef.DeviceId.ProductId = 0x3BFA;
readerDef.ProtocolType = PROTOCOL_TYPE_FEATURE_REPORT;

ReaderPtr handle = RikReader_Open(result, readerDef, 3);
if (result.HasException) {
fprintf(stderr, "Open failed: %s\n", result.Message);
return 1;
}

// 2. Init
Rik_Init(result, handle);
if (result.HasException) {
fprintf(stderr, "Init failed: %s\n", result.Message);
Rik_Close(result, handle);
return 1;
}

// 3. Use -- read metadata
ReaderMetadataStruct metadata = {0};
Rik_GetMetadataStruct(result, handle, &metadata, false);
if (!result.HasException) {
printf("Part Number: %s\n", metadata.PartNumber);
printf("Serial: %s\n", metadata.ESN);
}

// 3. Use -- beep
RikReader_Beep(result, handle, 2, BEEP_DURATION_SHORT);

// 4. Close
Rik_Close(result, handle);
return 0;
}

Error Handling with Goto Cleanup

int configure_reader(const RikCommon::ReaderDefinition* readerDef) {
RikResult result = {0};
int rc = 0;

ReaderPtr handle = RikReader_Open(result, *readerDef, 3);
if (result.HasException) { rc = 1; goto done; }

Rik_Init(result, handle);
if (result.HasException) { rc = 1; goto cleanup; }

// Disable keystroking
RikReader_EnableKeystroking(result, handle, false);
if (result.HasException) { rc = 1; goto cleanup; }

printf("Reader configured successfully\n");

cleanup:
{
RikResult closeResult = {0};
Rik_Close(closeResult, handle);
}
done:
if (rc != 0) {
fprintf(stderr, "Error: %s\n", result.Message);
}
return rc;
}

Reading Card Data

void poll_card(ReaderPtr handle) {
RikResult result = {0};
uint8_t cardBuffer[64] = {0};
uint32_t bitCount = 0;

RikReader_GetCardData(result, handle, cardBuffer, sizeof(cardBuffer), bitCount);

if (result.HasException) {
fprintf(stderr, "Card read error: %s\n", result.Message);
return;
}

if (bitCount == 0) {
printf("No card present\n");
return;
}

printf("Card data (%u bits):", bitCount);
size_t byteCount = (bitCount + 7) / 8;
for (size_t i = 0; i < byteCount; i++) {
printf(" %02X", cardBuffer[i]);
}
printf("\n");
}

See Also