MyBconAdapter Sample
The following files are part of the MyBconAdapter Sample which illustrates the implementation of important BCON Adapter library functions and how to debug these implementations.
This sample can be found in the folder Samples/BconAdapterSample
. There is also a GNU makefile for this sample, see Makefile.
MyBconAdapterLibrary.c
#include "MyBconAdapterLogging.h"
#include <assert.h>
#include <string.h>
#include <stdlib.h>
{
SetExternalLogFunction(pTraceFunc);
}
{
}
unsigned int *apiMajorVersion,
unsigned int *apiMinorVersion,
unsigned int *adapterMajorVersion,
unsigned int *adapterMinorVersion)
{
assert(apiMajorVersion);
assert(apiMinorVersion);
assert(adapterMajorVersion);
assert(adapterMinorVersion);
adapterMajorVersion = 1;
adapterMinorVersion = 0;
}
MyBconAdapterI2CConnection.c
#include "MyBconAdapterLogging.h"
#include <string.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <linux/i2c-dev.h>
#define MAX_STRING_LENGTH 256
{
return *((int*)(&arg));
}
{
char devicePath[MAX_STRING_LENGTH] = { 0 };
strcpy(devicePath, deviceId);
char *separator = (char*)strchr(devicePath, ':');
if (separator == NULL)
{
LogOutput(TRACE_LEVEL_ERROR, "Device ID incomplete.");
}
separator[0] = '\0';
int fp = open(devicePath, O_RDWR);
if (fp <= 0)
{
LogOutput(TRACE_LEVEL_ERROR, "Could not open device.");
}
int deviceAddress = atoi(separator + 1);
if ((deviceAddress < 0) || (deviceAddress >= 128))
{
LogOutput(TRACE_LEVEL_ERROR, "Error parsing device address, only 7-bit address allowed.");
close(deviceAddress);
}
if (pDeviceAddress != NULL)
{
pDeviceAddress = deviceAddress;
}
if (phBus != NULL)
{
}
}
{
(void)deviceAddress;
int ret = close(getFd(hBus));
if (ret != 0)
{
LogOutput(TRACE_LEVEL_ERROR, "Could not close device.");
}
}
uint32_t deviceAddress,
void *pData,
size_t sizeInBytes,
size_t *pBytesRead,
uint32_t timeout_ms)
{
(void)timeout_ms;
if (ioctl(getFd(hBus), I2C_SLAVE, deviceAddress) < 0)
{
LogOutput(TRACE_LEVEL_ERROR, "Error setting target address.");
}
int bytesRead = read(getFd(hBus), pData, sizeInBytes);
if (bytesRead <= 0)
{
}
else
{
pBytesRead = bytesRead;
}
}
uint32_t deviceAddress,
const void *pData,
size_t sizeInBytes,
uint32_t timeout_ms)
{
(void)timeout_ms;
if (ioctl(getFd(hBus), I2C_SLAVE, deviceAddress) < 0)
{
LogOutput(TRACE_LEVEL_ERROR, "Error setting target address.");
}
int bytesWritten = write(getFd(hBus), pData, sizeInBytes);
if (bytesWritten != sizeInBytes)
{
}
else
{
}
}
MyBconAdapterEnumerator.c
#include "MyBconAdapterLogging.h"
#include <assert.h>
#include <string.h>
#include <stdlib.h>
{
char *env_config = getenv("BCON_ADAPTER_I2C_DEVICES");
if (env_config == NULL)
{
LogOutput(TRACE_LEVEL_ERROR, "Error reading env.var. ");
}
char *config = strdup(env_config);
if (config == NULL)
{
LogOutput(TRACE_LEVEL_ERROR, "Error out of memory. ");
}
char *token = strtok(config, " ");
do
{
LogOutput(TRACE_LEVEL_INFORMATION, "Current Token is _%s_", token);
if (callbackToBconAdapterUser != NULL)
{
BCONSTATUS callbackToBconAdapterUserStatus = callbackToBconAdapterUser(token, (userCtx));
{
LogOutput(TRACE_LEVEL_ERROR, "Error calling BCON Adapter user callback in enumeration, status = 0x%08X", callbackToBconAdapterUserStatus);
returnCode = callbackToBconAdapterUserStatus;
}
}
token = strtok(NULL, " ");
}
while (token != NULL);
free(config);
return returnCode;
}
MyBconAdapterLogging.h
#pragma once
#define TRACE_LEVEL_FATAL (BconAdapterTraceLevel_Critical)
#define TRACE_LEVEL_ERROR (BconAdapterTraceLevel_Error)
#define TRACE_LEVEL_WARNING (BconAdapterTraceLevel_Warning)
#define TRACE_LEVEL_INFORMATION (BconAdapterTraceLevel_Information)
#define TRACE_LEVEL_VERBOSE (BconAdapterTraceLevel_Verbose)
#define TRACE_LEVEL_DEBUG (BconAdapterTraceLevel_Debug)
MyBconAdapterLogging.c
#include "MyBconAdapterLogging.h"
#include <stdio.h>
#include <stdarg.h>
{
if (g_pTraceFunc == NULL)
{
return;
}
va_list argptr;
va_start(argptr, pFormat);
if (pTraceFunc != NULL)
{
pTraceFunc(level, pFormat, argptr);
}
va_end(argptr);
}
{
g_pTraceFunc = pFunc;
}
Makefile
# Makefile for BCON Adapter sample library
.PHONY: all clean
# The shared library to build
NAME := libBconAdapterMySample.so
# Installation directories for pylon
PYLON_ROOT ?= /opt/pylon5
CC ?= gcc
CPPFLAGS := -I. $(shell $(PYLON_ROOT)/bin/pylon-config --cflags-only-I)
CFLAGS := -fPIC -fvisibility=hidden -Wall
LDFLAGS := -shared
DEPS := MyBconAdapterLogging.h
OBJS := MyBconAdapterLibrary.o MyBconAdapterI2CConnection.o MyBconAdapterLogging.o MyBconAdapterEnumerator.o
# Rules for building
%.o: %.c $(DEPS)
$(CC) $(CPPFLAGS) $(CFLAGS) -c -o $@ $<
all: $(NAME)
$(NAME): $(OBJS)
$(CC) $(LDFLAGS) -o $@ $^
clean:
$(RM) $(OBJS) $(NAME)