//! \file i2c.c
//! \author tkl
//! \date Apr 26, 2012
//! \brief Source file of the architecture independent i2c driver.
#include <stddef.h>
#include "i2c.h"

int i2c_open(struct i2c *device)
{
	if(device == NULL) {
		return -1;
	}
	i2c_fp_open_t open = device->fp->open;
	device->cnt_subscriber++;
	return open(device->arch_dep_device);
}

int i2c_close(struct i2c *device)
{
	int ret = -1;
	if(device == NULL) {
		return -1;
	}
	i2c_fp_close_t close = device->fp->close;
	device->cnt_subscriber--;
	if(device->cnt_subscriber == 0)
		ret = close(device->arch_dep_device);
	else
		ret = 0;	/* close only if all subscribers are gone. */
	return ret;
}

//-----------------------------------------------------------------------------
int i2c_write(const struct i2c *device, char addr, const char *buffer,
		unsigned int len)
{
	if(device == NULL) {
		return -1;
	}
	i2c_fp_write_t write = device->fp->write;
	return write(device->arch_dep_device, addr, buffer, len);
}

//-----------------------------------------------------------------------------
int i2c_read(const struct i2c *device, char addr, char *buffer,
		unsigned int len)
{
	if(device == NULL) {
		return -1;
	}
	i2c_fp_read_t read = device->fp->read;
	return read(device->arch_dep_device, addr, buffer, len);
}
