Index: EcnlProtoTool/trunk/asp3_dcre/tinet/netapp/dhcp4_cli.c
===================================================================
--- EcnlProtoTool/trunk/asp3_dcre/tinet/netapp/dhcp4_cli.c	(revision 331)
+++ EcnlProtoTool/trunk/asp3_dcre/tinet/netapp/dhcp4_cli.c	(revision 429)
@@ -206,4 +206,20 @@
 #endif
 
+#ifdef SEM_DHCP4_CLI_TIMER
+#define DHCP4_CLI_TIMER_LOCK() do { syscall(wai_sem(SEM_DHCP4_CLI_TIMER)); } while(0)
+#define DHCP4_CLI_TIMER_UNLOCK() do { syscall(sig_sem(SEM_DHCP4_CLI_TIMER)); } while(0)
+#else
+#define DHCP4_CLI_TIMER_LOCK() do { } while(0)
+#define DHCP4_CLI_TIMER_UNLOCK() do { } while(0)
+#endif
+
+#ifdef SEM_DHCP4_CLI_READY
+#define DHCP4_CLI_READY_WAIT(ct) do { syscall(wai_sem(SEM_DHCP4_CLI_READY)); } while(0)
+#define DHCP4_CLI_READY_SIGNAL(ct) do { syscall(sig_sem(SEM_DHCP4_CLI_READY)); } while(0)
+#else
+#define DHCP4_CLI_READY_WAIT(ct) do { } while(0)
+#define DHCP4_CLI_READY_SIGNAL(ct) do { ct->sig = 1; syscall(wup_tsk(ct->tskid)); } while(0)
+#endif
+
 /*
  *  dhcp4_cksum -- ãã§ãã¯ãµã ã®è¨ç®é¢æ°
@@ -381,5 +397,5 @@
 #endif	/* of #ifdef USE_LCD */
 #endif
-
+		ether_set_down();
 		}
 	}
@@ -508,7 +524,5 @@
 		lcd_putc(LCD_PORTID, '\n');
 #endif	/* of #ifdef USE_LCD */
-	if (ct->set_addr_callback != NULL)
-		ct->set_addr_callback();
-
+		ether_set_up();
 		}
 	}
@@ -951,8 +965,8 @@
 	int ix;
 
-	syscall(wai_sem(SEM_DHCP4_CLI_TIMER));
+	DHCP4_CLI_TIMER_LOCK();
 	for (ix = NUM_DHCP4C_TIMERS; ix -- > 0; )
 		ct->timers[ix] = 0;
-	syscall(sig_sem(SEM_DHCP4_CLI_TIMER));
+	DHCP4_CLI_TIMER_UNLOCK();
 	}
 
@@ -970,7 +984,7 @@
 
 	/* OFFER ã¡ãã»ã¼ã¸åä¿¡ã¿ã¤ã ã¢ã¦ããè¨­å®ããã*/
-	syscall(wai_sem(SEM_DHCP4_CLI_TIMER));
+	DHCP4_CLI_TIMER_LOCK();
 	ct->timers[DHCP4C_TIMER_RCV_OFFER] = SYSTIM2TIMER(TMO_DHCP4C_OFFER);
-	syscall(sig_sem(SEM_DHCP4_CLI_TIMER));
+	DHCP4_CLI_TIMER_UNLOCK();
 
 	/* éä¿¡å
@@ -987,5 +1001,5 @@
 	/* DISCOVER ã¡ãã»ã¼ã¸ãéä¿¡ããã*/
 	ct->flags |= DHCP4C_FLAG_TMOUT_SND_DISC;
-	syscall(sig_sem(SEM_DHCP4_CLI_READY));
+	DHCP4_CLI_READY_SIGNAL(ct);
 	}
 
@@ -1018,9 +1032,9 @@
 
 	/* EXPIREãRENEWãREBIND ã¿ã¤ãã¼ãè¨­å®ããã*/
-	syscall(wai_sem(SEM_DHCP4_CLI_TIMER));
+	DHCP4_CLI_TIMER_LOCK();
 	ct->timers[DHCP4C_TIMER_EXPIRE] = SYSTIM2TIMER(ct->expire);		/* EXPIRE      ã¾ã§ã®æé */
 	ct->timers[DHCP4C_TIMER_REBIND] = SYSTIM2TIMER(ct->rebind);		/* REBINDï¼T2ï¼ã¾ã§ã®æé */
 	ct->timers[DHCP4C_TIMER_RENEW]  = SYSTIM2TIMER(ct->renew);		/* RENEW ï¼T1ï¼ã¾ã§ã®æé */
-	syscall(sig_sem(SEM_DHCP4_CLI_TIMER));
+	DHCP4_CLI_TIMER_UNLOCK();
 
 	/* BOUND ç¶æ
@@ -1048,7 +1062,7 @@
 ã RENEW ä»¥å¤ã§ã¯ ACK/NAK ã¡ãã»ã¼ã¸åä¿¡ã¿ã¤ã ã¢ã¦ããè¨­å®ããã*/
 	if (fsm != DHCP4_FSM_RENEW) { 
-		syscall(wai_sem(SEM_DHCP4_CLI_TIMER));
+		DHCP4_CLI_TIMER_LOCK();
 		ct->timers[DHCP4C_TIMER_RCV_ACK] = SYSTIM2TIMER(TMO_DHCP4C_ACK);
-		syscall(sig_sem(SEM_DHCP4_CLI_TIMER));
+		DHCP4_CLI_TIMER_UNLOCK();
 		}
 
@@ -1073,5 +1087,5 @@
 	/* REQUEST ã¡ãã»ã¼ã¸ãéä¿¡ããã*/
 	ct->flags |= DHCP4C_FLAG_TMOUT_SND_REQ;
-	syscall(sig_sem(SEM_DHCP4_CLI_READY));
+	DHCP4_CLI_READY_SIGNAL(ct);
 	}
 
@@ -1103,5 +1117,5 @@
 	/* RELEASE ã¡ãã»ã¼ã¸ãéä¿¡ããã*/
 	ct->flags |= DHCP4C_FLAG_TMOUT_SND_REL;
-	syscall(sig_sem(SEM_DHCP4_CLI_READY));
+	DHCP4_CLI_READY_SIGNAL(ct);
 	}
 
@@ -1116,5 +1130,5 @@
 	uint8_t	*option;
 
-	memset((void*)&ct->snd_msg->msg, sizeof(ct->snd_msg->msg), 0);
+	memset((void*)&ct->snd_msg->msg, 0, sizeof(ct->snd_msg->msg));
 	ct->snd_msg->len = sizeof(ct->snd_msg->msg);
 
@@ -1410,7 +1424,7 @@
 
 		/* REQUEST ã¡ãã»ã¼ã¸éä¿¡ã¿ã¤ãã¼ãåæ­¢ããã*/
-		syscall(wai_sem(SEM_DHCP4_CLI_TIMER));
+		DHCP4_CLI_TIMER_LOCK();
 		ct->timers[DHCP4C_TIMER_SND_REQ] = 0;
-		syscall(sig_sem(SEM_DHCP4_CLI_TIMER));
+		DHCP4_CLI_TIMER_UNLOCK();
 
 		/*
@@ -1464,7 +1478,7 @@
 
 		/* ACK/NAK ã¡ãã»ã¼ã¸åä¿¡ã¿ã¤ãã¼ãåæ­¢ããã*/
-		syscall(wai_sem(SEM_DHCP4_CLI_TIMER));
+		DHCP4_CLI_TIMER_LOCK();
 		ct->timers[DHCP4C_TIMER_RCV_ACK] = 0;
-		syscall(sig_sem(SEM_DHCP4_CLI_TIMER));
+		DHCP4_CLI_TIMER_UNLOCK();
 
 		/*
@@ -1544,7 +1558,7 @@
 
 	/* åéä¿¡ã¿ã¤ã ã¢ã¦ããè¨­å®ããã*/
-	syscall(wai_sem(SEM_DHCP4_CLI_TIMER));
+	DHCP4_CLI_TIMER_LOCK();
 	ct->timers[DHCP4C_TIMER_SND_REQ] = SYSTIM2TIMER(ct->interval);
-	syscall(sig_sem(SEM_DHCP4_CLI_TIMER));
+	DHCP4_CLI_TIMER_UNLOCK();
 	}
 
@@ -1582,5 +1596,5 @@
 	/* ã¡ãã»ã¼ã¸éä¿¡å¾
 ã¡ãè¡ãã*/
-	syscall(wai_sem(SEM_DHCP4_CLI_READY));
+	DHCP4_CLI_READY_WAIT(ct);
 
 	/* SLEEP ç¶æ
@@ -1602,7 +1616,7 @@
 
 		/* DISCOVER ã¡ãã»ã¼ã¸éä¿¡ã¿ã¤ãã¼ãåæ­¢ããã*/
-		syscall(wai_sem(SEM_DHCP4_CLI_TIMER));
+		DHCP4_CLI_TIMER_LOCK();
 		ct->timers[DHCP4C_TIMER_SND_DISC] = 0;
-		syscall(sig_sem(SEM_DHCP4_CLI_TIMER));
+		DHCP4_CLI_TIMER_UNLOCK();
 
 		/* REQUEST ã¡ãã»ã¼ã¸ãéä¿¡ãã¦ REQUEST ç¶æ
@@ -1634,7 +1648,7 @@
 
 		/* OFFER ã¡ãã»ã¼ã¸åä¿¡ã¿ã¤ãã¼ãåæ­¢ããã*/
-		syscall(wai_sem(SEM_DHCP4_CLI_TIMER));
+		DHCP4_CLI_TIMER_LOCK();
 		ct->timers[DHCP4C_TIMER_RCV_OFFER] = 0;
-		syscall(sig_sem(SEM_DHCP4_CLI_TIMER));
+		DHCP4_CLI_TIMER_UNLOCK();
 
 		/* REQUEST ã¡ãã»ã¼ã¸ãéä¿¡ãã¦ REQUEST ç¶æ
@@ -1692,8 +1706,10 @@
 
 	/* åéä¿¡ã¿ã¤ã ã¢ã¦ããè¨­å®ããã*/
-	syscall(wai_sem(SEM_DHCP4_CLI_TIMER));
+	DHCP4_CLI_TIMER_LOCK();
 	ct->timers[DHCP4C_TIMER_SND_DISC] = SYSTIM2TIMER(ct->interval);
-	syscall(sig_sem(SEM_DHCP4_CLI_TIMER));
-	}
+	DHCP4_CLI_TIMER_UNLOCK();
+	}
+
+#ifdef DHCP4_CLI_TASK
 
 /*
@@ -1706,5 +1722,5 @@
 	int ix;
 
-	syscall(wai_sem(SEM_DHCP4_CLI_TIMER));
+	DHCP4_CLI_TIMER_LOCK();
 	for (ix = NUM_DHCP4C_TIMERS; ix -- > 0; ) {
 		if (ct->timers[ix] != 0) {
@@ -1712,13 +1728,15 @@
 			if (ct->timers[ix] == 0) {
 				ct->flags = (ct->flags & ~DHCP4C_FLAG_TMOUT_MASK) | DHCP4C_FLAG_TMOUT_TIMER | ix;
-				syscall(sig_sem(SEM_DHCP4_CLI_READY));
+				DHCP4_CLI_READY_SIGNAL(ct);
 				}
 			}
 		}
-	syscall(sig_sem(SEM_DHCP4_CLI_TIMER));
+	DHCP4_CLI_TIMER_UNLOCK();
 
 	/* 1ç§æ¯ã«ã¿ã¤ã ã¢ã¦ãããã*/
 	timeout((callout_func)dhcpc_timer, ct, NET_TIMER_HZ);
 	}
+
+#endif
 
 /*
@@ -1832,4 +1850,6 @@
 	}
 
+#ifdef DHCP4_CLI_TASK
+
 /*
  *  dispatch_event -- ã¤ãã³ãæ¯ã®å¦ç
@@ -1845,5 +1865,5 @@
 		/* ã¡ãã»ã¼ã¸ã®åä¿¡ã¨ã¿ã¤ã ã¢ã¦ããå¾
 ã¤ã*/
-		syscall(wai_sem(SEM_DHCP4_CLI_READY));
+		DHCP4_CLI_READY_WAIT(ct);
 
 		if (ct->flags & DHCP4C_FLAG_RCV_MSG) {
@@ -1863,4 +1883,6 @@
 	}
 
+#endif
+
 /*
  *  init_context -- DHCP ã¯ã©ã¤ã¢ã³ãã³ã³ãã­ã¹ãæ§é ä½ãåæåããã
@@ -1868,12 +1890,12 @@
 
 static void
-init_context (T_DHCP4_CLI_CONTEXT *ct, ID cepid)
+init_context (T_DHCP4_CLI_CONTEXT *ct, ID tskid, ID cepid)
 {
   	memset(ct, 0, sizeof(*ct));
+	ct->tskid	= tskid;
 	ct->cepid	= cepid;
 	ct->sc		= IF_ETHER_NIC_GET_SOFTC();
 	ct->dst.portno	= DHCP4_SRV_CFG_PORTNO;
 	ct->dst.ipaddr	= IPV4_ADDRANY;
-	ct->set_addr_callback = DHCP4_CLI_SET_ADDR_CALLBACK;
 	}
 
@@ -1968,5 +1990,6 @@
 
 		/* SLEEP ãè§£é¤ããã*/
-		wup_tsk(DHCP4_CLI_TASK);
+		context.req = 1;
+		wup_tsk(context.tskid);
 		return E_OK;
 		}
@@ -2061,8 +2084,10 @@
 				}
 			}
-		syscall(sig_sem(SEM_DHCP4_CLI_READY));
+		DHCP4_CLI_READY_SIGNAL((&context));
 		}
 	return E_OK;
 	}
+
+#ifdef DHCP4_CLI_TASK
 
 /*
@@ -2075,5 +2100,5 @@
 	T_DHCP4_CLI_CONTEXT	*ct;
 	ID			tskid;
-	ER			error;
+	ER			error = E_OK;
 
 	dly_tsk(1000);
@@ -2084,31 +2109,8 @@
 
 	/* DHCP ã¯ã©ã¤ã¢ã³ãã³ã³ãã­ã¹ãæ§é ä½ãåæåããã*/
-	init_context(ct, (ID)exinf);
+	init_context(ct, tskid, (ID)exinf);
+	ct->fsm = DHCP4_FSM_SLEEP;
 
 	while (true) {
-
-		/* 1ç§æ¯ã«ã¿ã¤ã ã¢ã¦ãããããã«ã¿ã¤ãã¼ãè¨­å®ããã*/
-		timeout((callout_func)dhcpc_timer, ct, NET_TIMER_HZ / DHCP4C_TIMER_HZ);
-
-		/* ã¡ãã»ã¼ã¸æ§é ä½ãåæåããã*/
-		if ((error = init_cli_msg(ct)) == E_OK) {
-
-			/* SELECT ç¶æ
-ã«é·ç§»ããã*/
-			start_select(ct);
-
-			/* ã¡ã¤ã³ã«ã¼ã */
-			error = dispatch_event(ct);
-			}
-
-		/* å
-¨ã¦ã® TIMER ãã­ã£ã³ã»ã«ããã*/
-		cancel_all_timers(ct);
-
-		/* ã¿ã¤ãã¼ãåæ­¢ããã*/
-		untimeout((callout_func)dhcpc_timer, ct);
-
-		/* ã¡ãã»ã¼ã¸æ§é ä½ãè§£æ¾ããã*/
-		rel_cli_msg (ct);
 
 		/* ãªã¼ã¹ãè§£æ¾å¾ãååå¾ãæå®ããã¦ããªããã°ä¼æ­¢ããã*/
@@ -2116,6 +2118,8 @@
 
 			/* ä¼æ­¢ããã*/
-			if (error == E_OK)
-				syslog(LOG_NOTICE, "[DHCP4C] lease released, go to sleep.");
+			if (error == E_OK) {
+				if (ct->fsm != DHCP4_FSM_SLEEP)
+					syslog(LOG_NOTICE, "[DHCP4C] lease released, go to sleep.");
+			}
 			else {
 				syslog(LOG_NOTICE, "[DHCP4C] server not available, go to sleep, error: %s.", itron_strerror(error));
@@ -2126,6 +2130,145 @@
 		ct->flags = 0;
 		ct->error = E_OK;
-		}
-	}
-
+
+		/* 1ç§æ¯ã«ã¿ã¤ã ã¢ã¦ãããããã«ã¿ã¤ãã¼ãè¨­å®ããã*/
+		timeout((callout_func)dhcpc_timer, ct, NET_TIMER_HZ / DHCP4C_TIMER_HZ);
+
+		/* ã¡ãã»ã¼ã¸æ§é ä½ãåæåããã*/
+		if ((error = init_cli_msg(ct)) == E_OK) {
+
+			/* SELECT ç¶æ
+ã«é·ç§»ããã*/
+			start_select(ct);
+
+			/* ã¡ã¤ã³ã«ã¼ã */
+			error = dispatch_event(ct);
+			}
+
+		/* å
+¨ã¦ã® TIMER ãã­ã£ã³ã»ã«ããã*/
+		cancel_all_timers(ct);
+
+		/* ã¿ã¤ãã¼ãåæ­¢ããã*/
+		untimeout((callout_func)dhcpc_timer, ct);
+
+		/* ã¡ãã»ã¼ã¸æ§é ä½ãè§£æ¾ããã*/
+		rel_cli_msg (ct);
+		}
+	}
+
+#else
+
+T_DHCP4_CLI_CONTEXT *
+dhcp4_cli_initialize(ID tskid, ID cepid)
+{
+	T_DHCP4_CLI_CONTEXT *ct = &context;
+	/* DHCP ã¯ã©ã¤ã¢ã³ãã³ã³ãã­ã¹ãæ§é ä½ãåæåããã*/
+	init_context(ct, tskid, cepid);
+	ct->fsm = DHCP4_FSM_SLEEP;
+	ct->timer = TMO_FEVR;
+	return ct;
+	}
+
+int
+dhcp4_cli_get_timer(T_DHCP4_CLI_CONTEXT *ct)
+{
+	if (ct->fsm == DHCP4_FSM_SLEEP)
+		return TMO_FEVR;
+
+	return ct->timer;
+	}
+
+void
+dhcp4_cli_progress(T_DHCP4_CLI_CONTEXT *ct, int elapse)
+{
+	int ix;
+
+	if ((ct->fsm == DHCP4_FSM_SLEEP) || (ct->timer == TMO_FEVR))
+		return;
+
+	ct->timer -= elapse;
+	if (ct->timer > 0)
+		return;
+
+	/* 1ç§æ¯ã«ã¿ã¤ã ã¢ã¦ãããããã«ã¿ã¤ãã¼ãè¨­å®ããã*/
+	ct->timer = 1000 * 1000;
+
+	for (ix = NUM_DHCP4C_TIMERS; ix-- > 0; ) {
+		if (ct->timers[ix] != 0) {
+			ct->timers[ix] --;
+			if (ct->timers[ix] <= 0) {
+				ct->timers[ix] = 0;
+				ct->flags = (ct->flags & ~DHCP4C_FLAG_TMOUT_MASK) | DHCP4C_FLAG_TMOUT_TIMER | ix;
+				DHCP4_CLI_READY_SIGNAL(ct);
+				}
+			}
+		}
+	}
+
+void
+dhcp4_cli_wakeup(T_DHCP4_CLI_CONTEXT *ct)
+{
+	if (ct->req) {
+		ct->req = 0;
+		if (ct->snd_msg == NULL) {
+			ct->flags = 0;
+			ct->error = E_OK;
+
+			/* ã¡ãã»ã¼ã¸æ§é ä½ãåæåããã*/
+			if ((ct->error = init_cli_msg(ct)) != E_OK)
+				return;
+
+			ct->timer = 1000 * 1000;
+
+			/* SELECT ç¶æ
+ã«é·ç§»ããã*/
+			start_select(ct);
+			}
+		}
+
+	if (!ct->sig)
+		return;
+	ct->sig = 0;
+
+	if (ct->flags & DHCP4C_FLAG_RCV_MSG) {
+		while (ct->val_lst != NULL) {
+			ct->error = eval_rcv_msg(ct);
+			}
+		ct->flags &= ~DHCP4C_FLAG_RCV_MSG;
+		}
+	}
+
+void
+dhcp4_cli_timeout(T_DHCP4_CLI_CONTEXT *ct)
+{
+	if (ct->flags & DHCP4C_FLAG_TMOUT_MASK)
+		ct->error = dispatch_timeout(ct);
+
+	if (ct->error != E_OK || ct->fsm == DHCP4_FSM_SLEEP) {
+		/* å
+¨ã¦ã® TIMER ãã­ã£ã³ã»ã«ããã*/
+		cancel_all_timers(ct);
+		ct->timer = TMO_FEVR;
+
+		/* ã¡ãã»ã¼ã¸æ§é ä½ãè§£æ¾ããã*/
+		rel_cli_msg(ct);
+
+		/* ãªã¼ã¹ãè§£æ¾å¾ãååå¾ãæå®ããã¦ããªããã°ä¼æ­¢ããã*/
+		if (!(ct->flags & DHCP4C_FLAG_RENEW)) {
+
+			/* ä¼æ­¢ããã*/
+			if (ct->error == E_OK) {
+				if (ct->fsm != DHCP4_FSM_SLEEP)
+					syslog(LOG_NOTICE, "[DHCP4C] lease released, go to sleep.");
+			}
+			else {
+				syslog(LOG_NOTICE, "[DHCP4C] server not available, go to sleep, error: %s.", itron_strerror(ct->error));
+				ct->error = E_OK;
+				ct->fsm = DHCP4_FSM_SLEEP;
+				}
+			}
+		}
+	}
+
+#endif /* DHCP4_CLI_TASK */
 #endif	/* of #ifdef DHCP4_CLI_CFG */
