From 6f577ed92e46bc8bf0463a7330ff698ec75853f2 Mon Sep 17 00:00:00 2001 From: nyatla Date: Sat, 4 Jun 2016 16:07:52 +0900 Subject: [PATCH] update libMiMic mbed.org r114 --- extlib/FreeRTOSlib/.cproject | 1729 +++++++------------ lib/Debug/makefile | 15 +- lib/Debug/sources.mk | 15 +- lib/src/NyLPC_cFifoBuffer.c | 48 +- lib/src/NyLPC_cFifoBuffer.h | 21 +- lib/src/NyLPC_cMiMicEnv.c | 21 +- lib/src/NyLPC_cMiMicEnv.h | 8 +- lib/src/NyLPC_cPtrStream.c | 60 +- lib/src/NyLPC_cPtrStream.h | 14 +- lib/src/NyLPC_cPtrStream_protected.h | 4 +- lib/src/NyLPC_cPtrTbl.c | 86 +- lib/src/NyLPC_cPtrTbl.h | 22 +- lib/src/NyLPC_cRingBuffer.c | 230 +-- lib/src/NyLPC_cRingBuffer.h | 16 +- lib/src/NyLPC_cRomPtrStream.c | 76 +- lib/src/NyLPC_cStr.c | 38 +- lib/src/NyLPC_cStr.h | 10 +- lib/src/NyLPC_stdlib.c | 256 +-- lib/src/driver/ethernet/EthDev.h | 1 + lib/src/driver/ethernet/NyLPC_IEthernetDevice.h | 32 +- lib/src/driver/ethernet/NyLPC_cEthernetMM.c | 95 +- lib/src/driver/ethernet/NyLPC_cEthernetMM.h | 106 +- lib/src/driver/ethernet/k64f/EthDev.c | 21 + lib/src/driver/ethernet/k64f/EthDev_K64F.c | 534 ++++++ .../driver/ethernet/k64f/EtherDev_K64F_protected.h | 19 + lib/src/driver/ethernet/k64f/fsl/fsl_enet_driver.c | 475 ++++++ .../ethernet/k64f/fsl/hardware_init_MK64F12.c | 95 ++ lib/src/driver/ethernet/k64f/k64f_emac_config.h | 48 + lib/src/driver/ethernet/lpc17xx/EthDev.c | 1 + lib/src/driver/ethernet/lpc17xx/EthDev_LPC17xx.c | 17 +- lib/src/driver/ethernet/lpc17xx/EthDev_LPC17xx.h | 2 +- .../driver/ethernet/lpc17xx/EtherDev_DP83848C.c | 8 +- .../ethernet/lpc17xx/EtherDev_DP83848C_protected.h | 1 + lib/src/driver/ethernet/lpc17xx/EtherDev_LAN8720.c | 10 +- .../ethernet/lpc17xx/EtherDev_LAN8720_protected.h | 1 + lib/src/driver/ethernet/lpc4088/EthDev_LPC4088.c | 69 +- .../driver/ethernet/lpc4088/copy_of_ethernet_api.h | 4 +- lib/src/driver/flash/LPC17xx_IAP.c | 124 -- lib/src/driver/flash/LPCxxx/LPC17xx_IAP.c | 126 ++ lib/src/driver/flash/{ => LPCxxx}/LPC17xx_IAP.h | 52 +- .../flash/LPCxxx/NyLPC_cMiMicConfiglation_LPCxxx.c | 105 ++ .../driver/flash/LPCxxx/NyLPC_cOnchipFlashWriter.c | 131 ++ .../flash/LPCxxx}/NyLPC_cOnchipFlashWriter.h | 68 +- lib/src/driver/flash/k64f/K64F_IAP.c | 225 +++ lib/src/driver/flash/k64f/K64F_IAP.h | 73 + .../flash/k64f/NyLPC_cMiMicConfiglation_K64F.c | 113 ++ lib/src/driver/irq/LPC1768_IRQ.c | 71 - lib/src/flash/NyLPC_cMiMicConfiglation.c | 102 -- lib/src/flash/NyLPC_cMiMicConfiglation.h | 15 +- lib/src/flash/NyLPC_cOnchipFlashWriter.c | 128 -- lib/src/http/NyLPC_cBase64.c | 1 + lib/src/http/NyLPC_cBase64.h | 1 + lib/src/http/NyLPC_cHttpBasicBodyParser.c | 295 ++-- lib/src/http/NyLPC_cHttpBasicBodyParser.h | 48 +- lib/src/http/NyLPC_cHttpBasicHeaderParser.c | 1232 +++++++------- lib/src/http/NyLPC_cHttpBasicHeaderParser.h | 106 +- .../http/NyLPC_cHttpBasicHeaderParser_protected.h | 4 +- lib/src/http/NyLPC_cHttpBodyParser.c | 64 +- lib/src/http/NyLPC_cHttpBodyParser.h | 8 +- lib/src/http/NyLPC_cHttpBodyWriter.c | 244 +-- lib/src/http/NyLPC_cHttpBodyWriter.h | 16 +- lib/src/http/NyLPC_cHttpHeaderWriter.c | 418 ++--- lib/src/http/NyLPC_cHttpHeaderWriter.h | 22 +- lib/src/http/NyLPC_cHttpNullRequestHeaderParser.c | 8 +- lib/src/http/NyLPC_cHttpNullRequestHeaderParser.h | 8 +- lib/src/http/NyLPC_cHttpStream.c | 256 +-- lib/src/http/NyLPC_cHttpStream.h | 34 +- lib/src/http/NyLPC_cHttpdConfig.h | 4 +- lib/src/http/NyLPC_cJsonWeiter.c | 62 +- lib/src/http/NyLPC_cMimeType.c | 48 +- lib/src/http/NyLPC_cMimeType.h | 4 +- lib/src/http/NyLPC_cUrlEncode.c | 76 +- lib/src/http/NyLPC_cUrlEncode.h | 14 +- lib/src/http/NyLPC_cUrlReader.c | 193 +-- lib/src/http/NyLPC_cUrlReader.h | 1 - lib/src/http/NyLPC_iHttpPtrStream.h | 24 +- lib/src/http/json/NyLPC_cJsonRpcParser.c | 2 +- lib/src/include/NyLPC_config.h | 50 +- lib/src/include/NyLPC_flash.h | 6 +- lib/src/include/NyLPC_http.h | 4 +- lib/src/include/NyLPC_jsonrpc.h | 1 + lib/src/include/NyLPC_mimicVm.h | 4 +- lib/src/include/NyLPC_net.h | 6 +- .../include/{NyLPC_uipService.h => NyLPC_netif.h} | 102 +- lib/src/include/NyLPC_stdlib.h | 144 +- lib/src/include/NyLPC_utils.h | 4 +- lib/src/jsonrpc/NyLPC_cJsonRpcFunction.h | 1 + lib/src/jsonrpc/NyLPC_cJsonRpcFunction_Memory.c | 1 + lib/src/mimicvm/NyLPC_cMiMicDbCompiler.c | 194 +-- lib/src/mimicvm/NyLPC_cMiMicDbCompiler.h | 10 +- lib/src/mimicvm/NyLPC_cMiMicTxtCompiler.c | 653 ++++---- lib/src/mimicvm/NyLPC_cMiMicTxtCompiler.h | 38 +- lib/src/mimicvm/NyLPC_cMiMicVM.c | 602 +++---- lib/src/mimicvm/NyLPC_cMiMicVM.h | 16 +- lib/src/mimicvm/NyLPC_cMiMicVM_protected.h | 136 +- lib/src/mimicvm/NyLPC_mimicvm_utils.c | 58 +- lib/src/net/NyLPC_cNet.c | 91 - lib/src/net/NyLPC_cNet.h | 85 - lib/src/net/NyLPC_cNetConfig.c | 116 +- lib/src/net/NyLPC_cNetConfig.h | 54 +- lib/src/net/apipa/NyLPC_cApipa.c | 95 -- lib/src/net/dhcp/NyLPC_cDhcpClient.c | 351 ---- lib/src/net/httpcl/NyLPC_cHttpClient.c | 21 +- lib/src/net/httpcl/NyLPC_cHttpClient.h | 21 +- lib/src/net/httpd/NyLPC_cHttpRequestPrefixParser.c | 94 +- lib/src/net/httpd/NyLPC_cHttpRequestPrefixParser.h | 8 +- lib/src/net/httpd/NyLPC_cHttpd.c | 58 +- lib/src/net/httpd/NyLPC_cHttpd.h | 28 +- lib/src/net/httpd/NyLPC_cHttpdConnection.c | 479 +++--- lib/src/net/httpd/NyLPC_cHttpdConnection.h | 57 +- .../net/httpd/NyLPC_cHttpdConnection_protected.h | 8 +- lib/src/net/httpd/NyLPC_cHttpdThread.c | 111 +- lib/src/net/httpd/NyLPC_cHttpdThread.h | 16 +- lib/src/net/httpd/NyLPC_cHttpdThread_protected.h | 4 +- lib/src/net/httpd/NyLPC_cHttpdUtils.c | 64 +- lib/src/net/httpd/NyLPC_cHttpd_protected.h | 4 +- lib/src/net/httpd/mod/NyLPC_cModFileIoBaseClass.c | 462 +++--- lib/src/net/httpd/mod/NyLPC_cModFileIoBaseClass.h | 16 +- lib/src/net/httpd/mod/NyLPC_cModJsonRpc.c | 2 +- lib/src/net/httpd/mod/NyLPC_cModMiMicSetting.c | 788 ++++----- lib/src/net/httpd/mod/NyLPC_cModMiMicSetting.h | 46 +- lib/src/net/httpd/mod/NyLPC_cModRemoteMcu.c | 770 ++++----- lib/src/net/httpd/mod/NyLPC_cModRemoteMcu.h | 2 +- lib/src/net/httpd/mod/NyLPC_cModRomFiles.c | 124 +- lib/src/net/httpd/mod/NyLPC_cModRomFiles.h | 18 +- lib/src/net/httpd/mod/NyLPC_cModUPnPDevice.c | 505 +++--- lib/src/net/httpd/mod/NyLPC_cModUPnPDevice.h | 18 +- lib/src/net/httpd/mod/NyLPC_cModUrl.c | 214 +-- lib/src/net/httpd/mod/NyLPC_cModUrl.h | 8 +- lib/src/net/httpd/mod/NyLPC_cModWebSocket.c | 2 +- lib/src/net/mdns/NyLPC_cMDnsServer.c | 1570 +++++++++++------- lib/src/net/mdns/NyLPC_cMDnsServer.h | 48 +- lib/src/net/upnp/NyLPC_UPnP_types.h | 2 +- lib/src/net/upnp/NyLPC_cSsdpSocket.c | 134 +- lib/src/net/upnp/NyLPC_cSsdpSocket.h | 10 +- lib/src/net/upnp/NyLPC_cUPnP.h | 2 +- .../NyLPC_NetIf_ethernet_types.c} | 117 +- .../NyLPC_NetIf_ethernet_types.h} | 220 +-- lib/src/netif/NyLPC_NetIf_ip_types.c | 432 +++++ .../NyLPC_uip.h => netif/NyLPC_NetIf_ip_types.h} | 942 +++++------ lib/src/{uip => netif}/NyLPC_cIPv4Config.c | 145 +- lib/src/{uip => netif}/NyLPC_cIPv4Config.h | 269 +-- lib/src/netif/NyLPC_cNet.c | 101 ++ lib/src/netif/NyLPC_cNet.h | 69 + lib/src/netif/NyLPC_iNetInterface.h | 98 ++ lib/src/netif/NyLPC_iTcpListener.h | 75 + lib/src/netif/NyLPC_iTcpSocket.h | 155 ++ lib/src/netif/NyLPC_iUdpSocket.h | 199 +++ lib/src/netif/apipa/NyLPC_cApipa.c | 91 + lib/src/{net => netif}/apipa/NyLPC_cApipa.h | 112 +- lib/src/netif/dhcp/NyLPC_cDhcpClient.c | 354 ++++ lib/src/{net => netif}/dhcp/NyLPC_cDhcpClient.h | 147 +- lib/src/netif/mimicip/NyLPC_cIPv4.c | 289 ++++ lib/src/{uip => netif/mimicip}/NyLPC_cIPv4.h | 436 +++-- lib/src/{uip => netif/mimicip}/NyLPC_cIPv4Arp.c | 669 ++++---- lib/src/{uip => netif/mimicip}/NyLPC_cIPv4Arp.h | 231 +-- lib/src/{uip => netif/mimicip}/NyLPC_cIPv4IComp.c | 297 ++-- lib/src/{uip => netif/mimicip}/NyLPC_cIPv4IComp.h | 145 +- .../mimicip}/NyLPC_cIPv4IComp_protected.h | 173 +- .../{uip => netif/mimicip}/NyLPC_cIPv4Payload.c | 286 ++-- .../{uip => netif/mimicip}/NyLPC_cIPv4Payload.h | 189 +-- .../mimicip}/NyLPC_cIPv4Payload_protected.h | 245 ++- lib/src/netif/mimicip/NyLPC_cMiMicIpNetIf.c | 953 +++++++++++ .../mimicip/NyLPC_cMiMicIpNetIf.h} | 252 ++- .../mimicip/NyLPC_cMiMicIpNetIf_protected.h} | 349 ++-- lib/src/netif/mimicip/NyLPC_cMiMicIpTcpListener.c | 219 +++ .../mimicip/NyLPC_cMiMicIpTcpListener.h} | 195 +-- .../mimicip/NyLPC_cMiMicIpTcpListener_protected.h} | 97 +- lib/src/netif/mimicip/NyLPC_cMiMicIpTcpSocket.c | 1542 +++++++++++++++++ lib/src/netif/mimicip/NyLPC_cMiMicIpTcpSocket.h | 151 ++ .../mimicip/NyLPC_cMiMicIpTcpSocket_protected.h} | 228 +-- lib/src/netif/mimicip/NyLPC_cMiMicIpUdpSocket.c | 430 +++++ lib/src/netif/mimicip/NyLPC_cMiMicIpUdpSocket.h | 78 + .../mimicip/NyLPC_cMiMicIpUdpSocket_protected.h} | 109 +- lib/src/os/NyLPC_cIsr.h | 2 +- lib/src/os/NyLPC_cMutex.h | 4 +- lib/src/os/NyLPC_cSemaphore.h | 2 +- lib/src/os/NyLPC_cThread.h | 4 +- lib/src/os/mbedrtos/NyLPC_cIsr.cpp | 70 + lib/src/os/mbedrtos/NyLPC_cMutex.cpp | 51 + lib/src/os/mbedrtos/NyLPC_cSemaphore.cpp | 51 + lib/src/os/mbedrtos/NyLPC_cStopwatch.cpp | 99 ++ lib/src/os/mbedrtos/NyLPC_cThread.cpp | 90 + lib/src/supporter/NyLPC_cSupporter.c | 1 + lib/src/uip/NyLPC_IPv6Icmp6.c | 157 -- lib/src/uip/NyLPC_cBaseSocket.c | 9 - lib/src/uip/NyLPC_cBaseSocket.h | 63 - lib/src/uip/NyLPC_cIPv4.c | 539 ------ lib/src/uip/NyLPC_cIPv4_typedef.h | 16 - lib/src/uip/NyLPC_cIPv6Tcp.c | 176 -- lib/src/uip/NyLPC_cIPv6Udp.c | 78 - lib/src/uip/NyLPC_cTcpListener.c | 213 --- lib/src/uip/NyLPC_cTcpSocket.c | 1492 ----------------- lib/src/uip/NyLPC_cTcpSocket.h | 226 --- lib/src/uip/NyLPC_cUdpSocket.c | 364 ---- lib/src/uip/NyLPC_cUdpSocket.h | 159 -- lib/src/uip/NyLPC_cUipService.c | 630 ------- lib/src/uip/NyLPC_uip.c | 431 ----- lib/src/utils/NyLPC_cFormatTextReader.c | 86 +- lib/src/utils/NyLPC_cFormatWriter.c | 484 +++--- lib/src/utils/NyLPC_cFormatWriter.h | 12 +- lib/src/utils/NyLPC_cRomFileSet.c | 24 +- lib/src/utils/NyLPC_cRomFileSet.h | 16 +- lib/src/utils/NyLPC_cUuid.h | 18 +- lib/src/utils/sha1/sha1.c | 1 + lib/src/utils/sha1/sha1.h | 1 + projects/app.RemoteMCU/.cproject | 1750 +++++++------------- projects/example/sample.ISA/.cproject | 1745 +++++++------------ projects/example/sample.ISA/.project | 2 +- projects/example/sample.ISA/src/sketch.c | 9 +- projects/example/sample.net.apipa/.cproject | 818 +-------- projects/example/sample.net.apipa/src/sketch.c | 10 +- projects/example/sample.net.dhcp/.cproject | 818 +-------- projects/example/sample.net.dhcp/src/sketch.c | 11 +- projects/example/sample.net.httpcl/.cproject | 821 +-------- projects/example/sample.net.httpcl/src/sketch.c | 14 +- projects/example/sample.net.loopback/.cproject | 1750 +++++++------------- projects/example/sample.net.loopback/src/sketch.c | 39 +- projects/example/sample.net.mdns/.cproject | 820 +-------- projects/example/sample.net.mdns/src/sketch.c | 10 +- projects/example/sample.net.simplehttpd/.cproject | 728 +------- .../example/sample.net.simplehttpd/src/sketch.c | 9 +- projects/example/sample.net.udp/.cproject | 824 +-------- projects/example/sample.net.udp/src/sketch.c | 20 +- projects/example/test.bigdata/.cproject | 722 +------- projects/example/test.bigdata/src/sketch.c | 9 +- projects/example/test.httppost/.cproject | 728 +------- projects/example/test.httppost/src/sketch.c | 4 +- projects/example/test.modfileio/.cproject | 728 +------- projects/example/test.modfileio/src/sketch.c | 7 +- projects/example/test.udp/.cproject | 818 +-------- projects/example/test.udp/src/sketch.c | 6 +- projects/example/test.upnp/.cproject | 818 +-------- projects/example/test.upnp/src/sketch.c | 7 +- projects/example/test.websocket/.cproject | 722 +------- projects/example/test.websocket/src/sketch.c | 9 +- projects/example/test/.cproject | 1744 +++++++------------ projects/example/test/src/sketch.c | 82 +- projects/sketch/.cproject | 1750 +++++++------------- projects/standard/.cproject | 1750 +++++++------------- 240 files changed, 22230 insertions(+), 31800 deletions(-) create mode 100644 lib/src/driver/ethernet/k64f/EthDev.c create mode 100644 lib/src/driver/ethernet/k64f/EthDev_K64F.c create mode 100644 lib/src/driver/ethernet/k64f/EtherDev_K64F_protected.h create mode 100644 lib/src/driver/ethernet/k64f/fsl/fsl_enet_driver.c create mode 100644 lib/src/driver/ethernet/k64f/fsl/hardware_init_MK64F12.c create mode 100644 lib/src/driver/ethernet/k64f/k64f_emac_config.h delete mode 100644 lib/src/driver/flash/LPC17xx_IAP.c create mode 100644 lib/src/driver/flash/LPCxxx/LPC17xx_IAP.c rename lib/src/driver/flash/{ => LPCxxx}/LPC17xx_IAP.h (96%) create mode 100644 lib/src/driver/flash/LPCxxx/NyLPC_cMiMicConfiglation_LPCxxx.c create mode 100644 lib/src/driver/flash/LPCxxx/NyLPC_cOnchipFlashWriter.c rename lib/src/{flash => driver/flash/LPCxxx}/NyLPC_cOnchipFlashWriter.h (97%) create mode 100644 lib/src/driver/flash/k64f/K64F_IAP.c create mode 100644 lib/src/driver/flash/k64f/K64F_IAP.h create mode 100644 lib/src/driver/flash/k64f/NyLPC_cMiMicConfiglation_K64F.c delete mode 100644 lib/src/driver/irq/LPC1768_IRQ.c delete mode 100644 lib/src/flash/NyLPC_cMiMicConfiglation.c delete mode 100644 lib/src/flash/NyLPC_cOnchipFlashWriter.c rename lib/src/include/{NyLPC_uipService.h => NyLPC_netif.h} (83%) delete mode 100644 lib/src/net/NyLPC_cNet.c delete mode 100644 lib/src/net/NyLPC_cNet.h delete mode 100644 lib/src/net/apipa/NyLPC_cApipa.c delete mode 100644 lib/src/net/dhcp/NyLPC_cDhcpClient.c rename lib/src/{uip/NyLPC_uip_ethernet.c => netif/NyLPC_NetIf_ethernet_types.c} (93%) rename lib/src/{uip/NyLPC_uip_ethernet.h => netif/NyLPC_NetIf_ethernet_types.h} (92%) create mode 100644 lib/src/netif/NyLPC_NetIf_ip_types.c rename lib/src/{uip/NyLPC_uip.h => netif/NyLPC_NetIf_ip_types.h} (71%) rename lib/src/{uip => netif}/NyLPC_cIPv4Config.c (70%) rename lib/src/{uip => netif}/NyLPC_cIPv4Config.h (84%) create mode 100644 lib/src/netif/NyLPC_cNet.c create mode 100644 lib/src/netif/NyLPC_cNet.h create mode 100644 lib/src/netif/NyLPC_iNetInterface.h create mode 100644 lib/src/netif/NyLPC_iTcpListener.h create mode 100644 lib/src/netif/NyLPC_iTcpSocket.h create mode 100644 lib/src/netif/NyLPC_iUdpSocket.h create mode 100644 lib/src/netif/apipa/NyLPC_cApipa.c rename lib/src/{net => netif}/apipa/NyLPC_cApipa.h (90%) create mode 100644 lib/src/netif/dhcp/NyLPC_cDhcpClient.c rename lib/src/{net => netif}/dhcp/NyLPC_cDhcpClient.h (85%) create mode 100644 lib/src/netif/mimicip/NyLPC_cIPv4.c rename lib/src/{uip => netif/mimicip}/NyLPC_cIPv4.h (73%) rename lib/src/{uip => netif/mimicip}/NyLPC_cIPv4Arp.c (57%) rename lib/src/{uip => netif/mimicip}/NyLPC_cIPv4Arp.h (89%) rename lib/src/{uip => netif/mimicip}/NyLPC_cIPv4IComp.c (50%) rename lib/src/{uip => netif/mimicip}/NyLPC_cIPv4IComp.h (92%) rename lib/src/{uip => netif/mimicip}/NyLPC_cIPv4IComp_protected.h (89%) rename lib/src/{uip => netif/mimicip}/NyLPC_cIPv4Payload.c (72%) rename lib/src/{uip => netif/mimicip}/NyLPC_cIPv4Payload.h (83%) rename lib/src/{uip => netif/mimicip}/NyLPC_cIPv4Payload_protected.h (89%) create mode 100644 lib/src/netif/mimicip/NyLPC_cMiMicIpNetIf.c rename lib/src/{uip/NyLPC_cUipService.h => netif/mimicip/NyLPC_cMiMicIpNetIf.h} (64%) rename lib/src/{uip/NyLPC_cUipService_protected.h => netif/mimicip/NyLPC_cMiMicIpNetIf_protected.h} (54%) create mode 100644 lib/src/netif/mimicip/NyLPC_cMiMicIpTcpListener.c rename lib/src/{uip/NyLPC_cTcpListener.h => netif/mimicip/NyLPC_cMiMicIpTcpListener.h} (67%) rename lib/src/{uip/NyLPC_cTcpListener_protected.h => netif/mimicip/NyLPC_cMiMicIpTcpListener_protected.h} (79%) create mode 100644 lib/src/netif/mimicip/NyLPC_cMiMicIpTcpSocket.c create mode 100644 lib/src/netif/mimicip/NyLPC_cMiMicIpTcpSocket.h rename lib/src/{uip/NyLPC_cTcpSocket_protected.h => netif/mimicip/NyLPC_cMiMicIpTcpSocket_protected.h} (71%) create mode 100644 lib/src/netif/mimicip/NyLPC_cMiMicIpUdpSocket.c create mode 100644 lib/src/netif/mimicip/NyLPC_cMiMicIpUdpSocket.h rename lib/src/{uip/NyLPC_cUdpSocket_protected.h => netif/mimicip/NyLPC_cMiMicIpUdpSocket_protected.h} (67%) create mode 100644 lib/src/os/mbedrtos/NyLPC_cIsr.cpp create mode 100644 lib/src/os/mbedrtos/NyLPC_cMutex.cpp create mode 100644 lib/src/os/mbedrtos/NyLPC_cSemaphore.cpp create mode 100644 lib/src/os/mbedrtos/NyLPC_cStopwatch.cpp create mode 100644 lib/src/os/mbedrtos/NyLPC_cThread.cpp delete mode 100644 lib/src/uip/NyLPC_IPv6Icmp6.c delete mode 100644 lib/src/uip/NyLPC_cBaseSocket.c delete mode 100644 lib/src/uip/NyLPC_cBaseSocket.h delete mode 100644 lib/src/uip/NyLPC_cIPv4.c delete mode 100644 lib/src/uip/NyLPC_cIPv4_typedef.h delete mode 100644 lib/src/uip/NyLPC_cIPv6Tcp.c delete mode 100644 lib/src/uip/NyLPC_cIPv6Udp.c delete mode 100644 lib/src/uip/NyLPC_cTcpListener.c delete mode 100644 lib/src/uip/NyLPC_cTcpSocket.c delete mode 100644 lib/src/uip/NyLPC_cTcpSocket.h delete mode 100644 lib/src/uip/NyLPC_cUdpSocket.c delete mode 100644 lib/src/uip/NyLPC_cUdpSocket.h delete mode 100644 lib/src/uip/NyLPC_cUipService.c delete mode 100644 lib/src/uip/NyLPC_uip.c diff --git a/extlib/FreeRTOSlib/.cproject b/extlib/FreeRTOSlib/.cproject index 659e0fc..02a08ad 100644 --- a/extlib/FreeRTOSlib/.cproject +++ b/extlib/FreeRTOSlib/.cproject @@ -1,7 +1,5 @@ - - - + @@ -9,21 +7,22 @@ + - - + + - + @@ -57,6 +56,7 @@ @@ -70,571 +70,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -642,21 +77,22 @@ + - - + + - + @@ -685,6 +121,7 @@ @@ -698,571 +135,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -1271,11 +143,8 @@ <?xml version="1.0" encoding="UTF-8"?> <TargetConfig> -<Properties property_0="" property_1="" property_2="" property_3="NXP" property_4="LPC1769" property_count="5" version="1"/> -<infoList vendor="NXP"> -<info chip="LPC1769" match_id="0x26113F37" name="LPC1769" package="lpc17_lqfp100.xml"> -<chip> -<name>LPC1769</name> +<Properties property_0="" property_2="LPC175x_6x_512.cfx" property_3="NXP" property_4="LPC1769" property_count="5" version="70002"/> +<infoList vendor="NXP"><info chip="LPC1769" flash_driver="LPC175x_6x_512.cfx" match_id="0x26113F37" name="LPC1769" package="lpc17_lqfp100.xml" stub="crt_emu_cm3_nxp"><chip><name>LPC1769</name> <family>LPC17xx</family> <vendor>NXP (formerly Philips)</vendor> <reset board="None" core="Real" sys="Real"/> @@ -1334,9 +203,10 @@ <peripheralInstance derived_from="LPC17_CANWAKESLEEP" determined="infoFile" id="CANWAKESLEEP" location="0x400FC110"/> <peripheralInstance derived_from="LPC17_CANCON" determined="infoFile" enable="SYSCTL.PCONP.PCCAN1&amp;0x1" id="CANCON1" location="0x40044000"/> <peripheralInstance derived_from="LPC17_CANCON" determined="infoFile" enable="SYSCTL.PCONP.PCCAN2&amp;0x1" id="CANCON2" location="0x40048000"/> +<peripheralInstance derived_from="LPC17_MCPWM" determined="infoFile" enable="SYSCTL.PCONP.PCMCPWM&amp;0x1" id="MCPWM" location="0x400B8000"/> +<peripheralInstance derived_from="LPC17_FMC" determined="infoFile" id="FMC" location="0x40084000"/> </chip> -<processor> -<name gcc_name="cortex-m3">Cortex-M3</name> +<processor><name gcc_name="cortex-m3">Cortex-M3</name> <family>Cortex-M</family> </processor> <link href="nxp_lpcxxxx_peripheral.xme" show="embed" type="simple"/> @@ -1344,4 +214,571 @@ </infoList> </TargetConfig> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/lib/Debug/makefile b/lib/Debug/makefile index 33e0516..2541052 100644 --- a/lib/Debug/makefile +++ b/lib/Debug/makefile @@ -10,28 +10,29 @@ RM := rm -rf -include sources.mk -include src/utils/sha1/subdir.mk -include src/utils/subdir.mk --include src/uip/subdir.mk -include src/supporter/subdir.mk -include src/os/freertos/subdir.mk +-include src/netif/mimicip/subdir.mk +-include src/netif/dhcp/subdir.mk +-include src/netif/apipa/subdir.mk +-include src/netif/subdir.mk -include src/net/upnp/subdir.mk -include src/net/mdns/subdir.mk -include src/net/httpd/mod/subdir.mk -include src/net/httpd/subdir.mk -include src/net/httpcl/subdir.mk --include src/net/dhcp/subdir.mk --include src/net/apipa/subdir.mk -include src/net/subdir.mk -include src/mimicvm/subdir.mk -include src/jsonrpc/subdir.mk -include src/include/boot/subdir.mk -include src/http/json/subdir.mk -include src/http/subdir.mk --include src/flash/subdir.mk --include src/econetlite/subdir.mk --include src/driver/irq/subdir.mk --include src/driver/flash/subdir.mk +-include src/driver/flash/k64f/subdir.mk +-include src/driver/flash/LPCxxx/subdir.mk -include src/driver/ethernet/lpc4088/subdir.mk -include src/driver/ethernet/lpc17xx/subdir.mk +-include src/driver/ethernet/k64f/fsl/subdir.mk +-include src/driver/ethernet/k64f/subdir.mk -include src/driver/ethernet/subdir.mk -include src/subdir.mk -include subdir.mk diff --git a/lib/Debug/sources.mk b/lib/Debug/sources.mk index 4a09ebd..663278f 100644 --- a/lib/Debug/sources.mk +++ b/lib/Debug/sources.mk @@ -16,28 +16,29 @@ ARCHIVES := SUBDIRS := \ src/utils/sha1 \ src/utils \ -src/uip \ src/supporter \ src/os/freertos \ +src/netif/mimicip \ +src/netif/dhcp \ +src/netif/apipa \ +src/netif \ src/net/upnp \ src/net/mdns \ src/net/httpd/mod \ src/net/httpd \ src/net/httpcl \ -src/net/dhcp \ -src/net/apipa \ src/net \ src/mimicvm \ src/jsonrpc \ src/include/boot \ src/http/json \ src/http \ -src/flash \ -src/econetlite \ -src/driver/irq \ -src/driver/flash \ +src/driver/flash/k64f \ +src/driver/flash/LPCxxx \ src/driver/ethernet/lpc4088 \ src/driver/ethernet/lpc17xx \ +src/driver/ethernet/k64f/fsl \ +src/driver/ethernet/k64f \ src/driver/ethernet \ src \ diff --git a/lib/src/NyLPC_cFifoBuffer.c b/lib/src/NyLPC_cFifoBuffer.c index 1bf0183..5ee2175 100644 --- a/lib/src/NyLPC_cFifoBuffer.c +++ b/lib/src/NyLPC_cFifoBuffer.c @@ -19,8 +19,8 @@ * along with this program. If not, see . * * For further information please contact. - * http://nyatla.jp/ - * or + * http://nyatla.jp/ + * or * *********************************************************************************/ #include "NyLPC_cFifoBuffer.h" @@ -31,9 +31,9 @@ */ void NyLPC_cFifoBuffer_initialize(NyLPC_TcFifoBuffer_t* i_inst,void* i_buf,NyLPC_TUInt16 i_buf_size) { - i_inst->len=0; - i_inst->size=i_buf_size; - i_inst->buf=i_buf; + i_inst->len=0; + i_inst->size=i_buf_size; + i_inst->buf=i_buf; } @@ -42,12 +42,20 @@ void NyLPC_cFifoBuffer_initialize(NyLPC_TcFifoBuffer_t* i_inst,void* i_buf,NyLPC */ void NyLPC_cFifoBuffer_push(NyLPC_TcFifoBuffer_t* i_inst,const void* i_data,NyLPC_TUInt16 i_data_len) { - NyLPC_TUInt8* wp; - NyLPC_ArgAssert(NyLPC_cFifoBuffer_getSpace(i_inst)>=i_data_len); + NyLPC_TUInt8* wp; + NyLPC_ArgAssert(NyLPC_cFifoBuffer_getSpace(i_inst)>=i_data_len); - wp=((NyLPC_TUInt8*)(i_inst->buf))+i_inst->len; - memcpy(wp,i_data,i_data_len); - i_inst->len+=i_data_len; + wp=((NyLPC_TUInt8*)(i_inst->buf))+i_inst->len; + memcpy(wp,i_data,i_data_len); + i_inst->len+=i_data_len; +} +void* NyLPC_cFifoBuffer_prePush(NyLPC_TcFifoBuffer_t* i_inst,NyLPC_TUInt16 i_data_len) +{ + NyLPC_TUInt8* wp; + NyLPC_ArgAssert(NyLPC_cFifoBuffer_getSpace(i_inst)>=i_data_len); + wp=((NyLPC_TUInt8*)(i_inst->buf))+i_inst->len; + i_inst->len+=i_data_len; + return wp; } /** @@ -55,13 +63,13 @@ void NyLPC_cFifoBuffer_push(NyLPC_TcFifoBuffer_t* i_inst,const void* i_data,NyLP */ void NyLPC_cFifoBuffer_pop(NyLPC_TcFifoBuffer_t* i_inst,NyLPC_TUInt16 i_len) { - NyLPC_TUInt8* wp; - NyLPC_TUInt8* rp; - NyLPC_ArgAssert(NyLPC_cFifoBuffer_getLength(i_inst)>=i_len); - wp=(NyLPC_TUInt8*)i_inst->buf; - rp=wp+i_len; - i_inst->len-=i_len; - memmove(wp,rp,i_inst->len); + NyLPC_TUInt8* wp; + NyLPC_TUInt8* rp; + NyLPC_ArgAssert(NyLPC_cFifoBuffer_getLength(i_inst)>=i_len); + wp=(NyLPC_TUInt8*)i_inst->buf; + rp=wp+i_len; + i_inst->len-=i_len; + memmove(wp,rp,i_inst->len); } /** @@ -69,19 +77,19 @@ void NyLPC_cFifoBuffer_pop(NyLPC_TcFifoBuffer_t* i_inst,NyLPC_TUInt16 i_len) */ void* NyLPC_cFifoBuffer_getPtr(const NyLPC_TcFifoBuffer_t* i_inst) { - return (void*)(i_inst->buf); + return (void*)(i_inst->buf); } /** * See Header file. */ NyLPC_TUInt16 NyLPC_cFifoBuffer_getLength(const NyLPC_TcFifoBuffer_t* i_inst) { - return i_inst->len; + return i_inst->len; } /** * See Header file. */ NyLPC_TUInt16 NyLPC_cFifoBuffer_getSpace(const NyLPC_TcFifoBuffer_t* i_inst) { - return i_inst->size-i_inst->len; + return i_inst->size-i_inst->len; } diff --git a/lib/src/NyLPC_cFifoBuffer.h b/lib/src/NyLPC_cFifoBuffer.h index a101993..728d72b 100644 --- a/lib/src/NyLPC_cFifoBuffer.h +++ b/lib/src/NyLPC_cFifoBuffer.h @@ -19,8 +19,8 @@ * along with this program. If not, see . * * For further information please contact. - * http://nyatla.jp/ - * or + * http://nyatla.jp/ + * or * *********************************************************************************/ /** @@ -47,12 +47,12 @@ typedef struct NyLPC_TcFifoBuffer NyLPC_TcFifoBuffer_t; */ struct NyLPC_TcFifoBuffer { - /** 配列の最大サイズ*/ - NyLPC_TUInt16 size; - /** 配列の現在の長さ*/ - NyLPC_TUInt16 len; - /** バッファ領域*/ - void* buf; + /** 配列の最大サイズ*/ + NyLPC_TUInt16 size; + /** 配列の現在の長さ*/ + NyLPC_TUInt16 len; + /** バッファ領域*/ + void* buf; }; /** @@ -97,6 +97,11 @@ void NyLPC_cFifoBuffer_initialize(NyLPC_TcFifoBuffer_t* i_inst,void* i_buf,NyLPC * */ void NyLPC_cFifoBuffer_push(NyLPC_TcFifoBuffer_t* i_inst,const void* i_data,NyLPC_TUInt16 i_data_len); +/** + * 先にメモリ領域を確保して、そのアドレスを返します。 + */ +void* NyLPC_cFifoBuffer_prePush(NyLPC_TcFifoBuffer_t* i_inst,NyLPC_TUInt16 i_data_len); + /** * この関数は、バッファの先頭からデータを削除します。 diff --git a/lib/src/NyLPC_cMiMicEnv.c b/lib/src/NyLPC_cMiMicEnv.c index 03c9917..5eafa7d 100644 --- a/lib/src/NyLPC_cMiMicEnv.c +++ b/lib/src/NyLPC_cMiMicEnv.c @@ -1,19 +1,24 @@ #include "NyLPC_cMiMicEnv.h" -#include "../uip/NyLPC_cUipService_protected.h" +#include "NyLPC_netif.h" -const static char* VERSION="MiMic/1.6.0"; +const static char* VERSION="MiMic/1.7.2"; #if NyLPC_MCU==NyLPC_MCU_LPC4088 const static char* MCU="LPC4088"; +static const char* PNAME_LPCXPRESSO="LPCXpresso"; +static const char* PNAME_MBED="mbed"; #elif NyLPC_MCU==NyLPC_MCU_LPC17xx const static char* MCU="LPC176x"; +static const char* PNAME_LPCXPRESSO="LPCXpresso"; +static const char* PNAME_MBED="mbed"; +#elif NyLPC_MCU==NyLPC_MCU_K64F +const static char* MCU="K64F"; +static const char* PNAME_FRDM="FRDM"; #endif const static char* UNKNOWN="UNKNOWN"; -static const char* PNAME_LPCXPRESSO="LPCXpresso"; -static const char* PNAME_MBED="mbed"; @@ -23,7 +28,10 @@ const char* NyLPC_cMiMicEnv_getStrProperty(NyLPC_TUInt16 i_id) case NyLPC_cMiMicEnv_VERSION: return VERSION; case NyLPC_cMiMicEnv_SHORT_NAME: - switch(*(NyLPC_cUipService_refDeviceName())){ +#if NyLPC_MCU==NyLPC_MCU_K64F + return PNAME_FRDM; +#else + switch(*(NyLPC_cNet_getInterfaceInfo()->device_name)){ case 'L': return PNAME_LPCXPRESSO; case 'D': @@ -31,8 +39,9 @@ const char* NyLPC_cMiMicEnv_getStrProperty(NyLPC_TUInt16 i_id) default: return UNKNOWN; } +#endif case NyLPC_cMiMicEnv_ETHERNET_PHY: - return NyLPC_cUipService_refDeviceName(); + return NyLPC_cNet_getInterfaceInfo()->device_name; case NyLPC_cMiMicEnv_MCU_NAME: return MCU; default: diff --git a/lib/src/NyLPC_cMiMicEnv.h b/lib/src/NyLPC_cMiMicEnv.h index 041dc1d..3c0bc28 100644 --- a/lib/src/NyLPC_cMiMicEnv.h +++ b/lib/src/NyLPC_cMiMicEnv.h @@ -14,10 +14,10 @@ extern "C" { #include "NyLPC_stdlib.h" -#define NyLPC_cMiMicEnv_VERSION 1 -#define NyLPC_cMiMicEnv_SHORT_NAME 2 -#define NyLPC_cMiMicEnv_ETHERNET_PHY 3 -#define NyLPC_cMiMicEnv_MCU_NAME 4 +#define NyLPC_cMiMicEnv_VERSION 1 +#define NyLPC_cMiMicEnv_SHORT_NAME 2 +#define NyLPC_cMiMicEnv_ETHERNET_PHY 3 +#define NyLPC_cMiMicEnv_MCU_NAME 4 const char* NyLPC_cMiMicEnv_getStrProperty(NyLPC_TUInt16 i_id); diff --git a/lib/src/NyLPC_cPtrStream.c b/lib/src/NyLPC_cPtrStream.c index 0fbb6e2..319bffb 100644 --- a/lib/src/NyLPC_cPtrStream.c +++ b/lib/src/NyLPC_cPtrStream.c @@ -19,8 +19,8 @@ * along with this program. If not, see . * * For further information please contact. - * http://nyatla.jp/ - * or + * http://nyatla.jp/ + * or * *********************************************************************************/ @@ -29,40 +29,40 @@ const struct NyLPC_TcPtrStream_TInterface NyLPC_TcPtrStream_Interface={ - NyLPC_cPtrStream_pread_func, - NyLPC_cPtrStream_write_func, - NyLPC_cPtrStream_pseek_func, - NyLPC_cPtrStream_close_func + NyLPC_cPtrStream_pread_func, + NyLPC_cPtrStream_write_func, + NyLPC_cPtrStream_pseek_func, + NyLPC_cPtrStream_close_func }; /*private*/ NyLPC_TInt32 NyLPC_cPtrStream_pread_func(NyLPC_TcPtrStream_t* i_inst,const void** o_buf_ptr,NyLPC_TUInt32 i_wait_msec) { - (void)i_inst; - (void)o_buf_ptr; - (void)i_wait_msec; - return -1; + (void)i_inst; + (void)o_buf_ptr; + (void)i_wait_msec; + return -1; } NyLPC_TBool NyLPC_cPtrStream_write_func(NyLPC_TcPtrStream_t* i_inst,const void* i_data,NyLPC_TInt16 i_length,NyLPC_TUInt32 i_wait_msec) { - (void)i_inst; - (void)i_data; - (void)i_length; - (void)i_wait_msec; - return NyLPC_TBool_FALSE; + (void)i_inst; + (void)i_data; + (void)i_length; + (void)i_wait_msec; + return NyLPC_TBool_FALSE; } void NyLPC_cPtrStream_pseek_func(NyLPC_TcPtrStream_t* i_inst,NyLPC_TUInt16 i_seek) { - (void)i_inst; - (void)i_seek; + (void)i_inst; + (void)i_seek; } void NyLPC_cPtrStream_close_func(NyLPC_TcPtrStream_t* i_inst) { - (void)i_inst; + (void)i_inst; } @@ -71,22 +71,22 @@ void NyLPC_cPtrStream_close_func(NyLPC_TcPtrStream_t* i_inst) */ NyLPC_TBool NyLPC_cPtrStream_writeln(NyLPC_TcPtrStream_t* i_inst,const void* i_data,NyLPC_TInt16 i_length,NyLPC_TUInt32 i_wait_msec) { - if(NyLPC_cPtrStream_write(i_inst,i_data,i_length,i_wait_msec)){ - if(NyLPC_cPtrStream_write(i_inst,"\r\n",2,i_wait_msec)){ - return NyLPC_TBool_TRUE; - } - } - return NyLPC_TBool_FALSE; + if(NyLPC_cPtrStream_write(i_inst,i_data,i_length,i_wait_msec)){ + if(NyLPC_cPtrStream_write(i_inst,"\r\n",2,i_wait_msec)){ + return NyLPC_TBool_TRUE; + } + } + return NyLPC_TBool_FALSE; } /** * See Header file. */ NyLPC_TBool NyLPC_cPtrStream_writeInt(NyLPC_TcPtrStream_t* i_inst,NyLPC_TInt32 i_val,NyLPC_TUInt32 i_wait_msec,NyLPC_TUInt32 i_base) { - NyLPC_TChar v[12]; - NyLPC_itoa(i_val,v,i_base); - if(NyLPC_cPtrStream_write(i_inst,v,strlen(v),i_wait_msec)){ - return NyLPC_TBool_TRUE; - } - return NyLPC_TBool_FALSE; + NyLPC_TChar v[12]; + NyLPC_itoa(i_val,v,i_base); + if(NyLPC_cPtrStream_write(i_inst,v,strlen(v),i_wait_msec)){ + return NyLPC_TBool_TRUE; + } + return NyLPC_TBool_FALSE; } diff --git a/lib/src/NyLPC_cPtrStream.h b/lib/src/NyLPC_cPtrStream.h index 9cad0d1..681fa48 100644 --- a/lib/src/NyLPC_cPtrStream.h +++ b/lib/src/NyLPC_cPtrStream.h @@ -19,8 +19,8 @@ * along with this program. If not, see . * * For further information please contact. - * http://nyatla.jp/ - * or + * http://nyatla.jp/ + * or * *********************************************************************************/ #ifndef NYLPC_TCPTRSTREAM_H_ @@ -50,16 +50,16 @@ typedef void (*NyLPC_TcPtrStream_close)(NyLPC_TcPtrStream_t* i_inst); struct NyLPC_TcPtrStream_TInterface { - NyLPC_TcPtrStream_pread pread; - NyLPC_TcPtrStream_write write; - NyLPC_TcPtrStream_rseek readSeek; - NyLPC_TcPtrStream_close close; + NyLPC_TcPtrStream_pread pread; + NyLPC_TcPtrStream_write write; + NyLPC_TcPtrStream_rseek readSeek; + NyLPC_TcPtrStream_close close; }; struct NyLPC_TcPtrStream { - const struct NyLPC_TcPtrStream_TInterface* _interface; + const struct NyLPC_TcPtrStream_TInterface* _interface; }; /** diff --git a/lib/src/NyLPC_cPtrStream_protected.h b/lib/src/NyLPC_cPtrStream_protected.h index e45886c..36aeb06 100644 --- a/lib/src/NyLPC_cPtrStream_protected.h +++ b/lib/src/NyLPC_cPtrStream_protected.h @@ -19,8 +19,8 @@ * along with this program. If not, see . * * For further information please contact. - * http://nyatla.jp/ - * or + * http://nyatla.jp/ + * or * *********************************************************************************/ #ifndef NyLPC_cPtrStream_protected_h diff --git a/lib/src/NyLPC_cPtrTbl.c b/lib/src/NyLPC_cPtrTbl.c index 4fc42d5..f00cf99 100644 --- a/lib/src/NyLPC_cPtrTbl.c +++ b/lib/src/NyLPC_cPtrTbl.c @@ -19,55 +19,55 @@ * along with this program. If not, see . * * For further information please contact. - * http://nyatla.jp/ - * or + * http://nyatla.jp/ + * or * *********************************************************************************/ #include "NyLPC_cPtrTbl.h" void NyLPC_cPtrTbl_initialize(NyLPC_TcPtrTbl_t* i_inst,void** i_buf,NyLPC_TUInt16 i_size) { - i_inst->size=i_size; - i_inst->len=0; - i_inst->buf=i_buf; - memset(i_inst->buf,0,i_size*sizeof(void*)); + i_inst->size=i_size; + i_inst->len=0; + i_inst->buf=i_buf; + memset(i_inst->buf,0,i_size*sizeof(void*)); } void* NyLPC_cPtrTbl_get(NyLPC_TcPtrTbl_t* i_inst,NyLPC_TUInt16 i_index) { - return i_inst->buf[i_index]; + return i_inst->buf[i_index]; } void NyLPC_cPtrTbl_set(NyLPC_TcPtrTbl_t* i_inst,NyLPC_TUInt16 i_index,void* i_val) { - void** p=(i_inst->buf+i_index); - if(*p==NULL){ - if(i_val!=NULL){ - //投入 - i_inst->len++; - } - }else{ - if(i_val==NULL){ - //消去 - i_inst->len--; - } - } - //値を反映 - *p=i_val; - return; + void** p=(i_inst->buf+i_index); + if(*p==NULL){ + if(i_val!=NULL){ + //投入 + i_inst->len++; + } + }else{ + if(i_val==NULL){ + //消去 + i_inst->len--; + } + } + //値を反映 + *p=i_val; + return; } NyLPC_TInt16 NyLPC_cPtrTbl_add(NyLPC_TcPtrTbl_t* i_inst,void* i_val) { - int i; - void** p=i_inst->buf; + int i; + void** p=i_inst->buf; - for(i=i_inst->size-1;i>=0;i--){ - if(*(p+i)==NULL){ - NyLPC_cPtrTbl_set(i_inst,i,i_val); - return i; - } - } - return -1; + for(i=i_inst->size-1;i>=0;i--){ + if(*(p+i)==NULL){ + NyLPC_cPtrTbl_set(i_inst,i,i_val); + return i; + } + } + return -1; } /** @@ -75,21 +75,21 @@ NyLPC_TInt16 NyLPC_cPtrTbl_add(NyLPC_TcPtrTbl_t* i_inst,void* i_val) */ void NyLPC_cPtrTbl_remove(NyLPC_TcPtrTbl_t* i_inst,NyLPC_TUInt16 i_index) { - NyLPC_cPtrTbl_set(i_inst,i_index,NULL); - return; + NyLPC_cPtrTbl_set(i_inst,i_index,NULL); + return; } NyLPC_TInt16 NyLPC_cPtrTbl_getIndex(NyLPC_TcPtrTbl_t* i_inst,void* i_val) { - int i; - void** p=i_inst->buf; + int i; + void** p=i_inst->buf; - for(i=i_inst->size-1;i>=0;i--){ - if(*(p+i)==i_val){ - return i; - } - } - return -1; + for(i=i_inst->size-1;i>=0;i--){ + if(*(p+i)==i_val){ + return i; + } + } + return -1; } /** @@ -98,10 +98,10 @@ NyLPC_TInt16 NyLPC_cPtrTbl_getIndex(NyLPC_TcPtrTbl_t* i_inst,void* i_val) */ NyLPC_TInt16 NyLPC_cPtrTbl_getLength(NyLPC_TcPtrTbl_t* i_inst) { - return i_inst->len; + return i_inst->len; } NyLPC_TBool NyLPC_cPtrTbl_hasEmpty(NyLPC_TcPtrTbl_t* i_inst) { - return i_inst->lensize; + return i_inst->lensize; } diff --git a/lib/src/NyLPC_cPtrTbl.h b/lib/src/NyLPC_cPtrTbl.h index 0346cd8..edc07fc 100644 --- a/lib/src/NyLPC_cPtrTbl.h +++ b/lib/src/NyLPC_cPtrTbl.h @@ -19,8 +19,8 @@ * along with this program. If not, see . * * For further information please contact. - * http://nyatla.jp/ - * or + * http://nyatla.jp/ + * or * *********************************************************************************/ #ifndef NYLPC_PTRTABLE_H_ @@ -49,12 +49,12 @@ typedef struct NyLPC_TcPtrTbl NyLPC_TcPtrTbl_t; struct NyLPC_TcPtrTbl { - /** 配列の最大サイズ*/ - NyLPC_TUInt16 size; - /** 配列の現在の長さ*/ - NyLPC_TUInt16 len; - /** 配列*/ - void** buf; + /** 配列の最大サイズ*/ + NyLPC_TUInt16 size; + /** 配列の現在の長さ*/ + NyLPC_TUInt16 len; + /** 配列*/ + void** buf; }; @@ -85,17 +85,17 @@ NyLPC_TInt16 NyLPC_cPtrTbl_add(NyLPC_TcPtrTbl_t* i_inst,void* i_val); void NyLPC_cPtrTbl_remove(NyLPC_TcPtrTbl_t* i_inst,NyLPC_TUInt16 i_index); /** - * 現在の長さを返します。 + * 現在の長さを返します。 */ NyLPC_TInt16 NyLPC_cPtrTbl_getLength(NyLPC_TcPtrTbl_t* i_inst); /** - * 空き領域の有無を返します。 + * 空き領域の有無を返します。 */ NyLPC_TBool NyLPC_cPtrTbl_hasEmpty(NyLPC_TcPtrTbl_t* i_inst); /** - * ポインタに一致するインデクスを返します。 + * ポインタに一致するインデクスを返します。 */ NyLPC_TInt16 NyLPC_cPtrTbl_getIndex(NyLPC_TcPtrTbl_t* i_inst,void* i_val); diff --git a/lib/src/NyLPC_cRingBuffer.c b/lib/src/NyLPC_cRingBuffer.c index ef4a58e..645e3d5 100644 --- a/lib/src/NyLPC_cRingBuffer.c +++ b/lib/src/NyLPC_cRingBuffer.c @@ -19,8 +19,8 @@ * along with this program. If not, see . * * For further information please contact. - * http://nyatla.jp/ - * or + * http://nyatla.jp/ + * or * *********************************************************************************/ #include "NyLPC_cRingBuffer.h" @@ -30,158 +30,158 @@ #elif NyLPC_ARCH==NyLPC_ARCH_WIN32 void NyLPC_cRingBuffer_dump(NyLPC_TcRingBuffer_t* i_inst) { - NyLPC_TUInt8* s=((NyLPC_TUInt8*)(i_inst+1));//バッファ開始位置 - int i; - if(i_inst->ro<=i_inst->wo){ - for(i=0;iro;i++){ - printf("-- "); - } - for(i=i_inst->ro;iwo;i++){ - printf("%02X ",*(s+i)); - } - for(i=i_inst->wo;ibl;i++){ - printf("-- "); - } - }else{ - for(i=0;iwo;i++){ - printf("%02X ",*(s+i)); - } - for(i=i_inst->wo;iro;i++){ - printf("-- "); - } - for(i=i_inst->ro;ibl;i++){ - printf("%02X ",*(s+i)); - } - } - printf("\n"); + NyLPC_TUInt8* s=((NyLPC_TUInt8*)(i_inst+1));//バッファ開始位置 + int i; + if(i_inst->ro<=i_inst->wo){ + for(i=0;iro;i++){ + printf("-- "); + } + for(i=i_inst->ro;iwo;i++){ + printf("%02X ",*(s+i)); + } + for(i=i_inst->wo;ibl;i++){ + printf("-- "); + } + }else{ + for(i=0;iwo;i++){ + printf("%02X ",*(s+i)); + } + for(i=i_inst->wo;iro;i++){ + printf("-- "); + } + for(i=i_inst->ro;ibl;i++){ + printf("%02X ",*(s+i)); + } + } + printf("\n"); } #else #endif void NyLPC_cRingBuffer_reset(NyLPC_TcRingBuffer_t* i_inst) { - i_inst->ro=i_inst->wo=0; + i_inst->ro=i_inst->wo=0; } NyLPC_TInt16 NyLPC_cRingBuffer_getReadableSize(NyLPC_TcRingBuffer_t* i_inst) { - volatile NyLPC_TUInt16 wo=(i_inst)->wo; - volatile NyLPC_TUInt16 ro=(i_inst)->ro; - if(wo>=ro) - { - return wo-ro; - }else{ - return (i_inst)->bl-ro; - } + volatile NyLPC_TUInt16 wo=(i_inst)->wo; + volatile NyLPC_TUInt16 ro=(i_inst)->ro; + if(wo>=ro) + { + return wo-ro; + }else{ + return (i_inst)->bl-ro; + } } NyLPC_TInt16 NyLPC_cRingBuffer_getWritableSize(const NyLPC_TcRingBuffer_t* i_inst) { - volatile NyLPC_TUInt16 wo=(i_inst)->wo; - volatile NyLPC_TUInt16 ro=(i_inst)->ro; - if(wo>=ro){ - //書込み可能サイズの計算 - return i_inst->bl-(wo-ro)-1; - }else{ - return ro-wo-1; - } + volatile NyLPC_TUInt16 wo=(i_inst)->wo; + volatile NyLPC_TUInt16 ro=(i_inst)->ro; + if(wo>=ro){ + //書込み可能サイズの計算 + return i_inst->bl-(wo-ro)-1; + }else{ + return ro-wo-1; + } } void NyLPC_cRingBuffer_initialize(NyLPC_TcRingBuffer_t* i_inst,void* i_buf,NyLPC_TUInt16 sizeof_buf) { - //バッファの開始位置と終了位置の計算 - i_inst->bl=sizeof_buf; - i_inst->ro=0; - i_inst->wo=0; - i_inst->buf=i_buf; + //バッファの開始位置と終了位置の計算 + i_inst->bl=sizeof_buf; + i_inst->ro=0; + i_inst->wo=0; + i_inst->buf=i_buf; } int NyLPC_cRingBuffer_write(NyLPC_TcRingBuffer_t* i_inst,NyLPC_TUInt8* i_data,const int i_len) { - NyLPC_TUInt8* s=(NyLPC_TUInt8*)i_inst->buf; - NyLPC_TUInt8* p; - NyLPC_TUInt16 wo=i_inst->wo; - NyLPC_TUInt16 ro=i_inst->ro; - NyLPC_TUInt16 wsize;//書込み可能サイズ - int l,i; - NyLPC_TUInt16 rw; + NyLPC_TUInt8* s=(NyLPC_TUInt8*)i_inst->buf; + NyLPC_TUInt8* p; + NyLPC_TUInt16 wo=i_inst->wo; + NyLPC_TUInt16 ro=i_inst->ro; + NyLPC_TUInt16 wsize;//書込み可能サイズ + int l,i; + NyLPC_TUInt16 rw; - if(wo>=ro){ - //書込み可能サイズの計算 - wsize=i_inst->bl-(wo-ro)-1; - if(wsize<1){ - return 0; - } - //書込みサイズの調整 - if(wsize>i_len){ - wsize=i_len; - } - //右側の書込みサイズの計算 - rw=i_inst->bl-wo; - l=(wsize=0;i--){ - *(p++)=*(i_data++); - } - //書込み位置の調整 - wo=(wo+l)%i_inst->bl; - l=wsize-l;//lに左側の書込みサイズストア - }else{ - wsize=ro-wo-1; - if(wsize>i_len){ - wsize=i_len; - } - l=wsize; - } - if(l>0){ - //左側の書込み - p=(s+wo); - for(i=l-1;i>=0;i--){ - *(p++)=*(i_data++); - } - wo+=l; - } - i_inst->wo=wo; - return wsize; + if(wo>=ro){ + //書込み可能サイズの計算 + wsize=i_inst->bl-(wo-ro)-1; + if(wsize<1){ + return 0; + } + //書込みサイズの調整 + if(wsize>i_len){ + wsize=i_len; + } + //右側の書込みサイズの計算 + rw=i_inst->bl-wo; + l=(wsize=0;i--){ + *(p++)=*(i_data++); + } + //書込み位置の調整 + wo=(wo+l)%i_inst->bl; + l=wsize-l;//lに左側の書込みサイズストア + }else{ + wsize=ro-wo-1; + if(wsize>i_len){ + wsize=i_len; + } + l=wsize; + } + if(l>0){ + //左側の書込み + p=(s+wo); + for(i=l-1;i>=0;i--){ + *(p++)=*(i_data++); + } + wo+=l; + } + i_inst->wo=wo; + return wsize; } //読出しポインタを得る。 NyLPC_TUInt8* NyLPC_cRingBuffer_pread(NyLPC_TcRingBuffer_t* i_inst,NyLPC_TUInt16 *len) { - NyLPC_TUInt16 ro=i_inst->ro; - *len=NyLPC_cRingBuffer_getReadableSize(i_inst); - return ((NyLPC_TUInt8*)(i_inst->buf))+ro; + NyLPC_TUInt16 ro=i_inst->ro; + *len=NyLPC_cRingBuffer_getReadableSize(i_inst); + return ((NyLPC_TUInt8*)(i_inst->buf))+ro; } //前方シークする。 void NyLPC_cRingBuffer_preadSeek(NyLPC_TcRingBuffer_t* i_inst,NyLPC_TUInt16 i_seek) { - NyLPC_Assert(NyLPC_cRingBuffer_getReadableSize(i_inst)>=i_seek); - i_inst->ro=(i_inst->ro+i_seek)%i_inst->bl; + NyLPC_Assert(NyLPC_cRingBuffer_getReadableSize(i_inst)>=i_seek); + i_inst->ro=(i_inst->ro+i_seek)%i_inst->bl; } #define TEST #ifndef TEST void main(void) { - NyLPC_TUInt16 l; - NyLPC_TUInt8* b; - int c; - char buf[sizeof(NyLPC_TcRingBuffer_t)+5]; - NyLPC_TcRingBuffer_t* s; - s=NyLPC_cRingBuffer_initialize(buf,sizeof(buf)); - for(;;){ - b=NyLPC_cRingBuffer_getReadPtr(s,&l); - printf("readable:%d\n",l); - c=NyLPC_cRingBuffer_write(s,"0123456789",3); - NyLPC_cRingBuffer_dump(s); - b=NyLPC_cRingBuffer_getReadPtr(s,&l); - printf("readable:%d\n",l); - NyLPC_cRingBuffer_seekReadPtr(s,(l>1)?l-1:1); - printf("read:%d\n",(l>1)?l-1:1); - NyLPC_cRingBuffer_dump(s); - } + NyLPC_TUInt16 l; + NyLPC_TUInt8* b; + int c; + char buf[sizeof(NyLPC_TcRingBuffer_t)+5]; + NyLPC_TcRingBuffer_t* s; + s=NyLPC_cRingBuffer_initialize(buf,sizeof(buf)); + for(;;){ + b=NyLPC_cRingBuffer_getReadPtr(s,&l); + printf("readable:%d\n",l); + c=NyLPC_cRingBuffer_write(s,"0123456789",3); + NyLPC_cRingBuffer_dump(s); + b=NyLPC_cRingBuffer_getReadPtr(s,&l); + printf("readable:%d\n",l); + NyLPC_cRingBuffer_seekReadPtr(s,(l>1)?l-1:1); + printf("read:%d\n",(l>1)?l-1:1); + NyLPC_cRingBuffer_dump(s); + } } #endif diff --git a/lib/src/NyLPC_cRingBuffer.h b/lib/src/NyLPC_cRingBuffer.h index 52e01a3..44d2cb8 100644 --- a/lib/src/NyLPC_cRingBuffer.h +++ b/lib/src/NyLPC_cRingBuffer.h @@ -19,8 +19,8 @@ * along with this program. If not, see . * * For further information please contact. - * http://nyatla.jp/ - * or + * http://nyatla.jp/ + * or * *********************************************************************************/ #ifndef NyLPC_TcRingBuffer_h @@ -44,10 +44,10 @@ typedef struct NyLPC_TcRingBuffer NyLPC_TcRingBuffer_t; struct NyLPC_TcRingBuffer { - void* buf; //バッファ - NyLPC_TUInt16 bl; //バッファ長さ - NyLPC_TUInt16 wo; //バッファ書込み位置オフセット - NyLPC_TUInt16 ro; //バッファ読み込み位置オフセット + void* buf; //バッファ + NyLPC_TUInt16 bl; //バッファ長さ + NyLPC_TUInt16 wo; //バッファ書込み位置オフセット + NyLPC_TUInt16 ro; //バッファ読み込み位置オフセット }; @@ -78,7 +78,7 @@ NyLPC_TInt16 NyLPC_cRingBuffer_getReadableSize(NyLPC_TcRingBuffer_t* i_inst); * この関数は、書き込み可能なサイズを計算して返します。 * @return * 書き込み可能なバイト数です。 - * この数値は、バッファ全体に対する空き領域と同じです。 + * この数値は、バッファ全体に対する空き領域と同じです。 */ NyLPC_TInt16 NyLPC_cRingBuffer_getWritableSize(const NyLPC_TcRingBuffer_t* i_inst); @@ -90,7 +90,7 @@ NyLPC_TInt16 NyLPC_cRingBuffer_getWritableSize(const NyLPC_TcRingBuffer_t* i_ins int NyLPC_cRingBuffer_write(NyLPC_TcRingBuffer_t* i_inst,NyLPC_TUInt8* i_data,const int i_len); /** - * この関数は、リングバッファを初期化します。 + * この関数は、リングバッファを初期化します。 */ void NyLPC_cRingBuffer_reset(NyLPC_TcRingBuffer_t* i_inst); diff --git a/lib/src/NyLPC_cRomPtrStream.c b/lib/src/NyLPC_cRomPtrStream.c index aa606cf..877b2ba 100644 --- a/lib/src/NyLPC_cRomPtrStream.c +++ b/lib/src/NyLPC_cRomPtrStream.c @@ -19,8 +19,8 @@ * along with this program. If not, see . * * For further information please contact. - * http://nyatla.jp/ - * or + * http://nyatla.jp/ + * or * *********************************************************************************/ #include "NyLPC_cPtrStream_protected.h" @@ -42,11 +42,11 @@ typedef struct NyLPC_TcRomPtrStream NyLPC_TcRomPtrStream_t; struct NyLPC_TcRomPtrStream { - NyLPC_TcPtrStream_t _super; - NyLPC_TChar* _rom; //ROMアドレス - NyLPC_TInt32 _rom_size; //ROMサイズ - NyLPC_TInt32 _ed; //読出し済みサイズ - NyLPC_TInt16 _packet_size; //一度当たりの読出しサイズ制限 + NyLPC_TcPtrStream_t _super; + NyLPC_TChar* _rom; //ROMアドレス + NyLPC_TInt32 _rom_size; //ROMサイズ + NyLPC_TInt32 _ed; //読出し済みサイズ + NyLPC_TInt16 _packet_size; //一度当たりの読出しサイズ制限 }; NyLPC_TBool NyLPC_TcRomPtrStream_initialize(NyLPC_TcRomPtrStream_t* i_inst,void* i_rom_addr,NyLPC_TInt32 i_length,NyLPC_TInt16 i_packetsize); @@ -60,58 +60,58 @@ static void m_preadSeek_func(NyLPC_TcPtrStream_t* i_inst,NyLPC_TUInt16 i_seek); //関数テーブル static struct NyLPC_TcPtrStream_TInterface NyLPC_TcRomPtrStream_Interface= { - m_pread, - NyLPC_cPtrStream_write_func, - m_preadSeek_func, - m_close + m_pread, + NyLPC_cPtrStream_write_func, + m_preadSeek_func, + m_close }; NyLPC_TBool NyLPC_TcRomPtrStream_initialize(NyLPC_TcRomPtrStream_t* i_inst,void* i_rom_addr,NyLPC_TInt32 i_length,NyLPC_TInt16 i_packetsize) { - NyLPC_ArgAssert(i_inst!=NULL); - NyLPC_ArgAssert(i_rom_addr!=NULL); - NyLPC_ArgAssert(i_length>0); - NyLPC_ArgAssert(i_packetsize>0); - i_inst->_super._interface=&NyLPC_TcRomPtrStream_Interface; - i_inst->_rom=(NyLPC_TChar*)i_rom_addr; - i_inst->_rom_size=i_length; - i_inst->_packet_size=i_packetsize; - return NyLPC_TBool_TRUE; + NyLPC_ArgAssert(i_inst!=NULL); + NyLPC_ArgAssert(i_rom_addr!=NULL); + NyLPC_ArgAssert(i_length>0); + NyLPC_ArgAssert(i_packetsize>0); + i_inst->_super._interface=&NyLPC_TcRomPtrStream_Interface; + i_inst->_rom=(NyLPC_TChar*)i_rom_addr; + i_inst->_rom_size=i_length; + i_inst->_packet_size=i_packetsize; + return NyLPC_TBool_TRUE; } static void m_preadSeek_func(NyLPC_TcPtrStream_t* i_inst,NyLPC_TUInt16 i_seek) { - (void)i_inst; - NyLPC_TcRomPtrStream_t* inst=(NyLPC_TcRomPtrStream_t*)i_inst; - inst->_rom+=i_seek; + (void)i_inst; + NyLPC_TcRomPtrStream_t* inst=(NyLPC_TcRomPtrStream_t*)i_inst; + inst->_rom+=i_seek; } /*private*/ static NyLPC_TBool m_pread(NyLPC_TcPtrStream_t* i_inst,const void** o_buf_ptr,NyLPC_TUInt32 i_wait_msec) { - NyLPC_TInt32 size,psize; - NyLPC_TcRomPtrStream_t* inst=(NyLPC_TcRomPtrStream_t*)i_inst; - //引数 - NyLPC_ArgAssert(i_inst!=NULL); - NyLPC_ArgAssert(o_buf_ptr!=NULL); - //クローズしてない? - NyLPC_Assert(inst->_rom!=NULL); - - size=inst->_rom_size-inst->_ed; //残り長さ計算 - psize=(size>inst->_packet_size)?inst->_packet_size:size; //パケットサイズ計算 - *o_buf_ptr=(inst->_rom+inst->_ed); //現在位置を返す - inst->_ed+=psize;//読出し位置更新 - return psize; + NyLPC_TInt32 size,psize; + NyLPC_TcRomPtrStream_t* inst=(NyLPC_TcRomPtrStream_t*)i_inst; + //引数 + NyLPC_ArgAssert(i_inst!=NULL); + NyLPC_ArgAssert(o_buf_ptr!=NULL); + //クローズしてない? + NyLPC_Assert(inst->_rom!=NULL); + + size=inst->_rom_size-inst->_ed; //残り長さ計算 + psize=(size>inst->_packet_size)?inst->_packet_size:size; //パケットサイズ計算 + *o_buf_ptr=(inst->_rom+inst->_ed); //現在位置を返す + inst->_ed+=psize;//読出し位置更新 + return psize; } static void m_close(NyLPC_TcPtrStream_t* i_inst) { - NyLPC_TcRomPtrStream_t* inst=(NyLPC_TcRomPtrStream_t*)i_inst; - inst->_rom=NULL; + NyLPC_TcRomPtrStream_t* inst=(NyLPC_TcRomPtrStream_t*)i_inst; + inst->_rom=NULL; } diff --git a/lib/src/NyLPC_cStr.c b/lib/src/NyLPC_cStr.c index 8d2c820..d7b8a44 100644 --- a/lib/src/NyLPC_cStr.c +++ b/lib/src/NyLPC_cStr.c @@ -19,8 +19,8 @@ * along with this program. If not, see . * * For further information please contact. - * http://nyatla.jp/ - * or + * http://nyatla.jp/ + * or * *********************************************************************************/ #include "NyLPC_cStr.h" @@ -28,29 +28,29 @@ void NyLPC_cStr_initialize(NyLPC_TcStr_t* i_inst,void* i_buf,int sizeof_buf) { - i_inst->s=(NyLPC_TInt16)(sizeof_buf-1); - i_inst->l=0; - i_inst->buf=i_buf; - *((NyLPC_TChar*)(i_inst->buf))=0; + i_inst->s=(NyLPC_TInt16)(sizeof_buf-1); + i_inst->l=0; + i_inst->buf=i_buf; + *((NyLPC_TChar*)(i_inst->buf))=0; } NyLPC_TBool NyLPC_cStr_put(NyLPC_TcStr_t* i_inst,NyLPC_TChar i_c) { - NyLPC_TChar* p=(NyLPC_TChar*)(i_inst->buf)+i_inst->l; - if(i_inst->s-i_inst->l>0){ - *p=i_c; - i_inst->l++; - *(++p)=0; - return NyLPC_TBool_TRUE; - } - return NyLPC_TBool_FALSE; + NyLPC_TChar* p=(NyLPC_TChar*)(i_inst->buf)+i_inst->l; + if(i_inst->s-i_inst->l>0){ + *p=i_c; + i_inst->l++; + *(++p)=0; + return NyLPC_TBool_TRUE; + } + return NyLPC_TBool_FALSE; } void NyLPC_cStr_toUpper(NyLPC_TcStr_t* i_inst) { - NyLPC_TChar* p; - for(p=NyLPC_cStr_str(i_inst);*p!=0;p++){ - *p=(NyLPC_TChar)toupper((int)*p); - } - return; + NyLPC_TChar* p; + for(p=NyLPC_cStr_str(i_inst);*p!=0;p++){ + *p=(NyLPC_TChar)toupper((int)*p); + } + return; } diff --git a/lib/src/NyLPC_cStr.h b/lib/src/NyLPC_cStr.h index cb7b34e..2b957c3 100644 --- a/lib/src/NyLPC_cStr.h +++ b/lib/src/NyLPC_cStr.h @@ -19,8 +19,8 @@ * along with this program. If not, see . * * For further information please contact. - * http://nyatla.jp/ - * or + * http://nyatla.jp/ + * or * *********************************************************************************/ #ifndef NYLPC_CSTR_H_ @@ -43,9 +43,9 @@ typedef struct NyLPC_TcStr NyLPC_TcStr_t; struct NyLPC_TcStr { - NyLPC_TInt16 s;//バッファサイズ - NyLPC_TInt16 l;//現在の文字列長 - void* buf; + NyLPC_TInt16 s;//バッファサイズ + NyLPC_TInt16 l;//現在の文字列長 + void* buf; }; /** diff --git a/lib/src/NyLPC_stdlib.c b/lib/src/NyLPC_stdlib.c index 34ea356..dad742f 100644 --- a/lib/src/NyLPC_stdlib.c +++ b/lib/src/NyLPC_stdlib.c @@ -19,23 +19,23 @@ * along with this program. If not, see . * * For further information please contact. - * http://nyatla.jp/ - * or + * http://nyatla.jp/ + * or * *********************************************************************************/ #include "NyLPC_stdlib.h" NyLPC_TUInt32 NyLPC_TUInt32_bswap(NyLPC_TUInt32 n) { - return( - ((((NyLPC_TUInt32)(n))<<24)&0xff000000)| - ((((NyLPC_TUInt32)(n))<< 8)&0x00ff0000)| - ((((NyLPC_TUInt32)(n))>> 8)&0x0000ff00)| - ((((NyLPC_TUInt32)(n))>>24)&0x000000ff)); + return( + ((((NyLPC_TUInt32)(n))<<24)&0xff000000)| + ((((NyLPC_TUInt32)(n))<< 8)&0x00ff0000)| + ((((NyLPC_TUInt32)(n))>> 8)&0x0000ff00)| + ((((NyLPC_TUInt32)(n))>>24)&0x000000ff)); } NyLPC_TUInt16 NyLPC_TUInt16_bswap(NyLPC_TUInt16 n) { - return NyLPC_TUInt16_BSWAP(n); + return NyLPC_TUInt16_BSWAP(n); } static int _line_log_l; @@ -46,48 +46,48 @@ unsigned int NyLPC_debug_counter=0; void NyLPC_assertHook(const char* m,int l) { - _line_log_l=l; - _line_log_m=m; - NyLPC_assert_counter++; - return; + _line_log_l=l; + _line_log_m=m; + NyLPC_assert_counter++; + return; } void NyLPC_abortHook(const char* m,int l) { - _line_log_l=l; - _line_log_m=m; - NyLPC_abort_counter++; + _line_log_l=l; + _line_log_m=m; + NyLPC_abort_counter++; } void NyLPC_debugHook(const char* m,int l) { - _line_log_l=l; - _line_log_m=m; - NyLPC_debug_counter++; - return; + _line_log_l=l; + _line_log_m=m; + NyLPC_debug_counter++; + return; } NyLPC_TBool NyLPC_TCharArrayPtr_seek(struct NyLPC_TCharArrayPtr* i_struct,NyLPC_TUInt16 i_seek) { - if(i_struct->lenptr+=i_seek; - i_struct->len-=i_seek; - return NyLPC_TBool_TRUE; + if(i_struct->lenptr+=i_seek; + i_struct->len-=i_seek; + return NyLPC_TBool_TRUE; } NyLPC_TBool NyLPC_TUInt32ArrayPtr_seek(struct NyLPC_TUInt32ArrayPtr* i_struct,NyLPC_TUInt16 i_seek) { - if(i_struct->lenptr+=i_seek; - i_struct->len-=i_seek; - return NyLPC_TBool_TRUE; + if(i_struct->lenptr+=i_seek; + i_struct->len-=i_seek; + return NyLPC_TBool_TRUE; } void NyLPC_TUInt32ArrayPtr_setBuf(struct NyLPC_TUInt32ArrayPtr* i_struct,NyLPC_TUInt32* i_ptr,NyLPC_TUInt16 i_len) { - i_struct->ptr=i_ptr; - i_struct->len=i_len; + i_struct->ptr=i_ptr; + i_struct->len=i_len; } @@ -101,33 +101,33 @@ void NyLPC_TUInt32ArrayPtr_setBuf(struct NyLPC_TUInt32ArrayPtr* i_struct,NyLPC_T NyLPC_TUInt8 NyLPC_TTextIdTbl_getMatchId(const NyLPC_TChar* i_str,const struct NyLPC_TTextIdTbl i_tbl[]) { - int i; - for(i=0;i_tbl[i].n!=NULL;i++){ - if(strcmp(i_str,i_tbl[i].n)==0){ - break; - } - } - return i_tbl[i].id; + int i; + for(i=0;i_tbl[i].n!=NULL;i++){ + if(strcmp(i_str,i_tbl[i].n)==0){ + break; + } + } + return i_tbl[i].id; } NyLPC_TUInt8 NyLPC_TTextIdTbl_getMatchIdIgnoreCase(const NyLPC_TChar* i_str,const struct NyLPC_TTextIdTbl i_tbl[]) { - int i; - for(i=0;i_tbl[i].n!=NULL;i++){ - if(NyLPC_stricmp(i_str,i_tbl[i].n)==0){ - break; - } - } - return i_tbl[i].id; + int i; + for(i=0;i_tbl[i].n!=NULL;i++){ + if(NyLPC_stricmp(i_str,i_tbl[i].n)==0){ + break; + } + } + return i_tbl[i].id; } const NyLPC_TChar* NyLPC_TTextIdTbl_getTextById(NyLPC_TUInt8 i_id,const struct NyLPC_TTextIdTbl i_tbl[]) { - int i; - for(i=0;i_tbl[i].n!=NULL;i++){ - if(i_id==i_tbl[i].id){ - return i_tbl[i].n; - } - } - return NULL; + int i; + for(i=0;i_tbl[i].n!=NULL;i++){ + if(i_id==i_tbl[i].id){ + return i_tbl[i].n; + } + } + return NULL; } @@ -141,34 +141,34 @@ const NyLPC_TChar* NyLPC_TTextIdTbl_getTextById(NyLPC_TUInt8 i_id,const struct N NyLPC_TInt8 NyLPC_itoa(int i_n,char* o_out,NyLPC_TInt8 i_base) { - NyLPC_TInt8 i,v; - int sign; - if ((sign = i_n) < 0){ - i_n = -i_n; - } - i = 0; - do{ - v=(NyLPC_TInt8)(i_n % i_base); - o_out[i++] = (v<10)?(v+'0'):(v+'a'-10); - }while ((i_n /= i_base) > 0); - if (sign < 0){ - o_out[i++] = '-'; - } - o_out[i] = '\0'; - NyLPC_reverse(o_out); - return i; + NyLPC_TInt8 i,v; + int sign; + if ((sign = i_n) < 0){ + i_n = -i_n; + } + i = 0; + do{ + v=(NyLPC_TInt8)(i_n % i_base); + o_out[i++] = (v<10)?(v+'0'):(v+'a'-10); + }while ((i_n /= i_base) > 0); + if (sign < 0){ + o_out[i++] = '-'; + } + o_out[i] = '\0'; + NyLPC_reverse(o_out); + return i; } NyLPC_TInt8 NyLPC_uitoa(unsigned int i_n,char* o_out,NyLPC_TInt8 i_base) { - NyLPC_TInt8 i = 0; - NyLPC_TInt8 v; - do{ - v=(NyLPC_TInt8)(i_n % i_base); - o_out[i++] = (v<10)?(v+'0'):(v+'a'-10); - }while ((i_n /= i_base) > 0); - o_out[i] = '\0'; - NyLPC_reverse(o_out); - return i; + NyLPC_TInt8 i = 0; + NyLPC_TInt8 v; + do{ + v=(NyLPC_TInt8)(i_n % i_base); + o_out[i++] = (v<10)?(v+'0'):(v+'a'-10); + }while ((i_n /= i_base) > 0); + o_out[i] = '\0'; + NyLPC_reverse(o_out); + return i; } /** @@ -176,76 +176,76 @@ NyLPC_TInt8 NyLPC_uitoa(unsigned int i_n,char* o_out,NyLPC_TInt8 i_base) */ NyLPC_TInt8 NyLPC_uitoa2(unsigned int i_n,char* o_out,NyLPC_TInt8 i_base,NyLPC_TInt8 i_digit) { - NyLPC_TInt8 i = 0; - NyLPC_TInt8 v; - do{ - v=(NyLPC_TInt8)(i_n % i_base); - o_out[i++] = (v<10)?(v+'0'):(v+'a'-10); - }while ((i_n /= i_base) > 0); - while(i 0); + while(i0;n2--,i_s1++,i_s2++) - { - if(*i_s1!=*i_s2){ - c=(int)((unsigned char)NyLPC_tolower(*i_s1)) - (int)((unsigned char)NyLPC_tolower(*i_s2)); - if(c!=0){ - return c; - } - } - } - return 0; + char c; + int n2=n; + for(;n2>0;n2--,i_s1++,i_s2++) + { + if(*i_s1!=*i_s2){ + c=(int)((unsigned char)NyLPC_tolower(*i_s1)) - (int)((unsigned char)NyLPC_tolower(*i_s2)); + if(c!=0){ + return c; + } + } + } + return 0; } int NyLPC_ctoi(char i) { - if('0'<=i && i<='9') return (i-'0'); - return 0; + if('0'<=i && i<='9') return (i-'0'); + return 0; } int NyLPC_ctox(char i) { - if('0'<=i && i<='9') return (i-'0'); - if('a'<=i && i<='f') return (i-'a'+10); - if('A'<=i && i<='F') return (i-'A'+10); - return 0; + if('0'<=i && i<='9') return (i-'0'); + if('a'<=i && i<='f') return (i-'a'+10); + if('A'<=i && i<='F') return (i-'A'+10); + return 0; } diff --git a/lib/src/driver/ethernet/EthDev.h b/lib/src/driver/ethernet/EthDev.h index 169713a..41cd29e 100644 --- a/lib/src/driver/ethernet/EthDev.h +++ b/lib/src/driver/ethernet/EthDev.h @@ -40,3 +40,4 @@ const struct TiEthernetDevice* getEthernetDevicePnP(void); #endif /* __cplusplus */ #endif + diff --git a/lib/src/driver/ethernet/NyLPC_IEthernetDevice.h b/lib/src/driver/ethernet/NyLPC_IEthernetDevice.h index b965a4c..09577ca 100644 --- a/lib/src/driver/ethernet/NyLPC_IEthernetDevice.h +++ b/lib/src/driver/ethernet/NyLPC_IEthernetDevice.h @@ -1,18 +1,21 @@ /* * NyLPC_IEthernetDevice.h * - * Created on: 2011/12/06 + * Created: 2011/12/06 * MiMicのイーサネットドライバインタフェイスを定義する。 */ #ifndef NyLPC_IEthernetDevice_h #define NyLPC_IEthernetDevice_h #include "NyLPC_stdlib.h" -#include "NyLPC_uipService.h" +#include "../../netif/NyLPC_NetIf_ip_types.h" + #ifdef __cplusplus extern "C" { #endif /* __cplusplus */ + + typedef struct NyLPC_TiEthernetDevice NyLPC_TiEthernetDevice_t; /**非同期イベントのメッセージタイプ*/ @@ -29,24 +32,6 @@ typedef unsigned int NyLPC_TiEthernetDevice_EVENT; typedef void (*NyLPC_TiEthernetDevice_onEvent)(void* i_param,NyLPC_TiEthernetDevice_EVENT i_type); -/** - * 送信バッフメモリのヘッダ。 - * この構造体は、TXバッファメモリブロックのヘッダーです。 - * TXバッファメモリブロックは、この構造体の後ろに、sizeに一致したメモリを連結したもので表現します。 - *
- * buffer=[struct NyLPC_TTxBufferHeader][n]
- * 
- */ -struct NyLPC_TTxBufferHeader -{ - //メモリブロックの参照カウンタ。 - NyLPC_TInt8 ref; - //送信用にロックしたかを示すフラグ - NyLPC_TUInt8 is_lock; - //32ビット境界に合わせるためのパディング。 - NyLPC_TUInt16 padding; -}; - /** @@ -82,7 +67,7 @@ typedef void (*NyLPC_TiEthernetDevice_nextRxEthFrame)(void); * 割り当てたメモリブロックのヘッダ。 */ #define NyLPC_iEthernetDevice_allocTxBuf(i,h,s) (i)->allocTxBuf((h),(s)) -typedef struct NyLPC_TTxBufferHeader* (*NyLPC_TiEthernetDevice_allocTxBuf)(NyLPC_TUInt16 i_hint,NyLPC_TUInt16* o_size); +typedef void* (*NyLPC_TiEthernetDevice_allocTxBuf)(NyLPC_TUInt16 i_hint,NyLPC_TUInt16* o_size); /** @@ -90,7 +75,7 @@ typedef struct NyLPC_TTxBufferHeader* (*NyLPC_TiEthernetDevice_allocTxBuf)(NyLPC * 関数は、メモリブロックの参照カウンタを1減算する。 */ #define NyLPC_iEthernetDevice_releaseTxBuf(i,b) (i)->releaseTxBuf(b) -typedef void (*NyLPC_TiEthernetDevice_releaseTxBuf)(struct NyLPC_TTxBufferHeader* i_buf); +typedef void (*NyLPC_TiEthernetDevice_releaseTxBuf)(void* i_buf); @@ -99,12 +84,11 @@ typedef void (*NyLPC_TiEthernetDevice_releaseTxBuf)(struct NyLPC_TTxBufferHeader * @param i_buf * allocTxBufで得たメモリか、初期化したNyLPC_TTxBufferHeaderメモリブロックを指定する。 * 送信が終わるまでの間、メモリを開放してはならない。 - * #外部で確保したメモリについては、利用不能なケースがあるかもしれない。現在のMiMicでは、使用できることを前提としている。 * @oaram i_size * i_bufの後ろに連結されているデータメモリの長さ */ #define NyLPC_iEthernetDevice_sendTxEthFrame(i,b,s) (i)->sendTxEthFrame((b),(s)) -typedef void (*NyLPC_TiEthernetDevice_sendTxEthFrame)(struct NyLPC_TTxBufferHeader* i_buf,unsigned short i_size); +typedef void (*NyLPC_TiEthernetDevice_sendTxEthFrame)(void* i_buf,unsigned short i_size); /** diff --git a/lib/src/driver/ethernet/NyLPC_cEthernetMM.c b/lib/src/driver/ethernet/NyLPC_cEthernetMM.c index d9092b8..b950bdd 100644 --- a/lib/src/driver/ethernet/NyLPC_cEthernetMM.c +++ b/lib/src/driver/ethernet/NyLPC_cEthernetMM.c @@ -1,51 +1,9 @@ #include "NyLPC_cEthernetMM.h" -/** - * メモリブロックの数 - */ -#define NUM_OF_MAX_BUF 3 -#define NUM_OF_512_BUF 3 -#define NUM_OF_256_BUF 4 -#define NUM_OF_128_BUF 16 -#define NUM_OF_64_BUF 4 - -/** - * FULLサイズのEthernetFrame送信メモリのサイズ。 - * ここで最大送信サイズを制限する。 - * 通常は1460+20+20+14=1514バイト - */ -#define MAX_TX_ETHERNET_FRAME_SIZE 1514 /** - * TXメモリブロックの配置 - * 9518バイト - */ -struct TTxMemoryBlock -{ - struct{ - struct NyLPC_TTxBufferHeader h; - NyLPC_TUInt8 b[MAX_TX_ETHERNET_FRAME_SIZE]; - }buf_max[NUM_OF_MAX_BUF];//(4+MAX_TX_ETHERNET_FRAME_SIZE(1514))*3=? default=4554 - struct{ - struct NyLPC_TTxBufferHeader h; - NyLPC_TUInt8 b[512]; - }buf_512[NUM_OF_512_BUF];//(4+512)*3=1548 - struct{ - struct NyLPC_TTxBufferHeader h; - NyLPC_TUInt8 b[256]; - }buf_256[NUM_OF_256_BUF];//(4+256)*4=1560 - struct{ - struct NyLPC_TTxBufferHeader h; - NyLPC_TUInt8 b[128]; - }buf_128[NUM_OF_128_BUF];//(4+128)*16=1584 - struct{ - struct NyLPC_TTxBufferHeader h; - NyLPC_TUInt8 b[64]; - }buf_64[NUM_OF_64_BUF];//(4+64)*4=272 -}; -/** * メモリブロックの配置 */ -static struct TTxMemoryBlock* _mem_addr; +static struct NyLPC_TcEthernetMM_TxMemoryBlock* _mem_addr; @@ -57,31 +15,31 @@ int NyLPC_cEthernetMM_dbg_getNumofUsedTx(void) int x; NyLPC_TUInt8 r1,r2,r3,r4,r5; r1=r2=r3=r4=r5=0; - for(x=0;xbuf_max[x].h.is_lock || _mem_addr->buf_max[x].h.ref>0){ r1++; continue; } } - for(x=0;xbuf_512[x].h.is_lock || _mem_addr->buf_512[x].h.ref>0){ r2++; continue; } } - for(x=0;xbuf_256[x].h.is_lock || _mem_addr->buf_256[x].h.ref>0){ r3++; continue; } } - for(x=0;xbuf_128[x].h.is_lock || _mem_addr->buf_128[x].h.ref>0){ r4++; continue; } } - for(x=0;xbuf_64[x].h.is_lock || _mem_addr->buf_64[x].h.ref>0){ r5++; continue; @@ -96,23 +54,23 @@ void NyLPC_cEthernetMM_initialize(void* i_memblock_addr) int x; _mem_addr=i_memblock_addr; //TXバッファを初期化 - for(x=0;xbuf_max[x].h.is_lock=NyLPC_TUInt8_FALSE; _mem_addr->buf_max[x].h.ref=0; } - for(x=0;xbuf_512[x].h.is_lock=NyLPC_TUInt8_FALSE; _mem_addr->buf_512[x].h.ref=0; } - for(x=0;xbuf_256[x].h.is_lock=NyLPC_TUInt8_FALSE; _mem_addr->buf_256[x].h.ref=0; } - for(x=0;xbuf_128[x].h.is_lock=NyLPC_TUInt8_FALSE; _mem_addr->buf_128[x].h.ref=0; } - for(x=0;xbuf_64[x].h.is_lock=NyLPC_TUInt8_FALSE; _mem_addr->buf_64[x].h.ref=0; } @@ -121,21 +79,21 @@ void NyLPC_cEthernetMM_initialize(void* i_memblock_addr) /** * 空のTxバッファのポインタを返します。 */ -struct NyLPC_TTxBufferHeader* NyLPC_cEthernetMM_alloc(NyLPC_TUInt16 i_hint,NyLPC_TUInt16* o_size) +void* NyLPC_cEthernetMM_alloc(NyLPC_TUInt16 i_hint,NyLPC_TUInt16* o_size) { int i; //ヒントから、割り当てるメモリブロックを決定 //特殊ブロック if(i_hint==NyLPC_TcEthernetMM_HINT_CTRL_PACKET){ - for(i=0;ibuf_64[i].h.ref>0 || _mem_addr->buf_64[i].h.is_lock){ continue; } _mem_addr->buf_64[i].h.ref++; *o_size=64; - return &(_mem_addr->buf_64[i].h); + return _mem_addr->buf_64[i].b; } return NULL; } @@ -152,54 +110,55 @@ struct NyLPC_TTxBufferHeader* NyLPC_cEthernetMM_alloc(NyLPC_TUInt16 i_hint,NyLPC } ALLOC_MAX: - for(i=0;ibuf_max[i].h.ref>0 || _mem_addr->buf_max[i].h.is_lock){ continue; } _mem_addr->buf_max[i].h.ref++; - *o_size=MAX_TX_ETHERNET_FRAME_SIZE; - return &(_mem_addr->buf_max[i].h); + *o_size=NyLPC_TcEthernetMM_MAX_TX_ETHERNET_FRAME_SIZE; + return _mem_addr->buf_max[i].b; } ALLOC_512: - for(i=0;ibuf_512[i].h.ref>0 || _mem_addr->buf_512[i].h.is_lock){ continue; } *o_size=512; _mem_addr->buf_512[i].h.ref++; - return &(_mem_addr->buf_512[i].h); + return _mem_addr->buf_512[i].b; } ALLOC_256: - for(i=0;ibuf_256[i].h.ref>0 || (_mem_addr->buf_256[i].h.is_lock)){ continue; } *o_size=256; _mem_addr->buf_256[i].h.ref++; - return &(_mem_addr->buf_256[i].h); + return _mem_addr->buf_256[i].b; } ALLOC_128: - for(i=0;ibuf_128[i].h.ref>0 || (_mem_addr->buf_128[i].h.is_lock)){ continue; } *o_size=128; _mem_addr->buf_128[i].h.ref++; - return &(_mem_addr->buf_128[i].h); + return _mem_addr->buf_128[i].b; } return NULL; } -void NyLPC_cEthernetMM_release(struct NyLPC_TTxBufferHeader* i_buf) +void NyLPC_cEthernetMM_release(void* i_buf) { + struct NyLPC_TTxBufferHeader* h=NyLPC_TTxBufferHeader_getBufferHeaderAddr(i_buf); //参照カウンタを1減算 - NyLPC_Assert(i_buf->ref>0); - i_buf->ref--; + NyLPC_Assert(h->ref>0); + h->ref--; return; } diff --git a/lib/src/driver/ethernet/NyLPC_cEthernetMM.h b/lib/src/driver/ethernet/NyLPC_cEthernetMM.h index dd7905a..8e5644c 100644 --- a/lib/src/driver/ethernet/NyLPC_cEthernetMM.h +++ b/lib/src/driver/ethernet/NyLPC_cEthernetMM.h @@ -23,7 +23,10 @@ * or * *********************************************************************************/ - +/** + * @file + * このファイルは、イーサネットメモリマネージャクラスを定義します。 + */ #ifndef NyLPC_cEthernetMM_protected_h #define NyLPC_cEthernetMM_protected_h @@ -34,15 +37,106 @@ extern "C" { #endif /* __cplusplus */ +#ifndef PACK_STRUCT_END + #define PACK_STRUCT_END __attribute((packed)) +#endif + +/** + * 構造体のアライメントサイズ(4 or 16) + */ +#ifndef NyLPC_TTxBufferHeader_ALIGNMENT +# define NyLPC_TTxBufferHeader_ALIGNMENT 4 +#endif + +/** + *バッファメモリのパディングサイズ + */ +#ifndef NyLPC_TcEthernetMM_BUF_PADDING +# define NyLPC_TcEthernetMM_BUF_PADDING 0 +#endif + +/** + * 送信バッフメモリのヘッダ。 + * この構造体は、TXバッファメモリブロックのヘッダーです。 + * TXバッファメモリブロックは、この構造体の後ろに、sizeに一致したメモリを連結したもので表現します。 + *
+ * buffer=[struct NyLPC_TTxBufferHeader][n]
+ * 
+ */ +struct NyLPC_TTxBufferHeader +{ + //メモリブロックの参照カウンタ。 + NyLPC_TInt8 ref; + //送信用にロックしたかを示すフラグ + NyLPC_TUInt8 is_lock; + //Nビット境界に合わせるためのパディング。 + NyLPC_TUInt8 padding[NyLPC_TTxBufferHeader_ALIGNMENT-2]; +}PACK_STRUCT_END; + +/** + * バッファメモリアドレスからメモリヘッダアドレスを復元します。 + */ +#define NyLPC_TTxBufferHeader_getBufferHeaderAddr(a) ((struct NyLPC_TTxBufferHeader*)(((NyLPC_TUInt8*)a)-sizeof(struct NyLPC_TTxBufferHeader))) + + /** * NyLPC_cEthernetMM_allocのヒント値。 * コントロールパケット用のサイズ要求をするときに使用します。 */ #define NyLPC_TcEthernetMM_HINT_CTRL_PACKET 0 + + /** - * @file - * このファイルは、イーサネットメモリマネージャクラスを定義します。 + * メモリブロック構造体の定数値 + */ +#define NyLPC_TcEthernetMM_NUM_OF_MAX_BUF 3 +#define NyLPC_TcEthernetMM_NUM_OF_512_BUF 3 +#define NyLPC_TcEthernetMM_NUM_OF_256_BUF 4 +#define NyLPC_TcEthernetMM_NUM_OF_128_BUF 16 +#define NyLPC_TcEthernetMM_NUM_OF_64_BUF 4 + +/** + * FULLサイズのEthernetFrame送信メモリのサイズ。 + * ここで最大送信サイズを制限します。 + * 通常は1460+20+20+14=1514バイト + * パディングと合計で128bit(16byte)アライメントにしておかないと不幸になる。 + */ +#define NyLPC_TcEthernetMM_MAX_TX_ETHERNET_FRAME_SIZE 1514 +/** + * Alignment padding(128bit) */ +#define NyLPC_TcEthernetMM_MAX_TX_ETHERNET_PADDING 22 + + +/** + * TXメモリブロックの定義配列 + */ +struct NyLPC_TcEthernetMM_TxMemoryBlock +{ + struct{ + struct NyLPC_TTxBufferHeader h; + NyLPC_TUInt8 b[NyLPC_TcEthernetMM_MAX_TX_ETHERNET_FRAME_SIZE]; + NyLPC_TUInt8 _padding[NyLPC_TcEthernetMM_MAX_TX_ETHERNET_PADDING+NyLPC_TcEthernetMM_BUF_PADDING]; + }buf_max[NyLPC_TcEthernetMM_NUM_OF_MAX_BUF];//(4+MAX_TX_ETHERNET_FRAME_SIZE(1514))*3=? default=4554 + struct{ + struct NyLPC_TTxBufferHeader h; + NyLPC_TUInt8 b[512+NyLPC_TcEthernetMM_BUF_PADDING]; + }buf_512[NyLPC_TcEthernetMM_NUM_OF_512_BUF];//(4+512)*3=1548 + struct{ + struct NyLPC_TTxBufferHeader h; + NyLPC_TUInt8 b[256+NyLPC_TcEthernetMM_BUF_PADDING]; + }buf_256[NyLPC_TcEthernetMM_NUM_OF_256_BUF];//(4+256)*4=1560 + struct{ + struct NyLPC_TTxBufferHeader h; + NyLPC_TUInt8 b[128+NyLPC_TcEthernetMM_BUF_PADDING]; + }buf_128[NyLPC_TcEthernetMM_NUM_OF_128_BUF];//(4+128)*16=1584 + struct{ + struct NyLPC_TTxBufferHeader h; + NyLPC_TUInt8 b[64+NyLPC_TcEthernetMM_BUF_PADDING]; + }buf_64[NyLPC_TcEthernetMM_NUM_OF_64_BUF];//(4+64)*4=272 +}PACK_STRUCT_END; + + int NyLPC_cEthernetMM_dbg_getNumofUsedTx(void); @@ -64,13 +158,13 @@ void NyLPC_cEthernetMM_initialize(void* i_memblock_addr); * @return * 割り当て不能な場合はNULLが帰ります。 * @bug - * 戻り値、メモリブロックヘッダ不要では? + * パケットバッファの先頭アドレス */ -struct NyLPC_TTxBufferHeader* NyLPC_cEthernetMM_alloc(NyLPC_TUInt16 i_hint,NyLPC_TUInt16* o_size); +void* NyLPC_cEthernetMM_alloc(NyLPC_TUInt16 i_hint,NyLPC_TUInt16* o_size); /** * NyLPC_EthernetMM_allocで得たメモリを解放します。 */ -void NyLPC_cEthernetMM_release(struct NyLPC_TTxBufferHeader* i_buf); +void NyLPC_cEthernetMM_release(void* i_buf); #ifdef __cplusplus } diff --git a/lib/src/driver/ethernet/k64f/EthDev.c b/lib/src/driver/ethernet/k64f/EthDev.c new file mode 100644 index 0000000..562714d --- /dev/null +++ b/lib/src/driver/ethernet/k64f/EthDev.c @@ -0,0 +1,21 @@ +#include "NyLPC_config.h" +#if NyLPC_MCU==NyLPC_MCU_K64F + +#include "../EthDev.h" +#include "EtherDev_K64F_protected.h" + + + +const struct TiEthernetDevice* getEthernetDevicePnP(void) +{ + const struct TiEthernetDevice* ret; + if(EthDev_K64F_getInterface(&ret)){ + return ret; + } + return NULL; +} + + +#endif + + diff --git a/lib/src/driver/ethernet/k64f/EthDev_K64F.c b/lib/src/driver/ethernet/k64f/EthDev_K64F.c new file mode 100644 index 0000000..dee8a59 --- /dev/null +++ b/lib/src/driver/ethernet/k64f/EthDev_K64F.c @@ -0,0 +1,534 @@ +#include "NyLPC_config.h" +#if NyLPC_MCU==NyLPC_MCU_K64F +#include "NyLPC_stdlib.h" +#include "NyLPC_os.h" +#include "copy_of_ethernet_api.h" +#include "NyLPC_IEthernetDevice.h" +#include "NyLPC_cEthernetMM.h" +////////// +#include "fsl_enet_driver.h" +#include "fsl_enet_hal.h" +#include "fsl_device_registers.h" +#include "fsl_phy_driver.h" +#include "fsl_interrupt_manager.h" +#include "k64f_emac_config.h" +#include +#include + +/** + */ +static NyLPC_TBool start(const struct NyLPC_TEthAddr* i_eth_addr,NyLPC_TiEthernetDevice_onEvent i_handler,void* i_param); +static void stop(void); +static void* getRxEthFrame(unsigned short* o_len_of_data); +static void nextRxEthFrame(void); +static void* allocTxBuf(NyLPC_TUInt16 i_hint,NyLPC_TUInt16* o_size); +static void releaseTxBuf(void* i_buf); +static void sendTxEthFrame(void* i_buf,unsigned short i_size); + +const static struct TiEthernetDevice _interface_KSZ8081RNACA= +{ + "KSZ8081RNACA", + start, + stop, + getRxEthFrame, + nextRxEthFrame, + allocTxBuf, + releaseTxBuf, + sendTxEthFrame, + NULL //deleted API +}; + + + +struct TEtherDriver{ + int rx_idx; + int tx_idx; + uint8_t *tx_desc_start_addr; /**< TX descriptor start address */ + uint8_t *rx_desc_start_addr; /**< RX descriptor start address */ +}; + +static struct TEtherDriver _driver; +static void* _event_param; +static NyLPC_TiEthernetDevice_onEvent _event_handler; + +//////////////////////////////////////////////////////////////////////////////// +// LANパケットバッファ +//////////////////////////////////////////////////////////////////////////////// +#define NUM_OF_RX_BUF 4 +#define SIZE_OF_ETH_PACKET (1536) //16バイト単位であること +static void* RX_BUF_BASE; //[NUM_OF_RX_BUF][SIZE_OF_ETH_PACKET] +static unsigned char* RX_BUF; //[NUM_OF_RX_BUF][SIZE_OF_ETH_PACKET] +static void* TX_BUF_BASE; // +static unsigned char* TX_BUF; //sizeof(struct NyLPC_TcEthernetMM_TxMemoryBlock) + +#define NUM_OF_RX_RING NUM_OF_RX_BUF +#define NUM_OF_TX_RING 4 + +//////////////////////////////////////////////////////////////////////////////// +//private function +//////////////////////////////////////////////////////////////////////////////// + + +static NyLPC_TBool low_level_init(const unsigned char* i_ethaddr,int i_addr_len); +static void setRxDesc(void* rx_buf, int idx); +static void updateRxDesc(int idx); +static void setTxDesc(int idx); +static void updateTxDesc(int idx, uint8_t *buffer, uint16_t length, bool isLast); +static void eth_arch_enable_interrupts(void); +static void eth_arch_disable_interrupts(void); +static NyLPC_TUInt32 waitForTxEthFrameEmpty(void); + +//////////////////////////////////////////////////////////////////////////////// +//LAN API +//////////////////////////////////////////////////////////////////////////////// +NyLPC_TBool EthDev_K64F_getInterface( + const struct TiEthernetDevice** o_dev) +{ + *o_dev=&_interface_KSZ8081RNACA; + RX_BUF_BASE=(unsigned char*)malloc(SIZE_OF_ETH_PACKET*NUM_OF_RX_BUF+RX_BUF_ALIGNMENT); + RX_BUF=(unsigned char*)ENET_ALIGN((NyLPC_TUInt32)RX_BUF_BASE,RX_BUF_ALIGNMENT); + TX_BUF_BASE=malloc(sizeof(struct NyLPC_TcEthernetMM_TxMemoryBlock)+TX_BUF_ALIGNMENT); + TX_BUF=(unsigned char*)ENET_ALIGN((NyLPC_TUInt32)TX_BUF_BASE,TX_BUF_ALIGNMENT); + + return NyLPC_TBool_TRUE; +} + +static NyLPC_TBool start(const struct NyLPC_TEthAddr* i_eth_addr,NyLPC_TiEthernetDevice_onEvent i_handler,void* i_param) +{ + _driver.rx_idx=0; + _driver.tx_idx=0; + //ISRw割り込み設定 + _event_handler=i_handler; + _event_param=i_param; + + if(!low_level_init((const NyLPC_TUInt8*)(i_eth_addr->addr),6)){ + return NyLPC_TBool_FALSE; + } + //TXメモリマネージャの準備(バッファのアライメントは16,パディングも16にしてね。謎バイトが2個いるから。) + NyLPC_cEthernetMM_initialize(TX_BUF); + + //Ethernetの割込み開始設定 + NyLPC_cIsr_enterCritical(); + //Ethernetの初期化シーケンス。割込みONとか + { + eth_arch_enable_interrupts(); + } + NyLPC_cIsr_exitCritical(); + + return NyLPC_TBool_TRUE; +} + +static void stop(void) +{ + NyLPC_cIsr_enterCritical(); + { + eth_arch_disable_interrupts(); + } + NyLPC_cIsr_exitCritical(); + return; +} + + +#define err_mask (kEnetRxBdTrunc | kEnetRxBdCrc | kEnetRxBdNoOctet | kEnetRxBdLengthViolation) + +static void* getRxEthFrame(unsigned short* o_len_of_data) +{ + int idx=_driver.rx_idx; + enet_bd_struct_t * bdPtr = (enet_bd_struct_t*)_driver.rx_desc_start_addr; + if((bdPtr[idx].control & kEnetRxBdEmpty)!=0){ + //パケット未着 + return NULL; + } + if((bdPtr[idx].control & err_mask) != 0){ + //エラー:パケットバッファを再設定して返却 + setRxDesc(RX_BUF+(idx*SIZE_OF_ETH_PACKET),idx); + _driver.rx_idx=(idx+1)%NUM_OF_RX_BUF; + return NULL; + } + *o_len_of_data =(unsigned short)enet_hal_get_bd_length(&(bdPtr[idx]))-2; + return &RX_BUF[idx*SIZE_OF_ETH_PACKET+2]; +} + +static void nextRxEthFrame(void) +{ + int idx=_driver.rx_idx; + enet_bd_struct_t * bdPtr = (enet_bd_struct_t*)_driver.rx_desc_start_addr; + //現在のRXメモリが有効かを確認 + if((bdPtr[idx].control & kEnetRxBdEmpty)==0){ + //パケットバッファを復活させる + updateRxDesc(idx); + //キューのエントリを進行 + _driver.rx_idx=(idx+1)%NUM_OF_RX_BUF; + return; + } +} + +static void* allocTxBuf(NyLPC_TUInt16 i_hint,NyLPC_TUInt16* o_size) +{ + return ((NyLPC_TUInt8*)NyLPC_cEthernetMM_alloc(i_hint,o_size))+2; +} +static void releaseTxBuf(void* i_buf) +{ + NyLPC_cEthernetMM_release((NyLPC_TUInt8*)i_buf-2); +} + + +static void sendTxEthFrame(void* i_buf,unsigned short i_size) +{ + int Index; + struct NyLPC_TTxBufferHeader* bh=NyLPC_TTxBufferHeader_getBufferHeaderAddr(i_buf-2); + //サイズ0なら送信の必要なし + if(i_size == 0) + { + return; + } + //送信デスクリプタが使えるようになるのを待つよ! + waitForTxEthFrameEmpty(); + + //送信対象のメモリブロックを送信中に設定。 + if(bh->is_lock){ + //送信中のメモリブロックなら無視 + return; + } + + //送信中にセット + bh->is_lock=NyLPC_TUInt8_TRUE; + + //最大送信サイズの制限 + if (i_size > ETH_FRAG_SIZE){ + i_size = ETH_FRAG_SIZE; + } + Index=_driver.tx_idx; + + updateTxDesc(Index,(uint8_t*)((unsigned char*)i_buf-2),i_size+2,Index==(NUM_OF_TX_RING-1)); + _driver.tx_idx=(_driver.tx_idx+1)%NUM_OF_TX_RING; + return; +} + +/** + * 送信キューが空くまで待ちます。 + * @return + * 次に書き込むことが出来る送信キューのIDだけど使わないで + */ +static NyLPC_TUInt32 waitForTxEthFrameEmpty(void) +{ + int idx; + int i; + struct NyLPC_TTxBufferHeader *b; + volatile void* p; + volatile enet_bd_struct_t * bdPtr = (enet_bd_struct_t*)_driver.tx_desc_start_addr; + + + //送信キューフルが解除されるまで待ち.現在のTQDescがReady状態の間ループ + + while((bdPtr[_driver.tx_idx].control & kEnetTxBdReady)!=0) + { + NyLPC_cThread_sleep(10); + } + //インデクスを起点にReady状態が0のデスクリプタを全て解放(無駄があるけど無視) + for(i=0;iis_lock=NyLPC_TUInt8_FALSE; + bdPtr[idx].buffer=0; + } + } + return _driver.tx_idx; +} + +//////////////////////////////////////////////////////////////////////////////// +// Ethernet interdface functions +//////////////////////////////////////////////////////////////////////////////// + + +// K64F-specific macros +#define RX_PBUF_AUTO_INDEX (-1) +extern void k64f_init_eth_hardware(void); + + +//static struct k64f_enetdata k64f_enetdata; + +static enet_dev_if_t enetDevIf[HW_ENET_INSTANCE_COUNT]; +static enet_mac_config_t g_enetMacCfg[HW_ENET_INSTANCE_COUNT] = +{ + { + ENET_ETH_MAX_FLEN , /*!< enet receive buffer size*/ + ENET_RX_LARGE_BUFFER_NUM, /*!< enet large receive buffer number*/ + NUM_OF_RX_RING, /*!< enet receive bd number*/ + NUM_OF_TX_RING, /*!< enet transmit bd number*/ + {0}, /*!< enet mac address*/ + kEnetCfgRmii, /*!< enet rmii interface*/ + kEnetCfgSpeed100M, /*!< enet rmii 100M*/ + kEnetCfgFullDuplex, /*!< enet rmii Full- duplex*/ + /*!< enet mac control flag recommended to use enet_mac_control_flag_t + we send frame with crc so receive crc forward for data length check test*/ + kEnetRxCrcFwdEnable | kEnetRxFlowControlEnable, + true, /*!< enet txaccelerator enabled*/ + true, /*!< enet rxaccelerator enabled*/ + false, /*!< enet store and forward*/ + {false, false, true, false, true}, /*!< enet rxaccelerator config*/ + {false, false, true}, /*!< enet txaccelerator config*/ + true, /*!< vlan frame support*/ + true, /*!< phy auto discover*/ + ENET_MII_CLOCK, /*!< enet MDC clock*/ + }, +}; +static enet_phy_config_t g_enetPhyCfg[HW_ENET_INSTANCE_COUNT] = +{ + {0, false} +}; + +static NyLPC_TBool k64f_rx_setup(enet_rxbd_config_t *rxbdCfg); +static NyLPC_TBool k64f_tx_setup(enet_txbd_config_t *txbdCfg); + + +/** + * i_idx番目のデスクリプタにバッファをセット + */ +static void setRxDesc(void* rx_buf, int idx) +{ + enet_bd_struct_t *start = (enet_bd_struct_t *)_driver.rx_desc_start_addr; + /* Setup descriptor and clear statuses */ + enet_hal_init_rxbds(start + idx, (uint8_t*)rx_buf,idx ==(NUM_OF_RX_RING - 1)); + enet_hal_active_rxbd(BOARD_DEBUG_ENET_INSTANCE); +} +static void updateRxDesc(int idx) +{ + enet_bd_struct_t *start = (enet_bd_struct_t *)_driver.rx_desc_start_addr; + /* Setup descriptor and clear statuses */ + enet_hal_update_rxbds(start + idx,NULL,false); + enet_hal_active_rxbd(BOARD_DEBUG_ENET_INSTANCE); +} +static void setTxDesc(int idx) +{ + enet_bd_struct_t *start = (enet_bd_struct_t *)_driver.tx_desc_start_addr; + /* Setup descriptor and clear statuses */ + enet_hal_init_txbds(start + idx,idx ==(NUM_OF_RX_RING - 1)); + enet_hal_active_txbd(BOARD_DEBUG_ENET_INSTANCE); +} + +static void updateTxDesc(int idx, uint8_t *buffer, uint16_t length, bool isLast) +{ + volatile enet_bd_struct_t * bdPtr = (enet_bd_struct_t *)(_driver.tx_desc_start_addr + idx * enet_hal_get_bd_size()); + + bdPtr->length = HTONS(length); /* Set data length*/ + bdPtr->buffer = (uint8_t *)HTONL((uint32_t)buffer); /* Set data buffer*/ + bdPtr->control |= kEnetTxBdLast;//最終フラグメントのフラグね + bdPtr->controlExtend1 |= kEnetTxBdTxInterrupt; + bdPtr->controlExtend2 &= ~TX_DESC_UPDATED_MASK; // descriptor not updated by DMA + bdPtr->control |= kEnetTxBdTransmitCrc | kEnetTxBdReady; + if(isLast){ + //これはデスクリプタの終了位置のフラグ + bdPtr->control |=kEnetTxBdWrap; + } + enet_hal_active_txbd(BOARD_DEBUG_ENET_INSTANCE); +} + +static void* ENET_MAC_CONTEXT_BUF=NULL; + +/** \brief Low level init of the MAC and PHY. + * + * \param[in] netif Pointer to LWIP netif structure + */ +NyLPC_TBool low_level_init(const unsigned char* i_ethaddr,int i_addr_len) +{ + enet_dev_if_t * enetIfPtr; + uint32_t device = BOARD_DEBUG_ENET_INSTANCE; + enet_rxbd_config_t rxbdCfg; + enet_txbd_config_t txbdCfg; + enet_phy_speed_t phy_speed; + enet_phy_duplex_t phy_duplex; + + //RX/TXメモリはデバイス選択時に確保 + k64f_init_eth_hardware(); + + /* Initialize device*/ + enetIfPtr = (enet_dev_if_t *)&enetDevIf[device]; + enetIfPtr->deviceNumber = device; + enetIfPtr->macCfgPtr = &g_enetMacCfg[device]; + enetIfPtr->phyCfgPtr = &g_enetPhyCfg[device]; + enetIfPtr->macApiPtr = &g_enetMacApi; + enetIfPtr->phyApiPtr = (void *)&g_enetPhyApi; + //macアドレスのコピー + memcpy(enetIfPtr->macCfgPtr->macAddr,(char*)i_ethaddr,i_addr_len); + //enetIfPtr->macContextPtrはgetInterface + if(ENET_MAC_CONTEXT_BUF!=NULL){ + free(ENET_MAC_CONTEXT_BUF); + ENET_MAC_CONTEXT_BUF=NULL; + } + ENET_MAC_CONTEXT_BUF=calloc(1, sizeof(enet_mac_context_t)); + if(ENET_MAC_CONTEXT_BUF==NULL){ + return NyLPC_TBool_FALSE;//ERR_BUF; + } + enetIfPtr->macContextPtr = (enet_mac_context_t *)ENET_MAC_CONTEXT_BUF; + + /* Initialize enet buffers*/ + if(!k64f_rx_setup(&rxbdCfg)) { + return NyLPC_TBool_FALSE;//ERR_BUF; + } + /* Initialize enet buffers*/ + if(!k64f_tx_setup(&txbdCfg)) { + return NyLPC_TBool_FALSE;//ERR_BUF; + } + /* Initialize enet module*/ + if (enet_mac_init(enetIfPtr, &rxbdCfg, &txbdCfg) == kStatus_ENET_Success) + { + /* Initialize PHY*/ + if (enetIfPtr->macCfgPtr->isPhyAutoDiscover) { + if (((enet_phy_api_t *)(enetIfPtr->phyApiPtr))->phy_auto_discover(enetIfPtr) != kStatus_PHY_Success) + return NyLPC_TBool_FALSE;//ERR_IF; + } + if (((enet_phy_api_t *)(enetIfPtr->phyApiPtr))->phy_init(enetIfPtr) != kStatus_PHY_Success) + return NyLPC_TBool_FALSE;//ERR_IF; + enetIfPtr->isInitialized = true; + }else{ + // TODOETH: cleanup memory + return NyLPC_TBool_FALSE;//ERR_IF; + } + + /* Get link information from PHY */ + phy_get_link_speed(enetIfPtr, &phy_speed); + phy_get_link_duplex(enetIfPtr, &phy_duplex); + BW_ENET_RCR_RMII_10T(enetIfPtr->deviceNumber, phy_speed == kEnetSpeed10M ? kEnetCfgSpeed10M : kEnetCfgSpeed100M); + BW_ENET_TCR_FDEN(enetIfPtr->deviceNumber, phy_duplex == kEnetFullDuplex ? kEnetCfgFullDuplex : kEnetCfgHalfDuplex); + + /* Enable Ethernet module*/ + enet_hal_config_ethernet(device, true, true); + + /* Active Receive buffer descriptor must be done after module enable*/ + enet_hal_active_rxbd(enetIfPtr->deviceNumber); + enet_hal_active_txbd(enetIfPtr->deviceNumber); + + return NyLPC_TBool_TRUE;//ERR_OK; +} + + +static void* RX_DESC_BUF_BASE=NULL; + + +/** \brief Sets up the RX descriptor ring buffers. + * + * This function sets up the descriptor list used for receive packets. + * + * \param[in] netif Pointer to driver data structure + * \returns true/false + */ +static NyLPC_TBool k64f_rx_setup(enet_rxbd_config_t *rxbdCfg) +{ +// struct k64f_enetdata *k64f_enet = &(netif->state); + enet_dev_if_t *enetIfPtr = (enet_dev_if_t *)&enetDevIf[BOARD_DEBUG_ENET_INSTANCE]; + uint32_t rxBufferSizeAligned; + int i; + + // Allocate RX descriptors + if(RX_DESC_BUF_BASE!=NULL){ + free(RX_DESC_BUF_BASE); + RX_DESC_BUF_BASE=NULL; + } + RX_DESC_BUF_BASE = (void*)calloc(1, enet_hal_get_bd_size() * enetIfPtr->macCfgPtr->rxBdNumber + ENET_BD_ALIGNMENT); + if(RX_DESC_BUF_BASE==NULL){ + return NyLPC_TBool_FALSE; + } + //16byteアライメントに修正 + _driver.rx_desc_start_addr = (uint8_t *)ENET_ALIGN((NyLPC_TUInt32)RX_DESC_BUF_BASE, ENET_BD_ALIGNMENT); + rxBufferSizeAligned = ENET_ALIGN(enetIfPtr->macCfgPtr->rxBufferSize, ENET_RX_BUFFER_ALIGNMENT); + enetIfPtr->macContextPtr->rxBufferSizeAligned = rxBufferSizeAligned; + rxbdCfg->rxBdPtrAlign = _driver.rx_desc_start_addr; + rxbdCfg->rxBdNum = enetIfPtr->macCfgPtr->rxBdNumber; + rxbdCfg->rxBufferNum = enetIfPtr->macCfgPtr->rxBdNumber; + + //初期化 + enet_hal_active_rxbd(BOARD_DEBUG_ENET_INSTANCE); + for(i=0;imacCfgPtr->txBdNumber + ENET_BD_ALIGNMENT); + if(TX_DESC_BUF_BASE==NULL){ + return NyLPC_TBool_FALSE; + } + + _driver.tx_desc_start_addr = (uint8_t *)ENET_ALIGN((uint32_t)TX_DESC_BUF_BASE, ENET_BD_ALIGNMENT); + + txbdCfg->txBdPtrAlign = _driver.tx_desc_start_addr; + txbdCfg->txBufferNum = enetIfPtr->macCfgPtr->txBdNumber; + txbdCfg->txBufferSizeAlign = ENET_ALIGN(enetIfPtr->maxFrameSize, ENET_TX_BUFFER_ALIGNMENT); + + // Make the TX descriptor ring circular + for(i=0;ideviceNumber, kEnetTxFrameInterrupt); + _event_handler(_event_param,NyLPC_TiEthernetDevice_EVENT_ON_TX); +} + +void ENET_Receive_IRQHandler(void) +{ + enet_hal_clear_interrupt(((enet_dev_if_t *)enetIfHandle)->deviceNumber, kEnetRxFrameInterrupt); + _event_handler(_event_param,NyLPC_TiEthernetDevice_EVENT_ON_RX); +} + + +#endif + + + diff --git a/lib/src/driver/ethernet/k64f/EtherDev_K64F_protected.h b/lib/src/driver/ethernet/k64f/EtherDev_K64F_protected.h new file mode 100644 index 0000000..c144462 --- /dev/null +++ b/lib/src/driver/ethernet/k64f/EtherDev_K64F_protected.h @@ -0,0 +1,19 @@ +#ifndef EtherDev_K64F_protected_h +#define EtherDev_K64F_protected_h +#include "NyLPC_stdlib.h" +#include "../NyLPC_IEthernetDevice.h" + + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +NyLPC_TBool EthDev_K64F_getInterface( + const struct TiEthernetDevice** o_dev); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif + diff --git a/lib/src/driver/ethernet/k64f/fsl/fsl_enet_driver.c b/lib/src/driver/ethernet/k64f/fsl/fsl_enet_driver.c new file mode 100644 index 0000000..4e73801 --- /dev/null +++ b/lib/src/driver/ethernet/k64f/fsl/fsl_enet_driver.c @@ -0,0 +1,475 @@ +/* +* Copyright (c) 2013 - 2014, Freescale Semiconductor, Inc. +* All rights reserved. +* +* Redistribution and use in source and binary forms, with or without modification, +* are permitted provided that the following conditions are met: +* +* o Redistributions of source code must retain the above copyright notice, this list +* of conditions and the following disclaimer. +* +* o Redistributions in binary form must reproduce the above copyright notice, this +* list of conditions and the following disclaimer in the documentation and/or +* other materials provided with the distribution. +* +* o Neither the name of Freescale Semiconductor, Inc. nor the names of its +* contributors may be used to endorse or promote products derived from this +* software without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR +* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +/* Modified by mbed for the lwIP port */ +#include "NyLPC_config.h" +#if NyLPC_MCU==NyLPC_MCU_K64F +#include "fsl_enet_driver.h" +#include "fsl_enet_hal.h" +#include "fsl_clock_manager.h" +#include "fsl_interrupt_manager.h" +#include + +//#include "sys_arch.h" + +/******************************************************************************* + * Variables + ******************************************************************************/ +/*! @brief Define ENET's IRQ list */ + +void *enetIfHandle; + +/*! @brief Define MAC driver API structure and for application of stack adaptor layer*/ +const enet_mac_api_t g_enetMacApi = +{ + enet_mac_init, + NULL, // enet_mac_deinit, + NULL, // enet_mac_send, +#if !ENET_RECEIVE_ALL_INTERRUPT + NULL, // enet_mac_receive, +#endif + enet_mii_read, + enet_mii_write, + NULL, // enet_mac_add_multicast_group, + NULL, //enet_mac_leave_multicast_group, +}; +/******************************************************************************* + * Code + ******************************************************************************/ + +// NOTE: we need these functions to be non-blocking fpr the PHY task, hence the +// osDelay() below + +/*FUNCTION**************************************************************** + * + * Function Name: enet_mii_read + * Return Value: The execution status. + * Description: Read function. + * This interface read data over the (R)MII bus from the specified PHY register, + * This function is called by all PHY interfaces. + *END*********************************************************************/ +uint32_t enet_mii_read(uint32_t instance, uint32_t phyAddr, uint32_t phyReg, uint32_t *dataPtr) +{ + uint32_t counter; + + /* Check the input parameters*/ + if (!dataPtr) + { + return kStatus_ENET_InvalidInput; + } + + /* Check if the mii is enabled*/ + if (!enet_hal_is_mii_enabled(instance)) + { + return kStatus_ENET_Miiuninitialized; + } + + /* Clear the MII interrupt event*/ + enet_hal_clear_interrupt(instance, kEnetMiiInterrupt); + + /* Read command operation*/ + enet_hal_set_mii_command(instance, phyAddr, phyReg, kEnetReadValidFrame, 0); + + /* Poll for MII complete*/ + for (counter = 0; counter < kEnetMaxTimeout; counter++) + { + if (enet_hal_get_interrupt_status(instance, kEnetMiiInterrupt)) + { + break; + } + osDelay(1); + } + + /* Check for timeout*/ + if (counter == kEnetMaxTimeout) + { + return kStatus_ENET_TimeOut; + } + + /* Get data from mii register*/ + *dataPtr = enet_hal_get_mii_data(instance); + + /* Clear MII interrupt event*/ + enet_hal_clear_interrupt(instance, kEnetMiiInterrupt); + + return kStatus_ENET_Success; +} + +/*FUNCTION**************************************************************** + * + * Function Name: enet_mii_write + * Return Value: The execution status. + * Description: Write function. + * This interface write data over the (R)MII bus to the specified PHY register. + * This function is called by all PHY interfaces. + *END*********************************************************************/ +uint32_t enet_mii_write(uint32_t instance, uint32_t phyAddr, uint32_t phyReg, uint32_t data) +{ + uint32_t counter; + + /* Check if the mii is enabled*/ + if (!enet_hal_is_mii_enabled(instance)) + { + return kStatus_ENET_Miiuninitialized; + } + + /* Clear the MII interrupt event*/ + enet_hal_clear_interrupt(instance, kEnetMiiInterrupt); + + /* Read command operation*/ + enet_hal_set_mii_command(instance, phyAddr, phyReg, kEnetWriteValidFrame, data); + + /* Poll for MII complete*/ + for (counter = 0; counter < kEnetMaxTimeout; counter++) + { + if (enet_hal_get_interrupt_status(instance, kEnetMiiInterrupt)) + { + break; + } + osDelay(1); + } + + /* Check for timeout*/ + if (counter == kEnetMaxTimeout) + { + return kStatus_ENET_TimeOut; + } + + /* Clear MII intrrupt event*/ + enet_hal_clear_interrupt(instance, kEnetMiiInterrupt); + + return kStatus_ENET_Success; +} +/*FUNCTION**************************************************************** + * + * Function Name: enet_mac_mii_init + * Return Value: The execution status. + * Description:Initialize the ENET Mac mii(mdc/mdio)interface. + *END*********************************************************************/ +uint32_t enet_mac_mii_init(enet_dev_if_t * enetIfPtr) +{ + uint32_t frequency; + + /* Check the input parameters*/ + if (enetIfPtr == NULL) + { + return kStatus_ENET_InvalidInput; + } + + /* Configure mii speed*/ + clock_manager_get_frequency(kSystemClock, &frequency); + enet_hal_config_mii(enetIfPtr->deviceNumber, (frequency/(2 * enetIfPtr->macCfgPtr->miiClock) + 1), + kEnetMdioHoldOneClkCycle, false); + + return kStatus_ENET_Success; +} + +/*FUNCTION**************************************************************** + * + * Function Name: enet_mac_rxbd_init + * Return Value: The execution status. + * Description:Initialize the ENET receive buffer descriptors. + * Note: If you do receive on receive interrupt handler the receive + * data buffer number can be the same as the receive descriptor numbers. + * But if you are polling receive frames please make sure the receive data + * buffers are more than buffer descriptors to guarantee a good performance. + *END*********************************************************************/ +uint32_t enet_mac_rxbd_init(enet_dev_if_t * enetIfPtr, enet_rxbd_config_t *rxbdCfg) +{ + /* Check the input parameters*/ + if ((!enetIfPtr) || (!rxbdCfg)) + { + return kStatus_ENET_InvalidInput; + } + + enetIfPtr->macContextPtr->bufferdescSize = enet_hal_get_bd_size(); + + /* Initialize the bd status*/ + enetIfPtr->macContextPtr->isRxFull = false; + + /* Initialize receive bd base address and current address*/ + enetIfPtr->macContextPtr->rxBdBasePtr = rxbdCfg->rxBdPtrAlign; + enetIfPtr->macContextPtr->rxBdCurPtr = enetIfPtr->macContextPtr->rxBdBasePtr; + enetIfPtr->macContextPtr->rxBdDirtyPtr = enetIfPtr->macContextPtr->rxBdBasePtr; + enet_hal_set_rxbd_address(enetIfPtr->deviceNumber, (uint32_t)(enetIfPtr->macContextPtr->rxBdBasePtr)); + + return kStatus_ENET_Success; +} + +/*FUNCTION**************************************************************** + * + * Function Name: enet_mac_txbd_init + * Return Value: The execution status. + * Description:Initialize the ENET transmit buffer descriptors. + * This function prepare all of the transmit buffer descriptors. + *END*********************************************************************/ +uint32_t enet_mac_txbd_init(enet_dev_if_t * enetIfPtr, enet_txbd_config_t *txbdCfg) +{ + /* Check the input parameters*/ + if ((!enetIfPtr) || (!txbdCfg)) + { + return kStatus_ENET_InvalidInput; + } + + /* Initialize the bd status*/ + enetIfPtr->macContextPtr->isTxFull = false; + + /* Initialize transmit bd base address and current address*/ + enetIfPtr->macContextPtr->txBdBasePtr = txbdCfg->txBdPtrAlign; + enetIfPtr->macContextPtr->txBdCurPtr = enetIfPtr->macContextPtr->txBdBasePtr; + enetIfPtr->macContextPtr->txBdDirtyPtr = enetIfPtr->macContextPtr->txBdBasePtr; + enet_hal_set_txbd_address(enetIfPtr->deviceNumber, (uint32_t)(enetIfPtr->macContextPtr->txBdBasePtr)); + return kStatus_ENET_Success; +} + +/*FUNCTION**************************************************************** + * + * Function Name: enet_mac_configure_fifo_accel + * Return Value: The execution status. + * Description: Configure the ENET FIFO and Accelerator. + *END*********************************************************************/ +uint32_t enet_mac_configure_fifo_accel(enet_dev_if_t * enetIfPtr) +{ + enet_config_rx_fifo_t rxFifo; + enet_config_tx_fifo_t txFifo; + + /* Check the input parameters*/ + if (!enetIfPtr) + { + return kStatus_ENET_InvalidInput; + } + + /* Initialize values that will not be initialized later on */ + rxFifo.rxEmpty = 0; + rxFifo.rxFull = 0; + txFifo.isStoreForwardEnabled = 0; + txFifo.txFifoWrite = 0; + txFifo.txEmpty = 0; + + /* Configure tx/rx accelerator*/ + if (enetIfPtr->macCfgPtr->isRxAccelEnabled) + { + enet_hal_config_rx_accelerator(enetIfPtr->deviceNumber, + (enet_config_rx_accelerator_t *)&(enetIfPtr->macCfgPtr->rxAcceler)); + if ((enetIfPtr->macCfgPtr->rxAcceler.isIpcheckEnabled) || (enetIfPtr->macCfgPtr->rxAcceler.isProtocolCheckEnabled)) + { + rxFifo.rxFull = 0; + } + } + if (enetIfPtr->macCfgPtr->isTxAccelEnabled) + { + enet_hal_config_tx_accelerator(enetIfPtr->deviceNumber, + (enet_config_tx_accelerator_t *)&(enetIfPtr->macCfgPtr->txAcceler)); + if ((enetIfPtr->macCfgPtr->txAcceler.isIpCheckEnabled) || (enetIfPtr->macCfgPtr->txAcceler.isProtocolCheckEnabled)) + { + txFifo.isStoreForwardEnabled = 1; + } + } + if (enetIfPtr->macCfgPtr->isStoreAndFwEnabled) + { + rxFifo.rxFull = 0; + txFifo.isStoreForwardEnabled = 1; + } + + + /* Set TFWR value if STRFWD is not being used */ + if (txFifo.isStoreForwardEnabled == 1) + txFifo.txFifoWrite = 0; + else + /* TFWR value is a trade-off between transmit latency and risk of transmit FIFO underrun due to contention for the system bus + TFWR = 15 means transmission will begin once 960 bytes has been written to the Tx FIFO (for frames larger than 960 bytes) + See Section 45.4.18 - Transmit FIFO Watermark Register of the K64F Reference Manual for details */ + txFifo.txFifoWrite = 15; + + /* Configure tx/rx FIFO with default value*/ + rxFifo.rxAlmostEmpty = 4; + rxFifo.rxAlmostFull = 4; + txFifo.txAlmostEmpty = 4; + txFifo.txAlmostFull = 8; + enet_hal_config_rx_fifo(enetIfPtr->deviceNumber, &rxFifo); + enet_hal_config_tx_fifo(enetIfPtr->deviceNumber, &txFifo); + + return kStatus_ENET_Success; +} + +/*FUNCTION**************************************************************** + * + * Function Name: enet_mac_configure_controller + * Return Value: The execution status. + * Description: Configure the ENET controller with the basic configuration. + *END*********************************************************************/ +uint32_t enet_mac_configure_controller(enet_dev_if_t * enetIfPtr) +{ + uint32_t macCtlCfg; + + /* Check the input parameters*/ + if (enetIfPtr == NULL) + { + return kStatus_ENET_InvalidInput; + } + + macCtlCfg = enetIfPtr->macCfgPtr->macCtlConfigure; + /* Configure rmii/mii interface*/ + enet_hal_config_rmii(enetIfPtr->deviceNumber, enetIfPtr->macCfgPtr->rmiiCfgMode, + enetIfPtr->macCfgPtr->speed, enetIfPtr->macCfgPtr->duplex, false, + (macCtlCfg & kEnetRxMiiLoopback)); + /* Configure receive buffer size*/ + if (enetIfPtr->macCfgPtr->isVlanEnabled) + { + enetIfPtr->maxFrameSize = kEnetMaxFrameVlanSize; + enet_hal_set_rx_max_size(enetIfPtr->deviceNumber, + enetIfPtr->macContextPtr->rxBufferSizeAligned, kEnetMaxFrameVlanSize); + } + else + { + enetIfPtr->maxFrameSize = kEnetMaxFrameSize; + enet_hal_set_rx_max_size(enetIfPtr->deviceNumber, + enetIfPtr->macContextPtr->rxBufferSizeAligned, kEnetMaxFrameSize); + } + + /* Set receive controller promiscuous */ + enet_hal_config_promiscuous(enetIfPtr->deviceNumber, macCtlCfg & kEnetRxPromiscuousEnable); + /* Set receive flow control*/ + enet_hal_enable_flowcontrol(enetIfPtr->deviceNumber, (macCtlCfg & kEnetRxFlowControlEnable)); + /* Set received PAUSE frames are forwarded/terminated*/ + enet_hal_enable_pauseforward(enetIfPtr->deviceNumber, (macCtlCfg & kEnetRxPauseFwdEnable)); + /* Set receive broadcast frame reject*/ + enet_hal_enable_broadcastreject(enetIfPtr->deviceNumber, (macCtlCfg & kEnetRxBcRejectEnable)); + /* Set padding is removed from the received frame*/ + enet_hal_enable_padremove(enetIfPtr->deviceNumber, (macCtlCfg & kEnetRxPadRemoveEnable)); + /* Set the crc of the received frame is stripped from the frame*/ + enet_hal_enable_rxcrcforward(enetIfPtr->deviceNumber, (macCtlCfg & kEnetRxCrcFwdEnable)); + /* Set receive payload length check*/ + enet_hal_enable_payloadcheck(enetIfPtr->deviceNumber, (macCtlCfg & kEnetPayloadlenCheckEnable)); + /* Set control sleep mode*/ + enet_hal_enable_sleep(enetIfPtr->deviceNumber, (macCtlCfg & kEnetSleepModeEnable)); + return kStatus_ENET_Success; +} + +/*FUNCTION**************************************************************** + * + * Function Name: enet_mac_init + * Return Value: The execution status. + * Description:Initialize the ENET device with the basic configuration + * When ENET is used, this function need to be called by the NET initialize + * interface. + *END*********************************************************************/ +uint32_t enet_mac_init(enet_dev_if_t * enetIfPtr, enet_rxbd_config_t *rxbdCfg, + enet_txbd_config_t *txbdCfg) +{ + uint32_t timeOut = 0; + uint32_t devNumber, result = 0; + + /* Check the input parameters*/ + if (enetIfPtr == NULL) + { + return kStatus_ENET_InvalidInput; + } + + /* Get device number and check the parameter*/ + devNumber = enetIfPtr->deviceNumber; + + /* Store the global ENET structure for ISR input parameters for instance 0*/ + if (!devNumber) + { + enetIfHandle = enetIfPtr; + } + + /* Turn on ENET module clock gate */ + clock_manager_set_gate(kClockModuleENET, 0U, true); + + /* Reset ENET mac*/ + enet_hal_reset_ethernet(devNumber); + while ((!enet_hal_is_reset_completed(devNumber)) && (timeOut < kEnetMaxTimeout)) + { + time_delay(1); + timeOut++; + } + + /* Check out if timeout*/ + if (timeOut == kEnetMaxTimeout) + { + return kStatus_ENET_TimeOut; + } + + /* Disable all ENET mac interrupt and Clear all interrupt events*/ + enet_hal_config_interrupt(devNumber, kEnetAllInterrupt, false); + enet_hal_clear_interrupt(devNumber, kEnetAllInterrupt); + + /* Program this station's physical address*/ + enet_hal_set_mac_address(devNumber, enetIfPtr->macCfgPtr->macAddr); + + /* Clear group and individual hash register*/ + enet_hal_set_group_hashtable(devNumber, 0, kEnetSpecialAddressInit); + enet_hal_set_individual_hashtable(devNumber, 0, kEnetSpecialAddressInit); + + /* Configure mac controller*/ + result = enet_mac_configure_controller(enetIfPtr); + if(result != kStatus_ENET_Success) + { + return result; + } + /* Clear mib zero counters*/ + enet_hal_clear_mib(devNumber, true); + + /* Initialize FIFO and accelerator*/ + result = enet_mac_configure_fifo_accel(enetIfPtr); + if(result != kStatus_ENET_Success) + { + return result; + } + /* Initialize receive buffer descriptors*/ + result = enet_mac_rxbd_init(enetIfPtr, rxbdCfg); + if(result != kStatus_ENET_Success) + { + return result; + } + /* Initialize transmit buffer descriptors*/ + result = enet_mac_txbd_init(enetIfPtr, txbdCfg); + if(result != kStatus_ENET_Success) + { + return result; + } + /* Initialize rmii/mii interface*/ + result = enet_mac_mii_init(enetIfPtr); + if (result != kStatus_ENET_Success) + { + return result; + } + + return kStatus_ENET_Success; +} + +/******************************************************************************* + * EOF + ******************************************************************************/ + +#endif + diff --git a/lib/src/driver/ethernet/k64f/fsl/hardware_init_MK64F12.c b/lib/src/driver/ethernet/k64f/fsl/hardware_init_MK64F12.c new file mode 100644 index 0000000..deadd52 --- /dev/null +++ b/lib/src/driver/ethernet/k64f/fsl/hardware_init_MK64F12.c @@ -0,0 +1,95 @@ +/* + * Copyright (c) 2013 - 2014, Freescale Semiconductor, Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * o Redistributions of source code must retain the above copyright notice, this list + * of conditions and the following disclaimer. + * + * o Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * o Neither the name of Freescale Semiconductor, Inc. nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +#include "NyLPC_config.h" +#if NyLPC_MCU==NyLPC_MCU_K64F +#include "fsl_port_hal.h" +#include "fsl_clock_manager.h" +#include "fsl_device_registers.h" +#include "fsl_sim_hal.h" + +/******************************************************************************* + * Code + ******************************************************************************/ +void k64f_init_eth_hardware(void) +{ + uint8_t count; + + /* Disable the mpu*/ + BW_MPU_CESR_VLD(0); + + /* Open POTR clock gate*/ + for (count = 0; count < HW_PORT_INSTANCE_COUNT; count++) + { + clock_manager_set_gate(kClockModulePORT, count, true); + } + + /* Configure gpio*/ + port_hal_mux_control(HW_PORTA, 12, kPortMuxAlt4); /*!< ENET RMII0_RXD1/MII0_RXD1*/ + port_hal_mux_control(HW_PORTA, 13, kPortMuxAlt4); /*!< ENET RMII0_RXD0/MII0_RXD0*/ + port_hal_mux_control(HW_PORTA, 14, kPortMuxAlt4); /*!< ENET RMII0_CRS_DV/MII0_RXDV*/ + port_hal_mux_control(HW_PORTA, 15, kPortMuxAlt4); /*!< ENET RMII0_TXEN/MII0_TXEN*/ + port_hal_mux_control(HW_PORTA, 16, kPortMuxAlt4); /*!< ENET RMII0_TXD0/MII0_TXD0*/ + port_hal_mux_control(HW_PORTA, 17, kPortMuxAlt4); /*!< ENET RMII0_TXD01/MII0_TXD1*/ + port_hal_mux_control(HW_PORTB, 0, kPortMuxAlt4); /*!< ENET RMII0_MDIO/MII0_MDIO*/ + port_hal_configure_open_drain(HW_PORTB,0, true); /*!< ENET RMII0_MDC/MII0_MDC*/ + + // Added for FRDM-K64F + port_hal_pull_select(HW_PORTB, 0, kPortPullUp); + port_hal_configure_pull(HW_PORTB, 0, true); + + port_hal_mux_control(HW_PORTB, 1, kPortMuxAlt4); + /* Configure GPIO for MII interface */ + port_hal_mux_control(HW_PORTA, 9, kPortMuxAlt4); /*!< ENET MII0_RXD3*/ + port_hal_mux_control(HW_PORTA, 10, kPortMuxAlt4); /*!< ENET MII0_RXD2*/ + port_hal_mux_control(HW_PORTA, 11, kPortMuxAlt4); /*!< ENET MII0_RXCLK*/ + port_hal_mux_control(HW_PORTA, 24, kPortMuxAlt4); /*!< ENET MII0_TXD2*/ + port_hal_mux_control(HW_PORTA, 25, kPortMuxAlt4); /*!< ENET MII0_TXCLK*/ + port_hal_mux_control(HW_PORTA, 26, kPortMuxAlt4); /*!< ENET MII0_TXD3*/ + port_hal_mux_control(HW_PORTA, 27, kPortMuxAlt4); /*!< ENET MII0_CRS*/ + port_hal_mux_control(HW_PORTA, 28, kPortMuxAlt4); /*!< ENET MII0_TXER*/ + port_hal_mux_control(HW_PORTA, 29, kPortMuxAlt4); /*!< ENET MII0_COL*/ +#if FSL_FEATURE_ENET_SUPPORT_PTP + port_hal_mux_control(HW_PORTC, (16 + ENET_TIMER_CHANNEL_NUM), kPortMuxAlt4); /* ENET ENET0_1588_TMR0*/ + port_hal_configure_drive_strength(HW_PORTC, (16 + ENET_TIMER_CHANNEL_NUM), kPortHighDriveStrength); +#endif + + /* Open ENET clock gate*/ + clock_manager_set_gate(kClockModuleENET,0,true); + + /* Select the ptp timer outclk*/ + clock_hal_set_clock_source(kSimClockTimeSrc, 2); +} + +/******************************************************************************* + * EOF + ******************************************************************************/ +#endif + + diff --git a/lib/src/driver/ethernet/k64f/k64f_emac_config.h b/lib/src/driver/ethernet/k64f/k64f_emac_config.h new file mode 100644 index 0000000..7c9f2e9 --- /dev/null +++ b/lib/src/driver/ethernet/k64f/k64f_emac_config.h @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2013 - 2014, Freescale Semiconductor, Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * o Redistributions of source code must retain the above copyright notice, this list + * of conditions and the following disclaimer. + * + * o Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * o Neither the name of Freescale Semiconductor, Inc. nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +#ifndef K64F_EMAC_CONFIG_H__ +#define K64F_EMAC_CONFIG_H__ + +//#define ENET_RX_RING_LEN (16) +//#define ENET_TX_RING_LEN (8) +#define ENET_RX_LARGE_BUFFER_NUM (0) +#define ENET_RX_BUFFER_ALIGNMENT (16) +#define ENET_TX_BUFFER_ALIGNMENT (16) +#define ENET_BD_ALIGNMENT (16) +#define ENET_MII_CLOCK (2500000L) +#define RX_BUF_ALIGNMENT (16) +#define TX_BUF_ALIGNMENT (8) +#define BOARD_DEBUG_ENET_INSTANCE (0) + +#define ENET_ETH_MAX_FLEN (1522) // recommended size for a VLAN frame + +#endif // #define K64F_EMAC_CONFIG_H__ + + diff --git a/lib/src/driver/ethernet/lpc17xx/EthDev.c b/lib/src/driver/ethernet/lpc17xx/EthDev.c index caae24b..3bd2616 100644 --- a/lib/src/driver/ethernet/lpc17xx/EthDev.c +++ b/lib/src/driver/ethernet/lpc17xx/EthDev.c @@ -22,3 +22,4 @@ const struct TiEthernetDevice* getEthernetDevicePnP(void) #endif + diff --git a/lib/src/driver/ethernet/lpc17xx/EthDev_LPC17xx.c b/lib/src/driver/ethernet/lpc17xx/EthDev_LPC17xx.c index 9f3fc37..56cd9da 100644 --- a/lib/src/driver/ethernet/lpc17xx/EthDev_LPC17xx.c +++ b/lib/src/driver/ethernet/lpc17xx/EthDev_LPC17xx.c @@ -10,6 +10,7 @@ #include "EthDev_LPC17xx.h" #include "NyLPC_os.h" #include "LPC17xx.h" +#include "../NyLPC_cEthernetMM.h" /* If no buffers are available, then wait this long before looking again.... */ #define emacBUFFER_WAIT_DELAY_MS 3 #define emacBUFFER_WAIT_EMPTY_DELAY_MS 10 @@ -68,14 +69,14 @@ static NyLPC_TUInt32 waitForTxEthFrameEmpty(void) { p=(void*)TX_DESC_PACKET(i); if(p!=NULL){ - b=((struct NyLPC_TTxBufferHeader*)p)-1; + b=NyLPC_TTxBufferHeader_getBufferHeaderAddr(p); b->is_lock=NyLPC_TUInt8_FALSE; TX_DESC_PACKET(i)=0; } } p=(void*)TX_DESC_PACKET(i); if(p!=NULL){ - b=((struct NyLPC_TTxBufferHeader*)p)-1; + b=NyLPC_TTxBufferHeader_getBufferHeaderAddr(p); b->is_lock=NyLPC_TUInt8_FALSE; TX_DESC_PACKET(i)=0; } @@ -89,15 +90,15 @@ void EthDev_LPC17xx_processTx(void) /** * Ethernetパケットを送信します。 - * allocTxBufで得たバッファか、NyLPC_TTxBufferHeaderのペイロード部分を指定すること。 + * allocTxBufで得たバッファを指定すること。 *

関数仕様

* この関数は、i_bufが * */ -void EthDev_LPC17xx_sendTxEthFrame(struct NyLPC_TTxBufferHeader* i_buf,unsigned short i_size) +void EthDev_LPC17xx_sendTxEthFrame(void* i_buf,unsigned short i_size) { NyLPC_TUInt32 IndexNext,Index; - + struct NyLPC_TTxBufferHeader* bh=NyLPC_TTxBufferHeader_getBufferHeaderAddr(i_buf); //サイズ0なら送信の必要なし if(i_size == 0) { @@ -109,11 +110,11 @@ void EthDev_LPC17xx_sendTxEthFrame(struct NyLPC_TTxBufferHeader* i_buf,unsigned //送信対象のメモリブロックを送信中に設定。 // b=(i_buf+1); //送信中のメモリブロックなら無視 - if(i_buf->is_lock){ + if(bh->is_lock){ return; } //送信中にセット - i_buf->is_lock=NyLPC_TUInt8_TRUE; + bh->is_lock=NyLPC_TUInt8_TRUE; //送信データのセット Index = LPC_EMAC->TxProduceIndex; @@ -122,7 +123,7 @@ void EthDev_LPC17xx_sendTxEthFrame(struct NyLPC_TTxBufferHeader* i_buf,unsigned } //送信処理 - TX_DESC_PACKET( Index ) = ( unsigned long )(i_buf+1); + TX_DESC_PACKET( Index ) = ( unsigned long )i_buf; //See UM10360.pdf Table 181. Transmit descriptor control word TX_DESC_CTRL( Index ) = ((i_size-1) | TCTRL_LAST | TCTRL_INT ); LPC_EMAC->TxProduceIndex = IndexNext; diff --git a/lib/src/driver/ethernet/lpc17xx/EthDev_LPC17xx.h b/lib/src/driver/ethernet/lpc17xx/EthDev_LPC17xx.h index f4fdbe8..4eb1828 100644 --- a/lib/src/driver/ethernet/lpc17xx/EthDev_LPC17xx.h +++ b/lib/src/driver/ethernet/lpc17xx/EthDev_LPC17xx.h @@ -320,7 +320,7 @@ typedef struct { /* TX Status struct */ void EthDev_LPC17xx_processTx(void); -void EthDev_LPC17xx_sendTxEthFrame(struct NyLPC_TTxBufferHeader* i_buf,unsigned short i_size); +void EthDev_LPC17xx_sendTxEthFrame(void* i_buf,unsigned short i_size); void* EthDev_LPC17xx_getRxEthFrame(unsigned short* o_len_of_data); void EthDev_LPC17xx_nextRxEthFrame(void); diff --git a/lib/src/driver/ethernet/lpc17xx/EtherDev_DP83848C.c b/lib/src/driver/ethernet/lpc17xx/EtherDev_DP83848C.c index 762f009..ba3c6bb 100644 --- a/lib/src/driver/ethernet/lpc17xx/EtherDev_DP83848C.c +++ b/lib/src/driver/ethernet/lpc17xx/EtherDev_DP83848C.c @@ -136,8 +136,8 @@ static NyLPC_TBool prvSetupLinkStatus( void ); static NyLPC_TBool start(const struct NyLPC_TEthAddr* i_eth_addr,NyLPC_TiEthernetDevice_onEvent i_handler,void* i_param); static void stop(void); -static struct NyLPC_TTxBufferHeader* allocTxBuf(NyLPC_TUInt16 i_hint,NyLPC_TUInt16* o_size); -static void releaseTxBuf(struct NyLPC_TTxBufferHeader* i_buf); +static void* allocTxBuf(NyLPC_TUInt16 i_hint,NyLPC_TUInt16* o_size); +static void releaseTxBuf(void* i_buf); /*-----------------------------------------------------------*/ #define ETH_TX_BUF_BASE (void*)(ETH_BUF_BASE+ETH_FRAG_SIZE*NUM_RX_FRAG) @@ -255,11 +255,11 @@ static void stop(void) return; } -static struct NyLPC_TTxBufferHeader* allocTxBuf(NyLPC_TUInt16 i_hint,NyLPC_TUInt16* o_size) +static void* allocTxBuf(NyLPC_TUInt16 i_hint,NyLPC_TUInt16* o_size) { return NyLPC_cEthernetMM_alloc(i_hint,o_size); } -static void releaseTxBuf(struct NyLPC_TTxBufferHeader* i_buf) +static void releaseTxBuf(void* i_buf) { NyLPC_cEthernetMM_release(i_buf); } diff --git a/lib/src/driver/ethernet/lpc17xx/EtherDev_DP83848C_protected.h b/lib/src/driver/ethernet/lpc17xx/EtherDev_DP83848C_protected.h index 50255df..6dbfc5a 100644 --- a/lib/src/driver/ethernet/lpc17xx/EtherDev_DP83848C_protected.h +++ b/lib/src/driver/ethernet/lpc17xx/EtherDev_DP83848C_protected.h @@ -22,3 +22,4 @@ NyLPC_TBool EthDev_DP83848C_getInterface( #endif /* __cplusplus */ #endif + diff --git a/lib/src/driver/ethernet/lpc17xx/EtherDev_LAN8720.c b/lib/src/driver/ethernet/lpc17xx/EtherDev_LAN8720.c index 2a3c756..38a05ed 100644 --- a/lib/src/driver/ethernet/lpc17xx/EtherDev_LAN8720.c +++ b/lib/src/driver/ethernet/lpc17xx/EtherDev_LAN8720.c @@ -123,8 +123,8 @@ static long prvSetupLinkStatus( void ); static NyLPC_TBool start(const struct NyLPC_TEthAddr* i_eth_addr,NyLPC_TiEthernetDevice_onEvent i_handler,void* i_param); static void stop(void); -static struct NyLPC_TTxBufferHeader* allocTxBuf(NyLPC_TUInt16 i_hint,NyLPC_TUInt16* o_size); -static void releaseTxBuf(struct NyLPC_TTxBufferHeader* i_buf); +static void* allocTxBuf(NyLPC_TUInt16 i_hint,NyLPC_TUInt16* o_size); +static void releaseTxBuf(void* i_buf); /** EMAC ISRハンドラ*/ static void emacIsrHandler(unsigned long i_status); @@ -245,11 +245,11 @@ static void stop(void) } -static struct NyLPC_TTxBufferHeader* allocTxBuf(NyLPC_TUInt16 i_hint,NyLPC_TUInt16* o_size) +static void* allocTxBuf(NyLPC_TUInt16 i_hint,NyLPC_TUInt16* o_size) { return NyLPC_cEthernetMM_alloc(i_hint,o_size); } -static void releaseTxBuf(struct NyLPC_TTxBufferHeader* i_buf) +static void releaseTxBuf(void* i_buf) { NyLPC_cEthernetMM_release(i_buf); } @@ -325,7 +325,7 @@ static void prvSetupEMACHardware( void ) /** * Private function depend on configulation. - * デバイス依存部分 + * デバイス依存部分 */ diff --git a/lib/src/driver/ethernet/lpc17xx/EtherDev_LAN8720_protected.h b/lib/src/driver/ethernet/lpc17xx/EtherDev_LAN8720_protected.h index 5a195f2..5a15e3e 100644 --- a/lib/src/driver/ethernet/lpc17xx/EtherDev_LAN8720_protected.h +++ b/lib/src/driver/ethernet/lpc17xx/EtherDev_LAN8720_protected.h @@ -21,3 +21,4 @@ NyLPC_TBool EthDev_LAN8720_getInterface( #endif /* __cplusplus */ #endif + diff --git a/lib/src/driver/ethernet/lpc4088/EthDev_LPC4088.c b/lib/src/driver/ethernet/lpc4088/EthDev_LPC4088.c index 6a5f5c2..b899bf0 100644 --- a/lib/src/driver/ethernet/lpc4088/EthDev_LPC4088.c +++ b/lib/src/driver/ethernet/lpc4088/EthDev_LPC4088.c @@ -49,9 +49,9 @@ static NyLPC_TBool start(const struct NyLPC_TEthAddr* i_eth_addr,NyLPC_TiEtherne static void stop(void); static void* getRxEthFrame(unsigned short* o_len_of_data); static void nextRxEthFrame(void); -static struct NyLPC_TTxBufferHeader* allocTxBuf(NyLPC_TUInt16 i_hint,NyLPC_TUInt16* o_size); -static void releaseTxBuf(struct NyLPC_TTxBufferHeader* i_buf); -static void sendTxEthFrame(struct NyLPC_TTxBufferHeader* i_buf,unsigned short i_size); +static void* allocTxBuf(NyLPC_TUInt16 i_hint,NyLPC_TUInt16* o_size); +static void releaseTxBuf(void* i_buf); +static void sendTxEthFrame(void* i_buf,unsigned short i_size); static void processTx(void); //////////////////////////////////////////////////////////////////////////////// @@ -107,7 +107,7 @@ NyLPC_TBool EthDev_LPC4088_getInterface( { int regv, tout; unsigned int clock = clockselect(); - + LPC_SC->PCONP |= 0x40000000; /* Power Up the EMAC controller. */ LPC_IOCON->P1_0 &= ~0x07; /* ENET I/O config */ LPC_IOCON->P1_0 |= 0x01; /* ENET_TXD0 */ @@ -129,24 +129,24 @@ NyLPC_TBool EthDev_LPC4088_getInterface( LPC_IOCON->P1_16 |= 0x01; /* ENET_MDC */ LPC_IOCON->P1_17 &= ~0x07; LPC_IOCON->P1_17 |= 0x01; /* ENET_MDIO */ - + /* Reset all EMAC internal modules. */ LPC_EMAC->MAC1 = MAC1_RES_TX | MAC1_RES_MCS_TX | MAC1_RES_RX | MAC1_RES_MCS_RX | MAC1_SIM_RES | MAC1_SOFT_RES; - LPC_EMAC->Command = CR_REG_RES | CR_TX_RES | CR_RX_RES | CR_PASS_RUNT_FRM; + LPC_EMAC->Command = CR_REG_RES | CR_TX_RES | CR_RX_RES | CR_PASS_RUNT_FRM; for (tout = 100; tout; tout--) { __NOP(); } /* A short delay */ - + /* Initialize MAC control registers. */ - LPC_EMAC->MAC1 = MAC1_PASS_ALL; + LPC_EMAC->MAC1 = MAC1_PASS_ALL; LPC_EMAC->MAC2 = MAC2_CRC_EN | MAC2_PAD_EN; LPC_EMAC->MAXF = ETH_MAX_FLEN; LPC_EMAC->CLRT = CLRT_DEF; LPC_EMAC->IPGR = IPGR_DEF; - + /* Enable Reduced MII interface. */ LPC_EMAC->MCFG = (clock << 0x2) & MCFG_CLK_SEL; /* Set clock */ LPC_EMAC->MCFG |= MCFG_RES_MII; /* and reset */ LPC_EMAC->Command = CR_RMII | CR_PASS_RUNT_FRM |CR_PASS_RX_FILT; /* Enable Reduced MII interface. */ - + for (tout = 100; tout; tout--) { __NOP(); } /* A short delay */ LPC_EMAC->MCFG = (clock << 0x2) & MCFG_CLK_SEL; @@ -154,7 +154,7 @@ for (tout = 100; tout; tout--) { __NOP(); } /* A short delay */ LPC_EMAC->SUPP = SUPP_RES_RMII; /* Reset Reduced MII Logic. */ for (tout = 100; tout; tout--) { __NOP(); } /* A short delay */ LPC_EMAC->SUPP = SUPP_SPEED; - + phy_write(PHY_REG_BMCR, PHY_BMCR_RESET); /* perform PHY reset */ for(tout = 0x20000; ; tout--) { /* Wait for hardware reset to end. */ regv = phy_read(PHY_REG_BMCR); @@ -180,7 +180,7 @@ for (tout = 100; tout; tout--) { __NOP(); } /* A short delay */ return NyLPC_TBool_FALSE; /* Error */ } LPC_EMAC->TxProduceIndex = 0; - LPC_EMAC->RxConsumeIndex = 0; + LPC_EMAC->RxConsumeIndex = 0; return NyLPC_TBool_TRUE; } @@ -203,7 +203,7 @@ static NyLPC_TBool start(const struct NyLPC_TEthAddr* i_eth_addr,NyLPC_TiEtherne /* Initialize Tx and Rx DMA Descriptors */ prevRxDescriptor(); prevTxDescriptor(); - //wait for link up + //wait for link up for(i=0;i<5;i++){ if(ethernet_link()!=0){ break; @@ -222,7 +222,7 @@ static NyLPC_TBool start(const struct NyLPC_TEthAddr* i_eth_addr,NyLPC_TiEtherne { LPC_EMAC->IntEnable = INT_RX_DONE | INT_TX_DONE; /* Enable EMAC interrupts. */ LPC_EMAC->IntClear = 0xFFFF; /* Reset all interrupts */ - + LPC_EMAC->Command |= (CR_RX_EN | CR_TX_EN); /* Enable receive and transmit mode of MAC Ethernet core */ LPC_EMAC->MAC1 |= MAC1_REC_EN; @@ -241,7 +241,7 @@ static void stop(void) { LPC_EMAC->IntEnable &= ~(INT_RX_DONE | INT_TX_DONE); LPC_EMAC->IntClear = 0xFFFF; - + NVIC_DisableIRQ( ENET_IRQn ); } NyLPC_cIsr_exitCritical(); @@ -253,11 +253,11 @@ static void stop(void) NyLPC_cEthernetMM_finalize(); } -static struct NyLPC_TTxBufferHeader* allocTxBuf(NyLPC_TUInt16 i_hint,NyLPC_TUInt16* o_size) +static void* allocTxBuf(NyLPC_TUInt16 i_hint,NyLPC_TUInt16* o_size) { return NyLPC_cEthernetMM_alloc(i_hint,o_size); } -static void releaseTxBuf(struct NyLPC_TTxBufferHeader* i_buf) +static void releaseTxBuf(void* i_buf) { NyLPC_cEthernetMM_release(i_buf); } @@ -274,14 +274,15 @@ static void processTx(void) /** * Ethernetパケットを送信します。 - * allocTxBufで得たバッファか、NyLPC_TTxBufferHeaderのペイロード部分を指定すること。 + * allocTxBufで得たバッファを指定すること。 *

関数仕様

* この関数は、i_bufが * */ -static void sendTxEthFrame(struct NyLPC_TTxBufferHeader* i_buf,unsigned short i_size) +static void sendTxEthFrame(void* i_buf,unsigned short i_size) { NyLPC_TUInt32 IndexNext,Index; + struct NyLPC_TTxBufferHeader* bh=NyLPC_TTxBufferHeader_getBufferHeaderAddr(i_buf); //サイズ0なら送信の必要なし if(i_size == 0) @@ -294,11 +295,11 @@ static void sendTxEthFrame(struct NyLPC_TTxBufferHeader* i_buf,unsigned short i_ //送信対象のメモリブロックを送信中に設定。 // b=(i_buf+1); //送信中のメモリブロックなら無視 - if(i_buf->is_lock){ + if(bh->is_lock){ return; } //送信中にセット - i_buf->is_lock=NyLPC_TUInt8_TRUE; + bh->is_lock=NyLPC_TUInt8_TRUE; //送信データのセット Index = LPC_EMAC->TxProduceIndex; @@ -306,7 +307,7 @@ static void sendTxEthFrame(struct NyLPC_TTxBufferHeader* i_buf,unsigned short i_ i_size = ETH_FRAG_SIZE; } //送信処理 - TX_DESC_PACKET( Index ) = ( unsigned long )(i_buf+1); + TX_DESC_PACKET( Index ) = ( unsigned long )i_buf; //See UM10360.pdf Table 181. Transmit descriptor control word TX_DESC_CTRL( Index ) = ((i_size-1) | TCTRL_LAST | TCTRL_INT ); LPC_EMAC->TxProduceIndex = IndexNext; @@ -423,14 +424,14 @@ static NyLPC_TUInt32 waitForTxEthFrameEmpty(void) { p=(void*)TX_DESC_PACKET(i); if(p!=NULL){ - b=((struct NyLPC_TTxBufferHeader*)p)-1; + b=NyLPC_TTxBufferHeader_getBufferHeaderAddr(p); b->is_lock=NyLPC_TUInt8_FALSE; TX_DESC_PACKET(i)=0; } } p=(void*)TX_DESC_PACKET(i); if(p!=NULL){ - b=((struct NyLPC_TTxBufferHeader*)p)-1; + b=NyLPC_TTxBufferHeader_getBufferHeaderAddr(p); b->is_lock=NyLPC_TUInt8_FALSE; TX_DESC_PACKET(i)=0; } @@ -444,22 +445,22 @@ static NyLPC_TUInt32 waitForTxEthFrameEmpty(void) static void ethernet_set_link(int speed, int duplex) { unsigned short phy_data; int tout; - + if((speed < 0) || (speed > 1)) { phy_data = PHY_AUTO_NEG; } else { phy_data = (((unsigned short) speed << 13) | ((unsigned short) duplex << 8)); } - + phy_write(PHY_REG_BMCR, phy_data); - + for (tout = 100; tout; tout--) { __NOP(); } /* A short delay */ - + switch(phy_id) { case DP83848C_ID: phy_data = phy_read(PHY_REG_STS); - + if(phy_data & PHY_STS_DUPLEX) { LPC_EMAC->MAC2 |= MAC2_FULL_DUP; LPC_EMAC->Command |= CR_FULL_DUP; @@ -469,17 +470,17 @@ static void ethernet_set_link(int speed, int duplex) { LPC_EMAC->Command &= ~CR_FULL_DUP; LPC_EMAC->IPGT = IPGT_HALF_DUP; } - + if(phy_data & PHY_STS_SPEED) { LPC_EMAC->SUPP &= ~SUPP_SPEED; } else { LPC_EMAC->SUPP |= SUPP_SPEED; } break; - + case LAN8720_ID: phy_data = phy_read(PHY_REG_SCSR); - + if (phy_data & PHY_SCSR_DUPLEX) { LPC_EMAC->MAC2 |= MAC2_FULL_DUP; LPC_EMAC->Command |= CR_FULL_DUP; @@ -488,13 +489,13 @@ static void ethernet_set_link(int speed, int duplex) { LPC_EMAC->Command &= ~CR_FULL_DUP; LPC_EMAC->IPGT = IPGT_HALF_DUP; } - + if(phy_data & PHY_SCSR_100MBIT) { LPC_EMAC->SUPP |= SUPP_SPEED; } else { LPC_EMAC->SUPP &= ~SUPP_SPEED; } - + break; } } diff --git a/lib/src/driver/ethernet/lpc4088/copy_of_ethernet_api.h b/lib/src/driver/ethernet/lpc4088/copy_of_ethernet_api.h index 1713252..c15addc 100644 --- a/lib/src/driver/ethernet/lpc4088/copy_of_ethernet_api.h +++ b/lib/src/driver/ethernet/lpc4088/copy_of_ethernet_api.h @@ -7,7 +7,7 @@ #include "cmsis.h" #include "mbed_interface.h" #include "toolchain.h" -#include "error.h" +//#include "error.h" #define NUM_RX_FRAG 3 /* Num.of RX Fragments. */ #define NUM_TX_FRAG 16 /* Num.of TX Fragments. */ @@ -307,4 +307,4 @@ typedef struct TX_STAT_TypeDef TX_STAT_TypeDef; #define PHY_BMSR_LINK 0x0004 /* PHY BMSR Link valid */ #define PHY_SCSR_100MBIT 0x0008 /* Speed: 1=100 MBit, 0=10Mbit */ -#define PHY_SCSR_DUPLEX 0x0010 /* PHY Duplex Mask */ +#define PHY_SCSR_DUPLEX 0x0010 /* PHY Duplex Mask */ \ No newline at end of file diff --git a/lib/src/driver/flash/LPC17xx_IAP.c b/lib/src/driver/flash/LPC17xx_IAP.c deleted file mode 100644 index 93e51ba..0000000 --- a/lib/src/driver/flash/LPC17xx_IAP.c +++ /dev/null @@ -1,124 +0,0 @@ -/* - * LPC17xx_IAP.c - * - * Created on: 2011/10/17 - * Author: nyatla - */ - -#include "LPC17xx_IAP.h" - -/** - * IPAに通知するCPUクロック。 - * ここで指定したクロック以上で動作させないでください。 - */ -#define IPA_CPU_FREQ_IN_HZ ( ( unsigned long ) 100000000 ) -#define LPC17xx_FLASH_SECTOR_ADDR_16 ((void*)0x00010000) - -//Define data structure or pointers to pass IAP command table and result table to the IAP -#define IAP_LOCATION 0x1FFF1FF1; -/** - * @param command - * - * @param result - */ -typedef void (*_LPCXpresso_IAP_FUNC)(unsigned long command[],unsigned long result[]); -/** - * - */ -_LPCXpresso_IAP_FUNC LPCXpresso_iap_entry=(_LPCXpresso_IAP_FUNC) IAP_LOCATION; - - -/** - * アドレスをフラッシュメモリのセクタ番号へ変換する。 - */ -int LPC17xx_IAP_addr2Sector(const void* addr,unsigned long* o_sector) -{ - unsigned long t; - if(addr>=LPC17xx_FLASH_SECTOR_ADDR_16){ - t=((((unsigned long)addr)-((unsigned long)LPC17xx_FLASH_SECTOR_ADDR_16))/0x8000)+16; - if(t>29){ - return LPC17xx_IAP_FALSE;//Error - } - *o_sector=t; - }else{ - *o_sector=((unsigned long)addr)/0x1000; - } - return LPC17xx_IAP_TRUE;//Error -} -/** - * セクタ番号をフラッシュメモリのアドレスへ変換する。 - */ -int LPC17xx_IAP_sector2Addr(unsigned int i_sector,void** o_addr) -{ - if(i_sector<16){ - *o_addr=(void*)(0x1000*i_sector); - }else if(i_sector>29){ - return LPC17xx_IAP_FALSE; - }else{ - *o_addr=(void*)(((unsigned int)LPC17xx_FLASH_SECTOR_ADDR_16)+(i_sector-16)*0x8000); - } - return LPC17xx_IAP_TRUE; -} - - -unsigned long LPC17xx_IAP_getSectorSize(unsigned int i_sector) -{ - if(i_sector<16){ - return 0x1000; - }else if(i_sector>29){ - return 0; - }else{ - return 0x8000; - } -} -/** - * IAPのprepareコマンドを実行 - */ -unsigned long LPC17xx_IAP_prepare(unsigned long i_start,unsigned long i_end) -{ - unsigned long b[8]; - unsigned long c[5]; - unsigned long r[5]; - c[0]=50; - c[1]=i_start; - c[2]=i_end; - memcpy(b,(void*)0x10000000,32); - LPCXpresso_iap_entry(c,r); - memcpy((void*)0x10000000,b,32); - return r[0]; -} -/** - * IAPのcopy ram to flashコマンドを実行。 - */ -unsigned long LPC17xx_IAP_copyRam2Flash(const void* i_flash_addr,const void* i_src_addr,unsigned long i_size) -{ - unsigned long b[8]; - unsigned long c[5]; - unsigned long r[5]; - c[0]=51; - c[1]=(unsigned long)i_flash_addr; - c[2]=(unsigned long)i_src_addr; - c[3]=i_size; - c[4]=IPA_CPU_FREQ_IN_HZ/1000; - memcpy(b,(void*)0x10000000,32); - LPCXpresso_iap_entry(c,r); - memcpy((void*)0x10000000,b,32); - return r[0]; -} -/** - * IAPのeraseコマンドを実行 - */ -unsigned long LPC17xx_IAP_erase(unsigned long i_start,unsigned long i_end) -{ - unsigned long b[8]; - unsigned long c[5]; - unsigned long r[5]; - c[0]=52; - c[1]=i_start; - c[2]=i_end; - c[3]=IPA_CPU_FREQ_IN_HZ/1000; - memcpy(b,(void*)0x10000000,32); - LPCXpresso_iap_entry(c,r); - memcpy((void*)0x10000000,b,32); - return r[0]; -} diff --git a/lib/src/driver/flash/LPCxxx/LPC17xx_IAP.c b/lib/src/driver/flash/LPCxxx/LPC17xx_IAP.c new file mode 100644 index 0000000..30f1f00 --- /dev/null +++ b/lib/src/driver/flash/LPCxxx/LPC17xx_IAP.c @@ -0,0 +1,126 @@ +/* + * LPC17xx_IAP.c + * + * Created on: 2011/10/17 + * Author: nyatla + */ +#include "NyLPC_config.h" +#if NyLPC_MCU==NyLPC_MCU_LPC4088 || NyLPC_MCU==NyLPC_MCU_LPC17xx + +#include "LPC17xx_IAP.h" +/** + * IPAに通知するCPUクロック。 + * ここで指定したクロック以上で動作させないでください。 + */ +#define IPA_CPU_FREQ_IN_HZ ( ( unsigned long ) 100000000 ) +#define LPC17xx_FLASH_SECTOR_ADDR_16 ((void*)0x00010000) + +//Define data structure or pointers to pass IAP command table and result table to the IAP +#define IAP_LOCATION 0x1FFF1FF1; +/** + * @param command + * + * @param result + */ +typedef void (*_LPCXpresso_IAP_FUNC)(unsigned long command[],unsigned long result[]); +/** + * + */ +_LPCXpresso_IAP_FUNC LPCXpresso_iap_entry=(_LPCXpresso_IAP_FUNC) IAP_LOCATION; + + +/** + * アドレスをフラッシュメモリのセクタ番号へ変換する。 + */ +int LPC17xx_IAP_addr2Sector(const void* addr,unsigned long* o_sector) +{ + unsigned long t; + if(addr>=LPC17xx_FLASH_SECTOR_ADDR_16){ + t=((((unsigned long)addr)-((unsigned long)LPC17xx_FLASH_SECTOR_ADDR_16))/0x8000)+16; + if(t>29){ + return LPC17xx_IAP_FALSE;//Error + } + *o_sector=t; + }else{ + *o_sector=((unsigned long)addr)/0x1000; + } + return LPC17xx_IAP_TRUE;//Error +} +/** + * セクタ番号をフラッシュメモリのアドレスへ変換する。 + */ +int LPC17xx_IAP_sector2Addr(unsigned int i_sector,void** o_addr) +{ + if(i_sector<16){ + *o_addr=(void*)(0x1000*i_sector); + }else if(i_sector>29){ + return LPC17xx_IAP_FALSE; + }else{ + *o_addr=(void*)(((unsigned int)LPC17xx_FLASH_SECTOR_ADDR_16)+(i_sector-16)*0x8000); + } + return LPC17xx_IAP_TRUE; +} + + +unsigned long LPC17xx_IAP_getSectorSize(unsigned int i_sector) +{ + if(i_sector<16){ + return 0x1000; + }else if(i_sector>29){ + return 0; + }else{ + return 0x8000; + } +} +/** + * IAPのprepareコマンドを実行 + */ +unsigned long LPC17xx_IAP_prepare(unsigned long i_start,unsigned long i_end) +{ + unsigned long b[8]; + unsigned long c[5]; + unsigned long r[5]; + c[0]=50; + c[1]=i_start; + c[2]=i_end; + memcpy(b,(void*)0x10000000,32); + LPCXpresso_iap_entry(c,r); + memcpy((void*)0x10000000,b,32); + return r[0]; +} +/** + * IAPのcopy ram to flashコマンドを実行。 + */ +unsigned long LPC17xx_IAP_copyRam2Flash(const void* i_flash_addr,const void* i_src_addr,unsigned long i_size) +{ + unsigned long b[8]; + unsigned long c[5]; + unsigned long r[5]; + c[0]=51; + c[1]=(unsigned long)i_flash_addr; + c[2]=(unsigned long)i_src_addr; + c[3]=i_size; + c[4]=IPA_CPU_FREQ_IN_HZ/1000; + memcpy(b,(void*)0x10000000,32); + LPCXpresso_iap_entry(c,r); + memcpy((void*)0x10000000,b,32); + return r[0]; +} +/** + * IAPのeraseコマンドを実行 + */ +unsigned long LPC17xx_IAP_erase(unsigned long i_start,unsigned long i_end) +{ + unsigned long b[8]; + unsigned long c[5]; + unsigned long r[5]; + c[0]=52; + c[1]=i_start; + c[2]=i_end; + c[3]=IPA_CPU_FREQ_IN_HZ/1000; + memcpy(b,(void*)0x10000000,32); + LPCXpresso_iap_entry(c,r); + memcpy((void*)0x10000000,b,32); + return r[0]; +} +#endif diff --git a/lib/src/driver/flash/LPC17xx_IAP.h b/lib/src/driver/flash/LPCxxx/LPC17xx_IAP.h similarity index 96% rename from lib/src/driver/flash/LPC17xx_IAP.h rename to lib/src/driver/flash/LPCxxx/LPC17xx_IAP.h index a89d20a..279522d 100644 --- a/lib/src/driver/flash/LPC17xx_IAP.h +++ b/lib/src/driver/flash/LPCxxx/LPC17xx_IAP.h @@ -1,26 +1,26 @@ -#ifndef LPC17xx_IAP_h -#define LPC17xx_IAP_h - -#include -#ifdef __cplusplus -extern "C" { -#endif /* __cplusplus */ - -#define LPC17xx_IAP_TRUE (0==0) -#define LPC17xx_IAP_FALSE (0!=0) -//Command is executed successfully. -#define LPC17xx_IAP_CMD_SUCCESS 0 - -int LPC17xx_IAP_addr2Sector(const void* addr,unsigned long* o_sector); -int LPC17xx_IAP_sector2Addr(unsigned int i_sector,void** o_addr); - -unsigned long LPC17xx_IAP_getSectorSize(unsigned int i_sector); -unsigned long LPC17xx_IAP_prepare(unsigned long i_start,unsigned long i_end); -unsigned long LPC17xx_IAP_copyRam2Flash(const void* i_flash_addr,const void* i_src_addr,unsigned long i_size); -unsigned long LPC17xx_IAP_erase(unsigned long i_start,unsigned long i_end); - -#ifdef __cplusplus -} -#endif /* __cplusplus */ - -#endif +#ifndef LPC17xx_IAP_h +#define LPC17xx_IAP_h + +#include +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +#define LPC17xx_IAP_TRUE (0==0) +#define LPC17xx_IAP_FALSE (0!=0) +//Command is executed successfully. +#define LPC17xx_IAP_CMD_SUCCESS 0 + +int LPC17xx_IAP_addr2Sector(const void* addr,unsigned long* o_sector); +int LPC17xx_IAP_sector2Addr(unsigned int i_sector,void** o_addr); + +unsigned long LPC17xx_IAP_getSectorSize(unsigned int i_sector); +unsigned long LPC17xx_IAP_prepare(unsigned long i_start,unsigned long i_end); +unsigned long LPC17xx_IAP_copyRam2Flash(const void* i_flash_addr,const void* i_src_addr,unsigned long i_size); +unsigned long LPC17xx_IAP_erase(unsigned long i_start,unsigned long i_end); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif diff --git a/lib/src/driver/flash/LPCxxx/NyLPC_cMiMicConfiglation_LPCxxx.c b/lib/src/driver/flash/LPCxxx/NyLPC_cMiMicConfiglation_LPCxxx.c new file mode 100644 index 0000000..046a2ec --- /dev/null +++ b/lib/src/driver/flash/LPCxxx/NyLPC_cMiMicConfiglation_LPCxxx.c @@ -0,0 +1,105 @@ +#include "NyLPC_config.h" +#if NyLPC_MCU==NyLPC_MCU_LPC4088 || NyLPC_MCU==NyLPC_MCU_LPC17xx +#include "NyLPC_flash.h" +#include "NyLPC_http.h" +#include "NyLPC_cOnchipFlashWriter.h" +#include "NyLPC_net.h" +/** + * Onchip flashを使ったコンフィギュレーション保存システムです。 + */ + + + + +#define IP2Int(a0,a1,a2,a3) ((0xff000000&(((NyLPC_TUInt32)(a0))<<24))|(0x00ff0000&(((NyLPC_TUInt32)(a1))<<16))|(0x0000ff00&(((NyLPC_TUInt32)(a2))<<8))|(0x000000ff&(((NyLPC_TUInt32)(a3))))) +/** + * コンフィギュレーションの保存セクタ + */ +#define MIMIC_CONFIGLATION_FLASH_SECTOR 29 +#define MIMIC_CONFIGLATION_FLASH_SECTOR_ADDR 0x00078000 + +/** + * コンフィギュレーション値はホストオーダーで保存する。 + */ +const struct NyLPC_TMiMicConfigulation factory_default= +{ + 0xffffffff, //fastboot + "MiMic020102030405", + 0x02010203,0x0405ffff, //Mac addr + //IPv4 + NyLPC_TcNetConfig_IPV4_FLAG_MODE_MANUAL, //flags + IP2Int(192,168,0,39), + IP2Int(255,255,255,0), + IP2Int(192,168,0,254), + //ServerFlags + NyLPC_TcNetConfig_SERVICE_FLAG_MDNS|NyLPC_TcNetConfig_SERVICE_FLAG_UPNP, + //HTTP + 80, //HTTP-Port + 0 //padding +}; + + + +const static NyLPC_TUInt32 FAST_BOOT_DATA=0xfffffffe; +/** + * ユーザコンフィギュレーションを更新する。 + * この関数をコールするときは、割込/FreeRTOSを一時停止すること。 + */ +NyLPC_TBool NyLPC_cMiMicConfiglation_updateConfigulation(const struct NyLPC_TMiMicConfigulation* i_congfiglation) +{ + const NyLPC_TUInt32* volatile fast_boot=&(factory_default.fast_boot); + //イレース + if(!NyLPC_cOnchipFlashWriter_elase(MIMIC_CONFIGLATION_FLASH_SECTOR,MIMIC_CONFIGLATION_FLASH_SECTOR)){ + NyLPC_OnErrorGoto(Error); + } + //コンフィギュレーションを書き込む。 + if(!NyLPC_cOnchipFlashWriter_writeSector(MIMIC_CONFIGLATION_FLASH_SECTOR,0x00000000,i_congfiglation,sizeof(struct NyLPC_TMiMicConfigulation))){ + NyLPC_OnErrorGoto(Error); + } + //プログラム済フラッシュの一部を書き換えてユーザコンフィギュレーションをONにする。 + if(*fast_boot==0xffffffff){ + //フラグ値のアドレスが4バイトアライメントにあるFlashメモリか確認する。 + if(((NyLPC_TUInt32)fast_boot)%4==0 && (!NyLPC_cOnchipFlashWriter_isOnchipFlash(fast_boot))){ + //書き込み + NyLPC_cOnchipFlashWriter_write(fast_boot,&FAST_BOOT_DATA,4); + }else{ + NyLPC_OnErrorGoto(Error); + } + } + return NyLPC_TBool_TRUE; +Error: + return NyLPC_TBool_FALSE; +} +/** + * コンフィギュレーション値を返す。 + */ +const struct NyLPC_TMiMicConfigulation* NyLPC_cMiMicConfiglation_loadFromFlash(void) +{ + if(NyLPC_cMiMicConfiglation_hasUserConfigulation()){ + //userコンフィギュレーション読むよ + return (const struct NyLPC_TMiMicConfigulation*)(MIMIC_CONFIGLATION_FLASH_SECTOR_ADDR); + }else{ + //Userコンフィギュレーションない + return &factory_default; + } +} +const struct NyLPC_TMiMicConfigulation* NyLPC_cMiMicConfiglation_loadFactoryDefault(void) +{ + return &factory_default; +} + + + + + +/** + * ユーザコンフィギュレーションが存在すると、true. + */ +NyLPC_TBool NyLPC_cMiMicConfiglation_hasUserConfigulation(void) +{ + volatile const NyLPC_TUInt32* fast_boot=&(factory_default.fast_boot); + return (*fast_boot)!=0xffffffff; +} + + +#endif diff --git a/lib/src/driver/flash/LPCxxx/NyLPC_cOnchipFlashWriter.c b/lib/src/driver/flash/LPCxxx/NyLPC_cOnchipFlashWriter.c new file mode 100644 index 0000000..be35080 --- /dev/null +++ b/lib/src/driver/flash/LPCxxx/NyLPC_cOnchipFlashWriter.c @@ -0,0 +1,131 @@ +#include "NyLPC_config.h" +#if NyLPC_MCU==NyLPC_MCU_LPC4088 || NyLPC_MCU==NyLPC_MCU_LPC17xx +#include "LPC17xx_IAP.h" +#include "NyLPC_cOnchipFlashWriter.h" + + +/** + * 指定したアドレスが、オンチップフラッシュかどうか + */ +NyLPC_TBool NyLPC_cOnchipFlashWriter_isOnchipFlash(const void* i_addr) +{ + unsigned long snum; + return LPC17xx_IAP_addr2Sector(i_addr,&snum)==LPC17xx_IAP_CMD_SUCCESS; +} + +NyLPC_TUInt8 _work[256]; + + +NyLPC_TBool NyLPC_cOnchipFlashWriter_write(const void* i_dest,const void* i_src,NyLPC_TUInt32 i_size) +{ + NyLPC_TUInt32 size; + const char* src; + NyLPC_TUInt32 snum; + const char* fblock_addr; + const char* dest_addr; + NyLPC_TUInt32 wsize; + NyLPC_TUInt16 s_padding; + NyLPC_TUInt16 free_size; + if(i_size%4!=0){ + NyLPC_OnErrorGoto(Error); + } + if(((NyLPC_TUInt32)i_dest)%4!=0){ + NyLPC_OnErrorGoto(Error); + } + size=i_size; + src=(const char*)i_src; + dest_addr=(const char*)i_dest; + for(;size>0;){ + //開始位置の端数を調べる + s_padding=((NyLPC_TUInt32)dest_addr)%256; + //書き込みアドレス取得 + fblock_addr=dest_addr-s_padding; + //書込み可能サイズを計算 + free_size=256-s_padding; + //書込みサイズを決定 + wsize=(free_size>size)?size:free_size; + //Flashから一時RAMへ前方パディングを読む + if(s_padding>0){ + memcpy(_work,fblock_addr,s_padding); + } + //書き込むデータを一時RAMへ書き込む + memcpy(_work+s_padding,src,wsize); + //後半 + if(256-(wsize+s_padding)>0){ + memcpy(_work+s_padding+wsize,fblock_addr+(wsize+s_padding),256-(wsize+s_padding)); + } + + //Flashへ書込み + //開始セクタ + if(!LPC17xx_IAP_addr2Sector(fblock_addr,&snum)){ + NyLPC_OnErrorGoto(Error); + } + //IAPのprepareコマンド + + if(LPC17xx_IAP_CMD_SUCCESS!=LPC17xx_IAP_prepare(snum,snum)){ + NyLPC_OnErrorGoto(Error); + } + //IAPのwriteコマンド + if(LPC17xx_IAP_CMD_SUCCESS!=LPC17xx_IAP_copyRam2Flash(fblock_addr,_work,256)){ + NyLPC_OnErrorGoto(Error); + } + dest_addr+=wsize; + src+=wsize; + size-=wsize; + } + return NyLPC_TBool_TRUE; +Error: + return NyLPC_TBool_FALSE; +} + +/** + * セクタ+オフセット形式で、データを書き込みます。 + */ +NyLPC_TBool NyLPC_cOnchipFlashWriter_writeSector(NyLPC_TUInt16 i_sector,NyLPC_TUInt32 i_offset,const void* i_src,NyLPC_TUInt32 i_size) +{ + void* addr; + if(!LPC17xx_IAP_sector2Addr(i_sector,&addr)){ + return NyLPC_TBool_FALSE; + } + addr=(void*)((NyLPC_TUInt32)addr+i_offset); + return NyLPC_cOnchipFlashWriter_write(addr,i_src,i_size); + +} +/** + * FlashRomのセクタ番号Nにイレースを実行します。 + */ +NyLPC_TBool NyLPC_cOnchipFlashWriter_elase(NyLPC_TUInt16 i_sector_s,NyLPC_TUInt16 i_sector_e) +{ + if(LPC17xx_IAP_CMD_SUCCESS!=LPC17xx_IAP_prepare(i_sector_s,i_sector_e)){ + return NyLPC_TBool_FALSE; + } + if(LPC17xx_IAP_CMD_SUCCESS!=LPC17xx_IAP_erase(i_sector_s,i_sector_e)){ + return NyLPC_TBool_FALSE; + } + return NyLPC_TBool_TRUE; +} + + + +#ifdef TEST +#include "stdio.h" +unsigned long buf[128]={0x1,0x2,0x03,0x04,0x05}; +#define MIMIC_CONFIG_ADDR ((long*)(0x00018000+1280)) +void setup(void) +{ + NyLPC_TcFlashWriter_t writer; + NyLPC_cFlashWriter_initialize(&writer); + unsigned long p; + NyLPC_cFlashWriter_elase(&writer,29); +// NyLPC_cFlashWriter_write(&writer,MIMIC_CONFIG_ADDR-8,buf,5*4); + NyLPC_cFlashWriter_finalize(&writer); + return; +} +void loop(void) +{ + //Implementation + //ここにメインタスクを書きます。 + for(;;){} +} +#endif +#endif diff --git a/lib/src/flash/NyLPC_cOnchipFlashWriter.h b/lib/src/driver/flash/LPCxxx/NyLPC_cOnchipFlashWriter.h similarity index 97% rename from lib/src/flash/NyLPC_cOnchipFlashWriter.h rename to lib/src/driver/flash/LPCxxx/NyLPC_cOnchipFlashWriter.h index d0d5fb2..5f24ac4 100644 --- a/lib/src/flash/NyLPC_cOnchipFlashWriter.h +++ b/lib/src/driver/flash/LPCxxx/NyLPC_cOnchipFlashWriter.h @@ -1,34 +1,34 @@ -#ifndef cOnchipFlashWriter_h -#define cOnchipFlashWriter_h - - -#include "NyLPC_stdlib.h" -#ifdef __cplusplus -extern "C" { -#endif /* __cplusplus */ - - - -/** - * アドレスi_destに、i_srcの内容を書き込みます。 - * 書き込みは、FlashROMがイレース済なものとして実行します。書込み範囲以外のデータは、変更されません。 - * 既に書込み済みのデータがある場合(0xFFFFFFFF以外)は、期待した結果が得られないので、注意してください。 - * この関数は、IAPインタフェイス経由で256バイト単位でデータを書き込みます。 - * IAPは0x1000000から32バイトをワークエリアとして使用します。 - * 関数はワークエリアの待避と復帰を行いますが、安全の為、使用前にRTOSを一旦停止させてください。 - * この関数はリエントラントではありません。 - */ -NyLPC_TBool NyLPC_cOnchipFlashWriter_write(const void* i_dest,const void* i_src,NyLPC_TUInt32 i_size); -/** - * この関数はリエントラントではありません。 - */ -NyLPC_TBool NyLPC_cOnchipFlashWriter_writeSector(NyLPC_TUInt16 i_sector,NyLPC_TUInt32 i_offset,const void* i_src,NyLPC_TUInt32 i_size); -NyLPC_TBool NyLPC_cOnchipFlashWriter_elase(NyLPC_TUInt16 i_sector_s,NyLPC_TUInt16 i_sector_e); -NyLPC_TBool NyLPC_cOnchipFlashWriter_isOnchipFlash(const void* i_addr); - - -#ifdef __cplusplus -} -#endif /* __cplusplus */ - -#endif +#ifndef cOnchipFlashWriter_h +#define cOnchipFlashWriter_h + + +#include "NyLPC_stdlib.h" +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + + + +/** + * アドレスi_destに、i_srcの内容を書き込みます。 + * 書き込みは、FlashROMがイレース済なものとして実行します。書込み範囲以外のデータは、変更されません。 + * 既に書込み済みのデータがある場合(0xFFFFFFFF以外)は、期待した結果が得られないので、注意してください。 + * この関数は、IAPインタフェイス経由で256バイト単位でデータを書き込みます。 + * IAPは0x1000000から32バイトをワークエリアとして使用します。 + * 関数はワークエリアの待避と復帰を行いますが、安全の為、使用前にRTOSを一旦停止させてください。 + * この関数はリエントラントではありません。 + */ +NyLPC_TBool NyLPC_cOnchipFlashWriter_write(const void* i_dest,const void* i_src,NyLPC_TUInt32 i_size); +/** + * この関数はリエントラントではありません。 + */ +NyLPC_TBool NyLPC_cOnchipFlashWriter_writeSector(NyLPC_TUInt16 i_sector,NyLPC_TUInt32 i_offset,const void* i_src,NyLPC_TUInt32 i_size); +NyLPC_TBool NyLPC_cOnchipFlashWriter_elase(NyLPC_TUInt16 i_sector_s,NyLPC_TUInt16 i_sector_e); +NyLPC_TBool NyLPC_cOnchipFlashWriter_isOnchipFlash(const void* i_addr); + + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif diff --git a/lib/src/driver/flash/k64f/K64F_IAP.c b/lib/src/driver/flash/k64f/K64F_IAP.c new file mode 100644 index 0000000..23d52d2 --- /dev/null +++ b/lib/src/driver/flash/k64f/K64F_IAP.c @@ -0,0 +1,225 @@ +/** + * Copyright 2014 MiMicProject + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/** + * This file based on https://mbed.org/users/Sissors/code/FreescaleIAP/ + */ +#include "NyLPC_config.h" +#if NyLPC_MCU==NyLPC_MCU_K64F +#include "K64F_IAP.h" +//For K64F +# include "MK64F12.h" +# define USE_ProgramPhrase 1 +# define FTFA FTFE +# define FTFA_FSTAT_FPVIOL_MASK FTFE_FSTAT_FPVIOL_MASK +# define FTFA_FSTAT_ACCERR_MASK FTFE_FSTAT_ACCERR_MASK +# define FTFA_FSTAT_RDCOLERR_MASK FTFE_FSTAT_RDCOLERR_MASK +# define FTFA_FSTAT_CCIF_MASK FTFE_FSTAT_CCIF_MASK +# define FTFA_FSTAT_MGSTAT0_MASK FTFE_FSTAT_MGSTAT0_MASK + +enum FCMD { + Read1s = 0x01, + ProgramCheck = 0x02, + ReadResource = 0x03, + ProgramLongword = 0x06, + ProgramPhrase = 0x07, + EraseSector = 0x09, + Read1sBlock = 0x40, + ReadOnce = 0x41, + ProgramOnce = 0x43, + EraseAll = 0x44, + VerifyBackdoor = 0x45 + }; + + +#define INT_FALSE (0!=0) +#define INT_TRUE (0==0) + +static inline void run_command(void); +//static int check_boundary(int address, unsigned int length); +static int check_align(int address); + +static K64F_IAP_TIAPCode verify_erased(int address, unsigned int length); +static K64F_IAP_TIAPCode check_error(void); +static K64F_IAP_TIAPCode program_word(int address, char *data); + +K64F_IAP_TIAPCode K64F_IAP_erase_sector(int address) { + #ifdef IAPDEBUG + printf("IAP: Erasing at %x\r\n", address); + #endif + if (check_align(address)) + return K64F_IAP_TIAPCode_AlignError; + + //Setup command + FTFA->FCCOB0 = EraseSector; + FTFA->FCCOB1 = (address >> 16) & 0xFF; + FTFA->FCCOB2 = (address >> 8) & 0xFF; + FTFA->FCCOB3 = address & 0xFF; + + run_command(); + + return check_error(); +} + +K64F_IAP_TIAPCode K64F_IAP_program_flash(int address, char *data, unsigned int length) { + #ifdef IAPDEBUG + printf("IAP: Programming flash at %x with length %d\r\n", address, length); + #endif + if (check_align(address)) + return K64F_IAP_TIAPCode_AlignError; + + K64F_IAP_TIAPCode eraseCheck = verify_erased(address, length); + if (eraseCheck != K64F_IAP_TIAPCode_Success) + return eraseCheck; + + K64F_IAP_TIAPCode progResult; + for (int i = 0; i < length; i+=8) { + progResult = program_word(address + i, data + i); + if (progResult != K64F_IAP_TIAPCode_Success) + return progResult; + } + return K64F_IAP_TIAPCode_Success; +} + +unsigned int K64F_IAP_flash_size(void) { + unsigned int retval = (SIM->FCFG2 & 0x7F000000u) >> (24-13); + if (SIM->FCFG2 & (1<<23)) //Possible second flash bank + retval += (SIM->FCFG2 & 0x007F0000u) >> (16-13); + return retval; +} + +static K64F_IAP_TIAPCode program_word(int address, char *data) { + #ifdef IAPDEBUG + printf("IAP: Programming word at %x, %d - %d - %d - %d - %d - %d - %d - %d\r\n", address, data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7]); + #endif + if (check_align(address)){ + return K64F_IAP_TIAPCode_AlignError; + } + FTFA->FCCOB0 = ProgramPhrase; + FTFA->FCCOB1 = (address >> 16) & 0xFF; + FTFA->FCCOB2 = (address >> 8) & 0xFF; + FTFA->FCCOB3 = address & 0xFF; + FTFA->FCCOB4 = data[3]; + FTFA->FCCOB5 = data[2]; + FTFA->FCCOB6 = data[1]; + FTFA->FCCOB7 = data[0]; + FTFA->FCCOB8 = data[7]; + FTFA->FCCOB9 = data[6]; + FTFA->FCCOBA = data[5]; + FTFA->FCCOBB = data[4]; + run_command(); + return check_error(); +} + +/* Clear possible flags which are set, run command, wait until done */ +static inline void run_command(void) { + //Clear possible old errors, start command, wait until done + __disable_irq(); //Disable IRQs, preventing IRQ routines from trying to access flash (thanks to https://mbed.org/users/mjr/) + FTFA->FSTAT = FTFA_FSTAT_FPVIOL_MASK | FTFA_FSTAT_ACCERR_MASK | FTFA_FSTAT_RDCOLERR_MASK; + FTFA->FSTAT = FTFA_FSTAT_CCIF_MASK; + while (!(FTFA->FSTAT & FTFA_FSTAT_CCIF_MASK)); + __enable_irq(); +} + + + +/* Check if no flash boundary is violated + Returns true on violation *//* +static int check_boundary(int address, unsigned int length) { + int temp = (address+length - 1) / K64F_IAP_SECTOR_SIZE; + address /= K64F_IAP_SECTOR_SIZE; + int retval = (address != temp); + #ifdef IAPDEBUG + if (retval) + printf("IAP: Boundary violation\r\n"); + #endif + return retval; +}*/ + +/* Check if address is correctly aligned + Returns true on violation */ +static int check_align(int address) { + int retval = address & 0x03; + #ifdef IAPDEBUG + if (retval) + printf("IAP: Alignment violation\r\n"); + #endif + return retval; +} + +/* Check if an area of flash memory is erased + Returns error code or Success (in case of fully erased) */ +static K64F_IAP_TIAPCode verify_erased(int address, unsigned int length) { + #ifdef IAPDEBUG + printf("IAP: Verify erased at %x with length %d\r\n", address, length); + #endif + + if (check_align(address)){ + return K64F_IAP_TIAPCode_AlignError; + } + + //Setup command + FTFA->FCCOB0 = Read1s; + FTFA->FCCOB1 = (address >> 16) & 0xFF; + FTFA->FCCOB2 = (address >> 8) & 0xFF; + FTFA->FCCOB3 = address & 0xFF; + FTFA->FCCOB4 = (length >> 10) & 0xFF; + FTFA->FCCOB5 = (length >> 2) & 0xFF; + FTFA->FCCOB6 = 0; + + run_command(); + + K64F_IAP_TIAPCode retval = check_error(); + if (retval == K64F_IAP_TIAPCode_RuntimeError) { + #ifdef IAPDEBUG + printf("IAP: Flash was not erased\r\n"); + #endif + return K64F_IAP_TIAPCode_EraseError; + } + return retval; +} + +/* Check if an error occured + Returns error code or Success*/ +static K64F_IAP_TIAPCode check_error(void) { + if (FTFA->FSTAT & FTFA_FSTAT_FPVIOL_MASK) { + #ifdef IAPDEBUG + printf("IAP: Protection violation\r\n"); + #endif + return K64F_IAP_TIAPCode_ProtectionError; + } + if (FTFA->FSTAT & FTFA_FSTAT_ACCERR_MASK) { + #ifdef IAPDEBUG + printf("IAP: Flash access error\r\n"); + #endif + return K64F_IAP_TIAPCode_AccessError; + } + if (FTFA->FSTAT & FTFA_FSTAT_RDCOLERR_MASK) { + #ifdef IAPDEBUG + printf("IAP: Collision error\r\n"); + #endif + return K64F_IAP_TIAPCode_CollisionError; + } + if (FTFA->FSTAT & FTFA_FSTAT_MGSTAT0_MASK) { + #ifdef IAPDEBUG + printf("IAP: Runtime error\r\n"); + #endif + return K64F_IAP_TIAPCode_RuntimeError; + } + #ifdef IAPDEBUG + printf("IAP: No error reported\r\n"); + #endif + return K64F_IAP_TIAPCode_Success; +} +#endif diff --git a/lib/src/driver/flash/k64f/K64F_IAP.h b/lib/src/driver/flash/k64f/K64F_IAP.h new file mode 100644 index 0000000..c27696a --- /dev/null +++ b/lib/src/driver/flash/k64f/K64F_IAP.h @@ -0,0 +1,73 @@ +/** + * Copyright 2014 MiMicProject + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/** + * This file based on https://mbed.org/users/Sissors/code/FreescaleIAP/ + */ +#ifndef K64F_IAP_h +#define K64F_IAP_h +#include "NyLPC_config.h" +#if NyLPC_MCU==NyLPC_MCU_K64F +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ +#define K64F_IAP_SECTOR_SIZE 4096 + +typedef int K64F_IAP_TIAPCode; + +#define K64F_IAP_TIAPCode_BoundaryError -99 //Commands may not span several sectors +#define K64F_IAP_TIAPCode_AlignError -98 //Data must be aligned on longword (two LSBs zero) +#define K64F_IAP_TIAPCode_ProtectionError -97 //Flash sector is protected +#define K64F_IAP_TIAPCode_AccessError -96 //Something went wrong +#define K64F_IAP_TIAPCode_CollisionError -95 //During writing something tried to flash which was written to +#define K64F_IAP_TIAPCode_LengthError -94 //The length must be multiples of 4 +#define K64F_IAP_TIAPCode_RuntimeError -93 +#define K64F_IAP_TIAPCode_EraseError -92 //The flash was not erased before writing to it +#define K64F_IAP_TIAPCode_Success 0 + + +/** Erase a flash sector + * + * The size erased depends on the used device + * + * @param address address in the sector which needs to be erased + * @param return Success if no errors were encountered, otherwise one of the error states + */ +K64F_IAP_TIAPCode K64F_IAP_erase_sector(int address); + +/** Program flash + * + * Before programming the used area needs to be erased. The erase state is checked + * before programming, and will return an error if not erased. + * + * @param address starting address where the data needs to be programmed (must be longword alligned: two LSBs must be zero) + * @param data pointer to array with the data to program + * @param length number of bytes to program (must be a multiple of 4. must be a multiple of 8 when K64F) + * @param return Success if no errors were encountered, otherwise one of the error states + */ +K64F_IAP_TIAPCode K64F_IAP_program_flash(int address, char *data, unsigned int length); + +/** + * Returns size of flash memory + * + * This is the first address which is not flash + * + * @param return length of flash memory in bytes + */ +unsigned int K64F_IAP_flash_size(void); +#ifdef __cplusplus +} +#endif /* __cplusplus */ +#endif +#endif diff --git a/lib/src/driver/flash/k64f/NyLPC_cMiMicConfiglation_K64F.c b/lib/src/driver/flash/k64f/NyLPC_cMiMicConfiglation_K64F.c new file mode 100644 index 0000000..52fab35 --- /dev/null +++ b/lib/src/driver/flash/k64f/NyLPC_cMiMicConfiglation_K64F.c @@ -0,0 +1,113 @@ +#include "NyLPC_config.h" +#if NyLPC_MCU==NyLPC_MCU_K64F +#include "NyLPC_stdlib.h" +#include "NyLPC_flash.h" +#include "NyLPC_http.h" +#include "NyLPC_net.h" +#include "K64F_IAP.h" +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ +/** + * Onchip flashを使ったコンフィギュレーション保存システムです。 + * K64Fの0x0080000に構造体NyLPC_TMiMicConfigulationを保存します。 + */ + + + + +#define IP2Int(a0,a1,a2,a3) ((0xff000000&(((NyLPC_TUInt32)(a0))<<24))|(0x00ff0000&(((NyLPC_TUInt32)(a1))<<16))|(0x0000ff00&(((NyLPC_TUInt32)(a2))<<8))|(0x000000ff&(((NyLPC_TUInt32)(a3))))) +/** + * コンフィギュレーションの保存セクタ + */ +#define MIMIC_CONFIGLATION_FLASH_ADDR ((void*)0x00080000) + +/** + * コンフィギュレーション値はホストオーダーで保存する。 + */ +const struct NyLPC_TMiMicConfigulation factory_default= +{ + 0xffffffff, //fastboot + "MiMic020102030405", + 0x02010203,0x0405ffff, //Mac addr + //IPv4 + NyLPC_TcNetConfig_IPV4_FLAG_MODE_MANUAL, //flags + IP2Int(192,168,0,39), + IP2Int(255,255,255,0), + IP2Int(192,168,0,254), + //ServerFlags + NyLPC_TcNetConfig_SERVICE_FLAG_MDNS|NyLPC_TcNetConfig_SERVICE_FLAG_UPNP, + //HTTP + 80, //HTTP-Port + 0 //padding +}; + + +#define FAST_BOOT_DATA 0xfffffffe +/** + * ユーザコンフィギュレーションを更新する。 + * この関数をコールするときは、割込/FreeRTOSを一時停止すること。 + */ +NyLPC_TBool NyLPC_cMiMicConfiglation_updateConfigulation(const struct NyLPC_TMiMicConfigulation* i_congfiglation) +{ + struct NyLPC_TMiMicConfigulation d; + d.fast_boot=FAST_BOOT_DATA; + memcpy(d.hostname,i_congfiglation->hostname,NyLPC_TcNetConfig_HOSTNAME_LEN); + d.mac_00_01_02_03=i_congfiglation->mac_00_01_02_03; + d.mac_04_05_xx_xx=i_congfiglation->mac_04_05_xx_xx; + d.ipv4_flags=i_congfiglation->ipv4_flags; + d.ipv4_addr_net=i_congfiglation->ipv4_addr_net; + d.ipv4_mask_net=i_congfiglation->ipv4_mask_net; + d.ipv4_drut_net=i_congfiglation->ipv4_drut_net; + d.srv_flags=i_congfiglation->srv_flags; + d.http_port=i_congfiglation->http_port; + d.padding32=0xffff; + d.padding64=0xffffffff; + + + if(K64F_IAP_erase_sector((int)MIMIC_CONFIGLATION_FLASH_ADDR)!=K64F_IAP_TIAPCode_Success){ + NyLPC_OnErrorGoto(Error); + } + if(K64F_IAP_program_flash((int)MIMIC_CONFIGLATION_FLASH_ADDR,(char*)(&d), sizeof(struct NyLPC_TMiMicConfigulation))!=K64F_IAP_TIAPCode_Success){ + NyLPC_OnErrorGoto(Error); + } + return NyLPC_TBool_TRUE; +Error: + return NyLPC_TBool_FALSE; +} +/** + * コンフィギュレーション値を返す。 + */ +const struct NyLPC_TMiMicConfigulation* NyLPC_cMiMicConfiglation_loadFromFlash(void) +{ + if(NyLPC_cMiMicConfiglation_hasUserConfigulation()){ + //userコンフィギュレーション読むよ + return (const struct NyLPC_TMiMicConfigulation*)(MIMIC_CONFIGLATION_FLASH_ADDR); + }else{ + //Userコンフィギュレーションない + return &factory_default; + } +} +const struct NyLPC_TMiMicConfigulation* NyLPC_cMiMicConfiglation_loadFactoryDefault(void) +{ + return &factory_default; +} + + + + + +/** + * ユーザコンフィギュレーションが存在すると、true. + */ +NyLPC_TBool NyLPC_cMiMicConfiglation_hasUserConfigulation(void) +{ + //初回読出しはFlashにFFFFFFFFが格納されているのを期待する。 + volatile const NyLPC_TUInt32* fast_boot=((NyLPC_TUInt32*)MIMIC_CONFIGLATION_FLASH_ADDR); + return (*fast_boot)!=0xffffffff; +} + +#ifdef __cplusplus +} +#endif /* __cplusplus */ +#endif diff --git a/lib/src/driver/irq/LPC1768_IRQ.c b/lib/src/driver/irq/LPC1768_IRQ.c deleted file mode 100644 index 772eb5f..0000000 --- a/lib/src/driver/irq/LPC1768_IRQ.c +++ /dev/null @@ -1,71 +0,0 @@ -extern void _vStackTop(void); - -static void IntDefaultHandler(void); - -//static void (* const g_pfnVectors[])(void) = -//{ -// // Core Level - CM3 -// (void *)&_vStackTop, // The initial stack pointer -// ResetISR, // The reset handler -// IntDefaultHandler, // The NMI handler -// IntDefaultHandler, // The hard fault handler -// IntDefaultHandler, // The MPU fault handler -// IntDefaultHandler, // The bus fault handler -// IntDefaultHandler, // The usage fault handler -// 0, // Reserved -// 0, // Reserved -// 0, // Reserved -// 0, // Reserved -// vPortSVCHandler, // SVCall handler -// IntDefaultHandler, // Debug monitor handler -// 0, // Reserved -// xPortPendSVHandler, // The PendSV handler -// xPortSysTickHandler, // The SysTick handler -// -// // Chip Level - LPC17 -// IntDefaultHandler, // 16, 0x40 - WDT -// IntDefaultHandler, // 17, 0x44 - TIMER0 -// IntDefaultHandler, // 18, 0x48 - TIMER1 -// IntDefaultHandler, // 19, 0x4c - TIMER2 -// IntDefaultHandler, // 20, 0x50 - TIMER3 -// IntDefaultHandler, // 21, 0x54 - UART0 -// IntDefaultHandler, // 22, 0x58 - UART1 -// IntDefaultHandler, // 23, 0x5c - UART2 -// IntDefaultHandler, // 24, 0x60 - UART3 -// IntDefaultHandler, // 25, 0x64 - PWM1 -// IntDefaultHandler, // 26, 0x68 - I2C0 -// IntDefaultHandler, // 27, 0x6c - I2C1 -// IntDefaultHandler, // 28, 0x70 - I2C2 -// IntDefaultHandler, // 29, 0x74 - SPI -// IntDefaultHandler, // 30, 0x78 - SSP0 -// IntDefaultHandler, // 31, 0x7c - SSP1 -// IntDefaultHandler, // 32, 0x80 - PLL0 (Main PLL) -// IntDefaultHandler, // 33, 0x84 - RTC -// IntDefaultHandler, // 34, 0x88 - EINT0 -// IntDefaultHandler, // 35, 0x8c - EINT1 -// IntDefaultHandler, // 36, 0x90 - EINT2 -// IntDefaultHandler, // 37, 0x94 - EINT3 -// IntDefaultHandler, // 38, 0x98 - ADC -// IntDefaultHandler, // 39, 0x9c - BOD -// IntDefaultHandler, // 40, 0xA0 - USB -// IntDefaultHandler, // 41, 0xa4 - CAN -// IntDefaultHandler, // 42, 0xa8 - GP DMA -// IntDefaultHandler, // 43, 0xac - I2S -// vEMAC_ISR, // Ethernet. -// IntDefaultHandler, // 45, 0xb4 - RITINT -// IntDefaultHandler, // 46, 0xb8 - Motor Control PWM -// IntDefaultHandler, // 47, 0xbc - Quadrature Encoder -// IntDefaultHandler, // 48, 0xc0 - PLL1 (USB PLL) -// IntDefaultHandler, // 49, 0xc4 - USB Activity interrupt to wakeup -// IntDefaultHandler, // 50, 0xc8 - CAN Activity interrupt to wakeup -//}; - -static void IntDefaultHandler(void) -{ - // - // Go into an infinite loop. - // - while(1) - { - } -} diff --git a/lib/src/flash/NyLPC_cMiMicConfiglation.c b/lib/src/flash/NyLPC_cMiMicConfiglation.c deleted file mode 100644 index fc8510b..0000000 --- a/lib/src/flash/NyLPC_cMiMicConfiglation.c +++ /dev/null @@ -1,102 +0,0 @@ -#include "NyLPC_flash.h" -#include "NyLPC_http.h" -#include "NyLPC_cMiMicConfiglation.h" -#include "NyLPC_net.h" -/** - * Onchip flashを使ったコンフィギュレーション保存システムです。 - */ - - - - -#define IP2Int(a0,a1,a2,a3) ((0xff000000&(((NyLPC_TUInt32)(a0))<<24))|(0x00ff0000&(((NyLPC_TUInt32)(a1))<<16))|(0x0000ff00&(((NyLPC_TUInt32)(a2))<<8))|(0x000000ff&(((NyLPC_TUInt32)(a3))))) -/** - * コンフィギュレーションの保存セクタ - */ -#define MIMIC_CONFIGLATION_FLASH_SECTOR 29 -#define MIMIC_CONFIGLATION_FLASH_SECTOR_ADDR 0x00078000 - -/** - * コンフィギュレーション値はホストオーダーで保存する。 - */ -const struct NyLPC_TMiMicConfigulation factory_default= -{ - 0xffffffff, //fastboot - "MiMic020102030405", - 0x02010203,0x0405ffff, //Mac addr - //IPv4 - NyLPC_TcNetConfig_IPV4_FLAG_MODE_MANUAL, //flags - IP2Int(192,168,0,39), - IP2Int(255,255,255,0), - IP2Int(192,168,0,254), - //ServerFlags - NyLPC_TcNetConfig_SERVICE_FLAG_MDNS|NyLPC_TcNetConfig_SERVICE_FLAG_UPNP, - //HTTP - 80, //HTTP-Port - 0 //padding -}; - - - -const static NyLPC_TUInt32 FAST_BOOT_DATA=0xfffffffe; -/** - * ユーザコンフィギュレーションを更新する。 - * この関数をコールするときは、割込/FreeRTOSを一時停止すること。 - */ -NyLPC_TBool NyLPC_cMiMicConfiglation_updateConfigulation(const struct NyLPC_TMiMicConfigulation* i_congfiglation) -{ - const NyLPC_TUInt32* volatile fast_boot=&(factory_default.fast_boot); - //イレース - if(!NyLPC_cOnchipFlashWriter_elase(MIMIC_CONFIGLATION_FLASH_SECTOR,MIMIC_CONFIGLATION_FLASH_SECTOR)){ - NyLPC_OnErrorGoto(Error); - } - //コンフィギュレーションを書き込む。 - if(!NyLPC_cOnchipFlashWriter_writeSector(MIMIC_CONFIGLATION_FLASH_SECTOR,0x00000000,i_congfiglation,sizeof(struct NyLPC_TMiMicConfigulation))){ - NyLPC_OnErrorGoto(Error); - } - //プログラム済フラッシュの一部を書き換えてユーザコンフィギュレーションをONにする。 - if(*fast_boot==0xffffffff){ - //フラグ値のアドレスが4バイトアライメントにあるFlashメモリか確認する。 - if(((NyLPC_TUInt32)fast_boot)%4==0 && (!NyLPC_cOnchipFlashWriter_isOnchipFlash(fast_boot))){ - //書き込み - NyLPC_cOnchipFlashWriter_write(fast_boot,&FAST_BOOT_DATA,4); - }else{ - NyLPC_OnErrorGoto(Error); - } - } - return NyLPC_TBool_TRUE; -Error: - return NyLPC_TBool_FALSE; -} -/** - * コンフィギュレーション値を返す。 - */ -const struct NyLPC_TMiMicConfigulation* NyLPC_cMiMicConfiglation_loadFromFlash(void) -{ - if(NyLPC_cMiMicConfiglation_hasUserConfigulation()){ - //userコンフィギュレーション読むよ - return (const struct NyLPC_TMiMicConfigulation*)(MIMIC_CONFIGLATION_FLASH_SECTOR_ADDR); - }else{ - //Userコンフィギュレーションない - return &factory_default; - } -} -const struct NyLPC_TMiMicConfigulation* NyLPC_cMiMicConfiglation_loadFactoryDefault(void) -{ - return &factory_default; -} - - - - - -/** - * ユーザコンフィギュレーションが存在すると、true. - */ -NyLPC_TBool NyLPC_cMiMicConfiglation_hasUserConfigulation(void) -{ - volatile const NyLPC_TUInt32* fast_boot=&(factory_default.fast_boot); - return (*fast_boot)!=0xffffffff; -} - - diff --git a/lib/src/flash/NyLPC_cMiMicConfiglation.h b/lib/src/flash/NyLPC_cMiMicConfiglation.h index 8f39f9e..0c0ca62 100644 --- a/lib/src/flash/NyLPC_cMiMicConfiglation.h +++ b/lib/src/flash/NyLPC_cMiMicConfiglation.h @@ -17,7 +17,7 @@ extern "C" { /** * IPアドレスはネットワークオーダーで格納する。 - * 構造体は4バイトアライメントであること。 + * 構造体は8バイトアライメントであること。 */ struct NyLPC_TMiMicConfigulation{ /** ROM焼検出用。0xFFFFFFFFを書く */ @@ -27,20 +27,20 @@ struct NyLPC_TMiMicConfigulation{ /** MACアドレスの下位4bit*/ NyLPC_TUInt32 mac_00_01_02_03; /** MACアドレスの上位2bit*/ - NyLPC_TUInt32 mac_04_05_xx_xx; + NyLPC_TUInt32 mac_04_05_xx_xx; //12+NyLPC_TcNetConfig_HOSTNAME_LEN /* * IPv4設定 */ /** * 0-1bit 起動モード - * 0:default,1:DHCP,2:AUTOIP,3:APIPA + * 0:default,1:DHCP,2:AUTOIP,3:APIPA */ NyLPC_TUInt32 ipv4_flags; /** IPV4アドレス*/ NyLPC_TUInt32 ipv4_addr_net; NyLPC_TUInt32 ipv4_mask_net; - NyLPC_TUInt32 ipv4_drut_net; + NyLPC_TUInt32 ipv4_drut_net; //16 /* * Service setting @@ -52,9 +52,10 @@ struct NyLPC_TMiMicConfigulation{ */ NyLPC_TUInt32 srv_flags; /** HTTPポート番号*/ - NyLPC_TUInt16 http_port; - NyLPC_TUInt16 padding; - + NyLPC_TUInt16 http_port; //6 + //total=(34=(12+16+6)+NyLPC_TcNetConfig_HOSTNAME_LEN) + NyLPC_TUInt16 padding32; + NyLPC_TUInt32 padding64; //total=40+NyLPC_TcNetConfig_HOSTNAME_LEN }; /** diff --git a/lib/src/flash/NyLPC_cOnchipFlashWriter.c b/lib/src/flash/NyLPC_cOnchipFlashWriter.c deleted file mode 100644 index 5fde03f..0000000 --- a/lib/src/flash/NyLPC_cOnchipFlashWriter.c +++ /dev/null @@ -1,128 +0,0 @@ -#include "../driver/flash/LPC17xx_IAP.h" -#include "NyLPC_cOnchipFlashWriter.h" - - -/** - * 指定したアドレスが、オンチップフラッシュかどうか - */ -NyLPC_TBool NyLPC_cOnchipFlashWriter_isOnchipFlash(const void* i_addr) -{ - unsigned long snum; - return LPC17xx_IAP_addr2Sector(i_addr,&snum)==LPC17xx_IAP_CMD_SUCCESS; -} - -NyLPC_TUInt8 _work[256]; - - -NyLPC_TBool NyLPC_cOnchipFlashWriter_write(const void* i_dest,const void* i_src,NyLPC_TUInt32 i_size) -{ - NyLPC_TUInt32 size; - const char* src; - NyLPC_TUInt32 snum; - const char* fblock_addr; - const char* dest_addr; - NyLPC_TUInt32 wsize; - NyLPC_TUInt16 s_padding; - NyLPC_TUInt16 free_size; - if(i_size%4!=0){ - NyLPC_OnErrorGoto(Error); - } - if(((NyLPC_TUInt32)i_dest)%4!=0){ - NyLPC_OnErrorGoto(Error); - } - size=i_size; - src=(const char*)i_src; - dest_addr=(const char*)i_dest; - for(;size>0;){ - //開始位置の端数を調べる - s_padding=((NyLPC_TUInt32)dest_addr)%256; - //書き込みアドレス取得 - fblock_addr=dest_addr-s_padding; - //書込み可能サイズを計算 - free_size=256-s_padding; - //書込みサイズを決定 - wsize=(free_size>size)?size:free_size; - //Flashから一時RAMへ前方パディングを読む - if(s_padding>0){ - memcpy(_work,fblock_addr,s_padding); - } - //書き込むデータを一時RAMへ書き込む - memcpy(_work+s_padding,src,wsize); - //後半 - if(256-(wsize+s_padding)>0){ - memcpy(_work+s_padding+wsize,fblock_addr+(wsize+s_padding),256-(wsize+s_padding)); - } - - //Flashへ書込み - //開始セクタ - if(!LPC17xx_IAP_addr2Sector(fblock_addr,&snum)){ - NyLPC_OnErrorGoto(Error); - } - //IAPのprepareコマンド - - if(LPC17xx_IAP_CMD_SUCCESS!=LPC17xx_IAP_prepare(snum,snum)){ - NyLPC_OnErrorGoto(Error); - } - //IAPのwriteコマンド - if(LPC17xx_IAP_CMD_SUCCESS!=LPC17xx_IAP_copyRam2Flash(fblock_addr,_work,256)){ - NyLPC_OnErrorGoto(Error); - } - dest_addr+=wsize; - src+=wsize; - size-=wsize; - } - return NyLPC_TBool_TRUE; -Error: - return NyLPC_TBool_FALSE; -} - -/** - * セクタ+オフセット形式で、データを書き込みます。 - */ -NyLPC_TBool NyLPC_cOnchipFlashWriter_writeSector(NyLPC_TUInt16 i_sector,NyLPC_TUInt32 i_offset,const void* i_src,NyLPC_TUInt32 i_size) -{ - void* addr; - if(!LPC17xx_IAP_sector2Addr(i_sector,&addr)){ - return NyLPC_TBool_FALSE; - } - addr=(void*)((NyLPC_TUInt32)addr+i_offset); - return NyLPC_cOnchipFlashWriter_write(addr,i_src,i_size); - -} -/** - * FlashRomのセクタ番号Nにイレースを実行します。 - */ -NyLPC_TBool NyLPC_cOnchipFlashWriter_elase(NyLPC_TUInt16 i_sector_s,NyLPC_TUInt16 i_sector_e) -{ - if(LPC17xx_IAP_CMD_SUCCESS!=LPC17xx_IAP_prepare(i_sector_s,i_sector_e)){ - return NyLPC_TBool_FALSE; - } - if(LPC17xx_IAP_CMD_SUCCESS!=LPC17xx_IAP_erase(i_sector_s,i_sector_e)){ - return NyLPC_TBool_FALSE; - } - return NyLPC_TBool_TRUE; -} - - - -#ifdef TEST -#include "stdio.h" -unsigned long buf[128]={0x1,0x2,0x03,0x04,0x05}; -#define MIMIC_CONFIG_ADDR ((long*)(0x00018000+1280)) -void setup(void) -{ - NyLPC_TcFlashWriter_t writer; - NyLPC_cFlashWriter_initialize(&writer); - unsigned long p; - NyLPC_cFlashWriter_elase(&writer,29); -// NyLPC_cFlashWriter_write(&writer,MIMIC_CONFIG_ADDR-8,buf,5*4); - NyLPC_cFlashWriter_finalize(&writer); - return; -} -void loop(void) -{ - //Implementation - //ここにメインタスクを書きます。 - for(;;){} -} -#endif diff --git a/lib/src/http/NyLPC_cBase64.c b/lib/src/http/NyLPC_cBase64.c index df91d96..15c0d69 100644 --- a/lib/src/http/NyLPC_cBase64.c +++ b/lib/src/http/NyLPC_cBase64.c @@ -49,3 +49,4 @@ void NyLPC_cBase64_encode(const NyLPC_TChar* i_src,NyLPC_TUInt16 length,char* i_ #endif /* NYLPC_CBASE64_C_ */ + diff --git a/lib/src/http/NyLPC_cBase64.h b/lib/src/http/NyLPC_cBase64.h index 457bcc5..b514af5 100644 --- a/lib/src/http/NyLPC_cBase64.h +++ b/lib/src/http/NyLPC_cBase64.h @@ -26,3 +26,4 @@ void NyLPC_cBase64_encode(const NyLPC_TChar* i_src,NyLPC_TUInt16 length,char* i_ #endif /* __cplusplus */ #endif /* NYLPC_CBASE64_H_ */ + diff --git a/lib/src/http/NyLPC_cHttpBasicBodyParser.c b/lib/src/http/NyLPC_cHttpBasicBodyParser.c index e705c7c..b7f7875 100644 --- a/lib/src/http/NyLPC_cHttpBasicBodyParser.c +++ b/lib/src/http/NyLPC_cHttpBasicBodyParser.c @@ -3,8 +3,8 @@ void NyLPC_cHttpBasicBodyParser_initialize(NyLPC_TcHttpBasicBodyParser_t* i_inst,struct NyLPC_TcHttpBasicBodyParser_Handler* i_handler) { - i_inst->_handler=i_handler; - i_inst->_status=NyLPC_TcHttpBasicBodyParser_ST_NULL; + i_inst->_handler=i_handler; + i_inst->_status=NyLPC_TcHttpBasicBodyParser_ST_NULL; } #define NyLPC_cHttpBasicBodyParser_finalize(i_inst) @@ -17,19 +17,19 @@ void NyLPC_cHttpBasicBodyParser_initialize(NyLPC_TcHttpBasicBodyParser_t* i_inst */ void NyLPC_cHttpBasicBodyParser_parseInit(NyLPC_TcHttpBasicBodyParser_t* i_inst,const struct NyLPC_THttpBasicHeader* i_info) { - switch(i_info->transfer_encoding) - { - case NyLPC_THttpMessgeHeader_TransferEncoding_CHUNKED: - i_inst->_encode_type=NyLPC_THttpMessgeHeader_TransferEncoding_CHUNKED; - i_inst->_status=NyLPC_TcHttpBasicBodyParser_ST_CHUNK_HEADER_START; - i_inst->_data.chunked.recv_len=0; - break; - default: - i_inst->_encode_type=NyLPC_THttpMessgeHeader_TransferEncoding_NONE; - i_inst->_data.normal.content_length=i_info->content_length; - i_inst->_status=NyLPC_TcHttpBasicBodyParser_ST_BODY; - break; - } + switch(i_info->transfer_encoding) + { + case NyLPC_THttpMessgeHeader_TransferEncoding_CHUNKED: + i_inst->_encode_type=NyLPC_THttpMessgeHeader_TransferEncoding_CHUNKED; + i_inst->_status=NyLPC_TcHttpBasicBodyParser_ST_CHUNK_HEADER_START; + i_inst->_data.chunked.recv_len=0; + break; + default: + i_inst->_encode_type=NyLPC_THttpMessgeHeader_TransferEncoding_NONE; + i_inst->_data.normal.content_length=i_info->content_length; + i_inst->_status=NyLPC_TcHttpBasicBodyParser_ST_BODY; + break; + } } /** @@ -39,9 +39,9 @@ void NyLPC_cHttpBasicBodyParser_parseInit(NyLPC_TcHttpBasicBodyParser_t* i_inst, */ NyLPC_TBool NyLPC_cHttpBasicBodyParser_parseFinish(NyLPC_TcHttpBasicBodyParser_t* i_inst) { - NyLPC_TBool ret=(i_inst->_status==NyLPC_TcHttpBasicBodyParser_ST_EOB); - i_inst->_status=NyLPC_TcHttpBasicBodyParser_ST_NULL; - return ret; + NyLPC_TBool ret=(i_inst->_status==NyLPC_TcHttpBasicBodyParser_ST_EOB); + i_inst->_status=NyLPC_TcHttpBasicBodyParser_ST_NULL; + return ret; } #define HTTP_CR 0x0D #define HTTP_LF 0x0A @@ -55,137 +55,136 @@ NyLPC_TBool NyLPC_cHttpBasicBodyParser_parseFinish(NyLPC_TcHttpBasicBodyParser_t */ NyLPC_TInt32 NyLPC_cHttpBasicBodyParser_parseChar(NyLPC_TcHttpBasicBodyParser_t* i_inst,const NyLPC_TChar* i_c,NyLPC_TInt32 i_size) { - NyLPC_TInt32 i; - NyLPC_TChar c; - switch(i_inst->_encode_type){ - case NyLPC_THttpMessgeHeader_TransferEncoding_CHUNKED: - for(i=0;i_status) - { - case NyLPC_TcHttpBasicBodyParser_ST_CHUNK_BODY: - //OnRecv - if(!i_inst->_handler->bodyHandler(i_inst,c)){ - i_inst->_data.chunked.recv_len--; - //中断 - if(i_inst->_data.chunked.recv_len==0){ - //content length分だけ読み取った - i_inst->_status=NyLPC_TcHttpBasicBodyParser_ST_CHUNK_FOOTER; - } - return i+1; - } - i_inst->_data.chunked.recv_len--; - if(i_inst->_data.chunked.recv_len==0){ - //content length分だけ読み取った - i_inst->_status=NyLPC_TcHttpBasicBodyParser_ST_CHUNK_FOOTER; - } - i++;//次の文字へ - break; - // HEX - case NyLPC_TcHttpBasicBodyParser_ST_CHUNK_HEADER_START: - if(isxdigit(c)){ - i_inst->_data.chunked.recv_len=i_inst->_data.chunked.recv_len*16+NyLPC_ctox(c); - //一応最大チャンクサイズは決めておこうか。 - if(i_inst->_data.chunked.recv_len>0x0fffffff){ - NyLPC_OnErrorGoto(ERROR); - } - i++;//次の文字へ - }else if(c==HTTP_SP){ - i_inst->_status=NyLPC_TcHttpBasicBodyParser_ST_CHUNK_HEADER_SP; - i++;//次の文字へ - }else if(c==HTTP_CR || c==HTTP_LF){ - i_inst->_status=NyLPC_TcHttpBasicBodyParser_ST_CHUNK_HEADER_EXT; - //読取位置を変化させずにEXTへ - }else{ - NyLPC_OnErrorGoto(ERROR); - } - break; - case NyLPC_TcHttpBasicBodyParser_ST_CHUNK_HEADER_SP: - if(c==HTTP_SP){ - i++;//次の文字へ - }else{ - //ext - i_inst->_status=NyLPC_TcHttpBasicBodyParser_ST_CHUNK_HEADER_EXT; - } - break; - case NyLPC_TcHttpBasicBodyParser_ST_CHUNK_HEADER_EXT: - //EXTの内容は読まない。 - if(c==HTTP_LF){ - if(i_inst->_data.chunked.recv_len==0){ - //chunksize=0でCRLFを検出したらend-chunk - i_inst->_status=NyLPC_TcHttpBasicBodyParser_ST_CHUNK_END; - }else{ - //chunksize>0でCRLFを検出したらBODY検出 - i_inst->_status=NyLPC_TcHttpBasicBodyParser_ST_CHUNK_BODY; - } - }else{ - //nothing to do - } - i++;//次の文字へ - break; - case NyLPC_TcHttpBasicBodyParser_ST_CHUNK_FOOTER: - //CRLF待ち - if(c==HTTP_CR){ - //無視 - }else if(c==HTTP_LF){ - //確定 - i_inst->_status=NyLPC_TcHttpBasicBodyParser_ST_CHUNK_HEADER_START; - }else{ - NyLPC_OnErrorGoto(ERROR); - } - i++; - break; - case NyLPC_TcHttpBasicBodyParser_ST_CHUNK_END: - //CRLF待ち - if(c==HTTP_CR){ - //無視 - }else if(c==HTTP_LF){ - //確定 - i_inst->_status=NyLPC_TcHttpBasicBodyParser_ST_EOB; - return i+1; - }else{ - NyLPC_OnErrorGoto(ERROR); - } - i++; - break; - default: - NyLPC_OnErrorGoto(ERROR); - } - } - return i_size; - break; - case NyLPC_THttpMessgeHeader_TransferEncoding_NONE: - if(i_inst->_status!=NyLPC_TcHttpBasicBodyParser_ST_BODY){ - NyLPC_OnErrorGoto(ERROR); - } - for(i=0;i_data.normal.content_length>0){ - //OnRecv - if(!i_inst->_handler->bodyHandler(i_inst,*(i_c+i))){ - i++; - i_inst->_data.normal.content_length--; - break;//中断(遷移無し) - } - i_inst->_data.normal.content_length--; - }else{ - //content-length==0;全て受信 - break; - } - } - if(i_inst->_data.normal.content_length==0){ - //content length分だけ読み取った - i_inst->_status=NyLPC_TcHttpBasicBodyParser_ST_EOB; - return i; - } - return i; - } + NyLPC_TInt32 i; + NyLPC_TChar c; + switch(i_inst->_encode_type){ + case NyLPC_THttpMessgeHeader_TransferEncoding_CHUNKED: + for(i=0;i_status) + { + case NyLPC_TcHttpBasicBodyParser_ST_CHUNK_BODY: + //OnRecv + if(!i_inst->_handler->bodyHandler(i_inst,c)){ + i_inst->_data.chunked.recv_len--; + //中断 + if(i_inst->_data.chunked.recv_len==0){ + //content length分だけ読み取った + i_inst->_status=NyLPC_TcHttpBasicBodyParser_ST_CHUNK_FOOTER; + } + return i+1; + } + i_inst->_data.chunked.recv_len--; + if(i_inst->_data.chunked.recv_len==0){ + //content length分だけ読み取った + i_inst->_status=NyLPC_TcHttpBasicBodyParser_ST_CHUNK_FOOTER; + } + i++;//次の文字へ + break; + // HEX + case NyLPC_TcHttpBasicBodyParser_ST_CHUNK_HEADER_START: + if(isxdigit((int)c)){ + i_inst->_data.chunked.recv_len=i_inst->_data.chunked.recv_len*16+NyLPC_ctox(c); + //一応最大チャンクサイズは決めておこうか。 + if(i_inst->_data.chunked.recv_len>0x0fffffff){ + NyLPC_OnErrorGoto(ERROR); + } + i++;//次の文字へ + }else if(c==HTTP_SP){ + i_inst->_status=NyLPC_TcHttpBasicBodyParser_ST_CHUNK_HEADER_SP; + i++;//次の文字へ + }else if(c==HTTP_CR || c==HTTP_LF){ + i_inst->_status=NyLPC_TcHttpBasicBodyParser_ST_CHUNK_HEADER_EXT; + //読取位置を変化させずにEXTへ + }else{ + NyLPC_OnErrorGoto(ERROR); + } + break; + case NyLPC_TcHttpBasicBodyParser_ST_CHUNK_HEADER_SP: + if(c==HTTP_SP){ + i++;//次の文字へ + }else{ + //ext + i_inst->_status=NyLPC_TcHttpBasicBodyParser_ST_CHUNK_HEADER_EXT; + } + break; + case NyLPC_TcHttpBasicBodyParser_ST_CHUNK_HEADER_EXT: + //EXTの内容は読まない。 + if(c==HTTP_LF){ + if(i_inst->_data.chunked.recv_len==0){ + //chunksize=0でCRLFを検出したらend-chunk + i_inst->_status=NyLPC_TcHttpBasicBodyParser_ST_CHUNK_END; + }else{ + //chunksize>0でCRLFを検出したらBODY検出 + i_inst->_status=NyLPC_TcHttpBasicBodyParser_ST_CHUNK_BODY; + } + }else{ + //nothing to do + } + i++;//次の文字へ + break; + case NyLPC_TcHttpBasicBodyParser_ST_CHUNK_FOOTER: + //CRLF待ち + if(c==HTTP_CR){ + //無視 + }else if(c==HTTP_LF){ + //確定 + i_inst->_status=NyLPC_TcHttpBasicBodyParser_ST_CHUNK_HEADER_START; + }else{ + NyLPC_OnErrorGoto(ERROR); + } + i++; + break; + case NyLPC_TcHttpBasicBodyParser_ST_CHUNK_END: + //CRLF待ち + if(c==HTTP_CR){ + //無視 + }else if(c==HTTP_LF){ + //確定 + i_inst->_status=NyLPC_TcHttpBasicBodyParser_ST_EOB; + return i+1; + }else{ + NyLPC_OnErrorGoto(ERROR); + } + i++; + break; + default: + NyLPC_OnErrorGoto(ERROR); + } + } + return i_size; + case NyLPC_THttpMessgeHeader_TransferEncoding_NONE: + if(i_inst->_status!=NyLPC_TcHttpBasicBodyParser_ST_BODY){ + NyLPC_OnErrorGoto(ERROR); + } + for(i=0;i_data.normal.content_length>0){ + //OnRecv + if(!i_inst->_handler->bodyHandler(i_inst,*(i_c+i))){ + i++; + i_inst->_data.normal.content_length--; + break;//中断(遷移無し) + } + i_inst->_data.normal.content_length--; + }else{ + //content-length==0;全て受信 + break; + } + } + if(i_inst->_data.normal.content_length==0){ + //content length分だけ読み取った + i_inst->_status=NyLPC_TcHttpBasicBodyParser_ST_EOB; + return i; + } + return i; + } ERROR: - //ERROR - i_inst->_status=NyLPC_TcHttpBasicBodyParser_ST_ERROR; - return -1; + //ERROR + i_inst->_status=NyLPC_TcHttpBasicBodyParser_ST_ERROR; + return -1; } diff --git a/lib/src/http/NyLPC_cHttpBasicBodyParser.h b/lib/src/http/NyLPC_cHttpBasicBodyParser.h index 22f7192..77ea1f7 100644 --- a/lib/src/http/NyLPC_cHttpBasicBodyParser.h +++ b/lib/src/http/NyLPC_cHttpBasicBodyParser.h @@ -19,8 +19,8 @@ * along with this program. If not, see . * * For further information please contact. - * http://nyatla.jp/ - * or + * http://nyatla.jp/ + * or * *********************************************************************************/ @@ -47,16 +47,16 @@ typedef struct NyLPC_TcHttpBasicBodyParser NyLPC_TcHttpBasicBodyParser_t; * BodyParserのステータス定義値。 */ typedef NyLPC_TUInt8 NyLPC_TcHttpBasicBodyParser_ST; -#define NyLPC_TcHttpBasicBodyParser_ST_CHUNK_HEADER_START ((NyLPC_TcHttpBasicBodyParser_ST)0x11)//ヘッダ解析待ち -#define NyLPC_TcHttpBasicBodyParser_ST_CHUNK_HEADER_SP ((NyLPC_TcHttpBasicBodyParser_ST)0x12)//ヘッダ解析中 -#define NyLPC_TcHttpBasicBodyParser_ST_CHUNK_HEADER_EXT ((NyLPC_TcHttpBasicBodyParser_ST)0x13)//ヘッダ解析中 -#define NyLPC_TcHttpBasicBodyParser_ST_CHUNK_BODY ((NyLPC_TcHttpBasicBodyParser_ST)0x14)//フッタ解析中 -#define NyLPC_TcHttpBasicBodyParser_ST_CHUNK_FOOTER ((NyLPC_TcHttpBasicBodyParser_ST)0x15)//フッタ解析中 -#define NyLPC_TcHttpBasicBodyParser_ST_CHUNK_END ((NyLPC_TcHttpBasicBodyParser_ST)0x16)//フッタ解析中 -#define NyLPC_TcHttpBasicBodyParser_ST_BODY ((NyLPC_TcHttpBasicBodyParser_ST)0x17)//BODYパース中 -#define NyLPC_TcHttpBasicBodyParser_ST_EOB ((NyLPC_TcHttpBasicBodyParser_ST)0x7F)//BODY確定 -#define NyLPC_TcHttpBasicBodyParser_ST_ERROR ((NyLPC_TcHttpBasicBodyParser_ST)0x80)//終わり -#define NyLPC_TcHttpBasicBodyParser_ST_NULL ((NyLPC_TcHttpBasicBodyParser_ST)0x00)//初期状態 +#define NyLPC_TcHttpBasicBodyParser_ST_CHUNK_HEADER_START ((NyLPC_TcHttpBasicBodyParser_ST)0x11)//ヘッダ解析待ち +#define NyLPC_TcHttpBasicBodyParser_ST_CHUNK_HEADER_SP ((NyLPC_TcHttpBasicBodyParser_ST)0x12)//ヘッダ解析中 +#define NyLPC_TcHttpBasicBodyParser_ST_CHUNK_HEADER_EXT ((NyLPC_TcHttpBasicBodyParser_ST)0x13)//ヘッダ解析中 +#define NyLPC_TcHttpBasicBodyParser_ST_CHUNK_BODY ((NyLPC_TcHttpBasicBodyParser_ST)0x14)//フッタ解析中 +#define NyLPC_TcHttpBasicBodyParser_ST_CHUNK_FOOTER ((NyLPC_TcHttpBasicBodyParser_ST)0x15)//フッタ解析中 +#define NyLPC_TcHttpBasicBodyParser_ST_CHUNK_END ((NyLPC_TcHttpBasicBodyParser_ST)0x16)//フッタ解析中 +#define NyLPC_TcHttpBasicBodyParser_ST_BODY ((NyLPC_TcHttpBasicBodyParser_ST)0x17)//BODYパース中 +#define NyLPC_TcHttpBasicBodyParser_ST_EOB ((NyLPC_TcHttpBasicBodyParser_ST)0x7F)//BODY確定 +#define NyLPC_TcHttpBasicBodyParser_ST_ERROR ((NyLPC_TcHttpBasicBodyParser_ST)0x80)//終わり +#define NyLPC_TcHttpBasicBodyParser_ST_NULL ((NyLPC_TcHttpBasicBodyParser_ST)0x00)//初期状態 #define NyLPC_TcHttpBasicBodyParser_ST_isError(i_v) ((i_v)==NyLPC_TcHttpBasicBodyParser_ST_ERROR) @@ -69,7 +69,7 @@ typedef NyLPC_TBool (*NyLPC_TcHttpBasicBodyParser_bodyHandler) (NyLPC_TcHttpBasi struct NyLPC_TcHttpBasicBodyParser_Handler { - NyLPC_TcHttpBasicBodyParser_bodyHandler bodyHandler; + NyLPC_TcHttpBasicBodyParser_bodyHandler bodyHandler; }; @@ -80,17 +80,17 @@ struct NyLPC_TcHttpBasicBodyParser_Handler */ struct NyLPC_TcHttpBasicBodyParser { - NyLPC_THttpMessgeHeader_TransferEncoding _encode_type; - NyLPC_TcHttpBasicBodyParser_ST _status; - union{ - struct{ - int recv_len; - }chunked; - struct{ - NyLPC_TUInt32 content_length; - }normal; - }_data; - struct NyLPC_TcHttpBasicBodyParser_Handler* _handler; + NyLPC_THttpMessgeHeader_TransferEncoding _encode_type; + NyLPC_TcHttpBasicBodyParser_ST _status; + union{ + struct{ + int recv_len; + }chunked; + struct{ + NyLPC_TUInt32 content_length; + }normal; + }_data; + struct NyLPC_TcHttpBasicBodyParser_Handler* _handler; }; diff --git a/lib/src/http/NyLPC_cHttpBasicHeaderParser.c b/lib/src/http/NyLPC_cHttpBasicHeaderParser.c index 23772ea..04536cf 100644 --- a/lib/src/http/NyLPC_cHttpBasicHeaderParser.c +++ b/lib/src/http/NyLPC_cHttpBasicHeaderParser.c @@ -19,8 +19,8 @@ * along with this program. If not, see . * * For further information please contact. - * http://nyatla.jp/ - * or + * http://nyatla.jp/ + * or * *********************************************************************************/ #include "NyLPC_cHttpBasicHeaderParser_protected.h" @@ -30,14 +30,14 @@ static const struct NyLPC_TTextIdTbl method_id_table[]= { - //HTTP STANDARD - {"GET",NyLPC_THttpMethodType_GET}, - {"POST",NyLPC_THttpMethodType_POST}, - {"HEAD",NyLPC_THttpMethodType_HEAD}, - //SSDP - {"M-SEARCH",NyLPC_THttpMethodType_M_SEARCH}, - {"NOTIFY",NyLPC_THttpMethodType_NOTIFY}, - {NULL,NyLPC_THttpMethodType_NULL} + //HTTP STANDARD + {"GET",NyLPC_THttpMethodType_GET}, + {"POST",NyLPC_THttpMethodType_POST}, + {"HEAD",NyLPC_THttpMethodType_HEAD}, + //SSDP + {"M-SEARCH",NyLPC_THttpMethodType_M_SEARCH}, + {"NOTIFY",NyLPC_THttpMethodType_NOTIFY}, + {NULL,NyLPC_THttpMethodType_NULL} }; /*-------------------------------------------------------------------------------- * @@ -46,23 +46,23 @@ static const struct NyLPC_TTextIdTbl method_id_table[]= --------------------------------------------------------------------------------*/ const char* NyLPC_THttpMethodType_toString(NyLPC_THttpMethodType i_method) { - const char* ret=NyLPC_TTextIdTbl_getTextById(i_method,method_id_table); - if(ret==NULL){ - NyLPC_Abort(); - } - return ret; + const char* ret=NyLPC_TTextIdTbl_getTextById(i_method,method_id_table); + if(ret==NULL){ + NyLPC_Abort(); + } + return ret; } static NyLPC_TBool parseRequestMethodStr(NyLPC_TcStr_t* i_str,NyLPC_THttpMethodType* o_out) { - //解析処理 - *o_out=NyLPC_TTextIdTbl_getMatchIdIgnoreCase(NyLPC_cStr_str(i_str),method_id_table); - if(*o_out==NyLPC_THttpMethodType_NULL){ - NyLPC_OnErrorGoto(ERROR); - } - return NyLPC_TBool_TRUE; + //解析処理 + *o_out=NyLPC_TTextIdTbl_getMatchIdIgnoreCase(NyLPC_cStr_str(i_str),method_id_table); + if(*o_out==NyLPC_THttpMethodType_NULL){ + NyLPC_OnErrorGoto(ERROR); + } + return NyLPC_TBool_TRUE; ERROR: - return NyLPC_TBool_FALSE; + return NyLPC_TBool_FALSE; } /*-------------------------------------------------------------------------------- * @@ -72,15 +72,15 @@ ERROR: NyLPC_TBool NyLPC_THttpBasicHeader_isPersistent(const struct NyLPC_THttpBasicHeader* i_struct) { - switch(i_struct->type) - { - case NyLPC_THttpHeaderType_REQUEST: - return (i_struct->connection!=NyLPC_THttpMessgeHeader_Connection_CLOSE)&&(i_struct->startline.req.version==NyLPC_THttpVersion_11); - case NyLPC_THttpHeaderType_RESPONSE: - default: - break; - } - return NyLPC_TBool_FALSE; + switch(i_struct->type) + { + case NyLPC_THttpHeaderType_REQUEST: + return (i_struct->connection!=NyLPC_THttpMessgeHeader_Connection_CLOSE)&&(i_struct->startline.req.version==NyLPC_THttpVersion_11); + case NyLPC_THttpHeaderType_RESPONSE: + default: + break; + } + return NyLPC_TBool_FALSE; } @@ -91,7 +91,7 @@ NyLPC_TBool NyLPC_THttpBasicHeader_isPersistent(const struct NyLPC_THttpBasicHea /* - 文字コードの定義 + 文字コードの定義 */ #define HTTP_SP 0x20 #define HTTP_LF 0x0A @@ -124,20 +124,20 @@ static NyLPC_TBool testHeader(struct NyLPC_THttpBasicHeader* i_header,NyLPC_TUIn */ static const struct NyLPC_TcHttpBasicHeaderParser_Handler _default_handler= { - NULL,NULL + NULL,NULL }; /*---------------------------------------- - Public/Protected関数 + Public/Protected関数 ----------------------------------------*/ void NyLPC_cHttpBasicHeaderParser_initialize(NyLPC_TcHttpBasicHeaderParser_t* i_inst,const struct NyLPC_TcHttpBasicHeaderParser_Handler* i_handler) { - NyLPC_cStr_initialize(&(i_inst->_wsb),i_inst->_wsb_buf,NyLPC_cHttpBasicHeaderParser_SIZE_OF_WBS); - i_inst->_handler=((i_handler==NULL)?&_default_handler:i_handler); + NyLPC_cStr_initialize(&(i_inst->_wsb),i_inst->_wsb_buf,NyLPC_cHttpBasicHeaderParser_SIZE_OF_WBS); + i_inst->_handler=((i_handler==NULL)?&_default_handler:i_handler); } /** @@ -145,27 +145,27 @@ void NyLPC_cHttpBasicHeaderParser_initialize(NyLPC_TcHttpBasicHeaderParser_t* i_ */ void NyLPC_cHttpBasicHeaderParser_parseInit(NyLPC_TcHttpBasicHeaderParser_t* i_inst,struct NyLPC_THttpBasicHeader* o_out) { - //出力構造体を初期化 - o_out->connection=NyLPC_THttpMessgeHeader_Connection_NONE; - o_out->content_length=NyLPC_THttpContentLength_INVALID_LENGTH; - o_out->transfer_encoding=NyLPC_THttpMessgeHeader_TransferEncoding_NONE; - i_inst->_st=NyLPC_TcHttpBasicHeaderParser_ST_START; + //出力構造体を初期化 + o_out->connection=NyLPC_THttpMessgeHeader_Connection_NONE; + o_out->content_length=NyLPC_THttpContentLength_INVALID_LENGTH; + o_out->transfer_encoding=NyLPC_THttpMessgeHeader_TransferEncoding_NONE; + i_inst->_st=NyLPC_TcHttpBasicHeaderParser_ST_START; } /** * parseCharがNyLPC_TcHttpBasicHeaderParser_ST_EOHを返却したらコールすること。 */ NyLPC_TBool NyLPC_cHttpBasicHeaderParser_parseFinish(NyLPC_TcHttpBasicHeaderParser_t* i_inst,struct NyLPC_THttpBasicHeader* o_out) { - if(i_inst->_st!=NyLPC_TcHttpBasicHeaderParser_ST_EOH) - { - return NyLPC_TBool_FALSE; - } - //整合性チェック - if(!testHeader(o_out,&i_inst->_rcode)){ - i_inst->_st=NyLPC_TcHttpBasicHeaderParser_ST_ERROR; - return NyLPC_TBool_FALSE; - } - return NyLPC_TBool_TRUE; + if(i_inst->_st!=NyLPC_TcHttpBasicHeaderParser_ST_EOH) + { + return NyLPC_TBool_FALSE; + } + //整合性チェック + if(!testHeader(o_out,&i_inst->_rcode)){ + i_inst->_st=NyLPC_TcHttpBasicHeaderParser_ST_ERROR; + return NyLPC_TBool_FALSE; + } + return NyLPC_TBool_TRUE; } /** * 文字列をパースします。 @@ -176,60 +176,60 @@ NyLPC_TBool NyLPC_cHttpBasicHeaderParser_parseFinish(NyLPC_TcHttpBasicHeaderPars */ NyLPC_TInt32 NyLPC_cHttpBasicHeaderParser_parseChar(NyLPC_TcHttpBasicHeaderParser_t* i_inst,const NyLPC_TChar* i_c,NyLPC_TInt32 i_size,struct NyLPC_THttpBasicHeader* o_out) { - int i; - NyLPC_TChar c; - //Errorチェック - if(NyLPC_TcHttpBasicHeaderParser_ST_ERROR==i_inst->_st) - { - return 0; - } - for(i=0;i_st) - { - case NyLPC_TcHttpBasicHeaderParser_ST_START: - i_inst->_st=parseStartLine(i_inst,c,o_out); - break; - case NyLPC_TcHttpBasicHeaderParser_ST_MSGPARAM: - i_inst->_st=parseMessageParam(i_inst,c,o_out); - break; - case NyLPC_TcHttpBasicHeaderParser_ST_MSGHEAD: - i_inst->_st=parseMessage1(i_inst,c,o_out); - break; - case NyLPC_TcHttpBasicHeaderParser_ST_RL_URL: - i_inst->_st=parseRequestUrl(i_inst,c,o_out); - break; - case NyLPC_TcHttpBasicHeaderParser_ST_RL_VERSION: - i_inst->_st=parseVersion(i_inst,c,NyLPC_TcHttpBasicHeaderParser_ST_MSGHEAD,o_out); - break; - case NyLPC_TcHttpBasicHeaderParser_ST_SL_STATUSCODE: - i_inst->_st=parseStatusCode(i_inst,c,o_out); - break; - case NyLPC_TcHttpBasicHeaderParser_ST_SL_REASON: - i_inst->_st=parseReason(i_inst,c); - break; - case NyLPC_TcHttpBasicHeaderParser_ST_MSG_CONTENTLENGTH: - i_inst->_st=parseMessage_ContentLength(i_inst,c,o_out); - break; - case NyLPC_TcHttpBasicHeaderParser_ST_MSG_CONNECTION: - i_inst->_st=parseMessage_Connection(i_inst,c,o_out); - break; - case NyLPC_TcHttpBasicHeaderParser_ST_MSG_TRANSFERENCODING: - i_inst->_st=parseMessage_TransferEncoding(i_inst,c,o_out); - break; - default: - i_inst->_rcode=500; - i_inst->_st=NyLPC_TcHttpBasicHeaderParser_ST_ERROR; - } - if(NyLPC_TcHttpBasicHeaderParser_ST_isError(i_inst->_st)){ - //エラー起こしたら終了。 - return i; - }else if(i_inst->_st==NyLPC_TcHttpBasicHeaderParser_ST_EOH){ - //ヘッダ終端なら終了。 - return i+1; - } - } - return i_size; + int i; + NyLPC_TChar c; + //Errorチェック + if(NyLPC_TcHttpBasicHeaderParser_ST_ERROR==i_inst->_st) + { + return 0; + } + for(i=0;i_st) + { + case NyLPC_TcHttpBasicHeaderParser_ST_START: + i_inst->_st=parseStartLine(i_inst,c,o_out); + break; + case NyLPC_TcHttpBasicHeaderParser_ST_MSGPARAM: + i_inst->_st=parseMessageParam(i_inst,c,o_out); + break; + case NyLPC_TcHttpBasicHeaderParser_ST_MSGHEAD: + i_inst->_st=parseMessage1(i_inst,c,o_out); + break; + case NyLPC_TcHttpBasicHeaderParser_ST_RL_URL: + i_inst->_st=parseRequestUrl(i_inst,c,o_out); + break; + case NyLPC_TcHttpBasicHeaderParser_ST_RL_VERSION: + i_inst->_st=parseVersion(i_inst,c,NyLPC_TcHttpBasicHeaderParser_ST_MSGHEAD,o_out); + break; + case NyLPC_TcHttpBasicHeaderParser_ST_SL_STATUSCODE: + i_inst->_st=parseStatusCode(i_inst,c,o_out); + break; + case NyLPC_TcHttpBasicHeaderParser_ST_SL_REASON: + i_inst->_st=parseReason(i_inst,c); + break; + case NyLPC_TcHttpBasicHeaderParser_ST_MSG_CONTENTLENGTH: + i_inst->_st=parseMessage_ContentLength(i_inst,c,o_out); + break; + case NyLPC_TcHttpBasicHeaderParser_ST_MSG_CONNECTION: + i_inst->_st=parseMessage_Connection(i_inst,c,o_out); + break; + case NyLPC_TcHttpBasicHeaderParser_ST_MSG_TRANSFERENCODING: + i_inst->_st=parseMessage_TransferEncoding(i_inst,c,o_out); + break; + default: + i_inst->_rcode=500; + i_inst->_st=NyLPC_TcHttpBasicHeaderParser_ST_ERROR; + } + if(NyLPC_TcHttpBasicHeaderParser_ST_isError(i_inst->_st)){ + //エラー起こしたら終了。 + return i; + }else if(i_inst->_st==NyLPC_TcHttpBasicHeaderParser_ST_EOH){ + //ヘッダ終端なら終了。 + return i+1; + } + } + return i_size; } @@ -243,28 +243,28 @@ NyLPC_TInt32 NyLPC_cHttpBasicHeaderParser_parseChar(NyLPC_TcHttpBasicHeaderParse */ NyLPC_TBool NyLPC_cHttpBasicHeaderParser_parseStream(NyLPC_TcHttpBasicHeaderParser_t* i_inst,NyLPC_TiHttpPtrStream_t* i_stream,struct NyLPC_THttpBasicHeader* o_out) { - const char* rp_base; - NyLPC_TInt32 rsize; - for(;;){ - //タイムアウト付でストリームから読み出し。 - rsize=NyLPC_iHttpPtrStream_pread(i_stream,(const void**)(&rp_base),HTTP_TIMEOUT); - if(rsize<=0){ - return NyLPC_TBool_FALSE; - } - rsize=NyLPC_cHttpBasicHeaderParser_parseChar(i_inst,rp_base,rsize,o_out); - if(i_inst->_st==NyLPC_TcHttpBasicHeaderParser_ST_ERROR){ - //パース失敗 - NyLPC_iHttpPtrStream_rseek(i_stream,rsize); - return NyLPC_TBool_FALSE; - } - if(i_inst->_st==NyLPC_TcHttpBasicHeaderParser_ST_EOH){ - //パース成功 - NyLPC_iHttpPtrStream_rseek(i_stream,rsize); - return NyLPC_TBool_TRUE; - } - NyLPC_iHttpPtrStream_rseek(i_stream,(NyLPC_TUInt16)rsize); - } - return NyLPC_TBool_FALSE; + const char* rp_base; + NyLPC_TInt32 rsize; + for(;;){ + //タイムアウト付でストリームから読み出し。 + rsize=NyLPC_iHttpPtrStream_pread(i_stream,(const void**)(&rp_base),HTTP_TIMEOUT); + if(rsize<=0){ + return NyLPC_TBool_FALSE; + } + rsize=NyLPC_cHttpBasicHeaderParser_parseChar(i_inst,rp_base,rsize,o_out); + if(i_inst->_st==NyLPC_TcHttpBasicHeaderParser_ST_ERROR){ + //パース失敗 + NyLPC_iHttpPtrStream_rseek(i_stream,rsize); + return NyLPC_TBool_FALSE; + } + if(i_inst->_st==NyLPC_TcHttpBasicHeaderParser_ST_EOH){ + //パース成功 + NyLPC_iHttpPtrStream_rseek(i_stream,rsize); + return NyLPC_TBool_TRUE; + } + NyLPC_iHttpPtrStream_rseek(i_stream,(NyLPC_TUInt16)rsize); + } + return NyLPC_TBool_FALSE; } @@ -273,440 +273,440 @@ NyLPC_TBool NyLPC_cHttpBasicHeaderParser_parseStream(NyLPC_TcHttpBasicHeaderPars // */ //NyLPC_TBool NyLPC_cHttpBasicHeaderParser_parse(NyLPC_TcHttpBasicHeaderParser_t* i_inst,NyLPC_TcHttpStream_t* i_stream,struct NyLPC_THttpBasicHeader* o_out) //{ -// NyLPC_TcHttpBasicHeaderParser_ST st; -// const char* rp_base; -// NyLPC_TInt32 rsize; -// char c; -// int i; +// NyLPC_TcHttpBasicHeaderParser_ST st; +// const char* rp_base; +// NyLPC_TInt32 rsize; +// char c; +// int i; // -// //出力構造体を初期化 -// st=NyLPC_TcHttpBasicHeaderParser_ST_START; -// o_out->connection=NyLPC_THttpMessgeHeader_Connection_NONE; -// o_out->content_length=NyLPC_THttpContentLength_INVALID_LENGTH; -// o_out->transfer_encoding=NyLPC_THttpMessgeHeader_TransferEncoding_NONE; +// //出力構造体を初期化 +// st=NyLPC_TcHttpBasicHeaderParser_ST_START; +// o_out->connection=NyLPC_THttpMessgeHeader_Connection_NONE; +// o_out->content_length=NyLPC_THttpContentLength_INVALID_LENGTH; +// o_out->transfer_encoding=NyLPC_THttpMessgeHeader_TransferEncoding_NONE; // -// for(;;){ -// //タイムアウト付でストリームから読み出し。 -// rsize=NyLPC_iHttpPtrStream_pread(i_stream,(const void**)(&rp_base)); -// if(rsize<=0){ -// return NyLPC_TBool_FALSE; -// } -// for(i=0;i_rcode=500; -// st=NyLPC_TcHttpBasicHeaderParser_ST_ERROR; -// } -// //エラー起こしたら終了。 -// if(NyLPC_TcHttpBasicHeaderParser_ST_isError(st)){ -// return NyLPC_TBool_FALSE; -// } -// //パース成功 -// if(st==NyLPC_TcHttpBasicHeaderParser_ST_EOH){ -// //整合性チェック -// if(!testHeader(o_out,&i_inst->_rcode)){ -// st=NyLPC_TcHttpBasicHeaderParser_ST_ERROR; -// return NyLPC_TBool_FALSE; -// } -// //シーク -// NyLPC_iHttpPtrStream_rseek(i_stream,i+1); -// return NyLPC_TBool_TRUE; -// } -// } -// //シーク -// NyLPC_iHttpPtrStream_rseek(i_stream,(NyLPC_TUInt16)rsize); -// } -// return NyLPC_TBool_FALSE; +// for(;;){ +// //タイムアウト付でストリームから読み出し。 +// rsize=NyLPC_iHttpPtrStream_pread(i_stream,(const void**)(&rp_base)); +// if(rsize<=0){ +// return NyLPC_TBool_FALSE; +// } +// for(i=0;i_rcode=500; +// st=NyLPC_TcHttpBasicHeaderParser_ST_ERROR; +// } +// //エラー起こしたら終了。 +// if(NyLPC_TcHttpBasicHeaderParser_ST_isError(st)){ +// return NyLPC_TBool_FALSE; +// } +// //パース成功 +// if(st==NyLPC_TcHttpBasicHeaderParser_ST_EOH){ +// //整合性チェック +// if(!testHeader(o_out,&i_inst->_rcode)){ +// st=NyLPC_TcHttpBasicHeaderParser_ST_ERROR; +// return NyLPC_TBool_FALSE; +// } +// //シーク +// NyLPC_iHttpPtrStream_rseek(i_stream,i+1); +// return NyLPC_TBool_TRUE; +// } +// } +// //シーク +// NyLPC_iHttpPtrStream_rseek(i_stream,(NyLPC_TUInt16)rsize); +// } +// return NyLPC_TBool_FALSE; //} /*---------------------------------------- - private関数 + private関数 ----------------------------------------*/ /** ヘッダの整合性をとる。 */ static NyLPC_TBool testHeader(struct NyLPC_THttpBasicHeader* i_header,NyLPC_TUInt16* o_error) { - switch(i_header->startline.req.version){ - case NyLPC_THttpVersion_09: - if(i_header->type==NyLPC_THttpHeaderType_REQUEST){ - //Requestの時だけmethodチェック - //GETだけ - if(i_header->startline.req.method!=NyLPC_THttpMethodType_GET){ - *o_error=400; - break; - } - } - //TEは受け付けない。 - if(i_header->transfer_encoding!=NyLPC_THttpMessgeHeader_TransferEncoding_NONE){ - *o_error=400; - break; - } - //ContentLength=0,Connection=Closedに修正。 - i_header->content_length=0; - i_header->connection=NyLPC_THttpMessgeHeader_Connection_CLOSE; - return NyLPC_TBool_TRUE; - case NyLPC_THttpVersion_10: - //TEは受け付けない。 - if(i_header->transfer_encoding!=NyLPC_THttpMessgeHeader_TransferEncoding_NONE){ - *o_error=406; - break; - } - //ContentLengthが無いときは0 - if(i_header->content_length==NyLPC_THttpContentLength_INVALID_LENGTH){ - i_header->content_length=0; - } - //Connection=Closedに修正。(1.0のKeepaliveは難しいから無視) - i_header->connection=NyLPC_THttpMessgeHeader_Connection_CLOSE; - return NyLPC_TBool_TRUE; - case NyLPC_THttpVersion_11: - if(i_header->content_length==NyLPC_THttpContentLength_INVALID_LENGTH){ - //Contentlength無しのChunked指定はOK - if(i_header->transfer_encoding!=NyLPC_THttpMessgeHeader_TransferEncoding_CHUNKED){ - //Chunkedが無い場合はContent-Lengthは0と仮定 - i_header->content_length=0; - }else{ - //content-length無し && Chunked有 - //OK - } - }else if(i_header->transfer_encoding!=NyLPC_THttpMessgeHeader_TransferEncoding_NONE){ - //ContentLengthあるのにChunkedとは何事 - *o_error=400; - break; - } - return NyLPC_TBool_TRUE; - case NyLPC_THttpVersion_UNKNOWN: - //おい馬鹿やめろ - *o_error=505; - break; - default: - *o_error=500; - break; - } - return NyLPC_TBool_FALSE; + switch(i_header->startline.req.version){ + case NyLPC_THttpVersion_09: + if(i_header->type==NyLPC_THttpHeaderType_REQUEST){ + //Requestの時だけmethodチェック + //GETだけ + if(i_header->startline.req.method!=NyLPC_THttpMethodType_GET){ + *o_error=400; + break; + } + } + //TEは受け付けない。 + if(i_header->transfer_encoding!=NyLPC_THttpMessgeHeader_TransferEncoding_NONE){ + *o_error=400; + break; + } + //ContentLength=0,Connection=Closedに修正。 + i_header->content_length=0; + i_header->connection=NyLPC_THttpMessgeHeader_Connection_CLOSE; + return NyLPC_TBool_TRUE; + case NyLPC_THttpVersion_10: + //TEは受け付けない。 + if(i_header->transfer_encoding!=NyLPC_THttpMessgeHeader_TransferEncoding_NONE){ + *o_error=406; + break; + } + //ContentLengthが無いときは0 + if(i_header->content_length==NyLPC_THttpContentLength_INVALID_LENGTH){ + i_header->content_length=0; + } + //Connection=Closedに修正。(1.0のKeepaliveは難しいから無視) + i_header->connection=NyLPC_THttpMessgeHeader_Connection_CLOSE; + return NyLPC_TBool_TRUE; + case NyLPC_THttpVersion_11: + if(i_header->content_length==NyLPC_THttpContentLength_INVALID_LENGTH){ + //Contentlength無しのChunked指定はOK + if(i_header->transfer_encoding!=NyLPC_THttpMessgeHeader_TransferEncoding_CHUNKED){ + //Chunkedが無い場合はContent-Lengthは0と仮定 + i_header->content_length=0; + }else{ + //content-length無し && Chunked有 + //OK + } + }else if(i_header->transfer_encoding!=NyLPC_THttpMessgeHeader_TransferEncoding_NONE){ + //ContentLengthあるのにChunkedとは何事 + *o_error=400; + break; + } + return NyLPC_TBool_TRUE; + case NyLPC_THttpVersion_UNKNOWN: + //おい馬鹿やめろ + *o_error=505; + break; + default: + *o_error=500; + break; + } + return NyLPC_TBool_FALSE; } static NyLPC_TcHttpBasicHeaderParser_ST parseMessage_TransferEncoding(NyLPC_TcHttpBasicHeaderParser_t* i_inst,NyLPC_TChar i_c,struct NyLPC_THttpBasicHeader* o_out) { - //先頭のスペース除外 - if(NyLPC_cStr_len(&(i_inst->_wsb))==0){ - if(i_c==HTTP_SP){ - return NyLPC_TcHttpBasicHeaderParser_ST_MSG_TRANSFERENCODING;//変化なし - } - } - if(i_c==HTTP_CR){ - //CRの無視 - return NyLPC_TcHttpBasicHeaderParser_ST_MSG_TRANSFERENCODING;//変化なし - }else if(i_c==HTTP_LF){ - //大文字化 - NyLPC_cStr_toUpper(&(i_inst->_wsb)); - //close? - o_out->transfer_encoding=NyLPC_cStr_isEqual(&(i_inst->_wsb),"CHUNKED")?NyLPC_THttpMessgeHeader_TransferEncoding_CHUNKED:NyLPC_THttpMessgeHeader_TransferEncoding_UNKNOWN; - NyLPC_cStr_clear(&(i_inst->_wsb)); - return NyLPC_TcHttpBasicHeaderParser_ST_MSGHEAD; - } - if(!NyLPC_cStr_put(&(i_inst->_wsb),i_c)){ - i_inst->_rcode=500; - NyLPC_OnErrorGoto(Error); - } - return NyLPC_TcHttpBasicHeaderParser_ST_MSG_TRANSFERENCODING;//変化なし; + //先頭のスペース除外 + if(NyLPC_cStr_len(&(i_inst->_wsb))==0){ + if(i_c==HTTP_SP){ + return NyLPC_TcHttpBasicHeaderParser_ST_MSG_TRANSFERENCODING;//変化なし + } + } + if(i_c==HTTP_CR){ + //CRの無視 + return NyLPC_TcHttpBasicHeaderParser_ST_MSG_TRANSFERENCODING;//変化なし + }else if(i_c==HTTP_LF){ + //大文字化 + NyLPC_cStr_toUpper(&(i_inst->_wsb)); + //close? + o_out->transfer_encoding=NyLPC_cStr_isEqual(&(i_inst->_wsb),"CHUNKED")?NyLPC_THttpMessgeHeader_TransferEncoding_CHUNKED:NyLPC_THttpMessgeHeader_TransferEncoding_UNKNOWN; + NyLPC_cStr_clear(&(i_inst->_wsb)); + return NyLPC_TcHttpBasicHeaderParser_ST_MSGHEAD; + } + if(!NyLPC_cStr_put(&(i_inst->_wsb),i_c)){ + i_inst->_rcode=500; + NyLPC_OnErrorGoto(Error); + } + return NyLPC_TcHttpBasicHeaderParser_ST_MSG_TRANSFERENCODING;//変化なし; Error: - return NyLPC_TcHttpBasicHeaderParser_ST_ERROR; + return NyLPC_TcHttpBasicHeaderParser_ST_ERROR; } static NyLPC_TcHttpBasicHeaderParser_ST parseMessage_Connection(NyLPC_TcHttpBasicHeaderParser_t* i_inst,NyLPC_TChar i_c,struct NyLPC_THttpBasicHeader* o_out) { - const static NyLPC_TUInt8 id[]={ - NyLPC_THttpMessgeHeader_Connection_CLOSE, - NyLPC_THttpMessgeHeader_Connection_KEEPALIVE, - NyLPC_THttpMessgeHeader_Connection_UPGRADE, - NyLPC_THttpMessgeHeader_Connection_UNKNOWN - }; - const static NyLPC_TChar* str[]={ - "CLOSE", - "KEEP-ALIVE", - "UPGRADE" - }; - NyLPC_TUInt8 i; - //先頭のスペース除外 - if(NyLPC_cStr_len(&(i_inst->_wsb))==0){ - if(i_c==HTTP_SP){ - return NyLPC_TcHttpBasicHeaderParser_ST_MSG_CONNECTION;//変化なし - } - } - if(i_c==HTTP_CR){ - //CRの無視 - return NyLPC_TcHttpBasicHeaderParser_ST_MSG_CONNECTION;//変化なし - }else if(i_c==HTTP_LF){ - //大文字化 - NyLPC_cStr_toUpper(&(i_inst->_wsb)); - //Convert to ID - o_out->connection=NyLPC_THttpMessgeHeader_Connection_UNKNOWN; - for(i=0;id[i]!=NyLPC_THttpMessgeHeader_Connection_UNKNOWN;i++){ - if(NyLPC_cStr_isEqual(&(i_inst->_wsb),str[i])){ - o_out->connection=id[i]; - break; - } - } - NyLPC_cStr_clear(&(i_inst->_wsb)); - return NyLPC_TcHttpBasicHeaderParser_ST_MSGHEAD; - } - if(!NyLPC_cStr_put(&(i_inst->_wsb),i_c)){ - i_inst->_rcode=500; - NyLPC_OnErrorGoto(Error); - } - return NyLPC_TcHttpBasicHeaderParser_ST_MSG_CONNECTION;//変化なし; + const static NyLPC_TUInt8 id[]={ + NyLPC_THttpMessgeHeader_Connection_CLOSE, + NyLPC_THttpMessgeHeader_Connection_KEEPALIVE, + NyLPC_THttpMessgeHeader_Connection_UPGRADE, + NyLPC_THttpMessgeHeader_Connection_UNKNOWN + }; + const static NyLPC_TChar* str[]={ + "CLOSE", + "KEEP-ALIVE", + "UPGRADE" + }; + NyLPC_TUInt8 i; + //先頭のスペース除外 + if(NyLPC_cStr_len(&(i_inst->_wsb))==0){ + if(i_c==HTTP_SP){ + return NyLPC_TcHttpBasicHeaderParser_ST_MSG_CONNECTION;//変化なし + } + } + if(i_c==HTTP_CR){ + //CRの無視 + return NyLPC_TcHttpBasicHeaderParser_ST_MSG_CONNECTION;//変化なし + }else if(i_c==HTTP_LF){ + //大文字化 + NyLPC_cStr_toUpper(&(i_inst->_wsb)); + //Convert to ID + o_out->connection=NyLPC_THttpMessgeHeader_Connection_UNKNOWN; + for(i=0;id[i]!=NyLPC_THttpMessgeHeader_Connection_UNKNOWN;i++){ + if(NyLPC_cStr_isEqual(&(i_inst->_wsb),str[i])){ + o_out->connection=id[i]; + break; + } + } + NyLPC_cStr_clear(&(i_inst->_wsb)); + return NyLPC_TcHttpBasicHeaderParser_ST_MSGHEAD; + } + if(!NyLPC_cStr_put(&(i_inst->_wsb),i_c)){ + i_inst->_rcode=500; + NyLPC_OnErrorGoto(Error); + } + return NyLPC_TcHttpBasicHeaderParser_ST_MSG_CONNECTION;//変化なし; Error: - return NyLPC_TcHttpBasicHeaderParser_ST_ERROR; + return NyLPC_TcHttpBasicHeaderParser_ST_ERROR; } static NyLPC_TcHttpBasicHeaderParser_ST parseMessage_ContentLength(NyLPC_TcHttpBasicHeaderParser_t* i_inst,NyLPC_TChar i_c,struct NyLPC_THttpBasicHeader* o_out) { - char* e; - char* p; - - //先頭のスペース除外 - if(NyLPC_cStr_len(&(i_inst->_wsb))==0) - { - if(i_c==HTTP_SP){ - return NyLPC_TcHttpBasicHeaderParser_ST_MSG_CONTENTLENGTH;//変化なし - } - } - if(i_c==HTTP_CR){ - //CRの無視 - return NyLPC_TcHttpBasicHeaderParser_ST_MSG_CONTENTLENGTH;//変化なし - }else if(i_c==HTTP_LF){ - p=NyLPC_cStr_str(&(i_inst->_wsb)); - o_out->content_length=strtol(p,&e,10); - if(e==p){ - i_inst->_rcode=400; - NyLPC_OnErrorGoto(Error);//ギャー - } - NyLPC_cStr_clear(&(i_inst->_wsb)); - return NyLPC_TcHttpBasicHeaderParser_ST_MSGHEAD; - } - if(!NyLPC_cStr_put(&(i_inst->_wsb),i_c)){ - i_inst->_rcode=500; - NyLPC_OnErrorGoto(Error); - } - return NyLPC_TcHttpBasicHeaderParser_ST_MSG_CONTENTLENGTH;//変化なし; + char* e; + char* p; + + //先頭のスペース除外 + if(NyLPC_cStr_len(&(i_inst->_wsb))==0) + { + if(i_c==HTTP_SP){ + return NyLPC_TcHttpBasicHeaderParser_ST_MSG_CONTENTLENGTH;//変化なし + } + } + if(i_c==HTTP_CR){ + //CRの無視 + return NyLPC_TcHttpBasicHeaderParser_ST_MSG_CONTENTLENGTH;//変化なし + }else if(i_c==HTTP_LF){ + p=NyLPC_cStr_str(&(i_inst->_wsb)); + o_out->content_length=strtol(p,&e,10); + if(e==p){ + i_inst->_rcode=400; + NyLPC_OnErrorGoto(Error);//ギャー + } + NyLPC_cStr_clear(&(i_inst->_wsb)); + return NyLPC_TcHttpBasicHeaderParser_ST_MSGHEAD; + } + if(!NyLPC_cStr_put(&(i_inst->_wsb),i_c)){ + i_inst->_rcode=500; + NyLPC_OnErrorGoto(Error); + } + return NyLPC_TcHttpBasicHeaderParser_ST_MSG_CONTENTLENGTH;//変化なし; Error: - return NyLPC_TcHttpBasicHeaderParser_ST_ERROR; + return NyLPC_TcHttpBasicHeaderParser_ST_ERROR; } static NyLPC_TcHttpBasicHeaderParser_ST parseStatusCode(NyLPC_TcHttpBasicHeaderParser_t* i_inst,NyLPC_TChar i_c,struct NyLPC_THttpBasicHeader* o_out) { - NyLPC_TcStr_t* ws=&(i_inst->_wsb); - char* e; - char* p; - - //先頭のスペース除外 - if(NyLPC_cStr_len(ws)==0) - { - if(i_c==HTTP_SP){ - return NyLPC_TcHttpBasicHeaderParser_ST_SL_STATUSCODE;//変化なし - } - } - if(i_c==HTTP_SP){ - //SPで終了 - p=NyLPC_cStr_str(ws); - o_out->startline.res.status=(strtol(p,&e,10)); - if(e==p){ - i_inst->_rcode=400; - NyLPC_OnErrorGoto(Error);//ギャー - } - NyLPC_cStr_clear(ws); - return NyLPC_TcHttpBasicHeaderParser_ST_SL_REASON; - } - if(!NyLPC_cStr_put(ws,i_c)){ - i_inst->_rcode=500; - NyLPC_OnErrorGoto(Error); - } - return NyLPC_TcHttpBasicHeaderParser_ST_SL_STATUSCODE;//変化なし; + NyLPC_TcStr_t* ws=&(i_inst->_wsb); + char* e; + char* p; + + //先頭のスペース除外 + if(NyLPC_cStr_len(ws)==0) + { + if(i_c==HTTP_SP){ + return NyLPC_TcHttpBasicHeaderParser_ST_SL_STATUSCODE;//変化なし + } + } + if(i_c==HTTP_SP){ + //SPで終了 + p=NyLPC_cStr_str(ws); + o_out->startline.res.status=(strtol(p,&e,10)); + if(e==p){ + i_inst->_rcode=400; + NyLPC_OnErrorGoto(Error);//ギャー + } + NyLPC_cStr_clear(ws); + return NyLPC_TcHttpBasicHeaderParser_ST_SL_REASON; + } + if(!NyLPC_cStr_put(ws,i_c)){ + i_inst->_rcode=500; + NyLPC_OnErrorGoto(Error); + } + return NyLPC_TcHttpBasicHeaderParser_ST_SL_STATUSCODE;//変化なし; Error: - return NyLPC_TcHttpBasicHeaderParser_ST_ERROR; + return NyLPC_TcHttpBasicHeaderParser_ST_ERROR; } static NyLPC_TcHttpBasicHeaderParser_ST parseReason(NyLPC_TcHttpBasicHeaderParser_t* i_inst,NyLPC_TChar i_c) { - NyLPC_TcStr_t* ws=&(i_inst->_wsb); - //LFくるまで飛ばす。 - switch(i_c){ - case HTTP_LF: - NyLPC_cStr_clear(ws); - return NyLPC_TcHttpBasicHeaderParser_ST_MSGHEAD; - default: - break; - } - //URLパーサへ通知 - return NyLPC_TcHttpBasicHeaderParser_ST_SL_REASON;//変化なし + NyLPC_TcStr_t* ws=&(i_inst->_wsb); + //LFくるまで飛ばす。 + switch(i_c){ + case HTTP_LF: + NyLPC_cStr_clear(ws); + return NyLPC_TcHttpBasicHeaderParser_ST_MSGHEAD; + default: + break; + } + //URLパーサへ通知 + return NyLPC_TcHttpBasicHeaderParser_ST_SL_REASON;//変化なし } static NyLPC_TcHttpBasicHeaderParser_ST parseMessageParam(NyLPC_TcHttpBasicHeaderParser_t* i_inst,NyLPC_TChar i_c,struct NyLPC_THttpBasicHeader* o_out) { - NyLPC_TcStr_t* ws=&(i_inst->_wsb); - //先頭のスペース除外 - if(NyLPC_cStr_len(ws)==0){ - if(i_c==HTTP_SP){ - return NyLPC_TcHttpBasicHeaderParser_ST_MSGPARAM;//変化なし - }else{ - NyLPC_cStr_put(ws,'C');//開始フラグ - } - } - switch(i_c){ - case HTTP_CR: - return NyLPC_TcHttpBasicHeaderParser_ST_MSGPARAM;//変化なし - case HTTP_LF: - //メッセージフィールドの終端を通知 - if(i_inst->_handler->messageHandler!=NULL){ - if(!i_inst->_handler->messageHandler(i_inst,NULL,0,o_out)){ - i_inst->_rcode=500; - NyLPC_OnErrorGoto(Error); - } - } - NyLPC_cStr_clear(ws); - return NyLPC_TcHttpBasicHeaderParser_ST_MSGHEAD; - default: - //メッセージフィールドの追記 - if(i_inst->_handler->messageHandler!=NULL){ - if(!i_inst->_handler->messageHandler(i_inst,NULL,i_c,o_out)){ - i_inst->_rcode=500; - NyLPC_OnErrorGoto(Error); - } - } - break; - } - //URLパーサへ通知 - return NyLPC_TcHttpBasicHeaderParser_ST_MSGPARAM;//変化なし + NyLPC_TcStr_t* ws=&(i_inst->_wsb); + //先頭のスペース除外 + if(NyLPC_cStr_len(ws)==0){ + if(i_c==HTTP_SP){ + return NyLPC_TcHttpBasicHeaderParser_ST_MSGPARAM;//変化なし + }else{ + NyLPC_cStr_put(ws,'C');//開始フラグ + } + } + switch(i_c){ + case HTTP_CR: + return NyLPC_TcHttpBasicHeaderParser_ST_MSGPARAM;//変化なし + case HTTP_LF: + //メッセージフィールドの終端を通知 + if(i_inst->_handler->messageHandler!=NULL){ + if(!i_inst->_handler->messageHandler(i_inst,NULL,0,o_out)){ + i_inst->_rcode=500; + NyLPC_OnErrorGoto(Error); + } + } + NyLPC_cStr_clear(ws); + return NyLPC_TcHttpBasicHeaderParser_ST_MSGHEAD; + default: + //メッセージフィールドの追記 + if(i_inst->_handler->messageHandler!=NULL){ + if(!i_inst->_handler->messageHandler(i_inst,NULL,i_c,o_out)){ + i_inst->_rcode=500; + NyLPC_OnErrorGoto(Error); + } + } + break; + } + //URLパーサへ通知 + return NyLPC_TcHttpBasicHeaderParser_ST_MSGPARAM;//変化なし Error: - return NyLPC_TcHttpBasicHeaderParser_ST_ERROR; + return NyLPC_TcHttpBasicHeaderParser_ST_ERROR; } static NyLPC_TcHttpBasicHeaderParser_ST parseMessage1(NyLPC_TcHttpBasicHeaderParser_t* i_inst,NyLPC_TChar i_c,struct NyLPC_THttpBasicHeader* o_out) { - const static char* KNOWN_MSG[]={"CONNECTION","CONTENT-LENGTH","TRANSFER-ENCODING",NULL}; - int i; - - switch(i_c){ - case HTTP_DM: - //メッセージの名前確定。遷移先判定 - //ヘッダ名を大文字にする。 - NyLPC_cStr_toUpper(&(i_inst->_wsb)); - for(i=0;KNOWN_MSG[i]!=NULL;i++){ - if(NyLPC_cStr_isEqual(&(i_inst->_wsb),KNOWN_MSG[i])){ - //確定。 - NyLPC_cStr_clear(&(i_inst->_wsb)); - switch(i){ - case 0://CONNECTION - return NyLPC_TcHttpBasicHeaderParser_ST_MSG_CONNECTION; - case 1://CONTENT-LENGTH - return NyLPC_TcHttpBasicHeaderParser_ST_MSG_CONTENTLENGTH; - case 2://TRANSFER-ENCODING - return NyLPC_TcHttpBasicHeaderParser_ST_MSG_TRANSFERENCODING; - default://エラー - break; - } - i_inst->_rcode=500; - NyLPC_OnErrorGoto(Error); - } - } - //メッセージフィールドの名前を通知 - if(i_inst->_handler->messageHandler!=NULL){ - if(!i_inst->_handler->messageHandler(i_inst,NyLPC_cStr_str(&(i_inst->_wsb)),0,o_out)){ - i_inst->_rcode=500; - NyLPC_OnErrorGoto(Error); - } - NyLPC_cStr_clear(&(i_inst->_wsb)); - } - //カスタムヘッダ解析へ。 - return NyLPC_TcHttpBasicHeaderParser_ST_MSGPARAM; - case HTTP_CR: - return NyLPC_TcHttpBasicHeaderParser_ST_MSGHEAD;//変化なし - case HTTP_LF: - //1文字で終了ならパースエンド。バリデーションチェックへ - if(NyLPC_cStr_len(&(i_inst->_wsb))==0){ - NyLPC_cStr_clear(&(i_inst->_wsb)); - return NyLPC_TcHttpBasicHeaderParser_ST_EOH; - } - //これはひどい。 - i_inst->_rcode=400; - NyLPC_OnErrorGoto(Error); - default: - break; - } - if(!NyLPC_cStr_put(&(i_inst->_wsb),i_c)){ - i_inst->_rcode=500; - NyLPC_OnErrorGoto(Error); - } - return NyLPC_TcHttpBasicHeaderParser_ST_MSGHEAD;//変化なし; + const static char* KNOWN_MSG[]={"CONNECTION","CONTENT-LENGTH","TRANSFER-ENCODING",NULL}; + int i; + + switch(i_c){ + case HTTP_DM: + //メッセージの名前確定。遷移先判定 + //ヘッダ名を大文字にする。 + NyLPC_cStr_toUpper(&(i_inst->_wsb)); + for(i=0;KNOWN_MSG[i]!=NULL;i++){ + if(NyLPC_cStr_isEqual(&(i_inst->_wsb),KNOWN_MSG[i])){ + //確定。 + NyLPC_cStr_clear(&(i_inst->_wsb)); + switch(i){ + case 0://CONNECTION + return NyLPC_TcHttpBasicHeaderParser_ST_MSG_CONNECTION; + case 1://CONTENT-LENGTH + return NyLPC_TcHttpBasicHeaderParser_ST_MSG_CONTENTLENGTH; + case 2://TRANSFER-ENCODING + return NyLPC_TcHttpBasicHeaderParser_ST_MSG_TRANSFERENCODING; + default://エラー + break; + } + i_inst->_rcode=500; + NyLPC_OnErrorGoto(Error); + } + } + //メッセージフィールドの名前を通知 + if(i_inst->_handler->messageHandler!=NULL){ + if(!i_inst->_handler->messageHandler(i_inst,NyLPC_cStr_str(&(i_inst->_wsb)),0,o_out)){ + i_inst->_rcode=500; + NyLPC_OnErrorGoto(Error); + } + NyLPC_cStr_clear(&(i_inst->_wsb)); + } + //カスタムヘッダ解析へ。 + return NyLPC_TcHttpBasicHeaderParser_ST_MSGPARAM; + case HTTP_CR: + return NyLPC_TcHttpBasicHeaderParser_ST_MSGHEAD;//変化なし + case HTTP_LF: + //1文字で終了ならパースエンド。バリデーションチェックへ + if(NyLPC_cStr_len(&(i_inst->_wsb))==0){ + NyLPC_cStr_clear(&(i_inst->_wsb)); + return NyLPC_TcHttpBasicHeaderParser_ST_EOH; + } + //これはひどい。 + i_inst->_rcode=400; + NyLPC_OnErrorGoto(Error); + default: + break; + } + if(!NyLPC_cStr_put(&(i_inst->_wsb),i_c)){ + i_inst->_rcode=500; + NyLPC_OnErrorGoto(Error); + } + return NyLPC_TcHttpBasicHeaderParser_ST_MSGHEAD;//変化なし; Error: - return NyLPC_TcHttpBasicHeaderParser_ST_ERROR; + return NyLPC_TcHttpBasicHeaderParser_ST_ERROR; } static NyLPC_TcHttpBasicHeaderParser_ST parseVersion(NyLPC_TcHttpBasicHeaderParser_t* i_inst,NyLPC_TChar i_c,NyLPC_TcHttpBasicHeaderParser_ST i_next,struct NyLPC_THttpBasicHeader* o_out) { - //先頭のスペース除外 - if(NyLPC_cStr_len(&(i_inst->_wsb))==0){ - if(i_c==HTTP_SP){ - return NyLPC_TcHttpBasicHeaderParser_ST_RL_VERSION;//変化なし - } - } - if(i_c==HTTP_CR){ - //CRの無視 - return NyLPC_TcHttpBasicHeaderParser_ST_RL_VERSION;//変化なし - }else if(i_c==HTTP_LF){ - //LFで確定 - if(!parseHttpVersionStr(&(i_inst->_wsb),&(o_out->startline.req.version))){ - i_inst->_rcode=505; - NyLPC_cStr_clear(&(i_inst->_wsb)); - NyLPC_OnErrorGoto(Error); - } - NyLPC_cStr_clear(&(i_inst->_wsb)); - return i_next;//遷移(エラーの時はそのままエラーコードが渡る。) - } - if(!NyLPC_cStr_put(&(i_inst->_wsb),i_c)){ - //追記処理しっぱい - i_inst->_rcode=500; - NyLPC_OnErrorGoto(Error); - } - return NyLPC_TcHttpBasicHeaderParser_ST_RL_VERSION;//変化なし + //先頭のスペース除外 + if(NyLPC_cStr_len(&(i_inst->_wsb))==0){ + if(i_c==HTTP_SP){ + return NyLPC_TcHttpBasicHeaderParser_ST_RL_VERSION;//変化なし + } + } + if(i_c==HTTP_CR){ + //CRの無視 + return NyLPC_TcHttpBasicHeaderParser_ST_RL_VERSION;//変化なし + }else if(i_c==HTTP_LF){ + //LFで確定 + if(!parseHttpVersionStr(&(i_inst->_wsb),&(o_out->startline.req.version))){ + i_inst->_rcode=505; + NyLPC_cStr_clear(&(i_inst->_wsb)); + NyLPC_OnErrorGoto(Error); + } + NyLPC_cStr_clear(&(i_inst->_wsb)); + return i_next;//遷移(エラーの時はそのままエラーコードが渡る。) + } + if(!NyLPC_cStr_put(&(i_inst->_wsb),i_c)){ + //追記処理しっぱい + i_inst->_rcode=500; + NyLPC_OnErrorGoto(Error); + } + return NyLPC_TcHttpBasicHeaderParser_ST_RL_VERSION;//変化なし Error: - return NyLPC_TcHttpBasicHeaderParser_ST_ERROR; + return NyLPC_TcHttpBasicHeaderParser_ST_ERROR; } /** @@ -714,70 +714,70 @@ URLパーサ。登録した関数に転送する? */ static NyLPC_TcHttpBasicHeaderParser_ST parseRequestUrl(NyLPC_TcHttpBasicHeaderParser_t* i_inst,NyLPC_TChar i_c,struct NyLPC_THttpBasicHeader* o_out) { - //先頭のスペース除外 - if(NyLPC_cStr_len(&(i_inst->_wsb))==0){ - if(i_c==HTTP_SP){ - return NyLPC_TcHttpBasicHeaderParser_ST_RL_URL;//変化なし - }else{ - NyLPC_cStr_put(&(i_inst->_wsb),'C');//開始フラグ - } - } - //次のスペースがくるまで。 - if(i_c==HTTP_SP){ - NyLPC_cStr_clear(&(i_inst->_wsb)); - //URLハンドラへ通知 - if(i_inst->_handler->urlHandler!=NULL){ - if(!i_inst->_handler->urlHandler(i_inst,0,o_out)){ - i_inst->_rcode=500; - NyLPC_OnErrorGoto(Error); - } - } - return NyLPC_TcHttpBasicHeaderParser_ST_RL_VERSION; - } - //URLパーサへ通知 - if(i_inst->_handler->urlHandler!=NULL){ - if(!i_inst->_handler->urlHandler(i_inst,i_c,o_out)){ - i_inst->_rcode=500; - NyLPC_OnErrorGoto(Error); - } - } - return NyLPC_TcHttpBasicHeaderParser_ST_RL_URL;//変化なし + //先頭のスペース除外 + if(NyLPC_cStr_len(&(i_inst->_wsb))==0){ + if(i_c==HTTP_SP){ + return NyLPC_TcHttpBasicHeaderParser_ST_RL_URL;//変化なし + }else{ + NyLPC_cStr_put(&(i_inst->_wsb),'C');//開始フラグ + } + } + //次のスペースがくるまで。 + if(i_c==HTTP_SP){ + NyLPC_cStr_clear(&(i_inst->_wsb)); + //URLハンドラへ通知 + if(i_inst->_handler->urlHandler!=NULL){ + if(!i_inst->_handler->urlHandler(i_inst,0,o_out)){ + i_inst->_rcode=500; + NyLPC_OnErrorGoto(Error); + } + } + return NyLPC_TcHttpBasicHeaderParser_ST_RL_VERSION; + } + //URLパーサへ通知 + if(i_inst->_handler->urlHandler!=NULL){ + if(!i_inst->_handler->urlHandler(i_inst,i_c,o_out)){ + i_inst->_rcode=500; + NyLPC_OnErrorGoto(Error); + } + } + return NyLPC_TcHttpBasicHeaderParser_ST_RL_URL;//変化なし Error: - return NyLPC_TcHttpBasicHeaderParser_ST_ERROR; + return NyLPC_TcHttpBasicHeaderParser_ST_ERROR; } /** Methodパーサ - [:HTTPMETHOD:]を得る。 + [:HTTPMETHOD:]を得る。 */ static NyLPC_TcHttpBasicHeaderParser_ST parseStartLine(NyLPC_TcHttpBasicHeaderParser_t* i_inst,NyLPC_TChar i_c,struct NyLPC_THttpBasicHeader* o_out) { - if(i_c==HTTP_SP){ - //SPがデリミタ - //HTTPステータスを試す。 - if(parseHttpVersionStr(&(i_inst->_wsb),&(o_out->startline.res.version))){ - //これはHTTPステータス - o_out->type=NyLPC_THttpHeaderType_RESPONSE; - NyLPC_cStr_clear(&(i_inst->_wsb)); - return NyLPC_TcHttpBasicHeaderParser_ST_SL_STATUSCODE; - } - //HTTPリクエストを試す。 - if(!parseRequestMethodStr(&(i_inst->_wsb),&(o_out->startline.req.method))){ - i_inst->_rcode=400; - NyLPC_OnErrorGoto(ERROR); - } - //これはHTTPリクエスト - o_out->type=NyLPC_THttpHeaderType_REQUEST; - NyLPC_cStr_clear(&(i_inst->_wsb)); - return NyLPC_TcHttpBasicHeaderParser_ST_RL_URL; - } - if(!NyLPC_cStr_put(&(i_inst->_wsb),i_c)){ - i_inst->_rcode=500; - NyLPC_OnErrorGoto(ERROR); - } - return NyLPC_TcHttpBasicHeaderParser_ST_START;//変化なし + if(i_c==HTTP_SP){ + //SPがデリミタ + //HTTPステータスを試す。 + if(parseHttpVersionStr(&(i_inst->_wsb),&(o_out->startline.res.version))){ + //これはHTTPステータス + o_out->type=NyLPC_THttpHeaderType_RESPONSE; + NyLPC_cStr_clear(&(i_inst->_wsb)); + return NyLPC_TcHttpBasicHeaderParser_ST_SL_STATUSCODE; + } + //HTTPリクエストを試す。 + if(!parseRequestMethodStr(&(i_inst->_wsb),&(o_out->startline.req.method))){ + i_inst->_rcode=400; + NyLPC_OnErrorGoto(ERROR); + } + //これはHTTPリクエスト + o_out->type=NyLPC_THttpHeaderType_REQUEST; + NyLPC_cStr_clear(&(i_inst->_wsb)); + return NyLPC_TcHttpBasicHeaderParser_ST_RL_URL; + } + if(!NyLPC_cStr_put(&(i_inst->_wsb),i_c)){ + i_inst->_rcode=500; + NyLPC_OnErrorGoto(ERROR); + } + return NyLPC_TcHttpBasicHeaderParser_ST_START;//変化なし ERROR: - return NyLPC_TcHttpBasicHeaderParser_ST_ERROR; + return NyLPC_TcHttpBasicHeaderParser_ST_ERROR; } @@ -786,59 +786,59 @@ ERROR: static NyLPC_TBool parseHttpVersionStr(NyLPC_TcStr_t* i_str,NyLPC_THttpVersion* o_out) { - NyLPC_TChar* p; - char* e; - long ma,mi; - - p=NyLPC_cStr_str(i_str); - if(NyLPC_cStr_len(i_str)<6){ - NyLPC_OnErrorGoto(Error); - } - if(strncmp(p,"HTTP/",5)!=0){ - NyLPC_OnErrorGoto(Error); - } - p+=5; - ma=strtol(p,&e,10); - if(p==e){ - NyLPC_OnErrorGoto(Error); - } - p=e;//.をチェック - if(*p!='.'){ - NyLPC_OnErrorGoto(Error); - } - p++; - mi=strtoul(p,&e,10); - if(p==e){ - NyLPC_OnErrorGoto(Error); - } - if(ma<0 ||mi<0){ - NyLPC_OnErrorGoto(Error); - } - switch(ma){ - case 0: - if(mi>=9){ - //HTTP0.9相当 - *o_out=NyLPC_THttpVersion_09; - } - break; - case 1: - if(mi==0){ - //HTTP1.0 - *o_out=NyLPC_THttpVersion_10; - }else if(mi>=1){ - //HTTP1.1 - *o_out=NyLPC_THttpVersion_11; - }else{ - *o_out=NyLPC_THttpVersion_UNKNOWN; - } - break; - default: - //お前など知らん - *o_out=NyLPC_THttpVersion_UNKNOWN; - break; - } - return NyLPC_TBool_TRUE;//変化なし + NyLPC_TChar* p; + char* e; + long ma,mi; + + p=NyLPC_cStr_str(i_str); + if(NyLPC_cStr_len(i_str)<6){ + NyLPC_OnErrorGoto(Error); + } + if(strncmp(p,"HTTP/",5)!=0){ + NyLPC_OnErrorGoto(Error); + } + p+=5; + ma=strtol(p,&e,10); + if(p==e){ + NyLPC_OnErrorGoto(Error); + } + p=e;//.をチェック + if(*p!='.'){ + NyLPC_OnErrorGoto(Error); + } + p++; + mi=strtoul(p,&e,10); + if(p==e){ + NyLPC_OnErrorGoto(Error); + } + if(ma<0 ||mi<0){ + NyLPC_OnErrorGoto(Error); + } + switch(ma){ + case 0: + if(mi>=9){ + //HTTP0.9相当 + *o_out=NyLPC_THttpVersion_09; + } + break; + case 1: + if(mi==0){ + //HTTP1.0 + *o_out=NyLPC_THttpVersion_10; + }else if(mi>=1){ + //HTTP1.1 + *o_out=NyLPC_THttpVersion_11; + }else{ + *o_out=NyLPC_THttpVersion_UNKNOWN; + } + break; + default: + //お前など知らん + *o_out=NyLPC_THttpVersion_UNKNOWN; + break; + } + return NyLPC_TBool_TRUE;//変化なし Error: - return NyLPC_TBool_FALSE; + return NyLPC_TBool_FALSE; } diff --git a/lib/src/http/NyLPC_cHttpBasicHeaderParser.h b/lib/src/http/NyLPC_cHttpBasicHeaderParser.h index 3cc56ae..93372df 100644 --- a/lib/src/http/NyLPC_cHttpBasicHeaderParser.h +++ b/lib/src/http/NyLPC_cHttpBasicHeaderParser.h @@ -19,8 +19,8 @@ * along with this program. If not, see . * * For further information please contact. - * http://nyatla.jp/ - * or + * http://nyatla.jp/ + * or * *********************************************************************************/ @@ -37,21 +37,21 @@ extern "C" { * HeaderParserのステータス定義値。 */ typedef NyLPC_TUInt8 NyLPC_TcHttpBasicHeaderParser_ST; -#define NyLPC_TcHttpBasicHeaderParser_ST_START ((NyLPC_TcHttpBasicHeaderParser_ST)0x01)//HTTPステータスラインか、リクエストライン -#define NyLPC_TcHttpBasicHeaderParser_ST_RL_URL ((NyLPC_TcHttpBasicHeaderParser_ST)0x12)//URL -#define NyLPC_TcHttpBasicHeaderParser_ST_RL_VERSION ((NyLPC_TcHttpBasicHeaderParser_ST)0x13)//VERSION -#define NyLPC_TcHttpBasicHeaderParser_ST_SL_STATUSCODE ((NyLPC_TcHttpBasicHeaderParser_ST)0x21)//ステータスコード -#define NyLPC_TcHttpBasicHeaderParser_ST_SL_REASON ((NyLPC_TcHttpBasicHeaderParser_ST)0x22)//Reason-Phrase -#define NyLPC_TcHttpBasicHeaderParser_ST_MSGHEAD ((NyLPC_TcHttpBasicHeaderParser_ST)0x31)//MESSAGE -#define NyLPC_TcHttpBasicHeaderParser_ST_MSGPARAM ((NyLPC_TcHttpBasicHeaderParser_ST)0x32)//MESSAGEパラメータ部 -#define NyLPC_TcHttpBasicHeaderParser_ST_MSG_CONTENTLENGTH ((NyLPC_TcHttpBasicHeaderParser_ST)0x33)//MESSAGEContentLength -#define NyLPC_TcHttpBasicHeaderParser_ST_MSG_CONNECTION ((NyLPC_TcHttpBasicHeaderParser_ST)0x34)//MESSAGEConnection -#define NyLPC_TcHttpBasicHeaderParser_ST_MSG_TRANSFERENCODING ((NyLPC_TcHttpBasicHeaderParser_ST)0x35)//TransferEncoding -#define NyLPC_TcHttpBasicHeaderParser_ST_MSG_RANGE ((NyLPC_TcHttpBasicHeaderParser_ST)0x36)//Range -#define NyLPC_TcHttpBasicHeaderParser_ST_EOH ((NyLPC_TcHttpBasicHeaderParser_ST)0x7F)//終わり -#define NyLPC_TcHttpBasicHeaderParser_ST_ERROR ((NyLPC_TcHttpBasicHeaderParser_ST)0x80)//終わり +#define NyLPC_TcHttpBasicHeaderParser_ST_START ((NyLPC_TcHttpBasicHeaderParser_ST)0x01)//HTTPステータスラインか、リクエストライン +#define NyLPC_TcHttpBasicHeaderParser_ST_RL_URL ((NyLPC_TcHttpBasicHeaderParser_ST)0x12)//URL +#define NyLPC_TcHttpBasicHeaderParser_ST_RL_VERSION ((NyLPC_TcHttpBasicHeaderParser_ST)0x13)//VERSION +#define NyLPC_TcHttpBasicHeaderParser_ST_SL_STATUSCODE ((NyLPC_TcHttpBasicHeaderParser_ST)0x21)//ステータスコード +#define NyLPC_TcHttpBasicHeaderParser_ST_SL_REASON ((NyLPC_TcHttpBasicHeaderParser_ST)0x22)//Reason-Phrase +#define NyLPC_TcHttpBasicHeaderParser_ST_MSGHEAD ((NyLPC_TcHttpBasicHeaderParser_ST)0x31)//MESSAGE +#define NyLPC_TcHttpBasicHeaderParser_ST_MSGPARAM ((NyLPC_TcHttpBasicHeaderParser_ST)0x32)//MESSAGEパラメータ部 +#define NyLPC_TcHttpBasicHeaderParser_ST_MSG_CONTENTLENGTH ((NyLPC_TcHttpBasicHeaderParser_ST)0x33)//MESSAGEContentLength +#define NyLPC_TcHttpBasicHeaderParser_ST_MSG_CONNECTION ((NyLPC_TcHttpBasicHeaderParser_ST)0x34)//MESSAGEConnection +#define NyLPC_TcHttpBasicHeaderParser_ST_MSG_TRANSFERENCODING ((NyLPC_TcHttpBasicHeaderParser_ST)0x35)//TransferEncoding +#define NyLPC_TcHttpBasicHeaderParser_ST_MSG_RANGE ((NyLPC_TcHttpBasicHeaderParser_ST)0x36)//Range +#define NyLPC_TcHttpBasicHeaderParser_ST_EOH ((NyLPC_TcHttpBasicHeaderParser_ST)0x7F)//終わり +#define NyLPC_TcHttpBasicHeaderParser_ST_ERROR ((NyLPC_TcHttpBasicHeaderParser_ST)0x80)//終わり /* - プライベート関数 + プライベート関数 */ #define NyLPC_TcHttpBasicHeaderParser_ST_isError(i_v) ((i_v)==NyLPC_TcHttpBasicHeaderParser_ST_ERROR) @@ -68,13 +68,13 @@ typedef struct NyLPC_TcHttpBasicHeaderParser NyLPC_TcHttpBasicHeaderParser_t; */ typedef NyLPC_TUInt8 NyLPC_THttpMethodType; //HTTP STANDARD -#define NyLPC_THttpMethodType_NULL ((NyLPC_THttpMethodType)0x00) -#define NyLPC_THttpMethodType_GET ((NyLPC_THttpMethodType)0x01) -#define NyLPC_THttpMethodType_POST ((NyLPC_THttpMethodType)0x02) -#define NyLPC_THttpMethodType_HEAD ((NyLPC_THttpMethodType)0x03) +#define NyLPC_THttpMethodType_NULL ((NyLPC_THttpMethodType)0x00) +#define NyLPC_THttpMethodType_GET ((NyLPC_THttpMethodType)0x01) +#define NyLPC_THttpMethodType_POST ((NyLPC_THttpMethodType)0x02) +#define NyLPC_THttpMethodType_HEAD ((NyLPC_THttpMethodType)0x03) //SSDP -#define NyLPC_THttpMethodType_M_SEARCH ((NyLPC_THttpMethodType)0x11) -#define NyLPC_THttpMethodType_NOTIFY ((NyLPC_THttpMethodType)0x12) +#define NyLPC_THttpMethodType_M_SEARCH ((NyLPC_THttpMethodType)0x11) +#define NyLPC_THttpMethodType_NOTIFY ((NyLPC_THttpMethodType)0x12) const char* NyLPC_THttpMethodType_toString(NyLPC_THttpMethodType i_method); @@ -82,10 +82,10 @@ const char* NyLPC_THttpMethodType_toString(NyLPC_THttpMethodType i_method); * HTTPバージョンの定義値 */ typedef NyLPC_TUInt8 NyLPC_THttpVersion; -#define NyLPC_THttpVersion_09 ((NyLPC_THttpVersion)0x01) -#define NyLPC_THttpVersion_10 ((NyLPC_THttpVersion)0x02) -#define NyLPC_THttpVersion_11 ((NyLPC_THttpVersion)0x03) -#define NyLPC_THttpVersion_UNKNOWN ((NyLPC_THttpVersion)0x04) +#define NyLPC_THttpVersion_09 ((NyLPC_THttpVersion)0x01) +#define NyLPC_THttpVersion_10 ((NyLPC_THttpVersion)0x02) +#define NyLPC_THttpVersion_11 ((NyLPC_THttpVersion)0x03) +#define NyLPC_THttpVersion_UNKNOWN ((NyLPC_THttpVersion)0x04) typedef NyLPC_TUInt8 NyLPC_THttpHeaderType; @@ -101,7 +101,7 @@ typedef NyLPC_TUInt8 NyLPC_THttpMessgeHeader_Connection; #define NyLPC_THttpMessgeHeader_Connection_UNKNOWN ((NyLPC_THttpMessgeHeader_Connection)0x10) typedef NyLPC_TUInt8 NyLPC_THttpMessgeHeader_TransferEncoding; -#define NyLPC_THttpMessgeHeader_TransferEncoding_NONE ((NyLPC_THttpMessgeHeader_TransferEncoding)0x01) //TEはない +#define NyLPC_THttpMessgeHeader_TransferEncoding_NONE ((NyLPC_THttpMessgeHeader_TransferEncoding)0x01) //TEはない #define NyLPC_THttpMessgeHeader_TransferEncoding_CHUNKED ((NyLPC_THttpMessgeHeader_TransferEncoding)0x02) #define NyLPC_THttpMessgeHeader_TransferEncoding_UNKNOWN ((NyLPC_THttpMessgeHeader_TransferEncoding)0x10) @@ -116,21 +116,21 @@ typedef NyLPC_TUInt8 NyLPC_THttpMessgeHeader_TransferEncoding; */ struct NyLPC_THttpBasicHeader { - NyLPC_THttpMessgeHeader_TransferEncoding transfer_encoding; - NyLPC_THttpMessgeHeader_Connection connection; - NyLPC_THttpHeaderType type; - NyLPC_TUInt8 _padding; - union{ - struct{ - NyLPC_THttpVersion version; - NyLPC_THttpMethodType method; - }req; - struct{ - NyLPC_THttpVersion version; - NyLPC_TUInt32 status; - }res; - }startline; - NyLPC_TUInt32 content_length; + NyLPC_THttpMessgeHeader_TransferEncoding transfer_encoding; + NyLPC_THttpMessgeHeader_Connection connection; + NyLPC_THttpHeaderType type; + NyLPC_TUInt8 _padding; + union{ + struct{ + NyLPC_THttpVersion version; + NyLPC_THttpMethodType method; + }req; + struct{ + NyLPC_THttpVersion version; + NyLPC_TUInt32 status; + }res; + }startline; + NyLPC_TUInt32 content_length; }; /** @@ -182,8 +182,8 @@ typedef NyLPC_TBool (*NyLPC_cHttpBasicHeaderParser_urlHandler) (NyLPC_TcHttpBasi */ struct NyLPC_TcHttpBasicHeaderParser_Handler { - NyLPC_cHttpBasicHeaderParser_messageHandler messageHandler; - NyLPC_cHttpBasicHeaderParser_urlHandler urlHandler; + NyLPC_cHttpBasicHeaderParser_messageHandler messageHandler; + NyLPC_cHttpBasicHeaderParser_urlHandler urlHandler; }; @@ -193,15 +193,15 @@ struct NyLPC_TcHttpBasicHeaderParser_Handler */ struct NyLPC_TcHttpBasicHeaderParser { - /**継承クラスで実装すべきインタフェイス*/ - const struct NyLPC_TcHttpBasicHeaderParser_Handler* _handler; - NyLPC_TUInt16 _rcode;//_stがERRORの時にエラーコードを格納する。 - /** パースの実行状態*/ - NyLPC_TcHttpBasicHeaderParser_ST _st; - /**ワーク文字列*/ - NyLPC_TcStr_t _wsb; - /**ワーク文字列のバッファ*/ - char _wsb_buf[NyLPC_cHttpBasicHeaderParser_SIZE_OF_WBS]; + /**継承クラスで実装すべきインタフェイス*/ + const struct NyLPC_TcHttpBasicHeaderParser_Handler* _handler; + NyLPC_TUInt16 _rcode;//_stがERRORの時にエラーコードを格納する。 + /** パースの実行状態*/ + NyLPC_TcHttpBasicHeaderParser_ST _st; + /**ワーク文字列*/ + NyLPC_TcStr_t _wsb; + /**ワーク文字列のバッファ*/ + char _wsb_buf[NyLPC_cHttpBasicHeaderParser_SIZE_OF_WBS]; }; diff --git a/lib/src/http/NyLPC_cHttpBasicHeaderParser_protected.h b/lib/src/http/NyLPC_cHttpBasicHeaderParser_protected.h index b5c4138..aef0269 100644 --- a/lib/src/http/NyLPC_cHttpBasicHeaderParser_protected.h +++ b/lib/src/http/NyLPC_cHttpBasicHeaderParser_protected.h @@ -19,8 +19,8 @@ * along with this program. If not, see . * * For further information please contact. - * http://nyatla.jp/ - * or + * http://nyatla.jp/ + * or * *********************************************************************************/ #ifndef NyLPC_cHttpBasicHeaderParser_protected_h diff --git a/lib/src/http/NyLPC_cHttpBodyParser.c b/lib/src/http/NyLPC_cHttpBodyParser.c index bc2566c..16e9519 100644 --- a/lib/src/http/NyLPC_cHttpBodyParser.c +++ b/lib/src/http/NyLPC_cHttpBodyParser.c @@ -4,21 +4,21 @@ static NyLPC_TBool bodyHandler(NyLPC_TcHttpBasicBodyParser_t* i_inst,NyLPC_TChar i_c) { - NyLPC_TcHttpBodyParser_t* inst=(NyLPC_TcHttpBodyParser_t*)i_inst; - *(inst->ref_buf+inst->len)=i_c; - inst->len++; - return inst->buf_size>inst->len; + NyLPC_TcHttpBodyParser_t* inst=(NyLPC_TcHttpBodyParser_t*)i_inst; + *(inst->ref_buf+inst->len)=i_c; + inst->len++; + return inst->buf_size>inst->len; } static struct NyLPC_TcHttpBasicBodyParser_Handler _bh={bodyHandler}; void NyLPC_cHttpBodyParser_initialize(NyLPC_TcHttpBodyParser_t* i_inst) { - NyLPC_cHttpBasicBodyParser_initialize(&i_inst->_super,&_bh); + NyLPC_cHttpBasicBodyParser_initialize(&i_inst->_super,&_bh); } void NyLPC_cHttpBodyParser_finalize(NyLPC_TcHttpBodyParser_t* i_inst) { - NyLPC_cHttpBasicBodyParser_finalize(&i_inst->_super); + NyLPC_cHttpBasicBodyParser_finalize(&i_inst->_super); } @@ -30,30 +30,30 @@ void NyLPC_cHttpBodyParser_finalize(NyLPC_TcHttpBodyParser_t* i_inst) */ NyLPC_TBool NyLPC_cHttpBodyParser_parseStream(NyLPC_TcHttpBodyParser_t* i_inst,NyLPC_TiHttpPtrStream_t* i_stream,NyLPC_TChar* i_buf,NyLPC_TInt16 i_buf_size,NyLPC_TInt16* i_out) { - NyLPC_TcHttpBodyParser_t* inst=(NyLPC_TcHttpBodyParser_t*)i_inst; - const char* rp_base; - NyLPC_TInt32 rsize; - inst->len=0; - inst->buf_size=i_buf_size; - inst->ref_buf=i_buf; - if(i_inst->_super._status==NyLPC_TcHttpBasicBodyParser_ST_EOB){ - *i_out=0; - return NyLPC_TBool_TRUE; - } - for(;;){ - //タイムアウト付でストリームから読み出し。 - rsize=NyLPC_iHttpPtrStream_pread(i_stream,(const void**)(&rp_base),HTTP_TIMEOUT); - if(rsize<=0){ - //Read失敗 - return NyLPC_TBool_FALSE; - } - rsize=NyLPC_cHttpBasicBodyParser_parseChar(&i_inst->_super,rp_base,rsize); - if(i_inst->_super._status==NyLPC_TcHttpBasicBodyParser_ST_ERROR){ - //パース失敗 - return NyLPC_TBool_FALSE; - } - NyLPC_iHttpPtrStream_rseek(i_stream,(NyLPC_TUInt16)rsize); - *i_out=i_inst->len; - return NyLPC_TBool_TRUE; - } + NyLPC_TcHttpBodyParser_t* inst=(NyLPC_TcHttpBodyParser_t*)i_inst; + const char* rp_base; + NyLPC_TInt32 rsize; + inst->len=0; + inst->buf_size=i_buf_size; + inst->ref_buf=i_buf; + if(i_inst->_super._status==NyLPC_TcHttpBasicBodyParser_ST_EOB){ + *i_out=0; + return NyLPC_TBool_TRUE; + } + for(;;){ + //タイムアウト付でストリームから読み出し。 + rsize=NyLPC_iHttpPtrStream_pread(i_stream,(const void**)(&rp_base),HTTP_TIMEOUT); + if(rsize<=0){ + //Read失敗 + return NyLPC_TBool_FALSE; + } + rsize=NyLPC_cHttpBasicBodyParser_parseChar(&i_inst->_super,rp_base,rsize); + if(i_inst->_super._status==NyLPC_TcHttpBasicBodyParser_ST_ERROR){ + //パース失敗 + return NyLPC_TBool_FALSE; + } + NyLPC_iHttpPtrStream_rseek(i_stream,(NyLPC_TUInt16)rsize); + *i_out=i_inst->len; + return NyLPC_TBool_TRUE; + } } diff --git a/lib/src/http/NyLPC_cHttpBodyParser.h b/lib/src/http/NyLPC_cHttpBodyParser.h index c05a091..665ee1a 100644 --- a/lib/src/http/NyLPC_cHttpBodyParser.h +++ b/lib/src/http/NyLPC_cHttpBodyParser.h @@ -14,10 +14,10 @@ typedef struct NyLPC_TcHttpBodyParser NyLPC_TcHttpBodyParser_t; struct NyLPC_TcHttpBodyParser { - NyLPC_TcHttpBasicBodyParser_t _super; - NyLPC_TChar* ref_buf; - NyLPC_TUInt16 buf_size; - NyLPC_TUInt16 len; + NyLPC_TcHttpBasicBodyParser_t _super; + NyLPC_TChar* ref_buf; + NyLPC_TUInt16 buf_size; + NyLPC_TUInt16 len; }; diff --git a/lib/src/http/NyLPC_cHttpBodyWriter.c b/lib/src/http/NyLPC_cHttpBodyWriter.c index 9d6c3ee..72d3bc2 100644 --- a/lib/src/http/NyLPC_cHttpBodyWriter.c +++ b/lib/src/http/NyLPC_cHttpBodyWriter.c @@ -19,8 +19,8 @@ * along with this program. If not, see . * * For further information please contact. - * http://nyatla.jp/ - * or + * http://nyatla.jp/ + * or * *********************************************************************************/ @@ -39,40 +39,40 @@ */ static NyLPC_TBool printHandler(void* i_inst,const void* i_buf,NyLPC_TUInt32 i_len) { - //エラー状態ならFALSE - if(((NyLPC_TcHttpBodyWriter_t*)i_inst)->_is_error){ - return NyLPC_TBool_FALSE; - } - ((NyLPC_TcHttpBodyWriter_t*)i_inst)->_size_of_sent+=i_len; - if(!NyLPC_iHttpPtrStream_write(((NyLPC_TcHttpBodyWriter_t*)i_inst)->_ref_stream,i_buf,i_len)){ - ((NyLPC_TcHttpBodyWriter_t*)i_inst)->_is_error=NyLPC_TUInt8_TRUE; - return NyLPC_TBool_FALSE; - } - return NyLPC_TBool_TRUE; + //エラー状態ならFALSE + if(((NyLPC_TcHttpBodyWriter_t*)i_inst)->_is_error){ + return NyLPC_TBool_FALSE; + } + ((NyLPC_TcHttpBodyWriter_t*)i_inst)->_size_of_sent+=i_len; + if(!NyLPC_iHttpPtrStream_write(((NyLPC_TcHttpBodyWriter_t*)i_inst)->_ref_stream,i_buf,i_len)){ + ((NyLPC_TcHttpBodyWriter_t*)i_inst)->_is_error=NyLPC_TUInt8_TRUE; + return NyLPC_TBool_FALSE; + } + return NyLPC_TBool_TRUE; } void NyLPC_cHttpBodyWriter_initialize(NyLPC_TcHttpBodyWriter_t* i_inst,NyLPC_TcHttpStream_t* i_stream) { - i_inst->_ref_stream=&(i_stream->super); - i_inst->_is_chunked=NyLPC_TUInt8_FALSE; - i_inst->_is_error=NyLPC_TUInt8_FALSE; - i_inst->_size_of_sent=0; - i_inst->_content_length=0; - NyLPC_iHttpPtrStream_setWriteEncoding(i_inst->_ref_stream,NyLPC_TiHttpPtrStream_ET_NONE); + i_inst->_ref_stream=&(i_stream->super); + i_inst->_is_chunked=NyLPC_TUInt8_FALSE; + i_inst->_is_error=NyLPC_TUInt8_FALSE; + i_inst->_size_of_sent=0; + i_inst->_content_length=0; + NyLPC_iHttpPtrStream_setWriteEncoding(i_inst->_ref_stream,NyLPC_TiHttpPtrStream_ET_NONE); } void NyLPC_cHttpBodyWriter_setChunked(NyLPC_TcHttpBodyWriter_t* i_inst) { - i_inst->_is_chunked=NyLPC_TUInt8_TRUE; - NyLPC_iHttpPtrStream_setWriteEncoding(i_inst->_ref_stream,NyLPC_TiHttpPtrStream_ET_CHUNKED); + i_inst->_is_chunked=NyLPC_TUInt8_TRUE; + NyLPC_iHttpPtrStream_setWriteEncoding(i_inst->_ref_stream,NyLPC_TiHttpPtrStream_ET_CHUNKED); } void NyLPC_cHttpBodyWriter_setContentLength(NyLPC_TcHttpBodyWriter_t* i_inst,NyLPC_TUInt32 i_content_length) { - i_inst->_is_chunked=NyLPC_TUInt8_FALSE; - i_inst->_content_length=i_content_length; - NyLPC_iHttpPtrStream_setWriteEncoding(i_inst->_ref_stream,NyLPC_TiHttpPtrStream_ET_NONE); + i_inst->_is_chunked=NyLPC_TUInt8_FALSE; + i_inst->_content_length=i_content_length; + NyLPC_iHttpPtrStream_setWriteEncoding(i_inst->_ref_stream,NyLPC_TiHttpPtrStream_ET_NONE); } @@ -83,7 +83,7 @@ void NyLPC_cHttpBodyWriter_setContentLength(NyLPC_TcHttpBodyWriter_t* i_inst,NyL */ NyLPC_TBool NyLPC_cHttpBodyWriter_write(NyLPC_TcHttpBodyWriter_t* i_inst,const void* i_buf,NyLPC_TUInt32 i_len) { - return printHandler(i_inst,i_buf,i_len); + return printHandler(i_inst,i_buf,i_len); } /** @@ -92,24 +92,24 @@ NyLPC_TBool NyLPC_cHttpBodyWriter_write(NyLPC_TcHttpBodyWriter_t* i_inst,const v */ NyLPC_TBool NyLPC_cHttpBodyWriter_close(NyLPC_TcHttpBodyWriter_t* i_inst) { - //エラー状態ならFALSE - if(i_inst->_is_error){ - return NyLPC_TBool_FALSE; - } - //chunkedの場合、フッタを書き込む - if(i_inst->_is_chunked){ - //エンコーディングを戻す。 - NyLPC_iHttpPtrStream_setWriteEncoding(i_inst->_ref_stream,NyLPC_TiHttpPtrStream_ET_NONE); - //フッタを書き込む。 - if(!NyLPC_iHttpPtrStream_write(i_inst->_ref_stream,"0\r\n\r\n",5)){ - i_inst->_is_error=NyLPC_TUInt8_TRUE; - return NyLPC_TBool_FALSE; - } - } - //エラーでないときはストリームをフラッシュ - NyLPC_iHttpPtrStream_flush(i_inst->_ref_stream); - //クローズのステータスで状態を変える。 - return NyLPC_TBool_TRUE; + //エラー状態ならFALSE + if(i_inst->_is_error){ + return NyLPC_TBool_FALSE; + } + //chunkedの場合、フッタを書き込む + if(i_inst->_is_chunked){ + //エンコーディングを戻す。 + NyLPC_iHttpPtrStream_setWriteEncoding(i_inst->_ref_stream,NyLPC_TiHttpPtrStream_ET_NONE); + //フッタを書き込む。 + if(!NyLPC_iHttpPtrStream_write(i_inst->_ref_stream,"0\r\n\r\n",5)){ + i_inst->_is_error=NyLPC_TUInt8_TRUE; + return NyLPC_TBool_FALSE; + } + } + //エラーでないときはストリームをフラッシュ + NyLPC_iHttpPtrStream_flush(i_inst->_ref_stream); + //クローズのステータスで状態を変える。 + return NyLPC_TBool_TRUE; } /** @@ -119,27 +119,27 @@ NyLPC_TBool NyLPC_cHttpBodyWriter_close(NyLPC_TcHttpBodyWriter_t* i_inst) */ NyLPC_TBool NyLPC_cHttpBodyWriter_format(NyLPC_TcHttpBodyWriter_t* i_inst,const NyLPC_TChar* i_fmt,...) { - NyLPC_TBool ret; - va_list a; - //エラー状態ならFALSE - if(i_inst->_is_error){ - return NyLPC_TBool_FALSE; - } - va_start(a,i_fmt); - ret= NyLPC_cFormatWriter_print(printHandler,i_inst,i_fmt,a); - va_end(a); - return ret; + NyLPC_TBool ret; + va_list a; + //エラー状態ならFALSE + if(i_inst->_is_error){ + return NyLPC_TBool_FALSE; + } + va_start(a,i_fmt); + ret= NyLPC_cFormatWriter_print(printHandler,i_inst,i_fmt,a); + va_end(a); + return ret; } NyLPC_TBool NyLPC_cHttpBodyWriter_formatV(NyLPC_TcHttpBodyWriter_t* i_inst,const NyLPC_TChar* i_fmt,va_list i_args) { - NyLPC_TBool ret; - //エラー状態ならFALSE - if(i_inst->_is_error){ - return NyLPC_TBool_FALSE; - } - ret=NyLPC_cFormatWriter_print(printHandler,i_inst,i_fmt,i_args); - return ret; + NyLPC_TBool ret; + //エラー状態ならFALSE + if(i_inst->_is_error){ + return NyLPC_TBool_FALSE; + } + ret=NyLPC_cFormatWriter_print(printHandler,i_inst,i_fmt,i_args); + return ret; } /** @@ -151,22 +151,22 @@ NyLPC_TBool NyLPC_cHttpBodyWriter_formatV(NyLPC_TcHttpBodyWriter_t* i_inst,const #include "NyLPC_cHttpHeaderWriter.h" const char* TP1= - "HTTP/0.9 200 OK\r\n" - "HOST: 127.0.0.0.0.0.1\r\n" - "CONTENt-LENGTH: 1285\r\n" - "CONNECTION: CloSe\r\n" - "ETAG: nyatla.jp\r\n" - "ETAG: nyatla.jp\r\n" - "Transfer-Encoding:chunked\r\n" - "\r\n"; + "HTTP/0.9 200 OK\r\n" + "HOST: 127.0.0.0.0.0.1\r\n" + "CONTENt-LENGTH: 1285\r\n" + "CONNECTION: CloSe\r\n" + "ETAG: nyatla.jp\r\n" + "ETAG: nyatla.jp\r\n" + "Transfer-Encoding:chunked\r\n" + "\r\n"; const char* TP2= - "GET /nyanyanya!/nyoronnnnnnnnnnnn?m,fpeofjregnoegnr HTTP/1.1\r\n" - "HOST: 127.0.0.0.0.0.1\r\n" - "CONTENt-LENGTH: 1285\r\n" - "CONNECTION: Keep\r\n" - "ETAG: nyatla.jp\r\n" - "ETAG: nyatla.jp\r\n" - "\r\n"; + "GET /nyanyanya!/nyoronnnnnnnnnnnn?m,fpeofjregnoegnr HTTP/1.1\r\n" + "HOST: 127.0.0.0.0.0.1\r\n" + "CONTENt-LENGTH: 1285\r\n" + "CONNECTION: Keep\r\n" + "ETAG: nyatla.jp\r\n" + "ETAG: nyatla.jp\r\n" + "\r\n"; const char* DT="0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"; @@ -176,53 +176,53 @@ const char* DT="0123456789012345678901234567890123456789012345678901234567890123 void main() { - NyLPC_TcHttpStream_t st; - NyLPC_TcHttpBasicHeaderParser_t hp; - struct NyLPC_THttpBasicHeader reqheader; - NyLPC_TcHttpHeaderWriter_t hw; - NyLPC_TcHttpBodyWriter_t bw; - NyLPC_TcTcpSocket_t ts; - int body_len; - NyLPC_cTcpSocket_initialized(NULL,TP2,strlen(TP2)); - - //TCPのオープン - if(!NyLPC_cHttpStream_initialize(&st,&ts)){ - //エラー - } - for(;;){ - //ヘッダ解析 - NyLPC_cHttpBasicHeaderParser_initialize(&hp); - if(!NyLPC_cHttpShortRequestHeaderParser_parse(&hp,&st,&reqheader)){ - //エラー - puts("Error"); - } - //ヘッダの内容確認 - if(reqheader.type!=NyLPC_THttpHeaderType_REQUEST){ - //BadRequest - puts("Error"); - } - if(reqheader.startline.req.method!=NyLPC_THttpMethodType_GET){ - //リクエストサポートしてない - puts("Error"); - } - // - NyLPC_cHttpHeaderWriter_initialize(&hw,&st,&reqheader); -// NyLPC_cHttpResponseWriter_setClose(&hw); - body_len=100; - NyLPC_cHttpHeaderWriter_setContentLength(&hw,body_len); - NyLPC_cHttpHeaderWriter_writeResponseHeader(&hw,500); - NyLPC_cHttpHeaderWriter_close(&hw); - - NyLPC_cHttpBodyWriter_initialize(&bw,&st); - NyLPC_cHttpBodyWriter_setChunked(&bw); - NyLPC_cHttpBodyWriter_write(&bw,"TEST",4); - NyLPC_cHttpBodyWriter_printf(&bw,"TEST"); - NyLPC_cHttpBodyWriter_printf(&bw,"TEST[%s][%d][%c],%%,[%x]","abcde",123,'s',0xff0011); - NyLPC_cHttpBodyWriter_close(&bw); - NyLPC_cHttpHttpWriter_finalize(&hw); - } - NyLPC_cHttpStream_finalize(&st); - //TCPのクローズ - return; + NyLPC_TcHttpStream_t st; + NyLPC_TcHttpBasicHeaderParser_t hp; + struct NyLPC_THttpBasicHeader reqheader; + NyLPC_TcHttpHeaderWriter_t hw; + NyLPC_TcHttpBodyWriter_t bw; + NyLPC_TcTcpSocket_t ts; + int body_len; + NyLPC_cTcpSocket_initialized(NULL,TP2,strlen(TP2)); + + //TCPのオープン + if(!NyLPC_cHttpStream_initialize(&st,&ts)){ + //エラー + } + for(;;){ + //ヘッダ解析 + NyLPC_cHttpBasicHeaderParser_initialize(&hp); + if(!NyLPC_cHttpShortRequestHeaderParser_parse(&hp,&st,&reqheader)){ + //エラー + puts("Error"); + } + //ヘッダの内容確認 + if(reqheader.type!=NyLPC_THttpHeaderType_REQUEST){ + //BadRequest + puts("Error"); + } + if(reqheader.startline.req.method!=NyLPC_THttpMethodType_GET){ + //リクエストサポートしてない + puts("Error"); + } + // + NyLPC_cHttpHeaderWriter_initialize(&hw,&st,&reqheader); +// NyLPC_cHttpResponseWriter_setClose(&hw); + body_len=100; + NyLPC_cHttpHeaderWriter_setContentLength(&hw,body_len); + NyLPC_cHttpHeaderWriter_writeResponseHeader(&hw,500); + NyLPC_cHttpHeaderWriter_close(&hw); + + NyLPC_cHttpBodyWriter_initialize(&bw,&st); + NyLPC_cHttpBodyWriter_setChunked(&bw); + NyLPC_cHttpBodyWriter_write(&bw,"TEST",4); + NyLPC_cHttpBodyWriter_printf(&bw,"TEST"); + NyLPC_cHttpBodyWriter_printf(&bw,"TEST[%s][%d][%c],%%,[%x]","abcde",123,'s',0xff0011); + NyLPC_cHttpBodyWriter_close(&bw); + NyLPC_cHttpHttpWriter_finalize(&hw); + } + NyLPC_cHttpStream_finalize(&st); + //TCPのクローズ + return; } #endif diff --git a/lib/src/http/NyLPC_cHttpBodyWriter.h b/lib/src/http/NyLPC_cHttpBodyWriter.h index fa3efc4..e1e59e2 100644 --- a/lib/src/http/NyLPC_cHttpBodyWriter.h +++ b/lib/src/http/NyLPC_cHttpBodyWriter.h @@ -19,8 +19,8 @@ * along with this program. If not, see . * * For further information please contact. - * http://nyatla.jp/ - * or + * http://nyatla.jp/ + * or * *********************************************************************************/ @@ -38,12 +38,12 @@ extern "C" { typedef struct NyLPC_TcHttpBodyWriter NyLPC_TcHttpBodyWriter_t; struct NyLPC_TcHttpBodyWriter { - NyLPC_TiHttpPtrStream_t* _ref_stream; - NyLPC_TUInt8 _is_chunked; - NyLPC_TUInt8 _is_error; - NyLPC_TUInt32 _size_of_sent; - /** _is_chunked==NyLPC_TUInt8_TRUEの時のみ有効. ContentLengthの値*/ - NyLPC_TUInt32 _content_length; + NyLPC_TiHttpPtrStream_t* _ref_stream; + NyLPC_TUInt8 _is_chunked; + NyLPC_TUInt8 _is_error; + NyLPC_TUInt32 _size_of_sent; + /** _is_chunked==NyLPC_TUInt8_TRUEの時のみ有効. ContentLengthの値*/ + NyLPC_TUInt32 _content_length; }; /** diff --git a/lib/src/http/NyLPC_cHttpHeaderWriter.c b/lib/src/http/NyLPC_cHttpHeaderWriter.c index e2740b6..712a9ba 100644 --- a/lib/src/http/NyLPC_cHttpHeaderWriter.c +++ b/lib/src/http/NyLPC_cHttpHeaderWriter.c @@ -19,8 +19,8 @@ * along with this program. If not, see . * * For further information please contact. - * http://nyatla.jp/ - * or + * http://nyatla.jp/ + * or * *********************************************************************************/ #include @@ -34,29 +34,29 @@ */ NyLPC_TBool NyLPC_cHttpHeaderWriter_initialize(NyLPC_TcHttpHeaderWriter_t* i_inst,NyLPC_TiHttpPtrStream_t* i_ref_stream,const struct NyLPC_THttpBasicHeader* i_req_header) { - i_inst->_is_chunked=NyLPC_TUInt8_FALSE; - i_inst->_content_length=0; - i_inst->_ref_stream=i_ref_stream; - i_inst->_is_error=NyLPC_TUInt8_FALSE; - //書込エンコーディングをなしにセット - NyLPC_iHttpPtrStream_setWriteEncoding(i_inst->_ref_stream,NyLPC_TiHttpPtrStream_ET_NONE); - //必要に応じてリクエストの内容をパース - if(i_req_header!=NULL){ - if(i_req_header->type!=NyLPC_THttpHeaderType_REQUEST){ - return NyLPC_TBool_FALSE; - } - //コネクションの持続性を決定 - if((i_req_header->connection==NyLPC_THttpMessgeHeader_Connection_CLOSE)|| - (i_req_header->startline.req.version!=NyLPC_THttpVersion_11)) - { - i_inst->_is_close=NyLPC_TUInt8_TRUE; - }else{ - i_inst->_is_close=NyLPC_TUInt8_FALSE; - } - }else{ - i_inst->_is_close=NyLPC_TUInt8_TRUE; - } - return NyLPC_TBool_TRUE; + i_inst->_is_chunked=NyLPC_TUInt8_FALSE; + i_inst->_content_length=0; + i_inst->_ref_stream=i_ref_stream; + i_inst->_is_error=NyLPC_TUInt8_FALSE; + //書込エンコーディングをなしにセット + NyLPC_iHttpPtrStream_setWriteEncoding(i_inst->_ref_stream,NyLPC_TiHttpPtrStream_ET_NONE); + //必要に応じてリクエストの内容をパース + if(i_req_header!=NULL){ + if(i_req_header->type!=NyLPC_THttpHeaderType_REQUEST){ + return NyLPC_TBool_FALSE; + } + //コネクションの持続性を決定 + if((i_req_header->connection==NyLPC_THttpMessgeHeader_Connection_CLOSE)|| + (i_req_header->startline.req.version!=NyLPC_THttpVersion_11)) + { + i_inst->_is_close=NyLPC_TUInt8_TRUE; + }else{ + i_inst->_is_close=NyLPC_TUInt8_FALSE; + } + }else{ + i_inst->_is_close=NyLPC_TUInt8_TRUE; + } + return NyLPC_TBool_TRUE; } #define NyLPC_cHttpHttpWriter_finalize(i) @@ -64,130 +64,130 @@ NyLPC_TBool NyLPC_cHttpHeaderWriter_initialize(NyLPC_TcHttpHeaderWriter_t* i_ins void NyLPC_cHttpHeaderWriter_setChunked(NyLPC_TcHttpHeaderWriter_t* i_inst) { - i_inst->_is_chunked=NyLPC_TUInt8_TRUE; - return; + i_inst->_is_chunked=NyLPC_TUInt8_TRUE; + return; } void NyLPC_cHttpHeaderWriter_setContentLength(NyLPC_TcHttpHeaderWriter_t* i_inst,NyLPC_TUInt32 i_content_length) { - i_inst->_content_length=i_content_length; - return; + i_inst->_content_length=i_content_length; + return; } void NyLPC_cHttpHeaderWriter_setConnectionClose(NyLPC_TcHttpHeaderWriter_t* i_inst,NyLPC_TBool i_is_close) { - i_inst->_is_close=i_is_close; - return; + i_inst->_is_close=i_is_close; + return; } const static struct{ - NyLPC_TUInt16 code; - const NyLPC_TChar* message; + NyLPC_TUInt16 code; + const NyLPC_TChar* message; }status_line_tbl[]={ - {200,"OK"}, - {301,"Moved Permanently"}, - {302,"Moved Temporarily"}, - {400,"Bad Request"}, - {403,"Forbidden"}, - {404,"Not Found"}, - {405,"Method Not Allowed"}, - {500,"Internal Server Error"}, - {0,NULL}//これ最後にしてね。 + {200,"OK"}, + {301,"Moved Permanently"}, + {302,"Moved Temporarily"}, + {400,"Bad Request"}, + {403,"Forbidden"}, + {404,"Not Found"}, + {405,"Method Not Allowed"}, + {500,"Internal Server Error"}, + {0,NULL}//これ最後にしてね。 }; const static char* getStatusMessage(NyLPC_TUInt16 i_status) { - int i=0; - while(status_line_tbl[i].code!=0){ - if(i_status==status_line_tbl[i].code){ - return status_line_tbl[i].message; - } - i++; - } - return NULL; + int i=0; + while(status_line_tbl[i].code!=0){ + if(i_status==status_line_tbl[i].code){ + return status_line_tbl[i].message; + } + i++; + } + return NULL; } static NyLPC_TBool writeln(NyLPC_TiHttpPtrStream_t* i_inst,const void* i_data,NyLPC_TInt16 i_length) { - if(NyLPC_iHttpPtrStream_write(i_inst,i_data,i_length)){ - if(NyLPC_iHttpPtrStream_write(i_inst,"\r\n",2)){ - return NyLPC_TBool_TRUE; - } - } - return NyLPC_TBool_FALSE; + if(NyLPC_iHttpPtrStream_write(i_inst,i_data,i_length)){ + if(NyLPC_iHttpPtrStream_write(i_inst,"\r\n",2)){ + return NyLPC_TBool_TRUE; + } + } + return NyLPC_TBool_FALSE; } NyLPC_TBool NyLPC_cHttpHeaderWriter_writeRequestHeader(NyLPC_TcHttpHeaderWriter_t* i_inst,NyLPC_THttpMethodType i_method,const struct NyLPC_TIPv4Addr* i_host,NyLPC_TUInt16 i_port,const NyLPC_TChar* i_path) { - const NyLPC_TChar* t; - NyLPC_TChar v[16]; - //エラー状態ならなにもしない。 - if(i_inst->_is_error){ - return NyLPC_TBool_FALSE; - } + const NyLPC_TChar* t; + NyLPC_TChar v[16]; + //エラー状態ならなにもしない。 + if(i_inst->_is_error){ + return NyLPC_TBool_FALSE; + } - t=NyLPC_THttpMethodType_toString(i_method); - if(t==NULL){ - return NyLPC_TBool_FALSE; - } - //リクエストラインの記述 - //Method - if(!NyLPC_iHttpPtrStream_write(i_inst->_ref_stream,t,-1)){ - NyLPC_OnErrorGoto(Error); - } - if(!NyLPC_iHttpPtrStream_write(i_inst->_ref_stream," ",1)){ - NyLPC_OnErrorGoto(Error); - } - //Path - if(!NyLPC_iHttpPtrStream_write(i_inst->_ref_stream,i_path,-1)){ - NyLPC_OnErrorGoto(Error); - } - if(!NyLPC_iHttpPtrStream_write(i_inst->_ref_stream," HTTP/1.1\r\n",11)){ - NyLPC_OnErrorGoto(Error); - } - //HOSTの記述 - if(!NyLPC_iHttpPtrStream_write(i_inst->_ref_stream,"Host: ",6)){ - NyLPC_OnErrorGoto(Error); - } - NyLPC_TIPv4Addr_toString(i_host,v); - if(!NyLPC_iHttpPtrStream_write(i_inst->_ref_stream,v,-1)){ - NyLPC_OnErrorGoto(Error); - } - if(!NyLPC_iHttpPtrStream_write(i_inst->_ref_stream,":",1)){ - NyLPC_OnErrorGoto(Error); - } - NyLPC_uitoa(i_port,v,10); - if(!writeln(i_inst->_ref_stream,v,-1)){ - NyLPC_OnErrorGoto(Error); - } + t=NyLPC_THttpMethodType_toString(i_method); + if(t==NULL){ + return NyLPC_TBool_FALSE; + } + //リクエストラインの記述 + //Method + if(!NyLPC_iHttpPtrStream_write(i_inst->_ref_stream,t,-1)){ + NyLPC_OnErrorGoto(Error); + } + if(!NyLPC_iHttpPtrStream_write(i_inst->_ref_stream," ",1)){ + NyLPC_OnErrorGoto(Error); + } + //Path + if(!NyLPC_iHttpPtrStream_write(i_inst->_ref_stream,i_path,-1)){ + NyLPC_OnErrorGoto(Error); + } + if(!NyLPC_iHttpPtrStream_write(i_inst->_ref_stream," HTTP/1.1\r\n",11)){ + NyLPC_OnErrorGoto(Error); + } + //HOSTの記述 + if(!NyLPC_iHttpPtrStream_write(i_inst->_ref_stream,"Host: ",6)){ + NyLPC_OnErrorGoto(Error); + } + NyLPC_TIPv4Addr_toString(i_host,v); + if(!NyLPC_iHttpPtrStream_write(i_inst->_ref_stream,v,-1)){ + NyLPC_OnErrorGoto(Error); + } + if(!NyLPC_iHttpPtrStream_write(i_inst->_ref_stream,":",1)){ + NyLPC_OnErrorGoto(Error); + } + NyLPC_uitoa(i_port,v,10); + if(!writeln(i_inst->_ref_stream,v,-1)){ + NyLPC_OnErrorGoto(Error); + } - //close - if(i_inst->_is_close){ - if(!NyLPC_iHttpPtrStream_write(i_inst->_ref_stream,"Connection: CLOSE\r\n",-1)){ - NyLPC_OnErrorGoto(Error); - } - } + //close + if(i_inst->_is_close){ + if(!NyLPC_iHttpPtrStream_write(i_inst->_ref_stream,"Connection: CLOSE\r\n",-1)){ + NyLPC_OnErrorGoto(Error); + } + } - //chunked - if(i_inst->_is_chunked){ - if(!NyLPC_iHttpPtrStream_write(i_inst->_ref_stream,"Transfer-Encoding: chunked\r\n",-1)){ - NyLPC_OnErrorGoto(Error); - } - }else{ - if(!NyLPC_iHttpPtrStream_write(i_inst->_ref_stream,"Content-Length: ",-1)){ - NyLPC_OnErrorGoto(Error); - } - NyLPC_uitoa(i_inst->_content_length,v,10); - if(!writeln(i_inst->_ref_stream,v,-1)){ - NyLPC_OnErrorGoto(Error); - } - } - //送信サイズをリセット - i_inst->_size_of_sent=0; - return NyLPC_TBool_TRUE; + //chunked + if(i_inst->_is_chunked){ + if(!NyLPC_iHttpPtrStream_write(i_inst->_ref_stream,"Transfer-Encoding: chunked\r\n",-1)){ + NyLPC_OnErrorGoto(Error); + } + }else{ + if(!NyLPC_iHttpPtrStream_write(i_inst->_ref_stream,"Content-Length: ",-1)){ + NyLPC_OnErrorGoto(Error); + } + NyLPC_uitoa(i_inst->_content_length,v,10); + if(!writeln(i_inst->_ref_stream,v,-1)){ + NyLPC_OnErrorGoto(Error); + } + } + //送信サイズをリセット + i_inst->_size_of_sent=0; + return NyLPC_TBool_TRUE; Error: - i_inst->_is_error=NyLPC_TUInt8_FALSE; - return NyLPC_TBool_FALSE; + i_inst->_is_error=NyLPC_TUInt8_FALSE; + return NyLPC_TBool_FALSE; } @@ -196,59 +196,59 @@ Error: */ NyLPC_TBool NyLPC_cHttpHeaderWriter_writeResponseHeader(NyLPC_TcHttpHeaderWriter_t* i_inst,NyLPC_TUInt16 i_status) { - NyLPC_TChar v[12]; - const char* m=getStatusMessage(i_status); - //エラー状態ならなにもしない。 - if(i_inst->_is_error){ - return NyLPC_TBool_FALSE; - } - //検索に失敗したら500番に変更 - if(m==NULL){ - i_status=500; - m=getStatusMessage(500); - } - //ステータスラインの記述 - if(!NyLPC_iHttpPtrStream_write(i_inst->_ref_stream,"HTTP/1.1 ",9)){ - NyLPC_OnErrorGoto(Error); - } - NyLPC_itoa(i_status,v,10); - if(!NyLPC_iHttpPtrStream_write(i_inst->_ref_stream,v,-1)){ - NyLPC_OnErrorGoto(Error); - } - if(!NyLPC_iHttpPtrStream_write(i_inst->_ref_stream," ",1)){ - NyLPC_OnErrorGoto(Error); - } - if(!writeln(i_inst->_ref_stream,m,-1)){ - NyLPC_OnErrorGoto(Error); - } - if(!NyLPC_iHttpPtrStream_write(i_inst->_ref_stream,"Server: " NyLPC_cHttpdConfig_SERVER "\r\n",-1)){ - NyLPC_OnErrorGoto(Error); - } - if(i_inst->_is_close){ - if(!NyLPC_iHttpPtrStream_write(i_inst->_ref_stream,"Connection: CLOSE\r\n",-1)){ - NyLPC_OnErrorGoto(Error); - } - } - //ヘッダの記述 - if(i_inst->_is_chunked){ - if(!NyLPC_iHttpPtrStream_write(i_inst->_ref_stream,"Transfer-Encoding: chunked\r\n",-1)){ - NyLPC_OnErrorGoto(Error); - } - }else{ - if(!NyLPC_iHttpPtrStream_write(i_inst->_ref_stream,"Content-Length: ",-1)){ - NyLPC_OnErrorGoto(Error); - } - NyLPC_uitoa(i_inst->_content_length,v,10); - if(!writeln(i_inst->_ref_stream,v,-1)){ - NyLPC_OnErrorGoto(Error); - } - } - //送信サイズをリセット - i_inst->_size_of_sent=0; - return NyLPC_TBool_TRUE; + NyLPC_TChar v[12]; + const char* m=getStatusMessage(i_status); + //エラー状態ならなにもしない。 + if(i_inst->_is_error){ + return NyLPC_TBool_FALSE; + } + //検索に失敗したら500番に変更 + if(m==NULL){ + i_status=500; + m=getStatusMessage(500); + } + //ステータスラインの記述 + if(!NyLPC_iHttpPtrStream_write(i_inst->_ref_stream,"HTTP/1.1 ",9)){ + NyLPC_OnErrorGoto(Error); + } + NyLPC_itoa(i_status,v,10); + if(!NyLPC_iHttpPtrStream_write(i_inst->_ref_stream,v,-1)){ + NyLPC_OnErrorGoto(Error); + } + if(!NyLPC_iHttpPtrStream_write(i_inst->_ref_stream," ",1)){ + NyLPC_OnErrorGoto(Error); + } + if(!writeln(i_inst->_ref_stream,m,-1)){ + NyLPC_OnErrorGoto(Error); + } + if(!NyLPC_iHttpPtrStream_write(i_inst->_ref_stream,"Server: " NyLPC_cHttpdConfig_SERVER "\r\n",-1)){ + NyLPC_OnErrorGoto(Error); + } + if(i_inst->_is_close){ + if(!NyLPC_iHttpPtrStream_write(i_inst->_ref_stream,"Connection: CLOSE\r\n",-1)){ + NyLPC_OnErrorGoto(Error); + } + } + //ヘッダの記述 + if(i_inst->_is_chunked){ + if(!NyLPC_iHttpPtrStream_write(i_inst->_ref_stream,"Transfer-Encoding: chunked\r\n",-1)){ + NyLPC_OnErrorGoto(Error); + } + }else{ + if(!NyLPC_iHttpPtrStream_write(i_inst->_ref_stream,"Content-Length: ",-1)){ + NyLPC_OnErrorGoto(Error); + } + NyLPC_uitoa(i_inst->_content_length,v,10); + if(!writeln(i_inst->_ref_stream,v,-1)){ + NyLPC_OnErrorGoto(Error); + } + } + //送信サイズをリセット + i_inst->_size_of_sent=0; + return NyLPC_TBool_TRUE; Error: - i_inst->_is_error=NyLPC_TUInt8_FALSE; - return NyLPC_TBool_FALSE; + i_inst->_is_error=NyLPC_TUInt8_FALSE; + return NyLPC_TBool_FALSE; } /** @@ -256,39 +256,39 @@ Error: */ NyLPC_TBool NyLPC_cHttpHeaderWriter_writeMessage(NyLPC_TcHttpHeaderWriter_t* i_inst,const NyLPC_TChar* i_name,const NyLPC_TChar* i_field) { - //エラー状態ならなにもしない。 - if(i_inst->_is_error){ - return NyLPC_TBool_FALSE; - } - if(!NyLPC_iHttpPtrStream_write(i_inst->_ref_stream,i_name,-1)){ - NyLPC_OnErrorGoto(Error); - } - if(!NyLPC_iHttpPtrStream_write(i_inst->_ref_stream,": ",2)){ - NyLPC_OnErrorGoto(Error); - } - if(!writeln(i_inst->_ref_stream,i_field,-1)){ - NyLPC_OnErrorGoto(Error); - } - return NyLPC_TBool_TRUE; + //エラー状態ならなにもしない。 + if(i_inst->_is_error){ + return NyLPC_TBool_FALSE; + } + if(!NyLPC_iHttpPtrStream_write(i_inst->_ref_stream,i_name,-1)){ + NyLPC_OnErrorGoto(Error); + } + if(!NyLPC_iHttpPtrStream_write(i_inst->_ref_stream,": ",2)){ + NyLPC_OnErrorGoto(Error); + } + if(!writeln(i_inst->_ref_stream,i_field,-1)){ + NyLPC_OnErrorGoto(Error); + } + return NyLPC_TBool_TRUE; Error: - i_inst->_is_error=NyLPC_TUInt8_FALSE; - return NyLPC_TBool_FALSE; + i_inst->_is_error=NyLPC_TUInt8_FALSE; + return NyLPC_TBool_FALSE; } NyLPC_TBool NyLPC_cHttpHeaderWriter_writeRawMessage(NyLPC_TcHttpHeaderWriter_t* i_inst,const NyLPC_TChar* i_additional_header) { - //エラー状態ならなにもしない。 - if(i_inst->_is_error){ - return NyLPC_TBool_FALSE; - } - if(!NyLPC_iHttpPtrStream_write(i_inst->_ref_stream,i_additional_header,strlen(i_additional_header))){ - NyLPC_OnErrorGoto(Error); - } - return NyLPC_TBool_TRUE; + //エラー状態ならなにもしない。 + if(i_inst->_is_error){ + return NyLPC_TBool_FALSE; + } + if(!NyLPC_iHttpPtrStream_write(i_inst->_ref_stream,i_additional_header,strlen(i_additional_header))){ + NyLPC_OnErrorGoto(Error); + } + return NyLPC_TBool_TRUE; Error: - i_inst->_is_error=NyLPC_TUInt8_FALSE; - return NyLPC_TBool_FALSE; + i_inst->_is_error=NyLPC_TUInt8_FALSE; + return NyLPC_TBool_FALSE; } /** @@ -299,16 +299,16 @@ Error: */ NyLPC_TBool NyLPC_cHttpHeaderWriter_close(NyLPC_TcHttpHeaderWriter_t* i_inst) { - //エラー状態ならなにもしない。 - if(i_inst->_is_error){ - return NyLPC_TBool_FALSE; - } - if(NyLPC_iHttpPtrStream_write(i_inst->_ref_stream,"\r\n",2)){ - if( NyLPC_iHttpPtrStream_flush(i_inst->_ref_stream)){ - return NyLPC_TBool_TRUE; - } - } - return NyLPC_TBool_FALSE; + //エラー状態ならなにもしない。 + if(i_inst->_is_error){ + return NyLPC_TBool_FALSE; + } + if(NyLPC_iHttpPtrStream_write(i_inst->_ref_stream,"\r\n",2)){ + if( NyLPC_iHttpPtrStream_flush(i_inst->_ref_stream)){ + return NyLPC_TBool_TRUE; + } + } + return NyLPC_TBool_FALSE; } diff --git a/lib/src/http/NyLPC_cHttpHeaderWriter.h b/lib/src/http/NyLPC_cHttpHeaderWriter.h index 2b9d8ff..a4507df 100644 --- a/lib/src/http/NyLPC_cHttpHeaderWriter.h +++ b/lib/src/http/NyLPC_cHttpHeaderWriter.h @@ -19,15 +19,15 @@ * along with this program. If not, see . * * For further information please contact. - * http://nyatla.jp/ - * or + * http://nyatla.jp/ + * or * *********************************************************************************/ #ifndef NYLPC_CHTTPHEADERWRITER_H_ #define NYLPC_CHTTPHEADERWRITER_H_ #include "NyLPC_stdlib.h" -#include "NyLPC_uipService.h" +#include "NyLPC_netif.h" #include "NyLPC_cHttpStream.h" #include "NyLPC_cHttpBasicHeaderParser.h" #include "NyLPC_cHttpdConfig.h" @@ -40,15 +40,15 @@ extern "C" { typedef struct NyLPC_TcHttpHeaderWriter NyLPC_TcHttpHeaderWriter_t; struct NyLPC_TcHttpHeaderWriter { - NyLPC_TUInt8 _is_chunked; - NyLPC_TUInt8 _is_close; - //異常状態をチェックするためのフラグ - NyLPC_TUInt8 _is_error; - - NyLPC_TUInt32 _content_length; - NyLPC_TUInt32 _size_of_sent; + NyLPC_TUInt8 _is_chunked; + NyLPC_TUInt8 _is_close; + //異常状態をチェックするためのフラグ + NyLPC_TUInt8 _is_error; + + NyLPC_TUInt32 _content_length; + NyLPC_TUInt32 _size_of_sent; - NyLPC_TiHttpPtrStream_t* _ref_stream; + NyLPC_TiHttpPtrStream_t* _ref_stream; }; /** diff --git a/lib/src/http/NyLPC_cHttpNullRequestHeaderParser.c b/lib/src/http/NyLPC_cHttpNullRequestHeaderParser.c index a49b28f..a58f88c 100644 --- a/lib/src/http/NyLPC_cHttpNullRequestHeaderParser.c +++ b/lib/src/http/NyLPC_cHttpNullRequestHeaderParser.c @@ -19,8 +19,8 @@ * along with this program. If not, see . * * For further information please contact. - * http://nyatla.jp/ - * or + * http://nyatla.jp/ + * or * *********************************************************************************/ #include "NyLPC_cHttpNullRequestHeaderParser.h" @@ -31,11 +31,11 @@ */ static const struct NyLPC_TcHttpBasicHeaderParser_Handler _handler= { - NULL,NULL + NULL,NULL }; void NyLPC_cHttpNullRequestHeaderParser_initialize(NyLPC_TcHttpNullRequestHeaderParser_t* i_inst) { - NyLPC_cHttpBasicHeaderParser_initialize(&(i_inst->super),&_handler); + NyLPC_cHttpBasicHeaderParser_initialize(&(i_inst->super),&_handler); } diff --git a/lib/src/http/NyLPC_cHttpNullRequestHeaderParser.h b/lib/src/http/NyLPC_cHttpNullRequestHeaderParser.h index d2175aa..9e80d5a 100644 --- a/lib/src/http/NyLPC_cHttpNullRequestHeaderParser.h +++ b/lib/src/http/NyLPC_cHttpNullRequestHeaderParser.h @@ -19,8 +19,8 @@ * along with this program. If not, see . * * For further information please contact. - * http://nyatla.jp/ - * or + * http://nyatla.jp/ + * or * *********************************************************************************/ #ifndef NYLPC_CHTTPNULLHTTPHEADERPARSER_H_ @@ -40,8 +40,8 @@ typedef struct NyLPC_TcHttpNullReqestHeaderParser NyLPC_TcHttpNullRequestHeaderP struct NyLPC_TcHttpNullReqestHeaderParser{ - NyLPC_TcHttpBasicHeaderParser_t super; - struct NyLPC_THttpBasicHeader _header; + NyLPC_TcHttpBasicHeaderParser_t super; + struct NyLPC_THttpBasicHeader _header; }; void NyLPC_cHttpNullRequestHeaderParser_initialize(NyLPC_TcHttpNullRequestHeaderParser_t* i_inst); diff --git a/lib/src/http/NyLPC_cHttpStream.c b/lib/src/http/NyLPC_cHttpStream.c index e359921..7affe7e 100644 --- a/lib/src/http/NyLPC_cHttpStream.c +++ b/lib/src/http/NyLPC_cHttpStream.c @@ -19,8 +19,8 @@ * along with this program. If not, see . * * For further information please contact. - * http://nyatla.jp/ - * or + * http://nyatla.jp/ + * or * *********************************************************************************/ #include "NyLPC_cHttpStream.h" @@ -32,37 +32,37 @@ char _wbuf[1024]; const char* _rbuf; int _rbuf_len; -void NyLPC_cTcpSocket_initialized(void* inst,const char* rb,int l) +void NyLPC_iTcpSocket_initialized(void* inst,const char* rb,int l) { - _rbuf=rb; - _rbuf_len=l; + _rbuf=rb; + _rbuf_len=l; } -void* NyLPC_cTcpSocket_allocSendBuf(void* inst,NyLPC_TUInt16 i_hint,NyLPC_TUInt16* o_len,NyLPC_TUInt32 i_to) +void* NyLPC_iIpTcpSocket_allocSendBuf(void* inst,NyLPC_TUInt16 i_hint,NyLPC_TUInt16* o_len,NyLPC_TUInt32 i_to) { - *o_len=30; - return _wbuf; + *o_len=30; + return _wbuf; } -NyLPC_TBool NyLPC_cTcpSocket_psend(void* inst,void* i_buf,NyLPC_TUInt16 i_len,NyLPC_TUInt32 i_to) +NyLPC_TBool NyLPC_iTcpSocket_psend(void* inst,void* i_buf,NyLPC_TUInt16 i_len,NyLPC_TUInt32 i_to) { - printf("%.*s",i_len,i_buf); - return NyLPC_TBool_TRUE; + printf("%.*s",i_len,i_buf); + return NyLPC_TBool_TRUE; } -NyLPC_TInt32 NyLPC_cTcpSocket_precv(void* i_inst,const void** o_buf_ptr,NyLPC_TUInt32 i_wait_msec) +NyLPC_TInt32 NyLPC_iTcpSocket_precv(void* i_inst,const void** o_buf_ptr,NyLPC_TUInt32 i_wait_msec) { - int l=(_rbuf_len>100)?100:_rbuf_len; - *o_buf_ptr=_rbuf; - return l; + int l=(_rbuf_len>100)?100:_rbuf_len; + *o_buf_ptr=_rbuf; + return l; } -void NyLPC_cTcpSocket_pseek(void* i_inst,NyLPC_TUInt16 i_seek) +void NyLPC_iTcpSocket_pseek(void* i_inst,NyLPC_TUInt16 i_seek) { - _rbuf+=i_seek; - _rbuf_len-=i_seek; + _rbuf+=i_seek; + _rbuf_len-=i_seek; } -void* NyLPC_cTcpSocket_releaseSendBuf(NyLPC_TcTcpSocket_t* i_inst,void* i_buf_ptr) +void* NyLPC_iTcpSocket_releaseSendBuf(NyLPC_TiTcpSocket_t* i_inst,void* i_buf_ptr) { - return NULL; + return NULL; } #endif @@ -83,12 +83,12 @@ static void setWriteEncoding_func(void* i_inst,NyLPC_TiHttpPtrStream_ET i_et); //関数テーブル const static struct NyLPC_TiHttpPtrStream_TInterface _interface= { - pread_func, - write_func, - pseek_func, - flush_func, - setReadEncoding_func, - setWriteEncoding_func + pread_func, + write_func, + pseek_func, + flush_func, + setReadEncoding_func, + setWriteEncoding_func }; /** @@ -96,95 +96,95 @@ const static struct NyLPC_TiHttpPtrStream_TInterface _interface= */ static void put_chunked_header(NyLPC_TUInt16 i_val,NyLPC_TUInt8* o_buf) { - const static char* D="0123456789ABCDEF"; - *(o_buf+0)=D[((i_val&0x0f00)>>8)]; - *(o_buf+1)=D[((i_val&0x00f0)>>4)]; - *(o_buf+2)=D[ (i_val&0x000f)]; - *(o_buf+3)='\r'; - *(o_buf+4)='\n'; + const static char* D="0123456789ABCDEF"; + *(o_buf+0)=D[((i_val&0x0f00)>>8)]; + *(o_buf+1)=D[((i_val&0x00f0)>>4)]; + *(o_buf+2)=D[ (i_val&0x000f)]; + *(o_buf+3)='\r'; + *(o_buf+4)='\n'; } /** * 接続済のソケットをストリームに抽象化します。 */ -NyLPC_TBool NyLPC_cHttpStream_initialize(NyLPC_TcHttpStream_t* i_inst,NyLPC_TcTcpSocket_t* i_ref_sock) -{ - i_inst->super.absfunc=&_interface; - i_inst->_ref_sock=i_ref_sock; - i_inst->we_type=NyLPC_TiHttpPtrStream_ET_NONE; - i_inst->re_type=NyLPC_TiHttpPtrStream_ET_NONE; - i_inst->txb=NULL; - return NyLPC_TBool_TRUE; +NyLPC_TBool NyLPC_cHttpStream_initialize(NyLPC_TcHttpStream_t* i_inst,NyLPC_TiTcpSocket_t* i_ref_sock) +{ + i_inst->super.absfunc=&_interface; + i_inst->_ref_sock=i_ref_sock; + i_inst->we_type=NyLPC_TiHttpPtrStream_ET_NONE; + i_inst->re_type=NyLPC_TiHttpPtrStream_ET_NONE; + i_inst->txb=NULL; + return NyLPC_TBool_TRUE; } void NyLPC_cHttpStream_finalize(NyLPC_TcHttpStream_t* i_inst) { - if(i_inst->txb!=NULL){ - NyLPC_cTcpSocket_releaseSendBuf(i_inst->_ref_sock,i_inst->txb); - } + if(i_inst->txb!=NULL){ + NyLPC_iTcpSocket_releaseSendBuf(i_inst->_ref_sock,i_inst->txb); + } } // -// インタフェイス +// インタフェイス // static NyLPC_TInt32 pread_func(void* i_inst,const void** o_buf_ptr,NyLPC_TUInt32 i_timeout) { - NyLPC_TcHttpStream_t* inst=(NyLPC_TcHttpStream_t*)i_inst; - return NyLPC_cTcpSocket_precv(inst->_ref_sock,o_buf_ptr,i_timeout); + NyLPC_TcHttpStream_t* inst=(NyLPC_TcHttpStream_t*)i_inst; + return NyLPC_iTcpSocket_precv(inst->_ref_sock,o_buf_ptr,i_timeout); } static NyLPC_TBool write_func(void* i_inst,const void* i_data,NyLPC_TInt32 i_length) { - NyLPC_TcHttpStream_t* inst=(NyLPC_TcHttpStream_t*)i_inst; - NyLPC_TUInt16 s,free_size; - NyLPC_TUInt32 l; - const char* src=(const char*)i_data; - l=((i_length<0)?strlen(src):i_length); - while(l>0){ - //送信バッファがNULLなら、割り当て。 - if(inst->txb==NULL){ - inst->txb=(NyLPC_TUInt8*)NyLPC_cTcpSocket_allocSendBuf(inst->_ref_sock,HTTP_TX_BUF_HINT,&s,NyLPC_TiHttpPtrStream_DEFAULT_HTTP_TIMEOUT); - if(inst->txb==NULL){ - return NyLPC_TBool_FALSE; - } - //chunked encodingなら、先頭5バイト+末尾2バイトを予約する. 000 - if(inst->we_type==NyLPC_TiHttpPtrStream_ET_CHUNKED){ - inst->tx_len=5; - inst->txb_size=s-7; - }else{ - inst->tx_len=0; - inst->txb_size=s; - } - } - //書き込み可能サイズの計算 - free_size=inst->txb_size-inst->tx_len; - if((NyLPC_TInt32)free_size>l){ - //書き込み可能サイズがi_length未満なら、バッファに貯めるだけ。 - memcpy(inst->txb+inst->tx_len,src,l); - inst->tx_len+=(NyLPC_TUInt16)l; - break; - } - //バッファフルになるなら、送信する。 - memcpy(inst->txb+inst->tx_len,src,free_size); - inst->tx_len+=free_size; - //書き込み - if(!flush_func(i_inst)){ - //書込みエラー・・・ - return NyLPC_TBool_FALSE; - } - //読み出し位置の調整 - l-=free_size; - src+=free_size; - }; - return NyLPC_TBool_TRUE; + NyLPC_TcHttpStream_t* inst=(NyLPC_TcHttpStream_t*)i_inst; + NyLPC_TUInt16 s,free_size; + NyLPC_TUInt32 l; + const char* src=(const char*)i_data; + l=((i_length<0)?strlen(src):i_length); + while(l>0){ + //送信バッファがNULLなら、割り当て。 + if(inst->txb==NULL){ + inst->txb=(NyLPC_TUInt8*)NyLPC_iTcpSocket_allocSendBuf(inst->_ref_sock,HTTP_TX_BUF_HINT,&s,NyLPC_TiHttpPtrStream_DEFAULT_HTTP_TIMEOUT); + if(inst->txb==NULL){ + return NyLPC_TBool_FALSE; + } + //chunked encodingなら、先頭5バイト+末尾2バイトを予約する. 000 + if(inst->we_type==NyLPC_TiHttpPtrStream_ET_CHUNKED){ + inst->tx_len=5; + inst->txb_size=s-7; + }else{ + inst->tx_len=0; + inst->txb_size=s; + } + } + //書き込み可能サイズの計算 + free_size=inst->txb_size-inst->tx_len; + if((NyLPC_TInt32)free_size>l){ + //書き込み可能サイズがi_length未満なら、バッファに貯めるだけ。 + memcpy(inst->txb+inst->tx_len,src,l); + inst->tx_len+=(NyLPC_TUInt16)l; + break; + } + //バッファフルになるなら、送信する。 + memcpy(inst->txb+inst->tx_len,src,free_size); + inst->tx_len+=free_size; + //書き込み + if(!flush_func(i_inst)){ + //書込みエラー・・・ + return NyLPC_TBool_FALSE; + } + //読み出し位置の調整 + l-=free_size; + src+=free_size; + }; + return NyLPC_TBool_TRUE; } static void pseek_func(void* i_inst,NyLPC_TUInt16 i_seek) { - NyLPC_TcHttpStream_t* inst=(NyLPC_TcHttpStream_t*)i_inst; + NyLPC_TcHttpStream_t* inst=(NyLPC_TcHttpStream_t*)i_inst; - NyLPC_cTcpSocket_pseek(inst->_ref_sock,i_seek); + NyLPC_iTcpSocket_pseek(inst->_ref_sock,i_seek); } /** @@ -192,48 +192,48 @@ static void pseek_func(void* i_inst,NyLPC_TUInt16 i_seek) */ static NyLPC_TBool flush_func(void* i_inst) { - NyLPC_TcHttpStream_t* inst=(NyLPC_TcHttpStream_t*)i_inst; - if(inst->txb==NULL){ - return NyLPC_TBool_TRUE; - } - //chunkedの場合は、header/footerをセットする。 - if(inst->we_type==NyLPC_TiHttpPtrStream_ET_CHUNKED){ - //5バイト分のヘッダを記述。 - put_chunked_header(inst->tx_len-5,inst->txb); - *(inst->txb+inst->tx_len)=0x0d; - *(inst->txb+inst->tx_len+1)=0x0a; - inst->tx_len+=2; - } - //送信する。 - if(!NyLPC_cTcpSocket_psend(inst->_ref_sock,inst->txb,inst->tx_len,NyLPC_TiHttpPtrStream_DEFAULT_HTTP_TIMEOUT)){ - //失敗。 - NyLPC_cTcpSocket_releaseSendBuf(inst->_ref_sock,inst->txb); - inst->txb=NULL; - return NyLPC_TBool_FALSE; - } - //キャッシュを開放 - inst->txb=NULL; - return NyLPC_TBool_TRUE; + NyLPC_TcHttpStream_t* inst=(NyLPC_TcHttpStream_t*)i_inst; + if(inst->txb==NULL){ + return NyLPC_TBool_TRUE; + } + //chunkedの場合は、header/footerをセットする。 + if(inst->we_type==NyLPC_TiHttpPtrStream_ET_CHUNKED){ + //5バイト分のヘッダを記述。 + put_chunked_header(inst->tx_len-5,inst->txb); + *(inst->txb+inst->tx_len)=0x0d; + *(inst->txb+inst->tx_len+1)=0x0a; + inst->tx_len+=2; + } + //送信する。 + if(!NyLPC_iTcpSocket_psend(inst->_ref_sock,inst->txb,inst->tx_len,NyLPC_TiHttpPtrStream_DEFAULT_HTTP_TIMEOUT)){ + //失敗。 + NyLPC_iTcpSocket_releaseSendBuf(inst->_ref_sock,inst->txb); + inst->txb=NULL; + return NyLPC_TBool_FALSE; + } + //キャッシュを開放 + inst->txb=NULL; + return NyLPC_TBool_TRUE; } static void setReadEncoding_func(void* i_inst,NyLPC_TiHttpPtrStream_ET i_et) { - //未実装。(この関数は不要?) - NyLPC_Abort(); - return; + //未実装。(この関数は不要?) + NyLPC_Abort(); + return; } static void setWriteEncoding_func(void* i_inst,NyLPC_TiHttpPtrStream_ET i_et) { - NyLPC_TcHttpStream_t* inst=(NyLPC_TcHttpStream_t*)i_inst; - if(inst->we_type==i_et) - { - return; - } - //バッファがあるならフラッシュしてしまう。 - if(inst->txb!=NULL){ - if(!flush_func(i_inst)){ - return; - } - } - //モードの切り替え。 - inst->we_type=i_et; + NyLPC_TcHttpStream_t* inst=(NyLPC_TcHttpStream_t*)i_inst; + if(inst->we_type==i_et) + { + return; + } + //バッファがあるならフラッシュしてしまう。 + if(inst->txb!=NULL){ + if(!flush_func(i_inst)){ + return; + } + } + //モードの切り替え。 + inst->we_type=i_et; } diff --git a/lib/src/http/NyLPC_cHttpStream.h b/lib/src/http/NyLPC_cHttpStream.h index 07f48b1..6d8a044 100644 --- a/lib/src/http/NyLPC_cHttpStream.h +++ b/lib/src/http/NyLPC_cHttpStream.h @@ -19,8 +19,8 @@ * along with this program. If not, see . * * For further information please contact. - * http://nyatla.jp/ - * or + * http://nyatla.jp/ + * or * *********************************************************************************/ #ifndef NyLPC_TcHttpStream_h @@ -36,19 +36,19 @@ extern "C" { #endif /* __cplusplus */ -typedef int NyLPC_TcTcpSocket_t; -void NyLPC_cTcpSocket_initialized(void* inst,const char* rb,int l); -void* NyLPC_cTcpSocket_allocSendBuf(void* inst,NyLPC_TUInt16 i_hint,NyLPC_TUInt16* o_len,NyLPC_TUInt32 i_to); -NyLPC_TBool NyLPC_cTcpSocket_psend(void* inst,void* i_buf,NyLPC_TUInt16 i_len,NyLPC_TUInt32 i_to); -NyLPC_TInt32 NyLPC_cTcpSocket_precv(void* i_inst,const void** o_buf_ptr,NyLPC_TUInt32 i_wait_msec); -void NyLPC_cTcpSocket_pseek(void* i_inst,NyLPC_TUInt16 i_seek); +typedef int NyLPC_TiMiMicIpTcpSocket_t; +void NyLPC_iTcpSocket_initialized(void* inst,const char* rb,int l); +void* NyLPC_iTcpSocket_allocSendBuf(void* inst,NyLPC_TUInt16 i_hint,NyLPC_TUInt16* o_len,NyLPC_TUInt32 i_to); +NyLPC_TBool NyLPC_iTcpSocket_psend(void* inst,void* i_buf,NyLPC_TUInt16 i_len,NyLPC_TUInt32 i_to); +NyLPC_TInt32 NyLPC_iTcpSocket_precv(void* i_inst,const void** o_buf_ptr,NyLPC_TUInt32 i_wait_msec); +void NyLPC_iTcpSocket_pseek(void* i_inst,NyLPC_TUInt16 i_seek); #ifdef __cplusplus } #endif /* __cplusplus */ #else -#include "../uip/NyLPC_cTcpSocket.h" +#include "../netif/NyLPC_iTcpSocket.h" #endif #ifdef __cplusplus @@ -64,13 +64,13 @@ typedef struct NyLPC_TcHttpStream NyLPC_TcHttpStream_t; struct NyLPC_TcHttpStream { - NyLPC_TiHttpPtrStream_t super; - NyLPC_TcTcpSocket_t* _ref_sock; - NyLPC_TUInt8* txb;//送信バッファ - NyLPC_TUInt16 txb_size;//送信バッファサイズ - NyLPC_TUInt16 tx_len; //送信サイズ - NyLPC_TiHttpPtrStream_ET re_type; - NyLPC_TiHttpPtrStream_ET we_type; + NyLPC_TiHttpPtrStream_t super; + NyLPC_TiTcpSocket_t* _ref_sock; + NyLPC_TUInt8* txb;//送信バッファ + NyLPC_TUInt16 txb_size;//送信バッファサイズ + NyLPC_TUInt16 tx_len; //送信サイズ + NyLPC_TiHttpPtrStream_ET re_type; + NyLPC_TiHttpPtrStream_ET we_type; }; @@ -80,7 +80,7 @@ struct NyLPC_TcHttpStream * このインスタンスは、NyLPC_TiHttpPtrStream_TInterfaceインタフェイスを提供します。 * @ */ -NyLPC_TBool NyLPC_cHttpStream_initialize(NyLPC_TcHttpStream_t* i_inst,NyLPC_TcTcpSocket_t* i_ref_sock); +NyLPC_TBool NyLPC_cHttpStream_initialize(NyLPC_TcHttpStream_t* i_inst,NyLPC_TiTcpSocket_t* i_ref_sock); void NyLPC_cHttpStream_finalize(NyLPC_TcHttpStream_t* i_inst); diff --git a/lib/src/http/NyLPC_cHttpdConfig.h b/lib/src/http/NyLPC_cHttpdConfig.h index 32a13c9..be29849 100644 --- a/lib/src/http/NyLPC_cHttpdConfig.h +++ b/lib/src/http/NyLPC_cHttpdConfig.h @@ -19,8 +19,8 @@ * along with this program. If not, see . * * For further information please contact. - * http://nyatla.jp/ - * or + * http://nyatla.jp/ + * or * *********************************************************************************/ #ifndef NYLPC_CHTTPDCONFIG_H_ diff --git a/lib/src/http/NyLPC_cJsonWeiter.c b/lib/src/http/NyLPC_cJsonWeiter.c index ddc21f9..26347d1 100644 --- a/lib/src/http/NyLPC_cJsonWeiter.c +++ b/lib/src/http/NyLPC_cJsonWeiter.c @@ -9,65 +9,65 @@ typedef NyLPC_TUInt8 NyLPC_NyLPC_TcJsonWeiter_ST; typedef struct NyLPC_TcJsonWeiter NyLPC_TcJsonWeiter_t; struct NyLPC_TcJsonWeiter { - NyLPC_NyLPC_TcJsonWeiter_ST _stack[8]; - NyLPC_TUInt8 _stack_level; - NyLPC_TBool _noerror; + NyLPC_NyLPC_TcJsonWeiter_ST _stack[8]; + NyLPC_TUInt8 _stack_level; + NyLPC_TBool _noerror; }; void NyLPC_cJsonWeiter_initialize(NyLPC_TcJsonWeiter_t* i_inst) { - i_inst->_stack_level=0; + i_inst->_stack_level=0; } NyLPC_TBool NyLPC_cJsonWeiter_putNode(NyLPC_TcJsonWeiter_t* i_inst,const NyLPC_TChar* i_name,const NyLPC_TChar* i_value) { - NyLPC_cHttpHeaderWriter_writeBody(NULL,i_name); - NyLPC_cHttpHeaderWriter_writeBody(NULL,":"); - NyLPC_cHttpHeaderWriter_writeBody(NULL,i_value); - NyLPC_cHttpHeaderWriter_writeBody(NULL,","); + NyLPC_cHttpHeaderWriter_writeBody(NULL,i_name); + NyLPC_cHttpHeaderWriter_writeBody(NULL,":"); + NyLPC_cHttpHeaderWriter_writeBody(NULL,i_value); + NyLPC_cHttpHeaderWriter_writeBody(NULL,","); } NyLPC_TBool NyLPC_cJsonWeiter_putStrNode(NyLPC_TcJsonWeiter_t* i_inst,const NyLPC_TChar* i_name,const NyLPC_TChar* i_value) { - NyLPC_cHttpHeaderWriter_writeBody(NULL,i_name); - NyLPC_cHttpHeaderWriter_writeBody(NULL,":\""); - NyLPC_cHttpHeaderWriter_writeBody(NULL,i_value); - NyLPC_cHttpHeaderWriter_writeBody(NULL,"\","); + NyLPC_cHttpHeaderWriter_writeBody(NULL,i_name); + NyLPC_cHttpHeaderWriter_writeBody(NULL,":\""); + NyLPC_cHttpHeaderWriter_writeBody(NULL,i_value); + NyLPC_cHttpHeaderWriter_writeBody(NULL,"\","); } NyLPC_TBool NyLPC_cJsonWeiter_putIntNode(NyLPC_TcJsonWeiter_t* i_inst,const NyLPC_TChar* i_name,const NyLPC_TInt32 i_value,int i_base) { - NyLPC_TChar v[12]; - if(!i_inst->_noerror){ - return NyLPC_TBool_FALSE; - } - itoa(i_value,v,i_base); - i_inst->_noerror=NyLPC_cJsonWeiter_putNode(i_inst,i_name,v); - return i_inst->_noerror; + NyLPC_TChar v[12]; + if(!i_inst->_noerror){ + return NyLPC_TBool_FALSE; + } + itoa(i_value,v,i_base); + i_inst->_noerror=NyLPC_cJsonWeiter_putNode(i_inst,i_name,v); + return i_inst->_noerror; } NyLPC_TBool NyLPC_cJsonWeiter_putBoolNode(NyLPC_TcJsonWeiter_t* i_inst,const NyLPC_TChar* i_name,NyLPC_TBool i_value) { - if(!i_inst->_noerror){ - return NyLPC_TBool_FALSE; - } - i_inst->_noerror=NyLPC_cJsonWeiter_putNode(i_inst,i_name,i_value?"true":"false"); - return i_inst->_noerror; + if(!i_inst->_noerror){ + return NyLPC_TBool_FALSE; + } + i_inst->_noerror=NyLPC_cJsonWeiter_putNode(i_inst,i_name,i_value?"true":"false"); + return i_inst->_noerror; } NyLPC_TBool NyLPC_cJsonWeiter_startNode(NyLPC_TcJsonWeiter_t* i_inst,const NyLPC_TChar* i_name) { - sprintf - NyLPC_cHttpHeaderWriter_writeBody(NULL,i_name); - NyLPC_cHttpHeaderWriter_writeBody(NULL,":{"); + sprintf + NyLPC_cHttpHeaderWriter_writeBody(NULL,i_name); + NyLPC_cHttpHeaderWriter_writeBody(NULL,":{"); } NyLPC_TBool NyLPC_cJsonWeiter_endNode(NyLPC_TcJsonWeiter_t* i_inst) { - NyLPC_cHttpHeaderWriter_writeBody(NULL,"},"); + NyLPC_cHttpHeaderWriter_writeBody(NULL,"},"); } NyLPC_TBool NyLPC_cJsonWeiter_startArray(NyLPC_TcJsonWeiter_t* i_inst,const NyLPC_TChar* i_name) { - NyLPC_cHttpHeaderWriter_writeBody(NULL,i_name); - NyLPC_cHttpHeaderWriter_writeBody(NULL,":["); + NyLPC_cHttpHeaderWriter_writeBody(NULL,i_name); + NyLPC_cHttpHeaderWriter_writeBody(NULL,":["); } NyLPC_TBool NyLPC_cJsonWeiter_endArray(NyLPC_TcJsonWeiter_t* i_inst,const NyLPC_TChar* i_name) { - NyLPC_cHttpHeaderWriter_writeBody(NULL,"],"); + NyLPC_cHttpHeaderWriter_writeBody(NULL,"],"); }*/ diff --git a/lib/src/http/NyLPC_cMimeType.c b/lib/src/http/NyLPC_cMimeType.c index 176ef98..4799cbf 100644 --- a/lib/src/http/NyLPC_cMimeType.c +++ b/lib/src/http/NyLPC_cMimeType.c @@ -1,36 +1,36 @@ #include #include "NyLPC_cMimeType.h" struct TMimeTypeTable{ - const char* ext; - const char* mimetype; + const char* ext; + const char* mimetype; }; const static struct TMimeTypeTable table[]= { - {"zip" ,"application/zip"}, - {"js" ,"application/x-javascript"}, - {"txt" ,"text/plain"}, - {"html","text/html"}, - {"htm","text/html"}, - {"css" ,"text/css"}, - {"jpeg","image/jpeg"}, - {"jpg" ,"image/jpeg"}, - {"png" ,"image/png"}, - {"gif" ,"image/gif"}, - {NULL,NULL} + {"zip" ,"application/zip"}, + {"js" ,"application/x-javascript"}, + {"txt" ,"text/plain"}, + {"html","text/html"}, + {"htm","text/html"}, + {"css" ,"text/css"}, + {"jpeg","image/jpeg"}, + {"jpg" ,"image/jpeg"}, + {"png" ,"image/png"}, + {"gif" ,"image/gif"}, + {NULL,NULL} }; const static char* default_mimetype="application/octet-stream"; const char* NyLPC_cMiMeType_getFileName2MimeType(const char* i_file_name) { - int i; - const char* p=strrchr(i_file_name,'.'); - if(p==NULL){ - return default_mimetype; - } - for(i=0;table[i].ext!=NULL;i++){ - if(NyLPC_stricmp(table[i].ext,p+1)==0){ - return table[i].mimetype; - } - } - return default_mimetype; + int i; + const char* p=strrchr(i_file_name,'.'); + if(p==NULL){ + return default_mimetype; + } + for(i=0;table[i].ext!=NULL;i++){ + if(NyLPC_stricmp(table[i].ext,p+1)==0){ + return table[i].mimetype; + } + } + return default_mimetype; } diff --git a/lib/src/http/NyLPC_cMimeType.h b/lib/src/http/NyLPC_cMimeType.h index 2e06abb..248cfad 100644 --- a/lib/src/http/NyLPC_cMimeType.h +++ b/lib/src/http/NyLPC_cMimeType.h @@ -19,8 +19,8 @@ * along with this program. If not, see . * * For further information please contact. - * http://nyatla.jp/ - * or + * http://nyatla.jp/ + * or * *********************************************************************************/ #ifndef NyLPC_TcMimeType_H_ diff --git a/lib/src/http/NyLPC_cUrlEncode.c b/lib/src/http/NyLPC_cUrlEncode.c index 5144157..bd8f462 100644 --- a/lib/src/http/NyLPC_cUrlEncode.c +++ b/lib/src/http/NyLPC_cUrlEncode.c @@ -5,48 +5,48 @@ NyLPC_TBool NyLPC_cUrlEncode_initialize(NyLPC_TcUrlEncode_t* i_inst) { - i_inst->_len=0; - return NyLPC_TBool_TRUE; + i_inst->_len=0; + return NyLPC_TBool_TRUE; } NyLPC_TcUrlEncode_ST NyLPC_cUrlEncode_decode(NyLPC_TcUrlEncode_t* i_inst,NyLPC_TChar c,NyLPC_TChar* out) { - int t; - if(c=='%'){ - if(i_inst->_len!=0){ - NyLPC_OnErrorGoto(Error); - } - i_inst->_len=1; - return NyLPC_TcUrlEncode_ST_NEXT; - }else{ - switch(i_inst->_len){ - case 0: - *out=c; - return NyLPC_TcUrlEncode_ST_DONE; - case 1: - if(!isxdigit((int)c)){ - NyLPC_OnErrorGoto(Error); - } - t=NyLPC_ctox(c); - i_inst->v=(NyLPC_TChar)t;//16進文字→HEX - i_inst->_len++; - return NyLPC_TcUrlEncode_ST_NEXT; - case 2: - if(!isxdigit((int)c)){ - NyLPC_OnErrorGoto(Error); - } - t=NyLPC_ctox(c); - *out=(NyLPC_TChar)((i_inst->v<<4) | t); - i_inst->v=0; - i_inst->_len=0; - return NyLPC_TcUrlEncode_ST_DONE; - default: - NyLPC_OnErrorGoto(Error); - } - } + int t; + if(c=='%'){ + if(i_inst->_len!=0){ + NyLPC_OnErrorGoto(Error); + } + i_inst->_len=1; + return NyLPC_TcUrlEncode_ST_NEXT; + }else{ + switch(i_inst->_len){ + case 0: + *out=c; + return NyLPC_TcUrlEncode_ST_DONE; + case 1: + if(!isxdigit((int)c)){ + NyLPC_OnErrorGoto(Error); + } + t=NyLPC_ctox(c); + i_inst->v=(NyLPC_TChar)t;//16進文字→HEX + i_inst->_len++; + return NyLPC_TcUrlEncode_ST_NEXT; + case 2: + if(!isxdigit((int)c)){ + NyLPC_OnErrorGoto(Error); + } + t=NyLPC_ctox(c); + *out=(NyLPC_TChar)((i_inst->v<<4) | t); + i_inst->v=0; + i_inst->_len=0; + return NyLPC_TcUrlEncode_ST_DONE; + default: + NyLPC_OnErrorGoto(Error); + } + } Error: - i_inst->v=0; - i_inst->_len=0; - return NyLPC_TcUrlEncode_ST_ERROR; + i_inst->v=0; + i_inst->_len=0; + return NyLPC_TcUrlEncode_ST_ERROR; } diff --git a/lib/src/http/NyLPC_cUrlEncode.h b/lib/src/http/NyLPC_cUrlEncode.h index 8eb3ae3..d07688f 100644 --- a/lib/src/http/NyLPC_cUrlEncode.h +++ b/lib/src/http/NyLPC_cUrlEncode.h @@ -15,16 +15,16 @@ typedef struct NyLPC_TcUrlEncode NyLPC_TcUrlEncode_t; typedef NyLPC_TUInt32 NyLPC_TcUrlEncode_ST; -#define NyLPC_TcUrlEncode_ST_NEXT 2 -#define NyLPC_TcUrlEncode_ST_DONE 1 -#define NyLPC_TcUrlEncode_ST_ERROR 0 +#define NyLPC_TcUrlEncode_ST_NEXT 2 +#define NyLPC_TcUrlEncode_ST_DONE 1 +#define NyLPC_TcUrlEncode_ST_ERROR 0 struct NyLPC_TcUrlEncode { - /**一時バッファ */ - NyLPC_TChar v; - /** バッファに蓄積してる長さ */ - NyLPC_TInt8 _len; + /**一時バッファ */ + NyLPC_TChar v; + /** バッファに蓄積してる長さ */ + NyLPC_TInt8 _len; }; NyLPC_TBool NyLPC_cUrlEncode_initialize(NyLPC_TcUrlEncode_t* i_inst); diff --git a/lib/src/http/NyLPC_cUrlReader.c b/lib/src/http/NyLPC_cUrlReader.c index c774a96..79a42d7 100644 --- a/lib/src/http/NyLPC_cUrlReader.c +++ b/lib/src/http/NyLPC_cUrlReader.c @@ -3,12 +3,13 @@ #include NyLPC_TBool NyLPC_cUrlReader_getPath(const NyLPC_TChar* i_src,const NyLPC_TChar** path,NyLPC_TInt32* path_len) { - const NyLPC_TChar* p=i_src; - for(;strchr("?#\0",*p)==NULL;p++); - *path=i_src; - *path_len=p-i_src; - return NyLPC_TBool_TRUE; + const NyLPC_TChar* p=i_src; + for(;strchr("?#\0",*p)==NULL;p++); + *path=i_src; + *path_len=p-i_src; + return NyLPC_TBool_TRUE; } + /** * 指定したURLクエリキーの値を探します。 * @return @@ -53,102 +54,102 @@ const NyLPC_TChar* NyLPC_cUrlReader_findKeyValue(const NyLPC_TChar* i_src,const */ NyLPC_TBool NyLPC_cUrlReader_getStr(const NyLPC_TChar* i_src,const NyLPC_TChar* i_key_name,const NyLPC_TChar** str,NyLPC_TInt32* str_len) { - const NyLPC_TChar* p; - p=NyLPC_cUrlReader_findKeyValue(i_src,i_key_name); - if(p==NULL || *p!='='){ - *str=p; - *str_len=0; - return NyLPC_TBool_FALSE; - } - p++; - *str=p; - for(;strchr("&#\0",*p)==NULL;p++); - *str_len=p-*str; - return NyLPC_TBool_TRUE; + const NyLPC_TChar* p; + p=NyLPC_cUrlReader_findKeyValue(i_src,i_key_name); + if(p==NULL || *p!='='){ + *str=p; + *str_len=0; + return NyLPC_TBool_FALSE; + } + p++; + *str=p; + for(;strchr("&#\0",*p)==NULL;p++); + *str_len=p-*str; + return NyLPC_TBool_TRUE; } NyLPC_TBool NyLPC_cUrlReader_getUInt(const NyLPC_TChar* i_buf,const NyLPC_TChar* i_key_name,NyLPC_TUInt32* value) { - NyLPC_TUInt32 l,r; - const NyLPC_TChar* p; - p=NyLPC_cUrlReader_findKeyValue(i_buf,i_key_name); - if(p==NULL || *p!='='){ - return NyLPC_TBool_FALSE; - } - p++; - //prefixの確認 - if((*p=='0') && (NyLPC_tolower(*(p+1))=='x')) - { - //16進数 - p+=2; - r=l=0; - while(isxdigit(*p)){ - r=r*16+NyLPC_ctox(*p); - if(l!=(r>>4)){return NyLPC_TBool_FALSE;} - l=r; - p++; - } - }else{ - r=l=0; - while(isdigit(*p)){ - r=r*10+NyLPC_ctoi(*p); - if(l!=(r/10)){return NyLPC_TBool_FALSE;} - l=r; - p++; - } - } - if(strchr("&#\0",*p)==NULL){ - //An error if it is not terminator - return NyLPC_TBool_FALSE; - } - *value=r; - return NyLPC_TBool_TRUE; + NyLPC_TUInt32 l,r; + const NyLPC_TChar* p; + p=NyLPC_cUrlReader_findKeyValue(i_buf,i_key_name); + if(p==NULL || *p!='='){ + return NyLPC_TBool_FALSE; + } + p++; + //prefixの確認 + if((*p=='0') && (NyLPC_tolower(*(p+1))=='x')) + { + //16進数 + p+=2; + r=l=0; + while(isxdigit((int)(*p))){ + r=r*16+NyLPC_ctox(*p); + if(l!=(r>>4)){return NyLPC_TBool_FALSE;} + l=r; + p++; + } + }else{ + r=l=0; + while(isdigit((int)(*p))){ + r=r*10+NyLPC_ctoi(*p); + if(l!=(r/10)){return NyLPC_TBool_FALSE;} + l=r; + p++; + } + } + if(strchr("&#\0",*p)==NULL){ + //An error if it is not terminator + return NyLPC_TBool_FALSE; + } + *value=r; + return NyLPC_TBool_TRUE; } NyLPC_TBool NyLPC_cUrlReader_getInt(const NyLPC_TChar* i_buf,const NyLPC_TChar* i_key_name,NyLPC_TInt32* value) { - NyLPC_TInt32 l,r; - const NyLPC_TChar* p; - p=NyLPC_cUrlReader_findKeyValue(i_buf,i_key_name); - if(p==NULL || *p!='='){ - return NyLPC_TBool_FALSE; - } - p++; - //prefixの確認 - if((*p=='0') && (NyLPC_tolower(*(p+1))=='x')) - { - //16進数 - p+=2; - r=l=0; - while(isxdigit(*p)){ - r=r*16+NyLPC_ctox(*p); - if(l!=(r>>4)){return NyLPC_TBool_FALSE;} - l=r; - p++; - } - }else{ - r=l=0; - if(*p!='-'){ - while(isdigit(*p)){ - r=r*10+NyLPC_ctoi(*p); - if(l!=(r/10)){return NyLPC_TBool_FALSE;} - l=r; - p++; - } - }else{ - p++; - while(isdigit(*p)){ - r=r*10+NyLPC_ctoi(*p); - if(l!=(r/10)){return NyLPC_TBool_FALSE;} - l=r; - p++; - } - r*=-1; - } - } - if(strchr("&#\0",*p)==NULL){ - //An error if it is not terminator - return NyLPC_TBool_FALSE; - } - *value=(int)r; - return NyLPC_TBool_TRUE; + NyLPC_TInt32 l,r; + const NyLPC_TChar* p; + p=NyLPC_cUrlReader_findKeyValue(i_buf,i_key_name); + if(p==NULL || *p!='='){ + return NyLPC_TBool_FALSE; + } + p++; + //prefixの確認 + if((*p=='0') && (NyLPC_tolower(*(p+1))=='x')) + { + //16進数 + p+=2; + r=l=0; + while(isxdigit((int)(*p))){ + r=r*16+NyLPC_ctox(*p); + if(l!=(r>>4)){return NyLPC_TBool_FALSE;} + l=r; + p++; + } + }else{ + r=l=0; + if(*p!='-'){ + while(isdigit((int)(*p))){ + r=r*10+NyLPC_ctoi(*p); + if(l!=(r/10)){return NyLPC_TBool_FALSE;} + l=r; + p++; + } + }else{ + p++; + while(isdigit((int)(*p))){ + r=r*10+NyLPC_ctoi(*p); + if(l!=(r/10)){return NyLPC_TBool_FALSE;} + l=r; + p++; + } + r*=-1; + } + } + if(strchr("&#\0",*p)==NULL){ + //An error if it is not terminator + return NyLPC_TBool_FALSE; + } + *value=(int)r; + return NyLPC_TBool_TRUE; } diff --git a/lib/src/http/NyLPC_cUrlReader.h b/lib/src/http/NyLPC_cUrlReader.h index 90c07e7..15d70e5 100644 --- a/lib/src/http/NyLPC_cUrlReader.h +++ b/lib/src/http/NyLPC_cUrlReader.h @@ -12,7 +12,6 @@ #ifdef __cplusplus extern "C" { #endif /* __cplusplus */ - /** * URLからパス文字列のポインタと長さを返します。 */ diff --git a/lib/src/http/NyLPC_iHttpPtrStream.h b/lib/src/http/NyLPC_iHttpPtrStream.h index 323dfd7..b91c25a 100644 --- a/lib/src/http/NyLPC_iHttpPtrStream.h +++ b/lib/src/http/NyLPC_iHttpPtrStream.h @@ -19,8 +19,8 @@ * along with this program. If not, see . * * For further information please contact. - * http://nyatla.jp/ - * or + * http://nyatla.jp/ + * or * *********************************************************************************/ #ifndef NyLPC_TiHttpPtrStream_H_ @@ -67,19 +67,19 @@ typedef void (*NyLPC_TiHttpPtrStream_setWriteEncoding)(void* i_inst,NyLPC_TiHttp */ struct NyLPC_TiHttpPtrStream_TInterface { - NyLPC_TiHttpPtrStream_pread pread; - NyLPC_TiHttpPtrStream_write write; - NyLPC_TiHttpPtrStream_rseek readSeek; - NyLPC_TiHttpPtrStream_flush flush; - NyLPC_TiHttpPtrStream_setReadEncoding setreadencoding; - NyLPC_TiHttpPtrStream_setWriteEncoding setwriteencoding; + NyLPC_TiHttpPtrStream_pread pread; + NyLPC_TiHttpPtrStream_write write; + NyLPC_TiHttpPtrStream_rseek readSeek; + NyLPC_TiHttpPtrStream_flush flush; + NyLPC_TiHttpPtrStream_setReadEncoding setreadencoding; + NyLPC_TiHttpPtrStream_setWriteEncoding setwriteencoding; }; struct NyLPC_TiHttpPtrStream { - /** - * 継承クラスで実装すべきハンドラ - */ - const struct NyLPC_TiHttpPtrStream_TInterface* absfunc; + /** + * 継承クラスで実装すべきハンドラ + */ + const struct NyLPC_TiHttpPtrStream_TInterface* absfunc; }; /** diff --git a/lib/src/http/json/NyLPC_cJsonRpcParser.c b/lib/src/http/json/NyLPC_cJsonRpcParser.c index c22cc88..97872b9 100644 --- a/lib/src/http/json/NyLPC_cJsonRpcParser.c +++ b/lib/src/http/json/NyLPC_cJsonRpcParser.c @@ -201,7 +201,7 @@ static NyLPC_TBool isSignedCharSet(const char i_c) /** * 整数を構成する文字セットであるかを返します。 */ -#define isUnSignedCharSet(i_c) isdigit(i_c) +#define isUnSignedCharSet(i_c) isdigit((int)(i_c)) static void putchar_params(NyLPC_TcJsonRpcParser_t* i_inst, char i_c) { diff --git a/lib/src/include/NyLPC_config.h b/lib/src/include/NyLPC_config.h index 386e2e1..dddcad3 100644 --- a/lib/src/include/NyLPC_config.h +++ b/lib/src/include/NyLPC_config.h @@ -19,8 +19,8 @@ * along with this program. If not, see . * * For further information please contact. - * http://nyatla.jp/ - * or + * http://nyatla.jp/ + * or * *********************************************************************************/ /** @@ -59,6 +59,7 @@ extern "C" { #define NyLPC_MCU_UNKNOWN 1 #define NyLPC_MCU_LPC17xx 2 #define NyLPC_MCU_LPC4088 3 +#define NyLPC_MCU_K64F 4 #define NyLPC_MCU NyLPC_MCU_LPC17xx @@ -85,9 +86,9 @@ extern "C" { * 通常は、NyLPC_ARCH_FREERTOSを使用します。 */ #ifdef WIN_DEBUG - #define NyLPC_ARCH NyLPC_ARCH_WIN32 + #define NyLPC_ARCH NyLPC_ARCH_WIN32 #else - #define NyLPC_ARCH NyLPC_ARCH_FREERTOS + #define NyLPC_ARCH NyLPC_ARCH_FREERTOS #endif @@ -96,11 +97,11 @@ extern "C" { //////////////////////////////////////////////////////////////////////////////////////////////// #if NyLPC_ARCH==NyLPC_ARCH_WIN32 - //PACKED STRUCTの無効化 - #define PACK_STRUCT_END - //OSタイプによりMCUを修正 -# undef NyLPC_MCU -# define NyLPC_MCU NyLPC_MCU_UNKNOWN + //PACKED STRUCTの無効化 + #define PACK_STRUCT_END + //OSタイプによりMCUを修正 +# undef NyLPC_MCU +# define NyLPC_MCU NyLPC_MCU_UNKNOWN #endif @@ -117,23 +118,32 @@ extern "C" { #define NyLPC_CONFIG_cHttpStream_DEBUG 0 //////////////////////////////////////////////////////////////////////////////////////////////// -//SDKのパラメータの上書きができます。 +//Application Configuration //////////////////////////////////////////////////////////////////////////////////////////////// -/*以下のパラメータは上書きができます。*/ -//# define NyLPC_cHttpd_NUMBER_OF_CONNECTION_THREAD 3 -//# define NyLPC_cHttpd_MAX_PERSISTENT_CONNECTION (NyLPC_cHttpd_NUMBER_OF_CONNECTION_THREAD-1) -//# define NyLPC_cHttpdThread_SIZE_OF_THREAD_STACK 1024 -//# define NyLPC_cHttpdConnection_SIZE_OF_RX_BUF 512 -//# define NyLPC_cUipService_config_STACK_SIZE (256+192+192) -//////////////////////////////////////////////////////////////////////////////////////////////// -//固有情報 -//////////////////////////////////////////////////////////////////////////////////////////////// +#define NyLPC_cHttpdThread_SIZE_OF_THREAD_STACK (1024+512) +#define NyLPC_cHttpd_MAX_PERSISTENT_CONNECTION 1 -/*固有プラットフォーム設定はここに記述します。*/ +#define NyLPC_cMiMicIpNetIf_config_TCPLISTENER_MAX 2 +#define NyLPC_cMiMicIpNetIf_config_TCPSOCKET_MAX 3 +#define NyLPC_cMiMicIpNetIf_config_UDPSOCKET_MAX 1 +#define NyLPC_cMiMicIpNetIf_config_NB_UDPSOCKET_MAX 2 +/*固有プラットフォーム設定はここに記述します。*/ +//#undef NyLPC_ARCH +//#define NyLPC_ARCH NyLPC_ARCH_MBEDRTOS +#ifdef TARGET_LPC4088 +# undef NyLPC_MCU +# define NyLPC_MCU NyLPC_MCU_LPC4088 +#endif +#ifdef TARGET_K64F +# undef NyLPC_MCU +# define NyLPC_MCU NyLPC_MCU_K64F +# define NyLPC_TTxBufferHeader_ALIGNMENT 16 //EthernetのTXメモリブロックヘッダを16バイトアラインメントにする +# define NyLPC_TcEthernetMM_BUF_PADDING 16 //2バイトでいいんだけど16バイトアライメントにするために16 +#endif #ifdef __cplusplus } diff --git a/lib/src/include/NyLPC_flash.h b/lib/src/include/NyLPC_flash.h index 7c25a2c..c8b53a1 100644 --- a/lib/src/include/NyLPC_flash.h +++ b/lib/src/include/NyLPC_flash.h @@ -19,8 +19,8 @@ * along with this program. If not, see . * * For further information please contact. - * http://nyatla.jp/ - * or + * http://nyatla.jp/ + * or * *********************************************************************************/ /** @@ -30,9 +30,7 @@ #ifndef NyLPC_flash_h #define NyLPC_flash_h #include "NyLPC_stdlib.h" -#include "../flash/NyLPC_cOnchipFlashWriter.h" #include "../flash/NyLPC_cMiMicConfiglation.h" - #ifdef __cplusplus extern "C" { #endif /* __cplusplus */ diff --git a/lib/src/include/NyLPC_http.h b/lib/src/include/NyLPC_http.h index e4766c7..f1b8062 100644 --- a/lib/src/include/NyLPC_http.h +++ b/lib/src/include/NyLPC_http.h @@ -19,8 +19,8 @@ * along with this program. If not, see . * * For further information please contact. - * http://nyatla.jp/ - * or + * http://nyatla.jp/ + * or * *********************************************************************************/ /** diff --git a/lib/src/include/NyLPC_jsonrpc.h b/lib/src/include/NyLPC_jsonrpc.h index a0ffb66..518e730 100644 --- a/lib/src/include/NyLPC_jsonrpc.h +++ b/lib/src/include/NyLPC_jsonrpc.h @@ -40,3 +40,4 @@ extern "C" { #endif /* __cplusplus */ #endif + diff --git a/lib/src/include/NyLPC_mimicVm.h b/lib/src/include/NyLPC_mimicVm.h index dc8e4b2..c0aad48 100644 --- a/lib/src/include/NyLPC_mimicVm.h +++ b/lib/src/include/NyLPC_mimicVm.h @@ -19,8 +19,8 @@ * along with this program. If not, see . * * For further information please contact. - * http://nyatla.jp/ - * or + * http://nyatla.jp/ + * or * *********************************************************************************/ /** diff --git a/lib/src/include/NyLPC_net.h b/lib/src/include/NyLPC_net.h index dfb17c7..110135c 100644 --- a/lib/src/include/NyLPC_net.h +++ b/lib/src/include/NyLPC_net.h @@ -19,8 +19,8 @@ * along with this program. If not, see . * * For further information please contact. - * http://nyatla.jp/ - * or + * http://nyatla.jp/ + * or * *********************************************************************************/ /** @@ -29,10 +29,8 @@ */ #ifndef NYLPC_NET_H_ #define NYLPC_NET_H_ -#include "../net/NyLPC_cNet.h" #include "../net/mdns/NyLPC_cMDnsServer.h" -#include "../net/apipa/NyLPC_cApipa.h" #include "../net/httpd/NyLPC_cHttpdConnection.h" #include "../net/httpd/NyLPC_cHttpd.h" diff --git a/lib/src/include/NyLPC_uipService.h b/lib/src/include/NyLPC_netif.h similarity index 83% rename from lib/src/include/NyLPC_uipService.h rename to lib/src/include/NyLPC_netif.h index a16744d..0adefb6 100644 --- a/lib/src/include/NyLPC_uipService.h +++ b/lib/src/include/NyLPC_netif.h @@ -1,50 +1,52 @@ -/********************************************************************************* - * PROJECT: MiMic - * -------------------------------------------------------------------------------- - * - * This file is part of MiMic - * Copyright (C)2011 Ryo Iizuka - * - * MiMic is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published - * by the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program. If not, see . - * - * For further information please contact. - * http://nyatla.jp/ - * or - * - *********************************************************************************/ -/** - * @file - * このファイルは、uipディレクトリにあるヘッダファイルを集積します。 - */ -#ifndef NyLPC_uipService_H -#define NyLPC_uipService_H - -#include "NyLPC_stdlib.h" -#include "../uip/NyLPC_cTcpListener.h" -#include "../uip/NyLPC_cTcpSocket.h" -#include "../uip/NyLPC_cUdpSocket.h" -#include "../uip/NyLPC_cUipService.h" -#include "../uip/NyLPC_uip_ethernet.h" - -#ifdef __cplusplus -extern "C" { -#endif /* __cplusplus */ - -#ifdef __cplusplus -} -#endif /* __cplusplus */ - - - -#endif +/********************************************************************************* + * PROJECT: MiMic + * -------------------------------------------------------------------------------- + * + * This file is part of MiMic + * Copyright (C)2011 Ryo Iizuka + * + * MiMic is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + * + * For further information please contact. + * http://nyatla.jp/ + * or + * + *********************************************************************************/ +/** + * @file + * このファイルは、uipディレクトリにあるヘッダファイルを集積します。 + */ +#ifndef NyLPC_uipService_H +#define NyLPC_uipService_H + +#include "NyLPC_stdlib.h" +#include "../netif/NyLPC_cIPv4Config.h" +#include "../netif/NyLPC_iTcpListener.h" +#include "../netif/NyLPC_iTcpSocket.h" +#include "../netif/NyLPC_iUdpSocket.h" +#include "../netif/NyLPC_NetIf_ip_types.h" +#include "../netif/NyLPC_cNet.h" +#include "../netif/NyLPC_iNetInterface.h" + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + + + +#endif diff --git a/lib/src/include/NyLPC_stdlib.h b/lib/src/include/NyLPC_stdlib.h index c4a0e03..0f406f3 100644 --- a/lib/src/include/NyLPC_stdlib.h +++ b/lib/src/include/NyLPC_stdlib.h @@ -19,8 +19,8 @@ * along with this program. If not, see . * * For further information please contact. - * http://nyatla.jp/ - * or + * http://nyatla.jp/ + * or * *********************************************************************************/ /** @@ -46,67 +46,67 @@ extern "C" { #define MIMIC_DEBUG 1 #ifdef MIMIC_DEBUG - /** - * 引数チェック専用のASSERT構文です。 - * デバック時のみ有効です。 - */ - #define NyLPC_ArgAssert(e) if(!(e)){NyLPC_assertHook(NULL,-1);}; - - /** - * ASSERTマクロです。 - * デバック時のみ有効です。 - */ - #define NyLPC_Assert(e) if(!(e)){NyLPC_assertHook(NULL,-1);}; - - /** - * デバック用のフックマクロです。 - * デバック時のみ有効です。 - */ - #define NyAR_DebugHook() {NyLPC_debugHook(__FILE__,__LINE__);}; - - /** - * Abortマクロです。プログラムを異常終了するときのエントリポイントになります。 - * デバック時/リリース時のどちらでも有効です。 - */ - #define NyLPC_Abort() {NyLPC_abortHook(__FILE__,__LINE__);}; //もうだめ - - /** - * Abortマクロです。eが偽の時に、異常終了します。 - * デバック時/リリース時のどちらでも有効です。 - * @param e - * 評価式です. - */ - #define NyLPC_AbortIfNot(e) if(!(e)){NyLPC_abortHook(__FILE__,__LINE__);}; - - /** - * 警告表示用のマクロです.デバックに使います. - * デバック時のみ有効です. - */ - #define NyLPC_Warning() {NyLPC_debugHook(__FILE__,__LINE__);}; - - /** - * 警告表示用のマクロです.eが偽の時に、警告を出します. - * デバック時のみ有効です. - * @param e - * 評価式です。 - */ - #define NyLPC_WarningIfNot(e) if(!(e)){NyLPC_debugHook(__FILE__,__LINE__);}; - - /* - * トレースマクロです。デバックに使います。 - * 内部変数に、最後にコールされたファイル名と、行番号を保存します。 - * デバック時のみ有効です. - */ - #define NyLPC_Trace() {NyLPC_debugHook(__FILE__,__LINE__);}; + /** + * 引数チェック専用のASSERT構文です。 + * デバック時のみ有効です。 + */ + #define NyLPC_ArgAssert(e) if(!(e)){NyLPC_assertHook(NULL,-1);}; + + /** + * ASSERTマクロです。 + * デバック時のみ有効です。 + */ + #define NyLPC_Assert(e) if(!(e)){NyLPC_assertHook(NULL,-1);}; + + /** + * デバック用のフックマクロです。 + * デバック時のみ有効です。 + */ + #define NyAR_DebugHook() {NyLPC_debugHook(__FILE__,__LINE__);}; + + /** + * Abortマクロです。プログラムを異常終了するときのエントリポイントになります。 + * デバック時/リリース時のどちらでも有効です。 + */ + #define NyLPC_Abort() {NyLPC_abortHook(__FILE__,__LINE__);}; //もうだめ + + /** + * Abortマクロです。eが偽の時に、異常終了します。 + * デバック時/リリース時のどちらでも有効です。 + * @param e + * 評価式です. + */ + #define NyLPC_AbortIfNot(e) if(!(e)){NyLPC_abortHook(__FILE__,__LINE__);}; + + /** + * 警告表示用のマクロです.デバックに使います. + * デバック時のみ有効です. + */ + #define NyLPC_Warning() {NyLPC_debugHook(__FILE__,__LINE__);}; + + /** + * 警告表示用のマクロです.eが偽の時に、警告を出します. + * デバック時のみ有効です. + * @param e + * 評価式です。 + */ + #define NyLPC_WarningIfNot(e) if(!(e)){NyLPC_debugHook(__FILE__,__LINE__);}; + + /* + * トレースマクロです。デバックに使います。 + * 内部変数に、最後にコールされたファイル名と、行番号を保存します。 + * デバック時のみ有効です. + */ + #define NyLPC_Trace() {NyLPC_debugHook(__FILE__,__LINE__);}; #else - #define NyLPC_ArgAssert(e) if(!(e)){NyLPC_assertHook(NULL,-1);}; - #define NyLPC_Assert(e) if(!(e)){NyLPC_assertHook(NULL,-1);}; - #define NyAR_DebugHook() {NyLPC_debugHook(__FILE__,__LINE__);}; - #define NyLPC_Abort() {NyLPC_abortHook(__FILE__,__LINE__);}; //もうだめ - #define NyLPC_AbortIfNot(e) if(!(e)){NyLPC_abortHook(__FILE__,__LINE__);}; - #define NyLPC_Warning() {NyLPC_debugHook(__FILE__,__LINE__);}; - #define NyLPC_WarningIfNot(e) if(!(e)){NyLPC_debugHook(__FILE__,__LINE__);}; - #define NyLPC_Trace() {NyLPC_debugHook(__FILE__,__LINE__);}; + #define NyLPC_ArgAssert(e) if(!(e)){NyLPC_assertHook(NULL,-1);}; + #define NyLPC_Assert(e) if(!(e)){NyLPC_assertHook(NULL,-1);}; + #define NyAR_DebugHook() {NyLPC_debugHook(__FILE__,__LINE__);}; + #define NyLPC_Abort() {NyLPC_abortHook(__FILE__,__LINE__);}; //もうだめ + #define NyLPC_AbortIfNot(e) if(!(e)){NyLPC_abortHook(__FILE__,__LINE__);}; + #define NyLPC_Warning() {NyLPC_debugHook(__FILE__,__LINE__);}; + #define NyLPC_WarningIfNot(e) if(!(e)){NyLPC_debugHook(__FILE__,__LINE__);}; + #define NyLPC_Trace() {NyLPC_debugHook(__FILE__,__LINE__);}; #endif /** @@ -413,10 +413,10 @@ NyLPC_TUInt32 NyLPC_TUInt32_bswap(NyLPC_TUInt32 n); */ struct NyLPC_TCharArrayPtr { - /** 配列のポインタ */ - NyLPC_TChar* ptr; - /** 配列の長さ */ - NyLPC_TUInt16 len; + /** 配列のポインタ */ + NyLPC_TChar* ptr; + /** 配列の長さ */ + NyLPC_TUInt16 len; }; /** * ptrの位置をi_seekだけ進行します。 @@ -434,10 +434,10 @@ NyLPC_TBool NyLPC_TCharArrayPtr_seek(struct NyLPC_TCharArrayPtr* i_struct,NyLPC_ */ struct NyLPC_TUInt32ArrayPtr { - /** 配列のポインタ */ - NyLPC_TUInt32* ptr; - /** 要素数 */ - NyLPC_TUInt16 len; + /** 配列のポインタ */ + NyLPC_TUInt32* ptr; + /** 要素数 */ + NyLPC_TUInt16 len; }; /** * ptrの位置をi_seekだけ進行します。 @@ -459,8 +459,8 @@ void NyLPC_TUInt32ArrayPtr_setBuf(struct NyLPC_TUInt32ArrayPtr* i_struct,NyLPC_T * TextとIDのテーブルです。 */ struct NyLPC_TTextIdTbl{ - const char* n; - NyLPC_TUInt8 id; + const char* n; + NyLPC_TUInt8 id; }; /** diff --git a/lib/src/include/NyLPC_utils.h b/lib/src/include/NyLPC_utils.h index d636169..d77f9f1 100644 --- a/lib/src/include/NyLPC_utils.h +++ b/lib/src/include/NyLPC_utils.h @@ -19,8 +19,8 @@ * along with this program. If not, see . * * For further information please contact. - * http://nyatla.jp/ - * or + * http://nyatla.jp/ + * or * *********************************************************************************/ /** diff --git a/lib/src/jsonrpc/NyLPC_cJsonRpcFunction.h b/lib/src/jsonrpc/NyLPC_cJsonRpcFunction.h index f6feed4..3a698ff 100644 --- a/lib/src/jsonrpc/NyLPC_cJsonRpcFunction.h +++ b/lib/src/jsonrpc/NyLPC_cJsonRpcFunction.h @@ -24,3 +24,4 @@ extern const struct NyLPC_TJsonRpcClassDef NyLPC_cJsonRpcFunction_Memory; #endif /* __cplusplus */ #endif /* NYLPC_CJSONRPCFUNCTION_H_ */ + diff --git a/lib/src/jsonrpc/NyLPC_cJsonRpcFunction_Memory.c b/lib/src/jsonrpc/NyLPC_cJsonRpcFunction_Memory.c index d573d60..c58a996 100644 --- a/lib/src/jsonrpc/NyLPC_cJsonRpcFunction_Memory.c +++ b/lib/src/jsonrpc/NyLPC_cJsonRpcFunction_Memory.c @@ -131,3 +131,4 @@ const static struct NyLPC_TJsonRpcMethodDef func_table[]= const struct NyLPC_TJsonRpcClassDef NyLPC_cJsonRpcFunction_Memory={ "MiMic","Memory",func_table }; + diff --git a/lib/src/mimicvm/NyLPC_cMiMicDbCompiler.c b/lib/src/mimicvm/NyLPC_cMiMicDbCompiler.c index 1a3f210..e33d038 100644 --- a/lib/src/mimicvm/NyLPC_cMiMicDbCompiler.c +++ b/lib/src/mimicvm/NyLPC_cMiMicDbCompiler.c @@ -5,7 +5,7 @@ void NyLPC_cMiMicDbCompiler_initialize(NyLPC_TcMiMicDbCompiler_t* i_inst) { - i_inst->_bc_fragment_len=0; + i_inst->_bc_fragment_len=0; } /** @@ -17,49 +17,49 @@ void NyLPC_cMiMicDbCompiler_initialize(NyLPC_TcMiMicDbCompiler_t* i_inst) */ NyLPC_TcMiMicDbCompiler_RET NyLPC_cMiMicDbCompiler_compileFragment(NyLPC_TcMiMicDbCompiler_t* i_inst,const struct NyLPC_TCharArrayPtr* i_bc,NyLPC_TUInt32* o_val,NyLPC_TUInt16* o_parsed_bc) { - int i; - NyLPC_TUInt8 c=i_inst->_bc_fragment_len; - if(c==0 && i_bc->len>=8){ - //キャッシュ0でソースが十分あるときは直接変換 - if(NyLPC_mimicvm_txt2UInt(i_bc->ptr,8,o_val)){ - *o_parsed_bc=8; - return NyLPC_TcMiMicDbCompiler_RET_OK; - }else{ - i_inst->error_reason=NyLPC_TcMiMicDbCompiler_ERROR_FORMAT; - return NyLPC_TcMiMicDbCompiler_RET_ERROR; - } - }else{ - //キャッシュが0でなければ、パディングして変換 - for(i=0;ilen;i++){ - i_inst->_tmp[c]=i_bc->ptr[i]; - c++; - //8個のバイトコードが溜まったら変換 - if(c==8){ - i_inst->_bc_fragment_len=0; - if(NyLPC_mimicvm_txt2UInt(i_inst->_tmp,8,o_val)){ - *o_parsed_bc=i+1;//見チェック - return NyLPC_TcMiMicDbCompiler_RET_OK; - }else{ - i_inst->error_reason=NyLPC_TcMiMicDbCompiler_ERROR_FORMAT; - return NyLPC_TcMiMicDbCompiler_RET_ERROR; - } - } - } - i_inst->_bc_fragment_len=c; - *o_parsed_bc=i_bc->len; - return NyLPC_TcMiMicDbCompiler_RET_CONTINUE; - } + int i; + NyLPC_TUInt8 c=i_inst->_bc_fragment_len; + if(c==0 && i_bc->len>=8){ + //キャッシュ0でソースが十分あるときは直接変換 + if(NyLPC_mimicvm_txt2UInt(i_bc->ptr,8,o_val)){ + *o_parsed_bc=8; + return NyLPC_TcMiMicDbCompiler_RET_OK; + }else{ + i_inst->error_reason=NyLPC_TcMiMicDbCompiler_ERROR_FORMAT; + return NyLPC_TcMiMicDbCompiler_RET_ERROR; + } + }else{ + //キャッシュが0でなければ、パディングして変換 + for(i=0;ilen;i++){ + i_inst->_tmp[c]=i_bc->ptr[i]; + c++; + //8個のバイトコードが溜まったら変換 + if(c==8){ + i_inst->_bc_fragment_len=0; + if(NyLPC_mimicvm_txt2UInt(i_inst->_tmp,8,o_val)){ + *o_parsed_bc=i+1;//見チェック + return NyLPC_TcMiMicDbCompiler_RET_OK; + }else{ + i_inst->error_reason=NyLPC_TcMiMicDbCompiler_ERROR_FORMAT; + return NyLPC_TcMiMicDbCompiler_RET_ERROR; + } + } + } + i_inst->_bc_fragment_len=c; + *o_parsed_bc=i_bc->len; + return NyLPC_TcMiMicDbCompiler_RET_CONTINUE; + } } /** * MiMicDBフラグメントを1文字パースします。 */ NyLPC_TcMiMicDbCompiler_RET NyLPC_cMiMicDbCompiler_compileFragment2(NyLPC_TcMiMicDbCompiler_t* i_inst,NyLPC_TChar i_bc,NyLPC_TUInt32* o_val) { - struct NyLPC_TCharArrayPtr bc; - NyLPC_TUInt16 l; - bc.ptr=&i_bc; - bc.len=1; - return NyLPC_cMiMicDbCompiler_compileFragment(i_inst,&bc,o_val,&l); + struct NyLPC_TCharArrayPtr bc; + NyLPC_TUInt16 l; + bc.ptr=&i_bc; + bc.len=1; + return NyLPC_cMiMicDbCompiler_compileFragment(i_inst,&bc,o_val,&l); } /** @@ -70,37 +70,37 @@ NyLPC_TcMiMicDbCompiler_RET NyLPC_cMiMicDbCompiler_compileFragment2(NyLPC_TcMiMi */ NyLPC_TUInt16 NyLPC_cMiMicDbCompiler_compile(NyLPC_TcMiMicDbCompiler_t* i_inst,const struct NyLPC_TCharArrayPtr* i_bc,struct NyLPC_TUInt32ArrayPtr* o_val) { - struct NyLPC_TUInt32ArrayPtr wp=*o_val; - struct NyLPC_TCharArrayPtr rp=*i_bc; - NyLPC_TUInt16 s; - NyLPC_Assert(i_bc->len>0); - while(i_bc->len>0){ - //空き領域チェック - if(wp.len==0){ - //空き領域不足 - i_inst->error_reason=NyLPC_TcMiMicDbCompiler_ERROR_OUT_BUFFER_TOO_SHORT; - return 0; - } - switch(NyLPC_cMiMicDbCompiler_compileFragment(i_inst,&rp,o_val->ptr,&s)){ - case NyLPC_TcMiMicDbCompiler_RET_OK: - //入力ポインタの移動 - if(!NyLPC_TCharArrayPtr_seek(&rp,s)){ - return 0; - } - //出力ポインタの移動 - if(!NyLPC_TUInt32ArrayPtr_seek(&wp,1)){ - return 0;//エラー - } - continue; - case NyLPC_TcMiMicDbCompiler_RET_CONTINUE: - //フラグメント化してたらエラー - i_inst->error_reason=NyLPC_TcMiMicDbCompiler_ERROR_FRAGMENT_UNIT; - default: - return 0; - } - } - //変換完了 - return o_val->len-wp.len; + struct NyLPC_TUInt32ArrayPtr wp=*o_val; + struct NyLPC_TCharArrayPtr rp=*i_bc; + NyLPC_TUInt16 s; + NyLPC_Assert(i_bc->len>0); + while(i_bc->len>0){ + //空き領域チェック + if(wp.len==0){ + //空き領域不足 + i_inst->error_reason=NyLPC_TcMiMicDbCompiler_ERROR_OUT_BUFFER_TOO_SHORT; + return 0; + } + switch(NyLPC_cMiMicDbCompiler_compileFragment(i_inst,&rp,o_val->ptr,&s)){ + case NyLPC_TcMiMicDbCompiler_RET_OK: + //入力ポインタの移動 + if(!NyLPC_TCharArrayPtr_seek(&rp,s)){ + return 0; + } + //出力ポインタの移動 + if(!NyLPC_TUInt32ArrayPtr_seek(&wp,1)){ + return 0;//エラー + } + continue; + case NyLPC_TcMiMicDbCompiler_RET_CONTINUE: + //フラグメント化してたらエラー + i_inst->error_reason=NyLPC_TcMiMicDbCompiler_ERROR_FRAGMENT_UNIT; + default: + return 0; + } + } + //変換完了 + return o_val->len-wp.len; } @@ -108,35 +108,35 @@ NyLPC_TUInt16 NyLPC_cMiMicDbCompiler_compile(NyLPC_TcMiMicDbCompiler_t* i_inst,c #ifndef TEST void main(void) { - int i=0; - struct NyLPC_TCharArrayPtr bc; - const char* BC="0000000100000002"; - const char* rp; - NyLPC_TcMiMicDbCompiler_t inst; - NyLPC_TUInt32 obuf[1024]; - NyLPC_TUInt16 pl; - NyLPC_cMiMicDbCompiler_initialize(&inst); - bc.ptr=(char*)BC; - bc.len=strlen(BC); - rp=BC; - NyLPC_cMiMicDbCompiler_compileFragment(&inst,&bc,obuf,&pl); + int i=0; + struct NyLPC_TCharArrayPtr bc; + const char* BC="0000000100000002"; + const char* rp; + NyLPC_TcMiMicDbCompiler_t inst; + NyLPC_TUInt32 obuf[1024]; + NyLPC_TUInt16 pl; + NyLPC_cMiMicDbCompiler_initialize(&inst); + bc.ptr=(char*)BC; + bc.len=strlen(BC); + rp=BC; + NyLPC_cMiMicDbCompiler_compileFragment(&inst,&bc,obuf,&pl); /* - for(;;){ - switch(NyLPC_cMiMicDbCompiler_compileFragment2(&inst,*bc.ptr,&obuf[i])) - { - case NyLPC_TcMiMicDbCompiler_RET_ERROR: - printf("ERROR"); - return; - case NyLPC_TcMiMicDbCompiler_RET_CONTINUE: - NyLPC_TCharArrayPtr_seek(&bc,1); - break; - case NyLPC_TcMiMicDbCompiler_RET_OK: - i++; - NyLPC_TCharArrayPtr_seek(&bc,1); - break; - } - } + for(;;){ + switch(NyLPC_cMiMicDbCompiler_compileFragment2(&inst,*bc.ptr,&obuf[i])) + { + case NyLPC_TcMiMicDbCompiler_RET_ERROR: + printf("ERROR"); + return; + case NyLPC_TcMiMicDbCompiler_RET_CONTINUE: + NyLPC_TCharArrayPtr_seek(&bc,1); + break; + case NyLPC_TcMiMicDbCompiler_RET_OK: + i++; + NyLPC_TCharArrayPtr_seek(&bc,1); + break; + } + } */ - return; + return; } #endif diff --git a/lib/src/mimicvm/NyLPC_cMiMicDbCompiler.h b/lib/src/mimicvm/NyLPC_cMiMicDbCompiler.h index ec7b769..7aaa69d 100644 --- a/lib/src/mimicvm/NyLPC_cMiMicDbCompiler.h +++ b/lib/src/mimicvm/NyLPC_cMiMicDbCompiler.h @@ -18,11 +18,11 @@ typedef struct NyLPC_TcMiMicDbCompiler NyLPC_TcMiMicDbCompiler_t; typedef NyLPC_TUInt8 NyLPC_TcMiMicDbCompiler_ERROR; //不明なエラー -#define NyLPC_TcMiMicDbCompiler_ERROR_UNKNOWN 0x00 +#define NyLPC_TcMiMicDbCompiler_ERROR_UNKNOWN 0x00 //出力バッファが足りない。 #define NyLPC_TcMiMicDbCompiler_ERROR_OUT_BUFFER_TOO_SHORT 0x01 //BCの形式がおかしい。 -#define NyLPC_TcMiMicDbCompiler_ERROR_FORMAT 0x02 +#define NyLPC_TcMiMicDbCompiler_ERROR_FORMAT 0x02 //BCフラグメント単位がおかしい。(途中で終わってる?) #define NyLPC_TcMiMicDbCompiler_ERROR_FRAGMENT_UNIT 0x03 @@ -36,9 +36,9 @@ typedef NyLPC_TUInt8 NyLPC_TcMiMicDbCompiler_RET; struct NyLPC_TcMiMicDbCompiler { - NyLPC_TUInt8 _bc_fragment_len; - NyLPC_TcMiMicDbCompiler_ERROR error_reason;//エラー理由 - NyLPC_TChar _tmp[8];//テンポラリ + NyLPC_TUInt8 _bc_fragment_len; + NyLPC_TcMiMicDbCompiler_ERROR error_reason;//エラー理由 + NyLPC_TChar _tmp[8];//テンポラリ }; void NyLPC_cMiMicDbCompiler_initialize(NyLPC_TcMiMicDbCompiler_t* i_inst); diff --git a/lib/src/mimicvm/NyLPC_cMiMicTxtCompiler.c b/lib/src/mimicvm/NyLPC_cMiMicTxtCompiler.c index b7a3dab..a63b3a1 100644 --- a/lib/src/mimicvm/NyLPC_cMiMicTxtCompiler.c +++ b/lib/src/mimicvm/NyLPC_cMiMicTxtCompiler.c @@ -19,8 +19,8 @@ * along with this program. If not, see . * * For further information please contact. - * http://nyatla.jp/ - * or + * http://nyatla.jp/ + * or * *********************************************************************************/ #include @@ -32,12 +32,12 @@ struct TInstructionDef { - /** MiMicBCのオペコード */ - const char* bcopcode; - /** インストラクションID */ - NyLPC_TcMiMicVM_OP_TYPE opid; - /** オペコードのタイプ */ - NyLPC_TcMiMicVM_OPR_TYPE optype; + /** MiMicBCのオペコード */ + const char* bcopcode; + /** インストラクションID */ + NyLPC_TcMiMicVM_OP_TYPE opid; + /** オペコードのタイプ */ + NyLPC_TcMiMicVM_OPR_TYPE optype; }; static NyLPC_TBool bc2opc(const NyLPC_TChar* i_char,NyLPC_TcMiMicVM_OP_TYPE* o_opc,NyLPC_TcMiMicVM_OPR_TYPE* o_opt); static NyLPC_TBool bc2ctrlc(const NyLPC_TChar* i_char,NyLPC_TcMiMicVM_OP_TYPE* o_opc,NyLPC_TcMiMicVM_OPR_TYPE* o_opt); @@ -48,18 +48,18 @@ static void NyLPC_TcMiMicVM_OPR_TYPE_getOpInfo(NyLPC_TcMiMicVM_OPR_TYPE i_type,N void NyLPC_cMiMicTxtCompiler_initialize(NyLPC_TcMiMicTxtCompiler_t* i_inst) { - i_inst->out_buf=NULL; - i_inst->st=NyLPC_TcMiMicTxtCompiler_ST_OPC; - i_inst->tmp_len=0; + i_inst->out_buf=NULL; + i_inst->st=NyLPC_TcMiMicTxtCompiler_ST_OPC; + i_inst->tmp_len=0; } -#define NyLPC_TcMiMicTxtCompiler_ST_OPC 0x01 //OPをパース中 -#define NyLPC_TcMiMicTxtCompiler_ST_OPR 0x02 //オペランドを解析中 -#define NyLPC_TcMiMicTxtCompiler_ST_CTR 0x03 //制御パラメータを解析中 -#define NyLPC_TcMiMicTxtCompiler_ST_OK 0x04 //パースを完了した。(.EMD検出) -#define NyLPC_TcMiMicTxtCompiler_ST_NG 0xff //パースでエラーが発生ウェーイ +#define NyLPC_TcMiMicTxtCompiler_ST_OPC 0x01 //OPをパース中 +#define NyLPC_TcMiMicTxtCompiler_ST_OPR 0x02 //オペランドを解析中 +#define NyLPC_TcMiMicTxtCompiler_ST_CTR 0x03 //制御パラメータを解析中 +#define NyLPC_TcMiMicTxtCompiler_ST_OK 0x04 //パースを完了した。(.EMD検出) +#define NyLPC_TcMiMicTxtCompiler_ST_NG 0xff //パースでエラーが発生ウェーイ @@ -76,140 +76,140 @@ void NyLPC_cMiMicTxtCompiler_initialize(NyLPC_TcMiMicTxtCompiler_t* i_inst) */ NyLPC_TcMiMicTxtCompiler_RET NyLPC_cMiMicTxtCompiler_compileFragment(NyLPC_TcMiMicTxtCompiler_t* i_inst,const struct NyLPC_TCharArrayPtr* i_bc,struct NyLPC_TUInt32ArrayPtr* i_bin,NyLPC_TUInt16* o_bin_len,NyLPC_TUInt16* o_parsed_bc) { - union NyLPC_TcMiMicVM_TInstruction* wptr; - int i; - for(i=0;ilen;i++) - { - switch(i_inst->st){ - case NyLPC_TcMiMicTxtCompiler_ST_OPC: - if(i_inst->tmp_len>2){ - NyLPC_OnErrorGoto(ERROR); - } - i_inst->tmp[i_inst->tmp_len]=*(i_bc->ptr+i); - i_inst->tmp_len++; - if(i_inst->tmp_len==2){ - //[A-Z]{2}がそろった。命令コードか制御命令か判定 - if(bc2opc(i_inst->tmp,&(i_inst->_current_opc),&(i_inst->_current_oprtype))){ - //命令コードならインストラクションの情報をもらってくる。 - NyLPC_TcMiMicVM_OPR_TYPE_getOpInfo(i_inst->_current_oprtype,&i_inst->_oprbc_len,&i_inst->_inst_len); - //オペランドが無ければ、命令確定。 - if(i_inst->_oprbc_len<=0){ - //書込みポインタ保管 - wptr=(union NyLPC_TcMiMicVM_TInstruction*)i_bin->ptr; - //バッファのシーク - if(!NyLPC_TUInt32ArrayPtr_seek(i_bin,1)){ - NyLPC_OnErrorGoto(ERROR); - } - //インストラクションの出力と処理したBC長の計算 - wptr->op.opc=i_inst->_current_opc; - wptr->op.oprtype=i_inst->_current_oprtype; - *o_parsed_bc=i+1; //パースしたBCの長さ - *o_bin_len=i_inst->_inst_len; - //解析バッファの長さをリセットして、次のBCブロックのパース準備 - i_inst->tmp_len=0; - return NyLPC_TcMiMicTxtCompiler_RET_OK;//命令確定。 - } - //オペランドがあるなら、パース対象を変更。 - i_inst->tmp_len=0; - i_inst->st=NyLPC_TcMiMicTxtCompiler_ST_OPR; - }else if(bc2ctrlc(i_inst->tmp,&(i_inst->_current_opc),&(i_inst->_current_oprtype))){ - //命令コードならインストラクションの情報をもらってくる。 - NyLPC_TcMiMicVM_OPR_TYPE_getOpInfo(i_inst->_current_oprtype,&i_inst->_oprbc_len,&i_inst->_inst_len); - //制御コードの解析 - if(i_inst->_oprbc_len>0){ - //パラメータのある制御命令未定義だからエラー。 - NyLPC_OnErrorGoto(ERROR); - } - //END制御命令? - if(i_inst->_current_opc==NyLPC_TcMiMicVM_CP_TYPE_END){ - *o_bin_len=0; - *o_parsed_bc=i+1; //パースしたBCの長さ - i_inst->tmp_len=0; - return NyLPC_TcMiMicTxtCompiler_RET_OK_END;//命令確定。(パース完了) - } - //END制御命令以外ならエラー - NyLPC_OnErrorGoto(ERROR); - }else{ - //不明な命令 - NyLPC_OnErrorGoto(ERROR); - } - } - break; - case NyLPC_TcMiMicTxtCompiler_ST_CTR: - NyLPC_OnErrorGoto(ERROR); - case NyLPC_TcMiMicTxtCompiler_ST_OPR: - //オペランド蓄積. - i_inst->tmp[i_inst->tmp_len]=*(i_bc->ptr+i); - i_inst->tmp_len++; - //オペランド長さになるまで値を追記 - if(i_inst->_oprbc_len==i_inst->tmp_len){ - //書込みポインタ保管 - wptr=(union NyLPC_TcMiMicVM_TInstruction*)i_bin->ptr; - //シーク - if(!NyLPC_TUInt32ArrayPtr_seek(i_bin,i_inst->_inst_len)){ - NyLPC_OnErrorGoto(ERROR); - } - wptr->op.opc=i_inst->_current_opc; - wptr->op.oprtype=i_inst->_current_oprtype; - //オペランドの変換処理 - switch(i_inst->_current_oprtype){ - case NyLPC_TcMiMicVM_OPR_TYPE_WM_WM: - if(!( txt2WMId(i_inst->tmp,&(wptr->wmwm_32.wm1)) && - txt2WMId(i_inst->tmp+2,&(wptr->wmwm_32.wm2)))) - { - NyLPC_OnErrorGoto(ERROR); - } - break; - case NyLPC_TcMiMicVM_OPR_TYPE_WM_H08: - if(!( txt2WMId(i_inst->tmp,&(wptr->wmh08_32.wm)) && - txt2UInt(i_inst->tmp+2,2,&(wptr->wmh08_32.h8)))) - { - NyLPC_OnErrorGoto(ERROR); - } - break; - case NyLPC_TcMiMicVM_OPR_TYPE_WM_H32: - if(!( txt2WMId(i_inst->tmp,&(wptr->wmh32_64.wm)) && - txt2UInt(i_inst->tmp+2,8,&(wptr->wmh32_64.h32)))){ - NyLPC_OnErrorGoto(ERROR); - } - break; - case NyLPC_TcMiMicVM_OPR_TYPE_WM: - if(!txt2WMId(i_inst->tmp,&(wptr->wm_32.wm))){ - NyLPC_OnErrorGoto(ERROR); - } - break; - case NyLPC_TcMiMicVM_OPR_TYPE_H32: - if(!txt2UInt(i_inst->tmp,8,&(wptr->h32_64.h32))){ - NyLPC_OnErrorGoto(ERROR); - } - break; - case NyLPC_TcMiMicVM_OPR_TYPE_H08: - if(!txt2UInt(i_inst->tmp,2,&(wptr->h8_32.h8))) - { - NyLPC_OnErrorGoto(ERROR); - } - break; - default: - NyLPC_OnErrorGoto(ERROR); - } - //OPR解析成功。パースしたブロックサイズの計算。 - *o_parsed_bc=i+1; //パースしたBCの長さ - *o_bin_len=i_inst->_inst_len; - i_inst->tmp_len=0; - i_inst->st=NyLPC_TcMiMicTxtCompiler_ST_OPC; - return NyLPC_TcMiMicTxtCompiler_RET_OK; - } - break; - default: - NyLPC_OnErrorGoto(ERROR); - } - } - *o_bin_len=0; - *o_parsed_bc=i_bc->len; - return NyLPC_TcMiMicTxtCompiler_RET_CONTINUE; + union NyLPC_TcMiMicVM_TInstruction* wptr; + int i; + for(i=0;ilen;i++) + { + switch(i_inst->st){ + case NyLPC_TcMiMicTxtCompiler_ST_OPC: + if(i_inst->tmp_len>2){ + NyLPC_OnErrorGoto(ERROR); + } + i_inst->tmp[i_inst->tmp_len]=*(i_bc->ptr+i); + i_inst->tmp_len++; + if(i_inst->tmp_len==2){ + //[A-Z]{2}がそろった。命令コードか制御命令か判定 + if(bc2opc(i_inst->tmp,&(i_inst->_current_opc),&(i_inst->_current_oprtype))){ + //命令コードならインストラクションの情報をもらってくる。 + NyLPC_TcMiMicVM_OPR_TYPE_getOpInfo(i_inst->_current_oprtype,&i_inst->_oprbc_len,&i_inst->_inst_len); + //オペランドが無ければ、命令確定。 + if(i_inst->_oprbc_len<=0){ + //書込みポインタ保管 + wptr=(union NyLPC_TcMiMicVM_TInstruction*)i_bin->ptr; + //バッファのシーク + if(!NyLPC_TUInt32ArrayPtr_seek(i_bin,1)){ + NyLPC_OnErrorGoto(ERROR); + } + //インストラクションの出力と処理したBC長の計算 + wptr->op.opc=i_inst->_current_opc; + wptr->op.oprtype=i_inst->_current_oprtype; + *o_parsed_bc=i+1; //パースしたBCの長さ + *o_bin_len=i_inst->_inst_len; + //解析バッファの長さをリセットして、次のBCブロックのパース準備 + i_inst->tmp_len=0; + return NyLPC_TcMiMicTxtCompiler_RET_OK;//命令確定。 + } + //オペランドがあるなら、パース対象を変更。 + i_inst->tmp_len=0; + i_inst->st=NyLPC_TcMiMicTxtCompiler_ST_OPR; + }else if(bc2ctrlc(i_inst->tmp,&(i_inst->_current_opc),&(i_inst->_current_oprtype))){ + //命令コードならインストラクションの情報をもらってくる。 + NyLPC_TcMiMicVM_OPR_TYPE_getOpInfo(i_inst->_current_oprtype,&i_inst->_oprbc_len,&i_inst->_inst_len); + //制御コードの解析 + if(i_inst->_oprbc_len>0){ + //パラメータのある制御命令未定義だからエラー。 + NyLPC_OnErrorGoto(ERROR); + } + //END制御命令? + if(i_inst->_current_opc==NyLPC_TcMiMicVM_CP_TYPE_END){ + *o_bin_len=0; + *o_parsed_bc=i+1; //パースしたBCの長さ + i_inst->tmp_len=0; + return NyLPC_TcMiMicTxtCompiler_RET_OK_END;//命令確定。(パース完了) + } + //END制御命令以外ならエラー + NyLPC_OnErrorGoto(ERROR); + }else{ + //不明な命令 + NyLPC_OnErrorGoto(ERROR); + } + } + break; + case NyLPC_TcMiMicTxtCompiler_ST_CTR: + NyLPC_OnErrorGoto(ERROR); + case NyLPC_TcMiMicTxtCompiler_ST_OPR: + //オペランド蓄積. + i_inst->tmp[i_inst->tmp_len]=*(i_bc->ptr+i); + i_inst->tmp_len++; + //オペランド長さになるまで値を追記 + if(i_inst->_oprbc_len==i_inst->tmp_len){ + //書込みポインタ保管 + wptr=(union NyLPC_TcMiMicVM_TInstruction*)i_bin->ptr; + //シーク + if(!NyLPC_TUInt32ArrayPtr_seek(i_bin,i_inst->_inst_len)){ + NyLPC_OnErrorGoto(ERROR); + } + wptr->op.opc=i_inst->_current_opc; + wptr->op.oprtype=i_inst->_current_oprtype; + //オペランドの変換処理 + switch(i_inst->_current_oprtype){ + case NyLPC_TcMiMicVM_OPR_TYPE_WM_WM: + if(!( txt2WMId(i_inst->tmp,&(wptr->wmwm_32.wm1)) && + txt2WMId(i_inst->tmp+2,&(wptr->wmwm_32.wm2)))) + { + NyLPC_OnErrorGoto(ERROR); + } + break; + case NyLPC_TcMiMicVM_OPR_TYPE_WM_H08: + if(!( txt2WMId(i_inst->tmp,&(wptr->wmh08_32.wm)) && + txt2UInt(i_inst->tmp+2,2,&(wptr->wmh08_32.h8)))) + { + NyLPC_OnErrorGoto(ERROR); + } + break; + case NyLPC_TcMiMicVM_OPR_TYPE_WM_H32: + if(!( txt2WMId(i_inst->tmp,&(wptr->wmh32_64.wm)) && + txt2UInt(i_inst->tmp+2,8,&(wptr->wmh32_64.h32)))){ + NyLPC_OnErrorGoto(ERROR); + } + break; + case NyLPC_TcMiMicVM_OPR_TYPE_WM: + if(!txt2WMId(i_inst->tmp,&(wptr->wm_32.wm))){ + NyLPC_OnErrorGoto(ERROR); + } + break; + case NyLPC_TcMiMicVM_OPR_TYPE_H32: + if(!txt2UInt(i_inst->tmp,8,&(wptr->h32_64.h32))){ + NyLPC_OnErrorGoto(ERROR); + } + break; + case NyLPC_TcMiMicVM_OPR_TYPE_H08: + if(!txt2UInt(i_inst->tmp,2,&(wptr->h8_32.h8))) + { + NyLPC_OnErrorGoto(ERROR); + } + break; + default: + NyLPC_OnErrorGoto(ERROR); + } + //OPR解析成功。パースしたブロックサイズの計算。 + *o_parsed_bc=i+1; //パースしたBCの長さ + *o_bin_len=i_inst->_inst_len; + i_inst->tmp_len=0; + i_inst->st=NyLPC_TcMiMicTxtCompiler_ST_OPC; + return NyLPC_TcMiMicTxtCompiler_RET_OK; + } + break; + default: + NyLPC_OnErrorGoto(ERROR); + } + } + *o_bin_len=0; + *o_parsed_bc=i_bc->len; + return NyLPC_TcMiMicTxtCompiler_RET_CONTINUE; ERROR: - i_inst->st=NyLPC_TcMiMicTxtCompiler_ST_NG; - return NyLPC_TcMiMicTxtCompiler_RET_NG; + i_inst->st=NyLPC_TcMiMicTxtCompiler_ST_NG; + return NyLPC_TcMiMicTxtCompiler_RET_NG; } @@ -226,11 +226,11 @@ ERROR: */ NyLPC_TcMiMicTxtCompiler_RET NyLPC_cMiMicTxtCompiler_compileFragment2(NyLPC_TcMiMicTxtCompiler_t* i_inst,NyLPC_TChar i_bc,struct NyLPC_TUInt32ArrayPtr* i_bin,NyLPC_TUInt16* o_bin_len) { - struct NyLPC_TCharArrayPtr bc; - NyLPC_TUInt16 bc_len; - bc.ptr=&i_bc; - bc.len=1; - return NyLPC_cMiMicTxtCompiler_compileFragment(i_inst,&bc,i_bin,o_bin_len,&bc_len); + struct NyLPC_TCharArrayPtr bc; + NyLPC_TUInt16 bc_len; + bc.ptr=&i_bc; + bc.len=1; + return NyLPC_cMiMicTxtCompiler_compileFragment(i_inst,&bc,i_bin,o_bin_len,&bc_len); } @@ -240,23 +240,23 @@ NyLPC_TcMiMicTxtCompiler_RET NyLPC_cMiMicTxtCompiler_compileFragment2(NyLPC_TcMi */ static NyLPC_TBool bc2ctrlc(const NyLPC_TChar* i_char,NyLPC_TcMiMicVM_OP_TYPE* o_opc,NyLPC_TcMiMicVM_OPR_TYPE* o_opt) { - //バイトコード変換の為のテーブル - const struct TInstructionDef _bc_type_tbl[]= - { - //制御命令 - {".E",NyLPC_TcMiMicVM_CP_TYPE_END,NyLPC_TcMiMicVM_OPR_TYPE_NONE}, - {NULL} - }; - int i; - for(i=0;_bc_type_tbl[i].bcopcode!=NULL;i++){ - //2バイト一致? - if((*i_char==_bc_type_tbl[i].bcopcode[0])&&(*(i_char+1)==_bc_type_tbl[i].bcopcode[1])){ - *o_opc=_bc_type_tbl[i].opid; - *o_opt=_bc_type_tbl[i].optype; - return NyLPC_TBool_TRUE; - } - } - return NyLPC_TBool_FALSE; + //バイトコード変換の為のテーブル + const struct TInstructionDef _bc_type_tbl[]= + { + //制御命令 + {".E",NyLPC_TcMiMicVM_CP_TYPE_END,NyLPC_TcMiMicVM_OPR_TYPE_NONE}, + {NULL} + }; + int i; + for(i=0;_bc_type_tbl[i].bcopcode!=NULL;i++){ + //2バイト一致? + if((*i_char==_bc_type_tbl[i].bcopcode[0])&&(*(i_char+1)==_bc_type_tbl[i].bcopcode[1])){ + *o_opc=_bc_type_tbl[i].opid; + *o_opt=_bc_type_tbl[i].optype; + return NyLPC_TBool_TRUE; + } + } + return NyLPC_TBool_FALSE; } @@ -265,62 +265,62 @@ static NyLPC_TBool bc2ctrlc(const NyLPC_TChar* i_char,NyLPC_TcMiMicVM_OP_TYPE* o */ static NyLPC_TBool bc2opc(const NyLPC_TChar* i_char,NyLPC_TcMiMicVM_OP_TYPE* o_opc,NyLPC_TcMiMicVM_OPR_TYPE* o_opt) { - //バイトコード変換の為のテーブル - const struct TInstructionDef _bc_type_tbl[]= - { - {"AA",NyLPC_TcMiMicVM_OP_TYPE_AND,NyLPC_TcMiMicVM_OPR_TYPE_WM_WM}, - {"AB",NyLPC_TcMiMicVM_OP_TYPE_AND,NyLPC_TcMiMicVM_OPR_TYPE_WM_H32}, - {"AE",NyLPC_TcMiMicVM_OP_TYPE_OR,NyLPC_TcMiMicVM_OPR_TYPE_WM_WM}, - {"AF",NyLPC_TcMiMicVM_OP_TYPE_OR,NyLPC_TcMiMicVM_OPR_TYPE_WM_H32}, - {"AI",NyLPC_TcMiMicVM_OP_TYPE_XOR,NyLPC_TcMiMicVM_OPR_TYPE_WM_WM}, - {"AJ",NyLPC_TcMiMicVM_OP_TYPE_XOR,NyLPC_TcMiMicVM_OPR_TYPE_WM_H32}, - {"AM",NyLPC_TcMiMicVM_OP_TYPE_NOT,NyLPC_TcMiMicVM_OPR_TYPE_WM}, - - {"BA",NyLPC_TcMiMicVM_OP_TYPE_SHL,NyLPC_TcMiMicVM_OPR_TYPE_WM_H08}, - {"BB",NyLPC_TcMiMicVM_OP_TYPE_SHL,NyLPC_TcMiMicVM_OPR_TYPE_WM_WM}, - {"BE",NyLPC_TcMiMicVM_OP_TYPE_SHR,NyLPC_TcMiMicVM_OPR_TYPE_WM_H08}, - {"BF",NyLPC_TcMiMicVM_OP_TYPE_SHR,NyLPC_TcMiMicVM_OPR_TYPE_WM_WM}, - - {"CA",NyLPC_TcMiMicVM_OP_TYPE_ADD,NyLPC_TcMiMicVM_OPR_TYPE_WM_WM}, - {"CB",NyLPC_TcMiMicVM_OP_TYPE_ADD,NyLPC_TcMiMicVM_OPR_TYPE_WM_H32}, - {"CE",NyLPC_TcMiMicVM_OP_TYPE_SUB,NyLPC_TcMiMicVM_OPR_TYPE_WM_WM}, - {"CF",NyLPC_TcMiMicVM_OP_TYPE_SUB,NyLPC_TcMiMicVM_OPR_TYPE_WM_H32}, - {"CI",NyLPC_TcMiMicVM_OP_TYPE_MUL,NyLPC_TcMiMicVM_OPR_TYPE_WM_WM}, - {"CJ",NyLPC_TcMiMicVM_OP_TYPE_MUL,NyLPC_TcMiMicVM_OPR_TYPE_WM_H32}, - - {"DA",NyLPC_TcMiMicVM_OP_TYPE_MGET,NyLPC_TcMiMicVM_OPR_TYPE_WM_H32}, - {"DB",NyLPC_TcMiMicVM_OP_TYPE_MGET,NyLPC_TcMiMicVM_OPR_TYPE_WM_WM}, - {"DE",NyLPC_TcMiMicVM_OP_TYPE_MPUT,NyLPC_TcMiMicVM_OPR_TYPE_WM_H32}, - {"DF",NyLPC_TcMiMicVM_OP_TYPE_MPUT,NyLPC_TcMiMicVM_OPR_TYPE_WM_WM}, - - {"EA",NyLPC_TcMiMicVM_OP_TYPE_SGET,NyLPC_TcMiMicVM_OPR_TYPE_WM}, - {"EE",NyLPC_TcMiMicVM_OP_TYPE_SPUT,NyLPC_TcMiMicVM_OPR_TYPE_WM}, - {"EF",NyLPC_TcMiMicVM_OP_TYPE_SPUT,NyLPC_TcMiMicVM_OPR_TYPE_H32}, - - {"FA",NyLPC_TcMiMicVM_OP_TYPE_LD,NyLPC_TcMiMicVM_OPR_TYPE_WM_WM}, - {"FB",NyLPC_TcMiMicVM_OP_TYPE_LD,NyLPC_TcMiMicVM_OPR_TYPE_WM_H32}, - - {"ZA",NyLPC_TcMiMicVM_OP_TYPE_NOP,NyLPC_TcMiMicVM_OPR_TYPE_NONE}, - {"ZB",NyLPC_TcMiMicVM_OP_TYPE_NOP,NyLPC_TcMiMicVM_OPR_TYPE_H08}, - - {"ZE",NyLPC_TcMiMicVM_OP_TYPE_CALL,NyLPC_TcMiMicVM_OPR_TYPE_WM}, - {"ZF",NyLPC_TcMiMicVM_OP_TYPE_CALL,NyLPC_TcMiMicVM_OPR_TYPE_H32}, - - - {"ZZ",NyLPC_TcMiMicVM_OP_TYPE_EXIT,NyLPC_TcMiMicVM_OPR_TYPE_NONE}, - {NULL} - }; - int i; - //ここ早くできますよね。 - for(i=0;_bc_type_tbl[i].bcopcode!=NULL;i++){ - //2バイト一致? - if((*i_char==_bc_type_tbl[i].bcopcode[0])&&(*(i_char+1)==_bc_type_tbl[i].bcopcode[1])){ - *o_opc=_bc_type_tbl[i].opid; - *o_opt=_bc_type_tbl[i].optype; - return NyLPC_TBool_TRUE; - } - } - return NyLPC_TBool_FALSE; + //バイトコード変換の為のテーブル + const struct TInstructionDef _bc_type_tbl[]= + { + {"AA",NyLPC_TcMiMicVM_OP_TYPE_AND,NyLPC_TcMiMicVM_OPR_TYPE_WM_WM}, + {"AB",NyLPC_TcMiMicVM_OP_TYPE_AND,NyLPC_TcMiMicVM_OPR_TYPE_WM_H32}, + {"AE",NyLPC_TcMiMicVM_OP_TYPE_OR,NyLPC_TcMiMicVM_OPR_TYPE_WM_WM}, + {"AF",NyLPC_TcMiMicVM_OP_TYPE_OR,NyLPC_TcMiMicVM_OPR_TYPE_WM_H32}, + {"AI",NyLPC_TcMiMicVM_OP_TYPE_XOR,NyLPC_TcMiMicVM_OPR_TYPE_WM_WM}, + {"AJ",NyLPC_TcMiMicVM_OP_TYPE_XOR,NyLPC_TcMiMicVM_OPR_TYPE_WM_H32}, + {"AM",NyLPC_TcMiMicVM_OP_TYPE_NOT,NyLPC_TcMiMicVM_OPR_TYPE_WM}, + + {"BA",NyLPC_TcMiMicVM_OP_TYPE_SHL,NyLPC_TcMiMicVM_OPR_TYPE_WM_H08}, + {"BB",NyLPC_TcMiMicVM_OP_TYPE_SHL,NyLPC_TcMiMicVM_OPR_TYPE_WM_WM}, + {"BE",NyLPC_TcMiMicVM_OP_TYPE_SHR,NyLPC_TcMiMicVM_OPR_TYPE_WM_H08}, + {"BF",NyLPC_TcMiMicVM_OP_TYPE_SHR,NyLPC_TcMiMicVM_OPR_TYPE_WM_WM}, + + {"CA",NyLPC_TcMiMicVM_OP_TYPE_ADD,NyLPC_TcMiMicVM_OPR_TYPE_WM_WM}, + {"CB",NyLPC_TcMiMicVM_OP_TYPE_ADD,NyLPC_TcMiMicVM_OPR_TYPE_WM_H32}, + {"CE",NyLPC_TcMiMicVM_OP_TYPE_SUB,NyLPC_TcMiMicVM_OPR_TYPE_WM_WM}, + {"CF",NyLPC_TcMiMicVM_OP_TYPE_SUB,NyLPC_TcMiMicVM_OPR_TYPE_WM_H32}, + {"CI",NyLPC_TcMiMicVM_OP_TYPE_MUL,NyLPC_TcMiMicVM_OPR_TYPE_WM_WM}, + {"CJ",NyLPC_TcMiMicVM_OP_TYPE_MUL,NyLPC_TcMiMicVM_OPR_TYPE_WM_H32}, + + {"DA",NyLPC_TcMiMicVM_OP_TYPE_MGET,NyLPC_TcMiMicVM_OPR_TYPE_WM_H32}, + {"DB",NyLPC_TcMiMicVM_OP_TYPE_MGET,NyLPC_TcMiMicVM_OPR_TYPE_WM_WM}, + {"DE",NyLPC_TcMiMicVM_OP_TYPE_MPUT,NyLPC_TcMiMicVM_OPR_TYPE_WM_H32}, + {"DF",NyLPC_TcMiMicVM_OP_TYPE_MPUT,NyLPC_TcMiMicVM_OPR_TYPE_WM_WM}, + + {"EA",NyLPC_TcMiMicVM_OP_TYPE_SGET,NyLPC_TcMiMicVM_OPR_TYPE_WM}, + {"EE",NyLPC_TcMiMicVM_OP_TYPE_SPUT,NyLPC_TcMiMicVM_OPR_TYPE_WM}, + {"EF",NyLPC_TcMiMicVM_OP_TYPE_SPUT,NyLPC_TcMiMicVM_OPR_TYPE_H32}, + + {"FA",NyLPC_TcMiMicVM_OP_TYPE_LD,NyLPC_TcMiMicVM_OPR_TYPE_WM_WM}, + {"FB",NyLPC_TcMiMicVM_OP_TYPE_LD,NyLPC_TcMiMicVM_OPR_TYPE_WM_H32}, + + {"ZA",NyLPC_TcMiMicVM_OP_TYPE_NOP,NyLPC_TcMiMicVM_OPR_TYPE_NONE}, + {"ZB",NyLPC_TcMiMicVM_OP_TYPE_NOP,NyLPC_TcMiMicVM_OPR_TYPE_H08}, + + {"ZE",NyLPC_TcMiMicVM_OP_TYPE_CALL,NyLPC_TcMiMicVM_OPR_TYPE_WM}, + {"ZF",NyLPC_TcMiMicVM_OP_TYPE_CALL,NyLPC_TcMiMicVM_OPR_TYPE_H32}, + + + {"ZZ",NyLPC_TcMiMicVM_OP_TYPE_EXIT,NyLPC_TcMiMicVM_OPR_TYPE_NONE}, + {NULL} + }; + int i; + //ここ早くできますよね。 + for(i=0;_bc_type_tbl[i].bcopcode!=NULL;i++){ + //2バイト一致? + if((*i_char==_bc_type_tbl[i].bcopcode[0])&&(*(i_char+1)==_bc_type_tbl[i].bcopcode[1])){ + *o_opc=_bc_type_tbl[i].opid; + *o_opt=_bc_type_tbl[i].optype; + return NyLPC_TBool_TRUE; + } + } + return NyLPC_TBool_FALSE; } @@ -333,123 +333,122 @@ static NyLPC_TBool bc2opc(const NyLPC_TChar* i_char,NyLPC_TcMiMicVM_OP_TYPE* o_o */ static NyLPC_TBool txt2UInt(const NyLPC_TChar* i_txt,NyLPC_TUInt8 i_num,void* out) { - NyLPC_TUInt32 ret=0; - NyLPC_TChar c; - int i; - - for(i=0;i=c && c>='a'){ - c=c-(NyLPC_TUInt8)'a'+10; - }else if('9'>=c && c>='0'){ - c-=(NyLPC_TUInt8)'0'; - }else{ - return NyLPC_TBool_FALSE; - } - ret=(ret<<4)|c; - } - //2,4,8だけ。 - switch(i_num){ - case 2: - *((NyLPC_TUInt8*)out)=(NyLPC_TUInt8)ret; - break; - case 4: - *((NyLPC_TUInt16*)out)=(NyLPC_TUInt16)ret; - break; - case 8: - *((NyLPC_TUInt32*)out)=(NyLPC_TUInt32)ret; - break; - default: - return NyLPC_TBool_FALSE; - } - return NyLPC_TBool_TRUE; + NyLPC_TUInt32 ret=0; + NyLPC_TChar c; + int i; + + for(i=0;i=c && c>='a'){ + c=c-(NyLPC_TUInt8)'a'+10; + }else if('9'>=c && c>='0'){ + c-=(NyLPC_TUInt8)'0'; + }else{ + return NyLPC_TBool_FALSE; + } + ret=(ret<<4)|c; + } + //2,4,8だけ。 + switch(i_num){ + case 2: + *((NyLPC_TUInt8*)out)=(NyLPC_TUInt8)ret; + break; + case 4: + *((NyLPC_TUInt16*)out)=(NyLPC_TUInt16)ret; + break; + case 8: + *((NyLPC_TUInt32*)out)=(NyLPC_TUInt32)ret; + break; + default: + return NyLPC_TBool_FALSE; + } + return NyLPC_TBool_TRUE; } /** * テキストデータをWMIDに変換する。WMIDは、VMの使用の影響を受ける。 */ static NyLPC_TBool txt2WMId(const NyLPC_TChar* i_txt,NyLPC_TUInt8* out) { - if(txt2UInt(i_txt,2,out)){ - if(*out<=NyLPC_TcMiMicVM_NUMBER_OF_WM){ - return NyLPC_TBool_TRUE; - } - } - return NyLPC_TBool_FALSE; + if(txt2UInt(i_txt,2,out)){ + if(*out<=NyLPC_TcMiMicVM_NUMBER_OF_WM){ + return NyLPC_TBool_TRUE; + } + } + return NyLPC_TBool_FALSE; } /** * オペランドタイプからオペランドのBC長と、インストラクションサイズを計算 */ static void NyLPC_TcMiMicVM_OPR_TYPE_getOpInfo(NyLPC_TcMiMicVM_OPR_TYPE i_type,NyLPC_TUInt8* oprbc_len,NyLPC_TUInt8* o_istlen) { - const struct{ - NyLPC_TcMiMicVM_OPR_TYPE t; - NyLPC_TUInt8 oprbc_len; - NyLPC_TUInt8 ist_len; - }_tbl[]={ - {NyLPC_TcMiMicVM_OPR_TYPE_NONE, 0, 1}, - {NyLPC_TcMiMicVM_OPR_TYPE_WM_WM, (1+1)*2, 1}, - {NyLPC_TcMiMicVM_OPR_TYPE_WM_H08, (1+1)*2, 1}, - {NyLPC_TcMiMicVM_OPR_TYPE_WM_H16, (1+2)*2, 2}, - {NyLPC_TcMiMicVM_OPR_TYPE_WM_H32, (1+4)*2, 2}, - {NyLPC_TcMiMicVM_OPR_TYPE_WM, (1)*2, 1}, - {NyLPC_TcMiMicVM_OPR_TYPE_H08, (1)*2, 1}, - {NyLPC_TcMiMicVM_OPR_TYPE_H16, (2)*2, 1}, - {NyLPC_TcMiMicVM_OPR_TYPE_H32, (4)*2, 2}, - {0,0,0} - }; - int i; - for(i=0;_tbl[i].t!=0;i++){ - if(_tbl[i].t==i_type){ - *oprbc_len=_tbl[i].oprbc_len; - *o_istlen=_tbl[i].ist_len; - return; - } - } - NyLPC_Abort(); - return; + const struct{ + NyLPC_TcMiMicVM_OPR_TYPE t; + NyLPC_TUInt8 oprbc_len; + NyLPC_TUInt8 ist_len; + }_tbl[]={ + {NyLPC_TcMiMicVM_OPR_TYPE_NONE, 0, 1}, + {NyLPC_TcMiMicVM_OPR_TYPE_WM_WM, (1+1)*2, 1}, + {NyLPC_TcMiMicVM_OPR_TYPE_WM_H08, (1+1)*2, 1}, + {NyLPC_TcMiMicVM_OPR_TYPE_WM_H16, (1+2)*2, 2}, + {NyLPC_TcMiMicVM_OPR_TYPE_WM_H32, (1+4)*2, 2}, + {NyLPC_TcMiMicVM_OPR_TYPE_WM, (1)*2, 1}, + {NyLPC_TcMiMicVM_OPR_TYPE_H08, (1)*2, 1}, + {NyLPC_TcMiMicVM_OPR_TYPE_H16, (2)*2, 1}, + {NyLPC_TcMiMicVM_OPR_TYPE_H32, (4)*2, 2}, + {0,0,0} + }; + int i; + for(i=0;_tbl[i].t!=0;i++){ + if(_tbl[i].t==i_type){ + *oprbc_len=_tbl[i].oprbc_len; + *o_istlen=_tbl[i].ist_len; + return; + } + } + NyLPC_Abort(); + return; } #define TEST #ifndef TEST void main(void) { - struct NyLPC_TCharArrayPtr bc; - const char* BC="AA0102AB0100000001AE0203AF0200000003AI0304AJ0300000004AM07BA0505BE0607CA0304CB0300000005CE0304CF0300000005CI0304CJ0300000005DA0400000000DE0400000000EA04EE04EF00000000ZAZZ.E"; - NyLPC_TcMiMicTxtCompiler_t inst; - struct NyLPC_TUInt32ArrayPtr bin; - - NyLPC_TUInt16 l,bl; - NyLPC_TUInt32 obuf[1024]; - NyLPC_cMiMicBcCompiler_initialize(&inst); - bc.ptr=(char* )BC; - bc.len=strlen(BC); - bin.ptr=obuf; - bin.len=5; - - for(;;){ - - switch(NyLPC_cMiMicBcCompiler_compileFragment(&inst,&bc,&bin,&bl,&l)) - { - case NyLPC_TcMiMicTxtCompiler_RET_OK: - //命令確定。 - NyLPC_TUInt32ArrayPtr_seek(&bin,bl); - NyLPC_TCharArrayPtr_seek(&bc,l); - break; - case NyLPC_TcMiMicTxtCompiler_RET_OK_END: - //命令終端 - printf("OK"); - break; - case NyLPC_TcMiMicTxtCompiler_RET_CONTINUE: - //蓄積中。 - NyLPC_TCharArrayPtr_seek(&bc,l); - break; - case NyLPC_TcMiMicTxtCompiler_RET_NG: - printf("エラー"); - return; - default: - break; - } - } + struct NyLPC_TCharArrayPtr bc; + const char* BC="AA0102AB0100000001AE0203AF0200000003AI0304AJ0300000004AM07BA0505BE0607CA0304CB0300000005CE0304CF0300000005CI0304CJ0300000005DA0400000000DE0400000000EA04EE04EF00000000ZAZZ.E"; + NyLPC_TcMiMicTxtCompiler_t inst; + struct NyLPC_TUInt32ArrayPtr bin; + + NyLPC_TUInt16 l,bl; + NyLPC_TUInt32 obuf[1024]; + NyLPC_cMiMicBcCompiler_initialize(&inst); + bc.ptr=(char* )BC; + bc.len=strlen(BC); + bin.ptr=obuf; + bin.len=5; + + for(;;){ + + switch(NyLPC_cMiMicBcCompiler_compileFragment(&inst,&bc,&bin,&bl,&l)) + { + case NyLPC_TcMiMicTxtCompiler_RET_OK: + //命令確定。 + NyLPC_TUInt32ArrayPtr_seek(&bin,bl); + NyLPC_TCharArrayPtr_seek(&bc,l); + break; + case NyLPC_TcMiMicTxtCompiler_RET_OK_END: + //命令終端 + printf("OK"); + break; + case NyLPC_TcMiMicTxtCompiler_RET_CONTINUE: + //蓄積中。 + NyLPC_TCharArrayPtr_seek(&bc,l); + break; + case NyLPC_TcMiMicTxtCompiler_RET_NG: + printf("エラー"); + return; + default: + break; + } + } } #endif - diff --git a/lib/src/mimicvm/NyLPC_cMiMicTxtCompiler.h b/lib/src/mimicvm/NyLPC_cMiMicTxtCompiler.h index 0bbbaed..86c9bde 100644 --- a/lib/src/mimicvm/NyLPC_cMiMicTxtCompiler.h +++ b/lib/src/mimicvm/NyLPC_cMiMicTxtCompiler.h @@ -19,8 +19,8 @@ * along with this program. If not, see . * * For further information please contact. - * http://nyatla.jp/ - * or + * http://nyatla.jp/ + * or * *********************************************************************************/ #ifndef NYLPC_CMIMICTXTCOMPILER_H_ @@ -41,29 +41,29 @@ typedef struct NyLPC_TcMiMicTxtCompiler NyLPC_TcMiMicTxtCompiler_t; typedef NyLPC_TUInt8 NyLPC_TcMiMicTxtCompiler_ST; -#define NyLPC_TcMiMicTxtCompiler_ST_OPC 0x01 //OPをパース中 -#define NyLPC_TcMiMicTxtCompiler_ST_OPR 0x02 //オペランドを解析中 -#define NyLPC_TcMiMicTxtCompiler_ST_CTR 0x03 //制御パラメータを解析中 -#define NyLPC_TcMiMicTxtCompiler_ST_OK 0x04 //パースを完了した。 -#define NyLPC_TcMiMicTxtCompiler_ST_NG 0xff //パースでエラーが発生ウェーイ +#define NyLPC_TcMiMicTxtCompiler_ST_OPC 0x01 //OPをパース中 +#define NyLPC_TcMiMicTxtCompiler_ST_OPR 0x02 //オペランドを解析中 +#define NyLPC_TcMiMicTxtCompiler_ST_CTR 0x03 //制御パラメータを解析中 +#define NyLPC_TcMiMicTxtCompiler_ST_OK 0x04 //パースを完了した。 +#define NyLPC_TcMiMicTxtCompiler_ST_NG 0xff //パースでエラーが発生ウェーイ struct NyLPC_TcMiMicTxtCompiler{ - NyLPC_TUInt8* out_buf; - NyLPC_TUInt8 st;//ステータス - NyLPC_TcMiMicVM_OP_TYPE _current_opc; //解析中のオペコード - NyLPC_TUInt8 _current_oprtype; //解析中のオペランドタイプ - NyLPC_TUInt8 _inst_len; //解析中のインストラクションセットの長さ - NyLPC_TUInt8 _oprbc_len; //オペコード解析の一時変数 - NyLPC_TUInt8 tmp_len;//tmpに格納したデータの数を数えたり。 - NyLPC_TChar tmp[24]; + NyLPC_TUInt8* out_buf; + NyLPC_TUInt8 st;//ステータス + NyLPC_TcMiMicVM_OP_TYPE _current_opc; //解析中のオペコード + NyLPC_TUInt8 _current_oprtype; //解析中のオペランドタイプ + NyLPC_TUInt8 _inst_len; //解析中のインストラクションセットの長さ + NyLPC_TUInt8 _oprbc_len; //オペコード解析の一時変数 + NyLPC_TUInt8 tmp_len;//tmpに格納したデータの数を数えたり。 + NyLPC_TChar tmp[24]; }; typedef NyLPC_TUInt8 NyLPC_TcMiMicTxtCompiler_RET; #define NyLPC_TcMiMicTxtCompiler_RET_OK 0x00 -#define NyLPC_TcMiMicTxtCompiler_RET_OK_END 0x01 //命令のパースに成功し、かつEND命令を検出。 -#define NyLPC_TcMiMicTxtCompiler_RET_CONTINUE 0x02 //続きのBCを要求している。 -#define NyLPC_TcMiMicTxtCompiler_RET_NG 0x80 +#define NyLPC_TcMiMicTxtCompiler_RET_OK_END 0x01 //命令のパースに成功し、かつEND命令を検出。 +#define NyLPC_TcMiMicTxtCompiler_RET_CONTINUE 0x02 //続きのBCを要求している。 +#define NyLPC_TcMiMicTxtCompiler_RET_NG 0x80 @@ -78,4 +78,4 @@ NyLPC_TcMiMicTxtCompiler_RET NyLPC_cMiMicTxtCompiler_compileFragment2(NyLPC_TcMi #endif /* __cplusplus */ -#endif /* NYLPC_CMIMICTXTCOMPILER_H_ */ +#endif /* NYLPC_CMIMICTXTCOMPILER_H_ */ \ No newline at end of file diff --git a/lib/src/mimicvm/NyLPC_cMiMicVM.c b/lib/src/mimicvm/NyLPC_cMiMicVM.c index e5bb4a9..dd6f2dd 100644 --- a/lib/src/mimicvm/NyLPC_cMiMicVM.c +++ b/lib/src/mimicvm/NyLPC_cMiMicVM.c @@ -19,8 +19,8 @@ * along with this program. If not, see . * * For further information please contact. - * http://nyatla.jp/ - * or + * http://nyatla.jp/ + * or * *********************************************************************************/ #include @@ -33,13 +33,13 @@ static NyLPC_TUInt8 process_instruction(NyLPC_TcMiMicVM_t* i_inst,const union Ny void NyLPC_cMiMicVM_initialize(NyLPC_TcMiMicVM_t* i_inst,struct NyLPC_TcMiMicVM_TEvent* i_handler) { - NyLPC_Assert(i_inst!=NULL); - NyLPC_Assert(i_handler!=NULL); - NyLPC_Assert(i_handler->get_stream!=NULL); - NyLPC_Assert(i_handler->put_stream!=NULL); - NyLPC_Assert(i_handler->sleep!=NULL); - i_inst->_event_handler=i_handler; - return; + NyLPC_Assert(i_inst!=NULL); + NyLPC_Assert(i_handler!=NULL); + NyLPC_Assert(i_handler->get_stream!=NULL); + NyLPC_Assert(i_handler->put_stream!=NULL); + NyLPC_Assert(i_handler->sleep!=NULL); + i_inst->_event_handler=i_handler; + return; } @@ -50,40 +50,40 @@ void NyLPC_cMiMicVM_initialize(NyLPC_TcMiMicVM_t* i_inst,struct NyLPC_TcMiMicVM_ */ NyLPC_TUInt32 NyLPC_cMiMicVM_run(NyLPC_TcMiMicVM_t* i_inst,const NyLPC_TUInt32* i_instruction,const NyLPC_TUInt16 i_size_of_instruction) { - //データ部をgetstreamと連動させること。 - NyLPC_TUInt32 retcode=NyLPC_cMiMicVM_RESULT_OK; - NyLPC_TUInt16 pc=0; - NyLPC_TUInt8 proc_in_byte; - if(i_size_of_instruction>0){ - proc_in_byte=process_instruction(i_inst,(const union NyLPC_TcMiMicVM_TInstruction*)(i_instruction+pc),&retcode); - pc+=proc_in_byte; - //プログラムの終端に到達するか、0バイト処理の場合にブレーク。 - while(proc_in_byte>0 && pc0){ + proc_in_byte=process_instruction(i_inst,(const union NyLPC_TcMiMicVM_TInstruction*)(i_instruction+pc),&retcode); + pc+=proc_in_byte; + //プログラムの終端に到達するか、0バイト処理の場合にブレーク。 + while(proc_in_byte>0 && pc_event_handler->put_stream(i_inst->_event_handler,i_val)){ - return NyLPC_TBool_FALSE; - } - return NyLPC_TBool_TRUE; + if(!i_inst->_event_handler->put_stream(i_inst->_event_handler,i_val)){ + return NyLPC_TBool_FALSE; + } + return NyLPC_TBool_TRUE; } /** * 入力ストリームから32ビット値を読み出す。 */ NyLPC_TBool NyLPC_cMiMicVM_sget(NyLPC_TcMiMicVM_t* i_inst,NyLPC_TUInt32* o_val) { - if(!i_inst->_event_handler->get_stream(i_inst->_event_handler,o_val)){ - return NyLPC_TBool_FALSE; - } - return NyLPC_TBool_TRUE; + if(!i_inst->_event_handler->get_stream(i_inst->_event_handler,o_val)){ + return NyLPC_TBool_FALSE; + } + return NyLPC_TBool_TRUE; } @@ -97,234 +97,234 @@ NyLPC_TBool NyLPC_cMiMicVM_sget(NyLPC_TcMiMicVM_t* i_inst,NyLPC_TUInt32* o_val) */ static NyLPC_TUInt8 process_instruction(NyLPC_TcMiMicVM_t* i_inst,const union NyLPC_TcMiMicVM_TInstruction* ist,NyLPC_TUInt32* o_code) { - NyLPC_TUInt32 tret; - switch(ist->op.opc){ - case NyLPC_TcMiMicVM_OP_TYPE_AND: - switch(ist->op.oprtype){ - case NyLPC_TcMiMicVM_OPR_TYPE_WM_WM: - i_inst->wm[ist->wmwm_32.wm1]&=i_inst->wm[ist->wmwm_32.wm2]; - break; - case NyLPC_TcMiMicVM_OPR_TYPE_WM_H32: - i_inst->wm[ist->wmh32_64.wm]&=ist->wmh32_64.h32; - break; - default: - NyLPC_OnErrorGoto(ERROR); - } - break; - case NyLPC_TcMiMicVM_OP_TYPE_OR: - switch(ist->op.oprtype){ - case NyLPC_TcMiMicVM_OPR_TYPE_WM_WM: - i_inst->wm[ist->wmwm_32.wm1]|=i_inst->wm[ist->wmwm_32.wm2]; - break; - case NyLPC_TcMiMicVM_OPR_TYPE_WM_H32: - i_inst->wm[ist->wmh32_64.wm]|=ist->wmh32_64.h32; - break; - default: - NyLPC_OnErrorGoto(ERROR); - } - break; - case NyLPC_TcMiMicVM_OP_TYPE_XOR: - switch(ist->op.oprtype){ - case NyLPC_TcMiMicVM_OPR_TYPE_WM_WM: - i_inst->wm[ist->wmwm_32.wm1]^=i_inst->wm[ist->wmwm_32.wm2]; - break; - case NyLPC_TcMiMicVM_OPR_TYPE_WM_H32: - i_inst->wm[ist->wmh32_64.wm]^=ist->wmh32_64.h32; - break; - default: - NyLPC_OnErrorGoto(ERROR); - } - break; - case NyLPC_TcMiMicVM_OP_TYPE_NOT: - switch(ist->op.oprtype){ - case NyLPC_TcMiMicVM_OPR_TYPE_WM: - i_inst->wm[ist->wm_32.wm]=~i_inst->wm[ist->wm_32.wm]; - break; - default: - NyLPC_OnErrorGoto(ERROR); - } - break; - case NyLPC_TcMiMicVM_OP_TYPE_SHL: - switch(ist->op.oprtype){ - case NyLPC_TcMiMicVM_OPR_TYPE_WM_H08: - i_inst->wm[ist->wmh08_32.wm]=i_inst->wm[ist->wmh08_32.wm]<wmh08_32.h8; - break; - case NyLPC_TcMiMicVM_OPR_TYPE_WM_WM: - i_inst->wm[ist->wmwm_32.wm1]=i_inst->wm[ist->wmwm_32.wm1]<wm[ist->wmwm_32.wm2]; - break; - default: - NyLPC_OnErrorGoto(ERROR); - } - break; - case NyLPC_TcMiMicVM_OP_TYPE_SHR: - switch(ist->op.oprtype){ - case NyLPC_TcMiMicVM_OPR_TYPE_WM_H08: - i_inst->wm[ist->wmh08_32.wm]=i_inst->wm[ist->wmh08_32.wm]>>ist->wmh08_32.h8; - break; - case NyLPC_TcMiMicVM_OPR_TYPE_WM_WM: - i_inst->wm[ist->wmwm_32.wm1]=i_inst->wm[ist->wmwm_32.wm1]>>i_inst->wm[ist->wmwm_32.wm2]; - break; - default: - NyLPC_OnErrorGoto(ERROR); - } - break; - case NyLPC_TcMiMicVM_OP_TYPE_ADD: - switch(ist->op.oprtype){ - case NyLPC_TcMiMicVM_OPR_TYPE_WM_WM: - i_inst->wm[ist->wmwm_32.wm1]+=i_inst->wm[ist->wmwm_32.wm2]; - break; - case NyLPC_TcMiMicVM_OPR_TYPE_WM_H32: - i_inst->wm[ist->wmh32_64.wm]+=ist->wmh32_64.h32; - break; - default: - NyLPC_OnErrorGoto(ERROR); - } - break; - case NyLPC_TcMiMicVM_OP_TYPE_SUB: - switch(ist->op.oprtype){ - case NyLPC_TcMiMicVM_OPR_TYPE_WM_WM: - i_inst->wm[ist->wmwm_32.wm1]-=i_inst->wm[ist->wmwm_32.wm2]; - break; - case NyLPC_TcMiMicVM_OPR_TYPE_WM_H32: - i_inst->wm[ist->wmh32_64.wm]-=ist->wmh32_64.h32; - break; - default: - NyLPC_OnErrorGoto(ERROR); - } - break; - case NyLPC_TcMiMicVM_OP_TYPE_MUL: - switch(ist->op.oprtype){ - case NyLPC_TcMiMicVM_OPR_TYPE_WM_WM: - i_inst->wm[ist->wmwm_32.wm1]*=i_inst->wm[ist->wmwm_32.wm2]; - break; - case NyLPC_TcMiMicVM_OPR_TYPE_WM_H32: - i_inst->wm[ist->wmh32_64.wm]*=ist->wmh32_64.h32; - break; - default: - NyLPC_OnErrorGoto(ERROR); - } - break; - case NyLPC_TcMiMicVM_OP_TYPE_MGET: - switch(ist->op.oprtype){ - case NyLPC_TcMiMicVM_OPR_TYPE_WM_H32: - i_inst->wm[ist->wmh32_64.wm]=*((NyLPC_TUInt32*)(ist->wmh32_64.h32)); - break; - case NyLPC_TcMiMicVM_OPR_TYPE_WM_WM: - i_inst->wm[ist->wmwm_32.wm1]=*((NyLPC_TUInt32*)(i_inst->wm[ist->wmwm_32.wm2])); - break; - default: - NyLPC_OnErrorGoto(ERROR); - } - break; - case NyLPC_TcMiMicVM_OP_TYPE_MPUT: - switch(ist->op.oprtype){ - case NyLPC_TcMiMicVM_OPR_TYPE_WM_H32: - *((NyLPC_TUInt32*)(ist->wmh32_64.h32))=i_inst->wm[ist->wmh32_64.wm]; - break; - case NyLPC_TcMiMicVM_OPR_TYPE_WM_WM: - *((NyLPC_TUInt32*)(i_inst->wm[ist->wmwm_32.wm2]))=i_inst->wm[ist->wmwm_32.wm1]; - break; - default: - NyLPC_OnErrorGoto(ERROR); - } - break; - case NyLPC_TcMiMicVM_OP_TYPE_SGET: - switch(ist->op.oprtype){ - case NyLPC_TcMiMicVM_OPR_TYPE_WM: - if(!i_inst->_event_handler->get_stream(i_inst->_event_handler,&(i_inst->wm[ist->wm_32.wm]))){ - NyLPC_OnErrorGoto(ERROR); - } - break; - default: - NyLPC_OnErrorGoto(ERROR); - } - break; - case NyLPC_TcMiMicVM_OP_TYPE_SPUT: - switch(ist->op.oprtype){ - case NyLPC_TcMiMicVM_OPR_TYPE_WM: - if(!i_inst->_event_handler->put_stream(i_inst->_event_handler,i_inst->wm[ist->wm_32.wm])){ - NyLPC_OnErrorGoto(ERROR); - } - break; - case NyLPC_TcMiMicVM_OPR_TYPE_H32: - if(!i_inst->_event_handler->put_stream(i_inst->_event_handler,ist->h32_64.h32)){ - NyLPC_OnErrorGoto(ERROR); - } - break; - default: - NyLPC_OnErrorGoto(ERROR); - } - break; - case NyLPC_TcMiMicVM_OP_TYPE_LD: - switch(ist->op.oprtype){ - case NyLPC_TcMiMicVM_OPR_TYPE_WM_WM: - i_inst->wm[ist->wmwm_32.wm1]=i_inst->wm[ist->wmwm_32.wm2]; - break; - case NyLPC_TcMiMicVM_OPR_TYPE_WM_H32: - i_inst->wm[ist->wmh32_64.wm]=ist->wmh32_64.h32; - break; - default: - NyLPC_OnErrorGoto(ERROR); - } - break; - case NyLPC_TcMiMicVM_OP_TYPE_NOP: - switch(ist->op.oprtype){ - case NyLPC_TcMiMicVM_OPR_TYPE_NONE: - break; - case NyLPC_TcMiMicVM_OPR_TYPE_H08: - i_inst->_event_handler->sleep(i_inst->_event_handler,ist->h8_32.h8); - break; - default: - NyLPC_OnErrorGoto(ERROR); - } - break; - //native call - case NyLPC_TcMiMicVM_OP_TYPE_CALL: - switch(ist->op.oprtype){ - case NyLPC_TcMiMicVM_OPR_TYPE_WM: - tret=i_inst->_event_handler->native_call(i_inst->_event_handler,i_inst->wm[ist->wm_32.wm],i_inst); - if(!NyLPC_cMiMicVM_RESULT_isOK(tret)){ - *o_code=tret; - NyLPC_OnErrorGoto(ERROR_INHERIT);//エラー継承 - } - break; - case NyLPC_TcMiMicVM_OPR_TYPE_H32: - tret=i_inst->_event_handler->native_call(i_inst->_event_handler,ist->h32_64.h32,i_inst); - if(!NyLPC_cMiMicVM_RESULT_isOK(tret)){ - *o_code=tret; - NyLPC_OnErrorGoto(ERROR_INHERIT);//エラー継承 - } - break; - default: - NyLPC_OnErrorGoto(ERROR); - } - break; - case NyLPC_TcMiMicVM_OP_TYPE_EXIT: - *o_code=NyLPC_cMiMicVM_RESULT_OK;//OKに上書き - return 0; - default: - NyLPC_OnErrorGoto(ERROR); - } - //実行したコードのワード長を返す。 - switch(ist->op.oprtype){ - case NyLPC_TcMiMicVM_OPR_TYPE_NONE: - case NyLPC_TcMiMicVM_OPR_TYPE_WM_WM: - case NyLPC_TcMiMicVM_OPR_TYPE_WM_H08: - case NyLPC_TcMiMicVM_OPR_TYPE_WM: - case NyLPC_TcMiMicVM_OPR_TYPE_H08: - case NyLPC_TcMiMicVM_OPR_TYPE_H16: - *o_code=NyLPC_cMiMicVM_RESULT_OK;//OKに上書き - return 1; - case NyLPC_TcMiMicVM_OPR_TYPE_WM_H16: - case NyLPC_TcMiMicVM_OPR_TYPE_WM_H32: - case NyLPC_TcMiMicVM_OPR_TYPE_H32: - *o_code=NyLPC_cMiMicVM_RESULT_OK;//OKに上書き - return 2; - } + NyLPC_TUInt32 tret; + switch(ist->op.opc){ + case NyLPC_TcMiMicVM_OP_TYPE_AND: + switch(ist->op.oprtype){ + case NyLPC_TcMiMicVM_OPR_TYPE_WM_WM: + i_inst->wm[ist->wmwm_32.wm1]&=i_inst->wm[ist->wmwm_32.wm2]; + break; + case NyLPC_TcMiMicVM_OPR_TYPE_WM_H32: + i_inst->wm[ist->wmh32_64.wm]&=ist->wmh32_64.h32; + break; + default: + NyLPC_OnErrorGoto(ERROR); + } + break; + case NyLPC_TcMiMicVM_OP_TYPE_OR: + switch(ist->op.oprtype){ + case NyLPC_TcMiMicVM_OPR_TYPE_WM_WM: + i_inst->wm[ist->wmwm_32.wm1]|=i_inst->wm[ist->wmwm_32.wm2]; + break; + case NyLPC_TcMiMicVM_OPR_TYPE_WM_H32: + i_inst->wm[ist->wmh32_64.wm]|=ist->wmh32_64.h32; + break; + default: + NyLPC_OnErrorGoto(ERROR); + } + break; + case NyLPC_TcMiMicVM_OP_TYPE_XOR: + switch(ist->op.oprtype){ + case NyLPC_TcMiMicVM_OPR_TYPE_WM_WM: + i_inst->wm[ist->wmwm_32.wm1]^=i_inst->wm[ist->wmwm_32.wm2]; + break; + case NyLPC_TcMiMicVM_OPR_TYPE_WM_H32: + i_inst->wm[ist->wmh32_64.wm]^=ist->wmh32_64.h32; + break; + default: + NyLPC_OnErrorGoto(ERROR); + } + break; + case NyLPC_TcMiMicVM_OP_TYPE_NOT: + switch(ist->op.oprtype){ + case NyLPC_TcMiMicVM_OPR_TYPE_WM: + i_inst->wm[ist->wm_32.wm]=~i_inst->wm[ist->wm_32.wm]; + break; + default: + NyLPC_OnErrorGoto(ERROR); + } + break; + case NyLPC_TcMiMicVM_OP_TYPE_SHL: + switch(ist->op.oprtype){ + case NyLPC_TcMiMicVM_OPR_TYPE_WM_H08: + i_inst->wm[ist->wmh08_32.wm]=i_inst->wm[ist->wmh08_32.wm]<wmh08_32.h8; + break; + case NyLPC_TcMiMicVM_OPR_TYPE_WM_WM: + i_inst->wm[ist->wmwm_32.wm1]=i_inst->wm[ist->wmwm_32.wm1]<wm[ist->wmwm_32.wm2]; + break; + default: + NyLPC_OnErrorGoto(ERROR); + } + break; + case NyLPC_TcMiMicVM_OP_TYPE_SHR: + switch(ist->op.oprtype){ + case NyLPC_TcMiMicVM_OPR_TYPE_WM_H08: + i_inst->wm[ist->wmh08_32.wm]=i_inst->wm[ist->wmh08_32.wm]>>ist->wmh08_32.h8; + break; + case NyLPC_TcMiMicVM_OPR_TYPE_WM_WM: + i_inst->wm[ist->wmwm_32.wm1]=i_inst->wm[ist->wmwm_32.wm1]>>i_inst->wm[ist->wmwm_32.wm2]; + break; + default: + NyLPC_OnErrorGoto(ERROR); + } + break; + case NyLPC_TcMiMicVM_OP_TYPE_ADD: + switch(ist->op.oprtype){ + case NyLPC_TcMiMicVM_OPR_TYPE_WM_WM: + i_inst->wm[ist->wmwm_32.wm1]+=i_inst->wm[ist->wmwm_32.wm2]; + break; + case NyLPC_TcMiMicVM_OPR_TYPE_WM_H32: + i_inst->wm[ist->wmh32_64.wm]+=ist->wmh32_64.h32; + break; + default: + NyLPC_OnErrorGoto(ERROR); + } + break; + case NyLPC_TcMiMicVM_OP_TYPE_SUB: + switch(ist->op.oprtype){ + case NyLPC_TcMiMicVM_OPR_TYPE_WM_WM: + i_inst->wm[ist->wmwm_32.wm1]-=i_inst->wm[ist->wmwm_32.wm2]; + break; + case NyLPC_TcMiMicVM_OPR_TYPE_WM_H32: + i_inst->wm[ist->wmh32_64.wm]-=ist->wmh32_64.h32; + break; + default: + NyLPC_OnErrorGoto(ERROR); + } + break; + case NyLPC_TcMiMicVM_OP_TYPE_MUL: + switch(ist->op.oprtype){ + case NyLPC_TcMiMicVM_OPR_TYPE_WM_WM: + i_inst->wm[ist->wmwm_32.wm1]*=i_inst->wm[ist->wmwm_32.wm2]; + break; + case NyLPC_TcMiMicVM_OPR_TYPE_WM_H32: + i_inst->wm[ist->wmh32_64.wm]*=ist->wmh32_64.h32; + break; + default: + NyLPC_OnErrorGoto(ERROR); + } + break; + case NyLPC_TcMiMicVM_OP_TYPE_MGET: + switch(ist->op.oprtype){ + case NyLPC_TcMiMicVM_OPR_TYPE_WM_H32: + i_inst->wm[ist->wmh32_64.wm]=*((NyLPC_TUInt32*)(ist->wmh32_64.h32)); + break; + case NyLPC_TcMiMicVM_OPR_TYPE_WM_WM: + i_inst->wm[ist->wmwm_32.wm1]=*((NyLPC_TUInt32*)(i_inst->wm[ist->wmwm_32.wm2])); + break; + default: + NyLPC_OnErrorGoto(ERROR); + } + break; + case NyLPC_TcMiMicVM_OP_TYPE_MPUT: + switch(ist->op.oprtype){ + case NyLPC_TcMiMicVM_OPR_TYPE_WM_H32: + *((NyLPC_TUInt32*)(ist->wmh32_64.h32))=i_inst->wm[ist->wmh32_64.wm]; + break; + case NyLPC_TcMiMicVM_OPR_TYPE_WM_WM: + *((NyLPC_TUInt32*)(i_inst->wm[ist->wmwm_32.wm2]))=i_inst->wm[ist->wmwm_32.wm1]; + break; + default: + NyLPC_OnErrorGoto(ERROR); + } + break; + case NyLPC_TcMiMicVM_OP_TYPE_SGET: + switch(ist->op.oprtype){ + case NyLPC_TcMiMicVM_OPR_TYPE_WM: + if(!i_inst->_event_handler->get_stream(i_inst->_event_handler,&(i_inst->wm[ist->wm_32.wm]))){ + NyLPC_OnErrorGoto(ERROR); + } + break; + default: + NyLPC_OnErrorGoto(ERROR); + } + break; + case NyLPC_TcMiMicVM_OP_TYPE_SPUT: + switch(ist->op.oprtype){ + case NyLPC_TcMiMicVM_OPR_TYPE_WM: + if(!i_inst->_event_handler->put_stream(i_inst->_event_handler,i_inst->wm[ist->wm_32.wm])){ + NyLPC_OnErrorGoto(ERROR); + } + break; + case NyLPC_TcMiMicVM_OPR_TYPE_H32: + if(!i_inst->_event_handler->put_stream(i_inst->_event_handler,ist->h32_64.h32)){ + NyLPC_OnErrorGoto(ERROR); + } + break; + default: + NyLPC_OnErrorGoto(ERROR); + } + break; + case NyLPC_TcMiMicVM_OP_TYPE_LD: + switch(ist->op.oprtype){ + case NyLPC_TcMiMicVM_OPR_TYPE_WM_WM: + i_inst->wm[ist->wmwm_32.wm1]=i_inst->wm[ist->wmwm_32.wm2]; + break; + case NyLPC_TcMiMicVM_OPR_TYPE_WM_H32: + i_inst->wm[ist->wmh32_64.wm]=ist->wmh32_64.h32; + break; + default: + NyLPC_OnErrorGoto(ERROR); + } + break; + case NyLPC_TcMiMicVM_OP_TYPE_NOP: + switch(ist->op.oprtype){ + case NyLPC_TcMiMicVM_OPR_TYPE_NONE: + break; + case NyLPC_TcMiMicVM_OPR_TYPE_H08: + i_inst->_event_handler->sleep(i_inst->_event_handler,ist->h8_32.h8); + break; + default: + NyLPC_OnErrorGoto(ERROR); + } + break; + //native call + case NyLPC_TcMiMicVM_OP_TYPE_CALL: + switch(ist->op.oprtype){ + case NyLPC_TcMiMicVM_OPR_TYPE_WM: + tret=i_inst->_event_handler->native_call(i_inst->_event_handler,i_inst->wm[ist->wm_32.wm],i_inst); + if(!NyLPC_cMiMicVM_RESULT_isOK(tret)){ + *o_code=tret; + NyLPC_OnErrorGoto(ERROR_INHERIT);//エラー継承 + } + break; + case NyLPC_TcMiMicVM_OPR_TYPE_H32: + tret=i_inst->_event_handler->native_call(i_inst->_event_handler,ist->h32_64.h32,i_inst); + if(!NyLPC_cMiMicVM_RESULT_isOK(tret)){ + *o_code=tret; + NyLPC_OnErrorGoto(ERROR_INHERIT);//エラー継承 + } + break; + default: + NyLPC_OnErrorGoto(ERROR); + } + break; + case NyLPC_TcMiMicVM_OP_TYPE_EXIT: + *o_code=NyLPC_cMiMicVM_RESULT_OK;//OKに上書き + return 0; + default: + NyLPC_OnErrorGoto(ERROR); + } + //実行したコードのワード長を返す。 + switch(ist->op.oprtype){ + case NyLPC_TcMiMicVM_OPR_TYPE_NONE: + case NyLPC_TcMiMicVM_OPR_TYPE_WM_WM: + case NyLPC_TcMiMicVM_OPR_TYPE_WM_H08: + case NyLPC_TcMiMicVM_OPR_TYPE_WM: + case NyLPC_TcMiMicVM_OPR_TYPE_H08: + case NyLPC_TcMiMicVM_OPR_TYPE_H16: + *o_code=NyLPC_cMiMicVM_RESULT_OK;//OKに上書き + return 1; + case NyLPC_TcMiMicVM_OPR_TYPE_WM_H16: + case NyLPC_TcMiMicVM_OPR_TYPE_WM_H32: + case NyLPC_TcMiMicVM_OPR_TYPE_H32: + *o_code=NyLPC_cMiMicVM_RESULT_OK;//OKに上書き + return 2; + } ERROR: - *o_code=NyLPC_cMiMicVM_RESULT_RUNTIME_NG;//ランタイムNG + *o_code=NyLPC_cMiMicVM_RESULT_RUNTIME_NG;//ランタイムNG ERROR_INHERIT: - return 0; + return 0; } #define TEST @@ -333,52 +333,52 @@ ERROR_INHERIT: #include "NyLPC_cMiMicTxtCompiler.h" void main(void) { - struct NyLPC_TcMiMicVM_TEvent eh; - NyLPC_TUInt32 ap; - NyLPC_TcMiMicVM_t vm; - struct NyLPC_TCharArrayPtr bc; - NyLPC_TcMiMicTxtCompiler_t inst; - struct NyLPC_TUInt32ArrayPtr bin; - char BC[1024]; - int ist_len; + struct NyLPC_TcMiMicVM_TEvent eh; + NyLPC_TUInt32 ap; + NyLPC_TcMiMicVM_t vm; + struct NyLPC_TCharArrayPtr bc; + NyLPC_TcMiMicTxtCompiler_t inst; + struct NyLPC_TUInt32ArrayPtr bin; + char BC[1024]; + int ist_len; - NyLPC_TUInt16 l,bl; - NyLPC_TUInt32 obuf[1024]; - NyLPC_cMiMicBcCompiler_initialize(&inst); - sprintf(BC,"AA0102AB0100000001AE0203AF0200000003AI0304AJ0300000004AM07BA0505BE0607CA0304CB0300000005CE0304CF0300000005CI0304CJ0300000005ZA.E",&ap,&ap); - bc.ptr=(char* )BC; - bc.len=strlen(BC); - bin.ptr=obuf; - bin.len=100; - ist_len=0; + NyLPC_TUInt16 l,bl; + NyLPC_TUInt32 obuf[1024]; + NyLPC_cMiMicBcCompiler_initialize(&inst); + sprintf(BC,"AA0102AB0100000001AE0203AF0200000003AI0304AJ0300000004AM07BA0505BE0607CA0304CB0300000005CE0304CF0300000005CI0304CJ0300000005ZA.E",&ap,&ap); + bc.ptr=(char* )BC; + bc.len=strlen(BC); + bin.ptr=obuf; + bin.len=100; + ist_len=0; - for(;;){ + for(;;){ - switch(NyLPC_cMiMicBcCompiler_compileFragment(&inst,&bc,&bin,&bl,&l)) - { - case NyLPC_TcMiMicTxtCompiler_RET_OK: - //命令確定。 - NyLPC_TCharArrayPtr_seek(&bc,l); - ist_len+=bl; - break; - case NyLPC_TcMiMicTxtCompiler_RET_OK_END: - //命令終端 - NyLPC_cMiMicVM_initialize(&vm,&eh); - if(!NyLPC_cMiMicVM_run(&vm,obuf,ist_len)){ - printf("エンダァ"); - } - printf("OK"); - break; - case NyLPC_TcMiMicTxtCompiler_RET_CONTINUE: - //蓄積中。 - NyLPC_TCharArrayPtr_seek(&bc,l); - break; - case NyLPC_TcMiMicTxtCompiler_RET_NG: - printf("エラー"); - return; - default: - break; - } - } + switch(NyLPC_cMiMicBcCompiler_compileFragment(&inst,&bc,&bin,&bl,&l)) + { + case NyLPC_TcMiMicTxtCompiler_RET_OK: + //命令確定。 + NyLPC_TCharArrayPtr_seek(&bc,l); + ist_len+=bl; + break; + case NyLPC_TcMiMicTxtCompiler_RET_OK_END: + //命令終端 + NyLPC_cMiMicVM_initialize(&vm,&eh); + if(!NyLPC_cMiMicVM_run(&vm,obuf,ist_len)){ + printf("エンダァ"); + } + printf("OK"); + break; + case NyLPC_TcMiMicTxtCompiler_RET_CONTINUE: + //蓄積中。 + NyLPC_TCharArrayPtr_seek(&bc,l); + break; + case NyLPC_TcMiMicTxtCompiler_RET_NG: + printf("エラー"); + return; + default: + break; + } + } } #endif diff --git a/lib/src/mimicvm/NyLPC_cMiMicVM.h b/lib/src/mimicvm/NyLPC_cMiMicVM.h index 69444e3..8884330 100644 --- a/lib/src/mimicvm/NyLPC_cMiMicVM.h +++ b/lib/src/mimicvm/NyLPC_cMiMicVM.h @@ -19,8 +19,8 @@ * along with this program. If not, see . * * For further information please contact. - * http://nyatla.jp/ - * or + * http://nyatla.jp/ + * or * *********************************************************************************/ #ifndef NYLPC_CMIMICVM_H_ @@ -67,17 +67,17 @@ typedef NyLPC_TUInt32 (*NyLPC_TcMiMicVM_nativeCall)(struct NyLPC_TcMiMicVM_TEven typedef void (*NyLPC_TcMiMicVM_sleep)(struct NyLPC_TcMiMicVM_TEvent* i_evh,NyLPC_TUInt32 i_sleep_in_msec); struct NyLPC_TcMiMicVM_TEvent { - NyLPC_TcMiMicVM_putStream put_stream; - NyLPC_TcMiMicVM_getStream get_stream; - NyLPC_TcMiMicVM_sleep sleep; - NyLPC_TcMiMicVM_nativeCall native_call; + NyLPC_TcMiMicVM_putStream put_stream; + NyLPC_TcMiMicVM_getStream get_stream; + NyLPC_TcMiMicVM_sleep sleep; + NyLPC_TcMiMicVM_nativeCall native_call; }; struct NyLPC_TcMiMicVM { - struct NyLPC_TcMiMicVM_TEvent* _event_handler; - NyLPC_TUInt32 wm[NyLPC_TcMiMicVM_NUMBER_OF_WM]; + struct NyLPC_TcMiMicVM_TEvent* _event_handler; + NyLPC_TUInt32 wm[NyLPC_TcMiMicVM_NUMBER_OF_WM]; }; void NyLPC_cMiMicVM_initialize(NyLPC_TcMiMicVM_t* i_inst,struct NyLPC_TcMiMicVM_TEvent* i_handler); #define NyLPC_cMiMicVM_finalize(i); diff --git a/lib/src/mimicvm/NyLPC_cMiMicVM_protected.h b/lib/src/mimicvm/NyLPC_cMiMicVM_protected.h index 902775e..b515c31 100644 --- a/lib/src/mimicvm/NyLPC_cMiMicVM_protected.h +++ b/lib/src/mimicvm/NyLPC_cMiMicVM_protected.h @@ -19,8 +19,8 @@ * along with this program. If not, see . * * For further information please contact. - * http://nyatla.jp/ - * or + * http://nyatla.jp/ + * or * *********************************************************************************/ #ifndef NYLPC_CMIMICVM_PROTECTED_H_ @@ -49,14 +49,14 @@ extern "C" { #define NyLPC_TcMiMicVM_OP_TYPE_MGET 0x41 #define NyLPC_TcMiMicVM_OP_TYPE_MPUT 0x42 -#define NyLPC_TcMiMicVM_OP_TYPE_SGET 0x51 -#define NyLPC_TcMiMicVM_OP_TYPE_SPUT 0x52 +#define NyLPC_TcMiMicVM_OP_TYPE_SGET 0x51 +#define NyLPC_TcMiMicVM_OP_TYPE_SPUT 0x52 -#define NyLPC_TcMiMicVM_OP_TYPE_NOP 0x61 -#define NyLPC_TcMiMicVM_OP_TYPE_EXIT 0x62 -#define NyLPC_TcMiMicVM_OP_TYPE_CALL 0x63 +#define NyLPC_TcMiMicVM_OP_TYPE_NOP 0x61 +#define NyLPC_TcMiMicVM_OP_TYPE_EXIT 0x62 +#define NyLPC_TcMiMicVM_OP_TYPE_CALL 0x63 -#define NyLPC_TcMiMicVM_OP_TYPE_LD 0x71 +#define NyLPC_TcMiMicVM_OP_TYPE_LD 0x71 #define NyLPC_TcMiMicVM_CP_TYPE_END 0x01 @@ -94,66 +94,66 @@ extern "C" { * */ union NyLPC_TcMiMicVM_TInstruction{ - /** - * オペコード情報のみを格納します - */ - struct{ - NyLPC_TcMiMicVM_OP_TYPE opc; - NyLPC_TUInt8 oprtype; - }op; - //32ビットコード - - /** - * WM[8],WM[8]オペランドの命令を格納します。 - */ - struct{ - NyLPC_TcMiMicVM_OP_TYPE opc; - NyLPC_TUInt8 oprtype; - NyLPC_TUInt8 wm1; - NyLPC_TUInt8 wm2; - }wmwm_32; - struct{ - NyLPC_TcMiMicVM_OP_TYPE opc; - NyLPC_TUInt8 oprtype; - NyLPC_TUInt8 wm; - NyLPC_TUInt8 h8; - }wmh08_32; - struct{ - NyLPC_TcMiMicVM_OP_TYPE opc; - NyLPC_TUInt8 oprtype; - NyLPC_TUInt8 wm; - }wm_32; - struct{ - NyLPC_TcMiMicVM_OP_TYPE opc; - NyLPC_TUInt8 oprtype; - NyLPC_TUInt8 h8; - }h8_32; - struct{ - NyLPC_TcMiMicVM_OP_TYPE opc; - NyLPC_TUInt8 oprtype; - NyLPC_TUInt8 h16; - }h16_32; - //64bit命令 - struct{ - NyLPC_TcMiMicVM_OP_TYPE opc; - NyLPC_TUInt8 oprtype; - NyLPC_TUInt8 wm; - NyLPC_TUInt8 _padding; - NyLPC_TUInt16 h16; - }wmh16_64; - struct{ - NyLPC_TcMiMicVM_OP_TYPE opc; - NyLPC_TUInt8 oprtype; - NyLPC_TUInt8 wm; - NyLPC_TUInt8 _padding; - NyLPC_TUInt32 h32; - }wmh32_64; - struct{ - NyLPC_TcMiMicVM_OP_TYPE opc; - NyLPC_TUInt8 oprtype; - NyLPC_TUInt8 _padding[2]; - NyLPC_TUInt32 h32; - }h32_64; + /** + * オペコード情報のみを格納します + */ + struct{ + NyLPC_TcMiMicVM_OP_TYPE opc; + NyLPC_TUInt8 oprtype; + }op; + //32ビットコード + + /** + * WM[8],WM[8]オペランドの命令を格納します。 + */ + struct{ + NyLPC_TcMiMicVM_OP_TYPE opc; + NyLPC_TUInt8 oprtype; + NyLPC_TUInt8 wm1; + NyLPC_TUInt8 wm2; + }wmwm_32; + struct{ + NyLPC_TcMiMicVM_OP_TYPE opc; + NyLPC_TUInt8 oprtype; + NyLPC_TUInt8 wm; + NyLPC_TUInt8 h8; + }wmh08_32; + struct{ + NyLPC_TcMiMicVM_OP_TYPE opc; + NyLPC_TUInt8 oprtype; + NyLPC_TUInt8 wm; + }wm_32; + struct{ + NyLPC_TcMiMicVM_OP_TYPE opc; + NyLPC_TUInt8 oprtype; + NyLPC_TUInt8 h8; + }h8_32; + struct{ + NyLPC_TcMiMicVM_OP_TYPE opc; + NyLPC_TUInt8 oprtype; + NyLPC_TUInt8 h16; + }h16_32; + //64bit命令 + struct{ + NyLPC_TcMiMicVM_OP_TYPE opc; + NyLPC_TUInt8 oprtype; + NyLPC_TUInt8 wm; + NyLPC_TUInt8 _padding; + NyLPC_TUInt16 h16; + }wmh16_64; + struct{ + NyLPC_TcMiMicVM_OP_TYPE opc; + NyLPC_TUInt8 oprtype; + NyLPC_TUInt8 wm; + NyLPC_TUInt8 _padding; + NyLPC_TUInt32 h32; + }wmh32_64; + struct{ + NyLPC_TcMiMicVM_OP_TYPE opc; + NyLPC_TUInt8 oprtype; + NyLPC_TUInt8 _padding[2]; + NyLPC_TUInt32 h32; + }h32_64; }; #ifdef __cplusplus diff --git a/lib/src/mimicvm/NyLPC_mimicvm_utils.c b/lib/src/mimicvm/NyLPC_mimicvm_utils.c index 50ce080..5e1bdf6 100644 --- a/lib/src/mimicvm/NyLPC_mimicvm_utils.c +++ b/lib/src/mimicvm/NyLPC_mimicvm_utils.c @@ -12,35 +12,35 @@ */ NyLPC_TBool NyLPC_mimicvm_txt2UInt(const NyLPC_TChar* i_txt,NyLPC_TUInt8 i_num,void* out) { - NyLPC_TUInt32 ret=0; - NyLPC_TChar c; - int i; + NyLPC_TUInt32 ret=0; + NyLPC_TChar c; + int i; - for(i=0;i=c && c>='a'){ - c=c-(NyLPC_TUInt8)'a'+10; - }else if('9'>=c && c>='0'){ - c-=(NyLPC_TUInt8)'0'; - }else{ - return NyLPC_TBool_FALSE; - } - ret=(ret<<4)|c; - } - //2,4,8だけ。 - switch(i_num){ - case 2: - *((NyLPC_TUInt8*)out)=(NyLPC_TUInt8)ret; - break; - case 4: - *((NyLPC_TUInt16*)out)=(NyLPC_TUInt16)ret; - break; - case 8: - *((NyLPC_TUInt32*)out)=(NyLPC_TUInt32)ret; - break; - default: - return NyLPC_TBool_FALSE; - } - return NyLPC_TBool_TRUE; + for(i=0;i=c && c>='a'){ + c=c-(NyLPC_TUInt8)'a'+10; + }else if('9'>=c && c>='0'){ + c-=(NyLPC_TUInt8)'0'; + }else{ + return NyLPC_TBool_FALSE; + } + ret=(ret<<4)|c; + } + //2,4,8だけ。 + switch(i_num){ + case 2: + *((NyLPC_TUInt8*)out)=(NyLPC_TUInt8)ret; + break; + case 4: + *((NyLPC_TUInt16*)out)=(NyLPC_TUInt16)ret; + break; + case 8: + *((NyLPC_TUInt32*)out)=(NyLPC_TUInt32)ret; + break; + default: + return NyLPC_TBool_FALSE; + } + return NyLPC_TBool_TRUE; } diff --git a/lib/src/net/NyLPC_cNet.c b/lib/src/net/NyLPC_cNet.c deleted file mode 100644 index 186e6c1..0000000 --- a/lib/src/net/NyLPC_cNet.c +++ /dev/null @@ -1,91 +0,0 @@ -/********************************************************************************* - * PROJECT: MiMic - * -------------------------------------------------------------------------------- - * - * This file is part of MiMic - * Copyright (C)2011 Ryo Iizuka - * - * MiMic is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published - * by the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program. If not, see . - * - * For further information please contact. - * http://nyatla.jp/ - * or - * - *********************************************************************************/ -#include "NyLPC_cNet.h" -#include "dhcp/NyLPC_cDhcpClient.h" -#include "../uip/NyLPC_cUipService_protected.h" - - - -void NyLPC_cNet_initialize(NyLPC_TcNet_t* i_inst) -{ - //uipサービス初期化。いろいろ利用可能に。 - if(!NyLPC_TcUipService_isInitService()){ - NyLPC_cUipService_initialize(); - } -} - -void NyLPC_cNet_start(NyLPC_TcNet_t* i_inst,const NyLPC_TcNetConfig_t* i_ref_config) -{ - NyLPC_cUipService_start(&(i_ref_config->super)); - return; -} - -void NyLPC_cNet_stop(NyLPC_TcNet_t* i_inst) -{ - NyLPC_cUipService_stop(); - return; -} - -/** - * NyLPC_TcIPv4Config_tをDHCPで更新します。 - * この関数をコールする時は、サービスは停止中でなければなりません。 - * @param i_cfg - * 更新するi_cfg構造体。 - * emac,default_mssは設定済である必要があります。他のフィールド値は不定で構いません。 - * 更新されるフィールドは、ip,netmast,default_rootの3つです。 - * @return - * 更新に成功した場合TRUE - */ -NyLPC_TBool NyLPC_cNet_requestAddrDhcp(NyLPC_TcNet_t* i_net,NyLPC_TcIPv4Config_t* i_cfg,NyLPC_TInt16 i_repeat) -{ - NyLPC_TBool ret; - NyLPC_TcDhcpClient_t sock; - //netを開始 - NyLPC_cDhcpClient_initialize(&sock); - ret=NyLPC_cDhcpClient_requestAddr(&sock,i_cfg,i_repeat); - NyLPC_cDhcpClient_finalize(&sock); - return ret; -} -/** - * NyLPC_TcIPv4Config_tをAPIPAで更新します。 - * この関数をコールする時は、サービスは停止中でなければなりません。 - * @param i_cfg - * 更新するi_cfg構造体。 - * emac,default_mssは設定済である必要があります。他のフィールド値は不定で構いません。 - * 更新されるフィールドは、ip,netmast,default_rootの3つです。 - * @return - * 更新に成功した場合TRUE - */ -NyLPC_TBool NyLPC_cNet_requestAddrApipa(NyLPC_TcNet_t* i_net,NyLPC_TcIPv4Config_t* i_cfg,NyLPC_TInt16 i_repeat) -{ - NyLPC_TBool ret; - NyLPC_TcApipa_t sock; - //netを開始 - NyLPC_cApipa_initialize(&sock); - ret=NyLPC_cApipa_requestAddr(&sock,i_cfg,i_repeat); - NyLPC_cApipa_finalize(&sock); - return ret; -} diff --git a/lib/src/net/NyLPC_cNet.h b/lib/src/net/NyLPC_cNet.h deleted file mode 100644 index 8fed451..0000000 --- a/lib/src/net/NyLPC_cNet.h +++ /dev/null @@ -1,85 +0,0 @@ -/********************************************************************************* - * PROJECT: MiMic - * -------------------------------------------------------------------------------- - * - * This file is part of MiMic - * Copyright (C)2011 Ryo Iizuka - * - * MiMic is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published - * by the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program. If not, see . - * - * For further information please contact. - * http://nyatla.jp/ - * or - * - *********************************************************************************/ -#ifndef NYLPC_CNET_H_ -#define NYLPC_CNET_H_ - -#include "NyLPC_stdlib.h" -#include "NyLPC_uipService.h" -#include "NyLPC_cNetConfig.h" -#ifdef __cplusplus -extern "C" { -#endif /* __cplusplus */ -/** - * クラス型を定義します。 - */ -typedef struct NyLPC_TcNet NyLPC_TcNet_t; - - - - -struct NyLPC_TcNet -{ - int dummy; -}; - -/** - * ネットワークを初期化する。 - */ -void NyLPC_cNet_initialize(NyLPC_TcNet_t* i_inst); - -#define NyLPC_cNet_finalize(inst) -/** - * ネットワークサービスを開始します。 - * サービスは停止中でなければなりません。 - * 関数は、ネットワークアダプタの値を元にNyLPC_cMiMicEnv_PlatformName変数の値を更新します。 - * @param i_ref_config - * Networkコンフィギュレーション変数。このオブジェクトはcNetをstopするまで維持すること。 - */ -void NyLPC_cNet_start(NyLPC_TcNet_t* i_inst,const NyLPC_TcNetConfig_t* i_ref_config); -/** - * ネットワークスタックを停止します。 - * サービスは開始中でなければなりません。 - * start関数で開始済である必要があります。 - * この関数をコールする前に、全てのTCPソケットを閉じ、非同期なソケット操作を停止してください。 - */ -void NyLPC_cNet_stop(NyLPC_TcNet_t* i_inst); - -/** - * NyLPC_TcIPv4Config_tをDHCPで更新します。 - * この関数をコールする時は、サービスは停止中でなければなりません。 - * @param i_cfg - * 更新するi_cfg構造体。 - * emac,default_mssは設定済である必要があります。他のフィールド値は不定で構いません。 - * 更新されるフィールドは、ip,netmast,default_rootの3つです。 - * @return - * 更新に成功した場合TRUE - */ -NyLPC_TBool NyLPC_cNet_requestAddrDhcp(NyLPC_TcNet_t* i_net,NyLPC_TcIPv4Config_t* i_cfg,NyLPC_TInt16 i_repeat); - -#ifdef __cplusplus -} -#endif /* __cplusplus */ -#endif /* NYLPC_CNET_H_ */ diff --git a/lib/src/net/NyLPC_cNetConfig.c b/lib/src/net/NyLPC_cNetConfig.c index 1400e20..653b8e0 100644 --- a/lib/src/net/NyLPC_cNetConfig.c +++ b/lib/src/net/NyLPC_cNetConfig.c @@ -19,8 +19,8 @@ * along with this program. If not, see . * * For further information please contact. - * http://nyatla.jp/ - * or + * http://nyatla.jp/ + * or * *********************************************************************************/ #include "NyLPC_cNetConfig.h" @@ -32,12 +32,12 @@ static void update(NyLPC_TcNetConfig_t* i_inst,const struct NyLPC_TMiMicConfigul void NyLPC_cNetConfig_initialize(NyLPC_TcNetConfig_t* i_inst,NyLPC_TBool i_is_factory_default) { - if(i_is_factory_default) - { - update(i_inst,NyLPC_cMiMicConfiglation_loadFactoryDefault()); - }else{ - update(i_inst,NyLPC_cMiMicConfiglation_loadFromFlash()); - } + if(i_is_factory_default) + { + update(i_inst,NyLPC_cMiMicConfiglation_loadFactoryDefault()); + }else{ + update(i_inst,NyLPC_cMiMicConfiglation_loadFromFlash()); + } } @@ -48,38 +48,38 @@ void NyLPC_cNetConfig_initialize(NyLPC_TcNetConfig_t* i_inst,NyLPC_TBool i_is_fa /* NyLPC_TBool NyLPC_cNetConfig_saveToOnchipFlash(NyLPC_TcNetConfig_t* i_inst) { - NyLPC_TUInt8* pdata; - NyLPC_TcOnchipFlashWriter_t s; - struct TNetConfigMemMap tmp; - NyLPC_cOnchipFlashWriter_initialize(&s); - //書込みデータを作成 - tmp.fast_boot=0xffffffff; - tmp.version =i_inst->version; - tmp.interface_type=NyLPC_cNetConfig_INTERFACE_TYPE_ETHERNET; - pdata=i_inst->interface_setting.ethernet.eth_mac.addr; - tmp.mac_00_01_02_03=(((NyLPC_TUInt32)pdata[0])<<24)|(((NyLPC_TUInt32)pdata[1])<<16)|(((NyLPC_TUInt32)pdata[2])<<8)|(((NyLPC_TUInt32)pdata[3])<<0); - tmp.mac_04_05_xx_xx=(((NyLPC_TUInt32)pdata[4])<<24)|(((NyLPC_TUInt32)pdata[5])<<16); - tmp.ipv4_addr_net=i_inst->interface_setting.ethernet.ip_addr.v; - tmp.ipv4_mask_net=i_inst->interface_setting.ethernet.netmask.v; - tmp.ipv4_drut_net=i_inst->interface_setting.ethernet.dr_addr.v; + NyLPC_TUInt8* pdata; + NyLPC_TcOnchipFlashWriter_t s; + struct TNetConfigMemMap tmp; + NyLPC_cOnchipFlashWriter_initialize(&s); + //書込みデータを作成 + tmp.fast_boot=0xffffffff; + tmp.version =i_inst->version; + tmp.interface_type=NyLPC_cNetConfig_INTERFACE_TYPE_ETHERNET; + pdata=i_inst->interface_setting.ethernet.eth_mac.addr; + tmp.mac_00_01_02_03=(((NyLPC_TUInt32)pdata[0])<<24)|(((NyLPC_TUInt32)pdata[1])<<16)|(((NyLPC_TUInt32)pdata[2])<<8)|(((NyLPC_TUInt32)pdata[3])<<0); + tmp.mac_04_05_xx_xx=(((NyLPC_TUInt32)pdata[4])<<24)|(((NyLPC_TUInt32)pdata[5])<<16); + tmp.ipv4_addr_net=i_inst->interface_setting.ethernet.ip_addr.v; + tmp.ipv4_mask_net=i_inst->interface_setting.ethernet.netmask.v; + tmp.ipv4_drut_net=i_inst->interface_setting.ethernet.dr_addr.v; - //イレース - if(!NyLPC_cOnchipFlashWriter_elase(&s,MIMIC_CONFIGLATION_FLASH_SECTOR,MIMIC_CONFIGLATION_FLASH_SECTOR)){ - NyLPC_OnErrorGoto(Error); - } - //コンフィギュレーションを書き込む。 - if(!NyLPC_cOnchipFlashWriter_writeSector(&s,MIMIC_CONFIGLATION_FLASH_SECTOR,0x00000000,&tmp,sizeof(struct TNetConfigMemMap))){ - NyLPC_OnErrorGoto(Error); - } - //ユーザコンフィギュレーションをONにする。 - if(setUserConfigulation()){ - NyLPC_OnErrorGoto(Error); - } - NyLPC_cOnchipFlashWriter_finalize(&s); - return NyLPC_TBool_TRUE; + //イレース + if(!NyLPC_cOnchipFlashWriter_elase(&s,MIMIC_CONFIGLATION_FLASH_SECTOR,MIMIC_CONFIGLATION_FLASH_SECTOR)){ + NyLPC_OnErrorGoto(Error); + } + //コンフィギュレーションを書き込む。 + if(!NyLPC_cOnchipFlashWriter_writeSector(&s,MIMIC_CONFIGLATION_FLASH_SECTOR,0x00000000,&tmp,sizeof(struct TNetConfigMemMap))){ + NyLPC_OnErrorGoto(Error); + } + //ユーザコンフィギュレーションをONにする。 + if(setUserConfigulation()){ + NyLPC_OnErrorGoto(Error); + } + NyLPC_cOnchipFlashWriter_finalize(&s); + return NyLPC_TBool_TRUE; Error: - NyLPC_cOnchipFlashWriter_finalize(&s); - return NyLPC_TBool_FALSE; + NyLPC_cOnchipFlashWriter_finalize(&s); + return NyLPC_TBool_FALSE; } */ @@ -92,26 +92,26 @@ Error: #define ETHERNET_FRAME_LEN 1400 static void update(NyLPC_TcNetConfig_t* i_inst,const struct NyLPC_TMiMicConfigulation* pdata) { - struct NyLPC_TEthAddr ea; - struct NyLPC_TIPv4Addr ip,mask,drt; - //値の読み出し - ea.addr[0]=(NyLPC_TUInt8)((pdata->mac_00_01_02_03>>24)&0xff); - ea.addr[1]=(NyLPC_TUInt8)((pdata->mac_00_01_02_03>>16)&0xff); - ea.addr[2]=(NyLPC_TUInt8)((pdata->mac_00_01_02_03>> 8)&0xff); - ea.addr[3]=(NyLPC_TUInt8)((pdata->mac_00_01_02_03>> 0)&0xff); - ea.addr[4]=(NyLPC_TUInt8)((pdata->mac_04_05_xx_xx>>24)&0xff); - ea.addr[5]=(NyLPC_TUInt8)((pdata->mac_04_05_xx_xx>>16)&0xff); - ip.v=NyLPC_htonl(pdata->ipv4_addr_net); - mask.v=NyLPC_htonl(pdata->ipv4_mask_net); - drt.v=NyLPC_htonl(pdata->ipv4_drut_net); - strcpy(i_inst->hostname,pdata->hostname); - NyLPC_cIPv4Config_initialzeForEthernet(&i_inst->super,&ea,ETHERNET_FRAME_LEN); - NyLPC_cIPv4Config_setDefaultRoute(&i_inst->super,&drt); - NyLPC_cIPv4Config_setIp(&i_inst->super,&ip,&mask); - i_inst->services.flags=pdata->srv_flags; - i_inst->services.http_port=pdata->http_port; - i_inst->tcp_mode=pdata->ipv4_flags; - return; + struct NyLPC_TEthAddr ea; + struct NyLPC_TIPv4Addr ip,mask,drt; + //値の読み出し + ea.addr[0]=(NyLPC_TUInt8)((pdata->mac_00_01_02_03>>24)&0xff); + ea.addr[1]=(NyLPC_TUInt8)((pdata->mac_00_01_02_03>>16)&0xff); + ea.addr[2]=(NyLPC_TUInt8)((pdata->mac_00_01_02_03>> 8)&0xff); + ea.addr[3]=(NyLPC_TUInt8)((pdata->mac_00_01_02_03>> 0)&0xff); + ea.addr[4]=(NyLPC_TUInt8)((pdata->mac_04_05_xx_xx>>24)&0xff); + ea.addr[5]=(NyLPC_TUInt8)((pdata->mac_04_05_xx_xx>>16)&0xff); + ip.v=NyLPC_htonl(pdata->ipv4_addr_net); + mask.v=NyLPC_htonl(pdata->ipv4_mask_net); + drt.v=NyLPC_htonl(pdata->ipv4_drut_net); + strcpy(i_inst->hostname,pdata->hostname); + NyLPC_cIPv4Config_initialzeForEthernet(&i_inst->super,&ea,ETHERNET_FRAME_LEN); + NyLPC_cIPv4Config_setDefaultRoute(&i_inst->super,&drt); + NyLPC_cIPv4Config_setIp(&i_inst->super,&ip,&mask); + i_inst->services.flags=pdata->srv_flags; + i_inst->services.http_port=pdata->http_port; + i_inst->tcp_mode=pdata->ipv4_flags; + return; } diff --git a/lib/src/net/NyLPC_cNetConfig.h b/lib/src/net/NyLPC_cNetConfig.h index 732b26c..e865239 100644 --- a/lib/src/net/NyLPC_cNetConfig.h +++ b/lib/src/net/NyLPC_cNetConfig.h @@ -19,15 +19,15 @@ * along with this program. If not, see . * * For further information please contact. - * http://nyatla.jp/ - * or + * http://nyatla.jp/ + * or * *********************************************************************************/ #ifndef NYLPC_CNETCONFIG_H_ #define NYLPC_CNETCONFIG_H_ #include "NyLPC_stdlib.h" -#include "NyLPC_uipService.h" +#include "NyLPC_netif.h" #ifdef __cplusplus extern "C" { #endif /* __cplusplus */ @@ -48,32 +48,32 @@ typedef struct NyLPC_TcNetConfig NyLPC_TcNetConfig_t; */ struct NyLPC_TcNetConfig { - NyLPC_TcIPv4Config_t super; - /** - * ホスト名 - */ - NyLPC_TChar hostname[NyLPC_TcNetConfig_HOSTNAME_LEN]; - /** - * tcp初期設定モードのフラグ値 -  * NyLPC_TcNetConfig_IPV4_FLAG_Xの組み合わせ - * bit 01:IP初期設定のモード. 0:Manual指定,1:DHCP指定,2:AutoIP指定,3:APIPA指定 - */ - NyLPC_TUInt32 tcp_mode; - struct{ - /** - * サービスのフラグセット。 - * NyLPC_TcNetConfig_SERVICE_FLAG_xの組み合わせ - */ - NyLPC_TUInt32 flags; - NyLPC_TUInt16 http_port; - NyLPC_TUInt16 padding; - }services; - - /** インタフェイス層の設定*/ + NyLPC_TcIPv4Config_t super; + /** + * ホスト名 + */ + NyLPC_TChar hostname[NyLPC_TcNetConfig_HOSTNAME_LEN]; + /** + * tcp初期設定モードのフラグ値 +  * NyLPC_TcNetConfig_IPV4_FLAG_Xの組み合わせ + * bit 01:IP初期設定のモード. 0:Manual指定,1:DHCP指定,2:AutoIP指定,3:APIPA指定 + */ + NyLPC_TUInt32 tcp_mode; + struct{ + /** + * サービスのフラグセット。 + * NyLPC_TcNetConfig_SERVICE_FLAG_xの組み合わせ + */ + NyLPC_TUInt32 flags; + NyLPC_TUInt16 http_port; + NyLPC_TUInt16 padding; + }services; + + /** インタフェイス層の設定 */ }; -#define NyLPC_TcNetConfig_IPV4_FLAG_MODE_MASK 0x00000003 +#define NyLPC_TcNetConfig_IPV4_FLAG_MODE_MASK 0x00000003 #define NyLPC_TcNetConfig_IPV4_FLAG_MODE_MANUAL 0x00000000 -#define NyLPC_TcNetConfig_IPV4_FLAG_MODE_DHCP 0x00000001 +#define NyLPC_TcNetConfig_IPV4_FLAG_MODE_DHCP 0x00000001 #define NyLPC_TcNetConfig_IPV4_FLAG_MODE_AUTOIP 0x00000002 #define NyLPC_TcNetConfig_IPV4_FLAG_MODE_APIPA (NyLPC_TcNetConfig_IPV4_FLAG_MODE_DHCP|NyLPC_TcNetConfig_IPV4_FLAG_MODE_AUTOIP) /*-------------------------------------------------- diff --git a/lib/src/net/apipa/NyLPC_cApipa.c b/lib/src/net/apipa/NyLPC_cApipa.c deleted file mode 100644 index b17f32d..0000000 --- a/lib/src/net/apipa/NyLPC_cApipa.c +++ /dev/null @@ -1,95 +0,0 @@ -#include "NyLPC_cApipa.h" -#include "NyLPC_uipService.h" -#include "../uip/NyLPC_cUipService_protected.h" -#include -#include - - - - - -/** - * ARPテーブルに指定IPが現れるまで待ちます。 - */ -static NyLPC_TBool waitForArpResponse(const struct NyLPC_TIPv4Addr* i_ip,NyLPC_TUInt32 i_wait_in_ms) -{ - NyLPC_TcStopwatch_t sw; - NyLPC_cStopwatch_initialize(&sw); - NyLPC_cStopwatch_startExpire(&sw,i_wait_in_ms); - while(!NyLPC_cStopwatch_isExpired(&sw)){ - NyLPC_cThread_yield(); - if(NyLPC_cUipService_hasArpInfo(i_ip)){ - return NyLPC_TBool_TRUE; - } - } - NyLPC_cStopwatch_finalize(&sw); - return NyLPC_TBool_FALSE; -} -static void makeIP(NyLPC_TcApipa_t* i_inst,struct NyLPC_TIPv4Addr* i_ip) -{ -// NyLPC_TIPv4Addr_set(i_ip,192,168,128,206);//for conflict test! - NyLPC_TIPv4Addr_set(i_ip,169,254,(i_inst->_seed>>8)&0xff,i_inst->_seed & 0xff); -} -static void updateSeed(NyLPC_TcApipa_t* i_inst) -{ - do{ - i_inst->_seed=(391*i_inst->_seed+392); - }while(((i_inst->_seed & 0xff)==0) || ((i_inst->_seed & 0xff00)==0)); -} - - - -void NyLPC_cApipa_initialize(NyLPC_TcApipa_t* i_inst) -{ - i_inst->_seed=0; -} - - -/** - * この関数はuipを操作します。 - * cNetは停止中である必要があります。 - */ -NyLPC_TBool NyLPC_cApipa_requestAddr(NyLPC_TcApipa_t* i_inst,NyLPC_TcIPv4Config_t* i_cfg,NyLPC_TInt16 i_repeat) -{ - int i; - NyLPC_TcIPv4Config_t cfg; - struct NyLPC_TIPv4Addr caip; - - //ゼロコンフィギュレーション用のIPを設定 - NyLPC_cIPv4Config_initialzeCopy(&cfg,i_cfg); - //seedを更新 - for(i=0;i<6;i++) - { - i_inst->_seed+=i_cfg->eth_mac.addr[i]; - } - NyLPC_cIPv4Config_setDefaultRoute(&cfg,&NyLPC_TIPv4Addr_ZERO); - for(i=i_repeat-1;i>=0;i--){ - NyLPC_cIPv4Config_setIp(&cfg,&NyLPC_TIPv4Addr_ZERO,&NyLPC_TIPv4Addr_ZERO); - updateSeed(i_inst); - makeIP(i_inst,&caip); - //startInterface - NyLPC_cUipService_start(&cfg); - NyLPC_cUipService_sendArpRequest(&caip); - //テーブル更新待ち - if(waitForArpResponse(&caip,512+(i_inst->_seed % 256))){ - NyLPC_cUipService_stop(); - continue; - } - NyLPC_cUipService_stop(); - //IPのコンフリクトテスト - NyLPC_cIPv4Config_setIp(&cfg,&caip,&NyLPC_TIPv4Addr_APIPA_MASK); - NyLPC_cUipService_start(&cfg); - //!ARP送信 - NyLPC_cUipService_sendArpRequest(&caip); - if(waitForArpResponse(&caip,512+(256-(i_inst->_seed % 256)))){ - //応答があったらエラー - NyLPC_cUipService_stop(); - continue; - } - //OK - NyLPC_cUipService_stop(); - NyLPC_cIPv4Config_setIp(i_cfg,&cfg.ip_addr,&cfg.netmask); - return NyLPC_TBool_TRUE; - } - return NyLPC_TBool_FALSE; -} diff --git a/lib/src/net/dhcp/NyLPC_cDhcpClient.c b/lib/src/net/dhcp/NyLPC_cDhcpClient.c deleted file mode 100644 index caa9a2a..0000000 --- a/lib/src/net/dhcp/NyLPC_cDhcpClient.c +++ /dev/null @@ -1,351 +0,0 @@ -/********************************************************************************* - * PROJECT: MiMic - * -------------------------------------------------------------------------------- - * - * This file is part of MiMic - * Copyright (C)2011-2013 Ryo Iizuka - * - * MiMic is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published - * by the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program. If not, see . - * - * For further information please contact. - * http://nyatla.jp/ - * or - * - *********************************************************************************/ -#include "NyLPC_cDhcpClient.h" -#include -#include - -struct NyLPC_TDhcpHeader -{ - NyLPC_TUInt8 op; - NyLPC_TUInt8 htype; - NyLPC_TUInt8 hlen; - NyLPC_TUInt8 hops; - NyLPC_TUInt32 xid; - NyLPC_TUInt16 secs; - NyLPC_TUInt16 flags; - NyLPC_TUInt32 ciaddr; - NyLPC_TUInt32 yiaddr; - NyLPC_TUInt32 siaddr; - NyLPC_TUInt32 giaddr; - struct{ - struct NyLPC_TEthAddr emac; - NyLPC_TChar padding[10]; - }chaddr; - NyLPC_TChar sname[64]; - NyLPC_TChar file[128]; -}PACK_STRUCT_END; - -#define NyLPC_TDhcpHeader_BOOTREQUEST 1 -#define NyLPC_TDhcpHeader_BOOTREPLY 2 - -#define DHCP_OPT_ID_ROUTER 3 -#define DHCP_OPT_ID_SERVER_ID 54 -#define DHCP_OPT_ID_NETMASK 1 -#define DHCP_OPT_ID_MESSAGETYPE 53 - - - -/** - * DHCPパケットから32bit値を読み出す。 - * @return - * ネットワークオーダー - */ -static NyLPC_TBool getUInt32Option(const NyLPC_TUInt8* i_buf,NyLPC_TUInt16 len,NyLPC_TUInt8 i_id,NyLPC_TUInt32* o_v) -{ - const NyLPC_TUInt8* p=i_buf+sizeof(struct NyLPC_TDhcpHeader)+4; - while(*p!=0x00 && p<(i_buf+len-5)){ - if(*p==i_id){ - if(*(p+1)==4){ - *o_v=*((NyLPC_TUInt32*)(p+2)); - return NyLPC_TBool_TRUE; - } - }else{ - p+=(*(p+1))+2; - } - } - return NyLPC_TBool_FALSE; -} -static NyLPC_TBool getUInt8Option(const NyLPC_TUInt8* i_buf,NyLPC_TUInt16 len,NyLPC_TUInt8 i_id,NyLPC_TUInt8* o_v) -{ - const NyLPC_TUInt8* p=i_buf+sizeof(struct NyLPC_TDhcpHeader)+4; - while(*p!=0x00 && p<(i_buf+len-5)){ - if(*p==i_id){ - if(*(p+1)==1){ - *o_v=*(p+2); - return NyLPC_TBool_TRUE; - } - }else{ - p+=(*(p+1))+2; - } - } - return NyLPC_TBool_FALSE; -} -static NyLPC_TBool NyLPC_TDhcpHeader_parseDHCPOFFER(const NyLPC_TUInt8* i_buf,NyLPC_TUInt16 i_len,NyLPC_TUInt32 i_xid,NyLPC_TcDhcpClient_t* i_inst) -{ - struct NyLPC_TDhcpHeader* p=(struct NyLPC_TDhcpHeader*)i_buf; - //XIDのチェック - if(p->xid!=NyLPC_HTONL(i_xid)){ - return NyLPC_TBool_FALSE; - } - //OFFERのclient IPアドレスをresultへ保存情報の保存 - i_inst->_result->ip_addr.v=p->yiaddr; - //SERVER IDを保存 - if(!getUInt32Option(i_buf,i_len,DHCP_OPT_ID_SERVER_ID,&i_inst->_offerserver.v)){ - return NyLPC_TBool_FALSE; - } - return NyLPC_TBool_TRUE; -} - -static NyLPC_TBool NyLPC_TDhcpHeader_parseDHCPACK(const NyLPC_TUInt8* i_buf,NyLPC_TUInt16 i_len,NyLPC_TUInt32 i_xid,NyLPC_TcIPv4Config_t* result) -{ - struct NyLPC_TDhcpHeader* p=(struct NyLPC_TDhcpHeader*)i_buf; - //XIDのチェック - if(p->xid!=NyLPC_HTONL(i_xid)){ - return NyLPC_TBool_FALSE; - } - if(!getUInt32Option(i_buf,i_len,DHCP_OPT_ID_ROUTER,&result->dr_addr.v)){ - result->dr_addr=NyLPC_TIPv4Addr_ZERO; - } - if(!getUInt32Option(i_buf,i_len,DHCP_OPT_ID_NETMASK,&result->netmask.v)){ - result->netmask=NyLPC_TIPv4Addr_ZERO; - } - result->ip_addr.v=p->yiaddr; - return NyLPC_TBool_TRUE; -} - -static void NyLPC_TDhcpHeader_setDHCPDISCOVER(char* i_buf,NyLPC_TUInt32 i_xid,const struct NyLPC_TEthAddr* emac,NyLPC_TUInt16* o_len) -{ - struct NyLPC_TDhcpHeader* p=(struct NyLPC_TDhcpHeader*)i_buf; - memset(i_buf,0,sizeof(struct NyLPC_TDhcpHeader)); - p->op=NyLPC_TDhcpHeader_BOOTREQUEST; - p->htype=1; - p->hlen=6; - p->xid=NyLPC_HTONL(i_xid); - p->chaddr.emac=*emac; - p->flags=NyLPC_HTONS(0x8000); - memcpy(i_buf+sizeof(struct NyLPC_TDhcpHeader), - "\x63\x82\x53\x63" //4 - "\x35\x01\x01" //3 MESSAGE TYPE - "\x37\x03\x01\x03\x06" //5 REQUEST LIST(1,3,6) - "\x3d\x07\x01\x00\x00\x00\x00\x00\x00" //9 CLIENT INDIFIRE - "\xff",4+3+5+9+1); - //emacの上書き - memcpy((i_buf+sizeof(struct NyLPC_TDhcpHeader)+4+3+5+3),emac->addr,6); - //送信するパケットの長さ - *o_len=sizeof(struct NyLPC_TDhcpHeader)+4+3+5+9+1; - return; -} -static void NyLPC_TDhcpHeader_setDHCPREQUEST(char* i_buf,NyLPC_TUInt32 i_xid,const struct NyLPC_TIPv4Addr* i_sid,const struct NyLPC_TIPv4Addr* i_reqid,const struct NyLPC_TEthAddr* emac,NyLPC_TUInt16* o_len) -{ - struct NyLPC_TDhcpHeader* p=(struct NyLPC_TDhcpHeader*)i_buf; - memset(i_buf,0,sizeof(struct NyLPC_TDhcpHeader)); - p->op=NyLPC_TDhcpHeader_BOOTREQUEST; - p->htype=1; - p->hlen=6; - p->xid=NyLPC_HTONL(i_xid); - p->chaddr.emac=*emac; - p->flags=NyLPC_HTONS(0x8000); - memcpy(i_buf+sizeof(struct NyLPC_TDhcpHeader), - "\x63\x82\x53\x63" //4 - "\x35\x01\x03" //3 MESSAGE TYPE - "\x37\x03\x01\x03\x06" //5 REQUEST LIST(1,3,6) - "\x3d\x07\x01\x00\x00\x00\x00\x00\x00" //9 CLIENT INDIFIRE - "\x36\x04\x00\x00\x00\x00" // 6 SERVER ID - "\x32\x04\x00\x00\x00\x00" // 6 Reqested IP - "\xff",4+3+5+9+6+6+1); - //emacの上書き - memcpy((i_buf+sizeof(struct NyLPC_TDhcpHeader)+4+3+5+3),emac->addr,6); - //sidの上書き - memcpy((i_buf+sizeof(struct NyLPC_TDhcpHeader)+4+3+5+9+2),i_sid,4); - //reqidの上書き - memcpy((i_buf+sizeof(struct NyLPC_TDhcpHeader)+4+3+5+9+6+2),i_reqid,4); - //送信するパケットの長さ - *o_len=sizeof(struct NyLPC_TDhcpHeader)+4+3+5+9+6+6+1; - return; -} - - - -#define TcDhcpSock_ST_WAIT_OFFER 1 -#define TcDhcpSock_ST_WAIT_OFFER_OK 2 -#define TcDhcpSock_ST_WAIT_ACK 3 -#define TcDhcpSock_ST_WAIT_ACK_OK 4 -#define TcDhcpSock_ST_DONE_NG 3 -#define TcDhcpSock_ST_DONE_OK 4 - - - - -#define DHCP_OPT_ID_MESSAGETYPE_ACK 5 -#define DHCP_OPT_ID_MESSAGETYPE_OFFER 2 - -static NyLPC_TBool onPacket(NyLPC_TcUdpSocket_t* i_inst,const void* i_buf,const struct NyLPC_TIPv4RxInfo* i_info); - -/** - * DHCPソケットを作成します。 - */ -NyLPC_TBool NyLPC_cDhcpClient_initialize(NyLPC_TcDhcpClient_t* i_inst) -{ - if(!NyLPC_cUdpSocket_initialize(&(i_inst->super),68,NULL,0)){ - return NyLPC_TBool_FALSE; - } - NyLPC_cUdpSocket_setBroadcast(&(i_inst->super)); - NyLPC_cUdpSocket_setOnRxHandler(&(i_inst->super),onPacket); - return NyLPC_TBool_TRUE; -} -void NyLPC_cDhcpClient_finalize(NyLPC_TcDhcpClient_t* i_inst) -{ - NyLPC_cUdpSocket_finalize(&(i_inst->super)); -} -#define TIMEOUT_SOCKAPI_MS 1000 -#define TIMEOUT_RECVMSG_MS 3000 - -/** - * ネットワークを更新します。 - * emac/default_mssを設定したネットワークが必要です。 - */ -static NyLPC_TBool NyLPC_cDhcpClient_dhcpRequest(NyLPC_TcDhcpClient_t* i_sock,NyLPC_TcIPv4Config_t* i_result) -{ - char* buf; - NyLPC_TcStopwatch_t sw; - NyLPC_TUInt16 s; - NyLPC_TInt16 hint=sizeof(struct NyLPC_TDhcpHeader)+128; - i_sock->txid+=(*(NyLPC_TUInt16*)(&(i_result->eth_mac.addr[2])))+(*(NyLPC_TUInt16*)(&(i_result->eth_mac.addr[4]))); - i_sock->_result=i_result; - buf=NyLPC_cUdpSocket_allocSendBuf(&i_sock->super,hint,&s,TIMEOUT_SOCKAPI_MS); - if(buf==NULL || stxid,&i_sock->_result->eth_mac,&s); - i_sock->_status=TcDhcpSock_ST_WAIT_OFFER; - if(!NyLPC_cUdpSocket_psend(&i_sock->super,&NyLPC_TIPv4Addr_BROADCAST,67,buf,s)){ - NyLPC_cUdpSocket_releaseSendBuf(&i_sock->super,buf); - return NyLPC_TBool_FALSE; - } - NyLPC_cStopwatch_initialize(&sw); - NyLPC_cStopwatch_startExpire(&sw,TIMEOUT_RECVMSG_MS); - while(i_sock->_status==TcDhcpSock_ST_WAIT_OFFER){ - if(NyLPC_cStopwatch_isExpired(&sw)){ - return NyLPC_TBool_FALSE; - } - } - //レスポンスのチェック - if(i_sock->_status!=TcDhcpSock_ST_WAIT_OFFER_OK) - { - return NyLPC_TBool_FALSE; - } - buf=NyLPC_cUdpSocket_allocSendBuf(&i_sock->super,hint,&s,TIMEOUT_SOCKAPI_MS); - if(buf==NULL || stxid,&(i_sock->_offerserver),&(i_sock->_result->ip_addr),&i_sock->_result->eth_mac,&s); - i_sock->_status=TcDhcpSock_ST_WAIT_ACK; - if(!NyLPC_cUdpSocket_psend(&i_sock->super,&NyLPC_TIPv4Addr_BROADCAST,67,buf,s)){ - NyLPC_cUdpSocket_releaseSendBuf(&i_sock->super,buf); - return NyLPC_TBool_FALSE; - } - NyLPC_cStopwatch_startExpire(&sw,TIMEOUT_RECVMSG_MS); - while(i_sock->_status==TcDhcpSock_ST_WAIT_ACK){ - if(NyLPC_cStopwatch_isExpired(&sw)){ - return NyLPC_TBool_FALSE; - } - } - //レスポンスのチェック - if(i_sock->_status!=TcDhcpSock_ST_WAIT_ACK_OK) - { - return NyLPC_TBool_FALSE; - } - return NyLPC_TBool_TRUE; -} - -/** - * NyLPC_TcIPv4Config_tをDHCPで更新します。 - * この関数をコールする時は、サービスは停止中でなければなりません。 - * @param i_cfg - * 更新するi_cfg構造体。 - * emac,default_mssは設定済である必要があります。他のフィールド値は不定で構いません。 - * 更新されるフィールドは、ip,netmast,default_rootの3つです。 - * @return - * 更新に成功した場合TRUE - */ -NyLPC_TBool NyLPC_cDhcpClient_requestAddr(NyLPC_TcDhcpClient_t* i_inst,NyLPC_TcIPv4Config_t* i_cfg,NyLPC_TInt16 i_repeat) -{ - NyLPC_TInt16 i; - NyLPC_TBool ret=NyLPC_TBool_FALSE; - NyLPC_TcIPv4Config_t c2; - //工場出荷時設定でリセットしてIPを0に - NyLPC_cIPv4Config_initialzeCopy(&c2,i_cfg); - NyLPC_cIPv4Config_setIp(&c2,&NyLPC_TIPv4Addr_ZERO,&NyLPC_TIPv4Addr_ZERO); - NyLPC_cIPv4Config_setDefaultRoute(&c2,&NyLPC_TIPv4Addr_ZERO); - //netを開始 - NyLPC_cUipService_start(&c2); - for(i=i_repeat-1;i>=0;i--){ - ret=NyLPC_cDhcpClient_dhcpRequest(i_inst,i_cfg); - if(ret){ - break; - } - } - NyLPC_cUipService_stop(); - NyLPC_cIPv4Config_finalize(&c2); - return ret; -} - - - -static NyLPC_TBool onPacket(NyLPC_TcUdpSocket_t* i_inst,const void* i_buf,const struct NyLPC_TIPv4RxInfo* i_info) -{ - NyLPC_TUInt8 mt;//message type - NyLPC_TcDhcpClient_t* inst=(NyLPC_TcDhcpClient_t*)i_inst; - struct NyLPC_TDhcpHeader* dnsh=(struct NyLPC_TDhcpHeader*)i_buf; - if(i_info->size_status) - { - case TcDhcpSock_ST_WAIT_ACK: - if(dnsh->op!=NyLPC_TDhcpHeader_BOOTREPLY){ - return NyLPC_TBool_FALSE; - } - if(!getUInt8Option(i_buf,i_info->size,DHCP_OPT_ID_MESSAGETYPE,&mt)){ - return NyLPC_TBool_FALSE; - } - if(mt!=DHCP_OPT_ID_MESSAGETYPE_ACK){ - return NyLPC_TBool_FALSE; - } - if(!NyLPC_TDhcpHeader_parseDHCPACK(i_buf,i_info->size,inst->txid,inst->_result)){ - return NyLPC_TBool_FALSE; - } - inst->_status=TcDhcpSock_ST_WAIT_ACK_OK; - break; - case TcDhcpSock_ST_WAIT_OFFER: - if(dnsh->op!=NyLPC_TDhcpHeader_BOOTREPLY){ - return NyLPC_TBool_FALSE; - } - if(!getUInt8Option(i_buf,i_info->size,DHCP_OPT_ID_MESSAGETYPE,&mt)){ - return NyLPC_TBool_FALSE; - } - if(mt!=DHCP_OPT_ID_MESSAGETYPE_OFFER){ - return NyLPC_TBool_FALSE; - } - if(!NyLPC_TDhcpHeader_parseDHCPOFFER(i_buf,i_info->size,inst->txid,inst)){ - return NyLPC_TBool_FALSE; - } - inst->_status=TcDhcpSock_ST_WAIT_OFFER_OK; - break; - } - return NyLPC_TBool_FALSE; - -} diff --git a/lib/src/net/httpcl/NyLPC_cHttpClient.c b/lib/src/net/httpcl/NyLPC_cHttpClient.c index 718f27c..a5fb271 100644 --- a/lib/src/net/httpcl/NyLPC_cHttpClient.c +++ b/lib/src/net/httpcl/NyLPC_cHttpClient.c @@ -15,15 +15,19 @@ typedef NyLPC_TUInt8 NyLPC_TcHttpClient_ST; -void NyLPC_cHttpClient_initialize(NyLPC_TcHttpClient_t* i_inst,void* i_rx_buf,NyLPC_TUInt16 i_rx_size) +NyLPC_TBool NyLPC_cHttpClient_initialize(NyLPC_TcHttpClient_t* i_inst,void* i_rx_buf,NyLPC_TUInt16 i_rx_size) { - NyLPC_cTcpSocket_initialize(&i_inst->_sock,i_rx_buf,i_rx_size); + i_inst->_sock=NyLPC_cNet_createTcpSocketEx(NyLPC_TSocketType_TCP_NORMAL); + if(i_inst->_sock==NULL){ + return NyLPC_TBool_FALSE; + } i_inst->_state=NyLPC_TcHttpClient_ST_CLOSED; + return NyLPC_TBool_TRUE; } void NyLPC_cHttpClient_finalize(NyLPC_TcHttpClient_t* i_inst) { NyLPC_cHttpClient_close(i_inst); - NyLPC_cTcpSocket_finalize(&i_inst->_sock); + NyLPC_iTcpSocket_finalize(i_inst->_sock); } void NyLPC_cHttpClient_close(NyLPC_TcHttpClient_t* i_inst) @@ -44,7 +48,7 @@ void NyLPC_cHttpClient_close(NyLPC_TcHttpClient_t* i_inst) case NyLPC_TcHttpClient_ST_CLOSED: return; } - NyLPC_cTcpSocket_close(&i_inst->_sock,1000); + NyLPC_iTcpSocket_close(i_inst->_sock,1000); i_inst->_state=NyLPC_TcHttpClient_ST_CLOSED; } @@ -61,11 +65,11 @@ NyLPC_TBool NyLPC_cHttpClient_connect(NyLPC_TcHttpClient_t* i_inst,const struct //ステータスをclosedへ遷移 NyLPC_cHttpClient_close(i_inst); //接続 - if(!NyLPC_cTcpSocket_connect(&i_inst->_sock,i_addr,i_port,3000)){ + if(!NyLPC_iTcpSocket_connect(i_inst->_sock,i_addr,i_port,3000)){ return NyLPC_TBool_FALSE; } //streamの生成 - if(!NyLPC_cHttpStream_initialize(&i_inst->_stream,&(i_inst->_sock))){ + if(!NyLPC_cHttpStream_initialize(&i_inst->_stream,i_inst->_sock)){ NyLPC_OnErrorGoto(ERROR); } //ステータス遷移 @@ -108,8 +112,8 @@ NyLPC_TBool NyLPC_cHttpClient_sendMethod( if(!NyLPC_cHttpHeaderWriter_writeRequestHeader( &i_inst->pw.head_writer, i_method, - NyLPC_cTcpSocket_getPeerAddr(&(i_inst->_sock)), - NyLPC_cTcpSocket_getPeerPort(&(i_inst->_sock)),i_path)){ + NyLPC_iTcpSocket_getPeerAddr((i_inst->_sock)), + NyLPC_iTcpSocket_getPeerPort((i_inst->_sock)),i_path)){ NyLPC_OnErrorGoto(Error_1); } //MimeType @@ -260,3 +264,4 @@ NyLPC_TBool NyLPC_cHttpClient_read(NyLPC_TcHttpClient_t* i_inst,void* i_buf,NyLP } return NyLPC_TBool_TRUE; } + diff --git a/lib/src/net/httpcl/NyLPC_cHttpClient.h b/lib/src/net/httpcl/NyLPC_cHttpClient.h index d93ae84..0b3b03c 100644 --- a/lib/src/net/httpcl/NyLPC_cHttpClient.h +++ b/lib/src/net/httpcl/NyLPC_cHttpClient.h @@ -11,7 +11,7 @@ #include "NyLPC_stdlib.h" #include "NyLPC_net.h" #include "NyLPC_http.h" -#include "NyLPC_uipService.h" +#include "NyLPC_netif.h" #ifdef __cplusplus extern "C" { @@ -24,7 +24,7 @@ struct NyLPC_TcHttpClient { NyLPC_TUInt8 _state; NyLPC_TUInt8 _padding1; - NyLPC_TcTcpSocket_t _sock; + NyLPC_TiTcpSocket_t* _sock; NyLPC_TcHttpStream_t _stream; union{ NyLPC_TcHttpHeaderWriter_t head_writer; @@ -35,7 +35,7 @@ struct NyLPC_TcHttpClient }; -void NyLPC_cHttpClient_initialize(NyLPC_TcHttpClient_t* i_inst,void* i_rx_buf,NyLPC_TUInt16 i_rx_size); +NyLPC_TBool NyLPC_cHttpClient_initialize(NyLPC_TcHttpClient_t* i_inst,void* i_rx_buf,NyLPC_TUInt16 i_rx_size); void NyLPC_cHttpClient_finalize(NyLPC_TcHttpClient_t* i_inst); @@ -59,18 +59,11 @@ NyLPC_TBool NyLPC_cHttpClient_connect(NyLPC_TcHttpClient_t* i_inst,const struct /** - * メソッドを送信し、レスポンスコードの受信を準備する。 + * POSTリクエストを送信する。 * ステータスはIDLEである必要がある。 - * Request bodyが有る場合はNyLPC_cHttpClient_writeで送信する。 * @param i_content_length * 送信bodyのサイズ。最大 0xfffffffe * NyLPC_cHttpHeaderWriter_CONTENT_LENGTH_UNLIMITEDの場合はChunked転送になる。 - * @param i_content_length - * 送信データサイズ。不要ならば0 - * @param i_mime_type - * 送信データのMimetype。不要ならばNULL - * @param i_additional_header - * 追加ヘッダ。\r\n区切りの文字列。 * @return * 引き続き処理が可能かを返す。 * TRUE - 成功。ステータスはSEND_REQ_BODYになる。write/getStatusを呼び出せる。 @@ -86,9 +79,8 @@ NyLPC_TBool NyLPC_cHttpClient_sendMethod( /** - * RequestBodyを送信する。 + * POSTリクエストのデータを送信する。 * ステータスはSEND_REQ_BODYである必要がある。 - * NyLPC_cHttpClient_getStatusを呼び出す前に使用できる。 * @return * TRUE - 成功。 * FALSE - 失敗。ステータスはCLOSEDになる。 @@ -98,7 +90,6 @@ NyLPC_TBool NyLPC_cHttpClient_write(NyLPC_TcHttpClient_t* i_inst,const void* i_b /** * 書式文字列としてPOSTリクエストのデータを送信する。 * ステータスはSEND_REQ_BODYである必要がある。 - * NyLPC_cHttpClient_getStatusを呼び出す前に使用できる。 * @param i_fmt * printfライクなフォーマット文字列 * @return @@ -111,7 +102,6 @@ NyLPC_TBool NyLPC_cHttpClient_writeFormatV(NyLPC_TcHttpClient_t* i_inst,const Ny /** * ステータスコードを返す。 * ステータスはSEND_REQ_BODYである必要がある。 - * RequestBodyを送信する場合は、関数をコールする前にNyLPC_cHttpClient_write*を使用する。 * @return * 0 - 失敗。ステータスはCLOSEDになる。 * その他 - ステータスコード。ステータスはRECV_RES_BODYになる。 @@ -140,3 +130,4 @@ NyLPC_TBool NyLPC_cHttpClient_read(NyLPC_TcHttpClient_t* i_inst,void* i_buf,NyLP #endif /* __cplusplus */ #endif /* NYLPC_CHTTPCLIENT_H_ */ + diff --git a/lib/src/net/httpd/NyLPC_cHttpRequestPrefixParser.c b/lib/src/net/httpd/NyLPC_cHttpRequestPrefixParser.c index f0f8c1c..94e8926 100644 --- a/lib/src/net/httpd/NyLPC_cHttpRequestPrefixParser.c +++ b/lib/src/net/httpd/NyLPC_cHttpRequestPrefixParser.c @@ -19,8 +19,8 @@ * along with this program. If not, see . * * For further information please contact. - * http://nyatla.jp/ - * or + * http://nyatla.jp/ + * or * *********************************************************************************/ #include "NyLPC_cHttpRequestPrefixParser.h" @@ -29,18 +29,18 @@ struct THttpHeaderPrefix{ - struct NyLPC_THttpBasicHeader super; - /** URL蓄積用のオブジェクト*/ - NyLPC_TcStr_t surl; - /** コールバック関数の成功/失敗を判定するフラグ*/ - NyLPC_TBool status; + struct NyLPC_THttpBasicHeader super; + /** URL蓄積用のオブジェクト*/ + NyLPC_TcStr_t surl; + /** コールバック関数の成功/失敗を判定するフラグ*/ + NyLPC_TBool status; }; const NyLPC_TChar* NyLPC_cHttpRequestPrefixParser_getUrlPrefix(const NyLPC_TcHttpRequestPrefixParser_t* i_inst) { - return i_inst->_url; + return i_inst->_url; } /** * URLハンドラ。 @@ -48,60 +48,60 @@ const NyLPC_TChar* NyLPC_cHttpRequestPrefixParser_getUrlPrefix(const NyLPC_TcHtt */ static NyLPC_TBool urlHandler(NyLPC_TcHttpBasicHeaderParser_t* i_inst,NyLPC_TChar i_c,struct NyLPC_THttpBasicHeader* o_out) { - struct THttpHeaderPrefix* s=(struct THttpHeaderPrefix*)o_out; + struct THttpHeaderPrefix* s=(struct THttpHeaderPrefix*)o_out; - //終端なら終わらせる。 - if(i_c=='\0'){ - s->status=NyLPC_TBool_TRUE; - return NyLPC_TBool_FALSE; - } - //容量がいっぱい->解析を終わらせる。 - if(NyLPC_cStr_capacity(&(s->surl))<1) - { - s->status=NyLPC_TBool_TRUE; - return NyLPC_TBool_FALSE; - } - NyLPC_cStr_put(&(s->surl),i_c); - return NyLPC_TBool_TRUE; + //終端なら終わらせる。 + if(i_c=='\0'){ + s->status=NyLPC_TBool_TRUE; + return NyLPC_TBool_FALSE; + } + //容量がいっぱい->解析を終わらせる。 + if(NyLPC_cStr_capacity(&(s->surl))<1) + { + s->status=NyLPC_TBool_TRUE; + return NyLPC_TBool_FALSE; + } + NyLPC_cStr_put(&(s->surl),i_c); + return NyLPC_TBool_TRUE; } /** * デフォルトハンドラ */ static const struct NyLPC_TcHttpBasicHeaderParser_Handler _handler= { - NULL, - urlHandler + NULL, + urlHandler }; void NyLPC_cHttpRequestPrefixParser_initialize(NyLPC_TcHttpRequestPrefixParser_t* i_inst) { - return; + return; } NyLPC_TBool NyLPC_cHttpRequestPrefixParser_parse(NyLPC_TcHttpRequestPrefixParser_t* i_inst,NyLPC_TiHttpPtrStream_t* i_stream) { - struct THttpHeaderPrefix hout; - NyLPC_TcHttpBasicHeaderParser_t parser; - NyLPC_cStr_initialize(&hout.surl,i_inst->_url,NyLPC_TcHttpRequestPrefixParser_MAX_URL_LEN); - hout.status=NyLPC_TBool_FALSE; - NyLPC_cHttpBasicHeaderParser_initialize(&parser,&_handler); - NyLPC_cHttpBasicHeaderParser_parseInit(&parser,&hout.super); - NyLPC_cHttpBasicHeaderParser_parseStream(&parser,i_stream,&hout.super);//どの道エラー - NyLPC_cHttpBasicHeaderParser_parseFinish(&parser,&hout.super);//どの道エラー - if(!hout.status){ - NyLPC_OnErrorGoto(Error); - } - //Errorで帰ってくるのでparsefinishは不要 - //NyLPC_cHttpBasicHeaderParser_parseFinish(&parser,&hout.super); - i_inst->method=hout.super.startline.req.method; - NyLPC_cHttpBasicHeaderParser_finalize(&parser); - //この時点では、メソッドとURLの一部がパースされているはず。 - NyLPC_cStr_finalize(&hout.surl); - //フラグをチェックして返す。 - return NyLPC_TBool_TRUE; + struct THttpHeaderPrefix hout; + NyLPC_TcHttpBasicHeaderParser_t parser; + NyLPC_cStr_initialize(&hout.surl,i_inst->_url,NyLPC_TcHttpRequestPrefixParser_MAX_URL_LEN); + hout.status=NyLPC_TBool_FALSE; + NyLPC_cHttpBasicHeaderParser_initialize(&parser,&_handler); + NyLPC_cHttpBasicHeaderParser_parseInit(&parser,&hout.super); + NyLPC_cHttpBasicHeaderParser_parseStream(&parser,i_stream,&hout.super);//どの道エラー + NyLPC_cHttpBasicHeaderParser_parseFinish(&parser,&hout.super);//どの道エラー + if(!hout.status){ + NyLPC_OnErrorGoto(Error); + } + //Errorで帰ってくるのでparsefinishは不要 + //NyLPC_cHttpBasicHeaderParser_parseFinish(&parser,&hout.super); + i_inst->method=hout.super.startline.req.method; + NyLPC_cHttpBasicHeaderParser_finalize(&parser); + //この時点では、メソッドとURLの一部がパースされているはず。 + NyLPC_cStr_finalize(&hout.surl); + //フラグをチェックして返す。 + return NyLPC_TBool_TRUE; Error: - NyLPC_cHttpBasicHeaderParser_finalize(&parser); - NyLPC_cStr_finalize(&hout.surl); - return NyLPC_TBool_FALSE; + NyLPC_cHttpBasicHeaderParser_finalize(&parser); + NyLPC_cStr_finalize(&hout.surl); + return NyLPC_TBool_FALSE; } diff --git a/lib/src/net/httpd/NyLPC_cHttpRequestPrefixParser.h b/lib/src/net/httpd/NyLPC_cHttpRequestPrefixParser.h index 70f90c4..749e4a8 100644 --- a/lib/src/net/httpd/NyLPC_cHttpRequestPrefixParser.h +++ b/lib/src/net/httpd/NyLPC_cHttpRequestPrefixParser.h @@ -19,8 +19,8 @@ * along with this program. If not, see . * * For further information please contact. - * http://nyatla.jp/ - * or + * http://nyatla.jp/ + * or * *********************************************************************************/ #ifndef NYLPC_cHttpRequestPrefixParser_H_ @@ -39,8 +39,8 @@ typedef struct NyLPC_TcHttpRequestPrefixParser NyLPC_TcHttpRequestPrefixParser_t */ struct NyLPC_TcHttpRequestPrefixParser { - NyLPC_THttpMethodType method; - NyLPC_TChar _url[NyLPC_TcHttpRequestPrefixParser_MAX_URL_LEN]; + NyLPC_THttpMethodType method; + NyLPC_TChar _url[NyLPC_TcHttpRequestPrefixParser_MAX_URL_LEN]; }; void NyLPC_cHttpRequestPrefixParser_initialize(NyLPC_TcHttpRequestPrefixParser_t* i_inst); diff --git a/lib/src/net/httpd/NyLPC_cHttpd.c b/lib/src/net/httpd/NyLPC_cHttpd.c index 76c8dad..baae265 100644 --- a/lib/src/net/httpd/NyLPC_cHttpd.c +++ b/lib/src/net/httpd/NyLPC_cHttpd.c @@ -1,47 +1,53 @@ #include "NyLPC_cHttpd_protected.h" - -void NyLPC_cHttpd_initialize(NyLPC_TcHttpd_t* i_inst,NyLPC_TUInt16 i_port_number) +NyLPC_TBool NyLPC_cHttpd_initialize(NyLPC_TcHttpd_t* i_inst,NyLPC_TUInt16 i_port_number) { - int i; - i_inst->_num_of_active_connection=0; - NyLPC_cMutex_initialize(&i_inst->_mutex); - NyLPC_cTcpListener_initialize(&(i_inst->_listener),i_port_number); - for(i=0;i_thread[i]),i_inst,NyLPC_TcThread_PRIORITY_IDLE); - } + int i; + i_inst->_num_of_active_connection=0; + NyLPC_cMutex_initialize(&i_inst->_mutex); + i_inst->_listener=NyLPC_cNet_createTcpListenerEx(i_port_number); + if(i_inst->_listener==NULL){ + return NyLPC_TBool_FALSE; + } + for(i=0;i_thread[i]),i_inst,NyLPC_TcThread_PRIORITY_IDLE)){ + return NyLPC_TBool_FALSE; + } + } + return NyLPC_TBool_TRUE; } void NyLPC_cHttpd_finalize(NyLPC_TcHttpd_t* i_inst) { - NyLPC_cMutex_finalize(&i_inst->_mutex); + NyLPC_iTcpListener_finaize(i_inst->_listener); + NyLPC_cMutex_finalize(&i_inst->_mutex); } void NyLPC_cHttpd_loop(NyLPC_TcHttpd_t* i_inst) { - int i; - for(;;){ - //ターミネイト状態のタスクを検索 - for(i=0;i_thread[i]),&i_inst->_listener); - } - NyLPC_cThread_yield(); - } + int i; + for(;;){ + //ターミネイト状態のタスクを検索 + for(i=0;i_thread[i]),i_inst->_listener); + } + NyLPC_cThread_yield(); + } } NyLPC_TUInt16 NyLPC_cHttpd_incNumOfConnection(NyLPC_TcHttpd_t* i_inst) { - i_inst->_num_of_active_connection++; - NyLPC_Assert(i_inst->_num_of_active_connection<=NyLPC_cHttpd_NUMBER_OF_CONNECTION_THREAD); - return i_inst->_num_of_active_connection; + i_inst->_num_of_active_connection++; + NyLPC_Assert(i_inst->_num_of_active_connection<=NyLPC_cHttpd_NUMBER_OF_CONNECTION_THREAD); + return i_inst->_num_of_active_connection; } NyLPC_TUInt16 NyLPC_cHttpd_decNumOfConnection(NyLPC_TcHttpd_t* i_inst) { - i_inst->_num_of_active_connection--; - NyLPC_Assert(i_inst->_num_of_active_connection>=0); - return i_inst->_num_of_active_connection; + i_inst->_num_of_active_connection--; + NyLPC_Assert(i_inst->_num_of_active_connection>=0); + return i_inst->_num_of_active_connection; } /** @@ -49,12 +55,12 @@ NyLPC_TUInt16 NyLPC_cHttpd_decNumOfConnection(NyLPC_TcHttpd_t* i_inst) */ void NyLPC_cHttpd_lock(NyLPC_TcHttpd_t* i_inst) { - NyLPC_cMutex_lock(&i_inst->_mutex); + NyLPC_cMutex_lock(&i_inst->_mutex); } /** * Httpd全体で唯一のロックを開放する。 */ void NyLPC_cHttpd_unlock(NyLPC_TcHttpd_t* i_inst) { - NyLPC_cMutex_unlock(&i_inst->_mutex); + NyLPC_cMutex_unlock(&i_inst->_mutex); } diff --git a/lib/src/net/httpd/NyLPC_cHttpd.h b/lib/src/net/httpd/NyLPC_cHttpd.h index 8aabf7e..f688cb2 100644 --- a/lib/src/net/httpd/NyLPC_cHttpd.h +++ b/lib/src/net/httpd/NyLPC_cHttpd.h @@ -14,7 +14,7 @@ extern "C" { * HTTPコネクションスレッドの数 */ #ifndef NyLPC_cHttpd_NUMBER_OF_CONNECTION_THREAD -# define NyLPC_cHttpd_NUMBER_OF_CONNECTION_THREAD 3 +# define NyLPC_cHttpd_NUMBER_OF_CONNECTION_THREAD 3 #endif /** @@ -22,7 +22,7 @@ extern "C" { * NyLPC_cHttpd_NUMBER_OF_CONNECTION_THREAD-1以下にしてください。 */ #ifndef NyLPC_cHttpd_MAX_PERSISTENT_CONNECTION -# define NyLPC_cHttpd_MAX_PERSISTENT_CONNECTION (NyLPC_cHttpd_NUMBER_OF_CONNECTION_THREAD-1) +# define NyLPC_cHttpd_MAX_PERSISTENT_CONNECTION (NyLPC_cHttpd_NUMBER_OF_CONNECTION_THREAD-1) #endif @@ -30,8 +30,8 @@ extern "C" { #ifndef DEFINE_NyLPC_TcHttpdConnection_t - typedef struct NyLPC_TcHttpdConnection NyLPC_TcHttpdConnection_t; - #define DEFINE_NyLPC_TcHttpdConnection_t + typedef struct NyLPC_TcHttpdConnection NyLPC_TcHttpdConnection_t; + #define DEFINE_NyLPC_TcHttpdConnection_t #endif @@ -44,24 +44,24 @@ typedef void (*NyLPC_TcHttpd_onRequest)(NyLPC_TcHttpdConnection_t* i_inst); * class definition */ #ifndef DEFINE_NyLPC_TcHttpd_t - typedef struct NyLPC_TcHttpd NyLPC_TcHttpd_t; - #define DEFINE_NyLPC_TcHttpd_t + typedef struct NyLPC_TcHttpd NyLPC_TcHttpd_t; + #define DEFINE_NyLPC_TcHttpd_t #endif struct NyLPC_TcHttpd { - struct{ - NyLPC_TcHttpd_onRequest onRequest; - }function; - NyLPC_TcMutex_t _mutex; - NyLPC_TcTcpListener_t _listener; - NyLPC_TcHttpdThread_t _thread[NyLPC_cHttpd_NUMBER_OF_CONNECTION_THREAD]; - NyLPC_TInt16 _num_of_active_connection; + struct{ + NyLPC_TcHttpd_onRequest onRequest; + }function; + NyLPC_TcMutex_t _mutex; + NyLPC_TiTcpListener_t* _listener; + NyLPC_TcHttpdThread_t _thread[NyLPC_cHttpd_NUMBER_OF_CONNECTION_THREAD]; + NyLPC_TInt16 _num_of_active_connection; }; -void NyLPC_cHttpd_initialize(NyLPC_TcHttpd_t* i_inst,NyLPC_TUInt16 i_port_number); +NyLPC_TBool NyLPC_cHttpd_initialize(NyLPC_TcHttpd_t* i_inst,NyLPC_TUInt16 i_port_number); void NyLPC_cHttpd_finalize(NyLPC_TcHttpd_t* i_inst); void NyLPC_cHttpd_loop(NyLPC_TcHttpd_t* i_inst); void NyLPC_cHttpd_lock(NyLPC_TcHttpd_t* i_inst); diff --git a/lib/src/net/httpd/NyLPC_cHttpdConnection.c b/lib/src/net/httpd/NyLPC_cHttpdConnection.c index f332d2e..7b9a81c 100644 --- a/lib/src/net/httpd/NyLPC_cHttpdConnection.c +++ b/lib/src/net/httpd/NyLPC_cHttpdConnection.c @@ -1,34 +1,39 @@ #include "NyLPC_cHttpdConnection_protected.h" #include "NyLPC_http.h" +#include "NyLPC_netif.h" #include "NyLPC_cHttpdUtils.h" #include "./NyLPC_cHttpd_protected.h" -void NyLPC_cHttpdConnection_initialize(NyLPC_TcHttpdConnection_t* i_inst,NyLPC_TcHttpd_t* i_parent_httpd) +NyLPC_TBool NyLPC_cHttpdConnection_initialize(NyLPC_TcHttpdConnection_t* i_inst,NyLPC_TcHttpd_t* i_parent_httpd) { - NyLPC_cTcpSocket_initialize(&(i_inst->_socket),i_inst->_rxbuf,NyLPC_cHttpdConnection_SIZE_OF_RX_BUF); - NyLPC_cHttpRequestPrefixParser_initialize(&(i_inst->_pparser)); - i_inst->_parent_httpd=i_parent_httpd; - i_inst->_res_status=NyLPC_cHttpdConnection_ResStatus_CLOSED; - i_inst->_req_status=NyLPC_cHttpdConnection_ReqStatus_LISTEN; + i_inst->_socket=NyLPC_cNet_createTcpSocketEx(NyLPC_TSocketType_TCP_HTTP); + if(i_inst->_socket==NULL){ + return NyLPC_TBool_FALSE; + } + NyLPC_cHttpRequestPrefixParser_initialize(&(i_inst->_pparser)); + i_inst->_parent_httpd=i_parent_httpd; + i_inst->_res_status=NyLPC_cHttpdConnection_ResStatus_CLOSED; + i_inst->_req_status=NyLPC_cHttpdConnection_ReqStatus_LISTEN; + return NyLPC_TBool_TRUE; } void NyLPC_cHttpdConnection_finalize(NyLPC_TcHttpdConnection_t* i_inst) { - NyLPC_cHttpdConnection_closeResponse(i_inst); - NyLPC_cHttpdConnection_closeSocket(i_inst); - NyLPC_cHttpRequestPrefixParser_finalize(i_inst); - NyLPC_cTcpSocket_finalize(&(i_inst->_socket)); + NyLPC_cHttpdConnection_closeResponse(i_inst); + NyLPC_cHttpdConnection_closeSocket(i_inst); + NyLPC_cHttpRequestPrefixParser_finalize(i_inst); + NyLPC_iTcpSocket_finalize(i_inst->_socket); } const NyLPC_TChar* NyLPC_cHttpdConnection_getUrlPrefix(const NyLPC_TcHttpdConnection_t* i_inst) { - return NyLPC_cHttpRequestPrefixParser_getUrlPrefix(&i_inst->_pparser); + return NyLPC_cHttpRequestPrefixParser_getUrlPrefix(&i_inst->_pparser); } void NyLPC_cHttpdConnection_setReqStatusParsed(NyLPC_TcHttpdConnection_t* i_inst) { - i_inst->_req_status=NyLPC_cHttpdConnection_ReqStatus_END; + i_inst->_req_status=NyLPC_cHttpdConnection_ReqStatus_END; } #define NyLPC_cHttpHeaderWriter_CONTENT_LENGTH_UNLIMITED 0xFFFFFFFF @@ -36,20 +41,20 @@ void NyLPC_cHttpdConnection_setReqStatusParsed(NyLPC_TcHttpdConnection_t* i_inst NyLPC_TBool NyLPC_cHttpdConnection_send100Continue(NyLPC_TcHttpdConnection_t* i_inst) { - //状態の確認 - if(i_inst->_res_status!=NyLPC_cHttpdConnection_ResStatus_HEAD) - { - NyLPC_OnErrorGoto(Error_Status); - } - //ステータスラインの記述 - if(!NyLPC_iHttpPtrStream_write(&(i_inst->_in_stream.super),"HTTP/1.1 100 Continue\r\n\r\n",25)){ - NyLPC_OnErrorGoto(Error); - } - return NyLPC_TBool_TRUE; + //状態の確認 + if(i_inst->_res_status!=NyLPC_cHttpdConnection_ResStatus_HEAD) + { + NyLPC_OnErrorGoto(Error_Status); + } + //ステータスラインの記述 + if(!NyLPC_iHttpPtrStream_write(&(i_inst->_in_stream.super),"HTTP/1.1 100 Continue\r\n\r\n",25)){ + NyLPC_OnErrorGoto(Error); + } + return NyLPC_TBool_TRUE; Error: Error_Status: - i_inst->_res_status=NyLPC_cHttpdConnection_ResStatus_ERROR; - return NyLPC_TBool_FALSE; + i_inst->_res_status=NyLPC_cHttpdConnection_ResStatus_ERROR; + return NyLPC_TBool_FALSE; } /** @@ -57,58 +62,58 @@ Error_Status: */ NyLPC_TBool NyLPC_cHttpdConnection_sendResponseHeader(NyLPC_TcHttpdConnection_t* i_inst,NyLPC_TUInt16 i_response_code,const NyLPC_TChar* i_content_type,const NyLPC_TChar* i_additional_header) { - return NyLPC_cHttpdConnection_sendResponseHeader2(i_inst,i_response_code,i_content_type,NyLPC_cHttpHeaderWriter_CONTENT_LENGTH_UNLIMITED,i_additional_header); + return NyLPC_cHttpdConnection_sendResponseHeader2(i_inst,i_response_code,i_content_type,NyLPC_cHttpHeaderWriter_CONTENT_LENGTH_UNLIMITED,i_additional_header); } NyLPC_TBool NyLPC_cHttpdConnection_sendResponseHeader2(NyLPC_TcHttpdConnection_t* i_inst,NyLPC_TUInt16 i_response_code,const NyLPC_TChar* i_content_type,NyLPC_TUInt32 i_content_length,const NyLPC_TChar* i_additional_header) { - NyLPC_TcHttpHeaderWriter_t* h=&(i_inst->_head_writer); - //状態の確認 - if(i_inst->_res_status!=NyLPC_cHttpdConnection_ResStatus_HEAD) - { - NyLPC_OnErrorGoto(Error_Status); - } - //ヘッダ送信 - if(!NyLPC_cHttpHeaderWriter_initialize(h,&(i_inst->_in_stream.super),NULL)){ - NyLPC_OnErrorGoto(ERROR_SEND); - } - //Headerの転送モードセット - if(i_content_length==NyLPC_cHttpHeaderWriter_CONTENT_LENGTH_UNLIMITED){ - NyLPC_cHttpHeaderWriter_setChunked(h); - }else{ - NyLPC_cHttpHeaderWriter_setContentLength(h,i_content_length); - } - //continueにセットされていたらcloseをFALSEに - NyLPC_cHttpHeaderWriter_setConnectionClose(h,(i_inst->_connection_message_mode!=NyLPC_TcHttpdConnection_CONNECTION_MODE_CONTINUE)); - - if(!NyLPC_cHttpHeaderWriter_writeResponseHeader(h,i_response_code)){ - NyLPC_OnErrorGoto(ERROR_SEND); - } - if(!NyLPC_cHttpHeaderWriter_writeMessage(h,"Content-type",i_content_type)){ - NyLPC_OnErrorGoto(ERROR_SEND); - } - if(i_additional_header!=NULL){ - if(!NyLPC_cHttpHeaderWriter_writeRawMessage(h,i_additional_header)){ - NyLPC_OnErrorGoto(ERROR_SEND); - } - } - NyLPC_cHttpHeaderWriter_close(h); - NyLPC_cHttpHeaderWriter_finalize(h); - i_inst->_res_status=NyLPC_cHttpdConnection_ResStatus_BODY; - //BodyWriter生成 - NyLPC_cHttpBodyWriter_initialize(&(i_inst->_body_writer),&(i_inst->_in_stream)); - //bodyのchunkedもセット - if(i_content_length==NyLPC_cHttpHeaderWriter_CONTENT_LENGTH_UNLIMITED){ - NyLPC_cHttpBodyWriter_setChunked(&(i_inst->_body_writer)); - }else{ - NyLPC_cHttpBodyWriter_setContentLength(&(i_inst->_body_writer),i_content_length); - } - return NyLPC_TBool_TRUE; + NyLPC_TcHttpHeaderWriter_t* h=&(i_inst->_head_writer); + //状態の確認 + if(i_inst->_res_status!=NyLPC_cHttpdConnection_ResStatus_HEAD) + { + NyLPC_OnErrorGoto(Error_Status); + } + //ヘッダ送信 + if(!NyLPC_cHttpHeaderWriter_initialize(h,&(i_inst->_in_stream.super),NULL)){ + NyLPC_OnErrorGoto(ERROR_SEND); + } + //Headerの転送モードセット + if(i_content_length==NyLPC_cHttpHeaderWriter_CONTENT_LENGTH_UNLIMITED){ + NyLPC_cHttpHeaderWriter_setChunked(h); + }else{ + NyLPC_cHttpHeaderWriter_setContentLength(h,i_content_length); + } + //continueにセットされていたらcloseをFALSEに + NyLPC_cHttpHeaderWriter_setConnectionClose(h,(i_inst->_connection_message_mode!=NyLPC_TcHttpdConnection_CONNECTION_MODE_CONTINUE)); + + if(!NyLPC_cHttpHeaderWriter_writeResponseHeader(h,i_response_code)){ + NyLPC_OnErrorGoto(ERROR_SEND); + } + if(!NyLPC_cHttpHeaderWriter_writeMessage(h,"Content-type",i_content_type)){ + NyLPC_OnErrorGoto(ERROR_SEND); + } + if(i_additional_header!=NULL){ + if(!NyLPC_cHttpHeaderWriter_writeRawMessage(h,i_additional_header)){ + NyLPC_OnErrorGoto(ERROR_SEND); + } + } + NyLPC_cHttpHeaderWriter_close(h); + NyLPC_cHttpHeaderWriter_finalize(h); + i_inst->_res_status=NyLPC_cHttpdConnection_ResStatus_BODY; + //BodyWriter生成 + NyLPC_cHttpBodyWriter_initialize(&(i_inst->_body_writer),&(i_inst->_in_stream)); + //bodyのchunkedもセット + if(i_content_length==NyLPC_cHttpHeaderWriter_CONTENT_LENGTH_UNLIMITED){ + NyLPC_cHttpBodyWriter_setChunked(&(i_inst->_body_writer)); + }else{ + NyLPC_cHttpBodyWriter_setContentLength(&(i_inst->_body_writer),i_content_length); + } + return NyLPC_TBool_TRUE; ERROR_SEND: - NyLPC_cHttpHeaderWriter_finalize(&(i_inst->_head_writer)); + NyLPC_cHttpHeaderWriter_finalize(&(i_inst->_head_writer)); Error_Status: - i_inst->_res_status=NyLPC_cHttpdConnection_ResStatus_ERROR; - return NyLPC_TBool_FALSE; + i_inst->_res_status=NyLPC_cHttpdConnection_ResStatus_ERROR; + return NyLPC_TBool_FALSE; } @@ -118,20 +123,20 @@ Error_Status: */ NyLPC_TBool NyLPC_cHttpdConnection_sendResponseBody(NyLPC_TcHttpdConnection_t* i_inst,const void* i_data,NyLPC_TUInt32 i_size) { - if(i_inst->_res_status!=NyLPC_cHttpdConnection_ResStatus_BODY) - { - NyLPC_OnErrorGoto(Error); - } - //Bodyの書込み - if(!NyLPC_cHttpBodyWriter_write(&(i_inst->_body_writer),i_data,i_size)){ - NyLPC_OnErrorGoto(Error_Send); - } - return NyLPC_TBool_TRUE; + if(i_inst->_res_status!=NyLPC_cHttpdConnection_ResStatus_BODY) + { + NyLPC_OnErrorGoto(Error); + } + //Bodyの書込み + if(!NyLPC_cHttpBodyWriter_write(&(i_inst->_body_writer),i_data,i_size)){ + NyLPC_OnErrorGoto(Error_Send); + } + return NyLPC_TBool_TRUE; Error_Send: - NyLPC_cHttpBodyWriter_finalize(&(i_inst->_in_stream)); + NyLPC_cHttpBodyWriter_finalize(&(i_inst->_in_stream)); Error: - i_inst->_res_status=NyLPC_cHttpdConnection_ResStatus_ERROR; - return NyLPC_TBool_FALSE; + i_inst->_res_status=NyLPC_cHttpdConnection_ResStatus_ERROR; + return NyLPC_TBool_FALSE; } /** * レスポンスBodyを書式出力して送信します。 @@ -139,24 +144,24 @@ Error: */ NyLPC_TBool NyLPC_cHttpdConnection_sendResponseBodyF(NyLPC_TcHttpdConnection_t* i_inst,const char* i_fmt,...) { - va_list a; - if(i_inst->_res_status!=NyLPC_cHttpdConnection_ResStatus_BODY) - { - NyLPC_OnErrorGoto(Error); - } - //Bodyの書込み - va_start(a,i_fmt); - if(!NyLPC_cHttpBodyWriter_formatV(&(i_inst->_body_writer),i_fmt,a)){ - NyLPC_OnErrorGoto(Error_Send); - } - va_end(a); - return NyLPC_TBool_TRUE; + va_list a; + if(i_inst->_res_status!=NyLPC_cHttpdConnection_ResStatus_BODY) + { + NyLPC_OnErrorGoto(Error); + } + //Bodyの書込み + va_start(a,i_fmt); + if(!NyLPC_cHttpBodyWriter_formatV(&(i_inst->_body_writer),i_fmt,a)){ + NyLPC_OnErrorGoto(Error_Send); + } + va_end(a); + return NyLPC_TBool_TRUE; Error_Send: - va_end(a); - NyLPC_cHttpBodyWriter_finalize(&(i_inst->_in_stream)); + va_end(a); + NyLPC_cHttpBodyWriter_finalize(&(i_inst->_in_stream)); Error: - i_inst->_res_status=NyLPC_cHttpdConnection_ResStatus_ERROR; - return NyLPC_TBool_FALSE; + i_inst->_res_status=NyLPC_cHttpdConnection_ResStatus_ERROR; + return NyLPC_TBool_FALSE; } /** @@ -165,47 +170,47 @@ Error: */ static void sendErrorResponse(NyLPC_TcHttpdConnection_t* i_inst,NyLPC_TInt16 i_status) { - NyLPC_TcHttpHeaderWriter_t* h=&(i_inst->_head_writer); - if(NyLPC_cHttpHeaderWriter_initialize(h,&i_inst->_in_stream.super,NULL)){ - //ヘッダを送信 - NyLPC_cHttpHeaderWriter_setConnectionClose(h,NyLPC_TBool_TRUE); - NyLPC_cHttpHeaderWriter_writeResponseHeader(h,i_status); - NyLPC_cHttpHeaderWriter_close(h); - NyLPC_cHttpHeaderWriter_finalize(h); - } + NyLPC_TcHttpHeaderWriter_t* h=&(i_inst->_head_writer); + if(NyLPC_cHttpHeaderWriter_initialize(h,&i_inst->_in_stream.super,NULL)){ + //ヘッダを送信 + NyLPC_cHttpHeaderWriter_setConnectionClose(h,NyLPC_TBool_TRUE); + NyLPC_cHttpHeaderWriter_writeResponseHeader(h,i_status); + NyLPC_cHttpHeaderWriter_close(h); + NyLPC_cHttpHeaderWriter_finalize(h); + } } /** * 関数を実行後、_res_statusはCLOSEDかHEADかERRORに遷移する。 */ NyLPC_TBool NyLPC_cHttpdConnection_closeResponse(NyLPC_TcHttpdConnection_t* i_inst) { - NyLPC_TcHttpBodyWriter_t* b; - switch(i_inst->_res_status){ - case NyLPC_cHttpdConnection_ResStatus_CLOSED: - case NyLPC_cHttpdConnection_ResStatus_ERROR: - //何もせずにコネクションをクローズする。 - return NyLPC_TBool_FALSE; - case NyLPC_cHttpdConnection_ResStatus_HEAD: - //エラー500を送信してクローズする。 - sendErrorResponse(i_inst,500); - i_inst->_res_status=NyLPC_cHttpdConnection_ResStatus_CLOSED; - return NyLPC_TBool_FALSE; - case NyLPC_cHttpdConnection_ResStatus_BODY: - //正常終了。BODYをクローズし、終了する。 - b=&(i_inst->_body_writer); - NyLPC_cHttpBodyWriter_close(b); - NyLPC_cHttpBodyWriter_finalize(&b); - i_inst->_res_status=NyLPC_cHttpdConnection_ResStatus_HEAD; - if(i_inst->_connection_message_mode!=NyLPC_TcHttpdConnection_CONNECTION_MODE_CONTINUE) - { - i_inst->_res_status=NyLPC_cHttpdConnection_ResStatus_CLOSED; - return NyLPC_TBool_FALSE; - } - return NyLPC_TBool_TRUE; - default: - NyLPC_Abort(); - } - return NyLPC_TBool_TRUE; + NyLPC_TcHttpBodyWriter_t* b; + switch(i_inst->_res_status){ + case NyLPC_cHttpdConnection_ResStatus_CLOSED: + case NyLPC_cHttpdConnection_ResStatus_ERROR: + //何もせずにコネクションをクローズする。 + return NyLPC_TBool_FALSE; + case NyLPC_cHttpdConnection_ResStatus_HEAD: + //エラー500を送信してクローズする。 + sendErrorResponse(i_inst,500); + i_inst->_res_status=NyLPC_cHttpdConnection_ResStatus_CLOSED; + return NyLPC_TBool_FALSE; + case NyLPC_cHttpdConnection_ResStatus_BODY: + //正常終了。BODYをクローズし、終了する。 + b=&(i_inst->_body_writer); + NyLPC_cHttpBodyWriter_close(b); + NyLPC_cHttpBodyWriter_finalize(&b); + i_inst->_res_status=NyLPC_cHttpdConnection_ResStatus_HEAD; + if(i_inst->_connection_message_mode!=NyLPC_TcHttpdConnection_CONNECTION_MODE_CONTINUE) + { + i_inst->_res_status=NyLPC_cHttpdConnection_ResStatus_CLOSED; + return NyLPC_TBool_FALSE; + } + return NyLPC_TBool_TRUE; + default: + NyLPC_Abort(); + } + return NyLPC_TBool_TRUE; } /** @@ -213,39 +218,39 @@ NyLPC_TBool NyLPC_cHttpdConnection_closeResponse(NyLPC_TcHttpdConnection_t* i_in */ NyLPC_TBool NyLPC_cHttpdConnection_pushPrefetchInfo(NyLPC_TcHttpdConnection_t* i_inst,NyLPC_TcHttpBasicHeaderParser_t* i_header_parser,struct NyLPC_THttpBasicHeader* o_out) { - const char* method=NyLPC_THttpMethodType_toString(i_inst->_pparser.method); - if(NyLPC_cHttpBasicHeaderParser_parseChar(i_header_parser,method,strlen(method),o_out)<0){ - NyLPC_OnErrorGoto(Error); - } - if(NyLPC_cHttpBasicHeaderParser_parseChar(i_header_parser," ",1,o_out)<0){ - NyLPC_OnErrorGoto(Error); - } - if(NyLPC_cHttpBasicHeaderParser_parseChar(i_header_parser,i_inst->_pparser._url,strlen(i_inst->_pparser._url),o_out)<0){ - NyLPC_OnErrorGoto(Error); - } - return NyLPC_TBool_TRUE; + const char* method=NyLPC_THttpMethodType_toString(i_inst->_pparser.method); + if(NyLPC_cHttpBasicHeaderParser_parseChar(i_header_parser,method,strlen(method),o_out)<0){ + NyLPC_OnErrorGoto(Error); + } + if(NyLPC_cHttpBasicHeaderParser_parseChar(i_header_parser," ",1,o_out)<0){ + NyLPC_OnErrorGoto(Error); + } + if(NyLPC_cHttpBasicHeaderParser_parseChar(i_header_parser,i_inst->_pparser._url,strlen(i_inst->_pparser._url),o_out)<0){ + NyLPC_OnErrorGoto(Error); + } + return NyLPC_TBool_TRUE; Error: - return NyLPC_TBool_FALSE; + return NyLPC_TBool_FALSE; } -#define NyLPC_cHttpdConnection_TIMEOUT_ACCEPT 3000 -#define NyLPC_cHttpdConnection_TIMEOUT_CLOSE 5000 -#define NyLPC_cHttpdConnection_TIMEOUT_LISTEN 5000 +#define NyLPC_cHttpdConnection_TIMEOUT_ACCEPT 3000 +#define NyLPC_cHttpdConnection_TIMEOUT_CLOSE 5000 +#define NyLPC_cHttpdConnection_TIMEOUT_LISTEN 5000 /** * listenerでConnectionのソケットに接続を待ちます。 */ -NyLPC_TBool NyLPC_cHttpdConnection_listenSocket(NyLPC_TcHttpdConnection_t* i_inst,NyLPC_TcTcpListener_t* i_listener) +NyLPC_TBool NyLPC_cHttpdConnection_listenSocket(NyLPC_TcHttpdConnection_t* i_inst,NyLPC_TiTcpListener_t* i_listener) { - NyLPC_Assert(i_inst->_req_status==NyLPC_cHttpdConnection_ReqStatus_LISTEN); - //リスニング - if(!NyLPC_cTcpListener_listen(i_listener,&(i_inst->_socket),NyLPC_cHttpdConnection_TIMEOUT_LISTEN)){ - return NyLPC_TBool_FALSE; - } - //成功したらステータス遷移 - i_inst->_req_status=NyLPC_cHttpdConnection_ReqStatus_ACCEPT; - return NyLPC_TBool_TRUE; + NyLPC_Assert(i_inst->_req_status==NyLPC_cHttpdConnection_ReqStatus_LISTEN); + //リスニング + if(!NyLPC_iTcpListener_listen(i_listener,i_inst->_socket,NyLPC_cHttpdConnection_TIMEOUT_LISTEN)){ + return NyLPC_TBool_FALSE; + } + //成功したらステータス遷移 + i_inst->_req_status=NyLPC_cHttpdConnection_ReqStatus_ACCEPT; + return NyLPC_TBool_TRUE; } /** @@ -253,43 +258,43 @@ NyLPC_TBool NyLPC_cHttpdConnection_listenSocket(NyLPC_TcHttpdConnection_t* i_ins */ NyLPC_TBool NyLPC_cHttpdConnection_acceptSocket(NyLPC_TcHttpdConnection_t* i_inst) { - NyLPC_Assert(i_inst->_req_status==NyLPC_cHttpdConnection_ReqStatus_ACCEPT); - - if(!NyLPC_cTcpSocket_accept(&(i_inst->_socket),NyLPC_cHttpdConnection_TIMEOUT_ACCEPT)){ - NyLPC_OnErrorGoto(Error); - } - //HttpStreamの生成 - if(!NyLPC_cHttpStream_initialize(&i_inst->_in_stream,&(i_inst->_socket))){ - NyLPC_OnErrorGoto(Error_Connected); - } - //初回だけHEADに遷移 - i_inst->_res_status=NyLPC_cHttpdConnection_ResStatus_HEAD; - i_inst->_req_status=NyLPC_cHttpdConnection_ReqStatus_PREFETCH; - i_inst->_connection_message_mode=NyLPC_TcHttpdConnection_CONNECTION_MODE_CLOSE; - return NyLPC_TBool_TRUE; + NyLPC_Assert(i_inst->_req_status==NyLPC_cHttpdConnection_ReqStatus_ACCEPT); + + if(!NyLPC_iTcpSocket_accept(i_inst->_socket,NyLPC_cHttpdConnection_TIMEOUT_ACCEPT)){ + NyLPC_OnErrorGoto(Error); + } + //HttpStreamの生成 + if(!NyLPC_cHttpStream_initialize(&i_inst->_in_stream,i_inst->_socket)){ + NyLPC_OnErrorGoto(Error_Connected); + } + //初回だけHEADに遷移 + i_inst->_res_status=NyLPC_cHttpdConnection_ResStatus_HEAD; + i_inst->_req_status=NyLPC_cHttpdConnection_ReqStatus_PREFETCH; + i_inst->_connection_message_mode=NyLPC_TcHttpdConnection_CONNECTION_MODE_CLOSE; + return NyLPC_TBool_TRUE; Error_Connected: - NyLPC_cTcpSocket_close(&(i_inst->_socket),NyLPC_cHttpdConnection_TIMEOUT_CLOSE); - i_inst->_req_status=NyLPC_cHttpdConnection_ReqStatus_LISTEN; + NyLPC_iTcpSocket_close(i_inst->_socket,NyLPC_cHttpdConnection_TIMEOUT_CLOSE); + i_inst->_req_status=NyLPC_cHttpdConnection_ReqStatus_LISTEN; Error: - return NyLPC_TBool_FALSE; + return NyLPC_TBool_FALSE; } NyLPC_TBool NyLPC_cHttpdConnection_prefetch(NyLPC_TcHttpdConnection_t* i_inst) { - NyLPC_Assert(i_inst->_req_status==NyLPC_cHttpdConnection_ReqStatus_PREFETCH); - - //Prefetchを実行 - if(!NyLPC_cHttpRequestPrefixParser_parse(&i_inst->_pparser,&i_inst->_in_stream.super)){ - //400エラー - sendErrorResponse(i_inst,400); - NyLPC_OnErrorGoto(Error_Prefetch); - } - i_inst->_req_status=NyLPC_cHttpdConnection_ReqStatus_REQPARSE; - return NyLPC_TBool_TRUE; + NyLPC_Assert(i_inst->_req_status==NyLPC_cHttpdConnection_ReqStatus_PREFETCH); + + //Prefetchを実行 + if(!NyLPC_cHttpRequestPrefixParser_parse(&i_inst->_pparser,&i_inst->_in_stream.super)){ + //400エラー + sendErrorResponse(i_inst,400); + NyLPC_OnErrorGoto(Error_Prefetch); + } + i_inst->_req_status=NyLPC_cHttpdConnection_ReqStatus_REQPARSE; + return NyLPC_TBool_TRUE; Error_Prefetch: - NyLPC_cTcpSocket_close(&(i_inst->_socket),NyLPC_cHttpdConnection_TIMEOUT_CLOSE); - i_inst->_req_status=NyLPC_cHttpdConnection_ReqStatus_LISTEN; - return NyLPC_TBool_FALSE; + NyLPC_iTcpSocket_close(i_inst->_socket,NyLPC_cHttpdConnection_TIMEOUT_CLOSE); + i_inst->_req_status=NyLPC_cHttpdConnection_ReqStatus_LISTEN; + return NyLPC_TBool_FALSE; } @@ -300,58 +305,58 @@ Error_Prefetch: NyLPC_TBool NyLPC_cHttpdConnection_prevNextPrefetch(NyLPC_TcHttpdConnection_t* i_inst) { - NyLPC_TcHttpNullRequestHeaderParser_t parser; - switch(i_inst->_req_status) - { - case NyLPC_cHttpdConnection_ReqStatus_REQPARSE: - //リクエストパース待ちなら前段のリクエストを吸収しておく。 - NyLPC_cHttpNullRequestHeaderParser_initialize(&parser); - //プリフェッチしたデータを流す - NyLPC_cHttpNullRequestHeaderParser_parseInit(&parser); - NyLPC_cHttpNullRequestHeaderParser_parseChar(&parser,"GET ",4);//決め打ち - NyLPC_cHttpNullRequestHeaderParser_parseChar(&parser,i_inst->_pparser._url,strlen(i_inst->_pparser._url)); - //後続をストリームから取り込む - if(NyLPC_cHttpNullRequestHeaderParser_parseStream(&parser,&(i_inst->_in_stream.super))){ - if(NyLPC_cHttpNullRequestHeaderParser_parseFinish(&parser)){ - NyLPC_cHttpNullRequestHeaderParser_finalize(&parser); - //OK:403 - sendErrorResponse(i_inst,403); - break;//OK - } - } - - NyLPC_cHttpNullRequestHeaderParser_finalize(&parser); - //NG:400 Bad Request - sendErrorResponse(i_inst,400); - return NyLPC_TBool_FALSE;//吸収失敗 - case NyLPC_cHttpdConnection_ReqStatus_END: - //リクエストがパース済みならprefetchに戻す。 - i_inst->_req_status=NyLPC_cHttpdConnection_ReqStatus_PREFETCH; - default: - NyLPC_Abort(); - } - //吸収成功 - return NyLPC_TBool_TRUE; + NyLPC_TcHttpNullRequestHeaderParser_t parser; + switch(i_inst->_req_status) + { + case NyLPC_cHttpdConnection_ReqStatus_REQPARSE: + //リクエストパース待ちなら前段のリクエストを吸収しておく。 + NyLPC_cHttpNullRequestHeaderParser_initialize(&parser); + //プリフェッチしたデータを流す + NyLPC_cHttpNullRequestHeaderParser_parseInit(&parser); + NyLPC_cHttpNullRequestHeaderParser_parseChar(&parser,"GET ",4);//決め打ち + NyLPC_cHttpNullRequestHeaderParser_parseChar(&parser,i_inst->_pparser._url,strlen(i_inst->_pparser._url)); + //後続をストリームから取り込む + if(NyLPC_cHttpNullRequestHeaderParser_parseStream(&parser,&(i_inst->_in_stream.super))){ + if(NyLPC_cHttpNullRequestHeaderParser_parseFinish(&parser)){ + NyLPC_cHttpNullRequestHeaderParser_finalize(&parser); + //OK:403 + sendErrorResponse(i_inst,403); + break;//OK + } + } + + NyLPC_cHttpNullRequestHeaderParser_finalize(&parser); + //NG:400 Bad Request + sendErrorResponse(i_inst,400); + return NyLPC_TBool_FALSE;//吸収失敗 + case NyLPC_cHttpdConnection_ReqStatus_END: + //リクエストがパース済みならprefetchに戻す。 + i_inst->_req_status=NyLPC_cHttpdConnection_ReqStatus_PREFETCH; + default: + NyLPC_Abort(); + } + //吸収成功 + return NyLPC_TBool_TRUE; } void NyLPC_cHttpdConnection_closeSocket(NyLPC_TcHttpdConnection_t* i_inst) { - switch(i_inst->_req_status) - { - case NyLPC_cHttpdConnection_ReqStatus_LISTEN: - //何も出来ない。 - break; - case NyLPC_cHttpdConnection_ReqStatus_END: - case NyLPC_cHttpdConnection_ReqStatus_REQPARSE: - case NyLPC_cHttpdConnection_ReqStatus_PREFETCH: - NyLPC_cHttpStream_finalize(&i_inst->_in_stream); - case NyLPC_cHttpdConnection_ReqStatus_ACCEPT: - NyLPC_cTcpSocket_close(&(i_inst->_socket),NyLPC_cHttpdConnection_TIMEOUT_CLOSE); - default: - break; - } - i_inst->_req_status=NyLPC_cHttpdConnection_ReqStatus_LISTEN; - i_inst->_res_status=NyLPC_cHttpdConnection_ResStatus_CLOSED; + switch(i_inst->_req_status) + { + case NyLPC_cHttpdConnection_ReqStatus_LISTEN: + //何も出来ない。 + break; + case NyLPC_cHttpdConnection_ReqStatus_END: + case NyLPC_cHttpdConnection_ReqStatus_REQPARSE: + case NyLPC_cHttpdConnection_ReqStatus_PREFETCH: + NyLPC_cHttpStream_finalize(&i_inst->_in_stream); + case NyLPC_cHttpdConnection_ReqStatus_ACCEPT: + NyLPC_iTcpSocket_close(i_inst->_socket,NyLPC_cHttpdConnection_TIMEOUT_CLOSE); + default: + break; + } + i_inst->_req_status=NyLPC_cHttpdConnection_ReqStatus_LISTEN; + i_inst->_res_status=NyLPC_cHttpdConnection_ResStatus_CLOSED; } /** @@ -359,12 +364,12 @@ void NyLPC_cHttpdConnection_closeSocket(NyLPC_TcHttpdConnection_t* i_inst) */ void NyLPC_cHttpdConnection_lock(NyLPC_TcHttpdConnection_t* i_inst) { - NyLPC_cHttpd_lock((NyLPC_TcHttpd_t*)(i_inst->_parent_httpd)); + NyLPC_cHttpd_lock((NyLPC_TcHttpd_t*)(i_inst->_parent_httpd)); } /** * Httpd全体で唯一のロックを開放する。 */ void NyLPC_cHttpdConnection_unlock(NyLPC_TcHttpdConnection_t* i_inst) { - NyLPC_cHttpd_unlock((NyLPC_TcHttpd_t*)(i_inst->_parent_httpd)); + NyLPC_cHttpd_unlock((NyLPC_TcHttpd_t*)(i_inst->_parent_httpd)); } diff --git a/lib/src/net/httpd/NyLPC_cHttpdConnection.h b/lib/src/net/httpd/NyLPC_cHttpdConnection.h index f5a0142..2c7cc41 100644 --- a/lib/src/net/httpd/NyLPC_cHttpdConnection.h +++ b/lib/src/net/httpd/NyLPC_cHttpdConnection.h @@ -15,30 +15,24 @@ extern "C" { #endif /* __cplusplus */ #ifndef DEFINE_NyLPC_TcHttpd_t - typedef struct NyLPC_TcHttpd NyLPC_TcHttpd_t; - #define DEFINE_NyLPC_TcHttpd_t + typedef struct NyLPC_TcHttpd NyLPC_TcHttpd_t; + #define DEFINE_NyLPC_TcHttpd_t #endif -/** HTTPコネクションの受信バッファのサイズ - */ -#ifndef NyLPC_cHttpdConnection_SIZE_OF_RX_BUF -# define NyLPC_cHttpdConnection_SIZE_OF_RX_BUF 512 -#endif - typedef NyLPC_TUInt8 NyLPC_TcHttpdConnection_Status; /** リクエストプレフィクスを読み出した状態*/ -#define NyLPC_cHttpdConnection_ReqStatus_LISTEN 0 +#define NyLPC_cHttpdConnection_ReqStatus_LISTEN 0 /** ACCEPT待ち*/ -#define NyLPC_cHttpdConnection_ReqStatus_ACCEPT 1 +#define NyLPC_cHttpdConnection_ReqStatus_ACCEPT 1 /** コネクションは接続済*/ -#define NyLPC_cHttpdConnection_ReqStatus_PREFETCH 2 +#define NyLPC_cHttpdConnection_ReqStatus_PREFETCH 2 /** リクエストパース待ち*/ -#define NyLPC_cHttpdConnection_ReqStatus_REQPARSE 3 +#define NyLPC_cHttpdConnection_ReqStatus_REQPARSE 3 /** リクエストパース済*/ -#define NyLPC_cHttpdConnection_ReqStatus_END 4 -#define NyLPC_cHttpdConnection_ReqStatus_BODYPARSE 5 +#define NyLPC_cHttpdConnection_ReqStatus_END 4 +#define NyLPC_cHttpdConnection_ReqStatus_BODYPARSE 5 @@ -46,11 +40,11 @@ typedef NyLPC_TUInt8 NyLPC_TcHttpdConnection_Status; /** レスポンスヘッダ送付済*/ #define NyLPC_cHttpdConnection_ResStatus_HEAD 1 /** レスポンスヘッダ送付済*/ -#define NyLPC_cHttpdConnection_ResStatus_BODY 2 +#define NyLPC_cHttpdConnection_ResStatus_BODY 2 /** レスポンスBODY送付済*/ -#define NyLPC_cHttpdConnection_ResStatus_CLOSED 3 +#define NyLPC_cHttpdConnection_ResStatus_CLOSED 3 /** エラーが発生した。*/ -#define NyLPC_cHttpdConnection_ResStatus_ERROR 4 +#define NyLPC_cHttpdConnection_ResStatus_ERROR 4 @@ -59,30 +53,29 @@ typedef NyLPC_TUInt8 NyLPC_TcHttpdConnection_Status; * */ #ifndef DEFINE_NyLPC_TcHttpdConnection_t - typedef struct NyLPC_TcHttpdConnection NyLPC_TcHttpdConnection_t; - #define DEFINE_NyLPC_TcHttpdConnection_t + typedef struct NyLPC_TcHttpdConnection NyLPC_TcHttpdConnection_t; + #define DEFINE_NyLPC_TcHttpdConnection_t #endif struct NyLPC_TcHttpdConnection { - NyLPC_TUInt8 _req_status;//リクエストステータス - NyLPC_TUInt8 _res_status;//レスポンスステータス - NyLPC_TUInt8 _connection_message_mode;//COnnection:closeをヘッダに書き込むかのフラグ - NyLPC_TcHttpd_t* _parent_httpd; //NyLPC_cHttpd - NyLPC_TcTcpSocket_t _socket; - NyLPC_TUInt8 _rxbuf[NyLPC_cHttpdConnection_SIZE_OF_RX_BUF]; - NyLPC_TcHttpStream_t _in_stream; - NyLPC_TcHttpRequestPrefixParser_t _pparser; - union{ - NyLPC_TcHttpBodyWriter_t _body_writer; - NyLPC_TcHttpHeaderWriter_t _head_writer; - }; + NyLPC_TUInt8 _req_status;//リクエストステータス + NyLPC_TUInt8 _res_status;//レスポンスステータス + NyLPC_TUInt8 _connection_message_mode;//COnnection:closeをヘッダに書き込むかのフラグ + NyLPC_TcHttpd_t* _parent_httpd; //NyLPC_cHttpd + NyLPC_TiTcpSocket_t* _socket; + NyLPC_TcHttpStream_t _in_stream; + NyLPC_TcHttpRequestPrefixParser_t _pparser; + union{ + NyLPC_TcHttpBodyWriter_t _body_writer; + NyLPC_TcHttpHeaderWriter_t _head_writer; + }; }; /** * @param i_parent_httpd * */ -void NyLPC_cHttpdConnection_initialize(NyLPC_TcHttpdConnection_t* i_inst,NyLPC_TcHttpd_t* i_parent_httpd); +NyLPC_TBool NyLPC_cHttpdConnection_initialize(NyLPC_TcHttpdConnection_t* i_inst,NyLPC_TcHttpd_t* i_parent_httpd); void NyLPC_cHttpdConnection_finalize(NyLPC_TcHttpdConnection_t* i_inst); /** diff --git a/lib/src/net/httpd/NyLPC_cHttpdConnection_protected.h b/lib/src/net/httpd/NyLPC_cHttpdConnection_protected.h index d9a5167..f021605 100644 --- a/lib/src/net/httpd/NyLPC_cHttpdConnection_protected.h +++ b/lib/src/net/httpd/NyLPC_cHttpdConnection_protected.h @@ -7,9 +7,9 @@ #ifndef NYLPC_CHTTPDCONNECTION_PROTECTED_H_ #define NYLPC_CHTTPDCONNECTION_PROTECTED_H_ -#include "NyLPC_uipService.h" -#include "NyLPC_cHttpdConnection.h" +#include "NyLPC_netif.h" #include "NyLPC_stdlib.h" +#include "NyLPC_cHttpdConnection.h" #include "NyLPC_cHttpdUtils.h" #ifdef __cplusplus @@ -51,7 +51,7 @@ NyLPC_TBool NyLPC_cHttpdConnection_closeResponse(NyLPC_TcHttpdConnection_t* i_in /** * ソケットをlistenします。LISTEN状態のソケットに使えます。 */ -NyLPC_TBool NyLPC_cHttpdConnection_listenSocket(NyLPC_TcHttpdConnection_t* i_inst,NyLPC_TcTcpListener_t* i_listener); +NyLPC_TBool NyLPC_cHttpdConnection_listenSocket(NyLPC_TcHttpdConnection_t* i_inst,NyLPC_TiTcpListener_t* i_listener); /** * コネクションのソケットをacceptします。 @@ -94,7 +94,7 @@ void NyLPC_cHttpdConnection_requestParsed(NyLPC_TcHttpdConnection_t* i_inst); * HTTPリクエストのバージョンが1.1かつconnectionがCLOSEで無い場合、CONTINUEを指定することで持続性接続に設定できる。 * 関数は、NyLPC_cHttpdConnection_sendResponseHeaderをコールする前に実行すること。 */ -#define NyLPC_cHttpdConnection_setConnectionMode(i_inst,i_mode) (i_inst)->_connection_message_mode=(i_mode) +#define NyLPC_cHttpdConnection_setConnectionMode(i_inst,i_mode) (i_inst)->_connection_message_mode=(i_mode) NyLPC_TUInt16 NyLPC_cHttpd_incNumOfConnection(NyLPC_TcHttpd_t* i_inst); NyLPC_TUInt16 NyLPC_cHttpd_decNumOfConnection(NyLPC_TcHttpd_t* i_inst); diff --git a/lib/src/net/httpd/NyLPC_cHttpdThread.c b/lib/src/net/httpd/NyLPC_cHttpdThread.c index 78b3da9..f325826 100644 --- a/lib/src/net/httpd/NyLPC_cHttpdThread.c +++ b/lib/src/net/httpd/NyLPC_cHttpdThread.c @@ -6,30 +6,33 @@ static int server(void* p); -void NyLPC_cHttpdThread_initialize(NyLPC_TcHttpdThread_t* i_inst,NyLPC_TcHttpd_t* i_parent,NyLPC_TInt32 i_prio) +NyLPC_TBool NyLPC_cHttpdThread_initialize(NyLPC_TcHttpdThread_t* i_inst,NyLPC_TcHttpd_t* i_parent,NyLPC_TInt32 i_prio) { - NyLPC_cHttpdConnection_initialize(&(i_inst->_connection),i_parent); - NyLPC_cThread_initialize(&(i_inst->_super),NyLPC_cHttpdThread_SIZE_OF_THREAD_STACK,i_prio); + if(!NyLPC_cHttpdConnection_initialize(&(i_inst->_connection),i_parent)){ + return NyLPC_TBool_FALSE; + } + NyLPC_cThread_initialize(&(i_inst->_super),NyLPC_cHttpdThread_SIZE_OF_THREAD_STACK,i_prio); + return NyLPC_TBool_TRUE; } void NyLPC_cHttpdThread_finalize(NyLPC_TcHttpdThread_t* i_inst) { - NyLPC_cThread_finalize(&i_inst->_super); - NyLPC_cHttpdConnection_finalize(&(i_inst->_connection)); + NyLPC_cThread_finalize(&i_inst->_super); + NyLPC_cHttpdConnection_finalize(&(i_inst->_connection)); } -NyLPC_TBool NyLPC_cHttpdThread_start(NyLPC_TcHttpdThread_t* i_inst,NyLPC_TcTcpListener_t* i_listener) +NyLPC_TBool NyLPC_cHttpdThread_start(NyLPC_TcHttpdThread_t* i_inst,NyLPC_TiTcpListener_t* i_listener) { - //停止中? - if(!NyLPC_cThread_isTerminated(&(i_inst->_super))){ - return NyLPC_TBool_FALSE; - } - //リスニング - if(!NyLPC_cHttpdConnection_listenSocket(&(i_inst->_connection),i_listener)){ - return NyLPC_TBool_FALSE; - } - //Accept可能なので開始。 - NyLPC_cThread_start(&(i_inst->_super),server,&i_inst->_connection); - return NyLPC_TBool_TRUE; + //停止中? + if(!NyLPC_cThread_isTerminated(&(i_inst->_super))){ + return NyLPC_TBool_FALSE; + } + //リスニング + if(!NyLPC_cHttpdConnection_listenSocket(&(i_inst->_connection),i_listener)){ + return NyLPC_TBool_FALSE; + } + //Accept可能なので開始。 + NyLPC_cThread_start(&(i_inst->_super),server,&i_inst->_connection); + return NyLPC_TBool_TRUE; } @@ -39,45 +42,45 @@ NyLPC_TBool NyLPC_cHttpdThread_start(NyLPC_TcHttpdThread_t* i_inst,NyLPC_TcTcpLi //Httpのセッション関数 static int server(void* p) { - NyLPC_TcHttpdConnection_t* inst=(NyLPC_TcHttpdConnection_t*)p; - //コネクションをAccept - if(!NyLPC_cHttpdConnection_acceptSocket(inst)){ - NyLPC_OnErrorGoto(Error); - } - //コネクション数の追加 - NyLPC_cHttpd_incNumOfConnection(inst->_parent_httpd); + NyLPC_TcHttpdConnection_t* inst=(NyLPC_TcHttpdConnection_t*)p; + //コネクションをAccept + if(!NyLPC_cHttpdConnection_acceptSocket(inst)){ + NyLPC_OnErrorGoto(Error); + } + //コネクション数の追加 + NyLPC_cHttpd_incNumOfConnection(inst->_parent_httpd); - //サブネットアクセスの確認 - for(;;){ - //リクエストのプレフィクスを取得 - if(!NyLPC_cHttpdConnection_prefetch(inst)){ - //Prefetch出来ないならループ終了。 - break; - } - //持続性接続の初期モードを設定 - if(NyLPC_cHttpd_getNumOfConnection(inst->_parent_httpd)>NyLPC_cHttpd_MAX_PERSISTENT_CONNECTION){ - NyLPC_cHttpdConnection_setConnectionMode(inst,NyLPC_TcHttpdConnection_CONNECTION_MODE_CLOSE); - }else{ - NyLPC_cHttpdConnection_setConnectionMode(inst,NyLPC_TcHttpdConnection_CONNECTION_MODE_CONTINUE); - } - {//handler - (inst->_parent_httpd->function.onRequest)(inst); - } - //HTTP層のクローズ - if(!NyLPC_cHttpdConnection_closeResponse(inst)){ - break; - } - //次のプリフェッチを準備。 - if(!NyLPC_cHttpdConnection_prevNextPrefetch(inst)){ - break; - } - } - NyLPC_cHttpd_decNumOfConnection(inst->_parent_httpd); - NyLPC_cHttpdConnection_closeSocket(inst); - return 0; + //サブネットアクセスの確認 + for(;;){ + //リクエストのプレフィクスを取得 + if(!NyLPC_cHttpdConnection_prefetch(inst)){ + //Prefetch出来ないならループ終了。 + break; + } + //持続性接続の初期モードを設定 + if(NyLPC_cHttpd_getNumOfConnection(inst->_parent_httpd)>NyLPC_cHttpd_MAX_PERSISTENT_CONNECTION){ + NyLPC_cHttpdConnection_setConnectionMode(inst,NyLPC_TcHttpdConnection_CONNECTION_MODE_CLOSE); + }else{ + NyLPC_cHttpdConnection_setConnectionMode(inst,NyLPC_TcHttpdConnection_CONNECTION_MODE_CONTINUE); + } + {//handler + (inst->_parent_httpd->function.onRequest)(inst); + } + //HTTP層のクローズ + if(!NyLPC_cHttpdConnection_closeResponse(inst)){ + break; + } + //次のプリフェッチを準備。 + if(!NyLPC_cHttpdConnection_prevNextPrefetch(inst)){ + break; + } + } + NyLPC_cHttpd_decNumOfConnection(inst->_parent_httpd); + NyLPC_cHttpdConnection_closeSocket(inst); + return 0; Error: - NyLPC_cHttpdConnection_closeSocket(inst); - return -1; + NyLPC_cHttpdConnection_closeSocket(inst); + return -1; } diff --git a/lib/src/net/httpd/NyLPC_cHttpdThread.h b/lib/src/net/httpd/NyLPC_cHttpdThread.h index c36d9a2..cc86202 100644 --- a/lib/src/net/httpd/NyLPC_cHttpdThread.h +++ b/lib/src/net/httpd/NyLPC_cHttpdThread.h @@ -16,13 +16,13 @@ extern "C" { #endif /* __cplusplus */ #ifndef DEFINE_NyLPC_TcHttpd_t - typedef struct NyLPC_TcHttpd NyLPC_TcHttpd_t; - #define DEFINE_NyLPC_TcHttpd_t + typedef struct NyLPC_TcHttpd NyLPC_TcHttpd_t; + #define DEFINE_NyLPC_TcHttpd_t #endif /** Httpdセッションスレッドのスタックサイズ*/ #ifndef NyLPC_cHttpdThread_SIZE_OF_THREAD_STACK -# define NyLPC_cHttpdThread_SIZE_OF_THREAD_STACK 1024 +# define NyLPC_cHttpdThread_SIZE_OF_THREAD_STACK 1024 #endif typedef struct NyLPC_TcHttpdThread NyLPC_TcHttpdThread_t; @@ -31,15 +31,15 @@ typedef struct NyLPC_TcHttpdThread NyLPC_TcHttpdThread_t; */ struct NyLPC_TcHttpdThread { - NyLPC_TcThread_t _super; - /** 関数アドレスを格納したポインタ*/ - NyLPC_TcHttpdConnection_t _connection; + NyLPC_TcThread_t _super; + /** 関数アドレスを格納したポインタ*/ + NyLPC_TcHttpdConnection_t _connection; }; -void NyLPC_cHttpdThread_initialize(NyLPC_TcHttpdThread_t* i_inst,NyLPC_TcHttpd_t* i_parent,NyLPC_TInt32 i_prio); +NyLPC_TBool NyLPC_cHttpdThread_initialize(NyLPC_TcHttpdThread_t* i_inst,NyLPC_TcHttpd_t* i_parent,NyLPC_TInt32 i_prio); void NyLPC_cHttpdThread_finalize(NyLPC_TcHttpdThread_t* i_inst); -NyLPC_TBool NyLPC_cHttpdThread_start(NyLPC_TcHttpdThread_t* i_inst,NyLPC_TcTcpListener_t* i_listener); +NyLPC_TBool NyLPC_cHttpdThread_start(NyLPC_TcHttpdThread_t* i_inst,NyLPC_TiTcpListener_t* i_listener); #ifdef __cplusplus } diff --git a/lib/src/net/httpd/NyLPC_cHttpdThread_protected.h b/lib/src/net/httpd/NyLPC_cHttpdThread_protected.h index ad781cc..a8d1352 100644 --- a/lib/src/net/httpd/NyLPC_cHttpdThread_protected.h +++ b/lib/src/net/httpd/NyLPC_cHttpdThread_protected.h @@ -16,8 +16,8 @@ extern "C" { #endif /* __cplusplus */ #ifndef DEFINE_NyLPC_TcHttpd_t - typedef struct NyLPC_TcHttpd NyLPC_TcHttpd_t; - #define DEFINE_NyLPC_TcHttpd_t + typedef struct NyLPC_TcHttpd NyLPC_TcHttpd_t; + #define DEFINE_NyLPC_TcHttpd_t #endif diff --git a/lib/src/net/httpd/NyLPC_cHttpdUtils.c b/lib/src/net/httpd/NyLPC_cHttpdUtils.c index 3286a0d..18125cc 100644 --- a/lib/src/net/httpd/NyLPC_cHttpdUtils.c +++ b/lib/src/net/httpd/NyLPC_cHttpdUtils.c @@ -11,36 +11,36 @@ NyLPC_TBool NyLPC_cHttpdUtils_sendFixedContentBatch(NyLPC_TcHttpdConnection_t* i_connection,const NyLPC_TChar* i_content_type,const NyLPC_TChar* i_content,NyLPC_TUInt32 i_size) { - //HEAD or GET - switch(NyLPC_cHttpdConnection_getMethod(i_connection)) - { - case NyLPC_THttpMethodType_HEAD: - //HTTP Header - NyLPC_cHttpdConnection_sendResponseHeader2(i_connection,200,i_content_type,i_size,NULL); - break; - case NyLPC_THttpMethodType_GET: - //HTTP Header - NyLPC_cHttpdConnection_sendResponseHeader2(i_connection,200,i_content_type,i_size,NULL); - //HTTP Body - NyLPC_cHttpdConnection_sendResponseBody(i_connection,i_content,i_size); - break; - default: - //ERROR 405 - NyLPC_cHttpdConnection_sendResponseHeader2(i_connection,405,"text/html",0,NULL); - return NyLPC_TBool_FALSE; - } - return NyLPC_TBool_TRUE; + //HEAD or GET + switch(NyLPC_cHttpdConnection_getMethod(i_connection)) + { + case NyLPC_THttpMethodType_HEAD: + //HTTP Header + NyLPC_cHttpdConnection_sendResponseHeader2(i_connection,200,i_content_type,i_size,NULL); + break; + case NyLPC_THttpMethodType_GET: + //HTTP Header + NyLPC_cHttpdConnection_sendResponseHeader2(i_connection,200,i_content_type,i_size,NULL); + //HTTP Body + NyLPC_cHttpdConnection_sendResponseBody(i_connection,i_content,i_size); + break; + default: + //ERROR 405 + NyLPC_cHttpdConnection_sendResponseHeader2(i_connection,405,"text/html",0,NULL); + return NyLPC_TBool_FALSE; + } + return NyLPC_TBool_TRUE; } void NyLPC_cHttpdUtils_sendErrorResponse(NyLPC_TcHttpdConnection_t* i_connection,int i_status) { - NyLPC_TUInt8 mt=NyLPC_cHttpdConnection_getMethod(i_connection); - //ConnectionをCLOSEへセット - NyLPC_cHttpdConnection_setConnectionMode(i_connection,NyLPC_TcHttpdConnection_CONNECTION_MODE_CLOSE); - NyLPC_cHttpdConnection_sendResponseHeader(i_connection,i_status,"text/html",NULL); - if(mt!=NyLPC_THttpMethodType_HEAD){ - NyLPC_cHttpdConnection_sendResponseBodyF(i_connection,"MiMicHTTPDStatus %d",i_status); - } + NyLPC_TUInt8 mt=NyLPC_cHttpdConnection_getMethod(i_connection); + //ConnectionをCLOSEへセット + NyLPC_cHttpdConnection_setConnectionMode(i_connection,NyLPC_TcHttpdConnection_CONNECTION_MODE_CLOSE); + NyLPC_cHttpdConnection_sendResponseHeader(i_connection,i_status,"text/html",NULL); + if(mt!=NyLPC_THttpMethodType_HEAD){ + NyLPC_cHttpdConnection_sendResponseBodyF(i_connection,"MiMicHTTPDStatus %d",i_status); + } } @@ -50,11 +50,11 @@ void NyLPC_cHttpdUtils_sendErrorResponse(NyLPC_TcHttpdConnection_t* i_connection */ NyLPC_TBool NyLPC_cHttpdUtils_sendJsonHeader(NyLPC_TcHttpdConnection_t* i_connection) { - const static char* additional_header= - "Access-Control-Allow-Origin:*\r\n" - "Pragma: no-cache\r\n" - "Cache-Control: no-cache\r\n"; - const static char* content_type="application/json"; - return NyLPC_cHttpdConnection_sendResponseHeader(i_connection,200,content_type,additional_header); + const static char* additional_header= + "Access-Control-Allow-Origin:*\r\n" + "Pragma: no-cache\r\n" + "Cache-Control: no-cache\r\n"; + const static char* content_type="application/json"; + return NyLPC_cHttpdConnection_sendResponseHeader(i_connection,200,content_type,additional_header); } diff --git a/lib/src/net/httpd/NyLPC_cHttpd_protected.h b/lib/src/net/httpd/NyLPC_cHttpd_protected.h index 408fc33..0c1fb10 100644 --- a/lib/src/net/httpd/NyLPC_cHttpd_protected.h +++ b/lib/src/net/httpd/NyLPC_cHttpd_protected.h @@ -11,8 +11,8 @@ extern "C" { #endif /* __cplusplus */ #ifndef DEFINE_NyLPC_TcHttpdConnection_t - typedef struct NyLPC_TcHttpdConnection NyLPC_TcHttpdConnection_t; - #define DEFINE_NyLPC_TcHttpdConnection_t + typedef struct NyLPC_TcHttpdConnection NyLPC_TcHttpdConnection_t; + #define DEFINE_NyLPC_TcHttpdConnection_t #endif diff --git a/lib/src/net/httpd/mod/NyLPC_cModFileIoBaseClass.c b/lib/src/net/httpd/mod/NyLPC_cModFileIoBaseClass.c index beb7d7d..0cd2504 100644 --- a/lib/src/net/httpd/mod/NyLPC_cModFileIoBaseClass.c +++ b/lib/src/net/httpd/mod/NyLPC_cModFileIoBaseClass.c @@ -19,8 +19,8 @@ * along with this program. If not, see . * * For further information please contact. - * http://nyatla.jp/ - * or + * http://nyatla.jp/ + * or * *********************************************************************************/ #include "NyLPC_cModFileIoBaseClass.h" @@ -30,27 +30,27 @@ #include "../NyLPC_cHttpdUtils.h" #include "NyLPC_net.h" -#define FNAME_MAX 48 -#define STRBUF_MAX 48 +#define FNAME_MAX 48 +#define STRBUF_MAX 48 struct TModFileIoHeader { - struct NyLPC_THttpBasicHeader super; - NyLPC_TcUrlEncode_t urlencode; - NyLPC_TUInt8 _content_id; - //解析用 - NyLPC_TUInt8 _qery_name_id; - NyLPC_TUInt8 _astate; - NyLPC_TInt16 _prefix_len; - NyLPC_TcStr_t _tstr; - NyLPC_TChar _tstr_buf[STRBUF_MAX]; - /** 文字列のパーサ*/ - char fname[FNAME_MAX];//対象ファイル名の格納先 + struct NyLPC_THttpBasicHeader super; + NyLPC_TcUrlEncode_t urlencode; + NyLPC_TUInt8 _content_id; + //解析用 + NyLPC_TUInt8 _qery_name_id; + NyLPC_TUInt8 _astate; + NyLPC_TInt16 _prefix_len; + NyLPC_TcStr_t _tstr; + NyLPC_TChar _tstr_buf[STRBUF_MAX]; + /** 文字列のパーサ*/ + char fname[FNAME_MAX];//対象ファイル名の格納先 }; #define ST_PARSE_PATH 1 #define ST_PARSE_QUERY_NAME 2 -#define ST_PARSE_QUERY_VALUE 3 //Query読み出し中 +#define ST_PARSE_QUERY_VALUE 3 //Query読み出し中 #define ST_PARSE_QUERY_VALUE_NAME 4 /** * コンテンツID定義(コンテンツ名に対応) @@ -61,137 +61,137 @@ struct TModFileIoHeader #define CONTENT_ID_UNKNOWN 0 -#define QNAME_ID_NAME 1 +#define QNAME_ID_NAME 1 #define QNAME_ID_UNKNOWN 0 static const struct NyLPC_TTextIdTbl url_tbl[]= { - {"upload.api",CONTENT_ID_UPLOAD}, - {"create.api",CONTENT_ID_CREATE}, - {"remove.api",CONTENT_ID_REMOVE}, - {NULL,CONTENT_ID_UNKNOWN} + {"upload.api",CONTENT_ID_UPLOAD}, + {"create.api",CONTENT_ID_CREATE}, + {"remove.api",CONTENT_ID_REMOVE}, + {NULL,CONTENT_ID_UNKNOWN} }; static const struct NyLPC_TTextIdTbl qname_id_table[]= { - {"name",QNAME_ID_NAME}, - {NULL,QNAME_ID_UNKNOWN} + {"name",QNAME_ID_NAME}, + {NULL,QNAME_ID_UNKNOWN} }; static NyLPC_TBool urlHandler(NyLPC_TcHttpBasicHeaderParser_t* i_inst,NyLPC_TChar i_c,struct NyLPC_THttpBasicHeader* o_out) { - struct TModFileIoHeader* out=(struct TModFileIoHeader*)o_out; - NyLPC_TChar c; - //読み飛ばし - if(out->_prefix_len<0){ - out->_prefix_len++; - return NyLPC_TBool_TRUE;//読み飛ばし - } - //Path解析 - if(out->_astate==ST_PARSE_PATH){ - if(i_c!='\0' && i_c!='?'){ - if(!NyLPC_cStr_put(&(out->_tstr),i_c)){ - //ERROR - NyLPC_OnErrorGoto(ERROR); - } - }else{ - out->_content_id=NyLPC_TTextIdTbl_getMatchId(NyLPC_cStr_str(&(out->_tstr)),url_tbl); - switch(out->_content_id) - { - case CONTENT_ID_UPLOAD: - case CONTENT_ID_CREATE: - case CONTENT_ID_REMOVE: - break; - default: - NyLPC_OnErrorGoto(ERROR); - } - NyLPC_cStr_clear(&(out->_tstr)); - out->_astate=ST_PARSE_QUERY_NAME;//クエリ名解析へ - } - return NyLPC_TBool_TRUE; - } - switch(out->_content_id) - { - case CONTENT_ID_UPLOAD: - case CONTENT_ID_CREATE: - case CONTENT_ID_REMOVE: - switch(out->_astate){ - case ST_PARSE_QUERY_NAME: - if(i_c!='\0' && i_c!='&' && i_c!='='){ - if(!NyLPC_cStr_put(&(out->_tstr),i_c)){ - NyLPC_OnErrorGoto(ERROR); - } - }else{ - //Query確定。 - out->_qery_name_id=NyLPC_TTextIdTbl_getMatchId(NyLPC_cStr_str(&(out->_tstr)),qname_id_table); - NyLPC_cStr_clear(&(out->_tstr)); - //クエリ値がある場合 - switch(out->_qery_name_id){ - case QNAME_ID_NAME: - NyLPC_cUrlEncode_reset(&out->urlencode); - out->_astate=ST_PARSE_QUERY_VALUE_NAME; - break; - default: - out->_astate=ST_PARSE_QUERY_VALUE; - break; - } - } - return NyLPC_TBool_TRUE; - case ST_PARSE_QUERY_VALUE: - //未知のクエリは無視 - if(i_c!='\0' && i_c!='&'){ - }else{ - //クエリ値解析完了 - out->_astate=ST_PARSE_QUERY_NAME; - } - return NyLPC_TBool_TRUE; - case ST_PARSE_QUERY_VALUE_NAME: - if(i_c!='\0' && i_c!='&'){ - //URLデコードしながら蓄積 - switch(NyLPC_cUrlEncode_decode(&out->urlencode,i_c,&c)){ - case NyLPC_TcUrlEncode_ST_NEXT: - break; - case NyLPC_TcUrlEncode_ST_DONE: - if(!NyLPC_cStr_put(&(out->_tstr),c)){ - NyLPC_OnErrorGoto(ERROR); - } - break; - default: - NyLPC_OnErrorGoto(ERROR); - } - return NyLPC_TBool_TRUE; - }else{ - if(NyLPC_cStr_len(&out->_tstr)<1){ - //ファイル名短すぎ - NyLPC_OnErrorGoto(ERROR); - } - //ファイル名を保存 - strcpy(out->fname,(const char*)NyLPC_cStr_str(&out->_tstr)); - //終端しているなら、次のクエリへ - out->_astate=ST_PARSE_QUERY_NAME; - } - return NyLPC_TBool_TRUE; - default: - break; - } - NyLPC_OnErrorGoto(ERROR); - default: - NyLPC_OnErrorGoto(ERROR); - } - return NyLPC_TBool_TRUE; + struct TModFileIoHeader* out=(struct TModFileIoHeader*)o_out; + NyLPC_TChar c; + //読み飛ばし + if(out->_prefix_len<0){ + out->_prefix_len++; + return NyLPC_TBool_TRUE;//読み飛ばし + } + //Path解析 + if(out->_astate==ST_PARSE_PATH){ + if(i_c!='\0' && i_c!='?'){ + if(!NyLPC_cStr_put(&(out->_tstr),i_c)){ + //ERROR + NyLPC_OnErrorGoto(ERROR); + } + }else{ + out->_content_id=NyLPC_TTextIdTbl_getMatchId(NyLPC_cStr_str(&(out->_tstr)),url_tbl); + switch(out->_content_id) + { + case CONTENT_ID_UPLOAD: + case CONTENT_ID_CREATE: + case CONTENT_ID_REMOVE: + break; + default: + NyLPC_OnErrorGoto(ERROR); + } + NyLPC_cStr_clear(&(out->_tstr)); + out->_astate=ST_PARSE_QUERY_NAME;//クエリ名解析へ + } + return NyLPC_TBool_TRUE; + } + switch(out->_content_id) + { + case CONTENT_ID_UPLOAD: + case CONTENT_ID_CREATE: + case CONTENT_ID_REMOVE: + switch(out->_astate){ + case ST_PARSE_QUERY_NAME: + if(i_c!='\0' && i_c!='&' && i_c!='='){ + if(!NyLPC_cStr_put(&(out->_tstr),i_c)){ + NyLPC_OnErrorGoto(ERROR); + } + }else{ + //Query確定。 + out->_qery_name_id=NyLPC_TTextIdTbl_getMatchId(NyLPC_cStr_str(&(out->_tstr)),qname_id_table); + NyLPC_cStr_clear(&(out->_tstr)); + //クエリ値がある場合 + switch(out->_qery_name_id){ + case QNAME_ID_NAME: + NyLPC_cUrlEncode_reset(&out->urlencode); + out->_astate=ST_PARSE_QUERY_VALUE_NAME; + break; + default: + out->_astate=ST_PARSE_QUERY_VALUE; + break; + } + } + return NyLPC_TBool_TRUE; + case ST_PARSE_QUERY_VALUE: + //未知のクエリは無視 + if(i_c!='\0' && i_c!='&'){ + }else{ + //クエリ値解析完了 + out->_astate=ST_PARSE_QUERY_NAME; + } + return NyLPC_TBool_TRUE; + case ST_PARSE_QUERY_VALUE_NAME: + if(i_c!='\0' && i_c!='&'){ + //URLデコードしながら蓄積 + switch(NyLPC_cUrlEncode_decode(&out->urlencode,i_c,&c)){ + case NyLPC_TcUrlEncode_ST_NEXT: + break; + case NyLPC_TcUrlEncode_ST_DONE: + if(!NyLPC_cStr_put(&(out->_tstr),c)){ + NyLPC_OnErrorGoto(ERROR); + } + break; + default: + NyLPC_OnErrorGoto(ERROR); + } + return NyLPC_TBool_TRUE; + }else{ + if(NyLPC_cStr_len(&out->_tstr)<1){ + //ファイル名短すぎ + NyLPC_OnErrorGoto(ERROR); + } + //ファイル名を保存 + strcpy(out->fname,(const char*)NyLPC_cStr_str(&out->_tstr)); + //終端しているなら、次のクエリへ + out->_astate=ST_PARSE_QUERY_NAME; + } + return NyLPC_TBool_TRUE; + default: + break; + } + NyLPC_OnErrorGoto(ERROR); + default: + NyLPC_OnErrorGoto(ERROR); + } + return NyLPC_TBool_TRUE; ERROR: - return NyLPC_TBool_FALSE; + return NyLPC_TBool_FALSE; } /** * デフォルトハンドラ */ static const struct NyLPC_TcHttpBasicHeaderParser_Handler handler= { - NULL, - urlHandler + NULL, + urlHandler }; @@ -200,18 +200,18 @@ static const struct NyLPC_TcHttpBasicHeaderParser_Handler handler= */ void NyLPC_cModFileIoBaseClass_initialize(NyLPC_TcModFileIoBaseClass_t* i_inst,const NyLPC_TChar* i_ref_root_path) { - NyLPC_cModRomFiles_initialize(&i_inst->super,i_ref_root_path,NULL,0); + NyLPC_cModRomFiles_initialize(&i_inst->super,i_ref_root_path,NULL,0); } void NyLPC_cModFileIoBaseClass_finalize(NyLPC_TcModFileIoBaseClass_t* i_inst) { - NyLPC_cModRomFiles_finalize(&i_inst->super); + NyLPC_cModRomFiles_finalize(&i_inst->super); } /** * モジュールがコネクションをハンドリングできるかを返します。 */ NyLPC_TBool NyLPC_cModFileIoBaseClass_canHandle(NyLPC_TcModFileIoBaseClass_t* i_inst,NyLPC_TcHttpdConnection_t* i_connection) { - return NyLPC_cModRomFiles_canHandle(&i_inst->super,i_connection); + return NyLPC_cModRomFiles_canHandle(&i_inst->super,i_connection); } static struct TModFileIoHeader single_header; @@ -222,118 +222,118 @@ static struct TModFileIoHeader single_header; */ NyLPC_TBool NyLPC_cModFileIoBaseClass_execute(NyLPC_TcModFileIoBaseClass_t* i_inst,NyLPC_TcHttpdConnection_t* i_connection) { - union{ - NyLPC_TcHttpBasicHeaderParser_t parser; - NyLPC_TcHttpBodyParser_t body_parser; - }sh; - NyLPC_TUInt8 method_type; - //リクエストParse済へ遷移(この関数の後はModが責任を持ってリクエストを返却) - NyLPC_cHttpdConnection_setReqStatusParsed(i_connection); + union{ + NyLPC_TcHttpBasicHeaderParser_t parser; + NyLPC_TcHttpBodyParser_t body_parser; + }sh; + NyLPC_TUInt8 method_type; + //リクエストParse済へ遷移(この関数の後はModが責任を持ってリクエストを返却) + NyLPC_cHttpdConnection_setReqStatusParsed(i_connection); - //排他ロック - NyLPC_cHttpdConnection_lock(i_connection); - {//parser + //排他ロック + NyLPC_cHttpdConnection_lock(i_connection); + {//parser - //URL解析の準備 - single_header._prefix_len=-((NyLPC_TInt16)strlen(i_inst->super._ref_root_path)+2); - single_header._astate=ST_PARSE_PATH; - single_header.fname[0]='\0'; - NyLPC_cUrlEncode_initialize(&single_header.urlencode); - NyLPC_cStr_initialize(&single_header._tstr,single_header._tstr_buf,STRBUF_MAX); + //URL解析の準備 + single_header._prefix_len=-((NyLPC_TInt16)strlen(i_inst->super._ref_root_path)+2); + single_header._astate=ST_PARSE_PATH; + single_header.fname[0]='\0'; + NyLPC_cUrlEncode_initialize(&single_header.urlencode); + NyLPC_cStr_initialize(&single_header._tstr,single_header._tstr_buf,STRBUF_MAX); - NyLPC_cHttpBasicHeaderParser_initialize(&sh.parser,&handler); + NyLPC_cHttpBasicHeaderParser_initialize(&sh.parser,&handler); - //プリフェッチしたデータを流す - NyLPC_cHttpBasicHeaderParser_parseInit(&sh.parser,&(single_header.super)); - NyLPC_cHttpdConnection_pushPrefetchInfo(i_connection,&sh.parser,&single_header.super); - //後続をストリームから取り込む - if(!NyLPC_cHttpBasicHeaderParser_parseStream(&sh.parser,NyLPC_cHttpdConnection_refStream(i_connection),&(single_header.super))){ - NyLPC_cHttpdUtils_sendErrorResponse(i_connection,500); - NyLPC_OnErrorGoto(Error1); - } - if(!NyLPC_cHttpBasicHeaderParser_parseFinish(&sh.parser,&(single_header.super))){ - NyLPC_cHttpdUtils_sendErrorResponse(i_connection,500); - NyLPC_OnErrorGoto(Error1); - } - //HeaderParserはここで破棄(URLEncode,cSTRも) - NyLPC_cHttpBasicHeaderParser_finalize(&sh.parser); - NyLPC_cUrlEncode_finalize(&single_header.urlencode); - NyLPC_cStr_finalize(&single_header._tstr); - } - //Request::ConnectionがClose設定,又はHTTP1.1では無い場合,CLOSE - if(single_header.super.connection==NyLPC_THttpMessgeHeader_Connection_CLOSE || single_header.super.startline.req.version!=NyLPC_THttpVersion_11) - { - NyLPC_cHttpdConnection_setConnectionMode(i_connection,NyLPC_TcHttpdConnection_CONNECTION_MODE_CLOSE); - } - //返答フェーズ - { - method_type=NyLPC_cHttpdConnection_getMethod(i_connection); + //プリフェッチしたデータを流す + NyLPC_cHttpBasicHeaderParser_parseInit(&sh.parser,&(single_header.super)); + NyLPC_cHttpdConnection_pushPrefetchInfo(i_connection,&sh.parser,&single_header.super); + //後続をストリームから取り込む + if(!NyLPC_cHttpBasicHeaderParser_parseStream(&sh.parser,NyLPC_cHttpdConnection_refStream(i_connection),&(single_header.super))){ + NyLPC_cHttpdUtils_sendErrorResponse(i_connection,500); + NyLPC_OnErrorGoto(Error1); + } + if(!NyLPC_cHttpBasicHeaderParser_parseFinish(&sh.parser,&(single_header.super))){ + NyLPC_cHttpdUtils_sendErrorResponse(i_connection,500); + NyLPC_OnErrorGoto(Error1); + } + //HeaderParserはここで破棄(URLEncode,cSTRも) + NyLPC_cHttpBasicHeaderParser_finalize(&sh.parser); + NyLPC_cUrlEncode_finalize(&single_header.urlencode); + NyLPC_cStr_finalize(&single_header._tstr); + } + //Request::ConnectionがClose設定,又はHTTP1.1では無い場合,CLOSE + if(single_header.super.connection==NyLPC_THttpMessgeHeader_Connection_CLOSE || single_header.super.startline.req.version!=NyLPC_THttpVersion_11) + { + NyLPC_cHttpdConnection_setConnectionMode(i_connection,NyLPC_TcHttpdConnection_CONNECTION_MODE_CLOSE); + } + //返答フェーズ + { + method_type=NyLPC_cHttpdConnection_getMethod(i_connection); - //CGIの実行 - switch(single_header._content_id) - { - case CONTENT_ID_UPLOAD: - //ファイル名とBodyParserを通知 - if(method_type==NyLPC_THttpMethodType_POST) - { - NyLPC_cHttpdConnection_send100Continue(i_connection); - NyLPC_cHttpBodyParser_initialize(&sh.body_parser); - NyLPC_cHttpBodyParser_parseInit(&sh.body_parser,&single_header.super); - //ハンドラ内ではparseStreamのみ実行 - if(!i_inst->_abstruct_function.upload(i_connection,single_header.fname,&sh.body_parser)){ - NyLPC_cHttpdConnection_setConnectionMode(i_connection,NyLPC_TcHttpdConnection_CONNECTION_MODE_CLOSE); - } - if(NyLPC_cHttpBodyParser_parseFinish(&sh.body_parser)){ - NyLPC_cHttpBodyParser_finalize(&sh.body_parser); - NyLPC_OnErrorGoto(Error2);//エラーメッセージはハンドラ内で送られていると仮定する。 - } - NyLPC_cHttpBodyParser_finalize(&sh.body_parser); - }else{ - NyLPC_OnErrorGoto(Error2); - } - break; - case CONTENT_ID_CREATE: - if(method_type==NyLPC_THttpMethodType_GET || method_type==NyLPC_THttpMethodType_HEAD) - { - //イベント起動 - if(!i_inst->_abstruct_function.create(i_connection,single_header.fname)){ - NyLPC_cHttpdConnection_setConnectionMode(i_connection,NyLPC_TcHttpdConnection_CONNECTION_MODE_CLOSE); - } - break; - } - NyLPC_OnErrorGoto(Error2_405); - case CONTENT_ID_REMOVE: - //ファイル名を通知 - if(method_type==NyLPC_THttpMethodType_GET || method_type==NyLPC_THttpMethodType_HEAD) - { - //イベント起動 - if(!i_inst->_abstruct_function.remove(i_connection,single_header.fname)){ - NyLPC_cHttpdConnection_setConnectionMode(i_connection,NyLPC_TcHttpdConnection_CONNECTION_MODE_CLOSE); - } - break; - } - NyLPC_OnErrorGoto(Error2_405); - default: - NyLPC_cHttpdUtils_sendErrorResponse(i_connection,400); - NyLPC_OnErrorGoto(Error2); - } - } + //CGIの実行 + switch(single_header._content_id) + { + case CONTENT_ID_UPLOAD: + //ファイル名とBodyParserを通知 + if(method_type==NyLPC_THttpMethodType_POST) + { + NyLPC_cHttpdConnection_send100Continue(i_connection); + NyLPC_cHttpBodyParser_initialize(&sh.body_parser); + NyLPC_cHttpBodyParser_parseInit(&sh.body_parser,&single_header.super); + //ハンドラ内ではparseStreamのみ実行 + if(!i_inst->_abstruct_function.upload(i_connection,single_header.fname,&sh.body_parser)){ + NyLPC_cHttpdConnection_setConnectionMode(i_connection,NyLPC_TcHttpdConnection_CONNECTION_MODE_CLOSE); + } + if(NyLPC_cHttpBodyParser_parseFinish(&sh.body_parser)){ + NyLPC_cHttpBodyParser_finalize(&sh.body_parser); + NyLPC_OnErrorGoto(Error2);//エラーメッセージはハンドラ内で送られていると仮定する。 + } + NyLPC_cHttpBodyParser_finalize(&sh.body_parser); + }else{ + NyLPC_OnErrorGoto(Error2); + } + break; + case CONTENT_ID_CREATE: + if(method_type==NyLPC_THttpMethodType_GET || method_type==NyLPC_THttpMethodType_HEAD) + { + //イベント起動 + if(!i_inst->_abstruct_function.create(i_connection,single_header.fname)){ + NyLPC_cHttpdConnection_setConnectionMode(i_connection,NyLPC_TcHttpdConnection_CONNECTION_MODE_CLOSE); + } + break; + } + NyLPC_OnErrorGoto(Error2_405); + case CONTENT_ID_REMOVE: + //ファイル名を通知 + if(method_type==NyLPC_THttpMethodType_GET || method_type==NyLPC_THttpMethodType_HEAD) + { + //イベント起動 + if(!i_inst->_abstruct_function.remove(i_connection,single_header.fname)){ + NyLPC_cHttpdConnection_setConnectionMode(i_connection,NyLPC_TcHttpdConnection_CONNECTION_MODE_CLOSE); + } + break; + } + NyLPC_OnErrorGoto(Error2_405); + default: + NyLPC_cHttpdUtils_sendErrorResponse(i_connection,400); + NyLPC_OnErrorGoto(Error2); + } + } //占有解除 - NyLPC_cHttpdConnection_unlock(i_connection); - return NyLPC_TBool_TRUE; + NyLPC_cHttpdConnection_unlock(i_connection); + return NyLPC_TBool_TRUE; Error2_405: - NyLPC_cHttpdUtils_sendErrorResponse(i_connection,405); + NyLPC_cHttpdUtils_sendErrorResponse(i_connection,405); Error2: - //VM排他ロックの解除 - NyLPC_cHttpdConnection_unlock(i_connection); - return NyLPC_TBool_FALSE; + //VM排他ロックの解除 + NyLPC_cHttpdConnection_unlock(i_connection); + return NyLPC_TBool_FALSE; Error1: - NyLPC_cHttpBasicHeaderParser_finalize(&parser); - NyLPC_cStr_finalize(&single_header._tstr); - NyLPC_cUrlEncode_finalize(&single_header.urlencode); - //VM排他ロックの解除 - NyLPC_cHttpdConnection_unlock(i_connection); - return NyLPC_TBool_FALSE; + NyLPC_cHttpBasicHeaderParser_finalize(&parser); + NyLPC_cStr_finalize(&single_header._tstr); + NyLPC_cUrlEncode_finalize(&single_header.urlencode); + //VM排他ロックの解除 + NyLPC_cHttpdConnection_unlock(i_connection); + return NyLPC_TBool_FALSE; } diff --git a/lib/src/net/httpd/mod/NyLPC_cModFileIoBaseClass.h b/lib/src/net/httpd/mod/NyLPC_cModFileIoBaseClass.h index 33f19a5..3f33a73 100644 --- a/lib/src/net/httpd/mod/NyLPC_cModFileIoBaseClass.h +++ b/lib/src/net/httpd/mod/NyLPC_cModFileIoBaseClass.h @@ -56,9 +56,9 @@ typedef NyLPC_TBool (*NyLPC_cModFileIoBaseClass_removeFunction)(NyLPC_TcHttpdCon */ struct NyLPC_TcModFileIoBaseClass_AbstructFunction { - NyLPC_cModFileIoBaseClass_uploadFunction upload; - NyLPC_cModFileIoBaseClass_createFunction create; - NyLPC_cModFileIoBaseClass_removeFunction remove; + NyLPC_cModFileIoBaseClass_uploadFunction upload; + NyLPC_cModFileIoBaseClass_createFunction create; + NyLPC_cModFileIoBaseClass_removeFunction remove; }; /** @@ -66,11 +66,11 @@ struct NyLPC_TcModFileIoBaseClass_AbstructFunction */ struct NyLPC_TcModFileIoBaseClass { - NyLPC_TcModRomFiles_t super; - /** - * 処理関数へのポインタ構造体 - */ - struct NyLPC_TcModFileIoBaseClass_AbstructFunction _abstruct_function; + NyLPC_TcModRomFiles_t super; + /** + * 処理関数へのポインタ構造体 + */ + struct NyLPC_TcModFileIoBaseClass_AbstructFunction _abstruct_function; }; /** diff --git a/lib/src/net/httpd/mod/NyLPC_cModJsonRpc.c b/lib/src/net/httpd/mod/NyLPC_cModJsonRpc.c index d810ac1..aabf63d 100644 --- a/lib/src/net/httpd/mod/NyLPC_cModJsonRpc.c +++ b/lib/src/net/httpd/mod/NyLPC_cModJsonRpc.c @@ -146,7 +146,7 @@ static const NyLPC_TChar* code2msg(NyLPC_TInt32 i_id) return _table[i].n; } } - return _table[6].n; + return _table[5].n; } NyLPC_TBool NyLPC_cModJsonRpc_putError(NyLPC_TcModJsonRpc_t* i_inst,NyLPC_TUInt32 i_id,NyLPC_TInt32 i_code) diff --git a/lib/src/net/httpd/mod/NyLPC_cModMiMicSetting.c b/lib/src/net/httpd/mod/NyLPC_cModMiMicSetting.c index 683c6d8..bcb8257 100644 --- a/lib/src/net/httpd/mod/NyLPC_cModMiMicSetting.c +++ b/lib/src/net/httpd/mod/NyLPC_cModMiMicSetting.c @@ -7,7 +7,7 @@ * * MiMic is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published - * by the Free Software Foundation, either version 3 of the License, or + * by the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, @@ -19,8 +19,8 @@ * along with this program. If not, see . * * For further information please contact. - * http://nyatla.jp/ - * or + * http://nyatla.jp/ + * or * *********************************************************************************/ #include "NyLPC_cModMiMicSetting.h" @@ -30,61 +30,60 @@ #include "NyLPC_mimicVm.h" #include "NyLPC_flash.h" #include "../NyLPC_cHttpdConnection_protected.h" -#include "../../NyLPC_cNet.h" //#include #define MOD_VERSION "ModMiMicSetting/1.4" #define SIZE_OF_SETUP_PARAM 8 struct TModMiMicSettingRequest { - struct NyLPC_THttpBasicHeader super; - NyLPC_TUInt8 _content_id; - //解析用 - NyLPC_TUInt8 _qery_name_id; - NyLPC_TUInt8 _astate; - NyLPC_TInt16 _prefix_len; - NyLPC_TcStr_t _tstr; - NyLPC_TChar _tstr_buf[16]; - /** 文字列のパーサ*/ - NyLPC_TcMiMicDbCompiler_t _binparser; - union{ - /** - * スタックサイズ削減のための構造体。tmpは受信処理に使用。 - * memimgはFlashへ書き込むときに使用。 - */ - union{ - /** - * 受信用構造体。host_name,param_buf[6]までのデータは、memimgのfast_boot以降のデータ構造と位置をあわせてください。 - * param_buf[7]以降については値変換必須 - * tmpにデータを作成後にmemimgへ整形して書きこむかんじ。 - */ - struct{ - NyLPC_TUInt16 param_len; - NyLPC_TUInt16 host_len; - NyLPC_TChar host_name[NyLPC_TcNetConfig_HOSTNAME_LEN]; - /**pパラメータ。最大長さは16。 - * 詳細はNyLPC_TcModMiMicSetting_tを参照 - */ - NyLPC_TUInt32 param_buf[SIZE_OF_SETUP_PARAM]; - NyLPC_TUInt32 cval;//コマンド値 - }tmp; - /** - * 書き込み用構造体 - */ - struct NyLPC_TMiMicConfigulation memimg; - }setup; - struct{ - /** - * 不明な名前の場合は、ここに名前をコピー - */ - NyLPC_TChar path[32]; - }unknown; - }content; + struct NyLPC_THttpBasicHeader super; + NyLPC_TUInt8 _content_id; + //解析用 + NyLPC_TUInt8 _qery_name_id; + NyLPC_TUInt8 _astate; + NyLPC_TInt16 _prefix_len; + NyLPC_TcStr_t _tstr; + NyLPC_TChar _tstr_buf[16]; + /** 文字列のパーサ*/ + NyLPC_TcMiMicDbCompiler_t _binparser; + union{ + /** + * スタックサイズ削減のための構造体。tmpは受信処理に使用。 + * memimgはFlashへ書き込むときに使用。 + */ + union{ + /** + * 受信用構造体。host_name,param_buf[6]までのデータは、memimgのfast_boot以降のデータ構造と位置をあわせてください。 + * param_buf[7]以降については値変換必須 + * tmpにデータを作成後にmemimgへ整形して書きこむかんじ。 + */ + struct{ + NyLPC_TUInt16 param_len; + NyLPC_TUInt16 host_len; + NyLPC_TChar host_name[NyLPC_TcNetConfig_HOSTNAME_LEN]; + /**pパラメータ。最大長さは16。 + * 詳細はNyLPC_TcModMiMicSetting_tを参照 + */ + NyLPC_TUInt32 param_buf[SIZE_OF_SETUP_PARAM]; + NyLPC_TUInt32 cval;//コマンド値 + }tmp; + /** + * 書き込み用構造体 + */ + struct NyLPC_TMiMicConfigulation memimg; + }setup; + struct{ + /** + * 不明な名前の場合は、ここに名前をコピー + */ + NyLPC_TChar path[32]; + }unknown; + }content; }; #define ST_PARSE_PATH 1 #define ST_PARSE_QUERY_NAME 2 -#define ST_PARSE_QUERY_VALUE 3 //Query読み出し中 +#define ST_PARSE_QUERY_VALUE 3 //Query読み出し中 #define ST_PARSE_QUERY_VALUE_P 4 #define ST_PARSE_QUERY_VALUE_C 5 #define ST_PARSE_QUERY_VALUE_HOST 6 @@ -98,9 +97,9 @@ struct TModMiMicSettingRequest #define CONTENT_ID_CSS 5 #define CONTENT_ID_LOGO 6 -#define QNAME_ID_P 4 -#define QNAME_ID_C 5 -#define QNAME_ID_HOST 6 +#define QNAME_ID_P 4 +#define QNAME_ID_C 5 +#define QNAME_ID_HOST 6 #define QNAME_ID_UNKNOWN 0 #define QVAL_C_GET 1 @@ -110,16 +109,16 @@ struct TModMiMicSettingRequest static const struct NyLPC_TTextIdTbl url_tbl[]= { - {"setup.api",CONTENT_ID_SETUP}, - {NULL,CONTENT_ID_UNKNOWN} + {"setup.api",CONTENT_ID_SETUP}, + {NULL,CONTENT_ID_UNKNOWN} }; static const struct NyLPC_TTextIdTbl qname_id_table[]= { - {"p",QNAME_ID_P}, - {"c",QNAME_ID_C}, - {"host",QNAME_ID_HOST}, - {NULL,QNAME_ID_UNKNOWN} + {"p",QNAME_ID_P}, + {"c",QNAME_ID_C}, + {"host",QNAME_ID_HOST}, + {NULL,QNAME_ID_UNKNOWN} }; @@ -128,156 +127,156 @@ static const struct NyLPC_TTextIdTbl qname_id_table[]= static NyLPC_TBool urlHandler(NyLPC_TcHttpBasicHeaderParser_t* i_inst,NyLPC_TChar i_c,struct NyLPC_THttpBasicHeader* o_out) { - struct TModMiMicSettingRequest* out=(struct TModMiMicSettingRequest*)o_out; - //読み飛ばし - if(out->_prefix_len<0){ - out->_prefix_len++; - return NyLPC_TBool_TRUE;//読み飛ばし - } - if(out->_astate==ST_PARSE_PATH){ - if(i_c!='\0' && i_c!='?'){ - if(!NyLPC_cStr_put(&(out->_tstr),i_c)){ - NyLPC_OnErrorGoto(ERROR); - } - return NyLPC_TBool_TRUE; - } - out->_content_id=NyLPC_TTextIdTbl_getMatchId(NyLPC_cStr_str(&(out->_tstr)),url_tbl); - switch(out->_content_id) - { - case CONTENT_ID_SETUP: - out->content.setup.tmp.param_len=0;//クエリが無い場合の初期値 - out->content.setup.tmp.host_len =0;//クエリが無い場合の初期値 - out->content.setup.tmp.cval=QVAL_C_UNKNOWN; - break; - default: - break; - } - NyLPC_cStr_clear(&(out->_tstr)); - out->_astate=ST_PARSE_QUERY_NAME;//クエリ名解析へ - return NyLPC_TBool_TRUE; - } - switch(out->_content_id) - { - case CONTENT_ID_SETUP: - switch(out->_astate){ - case ST_PARSE_QUERY_NAME: - if(i_c!='\0' && i_c!='&' && i_c!='='){ - if(!NyLPC_cStr_put(&(out->_tstr),i_c)){ - NyLPC_OnErrorGoto(ERROR); - } - return NyLPC_TBool_TRUE; - } - //Query確定。 - out->_qery_name_id=NyLPC_TTextIdTbl_getMatchId(NyLPC_cStr_str(&(out->_tstr)),qname_id_table); - NyLPC_cStr_clear(&(out->_tstr)); - //クエリ値がある場合 - switch(out->_qery_name_id){ - case QNAME_ID_P: - out->_astate=ST_PARSE_QUERY_VALUE_P;//MIMICBCのDBパラメータパーサを借用。 - out->content.setup.tmp.param_len=0; - break; - case QNAME_ID_C: - out->_astate=ST_PARSE_QUERY_VALUE_C; - break; - case QNAME_ID_HOST: - out->_astate=ST_PARSE_QUERY_VALUE_HOST;//_host_nameに蓄積 - out->content.setup.tmp.host_len=0; - break; - default: - out->_astate=ST_PARSE_QUERY_VALUE; - break; - } - return NyLPC_TBool_TRUE; - case ST_PARSE_QUERY_VALUE: - //未知のクエリは無視 - if(i_c!='\0' && i_c!='&'){ - return NyLPC_TBool_TRUE; - } - //クエリ値解析完了 - out->_astate=ST_PARSE_QUERY_NAME; - return NyLPC_TBool_TRUE; - case ST_PARSE_QUERY_VALUE_HOST: - //未知のクエリは無視 - if(i_c!='\0' && i_c!='&'){ - //許可する文字列は、[:AlNum:]||'_' - if(!isalnum(i_c) && i_c!='_'){ - NyLPC_OnErrorGoto(ERROR); - } - out->content.setup.tmp.host_name[out->content.setup.tmp.host_len++]=i_c; - if(out->content.setup.tmp.host_len>=NyLPC_TcNetConfig_HOSTNAME_LEN){ - //長すぎ - NyLPC_OnErrorGoto(ERROR); - } - return NyLPC_TBool_TRUE; - } - //クエリ値解析完了 - out->content.setup.tmp.host_name[out->content.setup.tmp.host_len]='\0'; - out->_astate=ST_PARSE_QUERY_NAME; - return NyLPC_TBool_TRUE; - case ST_PARSE_QUERY_VALUE_C: - if(i_c!='\0' && i_c!='&'){ - if(!NyLPC_cStr_put(&(out->_tstr),i_c)){ - NyLPC_OnErrorGoto(ERROR); - } - return NyLPC_TBool_TRUE; - } - if(NyLPC_cStr_isEqual(&out->_tstr,"get")){ - out->content.setup.tmp.cval=QVAL_C_GET; - }else if(NyLPC_cStr_isEqual(&out->_tstr,"update")){ - out->content.setup.tmp.cval=QVAL_C_UPDATE; - }else{ - NyLPC_OnErrorGoto(ERROR); - } - out->_astate=ST_PARSE_QUERY_NAME; - NyLPC_cStr_clear(&(out->_tstr)); - return NyLPC_TBool_TRUE; - case ST_PARSE_QUERY_VALUE_P: - if(i_c!='\0' && i_c!='&'){ - if(out->content.setup.tmp.param_len>=SIZE_OF_SETUP_PARAM) - { - NyLPC_OnErrorGoto(ERROR); - } - switch(NyLPC_cMiMicDbCompiler_compileFragment2(&(out->_binparser),i_c,out->content.setup.tmp.param_buf+out->content.setup.tmp.param_len)) - { - case NyLPC_TcMiMicDbCompiler_RET_CONTINUE: - break; - case NyLPC_TcMiMicDbCompiler_RET_OK: - out->content.setup.tmp.param_len++; - break; - case NyLPC_TcMiMicDbCompiler_RET_ERROR: - default: - //ERROR - NyLPC_OnErrorGoto(ERROR); - } - return NyLPC_TBool_TRUE; - } - //区切りのいいところで終わってる? - if(NyLPC_cMiMicDbCompiler_hasFragment(&(out->_binparser))){ - //ERROR - NyLPC_OnErrorGoto(ERROR); - } - //終端しているなら、次のクエリへ - out->_astate=ST_PARSE_QUERY_NAME; - NyLPC_cStr_clear(&(out->_tstr)); - return NyLPC_TBool_TRUE; - default: - break; - } - NyLPC_OnErrorGoto(ERROR); - default: - NyLPC_OnErrorGoto(ERROR); - } - return NyLPC_TBool_TRUE; + struct TModMiMicSettingRequest* out=(struct TModMiMicSettingRequest*)o_out; + //読み飛ばし + if(out->_prefix_len<0){ + out->_prefix_len++; + return NyLPC_TBool_TRUE;//読み飛ばし + } + if(out->_astate==ST_PARSE_PATH){ + if(i_c!='\0' && i_c!='?'){ + if(!NyLPC_cStr_put(&(out->_tstr),i_c)){ + NyLPC_OnErrorGoto(ERROR); + } + return NyLPC_TBool_TRUE; + } + out->_content_id=NyLPC_TTextIdTbl_getMatchId(NyLPC_cStr_str(&(out->_tstr)),url_tbl); + switch(out->_content_id) + { + case CONTENT_ID_SETUP: + out->content.setup.tmp.param_len=0;//クエリが無い場合の初期値 + out->content.setup.tmp.host_len =0;//クエリが無い場合の初期値 + out->content.setup.tmp.cval=QVAL_C_UNKNOWN; + break; + default: + break; + } + NyLPC_cStr_clear(&(out->_tstr)); + out->_astate=ST_PARSE_QUERY_NAME;//クエリ名解析へ + return NyLPC_TBool_TRUE; + } + switch(out->_content_id) + { + case CONTENT_ID_SETUP: + switch(out->_astate){ + case ST_PARSE_QUERY_NAME: + if(i_c!='\0' && i_c!='&' && i_c!='='){ + if(!NyLPC_cStr_put(&(out->_tstr),i_c)){ + NyLPC_OnErrorGoto(ERROR); + } + return NyLPC_TBool_TRUE; + } + //Query確定。 + out->_qery_name_id=NyLPC_TTextIdTbl_getMatchId(NyLPC_cStr_str(&(out->_tstr)),qname_id_table); + NyLPC_cStr_clear(&(out->_tstr)); + //クエリ値がある場合 + switch(out->_qery_name_id){ + case QNAME_ID_P: + out->_astate=ST_PARSE_QUERY_VALUE_P;//MIMICBCのDBパラメータパーサを借用。 + out->content.setup.tmp.param_len=0; + break; + case QNAME_ID_C: + out->_astate=ST_PARSE_QUERY_VALUE_C; + break; + case QNAME_ID_HOST: + out->_astate=ST_PARSE_QUERY_VALUE_HOST;//_host_nameに蓄積 + out->content.setup.tmp.host_len=0; + break; + default: + out->_astate=ST_PARSE_QUERY_VALUE; + break; + } + return NyLPC_TBool_TRUE; + case ST_PARSE_QUERY_VALUE: + //未知のクエリは無視 + if(i_c!='\0' && i_c!='&'){ + return NyLPC_TBool_TRUE; + } + //クエリ値解析完了 + out->_astate=ST_PARSE_QUERY_NAME; + return NyLPC_TBool_TRUE; + case ST_PARSE_QUERY_VALUE_HOST: + //未知のクエリは無視 + if(i_c!='\0' && i_c!='&'){ + //許可する文字列は、[:AlNum:]||'_' + if(!isalnum((int)i_c) && i_c!='_'){ + NyLPC_OnErrorGoto(ERROR); + } + out->content.setup.tmp.host_name[out->content.setup.tmp.host_len++]=i_c; + if(out->content.setup.tmp.host_len>=NyLPC_TcNetConfig_HOSTNAME_LEN){ + //長すぎ + NyLPC_OnErrorGoto(ERROR); + } + return NyLPC_TBool_TRUE; + } + //クエリ値解析完了 + out->content.setup.tmp.host_name[out->content.setup.tmp.host_len]='\0'; + out->_astate=ST_PARSE_QUERY_NAME; + return NyLPC_TBool_TRUE; + case ST_PARSE_QUERY_VALUE_C: + if(i_c!='\0' && i_c!='&'){ + if(!NyLPC_cStr_put(&(out->_tstr),i_c)){ + NyLPC_OnErrorGoto(ERROR); + } + return NyLPC_TBool_TRUE; + } + if(NyLPC_cStr_isEqual(&out->_tstr,"get")){ + out->content.setup.tmp.cval=QVAL_C_GET; + }else if(NyLPC_cStr_isEqual(&out->_tstr,"update")){ + out->content.setup.tmp.cval=QVAL_C_UPDATE; + }else{ + NyLPC_OnErrorGoto(ERROR); + } + out->_astate=ST_PARSE_QUERY_NAME; + NyLPC_cStr_clear(&(out->_tstr)); + return NyLPC_TBool_TRUE; + case ST_PARSE_QUERY_VALUE_P: + if(i_c!='\0' && i_c!='&'){ + if(out->content.setup.tmp.param_len>=SIZE_OF_SETUP_PARAM) + { + NyLPC_OnErrorGoto(ERROR); + } + switch(NyLPC_cMiMicDbCompiler_compileFragment2(&(out->_binparser),i_c,out->content.setup.tmp.param_buf+out->content.setup.tmp.param_len)) + { + case NyLPC_TcMiMicDbCompiler_RET_CONTINUE: + break; + case NyLPC_TcMiMicDbCompiler_RET_OK: + out->content.setup.tmp.param_len++; + break; + case NyLPC_TcMiMicDbCompiler_RET_ERROR: + default: + //ERROR + NyLPC_OnErrorGoto(ERROR); + } + return NyLPC_TBool_TRUE; + } + //区切りのいいところで終わってる? + if(NyLPC_cMiMicDbCompiler_hasFragment(&(out->_binparser))){ + //ERROR + NyLPC_OnErrorGoto(ERROR); + } + //終端しているなら、次のクエリへ + out->_astate=ST_PARSE_QUERY_NAME; + NyLPC_cStr_clear(&(out->_tstr)); + return NyLPC_TBool_TRUE; + default: + break; + } + NyLPC_OnErrorGoto(ERROR); + default: + NyLPC_OnErrorGoto(ERROR); + } + return NyLPC_TBool_TRUE; ERROR: - return NyLPC_TBool_FALSE; + return NyLPC_TBool_FALSE; } /** * デフォルトハンドラ */ static const struct NyLPC_TcHttpBasicHeaderParser_Handler handler= { - NULL, - urlHandler + NULL, + urlHandler }; @@ -286,18 +285,18 @@ static const struct NyLPC_TcHttpBasicHeaderParser_Handler handler= */ void NyLPC_cModMiMicSetting_initialize(NyLPC_TcModMiMicSetting_t* i_inst,const NyLPC_TChar* i_ref_root_path) { - NyLPC_cModRomFiles_initialize(&i_inst->super,i_ref_root_path,NULL,0); + NyLPC_cModRomFiles_initialize(&i_inst->super,i_ref_root_path,NULL,0); } void NyLPC_cModMiMicSetting_finalize(NyLPC_TcModMiMicSetting_t* i_inst) { - NyLPC_cModRomFiles_finalize(&i_inst->super); + NyLPC_cModRomFiles_finalize(&i_inst->super); } /** * モジュールがコネクションをハンドリングできるかを返します。 */ NyLPC_TBool NyLPC_cModMiMicSetting_canHandle(NyLPC_TcModMiMicSetting_t* i_inst,NyLPC_TcHttpdConnection_t* i_connection) { - return NyLPC_cModRomFiles_canHandle(&i_inst->super,i_connection); + return NyLPC_cModRomFiles_canHandle(&i_inst->super,i_connection); } @@ -309,206 +308,209 @@ static void setup_proc(NyLPC_TcHttpdConnection_t* i_connection,struct TModMiMicS */ NyLPC_TBool NyLPC_cModMiMicSetting_execute(NyLPC_TcModMiMicSetting_t* i_inst,NyLPC_TcHttpdConnection_t* i_connection) { - NyLPC_TUInt8 method_type; - struct TModMiMicSettingRequest header; - NyLPC_TcHttpBasicHeaderParser_t parser; + NyLPC_TUInt8 method_type; + struct TModMiMicSettingRequest header; + NyLPC_TcHttpBasicHeaderParser_t parser; - //リクエストParse済へ遷移(この関数の後はModが責任を持ってリクエストを返却) - NyLPC_cHttpdConnection_setReqStatusParsed(i_connection); + //リクエストParse済へ遷移(この関数の後はModが責任を持ってリクエストを返却) + NyLPC_cHttpdConnection_setReqStatusParsed(i_connection); - //URL解析の準備 - header._prefix_len=-((NyLPC_TInt16)strlen(i_inst->super._ref_root_path)+2); - header._astate=ST_PARSE_PATH; - NyLPC_cStr_initialize(&header._tstr,header._tstr_buf,16); - NyLPC_cMiMicDbCompiler_initialize(&header._binparser); + //URL解析の準備 + header._prefix_len=-((NyLPC_TInt16)strlen(i_inst->super._ref_root_path)+2); + header._astate=ST_PARSE_PATH; + NyLPC_cStr_initialize(&header._tstr,header._tstr_buf,16); + NyLPC_cMiMicDbCompiler_initialize(&header._binparser); - NyLPC_cHttpBasicHeaderParser_initialize(&parser,&handler); - //プリフェッチしたデータを流す - NyLPC_cHttpBasicHeaderParser_parseInit(&parser,&(header.super)); - NyLPC_cHttpdConnection_pushPrefetchInfo(i_connection,&parser,&header.super); - //後続をストリームから取り込む - if(!NyLPC_cHttpBasicHeaderParser_parseStream(&parser,NyLPC_cHttpdConnection_refStream(i_connection),&(header.super))){ - NyLPC_cHttpdUtils_sendErrorResponse(i_connection,500); - NyLPC_OnErrorGoto(Error2); - } - if(!NyLPC_cHttpBasicHeaderParser_parseFinish(&parser,&(header.super))){ - NyLPC_cHttpdUtils_sendErrorResponse(i_connection,500); - NyLPC_OnErrorGoto(Error2); - } - //GETかHEADに制限 - method_type=NyLPC_cHttpdConnection_getMethod(i_connection); - if(method_type!=NyLPC_THttpMethodType_GET && method_type!=NyLPC_THttpMethodType_HEAD) - { - NyLPC_cHttpdUtils_sendErrorResponse(i_connection,405); - NyLPC_OnErrorGoto(Error2); - } - //Request::ConnectionがClose設定,又はHTTP1.1では無い場合,CLOSE - if(header.super.connection==NyLPC_THttpMessgeHeader_Connection_CLOSE || header.super.startline.req.version!=NyLPC_THttpVersion_11) - { - NyLPC_cHttpdConnection_setConnectionMode(i_connection,NyLPC_TcHttpdConnection_CONNECTION_MODE_CLOSE); - } - //CGIの実行 - switch(header._content_id) - { - case CONTENT_ID_SETUP: - setup_proc(i_connection,&header); - break; - case CONTENT_ID_UNKNOWN: - default: - NyLPC_cHttpdUtils_sendErrorResponse(i_connection,500); - NyLPC_OnErrorGoto(Error2); - } - NyLPC_cHttpBasicHeaderParser_finalize(&parser); - NyLPC_cMiMicDbCompiler_finalize(&header._binparser); - NyLPC_cStr_finalize(&(header._tstr)); - return NyLPC_TBool_TRUE; + NyLPC_cHttpBasicHeaderParser_initialize(&parser,&handler); + //プリフェッチしたデータを流す + NyLPC_cHttpBasicHeaderParser_parseInit(&parser,&(header.super)); + NyLPC_cHttpdConnection_pushPrefetchInfo(i_connection,&parser,&header.super); + //後続をストリームから取り込む + if(!NyLPC_cHttpBasicHeaderParser_parseStream(&parser,NyLPC_cHttpdConnection_refStream(i_connection),&(header.super))){ + NyLPC_cHttpdUtils_sendErrorResponse(i_connection,500); + NyLPC_OnErrorGoto(Error2); + } + if(!NyLPC_cHttpBasicHeaderParser_parseFinish(&parser,&(header.super))){ + NyLPC_cHttpdUtils_sendErrorResponse(i_connection,500); + NyLPC_OnErrorGoto(Error2); + } + //GETかHEADに制限 + method_type=NyLPC_cHttpdConnection_getMethod(i_connection); + if(method_type!=NyLPC_THttpMethodType_GET && method_type!=NyLPC_THttpMethodType_HEAD) + { + NyLPC_cHttpdUtils_sendErrorResponse(i_connection,405); + NyLPC_OnErrorGoto(Error2); + } + //Request::ConnectionがClose設定,又はHTTP1.1では無い場合,CLOSE + if(header.super.connection==NyLPC_THttpMessgeHeader_Connection_CLOSE || header.super.startline.req.version!=NyLPC_THttpVersion_11) + { + NyLPC_cHttpdConnection_setConnectionMode(i_connection,NyLPC_TcHttpdConnection_CONNECTION_MODE_CLOSE); + } + //CGIの実行 + switch(header._content_id) + { + case CONTENT_ID_SETUP: + setup_proc(i_connection,&header); + break; + case CONTENT_ID_UNKNOWN: + default: + NyLPC_cHttpdUtils_sendErrorResponse(i_connection,500); + NyLPC_OnErrorGoto(Error2); + } + NyLPC_cHttpBasicHeaderParser_finalize(&parser); + NyLPC_cMiMicDbCompiler_finalize(&header._binparser); + NyLPC_cStr_finalize(&(header._tstr)); + return NyLPC_TBool_TRUE; Error2: - NyLPC_cHttpBasicHeaderParser_finalize(&parser); - NyLPC_cMiMicDbCompiler_finalize(&header._binparser); - NyLPC_cStr_finalize(&(header._tstr)); + NyLPC_cHttpBasicHeaderParser_finalize(&parser); + NyLPC_cMiMicDbCompiler_finalize(&header._binparser); + NyLPC_cStr_finalize(&(header._tstr)); - return NyLPC_TBool_FALSE; + return NyLPC_TBool_FALSE; } static void setup_proc(NyLPC_TcHttpdConnection_t* i_connection,struct TModMiMicSettingRequest* i_req) { - NyLPC_TBool ret; - const struct NyLPC_TMiMicConfigulation* config; - const NyLPC_TcNetConfig_t* currebt_cfg; - NyLPC_Assert( - (NyLPC_cHttpdConnection_getMethod(i_connection)==NyLPC_THttpMethodType_GET)|| - (NyLPC_cHttpdConnection_getMethod(i_connection)==NyLPC_THttpMethodType_HEAD)); + NyLPC_TBool ret; + const struct NyLPC_TMiMicConfigulation* config; + const NyLPC_TcNetConfig_t* currebt_cfg; + const struct NyLPC_TNetInterfaceInfo* netif_info; + NyLPC_Assert( + (NyLPC_cHttpdConnection_getMethod(i_connection)==NyLPC_THttpMethodType_GET)|| + (NyLPC_cHttpdConnection_getMethod(i_connection)==NyLPC_THttpMethodType_HEAD)); - switch(i_req->content.setup.tmp.cval){ - case QVAL_C_GET: - if(!NyLPC_cHttpdUtils_sendJsonHeader(i_connection)){ - NyLPC_OnErrorGoto(Error); - } - if(NyLPC_cHttpdConnection_getMethod(i_connection)==NyLPC_THttpMethodType_GET){ - config=NyLPC_cMiMicConfiglation_loadFromFlash(); - //Flashの内容から - if(!NyLPC_cHttpdConnection_sendResponseBodyF(i_connection, - "{" - "\"application\":\""MOD_VERSION";%s;%s(%s)\"," - "\"landev\":\"%s\",", - NyLPC_cMiMicEnv_getStrProperty(NyLPC_cMiMicEnv_VERSION), - NyLPC_cMiMicEnv_getStrProperty(NyLPC_cMiMicEnv_SHORT_NAME), - NyLPC_cMiMicEnv_getStrProperty(NyLPC_cMiMicEnv_MCU_NAME), - NyLPC_cMiMicEnv_getStrProperty(NyLPC_cMiMicEnv_ETHERNET_PHY) - )) - { - NyLPC_OnErrorGoto(Error); - } - if(!NyLPC_cHttpdConnection_sendResponseBodyF(i_connection, - "\"cfg\":{" - "\"mac00010203\":%u," - "\"mac0405xxxx\":%u," - "\"host\":\"%s\"," - "\"ipv4\":{" - "\"flags\":%u," - "\"ip\":%u," - "\"mask\":%u," - "\"droute\":%u," - "}," - "\"services\":{" - "\"flags\":%u," - "\"http_port\":%u" - "}},", - config->mac_00_01_02_03, - config->mac_04_05_xx_xx, - config->hostname, - config->ipv4_flags, - config->ipv4_addr_net, - config->ipv4_mask_net, - config->ipv4_drut_net, - config->srv_flags, - config->http_port - )){ - NyLPC_OnErrorGoto(Error); - } - //write current status - currebt_cfg=(const NyLPC_TcNetConfig_t*)NyLPC_cUipService_refCurrentConfig(); - if(!NyLPC_cHttpdConnection_sendResponseBodyF(i_connection, - "\"cur\":{" - "\"mac00010203\":%u," - "\"mac0405xxxx\":%u," - "\"host\":\"%s\"," - "\"ipv4\":{" - "\"flags\":%u," - "\"ip\":%u," - "\"mask\":%u," - "\"droute\":%u," - "}," - "\"services\":{" - "\"flags\":%u," - "\"http_port\":%u" - "}}}", - (currebt_cfg->super.eth_mac.addr[0]<<24)|(currebt_cfg->super.eth_mac.addr[1]<<16)|(currebt_cfg->super.eth_mac.addr[2]<<8)|currebt_cfg->super.eth_mac.addr[3], - (currebt_cfg->super.eth_mac.addr[4]<<24)|(currebt_cfg->super.eth_mac.addr[5]<<16), - currebt_cfg->hostname, - currebt_cfg->tcp_mode, - NyLPC_ntohl(currebt_cfg->super.ip_addr.v), - NyLPC_ntohl(currebt_cfg->super.netmask.v), - NyLPC_ntohl(currebt_cfg->super.dr_addr.v), - currebt_cfg->services.flags, - currebt_cfg->services.http_port - )){ - NyLPC_OnErrorGoto(Error); - } - } - break; - case QVAL_C_UPDATE: - //check parameter length - if(i_req->content.setup.tmp.param_len!=SIZE_OF_SETUP_PARAM || i_req->content.setup.tmp.host_len<1) - { - NyLPC_cHttpdUtils_sendErrorResponse(i_connection,500); - }else{ - //パラメータ→ROMイメージ変換 - i_req->content.setup.memimg.fast_boot=0xffffffff; -// ここの部分は受信時にデータ位置を合わせてあるのでコピー不要。 -// cfg_image.mac_00_01_02_03=(i_req->content.setup.param_buf[0]); -// cfg_image.mac_04_05_xx_xx=(i_req->content.setup.param_buf[1]&0xffff0000); -// cfg_image.ipv4_flags =i_req->content.setup.param_buf[2]; -// cfg_image.ipv4_addr_net =i_req->content.setup.param_buf[3]; -// cfg_image.ipv4_mask_net =i_req->content.setup.param_buf[4]; -// cfg_image.ipv4_drut_net =i_req->content.setup.param_buf[5]; -// cfg_image.srv_flags =i_req->content.setup.param_buf[6]; -// strcpy(cfg_image.hostname,i_req->content.setup.host_name); - i_req->content.setup.memimg.http_port =(NyLPC_TUInt16)(i_req->content.setup.tmp.param_buf[7]>>16); - i_req->content.setup.memimg.padding=0xffff; - //一応確認。 - if(i_req->content.setup.memimg.http_port==0){ - NyLPC_cHttpdUtils_sendErrorResponse(i_connection,500); - }else{ - //FreeRTOSの停止 - NyLPC_cIsr_enterCritical(); - //Flashへの書き込み - ret=NyLPC_cMiMicConfiglation_updateConfigulation(&i_req->content.setup.memimg); - //FreeRTOSの復帰 - NyLPC_cIsr_exitCritical(); - if(!ret){ - NyLPC_cHttpdUtils_sendErrorResponse(i_connection,500); - }else{ - if(!NyLPC_cHttpdUtils_sendJsonHeader(i_connection)){ - NyLPC_OnErrorGoto(Error); - } - if(NyLPC_cHttpdConnection_getMethod(i_connection)==NyLPC_THttpMethodType_GET){ - NyLPC_cHttpdConnection_sendResponseBodyF(i_connection, - "{\"application\":\""MOD_VERSION"\",\"result\":%u}", - ret?0x00000000:0x80000000); - } - } - } - } - //JSONを書く。 - break; - default: - NyLPC_cHttpdUtils_sendErrorResponse(i_connection,400); - NyLPC_OnErrorGoto(Error); - break; - } - return; + switch(i_req->content.setup.tmp.cval){ + case QVAL_C_GET: + if(!NyLPC_cHttpdUtils_sendJsonHeader(i_connection)){ + NyLPC_OnErrorGoto(Error); + } + if(NyLPC_cHttpdConnection_getMethod(i_connection)==NyLPC_THttpMethodType_GET){ + config=NyLPC_cMiMicConfiglation_loadFromFlash(); + //Flashの内容から + if(!NyLPC_cHttpdConnection_sendResponseBodyF(i_connection, + "{" + "\"application\":\""MOD_VERSION";%s;%s(%s)\"," + "\"landev\":\"%s\",", + NyLPC_cMiMicEnv_getStrProperty(NyLPC_cMiMicEnv_VERSION), + NyLPC_cMiMicEnv_getStrProperty(NyLPC_cMiMicEnv_SHORT_NAME), + NyLPC_cMiMicEnv_getStrProperty(NyLPC_cMiMicEnv_MCU_NAME), + NyLPC_cMiMicEnv_getStrProperty(NyLPC_cMiMicEnv_ETHERNET_PHY) + )) + { + NyLPC_OnErrorGoto(Error); + } + if(!NyLPC_cHttpdConnection_sendResponseBodyF(i_connection, + "\"cfg\":{" + "\"mac00010203\":%u," + "\"mac0405xxxx\":%u," + "\"host\":\"%s\"," + "\"ipv4\":{" + "\"flags\":%u," + "\"ip\":%u," + "\"mask\":%u," + "\"droute\":%u," + "}," + "\"services\":{" + "\"flags\":%u," + "\"http_port\":%u" + "}},", + config->mac_00_01_02_03, + config->mac_04_05_xx_xx, + config->hostname, + config->ipv4_flags, + config->ipv4_addr_net, + config->ipv4_mask_net, + config->ipv4_drut_net, + config->srv_flags, + config->http_port + )){ + NyLPC_OnErrorGoto(Error); + } + //write current status + netif_info=NyLPC_cNet_getInterfaceInfo(); + currebt_cfg=(const NyLPC_TcNetConfig_t*)(netif_info->current_config); + if(!NyLPC_cHttpdConnection_sendResponseBodyF(i_connection, + "\"cur\":{" + "\"mac00010203\":%u," + "\"mac0405xxxx\":%u," + "\"host\":\"%s\"," + "\"ipv4\":{" + "\"flags\":%u," + "\"ip\":%u," + "\"mask\":%u," + "\"droute\":%u," + "}," + "\"services\":{" + "\"flags\":%u," + "\"http_port\":%u" + "}}}", + (currebt_cfg->super.eth_mac.addr[0]<<24)|(currebt_cfg->super.eth_mac.addr[1]<<16)|(currebt_cfg->super.eth_mac.addr[2]<<8)|currebt_cfg->super.eth_mac.addr[3], + (currebt_cfg->super.eth_mac.addr[4]<<24)|(currebt_cfg->super.eth_mac.addr[5]<<16), + currebt_cfg->hostname, + currebt_cfg->tcp_mode, + NyLPC_ntohl(currebt_cfg->super.ip_addr.v), + NyLPC_ntohl(currebt_cfg->super.netmask.v), + NyLPC_ntohl(currebt_cfg->super.dr_addr.v), + currebt_cfg->services.flags, + currebt_cfg->services.http_port + )){ + NyLPC_OnErrorGoto(Error); + } + } + break; + case QVAL_C_UPDATE: + //check parameter length + if(i_req->content.setup.tmp.param_len!=SIZE_OF_SETUP_PARAM || i_req->content.setup.tmp.host_len<1) + { + NyLPC_cHttpdUtils_sendErrorResponse(i_connection,500); + }else{ + //パラメータ→ROMイメージ変換 + i_req->content.setup.memimg.fast_boot=0xffffffff; +// ここの部分は受信時にデータ位置を合わせてあるのでコピー不要。 +// cfg_image.mac_00_01_02_03=(i_req->content.setup.param_buf[0]); +// cfg_image.mac_04_05_xx_xx=(i_req->content.setup.param_buf[1]&0xffff0000); +// cfg_image.ipv4_flags =i_req->content.setup.param_buf[2]; +// cfg_image.ipv4_addr_net =i_req->content.setup.param_buf[3]; +// cfg_image.ipv4_mask_net =i_req->content.setup.param_buf[4]; +// cfg_image.ipv4_drut_net =i_req->content.setup.param_buf[5]; +// cfg_image.srv_flags =i_req->content.setup.param_buf[6]; +// strcpy(cfg_image.hostname,i_req->content.setup.host_name); + i_req->content.setup.memimg.http_port =(NyLPC_TUInt16)(i_req->content.setup.tmp.param_buf[7]>>16); + i_req->content.setup.memimg.padding32=0xffff; + i_req->content.setup.memimg.padding64=0xffffffff; + //一応確認。 + if(i_req->content.setup.memimg.http_port==0){ + NyLPC_cHttpdUtils_sendErrorResponse(i_connection,500); + }else{ + //FreeRTOSの停止 + NyLPC_cIsr_enterCritical(); + //Flashへの書き込み + ret=NyLPC_cMiMicConfiglation_updateConfigulation(&i_req->content.setup.memimg); + //FreeRTOSの復帰 + NyLPC_cIsr_exitCritical(); + if(!ret){ + NyLPC_cHttpdUtils_sendErrorResponse(i_connection,500); + }else{ + if(!NyLPC_cHttpdUtils_sendJsonHeader(i_connection)){ + NyLPC_OnErrorGoto(Error); + } + if(NyLPC_cHttpdConnection_getMethod(i_connection)==NyLPC_THttpMethodType_GET){ + NyLPC_cHttpdConnection_sendResponseBodyF(i_connection, + "{\"application\":\""MOD_VERSION"\",\"result\":%u}", + ret?0x00000000:0x80000000); + } + } + } + } + //JSONを書く。 + break; + default: + NyLPC_cHttpdUtils_sendErrorResponse(i_connection,400); + NyLPC_OnErrorGoto(Error); + break; + } + return; Error: - return; + return; } diff --git a/lib/src/net/httpd/mod/NyLPC_cModMiMicSetting.h b/lib/src/net/httpd/mod/NyLPC_cModMiMicSetting.h index 54b2bfb..f782086 100644 --- a/lib/src/net/httpd/mod/NyLPC_cModMiMicSetting.h +++ b/lib/src/net/httpd/mod/NyLPC_cModMiMicSetting.h @@ -19,8 +19,8 @@ * along with this program. If not, see . * * For further information please contact. - * http://nyatla.jp/ - * or + * http://nyatla.jp/ + * or * *********************************************************************************/ #ifndef NYLPC_CMODMIMICSETTING_H_ @@ -41,35 +41,35 @@ extern "C" { * MiMicの動作設定と取得を行います。 * c=get 現在の状態をjson形式で返却する。 * { - * application: [:string:], - * mac00010203: [:HEX32:], - * mac0405xxxx: [:HEX32:], - * ip: [:HEX32:], - * mask: [:HEX32:], - * droute: [:HEX32:], + * application: [:string:], + * mac00010203: [:HEX32:], + * mac0405xxxx: [:HEX32:], + * ip: [:HEX32:], + * mask: [:HEX32:], + * droute: [:HEX32:], * - * port: [:HEX16:], - * access\":%u}", + * port: [:HEX16:], + * access\":%u}", * c=update pパラメタ/hostの内容でFlashをアップデートする。 - * pパラメタは32bitの16進数文字列。 - * [ 0] emac_0123 ビックエンディアン48bit値+パディング16bit - * [ 1] emac_45xx : - * [emac4][emac5][x][x] - * [ 2] ipv4_flags IPV4設定フラグ - * [ 3] ipv4_ip IPアドレス。32bit値。ビックエンディアン - * [ 4] ipv4_mask サブネットマスク。32bit値。ビックエンディアン - * [ 5] ipv4_deoute defaultrootアドレス。32bit値。ビックエンディアン - * [ 6] service_flag - * See NyLPC_cNetConfig.h - * [ 7] http_param HTTPサービスポート番号。2桁のHEX値である。ビックエンディアン。値16bit、パディング16bit - * [port_h][port_l][x][x] + * pパラメタは32bitの16進数文字列。 + * [ 0] emac_0123 ビックエンディアン48bit値+パディング16bit + * [ 1] emac_45xx : + * [emac4][emac5][x][x] + * [ 2] ipv4_flags IPV4設定フラグ + * [ 3] ipv4_ip IPアドレス。32bit値。ビックエンディアン + * [ 4] ipv4_mask サブネットマスク。32bit値。ビックエンディアン + * [ 5] ipv4_deoute defaultrootアドレス。32bit値。ビックエンディアン + * [ 6] service_flag + * See NyLPC_cNetConfig.h + * [ 7] http_param HTTPサービスポート番号。2桁のHEX値である。ビックエンディアン。値16bit、パディング16bit + * [port_h][port_l][x][x] */ typedef struct NyLPC_TcModMiMicSetting NyLPC_TcModMiMicSetting_t; struct NyLPC_TcModMiMicSetting { - NyLPC_TcModRomFiles_t super; + NyLPC_TcModRomFiles_t super; }; /** diff --git a/lib/src/net/httpd/mod/NyLPC_cModRemoteMcu.c b/lib/src/net/httpd/mod/NyLPC_cModRemoteMcu.c index efa7256..470d0c7 100644 --- a/lib/src/net/httpd/mod/NyLPC_cModRemoteMcu.c +++ b/lib/src/net/httpd/mod/NyLPC_cModRemoteMcu.c @@ -19,8 +19,8 @@ * along with this program. If not, see . * * For further information please contact. - * http://nyatla.jp/ - * or + * http://nyatla.jp/ + * or * *********************************************************************************/ #include "NyLPC_cModRemoteMcu.h" @@ -35,44 +35,44 @@ #define SIZE_OF_IBUF 256 struct TModMiMicRemoteMcuHeader { - struct NyLPC_THttpBasicHeader super; - NyLPC_TUInt8 _content_id; - //解析用 - NyLPC_TUInt8 _qery_name_id; - NyLPC_TUInt8 _astate; - NyLPC_TInt16 _prefix_len; - NyLPC_TcStr_t _tstr; - NyLPC_TChar _tstr_buf[16]; - struct NyLPC_TUInt32ArrayPtr _binarray; - /** 文字列のパーサ*/ - NyLPC_TcMiMicDbCompiler_t _binparser; - NyLPC_TcMiMicTxtCompiler_t _txtcmp; - union{ - struct{ - NyLPC_TUInt8 v;//バージョン - NyLPC_TUInt8 o;//outputスタイル - /** - * il_bufはbcとdbの2パートのデータを格納します。 - * 先頭からbc_lenの長さのBCパートと、db_partからdb_lenの長さのデータです。 - */ - struct{ - /** MiMicVMインストラクションの蓄積用。前半にTXT,後半にDBを格納する。 */ - NyLPC_TUInt32 bc_buf[SIZE_OF_IBUF]; - /** MiMicVM入力ストリーム(MimicDB)の開始位置(bufの一部を指す) */ - const NyLPC_TUInt32* db_part; - /** MiMicTXTのワード長(1ワード32bit)*/ - NyLPC_TUInt16 txt_len; - /** MiMicDBのワード長(1ワード32bit)*/ - NyLPC_TUInt16 db_len; - }vm_instruction; - }mvm; - struct{ - /** - * 不明な名前の場合は、ここに名前をコピー - */ - NyLPC_TChar path[32]; - }unknown; - }content; + struct NyLPC_THttpBasicHeader super; + NyLPC_TUInt8 _content_id; + //解析用 + NyLPC_TUInt8 _qery_name_id; + NyLPC_TUInt8 _astate; + NyLPC_TInt16 _prefix_len; + NyLPC_TcStr_t _tstr; + NyLPC_TChar _tstr_buf[16]; + struct NyLPC_TUInt32ArrayPtr _binarray; + /** 文字列のパーサ*/ + NyLPC_TcMiMicDbCompiler_t _binparser; + NyLPC_TcMiMicTxtCompiler_t _txtcmp; + union{ + struct{ + NyLPC_TUInt8 v;//バージョン + NyLPC_TUInt8 o;//outputスタイル + /** + * il_bufはbcとdbの2パートのデータを格納します。 + * 先頭からbc_lenの長さのBCパートと、db_partからdb_lenの長さのデータです。 + */ + struct{ + /** MiMicVMインストラクションの蓄積用。前半にTXT,後半にDBを格納する。 */ + NyLPC_TUInt32 bc_buf[SIZE_OF_IBUF]; + /** MiMicVM入力ストリーム(MimicDB)の開始位置(bufの一部を指す) */ + const NyLPC_TUInt32* db_part; + /** MiMicTXTのワード長(1ワード32bit)*/ + NyLPC_TUInt16 txt_len; + /** MiMicDBのワード長(1ワード32bit)*/ + NyLPC_TUInt16 db_len; + }vm_instruction; + }mvm; + struct{ + /** + * 不明な名前の場合は、ここに名前をコピー + */ + NyLPC_TChar path[32]; + }unknown; + }content; }; @@ -84,7 +84,7 @@ static void status(NyLPC_TcHttpdConnection_t* i_connection); #define ST_PARSE_PATH 1 #define ST_PARSE_QUERY_NAME 2 -#define ST_PARSE_QUERY_VALUE 3 //Query読み出し中 +#define ST_PARSE_QUERY_VALUE 3 //Query読み出し中 #define ST_PARSE_QUERY_VALUE_V 4 #define ST_PARSE_QUERY_VALUE_O 5 #define ST_PARSE_QUERY_VALUE_BC 6 @@ -97,214 +97,214 @@ static void status(NyLPC_TcHttpdConnection_t* i_connection); #define CONTENT_ID_UNKNOWN 0 -#define QNAME_ID_V 1 -#define QNAME_ID_O 2 -#define QNAME_ID_BC 3 +#define QNAME_ID_V 1 +#define QNAME_ID_O 2 +#define QNAME_ID_BC 3 #define QNAME_ID_UNKNOWN 0 /** * TRemoteMcuRequest.content.mvm.oの値 */ -#define QVAL_O_UNKNOWN 0 //default +#define QVAL_O_UNKNOWN 0 //default #define QVAL_O_XML 1 #define QVAL_O_JSON 2 -#define QVAL_V_UNKNOWN 0 -#define QVAL_V_1 1 +#define QVAL_V_UNKNOWN 0 +#define QVAL_V_1 1 static const struct NyLPC_TTextIdTbl url_tbl[]= { - {"mvm.api",CONTENT_ID_MVM}, - {"status.api",CONTENT_ID_STATUS}, - {NULL,CONTENT_ID_UNKNOWN} + {"mvm.api",CONTENT_ID_MVM}, + {"status.api",CONTENT_ID_STATUS}, + {NULL,CONTENT_ID_UNKNOWN} }; static const struct NyLPC_TTextIdTbl qname_id_table[]= { - {"o",QNAME_ID_O}, - {"bc",QNAME_ID_BC}, - {"v",QNAME_ID_V}, - {NULL,QNAME_ID_UNKNOWN} + {"o",QNAME_ID_O}, + {"bc",QNAME_ID_BC}, + {"v",QNAME_ID_V}, + {NULL,QNAME_ID_UNKNOWN} }; static NyLPC_TBool urlHandler(NyLPC_TcHttpBasicHeaderParser_t* i_inst,NyLPC_TChar i_c,struct NyLPC_THttpBasicHeader* o_out) { - NyLPC_TUInt16 ol; - struct TModMiMicRemoteMcuHeader* out=(struct TModMiMicRemoteMcuHeader*)o_out; - //読み飛ばし - if(out->_prefix_len<0){ - out->_prefix_len++; - return NyLPC_TBool_TRUE;//読み飛ばし - } - if(out->_astate==ST_PARSE_PATH){ - if(i_c!='\0' && i_c!='?'){ - if(!NyLPC_cStr_put(&(out->_tstr),i_c)){ - //ERROR - NyLPC_OnErrorGoto(ERROR); - } - }else{ - out->_content_id=NyLPC_TTextIdTbl_getMatchId(NyLPC_cStr_str(&(out->_tstr)),url_tbl); - switch(out->_content_id) - { - case CONTENT_ID_MVM: - out->content.mvm.vm_instruction.txt_len=0; - out->content.mvm.vm_instruction.db_len=0; - out->content.mvm.vm_instruction.db_part=NULL; - NyLPC_TUInt32ArrayPtr_setBuf(&out->_binarray,out->content.mvm.vm_instruction.bc_buf,SIZE_OF_IBUF); - out->content.mvm.o=QVAL_O_JSON; - out->content.mvm.v=QVAL_V_UNKNOWN; - break; - default: - break; - } - NyLPC_cStr_clear(&(out->_tstr)); - out->_astate=ST_PARSE_QUERY_NAME;//クエリ名解析へ - } - return NyLPC_TBool_TRUE; - } - switch(out->_content_id) - { - case CONTENT_ID_MVM: - switch(out->_astate){ - case ST_PARSE_QUERY_NAME: - if(i_c!='\0' && i_c!='&' && i_c!='='){ - if(!NyLPC_cStr_put(&(out->_tstr),i_c)){ - NyLPC_OnErrorGoto(ERROR); - } - }else{ - //Query確定。 - out->_qery_name_id=NyLPC_TTextIdTbl_getMatchId(NyLPC_cStr_str(&(out->_tstr)),qname_id_table); - NyLPC_cStr_clear(&(out->_tstr)); - //クエリ値がある場合 - switch(out->_qery_name_id){ - case QNAME_ID_O: - out->_astate=ST_PARSE_QUERY_VALUE_O;//MIMICBCのDBパラメータパーサを借用。 - break; - case QNAME_ID_V: - out->_astate=ST_PARSE_QUERY_VALUE_V; - break; - case QNAME_ID_BC: - out->_astate=ST_PARSE_QUERY_VALUE_BC; - break; - default: - out->_astate=ST_PARSE_QUERY_VALUE; - break; - } - } - return NyLPC_TBool_TRUE; - case ST_PARSE_QUERY_VALUE: - //未知のクエリは無視 - if(i_c!='\0' && i_c!='&'){ - }else{ - //クエリ値解析完了 - out->_astate=ST_PARSE_QUERY_NAME; - } - return NyLPC_TBool_TRUE; - case ST_PARSE_QUERY_VALUE_O: - if(i_c!='\0' && i_c!='&'){ - if(!NyLPC_cStr_put(&(out->_tstr),i_c)){ - NyLPC_OnErrorGoto(ERROR); - } - }else{ - if(NyLPC_cStr_isEqual(&(out->_tstr),"j")){ - out->content.mvm.o=QVAL_O_JSON; - }else if(NyLPC_cStr_isEqual(&(out->_tstr),"x")){ - out->content.mvm.o=QVAL_O_XML; - } - out->_astate=ST_PARSE_QUERY_NAME; - NyLPC_cStr_clear(&(out->_tstr)); - } - return NyLPC_TBool_TRUE; - case ST_PARSE_QUERY_VALUE_V: - if(i_c!='\0' && i_c!='&'){ - if(!NyLPC_cStr_put(&(out->_tstr),i_c)){ - NyLPC_OnErrorGoto(ERROR); - } - }else{ - if(NyLPC_cStr_isEqual(&(out->_tstr),"1")){ - out->content.mvm.v=QVAL_V_1; - } - out->_astate=ST_PARSE_QUERY_NAME; - NyLPC_cStr_clear(&(out->_tstr)); - } - return NyLPC_TBool_TRUE; - case ST_PARSE_QUERY_VALUE_BC: - if(i_c!='\0' && i_c!='&'){ - //コンパイル - switch(NyLPC_cMiMicTxtCompiler_compileFragment2(&(out->_txtcmp),i_c,&(out->_binarray),&ol)) - { - case NyLPC_TcMiMicTxtCompiler_RET_OK: - //命令確定。 - break; - case NyLPC_TcMiMicTxtCompiler_RET_OK_END: - //命令終端検出->モード切替 - out->content.mvm.vm_instruction.txt_len=SIZE_OF_IBUF-out->_binarray.len+ol; - out->content.mvm.vm_instruction.db_part=out->content.mvm.vm_instruction.bc_buf+out->content.mvm.vm_instruction.txt_len; - out->_astate=ST_PARSE_QUERY_VALUE_DB; - break; - case NyLPC_TcMiMicTxtCompiler_RET_CONTINUE: - //何もしない - break; - case NyLPC_TcMiMicTxtCompiler_RET_NG: - default: - //ERROR - NyLPC_OnErrorGoto(ERROR); - } - } - return NyLPC_TBool_TRUE; -// //フラグメント終端が検出できない終了はエラー -// NyLPC_OnErrorGoto(ERROR); - case ST_PARSE_QUERY_VALUE_DB: - if(i_c!='\0' && i_c!='&'){ - switch(NyLPC_cMiMicDbCompiler_compileFragment2(&(out->_binparser),i_c,out->_binarray.ptr)) - { - case NyLPC_TcMiMicDbCompiler_RET_CONTINUE: - break; - case NyLPC_TcMiMicDbCompiler_RET_OK: - // - if(!NyLPC_TUInt32ArrayPtr_seek(&(out->_binarray),1)){ - //ERROR - NyLPC_OnErrorGoto(ERROR); - } - break; - case NyLPC_TcMiMicDbCompiler_RET_ERROR: - default: - //ERROR - NyLPC_OnErrorGoto(ERROR); - } - }else{ - //区切りのいいところで終わってる? - if(NyLPC_cMiMicDbCompiler_hasFragment(&(out->_binparser))){ - //ERROR - NyLPC_OnErrorGoto(ERROR); - } - out->content.mvm.vm_instruction.db_len=((NyLPC_TUInt8*)(out->_binarray.ptr)-(NyLPC_TUInt8*)(out->content.mvm.vm_instruction.db_part))/sizeof(NyLPC_TUInt32); - - //終端しているなら、次のクエリへ - out->_astate=ST_PARSE_QUERY_NAME; - } - return NyLPC_TBool_TRUE; - default: - break; - } - NyLPC_OnErrorGoto(ERROR); - default: - NyLPC_OnErrorGoto(ERROR); - } - return NyLPC_TBool_TRUE; + NyLPC_TUInt16 ol; + struct TModMiMicRemoteMcuHeader* out=(struct TModMiMicRemoteMcuHeader*)o_out; + //読み飛ばし + if(out->_prefix_len<0){ + out->_prefix_len++; + return NyLPC_TBool_TRUE;//読み飛ばし + } + if(out->_astate==ST_PARSE_PATH){ + if(i_c!='\0' && i_c!='?'){ + if(!NyLPC_cStr_put(&(out->_tstr),i_c)){ + //ERROR + NyLPC_OnErrorGoto(ERROR); + } + }else{ + out->_content_id=NyLPC_TTextIdTbl_getMatchId(NyLPC_cStr_str(&(out->_tstr)),url_tbl); + switch(out->_content_id) + { + case CONTENT_ID_MVM: + out->content.mvm.vm_instruction.txt_len=0; + out->content.mvm.vm_instruction.db_len=0; + out->content.mvm.vm_instruction.db_part=NULL; + NyLPC_TUInt32ArrayPtr_setBuf(&out->_binarray,out->content.mvm.vm_instruction.bc_buf,SIZE_OF_IBUF); + out->content.mvm.o=QVAL_O_JSON; + out->content.mvm.v=QVAL_V_UNKNOWN; + break; + default: + break; + } + NyLPC_cStr_clear(&(out->_tstr)); + out->_astate=ST_PARSE_QUERY_NAME;//クエリ名解析へ + } + return NyLPC_TBool_TRUE; + } + switch(out->_content_id) + { + case CONTENT_ID_MVM: + switch(out->_astate){ + case ST_PARSE_QUERY_NAME: + if(i_c!='\0' && i_c!='&' && i_c!='='){ + if(!NyLPC_cStr_put(&(out->_tstr),i_c)){ + NyLPC_OnErrorGoto(ERROR); + } + }else{ + //Query確定。 + out->_qery_name_id=NyLPC_TTextIdTbl_getMatchId(NyLPC_cStr_str(&(out->_tstr)),qname_id_table); + NyLPC_cStr_clear(&(out->_tstr)); + //クエリ値がある場合 + switch(out->_qery_name_id){ + case QNAME_ID_O: + out->_astate=ST_PARSE_QUERY_VALUE_O;//MIMICBCのDBパラメータパーサを借用。 + break; + case QNAME_ID_V: + out->_astate=ST_PARSE_QUERY_VALUE_V; + break; + case QNAME_ID_BC: + out->_astate=ST_PARSE_QUERY_VALUE_BC; + break; + default: + out->_astate=ST_PARSE_QUERY_VALUE; + break; + } + } + return NyLPC_TBool_TRUE; + case ST_PARSE_QUERY_VALUE: + //未知のクエリは無視 + if(i_c!='\0' && i_c!='&'){ + }else{ + //クエリ値解析完了 + out->_astate=ST_PARSE_QUERY_NAME; + } + return NyLPC_TBool_TRUE; + case ST_PARSE_QUERY_VALUE_O: + if(i_c!='\0' && i_c!='&'){ + if(!NyLPC_cStr_put(&(out->_tstr),i_c)){ + NyLPC_OnErrorGoto(ERROR); + } + }else{ + if(NyLPC_cStr_isEqual(&(out->_tstr),"j")){ + out->content.mvm.o=QVAL_O_JSON; + }else if(NyLPC_cStr_isEqual(&(out->_tstr),"x")){ + out->content.mvm.o=QVAL_O_XML; + } + out->_astate=ST_PARSE_QUERY_NAME; + NyLPC_cStr_clear(&(out->_tstr)); + } + return NyLPC_TBool_TRUE; + case ST_PARSE_QUERY_VALUE_V: + if(i_c!='\0' && i_c!='&'){ + if(!NyLPC_cStr_put(&(out->_tstr),i_c)){ + NyLPC_OnErrorGoto(ERROR); + } + }else{ + if(NyLPC_cStr_isEqual(&(out->_tstr),"1")){ + out->content.mvm.v=QVAL_V_1; + } + out->_astate=ST_PARSE_QUERY_NAME; + NyLPC_cStr_clear(&(out->_tstr)); + } + return NyLPC_TBool_TRUE; + case ST_PARSE_QUERY_VALUE_BC: + if(i_c!='\0' && i_c!='&'){ + //コンパイル + switch(NyLPC_cMiMicTxtCompiler_compileFragment2(&(out->_txtcmp),i_c,&(out->_binarray),&ol)) + { + case NyLPC_TcMiMicTxtCompiler_RET_OK: + //命令確定。 + break; + case NyLPC_TcMiMicTxtCompiler_RET_OK_END: + //命令終端検出->モード切替 + out->content.mvm.vm_instruction.txt_len=SIZE_OF_IBUF-out->_binarray.len+ol; + out->content.mvm.vm_instruction.db_part=out->content.mvm.vm_instruction.bc_buf+out->content.mvm.vm_instruction.txt_len; + out->_astate=ST_PARSE_QUERY_VALUE_DB; + break; + case NyLPC_TcMiMicTxtCompiler_RET_CONTINUE: + //何もしない + break; + case NyLPC_TcMiMicTxtCompiler_RET_NG: + default: + //ERROR + NyLPC_OnErrorGoto(ERROR); + } + } + return NyLPC_TBool_TRUE; +// //フラグメント終端が検出できない終了はエラー +// NyLPC_OnErrorGoto(ERROR); + case ST_PARSE_QUERY_VALUE_DB: + if(i_c!='\0' && i_c!='&'){ + switch(NyLPC_cMiMicDbCompiler_compileFragment2(&(out->_binparser),i_c,out->_binarray.ptr)) + { + case NyLPC_TcMiMicDbCompiler_RET_CONTINUE: + break; + case NyLPC_TcMiMicDbCompiler_RET_OK: + // + if(!NyLPC_TUInt32ArrayPtr_seek(&(out->_binarray),1)){ + //ERROR + NyLPC_OnErrorGoto(ERROR); + } + break; + case NyLPC_TcMiMicDbCompiler_RET_ERROR: + default: + //ERROR + NyLPC_OnErrorGoto(ERROR); + } + }else{ + //区切りのいいところで終わってる? + if(NyLPC_cMiMicDbCompiler_hasFragment(&(out->_binparser))){ + //ERROR + NyLPC_OnErrorGoto(ERROR); + } + out->content.mvm.vm_instruction.db_len=((NyLPC_TUInt8*)(out->_binarray.ptr)-(NyLPC_TUInt8*)(out->content.mvm.vm_instruction.db_part))/sizeof(NyLPC_TUInt32); + + //終端しているなら、次のクエリへ + out->_astate=ST_PARSE_QUERY_NAME; + } + return NyLPC_TBool_TRUE; + default: + break; + } + NyLPC_OnErrorGoto(ERROR); + default: + NyLPC_OnErrorGoto(ERROR); + } + return NyLPC_TBool_TRUE; ERROR: - return NyLPC_TBool_FALSE; + return NyLPC_TBool_FALSE; } /** * デフォルトハンドラ */ static const struct NyLPC_TcHttpBasicHeaderParser_Handler handler= { - NULL, - urlHandler + NULL, + urlHandler }; @@ -313,18 +313,18 @@ static const struct NyLPC_TcHttpBasicHeaderParser_Handler handler= */ void NyLPC_cModRemoteMcu_initialize(NyLPC_TcModRemoteMcu_t* i_inst,const NyLPC_TChar* i_ref_root_path) { - NyLPC_cModRomFiles_initialize(&i_inst->super,i_ref_root_path,NULL,0); + NyLPC_cModRomFiles_initialize(&i_inst->super,i_ref_root_path,NULL,0); } void NyLPC_cModRemoteMcu_finalize(NyLPC_TcModRemoteMcu_t* i_inst) { - NyLPC_cModRomFiles_finalize(&i_inst->super); + NyLPC_cModRomFiles_finalize(&i_inst->super); } /** * モジュールがコネクションをハンドリングできるかを返します。 */ NyLPC_TBool NyLPC_cModRemoteMcu_canHandle(NyLPC_TcModRemoteMcu_t* i_inst,NyLPC_TcHttpdConnection_t* i_connection) { - return NyLPC_cModRomFiles_canHandle(&i_inst->super,i_connection); + return NyLPC_cModRomFiles_canHandle(&i_inst->super,i_connection); } static struct TModMiMicRemoteMcuHeader single_header; @@ -334,78 +334,78 @@ static struct TModMiMicRemoteMcuHeader single_header; */ NyLPC_TBool NyLPC_cModRemoteMcu_execute(NyLPC_TcModRemoteMcu_t* i_inst,NyLPC_TcHttpdConnection_t* i_connection) { - NyLPC_TcHttpBasicHeaderParser_t parser; - NyLPC_TUInt8 method_type; - //リクエストParse済へ遷移(この関数の後はModが責任を持ってリクエストを返却) - NyLPC_cHttpdConnection_setReqStatusParsed(i_connection); - - - //VM起動の為の排他ロック - NyLPC_cHttpdConnection_lock(i_connection); - - - //URL解析の準備 - single_header._prefix_len=-((NyLPC_TInt16)strlen(i_inst->super._ref_root_path)+2); - single_header._astate=ST_PARSE_PATH; - NyLPC_cStr_initialize(&single_header._tstr,single_header._tstr_buf,16); - NyLPC_cMiMicDbCompiler_initialize(&single_header._binparser); - NyLPC_cMiMicTxtCompiler_initialize(&single_header._txtcmp); - - NyLPC_cHttpBasicHeaderParser_initialize(&parser,&handler); - - - //プリフェッチしたデータを流す - NyLPC_cHttpBasicHeaderParser_parseInit(&parser,&(single_header.super)); - NyLPC_cHttpdConnection_pushPrefetchInfo(i_connection,&parser,&single_header.super); - //後続をストリームから取り込む - if(!NyLPC_cHttpBasicHeaderParser_parseStream(&parser,NyLPC_cHttpdConnection_refStream(i_connection),&(single_header.super))){ - NyLPC_cHttpdUtils_sendErrorResponse(i_connection,500); - NyLPC_OnErrorGoto(Error1); - } - if(!NyLPC_cHttpBasicHeaderParser_parseFinish(&parser,&(single_header.super))){ - NyLPC_cHttpdUtils_sendErrorResponse(i_connection,500); - NyLPC_OnErrorGoto(Error1); - } - //GETかHEADに制限 - method_type=NyLPC_cHttpdConnection_getMethod(i_connection); - if(method_type!=NyLPC_THttpMethodType_GET && method_type!=NyLPC_THttpMethodType_HEAD) - { - NyLPC_cHttpdUtils_sendErrorResponse(i_connection,405); - NyLPC_OnErrorGoto(Error1); - } - //Request::ConnectionがClose設定,又はHTTP1.1では無い場合,CLOSE - if(single_header.super.connection==NyLPC_THttpMessgeHeader_Connection_CLOSE || single_header.super.startline.req.version!=NyLPC_THttpVersion_11) - { - NyLPC_cHttpdConnection_setConnectionMode(i_connection,NyLPC_TcHttpdConnection_CONNECTION_MODE_CLOSE); - } - //CGIの実行 - switch(single_header._content_id) - { - case CONTENT_ID_MVM: - mvm(i_connection,&single_header); - break; - case CONTENT_ID_STATUS: - status(i_connection); - break; - default: - NyLPC_cHttpdUtils_sendErrorResponse(i_connection,400); - NyLPC_OnErrorGoto(Error1); - } - NyLPC_cStr_finalize(&single_header._tstr); - NyLPC_cMiMicDbCompiler_finalize(&single_header._binparser); - NyLPC_cMiMicTxtCompiler_finalize(&single_header._txtcmp); - NyLPC_cHttpBasicHeaderParser_finalize(&parser); + NyLPC_TcHttpBasicHeaderParser_t parser; + NyLPC_TUInt8 method_type; + //リクエストParse済へ遷移(この関数の後はModが責任を持ってリクエストを返却) + NyLPC_cHttpdConnection_setReqStatusParsed(i_connection); + + + //VM起動の為の排他ロック + NyLPC_cHttpdConnection_lock(i_connection); + + + //URL解析の準備 + single_header._prefix_len=-((NyLPC_TInt16)strlen(i_inst->super._ref_root_path)+2); + single_header._astate=ST_PARSE_PATH; + NyLPC_cStr_initialize(&single_header._tstr,single_header._tstr_buf,16); + NyLPC_cMiMicDbCompiler_initialize(&single_header._binparser); + NyLPC_cMiMicTxtCompiler_initialize(&single_header._txtcmp); + + NyLPC_cHttpBasicHeaderParser_initialize(&parser,&handler); + + + //プリフェッチしたデータを流す + NyLPC_cHttpBasicHeaderParser_parseInit(&parser,&(single_header.super)); + NyLPC_cHttpdConnection_pushPrefetchInfo(i_connection,&parser,&single_header.super); + //後続をストリームから取り込む + if(!NyLPC_cHttpBasicHeaderParser_parseStream(&parser,NyLPC_cHttpdConnection_refStream(i_connection),&(single_header.super))){ + NyLPC_cHttpdUtils_sendErrorResponse(i_connection,500); + NyLPC_OnErrorGoto(Error1); + } + if(!NyLPC_cHttpBasicHeaderParser_parseFinish(&parser,&(single_header.super))){ + NyLPC_cHttpdUtils_sendErrorResponse(i_connection,500); + NyLPC_OnErrorGoto(Error1); + } + //GETかHEADに制限 + method_type=NyLPC_cHttpdConnection_getMethod(i_connection); + if(method_type!=NyLPC_THttpMethodType_GET && method_type!=NyLPC_THttpMethodType_HEAD) + { + NyLPC_cHttpdUtils_sendErrorResponse(i_connection,405); + NyLPC_OnErrorGoto(Error1); + } + //Request::ConnectionがClose設定,又はHTTP1.1では無い場合,CLOSE + if(single_header.super.connection==NyLPC_THttpMessgeHeader_Connection_CLOSE || single_header.super.startline.req.version!=NyLPC_THttpVersion_11) + { + NyLPC_cHttpdConnection_setConnectionMode(i_connection,NyLPC_TcHttpdConnection_CONNECTION_MODE_CLOSE); + } + //CGIの実行 + switch(single_header._content_id) + { + case CONTENT_ID_MVM: + mvm(i_connection,&single_header); + break; + case CONTENT_ID_STATUS: + status(i_connection); + break; + default: + NyLPC_cHttpdUtils_sendErrorResponse(i_connection,400); + NyLPC_OnErrorGoto(Error1); + } + NyLPC_cStr_finalize(&single_header._tstr); + NyLPC_cMiMicDbCompiler_finalize(&single_header._binparser); + NyLPC_cMiMicTxtCompiler_finalize(&single_header._txtcmp); + NyLPC_cHttpBasicHeaderParser_finalize(&parser); //占有解除 - NyLPC_cHttpdConnection_unlock(i_connection); - return NyLPC_TBool_TRUE; + NyLPC_cHttpdConnection_unlock(i_connection); + return NyLPC_TBool_TRUE; Error1: - NyLPC_cStr_finalize(&single_header._tstr); - NyLPC_cMiMicDbCompiler_finalize(&single_header._binparser); - NyLPC_cMiMicTxtCompiler_finalize(&single_header._txtcmp); - NyLPC_cHttpBasicHeaderParser_finalize(&parser); - //VM排他ロックの解除 - NyLPC_cHttpdConnection_unlock(i_connection); - return NyLPC_TBool_FALSE; + NyLPC_cStr_finalize(&single_header._tstr); + NyLPC_cMiMicDbCompiler_finalize(&single_header._binparser); + NyLPC_cMiMicTxtCompiler_finalize(&single_header._txtcmp); + NyLPC_cHttpBasicHeaderParser_finalize(&parser); + //VM排他ロックの解除 + NyLPC_cHttpdConnection_unlock(i_connection); + return NyLPC_TBool_FALSE; } @@ -414,14 +414,14 @@ Error1: */ struct TVmEventHandler { - struct NyLPC_TcMiMicVM_TEvent super; - const struct TModMiMicRemoteMcuHeader* req; - NyLPC_TcHttpdConnection_t* connection; - NyLPC_TUInt16 db_pos; - /** ストリームへ出力したデータの数*/ - NyLPC_TUInt16 st_len; - /** Bodyを送信するかのフラグ*/ - NyLPC_TBool is_send_body; + struct NyLPC_TcMiMicVM_TEvent super; + const struct TModMiMicRemoteMcuHeader* req; + NyLPC_TcHttpdConnection_t* connection; + NyLPC_TUInt16 db_pos; + /** ストリームへ出力したデータの数*/ + NyLPC_TUInt16 st_len; + /** Bodyを送信するかのフラグ*/ + NyLPC_TBool is_send_body; }; /** @@ -429,17 +429,17 @@ struct TVmEventHandler */ static NyLPC_TBool mvmputs_json(struct NyLPC_TcMiMicVM_TEvent* i_eh,NyLPC_TUInt32 i_val) { - struct TVmEventHandler* eh=(struct TVmEventHandler*)i_eh; - if(eh->is_send_body){ - if(eh->st_len>0){ - eh->st_len++; - return NyLPC_cHttpdConnection_sendResponseBodyF(eh->connection,",%u",i_val); - }else{ - eh->st_len++; - return NyLPC_cHttpdConnection_sendResponseBodyF(eh->connection,"%u",i_val); - } - } - return NyLPC_TBool_TRUE; + struct TVmEventHandler* eh=(struct TVmEventHandler*)i_eh; + if(eh->is_send_body){ + if(eh->st_len>0){ + eh->st_len++; + return NyLPC_cHttpdConnection_sendResponseBodyF(eh->connection,",%u",i_val); + }else{ + eh->st_len++; + return NyLPC_cHttpdConnection_sendResponseBodyF(eh->connection,"%u",i_val); + } + } + return NyLPC_TBool_TRUE; } /** @@ -447,53 +447,53 @@ static NyLPC_TBool mvmputs_json(struct NyLPC_TcMiMicVM_TEvent* i_eh,NyLPC_TUInt3 */ static NyLPC_TBool mvmgets(struct NyLPC_TcMiMicVM_TEvent* i_eh,NyLPC_TUInt32* o_val) { - struct TVmEventHandler* eh=(struct TVmEventHandler*)i_eh; - //読み出し済みDBサイズの確認 - if(eh->req->content.mvm.vm_instruction.db_len<=eh->db_pos){ - //読めない - return NyLPC_TBool_FALSE; - } - *o_val=eh->req->content.mvm.vm_instruction.db_part[eh->db_pos]; - eh->db_pos++; - return NyLPC_TBool_TRUE; + struct TVmEventHandler* eh=(struct TVmEventHandler*)i_eh; + //読み出し済みDBサイズの確認 + if(eh->req->content.mvm.vm_instruction.db_len<=eh->db_pos){ + //読めない + return NyLPC_TBool_FALSE; + } + *o_val=eh->req->content.mvm.vm_instruction.db_part[eh->db_pos]; + eh->db_pos++; + return NyLPC_TBool_TRUE; } /** * ネイティブCALLハンドラ */ static NyLPC_TUInt32 nativeCall(struct NyLPC_TcMiMicVM_TEvent* i_evh,NyLPC_TUInt32 i_id,NyLPC_TcMiMicVM_t* i_vm) { - (void)i_evh; -// NyLPC_TNativeFunction f=getNativeFunctionById(i_id); -// if(f==NULL){ -// return NyLPC_cMiMicVM_RESULT_RUNTIME_NG_UNKNOWN_CALL; -// } -// return f(i_vm)?NyLPC_cMiMicVM_RESULT_OK:NyLPC_cMiMicVM_RESULT_RUNTIME_NG_CALL; - return NyLPC_cMiMicVM_RESULT_RUNTIME_NG_CALL; + (void)i_evh; +// NyLPC_TNativeFunction f=getNativeFunctionById(i_id); +// if(f==NULL){ +// return NyLPC_cMiMicVM_RESULT_RUNTIME_NG_UNKNOWN_CALL; +// } +// return f(i_vm)?NyLPC_cMiMicVM_RESULT_OK:NyLPC_cMiMicVM_RESULT_RUNTIME_NG_CALL; + return NyLPC_cMiMicVM_RESULT_RUNTIME_NG_CALL; } static void mvmsleep(struct NyLPC_TcMiMicVM_TEvent* i_eh,NyLPC_TUInt32 i_sleep_in_msec) { - (void)i_eh; - NyLPC_cThread_sleep(i_sleep_in_msec); + (void)i_eh; + NyLPC_cThread_sleep(i_sleep_in_msec); } /** * RemoteMCUのステータスを返す。基本的にjson * { - * application:"[VERSION]" + * application:"[VERSION]" * } */ static void status(NyLPC_TcHttpdConnection_t* i_connection) { - if(!NyLPC_cHttpdUtils_sendJsonHeader(i_connection)){ - return; - } - //JSONを書く。 - if(NyLPC_cHttpdConnection_getMethod(i_connection)==NyLPC_THttpMethodType_GET){ - NyLPC_cHttpdConnection_sendResponseBodyF(i_connection,"{\"application\":\""MVM_VERSION"\"}"); - } - return; + if(!NyLPC_cHttpdUtils_sendJsonHeader(i_connection)){ + return; + } + //JSONを書く。 + if(NyLPC_cHttpdConnection_getMethod(i_connection)==NyLPC_THttpMethodType_GET){ + NyLPC_cHttpdConnection_sendResponseBodyF(i_connection,"{\"application\":\""MVM_VERSION"\"}"); + } + return; } /** * MimicVMの起動と,ResponseJSONの起動 @@ -502,52 +502,52 @@ static void status(NyLPC_TcHttpdConnection_t* i_connection) */ static void mvm(NyLPC_TcHttpdConnection_t* i_connection,const struct TModMiMicRemoteMcuHeader* i_rqh) { - struct TVmEventHandler he; - NyLPC_TcMiMicVM_t vm; - NyLPC_TUInt32 vmret; - if(i_rqh->content.mvm.v!=QVAL_V_1 || i_rqh->content.mvm.o!=QVAL_O_JSON) - { - NyLPC_cHttpdUtils_sendErrorResponse(i_connection,400); - return; - } - - //Bodyを書く - //ハンドラインスタンスの設定 - if(!NyLPC_cHttpdUtils_sendJsonHeader(i_connection)){ - NyLPC_OnErrorGoto(Error1); - } - - he.super.get_stream=mvmgets; - he.super.put_stream=mvmputs_json; - he.super.native_call=nativeCall; - he.super.sleep=mvmsleep; - he.db_pos=0; - he.st_len=0; - he.connection=i_connection; - he.req=i_rqh; - he.is_send_body=(NyLPC_cHttpdConnection_getMethod(i_connection)==NyLPC_THttpMethodType_GET); - - //起動VMの初期化 - NyLPC_cMiMicVM_initialize(&vm,(struct NyLPC_TcMiMicVM_TEvent*)&(he.super)); - - //JSONを書く。 - if(he.is_send_body){ - if(!NyLPC_cHttpdConnection_sendResponseBodyF(i_connection,"{\"version\":\""MVM_VERSION"\",\"stream\":[")){ - NyLPC_OnErrorGoto(Error1); - } - } - //VMの実行 - vmret=NyLPC_cMiMicVM_run(&(vm),i_rqh->content.mvm.vm_instruction.bc_buf,i_rqh->content.mvm.vm_instruction.txt_len); - //only GET method - if(he.is_send_body){ - if(!NyLPC_cHttpdConnection_sendResponseBodyF(i_connection,"],\"result\":%u}",vmret)){ - NyLPC_OnErrorGoto(Error1); - } - } - NyLPC_cMiMicVM_finalize(&vm); - return; + struct TVmEventHandler he; + NyLPC_TcMiMicVM_t vm; + NyLPC_TUInt32 vmret; + if(i_rqh->content.mvm.v!=QVAL_V_1 || i_rqh->content.mvm.o!=QVAL_O_JSON) + { + NyLPC_cHttpdUtils_sendErrorResponse(i_connection,400); + return; + } + + //Bodyを書く + //ハンドラインスタンスの設定 + if(!NyLPC_cHttpdUtils_sendJsonHeader(i_connection)){ + NyLPC_OnErrorGoto(Error1); + } + + he.super.get_stream=mvmgets; + he.super.put_stream=mvmputs_json; + he.super.native_call=nativeCall; + he.super.sleep=mvmsleep; + he.db_pos=0; + he.st_len=0; + he.connection=i_connection; + he.req=i_rqh; + he.is_send_body=(NyLPC_cHttpdConnection_getMethod(i_connection)==NyLPC_THttpMethodType_GET); + + //起動VMの初期化 + NyLPC_cMiMicVM_initialize(&vm,(struct NyLPC_TcMiMicVM_TEvent*)&(he.super)); + + //JSONを書く。 + if(he.is_send_body){ + if(!NyLPC_cHttpdConnection_sendResponseBodyF(i_connection,"{\"version\":\""MVM_VERSION"\",\"stream\":[")){ + NyLPC_OnErrorGoto(Error1); + } + } + //VMの実行 + vmret=NyLPC_cMiMicVM_run(&(vm),i_rqh->content.mvm.vm_instruction.bc_buf,i_rqh->content.mvm.vm_instruction.txt_len); + //only GET method + if(he.is_send_body){ + if(!NyLPC_cHttpdConnection_sendResponseBodyF(i_connection,"],\"result\":%u}",vmret)){ + NyLPC_OnErrorGoto(Error1); + } + } + NyLPC_cMiMicVM_finalize(&vm); + return; Error1: - NyLPC_cMiMicVM_finalize(&vm); - return; + NyLPC_cMiMicVM_finalize(&vm); + return; } diff --git a/lib/src/net/httpd/mod/NyLPC_cModRemoteMcu.h b/lib/src/net/httpd/mod/NyLPC_cModRemoteMcu.h index a783c9b..97e120f 100644 --- a/lib/src/net/httpd/mod/NyLPC_cModRemoteMcu.h +++ b/lib/src/net/httpd/mod/NyLPC_cModRemoteMcu.h @@ -29,7 +29,7 @@ typedef struct NyLPC_TcModRemoteMcu NyLPC_TcModRemoteMcu_t; struct NyLPC_TcModRemoteMcu { - NyLPC_TcModRomFiles_t super; + NyLPC_TcModRomFiles_t super; }; /** diff --git a/lib/src/net/httpd/mod/NyLPC_cModRomFiles.c b/lib/src/net/httpd/mod/NyLPC_cModRomFiles.c index db70c04..a76c83e 100644 --- a/lib/src/net/httpd/mod/NyLPC_cModRomFiles.c +++ b/lib/src/net/httpd/mod/NyLPC_cModRomFiles.c @@ -19,8 +19,8 @@ * along with this program. If not, see . * * For further information please contact. - * http://nyatla.jp/ - * or + * http://nyatla.jp/ + * or * *********************************************************************************/ #include "NyLPC_cModRomFiles_protected.h" @@ -41,14 +41,14 @@ */ void NyLPC_cModRomFiles_initialize(NyLPC_TcModRomFiles_t* i_inst,const NyLPC_TChar* i_ref_root_path,const struct NyLPC_TRomFileData* i_data,int i_num_of_data) { - NyLPC_cModUrl_initialize(&(i_inst->super)); - i_inst->_data=i_data; - i_inst->_num_of_data=i_num_of_data; - i_inst->_ref_root_path=i_ref_root_path; + NyLPC_cModUrl_initialize(&(i_inst->super)); + i_inst->_data=i_data; + i_inst->_num_of_data=i_num_of_data; + i_inst->_ref_root_path=i_ref_root_path; } void NyLPC_cModRomFiles_finalize(NyLPC_TcModRomFiles_t* i_inst) { - NyLPC_cModUrl_finalize(&(i_inst->super)); + NyLPC_cModUrl_finalize(&(i_inst->super)); } /** @@ -56,23 +56,23 @@ void NyLPC_cModRomFiles_finalize(NyLPC_TcModRomFiles_t* i_inst) */ NyLPC_TBool NyLPC_cModRomFiles_canHandle(NyLPC_TcModRomFiles_t* i_inst,NyLPC_TcHttpdConnection_t* i_connection) { - const NyLPC_TChar* in_url; - const NyLPC_TChar* base_url=i_inst->_ref_root_path; - //connectonの状態を確認 - if(!NyLPC_cHttpdConnection_getReqStatus(i_connection)==NyLPC_cHttpdConnection_ReqStatus_REQPARSE) - { - return NyLPC_TBool_FALSE; - } - in_url=NyLPC_cHttpdConnection_getUrlPrefix(i_connection); - size_t base_url_len=strlen(base_url); - //check '/'+base_url+'/' - if(strlen(in_url)-2_ref_root_path; + //connectonの状態を確認 + if(!NyLPC_cHttpdConnection_getReqStatus(i_connection)==NyLPC_cHttpdConnection_ReqStatus_REQPARSE) + { + return NyLPC_TBool_FALSE; + } + in_url=NyLPC_cHttpdConnection_getUrlPrefix(i_connection); + size_t base_url_len=strlen(base_url); + //check '/'+base_url+'/' + if(strlen(in_url)-2super),i_connection,url,32,strlen(i_inst->_ref_root_path)+2,NyLPC_cModUrl_ParseMode_PATH_ONLY)) - { - //Response処理はモジュール内で実行済 - NyLPC_OnErrorGoto(Error1); - } - //GETかHEADに制限 - method_type=NyLPC_cHttpdConnection_getMethod(i_connection); - if(method_type!=NyLPC_THttpMethodType_GET && method_type!=NyLPC_THttpMethodType_HEAD) - { - NyLPC_cHttpdUtils_sendErrorResponse(i_connection,405); - NyLPC_OnErrorGoto(Error1); - } - if(strlen(url)==0) - { - //PrefixがURLよりも短ければ403エラーで処理終了。 - NyLPC_cHttpdUtils_sendErrorResponse(i_connection,403); - NyLPC_OnErrorGoto(Error1); - } - //URL比較 - for(i=i_inst->_num_of_data-1;i>=0;i--) - { - if(strcmp(url,i_inst->_data[i].name)!=0){ - continue; - } - //Request::ConnectionがClose設定,又はHTTP1.1では無い場合,CLOSE - if(i_inst->super._header.connection==NyLPC_THttpMessgeHeader_Connection_CLOSE || i_inst->super._header.startline.req.version!=NyLPC_THttpVersion_11) - { - NyLPC_cHttpdConnection_setConnectionMode(i_connection,NyLPC_TcHttpdConnection_CONNECTION_MODE_CLOSE); - } - return NyLPC_cHttpdUtils_sendFixedContentBatch(i_connection,i_inst->_data[i].content_type,i_inst->_data[i].data,(i_inst->_data[i].size>0)?i_inst->_data[i].size:strlen(i_inst->_data[i].data)); - } - //404Error - NyLPC_cHttpdUtils_sendErrorResponse(i_connection,404); + int i; + char url[32]; + NyLPC_TUInt8 method_type; + //URLサフィックスを取得(rootpath+'2'*2) + if(!NyLPC_cModUrl_execute2(&(i_inst->super),i_connection,url,32,strlen(i_inst->_ref_root_path)+2,NyLPC_cModUrl_ParseMode_PATH_ONLY)) + { + //Response処理はモジュール内で実行済 + NyLPC_OnErrorGoto(Error1); + } + //GETかHEADに制限 + method_type=NyLPC_cHttpdConnection_getMethod(i_connection); + if(method_type!=NyLPC_THttpMethodType_GET && method_type!=NyLPC_THttpMethodType_HEAD) + { + NyLPC_cHttpdUtils_sendErrorResponse(i_connection,405); + NyLPC_OnErrorGoto(Error1); + } + if(strlen(url)==0) + { + //PrefixがURLよりも短ければ403エラーで処理終了。 + NyLPC_cHttpdUtils_sendErrorResponse(i_connection,403); + NyLPC_OnErrorGoto(Error1); + } + //URL比較 + for(i=i_inst->_num_of_data-1;i>=0;i--) + { + if(strcmp(url,i_inst->_data[i].name)!=0){ + continue; + } + //Request::ConnectionがClose設定,又はHTTP1.1では無い場合,CLOSE + if(i_inst->super._header.connection==NyLPC_THttpMessgeHeader_Connection_CLOSE || i_inst->super._header.startline.req.version!=NyLPC_THttpVersion_11) + { + NyLPC_cHttpdConnection_setConnectionMode(i_connection,NyLPC_TcHttpdConnection_CONNECTION_MODE_CLOSE); + } + return NyLPC_cHttpdUtils_sendFixedContentBatch(i_connection,i_inst->_data[i].content_type,i_inst->_data[i].data,(i_inst->_data[i].size>0)?i_inst->_data[i].size:strlen(i_inst->_data[i].data)); + } + //404Error + NyLPC_cHttpdUtils_sendErrorResponse(i_connection,404); Error1: - return NyLPC_TBool_FALSE; + return NyLPC_TBool_FALSE; } diff --git a/lib/src/net/httpd/mod/NyLPC_cModRomFiles.h b/lib/src/net/httpd/mod/NyLPC_cModRomFiles.h index 2e3db66..6eb0fed 100644 --- a/lib/src/net/httpd/mod/NyLPC_cModRomFiles.h +++ b/lib/src/net/httpd/mod/NyLPC_cModRomFiles.h @@ -19,8 +19,8 @@ * along with this program. If not, see . * * For further information please contact. - * http://nyatla.jp/ - * or + * http://nyatla.jp/ + * or * *********************************************************************************/ #ifndef NYLPC_CMODROMFILES_H_ @@ -46,13 +46,13 @@ typedef struct NyLPC_TcModRomFiles NyLPC_TcModRomFiles_t; struct NyLPC_TcModRomFiles { - NyLPC_TcModUrl_t super; - /** ルートパス*/ - const NyLPC_TChar* _ref_root_path; - /** ROMFileのデータ*/ - const struct NyLPC_TRomFileData* _data; - /** ROMFILEデータの数*/ - int _num_of_data; + NyLPC_TcModUrl_t super; + /** ルートパス*/ + const NyLPC_TChar* _ref_root_path; + /** ROMFileのデータ*/ + const struct NyLPC_TRomFileData* _data; + /** ROMFILEデータの数*/ + int _num_of_data; }; /** diff --git a/lib/src/net/httpd/mod/NyLPC_cModUPnPDevice.c b/lib/src/net/httpd/mod/NyLPC_cModUPnPDevice.c index 4a285df..2a2c544 100644 --- a/lib/src/net/httpd/mod/NyLPC_cModUPnPDevice.c +++ b/lib/src/net/httpd/mod/NyLPC_cModUPnPDevice.c @@ -19,8 +19,8 @@ * along with this program. If not, see . * * For further information please contact. - * http://nyatla.jp/ - * or + * http://nyatla.jp/ + * or * *********************************************************************************/ #include "NyLPC_cModUPnPDevice.h" @@ -28,6 +28,7 @@ #include "NyLPC_net.h" #include "NyLPC_stdlib.h" #include "NyLPC_http.h" +#include #include @@ -35,17 +36,17 @@ #define SIZE_OF_STRBUF 16 struct TUPnPDeviceHeader { - struct NyLPC_THttpBasicHeader super; - //解析用 - NyLPC_TUInt8 _content_id; - NyLPC_TUInt8 _astate; - NyLPC_TInt16 _prefix_len; - NyLPC_TcStr_t _tstr; - NyLPC_TChar _tstr_buf[SIZE_OF_STRBUF]; - /** 文字列のパーサ*/ - union{ - NyLPC_TInt16 service_idx; - }content; + struct NyLPC_THttpBasicHeader super; + //解析用 + NyLPC_TUInt8 _content_id; + NyLPC_TUInt8 _astate; + NyLPC_TInt16 _prefix_len; + NyLPC_TcStr_t _tstr; + NyLPC_TChar _tstr_buf[SIZE_OF_STRBUF]; + /** 文字列のパーサ*/ + union{ + NyLPC_TInt16 service_idx; + }content; }; #define ST_PARSE_PATH 1 @@ -54,10 +55,10 @@ struct TUPnPDeviceHeader /** * コンテンツID定義(コンテンツ名に対応) */ -#define CONTENT_ID_UNKNOWN 1 +#define CONTENT_ID_UNKNOWN 1 #define CONTENT_ID_DEVICE_XML 2 -#define CONTENT_ID_CONTROL 3 -#define CONTENT_ID_EVENT 4 +#define CONTENT_ID_CONTROL 3 +#define CONTENT_ID_EVENT 4 #define QNAME_ID_UNKNOWN 1 #define QNAME_IDX 2 @@ -73,143 +74,143 @@ struct TUPnPDeviceHeader static void writeDeviceNode(const struct NyLPC_TUPnPDevDescDevice* i_dev,NyLPC_TcHttpdConnection_t* i_connection,NyLPC_TUInt16* sidx) { - //Required - NyLPC_cHttpdConnection_sendResponseBodyF(i_connection, - "" - "%s" - "%s" - "%s", - i_dev->device_type, - i_dev->frendly_name, - i_dev->manufacturer); - NyLPC_TInt16 i; - //Optional - if(i_dev->manufacturer_url!=NULL){ - NyLPC_cHttpdConnection_sendResponseBodyF(i_connection, - "%s", - i_dev->manufacturer_url); - } - //Recommended - if(i_dev->model_descriprion!=NULL){ - NyLPC_cHttpdConnection_sendResponseBodyF(i_connection, - "%s", - i_dev->model_descriprion); - }else{ - NyLPC_cHttpdConnection_sendResponseBodyF(i_connection, - ""); //Recommended - } - //Required - NyLPC_cHttpdConnection_sendResponseBodyF(i_connection, - "%s", - i_dev->model_name); - //Recommended - if(i_dev->model_number!=NULL){ - NyLPC_cHttpdConnection_sendResponseBodyF(i_connection, - "%s", - i_dev->model_number); - }else{ - NyLPC_cHttpdConnection_sendResponseBodyF(i_connection, - ""); - } - //Optional - if(i_dev->model_url!=NULL){ - NyLPC_cHttpdConnection_sendResponseBodyF(i_connection, - "%s", - i_dev->model_url); - } - //Recommended - if(i_dev->serial_number!=NULL){ - NyLPC_cHttpdConnection_sendResponseBodyF(i_connection, - "%s", - i_dev->serial_number); - }else{ - NyLPC_cHttpdConnection_sendResponseBodyF(i_connection, - ""); - } - //Required - NyLPC_cHttpdConnection_sendResponseBodyF(i_connection, - "%s", - i_dev->udn); - //Oprional - if(i_dev->upc!=NULL){ - NyLPC_cHttpdConnection_sendResponseBodyF(i_connection, - "%s", - i_dev->upc); - } - if(i_dev->number_of_icon>0){ - NyLPC_cHttpdConnection_sendResponseBodyF(i_connection, - ""); - for(i=0;inumber_of_icon;i++){ - NyLPC_cHttpdConnection_sendResponseBodyF(i_connection, - "" - "%s" - "%d" - "%d" - "%d" - "%s" - "", - i_dev->icons[i].mimetype, - i_dev->icons[i].width, - i_dev->icons[i].height, - i_dev->icons[i].depth, - i_dev->icons[i].url); - } - NyLPC_cHttpdConnection_sendResponseBodyF(i_connection, - ""); - }else{ - NyLPC_cHttpdConnection_sendResponseBodyF(i_connection, - ""); - } - //Optional - if(i_dev->number_of_service>0){ - NyLPC_cHttpdConnection_sendResponseBodyF(i_connection, - ""); - for(i=0;inumber_of_service;i++){ - NyLPC_cHttpdConnection_sendResponseBodyF(i_connection, - "" - "%s" - "%s" - "%s" - "./control/%d" - "./event/%d" - "", - i_dev->services[i].scpd_url, - i_dev->services[i].service_type, - i_dev->services[i].service_id, - (*sidx)+i, - (*sidx)+i); - } - NyLPC_cHttpdConnection_sendResponseBodyF(i_connection, - ""); - } - if(i_dev->number_of_devices>0){ - NyLPC_cHttpdConnection_sendResponseBodyF(i_connection, - ""); - for(i=0;inumber_of_devices;i++){ - (*sidx)=(*sidx)+0x10; - writeDeviceNode(i_dev->devices[i],i_connection,sidx); - } - NyLPC_cHttpdConnection_sendResponseBodyF(i_connection, - ""); - } - if(i_dev->presentation_url!=NULL){ - NyLPC_cHttpdConnection_sendResponseBodyF(i_connection, - "%s", - i_dev->presentation_url); - } - else{ - NyLPC_cHttpdConnection_sendResponseBodyF(i_connection, - ""); - } + //Required + NyLPC_cHttpdConnection_sendResponseBodyF(i_connection, + "" + "%s" + "%s" + "%s", + i_dev->device_type, + i_dev->frendly_name, + i_dev->manufacturer); + NyLPC_TInt16 i; + //Optional + if(i_dev->manufacturer_url!=NULL){ + NyLPC_cHttpdConnection_sendResponseBodyF(i_connection, + "%s", + i_dev->manufacturer_url); + } + //Recommended + if(i_dev->model_descriprion!=NULL){ + NyLPC_cHttpdConnection_sendResponseBodyF(i_connection, + "%s", + i_dev->model_descriprion); + }else{ + NyLPC_cHttpdConnection_sendResponseBodyF(i_connection, + ""); //Recommended + } + //Required + NyLPC_cHttpdConnection_sendResponseBodyF(i_connection, + "%s", + i_dev->model_name); + //Recommended + if(i_dev->model_number!=NULL){ + NyLPC_cHttpdConnection_sendResponseBodyF(i_connection, + "%s", + i_dev->model_number); + }else{ + NyLPC_cHttpdConnection_sendResponseBodyF(i_connection, + ""); + } + //Optional + if(i_dev->model_url!=NULL){ + NyLPC_cHttpdConnection_sendResponseBodyF(i_connection, + "%s", + i_dev->model_url); + } + //Recommended + if(i_dev->serial_number!=NULL){ + NyLPC_cHttpdConnection_sendResponseBodyF(i_connection, + "%s", + i_dev->serial_number); + }else{ + NyLPC_cHttpdConnection_sendResponseBodyF(i_connection, + ""); + } + //Required + NyLPC_cHttpdConnection_sendResponseBodyF(i_connection, + "%s", + i_dev->udn); + //Oprional + if(i_dev->upc!=NULL){ + NyLPC_cHttpdConnection_sendResponseBodyF(i_connection, + "%s", + i_dev->upc); + } + if(i_dev->number_of_icon>0){ + NyLPC_cHttpdConnection_sendResponseBodyF(i_connection, + ""); + for(i=0;inumber_of_icon;i++){ + NyLPC_cHttpdConnection_sendResponseBodyF(i_connection, + "" + "%s" + "%d" + "%d" + "%d" + "%s" + "", + i_dev->icons[i].mimetype, + i_dev->icons[i].width, + i_dev->icons[i].height, + i_dev->icons[i].depth, + i_dev->icons[i].url); + } + NyLPC_cHttpdConnection_sendResponseBodyF(i_connection, + ""); + }else{ + NyLPC_cHttpdConnection_sendResponseBodyF(i_connection, + ""); + } + //Optional + if(i_dev->number_of_service>0){ + NyLPC_cHttpdConnection_sendResponseBodyF(i_connection, + ""); + for(i=0;inumber_of_service;i++){ + NyLPC_cHttpdConnection_sendResponseBodyF(i_connection, + "" + "%s" + "%s" + "%s" + "./control/%d" + "./event/%d" + "", + i_dev->services[i].scpd_url, + i_dev->services[i].service_type, + i_dev->services[i].service_id, + (*sidx)+i, + (*sidx)+i); + } + NyLPC_cHttpdConnection_sendResponseBodyF(i_connection, + ""); + } + if(i_dev->number_of_devices>0){ + NyLPC_cHttpdConnection_sendResponseBodyF(i_connection, + ""); + for(i=0;inumber_of_devices;i++){ + (*sidx)=(*sidx)+0x10; + writeDeviceNode(i_dev->devices[i],i_connection,sidx); + } + NyLPC_cHttpdConnection_sendResponseBodyF(i_connection, + ""); + } + if(i_dev->presentation_url!=NULL){ + NyLPC_cHttpdConnection_sendResponseBodyF(i_connection, + "%s", + i_dev->presentation_url); + } + else{ + NyLPC_cHttpdConnection_sendResponseBodyF(i_connection, + ""); + } } NyLPC_TBool writeDeviceDescription(const struct NyLPC_TUPnPDevDescDevice* i_dev,NyLPC_TcHttpdConnection_t* i_connection) { - NyLPC_cHttpdConnection_sendResponseBodyF(i_connection, - "" - "" - "10"); - writeDeviceNode(i_dev,i_connection,0); - return NyLPC_cHttpdConnection_sendResponseBodyF(i_connection,""); + NyLPC_cHttpdConnection_sendResponseBodyF(i_connection, + "" + "" + "10"); + writeDeviceNode(i_dev,i_connection,0); + return NyLPC_cHttpdConnection_sendResponseBodyF(i_connection,""); } @@ -222,61 +223,61 @@ NyLPC_TBool writeDeviceDescription(const struct NyLPC_TUPnPDevDescDevice* i_dev, */ static NyLPC_TInt16 parseSidx(const NyLPC_TChar* i_str) { - //先頭は/であること - if(*i_str!='/'){ - return -1; - } - //2桁の16進数であること - if(!isxdigit(*(i_str+1)) || !isxdigit(*(i_str+2))){ - return -1; - } - //サービスID化 - return NyLPC_ctox(*(i_str+1))<<8 | NyLPC_ctox(*(i_str+2)); + //先頭は/であること + if(*i_str!='/'){ + return -1; + } + //2桁の16進数であること + if(!isxdigit((int)(*(i_str+1))) || !isxdigit((int)(*(i_str+2)))){ + return -1; + } + //サービスID化 + return NyLPC_ctox(*(i_str+1))<<8 | NyLPC_ctox(*(i_str+2)); } static NyLPC_TBool urlHandler(NyLPC_TcHttpBasicHeaderParser_t* i_inst,NyLPC_TChar i_c,struct NyLPC_THttpBasicHeader* o_out) { - struct TUPnPDeviceHeader* out=(struct TUPnPDeviceHeader*)o_out; - //読み飛ばし - if(out->_prefix_len<0){ - out->_prefix_len++; - return NyLPC_TBool_TRUE;//読み飛ばし - } - if(out->_astate==ST_PARSE_PATH){ - if(i_c!='\0' && i_c!='?'){ - if(!NyLPC_cStr_put(&(out->_tstr),i_c)){ - NyLPC_OnErrorGoto(ERROR); - } - return NyLPC_TBool_TRUE; - } - if(strcmp(NyLPC_cStr_str(&(out->_tstr)),CONTENT_STR_DEVICE_XML)==0){ - out->_content_id=CONTENT_ID_DEVICE_XML; - }else if(strncmp(CONTENT_STR_CONTROL_PATH,NyLPC_cStr_str(&(out->_tstr)),7)==0){ - out->_content_id=CONTENT_ID_CONTROL; - parseSidx(NyLPC_cStr_str(&(out->_tstr))+7); - }else if(strncmp(CONTENT_STR_EVENT_PATH,NyLPC_cStr_str(&(out->_tstr)),5)==0){ - out->_content_id=CONTENT_ID_EVENT; - parseSidx(NyLPC_cStr_str(&(out->_tstr))+5); - }else{ - NyLPC_OnErrorGoto(ERROR); - } - NyLPC_cStr_clear(&(out->_tstr)); - out->_astate=ST_PARSE_QUERY_NAME;//クエリ名解析へ - return NyLPC_TBool_TRUE; - } - return NyLPC_TBool_TRUE; + struct TUPnPDeviceHeader* out=(struct TUPnPDeviceHeader*)o_out; + //読み飛ばし + if(out->_prefix_len<0){ + out->_prefix_len++; + return NyLPC_TBool_TRUE;//読み飛ばし + } + if(out->_astate==ST_PARSE_PATH){ + if(i_c!='\0' && i_c!='?'){ + if(!NyLPC_cStr_put(&(out->_tstr),i_c)){ + NyLPC_OnErrorGoto(ERROR); + } + return NyLPC_TBool_TRUE; + } + if(strcmp(NyLPC_cStr_str(&(out->_tstr)),CONTENT_STR_DEVICE_XML)==0){ + out->_content_id=CONTENT_ID_DEVICE_XML; + }else if(strncmp(CONTENT_STR_CONTROL_PATH,NyLPC_cStr_str(&(out->_tstr)),7)==0){ + out->_content_id=CONTENT_ID_CONTROL; + parseSidx(NyLPC_cStr_str(&(out->_tstr))+7); + }else if(strncmp(CONTENT_STR_EVENT_PATH,NyLPC_cStr_str(&(out->_tstr)),5)==0){ + out->_content_id=CONTENT_ID_EVENT; + parseSidx(NyLPC_cStr_str(&(out->_tstr))+5); + }else{ + NyLPC_OnErrorGoto(ERROR); + } + NyLPC_cStr_clear(&(out->_tstr)); + out->_astate=ST_PARSE_QUERY_NAME;//クエリ名解析へ + return NyLPC_TBool_TRUE; + } + return NyLPC_TBool_TRUE; ERROR: - return NyLPC_TBool_FALSE; + return NyLPC_TBool_FALSE; } /** * デフォルトハンドラ */ static const struct NyLPC_TcHttpBasicHeaderParser_Handler handler= { - NULL, - urlHandler + NULL, + urlHandler }; @@ -289,19 +290,19 @@ static const struct NyLPC_TcHttpBasicHeaderParser_Handler handler= */ void NyLPC_cModUPnPDevice_initialize(NyLPC_TcModUPnPDevice_t* i_inst,const NyLPC_TcUPnP_t* i_ref_upnp) { - NyLPC_cModRomFiles_initialize(&i_inst->super,i_ref_upnp->_ref_root_path,NULL,0); - i_inst->_ref_upnp=i_ref_upnp; + NyLPC_cModRomFiles_initialize(&i_inst->super,i_ref_upnp->_ref_root_path,NULL,0); + i_inst->_ref_upnp=i_ref_upnp; } void NyLPC_cModUPnPDevice_finalize(NyLPC_TcModUPnPDevice_t* i_inst) { - NyLPC_cModRomFiles_finalize(&i_inst->super); + NyLPC_cModRomFiles_finalize(&i_inst->super); } /** * モジュールがコネクションをハンドリングできるかを返します。 */ NyLPC_TBool NyLPC_cModUPnPDevice_canHandle(NyLPC_TcModUPnPDevice_t* i_inst,NyLPC_TcHttpdConnection_t* i_connection) { - return NyLPC_cModRomFiles_canHandle(&i_inst->super,i_connection); + return NyLPC_cModRomFiles_canHandle(&i_inst->super,i_connection); } /** @@ -309,65 +310,65 @@ NyLPC_TBool NyLPC_cModUPnPDevice_canHandle(NyLPC_TcModUPnPDevice_t* i_inst,NyLPC */ NyLPC_TBool NyLPC_cModUPnPDevice_execute(NyLPC_TcModUPnPDevice_t* i_inst,NyLPC_TcHttpdConnection_t* i_connection) { - NyLPC_TUInt8 method_type; - struct TUPnPDeviceHeader header; - NyLPC_TcHttpBasicHeaderParser_t parser; + NyLPC_TUInt8 method_type; + struct TUPnPDeviceHeader header; + NyLPC_TcHttpBasicHeaderParser_t parser; - //リクエストParse済へ遷移(この関数の後はModが責任を持ってリクエストを返却) - NyLPC_cHttpdConnection_setReqStatusParsed(i_connection); - NyLPC_cStr_initialize(&header._tstr,header._tstr_buf,SIZE_OF_STRBUF); + //リクエストParse済へ遷移(この関数の後はModが責任を持ってリクエストを返却) + NyLPC_cHttpdConnection_setReqStatusParsed(i_connection); + NyLPC_cStr_initialize(&header._tstr,header._tstr_buf,SIZE_OF_STRBUF); - //URL解析の準備 - header._prefix_len=-((NyLPC_TInt16)strlen(i_inst->super._ref_root_path)+2); - header._astate=ST_PARSE_PATH; + //URL解析の準備 + header._prefix_len=-((NyLPC_TInt16)strlen(i_inst->super._ref_root_path)+2); + header._astate=ST_PARSE_PATH; - NyLPC_cHttpBasicHeaderParser_initialize(&parser,&handler); - NyLPC_cHttpBasicHeaderParser_parseInit(&parser,&(header.super)); - //プリフェッチしたデータを流す - NyLPC_cHttpdConnection_pushPrefetchInfo(i_connection,&parser,&header.super); - //後続をストリームから取り込む - if(!NyLPC_cHttpBasicHeaderParser_parseStream(&parser,NyLPC_cHttpdConnection_refStream(i_connection),&(header.super))){ - NyLPC_cHttpdUtils_sendErrorResponse(i_connection,500); - NyLPC_OnErrorGoto(Error2); - } - if(!NyLPC_cHttpBasicHeaderParser_parseFinish(&parser,&(header.super))){ - NyLPC_cHttpdUtils_sendErrorResponse(i_connection,500); - NyLPC_OnErrorGoto(Error2); - } - //GETかHEADに制限(Descriptionの場合だけ) - method_type=NyLPC_cHttpdConnection_getMethod(i_connection); - if(method_type!=NyLPC_THttpMethodType_GET && method_type!=NyLPC_THttpMethodType_HEAD) - { - NyLPC_cHttpdUtils_sendErrorResponse(i_connection,405); - NyLPC_OnErrorGoto(Error2); - } - //Request::ConnectionがClose設定,又はHTTP1.1では無い場合,CLOSE - if(header.super.connection==NyLPC_THttpMessgeHeader_Connection_CLOSE || header.super.startline.req.version!=NyLPC_THttpVersion_11) - { - NyLPC_cHttpdConnection_setConnectionMode(i_connection,NyLPC_TcHttpdConnection_CONNECTION_MODE_CLOSE); - } - //CGIの実行 - switch(header._content_id) - { - case CONTENT_ID_DEVICE_XML: - NyLPC_cHttpdConnection_sendResponseHeader(i_connection,200,CONTENT_STR_XML_MIME_TYPE,NULL); - writeDeviceDescription(i_inst->_ref_upnp->ref_root_device,i_connection); - //DeviceXML - break; - case CONTENT_ID_CONTROL: - //SoapHandler 未実装 - case CONTENT_ID_EVENT: - //EventHandler 未実装 - default: - NyLPC_cHttpdUtils_sendErrorResponse(i_connection,500); - NyLPC_OnErrorGoto(Error2); - } - NyLPC_cHttpBasicHeaderParser_finalize(&parser); - NyLPC_cStr_finalize(&header._tstr); - return NyLPC_TBool_TRUE; + NyLPC_cHttpBasicHeaderParser_initialize(&parser,&handler); + NyLPC_cHttpBasicHeaderParser_parseInit(&parser,&(header.super)); + //プリフェッチしたデータを流す + NyLPC_cHttpdConnection_pushPrefetchInfo(i_connection,&parser,&header.super); + //後続をストリームから取り込む + if(!NyLPC_cHttpBasicHeaderParser_parseStream(&parser,NyLPC_cHttpdConnection_refStream(i_connection),&(header.super))){ + NyLPC_cHttpdUtils_sendErrorResponse(i_connection,500); + NyLPC_OnErrorGoto(Error2); + } + if(!NyLPC_cHttpBasicHeaderParser_parseFinish(&parser,&(header.super))){ + NyLPC_cHttpdUtils_sendErrorResponse(i_connection,500); + NyLPC_OnErrorGoto(Error2); + } + //GETかHEADに制限(Descriptionの場合だけ) + method_type=NyLPC_cHttpdConnection_getMethod(i_connection); + if(method_type!=NyLPC_THttpMethodType_GET && method_type!=NyLPC_THttpMethodType_HEAD) + { + NyLPC_cHttpdUtils_sendErrorResponse(i_connection,405); + NyLPC_OnErrorGoto(Error2); + } + //Request::ConnectionがClose設定,又はHTTP1.1では無い場合,CLOSE + if(header.super.connection==NyLPC_THttpMessgeHeader_Connection_CLOSE || header.super.startline.req.version!=NyLPC_THttpVersion_11) + { + NyLPC_cHttpdConnection_setConnectionMode(i_connection,NyLPC_TcHttpdConnection_CONNECTION_MODE_CLOSE); + } + //CGIの実行 + switch(header._content_id) + { + case CONTENT_ID_DEVICE_XML: + NyLPC_cHttpdConnection_sendResponseHeader(i_connection,200,CONTENT_STR_XML_MIME_TYPE,NULL); + writeDeviceDescription(i_inst->_ref_upnp->ref_root_device,i_connection); + //DeviceXML + break; + case CONTENT_ID_CONTROL: + //SoapHandler 未実装 + case CONTENT_ID_EVENT: + //EventHandler 未実装 + default: + NyLPC_cHttpdUtils_sendErrorResponse(i_connection,500); + NyLPC_OnErrorGoto(Error2); + } + NyLPC_cHttpBasicHeaderParser_finalize(&parser); + NyLPC_cStr_finalize(&header._tstr); + return NyLPC_TBool_TRUE; Error2: - NyLPC_cHttpBasicHeaderParser_finalize(&parser); - NyLPC_cStr_finalize(&header._tstr); - return NyLPC_TBool_FALSE; + NyLPC_cHttpBasicHeaderParser_finalize(&parser); + NyLPC_cStr_finalize(&header._tstr); + return NyLPC_TBool_FALSE; } diff --git a/lib/src/net/httpd/mod/NyLPC_cModUPnPDevice.h b/lib/src/net/httpd/mod/NyLPC_cModUPnPDevice.h index 07d7be9..d530ef4 100644 --- a/lib/src/net/httpd/mod/NyLPC_cModUPnPDevice.h +++ b/lib/src/net/httpd/mod/NyLPC_cModUPnPDevice.h @@ -19,8 +19,8 @@ * along with this program. If not, see . * * For further information please contact. - * http://nyatla.jp/ - * or + * http://nyatla.jp/ + * or * *********************************************************************************/ #ifndef NYLPC_CMODUPNPDEVICE_H_ @@ -31,7 +31,7 @@ #include "NyLPC_stdlib.h" #include "NyLPC_http.h" -#include "../../NyLPC_cNet.h" + #ifdef __cplusplus extern "C" { @@ -49,18 +49,18 @@ extern "C" { * 上位8bitがデバイスインデックス、下位8bitがサービスインデックスです。 * 例えば以下の構造のデバイスでは、サービス番号は以下のようになります。 *
- * dev:a		0x1n (0<=n<=0xf)
- *  +-dev:b 	0x2n (0<=n<=0xf)
- *  |+-dev:c	0x3n (0<=n<=0xf)
- *  +-dev:d		0x4n (0<=n<=0xf)
+ * dev:a        0x1n (0<=n<=0xf)
+ *  +-dev:b     0x2n (0<=n<=0xf)
+ *  |+-dev:c    0x3n (0<=n<=0xf)
+ *  +-dev:d     0x4n (0<=n<=0xf)
  * 
*/ typedef struct NyLPC_TcModUPnPDevice NyLPC_TcModUPnPDevice_t; struct NyLPC_TcModUPnPDevice { - NyLPC_TcModRomFiles_t super; - const NyLPC_TcUPnP_t* _ref_upnp; + NyLPC_TcModRomFiles_t super; + const NyLPC_TcUPnP_t* _ref_upnp; }; /** diff --git a/lib/src/net/httpd/mod/NyLPC_cModUrl.c b/lib/src/net/httpd/mod/NyLPC_cModUrl.c index cfe76bd..6e6936d 100644 --- a/lib/src/net/httpd/mod/NyLPC_cModUrl.c +++ b/lib/src/net/httpd/mod/NyLPC_cModUrl.c @@ -19,8 +19,8 @@ * along with this program. If not, see . * * For further information please contact. - * http://nyatla.jp/ - * or + * http://nyatla.jp/ + * or * *********************************************************************************/ #include "NyLPC_cModUrl.h" @@ -31,72 +31,72 @@ typedef struct TcHeaderParser { - NyLPC_TcHttpBasicHeaderParser_t super; - char* url_buf; - NyLPC_TInt16 length_of_buf; - NyLPC_TInt16 length_of_url; - NyLPC_TInt16 reason; - NyLPC_TUInt8 skip; - NyLPC_TUInt8 mode; + NyLPC_TcHttpBasicHeaderParser_t super; + char* url_buf; + NyLPC_TInt16 length_of_buf; + NyLPC_TInt16 length_of_url; + NyLPC_TInt16 reason; + NyLPC_TUInt8 skip; + NyLPC_TUInt8 mode; }TcHeaderParser_t; static NyLPC_TBool NyLPC_cModUrl_urlHandler(NyLPC_TcHttpBasicHeaderParser_t* i_inst,NyLPC_TChar i_c,struct NyLPC_THttpBasicHeader* o_out) { - TcHeaderParser_t* inst=(TcHeaderParser_t*)i_inst; - if(inst->skip){ - //SKIPが有効な場合 - if(inst->length_of_url<0){ - inst->length_of_url++; - if(inst->length_of_url==0){ - inst->skip=NyLPC_TUInt8_FALSE; - } - } - return NyLPC_TBool_TRUE; - } - if((inst->mode & NyLPC_cModUrl_ParseMode_PATH_ONLY)==NyLPC_cModUrl_ParseMode_PATH_ONLY){ - if(strchr("?#",i_c)){ - inst->url_buf[inst->length_of_url]='\0'; - inst->skip=NyLPC_TUInt8_TRUE; - return NyLPC_TBool_TRUE;//Terminate - } - } - inst->url_buf[inst->length_of_url]=i_c; - if(i_c=='\0'){ - return NyLPC_TBool_TRUE;//Terminate - } - inst->length_of_url++; - if(inst->length_of_url==inst->length_of_buf){ - inst->reason=414; - return NyLPC_TBool_FALSE;//長すぎる。 - } - - return NyLPC_TBool_TRUE; + TcHeaderParser_t* inst=(TcHeaderParser_t*)i_inst; + if(inst->skip){ + //SKIPが有効な場合 + if(inst->length_of_url<0){ + inst->length_of_url++; + if(inst->length_of_url==0){ + inst->skip=NyLPC_TUInt8_FALSE; + } + } + return NyLPC_TBool_TRUE; + } + if((inst->mode & NyLPC_cModUrl_ParseMode_PATH_ONLY)==NyLPC_cModUrl_ParseMode_PATH_ONLY){ + if(strchr("?#",i_c)){ + inst->url_buf[inst->length_of_url]='\0'; + inst->skip=NyLPC_TUInt8_TRUE; + return NyLPC_TBool_TRUE;//Terminate + } + } + inst->url_buf[inst->length_of_url]=i_c; + if(i_c=='\0'){ + return NyLPC_TBool_TRUE;//Terminate + } + inst->length_of_url++; + if(inst->length_of_url==inst->length_of_buf){ + inst->reason=414; + return NyLPC_TBool_FALSE;//長すぎる。 + } + + return NyLPC_TBool_TRUE; } /** * デフォルトハンドラ */ static const struct NyLPC_TcHttpBasicHeaderParser_Handler _handler= { - NULL, - NyLPC_cModUrl_urlHandler + NULL, + NyLPC_cModUrl_urlHandler }; void NyLPC_cModUrl_initialize(NyLPC_TcModUrl_t* i_inst) { - NyLPC_cHttpBodyParser_initialize(&i_inst->_body_parser); + NyLPC_cHttpBodyParser_initialize(&i_inst->_body_parser); } void NyLPC_cModUrl_finalize(NyLPC_TcModUrl_t* i_inst) { - NyLPC_cHttpBodyParser_finalize(&i_inst->_body_parser); + NyLPC_cHttpBodyParser_finalize(&i_inst->_body_parser); } const struct NyLPC_THttpBasicHeader* NyLPC_cModUrl_getHeader(const NyLPC_TcModUrl_t* i_inst) { - return &(i_inst->_header); + return &(i_inst->_header); } /** @@ -104,68 +104,68 @@ const struct NyLPC_THttpBasicHeader* NyLPC_cModUrl_getHeader(const NyLPC_TcModUr */ NyLPC_THttpMethodType NyLPC_cModUrl_getMethod(const NyLPC_TcModUrl_t* i_inst) { - return i_inst->_header.startline.req.method; + return i_inst->_header.startline.req.method; } NyLPC_TBool NyLPC_cModUrl_execute2(NyLPC_TcModUrl_t* i_inst,NyLPC_TcHttpdConnection_t* i_connection,char* o_url_buf,NyLPC_TInt16 i_length_buf,NyLPC_TInt16 i_pass_prefix_len,NyLPC_cModUrl_ParseMode i_mode) { - TcHeaderParser_t parser; - NyLPC_Assert(i_length_buf>0); - NyLPC_Assert(i_pass_prefix_len>=0); - //コネクションのステータスチェック - if(!NyLPC_cHttpdConnection_getReqStatus(i_connection)==NyLPC_cHttpdConnection_ReqStatus_REQPARSE) - { - NyLPC_OnErrorGoto(Error1); - } - //リクエストParse済へ遷移 - NyLPC_cHttpdConnection_setReqStatusParsed(i_connection); - - NyLPC_cHttpBasicHeaderParser_initialize(&parser.super,&_handler); - parser.length_of_buf=i_length_buf; - parser.length_of_url=-i_pass_prefix_len;//無視するPrefix長 - parser.skip=(parser.length_of_url<0)?NyLPC_TUInt8_TRUE:NyLPC_TUInt8_FALSE;//スキップの初期値の設定 - parser.url_buf=o_url_buf; - parser.url_buf[0]='\0';//URL長<=prefix長に備えてNULLターミネイト - parser.reason=400; - parser.mode=i_mode; - //プリフェッチしたデータを流す - NyLPC_cHttpBasicHeaderParser_parseInit(&parser.super,&(i_inst->_header)); - NyLPC_cHttpdConnection_pushPrefetchInfo(i_connection,&parser.super,&(i_inst->_header)); - //後続をストリームから取り込む - if(!NyLPC_cHttpBasicHeaderParser_parseStream(&parser.super,NyLPC_cHttpdConnection_refStream(i_connection),&(i_inst->_header))){ - NyLPC_OnErrorGoto(Error2); - } - if(!NyLPC_cHttpBasicHeaderParser_parseFinish(&parser.super,&(i_inst->_header))){ - NyLPC_OnErrorGoto(Error2); - } - //@todo http/1.1 && POSTの場合はcontinueを送る。 - if(i_inst->_header.startline.req.version==NyLPC_THttpVersion_11){ - if(i_inst->_header.startline.req.method==NyLPC_THttpMethodType_POST) - { - NyLPC_cHttpdConnection_send100Continue(i_connection); - } - } - //Request::ConnectionがClose設定,又はHTTP1.1では無い場合,CLOSE - if(i_inst->_header.connection==NyLPC_THttpMessgeHeader_Connection_CLOSE || i_inst->_header.startline.req.version!=NyLPC_THttpVersion_11) - { - NyLPC_cHttpdConnection_setConnectionMode(i_connection,NyLPC_TcHttpdConnection_CONNECTION_MODE_CLOSE); - } - - NyLPC_cHttpBasicHeaderParser_finalize(&parser); - //BodyParserの初期化 - if(NyLPC_cHttpBodyParser_getState(&i_inst->_body_parser)!=NyLPC_TcHttpBasicBodyParser_ST_NULL){ - NyLPC_cHttpBodyParser_parseFinish(&i_inst->_body_parser); - } - NyLPC_cHttpBodyParser_parseInit(&i_inst->_body_parser,&i_inst->_header); - return NyLPC_TBool_TRUE; + TcHeaderParser_t parser; + NyLPC_Assert(i_length_buf>0); + NyLPC_Assert(i_pass_prefix_len>=0); + //コネクションのステータスチェック + if(!NyLPC_cHttpdConnection_getReqStatus(i_connection)==NyLPC_cHttpdConnection_ReqStatus_REQPARSE) + { + NyLPC_OnErrorGoto(Error1); + } + //リクエストParse済へ遷移 + NyLPC_cHttpdConnection_setReqStatusParsed(i_connection); + + NyLPC_cHttpBasicHeaderParser_initialize(&parser.super,&_handler); + parser.length_of_buf=i_length_buf; + parser.length_of_url=-i_pass_prefix_len;//無視するPrefix長 + parser.skip=(parser.length_of_url<0)?NyLPC_TUInt8_TRUE:NyLPC_TUInt8_FALSE;//スキップの初期値の設定 + parser.url_buf=o_url_buf; + parser.url_buf[0]='\0';//URL長<=prefix長に備えてNULLターミネイト + parser.reason=400; + parser.mode=i_mode; + //プリフェッチしたデータを流す + NyLPC_cHttpBasicHeaderParser_parseInit(&parser.super,&(i_inst->_header)); + NyLPC_cHttpdConnection_pushPrefetchInfo(i_connection,&parser.super,&(i_inst->_header)); + //後続をストリームから取り込む + if(!NyLPC_cHttpBasicHeaderParser_parseStream(&parser.super,NyLPC_cHttpdConnection_refStream(i_connection),&(i_inst->_header))){ + NyLPC_OnErrorGoto(Error2); + } + if(!NyLPC_cHttpBasicHeaderParser_parseFinish(&parser.super,&(i_inst->_header))){ + NyLPC_OnErrorGoto(Error2); + } + //@todo http/1.1 && POSTの場合はcontinueを送る。 + if(i_inst->_header.startline.req.version==NyLPC_THttpVersion_11){ + if(i_inst->_header.startline.req.method==NyLPC_THttpMethodType_POST) + { + NyLPC_cHttpdConnection_send100Continue(i_connection); + } + } + //Request::ConnectionがClose設定,又はHTTP1.1では無い場合,CLOSE + if(i_inst->_header.connection==NyLPC_THttpMessgeHeader_Connection_CLOSE || i_inst->_header.startline.req.version!=NyLPC_THttpVersion_11) + { + NyLPC_cHttpdConnection_setConnectionMode(i_connection,NyLPC_TcHttpdConnection_CONNECTION_MODE_CLOSE); + } + + NyLPC_cHttpBasicHeaderParser_finalize(&parser); + //BodyParserの初期化 + if(NyLPC_cHttpBodyParser_getState(&i_inst->_body_parser)!=NyLPC_TcHttpBasicBodyParser_ST_NULL){ + NyLPC_cHttpBodyParser_parseFinish(&i_inst->_body_parser); + } + NyLPC_cHttpBodyParser_parseInit(&i_inst->_body_parser,&i_inst->_header); + return NyLPC_TBool_TRUE; Error2: - //400Error - NyLPC_cHttpdUtils_sendErrorResponse(i_connection,parser.reason); - NyLPC_cHttpBasicHeaderParser_finalize(&parser); + //400Error + NyLPC_cHttpdUtils_sendErrorResponse(i_connection,parser.reason); + NyLPC_cHttpBasicHeaderParser_finalize(&parser); Error1: - return NyLPC_TBool_FALSE; + return NyLPC_TBool_FALSE; } @@ -174,15 +174,15 @@ Error1: NyLPC_TInt16 NyLPC_cModUrl_readBody(NyLPC_TcModUrl_t* i_inst,NyLPC_TcHttpdConnection_t* i_connection,void* i_buf,NyLPC_TInt16 i_buf_size) { - NyLPC_TInt16 l; - //リクエストは解析済であること - if(!NyLPC_cHttpdConnection_getReqStatus(i_connection)==NyLPC_cHttpdConnection_ReqStatus_END) - { - return -1; - } - if(!NyLPC_cHttpBodyParser_parseStream(&i_inst->_body_parser,NyLPC_cHttpdConnection_refStream(i_connection),i_buf,i_buf_size,&l)){ - return -1; - } - return l; + NyLPC_TInt16 l; + //リクエストは解析済であること + if(!NyLPC_cHttpdConnection_getReqStatus(i_connection)==NyLPC_cHttpdConnection_ReqStatus_END) + { + return -1; + } + if(!NyLPC_cHttpBodyParser_parseStream(&i_inst->_body_parser,NyLPC_cHttpdConnection_refStream(i_connection),i_buf,i_buf_size,&l)){ + return -1; + } + return l; } diff --git a/lib/src/net/httpd/mod/NyLPC_cModUrl.h b/lib/src/net/httpd/mod/NyLPC_cModUrl.h index d9d7763..2e96305 100644 --- a/lib/src/net/httpd/mod/NyLPC_cModUrl.h +++ b/lib/src/net/httpd/mod/NyLPC_cModUrl.h @@ -19,8 +19,8 @@ * along with this program. If not, see . * * For further information please contact. - * http://nyatla.jp/ - * or + * http://nyatla.jp/ + * or * *********************************************************************************/ #ifndef NYLPC_CMODURL_H_ @@ -56,8 +56,8 @@ typedef struct NyLPC_TcModUrl NyLPC_TcModUrl_t; struct NyLPC_TcModUrl { - struct NyLPC_THttpBasicHeader _header; - NyLPC_TcHttpBodyParser_t _body_parser; + struct NyLPC_THttpBasicHeader _header; + NyLPC_TcHttpBodyParser_t _body_parser; }; /** diff --git a/lib/src/net/httpd/mod/NyLPC_cModWebSocket.c b/lib/src/net/httpd/mod/NyLPC_cModWebSocket.c index 95786a9..1ef7ef8 100644 --- a/lib/src/net/httpd/mod/NyLPC_cModWebSocket.c +++ b/lib/src/net/httpd/mod/NyLPC_cModWebSocket.c @@ -663,7 +663,7 @@ NyLPC_TBool NyLPC_cModWebSocket_writePayloadHeader(NyLPC_TcModWebSocket_t* i_ins s=2; }else{ w[1]=126; - s=3; + s=4; *((NyLPC_TUInt16*)(&(w[2])))=NyLPC_htons(i_len); } if(!NyLPC_iHttpPtrStream_write(NyLPC_cHttpdConnection_refStream(i_inst->_ref_connection),w,s)){ diff --git a/lib/src/net/mdns/NyLPC_cMDnsServer.c b/lib/src/net/mdns/NyLPC_cMDnsServer.c index a379a9f..62d9efe 100644 --- a/lib/src/net/mdns/NyLPC_cMDnsServer.c +++ b/lib/src/net/mdns/NyLPC_cMDnsServer.c @@ -19,12 +19,12 @@ * along with this program. If not, see . * * For further information please contact. - * http://nyatla.jp/ - * or + * http://nyatla.jp/ + * or * *********************************************************************************/ #include "NyLPC_cMDnsServer.h" -#include "NyLPC_uipService.h" +#include "NyLPC_netif.h" #include "NyLPC_http.h" #include "NyLPC_utils.h" #include @@ -34,20 +34,21 @@ /** * mDNSのポート番号 */ -#define MDNS_MCAST_PORT 5353 -static const struct NyLPC_TIPv4Addr MDNS_MCAST_IPADDR=NyLPC_TIPv4Addr_pack(224,0,0,251); +#define MDNS_MCAST_PORT 5353 +static const struct NyLPC_TIPv4Addr MDNS_MCAST_IPADDR = NyLPC_TIPv4Addr_pack(224, 0, 0, 251); #define TIMEOUT_IN_MS 1000 -#define NyLPC_TcMDns_TTL 120 +#define NyLPC_TcMDns_RES_TTL (120) //120 +#define NyLPC_TcMDns_STD_TTL (30*60) //(30min struct NyLPC_TDnsHeader { - NyLPC_TUInt16 id; - NyLPC_TUInt16 flag; - NyLPC_TUInt16 qd; - NyLPC_TUInt16 an; - NyLPC_TUInt16 ns; - NyLPC_TUInt16 ar; + NyLPC_TUInt16 id; + NyLPC_TUInt16 flag; + NyLPC_TUInt16 qd; + NyLPC_TUInt16 an; + NyLPC_TUInt16 ns; + NyLPC_TUInt16 ar; }PACK_STRUCT_END; #define NyLPC_TDnsHeader_FLAG_MASK_QR 0x8000 @@ -61,66 +62,191 @@ struct NyLPC_TDnsHeader struct NyLPC_TDnsQuestion { - const char* qname; - NyLPC_TUInt16 qtype; - NyLPC_TUInt16 qclass; + const char* buf; //Questionパケットの先頭 + NyLPC_TUInt16 buf_len; //パケット長さ + NyLPC_TUInt16 qtype; + NyLPC_TUInt16 qclass; + NyLPC_TInt16 qname_pos;//Qnameの開始位置 }; -#define NyLPC_TDnsQuestion_QTYPR_A 1 -#define NyLPC_TDnsQuestion_QTYPR_NS 2 -#define NyLPC_TDnsQuestion_QTYPR_CNAME 5 -#define NyLPC_TDnsQuestion_QTYPR_SOA 6 -#define NyLPC_TDnsQuestion_QTYPR_PTR 12 -#define NyLPC_TDnsQuestion_QTYPR_MX 15 -#define NyLPC_TDnsQuestion_QTYPR_TXT 16 -#define NyLPC_TDnsQuestion_QTYPR_ANY 255 -#define NyLPC_TDnsQuestion_QCLASS_IN 1 -#define NyLPC_TDnsQuestion_QCLASS_CH 3 -#define NyLPC_TDnsQuestion_QCLASS_HS 4 -#define NyLPC_TDnsQuestion_QTYPR_SRV 33 + +#define NyLPC_TDnsQuestion_QTYPR_A 1 +#define NyLPC_TDnsQuestion_QTYPR_NS 2 +#define NyLPC_TDnsQuestion_QTYPR_CNAME 5 +#define NyLPC_TDnsQuestion_QTYPR_SOA 6 +#define NyLPC_TDnsQuestion_QTYPR_PTR 12 +#define NyLPC_TDnsQuestion_QTYPR_MX 15 +#define NyLPC_TDnsQuestion_QTYPR_TXT 16 +#define NyLPC_TDnsQuestion_QTYPR_ANY 255 +#define NyLPC_TDnsQuestion_QTYPR_AAAA 28 +#define NyLPC_TDnsQuestion_QTYPR_NSEC 47 +#define NyLPC_TDnsQuestion_QCLASS_IN 1 +#define NyLPC_TDnsQuestion_QCLASS_CH 3 +#define NyLPC_TDnsQuestion_QCLASS_HS 4 +#define NyLPC_TDnsQuestion_QTYPR_SRV 33 +#define NyLPC_TDnsQuestion_QCLASS_CACHE_FLUSH 0x8000 + /************************************************** - * TLabelCache - **************************************************/ +* TLabelCache +**************************************************/ +/** + * nameフィールドの文字列圧縮を解除して圧縮後のテキストポインタを返します。 + */ +static const char* getExtractNamePos(const char* i_packet_buf, const char* i_spos) +{ + NyLPC_TUInt8 limit = 0; + const char* s = i_spos;// question->buf + pos;//クエリの解析位置 + for (;;){ + switch (*(const NyLPC_TUInt8*)s){ + case 0x00: + //queryが先に終了に到達するのはおかしい。 + return NULL; + case 0xc0: + s = i_packet_buf + *((const NyLPC_TUInt8*)s + 1);//参照先にジャンプ + if (i_spos <= s){ + //後方参照ならエラー + return NULL; + } + limit++; + if (limit > 32){ + return NULL; + } + continue; + default: + break; + } + break; + } + return s; +} -struct TLabelCache +/** + * 展開しない圧縮文字列の長さを返す + */ +static NyLPC_TInt16 sizeofCompressName(const char* i_str) { - NyLPC_TUInt16 idx; - const char* str; -}; -static void TLabelCache_reset(struct TLabelCache* i_struct) + NyLPC_TInt16 l = 0; + NyLPC_TUInt8 c; + for (;;){ + c = (NyLPC_TUInt8)*(i_str + l); + switch (c){ + case 0xc0: + return l + 2; + case 0x00: + return l + 1; + } + l += c + 1; + } +} +/** + * @return + * 圧縮するとtrue i_nameは2文字に圧縮される。 + */ +static NyLPC_TBool compressNameB(char* i_packet, NyLPC_TUInt16 i_spos, NyLPC_TUInt16 i_name_pos) +{ + NyLPC_TUInt16 ret; + const char* p; //プロトコル文字列の解析開始位置 + const char* s = i_packet + i_spos; //クエリの解析位置 + const char* s2; + + //Protocol + for (;;){ + //0xc0参照の解決 + s = getExtractNamePos(i_packet, s); + if (s == NULL){ + //一致しない + return 0; + } + for (;;){ + //検索位置のドメインを遡る + if (*s == *(i_packet + i_name_pos)){ + //先頭一致 + p = i_packet + i_name_pos; + s2 = getExtractNamePos(i_packet, s); + ret = (NyLPC_TUInt16)(s2 - i_packet); + for (;;){ + //一致判定 + if (s2 == NULL){ + break; + } + + if (memcmp(p, s2, (*(NyLPC_TUInt8*)s2 + 1)) != 0){ + //不一致 + break; + } + //検出位置の移動 + p += (*(NyLPC_TUInt8*)s2) + 1; + s2 += (*(NyLPC_TUInt8*)s2) + 1; + if (*p == 0 && *s2 == 0){ + //charで扱ってるクライアントのコケ防止 + if (ret>0xff){ + return 0; + } + *(i_packet + i_name_pos + 0) = 0xc0; + *(i_packet + i_name_pos + 1) = (NyLPC_TUInt8)ret; + return ret; + } + s2 = getExtractNamePos(i_packet, s2); + } + } + //一致しない->検索パターンの次のフラグメントを調べる + s += (*(NyLPC_TUInt8*)s) + 1; + break; + } + } +} +/** + * mDNSパケットからi_nameを検索する。 + * i_targetはi_packetに含まれ、NULL終端されていること + * @return 0 圧縮失敗/その他:圧縮後のパケットサイズ + * + */ +static NyLPC_TUInt16 compressNameA(char* i_packet, NyLPC_TUInt16 i_name_start, NyLPC_TUInt16 i_name_pos) { - i_struct->idx=0; - i_struct->str=NULL; + NyLPC_TUInt16 s = 12; + for (;;){ + if (i_name_start <= s){ + //圧縮対象文字列に到達 + return 0; + } + //i_targetとAnswer文字列が等しいか確認 + if (compressNameB(i_packet, s, i_name_pos)){ + return i_name_pos + 2; + } + // + s += sizeofCompressName(i_packet + s); + s += 2 + 2 + 4 + 2; + if (i_name_start <= s){ + //圧縮対象文字列に到達 + return 0; + } + // //データは圧縮ポインタにしない方がいいらしい。 + // if (compressNameB(i_packet, s, i_name_pos)){ + // return i_name_pos + 2; + // } + s += NyLPC_ntohs(*((NyLPC_TUInt16*)(i_packet + s - 2))); + } } -static NyLPC_TInt16 TLabelCache_compress(struct TLabelCache* i_struct,NyLPC_TChar* i_src) -{ - NyLPC_TInt16 s; - NyLPC_TInt16 k; - const NyLPC_TChar* q; - const NyLPC_TChar* d; - //初めてのインデクスは保存 - if(i_struct->idx==0){ - i_struct->idx=12; - i_struct->str=i_src; - }else{ - d=i_struct->str; - do{ - q=i_src; - do{ - if(strcmp(q,d)==0){ - //一致,インデクスを計算 - s=(NyLPC_TInt16)(q-i_src); - k=(NyLPC_TInt16)(d-i_struct->str); - //新しい長さを返す - *((NyLPC_TInt16*)(i_src+s))=NyLPC_HTONS(0xC000|(i_struct->idx+k)); - return s+2; - } - q+=*q+1; - }while(*q!=0); - d+=*d+1; - }while(*d!=0); - } - return (NyLPC_TInt16)(strlen(i_src)+1); +/** + * @return + * 新しいi_packetの長さ + */ +static NyLPC_TUInt16 compressName(char* i_packet, NyLPC_TUInt16 i_name_pos, NyLPC_TUInt16 i_name_len) +{ + NyLPC_TUInt16 p; + NyLPC_TUInt16 s = i_name_pos; + for (;;){ + p = compressNameA(i_packet, i_name_pos, s); + //i_targetとAnswer文字列が等しいか確認 + if (p != 0){ + return p; + } + s += (NyLPC_TUInt16)*(i_packet + s) + 1; + if (*(i_packet + s) == 0){ + //圧縮対象文字列に到達 + return (i_name_len + i_name_pos); + } + } } @@ -133,321 +259,510 @@ static NyLPC_TInt16 TLabelCache_compress(struct TLabelCache* i_struct,NyLPC_TCha * 受領可能なQuestionの数 * */ -static NyLPC_TUInt16 getNumberOfQuestion(const void* i_packet,NyLPC_TUInt16 i_len) -{ - struct NyLPC_TDnsHeader* ptr=(struct NyLPC_TDnsHeader*)i_packet; - NyLPC_TUInt16 t; - if(i_len<12){ - return NyLPC_TBool_FALSE; - } - //questrionの確認 - //QR==0 && op==0 && tc=0 - t=NyLPC_ntohs(ptr->flag); - if( ((t & NyLPC_TDnsHeader_FLAG_MASK_QR)!=0) && - ((t & NyLPC_TDnsHeader_FLAG_MASK_OPCODE)!=0) && - ((t & NyLPC_TDnsHeader_FLAG_MASK_TC)!=0)) - { - //this is response - return 0; - } - return NyLPC_ntohs(ptr->qd); -} - -static NyLPC_TInt16 NyLPC_TDnsQuestion_parse(const void* i_qpacket,NyLPC_TUInt16 i_len,struct NyLPC_TDnsQuestion* o_val) -{ - NyLPC_TUInt16 l; - NyLPC_TUInt16 i; - const char* p=(const char*)i_qpacket; - //QNameのパース'0終端' - l=i_len-4-1;//スキャン長はパケットサイズ-5まで - for(i=0;iqname=p; - p+=l; - o_val->qtype=NyLPC_ntohs(*(NyLPC_TUInt16*)p); - o_val->qclass=NyLPC_ntohs(*(NyLPC_TUInt16*)(p+sizeof(NyLPC_TUInt16))); - //OK - return l+4; - } - } - return 0; +static NyLPC_TUInt16 getNumberOfQuestion(const void* i_packet, NyLPC_TUInt16 i_len) +{ + struct NyLPC_TDnsHeader* ptr = (struct NyLPC_TDnsHeader*)i_packet; + NyLPC_TUInt16 t; + if (i_lenflag); + if (((t & NyLPC_TDnsHeader_FLAG_MASK_QR) != 0) && + ((t & NyLPC_TDnsHeader_FLAG_MASK_OPCODE) != 0) && + ((t & NyLPC_TDnsHeader_FLAG_MASK_TC) != 0)) + { + //this is response + return 0; + } + return NyLPC_ntohs(ptr->qd); } /** - * .区切りラベル文字列とDNSラベル文字列を比較する。 - * @param record - * .区切りのドメイン名 - * @param question - * QuestionName文字列 + * [i_name].[i_protocol].localをquestionと比較します。i_nameは省略ができます。 + * i_nameはincasesensitiveです。 + * @return 等しい場合true */ -inline static NyLPC_TBool isEqualName(const char* record,const char* question) -{ - char s; - const char* q=question; - const char* m=record; - for(;;){ - if(*m=='\0'){ - //questionは最後のフラグメントが残るはず - return memcmp(q,"\5local\0",7)==0; - } - s=*q; - switch(*q) - { - case 0x00: - return NyLPC_TBool_FALSE; - case 0xc0: - return NyLPC_TBool_FALSE;//offset - default: - q++; - if(strncmp(m,q,s)!=0){ - return NyLPC_TBool_FALSE; - } - m+=s; - q+=s; - } - m++; - } +static NyLPC_TBool NyLPC_TDnsQuestion_isEqualName(const struct NyLPC_TDnsQuestion* question, const char* i_name, const char* i_protocol) +{ + NyLPC_TUInt8 tmp; + const char* p; //プロトコル文字列の解析開始位置 + const char* s = question->buf + (NyLPC_TUInt8)question->qname_pos; //クエリの解析位置 + + //Domain + if (i_name != NULL){ + //0xc0参照の解決 + s = getExtractNamePos(question->buf, s); + if (s == NULL){ + return NyLPC_TBool_FALSE; + } + tmp = (NyLPC_TUInt8)strlen(i_name); + if (tmp != *s || NyLPC_strnicmp(s + 1, i_name, tmp) != 0){ + return NyLPC_TBool_FALSE; + } + s += (*s) + 1; + } + else{ + s = question->buf + (NyLPC_TUInt8)question->qname_pos;//クエリの解析位置 + } + p = i_protocol; + //Protocol + for (;;){ + //0xc0参照の解決 + s = getExtractNamePos(question->buf, s); + if (s == NULL){ + return NyLPC_TBool_FALSE; + } + //SRVの末端到達 + if (*p == 0){ + if (question->buf + question->buf_lenbuf + question->buf_lenbuf = i_packet; + o_val->buf_len = i_packet_len; + o_val->qname_pos = i_parse_start; + o_val->qtype = NyLPC_ntohs(*(NyLPC_TUInt16*)(i_packet + i_parse_start + qlen)); + o_val->qclass = NyLPC_ntohs(*(NyLPC_TUInt16*)(i_packet + i_parse_start + qlen + sizeof(NyLPC_TUInt16))); + return qlen + 4; + } + return 0; } + /** * DNSレコードのPRTフィールドとDNSラベル文字列を比較する。 */ -static NyLPC_TInt16 NyLPC_TDnsRecord_getMatchPtrIdx(const struct NyLPC_TDnsRecord* i_struct,const NyLPC_TChar* question) -{ - NyLPC_TInt16 i; - for(i=0;inum_of_srv;i++){ - if(isEqualName(i_struct->srv[i].protocol,question)){ - return i; - } - } - return -1; -} -static NyLPC_TInt16 NyLPC_TDnsRecord_getMatchSrvIdx(const struct NyLPC_TDnsRecord* i_struct,const NyLPC_TChar* question) -{ - NyLPC_TInt16 i; - NyLPC_TInt16 l=(NyLPC_TInt16)strlen(i_struct->name); - //Aレコードをチェック - if(l!=question[0] || memcmp(question+1,i_struct->name,l)!=0){ - return -1; - } - for(i=0;inum_of_srv;i++){ - if(isEqualName(i_struct->srv[i].protocol,question+l+1)){ - return i; - } - } - return -1; +static NyLPC_TInt16 NyLPC_TDnsRecord_getMatchPtrIdx(const struct NyLPC_TDnsRecord* i_struct, const struct NyLPC_TDnsQuestion* question) +{ + NyLPC_TInt16 i; + for (i = 0; inum_of_srv; i++){ + if (NyLPC_TDnsQuestion_isEqualName(question, NULL, i_struct->srv[i].protocol)){ + return i; + } + } + return -1; } -/** - * _services._dns-sd._udp.localであるか確認する。 - */ -static NyLPC_TBool NyLPC_TDnsRecord_isServicesDnsSd(const NyLPC_TChar* question) +static NyLPC_TInt16 NyLPC_TDnsRecord_getMatchSrvIdx(const struct NyLPC_TDnsRecord* i_struct, const struct NyLPC_TDnsQuestion* question) { - return memcmp(question,"\x09_services\x07_dns-sd\x04_udp\5local\x00",30)==0; + NyLPC_TInt16 i; + for (i = 0; inum_of_srv; i++){ + if (NyLPC_TDnsQuestion_isEqualName(question, i_struct->name, i_struct->srv[i].protocol)){ + return i; + } + } + return -1; } + /** * '.'区切り文字列をDNS形式の[n]text[n]text\0へ変換する。 * @return * 変換後のデータブロックの長さin byte * 終端の\0の長さを含みます。 */ -static NyLPC_TInt16 str2label(NyLPC_TChar* buf,const NyLPC_TChar* name) -{ - //proto文字列の変換 - NyLPC_TChar* lp; - const NyLPC_TChar* n=name; - NyLPC_TChar* b=buf; - while(*n!='\0'){ - lp=b; - b++; - for(;strchr(".\0",*n)==NULL;){ - *b=*n; - b++; - n++; - } - *lp=(char)(b-lp-1); - if(*n!='\0'){ - n++; - } - } - *b='\0'; - b++; - return b-buf; +static NyLPC_TInt16 str2label(NyLPC_TChar* buf, const NyLPC_TChar* name) +{ + //proto文字列の変換 + NyLPC_TChar* lp; + const NyLPC_TChar* n = name; + NyLPC_TChar* b = buf; + while (*n != '\0'){ + lp = b; + b++; + for (; strchr(".\0", *n) == NULL;){ + *b = *n; + b++; + n++; + } + *lp = (char)(b - lp - 1); + if (*n != '\0'){ + n++; + } + } + *b = '\0'; + b++; + return (NyLPC_TInt16)((int)b - (int)buf); } - /** - * ResourceHeaderのライタ + * query文字列をパケットに追記します。 + * @return 出力したバイト数 */ -static NyLPC_TInt16 writeResourceHeader(char* buf,NyLPC_TInt16 len,const char* i_name,NyLPC_TUInt16 i_type,NyLPC_TUInt16 i_class) -{ - NyLPC_TInt16 s; - NyLPC_TInt16 l=1+(NyLPC_TInt16)strlen(i_name)+1+5+1; - if(lenqname_pos; + NyLPC_TInt16 l = 0;//出力した文字数 + for (;;){ + n = getExtractNamePos(i_query->buf, i_query->buf + s); + if (n == NULL){ + *(i_packet + i_spos + l) = 0; + l++; + break; + } + else{ + memcpy(i_packet + i_spos + l, n, ((NyLPC_TUInt8)*n) + 1); + l += ((NyLPC_TUInt8)*n) + 1; + s = (NyLPC_TInt16)((int)n - (int)i_query->buf + ((NyLPC_TUInt8)*n) + 1); + if (obuflenname) + 1 + strlen(i_recode->srv[i_sid].protocol) + 1 + 5 + 1); + if (buflenname) - 1; + s += str2label(i_packet + i_spos + s, i_recode->srv[i_sid].protocol) - 1; + s += str2label(i_packet + i_spos + s, "local"); + l = compressName(i_packet, i_spos, s);//圧縮 + (*(NyLPC_TUInt16*)(i_packet + l)) = NyLPC_HTONS(i_type); + (*(NyLPC_TUInt16*)(i_packet + l + 2)) = NyLPC_HTONS(i_class); + (*(NyLPC_TUInt32*)(i_packet + l + 4)) = NyLPC_HTONL(i_ttl); + return l + 2 + 2 + 4; +} + +/** +* パケットヘッダを書き込みます。 +* @return パケットサイズ +*/ +static NyLPC_TUInt16 setResponseHeader(char* i_packet, const struct NyLPC_TDnsHeader* i_in_dns_header, NyLPC_TUInt16 i_an_count, NyLPC_TUInt16 i_ns_count, NyLPC_TUInt16 i_ar_count) +{ + struct NyLPC_TDnsHeader* p = (struct NyLPC_TDnsHeader*)i_packet; + if (i_in_dns_header != NULL){ + memcpy(p, i_in_dns_header, sizeof(struct NyLPC_TDnsHeader)); + p->flag = p->flag | NyLPC_HTONS(NyLPC_TDnsHeader_FLAG_MASK_QR | NyLPC_TDnsHeader_FLAG_MASK_AA); + p->flag = p->flag & NyLPC_HTONS(~(NyLPC_TDnsHeader_FLAG_MASK_RECODE | NyLPC_TDnsHeader_FLAG_MASK_TC | NyLPC_TDnsHeader_FLAG_MASK_RA)); + } + else{ + p->flag = 0; + p->id = 0; + } + p->qd = 0; + p->an = NyLPC_HTONS(i_an_count); + p->ns = NyLPC_HTONS(i_ns_count); + p->ar = NyLPC_HTONS(i_ar_count); + return sizeof(struct NyLPC_TDnsHeader); +} +static NyLPC_TUInt16 setAnnounceHeader(char* i_packet,NyLPC_TUInt16 i_an_count, NyLPC_TUInt16 i_ns_count, NyLPC_TUInt16 i_ar_count) +{ + struct NyLPC_TDnsHeader* p = (struct NyLPC_TDnsHeader*)i_packet; + p->id = 0; + p->flag = NyLPC_HTONS(NyLPC_TDnsHeader_FLAG_MASK_QR | NyLPC_TDnsHeader_FLAG_MASK_AA); + p->qd = 0; + p->an = NyLPC_HTONS(i_an_count); + p->ns = NyLPC_HTONS(i_ns_count); + p->ar = NyLPC_HTONS(i_ar_count); + return sizeof(struct NyLPC_TDnsHeader); } -inline static NyLPC_TInt16 writeSrvResourceHeader(char* buf,NyLPC_TInt16 len,const struct NyLPC_TDnsRecord* i_recode,int i_sid,NyLPC_TUInt16 i_type,NyLPC_TUInt16 i_class,struct TLabelCache* i_ca) -{ - NyLPC_TInt16 l=(NyLPC_TInt16)(1+strlen(i_recode->name)+1+strlen(i_recode->srv[i_sid].protocol)+1+5+1); - if(lenname)-1; - l+=str2label(buf+l,i_recode->srv[i_sid].protocol)-1; - l+=str2label(buf+l,"local"); - l=TLabelCache_compress(i_ca,buf);//圧縮 - (*(NyLPC_TUInt16*)(buf+l))=NyLPC_HTONS(i_type); - (*(NyLPC_TUInt16*)(buf+l+2))=NyLPC_HTONS(i_class); - (*(NyLPC_TUInt32*)(buf+l+4))=NyLPC_HTONL(NyLPC_TcMDns_TTL); - return l+2+2+4; +static NyLPC_TInt16 writeARecordData(char* i_packet, NyLPC_TInt16 i_spos,NyLPC_TInt16 obuflen,const struct NyLPC_TIPv4Addr* ip) +{ + NyLPC_TInt16 l=i_spos; + (*(NyLPC_TUInt16*)(i_packet + l)) = NyLPC_HTONS(NyLPC_TDnsQuestion_QTYPR_A); + (*(NyLPC_TUInt16*)(i_packet + l + 2)) = NyLPC_HTONS(NyLPC_TDnsQuestion_QCLASS_IN | NyLPC_TDnsQuestion_QCLASS_CACHE_FLUSH); + (*(NyLPC_TUInt32*)(i_packet + l + 4)) = NyLPC_HTONL(NyLPC_TcMDns_RES_TTL); + l += 2 + 2 + 4; + //A record header + if (obuflenv; + return l + 6;//NEXT_SPOS +} +/** +* ドメイン名からAレコードレスポンスを書きだす。 +*/ +static NyLPC_TInt16 writeARecord(char* i_packet, NyLPC_TInt16 i_spos, NyLPC_TInt16 obuflen, const NyLPC_TChar* i_name, const struct NyLPC_TIPv4Addr* ip) +{ + //AnswerはAレコードのみ + NyLPC_TInt16 s = 1 + (NyLPC_TInt16)strlen(i_name) + 1 + 5 + 1; + if (obuflenflag=p->flag | NyLPC_HTONS(NyLPC_TDnsHeader_FLAG_MASK_QR|NyLPC_TDnsHeader_FLAG_MASK_AA); - p->flag=p->flag & NyLPC_HTONS(~(NyLPC_TDnsHeader_FLAG_MASK_RECODE|NyLPC_TDnsHeader_FLAG_MASK_TC|NyLPC_TDnsHeader_FLAG_MASK_RA)); - }else{ - p->flag=0; - p->id=0; - } - p->qd=0; - p->an=NyLPC_HTONS(i_an_count); - p->ns=NyLPC_HTONS(i_ns_count); - p->ar=NyLPC_HTONS(i_ar_count); - return sizeof(struct NyLPC_TDnsHeader); +/** +* AレコードクエリからAレコードレスポンスを書きだす。 +*/ +static NyLPC_TInt16 writeARecordByQuery(char* i_packet, NyLPC_TInt16 i_spos, NyLPC_TInt16 obuflen, const struct NyLPC_TDnsQuestion* i_query, const struct NyLPC_TIPv4Addr* ip) +{ + NyLPC_TInt16 s = query2label(i_packet, i_spos, obuflen, i_query); + if (s == 0){ + return 0; + } + //レコード圧縮 + s = compressName(i_packet, i_spos, s); + return writeARecordData(i_packet,s,obuflen,ip); } -inline static NyLPC_TInt16 writeARecord(char* obuf,NyLPC_TInt16 obuflen,const NyLPC_TChar* a_rec,const struct NyLPC_TIPv4Addr* ip) -{ - NyLPC_TInt16 ret; - //AnswerはAレコードのみ - //A record header - ret=writeResourceHeader(obuf,obuflen,a_rec,NyLPC_TDnsQuestion_QTYPR_A,NyLPC_TDnsQuestion_QCLASS_IN); - if(ret==0 || obuflen-ret<8){ - return 0; - } - //Aレコードを書く - //IPADDR - (*(NyLPC_TUInt16*)(obuf+ret))=NyLPC_HTONS(4); - (*(NyLPC_TUInt32*)(obuf+ret+2))=ip->v; - return ret+8; + +/* +static NyLPC_TInt16 writeAAAARecordByQueryData(char* i_packet, NyLPC_TInt16 i_spos,NyLPC_TInt16 obuflen,const struct NyLPC_TIPv4Addr* ip) +{ + NyLPC_TInt16 l=i_spos; + (*(NyLPC_TUInt16*)(i_packet + l)) = NyLPC_HTONS(NyLPC_TDnsQuestion_QTYPR_AAAA); + (*(NyLPC_TUInt16*)(i_packet + l + 2)) = NyLPC_HTONS(NyLPC_TDnsQuestion_QCLASS_IN | NyLPC_TDnsQuestion_QCLASS_CACHE_FLUSH); + (*(NyLPC_TUInt32*)(i_packet + l + 4)) = NyLPC_HTONL(NyLPC_TcMDns_RES_TTL); + l += 2 + 2 + 4; + //A record header + if (obuflenv; + return l + 2 + 16; +}*/ + + +/** +* AレコードクエリからAレコードレスポンスを書きだす。 +*//* +static NyLPC_TInt16 writeAAAARecordByQuery(char* i_packet, NyLPC_TInt16 i_spos, NyLPC_TInt16 obuflen, const struct NyLPC_TDnsQuestion* i_query, const struct NyLPC_TIPv4Addr* ip) +{ + NyLPC_TInt16 s = query2label(i_packet, i_spos, obuflen, i_query); + if (s == 0){ + return 0; + } + //レコード圧縮 + s = compressName(i_packet, i_spos, s); + return writeAAAARecordByQueryData(i_packet,s,obuflen,ip); +}*/ +/** +* AレコードクエリからAレコードレスポンスを書きだす。 +*//* +static NyLPC_TInt16 writeAAAARecord(char* i_packet, NyLPC_TInt16 i_spos, NyLPC_TInt16 obuflen, const NyLPC_TChar* i_name, const struct NyLPC_TIPv4Addr* ip) +{ + NyLPC_TInt16 s = 1 + (NyLPC_TInt16)strlen(i_name) + 1 + 5 + 1; + if (obuflenprotocol)+1+5+1);//逆引き文字列の長さ(デリミタ×3+1) - if(obuflenprotocol)-1; - l+=str2label(obuf+l,"local"); - return l; +/** +* NSECレコードレスポンスを書きだす。 +* IPv6わからんし。 +*/ +static NyLPC_TInt16 writeNSECRecord(char* i_packet, NyLPC_TInt16 i_spos, NyLPC_TInt16 obuflen, const NyLPC_TChar* i_name) +{ + //AnswerはAレコードのみ + NyLPC_TInt16 s = 1 + (NyLPC_TInt16)strlen(i_name) + 1 + 5 + 1; + if (obuflensrv[i_sid].protocol)+1+5+1); - //Headerの長さチェック - if(obuflensrv[i_sid].protocol)-1; - s+=str2label(obuf+s,"local"); - s=TLabelCache_compress(i_ca,obuf); - (*(NyLPC_TUInt16*)(obuf+s))=NyLPC_HTONS(NyLPC_TDnsQuestion_QTYPR_PTR); - (*(NyLPC_TUInt16*)(obuf+s+2))=NyLPC_HTONS(NyLPC_TDnsQuestion_QCLASS_IN); - (*(NyLPC_TUInt32*)(obuf+s+4))=NyLPC_HTONS(NyLPC_TcMDns_TTL); - l=s+2+2+4; - - //Resourceの書込み - s=1+strlen(i_recode->name)+1+strlen(i_recode->srv[i_sid].protocol)+1+5+1;//逆引き文字列の長さ(デリミタ×3+1) - if(obuflenname)-1; - s+=str2label(obuf+l+s,i_recode->srv[i_sid].protocol)-1; - s+=str2label(obuf+l+s,"local"); - s=TLabelCache_compress(i_ca,obuf+l);//圧縮 - (*rlen)=NyLPC_ntohs(s); - return l+s; +/** +* NSECレコードレスポンスを書きだす。 +* IPv6わからんし。 +*/ +static NyLPC_TInt16 writeNSECRecordByQuery(char* i_packet, NyLPC_TInt16 i_spos, NyLPC_TInt16 obuflen, const struct NyLPC_TDnsQuestion* i_query) +{ + NyLPC_TInt16 s; + //AnswerはAレコードのみ + s = query2label(i_packet, i_spos, obuflen, i_query); + if (s == 0){ + return 0; + } + + //レコード圧縮 + s = i_spos + s; + // l=compressName(i_packet,i_spos,s); + return writeNSECRecordData(i_packet,s,obuflen,i_spos); } +static NyLPC_TInt16 writeSdPtrRecord(const struct NyLPC_TMDnsServiceRecord* i_srvlec, char* i_packet, NyLPC_TInt16 i_spos, NyLPC_TInt16 obuflen) +{ + NyLPC_TInt16 l, s; + NyLPC_TUInt16* rlen; + //Header + // s=(NyLPC_TInt16)*(i_question->buf+i_question->qname_pos); + //Headerの長さチェック + if (obuflen_ref_record,i_sid,NyLPC_TDnsQuestion_QTYPR_SRV,NyLPC_TDnsQuestion_QCLASS_IN,i_ca); - if(s==0){ - return 0; - } - - l=1+strlen(i_inst->_ref_record->a)+1+5+1;//逆引き文字列の長さ(デリミタ×3+1) - if(obuflen-s-l<8){ - return 0; - } - //IPADDR - rlen=(NyLPC_TUInt16*)(obuf+s); - (*(NyLPC_TUInt16*)(obuf+s+2))=NyLPC_HTONS(0);//Priority - (*(NyLPC_TUInt16*)(obuf+s+4))=NyLPC_HTONS(0);//Weight - (*(NyLPC_TUInt16*)(obuf+s+6))=NyLPC_HTONS(i_inst->_ref_record->srv[i_sid].port);//PORT - l=4*2+s; - s=str2label(obuf+l,i_inst->_ref_record->a)-1; - s+=str2label(obuf+l+s,"local"); - s=TLabelCache_compress(i_ca,obuf+l);//圧縮 - (*rlen)=NyLPC_HTONS(2+2+2+s); - return l+s; + //Resourceの書込み + s = (NyLPC_TInt16)(1 + strlen(i_srvlec->protocol) + 1 + 5 + 1);//逆引き文字列の長さ(デリミタ×3+1) + if (obuflenprotocol) - 1; + s += str2label(i_packet + l + s, "local"); + s = compressName(i_packet, l, s);//圧縮 + *rlen = NyLPC_ntohs(s - l); + return s; } -static NyLPC_TInt16 writeTXTRecord(NyLPC_TcMDnsServer_t* i_inst,NyLPC_TInt16 i_sid,char* obuf,NyLPC_TInt16 obuflen,struct TLabelCache* i_ca) -{ - NyLPC_TInt16 ret; - NyLPC_TInt16 l; - //Answer - ret=writeSrvResourceHeader(obuf,obuflen,i_inst->_ref_record,i_sid,NyLPC_TDnsQuestion_QTYPR_TXT,NyLPC_TDnsQuestion_QCLASS_IN,i_ca); - if(ret==0){ - return 0; - } - //name.proto.localを返す。 - if(obuflen-ret<2){ - return 0; - } - (*(NyLPC_TUInt16*)(obuf+ret))=NyLPC_ntohs(0); - //proto.name.local. - l=ret+2; - return l; + +static NyLPC_TInt16 writePtrRecord(const struct NyLPC_TDnsRecord* i_recode, NyLPC_TInt16 i_sid, char* i_packet, NyLPC_TInt16 i_spos, NyLPC_TInt16 obuflen) +{ + NyLPC_TInt16 l, s; + NyLPC_TUInt16* rlen; + //Header:開始文字数(1)+プレフィクス(n)+終端(1)+local(5)+1 + s = (NyLPC_TInt16)(1 + strlen(i_recode->srv[i_sid].protocol) + 1 + 5 + 1); + //Headerの長さチェック + if (obuflensrv[i_sid].protocol) - 1; + s += str2label(i_packet + i_spos + s, "local"); + s = compressName(i_packet, i_spos, s); + (*(NyLPC_TUInt16*)(i_packet + s)) = NyLPC_HTONS(NyLPC_TDnsQuestion_QTYPR_PTR); + (*(NyLPC_TUInt16*)(i_packet + s + 2)) = NyLPC_HTONS(NyLPC_TDnsQuestion_QCLASS_IN); + (*(NyLPC_TUInt32*)(i_packet + s + 4)) = NyLPC_HTONL(NyLPC_TcMDns_STD_TTL); + l = s + 2 + 2 + 4; + + //Resourceの書込み + s = (NyLPC_TInt16)(1 + strlen(i_recode->name) + 1 + strlen(i_recode->srv[i_sid].protocol) + 1 + 5 + 1);//逆引き文字列の長さ(デリミタ×3+1) + if (obuflenname) - 1; + s += str2label(i_packet + l + s, i_recode->srv[i_sid].protocol) - 1; + s += str2label(i_packet + l + s, "local"); + s = compressName(i_packet, l, s);//圧縮 + (*rlen) = NyLPC_ntohs(s - l); + return s; +} + +static NyLPC_TInt16 writeSRVRecord(NyLPC_TcMDnsServer_t* i_inst, NyLPC_TInt16 i_sid, char* i_packet, NyLPC_TUInt16 i_spos, NyLPC_TInt16 obuflen) +{ + NyLPC_TInt16 l, s; + NyLPC_TUInt16* rlen; + + //SRV Record + s = writeSrvResourceHeader(i_packet, i_spos, obuflen, i_inst->_ref_record, i_sid, NyLPC_TDnsQuestion_QTYPR_SRV, NyLPC_TDnsQuestion_QCLASS_IN | NyLPC_TDnsQuestion_QCLASS_CACHE_FLUSH,NyLPC_TcMDns_RES_TTL); + if (s == 0){ + return 0; + } + + l = (NyLPC_TInt16)(1 + strlen(i_inst->_ref_record->a) + 1 + 5 + 1);//逆引き文字列の長さ(デリミタ×3+1) + if (obuflen_ref_record->srv[i_sid].port);//PORT + l = 4 * 2 + s; + s = str2label(i_packet + l, i_inst->_ref_record->a) - 1; + s += str2label(i_packet + l + s, "local"); + s = compressName(i_packet, l, s);//圧縮 + (*rlen) = NyLPC_HTONS(2 + 2 + 2 + (s - l)); + return s; +} +static NyLPC_TInt16 writeTXTRecord(NyLPC_TcMDnsServer_t* i_inst, NyLPC_TInt16 i_sid, char* i_packet, NyLPC_TInt16 i_spos, NyLPC_TInt16 obuflen) +{ + NyLPC_TInt16 ret; + NyLPC_TInt16 l; + //Answer + ret = writeSrvResourceHeader(i_packet, i_spos, obuflen, i_inst->_ref_record, i_sid, NyLPC_TDnsQuestion_QTYPR_TXT, NyLPC_TDnsQuestion_QCLASS_IN | NyLPC_TDnsQuestion_QCLASS_CACHE_FLUSH,NyLPC_TcMDns_STD_TTL); + if (ret == 0){ + return 0; + } + //name.proto.localを返す。 + if (obuflen_ref_record->num_of_srv;i2++){ - TLabelCache_reset(&cache); - //Bufferの取得 - obuf=NyLPC_cUdpSocket_allocSendBuf(&(i_inst->_super),512,&obuflen,TIMEOUT_IN_MS); - if(obuf==NULL){ - return; - } - l=setResponseHeader(obuf,NULL,1,0,3); - s=writePtrRecord(i_inst->_ref_record,i2,obuf+l,obuflen-l,&cache); - if(s<=0){ - NyLPC_OnErrorGoto(ERROR); - } - l+=s; - s=writeSRVRecord(i_inst,i2,obuf+l,obuflen-l,&cache); - if(s<=0){ - NyLPC_OnErrorGoto(ERROR); - } - l+=s; - s=writeTXTRecord(i_inst,i2,obuf+l,obuflen-l,&cache); - if(s<=0){ - NyLPC_OnErrorGoto(ERROR); - } - l+=s; - //Aレコード - s=writeARecord(obuf+l,obuflen-l,i_inst->_ref_record->a,&(i_inst->_super.uip_udp_conn.lipaddr)); - if(s<=0){ - NyLPC_OnErrorGoto(ERROR); - } - l+=s; - if(!NyLPC_cUdpSocket_psend(&(i_inst->_super),&MDNS_MCAST_IPADDR,MDNS_MCAST_PORT,obuf,l)){ - NyLPC_OnErrorGoto(ERROR); - } - } - return; + char* obuf; + NyLPC_TUInt16 obuflen; + NyLPC_TUInt16 l; + int i,i2; + for(i2=0;i2_ref_record->num_of_srv;i2++){ + //Bufferの取得 + obuf=NyLPC_iUdpSocket_allocSendBuf(i_inst->_socket,512,&obuflen,TIMEOUT_IN_MS); + if(obuf==NULL){ + return; + } + l=setAnnounceHeader(obuf,1+i_inst->_ref_record->num_of_srv+4,0,0); + // + //PTR + l=writePtrRecord(i_inst->_ref_record,i2,obuf,l,obuflen); + //SD-PTR + if(l<=0){ + NyLPC_OnErrorGoto(ERROR); + } + for (i = 0; i_ref_record->num_of_srv; i++){ + l = writeSdPtrRecord(&(i_inst->_ref_record->srv[i]), obuf, l, obuflen); + if (l <= 0){ + NyLPC_OnErrorGoto(ERROR); + } + } + // + //SRV + l=writeSRVRecord(i_inst,i2,obuf,l,obuflen); + if(l<=0){ + NyLPC_OnErrorGoto(ERROR); + } + //TXT + l=writeTXTRecord(i_inst,i2,obuf,l,obuflen); + if(l<=0){ + NyLPC_OnErrorGoto(ERROR); + } + //Aレコード + l=writeARecord(obuf,l,obuflen,i_inst->_ref_record->a,NyLPC_iUdpSocket_getSockIP(i_inst->_socket)); + if(l<=0){ + NyLPC_OnErrorGoto(ERROR); + } +// //AAAAレコード +// l=writeAAAARecord(obuf,l,obuflen,i_inst->_ref_record->a,&(i_inst->_super.uip_udp_conn.lipaddr)); +// if(l<=0){ +// NyLPC_OnErrorGoto(ERROR); +// } + //NSEC + l=writeNSECRecord(obuf,l,obuflen,i_inst->_ref_record->a); + if(l<=0){ + NyLPC_OnErrorGoto(ERROR); + } + if(!NyLPC_iUdpSocket_psend(i_inst->_socket,&MDNS_MCAST_IPADDR,MDNS_MCAST_PORT,obuf,l)){ + NyLPC_OnErrorGoto(ERROR); + } + } + return; ERROR: - NyLPC_cUdpSocket_releaseSendBuf(&(i_inst->_super),obuf); - return; + NyLPC_iUdpSocket_releaseSendBuf(i_inst->_socket,obuf); + return; } -static void sendReply2(NyLPC_TcMDnsServer_t* i_inst,const struct NyLPC_TDnsHeader* i_dns_header,const struct NyLPC_TDnsQuestion* q) -{ - NyLPC_TInt16 ptr_recode; - NyLPC_TInt16 i2; - char* obuf; - NyLPC_TUInt16 obuflen; - NyLPC_TUInt16 l,s; - struct TLabelCache cache; - TLabelCache_reset(&cache); - //パケットヘッダの生成 - switch(q->qtype){ - case NyLPC_TDnsQuestion_QTYPR_SRV: - //SRV,A record - ptr_recode=NyLPC_TDnsRecord_getMatchSrvIdx(i_inst->_ref_record,q->qname); - if(ptr_recode<0){ - goto DROP; - } - //Bufferの取得 - obuf=NyLPC_cUdpSocket_allocSendBuf(&(i_inst->_super),512,&obuflen,0); - if(obuf==NULL){ - goto DROP; - } - l=setResponseHeader(obuf,i_dns_header,1,0,2); - s=writeSRVRecord(i_inst,ptr_recode,obuf+l,obuflen-l,&cache); - if(s<=0){ - NyLPC_OnErrorGoto(ERROR); - } - l+=s; - s=writeTXTRecord(i_inst,ptr_recode,obuf+l,obuflen-l,&cache); - if(s<=0){ - NyLPC_OnErrorGoto(ERROR); - } - l+=s; - //Aレコード - s=writeARecord(obuf+l,obuflen-l,i_inst->_ref_record->a,&(i_inst->_super.uip_udp_conn.lipaddr)); - if(s<=0){ - NyLPC_OnErrorGoto(ERROR); - } - l+=s; - break; - case NyLPC_TDnsQuestion_QTYPR_A: - //自分宛?(name.local) - if(!isEqualName(i_inst->_ref_record->a,q->qname)){ - goto DROP; - } - //Bufferの取得 - obuf=NyLPC_cUdpSocket_allocSendBuf(&(i_inst->_super),512,&obuflen,0); - if(obuf==NULL){ - goto DROP; - } - //Headerのコピー - l=setResponseHeader(obuf,i_dns_header,1,0,0); - //Aレコードのみ - s=writeARecord(obuf+l,obuflen-l,i_inst->_ref_record->a,&(i_inst->_super.uip_udp_conn.lipaddr)); - if(s<=0){ - NyLPC_OnErrorGoto(ERROR); - } - l+=s; - break; - case NyLPC_TDnsQuestion_QTYPR_PTR: - if(NyLPC_TDnsRecord_isServicesDnsSd(q->qname)){ - //Bufferの取得 - obuf=NyLPC_cUdpSocket_allocSendBuf(&(i_inst->_super),512,&obuflen,0); - if(obuf==NULL){ - goto DROP; - } - l=setResponseHeader(obuf,i_dns_header,i_inst->_ref_record->num_of_srv,0,0); - for(i2=0;i2_ref_record->num_of_srv;i2++){ - s=writeSdPtrRecord(q->qname,&(i_inst->_ref_record->srv[i2]),obuf+l,obuflen-l); - if(s<=0){ - NyLPC_OnErrorGoto(ERROR); - } - l+=s; - } - }else{ - //自分宛?(proto.local) - ptr_recode=NyLPC_TDnsRecord_getMatchPtrIdx(i_inst->_ref_record,q->qname); - if(ptr_recode<0){ - goto DROP; - } - //Bufferの取得 - obuf=NyLPC_cUdpSocket_allocSendBuf(&(i_inst->_super),512,&obuflen,0); - if(obuf==NULL){ - goto DROP; - } - l=setResponseHeader(obuf,i_dns_header,1,0,3); - s=writePtrRecord(i_inst->_ref_record,ptr_recode,obuf+l,obuflen-l,&cache); - if(s<=0){ - NyLPC_OnErrorGoto(ERROR); - } - l+=s; - s=writeSRVRecord(i_inst,ptr_recode,obuf+l,obuflen-l,&cache); - if(s<=0){ - NyLPC_OnErrorGoto(ERROR); - } - l+=s; - s=writeTXTRecord(i_inst,ptr_recode,obuf+l,obuflen-l,&cache); - if(s<=0){ - NyLPC_OnErrorGoto(ERROR); - } - l+=s; - //Aレコード - s=writeARecord(obuf+l,obuflen-l,i_inst->_ref_record->a,&(i_inst->_super.uip_udp_conn.lipaddr)); - if(s<=0){ - NyLPC_OnErrorGoto(ERROR); - } - l+=s; - } - break; - case NyLPC_TDnsQuestion_QTYPR_TXT: - //自分宛?(proto.local) - ptr_recode=NyLPC_TDnsRecord_getMatchSrvIdx(i_inst->_ref_record,q->qname); - if(ptr_recode<0){ - goto DROP; - } - //Bufferの取得 - obuf=NyLPC_cUdpSocket_allocSendBuf(&(i_inst->_super),512,&obuflen,0); - l=setResponseHeader(obuf,i_dns_header,1,0,1); - s=writeTXTRecord(i_inst,ptr_recode,obuf+l,obuflen-l,&cache); - if(s<=0){ - NyLPC_OnErrorGoto(ERROR); - } - l+=s; - s=writeARecord(obuf+l,obuflen-l,i_inst->_ref_record->a,&(i_inst->_super.uip_udp_conn.lipaddr)); - if(s<=0){ - NyLPC_OnErrorGoto(ERROR); - } - l+=s; - break; - default: - goto DROP; - } - if(!NyLPC_cUdpSocket_psend(&(i_inst->_super),&MDNS_MCAST_IPADDR,MDNS_MCAST_PORT,obuf,l)){ - NyLPC_OnErrorGoto(ERROR); - } - return; + +static void sendReply2(NyLPC_TcMDnsServer_t* i_inst, const struct NyLPC_TDnsHeader* i_dns_header, const struct NyLPC_TDnsQuestion* q) +{ + NyLPC_TInt16 ptr_recode; + NyLPC_TInt16 i2; + char* obuf; + NyLPC_TUInt16 obuflen; + NyLPC_TUInt16 l; + //パケットヘッダの生成 + switch (q->qtype){ + case NyLPC_TDnsQuestion_QTYPR_SRV: + // + ptr_recode = NyLPC_TDnsRecord_getMatchSrvIdx(i_inst->_ref_record, q); + if (ptr_recode<0){ + goto DROP; + } + //Bufferの取得 + obuf = NyLPC_iUdpSocket_allocSendBuf(i_inst->_socket, 512, &obuflen, 0); + if (obuf == NULL){ + goto DROP; + } + //SRV,(TXT,A,AAAA,NSEC) + l = setResponseHeader(obuf, i_dns_header, 1, 0, 3); + l = writeSRVRecord(i_inst, ptr_recode, obuf, l, obuflen); + if (l <= 0){ + NyLPC_OnErrorGoto(ERROR); + } + l = writeTXTRecord(i_inst, ptr_recode, obuf, l, obuflen); + if (l <= 0){ + NyLPC_OnErrorGoto(ERROR); + } + //Aレコード + l = writeARecord(obuf, l, obuflen, i_inst->_ref_record->a, NyLPC_iUdpSocket_getSockIP(i_inst->_socket)); + if (l <= 0){ + NyLPC_OnErrorGoto(ERROR); + } +// //AAAAレコード +// l = writeAAAARecord(obuf, l, obuflen, i_inst->_ref_record->a, &(i_inst->_super.uip_udp_conn.lipaddr)); +// if (l <= 0){ +// NyLPC_OnErrorGoto(ERROR); +// } + //NSEC + l = writeNSECRecord(obuf, l, obuflen, i_inst->_ref_record->a); + if (l <= 0){ + NyLPC_OnErrorGoto(ERROR); + } + break; +/* case NyLPC_TDnsQuestion_QTYPR_AAAA: + //自分宛?(name.local) + if (!NyLPC_TDnsQuestion_isEqualName(q, i_inst->_ref_record->a, "")){ + goto DROP; + } + //Bufferの取得 + obuf = NyLPC_cUdpSocket_allocSendBuf(&(i_inst->_super), 512, &obuflen, 0); + if (obuf == NULL){ + goto DROP; + } + //Headerのコピー + //AAAA,(A,NSEC) + l = setResponseHeader(obuf, i_dns_header, 1, 0, 2); + //AAAAレコード + l = writeAAAARecordByQuery(obuf, l, obuflen, q, &(i_inst->_super.uip_udp_conn.lipaddr)); + if (l <= 0){ + NyLPC_OnErrorGoto(ERROR); + } + //Aレコードのみ + l = writeARecordByQuery(obuf, l, obuflen, q, &(i_inst->_super.uip_udp_conn.lipaddr)); + if (l <= 0){ + NyLPC_OnErrorGoto(ERROR); + } + //NSEC + l = writeNSECRecordByQuery(obuf, l, obuflen, q); + if (l <= 0){ + NyLPC_OnErrorGoto(ERROR); + } + break;*/ + case NyLPC_TDnsQuestion_QTYPR_A: + //自分宛?(name.local) + if (!NyLPC_TDnsQuestion_isEqualName(q, i_inst->_ref_record->a, "")){ + goto DROP; + } + //Bufferの取得 + obuf = NyLPC_iUdpSocket_allocSendBuf(i_inst->_socket, 512, &obuflen, 0); + if (obuf == NULL){ + goto DROP; + } + //Headerのコピー + l = setResponseHeader(obuf, i_dns_header, 1, 0, 1); + //A、(NSEC + l = writeARecordByQuery(obuf, l, obuflen, q, NyLPC_iUdpSocket_getSockIP(i_inst->_socket)); + if (l <= 0){ + NyLPC_OnErrorGoto(ERROR); + } + l = writeNSECRecordByQuery(obuf, l, obuflen, q); + if (l <= 0){ + NyLPC_OnErrorGoto(ERROR); + } + break; + case NyLPC_TDnsQuestion_QTYPR_PTR: + //_service._dns-sd._udpかどうか + if (NyLPC_TDnsQuestion_isEqualName(q, NULL, "_services._dns-sd._udp")){ + //Bufferの取得 + obuf = NyLPC_iUdpSocket_allocSendBuf(i_inst->_socket, 512, &obuflen, 0); + if (obuf == NULL){ + goto DROP; + } + l = setResponseHeader(obuf, i_dns_header, i_inst->_ref_record->num_of_srv, 0, 0); + for (i2 = 0; i2_ref_record->num_of_srv; i2++){ + l = writeSdPtrRecord(&(i_inst->_ref_record->srv[i2]), obuf, l, obuflen); + if (l <= 0){ + NyLPC_OnErrorGoto(ERROR); + } + } + } + else{ + //自分宛?(proto.local) + ptr_recode = NyLPC_TDnsRecord_getMatchPtrIdx(i_inst->_ref_record, q); + if (ptr_recode<0){ + goto DROP; + } + //Bufferの取得 + obuf = NyLPC_iUdpSocket_allocSendBuf(i_inst->_socket, 512, &obuflen, 0); + if (obuf == NULL){ + goto DROP; + } + l = setResponseHeader(obuf, i_dns_header, 1, 0, 4); + l = writePtrRecord(i_inst->_ref_record, ptr_recode, obuf, l, obuflen); + if (l <= 0){ + NyLPC_OnErrorGoto(ERROR); + } + //SRV + l = writeSRVRecord(i_inst, ptr_recode, obuf, l, obuflen); + if (l <= 0){ + NyLPC_OnErrorGoto(ERROR); + } + //TXT + l = writeTXTRecord(i_inst, ptr_recode, obuf, l, obuflen); + if (l <= 0){ + NyLPC_OnErrorGoto(ERROR); + } + //Aレコード + l = writeARecord(obuf, l, obuflen, i_inst->_ref_record->a,NyLPC_iUdpSocket_getSockIP(i_inst->_socket)); + if (l <= 0){ + NyLPC_OnErrorGoto(ERROR); + } +// //AAAAレコード +// l = writeAAAARecord(obuf, l, obuflen, i_inst->_ref_record->a, &(i_inst->_super.uip_udp_conn.lipaddr)); +// if (l <= 0){ +// NyLPC_OnErrorGoto(ERROR); +// } + //NSEC + l = writeNSECRecord(obuf, l, obuflen, i_inst->_ref_record->a); + if (l <= 0){ + NyLPC_OnErrorGoto(ERROR); + } + } + break; + case NyLPC_TDnsQuestion_QTYPR_TXT: + //自分宛?(proto.local) + ptr_recode = NyLPC_TDnsRecord_getMatchSrvIdx(i_inst->_ref_record, q); + if (ptr_recode<0){ + goto DROP; + } + //Bufferの取得 + obuf = NyLPC_iUdpSocket_allocSendBuf(i_inst->_socket, 512, &obuflen, 0); + l = setResponseHeader(obuf, i_dns_header, 1, 0, 2); + l = writeTXTRecord(i_inst, ptr_recode, obuf, l, obuflen); + if (l <= 0){ + NyLPC_OnErrorGoto(ERROR); + } + //A recoad + l = writeARecord(obuf, l, obuflen, i_inst->_ref_record->a,NyLPC_iUdpSocket_getSockIP(i_inst->_socket)); + if (l <= 0){ + NyLPC_OnErrorGoto(ERROR); + } +// //AAAAレコード +// l = writeAAAARecord(obuf, l, obuflen, i_inst->_ref_record->a, &(i_inst->_super.uip_udp_conn.lipaddr)); +// if (l <= 0){ +// NyLPC_OnErrorGoto(ERROR); +// } + //NSEC + l = writeNSECRecord(obuf, l, obuflen, i_inst->_ref_record->a); + if (l <= 0){ + NyLPC_OnErrorGoto(ERROR); + } + break; + default: + goto DROP; + } + if (!NyLPC_iUdpSocket_psend(i_inst->_socket, &MDNS_MCAST_IPADDR, MDNS_MCAST_PORT, obuf, l)){ + NyLPC_OnErrorGoto(ERROR); + } + return; ERROR: - NyLPC_cUdpSocket_releaseSendBuf(&(i_inst->_super),obuf); + NyLPC_iUdpSocket_releaseSendBuf(i_inst->_socket, obuf); DROP: - return; + return; } -#define CTRL_FLAG_INIT 0x00 -#define CTRL_FLAG_STARTED 0x80 -#define CTRL_FLAG_STOP_REQUESTED 0x40 -#define CTRL_FLAG_PROCESS_PACKET 0x20 //パケット処理中の間1 - -static NyLPC_TBool onPacket(NyLPC_TcUdpSocket_t* i_inst,const void* i_buf,const struct NyLPC_TIPv4RxInfo* i_info) -{ - NyLPC_TUInt16 in_len=i_info->size; - NyLPC_TUInt16 num_of_query; - const char* qptr; - struct NyLPC_TDnsQuestion q; - NyLPC_TUInt16 s; - NyLPC_TInt16 i; - - if(i_info->peer_port!=MDNS_MCAST_PORT || !NyLPC_TIPv4Addr_isEqual(&MDNS_MCAST_IPADDR,&i_info->ip)){ - return NyLPC_TBool_FALSE; - } - - num_of_query=getNumberOfQuestion(i_buf,in_len); - if(num_of_query==0){ - goto DROP; - } - qptr=(const char*)i_buf+sizeof(struct NyLPC_TDnsHeader); - in_len-=sizeof(struct NyLPC_TDnsHeader); - for(i=0;ipeer_port!=MDNS_MCAST_PORT || !NyLPC_TIPv4Addr_isEqual(&MDNS_MCAST_IPADDR,&i_info->ip)){ + return NyLPC_TBool_FALSE; + } + + num_of_query=getNumberOfQuestion(i_buf,i_info->size); + if(num_of_query==0){ + goto DROP; + } + in_len=sizeof(struct NyLPC_TDnsHeader); + for(i=0;isize,in_len,&q); + if(s==0){ + goto DROP; + } + in_len+=s; + sendReply2((NyLPC_TcMDnsServer_t*)i_inst->_tag,(const struct NyLPC_TDnsHeader*)i_buf,&q); + } + //パケット処理終了 + return NyLPC_TBool_FALSE; DROP: - return NyLPC_TBool_FALSE; + return NyLPC_TBool_FALSE; } -static void onPeriodic(NyLPC_TcUdpSocket_t* i_inst) +static void onPeriodic(NyLPC_TiUdpSocket_t* i_inst) { - - if(NyLPC_cStopwatch_isExpired(&((NyLPC_TcMDnsServer_t*)i_inst)->_periodic_sw)){ - //アナウンス - sendAnnounse(((NyLPC_TcMDnsServer_t*)i_inst)); - //TTL(msec)*1000*80% - NyLPC_cStopwatch_startExpire((&((NyLPC_TcMDnsServer_t*)i_inst)->_periodic_sw),NyLPC_TcMDns_TTL*5000/4); - } + NyLPC_TcMDnsServer_t* inst=(NyLPC_TcMDnsServer_t*)i_inst->_tag; + //Announce Timeout + if(NyLPC_cStopwatch_isExpired(&((NyLPC_TcMDnsServer_t*)inst)->_periodic_sw)){ + switch(inst->_state){ + case ST_WAIT: + inst->_state_val=0; + inst->_state=ST_ANNOUNCE;// set Announce status + case ST_ANNOUNCE: + //アナウンス + inst->_state_val++; + if(inst->_state_val<=3){ + sendAnnounse(((NyLPC_TcMDnsServer_t*)inst)); + NyLPC_cStopwatch_startExpire((&((NyLPC_TcMDnsServer_t*)inst)->_periodic_sw),1000); + }else{ + inst->_state=ST_WAIT; + //TTL(msec)*1000*80% + NyLPC_cStopwatch_startExpire((&((NyLPC_TcMDnsServer_t*)inst)->_periodic_sw),NyLPC_TcMDns_STD_TTL*1000*4/5); + } + } + } } NyLPC_TBool NyLPC_cMDnsServer_initialize( - NyLPC_TcMDnsServer_t* i_inst,const struct NyLPC_TDnsRecord* i_ref_record) -{ - NyLPC_cStopwatch_initialize(&(i_inst->_periodic_sw)); - NyLPC_cStopwatch_startExpire(&(i_inst->_periodic_sw),1000); - NyLPC_cUdpSocket_initialize(&(i_inst->_super),MDNS_MCAST_PORT,NULL,0); - NyLPC_cUdpSocket_setOnRxHandler(&(i_inst->_super),onPacket); - NyLPC_cUdpSocket_setOnPeriodicHandler(&(i_inst->_super),onPeriodic); - NyLPC_cUdpSocket_joinMulticast(&(i_inst->_super),&MDNS_MCAST_IPADDR); - i_inst->_ref_record=i_ref_record; - return NyLPC_TBool_TRUE; + NyLPC_TcMDnsServer_t* i_inst,const struct NyLPC_TDnsRecord* i_ref_record) +{ + NyLPC_cStopwatch_initialize(&(i_inst->_periodic_sw)); + NyLPC_cStopwatch_startExpire(&(i_inst->_periodic_sw),1000); + i_inst->_socket=NyLPC_cNet_createUdpSocketEx(MDNS_MCAST_PORT,NyLPC_TSocketType_UDP_NOBUF); + i_inst->_socket->_tag=i_inst; + NyLPC_iUdpSocket_setOnRxHandler(i_inst->_socket,onPacket); + NyLPC_iUdpSocket_setOnPeriodicHandler(i_inst->_socket,onPeriodic); + NyLPC_iUdpSocket_joinMulticast(i_inst->_socket,&MDNS_MCAST_IPADDR); + i_inst->_state=ST_WAIT; + i_inst->_state_val=0; + i_inst->_ref_record=i_ref_record; + return NyLPC_TBool_TRUE; } void NyLPC_cMDnsServer_finalize( - NyLPC_TcMDnsServer_t* i_inst) + NyLPC_TcMDnsServer_t* i_inst) { - NyLPC_cUdpSocket_finalize(&(i_inst->_super)); - NyLPC_cStopwatch_finalize(&(i_inst->_periodic_sw)); + NyLPC_iUdpSocket_finalize(i_inst->_socket); + NyLPC_cStopwatch_finalize(&(i_inst->_periodic_sw)); } diff --git a/lib/src/net/mdns/NyLPC_cMDnsServer.h b/lib/src/net/mdns/NyLPC_cMDnsServer.h index 9ae0065..6fb7259 100644 --- a/lib/src/net/mdns/NyLPC_cMDnsServer.h +++ b/lib/src/net/mdns/NyLPC_cMDnsServer.h @@ -19,41 +19,41 @@ * along with this program. If not, see . * * For further information please contact. - * http://nyatla.jp/ - * or + * http://nyatla.jp/ + * or * *********************************************************************************/ #ifndef NYLPC_CMDNSSERVER_H_ #define NYLPC_CMDNSSERVER_H_ +#include "NyLPC_netif.h" #ifdef __cplusplus extern "C" { #endif /* __cplusplus */ -#include "NyLPC_net.h" /** * NyLPC_TDnsRecordで使用するサービスレコード */ struct NyLPC_TMDnsServiceRecord { - const char* protocol; - NyLPC_TUInt16 port; + const char* protocol; + NyLPC_TUInt16 port; }; /** * DNSレコード */ struct NyLPC_TDnsRecord { - /** Service name*/ - const NyLPC_TChar* name; - /** Host name recommended name[:emac:]*/ - const NyLPC_TChar* a; - /** - * 配列の数 - */ - NyLPC_TUInt16 num_of_srv; - const struct NyLPC_TMDnsServiceRecord* srv; + /** Service name*/ + const NyLPC_TChar* name; + /** Host name recommended name[:emac:]*/ + const NyLPC_TChar* a; + /** + * 配列の数 + */ + NyLPC_TUInt16 num_of_srv; + const struct NyLPC_TMDnsServiceRecord* srv; }; @@ -67,12 +67,16 @@ typedef struct NyLPC_TcMDnsServer NyLPC_TcMDnsServer_t; struct NyLPC_TcMDnsServer { - /** マルチキャストのUDPソケット*/ - NyLPC_TcUdpSocket_t _super; - /**周期実行タイマ*/ - NyLPC_TcStopwatch_t _periodic_sw; - /** DNSレコードの参照情報*/ - const struct NyLPC_TDnsRecord* _ref_record; + /** マルチキャストのUDPソケット*/ + NyLPC_TiUdpSocket_t* _socket; + /**周期実行タイマ*/ + NyLPC_TcStopwatch_t _periodic_sw; + /** 動作モード(private)*/ + NyLPC_TUInt8 _state; + NyLPC_TUInt8 _state_val; + NyLPC_TUInt8 _padding[2]; + /** DNSレコードの参照情報*/ + const struct NyLPC_TDnsRecord* _ref_record; }; /** @@ -81,10 +85,10 @@ struct NyLPC_TcMDnsServer * DNSレコードの参照値。インスタンス */ NyLPC_TBool NyLPC_cMDnsServer_initialize( - NyLPC_TcMDnsServer_t* i_inst,const struct NyLPC_TDnsRecord* i_ref_record); + NyLPC_TcMDnsServer_t* i_inst,const struct NyLPC_TDnsRecord* i_ref_record); void NyLPC_cMDnsServer_finalize( - NyLPC_TcMDnsServer_t* i_inst); + NyLPC_TcMDnsServer_t* i_inst); void NyLPC_cMDnsServer_periodicRecvProc(NyLPC_TcMDnsServer_t* i_inst); /** diff --git a/lib/src/net/upnp/NyLPC_UPnP_types.h b/lib/src/net/upnp/NyLPC_UPnP_types.h index 61bf106..b0e48e5 100644 --- a/lib/src/net/upnp/NyLPC_UPnP_types.h +++ b/lib/src/net/upnp/NyLPC_UPnP_types.h @@ -70,10 +70,10 @@ struct NyLPC_TUPnPDevDescDevice const struct NyLPC_TUPnPDevDescIcon* icons; }; - #ifdef __cplusplus } #endif /* __cplusplus */ + #endif diff --git a/lib/src/net/upnp/NyLPC_cSsdpSocket.c b/lib/src/net/upnp/NyLPC_cSsdpSocket.c index e7dbe7b..b04e19c 100644 --- a/lib/src/net/upnp/NyLPC_cSsdpSocket.c +++ b/lib/src/net/upnp/NyLPC_cSsdpSocket.c @@ -25,8 +25,8 @@ *********************************************************************************/ #include "NyLPC_cSsdpSocket.h" #include "NyLPC_http.h" -#include "NyLPC_uipService.h" -#include "../uip/NyLPC_cUipService_protected.h" +#include "NyLPC_netif.h" + #include #include @@ -82,7 +82,7 @@ static NyLPC_TBool urlHandler(NyLPC_TcHttpBasicHeaderParser_t* i_inst,NyLPC_TCha * SERVER MessageHeaderの値 * 40文字以内であること。 */ -#define SERVER_MESSAGE_HEADER "MiMic/1.4;UPnP/1.0;MiMicUPnP/0.1" +#define SERVER_MESSAGE_HEADER "MiMic/1.4 UPnP/1.0 MiMicUPnP/0.2" /** @@ -119,7 +119,7 @@ static void* allocMsearchResponeTx( // "USN: %s%s\r\n" //5+2=7 // "ST: %s\r\n\r\n" //4+4=8 l=166+len_location+len_usn+len_udn+i_st_len; - obuf=NyLPC_cUdpSocket_allocSendBuf(&(i_inst->super),l,&l,TIMEOUT_IN_MS); + obuf=NyLPC_iUdpSocket_allocSendBuf(i_inst->_socket,l,&l,TIMEOUT_IN_MS); if(obuf==NULL){ return NULL; @@ -127,7 +127,7 @@ static void* allocMsearchResponeTx( //必要なメモリサイズを確保できた? if(l<161+len_location+len_usn+len_udn+i_st_len) { - NyLPC_cUdpSocket_releaseSendBuf(&i_inst->super,obuf); + NyLPC_iUdpSocket_releaseSendBuf(i_inst->_socket,obuf); return NULL; } //ワーク変数lの再初期化 @@ -140,7 +140,7 @@ static void* allocMsearchResponeTx( "LOCATION: http://"); l+=strlen(obuf); //IP addr:port\r\n - l+=NyLPC_TIPv4Addr_toString(NyLPC_cUdpSocket_getSockIP(&i_inst->super),obuf+l); + l+=NyLPC_TIPv4Addr_toString(NyLPC_iUdpSocket_getSockIP(i_inst->_socket),obuf+l); *(obuf+l)=':'; l+=1+NyLPC_itoa(i_inst->location_port,obuf+l+1,10); *(obuf+l)='/';l++; @@ -203,14 +203,14 @@ static void* allocNotifyTx( // "USN: %s%s\r\n" //5+2=7 // "NT: %s\r\n\r\n" //4+4=8 l2=204+len_location+len_usn+len_udn+((len_usn>0)?len_usn:len_udn); - obuf=NyLPC_cUdpSocket_allocSendBuf(&(i_inst->super),l2,&l,TIMEOUT_IN_MS); + obuf=NyLPC_iUdpSocket_allocSendBuf(i_inst->_socket,l2,&l,TIMEOUT_IN_MS); if(obuf==NULL){ return NULL; } //必要なメモリサイズを確保できた? if(lsuper,obuf); + NyLPC_iUdpSocket_releaseSendBuf(i_inst->_socket,obuf); return NULL; } //ワーク変数lの再初期化 @@ -224,7 +224,7 @@ static void* allocNotifyTx( "LOCATION: http://"); l+=strlen(obuf); //IP addr:port\r\n - l+=NyLPC_TIPv4Addr_toString(NyLPC_cUdpSocket_getSockIP(&i_inst->super),obuf+l); + l+=NyLPC_TIPv4Addr_toString(NyLPC_iUdpSocket_getSockIP(i_inst->_socket),obuf+l); *(obuf+l)=':'; l+=1+NyLPC_itoa(i_inst->location_port,obuf+l+1,10); *(obuf+l)='/';l++; @@ -337,14 +337,14 @@ static NyLPC_TBool parseHeader(struct TMSearchHeader* i_out,const void* i_rx,NyL return NyLPC_TBool_TRUE;//OK } -static NyLPC_TBool onPacket(NyLPC_TcUdpSocket_t* i_inst,const void* i_buf,const struct NyLPC_TIPv4RxInfo* i_info) +static NyLPC_TBool onPacket(NyLPC_TiUdpSocket_t* i_sock,const void* i_buf,const struct NyLPC_TIPv4RxInfo* i_info) { //パケット解析 void* tx; struct TMSearchHeader header; NyLPC_TInt16 tx_len; NyLPC_TInt8 i,i2; - NyLPC_TcSsdpSocket_t* sock=(NyLPC_TcSsdpSocket_t*)i_inst; + NyLPC_TcSsdpSocket_t* inst=((NyLPC_TcSsdpSocket_t*)i_sock->_tag); if(!parseHeader(&header,i_buf,i_info->size)){ NyLPC_OnErrorGoto(ERROR1); } @@ -364,40 +364,40 @@ static NyLPC_TBool onPacket(NyLPC_TcUdpSocket_t* i_inst,const void* i_buf,const //STによる処理分岐 if(strncmp("ssdp:all",header.result.st_str,8)==0){ tx=allocMsearchResponeTx( - sock,header.result.st_str, - sock->ref_device_record[0]->udn,STR_UPNP_ROOT_DEVICE, + inst,header.result.st_str, + inst->ref_device_record[0]->udn,STR_UPNP_ROOT_DEVICE, header.result.st_len, &tx_len); if(tx==NULL){ NyLPC_OnErrorGoto(ERROR1); } - if(!NyLPC_cUdpSocket_psend(i_inst,&i_info->peer_ip,i_info->peer_port,tx,tx_len)){ + if(!NyLPC_iUdpSocket_psend(i_sock,&i_info->peer_ip,i_info->peer_port,tx,tx_len)){ NyLPC_OnErrorGoto(ERROR2); } //全デバイスの送信 - for(i=0;inumber_of_device;i++){ + for(i=0;inumber_of_device;i++){ tx=allocMsearchResponeTx( - sock,header.result.st_str, - sock->ref_device_record[i]->udn,sock->ref_device_record[i]->device_type, + inst,header.result.st_str, + inst->ref_device_record[i]->udn,inst->ref_device_record[i]->device_type, header.result.st_len, &tx_len); if(tx==NULL){ NyLPC_OnErrorGoto(ERROR1); } - if(!NyLPC_cUdpSocket_psend(i_inst,&i_info->peer_ip,i_info->peer_port,tx,tx_len)){ + if(!NyLPC_iUdpSocket_psend(i_sock,&i_info->peer_ip,i_info->peer_port,tx,tx_len)){ NyLPC_OnErrorGoto(ERROR2); } - for(i2=0;i2ref_device_record[i]->number_of_service;i2++){ + for(i2=0;i2ref_device_record[i]->number_of_service;i2++){ //serviceに一致 tx=allocMsearchResponeTx( - sock,header.result.st_str, - sock->ref_device_record[i]->udn,sock->ref_device_record[i]->services[i2].service_type, + inst,header.result.st_str, + inst->ref_device_record[i]->udn,inst->ref_device_record[i]->services[i2].service_type, header.result.st_len, &tx_len); if(tx==NULL){ NyLPC_OnErrorGoto(ERROR1); } - if(!NyLPC_cUdpSocket_psend(i_inst,&i_info->peer_ip,i_info->peer_port,tx,tx_len)){ + if(!NyLPC_iUdpSocket_psend(i_sock,&i_info->peer_ip,i_info->peer_port,tx,tx_len)){ NyLPC_OnErrorGoto(ERROR2); } } @@ -405,66 +405,66 @@ static NyLPC_TBool onPacket(NyLPC_TcUdpSocket_t* i_inst,const void* i_buf,const }else if(strncmp("uuid:",header.result.st_str,5)==0){ //UDNの一致するデバイスの送信 NyLPC_TInt16 i; - for(i=sock->number_of_device-1;i>=0;i--){ - if(strncmp(header.result.st_str,sock->ref_device_record[i]->udn,header.result.st_len)==0){ + for(i=inst->number_of_device-1;i>=0;i--){ + if(strncmp(header.result.st_str,inst->ref_device_record[i]->udn,header.result.st_len)==0){ //UDN一致 tx=allocMsearchResponeTx( - sock,header.result.st_str, - sock->ref_device_record[i]->udn,NULL, + inst,header.result.st_str, + inst->ref_device_record[i]->udn,NULL, header.result.st_len, &tx_len); if(tx==NULL){ NyLPC_OnErrorGoto(ERROR1); } - if(!NyLPC_cUdpSocket_psend(i_inst,&i_info->peer_ip,i_info->peer_port,tx,tx_len)){ + if(!NyLPC_iUdpSocket_psend(i_sock,&i_info->peer_ip,i_info->peer_port,tx,tx_len)){ NyLPC_OnErrorGoto(ERROR2); } break;//送信処理終了 } } }else if(strncmp(STR_UPNP_ROOT_DEVICE,header.result.st_str,15)==0){ - //rootDeviceはdevice0 + //rootDeviceはSTR_UPNP_ROOT_DEVICE tx=allocMsearchResponeTx( - sock,header.result.st_str, - sock->ref_device_record[0]->udn,sock->ref_device_record[0]->device_type, + inst,header.result.st_str, + inst->ref_device_record[0]->udn,STR_UPNP_ROOT_DEVICE, header.result.st_len, &tx_len); if(tx==NULL){ NyLPC_OnErrorGoto(ERROR1); } - if(!NyLPC_cUdpSocket_psend(i_inst,&i_info->peer_ip,i_info->peer_port,tx,tx_len)){ + if(!NyLPC_iUdpSocket_psend(i_sock,&i_info->peer_ip,i_info->peer_port,tx,tx_len)){ NyLPC_OnErrorGoto(ERROR2); } }else if(strncmp("urn:",header.result.st_str,4)==0){ - for(i=0;inumber_of_device;i++){ + for(i=0;inumber_of_device;i++){ //urn一致チェック - if(strncmp(sock->ref_device_record[i]->device_type,header.result.st_str,header.result.st_len)==0){ + if(strncmp(inst->ref_device_record[i]->device_type,header.result.st_str,header.result.st_len)==0){ //deviceに一致 tx=allocMsearchResponeTx( - sock,header.result.st_str, - sock->ref_device_record[i]->udn,sock->ref_device_record[i]->device_type, + inst,header.result.st_str, + inst->ref_device_record[i]->udn,inst->ref_device_record[i]->device_type, header.result.st_len, &tx_len); if(tx==NULL){ NyLPC_OnErrorGoto(ERROR1); } - if(!NyLPC_cUdpSocket_psend(i_inst,&i_info->peer_ip,i_info->peer_port,tx,tx_len)){ + if(!NyLPC_iUdpSocket_psend(i_sock,&i_info->peer_ip,i_info->peer_port,tx,tx_len)){ NyLPC_OnErrorGoto(ERROR2); } continue; } - for(i2=0;i2ref_device_record[i]->number_of_service;i2++){ - if(strncmp(sock->ref_device_record[i]->services[i2].service_type,header.result.st_str,header.result.st_len)==0){ + for(i2=0;i2ref_device_record[i]->number_of_service;i2++){ + if(strncmp(inst->ref_device_record[i]->services[i2].service_type,header.result.st_str,header.result.st_len)==0){ //serviceに一致 tx=allocMsearchResponeTx( - sock,header.result.st_str, - sock->ref_device_record[i]->udn,sock->ref_device_record[i]->services[i2].service_type, + inst,header.result.st_str, + inst->ref_device_record[i]->udn,inst->ref_device_record[i]->services[i2].service_type, header.result.st_len, &tx_len); if(tx==NULL){ NyLPC_OnErrorGoto(ERROR1); } - if(!NyLPC_cUdpSocket_psend(i_inst,&i_info->peer_ip,i_info->peer_port,tx,tx_len)){ + if(!NyLPC_iUdpSocket_psend(i_sock,&i_info->peer_ip,i_info->peer_port,tx,tx_len)){ NyLPC_OnErrorGoto(ERROR2); } } @@ -474,46 +474,46 @@ static NyLPC_TBool onPacket(NyLPC_TcUdpSocket_t* i_inst,const void* i_buf,const //正常終了 return NyLPC_TBool_FALSE; ERROR2: - NyLPC_cUdpSocket_releaseSendBuf(i_inst,tx); + NyLPC_iUdpSocket_releaseSendBuf(i_sock,tx); ERROR1: return NyLPC_TBool_FALSE; } -#define SSDP_NOTIFY_INTERVAL 240*1000 //300*0.8*1000 +#define SSDP_NOTIFY_INTERVAL 150*1000 //300*0.5*1000 #define FLAG_ORDER_START_SERVICE 0 #define FLAG_ORDER_STOP_SERVICE 1 #define FLAG_IS_SERVICE_RUNNING 2 -static void onPeriodic(NyLPC_TcUdpSocket_t* i_inst) +static void onPeriodic(NyLPC_TiUdpSocket_t* i_sock) { - NyLPC_TcSsdpSocket_t* sock=(NyLPC_TcSsdpSocket_t*)i_inst; - if(NyLPC_TUInt8_isBitOn(sock->_flags,FLAG_IS_SERVICE_RUNNING)){ + NyLPC_TcSsdpSocket_t* inst=(NyLPC_TcSsdpSocket_t*)i_sock->_tag; + if(NyLPC_TUInt8_isBitOn(inst->_flags,FLAG_IS_SERVICE_RUNNING)){ //実行中 //停止要求着てる? - if(NyLPC_TUInt8_isBitOn(sock->_flags,FLAG_ORDER_STOP_SERVICE)) + if(NyLPC_TUInt8_isBitOn(inst->_flags,FLAG_ORDER_STOP_SERVICE)) { //状態変更 - NyLPC_TUInt8_unsetBit(sock->_flags,FLAG_IS_SERVICE_RUNNING); + NyLPC_TUInt8_unsetBit(inst->_flags,FLAG_IS_SERVICE_RUNNING); //要求フラグクリア - NyLPC_TUInt8_unsetBit(sock->_flags,FLAG_ORDER_STOP_SERVICE); + NyLPC_TUInt8_unsetBit(inst->_flags,FLAG_ORDER_STOP_SERVICE); //@bug ByeBye送信しろ - }else if(NyLPC_cStopwatch_isExpired(&sock->_periodic_sw)){ + }else if(NyLPC_cStopwatch_isExpired(&inst->_periodic_sw)){ //Notify送信 - NyLPC_cSsdpSocket_notify(sock); + NyLPC_cSsdpSocket_notify(inst); //タイマ再始動 - NyLPC_cStopwatch_startExpire(&sock->_periodic_sw,SSDP_NOTIFY_INTERVAL); + NyLPC_cStopwatch_startExpire(&inst->_periodic_sw,SSDP_NOTIFY_INTERVAL); } }else{ //停止中 //開始要求着てる? - if(NyLPC_TUInt8_isBitOn(sock->_flags,FLAG_ORDER_START_SERVICE)) + if(NyLPC_TUInt8_isBitOn(inst->_flags,FLAG_ORDER_START_SERVICE)) { //状態変更 - NyLPC_TUInt8_setBit(sock->_flags,FLAG_IS_SERVICE_RUNNING); + NyLPC_TUInt8_setBit(inst->_flags,FLAG_IS_SERVICE_RUNNING); //要求フラグクリア - NyLPC_TUInt8_unsetBit(sock->_flags,FLAG_ORDER_START_SERVICE); + NyLPC_TUInt8_unsetBit(inst->_flags,FLAG_ORDER_START_SERVICE); //次回expireするように - NyLPC_cStopwatch_startExpire(&sock->_periodic_sw,SSDP_NOTIFY_INTERVAL); + NyLPC_cStopwatch_startExpire(&inst->_periodic_sw,SSDP_NOTIFY_INTERVAL); } } } @@ -540,11 +540,13 @@ void NyLPC_cSsdpSocket_initialize( const struct NyLPC_TUPnPDevDescDevice* i_ref_dev_record, NyLPC_TUInt16 i_server_port,const NyLPC_TChar* i_ref_location_path) { - NyLPC_cUdpSocket_initialize(&(i_inst->super),1900,NULL,0); - NyLPC_cUdpSocket_setOnRxHandler(&(i_inst->super),onPacket); - NyLPC_cUdpSocket_setOnPeriodicHandler(&(i_inst->super),onPeriodic); + i_inst->_socket=NyLPC_cNet_createUdpSocketEx(1900,NyLPC_TSocketType_UDP_NOBUF); + i_inst->_socket->_tag=i_inst; + + NyLPC_iUdpSocket_setOnRxHandler(i_inst->_socket,onPacket); + NyLPC_iUdpSocket_setOnPeriodicHandler(i_inst->_socket,onPeriodic); - NyLPC_cUdpSocket_joinMulticast(&(i_inst->super),&SSDP_MCAST_IPADDR); + NyLPC_iUdpSocket_joinMulticast(i_inst->_socket,&SSDP_MCAST_IPADDR); i_inst->_flags=0; NyLPC_cStopwatch_initialize(&(i_inst->_periodic_sw)); i_inst->number_of_device=0; @@ -555,7 +557,7 @@ void NyLPC_cSsdpSocket_initialize( void NyLPC_cSsdpSocket_finalize(NyLPC_TcSsdpSocket_t* i_inst) { NyLPC_cStopwatch_finalize(&(i_inst->_periodic_sw)); - NyLPC_cUdpSocket_finalize(&(i_inst->super)); + NyLPC_iUdpSocket_finalize(i_inst->_socket); } void NyLPC_cSsdpSocket_start(NyLPC_TcSsdpSocket_t* i_inst) @@ -598,7 +600,7 @@ void NyLPC_cSsdpSocket_notify(NyLPC_TcSsdpSocket_t* i_inst) if(tx==NULL){ NyLPC_OnErrorGoto(ERROR1); } - if(!NyLPC_cUdpSocket_psend(&i_inst->super,&SSDP_MCAST_IPADDR,1900,tx,tx_len)){ + if(!NyLPC_iUdpSocket_psend(i_inst->_socket,&SSDP_MCAST_IPADDR,1900,tx,tx_len)){ NyLPC_OnErrorGoto(ERROR2); } //all device @@ -611,7 +613,7 @@ void NyLPC_cSsdpSocket_notify(NyLPC_TcSsdpSocket_t* i_inst) if(tx==NULL){ NyLPC_OnErrorGoto(ERROR1); } - if(!NyLPC_cUdpSocket_psend(&i_inst->super,&SSDP_MCAST_IPADDR,1900,tx,tx_len)){ + if(!NyLPC_iUdpSocket_psend(i_inst->_socket,&SSDP_MCAST_IPADDR,1900,tx,tx_len)){ NyLPC_OnErrorGoto(ERROR2); } //devicatype @@ -622,7 +624,7 @@ void NyLPC_cSsdpSocket_notify(NyLPC_TcSsdpSocket_t* i_inst) if(tx==NULL){ NyLPC_OnErrorGoto(ERROR1); } - if(!NyLPC_cUdpSocket_psend(&i_inst->super,&SSDP_MCAST_IPADDR,1900,tx,tx_len)){ + if(!NyLPC_iUdpSocket_psend(i_inst->_socket,&SSDP_MCAST_IPADDR,1900,tx,tx_len)){ NyLPC_OnErrorGoto(ERROR2); } for(i2=0;i2ref_device_record[i]->number_of_service;i2++){ @@ -633,14 +635,14 @@ void NyLPC_cSsdpSocket_notify(NyLPC_TcSsdpSocket_t* i_inst) if(tx==NULL){ NyLPC_OnErrorGoto(ERROR1); } - if(!NyLPC_cUdpSocket_psend(&i_inst->super,&SSDP_MCAST_IPADDR,1900,tx,tx_len)){ + if(!NyLPC_iUdpSocket_psend(i_inst->_socket,&SSDP_MCAST_IPADDR,1900,tx,tx_len)){ NyLPC_OnErrorGoto(ERROR2); } } } return; ERROR2: - NyLPC_cUdpSocket_releaseSendBuf(&i_inst->super,tx); + NyLPC_iUdpSocket_releaseSendBuf(i_inst->_socket,tx); ERROR1: return; } diff --git a/lib/src/net/upnp/NyLPC_cSsdpSocket.h b/lib/src/net/upnp/NyLPC_cSsdpSocket.h index f066b55..69ce5a8 100644 --- a/lib/src/net/upnp/NyLPC_cSsdpSocket.h +++ b/lib/src/net/upnp/NyLPC_cSsdpSocket.h @@ -7,8 +7,8 @@ #ifndef NYLPC_CSSDPSOCKET_H_ #define NYLPC_CSSDPSOCKET_H_ +#include "NyLPC_netif.h" #include "NyLPC_UPnP_types.h" -#include "NyLPC_uipService.h" #ifdef __cplusplus extern "C" { @@ -27,7 +27,7 @@ typedef struct NyLPC_TcSsdpSocket NyLPC_TcSsdpSocket_t; struct NyLPC_TcSsdpSocket { - NyLPC_TcUdpSocket_t super; + NyLPC_TiUdpSocket_t* _socket; NyLPC_TcStopwatch_t _periodic_sw; /** * locationパス @@ -81,14 +81,14 @@ void NyLPC_cSsdpSocket_start(NyLPC_TcSsdpSocket_t* i_inst); void NyLPC_cSsdpSocket_stop(NyLPC_TcSsdpSocket_t* i_inst); +#ifdef __cplusplus +} +#endif /* __cplusplus */ -#ifdef __cplusplus -} -#endif /* __cplusplus */ diff --git a/lib/src/net/upnp/NyLPC_cUPnP.h b/lib/src/net/upnp/NyLPC_cUPnP.h index 4338854..d98abd0 100644 --- a/lib/src/net/upnp/NyLPC_cUPnP.h +++ b/lib/src/net/upnp/NyLPC_cUPnP.h @@ -38,9 +38,9 @@ void NyLPC_cUPnP_initialize(NyLPC_TcUPnP_t* i_inst,NyLPC_TUInt16 i_http_port,con void NyLPC_cUPnP_finalize(NyLPC_TcUPnP_t* i_inst); void NyLPC_cUPnP_start(NyLPC_TcUPnP_t* i_inst); - #ifdef __cplusplus } #endif /* __cplusplus */ + #endif diff --git a/lib/src/uip/NyLPC_uip_ethernet.c b/lib/src/netif/NyLPC_NetIf_ethernet_types.c similarity index 93% rename from lib/src/uip/NyLPC_uip_ethernet.c rename to lib/src/netif/NyLPC_NetIf_ethernet_types.c index 2ce8de3..309a8be 100644 --- a/lib/src/uip/NyLPC_uip_ethernet.c +++ b/lib/src/netif/NyLPC_NetIf_ethernet_types.c @@ -1,58 +1,59 @@ -/********************************************************************************* - * PROJECT: MiMic - * -------------------------------------------------------------------------------- - * - * This file is part of MiMic - * Copyright (C)2011 Ryo Iizuka - * - * MiMic is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published - * by the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program. If not, see . - * - * For further information please contact. - * http://nyatla.jp/ - * or - * - * - * Parts of this file were leveraged from uIP: - * - * Copyright (c) 2001-2003, Adam Dunkels. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote - * products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS - * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY - * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE - * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ -#include "NyLPC_uip_ethernet.h" - -const struct NyLPC_TEthAddr NyLPC_TEthAddr_BROADCAST = { { 0xff, 0xff, 0xff,0xff, 0xff, 0xff } }; - +/********************************************************************************* + * PROJECT: MiMic + * -------------------------------------------------------------------------------- + * + * This file is part of MiMic + * Copyright (C)2011 Ryo Iizuka + * + * MiMic is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + * + * For further information please contact. + * http://nyatla.jp/ + * or + * + * + * Parts of this file were leveraged from uIP: + * + * Copyright (c) 2001-2003, Adam Dunkels. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +#include "NyLPC_NetIf_ethernet_types.h" + +const struct NyLPC_TEthAddr NyLPC_TEthAddr_BROADCAST = { { 0xff, 0xff, 0xff,0xff, 0xff, 0xff } }; + + diff --git a/lib/src/uip/NyLPC_uip_ethernet.h b/lib/src/netif/NyLPC_NetIf_ethernet_types.h similarity index 92% rename from lib/src/uip/NyLPC_uip_ethernet.h rename to lib/src/netif/NyLPC_NetIf_ethernet_types.h index 06b4b2b..ab940df 100644 --- a/lib/src/uip/NyLPC_uip_ethernet.h +++ b/lib/src/netif/NyLPC_NetIf_ethernet_types.h @@ -1,110 +1,110 @@ -/********************************************************************************* - * PROJECT: MiMic - * -------------------------------------------------------------------------------- - * - * This file is part of MiMic - * Copyright (C)2011 Ryo Iizuka - * - * MiMic is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published - * by the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program. If not, see . - * - * For further information please contact. - * http://nyatla.jp/ - * or - * - * - * Parts of this file were leveraged from uIP: - * - * Copyright (c) 2001-2003, Adam Dunkels. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote - * products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS - * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY - * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE - * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ -#ifndef NyLPC_uip_ethernet_h -#define NyLPC_uip_ethernet_h -#include "../include/NyLPC_config.h" -#include "../include/NyLPC_stdlib.h" -#ifdef __cplusplus -extern "C" { -#endif /* __cplusplus */ - - -#ifndef PACK_STRUCT_END - #define PACK_STRUCT_END __attribute((packed)) -#endif - -/********************************************************************** - * - * struct NyLPC_TEthAddr - * - **********************************************************************/ - -/** - * この構造体は、48bitのイーサネットアドレスを格納します。 - */ -struct NyLPC_TEthAddr -{ - NyLPC_TUInt8 addr[6]; -}PACK_STRUCT_END; - - -/** - * 構造体にEthernetアドレスをセットします。 - * 次のように使います。 - \code - struct NyLPC_TEthAddr en=NyLPC_TEthAddr_pack(1,2,3,4,5,6); - \endcode - */ -#define NyLPC_TEthAddr_pack(a1,a2,a3,a4,a5,a6) {{(a1),(a2),(a3),(a4),(a5),(a6)}} -/** - * 変数にEthernetアドレスをセットします。 - * 次のように使います。 - \code - struct NyLPC_TEthAddr en; - NyLPC_TEthAddr_set(&en,1,2,3,4,5,6); - \endcode - */ -#define NyLPC_TEthAddr_set(v,a1,a2,a3,a4,a5,a6) {(v)->addr[0]=(a1);(v)->addr[1]=(a2);(v)->addr[2]=(a3);(v)->addr[3]=(a4);(v)->addr[4]=(a5);(v)->addr[5]=(a6);} - -extern const struct NyLPC_TEthAddr NyLPC_TEthAddr_BROADCAST; - - -#ifdef __cplusplus -} -#endif /* __cplusplus */ - - -#endif - +/********************************************************************************* + * PROJECT: MiMic + * -------------------------------------------------------------------------------- + * + * This file is part of MiMic + * Copyright (C)2011 Ryo Iizuka + * + * MiMic is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + * + * For further information please contact. + * http://nyatla.jp/ + * or + * + * + * Parts of this file were leveraged from uIP: + * + * Copyright (c) 2001-2003, Adam Dunkels. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +#ifndef NyLPC_NetIF_ethernet_types_h +#define NyLPC_NetIF_ethernet_types_h +#include "../include/NyLPC_config.h" +#include "../include/NyLPC_stdlib.h" +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + + +#ifndef PACK_STRUCT_END + #define PACK_STRUCT_END __attribute((packed)) +#endif + +/********************************************************************** + * + * struct NyLPC_TEthAddr + * + **********************************************************************/ + +/** + * この構造体は、48bitのイーサネットアドレスを格納します。 + */ +struct NyLPC_TEthAddr +{ + NyLPC_TUInt8 addr[6]; +}PACK_STRUCT_END; + + +/** + * 構造体にEthernetアドレスをセットします。 + * 次のように使います。 + \code + struct NyLPC_TEthAddr en=NyLPC_TEthAddr_pack(1,2,3,4,5,6); + \endcode + */ +#define NyLPC_TEthAddr_pack(a1,a2,a3,a4,a5,a6) {{(a1),(a2),(a3),(a4),(a5),(a6)}} +/** + * 変数にEthernetアドレスをセットします。 + * 次のように使います。 + \code + struct NyLPC_TEthAddr en; + NyLPC_TEthAddr_set(&en,1,2,3,4,5,6); + \endcode + */ +#define NyLPC_TEthAddr_set(v,a1,a2,a3,a4,a5,a6) {(v)->addr[0]=(a1);(v)->addr[1]=(a2);(v)->addr[2]=(a3);(v)->addr[3]=(a4);(v)->addr[4]=(a5);(v)->addr[5]=(a6);} + +extern const struct NyLPC_TEthAddr NyLPC_TEthAddr_BROADCAST; + + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + + +#endif + diff --git a/lib/src/netif/NyLPC_NetIf_ip_types.c b/lib/src/netif/NyLPC_NetIf_ip_types.c new file mode 100644 index 0000000..78032e9 --- /dev/null +++ b/lib/src/netif/NyLPC_NetIf_ip_types.c @@ -0,0 +1,432 @@ +/********************************************************************************* + * PROJECT: MiMic + * -------------------------------------------------------------------------------- + * + * This file is part of MiMic + * Copyright (C)2011 Ryo Iizuka + * + * MiMic is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + * + * For further information please contact. + * http://nyatla.jp/ + * or + * + * + * Parts of this file were leveraged from uIP: + * + * Copyright (c) 2001-2003, Adam Dunkels. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +#include "NyLPC_NetIf_ip_types.h" + + + +const struct NyLPC_TIPv4Addr NyLPC_TIPv4Addr_ZERO={0x00000000}; +const struct NyLPC_TIPv4Addr NyLPC_TIPv4Addr_ALL ={0xffffffff}; +const struct NyLPC_TIPv4Addr NyLPC_TIPv4Addr_BROADCAST = { 0xffffffff }; +const struct NyLPC_TIPv4Addr NyLPC_TIPv4Addr_MULTICAST = NyLPC_TIPv4Addr_pack(224,0,0,0); +const struct NyLPC_TIPv4Addr NyLPC_TIPv4Addr_MULTICAST_MASK = NyLPC_TIPv4Addr_pack(224,0,0,0); +const struct NyLPC_TIPv4Addr NyLPC_TIPv4Addr_APIPA_MASK = NyLPC_TIPv4Addr_pack(255,255,0,0); + +NyLPC_TInt16 NyLPC_TIPv4Addr_toString(const struct NyLPC_TIPv4Addr* i_ip,NyLPC_TChar* i_buf) +{ + NyLPC_TUInt32 ip; + NyLPC_TChar* p=i_buf; + NyLPC_TUInt8 v; + NyLPC_TInt8 l; + //IPをホストオーダーにする。 + ip=NyLPC_NTOHL(i_ip->v); + for(l=3;l>=0;l--){ + v=(ip>>(8*l))&0xff; + if(v>=100){ + *p=(v/100)+'0'; + p++; + } + if(v>=10){ + *p=((v%100)/10)+'0'; + p++; + } + *p=(v%10)+'0'; + *(p+1)='.'; + p+=2; + } + *(p-1)='\0'; + return p-i_buf-1; +} + + +NyLPC_TUInt16 NyLPC_uip_chksum(NyLPC_TUInt16 sum, const NyLPC_TUInt8 *data, NyLPC_TUInt16 len) +{ + NyLPC_TUInt16 t; + const NyLPC_TUInt8 *dataptr; + const NyLPC_TUInt8 *last_byte; + + dataptr = data; + last_byte = data + len - 1; + + while (dataptr < last_byte) { /* At least two more bytes */ + t = (dataptr[0] << 8) + dataptr[1]; + sum += t; + if (sum < t) { + sum++; /* carry */ + } + dataptr += 2; + } + + if (dataptr == last_byte) { + t = (dataptr[0] << 8) + 0; + sum += t; + if (sum < t) { + sum++; /* carry */ + } + } + + /* Return sum in host byte order. */ + return sum; +} + +/*-------------------------------------------------------------------------------- + * + * struct NyLPC_TEthernetIIHeader + * + *------------------------------------------------------------------------------*/ + +/** + * Ethernetヘッダの内容を、ARPパケットの内容に一致するように書き換えます。 + * i_structの後方にあるものと仮定します。 + * 戻り値は、フレームの長さです。 + */ +NyLPC_TUInt16 NyLPC_TEthernetIIHeader_setArpTx( + struct NyLPC_TEthernetIIHeader* i_struct, + const struct NyLPC_TEthAddr* i_my_eth_addr) +{ + struct NyLPC_TArpHeader* arph=(struct NyLPC_TArpHeader*)(((NyLPC_TUInt8*)i_struct)+sizeof(struct NyLPC_TEthernetIIHeader)); + + i_struct->type = NyLPC_HTONS(NyLPC_TEthernetIIHeader_TYPE_ARP); + switch(arph->opcode){ + case NyLPC_HTONS(ARP_REPLY): + memcpy(i_struct->src.addr, i_my_eth_addr->addr, 6); + memcpy(i_struct->dest.addr, arph->dhwaddr.addr, 6); + break; + case NyLPC_HTONS(ARP_REQUEST): + memset(i_struct->dest.addr, 0xff, 6); + memcpy(i_struct->src.addr, i_my_eth_addr->addr, 6); + break; + } + return sizeof(struct NyLPC_TEthernetIIHeader)+sizeof(struct NyLPC_TArpHeader); +} + +/** + * イーサネットヘッダをIPv4向けにセットアップする。 + * 関数は、ペイロードをIPv4ヘッダとして、フレームサイズを計算する。 + */ +NyLPC_TUInt16 NyLPC_TEthernetIIHeader_setIPv4Tx( + struct NyLPC_TEthernetIIHeader* i_eth, + const struct NyLPC_TEthAddr* i_src_eth_addr, + const struct NyLPC_TEthAddr* i_dest_eth_addr) +{ + struct NyLPC_TIPv4Header* iph=(struct NyLPC_TIPv4Header*)(((NyLPC_TUInt8*)i_eth)+sizeof(struct NyLPC_TEthernetIIHeader)); + + i_eth->type = NyLPC_HTONS(NyLPC_TEthernetIIHeader_TYPE_IP); + /* Build an ethernet header. */ + memcpy(i_eth->dest.addr,i_dest_eth_addr, 6); + memcpy(i_eth->src.addr, i_src_eth_addr->addr, 6); + + + //IPフラグメントに応じたサイズ計算 + switch(iph->proto){ + case UIP_PROTO_TCP: + return sizeof(struct NyLPC_TEthernetIIHeader)+NyLPC_htons(iph->len16); + case UIP_PROTO_UDP: + return sizeof(struct NyLPC_TEthernetIIHeader)+NyLPC_htons(iph->len16); + case UIP_PROTO_ICMP: + return sizeof(struct NyLPC_TEthernetIIHeader)+NyLPC_htons(iph->len16); + } + return 0; +} +/*-------------------------------------------------------------------------------- + * + * struct NyLPC_TIPv4Header + * + *------------------------------------------------------------------------------*/ + +/** + * based on uip_ipchksum + */ +NyLPC_TUInt16 NyLPC_TIPv4Header_makeIpChecksum(const struct NyLPC_TIPv4Header* ip_header) +{ + NyLPC_TUInt16 sum; + sum = NyLPC_uip_chksum(0, (const NyLPC_TUInt8 *)ip_header,NyLPC_TIPv4Header_getHeaderLength(ip_header)); + return (sum == 0) ? 0xffff : NyLPC_htons(sum); +} + + + +NyLPC_TBool NyLPC_TIPv4Header_isCorrectIpCheckSum(const struct NyLPC_TIPv4Header* ip_header) +{ + return (NyLPC_TIPv4Header_makeIpChecksum(ip_header)==0xffff); +} + +NyLPC_TBool NyLPC_cIPv4Packet_isCorrectTcpCheckSum(const struct NyLPC_TIPv4Header* ip_header) +{ + return (NyLPC_TIPv4Header_makeTcpChecksum(ip_header) == 0xffff); +} + + + + +/** + * TCPチェックサムを計算します。 + * ペイロードはIPヘッダの後方に連続して存在する物と仮定します。 + * i_lenは、ペイロード長さ + */ +NyLPC_TUInt16 NyLPC_TIPv4Header_makeTcpChecksum( + const struct NyLPC_TIPv4Header* i_iph) +{ + NyLPC_TUInt16 sum; + NyLPC_TUInt16 iph_len=NyLPC_TIPv4Header_getHeaderLength(i_iph); + NyLPC_TUInt16 len = NyLPC_ntohs((i_iph)->len16)- iph_len; + NyLPC_ArgAssert(i_iph!=NULL); + /*TCP疑似ヘッダ部分*/ + /* IP protocol and length fields. This addition cannot carry. */ + sum = len + i_iph->proto; + /* Sum IP source and destination addresses. */ + sum = NyLPC_uip_chksum(sum, (NyLPC_TUInt8 *) &(i_iph->srcipaddr), 2 * sizeof(struct NyLPC_TIPv4Addr)); + /* Sum TCP header and data. */ + sum = NyLPC_uip_chksum(sum, (((NyLPC_TUInt8 *)(i_iph))+iph_len),len); + // sum = chksum(sum, &uip_buf[UIP_IPH_LEN + UIP_LLH_LEN], i_len_of_data); + return (sum == 0) ? 0xffff : NyLPC_htons(sum); +} + +static NyLPC_TUInt16 pid=0x3939; +/** + * IPヘッダを送信パケット用に設定する。 + * ipid16にはコールされるたびに新しい値を設定する。 + * ipcecksumには0を設定する。 + * この関数は、パケットサイズ,ローカルIP/リモートIPの設定はしない。 + */ +void NyLPC_TIPv4Header_writeTxIpHeader( + struct NyLPC_TIPv4Header* i_struct, + NyLPC_TUInt8 i_proto) +{ + //IPパケットのセット + i_struct->proto=i_proto; + i_struct->ttl = UIP_DEFAULT_IP_TTL; + i_struct->tos = 0; + i_struct->ipid16=(pid++); + i_struct->ipoffset=0;//NyLPC_HTONS(0|0x4000); + i_struct->ipchksum = 0; +} +/*-------------------------------------------------------------------------------- + * + * struct NyLPC_TIPv6Header + * + *------------------------------------------------------------------------------*/ +#define IPV6_HEADER_SIZE 40 +/** + * IPヘッダーを作って埋める関数 + */ +void NyLPC_TIPv6Header_setSendHeader( + struct NyLPC_TIPv6Header* i_iph, + uip_ip6addr_t i_src, + uip_ip6addr_t i_dest, + NyLPC_TUInt8 i_proto, + NyLPC_TUInt8 i_ttl, + NyLPC_TUInt16 i_len) +{ + i_iph->srcipaddr=i_src; + i_iph->destipaddr=i_dest; + i_iph->proto=i_proto; + i_iph->ttl = i_ttl; + i_iph->vtc = 0x60; + i_iph->tcflow = 0x00; + i_iph->flow = 0x00; + i_iph->len16= NyLPC_htons(i_len - IPV6_HEADER_SIZE); + return; +} + + +/** + * チェックサムは、TCP疑似ヘッダから計算。 + * i_tcpiphの送信/受信アドレス、ProtocolID,DATAフィールドは有効であること。 + */ +NyLPC_TUInt16 NyLPC_TIPv6Header_makeTcpChecksum( + struct NyLPC_TIPv6Header* i_iph, + NyLPC_TUInt16 i_len) +{ + NyLPC_TUInt16 sum; + NyLPC_TUInt16 len; + len = i_len; + /*TCP疑似ヘッダ部分*/ + /* IP protocol and length fields. This addition cannot carry. */ + sum = len + i_iph->proto; + /* Sum IP source and destination addresses. */ + sum = NyLPC_uip_chksum(sum, (NyLPC_TUInt8 *) &(i_iph->srcipaddr), 2 * sizeof(uip_ip6addr_t)); + /* Sum TCP header and data. */ + sum = NyLPC_uip_chksum(sum, (((NyLPC_TUInt8 *)(i_iph))+IPV6_HEADER_SIZE),len); + // sum = chksum(sum, &uip_buf[UIP_IPH_LEN + UIP_LLH_LEN], i_len_of_data); + return (sum == 0) ? 0xffff : NyLPC_htons(sum); +} + + + +/*-------------------------------------------------------------------------------- + * + * struct NyLPC_TTcpHeader + * + *------------------------------------------------------------------------------*/ + + +/** + * MMSオプションの値を返す。 + */ +NyLPC_TBool NyLPC_TTcpHeader_getTcpMmsOpt( + const struct NyLPC_TTcpHeader* i_struct,NyLPC_TUInt16* o_val) +{ + NyLPC_TUInt8* opt; + opt=NyLPC_TTcpHeader_getTcpOptFragmentPtr(i_struct,TCP_OPT_MSS); + if(opt!=NULL){ + if (*(opt+1) == TCP_OPT_MSS_LEN) + { + // An MSS option with the right option length. + *o_val = ((NyLPC_TUInt16) (*(opt+2)) << 8) | (NyLPC_TUInt16) (*(opt + 3)); + //And we are done processing options. + return NyLPC_TBool_TRUE; + } + } + return NyLPC_TBool_FALSE; +} + +#define DEFAULT_TCP_HEADER_LEN 20 +/** + * TCPフラグメントのポインタを返す。 + */ +NyLPC_TUInt8* NyLPC_TTcpHeader_getTcpOptFragmentPtr( + const struct NyLPC_TTcpHeader* i_struct, + NyLPC_TUInt8 i_opt_id) +{ + NyLPC_TUInt8 opt; + int c; + NyLPC_TUInt8* opt_buf=((NyLPC_TUInt8*)(i_struct+1)); + + /* Parse the TCP MSS option, if present. */ + if ((i_struct->tcpoffset & 0xf0) > 0x50){ + for (c = 0; c < ((i_struct->tcpoffset >> 4) - 5) << 2;) + { + opt=opt_buf[c]; + if(opt==i_opt_id){ + return opt_buf+c;//found! + } + switch(opt) + { + case TCP_OPT_NOOP: + continue;//NOP option. + case TCP_OPT_END: + return NULL;//End of options. + default: + // All other options have a length field, so that we easily can skip past them. + if (opt_buf[1 + c] == 0) { + // If the length field is zero, the options are malformed and we don't process them further. + NyLPC_OnErrorGoto(ERROR_INVALID_OPTION); + } + c += opt_buf[1 + c]; + } + } + } +ERROR_INVALID_OPTION: + return NULL; +} +/* + * Optionパラメタを書きだす。 + */ +void NyLPC_TTcpHeader_setMmsOpt(NyLPC_TUInt8* i_opt_addr,NyLPC_TUInt16 i_mms) +{ + i_opt_addr[0] = TCP_OPT_MSS; + i_opt_addr[1] = TCP_OPT_MSS_LEN; + i_opt_addr[2] = (i_mms) / 256; + i_opt_addr[3] = (i_mms) & 255; + return; +} + + +NyLPC_TUInt16 NyLPC_TTcpHeader_getHeaderLength(const struct NyLPC_TTcpHeader* ip_header) +{ + return (ip_header->tcpoffset>>4)*4; +} +/*-------------------------------------------------------------------------------- + * + * struct NyLPC_TUdpHeader + * + *------------------------------------------------------------------------------*/ + + + +/*-------------------------------------------------------------------------------- + * + * struct NyLPC_TArpHeader + * + *------------------------------------------------------------------------------*/ +/** + * i_req_addrを問い合わせるARP_REQUESTを生成します。 + */ +void NyLPC_TArpHeader_setArpRequest( + struct NyLPC_TArpHeader* i_struct, + const struct NyLPC_TIPv4Addr i_saddr, + const struct NyLPC_TEthAddr* i_srceth, + const struct NyLPC_TIPv4Addr* i_req_addr) +{ + memset(i_struct->dhwaddr.addr, 0x00, 6); + memcpy(i_struct->shwaddr.addr, i_srceth, 6); + i_struct->dipaddr=*i_req_addr; + i_struct->sipaddr=i_saddr; + i_struct->opcode = NyLPC_HTONS(ARP_REQUEST); /* ARP request. */ + i_struct->hwtype = NyLPC_HTONS(ARP_HWTYPE_ETH); + i_struct->protocol = NyLPC_HTONS(NyLPC_TEthernetIIHeader_TYPE_IP); + i_struct->hwlen = 6; + i_struct->protolen = 4; + return; +} + +/*-------------------------------------------------------------------------------- + * + * class IPv4Route + * + *------------------------------------------------------------------------------*/ + + + diff --git a/lib/src/uip/NyLPC_uip.h b/lib/src/netif/NyLPC_NetIf_ip_types.h similarity index 71% rename from lib/src/uip/NyLPC_uip.h rename to lib/src/netif/NyLPC_NetIf_ip_types.h index 01fc27f..68dc02e 100644 --- a/lib/src/uip/NyLPC_uip.h +++ b/lib/src/netif/NyLPC_NetIf_ip_types.h @@ -1,474 +1,480 @@ -/********************************************************************************* - * PROJECT: MiMic - * -------------------------------------------------------------------------------- - * - * This file is part of MiMic - * Copyright (C)2011 Ryo Iizuka - * - * MiMic is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published - * by the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program. If not, see . - * - * For further information please contact. - * http://nyatla.jp/ - * or - * - * - * Parts of this file were leveraged from uIP: - * - * Copyright (c) 2001-2003, Adam Dunkels. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote - * products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS - * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY - * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE - * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ -#ifndef NyLPC_uip_h -#define NyLPC_uip_h -#include "../include/NyLPC_config.h" -#include "../include/NyLPC_stdlib.h" -#include "NyLPC_uip_ethernet.h" - -#ifdef __cplusplus -extern "C" { -#endif /* __cplusplus */ - -#define UIP_PROTO_ICMP 1 -#define UIP_PROTO_TCP 6 -#define UIP_PROTO_UDP 17 -#define UIP_PROTO_ICMP6 58 - -#define ARP_REQUEST 1 -#define ARP_REPLY 2 -#define ARP_HWTYPE_ETH 1 - - -#ifndef PACK_STRUCT_END - #define PACK_STRUCT_END __attribute((packed)) -#endif - -/********************************************************************** - * - * - * - **********************************************************************/ - -/** - * IPアドレスを格納します。 - * IPアドレスは、ネットワークオーダーで設定します。 - */ -struct NyLPC_TIPv4Addr -{ - NyLPC_TUInt32 v; -}PACK_STRUCT_END; -extern const struct NyLPC_TIPv4Addr NyLPC_TIPv4Addr_ZERO; -extern const struct NyLPC_TIPv4Addr NyLPC_TIPv4Addr_ALL; -extern const struct NyLPC_TIPv4Addr NyLPC_TIPv4Addr_BROADCAST; -extern const struct NyLPC_TIPv4Addr NyLPC_TIPv4Addr_MULTICAST; -extern const struct NyLPC_TIPv4Addr NyLPC_TIPv4Addr_MULTICAST_MASK; -extern const struct NyLPC_TIPv4Addr NyLPC_TIPv4Addr_APIPA_MASK; - -/** - * addr1とaddr2が全く同じであるかをテストします。 - * \hideinitializer - */ -#define NyLPC_TIPv4Addr_isEqual(v1,v2) ((v1)->v==(v2)->v) -/** - * addr1とaddr2をmaskでマスクした結果を比較します。 +/********************************************************************************* + * PROJECT: MiMic + * -------------------------------------------------------------------------------- + * + * This file is part of MiMic + * Copyright (C)2011 Ryo Iizuka + * + * MiMic is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + * + * For further information please contact. + * http://nyatla.jp/ + * or + * + * + * Parts of this file were leveraged from uIP: + * + * Copyright (c) 2001-2003, Adam Dunkels. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +#ifndef NyLPC_NETIF_IP_TYPES_h +#define NyLPC_NETIF_IP_TYPES_h +#include "NyLPC_config.h" +#include "NyLPC_stdlib.h" +#include "NyLPC_NetIf_ethernet_types.h" +#include "NyLPC_NetIf_ip_types.h" + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +#define UIP_PROTO_ICMP 1 +#define UIP_PROTO_TCP 6 +#define UIP_PROTO_UDP 17 +#define UIP_PROTO_ICMP6 58 + +#define ARP_REQUEST 1 +#define ARP_REPLY 2 +#define ARP_HWTYPE_ETH 1 + +/** イーサネットヘッダのサイズ値*/ +#define UIP_ETHERHEADER_LEN 14 +#define UIP_TCPH_LEN 20 /* Size of TCP header */ +#define UIP_IPH_LEN 20 /* Size of IP header */ + +#ifndef PACK_STRUCT_END + #define PACK_STRUCT_END __attribute((packed)) +#endif + +/********************************************************************** + * + * + * + **********************************************************************/ + +/** + * IPアドレスを格納します。 + * IPアドレスは、ネットワークオーダーで設定します。 + */ +struct NyLPC_TIPv4Addr +{ + NyLPC_TUInt32 v; +}PACK_STRUCT_END; +extern const struct NyLPC_TIPv4Addr NyLPC_TIPv4Addr_ZERO; +extern const struct NyLPC_TIPv4Addr NyLPC_TIPv4Addr_ALL; +extern const struct NyLPC_TIPv4Addr NyLPC_TIPv4Addr_BROADCAST; +extern const struct NyLPC_TIPv4Addr NyLPC_TIPv4Addr_MULTICAST; +extern const struct NyLPC_TIPv4Addr NyLPC_TIPv4Addr_MULTICAST_MASK; +extern const struct NyLPC_TIPv4Addr NyLPC_TIPv4Addr_APIPA_MASK; + +/** + * addr1とaddr2が全く同じであるかをテストします。 * \hideinitializer - */ -#define NyLPC_TIPv4Addr_isEqualWithMask(addr1, addr2, mask) ((((addr1)->v) & ((mask)->v))==(((addr2)->v) & ((mask)->v))) - -/** - * 変数にIP v4アドレスをセットします。 - * 次のように使います。 - \code - NyLPC_TIPv4Addr ip; - NyLPC_TIpv4Addr_set(&ip,1,2,3,4); - \endcode - */ -#define NyLPC_TIPv4Addr_set(s,a0,a1,a2,a3) (s)->v=NyLPC_htonl((0xff000000&(((NyLPC_TUInt32)(a0))<<24))|(0x00ff0000&(((NyLPC_TUInt32)(a1))<<16))|(0x0000ff00&(((NyLPC_TUInt32)(a2))<<8))|(0x000000ff&((NyLPC_TUInt32)(a3)))) -#define NyLPC_TIPv4Addr_pack(a0,a1,a2,a3) {NyLPC_HTONL((0xff000000&(((NyLPC_TUInt32)(a0))<<24))|(0x00ff0000&(((NyLPC_TUInt32)(a1))<<16))|(0x0000ff00&(((NyLPC_TUInt32)(a2))<<8))|(0x000000ff&((NyLPC_TUInt32)(a3))))} - -/** - * IPアドレスを文字列に変換して返します。 - */ -NyLPC_TInt16 NyLPC_TIPv4Addr_toString(const struct NyLPC_TIPv4Addr* i_ip,NyLPC_TChar* i_buf); - - - - - -struct uip_ip6addr -{ - NyLPC_TUInt16 v[8]; -}PACK_STRUCT_END; - -typedef struct uip_ip6addr uip_ip6addr_t; - -NyLPC_TUInt16 NyLPC_uip_chksum(NyLPC_TUInt16 sum, const NyLPC_TUInt8 *data, NyLPC_TUInt16 len); - - - - -/** - * TTL - */ -#define UIP_DEFAULT_IP_TTL 64 - - - -/** - * The maximum number of times a segment should be retransmitted - * before the connection should be aborted. - * - * This should not be changed. - */ -#define UIP_MAXRTX 8 - - - - - -# if UIP_BYTE_ORDER == NyLPC_ENDIAN_BIG -# define NyLPC_HTONS(n) (n) -# define NyLPC_htons(n) (n) -# define NyLPC_ntohs(n) (n) -# define NyLPC_htonl(n) (n) -# define NyLPC_ntohl(n) (n) -# define NyLPC_HTONS(n) (n) -# define NyLPC_NTOHS(n) (n) -# else -# define NyLPC_htonl(n) NyLPC_TUInt32_bswap(n) -# define NyLPC_ntohl(n) NyLPC_TUInt32_bswap(n) -# define NyLPC_htons(n) NyLPC_TUInt16_bswap(n) -# define NyLPC_ntohs(n) NyLPC_TUInt16_bswap(n) -# define NyLPC_HTONS(n) NyLPC_TUInt16_BSWAP(n) -# define NyLPC_NTOHS(n) NyLPC_TUInt16_BSWAP(n) -# define NyLPC_NTOHL(n) NyLPC_TUInt32_BSWAP(n) -# define NyLPC_HTONL(n) NyLPC_TUInt32_BSWAP(n) -#endif /* NyLPC_HTONS */ - - - - -/********************************************************************** - * - * struct NyLPC_TEthernetIIHeader - * - **********************************************************************/ - -#define NyLPC_TEthernetIIHeader_TYPE_IP 0x0800 -#define NyLPC_TEthernetIIHeader_TYPE_ARP 0x0806 -#define NyLPC_TEthernetIIHeader_TYPE_IPV6 0x86DD -//#define UIP_ETHTYPE_IP 0x0800 -//#define UIP_ETHTYPE_ARP 0x0806 -//#define UIP_ETHTYPE_IP6 0x86dd - -struct NyLPC_TEthernetIIHeader -{ - struct NyLPC_TEthAddr dest; - struct NyLPC_TEthAddr src; - NyLPC_TUInt16 type; -}PACK_STRUCT_END; - - -NyLPC_TUInt16 NyLPC_TEthernetIIHeader_setArpTx( - struct NyLPC_TEthernetIIHeader* i_struct, - const struct NyLPC_TEthAddr* i_my_eth_addr); - -NyLPC_TUInt16 NyLPC_TEthernetIIHeader_setIPv4Tx( - struct NyLPC_TEthernetIIHeader* i_eth, - const struct NyLPC_TEthAddr* i_src_eth_addr, - const struct NyLPC_TEthAddr* i_dest_eth_addr); -/********************************************************************** - * - * struct NyLPC_TIPv4Header - * - **********************************************************************/ - - -/** - * IPパケットヘッダのメモリマップ構造体 + */ +#define NyLPC_TIPv4Addr_isEqual(v1,v2) ((v1)->v==(v2)->v) +/** + * addr1とaddr2をmaskでマスクした結果を比較します。 + * \hideinitializer + */ +#define NyLPC_TIPv4Addr_isEqualWithMask(addr1, addr2, mask) ((((addr1)->v) & ((mask)->v))==(((addr2)->v) & ((mask)->v))) + +/** + * 変数にIP v4アドレスをセットします。 + * 次のように使います。 + \code + NyLPC_TIPv4Addr ip; + NyLPC_TIpv4Addr_set(&ip,1,2,3,4); + \endcode + */ +#define NyLPC_TIPv4Addr_set(s,a0,a1,a2,a3) (s)->v=NyLPC_htonl((0xff000000&(((NyLPC_TUInt32)(a0))<<24))|(0x00ff0000&(((NyLPC_TUInt32)(a1))<<16))|(0x0000ff00&(((NyLPC_TUInt32)(a2))<<8))|(0x000000ff&((NyLPC_TUInt32)(a3)))) +#define NyLPC_TIPv4Addr_pack(a0,a1,a2,a3) {NyLPC_HTONL((0xff000000&(((NyLPC_TUInt32)(a0))<<24))|(0x00ff0000&(((NyLPC_TUInt32)(a1))<<16))|(0x0000ff00&(((NyLPC_TUInt32)(a2))<<8))|(0x000000ff&((NyLPC_TUInt32)(a3))))} + +/** + * IPアドレスを文字列に変換して返します。 + */ +NyLPC_TInt16 NyLPC_TIPv4Addr_toString(const struct NyLPC_TIPv4Addr* i_ip,NyLPC_TChar* i_buf); + + + + + +struct uip_ip6addr +{ + NyLPC_TUInt16 v[8]; +}PACK_STRUCT_END; + +typedef struct uip_ip6addr uip_ip6addr_t; + +NyLPC_TUInt16 NyLPC_uip_chksum(NyLPC_TUInt16 sum, const NyLPC_TUInt8 *data, NyLPC_TUInt16 len); + + + + +/** + * TTL + */ +#define UIP_DEFAULT_IP_TTL 64 + + + +/** + * The maximum number of times a segment should be retransmitted + * before the connection should be aborted. + * + * This should not be changed. + */ +#define UIP_MAXRTX 8 + + + + + +# if UIP_BYTE_ORDER == NyLPC_ENDIAN_BIG +# define NyLPC_HTONS(n) (n) +# define NyLPC_htons(n) (n) +# define NyLPC_ntohs(n) (n) +# define NyLPC_htonl(n) (n) +# define NyLPC_ntohl(n) (n) +# define NyLPC_HTONS(n) (n) +# define NyLPC_NTOHS(n) (n) +# else +# define NyLPC_htonl(n) NyLPC_TUInt32_bswap(n) +# define NyLPC_ntohl(n) NyLPC_TUInt32_bswap(n) +# define NyLPC_htons(n) NyLPC_TUInt16_bswap(n) +# define NyLPC_ntohs(n) NyLPC_TUInt16_bswap(n) +# define NyLPC_HTONS(n) NyLPC_TUInt16_BSWAP(n) +# define NyLPC_NTOHS(n) NyLPC_TUInt16_BSWAP(n) +# define NyLPC_NTOHL(n) NyLPC_TUInt32_BSWAP(n) +# define NyLPC_HTONL(n) NyLPC_TUInt32_BSWAP(n) +#endif /* NyLPC_HTONS */ + + + + +/********************************************************************** + * + * struct NyLPC_TEthernetIIHeader + * + **********************************************************************/ + +#define NyLPC_TEthernetIIHeader_TYPE_IP 0x0800 +#define NyLPC_TEthernetIIHeader_TYPE_ARP 0x0806 +#define NyLPC_TEthernetIIHeader_TYPE_IPV6 0x86DD +//#define UIP_ETHTYPE_IP 0x0800 +//#define UIP_ETHTYPE_ARP 0x0806 +//#define UIP_ETHTYPE_IP6 0x86dd + +struct NyLPC_TEthernetIIHeader +{ + struct NyLPC_TEthAddr dest; + struct NyLPC_TEthAddr src; + NyLPC_TUInt16 type; +}PACK_STRUCT_END; + + +NyLPC_TUInt16 NyLPC_TEthernetIIHeader_setArpTx( + struct NyLPC_TEthernetIIHeader* i_struct, + const struct NyLPC_TEthAddr* i_my_eth_addr); + +NyLPC_TUInt16 NyLPC_TEthernetIIHeader_setIPv4Tx( + struct NyLPC_TEthernetIIHeader* i_eth, + const struct NyLPC_TEthAddr* i_src_eth_addr, + const struct NyLPC_TEthAddr* i_dest_eth_addr); +/********************************************************************** + * + * struct NyLPC_TIPv4Header + * + **********************************************************************/ + + +/** + * IPパケットヘッダのメモリマップ構造体 * 値はすべてネットワークオーダーです。 - */ -struct NyLPC_TIPv4Header -{ - NyLPC_TUInt8 vhl; - NyLPC_TUInt8 tos; - NyLPC_TUInt16 len16; - NyLPC_TUInt16 ipid16; - NyLPC_TUInt16 ipoffset; - NyLPC_TUInt8 ttl; - NyLPC_TUInt8 proto; - NyLPC_TUInt16 ipchksum; - struct NyLPC_TIPv4Addr srcipaddr; - struct NyLPC_TIPv4Addr destipaddr; -}PACK_STRUCT_END; - - - - -NyLPC_TBool NyLPC_TIPv4Header_isCorrectIpCheckSum( - const struct NyLPC_TIPv4Header* ip_header); - -NyLPC_TBool NyLPC_cIPv4Packet_isCorrectTcpCheckSum( - const struct NyLPC_TIPv4Header* ip_header); - -NyLPC_TUInt16 NyLPC_TIPv4Header_makeIpChecksum(const struct NyLPC_TIPv4Header* ip_header); - - -NyLPC_TUInt16 NyLPC_TIPv4Header_makeTcpChecksum( - const struct NyLPC_TIPv4Header* i_iph); - -#define NyLPC_TIPv4Header_isDestAddrEqual(i_struct,i_addr) ((i_struct)->destipaddr==(i_addr)) -#define NyLPC_TIPv4Header_isSrcAddrEqual(i_struct,i_addr) ((i_struct)->srcipaddr==(i_addr)) - -/** - * IPヘッダの長さを返す。 - */ -#define NyLPC_TIPv4Header_getHeaderLength(i_iph) (((i_iph)->vhl & 0x0f)*4) -/** + */ +struct NyLPC_TIPv4Header +{ + NyLPC_TUInt8 vhl; + NyLPC_TUInt8 tos; + NyLPC_TUInt16 len16; + NyLPC_TUInt16 ipid16; + NyLPC_TUInt16 ipoffset; + NyLPC_TUInt8 ttl; + NyLPC_TUInt8 proto; + NyLPC_TUInt16 ipchksum; + struct NyLPC_TIPv4Addr srcipaddr; + struct NyLPC_TIPv4Addr destipaddr; +}PACK_STRUCT_END; + + + + +NyLPC_TBool NyLPC_TIPv4Header_isCorrectIpCheckSum( + const struct NyLPC_TIPv4Header* ip_header); + +NyLPC_TBool NyLPC_cIPv4Packet_isCorrectTcpCheckSum( + const struct NyLPC_TIPv4Header* ip_header); + +NyLPC_TUInt16 NyLPC_TIPv4Header_makeIpChecksum(const struct NyLPC_TIPv4Header* ip_header); + + +NyLPC_TUInt16 NyLPC_TIPv4Header_makeTcpChecksum( + const struct NyLPC_TIPv4Header* i_iph); + +#define NyLPC_TIPv4Header_isDestAddrEqual(i_struct,i_addr) ((i_struct)->destipaddr==(i_addr)) +#define NyLPC_TIPv4Header_isSrcAddrEqual(i_struct,i_addr) ((i_struct)->srcipaddr==(i_addr)) + +/** + * IPヘッダの長さを返す。 + */ +#define NyLPC_TIPv4Header_getHeaderLength(i_iph) (((i_iph)->vhl & 0x0f)*4) +/** * IPパケット全体の長さを返す。 - */ -#define NyLPC_TIPv4Header_getPacketLength(i_iph) (NyLPC_ntohs((i_iph)->len16)) - -/** - * IPヘッダを送信パケット用に設定する。 - * ipid16にはコールされるたびに新しい値を設定する。 - * ipcecksumには0を設定する。 - * この関数は、パケットサイズ,ローカルIP/リモートIPの設定はしない。 - */ -void NyLPC_TIPv4Header_writeTxIpHeader( - struct NyLPC_TIPv4Header* i_struct, - NyLPC_TUInt8 i_proto); -/********************************************************************** - * - * struct NyLPC_TIPv6Header - * - **********************************************************************/ - - -struct NyLPC_TIPv6Header -{ - /* IPv6 header. */ - NyLPC_TUInt8 vtc; - NyLPC_TUInt8 tcflow; - NyLPC_TUInt16 flow; - NyLPC_TUInt8 len16; - NyLPC_TUInt8 proto, ttl; - uip_ip6addr_t srcipaddr; - uip_ip6addr_t destipaddr; -}PACK_STRUCT_END; - -void NyLPC_TIPv6Header_setSendHeader( - struct NyLPC_TIPv6Header* i_iph, - uip_ip6addr_t i_src, - uip_ip6addr_t i_dest, - NyLPC_TUInt8 i_proto, - NyLPC_TUInt8 i_ttl, - NyLPC_TUInt16 i_len); - -NyLPC_TUInt16 NyLPC_TIPv6Header_makeTcpChecksum( - struct NyLPC_TIPv6Header* i_iph, - NyLPC_TUInt16 i_len); - -#define NyLPC_TIPv6Header_isDestAddrEqual(i_struct,i_addr) (memcmp((i_struct)->destipaddr,(i_addr),sizeof(uip_ip6addr_t))) -#define NyLPC_TIPv6Header_isSrcAddrEqual(i_struct,i_addr) (memcmp(i_struct)->srcipaddr,(i_addr),sizeof(uip_ip6addr_t))) -/********************************************************************** - * - * struct NyLPC_TTcpHeader - * - **********************************************************************/ -#define TCP_OPT_END 0 /* End of TCP options list */ -#define TCP_OPT_NOOP 1 /* "No-operation" TCP option */ -#define TCP_OPT_MSS 2 /* Maximum segment size TCP option */ -#define TCP_OPT_MSS_LEN 4 /* Length of TCP MSS option. */ - -/** - * TCP/IPヘッダのメモリマップ構造体 + */ +#define NyLPC_TIPv4Header_getPacketLength(i_iph) (NyLPC_ntohs((i_iph)->len16)) + +/** + * IPヘッダを送信パケット用に設定する。 + * ipid16にはコールされるたびに新しい値を設定する。 + * ipcecksumには0を設定する。 + * この関数は、パケットサイズ,ローカルIP/リモートIPの設定はしない。 + */ +void NyLPC_TIPv4Header_writeTxIpHeader( + struct NyLPC_TIPv4Header* i_struct, + NyLPC_TUInt8 i_proto); +/********************************************************************** + * + * struct NyLPC_TIPv6Header + * + **********************************************************************/ + + +struct NyLPC_TIPv6Header +{ + /* IPv6 header. */ + NyLPC_TUInt8 vtc; + NyLPC_TUInt8 tcflow; + NyLPC_TUInt16 flow; + NyLPC_TUInt8 len16; + NyLPC_TUInt8 proto, ttl; + uip_ip6addr_t srcipaddr; + uip_ip6addr_t destipaddr; +}PACK_STRUCT_END; + +void NyLPC_TIPv6Header_setSendHeader( + struct NyLPC_TIPv6Header* i_iph, + uip_ip6addr_t i_src, + uip_ip6addr_t i_dest, + NyLPC_TUInt8 i_proto, + NyLPC_TUInt8 i_ttl, + NyLPC_TUInt16 i_len); + +NyLPC_TUInt16 NyLPC_TIPv6Header_makeTcpChecksum( + struct NyLPC_TIPv6Header* i_iph, + NyLPC_TUInt16 i_len); + +#define NyLPC_TIPv6Header_isDestAddrEqual(i_struct,i_addr) (memcmp((i_struct)->destipaddr,(i_addr),sizeof(uip_ip6addr_t))) +#define NyLPC_TIPv6Header_isSrcAddrEqual(i_struct,i_addr) (memcmp(i_struct)->srcipaddr,(i_addr),sizeof(uip_ip6addr_t))) +/********************************************************************** + * + * struct NyLPC_TTcpHeader + * + **********************************************************************/ +#define TCP_OPT_END 0 /* End of TCP options list */ +#define TCP_OPT_NOOP 1 /* "No-operation" TCP option */ +#define TCP_OPT_MSS 2 /* Maximum segment size TCP option */ +#define TCP_OPT_MSS_LEN 4 /* Length of TCP MSS option. */ + +/** + * TCP/IPヘッダのメモリマップ構造体 * マルチバイトの値は、全てネットワークオーダーです。 - */ -struct NyLPC_TTcpHeader -{ - //TCP header. - NyLPC_TUInt16 srcport; - NyLPC_TUInt16 destport; - NyLPC_TUInt32 seqno32; - NyLPC_TUInt32 ackno32; - NyLPC_TUInt8 tcpoffset; - NyLPC_TUInt8 flags; - NyLPC_TUInt16 wnd16; - NyLPC_TUInt16 tcpchksum; - NyLPC_TUInt8 urgp[2]; -// NyLPC_TUInt8 optdata[4]; -} PACK_STRUCT_END; - - -NyLPC_TUInt8* NyLPC_TTcpHeader_getTcpOptFragmentPtr( - const struct NyLPC_TTcpHeader* i_struct, - NyLPC_TUInt8 i_opt_id); - -NyLPC_TBool NyLPC_TTcpHeader_getTcpMmsOpt( - const struct NyLPC_TTcpHeader* i_struct,NyLPC_TUInt16* o_val); - -/** - * この関数は、TCPヘッダの長さを返します。ヘッダの長さは、i_structのフィールドから計算します。 - * @param i_struct + */ +struct NyLPC_TTcpHeader +{ + //TCP header. + NyLPC_TUInt16 srcport; + NyLPC_TUInt16 destport; + NyLPC_TUInt32 seqno32; + NyLPC_TUInt32 ackno32; + NyLPC_TUInt8 tcpoffset; + NyLPC_TUInt8 flags; + NyLPC_TUInt16 wnd16; + NyLPC_TUInt16 tcpchksum; + NyLPC_TUInt8 urgp[2]; +// NyLPC_TUInt8 optdata[4]; +} PACK_STRUCT_END; + + +NyLPC_TUInt8* NyLPC_TTcpHeader_getTcpOptFragmentPtr( + const struct NyLPC_TTcpHeader* i_struct, + NyLPC_TUInt8 i_opt_id); + +NyLPC_TBool NyLPC_TTcpHeader_getTcpMmsOpt( + const struct NyLPC_TTcpHeader* i_struct,NyLPC_TUInt16* o_val); + +/** + * この関数は、TCPヘッダの長さを返します。ヘッダの長さは、i_structのフィールドから計算します。 + * @param i_struct * 構造体のアドレス。 - */ -NyLPC_TUInt16 NyLPC_TTcpHeader_getHeaderLength(const struct NyLPC_TTcpHeader* i_struct); - -/** + */ +NyLPC_TUInt16 NyLPC_TTcpHeader_getHeaderLength(const struct NyLPC_TTcpHeader* i_struct); + +/** * この関数は、指定したアドレスに、mmsオプション値を書き込みます。 - */ -void NyLPC_TTcpHeader_setMmsOpt(NyLPC_TUInt8* i_opt_addr,NyLPC_TUInt16 i_mms); - - -/********************************************************************** - * - * struct NyLPC_TUdpHeader - * - **********************************************************************/ - -/** - * UDP/IPヘッダのメモリマップ構造体 - */ -struct NyLPC_TUdpHeader -{ - NyLPC_TUInt16 srcport; - NyLPC_TUInt16 destport; - NyLPC_TUInt16 udplen; - NyLPC_TUInt16 udpchksum; -} PACK_STRUCT_END; - -/** - * UDPヘッダの長さを返す。 - */ -#define NyLPC_TUdpHeader_getHeaderLength(i_struct) (8) - -/********************************************************************** - * - * struct NyLPC_TIcmpipHeader - * - **********************************************************************/ - - -struct NyLPC_TIcmpHeader -{ - /* ICMP (echo) header. */ - NyLPC_TUInt8 type, icode; - NyLPC_TUInt16 icmpchksum; - #if !UIP_CONF_IPV6 - NyLPC_TUInt16 id, seqno; - #else /* !UIP_CONF_IPV6 */ - NyLPC_TUInt8 flags, reserved1, reserved2, reserved3; - NyLPC_TUInt8 icmp6data[16]; - NyLPC_TUInt8 options[1]; - #endif /* !UIP_CONF_IPV6 */ -} PACK_STRUCT_END; - -/********************************************************************** - * - * struct NyLPC_TIcmpipHeader - * - **********************************************************************/ - -struct NyLPC_TArpHeader -{ - NyLPC_TUInt16 hwtype; - NyLPC_TUInt16 protocol; - NyLPC_TUInt8 hwlen; - NyLPC_TUInt8 protolen; - NyLPC_TUInt16 opcode; - struct NyLPC_TEthAddr shwaddr; - struct NyLPC_TIPv4Addr sipaddr; - struct NyLPC_TEthAddr dhwaddr; - struct NyLPC_TIPv4Addr dipaddr; -} PACK_STRUCT_END; - - -/** - * i_req_addrを問い合わせるARP_REQUESTを生成します。 - */ -void NyLPC_TArpHeader_setArpRequest( - struct NyLPC_TArpHeader* i_struct, - const struct NyLPC_TIPv4Addr i_saddr, - const struct NyLPC_TEthAddr* i_srceth, - const struct NyLPC_TIPv4Addr* i_req_addr); - - -typedef struct NyLPC_TcEthernetIIPayload NyLPC_TcEthernetIIPayload_t; - - - - -/********************************************************************** - * - * NyLPC_TIPv6Payload - * - **********************************************************************/ - - -struct NyLPC_TIPv6Payload -{ - struct NyLPC_TIPv6Header* header; - union{ - void* rawbuf; - void* tcp; - void* udp; - void* icmp6; - }payload; -}; - -struct NyLPC_TcEthernetIIPayload -{ - struct NyLPC_TEthernetIIHeader* header; - union{ - void* rawbuf; - struct NyLPC_TArpHeader* arp; - struct NyLPC_TIPv4Payload* ipv4; - struct NyLPC_TIPv6Payload* ipv6; - }payload; - NyLPC_TUInt16 len; -}; - - - - - -#ifdef __cplusplus -} -#endif /* __cplusplus */ - - -#endif + */ +void NyLPC_TTcpHeader_setMmsOpt(NyLPC_TUInt8* i_opt_addr,NyLPC_TUInt16 i_mms); + + +/********************************************************************** + * + * struct NyLPC_TUdpHeader + * + **********************************************************************/ + +/** + * UDP/IPヘッダのメモリマップ構造体 + */ +struct NyLPC_TUdpHeader +{ + NyLPC_TUInt16 srcport; + NyLPC_TUInt16 destport; + NyLPC_TUInt16 udplen; + NyLPC_TUInt16 udpchksum; +} PACK_STRUCT_END; + +/** + * UDPヘッダの長さを返す。 + */ +#define NyLPC_TUdpHeader_getHeaderLength(i_struct) (8) + +/********************************************************************** + * + * struct NyLPC_TIcmpipHeader + * + **********************************************************************/ + + +struct NyLPC_TIcmpHeader +{ + /* ICMP (echo) header. */ + NyLPC_TUInt8 type, icode; + NyLPC_TUInt16 icmpchksum; + #if !UIP_CONF_IPV6 + NyLPC_TUInt16 id, seqno; + #else /* !UIP_CONF_IPV6 */ + NyLPC_TUInt8 flags, reserved1, reserved2, reserved3; + NyLPC_TUInt8 icmp6data[16]; + NyLPC_TUInt8 options[1]; + #endif /* !UIP_CONF_IPV6 */ +} PACK_STRUCT_END; + +/********************************************************************** + * + * struct NyLPC_TIcmpipHeader + * + **********************************************************************/ + +struct NyLPC_TArpHeader +{ + NyLPC_TUInt16 hwtype; + NyLPC_TUInt16 protocol; + NyLPC_TUInt8 hwlen; + NyLPC_TUInt8 protolen; + NyLPC_TUInt16 opcode; + struct NyLPC_TEthAddr shwaddr; + struct NyLPC_TIPv4Addr sipaddr; + struct NyLPC_TEthAddr dhwaddr; + struct NyLPC_TIPv4Addr dipaddr; +} PACK_STRUCT_END; + + +/** + * i_req_addrを問い合わせるARP_REQUESTを生成します。 + */ +void NyLPC_TArpHeader_setArpRequest( + struct NyLPC_TArpHeader* i_struct, + const struct NyLPC_TIPv4Addr i_saddr, + const struct NyLPC_TEthAddr* i_srceth, + const struct NyLPC_TIPv4Addr* i_req_addr); + + +typedef struct NyLPC_TcEthernetIIPayload NyLPC_TcEthernetIIPayload_t; + + + + +/********************************************************************** + * + * NyLPC_TIPv6Payload + * + **********************************************************************/ + + +struct NyLPC_TIPv6Payload +{ + struct NyLPC_TIPv6Header* header; + union{ + void* rawbuf; + void* tcp; + void* udp; + void* icmp6; + }payload; +}; + +struct NyLPC_TcEthernetIIPayload +{ + struct NyLPC_TEthernetIIHeader* header; + union{ + void* rawbuf; + struct NyLPC_TArpHeader* arp; + struct NyLPC_TIPv4Payload* ipv4; + struct NyLPC_TIPv6Payload* ipv6; + }payload; + NyLPC_TUInt16 len; +}; + + + + + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + + +#endif + diff --git a/lib/src/uip/NyLPC_cIPv4Config.c b/lib/src/netif/NyLPC_cIPv4Config.c similarity index 70% rename from lib/src/uip/NyLPC_cIPv4Config.c rename to lib/src/netif/NyLPC_cIPv4Config.c index 71c2e67..602a6fa 100644 --- a/lib/src/uip/NyLPC_cIPv4Config.c +++ b/lib/src/netif/NyLPC_cIPv4Config.c @@ -1,73 +1,72 @@ -/********************************************************************************* - * PROJECT: MiMic - * -------------------------------------------------------------------------------- - * - * This file is part of MiMic - * Copyright (C)2011 Ryo Iizuka - * - * MiMic is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published - * by the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program. If not, see . - * - * For further information please contact. - * http://nyatla.jp/ - * or - * - *********************************************************************************/ -#include "NyLPC_uip.h" -#include "NyLPC_cIPv4Payload_protected.h" -#include "NyLPC_cIPv4Config.h" - -/** イーサネットヘッダのサイズ値*/ -#define UIP_ETHERHEADER_LEN 14 - -/** - * See header file. - */ -void NyLPC_cIPv4Config_initialzeForEthernet(NyLPC_TcIPv4Config_t* i_inst,const struct NyLPC_TEthAddr* i_ether_addr,NyLPC_TUInt16 i_ether_frame_len) -{ - i_inst->ip_addr=i_inst->netmask=i_inst->dr_addr=NyLPC_TIPv4Addr_ZERO; - i_inst->eth_mac=*i_ether_addr; - //mssの計算 - i_inst->default_mss=i_ether_frame_len-(UIP_ETHERHEADER_LEN+UIP_TCPH_LEN + UIP_IPH_LEN); - return; -} -void NyLPC_cIPv4Config_initialzeCopy(NyLPC_TcIPv4Config_t* i_inst,const NyLPC_TcIPv4Config_t* i_src) -{ - memcpy(i_inst,i_src,sizeof(NyLPC_TcIPv4Config_t)); -} -/** - * See header file. - */ -void NyLPC_cIPv4Config_setDefaultRoute(NyLPC_TcIPv4Config_t* i_inst,const struct NyLPC_TIPv4Addr* i_dr_addr) -{ - i_inst->dr_addr=*i_dr_addr; - return; -} - -/** - * See header file. - */ -void NyLPC_cIPv4Config_setIp(NyLPC_TcIPv4Config_t* i_inst,const struct NyLPC_TIPv4Addr* i_ipaddr,const struct NyLPC_TIPv4Addr* i_netmask) -{ - i_inst->ip_addr=*i_ipaddr; - i_inst->netmask=*i_netmask; - return; -} - -/** - * See header file. - */ -NyLPC_TBool NyLPC_cIPv4Config_isLocalIP(const NyLPC_TcIPv4Config_t* i_inst,const struct NyLPC_TIPv4Addr* i_target_ip) -{ - return NyLPC_TIPv4Addr_isEqualWithMask(&(i_inst->ip_addr),i_target_ip,&(i_inst->netmask)); -} +/********************************************************************************* + * PROJECT: MiMic + * -------------------------------------------------------------------------------- + * + * This file is part of MiMic + * Copyright (C)2011 Ryo Iizuka + * + * MiMic is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + * + * For further information please contact. + * http://nyatla.jp/ + * or + * + *********************************************************************************/ + +#include "NyLPC_cIPv4Config.h" + + + +/** + * See header file. + */ +void NyLPC_cIPv4Config_initialzeForEthernet(NyLPC_TcIPv4Config_t* i_inst,const struct NyLPC_TEthAddr* i_ether_addr,NyLPC_TUInt16 i_ether_frame_len) +{ + i_inst->ip_addr=i_inst->netmask=i_inst->dr_addr=NyLPC_TIPv4Addr_ZERO; + i_inst->eth_mac=*i_ether_addr; + //mssの計算 + i_inst->default_mss=i_ether_frame_len-(UIP_ETHERHEADER_LEN+UIP_TCPH_LEN + UIP_IPH_LEN); + return; +} +void NyLPC_cIPv4Config_initialzeCopy(NyLPC_TcIPv4Config_t* i_inst,const NyLPC_TcIPv4Config_t* i_src) +{ + memcpy(i_inst,i_src,sizeof(NyLPC_TcIPv4Config_t)); +} +/** + * See header file. + */ +void NyLPC_cIPv4Config_setDefaultRoute(NyLPC_TcIPv4Config_t* i_inst,const struct NyLPC_TIPv4Addr* i_dr_addr) +{ + i_inst->dr_addr=*i_dr_addr; + return; +} + +/** + * See header file. + */ +void NyLPC_cIPv4Config_setIp(NyLPC_TcIPv4Config_t* i_inst,const struct NyLPC_TIPv4Addr* i_ipaddr,const struct NyLPC_TIPv4Addr* i_netmask) +{ + i_inst->ip_addr=*i_ipaddr; + i_inst->netmask=*i_netmask; + return; +} + +/** + * See header file. + */ +NyLPC_TBool NyLPC_cIPv4Config_isLocalIP(const NyLPC_TcIPv4Config_t* i_inst,const struct NyLPC_TIPv4Addr* i_target_ip) +{ + return NyLPC_TIPv4Addr_isEqualWithMask(&(i_inst->ip_addr),i_target_ip,&(i_inst->netmask)); +} + diff --git a/lib/src/uip/NyLPC_cIPv4Config.h b/lib/src/netif/NyLPC_cIPv4Config.h similarity index 84% rename from lib/src/uip/NyLPC_cIPv4Config.h rename to lib/src/netif/NyLPC_cIPv4Config.h index c11c160..f661665 100644 --- a/lib/src/uip/NyLPC_cIPv4Config.h +++ b/lib/src/netif/NyLPC_cIPv4Config.h @@ -1,134 +1,135 @@ -/********************************************************************************* - * PROJECT: MiMic - * -------------------------------------------------------------------------------- - * - * This file is part of MiMic - * Copyright (C)2011 Ryo Iizuka - * - * MiMic is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published - * by the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program. If not, see . - * - * For further information please contact. - * http://nyatla.jp/ - * or - * - *********************************************************************************/ -/** - * @file - * このファイルは、NyLPC_cIPv4Configクラスを定義します。 - */ -#ifndef NYLPC_CIPV4CONFIG_H_ -#define NYLPC_CIPV4CONFIG_H_ - - -#include "NyLPC_uip.h" - -/** - * クラス型を定義します。 - * NyLPC_cIPv4Configクラスは、IPと、下位のネットワーク層の設定を保持します。 - * 関連するオブジェクトが、ネットワーク設定を問い合わせる為に使います。 - */ -typedef struct NyLPC_TcIPv4Config NyLPC_TcIPv4Config_t; - -#ifdef __cplusplus -extern "C" { -#endif /* __cplusplus */ -/********************************************************************** - * - * class NyLPC_TcIPv4Config - * - **********************************************************************/ - - - - -/** - * NyLPC_TcIPv4Configクラスの構造体です。 - */ -struct NyLPC_TcIPv4Config -{ - /** イーサネットアドレスを格納します。 */ - struct NyLPC_TEthAddr eth_mac; - /** IPアドレスを格納します。Network orderです。 */ - struct NyLPC_TIPv4Addr ip_addr; - /** ネットマスクを格納します。Network orderです。 */ - struct NyLPC_TIPv4Addr netmask; - /** デフォルトゲートウェイアドレスを格納します。Network orderです。 */ - struct NyLPC_TIPv4Addr dr_addr; - /** デフォルトMMSサイズです。送信パケットのMSS値、受信パケットのデフォルトMSS値として使います。 */ - NyLPC_TUInt16 default_mss; -}; - -#define NyLPC_TcIPv4Config_getEtherMac000120203(v)(((v)->eth_mac.addr[0]<<24)|((v)->eth_mac.addr[1]<<16)|((v)->eth_mac.addr[2]<<8)|((v)->eth_mac.addr[3]<<0)) -#define NyLPC_TcIPv4Config_getEtherMac0405xxxx(v) (((v)->eth_mac.addr[4]<<24)|((v)->eth_mac.addr[5]<<16)) - -/** - * コンストラクタです。 - * イーサネット用にコンフィギュレーションを初期化します。 - * @param i_inst - * 初期化するインスタンスです。 - * @param i_ether_frame_len - * イーサネットフレームのサイズ。この数値から、MSSのデフォルト値を計算します。 - */ -void NyLPC_cIPv4Config_initialzeForEthernet(NyLPC_TcIPv4Config_t* i_inst,const struct NyLPC_TEthAddr* i_ether_addr,NyLPC_TUInt16 i_ether_frame_len); - -/** - * コピーコンストラクタ - */ -void NyLPC_cIPv4Config_initialzeCopy(NyLPC_TcIPv4Config_t* i_inst,const NyLPC_TcIPv4Config_t* i_src); - - -/** - * デストラクタです。インスタンスを破棄して、確保している動的リソースを元に戻します。 - * @param i_inst - * 破棄するインスタンスです。 - * initializeに成功したインスタンスだけが指定できます。 - */ -#define NyLPC_cIPv4Config_finalize(i_inst) - -/** - * この関数は、IPのデフォルトゲートウェイを設定します。dr_addrの値を更新します。 - * @param i_inst - * 操作するインスタンスです。 - * @param i_dr_addr - * 設定するIPアドレスを格納したアドレスです。 - */ -void NyLPC_cIPv4Config_setDefaultRoute(NyLPC_TcIPv4Config_t* i_inst,const struct NyLPC_TIPv4Addr* i_dr_addr); - -/** - * この関数は、ローカルIPアドレスとネットマスクを設定します。 - * @param i_inst - * 操作するインスタンスです。 - * @param i_ipaddr - * 設定するIPアドレスを格納したアドレスです。 - * @param i_netmask - * 設定するネットマスクを格納したアドレスです。 - */ -void NyLPC_cIPv4Config_setIp(NyLPC_TcIPv4Config_t* i_inst,const struct NyLPC_TIPv4Addr* i_ipaddr,const struct NyLPC_TIPv4Addr* i_netmask); - -/** - * この関数は、i_target_ipが、現在のIPアドレスに対するローカルアドレスであるかを返します。 - * @param i_inst - * 操作するインスタンスです。 - * @param i_target_ip - * 確認するIPアドレスです。 - * @return - * i_target_ipがローカルIPアドレスなら、TRUEを返します。 - */ -NyLPC_TBool NyLPC_cIPv4Config_isLocalIP(const NyLPC_TcIPv4Config_t* i_inst,const struct NyLPC_TIPv4Addr* i_target_ip); - -#ifdef __cplusplus -} -#endif /* __cplusplus */ - -#endif +/********************************************************************************* + * PROJECT: MiMic + * -------------------------------------------------------------------------------- + * + * This file is part of MiMic + * Copyright (C)2011 Ryo Iizuka + * + * MiMic is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + * + * For further information please contact. + * http://nyatla.jp/ + * or + * + *********************************************************************************/ +/** + * @file + * このファイルは、NyLPC_cIPv4Configクラスを定義します。 + */ +#ifndef NYLPC_CIPV4CONFIG_H_ +#define NYLPC_CIPV4CONFIG_H_ + + +#include "NyLPC_NetIf_ip_types.h" + +/** + * クラス型を定義します。 + * NyLPC_cIPv4Configクラスは、IPと、下位のネットワーク層の設定を保持します。 + * 関連するオブジェクトが、ネットワーク設定を問い合わせる為に使います。 + */ +typedef struct NyLPC_TcIPv4Config NyLPC_TcIPv4Config_t; + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ +/********************************************************************** + * + * class NyLPC_TcIPv4Config + * + **********************************************************************/ + + + + +/** + * NyLPC_TcIPv4Configクラスの構造体です。 + */ +struct NyLPC_TcIPv4Config +{ + /** イーサネットアドレスを格納します。 */ + struct NyLPC_TEthAddr eth_mac; + /** IPアドレスを格納します。Network orderです。 */ + struct NyLPC_TIPv4Addr ip_addr; + /** ネットマスクを格納します。Network orderです。 */ + struct NyLPC_TIPv4Addr netmask; + /** デフォルトゲートウェイアドレスを格納します。Network orderです。 */ + struct NyLPC_TIPv4Addr dr_addr; + /** デフォルトMMSサイズです。送信パケットのMSS値、受信パケットのデフォルトMSS値として使います。 */ + NyLPC_TUInt16 default_mss; +}; + +#define NyLPC_TcIPv4Config_getEtherMac000120203(v)(((v)->eth_mac.addr[0]<<24)|((v)->eth_mac.addr[1]<<16)|((v)->eth_mac.addr[2]<<8)|((v)->eth_mac.addr[3]<<0)) +#define NyLPC_TcIPv4Config_getEtherMac0405xxxx(v) (((v)->eth_mac.addr[4]<<24)|((v)->eth_mac.addr[5]<<16)) + +/** + * コンストラクタです。 + * イーサネット用にコンフィギュレーションを初期化します。 + * @param i_inst + * 初期化するインスタンスです。 + * @param i_ether_frame_len + * イーサネットフレームのサイズ。この数値から、MSSのデフォルト値を計算します。 + */ +void NyLPC_cIPv4Config_initialzeForEthernet(NyLPC_TcIPv4Config_t* i_inst,const struct NyLPC_TEthAddr* i_ether_addr,NyLPC_TUInt16 i_ether_frame_len); + +/** + * コピーコンストラクタ + */ +void NyLPC_cIPv4Config_initialzeCopy(NyLPC_TcIPv4Config_t* i_inst,const NyLPC_TcIPv4Config_t* i_src); + + +/** + * デストラクタです。インスタンスを破棄して、確保している動的リソースを元に戻します。 + * @param i_inst + * 破棄するインスタンスです。 + * initializeに成功したインスタンスだけが指定できます。 + */ +#define NyLPC_cIPv4Config_finalize(i_inst) + +/** + * この関数は、IPのデフォルトゲートウェイを設定します。dr_addrの値を更新します。 + * @param i_inst + * 操作するインスタンスです。 + * @param i_dr_addr + * 設定するIPアドレスを格納したアドレスです。 + */ +void NyLPC_cIPv4Config_setDefaultRoute(NyLPC_TcIPv4Config_t* i_inst,const struct NyLPC_TIPv4Addr* i_dr_addr); + +/** + * この関数は、ローカルIPアドレスとネットマスクを設定します。 + * @param i_inst + * 操作するインスタンスです。 + * @param i_ipaddr + * 設定するIPアドレスを格納したアドレスです。 + * @param i_netmask + * 設定するネットマスクを格納したアドレスです。 + */ +void NyLPC_cIPv4Config_setIp(NyLPC_TcIPv4Config_t* i_inst,const struct NyLPC_TIPv4Addr* i_ipaddr,const struct NyLPC_TIPv4Addr* i_netmask); + +/** + * この関数は、i_target_ipが、現在のIPアドレスに対するローカルアドレスであるかを返します。 + * @param i_inst + * 操作するインスタンスです。 + * @param i_target_ip + * 確認するIPアドレスです。 + * @return + * i_target_ipがローカルIPアドレスなら、TRUEを返します。 + */ +NyLPC_TBool NyLPC_cIPv4Config_isLocalIP(const NyLPC_TcIPv4Config_t* i_inst,const struct NyLPC_TIPv4Addr* i_target_ip); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif + diff --git a/lib/src/netif/NyLPC_cNet.c b/lib/src/netif/NyLPC_cNet.c new file mode 100644 index 0000000..91205e9 --- /dev/null +++ b/lib/src/netif/NyLPC_cNet.c @@ -0,0 +1,101 @@ + +#include "NyLPC_cNet.h" +#include "./mimicip/NyLPC_cMiMicIpNetIf_protected.h" +#include "dhcp/NyLPC_cDhcpClient.h" +#include "apipa/NyLPC_cApipa.h" + + +/** + * 唯一のネットワークインタフェイス + */ +const static struct NyLPC_TiNetInterface_Interface* netif; + + + + + +void NyLPC_cNet_initialize(const struct NyLPC_TiNetInterface_Interface* i_netif) +{ + NyLPC_Assert(netif==NULL); + netif=i_netif; +} + +void NyLPC_cNet_start(const NyLPC_TcIPv4Config_t* i_ref_config) +{ + netif->start(i_ref_config); + return; +} + +void NyLPC_cNet_stop(void) +{ + netif->stop(); + return; +} + + + +/** + * 指定したIPアドレスを要求するARPリクエストを発行します。 + */ +void NyLPC_cNet_sendArpRequest(const struct NyLPC_TIPv4Addr* i_addr) +{ + netif->sendarprequest(i_addr); +} +/** + * ARPテーブルに指定したIPがあるかを返します。 + */ +NyLPC_TBool NyLPC_cNet_hasArpInfo(const struct NyLPC_TIPv4Addr* i_addr) +{ + return netif->hasarpinfo(i_addr); +} + +NyLPC_TBool NyLPC_cNet_isInitService(void) +{ + return netif->isinitservice(); +} + +NyLPC_TiTcpSocket_t* NyLPC_cNet_createTcpSocketEx(NyLPC_TSocketType i_socktype) +{ + return netif->createTcpSocketEx(i_socktype); +} +NyLPC_TiUdpSocket_t* NyLPC_cNet_createUdpSocketEx(NyLPC_TUInt16 i_port,NyLPC_TSocketType i_socktype) +{ + return netif->createUdpSocetEx(i_port,i_socktype); +} +NyLPC_TiTcpListener_t* NyLPC_cNet_createTcpListenerEx(NyLPC_TUInt16 i_port) +{ + return netif->createTcpListener(i_port); +} + +const struct NyLPC_TNetInterfaceInfo* NyLPC_cNet_getInterfaceInfo(void) +{ + return netif->getinterfaceinfo(); +} + + + +NyLPC_TBool NyLPC_cNet_requestAddrDhcp(NyLPC_TcIPv4Config_t* i_cfg,NyLPC_TInt16 i_repeat) +{ + NyLPC_TBool ret; + NyLPC_TcDhcpClient_t sock; + //netを開始 + NyLPC_cDhcpClient_initialize(&sock); + ret=NyLPC_cDhcpClient_requestAddr(&sock,i_cfg,i_repeat); + NyLPC_cDhcpClient_finalize(&sock); + return ret; +} + +NyLPC_TBool NyLPC_cNet_requestAddrApipa(NyLPC_TcIPv4Config_t* i_cfg,NyLPC_TInt16 i_repeat) +{ + NyLPC_TBool ret; + NyLPC_TcApipa_t sock; + //netを開始 + NyLPC_cApipa_initialize(&sock); + ret=NyLPC_cApipa_requestAddr(&sock,i_cfg,i_repeat); + NyLPC_cApipa_finalize(&sock); + return ret; +} + + + + diff --git a/lib/src/netif/NyLPC_cNet.h b/lib/src/netif/NyLPC_cNet.h new file mode 100644 index 0000000..f681ec1 --- /dev/null +++ b/lib/src/netif/NyLPC_cNet.h @@ -0,0 +1,69 @@ +#ifndef NYLPC_CNET_H_ +#define NYLPC_CNET_H_ +#include "NyLPC_stdlib.h" +#include "NyLPC_cIPv4Config.h" +#include "NyLPC_iTcpListener.h" +#include "NyLPC_iTcpSocket.h" +#include "NyLPC_iUdpSocket.h" +#include "NyLPC_iNetInterface.h" +#include "NyLPC_NetIf_ip_types.h" + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +typedef struct NyLPC_TcNet NyLPC_TcNet_t; + +void NyLPC_cNet_initialize(const struct NyLPC_TiNetInterface_Interface* i_netif); +#define NyLPC_cNet_finalize() + +NyLPC_TiTcpSocket_t* NyLPC_cNet_createTcpSocketEx(NyLPC_TSocketType i_socktype); +NyLPC_TiUdpSocket_t* NyLPC_cNet_createUdpSocketEx(NyLPC_TUInt16 i_port,NyLPC_TSocketType i_socktype); +NyLPC_TiTcpListener_t* NyLPC_cNet_createTcpListenerEx(NyLPC_TUInt16 i_port); + +/** + * 指定したIPアドレスを要求するARPリクエストを発行します。 + */ +void NyLPC_cNet_sendArpRequest(const struct NyLPC_TIPv4Addr* i_addr); + +/** + * ARPテーブルに指定したIPがあるかを返します。 + */ +NyLPC_TBool NyLPC_cNet_hasArpInfo(const struct NyLPC_TIPv4Addr* i_addr); + +void NyLPC_cNet_start(const NyLPC_TcIPv4Config_t* i_ref_config); +void NyLPC_cNet_stop(void); +NyLPC_TBool NyLPC_cNet_isInitService(void); + +/** + * NyLPC_TcIPv4Config_tをDHCPで更新します。 + * この関数をコールする時は、サービスは停止中でなければなりません。 + * @param i_cfg + * 更新するi_cfg構造体。 + * emac,default_mssは設定済である必要があります。他のフィールド値は不定で構いません。 + * 更新されるフィールドは、ip,netmast,default_rootの3つです。 + * @return + * 更新に成功した場合TRUE + */ +NyLPC_TBool NyLPC_cNet_requestAddrDhcp(NyLPC_TcIPv4Config_t* i_cfg,NyLPC_TInt16 i_repeat); +/** + * NyLPC_TcIPv4Config_tをAPIPAで更新します。 + * この関数をコールする時は、サービスは停止中でなければなりません。 + * @param i_cfg + * 更新するi_cfg構造体。 + * emac,default_mssは設定済である必要があります。他のフィールド値は不定で構いません。 + * 更新されるフィールドは、ip,netmast,default_rootの3つです。 + * @return + * 更新に成功した場合TRUE + */ +NyLPC_TBool NyLPC_cNet_requestAddrApipa(NyLPC_TcIPv4Config_t* i_cfg,NyLPC_TInt16 i_repeat); + +const struct NyLPC_TNetInterfaceInfo* NyLPC_cNet_getInterfaceInfo(void); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif + + diff --git a/lib/src/netif/NyLPC_iNetInterface.h b/lib/src/netif/NyLPC_iNetInterface.h new file mode 100644 index 0000000..b929a6e --- /dev/null +++ b/lib/src/netif/NyLPC_iNetInterface.h @@ -0,0 +1,98 @@ +/********************************************************************************* + * PROJECT: MiMic + * -------------------------------------------------------------------------------- + * + * This file is part of MiMic + * Copyright (C)2011 Ryo Iizuka + * + * MiMic is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + * + * For further information please contact. + * http://nyatla.jp/ + * or + * + *********************************************************************************/ +#ifndef NYLPC_INETINTERFACE_H_ +#define NYLPC_INETINTERFACE_H_ + + +#include "NyLPC_stdlib.h" +#include "NyLPC_os.h" +#include "NyLPC_iTcpSocket.h" +#include "NyLPC_iUdpSocket.h" +#include "NyLPC_iTcpListener.h" +#include "NyLPC_cIPv4Config.h" +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +struct NyLPC_TNetInterfaceInfo{ + const char* device_name; + const NyLPC_TcIPv4Config_t* current_config; +}; + +/** + * ソケット生成のヒント値 + */ +typedef NyLPC_TUInt16 NyLPC_TSocketType; +#define NyLPC_TSocketType_TCP_NORMAL 0x0100 +#define NyLPC_TSocketType_TCP_HTTP 0x0101 +#define NyLPC_TSocketType_UDP_NORMAL 0x0200 +#define NyLPC_TSocketType_UDP_NOBUF 0x0202 +/********************************************************************** + * Function + **********************************************************************/ + +/** + * 制限時間付きでソケットに接続を待ちます。 + */ +typedef NyLPC_TiTcpSocket_t* (*NyLPC_TiNetInterface_createTcpSocetEx)(NyLPC_TSocketType i_socktype); +typedef NyLPC_TiUdpSocket_t* (*NyLPC_TiNetInterface_createUdpSocetEx)(NyLPC_TUInt16 i_port,NyLPC_TSocketType i_socktype); +typedef NyLPC_TiTcpListener_t* (*NyLPC_TiNetInterface_createTcpListener)(NyLPC_TUInt16 i_port); +/** + * Start関数はイベント関数の定期コールが開始される前に呼び出されます。 + */ +typedef void (*NyLPC_TiNetInterface_start)(const NyLPC_TcIPv4Config_t* i_cfg); +typedef void (*NyLPC_TiNetInterface_stop)(void); + +typedef void(*NyLPC_TiNetInterface_sendArpRequest)(const struct NyLPC_TIPv4Addr* i_addr); +typedef NyLPC_TBool(*NyLPC_TiNetInterface_hasArpInfo)(const struct NyLPC_TIPv4Addr* i_addr); +typedef NyLPC_TBool(*NyLPC_TiNetInterface_isInitService)(void); + +typedef const struct NyLPC_TNetInterfaceInfo* (*NyLPC_TiNetInterface_getInterfaceInfo)(void); + +/********************************************************************** + * Interface + **********************************************************************/ +struct NyLPC_TiNetInterface_Interface +{ + NyLPC_TiNetInterface_createTcpSocetEx createTcpSocketEx; + NyLPC_TiNetInterface_createUdpSocetEx createUdpSocetEx; + NyLPC_TiNetInterface_createTcpListener createTcpListener; + NyLPC_TiNetInterface_start start; + NyLPC_TiNetInterface_stop stop; + NyLPC_TiNetInterface_sendArpRequest sendarprequest; + NyLPC_TiNetInterface_hasArpInfo hasarpinfo; + NyLPC_TiNetInterface_isInitService isinitservice; + NyLPC_TiNetInterface_getInterfaceInfo getinterfaceinfo; +}; + + + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* NYLPC_CTCPLISTENER_H_ */ + diff --git a/lib/src/netif/NyLPC_iTcpListener.h b/lib/src/netif/NyLPC_iTcpListener.h new file mode 100644 index 0000000..6fcfc44 --- /dev/null +++ b/lib/src/netif/NyLPC_iTcpListener.h @@ -0,0 +1,75 @@ +/********************************************************************************* + * PROJECT: MiMic + * -------------------------------------------------------------------------------- + * + * This file is part of MiMic + * Copyright (C)2011 Ryo Iizuka + * + * MiMic is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + * + * For further information please contact. + * http://nyatla.jp/ + * or + * + *********************************************************************************/ +#ifndef NYLPC_ITCPLISTENER_H_ +#define NYLPC_ITCPLISTENER_H_ + + +#include "NyLPC_stdlib.h" +#include "NyLPC_os.h" +#include "NyLPC_NetIf_ip_types.h" +#include "NyLPC_iTcpSocket.h" +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +typedef struct NyLPC_TiTcpListener NyLPC_TiTcpListener_t; + +/********************************************************************** + * Function + **********************************************************************/ + + +/** + * 制限時間付きでソケットに接続を待ちます。 + */ +typedef NyLPC_TBool (*NyLPC_TiTcpListener_listen)(NyLPC_TiTcpListener_t* i_inst,NyLPC_TiTcpSocket_t* i_sock,NyLPC_TUInt32 i_wait_msec); +typedef void (*NyLPC_TiTcpListener_finaize)(NyLPC_TiTcpListener_t* i_inst); +/********************************************************************** + * Interface + **********************************************************************/ + +/** + */ +struct NyLPC_TiTcpListener_Interface +{ + NyLPC_TiTcpListener_listen listen; + NyLPC_TiTcpListener_finaize finalize; +}; + +struct NyLPC_TiTcpListener +{ + const struct NyLPC_TiTcpListener_Interface* _interface; +}; + +#define NyLPC_iTcpListener_listen(i_inst,i_sock,i_wait_msec) ((i_inst)->_interface->listen((i_inst),(i_sock),(i_wait_msec))) +#define NyLPC_iTcpListener_finaize(i_inst) ((i_inst)->_interface->finalize((i_inst))) + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* NYLPC_ITCPLISTENER_H_ */ + diff --git a/lib/src/netif/NyLPC_iTcpSocket.h b/lib/src/netif/NyLPC_iTcpSocket.h new file mode 100644 index 0000000..e71a758 --- /dev/null +++ b/lib/src/netif/NyLPC_iTcpSocket.h @@ -0,0 +1,155 @@ +/********************************************************************************* + * PROJECT: MiMic + * -------------------------------------------------------------------------------- + * + * This file is part of MiMic + * Copyright (C)2011 Ryo Iizuka + * + * MiMic is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + * + * For further information please contact. + * http://nyatla.jp/ + * or + * + *********************************************************************************/ +#ifndef NYLPC_ITCPSOCKET_H_ +#define NYLPC_ITCPSOCKET_H_ + + +#include "NyLPC_stdlib.h" +#include "NyLPC_os.h" +#include "NyLPC_NetIf_ip_types.h" + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +typedef struct NyLPC_TiTcpSocket NyLPC_TiTcpSocket_t; + +/********************************************************************** + * Function + **********************************************************************/ + + +typedef const struct NyLPC_TIPv4Addr* (*NyLPC_TiTcpSocket_getPeerAddr)(const NyLPC_TiTcpSocket_t* i_inst); +typedef NyLPC_TUInt16 (*NyLPC_TiTcpSocket_getPeerPort)(const NyLPC_TiTcpSocket_t* i_inst); + + +typedef NyLPC_TBool (*NyLPC_TiTcpSocket_accept)(NyLPC_TiTcpSocket_t* i_inst,NyLPC_TUInt32 i_wait_in_msec); +/** + * @return + * 1 - 以上:受信に成功した。 + * 0 - タイムアウト + * -1 - ソケットがクローズしている + */ +typedef NyLPC_TInt32 (*NyLPC_TiTcpSocket_precv)(NyLPC_TiTcpSocket_t* i_inst,const void** o_buf_ptr,NyLPC_TUInt32 i_wait_msec); +typedef void (*NyLPC_TiTcpSocket_pseek)(NyLPC_TiTcpSocket_t* i_inst,NyLPC_TUInt16 i_seek); +/** + * 送信未達は保障されません。 + * エラーを検出したら、基本的にはソケットをクローズしてください。 + * @param i_wait_msec + * 送信失敗までの待ち時間を指定します。現在は、 + * RTT推定ができないため、TCPの再送を考慮して、最低でも10秒(10000)程度を指定してください。 + * @return + * 送信したバイト数を返します。エラーならば0未満の数を返します。 + * + */ +typedef NyLPC_TInt32(*NyLPC_TiTcpSocket_send)(NyLPC_TiTcpSocket_t* i_inst,const void* i_buf_ptr,NyLPC_TInt32 i_len,NyLPC_TUInt32 i_wait_in_msec); +typedef void(*NyLPC_TiTcpSocket_close)(NyLPC_TiTcpSocket_t* i_inst,NyLPC_TUInt32 i_wait_in_msec); + + +/** + * NyLPC_iTcpSocket_psendで送信するための送信バッファ準備します。 + * @param i_hint + * 送信を希望するデータサイズを指定します。 + * アロケータは出来る限り希望に沿ってメモリを返します。 + * @param o_buf_size + * 取得できたバッファサイズを返します。 + * @return + * 成功した場合、送信バッファを返します。 + * アプリケーションは、可能な限り速やかにデータを書き込んで、NyLPC_iTcpSocket_psendをコールしてください。 + * @note + * Optionフィールドを持つパケットを送信する場合は、オプションデータサイズの合計をデータサイズに指定して、payloadwriterで調整すること。 + */ +typedef void* (*NyLPC_TiTcpSocket_allocSendBuf)(NyLPC_TiTcpSocket_t* i_inst,NyLPC_TUInt16 i_hint,NyLPC_TUInt16* o_buf_size,NyLPC_TUInt32 i_wait_in_msec); + +/** + * NyLPC_iTcpSocket_allocSendBufで確保したメモリを開放します。 + */ +typedef void (*NyLPC_TiTcpSocket_releaseSendBuf)(NyLPC_TiTcpSocket_t* i_inst,void* i_buf_ptr); + +/** + * 事前にAllocしたTxパケットを送信します。 + * このAPIはゼロコピー送信をサポートするためのものです。 + * @param i_buf_ptr + * allocSendBufで取得したメモリを指定します。 + * @return + * 失敗した場合、メモリは開放されません。 + */ +typedef NyLPC_TBool (*NyLPC_TiTcpSocket_psend)(NyLPC_TiTcpSocket_t* i_inst,void* i_buf_ptr,int i_len,NyLPC_TUInt32 i_wait_in_msec); + +/** + * TCPソケットをクライアントとしてサーバへ接続します。 + */ +typedef NyLPC_TBool (*NyLPC_TiTcpSocket_connect)(NyLPC_TiTcpSocket_t* i_inst,const struct NyLPC_TIPv4Addr* i_addr,NyLPC_TUInt16 i_peer_port,NyLPC_TUInt32 i_wait_in_msec); + +typedef void (*NyLPC_TiTcpSocket_finalize)(NyLPC_TiTcpSocket_t* i_inst); + +/********************************************************************** + * Interface + **********************************************************************/ + +/** + */ +struct NyLPC_TiTcpSocket_Interface +{ + NyLPC_TiTcpSocket_getPeerAddr getpeeraddr; + NyLPC_TiTcpSocket_getPeerPort getpeerport; + NyLPC_TiTcpSocket_accept accept; + NyLPC_TiTcpSocket_precv precv; + NyLPC_TiTcpSocket_pseek pseek; + NyLPC_TiTcpSocket_send send; + NyLPC_TiTcpSocket_close close; + NyLPC_TiTcpSocket_allocSendBuf allocSendBuf; + NyLPC_TiTcpSocket_releaseSendBuf releaseSendBuf; + NyLPC_TiTcpSocket_psend psend; + NyLPC_TiTcpSocket_connect connect; + NyLPC_TiTcpSocket_finalize finalize; +}; + +struct NyLPC_TiTcpSocket +{ + const struct NyLPC_TiTcpSocket_Interface* _interface; +}; + + +#define NyLPC_iTcpSocket_getPeerAddr(i_inst) ((i_inst)->_interface->getpeeraddr((i_inst))) +#define NyLPC_iTcpSocket_getPeerPort(i_inst) ((i_inst)->_interface->getpeerport((i_inst))) +#define NyLPC_iTcpSocket_accept(i_inst,i_wait_in_msec) ((i_inst)->_interface->accept((i_inst),(i_wait_in_msec))) +#define NyLPC_iTcpSocket_precv(i_inst,o_buf_ptr,i_wait_msec) ((i_inst)->_interface->precv((i_inst),(o_buf_ptr),(i_wait_msec))) +#define NyLPC_iTcpSocket_pseek(i_inst,i_seek) ((i_inst)->_interface->pseek((i_inst),(i_seek))) +#define NyLPC_iTcpSocket_send(i_inst,i_buf_ptr,i_len,i_wait_in_msec) ((i_inst)->_interface->send((i_inst),(i_buf_ptr),(i_len),(i_wait_in_msec))) +#define NyLPC_iTcpSocket_close(i_inst,i_wait_in_msec) ((i_inst)->_interface->close((i_inst),(i_wait_in_msec))) +#define NyLPC_iTcpSocket_allocSendBuf(i_inst,i_hint,o_buf_size,i_wait_in_msec) ((i_inst)->_interface->allocSendBuf((i_inst),(i_hint),(o_buf_size),(i_wait_in_msec))) +#define NyLPC_iTcpSocket_releaseSendBuf(i_inst,i_buf_ptr) ((i_inst)->_interface->releaseSendBuf((i_inst),(i_buf_ptr))) +#define NyLPC_iTcpSocket_psend(i_inst,i_buf_ptr,i_len,i_wait_in_msec) ((i_inst)->_interface->psend((i_inst),(i_buf_ptr),(i_len),(i_wait_in_msec))) +#define NyLPC_iTcpSocket_connect(i_inst,i_addr,i_peer_port,i_wait_in_msec) ((i_inst)->_interface->connect((i_inst),(i_addr),(i_peer_port),(i_wait_in_msec))) +#define NyLPC_iTcpSocket_finalize(i_inst) ((i_inst)->_interface->finalize((i_inst))) + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* NYLPC_ITCPSOCKET_H_ */ + diff --git a/lib/src/netif/NyLPC_iUdpSocket.h b/lib/src/netif/NyLPC_iUdpSocket.h new file mode 100644 index 0000000..50148d2 --- /dev/null +++ b/lib/src/netif/NyLPC_iUdpSocket.h @@ -0,0 +1,199 @@ +/********************************************************************************* + * PROJECT: MiMic + * -------------------------------------------------------------------------------- + * + * This file is part of MiMic + * Copyright (C)2011 Ryo Iizuka + * + * MiMic is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + * + * For further information please contact. + * http://nyatla.jp/ + * or + * + *********************************************************************************/ +#ifndef NYLPC_IIUDPSOCKET_H_ +#define NYLPC_IIUDPSOCKET_H_ + + +#include "NyLPC_stdlib.h" +#include "NyLPC_os.h" +#include "NyLPC_NetIf_ip_types.h" +#include "NyLPC_iTcpSocket.h" +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +typedef struct NyLPC_TiUdpSocket NyLPC_TiUdpSocket_t; +/********************************************************************** + * Struct + **********************************************************************/ + +/** + * 受信情報を格納する構造体 + */ +struct NyLPC_TIPv4RxInfo +{ + NyLPC_TUInt16 size;//パケットサイズ + NyLPC_TUInt16 port;//受信ポート + NyLPC_TUInt16 peer_port;//PEERポート + struct NyLPC_TIPv4Addr ip;//受信IP + struct NyLPC_TIPv4Addr peer_ip;//PEERIP +}; + + +/********************************************************************** + * Event + **********************************************************************/ + +/** + * 受信時に非同期にコールされるハンドラ + * UIPサービスタスクが実行する。 + * @return + * TRUEならパケットを受信キューへ追加する。FALSEならパケットを受信キューへ追加しない。 + */ +typedef NyLPC_TBool (*NyLPC_TiUdpSocket_onRxHandler)(NyLPC_TiUdpSocket_t* i_inst,const void* i_buf,const struct NyLPC_TIPv4RxInfo* i_info); + +/** + * 非同期にコールされるハンドラ。 + * UIPサービスタスクが実行する。 + */ +typedef void (*NyLPC_TiUdpSocket_onPeriodicHandler)(NyLPC_TiUdpSocket_t* i_inst); + +/********************************************************************** + * Function + **********************************************************************/ + + + +/** + * マルチキャストアドレスに参加する。 + * @param i_addr + * 参加するマルチキャストグループを指定する。 + * 同じマルチキャスとグループに参加できるのは、システムの中で1つに限られます。 + * 0を指定した場合、マルチキャスとグループから離脱します。 + */ +typedef void (*NyLPC_TiUdpSocket_joinMulticast)(NyLPC_TiUdpSocket_t* i_inst,const struct NyLPC_TIPv4Addr* i_addr); + +/** + * ブロードキャストに参加する。 + */ +typedef void (*NyLPC_TiUdpSocket_setBroadcast)(NyLPC_TiUdpSocket_t* i_inst); + + +/** + * この関数は、ソケットの受信バッファの読み取り位置と、読み出せるデータサイズを返却します。 + * 関数はポインターを返却するだけで、バッファの読み取り位置をシークしません。 + * シークするにはNyLPC_cTcpSocket_pseekを使います。 + */ +typedef NyLPC_TInt32 (*NyLPC_TiUdpSocket_precv)(NyLPC_TiUdpSocket_t* i_inst,const void** o_buf_ptr,const struct NyLPC_TIPv4RxInfo** o_info,NyLPC_TUInt32 i_wait_msec); +/** + * 受信バッファを次のバッファまでシークします。 + */ +typedef void (*NyLPC_TiUdpSocket_pseek)(NyLPC_TiUdpSocket_t* i_inst); + +/** + * 送信バッファを割り当てて返します。 + * 割り当てたメモリは、releaseSendBuf関数か、psend関数を成功させて開放する必要があります。 + * @param i_hint + * 取得したいメモリサイズをセットします。 + * 関数は要求サイズより小さいメモリを返すことがあります。 + */ +typedef void* (*NyLPC_TiUdpSocket_allocSendBuf)(NyLPC_TiUdpSocket_t* i_inst,NyLPC_TUInt16 i_hint,NyLPC_TUInt16* o_buf_size,NyLPC_TUInt32 i_wait_in_msec); + +typedef void (*NyLPC_TiUdpSocket_releaseSendBuf)(NyLPC_TiUdpSocket_t* i_inst,void* i_buf_ptr); + +/** + * 事前にAllocしたTxパケットを送信します。 + * このAPIはゼロコピー送信をサポートするためのものです。 + * @param i_buf_ptr + * allocSendBufで取得したメモリを指定します。 + * @return + * 関数が失敗した場合、i_buf_ptrは「開放されません。」 + */ +typedef NyLPC_TBool (*NyLPC_TiUdpSocket_psend)(NyLPC_TiUdpSocket_t* i_inst,const struct NyLPC_TIPv4Addr* i_addr,NyLPC_TUInt16 i_port,void* i_buf_ptr,int i_len); + +/** + * 最大送信サイズは1200バイトです。 + */ +typedef NyLPC_TInt32 (*NyLPC_TiUdpSocket_send)(NyLPC_TiUdpSocket_t* i_inst,const struct NyLPC_TIPv4Addr* i_addr,NyLPC_TUInt16 i_port,const void* i_buf_ptr,NyLPC_TInt32 i_len,NyLPC_TUInt32 i_wait_in_msec); + +/** + * 非同期パケットハンドラを設定する。 + */ +typedef void (*NyLPC_TiUdpSocket_setOnRxHandler)(NyLPC_TiUdpSocket_t* i_inst,NyLPC_TiUdpSocket_onRxHandler i_handler); + +/** + * 非同期タイマ呼び出しハンドラを設定する。 + */ +typedef void (*NyLPC_TiUdpSocket_setOnPeriodicHandler)(NyLPC_TiUdpSocket_t* i_inst,NyLPC_TiUdpSocket_onPeriodicHandler i_handler); + + +/** + * ソケットのローカルIPのアドレスを返す。 + * 値はuipが動作中のみ有効。 + */ +typedef const struct NyLPC_TIPv4Addr* (*NyLPC_TiUdpSocket_getSockIP)(const NyLPC_TiUdpSocket_t* i_inst); + +typedef void (*NyLPC_TiUdpSocket_finalize)(NyLPC_TiUdpSocket_t* i_inst); + + +/********************************************************************** + * Interface + **********************************************************************/ + +/** + */ +struct NyLPC_TiUdpSocket_Interface +{ + NyLPC_TiUdpSocket_joinMulticast joinMulticast; + NyLPC_TiUdpSocket_setBroadcast setBroadcast; + NyLPC_TiUdpSocket_precv precv; + NyLPC_TiUdpSocket_pseek pseek; + NyLPC_TiUdpSocket_allocSendBuf allocSendBuf; + NyLPC_TiUdpSocket_releaseSendBuf releaseSendBuf; + NyLPC_TiUdpSocket_psend psend; + NyLPC_TiUdpSocket_send send; + NyLPC_TiUdpSocket_setOnRxHandler setOnRxHandler; + NyLPC_TiUdpSocket_setOnPeriodicHandler setOnPeriodicHandler; + NyLPC_TiUdpSocket_getSockIP getSockIP; + NyLPC_TiUdpSocket_finalize finalize; +}; + +struct NyLPC_TiUdpSocket +{ + const struct NyLPC_TiUdpSocket_Interface* _interface; + void* _tag; +}; + + +#define NyLPC_iUdpSocket_joinMulticast(i_inst,i_addr) ((i_inst)->_interface->joinMulticast((i_inst),(i_addr))) +#define NyLPC_iUdpSocket_setBroadcast(i_inst) ((i_inst)->_interface->setBroadcast((i_inst))) +#define NyLPC_iUdpSocket_precv(i_inst,o_buf_ptr,o_info,i_wait_msec) ((i_inst)->_interface->precv((i_inst),(o_buf_ptr),(o_info),(i_wait_msec))) +#define NyLPC_iUdpSocket_pseek(i_inst) ((i_inst)->_interface->pseek((i_inst))) +#define NyLPC_iUdpSocket_allocSendBuf(i_inst,i_hint,o_buf_size,i_wait_in_msec) ((i_inst)->_interface->allocSendBuf((i_inst),(i_hint),(o_buf_size),(i_wait_in_msec))) +#define NyLPC_iUdpSocket_releaseSendBuf(i_inst,i_buf_ptr) ((i_inst)->_interface->releaseSendBuf((i_inst),(i_buf_ptr))) +#define NyLPC_iUdpSocket_psend(i_inst,i_addr,i_port,i_buf_ptr,i_len) ((i_inst)->_interface->psend((i_inst),(i_addr),(i_port),(i_buf_ptr),(i_len))) +#define NyLPC_iUdpSocket_send(i_inst,i_addr,i_port,i_buf_ptr,i_len,i_wait_in_msec) ((i_inst)->_interface->send((i_inst),(i_addr),(i_port),(i_buf_ptr),(i_len),(i_wait_in_msec))) +#define NyLPC_iUdpSocket_setOnRxHandler(i_inst,i_handler) ((i_inst)->_interface->setOnRxHandler((i_inst),(i_handler))) +#define NyLPC_iUdpSocket_setOnPeriodicHandler(i_inst,i_handler) ((i_inst)->_interface->setOnPeriodicHandler((i_inst),(i_handler))) +#define NyLPC_iUdpSocket_getSockIP(i_inst) ((i_inst)->_interface->getSockIP((i_inst))) +#define NyLPC_iUdpSocket_finalize(i_inst) ((i_inst)->_interface->finalize((i_inst))) + + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* NYLPC_IIUDPSOCKET_H_ */ diff --git a/lib/src/netif/apipa/NyLPC_cApipa.c b/lib/src/netif/apipa/NyLPC_cApipa.c new file mode 100644 index 0000000..c366682 --- /dev/null +++ b/lib/src/netif/apipa/NyLPC_cApipa.c @@ -0,0 +1,91 @@ +#include "NyLPC_cApipa.h" +#include "../NyLPC_cNet.h" +#include +#include + + +/** + * ARPテーブルに指定IPが現れるまで待ちます。 + */ +static NyLPC_TBool waitForArpResponse(const struct NyLPC_TIPv4Addr* i_ip,NyLPC_TUInt32 i_wait_in_ms) +{ + NyLPC_TcStopwatch_t sw; + NyLPC_cStopwatch_initialize(&sw); + NyLPC_cStopwatch_startExpire(&sw,i_wait_in_ms); + while(!NyLPC_cStopwatch_isExpired(&sw)){ + NyLPC_cThread_yield(); + if(NyLPC_cNet_hasArpInfo(i_ip)){ + return NyLPC_TBool_TRUE; + } + } + NyLPC_cStopwatch_finalize(&sw); + return NyLPC_TBool_FALSE; +} +static void makeIP(NyLPC_TcApipa_t* i_inst,struct NyLPC_TIPv4Addr* i_ip) +{ +// NyLPC_TIPv4Addr_set(i_ip,192,168,128,206);//for conflict test! + NyLPC_TIPv4Addr_set(i_ip,169,254,(i_inst->_seed>>8)&0xff,i_inst->_seed & 0xff); +} +static void updateSeed(NyLPC_TcApipa_t* i_inst) +{ + do{ + i_inst->_seed=(391*i_inst->_seed+392); + }while(((i_inst->_seed & 0xff)==0) || ((i_inst->_seed & 0xff00)==0)); +} + + + +void NyLPC_cApipa_initialize(NyLPC_TcApipa_t* i_inst) +{ + i_inst->_seed=0; +} + + +/** + * この関数はuipを操作します。 + * cNetは停止中である必要があります。 + */ +NyLPC_TBool NyLPC_cApipa_requestAddr(NyLPC_TcApipa_t* i_inst,NyLPC_TcIPv4Config_t* i_cfg,NyLPC_TInt16 i_repeat) +{ + int i; + NyLPC_TcIPv4Config_t cfg; + struct NyLPC_TIPv4Addr caip; + + //ゼロコンフィギュレーション用のIPを設定 + NyLPC_cIPv4Config_initialzeCopy(&cfg,i_cfg); + //seedを更新 + for(i=0;i<6;i++) + { + i_inst->_seed+=i_cfg->eth_mac.addr[i]; + } + NyLPC_cIPv4Config_setDefaultRoute(&cfg,&NyLPC_TIPv4Addr_ZERO); + for(i=i_repeat-1;i>=0;i--){ + NyLPC_cIPv4Config_setIp(&cfg,&NyLPC_TIPv4Addr_ZERO,&NyLPC_TIPv4Addr_ZERO); + updateSeed(i_inst); + makeIP(i_inst,&caip); + //startInterface + NyLPC_cNet_start(&cfg); + NyLPC_cNet_sendArpRequest(&caip); + //テーブル更新待ち + if(waitForArpResponse(&caip,512+(i_inst->_seed % 256))){ + NyLPC_cNet_stop(); + continue; + } + NyLPC_cNet_stop(); + //IPのコンフリクトテスト + NyLPC_cIPv4Config_setIp(&cfg,&caip,&NyLPC_TIPv4Addr_APIPA_MASK); + NyLPC_cNet_start(&cfg); + //!ARP送信 + NyLPC_cNet_sendArpRequest(&caip); + if(waitForArpResponse(&caip,512+(256-(i_inst->_seed % 256)))){ + //応答があったらエラー + NyLPC_cNet_stop(); + continue; + } + //OK + NyLPC_cNet_stop(); + NyLPC_cIPv4Config_setIp(i_cfg,&cfg.ip_addr,&cfg.netmask); + return NyLPC_TBool_TRUE; + } + return NyLPC_TBool_FALSE; +} diff --git a/lib/src/net/apipa/NyLPC_cApipa.h b/lib/src/netif/apipa/NyLPC_cApipa.h similarity index 90% rename from lib/src/net/apipa/NyLPC_cApipa.h rename to lib/src/netif/apipa/NyLPC_cApipa.h index 53637c3..87f5c95 100644 --- a/lib/src/net/apipa/NyLPC_cApipa.h +++ b/lib/src/netif/apipa/NyLPC_cApipa.h @@ -1,56 +1,56 @@ -/********************************************************************************* - * PROJECT: MiMic - * -------------------------------------------------------------------------------- - * - * This file is part of MiMic - * Copyright (C)2011-2013 Ryo Iizuka - * - * MiMic is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published - * by the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program. If not, see . - * - * For further information please contact. - * http://nyatla.jp/ - * or - * - *********************************************************************************/ -#ifndef NYLPC_CAPIPA_H_ -#define NYLPC_CAPIPA_H_ - -#ifdef __cplusplus -extern "C" { -#endif /* __cplusplus */ - -#include "NyLPC_stdlib.h" -#include "NyLPC_net.h" - -typedef struct NyLPC_cApipa NyLPC_TcApipa_t; - -struct NyLPC_cApipa -{ - NyLPC_TUInt32 _seed; -}; -void NyLPC_cApipa_initialize(NyLPC_TcApipa_t* i_inst); - -#define NyLPC_cApipa_finalize(i_inst) - - -/** - * i_cfgにAutoIPのアドレスを取得します。 - * この関数はuipを操作します。uipServiceは停止中である必要があります。 - */ -NyLPC_TBool NyLPC_cApipa_requestAddr(NyLPC_TcApipa_t* i_inst,NyLPC_TcIPv4Config_t* i_cfg,NyLPC_TInt16 i_repeat); - -#ifdef __cplusplus -} -#endif /* __cplusplus */ -#endif /* NYLPC_CAPIPA_H_ */ +/********************************************************************************* + * PROJECT: MiMic + * -------------------------------------------------------------------------------- + * + * This file is part of MiMic + * Copyright (C)2011-2013 Ryo Iizuka + * + * MiMic is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + * + * For further information please contact. + * http://nyatla.jp/ + * or + * + *********************************************************************************/ +#ifndef NYLPC_CAPIPA_H_ +#define NYLPC_CAPIPA_H_ + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +#include "NyLPC_stdlib.h" +#include "../NyLPC_cIPv4Config.h" + +typedef struct NyLPC_cApipa NyLPC_TcApipa_t; + +struct NyLPC_cApipa +{ + NyLPC_TUInt32 _seed; +}; +void NyLPC_cApipa_initialize(NyLPC_TcApipa_t* i_inst); + +#define NyLPC_cApipa_finalize(i_inst) + + +/** + * i_cfgにAutoIPのアドレスを取得します。 + * この関数はuipを操作します。uipServiceは停止中である必要があります。 + */ +NyLPC_TBool NyLPC_cApipa_requestAddr(NyLPC_TcApipa_t* i_inst,NyLPC_TcIPv4Config_t* i_cfg,NyLPC_TInt16 i_repeat); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ +#endif /* NYLPC_CAPIPA_H_ */ diff --git a/lib/src/netif/dhcp/NyLPC_cDhcpClient.c b/lib/src/netif/dhcp/NyLPC_cDhcpClient.c new file mode 100644 index 0000000..ef627b4 --- /dev/null +++ b/lib/src/netif/dhcp/NyLPC_cDhcpClient.c @@ -0,0 +1,354 @@ +/********************************************************************************* + * PROJECT: MiMic + * -------------------------------------------------------------------------------- + * + * This file is part of MiMic + * Copyright (C)2011-2013 Ryo Iizuka + * + * MiMic is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + * + * For further information please contact. + * http://nyatla.jp/ + * or + * + *********************************************************************************/ +#include "NyLPC_cDhcpClient.h" +#include "../NyLPC_cNet.h" +#include +#include + +struct NyLPC_TDhcpHeader +{ + NyLPC_TUInt8 op; + NyLPC_TUInt8 htype; + NyLPC_TUInt8 hlen; + NyLPC_TUInt8 hops; + NyLPC_TUInt32 xid; + NyLPC_TUInt16 secs; + NyLPC_TUInt16 flags; + NyLPC_TUInt32 ciaddr; + NyLPC_TUInt32 yiaddr; + NyLPC_TUInt32 siaddr; + NyLPC_TUInt32 giaddr; + struct{ + struct NyLPC_TEthAddr emac; + NyLPC_TChar padding[10]; + }chaddr; + NyLPC_TChar sname[64]; + NyLPC_TChar file[128]; +}PACK_STRUCT_END; + +#define NyLPC_TDhcpHeader_BOOTREQUEST 1 +#define NyLPC_TDhcpHeader_BOOTREPLY 2 + +#define DHCP_OPT_ID_ROUTER 3 +#define DHCP_OPT_ID_SERVER_ID 54 +#define DHCP_OPT_ID_NETMASK 1 +#define DHCP_OPT_ID_MESSAGETYPE 53 + + + +/** + * DHCPパケットから32bit値を読み出す。 + * @return + * ネットワークオーダー + */ +static NyLPC_TBool getUInt32Option(const NyLPC_TUInt8* i_buf,NyLPC_TUInt16 len,NyLPC_TUInt8 i_id,NyLPC_TUInt32* o_v) +{ + const NyLPC_TUInt8* p=i_buf+sizeof(struct NyLPC_TDhcpHeader)+4; + while(*p!=0x00 && p<(i_buf+len-5)){ + if(*p==i_id){ + if(*(p+1)==4){ + *o_v=*((NyLPC_TUInt32*)(p+2)); + return NyLPC_TBool_TRUE; + } + }else{ + p+=(*(p+1))+2; + } + } + return NyLPC_TBool_FALSE; +} +static NyLPC_TBool getUInt8Option(const NyLPC_TUInt8* i_buf,NyLPC_TUInt16 len,NyLPC_TUInt8 i_id,NyLPC_TUInt8* o_v) +{ + const NyLPC_TUInt8* p=i_buf+sizeof(struct NyLPC_TDhcpHeader)+4; + while(*p!=0x00 && p<(i_buf+len-5)){ + if(*p==i_id){ + if(*(p+1)==1){ + *o_v=*(p+2); + return NyLPC_TBool_TRUE; + } + }else{ + p+=(*(p+1))+2; + } + } + return NyLPC_TBool_FALSE; +} +static NyLPC_TBool NyLPC_TDhcpHeader_parseDHCPOFFER(const NyLPC_TUInt8* i_buf,NyLPC_TUInt16 i_len,NyLPC_TUInt32 i_xid,NyLPC_TcDhcpClient_t* i_inst) +{ + struct NyLPC_TDhcpHeader* p=(struct NyLPC_TDhcpHeader*)i_buf; + //XIDのチェック + if(p->xid!=NyLPC_HTONL(i_xid)){ + return NyLPC_TBool_FALSE; + } + //OFFERのclient IPアドレスをresultへ保存情報の保存 + i_inst->_result->ip_addr.v=p->yiaddr; + //SERVER IDを保存 + if(!getUInt32Option(i_buf,i_len,DHCP_OPT_ID_SERVER_ID,&i_inst->_offerserver.v)){ + return NyLPC_TBool_FALSE; + } + return NyLPC_TBool_TRUE; +} + +static NyLPC_TBool NyLPC_TDhcpHeader_parseDHCPACK(const NyLPC_TUInt8* i_buf,NyLPC_TUInt16 i_len,NyLPC_TUInt32 i_xid,NyLPC_TcIPv4Config_t* result) +{ + struct NyLPC_TDhcpHeader* p=(struct NyLPC_TDhcpHeader*)i_buf; + //XIDのチェック + if(p->xid!=NyLPC_HTONL(i_xid)){ + return NyLPC_TBool_FALSE; + } + if(!getUInt32Option(i_buf,i_len,DHCP_OPT_ID_ROUTER,&result->dr_addr.v)){ + result->dr_addr=NyLPC_TIPv4Addr_ZERO; + } + if(!getUInt32Option(i_buf,i_len,DHCP_OPT_ID_NETMASK,&result->netmask.v)){ + result->netmask=NyLPC_TIPv4Addr_ZERO; + } + result->ip_addr.v=p->yiaddr; + return NyLPC_TBool_TRUE; +} + +static void NyLPC_TDhcpHeader_setDHCPDISCOVER(char* i_buf,NyLPC_TUInt32 i_xid,const struct NyLPC_TEthAddr* emac,NyLPC_TUInt16* o_len) +{ + struct NyLPC_TDhcpHeader* p=(struct NyLPC_TDhcpHeader*)i_buf; + memset(i_buf,0,sizeof(struct NyLPC_TDhcpHeader)); + p->op=NyLPC_TDhcpHeader_BOOTREQUEST; + p->htype=1; + p->hlen=6; + p->xid=NyLPC_HTONL(i_xid); + p->chaddr.emac=*emac; + p->flags=NyLPC_HTONS(0x8000); + memcpy(i_buf+sizeof(struct NyLPC_TDhcpHeader), + "\x63\x82\x53\x63" //4 + "\x35\x01\x01" //3 MESSAGE TYPE + "\x37\x03\x01\x03\x06" //5 REQUEST LIST(1,3,6) + "\x3d\x07\x01\x00\x00\x00\x00\x00\x00" //9 CLIENT INDIFIRE + "\xff",4+3+5+9+1); + //emacの上書き + memcpy((i_buf+sizeof(struct NyLPC_TDhcpHeader)+4+3+5+3),emac->addr,6); + //送信するパケットの長さ + *o_len=sizeof(struct NyLPC_TDhcpHeader)+4+3+5+9+1; + return; +} +static void NyLPC_TDhcpHeader_setDHCPREQUEST(char* i_buf,NyLPC_TUInt32 i_xid,const struct NyLPC_TIPv4Addr* i_sid,const struct NyLPC_TIPv4Addr* i_reqid,const struct NyLPC_TEthAddr* emac,NyLPC_TUInt16* o_len) +{ + struct NyLPC_TDhcpHeader* p=(struct NyLPC_TDhcpHeader*)i_buf; + memset(i_buf,0,sizeof(struct NyLPC_TDhcpHeader)); + p->op=NyLPC_TDhcpHeader_BOOTREQUEST; + p->htype=1; + p->hlen=6; + p->xid=NyLPC_HTONL(i_xid); + p->chaddr.emac=*emac; + p->flags=NyLPC_HTONS(0x8000); + memcpy(i_buf+sizeof(struct NyLPC_TDhcpHeader), + "\x63\x82\x53\x63" //4 + "\x35\x01\x03" //3 MESSAGE TYPE + "\x37\x03\x01\x03\x06" //5 REQUEST LIST(1,3,6) + "\x3d\x07\x01\x00\x00\x00\x00\x00\x00" //9 CLIENT INDIFIRE + "\x36\x04\x00\x00\x00\x00" // 6 SERVER ID + "\x32\x04\x00\x00\x00\x00" // 6 Reqested IP + "\xff",4+3+5+9+6+6+1); + //emacの上書き + memcpy((i_buf+sizeof(struct NyLPC_TDhcpHeader)+4+3+5+3),emac->addr,6); + //sidの上書き + memcpy((i_buf+sizeof(struct NyLPC_TDhcpHeader)+4+3+5+9+2),i_sid,4); + //reqidの上書き + memcpy((i_buf+sizeof(struct NyLPC_TDhcpHeader)+4+3+5+9+6+2),i_reqid,4); + //送信するパケットの長さ + *o_len=sizeof(struct NyLPC_TDhcpHeader)+4+3+5+9+6+6+1; + return; +} + + + +#define TcDhcpSock_ST_WAIT_OFFER 1 +#define TcDhcpSock_ST_WAIT_OFFER_OK 2 +#define TcDhcpSock_ST_WAIT_ACK 3 +#define TcDhcpSock_ST_WAIT_ACK_OK 4 +#define TcDhcpSock_ST_DONE_NG 3 +#define TcDhcpSock_ST_DONE_OK 4 + + + + +#define DHCP_OPT_ID_MESSAGETYPE_ACK 5 +#define DHCP_OPT_ID_MESSAGETYPE_OFFER 2 + +static NyLPC_TBool onPacket(NyLPC_TiUdpSocket_t* i_inst,const void* i_buf,const struct NyLPC_TIPv4RxInfo* i_info); + +/** + * DHCPソケットを作成します。 + */ +NyLPC_TBool NyLPC_cDhcpClient_initialize(NyLPC_TcDhcpClient_t* i_inst) +{ + i_inst->_socket=NyLPC_cNet_createUdpSocketEx(68,NyLPC_TSocketType_UDP_NOBUF); + if(i_inst->_socket==NULL){ + return NyLPC_TBool_FALSE; + } + i_inst->_socket->_tag=i_inst; + NyLPC_iUdpSocket_setBroadcast(i_inst->_socket); + NyLPC_iUdpSocket_setOnRxHandler(i_inst->_socket,onPacket); + return NyLPC_TBool_TRUE; +} +void NyLPC_cDhcpClient_finalize(NyLPC_TcDhcpClient_t* i_inst) +{ + NyLPC_iUdpSocket_finalize(i_inst->_socket); +} +#define TIMEOUT_SOCKAPI_MS 1000 +#define TIMEOUT_RECVMSG_MS 3000 + +/** + * ネットワークを更新します。 + * emac/default_mssを設定したネットワークが必要です。 + */ +static NyLPC_TBool NyLPC_cDhcpClient_dhcpRequest(NyLPC_TcDhcpClient_t* i_inst,NyLPC_TcIPv4Config_t* i_result) +{ + char* buf; + NyLPC_TcStopwatch_t sw; + NyLPC_TUInt16 s; + NyLPC_TInt16 hint=sizeof(struct NyLPC_TDhcpHeader)+128; + i_inst->txid+=(*(NyLPC_TUInt16*)(&(i_result->eth_mac.addr[2])))+(*(NyLPC_TUInt16*)(&(i_result->eth_mac.addr[4]))); + i_inst->_result=i_result; + buf=NyLPC_iUdpSocket_allocSendBuf(i_inst->_socket,hint,&s,TIMEOUT_SOCKAPI_MS); + if(buf==NULL || stxid,&i_inst->_result->eth_mac,&s); + i_inst->_status=TcDhcpSock_ST_WAIT_OFFER; + if(!NyLPC_iUdpSocket_psend(i_inst->_socket,&NyLPC_TIPv4Addr_BROADCAST,67,buf,s)){ + NyLPC_iUdpSocket_releaseSendBuf(i_inst->_socket,buf); + return NyLPC_TBool_FALSE; + } + NyLPC_cStopwatch_initialize(&sw); + NyLPC_cStopwatch_startExpire(&sw,TIMEOUT_RECVMSG_MS); + while(i_inst->_status==TcDhcpSock_ST_WAIT_OFFER){ + if(NyLPC_cStopwatch_isExpired(&sw)){ + return NyLPC_TBool_FALSE; + } + } + //レスポンスのチェック + if(i_inst->_status!=TcDhcpSock_ST_WAIT_OFFER_OK) + { + return NyLPC_TBool_FALSE; + } + buf=NyLPC_iUdpSocket_allocSendBuf(i_inst->_socket,hint,&s,TIMEOUT_SOCKAPI_MS); + if(buf==NULL || stxid,&(i_inst->_offerserver),&(i_inst->_result->ip_addr),&i_inst->_result->eth_mac,&s); + i_inst->_status=TcDhcpSock_ST_WAIT_ACK; + if(!NyLPC_iUdpSocket_psend(i_inst->_socket,&NyLPC_TIPv4Addr_BROADCAST,67,buf,s)){ + NyLPC_iUdpSocket_releaseSendBuf(i_inst->_socket,buf); + return NyLPC_TBool_FALSE; + } + NyLPC_cStopwatch_startExpire(&sw,TIMEOUT_RECVMSG_MS); + while(i_inst->_status==TcDhcpSock_ST_WAIT_ACK){ + if(NyLPC_cStopwatch_isExpired(&sw)){ + return NyLPC_TBool_FALSE; + } + } + //レスポンスのチェック + if(i_inst->_status!=TcDhcpSock_ST_WAIT_ACK_OK) + { + return NyLPC_TBool_FALSE; + } + return NyLPC_TBool_TRUE; +} + +/** + * NyLPC_TcIPv4Config_tをDHCPで更新します。 + * この関数をコールする時は、サービスは停止中でなければなりません。 + * @param i_cfg + * 更新するi_cfg構造体。 + * emac,default_mssは設定済である必要があります。他のフィールド値は不定で構いません。 + * 更新されるフィールドは、ip,netmast,default_rootの3つです。 + * @return + * 更新に成功した場合TRUE + */ +NyLPC_TBool NyLPC_cDhcpClient_requestAddr(NyLPC_TcDhcpClient_t* i_inst,NyLPC_TcIPv4Config_t* i_cfg,NyLPC_TInt16 i_repeat) +{ + NyLPC_TInt16 i; + NyLPC_TBool ret=NyLPC_TBool_FALSE; + NyLPC_TcIPv4Config_t c2; + //工場出荷時設定でリセットしてIPを0に + NyLPC_cIPv4Config_initialzeCopy(&c2,i_cfg); + NyLPC_cIPv4Config_setIp(&c2,&NyLPC_TIPv4Addr_ZERO,&NyLPC_TIPv4Addr_ZERO); + NyLPC_cIPv4Config_setDefaultRoute(&c2,&NyLPC_TIPv4Addr_ZERO); + //netを開始 + NyLPC_cNet_start(&c2); + for(i=i_repeat-1;i>=0;i--){ + ret=NyLPC_cDhcpClient_dhcpRequest(i_inst,i_cfg); + if(ret){ + break; + } + } + NyLPC_cNet_stop(); + NyLPC_cIPv4Config_finalize(&c2); + return ret; +} + + + +static NyLPC_TBool onPacket(NyLPC_TiUdpSocket_t* i_inst,const void* i_buf,const struct NyLPC_TIPv4RxInfo* i_info) +{ + NyLPC_TUInt8 mt;//message type + NyLPC_TcDhcpClient_t* inst=(NyLPC_TcDhcpClient_t*)i_inst->_tag; + struct NyLPC_TDhcpHeader* dnsh=(struct NyLPC_TDhcpHeader*)i_buf; + if(i_info->size_status) + { + case TcDhcpSock_ST_WAIT_ACK: + if(dnsh->op!=NyLPC_TDhcpHeader_BOOTREPLY){ + return NyLPC_TBool_FALSE; + } + if(!getUInt8Option(i_buf,i_info->size,DHCP_OPT_ID_MESSAGETYPE,&mt)){ + return NyLPC_TBool_FALSE; + } + if(mt!=DHCP_OPT_ID_MESSAGETYPE_ACK){ + return NyLPC_TBool_FALSE; + } + if(!NyLPC_TDhcpHeader_parseDHCPACK(i_buf,i_info->size,inst->txid,inst->_result)){ + return NyLPC_TBool_FALSE; + } + inst->_status=TcDhcpSock_ST_WAIT_ACK_OK; + break; + case TcDhcpSock_ST_WAIT_OFFER: + if(dnsh->op!=NyLPC_TDhcpHeader_BOOTREPLY){ + return NyLPC_TBool_FALSE; + } + if(!getUInt8Option(i_buf,i_info->size,DHCP_OPT_ID_MESSAGETYPE,&mt)){ + return NyLPC_TBool_FALSE; + } + if(mt!=DHCP_OPT_ID_MESSAGETYPE_OFFER){ + return NyLPC_TBool_FALSE; + } + if(!NyLPC_TDhcpHeader_parseDHCPOFFER(i_buf,i_info->size,inst->txid,inst)){ + return NyLPC_TBool_FALSE; + } + inst->_status=TcDhcpSock_ST_WAIT_OFFER_OK; + break; + } + return NyLPC_TBool_FALSE; + +} diff --git a/lib/src/net/dhcp/NyLPC_cDhcpClient.h b/lib/src/netif/dhcp/NyLPC_cDhcpClient.h similarity index 85% rename from lib/src/net/dhcp/NyLPC_cDhcpClient.h rename to lib/src/netif/dhcp/NyLPC_cDhcpClient.h index 1557de7..c844b82 100644 --- a/lib/src/net/dhcp/NyLPC_cDhcpClient.h +++ b/lib/src/netif/dhcp/NyLPC_cDhcpClient.h @@ -1,73 +1,74 @@ -/********************************************************************************* - * PROJECT: MiMic - * -------------------------------------------------------------------------------- - * - * This file is part of MiMic - * Copyright (C)2011-2013 Ryo Iizuka - * - * MiMic is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published - * by the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program. If not, see . - * - * For further information please contact. - * http://nyatla.jp/ - * or - * - *********************************************************************************/ -#ifndef NYLPC_CDHCPCLIENT_H_ -#define NYLPC_CDHCPCLIENT_H_ - -#ifdef __cplusplus -extern "C" { -#endif /* __cplusplus */ - -#include "NyLPC_net.h" - -typedef struct NyLPC_TcDhcpClient NyLPC_TcDhcpClient_t; - -struct NyLPC_TcDhcpClient{ - NyLPC_TcUdpSocket_t super; - NyLPC_TcIPv4Config_t* _result; - NyLPC_TUInt32 txid; - volatile NyLPC_TUInt16 _status; - //offer情報 - struct NyLPC_TIPv4Addr _offerserver; -}; - - -/** - * DHCPソケットを作成します。 - */ -NyLPC_TBool NyLPC_cDhcpClient_initialize(NyLPC_TcDhcpClient_t* i_inst); - -void NyLPC_cDhcpClient_finalize(NyLPC_TcDhcpClient_t* i_inst); - -/** - * NyLPC_TcIPv4Config_tをDHCPで更新します。 - * この関数をコールする時は、サービスは停止中でなければなりません。 - * @param i_cfg - * 更新するi_cfg構造体。 - * emac,default_mssは設定済である必要があります。他のフィールド値は不定で構いません。 - * 更新されるフィールドは、ip,netmast,default_rootの3つです。 - * @return - * 更新に成功した場合TRUE - */ -NyLPC_TBool NyLPC_cDhcpClient_requestAddr(NyLPC_TcDhcpClient_t* i_inst,NyLPC_TcIPv4Config_t* i_cfg,NyLPC_TInt16 i_repeat); - - - - -#ifdef __cplusplus -} -#endif /* __cplusplus */ - -#endif +/********************************************************************************* + * PROJECT: MiMic + * -------------------------------------------------------------------------------- + * + * This file is part of MiMic + * Copyright (C)2011-2013 Ryo Iizuka + * + * MiMic is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + * + * For further information please contact. + * http://nyatla.jp/ + * or + * + *********************************************************************************/ +#ifndef NYLPC_CDHCPCLIENT_H_ +#define NYLPC_CDHCPCLIENT_H_ + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ +#include "NyLPC_stdlib.h" +#include "../NyLPC_iUdpSocket.h" +#include "../NyLPC_cIPv4Config.h" + +typedef struct NyLPC_TcDhcpClient NyLPC_TcDhcpClient_t; + +struct NyLPC_TcDhcpClient{ + NyLPC_TiUdpSocket_t* _socket; + NyLPC_TcIPv4Config_t* _result; + NyLPC_TUInt32 txid; + volatile NyLPC_TUInt16 _status; + //offer情報 + struct NyLPC_TIPv4Addr _offerserver; +}; + + +/** + * DHCPソケットを作成します。 + */ +NyLPC_TBool NyLPC_cDhcpClient_initialize(NyLPC_TcDhcpClient_t* i_inst); + +void NyLPC_cDhcpClient_finalize(NyLPC_TcDhcpClient_t* i_inst); + +/** + * NyLPC_TcIPv4Config_tをDHCPで更新します。 + * この関数をコールする時は、サービスは停止中でなければなりません。 + * @param i_cfg + * 更新するi_cfg構造体。 + * emac,default_mssは設定済である必要があります。他のフィールド値は不定で構いません。 + * 更新されるフィールドは、ip,netmast,default_rootの3つです。 + * @return + * 更新に成功した場合TRUE + */ +NyLPC_TBool NyLPC_cDhcpClient_requestAddr(NyLPC_TcDhcpClient_t* i_inst,NyLPC_TcIPv4Config_t* i_cfg,NyLPC_TInt16 i_repeat); + + + + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif diff --git a/lib/src/netif/mimicip/NyLPC_cIPv4.c b/lib/src/netif/mimicip/NyLPC_cIPv4.c new file mode 100644 index 0000000..b4f6680 --- /dev/null +++ b/lib/src/netif/mimicip/NyLPC_cIPv4.c @@ -0,0 +1,289 @@ +/********************************************************************************* + * PROJECT: MiMic + * -------------------------------------------------------------------------------- + * + * This file is part of MiMic + * Copyright (C)2011 Ryo Iizuka + * + * MiMic is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + * + * For further information please contact. + * http://nyatla.jp/ + * or + * + * + * Parts of this file were leveraged from uIP: + * + * Copyright (c) 2001-2003, Adam Dunkels. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +#include "NyLPC_cIPv4.h" +#include "NyLPC_stdlib.h" +#include "NyLPC_os.h" +#include "NyLPC_cIPv4Payload_protected.h" +#include "NyLPC_cMiMicIpTcpSocket_protected.h" +#include "NyLPC_cMiMicIpTcpListener_protected.h" +#include "NyLPC_cMiMicIpUdpSocket_protected.h" +#include "NyLPC_cIPv4IComp_protected.h" +#include "NyLPC_cMiMicIpNetIf_protected.h" + + + + + +/**************************************************** + * NyLPC_cIPv4 + ***************************************************/ + +/** + * Static関数 + */ + +static void* tcp_rx( + NyLPC_TcIPv4_t* i_inst, + const NyLPC_TcIPv4Payload_t* i_ipp); + +static NyLPC_TBool udp_rx( + NyLPC_TcIPv4_t* i_inst, + NyLPC_TcIPv4Payload_t* i_ipp); + +/** + * See Header file. + */ +void NyLPC_cIPv4_initialize( + NyLPC_TcIPv4_t* i_inst) +{ + //IP制御パケットの為に40バイト以上のシステムTXメモリが必要。 + NyLPC_ArgAssert(NyLPC_cMiMicIpNetIf_SYS_TX_BUF_SIZE>40); + //instanceの初期化 + NyLPC_cMutex_initialize(&(i_inst->_sock_mutex)); + NyLPC_cMutex_initialize(&(i_inst->_listener_mutex)); + i_inst->tcp_port_counter=0; + i_inst->_ref_config=NULL; + return; +} + +/** + * See header file. + */ +void NyLPC_cIPv4_finalize( + NyLPC_TcIPv4_t* i_inst) +{ + NyLPC_cMutex_finalize(&(i_inst->_sock_mutex)); + NyLPC_cMutex_finalize(&(i_inst->_listener_mutex)); + return; +} + +/** + * See header file. + */ +void NyLPC_cIPv4_start( + NyLPC_TcIPv4_t* i_inst, + const NyLPC_TcIPv4Config_t* i_ref_configlation) +{ + NyLPC_ArgAssert(i_ref_configlation!=NULL); + //リストの初期化、ここでするべき?しないべき? + i_inst->_ref_config=i_ref_configlation; + //configulationのアップデートを登録されてるソケットに通知 + NyLPC_cMiMicIpNetIf_callSocketStart(i_ref_configlation); + return; +} + +/** + * See header file. + */ +void NyLPC_cIPv4_stop( + NyLPC_TcIPv4_t* i_inst) +{ + NyLPC_cMiMicIpNetIf_callSocketStop(); + i_inst->_ref_config=NULL; + return; +} + + + +#define IS_START(i_inst) ((i_inst)->_ref_config!=NULL) + +/** + * 稼動時に、1s置きに呼び出す関数です。 + */ +void NyLPC_cIPv4_periodec(NyLPC_TcIPv4_t* i_inst) +{ + NyLPC_cMiMicIpNetIf_callPeriodic(); +} + + +/** + * IPv4ペイロードを処理する関数。 + * この関数は、パケット受信タスクから実行します。 + * @param i_rx + * 先頭ポインタ。 + * @return + * TRUEなら、i_rxに応答パケットをセットして返します。 + */ +void* NyLPC_cIPv4_rx(NyLPC_TcIPv4_t* i_inst,const void* i_rx,NyLPC_TUInt16 i_rx_size) +{ + NyLPC_TcMiMicIpNetIf_t* inst=_NyLPC_TcMiMicIpNetIf_inst; + NyLPC_TcIPv4Payload_t ipv4; + //NOT開始状態なら受け付けないよ。 + if(!IS_START(i_inst)){ + NyLPC_OnErrorGoto(ERROR_DROP); + } + + NyLPC_cIPv4Payload_initialize(&ipv4); + //IPフラグメントを読出し用にセットする。 + if(!NyLPC_cIPv4Payload_attachRxBuf(&ipv4,i_rx,i_rx_size)) + { + NyLPC_OnErrorGoto(ERROR_DROP); + } + switch(ipv4.header->proto) + { + case UIP_PROTO_TCP: + //TCP受信処理 + return tcp_rx(i_inst,&ipv4); + case UIP_PROTO_UDP: + //UDP処理 + udp_rx(i_inst,&ipv4);//r + return NyLPC_TBool_FALSE; + case UIP_PROTO_ICMP: + return NyLPC_cIPv4IComp_rx(&(inst->_icomp),&ipv4); + } + return NULL; +ERROR_DROP: + return NULL; +} + +NyLPC_TUInt16 NyLPC_cIPv4_getNewPortNumber(NyLPC_TcIPv4_t* i_inst) +{ + NyLPC_TUInt16 i,n; + for(i=0;i<0x0fff;i--){ + i_inst->tcp_port_counter=(i_inst->tcp_port_counter+1)%0x0fff; + n=i_inst->tcp_port_counter+49152; + if(NyLPC_cMiMicIpNetIf_isClosedTcpPort(n)) + { + return n; + } + } + return 0; +} + + +/********************************************************************** + * + * packet handler + * + **********************************************************************/ + + +static void* tcp_rx( + NyLPC_TcIPv4_t* i_inst, + const NyLPC_TcIPv4Payload_t* i_ipp) +{ + NyLPC_TcMiMicIpTcpSocket_t* sock; + NyLPC_TcMiMicIpTcpListener_t* listener; + + //自分自身のIPに対する呼び出し? + if(!NyLPC_TIPv4Addr_isEqual(&(i_ipp->header->destipaddr),&(i_inst->_ref_config->ip_addr))) + { + //自分以外のパケットはドロップ + goto DROP; + } + //チェックサムの計算 + if((NyLPC_TIPv4Header_makeTcpChecksum(i_ipp->header) != 0xffff)) + { + //受信エラーのあるパケットはドロップ + goto DROP; + } + //アクティブなTCPソケットを探す。 + sock=NyLPC_cMiMicIpNetIf_getMatchTcpSocket(i_ipp->payload.tcp->destport,i_ipp->header->srcipaddr,i_ipp->payload.tcp->srcport); + if(sock!=NULL) + { + //既存の接続を処理 + return NyLPC_cMiMicIpTcpSocket_parseRx(sock,i_ipp); + } + + //このポートに対応したListenerを得る。 + listener=NyLPC_cMiMicIpNetIf_getListenerByPeerPort(i_ipp->payload.tcp->destport); + if(listener==NULL){ + //Listen対象ではない。RST送信 + return NyLPC_cMiMicIpTcpSocket_allocTcpReverseRstAck(i_ipp); + } + //リスナにソケットのバインドを依頼する。 + NyLPC_cMiMicIpTcpListener_synPacket(listener,i_ipp); + return NULL;//LISTEN成功。送信データなし +DROP: + return NULL; +} + + + +static NyLPC_TBool udp_rx( + NyLPC_TcIPv4_t* i_inst, + NyLPC_TcIPv4Payload_t* i_ipp) +{ + NyLPC_TcMiMicIpUdpSocket_t* sock=NULL; + if(!NyLPC_TIPv4Addr_isEqual(&(i_ipp->header->destipaddr),&(i_inst->_ref_config->ip_addr))) + { + sock=NyLPC_cMiMicIpNetIf_getMatchUdpSocket(i_ipp->payload.udp->destport); + }else{ + if(NyLPC_TIPv4Addr_isEqualWithMask(&(i_ipp->header->destipaddr),&NyLPC_TIPv4Addr_MULTICAST,&NyLPC_TIPv4Addr_MULTICAST_MASK)){ + //MultiCast? + //マルチキャストに参加している&&portの一致するソケットを検索 + sock=NyLPC_cMiMicIpNetIf_getMatchMulticastUdpSocket(&(i_ipp->header->destipaddr),i_ipp->payload.udp->destport); + }else if(!NyLPC_TIPv4Addr_isEqual(&(i_ipp->header->destipaddr),&NyLPC_TIPv4Addr_BROADCAST)){ + //Broadcast? + sock=NyLPC_cMiMicIpNetIf_getMatchUdpSocket(i_ipp->payload.udp->destport); + } + } + if(sock==NULL) + { + goto DROP; + } + //パケットのエラーチェック + if((NyLPC_TIPv4Header_makeTcpChecksum(i_ipp->header) != 0xffff)) + { + //受信エラーのあるパケットはドロップ + goto DROP; + } + //既存の接続を処理 + return NyLPC_cMiMicIpUdpSocket_parseRx(sock,i_ipp); +DROP: + return NyLPC_TBool_FALSE; +} + + diff --git a/lib/src/uip/NyLPC_cIPv4.h b/lib/src/netif/mimicip/NyLPC_cIPv4.h similarity index 73% rename from lib/src/uip/NyLPC_cIPv4.h rename to lib/src/netif/mimicip/NyLPC_cIPv4.h index f637059..15e22fc 100644 --- a/lib/src/uip/NyLPC_cIPv4.h +++ b/lib/src/netif/mimicip/NyLPC_cIPv4.h @@ -1,233 +1,203 @@ -/********************************************************************************* - * PROJECT: MiMic - * -------------------------------------------------------------------------------- - * - * This file is part of MiMic - * Copyright (C)2011 Ryo Iizuka - * - * MiMic is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published - * by the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program. If not, see . - * - * For further information please contact. - * http://nyatla.jp/ - * or - * - * - * Parts of this file were leveraged from uIP: - * - * Copyright (c) 2001-2003, Adam Dunkels. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote - * products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS - * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY - * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE - * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ -/** - * @file - * このファイルは、NyLPC_cIPv4IPv4クラスを定義します。 - */ -#ifndef NYLPC_CIPV4TCP_H_ -#define NYLPC_CIPV4TCP_H_ - - - -#include "NyLPC_uip.h" -#include "NyLPC_cTcpListener.h" -#include "NyLPC_cTcpSocket.h" -#include "NyLPC_cIPv4Config.h" - -#ifdef __cplusplus -extern "C" { -#endif /* __cplusplus */ - - - - -/********************************************************************** - * - * class NyLPC_TcIPv4 - * - **********************************************************************/ -#include "NyLPC_cIPv4_typedef.h" - -///** -// * 環境定数です。NyLPC_TcTcpListenerインスタンスリストの数を設定します。 -// * この値は、NyLPC_cTcpListenerクラスの最大生成数になります。 -// */ -//#define NyLPC_cIPv4_MAX_TCP_LISTENER 10 - -/** - * Socketの最大生成数 - * この値は、NyLPC_cTcpSocketクラス,NyLPC_cTcpListener,NyLPC_cUdpの最大生成数になります。 - */ -#define NyLPC_cIPv4_MAX_SOCKET 10 - - - -/** - * NyLPC_TcIPv4クラスの構造体です。 - */ -struct NyLPC_TcIPv4 -{ - /** 参照しているIPスタックの環境値です。この値は、start関数が設定します。*/ - const NyLPC_TcIPv4Config_t* _ref_config; - /** ソケットリソースの保護用。コールバック関数から呼び出されるソケット内部のリソース保護に使用する共通MUTEX*/ - NyLPC_TcMutex_t _sock_mutex; - /** リスナリソースの保護用。コールバック関数から呼び出されるソケット内部のリソース保護に使用する共通MUTEX*/ - NyLPC_TcMutex_t _listener_mutex; - /** NyLPC_cTcpSocketを管理するポインタリストです。*/ - NyLPC_TcPtrTbl_t _socket_tbl; - /** _socket_tblが使用するメモリ領域です。*/ - NyLPC_TcBaseSocket_t* _socket_array_buf[NyLPC_cIPv4_MAX_SOCKET]; - /** 0-0xfffまでを巡回するカウンタ*/ - NyLPC_TUInt16 tcp_port_counter; -}; - -/** - * コンストラクタです。インスタンスを初期化します。 - * @param i_inst - * 初期化するインスタンス - */ -void NyLPC_cIPv4_initialize( - NyLPC_TcIPv4_t* i_inst); - -/** - * デストラクタです。インスタンスを破棄して、確保している動的リソースを元に戻します。 - * @param i_inst - * 破棄するインスタンス - * initializeが成功したインスタンスだけが指定できます。 - */ -void NyLPC_cIPv4_finalize( - NyLPC_TcIPv4_t* i_inst); - -/** - * この関数は、インスタンスにTCP/IP処理の準備をするように伝えます。 - * @param i_inst - * 操作するインスタンス - * @param i_ref_configlation - * IPの環境値をセットしたオブジェクトを指定します。 - * この値は、stop関数を実行するまでの間、維持してください。 - */ -void NyLPC_cIPv4_start( - NyLPC_TcIPv4_t* i_inst, - const NyLPC_TcIPv4Config_t* i_ref_configlation); - -/** - * この関数はTCP/IP処理を停止することを伝えます。 - * @param i_inst - * 操作するインスタンス。 - * startで開始済みで無ければなりません。 - * @note - * 現在、接続中の接続に対する保障は未実装です。安全に使用することが出来ません。 - */ -void NyLPC_cIPv4_stop( - NyLPC_TcIPv4_t* i_inst); - -/** - * この関数は、NyLPC_TcBaseSocketオブジェクトを管理リストへ追加します。 - * @param i_inst - * 操作するインスタンス。 - * @param i_sock - * 追加するインスタンスのポインタ - * @return - * 追加が成功するとTRUEを返します。 - */ -NyLPC_TBool NyLPC_cIPv4_addSocket( - NyLPC_TcIPv4_t* i_inst, - NyLPC_TcBaseSocket_t* i_sock); - -/** - * この関数は、NyLPC_cTcpSocketオブジェクトを管理リストから除外します。 - * NyLPC_TcBaseSocketが使います。 - * @param i_inst - * 操作するインスタンス。 - * @param i_sock - * 削除するインスタンスのポインタ - * @return - * 削除が成功するとTRUEを返します。 - */ -NyLPC_TBool NyLPC_cIPv4_removeSocket( - NyLPC_TcIPv4_t* i_inst, - NyLPC_TcBaseSocket_t* i_sock); - - -/** - * この関数は、RxIPパケットを処理して、管理下のインスタンスに処理を依頼します。 - * 現在の関数は、i_rxに最大64バイトの応答パケットのイメージを格納することがあります。 - * 応答パケットは、RXに対するACKパケットです。 - * 格納の有無は戻り値を確認することで判ります。 - * この関数はstart-stopの間だけコールすることが出来ます。start,stopと非同期に実行しないでください。 - * @param i_inst - * 操作するインスタンスです。 - * @param i_rx - * RXパケットを格納したメモリアドレスです。 - * 最低でも、64バイト以上のサイズが必要です。 - * @param i_rx_size - * i_rxに格納したデータのサイズです。 - * @return - * 応答パケットを格納したメモリです。 - */ -void* NyLPC_cIPv4_rx(NyLPC_TcIPv4_t* i_inst,const void* i_rx,NyLPC_TUInt16 i_rx_size); -/** - * この関数は、定期的にインスタンスへ実行機会を与える関数です。 - * TCPの再送、無通信タイムアウトなどを処理します。 - * 約1秒おきに呼び出してください。 - * @param i_inst - * 操作するインスタンスです。 - */ -void NyLPC_cIPv4_periodec(NyLPC_TcIPv4_t* i_inst); - -/** - * ソケットリソースとコールバックの排他処理に使う共通MUTEXを返します。 - * このMutexはソケット同士の干渉が起こらない処理にだけ使ってください。 - */ -#define NyLPC_cIPv4_getSockMutex(i_inst) (&((i_inst)->_sock_mutex)) -/** - * リスナーリソースとコールバックの排他処理に使う共通MUTEXを返します。 - */ -#define NyLPC_cIPv4_getListenerMutex(i_inst) (&((i_inst)->_listener_mutex)) - -/** - * ポート0で使用するポート番号を返します。 - * @return - * 49152 - (49152+0x0ffff)番までのポートのうち、使用中でないポート番号を返します。 - * エラー時は0です。 - */ -NyLPC_TUInt16 NyLPC_cIPv4_getNewPortNumber(NyLPC_TcIPv4_t* i_inst); - - -#ifdef __cplusplus -} -#endif /* __cplusplus */ - -#endif +/********************************************************************************* + * PROJECT: MiMic + * -------------------------------------------------------------------------------- + * + * This file is part of MiMic + * Copyright (C)2011 Ryo Iizuka + * + * MiMic is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + * + * For further information please contact. + * http://nyatla.jp/ + * or + * + * + * Parts of this file were leveraged from uIP: + * + * Copyright (c) 2001-2003, Adam Dunkels. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +/** + * @file + * このファイルは、NyLPC_cIPv4IPv4クラスを定義します。 + */ +#ifndef NYLPC_CIPV4TCP_H_ +#define NYLPC_CIPV4TCP_H_ + + + +#include "NyLPC_os.h" +#include "../NyLPC_NetIf_ip_types.h" +#include "../NyLPC_cIPv4Config.h" + + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ +/********************************************************************** + * + * Closs reference + * + **********************************************************************/ + +typedef struct NyLPC_TcMiMicIpBaseSocket NyLPC_TcMiMicIpBaseSocket_t; + + +/********************************************************************** + * + * class NyLPC_TcIPv4 + * + **********************************************************************/ +typedef struct NyLPC_TcIPv4 NyLPC_TcIPv4_t; + + + + +/** + * Socketの最大生成数 + * この値は、NyLPC_cTcpSocketクラス,NyLPC_cTcpListener,NyLPC_cUdpの最大生成数になります。 + */ +#define NyLPC_cIPv4_MAX_SOCKET 10 + + + +/** + * NyLPC_TcIPv4クラスの構造体です。 + */ +struct NyLPC_TcIPv4 +{ + /** 参照しているIPスタックの環境値です。この値は、start関数が設定します。*/ + const NyLPC_TcIPv4Config_t* _ref_config; + /** ソケットリソースの保護用。コールバック関数から呼び出されるソケット内部のリソース保護に使用する共通MUTEX*/ + NyLPC_TcMutex_t _sock_mutex; + /** リスナリソースの保護用。コールバック関数から呼び出されるソケット内部のリソース保護に使用する共通MUTEX*/ + NyLPC_TcMutex_t _listener_mutex; + /** 0-0xfffまでを巡回するカウンタ*/ + NyLPC_TUInt16 tcp_port_counter; +}; + +/** + * コンストラクタです。インスタンスを初期化します。 + * @param i_inst + * 初期化するインスタンス + */ +void NyLPC_cIPv4_initialize( + NyLPC_TcIPv4_t* i_inst); + +/** + * デストラクタです。インスタンスを破棄して、確保している動的リソースを元に戻します。 + * @param i_inst + * 破棄するインスタンス + * initializeが成功したインスタンスだけが指定できます。 + */ +void NyLPC_cIPv4_finalize( + NyLPC_TcIPv4_t* i_inst); + +/** + * この関数は、インスタンスにTCP/IP処理の準備をするように伝えます。 + * @param i_inst + * 操作するインスタンス + * @param i_ref_configlation + * IPの環境値をセットしたオブジェクトを指定します。 + * この値は、stop関数を実行するまでの間、維持してください。 + */ +void NyLPC_cIPv4_start( + NyLPC_TcIPv4_t* i_inst, + const NyLPC_TcIPv4Config_t* i_ref_configlation); + +/** + * この関数はTCP/IP処理を停止することを伝えます。 + * @param i_inst + * 操作するインスタンス。 + * startで開始済みで無ければなりません。 + * @note + * 現在、接続中の接続に対する保障は未実装です。安全に使用することが出来ません。 + */ +void NyLPC_cIPv4_stop( + NyLPC_TcIPv4_t* i_inst); + +/** + * この関数は、RxIPパケットを処理して、管理下のインスタンスに処理を依頼します。 + * 現在の関数は、i_rxに最大64バイトの応答パケットのイメージを格納することがあります。 + * 応答パケットは、RXに対するACKパケットです。 + * 格納の有無は戻り値を確認することで判ります。 + * この関数はstart-stopの間だけコールすることが出来ます。start,stopと非同期に実行しないでください。 + * @param i_inst + * 操作するインスタンスです。 + * @param i_rx + * RXパケットを格納したメモリアドレスです。 + * 最低でも、64バイト以上のサイズが必要です。 + * @param i_rx_size + * i_rxに格納したデータのサイズです。 + * @return + * 応答パケットを格納したメモリです。 + */ +void* NyLPC_cIPv4_rx(NyLPC_TcIPv4_t* i_inst,const void* i_rx,NyLPC_TUInt16 i_rx_size); +/** + * この関数は、定期的にインスタンスへ実行機会を与える関数です。 + * TCPの再送、無通信タイムアウトなどを処理します。 + * 約1秒おきに呼び出してください。 + * @param i_inst + * 操作するインスタンスです。 + */ +void NyLPC_cIPv4_periodec(NyLPC_TcIPv4_t* i_inst); + +/** + * ソケットリソースとコールバックの排他処理に使う共通MUTEXを返します。 + * このMutexはソケット同士の干渉が起こらない処理にだけ使ってください。 + */ +#define NyLPC_cIPv4_getSockMutex(i_inst) (&((i_inst)->_sock_mutex)) +/** + * リスナーリソースとコールバックの排他処理に使う共通MUTEXを返します。 + */ +#define NyLPC_cIPv4_getListenerMutex(i_inst) (&((i_inst)->_listener_mutex)) + +/** + * ポート0で使用するポート番号を返します。 + * @return + * 49152 - (49152+0x0ffff)番までのポートのうち、使用中でないポート番号を返します。 + * エラー時は0です。 + */ +NyLPC_TUInt16 NyLPC_cIPv4_getNewPortNumber(NyLPC_TcIPv4_t* i_inst); + + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif diff --git a/lib/src/uip/NyLPC_cIPv4Arp.c b/lib/src/netif/mimicip/NyLPC_cIPv4Arp.c similarity index 57% rename from lib/src/uip/NyLPC_cIPv4Arp.c rename to lib/src/netif/mimicip/NyLPC_cIPv4Arp.c index 7f3d127..d7969db 100644 --- a/lib/src/uip/NyLPC_cIPv4Arp.c +++ b/lib/src/netif/mimicip/NyLPC_cIPv4Arp.c @@ -1,334 +1,335 @@ -/********************************************************************************* - * PROJECT: MiMic - * -------------------------------------------------------------------------------- - * - * This file is part of MiMic - * Copyright (C)2011 Ryo Iizuka - * - * MiMic is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published - * by the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program. If not, see . - * - * For further information please contact. - * http://nyatla.jp/ - * or - * - * - * Parts of this file were leveraged from uIP: - * - * Copyright (c) 2001-2003, Adam Dunkels. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote - * products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS - * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY - * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE - * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ -#include "NyLPC_cIPv4Arp.h" -#include "NyLPC_uip.h" -#include "NyLPC_cUipService_protected.h" -#include - - -/** - * The maxium age of ARP table entries measured in 10ths of seconds. - * - * An UIP_ARP_MAXAGE of 120 corresponds to 20 minutes (BSD - * default). - */ -#define UIP_ARP_MAXAGE 120 - - -//static const struct NyLPC_TEthAddr broadcast_ethaddr = { { 0xff, 0xff, 0xff,0xff, 0xff, 0xff } }; -//static const struct NyLPC_TIPv4Addr broadcast_ipaddr = { 0xfffffff }; - - - - -static void uip_arp_update(NyLPC_TcIPv4Arp_t* i_inst,const struct NyLPC_TIPv4Addr* ipaddr,const struct NyLPC_TEthAddr *ethaddr); -/*-----------------------------------------------------------------------------------*/ -/** - * Initialize the ARP module. - * - */ -/*-----------------------------------------------------------------------------------*/ -void NyLPC_cIPv4Arp_initialize(NyLPC_TcIPv4Arp_t* i_inst,const NyLPC_TcIPv4Config_t* i_ref_config) -{ - int i; - struct NyLPC_TArpTableItem* tbl=i_inst->arp_table; - i_inst->_cfg = i_ref_config; - i_inst->arptime = 0; - i_inst->tmpage = 0; - for (i = 0; i < NyLPC_TcIPv4Arp_ARPTAB_SIZE; ++i) { - memset(&(tbl[i].ipaddr), 0, sizeof(struct NyLPC_TIPv4Addr)); - } -} -/*-----------------------------------------------------------------------------------*/ -/** - * Periodic ARP processing function. - * - * This function performs periodic timer processing in the ARP module - * and should be called at regular intervals. The recommended interval - * is 10 seconds between the calls. - * - */ -/*-----------------------------------------------------------------------------------*/ -void NyLPC_cIPv4Arp_periodic(NyLPC_TcIPv4Arp_t* i_inst) -{ - struct NyLPC_TArpTableItem* tbl=i_inst->arp_table; - struct NyLPC_TArpTableItem* tabptr; - int i; - i_inst->arptime++; - for (i = 0; i < NyLPC_TcIPv4Arp_ARPTAB_SIZE; ++i) { - tabptr = &tbl[i]; - if (tabptr->ipaddr.v != 0 && i_inst->arptime - tabptr->time >= UIP_ARP_MAXAGE) - { - tabptr->ipaddr.v = 0; - } - } - -} -/*-----------------------------------------------------------------------------------*/ -/** - * ARP processing for incoming IP packets - * - * This function should be called by the device driver when an IP - * packet has been received. The function will check if the address is - * in the ARP cache, and if so the ARP cache entry will be - * refreshed. If no ARP cache entry was found, a new one is created. - * - * This function expects an IP packet with a prepended Ethernet header - * in the uip_buf[] buffer, and the length of the packet in the global - * variable uip_len. - */ -/*-----------------------------------------------------------------------------------*/ -void NyLPC_cIPv4Arp_incomingIp(NyLPC_TcIPv4Arp_t* i_inst,const struct NyLPC_TEthernetIIHeader* i_eth,struct NyLPC_TIPv4Addr i_ip_src) -{ - //EtherとIPv4の値を読みだす。 - /* Only insert/update an entry if the source IP address of the - incoming IP packet comes from a host on the local network. */ - if ((i_ip_src.v & i_inst->_cfg->netmask.v) != (i_inst->_cfg->ip_addr.v & i_inst->_cfg->netmask.v)) { - return; - } - uip_arp_update(i_inst,&(i_ip_src), &(i_eth->src)); - return; -} -/** - * ARP processing for incoming ARP packets. - * - * This function should be called by the device driver when an ARP - * packet has been received. The function will act differently - * depending on the ARP packet type: if it is a reply for a request - * that we previously sent out, the ARP cache will be filled in with - * the values from the ARP reply. If the incoming ARP packet is an ARP - * request for our IP address, an ARP reply packet is created and put - * into the uip_buf[] buffer. - * - * When the function returns, the value of the global variable uip_len - * indicates whether the device driver should send out a packet or - * not. If uip_len is zero, no packet should be sent. If uip_len is - * non-zero, it contains the length of the outbound packet that is - * present in the uip_buf[] buffer. - * - * This function expects an ARP packet with a prepended Ethernet - * header in the uip_buf[] buffer, and the length of the packet in the - * global variable uip_len. - */ - - -/** - * ARPパケットの読出し用構造体 - */ -struct TArpPacketPtr -{ - struct NyLPC_TEthernetIIHeader header; - struct NyLPC_TArpHeader arp; -}PACK_STRUCT_END; - -/** - * arpパケットを処理します。 - */ -void* NyLPC_cIPv4Arp_rx(NyLPC_TcIPv4Arp_t* i_inst,const struct NyLPC_TArpHeader* i_arp, NyLPC_TUInt16 i_len, NyLPC_TUInt16* o_tx_len) -{ - struct NyLPC_TArpHeader* arp_tx; - if (i_len < sizeof(struct NyLPC_TArpHeader)) { - return NULL; - } - const NyLPC_TcIPv4Config_t* cfg=i_inst->_cfg; - switch (i_arp->opcode) { - case NyLPC_HTONS(ARP_REQUEST): - /* ARP request. If it asked for our address, we send out a reply. */ - if (NyLPC_TIPv4Addr_isEqual(&(i_arp->dipaddr), &(cfg->ip_addr))) { - /* First, we register the one who made the request in our ARP - table, since it is likely that we will do more communication - with this host in the future. */ - uip_arp_update(i_inst,&(i_arp->sipaddr), &i_arp->shwaddr); - //イーサネットヘッダもいじくるから - arp_tx=(struct NyLPC_TArpHeader*)NyLPC_cUipService_allocSysTxBuf(); - - /* The reply opcode is 2. */ - arp_tx->hwtype =i_arp->hwtype; - arp_tx->protocol =i_arp->protocol; - arp_tx->hwlen =i_arp->hwlen; - arp_tx->protolen =i_arp->protolen; - arp_tx->opcode = NyLPC_HTONS(2); - memcpy(arp_tx->dhwaddr.addr, i_arp->shwaddr.addr, 6); - memcpy(arp_tx->shwaddr.addr, cfg->eth_mac.addr, 6); - arp_tx->dipaddr = i_arp->sipaddr; - arp_tx->sipaddr = cfg->ip_addr; - *o_tx_len=NyLPC_TEthernetIIHeader_setArpTx((((struct NyLPC_TEthernetIIHeader*)arp_tx)-1),&(i_inst->_cfg->eth_mac)); - -// /* The reply opcode is 2. */ -// i_arp->opcode = NyLPC_HTONS(2); -// -// memcpy(i_arp->dhwaddr.addr, i_arp->shwaddr.addr, 6); -// memcpy(i_arp->shwaddr.addr, cfg->eth_mac.addr, 6); -// -// i_arp->dipaddr = i_arp->sipaddr; -// i_arp->sipaddr = cfg->ip_addr; - return arp_tx; - } - break; - case NyLPC_HTONS(ARP_REPLY): - // ARP reply. We insert or update the ARP table if it was meant for us. - if (NyLPC_TIPv4Addr_isEqual(&(i_arp->dipaddr),&(cfg->ip_addr))) { - uip_arp_update(i_inst,&(i_arp->sipaddr), &i_arp->shwaddr); - } - break; - } - return NULL; -} -/** - * Prepend Ethernet header to an outbound IP packet and see if we need - * to send out an ARP request. - * - * This function should be called before sending out an IP packet. The - * function checks the destination IP address of the IP packet to see - * what Ethernet MAC address that should be used as a destination MAC - * address on the Ethernet. - * - * If the destination IP address is in the local network (determined - * by logical ANDing of netmask and our IP address), the function - * checks the ARP cache to see if an entry for the destination IP - * address is found. If so, an Ethernet header is prepended and the - * function returns. If no ARP cache entry is found for the - * destination IP address, the packet in the uip_buf[] is replaced by - * an ARP request packet for the IP address. The IP packet is dropped - * and it is assumed that they higher level protocols (e.g., TCP) - * eventually will retransmit the dropped packet. - * - * If the destination IP address is not on the local network, the IP - * address of the default router is used instead. - * - * When the function returns, a packet is present in the uip_buf[] - * buffer, and the length of the packet is in the global variable - * uip_len. - */ - -/** - * IPアドレス-MACアドレス交換 - */ -const struct NyLPC_TEthAddr* NyLPC_cIPv4Arp_IPv4toEthAddr(NyLPC_TcIPv4Arp_t* i_inst,const struct NyLPC_TIPv4Addr i_ip_addr) -{ - int i; - struct NyLPC_TArpTableItem *tabptr; - //ARPテーブルから検索 - for (i = NyLPC_TcIPv4Arp_ARPTAB_SIZE - 1; i >= 0; i--) { - tabptr = &i_inst->arp_table[i]; - if (NyLPC_TIPv4Addr_isEqual(&i_ip_addr,&(tabptr->ipaddr))) { - return &tabptr->ethaddr; - } - } - return NULL; -} - - - - - - - -static void uip_arp_update(NyLPC_TcIPv4Arp_t* i_inst,const struct NyLPC_TIPv4Addr* ipaddr,const struct NyLPC_TEthAddr *ethaddr) -{ - register struct NyLPC_TArpTableItem *tabptr; - int i,c; - /* Walk through the ARP mapping table and try to find an entry to - update. If none is found, the IP -> MAC address mapping is - inserted in the ARP table. */ - for (i = 0; i < NyLPC_TcIPv4Arp_ARPTAB_SIZE; ++i) { - tabptr = &i_inst->arp_table[i]; - /* Only check those entries that are actually in use. */ - if (tabptr->ipaddr.v != 0) { - /* Check if the source IP address of the incoming packet matches - the IP address in this ARP table entry. */ - if (ipaddr->v == tabptr->ipaddr.v) { - /* An old entry found, update this and return. */ - memcpy(tabptr->ethaddr.addr, ethaddr->addr, 6); - tabptr->time = i_inst->arptime; - - return; - } - } - } - - /* If we get here, no existing ARP table entry was found, so we - create one. */ - /* First, we try to find an unused entry in the ARP table. */ - for (i = 0; i < NyLPC_TcIPv4Arp_ARPTAB_SIZE; ++i) { - tabptr = &i_inst->arp_table[i]; - if (tabptr->ipaddr.v == 0) { - break; - } - } - - /* If no unused entry is found, we try to find the oldest entry and - throw it away. */ - if (i == NyLPC_TcIPv4Arp_ARPTAB_SIZE) { - i_inst->tmpage = 0; - c = 0; - for (i = 0; i < NyLPC_TcIPv4Arp_ARPTAB_SIZE; ++i) { - tabptr = &i_inst->arp_table[i]; - if (i_inst->arptime - tabptr->time > i_inst->tmpage) { - i_inst->tmpage = i_inst->arptime - tabptr->time; - c = i; - } - } - i = c; - tabptr = &i_inst->arp_table[i]; - } - - /* Now, i is the ARP table entry which we will fill with the new information. */ - tabptr->ipaddr = *ipaddr; - memcpy(tabptr->ethaddr.addr, ethaddr->addr, 6); - tabptr->time = i_inst->arptime; -} - - +/********************************************************************************* + * PROJECT: MiMic + * -------------------------------------------------------------------------------- + * + * This file is part of MiMic + * Copyright (C)2011 Ryo Iizuka + * + * MiMic is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + * + * For further information please contact. + * http://nyatla.jp/ + * or + * + * + * Parts of this file were leveraged from uIP: + * + * Copyright (c) 2001-2003, Adam Dunkels. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +#include "NyLPC_cIPv4Arp.h" +#include "../NyLPC_NetIf_ip_types.h" +#include "NyLPC_cMiMicIpNetIf_protected.h" +#include + + +/** + * The maxium age of ARP table entries measured in 10ths of seconds. + * + * An UIP_ARP_MAXAGE of 120 corresponds to 20 minutes (BSD + * default). + */ +#define UIP_ARP_MAXAGE 120 + + +//static const struct NyLPC_TEthAddr broadcast_ethaddr = { { 0xff, 0xff, 0xff,0xff, 0xff, 0xff } }; +//static const struct NyLPC_TIPv4Addr broadcast_ipaddr = { 0xfffffff }; + + + + +static void uip_arp_update(NyLPC_TcIPv4Arp_t* i_inst,const struct NyLPC_TIPv4Addr* ipaddr,const struct NyLPC_TEthAddr *ethaddr); +/*-----------------------------------------------------------------------------------*/ +/** + * Initialize the ARP module. + * + */ +/*-----------------------------------------------------------------------------------*/ +void NyLPC_cIPv4Arp_initialize(NyLPC_TcIPv4Arp_t* i_inst,const NyLPC_TcIPv4Config_t* i_ref_config) +{ + int i; + struct NyLPC_TArpTableItem* tbl=i_inst->arp_table; + i_inst->_cfg = i_ref_config; + i_inst->arptime = 0; + i_inst->tmpage = 0; + for (i = 0; i < NyLPC_TcIPv4Arp_ARPTAB_SIZE; ++i) { + memset(&(tbl[i].ipaddr), 0, sizeof(struct NyLPC_TIPv4Addr)); + } +} +/*-----------------------------------------------------------------------------------*/ +/** + * Periodic ARP processing function. + * + * This function performs periodic timer processing in the ARP module + * and should be called at regular intervals. The recommended interval + * is 10 seconds between the calls. + * + */ +/*-----------------------------------------------------------------------------------*/ +void NyLPC_cIPv4Arp_periodic(NyLPC_TcIPv4Arp_t* i_inst) +{ + struct NyLPC_TArpTableItem* tbl=i_inst->arp_table; + struct NyLPC_TArpTableItem* tabptr; + int i; + i_inst->arptime++; + for (i = 0; i < NyLPC_TcIPv4Arp_ARPTAB_SIZE; ++i) { + tabptr = &tbl[i]; + if (tabptr->ipaddr.v != 0 && i_inst->arptime - tabptr->time >= UIP_ARP_MAXAGE) + { + tabptr->ipaddr.v = 0; + } + } + +} +/*-----------------------------------------------------------------------------------*/ +/** + * ARP processing for incoming IP packets + * + * This function should be called by the device driver when an IP + * packet has been received. The function will check if the address is + * in the ARP cache, and if so the ARP cache entry will be + * refreshed. If no ARP cache entry was found, a new one is created. + * + * This function expects an IP packet with a prepended Ethernet header + * in the uip_buf[] buffer, and the length of the packet in the global + * variable uip_len. + */ +/*-----------------------------------------------------------------------------------*/ +void NyLPC_cIPv4Arp_incomingIp(NyLPC_TcIPv4Arp_t* i_inst,const struct NyLPC_TEthernetIIHeader* i_eth,struct NyLPC_TIPv4Addr i_ip_src) +{ + //EtherとIPv4の値を読みだす。 + /* Only insert/update an entry if the source IP address of the + incoming IP packet comes from a host on the local network. */ + if ((i_ip_src.v & i_inst->_cfg->netmask.v) != (i_inst->_cfg->ip_addr.v & i_inst->_cfg->netmask.v)) { + return; + } + uip_arp_update(i_inst,&(i_ip_src), &(i_eth->src)); + return; +} +/** + * ARP processing for incoming ARP packets. + * + * This function should be called by the device driver when an ARP + * packet has been received. The function will act differently + * depending on the ARP packet type: if it is a reply for a request + * that we previously sent out, the ARP cache will be filled in with + * the values from the ARP reply. If the incoming ARP packet is an ARP + * request for our IP address, an ARP reply packet is created and put + * into the uip_buf[] buffer. + * + * When the function returns, the value of the global variable uip_len + * indicates whether the device driver should send out a packet or + * not. If uip_len is zero, no packet should be sent. If uip_len is + * non-zero, it contains the length of the outbound packet that is + * present in the uip_buf[] buffer. + * + * This function expects an ARP packet with a prepended Ethernet + * header in the uip_buf[] buffer, and the length of the packet in the + * global variable uip_len. + */ + + +/** + * ARPパケットの読出し用構造体 + */ +struct TArpPacketPtr +{ + struct NyLPC_TEthernetIIHeader header; + struct NyLPC_TArpHeader arp; +}PACK_STRUCT_END; + +/** + * arpパケットを処理します。 + */ +void* NyLPC_cIPv4Arp_rx(NyLPC_TcIPv4Arp_t* i_inst,const struct NyLPC_TArpHeader* i_arp, NyLPC_TUInt16 i_len, NyLPC_TUInt16* o_tx_len) +{ + struct NyLPC_TArpHeader* arp_tx; + if (i_len < sizeof(struct NyLPC_TArpHeader)) { + return NULL; + } + const NyLPC_TcIPv4Config_t* cfg=i_inst->_cfg; + switch (i_arp->opcode) { + case NyLPC_HTONS(ARP_REQUEST): + /* ARP request. If it asked for our address, we send out a reply. */ + if (NyLPC_TIPv4Addr_isEqual(&(i_arp->dipaddr), &(cfg->ip_addr))) { + /* First, we register the one who made the request in our ARP + table, since it is likely that we will do more communication + with this host in the future. */ + uip_arp_update(i_inst,&(i_arp->sipaddr), &i_arp->shwaddr); + //イーサネットヘッダもいじくるから + arp_tx=(struct NyLPC_TArpHeader*)NyLPC_cMiMicIpNetIf_allocSysTxBuf(); + + /* The reply opcode is 2. */ + arp_tx->hwtype =i_arp->hwtype; + arp_tx->protocol =i_arp->protocol; + arp_tx->hwlen =i_arp->hwlen; + arp_tx->protolen =i_arp->protolen; + arp_tx->opcode = NyLPC_HTONS(2); + memcpy(arp_tx->dhwaddr.addr, i_arp->shwaddr.addr, 6); + memcpy(arp_tx->shwaddr.addr, cfg->eth_mac.addr, 6); + arp_tx->dipaddr = i_arp->sipaddr; + arp_tx->sipaddr = cfg->ip_addr; + *o_tx_len=NyLPC_TEthernetIIHeader_setArpTx((((struct NyLPC_TEthernetIIHeader*)arp_tx)-1),&(i_inst->_cfg->eth_mac)); + +// /* The reply opcode is 2. */ +// i_arp->opcode = NyLPC_HTONS(2); +// +// memcpy(i_arp->dhwaddr.addr, i_arp->shwaddr.addr, 6); +// memcpy(i_arp->shwaddr.addr, cfg->eth_mac.addr, 6); +// +// i_arp->dipaddr = i_arp->sipaddr; +// i_arp->sipaddr = cfg->ip_addr; + return arp_tx; + } + break; + case NyLPC_HTONS(ARP_REPLY): + // ARP reply. We insert or update the ARP table if it was meant for us. + if (NyLPC_TIPv4Addr_isEqual(&(i_arp->dipaddr),&(cfg->ip_addr))) { + uip_arp_update(i_inst,&(i_arp->sipaddr), &i_arp->shwaddr); + } + break; + } + return NULL; +} +/** + * Prepend Ethernet header to an outbound IP packet and see if we need + * to send out an ARP request. + * + * This function should be called before sending out an IP packet. The + * function checks the destination IP address of the IP packet to see + * what Ethernet MAC address that should be used as a destination MAC + * address on the Ethernet. + * + * If the destination IP address is in the local network (determined + * by logical ANDing of netmask and our IP address), the function + * checks the ARP cache to see if an entry for the destination IP + * address is found. If so, an Ethernet header is prepended and the + * function returns. If no ARP cache entry is found for the + * destination IP address, the packet in the uip_buf[] is replaced by + * an ARP request packet for the IP address. The IP packet is dropped + * and it is assumed that they higher level protocols (e.g., TCP) + * eventually will retransmit the dropped packet. + * + * If the destination IP address is not on the local network, the IP + * address of the default router is used instead. + * + * When the function returns, a packet is present in the uip_buf[] + * buffer, and the length of the packet is in the global variable + * uip_len. + */ + +/** + * IPアドレス-MACアドレス交換 + */ +const struct NyLPC_TEthAddr* NyLPC_cIPv4Arp_IPv4toEthAddr(NyLPC_TcIPv4Arp_t* i_inst,const struct NyLPC_TIPv4Addr i_ip_addr) +{ + int i; + struct NyLPC_TArpTableItem *tabptr; + //ARPテーブルから検索 + for (i = NyLPC_TcIPv4Arp_ARPTAB_SIZE - 1; i >= 0; i--) { + tabptr = &i_inst->arp_table[i]; + if (NyLPC_TIPv4Addr_isEqual(&i_ip_addr,&(tabptr->ipaddr))) { + return &tabptr->ethaddr; + } + } + return NULL; +} + + + + + + + +static void uip_arp_update(NyLPC_TcIPv4Arp_t* i_inst,const struct NyLPC_TIPv4Addr* ipaddr,const struct NyLPC_TEthAddr *ethaddr) +{ + register struct NyLPC_TArpTableItem *tabptr; + int i,c; + /* Walk through the ARP mapping table and try to find an entry to + update. If none is found, the IP -> MAC address mapping is + inserted in the ARP table. */ + for (i = 0; i < NyLPC_TcIPv4Arp_ARPTAB_SIZE; ++i) { + tabptr = &i_inst->arp_table[i]; + /* Only check those entries that are actually in use. */ + if (tabptr->ipaddr.v != 0) { + /* Check if the source IP address of the incoming packet matches + the IP address in this ARP table entry. */ + if (ipaddr->v == tabptr->ipaddr.v) { + /* An old entry found, update this and return. */ + memcpy(tabptr->ethaddr.addr, ethaddr->addr, 6); + tabptr->time = i_inst->arptime; + + return; + } + } + } + + /* If we get here, no existing ARP table entry was found, so we + create one. */ + /* First, we try to find an unused entry in the ARP table. */ + for (i = 0; i < NyLPC_TcIPv4Arp_ARPTAB_SIZE; ++i) { + tabptr = &i_inst->arp_table[i]; + if (tabptr->ipaddr.v == 0) { + break; + } + } + + /* If no unused entry is found, we try to find the oldest entry and + throw it away. */ + if (i == NyLPC_TcIPv4Arp_ARPTAB_SIZE) { + i_inst->tmpage = 0; + c = 0; + for (i = 0; i < NyLPC_TcIPv4Arp_ARPTAB_SIZE; ++i) { + tabptr = &i_inst->arp_table[i]; + if (i_inst->arptime - tabptr->time > i_inst->tmpage) { + i_inst->tmpage = i_inst->arptime - tabptr->time; + c = i; + } + } + i = c; + tabptr = &i_inst->arp_table[i]; + } + + /* Now, i is the ARP table entry which we will fill with the new information. */ + tabptr->ipaddr = *ipaddr; + memcpy(tabptr->ethaddr.addr, ethaddr->addr, 6); + tabptr->time = i_inst->arptime; +} + + + diff --git a/lib/src/uip/NyLPC_cIPv4Arp.h b/lib/src/netif/mimicip/NyLPC_cIPv4Arp.h similarity index 89% rename from lib/src/uip/NyLPC_cIPv4Arp.h rename to lib/src/netif/mimicip/NyLPC_cIPv4Arp.h index aaa85fe..82048da 100644 --- a/lib/src/uip/NyLPC_cIPv4Arp.h +++ b/lib/src/netif/mimicip/NyLPC_cIPv4Arp.h @@ -1,115 +1,116 @@ -/********************************************************************************* - * PROJECT: MiMic - * -------------------------------------------------------------------------------- - * - * This file is part of MiMic - * Copyright (C)2011 Ryo Iizuka - * - * MiMic is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published - * by the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program. If not, see . - * - * For further information please contact. - * http://nyatla.jp/ - * or - * - * - * Parts of this file were leveraged from uIP: - * - * Copyright (c) 2001-2003, Adam Dunkels. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote - * products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS - * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY - * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE - * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ -#ifndef NYLPC_CIPV4ARP_H_ -#define NYLPC_CIPV4ARP_H_ -#include "NyLPC_cIPv4Config.h" - -#ifdef __cplusplus -extern "C" { -#endif /* __cplusplus */ - - -struct NyLPC_TArpTableItem -{ - struct NyLPC_TIPv4Addr ipaddr; - struct NyLPC_TEthAddr ethaddr; - NyLPC_TUInt8 time; -}; - -/********************************************************************** - * - * class NyLPC_TcIPv4Config - * - **********************************************************************/ - - -#define NyLPC_TcIPv4Arp_ARPTAB_SIZE 8 - - -typedef struct NyLPC_TcIPv4Arp NyLPC_TcIPv4Arp_t; - -/** - * NyLPC_TcIPv4クラスの構造体です。 - */ -struct NyLPC_TcIPv4Arp -{ - const NyLPC_TcIPv4Config_t* _cfg; - NyLPC_TUInt8 arptime; - NyLPC_TUInt8 tmpage; - struct NyLPC_TArpTableItem arp_table[NyLPC_TcIPv4Arp_ARPTAB_SIZE]; -}; - -void NyLPC_cIPv4Arp_initialize(NyLPC_TcIPv4Arp_t* i_inst,const NyLPC_TcIPv4Config_t* i_ref_config); -#define NyLPC_cIPv4Arp_finalize(i_inst) -void NyLPC_cIPv4Arp_periodic(NyLPC_TcIPv4Arp_t* i_inst); -void NyLPC_cIPv4Arp_incomingIp(NyLPC_TcIPv4Arp_t* i_inst,const struct NyLPC_TEthernetIIHeader* i_eth,struct NyLPC_TIPv4Addr i_ip_src); -/** - * ARPメッセージを処理します。 - * @param o_tx_len - * 戻り値がある場合そのサイズ - * @return - * 応答パケットを格納したcUipService_allocBufで確保したメモリ - */ -void* NyLPC_cIPv4Arp_rx(NyLPC_TcIPv4Arp_t* i_inst,const struct NyLPC_TArpHeader* i_arp, NyLPC_TUInt16 i_len, NyLPC_TUInt16* o_tx_len); -const struct NyLPC_TEthAddr* NyLPC_cIPv4Arp_IPv4toEthAddr(NyLPC_TcIPv4Arp_t* i_inst,const struct NyLPC_TIPv4Addr i_ip_addr); - - -#ifdef __cplusplus -} -#endif /* __cplusplus */ - - - -#endif /* NYLPC_CIPV4ARP_H_ */ +/********************************************************************************* + * PROJECT: MiMic + * -------------------------------------------------------------------------------- + * + * This file is part of MiMic + * Copyright (C)2011 Ryo Iizuka + * + * MiMic is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + * + * For further information please contact. + * http://nyatla.jp/ + * or + * + * + * Parts of this file were leveraged from uIP: + * + * Copyright (c) 2001-2003, Adam Dunkels. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +#ifndef NYLPC_CIPV4ARP_H_ +#define NYLPC_CIPV4ARP_H_ +#include "../NyLPC_cIPv4Config.h" + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + + +struct NyLPC_TArpTableItem +{ + struct NyLPC_TIPv4Addr ipaddr; + struct NyLPC_TEthAddr ethaddr; + NyLPC_TUInt8 time; +}; + +/********************************************************************** + * + * class NyLPC_TcIPv4Config + * + **********************************************************************/ + + +#define NyLPC_TcIPv4Arp_ARPTAB_SIZE 8 + + +typedef struct NyLPC_TcIPv4Arp NyLPC_TcIPv4Arp_t; + +/** + * NyLPC_TcIPv4クラスの構造体です。 + */ +struct NyLPC_TcIPv4Arp +{ + const NyLPC_TcIPv4Config_t* _cfg; + NyLPC_TUInt8 arptime; + NyLPC_TUInt8 tmpage; + struct NyLPC_TArpTableItem arp_table[NyLPC_TcIPv4Arp_ARPTAB_SIZE]; +}; + +void NyLPC_cIPv4Arp_initialize(NyLPC_TcIPv4Arp_t* i_inst,const NyLPC_TcIPv4Config_t* i_ref_config); +#define NyLPC_cIPv4Arp_finalize(i_inst) +void NyLPC_cIPv4Arp_periodic(NyLPC_TcIPv4Arp_t* i_inst); +void NyLPC_cIPv4Arp_incomingIp(NyLPC_TcIPv4Arp_t* i_inst,const struct NyLPC_TEthernetIIHeader* i_eth,struct NyLPC_TIPv4Addr i_ip_src); +/** + * ARPメッセージを処理します。 + * @param o_tx_len + * 戻り値がある場合そのサイズ + * @return + * 応答パケットを格納したcUipService_allocBufで確保したメモリ + */ +void* NyLPC_cIPv4Arp_rx(NyLPC_TcIPv4Arp_t* i_inst,const struct NyLPC_TArpHeader* i_arp, NyLPC_TUInt16 i_len, NyLPC_TUInt16* o_tx_len); +const struct NyLPC_TEthAddr* NyLPC_cIPv4Arp_IPv4toEthAddr(NyLPC_TcIPv4Arp_t* i_inst,const struct NyLPC_TIPv4Addr i_ip_addr); + + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + + + +#endif /* NYLPC_CIPV4ARP_H_ */ + diff --git a/lib/src/uip/NyLPC_cIPv4IComp.c b/lib/src/netif/mimicip/NyLPC_cIPv4IComp.c similarity index 50% rename from lib/src/uip/NyLPC_cIPv4IComp.c rename to lib/src/netif/mimicip/NyLPC_cIPv4IComp.c index 3fd567c..a22585f 100644 --- a/lib/src/uip/NyLPC_cIPv4IComp.c +++ b/lib/src/netif/mimicip/NyLPC_cIPv4IComp.c @@ -1,148 +1,149 @@ -/********************************************************************************* - * PROJECT: MiMic - * -------------------------------------------------------------------------------- - * - * This file is part of MiMic - * Copyright (C)2011 Ryo Iizuka - * - * MiMic is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published - * by the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program. If not, see . - * - * For further information please contact. - * http://nyatla.jp/ - * or - * - * - * Parts of this file were leveraged from uIP: - * - * Copyright (c) 2001-2003, Adam Dunkels. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote - * products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS - * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY - * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE - * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ -#include "NyLPC_cIPv4IComp_protected.h" -#include "NyLPC_cUipService_protected.h" -#define ICMP_ECHO_REPLY 0 -#define ICMP_ECHO 8 - - - -NyLPC_TBool NyLPC_cIPv4IComp_initialize( - NyLPC_TcIPv4IComp_t* i_inst, - const NyLPC_TcIPv4Config_t* i_ref_config) -{ - i_inst->_ref_config=i_ref_config; - return NyLPC_TBool_TRUE; -} -void NyLPC_cIPv4IComp_finalize( - NyLPC_TcIPv4IComp_t* i_inst) -{ - return; -} -/** - * ヘッダブロックの後ろにあるIPペイロードのアドレスを返します。 - */ -#define NyLPC_TIPv4Header_getHeaderSize(i) (((i)->vhl & 0x0f)*4) - -void* NyLPC_cIPv4IComp_rx( - const NyLPC_TcIPv4IComp_t* i_inst, - const NyLPC_TcIPv4Payload_t* i_ipp) -{ - NyLPC_TUInt16 tx_size; - struct NyLPC_TIPv4Header* tx; - struct NyLPC_TIcmpHeader* payload; - const struct NyLPC_TIPv4Addr* my_ip=&(i_inst->_ref_config->ip_addr); - - if (NyLPC_TIPv4Addr_isEqual(&(i_inst->_ref_config->ip_addr),&NyLPC_TIPv4Addr_ZERO)) - { - /* If we are configured to use ping IP address configuration and - hasn't been assigned an IP address yet, we accept all ICMP - packets. */ - } else { - /* Check if the packet is destined for our IP address. */ - if (!NyLPC_TIPv4Addr_isEqual(&(i_ipp->header->destipaddr),my_ip)) - { - return NyLPC_TBool_FALSE; - } - } - // ICMP echo (i.e., ping) processing. This is simple, we only change the ICMP type from ECHO to ECHO_REPLY and adjust the ICMP checksum before we return the packet. - if (i_ipp->payload.icmp->type != ICMP_ECHO) - { - return NyLPC_TBool_FALSE; - } - //返送パケットの取得 - tx=(struct NyLPC_TIPv4Header*)NyLPC_cUipService_allocTxBuf(NyLPC_NTOHS(i_ipp->header->len16),&tx_size); - if(tx==NULL){ - return NyLPC_TBool_FALSE; - } - //パケットサイズのチェック - if(tx_sizeheader->len16)){ - NyLPC_cUipService_releaseTxBuf(tx); - return NyLPC_TBool_FALSE; - } - //返送パケットの構築 - ; - - //複製 - memcpy(tx,i_ipp->header,NyLPC_NTOHS(i_ipp->header->len16)); - - //ペイロードの編集 - payload=(struct NyLPC_TIcmpHeader*)(((NyLPC_TUInt8*)tx)+NyLPC_TIPv4Header_getHeaderSize(tx)); - payload->type = ICMP_ECHO_REPLY; - //update checksum - if (payload->icmpchksum >= NyLPC_HTONS(0xffff - (ICMP_ECHO << 8))) { - payload->icmpchksum += NyLPC_HTONS(ICMP_ECHO << 8) + 1; - } else { - payload->icmpchksum += NyLPC_HTONS(ICMP_ECHO << 8); - } - //IPヘッダの編集 - tx->destipaddr=tx->srcipaddr; - tx->srcipaddr=*my_ip; -/* - - //チェックサムの再計算 - i_ipp->payload.icmp->type = ICMP_ECHO_REPLY; - if (i_ipp->payload.icmp->icmpchksum >= NyLPC_HTONS(0xffff - (ICMP_ECHO << 8))) { - i_ipp->payload.icmp->icmpchksum += NyLPC_HTONS(ICMP_ECHO << 8) + 1; - } else { - i_ipp->payload.icmp->icmpchksum += NyLPC_HTONS(ICMP_ECHO << 8); - } - //OUT/INアドレスの反転 - i_ipp->header->destipaddr=i_ipp->header->srcipaddr; - i_ipp->header->srcipaddr=*my_ip; -*/ - return tx; - -} +/********************************************************************************* + * PROJECT: MiMic + * -------------------------------------------------------------------------------- + * + * This file is part of MiMic + * Copyright (C)2011 Ryo Iizuka + * + * MiMic is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + * + * For further information please contact. + * http://nyatla.jp/ + * or + * + * + * Parts of this file were leveraged from uIP: + * + * Copyright (c) 2001-2003, Adam Dunkels. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +#include "NyLPC_cIPv4IComp_protected.h" +#include "NyLPC_cMiMicIpNetIf_protected.h" +#define ICMP_ECHO_REPLY 0 +#define ICMP_ECHO 8 + + + +NyLPC_TBool NyLPC_cIPv4IComp_initialize( + NyLPC_TcIPv4IComp_t* i_inst, + const NyLPC_TcIPv4Config_t* i_ref_config) +{ + i_inst->_ref_config=i_ref_config; + return NyLPC_TBool_TRUE; +} +void NyLPC_cIPv4IComp_finalize( + NyLPC_TcIPv4IComp_t* i_inst) +{ + return; +} +/** + * ヘッダブロックの後ろにあるIPペイロードのアドレスを返します。 + */ +#define NyLPC_TIPv4Header_getHeaderSize(i) (((i)->vhl & 0x0f)*4) + +void* NyLPC_cIPv4IComp_rx( + const NyLPC_TcIPv4IComp_t* i_inst, + const NyLPC_TcIPv4Payload_t* i_ipp) +{ + NyLPC_TUInt16 tx_size; + struct NyLPC_TIPv4Header* tx; + struct NyLPC_TIcmpHeader* payload; + const struct NyLPC_TIPv4Addr* my_ip=&(i_inst->_ref_config->ip_addr); + + if (NyLPC_TIPv4Addr_isEqual(&(i_inst->_ref_config->ip_addr),&NyLPC_TIPv4Addr_ZERO)) + { + /* If we are configured to use ping IP address configuration and + hasn't been assigned an IP address yet, we accept all ICMP + packets. */ + } else { + /* Check if the packet is destined for our IP address. */ + if (!NyLPC_TIPv4Addr_isEqual(&(i_ipp->header->destipaddr),my_ip)) + { + return NyLPC_TBool_FALSE; + } + } + // ICMP echo (i.e., ping) processing. This is simple, we only change the ICMP type from ECHO to ECHO_REPLY and adjust the ICMP checksum before we return the packet. + if (i_ipp->payload.icmp->type != ICMP_ECHO) + { + return NyLPC_TBool_FALSE; + } + //返送パケットの取得 + tx=(struct NyLPC_TIPv4Header*)NyLPC_cMiMicIpNetIf_allocTxBuf(NyLPC_NTOHS(i_ipp->header->len16),&tx_size); + if(tx==NULL){ + return NyLPC_TBool_FALSE; + } + //パケットサイズのチェック + if(tx_sizeheader->len16)){ + NyLPC_cMiMicIpNetIf_releaseTxBuf(tx); + return NyLPC_TBool_FALSE; + } + //返送パケットの構築 + ; + + //複製 + memcpy(tx,i_ipp->header,NyLPC_NTOHS(i_ipp->header->len16)); + + //ペイロードの編集 + payload=(struct NyLPC_TIcmpHeader*)(((NyLPC_TUInt8*)tx)+NyLPC_TIPv4Header_getHeaderSize(tx)); + payload->type = ICMP_ECHO_REPLY; + //update checksum + if (payload->icmpchksum >= NyLPC_HTONS(0xffff - (ICMP_ECHO << 8))) { + payload->icmpchksum += NyLPC_HTONS(ICMP_ECHO << 8) + 1; + } else { + payload->icmpchksum += NyLPC_HTONS(ICMP_ECHO << 8); + } + //IPヘッダの編集 + tx->destipaddr=tx->srcipaddr; + tx->srcipaddr=*my_ip; +/* + + //チェックサムの再計算 + i_ipp->payload.icmp->type = ICMP_ECHO_REPLY; + if (i_ipp->payload.icmp->icmpchksum >= NyLPC_HTONS(0xffff - (ICMP_ECHO << 8))) { + i_ipp->payload.icmp->icmpchksum += NyLPC_HTONS(ICMP_ECHO << 8) + 1; + } else { + i_ipp->payload.icmp->icmpchksum += NyLPC_HTONS(ICMP_ECHO << 8); + } + //OUT/INアドレスの反転 + i_ipp->header->destipaddr=i_ipp->header->srcipaddr; + i_ipp->header->srcipaddr=*my_ip; +*/ + return tx; + +} + diff --git a/lib/src/uip/NyLPC_cIPv4IComp.h b/lib/src/netif/mimicip/NyLPC_cIPv4IComp.h similarity index 92% rename from lib/src/uip/NyLPC_cIPv4IComp.h rename to lib/src/netif/mimicip/NyLPC_cIPv4IComp.h index 5d7ae38..b1659f6 100644 --- a/lib/src/uip/NyLPC_cIPv4IComp.h +++ b/lib/src/netif/mimicip/NyLPC_cIPv4IComp.h @@ -1,72 +1,73 @@ -/********************************************************************************* - * PROJECT: MiMic - * -------------------------------------------------------------------------------- - * - * This file is part of MiMic - * Copyright (C)2011 Ryo Iizuka - * - * MiMic is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published - * by the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program. If not, see . - * - * For further information please contact. - * http://nyatla.jp/ - * or - * - * - * Parts of this file were leveraged from uIP: - * - * Copyright (c) 2001-2003, Adam Dunkels. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote - * products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS - * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY - * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE - * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ -#ifndef NYLPC_CIPV4ICOMP_H_ -#define NYLPC_CIPV4ICOMP_H_ -#include "NyLPC_cIPv4Config.h" -#ifdef __cplusplus -extern "C" { -#endif /* __cplusplus */ - -typedef struct NyLPC_TcIPv4IComp NyLPC_TcIPv4IComp_t; -struct NyLPC_TcIPv4IComp -{ - const NyLPC_TcIPv4Config_t* _ref_config; -}; - -#ifdef __cplusplus -} -#endif /* __cplusplus */ - -#endif /* NYLPC_CIPV4ICOMP_H_ */ +/********************************************************************************* + * PROJECT: MiMic + * -------------------------------------------------------------------------------- + * + * This file is part of MiMic + * Copyright (C)2011 Ryo Iizuka + * + * MiMic is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + * + * For further information please contact. + * http://nyatla.jp/ + * or + * + * + * Parts of this file were leveraged from uIP: + * + * Copyright (c) 2001-2003, Adam Dunkels. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +#ifndef NYLPC_CIPV4ICOMP_H_ +#define NYLPC_CIPV4ICOMP_H_ +#include "../NyLPC_cIPv4Config.h" +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +typedef struct NyLPC_TcIPv4IComp NyLPC_TcIPv4IComp_t; +struct NyLPC_TcIPv4IComp +{ + const NyLPC_TcIPv4Config_t* _ref_config; +}; + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* NYLPC_CIPV4ICOMP_H_ */ + diff --git a/lib/src/uip/NyLPC_cIPv4IComp_protected.h b/lib/src/netif/mimicip/NyLPC_cIPv4IComp_protected.h similarity index 89% rename from lib/src/uip/NyLPC_cIPv4IComp_protected.h rename to lib/src/netif/mimicip/NyLPC_cIPv4IComp_protected.h index 6c09e5e..901b4cb 100644 --- a/lib/src/uip/NyLPC_cIPv4IComp_protected.h +++ b/lib/src/netif/mimicip/NyLPC_cIPv4IComp_protected.h @@ -1,87 +1,88 @@ -/********************************************************************************* - * PROJECT: MiMic - * -------------------------------------------------------------------------------- - * - * This file is part of MiMic - * Copyright (C)2011 Ryo Iizuka - * - * MiMic is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published - * by the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program. If not, see . - * - * For further information please contact. - * http://nyatla.jp/ - * or - * - * - * Parts of this file were leveraged from uIP: - * - * Copyright (c) 2001-2003, Adam Dunkels. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote - * products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS - * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY - * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE - * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ -#ifndef NYLPC_NyLPC_cIPv4IComp_protected_H_ -#define NYLPC_NyLPC_cIPv4IComp_protected_H_ - -#include "NyLPC_uip.h" -#include "NyLPC_cIPv4Payload_protected.h" -#include "NyLPC_cIPv4IComp.h" - -#ifdef __cplusplus -extern "C" { -#endif /* __cplusplus */ - -NyLPC_TBool NyLPC_cIPv4IComp_initialize( - NyLPC_TcIPv4IComp_t* i_inst, - const NyLPC_TcIPv4Config_t* i_ref_config); - -void NyLPC_cIPv4IComp_finalize( - NyLPC_TcIPv4IComp_t* i_inst); - -/** - * この関数は、ICOMP IPv4パケットを処理します。 - * @return - * イーサネットパケットを格納したメモリを返します。 +/********************************************************************************* + * PROJECT: MiMic + * -------------------------------------------------------------------------------- + * + * This file is part of MiMic + * Copyright (C)2011 Ryo Iizuka + * + * MiMic is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + * + * For further information please contact. + * http://nyatla.jp/ + * or + * + * + * Parts of this file were leveraged from uIP: + * + * Copyright (c) 2001-2003, Adam Dunkels. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +#ifndef NYLPC_NyLPC_cIPv4IComp_protected_H_ +#define NYLPC_NyLPC_cIPv4IComp_protected_H_ + +#include "NyLPC_cIPv4Payload_protected.h" +#include "../NyLPC_NetIf_ip_types.h" +#include "NyLPC_cIPv4IComp.h" + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +NyLPC_TBool NyLPC_cIPv4IComp_initialize( + NyLPC_TcIPv4IComp_t* i_inst, + const NyLPC_TcIPv4Config_t* i_ref_config); + +void NyLPC_cIPv4IComp_finalize( + NyLPC_TcIPv4IComp_t* i_inst); + +/** + * この関数は、ICOMP IPv4パケットを処理します。 + * @return + * イーサネットパケットを格納したメモリを返します。 * このメモリはNyLPC_cUipService_allocTxBufで取得されたメモリです。 - */ -void* NyLPC_cIPv4IComp_rx( - const NyLPC_TcIPv4IComp_t* i_inst, - const NyLPC_TcIPv4Payload_t* i_ipp); - -#ifdef __cplusplus -} -#endif /* __cplusplus */ - -#endif + */ +void* NyLPC_cIPv4IComp_rx( + const NyLPC_TcIPv4IComp_t* i_inst, + const NyLPC_TcIPv4Payload_t* i_ipp); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif + diff --git a/lib/src/uip/NyLPC_cIPv4Payload.c b/lib/src/netif/mimicip/NyLPC_cIPv4Payload.c similarity index 72% rename from lib/src/uip/NyLPC_cIPv4Payload.c rename to lib/src/netif/mimicip/NyLPC_cIPv4Payload.c index 03245a9..4f2bde3 100644 --- a/lib/src/uip/NyLPC_cIPv4Payload.c +++ b/lib/src/netif/mimicip/NyLPC_cIPv4Payload.c @@ -1,144 +1,144 @@ -/********************************************************************************* - * PROJECT: MiMic - * -------------------------------------------------------------------------------- - * - * This file is part of MiMic - * Copyright (C)2011 Ryo Iizuka - * - * MiMic is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published - * by the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program. If not, see . - * - * For further information please contact. - * http://nyatla.jp/ - * or - * - * - * Parts of this file were leveraged from uIP: - * - * Copyright (c) 2001-2003, Adam Dunkels. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote - * products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS - * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY - * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE - * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ -#include "NyLPC_uip.h" -#include "NyLPC_cIPv4Payload_protected.h" - - - - - -/********************************************************************************* +/********************************************************************************* + * PROJECT: MiMic + * -------------------------------------------------------------------------------- + * + * This file is part of MiMic + * Copyright (C)2011 Ryo Iizuka + * + * MiMic is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + * + * For further information please contact. + * http://nyatla.jp/ + * or + * + * + * Parts of this file were leveraged from uIP: + * + * Copyright (c) 2001-2003, Adam Dunkels. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +#include "NyLPC_cIPv4Payload_protected.h" + + + + + +/********************************************************************************* * public 関数 - *********************************************************************************/ - - - - -void NyLPC_cIPv4Payload_initialize(NyLPC_TcIPv4Payload_t* i_inst) -{ - return; -} - -/** - * IPパケットを格納したバッファをセットして、ペイロードのポインタを返します。 - * 失敗時はFALSE - */ -NyLPC_TBool NyLPC_cIPv4Payload_attachRxBuf(NyLPC_TcIPv4Payload_t* i_inst,const void* i_buf,NyLPC_TUInt16 i_flagment_size) -{ - i_inst->header=(const struct NyLPC_TIPv4Header*)i_buf; - i_inst->payload.rawbuf=(const NyLPC_TUInt8*)i_buf+(i_inst->header->vhl & 0x0f)*4; - //IPパケットのバージョンチェック - if((i_inst->header->vhl & 0xf0)!=0x40){ - NyLPC_OnErrorGoto(Error); - } - //IPフレームサイズの調整 - if(NyLPC_ntohs(i_inst->header->len16)>i_flagment_size){ - NyLPC_OnErrorGoto(Error); - } - //フラグメントは許可しない。 - if ((NyLPC_ntohs(i_inst->header->ipoffset) & 0x3fff) != 0){ - NyLPC_OnErrorGoto(Error); - } - //IPv4ヘッダのチェックサムを確認 - if(!NyLPC_TIPv4Header_isCorrectIpCheckSum(i_inst->header)) - { - NyLPC_OnErrorGoto(Error); - } - return NyLPC_TBool_TRUE; -Error: - return NyLPC_TBool_FALSE; -} - -/* - * TcpIpのRxバッファをセットします。 - * @todo いらない? - */ -/* -void* NyLPC_cIPv4Payload_setTcpRxBuf(NyLPC_TcIPv4Payload_t* i_inst,void* i_buf,NyLPC_TUInt16 i_flagment_size) -{ - if(!NyLPC_cIPv4Payload_setRxBuf(i_inst,i_buf,i_flagment_size)){ - return NULL; - } - i_inst->header=(struct NyLPC_TIPv4Header*)i_buf; - return i_inst->payload.rawbuf+(i_inst->payload.tcp->tcpoffset>>4)*4; -} -*/ - - -const void* NyLPC_cIPv4Payload_detachBuf(NyLPC_TcIPv4Payload_t* i_inst) -{ - const void* r=i_inst->header; - NyLPC_ArgAssert(r!=NULL); - i_inst->header=NULL; - return r; -} -/* なんだっけっこれ? -//1の補数v1にv2を加算する。 -static NyLPC_TUInt16 add16c(NyLPC_TUInt16 i_v1,NyLPC_TUInt16 i_v2) -{ - NyLPC_TUInt16 t; - t=i_v1+i_v2; - return (t>i_v1)?t:t+1; -} -//1の補数v1から、v2を減算する。 -static NyLPC_TUInt16 sub16c(NyLPC_TUInt16 i_v1,NyLPC_TUInt16 i_v2) -{ - NyLPC_TUInt16 t; - t=i_v1-i_v2; - return (theader=(const struct NyLPC_TIPv4Header*)i_buf; + i_inst->payload.rawbuf=(const NyLPC_TUInt8*)i_buf+(i_inst->header->vhl & 0x0f)*4; + //IPパケットのバージョンチェック + if((i_inst->header->vhl & 0xf0)!=0x40){ + NyLPC_OnErrorGoto(Error); + } + //IPフレームサイズの調整 + if(NyLPC_ntohs(i_inst->header->len16)>i_flagment_size){ + NyLPC_OnErrorGoto(Error); + } + //フラグメントは許可しない。 + if ((NyLPC_ntohs(i_inst->header->ipoffset) & 0x3fff) != 0){ + NyLPC_OnErrorGoto(Error); + } + //IPv4ヘッダのチェックサムを確認 + if(!NyLPC_TIPv4Header_isCorrectIpCheckSum(i_inst->header)) + { + NyLPC_OnErrorGoto(Error); + } + return NyLPC_TBool_TRUE; +Error: + return NyLPC_TBool_FALSE; +} + +/* + * TcpIpのRxバッファをセットします。 + * @todo いらない? + */ +/* +void* NyLPC_cIPv4Payload_setTcpRxBuf(NyLPC_TcIPv4Payload_t* i_inst,void* i_buf,NyLPC_TUInt16 i_flagment_size) +{ + if(!NyLPC_cIPv4Payload_setRxBuf(i_inst,i_buf,i_flagment_size)){ + return NULL; + } + i_inst->header=(struct NyLPC_TIPv4Header*)i_buf; + return i_inst->payload.rawbuf+(i_inst->payload.tcp->tcpoffset>>4)*4; +} +*/ + + +const void* NyLPC_cIPv4Payload_detachBuf(NyLPC_TcIPv4Payload_t* i_inst) +{ + const void* r=i_inst->header; + NyLPC_ArgAssert(r!=NULL); + i_inst->header=NULL; + return r; +} +/* なんだっけっこれ? +//1の補数v1にv2を加算する。 +static NyLPC_TUInt16 add16c(NyLPC_TUInt16 i_v1,NyLPC_TUInt16 i_v2) +{ + NyLPC_TUInt16 t; + t=i_v1+i_v2; + return (t>i_v1)?t:t+1; +} +//1の補数v1から、v2を減算する。 +static NyLPC_TUInt16 sub16c(NyLPC_TUInt16 i_v1,NyLPC_TUInt16 i_v2) +{ + NyLPC_TUInt16 t; + t=i_v1-i_v2; + return (t. - * - * For further information please contact. - * http://nyatla.jp/ - * or - * - * - * Parts of this file were leveraged from uIP: - * - * Copyright (c) 2001-2003, Adam Dunkels. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote - * products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS - * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY - * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE - * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ -#ifndef NyLPC_cIPv4Payload_h -#define NyLPC_cIPv4Payload_h -typedef struct NyLPC_TcIPv4Payload NyLPC_TcIPv4Payload_t; - -#include "NyLPC_uip.h" - - -/********************************************************************** - * - * NyLPC_TcIPv4Payload - * - **********************************************************************/ -#ifdef __cplusplus -extern "C" { -#endif /* __cplusplus */ - -struct NyLPC_TcIPv4Payload -{ - //IPヘッダへのポインタです。バッファの先頭アドレスと同一です。 - const struct NyLPC_TIPv4Header* header; - union{ - //IPヘッダ終了位置のポインタです。 - const NyLPC_TUInt8* rawbuf; - const struct NyLPC_TIcmpHeader* icmp; - const struct NyLPC_TTcpHeader* tcp; - const struct NyLPC_TUdpHeader* udp; - }payload; -// NyLPC_TUInt16 buf_len; -}; - - - - - -#ifdef __cplusplus -} -#endif /* __cplusplus */ - -#endif +/********************************************************************************* + * PROJECT: MiMic + * -------------------------------------------------------------------------------- + * + * This file is part of MiMic + * Copyright (C)2011 Ryo Iizuka + * + * MiMic is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + * + * For further information please contact. + * http://nyatla.jp/ + * or + * + * + * Parts of this file were leveraged from uIP: + * + * Copyright (c) 2001-2003, Adam Dunkels. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "../NyLPC_NetIf_ip_types.h" + +#ifndef NyLPC_cIPv4Payload_h +#define NyLPC_cIPv4Payload_h +typedef struct NyLPC_TcIPv4Payload NyLPC_TcIPv4Payload_t; + + + +/********************************************************************** + * + * NyLPC_TcIPv4Payload + * + **********************************************************************/ +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +struct NyLPC_TcIPv4Payload +{ + //IPヘッダへのポインタです。バッファの先頭アドレスと同一です。 + const struct NyLPC_TIPv4Header* header; + union{ + //IPヘッダ終了位置のポインタです。 + const NyLPC_TUInt8* rawbuf; + const struct NyLPC_TIcmpHeader* icmp; + const struct NyLPC_TTcpHeader* tcp; + const struct NyLPC_TUdpHeader* udp; + }payload; +// NyLPC_TUInt16 buf_len; +}; + + + + + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif + diff --git a/lib/src/uip/NyLPC_cIPv4Payload_protected.h b/lib/src/netif/mimicip/NyLPC_cIPv4Payload_protected.h similarity index 89% rename from lib/src/uip/NyLPC_cIPv4Payload_protected.h rename to lib/src/netif/mimicip/NyLPC_cIPv4Payload_protected.h index 12cebda..d4b01c9 100644 --- a/lib/src/uip/NyLPC_cIPv4Payload_protected.h +++ b/lib/src/netif/mimicip/NyLPC_cIPv4Payload_protected.h @@ -1,123 +1,122 @@ -/********************************************************************************* - * PROJECT: MiMic - * -------------------------------------------------------------------------------- - * - * This file is part of MiMic - * Copyright (C)2011 Ryo Iizuka - * - * MiMic is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published - * by the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program. If not, see . - * - * For further information please contact. - * http://nyatla.jp/ - * or - * - * - * Parts of this file were leveraged from uIP: - * - * Copyright (c) 2001-2003, Adam Dunkels. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote - * products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS - * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY - * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE - * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ -#ifndef NYLPC_CIPV4PAYLOAD_PROTECTED_H_ -#define NYLPC_CIPV4PAYLOAD_PROTECTED_H_ - -#include "NyLPC_cIPv4Payload.h" -#include "NyLPC_cIPv4.h" -#include "NyLPC_cTcpSocket.h" -#include "NyLPC_cUdpSocket.h" - -#ifdef __cplusplus -extern "C" { -#endif /* __cplusplus */ - -#define TCP_FIN 0x01 -#define TCP_SYN 0x02 -#define TCP_RST 0x04 -#define TCP_PSH 0x08 -#define TCP_ACK 0x10 -#define TCP_URG 0x20 -#define TCP_CTL 0x3f -#define UIP_TCPH_LEN 20 /* Size of TCP header */ -#define UIP_IPH_LEN 20 /* Size of IP header */ - - - -void NyLPC_cIPv4Payload_initialize(NyLPC_TcIPv4Payload_t* i_inst); -#define NyLPC_cIPv4Payload_finalize(i) - -/** - * アタッチされているバッファを返します。 - */ -#define NyLPC_cIPv4Payload_getBuf(i) ((i)->header) - - - -void NyLPC_cIPv4Payload_attachTxBuf(NyLPC_TcIPv4Payload_t* i_inst,void* i_buf); -const void* NyLPC_cIPv4Payload_detachBuf(NyLPC_TcIPv4Payload_t* i_inst); - -NyLPC_TBool NyLPC_cIPv4Payload_attachRxBuf(NyLPC_TcIPv4Payload_t* i_inst,const void* i_buf,NyLPC_TUInt16 i_flagment_size); -void NyLPC_cIPv4Payload_setTcpReverseRstAck( - NyLPC_TcIPv4Payload_t* i_inst); -void NyLPC_cIPv4Payload_setTcpReverseRstAck2( - NyLPC_TcIPv4Payload_t* i_inst, - const NyLPC_TcIPv4Payload_t* i_src); - - - - -/** - * UDPの送信バッファを初期化します。 - */ -void* NyLPC_cIPv4Payload_initUdpTx(NyLPC_TcIPv4Payload_t* i_inst,NyLPC_TUInt8 i_iph_word,NyLPC_TUInt16 i_tcp_payload_size); -/** - * UDPの送信情報を設定します。 - */ -void NyLPC_cIPv4Payload_setUdpTxHeaderByConnection(NyLPC_TcIPv4Payload_t* i_inst,const struct uip_udp_conn* i_conn,const struct NyLPC_TIPv4Addr* i_dest_ip,NyLPC_TUInt16 i_dest_port); - -void NyLPC_cIPv4Payload_closeUdpTxPacket( - NyLPC_TcIPv4Payload_t* i_inst); - - -void NyLPC_TcIPv4TxPacket_initialize_icomp(NyLPC_TcIPv4Payload_t* i_inst,void* i_buf,NyLPC_TUInt8 i_iph_word,NyLPC_TUInt16 i_tcp_payload_size); - - - -#ifdef __cplusplus -} -#endif /* __cplusplus */ - -#endif /* NYLPC_CIPV4PAYLOAD_PROTECTED_H_ */ +/********************************************************************************* + * PROJECT: MiMic + * -------------------------------------------------------------------------------- + * + * This file is part of MiMic + * Copyright (C)2011 Ryo Iizuka + * + * MiMic is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + * + * For further information please contact. + * http://nyatla.jp/ + * or + * + * + * Parts of this file were leveraged from uIP: + * + * Copyright (c) 2001-2003, Adam Dunkels. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +#ifndef NYLPC_CIPV4PAYLOAD_PROTECTED_H_ +#define NYLPC_CIPV4PAYLOAD_PROTECTED_H_ + +#include "NyLPC_cIPv4Payload.h" +#include "NyLPC_cIPv4.h" +#include "NyLPC_cMiMicIpUdpSocket.h" + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +#define TCP_FIN 0x01 +#define TCP_SYN 0x02 +#define TCP_RST 0x04 +#define TCP_PSH 0x08 +#define TCP_ACK 0x10 +#define TCP_URG 0x20 +#define TCP_CTL 0x3f + + + + +void NyLPC_cIPv4Payload_initialize(NyLPC_TcIPv4Payload_t* i_inst); +#define NyLPC_cIPv4Payload_finalize(i) + +/** + * アタッチされているバッファを返します。 + */ +#define NyLPC_cIPv4Payload_getBuf(i) ((i)->header) + + + +void NyLPC_cIPv4Payload_attachTxBuf(NyLPC_TcIPv4Payload_t* i_inst,void* i_buf); +const void* NyLPC_cIPv4Payload_detachBuf(NyLPC_TcIPv4Payload_t* i_inst); + +NyLPC_TBool NyLPC_cIPv4Payload_attachRxBuf(NyLPC_TcIPv4Payload_t* i_inst,const void* i_buf,NyLPC_TUInt16 i_flagment_size); +void NyLPC_cIPv4Payload_setTcpReverseRstAck( + NyLPC_TcIPv4Payload_t* i_inst); +void NyLPC_cIPv4Payload_setTcpReverseRstAck2( + NyLPC_TcIPv4Payload_t* i_inst, + const NyLPC_TcIPv4Payload_t* i_src); + + + + +/** + * UDPの送信バッファを初期化します。 + */ +void* NyLPC_cIPv4Payload_initUdpTx(NyLPC_TcIPv4Payload_t* i_inst,NyLPC_TUInt8 i_iph_word,NyLPC_TUInt16 i_tcp_payload_size); +/** + * UDPの送信情報を設定します。 + */ +void NyLPC_cIPv4Payload_setUdpTxHeaderByConnection(NyLPC_TcIPv4Payload_t* i_inst,const struct uip_udp_conn* i_conn,const struct NyLPC_TIPv4Addr* i_dest_ip,NyLPC_TUInt16 i_dest_port); + +void NyLPC_cIPv4Payload_closeUdpTxPacket( + NyLPC_TcIPv4Payload_t* i_inst); + + +void NyLPC_TcIPv4TxPacket_initialize_icomp(NyLPC_TcIPv4Payload_t* i_inst,void* i_buf,NyLPC_TUInt8 i_iph_word,NyLPC_TUInt16 i_tcp_payload_size); + + + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* NYLPC_CIPV4PAYLOAD_PROTECTED_H_ */ + diff --git a/lib/src/netif/mimicip/NyLPC_cMiMicIpNetIf.c b/lib/src/netif/mimicip/NyLPC_cMiMicIpNetIf.c new file mode 100644 index 0000000..0148739 --- /dev/null +++ b/lib/src/netif/mimicip/NyLPC_cMiMicIpNetIf.c @@ -0,0 +1,953 @@ +/********************************************************************************* + * PROJECT: MiMic + * -------------------------------------------------------------------------------- + * + * This file is part of MiMic + * Copyright (C)2011 Ryo Iizuka + * + * MiMic is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + * + * For further information please contact. + * http://nyatla.jp/ + * or + * + * + * Parts of this file were leveraged from uIP: + * + * Copyright (c) 2001-2003, Adam Dunkels. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +//#include "NyLPC_cIPv4IComp_protected.h" +//#include "NyLPC_cTcpListener_protected.h" +#include "NyLPC_cMiMicIpNetIf_protected.h" +#include "NyLPC_stdlib.h" +#include "../NyLPC_NetIf_ip_types.h" +#include "NyLPC_cIPv4IComp_protected.h" +#include "NyLPC_cMiMicIpTcpSocket_protected.h" +#include "NyLPC_cMiMicIpUdpSocket_protected.h" +#include "NyLPC_cMiMicIpTcpListener_protected.h" + + + + + +/**************************************************** + * UipServiceに関する宣言:その他 + ***************************************************/ +/** + * イーサネットフレームの読み出し構造体 + */ +struct TEthPacket +{ + struct NyLPC_TEthernetIIHeader header; + union{ + struct NyLPC_TArpHeader arp; + struct NyLPC_TIPv4Header ipv4; + }data; +}PACK_STRUCT_END; + + + +/** + * サービスインスタンスのポインタ。サービスが稼働中はインスタンスのポインタが有効です。 + */ +NyLPC_TcMiMicIpNetIf_t* _NyLPC_TcMiMicIpNetIf_inst=NULL; + + + +/** + * uipタスク + */ +static int uipTask(void *pvParameters); + +/** イーサネットドライバからのハンドラ*/ +static void ethernet_handler(void* i_param,NyLPC_TiEthernetDevice_EVENT i_type); + +//-------------------------------------------------------------- + + +static NyLPC_TBool sendIPv4Tx(struct TEthPacket* i_eth_buf); + +//static void sendArpReqest(const struct TEthPacket* i_eth_packet); +static void sendRawEthFrameNL(void* i_buf,NyLPC_TUInt16 i_len); +static void releaseTxBufNL(void* i_buf); + +/**メッセージなし*/ +#define TTaskMessage_MSG_NULL 0x0000 +/**uipコアタスクに、開始要求する*/ +#define TTaskMessage_MSG_RUN 0x0001 +/**uipコアタスクに、停止要求する*/ +#define TTaskMessage_MSG_STOP 0x0002 + + +static NyLPC_TcThread_t th; + +NyLPC_TBool NyLPC_cMiMicIpNetIf_initialize(NyLPC_TcMiMicIpNetIf_t* i_inst) +{ + //サービスは停止している事。 - Service must be uninitialized. + NyLPC_Assert(!NyLPC_cMiMicIpNetIf_isInitService()); + //IP処理部分の初期化 + NyLPC_cIPv4_initialize(&(i_inst->_tcpv4)); + //EMAC割込セマフォ + NyLPC_cSemaphore_initialize(&i_inst->_emac_semapho); + + i_inst->_status=0x00; + NyLPC_cStopwatch_initialize(&(i_inst->_arp_sw)); + NyLPC_cStopwatch_initialize(&(i_inst->_periodic_sw)); + NyLPC_AbortIfNot(NyLPC_cMutex_initialize(&(i_inst->_mutex))); + + _NyLPC_TcMiMicIpNetIf_inst=i_inst; + //タスク起動 + NyLPC_cThread_initialize(&th,NyLPC_cMiMicIpNetIf_config_STACK_SIZE,NyLPC_TcThread_PRIORITY_SERVICE); + NyLPC_cThread_start(&th,uipTask,NULL); + return NyLPC_TBool_TRUE; +} + + + + + + + +/** + * UIP処理を開始します。 + * この関数はリエントラントではありません。複数のタスクから共有するときには、排他ロックを掛けてください。 + * @param i_ref_config + * このコンフィギュレーションは、stopを実行するまでの間、インスタンスから参照します。外部で保持してください。 + */ +void NyLPC_cMiMicIpNetIf_start(const NyLPC_TcIPv4Config_t* i_ref_config) +{ + NyLPC_TcMiMicIpNetIf_t* inst=_NyLPC_TcMiMicIpNetIf_inst; + NyLPC_Assert(NyLPC_cMiMicIpNetIf_isInitService()); + if(!NyLPC_cMiMicIpNetIf_isRun()) + { + //はじめて起動するときに1度だけデバイス取得(タスクスイッチが動いてないと動かないからここで。) + if(inst->_ethif==NULL){ + inst->_ethif=getEthernetDevicePnP(); + } + //コンフィグレーションセット + inst->_netinfo.current_config=i_ref_config; + //開始要求セット + NyLPC_TUInt16_setBit(inst->_status,NyLPC_TcMiMicIpNetIf_ORDER_START); + //Order実行待ち + while(NyLPC_TUInt16_isBitOn(inst->_status,NyLPC_TcMiMicIpNetIf_ORDER_START)){ + NyLPC_cThread_sleep(10); + } + //デバイス情報の追記 + inst->_netinfo.device_name=NyLPC_iEthernetDevice_getDevicName(inst->_ethif); + } + return; +} +/** + * UIP処理を停止します。 + * この関数はリエントラントではありません。複数のタスクから共有するときには、排他ロックを掛けてください。 + * いまのところ、ストップシーケンスの実装は良くありません。 + * 再設計が必要。 + */ +void NyLPC_cMiMicIpNetIf_stop(void) +{ + NyLPC_TcMiMicIpNetIf_t* inst=_NyLPC_TcMiMicIpNetIf_inst; + NyLPC_Assert(NyLPC_cMiMicIpNetIf_isInitService()); + if(NyLPC_cMiMicIpNetIf_isRun()) + { + NyLPC_TUInt16_setBit(inst->_status,NyLPC_TcMiMicIpNetIf_ORDER_STOP); + //Order実行待ち + while(NyLPC_TUInt16_isBitOn(inst->_status,NyLPC_TcMiMicIpNetIf_ORDER_STOP)){ + NyLPC_cThread_sleep(10); + } + } + return; +} + +static const struct NyLPC_TNetInterfaceInfo* NyLPC_cMiMicIpNetIf_getInterfaceInfo(void) +{ + NyLPC_TcMiMicIpNetIf_t* inst=_NyLPC_TcMiMicIpNetIf_inst; + return &inst->_netinfo; +} + +/********************************************************************** + * + * + * + *********************************************************************/ + + +//PERIODIC rate +#define PERIODIC_TIMER (1*200) +#define ARP_TIMER (60*1000*10) + + + +/** + * 操作キューを確認して、タスクのステータスをアップデートします。 + * 高速化のため、Proc-Callerを使用していません。複雑なタスク操作をするときには、書き換えてください。 + * @return + * UIPタスクの実行状態 + */ +static NyLPC_TBool updateTaskStatus() +{ + NyLPC_TcMiMicIpNetIf_t* inst=_NyLPC_TcMiMicIpNetIf_inst; + if(NyLPC_cMiMicIpNetIf_isRun()){ + //開始状態 + if(NyLPC_TUInt16_isBitOn(inst->_status,NyLPC_TcMiMicIpNetIf_ORDER_STOP)) + { + //停止操作 + NyLPC_iEthernetDevice_stop(inst->_ethif); + NyLPC_cIPv4_stop(&(inst->_tcpv4)); + NyLPC_cIPv4IComp_finalize(&(inst->_icomp)); + NyLPC_cIPv4Arp_finalize(&(inst->_arp)); +// inst->_ref_config=NULL; + NyLPC_TUInt16_unsetBit(inst->_status,NyLPC_TcMiMicIpNetIf_STATUSBIT_IS_RUNNING); + NyLPC_TUInt16_unsetBit(inst->_status,NyLPC_TcMiMicIpNetIf_ORDER_STOP); + return NyLPC_TBool_FALSE; + } + return NyLPC_TBool_TRUE; + }else{ + //停止状態 + if(NyLPC_TUInt16_isBitOn(inst->_status,NyLPC_TcMiMicIpNetIf_ORDER_START)) + { + //TCP,ICOMPの初期化 + NyLPC_cIPv4_start(&(inst->_tcpv4),inst->_netinfo.current_config); + NyLPC_cIPv4IComp_initialize(&(inst->_icomp),inst->_netinfo.current_config); + //uip_arp_init(msg->start.ref_config); + NyLPC_cIPv4Arp_initialize(&(inst->_arp),inst->_netinfo.current_config); + NyLPC_cStopwatch_startExpire(&(inst->_arp_sw),1);//1度ARPを起動するため。 + NyLPC_cStopwatch_startExpire(&(inst->_periodic_sw),PERIODIC_TIMER); + //EtherNETデバイス初期化 + while(!NyLPC_iEthernetDevice_start(inst->_ethif,&(inst->_netinfo.current_config->eth_mac),ethernet_handler,inst)); + NyLPC_TUInt16_setBit(inst->_status,NyLPC_TcMiMicIpNetIf_STATUSBIT_IS_RUNNING); + NyLPC_TUInt16_unsetBit(inst->_status,NyLPC_TcMiMicIpNetIf_ORDER_START); + return NyLPC_TBool_TRUE; + } + return NyLPC_TBool_FALSE; + } +} + +/** + * uipタスクのメインループ + */ +static int uipTask(void *pvParameters) +{ + NyLPC_TUInt16 rx_len,tx_len; + struct TEthPacket* ethbuf; + NyLPC_TcMiMicIpNetIf_t* inst=_NyLPC_TcMiMicIpNetIf_inst; + void* r; + (void)pvParameters; + for( ;; ) + { + //タスク状態の更新 + if(!updateTaskStatus()) + { + //RUNステータス以外の時は、ここで終了する。 + NyLPC_cThread_sleep(50); + continue; + } + //イーサネットフレームの取得 + //Ethernet Device Lock(ARPを含む) + NyLPC_cMutex_lock(&(inst->_mutex)); + ethbuf= (struct TEthPacket*)NyLPC_iEthernetDevice_getRxEthFrame(inst->_ethif,&rx_len); + tx_len=0; + while(ethbuf != NULL){ + if(rx_len>0) + { + //ペイロードサイズを計算 + rx_len-=sizeof(struct NyLPC_TEthernetIIHeader); + switch(ethbuf->header.type) + { + case NyLPC_HTONS(NyLPC_TEthernetIIHeader_TYPE_IP): + //ARPテーブルの更新 + //uip_arp_ipin(&(ethbuf->header),ethbuf->data.ipv4.srcipaddr); + NyLPC_cIPv4Arp_incomingIp(&inst->_arp,&(ethbuf->header),ethbuf->data.ipv4.srcipaddr); + //Ethernet Device UnLock(NyLPC_cIPv4_rxがallocをコールする可能性があるので一時的にロック解除) + NyLPC_cMutex_unlock(&(inst->_mutex)); + //IPパケットの処理 + r=NyLPC_cIPv4_rx(&(inst->_tcpv4),&(ethbuf->data.ipv4),rx_len); + if(r!=NULL){ + //IPパケットとして送信 + NyLPC_cMiMicIpNetIf_sendIPv4Tx(r); + } + //ロックの復帰 + NyLPC_cMutex_lock(&(inst->_mutex)); + if(r!=NULL){ + releaseTxBufNL(r); + } + break; + case NyLPC_HTONS(NyLPC_TEthernetIIHeader_TYPE_ARP): + //Ethernet Device UnLock(NyLPC_cIPv4_rxがallocをコールする可能性があるので一時的にロック解除) + NyLPC_cMutex_unlock(&(inst->_mutex)); + r=NyLPC_cIPv4Arp_rx(&inst->_arp,&(ethbuf->data.arp),rx_len,&tx_len); + NyLPC_cMutex_lock(&(inst->_mutex)); + if(r!=NULL){ + sendRawEthFrameNL(r,tx_len); + releaseTxBufNL(r); + } + break; + case NyLPC_HTONS(NyLPC_TEthernetIIHeader_TYPE_IPV6): + //uip_process_ipv6(); + break; + default: + break; + } + } + //受信キューを進行。 + NyLPC_iEthernetDevice_nextRxEthFrame(inst->_ethif); + //受信処理 + ethbuf= (struct TEthPacket*)NyLPC_iEthernetDevice_getRxEthFrame(inst->_ethif,&rx_len); + } + //データが無い。 + if(NyLPC_cStopwatch_isExpired(&(inst->_arp_sw))){ + //uip_arp_timer(); + NyLPC_cIPv4Arp_periodic(&inst->_arp); + NyLPC_cStopwatch_startExpire(&(inst->_arp_sw),ARP_TIMER); + } + if(NyLPC_cStopwatch_isExpired(&(inst->_periodic_sw))){ + NyLPC_cMutex_unlock(&(inst->_mutex)); + NyLPC_cIPv4_periodec(&(inst->_tcpv4)); + NyLPC_cMutex_lock(&(inst->_mutex)); + NyLPC_cStopwatch_startExpire(&(inst->_periodic_sw),PERIODIC_TIMER); + } + //リソースロックの解除 + NyLPC_cMutex_unlock(&(inst->_mutex)); + //割込によるセマフォの解除か、タイムアウトで再開する。(タイムアウト値は周期関数の実行レート以下にすること。) + NyLPC_cSemaphore_take(&(_NyLPC_TcMiMicIpNetIf_inst->_emac_semapho),PERIODIC_TIMER); + } + return 0; +} + + +/** + * イーサネットドライバからのハンドラ + */ +static void ethernet_handler(void* i_param,NyLPC_TiEthernetDevice_EVENT i_type) +{ + switch(i_type){ + case NyLPC_TiEthernetDevice_EVENT_ON_RX: + //受信系のセマフォブロックの解除 + NyLPC_cSemaphore_giveFromISR(&(((NyLPC_TcMiMicIpNetIf_t*)i_param)->_emac_semapho)); + break; + default: + break; + } +} + +/** + * IPv4パケットのpeerIPを問い合わせるARPパケットを送信します。 + * allocを中でコールするから要UNLOCK状態 + */ +void NyLPC_cMiMicIpNetIf_sendArpRequest(const struct NyLPC_TIPv4Addr* i_addr) +{ + NyLPC_TcMiMicIpNetIf_t* inst=_NyLPC_TcMiMicIpNetIf_inst; + NyLPC_TUInt16 tx_len; + struct TEthPacket* ethbuf; + //システムTXメモリを得てイーサフレームのポインタを復元 + ethbuf=(struct TEthPacket*)(((struct NyLPC_TEthernetIIHeader*)NyLPC_cMiMicIpNetIf_allocSysTxBuf()-1)); + //ARPパケットを作る。 + NyLPC_TArpHeader_setArpRequest(&(ethbuf->data.arp),inst->_netinfo.current_config->ip_addr,&(inst->_netinfo.current_config->eth_mac),i_addr); + tx_len=NyLPC_TEthernetIIHeader_setArpTx(&(ethbuf->header),&(inst->_netinfo.current_config->eth_mac)); + //送信 + NyLPC_cMutex_lock(&(inst->_mutex)); + NyLPC_iEthernetDevice_sendTxEthFrame(inst->_ethif,ethbuf,tx_len); + NyLPC_iEthernetDevice_releaseTxBuf(inst->_ethif,ethbuf);//NyLPC_cMiMicIpNetIf_releaseTxBufの代用だから元のイーサフレームメモリの値で開放 + NyLPC_cMutex_unlock(&(inst->_mutex)); +} + + + + +/** + * allocTxBufで取得したペイロードメモリを"IPパケットとして"送信します。 + * @param i_eth_payload + * [NyLPC_TEthernetIIHeader][payload]メモリの、[payload]のアドレスを指定します。 + * 通常は、NyLPC_cUipService_allocTxBufの返却したメモリを指定します。 + */ + +void NyLPC_cMiMicIpNetIf_sendIPv4Tx(void* i_eth_payload) +{ + NyLPC_TcMiMicIpNetIf_t* inst=_NyLPC_TcMiMicIpNetIf_inst; + void* p=((struct NyLPC_TEthernetIIHeader*)i_eth_payload)-1; + NyLPC_cMutex_lock(&(inst->_mutex)); + //IPパケットの送信を試行 + if(sendIPv4Tx((struct TEthPacket*)p)){ + NyLPC_cMutex_unlock(&(inst->_mutex)); + return; + } + NyLPC_cMutex_unlock(&(inst->_mutex)); + //ARPリクエストを代わりに送信 + NyLPC_cMiMicIpNetIf_sendArpRequest(&((struct NyLPC_TIPv4Header*)i_eth_payload)->destipaddr); + return; +} + + +/** + * ARPテーブルに指定したIPがあるかを返します。 + */ +NyLPC_TBool NyLPC_cMiMicIpNetIf_hasArpInfo(const struct NyLPC_TIPv4Addr* i_addr) +{ + NyLPC_TcMiMicIpNetIf_t* inst=_NyLPC_TcMiMicIpNetIf_inst; + return NyLPC_cIPv4Arp_IPv4toEthAddr(&inst->_arp,*i_addr)!=NULL; +} + +/** + * システム用の送信ペイロードを返します。 + * 関数は必ず成功します。 + */ +void* NyLPC_cMiMicIpNetIf_allocSysTxBuf(void) +{ + NyLPC_TUInt16 s; + NyLPC_TcMiMicIpNetIf_t* inst=_NyLPC_TcMiMicIpNetIf_inst; + struct TEthPacket* ethbuf; + //排他処理をして、メモリを取得する。SYSTEMメモリはEthernetドライバの解放待ちのみなのでまとめてLOCKしておk + NyLPC_cMutex_lock(&(inst->_mutex)); + for(;;){ + ethbuf=(struct TEthPacket*)NyLPC_iEthernetDevice_allocTxBuf(inst->_ethif,NyLPC_TcEthernetMM_HINT_CTRL_PACKET,&s); + if(ethbuf==NULL){ + NyLPC_cThread_yield(); + continue; + } + break; + } + NyLPC_cMutex_unlock(&(inst->_mutex)); + //イーサネットバッファのアドレスを計算 + return &(ethbuf->data); +} + + + +void* NyLPC_cMiMicIpNetIf_allocTxBuf(NyLPC_TUInt16 i_hint,NyLPC_TUInt16* o_size) +{ + NyLPC_TcMiMicIpNetIf_t* inst=_NyLPC_TcMiMicIpNetIf_inst; + struct TEthPacket* ethbuf; + //排他処理をして、メモリを取得する。 + NyLPC_cMutex_lock(&(inst->_mutex)); + ethbuf=(struct TEthPacket*)NyLPC_iEthernetDevice_allocTxBuf(inst->_ethif,i_hint+sizeof(struct NyLPC_TEthernetIIHeader),o_size); + NyLPC_cMutex_unlock(&(inst->_mutex)); + if(ethbuf==NULL){ + return NULL; + } + //イーサネットバッファのサイズを計算 + *o_size-=sizeof(struct NyLPC_TEthernetIIHeader); + //イーサネットバッファのアドレスを計算 + return &(ethbuf->data); +} + + +void* NyLPC_cMiMicIpNetIf_releaseTxBuf(void* i_buf) +{ + //排他処理をして、メモリを開放する。 + NyLPC_TcMiMicIpNetIf_t* inst=_NyLPC_TcMiMicIpNetIf_inst; + NyLPC_cMutex_lock(&(inst->_mutex)); + //ペイロードの位置から、メモリブロックを再生。 + NyLPC_iEthernetDevice_releaseTxBuf(inst->_ethif,((struct NyLPC_TEthernetIIHeader*)i_buf)-1); + NyLPC_cMutex_unlock(&(inst->_mutex)); + return NULL; +} + + + + + + + + + +/** + * イーサネットフレームを送信します。 + * この関数はiptaskで実行される関数からのみ使用てください。 + * @i_buf + * イーサネットフレームを格納したメモリです。 + * @i_len + * イーサネットペイロードのサイズです。 + */ +static void sendRawEthFrameNL(void* i_buf,NyLPC_TUInt16 i_len) +{ + NyLPC_iEthernetDevice_sendTxEthFrame( + _NyLPC_TcMiMicIpNetIf_inst->_ethif, + ((struct NyLPC_TEthernetIIHeader*)i_buf)-1, + i_len); + return; +} +/** + * ロック状態で使用できるreleaseTxBuf。 + * この関数はiptaskで実行される関数からのみ使用してください。 + */ +static void releaseTxBufNL(void* i_buf) +{ + //ペイロードの位置から、メモリブロックを再生。 + NyLPC_iEthernetDevice_releaseTxBuf( + _NyLPC_TcMiMicIpNetIf_inst->_ethif, + ((struct NyLPC_TEthernetIIHeader*)i_buf)-1); + return; +} +/** + * マルチキャスとアドレスへ変換する。 + */ +static void ip2MulticastEmacAddr(const struct NyLPC_TIPv4Addr* i_addr,struct NyLPC_TEthAddr* o_emac) +{ + NyLPC_TUInt32 n=NyLPC_htonl(i_addr->v); + o_emac->addr[0]=0x01; + o_emac->addr[1]=0x00; + o_emac->addr[2]=0x5E; + o_emac->addr[3]=((n>>16) & 0x7f); + o_emac->addr[4]=((n>> 8) & 0xff); + o_emac->addr[5]=(n & 0xff); + return; +}; + +/** + * ペイロードをIPパケットとしてネットワークへ送出します。 + * コール前に、必ずロックしてから呼び出してください。 + * @param i_eth_payload + * allocTxBufで確保したメモリを指定してください。 + * ペイロードには、TCP/IPパケットを格納します。 + */ +static NyLPC_TBool sendIPv4Tx(struct TEthPacket* i_eth_buf) +{ + NyLPC_TcMiMicIpNetIf_t* inst=_NyLPC_TcMiMicIpNetIf_inst; + struct NyLPC_TEthAddr emac; + NyLPC_TUInt16 tx_len; + const struct NyLPC_TEthAddr* eth_dest; + //ペイロードのアドレスから、イーサネットフレームバッファのアドレスを復元 + + if(NyLPC_TIPv4Addr_isEqual(&(i_eth_buf->data.ipv4.destipaddr),&NyLPC_TIPv4Addr_BROADCAST)) { + //ブロードキャストならそのまま + eth_dest=&NyLPC_TEthAddr_BROADCAST; + }else if(NyLPC_TIPv4Addr_isEqualWithMask(&(i_eth_buf->data.ipv4.destipaddr),&NyLPC_TIPv4Addr_MULTICAST,&NyLPC_TIPv4Addr_MULTICAST_MASK)){ + //マルチキャスト + ip2MulticastEmacAddr(&(i_eth_buf->data.ipv4.destipaddr),&emac); + eth_dest=&emac; + }else{ + //LocalIP以外ならdefaultRootへ変換 + eth_dest=NyLPC_cIPv4Arp_IPv4toEthAddr( + &inst->_arp, + NyLPC_cIPv4Config_isLocalIP(inst->_netinfo.current_config, &(i_eth_buf->data.ipv4.destipaddr))?(i_eth_buf->data.ipv4.destipaddr):(inst->_netinfo.current_config->dr_addr)); + //IP->MAC変換をテスト。 + if(eth_dest==NULL){ + return NyLPC_TBool_FALSE; + } + } + //変換可能なら、イーサネットヘッダを更新して、送信処理へ。 + tx_len=NyLPC_TEthernetIIHeader_setIPv4Tx(&(i_eth_buf->header),&(inst->_netinfo.current_config->eth_mac),eth_dest); + NyLPC_iEthernetDevice_sendTxEthFrame(inst->_ethif,i_eth_buf,tx_len); + return NyLPC_TBool_TRUE; +} + +static NyLPC_TBool isInitService(void) +{ + return _NyLPC_TcMiMicIpNetIf_inst!=NULL; +} + +//-------------------------------------------------------------------------------- +// +// NetIF Interface +// +//-------------------------------------------------------------------------------- + + +//-------------------------------------------------------------------------------- +// ソケットテーブル +//-------------------------------------------------------------------------------- + +#define FLAGS_USED 0x00000001 + +struct TTcpTable +{ + NyLPC_TUInt32 flags; + NyLPC_TcMiMicIpTcpSocket_t socket; + NyLPC_TUInt8 rxbuf[NyLPC_cMiMicIpNetIf_config_TCPSOCKET_RX_BUFFER_SIZE]; +}; +struct TUdpTable +{ + NyLPC_TUInt32 flags; + NyLPC_TcMiMicIpUdpSocket_t socket; + NyLPC_TUInt8 rxbuf[NyLPC_cMiMicIpNetIf_config_UDPSOCKET_RX_BUFFER_SIZE]; +}; +struct TUdpNBTable +{ + NyLPC_TUInt32 flags; + NyLPC_TcMiMicIpUdpSocket_t socket; +}; +struct TTcpListenerTable +{ + NyLPC_TUInt32 flags; + NyLPC_TcMiMicIpTcpListener_t listener; +}; + + + +static struct TTcpTable tcp_socket_table[NyLPC_cMiMicIpNetIf_config_TCPSOCKET_MAX]; +static struct TUdpTable udp_socket_table[NyLPC_cMiMicIpNetIf_config_UDPSOCKET_MAX]; +static struct TUdpNBTable udp_socket_nb_table[NyLPC_cMiMicIpNetIf_config_NB_UDPSOCKET_MAX]; +static struct TTcpListenerTable tcp_listener_table[NyLPC_cMiMicIpNetIf_config_TCPLISTENER_MAX]; + +NyLPC_TcMiMicIpTcpListener_t* NyLPC_cMiMicIpNetIf_getListenerByPeerPort(NyLPC_TUInt16 i_port) +{ + int i; + //一致するポートを検索して、acceptをコールする。 + for(i=NyLPC_cMiMicIpNetIf_config_TCPLISTENER_MAX-1;i>=0;i--){ + if((tcp_listener_table[i].flags&FLAGS_USED)==0){ + continue; + } + if(tcp_listener_table[i].listener._port!=i_port){ + continue; + } + return &tcp_listener_table[i].listener; + } + return NULL; +} + +/** + * 指定番号のTCPポートが未使用かを返す。 + * @return + * i_lport番のポートが未使用であればTRUE + */ +NyLPC_TBool NyLPC_cMiMicIpNetIf_isClosedTcpPort(NyLPC_TUInt16 i_lport) +{ + int i; + //未使用のTCPソケット? + for(i=NyLPC_cMiMicIpNetIf_config_TCPSOCKET_MAX-1;i>=0;i--){ + if( ((tcp_socket_table[i].flags&FLAGS_USED)!=0) && + (NyLPC_cMiMicIpTcpSocket_getLocalPort(&tcp_socket_table[i].socket)==i_lport)&& + (!NyLPC_cMiMicIpTcpSocket_isClosed(&tcp_socket_table[i].socket))){ + //ポート使用中 + return NyLPC_TBool_FALSE; + } + } + for(i=NyLPC_cMiMicIpNetIf_config_TCPLISTENER_MAX-1;i>=0;i--){ + if( ((tcp_listener_table[i].flags&FLAGS_USED)!=0) && + (NyLPC_cMiMicIpTcpListener_getLocalPort(&tcp_listener_table[i].listener)==i_lport)){ + //ポート使用中 + return NyLPC_TBool_FALSE; + } + } + return NyLPC_TBool_TRUE; +} + +/** + * 条件に一致する、アクティブなTCPソケットオブジェクトを取得します。 + * この関数は、ローカルIPが一致していると仮定して検索をします。 + * @param i_rip + * リモートIPアドレスを指定します。 + */ +NyLPC_TcMiMicIpTcpSocket_t* NyLPC_cMiMicIpNetIf_getMatchTcpSocket( + NyLPC_TUInt16 i_lport,struct NyLPC_TIPv4Addr i_rip,NyLPC_TUInt16 i_rport) +{ + NyLPC_TcMiMicIpTcpSocket_t* tp; + int i; + //一致するポートを検索 + for(i=NyLPC_cMiMicIpNetIf_config_TCPSOCKET_MAX-1;i>=0;i--){ + if((tcp_socket_table[i].flags&FLAGS_USED)==0){ + continue; + } + if(NyLPC_cMiMicIpTcpSocket_isClosed(&tcp_socket_table[i].socket)){ + continue; + } + tp=&tcp_socket_table[i].socket; + //パラメータの一致チェック + if(i_lport!=tp->uip_connr.lport || i_rport!= tp->uip_connr.rport || i_rip.v!=tp->uip_connr.ripaddr.v) + { + continue; + } + return tp; + } + return NULL; +} +NyLPC_TcMiMicIpUdpSocket_t* NyLPC_cMiMicIpNetIf_getMatchUdpSocket( + NyLPC_TUInt16 i_lport) +{ + int i; + for(i=NyLPC_cMiMicIpNetIf_config_UDPSOCKET_MAX-1;i>=0;i--){ + if((udp_socket_table[i].flags&FLAGS_USED)==0){ + continue; + } + if(i_lport!=udp_socket_table[i].socket.uip_udp_conn.lport){ + continue; + } + //unicast + return &udp_socket_table[i].socket; + } + for(i=NyLPC_cMiMicIpNetIf_config_NB_UDPSOCKET_MAX-1;i>=0;i--){ + if((udp_socket_nb_table[i].flags&FLAGS_USED)==0){ + continue; + } + if(i_lport!=udp_socket_nb_table[i].socket.uip_udp_conn.lport){ + continue; + } + //unicast + return &udp_socket_nb_table[i].socket; + } + return NULL; +} +NyLPC_TcMiMicIpUdpSocket_t* NyLPC_cMiMicIpNetIf_getMatchMulticastUdpSocket( + const struct NyLPC_TIPv4Addr* i_mcast_ip, + NyLPC_TUInt16 i_lport) +{ + int i; + for(i=NyLPC_cMiMicIpNetIf_config_UDPSOCKET_MAX-1;i>=0;i--){ + if((udp_socket_table[i].flags&FLAGS_USED)==0){ + continue; + } + if(i_lport!=udp_socket_table[i].socket.uip_udp_conn.lport || (!NyLPC_TIPv4Addr_isEqual(i_mcast_ip,&(udp_socket_table[i].socket.uip_udp_conn.mcastaddr)))) + { + continue; + } + return &udp_socket_table[i].socket; + } + for(i=NyLPC_cMiMicIpNetIf_config_NB_UDPSOCKET_MAX-1;i>=0;i--){ + if((udp_socket_nb_table[i].flags&FLAGS_USED)==0){ + continue; + } + if(i_lport!=udp_socket_nb_table[i].socket.uip_udp_conn.lport || (!NyLPC_TIPv4Addr_isEqual(i_mcast_ip,&(udp_socket_nb_table[i].socket.uip_udp_conn.mcastaddr)))) + { + continue; + } + return &udp_socket_nb_table[i].socket; + } + return NULL; +} + + +void NyLPC_cMiMicIpNetIf_callPeriodic(void) +{ + int i; + for(i=NyLPC_cMiMicIpNetIf_config_UDPSOCKET_MAX-1;i>=0;i--){ + if((udp_socket_table[i].flags&FLAGS_USED)!=0){ + NyLPC_cMiMicIpUdpSocket_periodic(&udp_socket_table[i].socket); + } + } + for(i=NyLPC_cMiMicIpNetIf_config_NB_UDPSOCKET_MAX-1;i>=0;i--){ + if((udp_socket_nb_table[i].flags&FLAGS_USED)!=0){ + NyLPC_cMiMicIpUdpSocket_periodic(&udp_socket_nb_table[i].socket); + } + } + for(i=NyLPC_cMiMicIpNetIf_config_TCPSOCKET_MAX-1;i>=0;i--){ + if((tcp_socket_table[i].flags&FLAGS_USED)!=0){ + NyLPC_cMiMicIpTcpSocket_periodic(&tcp_socket_table[i].socket); + } + } +} +void NyLPC_cMiMicIpNetIf_callSocketStart( + const NyLPC_TcIPv4Config_t* i_cfg) +{ + int i; + for(i=NyLPC_cMiMicIpNetIf_config_UDPSOCKET_MAX-1;i>=0;i--){ + if((udp_socket_table[i].flags&FLAGS_USED)!=0){ + NyLPC_cMiMicIpUdpSocket_startService(&udp_socket_table[i].socket,i_cfg); + } + } + for(i=NyLPC_cMiMicIpNetIf_config_NB_UDPSOCKET_MAX-1;i>=0;i--){ + if((udp_socket_nb_table[i].flags&FLAGS_USED)!=0){ + NyLPC_cMiMicIpUdpSocket_startService(&udp_socket_nb_table[i].socket,i_cfg); + } + } + for(i=NyLPC_cMiMicIpNetIf_config_TCPSOCKET_MAX-1;i>=0;i--){ + if((tcp_socket_table[i].flags&FLAGS_USED)!=0){ + NyLPC_cMiMicIpTcpSocket_startService(&tcp_socket_table[i].socket,i_cfg); + } + } +} +void NyLPC_cMiMicIpNetIf_callSocketStop(void) +{ + int i; + for(i=NyLPC_cMiMicIpNetIf_config_UDPSOCKET_MAX-1;i>=0;i--){ + if((udp_socket_table[i].flags&FLAGS_USED)!=0){ + NyLPC_cMiMicIpUdpSocket_stopService(&udp_socket_table[i].socket); + } + } + for(i=NyLPC_cMiMicIpNetIf_config_NB_UDPSOCKET_MAX-1;i>=0;i--){ + if((udp_socket_nb_table[i].flags&FLAGS_USED)!=0){ + NyLPC_cMiMicIpUdpSocket_stopService(&udp_socket_nb_table[i].socket); + } + } + for(i=NyLPC_cMiMicIpNetIf_config_TCPSOCKET_MAX-1;i>=0;i--){ + if((tcp_socket_table[i].flags&FLAGS_USED)!=0){ + NyLPC_cMiMicIpTcpSocket_stopService(&tcp_socket_table[i].socket); + } + } +} + + + +//-------------------------------------------------------------------------------- +// インタフェイス関数 + +static NyLPC_TiTcpSocket_t* createTcpSocetEx(NyLPC_TSocketType i_socktype) +{ + NyLPC_TUInt16 i; + switch(i_socktype){ + case NyLPC_TSocketType_TCP_HTTP: + case NyLPC_TSocketType_TCP_NORMAL: + //空きソケットの探索 + for(i=0;i. - * - * For further information please contact. - * http://nyatla.jp/ - * or - * - * - * Parts of this file were leveraged from uIP: - * - * Copyright (c) 2001-2003, Adam Dunkels. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote - * products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS - * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY - * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE - * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ -#ifndef NyLPC_TcUipService_H_ -#define NyLPC_TcUipService_H_ - - -/********************************************************************** +/********************************************************************************* + * PROJECT: MiMic + * -------------------------------------------------------------------------------- + * + * This file is part of MiMic + * Copyright (C)2011 Ryo Iizuka + * + * MiMic is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + * + * For further information please contact. + * http://nyatla.jp/ + * or + * + * + * Parts of this file were leveraged from uIP: + * + * Copyright (c) 2001-2003, Adam Dunkels. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +#ifndef NyLPC_TcMiMicIpNetIf_H_ +#define NyLPC_TcMiMicIpNetIf_H_ +#include "NyLPC_config.h" +#include "NyLPC_stdlib.h" +#include "../NyLPC_iNetInterface.h" +#include "../NyLPC_cIPv4Config.h" +/********************************************************************** * Heder files - **********************************************************************/ - -#include "NyLPC_cTcpSocket.h" -#include "NyLPC_cTcpListener.h" -#include "NyLPC_cIPv4IComp.h" -#include "NyLPC_cIPv4Arp.h" -#include "NyLPC_cIPv4.h" - -#ifdef __cplusplus -extern "C" { -#endif /* __cplusplus */ - -/********************************************************************** - * - * NyLPC_TcUipService class - * - **********************************************************************/ -/** - * uipタスクを管理する、サービスクラスです。 - */ -typedef struct NyLPC_TcUipService NyLPC_TcUipService_t; - - -/** サービスタスクスタックサイズ - * mdns 256+96(MIN) - * upnp 256+160(MIN) - * mbedのExportしたのをLPCXpressoでコンパイルするときは+192しないと落ちる。 - */ -#ifndef NyLPC_cUipService_config_STACK_SIZE -#define NyLPC_cUipService_config_STACK_SIZE (256+192+192) -#endif - - - - -/** - * \param i_mac_addr - * システムで唯一のUIPサービスを初期化します。1度だけ実行できます。 - */ -NyLPC_TBool NyLPC_cUipService_initialize(void); - -/** - * 処理を開始します。 - * この関数はリエントラントではありません。複数のタスクから共有するときには、排他ロックを掛けてください。 - * @param i_ref_config - * このコンフィギュレーションは、stopを実行するまでの間、インスタンスから参照します。外部で保持してください。 - */ -void NyLPC_cUipService_start(const NyLPC_TcIPv4Config_t* i_ref_config); - -/** - * UIP処理を停止します。 - * この関数はリエントラントではありません。複数のタスクから共有するときには、排他ロックを掛けてください。 - * 関数を使用する前に、全ての非同期ソケット操作を停止してください。 - */ -void NyLPC_cUipService_stop(void); - -/** - * イーサネットデバイスの名前を返します。 - * この関数は、サービスが開始されているときのみ、動作します。 - */ -const char* NyLPC_cUipService_refDeviceName(void); -const NyLPC_TcIPv4Config_t* NyLPC_cUipService_refCurrentConfig(void); - -#ifdef __cplusplus -} -#endif /* __cplusplus */ - -#endif + **********************************************************************/ + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +/********************************************************************** + * + * NyLPC_TcUipService class + * + **********************************************************************/ +/** + * uipタスクを管理する、サービスクラスです。 + */ +typedef struct NyLPC_TcMiMicIpNetIf NyLPC_TcMiMicIpNetIf_t; + + +/** サービスタスクスタックサイズ + * mdns 256+96(MIN) + * upnp 256+160(MIN) + * mbedのExportしたのをLPCXpressoでコンパイルするときは+192しないと落ちる。 + */ +#ifndef NyLPC_cMiMicIpNetIf_config_STACK_SIZE +# define NyLPC_cMiMicIpNetIf_config_STACK_SIZE (256+256+192) +#endif + +/** + * SOCKETリソースの最大数とRXメモリサイズ + */ + +#ifndef NyLPC_cMiMicIpNetIf_config_TCPLISTENER_MAX +# define NyLPC_cMiMicIpNetIf_config_TCPLISTENER_MAX 2 +#endif +#ifndef NyLPC_cMiMicIpNetIf_config_TCPSOCKET_MAX +# define NyLPC_cMiMicIpNetIf_config_TCPSOCKET_MAX 3 +#endif +#ifndef NyLPC_cMiMicIpNetIf_config_UDPSOCKET_MAX +# define NyLPC_cMiMicIpNetIf_config_UDPSOCKET_MAX 1 +#endif +#ifndef NyLPC_cMiMicIpNetIf_config_NB_UDPSOCKET_MAX +# define NyLPC_cMiMicIpNetIf_config_NB_UDPSOCKET_MAX 2 +#endif +#ifndef NyLPC_cMiMicIpNetIf_config_TCPSOCKET_RX_BUFFER_SIZE +# define NyLPC_cMiMicIpNetIf_config_TCPSOCKET_RX_BUFFER_SIZE 512 +#endif +#ifndef NyLPC_cMiMicIpNetIf_config_UDPSOCKET_RX_BUFFER_SIZE +# define NyLPC_cMiMicIpNetIf_config_UDPSOCKET_RX_BUFFER_SIZE 512 +#endif + + + + + + +//const NyLPC_TcIPv4Config_t* NyLPC_cUipService_refCurrentConfig(void); +const struct NyLPC_TiNetInterface_Interface* NyLPC_cMiMicIpNetIf_getNetInterface(void); + + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif + diff --git a/lib/src/uip/NyLPC_cUipService_protected.h b/lib/src/netif/mimicip/NyLPC_cMiMicIpNetIf_protected.h similarity index 54% rename from lib/src/uip/NyLPC_cUipService_protected.h rename to lib/src/netif/mimicip/NyLPC_cMiMicIpNetIf_protected.h index 9450421..d1b3ddd 100644 --- a/lib/src/uip/NyLPC_cUipService_protected.h +++ b/lib/src/netif/mimicip/NyLPC_cMiMicIpNetIf_protected.h @@ -1,159 +1,190 @@ -/********************************************************************************* - * PROJECT: MiMic - * -------------------------------------------------------------------------------- - * - * This file is part of MiMic - * Copyright (C)2011 Ryo Iizuka - * - * MiMic is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published - * by the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program. If not, see . - * - * For further information please contact. - * http://nyatla.jp/ - * or - * - *********************************************************************************/ - -#ifndef NyLPC_uipService_protected_H -#define NyLPC_uipService_protected_H -#include "NyLPC_cUipService.h" -#include "../driver/ethernet/EthDev.h" - -/********************************************************************** - * - * NyLPC_TcUipService_t - * - **********************************************************************/ -#ifdef __cplusplus -extern "C" { -#endif /* __cplusplus */ - -//#define NyLPC_TcUipService_SIZE_OF_REPLY_BUF 128 - - - -struct NyLPC_TcUipService -{ - const NyLPC_TcIPv4Config_t* _ref_config; - volatile NyLPC_TUInt16 _status; /**< ステータスビット*/ - NyLPC_TcSemaphore_t _emac_semapho; /** EMACの制御用セマフォです。*/ - NyLPC_TcStopwatch_t _arp_sw; /**_status,NyLPC_TcUipService_STATUSBIT_IS_RUNNING) - - -/********************************************************************** - * コントロールビットの定義 - **********************************************************************/ -//サービスが実行中の場合1 -#define NyLPC_TcUipService_STATUSBIT_IS_RUNNING 0 -#define NyLPC_TcUipService_ORDER_START 1 -#define NyLPC_TcUipService_ORDER_STOP 2 - - - -#define INST_TYPE_NyLPC_Unknown 0 -#define INST_TYPE_NyLPC_TcTcpListener 1 -#define INST_TYPE_NyLPC_TcTcpSocket 2 - - -/********************************************************************** - * cTcpSocketからコールする関数 - **********************************************************************/ - -/** - * NyLPC_cUipService_allocTxBufが返却するメモリサイズ。 - * - */ -#define NyLPC_cUipService_SYS_TX_BUF_SIZE (64-sizeof(struct NyLPC_TEthernetIIHeader)) - - -/** - * IPv4パケットをネットワークに送信します。 - * この関数は、リエントラントを許容します。 - * @param i_eth_payload - * NyLPC_cUipService_getTxFrame、または、NyLPC_cUipService_recvIPv4Rxで得たバッファに、 - * IPv4パケットを書きこんだものを指定してください。 - */ -void NyLPC_cUipService_sendIPv4Tx(void* i_eth_payload); - -/** - * 送信ペイロードメモリを返します。 - * この関数は、リエントラントを許容します。 - * @param i_hint - * 取得したいメモリサイズを指定します。(このサイズは、イーサネットヘッダのサイズを含みません。) - * このサイズよりも小さなサイズが割り当てられることがあります。 - * @param o_size - * 取得メモリのイーサネットヘッダを除いたペイロード部分の長さ - * @return - * 成功:IPペイロードのためのメモリブロックを返します。/失敗:NULL - * メモリは、[TEthPacket][payload]の構造で返されます。 - */ -void* NyLPC_cUipService_allocTxBuf(NyLPC_TUInt16 i_hint,NyLPC_TUInt16* o_size); - -/** - * システム用の小さな送信ペイロードメモリを返します。 - * この関数は、リエントラントを許容します。 - * 返却するメモリブロックのサイズが小さいこと、メモリが確実に返される点がNyLPC_cUipService_allocTxBufと異なります。 - * この関数が返すメモリのサイズは、NyLPC_cUipService_SYS_TX_BUF_SIZEの値です。 - * 関数を使用するコードでは開始時に1度だけNyLPC_cUipService_SYS_TX_BUF_SIZEの値を確認して下さい。 - */ -void* NyLPC_cUipService_allocSysTxBuf(void); - -/** - * allocTxbufで確保したメモリを開放します。 - */ -void* NyLPC_cUipService_releaseTxBuf(void* i_buf); - -/** - * 指定したIPアドレスを要求するARPリクエストを発行します。 - */ -void NyLPC_cUipService_sendArpRequest(const struct NyLPC_TIPv4Addr* i_addr); - -/** - * ARPテーブルに指定したIPがあるかを返します。 - */ -NyLPC_TBool NyLPC_cUipService_hasArpInfo(const struct NyLPC_TIPv4Addr* i_addr); - - - - - - -#ifdef __cplusplus -} -#endif /* __cplusplus */ - -#endif +/********************************************************************************* + * PROJECT: MiMic + * -------------------------------------------------------------------------------- + * + * This file is part of MiMic + * Copyright (C)2011 Ryo Iizuka + * + * MiMic is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + * + * For further information please contact. + * http://nyatla.jp/ + * or + * + *********************************************************************************/ + +#ifndef NyLPC_MiMicIpNetIf_protected_H +#define NyLPC_MiMicIpNetIf_protected_H +#include "NyLPC_cMiMicIpNetIf.h" +#include "NyLPC_os.h" +#include "NyLPC_cIPv4Arp.h" +#include "NyLPC_cIPv4.h" +#include "NyLPC_cIPv4IComp.h" +#include "../driver/ethernet/EthDev.h" +#include "NyLPC_cMiMicIpTcpSocket.h" +#include "NyLPC_cMiMicIpUdpSocket.h" +#include "NyLPC_cMiMicIpTcpListener.h" + +/********************************************************************** + * + * NyLPC_TcUipService_t + * + **********************************************************************/ +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +//#define NyLPC_TcUipService_SIZE_OF_REPLY_BUF 128 + + + +struct NyLPC_TcMiMicIpNetIf +{ + volatile NyLPC_TUInt16 _status; /**< ステータスビット*/ + NyLPC_TcSemaphore_t _emac_semapho; /** EMACの制御用セマフォです。*/ + NyLPC_TcStopwatch_t _arp_sw; /**_status,NyLPC_TcMiMicIpNetIf_STATUSBIT_IS_RUNNING) + + +/********************************************************************** + * コントロールビットの定義 + **********************************************************************/ +//サービスが実行中の場合1 +#define NyLPC_TcMiMicIpNetIf_STATUSBIT_IS_RUNNING 0 +#define NyLPC_TcMiMicIpNetIf_ORDER_START 1 +#define NyLPC_TcMiMicIpNetIf_ORDER_STOP 2 + + + +#define INST_TYPE_NyLPC_Unknown 0 +#define INST_TYPE_NyLPC_TcTcpListener 1 +#define INST_TYPE_NyLPC_TcTcpSocket 2 + + +/********************************************************************** + * + * IPスタックからコールする関数群 + * + **********************************************************************/ + +/** + * NyLPC_cUipService_allocTxBufが返却するメモリサイズ。 + * + */ +#define NyLPC_cMiMicIpNetIf_SYS_TX_BUF_SIZE (64-sizeof(struct NyLPC_TEthernetIIHeader)) + + +/** + * IPv4パケットをネットワークに送信します。 + * この関数は、リエントラントを許容します。 + * @param i_eth_payload + * NyLPC_cUipService_getTxFrame、または、NyLPC_cUipService_recvIPv4Rxで得たバッファに、 + * IPv4パケットを書きこんだものを指定してください。 + */ +void NyLPC_cMiMicIpNetIf_sendIPv4Tx(void* i_eth_payload); + +/** + * 送信ペイロードメモリを返します。 + * この関数は、リエントラントを許容します。 + * @param i_hint + * 取得したいメモリサイズを指定します。(このサイズは、イーサネットヘッダのサイズを含みません。) + * このサイズよりも小さなサイズが割り当てられることがあります。 + * @param o_size + * 取得メモリのイーサネットヘッダを除いたペイロード部分の長さ + * @return + * 成功:IPペイロードのためのメモリブロックを返します。/失敗:NULL + * メモリは、[TEthPacket][payload]の構造で返されます。 + */ +void* NyLPC_cMiMicIpNetIf_allocTxBuf(NyLPC_TUInt16 i_hint,NyLPC_TUInt16* o_size); + +/** + * システム用の小さな送信ペイロードメモリを返します。 + * この関数は、リエントラントを許容します。 + * 返却するメモリブロックのサイズが小さいこと、メモリが確実に返される点がNyLPC_cUipService_allocTxBufと異なります。 + * この関数が返すメモリのサイズは、NyLPC_cUipService_SYS_TX_BUF_SIZEの値です。 + * 関数を使用するコードでは開始時に1度だけNyLPC_cUipService_SYS_TX_BUF_SIZEの値を確認して下さい。 + */ +void* NyLPC_cMiMicIpNetIf_allocSysTxBuf(void); + +/** + * allocTxbufで確保したメモリを開放します。 + */ +void* NyLPC_cMiMicIpNetIf_releaseTxBuf(void* i_buf); + +/** + * 指定したIPアドレスを要求するARPリクエストを発行します。 + */ +void NyLPC_cMiMicIpNetIf_sendArpRequest(const struct NyLPC_TIPv4Addr* i_addr); + +/** + * ARPテーブルに指定したIPがあるかを返します。 + */ +NyLPC_TBool NyLPC_cMiMicIpNetIf_hasArpInfo(const struct NyLPC_TIPv4Addr* i_addr); + + +void NyLPC_cMiMicIpNetIf_releaseTcpSocketMemory(const NyLPC_TcMiMicIpTcpSocket_t* i_inst); +void NyLPC_cMiMicIpNetIf_releaseUdpSocketMemory(const NyLPC_TcMiMicIpUdpSocket_t* i_inst); +void NyLPC_cMiMicIpNetIf_releaseTcpListenerMemory(const NyLPC_TcMiMicIpTcpListener_t* i_inst); + +NyLPC_TcMiMicIpTcpListener_t* NyLPC_cMiMicIpNetIf_getListenerByPeerPort(NyLPC_TUInt16 i_port); +NyLPC_TcMiMicIpUdpSocket_t* NyLPC_cMiMicIpNetIf_getMatchUdpSocket(NyLPC_TUInt16 i_lport); +NyLPC_TBool NyLPC_cMiMicIpNetIf_isClosedTcpPort(NyLPC_TUInt16 i_lport); +NyLPC_TcMiMicIpUdpSocket_t* NyLPC_cMiMicIpNetIf_getMatchMulticastUdpSocket( + const struct NyLPC_TIPv4Addr* i_mcast_ip,NyLPC_TUInt16 i_lport); +NyLPC_TcMiMicIpTcpSocket_t* NyLPC_cMiMicIpNetIf_getMatchTcpSocket( + NyLPC_TUInt16 i_lport,struct NyLPC_TIPv4Addr i_rip,NyLPC_TUInt16 i_rport); +void NyLPC_cMiMicIpNetIf_callPeriodic(void); +void NyLPC_cMiMicIpNetIf_callSocketStart(const NyLPC_TcIPv4Config_t* i_cfg); +void NyLPC_cMiMicIpNetIf_callSocketStop(void); + + + + + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif + diff --git a/lib/src/netif/mimicip/NyLPC_cMiMicIpTcpListener.c b/lib/src/netif/mimicip/NyLPC_cMiMicIpTcpListener.c new file mode 100644 index 0000000..77b5179 --- /dev/null +++ b/lib/src/netif/mimicip/NyLPC_cMiMicIpTcpListener.c @@ -0,0 +1,219 @@ +/********************************************************************************* + * PROJECT: MiMic + * -------------------------------------------------------------------------------- + * + * This file is part of MiMic + * Copyright (C)2011 Ryo Iizuka + * + * MiMic is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + * + * For further information please contact. + * http://nyatla.jp/ + * or + * + *********************************************************************************/ +#include "NyLPC_cMiMicIpTcpListener_protected.h" +#include "NyLPC_cMiMicIpTcpSocket_protected.h" +#include "NyLPC_cMiMicIpNetIf_protected.h" +#include "../NyLPC_iTcpListener.h" +#include "NyLPC_cIPv4.h" +#include "NyLPC_stdlib.h" + +/** + * NyLPC_TTcpListenerListenQ + */ + +void NyLPC_TTcpListenerListenQ_init(struct NyLPC_TTcpListenerListenQ* i_struct) +{ + i_struct->wp=0; + int i; + for(i=NyLPC_TcMiMicIpTcpListener_NUMBER_OF_Q-1;i>=0;i--){ + i_struct->item[i].rport=0; + } +} + +/** + * ListenQへSYNパケットの情報を追加する。 + */ +void NyLPC_TTcpListenerListenQ_add(struct NyLPC_TTcpListenerListenQ* i_struct,const NyLPC_TcIPv4Payload_t* i_payload) +{ + struct NyLPC_TTcpSocketSynParam* item=&i_struct->item[i_struct->wp]; + //未処理のものがあれば登録しない。 + if(item->rport!=0){ + return; + } + + //SYNリングバッファにセット + item->rport = i_payload->payload.tcp->srcport; + item->srcaddr=i_payload->header->srcipaddr; + item->rcv_nxt32=NyLPC_ntohl(i_payload->payload.tcp->seqno32)+1; + //MSSの設定 + if(!NyLPC_TTcpHeader_getTcpMmsOpt(i_payload->payload.tcp,&item->mss)){ + item->mss=0; + } + //書込み位置の進行 + i_struct->wp=(i_struct->wp+1)%NyLPC_TcMiMicIpTcpListener_NUMBER_OF_Q; +} + +/** + * 最も古いSYNパケット情報のインデクスをキューから返す。 + * @return + * 見つからない場合-1である。 + */ +int NyLPC_TTcpListenerListenQ_getLastIndex(struct NyLPC_TTcpListenerListenQ* i_struct) +{ + int i,t; + //古いものから順に返す + for(i=1;i<=NyLPC_TcMiMicIpTcpListener_NUMBER_OF_Q;i++){ + t=(i_struct->wp+i)%NyLPC_TcMiMicIpTcpListener_NUMBER_OF_Q; + //有効なデータ? + if(i_struct->item[t].rport!=0){ + return t; + } + } + return -1; +} + +/** + * ListenQのN番目を削除する。 + */ +void NyLPC_TTcpListenerListenQ_remove(struct NyLPC_TTcpListenerListenQ* i_struct,int i_idx) +{ + i_struct->item[i_idx].rport=0; + return; +} + + + + + + +//#define lockResource(i_inst) NyLPC_cMutex_lock(((i_inst)->_mutex)) +//#define unlockResource(i_inst) NyLPC_cMutex_unlock(((i_inst)->_mutex)) +#define lockResource(i_inst) NyLPC_cMutex_lock(NyLPC_cIPv4_getListenerMutex(((i_inst)->_parent_ipv4))) +#define unlockResource(i_inst) NyLPC_cMutex_unlock(NyLPC_cIPv4_getListenerMutex(((i_inst)->_parent_ipv4))) + + +static NyLPC_TBool listen(NyLPC_TiTcpListener_t* i_inst,NyLPC_TiTcpSocket_t* i_sock,NyLPC_TUInt32 i_wait_msec); +static void finaize(NyLPC_TiTcpListener_t* i_inst); + +const static struct NyLPC_TiTcpListener_Interface interface= +{ + listen, + finaize +}; + +/** + * uipサービスが稼働中にのみ機能します。 + */ +NyLPC_TBool NyLPC_cMiMicIpTcpListener_initialize(NyLPC_TcMiMicIpTcpListener_t* i_inst,NyLPC_TUInt16 i_port) +{ + NyLPC_TcMiMicIpNetIf_t* srv=_NyLPC_TcMiMicIpNetIf_inst; + i_inst->_super._interface=&interface; + i_inst->_parent_ipv4=&srv->_tcpv4; + NyLPC_TTcpListenerListenQ_init(&i_inst->_listen_q); + //uipサービスは初期化済であること。 + NyLPC_Assert(NyLPC_cMiMicIpNetIf_isInitService()); + //初期化 +// // NyLPC_cMutex_initialize(&(i_inst->_mutex)); +// i_inst->_mutex=NyLPC_cIPv4_getListenerMutex(&srv->_tcpv4);// NyLPC_cMutex_initialize(&(i_inst->_mutex)); + i_inst->_port=NyLPC_htons(i_port); + //管理リストへ登録。 + return NyLPC_TBool_TRUE; +} + + + +static void finaize(NyLPC_TiTcpListener_t* i_inst) +{ + NyLPC_Assert(NyLPC_cMiMicIpNetIf_isInitService()); + NyLPC_cMiMicIpNetIf_releaseTcpListenerMemory((NyLPC_TcMiMicIpTcpListener_t*)i_inst); + return; +} + + +static NyLPC_TBool listen(NyLPC_TiTcpListener_t* i_inst,NyLPC_TiTcpSocket_t* i_sock,NyLPC_TUInt32 i_wait_msec) +{ + int qi; + NyLPC_TcMiMicIpTcpListener_t* inst=(NyLPC_TcMiMicIpTcpListener_t*)i_inst; + NyLPC_TcStopwatch_t sw; + NyLPC_TBool ret=NyLPC_TBool_FALSE; + //サービスは稼働中であること。 + NyLPC_Assert(NyLPC_cMiMicIpNetIf_isRun()); + + //入力ソケットはCLOSEDであること。 + if(((NyLPC_TcMiMicIpTcpSocket_t*)i_sock)->tcpstateflags!=UIP_CLOSED){ + return NyLPC_TBool_FALSE; + } + + //ストップウォッチを起動 + NyLPC_cStopwatch_initialize(&sw); + NyLPC_cStopwatch_setNow(&sw); + + + //Listenerのリソースロック + lockResource(inst); + + while(NyLPC_cStopwatch_elapseInMsec(&sw)_listen_q); + if(qi>=0){ + //SYN処理要求がある + if(!NyLPC_cMiMicIpTcpSocket_listenSyn(((NyLPC_TcMiMicIpTcpSocket_t*)i_sock),&inst->_listen_q.item[qi],inst->_port)){ + ret=NyLPC_TBool_FALSE; + }else{ + //成功 + ret=NyLPC_TBool_TRUE; + } + //処理したSYNの削除 + NyLPC_TTcpListenerListenQ_remove(&inst->_listen_q,qi); + break; + }else{ + //SYN処理要求は無い(しばらくまつ) + unlockResource(inst); + NyLPC_cThread_yield(); + lockResource(inst); + } + } + //タイムアウト + unlockResource(inst); + NyLPC_cStopwatch_finalize(&sw); + return ret; +} + +/** + * この関数は、Uip受信タスクから実行します。 + */ +NyLPC_TBool NyLPC_cMiMicIpTcpListener_synPacket(NyLPC_TcMiMicIpTcpListener_t* i_inst,const NyLPC_TcIPv4Payload_t* i_payload) +{ + //パケットチェック。SYN設定されてる? + if(!(i_payload->payload.tcp->flags & TCP_SYN)){ + //SYNない + return NyLPC_TBool_FALSE; + } + //peer port==0は受け取らない。 + if(i_payload->payload.tcp->srcport==0){ + return NyLPC_TBool_FALSE; + } + //Listenerのリソースロック + lockResource(i_inst); + //ListenQへ追加 + NyLPC_TTcpListenerListenQ_add(&(i_inst->_listen_q),i_payload); + //Listenerのリソースアンロック + unlockResource(i_inst); + return NyLPC_TBool_TRUE; +} + + + + diff --git a/lib/src/uip/NyLPC_cTcpListener.h b/lib/src/netif/mimicip/NyLPC_cMiMicIpTcpListener.h similarity index 67% rename from lib/src/uip/NyLPC_cTcpListener.h rename to lib/src/netif/mimicip/NyLPC_cMiMicIpTcpListener.h index 92485e5..437f5f9 100644 --- a/lib/src/uip/NyLPC_cTcpListener.h +++ b/lib/src/netif/mimicip/NyLPC_cMiMicIpTcpListener.h @@ -1,97 +1,98 @@ -/********************************************************************************* - * PROJECT: MiMic - * -------------------------------------------------------------------------------- - * - * This file is part of MiMic - * Copyright (C)2011 Ryo Iizuka - * - * MiMic is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published - * by the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program. If not, see . - * - * For further information please contact. - * http://nyatla.jp/ - * or - * - *********************************************************************************/ -#ifndef NYLPC_CTCPLISTENER_H_ -#define NYLPC_CTCPLISTENER_H_ - - -#include "NyLPC_stdlib.h" -#include "NyLPC_os.h" -#include "NyLPC_cBaseSocket.h" -#include "NyLPC_cTcpSocket.h" -#ifdef __cplusplus -extern "C" { -#endif /* __cplusplus */ - -typedef struct NyLPC_TcTcpListener NyLPC_TcTcpListener_t; - -/********************************************************************** - * - * NyLPC_TTcpListenerListenQ struct - * - **********************************************************************/ - -#define NyLPC_TcTcpListener_NUMBER_OF_Q 10 - -struct NyLPC_TTcpListenerListenQ -{ - struct NyLPC_TTcpSocketSynParam item[NyLPC_TcTcpListener_NUMBER_OF_Q]; - NyLPC_TUInt16 wp; -}; - - -/********************************************************************** - * - * NyLPC_TcTcpListener class - * - **********************************************************************/ - -/** - * TCP listenerクラス型です。 - */ -struct NyLPC_TcTcpListener -{ - NyLPC_TcBaseSocket_t _super; - NyLPC_TUInt16 _port; /**<ネットワークオーダーのポート番号*/ -// /** -// * タスク間の調停用Mutex -// * Listener用の共通Mutexポインタ -// */ -// NyLPC_TcMutex_t* _mutex; - /** - * SYNパケットのキュー - */ - struct NyLPC_TTcpListenerListenQ _listen_q; -}; -/** - * この関数は、TCPのリスナーを初期化します。 - * 初期化したリスナーをサービスに登録することにより、listen関数を使用できるようになります。 - * サービスへの登録は、NyLPC_cUipService_addListenerを使います。 - * @param i_port - * ポート番号。host orderです。 - */ -NyLPC_TBool NyLPC_cTcpListener_initialize(NyLPC_TcTcpListener_t* i_inst,NyLPC_TUInt16 i_port); - -/** - * この関数は、一定時間i_sockに接続を受け付けます。 - */ -NyLPC_TBool NyLPC_cTcpListener_listen(NyLPC_TcTcpListener_t* i_inst,NyLPC_TcTcpSocket_t* i_sock,NyLPC_TUInt32 i_wait_msec); - - -#ifdef __cplusplus -} -#endif /* __cplusplus */ - -#endif /* NYLPC_CTCPLISTENER_H_ */ +/********************************************************************************* + * PROJECT: MiMic + * -------------------------------------------------------------------------------- + * + * This file is part of MiMic + * Copyright (C)2011 Ryo Iizuka + * + * MiMic is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + * + * For further information please contact. + * http://nyatla.jp/ + * or + * + *********************************************************************************/ +#ifndef NYLPC_CTCPLISTENER_H_ +#define NYLPC_CTCPLISTENER_H_ + + +#include "NyLPC_stdlib.h" +#include "NyLPC_os.h" +#include "../NyLPC_iTcpListener.h" +#include "NyLPC_cMiMicIpTcpSocket.h" +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +typedef struct NyLPC_TcMiMicIpTcpListener NyLPC_TcMiMicIpTcpListener_t; + +/********************************************************************** + * + * NyLPC_TTcpListenerListenQ struct + * + **********************************************************************/ + +#define NyLPC_TcMiMicIpTcpListener_NUMBER_OF_Q 10 + +struct NyLPC_TTcpListenerListenQ +{ + struct NyLPC_TTcpSocketSynParam item[NyLPC_TcMiMicIpTcpListener_NUMBER_OF_Q]; + NyLPC_TUInt16 wp; +}; + + +/********************************************************************** + * + * NyLPC_TcTcpListener class + * + **********************************************************************/ + +/** + * TCP listenerクラス型です。 + */ +struct NyLPC_TcMiMicIpTcpListener +{ + struct NyLPC_TiTcpListener _super; + NyLPC_TcIPv4_t* _parent_ipv4; + NyLPC_TUInt16 _port; /**<ネットワークオーダーのポート番号*/ +// /** +// * タスク間の調停用Mutex +// * Listener用の共通Mutexポインタ +// */ +// NyLPC_TcMutex_t* _mutex; + /** + * SYNパケットのキュー + */ + struct NyLPC_TTcpListenerListenQ _listen_q; +}; +/** + * この関数は、TCPのリスナーを初期化します。 + * 初期化したリスナーをサービスに登録することにより、listen関数を使用できるようになります。 + * サービスへの登録は、NyLPC_cUipService_addListenerを使います。 + * @param i_port + * ポート番号。host orderです。 + */ +NyLPC_TBool NyLPC_cMiMicIpTcpListener_initialize(NyLPC_TcMiMicIpTcpListener_t* i_inst,NyLPC_TUInt16 i_port); + +/** + * この関数は、一定時間i_sockに接続を受け付けます。 + */ +NyLPC_TBool NyLPC_cMiMicIpTcpListener_listen(NyLPC_TcMiMicIpTcpListener_t* i_inst,NyLPC_TcMiMicIpTcpSocket_t* i_sock,NyLPC_TUInt32 i_wait_msec); + + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* NYLPC_CTCPLISTENER_H_ */ diff --git a/lib/src/uip/NyLPC_cTcpListener_protected.h b/lib/src/netif/mimicip/NyLPC_cMiMicIpTcpListener_protected.h similarity index 79% rename from lib/src/uip/NyLPC_cTcpListener_protected.h rename to lib/src/netif/mimicip/NyLPC_cMiMicIpTcpListener_protected.h index a7cefa9..f0ef8ea 100644 --- a/lib/src/uip/NyLPC_cTcpListener_protected.h +++ b/lib/src/netif/mimicip/NyLPC_cMiMicIpTcpListener_protected.h @@ -1,48 +1,49 @@ -/********************************************************************************* - * PROJECT: MiMic - * -------------------------------------------------------------------------------- - * - * This file is part of MiMic - * Copyright (C)2011 Ryo Iizuka - * - * MiMic is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published - * by the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program. If not, see . - * - * For further information please contact. - * http://nyatla.jp/ - * or - * - *********************************************************************************/ -#ifndef NYLPC_CTCPLISTENER_PROTECTED_H_ -#define NYLPC_CTCPLISTENER_PROTECTED_H_ -#include "NyLPC_cTcpListener.h" -#include "NyLPC_cUipService_protected.h" - -#ifdef __cplusplus -extern "C" { -#endif /* __cplusplus */ - - - -/** - * ListenerへSYNパケットを通知します。 - * @return - * 常にTRUE - */ -NyLPC_TBool NyLPC_cTcpListener_synPacket(NyLPC_TcTcpListener_t* i_inst,const NyLPC_TcIPv4Payload_t* i_payload); - -#ifdef __cplusplus -} -#endif /* __cplusplus */ - -#endif /* NYLPC_CTCPLISTENER_PROTECTED_H_ */ +/********************************************************************************* + * PROJECT: MiMic + * -------------------------------------------------------------------------------- + * + * This file is part of MiMic + * Copyright (C)2011 Ryo Iizuka + * + * MiMic is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + * + * For further information please contact. + * http://nyatla.jp/ + * or + * + *********************************************************************************/ +#ifndef NYLPC_CTCPLISTENER_PROTECTED_H_ +#define NYLPC_CTCPLISTENER_PROTECTED_H_ +#include "NyLPC_cMiMicIpTcpListener.h" +#include "NyLPC_cMiMicIpNetIf_protected.h" + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + + + +/** + * ListenerへSYNパケットを通知します。 + * @return + * 常にTRUE + */ +NyLPC_TBool NyLPC_cMiMicIpTcpListener_synPacket(NyLPC_TcMiMicIpTcpListener_t* i_inst,const NyLPC_TcIPv4Payload_t* i_payload); +#define NyLPC_cMiMicIpTcpListener_getLocalPort(i) ((i)->_port) + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* NYLPC_CTCPLISTENER_PROTECTED_H_ */ diff --git a/lib/src/netif/mimicip/NyLPC_cMiMicIpTcpSocket.c b/lib/src/netif/mimicip/NyLPC_cMiMicIpTcpSocket.c new file mode 100644 index 0000000..2b2ae8e --- /dev/null +++ b/lib/src/netif/mimicip/NyLPC_cMiMicIpTcpSocket.c @@ -0,0 +1,1542 @@ +/********************************************************************************* + * PROJECT: MiMic + * -------------------------------------------------------------------------------- + * + * This file is part of MiMic + * Copyright (C)2011 Ryo Iizuka + * + * MiMic is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + * + * For further information please contact. + * http://nyatla.jp/ + * or + * + *********************************************************************************/ +#include "NyLPC_cMiMicIpTcpSocket_protected.h" +#include "NyLPC_stdlib.h" +#include "NyLPC_cMiMicIpNetIf_protected.h" + + +static NyLPC_TUInt32 iss32=3939; +#define SIZE_OF_IPv4_TCPIP_HEADER 40 + +/** + * TCPのRTOの最大値。 + * ms単位である。 + * defaultは64SEC + */ +#define UIP_IP_RTO_MAX_RTO 64000 +/** + * TCPのRTOの初期値。 + * ms単位である。 + * 伝送路の特性に合わせて調整すること。 + */ +#define UIP_TCP_RTO_INITIAL 3000 + +/** + * CONNECTION時のRTO + */ +#define UIP_TCP_RTO_CONNECTION_INITIAL 200 + +/** + * 下限値 + */ +#define UIP_TCP_RTO_MINIMUM 100 + + +/** + * for Debug + * RTOの情報をログ領域に取る。 + */ +#ifdef RTO_LOG + NyLPC_TUInt32 rto_log[256]; + int rto_log_st=0; + #define DEBUG_RTO_LOG(i_inst) if(rto_log_st<256){rto_log[rto_log_st++]=i_inst->uip_connr.current_rto32;}; +#else + #define DEBUG_RTO_LOG(i_inst) +#endif + +//#define lockResource(i_inst) NyLPC_cMutex_lock(&((i_inst)->_smutex)) +//#define unlockResource(i_inst) NyLPC_cMutex_unlock(&((i_inst)->_smutex)) +#define lockResource(i_inst) NyLPC_cMutex_lock(NyLPC_cIPv4_getSockMutex(((i_inst)->_parent_ipv4))) +#define unlockResource(i_inst) NyLPC_cMutex_unlock(NyLPC_cIPv4_getSockMutex(((i_inst)->_parent_ipv4))) + +static void sendRst(NyLPC_TcMiMicIpTcpSocket_t* i_inst); + + + + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// +// Packet writer +// +//////////////////////////////////////////////////////////////////////////////////////////////////// + + +/** + * TCPヘッダに値をセットする。checksum,wndは0初期化する。 + */ +static void setTcpTxHeader(struct NyLPC_TTcpHeader* i_struct,NyLPC_TUInt8 i_flag,const struct uip_conn* i_conn) +{ + i_struct->flags = i_flag; + //sorce & destination port + i_struct->srcport = i_conn->lport; + i_struct->destport = i_conn->rport; + //ACK number + i_struct->ackno32 = NyLPC_htonl(i_conn->rcv_nxt32); + //Seq Number + i_struct->seqno32 = NyLPC_htonl(i_conn->snd_nxt32); + //uip_func_tcp_send_noconn(BUF); + i_struct->urgp[0] = i_struct->urgp[1] = 0; + i_struct->tcpchksum= 0; +} + +static void setTxPacket(const NyLPC_TcMiMicIpTcpSocket_t* i_inst,void* i_tx_buf,NyLPC_TUInt8 i_tcpf,const void* i_buf,NyLPC_TUInt16 i_len) +{ + struct NyLPC_TIPv4Header* iph; + struct NyLPC_TTcpHeader* tcph; + NyLPC_TUInt8 iph_word=0x05; + NyLPC_TUInt8 tcph_word=(UIP_TCPH_LEN) / 4; + //IPヘッダの更新 + iph=(struct NyLPC_TIPv4Header*)i_tx_buf; + iph->vhl=0x40|(0x0f&iph_word); + iph->destipaddr=i_inst->uip_connr.ripaddr; + iph->srcipaddr =*(i_inst->uip_connr.lipaddr); + NyLPC_TIPv4Header_writeTxIpHeader(iph,UIP_PROTO_TCP); + //TCPヘッダの更新 + tcph=(struct NyLPC_TTcpHeader*)(((NyLPC_TUInt8*)i_tx_buf)+NyLPC_TIPv4Header_getHeaderLength(iph)); + + + //SYNが有るならMSSの書き込み + if((TCP_SYN & i_tcpf)){ + tcph_word+=((TCP_OPT_MSS_LEN) / 4); + NyLPC_TTcpHeader_setMmsOpt(((NyLPC_TUInt8*)(tcph+1)),i_inst->uip_connr.default_mss); + } + tcph->tcpoffset=(tcph_word<<4); + setTcpTxHeader(tcph,i_tcpf,&(i_inst->uip_connr)); + + //最終的なパケットサイズと必要ならペイロードを書き込み + if(i_buf!=NULL){ + iph->len16=NyLPC_htons(i_len+(iph_word+tcph_word)*4); + memcpy(((NyLPC_TUInt8*)i_tx_buf)+((iph_word+tcph_word)*4),i_buf,i_len); + }else{ + iph->len16=NyLPC_htons((iph_word+tcph_word)*4); + } + //WND設定 + tcph->wnd16=NyLPC_htons(NyLPC_cFifoBuffer_getSpace(&(i_inst->rxbuf))); + //Checksumの生成 + tcph->tcpchksum=~(NyLPC_TIPv4Header_makeTcpChecksum(iph)); + iph->ipchksum = ~(NyLPC_TIPv4Header_makeIpChecksum(iph)); + return; +} + +/** + * IP/TCPヘッダが40バイト固定として、i_tx_buf+40の位置にあるペイロードに対するIP/TCPヘッダを書き込みます。 + */ +static void setTxPacketHeader(const NyLPC_TcMiMicIpTcpSocket_t* i_inst,void* i_tx_buf,NyLPC_TUInt8 i_tcpf,NyLPC_TUInt16 i_len) +{ + struct NyLPC_TIPv4Header* iph; + struct NyLPC_TTcpHeader* tcph; + NyLPC_TUInt8 iph_word=0x05; + NyLPC_TUInt8 tcph_word=(UIP_TCPH_LEN) / 4; + //IPヘッダの更新 + iph=(struct NyLPC_TIPv4Header*)i_tx_buf; + iph->vhl=0x40|(0x0f&iph_word); + iph->destipaddr=i_inst->uip_connr.ripaddr; + iph->srcipaddr =*(i_inst->uip_connr.lipaddr); + NyLPC_TIPv4Header_writeTxIpHeader(iph,UIP_PROTO_TCP); + + //TCPヘッダの更新 + tcph=(struct NyLPC_TTcpHeader*)(((NyLPC_TUInt8*)i_tx_buf)+NyLPC_TIPv4Header_getHeaderLength(iph)); + tcph->tcpoffset=(tcph_word<<4); + setTcpTxHeader(tcph,i_tcpf,&(i_inst->uip_connr)); + + //最終的なパケットサイズと必要ならペイロードを書き込み + iph->len16=NyLPC_htons(i_len+(iph_word+tcph_word)*4); + //WND設定 + tcph->wnd16=NyLPC_htons(NyLPC_cFifoBuffer_getSpace(&(i_inst->rxbuf))); + //Checksumの生成 + tcph->tcpchksum=~(NyLPC_TIPv4Header_makeTcpChecksum(iph)); + iph->ipchksum = ~(NyLPC_TIPv4Header_makeIpChecksum(iph)); + return; +} + + + + + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// +// Mainclass::private +// +//////////////////////////////////////////////////////////////////////////////////////////////////// + +/** + * ACK番号を更新する。 + * @param i_ackno + * ネットワークオーダーのACK番号 + */ +static void updateAckNo(void* i_tx_buf,NyLPC_TUInt32 i_ackno) +{ + struct NyLPC_TIPv4Header* iph=(struct NyLPC_TIPv4Header*)i_tx_buf; + struct NyLPC_TTcpHeader* tcph=(struct NyLPC_TTcpHeader*)(((NyLPC_TUInt8*)i_tx_buf)+NyLPC_TIPv4Header_getHeaderLength(iph)); + +/* union{ + NyLPC_TUInt32 l; + NyLPC_TUInt8 b[4]; + }old_ack,new_ack; + NyLPC_TUInt16 v1; + //checksumの計算 + old_ack.l=i_inst->payload.tcp->ackno32;//古いACK番号 + new_ack.l=i_ackno;//新しいACK番号 + v1=NyLPC_ntohs(~(i_inst->payload.tcp->tcpchksum));//1の補数を取って、ホストオーダーに戻す。 + //減算 + v1=sub16c(v1,(old_ack.b[0]<<8)+old_ack.b[1]); + v1=sub16c(v1,(old_ack.b[2]<<8)+old_ack.b[3]); + //加算 + v1=add16c(v1,(new_ack.b[0]<<8)+new_ack.b[1]); + v1=add16c(v1,(new_ack.b[2]<<8)+new_ack.b[3]); + v1=~NyLPC_htons(v1);*/ +NyLPC_Trace(); + tcph->ackno32=i_ackno; +NyLPC_Trace(); + tcph->tcpchksum = 0; +NyLPC_Trace(); + tcph->tcpchksum = ~(NyLPC_TIPv4Header_makeTcpChecksum(iph)); +NyLPC_Trace(); + +/* + if((i_inst->payload.tcp->tcpchksum!=v1)){ + NyLPC_Warning(); + }*/ +} + + + +/** + * 指定した送信パケットがACK済であるか調べる。 + */ +static NyLPC_TBool isPacketAcked(NyLPC_TcMiMicIpTcpSocket_t* i_inst,NyLPC_TUInt32 i_sq) +{ + int rp; + struct NyLPC_TcTcpSocket_TxQItem* q=i_inst->txbuf.txq; + rp=i_inst->txbuf.rp; + while(rp!=i_inst->txbuf.wp){ + if(q[rp].ackno==i_sq){ + return NyLPC_TBool_FALSE; + } + rp=(rp+1)%NyLPC_TcTcpSocket_NUMBER_OF_TXQ; + } + return NyLPC_TBool_TRUE; +} +/** + * 送信キューからi_sq以前に送信したパケットを除外して、残り個数を返却する。 + */ +static int getNumOfSending(NyLPC_TcMiMicIpTcpSocket_t* i_inst,NyLPC_TUInt32 i_sq) +{ + int rp,n; + struct NyLPC_TcTcpSocket_TxQItem* q=i_inst->txbuf.txq; + rp=i_inst->txbuf.rp; + n=0; + while(rp!=i_inst->txbuf.wp){ + if(q[rp].ackno==i_sq){ + return n; + } + n++; + rp=(rp+1)%NyLPC_TcTcpSocket_NUMBER_OF_TXQ; + } + return n; +} +/** + * この関数は、コネクションをリセットします。 + * ロック状態でコールしてください。 + * 関数は、現在バッファにある再送信待ちデータを開放します。 + */ +static void resetTxQWithUnlock(NyLPC_TcMiMicIpTcpSocket_t* i_inst) +{ + int i,l; + struct NyLPC_TcTcpSocket_TxQItem* q=i_inst->txbuf.txq; + void* dlist[NyLPC_TcTcpSocket_NUMBER_OF_TXQ]; + + l=0; + while(i_inst->txbuf.rp!=i_inst->txbuf.wp){ + dlist[l]=q[i_inst->txbuf.rp].packet; + l++; + i_inst->txbuf.rp=(i_inst->txbuf.rp+1)%NyLPC_TcTcpSocket_NUMBER_OF_TXQ; + } + i_inst->txbuf.rp=i_inst->txbuf.wp=0; + //ロック解除 + unlockResource(i_inst); + //セーブしたバッファを開放 + for(i=0;itxbuf.txq; + NyLPC_ArgAssert(i_inst!=NULL); + rp=i_inst->txbuf.rp; + while(rp!=i_inst->txbuf.wp){ + updateAckNo(q[rp].packet,i_ackno); + rp=(rp+1)%NyLPC_TcTcpSocket_NUMBER_OF_TXQ; + } +} + +/** + * RTOの予測関数 + */ +static void estimateRTO(NyLPC_TcMiMicIpTcpSocket_t* i_inst,int s,int n) +{ + NyLPC_TcStopwatch_t sw; + NyLPC_TUInt32 cr_rtt_min,cr_rtt_max,sk_rto,new_rto,w; + int i; + struct NyLPC_TcTcpSocket_TxQItem* q=i_inst->txbuf.txq; + NyLPC_cStopwatch_initialize(&sw); + + sk_rto=i_inst->uip_connr.current_rto32; + //ACKされたパケットの個数は? + switch(n){ + case 1: + NyLPC_cStopwatch_set(&sw,q[s].tick_of_sent); + cr_rtt_min=NyLPC_cStopwatch_elapseInMsec(&sw); + if(sk_rtow){ + cr_rtt_min=w; + } + if(cr_rtt_maxuip_connr.current_rto32=new_rto; +} + +/** + * TXキューから、入力されたシーケンス番号より前のパケットを除外します。 + * リングバッファのrp->wp-1までをチェックして、sqに等しいi_sq以前のパケットバッファをo_dlistへ返します。 + * + */ +static int updateTxQByIndex(NyLPC_TcMiMicIpTcpSocket_t* i_inst,NyLPC_TUInt32 i_sq,void* o_dlist[]) +{ + int rp,n; + struct NyLPC_TcTcpSocket_TxQItem* q=i_inst->txbuf.txq; + //ロック状態なう + rp=i_inst->txbuf.rp; + n=0; + //This is debug + DEBUG_RTO_LOG(i_inst); + + while(rp!=i_inst->txbuf.wp){ + o_dlist[n]=q[rp].packet; + if(q[rp].ackno==i_sq){ + //i_inst->txbuf.rp->rpのパケットのRTOからbaseRTOの値を再計算。 + estimateRTO(i_inst,i_inst->txbuf.rp,n+1); + i_inst->txbuf.rp=(rp+1)%NyLPC_TcTcpSocket_NUMBER_OF_TXQ; + return n+1; + } + n++; + rp=(rp+1)%NyLPC_TcTcpSocket_NUMBER_OF_TXQ; + } + return 0; +} + + + +/** + * 空きキューを1個返します。 + * 空きキューの + */ +static struct NyLPC_TcTcpSocket_TxQItem* getTxQ(NyLPC_TcMiMicIpTcpSocket_t* i_inst,NyLPC_TcStopwatch_t* i_timer) +{ + int i; + struct NyLPC_TcTcpSocket_TxQItem* q=i_inst->txbuf.txq; + do{ + //クローズドに遷移してしまったら、エラーである。 + if(i_inst->tcpstateflags==UIP_CLOSED){ + return NULL; + } + //キューの空きをチェック。wp+1==rpなら、キューがいっぱい。rp==wpなら、キューが空。 + if(((i_inst->txbuf.wp+1)%NyLPC_TcTcpSocket_NUMBER_OF_TXQ)==i_inst->txbuf.rp){ + //一時的なアンロック + unlockResource(i_inst); + //タスクスイッチ + NyLPC_cThread_yield(); + //ロック + lockResource(i_inst); + continue; + } + i=i_inst->txbuf.wp; + i_inst->txbuf.wp=(i+1)%NyLPC_TcTcpSocket_NUMBER_OF_TXQ; + return &(q[i]); + }while(!NyLPC_cStopwatch_isExpired(i_timer)); + //失敗。タイムアウト。 + return NULL; +} + + + + + + +/********************************************************************** + * public 関数 + **********************************************************************/ +static const struct NyLPC_TIPv4Addr* getPeerAddr(const NyLPC_TiTcpSocket_t* i_inst); +static NyLPC_TUInt16 getPeerPort(const NyLPC_TiTcpSocket_t* i_inst); +static NyLPC_TBool accept(NyLPC_TiTcpSocket_t* i_inst,NyLPC_TUInt32 i_wait_in_msec); +static NyLPC_TInt32 precv(NyLPC_TiTcpSocket_t* i_inst,const void** o_buf_ptr,NyLPC_TUInt32 i_wait_msec); +static void pseek(NyLPC_TiTcpSocket_t* i_inst,NyLPC_TUInt16 i_seek); +static NyLPC_TInt32 send(NyLPC_TiTcpSocket_t* i_inst,const void* i_buf_ptr,NyLPC_TInt32 i_len,NyLPC_TUInt32 i_wait_in_msec); +static void close(NyLPC_TiTcpSocket_t* i_inst,NyLPC_TUInt32 i_wait_in_msec); +static void* allocSendBuf(NyLPC_TiTcpSocket_t* i_inst,NyLPC_TUInt16 i_hint,NyLPC_TUInt16* o_buf_size,NyLPC_TUInt32 i_wait_in_msec); +static void releaseSendBuf(NyLPC_TiTcpSocket_t* i_inst,void* i_buf_ptr); +static NyLPC_TBool psend(NyLPC_TiTcpSocket_t* i_inst,void* i_buf_ptr,int i_len,NyLPC_TUInt32 i_wait_in_msec); +static NyLPC_TBool connect(NyLPC_TiTcpSocket_t* i_inst,const struct NyLPC_TIPv4Addr* i_addr,NyLPC_TUInt16 i_peer_port,NyLPC_TUInt32 i_wait_in_msec); +static void finalize(NyLPC_TiTcpSocket_t* i_inst); + +const static struct NyLPC_TiTcpSocket_Interface _interface= +{ + getPeerAddr, + getPeerPort, + accept, + precv, + pseek, + send, + close, + allocSendBuf, + releaseSendBuf, + psend, + connect, + finalize +}; + +static const struct NyLPC_TIPv4Addr* getPeerAddr(const NyLPC_TiTcpSocket_t* i_inst) +{ + const NyLPC_TcMiMicIpTcpSocket_t* inst=(const NyLPC_TcMiMicIpTcpSocket_t*)i_inst; + return &inst->uip_connr.ripaddr; +} +static NyLPC_TUInt16 getPeerPort(const NyLPC_TiTcpSocket_t* i_inst) +{ + const NyLPC_TcMiMicIpTcpSocket_t* inst=(const NyLPC_TcMiMicIpTcpSocket_t*)i_inst; + return inst->uip_connr.rport; +} + + + + +NyLPC_TBool NyLPC_cMiMicIpTcpSocket_initialize(NyLPC_TcMiMicIpTcpSocket_t* i_inst,void* i_rbuf,NyLPC_TUInt16 i_rbuf_len) +{ + int i; + NyLPC_TcMiMicIpNetIf_t* srv=_NyLPC_TcMiMicIpNetIf_inst; + i_inst->_super._interface=&_interface; + i_inst->_parent_ipv4=&srv->_tcpv4; + + //uipサービスは初期化済であること。 + NyLPC_Assert(NyLPC_cMiMicIpNetIf_isInitService()); + + NyLPC_cFifoBuffer_initialize(&(i_inst->rxbuf),i_rbuf,i_rbuf_len); + // NyLPC_AbortIfNot(NyLPC_cMutex_initialize(&(i_inst->_smutex)));//個別Mutex +// i_inst->_smutex=NyLPC_cIPv4_getSockMutex(&(srv->_tcpv4));//共有Mutex + i_inst->tcpstateflags=UIP_CLOSED; + i_inst->txbuf.rp=i_inst->txbuf.wp=0; + for(i=0;itxbuf.txq[i].packet=NULL; + } + //管理リストへ登録。 + return NyLPC_TBool_TRUE; +} + + + +NyLPC_TBool NyLPC_cMiMicIpTcpSocket_listenSyn(NyLPC_TcMiMicIpTcpSocket_t* i_inst,const struct NyLPC_TTcpSocketSynParam* i_lq,NyLPC_TUInt16 i_lport) +{ +// NyLPC_Assert(NyLPC_cMutex_isLocked(i_inst->_smutex)); + lockResource(i_inst); + //ソケットが無効であること。 + if(i_inst->tcpstateflags==UIP_CLOSED) + { + //localipとdefault_mmsは別枠で設定 + /* Fill in the necessary fields for the new connection. */ + i_inst->uip_connr.current_rto32 = UIP_TCP_RTO_INITIAL; + i_inst->uip_connr.lport = i_lport; + i_inst->uip_connr.rport = i_lq->rport; + i_inst->uip_connr.ripaddr=i_lq->srcaddr; + i_inst->uip_connr.snd_nxt32=iss32; + /* rcv_nxt should be the seqno from the incoming packet + 1. */ + i_inst->uip_connr.rcv_nxt32= i_lq->rcv_nxt32; + //MSSの設定 + i_inst->uip_connr.peer_mss=(i_lq->mss!=0)?i_lq->mss:i_inst->uip_connr.default_mss; + i_inst->uip_connr.peer_win=0; + NyLPC_cFifoBuffer_clear(&(i_inst->rxbuf)); + //ここでステータスがかわる。 + i_inst->tcpstateflags = UIP_SYN_RCVD; + //前回のデータが残っていた場合の保険 + if(i_inst->txbuf.rp!=i_inst->txbuf.wp){ + resetTxQWithUnlock(i_inst); + }else{ + unlockResource(i_inst); + } + return NyLPC_TBool_TRUE; + } + unlockResource(i_inst); + return NyLPC_TBool_FALSE; +} + + +/** + * sq番のTxがキューから消え去るのを待ちます。 + * この関数は、アンロック状態でコールしてください。 + *
+ * パケットがキューからなくなる条件は、以下の2つです。 + *
    + *
  • ACKを受信してパケットキューが更新された。
  • + *
  • RSTを受信して(CLOSEDに遷移して)、キューがクリアされた。
  • + *
  • 送信タイムアウトで関数が(CLOSEDに遷移させて)キューをクリアした。
  • + *
+ *
+ * @param i_wait_msec + * @return + * 1番目の条件でパケットが消失したときのみ、TRUEを返します。 + * 失敗した場合、TCPステータスがCLOSEDでなければ、RSTを送信してステータスをCLOSEDにします。 + */ +static NyLPC_TBool waitForTxRemove(NyLPC_TcMiMicIpTcpSocket_t* i_inst,NyLPC_TUInt32 i_sq,NyLPC_TcStopwatch_t* i_timer) +{ + NyLPC_TUInt8 f; + lockResource(i_inst); + do{ + //パケットが送信中か調べる。 + if(!isPacketAcked(i_inst,i_sq)){ + //まだある場合は、タスクスイッチを繰り返して消失を待つ。 + unlockResource(i_inst); + NyLPC_cThread_yield(); + lockResource(i_inst); + continue; + } + //なくなった場合は、原因を調べる。 + f=i_inst->tcpstateflags; + unlockResource(i_inst); + return (f==UIP_CLOSED)?NyLPC_TBool_FALSE:NyLPC_TBool_TRUE; + }while(!NyLPC_cStopwatch_isExpired(i_timer)); + unlockResource(i_inst); + return NyLPC_TBool_FALSE; +} + + +/** + * 再送信処理をセットして、パケットを送信します。 + * この関数は「アンロック状態で」実行してください。 + * @param i_len + * 送信データサイズを指定します。 + * この番号は、シーケンス番号の加算値ではありませんので、注意をしてください。 + * @return + *
    + *
  • n=-1:送信キューへの投入に失敗した。
  • + *
  • n>=0:nバイトのデータを送信キューへの投入することに成功した。
  • + *
+ * 送信キューに失敗する理由は2つあります。1つは、TXバッファがフルでタイムアウト。もうひとつは、非同期なコネクリョンのリセットです。 + * 失敗した場合、TCPステータスがCLOSEDでなければ、RSTを送信してステータスをCLOSEDにします。 + */ +static NyLPC_TInt32 sendWithRetransmit(NyLPC_TcMiMicIpTcpSocket_t* i_inst,NyLPC_TUInt8 i_tcpf,const void* i_buf,NyLPC_TUInt16 i_len,NyLPC_TcStopwatch_t* i_timer,NyLPC_TUInt32* o_ack) +{ + struct NyLPC_TcTcpSocket_TxQItem* txq; + NyLPC_TUInt16 s; + void* buf; + NyLPC_TUInt32 next_ack; + //送信バッファを取得 + //@bug オブションパケット送信時に4バイト足りないメモリ要求しない?問題になってないけど。 + for(;;){ + buf=NyLPC_cMiMicIpNetIf_allocTxBuf(i_len+(SIZE_OF_IPv4_TCPIP_HEADER),&s); + if(buf!=NULL){ + break; + } + //タイムアウト確認 + if(NyLPC_cStopwatch_isExpired(i_timer)){ + return -1; + } + }; + lockResource(i_inst); + //ペイロードがある場合のみ、相手のwindowサイズが0以上になるのを待つ。 + if(i_len>0){ + while(i_inst->uip_connr.peer_win==0){ + unlockResource(i_inst); + //時間切れならエラー。 + if(NyLPC_cStopwatch_isExpired(i_timer)){ + return -1; + } + NyLPC_cThread_yield(); + lockResource(i_inst); + } + } + //送信キューの取得 + txq=getTxQ(i_inst,i_timer); + //送信キューが取れなかった。 + if(txq==NULL){ + //シーケンス番号をロールバックできないので、エラーとする。 + unlockResource(i_inst); + NyLPC_cMiMicIpNetIf_releaseTxBuf(buf); + return -1; + } + + //送信バッファを基準とした送信サイズを計算 + s-=SIZE_OF_IPv4_TCPIP_HEADER; + //送信サイズよりMMSが小さければ、送信サイズを修正 + if(i_inst->uip_connr.peer_mssuip_connr.peer_mss; + } + //送信サイズよりpeerのウインドウサイズが小さければ修正 + if(i_inst->uip_connr.peer_winuip_connr.peer_win; + } + //送信サイズより、データサイズが小さければ、送信サイズを修正 + if(i_lenuip_connr.snd_nxt32+s+(((i_tcpf&(TCP_FIN|TCP_SYN))!=0x00)?1:0); + txq->rto32=i_inst->uip_connr.current_rto32; + txq->tick_of_sent=NyLPC_cStopwatch_now(); + + //パケットの書き込み + setTxPacket(i_inst,buf,i_tcpf,i_buf,s); + txq->packet=buf; + + //シーケンス番号の更新 + i_inst->uip_connr.snd_nxt32=next_ack; + //Peerのウインドウサイズを更新 + i_inst->uip_connr.peer_win-=s; + //ACK番号の返却 + *o_ack=txq->ackno=NyLPC_HTONL(next_ack); + unlockResource(i_inst); + NyLPC_cMiMicIpNetIf_sendIPv4Tx(buf); + return s; +} +/** + * RSTを1フレームだけ送信します。 + * この関数は、クローズドステータスのソケットにしてからコールします。 + * この関数は、アンロック状態でコールしてね。 + */ +static void sendRst(NyLPC_TcMiMicIpTcpSocket_t* i_inst) +{ + void* buf; + + NyLPC_Assert(i_inst->tcpstateflags==UIP_CLOSED); + //ペイロードライタの初期化 + + //@bug バッファが取れるまで通信がブロックするの。ここはなんとかしないと。 + buf=NyLPC_cMiMicIpNetIf_allocSysTxBuf(); + lockResource(i_inst); + i_inst->uip_connr.snd_nxt32++; + unlockResource(i_inst); + setTxPacket(i_inst,buf,TCP_RST|TCP_ACK,NULL,0); + NyLPC_cMiMicIpNetIf_sendIPv4Tx(buf); + NyLPC_cMiMicIpNetIf_releaseTxBuf(buf); + NyLPC_cIPv4Payload_finalize(&ipv4); + return; +} + + + +/** + * 受信データをバッファに書き込む。 + * 十分な空き領域がない場合、失敗する。 + * この関数は、ロックして実行してください。 + */ +static NyLPC_TBool addRecvData(NyLPC_TcMiMicIpTcpSocket_t* i_inst,const void* i_data,NyLPC_TUInt16 i_data_size) +{ + //受信データサイズを確認 + if(NyLPC_cFifoBuffer_getSpace(&(i_inst->rxbuf))>=i_data_size){ + //バッファに格納可能なら、格納。 + NyLPC_cFifoBuffer_push(&(i_inst->rxbuf),i_data,i_data_size); + }else{ + //エラー:ドロップする。 + return NyLPC_TBool_FALSE; + } + + return NyLPC_TBool_TRUE; +} + + + + + +/** + * Public function + */ +static void finalize(NyLPC_TiTcpSocket_t* i_inst) +{ + int i; + NyLPC_TcMiMicIpTcpSocket_t* inst=(NyLPC_TcMiMicIpTcpSocket_t*)i_inst; + NyLPC_Assert(NyLPC_cMiMicIpNetIf_isInitService()); + //開放漏れの保険 + if(inst->txbuf.rp!=inst->txbuf.wp){ + lockResource(inst); + resetTxQWithUnlock(inst); + } + for(i=0;itxbuf.txq[i].packet=NULL; + } + NyLPC_cFifoBuffer_finalize(&(inst->rxbuf)); +// NyLPC_cMutex_finalize(&(i_inst->_smutex)); + NyLPC_cMiMicIpNetIf_releaseTcpSocketMemory(inst); + + return; +} + +static NyLPC_TBool connect(NyLPC_TiTcpSocket_t* i_inst,const struct NyLPC_TIPv4Addr* i_addr,NyLPC_TUInt16 i_peer_port,NyLPC_TUInt32 i_wait_in_msec) +{ + volatile NyLPC_TUInt8 f; + NyLPC_TcMiMicIpTcpSocket_t* inst=(NyLPC_TcMiMicIpTcpSocket_t*)i_inst; + NyLPC_TUInt32 sq; + NyLPC_TcStopwatch_t sw; + NyLPC_TUInt16 lport; + lockResource(inst); + //ソケットが無効であること。 + if(inst->tcpstateflags!=UIP_CLOSED) + { + NyLPC_OnErrorGoto(Error); + } + //ポート番号の取得(lockResourceが他のソケットと共有なので、重複ポートの割当は起こりえない。でもちょっと注意して) + lport=NyLPC_htons(NyLPC_cIPv4_getNewPortNumber(inst->_parent_ipv4)); + if(lport==0){ + NyLPC_OnErrorGoto(Error); + } + //connectの為の準備 + + //localipとdefault_mmsは別枠で設定 + /* Fill in the necessary fields for the new connection. */ + inst->uip_connr.current_rto32 = UIP_TCP_RTO_CONNECTION_INITIAL;//RTOを短くしてARP発行時の再接続短縮を期待する。 + inst->uip_connr.lport = lport; + inst->uip_connr.rport = NyLPC_htons(i_peer_port); + inst->uip_connr.ripaddr=*i_addr; + inst->uip_connr.snd_nxt32=iss32;//should be random + /* rcv_nxt should be the seqno from the incoming packet + 1. */ + inst->uip_connr.rcv_nxt32=0; + //MSSの設定 + inst->uip_connr.peer_mss=inst->uip_connr.default_mss; + inst->uip_connr.peer_win=1;//periodicの再送信を期待するために相手のWindowサイズは1と仮定する。 + NyLPC_cFifoBuffer_clear(&(inst->rxbuf)); + //ここでステータスがかわる。 + inst->tcpstateflags = UIP_SYN_SENT; + //前回のデータが残っていた場合の保険 + if(inst->txbuf.rp!=inst->txbuf.wp){ + resetTxQWithUnlock(inst); + }else{ + unlockResource(inst); + } + + NyLPC_cStopwatch_initialize(&sw); + + NyLPC_cStopwatch_startExpire(&sw,i_wait_in_msec); + if(sendWithRetransmit(inst,TCP_SYN,NULL,0,&sw,&sq)==0){ + //ちょっと待つ。 + NyLPC_cThread_yield(); + //キューにあるTXが消えるのを待つ。 + if(waitForTxRemove(inst,sq,&sw)){ + //ACK受信に成功して、TXが消失 + NyLPC_cStopwatch_finalize(&sw); + return NyLPC_TBool_TRUE; + } + } + //ロックして、強制的なステータス遷移 + lockResource(inst); + f=inst->tcpstateflags; + if(f!=UIP_CLOSED){ + //もし、強制CLOSE遷移であれば、RSTも送信。 + inst->tcpstateflags=UIP_CLOSED; + unlockResource(inst); + sendRst(inst); + }else{ + unlockResource(inst); + } + return NyLPC_TBool_FALSE; +Error: + unlockResource(inst); + return NyLPC_TBool_FALSE; +} + +/** + * この関数は、UIP_SYN_RCVDステータスのソケットを、ESTABLISHEDへ遷移させます。 + * cTcpListener_listen関数を通過したインスタンスに実行してください。 + * この関数は、アプリケーションが呼び出します。 + * @return + * + */ +static NyLPC_TBool accept(NyLPC_TiTcpSocket_t* i_inst,NyLPC_TUInt32 i_wait_in_msec) +{ + volatile NyLPC_TUInt8 f; + NyLPC_TcMiMicIpTcpSocket_t* inst=(NyLPC_TcMiMicIpTcpSocket_t*)i_inst; + NyLPC_TUInt32 sq; + NyLPC_TcStopwatch_t sw; + + NyLPC_cStopwatch_initialize(&sw); + //ステータスチェック + f=inst->tcpstateflags; + switch(f) + { + case UIP_ESTABLISHED: + return NyLPC_TBool_TRUE; + case UIP_SYN_RCVD: + //処理対象 + break; + default: + return NyLPC_TBool_FALSE; + } + NyLPC_cStopwatch_startExpire(&sw,i_wait_in_msec); + if(sendWithRetransmit(inst,TCP_SYN|TCP_ACK,NULL,0,&sw,&sq)==0){ + //ちょっと待つ。 + NyLPC_cThread_yield(); + //キューにあるTXが消えるのを待つ。 + if(waitForTxRemove(inst,sq,&sw)){ + //ACK受信に成功して、TXが消失 + NyLPC_cStopwatch_finalize(&sw); + return NyLPC_TBool_TRUE; + } + } + //ロックして、強制的なステータス遷移 + lockResource(inst); + f=inst->tcpstateflags; + if(f!=UIP_CLOSED){ + //もし、強制CLOSE遷移であれば、RSTも送信。 + inst->tcpstateflags=UIP_CLOSED; + unlockResource(inst); + sendRst(inst); + }else{ + unlockResource(inst); + } + return NyLPC_TBool_FALSE; +} + + +/** + * この関数は、ソケットの受信バッファの読み取り位置と、読み出せるデータサイズを返却します。 + * 関数はポインターを返却するだけで、バッファの読み取り位置をシークしません。 + * シークするにはNyLPC_cTcpSocket_pseekを使います。 + */ +static NyLPC_TInt32 precv(NyLPC_TiTcpSocket_t* i_inst,const void** o_buf_ptr,NyLPC_TUInt32 i_wait_msec) +{ + NyLPC_TcMiMicIpTcpSocket_t* inst=(NyLPC_TcMiMicIpTcpSocket_t*)i_inst; + volatile NyLPC_TUInt8 st; + NyLPC_TUInt16 rlen; + //タイマを生成 + NyLPC_TcStopwatch_t sw; + NyLPC_cStopwatch_initialize(&sw); + + //ESTABLISHED以外の場合は、エラー。 + NyLPC_cStopwatch_setNow(&sw); + do{ + //読み出しバッファ情報のコピー + //MUTEX LOCK + lockResource(inst); + st=inst->tcpstateflags; + rlen=NyLPC_cFifoBuffer_getLength(&(inst->rxbuf)); + *o_buf_ptr=NyLPC_cFifoBuffer_getPtr(&(inst->rxbuf)); + //MUTEX UNLOCK + unlockResource(inst); + + //バッファが空の場合は、ステータスチェック。ESTABLISHEDでなければ、エラー(PASVCLOSE等の場合) + switch(st){ + case UIP_ESTABLISHED: + if(rlen>0){ + //バッファにパケットがあれば返却 + NyLPC_cStopwatch_finalize(&sw); + return rlen; + } + break; + case UIP_CLOSE_WAIT: + if(rlen>0){ + //バッファにパケットがあれば返却 + NyLPC_cStopwatch_finalize(&sw); + return rlen; + } + //引き続きエラー処理 + default: + //他の場合はエラー + NyLPC_cStopwatch_finalize(&sw); + return -1; + } + //タスクスイッチ + NyLPC_cThread_yield(); + }while(NyLPC_cStopwatch_elapseInMsec(&sw)rxbuf))); + if(i_seek==0){ + return; + } + + //ACK送信バッファの取得 + buf=NyLPC_cMiMicIpNetIf_allocSysTxBuf(); + + //MUTEX LOCK + lockResource(inst); + + //受信バッファを読み出しシーク + NyLPC_cFifoBuffer_pop(&(inst->rxbuf),i_seek); + //ACKパケットの生成 + setTxPacket(inst,buf,TCP_ACK,NULL,0); + unlockResource(inst); + //ACK送信 + NyLPC_cMiMicIpNetIf_sendIPv4Tx(buf); + NyLPC_cMiMicIpNetIf_releaseTxBuf(buf); + +} + +/** + * See header file. + */ +static void* allocSendBuf(NyLPC_TiTcpSocket_t* i_inst,NyLPC_TUInt16 i_hint,NyLPC_TUInt16* o_buf_size,NyLPC_TUInt32 i_wait_in_msec) +{ + NyLPC_TcMiMicIpTcpSocket_t* inst=(NyLPC_TcMiMicIpTcpSocket_t*)i_inst; + + NyLPC_TUInt16 s; + void* buf; + NyLPC_TcStopwatch_t sw; + + NyLPC_cStopwatch_initialize(&sw); + NyLPC_cStopwatch_startExpire(&sw,i_wait_in_msec); + + //送信バッファを取得 + //@bug バッファが取れるまで通信がブロックするの。ここはなんとかしないと。 + for(;;){ + //ESTABLISHED以外に非同期遷移 + if(inst->tcpstateflags!=UIP_ESTABLISHED){ + NyLPC_cStopwatch_finalize(&sw); + return NULL; + } + buf=NyLPC_cMiMicIpNetIf_allocTxBuf(i_hint+(SIZE_OF_IPv4_TCPIP_HEADER),&s); + if(buf!=NULL){ + break; + } + //タイムアウト時もエラー + if(NyLPC_cStopwatch_isExpired(&sw)){ + NyLPC_cStopwatch_finalize(&sw); + return NULL; + } + } + +//@todo 前段処理と順番を入れ替えて、要求サイズとpeerのwinのうち、小さいほうを割り当てたほうが良くない? +//ここで相手のwin待ちをする理由は、相手に確実に受け取れるサイズを決定する為。 + lockResource(inst); + //ペイロードがある場合のみ、相手のwindowサイズが0以上になるのを待つ。 + while(inst->uip_connr.peer_win==0){ + unlockResource(inst); + //ESTABLISHED以外に非同期遷移 orタイムアウト確認 + if(NyLPC_cStopwatch_isExpired(&sw)||(inst->tcpstateflags!=UIP_ESTABLISHED)){ + NyLPC_cMiMicIpNetIf_releaseTxBuf(buf); + NyLPC_cStopwatch_finalize(&sw); + return NULL; + } + NyLPC_cThread_yield(); + lockResource(inst); + } + //送信バッファを基準とした送信サイズを計算 + s-=SIZE_OF_IPv4_TCPIP_HEADER; + //送信サイズよりMMSが小さければ、送信サイズを修正 + if(inst->uip_connr.peer_mssuip_connr.peer_mss; + } + //送信サイズよりpeerのウインドウサイズが小さければ修正 + if(inst->uip_connr.peer_winuip_connr.peer_win; + } + unlockResource(inst); + //バッファサイズ確定。 + *o_buf_size=s; + NyLPC_cStopwatch_finalize(&sw); + return (NyLPC_TUInt8*)buf+SIZE_OF_IPv4_TCPIP_HEADER; +} +/** + * See Header file. + */ +static void releaseSendBuf(NyLPC_TiTcpSocket_t* i_inst,void* i_buf_ptr) +{ + NyLPC_cMiMicIpNetIf_releaseTxBuf((NyLPC_TUInt8*)i_buf_ptr-SIZE_OF_IPv4_TCPIP_HEADER); +} + + +/** + * 事前にAllocしたTxパケットを送信します。 + * このAPIはゼロコピー送信をサポートするためのものです。 + * @param i_buf_ptr + * allocSendBufで取得したメモリを指定します。 + * @return + * 関数が失敗した場合、i_buf_ptrは「開放されません。」 + */ +static NyLPC_TBool psend(NyLPC_TiTcpSocket_t* i_inst,void* i_buf_ptr,int i_len,NyLPC_TUInt32 i_wait_in_msec) +{ + NyLPC_TcMiMicIpTcpSocket_t* inst=(NyLPC_TcMiMicIpTcpSocket_t*)i_inst; + struct NyLPC_TcTcpSocket_TxQItem* txq; + void* buf; + NyLPC_TcStopwatch_t sw; + //ESTABLISHEDでなければエラー + if(inst->tcpstateflags!=UIP_ESTABLISHED){ + //ESTABLISHEDでなければエラー + return NyLPC_TBool_FALSE; + } + //送信データ0なら何もしない。 + if(i_len<1){ + releaseSendBuf(i_inst,i_buf_ptr); + return NyLPC_TBool_TRUE; + } + NyLPC_cStopwatch_initialize(&sw); + NyLPC_cStopwatch_startExpire(&sw,i_wait_in_msec); + + //先頭ポインタは、i_buf-sizeof(SIZE_OF_IPv4_TCPIP_HEADER)固定 + buf=(NyLPC_TUInt8*)i_buf_ptr-SIZE_OF_IPv4_TCPIP_HEADER; + lockResource(inst); + //送信キューの取得 + txq=getTxQ(inst,&sw); + //送信キューが取れなかった。 + if(txq==NULL){ + //シーケンス番号をロールバックできないので、エラーとする。 + unlockResource(inst); + NyLPC_cStopwatch_finalize(&sw); + return NyLPC_TBool_FALSE; + } + //ここから先はi_bufの所有権はインスタンスになってる。 + + //IPv4ペイロードの書き込み + + //allocをした時点でwin,mssは考慮されているので、そのままそうしんしる。 + + //ACK番号の計算 + txq->rto32=inst->uip_connr.current_rto32; + txq->tick_of_sent=NyLPC_cStopwatch_now(); + //パケットヘッダの生成(ヘッダ長はpreadで定義した値(4+6)*4=40です。) + setTxPacketHeader(inst,buf,TCP_ACK|TCP_PSH,i_len); + txq->packet=buf; + + //シーケンス番号の更新 + inst->uip_connr.snd_nxt32=inst->uip_connr.snd_nxt32+i_len; + //Peerのウインドウサイズを更新 + inst->uip_connr.peer_win-=i_len; + //ACK番号の返却 + txq->ackno=NyLPC_HTONL(inst->uip_connr.snd_nxt32); + unlockResource(inst); + NyLPC_cMiMicIpNetIf_sendIPv4Tx(buf); + NyLPC_cStopwatch_finalize(&sw); + return NyLPC_TBool_TRUE; +} + +/** + * See header file. + */ +static NyLPC_TInt32 send(NyLPC_TiTcpSocket_t* i_inst,const void* i_buf_ptr,NyLPC_TInt32 i_len,NyLPC_TUInt32 i_wait_in_msec) +{ + NyLPC_TInt16 hint; + NyLPC_TUInt16 s; + void* buf; + if(i_len<1){ + return 0; + } + hint=(i_len>32767)?32767:i_len; + buf=allocSendBuf(i_inst,hint,&s,i_wait_in_msec); + if(buf==NULL){ + return -1; + } + //送信サイズの計算 + s=((NyLPC_TInt32)stcpstateflags; + //ステータスチェック + switch(f) + { + case UIP_CLOSED: + //閉じている。 + goto ReturnWithUnlock; + case UIP_ESTABLISHED: + //アクティブクローズ。 + inst->tcpstateflags=UIP_FIN_WAIT_1; + //送信のために一旦解除 + unlockResource(inst); + //FINの送信 + if(sendWithRetransmit(inst,TCP_FIN|TCP_ACK,NULL,0,&sw,&sq)==0){ + //ちょっと待つ。 + NyLPC_cThread_yield(); + //TXの消去待ち + if(waitForTxRemove(inst,sq,&sw)){ + //再ロック + lockResource(inst); + //タイムアウトするか、UIP_CLOSED、もしくはTIME_WAITに遷移するのを待つ。(遷移はRxprocで自動的に実行。) + do{ + switch(inst->tcpstateflags) + { + case UIP_TIME_WAIT: + inst->tcpstateflags=UIP_CLOSED; + case UIP_CLOSED: + NyLPC_Assert(inst->txbuf.rp==inst->txbuf.wp); + //成功。 + goto ReturnWithUnlock; + case UIP_FIN_WAIT_1: + case UIP_FIN_WAIT_2: + case UIP_CLOSING: + //一時的なアンロック + unlockResource(inst); + NyLPC_cThread_yield(); + lockResource(inst); + default: + break; + } + }while(!NyLPC_cStopwatch_isExpired(&sw)); + unlockResource(inst); + } + } + break; + case UIP_CLOSE_WAIT: + //LAST_ACKへ遷移 + inst->tcpstateflags=UIP_LAST_ACK; + //送信のために一旦解除 + unlockResource(inst); + if(sendWithRetransmit(inst,TCP_FIN|TCP_ACK,NULL,0,&sw,&sq)==0){ + //ちょっと待つ。 + NyLPC_cThread_yield(); + //TXの消去待ち + if(waitForTxRemove(inst,sq,&sw)){ + //再ロック + lockResource(inst); + //TX消去後にCLOSEDに遷移していればOK + if(inst->tcpstateflags==UIP_CLOSED) + { + NyLPC_Assert(inst->txbuf.rp==inst->txbuf.wp); + goto ReturnWithUnlock; + } + unlockResource(inst); + } + } + //エラー。RSTで切断。 + break; + default: + unlockResource(inst); + NyLPC_Warning(); + break; + } +// if(i_inst->_smutex._lock_count>0){ +// NyLPC_Warning(); +// } + //このパスに到達するのは、FIN送信/ACKに成功したにも拘らず、規定時間内にCLOSEDに遷移しなかった場合。 + //コネクションを強制遷移して、RST + lockResource(inst); + f=inst->tcpstateflags; + if(f!=UIP_CLOSED){ + //もし、強制CLOSE遷移であれば、RSTも送信。 + inst->tcpstateflags=UIP_CLOSED; + unlockResource(inst); + sendRst(inst); + }else{ + unlockResource(inst); + } + NyLPC_cStopwatch_finalize(&sw); + return; +ReturnWithUnlock: + unlockResource(inst); + NyLPC_cStopwatch_finalize(&sw); + return; +} + +/** + * uipサービスタスクが実行する関数です。 + * 定期的に実行する関数。最低でも1s単位で実行してください。 + */ +void NyLPC_cMiMicIpTcpSocket_periodic( + NyLPC_TcMiMicIpTcpSocket_t* i_inst) +{ + int i; + struct NyLPC_TcTcpSocket_TxQItem* q=i_inst->txbuf.txq; + NyLPC_TcStopwatch_t sw; + NyLPC_TUInt32 now; + int rp; + NyLPC_cStopwatch_initialize(&sw); + now=NyLPC_cStopwatch_now(); + //MUTEX LOCK + lockResource(i_inst); + if(i_inst->tcpstateflags==UIP_CLOSED) + { + //CLOSEDなら、バッファ開放。 + resetTxQWithUnlock(i_inst); + }else if(i_inst->txbuf.rp==i_inst->txbuf.wp){ + //再送信パケットがなければ何もしないよ。 + unlockResource(i_inst); + }else if(i_inst->uip_connr.peer_win==0){ + //peer_winが0の場合は何もしない。 + unlockResource(i_inst); + }else{ + //再送信処理 + rp=i_inst->txbuf.rp; + NyLPC_cStopwatch_set(&sw,q[rp].tick_of_sent); + if(NyLPC_cStopwatch_elapseInMsec(&sw)>q[rp].rto32){ + //最古のパケットの送信時間をチェックして、タイムアウトが発生したら、再送時間と送信時刻をセット + //最古パケットRTOを2倍。 + q[rp].rto32*=2; + for(i=rp;i!=i_inst->txbuf.wp;i=(i+1)%NyLPC_TcTcpSocket_NUMBER_OF_TXQ){ + q[i].tick_of_sent=now; + } + if(q[rp].rto32>UIP_IP_RTO_MAX_RTO){ + //最古のRTOが64秒を超えたら、CLOSED + i_inst->tcpstateflags =UIP_CLOSED; + resetTxQWithUnlock(i_inst); + sendRst(i_inst); + }else{ + //規定時間内なら、再送処理 + for(i=rp;i!=i_inst->txbuf.wp;i=(i+1)%NyLPC_TcTcpSocket_NUMBER_OF_TXQ){ +// NyLPC_cUipService_sendIPv4Tx(NyLPC_cIPv4Payload_getBuf(&(q[i].data))); + NyLPC_cMiMicIpNetIf_sendIPv4Tx(q[i].packet); + } + unlockResource(i_inst); + } + }else{ + unlockResource(i_inst); + } + } + NyLPC_cStopwatch_finalize(&sw); + return; +} +/** + * uipサービスタスクが実行する関数です。 + * サービスの開始を通知します。 + */ +void NyLPC_cMiMicIpTcpSocket_startService(NyLPC_TcMiMicIpTcpSocket_t* i_inst,const NyLPC_TcIPv4Config_t* i_config) +{ + NyLPC_Assert(i_inst->tcpstateflags==UIP_CLOSED);//閉じてなければおかしい。 + i_inst->uip_connr.lipaddr=&(i_config->ip_addr); + i_inst->uip_connr.default_mss=i_config->default_mss; + //NyLPC_cTcpSocket_setSynPayload関数でも実行するけど、IFのリセット時なのでここでもやる。 + NyLPC_cFifoBuffer_clear(&(i_inst->rxbuf)); + return; +} +/** + * uipサービスタスクが実行する関数です。 + * サービスの停止を通知します。 + */ +void NyLPC_cMiMicIpTcpSocket_stopService(NyLPC_TcMiMicIpTcpSocket_t* i_inst) +{ + lockResource(i_inst); + if(i_inst->tcpstateflags==UIP_CLOSED) + { + unlockResource(i_inst); + }else{ + i_inst->tcpstateflags=UIP_CLOSED; + resetTxQWithUnlock(i_inst); + sendRst(i_inst); + } + return; +} + + +void* NyLPC_cMiMicIpTcpSocket_parseRx( + NyLPC_TcMiMicIpTcpSocket_t* i_inst, + const NyLPC_TcIPv4Payload_t* i_ipp) +{ + int i,s; + NyLPC_TUInt16 tmp16; + NyLPC_TUInt16 data_size; + NyLPC_TUInt8 in_tcpflag=i_ipp->payload.tcp->flags; + const void* tcp_data_offset; + NyLPC_TBool is_new_packet; + int num_of_noack; + void* dlist[NyLPC_TcTcpSocket_NUMBER_OF_TXQ]; + void* ret; + + //パラメータの計算 + + tmp16=NyLPC_TTcpHeader_getHeaderLength(i_ipp->payload.tcp); + //TCPペイロードの長さは、IPパケットの長さ-(IPヘッダ+TCPヘッダ) + data_size=NyLPC_TIPv4Header_getPacketLength(i_ipp->header)-NyLPC_TIPv4Header_getHeaderLength(i_ipp->header)-tmp16; + //TCPデータオフセット + tcp_data_offset=i_ipp->payload.rawbuf+tmp16; + + //インスタンスをロックする。 + lockResource(i_inst); + + //RSTのチェック。RST受信時は、状態にかかわらず、CLOSEDステータスに移行する。 + if (in_tcpflag & TCP_RST) + { + i_inst->tcpstateflags =UIP_CLOSED; + goto DROP; + } + + + is_new_packet=NyLPC_ntohl(i_ipp->payload.tcp->seqno32)==i_inst->uip_connr.rcv_nxt32; + + + //OPTIONの反映 + + //MSSの取得 + if(NyLPC_TTcpHeader_getTcpMmsOpt(i_ipp->payload.tcp,&tmp16)){ + //取得で着たら更新 + i_inst->uip_connr.peer_mss=tmp16; + } + //受信パケットを元に、未ACKパケットの数を計算 + num_of_noack=getNumOfSending(i_inst,i_ipp->payload.tcp->ackno32);//i_inst->txbuf.num_of_txq; + + //ステータス毎のACK応答 + switch(i_inst->tcpstateflags) + { + case UIP_SYN_RCVD: + //ACKを受信したら、ESTABLISHEDへ。 + //すべてのパケットをACKしたかで判定。() + if(num_of_noack==0){ + i_inst->tcpstateflags=UIP_ESTABLISHED; + }else{ + //それ以外のパケットはドロップする。 + break;//goto DROP; + } + //新しいパケットがなければ、無応答 + if(!is_new_packet){ + break;//goto DROP; + } + //引き続き、ESTABLISHEDの処理へ。 + case UIP_ESTABLISHED: + if(data_size>0){ + if(is_new_packet){ + if(addRecvData(i_inst,tcp_data_offset,data_size)){ + //通常のACK返却 + i_inst->uip_connr.rcv_nxt32+=data_size; + }else{ + //失敗したときは必要に応じて単純ACK + } + } + } + //どちらにしろ、ACK送信 + if(is_new_packet && (in_tcpflag & TCP_FIN)){ + //FINがあるときは、ステータスをCLOSE_WAITへセットして、ACKを返す。 + i_inst->tcpstateflags = UIP_CLOSE_WAIT; + i_inst->uip_connr.rcv_nxt32++; + } + break; + case UIP_CLOSE_WAIT: + //必要に応じたACK応答 + break; + case UIP_LAST_ACK: + //ACK(by FIN)が得られたなら、CLOSEDへ。 + if(num_of_noack==0){ + i_inst->tcpstateflags=UIP_CLOSED; + } + //必要に応じたACK応答 + break; + case UIP_FIN_WAIT_1: + //FIN受信->CLOSINGへ + if(is_new_packet){ + i_inst->uip_connr.rcv_nxt32+=data_size; + if(in_tcpflag & TCP_FIN){ + i_inst->uip_connr.rcv_nxt32++; + if(num_of_noack==0){ + //FINとACKを受信 + i_inst->tcpstateflags=UIP_TIME_WAIT; + }else{ + //FINのみ + i_inst->tcpstateflags=UIP_CLOSING; + } + } + }else if(num_of_noack==0){ + //ACKのみ + i_inst->tcpstateflags=UIP_FIN_WAIT_2; + } + //必要に応じたACK応答 + break; + case UIP_FIN_WAIT_2: + //FIN受信->TIME_WAITへ(pureACK) + if(is_new_packet && (in_tcpflag & TCP_FIN)){ + i_inst->uip_connr.rcv_nxt32++; + i_inst->tcpstateflags=UIP_TIME_WAIT; + } + break; + case UIP_CLOSING: + //ACK受信したら、TIME_WAITへ + if(num_of_noack==0){ + i_inst->tcpstateflags=UIP_TIME_WAIT; + } + break; + case UIP_CLOSED: + //何もできない。何もしない。 + break; + case UIP_TIME_WAIT: + //最終ACKを送り続ける。 + break; + case UIP_SYN_SENT: + //connect関数実行中しか起動しないステータス + if(num_of_noack==0){ + i_inst->tcpstateflags=UIP_ESTABLISHED; + i_inst->uip_connr.rcv_nxt32=NyLPC_ntohl(i_ipp->payload.tcp->seqno32)+1; + }else{ + //それ以外のパケットはドロップする。 + break;//goto DROP; + } + //ACKを送る。 + break; + default: + goto DROP; + } + //ウインドウサイズを更新 + i_inst->uip_connr.peer_win=NyLPC_ntohs(i_ipp->payload.tcp->wnd16); + + //送信キューから、Peerが受信したデータを削除する。 + if(in_tcpflag & TCP_ACK){ + //再送パケットキューから送信済みのデータを回収(後で開放) + NyLPC_Trace(); + s=updateTxQByIndex(i_inst,i_ipp->payload.tcp->ackno32,dlist); + NyLPC_Trace(); + }else{ + s=0; + } + //新しいパケットがきた場合は、再送キューのACKを更新する。 + if(is_new_packet){ + //再送キューのACKを更新 + updateTxAck(i_inst,NyLPC_htonl(i_inst->uip_connr.rcv_nxt32)); + } + + //送信キューのない + if(((in_tcpflag&(TCP_FIN|TCP_SYN))!=0x00) || + ((!is_new_packet) && (data_size>0))) + { + //ソケットからPureACKを生成 as setPacket(i_inst,i_ipp,TCP_ACK,NULL,0); + ret=NyLPC_cMiMicIpNetIf_allocSysTxBuf(); + setTxPacket(i_inst,ret,TCP_ACK,NULL,0); + }else{ + ret=NULL; + } + unlockResource(i_inst); + //取り外したTXメモリの開放 + for(i=0;ivhl=0x40|(0x0f&iph_word); + iph->destipaddr=i_src->header->srcipaddr; + iph->srcipaddr =i_src->header->destipaddr; + NyLPC_TIPv4Header_writeTxIpHeader(iph,UIP_PROTO_TCP); + + //TCPヘッダの更新 + tcph=(struct NyLPC_TTcpHeader*)(((NyLPC_TUInt8*)txb)+NyLPC_TIPv4Header_getHeaderLength(iph)); + + tcph->tcpoffset=(tcph_word<<4); + + tcph->flags = TCP_RST | TCP_ACK; + //sorce & destination port + tcph->srcport = i_src->payload.tcp->destport; + tcph->destport = i_src->payload.tcp->srcport; + //ACK number + tcph->ackno32 = NyLPC_htonl(NyLPC_ntohl(i_src->payload.tcp->seqno32)+1); + //Seq Number + tcph->seqno32 = i_src->payload.tcp->ackno32; + //uip_func_tcp_send_noconn(BUF); + tcph->urgp[0] = tcph->urgp[1] = 0; + tcph->tcpchksum= 0; + + + //最終的なパケットサイズと必要ならペイロードを書き込み + iph->len16=NyLPC_htons((iph_word+tcph_word)*4); + //WND設定 + tcph->wnd16=0; + //Checksumの生成 + tcph->tcpchksum=~(NyLPC_TIPv4Header_makeTcpChecksum(iph)); + iph->ipchksum = ~(NyLPC_TIPv4Header_makeIpChecksum(iph)); + return txb; +} + + + + diff --git a/lib/src/netif/mimicip/NyLPC_cMiMicIpTcpSocket.h b/lib/src/netif/mimicip/NyLPC_cMiMicIpTcpSocket.h new file mode 100644 index 0000000..bc4d1a1 --- /dev/null +++ b/lib/src/netif/mimicip/NyLPC_cMiMicIpTcpSocket.h @@ -0,0 +1,151 @@ +/********************************************************************************* + * PROJECT: MiMic + * -------------------------------------------------------------------------------- + * + * This file is part of MiMic + * Copyright (C)2011 Ryo Iizuka + * + * MiMic is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + * + * For further information please contact. + * http://nyatla.jp/ + * or + * + *********************************************************************************/ +#ifndef NYLPC_CMIMICIPTCPSOCKET_H_ +#define NYLPC_CMIMICIPTCPSOCKET_H_ + + + +#include "NyLPC_os.h" +#include "../NyLPC_NetIf_ip_types.h" +#include "NyLPC_cIPv4Payload.h" +#include "../NyLPC_iTcpSocket.h" +#include "NyLPC_cIPv4.h" + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + + +typedef struct NyLPC_TcMiMicIpTcpSocket NyLPC_TcMiMicIpTcpSocket_t; + + +/********************************************************************** + * + * NyLPC_TTcpListenerSynInfo struct + * + **********************************************************************/ + + +struct NyLPC_TTcpSocketSynParam +{ + struct NyLPC_TIPv4Addr srcaddr; + NyLPC_TUInt16 rport; + NyLPC_TUInt16 mss; + NyLPC_TUInt32 rcv_nxt32; +}; + + +/********************************************************************** + * + * NyLPC_TcTcpSocket_TxQItem struct + * + **********************************************************************/ + +/** + * TXキューの数。この値は8未満にしてください。 + */ +#define NyLPC_TcTcpSocket_NUMBER_OF_TXQ 4 + +struct NyLPC_TcTcpSocket_TxQItem +{ + //最終更新時刻 + NyLPC_TUInt32 tick_of_sent; + //このパケットのRTO(秒間隔) + NyLPC_TUInt32 rto32; + void* packet; + //パケットのACK番号。この番号を受信できれば、再送パケットは消去可能である。 + NyLPC_TUInt32 ackno; +}; + +/********************************************************************** + * + * uip_conn struct + * + **********************************************************************/ + +struct uip_conn +{ + struct NyLPC_TIPv4Addr ripaddr; /**< The IP address of the remote host. */ + const struct NyLPC_TIPv4Addr* lipaddr; /**< ローカルIP*/ + NyLPC_TUInt16 lport; /**< The local TCP port, in network byte order. */ + NyLPC_TUInt16 rport; /**< The local remote TCP port, in network byte order. */ + NyLPC_TUInt32 rcv_nxt32; /**< The sequence number that we expect to receive next. */ + NyLPC_TUInt32 snd_nxt32; /**< 送信用sqカウンター*/ + NyLPC_TUInt16 peer_mss; /**< PeerのMSS*/ + NyLPC_TUInt16 default_mss; /**< Peerの初期MMS*/ + /**Peerのウインドウサイズ*/ + NyLPC_TUInt16 peer_win; + NyLPC_TUInt16 _padding; + /**現在ソケットのRTO*/ + NyLPC_TUInt32 current_rto32; +}; + + + + + + + + +/********************************************************************** + * + * NyLPC_TcTcpSocket class + * + **********************************************************************/ + +/** + * uipサービスを使用したTCPソケットクラスです。 + * この関数は2つのタスクから呼び出されます。 + * [uipTask] -> [cTcpSocket] <- [Application] + * ApplicationとuipTaskとの間での排他処理はインスタンスで制御されています。 + * Application側からのコールは内部でuipTaskとの間で排他処理を実行します。 + * Application側からのコールはリエントラントではありません。 + */ + +struct NyLPC_TcMiMicIpTcpSocket +{ + struct NyLPC_TiTcpSocket _super; + NyLPC_TcIPv4_t* _parent_ipv4; + //この変数は、uipタスクの実行する関数のみが変更する。 + struct uip_conn uip_connr; + NyLPC_TcFifoBuffer_t rxbuf; + struct{ + NyLPC_TUInt8 rp; + NyLPC_TUInt8 wp; + //送信キュー + struct NyLPC_TcTcpSocket_TxQItem txq[NyLPC_TcTcpSocket_NUMBER_OF_TXQ]; + }txbuf; + volatile NyLPC_TUInt8 tcpstateflags; /**< TCP state and flags. */ +}; + +NyLPC_TBool NyLPC_cMiMicIpTcpSocket_initialize(NyLPC_TcMiMicIpTcpSocket_t* i_inst,void* i_rbuf,NyLPC_TUInt16 i_rbuf_len); + + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* NYLPC_CTCPSOCKET_H_ */ diff --git a/lib/src/uip/NyLPC_cTcpSocket_protected.h b/lib/src/netif/mimicip/NyLPC_cMiMicIpTcpSocket_protected.h similarity index 71% rename from lib/src/uip/NyLPC_cTcpSocket_protected.h rename to lib/src/netif/mimicip/NyLPC_cMiMicIpTcpSocket_protected.h index 9b5958b..071000f 100644 --- a/lib/src/uip/NyLPC_cTcpSocket_protected.h +++ b/lib/src/netif/mimicip/NyLPC_cMiMicIpTcpSocket_protected.h @@ -1,113 +1,117 @@ -/********************************************************************************* - * PROJECT: MiMic - * -------------------------------------------------------------------------------- - * - * This file is part of MiMic - * Copyright (C)2011 Ryo Iizuka - * - * MiMic is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published - * by the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program. If not, see . - * - * For further information please contact. - * http://nyatla.jp/ - * or - * - *********************************************************************************/ - -#ifndef NYLPC_CTCPSOCKET_PROTECTED_H_ -#define NYLPC_CTCPSOCKET_PROTECTED_H_ -#include "NyLPC_cIPv4.h" -#include "NyLPC_cTcpSocket.h" -#include "NyLPC_cIPv4Config.h" -#include "NyLPC_cIPv4Payload_protected.h" -#ifdef __cplusplus -extern "C" { -#endif /* __cplusplus */ - -/********************************************************************** - * ステータス値の定義 - **********************************************************************/ -#define UIP_CLOSED 0 -#define UIP_SYN_RCVD 1 -#define UIP_SYN_SENT 2 -#define UIP_ESTABLISHED 3 -#define UIP_FIN_WAIT_1 4 -#define UIP_FIN_WAIT_2 5 -#define UIP_CLOSING 6 -#define UIP_TIME_WAIT 7 -#define UIP_CLOSE_WAIT 8 -#define UIP_LAST_ACK 9 - - - - - - - - -/** - * パース結果をもとに、ソケットのuipconnectionを初期化します。 +/********************************************************************************* + * PROJECT: MiMic + * -------------------------------------------------------------------------------- + * + * This file is part of MiMic + * Copyright (C)2011 Ryo Iizuka + * + * MiMic is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + * + * For further information please contact. + * http://nyatla.jp/ + * or + * + *********************************************************************************/ + +#ifndef NYLPC_CMIMICIPTCPSOCKET_PROTECTED_H_ +#define NYLPC_CMIMICIPTCPSOCKET_PROTECTED_H_ +#include "NyLPC_cMiMicIpTcpSocket.h" +#include "../NyLPC_cIPv4Config.h" +#include "NyLPC_cIPv4Payload_protected.h" +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +/********************************************************************** + * ステータス値の定義 + **********************************************************************/ +#define UIP_CLOSED 0 +#define UIP_SYN_RCVD 1 +#define UIP_SYN_SENT 2 +#define UIP_ESTABLISHED 3 +#define UIP_FIN_WAIT_1 4 +#define UIP_FIN_WAIT_2 5 +#define UIP_CLOSING 6 +#define UIP_TIME_WAIT 7 +#define UIP_CLOSE_WAIT 8 +#define UIP_LAST_ACK 9 + + + + + + + + +/** + * パース結果をもとに、ソケットのuipconnectionを初期化します。 * この関数は、cUipServiceからのみコールできます。 - */ -void NyLPC_cTcpSocket_initConnection(NyLPC_TcTcpSocket_t* i_inst,const NyLPC_TcIPv4Config_t* i_config,const NyLPC_TcIPv4Payload_t* i_ipp); - -/** - * TCPペイロードを処理して、応答パケットをペイロードに返します。 - * uipサービスタスクが実行する関数です。 - * @return - * 応答パケットを格納したメモリブロックを返します。 - * このメモリは、NyLPC_cUipService_allocSysTxBuf関数で確保されたメモリです。 - */ -void* NyLPC_cTcpSocket_parseRx( - NyLPC_TcTcpSocket_t* i_inst, - const NyLPC_TcIPv4Payload_t* i_ipp); - -/** - * 定期的に実行する関数。最低でも1s単位で実行してください。 - * uipサービスタスクが実行する関数です。 - */ -void NyLPC_cTcpSocket_periodic( - NyLPC_TcTcpSocket_t* i_inst); - -/** - * CLOSEステータスのソケットを、SYN情報の内容でSYNRECV状態にします。 - * この関数は、NyLPC_TcTcpListenerクラスからコールされます。 - * @return - * 遷移に成功すると、TRUEを返します。 - */ -NyLPC_TBool NyLPC_cTcpSocket_listenSyn(NyLPC_TcTcpSocket_t* i_inst,const struct NyLPC_TTcpSocketSynParam* i_lq,NyLPC_TUInt16 i_lport); - - -/** - * uipサービスタスクが実行する関数です。 - * サービスの開始を通知します。 - * この関数は他のAPIが非同期に実行されないことが保証される状況で使用する必要があります。 - */ -void NyLPC_cTcpSocket_startService(NyLPC_TcTcpSocket_t* i_inst,const NyLPC_TcIPv4Config_t* i_config); - -/** - * uipサービスタスクが実行する関数です。 - * サービスの停止を通知します。 - * この関数は他のAPIが非同期に実行されないことが保証される状況で使用する必要があります。 - */ -void NyLPC_cTcpSocket_stopService(NyLPC_TcTcpSocket_t* i_inst); - - -void* NyLPC_cTcpSocket_allocTcpReverseRstAck( - const NyLPC_TcIPv4Payload_t* i_src); - -#ifdef __cplusplus -} -#endif /* __cplusplus */ - -#endif /* NYLPC_CTCPSOCKET_PROTECTED_H_ */ + */ +void NyLPC_cMiMicIpTcpSocket_initConnection(NyLPC_TcMiMicIpTcpSocket_t* i_inst,const NyLPC_TcIPv4Config_t* i_config,const NyLPC_TcIPv4Payload_t* i_ipp); + +/** + * TCPペイロードを処理して、応答パケットをペイロードに返します。 + * uipサービスタスクが実行する関数です。 + * @return + * 応答パケットを格納したメモリブロックを返します。 + * このメモリは、NyLPC_cUipService_allocSysTxBuf関数で確保されたメモリです。 + */ +void* NyLPC_cMiMicIpTcpSocket_parseRx( + NyLPC_TcMiMicIpTcpSocket_t* i_inst, + const NyLPC_TcIPv4Payload_t* i_ipp); + +/** + * 定期的に実行する関数。最低でも1s単位で実行してください。 + * uipサービスタスクが実行する関数です。 + */ +void NyLPC_cMiMicIpTcpSocket_periodic( + NyLPC_TcMiMicIpTcpSocket_t* i_inst); + +/** + * CLOSEステータスのソケットを、SYN情報の内容でSYNRECV状態にします。 + * この関数は、NyLPC_TcTcpListenerクラスからコールされます。 + * @return + * 遷移に成功すると、TRUEを返します。 + */ +NyLPC_TBool NyLPC_cMiMicIpTcpSocket_listenSyn(NyLPC_TcMiMicIpTcpSocket_t* i_inst,const struct NyLPC_TTcpSocketSynParam* i_lq,NyLPC_TUInt16 i_lport); + + +/** + * uipサービスタスクが実行する関数です。 + * サービスの開始を通知します。 + * この関数は他のAPIが非同期に実行されないことが保証される状況で使用する必要があります。 + */ +void NyLPC_cMiMicIpTcpSocket_startService(NyLPC_TcMiMicIpTcpSocket_t* i_inst,const NyLPC_TcIPv4Config_t* i_config); + +/** + * uipサービスタスクが実行する関数です。 + * サービスの停止を通知します。 + * この関数は他のAPIが非同期に実行されないことが保証される状況で使用する必要があります。 + */ +void NyLPC_cMiMicIpTcpSocket_stopService(NyLPC_TcMiMicIpTcpSocket_t* i_inst); + + +void* NyLPC_cMiMicIpTcpSocket_allocTcpReverseRstAck( + const NyLPC_TcIPv4Payload_t* i_src); + +/** + * ローカルポートを返します。 + */ +#define NyLPC_cMiMicIpTcpSocket_getLocalPort(i) ((i)->uip_connr.lport) +#define NyLPC_cMiMicIpTcpSocket_isClosed(i) ((i)->tcpstateflags==UIP_CLOSED) +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* NYLPC_CTCPSOCKET_PROTECTED_H_ */ diff --git a/lib/src/netif/mimicip/NyLPC_cMiMicIpUdpSocket.c b/lib/src/netif/mimicip/NyLPC_cMiMicIpUdpSocket.c new file mode 100644 index 0000000..fdd7c28 --- /dev/null +++ b/lib/src/netif/mimicip/NyLPC_cMiMicIpUdpSocket.c @@ -0,0 +1,430 @@ +/********************************************************************************* + * PROJECT: MiMic + * -------------------------------------------------------------------------------- + * + * This file is part of MiMic + * Copyright (C)2011 Ryo Iizuka + * + * MiMic is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + * + * For further information please contact. + * http://nyatla.jp/ + * or + * + * + * Parts of this file were leveraged from uIP: + * + * Copyright (c) 2001-2003, Adam Dunkels. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +#include "NyLPC_cMiMicIpUdpSocket_protected.h" +#include "NyLPC_cIPv4Payload_protected.h" +#include "NyLPC_cMiMicIpNetIf_protected.h" + +/** + * フラグ値 + */ +#define NyLPC_cMiMicIpUdpSocket_FLAG_BROADCAST 0 +/** + * UDP/IPヘッダのサイズ + */ +#define SIZE_OF_IPv4_UDPIP_HEADER 28 + +#define lockResource(i_inst) NyLPC_cMutex_lock(((i_inst)->_smutex)) +#define unlockResource(i_inst) NyLPC_cMutex_unlock(((i_inst)->_smutex)) + +/* + * 関数テーブル + */ +static void joinMulticast(NyLPC_TiUdpSocket_t* i_inst,const struct NyLPC_TIPv4Addr* i_addr); +static void setBroadcast(NyLPC_TiUdpSocket_t* i_inst); +static NyLPC_TInt32 precv(NyLPC_TiUdpSocket_t* i_inst,const void** o_buf_ptr,const struct NyLPC_TIPv4RxInfo** o_info,NyLPC_TUInt32 i_wait_msec); +static void pseek(NyLPC_TiUdpSocket_t* i_inst); +static void* allocSendBuf(NyLPC_TiUdpSocket_t* i_inst,NyLPC_TUInt16 i_hint,NyLPC_TUInt16* o_buf_size,NyLPC_TUInt32 i_wait_in_msec); +static void releaseSendBuf(NyLPC_TiUdpSocket_t* i_inst,void* i_buf_ptr); +static NyLPC_TBool psend(NyLPC_TiUdpSocket_t* i_inst,const struct NyLPC_TIPv4Addr* i_addr,NyLPC_TUInt16 i_port,void* i_buf_ptr,int i_len); +static NyLPC_TInt32 send(NyLPC_TiUdpSocket_t* i_inst,const struct NyLPC_TIPv4Addr* i_addr,NyLPC_TUInt16 i_port,const void* i_buf_ptr,NyLPC_TInt32 i_len,NyLPC_TUInt32 i_wait_in_msec); +static void setOnRxHandler(NyLPC_TiUdpSocket_t* i_inst,NyLPC_TiUdpSocket_onRxHandler i_handler); +static void setOnPeriodicHandler(NyLPC_TiUdpSocket_t* i_inst,NyLPC_TiUdpSocket_onPeriodicHandler i_handler); +static const struct NyLPC_TIPv4Addr* getSockIP(const NyLPC_TiUdpSocket_t* i_inst); +static void finalize(NyLPC_TiUdpSocket_t* i_inst); + +static const struct NyLPC_TiUdpSocket_Interface interface= +{ + joinMulticast, + setBroadcast, + precv, + pseek, + allocSendBuf, + releaseSendBuf, + psend, + send, + setOnRxHandler, + setOnPeriodicHandler, + getSockIP, + finalize +}; + + + + +/* + * Initializer/Finalizer + */ + + +NyLPC_TBool NyLPC_cMiMicIpUdpSocket_initialize(NyLPC_TcMiMicIpUdpSocket_t* i_inst,NyLPC_TUInt16 i_port,void* i_rbuf,NyLPC_TUInt16 i_rbuf_len) +{ + NyLPC_TcMiMicIpNetIf_t* srv=_NyLPC_TcMiMicIpNetIf_inst; + i_inst->_super._interface=&interface; + i_inst->_super._tag=NULL; + //uipサービスは初期化済であること。 + NyLPC_Assert(NyLPC_cMiMicIpNetIf_isInitService()); + i_inst->_smutex=NyLPC_cIPv4_getSockMutex(&(srv->_tcpv4)); + i_inst->uip_udp_conn.lport=NyLPC_htons(i_port); + i_inst->uip_udp_conn.mcastaddr=NyLPC_TIPv4Addr_ZERO; + i_inst->uip_udp_conn.flags=0x00; + i_inst->as_handler.rx=NULL; + i_inst->as_handler.periodic=NULL; + + NyLPC_cFifoBuffer_initialize(&(i_inst->rxbuf),i_rbuf,i_rbuf_len); + //管理リストへ登録。 + return NyLPC_TBool_TRUE; +} + + + +/** + * IP+UDPヘッダサイズを0x05*4+8バイトとして、UDPの送信バッファをセットします。 + */ +static void setUdpTxBufHeader(const NyLPC_TcMiMicIpUdpSocket_t* i_inst,void*i_buf,const struct NyLPC_TIPv4Addr* i_dest_ip,NyLPC_TUInt16 i_dest_port,NyLPC_TUInt8 i_iph_word,NyLPC_TUInt16 i_payload_size) +{ + struct NyLPC_TIPv4Header* header=(struct NyLPC_TIPv4Header*)i_buf; + struct NyLPC_TUdpHeader* udp =(struct NyLPC_TUdpHeader*)(((NyLPC_TUInt8*)i_buf)+i_iph_word*4); + + header->vhl=0x40|(0x0f&i_iph_word); + header->len16=NyLPC_htons(i_payload_size+(i_iph_word*4+8)); + udp->udplen=NyLPC_htons(i_payload_size+(8)); + //IPv4のTxヘッダを書き込む。 + header->destipaddr=*i_dest_ip; + header->srcipaddr =i_inst->uip_udp_conn.lipaddr; + + NyLPC_TIPv4Header_writeTxIpHeader(header,UIP_PROTO_UDP); + + //UDPのTxヘッダを書き込む + //sorce & destination port + udp->srcport = i_inst->uip_udp_conn.lport; + udp->destport = NyLPC_htons(i_dest_port); + udp->udpchksum= 0; + + udp->udpchksum=~(NyLPC_TIPv4Header_makeTcpChecksum(header)); + header->ipchksum = ~(NyLPC_TIPv4Header_makeIpChecksum(header)); +} + + + + +/** + * この関数は、rxパケットを処理して、ソケットの状態を更新します。 + * uipサービスタスクが実行する関数です。 + * この関数はNyLPC_cTcpSocket_periodicと排他実行すること。 + */ +NyLPC_TBool NyLPC_cMiMicIpUdpSocket_parseRx( + NyLPC_TcMiMicIpUdpSocket_t* i_inst, + const NyLPC_TcIPv4Payload_t* i_ipp) +{ + NyLPC_TUInt16 tmp16; + struct NyLPC_TIPv4RxInfo dheader; + const void* data_offset; + //ブロードキャストの場合、フラグを確認 + if(NyLPC_TIPv4Addr_isEqual(&(i_ipp->header->destipaddr),&NyLPC_TIPv4Addr_BROADCAST)){ + if(!NyLPC_TUInt8_isBitOn(i_inst->uip_udp_conn.flags,NyLPC_cMiMicIpUdpSocket_FLAG_BROADCAST)){ + goto DROP; + } + } + //パラメータの計算 + tmp16=NyLPC_TUdpHeader_getHeaderLength(i_ipp->payload.tcp); + //UDPペイロードの長さは、IPパケットの長さ-(IPヘッダ+UDPヘッダ) + dheader.size=NyLPC_TIPv4Header_getPacketLength(i_ipp->header)-NyLPC_TIPv4Header_getHeaderLength(i_ipp->header)-tmp16; + dheader.peer_ip=i_ipp->header->srcipaddr; + dheader.peer_port=NyLPC_ntohs(i_ipp->payload.udp->srcport); + dheader.ip=i_ipp->header->destipaddr; + dheader.port=NyLPC_ntohs(i_ipp->payload.udp->destport); + if(i_inst->as_handler.rx!=NULL){ + if(!i_inst->as_handler.rx((NyLPC_TiUdpSocket_t*)(i_inst),i_ipp->payload.rawbuf+tmp16,&dheader)){ + return NyLPC_TBool_FALSE;//UDPはReturnパケットなし + } + } + //TCPデータオフセット + data_offset=i_ipp->payload.rawbuf+tmp16; + + //インスタンスをロックする。 + lockResource(i_inst); + //受信キューへ追加(データ構造はsize[2]+data[n]).sizeに16ビットの受信サイズ,後続にデータ + + //受信データサイズを確認 + if(NyLPC_cFifoBuffer_getSpace(&(i_inst->rxbuf))rxbuf),&dheader,sizeof(struct NyLPC_TIPv4RxInfo)); + NyLPC_cFifoBuffer_push(&(i_inst->rxbuf),data_offset,dheader.size); + unlockResource(i_inst); + return NyLPC_TBool_FALSE;//UDPはReturnパケットなし +DROP: + unlockResource(i_inst); + return NyLPC_TBool_FALSE; +} + + + + +static void finalize(NyLPC_TiUdpSocket_t* i_inst) +{ + NyLPC_Assert(NyLPC_cMiMicIpNetIf_isInitService()); + + NyLPC_cFifoBuffer_finalize(&(i_inst->rxbuf)); + NyLPC_cMiMicIpNetIf_releaseUdpSocketMemory((NyLPC_TcMiMicIpUdpSocket_t*)i_inst); + return; +} + + +static void joinMulticast(NyLPC_TiUdpSocket_t* i_inst,const struct NyLPC_TIPv4Addr* i_addr) +{ + NyLPC_TcMiMicIpUdpSocket_t* inst=(NyLPC_TcMiMicIpUdpSocket_t*)i_inst; + inst->uip_udp_conn.mcastaddr=*i_addr; +} +static void setBroadcast(NyLPC_TiUdpSocket_t* i_inst) +{ + NyLPC_TcMiMicIpUdpSocket_t* inst=(NyLPC_TcMiMicIpUdpSocket_t*)i_inst; + NyLPC_TUInt8_setBit(inst->uip_udp_conn.flags,NyLPC_cMiMicIpUdpSocket_FLAG_BROADCAST); +} + + + +/** + * see Header file + */ +static NyLPC_TInt32 precv(NyLPC_TiUdpSocket_t* i_inst,const void** o_buf_ptr,const struct NyLPC_TIPv4RxInfo** o_info,NyLPC_TUInt32 i_wait_msec) +{ + NyLPC_TcMiMicIpUdpSocket_t* inst=(NyLPC_TcMiMicIpUdpSocket_t*)i_inst; + NyLPC_TUInt16 rlen; + //タイマを生成 + NyLPC_TcStopwatch_t sw; + NyLPC_cStopwatch_initialize(&sw); + const char* b; + const struct NyLPC_TIPv4RxInfo* rh; + + //ESTABLISHED以外の場合は、エラー。 + NyLPC_cStopwatch_setNow(&sw); + while(NyLPC_cStopwatch_elapseInMsec(&sw)rxbuf)); + //MUTEX UNLOCK + if(rlen>0){ + //受信キューにデータがあれば返す。 + b=(char*)NyLPC_cFifoBuffer_getPtr(&(inst->rxbuf)); + rh=(const struct NyLPC_TIPv4RxInfo*)b; + *o_buf_ptr=b+sizeof(struct NyLPC_TIPv4RxInfo); + if(o_info!=NULL){ + *o_info=rh; + } + unlockResource(inst); + NyLPC_cStopwatch_finalize(&sw); + return rh->size; + } + unlockResource(inst); + //タスクスイッチ + NyLPC_cThread_yield(); + }; + NyLPC_cStopwatch_finalize(&sw); + return 0; +} +/** + * See header file + */ +static void pseek(NyLPC_TiUdpSocket_t* i_inst) +{ + NyLPC_TcMiMicIpUdpSocket_t* inst=(NyLPC_TcMiMicIpUdpSocket_t*)i_inst; + NyLPC_TUInt16 s; + const struct NyLPC_TIPv4RxInfo* rh; + //シークサイズを決定 + lockResource(inst); + s=NyLPC_cFifoBuffer_getLength(&(inst->rxbuf)); + if(s>0){ + rh=(const struct NyLPC_TIPv4RxInfo*)NyLPC_cFifoBuffer_getPtr(&(inst->rxbuf)); + NyLPC_cFifoBuffer_pop(&(inst->rxbuf),rh->size+sizeof(struct NyLPC_TIPv4RxInfo)); + } + unlockResource(inst); +} + +/** + * See header file. + */ +static void* allocSendBuf(NyLPC_TiUdpSocket_t* i_inst,NyLPC_TUInt16 i_hint,NyLPC_TUInt16* o_buf_size,NyLPC_TUInt32 i_wait_in_msec) +{ + NyLPC_TUInt16 s; + void* buf; + NyLPC_TcStopwatch_t sw; + + NyLPC_cStopwatch_initialize(&sw); + NyLPC_cStopwatch_startExpire(&sw,i_wait_in_msec); + + //送信バッファを取得 + //@bug バッファが取れるまで通信がブロックするの。ここはなんとかしないと。 + for(;;){ + buf=NyLPC_cMiMicIpNetIf_allocTxBuf(i_hint+(SIZE_OF_IPv4_UDPIP_HEADER),&s); + if(buf!=NULL){ + break; + } + //タイムアウト確認 + if(NyLPC_cStopwatch_isExpired(&sw)){ + return NULL; + } + } + //バッファサイズ確定。 + *o_buf_size=s; + NyLPC_cStopwatch_finalize(&sw); + return (NyLPC_TUInt8*)buf+SIZE_OF_IPv4_UDPIP_HEADER; +} +/** + * See Header file. + */ +static void releaseSendBuf(NyLPC_TiUdpSocket_t* i_inst,void* i_buf_ptr) +{ + NyLPC_cMiMicIpNetIf_releaseTxBuf((NyLPC_TUInt8*)i_buf_ptr-SIZE_OF_IPv4_UDPIP_HEADER); +} + +/** + * See header file + */ +static NyLPC_TBool psend(NyLPC_TiUdpSocket_t* i_inst,const struct NyLPC_TIPv4Addr* i_addr,NyLPC_TUInt16 i_port,void* i_buf_ptr,int i_len) +{ + void* buf; + NyLPC_TcMiMicIpUdpSocket_t* inst=(NyLPC_TcMiMicIpUdpSocket_t*)i_inst; + //ブロードキャストの場合、フラグを確認 + if(NyLPC_TIPv4Addr_isEqual(i_addr,&NyLPC_TIPv4Addr_BROADCAST)){ + if(!NyLPC_TUInt8_isBitOn(inst->uip_udp_conn.flags,NyLPC_cMiMicIpUdpSocket_FLAG_BROADCAST)){ + return NyLPC_TBool_FALSE; + } + } + + //先頭ポインタは、i_buf-sizeof(SIZE_OF_IPv4_TCPIP_HEADER)固定 + buf=(NyLPC_TUInt8*)i_buf_ptr-SIZE_OF_IPv4_UDPIP_HEADER; + + lockResource(inst); + //IPv4ペイロードの書き込み + setUdpTxBufHeader(inst,buf,i_addr,i_port,0x05,i_len); + unlockResource(inst); + // !(BroadCast || Multicast)の場合は送信前にARPテーブルをチェックする。 + if(!(NyLPC_TIPv4Addr_isEqual(i_addr,&NyLPC_TIPv4Addr_BROADCAST) || NyLPC_TIPv4Addr_isEqualWithMask(i_addr,&NyLPC_TIPv4Addr_MULTICAST,&NyLPC_TIPv4Addr_MULTICAST_MASK))){ + if(!NyLPC_cMiMicIpNetIf_hasArpInfo(i_addr)){ + NyLPC_cMiMicIpNetIf_sendArpRequest(i_addr); + NyLPC_cThread_sleep(30); + } + } + NyLPC_cMiMicIpNetIf_sendIPv4Tx(buf); + NyLPC_cMiMicIpNetIf_releaseTxBuf(buf); + return NyLPC_TBool_TRUE; +} + +/** + * See header file. + */ +static NyLPC_TInt32 send(NyLPC_TiUdpSocket_t* i_inst,const struct NyLPC_TIPv4Addr* i_addr,NyLPC_TUInt16 i_port,const void* i_buf_ptr,NyLPC_TInt32 i_len,NyLPC_TUInt32 i_wait_in_msec) +{ + NyLPC_TUInt16 s; + int i; + void* buf; + if(i_len<1 || i_len>1200){ + return 0; + } + //バッファの取得確率を上げるために2倍のサイズを要求 + for(i=0;i<3;i++){ + buf=allocSendBuf(i_inst,i_len*2,&s,i_wait_in_msec); + if(buf==NULL || sas_handler.rx=i_handler; +} +static void setOnPeriodicHandler(NyLPC_TiUdpSocket_t* i_inst,NyLPC_TiUdpSocket_onPeriodicHandler i_handler) +{ + NyLPC_TcMiMicIpUdpSocket_t* inst=(NyLPC_TcMiMicIpUdpSocket_t*)i_inst; + inst->as_handler.periodic=i_handler; +} +static const struct NyLPC_TIPv4Addr* getSockIP(const NyLPC_TiUdpSocket_t* i_inst) +{ + NyLPC_TcMiMicIpUdpSocket_t* inst=(NyLPC_TcMiMicIpUdpSocket_t*)i_inst; + return &inst->uip_udp_conn.lipaddr; +} + + +void NyLPC_cMiMicIpUdpSocket_startService(NyLPC_TcMiMicIpUdpSocket_t* i_inst,const NyLPC_TcIPv4Config_t* i_config) +{ + i_inst->uip_udp_conn.lipaddr=i_config->ip_addr; + //受信バッファのクリア + NyLPC_cFifoBuffer_clear(&(i_inst->rxbuf)); + return; +} + + +void NyLPC_cMiMicIpUdpSocket_stopService(NyLPC_TcMiMicIpUdpSocket_t* i_inst) +{ + //停止処理? +} + + diff --git a/lib/src/netif/mimicip/NyLPC_cMiMicIpUdpSocket.h b/lib/src/netif/mimicip/NyLPC_cMiMicIpUdpSocket.h new file mode 100644 index 0000000..b4a01e6 --- /dev/null +++ b/lib/src/netif/mimicip/NyLPC_cMiMicIpUdpSocket.h @@ -0,0 +1,78 @@ +/* + * NyLPC_cUdpSocket.h + * + * Created on: 2013/05/20 + * Author: nyatla + */ + +#ifndef NYLPC_CMIMICIPUDPSOCKET_H_ +#define NYLPC_CMIMICIPUDPSOCKET_H_ + +#include "NyLPC_os.h" +#include "../NyLPC_iUdpSocket.h" + + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +#define NyLPC_TcMiMicIpUdpSocket_MAX_UDP_SOCKET 1 + +/** + * Class struct + */ +typedef struct NyLPC_TcMiMicIpUdpSocket NyLPC_TcMiMicIpUdpSocket_t; + + + +/** + * Representation of a uIP UDP connection. + */ +struct uip_udp_conn{ + struct NyLPC_TIPv4Addr lipaddr; /**< The IP address of the remote peer. */ + /** マルチキャスとアドレス(ZEROで無効)*/ + struct NyLPC_TIPv4Addr mcastaddr; + NyLPC_TUInt16 lport; /**< The local port number in network byte order. */ + NyLPC_TUInt8 flags; /**フラグ*/ + NyLPC_TUInt8 padding; /***/ +}; + + + + +struct NyLPC_TcMiMicIpUdpSocket +{ + struct NyLPC_TiUdpSocket _super; + //この変数は、uipタスクの実行する関数のみが変更する。 + struct uip_udp_conn uip_udp_conn; + NyLPC_TcFifoBuffer_t rxbuf; + NyLPC_TcMutex_t* _smutex; + struct{ + /** 受信ハンドラ。サービス実装に使用する。*/ + NyLPC_TiUdpSocket_onRxHandler rx; + /** 定期実行ハンドラ。サービス実装に使用する。最低保障周期は1s*/ + NyLPC_TiUdpSocket_onPeriodicHandler periodic; + }as_handler; +}; + + + + + + +/** + * @param i_rbuf + * 受信バッファアアドレス。サイズは、(最大受信サイズ-4バイト)*キュー数で計算します。 + * @param i_rbuf_len + * 受信バッファのサイズ。 + */ +NyLPC_TBool NyLPC_cMiMicIpUdpSocket_initialize(NyLPC_TcMiMicIpUdpSocket_t* i_inst,NyLPC_TUInt16 i_port,void* i_rbuf,NyLPC_TUInt16 i_rbuf_len); +void NyLPC_cMiMicIpUdpSocket_finalize(NyLPC_TcMiMicIpUdpSocket_t* i_inst); + + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + + +#endif /* NYLPC_CUDPSOCKET_H_ */ diff --git a/lib/src/uip/NyLPC_cUdpSocket_protected.h b/lib/src/netif/mimicip/NyLPC_cMiMicIpUdpSocket_protected.h similarity index 67% rename from lib/src/uip/NyLPC_cUdpSocket_protected.h rename to lib/src/netif/mimicip/NyLPC_cMiMicIpUdpSocket_protected.h index 637faee..2dcfb7d 100644 --- a/lib/src/uip/NyLPC_cUdpSocket_protected.h +++ b/lib/src/netif/mimicip/NyLPC_cMiMicIpUdpSocket_protected.h @@ -1,53 +1,56 @@ -/* - * NyLPC_cUdpSocket.h - * - * Created on: 2013/05/20 - * Author: nyatla - */ - -#ifndef NYLPC_CUDPSOCKET_PROTECTED_H_ -#define NYLPC_CUDPSOCKET_PROTECTED_H_ -#include "NyLPC_cUdpSocket.h" -#include "NyLPC_cIPv4Payload.h" -#include "NyLPC_cIPv4Config.h" - -#ifdef __cplusplus -extern "C" { -#endif /* __cplusplus */ - - -/** - * この関数は、rxパケットを処理して、ソケットの状態を更新します。 - * uipサービスタスクが実行する関数です。 - */ -NyLPC_TBool NyLPC_cUdpSocket_parseRx( - NyLPC_TcUdpSocket_t* i_inst, - const NyLPC_TcIPv4Payload_t* i_ipp); - -/** - * uipサービスタスクが実行する関数です。 - * サービスの開始を通知します。 - * この関数は他のAPIが非同期に実行されないことが保証される状況で使用する必要があります。 - */ -void NyLPC_cUdpSocket_startService(NyLPC_TcUdpSocket_t* i_inst,const NyLPC_TcIPv4Config_t* i_config); - -/** - * uipサービスタスクが実行する関数です。 - * サービスの停止を通知します。 - * この関数は他のAPIが非同期に実行されないことが保証される状況で使用する必要があります。 - */ -void NyLPC_cUdpSocket_stopService(NyLPC_TcUdpSocket_t* i_inst); - - -/** - * 定期的に実行する関数。最低でも1s単位で実行してください。 - * uipサービスタスクが実行する関数です。 - */ -#define NyLPC_cUdpSocket_periodic(i_inst) if((i_inst)->as_handler.periodic!=NULL){(i_inst)->as_handler.periodic(i_inst);} - -#ifdef __cplusplus -} -#endif /* __cplusplus */ - - -#endif /* NYLPC_CUDPSOCKET_H_ */ +/* + * NyLPC_cUdpSocket.h + * + * Created on: 2013/05/20 + * Author: nyatla + */ + +#ifndef NYLPC_CUDPSOCKET_PROTECTED_H_ +#define NYLPC_CUDPSOCKET_PROTECTED_H_ +#include "NyLPC_cMiMicIpUdpSocket.h" +#include "NyLPC_cIPv4Payload.h" +#include "../NyLPC_cIPv4Config.h" + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + + + + + +/** + * この関数は、rxパケットを処理して、ソケットの状態を更新します。 + * uipサービスタスクが実行する関数です。 + */ +NyLPC_TBool NyLPC_cMiMicIpUdpSocket_parseRx( + NyLPC_TcMiMicIpUdpSocket_t* i_inst, + const NyLPC_TcIPv4Payload_t* i_ipp); + +/** + * uipサービスタスクが実行する関数です。 + * サービスの開始を通知します。 + * この関数は他のAPIが非同期に実行されないことが保証される状況で使用する必要があります。 + */ +void NyLPC_cMiMicIpUdpSocket_startService(NyLPC_TcMiMicIpUdpSocket_t* i_inst,const NyLPC_TcIPv4Config_t* i_config); + +/** + * uipサービスタスクが実行する関数です。 + * サービスの停止を通知します。 + * この関数は他のAPIが非同期に実行されないことが保証される状況で使用する必要があります。 + */ +void NyLPC_cMiMicIpUdpSocket_stopService(NyLPC_TcMiMicIpUdpSocket_t* i_inst); + + +/** + * 定期的に実行する関数。最低でも1s単位で実行してください。 + * uipサービスタスクが実行する関数です。 + */ +#define NyLPC_cMiMicIpUdpSocket_periodic(i_inst) if((i_inst)->as_handler.periodic!=NULL){(i_inst)->as_handler.periodic((NyLPC_TiUdpSocket_t*)(i_inst));} +#ifdef __cplusplus +} +#endif /* __cplusplus */ + + +#endif /* NYLPC_CUDPSOCKET_H_ */ + diff --git a/lib/src/os/NyLPC_cIsr.h b/lib/src/os/NyLPC_cIsr.h index 223bcf1..d21fa70 100644 --- a/lib/src/os/NyLPC_cIsr.h +++ b/lib/src/os/NyLPC_cIsr.h @@ -35,7 +35,7 @@ #include "semphr.h" #elif NyLPC_ARCH==NyLPC_ARCH_MBEDRTOS #elif NyLPC_ARCH==NyLPC_ARCH_WIN32 - //not work! + //not work! #else #error Bad NyLPC_ARCH! #endif diff --git a/lib/src/os/NyLPC_cMutex.h b/lib/src/os/NyLPC_cMutex.h index 3677b00..4e18a82 100644 --- a/lib/src/os/NyLPC_cMutex.h +++ b/lib/src/os/NyLPC_cMutex.h @@ -35,7 +35,7 @@ #include "semphr.h" #elif NyLPC_ARCH==NyLPC_ARCH_MBEDRTOS #elif NyLPC_ARCH==NyLPC_ARCH_WIN32 - //not implemented! + //not implemented! #else #error Bad NyLPC_ARCH! #endif @@ -68,7 +68,7 @@ struct NyLPC_TcMutex //not work! struct NyLPC_TcMutex { - int dummy; + int dummy; }; #endif diff --git a/lib/src/os/NyLPC_cSemaphore.h b/lib/src/os/NyLPC_cSemaphore.h index e6f0e5e..7042748 100644 --- a/lib/src/os/NyLPC_cSemaphore.h +++ b/lib/src/os/NyLPC_cSemaphore.h @@ -35,7 +35,7 @@ #include "semphr.h" #elif NyLPC_ARCH==NyLPC_ARCH_MBEDRTOS #elif NyLPC_ARCH==NyLPC_ARCH_WIN32 - //not work! + //not work! #else #error Bad NyLPC_ARCH! #endif diff --git a/lib/src/os/NyLPC_cThread.h b/lib/src/os/NyLPC_cThread.h index 1540a14..10964f4 100644 --- a/lib/src/os/NyLPC_cThread.h +++ b/lib/src/os/NyLPC_cThread.h @@ -35,7 +35,7 @@ #include "task.h" #elif NyLPC_ARCH==NyLPC_ARCH_MBEDRTOS #elif NyLPC_ARCH==NyLPC_ARCH_WIN32 - //not work! + //not work! #else #error Bad NyLPC_ARCH! #endif @@ -91,7 +91,7 @@ struct NyLPC_TcThread //this is dummy definition. struct NyLPC_TcThread { - int dummy; + int dummy; NyLPC_TcThread_ThreadFunc _func; }; #else diff --git a/lib/src/os/mbedrtos/NyLPC_cIsr.cpp b/lib/src/os/mbedrtos/NyLPC_cIsr.cpp new file mode 100644 index 0000000..d8ac136 --- /dev/null +++ b/lib/src/os/mbedrtos/NyLPC_cIsr.cpp @@ -0,0 +1,70 @@ +/********************************************************************************* + * PROJECT: MiMic + * -------------------------------------------------------------------------------- + * + * This file is part of MiMic + * Copyright (C)2011 Ryo Iizuka + * + * MiMic is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + * + * For further information please contact. + * http://nyatla.jp/ + * or + * + *********************************************************************************/ +#include "../NyLPC_cIsr.h" +#if NyLPC_ARCH==NyLPC_ARCH_MBEDRTOS +#include "mbed.h" +#include "rtos.h" + +void NyLPC_cIsr_enterCritical(void) +{ + __disable_irq(); +} + +/** + * 全ての割込みとタスクスイッチを再開します。 + */ +void NyLPC_cIsr_exitCritical(void) +{ + __enable_irq(); +} + +static NyLPC_cIsr_EventHandler _eth_irs=NULL; +void NyLPC_cIsr_setEnetISR(NyLPC_cIsr_EventHandler i_handler) +{ + _eth_irs=i_handler; +} + +//LPC176xとLPC4088の場合 +#if NyLPC_MCU==NyLPC_MCU_LPC4088 || NyLPC_MCU==NyLPC_MCU_LPC17xx +/** + * 割込み解除を通知するセマフォ。lEMACInitで設定する。 + */ +extern "C" void ENET_IRQHandler(void) +{ + unsigned long ulStatus; + ulStatus = LPC_EMAC->IntStatus; + /* Clear the interrupt. */ + LPC_EMAC->IntClear = ulStatus; + if(_eth_irs!=NULL){ + _eth_irs(ulStatus); + } +} +#elif NyLPC_MCU==NyLPC_MCU_K64F +//F64Fのイーサネットハンドラはドライバに書いてあります。 +#else +#error "BAD MCU" +#endif +#endif diff --git a/lib/src/os/mbedrtos/NyLPC_cMutex.cpp b/lib/src/os/mbedrtos/NyLPC_cMutex.cpp new file mode 100644 index 0000000..9ffb604 --- /dev/null +++ b/lib/src/os/mbedrtos/NyLPC_cMutex.cpp @@ -0,0 +1,51 @@ +/********************************************************************************* + * PROJECT: MiMic + * -------------------------------------------------------------------------------- + * + * This file is part of MiMic + * Copyright (C)2011 Ryo Iizuka + * + * MiMic is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + * + * For further information please contact. + * http://nyatla.jp/ + * or + * + *********************************************************************************/ +#include "../NyLPC_cMutex.h" + +#if NyLPC_ARCH==NyLPC_ARCH_MBEDRTOS +#include "mbed.h" +#include "rtos.h" + +NyLPC_TBool NyLPC_cMutex_initialize(NyLPC_TcMutex_t* i_inst) +{ + i_inst->_lock_count=0; + i_inst->_mutex=new Mutex(); + return NyLPC_TBool_TRUE; +} +NyLPC_TBool NyLPC_cMutex_lock(NyLPC_TcMutex_t* i_inst) +{ + i_inst->_lock_count++; + ((Mutex*)(i_inst->_mutex))->lock(); + return NyLPC_TBool_TRUE; +} +NyLPC_TBool NyLPC_cMutex_unlock(NyLPC_TcMutex_t* i_inst) +{ + NyLPC_Assert(i_inst->_lock_count>0); + i_inst->_lock_count--; + ((Mutex*)(i_inst->_mutex))->unlock(); + return NyLPC_TBool_TRUE; +} +#endif \ No newline at end of file diff --git a/lib/src/os/mbedrtos/NyLPC_cSemaphore.cpp b/lib/src/os/mbedrtos/NyLPC_cSemaphore.cpp new file mode 100644 index 0000000..84babcb --- /dev/null +++ b/lib/src/os/mbedrtos/NyLPC_cSemaphore.cpp @@ -0,0 +1,51 @@ +/********************************************************************************* + * PROJECT: MiMic + * -------------------------------------------------------------------------------- + * + * This file is part of MiMic + * Copyright (C)2011 Ryo Iizuka + * + * MiMic is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + * + * For further information please contact. + * http://nyatla.jp/ + * or + * + *********************************************************************************/ +#include "../NyLPC_cSemaphore.h" + +#if NyLPC_ARCH==NyLPC_ARCH_MBEDRTOS +#include "mbed.h" +#include "rtos.h" + +void NyLPC_cSemaphore_initialize(NyLPC_TcSemaphore_t* i_inst) +{ + i_inst->_handle=new Semaphore(1); +} + + +void NyLPC_cSemaphore_giveFromISR(const NyLPC_TcSemaphore_t* i_inst) +{ + //セマフォブロックの解除 + ((Semaphore*)(i_inst->_handle))->release(); +} +/** + * @param i_timeout + * タイムアウト時間はms指定 + */ +void NyLPC_cSemaphore_take(const NyLPC_TcSemaphore_t* i_inst,NyLPC_TUInt32 i_timeout) +{ + ((Semaphore*)(i_inst->_handle))->wait(i_timeout); +} +#endif diff --git a/lib/src/os/mbedrtos/NyLPC_cStopwatch.cpp b/lib/src/os/mbedrtos/NyLPC_cStopwatch.cpp new file mode 100644 index 0000000..4b69f6b --- /dev/null +++ b/lib/src/os/mbedrtos/NyLPC_cStopwatch.cpp @@ -0,0 +1,99 @@ +/********************************************************************************* + * PROJECT: MiMic + * -------------------------------------------------------------------------------- + * + * This file is part of MiMic + * Copyright (C)2011 Ryo Iizuka + * + * MiMic is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + * + * For further information please contact. + * http://nyatla.jp/ + * or + * + *********************************************************************************/ +#include "../NyLPC_cStopwatch.h" +#if NyLPC_ARCH==NyLPC_ARCH_MBEDRTOS +#include "mbed.h" +#include "rtos.h" + +static Timer _timer; +static NyLPC_TBool _is_start=NyLPC_TBool_FALSE; +/** + * 現在のtickCountを返します。 + */ +NyLPC_TUInt32 NyLPC_cStopwatch_now(void) +{ + return (NyLPC_TUInt32)(_timer. read_ms()); +} + +/** + * インスタンスを生成します。 + */ +void NyLPC_cStopwatch_initialize(NyLPC_TcStopwatch_t* i_inst) +{ + if(!_is_start){ + _timer.start(); + _is_start=NyLPC_TBool_TRUE; + } + return; +} +/** + * 基準時刻をセットします。 + * 値は、システム依存のtick値です。 + */ +void NyLPC_cStopwatch_set(NyLPC_TcStopwatch_t* i_inst,NyLPC_TUInt32 i_initial) +{ + i_inst->_tick=i_initial; + return; +} + +/** + * 基準時刻に、現在時刻をセットします。 + */ +void NyLPC_cStopwatch_setNow(NyLPC_TcStopwatch_t* i_inst) +{ + i_inst->_tick=NyLPC_cStopwatch_now(); +} +/** + * タイムアウト計測を開始します。 + * この関数は、基準時刻に現在の時刻をセットします。 + */ +void NyLPC_cStopwatch_startExpire(NyLPC_TcStopwatch_t* i_inst,NyLPC_TUInt32 i_timeout) +{ + NyLPC_cStopwatch_setNow(i_inst); + i_inst->_ex_timeout=i_timeout; +} + +/** + * NyLPC_cStopwatch_startExpireで設定した時間を経過したかを返します。 + */ +NyLPC_TBool NyLPC_cStopwatch_isExpired(NyLPC_TcStopwatch_t* i_inst) +{ + //経過時間の判定 + if(NyLPC_cStopwatch_elapseInMsec(i_inst)>i_inst->_ex_timeout){ + i_inst->_ex_timeout=0; + } + return (i_inst->_ex_timeout==0); +} + +/** + * 基準時刻と現在時刻の差を計算して、経過時間をミリ秒で返します。 + */ +NyLPC_TUInt32 NyLPC_cStopwatch_elapseInMsec(const NyLPC_TcStopwatch_t* i_inst) +{ + return (NyLPC_cStopwatch_now()-i_inst->_tick); +} + +#endif diff --git a/lib/src/os/mbedrtos/NyLPC_cThread.cpp b/lib/src/os/mbedrtos/NyLPC_cThread.cpp new file mode 100644 index 0000000..dfd360a --- /dev/null +++ b/lib/src/os/mbedrtos/NyLPC_cThread.cpp @@ -0,0 +1,90 @@ +/********************************************************************************* + * PROJECT: MiMic + * -------------------------------------------------------------------------------- + * + * This file is part of MiMic + * Copyright (C)2011 Ryo Iizuka + * + * MiMic is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + * + * For further information please contact. + * http://nyatla.jp/ + * or + * + *********************************************************************************/ +#include "../NyLPC_cThread.h" + +#if NyLPC_ARCH==NyLPC_ARCH_MBEDRTOS +#include "mbed.h" +#include "rtos.h" + +static osPriority prio_table[]={ + osPriorityNormal,osPriorityHigh}; + + + +static void proc(void const *argument) +{ + NyLPC_TcThread_t* t=(NyLPC_TcThread_t*)argument; + for(;;){ + do{ + Thread::wait(30);// danger wait! + }while(NyLPC_TUInt32_isBitOn(t->_sbit,NyLPC_TcThread_BIT_IS_TERMINATED)); + t->_func(t->_arg); + NyLPC_TUInt32_setBit(t->_sbit,NyLPC_TcThread_BIT_IS_TERMINATED); + } +} + + +void NyLPC_cThread_initialize(NyLPC_TcThread_t* i_inst,NyLPC_TInt32 i_stack,NyLPC_TInt32 i_prio) +{ + NyLPC_TUInt32_setBit(i_inst->_sbit,NyLPC_TcThread_BIT_IS_TERMINATED); + i_inst->_thread=new Thread(proc,i_inst,prio_table[i_prio],i_stack); +} +void NyLPC_cThread_finalize(NyLPC_TcThread_t* i_inst) +{ + NyLPC_cThread_join(i_inst); + delete (Thread*)(i_inst->_thread); +} + + +void NyLPC_cThread_start(NyLPC_TcThread_t* i_inst,NyLPC_TcThread_ThreadFunc i_func,void* i_param) +{ + NyLPC_ArgAssert(i_inst!=NULL); + NyLPC_ArgAssert(i_func!=NULL); + i_inst->_sbit=0; + i_inst->_func=i_func; + i_inst->_arg=i_param; + return; +} +void NyLPC_cThread_join(NyLPC_TcThread_t* i_inst) +{ + NyLPC_TUInt32_setBit(i_inst->_sbit,NyLPC_TcThread_BIT_IS_JOIN_REQ); + while(!NyLPC_TUInt32_isBitOn(i_inst->_sbit,NyLPC_TcThread_BIT_IS_TERMINATED)) + { + Thread::wait(10); + } + return; +} +void NyLPC_cThread_sleep(NyLPC_TUInt32 i_time_in_msec) +{ + //ミリ秒単位で待つ + Thread::wait(i_time_in_msec); +} +void NyLPC_cThread_yield(void) +{ + Thread::yield(); +} + +#endif diff --git a/lib/src/supporter/NyLPC_cSupporter.c b/lib/src/supporter/NyLPC_cSupporter.c index dd7c90c..163ae87 100644 --- a/lib/src/supporter/NyLPC_cSupporter.c +++ b/lib/src/supporter/NyLPC_cSupporter.c @@ -1 +1,2 @@ #include "NyLPC_cSupporter.h" + diff --git a/lib/src/uip/NyLPC_IPv6Icmp6.c b/lib/src/uip/NyLPC_IPv6Icmp6.c deleted file mode 100644 index f5241b4..0000000 --- a/lib/src/uip/NyLPC_IPv6Icmp6.c +++ /dev/null @@ -1,157 +0,0 @@ -/********************************************************************************* - * PROJECT: MiMic - * -------------------------------------------------------------------------------- - * - * This file is part of MiMic - * Copyright (C)2011 Ryo Iizuka - * - * MiMic is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published - * by the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program. If not, see . - * - * For further information please contact. - * http://nyatla.jp/ - * or - * - * - * Parts of this file were leveraged from uIP: - * - * Copyright (c) 2001-2003, Adam Dunkels. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote - * products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS - * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY - * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE - * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ -#define ICMP6_ECHO_REPLY 129 -#define ICMP6_ECHO 128 -#define ICMP6_NEIGHBOR_SOLICITATION 135 -#define ICMP6_NEIGHBOR_ADVERTISEMENT 136 - -#define ICMP6_FLAG_S (1 << 6) - -#define ICMP6_OPTION_SOURCE_LINK_ADDRESS 1 -#define ICMP6_OPTION_TARGET_LINK_ADDRESS 2 - -void uip_process_ICMP6( - const void* in_packet) -{ -#if UIP_CONF_IPV6 - - /* This is IPv6 ICMPv6 processing code. */ - DEBUG_PRINTF("icmp6_input: length %d\n", uip_len); - - - UIP_STAT(++uip_stat.icmp.recv); - - /* If we get a neighbor solicitation for our address we should send - a neighbor advertisement message back. */ - if(ICMPBUF->type == ICMP6_NEIGHBOR_SOLICITATION) { - if(uip_ipaddr_cmp(ICMPBUF->icmp6data, uip_hostaddr)) { - - if(ICMPBUF->options[0] == ICMP6_OPTION_SOURCE_LINK_ADDRESS) { - /* Save the sender's address in our neighbor list. */ - uip_neighbor_add(ICMPBUF->srcipaddr, &(ICMPBUF->options[2])); - } - - /* We should now send a neighbor advertisement back to where the - neighbor solicication came from. */ - ICMPBUF->type = ICMP6_NEIGHBOR_ADVERTISEMENT; - ICMPBUF->flags = ICMP6_FLAG_S; /* Solicited flag. */ - - ICMPBUF->reserved1 = ICMPBUF->reserved2 = ICMPBUF->reserved3 = 0; - - uip_ipaddr_copy(ICMPBUF->destipaddr, ICMPBUF->srcipaddr); - uip_ipaddr_copy(ICMPBUF->srcipaddr, uip_hostaddr); - ICMPBUF->options[0] = ICMP6_OPTION_TARGET_LINK_ADDRESS; - ICMPBUF->options[1] = 1; /* Options length, 1 = 8 bytes. */ - memcpy(&(ICMPBUF->options[2]), &uip_ethaddr, sizeof(uip_ethaddr)); - ICMPBUF->icmpchksum = 0; - ICMPBUF->icmpchksum = ~uip_icmp6chksum(); - goto send; - - } - uip_func_drop(); - return; - - } else if(ICMPBUF->type == ICMP6_ECHO) { - /* ICMP echo (i.e., ping) processing. This is simple, we only - change the ICMP type from ECHO to ECHO_REPLY and update the - ICMP checksum before we return the packet. */ - - ICMPBUF->type = ICMP6_ECHO_REPLY; - - uip_ipaddr_copy(BUF->destipaddr, BUF->srcipaddr); - uip_ipaddr_copy(BUF->srcipaddr, uip_hostaddr); - ICMPBUF->icmpchksum = 0; - ICMPBUF->icmpchksum = ~uip_icmp6chksum(); - - UIP_STAT(++uip_stat.icmp.sent); - goto send; - } else { - DEBUG_PRINTF("Unknown icmp6 message type %d\n", ICMPBUF->type); - UIP_STAT(++uip_stat.icmp.drop); - UIP_STAT(++uip_stat.icmp.typeerr); - UIP_LOG("icmp: unknown ICMP message."); - uip_func_drop(); - return; - - } -#endif -} - - - - - - -void uip_icmp6_v6(void) -{ -// if (uip_ipaddr_cmp(uip_hostaddr, all_zeroes_addr)) { - /* If we are configured to use ping IP address configuration and - hasn't been assigned an IP address yet, we accept all ICMP - packets. */ - - -// } else { - /* For IPv6, packet reception is a little trickier as we need to - make sure that we listen to certain multicast addresses (all - hosts multicast address, and the solicited-node multicast - address) as well. However, we will cheat here and accept all - multicast packets that are sent to the ff02::/16 addresses. */ -/* if(!uip_ipaddr_cmp(BUF->destipaddr, uip_hostaddr) && - BUF->destipaddr[0] != NyLPC_HTONS(0xff02)) { - UIP_STAT(++uip_stat.header.drop); - goto drop; - } -*/// } -} diff --git a/lib/src/uip/NyLPC_cBaseSocket.c b/lib/src/uip/NyLPC_cBaseSocket.c deleted file mode 100644 index 632aa1a..0000000 --- a/lib/src/uip/NyLPC_cBaseSocket.c +++ /dev/null @@ -1,9 +0,0 @@ -#include "NyLPC_cBaseSocket.h" -#include "NyLPC_cUipService_protected.h" - -void NyLPC_cBaseSocket_initialize(NyLPC_TcBaseSocket_t* i_inst,NyLPC_TUInt8 i_typeid) -{ - NyLPC_TcUipService_t* srv=_NyLPC_TcUipService_inst; - i_inst->_typeid=i_typeid; - i_inst->_parent_ipv4=&(srv->_tcpv4); -} diff --git a/lib/src/uip/NyLPC_cBaseSocket.h b/lib/src/uip/NyLPC_cBaseSocket.h deleted file mode 100644 index 01a3c90..0000000 --- a/lib/src/uip/NyLPC_cBaseSocket.h +++ /dev/null @@ -1,63 +0,0 @@ -/********************************************************************************* - * PROJECT: MiMic - * -------------------------------------------------------------------------------- - * - * This file is part of MiMic - * Copyright (C)2011 Ryo Iizuka - * - * MiMic is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published - * by the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program. If not, see . - * - * For further information please contact. - * http://nyatla.jp/ - * or - * - *********************************************************************************/ -#ifndef NYLPC_CBASESOCKET_H_ -#define NYLPC_CBASESOCKET_H_ -#ifdef __cplusplus -extern "C" { -#endif /* __cplusplus */ - -#include "NyLPC_stdlib.h" -#include "NyLPC_cIPv4_typedef.h" - -/** - * Base socket class - * cIPv4 classが管理するソケットオブジェクトのベースクラスです。 - */ -typedef struct NyLPC_TcBaseSocket NyLPC_TcBaseSocket_t; - -#define NyLPC_TcBaseSocket_TYPEID_UDP_SOCK 1 -#define NyLPC_TcBaseSocket_TYPEID_TCP_SOCK 2 -#define NyLPC_TcBaseSocket_TYPEID_TCP_LISTENER 3 - -struct NyLPC_TcBaseSocket -{ - /**タイプID 継承クラスのinitializerで設定。 */ - NyLPC_TUInt8 _typeid; - NyLPC_TUInt8 _padding8; - NyLPC_TUInt16 _padding16; - /** 所属してるIPv4コンローラ*/ - NyLPC_TcIPv4_t* _parent_ipv4; -}; -void NyLPC_cBaseSocket_initialize(NyLPC_TcBaseSocket_t* i_inst,NyLPC_TUInt8 i_typeid); -#define NyLPC_cBaseSocket_finalize(i_inst) - - - -#ifdef __cplusplus -} -#endif /* __cplusplus */ - -#endif /* NYLPC_CBASESOCKET_H_ */ diff --git a/lib/src/uip/NyLPC_cIPv4.c b/lib/src/uip/NyLPC_cIPv4.c deleted file mode 100644 index acad20f..0000000 --- a/lib/src/uip/NyLPC_cIPv4.c +++ /dev/null @@ -1,539 +0,0 @@ -/********************************************************************************* - * PROJECT: MiMic - * -------------------------------------------------------------------------------- - * - * This file is part of MiMic - * Copyright (C)2011 Ryo Iizuka - * - * MiMic is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published - * by the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program. If not, see . - * - * For further information please contact. - * http://nyatla.jp/ - * or - * - * - * Parts of this file were leveraged from uIP: - * - * Copyright (c) 2001-2003, Adam Dunkels. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote - * products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS - * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY - * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE - * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ -#include "NyLPC_stdlib.h" -#include "NyLPC_uip.h" -#include "NyLPC_cIPv4.h" -#include "NyLPC_cIPv4Payload_protected.h" -#include "NyLPC_cTcpSocket_protected.h" -#include "NyLPC_cUdpSocket_protected.h" -#include "NyLPC_cTcpListener_protected.h" -#include "NyLPC_cIPv4IComp_protected.h" -#include "NyLPC_cUipService_protected.h" - - - -/**************************************************** - * Socketテーブルに関する宣言 - ***************************************************/ - -#define cSocketTbl_initialize(i_inst,buf) NyLPC_cPtrTbl_initialize(i_inst,buf,NyLPC_cIPv4_MAX_SOCKET) -#define cSocketTbl_finalize(i_inst) - -/** - * 条件に一致する、アクティブなTCPソケットオブジェクトを取得します。 - * この関数は、ローカルIPが一致していると仮定して検索をします。 - * @param i_rip - * リモートIPアドレスを指定します。 - */ -static NyLPC_TcTcpSocket_t* cSocketTbl_getMatchTcpSocket( - NyLPC_TcPtrTbl_t* i_inst, - NyLPC_TUInt16 i_lport, - struct NyLPC_TIPv4Addr i_rip, - NyLPC_TUInt16 i_rport) -{ - NyLPC_TcBaseSocket_t** p=(NyLPC_TcBaseSocket_t**)(i_inst->buf); - NyLPC_TcTcpSocket_t* tp; - int i; - //一致するポートを検索 - for(i=i_inst->size-1;i>=0;i--){ - if(p[i]==NULL || p[i]->_typeid!=NyLPC_TcBaseSocket_TYPEID_TCP_SOCK){ - continue; - } - tp=(NyLPC_TcTcpSocket_t*)p[i]; - if(tp->tcpstateflags==UIP_CLOSED){ - continue; - } - //パラメータの一致チェック - if(i_lport!=tp->uip_connr.lport || i_rport!= tp->uip_connr.rport || i_rip.v!=tp->uip_connr.ripaddr.v) - { - continue; - } - return tp; - } - return NULL; -} -static NyLPC_TcUdpSocket_t* cSocketTbl_getMatchUdpSocket( - NyLPC_TcPtrTbl_t* i_inst, - NyLPC_TUInt16 i_lport) -{ - NyLPC_TcBaseSocket_t** p=(NyLPC_TcBaseSocket_t**)(i_inst->buf); - NyLPC_TcUdpSocket_t* tp; - int i; - //一致するポートを検索 - for(i=i_inst->size-1;i>=0;i--){ - if(p[i]==NULL || p[i]->_typeid!=NyLPC_TcBaseSocket_TYPEID_UDP_SOCK){ - continue; - } - tp=(NyLPC_TcUdpSocket_t*)p[i]; - //パラメータの一致チェック - if(i_lport==tp->uip_udp_conn.lport){ - //unicast - return tp; - } - } - return NULL; -} -static NyLPC_TcUdpSocket_t* cSocketTbl_getMatchMulticastUdpSocket( - NyLPC_TcPtrTbl_t* i_inst, - const struct NyLPC_TIPv4Addr* i_mcast_ip, - NyLPC_TUInt16 i_lport) -{ - NyLPC_TcBaseSocket_t** p=(NyLPC_TcBaseSocket_t**)(i_inst->buf); - NyLPC_TcUdpSocket_t* tp; - int i; - //一致するポートを検索 - for(i=i_inst->size-1;i>=0;i--){ - if(p[i]==NULL || p[i]->_typeid!=NyLPC_TcBaseSocket_TYPEID_UDP_SOCK){ - continue; - } - tp=(NyLPC_TcUdpSocket_t*)p[i]; - //パラメータの一致チェック - if(i_lport!=tp->uip_udp_conn.lport || (!NyLPC_TIPv4Addr_isEqual(i_mcast_ip,&(tp->uip_udp_conn.mcastaddr)))) - { - continue; - } - return tp; - } - return NULL; -} - -/** - * i_port番号に一致するリスナを返します。 - */ -static NyLPC_TcTcpListener_t* cSocketTbl_getListenerByPeerPort(NyLPC_TcPtrTbl_t* i_inst,NyLPC_TUInt16 i_port) -{ - NyLPC_TcBaseSocket_t** p=(NyLPC_TcBaseSocket_t**)(i_inst->buf); - NyLPC_TcTcpListener_t* lp; - int i; - //一致するポートを検索して、acceptをコールする。 - for(i=i_inst->size-1;i>=0;i--){ - if(p[i]==NULL || p[i]->_typeid!=NyLPC_TcBaseSocket_TYPEID_TCP_LISTENER){ - continue; - } - lp=(NyLPC_TcTcpListener_t*)p[i]; - if(lp->_port!=i_port){ - continue; - } - return lp; - } - return NULL; -} -/** - * 指定番号のTCPポートが未使用かを返す。 - * @return - * i_lport番のポートが未使用であればTRUE - */ -static NyLPC_TBool cSocketTbl_isClosedTcpPort( - NyLPC_TcPtrTbl_t* i_inst, - NyLPC_TUInt16 i_lport) -{ - NyLPC_TcBaseSocket_t** p=(NyLPC_TcBaseSocket_t**)(i_inst->buf); - NyLPC_TcTcpSocket_t* tp; - int i; - //一致するポートを検索 - for(i=i_inst->size-1;i>=0;i--){ - if(p[i]==NULL){ - continue; - } - if(p[i]->_typeid!=NyLPC_TcBaseSocket_TYPEID_TCP_SOCK){ - tp=((NyLPC_TcTcpSocket_t*)p[i]); - //TCPソケット && !クローズ && ポート一致なら使用中 - if((tp->tcpstateflags!=UIP_CLOSED) && tp->uip_connr.lport==i_lport){ - return NyLPC_TBool_FALSE; - } - } - if(p[i]->_typeid!=NyLPC_TcBaseSocket_TYPEID_TCP_LISTENER){ - //Listenerソケット && ポート一致なら使用中 - if(((NyLPC_TcTcpListener_t*)p[i])->_port==i_lport){ - return NyLPC_TBool_FALSE; - } - } - } - //未使用 - return NyLPC_TBool_TRUE; -} -/** - * テーブルにある有効なソケットのperiodicをすべて呼び出します。 - */ -static void cSocketTbl_callPeriodic( - NyLPC_TcPtrTbl_t* i_inst) -{ - NyLPC_TcBaseSocket_t** p=(NyLPC_TcBaseSocket_t**)(i_inst->buf); - int i; - for(i=i_inst->size-1;i>=0;i--){ - if(p[i]==NULL){ - continue; - } - switch(p[i]->_typeid){ - case NyLPC_TcBaseSocket_TYPEID_TCP_SOCK: - //downcast! - NyLPC_cTcpSocket_periodic((NyLPC_TcTcpSocket_t*)(p[i])); - break; - case NyLPC_TcBaseSocket_TYPEID_UDP_SOCK: - NyLPC_cUdpSocket_periodic((NyLPC_TcUdpSocket_t*)(p[i])); - break; - default: - continue; - } - } -} - -/** - * テーブルにある有効なソケットのstartを全て呼び出します。 - */ -static void cSocketTbl_callSocketStart( - NyLPC_TcPtrTbl_t* i_inst, - const NyLPC_TcIPv4Config_t* i_cfg) -{ - NyLPC_TcBaseSocket_t** p=(NyLPC_TcBaseSocket_t**)(i_inst->buf); - int i; - for(i=i_inst->size-1;i>=0;i--){ - if(p[i]==NULL){ - continue; - } - switch(p[i]->_typeid){ - case NyLPC_TcBaseSocket_TYPEID_UDP_SOCK: - NyLPC_cUdpSocket_startService((NyLPC_TcUdpSocket_t*)(p[i]),i_cfg); - break; - case NyLPC_TcBaseSocket_TYPEID_TCP_SOCK: - NyLPC_cTcpSocket_startService((NyLPC_TcTcpSocket_t*)(p[i]),i_cfg); - break; - default: - continue; - } - } -} -/** - * テーブルにある有効なソケットのstartを全て呼び出します。 - */ -static void cSocketTbl_callSocketStop( - NyLPC_TcPtrTbl_t* i_inst) -{ - NyLPC_TcBaseSocket_t** p=(NyLPC_TcBaseSocket_t**)(i_inst->buf); - int i; - for(i=i_inst->size-1;i>=0;i--){ - if(p[i]==NULL){ - continue; - } - switch(p[i]->_typeid){ - case NyLPC_TcBaseSocket_TYPEID_UDP_SOCK: - NyLPC_cUdpSocket_stopService((NyLPC_TcUdpSocket_t*)(p[i])); - break; - case NyLPC_TcBaseSocket_TYPEID_TCP_SOCK: - NyLPC_cTcpSocket_stopService((NyLPC_TcTcpSocket_t*)(p[i])); - break; - default: - continue; - } - } -} - -/**************************************************** - * NyLPC_cIPv4 - ***************************************************/ - -/** - * Static関数 - */ - -static void* tcp_rx( - NyLPC_TcIPv4_t* i_inst, - const NyLPC_TcIPv4Payload_t* i_ipp); - -static NyLPC_TBool udp_rx( - NyLPC_TcIPv4_t* i_inst, - NyLPC_TcIPv4Payload_t* i_ipp); - -/** - * See Header file. - */ -void NyLPC_cIPv4_initialize( - NyLPC_TcIPv4_t* i_inst) -{ - //IP制御パケットの為に40バイト以上のシステムTXメモリが必要。 - NyLPC_ArgAssert(NyLPC_cUipService_SYS_TX_BUF_SIZE>40); - //内部テーブルの初期化 - cSocketTbl_initialize(&(i_inst->_socket_tbl),(void**)(i_inst->_socket_array_buf)); - //instanceの初期化 - NyLPC_cMutex_initialize(&(i_inst->_sock_mutex)); - NyLPC_cMutex_initialize(&(i_inst->_listener_mutex)); - i_inst->tcp_port_counter=0; - i_inst->_ref_config=NULL; - return; -} - -/** - * See header file. - */ -void NyLPC_cIPv4_finalize( - NyLPC_TcIPv4_t* i_inst) -{ - cSocketTbl_finalize(&(i_inst->_socket_tbl)); - NyLPC_cMutex_finalize(&(i_inst->_sock_mutex)); - NyLPC_cMutex_finalize(&(i_inst->_listener_mutex)); - return; -} - -/** - * See header file. - */ -void NyLPC_cIPv4_start( - NyLPC_TcIPv4_t* i_inst, - const NyLPC_TcIPv4Config_t* i_ref_configlation) -{ - NyLPC_ArgAssert(i_ref_configlation!=NULL); - //リストの初期化、ここでするべき?しないべき? - i_inst->_ref_config=i_ref_configlation; - //configulationのアップデートを登録されてるソケットに通知 - cSocketTbl_callSocketStart(&(i_inst->_socket_tbl),i_ref_configlation); - return; -} - -/** - * See header file. - */ -void NyLPC_cIPv4_stop( - NyLPC_TcIPv4_t* i_inst) -{ - cSocketTbl_callSocketStop(&(i_inst->_socket_tbl)); - i_inst->_ref_config=NULL; - return; -} - -/** - * See header file. - */ -NyLPC_TBool NyLPC_cIPv4_addSocket( - NyLPC_TcIPv4_t* i_inst, - NyLPC_TcBaseSocket_t* i_sock) -{ - //当面、stop中しか成功しない。 - NyLPC_Assert(!NyLPC_cUipService_isRun()); - return NyLPC_cPtrTbl_add(&(i_inst->_socket_tbl),i_sock)>=0; -} - -/** - * See header file. - */ -NyLPC_TBool NyLPC_cIPv4_removeSocket( - NyLPC_TcIPv4_t* i_inst, - NyLPC_TcBaseSocket_t* i_sock) -{ - NyLPC_TInt16 i; - NyLPC_Assert(!NyLPC_cUipService_isRun()); - i=NyLPC_cPtrTbl_getIndex(&(i_inst->_socket_tbl),i_sock); - if(i>=0){ - NyLPC_cPtrTbl_remove(&(i_inst->_socket_tbl),i); - return NyLPC_TBool_TRUE; - } - return NyLPC_TBool_FALSE; -} - - -#define IS_START(i_inst) ((i_inst)->_ref_config!=NULL) - -/** - * 稼動時に、1s置きに呼び出す関数です。 - */ -void NyLPC_cIPv4_periodec(NyLPC_TcIPv4_t* i_inst) -{ - cSocketTbl_callPeriodic(&(i_inst->_socket_tbl)); -} - - -/** - * IPv4ペイロードを処理する関数。 - * この関数は、パケット受信タスクから実行します。 - * @param i_rx - * 先頭ポインタ。 - * @return - * TRUEなら、i_rxに応答パケットをセットして返します。 - */ -void* NyLPC_cIPv4_rx(NyLPC_TcIPv4_t* i_inst,const void* i_rx,NyLPC_TUInt16 i_rx_size) -{ - NyLPC_TcUipService_t* inst=_NyLPC_TcUipService_inst; - NyLPC_TcIPv4Payload_t ipv4; - //NOT開始状態なら受け付けないよ。 - if(!IS_START(i_inst)){ - NyLPC_OnErrorGoto(ERROR_DROP); - } - - NyLPC_cIPv4Payload_initialize(&ipv4); - //IPフラグメントを読出し用にセットする。 - if(!NyLPC_cIPv4Payload_attachRxBuf(&ipv4,i_rx,i_rx_size)) - { - NyLPC_OnErrorGoto(ERROR_DROP); - } - switch(ipv4.header->proto) - { - case UIP_PROTO_TCP: - //TCP受信処理 - return tcp_rx(i_inst,&ipv4); - case UIP_PROTO_UDP: - //UDP処理 - udp_rx(i_inst,&ipv4);//r - return NyLPC_TBool_FALSE; - case UIP_PROTO_ICMP: - return NyLPC_cIPv4IComp_rx(&(inst->_icomp),&ipv4); - } - return NULL; -ERROR_DROP: - return NULL; -} - -NyLPC_TUInt16 NyLPC_cIPv4_getNewPortNumber(NyLPC_TcIPv4_t* i_inst) -{ - NyLPC_TUInt16 i,n; - for(i=0;i<0x0fff;i--){ - i_inst->tcp_port_counter=(i_inst->tcp_port_counter+1)%0x0fff; - n=i_inst->tcp_port_counter+49152; - if(cSocketTbl_isClosedTcpPort(&i_inst->_socket_tbl,n)) - { - return n; - } - } - return 0; -} - - -/********************************************************************** - * - * packet handler - * - **********************************************************************/ - - -static void* tcp_rx( - NyLPC_TcIPv4_t* i_inst, - const NyLPC_TcIPv4Payload_t* i_ipp) -{ - NyLPC_TcTcpSocket_t* sock; - NyLPC_TcTcpListener_t* listener; - - //自分自身のIPに対する呼び出し? - if(!NyLPC_TIPv4Addr_isEqual(&(i_ipp->header->destipaddr),&(i_inst->_ref_config->ip_addr))) - { - //自分以外のパケットはドロップ - goto DROP; - } - //チェックサムの計算 - if((NyLPC_TIPv4Header_makeTcpChecksum(i_ipp->header) != 0xffff)) - { - //受信エラーのあるパケットはドロップ - goto DROP; - } - //アクティブなTCPソケットを探す。 - sock=cSocketTbl_getMatchTcpSocket(&(i_inst->_socket_tbl),i_ipp->payload.tcp->destport,i_ipp->header->srcipaddr,i_ipp->payload.tcp->srcport); - if(sock!=NULL) - { - //既存の接続を処理 - return NyLPC_cTcpSocket_parseRx(sock,i_ipp); - } - - //未知の接続 - if(!NyLPC_cPtrTbl_hasEmpty(&(i_inst->_socket_tbl))){ - //ソケットテーブルが不十分。RST送信 - return NyLPC_cTcpSocket_allocTcpReverseRstAck(i_ipp); - } - //このポートに対応したListenerを得る。 - listener=cSocketTbl_getListenerByPeerPort(&(i_inst->_socket_tbl),i_ipp->payload.tcp->destport); - if(listener==NULL){ - //Listen対象ではない。RST送信 - return NyLPC_cTcpSocket_allocTcpReverseRstAck(i_ipp); - } - //リスナにソケットのバインドを依頼する。 - NyLPC_cTcpListener_synPacket(listener,i_ipp); - return NULL;//LISTEN成功。送信データなし -DROP: - return NULL; -} - - - -static NyLPC_TBool udp_rx( - NyLPC_TcIPv4_t* i_inst, - NyLPC_TcIPv4Payload_t* i_ipp) -{ - NyLPC_TcUdpSocket_t* sock=NULL; - if(!NyLPC_TIPv4Addr_isEqual(&(i_ipp->header->destipaddr),&(i_inst->_ref_config->ip_addr))) - { - sock=cSocketTbl_getMatchUdpSocket(&(i_inst->_socket_tbl),i_ipp->payload.udp->destport); - }else{ - if(NyLPC_TIPv4Addr_isEqualWithMask(&(i_ipp->header->destipaddr),&NyLPC_TIPv4Addr_MULTICAST,&NyLPC_TIPv4Addr_MULTICAST_MASK)){ - //MultiCast? - //マルチキャストに参加している&&portの一致するソケットを検索 - sock=cSocketTbl_getMatchMulticastUdpSocket(&(i_inst->_socket_tbl),&(i_ipp->header->destipaddr),i_ipp->payload.udp->destport); - }else if(!NyLPC_TIPv4Addr_isEqual(&(i_ipp->header->destipaddr),&NyLPC_TIPv4Addr_BROADCAST)){ - //Broadcast? - sock=cSocketTbl_getMatchUdpSocket(&(i_inst->_socket_tbl),i_ipp->payload.udp->destport); - } - } - if(sock==NULL) - { - goto DROP; - } - //パケットのエラーチェック - if((NyLPC_TIPv4Header_makeTcpChecksum(i_ipp->header) != 0xffff)) - { - //受信エラーのあるパケットはドロップ - goto DROP; - } - //既存の接続を処理 - return NyLPC_cUdpSocket_parseRx(sock,i_ipp); -DROP: - return NyLPC_TBool_FALSE; -} - - diff --git a/lib/src/uip/NyLPC_cIPv4_typedef.h b/lib/src/uip/NyLPC_cIPv4_typedef.h deleted file mode 100644 index 536daa1..0000000 --- a/lib/src/uip/NyLPC_cIPv4_typedef.h +++ /dev/null @@ -1,16 +0,0 @@ -#ifndef NYLPC_CIPV4_TYPEDEF_H_ -#define NYLPC_CIPV4_TYPEDEF_H_ - -/** - * クラス型を定義します。 - * NyLPC_cIPv4クラスは、NyLPC_cUipServiceクラスの一部として働きます。 - * 通常ユーザが操作することはありません。 - * IPv4における、ソケットクラス(NyLPC_cTcpSocketとNyLPC_cTcpListener)の管理を担当します。 - * クラスのインスタンスは、NyLPC_cUipServiceのインスタンスにより生成されます。 - * インスタンスは2つのポインタリストをもち、ここにこれらのインスタンスを登録します。 - * インスタンスは、NyLPC_cUipServiceから送られてきた受信パケットを、登録されているソケットクラスに - * ディスパッチする機能を持ちます。 - */ -typedef struct NyLPC_TcIPv4 NyLPC_TcIPv4_t; - -#endif /* NYLPC_CIPV4_PUBLIC_H_ */ diff --git a/lib/src/uip/NyLPC_cIPv6Tcp.c b/lib/src/uip/NyLPC_cIPv6Tcp.c deleted file mode 100644 index 8eeffb9..0000000 --- a/lib/src/uip/NyLPC_cIPv6Tcp.c +++ /dev/null @@ -1,176 +0,0 @@ -/********************************************************************************* - * PROJECT: MiMic - * -------------------------------------------------------------------------------- - * - * This file is part of MiMic - * Copyright (C)2011 Ryo Iizuka - * - * MiMic is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published - * by the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program. If not, see . - * - * For further information please contact. - * http://nyatla.jp/ - * or - * - * - * Parts of this file were leveraged from uIP: - * - * Copyright (c) 2001-2003, Adam Dunkels. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote - * products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS - * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY - * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE - * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ -#include "NyLPC_uip.h" -/* Header sizes. */ -#if UIP_CONF_IPV6 -#define UIP_IPH_LEN 40 -#else /* UIP_CONF_IPV6 */ -#define UIP_IPH_LEN 20 /* Size of IP header */ -#endif /* UIP_CONF_IPV6 */ -#define UIP_UDPH_LEN 8 /* Size of UDP header */ -#define UIP_TCPH_LEN 20 /* Size of TCP header */ -#define UIP_IPUDPH_LEN (UIP_UDPH_LEN + UIP_IPH_LEN) /* Size of IP + - UDP - header */ -#define UIP_IPTCPH_LEN (UIP_TCPH_LEN + UIP_IPH_LEN) /* Size of IP + - TCP - header */ -#define UIP_TCPIP_HLEN UIP_IPTCPH_LEN - - -#if !UIP_CONF_IPV6 - -#else /* !UIP_CONF_IPV6 */ -#define uip_ipaddr_copy(dest, src) memcpy(dest, src, sizeof(uip_ip6addr_t)) -#endif /* !UIP_CONF_IPV6 */ - -#define uip_ip6addr(addr, addr0,addr1,addr2,addr3,addr4,addr5,addr6,addr7) do { \ - ((u16_t *)(addr))[0] = NyLPC_HTONS((addr0)); \ - ((u16_t *)(addr))[1] = NyLPC_HTONS((addr1)); \ - ((u16_t *)(addr))[2] = NyLPC_HTONS((addr2)); \ - ((u16_t *)(addr))[3] = NyLPC_HTONS((addr3)); \ - ((u16_t *)(addr))[4] = NyLPC_HTONS((addr4)); \ - ((u16_t *)(addr))[5] = NyLPC_HTONS((addr5)); \ - ((u16_t *)(addr))[6] = NyLPC_HTONS((addr6)); \ - ((u16_t *)(addr))[7] = NyLPC_HTONS((addr7)); \ - } while(0) - -/*---------------------------------------------------------------------------*/ -#if UIP_CONF_IPV6 -NyLPC_TUInt16 -uip_icmp6chksum(void) -{ - return upper_layer_chksum(UIP_PROTO_ICMP6); - -} - -/*---------------------------------------------------------------------------*/ - -/** - * チェックサムは、TCP疑似ヘッダから計算。 - * 送信元IPアドレス+あて先/もどりIPアドレス+ProtocolID+パケット長 - */ -static NyLPC_TUInt16 upper_layer_chksum(NyLPC_TUInt8 proto,NyLPC_TUInt16 i_len_of_data) -{ - NyLPC_TUInt16 sum; - -#if UIP_CONF_IPV6 - i_len_of_data = i_len_of_data; -#else /* UIP_CONF_IPV6 */ - i_len_of_data = i_len_of_data- UIP_IPH_LEN; -#endif /* UIP_CONF_IPV6 */ - - /* First sum pseudoheader. */ - - /* IP protocol and length fields. This addition cannot carry. */ - sum = i_len_of_data + proto; - /* Sum IP source and destination addresses. */ -// sum = chksum(sum, (u8_t *) &(BUF->srcipaddr), 2 * sizeof(uip_ipaddr_t)); - - /* Sum TCP header and data. */ -// sum = chksum(sum, &uip_buf[UIP_IPH_LEN + UIP_LLH_LEN], i_len_of_data); - - return (sum == 0) ? 0xffff : NyLPC_htons(sum); -} - - -void uip_process_ip6(const struct NyLPC_TIPv4Payload* i_rx,NyLPC_TUInt16 i_payload_size) -{/* - u16_t tmp; - - //IPパケットのバージョンチェック - if((i_rx->header->vhl & 0xf0)!=0x60){ - return;//drop; - } - if(NyLPC_ntohs(i_rx->->len16) <= uip_len){ - uip_len += 40; - /* The length reported in the IPv6 header is the - length of the payload that follows the - header. However, uIP uses the uip_len variable - for holding the size of the entire packet, - including the IP header. For IPv4 this is not a - problem as the length field in the IPv4 header - contains the length of the entire packet. But - for IPv6 we need to add the size of the IPv6 - header (40 bytes). *//* - }else{ - return;//drop; - } - switch(in.v6.header->proto) - { - case UIP_PROTO_TCP: - case UIP_PROTO_UDP: - // //uip_process_UDP();//実装したら解除いして - return; - case UIP_PROTO_ICMP6: - // uip_process_ICMP6(&in_packet,&out_packet); - return; - } - break;//drop*/ -} -void uip_tcp_v6(void) -{ - /* For IPv6, packet reception is a little trickier as we need to - make sure that we listen to certain multicast addresses (all - hosts multicast address, and the solicited-node multicast - address) as well. However, we will cheat here and accept all - multicast packets that are sent to the ff02::/16 addresses. */ -/* if(!uip_ipaddr_cmp(BUF->destipaddr, uip_hostaddr) && - BUF->destipaddr[0] != NyLPC_HTONS(0xff02)) { - UIP_STAT(++uip_stat.header.drop); - goto drop; - }*/ -} -#endif /* UIP_CONF_IPV6 */ diff --git a/lib/src/uip/NyLPC_cIPv6Udp.c b/lib/src/uip/NyLPC_cIPv6Udp.c deleted file mode 100644 index aefb5ec..0000000 --- a/lib/src/uip/NyLPC_cIPv6Udp.c +++ /dev/null @@ -1,78 +0,0 @@ -/********************************************************************************* - * PROJECT: MiMic - * -------------------------------------------------------------------------------- - * - * This file is part of MiMic - * Copyright (C)2011 Ryo Iizuka - * - * MiMic is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published - * by the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program. If not, see . - * - * For further information please contact. - * http://nyatla.jp/ - * or - * - * - * Parts of this file were leveraged from uIP: - * - * Copyright (c) 2001-2003, Adam Dunkels. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote - * products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS - * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY - * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE - * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ -void uip_udp_v6(void) -{ - //If IP broadcast support is configured, we check for a broadcast UDP packet, which may be destined to us. - #if UIP_BROADCAST - DEBUG_PRINTF("UDP IP checksum 0x%04x\n", uip_ipchksum()); - if(BUF->proto == UIP_PROTO_UDP && - uip_ipaddr_cmp(BUF->destipaddr, all_ones_addr) - /*&& - uip_ipchksum() == 0xffff*/) { - goto udp_input; - } - #endif /* UIP_BROADCAST */ - - /* For IPv6, packet reception is a little trickier as we need to - make sure that we listen to certain multicast addresses (all - hosts multicast address, and the solicited-node multicast - address) as well. However, we will cheat here and accept all - multicast packets that are sent to the ff02::/16 addresses. */ -/* if(!uip_ipaddr_cmp(BUF->destipaddr, uip_hostaddr) && - BUF->destipaddr[0] != NyLPC_HTONS(0xff02)) { - UIP_STAT(++uip_stat.header.drop); - goto drop; - }*/ -} diff --git a/lib/src/uip/NyLPC_cTcpListener.c b/lib/src/uip/NyLPC_cTcpListener.c deleted file mode 100644 index fda579a..0000000 --- a/lib/src/uip/NyLPC_cTcpListener.c +++ /dev/null @@ -1,213 +0,0 @@ -/********************************************************************************* - * PROJECT: MiMic - * -------------------------------------------------------------------------------- - * - * This file is part of MiMic - * Copyright (C)2011 Ryo Iizuka - * - * MiMic is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published - * by the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program. If not, see . - * - * For further information please contact. - * http://nyatla.jp/ - * or - * - *********************************************************************************/ -#include "NyLPC_cTcpListener_protected.h" -#include "NyLPC_cTcpSocket_protected.h" -#include "NyLPC_cUipService_protected.h" -#include "NyLPC_cIPv4.h" -#include "NyLPC_stdlib.h" -#include -/** - * NyLPC_TTcpListenerListenQ - */ - -void NyLPC_TTcpListenerListenQ_init(struct NyLPC_TTcpListenerListenQ* i_struct) -{ - i_struct->wp=0; - int i; - for(i=NyLPC_TcTcpListener_NUMBER_OF_Q-1;i>=0;i--){ - i_struct->item[i].rport=0; - } -} - -/** - * ListenQへSYNパケットの情報を追加する。 - */ -void NyLPC_TTcpListenerListenQ_add(struct NyLPC_TTcpListenerListenQ* i_struct,const NyLPC_TcIPv4Payload_t* i_payload) -{ - struct NyLPC_TTcpSocketSynParam* item=&i_struct->item[i_struct->wp]; - //未処理のものがあれば登録しない。 - if(item->rport!=0){ - return; - } - - //SYNリングバッファにセット - item->rport = i_payload->payload.tcp->srcport; - item->srcaddr=i_payload->header->srcipaddr; - item->rcv_nxt32=NyLPC_ntohl(i_payload->payload.tcp->seqno32)+1; - //MSSの設定 - if(!NyLPC_TTcpHeader_getTcpMmsOpt(i_payload->payload.tcp,&item->mss)){ - item->mss=0; - } - //書込み位置の進行 - i_struct->wp=(i_struct->wp+1)%NyLPC_TcTcpListener_NUMBER_OF_Q; -} - -/** - * 最も古いSYNパケット情報のインデクスをキューから返す。 - * @return - * 見つからない場合-1である。 - */ -int NyLPC_TTcpListenerListenQ_getLastIndex(struct NyLPC_TTcpListenerListenQ* i_struct) -{ - int i,t; - //古いものから順に返す - for(i=1;i<=NyLPC_TcTcpListener_NUMBER_OF_Q;i++){ - t=(i_struct->wp+i)%NyLPC_TcTcpListener_NUMBER_OF_Q; - //有効なデータ? - if(i_struct->item[t].rport!=0){ - return t; - } - } - return -1; -} - -/** - * ListenQのN番目を削除する。 - */ -void NyLPC_TTcpListenerListenQ_remove(struct NyLPC_TTcpListenerListenQ* i_struct,int i_idx) -{ - i_struct->item[i_idx].rport=0; - return; -} - - - - - - -//#define lockResource(i_inst) NyLPC_cMutex_lock(((i_inst)->_mutex)) -//#define unlockResource(i_inst) NyLPC_cMutex_unlock(((i_inst)->_mutex)) -#define lockResource(i_inst) NyLPC_cMutex_lock(NyLPC_cIPv4_getListenerMutex(((i_inst)->_super._parent_ipv4))) -#define unlockResource(i_inst) NyLPC_cMutex_unlock(NyLPC_cIPv4_getListenerMutex(((i_inst)->_super._parent_ipv4))) - - -/** - * uipサービスが稼働中にのみ機能します。 - */ -NyLPC_TBool NyLPC_cTcpListener_initialize(NyLPC_TcTcpListener_t* i_inst,NyLPC_TUInt16 i_port) -{ - NyLPC_TcUipService_t* srv=_NyLPC_TcUipService_inst; - NyLPC_cBaseSocket_initialize(&(i_inst->_super),NyLPC_TcBaseSocket_TYPEID_TCP_LISTENER); - NyLPC_TTcpListenerListenQ_init(&i_inst->_listen_q); - //uipサービスは初期化済であること。 - NyLPC_Assert(NyLPC_TcUipService_isInitService()); - //初期化 -// // NyLPC_cMutex_initialize(&(i_inst->_mutex)); -// i_inst->_mutex=NyLPC_cIPv4_getListenerMutex(&srv->_tcpv4);// NyLPC_cMutex_initialize(&(i_inst->_mutex)); - i_inst->_port=NyLPC_htons(i_port); - //管理リストへ登録。 - return NyLPC_cIPv4_addSocket(&(srv->_tcpv4),&(i_inst->_super)); -} - -void NyLPC_cTcpListener_finaize(NyLPC_TcTcpListener_t* i_inst) -{ - NyLPC_TcUipService_t* srv=_NyLPC_TcUipService_inst; - NyLPC_Assert(NyLPC_TcUipService_isInitService()); - //uipサービスは初期化済であること。 - if(!NyLPC_cIPv4_removeSocket(&(srv->_tcpv4),&(i_inst->_super))){ - //削除失敗、それは死を意味する。 - NyLPC_Abort(); - } - NyLPC_cBaseSocket_finalize(&(i_inst->_super)); - return; -} - - - - -NyLPC_TBool NyLPC_cTcpListener_listen(NyLPC_TcTcpListener_t* i_inst,NyLPC_TcTcpSocket_t* i_sock,NyLPC_TUInt32 i_wait_msec) -{ - int qi; - NyLPC_TcStopwatch_t sw; - NyLPC_TBool ret=NyLPC_TBool_FALSE; - //サービスは稼働中であること。 - NyLPC_Assert(NyLPC_cUipService_isRun()); - - //入力ソケットはCLOSEDであること。 - if(i_sock->tcpstateflags!=UIP_CLOSED){ - return NyLPC_TBool_FALSE; - } - - //ストップウォッチを起動 - NyLPC_cStopwatch_initialize(&sw); - NyLPC_cStopwatch_setNow(&sw); - - - //Listenerのリソースロック - lockResource(i_inst); - - while(NyLPC_cStopwatch_elapseInMsec(&sw)_listen_q); - if(qi>=0){ - //SYN処理要求がある - if(!NyLPC_cTcpSocket_listenSyn(i_sock,&i_inst->_listen_q.item[qi],i_inst->_port)){ - ret=NyLPC_TBool_FALSE; - }else{ - //成功 - ret=NyLPC_TBool_TRUE; - } - //処理したSYNの削除 - NyLPC_TTcpListenerListenQ_remove(&i_inst->_listen_q,qi); - break; - }else{ - //SYN処理要求は無い(しばらくまつ) - unlockResource(i_inst); - NyLPC_cThread_yield(); - lockResource(i_inst); - } - } - //タイムアウト - unlockResource(i_inst); - NyLPC_cStopwatch_finalize(&sw); - return ret; -} - -/** - * この関数は、Uip受信タスクから実行します。 - */ -NyLPC_TBool NyLPC_cTcpListener_synPacket(NyLPC_TcTcpListener_t* i_inst,const NyLPC_TcIPv4Payload_t* i_payload) -{ - //パケットチェック。SYN設定されてる? - if(!(i_payload->payload.tcp->flags & TCP_SYN)){ - //SYNない - return NyLPC_TBool_FALSE; - } - //peer port==0は受け取らない。 - if(i_payload->payload.tcp->srcport==0){ - return NyLPC_TBool_FALSE; - } - //Listenerのリソースロック - lockResource(i_inst); - //ListenQへ追加 - NyLPC_TTcpListenerListenQ_add(&(i_inst->_listen_q),i_payload); - //Listenerのリソースアンロック - unlockResource(i_inst); - return NyLPC_TBool_TRUE; -} - - - - diff --git a/lib/src/uip/NyLPC_cTcpSocket.c b/lib/src/uip/NyLPC_cTcpSocket.c deleted file mode 100644 index 5c80028..0000000 --- a/lib/src/uip/NyLPC_cTcpSocket.c +++ /dev/null @@ -1,1492 +0,0 @@ -/********************************************************************************* - * PROJECT: MiMic - * -------------------------------------------------------------------------------- - * - * This file is part of MiMic - * Copyright (C)2011 Ryo Iizuka - * - * MiMic is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published - * by the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program. If not, see . - * - * For further information please contact. - * http://nyatla.jp/ - * or - * - *********************************************************************************/ -#include "NyLPC_cTcpSocket_protected.h" -#include "NyLPC_stdlib.h" -#include "NyLPC_cUipService_protected.h" - - -static NyLPC_TUInt32 iss32=3939; -#define SIZE_OF_IPv4_TCPIP_HEADER 40 - -/** - * TCPのRTOの最大値。 - * ms単位である。 - * defaultは64SEC - */ -#define UIP_IP_RTO_MAX_RTO 64000 -/** - * TCPのRTOの初期値。 - * ms単位である。 - * 伝送路の特性に合わせて調整すること。 - */ -#define UIP_TCP_RTO_INITIAL 3000 - -/** - * CONNECTION時のRTO - */ -#define UIP_TCP_RTO_CONNECTION_INITIAL 200 - -/** - * 下限値 - */ -#define UIP_TCP_RTO_MINIMUM 100 - - -/** - * for Debug - * RTOの情報をログ領域に取る。 - */ -#ifdef RTO_LOG - NyLPC_TUInt32 rto_log[256]; - int rto_log_st=0; - #define DEBUG_RTO_LOG(i_inst) if(rto_log_st<256){rto_log[rto_log_st++]=i_inst->uip_connr.current_rto32;}; -#else - #define DEBUG_RTO_LOG(i_inst) -#endif - -//#define lockResource(i_inst) NyLPC_cMutex_lock(&((i_inst)->_smutex)) -//#define unlockResource(i_inst) NyLPC_cMutex_unlock(&((i_inst)->_smutex)) -#define lockResource(i_inst) NyLPC_cMutex_lock(NyLPC_cIPv4_getSockMutex(((i_inst)->_super._parent_ipv4))) -#define unlockResource(i_inst) NyLPC_cMutex_unlock(NyLPC_cIPv4_getSockMutex(((i_inst)->_super._parent_ipv4))) - -static void sendRst(NyLPC_TcTcpSocket_t* i_inst); - - - - -//////////////////////////////////////////////////////////////////////////////////////////////////// -// -// Packet writer -// -//////////////////////////////////////////////////////////////////////////////////////////////////// - - -/** - * TCPヘッダに値をセットする。checksum,wndは0初期化する。 - */ -static void setTcpTxHeader(struct NyLPC_TTcpHeader* i_struct,NyLPC_TUInt8 i_flag,const struct uip_conn* i_conn) -{ - i_struct->flags = i_flag; - //sorce & destination port - i_struct->srcport = i_conn->lport; - i_struct->destport = i_conn->rport; - //ACK number - i_struct->ackno32 = NyLPC_htonl(i_conn->rcv_nxt32); - //Seq Number - i_struct->seqno32 = NyLPC_htonl(i_conn->snd_nxt32); - //uip_func_tcp_send_noconn(BUF); - i_struct->urgp[0] = i_struct->urgp[1] = 0; - i_struct->tcpchksum= 0; -} - -static void setTxPacket(const NyLPC_TcTcpSocket_t* i_inst,void* i_tx_buf,NyLPC_TUInt8 i_tcpf,const void* i_buf,NyLPC_TUInt16 i_len) -{ - struct NyLPC_TIPv4Header* iph; - struct NyLPC_TTcpHeader* tcph; - NyLPC_TUInt8 iph_word=0x05; - NyLPC_TUInt8 tcph_word=(UIP_TCPH_LEN) / 4; - //IPヘッダの更新 - iph=(struct NyLPC_TIPv4Header*)i_tx_buf; - iph->vhl=0x40|(0x0f&iph_word); - iph->destipaddr=i_inst->uip_connr.ripaddr; - iph->srcipaddr =*(i_inst->uip_connr.lipaddr); - NyLPC_TIPv4Header_writeTxIpHeader(iph,UIP_PROTO_TCP); - //TCPヘッダの更新 - tcph=(struct NyLPC_TTcpHeader*)(((NyLPC_TUInt8*)i_tx_buf)+NyLPC_TIPv4Header_getHeaderLength(iph)); - - - //SYNが有るならMSSの書き込み - if((TCP_SYN & i_tcpf)){ - tcph_word+=((TCP_OPT_MSS_LEN) / 4); - NyLPC_TTcpHeader_setMmsOpt(((NyLPC_TUInt8*)(tcph+1)),i_inst->uip_connr.default_mss); - } - tcph->tcpoffset=(tcph_word<<4); - setTcpTxHeader(tcph,i_tcpf,&(i_inst->uip_connr)); - - //最終的なパケットサイズと必要ならペイロードを書き込み - if(i_buf!=NULL){ - iph->len16=NyLPC_htons(i_len+(iph_word+tcph_word)*4); - memcpy(((NyLPC_TUInt8*)i_tx_buf)+((iph_word+tcph_word)*4),i_buf,i_len); - }else{ - iph->len16=NyLPC_htons((iph_word+tcph_word)*4); - } - //WND設定 - tcph->wnd16=NyLPC_htons(NyLPC_cFifoBuffer_getSpace(&(i_inst->rxbuf))); - //Checksumの生成 - tcph->tcpchksum=~(NyLPC_TIPv4Header_makeTcpChecksum(iph)); - iph->ipchksum = ~(NyLPC_TIPv4Header_makeIpChecksum(iph)); - return; -} - -/** - * IP/TCPヘッダが40バイト固定として、i_tx_buf+40の位置にあるペイロードに対するIP/TCPヘッダを書き込みます。 - */ -static void setTxPacketHeader(const NyLPC_TcTcpSocket_t* i_inst,void* i_tx_buf,NyLPC_TUInt8 i_tcpf,NyLPC_TUInt16 i_len) -{ - struct NyLPC_TIPv4Header* iph; - struct NyLPC_TTcpHeader* tcph; - NyLPC_TUInt8 iph_word=0x05; - NyLPC_TUInt8 tcph_word=(UIP_TCPH_LEN) / 4; - //IPヘッダの更新 - iph=(struct NyLPC_TIPv4Header*)i_tx_buf; - iph->vhl=0x40|(0x0f&iph_word); - iph->destipaddr=i_inst->uip_connr.ripaddr; - iph->srcipaddr =*(i_inst->uip_connr.lipaddr); - NyLPC_TIPv4Header_writeTxIpHeader(iph,UIP_PROTO_TCP); - - //TCPヘッダの更新 - tcph=(struct NyLPC_TTcpHeader*)(((NyLPC_TUInt8*)i_tx_buf)+NyLPC_TIPv4Header_getHeaderLength(iph)); - tcph->tcpoffset=(tcph_word<<4); - setTcpTxHeader(tcph,i_tcpf,&(i_inst->uip_connr)); - - //最終的なパケットサイズと必要ならペイロードを書き込み - iph->len16=NyLPC_htons(i_len+(iph_word+tcph_word)*4); - //WND設定 - tcph->wnd16=NyLPC_htons(NyLPC_cFifoBuffer_getSpace(&(i_inst->rxbuf))); - //Checksumの生成 - tcph->tcpchksum=~(NyLPC_TIPv4Header_makeTcpChecksum(iph)); - iph->ipchksum = ~(NyLPC_TIPv4Header_makeIpChecksum(iph)); - return; -} - - - - - -//////////////////////////////////////////////////////////////////////////////////////////////////// -// -// Mainclass::private -// -//////////////////////////////////////////////////////////////////////////////////////////////////// - -/** - * ACK番号を更新する。 - * @param i_ackno - * ネットワークオーダーのACK番号 - */ -static void updateAckNo(void* i_tx_buf,NyLPC_TUInt32 i_ackno) -{ - struct NyLPC_TIPv4Header* iph=(struct NyLPC_TIPv4Header*)i_tx_buf; - struct NyLPC_TTcpHeader* tcph=(struct NyLPC_TTcpHeader*)(((NyLPC_TUInt8*)i_tx_buf)+NyLPC_TIPv4Header_getHeaderLength(iph)); - -/* union{ - NyLPC_TUInt32 l; - NyLPC_TUInt8 b[4]; - }old_ack,new_ack; - NyLPC_TUInt16 v1; - //checksumの計算 - old_ack.l=i_inst->payload.tcp->ackno32;//古いACK番号 - new_ack.l=i_ackno;//新しいACK番号 - v1=NyLPC_ntohs(~(i_inst->payload.tcp->tcpchksum));//1の補数を取って、ホストオーダーに戻す。 - //減算 - v1=sub16c(v1,(old_ack.b[0]<<8)+old_ack.b[1]); - v1=sub16c(v1,(old_ack.b[2]<<8)+old_ack.b[3]); - //加算 - v1=add16c(v1,(new_ack.b[0]<<8)+new_ack.b[1]); - v1=add16c(v1,(new_ack.b[2]<<8)+new_ack.b[3]); - v1=~NyLPC_htons(v1);*/ -NyLPC_Trace(); - tcph->ackno32=i_ackno; -NyLPC_Trace(); - tcph->tcpchksum = 0; -NyLPC_Trace(); - tcph->tcpchksum = ~(NyLPC_TIPv4Header_makeTcpChecksum(iph)); -NyLPC_Trace(); - -/* - if((i_inst->payload.tcp->tcpchksum!=v1)){ - NyLPC_Warning(); - }*/ -} - - - -/** - * 指定した送信パケットがACK済であるか調べる。 - */ -static NyLPC_TBool isPacketAcked(NyLPC_TcTcpSocket_t* i_inst,NyLPC_TUInt32 i_sq) -{ - int rp; - struct NyLPC_TcTcpSocket_TxQItem* q=i_inst->txbuf.txq; - rp=i_inst->txbuf.rp; - while(rp!=i_inst->txbuf.wp){ - if(q[rp].ackno==i_sq){ - return NyLPC_TBool_FALSE; - } - rp=(rp+1)%NyLPC_TcTcpSocket_NUMBER_OF_TXQ; - } - return NyLPC_TBool_TRUE; -} -/** - * 送信キューからi_sq以前に送信したパケットを除外して、残り個数を返却する。 - */ -static int getNumOfSending(NyLPC_TcTcpSocket_t* i_inst,NyLPC_TUInt32 i_sq) -{ - int rp,n; - struct NyLPC_TcTcpSocket_TxQItem* q=i_inst->txbuf.txq; - rp=i_inst->txbuf.rp; - n=0; - while(rp!=i_inst->txbuf.wp){ - if(q[rp].ackno==i_sq){ - return n; - } - n++; - rp=(rp+1)%NyLPC_TcTcpSocket_NUMBER_OF_TXQ; - } - return n; -} -/** - * この関数は、コネクションをリセットします。 - * ロック状態でコールしてください。 - * 関数は、現在バッファにある再送信待ちデータを開放します。 - */ -static void resetTxQWithUnlock(NyLPC_TcTcpSocket_t* i_inst) -{ - int i,l; - struct NyLPC_TcTcpSocket_TxQItem* q=i_inst->txbuf.txq; - void* dlist[NyLPC_TcTcpSocket_NUMBER_OF_TXQ]; - - l=0; - while(i_inst->txbuf.rp!=i_inst->txbuf.wp){ - dlist[l]=q[i_inst->txbuf.rp].packet; - l++; - i_inst->txbuf.rp=(i_inst->txbuf.rp+1)%NyLPC_TcTcpSocket_NUMBER_OF_TXQ; - } - i_inst->txbuf.rp=i_inst->txbuf.wp=0; - //ロック解除 - unlockResource(i_inst); - //セーブしたバッファを開放 - for(i=0;itxbuf.txq; - NyLPC_ArgAssert(i_inst!=NULL); - rp=i_inst->txbuf.rp; - while(rp!=i_inst->txbuf.wp){ - updateAckNo(q[rp].packet,i_ackno); - rp=(rp+1)%NyLPC_TcTcpSocket_NUMBER_OF_TXQ; - } -} - -/** - * RTOの予測関数 - */ -static void estimateRTO(NyLPC_TcTcpSocket_t* i_inst,int s,int n) -{ - NyLPC_TcStopwatch_t sw; - NyLPC_TUInt32 cr_rtt_min,cr_rtt_max,sk_rto,new_rto,w; - int i; - struct NyLPC_TcTcpSocket_TxQItem* q=i_inst->txbuf.txq; - NyLPC_cStopwatch_initialize(&sw); - - sk_rto=i_inst->uip_connr.current_rto32; - //ACKされたパケットの個数は? - switch(n){ - case 1: - NyLPC_cStopwatch_set(&sw,q[s].tick_of_sent); - cr_rtt_min=NyLPC_cStopwatch_elapseInMsec(&sw); - if(sk_rtow){ - cr_rtt_min=w; - } - if(cr_rtt_maxuip_connr.current_rto32=new_rto; -} - -/** - * TXキューから、入力されたシーケンス番号より前のパケットを除外します。 - * リングバッファのrp->wp-1までをチェックして、sqに等しいi_sq以前のパケットバッファをo_dlistへ返します。 - * - */ -static int updateTxQByIndex(NyLPC_TcTcpSocket_t* i_inst,NyLPC_TUInt32 i_sq,void* o_dlist[]) -{ - int rp,n; - struct NyLPC_TcTcpSocket_TxQItem* q=i_inst->txbuf.txq; - //ロック状態なう - rp=i_inst->txbuf.rp; - n=0; - //This is debug - DEBUG_RTO_LOG(i_inst); - - while(rp!=i_inst->txbuf.wp){ - o_dlist[n]=q[rp].packet; - if(q[rp].ackno==i_sq){ - //i_inst->txbuf.rp->rpのパケットのRTOからbaseRTOの値を再計算。 - estimateRTO(i_inst,i_inst->txbuf.rp,n+1); - i_inst->txbuf.rp=(rp+1)%NyLPC_TcTcpSocket_NUMBER_OF_TXQ; - return n+1; - } - n++; - rp=(rp+1)%NyLPC_TcTcpSocket_NUMBER_OF_TXQ; - } - return 0; -} - - - -/** - * 空きキューを1個返します。 - * 空きキューの - */ -static struct NyLPC_TcTcpSocket_TxQItem* getTxQ(NyLPC_TcTcpSocket_t* i_inst,NyLPC_TcStopwatch_t* i_timer) -{ - int i; - struct NyLPC_TcTcpSocket_TxQItem* q=i_inst->txbuf.txq; - do{ - //クローズドに遷移してしまったら、エラーである。 - if(i_inst->tcpstateflags==UIP_CLOSED){ - return NULL; - } - //キューの空きをチェック。wp+1==rpなら、キューがいっぱい。rp==wpなら、キューが空。 - if(((i_inst->txbuf.wp+1)%NyLPC_TcTcpSocket_NUMBER_OF_TXQ)==i_inst->txbuf.rp){ - //一時的なアンロック - unlockResource(i_inst); - //タスクスイッチ - NyLPC_cThread_yield(); - //ロック - lockResource(i_inst); - continue; - } - i=i_inst->txbuf.wp; - i_inst->txbuf.wp=(i+1)%NyLPC_TcTcpSocket_NUMBER_OF_TXQ; - return &(q[i]); - }while(!NyLPC_cStopwatch_isExpired(i_timer)); - //失敗。タイムアウト。 - return NULL; -} - - - - - - -/********************************************************************** - * public 関数 - **********************************************************************/ - -NyLPC_TBool NyLPC_cTcpSocket_initialize(NyLPC_TcTcpSocket_t* i_inst,void* i_rbuf,NyLPC_TUInt16 i_rbuf_len) -{ - int i; - NyLPC_TcUipService_t* srv=_NyLPC_TcUipService_inst; - NyLPC_cBaseSocket_initialize(&(i_inst->_super),NyLPC_TcBaseSocket_TYPEID_TCP_SOCK); - //uipサービスは初期化済であること。 - NyLPC_Assert(NyLPC_TcUipService_isInitService()); - - NyLPC_cFifoBuffer_initialize(&(i_inst->rxbuf),i_rbuf,i_rbuf_len); - // NyLPC_AbortIfNot(NyLPC_cMutex_initialize(&(i_inst->_smutex)));//個別Mutex -// i_inst->_smutex=NyLPC_cIPv4_getSockMutex(&(srv->_tcpv4));//共有Mutex - i_inst->tcpstateflags=UIP_CLOSED; - i_inst->txbuf.rp=i_inst->txbuf.wp=0; - for(i=0;itxbuf.txq[i].packet=NULL; - } - //管理リストへ登録。 - return NyLPC_cIPv4_addSocket(&(srv->_tcpv4),&(i_inst->_super)); -} -void NyLPC_cTcpSocket_finalize(NyLPC_TcTcpSocket_t* i_inst) -{ - int i; - NyLPC_TcUipService_t* srv=_NyLPC_TcUipService_inst; - NyLPC_Assert(NyLPC_TcUipService_isInitService()); - //uipサービスは初期化済であること。 - if(!NyLPC_cIPv4_removeSocket(&(srv->_tcpv4),&(i_inst->_super))){ - //削除失敗、それは死を意味する。 - NyLPC_Abort(); - } - //開放漏れの保険 - if(i_inst->txbuf.rp!=i_inst->txbuf.wp){ - lockResource(i_inst); - resetTxQWithUnlock(i_inst); - } - for(i=0;itxbuf.txq[i].packet=NULL; - } - NyLPC_cFifoBuffer_finalize(&(i_inst->rxbuf)); -// NyLPC_cMutex_finalize(&(i_inst->_smutex)); - NyLPC_cBaseSocket_finalize(&(i_inst->_super)); - return; -} - - - -NyLPC_TBool NyLPC_cTcpSocket_listenSyn(NyLPC_TcTcpSocket_t* i_inst,const struct NyLPC_TTcpSocketSynParam* i_lq,NyLPC_TUInt16 i_lport) -{ -// NyLPC_Assert(NyLPC_cMutex_isLocked(i_inst->_smutex)); - lockResource(i_inst); - //ソケットが無効であること。 - if(i_inst->tcpstateflags==UIP_CLOSED) - { - //localipとdefault_mmsは別枠で設定 - /* Fill in the necessary fields for the new connection. */ - i_inst->uip_connr.current_rto32 = UIP_TCP_RTO_INITIAL; - i_inst->uip_connr.lport = i_lport; - i_inst->uip_connr.rport = i_lq->rport; - i_inst->uip_connr.ripaddr=i_lq->srcaddr; - i_inst->uip_connr.snd_nxt32=iss32; - /* rcv_nxt should be the seqno from the incoming packet + 1. */ - i_inst->uip_connr.rcv_nxt32= i_lq->rcv_nxt32; - //MSSの設定 - i_inst->uip_connr.peer_mss=(i_lq->mss!=0)?i_lq->mss:i_inst->uip_connr.default_mss; - i_inst->uip_connr.peer_win=0; - NyLPC_cFifoBuffer_clear(&(i_inst->rxbuf)); - //ここでステータスがかわる。 - i_inst->tcpstateflags = UIP_SYN_RCVD; - //前回のデータが残っていた場合の保険 - if(i_inst->txbuf.rp!=i_inst->txbuf.wp){ - resetTxQWithUnlock(i_inst); - }else{ - unlockResource(i_inst); - } - return NyLPC_TBool_TRUE; - } - unlockResource(i_inst); - return NyLPC_TBool_FALSE; -} - - -/** - * sq番のTxがキューから消え去るのを待ちます。 - * この関数は、アンロック状態でコールしてください。 - *
- * パケットがキューからなくなる条件は、以下の2つです。 - *
    - *
  • ACKを受信してパケットキューが更新された。
  • - *
  • RSTを受信して(CLOSEDに遷移して)、キューがクリアされた。
  • - *
  • 送信タイムアウトで関数が(CLOSEDに遷移させて)キューをクリアした。
  • - *
- *
- * @param i_wait_msec - * @return - * 1番目の条件でパケットが消失したときのみ、TRUEを返します。 - * 失敗した場合、TCPステータスがCLOSEDでなければ、RSTを送信してステータスをCLOSEDにします。 - */ -static NyLPC_TBool waitForTxRemove(NyLPC_TcTcpSocket_t* i_inst,NyLPC_TUInt32 i_sq,NyLPC_TcStopwatch_t* i_timer) -{ - NyLPC_TUInt8 f; - lockResource(i_inst); - do{ - //パケットが送信中か調べる。 - if(!isPacketAcked(i_inst,i_sq)){ - //まだある場合は、タスクスイッチを繰り返して消失を待つ。 - unlockResource(i_inst); - NyLPC_cThread_yield(); - lockResource(i_inst); - continue; - } - //なくなった場合は、原因を調べる。 - f=i_inst->tcpstateflags; - unlockResource(i_inst); - return (f==UIP_CLOSED)?NyLPC_TBool_FALSE:NyLPC_TBool_TRUE; - }while(!NyLPC_cStopwatch_isExpired(i_timer)); - unlockResource(i_inst); - return NyLPC_TBool_FALSE; -} - - -/** - * 再送信処理をセットして、パケットを送信します。 - * この関数は「アンロック状態で」実行してください。 - * @param i_len - * 送信データサイズを指定します。 - * この番号は、シーケンス番号の加算値ではありませんので、注意をしてください。 - * @return - *
    - *
  • n=-1:送信キューへの投入に失敗した。
  • - *
  • n>=0:nバイトのデータを送信キューへの投入することに成功した。
  • - *
- * 送信キューに失敗する理由は2つあります。1つは、TXバッファがフルでタイムアウト。もうひとつは、非同期なコネクリョンのリセットです。 - * 失敗した場合、TCPステータスがCLOSEDでなければ、RSTを送信してステータスをCLOSEDにします。 - */ -static NyLPC_TInt32 sendWithRetransmit(NyLPC_TcTcpSocket_t* i_inst,NyLPC_TUInt8 i_tcpf,const void* i_buf,NyLPC_TUInt16 i_len,NyLPC_TcStopwatch_t* i_timer,NyLPC_TUInt32* o_ack) -{ - struct NyLPC_TcTcpSocket_TxQItem* txq; - NyLPC_TUInt16 s; - void* buf; - NyLPC_TUInt32 next_ack; - //送信バッファを取得 - //@bug オブションパケット送信時に4バイト足りないメモリ要求しない?問題になってないけど。 - for(;;){ - buf=NyLPC_cUipService_allocTxBuf(i_len+(SIZE_OF_IPv4_TCPIP_HEADER),&s); - if(buf!=NULL){ - break; - } - //タイムアウト確認 - if(NyLPC_cStopwatch_isExpired(i_timer)){ - return -1; - } - }; - lockResource(i_inst); - //ペイロードがある場合のみ、相手のwindowサイズが0以上になるのを待つ。 - if(i_len>0){ - while(i_inst->uip_connr.peer_win==0){ - unlockResource(i_inst); - //時間切れならエラー。 - if(NyLPC_cStopwatch_isExpired(i_timer)){ - return -1; - } - NyLPC_cThread_yield(); - lockResource(i_inst); - } - } - //送信キューの取得 - txq=getTxQ(i_inst,i_timer); - //送信キューが取れなかった。 - if(txq==NULL){ - //シーケンス番号をロールバックできないので、エラーとする。 - unlockResource(i_inst); - NyLPC_cUipService_releaseTxBuf(buf); - return -1; - } - - //送信バッファを基準とした送信サイズを計算 - s-=SIZE_OF_IPv4_TCPIP_HEADER; - //送信サイズよりMMSが小さければ、送信サイズを修正 - if(i_inst->uip_connr.peer_mssuip_connr.peer_mss; - } - //送信サイズよりpeerのウインドウサイズが小さければ修正 - if(i_inst->uip_connr.peer_winuip_connr.peer_win; - } - //送信サイズより、データサイズが小さければ、送信サイズを修正 - if(i_lenuip_connr.snd_nxt32+s+(((i_tcpf&(TCP_FIN|TCP_SYN))!=0x00)?1:0); - txq->rto32=i_inst->uip_connr.current_rto32; - txq->tick_of_sent=NyLPC_cStopwatch_now(); - - //パケットの書き込み - setTxPacket(i_inst,buf,i_tcpf,i_buf,s); - txq->packet=buf; - - //シーケンス番号の更新 - i_inst->uip_connr.snd_nxt32=next_ack; - //Peerのウインドウサイズを更新 - i_inst->uip_connr.peer_win-=s; - //ACK番号の返却 - *o_ack=txq->ackno=NyLPC_HTONL(next_ack); - unlockResource(i_inst); - NyLPC_cUipService_sendIPv4Tx(buf); - return s; -} -/** - * RSTを1フレームだけ送信します。 - * この関数は、クローズドステータスのソケットにしてからコールします。 - * この関数は、アンロック状態でコールしてね。 - */ -static void sendRst(NyLPC_TcTcpSocket_t* i_inst) -{ - void* buf; - - NyLPC_Assert(i_inst->tcpstateflags==UIP_CLOSED); - //ペイロードライタの初期化 - - //@bug バッファが取れるまで通信がブロックするの。ここはなんとかしないと。 - buf=NyLPC_cUipService_allocSysTxBuf(); - lockResource(i_inst); - i_inst->uip_connr.snd_nxt32++; - unlockResource(i_inst); - setTxPacket(i_inst,buf,TCP_RST|TCP_ACK,NULL,0); - NyLPC_cUipService_sendIPv4Tx(buf); - NyLPC_cUipService_releaseTxBuf(buf); - NyLPC_cIPv4Payload_finalize(&ipv4); - return; -} - - - -/** - * 受信データをバッファに書き込む。 - * 十分な空き領域がない場合、失敗する。 - * この関数は、ロックして実行してください。 - */ -static NyLPC_TBool addRecvData(NyLPC_TcTcpSocket_t* i_inst,const void* i_data,NyLPC_TUInt16 i_data_size) -{ - //受信データサイズを確認 - if(NyLPC_cFifoBuffer_getSpace(&(i_inst->rxbuf))>=i_data_size){ - //バッファに格納可能なら、格納。 - NyLPC_cFifoBuffer_push(&(i_inst->rxbuf),i_data,i_data_size); - }else{ - //エラー:ドロップする。 - return NyLPC_TBool_FALSE; - } - - return NyLPC_TBool_TRUE; -} - - - - -/** - * Public function - */ - -NyLPC_TBool NyLPC_cTcpSocket_connect(NyLPC_TcTcpSocket_t* i_inst,const struct NyLPC_TIPv4Addr* i_addr,NyLPC_TUInt16 i_peer_port,NyLPC_TUInt32 i_wait_in_msec) -{ - volatile NyLPC_TUInt8 f; - NyLPC_TUInt32 sq; - NyLPC_TcStopwatch_t sw; - NyLPC_TUInt16 lport; - lockResource(i_inst); - //ソケットが無効であること。 - if(i_inst->tcpstateflags!=UIP_CLOSED) - { - NyLPC_OnErrorGoto(Error); - } - //ポート番号の取得(lockResourceが他のソケットと共有なので、重複ポートの割当は起こりえない。でもちょっと注意して) - lport=NyLPC_htons(NyLPC_cIPv4_getNewPortNumber(i_inst->_super._parent_ipv4)); - if(lport==0){ - NyLPC_OnErrorGoto(Error); - } - //connectの為の準備 - - //localipとdefault_mmsは別枠で設定 - /* Fill in the necessary fields for the new connection. */ - i_inst->uip_connr.current_rto32 = UIP_TCP_RTO_CONNECTION_INITIAL;//RTOを短くしてARP発行時の再接続短縮を期待する。 - i_inst->uip_connr.lport = lport; - i_inst->uip_connr.rport = NyLPC_htons(i_peer_port); - i_inst->uip_connr.ripaddr=*i_addr; - i_inst->uip_connr.snd_nxt32=iss32;//should be random - /* rcv_nxt should be the seqno from the incoming packet + 1. */ - i_inst->uip_connr.rcv_nxt32=0; - //MSSの設定 - i_inst->uip_connr.peer_mss=i_inst->uip_connr.default_mss; - i_inst->uip_connr.peer_win=1;//periodicの再送信を期待するために相手のWindowサイズは1と仮定する。 - NyLPC_cFifoBuffer_clear(&(i_inst->rxbuf)); - //ここでステータスがかわる。 - i_inst->tcpstateflags = UIP_SYN_SENT; - //前回のデータが残っていた場合の保険 - if(i_inst->txbuf.rp!=i_inst->txbuf.wp){ - resetTxQWithUnlock(i_inst); - }else{ - unlockResource(i_inst); - } - - NyLPC_cStopwatch_initialize(&sw); - - NyLPC_cStopwatch_startExpire(&sw,i_wait_in_msec); - if(sendWithRetransmit(i_inst,TCP_SYN,NULL,0,&sw,&sq)==0){ - //ちょっと待つ。 - NyLPC_cThread_yield(); - //キューにあるTXが消えるのを待つ。 - if(waitForTxRemove(i_inst,sq,&sw)){ - //ACK受信に成功して、TXが消失 - NyLPC_cStopwatch_finalize(&sw); - return NyLPC_TBool_TRUE; - } - } - //ロックして、強制的なステータス遷移 - lockResource(i_inst); - f=i_inst->tcpstateflags; - if(f!=UIP_CLOSED){ - //もし、強制CLOSE遷移であれば、RSTも送信。 - i_inst->tcpstateflags=UIP_CLOSED; - unlockResource(i_inst); - sendRst(i_inst); - }else{ - unlockResource(i_inst); - } - return NyLPC_TBool_FALSE; -Error: - unlockResource(i_inst); - return NyLPC_TBool_FALSE; -} - -/** - * この関数は、UIP_SYN_RCVDステータスのソケットを、ESTABLISHEDへ遷移させます。 - * cTcpListener_listen関数を通過したインスタンスに実行してください。 - * この関数は、アプリケーションが呼び出します。 - * @return - * - */ -NyLPC_TBool NyLPC_cTcpSocket_accept(NyLPC_TcTcpSocket_t* i_inst,NyLPC_TUInt32 i_wait_in_msec) -{ - volatile NyLPC_TUInt8 f; - NyLPC_TUInt32 sq; - NyLPC_TcStopwatch_t sw; - - NyLPC_cStopwatch_initialize(&sw); - //ステータスチェック - f=i_inst->tcpstateflags; - switch(f) - { - case UIP_ESTABLISHED: - return NyLPC_TBool_TRUE; - case UIP_SYN_RCVD: - //処理対象 - break; - default: - return NyLPC_TBool_FALSE; - } - NyLPC_cStopwatch_startExpire(&sw,i_wait_in_msec); - if(sendWithRetransmit(i_inst,TCP_SYN|TCP_ACK,NULL,0,&sw,&sq)==0){ - //ちょっと待つ。 - NyLPC_cThread_yield(); - //キューにあるTXが消えるのを待つ。 - if(waitForTxRemove(i_inst,sq,&sw)){ - //ACK受信に成功して、TXが消失 - NyLPC_cStopwatch_finalize(&sw); - return NyLPC_TBool_TRUE; - } - } - //ロックして、強制的なステータス遷移 - lockResource(i_inst); - f=i_inst->tcpstateflags; - if(f!=UIP_CLOSED){ - //もし、強制CLOSE遷移であれば、RSTも送信。 - i_inst->tcpstateflags=UIP_CLOSED; - unlockResource(i_inst); - sendRst(i_inst); - }else{ - unlockResource(i_inst); - } - return NyLPC_TBool_FALSE; -} - - -/** - * この関数は、ソケットの受信バッファの読み取り位置と、読み出せるデータサイズを返却します。 - * 関数はポインターを返却するだけで、バッファの読み取り位置をシークしません。 - * シークするにはNyLPC_cTcpSocket_pseekを使います。 - */ -NyLPC_TInt32 NyLPC_cTcpSocket_precv(NyLPC_TcTcpSocket_t* i_inst,const void** o_buf_ptr,NyLPC_TUInt32 i_wait_msec) -{ - volatile NyLPC_TUInt8 st; - NyLPC_TUInt16 rlen; - //タイマを生成 - NyLPC_TcStopwatch_t sw; - NyLPC_cStopwatch_initialize(&sw); - - //ESTABLISHED以外の場合は、エラー。 - NyLPC_cStopwatch_setNow(&sw); - do{ - //読み出しバッファ情報のコピー - //MUTEX LOCK - lockResource(i_inst); - st=i_inst->tcpstateflags; - rlen=NyLPC_cFifoBuffer_getLength(&(i_inst->rxbuf)); - *o_buf_ptr=NyLPC_cFifoBuffer_getPtr(&(i_inst->rxbuf)); - //MUTEX UNLOCK - unlockResource(i_inst); - - //バッファが空の場合は、ステータスチェック。ESTABLISHEDでなければ、エラー(PASVCLOSE等の場合) - switch(st){ - case UIP_ESTABLISHED: - if(rlen>0){ - //バッファにパケットがあれば返却 - NyLPC_cStopwatch_finalize(&sw); - return rlen; - } - break; - case UIP_CLOSE_WAIT: - if(rlen>0){ - //バッファにパケットがあれば返却 - NyLPC_cStopwatch_finalize(&sw); - return rlen; - } - //引き続きエラー処理 - default: - //他の場合はエラー - NyLPC_cStopwatch_finalize(&sw); - return -1; - } - //タスクスイッチ - NyLPC_cThread_yield(); - }while(NyLPC_cStopwatch_elapseInMsec(&sw)rxbuf))); - if(i_seek==0){ - return; - } - - //ACK送信バッファの取得 - buf=NyLPC_cUipService_allocSysTxBuf(); - - //MUTEX LOCK - lockResource(i_inst); - - //受信バッファを読み出しシーク - NyLPC_cFifoBuffer_pop(&(i_inst->rxbuf),i_seek); - //ACKパケットの生成 - setTxPacket(i_inst,buf,TCP_ACK,NULL,0); - unlockResource(i_inst); - //ACK送信 - NyLPC_cUipService_sendIPv4Tx(buf); - NyLPC_cUipService_releaseTxBuf(buf); - -} - -/** - * See header file. - */ -void* NyLPC_cTcpSocket_allocSendBuf(NyLPC_TcTcpSocket_t* i_inst,NyLPC_TUInt16 i_hint,NyLPC_TUInt16* o_buf_size,NyLPC_TUInt32 i_wait_in_msec) -{ - NyLPC_TUInt16 s; - void* buf; - NyLPC_TcStopwatch_t sw; - - NyLPC_cStopwatch_initialize(&sw); - NyLPC_cStopwatch_startExpire(&sw,i_wait_in_msec); - - //送信バッファを取得 - //@bug バッファが取れるまで通信がブロックするの。ここはなんとかしないと。 - for(;;){ - //ESTABLISHED以外に非同期遷移 - if(i_inst->tcpstateflags!=UIP_ESTABLISHED){ - NyLPC_cStopwatch_finalize(&sw); - return NULL; - } - buf=NyLPC_cUipService_allocTxBuf(i_hint+(SIZE_OF_IPv4_TCPIP_HEADER),&s); - if(buf!=NULL){ - break; - } - //タイムアウト時もエラー - if(NyLPC_cStopwatch_isExpired(&sw)){ - NyLPC_cStopwatch_finalize(&sw); - return NULL; - } - } - -//@todo 前段処理と順番を入れ替えて、要求サイズとpeerのwinのうち、小さいほうを割り当てたほうが良くない? -//ここで相手のwin待ちをする理由は、相手に確実に受け取れるサイズを決定する為。 - lockResource(i_inst); - //ペイロードがある場合のみ、相手のwindowサイズが0以上になるのを待つ。 - while(i_inst->uip_connr.peer_win==0){ - unlockResource(i_inst); - //ESTABLISHED以外に非同期遷移 orタイムアウト確認 - if(NyLPC_cStopwatch_isExpired(&sw)||(i_inst->tcpstateflags!=UIP_ESTABLISHED)){ - NyLPC_cUipService_releaseTxBuf(buf); - NyLPC_cStopwatch_finalize(&sw); - return NULL; - } - NyLPC_cThread_yield(); - lockResource(i_inst); - } - //送信バッファを基準とした送信サイズを計算 - s-=SIZE_OF_IPv4_TCPIP_HEADER; - //送信サイズよりMMSが小さければ、送信サイズを修正 - if(i_inst->uip_connr.peer_mssuip_connr.peer_mss; - } - //送信サイズよりpeerのウインドウサイズが小さければ修正 - if(i_inst->uip_connr.peer_winuip_connr.peer_win; - } - unlockResource(i_inst); - //バッファサイズ確定。 - *o_buf_size=s; - NyLPC_cStopwatch_finalize(&sw); - return (NyLPC_TUInt8*)buf+SIZE_OF_IPv4_TCPIP_HEADER; -} -/** - * See Header file. - */ -void NyLPC_cTcpSocket_releaseSendBuf(NyLPC_TcTcpSocket_t* i_inst,void* i_buf_ptr) -{ - NyLPC_cUipService_releaseTxBuf((NyLPC_TUInt8*)i_buf_ptr-SIZE_OF_IPv4_TCPIP_HEADER); -} - - -/** - * 事前にAllocしたTxパケットを送信します。 - * このAPIはゼロコピー送信をサポートするためのものです。 - * @param i_buf_ptr - * allocSendBufで取得したメモリを指定します。 - * @return - * 関数が失敗した場合、i_buf_ptrは「開放されません。」 - */ -NyLPC_TBool NyLPC_cTcpSocket_psend(NyLPC_TcTcpSocket_t* i_inst,void* i_buf_ptr,int i_len,NyLPC_TUInt32 i_wait_in_msec) -{ - struct NyLPC_TcTcpSocket_TxQItem* txq; - void* buf; - NyLPC_TcStopwatch_t sw; - //ESTABLISHEDでなければエラー - if(i_inst->tcpstateflags!=UIP_ESTABLISHED){ - //ESTABLISHEDでなければエラー - return NyLPC_TBool_FALSE; - } - //送信データ0なら何もしない。 - if(i_len<1){ - NyLPC_cTcpSocket_releaseSendBuf(i_inst,i_buf_ptr); - return NyLPC_TBool_TRUE; - } - NyLPC_cStopwatch_initialize(&sw); - NyLPC_cStopwatch_startExpire(&sw,i_wait_in_msec); - - //先頭ポインタは、i_buf-sizeof(SIZE_OF_IPv4_TCPIP_HEADER)固定 - buf=(NyLPC_TUInt8*)i_buf_ptr-SIZE_OF_IPv4_TCPIP_HEADER; - lockResource(i_inst); - //送信キューの取得 - txq=getTxQ(i_inst,&sw); - //送信キューが取れなかった。 - if(txq==NULL){ - //シーケンス番号をロールバックできないので、エラーとする。 - unlockResource(i_inst); - NyLPC_cStopwatch_finalize(&sw); - return NyLPC_TBool_FALSE; - } - //ここから先はi_bufの所有権はインスタンスになってる。 - - //IPv4ペイロードの書き込み - - //allocをした時点でwin,mssは考慮されているので、そのままそうしんしる。 - - //ACK番号の計算 - txq->rto32=i_inst->uip_connr.current_rto32; - txq->tick_of_sent=NyLPC_cStopwatch_now(); - //パケットヘッダの生成(ヘッダ長はpreadで定義した値(4+6)*4=40です。) - setTxPacketHeader(i_inst,buf,TCP_ACK|TCP_PSH,i_len); - txq->packet=buf; - - //シーケンス番号の更新 - i_inst->uip_connr.snd_nxt32=i_inst->uip_connr.snd_nxt32+i_len; - //Peerのウインドウサイズを更新 - i_inst->uip_connr.peer_win-=i_len; - //ACK番号の返却 - txq->ackno=NyLPC_HTONL(i_inst->uip_connr.snd_nxt32); - unlockResource(i_inst); - NyLPC_cUipService_sendIPv4Tx(buf); - NyLPC_cStopwatch_finalize(&sw); - return NyLPC_TBool_TRUE; -} - -/** - * See header file. - */ -NyLPC_TInt32 NyLPC_cTcpSocket_send(NyLPC_TcTcpSocket_t* i_inst,const void* i_buf_ptr,NyLPC_TInt32 i_len,NyLPC_TUInt32 i_wait_in_msec) -{ - NyLPC_TInt16 hint; - NyLPC_TUInt16 s; - void* buf; - if(i_len<1){ - return 0; - } - hint=(i_len>32767)?32767:i_len; - buf=NyLPC_cTcpSocket_allocSendBuf(i_inst,hint,&s,i_wait_in_msec); - if(buf==NULL){ - return -1; - } - //送信サイズの計算 - s=((NyLPC_TInt32)stcpstateflags; - //ステータスチェック - switch(f) - { - case UIP_CLOSED: - //閉じている。 - goto ReturnWithUnlock; - case UIP_ESTABLISHED: - //アクティブクローズ。 - i_inst->tcpstateflags=UIP_FIN_WAIT_1; - //送信のために一旦解除 - unlockResource(i_inst); - //FINの送信 - if(sendWithRetransmit(i_inst,TCP_FIN|TCP_ACK,NULL,0,&sw,&sq)==0){ - //ちょっと待つ。 - NyLPC_cThread_yield(); - //TXの消去待ち - if(waitForTxRemove(i_inst,sq,&sw)){ - //再ロック - lockResource(i_inst); - //タイムアウトするか、UIP_CLOSED、もしくはTIME_WAITに遷移するのを待つ。(遷移はRxprocで自動的に実行。) - do{ - switch(i_inst->tcpstateflags) - { - case UIP_TIME_WAIT: - i_inst->tcpstateflags=UIP_CLOSED; - case UIP_CLOSED: - NyLPC_Assert(i_inst->txbuf.rp==i_inst->txbuf.wp); - //成功。 - goto ReturnWithUnlock; - case UIP_FIN_WAIT_1: - case UIP_FIN_WAIT_2: - case UIP_CLOSING: - //一時的なアンロック - unlockResource(i_inst); - NyLPC_cThread_yield(); - lockResource(i_inst); - default: - break; - } - }while(!NyLPC_cStopwatch_isExpired(&sw)); - unlockResource(i_inst); - } - } - break; - case UIP_CLOSE_WAIT: - //LAST_ACKへ遷移 - i_inst->tcpstateflags=UIP_LAST_ACK; - //送信のために一旦解除 - unlockResource(i_inst); - if(sendWithRetransmit(i_inst,TCP_FIN|TCP_ACK,NULL,0,&sw,&sq)==0){ - //ちょっと待つ。 - NyLPC_cThread_yield(); - //TXの消去待ち - if(waitForTxRemove(i_inst,sq,&sw)){ - //再ロック - lockResource(i_inst); - //TX消去後にCLOSEDに遷移していればOK - if(i_inst->tcpstateflags==UIP_CLOSED) - { - NyLPC_Assert(i_inst->txbuf.rp==i_inst->txbuf.wp); - goto ReturnWithUnlock; - } - unlockResource(i_inst); - } - } - //エラー。RSTで切断。 - break; - default: - unlockResource(i_inst); - NyLPC_Warning(); - break; - } -// if(i_inst->_smutex._lock_count>0){ -// NyLPC_Warning(); -// } - //このパスに到達するのは、FIN送信/ACKに成功したにも拘らず、規定時間内にCLOSEDに遷移しなかった場合。 - //コネクションを強制遷移して、RST - lockResource(i_inst); - f=i_inst->tcpstateflags; - if(f!=UIP_CLOSED){ - //もし、強制CLOSE遷移であれば、RSTも送信。 - i_inst->tcpstateflags=UIP_CLOSED; - unlockResource(i_inst); - sendRst(i_inst); - }else{ - unlockResource(i_inst); - } - NyLPC_cStopwatch_finalize(&sw); - return; -ReturnWithUnlock: - unlockResource(i_inst); - NyLPC_cStopwatch_finalize(&sw); - return; -} - -/** - * uipサービスタスクが実行する関数です。 - * 定期的に実行する関数。最低でも1s単位で実行してください。 - */ -void NyLPC_cTcpSocket_periodic( - NyLPC_TcTcpSocket_t* i_inst) -{ - int i; - struct NyLPC_TcTcpSocket_TxQItem* q=i_inst->txbuf.txq; - NyLPC_TcStopwatch_t sw; - NyLPC_TUInt32 now; - int rp; - NyLPC_cStopwatch_initialize(&sw); - now=NyLPC_cStopwatch_now(); - //MUTEX LOCK - lockResource(i_inst); - if(i_inst->tcpstateflags==UIP_CLOSED) - { - //CLOSEDなら、バッファ開放。 - resetTxQWithUnlock(i_inst); - }else if(i_inst->txbuf.rp==i_inst->txbuf.wp){ - //再送信パケットがなければ何もしないよ。 - unlockResource(i_inst); - }else if(i_inst->uip_connr.peer_win==0){ - //peer_winが0の場合は何もしない。 - unlockResource(i_inst); - }else{ - //再送信処理 - rp=i_inst->txbuf.rp; - NyLPC_cStopwatch_set(&sw,q[rp].tick_of_sent); - if(NyLPC_cStopwatch_elapseInMsec(&sw)>q[rp].rto32){ - //最古のパケットの送信時間をチェックして、タイムアウトが発生したら、再送時間と送信時刻をセット - //最古パケットRTOを2倍。 - q[rp].rto32*=2; - for(i=rp;i!=i_inst->txbuf.wp;i=(i+1)%NyLPC_TcTcpSocket_NUMBER_OF_TXQ){ - q[i].tick_of_sent=now; - } - if(q[rp].rto32>UIP_IP_RTO_MAX_RTO){ - //最古のRTOが64秒を超えたら、CLOSED - i_inst->tcpstateflags =UIP_CLOSED; - resetTxQWithUnlock(i_inst); - sendRst(i_inst); - }else{ - //規定時間内なら、再送処理 - for(i=rp;i!=i_inst->txbuf.wp;i=(i+1)%NyLPC_TcTcpSocket_NUMBER_OF_TXQ){ -// NyLPC_cUipService_sendIPv4Tx(NyLPC_cIPv4Payload_getBuf(&(q[i].data))); - NyLPC_cUipService_sendIPv4Tx(q[i].packet); - } - unlockResource(i_inst); - } - }else{ - unlockResource(i_inst); - } - } - NyLPC_cStopwatch_finalize(&sw); - return; -} -/** - * uipサービスタスクが実行する関数です。 - * サービスの開始を通知します。 - */ -void NyLPC_cTcpSocket_startService(NyLPC_TcTcpSocket_t* i_inst,const NyLPC_TcIPv4Config_t* i_config) -{ - NyLPC_Assert(i_inst->tcpstateflags==UIP_CLOSED);//閉じてなければおかしい。 - i_inst->uip_connr.lipaddr=&(i_config->ip_addr); - i_inst->uip_connr.default_mss=i_config->default_mss; - //NyLPC_cTcpSocket_setSynPayload関数でも実行するけど、IFのリセット時なのでここでもやる。 - NyLPC_cFifoBuffer_clear(&(i_inst->rxbuf)); - return; -} -/** - * uipサービスタスクが実行する関数です。 - * サービスの停止を通知します。 - */ -void NyLPC_cTcpSocket_stopService(NyLPC_TcTcpSocket_t* i_inst) -{ - lockResource(i_inst); - if(i_inst->tcpstateflags==UIP_CLOSED) - { - unlockResource(i_inst); - }else{ - i_inst->tcpstateflags=UIP_CLOSED; - resetTxQWithUnlock(i_inst); - sendRst(i_inst); - } - return; -} - - -void* NyLPC_cTcpSocket_parseRx( - NyLPC_TcTcpSocket_t* i_inst, - const NyLPC_TcIPv4Payload_t* i_ipp) -{ - int i,s; - NyLPC_TUInt16 tmp16; - NyLPC_TUInt16 data_size; - NyLPC_TUInt8 in_tcpflag=i_ipp->payload.tcp->flags; - const void* tcp_data_offset; - NyLPC_TBool is_new_packet; - int num_of_noack; - void* dlist[NyLPC_TcTcpSocket_NUMBER_OF_TXQ]; - void* ret; - - //パラメータの計算 - - tmp16=NyLPC_TTcpHeader_getHeaderLength(i_ipp->payload.tcp); - //TCPペイロードの長さは、IPパケットの長さ-(IPヘッダ+TCPヘッダ) - data_size=NyLPC_TIPv4Header_getPacketLength(i_ipp->header)-NyLPC_TIPv4Header_getHeaderLength(i_ipp->header)-tmp16; - //TCPデータオフセット - tcp_data_offset=i_ipp->payload.rawbuf+tmp16; - - //インスタンスをロックする。 - lockResource(i_inst); - - //RSTのチェック。RST受信時は、状態にかかわらず、CLOSEDステータスに移行する。 - if (in_tcpflag & TCP_RST) - { - i_inst->tcpstateflags =UIP_CLOSED; - goto DROP; - } - - - is_new_packet=NyLPC_ntohl(i_ipp->payload.tcp->seqno32)==i_inst->uip_connr.rcv_nxt32; - - - //OPTIONの反映 - - //MSSの取得 - if(NyLPC_TTcpHeader_getTcpMmsOpt(i_ipp->payload.tcp,&tmp16)){ - //取得で着たら更新 - i_inst->uip_connr.peer_mss=tmp16; - } - //受信パケットを元に、未ACKパケットの数を計算 - num_of_noack=getNumOfSending(i_inst,i_ipp->payload.tcp->ackno32);//i_inst->txbuf.num_of_txq; - - //ステータス毎のACK応答 - switch(i_inst->tcpstateflags) - { - case UIP_SYN_RCVD: - //ACKを受信したら、ESTABLISHEDへ。 - //すべてのパケットをACKしたかで判定。() - if(num_of_noack==0){ - i_inst->tcpstateflags=UIP_ESTABLISHED; - }else{ - //それ以外のパケットはドロップする。 - break;//goto DROP; - } - //新しいパケットがなければ、無応答 - if(!is_new_packet){ - break;//goto DROP; - } - //引き続き、ESTABLISHEDの処理へ。 - case UIP_ESTABLISHED: - if(data_size>0){ - if(is_new_packet){ - if(addRecvData(i_inst,tcp_data_offset,data_size)){ - //通常のACK返却 - i_inst->uip_connr.rcv_nxt32+=data_size; - }else{ - //失敗したときは必要に応じて単純ACK - } - } - } - //どちらにしろ、ACK送信 - if(is_new_packet && (in_tcpflag & TCP_FIN)){ - //FINがあるときは、ステータスをCLOSE_WAITへセットして、ACKを返す。 - i_inst->tcpstateflags = UIP_CLOSE_WAIT; - i_inst->uip_connr.rcv_nxt32++; - } - break; - case UIP_CLOSE_WAIT: - //必要に応じたACK応答 - break; - case UIP_LAST_ACK: - //ACK(by FIN)が得られたなら、CLOSEDへ。 - if(num_of_noack==0){ - i_inst->tcpstateflags=UIP_CLOSED; - } - //必要に応じたACK応答 - break; - case UIP_FIN_WAIT_1: - //FIN受信->CLOSINGへ - if(is_new_packet){ - i_inst->uip_connr.rcv_nxt32+=data_size; - if(in_tcpflag & TCP_FIN){ - i_inst->uip_connr.rcv_nxt32++; - if(num_of_noack==0){ - //FINとACKを受信 - i_inst->tcpstateflags=UIP_TIME_WAIT; - }else{ - //FINのみ - i_inst->tcpstateflags=UIP_CLOSING; - } - } - }else if(num_of_noack==0){ - //ACKのみ - i_inst->tcpstateflags=UIP_FIN_WAIT_2; - } - //必要に応じたACK応答 - break; - case UIP_FIN_WAIT_2: - //FIN受信->TIME_WAITへ(pureACK) - if(is_new_packet && (in_tcpflag & TCP_FIN)){ - i_inst->uip_connr.rcv_nxt32++; - i_inst->tcpstateflags=UIP_TIME_WAIT; - } - break; - case UIP_CLOSING: - //ACK受信したら、TIME_WAITへ - if(num_of_noack==0){ - i_inst->tcpstateflags=UIP_TIME_WAIT; - } - break; - case UIP_CLOSED: - //何もできない。何もしない。 - break; - case UIP_TIME_WAIT: - //最終ACKを送り続ける。 - break; - case UIP_SYN_SENT: - //connect関数実行中しか起動しないステータス - if(num_of_noack==0){ - i_inst->tcpstateflags=UIP_ESTABLISHED; - i_inst->uip_connr.rcv_nxt32=NyLPC_ntohl(i_ipp->payload.tcp->seqno32)+1; - }else{ - //それ以外のパケットはドロップする。 - break;//goto DROP; - } - //ACKを送る。 - break; - default: - goto DROP; - } - //ウインドウサイズを更新 - i_inst->uip_connr.peer_win=NyLPC_ntohs(i_ipp->payload.tcp->wnd16); - - //送信キューから、Peerが受信したデータを削除する。 - if(in_tcpflag & TCP_ACK){ - //再送パケットキューから送信済みのデータを回収(後で開放) - NyLPC_Trace(); - s=updateTxQByIndex(i_inst,i_ipp->payload.tcp->ackno32,dlist); - NyLPC_Trace(); - }else{ - s=0; - } - //新しいパケットがきた場合は、再送キューのACKを更新する。 - if(is_new_packet){ - //再送キューのACKを更新 - updateTxAck(i_inst,NyLPC_htonl(i_inst->uip_connr.rcv_nxt32)); - } - - //送信キューのない - if(((in_tcpflag&(TCP_FIN|TCP_SYN))!=0x00) || - ((!is_new_packet) && (data_size>0))) - { - //ソケットからPureACKを生成 as setPacket(i_inst,i_ipp,TCP_ACK,NULL,0); - ret=NyLPC_cUipService_allocSysTxBuf(); - setTxPacket(i_inst,ret,TCP_ACK,NULL,0); - }else{ - ret=NULL; - } - unlockResource(i_inst); - //取り外したTXメモリの開放 - for(i=0;ivhl=0x40|(0x0f&iph_word); - iph->destipaddr=i_src->header->srcipaddr; - iph->srcipaddr =i_src->header->destipaddr; - NyLPC_TIPv4Header_writeTxIpHeader(iph,UIP_PROTO_TCP); - - //TCPヘッダの更新 - tcph=(struct NyLPC_TTcpHeader*)(((NyLPC_TUInt8*)txb)+NyLPC_TIPv4Header_getHeaderLength(iph)); - - tcph->tcpoffset=(tcph_word<<4); - - tcph->flags = TCP_RST | TCP_ACK; - //sorce & destination port - tcph->srcport = i_src->payload.tcp->destport; - tcph->destport = i_src->payload.tcp->srcport; - //ACK number - tcph->ackno32 = NyLPC_htonl(NyLPC_ntohl(i_src->payload.tcp->seqno32)+1); - //Seq Number - tcph->seqno32 = i_src->payload.tcp->ackno32; - //uip_func_tcp_send_noconn(BUF); - tcph->urgp[0] = tcph->urgp[1] = 0; - tcph->tcpchksum= 0; - - - //最終的なパケットサイズと必要ならペイロードを書き込み - iph->len16=NyLPC_htons((iph_word+tcph_word)*4); - //WND設定 - tcph->wnd16=0; - //Checksumの生成 - tcph->tcpchksum=~(NyLPC_TIPv4Header_makeTcpChecksum(iph)); - iph->ipchksum = ~(NyLPC_TIPv4Header_makeIpChecksum(iph)); - return txb; -} - - - - diff --git a/lib/src/uip/NyLPC_cTcpSocket.h b/lib/src/uip/NyLPC_cTcpSocket.h deleted file mode 100644 index 2f08609..0000000 --- a/lib/src/uip/NyLPC_cTcpSocket.h +++ /dev/null @@ -1,226 +0,0 @@ -/********************************************************************************* - * PROJECT: MiMic - * -------------------------------------------------------------------------------- - * - * This file is part of MiMic - * Copyright (C)2011 Ryo Iizuka - * - * MiMic is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published - * by the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program. If not, see . - * - * For further information please contact. - * http://nyatla.jp/ - * or - * - *********************************************************************************/ -#ifndef NYLPC_CTCPSOCKET_H_ -#define NYLPC_CTCPSOCKET_H_ - - - -#include "NyLPC_uip.h" -#include "NyLPC_os.h" -#include "NyLPC_cIPv4Payload.h" -#include "NyLPC_cBaseSocket.h" - - -#ifdef __cplusplus -extern "C" { -#endif /* __cplusplus */ - - -typedef struct NyLPC_TcTcpSocket NyLPC_TcTcpSocket_t; - - -/********************************************************************** - * - * NyLPC_TTcpListenerSynInfo struct - * - **********************************************************************/ - - -struct NyLPC_TTcpSocketSynParam -{ - struct NyLPC_TIPv4Addr srcaddr; - NyLPC_TUInt16 rport; - NyLPC_TUInt16 mss; - NyLPC_TUInt32 rcv_nxt32; -}; - - -/********************************************************************** - * - * NyLPC_TcTcpSocket_TxQItem struct - * - **********************************************************************/ - -/** - * TXキューの数。この値は8未満にしてください。 - */ -#define NyLPC_TcTcpSocket_NUMBER_OF_TXQ 4 - -struct NyLPC_TcTcpSocket_TxQItem -{ - //最終更新時刻 - NyLPC_TUInt32 tick_of_sent; - //このパケットのRTO(秒間隔) - NyLPC_TUInt32 rto32; - void* packet; - //パケットのACK番号。この番号を受信できれば、再送パケットは消去可能である。 - NyLPC_TUInt32 ackno; -}; - -/********************************************************************** - * - * uip_conn struct - * - **********************************************************************/ - -struct uip_conn -{ - struct NyLPC_TIPv4Addr ripaddr; /**< The IP address of the remote host. */ - const struct NyLPC_TIPv4Addr* lipaddr; /**< ローカルIP*/ - NyLPC_TUInt16 lport; /**< The local TCP port, in network byte order. */ - NyLPC_TUInt16 rport; /**< The local remote TCP port, in network byte order. */ - NyLPC_TUInt32 rcv_nxt32; /**< The sequence number that we expect to receive next. */ - NyLPC_TUInt32 snd_nxt32; /**< 送信用sqカウンター*/ - NyLPC_TUInt16 peer_mss; /**< PeerのMSS*/ - NyLPC_TUInt16 default_mss; /**< Peerの初期MMS*/ - /**Peerのウインドウサイズ*/ - NyLPC_TUInt16 peer_win; - NyLPC_TUInt16 _padding; - /**現在ソケットのRTO*/ - NyLPC_TUInt32 current_rto32; -}; - - - - - - - - -/********************************************************************** - * - * NyLPC_TcTcpSocket class - * - **********************************************************************/ - -/** - * uipサービスを使用したTCPソケットクラスです。 - * この関数は2つのタスクから呼び出されます。 - * [uipTask] -> [cTcpSocket] <- [Application] - * ApplicationとuipTaskとの間での排他処理はインスタンスで制御されています。 - * Application側からのコールは内部でuipTaskとの間で排他処理を実行します。 - * Application側からのコールはリエントラントではありません。 - */ - -struct NyLPC_TcTcpSocket -{ - /** Base class*/ - NyLPC_TcBaseSocket_t _super; - //この変数は、uipタスクの実行する関数のみが変更する。 - struct uip_conn uip_connr; - NyLPC_TcFifoBuffer_t rxbuf; - struct{ - NyLPC_TUInt8 rp; - NyLPC_TUInt8 wp; - //送信キュー - struct NyLPC_TcTcpSocket_TxQItem txq[NyLPC_TcTcpSocket_NUMBER_OF_TXQ]; - }txbuf; - volatile NyLPC_TUInt8 tcpstateflags; /**< TCP state and flags. */ -}; - - -#define NyLPC_cTcpSocket_getPeerAddr(i_inst) (&((i_inst)->uip_connr.ripaddr)) -#define NyLPC_cTcpSocket_getPeerPort(i_inst) (((i_inst)->uip_connr.rport)) - -/** - * 初期化関数です。 - * uipserviceは初期化済である必要があります。 - * また、暫定条件として、サービスが停止中である必要もあります。 - * @param i_recv_buf - * 受信バッファを指定します。 - */ -NyLPC_TBool NyLPC_cTcpSocket_initialize(NyLPC_TcTcpSocket_t* i_inst,void* i_rbuf,NyLPC_TUInt16 i_rbuf_len); -/** - * 終期化関数です。 - * uipserviceは初期化済である必要があります。 - * また、暫定条件として、サービスが停止中である必要もあります。 - */ -void NyLPC_cTcpSocket_finalize(NyLPC_TcTcpSocket_t* i_inst); - -NyLPC_TBool NyLPC_cTcpSocket_accept(NyLPC_TcTcpSocket_t* i_inst,NyLPC_TUInt32 i_wait_in_msec); -/** - * @return - * 1 - 以上:受信に成功した。 - * 0 - タイムアウト - * -1 - ソケットがクローズしている - */ -NyLPC_TInt32 NyLPC_cTcpSocket_precv(NyLPC_TcTcpSocket_t* i_inst,const void** o_buf_ptr,NyLPC_TUInt32 i_wait_msec); -void NyLPC_cTcpSocket_pseek(NyLPC_TcTcpSocket_t* i_inst,NyLPC_TUInt16 i_seek); -/** - * 送信未達は保障されません。 - * エラーを検出したら、基本的にはソケットをクローズしてください。 - * @param i_wait_msec - * 送信失敗までの待ち時間を指定します。現在は、 - * RTT推定ができないため、TCPの再送を考慮して、最低でも10秒(10000)程度を指定してください。 - * @return - * 送信したバイト数を返します。エラーならば0未満の数を返します。 - * - */ -NyLPC_TInt32 NyLPC_cTcpSocket_send(NyLPC_TcTcpSocket_t* i_inst,const void* i_buf_ptr,NyLPC_TInt32 i_len,NyLPC_TUInt32 i_wait_in_msec); -void NyLPC_cTcpSocket_close(NyLPC_TcTcpSocket_t* i_inst,NyLPC_TUInt32 i_wait_in_msec); - - -/** - * NyLPC_cTcpSocket_psendで送信するための送信バッファ準備します。 - * @param i_hint - * 送信を希望するデータサイズを指定します。 - * アロケータは出来る限り希望に沿ってメモリを返します。 - * @param o_buf_size - * 取得できたバッファサイズを返します。 - * @return - * 成功した場合、送信バッファを返します。 - * アプリケーションは、可能な限り速やかにデータを書き込んで、NyLPC_cTcpSocket_psendをコールしてください。 - * @note - * Optionフィールドを持つパケットを送信する場合は、オプションデータサイズの合計をデータサイズに指定して、payloadwriterで調整すること。 - */ -void* NyLPC_cTcpSocket_allocSendBuf(NyLPC_TcTcpSocket_t* i_inst,NyLPC_TUInt16 i_hint,NyLPC_TUInt16* o_buf_size,NyLPC_TUInt32 i_wait_in_msec); - -/** - * NyLPC_cTcpSocket_allocSendBufで確保したメモリを開放します。 - */ -void NyLPC_cTcpSocket_releaseSendBuf(NyLPC_TcTcpSocket_t* i_inst,void* i_buf_ptr); - -/** - * 事前にAllocしたTxパケットを送信します。 - * このAPIはゼロコピー送信をサポートするためのものです。 - * @param i_buf_ptr - * allocSendBufで取得したメモリを指定します。 - * @return - * 失敗した場合、メモリは開放されません。 - */ -NyLPC_TBool NyLPC_cTcpSocket_psend(NyLPC_TcTcpSocket_t* i_inst,void* i_buf_ptr,int i_len,NyLPC_TUInt32 i_wait_in_msec); - -/** - * TCPソケットをクライアントとしてサーバへ接続します。 - */ -NyLPC_TBool NyLPC_cTcpSocket_connect(NyLPC_TcTcpSocket_t* i_inst,const struct NyLPC_TIPv4Addr* i_addr,NyLPC_TUInt16 i_peer_port,NyLPC_TUInt32 i_wait_in_msec); - - -#ifdef __cplusplus -} -#endif /* __cplusplus */ - -#endif /* NYLPC_CTCPSOCKET_H_ */ diff --git a/lib/src/uip/NyLPC_cUdpSocket.c b/lib/src/uip/NyLPC_cUdpSocket.c deleted file mode 100644 index 5d1b541..0000000 --- a/lib/src/uip/NyLPC_cUdpSocket.c +++ /dev/null @@ -1,364 +0,0 @@ -/********************************************************************************* - * PROJECT: MiMic - * -------------------------------------------------------------------------------- - * - * This file is part of MiMic - * Copyright (C)2011 Ryo Iizuka - * - * MiMic is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published - * by the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program. If not, see . - * - * For further information please contact. - * http://nyatla.jp/ - * or - * - * - * Parts of this file were leveraged from uIP: - * - * Copyright (c) 2001-2003, Adam Dunkels. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote - * products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS - * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY - * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE - * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ -#include "NyLPC_cUdpSocket_protected.h" -#include "NyLPC_cIPv4Payload_protected.h" -#include "NyLPC_cUipService_protected.h" - -/** - * フラグ値 - */ -#define NyLPC_cUdpSocket_FLAG_BROADCAST 0 - -#define lockResource(i_inst) NyLPC_cMutex_lock(((i_inst)->_smutex)) -#define unlockResource(i_inst) NyLPC_cMutex_unlock(((i_inst)->_smutex)) - -#define SIZE_OF_IPv4_UDPIP_HEADER 28 - - - - -/** - * IP+UDPヘッダサイズを0x05*4+8バイトとして、UDPの送信バッファをセットします。 - */ -static void setUdpTxBufHeader(const NyLPC_TcUdpSocket_t* i_inst,void*i_buf,const struct NyLPC_TIPv4Addr* i_dest_ip,NyLPC_TUInt16 i_dest_port,NyLPC_TUInt8 i_iph_word,NyLPC_TUInt16 i_payload_size) -{ - struct NyLPC_TIPv4Header* header=(struct NyLPC_TIPv4Header*)i_buf; - struct NyLPC_TUdpHeader* udp =(struct NyLPC_TUdpHeader*)(((NyLPC_TUInt8*)i_buf)+i_iph_word*4); - - header->vhl=0x40|(0x0f&i_iph_word); - header->len16=NyLPC_htons(i_payload_size+(i_iph_word*4+8)); - udp->udplen=NyLPC_htons(i_payload_size+(8)); - //IPv4のTxヘッダを書き込む。 - header->destipaddr=*i_dest_ip; - header->srcipaddr =i_inst->uip_udp_conn.lipaddr; - - NyLPC_TIPv4Header_writeTxIpHeader(header,UIP_PROTO_UDP); - - //UDPのTxヘッダを書き込む - //sorce & destination port - udp->srcport = i_inst->uip_udp_conn.lport; - udp->destport = NyLPC_htons(i_dest_port); - udp->udpchksum= 0; - - udp->udpchksum=~(NyLPC_TIPv4Header_makeTcpChecksum(header)); - header->ipchksum = ~(NyLPC_TIPv4Header_makeIpChecksum(header)); -} - - - - -NyLPC_TBool NyLPC_cUdpSocket_initialize(NyLPC_TcUdpSocket_t* i_inst,NyLPC_TUInt16 i_port,void* i_rbuf,NyLPC_TUInt16 i_rbuf_len) -{ - NyLPC_TcUipService_t* srv=_NyLPC_TcUipService_inst; - NyLPC_cBaseSocket_initialize(&(i_inst->_super),NyLPC_TcBaseSocket_TYPEID_UDP_SOCK); - //uipサービスは初期化済であること。 - NyLPC_Assert(NyLPC_TcUipService_isInitService()); - i_inst->_smutex=NyLPC_cIPv4_getSockMutex(&(srv->_tcpv4)); - i_inst->uip_udp_conn.lport=NyLPC_htons(i_port); - i_inst->uip_udp_conn.mcastaddr=NyLPC_TIPv4Addr_ZERO; - i_inst->uip_udp_conn.flags=0x00; - i_inst->as_handler.rx=NULL; - i_inst->as_handler.periodic=NULL; - - NyLPC_cFifoBuffer_initialize(&(i_inst->rxbuf),i_rbuf,i_rbuf_len); - //管理リストへ登録。 - return NyLPC_cIPv4_addSocket(&(srv->_tcpv4),&(i_inst->_super)); -} - -void NyLPC_cUdpSocket_finalize(NyLPC_TcUdpSocket_t* i_inst) -{ - NyLPC_TcUipService_t* srv=_NyLPC_TcUipService_inst; - NyLPC_Assert(NyLPC_TcUipService_isInitService()); - //uipサービスは初期化済であること。 - if(!NyLPC_cIPv4_removeSocket(&(srv->_tcpv4),&(i_inst->_super))){ - //削除失敗、それは死を意味する。 - NyLPC_Abort(); - } - NyLPC_cFifoBuffer_finalize(&(i_inst->rxbuf)); - NyLPC_cBaseSocket_finalize(&(i_inst->_super)); - return; -} -void NyLPC_cUdpSocket_joinMulticast(NyLPC_TcUdpSocket_t* i_inst,const struct NyLPC_TIPv4Addr* i_addr) -{ - i_inst->uip_udp_conn.mcastaddr=*i_addr; -} -void NyLPC_cUdpSocket_setBroadcast(NyLPC_TcUdpSocket_t* i_inst) -{ - NyLPC_TUInt8_setBit(i_inst->uip_udp_conn.flags,NyLPC_cUdpSocket_FLAG_BROADCAST); -} - - - -/** - * この関数は、rxパケットを処理して、ソケットの状態を更新します。 - * uipサービスタスクが実行する関数です。 - * この関数はNyLPC_cTcpSocket_periodicと排他実行すること。 - */ -NyLPC_TBool NyLPC_cUdpSocket_parseRx( - NyLPC_TcUdpSocket_t* i_inst, - const NyLPC_TcIPv4Payload_t* i_ipp) -{ - NyLPC_TUInt16 tmp16; - struct NyLPC_TIPv4RxInfo dheader; - const void* data_offset; - //ブロードキャストの場合、フラグを確認 - if(NyLPC_TIPv4Addr_isEqual(&(i_ipp->header->destipaddr),&NyLPC_TIPv4Addr_BROADCAST)){ - if(!NyLPC_TUInt8_isBitOn(i_inst->uip_udp_conn.flags,NyLPC_cUdpSocket_FLAG_BROADCAST)){ - goto DROP; - } - } - //パラメータの計算 - tmp16=NyLPC_TUdpHeader_getHeaderLength(i_ipp->payload.tcp); - //UDPペイロードの長さは、IPパケットの長さ-(IPヘッダ+UDPヘッダ) - dheader.size=NyLPC_TIPv4Header_getPacketLength(i_ipp->header)-NyLPC_TIPv4Header_getHeaderLength(i_ipp->header)-tmp16; - dheader.peer_ip=i_ipp->header->srcipaddr; - dheader.peer_port=NyLPC_ntohs(i_ipp->payload.udp->srcport); - dheader.ip=i_ipp->header->destipaddr; - dheader.port=NyLPC_ntohs(i_ipp->payload.udp->destport); - if(i_inst->as_handler.rx!=NULL){ - if(!i_inst->as_handler.rx(i_inst,i_ipp->payload.rawbuf+tmp16,&dheader)){ - return NyLPC_TBool_FALSE;//UDPはReturnパケットなし - } - } - //TCPデータオフセット - data_offset=i_ipp->payload.rawbuf+tmp16; - - //インスタンスをロックする。 - lockResource(i_inst); - //受信キューへ追加(データ構造はsize[2]+data[n]).sizeに16ビットの受信サイズ,後続にデータ - - //受信データサイズを確認 - if(NyLPC_cFifoBuffer_getSpace(&(i_inst->rxbuf))rxbuf),&dheader,sizeof(struct NyLPC_TIPv4RxInfo)); - NyLPC_cFifoBuffer_push(&(i_inst->rxbuf),data_offset,dheader.size); - unlockResource(i_inst); - return NyLPC_TBool_FALSE;//UDPはReturnパケットなし -DROP: - unlockResource(i_inst); - return NyLPC_TBool_FALSE; -} - - - -/** - * see Header file - */ -NyLPC_TInt32 NyLPC_cUdpSocket_precv(NyLPC_TcUdpSocket_t* i_inst,const void** o_buf_ptr,const struct NyLPC_TIPv4RxInfo** o_info,NyLPC_TUInt32 i_wait_msec) -{ - NyLPC_TUInt16 rlen; - //タイマを生成 - NyLPC_TcStopwatch_t sw; - NyLPC_cStopwatch_initialize(&sw); - const char* b; - const struct NyLPC_TIPv4RxInfo* rh; - - //ESTABLISHED以外の場合は、エラー。 - NyLPC_cStopwatch_setNow(&sw); - while(NyLPC_cStopwatch_elapseInMsec(&sw)rxbuf)); - //MUTEX UNLOCK - unlockResource(i_inst); - if(rlen>0){ - //受信キューにデータがあれば返す。 - b=(char*)NyLPC_cFifoBuffer_getPtr(&(i_inst->rxbuf)); - rh=(const struct NyLPC_TIPv4RxInfo*)b; - *o_buf_ptr=b+sizeof(struct NyLPC_TIPv4RxInfo); - if(o_info!=NULL){ - *o_info=rh; - } - return rh->size; - } - //タスクスイッチ - NyLPC_cThread_yield(); - }; - NyLPC_cStopwatch_finalize(&sw); - return 0; -} -/** - * See header file - */ -void NyLPC_cUdpSocket_pseek(NyLPC_TcUdpSocket_t* i_inst) -{ - NyLPC_TUInt16 s; - const struct NyLPC_TIPv4RxInfo* rh; - //シークサイズを決定 - s=NyLPC_cFifoBuffer_getLength(&(i_inst->rxbuf)); - if(s==0){ - return; - } - rh=(const struct NyLPC_TIPv4RxInfo*)NyLPC_cFifoBuffer_getPtr(&(i_inst->rxbuf)); - NyLPC_cFifoBuffer_pop(&(i_inst->rxbuf),rh->size+sizeof(struct NyLPC_TIPv4RxInfo)); -} - -/** - * See header file. - */ -void* NyLPC_cUdpSocket_allocSendBuf(NyLPC_TcUdpSocket_t* i_inst,NyLPC_TUInt16 i_hint,NyLPC_TUInt16* o_buf_size,NyLPC_TUInt32 i_wait_in_msec) -{ - NyLPC_TUInt16 s; - void* buf; - NyLPC_TcStopwatch_t sw; - - NyLPC_cStopwatch_initialize(&sw); - NyLPC_cStopwatch_startExpire(&sw,i_wait_in_msec); - - //送信バッファを取得 - //@bug バッファが取れるまで通信がブロックするの。ここはなんとかしないと。 - for(;;){ - buf=NyLPC_cUipService_allocTxBuf(i_hint+(SIZE_OF_IPv4_UDPIP_HEADER),&s); - if(buf!=NULL){ - break; - } - //タイムアウト確認 - if(NyLPC_cStopwatch_isExpired(&sw)){ - return NULL; - } - } - //バッファサイズ確定。 - *o_buf_size=s; - NyLPC_cStopwatch_finalize(&sw); - return (NyLPC_TUInt8*)buf+SIZE_OF_IPv4_UDPIP_HEADER; -} -/** - * See Header file. - */ -void NyLPC_cUdpSocket_releaseSendBuf(NyLPC_TcUdpSocket_t* i_inst,void* i_buf_ptr) -{ - NyLPC_cUipService_releaseTxBuf((NyLPC_TUInt8*)i_buf_ptr-SIZE_OF_IPv4_UDPIP_HEADER); -} - -/** - * See header file - */ -NyLPC_TBool NyLPC_cUdpSocket_psend(NyLPC_TcUdpSocket_t* i_inst,const struct NyLPC_TIPv4Addr* i_addr,NyLPC_TUInt16 i_port,void* i_buf_ptr,int i_len) -{ - void* buf; - //ブロードキャストの場合、フラグを確認 - if(NyLPC_TIPv4Addr_isEqual(i_addr,&NyLPC_TIPv4Addr_BROADCAST)){ - if(!NyLPC_TUInt8_isBitOn(i_inst->uip_udp_conn.flags,NyLPC_cUdpSocket_FLAG_BROADCAST)){ - return NyLPC_TBool_FALSE; - } - } - - //先頭ポインタは、i_buf-sizeof(SIZE_OF_IPv4_TCPIP_HEADER)固定 - buf=(NyLPC_TUInt8*)i_buf_ptr-SIZE_OF_IPv4_UDPIP_HEADER; - - lockResource(i_inst); - //IPv4ペイロードの書き込み - setUdpTxBufHeader(i_inst,buf,i_addr,i_port,0x05,i_len); - unlockResource(i_inst); - // !(BroadCast || Multicast)の場合は送信前にARPテーブルをチェックする。 - if(!(NyLPC_TIPv4Addr_isEqual(i_addr,&NyLPC_TIPv4Addr_BROADCAST) || NyLPC_TIPv4Addr_isEqualWithMask(i_addr,&NyLPC_TIPv4Addr_MULTICAST,&NyLPC_TIPv4Addr_MULTICAST_MASK))){ - if(!NyLPC_cUipService_hasArpInfo(i_addr)){ - NyLPC_cUipService_sendArpRequest(i_addr); - NyLPC_cThread_sleep(30); - } - } - NyLPC_cUipService_sendIPv4Tx(buf); - NyLPC_cUipService_releaseTxBuf(buf); - return NyLPC_TBool_TRUE; -} - -/** - * See header file. - */ -NyLPC_TInt32 NyLPC_cUdpSocket_send(NyLPC_TcUdpSocket_t* i_inst,const struct NyLPC_TIPv4Addr* i_addr,NyLPC_TUInt16 i_port,const void* i_buf_ptr,NyLPC_TInt32 i_len,NyLPC_TUInt32 i_wait_in_msec) -{ - NyLPC_TUInt16 s; - int i; - void* buf; - if(i_len<1 || i_len>1200){ - return 0; - } - //バッファの取得確率を上げるために2倍のサイズを要求 - for(i=0;i<3;i++){ - buf=NyLPC_cUdpSocket_allocSendBuf(i_inst,i_len*2,&s,i_wait_in_msec); - if(buf==NULL || suip_udp_conn.lipaddr=i_config->ip_addr; - //受信バッファのクリア - NyLPC_cFifoBuffer_clear(&(i_inst->rxbuf)); - return; -} - - -void NyLPC_cUdpSocket_stopService(NyLPC_TcUdpSocket_t* i_inst) -{ - //停止処理? -} - diff --git a/lib/src/uip/NyLPC_cUdpSocket.h b/lib/src/uip/NyLPC_cUdpSocket.h deleted file mode 100644 index 6f3e238..0000000 --- a/lib/src/uip/NyLPC_cUdpSocket.h +++ /dev/null @@ -1,159 +0,0 @@ -/* - * NyLPC_cUdpSocket.h - * - * Created on: 2013/05/20 - * Author: nyatla - */ - -#ifndef NYLPC_CUDPSOCKET_H_ -#define NYLPC_CUDPSOCKET_H_ -#include "NyLPC_uip.h" -#include "NyLPC_os.h" -#include "NyLPC_cBaseSocket.h" - - -#ifdef __cplusplus -extern "C" { -#endif /* __cplusplus */ - - -typedef struct NyLPC_TcUdpSocket NyLPC_TcUdpSocket_t; -/** - * 受信情報を格納する構造体 - */ -struct NyLPC_TIPv4RxInfo -{ - NyLPC_TUInt16 size;//パケットサイズ - NyLPC_TUInt16 port;//受信ポート - NyLPC_TUInt16 peer_port;//PEERポート - struct NyLPC_TIPv4Addr ip;//受信IP - struct NyLPC_TIPv4Addr peer_ip;//PEERIP -}; - -/** - * Representation of a uIP UDP connection. - */ -struct uip_udp_conn{ - struct NyLPC_TIPv4Addr lipaddr; /**< The IP address of the remote peer. */ - /** マルチキャスとアドレス(ZEROで無効)*/ - struct NyLPC_TIPv4Addr mcastaddr; - NyLPC_TUInt16 lport; /**< The local port number in network byte order. */ - NyLPC_TUInt8 flags; /**フラグ*/ - NyLPC_TUInt8 padding; /***/ -}; - -/** - * 受信時に非同期にコールされるハンドラ - * UIPサービスタスクが実行する。 - * @return - * TRUEならパケットを受信キューへ追加する。FALSEならパケットを受信キューへ追加しない。 - */ -typedef NyLPC_TBool (*NyLPC_TcUdpSocket_onRxHandler)(NyLPC_TcUdpSocket_t* i_inst,const void* i_buf,const struct NyLPC_TIPv4RxInfo* i_info); -/** - * 非同期にコールされるハンドラ。 - * UIPサービスタスクが実行する。 - */ -typedef void (*NyLPC_TcUdpSocket_onPeriodic)(NyLPC_TcUdpSocket_t* i_inst); - -struct NyLPC_TcUdpSocket -{ - NyLPC_TcBaseSocket_t _super; - //この変数は、uipタスクの実行する関数のみが変更する。 - struct uip_udp_conn uip_udp_conn; - NyLPC_TcFifoBuffer_t rxbuf; - NyLPC_TcMutex_t* _smutex; - struct{ - /** 受信ハンドラ。サービス実装に使用する。*/ - NyLPC_TcUdpSocket_onRxHandler rx; - /** 定期実行ハンドラ。サービス実装に使用する。最低保障周期は1s*/ - NyLPC_TcUdpSocket_onPeriodic periodic; - }as_handler; -}; - - - - - -/** - * @param i_rbuf - * 受信バッファアアドレス。サイズは、(最大受信サイズ-4バイト)*キュー数で計算します。 - * @param i_rbuf_len - * 受信バッファのサイズ。 - */ -NyLPC_TBool NyLPC_cUdpSocket_initialize(NyLPC_TcUdpSocket_t* i_inst,NyLPC_TUInt16 i_port,void* i_rbuf,NyLPC_TUInt16 i_rbuf_len); -void NyLPC_cUdpSocket_finalize(NyLPC_TcUdpSocket_t* i_inst); - -/** - * マルチキャストアドレスに参加する。 - * @param i_addr - * 参加するマルチキャストグループを指定する。 - * 同じマルチキャスとグループに参加できるのは、システムの中で1つに限られます。 - * 0を指定した場合、マルチキャスとグループから離脱します。 - */ -void NyLPC_cUdpSocket_joinMulticast(NyLPC_TcUdpSocket_t* i_inst,const struct NyLPC_TIPv4Addr* i_addr); - -/** - * ブロードキャストに参加する。 - */ -void NyLPC_cUdpSocket_setBroadcast(NyLPC_TcUdpSocket_t* i_inst); - - -/** - * この関数は、ソケットの受信バッファの読み取り位置と、読み出せるデータサイズを返却します。 - * 関数はポインターを返却するだけで、バッファの読み取り位置をシークしません。 - * シークするにはNyLPC_cTcpSocket_pseekを使います。 - */ -NyLPC_TInt32 NyLPC_cUdpSocket_precv(NyLPC_TcUdpSocket_t* i_inst,const void** o_buf_ptr,const struct NyLPC_TIPv4RxInfo** o_info,NyLPC_TUInt32 i_wait_msec); -/** - * 受信バッファを次のバッファまでシークします。 - */ -void NyLPC_cUdpSocket_pseek(NyLPC_TcUdpSocket_t* i_inst); - -/** - * 送信バッファを割り当てて返します。 - * 割り当てたメモリは、releaseSendBuf関数か、psend関数を成功させて開放する必要があります。 - * @param i_hint - * 取得したいメモリサイズをセットします。 - * 関数は要求サイズより小さいメモリを返すことがあります。 - */ -void* NyLPC_cUdpSocket_allocSendBuf(NyLPC_TcUdpSocket_t* i_inst,NyLPC_TUInt16 i_hint,NyLPC_TUInt16* o_buf_size,NyLPC_TUInt32 i_wait_in_msec); - -void NyLPC_cUdpSocket_releaseSendBuf(NyLPC_TcUdpSocket_t* i_inst,void* i_buf_ptr); - -/** - * 事前にAllocしたTxパケットを送信します。 - * このAPIはゼロコピー送信をサポートするためのものです。 - * @param i_buf_ptr - * allocSendBufで取得したメモリを指定します。 - * @return - * 関数が失敗した場合、i_buf_ptrは「開放されません。」 - */ -NyLPC_TBool NyLPC_cUdpSocket_psend(NyLPC_TcUdpSocket_t* i_inst,const struct NyLPC_TIPv4Addr* i_addr,NyLPC_TUInt16 i_port,void* i_buf_ptr,int i_len); - -/** - * 最大送信サイズは1200バイトです。 - */ -NyLPC_TInt32 NyLPC_cUdpSocket_send(NyLPC_TcUdpSocket_t* i_inst,const struct NyLPC_TIPv4Addr* i_addr,NyLPC_TUInt16 i_port,const void* i_buf_ptr,NyLPC_TInt32 i_len,NyLPC_TUInt32 i_wait_in_msec); - -/** - * 非同期パケットハンドラを設定する。 - */ -#define NyLPC_cUdpSocket_setOnRxHandler(i_inst,i_handler) (i_inst)->as_handler.rx=i_handler; - -/** - * 非同期タイマ呼び出しハンドラを設定する。 - */ -#define NyLPC_cUdpSocket_setOnPeriodicHandler(i_inst,i_handler) (i_inst)->as_handler.periodic=i_handler; - -/** - * ソケットのローカルIPのアドレスを返す。 - * 値はuipが動作中のみ有効。 - */ -#define NyLPC_cUdpSocket_getSockIP(i_inst) (&(i_inst)->uip_udp_conn.lipaddr) - -#ifdef __cplusplus -} -#endif /* __cplusplus */ - - -#endif /* NYLPC_CUDPSOCKET_H_ */ diff --git a/lib/src/uip/NyLPC_cUipService.c b/lib/src/uip/NyLPC_cUipService.c deleted file mode 100644 index 6d75d89..0000000 --- a/lib/src/uip/NyLPC_cUipService.c +++ /dev/null @@ -1,630 +0,0 @@ -/********************************************************************************* - * PROJECT: MiMic - * -------------------------------------------------------------------------------- - * - * This file is part of MiMic - * Copyright (C)2011 Ryo Iizuka - * - * MiMic is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published - * by the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program. If not, see . - * - * For further information please contact. - * http://nyatla.jp/ - * or - * - * - * Parts of this file were leveraged from uIP: - * - * Copyright (c) 2001-2003, Adam Dunkels. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote - * products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS - * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY - * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE - * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ -#include "NyLPC_cUipService_protected.h" -#include "NyLPC_cIPv4IComp_protected.h" -#include "NyLPC_cTcpListener_protected.h" -#include "NyLPC_stdlib.h" -#include "NyLPC_uip.h" - - - - - - - -/**************************************************** - * UipServiceに関する宣言:その他 - ***************************************************/ -/** - * イーサネットフレームの読み出し構造体 - */ -struct TEthPacket -{ - struct NyLPC_TEthernetIIHeader header; - union{ - struct NyLPC_TArpHeader arp; - struct NyLPC_TIPv4Header ipv4; - }data; -}PACK_STRUCT_END; - - - -/** - * サービスインスタンスのポインタ。サービスが稼働中はインスタンスのポインタが有効です。 - */ -NyLPC_TcUipService_t* _NyLPC_TcUipService_inst=NULL; - -/** - * 唯一のインスタンス - */ -static NyLPC_TcUipService_t _service_instance; - - - - -/** - * uipタスク - */ -static int uipTask(void *pvParameters); - -/** イーサネットドライバからのハンドラ*/ -static void ethernet_handler(void* i_param,NyLPC_TiEthernetDevice_EVENT i_type); - -//-------------------------------------------------------------- - - -static NyLPC_TBool sendIPv4Tx(struct NyLPC_TTxBufferHeader* i_eth_buf); - -//static void sendArpReqest(const struct TEthPacket* i_eth_packet); -static void sendRawEthFrameNL(void* i_buf,NyLPC_TUInt16 i_len); -static void releaseTxBufNL(void* i_buf); - -/**メッセージなし*/ -#define TTaskMessage_MSG_NULL 0x0000 -/**uipコアタスクに、開始要求する*/ -#define TTaskMessage_MSG_RUN 0x0001 -/**uipコアタスクに、停止要求する*/ -#define TTaskMessage_MSG_STOP 0x0002 - - -NyLPC_TcThread_t th; - -NyLPC_TBool NyLPC_cUipService_initialize(void) -{ - NyLPC_TcUipService_t* inst=&_service_instance; - //サービスは停止している事。 - Service must be uninitialized. - NyLPC_Assert(!NyLPC_TcUipService_isInitService()); - //IP処理部分の初期化 - NyLPC_cIPv4_initialize(&(inst->_tcpv4)); - //EMAC割込セマフォ - NyLPC_cSemaphore_initialize(&inst->_emac_semapho); - - inst->_status=0x00; - NyLPC_cStopwatch_initialize(&(inst->_arp_sw)); - NyLPC_cStopwatch_initialize(&(inst->_periodic_sw)); - NyLPC_cIPv4_initialize(&(inst->_tcpv4)); - NyLPC_AbortIfNot(NyLPC_cMutex_initialize(&(inst->_mutex))); - - _NyLPC_TcUipService_inst=inst; - //タスク起動 - NyLPC_cThread_initialize(&th,NyLPC_cUipService_config_STACK_SIZE,NyLPC_TcThread_PRIORITY_SERVICE); - NyLPC_cThread_start(&th,uipTask,NULL); - return NyLPC_TBool_TRUE; -} - - - - - - - -/** - * UIP処理を開始します。 - * この関数はリエントラントではありません。複数のタスクから共有するときには、排他ロックを掛けてください。 - * @param i_ref_config - * このコンフィギュレーションは、stopを実行するまでの間、インスタンスから参照します。外部で保持してください。 - */ -void NyLPC_cUipService_start(const NyLPC_TcIPv4Config_t* i_ref_config) -{ - NyLPC_TcUipService_t* inst=&_service_instance; - NyLPC_Assert(NyLPC_TcUipService_isInitService()); - if(!NyLPC_cUipService_isRun()) - { - //はじめて起動するときに1度だけデバイス取得(タスクスイッチが動いてないと動かないからここで。) - if(inst->_ethif==NULL){ - inst->_ethif=getEthernetDevicePnP(); - } - //コンフィグレーションセット - inst->_ref_config=i_ref_config; - //開始要求セット - NyLPC_TUInt16_setBit(inst->_status,NyLPC_TcUipService_ORDER_START); - //Order実行待ち - while(NyLPC_TUInt16_isBitOn(inst->_status,NyLPC_TcUipService_ORDER_START)){ - NyLPC_cThread_sleep(10); - } - } - return; -} -/** - * UIP処理を停止します。 - * この関数はリエントラントではありません。複数のタスクから共有するときには、排他ロックを掛けてください。 - * いまのところ、ストップシーケンスの実装は良くありません。 - * 再設計が必要。 - */ -void NyLPC_cUipService_stop(void) -{ - NyLPC_TcUipService_t* inst=&_service_instance; - NyLPC_Assert(NyLPC_TcUipService_isInitService()); - if(NyLPC_cUipService_isRun()) - { - NyLPC_TUInt16_setBit(inst->_status,NyLPC_TcUipService_ORDER_STOP); - //Order実行待ち - while(NyLPC_TUInt16_isBitOn(inst->_status,NyLPC_TcUipService_ORDER_STOP)){ - NyLPC_cThread_sleep(10); - } - } - return; -} - - -const char* NyLPC_cUipService_refDeviceName(void) -{ - NyLPC_TcUipService_t* inst=&_service_instance; - return NyLPC_cUipService_isRun()?NyLPC_iEthernetDevice_getDevicName(inst->_ethif):NULL; -} -const NyLPC_TcIPv4Config_t* NyLPC_cUipService_refCurrentConfig(void) -{ - NyLPC_TcUipService_t* inst=&_service_instance; - return inst->_ref_config; -} -/********************************************************************** - * - * - * - *********************************************************************/ - - -//PERIODIC rate -#define PERIODIC_TIMER (1*200) -#define ARP_TIMER (60*1000*10) - - - -/** - * 操作キューを確認して、タスクのステータスをアップデートします。 - * 高速化のため、Proc-Callerを使用していません。複雑なタスク操作をするときには、書き換えてください。 - * @return - * UIPタスクの実行状態 - */ -static NyLPC_TBool updateTaskStatus() -{ - NyLPC_TcUipService_t* inst=_NyLPC_TcUipService_inst; - if(NyLPC_cUipService_isRun()){ - //開始状態 - if(NyLPC_TUInt16_isBitOn(inst->_status,NyLPC_TcUipService_ORDER_STOP)) - { - //停止操作 - NyLPC_iEthernetDevice_stop(inst->_ethif); - NyLPC_cIPv4_stop(&(inst->_tcpv4)); - NyLPC_cIPv4IComp_finalize(&(inst->_icomp)); - NyLPC_cIPv4Arp_finalize(&(inst->_arp)); - inst->_ref_config=NULL; - NyLPC_TUInt16_unsetBit(inst->_status,NyLPC_TcUipService_STATUSBIT_IS_RUNNING); - NyLPC_TUInt16_unsetBit(inst->_status,NyLPC_TcUipService_ORDER_STOP); - return NyLPC_TBool_FALSE; - } - return NyLPC_TBool_TRUE; - }else{ - //停止状態 - if(NyLPC_TUInt16_isBitOn(inst->_status,NyLPC_TcUipService_ORDER_START)) - { - //TCP,ICOMPの初期化 - NyLPC_cIPv4_start(&(inst->_tcpv4),inst->_ref_config); - NyLPC_cIPv4IComp_initialize(&(inst->_icomp),inst->_ref_config); - //uip_arp_init(msg->start.ref_config); - NyLPC_cIPv4Arp_initialize(&(inst->_arp),inst->_ref_config); - NyLPC_cStopwatch_startExpire(&(inst->_arp_sw),1);//1度ARPを起動するため。 - NyLPC_cStopwatch_startExpire(&(inst->_periodic_sw),PERIODIC_TIMER); - //EtherNETデバイス初期化 - while(!NyLPC_iEthernetDevice_start(inst->_ethif,&(inst->_ref_config->eth_mac),ethernet_handler,inst)); - NyLPC_TUInt16_setBit(inst->_status,NyLPC_TcUipService_STATUSBIT_IS_RUNNING); - NyLPC_TUInt16_unsetBit(inst->_status,NyLPC_TcUipService_ORDER_START); - return NyLPC_TBool_TRUE; - } - return NyLPC_TBool_FALSE; - } -} - -/** - * uipタスクのメインループ - */ -static int uipTask(void *pvParameters) -{ - NyLPC_TUInt16 rx_len,tx_len; - struct TEthPacket* ethbuf; - NyLPC_TcUipService_t* inst=_NyLPC_TcUipService_inst; - void* r; - (void)pvParameters; - for( ;; ) - { - //タスク状態の更新 - if(!updateTaskStatus()) - { - //RUNステータス以外の時は、ここで終了する。 - NyLPC_cThread_sleep(50); - continue; - } - //イーサネットフレームの取得 - //Ethernet Device Lock(ARPを含む) - NyLPC_cMutex_lock(&(inst->_mutex)); - ethbuf= (struct TEthPacket*)NyLPC_iEthernetDevice_getRxEthFrame(inst->_ethif,&rx_len); - tx_len=0; - while(ethbuf != NULL){ - if(rx_len>0) - { - //ペイロードサイズを計算 - rx_len-=sizeof(struct NyLPC_TEthernetIIHeader); - switch(ethbuf->header.type) - { - case NyLPC_HTONS(NyLPC_TEthernetIIHeader_TYPE_IP): - //ARPテーブルの更新 - //uip_arp_ipin(&(ethbuf->header),ethbuf->data.ipv4.srcipaddr); - NyLPC_cIPv4Arp_incomingIp(&inst->_arp,&(ethbuf->header),ethbuf->data.ipv4.srcipaddr); - //Ethernet Device UnLock(NyLPC_cIPv4_rxがallocをコールする可能性があるので一時的にロック解除) - NyLPC_cMutex_unlock(&(inst->_mutex)); - //IPパケットの処理 - r=NyLPC_cIPv4_rx(&(inst->_tcpv4),&(ethbuf->data.ipv4),rx_len); - if(r!=NULL){ - //IPパケットとして送信 - NyLPC_cUipService_sendIPv4Tx(r); - } - //ロックの復帰 - NyLPC_cMutex_lock(&(inst->_mutex)); - if(r!=NULL){ - releaseTxBufNL(r); - } - break; - case NyLPC_HTONS(NyLPC_TEthernetIIHeader_TYPE_ARP): - //Ethernet Device UnLock(NyLPC_cIPv4_rxがallocをコールする可能性があるので一時的にロック解除) - NyLPC_cMutex_unlock(&(inst->_mutex)); - r=NyLPC_cIPv4Arp_rx(&inst->_arp,&(ethbuf->data.arp),rx_len,&tx_len); - NyLPC_cMutex_lock(&(inst->_mutex)); - if(r!=NULL){ - sendRawEthFrameNL(r,tx_len); - releaseTxBufNL(r); - } - break; - case NyLPC_HTONS(NyLPC_TEthernetIIHeader_TYPE_IPV6): - //uip_process_ipv6(); - break; - default: - break; - } - } - //受信キューを進行。 - NyLPC_iEthernetDevice_nextRxEthFrame(inst->_ethif); - //受信処理 - ethbuf= (struct TEthPacket*)NyLPC_iEthernetDevice_getRxEthFrame(inst->_ethif,&rx_len); - } - //データが無い。 - if(NyLPC_cStopwatch_isExpired(&(inst->_arp_sw))){ - //uip_arp_timer(); - NyLPC_cIPv4Arp_periodic(&inst->_arp); - NyLPC_cStopwatch_startExpire(&(inst->_arp_sw),ARP_TIMER); - } - if(NyLPC_cStopwatch_isExpired(&(inst->_periodic_sw))){ - NyLPC_cMutex_unlock(&(inst->_mutex)); - NyLPC_cIPv4_periodec(&(inst->_tcpv4)); - NyLPC_cMutex_lock(&(inst->_mutex)); - NyLPC_cStopwatch_startExpire(&(inst->_periodic_sw),PERIODIC_TIMER); - } - //リソースロックの解除 - NyLPC_cMutex_unlock(&(inst->_mutex)); - //割込によるセマフォの解除か、タイムアウトで再開する。(タイムアウト値は周期関数の実行レート以下にすること。) - NyLPC_cSemaphore_take(&(_NyLPC_TcUipService_inst->_emac_semapho),PERIODIC_TIMER); - } - return 0; -} - - -/** - * イーサネットドライバからのハンドラ - */ -static void ethernet_handler(void* i_param,NyLPC_TiEthernetDevice_EVENT i_type) -{ - switch(i_type){ - case NyLPC_TiEthernetDevice_EVENT_ON_RX: - //受信系のセマフォブロックの解除 - NyLPC_cSemaphore_giveFromISR(&(((NyLPC_TcUipService_t*)i_param)->_emac_semapho)); - break; - default: - break; - } -} - -/** - * IPv4パケットのpeerIPを問い合わせるARPパケットを送信します。 - * allocを中でコールするから要UNLOCK状態 - */ -void NyLPC_cUipService_sendArpRequest(const struct NyLPC_TIPv4Addr* i_addr) -{ - NyLPC_TcUipService_t* inst=_NyLPC_TcUipService_inst; - struct NyLPC_TTxBufferHeader* p; - NyLPC_TUInt16 tx_len; - struct TEthPacket* ethbuf; - //システムTxBufを得る - ethbuf=(struct TEthPacket*)NyLPC_cUipService_allocSysTxBuf(); - //ARPパケットを作る。 - NyLPC_TArpHeader_setArpRequest(&(ethbuf->data.arp),inst->_ref_config->ip_addr,&(inst->_ref_config->eth_mac),i_addr); - tx_len=NyLPC_TEthernetIIHeader_setArpTx(&(ethbuf->header),&(inst->_ref_config->eth_mac)); - //送信 - p=((struct NyLPC_TTxBufferHeader*)(((struct NyLPC_TEthernetIIHeader*)ethbuf)-1))-1; - - NyLPC_cMutex_lock(&(inst->_mutex)); - NyLPC_iEthernetDevice_sendTxEthFrame(inst->_ethif,p,tx_len); - NyLPC_iEthernetDevice_releaseTxBuf(inst->_ethif,p); - NyLPC_cMutex_unlock(&(inst->_mutex)); -} - - - - -/** - * allocTxBufで取得したペイロードメモリを"IPパケットとして"送信します。 - * @param i_eth_payload - * [NyLPC_TTxBufferHeader][NyLPC_TEthernetIIHeader][payload]メモリの、[payload]のアドレスを指定します。 - * 通常は、NyLPC_cUipService_allocTxBufの返却したメモリを指定します。 - */ - -void NyLPC_cUipService_sendIPv4Tx(void* i_eth_payload) -{ - NyLPC_TcUipService_t* inst=_NyLPC_TcUipService_inst; - struct NyLPC_TTxBufferHeader* p=((struct NyLPC_TTxBufferHeader*)(((struct NyLPC_TEthernetIIHeader*)i_eth_payload)-1))-1; - NyLPC_cMutex_lock(&(inst->_mutex)); - //IPパケットの送信を試行 - if(sendIPv4Tx(p)){ - NyLPC_cMutex_unlock(&(inst->_mutex)); - return; - } - NyLPC_cMutex_unlock(&(inst->_mutex)); - //ARPリクエストを代わりに送信 - NyLPC_cUipService_sendArpRequest(&((struct NyLPC_TIPv4Header*)i_eth_payload)->destipaddr); - return; -} - - -/** - * ARPテーブルに指定したIPがあるかを返します。 - */ -NyLPC_TBool NyLPC_cUipService_hasArpInfo(const struct NyLPC_TIPv4Addr* i_addr) -{ - NyLPC_TcUipService_t* inst=_NyLPC_TcUipService_inst; - return NyLPC_cIPv4Arp_IPv4toEthAddr(&inst->_arp,*i_addr)!=NULL; -} - -/** - * システム用の送信ペイロードを返します。 - * 関数は必ず成功します。 - */ -void* NyLPC_cUipService_allocSysTxBuf(void) -{ - NyLPC_TUInt16 s; - NyLPC_TcUipService_t* inst=_NyLPC_TcUipService_inst; - struct NyLPC_TTxBufferHeader* ethbuf; - //排他処理をして、メモリを取得する。SYSTEMメモリはEthernetドライバの解放待ちのみなのでまとめてLOCKしておk - NyLPC_cMutex_lock(&(inst->_mutex)); - for(;;){ - ethbuf=(struct NyLPC_TTxBufferHeader*)NyLPC_iEthernetDevice_allocTxBuf(inst->_ethif,NyLPC_TcEthernetMM_HINT_CTRL_PACKET,&s); - if(ethbuf==NULL){ - NyLPC_cThread_yield(); - continue; - } - break; - } - NyLPC_cMutex_unlock(&(inst->_mutex)); - //イーサネットバッファのアドレスを計算 - return &(((struct TEthPacket*)(ethbuf+1))->data); -} - - - -void* NyLPC_cUipService_allocTxBuf(NyLPC_TUInt16 i_hint,NyLPC_TUInt16* o_size) -{ - NyLPC_TcUipService_t* inst=_NyLPC_TcUipService_inst; - struct NyLPC_TTxBufferHeader* ethbuf; - //排他処理をして、メモリを取得する。 - NyLPC_cMutex_lock(&(inst->_mutex)); - ethbuf=(struct NyLPC_TTxBufferHeader*)NyLPC_iEthernetDevice_allocTxBuf(inst->_ethif,i_hint+sizeof(struct NyLPC_TEthernetIIHeader),o_size); - NyLPC_cMutex_unlock(&(inst->_mutex)); - if(ethbuf==NULL){ - return NULL; - } - //イーサネットバッファのサイズを計算 - *o_size-=sizeof(struct NyLPC_TEthernetIIHeader); - //イーサネットバッファのアドレスを計算 - return &(((struct TEthPacket*)(ethbuf+1))->data); -} - - -void* NyLPC_cUipService_releaseTxBuf(void* i_buf) -{ - //排他処理をして、メモリを開放する。 - NyLPC_TcUipService_t* inst=_NyLPC_TcUipService_inst; - NyLPC_cMutex_lock(&(inst->_mutex)); - //ペイロードの位置から、メモリブロックを再生。 - NyLPC_iEthernetDevice_releaseTxBuf(inst->_ethif,((struct NyLPC_TTxBufferHeader*)(((struct NyLPC_TEthernetIIHeader*)i_buf)-1))-1); - NyLPC_cMutex_unlock(&(inst->_mutex)); - return NULL; -} - - - - - - - - -/********** - * イーサネットHWのコントロール関数 - */ -/** - * "IPv4パケットを格納した"イーサフレームを送信します。 - * コール前に、必ずロックしてから呼び出してください。 - *//* -static void copyAndSendIPv4Tx(const struct TEthPacket* i_buf) -{ - NyLPC_TcUipService_t* inst=_NyLPC_TcUipService_inst; - NyLPC_TUInt16 s; - //送信する。 - s=NyLPC_htons(i_buf->data.ipv4.len16)+sizeof(struct NyLPC_TEthernetIIHeader); - memcpy(inst->stx.buf,i_buf,s); - if(!sendIPv4Tx(&(inst->stx.h))){ - //失敗した場合はARPリクエストに変換して再送 -//@todo unchecked PASS! - sendArpReqest(&i_buf->data.ipv4.destipaddr); - } - return; -}*/ -/** - * "IPv4パケットを格納した"イーサフレームを送信します。 - * コール前に、必ずロックしてから呼び出してください。 - */ -/* -static void copyAndSendIPv4Tx(const struct TEthPacket* i_buf) -{ - NyLPC_TcUipService_t* inst=_NyLPC_TcUipService_inst; - NyLPC_TUInt16 s; - //ACK送信用の自己バッファが空くまで待つ - while(inst->stx.h.is_lock){ - NyLPC_iEthernetDevice_processTx(inst->_ethif); - } - //送信する。 - s=NyLPC_htons(i_buf->data.ipv4.len16)+sizeof(struct NyLPC_TEthernetIIHeader); - memcpy(inst->stx.buf,i_buf,s); - if(!sendIPv4Tx(&(inst->stx.h))){ - //失敗した場合はARPリクエストに変換して再送 -//@todo unchecked PASS! - sendArpReqest(&i_buf->data.ipv4.destipaddr); - } - return; -} -*/ - - -/** - * イーサネットフレームを送信します。 - * この関数はiptaskで実行される関数からのみ使用てください。 - * @i_buf - * イーサネットフレームを格納したメモリです。 - * @i_len - * イーサネットペイロードのサイズです。 - */ -static void sendRawEthFrameNL(void* i_buf,NyLPC_TUInt16 i_len) -{ - NyLPC_iEthernetDevice_sendTxEthFrame( - _NyLPC_TcUipService_inst->_ethif, - ((struct NyLPC_TTxBufferHeader*)(((struct NyLPC_TEthernetIIHeader*)i_buf)-1))-1, - i_len); - return; -} -/** - * ロック状態で使用できるreleaseTxBuf。 - * この関数はiptaskで実行される関数からのみ使用してください。 - */ -static void releaseTxBufNL(void* i_buf) -{ - //ペイロードの位置から、メモリブロックを再生。 - NyLPC_iEthernetDevice_releaseTxBuf( - _NyLPC_TcUipService_inst->_ethif, - ((struct NyLPC_TTxBufferHeader*)(((struct NyLPC_TEthernetIIHeader*)i_buf)-1))-1); - return; -} -/** - * マルチキャスとアドレスへ変換する。 - */ -static void ip2MulticastEmacAddr(const struct NyLPC_TIPv4Addr* i_addr,struct NyLPC_TEthAddr* o_emac) -{ - NyLPC_TUInt32 n=NyLPC_htonl(i_addr->v); - o_emac->addr[0]=0x01; - o_emac->addr[1]=0x00; - o_emac->addr[2]=0x5E; - o_emac->addr[3]=((n>>16) & 0x7f); - o_emac->addr[4]=((n>> 8) & 0xff); - o_emac->addr[5]=(n & 0xff); - return; -}; - -/** - * ペイロードをIPパケットとしてネットワークへ送出します。 - * コール前に、必ずロックしてから呼び出してください。 - * @param i_eth_payload - * allocTxBufで確保したメモリを指定してください。 - * ペイロードには、TCP/IPパケットを格納します。 - */ -static NyLPC_TBool sendIPv4Tx(struct NyLPC_TTxBufferHeader* i_eth_buf) -{ - NyLPC_TcUipService_t* inst=_NyLPC_TcUipService_inst; - struct NyLPC_TEthAddr emac; - NyLPC_TUInt16 tx_len; - const struct NyLPC_TEthAddr* eth_dest; - struct TEthPacket* ethbuf=(struct TEthPacket*)(i_eth_buf+1); - //ペイロードのアドレスから、イーサネットフレームバッファのアドレスを復元 - - if(NyLPC_TIPv4Addr_isEqual(&(ethbuf->data.ipv4.destipaddr),&NyLPC_TIPv4Addr_BROADCAST)) { - //ブロードキャストならそのまま - eth_dest=&NyLPC_TEthAddr_BROADCAST; - }else if(NyLPC_TIPv4Addr_isEqualWithMask(&(ethbuf->data.ipv4.destipaddr),&NyLPC_TIPv4Addr_MULTICAST,&NyLPC_TIPv4Addr_MULTICAST_MASK)){ - //マルチキャスト - ip2MulticastEmacAddr(&(ethbuf->data.ipv4.destipaddr),&emac); - eth_dest=&emac; - }else{ - //LocalIP以外ならdefaultRootへ変換 - eth_dest=NyLPC_cIPv4Arp_IPv4toEthAddr( - &inst->_arp, - NyLPC_cIPv4Config_isLocalIP(inst->_ref_config, &(ethbuf->data.ipv4.destipaddr))?(ethbuf->data.ipv4.destipaddr):(inst->_ref_config->dr_addr)); - //IP->MAC変換をテスト。 - if(eth_dest==NULL){ - return NyLPC_TBool_FALSE; - } - } - //変換可能なら、イーサネットヘッダを更新して、送信処理へ。 - tx_len=NyLPC_TEthernetIIHeader_setIPv4Tx(&(ethbuf->header),&(inst->_ref_config->eth_mac),eth_dest); - NyLPC_iEthernetDevice_sendTxEthFrame(inst->_ethif,i_eth_buf,tx_len); - return NyLPC_TBool_TRUE; -} - - - - diff --git a/lib/src/uip/NyLPC_uip.c b/lib/src/uip/NyLPC_uip.c deleted file mode 100644 index f6ba82d..0000000 --- a/lib/src/uip/NyLPC_uip.c +++ /dev/null @@ -1,431 +0,0 @@ -/********************************************************************************* - * PROJECT: MiMic - * -------------------------------------------------------------------------------- - * - * This file is part of MiMic - * Copyright (C)2011 Ryo Iizuka - * - * MiMic is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published - * by the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program. If not, see . - * - * For further information please contact. - * http://nyatla.jp/ - * or - * - * - * Parts of this file were leveraged from uIP: - * - * Copyright (c) 2001-2003, Adam Dunkels. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote - * products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS - * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY - * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE - * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ -#include "NyLPC_uip.h" - - - -const struct NyLPC_TIPv4Addr NyLPC_TIPv4Addr_ZERO={0x00000000}; -const struct NyLPC_TIPv4Addr NyLPC_TIPv4Addr_ALL ={0xffffffff}; -const struct NyLPC_TIPv4Addr NyLPC_TIPv4Addr_BROADCAST = { 0xffffffff }; -const struct NyLPC_TIPv4Addr NyLPC_TIPv4Addr_MULTICAST = NyLPC_TIPv4Addr_pack(224,0,0,0); -const struct NyLPC_TIPv4Addr NyLPC_TIPv4Addr_MULTICAST_MASK = NyLPC_TIPv4Addr_pack(224,0,0,0); -const struct NyLPC_TIPv4Addr NyLPC_TIPv4Addr_APIPA_MASK = NyLPC_TIPv4Addr_pack(255,255,0,0); - -NyLPC_TInt16 NyLPC_TIPv4Addr_toString(const struct NyLPC_TIPv4Addr* i_ip,NyLPC_TChar* i_buf) -{ - NyLPC_TUInt32 ip; - NyLPC_TChar* p=i_buf; - NyLPC_TUInt8 v; - NyLPC_TInt8 l; - //IPをホストオーダーにする。 - ip=NyLPC_NTOHL(i_ip->v); - for(l=3;l>=0;l--){ - v=(ip>>(8*l))&0xff; - if(v>=100){ - *p=(v/100)+'0'; - p++; - } - if(v>=10){ - *p=((v%100)/10)+'0'; - p++; - } - *p=(v%10)+'0'; - *(p+1)='.'; - p+=2; - } - *(p-1)='\0'; - return p-i_buf-1; -} - - -NyLPC_TUInt16 NyLPC_uip_chksum(NyLPC_TUInt16 sum, const NyLPC_TUInt8 *data, NyLPC_TUInt16 len) -{ - NyLPC_TUInt16 t; - const NyLPC_TUInt8 *dataptr; - const NyLPC_TUInt8 *last_byte; - - dataptr = data; - last_byte = data + len - 1; - - while (dataptr < last_byte) { /* At least two more bytes */ - t = (dataptr[0] << 8) + dataptr[1]; - sum += t; - if (sum < t) { - sum++; /* carry */ - } - dataptr += 2; - } - - if (dataptr == last_byte) { - t = (dataptr[0] << 8) + 0; - sum += t; - if (sum < t) { - sum++; /* carry */ - } - } - - /* Return sum in host byte order. */ - return sum; -} - -/*-------------------------------------------------------------------------------- - * - * struct NyLPC_TEthernetIIHeader - * - *------------------------------------------------------------------------------*/ - -/** - * Ethernetヘッダの内容を、ARPパケットの内容に一致するように書き換えます。 - * i_structの後方にあるものと仮定します。 - * 戻り値は、フレームの長さです。 - */ -NyLPC_TUInt16 NyLPC_TEthernetIIHeader_setArpTx( - struct NyLPC_TEthernetIIHeader* i_struct, - const struct NyLPC_TEthAddr* i_my_eth_addr) -{ - struct NyLPC_TArpHeader* arph=(struct NyLPC_TArpHeader*)(((NyLPC_TUInt8*)i_struct)+sizeof(struct NyLPC_TEthernetIIHeader)); - - i_struct->type = NyLPC_HTONS(NyLPC_TEthernetIIHeader_TYPE_ARP); - switch(arph->opcode){ - case NyLPC_HTONS(ARP_REPLY): - memcpy(i_struct->src.addr, i_my_eth_addr->addr, 6); - memcpy(i_struct->dest.addr, arph->dhwaddr.addr, 6); - break; - case NyLPC_HTONS(ARP_REQUEST): - memset(i_struct->dest.addr, 0xff, 6); - memcpy(i_struct->src.addr, i_my_eth_addr->addr, 6); - break; - } - return sizeof(struct NyLPC_TEthernetIIHeader)+sizeof(struct NyLPC_TArpHeader); -} - -/** - * イーサネットヘッダをIPv4向けにセットアップする。 - * 関数は、ペイロードをIPv4ヘッダとして、フレームサイズを計算する。 - */ -NyLPC_TUInt16 NyLPC_TEthernetIIHeader_setIPv4Tx( - struct NyLPC_TEthernetIIHeader* i_eth, - const struct NyLPC_TEthAddr* i_src_eth_addr, - const struct NyLPC_TEthAddr* i_dest_eth_addr) -{ - struct NyLPC_TIPv4Header* iph=(struct NyLPC_TIPv4Header*)(((NyLPC_TUInt8*)i_eth)+sizeof(struct NyLPC_TEthernetIIHeader)); - - i_eth->type = NyLPC_HTONS(NyLPC_TEthernetIIHeader_TYPE_IP); - /* Build an ethernet header. */ - memcpy(i_eth->dest.addr,i_dest_eth_addr, 6); - memcpy(i_eth->src.addr, i_src_eth_addr->addr, 6); - - - //IPフラグメントに応じたサイズ計算 - switch(iph->proto){ - case UIP_PROTO_TCP: - return sizeof(struct NyLPC_TEthernetIIHeader)+NyLPC_htons(iph->len16); - case UIP_PROTO_UDP: - return sizeof(struct NyLPC_TEthernetIIHeader)+NyLPC_htons(iph->len16); - case UIP_PROTO_ICMP: - return sizeof(struct NyLPC_TEthernetIIHeader)+NyLPC_htons(iph->len16); - } - return 0; -} -/*-------------------------------------------------------------------------------- - * - * struct NyLPC_TIPv4Header - * - *------------------------------------------------------------------------------*/ - -/** - * based on uip_ipchksum - */ -NyLPC_TUInt16 NyLPC_TIPv4Header_makeIpChecksum(const struct NyLPC_TIPv4Header* ip_header) -{ - NyLPC_TUInt16 sum; - sum = NyLPC_uip_chksum(0, (const NyLPC_TUInt8 *)ip_header,NyLPC_TIPv4Header_getHeaderLength(ip_header)); - return (sum == 0) ? 0xffff : NyLPC_htons(sum); -} - - - -NyLPC_TBool NyLPC_TIPv4Header_isCorrectIpCheckSum(const struct NyLPC_TIPv4Header* ip_header) -{ - return (NyLPC_TIPv4Header_makeIpChecksum(ip_header)==0xffff); -} - -NyLPC_TBool NyLPC_cIPv4Packet_isCorrectTcpCheckSum(const struct NyLPC_TIPv4Header* ip_header) -{ - return (NyLPC_TIPv4Header_makeTcpChecksum(ip_header) == 0xffff); -} - - - - -/** - * TCPチェックサムを計算します。 - * ペイロードはIPヘッダの後方に連続して存在する物と仮定します。 - * i_lenは、ペイロード長さ - */ -NyLPC_TUInt16 NyLPC_TIPv4Header_makeTcpChecksum( - const struct NyLPC_TIPv4Header* i_iph) -{ - NyLPC_TUInt16 sum; - NyLPC_TUInt16 iph_len=NyLPC_TIPv4Header_getHeaderLength(i_iph); - NyLPC_TUInt16 len = NyLPC_ntohs((i_iph)->len16)- iph_len; - NyLPC_ArgAssert(i_iph!=NULL); - /*TCP疑似ヘッダ部分*/ - /* IP protocol and length fields. This addition cannot carry. */ - sum = len + i_iph->proto; - /* Sum IP source and destination addresses. */ - sum = NyLPC_uip_chksum(sum, (NyLPC_TUInt8 *) &(i_iph->srcipaddr), 2 * sizeof(struct NyLPC_TIPv4Addr)); - /* Sum TCP header and data. */ - sum = NyLPC_uip_chksum(sum, (((NyLPC_TUInt8 *)(i_iph))+iph_len),len); - // sum = chksum(sum, &uip_buf[UIP_IPH_LEN + UIP_LLH_LEN], i_len_of_data); - return (sum == 0) ? 0xffff : NyLPC_htons(sum); -} - -static NyLPC_TUInt16 pid=0x3939; -/** - * IPヘッダを送信パケット用に設定する。 - * ipid16にはコールされるたびに新しい値を設定する。 - * ipcecksumには0を設定する。 - * この関数は、パケットサイズ,ローカルIP/リモートIPの設定はしない。 - */ -void NyLPC_TIPv4Header_writeTxIpHeader( - struct NyLPC_TIPv4Header* i_struct, - NyLPC_TUInt8 i_proto) -{ - //IPパケットのセット - i_struct->proto=i_proto; - i_struct->ttl = UIP_DEFAULT_IP_TTL; - i_struct->tos = 0; - i_struct->ipid16=(pid++); - i_struct->ipoffset=0;//NyLPC_HTONS(0|0x4000); - i_struct->ipchksum = 0; -} -/*-------------------------------------------------------------------------------- - * - * struct NyLPC_TIPv6Header - * - *------------------------------------------------------------------------------*/ -#define IPV6_HEADER_SIZE 40 -/** - * IPヘッダーを作って埋める関数 - */ -void NyLPC_TIPv6Header_setSendHeader( - struct NyLPC_TIPv6Header* i_iph, - uip_ip6addr_t i_src, - uip_ip6addr_t i_dest, - NyLPC_TUInt8 i_proto, - NyLPC_TUInt8 i_ttl, - NyLPC_TUInt16 i_len) -{ - i_iph->srcipaddr=i_src; - i_iph->destipaddr=i_dest; - i_iph->proto=i_proto; - i_iph->ttl = i_ttl; - i_iph->vtc = 0x60; - i_iph->tcflow = 0x00; - i_iph->flow = 0x00; - i_iph->len16= NyLPC_htons(i_len - IPV6_HEADER_SIZE); - return; -} - - -/** - * チェックサムは、TCP疑似ヘッダから計算。 - * i_tcpiphの送信/受信アドレス、ProtocolID,DATAフィールドは有効であること。 - */ -NyLPC_TUInt16 NyLPC_TIPv6Header_makeTcpChecksum( - struct NyLPC_TIPv6Header* i_iph, - NyLPC_TUInt16 i_len) -{ - NyLPC_TUInt16 sum; - NyLPC_TUInt16 len; - len = i_len; - /*TCP疑似ヘッダ部分*/ - /* IP protocol and length fields. This addition cannot carry. */ - sum = len + i_iph->proto; - /* Sum IP source and destination addresses. */ - sum = NyLPC_uip_chksum(sum, (NyLPC_TUInt8 *) &(i_iph->srcipaddr), 2 * sizeof(uip_ip6addr_t)); - /* Sum TCP header and data. */ - sum = NyLPC_uip_chksum(sum, (((NyLPC_TUInt8 *)(i_iph))+IPV6_HEADER_SIZE),len); - // sum = chksum(sum, &uip_buf[UIP_IPH_LEN + UIP_LLH_LEN], i_len_of_data); - return (sum == 0) ? 0xffff : NyLPC_htons(sum); -} - - - -/*-------------------------------------------------------------------------------- - * - * struct NyLPC_TTcpHeader - * - *------------------------------------------------------------------------------*/ - - -/** - * MMSオプションの値を返す。 - */ -NyLPC_TBool NyLPC_TTcpHeader_getTcpMmsOpt( - const struct NyLPC_TTcpHeader* i_struct,NyLPC_TUInt16* o_val) -{ - NyLPC_TUInt8* opt; - opt=NyLPC_TTcpHeader_getTcpOptFragmentPtr(i_struct,TCP_OPT_MSS); - if(opt!=NULL){ - if (*(opt+1) == TCP_OPT_MSS_LEN) - { - // An MSS option with the right option length. - *o_val = ((NyLPC_TUInt16) (*(opt+2)) << 8) | (NyLPC_TUInt16) (*(opt + 3)); - //And we are done processing options. - return NyLPC_TBool_TRUE; - } - } - return NyLPC_TBool_FALSE; -} - -#define DEFAULT_TCP_HEADER_LEN 20 -/** - * TCPフラグメントのポインタを返す。 - */ -NyLPC_TUInt8* NyLPC_TTcpHeader_getTcpOptFragmentPtr( - const struct NyLPC_TTcpHeader* i_struct, - NyLPC_TUInt8 i_opt_id) -{ - NyLPC_TUInt8 opt; - int c; - NyLPC_TUInt8* opt_buf=((NyLPC_TUInt8*)(i_struct+1)); - - /* Parse the TCP MSS option, if present. */ - if ((i_struct->tcpoffset & 0xf0) > 0x50){ - for (c = 0; c < ((i_struct->tcpoffset >> 4) - 5) << 2;) - { - opt=opt_buf[c]; - if(opt==i_opt_id){ - return opt_buf+c;//found! - } - switch(opt) - { - case TCP_OPT_NOOP: - continue;//NOP option. - case TCP_OPT_END: - return NULL;//End of options. - default: - // All other options have a length field, so that we easily can skip past them. - if (opt_buf[1 + c] == 0) { - // If the length field is zero, the options are malformed and we don't process them further. - NyLPC_OnErrorGoto(ERROR_INVALID_OPTION); - } - c += opt_buf[1 + c]; - } - } - } -ERROR_INVALID_OPTION: - return NULL; -} -/* - * Optionパラメタを書きだす。 - */ -void NyLPC_TTcpHeader_setMmsOpt(NyLPC_TUInt8* i_opt_addr,NyLPC_TUInt16 i_mms) -{ - i_opt_addr[0] = TCP_OPT_MSS; - i_opt_addr[1] = TCP_OPT_MSS_LEN; - i_opt_addr[2] = (i_mms) / 256; - i_opt_addr[3] = (i_mms) & 255; - return; -} - - -NyLPC_TUInt16 NyLPC_TTcpHeader_getHeaderLength(const struct NyLPC_TTcpHeader* ip_header) -{ - return (ip_header->tcpoffset>>4)*4; -} -/*-------------------------------------------------------------------------------- - * - * struct NyLPC_TUdpHeader - * - *------------------------------------------------------------------------------*/ - - - -/*-------------------------------------------------------------------------------- - * - * struct NyLPC_TArpHeader - * - *------------------------------------------------------------------------------*/ -/** - * i_req_addrを問い合わせるARP_REQUESTを生成します。 - */ -void NyLPC_TArpHeader_setArpRequest( - struct NyLPC_TArpHeader* i_struct, - const struct NyLPC_TIPv4Addr i_saddr, - const struct NyLPC_TEthAddr* i_srceth, - const struct NyLPC_TIPv4Addr* i_req_addr) -{ - memset(i_struct->dhwaddr.addr, 0x00, 6); - memcpy(i_struct->shwaddr.addr, i_srceth, 6); - i_struct->dipaddr=*i_req_addr; - i_struct->sipaddr=i_saddr; - i_struct->opcode = NyLPC_HTONS(ARP_REQUEST); /* ARP request. */ - i_struct->hwtype = NyLPC_HTONS(ARP_HWTYPE_ETH); - i_struct->protocol = NyLPC_HTONS(NyLPC_TEthernetIIHeader_TYPE_IP); - i_struct->hwlen = 6; - i_struct->protolen = 4; - return; -} - -/*-------------------------------------------------------------------------------- - * - * class IPv4Route - * - *------------------------------------------------------------------------------*/ - - diff --git a/lib/src/utils/NyLPC_cFormatTextReader.c b/lib/src/utils/NyLPC_cFormatTextReader.c index 06e6916..4ae7991 100644 --- a/lib/src/utils/NyLPC_cFormatTextReader.c +++ b/lib/src/utils/NyLPC_cFormatTextReader.c @@ -20,7 +20,7 @@ NyLPC_TInt32 NyLPC_cFormatTextReader_readWord(const NyLPC_TChar* buf,const NyLPC const NyLPC_TChar* p=buf; *top=p; for(;*p!='\0' && (isalnum(*p)|| (strchr("_-",*p)!=NULL));p++);//skip words - return p-(*top); + return p-(*top); } /** @@ -34,28 +34,28 @@ NyLPC_TInt32 NyLPC_cFormatTextReader_readWord(const NyLPC_TChar* buf,const NyLPC */ NyLPC_TInt32 NyLPC_cFormatTextReader_readIpAddr(const NyLPC_TChar* buf,NyLPC_TUInt8* v) { - NyLPC_TInt32 t; + NyLPC_TInt32 t; const NyLPC_TChar* p=buf; NyLPC_TInt32 i; for(i=0;i<4;i++){ t=0; - for(;isdigit(*p);p++){ - t=t*10+NyLPC_ctoi(*p); - if(t>255){ - return 0; - } + for(;isdigit(*p);p++){ + t=t*10+NyLPC_ctoi(*p); + if(t>255){ + return 0; + } + } + v[i]=t; + if(i<3){ + if(*p!='.'){ + return 0; + } + p++; + }else if(!isspace(*p) && *p!='\0'){ + return 0; } - v[i]=t; - if(i<3){ - if(*p!='.'){ - return 0; - } - p++; - }else if(!isspace(*p) && *p!='\0'){ - return 0; - } } - return (p-buf); + return (p-buf); } /** * 文字列から10進数の数値を読み出します。 @@ -64,14 +64,14 @@ NyLPC_TInt32 NyLPC_cFormatTextReader_readIpAddr(const NyLPC_TChar* buf,NyLPC_TUI */ NyLPC_TInt32 NyLPC_cFormatTextReader_readUInt(const NyLPC_TChar* buf,NyLPC_TUInt32* v) { - NyLPC_TUInt32 t; + NyLPC_TUInt32 t; const NyLPC_TChar* p=buf; - t=0; - for(;isdigit(*p);p++){ - t=t*10+NyLPC_ctoi(*p); - } - *v=t; - return (p-buf); + t=0; + for(;isdigit(*p);p++){ + t=t*10+NyLPC_ctoi(*p); + } + *v=t; + return (p-buf); } /** @@ -82,27 +82,27 @@ NyLPC_TInt32 NyLPC_cFormatTextReader_readUInt(const NyLPC_TChar* buf,NyLPC_TUInt */ NyLPC_TInt32 NyLPC_cFormatTextReader_readMacAddr(const NyLPC_TChar* buf,NyLPC_TUInt8* v) { - NyLPC_TInt32 t,i; + NyLPC_TInt32 t,i; const NyLPC_TChar* p=buf; for(i=0;i<6;i++){ t=0; - for(;isxdigit(*p);p++){ - t=t*16+NyLPC_ctox(*p); - if(t>255){ - return 0; - } + for(;isxdigit(*p);p++){ + t=t*16+NyLPC_ctox(*p); + if(t>255){ + return 0; + } + } + v[i]=t; + if(i<5){ + if(*p!=':'){ + return 0; + } + p++; + }else if(!isspace(*p) && *p!='\0'){ + return 0; } - v[i]=t; - if(i<5){ - if(*p!=':'){ - return 0; - } - p++; - }else if(!isspace(*p) && *p!='\0'){ - return 0; - } } - return (p-buf); + return (p-buf); } /** * 連続するスペースを読み飛ばします。 @@ -111,9 +111,9 @@ NyLPC_TInt32 NyLPC_cFormatTextReader_readMacAddr(const NyLPC_TChar* buf,NyLPC_TU */ NyLPC_TInt32 NyLPC_cFormatTextReader_seekSpace(const NyLPC_TChar* s) { - const NyLPC_TChar* p=s; - for(;*p!='\0' && isspace(*p);p++); - return p-s; + const NyLPC_TChar* p=s; + for(;*p!='\0' && isspace(*p);p++); + return p-s; } diff --git a/lib/src/utils/NyLPC_cFormatWriter.c b/lib/src/utils/NyLPC_cFormatWriter.c index 26af5fc..070549b 100644 --- a/lib/src/utils/NyLPC_cFormatWriter.c +++ b/lib/src/utils/NyLPC_cFormatWriter.c @@ -7,250 +7,250 @@ #define NUM_OF_WORK 16 NyLPC_TBool NyLPC_cFormatWriter_print(NyLPC_cFormatWriter_printHandler i_handler,void* i_inst,const NyLPC_TChar* i_fmt,va_list args) { - const char* rp=i_fmt; - const char* sp; - char wk[NUM_OF_WORK]; - NyLPC_TUInt8 ftype; - NyLPC_TUInt32 ut; - NyLPC_TInt16 ol=0; - while(*rp!='\0'){ - if(*rp=='%'){ - ftype=FTYPE_NOTHING; - rp++; - FMT_NEXT: - switch (*rp){ - case '.': - //%.*(s) - if(*(rp+1)=='*'){ - //%.* - ftype=FTYPE_LENGTH; - rp+=2; - goto FMT_NEXT; - } - //その他 - wk[ol]=*rp; - ol++; - rp++; - break; - case 's': - switch(ftype){ - case FTYPE_LENGTH: - //%.*sの場合 - ut=va_arg(args,NyLPC_TUInt32); - break; - default: - ut=0x7FFFFFFF; - } - sp=va_arg(args,const char*); - while(*sp!=0 && ut>0){ - wk[ol]=*sp; - ol++; - sp++; - //バッファフルなら書込み。 - if(ol>=NUM_OF_WORK){ - i_handler(i_inst,wk,NUM_OF_WORK); - ol=0; - } - ut--; - } - rp++; - continue; - case 'c': - wk[ol]=(char)va_arg(args,int); - rp++; - ol++; - break; - case 'd': - //ワークを空にする。 - if(ol>0){ - i_handler(i_inst,wk,ol); - ol=0; - } - NyLPC_itoa((va_arg(args,int)),wk,10); - //強制コミット - i_handler(i_inst,wk,strlen(wk)); - rp++; - continue; - case 'u': - //ワークを空にする。 - if (ol>0){ - i_handler(i_inst, wk, ol); - ol = 0; - } - NyLPC_uitoa((va_arg(args, NyLPC_TUInt32)), wk, 10); - //強制コミット - i_handler(i_inst, wk, strlen(wk)); - rp++; - continue; - //16進数 - case 'x': - //ワークを空にする。 - if(ol>0){ - i_handler(i_inst,wk,ol); - ol=0; - } - NyLPC_uitoa((va_arg(args,unsigned int)),wk,16); - //強制コミット - i_handler(i_inst,wk,strlen(wk)); - rp++; - continue; - //BYTE値のHEXエンコード文字列(XX形式のテキスト。%.*Bのみ) - case 'B': - switch(ftype){ - case FTYPE_LENGTH: - //%.*Bの場合 - ut=va_arg(args,NyLPC_TUInt32); - break; - default: - ut=0; - } - sp=va_arg(args,const char*); - while(ut>0){ - //2文字以上の空きがないなら書き込み - if (ol >= (NUM_OF_WORK - 2)){ - i_handler(i_inst, wk, ol); - ol = 0; - } - NyLPC_uitoa2((int)(*sp), wk + ol, 16, 2); - ol += 2; - sp++; - ut--; - } - rp++; - continue; - case '%': - wk[ol]='%'; - ol++; - rp++; - break; - case '\0': - //オワタ(ループ抜けるためにrpはそのまま。) - break; - default: - wk[ol]=*rp; - ol++; - } - //バッファフルなら書込み。 - if(ol>=NUM_OF_WORK){ - i_handler(i_inst,wk,NUM_OF_WORK); - ol=0; - } - }else if(*rp==0){ - //オワタ - break; - }else{ - wk[ol]=*rp; - ol++; - rp++; - if(ol>=NUM_OF_WORK){ - i_handler(i_inst,wk,NUM_OF_WORK); - ol=0; - } - } - } - //どこかでエラーが起こってればFALSE返す。 - return i_handler(i_inst,wk,ol); + const char* rp=i_fmt; + const char* sp; + char wk[NUM_OF_WORK]; + NyLPC_TUInt8 ftype; + NyLPC_TUInt32 ut; + NyLPC_TInt16 ol=0; + while(*rp!='\0'){ + if(*rp=='%'){ + ftype=FTYPE_NOTHING; + rp++; + FMT_NEXT: + switch (*rp){ + case '.': + //%.*(s) + if(*(rp+1)=='*'){ + //%.* + ftype=FTYPE_LENGTH; + rp+=2; + goto FMT_NEXT; + } + //その他 + wk[ol]=*rp; + ol++; + rp++; + break; + case 's': + switch(ftype){ + case FTYPE_LENGTH: + //%.*sの場合 + ut=va_arg(args,NyLPC_TUInt32); + break; + default: + ut=0x7FFFFFFF; + } + sp=va_arg(args,const char*); + while(*sp!=0 && ut>0){ + wk[ol]=*sp; + ol++; + sp++; + //バッファフルなら書込み。 + if(ol>=NUM_OF_WORK){ + i_handler(i_inst,wk,NUM_OF_WORK); + ol=0; + } + ut--; + } + rp++; + continue; + case 'c': + wk[ol]=(char)va_arg(args,int); + rp++; + ol++; + break; + case 'd': + //ワークを空にする。 + if(ol>0){ + i_handler(i_inst,wk,ol); + ol=0; + } + NyLPC_itoa((va_arg(args,int)),wk,10); + //強制コミット + i_handler(i_inst,wk,strlen(wk)); + rp++; + continue; + case 'u': + //ワークを空にする。 + if (ol>0){ + i_handler(i_inst, wk, ol); + ol = 0; + } + NyLPC_uitoa((va_arg(args, NyLPC_TUInt32)), wk, 10); + //強制コミット + i_handler(i_inst, wk, strlen(wk)); + rp++; + continue; + //16進数 + case 'x': + //ワークを空にする。 + if(ol>0){ + i_handler(i_inst,wk,ol); + ol=0; + } + NyLPC_uitoa((va_arg(args,unsigned int)),wk,16); + //強制コミット + i_handler(i_inst,wk,strlen(wk)); + rp++; + continue; + //BYTE値のHEXエンコード文字列(XX形式のテキスト。%.*Bのみ) + case 'B': + switch(ftype){ + case FTYPE_LENGTH: + //%.*Bの場合 + ut=va_arg(args,NyLPC_TUInt32); + break; + default: + ut=0; + } + sp=va_arg(args,const char*); + while(ut>0){ + //2文字以上の空きがないなら書き込み + if (ol >= (NUM_OF_WORK - 2)){ + i_handler(i_inst, wk, ol); + ol = 0; + } + NyLPC_uitoa2((int)(*sp), wk + ol, 16, 2); + ol += 2; + sp++; + ut--; + } + rp++; + continue; + case '%': + wk[ol]='%'; + ol++; + rp++; + break; + case '\0': + //オワタ(ループ抜けるためにrpはそのまま。) + break; + default: + wk[ol]=*rp; + ol++; + } + //バッファフルなら書込み。 + if(ol>=NUM_OF_WORK){ + i_handler(i_inst,wk,NUM_OF_WORK); + ol=0; + } + }else if(*rp==0){ + //オワタ + break; + }else{ + wk[ol]=*rp; + ol++; + rp++; + if(ol>=NUM_OF_WORK){ + i_handler(i_inst,wk,NUM_OF_WORK); + ol=0; + } + } + } + //どこかでエラーが起こってればFALSE返す。 + return i_handler(i_inst,wk,ol); } NyLPC_TInt16 NyLPC_cFormatWriter_length(const NyLPC_TChar* i_fmt,va_list args) { - const char* rp=i_fmt; - const char* sp; - char wk[NUM_OF_WORK]; - NyLPC_TUInt32 ut; - NyLPC_TUInt8 ftype; - NyLPC_TInt16 len=0; - while(*rp!='\0'){ - if(*rp=='%'){ - ftype=FTYPE_NOTHING; - rp++; - FMT_NEXT: - switch (*rp){ - case '.': - //%.*(s) - if(*(rp+1)=='*'){ - //%.* - ftype=FTYPE_LENGTH; - rp+=2; - goto FMT_NEXT; - } - //その他 - len++; - rp++; - break; - case 's': - switch(ftype){ - case FTYPE_LENGTH: - //%.*sの場合 - ut=va_arg(args,NyLPC_TUInt32); - break; - default: - ut=0x7FFFFFFF; - } - sp=va_arg(args,const char*); - while(*sp!=0 && ut>0){ - len++; - sp++; - ut--; - } - rp++; - continue; - case 'c': - va_arg(args,int); - len++; - rp++; - break; - case 'd': - NyLPC_itoa((va_arg(args,int)),wk,10); - //強制コミット - len+=(NyLPC_TInt16)strlen(wk); - rp++; - continue; - case 'u': - //ワークを空にする。 - NyLPC_uitoa((va_arg(args, NyLPC_TUInt32)), wk, 10); - //強制コミット - len += (NyLPC_TInt16)strlen(wk); - rp++; - continue; - case 'x': - NyLPC_uitoa((va_arg(args,unsigned int)),wk,16); - //強制コミット - len+=(NyLPC_TInt16)strlen(wk); - rp++; - continue; - //BYTE値のHEXエンコード文字列(XX形式のテキスト。%.*Bのみ) - case 'B': - switch(ftype){ - case FTYPE_LENGTH: - //%.*Bの場合 - ut=va_arg(args,NyLPC_TUInt32); - break; - default: - ut=0; - } - sp=va_arg(args,const char*); - len += (NyLPC_TInt16)ut * 2; - rp++; - continue; - case '%': - len++; - rp++; - break; - case '\0': - //オワタ(ループ抜けるためにrpはそのまま。) - break; - default: - len++; - } - }else if(*rp==0){ - //オワタ - break; - }else{ - len++; - rp++; - } - } - //どこかでエラーが起こってればFALSE返す。 - return len; + const char* rp=i_fmt; + const char* sp; + char wk[NUM_OF_WORK]; + NyLPC_TUInt32 ut; + NyLPC_TUInt8 ftype; + NyLPC_TInt16 len=0; + while(*rp!='\0'){ + if(*rp=='%'){ + ftype=FTYPE_NOTHING; + rp++; + FMT_NEXT: + switch (*rp){ + case '.': + //%.*(s) + if(*(rp+1)=='*'){ + //%.* + ftype=FTYPE_LENGTH; + rp+=2; + goto FMT_NEXT; + } + //その他 + len++; + rp++; + break; + case 's': + switch(ftype){ + case FTYPE_LENGTH: + //%.*sの場合 + ut=va_arg(args,NyLPC_TUInt32); + break; + default: + ut=0x7FFFFFFF; + } + sp=va_arg(args,const char*); + while(*sp!=0 && ut>0){ + len++; + sp++; + ut--; + } + rp++; + continue; + case 'c': + va_arg(args,int); + len++; + rp++; + break; + case 'd': + NyLPC_itoa((va_arg(args,int)),wk,10); + //強制コミット + len+=(NyLPC_TInt16)strlen(wk); + rp++; + continue; + case 'u': + //ワークを空にする。 + NyLPC_uitoa((va_arg(args, NyLPC_TUInt32)), wk, 10); + //強制コミット + len += (NyLPC_TInt16)strlen(wk); + rp++; + continue; + case 'x': + NyLPC_uitoa((va_arg(args,unsigned int)),wk,16); + //強制コミット + len+=(NyLPC_TInt16)strlen(wk); + rp++; + continue; + //BYTE値のHEXエンコード文字列(XX形式のテキスト。%.*Bのみ) + case 'B': + switch(ftype){ + case FTYPE_LENGTH: + //%.*Bの場合 + ut=va_arg(args,NyLPC_TUInt32); + break; + default: + ut=0; + } + sp=va_arg(args,const char*); + len += (NyLPC_TInt16)ut * 2; + rp++; + continue; + case '%': + len++; + rp++; + break; + case '\0': + //オワタ(ループ抜けるためにrpはそのまま。) + break; + default: + len++; + } + }else if(*rp==0){ + //オワタ + break; + }else{ + len++; + rp++; + } + } + //どこかでエラーが起こってればFALSE返す。 + return len; } diff --git a/lib/src/utils/NyLPC_cFormatWriter.h b/lib/src/utils/NyLPC_cFormatWriter.h index 524dd0c..0a7f626 100644 --- a/lib/src/utils/NyLPC_cFormatWriter.h +++ b/lib/src/utils/NyLPC_cFormatWriter.h @@ -12,13 +12,13 @@ typedef NyLPC_TBool(*NyLPC_cFormatWriter_printHandler)(void* i_inst,const void* * printfライクな書式出力関数です。i_handlerへi_fmtに示される書式文字列を出力します。 * @param i_fmt * フォーマット文字列。以下の形式をサポートします。 - * %d int値 - * %u uint値 - * %c char値 - * %% '%' - * %s,%.*s 文字列 + * %d int値 + * %u uint値 + * %c char値 + * %% '%' + * %s,%.*s 文字列 * 独自拡張 - * %.*B BYTE配列をXX形式で並べた文字列 + * %.*B BYTE配列をXX形式で並べた文字列 * */ NyLPC_TBool NyLPC_cFormatWriter_print(NyLPC_cFormatWriter_printHandler i_handler,void* i_inst,const NyLPC_TChar* i_fmt,va_list args); diff --git a/lib/src/utils/NyLPC_cRomFileSet.c b/lib/src/utils/NyLPC_cRomFileSet.c index 9c3a750..72ee968 100644 --- a/lib/src/utils/NyLPC_cRomFileSet.c +++ b/lib/src/utils/NyLPC_cRomFileSet.c @@ -19,8 +19,8 @@ * along with this program. If not, see . * * For further information please contact. - * http://nyatla.jp/ - * or + * http://nyatla.jp/ + * or * *********************************************************************************/ #include "NyLPC_cRomFileSet.h" @@ -30,20 +30,20 @@ */ void NyLPC_cRomFileSet_initialize(NyLPC_TcRomFileSet_t* i_inst,const struct NyLPC_TRomFileData* i_ref_fs[],NyLPC_TUInt32 i_num_of_file) { - i_inst->_ref_fs=i_ref_fs; - i_inst->_num_of_fs=i_num_of_file; - return; + i_inst->_ref_fs=i_ref_fs; + i_inst->_num_of_fs=i_num_of_file; + return; } /** * See Header file. */ const struct NyLPC_TRomFileData* NyLPC_cRomFileSet_getFilaData(NyLPC_TcRomFileSet_t* i_inst,const NyLPC_TChar* i_name) { - int i; - for(i=0;i_num_of_fs;i++){ - if(strcmp(i_inst->_ref_fs[i]->name,i_name)==0){ - return i_inst->_ref_fs[i]; - } - } - return NULL; + int i; + for(i=0;i_num_of_fs;i++){ + if(strcmp(i_inst->_ref_fs[i]->name,i_name)==0){ + return i_inst->_ref_fs[i]; + } + } + return NULL; } diff --git a/lib/src/utils/NyLPC_cRomFileSet.h b/lib/src/utils/NyLPC_cRomFileSet.h index 9c42825..647dae5 100644 --- a/lib/src/utils/NyLPC_cRomFileSet.h +++ b/lib/src/utils/NyLPC_cRomFileSet.h @@ -19,8 +19,8 @@ * along with this program. If not, see . * * For further information please contact. - * http://nyatla.jp/ - * or + * http://nyatla.jp/ + * or * *********************************************************************************/ #ifndef NYLPC_CROMFILESET_H_ @@ -36,10 +36,10 @@ extern "C" { * オンメモリファイルの構造体。 */ struct NyLPC_TRomFileData{ - const char* name; - NyLPC_TUInt32 size; - const char* content_type; - const char* data; + const char* name; + NyLPC_TUInt32 size; + const char* content_type; + const char* data; }; /** * このクラスは、NyLPC_TRomFileData構造体の配列を管理します。 @@ -48,8 +48,8 @@ typedef struct NyLPC_TcRomFileSet NyLPC_TcRomFileSet_t; struct NyLPC_TcRomFileSet { - const struct NyLPC_TRomFileData** _ref_fs; - NyLPC_TUInt32 _num_of_fs; + const struct NyLPC_TRomFileData** _ref_fs; + NyLPC_TUInt32 _num_of_fs; }; /** diff --git a/lib/src/utils/NyLPC_cUuid.h b/lib/src/utils/NyLPC_cUuid.h index 8b2cb99..1f4cd32 100644 --- a/lib/src/utils/NyLPC_cUuid.h +++ b/lib/src/utils/NyLPC_cUuid.h @@ -19,14 +19,14 @@ * along with this program. If not, see . * * For further information please contact. - * http://nyatla.jp/ - * or + * http://nyatla.jp/ + * or * *********************************************************************************/ #ifndef NYLPC_CUUID_H_ #define NYLPC_CUUID_H_ #include "NyLPC_stdlib.h" -#include "NyLPC_uipService.h" +#include "NyLPC_netif.h" #ifdef __cplusplus @@ -37,12 +37,12 @@ extern "C" { typedef struct NyLPC_TcUuid NyLPC_TcUuid_t; struct NyLPC_TcUuid { - NyLPC_TUInt32 f1; //time-L - NyLPC_TUInt16 f2; //time-M - NyLPC_TUInt16 f3; //time-H version - NyLPC_TUInt8 f4; //SQ-H RES - NyLPC_TUInt8 f5; //SQ-L - NyLPC_TUInt8 f6[6];//NODE + NyLPC_TUInt32 f1; //time-L + NyLPC_TUInt16 f2; //time-M + NyLPC_TUInt16 f3; //time-H version + NyLPC_TUInt8 f4; //SQ-H RES + NyLPC_TUInt8 f5; //SQ-L + NyLPC_TUInt8 f6[6];//NODE }; void NyLPC_cUuid_initialize(NyLPC_TcUuid_t* i_inst); #define NyLPC_cUuid_finalize(i_inst) diff --git a/lib/src/utils/sha1/sha1.c b/lib/src/utils/sha1/sha1.c index dbf4178..0559f2a 100644 --- a/lib/src/utils/sha1/sha1.c +++ b/lib/src/utils/sha1/sha1.c @@ -226,3 +226,4 @@ FILE* file; exit(0); } #endif + diff --git a/lib/src/utils/sha1/sha1.h b/lib/src/utils/sha1/sha1.h index 09cc267..94d3467 100644 --- a/lib/src/utils/sha1/sha1.h +++ b/lib/src/utils/sha1/sha1.h @@ -37,3 +37,4 @@ void SHA1Final(unsigned char digest[20], SHA1_CTX* context); #define __SHA1_INCLUDE_ #endif /* __SHA1_INCLUDE_ */ + diff --git a/projects/app.RemoteMCU/.cproject b/projects/app.RemoteMCU/.cproject index a3b9f7a..4b5bf6b 100644 --- a/projects/app.RemoteMCU/.cproject +++ b/projects/app.RemoteMCU/.cproject @@ -1,7 +1,5 @@ - - - + @@ -9,18 +7,18 @@ - - + + - + @@ -47,9 +45,9 @@ + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + - + @@ -753,12 +188,13 @@ + @@ -787,584 +223,20 @@ + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -1373,8 +245,8 @@ <?xml version="1.0" encoding="UTF-8"?> <TargetConfig> -<Properties property_0="" property_1="" property_2="" property_3="NXP" property_4="LPC1768" property_count="5" version="1"/> -<infoList vendor="NXP"><info chip="LPC1768" match_id="0x00013f37,0x26013F37,0x26113F37" name="LPC1768" package="lpc17_lqfp100.xml"><chip><name>LPC1768</name> +<Properties property_0="" property_2="LPC175x_6x_512.cfx" property_3="NXP" property_4="LPC1768" property_count="5" version="70002"/> +<infoList vendor="NXP"><info chip="LPC1768" flash_driver="LPC175x_6x_512.cfx" match_id="0x00013f37,0x26013F37,0x26113F37" name="LPC1768" package="lpc17_lqfp100.xml" stub="crt_emu_cm3_nxp"><chip><name>LPC1768</name> <family>LPC17xx</family> <vendor>NXP (formerly Philips)</vendor> <reset board="None" core="Real" sys="Real"/> @@ -1434,6 +306,7 @@ <peripheralInstance derived_from="LPC17_CANCON" determined="infoFile" enable="SYSCTL.PCONP.PCCAN1&amp;0x1" id="CANCON1" location="0x40044000"/> <peripheralInstance derived_from="LPC17_CANCON" determined="infoFile" enable="SYSCTL.PCONP.PCCAN2&amp;0x1" id="CANCON2" location="0x40048000"/> <peripheralInstance derived_from="LPC17_MCPWM" determined="infoFile" enable="SYSCTL.PCONP.PCMCPWM&amp;0x1" id="MCPWM" location="0x400B8000"/> +<peripheralInstance derived_from="LPC17_FMC" determined="infoFile" id="FMC" location="0x40084000"/> </chip> <processor><name gcc_name="cortex-m3">Cortex-M3</name> <family>Cortex-M</family> @@ -1443,4 +316,571 @@ </infoList> </TargetConfig> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/projects/example/sample.ISA/.cproject b/projects/example/sample.ISA/.cproject index c834d2f..0a3d358 100644 --- a/projects/example/sample.ISA/.cproject +++ b/projects/example/sample.ISA/.cproject @@ -1,7 +1,5 @@ - - - + @@ -9,18 +7,18 @@ - - + + - + @@ -47,9 +45,9 @@ + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + - + @@ -741,12 +177,13 @@ @@ -1339,8 +211,8 @@ <?xml version="1.0" encoding="UTF-8"?> <TargetConfig> -<Properties property_0="" property_3="NXP" property_4="LPC1768" property_count="5" version="1"/> -<infoList vendor="NXP"><info chip="LPC1768" match_id="0x00013f37,0x26013F37,0x26113F37" name="LPC1768" package="lpc17_lqfp100.xml"><chip><name>LPC1768</name> +<Properties property_0="" property_2="LPC175x_6x_512.cfx" property_3="NXP" property_4="LPC1768" property_count="5" version="70002"/> +<infoList vendor="NXP"><info chip="LPC1768" flash_driver="LPC175x_6x_512.cfx" match_id="0x00013f37,0x26013F37,0x26113F37" name="LPC1768" package="lpc17_lqfp100.xml" stub="crt_emu_cm3_nxp"><chip><name>LPC1768</name> <family>LPC17xx</family> <vendor>NXP (formerly Philips)</vendor> <reset board="None" core="Real" sys="Real"/> @@ -1399,6 +271,8 @@ <peripheralInstance derived_from="LPC17_CANWAKESLEEP" determined="infoFile" id="CANWAKESLEEP" location="0x400FC110"/> <peripheralInstance derived_from="LPC17_CANCON" determined="infoFile" enable="SYSCTL.PCONP.PCCAN1&amp;0x1" id="CANCON1" location="0x40044000"/> <peripheralInstance derived_from="LPC17_CANCON" determined="infoFile" enable="SYSCTL.PCONP.PCCAN2&amp;0x1" id="CANCON2" location="0x40048000"/> +<peripheralInstance derived_from="LPC17_MCPWM" determined="infoFile" enable="SYSCTL.PCONP.PCMCPWM&amp;0x1" id="MCPWM" location="0x400B8000"/> +<peripheralInstance derived_from="LPC17_FMC" determined="infoFile" id="FMC" location="0x40084000"/> </chip> <processor><name gcc_name="cortex-m3">Cortex-M3</name> <family>Cortex-M</family> @@ -1408,4 +282,571 @@ </infoList> </TargetConfig> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/projects/example/sample.ISA/.project b/projects/example/sample.ISA/.project index ff6a7b9..934403f 100644 --- a/projects/example/sample.ISA/.project +++ b/projects/example/sample.ISA/.project @@ -3,8 +3,8 @@ sample.IAP - NyLPCLib FreeRTOSLib + NyLPCLib diff --git a/projects/example/sample.ISA/src/sketch.c b/projects/example/sample.ISA/src/sketch.c index 8e8bfe7..b5fffc6 100644 --- a/projects/example/sample.ISA/src/sketch.c +++ b/projects/example/sample.ISA/src/sketch.c @@ -4,20 +4,17 @@ * 取り扱いには十分な注意が必要です。 */ #include "boot/sketch.h" -#include "NyLPC_flash.h" +#include "../driver/flash/LPCxxx/NyLPC_cOnchipFlashWriter.h" #include "stdio.h" unsigned long buf[128]={0x1,0x2,0x03,0x04,0x05}; -NyLPC_TcOnchipFlashWriter_t writer; void setup(void) { - NyLPC_cOnchipFlashWriter_initialize(&writer); - NyLPC_cOnchipFlashWriter_elase(&writer,29,29); - NyLPC_cOnchipFlashWriter_writeSector(&writer,29,0,buf,5*4); - NyLPC_cOnchipFlashWriter_finalize(&writer); + NyLPC_cOnchipFlashWriter_elase(29,29); + NyLPC_cOnchipFlashWriter_writeSector(29,0,buf,5*4); return; } void loop(void) diff --git a/projects/example/sample.net.apipa/.cproject b/projects/example/sample.net.apipa/.cproject index 36c0f05..e7fb668 100644 --- a/projects/example/sample.net.apipa/.cproject +++ b/projects/example/sample.net.apipa/.cproject @@ -1,7 +1,5 @@ - - - + @@ -9,18 +7,18 @@ - - + + - + @@ -47,9 +45,9 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + - + @@ -522,7 +182,7 @@ @@ -889,8 +210,8 @@ <?xml version="1.0" encoding="UTF-8"?> <TargetConfig> -<Properties property_0="" property_3="NXP" property_4="LPC1768" property_count="5" version="1"/> -<infoList vendor="NXP"><info chip="LPC1768" match_id="0x00013f37,0x26013F37,0x26113F37" name="LPC1768" package="lpc17_lqfp100.xml"><chip><name>LPC1768</name> +<Properties property_0="" property_2="LPC175x_6x_512.cfx" property_3="NXP" property_4="LPC1768" property_count="5" version="70002"/> +<infoList vendor="NXP"><info chip="LPC1768" flash_driver="LPC175x_6x_512.cfx" match_id="0x00013f37,0x26013F37,0x26113F37" name="LPC1768" package="lpc17_lqfp100.xml" stub="crt_emu_cm3_nxp"><chip><name>LPC1768</name> <family>LPC17xx</family> <vendor>NXP (formerly Philips)</vendor> <reset board="None" core="Real" sys="Real"/> @@ -898,58 +219,59 @@ <memory can_program="true" id="Flash" is_ro="true" type="Flash"/> <memory id="RAM" type="RAM"/> <memory id="Periph" is_volatile="true" type="Peripheral"/> -<memoryInstance derived_from="Flash" id="MFlash512" location="0x0" size="0x80000"/> +<memoryInstance derived_from="Flash" id="MFlash512" location="0x00000000" size="0x80000"/> <memoryInstance derived_from="RAM" id="RamLoc32" location="0x10000000" size="0x8000"/> <memoryInstance derived_from="RAM" id="RamAHB32" location="0x2007c000" size="0x8000"/> <prog_flash blocksz="0x1000" location="0" maxprgbuff="0x1000" progwithcode="TRUE" size="0x10000"/> <prog_flash blocksz="0x8000" location="0x10000" maxprgbuff="0x1000" progwithcode="TRUE" size="0x70000"/> -<peripheralInstance derived_from="LPC17_NVIC" id="NVIC" location="0xE000E000"/> -<peripheralInstance derived_from="TIMER" enable="SYSCTL.PCONP.PCTIM0&amp;0x1" id="TIMER0" location="0x40004000"/> -<peripheralInstance derived_from="TIMER" enable="SYSCTL.PCONP.PCTIM1&amp;0x1" id="TIMER1" location="0x40008000"/> -<peripheralInstance derived_from="TIMER" enable="SYSCTL.PCONP.PCTIM2&amp;0x1" id="TIMER2" location="0x40090000"/> -<peripheralInstance derived_from="TIMER" enable="SYSCTL.PCONP.PCTIM3&amp;0x1" id="TIMER3" location="0x40094000"/> -<peripheralInstance derived_from="LPC17_RIT" enable="SYSCTL.PCONP.PCRIT&amp;0x1" id="RIT" location="0x400B0000"/> -<peripheralInstance derived_from="FGPIO" enable="SYSCTL.PCONP.PCGPIO&amp;0x1" id="GPIO0" location="0x2009C000"/> -<peripheralInstance derived_from="FGPIO" enable="SYSCTL.PCONP.PCGPIO&amp;0x1" id="GPIO1" location="0x2009C020"/> -<peripheralInstance derived_from="FGPIO" enable="SYSCTL.PCONP.PCGPIO&amp;0x1" id="GPIO2" location="0x2009C040"/> -<peripheralInstance derived_from="FGPIO" enable="SYSCTL.PCONP.PCGPIO&amp;0x1" id="GPIO3" location="0x2009C060"/> -<peripheralInstance derived_from="FGPIO" enable="SYSCTL.PCONP.PCGPIO&amp;0x1" id="GPIO4" location="0x2009C080"/> -<peripheralInstance derived_from="LPC17_I2S" enable="SYSCTL.PCONP&amp;0x08000000" id="I2S" location="0x400A8000"/> -<peripheralInstance derived_from="LPC17_SYSCTL" id="SYSCTL" location="0x400FC000"/> -<peripheralInstance derived_from="LPC17_DAC" enable="PCB.PINSEL1.P0_26&amp;0x2=2" id="DAC" location="0x4008C000"/> -<peripheralInstance derived_from="LPC17xx_UART" enable="SYSCTL.PCONP.PCUART0&amp;0x1" id="UART0" location="0x4000C000"/> -<peripheralInstance derived_from="LPC17xx_UART_MODEM" enable="SYSCTL.PCONP.PCUART1&amp;0x1" id="UART1" location="0x40010000"/> -<peripheralInstance derived_from="LPC17xx_UART" enable="SYSCTL.PCONP.PCUART2&amp;0x1" id="UART2" location="0x40098000"/> -<peripheralInstance derived_from="LPC17xx_UART" enable="SYSCTL.PCONP.PCUART3&amp;0x1" id="UART3" location="0x4009C000"/> -<peripheralInstance derived_from="SPI" enable="SYSCTL.PCONP.PCSPI&amp;0x1" id="SPI" location="0x40020000"/> -<peripheralInstance derived_from="LPC17_SSP" enable="SYSCTL.PCONP.PCSSP0&amp;0x1" id="SSP0" location="0x40088000"/> -<peripheralInstance derived_from="LPC17_SSP" enable="SYSCTL.PCONP.PCSSP1&amp;0x1" id="SSP1" location="0x40030000"/> -<peripheralInstance derived_from="LPC17_ADC" enable="SYSCTL.PCONP.PCAD&amp;0x1" id="ADC" location="0x40034000"/> -<peripheralInstance derived_from="LPC17_USBINTST" enable="USBCLKCTL.USBClkCtrl&amp;0x12" id="USBINTSTAT" location="0x400fc1c0"/> -<peripheralInstance derived_from="LPC17_USB_CLK_CTL" id="USBCLKCTL" location="0x5000cff4"/> -<peripheralInstance derived_from="LPC17_USBDEV" enable="USBCLKCTL.USBClkSt&amp;0x12=0x12" id="USBDEV" location="0x5000C200"/> -<peripheralInstance derived_from="LPC17_PWM" enable="SYSCTL.PCONP.PWM1&amp;0x1" id="PWM" location="0x40018000"/> -<peripheralInstance derived_from="LPC17_I2C" enable="SYSCTL.PCONP.PCI2C0&amp;0x1" id="I2C0" location="0x4001C000"/> -<peripheralInstance derived_from="LPC17_I2C" enable="SYSCTL.PCONP.PCI2C1&amp;0x1" id="I2C1" location="0x4005C000"/> -<peripheralInstance derived_from="LPC17_I2C" enable="SYSCTL.PCONP.PCI2C2&amp;0x1" id="I2C2" location="0x400A0000"/> -<peripheralInstance derived_from="LPC17_DMA" enable="SYSCTL.PCONP.PCGPDMA&amp;0x1" id="DMA" location="0x50004000"/> -<peripheralInstance derived_from="LPC17_ENET" enable="SYSCTL.PCONP.PCENET&amp;0x1" id="ENET" location="0x50000000"/> -<peripheralInstance derived_from="CM3_DCR" id="DCR" location="0xE000EDF0"/> -<peripheralInstance derived_from="LPC17_PCB" id="PCB" location="0x4002c000"/> -<peripheralInstance derived_from="LPC17_QEI" enable="SYSCTL.PCONP.PCQEI&amp;0x1" id="QEI" location="0x400bc000"/> -<peripheralInstance derived_from="LPC17_USBHOST" enable="USBCLKCTL.USBClkSt&amp;0x11=0x11" id="USBHOST" location="0x5000C000"/> -<peripheralInstance derived_from="LPC17_USBOTG" enable="USBCLKCTL.USBClkSt&amp;0x1c=0x1c" id="USBOTG" location="0x5000C000"/> -<peripheralInstance derived_from="LPC17_RTC" enable="SYSCTL.PCONP.PCRTC&amp;0x1" id="RTC" location="0x40024000"/> -<peripheralInstance derived_from="MPU" id="MPU" location="0xE000ED90"/> -<peripheralInstance derived_from="LPC1x_WDT" id="WDT" location="0x40000000"/> -<peripheralInstance derived_from="LPC17_FLASHCFG" id="FLASHACCEL" location="0x400FC000"/> -<peripheralInstance derived_from="GPIO_INT" id="GPIOINTMAP" location="0x40028080"/> -<peripheralInstance derived_from="LPC17_CANAFR" enable="SYSCTL.PCONP.PCCAN1&amp;0x1|SYSCTL.PCONP.PCCAN2&amp;0x1" id="CANAFR" location="0x4003C000"/> -<peripheralInstance derived_from="LPC17_CANCEN" enable="SYSCTL.PCONP.PCCAN1&amp;0x1|SYSCTL.PCONP.PCCAN2&amp;0x1" id="CANCEN" location="0x40040000"/> -<peripheralInstance derived_from="LPC17_CANWAKESLEEP" id="CANWAKESLEEP" location="0x400FC110"/> -<peripheralInstance derived_from="LPC17_CANCON" enable="SYSCTL.PCONP.PCCAN1&amp;0x1" id="CANCON1" location="0x40044000"/> -<peripheralInstance derived_from="LPC17_CANCON" enable="SYSCTL.PCONP.PCCAN2&amp;0x1" id="CANCON2" location="0x40048000"/> -<peripheralInstance derived_from="LPC17_MCPWM" enable="SYSCTL.PCONP.PCMCPWM&amp;0x1" id="MCPWM" location="0x400B8000"/> +<peripheralInstance derived_from="LPC17_NVIC" determined="infoFile" id="NVIC" location="0xE000E000"/> +<peripheralInstance derived_from="TIMER" determined="infoFile" enable="SYSCTL.PCONP.PCTIM0&amp;0x1" id="TIMER0" location="0x40004000"/> +<peripheralInstance derived_from="TIMER" determined="infoFile" enable="SYSCTL.PCONP.PCTIM1&amp;0x1" id="TIMER1" location="0x40008000"/> +<peripheralInstance derived_from="TIMER" determined="infoFile" enable="SYSCTL.PCONP.PCTIM2&amp;0x1" id="TIMER2" location="0x40090000"/> +<peripheralInstance derived_from="TIMER" determined="infoFile" enable="SYSCTL.PCONP.PCTIM3&amp;0x1" id="TIMER3" location="0x40094000"/> +<peripheralInstance derived_from="LPC17_RIT" determined="infoFile" enable="SYSCTL.PCONP.PCRIT&amp;0x1" id="RIT" location="0x400B0000"/> +<peripheralInstance derived_from="FGPIO" determined="infoFile" enable="SYSCTL.PCONP.PCGPIO&amp;0x1" id="GPIO0" location="0x2009C000"/> +<peripheralInstance derived_from="FGPIO" determined="infoFile" enable="SYSCTL.PCONP.PCGPIO&amp;0x1" id="GPIO1" location="0x2009C020"/> +<peripheralInstance derived_from="FGPIO" determined="infoFile" enable="SYSCTL.PCONP.PCGPIO&amp;0x1" id="GPIO2" location="0x2009C040"/> +<peripheralInstance derived_from="FGPIO" determined="infoFile" enable="SYSCTL.PCONP.PCGPIO&amp;0x1" id="GPIO3" location="0x2009C060"/> +<peripheralInstance derived_from="FGPIO" determined="infoFile" enable="SYSCTL.PCONP.PCGPIO&amp;0x1" id="GPIO4" location="0x2009C080"/> +<peripheralInstance derived_from="LPC17_I2S" determined="infoFile" enable="SYSCTL.PCONP&amp;0x08000000" id="I2S" location="0x400A8000"/> +<peripheralInstance derived_from="LPC17_SYSCTL" determined="infoFile" id="SYSCTL" location="0x400FC000"/> +<peripheralInstance derived_from="LPC17_DAC" determined="infoFile" enable="PCB.PINSEL1.P0_26&amp;0x2=2" id="DAC" location="0x4008C000"/> +<peripheralInstance derived_from="LPC17xx_UART" determined="infoFile" enable="SYSCTL.PCONP.PCUART0&amp;0x1" id="UART0" location="0x4000C000"/> +<peripheralInstance derived_from="LPC17xx_UART_MODEM" determined="infoFile" enable="SYSCTL.PCONP.PCUART1&amp;0x1" id="UART1" location="0x40010000"/> +<peripheralInstance derived_from="LPC17xx_UART" determined="infoFile" enable="SYSCTL.PCONP.PCUART2&amp;0x1" id="UART2" location="0x40098000"/> +<peripheralInstance derived_from="LPC17xx_UART" determined="infoFile" enable="SYSCTL.PCONP.PCUART3&amp;0x1" id="UART3" location="0x4009C000"/> +<peripheralInstance derived_from="SPI" determined="infoFile" enable="SYSCTL.PCONP.PCSPI&amp;0x1" id="SPI" location="0x40020000"/> +<peripheralInstance derived_from="LPC17_SSP" determined="infoFile" enable="SYSCTL.PCONP.PCSSP0&amp;0x1" id="SSP0" location="0x40088000"/> +<peripheralInstance derived_from="LPC17_SSP" determined="infoFile" enable="SYSCTL.PCONP.PCSSP1&amp;0x1" id="SSP1" location="0x40030000"/> +<peripheralInstance derived_from="LPC17_ADC" determined="infoFile" enable="SYSCTL.PCONP.PCAD&amp;0x1" id="ADC" location="0x40034000"/> +<peripheralInstance derived_from="LPC17_USBINTST" determined="infoFile" enable="USBCLKCTL.USBClkCtrl&amp;0x12" id="USBINTSTAT" location="0x400fc1c0"/> +<peripheralInstance derived_from="LPC17_USB_CLK_CTL" determined="infoFile" id="USBCLKCTL" location="0x5000cff4"/> +<peripheralInstance derived_from="LPC17_USBDEV" determined="infoFile" enable="USBCLKCTL.USBClkSt&amp;0x12=0x12" id="USBDEV" location="0x5000C200"/> +<peripheralInstance derived_from="LPC17_PWM" determined="infoFile" enable="SYSCTL.PCONP.PWM1&amp;0x1" id="PWM" location="0x40018000"/> +<peripheralInstance derived_from="LPC17_I2C" determined="infoFile" enable="SYSCTL.PCONP.PCI2C0&amp;0x1" id="I2C0" location="0x4001C000"/> +<peripheralInstance derived_from="LPC17_I2C" determined="infoFile" enable="SYSCTL.PCONP.PCI2C1&amp;0x1" id="I2C1" location="0x4005C000"/> +<peripheralInstance derived_from="LPC17_I2C" determined="infoFile" enable="SYSCTL.PCONP.PCI2C2&amp;0x1" id="I2C2" location="0x400A0000"/> +<peripheralInstance derived_from="LPC17_DMA" determined="infoFile" enable="SYSCTL.PCONP.PCGPDMA&amp;0x1" id="DMA" location="0x50004000"/> +<peripheralInstance derived_from="LPC17_ENET" determined="infoFile" enable="SYSCTL.PCONP.PCENET&amp;0x1" id="ENET" location="0x50000000"/> +<peripheralInstance derived_from="CM3_DCR" determined="infoFile" id="DCR" location="0xE000EDF0"/> +<peripheralInstance derived_from="LPC17_PCB" determined="infoFile" id="PCB" location="0x4002c000"/> +<peripheralInstance derived_from="LPC17_QEI" determined="infoFile" enable="SYSCTL.PCONP.PCQEI&amp;0x1" id="QEI" location="0x400bc000"/> +<peripheralInstance derived_from="LPC17_USBHOST" determined="infoFile" enable="USBCLKCTL.USBClkSt&amp;0x11=0x11" id="USBHOST" location="0x5000C000"/> +<peripheralInstance derived_from="LPC17_USBOTG" determined="infoFile" enable="USBCLKCTL.USBClkSt&amp;0x1c=0x1c" id="USBOTG" location="0x5000C000"/> +<peripheralInstance derived_from="LPC17_RTC" determined="infoFile" enable="SYSCTL.PCONP.PCRTC&amp;0x1" id="RTC" location="0x40024000"/> +<peripheralInstance derived_from="MPU" determined="infoFile" id="MPU" location="0xE000ED90"/> +<peripheralInstance derived_from="LPC1x_WDT" determined="infoFile" id="WDT" location="0x40000000"/> +<peripheralInstance derived_from="LPC17_FLASHCFG" determined="infoFile" id="FLASHACCEL" location="0x400FC000"/> +<peripheralInstance derived_from="GPIO_INT" determined="infoFile" id="GPIOINTMAP" location="0x40028080"/> +<peripheralInstance derived_from="LPC17_CANAFR" determined="infoFile" enable="SYSCTL.PCONP.PCCAN1&amp;0x1|SYSCTL.PCONP.PCCAN2&amp;0x1" id="CANAFR" location="0x4003C000"/> +<peripheralInstance derived_from="LPC17_CANCEN" determined="infoFile" enable="SYSCTL.PCONP.PCCAN1&amp;0x1|SYSCTL.PCONP.PCCAN2&amp;0x1" id="CANCEN" location="0x40040000"/> +<peripheralInstance derived_from="LPC17_CANWAKESLEEP" determined="infoFile" id="CANWAKESLEEP" location="0x400FC110"/> +<peripheralInstance derived_from="LPC17_CANCON" determined="infoFile" enable="SYSCTL.PCONP.PCCAN1&amp;0x1" id="CANCON1" location="0x40044000"/> +<peripheralInstance derived_from="LPC17_CANCON" determined="infoFile" enable="SYSCTL.PCONP.PCCAN2&amp;0x1" id="CANCON2" location="0x40048000"/> +<peripheralInstance derived_from="LPC17_MCPWM" determined="infoFile" enable="SYSCTL.PCONP.PCMCPWM&amp;0x1" id="MCPWM" location="0x400B8000"/> +<peripheralInstance derived_from="LPC17_FMC" determined="infoFile" id="FMC" location="0x40084000"/> </chip> <processor><name gcc_name="cortex-m3">Cortex-M3</name> <family>Cortex-M</family> diff --git a/projects/example/sample.net.apipa/src/sketch.c b/projects/example/sample.net.apipa/src/sketch.c index a823afc..dd8d05b 100644 --- a/projects/example/sample.net.apipa/src/sketch.c +++ b/projects/example/sample.net.apipa/src/sketch.c @@ -6,16 +6,20 @@ #include "boot/sketch.h" #include "NyLPC_net.h" +#include "NyLPC_netif.h" #include "NyLPC_utils.h" +#include "../netif/mimicip/NyLPC_cMiMicIpNetIf.h" +#include "../netif/apipa/NyLPC_cApipa.h" #include #include -NyLPC_TcNet_t net; +const struct NyLPC_TiNetInterface_Interface* netif; void setup(void) { //uipサービス初期化。いろいろ利用可能に。 - NyLPC_cNet_initialize(&net); + netif=NyLPC_cMiMicIpNetIf_getNetInterface(); + NyLPC_cNet_initialize(netif); } @@ -29,7 +33,7 @@ void loop(void) //DHCP NyLPC_cApipa_initialize(&apipa); NyLPC_cApipa_requestAddr(&apipa,&(c2.super),3); - NyLPC_cNet_start(&net,&c2); + NyLPC_cNet_start(&(c2.super)); //受信待ち for(;;){ } diff --git a/projects/example/sample.net.dhcp/.cproject b/projects/example/sample.net.dhcp/.cproject index 36c0f05..402d331 100644 --- a/projects/example/sample.net.dhcp/.cproject +++ b/projects/example/sample.net.dhcp/.cproject @@ -1,7 +1,5 @@ - - - + @@ -9,18 +7,18 @@ - - + + - + @@ -47,9 +45,9 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + - + @@ -522,7 +182,7 @@ @@ -889,8 +210,8 @@ <?xml version="1.0" encoding="UTF-8"?> <TargetConfig> -<Properties property_0="" property_3="NXP" property_4="LPC1768" property_count="5" version="1"/> -<infoList vendor="NXP"><info chip="LPC1768" match_id="0x00013f37,0x26013F37,0x26113F37" name="LPC1768" package="lpc17_lqfp100.xml"><chip><name>LPC1768</name> +<Properties property_0="" property_2="LPC175x_6x_512.cfx" property_3="NXP" property_4="LPC1768" property_count="5" version="70002"/> +<infoList vendor="NXP"><info chip="LPC1768" flash_driver="LPC175x_6x_512.cfx" match_id="0x00013f37,0x26013F37,0x26113F37" name="LPC1768" package="lpc17_lqfp100.xml" stub="crt_emu_cm3_nxp"><chip><name>LPC1768</name> <family>LPC17xx</family> <vendor>NXP (formerly Philips)</vendor> <reset board="None" core="Real" sys="Real"/> @@ -898,58 +219,59 @@ <memory can_program="true" id="Flash" is_ro="true" type="Flash"/> <memory id="RAM" type="RAM"/> <memory id="Periph" is_volatile="true" type="Peripheral"/> -<memoryInstance derived_from="Flash" id="MFlash512" location="0x0" size="0x80000"/> +<memoryInstance derived_from="Flash" id="MFlash512" location="0x00000000" size="0x80000"/> <memoryInstance derived_from="RAM" id="RamLoc32" location="0x10000000" size="0x8000"/> <memoryInstance derived_from="RAM" id="RamAHB32" location="0x2007c000" size="0x8000"/> <prog_flash blocksz="0x1000" location="0" maxprgbuff="0x1000" progwithcode="TRUE" size="0x10000"/> <prog_flash blocksz="0x8000" location="0x10000" maxprgbuff="0x1000" progwithcode="TRUE" size="0x70000"/> -<peripheralInstance derived_from="LPC17_NVIC" id="NVIC" location="0xE000E000"/> -<peripheralInstance derived_from="TIMER" enable="SYSCTL.PCONP.PCTIM0&amp;0x1" id="TIMER0" location="0x40004000"/> -<peripheralInstance derived_from="TIMER" enable="SYSCTL.PCONP.PCTIM1&amp;0x1" id="TIMER1" location="0x40008000"/> -<peripheralInstance derived_from="TIMER" enable="SYSCTL.PCONP.PCTIM2&amp;0x1" id="TIMER2" location="0x40090000"/> -<peripheralInstance derived_from="TIMER" enable="SYSCTL.PCONP.PCTIM3&amp;0x1" id="TIMER3" location="0x40094000"/> -<peripheralInstance derived_from="LPC17_RIT" enable="SYSCTL.PCONP.PCRIT&amp;0x1" id="RIT" location="0x400B0000"/> -<peripheralInstance derived_from="FGPIO" enable="SYSCTL.PCONP.PCGPIO&amp;0x1" id="GPIO0" location="0x2009C000"/> -<peripheralInstance derived_from="FGPIO" enable="SYSCTL.PCONP.PCGPIO&amp;0x1" id="GPIO1" location="0x2009C020"/> -<peripheralInstance derived_from="FGPIO" enable="SYSCTL.PCONP.PCGPIO&amp;0x1" id="GPIO2" location="0x2009C040"/> -<peripheralInstance derived_from="FGPIO" enable="SYSCTL.PCONP.PCGPIO&amp;0x1" id="GPIO3" location="0x2009C060"/> -<peripheralInstance derived_from="FGPIO" enable="SYSCTL.PCONP.PCGPIO&amp;0x1" id="GPIO4" location="0x2009C080"/> -<peripheralInstance derived_from="LPC17_I2S" enable="SYSCTL.PCONP&amp;0x08000000" id="I2S" location="0x400A8000"/> -<peripheralInstance derived_from="LPC17_SYSCTL" id="SYSCTL" location="0x400FC000"/> -<peripheralInstance derived_from="LPC17_DAC" enable="PCB.PINSEL1.P0_26&amp;0x2=2" id="DAC" location="0x4008C000"/> -<peripheralInstance derived_from="LPC17xx_UART" enable="SYSCTL.PCONP.PCUART0&amp;0x1" id="UART0" location="0x4000C000"/> -<peripheralInstance derived_from="LPC17xx_UART_MODEM" enable="SYSCTL.PCONP.PCUART1&amp;0x1" id="UART1" location="0x40010000"/> -<peripheralInstance derived_from="LPC17xx_UART" enable="SYSCTL.PCONP.PCUART2&amp;0x1" id="UART2" location="0x40098000"/> -<peripheralInstance derived_from="LPC17xx_UART" enable="SYSCTL.PCONP.PCUART3&amp;0x1" id="UART3" location="0x4009C000"/> -<peripheralInstance derived_from="SPI" enable="SYSCTL.PCONP.PCSPI&amp;0x1" id="SPI" location="0x40020000"/> -<peripheralInstance derived_from="LPC17_SSP" enable="SYSCTL.PCONP.PCSSP0&amp;0x1" id="SSP0" location="0x40088000"/> -<peripheralInstance derived_from="LPC17_SSP" enable="SYSCTL.PCONP.PCSSP1&amp;0x1" id="SSP1" location="0x40030000"/> -<peripheralInstance derived_from="LPC17_ADC" enable="SYSCTL.PCONP.PCAD&amp;0x1" id="ADC" location="0x40034000"/> -<peripheralInstance derived_from="LPC17_USBINTST" enable="USBCLKCTL.USBClkCtrl&amp;0x12" id="USBINTSTAT" location="0x400fc1c0"/> -<peripheralInstance derived_from="LPC17_USB_CLK_CTL" id="USBCLKCTL" location="0x5000cff4"/> -<peripheralInstance derived_from="LPC17_USBDEV" enable="USBCLKCTL.USBClkSt&amp;0x12=0x12" id="USBDEV" location="0x5000C200"/> -<peripheralInstance derived_from="LPC17_PWM" enable="SYSCTL.PCONP.PWM1&amp;0x1" id="PWM" location="0x40018000"/> -<peripheralInstance derived_from="LPC17_I2C" enable="SYSCTL.PCONP.PCI2C0&amp;0x1" id="I2C0" location="0x4001C000"/> -<peripheralInstance derived_from="LPC17_I2C" enable="SYSCTL.PCONP.PCI2C1&amp;0x1" id="I2C1" location="0x4005C000"/> -<peripheralInstance derived_from="LPC17_I2C" enable="SYSCTL.PCONP.PCI2C2&amp;0x1" id="I2C2" location="0x400A0000"/> -<peripheralInstance derived_from="LPC17_DMA" enable="SYSCTL.PCONP.PCGPDMA&amp;0x1" id="DMA" location="0x50004000"/> -<peripheralInstance derived_from="LPC17_ENET" enable="SYSCTL.PCONP.PCENET&amp;0x1" id="ENET" location="0x50000000"/> -<peripheralInstance derived_from="CM3_DCR" id="DCR" location="0xE000EDF0"/> -<peripheralInstance derived_from="LPC17_PCB" id="PCB" location="0x4002c000"/> -<peripheralInstance derived_from="LPC17_QEI" enable="SYSCTL.PCONP.PCQEI&amp;0x1" id="QEI" location="0x400bc000"/> -<peripheralInstance derived_from="LPC17_USBHOST" enable="USBCLKCTL.USBClkSt&amp;0x11=0x11" id="USBHOST" location="0x5000C000"/> -<peripheralInstance derived_from="LPC17_USBOTG" enable="USBCLKCTL.USBClkSt&amp;0x1c=0x1c" id="USBOTG" location="0x5000C000"/> -<peripheralInstance derived_from="LPC17_RTC" enable="SYSCTL.PCONP.PCRTC&amp;0x1" id="RTC" location="0x40024000"/> -<peripheralInstance derived_from="MPU" id="MPU" location="0xE000ED90"/> -<peripheralInstance derived_from="LPC1x_WDT" id="WDT" location="0x40000000"/> -<peripheralInstance derived_from="LPC17_FLASHCFG" id="FLASHACCEL" location="0x400FC000"/> -<peripheralInstance derived_from="GPIO_INT" id="GPIOINTMAP" location="0x40028080"/> -<peripheralInstance derived_from="LPC17_CANAFR" enable="SYSCTL.PCONP.PCCAN1&amp;0x1|SYSCTL.PCONP.PCCAN2&amp;0x1" id="CANAFR" location="0x4003C000"/> -<peripheralInstance derived_from="LPC17_CANCEN" enable="SYSCTL.PCONP.PCCAN1&amp;0x1|SYSCTL.PCONP.PCCAN2&amp;0x1" id="CANCEN" location="0x40040000"/> -<peripheralInstance derived_from="LPC17_CANWAKESLEEP" id="CANWAKESLEEP" location="0x400FC110"/> -<peripheralInstance derived_from="LPC17_CANCON" enable="SYSCTL.PCONP.PCCAN1&amp;0x1" id="CANCON1" location="0x40044000"/> -<peripheralInstance derived_from="LPC17_CANCON" enable="SYSCTL.PCONP.PCCAN2&amp;0x1" id="CANCON2" location="0x40048000"/> -<peripheralInstance derived_from="LPC17_MCPWM" enable="SYSCTL.PCONP.PCMCPWM&amp;0x1" id="MCPWM" location="0x400B8000"/> +<peripheralInstance derived_from="LPC17_NVIC" determined="infoFile" id="NVIC" location="0xE000E000"/> +<peripheralInstance derived_from="TIMER" determined="infoFile" enable="SYSCTL.PCONP.PCTIM0&amp;0x1" id="TIMER0" location="0x40004000"/> +<peripheralInstance derived_from="TIMER" determined="infoFile" enable="SYSCTL.PCONP.PCTIM1&amp;0x1" id="TIMER1" location="0x40008000"/> +<peripheralInstance derived_from="TIMER" determined="infoFile" enable="SYSCTL.PCONP.PCTIM2&amp;0x1" id="TIMER2" location="0x40090000"/> +<peripheralInstance derived_from="TIMER" determined="infoFile" enable="SYSCTL.PCONP.PCTIM3&amp;0x1" id="TIMER3" location="0x40094000"/> +<peripheralInstance derived_from="LPC17_RIT" determined="infoFile" enable="SYSCTL.PCONP.PCRIT&amp;0x1" id="RIT" location="0x400B0000"/> +<peripheralInstance derived_from="FGPIO" determined="infoFile" enable="SYSCTL.PCONP.PCGPIO&amp;0x1" id="GPIO0" location="0x2009C000"/> +<peripheralInstance derived_from="FGPIO" determined="infoFile" enable="SYSCTL.PCONP.PCGPIO&amp;0x1" id="GPIO1" location="0x2009C020"/> +<peripheralInstance derived_from="FGPIO" determined="infoFile" enable="SYSCTL.PCONP.PCGPIO&amp;0x1" id="GPIO2" location="0x2009C040"/> +<peripheralInstance derived_from="FGPIO" determined="infoFile" enable="SYSCTL.PCONP.PCGPIO&amp;0x1" id="GPIO3" location="0x2009C060"/> +<peripheralInstance derived_from="FGPIO" determined="infoFile" enable="SYSCTL.PCONP.PCGPIO&amp;0x1" id="GPIO4" location="0x2009C080"/> +<peripheralInstance derived_from="LPC17_I2S" determined="infoFile" enable="SYSCTL.PCONP&amp;0x08000000" id="I2S" location="0x400A8000"/> +<peripheralInstance derived_from="LPC17_SYSCTL" determined="infoFile" id="SYSCTL" location="0x400FC000"/> +<peripheralInstance derived_from="LPC17_DAC" determined="infoFile" enable="PCB.PINSEL1.P0_26&amp;0x2=2" id="DAC" location="0x4008C000"/> +<peripheralInstance derived_from="LPC17xx_UART" determined="infoFile" enable="SYSCTL.PCONP.PCUART0&amp;0x1" id="UART0" location="0x4000C000"/> +<peripheralInstance derived_from="LPC17xx_UART_MODEM" determined="infoFile" enable="SYSCTL.PCONP.PCUART1&amp;0x1" id="UART1" location="0x40010000"/> +<peripheralInstance derived_from="LPC17xx_UART" determined="infoFile" enable="SYSCTL.PCONP.PCUART2&amp;0x1" id="UART2" location="0x40098000"/> +<peripheralInstance derived_from="LPC17xx_UART" determined="infoFile" enable="SYSCTL.PCONP.PCUART3&amp;0x1" id="UART3" location="0x4009C000"/> +<peripheralInstance derived_from="SPI" determined="infoFile" enable="SYSCTL.PCONP.PCSPI&amp;0x1" id="SPI" location="0x40020000"/> +<peripheralInstance derived_from="LPC17_SSP" determined="infoFile" enable="SYSCTL.PCONP.PCSSP0&amp;0x1" id="SSP0" location="0x40088000"/> +<peripheralInstance derived_from="LPC17_SSP" determined="infoFile" enable="SYSCTL.PCONP.PCSSP1&amp;0x1" id="SSP1" location="0x40030000"/> +<peripheralInstance derived_from="LPC17_ADC" determined="infoFile" enable="SYSCTL.PCONP.PCAD&amp;0x1" id="ADC" location="0x40034000"/> +<peripheralInstance derived_from="LPC17_USBINTST" determined="infoFile" enable="USBCLKCTL.USBClkCtrl&amp;0x12" id="USBINTSTAT" location="0x400fc1c0"/> +<peripheralInstance derived_from="LPC17_USB_CLK_CTL" determined="infoFile" id="USBCLKCTL" location="0x5000cff4"/> +<peripheralInstance derived_from="LPC17_USBDEV" determined="infoFile" enable="USBCLKCTL.USBClkSt&amp;0x12=0x12" id="USBDEV" location="0x5000C200"/> +<peripheralInstance derived_from="LPC17_PWM" determined="infoFile" enable="SYSCTL.PCONP.PWM1&amp;0x1" id="PWM" location="0x40018000"/> +<peripheralInstance derived_from="LPC17_I2C" determined="infoFile" enable="SYSCTL.PCONP.PCI2C0&amp;0x1" id="I2C0" location="0x4001C000"/> +<peripheralInstance derived_from="LPC17_I2C" determined="infoFile" enable="SYSCTL.PCONP.PCI2C1&amp;0x1" id="I2C1" location="0x4005C000"/> +<peripheralInstance derived_from="LPC17_I2C" determined="infoFile" enable="SYSCTL.PCONP.PCI2C2&amp;0x1" id="I2C2" location="0x400A0000"/> +<peripheralInstance derived_from="LPC17_DMA" determined="infoFile" enable="SYSCTL.PCONP.PCGPDMA&amp;0x1" id="DMA" location="0x50004000"/> +<peripheralInstance derived_from="LPC17_ENET" determined="infoFile" enable="SYSCTL.PCONP.PCENET&amp;0x1" id="ENET" location="0x50000000"/> +<peripheralInstance derived_from="CM3_DCR" determined="infoFile" id="DCR" location="0xE000EDF0"/> +<peripheralInstance derived_from="LPC17_PCB" determined="infoFile" id="PCB" location="0x4002c000"/> +<peripheralInstance derived_from="LPC17_QEI" determined="infoFile" enable="SYSCTL.PCONP.PCQEI&amp;0x1" id="QEI" location="0x400bc000"/> +<peripheralInstance derived_from="LPC17_USBHOST" determined="infoFile" enable="USBCLKCTL.USBClkSt&amp;0x11=0x11" id="USBHOST" location="0x5000C000"/> +<peripheralInstance derived_from="LPC17_USBOTG" determined="infoFile" enable="USBCLKCTL.USBClkSt&amp;0x1c=0x1c" id="USBOTG" location="0x5000C000"/> +<peripheralInstance derived_from="LPC17_RTC" determined="infoFile" enable="SYSCTL.PCONP.PCRTC&amp;0x1" id="RTC" location="0x40024000"/> +<peripheralInstance derived_from="MPU" determined="infoFile" id="MPU" location="0xE000ED90"/> +<peripheralInstance derived_from="LPC1x_WDT" determined="infoFile" id="WDT" location="0x40000000"/> +<peripheralInstance derived_from="LPC17_FLASHCFG" determined="infoFile" id="FLASHACCEL" location="0x400FC000"/> +<peripheralInstance derived_from="GPIO_INT" determined="infoFile" id="GPIOINTMAP" location="0x40028080"/> +<peripheralInstance derived_from="LPC17_CANAFR" determined="infoFile" enable="SYSCTL.PCONP.PCCAN1&amp;0x1|SYSCTL.PCONP.PCCAN2&amp;0x1" id="CANAFR" location="0x4003C000"/> +<peripheralInstance derived_from="LPC17_CANCEN" determined="infoFile" enable="SYSCTL.PCONP.PCCAN1&amp;0x1|SYSCTL.PCONP.PCCAN2&amp;0x1" id="CANCEN" location="0x40040000"/> +<peripheralInstance derived_from="LPC17_CANWAKESLEEP" determined="infoFile" id="CANWAKESLEEP" location="0x400FC110"/> +<peripheralInstance derived_from="LPC17_CANCON" determined="infoFile" enable="SYSCTL.PCONP.PCCAN1&amp;0x1" id="CANCON1" location="0x40044000"/> +<peripheralInstance derived_from="LPC17_CANCON" determined="infoFile" enable="SYSCTL.PCONP.PCCAN2&amp;0x1" id="CANCON2" location="0x40048000"/> +<peripheralInstance derived_from="LPC17_MCPWM" determined="infoFile" enable="SYSCTL.PCONP.PCMCPWM&amp;0x1" id="MCPWM" location="0x400B8000"/> +<peripheralInstance derived_from="LPC17_FMC" determined="infoFile" id="FMC" location="0x40084000"/> </chip> <processor><name gcc_name="cortex-m3">Cortex-M3</name> <family>Cortex-M</family> diff --git a/projects/example/sample.net.dhcp/src/sketch.c b/projects/example/sample.net.dhcp/src/sketch.c index 95d9b37..8f152a1 100644 --- a/projects/example/sample.net.dhcp/src/sketch.c +++ b/projects/example/sample.net.dhcp/src/sketch.c @@ -6,15 +6,18 @@ #include "NyLPC_net.h" #include "NyLPC_utils.h" +#include "../netif/mimicip/NyLPC_cMiMicIpNetIf.h" +#include "../netif/dhcp/NyLPC_cDhcpClient.h" #include #include -NyLPC_TcNet_t net; +const struct NyLPC_TiNetInterface_Interface* netif; void setup(void) { //uipサービス初期化。いろいろ利用可能に。 - NyLPC_cNet_initialize(&net); + netif=NyLPC_cMiMicIpNetIf_getNetInterface(); + NyLPC_cNet_initialize(netif); } void loop(void) @@ -23,8 +26,8 @@ void loop(void) //デフォルト設定の呼び出し NyLPC_cNetConfig_initialize(&c2,NyLPC_TBool_TRUE); //DHCP - NyLPC_cNet_requestAddrDhcp(&net,&(c2.super),3); - NyLPC_cNet_start(&net,&c2); + NyLPC_cNet_requestAddrDhcp(&(c2.super),3); + NyLPC_cNet_start(&c2); //受信待ち for(;;){ } diff --git a/projects/example/sample.net.httpcl/.cproject b/projects/example/sample.net.httpcl/.cproject index eaa1f15..8e7b2ff 100644 --- a/projects/example/sample.net.httpcl/.cproject +++ b/projects/example/sample.net.httpcl/.cproject @@ -1,7 +1,5 @@ - - - + @@ -9,14 +7,14 @@ - - + + @@ -47,9 +45,9 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + @@ -519,7 +183,7 @@ @@ -886,8 +211,8 @@ <?xml version="1.0" encoding="UTF-8"?> <TargetConfig> -<Properties property_0="" property_3="NXP" property_4="LPC1769" property_count="5" version="1"/> -<infoList vendor="NXP"><info chip="LPC1769" match_id="0x26113F37" name="LPC1769" package="lpc17_lqfp100.xml"><chip><name>LPC1769</name> +<Properties property_0="" property_2="LPC175x_6x_512.cfx" property_3="NXP" property_4="LPC1769" property_count="5" version="70002"/> +<infoList vendor="NXP"><info chip="LPC1769" flash_driver="LPC175x_6x_512.cfx" match_id="0x26113F37" name="LPC1769" package="lpc17_lqfp100.xml" stub="crt_emu_cm3_nxp"><chip><name>LPC1769</name> <family>LPC17xx</family> <vendor>NXP (formerly Philips)</vendor> <reset board="None" core="Real" sys="Real"/> @@ -895,59 +220,59 @@ <memory can_program="true" id="Flash" is_ro="true" type="Flash"/> <memory id="RAM" type="RAM"/> <memory id="Periph" is_volatile="true" type="Peripheral"/> -<memoryInstance derived_from="Flash" id="MFlash512" location="0x0" size="0x80000"/> +<memoryInstance derived_from="Flash" id="MFlash512" location="0x00000000" size="0x80000"/> <memoryInstance derived_from="RAM" id="RamLoc32" location="0x10000000" size="0x8000"/> <memoryInstance derived_from="RAM" id="RamAHB32" location="0x2007c000" size="0x8000"/> <prog_flash blocksz="0x1000" location="0" maxprgbuff="0x1000" progwithcode="TRUE" size="0x10000"/> <prog_flash blocksz="0x8000" location="0x10000" maxprgbuff="0x1000" progwithcode="TRUE" size="0x70000"/> -<peripheralInstance derived_from="LPC17_NVIC" id="NVIC" location="0xE000E000"/> -<peripheralInstance derived_from="TIMER" enable="SYSCTL.PCONP.PCTIM0&amp;0x1" id="TIMER0" location="0x40004000"/> -<peripheralInstance derived_from="TIMER" enable="SYSCTL.PCONP.PCTIM1&amp;0x1" id="TIMER1" location="0x40008000"/> -<peripheralInstance derived_from="TIMER" enable="SYSCTL.PCONP.PCTIM2&amp;0x1" id="TIMER2" location="0x40090000"/> -<peripheralInstance derived_from="TIMER" enable="SYSCTL.PCONP.PCTIM3&amp;0x1" id="TIMER3" location="0x40094000"/> -<peripheralInstance derived_from="LPC17_RIT" enable="SYSCTL.PCONP.PCRIT&amp;0x1" id="RIT" location="0x400B0000"/> -<peripheralInstance derived_from="FGPIO" enable="SYSCTL.PCONP.PCGPIO&amp;0x1" id="GPIO0" location="0x2009C000"/> -<peripheralInstance derived_from="FGPIO" enable="SYSCTL.PCONP.PCGPIO&amp;0x1" id="GPIO1" location="0x2009C020"/> -<peripheralInstance derived_from="FGPIO" enable="SYSCTL.PCONP.PCGPIO&amp;0x1" id="GPIO2" location="0x2009C040"/> -<peripheralInstance derived_from="FGPIO" enable="SYSCTL.PCONP.PCGPIO&amp;0x1" id="GPIO3" location="0x2009C060"/> -<peripheralInstance derived_from="FGPIO" enable="SYSCTL.PCONP.PCGPIO&amp;0x1" id="GPIO4" location="0x2009C080"/> -<peripheralInstance derived_from="LPC17_I2S" enable="SYSCTL.PCONP&amp;0x08000000" id="I2S" location="0x400A8000"/> -<peripheralInstance derived_from="LPC17_SYSCTL" id="SYSCTL" location="0x400FC000"/> -<peripheralInstance derived_from="LPC17_DAC" enable="PCB.PINSEL1.P0_26&amp;0x2=2" id="DAC" location="0x4008C000"/> -<peripheralInstance derived_from="LPC17xx_UART" enable="SYSCTL.PCONP.PCUART0&amp;0x1" id="UART0" location="0x4000C000"/> -<peripheralInstance derived_from="LPC17xx_UART_MODEM" enable="SYSCTL.PCONP.PCUART1&amp;0x1" id="UART1" location="0x40010000"/> -<peripheralInstance derived_from="LPC17xx_UART" enable="SYSCTL.PCONP.PCUART2&amp;0x1" id="UART2" location="0x40098000"/> -<peripheralInstance derived_from="LPC17xx_UART" enable="SYSCTL.PCONP.PCUART3&amp;0x1" id="UART3" location="0x4009C000"/> -<peripheralInstance derived_from="SPI" enable="SYSCTL.PCONP.PCSPI&amp;0x1" id="SPI" location="0x40020000"/> -<peripheralInstance derived_from="LPC17_SSP" enable="SYSCTL.PCONP.PCSSP0&amp;0x1" id="SSP0" location="0x40088000"/> -<peripheralInstance derived_from="LPC17_SSP" enable="SYSCTL.PCONP.PCSSP1&amp;0x1" id="SSP1" location="0x40030000"/> -<peripheralInstance derived_from="LPC17_ADC" enable="SYSCTL.PCONP.PCAD&amp;0x1" id="ADC" location="0x40034000"/> -<peripheralInstance derived_from="LPC17_USBINTST" enable="USBCLKCTL.USBClkCtrl&amp;0x12" id="USBINTSTAT" location="0x400fc1c0"/> -<peripheralInstance derived_from="LPC17_USB_CLK_CTL" id="USBCLKCTL" location="0x5000cff4"/> -<peripheralInstance derived_from="LPC17_USBDEV" enable="USBCLKCTL.USBClkSt&amp;0x12=0x12" id="USBDEV" location="0x5000C200"/> -<peripheralInstance derived_from="LPC17_PWM" enable="SYSCTL.PCONP.PWM1&amp;0x1" id="PWM" location="0x40018000"/> -<peripheralInstance derived_from="LPC17_I2C" enable="SYSCTL.PCONP.PCI2C0&amp;0x1" id="I2C0" location="0x4001C000"/> -<peripheralInstance derived_from="LPC17_I2C" enable="SYSCTL.PCONP.PCI2C1&amp;0x1" id="I2C1" location="0x4005C000"/> -<peripheralInstance derived_from="LPC17_I2C" enable="SYSCTL.PCONP.PCI2C2&amp;0x1" id="I2C2" location="0x400A0000"/> -<peripheralInstance derived_from="LPC17_DMA" enable="SYSCTL.PCONP.PCGPDMA&amp;0x1" id="DMA" location="0x50004000"/> -<peripheralInstance derived_from="LPC17_ENET" enable="SYSCTL.PCONP.PCENET&amp;0x1" id="ENET" location="0x50000000"/> -<peripheralInstance derived_from="CM3_DCR" id="DCR" location="0xE000EDF0"/> -<peripheralInstance derived_from="LPC17_PCB" id="PCB" location="0x4002c000"/> -<peripheralInstance derived_from="LPC17_QEI" enable="SYSCTL.PCONP.PCQEI&amp;0x1" id="QEI" location="0x400bc000"/> -<peripheralInstance derived_from="LPC17_USBHOST" enable="USBCLKCTL.USBClkSt&amp;0x11=0x11" id="USBHOST" location="0x5000C000"/> -<peripheralInstance derived_from="LPC17_USBOTG" enable="USBCLKCTL.USBClkSt&amp;0x1c=0x1c" id="USBOTG" location="0x5000C000"/> -<peripheralInstance derived_from="LPC17_RTC" enable="SYSCTL.PCONP.PCRTC&amp;0x1" id="RTC" location="0x40024000"/> -<peripheralInstance derived_from="MPU" id="MPU" location="0xE000ED90"/> -<peripheralInstance derived_from="LPC1x_WDT" id="WDT" location="0x40000000"/> -<peripheralInstance derived_from="LPC17_FLASHCFG" id="FLASHACCEL" location="0x400FC000"/> -<peripheralInstance derived_from="GPIO_INT" id="GPIOINTMAP" location="0x40028080"/> -<peripheralInstance derived_from="LPC17_CANAFR" enable="SYSCTL.PCONP.PCCAN1&amp;0x1|SYSCTL.PCONP.PCCAN2&amp;0x1" id="CANAFR" location="0x4003C000"/> -<peripheralInstance derived_from="LPC17_CANCEN" enable="SYSCTL.PCONP.PCCAN1&amp;0x1|SYSCTL.PCONP.PCCAN2&amp;0x1" id="CANCEN" location="0x40040000"/> -<peripheralInstance derived_from="LPC17_CANWAKESLEEP" id="CANWAKESLEEP" location="0x400FC110"/> -<peripheralInstance derived_from="LPC17_CANCON" enable="SYSCTL.PCONP.PCCAN1&amp;0x1" id="CANCON1" location="0x40044000"/> -<peripheralInstance derived_from="LPC17_CANCON" enable="SYSCTL.PCONP.PCCAN2&amp;0x1" id="CANCON2" location="0x40048000"/> -<peripheralInstance derived_from="LPC17_MCPWM" enable="SYSCTL.PCONP.PCMCPWM&amp;0x1" id="MCPWM" location="0x400B8000"/> -<peripheralInstance derived_from="LPC17_FMC" id="FMC" location="0x40084000"/> +<peripheralInstance derived_from="LPC17_NVIC" determined="infoFile" id="NVIC" location="0xE000E000"/> +<peripheralInstance derived_from="TIMER" determined="infoFile" enable="SYSCTL.PCONP.PCTIM0&amp;0x1" id="TIMER0" location="0x40004000"/> +<peripheralInstance derived_from="TIMER" determined="infoFile" enable="SYSCTL.PCONP.PCTIM1&amp;0x1" id="TIMER1" location="0x40008000"/> +<peripheralInstance derived_from="TIMER" determined="infoFile" enable="SYSCTL.PCONP.PCTIM2&amp;0x1" id="TIMER2" location="0x40090000"/> +<peripheralInstance derived_from="TIMER" determined="infoFile" enable="SYSCTL.PCONP.PCTIM3&amp;0x1" id="TIMER3" location="0x40094000"/> +<peripheralInstance derived_from="LPC17_RIT" determined="infoFile" enable="SYSCTL.PCONP.PCRIT&amp;0x1" id="RIT" location="0x400B0000"/> +<peripheralInstance derived_from="FGPIO" determined="infoFile" enable="SYSCTL.PCONP.PCGPIO&amp;0x1" id="GPIO0" location="0x2009C000"/> +<peripheralInstance derived_from="FGPIO" determined="infoFile" enable="SYSCTL.PCONP.PCGPIO&amp;0x1" id="GPIO1" location="0x2009C020"/> +<peripheralInstance derived_from="FGPIO" determined="infoFile" enable="SYSCTL.PCONP.PCGPIO&amp;0x1" id="GPIO2" location="0x2009C040"/> +<peripheralInstance derived_from="FGPIO" determined="infoFile" enable="SYSCTL.PCONP.PCGPIO&amp;0x1" id="GPIO3" location="0x2009C060"/> +<peripheralInstance derived_from="FGPIO" determined="infoFile" enable="SYSCTL.PCONP.PCGPIO&amp;0x1" id="GPIO4" location="0x2009C080"/> +<peripheralInstance derived_from="LPC17_I2S" determined="infoFile" enable="SYSCTL.PCONP&amp;0x08000000" id="I2S" location="0x400A8000"/> +<peripheralInstance derived_from="LPC17_SYSCTL" determined="infoFile" id="SYSCTL" location="0x400FC000"/> +<peripheralInstance derived_from="LPC17_DAC" determined="infoFile" enable="PCB.PINSEL1.P0_26&amp;0x2=2" id="DAC" location="0x4008C000"/> +<peripheralInstance derived_from="LPC17xx_UART" determined="infoFile" enable="SYSCTL.PCONP.PCUART0&amp;0x1" id="UART0" location="0x4000C000"/> +<peripheralInstance derived_from="LPC17xx_UART_MODEM" determined="infoFile" enable="SYSCTL.PCONP.PCUART1&amp;0x1" id="UART1" location="0x40010000"/> +<peripheralInstance derived_from="LPC17xx_UART" determined="infoFile" enable="SYSCTL.PCONP.PCUART2&amp;0x1" id="UART2" location="0x40098000"/> +<peripheralInstance derived_from="LPC17xx_UART" determined="infoFile" enable="SYSCTL.PCONP.PCUART3&amp;0x1" id="UART3" location="0x4009C000"/> +<peripheralInstance derived_from="SPI" determined="infoFile" enable="SYSCTL.PCONP.PCSPI&amp;0x1" id="SPI" location="0x40020000"/> +<peripheralInstance derived_from="LPC17_SSP" determined="infoFile" enable="SYSCTL.PCONP.PCSSP0&amp;0x1" id="SSP0" location="0x40088000"/> +<peripheralInstance derived_from="LPC17_SSP" determined="infoFile" enable="SYSCTL.PCONP.PCSSP1&amp;0x1" id="SSP1" location="0x40030000"/> +<peripheralInstance derived_from="LPC17_ADC" determined="infoFile" enable="SYSCTL.PCONP.PCAD&amp;0x1" id="ADC" location="0x40034000"/> +<peripheralInstance derived_from="LPC17_USBINTST" determined="infoFile" enable="USBCLKCTL.USBClkCtrl&amp;0x12" id="USBINTSTAT" location="0x400fc1c0"/> +<peripheralInstance derived_from="LPC17_USB_CLK_CTL" determined="infoFile" id="USBCLKCTL" location="0x5000cff4"/> +<peripheralInstance derived_from="LPC17_USBDEV" determined="infoFile" enable="USBCLKCTL.USBClkSt&amp;0x12=0x12" id="USBDEV" location="0x5000C200"/> +<peripheralInstance derived_from="LPC17_PWM" determined="infoFile" enable="SYSCTL.PCONP.PWM1&amp;0x1" id="PWM" location="0x40018000"/> +<peripheralInstance derived_from="LPC17_I2C" determined="infoFile" enable="SYSCTL.PCONP.PCI2C0&amp;0x1" id="I2C0" location="0x4001C000"/> +<peripheralInstance derived_from="LPC17_I2C" determined="infoFile" enable="SYSCTL.PCONP.PCI2C1&amp;0x1" id="I2C1" location="0x4005C000"/> +<peripheralInstance derived_from="LPC17_I2C" determined="infoFile" enable="SYSCTL.PCONP.PCI2C2&amp;0x1" id="I2C2" location="0x400A0000"/> +<peripheralInstance derived_from="LPC17_DMA" determined="infoFile" enable="SYSCTL.PCONP.PCGPDMA&amp;0x1" id="DMA" location="0x50004000"/> +<peripheralInstance derived_from="LPC17_ENET" determined="infoFile" enable="SYSCTL.PCONP.PCENET&amp;0x1" id="ENET" location="0x50000000"/> +<peripheralInstance derived_from="CM3_DCR" determined="infoFile" id="DCR" location="0xE000EDF0"/> +<peripheralInstance derived_from="LPC17_PCB" determined="infoFile" id="PCB" location="0x4002c000"/> +<peripheralInstance derived_from="LPC17_QEI" determined="infoFile" enable="SYSCTL.PCONP.PCQEI&amp;0x1" id="QEI" location="0x400bc000"/> +<peripheralInstance derived_from="LPC17_USBHOST" determined="infoFile" enable="USBCLKCTL.USBClkSt&amp;0x11=0x11" id="USBHOST" location="0x5000C000"/> +<peripheralInstance derived_from="LPC17_USBOTG" determined="infoFile" enable="USBCLKCTL.USBClkSt&amp;0x1c=0x1c" id="USBOTG" location="0x5000C000"/> +<peripheralInstance derived_from="LPC17_RTC" determined="infoFile" enable="SYSCTL.PCONP.PCRTC&amp;0x1" id="RTC" location="0x40024000"/> +<peripheralInstance derived_from="MPU" determined="infoFile" id="MPU" location="0xE000ED90"/> +<peripheralInstance derived_from="LPC1x_WDT" determined="infoFile" id="WDT" location="0x40000000"/> +<peripheralInstance derived_from="LPC17_FLASHCFG" determined="infoFile" id="FLASHACCEL" location="0x400FC000"/> +<peripheralInstance derived_from="GPIO_INT" determined="infoFile" id="GPIOINTMAP" location="0x40028080"/> +<peripheralInstance derived_from="LPC17_CANAFR" determined="infoFile" enable="SYSCTL.PCONP.PCCAN1&amp;0x1|SYSCTL.PCONP.PCCAN2&amp;0x1" id="CANAFR" location="0x4003C000"/> +<peripheralInstance derived_from="LPC17_CANCEN" determined="infoFile" enable="SYSCTL.PCONP.PCCAN1&amp;0x1|SYSCTL.PCONP.PCCAN2&amp;0x1" id="CANCEN" location="0x40040000"/> +<peripheralInstance derived_from="LPC17_CANWAKESLEEP" determined="infoFile" id="CANWAKESLEEP" location="0x400FC110"/> +<peripheralInstance derived_from="LPC17_CANCON" determined="infoFile" enable="SYSCTL.PCONP.PCCAN1&amp;0x1" id="CANCON1" location="0x40044000"/> +<peripheralInstance derived_from="LPC17_CANCON" determined="infoFile" enable="SYSCTL.PCONP.PCCAN2&amp;0x1" id="CANCON2" location="0x40048000"/> +<peripheralInstance derived_from="LPC17_MCPWM" determined="infoFile" enable="SYSCTL.PCONP.PCMCPWM&amp;0x1" id="MCPWM" location="0x400B8000"/> +<peripheralInstance derived_from="LPC17_FMC" determined="infoFile" id="FMC" location="0x40084000"/> </chip> <processor><name gcc_name="cortex-m3">Cortex-M3</name> <family>Cortex-M</family> diff --git a/projects/example/sample.net.httpcl/src/sketch.c b/projects/example/sample.net.httpcl/src/sketch.c index 3a0586d..a5329d3 100644 --- a/projects/example/sample.net.httpcl/src/sketch.c +++ b/projects/example/sample.net.httpcl/src/sketch.c @@ -6,15 +6,15 @@ #include "boot/sketch.h" #include "NyLPC_net.h" -#include "NyLPC_uipService.h" #include "NyLPC_http.h" #include "NyLPC_utils.h" +#include "../netif/mimicip/NyLPC_cMiMicIpNetIf.h" #include #include /** - * UDP loopback test + * httpcl test */ -NyLPC_TcNet_t net; +const struct NyLPC_TiNetInterface_Interface* netif; NyLPC_TcHttpClient_t http; NyLPC_TcNetConfig_t c2; struct NyLPC_TIPv4Addr client_addr; @@ -23,7 +23,8 @@ char buftx[512]; void setup(void) { //uipサービス初期化。いろいろ利用可能に。 - NyLPC_cNet_initialize(&net); + netif=NyLPC_cMiMicIpNetIf_getNetInterface(); + NyLPC_cNet_initialize(netif); NyLPC_cHttpClient_initialize(&http,buf,512); } @@ -31,14 +32,13 @@ void setup(void) void loop(void) { NyLPC_TChar rx[16]; - NyLPC_TUInt16 s; NyLPC_TInt16 rs; NyLPC_TIPv4Addr_set(&client_addr,192,168,128,196); NyLPC_cNetConfig_initialize(&c2,NyLPC_TBool_TRUE); NyLPC_cNetConfig_setIpAddr(&c2,192,168,128,39); NyLPC_cNetConfig_setGateway(&c2,192,168,128,254); - NyLPC_cNet_start(&net,&c2); + NyLPC_cNet_start(&(c2.super)); for(;;) { @@ -71,7 +71,7 @@ void loop(void) if(!NyLPC_cHttpClient_write(&http,"0123456789",10)){ break; } - s=NyLPC_cHttpClient_getStatus(&http); + NyLPC_cHttpClient_getStatus(&http); do{ if(NyLPC_cHttpClient_read(&http,rx,16,&rs)){ printf("%.*s",rx,rs); diff --git a/projects/example/sample.net.loopback/.cproject b/projects/example/sample.net.loopback/.cproject index c474604..93da158 100644 --- a/projects/example/sample.net.loopback/.cproject +++ b/projects/example/sample.net.loopback/.cproject @@ -1,7 +1,5 @@ - - - + @@ -9,18 +7,18 @@ - - + + - + @@ -47,9 +45,9 @@ + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + - + @@ -741,12 +177,13 @@ @@ -1339,11 +211,8 @@ <?xml version="1.0" encoding="UTF-8"?> <TargetConfig> -<Properties property_0="" property_1="" property_2="" property_3="NXP" property_4="LPC1768" property_count="5" version="1"/> -<infoList vendor="NXP"> -<info chip="LPC1768" match_id="0x00013f37,0x26013F37,0x26113F37" name="LPC1768" package="lpc17_lqfp100.xml"> -<chip> -<name>LPC1768</name> +<Properties property_0="" property_2="LPC175x_6x_512.cfx" property_3="NXP" property_4="LPC1768" property_count="5" version="70002"/> +<infoList vendor="NXP"><info chip="LPC1768" flash_driver="LPC175x_6x_512.cfx" match_id="0x00013f37,0x26013F37,0x26113F37" name="LPC1768" package="lpc17_lqfp100.xml" stub="crt_emu_cm3_nxp"><chip><name>LPC1768</name> <family>LPC17xx</family> <vendor>NXP (formerly Philips)</vendor> <reset board="None" core="Real" sys="Real"/> @@ -1403,9 +272,9 @@ <peripheralInstance derived_from="LPC17_CANCON" determined="infoFile" enable="SYSCTL.PCONP.PCCAN1&amp;0x1" id="CANCON1" location="0x40044000"/> <peripheralInstance derived_from="LPC17_CANCON" determined="infoFile" enable="SYSCTL.PCONP.PCCAN2&amp;0x1" id="CANCON2" location="0x40048000"/> <peripheralInstance derived_from="LPC17_MCPWM" determined="infoFile" enable="SYSCTL.PCONP.PCMCPWM&amp;0x1" id="MCPWM" location="0x400B8000"/> +<peripheralInstance derived_from="LPC17_FMC" determined="infoFile" id="FMC" location="0x40084000"/> </chip> -<processor> -<name gcc_name="cortex-m3">Cortex-M3</name> +<processor><name gcc_name="cortex-m3">Cortex-M3</name> <family>Cortex-M</family> </processor> <link href="nxp_lpcxxxx_peripheral.xme" show="embed" type="simple"/> @@ -1413,4 +282,571 @@ </infoList> </TargetConfig> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/projects/example/sample.net.loopback/src/sketch.c b/projects/example/sample.net.loopback/src/sketch.c index eeec85f..9273594 100644 --- a/projects/example/sample.net.loopback/src/sketch.c +++ b/projects/example/sample.net.loopback/src/sketch.c @@ -3,7 +3,9 @@ * 23番ポートでListenして、受信データをエコーバックします。 */ #include "boot/sketch.h" -#include "NyLPC_uipService.h" +#include "NyLPC_net.h" +#include "../netif/mimicip/NyLPC_cMiMicIpNetIf.h" + //イーサネット用の初期化情報 const struct NyLPC_TEthAddr ethaddr=NyLPC_TEthAddr_pack(0x02,0x01,0x02,0x03,0x04,0x05); const struct NyLPC_TIPv4Addr ipaddr=NyLPC_TIPv4Addr_pack(192,168,128,201); @@ -15,20 +17,21 @@ const struct NyLPC_TIPv4Addr gateway=NyLPC_TIPv4Addr_pack(192,168,128,254); #define NUM_OF_TH 8 struct TProc{ NyLPC_TcThread_t th; - NyLPC_TcTcpSocket_t socket; - char rbuf[512]; + NyLPC_TiTcpSocket_t* socket; }proc[NUM_OF_TH]; - +const struct NyLPC_TiNetInterface_Interface* netif; void setup(void) { int i; - //UipServiceの初期化 - NyLPC_cUipService_initialize(); + //uipサービス初期化。いろいろ利用可能に。 + netif=NyLPC_cMiMicIpNetIf_getNetInterface(); + NyLPC_cNet_initialize(netif); + //TCPスレッドの準備 for(i=0;isocket),3000)){ + if(!NyLPC_iTcpSocket_accept(proc->socket,3000)){ return -1; } //接続中は送信する。 - s=NyLPC_cTcpSocket_precv(&(proc->socket),(const void**)&buf,1000); + s=NyLPC_iTcpSocket_precv(proc->socket,(const void**)&buf,1000); while(s>=0){ if(s>0){ //受信した内容をそのまま送信。 src=buf; while(s>0){ - sent=NyLPC_cTcpSocket_send(&(proc->socket),src,s,30000); + sent=NyLPC_iTcpSocket_send(proc->socket,src,s,30000); if(sent<=0){ - NyLPC_cTcpSocket_close(&(proc->socket),10000); + NyLPC_iTcpSocket_close(proc->socket,10000); return -1; } //読み出し位置を調整 src+=sent; s-=sent; - NyLPC_cTcpSocket_pseek(&(proc->socket),sent); + NyLPC_iTcpSocket_pseek(proc->socket,sent); } } - s=NyLPC_cTcpSocket_precv(&(proc->socket),(const void**)&buf,1000); + s=NyLPC_iTcpSocket_precv(proc->socket,(const void**)&buf,1000); } - NyLPC_cTcpSocket_close(&(proc->socket),10000); + NyLPC_iTcpSocket_close(proc->socket,10000); return 1; } @@ -77,7 +80,7 @@ int server(void* p) void loop(void) { NyLPC_TcIPv4Config_t config; - NyLPC_TcTcpListener_t listener; + NyLPC_TiTcpListener_t* listener; int i; @@ -86,14 +89,14 @@ void loop(void) NyLPC_cIPv4Config_setIp(&config,&ipaddr,&netmask); - NyLPC_cTcpListener_initialize(&listener,23); - NyLPC_cUipService_start(&config); + listener=NyLPC_cNet_createTcpListenerEx(23); + NyLPC_cNet_start(&config); for(;;){ //ターミネイト状態のタスクを検索 for(i=0;i - - - + @@ -9,14 +7,14 @@ - - + + @@ -47,9 +45,9 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + @@ -519,7 +183,7 @@ @@ -886,8 +211,8 @@ <?xml version="1.0" encoding="UTF-8"?> <TargetConfig> -<Properties property_0="" property_3="NXP" property_4="LPC1768" property_count="5" version="1"/> -<infoList vendor="NXP"><info chip="LPC1768" match_id="0x00013f37,0x26013F37,0x26113F37" name="LPC1768" package="lpc17_lqfp100.xml"><chip><name>LPC1768</name> +<Properties property_0="" property_2="LPC175x_6x_512.cfx" property_3="NXP" property_4="LPC1768" property_count="5" version="70002"/> +<infoList vendor="NXP"><info chip="LPC1768" flash_driver="LPC175x_6x_512.cfx" match_id="0x00013f37,0x26013F37,0x26113F37" name="LPC1768" package="lpc17_lqfp100.xml" stub="crt_emu_cm3_nxp"><chip><name>LPC1768</name> <family>LPC17xx</family> <vendor>NXP (formerly Philips)</vendor> <reset board="None" core="Real" sys="Real"/> @@ -895,58 +220,59 @@ <memory can_program="true" id="Flash" is_ro="true" type="Flash"/> <memory id="RAM" type="RAM"/> <memory id="Periph" is_volatile="true" type="Peripheral"/> -<memoryInstance derived_from="Flash" id="MFlash512" location="0x0" size="0x80000"/> +<memoryInstance derived_from="Flash" id="MFlash512" location="0x00000000" size="0x80000"/> <memoryInstance derived_from="RAM" id="RamLoc32" location="0x10000000" size="0x8000"/> <memoryInstance derived_from="RAM" id="RamAHB32" location="0x2007c000" size="0x8000"/> <prog_flash blocksz="0x1000" location="0" maxprgbuff="0x1000" progwithcode="TRUE" size="0x10000"/> <prog_flash blocksz="0x8000" location="0x10000" maxprgbuff="0x1000" progwithcode="TRUE" size="0x70000"/> -<peripheralInstance derived_from="LPC17_NVIC" id="NVIC" location="0xE000E000"/> -<peripheralInstance derived_from="TIMER" enable="SYSCTL.PCONP.PCTIM0&amp;0x1" id="TIMER0" location="0x40004000"/> -<peripheralInstance derived_from="TIMER" enable="SYSCTL.PCONP.PCTIM1&amp;0x1" id="TIMER1" location="0x40008000"/> -<peripheralInstance derived_from="TIMER" enable="SYSCTL.PCONP.PCTIM2&amp;0x1" id="TIMER2" location="0x40090000"/> -<peripheralInstance derived_from="TIMER" enable="SYSCTL.PCONP.PCTIM3&amp;0x1" id="TIMER3" location="0x40094000"/> -<peripheralInstance derived_from="LPC17_RIT" enable="SYSCTL.PCONP.PCRIT&amp;0x1" id="RIT" location="0x400B0000"/> -<peripheralInstance derived_from="FGPIO" enable="SYSCTL.PCONP.PCGPIO&amp;0x1" id="GPIO0" location="0x2009C000"/> -<peripheralInstance derived_from="FGPIO" enable="SYSCTL.PCONP.PCGPIO&amp;0x1" id="GPIO1" location="0x2009C020"/> -<peripheralInstance derived_from="FGPIO" enable="SYSCTL.PCONP.PCGPIO&amp;0x1" id="GPIO2" location="0x2009C040"/> -<peripheralInstance derived_from="FGPIO" enable="SYSCTL.PCONP.PCGPIO&amp;0x1" id="GPIO3" location="0x2009C060"/> -<peripheralInstance derived_from="FGPIO" enable="SYSCTL.PCONP.PCGPIO&amp;0x1" id="GPIO4" location="0x2009C080"/> -<peripheralInstance derived_from="LPC17_I2S" enable="SYSCTL.PCONP&amp;0x08000000" id="I2S" location="0x400A8000"/> -<peripheralInstance derived_from="LPC17_SYSCTL" id="SYSCTL" location="0x400FC000"/> -<peripheralInstance derived_from="LPC17_DAC" enable="PCB.PINSEL1.P0_26&amp;0x2=2" id="DAC" location="0x4008C000"/> -<peripheralInstance derived_from="LPC17xx_UART" enable="SYSCTL.PCONP.PCUART0&amp;0x1" id="UART0" location="0x4000C000"/> -<peripheralInstance derived_from="LPC17xx_UART_MODEM" enable="SYSCTL.PCONP.PCUART1&amp;0x1" id="UART1" location="0x40010000"/> -<peripheralInstance derived_from="LPC17xx_UART" enable="SYSCTL.PCONP.PCUART2&amp;0x1" id="UART2" location="0x40098000"/> -<peripheralInstance derived_from="LPC17xx_UART" enable="SYSCTL.PCONP.PCUART3&amp;0x1" id="UART3" location="0x4009C000"/> -<peripheralInstance derived_from="SPI" enable="SYSCTL.PCONP.PCSPI&amp;0x1" id="SPI" location="0x40020000"/> -<peripheralInstance derived_from="LPC17_SSP" enable="SYSCTL.PCONP.PCSSP0&amp;0x1" id="SSP0" location="0x40088000"/> -<peripheralInstance derived_from="LPC17_SSP" enable="SYSCTL.PCONP.PCSSP1&amp;0x1" id="SSP1" location="0x40030000"/> -<peripheralInstance derived_from="LPC17_ADC" enable="SYSCTL.PCONP.PCAD&amp;0x1" id="ADC" location="0x40034000"/> -<peripheralInstance derived_from="LPC17_USBINTST" enable="USBCLKCTL.USBClkCtrl&amp;0x12" id="USBINTSTAT" location="0x400fc1c0"/> -<peripheralInstance derived_from="LPC17_USB_CLK_CTL" id="USBCLKCTL" location="0x5000cff4"/> -<peripheralInstance derived_from="LPC17_USBDEV" enable="USBCLKCTL.USBClkSt&amp;0x12=0x12" id="USBDEV" location="0x5000C200"/> -<peripheralInstance derived_from="LPC17_PWM" enable="SYSCTL.PCONP.PWM1&amp;0x1" id="PWM" location="0x40018000"/> -<peripheralInstance derived_from="LPC17_I2C" enable="SYSCTL.PCONP.PCI2C0&amp;0x1" id="I2C0" location="0x4001C000"/> -<peripheralInstance derived_from="LPC17_I2C" enable="SYSCTL.PCONP.PCI2C1&amp;0x1" id="I2C1" location="0x4005C000"/> -<peripheralInstance derived_from="LPC17_I2C" enable="SYSCTL.PCONP.PCI2C2&amp;0x1" id="I2C2" location="0x400A0000"/> -<peripheralInstance derived_from="LPC17_DMA" enable="SYSCTL.PCONP.PCGPDMA&amp;0x1" id="DMA" location="0x50004000"/> -<peripheralInstance derived_from="LPC17_ENET" enable="SYSCTL.PCONP.PCENET&amp;0x1" id="ENET" location="0x50000000"/> -<peripheralInstance derived_from="CM3_DCR" id="DCR" location="0xE000EDF0"/> -<peripheralInstance derived_from="LPC17_PCB" id="PCB" location="0x4002c000"/> -<peripheralInstance derived_from="LPC17_QEI" enable="SYSCTL.PCONP.PCQEI&amp;0x1" id="QEI" location="0x400bc000"/> -<peripheralInstance derived_from="LPC17_USBHOST" enable="USBCLKCTL.USBClkSt&amp;0x11=0x11" id="USBHOST" location="0x5000C000"/> -<peripheralInstance derived_from="LPC17_USBOTG" enable="USBCLKCTL.USBClkSt&amp;0x1c=0x1c" id="USBOTG" location="0x5000C000"/> -<peripheralInstance derived_from="LPC17_RTC" enable="SYSCTL.PCONP.PCRTC&amp;0x1" id="RTC" location="0x40024000"/> -<peripheralInstance derived_from="MPU" id="MPU" location="0xE000ED90"/> -<peripheralInstance derived_from="LPC1x_WDT" id="WDT" location="0x40000000"/> -<peripheralInstance derived_from="LPC17_FLASHCFG" id="FLASHACCEL" location="0x400FC000"/> -<peripheralInstance derived_from="GPIO_INT" id="GPIOINTMAP" location="0x40028080"/> -<peripheralInstance derived_from="LPC17_CANAFR" enable="SYSCTL.PCONP.PCCAN1&amp;0x1|SYSCTL.PCONP.PCCAN2&amp;0x1" id="CANAFR" location="0x4003C000"/> -<peripheralInstance derived_from="LPC17_CANCEN" enable="SYSCTL.PCONP.PCCAN1&amp;0x1|SYSCTL.PCONP.PCCAN2&amp;0x1" id="CANCEN" location="0x40040000"/> -<peripheralInstance derived_from="LPC17_CANWAKESLEEP" id="CANWAKESLEEP" location="0x400FC110"/> -<peripheralInstance derived_from="LPC17_CANCON" enable="SYSCTL.PCONP.PCCAN1&amp;0x1" id="CANCON1" location="0x40044000"/> -<peripheralInstance derived_from="LPC17_CANCON" enable="SYSCTL.PCONP.PCCAN2&amp;0x1" id="CANCON2" location="0x40048000"/> -<peripheralInstance derived_from="LPC17_MCPWM" enable="SYSCTL.PCONP.PCMCPWM&amp;0x1" id="MCPWM" location="0x400B8000"/> +<peripheralInstance derived_from="LPC17_NVIC" determined="infoFile" id="NVIC" location="0xE000E000"/> +<peripheralInstance derived_from="TIMER" determined="infoFile" enable="SYSCTL.PCONP.PCTIM0&amp;0x1" id="TIMER0" location="0x40004000"/> +<peripheralInstance derived_from="TIMER" determined="infoFile" enable="SYSCTL.PCONP.PCTIM1&amp;0x1" id="TIMER1" location="0x40008000"/> +<peripheralInstance derived_from="TIMER" determined="infoFile" enable="SYSCTL.PCONP.PCTIM2&amp;0x1" id="TIMER2" location="0x40090000"/> +<peripheralInstance derived_from="TIMER" determined="infoFile" enable="SYSCTL.PCONP.PCTIM3&amp;0x1" id="TIMER3" location="0x40094000"/> +<peripheralInstance derived_from="LPC17_RIT" determined="infoFile" enable="SYSCTL.PCONP.PCRIT&amp;0x1" id="RIT" location="0x400B0000"/> +<peripheralInstance derived_from="FGPIO" determined="infoFile" enable="SYSCTL.PCONP.PCGPIO&amp;0x1" id="GPIO0" location="0x2009C000"/> +<peripheralInstance derived_from="FGPIO" determined="infoFile" enable="SYSCTL.PCONP.PCGPIO&amp;0x1" id="GPIO1" location="0x2009C020"/> +<peripheralInstance derived_from="FGPIO" determined="infoFile" enable="SYSCTL.PCONP.PCGPIO&amp;0x1" id="GPIO2" location="0x2009C040"/> +<peripheralInstance derived_from="FGPIO" determined="infoFile" enable="SYSCTL.PCONP.PCGPIO&amp;0x1" id="GPIO3" location="0x2009C060"/> +<peripheralInstance derived_from="FGPIO" determined="infoFile" enable="SYSCTL.PCONP.PCGPIO&amp;0x1" id="GPIO4" location="0x2009C080"/> +<peripheralInstance derived_from="LPC17_I2S" determined="infoFile" enable="SYSCTL.PCONP&amp;0x08000000" id="I2S" location="0x400A8000"/> +<peripheralInstance derived_from="LPC17_SYSCTL" determined="infoFile" id="SYSCTL" location="0x400FC000"/> +<peripheralInstance derived_from="LPC17_DAC" determined="infoFile" enable="PCB.PINSEL1.P0_26&amp;0x2=2" id="DAC" location="0x4008C000"/> +<peripheralInstance derived_from="LPC17xx_UART" determined="infoFile" enable="SYSCTL.PCONP.PCUART0&amp;0x1" id="UART0" location="0x4000C000"/> +<peripheralInstance derived_from="LPC17xx_UART_MODEM" determined="infoFile" enable="SYSCTL.PCONP.PCUART1&amp;0x1" id="UART1" location="0x40010000"/> +<peripheralInstance derived_from="LPC17xx_UART" determined="infoFile" enable="SYSCTL.PCONP.PCUART2&amp;0x1" id="UART2" location="0x40098000"/> +<peripheralInstance derived_from="LPC17xx_UART" determined="infoFile" enable="SYSCTL.PCONP.PCUART3&amp;0x1" id="UART3" location="0x4009C000"/> +<peripheralInstance derived_from="SPI" determined="infoFile" enable="SYSCTL.PCONP.PCSPI&amp;0x1" id="SPI" location="0x40020000"/> +<peripheralInstance derived_from="LPC17_SSP" determined="infoFile" enable="SYSCTL.PCONP.PCSSP0&amp;0x1" id="SSP0" location="0x40088000"/> +<peripheralInstance derived_from="LPC17_SSP" determined="infoFile" enable="SYSCTL.PCONP.PCSSP1&amp;0x1" id="SSP1" location="0x40030000"/> +<peripheralInstance derived_from="LPC17_ADC" determined="infoFile" enable="SYSCTL.PCONP.PCAD&amp;0x1" id="ADC" location="0x40034000"/> +<peripheralInstance derived_from="LPC17_USBINTST" determined="infoFile" enable="USBCLKCTL.USBClkCtrl&amp;0x12" id="USBINTSTAT" location="0x400fc1c0"/> +<peripheralInstance derived_from="LPC17_USB_CLK_CTL" determined="infoFile" id="USBCLKCTL" location="0x5000cff4"/> +<peripheralInstance derived_from="LPC17_USBDEV" determined="infoFile" enable="USBCLKCTL.USBClkSt&amp;0x12=0x12" id="USBDEV" location="0x5000C200"/> +<peripheralInstance derived_from="LPC17_PWM" determined="infoFile" enable="SYSCTL.PCONP.PWM1&amp;0x1" id="PWM" location="0x40018000"/> +<peripheralInstance derived_from="LPC17_I2C" determined="infoFile" enable="SYSCTL.PCONP.PCI2C0&amp;0x1" id="I2C0" location="0x4001C000"/> +<peripheralInstance derived_from="LPC17_I2C" determined="infoFile" enable="SYSCTL.PCONP.PCI2C1&amp;0x1" id="I2C1" location="0x4005C000"/> +<peripheralInstance derived_from="LPC17_I2C" determined="infoFile" enable="SYSCTL.PCONP.PCI2C2&amp;0x1" id="I2C2" location="0x400A0000"/> +<peripheralInstance derived_from="LPC17_DMA" determined="infoFile" enable="SYSCTL.PCONP.PCGPDMA&amp;0x1" id="DMA" location="0x50004000"/> +<peripheralInstance derived_from="LPC17_ENET" determined="infoFile" enable="SYSCTL.PCONP.PCENET&amp;0x1" id="ENET" location="0x50000000"/> +<peripheralInstance derived_from="CM3_DCR" determined="infoFile" id="DCR" location="0xE000EDF0"/> +<peripheralInstance derived_from="LPC17_PCB" determined="infoFile" id="PCB" location="0x4002c000"/> +<peripheralInstance derived_from="LPC17_QEI" determined="infoFile" enable="SYSCTL.PCONP.PCQEI&amp;0x1" id="QEI" location="0x400bc000"/> +<peripheralInstance derived_from="LPC17_USBHOST" determined="infoFile" enable="USBCLKCTL.USBClkSt&amp;0x11=0x11" id="USBHOST" location="0x5000C000"/> +<peripheralInstance derived_from="LPC17_USBOTG" determined="infoFile" enable="USBCLKCTL.USBClkSt&amp;0x1c=0x1c" id="USBOTG" location="0x5000C000"/> +<peripheralInstance derived_from="LPC17_RTC" determined="infoFile" enable="SYSCTL.PCONP.PCRTC&amp;0x1" id="RTC" location="0x40024000"/> +<peripheralInstance derived_from="MPU" determined="infoFile" id="MPU" location="0xE000ED90"/> +<peripheralInstance derived_from="LPC1x_WDT" determined="infoFile" id="WDT" location="0x40000000"/> +<peripheralInstance derived_from="LPC17_FLASHCFG" determined="infoFile" id="FLASHACCEL" location="0x400FC000"/> +<peripheralInstance derived_from="GPIO_INT" determined="infoFile" id="GPIOINTMAP" location="0x40028080"/> +<peripheralInstance derived_from="LPC17_CANAFR" determined="infoFile" enable="SYSCTL.PCONP.PCCAN1&amp;0x1|SYSCTL.PCONP.PCCAN2&amp;0x1" id="CANAFR" location="0x4003C000"/> +<peripheralInstance derived_from="LPC17_CANCEN" determined="infoFile" enable="SYSCTL.PCONP.PCCAN1&amp;0x1|SYSCTL.PCONP.PCCAN2&amp;0x1" id="CANCEN" location="0x40040000"/> +<peripheralInstance derived_from="LPC17_CANWAKESLEEP" determined="infoFile" id="CANWAKESLEEP" location="0x400FC110"/> +<peripheralInstance derived_from="LPC17_CANCON" determined="infoFile" enable="SYSCTL.PCONP.PCCAN1&amp;0x1" id="CANCON1" location="0x40044000"/> +<peripheralInstance derived_from="LPC17_CANCON" determined="infoFile" enable="SYSCTL.PCONP.PCCAN2&amp;0x1" id="CANCON2" location="0x40048000"/> +<peripheralInstance derived_from="LPC17_MCPWM" determined="infoFile" enable="SYSCTL.PCONP.PCMCPWM&amp;0x1" id="MCPWM" location="0x400B8000"/> +<peripheralInstance derived_from="LPC17_FMC" determined="infoFile" id="FMC" location="0x40084000"/> </chip> <processor><name gcc_name="cortex-m3">Cortex-M3</name> <family>Cortex-M</family> diff --git a/projects/example/sample.net.mdns/src/sketch.c b/projects/example/sample.net.mdns/src/sketch.c index 6b913fc..5128449 100644 --- a/projects/example/sample.net.mdns/src/sketch.c +++ b/projects/example/sample.net.mdns/src/sketch.c @@ -4,6 +4,7 @@ #include "boot/sketch.h" #include "NyLPC_net.h" +#include "../netif/mimicip/NyLPC_cMiMicIpNetIf.h" #include #include @@ -20,13 +21,14 @@ struct NyLPC_TDnsRecord a={ sr }; -NyLPC_TcNet_t net; -NyLPC_TcMDnsServer_t mdns; +NyLPC_TcMDnsServer_t mdns; +const struct NyLPC_TiNetInterface_Interface* netif; void setup(void) { //uipサービス初期化。いろいろ利用可能に。 - NyLPC_cNet_initialize(&net); + netif=NyLPC_cMiMicIpNetIf_getNetInterface(); + NyLPC_cNet_initialize(netif); NyLPC_cMDnsServer_initialize(&mdns,&a); } @@ -41,7 +43,7 @@ void loop(void) NyLPC_cNetConfig_initialize(&c2,NyLPC_TBool_TRUE); NyLPC_cNetConfig_setIpAddr(&c2,192,168,128,39); NyLPC_cNetConfig_setGateway(&c2,192,168,128,254); - NyLPC_cNet_start(&net,&c2); + NyLPC_cNet_start(&(c2.super)); for(;;); } diff --git a/projects/example/sample.net.simplehttpd/.cproject b/projects/example/sample.net.simplehttpd/.cproject index 2cfbfd5..d21d274 100644 --- a/projects/example/sample.net.simplehttpd/.cproject +++ b/projects/example/sample.net.simplehttpd/.cproject @@ -1,7 +1,5 @@ - - - + @@ -9,18 +7,18 @@ - - + + - + @@ -47,9 +45,9 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + - + @@ -522,7 +182,7 @@ @@ -889,11 +210,8 @@ <?xml version="1.0" encoding="UTF-8"?> <TargetConfig> -<Properties property_0="" property_1="" property_2="" property_3="NXP" property_4="LPC1768" property_count="5" version="1"/> -<infoList vendor="NXP"> -<info chip="LPC1768" match_id="0x00013f37,0x26013F37,0x26113F37" name="LPC1768" package="lpc17_lqfp100.xml"> -<chip> -<name>LPC1768</name> +<Properties property_0="" property_2="LPC175x_6x_512.cfx" property_3="NXP" property_4="LPC1768" property_count="5" version="70002"/> +<infoList vendor="NXP"><info chip="LPC1768" flash_driver="LPC175x_6x_512.cfx" match_id="0x00013f37,0x26013F37,0x26113F37" name="LPC1768" package="lpc17_lqfp100.xml" stub="crt_emu_cm3_nxp"><chip><name>LPC1768</name> <family>LPC17xx</family> <vendor>NXP (formerly Philips)</vendor> <reset board="None" core="Real" sys="Real"/> @@ -953,9 +271,9 @@ <peripheralInstance derived_from="LPC17_CANCON" determined="infoFile" enable="SYSCTL.PCONP.PCCAN1&amp;0x1" id="CANCON1" location="0x40044000"/> <peripheralInstance derived_from="LPC17_CANCON" determined="infoFile" enable="SYSCTL.PCONP.PCCAN2&amp;0x1" id="CANCON2" location="0x40048000"/> <peripheralInstance derived_from="LPC17_MCPWM" determined="infoFile" enable="SYSCTL.PCONP.PCMCPWM&amp;0x1" id="MCPWM" location="0x400B8000"/> +<peripheralInstance derived_from="LPC17_FMC" determined="infoFile" id="FMC" location="0x40084000"/> </chip> -<processor> -<name gcc_name="cortex-m3">Cortex-M3</name> +<processor><name gcc_name="cortex-m3">Cortex-M3</name> <family>Cortex-M</family> </processor> <link href="nxp_lpcxxxx_peripheral.xme" show="embed" type="simple"/> diff --git a/projects/example/sample.net.simplehttpd/src/sketch.c b/projects/example/sample.net.simplehttpd/src/sketch.c index e607943..06530d3 100644 --- a/projects/example/sample.net.simplehttpd/src/sketch.c +++ b/projects/example/sample.net.simplehttpd/src/sketch.c @@ -7,9 +7,9 @@ #include "NyLPC_http.h" #include "NyLPC_utils.h" +#include "../netif/mimicip/NyLPC_cMiMicIpNetIf.h" - -NyLPC_TcNet_t net; +const struct NyLPC_TiNetInterface_Interface* netif; struct MyHttpd{ NyLPC_TcHttpd_t super; }httpd; @@ -38,7 +38,8 @@ void handler(NyLPC_TcHttpdConnection_t* i_connection) void setup(void) { //uipサービス初期化。いろいろ利用可能に。 - NyLPC_cNet_initialize(&net); + netif=NyLPC_cMiMicIpNetIf_getNetInterface(); + NyLPC_cNet_initialize(netif); NyLPC_cHttpd_initialize(&httpd.super,80); httpd.super.function.onRequest=handler; @@ -50,7 +51,7 @@ void loop(void) NyLPC_TcNetConfig_t c2; //192.168.0.39 NyLPC_cNetConfig_initialize(&c2,NyLPC_TBool_TRUE); - NyLPC_cNet_start(&net,&c2); + NyLPC_cNet_start(&(c2.super)); NyLPC_cHttpd_loop(&httpd.super); for(;;); } diff --git a/projects/example/sample.net.udp/.cproject b/projects/example/sample.net.udp/.cproject index 7e0400b..269a1ad 100644 --- a/projects/example/sample.net.udp/.cproject +++ b/projects/example/sample.net.udp/.cproject @@ -1,7 +1,5 @@ - - - + @@ -9,18 +7,18 @@ - - + + - + @@ -47,9 +45,9 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + - + @@ -519,7 +183,7 @@ @@ -886,8 +211,8 @@ <?xml version="1.0" encoding="UTF-8"?> <TargetConfig> -<Properties property_0="" property_3="NXP" property_4="LPC1768" property_count="5" version="1"/> -<infoList vendor="NXP"><info chip="LPC1768" match_id="0x00013f37,0x26013F37,0x26113F37" name="LPC1768" package="lpc17_lqfp100.xml"><chip><name>LPC1768</name> +<Properties property_0="" property_2="LPC175x_6x_512.cfx" property_3="NXP" property_4="LPC1768" property_count="5" version="70002"/> +<infoList vendor="NXP"><info chip="LPC1768" flash_driver="LPC175x_6x_512.cfx" match_id="0x00013f37,0x26013F37,0x26113F37" name="LPC1768" package="lpc17_lqfp100.xml" stub="crt_emu_cm3_nxp"><chip><name>LPC1768</name> <family>LPC17xx</family> <vendor>NXP (formerly Philips)</vendor> <reset board="None" core="Real" sys="Real"/> @@ -895,58 +220,59 @@ <memory can_program="true" id="Flash" is_ro="true" type="Flash"/> <memory id="RAM" type="RAM"/> <memory id="Periph" is_volatile="true" type="Peripheral"/> -<memoryInstance derived_from="Flash" id="MFlash512" location="0x0" size="0x80000"/> +<memoryInstance derived_from="Flash" id="MFlash512" location="0x00000000" size="0x80000"/> <memoryInstance derived_from="RAM" id="RamLoc32" location="0x10000000" size="0x8000"/> <memoryInstance derived_from="RAM" id="RamAHB32" location="0x2007c000" size="0x8000"/> <prog_flash blocksz="0x1000" location="0" maxprgbuff="0x1000" progwithcode="TRUE" size="0x10000"/> <prog_flash blocksz="0x8000" location="0x10000" maxprgbuff="0x1000" progwithcode="TRUE" size="0x70000"/> -<peripheralInstance derived_from="LPC17_NVIC" id="NVIC" location="0xE000E000"/> -<peripheralInstance derived_from="TIMER" enable="SYSCTL.PCONP.PCTIM0&amp;0x1" id="TIMER0" location="0x40004000"/> -<peripheralInstance derived_from="TIMER" enable="SYSCTL.PCONP.PCTIM1&amp;0x1" id="TIMER1" location="0x40008000"/> -<peripheralInstance derived_from="TIMER" enable="SYSCTL.PCONP.PCTIM2&amp;0x1" id="TIMER2" location="0x40090000"/> -<peripheralInstance derived_from="TIMER" enable="SYSCTL.PCONP.PCTIM3&amp;0x1" id="TIMER3" location="0x40094000"/> -<peripheralInstance derived_from="LPC17_RIT" enable="SYSCTL.PCONP.PCRIT&amp;0x1" id="RIT" location="0x400B0000"/> -<peripheralInstance derived_from="FGPIO" enable="SYSCTL.PCONP.PCGPIO&amp;0x1" id="GPIO0" location="0x2009C000"/> -<peripheralInstance derived_from="FGPIO" enable="SYSCTL.PCONP.PCGPIO&amp;0x1" id="GPIO1" location="0x2009C020"/> -<peripheralInstance derived_from="FGPIO" enable="SYSCTL.PCONP.PCGPIO&amp;0x1" id="GPIO2" location="0x2009C040"/> -<peripheralInstance derived_from="FGPIO" enable="SYSCTL.PCONP.PCGPIO&amp;0x1" id="GPIO3" location="0x2009C060"/> -<peripheralInstance derived_from="FGPIO" enable="SYSCTL.PCONP.PCGPIO&amp;0x1" id="GPIO4" location="0x2009C080"/> -<peripheralInstance derived_from="LPC17_I2S" enable="SYSCTL.PCONP&amp;0x08000000" id="I2S" location="0x400A8000"/> -<peripheralInstance derived_from="LPC17_SYSCTL" id="SYSCTL" location="0x400FC000"/> -<peripheralInstance derived_from="LPC17_DAC" enable="PCB.PINSEL1.P0_26&amp;0x2=2" id="DAC" location="0x4008C000"/> -<peripheralInstance derived_from="LPC17xx_UART" enable="SYSCTL.PCONP.PCUART0&amp;0x1" id="UART0" location="0x4000C000"/> -<peripheralInstance derived_from="LPC17xx_UART_MODEM" enable="SYSCTL.PCONP.PCUART1&amp;0x1" id="UART1" location="0x40010000"/> -<peripheralInstance derived_from="LPC17xx_UART" enable="SYSCTL.PCONP.PCUART2&amp;0x1" id="UART2" location="0x40098000"/> -<peripheralInstance derived_from="LPC17xx_UART" enable="SYSCTL.PCONP.PCUART3&amp;0x1" id="UART3" location="0x4009C000"/> -<peripheralInstance derived_from="SPI" enable="SYSCTL.PCONP.PCSPI&amp;0x1" id="SPI" location="0x40020000"/> -<peripheralInstance derived_from="LPC17_SSP" enable="SYSCTL.PCONP.PCSSP0&amp;0x1" id="SSP0" location="0x40088000"/> -<peripheralInstance derived_from="LPC17_SSP" enable="SYSCTL.PCONP.PCSSP1&amp;0x1" id="SSP1" location="0x40030000"/> -<peripheralInstance derived_from="LPC17_ADC" enable="SYSCTL.PCONP.PCAD&amp;0x1" id="ADC" location="0x40034000"/> -<peripheralInstance derived_from="LPC17_USBINTST" enable="USBCLKCTL.USBClkCtrl&amp;0x12" id="USBINTSTAT" location="0x400fc1c0"/> -<peripheralInstance derived_from="LPC17_USB_CLK_CTL" id="USBCLKCTL" location="0x5000cff4"/> -<peripheralInstance derived_from="LPC17_USBDEV" enable="USBCLKCTL.USBClkSt&amp;0x12=0x12" id="USBDEV" location="0x5000C200"/> -<peripheralInstance derived_from="LPC17_PWM" enable="SYSCTL.PCONP.PWM1&amp;0x1" id="PWM" location="0x40018000"/> -<peripheralInstance derived_from="LPC17_I2C" enable="SYSCTL.PCONP.PCI2C0&amp;0x1" id="I2C0" location="0x4001C000"/> -<peripheralInstance derived_from="LPC17_I2C" enable="SYSCTL.PCONP.PCI2C1&amp;0x1" id="I2C1" location="0x4005C000"/> -<peripheralInstance derived_from="LPC17_I2C" enable="SYSCTL.PCONP.PCI2C2&amp;0x1" id="I2C2" location="0x400A0000"/> -<peripheralInstance derived_from="LPC17_DMA" enable="SYSCTL.PCONP.PCGPDMA&amp;0x1" id="DMA" location="0x50004000"/> -<peripheralInstance derived_from="LPC17_ENET" enable="SYSCTL.PCONP.PCENET&amp;0x1" id="ENET" location="0x50000000"/> -<peripheralInstance derived_from="CM3_DCR" id="DCR" location="0xE000EDF0"/> -<peripheralInstance derived_from="LPC17_PCB" id="PCB" location="0x4002c000"/> -<peripheralInstance derived_from="LPC17_QEI" enable="SYSCTL.PCONP.PCQEI&amp;0x1" id="QEI" location="0x400bc000"/> -<peripheralInstance derived_from="LPC17_USBHOST" enable="USBCLKCTL.USBClkSt&amp;0x11=0x11" id="USBHOST" location="0x5000C000"/> -<peripheralInstance derived_from="LPC17_USBOTG" enable="USBCLKCTL.USBClkSt&amp;0x1c=0x1c" id="USBOTG" location="0x5000C000"/> -<peripheralInstance derived_from="LPC17_RTC" enable="SYSCTL.PCONP.PCRTC&amp;0x1" id="RTC" location="0x40024000"/> -<peripheralInstance derived_from="MPU" id="MPU" location="0xE000ED90"/> -<peripheralInstance derived_from="LPC1x_WDT" id="WDT" location="0x40000000"/> -<peripheralInstance derived_from="LPC17_FLASHCFG" id="FLASHACCEL" location="0x400FC000"/> -<peripheralInstance derived_from="GPIO_INT" id="GPIOINTMAP" location="0x40028080"/> -<peripheralInstance derived_from="LPC17_CANAFR" enable="SYSCTL.PCONP.PCCAN1&amp;0x1|SYSCTL.PCONP.PCCAN2&amp;0x1" id="CANAFR" location="0x4003C000"/> -<peripheralInstance derived_from="LPC17_CANCEN" enable="SYSCTL.PCONP.PCCAN1&amp;0x1|SYSCTL.PCONP.PCCAN2&amp;0x1" id="CANCEN" location="0x40040000"/> -<peripheralInstance derived_from="LPC17_CANWAKESLEEP" id="CANWAKESLEEP" location="0x400FC110"/> -<peripheralInstance derived_from="LPC17_CANCON" enable="SYSCTL.PCONP.PCCAN1&amp;0x1" id="CANCON1" location="0x40044000"/> -<peripheralInstance derived_from="LPC17_CANCON" enable="SYSCTL.PCONP.PCCAN2&amp;0x1" id="CANCON2" location="0x40048000"/> -<peripheralInstance derived_from="LPC17_MCPWM" enable="SYSCTL.PCONP.PCMCPWM&amp;0x1" id="MCPWM" location="0x400B8000"/> +<peripheralInstance derived_from="LPC17_NVIC" determined="infoFile" id="NVIC" location="0xE000E000"/> +<peripheralInstance derived_from="TIMER" determined="infoFile" enable="SYSCTL.PCONP.PCTIM0&amp;0x1" id="TIMER0" location="0x40004000"/> +<peripheralInstance derived_from="TIMER" determined="infoFile" enable="SYSCTL.PCONP.PCTIM1&amp;0x1" id="TIMER1" location="0x40008000"/> +<peripheralInstance derived_from="TIMER" determined="infoFile" enable="SYSCTL.PCONP.PCTIM2&amp;0x1" id="TIMER2" location="0x40090000"/> +<peripheralInstance derived_from="TIMER" determined="infoFile" enable="SYSCTL.PCONP.PCTIM3&amp;0x1" id="TIMER3" location="0x40094000"/> +<peripheralInstance derived_from="LPC17_RIT" determined="infoFile" enable="SYSCTL.PCONP.PCRIT&amp;0x1" id="RIT" location="0x400B0000"/> +<peripheralInstance derived_from="FGPIO" determined="infoFile" enable="SYSCTL.PCONP.PCGPIO&amp;0x1" id="GPIO0" location="0x2009C000"/> +<peripheralInstance derived_from="FGPIO" determined="infoFile" enable="SYSCTL.PCONP.PCGPIO&amp;0x1" id="GPIO1" location="0x2009C020"/> +<peripheralInstance derived_from="FGPIO" determined="infoFile" enable="SYSCTL.PCONP.PCGPIO&amp;0x1" id="GPIO2" location="0x2009C040"/> +<peripheralInstance derived_from="FGPIO" determined="infoFile" enable="SYSCTL.PCONP.PCGPIO&amp;0x1" id="GPIO3" location="0x2009C060"/> +<peripheralInstance derived_from="FGPIO" determined="infoFile" enable="SYSCTL.PCONP.PCGPIO&amp;0x1" id="GPIO4" location="0x2009C080"/> +<peripheralInstance derived_from="LPC17_I2S" determined="infoFile" enable="SYSCTL.PCONP&amp;0x08000000" id="I2S" location="0x400A8000"/> +<peripheralInstance derived_from="LPC17_SYSCTL" determined="infoFile" id="SYSCTL" location="0x400FC000"/> +<peripheralInstance derived_from="LPC17_DAC" determined="infoFile" enable="PCB.PINSEL1.P0_26&amp;0x2=2" id="DAC" location="0x4008C000"/> +<peripheralInstance derived_from="LPC17xx_UART" determined="infoFile" enable="SYSCTL.PCONP.PCUART0&amp;0x1" id="UART0" location="0x4000C000"/> +<peripheralInstance derived_from="LPC17xx_UART_MODEM" determined="infoFile" enable="SYSCTL.PCONP.PCUART1&amp;0x1" id="UART1" location="0x40010000"/> +<peripheralInstance derived_from="LPC17xx_UART" determined="infoFile" enable="SYSCTL.PCONP.PCUART2&amp;0x1" id="UART2" location="0x40098000"/> +<peripheralInstance derived_from="LPC17xx_UART" determined="infoFile" enable="SYSCTL.PCONP.PCUART3&amp;0x1" id="UART3" location="0x4009C000"/> +<peripheralInstance derived_from="SPI" determined="infoFile" enable="SYSCTL.PCONP.PCSPI&amp;0x1" id="SPI" location="0x40020000"/> +<peripheralInstance derived_from="LPC17_SSP" determined="infoFile" enable="SYSCTL.PCONP.PCSSP0&amp;0x1" id="SSP0" location="0x40088000"/> +<peripheralInstance derived_from="LPC17_SSP" determined="infoFile" enable="SYSCTL.PCONP.PCSSP1&amp;0x1" id="SSP1" location="0x40030000"/> +<peripheralInstance derived_from="LPC17_ADC" determined="infoFile" enable="SYSCTL.PCONP.PCAD&amp;0x1" id="ADC" location="0x40034000"/> +<peripheralInstance derived_from="LPC17_USBINTST" determined="infoFile" enable="USBCLKCTL.USBClkCtrl&amp;0x12" id="USBINTSTAT" location="0x400fc1c0"/> +<peripheralInstance derived_from="LPC17_USB_CLK_CTL" determined="infoFile" id="USBCLKCTL" location="0x5000cff4"/> +<peripheralInstance derived_from="LPC17_USBDEV" determined="infoFile" enable="USBCLKCTL.USBClkSt&amp;0x12=0x12" id="USBDEV" location="0x5000C200"/> +<peripheralInstance derived_from="LPC17_PWM" determined="infoFile" enable="SYSCTL.PCONP.PWM1&amp;0x1" id="PWM" location="0x40018000"/> +<peripheralInstance derived_from="LPC17_I2C" determined="infoFile" enable="SYSCTL.PCONP.PCI2C0&amp;0x1" id="I2C0" location="0x4001C000"/> +<peripheralInstance derived_from="LPC17_I2C" determined="infoFile" enable="SYSCTL.PCONP.PCI2C1&amp;0x1" id="I2C1" location="0x4005C000"/> +<peripheralInstance derived_from="LPC17_I2C" determined="infoFile" enable="SYSCTL.PCONP.PCI2C2&amp;0x1" id="I2C2" location="0x400A0000"/> +<peripheralInstance derived_from="LPC17_DMA" determined="infoFile" enable="SYSCTL.PCONP.PCGPDMA&amp;0x1" id="DMA" location="0x50004000"/> +<peripheralInstance derived_from="LPC17_ENET" determined="infoFile" enable="SYSCTL.PCONP.PCENET&amp;0x1" id="ENET" location="0x50000000"/> +<peripheralInstance derived_from="CM3_DCR" determined="infoFile" id="DCR" location="0xE000EDF0"/> +<peripheralInstance derived_from="LPC17_PCB" determined="infoFile" id="PCB" location="0x4002c000"/> +<peripheralInstance derived_from="LPC17_QEI" determined="infoFile" enable="SYSCTL.PCONP.PCQEI&amp;0x1" id="QEI" location="0x400bc000"/> +<peripheralInstance derived_from="LPC17_USBHOST" determined="infoFile" enable="USBCLKCTL.USBClkSt&amp;0x11=0x11" id="USBHOST" location="0x5000C000"/> +<peripheralInstance derived_from="LPC17_USBOTG" determined="infoFile" enable="USBCLKCTL.USBClkSt&amp;0x1c=0x1c" id="USBOTG" location="0x5000C000"/> +<peripheralInstance derived_from="LPC17_RTC" determined="infoFile" enable="SYSCTL.PCONP.PCRTC&amp;0x1" id="RTC" location="0x40024000"/> +<peripheralInstance derived_from="MPU" determined="infoFile" id="MPU" location="0xE000ED90"/> +<peripheralInstance derived_from="LPC1x_WDT" determined="infoFile" id="WDT" location="0x40000000"/> +<peripheralInstance derived_from="LPC17_FLASHCFG" determined="infoFile" id="FLASHACCEL" location="0x400FC000"/> +<peripheralInstance derived_from="GPIO_INT" determined="infoFile" id="GPIOINTMAP" location="0x40028080"/> +<peripheralInstance derived_from="LPC17_CANAFR" determined="infoFile" enable="SYSCTL.PCONP.PCCAN1&amp;0x1|SYSCTL.PCONP.PCCAN2&amp;0x1" id="CANAFR" location="0x4003C000"/> +<peripheralInstance derived_from="LPC17_CANCEN" determined="infoFile" enable="SYSCTL.PCONP.PCCAN1&amp;0x1|SYSCTL.PCONP.PCCAN2&amp;0x1" id="CANCEN" location="0x40040000"/> +<peripheralInstance derived_from="LPC17_CANWAKESLEEP" determined="infoFile" id="CANWAKESLEEP" location="0x400FC110"/> +<peripheralInstance derived_from="LPC17_CANCON" determined="infoFile" enable="SYSCTL.PCONP.PCCAN1&amp;0x1" id="CANCON1" location="0x40044000"/> +<peripheralInstance derived_from="LPC17_CANCON" determined="infoFile" enable="SYSCTL.PCONP.PCCAN2&amp;0x1" id="CANCON2" location="0x40048000"/> +<peripheralInstance derived_from="LPC17_MCPWM" determined="infoFile" enable="SYSCTL.PCONP.PCMCPWM&amp;0x1" id="MCPWM" location="0x400B8000"/> +<peripheralInstance derived_from="LPC17_FMC" determined="infoFile" id="FMC" location="0x40084000"/> </chip> <processor><name gcc_name="cortex-m3">Cortex-M3</name> <family>Cortex-M</family> diff --git a/projects/example/sample.net.udp/src/sketch.c b/projects/example/sample.net.udp/src/sketch.c index d4544ce..43f3e0e 100644 --- a/projects/example/sample.net.udp/src/sketch.c +++ b/projects/example/sample.net.udp/src/sketch.c @@ -5,7 +5,7 @@ #include "boot/sketch.h" #include "NyLPC_net.h" -#include "NyLPC_uipService.h" +#include "../netif/mimicip/NyLPC_cMiMicIpNetIf.h" #include "NyLPC_http.h" #include "NyLPC_utils.h" #include @@ -13,14 +13,14 @@ /** * UDP loopback test */ -NyLPC_TcNet_t net; -NyLPC_TcUdpSocket_t udp; -char buf[512]; +const struct NyLPC_TiNetInterface_Interface* netif; +NyLPC_TiUdpSocket_t* udp; void setup(void) { //uipサービス初期化。いろいろ利用可能に。 - NyLPC_cNet_initialize(&net); - NyLPC_cUdpSocket_initialize(&udp,9999,buf,512); + netif=NyLPC_cMiMicIpNetIf_getNetInterface(); + NyLPC_cNet_initialize(netif); + udp=NyLPC_cNet_createUdpSocketEx(9999,NyLPC_TSocketType_UDP_NORMAL); } @@ -36,13 +36,13 @@ void loop(void) NyLPC_cNetConfig_initialize(&c2,NyLPC_TBool_TRUE); NyLPC_cNetConfig_setIpAddr(&c2,192,168,128,39); NyLPC_cNetConfig_setGateway(&c2,192,168,128,254); - NyLPC_cNet_start(&net,&c2); + NyLPC_cNet_start(&(c2.super)); for(;;) { - rs=NyLPC_cUdpSocket_precv(&udp,&b,&info,1000); + rs=NyLPC_iUdpSocket_precv(udp,&b,&info,1000); if(rs>0){ - NyLPC_cUdpSocket_send(&udp,&info->peer_ip,info->peer_port,b,rs,1000); - NyLPC_cUdpSocket_pseek(&udp); + NyLPC_iUdpSocket_send(udp,&info->peer_ip,info->peer_port,b,rs,1000); + NyLPC_iUdpSocket_pseek(udp); } } } diff --git a/projects/example/test.bigdata/.cproject b/projects/example/test.bigdata/.cproject index 04303d2..2f3d87b 100644 --- a/projects/example/test.bigdata/.cproject +++ b/projects/example/test.bigdata/.cproject @@ -1,7 +1,5 @@ - - - + @@ -9,18 +7,18 @@ - - + + - + @@ -47,9 +45,9 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + - + @@ -522,7 +182,7 @@ @@ -889,8 +210,8 @@ <?xml version="1.0" encoding="UTF-8"?> <TargetConfig> -<Properties property_0="" property_3="NXP" property_4="LPC1768" property_count="5" version="1"/> -<infoList vendor="NXP"><info chip="LPC1768" match_id="0x00013f37,0x26013F37,0x26113F37" name="LPC1768" package="lpc17_lqfp100.xml"><chip><name>LPC1768</name> +<Properties property_0="" property_2="LPC175x_6x_512.cfx" property_3="NXP" property_4="LPC1768" property_count="5" version="70002"/> +<infoList vendor="NXP"><info chip="LPC1768" flash_driver="LPC175x_6x_512.cfx" match_id="0x00013f37,0x26013F37,0x26113F37" name="LPC1768" package="lpc17_lqfp100.xml" stub="crt_emu_cm3_nxp"><chip><name>LPC1768</name> <family>LPC17xx</family> <vendor>NXP (formerly Philips)</vendor> <reset board="None" core="Real" sys="Real"/> @@ -950,6 +271,7 @@ <peripheralInstance derived_from="LPC17_CANCON" determined="infoFile" enable="SYSCTL.PCONP.PCCAN1&amp;0x1" id="CANCON1" location="0x40044000"/> <peripheralInstance derived_from="LPC17_CANCON" determined="infoFile" enable="SYSCTL.PCONP.PCCAN2&amp;0x1" id="CANCON2" location="0x40048000"/> <peripheralInstance derived_from="LPC17_MCPWM" determined="infoFile" enable="SYSCTL.PCONP.PCMCPWM&amp;0x1" id="MCPWM" location="0x400B8000"/> +<peripheralInstance derived_from="LPC17_FMC" determined="infoFile" id="FMC" location="0x40084000"/> </chip> <processor><name gcc_name="cortex-m3">Cortex-M3</name> <family>Cortex-M</family> diff --git a/projects/example/test.bigdata/src/sketch.c b/projects/example/test.bigdata/src/sketch.c index d008bfa..d4384f0 100644 --- a/projects/example/test.bigdata/src/sketch.c +++ b/projects/example/test.bigdata/src/sketch.c @@ -4,12 +4,12 @@ */ #include "boot/sketch.h" #include "NyLPC_net.h" - +#include "../netif/mimicip/NyLPC_cMiMicIpNetIf.h" #include "NyLPC_http.h" #include "NyLPC_utils.h" -NyLPC_TcNet_t net; +const struct NyLPC_TiNetInterface_Interface* netif; struct MyHttpd{ NyLPC_TcHttpd_t super; }httpd; @@ -37,7 +37,8 @@ void handler(NyLPC_TcHttpdConnection_t* i_connection) void setup(void) { //uipサービス初期化。いろいろ利用可能に。 - NyLPC_cNet_initialize(&net); + netif=NyLPC_cMiMicIpNetIf_getNetInterface(); + NyLPC_cNet_initialize(netif); NyLPC_cHttpd_initialize(&httpd.super,80); httpd.super.function.onRequest=handler; @@ -50,7 +51,7 @@ void loop(void) NyLPC_cNetConfig_initialize(&c2,NyLPC_TBool_TRUE); NyLPC_cNetConfig_setIpAddr(&c2,192,168,128,39); NyLPC_cNetConfig_setGateway(&c2,192,168,128,254); - NyLPC_cNet_start(&net,&c2); + NyLPC_cNet_start(&c2.super); NyLPC_cHttpd_loop(&httpd.super); for(;;); } diff --git a/projects/example/test.httppost/.cproject b/projects/example/test.httppost/.cproject index 2cfbfd5..c0d9b96 100644 --- a/projects/example/test.httppost/.cproject +++ b/projects/example/test.httppost/.cproject @@ -1,7 +1,5 @@ - - - + @@ -9,18 +7,18 @@ - - + + - + @@ -47,9 +45,9 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + - + @@ -522,7 +182,7 @@ @@ -889,11 +210,8 @@ <?xml version="1.0" encoding="UTF-8"?> <TargetConfig> -<Properties property_0="" property_1="" property_2="" property_3="NXP" property_4="LPC1768" property_count="5" version="1"/> -<infoList vendor="NXP"> -<info chip="LPC1768" match_id="0x00013f37,0x26013F37,0x26113F37" name="LPC1768" package="lpc17_lqfp100.xml"> -<chip> -<name>LPC1768</name> +<Properties property_0="" property_2="LPC175x_6x_512.cfx" property_3="NXP" property_4="LPC1768" property_count="5" version="70002"/> +<infoList vendor="NXP"><info chip="LPC1768" flash_driver="LPC175x_6x_512.cfx" match_id="0x00013f37,0x26013F37,0x26113F37" name="LPC1768" package="lpc17_lqfp100.xml" stub="crt_emu_cm3_nxp"><chip><name>LPC1768</name> <family>LPC17xx</family> <vendor>NXP (formerly Philips)</vendor> <reset board="None" core="Real" sys="Real"/> @@ -953,9 +271,9 @@ <peripheralInstance derived_from="LPC17_CANCON" determined="infoFile" enable="SYSCTL.PCONP.PCCAN1&amp;0x1" id="CANCON1" location="0x40044000"/> <peripheralInstance derived_from="LPC17_CANCON" determined="infoFile" enable="SYSCTL.PCONP.PCCAN2&amp;0x1" id="CANCON2" location="0x40048000"/> <peripheralInstance derived_from="LPC17_MCPWM" determined="infoFile" enable="SYSCTL.PCONP.PCMCPWM&amp;0x1" id="MCPWM" location="0x400B8000"/> +<peripheralInstance derived_from="LPC17_FMC" determined="infoFile" id="FMC" location="0x40084000"/> </chip> -<processor> -<name gcc_name="cortex-m3">Cortex-M3</name> +<processor><name gcc_name="cortex-m3">Cortex-M3</name> <family>Cortex-M</family> </processor> <link href="nxp_lpcxxxx_peripheral.xme" show="embed" type="simple"/> diff --git a/projects/example/test.httppost/src/sketch.c b/projects/example/test.httppost/src/sketch.c index 1054f19..b9f7f77 100644 --- a/projects/example/test.httppost/src/sketch.c +++ b/projects/example/test.httppost/src/sketch.c @@ -4,12 +4,12 @@ */ #include "boot/sketch.h" #include "NyLPC_net.h" - +#include "../netif/mimicip/NyLPC_cMiMicIpNetIf.h" #include "NyLPC_http.h" #include "NyLPC_utils.h" -NyLPC_TcNet_t net; +const struct NyLPC_TiNetInterface_Interface* netif; struct MyHttpd{ NyLPC_TcHttpd_t super; }httpd; diff --git a/projects/example/test.modfileio/.cproject b/projects/example/test.modfileio/.cproject index 2cfbfd5..57abcf0 100644 --- a/projects/example/test.modfileio/.cproject +++ b/projects/example/test.modfileio/.cproject @@ -1,7 +1,5 @@ - - - + @@ -9,18 +7,18 @@ - - + + - + @@ -47,9 +45,9 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + - + @@ -522,7 +182,7 @@ @@ -889,11 +210,8 @@ <?xml version="1.0" encoding="UTF-8"?> <TargetConfig> -<Properties property_0="" property_1="" property_2="" property_3="NXP" property_4="LPC1768" property_count="5" version="1"/> -<infoList vendor="NXP"> -<info chip="LPC1768" match_id="0x00013f37,0x26013F37,0x26113F37" name="LPC1768" package="lpc17_lqfp100.xml"> -<chip> -<name>LPC1768</name> +<Properties property_0="" property_2="LPC175x_6x_512.cfx" property_3="NXP" property_4="LPC1768" property_count="5" version="70002"/> +<infoList vendor="NXP"><info chip="LPC1768" flash_driver="LPC175x_6x_512.cfx" match_id="0x00013f37,0x26013F37,0x26113F37" name="LPC1768" package="lpc17_lqfp100.xml" stub="crt_emu_cm3_nxp"><chip><name>LPC1768</name> <family>LPC17xx</family> <vendor>NXP (formerly Philips)</vendor> <reset board="None" core="Real" sys="Real"/> @@ -953,9 +271,9 @@ <peripheralInstance derived_from="LPC17_CANCON" determined="infoFile" enable="SYSCTL.PCONP.PCCAN1&amp;0x1" id="CANCON1" location="0x40044000"/> <peripheralInstance derived_from="LPC17_CANCON" determined="infoFile" enable="SYSCTL.PCONP.PCCAN2&amp;0x1" id="CANCON2" location="0x40048000"/> <peripheralInstance derived_from="LPC17_MCPWM" determined="infoFile" enable="SYSCTL.PCONP.PCMCPWM&amp;0x1" id="MCPWM" location="0x400B8000"/> +<peripheralInstance derived_from="LPC17_FMC" determined="infoFile" id="FMC" location="0x40084000"/> </chip> -<processor> -<name gcc_name="cortex-m3">Cortex-M3</name> +<processor><name gcc_name="cortex-m3">Cortex-M3</name> <family>Cortex-M</family> </processor> <link href="nxp_lpcxxxx_peripheral.xme" show="embed" type="simple"/> diff --git a/projects/example/test.modfileio/src/sketch.c b/projects/example/test.modfileio/src/sketch.c index 4f1e8d8..9c516ce 100644 --- a/projects/example/test.modfileio/src/sketch.c +++ b/projects/example/test.modfileio/src/sketch.c @@ -7,9 +7,9 @@ #include "NyLPC_http.h" #include "NyLPC_utils.h" +#include "../netif/mimicip/NyLPC_cMiMicIpNetIf.h" - -NyLPC_TcNet_t net; +const struct NyLPC_TiNetInterface_Interface* netif; struct MyHttpd{ NyLPC_TcHttpd_t super; }httpd; @@ -88,7 +88,8 @@ void handler(NyLPC_TcHttpdConnection_t* i_connection) void setup(void) { //uipサービス初期化。いろいろ利用可能に。 - NyLPC_cNet_initialize(&net); + netif=NyLPC_cMiMicIpNetIf_getNetInterface(); + NyLPC_cNet_initialize(netif); NyLPC_cHttpd_initialize(&httpd.super,80); httpd.super.function.onRequest=handler; diff --git a/projects/example/test.udp/.cproject b/projects/example/test.udp/.cproject index 36c0f05..a5b27b5 100644 --- a/projects/example/test.udp/.cproject +++ b/projects/example/test.udp/.cproject @@ -1,7 +1,5 @@ - - - + @@ -9,18 +7,18 @@ - - + + - + @@ -47,9 +45,9 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + - + @@ -522,7 +182,7 @@ @@ -889,8 +210,8 @@ <?xml version="1.0" encoding="UTF-8"?> <TargetConfig> -<Properties property_0="" property_3="NXP" property_4="LPC1768" property_count="5" version="1"/> -<infoList vendor="NXP"><info chip="LPC1768" match_id="0x00013f37,0x26013F37,0x26113F37" name="LPC1768" package="lpc17_lqfp100.xml"><chip><name>LPC1768</name> +<Properties property_0="" property_2="LPC175x_6x_512.cfx" property_3="NXP" property_4="LPC1768" property_count="5" version="70002"/> +<infoList vendor="NXP"><info chip="LPC1768" flash_driver="LPC175x_6x_512.cfx" match_id="0x00013f37,0x26013F37,0x26113F37" name="LPC1768" package="lpc17_lqfp100.xml" stub="crt_emu_cm3_nxp"><chip><name>LPC1768</name> <family>LPC17xx</family> <vendor>NXP (formerly Philips)</vendor> <reset board="None" core="Real" sys="Real"/> @@ -898,58 +219,59 @@ <memory can_program="true" id="Flash" is_ro="true" type="Flash"/> <memory id="RAM" type="RAM"/> <memory id="Periph" is_volatile="true" type="Peripheral"/> -<memoryInstance derived_from="Flash" id="MFlash512" location="0x0" size="0x80000"/> +<memoryInstance derived_from="Flash" id="MFlash512" location="0x00000000" size="0x80000"/> <memoryInstance derived_from="RAM" id="RamLoc32" location="0x10000000" size="0x8000"/> <memoryInstance derived_from="RAM" id="RamAHB32" location="0x2007c000" size="0x8000"/> <prog_flash blocksz="0x1000" location="0" maxprgbuff="0x1000" progwithcode="TRUE" size="0x10000"/> <prog_flash blocksz="0x8000" location="0x10000" maxprgbuff="0x1000" progwithcode="TRUE" size="0x70000"/> -<peripheralInstance derived_from="LPC17_NVIC" id="NVIC" location="0xE000E000"/> -<peripheralInstance derived_from="TIMER" enable="SYSCTL.PCONP.PCTIM0&amp;0x1" id="TIMER0" location="0x40004000"/> -<peripheralInstance derived_from="TIMER" enable="SYSCTL.PCONP.PCTIM1&amp;0x1" id="TIMER1" location="0x40008000"/> -<peripheralInstance derived_from="TIMER" enable="SYSCTL.PCONP.PCTIM2&amp;0x1" id="TIMER2" location="0x40090000"/> -<peripheralInstance derived_from="TIMER" enable="SYSCTL.PCONP.PCTIM3&amp;0x1" id="TIMER3" location="0x40094000"/> -<peripheralInstance derived_from="LPC17_RIT" enable="SYSCTL.PCONP.PCRIT&amp;0x1" id="RIT" location="0x400B0000"/> -<peripheralInstance derived_from="FGPIO" enable="SYSCTL.PCONP.PCGPIO&amp;0x1" id="GPIO0" location="0x2009C000"/> -<peripheralInstance derived_from="FGPIO" enable="SYSCTL.PCONP.PCGPIO&amp;0x1" id="GPIO1" location="0x2009C020"/> -<peripheralInstance derived_from="FGPIO" enable="SYSCTL.PCONP.PCGPIO&amp;0x1" id="GPIO2" location="0x2009C040"/> -<peripheralInstance derived_from="FGPIO" enable="SYSCTL.PCONP.PCGPIO&amp;0x1" id="GPIO3" location="0x2009C060"/> -<peripheralInstance derived_from="FGPIO" enable="SYSCTL.PCONP.PCGPIO&amp;0x1" id="GPIO4" location="0x2009C080"/> -<peripheralInstance derived_from="LPC17_I2S" enable="SYSCTL.PCONP&amp;0x08000000" id="I2S" location="0x400A8000"/> -<peripheralInstance derived_from="LPC17_SYSCTL" id="SYSCTL" location="0x400FC000"/> -<peripheralInstance derived_from="LPC17_DAC" enable="PCB.PINSEL1.P0_26&amp;0x2=2" id="DAC" location="0x4008C000"/> -<peripheralInstance derived_from="LPC17xx_UART" enable="SYSCTL.PCONP.PCUART0&amp;0x1" id="UART0" location="0x4000C000"/> -<peripheralInstance derived_from="LPC17xx_UART_MODEM" enable="SYSCTL.PCONP.PCUART1&amp;0x1" id="UART1" location="0x40010000"/> -<peripheralInstance derived_from="LPC17xx_UART" enable="SYSCTL.PCONP.PCUART2&amp;0x1" id="UART2" location="0x40098000"/> -<peripheralInstance derived_from="LPC17xx_UART" enable="SYSCTL.PCONP.PCUART3&amp;0x1" id="UART3" location="0x4009C000"/> -<peripheralInstance derived_from="SPI" enable="SYSCTL.PCONP.PCSPI&amp;0x1" id="SPI" location="0x40020000"/> -<peripheralInstance derived_from="LPC17_SSP" enable="SYSCTL.PCONP.PCSSP0&amp;0x1" id="SSP0" location="0x40088000"/> -<peripheralInstance derived_from="LPC17_SSP" enable="SYSCTL.PCONP.PCSSP1&amp;0x1" id="SSP1" location="0x40030000"/> -<peripheralInstance derived_from="LPC17_ADC" enable="SYSCTL.PCONP.PCAD&amp;0x1" id="ADC" location="0x40034000"/> -<peripheralInstance derived_from="LPC17_USBINTST" enable="USBCLKCTL.USBClkCtrl&amp;0x12" id="USBINTSTAT" location="0x400fc1c0"/> -<peripheralInstance derived_from="LPC17_USB_CLK_CTL" id="USBCLKCTL" location="0x5000cff4"/> -<peripheralInstance derived_from="LPC17_USBDEV" enable="USBCLKCTL.USBClkSt&amp;0x12=0x12" id="USBDEV" location="0x5000C200"/> -<peripheralInstance derived_from="LPC17_PWM" enable="SYSCTL.PCONP.PWM1&amp;0x1" id="PWM" location="0x40018000"/> -<peripheralInstance derived_from="LPC17_I2C" enable="SYSCTL.PCONP.PCI2C0&amp;0x1" id="I2C0" location="0x4001C000"/> -<peripheralInstance derived_from="LPC17_I2C" enable="SYSCTL.PCONP.PCI2C1&amp;0x1" id="I2C1" location="0x4005C000"/> -<peripheralInstance derived_from="LPC17_I2C" enable="SYSCTL.PCONP.PCI2C2&amp;0x1" id="I2C2" location="0x400A0000"/> -<peripheralInstance derived_from="LPC17_DMA" enable="SYSCTL.PCONP.PCGPDMA&amp;0x1" id="DMA" location="0x50004000"/> -<peripheralInstance derived_from="LPC17_ENET" enable="SYSCTL.PCONP.PCENET&amp;0x1" id="ENET" location="0x50000000"/> -<peripheralInstance derived_from="CM3_DCR" id="DCR" location="0xE000EDF0"/> -<peripheralInstance derived_from="LPC17_PCB" id="PCB" location="0x4002c000"/> -<peripheralInstance derived_from="LPC17_QEI" enable="SYSCTL.PCONP.PCQEI&amp;0x1" id="QEI" location="0x400bc000"/> -<peripheralInstance derived_from="LPC17_USBHOST" enable="USBCLKCTL.USBClkSt&amp;0x11=0x11" id="USBHOST" location="0x5000C000"/> -<peripheralInstance derived_from="LPC17_USBOTG" enable="USBCLKCTL.USBClkSt&amp;0x1c=0x1c" id="USBOTG" location="0x5000C000"/> -<peripheralInstance derived_from="LPC17_RTC" enable="SYSCTL.PCONP.PCRTC&amp;0x1" id="RTC" location="0x40024000"/> -<peripheralInstance derived_from="MPU" id="MPU" location="0xE000ED90"/> -<peripheralInstance derived_from="LPC1x_WDT" id="WDT" location="0x40000000"/> -<peripheralInstance derived_from="LPC17_FLASHCFG" id="FLASHACCEL" location="0x400FC000"/> -<peripheralInstance derived_from="GPIO_INT" id="GPIOINTMAP" location="0x40028080"/> -<peripheralInstance derived_from="LPC17_CANAFR" enable="SYSCTL.PCONP.PCCAN1&amp;0x1|SYSCTL.PCONP.PCCAN2&amp;0x1" id="CANAFR" location="0x4003C000"/> -<peripheralInstance derived_from="LPC17_CANCEN" enable="SYSCTL.PCONP.PCCAN1&amp;0x1|SYSCTL.PCONP.PCCAN2&amp;0x1" id="CANCEN" location="0x40040000"/> -<peripheralInstance derived_from="LPC17_CANWAKESLEEP" id="CANWAKESLEEP" location="0x400FC110"/> -<peripheralInstance derived_from="LPC17_CANCON" enable="SYSCTL.PCONP.PCCAN1&amp;0x1" id="CANCON1" location="0x40044000"/> -<peripheralInstance derived_from="LPC17_CANCON" enable="SYSCTL.PCONP.PCCAN2&amp;0x1" id="CANCON2" location="0x40048000"/> -<peripheralInstance derived_from="LPC17_MCPWM" enable="SYSCTL.PCONP.PCMCPWM&amp;0x1" id="MCPWM" location="0x400B8000"/> +<peripheralInstance derived_from="LPC17_NVIC" determined="infoFile" id="NVIC" location="0xE000E000"/> +<peripheralInstance derived_from="TIMER" determined="infoFile" enable="SYSCTL.PCONP.PCTIM0&amp;0x1" id="TIMER0" location="0x40004000"/> +<peripheralInstance derived_from="TIMER" determined="infoFile" enable="SYSCTL.PCONP.PCTIM1&amp;0x1" id="TIMER1" location="0x40008000"/> +<peripheralInstance derived_from="TIMER" determined="infoFile" enable="SYSCTL.PCONP.PCTIM2&amp;0x1" id="TIMER2" location="0x40090000"/> +<peripheralInstance derived_from="TIMER" determined="infoFile" enable="SYSCTL.PCONP.PCTIM3&amp;0x1" id="TIMER3" location="0x40094000"/> +<peripheralInstance derived_from="LPC17_RIT" determined="infoFile" enable="SYSCTL.PCONP.PCRIT&amp;0x1" id="RIT" location="0x400B0000"/> +<peripheralInstance derived_from="FGPIO" determined="infoFile" enable="SYSCTL.PCONP.PCGPIO&amp;0x1" id="GPIO0" location="0x2009C000"/> +<peripheralInstance derived_from="FGPIO" determined="infoFile" enable="SYSCTL.PCONP.PCGPIO&amp;0x1" id="GPIO1" location="0x2009C020"/> +<peripheralInstance derived_from="FGPIO" determined="infoFile" enable="SYSCTL.PCONP.PCGPIO&amp;0x1" id="GPIO2" location="0x2009C040"/> +<peripheralInstance derived_from="FGPIO" determined="infoFile" enable="SYSCTL.PCONP.PCGPIO&amp;0x1" id="GPIO3" location="0x2009C060"/> +<peripheralInstance derived_from="FGPIO" determined="infoFile" enable="SYSCTL.PCONP.PCGPIO&amp;0x1" id="GPIO4" location="0x2009C080"/> +<peripheralInstance derived_from="LPC17_I2S" determined="infoFile" enable="SYSCTL.PCONP&amp;0x08000000" id="I2S" location="0x400A8000"/> +<peripheralInstance derived_from="LPC17_SYSCTL" determined="infoFile" id="SYSCTL" location="0x400FC000"/> +<peripheralInstance derived_from="LPC17_DAC" determined="infoFile" enable="PCB.PINSEL1.P0_26&amp;0x2=2" id="DAC" location="0x4008C000"/> +<peripheralInstance derived_from="LPC17xx_UART" determined="infoFile" enable="SYSCTL.PCONP.PCUART0&amp;0x1" id="UART0" location="0x4000C000"/> +<peripheralInstance derived_from="LPC17xx_UART_MODEM" determined="infoFile" enable="SYSCTL.PCONP.PCUART1&amp;0x1" id="UART1" location="0x40010000"/> +<peripheralInstance derived_from="LPC17xx_UART" determined="infoFile" enable="SYSCTL.PCONP.PCUART2&amp;0x1" id="UART2" location="0x40098000"/> +<peripheralInstance derived_from="LPC17xx_UART" determined="infoFile" enable="SYSCTL.PCONP.PCUART3&amp;0x1" id="UART3" location="0x4009C000"/> +<peripheralInstance derived_from="SPI" determined="infoFile" enable="SYSCTL.PCONP.PCSPI&amp;0x1" id="SPI" location="0x40020000"/> +<peripheralInstance derived_from="LPC17_SSP" determined="infoFile" enable="SYSCTL.PCONP.PCSSP0&amp;0x1" id="SSP0" location="0x40088000"/> +<peripheralInstance derived_from="LPC17_SSP" determined="infoFile" enable="SYSCTL.PCONP.PCSSP1&amp;0x1" id="SSP1" location="0x40030000"/> +<peripheralInstance derived_from="LPC17_ADC" determined="infoFile" enable="SYSCTL.PCONP.PCAD&amp;0x1" id="ADC" location="0x40034000"/> +<peripheralInstance derived_from="LPC17_USBINTST" determined="infoFile" enable="USBCLKCTL.USBClkCtrl&amp;0x12" id="USBINTSTAT" location="0x400fc1c0"/> +<peripheralInstance derived_from="LPC17_USB_CLK_CTL" determined="infoFile" id="USBCLKCTL" location="0x5000cff4"/> +<peripheralInstance derived_from="LPC17_USBDEV" determined="infoFile" enable="USBCLKCTL.USBClkSt&amp;0x12=0x12" id="USBDEV" location="0x5000C200"/> +<peripheralInstance derived_from="LPC17_PWM" determined="infoFile" enable="SYSCTL.PCONP.PWM1&amp;0x1" id="PWM" location="0x40018000"/> +<peripheralInstance derived_from="LPC17_I2C" determined="infoFile" enable="SYSCTL.PCONP.PCI2C0&amp;0x1" id="I2C0" location="0x4001C000"/> +<peripheralInstance derived_from="LPC17_I2C" determined="infoFile" enable="SYSCTL.PCONP.PCI2C1&amp;0x1" id="I2C1" location="0x4005C000"/> +<peripheralInstance derived_from="LPC17_I2C" determined="infoFile" enable="SYSCTL.PCONP.PCI2C2&amp;0x1" id="I2C2" location="0x400A0000"/> +<peripheralInstance derived_from="LPC17_DMA" determined="infoFile" enable="SYSCTL.PCONP.PCGPDMA&amp;0x1" id="DMA" location="0x50004000"/> +<peripheralInstance derived_from="LPC17_ENET" determined="infoFile" enable="SYSCTL.PCONP.PCENET&amp;0x1" id="ENET" location="0x50000000"/> +<peripheralInstance derived_from="CM3_DCR" determined="infoFile" id="DCR" location="0xE000EDF0"/> +<peripheralInstance derived_from="LPC17_PCB" determined="infoFile" id="PCB" location="0x4002c000"/> +<peripheralInstance derived_from="LPC17_QEI" determined="infoFile" enable="SYSCTL.PCONP.PCQEI&amp;0x1" id="QEI" location="0x400bc000"/> +<peripheralInstance derived_from="LPC17_USBHOST" determined="infoFile" enable="USBCLKCTL.USBClkSt&amp;0x11=0x11" id="USBHOST" location="0x5000C000"/> +<peripheralInstance derived_from="LPC17_USBOTG" determined="infoFile" enable="USBCLKCTL.USBClkSt&amp;0x1c=0x1c" id="USBOTG" location="0x5000C000"/> +<peripheralInstance derived_from="LPC17_RTC" determined="infoFile" enable="SYSCTL.PCONP.PCRTC&amp;0x1" id="RTC" location="0x40024000"/> +<peripheralInstance derived_from="MPU" determined="infoFile" id="MPU" location="0xE000ED90"/> +<peripheralInstance derived_from="LPC1x_WDT" determined="infoFile" id="WDT" location="0x40000000"/> +<peripheralInstance derived_from="LPC17_FLASHCFG" determined="infoFile" id="FLASHACCEL" location="0x400FC000"/> +<peripheralInstance derived_from="GPIO_INT" determined="infoFile" id="GPIOINTMAP" location="0x40028080"/> +<peripheralInstance derived_from="LPC17_CANAFR" determined="infoFile" enable="SYSCTL.PCONP.PCCAN1&amp;0x1|SYSCTL.PCONP.PCCAN2&amp;0x1" id="CANAFR" location="0x4003C000"/> +<peripheralInstance derived_from="LPC17_CANCEN" determined="infoFile" enable="SYSCTL.PCONP.PCCAN1&amp;0x1|SYSCTL.PCONP.PCCAN2&amp;0x1" id="CANCEN" location="0x40040000"/> +<peripheralInstance derived_from="LPC17_CANWAKESLEEP" determined="infoFile" id="CANWAKESLEEP" location="0x400FC110"/> +<peripheralInstance derived_from="LPC17_CANCON" determined="infoFile" enable="SYSCTL.PCONP.PCCAN1&amp;0x1" id="CANCON1" location="0x40044000"/> +<peripheralInstance derived_from="LPC17_CANCON" determined="infoFile" enable="SYSCTL.PCONP.PCCAN2&amp;0x1" id="CANCON2" location="0x40048000"/> +<peripheralInstance derived_from="LPC17_MCPWM" determined="infoFile" enable="SYSCTL.PCONP.PCMCPWM&amp;0x1" id="MCPWM" location="0x400B8000"/> +<peripheralInstance derived_from="LPC17_FMC" determined="infoFile" id="FMC" location="0x40084000"/> </chip> <processor><name gcc_name="cortex-m3">Cortex-M3</name> <family>Cortex-M</family> diff --git a/projects/example/test.udp/src/sketch.c b/projects/example/test.udp/src/sketch.c index 8b7d406..31e48ea 100644 --- a/projects/example/test.udp/src/sketch.c +++ b/projects/example/test.udp/src/sketch.c @@ -6,15 +6,17 @@ #include "NyLPC_net.h" #include "NyLPC_utils.h" +#include "../netif/mimicip/NyLPC_cMiMicIpNetIf.h" #include #include -NyLPC_TcNet_t net; +const struct NyLPC_TiNetInterface_Interface* netif; void setup(void) { //uipサービス初期化。いろいろ利用可能に。 - NyLPC_cNet_initialize(&net); + netif=NyLPC_cMiMicIpNetIf_getNetInterface(); + NyLPC_cNet_initialize(netif); } struct A{ diff --git a/projects/example/test.upnp/.cproject b/projects/example/test.upnp/.cproject index b0bcc99..fb86f7f 100644 --- a/projects/example/test.upnp/.cproject +++ b/projects/example/test.upnp/.cproject @@ -1,7 +1,5 @@ - - - + @@ -9,18 +7,18 @@ - - + + - + @@ -47,9 +45,9 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + - + @@ -522,7 +182,7 @@ @@ -889,8 +210,8 @@ <?xml version="1.0" encoding="UTF-8"?> <TargetConfig> -<Properties property_0="" property_3="NXP" property_4="LPC1768" property_count="5" version="1"/> -<infoList vendor="NXP"><info chip="LPC1768" match_id="0x00013f37,0x26013F37,0x26113F37" name="LPC1768" package="lpc17_lqfp100.xml"><chip><name>LPC1768</name> +<Properties property_0="" property_2="LPC175x_6x_512.cfx" property_3="NXP" property_4="LPC1768" property_count="5" version="70002"/> +<infoList vendor="NXP"><info chip="LPC1768" flash_driver="LPC175x_6x_512.cfx" match_id="0x00013f37,0x26013F37,0x26113F37" name="LPC1768" package="lpc17_lqfp100.xml" stub="crt_emu_cm3_nxp"><chip><name>LPC1768</name> <family>LPC17xx</family> <vendor>NXP (formerly Philips)</vendor> <reset board="None" core="Real" sys="Real"/> @@ -898,58 +219,59 @@ <memory can_program="true" id="Flash" is_ro="true" type="Flash"/> <memory id="RAM" type="RAM"/> <memory id="Periph" is_volatile="true" type="Peripheral"/> -<memoryInstance derived_from="Flash" id="MFlash512" location="0x0" size="0x80000"/> +<memoryInstance derived_from="Flash" id="MFlash512" location="0x00000000" size="0x80000"/> <memoryInstance derived_from="RAM" id="RamLoc32" location="0x10000000" size="0x8000"/> <memoryInstance derived_from="RAM" id="RamAHB32" location="0x2007c000" size="0x8000"/> <prog_flash blocksz="0x1000" location="0" maxprgbuff="0x1000" progwithcode="TRUE" size="0x10000"/> <prog_flash blocksz="0x8000" location="0x10000" maxprgbuff="0x1000" progwithcode="TRUE" size="0x70000"/> -<peripheralInstance derived_from="LPC17_NVIC" id="NVIC" location="0xE000E000"/> -<peripheralInstance derived_from="TIMER" enable="SYSCTL.PCONP.PCTIM0&amp;0x1" id="TIMER0" location="0x40004000"/> -<peripheralInstance derived_from="TIMER" enable="SYSCTL.PCONP.PCTIM1&amp;0x1" id="TIMER1" location="0x40008000"/> -<peripheralInstance derived_from="TIMER" enable="SYSCTL.PCONP.PCTIM2&amp;0x1" id="TIMER2" location="0x40090000"/> -<peripheralInstance derived_from="TIMER" enable="SYSCTL.PCONP.PCTIM3&amp;0x1" id="TIMER3" location="0x40094000"/> -<peripheralInstance derived_from="LPC17_RIT" enable="SYSCTL.PCONP.PCRIT&amp;0x1" id="RIT" location="0x400B0000"/> -<peripheralInstance derived_from="FGPIO" enable="SYSCTL.PCONP.PCGPIO&amp;0x1" id="GPIO0" location="0x2009C000"/> -<peripheralInstance derived_from="FGPIO" enable="SYSCTL.PCONP.PCGPIO&amp;0x1" id="GPIO1" location="0x2009C020"/> -<peripheralInstance derived_from="FGPIO" enable="SYSCTL.PCONP.PCGPIO&amp;0x1" id="GPIO2" location="0x2009C040"/> -<peripheralInstance derived_from="FGPIO" enable="SYSCTL.PCONP.PCGPIO&amp;0x1" id="GPIO3" location="0x2009C060"/> -<peripheralInstance derived_from="FGPIO" enable="SYSCTL.PCONP.PCGPIO&amp;0x1" id="GPIO4" location="0x2009C080"/> -<peripheralInstance derived_from="LPC17_I2S" enable="SYSCTL.PCONP&amp;0x08000000" id="I2S" location="0x400A8000"/> -<peripheralInstance derived_from="LPC17_SYSCTL" id="SYSCTL" location="0x400FC000"/> -<peripheralInstance derived_from="LPC17_DAC" enable="PCB.PINSEL1.P0_26&amp;0x2=2" id="DAC" location="0x4008C000"/> -<peripheralInstance derived_from="LPC17xx_UART" enable="SYSCTL.PCONP.PCUART0&amp;0x1" id="UART0" location="0x4000C000"/> -<peripheralInstance derived_from="LPC17xx_UART_MODEM" enable="SYSCTL.PCONP.PCUART1&amp;0x1" id="UART1" location="0x40010000"/> -<peripheralInstance derived_from="LPC17xx_UART" enable="SYSCTL.PCONP.PCUART2&amp;0x1" id="UART2" location="0x40098000"/> -<peripheralInstance derived_from="LPC17xx_UART" enable="SYSCTL.PCONP.PCUART3&amp;0x1" id="UART3" location="0x4009C000"/> -<peripheralInstance derived_from="SPI" enable="SYSCTL.PCONP.PCSPI&amp;0x1" id="SPI" location="0x40020000"/> -<peripheralInstance derived_from="LPC17_SSP" enable="SYSCTL.PCONP.PCSSP0&amp;0x1" id="SSP0" location="0x40088000"/> -<peripheralInstance derived_from="LPC17_SSP" enable="SYSCTL.PCONP.PCSSP1&amp;0x1" id="SSP1" location="0x40030000"/> -<peripheralInstance derived_from="LPC17_ADC" enable="SYSCTL.PCONP.PCAD&amp;0x1" id="ADC" location="0x40034000"/> -<peripheralInstance derived_from="LPC17_USBINTST" enable="USBCLKCTL.USBClkCtrl&amp;0x12" id="USBINTSTAT" location="0x400fc1c0"/> -<peripheralInstance derived_from="LPC17_USB_CLK_CTL" id="USBCLKCTL" location="0x5000cff4"/> -<peripheralInstance derived_from="LPC17_USBDEV" enable="USBCLKCTL.USBClkSt&amp;0x12=0x12" id="USBDEV" location="0x5000C200"/> -<peripheralInstance derived_from="LPC17_PWM" enable="SYSCTL.PCONP.PWM1&amp;0x1" id="PWM" location="0x40018000"/> -<peripheralInstance derived_from="LPC17_I2C" enable="SYSCTL.PCONP.PCI2C0&amp;0x1" id="I2C0" location="0x4001C000"/> -<peripheralInstance derived_from="LPC17_I2C" enable="SYSCTL.PCONP.PCI2C1&amp;0x1" id="I2C1" location="0x4005C000"/> -<peripheralInstance derived_from="LPC17_I2C" enable="SYSCTL.PCONP.PCI2C2&amp;0x1" id="I2C2" location="0x400A0000"/> -<peripheralInstance derived_from="LPC17_DMA" enable="SYSCTL.PCONP.PCGPDMA&amp;0x1" id="DMA" location="0x50004000"/> -<peripheralInstance derived_from="LPC17_ENET" enable="SYSCTL.PCONP.PCENET&amp;0x1" id="ENET" location="0x50000000"/> -<peripheralInstance derived_from="CM3_DCR" id="DCR" location="0xE000EDF0"/> -<peripheralInstance derived_from="LPC17_PCB" id="PCB" location="0x4002c000"/> -<peripheralInstance derived_from="LPC17_QEI" enable="SYSCTL.PCONP.PCQEI&amp;0x1" id="QEI" location="0x400bc000"/> -<peripheralInstance derived_from="LPC17_USBHOST" enable="USBCLKCTL.USBClkSt&amp;0x11=0x11" id="USBHOST" location="0x5000C000"/> -<peripheralInstance derived_from="LPC17_USBOTG" enable="USBCLKCTL.USBClkSt&amp;0x1c=0x1c" id="USBOTG" location="0x5000C000"/> -<peripheralInstance derived_from="LPC17_RTC" enable="SYSCTL.PCONP.PCRTC&amp;0x1" id="RTC" location="0x40024000"/> -<peripheralInstance derived_from="MPU" id="MPU" location="0xE000ED90"/> -<peripheralInstance derived_from="LPC1x_WDT" id="WDT" location="0x40000000"/> -<peripheralInstance derived_from="LPC17_FLASHCFG" id="FLASHACCEL" location="0x400FC000"/> -<peripheralInstance derived_from="GPIO_INT" id="GPIOINTMAP" location="0x40028080"/> -<peripheralInstance derived_from="LPC17_CANAFR" enable="SYSCTL.PCONP.PCCAN1&amp;0x1|SYSCTL.PCONP.PCCAN2&amp;0x1" id="CANAFR" location="0x4003C000"/> -<peripheralInstance derived_from="LPC17_CANCEN" enable="SYSCTL.PCONP.PCCAN1&amp;0x1|SYSCTL.PCONP.PCCAN2&amp;0x1" id="CANCEN" location="0x40040000"/> -<peripheralInstance derived_from="LPC17_CANWAKESLEEP" id="CANWAKESLEEP" location="0x400FC110"/> -<peripheralInstance derived_from="LPC17_CANCON" enable="SYSCTL.PCONP.PCCAN1&amp;0x1" id="CANCON1" location="0x40044000"/> -<peripheralInstance derived_from="LPC17_CANCON" enable="SYSCTL.PCONP.PCCAN2&amp;0x1" id="CANCON2" location="0x40048000"/> -<peripheralInstance derived_from="LPC17_MCPWM" enable="SYSCTL.PCONP.PCMCPWM&amp;0x1" id="MCPWM" location="0x400B8000"/> +<peripheralInstance derived_from="LPC17_NVIC" determined="infoFile" id="NVIC" location="0xE000E000"/> +<peripheralInstance derived_from="TIMER" determined="infoFile" enable="SYSCTL.PCONP.PCTIM0&amp;0x1" id="TIMER0" location="0x40004000"/> +<peripheralInstance derived_from="TIMER" determined="infoFile" enable="SYSCTL.PCONP.PCTIM1&amp;0x1" id="TIMER1" location="0x40008000"/> +<peripheralInstance derived_from="TIMER" determined="infoFile" enable="SYSCTL.PCONP.PCTIM2&amp;0x1" id="TIMER2" location="0x40090000"/> +<peripheralInstance derived_from="TIMER" determined="infoFile" enable="SYSCTL.PCONP.PCTIM3&amp;0x1" id="TIMER3" location="0x40094000"/> +<peripheralInstance derived_from="LPC17_RIT" determined="infoFile" enable="SYSCTL.PCONP.PCRIT&amp;0x1" id="RIT" location="0x400B0000"/> +<peripheralInstance derived_from="FGPIO" determined="infoFile" enable="SYSCTL.PCONP.PCGPIO&amp;0x1" id="GPIO0" location="0x2009C000"/> +<peripheralInstance derived_from="FGPIO" determined="infoFile" enable="SYSCTL.PCONP.PCGPIO&amp;0x1" id="GPIO1" location="0x2009C020"/> +<peripheralInstance derived_from="FGPIO" determined="infoFile" enable="SYSCTL.PCONP.PCGPIO&amp;0x1" id="GPIO2" location="0x2009C040"/> +<peripheralInstance derived_from="FGPIO" determined="infoFile" enable="SYSCTL.PCONP.PCGPIO&amp;0x1" id="GPIO3" location="0x2009C060"/> +<peripheralInstance derived_from="FGPIO" determined="infoFile" enable="SYSCTL.PCONP.PCGPIO&amp;0x1" id="GPIO4" location="0x2009C080"/> +<peripheralInstance derived_from="LPC17_I2S" determined="infoFile" enable="SYSCTL.PCONP&amp;0x08000000" id="I2S" location="0x400A8000"/> +<peripheralInstance derived_from="LPC17_SYSCTL" determined="infoFile" id="SYSCTL" location="0x400FC000"/> +<peripheralInstance derived_from="LPC17_DAC" determined="infoFile" enable="PCB.PINSEL1.P0_26&amp;0x2=2" id="DAC" location="0x4008C000"/> +<peripheralInstance derived_from="LPC17xx_UART" determined="infoFile" enable="SYSCTL.PCONP.PCUART0&amp;0x1" id="UART0" location="0x4000C000"/> +<peripheralInstance derived_from="LPC17xx_UART_MODEM" determined="infoFile" enable="SYSCTL.PCONP.PCUART1&amp;0x1" id="UART1" location="0x40010000"/> +<peripheralInstance derived_from="LPC17xx_UART" determined="infoFile" enable="SYSCTL.PCONP.PCUART2&amp;0x1" id="UART2" location="0x40098000"/> +<peripheralInstance derived_from="LPC17xx_UART" determined="infoFile" enable="SYSCTL.PCONP.PCUART3&amp;0x1" id="UART3" location="0x4009C000"/> +<peripheralInstance derived_from="SPI" determined="infoFile" enable="SYSCTL.PCONP.PCSPI&amp;0x1" id="SPI" location="0x40020000"/> +<peripheralInstance derived_from="LPC17_SSP" determined="infoFile" enable="SYSCTL.PCONP.PCSSP0&amp;0x1" id="SSP0" location="0x40088000"/> +<peripheralInstance derived_from="LPC17_SSP" determined="infoFile" enable="SYSCTL.PCONP.PCSSP1&amp;0x1" id="SSP1" location="0x40030000"/> +<peripheralInstance derived_from="LPC17_ADC" determined="infoFile" enable="SYSCTL.PCONP.PCAD&amp;0x1" id="ADC" location="0x40034000"/> +<peripheralInstance derived_from="LPC17_USBINTST" determined="infoFile" enable="USBCLKCTL.USBClkCtrl&amp;0x12" id="USBINTSTAT" location="0x400fc1c0"/> +<peripheralInstance derived_from="LPC17_USB_CLK_CTL" determined="infoFile" id="USBCLKCTL" location="0x5000cff4"/> +<peripheralInstance derived_from="LPC17_USBDEV" determined="infoFile" enable="USBCLKCTL.USBClkSt&amp;0x12=0x12" id="USBDEV" location="0x5000C200"/> +<peripheralInstance derived_from="LPC17_PWM" determined="infoFile" enable="SYSCTL.PCONP.PWM1&amp;0x1" id="PWM" location="0x40018000"/> +<peripheralInstance derived_from="LPC17_I2C" determined="infoFile" enable="SYSCTL.PCONP.PCI2C0&amp;0x1" id="I2C0" location="0x4001C000"/> +<peripheralInstance derived_from="LPC17_I2C" determined="infoFile" enable="SYSCTL.PCONP.PCI2C1&amp;0x1" id="I2C1" location="0x4005C000"/> +<peripheralInstance derived_from="LPC17_I2C" determined="infoFile" enable="SYSCTL.PCONP.PCI2C2&amp;0x1" id="I2C2" location="0x400A0000"/> +<peripheralInstance derived_from="LPC17_DMA" determined="infoFile" enable="SYSCTL.PCONP.PCGPDMA&amp;0x1" id="DMA" location="0x50004000"/> +<peripheralInstance derived_from="LPC17_ENET" determined="infoFile" enable="SYSCTL.PCONP.PCENET&amp;0x1" id="ENET" location="0x50000000"/> +<peripheralInstance derived_from="CM3_DCR" determined="infoFile" id="DCR" location="0xE000EDF0"/> +<peripheralInstance derived_from="LPC17_PCB" determined="infoFile" id="PCB" location="0x4002c000"/> +<peripheralInstance derived_from="LPC17_QEI" determined="infoFile" enable="SYSCTL.PCONP.PCQEI&amp;0x1" id="QEI" location="0x400bc000"/> +<peripheralInstance derived_from="LPC17_USBHOST" determined="infoFile" enable="USBCLKCTL.USBClkSt&amp;0x11=0x11" id="USBHOST" location="0x5000C000"/> +<peripheralInstance derived_from="LPC17_USBOTG" determined="infoFile" enable="USBCLKCTL.USBClkSt&amp;0x1c=0x1c" id="USBOTG" location="0x5000C000"/> +<peripheralInstance derived_from="LPC17_RTC" determined="infoFile" enable="SYSCTL.PCONP.PCRTC&amp;0x1" id="RTC" location="0x40024000"/> +<peripheralInstance derived_from="MPU" determined="infoFile" id="MPU" location="0xE000ED90"/> +<peripheralInstance derived_from="LPC1x_WDT" determined="infoFile" id="WDT" location="0x40000000"/> +<peripheralInstance derived_from="LPC17_FLASHCFG" determined="infoFile" id="FLASHACCEL" location="0x400FC000"/> +<peripheralInstance derived_from="GPIO_INT" determined="infoFile" id="GPIOINTMAP" location="0x40028080"/> +<peripheralInstance derived_from="LPC17_CANAFR" determined="infoFile" enable="SYSCTL.PCONP.PCCAN1&amp;0x1|SYSCTL.PCONP.PCCAN2&amp;0x1" id="CANAFR" location="0x4003C000"/> +<peripheralInstance derived_from="LPC17_CANCEN" determined="infoFile" enable="SYSCTL.PCONP.PCCAN1&amp;0x1|SYSCTL.PCONP.PCCAN2&amp;0x1" id="CANCEN" location="0x40040000"/> +<peripheralInstance derived_from="LPC17_CANWAKESLEEP" determined="infoFile" id="CANWAKESLEEP" location="0x400FC110"/> +<peripheralInstance derived_from="LPC17_CANCON" determined="infoFile" enable="SYSCTL.PCONP.PCCAN1&amp;0x1" id="CANCON1" location="0x40044000"/> +<peripheralInstance derived_from="LPC17_CANCON" determined="infoFile" enable="SYSCTL.PCONP.PCCAN2&amp;0x1" id="CANCON2" location="0x40048000"/> +<peripheralInstance derived_from="LPC17_MCPWM" determined="infoFile" enable="SYSCTL.PCONP.PCMCPWM&amp;0x1" id="MCPWM" location="0x400B8000"/> +<peripheralInstance derived_from="LPC17_FMC" determined="infoFile" id="FMC" location="0x40084000"/> </chip> <processor><name gcc_name="cortex-m3">Cortex-M3</name> <family>Cortex-M</family> diff --git a/projects/example/test.upnp/src/sketch.c b/projects/example/test.upnp/src/sketch.c index 524f1c9..5d71c29 100644 --- a/projects/example/test.upnp/src/sketch.c +++ b/projects/example/test.upnp/src/sketch.c @@ -6,7 +6,7 @@ #include "NyLPC_http.h" #include "NyLPC_utils.h" - +#include "../netif/mimicip/NyLPC_cMiMicIpNetIf.h" struct NyLPC_TUPnPDevDescIcon icon= { "image/png", @@ -42,7 +42,7 @@ const struct NyLPC_TUPnPDevDescDevice devdesc= -NyLPC_TcNet_t net; +const struct NyLPC_TiNetInterface_Interface* netif; struct MyHttpd{ NyLPC_TcHttpd_t super; }httpd; @@ -88,7 +88,8 @@ void setup(void) NyLPC_cNetConfig_setIpAddr(&c2,192,168,128,40); NyLPC_cNetConfig_setGateway(&c2,192,168,128,254); - NyLPC_cNet_initialize(&net); + netif=NyLPC_cMiMicIpNetIf_getNetInterface(); + NyLPC_cNet_initialize(netif); NyLPC_cHttpd_initialize(&httpd.super,c2.services.http_port); NyLPC_cUPnP_initialize(&upnp,c2.services.http_port,PATH_upnp,&devdesc); diff --git a/projects/example/test.websocket/.cproject b/projects/example/test.websocket/.cproject index 04303d2..a9bcf52 100644 --- a/projects/example/test.websocket/.cproject +++ b/projects/example/test.websocket/.cproject @@ -1,7 +1,5 @@ - - - + @@ -9,18 +7,18 @@ - - + + - + @@ -47,9 +45,9 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + - + @@ -522,7 +182,7 @@ @@ -889,8 +210,8 @@ <?xml version="1.0" encoding="UTF-8"?> <TargetConfig> -<Properties property_0="" property_3="NXP" property_4="LPC1768" property_count="5" version="1"/> -<infoList vendor="NXP"><info chip="LPC1768" match_id="0x00013f37,0x26013F37,0x26113F37" name="LPC1768" package="lpc17_lqfp100.xml"><chip><name>LPC1768</name> +<Properties property_0="" property_2="LPC175x_6x_512.cfx" property_3="NXP" property_4="LPC1768" property_count="5" version="70002"/> +<infoList vendor="NXP"><info chip="LPC1768" flash_driver="LPC175x_6x_512.cfx" match_id="0x00013f37,0x26013F37,0x26113F37" name="LPC1768" package="lpc17_lqfp100.xml" stub="crt_emu_cm3_nxp"><chip><name>LPC1768</name> <family>LPC17xx</family> <vendor>NXP (formerly Philips)</vendor> <reset board="None" core="Real" sys="Real"/> @@ -950,6 +271,7 @@ <peripheralInstance derived_from="LPC17_CANCON" determined="infoFile" enable="SYSCTL.PCONP.PCCAN1&amp;0x1" id="CANCON1" location="0x40044000"/> <peripheralInstance derived_from="LPC17_CANCON" determined="infoFile" enable="SYSCTL.PCONP.PCCAN2&amp;0x1" id="CANCON2" location="0x40048000"/> <peripheralInstance derived_from="LPC17_MCPWM" determined="infoFile" enable="SYSCTL.PCONP.PCMCPWM&amp;0x1" id="MCPWM" location="0x400B8000"/> +<peripheralInstance derived_from="LPC17_FMC" determined="infoFile" id="FMC" location="0x40084000"/> </chip> <processor><name gcc_name="cortex-m3">Cortex-M3</name> <family>Cortex-M</family> diff --git a/projects/example/test.websocket/src/sketch.c b/projects/example/test.websocket/src/sketch.c index a4bada1..d811897 100644 --- a/projects/example/test.websocket/src/sketch.c +++ b/projects/example/test.websocket/src/sketch.c @@ -7,9 +7,9 @@ #include "NyLPC_http.h" #include "NyLPC_utils.h" +#include "../netif/mimicip/NyLPC_cMiMicIpNetIf.h" - -NyLPC_TcNet_t net; +const struct NyLPC_TiNetInterface_Interface* netif; struct MyHttpd{ NyLPC_TcHttpd_t super; }httpd; @@ -50,7 +50,8 @@ void handler(NyLPC_TcHttpdConnection_t* i_connection) void setup(void) { //uipサービス初期化。いろいろ利用可能に。 - NyLPC_cNet_initialize(&net); + netif=NyLPC_cMiMicIpNetIf_getNetInterface(); + NyLPC_cNet_initialize(netif); NyLPC_cHttpd_initialize(&httpd.super,80); httpd.super.function.onRequest=handler; @@ -63,7 +64,7 @@ void loop(void) NyLPC_cNetConfig_initialize(&c2,NyLPC_TBool_TRUE); NyLPC_cNetConfig_setIpAddr(&c2,192,168,128,39); NyLPC_cNetConfig_setGateway(&c2,192,168,128,254); - NyLPC_cNet_start(&net,&c2); + NyLPC_cNet_start(&c2.super); NyLPC_cHttpd_loop(&httpd.super); for(;;); } diff --git a/projects/example/test/.cproject b/projects/example/test/.cproject index 161e604..e3f04f4 100644 --- a/projects/example/test/.cproject +++ b/projects/example/test/.cproject @@ -1,7 +1,5 @@ - - - + @@ -9,18 +7,18 @@ - - + + - + @@ -47,9 +45,9 @@ + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + - + @@ -741,12 +177,13 @@ @@ -1339,8 +211,8 @@ <?xml version="1.0" encoding="UTF-8"?> <TargetConfig> -<Properties property_0="" property_3="NXP" property_4="LPC1768" property_count="5" version="1"/> -<infoList vendor="NXP"><info chip="LPC1768" match_id="0x00013f37,0x26013F37,0x26113F37" name="LPC1768" package="lpc17_lqfp100.xml"><chip><name>LPC1768</name> +<Properties property_0="" property_2="LPC175x_6x_512.cfx" property_3="NXP" property_4="LPC1768" property_count="5" version="70002"/> +<infoList vendor="NXP"><info chip="LPC1768" flash_driver="LPC175x_6x_512.cfx" match_id="0x00013f37,0x26013F37,0x26113F37" name="LPC1768" package="lpc17_lqfp100.xml" stub="crt_emu_cm3_nxp"><chip><name>LPC1768</name> <family>LPC17xx</family> <vendor>NXP (formerly Philips)</vendor> <reset board="None" core="Real" sys="Real"/> @@ -1400,6 +272,7 @@ <peripheralInstance derived_from="LPC17_CANCON" determined="infoFile" enable="SYSCTL.PCONP.PCCAN1&amp;0x1" id="CANCON1" location="0x40044000"/> <peripheralInstance derived_from="LPC17_CANCON" determined="infoFile" enable="SYSCTL.PCONP.PCCAN2&amp;0x1" id="CANCON2" location="0x40048000"/> <peripheralInstance derived_from="LPC17_MCPWM" determined="infoFile" enable="SYSCTL.PCONP.PCMCPWM&amp;0x1" id="MCPWM" location="0x400B8000"/> +<peripheralInstance derived_from="LPC17_FMC" determined="infoFile" id="FMC" location="0x40084000"/> </chip> <processor><name gcc_name="cortex-m3">Cortex-M3</name> <family>Cortex-M</family> @@ -1409,4 +282,571 @@ </infoList> </TargetConfig> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/projects/example/test/src/sketch.c b/projects/example/test/src/sketch.c index b0db8d1..0b4ca5a 100644 --- a/projects/example/test/src/sketch.c +++ b/projects/example/test/src/sketch.c @@ -2,7 +2,11 @@ * スケッチシステムを使う場合の実装 */ #include "boot/sketch.h" -#include "NyLPC_uipService.h" +#include "../include/NyLPC_net.h" + +#include "../netif/mimicip/NyLPC_cMiMicIpNetIf.h" + +const struct NyLPC_TiNetInterface_Interface* netif; #include //イーサネット用の初期化情報 const struct NyLPC_TEthAddr ethaddr=NyLPC_TEthAddr_pack(0x00,0x01,0x02,0x03,0x04,0x05); @@ -16,7 +20,8 @@ void setup(void) { int i; //uipサービス初期化。いろいろ利用可能に。 - NyLPC_cUipService_initialize(); + netif=NyLPC_cMiMicIpNetIf_getNetInterface(); + NyLPC_cNet_initialize(netif); for(i=0;i<512;i++){ sockbuf[i]='A'; workbuf[i]='A'; @@ -29,8 +34,8 @@ void setup(void) void recv_loop(void) { NyLPC_TcIPv4Config_t config; - NyLPC_TcTcpListener_t listener; - NyLPC_TcTcpSocket_t socket; + NyLPC_TiTcpListener_t* listener; + NyLPC_TiTcpSocket_t* socket; const char *buf; NyLPC_TInt32 s,stotal; @@ -40,32 +45,31 @@ void recv_loop(void) NyLPC_cIPv4Config_setDefaultRoute(&config,&gateway); NyLPC_cIPv4Config_setIp(&config,&ipaddr,&netmask); - - NyLPC_cTcpListener_initialize(&listener,23); - NyLPC_cTcpSocket_initialize(&socket,sockbuf,512); - NyLPC_cUipService_start(&config); + listener=NyLPC_cNet_createTcpListenerEx(23); + socket=NyLPC_cNet_createTcpSocketEx(NyLPC_TSocketType_TCP_NORMAL); + NyLPC_cNet_start(&config); for(;;){ //リスニング - if(!NyLPC_cTcpListener_listen(&listener,&socket,10000)){ + if(!NyLPC_iTcpListener_listen(listener,socket,10000)){ continue; } //Accept - if(!NyLPC_cTcpSocket_accept(&socket,10000)){ - NyLPC_cTcpSocket_close(&socket,10000); + if(!NyLPC_iTcpSocket_accept(socket,10000)){ + NyLPC_iTcpSocket_close(socket,10000); continue; } //接続中は送信する。 stotal=0; - s=NyLPC_cTcpSocket_precv(&socket,(const void**)&buf,1000); + s=NyLPC_iTcpSocket_precv(socket,(const void**)&buf,1000); while(s>=0){ - NyLPC_cTcpSocket_pseek(&socket,s); + NyLPC_iTcpSocket_pseek(socket,s); stotal+=s; if(stotal>=1024*1024){ break; } - s=NyLPC_cTcpSocket_precv(&socket,(const void**)&buf,1000); + s=NyLPC_iTcpSocket_precv(socket,(const void**)&buf,1000); } - NyLPC_cTcpSocket_close(&socket,10000); + NyLPC_iTcpSocket_close(socket,10000); } } @@ -75,8 +79,8 @@ void recv_loop(void) void send_loop(void) { NyLPC_TcIPv4Config_t config; - NyLPC_TcTcpListener_t listener; - NyLPC_TcTcpSocket_t socket; + NyLPC_TiTcpListener_t* listener; + NyLPC_TiTcpSocket_t* socket; const char *rp; NyLPC_TInt32 s,r,stotal; @@ -87,30 +91,30 @@ void send_loop(void) NyLPC_cIPv4Config_setIp(&config,&ipaddr,&netmask); - NyLPC_cTcpListener_initialize(&listener,23); - NyLPC_cTcpSocket_initialize(&socket,sockbuf,512); - NyLPC_cUipService_start(&config); + listener=NyLPC_cNet_createTcpListenerEx(23); + socket=NyLPC_cNet_createTcpSocketEx(NyLPC_TSocketType_TCP_NORMAL); + NyLPC_cNet_start(&config); for(;;){ //リスニング - if(!NyLPC_cTcpListener_listen(&listener,&socket,10000)){ + if(!NyLPC_iTcpListener_listen(listener,socket,10000)){ continue; } //Accept - if(!NyLPC_cTcpSocket_accept(&socket,10000)){ - NyLPC_cTcpSocket_close(&socket,10000); + if(!NyLPC_iTcpSocket_accept(socket,10000)){ + NyLPC_iTcpSocket_close(socket,10000); continue; } //接続中は送信する。 stotal=0; - s=NyLPC_cTcpSocket_send(&socket,sockbuf,1024,30000); + s=NyLPC_iTcpSocket_send(socket,sockbuf,1024,30000); while(s>=0){ stotal+=s; - s=NyLPC_cTcpSocket_send(&socket,sockbuf,1024,30000); + s=NyLPC_iTcpSocket_send(socket,sockbuf,1024,30000); if(stotal>=10*1024*1024){ break; } } - NyLPC_cTcpSocket_close(&socket,10000); + NyLPC_iTcpSocket_close(socket,10000); } } @@ -119,8 +123,8 @@ void send_loop(void) void loopback(void) { NyLPC_TcIPv4Config_t config; - NyLPC_TcTcpListener_t listener; - NyLPC_TcTcpSocket_t socket; + NyLPC_TiTcpListener_t* listener; + NyLPC_TiTcpSocket_t* socket; const char *buf; NyLPC_TInt32 s,stotal; @@ -131,30 +135,30 @@ void loopback(void) NyLPC_cIPv4Config_setIp(&config,&ipaddr,&netmask); - NyLPC_cTcpListener_initialize(&listener,23); - NyLPC_cTcpSocket_initialize(&socket,sockbuf,1024); - NyLPC_cUipService_start(&config); + listener=NyLPC_cNet_createTcpListenerEx(23); + socket=NyLPC_cNet_createTcpSocketEx(NyLPC_TSocketType_TCP_NORMAL); + NyLPC_cNet_start(&config); for(;;){ //リスニング - if(!NyLPC_cTcpListener_listen(&listener,&socket,10000)){ + if(!NyLPC_iTcpListener_listen(listener,socket,10000)){ continue; } //Accept - if(!NyLPC_cTcpSocket_accept(&socket,10000)){ - NyLPC_cTcpSocket_close(&socket,10000); + if(!NyLPC_iTcpSocket_accept(socket,10000)){ + NyLPC_iTcpSocket_close(socket,10000); continue; } //接続中は送信する。 - s=NyLPC_cTcpSocket_precv(&socket,(const void**)&buf,1000); + s=NyLPC_iTcpSocket_precv(socket,(const void**)&buf,1000); while(s>=0){ //memcpy(workbuf,buf,s); if(s>0){ - NyLPC_cTcpSocket_pseek(&socket,s); - NyLPC_cTcpSocket_send(&socket,workbuf,s,30000); + NyLPC_iTcpSocket_pseek(socket,s); + NyLPC_iTcpSocket_send(socket,workbuf,s,30000); } - s=NyLPC_cTcpSocket_precv(&socket,(const void**)&buf,1000); + s=NyLPC_iTcpSocket_precv(socket,(const void**)&buf,1000); } - NyLPC_cTcpSocket_close(&socket,10000); + NyLPC_iTcpSocket_close(socket,10000); } } diff --git a/projects/sketch/.cproject b/projects/sketch/.cproject index eb354e2..7219eaa 100644 --- a/projects/sketch/.cproject +++ b/projects/sketch/.cproject @@ -1,7 +1,5 @@ - - - + @@ -9,18 +7,18 @@ - - + + - + @@ -47,9 +45,9 @@ + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + - + @@ -741,12 +176,13 @@ @@ -1339,11 +210,8 @@ <?xml version="1.0" encoding="UTF-8"?> <TargetConfig> -<Properties property_0="" property_1="" property_2="" property_3="NXP" property_4="LPC1768" property_count="5" version="1"/> -<infoList vendor="NXP"> -<info chip="LPC1768" match_id="0x00013f37,0x26013F37,0x26113F37" name="LPC1768" package="lpc17_lqfp100.xml"> -<chip> -<name>LPC1768</name> +<Properties property_0="" property_2="LPC175x_6x_512.cfx" property_3="NXP" property_4="LPC1768" property_count="5" version="70002"/> +<infoList vendor="NXP"><info chip="LPC1768" flash_driver="LPC175x_6x_512.cfx" match_id="0x00013f37,0x26013F37,0x26113F37" name="LPC1768" package="lpc17_lqfp100.xml" stub="crt_emu_cm3_nxp"><chip><name>LPC1768</name> <family>LPC17xx</family> <vendor>NXP (formerly Philips)</vendor> <reset board="None" core="Real" sys="Real"/> @@ -1402,9 +270,10 @@ <peripheralInstance derived_from="LPC17_CANWAKESLEEP" determined="infoFile" id="CANWAKESLEEP" location="0x400FC110"/> <peripheralInstance derived_from="LPC17_CANCON" determined="infoFile" enable="SYSCTL.PCONP.PCCAN1&amp;0x1" id="CANCON1" location="0x40044000"/> <peripheralInstance derived_from="LPC17_CANCON" determined="infoFile" enable="SYSCTL.PCONP.PCCAN2&amp;0x1" id="CANCON2" location="0x40048000"/> +<peripheralInstance derived_from="LPC17_MCPWM" determined="infoFile" enable="SYSCTL.PCONP.PCMCPWM&amp;0x1" id="MCPWM" location="0x400B8000"/> +<peripheralInstance derived_from="LPC17_FMC" determined="infoFile" id="FMC" location="0x40084000"/> </chip> -<processor> -<name gcc_name="cortex-m3">Cortex-M3</name> +<processor><name gcc_name="cortex-m3">Cortex-M3</name> <family>Cortex-M</family> </processor> <link href="nxp_lpcxxxx_peripheral.xme" show="embed" type="simple"/> @@ -1412,4 +281,571 @@ </infoList> </TargetConfig> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/projects/standard/.cproject b/projects/standard/.cproject index 22188bc..8d1e27c 100644 --- a/projects/standard/.cproject +++ b/projects/standard/.cproject @@ -1,7 +1,5 @@ - - - + @@ -9,18 +7,18 @@ - - + + - + @@ -47,9 +45,9 @@ + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + - + @@ -741,12 +176,13 @@ @@ -1339,11 +210,8 @@ <?xml version="1.0" encoding="UTF-8"?> <TargetConfig> -<Properties property_0="" property_1="" property_2="" property_3="NXP" property_4="LPC1768" property_count="5" version="1"/> -<infoList vendor="NXP"> -<info chip="LPC1768" match_id="0x00013f37,0x26013F37,0x26113F37" name="LPC1768" package="lpc17_lqfp100.xml"> -<chip> -<name>LPC1768</name> +<Properties property_0="" property_2="LPC175x_6x_512.cfx" property_3="NXP" property_4="LPC1768" property_count="5" version="70002"/> +<infoList vendor="NXP"><info chip="LPC1768" flash_driver="LPC175x_6x_512.cfx" match_id="0x00013f37,0x26013F37,0x26113F37" name="LPC1768" package="lpc17_lqfp100.xml" stub="crt_emu_cm3_nxp"><chip><name>LPC1768</name> <family>LPC17xx</family> <vendor>NXP (formerly Philips)</vendor> <reset board="None" core="Real" sys="Real"/> @@ -1402,9 +270,10 @@ <peripheralInstance derived_from="LPC17_CANWAKESLEEP" determined="infoFile" id="CANWAKESLEEP" location="0x400FC110"/> <peripheralInstance derived_from="LPC17_CANCON" determined="infoFile" enable="SYSCTL.PCONP.PCCAN1&amp;0x1" id="CANCON1" location="0x40044000"/> <peripheralInstance derived_from="LPC17_CANCON" determined="infoFile" enable="SYSCTL.PCONP.PCCAN2&amp;0x1" id="CANCON2" location="0x40048000"/> +<peripheralInstance derived_from="LPC17_MCPWM" determined="infoFile" enable="SYSCTL.PCONP.PCMCPWM&amp;0x1" id="MCPWM" location="0x400B8000"/> +<peripheralInstance derived_from="LPC17_FMC" determined="infoFile" id="FMC" location="0x40084000"/> </chip> -<processor> -<name gcc_name="cortex-m3">Cortex-M3</name> +<processor><name gcc_name="cortex-m3">Cortex-M3</name> <family>Cortex-M</family> </processor> <link href="nxp_lpcxxxx_peripheral.xme" show="embed" type="simple"/> @@ -1412,4 +281,571 @@ </infoList> </TargetConfig> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + -- 2.11.0