From b171814c2312f60aaf9ae56703997622dff26937 Mon Sep 17 00:00:00 2001
From: Zhenhua Zhang <zhenhua.zhang@intel.com>
Date: Wed, 9 Dec 2009 13:07:35 +0800
Subject: [PATCH 4/4] Add clip_timeout for notify incoming call

Schedule a clip_timeout in the ring notify. If CLIP comes right
after RING, we cancel the timer. If doesn't, we notify the call
without phone number.
---
 drivers/hfpmodem/voicecall.c |   38 ++++++++++++++++++++++++++++++++++++++
 1 files changed, 38 insertions(+), 0 deletions(-)

diff --git a/drivers/hfpmodem/voicecall.c b/drivers/hfpmodem/voicecall.c
index e7971a1..ebb5b7d 100644
--- a/drivers/hfpmodem/voicecall.c
+++ b/drivers/hfpmodem/voicecall.c
@@ -40,6 +40,7 @@
 #include "hfpmodem.h"
 
 #define POLL_CLCC_INTERVAL 2000
+#define CLIP_TIMEOUT 500
 
 static const char *none_prefix[] = { NULL };
 static const char *clcc_prefix[] = { "+CLCC:", NULL };
@@ -53,6 +54,7 @@ struct voicecall_data {
 	int cind_val[HFP_INDICATOR_LAST];
 	unsigned int local_release;
 	unsigned int clcc_source;
+	unsigned int clip_source;
 };
 
 struct release_id_req {
@@ -674,6 +676,29 @@ static void ccwa_notify(GAtResult *result, gpointer user_data)
 	ofono_voicecall_notify(vc, call);
 }
 
+static gboolean clip_timeout(gpointer user_data)
+{
+	struct ofono_voicecall *vc = user_data;
+	struct voicecall_data *vd = ofono_voicecall_get_data(vc);
+	GSList *l;
+	struct ofono_call *call;
+
+	l = g_slist_find_custom(vd->calls,
+				GINT_TO_POINTER(CALL_STATUS_INCOMING),
+				at_util_call_compare_by_status);
+
+	if (l == NULL)
+		return FALSE;
+
+	call = l->data;
+
+	ofono_voicecall_notify(vc, call);
+
+	vd->clip_source = 0;
+
+	return FALSE;
+}
+
 static void ring_notify(GAtResult *result, gpointer user_data)
 {
 	struct ofono_voicecall *vc = user_data;
@@ -717,6 +742,11 @@ static void ring_notify(GAtResult *result, gpointer user_data)
 
 	if (!call)
 		ofono_error("Couldn't create call, call management is fubar!");
+
+	/* We don't know the number must wait for CLIP to arrive before
+	 * announcing the call. If timeout, we notify the call as it is.
+	 */
+	vd->clip_source = g_timeout_add(CLIP_TIMEOUT, clip_timeout, vc);
 }
 
 static void clip_notify(GAtResult *result, gpointer user_data)
@@ -773,6 +803,11 @@ static void clip_notify(GAtResult *result, gpointer user_data)
 	call->clip_validity = validity;
 
 	ofono_voicecall_notify(vc, call);
+
+	if (vd->clip_source) {
+		g_source_remove(vd->clip_source);
+		vd->clip_source = 0;
+	}
 }
 
 static void ciev_call_notify(struct ofono_voicecall *vc,
@@ -1127,6 +1162,9 @@ static void hfp_voicecall_remove(struct ofono_voicecall *vc)
 	if (vd->clcc_source)
 		g_source_remove(vd->clcc_source);
 
+	if (vd->clip_source)
+		g_source_remove(vd->clip_source);
+
 	g_slist_foreach(vd->calls, (GFunc) g_free, NULL);
 	g_slist_free(vd->calls);
 
-- 
1.6.5.2

