/* samsungtransport.c generated by valac 0.14.2, the Vala compiler
 * generated from samsungtransport.vala, do not modify */

/*
 * Copyright (C) 2011 Simon Busch <morphis@gravedo.de>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.

 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.

 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 *
 */

#include <glib.h>
#include <glib-object.h>
#include <fsotransport.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <fsobasics.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <samsung_modem_ctl.h>


#define FSO_GSM_TYPE_SAMSUNG_MODEM_TRANSPORT (fso_gsm_samsung_modem_transport_get_type ())
#define FSO_GSM_SAMSUNG_MODEM_TRANSPORT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), FSO_GSM_TYPE_SAMSUNG_MODEM_TRANSPORT, FsoGsmSamsungModemTransport))
#define FSO_GSM_SAMSUNG_MODEM_TRANSPORT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), FSO_GSM_TYPE_SAMSUNG_MODEM_TRANSPORT, FsoGsmSamsungModemTransportClass))
#define FSO_GSM_IS_SAMSUNG_MODEM_TRANSPORT(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), FSO_GSM_TYPE_SAMSUNG_MODEM_TRANSPORT))
#define FSO_GSM_IS_SAMSUNG_MODEM_TRANSPORT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), FSO_GSM_TYPE_SAMSUNG_MODEM_TRANSPORT))
#define FSO_GSM_SAMSUNG_MODEM_TRANSPORT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), FSO_GSM_TYPE_SAMSUNG_MODEM_TRANSPORT, FsoGsmSamsungModemTransportClass))

typedef struct _FsoGsmSamsungModemTransport FsoGsmSamsungModemTransport;
typedef struct _FsoGsmSamsungModemTransportClass FsoGsmSamsungModemTransportClass;
typedef struct _FsoGsmSamsungModemTransportPrivate FsoGsmSamsungModemTransportPrivate;
#define _g_free0(var) (var = (g_free (var), NULL))

struct _FsoGsmSamsungModemTransport {
	FsoFrameworkBaseTransport parent_instance;
	FsoGsmSamsungModemTransportPrivate * priv;
};

struct _FsoGsmSamsungModemTransportClass {
	FsoFrameworkBaseTransportClass parent_class;
};


static gpointer fso_gsm_samsung_modem_transport_parent_class = NULL;
static GType fso_gsm_samsung_modem_transport_type_id = 0;

GType fso_gsm_samsung_modem_transport_get_type (void) G_GNUC_CONST;
GType fso_gsm_samsung_modem_transport_register_type (GTypeModule * module);
enum  {
	FSO_GSM_SAMSUNG_MODEM_TRANSPORT_DUMMY_PROPERTY
};
#define FSO_GSM_SAMSUNG_MODEM_TRANSPORT_MAX_BUFFER_SIZE ((guint) 0x1000)
FsoGsmSamsungModemTransport* fso_gsm_samsung_modem_transport_new (const gchar* portname);
FsoGsmSamsungModemTransport* fso_gsm_samsung_modem_transport_construct (GType object_type, const gchar* portname);
static gboolean fso_gsm_samsung_modem_transport_real_open (FsoFrameworkTransport* base);
static gssize fso_gsm_samsung_modem_transport_real__real_write (FsoFrameworkBaseTransport* base, gint fd, void* data, gint len);
static gssize fso_gsm_samsung_modem_transport_real__real_read (FsoFrameworkBaseTransport* base, gint fd, void* data, gint len);
static gint fso_gsm_samsung_modem_transport_real_writeAndRead (FsoFrameworkTransport* base, guchar* wdata, gint wlength, guchar* rdata, gint rlength, gint maxWait);
static gchar* fso_gsm_samsung_modem_transport_real_repr (FsoFrameworkBaseTransport* base);
static void fso_gsm_samsung_modem_transport_real_configure (FsoFrameworkBaseTransport* base);
static gboolean fso_gsm_samsung_modem_transport_real_suspend (FsoFrameworkTransport* base);
static void fso_gsm_samsung_modem_transport_real_resume (FsoFrameworkTransport* base);


FsoGsmSamsungModemTransport* fso_gsm_samsung_modem_transport_construct (GType object_type, const gchar* portname) {
	FsoGsmSamsungModemTransport * self = NULL;
	const gchar* _tmp0_;
	g_return_val_if_fail (portname != NULL, NULL);
	_tmp0_ = portname;
	self = (FsoGsmSamsungModemTransport*) fso_framework_base_transport_construct (object_type, _tmp0_, (guint) 0, TRUE, TRUE);
	fso_framework_transport_setBuffered ((FsoFrameworkTransport*) self, FALSE);
	return self;
}


FsoGsmSamsungModemTransport* fso_gsm_samsung_modem_transport_new (const gchar* portname) {
	return fso_gsm_samsung_modem_transport_construct (FSO_GSM_TYPE_SAMSUNG_MODEM_TRANSPORT, portname);
}


static gboolean fso_gsm_samsung_modem_transport_real_open (FsoFrameworkTransport* base) {
	FsoGsmSamsungModemTransport * self;
	gboolean result = FALSE;
	const gchar* _tmp0_;
	gint _tmp1_ = 0;
	gint _tmp2_;
	gboolean _tmp9_ = FALSE;
	self = (FsoGsmSamsungModemTransport*) base;
	_tmp0_ = ((FsoFrameworkBaseTransport*) self)->name;
	_tmp1_ = open (_tmp0_, (O_RDWR | O_NOCTTY) | O_NONBLOCK, (mode_t) 0);
	((FsoFrameworkBaseTransport*) self)->fd = _tmp1_;
	_tmp2_ = ((FsoFrameworkBaseTransport*) self)->fd;
	if (_tmp2_ == (-1)) {
		FsoFrameworkLogger* _tmp3_;
		const gchar* _tmp4_;
		gint _tmp5_;
		const gchar* _tmp6_ = NULL;
		gchar* _tmp7_ = NULL;
		gchar* _tmp8_;
		_tmp3_ = ((FsoFrameworkTransport*) self)->logger;
		_tmp4_ = ((FsoFrameworkBaseTransport*) self)->name;
		_tmp5_ = errno;
		_tmp6_ = strerror (_tmp5_);
		_tmp7_ = g_strdup_printf ("could not open %s: %s", _tmp4_, _tmp6_);
		_tmp8_ = _tmp7_;
		fso_framework_logger_warning (_tmp3_, _tmp8_);
		_g_free0 (_tmp8_);
		result = FALSE;
		return result;
	}
	fso_framework_base_transport_configure ((FsoFrameworkBaseTransport*) self);
	_tmp9_ = FSO_FRAMEWORK_TRANSPORT_CLASS (fso_gsm_samsung_modem_transport_parent_class)->open ((FsoFrameworkTransport*) FSO_FRAMEWORK_BASE_TRANSPORT (self));
	result = _tmp9_;
	return result;
}


static gssize fso_gsm_samsung_modem_transport_real__real_write (FsoFrameworkBaseTransport* base, gint fd, void* data, gint len) {
	FsoGsmSamsungModemTransport * self;
	gssize result = 0L;
	gint _tmp0_;
	void* _tmp1_;
	gint _tmp2_ = 0;
	gint rc;
	FsoFrameworkLogger* _tmp3_;
	gint _tmp4_;
	gchar* _tmp5_ = NULL;
	gchar* _tmp6_;
	gchar* _tmp7_ = NULL;
	gchar* _tmp8_;
	gboolean _tmp9_ = FALSE;
	gint _tmp10_;
	gint _tmp16_;
	self = (FsoGsmSamsungModemTransport*) base;
	_tmp0_ = fd;
	_tmp1_ = data;
	_tmp2_ = ioctl (_tmp0_, (gint) IOCTL_MODEM_SEND, _tmp1_);
	rc = _tmp2_;
	_tmp3_ = ((FsoFrameworkTransport*) self)->logger;
	_tmp4_ = len;
	_tmp5_ = g_strdup_printf ("%i", _tmp4_);
	_tmp6_ = _tmp5_;
	_tmp7_ = g_strconcat ("Send request to modem with size = ", _tmp6_, NULL);
	_tmp8_ = _tmp7_;
	_tmp9_ = fso_framework_logger_debug (_tmp3_, _tmp8_);
	g_assert (_tmp9_);
	_g_free0 (_tmp8_);
	_g_free0 (_tmp6_);
	_tmp10_ = rc;
	if (_tmp10_ < 0) {
		FsoFrameworkLogger* _tmp11_;
		gint _tmp12_;
		const gchar* _tmp13_ = NULL;
		gchar* _tmp14_ = NULL;
		gchar* _tmp15_;
		_tmp11_ = ((FsoFrameworkTransport*) self)->logger;
		_tmp12_ = errno;
		_tmp13_ = strerror (_tmp12_);
		_tmp14_ = g_strdup_printf ("Can't issue IOCTL_MODEM_SEND ioctl to modem dev node: %s", _tmp13_);
		_tmp15_ = _tmp14_;
		fso_framework_logger_error (_tmp11_, _tmp15_);
		_g_free0 (_tmp15_);
		result = (gssize) 0;
		return result;
	}
	_tmp16_ = len;
	result = (gssize) _tmp16_;
	return result;
}


static gssize fso_gsm_samsung_modem_transport_real__real_read (FsoFrameworkBaseTransport* base, gint fd, void* data, gint len) {
	FsoGsmSamsungModemTransport * self;
	gssize result = 0L;
	gint _tmp0_;
	gint _tmp2_;
	void* _tmp3_;
	gint _tmp4_ = 0;
	gint rc;
	gint _tmp5_;
	gint _tmp11_;
	self = (FsoGsmSamsungModemTransport*) base;
	_tmp0_ = len;
	if (((guint) _tmp0_) < FSO_GSM_SAMSUNG_MODEM_TRANSPORT_MAX_BUFFER_SIZE) {
		FsoFrameworkLogger* _tmp1_;
		_tmp1_ = ((FsoFrameworkTransport*) self)->logger;
		fso_framework_logger_warning (_tmp1_, "Can't receive modem response as read buffer is too small!");
		result = (gssize) 0;
		return result;
	}
	_tmp2_ = fd;
	_tmp3_ = data;
	_tmp4_ = ioctl (_tmp2_, (gint) IOCTL_MODEM_RECV, _tmp3_);
	rc = _tmp4_;
	_tmp5_ = rc;
	if (_tmp5_ < 0) {
		FsoFrameworkLogger* _tmp6_;
		gint _tmp7_;
		const gchar* _tmp8_ = NULL;
		gchar* _tmp9_ = NULL;
		gchar* _tmp10_;
		_tmp6_ = ((FsoFrameworkTransport*) self)->logger;
		_tmp7_ = errno;
		_tmp8_ = strerror (_tmp7_);
		_tmp9_ = g_strdup_printf ("Can't issue IOCTL_MODEM_RECV ioctl to modem dev node: %s", _tmp8_);
		_tmp10_ = _tmp9_;
		fso_framework_logger_error (_tmp6_, _tmp10_);
		_g_free0 (_tmp10_);
		result = (gssize) 0;
		return result;
	}
	_tmp11_ = len;
	result = (gssize) _tmp11_;
	return result;
}


static gint fso_gsm_samsung_modem_transport_real_writeAndRead (FsoFrameworkTransport* base, guchar* wdata, gint wlength, guchar* rdata, gint rlength, gint maxWait) {
	FsoGsmSamsungModemTransport * self;
	gint result = 0;
	self = (FsoGsmSamsungModemTransport*) base;
	result = 0;
	return result;
}


static gchar* fso_gsm_samsung_modem_transport_real_repr (FsoFrameworkBaseTransport* base) {
	FsoGsmSamsungModemTransport * self;
	gchar* result = NULL;
	const gchar* _tmp0_;
	gint _tmp1_;
	gchar* _tmp2_ = NULL;
	self = (FsoGsmSamsungModemTransport*) base;
	_tmp0_ = ((FsoFrameworkBaseTransport*) self)->name;
	_tmp1_ = ((FsoFrameworkBaseTransport*) self)->fd;
	_tmp2_ = g_strdup_printf ("<Samsung %s (fd %d)>", _tmp0_, _tmp1_);
	result = _tmp2_;
	return result;
}


/**
     * We override the configure method here to be sure no configure options are set by
     * anyone.
     **/
static void fso_gsm_samsung_modem_transport_real_configure (FsoFrameworkBaseTransport* base) {
	FsoGsmSamsungModemTransport * self;
	self = (FsoGsmSamsungModemTransport*) base;
}


/**
     * This will suspend the transport. After it is suspend we can't send any more bytes
     * to the remote side.
     **/
static gboolean fso_gsm_samsung_modem_transport_real_suspend (FsoFrameworkTransport* base) {
	FsoGsmSamsungModemTransport * self;
	gboolean result = FALSE;
	FsoFrameworkLogger* _tmp0_;
	gboolean _tmp1_ = FALSE;
	self = (FsoGsmSamsungModemTransport*) base;
	_tmp0_ = ((FsoFrameworkTransport*) self)->logger;
	_tmp1_ = fso_framework_logger_debug (_tmp0_, "Successfully suspended the transport!");
	g_assert (_tmp1_);
	result = TRUE;
	return result;
}


/**
     * Resume the transport so we can send and receive our bytes again.
     **/
static void fso_gsm_samsung_modem_transport_real_resume (FsoFrameworkTransport* base) {
	FsoGsmSamsungModemTransport * self;
	FsoFrameworkLogger* _tmp0_;
	gboolean _tmp1_ = FALSE;
	self = (FsoGsmSamsungModemTransport*) base;
	_tmp0_ = ((FsoFrameworkTransport*) self)->logger;
	_tmp1_ = fso_framework_logger_debug (_tmp0_, "Successfully resumed transport!");
	g_assert (_tmp1_);
}


static void fso_gsm_samsung_modem_transport_class_init (FsoGsmSamsungModemTransportClass * klass) {
	fso_gsm_samsung_modem_transport_parent_class = g_type_class_peek_parent (klass);
	FSO_FRAMEWORK_TRANSPORT_CLASS (klass)->open = fso_gsm_samsung_modem_transport_real_open;
	FSO_FRAMEWORK_BASE_TRANSPORT_CLASS (klass)->_real_write = fso_gsm_samsung_modem_transport_real__real_write;
	FSO_FRAMEWORK_BASE_TRANSPORT_CLASS (klass)->_real_read = fso_gsm_samsung_modem_transport_real__real_read;
	FSO_FRAMEWORK_TRANSPORT_CLASS (klass)->writeAndRead = fso_gsm_samsung_modem_transport_real_writeAndRead;
	FSO_FRAMEWORK_BASE_TRANSPORT_CLASS (klass)->repr = fso_gsm_samsung_modem_transport_real_repr;
	FSO_FRAMEWORK_BASE_TRANSPORT_CLASS (klass)->configure = fso_gsm_samsung_modem_transport_real_configure;
	FSO_FRAMEWORK_TRANSPORT_CLASS (klass)->suspend = fso_gsm_samsung_modem_transport_real_suspend;
	FSO_FRAMEWORK_TRANSPORT_CLASS (klass)->resume = fso_gsm_samsung_modem_transport_real_resume;
}


static void fso_gsm_samsung_modem_transport_instance_init (FsoGsmSamsungModemTransport * self) {
}


GType fso_gsm_samsung_modem_transport_get_type (void) {
	return fso_gsm_samsung_modem_transport_type_id;
}


GType fso_gsm_samsung_modem_transport_register_type (GTypeModule * module) {
	static const GTypeInfo g_define_type_info = { sizeof (FsoGsmSamsungModemTransportClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) fso_gsm_samsung_modem_transport_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (FsoGsmSamsungModemTransport), 0, (GInstanceInitFunc) fso_gsm_samsung_modem_transport_instance_init, NULL };
	fso_gsm_samsung_modem_transport_type_id = g_type_module_register_type (module, FSO_FRAMEWORK_TYPE_BASE_TRANSPORT, "FsoGsmSamsungModemTransport", &g_define_type_info, 0);
	return fso_gsm_samsung_modem_transport_type_id;
}



