Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: MbedFileServer_1768MiniDK2 RedWireBridge IssueDebug_gcc MiMicRemoteMCU-for-Mbed ... more
core/uip/NyLPC_cUipService.c@69:8c5f220441f5, 2014-05-29 (annotated)
- Committer:
- nyatla
- Date:
- Thu May 29 14:29:15 2014 +0000
- Revision:
- 69:8c5f220441f5
- Parent:
- 68:f7def7eb5504
- Child:
- 79:baa21f8763cf
r354????; LPC4088?????
Who changed what in which revision?
| User | Revision | Line number | New 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 | 68:f7def7eb5504 | 65 | |
| nyatla | 0:142ee8b12fef | 66 | |
| nyatla | 0:142ee8b12fef | 67 | /**************************************************** |
| nyatla | 0:142ee8b12fef | 68 | * UipServiceに関する宣言:その他 |
| nyatla | 0:142ee8b12fef | 69 | ***************************************************/ |
| nyatla | 0:142ee8b12fef | 70 | /** |
| nyatla | 0:142ee8b12fef | 71 | * イーサネットフレームの読み出し構造体 |
| nyatla | 0:142ee8b12fef | 72 | */ |
| nyatla | 0:142ee8b12fef | 73 | struct TEthPacket |
| nyatla | 0:142ee8b12fef | 74 | { |
| nyatla | 0:142ee8b12fef | 75 | struct NyLPC_TEthernetIIHeader header; |
| nyatla | 0:142ee8b12fef | 76 | union{ |
| nyatla | 0:142ee8b12fef | 77 | struct NyLPC_TArpHeader arp; |
| nyatla | 0:142ee8b12fef | 78 | struct NyLPC_TIPv4Header ipv4; |
| nyatla | 0:142ee8b12fef | 79 | }data; |
| nyatla | 57:bc4330dfa62f | 80 | }PACK_STRUCT_END; |
| nyatla | 0:142ee8b12fef | 81 | |
| nyatla | 0:142ee8b12fef | 82 | |
| nyatla | 0:142ee8b12fef | 83 | |
| nyatla | 0:142ee8b12fef | 84 | /** |
| nyatla | 0:142ee8b12fef | 85 | * サービスインスタンスのポインタ。サービスが稼働中はインスタンスのポインタが有効です。 |
| nyatla | 0:142ee8b12fef | 86 | */ |
| nyatla | 0:142ee8b12fef | 87 | NyLPC_TcUipService_t* _NyLPC_TcUipService_inst=NULL; |
| nyatla | 0:142ee8b12fef | 88 | |
| nyatla | 0:142ee8b12fef | 89 | /** |
| nyatla | 0:142ee8b12fef | 90 | * 唯一のインスタンス |
| nyatla | 0:142ee8b12fef | 91 | */ |
| nyatla | 0:142ee8b12fef | 92 | static NyLPC_TcUipService_t _service_instance; |
| nyatla | 0:142ee8b12fef | 93 | |
| nyatla | 0:142ee8b12fef | 94 | |
| nyatla | 0:142ee8b12fef | 95 | |
| nyatla | 0:142ee8b12fef | 96 | |
| nyatla | 0:142ee8b12fef | 97 | /** |
| nyatla | 0:142ee8b12fef | 98 | * uipタスク |
| nyatla | 0:142ee8b12fef | 99 | */ |
| nyatla | 0:142ee8b12fef | 100 | static int uipTask(void *pvParameters); |
| nyatla | 0:142ee8b12fef | 101 | |
| nyatla | 68:f7def7eb5504 | 102 | /** イーサネットドライバからのハンドラ*/ |
| nyatla | 68:f7def7eb5504 | 103 | static void ethernet_handler(void* i_param,NyLPC_TiEthernetDevice_EVENT i_type); |
| nyatla | 68:f7def7eb5504 | 104 | |
| nyatla | 0:142ee8b12fef | 105 | //-------------------------------------------------------------- |
| nyatla | 0:142ee8b12fef | 106 | |
| nyatla | 0:142ee8b12fef | 107 | |
| nyatla | 0:142ee8b12fef | 108 | static NyLPC_TBool sendIPv4Tx(struct NyLPC_TTxBufferHeader* i_eth_buf); |
| nyatla | 69:8c5f220441f5 | 109 | |
| nyatla | 57:bc4330dfa62f | 110 | //static void sendArpReqest(const struct TEthPacket* i_eth_packet); |
| nyatla | 69:8c5f220441f5 | 111 | static void sendRawEthFrameNL(void* i_buf,NyLPC_TUInt16 i_len); |
| nyatla | 69:8c5f220441f5 | 112 | static void releaseTxBufNL(void* i_buf); |
| nyatla | 0:142ee8b12fef | 113 | |
| nyatla | 0:142ee8b12fef | 114 | /**メッセージなし*/ |
| nyatla | 0:142ee8b12fef | 115 | #define TTaskMessage_MSG_NULL 0x0000 |
| nyatla | 0:142ee8b12fef | 116 | /**uipコアタスクに、開始要求する*/ |
| nyatla | 0:142ee8b12fef | 117 | #define TTaskMessage_MSG_RUN 0x0001 |
| nyatla | 0:142ee8b12fef | 118 | /**uipコアタスクに、停止要求する*/ |
| nyatla | 0:142ee8b12fef | 119 | #define TTaskMessage_MSG_STOP 0x0002 |
| nyatla | 0:142ee8b12fef | 120 | |
| nyatla | 0:142ee8b12fef | 121 | |
| nyatla | 0:142ee8b12fef | 122 | NyLPC_TcThread_t th; |
| nyatla | 0:142ee8b12fef | 123 | |
| nyatla | 0:142ee8b12fef | 124 | NyLPC_TBool NyLPC_cUipService_initialize(void) |
| nyatla | 0:142ee8b12fef | 125 | { |
| nyatla | 0:142ee8b12fef | 126 | NyLPC_TcUipService_t* inst=&_service_instance; |
| nyatla | 0:142ee8b12fef | 127 | //サービスは停止している事。 - Service must be uninitialized. |
| nyatla | 0:142ee8b12fef | 128 | NyLPC_Assert(!NyLPC_TcUipService_isInitService()); |
| nyatla | 0:142ee8b12fef | 129 | //IP処理部分の初期化 |
| nyatla | 0:142ee8b12fef | 130 | NyLPC_cIPv4_initialize(&(inst->_tcpv4)); |
| nyatla | 0:142ee8b12fef | 131 | //EMAC割込セマフォ |
| nyatla | 0:142ee8b12fef | 132 | NyLPC_cSemaphore_initialize(&inst->_emac_semapho); |
| nyatla | 0:142ee8b12fef | 133 | |
| nyatla | 48:00d211aac2ec | 134 | inst->_status=0x00; |
| nyatla | 0:142ee8b12fef | 135 | NyLPC_cStopwatch_initialize(&(inst->_arp_sw)); |
| nyatla | 0:142ee8b12fef | 136 | NyLPC_cStopwatch_initialize(&(inst->_periodic_sw)); |
| nyatla | 0:142ee8b12fef | 137 | NyLPC_cIPv4_initialize(&(inst->_tcpv4)); |
| nyatla | 0:142ee8b12fef | 138 | NyLPC_AbortIfNot(NyLPC_cMutex_initialize(&(inst->_mutex))); |
| nyatla | 0:142ee8b12fef | 139 | |
| nyatla | 0:142ee8b12fef | 140 | _NyLPC_TcUipService_inst=inst; |
| nyatla | 0:142ee8b12fef | 141 | //タスク起動 |
| nyatla | 0:142ee8b12fef | 142 | NyLPC_cThread_initialize(&th,NyLPC_TcUipService_config_STACK_SIZE,NyLPC_TcThread_PRIORITY_SERVICE); |
| nyatla | 0:142ee8b12fef | 143 | NyLPC_cThread_start(&th,uipTask,NULL); |
| nyatla | 0:142ee8b12fef | 144 | return NyLPC_TBool_TRUE; |
| nyatla | 0:142ee8b12fef | 145 | } |
| nyatla | 0:142ee8b12fef | 146 | |
| nyatla | 0:142ee8b12fef | 147 | |
| nyatla | 0:142ee8b12fef | 148 | |
| nyatla | 0:142ee8b12fef | 149 | |
| nyatla | 0:142ee8b12fef | 150 | |
| nyatla | 0:142ee8b12fef | 151 | |
| nyatla | 0:142ee8b12fef | 152 | |
| nyatla | 0:142ee8b12fef | 153 | /** |
| nyatla | 0:142ee8b12fef | 154 | * UIP処理を開始します。 |
| nyatla | 0:142ee8b12fef | 155 | * この関数はリエントラントではありません。複数のタスクから共有するときには、排他ロックを掛けてください。 |
| nyatla | 0:142ee8b12fef | 156 | * @param i_ref_config |
| nyatla | 0:142ee8b12fef | 157 | * このコンフィギュレーションは、stopを実行するまでの間、インスタンスから参照します。外部で保持してください。 |
| nyatla | 0:142ee8b12fef | 158 | */ |
| nyatla | 0:142ee8b12fef | 159 | void NyLPC_cUipService_start(const NyLPC_TcIPv4Config_t* i_ref_config) |
| nyatla | 0:142ee8b12fef | 160 | { |
| nyatla | 0:142ee8b12fef | 161 | NyLPC_TcUipService_t* inst=&_service_instance; |
| nyatla | 0:142ee8b12fef | 162 | NyLPC_Assert(NyLPC_TcUipService_isInitService()); |
| nyatla | 0:142ee8b12fef | 163 | if(!NyLPC_cUipService_isRun()) |
| nyatla | 0:142ee8b12fef | 164 | { |
| nyatla | 0:142ee8b12fef | 165 | //はじめて起動するときに1度だけデバイス取得(タスクスイッチが動いてないと動かないからここで。) |
| nyatla | 0:142ee8b12fef | 166 | if(inst->_ethif==NULL){ |
| nyatla | 0:142ee8b12fef | 167 | inst->_ethif=getEthernetDevicePnP(); |
| nyatla | 0:142ee8b12fef | 168 | } |
| nyatla | 48:00d211aac2ec | 169 | //コンフィグレーションセット |
| nyatla | 48:00d211aac2ec | 170 | inst->_ref_config=i_ref_config; |
| nyatla | 48:00d211aac2ec | 171 | //開始要求セット |
| nyatla | 48:00d211aac2ec | 172 | NyLPC_TUInt16_setBit(inst->_status,NyLPC_TcUipService_ORDER_START); |
| nyatla | 48:00d211aac2ec | 173 | //Order実行待ち |
| nyatla | 48:00d211aac2ec | 174 | while(NyLPC_TUInt16_isBitOn(inst->_status,NyLPC_TcUipService_ORDER_START)){ |
| nyatla | 0:142ee8b12fef | 175 | NyLPC_cThread_sleep(10); |
| nyatla | 0:142ee8b12fef | 176 | } |
| nyatla | 0:142ee8b12fef | 177 | } |
| nyatla | 0:142ee8b12fef | 178 | return; |
| nyatla | 0:142ee8b12fef | 179 | } |
| nyatla | 0:142ee8b12fef | 180 | /** |
| nyatla | 0:142ee8b12fef | 181 | * UIP処理を停止します。 |
| nyatla | 0:142ee8b12fef | 182 | * この関数はリエントラントではありません。複数のタスクから共有するときには、排他ロックを掛けてください。 |
| nyatla | 0:142ee8b12fef | 183 | * いまのところ、ストップシーケンスの実装は良くありません。 |
| nyatla | 0:142ee8b12fef | 184 | * 再設計が必要。 |
| nyatla | 0:142ee8b12fef | 185 | */ |
| nyatla | 0:142ee8b12fef | 186 | void NyLPC_cUipService_stop(void) |
| nyatla | 0:142ee8b12fef | 187 | { |
| nyatla | 0:142ee8b12fef | 188 | NyLPC_TcUipService_t* inst=&_service_instance; |
| nyatla | 0:142ee8b12fef | 189 | NyLPC_Assert(NyLPC_TcUipService_isInitService()); |
| nyatla | 0:142ee8b12fef | 190 | if(NyLPC_cUipService_isRun()) |
| nyatla | 0:142ee8b12fef | 191 | { |
| nyatla | 48:00d211aac2ec | 192 | NyLPC_TUInt16_setBit(inst->_status,NyLPC_TcUipService_ORDER_STOP); |
| nyatla | 48:00d211aac2ec | 193 | //Order実行待ち |
| nyatla | 48:00d211aac2ec | 194 | while(NyLPC_TUInt16_isBitOn(inst->_status,NyLPC_TcUipService_ORDER_STOP)){ |
| nyatla | 0:142ee8b12fef | 195 | NyLPC_cThread_sleep(10); |
| nyatla | 0:142ee8b12fef | 196 | } |
| nyatla | 0:142ee8b12fef | 197 | } |
| nyatla | 0:142ee8b12fef | 198 | return; |
| nyatla | 0:142ee8b12fef | 199 | } |
| nyatla | 0:142ee8b12fef | 200 | |
| nyatla | 0:142ee8b12fef | 201 | |
| nyatla | 0:142ee8b12fef | 202 | const char* NyLPC_cUipService_refDeviceName(void) |
| nyatla | 0:142ee8b12fef | 203 | { |
| nyatla | 0:142ee8b12fef | 204 | NyLPC_TcUipService_t* inst=&_service_instance; |
| nyatla | 68:f7def7eb5504 | 205 | return NyLPC_cUipService_isRun()?NyLPC_iEthernetDevice_getDevicName(inst->_ethif):NULL; |
| nyatla | 0:142ee8b12fef | 206 | } |
| nyatla | 21:f2a70f15301f | 207 | const NyLPC_TcIPv4Config_t* NyLPC_cUipService_refCurrentConfig(void) |
| nyatla | 21:f2a70f15301f | 208 | { |
| nyatla | 21:f2a70f15301f | 209 | NyLPC_TcUipService_t* inst=&_service_instance; |
| nyatla | 21:f2a70f15301f | 210 | return inst->_ref_config; |
| nyatla | 21:f2a70f15301f | 211 | } |
| nyatla | 0:142ee8b12fef | 212 | /********************************************************************** |
| nyatla | 0:142ee8b12fef | 213 | * |
| nyatla | 0:142ee8b12fef | 214 | * </HWコールバックに関わる宣言> |
| nyatla | 0:142ee8b12fef | 215 | * |
| nyatla | 0:142ee8b12fef | 216 | *********************************************************************/ |
| nyatla | 0:142ee8b12fef | 217 | |
| nyatla | 0:142ee8b12fef | 218 | |
| nyatla | 0:142ee8b12fef | 219 | //PERIODIC rate |
| nyatla | 0:142ee8b12fef | 220 | #define PERIODIC_TIMER (1*200) |
| nyatla | 0:142ee8b12fef | 221 | #define ARP_TIMER (60*1000*10) |
| nyatla | 0:142ee8b12fef | 222 | |
| nyatla | 37:fc4b4fd6a649 | 223 | |
| nyatla | 37:fc4b4fd6a649 | 224 | |
| nyatla | 0:142ee8b12fef | 225 | /** |
| nyatla | 0:142ee8b12fef | 226 | * 操作キューを確認して、タスクのステータスをアップデートします。 |
| nyatla | 0:142ee8b12fef | 227 | * 高速化のため、Proc-Callerを使用していません。複雑なタスク操作をするときには、書き換えてください。 |
| nyatla | 48:00d211aac2ec | 228 | * @return |
| nyatla | 48:00d211aac2ec | 229 | * UIPタスクの実行状態 |
| nyatla | 0:142ee8b12fef | 230 | */ |
| nyatla | 48:00d211aac2ec | 231 | static NyLPC_TBool updateTaskStatus() |
| nyatla | 0:142ee8b12fef | 232 | { |
| nyatla | 0:142ee8b12fef | 233 | NyLPC_TcUipService_t* inst=_NyLPC_TcUipService_inst; |
| nyatla | 48:00d211aac2ec | 234 | if(NyLPC_cUipService_isRun()){ |
| nyatla | 48:00d211aac2ec | 235 | //開始状態 |
| nyatla | 48:00d211aac2ec | 236 | if(NyLPC_TUInt16_isBitOn(inst->_status,NyLPC_TcUipService_ORDER_STOP)) |
| nyatla | 48:00d211aac2ec | 237 | { |
| nyatla | 48:00d211aac2ec | 238 | //停止操作 |
| nyatla | 68:f7def7eb5504 | 239 | NyLPC_iEthernetDevice_stop(inst->_ethif); |
| nyatla | 48:00d211aac2ec | 240 | NyLPC_cIPv4_stop(&(inst->_tcpv4)); |
| nyatla | 48:00d211aac2ec | 241 | NyLPC_cIPv4IComp_finalize(&(inst->_icomp)); |
| nyatla | 48:00d211aac2ec | 242 | NyLPC_cIPv4Arp_finalize(&(inst->_arp)); |
| nyatla | 48:00d211aac2ec | 243 | inst->_ref_config=NULL; |
| nyatla | 48:00d211aac2ec | 244 | NyLPC_TUInt16_unsetBit(inst->_status,NyLPC_TcUipService_STATUSBIT_IS_RUNNING); |
| nyatla | 48:00d211aac2ec | 245 | NyLPC_TUInt16_unsetBit(inst->_status,NyLPC_TcUipService_ORDER_STOP); |
| nyatla | 48:00d211aac2ec | 246 | return NyLPC_TBool_FALSE; |
| nyatla | 0:142ee8b12fef | 247 | } |
| nyatla | 48:00d211aac2ec | 248 | return NyLPC_TBool_TRUE; |
| nyatla | 48:00d211aac2ec | 249 | }else{ |
| nyatla | 48:00d211aac2ec | 250 | //停止状態 |
| nyatla | 48:00d211aac2ec | 251 | if(NyLPC_TUInt16_isBitOn(inst->_status,NyLPC_TcUipService_ORDER_START)) |
| nyatla | 48:00d211aac2ec | 252 | { |
| nyatla | 48:00d211aac2ec | 253 | //TCP,ICOMPの初期化 |
| nyatla | 48:00d211aac2ec | 254 | NyLPC_cIPv4_start(&(inst->_tcpv4),inst->_ref_config); |
| nyatla | 48:00d211aac2ec | 255 | NyLPC_cIPv4IComp_initialize(&(inst->_icomp),inst->_ref_config); |
| nyatla | 48:00d211aac2ec | 256 | //uip_arp_init(msg->start.ref_config); |
| nyatla | 48:00d211aac2ec | 257 | NyLPC_cIPv4Arp_initialize(&(inst->_arp),inst->_ref_config); |
| nyatla | 48:00d211aac2ec | 258 | NyLPC_cStopwatch_startExpire(&(inst->_arp_sw),1);//1度ARPを起動するため。 |
| nyatla | 48:00d211aac2ec | 259 | NyLPC_cStopwatch_startExpire(&(inst->_periodic_sw),PERIODIC_TIMER); |
| nyatla | 48:00d211aac2ec | 260 | //EtherNETデバイス初期化 |
| nyatla | 68:f7def7eb5504 | 261 | while(!NyLPC_iEthernetDevice_start(inst->_ethif,&(inst->_ref_config->eth_mac),ethernet_handler,inst)); |
| nyatla | 48:00d211aac2ec | 262 | NyLPC_TUInt16_setBit(inst->_status,NyLPC_TcUipService_STATUSBIT_IS_RUNNING); |
| nyatla | 48:00d211aac2ec | 263 | NyLPC_TUInt16_unsetBit(inst->_status,NyLPC_TcUipService_ORDER_START); |
| nyatla | 48:00d211aac2ec | 264 | return NyLPC_TBool_TRUE; |
| nyatla | 0:142ee8b12fef | 265 | } |
| nyatla | 48:00d211aac2ec | 266 | return NyLPC_TBool_FALSE; |
| nyatla | 0:142ee8b12fef | 267 | } |
| nyatla | 0:142ee8b12fef | 268 | } |
| nyatla | 0:142ee8b12fef | 269 | |
| nyatla | 0:142ee8b12fef | 270 | /** |
| nyatla | 0:142ee8b12fef | 271 | * uipタスクのメインループ |
| nyatla | 0:142ee8b12fef | 272 | */ |
| nyatla | 0:142ee8b12fef | 273 | static int uipTask(void *pvParameters) |
| nyatla | 0:142ee8b12fef | 274 | { |
| nyatla | 0:142ee8b12fef | 275 | NyLPC_TUInt16 rx_len,tx_len; |
| nyatla | 0:142ee8b12fef | 276 | struct TEthPacket* ethbuf; |
| nyatla | 0:142ee8b12fef | 277 | NyLPC_TcUipService_t* inst=_NyLPC_TcUipService_inst; |
| nyatla | 69:8c5f220441f5 | 278 | void* r; |
| nyatla | 0:142ee8b12fef | 279 | (void)pvParameters; |
| nyatla | 0:142ee8b12fef | 280 | for( ;; ) |
| nyatla | 0:142ee8b12fef | 281 | { |
| nyatla | 0:142ee8b12fef | 282 | //タスク状態の更新 |
| nyatla | 48:00d211aac2ec | 283 | if(!updateTaskStatus()) |
| nyatla | 0:142ee8b12fef | 284 | { |
| nyatla | 0:142ee8b12fef | 285 | //RUNステータス以外の時は、ここで終了する。 |
| nyatla | 0:142ee8b12fef | 286 | NyLPC_cThread_sleep(50); |
| nyatla | 0:142ee8b12fef | 287 | continue; |
| nyatla | 0:142ee8b12fef | 288 | } |
| nyatla | 0:142ee8b12fef | 289 | //イーサネットフレームの取得 |
| nyatla | 0:142ee8b12fef | 290 | //Ethernet Device Lock(ARPを含む) |
| nyatla | 2:b96c1e90d120 | 291 | NyLPC_cMutex_lock(&(inst->_mutex)); |
| nyatla | 68:f7def7eb5504 | 292 | ethbuf= (struct TEthPacket*)NyLPC_iEthernetDevice_getRxEthFrame(inst->_ethif,&rx_len); |
| nyatla | 0:142ee8b12fef | 293 | tx_len=0; |
| nyatla | 0:142ee8b12fef | 294 | while(ethbuf != NULL){ |
| nyatla | 0:142ee8b12fef | 295 | if(rx_len>0) |
| nyatla | 0:142ee8b12fef | 296 | { |
| nyatla | 0:142ee8b12fef | 297 | //ペイロードサイズを計算 |
| nyatla | 0:142ee8b12fef | 298 | rx_len-=sizeof(struct NyLPC_TEthernetIIHeader); |
| nyatla | 0:142ee8b12fef | 299 | switch(ethbuf->header.type) |
| nyatla | 0:142ee8b12fef | 300 | { |
| nyatla | 0:142ee8b12fef | 301 | case NyLPC_HTONS(NyLPC_TEthernetIIHeader_TYPE_IP): |
| nyatla | 0:142ee8b12fef | 302 | //ARPテーブルの更新 |
| nyatla | 0:142ee8b12fef | 303 | //uip_arp_ipin(&(ethbuf->header),ethbuf->data.ipv4.srcipaddr); |
| nyatla | 0:142ee8b12fef | 304 | NyLPC_cIPv4Arp_incomingIp(&inst->_arp,&(ethbuf->header),ethbuf->data.ipv4.srcipaddr); |
| nyatla | 69:8c5f220441f5 | 305 | //Ethernet Device UnLock(NyLPC_cIPv4_rxがallocをコールする可能性があるので一時的にロック解除) |
| nyatla | 0:142ee8b12fef | 306 | NyLPC_cMutex_unlock(&(inst->_mutex)); |
| nyatla | 0:142ee8b12fef | 307 | //IPパケットの処理 |
| nyatla | 0:142ee8b12fef | 308 | r=NyLPC_cIPv4_rx(&(inst->_tcpv4),&(ethbuf->data.ipv4),rx_len); |
| nyatla | 69:8c5f220441f5 | 309 | if(r!=NULL){ |
| nyatla | 69:8c5f220441f5 | 310 | //IPパケットとして送信 |
| nyatla | 69:8c5f220441f5 | 311 | NyLPC_cUipService_sendIPv4Tx(r); |
| nyatla | 69:8c5f220441f5 | 312 | } |
| nyatla | 0:142ee8b12fef | 313 | //ロックの復帰 |
| nyatla | 0:142ee8b12fef | 314 | NyLPC_cMutex_lock(&(inst->_mutex)); |
| nyatla | 69:8c5f220441f5 | 315 | if(r!=NULL){ |
| nyatla | 69:8c5f220441f5 | 316 | releaseTxBufNL(r); |
| nyatla | 0:142ee8b12fef | 317 | } |
| nyatla | 0:142ee8b12fef | 318 | break; |
| nyatla | 0:142ee8b12fef | 319 | case NyLPC_HTONS(NyLPC_TEthernetIIHeader_TYPE_ARP): |
| nyatla | 69:8c5f220441f5 | 320 | //Ethernet Device UnLock(NyLPC_cIPv4_rxがallocをコールする可能性があるので一時的にロック解除) |
| nyatla | 69:8c5f220441f5 | 321 | NyLPC_cMutex_unlock(&(inst->_mutex)); |
| nyatla | 69:8c5f220441f5 | 322 | r=NyLPC_cIPv4Arp_rx(&inst->_arp,&(ethbuf->data.arp),rx_len,&tx_len); |
| nyatla | 69:8c5f220441f5 | 323 | NyLPC_cMutex_lock(&(inst->_mutex)); |
| nyatla | 69:8c5f220441f5 | 324 | if(r!=NULL){ |
| nyatla | 69:8c5f220441f5 | 325 | sendRawEthFrameNL(r,tx_len); |
| nyatla | 69:8c5f220441f5 | 326 | releaseTxBufNL(r); |
| nyatla | 0:142ee8b12fef | 327 | } |
| nyatla | 0:142ee8b12fef | 328 | break; |
| nyatla | 0:142ee8b12fef | 329 | case NyLPC_HTONS(NyLPC_TEthernetIIHeader_TYPE_IPV6): |
| nyatla | 0:142ee8b12fef | 330 | //uip_process_ipv6(); |
| nyatla | 0:142ee8b12fef | 331 | break; |
| nyatla | 0:142ee8b12fef | 332 | default: |
| nyatla | 0:142ee8b12fef | 333 | break; |
| nyatla | 0:142ee8b12fef | 334 | } |
| nyatla | 0:142ee8b12fef | 335 | } |
| nyatla | 2:b96c1e90d120 | 336 | //受信キューを進行。 |
| nyatla | 68:f7def7eb5504 | 337 | NyLPC_iEthernetDevice_nextRxEthFrame(inst->_ethif); |
| nyatla | 0:142ee8b12fef | 338 | //受信処理 |
| nyatla | 68:f7def7eb5504 | 339 | ethbuf= (struct TEthPacket*)NyLPC_iEthernetDevice_getRxEthFrame(inst->_ethif,&rx_len); |
| nyatla | 0:142ee8b12fef | 340 | } |
| nyatla | 0:142ee8b12fef | 341 | //データが無い。 |
| nyatla | 0:142ee8b12fef | 342 | if(NyLPC_cStopwatch_isExpired(&(inst->_arp_sw))){ |
| nyatla | 0:142ee8b12fef | 343 | //uip_arp_timer(); |
| nyatla | 0:142ee8b12fef | 344 | NyLPC_cIPv4Arp_periodic(&inst->_arp); |
| nyatla | 0:142ee8b12fef | 345 | NyLPC_cStopwatch_startExpire(&(inst->_arp_sw),ARP_TIMER); |
| nyatla | 2:b96c1e90d120 | 346 | } |
| nyatla | 0:142ee8b12fef | 347 | if(NyLPC_cStopwatch_isExpired(&(inst->_periodic_sw))){ |
| nyatla | 37:fc4b4fd6a649 | 348 | NyLPC_cMutex_unlock(&(inst->_mutex)); |
| nyatla | 0:142ee8b12fef | 349 | NyLPC_cIPv4_periodec(&(inst->_tcpv4)); |
| nyatla | 37:fc4b4fd6a649 | 350 | NyLPC_cMutex_lock(&(inst->_mutex)); |
| nyatla | 0:142ee8b12fef | 351 | NyLPC_cStopwatch_startExpire(&(inst->_periodic_sw),PERIODIC_TIMER); |
| nyatla | 0:142ee8b12fef | 352 | } |
| nyatla | 0:142ee8b12fef | 353 | //リソースロックの解除 |
| nyatla | 0:142ee8b12fef | 354 | NyLPC_cMutex_unlock(&(inst->_mutex)); |
| nyatla | 0:142ee8b12fef | 355 | //割込によるセマフォの解除か、タイムアウトで再開する。(タイムアウト値は周期関数の実行レート以下にすること。) |
| nyatla | 0:142ee8b12fef | 356 | NyLPC_cSemaphore_take(&(_NyLPC_TcUipService_inst->_emac_semapho),PERIODIC_TIMER); |
| nyatla | 0:142ee8b12fef | 357 | } |
| nyatla | 0:142ee8b12fef | 358 | return 0; |
| nyatla | 0:142ee8b12fef | 359 | } |
| nyatla | 0:142ee8b12fef | 360 | |
| nyatla | 0:142ee8b12fef | 361 | |
| nyatla | 0:142ee8b12fef | 362 | /** |
| nyatla | 68:f7def7eb5504 | 363 | * イーサネットドライバからのハンドラ |
| nyatla | 68:f7def7eb5504 | 364 | */ |
| nyatla | 68:f7def7eb5504 | 365 | static void ethernet_handler(void* i_param,NyLPC_TiEthernetDevice_EVENT i_type) |
| nyatla | 68:f7def7eb5504 | 366 | { |
| nyatla | 68:f7def7eb5504 | 367 | switch(i_type){ |
| nyatla | 68:f7def7eb5504 | 368 | case NyLPC_TiEthernetDevice_EVENT_ON_RX: |
| nyatla | 68:f7def7eb5504 | 369 | //受信系のセマフォブロックの解除 |
| nyatla | 68:f7def7eb5504 | 370 | NyLPC_cSemaphore_giveFromISR(&(((NyLPC_TcUipService_t*)i_param)->_emac_semapho)); |
| nyatla | 68:f7def7eb5504 | 371 | break; |
| nyatla | 68:f7def7eb5504 | 372 | default: |
| nyatla | 68:f7def7eb5504 | 373 | break; |
| nyatla | 68:f7def7eb5504 | 374 | } |
| nyatla | 68:f7def7eb5504 | 375 | } |
| nyatla | 68:f7def7eb5504 | 376 | |
| nyatla | 68:f7def7eb5504 | 377 | /** |
| nyatla | 57:bc4330dfa62f | 378 | * IPv4パケットのpeerIPを問い合わせるARPパケットを送信します。 |
| nyatla | 69:8c5f220441f5 | 379 | * allocを中でコールするから要UNLOCK状態 |
| nyatla | 0:142ee8b12fef | 380 | */ |
| nyatla | 69:8c5f220441f5 | 381 | void NyLPC_cUipService_sendArpRequest(const struct NyLPC_TIPv4Addr* i_addr) |
| nyatla | 37:fc4b4fd6a649 | 382 | { |
| nyatla | 37:fc4b4fd6a649 | 383 | NyLPC_TcUipService_t* inst=_NyLPC_TcUipService_inst; |
| nyatla | 69:8c5f220441f5 | 384 | struct NyLPC_TTxBufferHeader* p; |
| nyatla | 37:fc4b4fd6a649 | 385 | NyLPC_TUInt16 tx_len; |
| nyatla | 37:fc4b4fd6a649 | 386 | struct TEthPacket* ethbuf; |
| nyatla | 69:8c5f220441f5 | 387 | //システムTxBufを得る |
| nyatla | 69:8c5f220441f5 | 388 | ethbuf=(struct TEthPacket*)NyLPC_cUipService_allocSysTxBuf(); |
| nyatla | 37:fc4b4fd6a649 | 389 | //ARPパケットを作る。 |
| nyatla | 57:bc4330dfa62f | 390 | NyLPC_TArpHeader_setArpRequest(&(ethbuf->data.arp),inst->_ref_config->ip_addr,&(inst->_ref_config->eth_mac),i_addr); |
| nyatla | 37:fc4b4fd6a649 | 391 | tx_len=NyLPC_TEthernetIIHeader_setArpTx(&(ethbuf->header),&(inst->_ref_config->eth_mac)); |
| nyatla | 37:fc4b4fd6a649 | 392 | //送信 |
| nyatla | 69:8c5f220441f5 | 393 | p=((struct NyLPC_TTxBufferHeader*)(((struct NyLPC_TEthernetIIHeader*)ethbuf)-1))-1; |
| nyatla | 69:8c5f220441f5 | 394 | |
| nyatla | 69:8c5f220441f5 | 395 | NyLPC_cMutex_lock(&(inst->_mutex)); |
| nyatla | 69:8c5f220441f5 | 396 | NyLPC_iEthernetDevice_sendTxEthFrame(inst->_ethif,p,tx_len); |
| nyatla | 69:8c5f220441f5 | 397 | NyLPC_iEthernetDevice_releaseTxBuf(inst->_ethif,p); |
| nyatla | 69:8c5f220441f5 | 398 | NyLPC_cMutex_unlock(&(inst->_mutex)); |
| nyatla | 57:bc4330dfa62f | 399 | } |
| nyatla | 57:bc4330dfa62f | 400 | |
| nyatla | 57:bc4330dfa62f | 401 | |
| nyatla | 57:bc4330dfa62f | 402 | |
| nyatla | 57:bc4330dfa62f | 403 | |
| nyatla | 57:bc4330dfa62f | 404 | /** |
| nyatla | 57:bc4330dfa62f | 405 | * allocTxBufで取得したペイロードメモリを"IPパケットとして"送信します。 |
| nyatla | 57:bc4330dfa62f | 406 | * @param i_eth_payload |
| nyatla | 57:bc4330dfa62f | 407 | * [NyLPC_TTxBufferHeader][NyLPC_TEthernetIIHeader][payload]メモリの、[payload]のアドレスを指定します。 |
| nyatla | 57:bc4330dfa62f | 408 | * 通常は、NyLPC_cUipService_allocTxBufの返却したメモリを指定します。 |
| nyatla | 57:bc4330dfa62f | 409 | */ |
| nyatla | 57:bc4330dfa62f | 410 | |
| nyatla | 57:bc4330dfa62f | 411 | void NyLPC_cUipService_sendIPv4Tx(void* i_eth_payload) |
| nyatla | 57:bc4330dfa62f | 412 | { |
| nyatla | 57:bc4330dfa62f | 413 | NyLPC_TcUipService_t* inst=_NyLPC_TcUipService_inst; |
| nyatla | 57:bc4330dfa62f | 414 | struct NyLPC_TTxBufferHeader* p=((struct NyLPC_TTxBufferHeader*)(((struct NyLPC_TEthernetIIHeader*)i_eth_payload)-1))-1; |
| nyatla | 57:bc4330dfa62f | 415 | NyLPC_cMutex_lock(&(inst->_mutex)); |
| nyatla | 57:bc4330dfa62f | 416 | //IPパケットの送信を試行 |
| nyatla | 69:8c5f220441f5 | 417 | if(sendIPv4Tx(p)){ |
| nyatla | 69:8c5f220441f5 | 418 | NyLPC_cMutex_unlock(&(inst->_mutex)); |
| nyatla | 69:8c5f220441f5 | 419 | return; |
| nyatla | 57:bc4330dfa62f | 420 | } |
| nyatla | 57:bc4330dfa62f | 421 | NyLPC_cMutex_unlock(&(inst->_mutex)); |
| nyatla | 69:8c5f220441f5 | 422 | //ARPリクエストを代わりに送信 |
| nyatla | 69:8c5f220441f5 | 423 | NyLPC_cUipService_sendArpRequest(&((struct NyLPC_TIPv4Header*)i_eth_payload)->destipaddr); |
| nyatla | 57:bc4330dfa62f | 424 | return; |
| nyatla | 57:bc4330dfa62f | 425 | } |
| nyatla | 57:bc4330dfa62f | 426 | |
| nyatla | 57:bc4330dfa62f | 427 | |
| nyatla | 37:fc4b4fd6a649 | 428 | /** |
| nyatla | 37:fc4b4fd6a649 | 429 | * ARPテーブルに指定したIPがあるかを返します。 |
| nyatla | 37:fc4b4fd6a649 | 430 | */ |
| nyatla | 37:fc4b4fd6a649 | 431 | NyLPC_TBool NyLPC_cUipService_hasArpInfo(const struct NyLPC_TIPv4Addr* i_addr) |
| nyatla | 37:fc4b4fd6a649 | 432 | { |
| nyatla | 37:fc4b4fd6a649 | 433 | NyLPC_TcUipService_t* inst=_NyLPC_TcUipService_inst; |
| nyatla | 37:fc4b4fd6a649 | 434 | return NyLPC_cIPv4Arp_IPv4toEthAddr(&inst->_arp,*i_addr)!=NULL; |
| nyatla | 37:fc4b4fd6a649 | 435 | } |
| nyatla | 0:142ee8b12fef | 436 | |
| nyatla | 69:8c5f220441f5 | 437 | /** |
| nyatla | 69:8c5f220441f5 | 438 | * システム用の送信ペイロードを返します。 |
| nyatla | 69:8c5f220441f5 | 439 | * 関数は必ず成功します。 |
| nyatla | 69:8c5f220441f5 | 440 | */ |
| nyatla | 69:8c5f220441f5 | 441 | void* NyLPC_cUipService_allocSysTxBuf(void) |
| nyatla | 69:8c5f220441f5 | 442 | { |
| nyatla | 69:8c5f220441f5 | 443 | NyLPC_TUInt16 s; |
| nyatla | 69:8c5f220441f5 | 444 | NyLPC_TcUipService_t* inst=_NyLPC_TcUipService_inst; |
| nyatla | 69:8c5f220441f5 | 445 | struct NyLPC_TTxBufferHeader* ethbuf; |
| nyatla | 69:8c5f220441f5 | 446 | //排他処理をして、メモリを取得する。SYSTEMメモリはEthernetドライバの解放待ちのみなのでまとめてLOCKしておk |
| nyatla | 69:8c5f220441f5 | 447 | NyLPC_cMutex_lock(&(inst->_mutex)); |
| nyatla | 69:8c5f220441f5 | 448 | for(;;){ |
| nyatla | 69:8c5f220441f5 | 449 | ethbuf=(struct NyLPC_TTxBufferHeader*)NyLPC_iEthernetDevice_allocTxBuf(inst->_ethif,NyLPC_TcEthernetMM_HINT_CTRL_PACKET,&s); |
| nyatla | 69:8c5f220441f5 | 450 | if(ethbuf==NULL){ |
| nyatla | 69:8c5f220441f5 | 451 | NyLPC_cThread_yield(); |
| nyatla | 69:8c5f220441f5 | 452 | continue; |
| nyatla | 69:8c5f220441f5 | 453 | } |
| nyatla | 69:8c5f220441f5 | 454 | break; |
| nyatla | 69:8c5f220441f5 | 455 | } |
| nyatla | 69:8c5f220441f5 | 456 | NyLPC_cMutex_unlock(&(inst->_mutex)); |
| nyatla | 69:8c5f220441f5 | 457 | //イーサネットバッファのアドレスを計算 |
| nyatla | 69:8c5f220441f5 | 458 | return &(((struct TEthPacket*)(ethbuf+1))->data); |
| nyatla | 69:8c5f220441f5 | 459 | } |
| nyatla | 0:142ee8b12fef | 460 | |
| nyatla | 57:bc4330dfa62f | 461 | |
| nyatla | 57:bc4330dfa62f | 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 | 68:f7def7eb5504 | 469 | ethbuf=(struct NyLPC_TTxBufferHeader*)NyLPC_iEthernetDevice_allocTxBuf(inst->_ethif,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 | 68:f7def7eb5504 | 487 | NyLPC_iEthernetDevice_releaseTxBuf(inst->_ethif,((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 | 69:8c5f220441f5 | 502 | /** |
| nyatla | 69:8c5f220441f5 | 503 | * "IPv4パケットを格納した"イーサフレームを送信します。 |
| nyatla | 69:8c5f220441f5 | 504 | * コール前に、必ずロックしてから呼び出してください。 |
| nyatla | 69:8c5f220441f5 | 505 | *//* |
| nyatla | 69:8c5f220441f5 | 506 | static void copyAndSendIPv4Tx(const struct TEthPacket* i_buf) |
| nyatla | 69:8c5f220441f5 | 507 | { |
| nyatla | 69:8c5f220441f5 | 508 | NyLPC_TcUipService_t* inst=_NyLPC_TcUipService_inst; |
| nyatla | 69:8c5f220441f5 | 509 | NyLPC_TUInt16 s; |
| nyatla | 69:8c5f220441f5 | 510 | //送信する。 |
| nyatla | 69:8c5f220441f5 | 511 | s=NyLPC_htons(i_buf->data.ipv4.len16)+sizeof(struct NyLPC_TEthernetIIHeader); |
| nyatla | 69:8c5f220441f5 | 512 | memcpy(inst->stx.buf,i_buf,s); |
| nyatla | 69:8c5f220441f5 | 513 | if(!sendIPv4Tx(&(inst->stx.h))){ |
| nyatla | 69:8c5f220441f5 | 514 | //失敗した場合はARPリクエストに変換して再送 |
| nyatla | 69:8c5f220441f5 | 515 | //@todo unchecked PASS! |
| nyatla | 69:8c5f220441f5 | 516 | sendArpReqest(&i_buf->data.ipv4.destipaddr); |
| nyatla | 69:8c5f220441f5 | 517 | } |
| nyatla | 69:8c5f220441f5 | 518 | return; |
| nyatla | 69:8c5f220441f5 | 519 | }*/ |
| nyatla | 0:142ee8b12fef | 520 | /** |
| nyatla | 57:bc4330dfa62f | 521 | * "IPv4パケットを格納した"イーサフレームを送信します。 |
| nyatla | 0:142ee8b12fef | 522 | * コール前に、必ずロックしてから呼び出してください。 |
| nyatla | 0:142ee8b12fef | 523 | */ |
| nyatla | 69:8c5f220441f5 | 524 | /* |
| nyatla | 0:142ee8b12fef | 525 | static void copyAndSendIPv4Tx(const struct TEthPacket* i_buf) |
| nyatla | 0:142ee8b12fef | 526 | { |
| nyatla | 0:142ee8b12fef | 527 | NyLPC_TcUipService_t* inst=_NyLPC_TcUipService_inst; |
| nyatla | 0:142ee8b12fef | 528 | NyLPC_TUInt16 s; |
| nyatla | 0:142ee8b12fef | 529 | //ACK送信用の自己バッファが空くまで待つ |
| nyatla | 0:142ee8b12fef | 530 | while(inst->stx.h.is_lock){ |
| nyatla | 68:f7def7eb5504 | 531 | NyLPC_iEthernetDevice_processTx(inst->_ethif); |
| nyatla | 0:142ee8b12fef | 532 | } |
| nyatla | 0:142ee8b12fef | 533 | //送信する。 |
| nyatla | 0:142ee8b12fef | 534 | s=NyLPC_htons(i_buf->data.ipv4.len16)+sizeof(struct NyLPC_TEthernetIIHeader); |
| nyatla | 0:142ee8b12fef | 535 | memcpy(inst->stx.buf,i_buf,s); |
| nyatla | 0:142ee8b12fef | 536 | if(!sendIPv4Tx(&(inst->stx.h))){ |
| nyatla | 0:142ee8b12fef | 537 | //失敗した場合はARPリクエストに変換して再送 |
| nyatla | 57:bc4330dfa62f | 538 | //@todo unchecked PASS! |
| nyatla | 57:bc4330dfa62f | 539 | sendArpReqest(&i_buf->data.ipv4.destipaddr); |
| nyatla | 0:142ee8b12fef | 540 | } |
| nyatla | 0:142ee8b12fef | 541 | return; |
| nyatla | 0:142ee8b12fef | 542 | } |
| nyatla | 69:8c5f220441f5 | 543 | */ |
| nyatla | 57:bc4330dfa62f | 544 | |
| nyatla | 0:142ee8b12fef | 545 | |
| nyatla | 0:142ee8b12fef | 546 | /** |
| nyatla | 69:8c5f220441f5 | 547 | * イーサネットフレームを送信します。 |
| nyatla | 69:8c5f220441f5 | 548 | * この関数はiptaskで実行される関数からのみ使用てください。 |
| nyatla | 0:142ee8b12fef | 549 | * @i_buf |
| nyatla | 0:142ee8b12fef | 550 | * イーサネットフレームを格納したメモリです。 |
| nyatla | 0:142ee8b12fef | 551 | * @i_len |
| nyatla | 0:142ee8b12fef | 552 | * イーサネットペイロードのサイズです。 |
| nyatla | 0:142ee8b12fef | 553 | */ |
| nyatla | 69:8c5f220441f5 | 554 | static void sendRawEthFrameNL(void* i_buf,NyLPC_TUInt16 i_len) |
| nyatla | 0:142ee8b12fef | 555 | { |
| nyatla | 69:8c5f220441f5 | 556 | NyLPC_iEthernetDevice_sendTxEthFrame( |
| nyatla | 69:8c5f220441f5 | 557 | _NyLPC_TcUipService_inst->_ethif, |
| nyatla | 69:8c5f220441f5 | 558 | ((struct NyLPC_TTxBufferHeader*)(((struct NyLPC_TEthernetIIHeader*)i_buf)-1))-1, |
| nyatla | 69:8c5f220441f5 | 559 | i_len); |
| nyatla | 0:142ee8b12fef | 560 | return; |
| nyatla | 0:142ee8b12fef | 561 | } |
| nyatla | 69:8c5f220441f5 | 562 | /** |
| nyatla | 69:8c5f220441f5 | 563 | * ロック状態で使用できるreleaseTxBuf。 |
| nyatla | 69:8c5f220441f5 | 564 | * この関数はiptaskで実行される関数からのみ使用してください。 |
| nyatla | 69:8c5f220441f5 | 565 | */ |
| nyatla | 69:8c5f220441f5 | 566 | static void releaseTxBufNL(void* i_buf) |
| nyatla | 69:8c5f220441f5 | 567 | { |
| nyatla | 69:8c5f220441f5 | 568 | //ペイロードの位置から、メモリブロックを再生。 |
| nyatla | 69:8c5f220441f5 | 569 | NyLPC_iEthernetDevice_releaseTxBuf( |
| nyatla | 69:8c5f220441f5 | 570 | _NyLPC_TcUipService_inst->_ethif, |
| nyatla | 69:8c5f220441f5 | 571 | ((struct NyLPC_TTxBufferHeader*)(((struct NyLPC_TEthernetIIHeader*)i_buf)-1))-1); |
| nyatla | 69:8c5f220441f5 | 572 | return; |
| nyatla | 69:8c5f220441f5 | 573 | } |
| nyatla | 37:fc4b4fd6a649 | 574 | /** |
| nyatla | 37:fc4b4fd6a649 | 575 | * マルチキャスとアドレスへ変換する。 |
| nyatla | 37:fc4b4fd6a649 | 576 | */ |
| nyatla | 37:fc4b4fd6a649 | 577 | static void ip2MulticastEmacAddr(const struct NyLPC_TIPv4Addr* i_addr,struct NyLPC_TEthAddr* o_emac) |
| nyatla | 37:fc4b4fd6a649 | 578 | { |
| nyatla | 37:fc4b4fd6a649 | 579 | NyLPC_TUInt32 n=NyLPC_htonl(i_addr->v); |
| nyatla | 37:fc4b4fd6a649 | 580 | o_emac->addr[0]=0x01; |
| nyatla | 37:fc4b4fd6a649 | 581 | o_emac->addr[1]=0x00; |
| nyatla | 37:fc4b4fd6a649 | 582 | o_emac->addr[2]=0x5E; |
| nyatla | 37:fc4b4fd6a649 | 583 | o_emac->addr[3]=((n>>16) & 0x7f); |
| nyatla | 37:fc4b4fd6a649 | 584 | o_emac->addr[4]=((n>> 8) & 0xff); |
| nyatla | 37:fc4b4fd6a649 | 585 | o_emac->addr[5]=(n & 0xff); |
| nyatla | 37:fc4b4fd6a649 | 586 | return; |
| nyatla | 37:fc4b4fd6a649 | 587 | }; |
| nyatla | 0:142ee8b12fef | 588 | |
| nyatla | 0:142ee8b12fef | 589 | /** |
| nyatla | 0:142ee8b12fef | 590 | * ペイロードをIPパケットとしてネットワークへ送出します。 |
| nyatla | 0:142ee8b12fef | 591 | * コール前に、必ずロックしてから呼び出してください。 |
| nyatla | 0:142ee8b12fef | 592 | * @param i_eth_payload |
| nyatla | 0:142ee8b12fef | 593 | * allocTxBufで確保したメモリを指定してください。 |
| nyatla | 0:142ee8b12fef | 594 | * ペイロードには、TCP/IPパケットを格納します。 |
| nyatla | 0:142ee8b12fef | 595 | */ |
| nyatla | 0:142ee8b12fef | 596 | static NyLPC_TBool sendIPv4Tx(struct NyLPC_TTxBufferHeader* i_eth_buf) |
| nyatla | 0:142ee8b12fef | 597 | { |
| nyatla | 0:142ee8b12fef | 598 | NyLPC_TcUipService_t* inst=_NyLPC_TcUipService_inst; |
| nyatla | 37:fc4b4fd6a649 | 599 | struct NyLPC_TEthAddr emac; |
| nyatla | 0:142ee8b12fef | 600 | NyLPC_TUInt16 tx_len; |
| nyatla | 37:fc4b4fd6a649 | 601 | const struct NyLPC_TEthAddr* eth_dest; |
| nyatla | 0:142ee8b12fef | 602 | struct TEthPacket* ethbuf=(struct TEthPacket*)(i_eth_buf+1); |
| nyatla | 0:142ee8b12fef | 603 | //ペイロードのアドレスから、イーサネットフレームバッファのアドレスを復元 |
| nyatla | 37:fc4b4fd6a649 | 604 | |
| nyatla | 37:fc4b4fd6a649 | 605 | if(NyLPC_TIPv4Addr_isEqual(&(ethbuf->data.ipv4.destipaddr),&NyLPC_TIPv4Addr_BROADCAST)) { |
| nyatla | 37:fc4b4fd6a649 | 606 | //ブロードキャストならそのまま |
| nyatla | 37:fc4b4fd6a649 | 607 | eth_dest=&NyLPC_TEthAddr_BROADCAST; |
| nyatla | 37:fc4b4fd6a649 | 608 | }else if(NyLPC_TIPv4Addr_isEqualWithMask(&(ethbuf->data.ipv4.destipaddr),&NyLPC_TIPv4Addr_MULTICAST,&NyLPC_TIPv4Addr_MULTICAST_MASK)){ |
| nyatla | 37:fc4b4fd6a649 | 609 | //マルチキャスト |
| nyatla | 37:fc4b4fd6a649 | 610 | ip2MulticastEmacAddr(&(ethbuf->data.ipv4.destipaddr),&emac); |
| nyatla | 37:fc4b4fd6a649 | 611 | eth_dest=&emac; |
| nyatla | 37:fc4b4fd6a649 | 612 | }else{ |
| nyatla | 37:fc4b4fd6a649 | 613 | //LocalIP以外ならdefaultRootへ変換 |
| nyatla | 37:fc4b4fd6a649 | 614 | eth_dest=NyLPC_cIPv4Arp_IPv4toEthAddr( |
| nyatla | 37:fc4b4fd6a649 | 615 | &inst->_arp, |
| nyatla | 37:fc4b4fd6a649 | 616 | NyLPC_cIPv4Config_isLocalIP(inst->_ref_config, &(ethbuf->data.ipv4.destipaddr))?(ethbuf->data.ipv4.destipaddr):(inst->_ref_config->dr_addr)); |
| nyatla | 37:fc4b4fd6a649 | 617 | //IP->MAC変換をテスト。 |
| nyatla | 37:fc4b4fd6a649 | 618 | if(eth_dest==NULL){ |
| nyatla | 37:fc4b4fd6a649 | 619 | return NyLPC_TBool_FALSE; |
| nyatla | 37:fc4b4fd6a649 | 620 | } |
| nyatla | 0:142ee8b12fef | 621 | } |
| nyatla | 0:142ee8b12fef | 622 | //変換可能なら、イーサネットヘッダを更新して、送信処理へ。 |
| nyatla | 0:142ee8b12fef | 623 | tx_len=NyLPC_TEthernetIIHeader_setIPv4Tx(&(ethbuf->header),&(inst->_ref_config->eth_mac),eth_dest); |
| nyatla | 68:f7def7eb5504 | 624 | NyLPC_iEthernetDevice_sendTxEthFrame(inst->_ethif,i_eth_buf,tx_len); |
| nyatla | 0:142ee8b12fef | 625 | return NyLPC_TBool_TRUE; |
| nyatla | 0:142ee8b12fef | 626 | } |
| nyatla | 0:142ee8b12fef | 627 | |
| nyatla | 0:142ee8b12fef | 628 | |
| nyatla | 0:142ee8b12fef | 629 | |
| nyatla | 20:3b0b444b4deb | 630 |
MiMic Webservice library