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

/*
 * Copyright (C) 2009-2011 Michael 'Mickey' Lauer <mlauer@vanille-media.de>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.

 * This program 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 General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; 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 <fsogsm.h>
#include <stdlib.h>
#include <string.h>
#include <gio/gio.h>
#include <fsotransport.h>
#include <fsobasics.h>
#include <fsoframework.h>


#define PDP_TYPE_PPP_MUX (pdp_ppp_mux_get_type ())
#define PDP_PPP_MUX(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), PDP_TYPE_PPP_MUX, PdpPppMux))
#define PDP_PPP_MUX_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), PDP_TYPE_PPP_MUX, PdpPppMuxClass))
#define PDP_IS_PPP_MUX(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), PDP_TYPE_PPP_MUX))
#define PDP_IS_PPP_MUX_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), PDP_TYPE_PPP_MUX))
#define PDP_PPP_MUX_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), PDP_TYPE_PPP_MUX, PdpPppMuxClass))

typedef struct _PdpPppMux PdpPppMux;
typedef struct _PdpPppMuxClass PdpPppMuxClass;
typedef struct _PdpPppMuxPrivate PdpPppMuxPrivate;
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _g_free0(var) (var = (g_free (var), NULL))
typedef struct _pdp_ppp_mux_setupTransportData pdp_ppp_mux_setupTransportData;

struct _PdpPppMux {
	FsoGsmAtPdpHandler parent_instance;
	PdpPppMuxPrivate * priv;
};

struct _PdpPppMuxClass {
	FsoGsmAtPdpHandlerClass parent_class;
};

struct _PdpPppMuxPrivate {
	FsoGsmLibGsm0710muxTransport* transport;
	gint inputfd;
	gint outputfd;
};

struct _pdp_ppp_mux_setupTransportData {
	int _state_;
	GObject* _source_object_;
	GAsyncResult* _res_;
	GSimpleAsyncResult* _async_result;
	PdpPppMux* self;
	FsoGsmModem* _tmp0_;
	FsoGsmChannel* _tmp1_;
	FsoGsmChannel* _tmp2_;
	FsoFrameworkTransport* _tmp3_;
	FsoFrameworkTransport* _tmp4_;
	FsoGsmLibGsm0710muxTransport* _tmp5_;
	FsoGsmLibGsm0710muxTransport* _tmp6_;
	FsoGsmLibGsm0710muxTransport* transport;
	gint _tmp7_;
	gint _tmp8_;
};


static gpointer pdp_ppp_mux_parent_class = NULL;
static GType pdp_ppp_mux_type_id = 0;

GType pdp_ppp_mux_get_type (void) G_GNUC_CONST;
GType pdp_ppp_mux_register_type (GTypeModule * module);
#define PDP_PPP_MUX_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), PDP_TYPE_PPP_MUX, PdpPppMuxPrivate))
enum  {
	PDP_PPP_MUX_DUMMY_PROPERTY
};
#define PDP_PPP_MUX_MODULE_NAME "fsogsm.pdp_ppp_mux"
static gchar* pdp_ppp_mux_real_repr (FsoFrameworkAbstractObject* base);
static gchar** pdp_ppp_mux_real_buildCommandLine (FsoGsmAtPdpHandler* base, int* result_length1);
static void _vala_array_add1 (gchar*** array, int* length, int* size, gchar* value);
static void _vala_array_add2 (gchar*** array, int* length, int* size, gchar* value);
static void pdp_ppp_mux_real_setupTransport_data_free (gpointer _data);
static void pdp_ppp_mux_real_setupTransport (FsoGsmAtPdpHandler* base, GAsyncReadyCallback _callback_, gpointer _user_data_);
static gboolean pdp_ppp_mux_real_setupTransport_co (pdp_ppp_mux_setupTransportData* _data_);
static void pdp_ppp_mux_real_shutdownTransport (FsoGsmAtPdpHandler* base);
static gboolean pdp_ppp_mux_real_launchPppDaemon (FsoGsmAtPdpHandler* base, gchar** cmdline, int cmdline_length1);
PdpPppMux* pdp_ppp_mux_new (void);
PdpPppMux* pdp_ppp_mux_construct (GType object_type);
static void pdp_ppp_mux_finalize (GObject* obj);
gchar* fso_factory_function (FsoFrameworkSubsystem* subsystem, GError** error);
void fso_register_function (GTypeModule* module);
static void _vala_array_destroy (gpointer array, gint array_length, GDestroyNotify destroy_func);
static void _vala_array_free (gpointer array, gint array_length, GDestroyNotify destroy_func);


static gchar* pdp_ppp_mux_real_repr (FsoFrameworkAbstractObject* base) {
	PdpPppMux * self;
	gchar* result = NULL;
	gchar* _tmp0_;
	self = (PdpPppMux*) base;
	_tmp0_ = g_strdup ("<>");
	result = _tmp0_;
	return result;
}


static void _vala_array_add1 (gchar*** array, int* length, int* size, gchar* value) {
	if ((*length) == (*size)) {
		*size = (*size) ? (2 * (*size)) : 4;
		*array = g_renew (gchar*, *array, (*size) + 1);
	}
	(*array)[(*length)++] = value;
	(*array)[*length] = NULL;
}


static void _vala_array_add2 (gchar*** array, int* length, int* size, gchar* value) {
	if ((*length) == (*size)) {
		*size = (*size) ? (2 * (*size)) : 4;
		*array = g_renew (gchar*, *array, (*size) + 1);
	}
	(*array)[(*length)++] = value;
	(*array)[*length] = NULL;
}


static gchar** pdp_ppp_mux_real_buildCommandLine (FsoGsmAtPdpHandler* base, int* result_length1) {
	PdpPppMux * self;
	gchar** result = NULL;
	FsoGsmModem* _tmp0_;
	FsoGsmModemData* _tmp1_ = NULL;
	FsoGsmModemData* data;
	FsoGsmModemData* _tmp2_;
	const gchar* _tmp3_;
	gchar* _tmp4_;
	gchar** _tmp5_ = NULL;
	gchar** cmdline;
	gint cmdline_length1;
	gint _cmdline_size_;
	gchar** _tmp6_;
	gint _tmp6__length1;
	gchar* _tmp7_;
	FsoGsmModemData* _tmp8_;
	gchar** _tmp9_;
	gint _tmp9__length1;
	gchar** _tmp14_;
	gint _tmp14__length1;
	self = (PdpPppMux*) base;
	_tmp0_ = fso_gsm_theModem;
	_tmp1_ = fso_gsm_modem_data (_tmp0_);
	data = _tmp1_;
	_tmp2_ = data;
	_tmp3_ = _tmp2_->pppCommand;
	_tmp4_ = g_strdup (_tmp3_);
	_tmp5_ = g_new0 (gchar*, 1 + 1);
	_tmp5_[0] = _tmp4_;
	cmdline = _tmp5_;
	cmdline_length1 = 1;
	_cmdline_size_ = cmdline_length1;
	_tmp6_ = cmdline;
	_tmp6__length1 = cmdline_length1;
	_tmp7_ = g_strdup ("notty");
	_vala_array_add1 (&cmdline, &cmdline_length1, &_cmdline_size_, _tmp7_);
	_tmp8_ = data;
	_tmp9_ = _tmp8_->pppOptions;
	_tmp9__length1 = _tmp8_->pppOptions_length1;
	{
		gchar** option_collection = NULL;
		gint option_collection_length1 = 0;
		gint _option_collection_size_ = 0;
		gint option_it = 0;
		option_collection = _tmp9_;
		option_collection_length1 = _tmp9__length1;
		for (option_it = 0; option_it < _tmp9__length1; option_it = option_it + 1) {
			gchar* _tmp10_;
			gchar* option = NULL;
			_tmp10_ = g_strdup (option_collection[option_it]);
			option = _tmp10_;
			{
				gchar** _tmp11_;
				gint _tmp11__length1;
				const gchar* _tmp12_;
				gchar* _tmp13_;
				_tmp11_ = cmdline;
				_tmp11__length1 = cmdline_length1;
				_tmp12_ = option;
				_tmp13_ = g_strdup (_tmp12_);
				_vala_array_add2 (&cmdline, &cmdline_length1, &_cmdline_size_, _tmp13_);
				_g_free0 (option);
			}
		}
	}
	_tmp14_ = cmdline;
	_tmp14__length1 = cmdline_length1;
	if (result_length1) {
		*result_length1 = _tmp14__length1;
	}
	result = _tmp14_;
	_g_object_unref0 (data);
	return result;
}


static void pdp_ppp_mux_real_setupTransport_data_free (gpointer _data) {
	pdp_ppp_mux_setupTransportData* _data_;
	_data_ = _data;
	_g_object_unref0 (_data_->self);
	g_slice_free (pdp_ppp_mux_setupTransportData, _data_);
}


static gpointer _g_object_ref0 (gpointer self) {
	return self ? g_object_ref (self) : NULL;
}


static void pdp_ppp_mux_real_setupTransport (FsoGsmAtPdpHandler* base, GAsyncReadyCallback _callback_, gpointer _user_data_) {
	PdpPppMux * self;
	pdp_ppp_mux_setupTransportData* _data_;
	PdpPppMux* _tmp0_;
	self = (PdpPppMux*) base;
	_data_ = g_slice_new0 (pdp_ppp_mux_setupTransportData);
	_data_->_async_result = g_simple_async_result_new (G_OBJECT (self), _callback_, _user_data_, pdp_ppp_mux_real_setupTransport);
	g_simple_async_result_set_op_res_gpointer (_data_->_async_result, _data_, pdp_ppp_mux_real_setupTransport_data_free);
	_tmp0_ = _g_object_ref0 (self);
	_data_->self = _tmp0_;
	pdp_ppp_mux_real_setupTransport_co (_data_);
}


static void pdp_ppp_mux_real_setupTransport_finish (FsoGsmAtPdpHandler* base, GAsyncResult* _res_) {
	pdp_ppp_mux_setupTransportData* _data_;
	_data_ = g_simple_async_result_get_op_res_gpointer (G_SIMPLE_ASYNC_RESULT (_res_));
}


static gboolean pdp_ppp_mux_real_setupTransport_co (pdp_ppp_mux_setupTransportData* _data_) {
	switch (_data_->_state_) {
		case 0:
		goto _state_0;
		default:
		g_assert_not_reached ();
	}
	_state_0:
	_data_->_tmp0_ = fso_gsm_theModem;
	_data_->_tmp1_ = NULL;
	_data_->_tmp1_ = fso_gsm_modem_channel (_data_->_tmp0_, "data");
	_data_->_tmp2_ = _data_->_tmp1_;
	_data_->_tmp3_ = fso_framework_command_queue_get_transport ((FsoFrameworkCommandQueue*) _data_->_tmp2_);
	_data_->_tmp4_ = _data_->_tmp3_;
	_data_->_tmp5_ = _g_object_ref0 (FSO_GSM_IS_LIB_GSM0710MUX_TRANSPORT (_data_->_tmp4_) ? ((FsoGsmLibGsm0710muxTransport*) _data_->_tmp4_) : NULL);
	_data_->_tmp6_ = _data_->_tmp5_;
	_g_object_unref0 (_data_->_tmp2_);
	_data_->transport = _data_->_tmp6_;
	_data_->_tmp7_ = _data_->self->priv->inputfd;
	_data_->_tmp8_ = _data_->self->priv->outputfd;
	fso_gsm_lib_gsm0710mux_transport_startForwardingToPPP (_data_->transport, _data_->_tmp7_, _data_->_tmp8_);
	_g_object_unref0 (_data_->transport);
	if (_data_->_state_ == 0) {
		g_simple_async_result_complete_in_idle (_data_->_async_result);
	} else {
		g_simple_async_result_complete (_data_->_async_result);
	}
	g_object_unref (_data_->_async_result);
	return FALSE;
}


static void pdp_ppp_mux_real_shutdownTransport (FsoGsmAtPdpHandler* base) {
	PdpPppMux * self;
	FsoGsmModem* _tmp0_;
	FsoGsmChannel* _tmp1_ = NULL;
	FsoGsmChannel* _tmp2_;
	FsoFrameworkTransport* _tmp3_;
	FsoFrameworkTransport* _tmp4_;
	FsoGsmLibGsm0710muxTransport* _tmp5_;
	FsoGsmLibGsm0710muxTransport* _tmp6_;
	FsoGsmLibGsm0710muxTransport* transport;
	FsoGsmLibGsm0710muxTransport* _tmp7_;
	gboolean _tmp8_ = FALSE;
	self = (PdpPppMux*) base;
	_tmp0_ = fso_gsm_theModem;
	_tmp1_ = fso_gsm_modem_channel (_tmp0_, "data");
	_tmp2_ = _tmp1_;
	_tmp3_ = fso_framework_command_queue_get_transport ((FsoFrameworkCommandQueue*) _tmp2_);
	_tmp4_ = _tmp3_;
	_tmp5_ = _g_object_ref0 (FSO_GSM_IS_LIB_GSM0710MUX_TRANSPORT (_tmp4_) ? ((FsoGsmLibGsm0710muxTransport*) _tmp4_) : NULL);
	_tmp6_ = _tmp5_;
	_g_object_unref0 (_tmp2_);
	transport = _tmp6_;
	_tmp7_ = transport;
	_tmp8_ = fso_gsm_lib_gsm0710mux_transport_isForwardingToPPP (_tmp7_);
	if (_tmp8_) {
		FsoGsmLibGsm0710muxTransport* _tmp9_;
		_tmp9_ = transport;
		fso_gsm_lib_gsm0710mux_transport_stopForwardingToPPP (_tmp9_);
	}
	_g_object_unref0 (transport);
}


static gboolean pdp_ppp_mux_real_launchPppDaemon (FsoGsmAtPdpHandler* base, gchar** cmdline, int cmdline_length1) {
	PdpPppMux * self;
	gboolean result = FALSE;
	FsoFrameworkGProcessGuard* _tmp0_;
	FsoFrameworkGProcessGuard* _tmp1_;
	gchar** _tmp2_;
	gint _tmp2__length1;
	gint _tmp3_ = 0;
	gint _tmp4_ = 0;
	gboolean _tmp5_ = FALSE;
	self = (PdpPppMux*) base;
	_tmp0_ = fso_framework_gprocess_guard_new ();
	_g_object_unref0 (((FsoGsmAtPdpHandler*) self)->ppp);
	((FsoGsmAtPdpHandler*) self)->ppp = _tmp0_;
	_tmp1_ = ((FsoGsmAtPdpHandler*) self)->ppp;
	_tmp2_ = cmdline;
	_tmp2__length1 = cmdline_length1;
	_tmp5_ = fso_framework_gprocess_guard_launchWithPipes (_tmp1_, _tmp2_, _tmp2__length1, &_tmp3_, &_tmp4_);
	self->priv->inputfd = _tmp3_;
	self->priv->outputfd = _tmp4_;
	result = _tmp5_;
	return result;
}


PdpPppMux* pdp_ppp_mux_construct (GType object_type) {
	PdpPppMux * self = NULL;
	self = (PdpPppMux*) fso_gsm_at_pdp_handler_construct (object_type);
	return self;
}


PdpPppMux* pdp_ppp_mux_new (void) {
	return pdp_ppp_mux_construct (PDP_TYPE_PPP_MUX);
}


static void pdp_ppp_mux_class_init (PdpPppMuxClass * klass) {
	pdp_ppp_mux_parent_class = g_type_class_peek_parent (klass);
	g_type_class_add_private (klass, sizeof (PdpPppMuxPrivate));
	FSO_FRAMEWORK_ABSTRACT_OBJECT_CLASS (klass)->repr = pdp_ppp_mux_real_repr;
	FSO_GSM_AT_PDP_HANDLER_CLASS (klass)->buildCommandLine = pdp_ppp_mux_real_buildCommandLine;
	FSO_GSM_AT_PDP_HANDLER_CLASS (klass)->setupTransport = pdp_ppp_mux_real_setupTransport;
	FSO_GSM_AT_PDP_HANDLER_CLASS (klass)->setupTransport_finish = pdp_ppp_mux_real_setupTransport_finish;
	FSO_GSM_AT_PDP_HANDLER_CLASS (klass)->shutdownTransport = pdp_ppp_mux_real_shutdownTransport;
	FSO_GSM_AT_PDP_HANDLER_CLASS (klass)->launchPppDaemon = pdp_ppp_mux_real_launchPppDaemon;
	G_OBJECT_CLASS (klass)->finalize = pdp_ppp_mux_finalize;
}


static void pdp_ppp_mux_instance_init (PdpPppMux * self) {
	self->priv = PDP_PPP_MUX_GET_PRIVATE (self);
}


static void pdp_ppp_mux_finalize (GObject* obj) {
	PdpPppMux * self;
	self = PDP_PPP_MUX (obj);
	_g_object_unref0 (self->priv->transport);
	G_OBJECT_CLASS (pdp_ppp_mux_parent_class)->finalize (obj);
}


/**
 * @class Pdp.PppMux
 *
 * This PdpHandler uses ppp over a multiplexed line to implement the Pdp handler interface
 **/
GType pdp_ppp_mux_get_type (void) {
	return pdp_ppp_mux_type_id;
}


GType pdp_ppp_mux_register_type (GTypeModule * module) {
	static const GTypeInfo g_define_type_info = { sizeof (PdpPppMuxClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) pdp_ppp_mux_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (PdpPppMux), 0, (GInstanceInitFunc) pdp_ppp_mux_instance_init, NULL };
	pdp_ppp_mux_type_id = g_type_module_register_type (module, FSO_GSM_TYPE_AT_PDP_HANDLER, "PdpPppMux", &g_define_type_info, 0);
	return pdp_ppp_mux_type_id;
}


/**
 * This function gets called on plugin initialization time.
 * @return the name of your plugin here
 * @note that it needs to be a name in the format <subsystem>.<plugin>
 * else your module will be unloaded immediately.
 **/
gchar* fso_factory_function (FsoFrameworkSubsystem* subsystem, GError** error) {
	gchar* result = NULL;
	FsoFrameworkLogger* _tmp0_;
	gchar* _tmp1_;
	g_return_val_if_fail (subsystem != NULL, NULL);
	_tmp0_ = fso_framework_theLogger;
	fso_framework_logger_debug (_tmp0_, "pdp_ppp_mux fso_factory_function");
	_tmp1_ = g_strdup (PDP_PPP_MUX_MODULE_NAME);
	result = _tmp1_;
	return result;
}


void fso_register_function (GTypeModule* module) {
	g_return_if_fail (module != NULL);
	pdp_ppp_mux_register_type (module);
}


static void _vala_array_destroy (gpointer array, gint array_length, GDestroyNotify destroy_func) {
	if ((array != NULL) && (destroy_func != NULL)) {
		int i;
		for (i = 0; i < array_length; i = i + 1) {
			if (((gpointer*) array)[i] != NULL) {
				destroy_func (((gpointer*) array)[i]);
			}
		}
	}
}


static void _vala_array_free (gpointer array, gint array_length, GDestroyNotify destroy_func) {
	_vala_array_destroy (array, array_length, destroy_func);
	g_free (array);
}



