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