Index: asp3_tinet_ecnl_arm/trunk/app1_usb_watt_meter/src/echonet_main.c
===================================================================
--- asp3_tinet_ecnl_arm/trunk/app1_usb_watt_meter/src/echonet_main.c	(revision 352)
+++ asp3_tinet_ecnl_arm/trunk/app1_usb_watt_meter/src/echonet_main.c	(revision 364)
@@ -1,7 +1,7 @@
 /*
  *  TOPPERS ECHONET Lite Communication Middleware
- * 
+ *
  *  Copyright (C) 2016-2018 Cores Co., Ltd. Japan
- * 
+ *
  *  上記著作権者は，以下の(1)～(4)の条件を満たす場合に限り，本ソフトウェ
  *  ア（本ソフトウェアを改変したものを含む．以下同じ）を使用・複製・改
@@ -26,5 +26,5 @@
  *      由に基づく請求からも，上記著作権者およびTOPPERSプロジェクトを
  *      免責すること．
- * 
+ *
  *  本ソフトウェアは，無保証で提供されているものである．上記著作権者お
  *  よびTOPPERSプロジェクトは，本ソフトウェアに関して，特定の使用目的
@@ -32,9 +32,9 @@
  *  アの利用により直接的または間接的に生じたいかなる損害に関しても，そ
  *  の責任を負わない．
- * 
+ *
  *  @(#) $Id$
  */
 
-/* 
+/*
  *  サンプルプログラム(1)の本体
  */
@@ -164,10 +164,10 @@
 {
 	/* サイズが１以外は受け付けない */
-	if(size != 1)
+	if (size != 1)
 		return 0;
 
 	*anno = *((uint8_t*)item->exinf) != *((uint8_t*)src);
 
-	switch(*(uint8_t *)src){
+	switch (*(uint8_t *)src) {
 	/* ONの場合 */
 	case 0x30:
@@ -196,10 +196,10 @@
 {
 	/* サイズが１以外は受け付けない */
-	if(size != 1)
+	if (size != 1)
 		return 0;
 
 	*anno = *((uint8_t *)item->exinf) != *((uint8_t *)src);
 
-	switch(*(uint8_t *)src){
+	switch (*(uint8_t *)src) {
 	/* 異常発生ありの場合 */
 	case 0x41:
@@ -271,5 +271,5 @@
 int date_prop_set(const EPRPINIB *item, const void *src, int size, bool_t *anno)
 {
-	uint8_t *p_src;
+	const uint8_t *p_src;
 	time_t temp;
 	struct tm _tm;
@@ -283,5 +283,5 @@
 	/* 年月日設定 */
 	p_src = (uint8_t *)src;
-	_tm.tm_year = (*(p_src++) * 100) + *p_src++;
+	_tm.tm_year = (*p_src++ * 100) + *p_src++;
 	_tm.tm_mon = (*p_src++) - 1;
 	_tm.tm_mday = *p_src++;
@@ -325,5 +325,5 @@
 
 	/* サイズが4以外は受け付けない */
-	if(size != 4)
+	if (size != 4)
 		return 0;
 
@@ -345,5 +345,5 @@
 
 	len = 48 - meter->current_pos;
-	if(len > 0)
+	if (len > 0)
 		memcpy(dst, &meter->integral_electric_energy_measurement_log[meter->current_pos], sizeof(uint32_t) * len);
 	dst = &((uint8_t *)dst)[len];
@@ -404,10 +404,10 @@
 
 	ret2 = get_tim(&now);
-	if (ret2 != E_OK){
+	if (ret2 != E_OK) {
 		syslog(LOG_ERROR, "get_tim");
 		return;
 	}
 
-	for(;;){
+	for (;;) {
 		prev = now;
 
@@ -417,5 +417,5 @@
 		/* 応答電文待ち */
 		ret = ecn_trcv_esv(&esv, timer);
-		if ((ret != E_OK) && (ret != E_BRK) && (ret != E_TMOUT)){
+		if ((ret != E_OK) && (ret != E_BRK) && (ret != E_TMOUT)) {
 			syslog(LOG_ERROR, "ecn_trcv_esv");
 			break;
@@ -423,5 +423,5 @@
 
 		ret2 = get_tim(&now);
-		if (ret2 != E_OK){
+		if (ret2 != E_OK) {
 			syslog(LOG_ERROR, "get_tim");
 			break;
@@ -438,5 +438,5 @@
 			/* 領域解放 */
 			ret = ecn_rel_esv(esv);
-			if (ret != E_OK){
+			if (ret != E_OK) {
 				syslog(LOG_ERROR, "ecn_rel_esv");
 				break;
@@ -447,5 +447,5 @@
 			/* 応答電文待ちの割り込みデータ取得 */
 			ret = ecn_get_brk_dat(esv, brkdat, sizeof(brkdat), &len);
-			if (ret != E_OK){
+			if (ret != E_OK) {
 				syslog(LOG_ERROR, "ecn_get_brk_dat");
 				break;
@@ -457,5 +457,5 @@
 			/* 領域解放 */
 			ret = ecn_rel_esv(esv);
-			if (ret != E_OK){
+			if (ret != E_OK) {
 				syslog(LOG_ERROR, "ecn_rel_esv");
 				break;
@@ -468,6 +468,4 @@
 }
 
-bool_t started = false;
-
 void echonet_change_netif_link(uint8_t link_up, uint8_t up)
 {
@@ -477,14 +475,10 @@
 		return;
 
-	if (up && !started) {
-		started = true;
-
-		/* ECHONETミドルウェアを起動 */
-		ret = ecn_sta_svc();
-		if (ret != E_OK)
-			return;
-
-		/* ECHONETミドルウェアを起動するのを待つ */
-		dly_tsk(100);
+	if (up) {
+		/* インスタンスリスト通知の送信 */
+		ret = ecn_ntf_inl();
+		if (ret != E_OK) {
+			syslog(LOG_ERROR, "ecn_ntf_inl");
+		}
 	}
 
@@ -500,10 +494,11 @@
 }
 
-enum main_state_t{
+enum main_state_t {
+	main_state_start,
 	main_state_idle,
 };
 
 int main_timer = TMO_FEVR;
-enum main_state_t main_state = main_state_idle;
+enum main_state_t main_state = main_state_start;
 int main_lcd_state = 0;
 int main_lcd_timer = TMO_FEVR;
@@ -557,8 +552,8 @@
 	analogin_init(&joystick, P1_12);
 
-	/* 10ms後にボタン状態を確認 */
-	main_timer = 10 * 1000;
-
-	/* ボタン状態読み込み */
+	/* ECHONETミドルウェアを起動するのを待つ */
+	main_state = main_state_start;
+	main_timer = 1000 * 1000;
+
 	btn = main_read_button(&joystick);
 	main_btn1_state = btn == BUTTON_LEFT;
@@ -568,5 +563,5 @@
 	spi_init(&lcd.hspi, P10_14, P10_15, P10_12, NC);
 	spi_format(&lcd.hspi, 8, 0, 0);
-	spi_frequency(&lcd.hspi, 1000000);
+	spi_frequency(&lcd.hspi, 4000000);
 
 	gpio_init_out(&lcd.cs_pin, P10_13);
@@ -590,4 +585,5 @@
 	main_adv_timer = 1000 * 1000;
 
+	/* メインタスクを起動 */
 	ER ret = act_tsk(MAIN_TASK);
 	if (ret != E_OK) {
@@ -603,6 +599,6 @@
 	int result = main_timer;
 
-	if((result == TMO_FEVR)
-		|| ((main_lcd_timer != TMO_FEVR) && (main_lcd_timer < result))){
+	if ((result == TMO_FEVR)
+		|| ((main_lcd_timer != TMO_FEVR) && (main_lcd_timer < result))) {
 		result = main_lcd_timer;
 	}
@@ -621,14 +617,14 @@
 static void main_progress(int interval)
 {
-	if(main_timer != TMO_FEVR){
+	if (main_timer != TMO_FEVR) {
 		main_timer -= interval;
-		if(main_timer < 0){
+		if (main_timer < 0) {
 			main_timer = 0;
 		}
 	}
 
-	if(main_lcd_timer != TMO_FEVR){
+	if (main_lcd_timer != TMO_FEVR) {
 		main_lcd_timer -= interval;
-		if(main_lcd_timer < 0){
+		if (main_lcd_timer < 0) {
 			main_lcd_timer = 0;
 		}
@@ -655,11 +651,11 @@
 
 	ret = ecn_itr_ini(&enm, esv);
-	if(ret != E_OK){
+	if (ret != E_OK) {
 		syslog(LOG_ERROR, "ecn_itr_ini");
 		return;
 	}
 
-	for(;;) {
-		while((ret = ecn_itr_nxt(&enm, &epc, &pdc, p_edt)) == E_OK) {
+	for (;;) {
+		while ((ret = ecn_itr_nxt(&enm, &epc, &pdc, p_edt)) == E_OK) {
 			switch (epc) {
 			case 0x80:
@@ -669,9 +665,9 @@
 			}
 		}
-		if(ret != E_BOVR){
+		if (ret != E_BOVR) {
 			syslog(LOG_ERROR, "ecn_itr_nxt");
 			break;
 		}
-		if(enm.is_eof)
+		if (enm.is_eof)
 			break;
 	}
@@ -684,9 +680,9 @@
 {
 	char mac_text[30] = "mac: ";
-	char ipaddr_text[30] = "ipaddr: ";
-	T_IN4_ADDR *ip4_addr;
+	char ipaddr_text[30] = "ipadr: ";
+	const T_IN4_ADDR *ip4_addr;
 	int pos;
 
-	switch(main_state){
+	switch (main_state) {
 	case main_state_idle:
 		if (len == 0)
@@ -704,10 +700,10 @@
 			pos = str_macaddr(&mac_text[4], sizeof(mac_text) - 4, mac_addr, 0);
 			mac_text[pos + 4] = '\0';
-			lcd_drawString(&lcd, mac_text, X_LINE_TO_PIX(&lcd, 0) + 10, Y_ROW_TO_PIX(&lcd, 14) + 10, ST7735_BLACK, ST7735_WHITE);
+			lcd_drawString(&lcd, mac_text, X_LINE_TO_PIX(&lcd, 0), Y_ROW_TO_PIX(&lcd, 11), ST7735_BLACK, ST7735_WHITE);
 
 			ip4_addr = in4_get_ifaddr(0);
-			pos = str_ipv4addr(&ipaddr_text[7], sizeof(ipaddr_text) - 7, ip4_addr, 0);
-			ipaddr_text[pos + 7] = '\0';
-			lcd_drawString(&lcd, ipaddr_text, X_LINE_TO_PIX(&lcd, 0) + 10, Y_ROW_TO_PIX(&lcd, 15) + 10, ST7735_BLACK, ST7735_WHITE);
+			pos = str_ipv4addr(&ipaddr_text[6], sizeof(ipaddr_text) - 6, ip4_addr, 0);
+			ipaddr_text[pos + 6] = '\0';
+			lcd_drawString(&lcd, ipaddr_text, X_LINE_TO_PIX(&lcd, 0), Y_ROW_TO_PIX(&lcd, 12), ST7735_BLACK, ST7735_WHITE);
 		}
 		break;
@@ -724,9 +720,9 @@
 static void main_timeout()
 {
-	if(main_timer == 0){
+	if (main_timer == 0) {
 		main_ontimer();
 	}
 
-	if(main_lcd_timer == 0){
+	if (main_lcd_timer == 0) {
 		main_lcd_ontimer();
 	}
@@ -746,5 +742,5 @@
 	int port_no, btn;
 
-	switch(main_lcd_state) {
+	switch (main_lcd_state) {
 	case 0:
 	case 1:
@@ -757,10 +753,10 @@
 		text[16] = '0' + main_7seg[port_no][2];
 		text[17] = '0' + main_7seg[port_no][3];
-		lcd_drawString(&lcd, &text[14], X_LINE_HALF_TO_PIX(&lcd, 11) + 10, Y_ROW_TO_PIX(&lcd, main_lcd_state + 4) + 10, ST7735_BLACK, ST7735_WHITE);
+		lcd_drawString(&lcd, &text[14], X_LINE_HALF_TO_PIX(&lcd, 11), Y_ROW_TO_PIX(&lcd, main_lcd_state + 2), ST7735_BLACK, ST7735_WHITE);
 		main_lcd_state++;
 		break;
 	case 6:
 		btn = main_read_button(&joystick);
-		lcd_drawString(&lcd, btn_texts[btn], X_LINE_TO_PIX(&lcd, 0) + 10, Y_ROW_TO_PIX(&lcd, 10) + 10, ST7735_BLACK, ST7735_WHITE);
+		lcd_drawString(&lcd, btn_texts[btn], X_LINE_TO_PIX(&lcd, 0), Y_ROW_TO_PIX(&lcd, 9), ST7735_BLACK, ST7735_WHITE);
 		main_lcd_state++;
 		break;
@@ -769,8 +765,8 @@
 		break;
 	case 10:
-		lcd_drawString(&lcd, "USB電力計", X_LINE_TO_PIX(&lcd, 10) + 10, Y_ROW_TO_PIX(&lcd, 0) + 10, ST7735_BLACK, ST7735_WHITE);
+		lcd_drawString(&lcd, "USB電力計", X_LINE_TO_PIX(&lcd, 6), Y_ROW_TO_PIX(&lcd, 0), ST7735_BLACK, ST7735_WHITE);
 		for (port_no = 0; port_no < 6; port_no++) {
 			text[3] = '1' + port_no;
-			lcd_drawString(&lcd, text, X_LINE_TO_PIX(&lcd, 0) + 10, Y_ROW_TO_PIX(&lcd, port_no + 4) + 10, ST7735_BLACK, ST7735_WHITE);
+			lcd_drawString(&lcd, text, X_LINE_TO_PIX(&lcd, 0), Y_ROW_TO_PIX(&lcd, port_no + 2), ST7735_BLACK, ST7735_WHITE);
 		}
 		main_lcd_state = 0;
@@ -802,8 +798,20 @@
 static void main_ontimer()
 {
+	ER ret;
 	uint8_t btn;
 	uint16_t ad_value;
 
-	switch(main_state){
+	switch (main_state) {
+	case main_state_start:
+		/* ECHONETミドルウェアを起動 */
+		ret = ecn_sta_svc();
+		if (ret != E_OK) {
+			syslog(LOG_ERROR, "ecn_sta_svc");
+		}
+
+		/* 10ms後にボタン状態を確認 */
+		main_state = main_state_idle;
+		main_timer = 10 * 1000;
+		break;
 	case main_state_idle:
 		/* 10ms後にボタン状態を確認 */
@@ -845,7 +853,7 @@
 
 		/* ボタン１の処理 */
-		if((btn == BUTTON_LEFT) && !main_btn1_state){
+		if ((btn == BUTTON_LEFT) && !main_btn1_state) {
 			main_btn1_count++;
-			if(main_btn1_count > 10){
+			if (main_btn1_count > 10) {
 				main_btn1_count = 0;
 				main_btn1_state = true;
@@ -854,7 +862,7 @@
 			}
 		}
-		else if((btn != BUTTON_LEFT) && main_btn1_state){
+		else if ((btn != BUTTON_LEFT) && main_btn1_state) {
 			main_btn1_count++;
-			if(main_btn1_count > 10){
+			if (main_btn1_count > 10) {
 				main_btn1_count = 0;
 				main_btn1_state = false;
@@ -865,7 +873,7 @@
 
 		/* ボタン２の処理 */
-		if((btn == BUTTON_RIGHT) && !main_btn2_state){
+		if ((btn == BUTTON_RIGHT) && !main_btn2_state) {
 			main_btn2_count++;
-			if(main_btn2_count > 10){
+			if (main_btn2_count > 10) {
 				main_btn2_count = 0;
 				main_btn2_state = true;
@@ -874,7 +882,7 @@
 			}
 		}
-		else if((btn != BUTTON_RIGHT) && main_btn2_state){
+		else if ((btn != BUTTON_RIGHT) && main_btn2_state) {
 			main_btn2_count++;
-			if(main_btn2_count > 10){
+			if (main_btn2_count > 10) {
 				main_btn2_count = 0;
 				main_btn2_state = false;
@@ -897,21 +905,21 @@
 static uint8_t main_read_button(analogin_t *obj)
 {
-    float a = analogin_read(obj);
-
-    a *= 5.0;
-
-    //printf("Button read analog = %f\r\n",a);
-    if (a < 0.2)
-    	return BUTTON_DOWN;
-    if (a < 1.0)
-    	return BUTTON_RIGHT;
-    if (a < 1.7)
-    	return BUTTON_SELECT;
-    if (a < 2.6)
-    	return BUTTON_UP;
-    if (a < 4.6)
-    	return BUTTON_LEFT;
-    else
-    	return BUTTON_NONE;
+	float a = analogin_read(obj);
+
+	a *= 5.0;
+
+	//printf("Button read analog = %f\r\n",a);
+	if (a < 0.2)
+		return BUTTON_DOWN;
+	if (a < 1.0)
+		return BUTTON_RIGHT;
+	if (a < 1.7)
+		return BUTTON_SELECT;
+	if (a < 2.6)
+		return BUTTON_UP;
+	if (a < 4.6)
+		return BUTTON_LEFT;
+	else
+		return BUTTON_NONE;
 }
 
@@ -924,11 +932,11 @@
 {
 	/* 押されて戻った時に処理する */
-	if(push)
+	if (push)
 		return;
 
 	/* 表示ポート番号 */
-	if(!main_rev){
+	if (!main_rev) {
 		main_port_no--;
-		if(main_port_no < 1){
+		if (main_port_no < 1) {
 			main_port_no = 1;
 			main_rev = true;
@@ -936,7 +944,7 @@
 		}
 	}
-	else{
+	else {
 		main_port_no++;
-		if(main_port_no > main_ad_count){
+		if (main_port_no > main_ad_count) {
 			main_port_no = main_ad_count;
 			main_rev = false;
@@ -952,5 +960,5 @@
 {
 	/* 押されて戻った時に処理する */
-	if(push)
+	if (push)
 		return;
 
@@ -961,7 +969,7 @@
 
 	/* 表示ポート番号 */
-	if(!main_rev){
+	if (!main_rev) {
 		main_port_no++;
-		if(main_port_no > main_ad_count){
+		if (main_port_no > main_ad_count) {
 			main_port_no = main_ad_count;
 			main_rev = false;
@@ -969,7 +977,7 @@
 		}
 	}
-	else{
+	else {
 		main_port_no--;
-		if(main_port_no < 1){
+		if (main_port_no < 1) {
 			main_port_no = 1;
 			main_rev = true;
@@ -988,5 +996,5 @@
 
 	main_port_no++;
-	if(main_port_no > main_ad_count)
+	if (main_port_no > main_ad_count)
 		main_port_no = 1;
 
