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


#include <glib.h>
#include <glib-object.h>
#include <fsogsm.h>
#include <stdlib.h>
#include <string.h>
#include <fsobasics.h>
#include <time.h>


#define QUALCOMM_HTC_TYPE_UNSOLICITED_RESPONSE_HANDLER (qualcomm_htc_unsolicited_response_handler_get_type ())
#define QUALCOMM_HTC_UNSOLICITED_RESPONSE_HANDLER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), QUALCOMM_HTC_TYPE_UNSOLICITED_RESPONSE_HANDLER, QualcommHtcUnsolicitedResponseHandler))
#define QUALCOMM_HTC_UNSOLICITED_RESPONSE_HANDLER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), QUALCOMM_HTC_TYPE_UNSOLICITED_RESPONSE_HANDLER, QualcommHtcUnsolicitedResponseHandlerClass))
#define QUALCOMM_HTC_IS_UNSOLICITED_RESPONSE_HANDLER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), QUALCOMM_HTC_TYPE_UNSOLICITED_RESPONSE_HANDLER))
#define QUALCOMM_HTC_IS_UNSOLICITED_RESPONSE_HANDLER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), QUALCOMM_HTC_TYPE_UNSOLICITED_RESPONSE_HANDLER))
#define QUALCOMM_HTC_UNSOLICITED_RESPONSE_HANDLER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), QUALCOMM_HTC_TYPE_UNSOLICITED_RESPONSE_HANDLER, QualcommHtcUnsolicitedResponseHandlerClass))

typedef struct _QualcommHtcUnsolicitedResponseHandler QualcommHtcUnsolicitedResponseHandler;
typedef struct _QualcommHtcUnsolicitedResponseHandlerClass QualcommHtcUnsolicitedResponseHandlerClass;
typedef struct _QualcommHtcUnsolicitedResponseHandlerPrivate QualcommHtcUnsolicitedResponseHandlerPrivate;

#define QUALCOMM_HTC_TYPE_PLUS_HTCCTZV (qualcomm_htc_plus_htcctzv_get_type ())
#define QUALCOMM_HTC_PLUS_HTCCTZV(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), QUALCOMM_HTC_TYPE_PLUS_HTCCTZV, QualcommHtcPlusHTCCTZV))
#define QUALCOMM_HTC_PLUS_HTCCTZV_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), QUALCOMM_HTC_TYPE_PLUS_HTCCTZV, QualcommHtcPlusHTCCTZVClass))
#define QUALCOMM_HTC_IS_PLUS_HTCCTZV(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), QUALCOMM_HTC_TYPE_PLUS_HTCCTZV))
#define QUALCOMM_HTC_IS_PLUS_HTCCTZV_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), QUALCOMM_HTC_TYPE_PLUS_HTCCTZV))
#define QUALCOMM_HTC_PLUS_HTCCTZV_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), QUALCOMM_HTC_TYPE_PLUS_HTCCTZV, QualcommHtcPlusHTCCTZVClass))

typedef struct _QualcommHtcPlusHTCCTZV QualcommHtcPlusHTCCTZV;
typedef struct _QualcommHtcPlusHTCCTZVClass QualcommHtcPlusHTCCTZVClass;
#define _g_free0(var) (var = (g_free (var), NULL))
typedef struct _QualcommHtcPlusHTCCTZVPrivate QualcommHtcPlusHTCCTZVPrivate;
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))

struct _QualcommHtcUnsolicitedResponseHandler {
	FsoGsmAtUnsolicitedResponseHandler parent_instance;
	QualcommHtcUnsolicitedResponseHandlerPrivate * priv;
};

struct _QualcommHtcUnsolicitedResponseHandlerClass {
	FsoGsmAtUnsolicitedResponseHandlerClass parent_class;
	void (*plusPB_READY) (QualcommHtcUnsolicitedResponseHandler* self, const gchar* prefix, const gchar* rhs);
	void (*plusHTCCTZV) (QualcommHtcUnsolicitedResponseHandler* self, const gchar* prefix, const gchar* rhs);
};

struct _QualcommHtcUnsolicitedResponseHandlerPrivate {
	gboolean phbReady;
	gboolean fullReady;
};

struct _QualcommHtcPlusHTCCTZV {
	FsoGsmAbstractAtCommand parent_instance;
	QualcommHtcPlusHTCCTZVPrivate * priv;
	gint year;
	gint month;
	gint day;
	gint hour;
	gint minute;
	gint second;
	gint tzoffset;
};

struct _QualcommHtcPlusHTCCTZVClass {
	FsoGsmAbstractAtCommandClass parent_class;
};


static gpointer qualcomm_htc_unsolicited_response_handler_parent_class = NULL;
static GType qualcomm_htc_unsolicited_response_handler_type_id = 0;

GType qualcomm_htc_unsolicited_response_handler_get_type (void) G_GNUC_CONST;
GType qualcomm_htc_unsolicited_response_handler_register_type (GTypeModule * module);
#define QUALCOMM_HTC_UNSOLICITED_RESPONSE_HANDLER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), QUALCOMM_HTC_TYPE_UNSOLICITED_RESPONSE_HANDLER, QualcommHtcUnsolicitedResponseHandlerPrivate))
enum  {
	QUALCOMM_HTC_UNSOLICITED_RESPONSE_HANDLER_DUMMY_PROPERTY
};
static void qualcomm_htc_unsolicited_response_handler_updateReadyness (QualcommHtcUnsolicitedResponseHandler* self);
QualcommHtcUnsolicitedResponseHandler* qualcomm_htc_unsolicited_response_handler_new (void);
QualcommHtcUnsolicitedResponseHandler* qualcomm_htc_unsolicited_response_handler_construct (GType object_type);
void qualcomm_htc_unsolicited_response_handler_plusPB_READY (QualcommHtcUnsolicitedResponseHandler* self, const gchar* prefix, const gchar* rhs);
static void _qualcomm_htc_unsolicited_response_handler_plusPB_READY_unsolicited_response_handler_func (const gchar* prefix, const gchar* rhs, gpointer self);
void qualcomm_htc_unsolicited_response_handler_plusHTCCTZV (QualcommHtcUnsolicitedResponseHandler* self, const gchar* prefix, const gchar* rhs);
static void _qualcomm_htc_unsolicited_response_handler_plusHTCCTZV_unsolicited_response_handler_func (const gchar* prefix, const gchar* rhs, gpointer self);
static void qualcomm_htc_unsolicited_response_handler_real_plusPB_READY (QualcommHtcUnsolicitedResponseHandler* self, const gchar* prefix, const gchar* rhs);
static void qualcomm_htc_unsolicited_response_handler_real_plusHTCCTZV (QualcommHtcUnsolicitedResponseHandler* self, const gchar* prefix, const gchar* rhs);
GType qualcomm_htc_plus_htcctzv_get_type (void) G_GNUC_CONST;
GType qualcomm_htc_plus_htcctzv_register_type (GTypeModule * module);
static void qualcomm_htc_unsolicited_response_handler_finalize (GObject* obj);


static void qualcomm_htc_unsolicited_response_handler_updateReadyness (QualcommHtcUnsolicitedResponseHandler* self) {
	gboolean _tmp0_;
	gboolean newFullReady;
	gboolean _tmp1_;
	gboolean _tmp2_;
	g_return_if_fail (self != NULL);
	_tmp0_ = self->priv->phbReady;
	newFullReady = _tmp0_;
	_tmp1_ = newFullReady;
	_tmp2_ = self->priv->fullReady;
	if (_tmp1_ != _tmp2_) {
		gboolean _tmp3_;
		gboolean _tmp4_;
		_tmp3_ = newFullReady;
		self->priv->fullReady = _tmp3_;
		_tmp4_ = self->priv->fullReady;
		if (_tmp4_) {
			FsoGsmModem* _tmp5_;
			FsoFrameworkLogger* _tmp6_;
			FsoGsmModem* _tmp7_;
			_tmp5_ = fso_gsm_theModem;
			_tmp6_ = ((FsoFrameworkAbstractObject*) _tmp5_)->logger;
			fso_framework_logger_info (_tmp6_, "qualcomm_htc sim ready");
			_tmp7_ = fso_gsm_theModem;
			fso_gsm_modem_advanceToState (_tmp7_, FSO_GSM_MODEM_STATUS_ALIVE_SIM_READY, FALSE);
		}
	}
}


static void _qualcomm_htc_unsolicited_response_handler_plusPB_READY_unsolicited_response_handler_func (const gchar* prefix, const gchar* rhs, gpointer self) {
	qualcomm_htc_unsolicited_response_handler_plusPB_READY (self, prefix, rhs);
}


static void _qualcomm_htc_unsolicited_response_handler_plusHTCCTZV_unsolicited_response_handler_func (const gchar* prefix, const gchar* rhs, gpointer self) {
	qualcomm_htc_unsolicited_response_handler_plusHTCCTZV (self, prefix, rhs);
}


QualcommHtcUnsolicitedResponseHandler* qualcomm_htc_unsolicited_response_handler_construct (GType object_type) {
	QualcommHtcUnsolicitedResponseHandler * self = NULL;
	self = (QualcommHtcUnsolicitedResponseHandler*) fso_gsm_at_unsolicited_response_handler_construct (object_type);
	fso_gsm_base_unsolicited_response_handler_registerUrc ((FsoGsmBaseUnsolicitedResponseHandler*) self, "+PB_READY", _qualcomm_htc_unsolicited_response_handler_plusPB_READY_unsolicited_response_handler_func, self);
	fso_gsm_base_unsolicited_response_handler_registerUrc ((FsoGsmBaseUnsolicitedResponseHandler*) self, "+HTCCTZV", _qualcomm_htc_unsolicited_response_handler_plusHTCCTZV_unsolicited_response_handler_func, self);
	return self;
}


QualcommHtcUnsolicitedResponseHandler* qualcomm_htc_unsolicited_response_handler_new (void) {
	return qualcomm_htc_unsolicited_response_handler_construct (QUALCOMM_HTC_TYPE_UNSOLICITED_RESPONSE_HANDLER);
}


/**
     * Qualcomm HTC subsystem status report:
     *
     * +PB_READY
     *
     * Sent, after the reading the SIM phonebook into internal RAM
     **/
static void qualcomm_htc_unsolicited_response_handler_real_plusPB_READY (QualcommHtcUnsolicitedResponseHandler* self, const gchar* prefix, const gchar* rhs) {
	g_return_if_fail (prefix != NULL);
	g_return_if_fail (rhs != NULL);
	self->priv->phbReady = TRUE;
	qualcomm_htc_unsolicited_response_handler_updateReadyness (self);
}


void qualcomm_htc_unsolicited_response_handler_plusPB_READY (QualcommHtcUnsolicitedResponseHandler* self, const gchar* prefix, const gchar* rhs) {
	g_return_if_fail (self != NULL);
	QUALCOMM_HTC_UNSOLICITED_RESPONSE_HANDLER_GET_CLASS (self)->plusPB_READY (self, prefix, rhs);
}


/**
     * Qualcomm HTC time report
     *
     * +HTCCTZV: "10/05/02,15:27:30+08,1"
     *
     */
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 qualcomm_htc_unsolicited_response_handler_real_plusHTCCTZV (QualcommHtcUnsolicitedResponseHandler* self, const gchar* prefix, const gchar* rhs) {
	FsoGsmModem* _tmp0_;
	gpointer _tmp1_ = NULL;
	QualcommHtcPlusHTCCTZV* htcctv;
	QualcommHtcPlusHTCCTZV* _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_, QUALCOMM_HTC_TYPE_PLUS_HTCCTZV, (GBoxedCopyFunc) g_object_ref, g_object_unref, "+HTCCTZV");
	htcctv = (QualcommHtcPlusHTCCTZV*) _tmp1_;
	_tmp2_ = htcctv;
	_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_) {
		struct tm _tmp11_ = {0};
		struct tm t;
		QualcommHtcPlusHTCCTZV* _tmp12_;
		gint _tmp13_;
		QualcommHtcPlusHTCCTZV* _tmp14_;
		gint _tmp15_;
		QualcommHtcPlusHTCCTZV* _tmp16_;
		gint _tmp17_;
		QualcommHtcPlusHTCCTZV* _tmp18_;
		gint _tmp19_;
		QualcommHtcPlusHTCCTZV* _tmp20_;
		gint _tmp21_;
		QualcommHtcPlusHTCCTZV* _tmp22_;
		gint _tmp23_;
		struct tm _tmp24_;
		time_t _tmp25_ = 0;
		time_t epoch;
		FsoGsmModem* _tmp26_;
		FsoGsmModemData* _tmp27_ = NULL;
		FsoGsmModemData* data;
		FsoGsmModemData* _tmp28_;
		FsoGsmNetworkTimeReport* _tmp29_;
		time_t _tmp30_;
		QualcommHtcPlusHTCCTZV* _tmp31_;
		gint _tmp32_;
		t = _tmp11_;
		_tmp12_ = htcctv;
		_tmp13_ = _tmp12_->hour;
		t.tm_hour = _tmp13_;
		_tmp14_ = htcctv;
		_tmp15_ = _tmp14_->minute;
		t.tm_min = _tmp15_;
		_tmp16_ = htcctv;
		_tmp17_ = _tmp16_->second;
		t.tm_sec = _tmp17_;
		_tmp18_ = htcctv;
		_tmp19_ = _tmp18_->day;
		t.tm_mday = _tmp19_;
		_tmp20_ = htcctv;
		_tmp21_ = _tmp20_->month;
		t.tm_mon = _tmp21_ - 1;
		_tmp22_ = htcctv;
		_tmp23_ = _tmp22_->year;
		t.tm_year = _tmp23_ - 1900;
		_tmp24_ = t;
		_tmp25_ = timegm (&_tmp24_);
		epoch = _tmp25_;
		_tmp26_ = fso_gsm_theModem;
		_tmp27_ = fso_gsm_modem_data (_tmp26_);
		data = _tmp27_;
		_tmp28_ = data;
		_tmp29_ = _tmp28_->networkTimeReport;
		_tmp30_ = epoch;
		_tmp31_ = htcctv;
		_tmp32_ = _tmp31_->tzoffset;
		fso_gsm_network_time_report_setTimeAndZone (_tmp29_, (gint) _tmp30_, _tmp32_);
		_g_object_unref0 (data);
	} else {
		FsoFrameworkLogger* _tmp33_;
		const gchar* _tmp34_;
		const gchar* _tmp35_ = NULL;
		gchar* _tmp36_ = NULL;
		gchar* _tmp37_;
		_tmp33_ = ((FsoFrameworkAbstractObject*) self)->logger;
		_tmp34_ = rhs;
		_tmp35_ = string_to_string (_tmp34_);
		_tmp36_ = g_strconcat ("Received invalid +HTCCTZV message ", _tmp35_, ". Please report", NULL);
		_tmp37_ = _tmp36_;
		fso_framework_logger_warning (_tmp33_, _tmp37_);
		_g_free0 (_tmp37_);
	}
	_g_object_unref0 (htcctv);
}


void qualcomm_htc_unsolicited_response_handler_plusHTCCTZV (QualcommHtcUnsolicitedResponseHandler* self, const gchar* prefix, const gchar* rhs) {
	g_return_if_fail (self != NULL);
	QUALCOMM_HTC_UNSOLICITED_RESPONSE_HANDLER_GET_CLASS (self)->plusHTCCTZV (self, prefix, rhs);
}


static void qualcomm_htc_unsolicited_response_handler_class_init (QualcommHtcUnsolicitedResponseHandlerClass * klass) {
	qualcomm_htc_unsolicited_response_handler_parent_class = g_type_class_peek_parent (klass);
	g_type_class_add_private (klass, sizeof (QualcommHtcUnsolicitedResponseHandlerPrivate));
	QUALCOMM_HTC_UNSOLICITED_RESPONSE_HANDLER_CLASS (klass)->plusPB_READY = qualcomm_htc_unsolicited_response_handler_real_plusPB_READY;
	QUALCOMM_HTC_UNSOLICITED_RESPONSE_HANDLER_CLASS (klass)->plusHTCCTZV = qualcomm_htc_unsolicited_response_handler_real_plusHTCCTZV;
	G_OBJECT_CLASS (klass)->finalize = qualcomm_htc_unsolicited_response_handler_finalize;
}


static void qualcomm_htc_unsolicited_response_handler_instance_init (QualcommHtcUnsolicitedResponseHandler * self) {
	self->priv = QUALCOMM_HTC_UNSOLICITED_RESPONSE_HANDLER_GET_PRIVATE (self);
}


static void qualcomm_htc_unsolicited_response_handler_finalize (GObject* obj) {
	QualcommHtcUnsolicitedResponseHandler * self;
	self = QUALCOMM_HTC_UNSOLICITED_RESPONSE_HANDLER (obj);
	G_OBJECT_CLASS (qualcomm_htc_unsolicited_response_handler_parent_class)->finalize (obj);
}


/**
 * 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
 *
 */
GType qualcomm_htc_unsolicited_response_handler_get_type (void) {
	return qualcomm_htc_unsolicited_response_handler_type_id;
}


GType qualcomm_htc_unsolicited_response_handler_register_type (GTypeModule * module) {
	static const GTypeInfo g_define_type_info = { sizeof (QualcommHtcUnsolicitedResponseHandlerClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) qualcomm_htc_unsolicited_response_handler_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (QualcommHtcUnsolicitedResponseHandler), 0, (GInstanceInitFunc) qualcomm_htc_unsolicited_response_handler_instance_init, NULL };
	qualcomm_htc_unsolicited_response_handler_type_id = g_type_module_register_type (module, FSO_GSM_TYPE_AT_UNSOLICITED_RESPONSE_HANDLER, "QualcommHtcUnsolicitedResponseHandler", &g_define_type_info, 0);
	return qualcomm_htc_unsolicited_response_handler_type_id;
}



