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

/*
 * Copyright (C) 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 <fsobasics.h>


#define GTM601_TYPE_UNSOLICITED_RESPONSE_HANDLER (gtm601_unsolicited_response_handler_get_type ())
#define GTM601_UNSOLICITED_RESPONSE_HANDLER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTM601_TYPE_UNSOLICITED_RESPONSE_HANDLER, Gtm601UnsolicitedResponseHandler))
#define GTM601_UNSOLICITED_RESPONSE_HANDLER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTM601_TYPE_UNSOLICITED_RESPONSE_HANDLER, Gtm601UnsolicitedResponseHandlerClass))
#define GTM601_IS_UNSOLICITED_RESPONSE_HANDLER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTM601_TYPE_UNSOLICITED_RESPONSE_HANDLER))
#define GTM601_IS_UNSOLICITED_RESPONSE_HANDLER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTM601_TYPE_UNSOLICITED_RESPONSE_HANDLER))
#define GTM601_UNSOLICITED_RESPONSE_HANDLER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTM601_TYPE_UNSOLICITED_RESPONSE_HANDLER, Gtm601UnsolicitedResponseHandlerClass))

typedef struct _Gtm601UnsolicitedResponseHandler Gtm601UnsolicitedResponseHandler;
typedef struct _Gtm601UnsolicitedResponseHandlerClass Gtm601UnsolicitedResponseHandlerClass;
typedef struct _Gtm601UnsolicitedResponseHandlerPrivate Gtm601UnsolicitedResponseHandlerPrivate;

#define GTM601_TYPE_UNDERSCORE_OSIGQ (gtm601_underscore_osigq_get_type ())
#define GTM601_UNDERSCORE_OSIGQ(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTM601_TYPE_UNDERSCORE_OSIGQ, Gtm601UnderscoreOSIGQ))
#define GTM601_UNDERSCORE_OSIGQ_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTM601_TYPE_UNDERSCORE_OSIGQ, Gtm601UnderscoreOSIGQClass))
#define GTM601_IS_UNDERSCORE_OSIGQ(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTM601_TYPE_UNDERSCORE_OSIGQ))
#define GTM601_IS_UNDERSCORE_OSIGQ_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTM601_TYPE_UNDERSCORE_OSIGQ))
#define GTM601_UNDERSCORE_OSIGQ_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTM601_TYPE_UNDERSCORE_OSIGQ, Gtm601UnderscoreOSIGQClass))

typedef struct _Gtm601UnderscoreOSIGQ Gtm601UnderscoreOSIGQ;
typedef struct _Gtm601UnderscoreOSIGQClass Gtm601UnderscoreOSIGQClass;
#define _g_free0(var) (var = (g_free (var), NULL))
typedef struct _Gtm601UnderscoreOSIGQPrivate Gtm601UnderscoreOSIGQPrivate;
#define _fso_gsm_constants_unref0(var) ((var == NULL) ? NULL : (var = (fso_gsm_constants_unref (var), NULL)))
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))

struct _Gtm601UnsolicitedResponseHandler {
	FsoGsmAtUnsolicitedResponseHandler parent_instance;
	Gtm601UnsolicitedResponseHandlerPrivate * priv;
};

struct _Gtm601UnsolicitedResponseHandlerClass {
	FsoGsmAtUnsolicitedResponseHandlerClass parent_class;
	void (*dollarQCSIMSTAT) (Gtm601UnsolicitedResponseHandler* self, const gchar* prefix, const gchar* rhs);
	void (*underscoreOSIGQ) (Gtm601UnsolicitedResponseHandler* self, const gchar* prefix, const gchar* rhs);
};

struct _Gtm601UnderscoreOSIGQ {
	FsoGsmAbstractAtCommand parent_instance;
	Gtm601UnderscoreOSIGQPrivate * priv;
	gint strength;
};

struct _Gtm601UnderscoreOSIGQClass {
	FsoGsmAbstractAtCommandClass parent_class;
};


static gpointer gtm601_unsolicited_response_handler_parent_class = NULL;
static GType gtm601_unsolicited_response_handler_type_id = 0;

GType gtm601_unsolicited_response_handler_get_type (void) G_GNUC_CONST;
GType gtm601_unsolicited_response_handler_register_type (GTypeModule * module);
enum  {
	GTM601_UNSOLICITED_RESPONSE_HANDLER_DUMMY_PROPERTY
};
Gtm601UnsolicitedResponseHandler* gtm601_unsolicited_response_handler_new (void);
Gtm601UnsolicitedResponseHandler* gtm601_unsolicited_response_handler_construct (GType object_type);
void gtm601_unsolicited_response_handler_dollarQCSIMSTAT (Gtm601UnsolicitedResponseHandler* self, const gchar* prefix, const gchar* rhs);
static void _gtm601_unsolicited_response_handler_dollarQCSIMSTAT_unsolicited_response_handler_func (const gchar* prefix, const gchar* rhs, gpointer self);
void gtm601_unsolicited_response_handler_underscoreOSIGQ (Gtm601UnsolicitedResponseHandler* self, const gchar* prefix, const gchar* rhs);
static void _gtm601_unsolicited_response_handler_underscoreOSIGQ_unsolicited_response_handler_func (const gchar* prefix, const gchar* rhs, gpointer self);
static void gtm601_unsolicited_response_handler_real_dollarQCSIMSTAT (Gtm601UnsolicitedResponseHandler* self, const gchar* prefix, const gchar* rhs);
static gboolean ___lambda0_ (Gtm601UnsolicitedResponseHandler* self);
static gboolean ____lambda0__gsource_func (gpointer self);
static void gtm601_unsolicited_response_handler_real_underscoreOSIGQ (Gtm601UnsolicitedResponseHandler* self, const gchar* prefix, const gchar* rhs);
GType gtm601_underscore_osigq_get_type (void) G_GNUC_CONST;
GType gtm601_underscore_osigq_register_type (GTypeModule * module);


static void _gtm601_unsolicited_response_handler_dollarQCSIMSTAT_unsolicited_response_handler_func (const gchar* prefix, const gchar* rhs, gpointer self) {
	gtm601_unsolicited_response_handler_dollarQCSIMSTAT (self, prefix, rhs);
}


static void _gtm601_unsolicited_response_handler_underscoreOSIGQ_unsolicited_response_handler_func (const gchar* prefix, const gchar* rhs, gpointer self) {
	gtm601_unsolicited_response_handler_underscoreOSIGQ (self, prefix, rhs);
}


Gtm601UnsolicitedResponseHandler* gtm601_unsolicited_response_handler_construct (GType object_type) {
	Gtm601UnsolicitedResponseHandler * self = NULL;
	self = (Gtm601UnsolicitedResponseHandler*) fso_gsm_at_unsolicited_response_handler_construct (object_type);
	fso_gsm_base_unsolicited_response_handler_registerUrc ((FsoGsmBaseUnsolicitedResponseHandler*) self, "$QCSIMSTAT", _gtm601_unsolicited_response_handler_dollarQCSIMSTAT_unsolicited_response_handler_func, self);
	fso_gsm_base_unsolicited_response_handler_registerUrc ((FsoGsmBaseUnsolicitedResponseHandler*) self, "_OSIGQ", _gtm601_unsolicited_response_handler_underscoreOSIGQ_unsolicited_response_handler_func, self);
	return self;
}


Gtm601UnsolicitedResponseHandler* gtm601_unsolicited_response_handler_new (void) {
	return gtm601_unsolicited_response_handler_construct (GTM601_TYPE_UNSOLICITED_RESPONSE_HANDLER);
}


/**
     * GTM SIM status report: "$QCSIMSTAT: 1, SIM INIT COMPLETED"
     **/
static gboolean ___lambda0_ (Gtm601UnsolicitedResponseHandler* self) {
	gboolean result = FALSE;
	FsoGsmModem* _tmp0_;
	_tmp0_ = fso_gsm_theModem;
	fso_gsm_modem_advanceToState (_tmp0_, FSO_GSM_MODEM_STATUS_ALIVE_SIM_READY, FALSE);
	result = FALSE;
	return result;
}


static gboolean ____lambda0__gsource_func (gpointer self) {
	gboolean result;
	result = ___lambda0_ (self);
	return result;
}


static void gtm601_unsolicited_response_handler_real_dollarQCSIMSTAT (Gtm601UnsolicitedResponseHandler* self, const gchar* prefix, const gchar* rhs) {
	const gchar* _tmp0_;
	gboolean _tmp1_ = FALSE;
	g_return_if_fail (prefix != NULL);
	g_return_if_fail (rhs != NULL);
	_tmp0_ = rhs;
	_tmp1_ = g_str_has_suffix (_tmp0_, "SIM INIT COMPLETED");
	if (_tmp1_) {
		FsoGsmModem* _tmp2_;
		FsoFrameworkLogger* _tmp3_;
		_tmp2_ = fso_gsm_theModem;
		_tmp3_ = ((FsoFrameworkAbstractObject*) _tmp2_)->logger;
		fso_framework_logger_info (_tmp3_, "GTM 601 SIM now ready");
		g_timeout_add_seconds_full (G_PRIORITY_DEFAULT, (guint) 2, ____lambda0__gsource_func, g_object_ref (self), g_object_unref);
	}
}


void gtm601_unsolicited_response_handler_dollarQCSIMSTAT (Gtm601UnsolicitedResponseHandler* self, const gchar* prefix, const gchar* rhs) {
	g_return_if_fail (self != NULL);
	GTM601_UNSOLICITED_RESPONSE_HANDLER_GET_CLASS (self)->dollarQCSIMSTAT (self, prefix, rhs);
}


/**
     * GTM network signal strength report: "_OSIGQ: 8,0"
     **/
static const gchar* string_to_string (const gchar* self) {
	const gchar* result = NULL;
	g_return_val_if_fail (self != NULL, NULL);
	result = self;
	return result;
}


static void gtm601_unsolicited_response_handler_real_underscoreOSIGQ (Gtm601UnsolicitedResponseHandler* self, const gchar* prefix, const gchar* rhs) {
	FsoGsmModem* _tmp0_;
	gpointer _tmp1_ = NULL;
	Gtm601UnderscoreOSIGQ* cmd;
	Gtm601UnderscoreOSIGQ* _tmp2_;
	const gchar* _tmp3_;
	const gchar* _tmp4_ = NULL;
	const gchar* _tmp5_;
	const gchar* _tmp6_ = NULL;
	gchar* _tmp7_ = NULL;
	gchar* _tmp8_;
	FsoGsmConstantsAtResponse _tmp9_ = 0;
	gboolean _tmp10_;
	g_return_if_fail (prefix != NULL);
	g_return_if_fail (rhs != NULL);
	_tmp0_ = fso_gsm_theModem;
	_tmp1_ = fso_gsm_modem_createAtCommand (_tmp0_, GTM601_TYPE_UNDERSCORE_OSIGQ, (GBoxedCopyFunc) g_object_ref, g_object_unref, "_OSIGQ");
	cmd = (Gtm601UnderscoreOSIGQ*) _tmp1_;
	_tmp2_ = cmd;
	_tmp3_ = prefix;
	_tmp4_ = string_to_string (_tmp3_);
	_tmp5_ = rhs;
	_tmp6_ = string_to_string (_tmp5_);
	_tmp7_ = g_strconcat (_tmp4_, ": ", _tmp6_, NULL);
	_tmp8_ = _tmp7_;
	_tmp9_ = fso_gsm_abstract_at_command_validateUrc ((FsoGsmAbstractAtCommand*) _tmp2_, _tmp8_);
	_tmp10_ = _tmp9_ == FSO_GSM_CONSTANTS_AT_RESPONSE_VALID;
	_g_free0 (_tmp8_);
	if (_tmp10_) {
		FsoGsmConstants* _tmp11_ = NULL;
		FsoGsmConstants* _tmp12_;
		Gtm601UnderscoreOSIGQ* _tmp13_;
		gint _tmp14_;
		gint _tmp15_ = 0;
		gint _tmp16_;
		gint strength;
		gint _tmp17_;
		_tmp11_ = fso_gsm_constants_instance ();
		_tmp12_ = _tmp11_;
		_tmp13_ = cmd;
		_tmp14_ = _tmp13_->strength;
		_tmp15_ = fso_gsm_constants_networkSignalToPercentage (_tmp12_, _tmp14_);
		_tmp16_ = _tmp15_;
		_fso_gsm_constants_unref0 (_tmp12_);
		strength = _tmp16_;
		_tmp17_ = strength;
		fso_gsm_updateNetworkSignalStrength (_tmp17_, NULL, NULL);
	} else {
		FsoFrameworkLogger* _tmp18_;
		const gchar* _tmp19_;
		const gchar* _tmp20_ = NULL;
		gchar* _tmp21_ = NULL;
		gchar* _tmp22_;
		_tmp18_ = ((FsoFrameworkAbstractObject*) self)->logger;
		_tmp19_ = rhs;
		_tmp20_ = string_to_string (_tmp19_);
		_tmp21_ = g_strconcat ("Received invalid _OSIGQ message ", _tmp20_, ". Please report", NULL);
		_tmp22_ = _tmp21_;
		fso_framework_logger_warning (_tmp18_, _tmp22_);
		_g_free0 (_tmp22_);
	}
	_g_object_unref0 (cmd);
}


void gtm601_unsolicited_response_handler_underscoreOSIGQ (Gtm601UnsolicitedResponseHandler* self, const gchar* prefix, const gchar* rhs) {
	g_return_if_fail (self != NULL);
	GTM601_UNSOLICITED_RESPONSE_HANDLER_GET_CLASS (self)->underscoreOSIGQ (self, prefix, rhs);
}


static void gtm601_unsolicited_response_handler_class_init (Gtm601UnsolicitedResponseHandlerClass * klass) {
	gtm601_unsolicited_response_handler_parent_class = g_type_class_peek_parent (klass);
	GTM601_UNSOLICITED_RESPONSE_HANDLER_CLASS (klass)->dollarQCSIMSTAT = gtm601_unsolicited_response_handler_real_dollarQCSIMSTAT;
	GTM601_UNSOLICITED_RESPONSE_HANDLER_CLASS (klass)->underscoreOSIGQ = gtm601_unsolicited_response_handler_real_underscoreOSIGQ;
}


static void gtm601_unsolicited_response_handler_instance_init (Gtm601UnsolicitedResponseHandler * self) {
}


GType gtm601_unsolicited_response_handler_get_type (void) {
	return gtm601_unsolicited_response_handler_type_id;
}


GType gtm601_unsolicited_response_handler_register_type (GTypeModule * module) {
	static const GTypeInfo g_define_type_info = { sizeof (Gtm601UnsolicitedResponseHandlerClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) gtm601_unsolicited_response_handler_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (Gtm601UnsolicitedResponseHandler), 0, (GInstanceInitFunc) gtm601_unsolicited_response_handler_instance_init, NULL };
	gtm601_unsolicited_response_handler_type_id = g_type_module_register_type (module, FSO_GSM_TYPE_AT_UNSOLICITED_RESPONSE_HANDLER, "Gtm601UnsolicitedResponseHandler", &g_define_type_info, 0);
	return gtm601_unsolicited_response_handler_type_id;
}



