This is Webservice SDK for mbed. LPCXpresso1769/LPC1768/FRDM-K64F/LPC4088

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

libMiMic(MiMic library for mbed)は、WebService機能を提供するSDKです。 mbedでWebAPIに対応したネットワークデバイスを簡単に作ることが出来ます。

libMiMicはMiMic projectで開発しています。MiMic projectについてはこちらをご覧ください。 http://nyatla.jp/mimic/wp/

構成

libMiMicはmbedRTOS上で動作し、ユーザアプリケーションにAPIを提供します。コアAPIはC言語で記述されていますが、使用頻度の高いものについてはmbed向けのC++APIが準備されています。

/media/uploads/nyatla/libmimic-sdk.png

※libMiMicはmbedの標準イーサネットドライバをしようしていません。

標準イーサネットドライバと同時に使用することはできません。

  • MiMicIP - IPv4スタックです。レテンシとメモリ消費量を抑えたuipベースのライブラリです。
  • ARP/ICMP/UDP/TCP - 基礎的なソケットAPIを提供します。APIは独自です。
  • HTTP/1.1 Server - HTTP/1.1に対応したサーバです。マルチセッション・Chunked・持続性接続に対応しています。
  • HTTP Modules - HTTP/1.1の機能モジュールです。以下のモジュールがあります。
    • ROM file provider - ROMに格納したファイルイメージを公開します。
    • File system provider - mbedファイルシステムを公開します。
    • Onchip configuration - プログラムフラッシュを利用して設定を保存します。
    • MiMicVM processor - RPCリクエスト(MiMicVM)を処理します。
    • FileUpload - ファイルアップロードを受け取ります。
    • URL decoder - HTTPリクエストを解析します。
    • UPnP handler -UPnPメッセージを処理します。
    • WebSocket - Websocketサーバです。
  • mDNS - マルチキャストDNSサービスです。
  • UPnP - UPnP/1.0の機能を提供します。UPnP handlerと協調して動作します。(現在はデバイス探索(SSDP)・デスクリプション(Description)のみ実装してあります。)
  • DHCP/APIPA - ゼロコンフィギュレーション用のモジュールです。
  • HTTP/1.1 Client
  • mbed C++ class library - mbed向けのC++CPIです。C言語のものより簡単です。

対応機種

  • mbed(mbed LPC1768)
  • LPCXpresso1769

プログラム

Import programMiMicRemoteMCU-for-Mbed

MiMic RemoteMCU for mbed. This program provides MCU control API over REST API. It can control MCU from Javascript,PHP or any HTTP rest client directly. And, The application has self development environment.

Import programMbedFileServer

The program publishes files at local directory and SD filesystem. It is a full-fledged webServer somewhat.

サンプル

Import programMiMicSimpleHttpd

This is a simplest HTTP server made ​​of libMiMic. It will echo back a request path.

Import programUPnPBasicDevice

Simplest UPnP basic device example. This program to run UPnP basic device on the mbed.

Import programWebSocketSample

MiMicSDK Websocket module sample program.

Import programHttpClientSamlpe

A http client sample program.

Import programTcpSocketClientSamlpe

MiMicSDK Tcp client socket sample program.

Import programUdpSocketSamlpe

Udp socket sample program. This program will send back the received packet.

チュートリアル

English

libMiMic(MiMic library for mbed) is SDK which provides Webservice functions. It can be created networking device easily using mbed.

See more MiMic information, See MiMic project website. http://nyatla.jp/mimic/wp/

Structure

libMiMic run on mbed RTOS and provides networking API to user application. This library has C++ class API for the mbed, and low-level C language API.

/media/uploads/nyatla/libmimic-sdk.png

For WebService general, it can be written in a simple C + + API.

libMiMic does not have the standard Ethernet driver of mbed. It is not possible that will be used with the standard Ethernet driver.

  • MiMicIP - IPv4 protocol stack. This is based uip which is reduced memory and latency.
  • ARP / ICMP / UDP / TCP - Those are provide basic IP protocols.
  • HTTP/1.1 Server - The Http server compatible HTTP/1.1. It supports multi-session, chunked transport, persistent connection.
  • HTTP Modules - There are addon-module for HTTP server. The following modules.
    • ROM file module - Publish the file images in ROM.
    • File system module - Publish thefiles in mbed file system.
    • Onchip configuration module - To save the network settings to the program flash via REST.
    • MiMicVM module - To handle the (MiMicVM) RPC request.
    • FileUpload module - Accept a file via HTTP POST.
    • URL dedoce module - A versatility URL decoder.
    • UPnP handle module - To handle UPnP messages.
    • UPnP - This provides UPnP/1.0 device functions. It works together with UPnP handler.
    • Websocket - websocket (version13) server
  • mDNS Service - DNS-SD protocol server.
  • UPnP - This provides UPnP/1.0 device functions which works with UPnP handler. (You have been implemented (SSDP) ? description only (Description) device search now.) It is a module zero configuration for - DHCP / APIPA. mbed C + + class library - C of mbed for + + is the CPI. It is simple than that of the C language.
  • DHCP/APIPA - It support zero-cpnfigulation.
  • mbed C++ class library. Almost APIs for Web applications are available.
  • HTTP/1.1 Client

Supported target

  • mbed(mbed LPC1768)
  • LPCXpresso1769

Application

Import programMiMicRemoteMCU-for-Mbed

MiMic RemoteMCU for mbed. This program provides MCU control API over REST API. It can control MCU from Javascript,PHP or any HTTP rest client directly. And, The application has self development environment.

Import programMbedFileServer

The program publishes files at local directory and SD filesystem. It is a full-fledged webServer somewhat.

Sample

Import programMiMicSimpleHttpd

This is a simplest HTTP server made ​​of libMiMic. It will echo back a request path.

Import programUPnPBasicDevice

Simplest UPnP basic device example. This program to run UPnP basic device on the mbed.

Import programWebSocketSample

MiMicSDK Websocket module sample program.

Import programHttpClientSamlpe

A http client sample program.

Import programTcpSocketClientSamlpe

MiMicSDK Tcp client socket sample program.

Import programUdpSocketSamlpe

Udp socket sample program. This program will send back the received packet.

Tutorial

Committer:
nyatla
Date:
Thu May 29 14:29:15 2014 +0000
Revision:
69:8c5f220441f5
Parent:
48:00d211aac2ec
r354????; LPC4088?????

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nyatla 37:fc4b4fd6a649 1 /*********************************************************************************
nyatla 37:fc4b4fd6a649 2 * PROJECT: MiMic
nyatla 37:fc4b4fd6a649 3 * --------------------------------------------------------------------------------
nyatla 37:fc4b4fd6a649 4 *
nyatla 37:fc4b4fd6a649 5 * This file is part of MiMic
nyatla 37:fc4b4fd6a649 6 * Copyright (C)2011 Ryo Iizuka
nyatla 37:fc4b4fd6a649 7 *
nyatla 37:fc4b4fd6a649 8 * MiMic is free software: you can redistribute it and/or modify
nyatla 37:fc4b4fd6a649 9 * it under the terms of the GNU Lesser General Public License as published
nyatla 37:fc4b4fd6a649 10 * by the Free Software Foundation, either version 3 of the License, or
nyatla 37:fc4b4fd6a649 11 * (at your option) any later version.
nyatla 37:fc4b4fd6a649 12 *
nyatla 37:fc4b4fd6a649 13 * This program is distributed in the hope that it will be useful,
nyatla 37:fc4b4fd6a649 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nyatla 37:fc4b4fd6a649 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
nyatla 37:fc4b4fd6a649 16 * GNU General Public License for more details.
nyatla 37:fc4b4fd6a649 17 *
nyatla 37:fc4b4fd6a649 18 * You should have received a copy of the GNU Lesser General Public License
nyatla 37:fc4b4fd6a649 19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
nyatla 37:fc4b4fd6a649 20 *
nyatla 37:fc4b4fd6a649 21 * For further information please contact.
nyatla 37:fc4b4fd6a649 22 * http://nyatla.jp/
nyatla 37:fc4b4fd6a649 23 * <airmail(at)ebony.plala.or.jp> or <nyatla(at)nyatla.jp>
nyatla 37:fc4b4fd6a649 24 *
nyatla 37:fc4b4fd6a649 25 *
nyatla 37:fc4b4fd6a649 26 * Parts of this file were leveraged from uIP:
nyatla 37:fc4b4fd6a649 27 *
nyatla 37:fc4b4fd6a649 28 * Copyright (c) 2001-2003, Adam Dunkels.
nyatla 37:fc4b4fd6a649 29 * All rights reserved.
nyatla 37:fc4b4fd6a649 30 *
nyatla 37:fc4b4fd6a649 31 * Redistribution and use in source and binary forms, with or without
nyatla 37:fc4b4fd6a649 32 * modification, are permitted provided that the following conditions
nyatla 37:fc4b4fd6a649 33 * are met:
nyatla 37:fc4b4fd6a649 34 * 1. Redistributions of source code must retain the above copyright
nyatla 37:fc4b4fd6a649 35 * notice, this list of conditions and the following disclaimer.
nyatla 37:fc4b4fd6a649 36 * 2. Redistributions in binary form must reproduce the above copyright
nyatla 37:fc4b4fd6a649 37 * notice, this list of conditions and the following disclaimer in the
nyatla 37:fc4b4fd6a649 38 * documentation and/or other materials provided with the distribution.
nyatla 37:fc4b4fd6a649 39 * 3. The name of the author may not be used to endorse or promote
nyatla 37:fc4b4fd6a649 40 * products derived from this software without specific prior
nyatla 37:fc4b4fd6a649 41 * written permission.
nyatla 37:fc4b4fd6a649 42 *
nyatla 37:fc4b4fd6a649 43 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
nyatla 37:fc4b4fd6a649 44 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
nyatla 37:fc4b4fd6a649 45 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
nyatla 37:fc4b4fd6a649 46 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
nyatla 37:fc4b4fd6a649 47 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
nyatla 37:fc4b4fd6a649 48 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
nyatla 37:fc4b4fd6a649 49 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
nyatla 37:fc4b4fd6a649 50 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
nyatla 37:fc4b4fd6a649 51 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
nyatla 37:fc4b4fd6a649 52 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
nyatla 37:fc4b4fd6a649 53 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
nyatla 37:fc4b4fd6a649 54 */
nyatla 37:fc4b4fd6a649 55 #include "NyLPC_cUdpSocket_protected.h"
nyatla 37:fc4b4fd6a649 56 #include "NyLPC_cIPv4Payload_protected.h"
nyatla 37:fc4b4fd6a649 57 #include "NyLPC_cUipService_protected.h"
nyatla 37:fc4b4fd6a649 58
nyatla 37:fc4b4fd6a649 59 /**
nyatla 37:fc4b4fd6a649 60 * フラグ値
nyatla 37:fc4b4fd6a649 61 */
nyatla 48:00d211aac2ec 62 #define NyLPC_cUdpSocket_FLAG_BROADCAST 0
nyatla 37:fc4b4fd6a649 63
nyatla 37:fc4b4fd6a649 64 #define lockResource(i_inst) NyLPC_cMutex_lock(((i_inst)->_smutex))
nyatla 37:fc4b4fd6a649 65 #define unlockResource(i_inst) NyLPC_cMutex_unlock(((i_inst)->_smutex))
nyatla 37:fc4b4fd6a649 66
nyatla 37:fc4b4fd6a649 67 #define SIZE_OF_IPv4_UDPIP_HEADER 28
nyatla 37:fc4b4fd6a649 68
nyatla 69:8c5f220441f5 69
nyatla 69:8c5f220441f5 70
nyatla 69:8c5f220441f5 71
nyatla 69:8c5f220441f5 72 /**
nyatla 69:8c5f220441f5 73 * IP+UDPヘッダサイズを0x05*4+8バイトとして、UDPの送信バッファをセットします。
nyatla 69:8c5f220441f5 74 */
nyatla 69:8c5f220441f5 75 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)
nyatla 69:8c5f220441f5 76 {
nyatla 69:8c5f220441f5 77 struct NyLPC_TIPv4Header* header=(struct NyLPC_TIPv4Header*)i_buf;
nyatla 69:8c5f220441f5 78 struct NyLPC_TUdpHeader* udp =(struct NyLPC_TUdpHeader*)(((NyLPC_TUInt8*)i_buf)+i_iph_word*4);
nyatla 69:8c5f220441f5 79
nyatla 69:8c5f220441f5 80 header->vhl=0x40|(0x0f&i_iph_word);
nyatla 69:8c5f220441f5 81 header->len16=NyLPC_htons(i_payload_size+(i_iph_word*4+8));
nyatla 69:8c5f220441f5 82 udp->udplen=NyLPC_htons(i_payload_size+(8));
nyatla 69:8c5f220441f5 83 //IPv4のTxヘッダを書き込む。
nyatla 69:8c5f220441f5 84 header->destipaddr=*i_dest_ip;
nyatla 69:8c5f220441f5 85 header->srcipaddr =i_inst->uip_udp_conn.lipaddr;
nyatla 69:8c5f220441f5 86
nyatla 69:8c5f220441f5 87 NyLPC_TIPv4Header_writeTxIpHeader(header,UIP_PROTO_UDP);
nyatla 69:8c5f220441f5 88
nyatla 69:8c5f220441f5 89 //UDPのTxヘッダを書き込む
nyatla 69:8c5f220441f5 90 //sorce & destination port
nyatla 69:8c5f220441f5 91 udp->srcport = i_inst->uip_udp_conn.lport;
nyatla 69:8c5f220441f5 92 udp->destport = NyLPC_htons(i_dest_port);
nyatla 69:8c5f220441f5 93 udp->udpchksum= 0;
nyatla 69:8c5f220441f5 94
nyatla 69:8c5f220441f5 95 udp->udpchksum=~(NyLPC_TIPv4Header_makeTcpChecksum(header));
nyatla 69:8c5f220441f5 96 header->ipchksum = ~(NyLPC_TIPv4Header_makeIpChecksum(header));
nyatla 69:8c5f220441f5 97 }
nyatla 69:8c5f220441f5 98
nyatla 69:8c5f220441f5 99
nyatla 69:8c5f220441f5 100
nyatla 69:8c5f220441f5 101
nyatla 37:fc4b4fd6a649 102 NyLPC_TBool NyLPC_cUdpSocket_initialize(NyLPC_TcUdpSocket_t* i_inst,NyLPC_TUInt16 i_port,void* i_rbuf,NyLPC_TUInt16 i_rbuf_len)
nyatla 37:fc4b4fd6a649 103 {
nyatla 37:fc4b4fd6a649 104 NyLPC_TcUipService_t* srv=_NyLPC_TcUipService_inst;
nyatla 37:fc4b4fd6a649 105 NyLPC_cBaseSocket_initialize(&(i_inst->_super),NyLPC_TcBaseSocket_TYPEID_UDP_SOCK);
nyatla 37:fc4b4fd6a649 106 //uipサービスは初期化済であること。
nyatla 37:fc4b4fd6a649 107 NyLPC_Assert(NyLPC_TcUipService_isInitService());
nyatla 37:fc4b4fd6a649 108 i_inst->_smutex=NyLPC_cIPv4_getSockMutex(&(srv->_tcpv4));
nyatla 37:fc4b4fd6a649 109 i_inst->uip_udp_conn.lport=NyLPC_htons(i_port);
nyatla 37:fc4b4fd6a649 110 i_inst->uip_udp_conn.mcastaddr=NyLPC_TIPv4Addr_ZERO;
nyatla 37:fc4b4fd6a649 111 i_inst->uip_udp_conn.flags=0x00;
nyatla 37:fc4b4fd6a649 112 i_inst->as_handler.rx=NULL;
nyatla 37:fc4b4fd6a649 113 i_inst->as_handler.periodic=NULL;
nyatla 37:fc4b4fd6a649 114
nyatla 37:fc4b4fd6a649 115 NyLPC_cFifoBuffer_initialize(&(i_inst->rxbuf),i_rbuf,i_rbuf_len);
nyatla 37:fc4b4fd6a649 116 //管理リストへ登録。
nyatla 37:fc4b4fd6a649 117 return NyLPC_cIPv4_addSocket(&(srv->_tcpv4),&(i_inst->_super));
nyatla 37:fc4b4fd6a649 118 }
nyatla 37:fc4b4fd6a649 119
nyatla 37:fc4b4fd6a649 120 void NyLPC_cUdpSocket_finalize(NyLPC_TcUdpSocket_t* i_inst)
nyatla 37:fc4b4fd6a649 121 {
nyatla 37:fc4b4fd6a649 122 NyLPC_TcUipService_t* srv=_NyLPC_TcUipService_inst;
nyatla 37:fc4b4fd6a649 123 NyLPC_Assert(NyLPC_TcUipService_isInitService());
nyatla 37:fc4b4fd6a649 124 //uipサービスは初期化済であること。
nyatla 37:fc4b4fd6a649 125 if(!NyLPC_cIPv4_removeSocket(&(srv->_tcpv4),&(i_inst->_super))){
nyatla 37:fc4b4fd6a649 126 //削除失敗、それは死を意味する。
nyatla 37:fc4b4fd6a649 127 NyLPC_Abort();
nyatla 37:fc4b4fd6a649 128 }
nyatla 37:fc4b4fd6a649 129 NyLPC_cFifoBuffer_finalize(&(i_inst->rxbuf));
nyatla 37:fc4b4fd6a649 130 NyLPC_cBaseSocket_finalize(&(i_inst->_super));
nyatla 37:fc4b4fd6a649 131 return;
nyatla 37:fc4b4fd6a649 132 }
nyatla 37:fc4b4fd6a649 133 void NyLPC_cUdpSocket_joinMulticast(NyLPC_TcUdpSocket_t* i_inst,const struct NyLPC_TIPv4Addr* i_addr)
nyatla 37:fc4b4fd6a649 134 {
nyatla 37:fc4b4fd6a649 135 i_inst->uip_udp_conn.mcastaddr=*i_addr;
nyatla 37:fc4b4fd6a649 136 }
nyatla 37:fc4b4fd6a649 137 void NyLPC_cUdpSocket_setBroadcast(NyLPC_TcUdpSocket_t* i_inst)
nyatla 37:fc4b4fd6a649 138 {
nyatla 48:00d211aac2ec 139 NyLPC_TUInt8_setBit(i_inst->uip_udp_conn.flags,NyLPC_cUdpSocket_FLAG_BROADCAST);
nyatla 37:fc4b4fd6a649 140 }
nyatla 37:fc4b4fd6a649 141
nyatla 37:fc4b4fd6a649 142
nyatla 37:fc4b4fd6a649 143
nyatla 37:fc4b4fd6a649 144 /**
nyatla 37:fc4b4fd6a649 145 * この関数は、rxパケットを処理して、ソケットの状態を更新します。
nyatla 37:fc4b4fd6a649 146 * uipサービスタスクが実行する関数です。
nyatla 37:fc4b4fd6a649 147 * この関数はNyLPC_cTcpSocket_periodicと排他実行すること。
nyatla 37:fc4b4fd6a649 148 */
nyatla 37:fc4b4fd6a649 149 NyLPC_TBool NyLPC_cUdpSocket_parseRx(
nyatla 37:fc4b4fd6a649 150 NyLPC_TcUdpSocket_t* i_inst,
nyatla 69:8c5f220441f5 151 const NyLPC_TcIPv4Payload_t* i_ipp)
nyatla 37:fc4b4fd6a649 152 {
nyatla 37:fc4b4fd6a649 153 NyLPC_TUInt16 tmp16;
nyatla 37:fc4b4fd6a649 154 struct NyLPC_TIPv4RxInfo dheader;
nyatla 69:8c5f220441f5 155 const void* data_offset;
nyatla 37:fc4b4fd6a649 156 //ブロードキャストの場合、フラグを確認
nyatla 69:8c5f220441f5 157 if(NyLPC_TIPv4Addr_isEqual(&(i_ipp->header->destipaddr),&NyLPC_TIPv4Addr_BROADCAST)){
nyatla 37:fc4b4fd6a649 158 if(!NyLPC_TUInt8_isBitOn(i_inst->uip_udp_conn.flags,NyLPC_cUdpSocket_FLAG_BROADCAST)){
nyatla 37:fc4b4fd6a649 159 goto DROP;
nyatla 37:fc4b4fd6a649 160 }
nyatla 37:fc4b4fd6a649 161 }
nyatla 37:fc4b4fd6a649 162 //パラメータの計算
nyatla 69:8c5f220441f5 163 tmp16=NyLPC_TUdpHeader_getHeaderLength(i_ipp->payload.tcp);
nyatla 37:fc4b4fd6a649 164 //UDPペイロードの長さは、IPパケットの長さ-(IPヘッダ+UDPヘッダ)
nyatla 69:8c5f220441f5 165 dheader.size=NyLPC_TIPv4Header_getPacketLength(i_ipp->header)-NyLPC_TIPv4Header_getHeaderLength(i_ipp->header)-tmp16;
nyatla 69:8c5f220441f5 166 dheader.peer_ip=i_ipp->header->srcipaddr;
nyatla 69:8c5f220441f5 167 dheader.peer_port=NyLPC_ntohs(i_ipp->payload.udp->srcport);
nyatla 69:8c5f220441f5 168 dheader.ip=i_ipp->header->destipaddr;
nyatla 69:8c5f220441f5 169 dheader.port=NyLPC_ntohs(i_ipp->payload.udp->destport);
nyatla 37:fc4b4fd6a649 170 if(i_inst->as_handler.rx!=NULL){
nyatla 69:8c5f220441f5 171 if(!i_inst->as_handler.rx(i_inst,i_ipp->payload.rawbuf+tmp16,&dheader)){
nyatla 37:fc4b4fd6a649 172 return NyLPC_TBool_FALSE;//UDPはReturnパケットなし
nyatla 37:fc4b4fd6a649 173 }
nyatla 37:fc4b4fd6a649 174 }
nyatla 37:fc4b4fd6a649 175 //TCPデータオフセット
nyatla 69:8c5f220441f5 176 data_offset=i_ipp->payload.rawbuf+tmp16;
nyatla 37:fc4b4fd6a649 177
nyatla 37:fc4b4fd6a649 178 //インスタンスをロックする。
nyatla 37:fc4b4fd6a649 179 lockResource(i_inst);
nyatla 37:fc4b4fd6a649 180 //受信キューへ追加(データ構造はsize[2]+data[n]).sizeに16ビットの受信サイズ,後続にデータ
nyatla 37:fc4b4fd6a649 181
nyatla 37:fc4b4fd6a649 182 //受信データサイズを確認
nyatla 37:fc4b4fd6a649 183 if(NyLPC_cFifoBuffer_getSpace(&(i_inst->rxbuf))<dheader.size+sizeof(struct NyLPC_TIPv4RxInfo)){
nyatla 37:fc4b4fd6a649 184 goto DROP;
nyatla 37:fc4b4fd6a649 185 }
nyatla 37:fc4b4fd6a649 186 //バッファに格納可能なら、格納。
nyatla 37:fc4b4fd6a649 187 NyLPC_cFifoBuffer_push(&(i_inst->rxbuf),&dheader,sizeof(struct NyLPC_TIPv4RxInfo));
nyatla 37:fc4b4fd6a649 188 NyLPC_cFifoBuffer_push(&(i_inst->rxbuf),data_offset,dheader.size);
nyatla 37:fc4b4fd6a649 189 unlockResource(i_inst);
nyatla 37:fc4b4fd6a649 190 return NyLPC_TBool_FALSE;//UDPはReturnパケットなし
nyatla 37:fc4b4fd6a649 191 DROP:
nyatla 37:fc4b4fd6a649 192 unlockResource(i_inst);
nyatla 37:fc4b4fd6a649 193 return NyLPC_TBool_FALSE;
nyatla 37:fc4b4fd6a649 194 }
nyatla 37:fc4b4fd6a649 195
nyatla 37:fc4b4fd6a649 196
nyatla 37:fc4b4fd6a649 197
nyatla 37:fc4b4fd6a649 198 /**
nyatla 37:fc4b4fd6a649 199 * see Header file
nyatla 37:fc4b4fd6a649 200 */
nyatla 37:fc4b4fd6a649 201 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)
nyatla 37:fc4b4fd6a649 202 {
nyatla 37:fc4b4fd6a649 203 NyLPC_TUInt16 rlen;
nyatla 37:fc4b4fd6a649 204 //タイマを生成
nyatla 37:fc4b4fd6a649 205 NyLPC_TcStopwatch_t sw;
nyatla 37:fc4b4fd6a649 206 NyLPC_cStopwatch_initialize(&sw);
nyatla 37:fc4b4fd6a649 207 const char* b;
nyatla 37:fc4b4fd6a649 208 const struct NyLPC_TIPv4RxInfo* rh;
nyatla 37:fc4b4fd6a649 209
nyatla 37:fc4b4fd6a649 210 //ESTABLISHED以外の場合は、エラー。
nyatla 37:fc4b4fd6a649 211 NyLPC_cStopwatch_setNow(&sw);
nyatla 37:fc4b4fd6a649 212 while(NyLPC_cStopwatch_elapseInMsec(&sw)<i_wait_msec)
nyatla 37:fc4b4fd6a649 213 {
nyatla 37:fc4b4fd6a649 214 //MUTEX LOCK
nyatla 37:fc4b4fd6a649 215 lockResource(i_inst);
nyatla 37:fc4b4fd6a649 216 rlen=NyLPC_cFifoBuffer_getLength(&(i_inst->rxbuf));
nyatla 37:fc4b4fd6a649 217 //MUTEX UNLOCK
nyatla 37:fc4b4fd6a649 218 unlockResource(i_inst);
nyatla 37:fc4b4fd6a649 219 if(rlen>0){
nyatla 37:fc4b4fd6a649 220 //受信キューにデータがあれば返す。
nyatla 37:fc4b4fd6a649 221 b=(char*)NyLPC_cFifoBuffer_getPtr(&(i_inst->rxbuf));
nyatla 37:fc4b4fd6a649 222 rh=(const struct NyLPC_TIPv4RxInfo*)b;
nyatla 37:fc4b4fd6a649 223 *o_buf_ptr=b+sizeof(struct NyLPC_TIPv4RxInfo);
nyatla 37:fc4b4fd6a649 224 if(o_info!=NULL){
nyatla 37:fc4b4fd6a649 225 *o_info=rh;
nyatla 37:fc4b4fd6a649 226 }
nyatla 37:fc4b4fd6a649 227 return rh->size;
nyatla 37:fc4b4fd6a649 228 }
nyatla 37:fc4b4fd6a649 229 //タスクスイッチ
nyatla 37:fc4b4fd6a649 230 NyLPC_cThread_yield();
nyatla 37:fc4b4fd6a649 231 };
nyatla 37:fc4b4fd6a649 232 NyLPC_cStopwatch_finalize(&sw);
nyatla 37:fc4b4fd6a649 233 return 0;
nyatla 37:fc4b4fd6a649 234 }
nyatla 37:fc4b4fd6a649 235 /**
nyatla 37:fc4b4fd6a649 236 * See header file
nyatla 37:fc4b4fd6a649 237 */
nyatla 37:fc4b4fd6a649 238 void NyLPC_cUdpSocket_pseek(NyLPC_TcUdpSocket_t* i_inst)
nyatla 37:fc4b4fd6a649 239 {
nyatla 37:fc4b4fd6a649 240 NyLPC_TUInt16 s;
nyatla 37:fc4b4fd6a649 241 const struct NyLPC_TIPv4RxInfo* rh;
nyatla 37:fc4b4fd6a649 242 //シークサイズを決定
nyatla 37:fc4b4fd6a649 243 s=NyLPC_cFifoBuffer_getLength(&(i_inst->rxbuf));
nyatla 37:fc4b4fd6a649 244 if(s==0){
nyatla 37:fc4b4fd6a649 245 return;
nyatla 37:fc4b4fd6a649 246 }
nyatla 37:fc4b4fd6a649 247 rh=(const struct NyLPC_TIPv4RxInfo*)NyLPC_cFifoBuffer_getPtr(&(i_inst->rxbuf));
nyatla 37:fc4b4fd6a649 248 NyLPC_cFifoBuffer_pop(&(i_inst->rxbuf),rh->size+sizeof(struct NyLPC_TIPv4RxInfo));
nyatla 37:fc4b4fd6a649 249 }
nyatla 37:fc4b4fd6a649 250
nyatla 37:fc4b4fd6a649 251 /**
nyatla 37:fc4b4fd6a649 252 * See header file.
nyatla 37:fc4b4fd6a649 253 */
nyatla 37:fc4b4fd6a649 254 void* NyLPC_cUdpSocket_allocSendBuf(NyLPC_TcUdpSocket_t* i_inst,NyLPC_TUInt16 i_hint,NyLPC_TUInt16* o_buf_size,NyLPC_TUInt32 i_wait_in_msec)
nyatla 37:fc4b4fd6a649 255 {
nyatla 37:fc4b4fd6a649 256 NyLPC_TUInt16 s;
nyatla 37:fc4b4fd6a649 257 void* buf;
nyatla 37:fc4b4fd6a649 258 NyLPC_TcStopwatch_t sw;
nyatla 37:fc4b4fd6a649 259
nyatla 37:fc4b4fd6a649 260 NyLPC_cStopwatch_initialize(&sw);
nyatla 37:fc4b4fd6a649 261 NyLPC_cStopwatch_startExpire(&sw,i_wait_in_msec);
nyatla 37:fc4b4fd6a649 262
nyatla 37:fc4b4fd6a649 263 //送信バッファを取得
nyatla 37:fc4b4fd6a649 264 //@bug バッファが取れるまで通信がブロックするの。ここはなんとかしないと。
nyatla 37:fc4b4fd6a649 265 for(;;){
nyatla 37:fc4b4fd6a649 266 buf=NyLPC_cUipService_allocTxBuf(i_hint+(SIZE_OF_IPv4_UDPIP_HEADER),&s);
nyatla 37:fc4b4fd6a649 267 if(buf!=NULL){
nyatla 37:fc4b4fd6a649 268 break;
nyatla 37:fc4b4fd6a649 269 }
nyatla 37:fc4b4fd6a649 270 //タイムアウト確認
nyatla 37:fc4b4fd6a649 271 if(NyLPC_cStopwatch_isExpired(&sw)){
nyatla 37:fc4b4fd6a649 272 return NULL;
nyatla 37:fc4b4fd6a649 273 }
nyatla 37:fc4b4fd6a649 274 }
nyatla 37:fc4b4fd6a649 275 //バッファサイズ確定。
nyatla 37:fc4b4fd6a649 276 *o_buf_size=s;
nyatla 37:fc4b4fd6a649 277 NyLPC_cStopwatch_finalize(&sw);
nyatla 37:fc4b4fd6a649 278 return (NyLPC_TUInt8*)buf+SIZE_OF_IPv4_UDPIP_HEADER;
nyatla 37:fc4b4fd6a649 279 }
nyatla 37:fc4b4fd6a649 280 /**
nyatla 37:fc4b4fd6a649 281 * See Header file.
nyatla 37:fc4b4fd6a649 282 */
nyatla 37:fc4b4fd6a649 283 void NyLPC_cUdpSocket_releaseSendBuf(NyLPC_TcUdpSocket_t* i_inst,void* i_buf_ptr)
nyatla 37:fc4b4fd6a649 284 {
nyatla 37:fc4b4fd6a649 285 NyLPC_cUipService_releaseTxBuf((NyLPC_TUInt8*)i_buf_ptr-SIZE_OF_IPv4_UDPIP_HEADER);
nyatla 37:fc4b4fd6a649 286 }
nyatla 37:fc4b4fd6a649 287
nyatla 37:fc4b4fd6a649 288 /**
nyatla 37:fc4b4fd6a649 289 * See header file
nyatla 37:fc4b4fd6a649 290 */
nyatla 37:fc4b4fd6a649 291 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)
nyatla 37:fc4b4fd6a649 292 {
nyatla 37:fc4b4fd6a649 293 void* buf;
nyatla 37:fc4b4fd6a649 294 //ブロードキャストの場合、フラグを確認
nyatla 37:fc4b4fd6a649 295 if(NyLPC_TIPv4Addr_isEqual(i_addr,&NyLPC_TIPv4Addr_BROADCAST)){
nyatla 37:fc4b4fd6a649 296 if(!NyLPC_TUInt8_isBitOn(i_inst->uip_udp_conn.flags,NyLPC_cUdpSocket_FLAG_BROADCAST)){
nyatla 37:fc4b4fd6a649 297 return NyLPC_TBool_FALSE;
nyatla 37:fc4b4fd6a649 298 }
nyatla 37:fc4b4fd6a649 299 }
nyatla 37:fc4b4fd6a649 300
nyatla 37:fc4b4fd6a649 301 //先頭ポインタは、i_buf-sizeof(SIZE_OF_IPv4_TCPIP_HEADER)固定
nyatla 37:fc4b4fd6a649 302 buf=(NyLPC_TUInt8*)i_buf_ptr-SIZE_OF_IPv4_UDPIP_HEADER;
nyatla 37:fc4b4fd6a649 303
nyatla 37:fc4b4fd6a649 304 lockResource(i_inst);
nyatla 37:fc4b4fd6a649 305 //IPv4ペイロードの書き込み
nyatla 69:8c5f220441f5 306 setUdpTxBufHeader(i_inst,buf,i_addr,i_port,0x05,i_len);
nyatla 37:fc4b4fd6a649 307 unlockResource(i_inst);
nyatla 37:fc4b4fd6a649 308 // !(BroadCast || Multicast)の場合は送信前にARPテーブルをチェックする。
nyatla 37:fc4b4fd6a649 309 if(!(NyLPC_TIPv4Addr_isEqual(i_addr,&NyLPC_TIPv4Addr_BROADCAST) || NyLPC_TIPv4Addr_isEqualWithMask(i_addr,&NyLPC_TIPv4Addr_MULTICAST,&NyLPC_TIPv4Addr_MULTICAST_MASK))){
nyatla 37:fc4b4fd6a649 310 if(!NyLPC_cUipService_hasArpInfo(i_addr)){
nyatla 37:fc4b4fd6a649 311 NyLPC_cUipService_sendArpRequest(i_addr);
nyatla 37:fc4b4fd6a649 312 NyLPC_cThread_sleep(30);
nyatla 37:fc4b4fd6a649 313 }
nyatla 37:fc4b4fd6a649 314 }
nyatla 37:fc4b4fd6a649 315 NyLPC_cUipService_sendIPv4Tx(buf);
nyatla 37:fc4b4fd6a649 316 NyLPC_cUipService_releaseTxBuf(buf);
nyatla 37:fc4b4fd6a649 317 return NyLPC_TBool_TRUE;
nyatla 37:fc4b4fd6a649 318 }
nyatla 37:fc4b4fd6a649 319
nyatla 37:fc4b4fd6a649 320 /**
nyatla 37:fc4b4fd6a649 321 * See header file.
nyatla 37:fc4b4fd6a649 322 */
nyatla 37:fc4b4fd6a649 323 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)
nyatla 37:fc4b4fd6a649 324 {
nyatla 37:fc4b4fd6a649 325 NyLPC_TUInt16 s;
nyatla 37:fc4b4fd6a649 326 int i;
nyatla 37:fc4b4fd6a649 327 void* buf;
nyatla 37:fc4b4fd6a649 328 if(i_len<1 || i_len>1200){
nyatla 37:fc4b4fd6a649 329 return 0;
nyatla 37:fc4b4fd6a649 330 }
nyatla 37:fc4b4fd6a649 331 //バッファの取得確率を上げるために2倍のサイズを要求
nyatla 37:fc4b4fd6a649 332 for(i=0;i<3;i++){
nyatla 37:fc4b4fd6a649 333 buf=NyLPC_cUdpSocket_allocSendBuf(i_inst,i_len*2,&s,i_wait_in_msec);
nyatla 37:fc4b4fd6a649 334 if(buf==NULL || s<i_len){
nyatla 37:fc4b4fd6a649 335 continue;
nyatla 37:fc4b4fd6a649 336 }
nyatla 37:fc4b4fd6a649 337 break;
nyatla 37:fc4b4fd6a649 338 }
nyatla 37:fc4b4fd6a649 339 if(buf==NULL){
nyatla 37:fc4b4fd6a649 340 return -1;
nyatla 37:fc4b4fd6a649 341 }
nyatla 37:fc4b4fd6a649 342 //送信サイズの計算
nyatla 37:fc4b4fd6a649 343 memcpy(buf,i_buf_ptr,i_len);
nyatla 37:fc4b4fd6a649 344 if(!NyLPC_cUdpSocket_psend(i_inst,i_addr,i_port,buf,i_len)){
nyatla 37:fc4b4fd6a649 345 NyLPC_cUdpSocket_releaseSendBuf(i_inst,buf);
nyatla 37:fc4b4fd6a649 346 return -1;
nyatla 37:fc4b4fd6a649 347 }
nyatla 37:fc4b4fd6a649 348 return i_len;
nyatla 37:fc4b4fd6a649 349 }
nyatla 37:fc4b4fd6a649 350
nyatla 37:fc4b4fd6a649 351 void NyLPC_cUdpSocket_startService(NyLPC_TcUdpSocket_t* i_inst,const NyLPC_TcIPv4Config_t* i_config)
nyatla 37:fc4b4fd6a649 352 {
nyatla 37:fc4b4fd6a649 353 i_inst->uip_udp_conn.lipaddr=i_config->ip_addr;
nyatla 37:fc4b4fd6a649 354 //受信バッファのクリア
nyatla 37:fc4b4fd6a649 355 NyLPC_cFifoBuffer_clear(&(i_inst->rxbuf));
nyatla 37:fc4b4fd6a649 356 return;
nyatla 37:fc4b4fd6a649 357 }
nyatla 37:fc4b4fd6a649 358
nyatla 37:fc4b4fd6a649 359
nyatla 37:fc4b4fd6a649 360 void NyLPC_cUdpSocket_stopService(NyLPC_TcUdpSocket_t* i_inst)
nyatla 37:fc4b4fd6a649 361 {
nyatla 37:fc4b4fd6a649 362 //停止処理?
nyatla 37:fc4b4fd6a649 363 }
nyatla 37:fc4b4fd6a649 364