Ryo Iizuka / libMiMic

Dependents:   MbedFileServer_1768MiniDK2 RedWireBridge IssueDebug_gcc MiMicRemoteMCU-for-Mbed ... more

Committer:
nyatla
Date:
Thu Aug 08 15:21:43 2013 +0000
Revision:
50:35e765380772
Parent:
48:00d211aac2ec
Child:
57:bc4330dfa62f
bugfix; ????????????????; UPnP??????????????

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nyatla 0:142ee8b12fef 1 /*********************************************************************************
nyatla 0:142ee8b12fef 2 * PROJECT: MiMic
nyatla 0:142ee8b12fef 3 * --------------------------------------------------------------------------------
nyatla 0:142ee8b12fef 4 *
nyatla 0:142ee8b12fef 5 * This file is part of MiMic
nyatla 0:142ee8b12fef 6 * Copyright (C)2011 Ryo Iizuka
nyatla 0:142ee8b12fef 7 *
nyatla 0:142ee8b12fef 8 * MiMic is free software: you can redistribute it and/or modify
nyatla 0:142ee8b12fef 9 * it under the terms of the GNU Lesser General Public License as published
nyatla 0:142ee8b12fef 10 * by the Free Software Foundation, either version 3 of the License, or
nyatla 0:142ee8b12fef 11 * (at your option) any later version.
nyatla 0:142ee8b12fef 12 *
nyatla 0:142ee8b12fef 13 * This program is distributed in the hope that it will be useful,
nyatla 0:142ee8b12fef 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nyatla 0:142ee8b12fef 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
nyatla 0:142ee8b12fef 16 * GNU General Public License for more details.
nyatla 0:142ee8b12fef 17 *
nyatla 0:142ee8b12fef 18 * You should have received a copy of the GNU Lesser General Public License
nyatla 0:142ee8b12fef 19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
nyatla 0:142ee8b12fef 20 *
nyatla 0:142ee8b12fef 21 * For further information please contact.
nyatla 0:142ee8b12fef 22 * http://nyatla.jp/
nyatla 0:142ee8b12fef 23 * <airmail(at)ebony.plala.or.jp> or <nyatla(at)nyatla.jp>
nyatla 0:142ee8b12fef 24 *
nyatla 0:142ee8b12fef 25 *
nyatla 0:142ee8b12fef 26 * Parts of this file were leveraged from uIP:
nyatla 0:142ee8b12fef 27 *
nyatla 0:142ee8b12fef 28 * Copyright (c) 2001-2003, Adam Dunkels.
nyatla 0:142ee8b12fef 29 * All rights reserved.
nyatla 0:142ee8b12fef 30 *
nyatla 0:142ee8b12fef 31 * Redistribution and use in source and binary forms, with or without
nyatla 0:142ee8b12fef 32 * modification, are permitted provided that the following conditions
nyatla 0:142ee8b12fef 33 * are met:
nyatla 0:142ee8b12fef 34 * 1. Redistributions of source code must retain the above copyright
nyatla 0:142ee8b12fef 35 * notice, this list of conditions and the following disclaimer.
nyatla 0:142ee8b12fef 36 * 2. Redistributions in binary form must reproduce the above copyright
nyatla 0:142ee8b12fef 37 * notice, this list of conditions and the following disclaimer in the
nyatla 0:142ee8b12fef 38 * documentation and/or other materials provided with the distribution.
nyatla 0:142ee8b12fef 39 * 3. The name of the author may not be used to endorse or promote
nyatla 0:142ee8b12fef 40 * products derived from this software without specific prior
nyatla 0:142ee8b12fef 41 * written permission.
nyatla 0:142ee8b12fef 42 *
nyatla 0:142ee8b12fef 43 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
nyatla 0:142ee8b12fef 44 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
nyatla 0:142ee8b12fef 45 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
nyatla 0:142ee8b12fef 46 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
nyatla 0:142ee8b12fef 47 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
nyatla 0:142ee8b12fef 48 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
nyatla 0:142ee8b12fef 49 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
nyatla 0:142ee8b12fef 50 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
nyatla 0:142ee8b12fef 51 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
nyatla 0:142ee8b12fef 52 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
nyatla 0:142ee8b12fef 53 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
nyatla 0:142ee8b12fef 54 */
nyatla 37:fc4b4fd6a649 55 #include "NyLPC_cUipService_protected.h"
nyatla 2:b96c1e90d120 56 #include "NyLPC_cIPv4IComp_protected.h"
nyatla 0:142ee8b12fef 57 #include "NyLPC_cTcpListener_protected.h"
nyatla 0:142ee8b12fef 58 #include "NyLPC_stdlib.h"
nyatla 0:142ee8b12fef 59 #include "NyLPC_uip.h"
nyatla 0:142ee8b12fef 60
nyatla 2:b96c1e90d120 61
nyatla 2:b96c1e90d120 62
nyatla 2:b96c1e90d120 63
nyatla 0:142ee8b12fef 64
nyatla 0:142ee8b12fef 65 /****************************************************
nyatla 0:142ee8b12fef 66 * UipServiceに関する宣言:タスクメッセージ
nyatla 0:142ee8b12fef 67 ***************************************************/
nyatla 48:00d211aac2ec 68 /*
nyatla 0:142ee8b12fef 69 #define TTaskMsg_NOP 0
nyatla 0:142ee8b12fef 70 #define TTaskMsg_START 1
nyatla 0:142ee8b12fef 71 #define TTaskMsg_STOP 2
nyatla 48:00d211aac2ec 72 */
nyatla 0:142ee8b12fef 73 /**
nyatla 0:142ee8b12fef 74 * サービスの起動メッセージです。
nyatla 48:00d211aac2ec 75 *//*
nyatla 0:142ee8b12fef 76 struct TTaskMsg{
nyatla 0:142ee8b12fef 77 NyLPC_TUInt16 msg;
nyatla 0:142ee8b12fef 78 union{
nyatla 0:142ee8b12fef 79 struct{
nyatla 0:142ee8b12fef 80 const NyLPC_TcIPv4Config_t* ref_config;
nyatla 0:142ee8b12fef 81 }start;
nyatla 0:142ee8b12fef 82 };
nyatla 48:00d211aac2ec 83 };*/
nyatla 0:142ee8b12fef 84
nyatla 0:142ee8b12fef 85 /****************************************************
nyatla 0:142ee8b12fef 86 * UipServiceに関する宣言:その他
nyatla 0:142ee8b12fef 87 ***************************************************/
nyatla 0:142ee8b12fef 88 /**
nyatla 0:142ee8b12fef 89 * イーサネットフレームの読み出し構造体
nyatla 0:142ee8b12fef 90 */
nyatla 0:142ee8b12fef 91 struct TEthPacket
nyatla 0:142ee8b12fef 92 {
nyatla 0:142ee8b12fef 93 struct NyLPC_TEthernetIIHeader header;
nyatla 0:142ee8b12fef 94 union{
nyatla 0:142ee8b12fef 95 struct NyLPC_TArpHeader arp;
nyatla 0:142ee8b12fef 96 struct NyLPC_TIPv4Header ipv4;
nyatla 0:142ee8b12fef 97 }data;
nyatla 0:142ee8b12fef 98 };
nyatla 0:142ee8b12fef 99
nyatla 0:142ee8b12fef 100
nyatla 0:142ee8b12fef 101
nyatla 0:142ee8b12fef 102 /**
nyatla 0:142ee8b12fef 103 * サービスインスタンスのポインタ。サービスが稼働中はインスタンスのポインタが有効です。
nyatla 0:142ee8b12fef 104 */
nyatla 0:142ee8b12fef 105 NyLPC_TcUipService_t* _NyLPC_TcUipService_inst=NULL;
nyatla 0:142ee8b12fef 106
nyatla 0:142ee8b12fef 107 /**
nyatla 0:142ee8b12fef 108 * 唯一のインスタンス
nyatla 0:142ee8b12fef 109 */
nyatla 0:142ee8b12fef 110 static NyLPC_TcUipService_t _service_instance;
nyatla 0:142ee8b12fef 111
nyatla 0:142ee8b12fef 112
nyatla 0:142ee8b12fef 113
nyatla 0:142ee8b12fef 114
nyatla 0:142ee8b12fef 115 /**
nyatla 0:142ee8b12fef 116 * uipタスク
nyatla 0:142ee8b12fef 117 */
nyatla 0:142ee8b12fef 118 static int uipTask(void *pvParameters);
nyatla 0:142ee8b12fef 119
nyatla 0:142ee8b12fef 120 //--------------------------------------------------------------
nyatla 0:142ee8b12fef 121
nyatla 0:142ee8b12fef 122
nyatla 0:142ee8b12fef 123 static NyLPC_TBool sendIPv4Tx(struct NyLPC_TTxBufferHeader* i_eth_buf);
nyatla 0:142ee8b12fef 124 static void copyAndSendIPv4Tx(const struct TEthPacket* i_buf);
nyatla 0:142ee8b12fef 125 static void sendArpReqest(const struct TEthPacket* i_eth_packet);
nyatla 0:142ee8b12fef 126 static void sendRawEthFrame(void* i_buf,NyLPC_TUInt16 i_len);
nyatla 0:142ee8b12fef 127 static void emacIsrHandler(unsigned long i_status);
nyatla 0:142ee8b12fef 128
nyatla 0:142ee8b12fef 129 /**メッセージなし*/
nyatla 0:142ee8b12fef 130 #define TTaskMessage_MSG_NULL 0x0000
nyatla 0:142ee8b12fef 131 /**uipコアタスクに、開始要求する*/
nyatla 0:142ee8b12fef 132 #define TTaskMessage_MSG_RUN 0x0001
nyatla 0:142ee8b12fef 133 /**uipコアタスクに、停止要求する*/
nyatla 0:142ee8b12fef 134 #define TTaskMessage_MSG_STOP 0x0002
nyatla 0:142ee8b12fef 135
nyatla 0:142ee8b12fef 136
nyatla 0:142ee8b12fef 137 NyLPC_TcThread_t th;
nyatla 0:142ee8b12fef 138
nyatla 50:35e765380772 139 void led(int i);
nyatla 0:142ee8b12fef 140 NyLPC_TBool NyLPC_cUipService_initialize(void)
nyatla 0:142ee8b12fef 141 {
nyatla 0:142ee8b12fef 142 NyLPC_TcUipService_t* inst=&_service_instance;
nyatla 0:142ee8b12fef 143 //サービスは停止している事。 - Service must be uninitialized.
nyatla 0:142ee8b12fef 144 NyLPC_Assert(!NyLPC_TcUipService_isInitService());
nyatla 0:142ee8b12fef 145 //IP処理部分の初期化
nyatla 0:142ee8b12fef 146 NyLPC_cIPv4_initialize(&(inst->_tcpv4));
nyatla 0:142ee8b12fef 147 //EMACからの割込処理
nyatla 1:9f6a78395432 148 NyLPC_cIsr_setEnetISR(emacIsrHandler);
nyatla 0:142ee8b12fef 149 //EMAC割込セマフォ
nyatla 0:142ee8b12fef 150 NyLPC_cSemaphore_initialize(&inst->_emac_semapho);
nyatla 0:142ee8b12fef 151
nyatla 48:00d211aac2ec 152 inst->_status=0x00;
nyatla 0:142ee8b12fef 153 NyLPC_cStopwatch_initialize(&(inst->_arp_sw));
nyatla 0:142ee8b12fef 154 NyLPC_cStopwatch_initialize(&(inst->_periodic_sw));
nyatla 0:142ee8b12fef 155 NyLPC_cIPv4_initialize(&(inst->_tcpv4));
nyatla 0:142ee8b12fef 156 NyLPC_AbortIfNot(NyLPC_cMutex_initialize(&(inst->_mutex)));
nyatla 0:142ee8b12fef 157
nyatla 0:142ee8b12fef 158 _NyLPC_TcUipService_inst=inst;
nyatla 0:142ee8b12fef 159 //タスク起動
nyatla 0:142ee8b12fef 160 NyLPC_cThread_initialize(&th,NyLPC_TcUipService_config_STACK_SIZE,NyLPC_TcThread_PRIORITY_SERVICE);
nyatla 0:142ee8b12fef 161 NyLPC_cThread_start(&th,uipTask,NULL);
nyatla 0:142ee8b12fef 162 return NyLPC_TBool_TRUE;
nyatla 0:142ee8b12fef 163 }
nyatla 0:142ee8b12fef 164
nyatla 0:142ee8b12fef 165
nyatla 0:142ee8b12fef 166
nyatla 0:142ee8b12fef 167
nyatla 0:142ee8b12fef 168
nyatla 0:142ee8b12fef 169
nyatla 0:142ee8b12fef 170
nyatla 0:142ee8b12fef 171 /**
nyatla 0:142ee8b12fef 172 * UIP処理を開始します。
nyatla 0:142ee8b12fef 173 * この関数はリエントラントではありません。複数のタスクから共有するときには、排他ロックを掛けてください。
nyatla 0:142ee8b12fef 174 * @param i_ref_config
nyatla 0:142ee8b12fef 175 * このコンフィギュレーションは、stopを実行するまでの間、インスタンスから参照します。外部で保持してください。
nyatla 0:142ee8b12fef 176 */
nyatla 0:142ee8b12fef 177 void NyLPC_cUipService_start(const NyLPC_TcIPv4Config_t* i_ref_config)
nyatla 0:142ee8b12fef 178 {
nyatla 0:142ee8b12fef 179 NyLPC_TcUipService_t* inst=&_service_instance;
nyatla 0:142ee8b12fef 180 NyLPC_Assert(NyLPC_TcUipService_isInitService());
nyatla 0:142ee8b12fef 181 if(!NyLPC_cUipService_isRun())
nyatla 0:142ee8b12fef 182 {
nyatla 0:142ee8b12fef 183 //はじめて起動するときに1度だけデバイス取得(タスクスイッチが動いてないと動かないからここで。)
nyatla 0:142ee8b12fef 184 if(inst->_ethif==NULL){
nyatla 0:142ee8b12fef 185 inst->_ethif=getEthernetDevicePnP();
nyatla 0:142ee8b12fef 186 }
nyatla 48:00d211aac2ec 187 //コンフィグレーションセット
nyatla 48:00d211aac2ec 188 inst->_ref_config=i_ref_config;
nyatla 48:00d211aac2ec 189 //開始要求セット
nyatla 48:00d211aac2ec 190 NyLPC_TUInt16_setBit(inst->_status,NyLPC_TcUipService_ORDER_START);
nyatla 48:00d211aac2ec 191 //Order実行待ち
nyatla 48:00d211aac2ec 192 while(NyLPC_TUInt16_isBitOn(inst->_status,NyLPC_TcUipService_ORDER_START)){
nyatla 0:142ee8b12fef 193 NyLPC_cThread_sleep(10);
nyatla 0:142ee8b12fef 194 }
nyatla 0:142ee8b12fef 195 }
nyatla 0:142ee8b12fef 196 return;
nyatla 0:142ee8b12fef 197 }
nyatla 0:142ee8b12fef 198 /**
nyatla 0:142ee8b12fef 199 * UIP処理を停止します。
nyatla 0:142ee8b12fef 200 * この関数はリエントラントではありません。複数のタスクから共有するときには、排他ロックを掛けてください。
nyatla 0:142ee8b12fef 201 * いまのところ、ストップシーケンスの実装は良くありません。
nyatla 0:142ee8b12fef 202 * 再設計が必要。
nyatla 0:142ee8b12fef 203 */
nyatla 0:142ee8b12fef 204 void NyLPC_cUipService_stop(void)
nyatla 0:142ee8b12fef 205 {
nyatla 0:142ee8b12fef 206 NyLPC_TcUipService_t* inst=&_service_instance;
nyatla 0:142ee8b12fef 207 NyLPC_Assert(NyLPC_TcUipService_isInitService());
nyatla 0:142ee8b12fef 208 if(NyLPC_cUipService_isRun())
nyatla 0:142ee8b12fef 209 {
nyatla 48:00d211aac2ec 210 NyLPC_TUInt16_setBit(inst->_status,NyLPC_TcUipService_ORDER_STOP);
nyatla 48:00d211aac2ec 211 //Order実行待ち
nyatla 48:00d211aac2ec 212 while(NyLPC_TUInt16_isBitOn(inst->_status,NyLPC_TcUipService_ORDER_STOP)){
nyatla 0:142ee8b12fef 213 NyLPC_cThread_sleep(10);
nyatla 0:142ee8b12fef 214 }
nyatla 0:142ee8b12fef 215 }
nyatla 0:142ee8b12fef 216 return;
nyatla 0:142ee8b12fef 217 }
nyatla 0:142ee8b12fef 218
nyatla 0:142ee8b12fef 219
nyatla 0:142ee8b12fef 220 const char* NyLPC_cUipService_refDeviceName(void)
nyatla 0:142ee8b12fef 221 {
nyatla 0:142ee8b12fef 222 NyLPC_TcUipService_t* inst=&_service_instance;
nyatla 0:142ee8b12fef 223 return NyLPC_cUipService_isRun()?inst->_ethif->device_name:NULL;
nyatla 0:142ee8b12fef 224 }
nyatla 21:f2a70f15301f 225 const NyLPC_TcIPv4Config_t* NyLPC_cUipService_refCurrentConfig(void)
nyatla 21:f2a70f15301f 226 {
nyatla 21:f2a70f15301f 227 NyLPC_TcUipService_t* inst=&_service_instance;
nyatla 21:f2a70f15301f 228 return inst->_ref_config;
nyatla 21:f2a70f15301f 229 }
nyatla 0:142ee8b12fef 230 /**********************************************************************
nyatla 0:142ee8b12fef 231 *
nyatla 0:142ee8b12fef 232 * </HWコールバックに関わる宣言>
nyatla 0:142ee8b12fef 233 *
nyatla 0:142ee8b12fef 234 *********************************************************************/
nyatla 0:142ee8b12fef 235
nyatla 0:142ee8b12fef 236 /**
nyatla 0:142ee8b12fef 237 * EMACからのハンドラ
nyatla 0:142ee8b12fef 238 */
nyatla 0:142ee8b12fef 239 static void emacIsrHandler(unsigned long i_status)
nyatla 0:142ee8b12fef 240 {
nyatla 0:142ee8b12fef 241 NyLPC_TcUipService_t* inst=&_service_instance;
nyatla 0:142ee8b12fef 242
nyatla 0:142ee8b12fef 243 if( i_status & INT_RX_DONE )
nyatla 0:142ee8b12fef 244 {
nyatla 0:142ee8b12fef 245 //受信系のセマフォブロックの解除
nyatla 0:142ee8b12fef 246 NyLPC_cSemaphore_giveFromISR(&inst->_emac_semapho);
nyatla 0:142ee8b12fef 247 }
nyatla 0:142ee8b12fef 248 if( i_status & INT_TX_DONE )
nyatla 0:142ee8b12fef 249 {
nyatla 0:142ee8b12fef 250 }
nyatla 0:142ee8b12fef 251 }
nyatla 0:142ee8b12fef 252
nyatla 0:142ee8b12fef 253
nyatla 0:142ee8b12fef 254 //PERIODIC rate
nyatla 0:142ee8b12fef 255 #define PERIODIC_TIMER (1*200)
nyatla 0:142ee8b12fef 256 #define ARP_TIMER (60*1000*10)
nyatla 0:142ee8b12fef 257
nyatla 37:fc4b4fd6a649 258
nyatla 37:fc4b4fd6a649 259
nyatla 0:142ee8b12fef 260 /**
nyatla 0:142ee8b12fef 261 * 操作キューを確認して、タスクのステータスをアップデートします。
nyatla 0:142ee8b12fef 262 * 高速化のため、Proc-Callerを使用していません。複雑なタスク操作をするときには、書き換えてください。
nyatla 48:00d211aac2ec 263 * @return
nyatla 48:00d211aac2ec 264 * UIPタスクの実行状態
nyatla 0:142ee8b12fef 265 */
nyatla 48:00d211aac2ec 266 static NyLPC_TBool updateTaskStatus()
nyatla 0:142ee8b12fef 267 {
nyatla 0:142ee8b12fef 268 NyLPC_TcUipService_t* inst=_NyLPC_TcUipService_inst;
nyatla 48:00d211aac2ec 269 if(NyLPC_cUipService_isRun()){
nyatla 48:00d211aac2ec 270 //開始状態
nyatla 48:00d211aac2ec 271 if(NyLPC_TUInt16_isBitOn(inst->_status,NyLPC_TcUipService_ORDER_STOP))
nyatla 48:00d211aac2ec 272 {
nyatla 48:00d211aac2ec 273 //停止操作
nyatla 48:00d211aac2ec 274 inst->_ethif->stop();
nyatla 48:00d211aac2ec 275 NyLPC_cIPv4_stop(&(inst->_tcpv4));
nyatla 48:00d211aac2ec 276 NyLPC_cIPv4IComp_finalize(&(inst->_icomp));
nyatla 48:00d211aac2ec 277 NyLPC_cIPv4Arp_finalize(&(inst->_arp));
nyatla 48:00d211aac2ec 278 inst->_ref_config=NULL;
nyatla 48:00d211aac2ec 279 NyLPC_TUInt16_unsetBit(inst->_status,NyLPC_TcUipService_STATUSBIT_IS_RUNNING);
nyatla 48:00d211aac2ec 280 NyLPC_TUInt16_unsetBit(inst->_status,NyLPC_TcUipService_ORDER_STOP);
nyatla 48:00d211aac2ec 281 return NyLPC_TBool_FALSE;
nyatla 0:142ee8b12fef 282 }
nyatla 48:00d211aac2ec 283 return NyLPC_TBool_TRUE;
nyatla 48:00d211aac2ec 284 }else{
nyatla 48:00d211aac2ec 285 //停止状態
nyatla 48:00d211aac2ec 286 if(NyLPC_TUInt16_isBitOn(inst->_status,NyLPC_TcUipService_ORDER_START))
nyatla 48:00d211aac2ec 287 {
nyatla 48:00d211aac2ec 288 //TCP,ICOMPの初期化
nyatla 48:00d211aac2ec 289 NyLPC_cIPv4_start(&(inst->_tcpv4),inst->_ref_config);
nyatla 48:00d211aac2ec 290 NyLPC_cIPv4IComp_initialize(&(inst->_icomp),inst->_ref_config);
nyatla 48:00d211aac2ec 291 //uip_arp_init(msg->start.ref_config);
nyatla 48:00d211aac2ec 292 NyLPC_cIPv4Arp_initialize(&(inst->_arp),inst->_ref_config);
nyatla 48:00d211aac2ec 293 NyLPC_cStopwatch_startExpire(&(inst->_arp_sw),1);//1度ARPを起動するため。
nyatla 48:00d211aac2ec 294 NyLPC_cStopwatch_startExpire(&(inst->_periodic_sw),PERIODIC_TIMER);
nyatla 48:00d211aac2ec 295 //InBuffer初期化
nyatla 48:00d211aac2ec 296 inst->stx.h.is_lock=NyLPC_TUInt8_FALSE;
nyatla 48:00d211aac2ec 297 inst->stx.h.ref=0;
nyatla 48:00d211aac2ec 298 //EtherNETデバイス初期化
nyatla 48:00d211aac2ec 299 while(!inst->_ethif->start(&(inst->_ref_config->eth_mac)));
nyatla 48:00d211aac2ec 300 NyLPC_TUInt16_setBit(inst->_status,NyLPC_TcUipService_STATUSBIT_IS_RUNNING);
nyatla 48:00d211aac2ec 301 NyLPC_TUInt16_unsetBit(inst->_status,NyLPC_TcUipService_ORDER_START);
nyatla 48:00d211aac2ec 302 return NyLPC_TBool_TRUE;
nyatla 0:142ee8b12fef 303 }
nyatla 48:00d211aac2ec 304 return NyLPC_TBool_FALSE;
nyatla 0:142ee8b12fef 305 }
nyatla 0:142ee8b12fef 306 }
nyatla 0:142ee8b12fef 307
nyatla 0:142ee8b12fef 308 /**
nyatla 0:142ee8b12fef 309 * uipタスクのメインループ
nyatla 0:142ee8b12fef 310 */
nyatla 0:142ee8b12fef 311 static int uipTask(void *pvParameters)
nyatla 0:142ee8b12fef 312 {
nyatla 0:142ee8b12fef 313 NyLPC_TUInt16 rx_len,tx_len;
nyatla 0:142ee8b12fef 314 struct TEthPacket* ethbuf;
nyatla 0:142ee8b12fef 315 NyLPC_TcUipService_t* inst=_NyLPC_TcUipService_inst;
nyatla 0:142ee8b12fef 316 NyLPC_TBool r;
nyatla 0:142ee8b12fef 317 (void)pvParameters;
nyatla 0:142ee8b12fef 318 for( ;; )
nyatla 0:142ee8b12fef 319 {
nyatla 0:142ee8b12fef 320 //タスク状態の更新
nyatla 48:00d211aac2ec 321 if(!updateTaskStatus())
nyatla 0:142ee8b12fef 322 {
nyatla 0:142ee8b12fef 323 //RUNステータス以外の時は、ここで終了する。
nyatla 0:142ee8b12fef 324 NyLPC_cThread_sleep(50);
nyatla 0:142ee8b12fef 325 continue;
nyatla 0:142ee8b12fef 326 }
nyatla 0:142ee8b12fef 327 //イーサネットフレームの取得
nyatla 0:142ee8b12fef 328 //Ethernet Device Lock(ARPを含む)
nyatla 2:b96c1e90d120 329 NyLPC_cMutex_lock(&(inst->_mutex));
nyatla 0:142ee8b12fef 330 ethbuf= (struct TEthPacket*)inst->_ethif->getRxEthFrame(&rx_len);
nyatla 0:142ee8b12fef 331 tx_len=0;
nyatla 0:142ee8b12fef 332 while(ethbuf != NULL){
nyatla 0:142ee8b12fef 333 if(rx_len>0)
nyatla 0:142ee8b12fef 334 {
nyatla 0:142ee8b12fef 335 //ペイロードサイズを計算
nyatla 0:142ee8b12fef 336 rx_len-=sizeof(struct NyLPC_TEthernetIIHeader);
nyatla 0:142ee8b12fef 337 switch(ethbuf->header.type)
nyatla 0:142ee8b12fef 338 {
nyatla 0:142ee8b12fef 339 case NyLPC_HTONS(NyLPC_TEthernetIIHeader_TYPE_IP):
nyatla 0:142ee8b12fef 340 //ARPテーブルの更新
nyatla 0:142ee8b12fef 341 //uip_arp_ipin(&(ethbuf->header),ethbuf->data.ipv4.srcipaddr);
nyatla 0:142ee8b12fef 342 NyLPC_cIPv4Arp_incomingIp(&inst->_arp,&(ethbuf->header),ethbuf->data.ipv4.srcipaddr);
nyatla 0:142ee8b12fef 343 //Ethernet Device UnLock(パケット解析の為に一時的な解除)
nyatla 0:142ee8b12fef 344 NyLPC_cMutex_unlock(&(inst->_mutex));
nyatla 0:142ee8b12fef 345 //IPパケットの処理
nyatla 0:142ee8b12fef 346 r=NyLPC_cIPv4_rx(&(inst->_tcpv4),&(ethbuf->data.ipv4),rx_len);
nyatla 0:142ee8b12fef 347 //ロックの復帰
nyatla 0:142ee8b12fef 348 NyLPC_cMutex_lock(&(inst->_mutex));
nyatla 0:142ee8b12fef 349 if(!r){
nyatla 0:142ee8b12fef 350 //応答データは存在しない。
nyatla 0:142ee8b12fef 351 break;
nyatla 0:142ee8b12fef 352 }
nyatla 0:142ee8b12fef 353 //IPパケットをTxバッファに転写して送信
nyatla 0:142ee8b12fef 354 copyAndSendIPv4Tx(ethbuf);
nyatla 0:142ee8b12fef 355 ethbuf=NULL;
nyatla 0:142ee8b12fef 356 break;
nyatla 0:142ee8b12fef 357 case NyLPC_HTONS(NyLPC_TEthernetIIHeader_TYPE_ARP):
nyatla 0:142ee8b12fef 358 //if(uip_arp_arpin(&(ethbuf->data.arp),rx_len)){
nyatla 0:142ee8b12fef 359 if(NyLPC_cIPv4Arp_incomingArp(&inst->_arp,&(ethbuf->data.arp),rx_len)){
nyatla 0:142ee8b12fef 360 tx_len=NyLPC_TEthernetIIHeader_setArpTx(&(ethbuf->header),&(inst->_ref_config->eth_mac));
nyatla 0:142ee8b12fef 361 }
nyatla 0:142ee8b12fef 362 if(tx_len>0){
nyatla 0:142ee8b12fef 363 sendRawEthFrame(ethbuf,tx_len);
nyatla 0:142ee8b12fef 364 }
nyatla 0:142ee8b12fef 365 break;
nyatla 0:142ee8b12fef 366 case NyLPC_HTONS(NyLPC_TEthernetIIHeader_TYPE_IPV6):
nyatla 0:142ee8b12fef 367 //uip_process_ipv6();
nyatla 0:142ee8b12fef 368 break;
nyatla 0:142ee8b12fef 369 default:
nyatla 0:142ee8b12fef 370 break;
nyatla 0:142ee8b12fef 371 }
nyatla 0:142ee8b12fef 372 }
nyatla 2:b96c1e90d120 373 //受信キューを進行。
nyatla 0:142ee8b12fef 374 inst->_ethif->nextRxEthFrame();
nyatla 0:142ee8b12fef 375 //受信処理
nyatla 0:142ee8b12fef 376 ethbuf= (struct TEthPacket*)inst->_ethif->getRxEthFrame(&rx_len);
nyatla 0:142ee8b12fef 377 }
nyatla 0:142ee8b12fef 378 //データが無い。
nyatla 0:142ee8b12fef 379 if(NyLPC_cStopwatch_isExpired(&(inst->_arp_sw))){
nyatla 0:142ee8b12fef 380 //uip_arp_timer();
nyatla 0:142ee8b12fef 381 NyLPC_cIPv4Arp_periodic(&inst->_arp);
nyatla 0:142ee8b12fef 382 NyLPC_cStopwatch_startExpire(&(inst->_arp_sw),ARP_TIMER);
nyatla 2:b96c1e90d120 383 }
nyatla 0:142ee8b12fef 384 if(NyLPC_cStopwatch_isExpired(&(inst->_periodic_sw))){
nyatla 37:fc4b4fd6a649 385 NyLPC_cMutex_unlock(&(inst->_mutex));
nyatla 0:142ee8b12fef 386 NyLPC_cIPv4_periodec(&(inst->_tcpv4));
nyatla 37:fc4b4fd6a649 387 NyLPC_cMutex_lock(&(inst->_mutex));
nyatla 0:142ee8b12fef 388 NyLPC_cStopwatch_startExpire(&(inst->_periodic_sw),PERIODIC_TIMER);
nyatla 0:142ee8b12fef 389 }
nyatla 0:142ee8b12fef 390 //リソースロックの解除
nyatla 0:142ee8b12fef 391 NyLPC_cMutex_unlock(&(inst->_mutex));
nyatla 0:142ee8b12fef 392 //割込によるセマフォの解除か、タイムアウトで再開する。(タイムアウト値は周期関数の実行レート以下にすること。)
nyatla 0:142ee8b12fef 393 NyLPC_cSemaphore_take(&(_NyLPC_TcUipService_inst->_emac_semapho),PERIODIC_TIMER);
nyatla 0:142ee8b12fef 394 }
nyatla 0:142ee8b12fef 395 return 0;
nyatla 0:142ee8b12fef 396 }
nyatla 0:142ee8b12fef 397
nyatla 0:142ee8b12fef 398
nyatla 0:142ee8b12fef 399
nyatla 0:142ee8b12fef 400
nyatla 0:142ee8b12fef 401 /**
nyatla 0:142ee8b12fef 402 * allocTxBufで取得したメモリを"IPパケットとして"送信します。
nyatla 0:142ee8b12fef 403 * @param i_eth_payload
nyatla 0:142ee8b12fef 404 * [NyLPC_TTxBufferHeader][NyLPC_TEthernetIIHeader][payload]メモリの、[payload]のアドレスを指定します。
nyatla 0:142ee8b12fef 405 * 通常は、NyLPC_cUipService_allocTxBufの返却したメモリを指定します。
nyatla 0:142ee8b12fef 406 */
nyatla 0:142ee8b12fef 407
nyatla 0:142ee8b12fef 408 void NyLPC_cUipService_sendIPv4Tx(void* i_eth_payload)
nyatla 0:142ee8b12fef 409 {
nyatla 0:142ee8b12fef 410 NyLPC_TcUipService_t* inst=_NyLPC_TcUipService_inst;
nyatla 0:142ee8b12fef 411 NyLPC_cMutex_lock(&(inst->_mutex));
nyatla 0:142ee8b12fef 412 //IPパケットの送信を試行
nyatla 0:142ee8b12fef 413 if(!sendIPv4Tx(((struct NyLPC_TTxBufferHeader*)(((struct NyLPC_TEthernetIIHeader*)i_eth_payload)-1))-1)){
nyatla 0:142ee8b12fef 414 //ARPリクエストを代わりに送信
nyatla 0:142ee8b12fef 415 sendArpReqest(((struct TEthPacket*)i_eth_payload)-1);
nyatla 0:142ee8b12fef 416 }
nyatla 0:142ee8b12fef 417 NyLPC_cMutex_unlock(&(inst->_mutex));
nyatla 0:142ee8b12fef 418 return;
nyatla 0:142ee8b12fef 419 }
nyatla 37:fc4b4fd6a649 420 /**
nyatla 37:fc4b4fd6a649 421 * 指定したIPアドレスに対してARPRequestを送信します。
nyatla 37:fc4b4fd6a649 422 */
nyatla 37:fc4b4fd6a649 423 void NyLPC_cUipService_sendArpRequest(const struct NyLPC_TIPv4Addr* i_addr)
nyatla 37:fc4b4fd6a649 424 {
nyatla 37:fc4b4fd6a649 425 NyLPC_TcUipService_t* inst=_NyLPC_TcUipService_inst;
nyatla 37:fc4b4fd6a649 426 NyLPC_TUInt16 tx_len;
nyatla 37:fc4b4fd6a649 427 struct TEthPacket* ethbuf;
nyatla 37:fc4b4fd6a649 428 //ACK送信用の自己バッファが空くまで待つ
nyatla 37:fc4b4fd6a649 429 while(inst->stx.h.is_lock){
nyatla 37:fc4b4fd6a649 430 inst->_ethif->processTx();
nyatla 37:fc4b4fd6a649 431 }
nyatla 37:fc4b4fd6a649 432 //ARPパケットを作る。
nyatla 37:fc4b4fd6a649 433 ethbuf=(struct TEthPacket*)(inst->stx.buf);
nyatla 37:fc4b4fd6a649 434 NyLPC_TArpHeader_setArpRequest(&(ethbuf->data.arp),inst->_ref_config->ip_addr,&(inst->_ref_config->eth_mac),*i_addr);
nyatla 37:fc4b4fd6a649 435 tx_len=NyLPC_TEthernetIIHeader_setArpTx(&(ethbuf->header),&(inst->_ref_config->eth_mac));
nyatla 37:fc4b4fd6a649 436 //送信
nyatla 37:fc4b4fd6a649 437 inst->_ethif->sendTxEthFrame(&(inst->stx.h),tx_len);
nyatla 37:fc4b4fd6a649 438 return;
nyatla 37:fc4b4fd6a649 439 }
nyatla 0:142ee8b12fef 440
nyatla 37:fc4b4fd6a649 441 /**
nyatla 37:fc4b4fd6a649 442 * ARPテーブルに指定したIPがあるかを返します。
nyatla 37:fc4b4fd6a649 443 */
nyatla 37:fc4b4fd6a649 444 NyLPC_TBool NyLPC_cUipService_hasArpInfo(const struct NyLPC_TIPv4Addr* i_addr)
nyatla 37:fc4b4fd6a649 445 {
nyatla 37:fc4b4fd6a649 446 NyLPC_TcUipService_t* inst=_NyLPC_TcUipService_inst;
nyatla 37:fc4b4fd6a649 447 return NyLPC_cIPv4Arp_IPv4toEthAddr(&inst->_arp,*i_addr)!=NULL;
nyatla 37:fc4b4fd6a649 448 }
nyatla 0:142ee8b12fef 449
nyatla 0:142ee8b12fef 450
nyatla 0:142ee8b12fef 451 /**
nyatla 0:142ee8b12fef 452 * 送信ペイロードメモリを返します。
nyatla 0:142ee8b12fef 453 * この関数は、リエントラントを許容します。
nyatla 0:142ee8b12fef 454 * @param i_hint
nyatla 0:142ee8b12fef 455 * 取得したいメモリサイズを指定します。(このサイズは、イーサネットヘッダのサイズを含みません。)
nyatla 0:142ee8b12fef 456 * このサイズよりも小さなサイズが割り当てられることがあります。
nyatla 37:fc4b4fd6a649 457 * @param o_size
nyatla 37:fc4b4fd6a649 458 * イーサネットヘッダを除いたペイロード部分の長さ
nyatla 0:142ee8b12fef 459 * @return
nyatla 20:3b0b444b4deb 460 * 成功:IPペイロードのためのメモリブロックを返します。/失敗:NULL
nyatla 37:fc4b4fd6a649 461 * 返されるメモリはブロックの[TEthPacket][payload]の構造で、[payload]のアドレスが返されます。
nyatla 0:142ee8b12fef 462 */
nyatla 0:142ee8b12fef 463 void* NyLPC_cUipService_allocTxBuf(NyLPC_TUInt16 i_hint,NyLPC_TUInt16* o_size)
nyatla 0:142ee8b12fef 464 {
nyatla 0:142ee8b12fef 465 NyLPC_TcUipService_t* inst=_NyLPC_TcUipService_inst;
nyatla 0:142ee8b12fef 466 struct NyLPC_TTxBufferHeader* ethbuf;
nyatla 37:fc4b4fd6a649 467 //排他処理をして、メモリを取得する。
nyatla 0:142ee8b12fef 468 NyLPC_cMutex_lock(&(inst->_mutex));
nyatla 0:142ee8b12fef 469 ethbuf=(struct NyLPC_TTxBufferHeader*)inst->_ethif->allocTxBuf(i_hint+sizeof(struct NyLPC_TEthernetIIHeader),o_size);
nyatla 0:142ee8b12fef 470 NyLPC_cMutex_unlock(&(inst->_mutex));
nyatla 20:3b0b444b4deb 471 if(ethbuf==NULL){
nyatla 20:3b0b444b4deb 472 return NULL;
nyatla 0:142ee8b12fef 473 }
nyatla 20:3b0b444b4deb 474 //イーサネットバッファのサイズを計算
nyatla 20:3b0b444b4deb 475 *o_size-=sizeof(struct NyLPC_TEthernetIIHeader);
nyatla 20:3b0b444b4deb 476 //イーサネットバッファのアドレスを計算
nyatla 20:3b0b444b4deb 477 return &(((struct TEthPacket*)(ethbuf+1))->data);
nyatla 0:142ee8b12fef 478 }
nyatla 0:142ee8b12fef 479
nyatla 0:142ee8b12fef 480
nyatla 0:142ee8b12fef 481 void* NyLPC_cUipService_releaseTxBuf(void* i_buf)
nyatla 0:142ee8b12fef 482 {
nyatla 0:142ee8b12fef 483 //排他処理をして、メモリを開放する。
nyatla 0:142ee8b12fef 484 NyLPC_TcUipService_t* inst=_NyLPC_TcUipService_inst;
nyatla 0:142ee8b12fef 485 NyLPC_cMutex_lock(&(inst->_mutex));
nyatla 0:142ee8b12fef 486 //ペイロードの位置から、メモリブロックを再生。
nyatla 0:142ee8b12fef 487 inst->_ethif->releaseTxBuf(((struct NyLPC_TTxBufferHeader*)(((struct NyLPC_TEthernetIIHeader*)i_buf)-1))-1);
nyatla 0:142ee8b12fef 488 NyLPC_cMutex_unlock(&(inst->_mutex));
nyatla 0:142ee8b12fef 489 return NULL;
nyatla 0:142ee8b12fef 490 }
nyatla 0:142ee8b12fef 491
nyatla 0:142ee8b12fef 492
nyatla 0:142ee8b12fef 493
nyatla 0:142ee8b12fef 494
nyatla 0:142ee8b12fef 495
nyatla 0:142ee8b12fef 496
nyatla 0:142ee8b12fef 497
nyatla 0:142ee8b12fef 498
nyatla 0:142ee8b12fef 499 /**********
nyatla 0:142ee8b12fef 500 * イーサネットHWのコントロール関数
nyatla 0:142ee8b12fef 501 */
nyatla 0:142ee8b12fef 502
nyatla 0:142ee8b12fef 503 /**
nyatla 0:142ee8b12fef 504 * 新たにメモリを確保して、"IPv4パケットを格納した"イーサフレームを送信します。
nyatla 0:142ee8b12fef 505 * コール前に、必ずロックしてから呼び出してください。
nyatla 0:142ee8b12fef 506 */
nyatla 0:142ee8b12fef 507 static void copyAndSendIPv4Tx(const struct TEthPacket* i_buf)
nyatla 0:142ee8b12fef 508 {
nyatla 0:142ee8b12fef 509 NyLPC_TcUipService_t* inst=_NyLPC_TcUipService_inst;
nyatla 0:142ee8b12fef 510 NyLPC_TUInt16 s;
nyatla 0:142ee8b12fef 511 //ACK送信用の自己バッファが空くまで待つ
nyatla 0:142ee8b12fef 512 while(inst->stx.h.is_lock){
nyatla 0:142ee8b12fef 513 inst->_ethif->processTx();
nyatla 0:142ee8b12fef 514 }
nyatla 0:142ee8b12fef 515 //送信する。
nyatla 0:142ee8b12fef 516 s=NyLPC_htons(i_buf->data.ipv4.len16)+sizeof(struct NyLPC_TEthernetIIHeader);
nyatla 0:142ee8b12fef 517 memcpy(inst->stx.buf,i_buf,s);
nyatla 0:142ee8b12fef 518 if(!sendIPv4Tx(&(inst->stx.h))){
nyatla 0:142ee8b12fef 519 //失敗した場合はARPリクエストに変換して再送
nyatla 0:142ee8b12fef 520 sendArpReqest(i_buf);
nyatla 0:142ee8b12fef 521 }
nyatla 0:142ee8b12fef 522 return;
nyatla 0:142ee8b12fef 523 }
nyatla 0:142ee8b12fef 524 /**
nyatla 0:142ee8b12fef 525 * IPv4パケットのpeerIPを問い合わせるARPパケットを送信します。
nyatla 0:142ee8b12fef 526 */
nyatla 0:142ee8b12fef 527 static void sendArpReqest(const struct TEthPacket* i_eth_packet)
nyatla 0:142ee8b12fef 528 {
nyatla 0:142ee8b12fef 529 NyLPC_TcUipService_t* inst=_NyLPC_TcUipService_inst;
nyatla 0:142ee8b12fef 530 NyLPC_TUInt16 tx_len;
nyatla 0:142ee8b12fef 531 struct TEthPacket* ethbuf;
nyatla 0:142ee8b12fef 532 //ACK送信用の自己バッファが空くまで待つ
nyatla 0:142ee8b12fef 533 while(inst->stx.h.is_lock){
nyatla 0:142ee8b12fef 534 inst->_ethif->processTx();
nyatla 0:142ee8b12fef 535 }
nyatla 0:142ee8b12fef 536 //ARPパケットを作る。
nyatla 0:142ee8b12fef 537 ethbuf=(struct TEthPacket*)(inst->stx.buf);
nyatla 0:142ee8b12fef 538 NyLPC_TArpHeader_setArpRequest(&(ethbuf->data.arp),inst->_ref_config->ip_addr,&(inst->_ref_config->eth_mac),i_eth_packet->data.ipv4.destipaddr);
nyatla 0:142ee8b12fef 539 tx_len=NyLPC_TEthernetIIHeader_setArpTx(&(ethbuf->header),&(inst->_ref_config->eth_mac));
nyatla 0:142ee8b12fef 540 //送信
nyatla 0:142ee8b12fef 541 inst->_ethif->sendTxEthFrame(&(inst->stx.h),tx_len);
nyatla 0:142ee8b12fef 542 }
nyatla 0:142ee8b12fef 543
nyatla 0:142ee8b12fef 544 /**
nyatla 0:142ee8b12fef 545 * uipタスクが所有するTXバッファを使用してデータを送信します。
nyatla 0:142ee8b12fef 546 * この関数は、i_bufをコピーします。
nyatla 0:142ee8b12fef 547 * @i_buf
nyatla 0:142ee8b12fef 548 * イーサネットフレームを格納したメモリです。
nyatla 0:142ee8b12fef 549 * @i_len
nyatla 0:142ee8b12fef 550 * イーサネットペイロードのサイズです。
nyatla 0:142ee8b12fef 551 */
nyatla 0:142ee8b12fef 552 static void sendRawEthFrame(void* i_buf,NyLPC_TUInt16 i_len)
nyatla 0:142ee8b12fef 553 {
nyatla 0:142ee8b12fef 554 NyLPC_TcUipService_t* inst=_NyLPC_TcUipService_inst;
nyatla 0:142ee8b12fef 555
nyatla 0:142ee8b12fef 556 //ACK送信用の自己バッファが空くまで待つ
nyatla 0:142ee8b12fef 557 while(inst->stx.h.is_lock){
nyatla 0:142ee8b12fef 558 inst->_ethif->processTx();
nyatla 0:142ee8b12fef 559 }
nyatla 0:142ee8b12fef 560 //64バイトを超えるとかありえない。
nyatla 0:142ee8b12fef 561 if(i_len+sizeof(struct NyLPC_TEthernetIIHeader)>NyLPC_TcUipService_SIZE_OF_REPLY_BUF){
nyatla 0:142ee8b12fef 562 return;
nyatla 0:142ee8b12fef 563 }
nyatla 0:142ee8b12fef 564 //送信する。
nyatla 0:142ee8b12fef 565 memcpy(inst->stx.buf,i_buf,i_len);
nyatla 0:142ee8b12fef 566 inst->_ethif->sendTxEthFrame(&(inst->stx.h),i_len);
nyatla 0:142ee8b12fef 567 return;
nyatla 0:142ee8b12fef 568 }
nyatla 0:142ee8b12fef 569
nyatla 37:fc4b4fd6a649 570 /**
nyatla 37:fc4b4fd6a649 571 * マルチキャスとアドレスへ変換する。
nyatla 37:fc4b4fd6a649 572 */
nyatla 37:fc4b4fd6a649 573 static void ip2MulticastEmacAddr(const struct NyLPC_TIPv4Addr* i_addr,struct NyLPC_TEthAddr* o_emac)
nyatla 37:fc4b4fd6a649 574 {
nyatla 37:fc4b4fd6a649 575 NyLPC_TUInt32 n=NyLPC_htonl(i_addr->v);
nyatla 37:fc4b4fd6a649 576 o_emac->addr[0]=0x01;
nyatla 37:fc4b4fd6a649 577 o_emac->addr[1]=0x00;
nyatla 37:fc4b4fd6a649 578 o_emac->addr[2]=0x5E;
nyatla 37:fc4b4fd6a649 579 o_emac->addr[3]=((n>>16) & 0x7f);
nyatla 37:fc4b4fd6a649 580 o_emac->addr[4]=((n>> 8) & 0xff);
nyatla 37:fc4b4fd6a649 581 o_emac->addr[5]=(n & 0xff);
nyatla 37:fc4b4fd6a649 582 return;
nyatla 37:fc4b4fd6a649 583 };
nyatla 0:142ee8b12fef 584
nyatla 0:142ee8b12fef 585 /**
nyatla 0:142ee8b12fef 586 * ペイロードをIPパケットとしてネットワークへ送出します。
nyatla 0:142ee8b12fef 587 * コール前に、必ずロックしてから呼び出してください。
nyatla 0:142ee8b12fef 588 * @param i_eth_payload
nyatla 0:142ee8b12fef 589 * allocTxBufで確保したメモリを指定してください。
nyatla 0:142ee8b12fef 590 * ペイロードには、TCP/IPパケットを格納します。
nyatla 0:142ee8b12fef 591 */
nyatla 0:142ee8b12fef 592 static NyLPC_TBool sendIPv4Tx(struct NyLPC_TTxBufferHeader* i_eth_buf)
nyatla 0:142ee8b12fef 593 {
nyatla 0:142ee8b12fef 594 NyLPC_TcUipService_t* inst=_NyLPC_TcUipService_inst;
nyatla 37:fc4b4fd6a649 595 struct NyLPC_TEthAddr emac;
nyatla 0:142ee8b12fef 596 NyLPC_TUInt16 tx_len;
nyatla 37:fc4b4fd6a649 597 const struct NyLPC_TEthAddr* eth_dest;
nyatla 0:142ee8b12fef 598 struct TEthPacket* ethbuf=(struct TEthPacket*)(i_eth_buf+1);
nyatla 0:142ee8b12fef 599 //ペイロードのアドレスから、イーサネットフレームバッファのアドレスを復元
nyatla 37:fc4b4fd6a649 600
nyatla 37:fc4b4fd6a649 601 if(NyLPC_TIPv4Addr_isEqual(&(ethbuf->data.ipv4.destipaddr),&NyLPC_TIPv4Addr_BROADCAST)) {
nyatla 37:fc4b4fd6a649 602 //ブロードキャストならそのまま
nyatla 37:fc4b4fd6a649 603 eth_dest=&NyLPC_TEthAddr_BROADCAST;
nyatla 37:fc4b4fd6a649 604 }else if(NyLPC_TIPv4Addr_isEqualWithMask(&(ethbuf->data.ipv4.destipaddr),&NyLPC_TIPv4Addr_MULTICAST,&NyLPC_TIPv4Addr_MULTICAST_MASK)){
nyatla 37:fc4b4fd6a649 605 //マルチキャスト
nyatla 37:fc4b4fd6a649 606 ip2MulticastEmacAddr(&(ethbuf->data.ipv4.destipaddr),&emac);
nyatla 37:fc4b4fd6a649 607 eth_dest=&emac;
nyatla 37:fc4b4fd6a649 608 }else{
nyatla 37:fc4b4fd6a649 609 //LocalIP以外ならdefaultRootへ変換
nyatla 37:fc4b4fd6a649 610 eth_dest=NyLPC_cIPv4Arp_IPv4toEthAddr(
nyatla 37:fc4b4fd6a649 611 &inst->_arp,
nyatla 37:fc4b4fd6a649 612 NyLPC_cIPv4Config_isLocalIP(inst->_ref_config, &(ethbuf->data.ipv4.destipaddr))?(ethbuf->data.ipv4.destipaddr):(inst->_ref_config->dr_addr));
nyatla 37:fc4b4fd6a649 613 //IP->MAC変換をテスト。
nyatla 37:fc4b4fd6a649 614 if(eth_dest==NULL){
nyatla 37:fc4b4fd6a649 615 return NyLPC_TBool_FALSE;
nyatla 37:fc4b4fd6a649 616 }
nyatla 0:142ee8b12fef 617 }
nyatla 0:142ee8b12fef 618 //変換可能なら、イーサネットヘッダを更新して、送信処理へ。
nyatla 0:142ee8b12fef 619 tx_len=NyLPC_TEthernetIIHeader_setIPv4Tx(&(ethbuf->header),&(inst->_ref_config->eth_mac),eth_dest);
nyatla 0:142ee8b12fef 620 inst->_ethif->sendTxEthFrame(i_eth_buf,tx_len);
nyatla 0:142ee8b12fef 621 return NyLPC_TBool_TRUE;
nyatla 0:142ee8b12fef 622 }
nyatla 0:142ee8b12fef 623
nyatla 0:142ee8b12fef 624
nyatla 0:142ee8b12fef 625
nyatla 0:142ee8b12fef 626 //static void startEther()
nyatla 0:142ee8b12fef 627 //{
nyatla 0:142ee8b12fef 628 // NyLPC_TcUipService_t* inst=_NyLPC_TcUipService_inst;
nyatla 0:142ee8b12fef 629 //
nyatla 0:142ee8b12fef 630 // //Ethernetの起動待ち
nyatla 0:142ee8b12fef 631 // while(lEMACInit(_NyLPC_TcUipService_inst->_emac_semapho,&(inst->_ref_config->eth_mac))!= pdPASS )
nyatla 0:142ee8b12fef 632 // {
nyatla 0:142ee8b12fef 633 // vTaskDelay( 100 / portTICK_RATE_MS );
nyatla 0:142ee8b12fef 634 // }
nyatla 0:142ee8b12fef 635 // //Ethernetの割込み開始設定
nyatla 0:142ee8b12fef 636 // portENTER_CRITICAL();
nyatla 0:142ee8b12fef 637 // {
nyatla 0:142ee8b12fef 638 // LPC_EMAC->IntEnable = ( INT_RX_DONE | INT_TX_DONE );
nyatla 0:142ee8b12fef 639 // /* Set the interrupt priority to the max permissible to cause some
nyatla 0:142ee8b12fef 640 // interrupt nesting. */
nyatla 0:142ee8b12fef 641 // NVIC_SetPriority( ENET_IRQn, configEMAC_INTERRUPT_PRIORITY );
nyatla 0:142ee8b12fef 642 //
nyatla 0:142ee8b12fef 643 // /* Enable the interrupt. */
nyatla 0:142ee8b12fef 644 // NVIC_EnableIRQ( ENET_IRQn );
nyatla 0:142ee8b12fef 645 // }
nyatla 0:142ee8b12fef 646 // portEXIT_CRITICAL();
nyatla 0:142ee8b12fef 647 //}
nyatla 0:142ee8b12fef 648 //
nyatla 0:142ee8b12fef 649 //static void stopEther()
nyatla 0:142ee8b12fef 650 //{
nyatla 0:142ee8b12fef 651 // portENTER_CRITICAL();
nyatla 0:142ee8b12fef 652 // {
nyatla 0:142ee8b12fef 653 // LPC_EMAC->IntEnable = (~(INT_RX_DONE|INT_TX_DONE))&LPC_EMAC->IntEnable;
nyatla 0:142ee8b12fef 654 // NVIC_DisableIRQ( ENET_IRQn );
nyatla 0:142ee8b12fef 655 // }
nyatla 0:142ee8b12fef 656 // portEXIT_CRITICAL();
nyatla 20:3b0b444b4deb 657 //}
nyatla 20:3b0b444b4deb 658