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:
Wed Jun 19 09:33:01 2013 +0000
Revision:
37:fc4b4fd6a649
Child:
48:00d211aac2ec
update; MiMic Core r263; add mDNS service,DHCP client,APIPA client

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 37:fc4b4fd6a649 62 #define NyLPC_cUdpSocket_FLAG_BROADCAST 0x01
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 37:fc4b4fd6a649 69 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 70 {
nyatla 37:fc4b4fd6a649 71 NyLPC_TcUipService_t* srv=_NyLPC_TcUipService_inst;
nyatla 37:fc4b4fd6a649 72 NyLPC_cBaseSocket_initialize(&(i_inst->_super),NyLPC_TcBaseSocket_TYPEID_UDP_SOCK);
nyatla 37:fc4b4fd6a649 73 //uipサービスは初期化済であること。
nyatla 37:fc4b4fd6a649 74 NyLPC_Assert(NyLPC_TcUipService_isInitService());
nyatla 37:fc4b4fd6a649 75 i_inst->_smutex=NyLPC_cIPv4_getSockMutex(&(srv->_tcpv4));
nyatla 37:fc4b4fd6a649 76 i_inst->uip_udp_conn.lport=NyLPC_htons(i_port);
nyatla 37:fc4b4fd6a649 77 i_inst->uip_udp_conn.mcastaddr=NyLPC_TIPv4Addr_ZERO;
nyatla 37:fc4b4fd6a649 78 i_inst->uip_udp_conn.flags=0x00;
nyatla 37:fc4b4fd6a649 79 i_inst->as_handler.rx=NULL;
nyatla 37:fc4b4fd6a649 80 i_inst->as_handler.periodic=NULL;
nyatla 37:fc4b4fd6a649 81
nyatla 37:fc4b4fd6a649 82 NyLPC_cFifoBuffer_initialize(&(i_inst->rxbuf),i_rbuf,i_rbuf_len);
nyatla 37:fc4b4fd6a649 83 //管理リストへ登録。
nyatla 37:fc4b4fd6a649 84 return NyLPC_cIPv4_addSocket(&(srv->_tcpv4),&(i_inst->_super));
nyatla 37:fc4b4fd6a649 85 }
nyatla 37:fc4b4fd6a649 86
nyatla 37:fc4b4fd6a649 87 void NyLPC_cUdpSocket_finalize(NyLPC_TcUdpSocket_t* i_inst)
nyatla 37:fc4b4fd6a649 88 {
nyatla 37:fc4b4fd6a649 89 NyLPC_TcUipService_t* srv=_NyLPC_TcUipService_inst;
nyatla 37:fc4b4fd6a649 90 NyLPC_Assert(NyLPC_TcUipService_isInitService());
nyatla 37:fc4b4fd6a649 91 //uipサービスは初期化済であること。
nyatla 37:fc4b4fd6a649 92 if(!NyLPC_cIPv4_removeSocket(&(srv->_tcpv4),&(i_inst->_super))){
nyatla 37:fc4b4fd6a649 93 //削除失敗、それは死を意味する。
nyatla 37:fc4b4fd6a649 94 NyLPC_Abort();
nyatla 37:fc4b4fd6a649 95 }
nyatla 37:fc4b4fd6a649 96 NyLPC_cFifoBuffer_finalize(&(i_inst->rxbuf));
nyatla 37:fc4b4fd6a649 97 NyLPC_cBaseSocket_finalize(&(i_inst->_super));
nyatla 37:fc4b4fd6a649 98 return;
nyatla 37:fc4b4fd6a649 99 }
nyatla 37:fc4b4fd6a649 100 void NyLPC_cUdpSocket_joinMulticast(NyLPC_TcUdpSocket_t* i_inst,const struct NyLPC_TIPv4Addr* i_addr)
nyatla 37:fc4b4fd6a649 101 {
nyatla 37:fc4b4fd6a649 102 i_inst->uip_udp_conn.mcastaddr=*i_addr;
nyatla 37:fc4b4fd6a649 103 }
nyatla 37:fc4b4fd6a649 104 void NyLPC_cUdpSocket_setBroadcast(NyLPC_TcUdpSocket_t* i_inst)
nyatla 37:fc4b4fd6a649 105 {
nyatla 37:fc4b4fd6a649 106 i_inst->uip_udp_conn.flags|=NyLPC_cUdpSocket_FLAG_BROADCAST;
nyatla 37:fc4b4fd6a649 107 }
nyatla 37:fc4b4fd6a649 108
nyatla 37:fc4b4fd6a649 109
nyatla 37:fc4b4fd6a649 110
nyatla 37:fc4b4fd6a649 111 /**
nyatla 37:fc4b4fd6a649 112 * この関数は、rxパケットを処理して、ソケットの状態を更新します。
nyatla 37:fc4b4fd6a649 113 * uipサービスタスクが実行する関数です。
nyatla 37:fc4b4fd6a649 114 * o_ippのペイロードに、応答ペイロードを設定することがあります。
nyatla 37:fc4b4fd6a649 115 * この関数はNyLPC_cTcpSocket_periodicと排他実行すること。
nyatla 37:fc4b4fd6a649 116 */
nyatla 37:fc4b4fd6a649 117 NyLPC_TBool NyLPC_cUdpSocket_parseRx(
nyatla 37:fc4b4fd6a649 118 NyLPC_TcUdpSocket_t* i_inst,
nyatla 37:fc4b4fd6a649 119 NyLPC_TcIPv4Payload_t* o_ipp)
nyatla 37:fc4b4fd6a649 120 {
nyatla 37:fc4b4fd6a649 121 NyLPC_TUInt16 tmp16;
nyatla 37:fc4b4fd6a649 122 struct NyLPC_TIPv4RxInfo dheader;
nyatla 37:fc4b4fd6a649 123 void* data_offset;
nyatla 37:fc4b4fd6a649 124 //ブロードキャストの場合、フラグを確認
nyatla 37:fc4b4fd6a649 125 if(NyLPC_TIPv4Addr_isEqual(&(o_ipp->header->destipaddr),&NyLPC_TIPv4Addr_BROADCAST)){
nyatla 37:fc4b4fd6a649 126 if(!NyLPC_TUInt8_isBitOn(i_inst->uip_udp_conn.flags,NyLPC_cUdpSocket_FLAG_BROADCAST)){
nyatla 37:fc4b4fd6a649 127 goto DROP;
nyatla 37:fc4b4fd6a649 128 }
nyatla 37:fc4b4fd6a649 129 }
nyatla 37:fc4b4fd6a649 130 //パラメータの計算
nyatla 37:fc4b4fd6a649 131 tmp16=NyLPC_TUdpHeader_getHeaderLength(o_ipp->payload.tcp);
nyatla 37:fc4b4fd6a649 132 //UDPペイロードの長さは、IPパケットの長さ-(IPヘッダ+UDPヘッダ)
nyatla 37:fc4b4fd6a649 133 dheader.size=NyLPC_TIPv4Header_getPacketLength(o_ipp->header)-NyLPC_TIPv4Header_getHeaderLength(o_ipp->header)-tmp16;
nyatla 37:fc4b4fd6a649 134 dheader.peer_ip=o_ipp->header->srcipaddr;
nyatla 37:fc4b4fd6a649 135 dheader.peer_port=NyLPC_ntohs(o_ipp->payload.udp->srcport);
nyatla 37:fc4b4fd6a649 136 dheader.ip=o_ipp->header->destipaddr;
nyatla 37:fc4b4fd6a649 137 dheader.port=NyLPC_ntohs(o_ipp->payload.udp->destport);
nyatla 37:fc4b4fd6a649 138 if(i_inst->as_handler.rx!=NULL){
nyatla 37:fc4b4fd6a649 139 if(!i_inst->as_handler.rx(i_inst,o_ipp->payload.rawbuf+tmp16,&dheader)){
nyatla 37:fc4b4fd6a649 140 return NyLPC_TBool_FALSE;//UDPはReturnパケットなし
nyatla 37:fc4b4fd6a649 141 }
nyatla 37:fc4b4fd6a649 142 }
nyatla 37:fc4b4fd6a649 143 //TCPデータオフセット
nyatla 37:fc4b4fd6a649 144 data_offset=o_ipp->payload.rawbuf+tmp16;
nyatla 37:fc4b4fd6a649 145
nyatla 37:fc4b4fd6a649 146 //インスタンスをロックする。
nyatla 37:fc4b4fd6a649 147 lockResource(i_inst);
nyatla 37:fc4b4fd6a649 148 //受信キューへ追加(データ構造はsize[2]+data[n]).sizeに16ビットの受信サイズ,後続にデータ
nyatla 37:fc4b4fd6a649 149
nyatla 37:fc4b4fd6a649 150 //受信データサイズを確認
nyatla 37:fc4b4fd6a649 151 if(NyLPC_cFifoBuffer_getSpace(&(i_inst->rxbuf))<dheader.size+sizeof(struct NyLPC_TIPv4RxInfo)){
nyatla 37:fc4b4fd6a649 152 goto DROP;
nyatla 37:fc4b4fd6a649 153 }
nyatla 37:fc4b4fd6a649 154 //バッファに格納可能なら、格納。
nyatla 37:fc4b4fd6a649 155 NyLPC_cFifoBuffer_push(&(i_inst->rxbuf),&dheader,sizeof(struct NyLPC_TIPv4RxInfo));
nyatla 37:fc4b4fd6a649 156 NyLPC_cFifoBuffer_push(&(i_inst->rxbuf),data_offset,dheader.size);
nyatla 37:fc4b4fd6a649 157 unlockResource(i_inst);
nyatla 37:fc4b4fd6a649 158 return NyLPC_TBool_FALSE;//UDPはReturnパケットなし
nyatla 37:fc4b4fd6a649 159 DROP:
nyatla 37:fc4b4fd6a649 160 unlockResource(i_inst);
nyatla 37:fc4b4fd6a649 161 return NyLPC_TBool_FALSE;
nyatla 37:fc4b4fd6a649 162 }
nyatla 37:fc4b4fd6a649 163
nyatla 37:fc4b4fd6a649 164
nyatla 37:fc4b4fd6a649 165
nyatla 37:fc4b4fd6a649 166 /**
nyatla 37:fc4b4fd6a649 167 * see Header file
nyatla 37:fc4b4fd6a649 168 */
nyatla 37:fc4b4fd6a649 169 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 170 {
nyatla 37:fc4b4fd6a649 171 NyLPC_TUInt16 rlen;
nyatla 37:fc4b4fd6a649 172 //タイマを生成
nyatla 37:fc4b4fd6a649 173 NyLPC_TcStopwatch_t sw;
nyatla 37:fc4b4fd6a649 174 NyLPC_cStopwatch_initialize(&sw);
nyatla 37:fc4b4fd6a649 175 const char* b;
nyatla 37:fc4b4fd6a649 176 const struct NyLPC_TIPv4RxInfo* rh;
nyatla 37:fc4b4fd6a649 177
nyatla 37:fc4b4fd6a649 178 //ESTABLISHED以外の場合は、エラー。
nyatla 37:fc4b4fd6a649 179 NyLPC_cStopwatch_setNow(&sw);
nyatla 37:fc4b4fd6a649 180 while(NyLPC_cStopwatch_elapseInMsec(&sw)<i_wait_msec)
nyatla 37:fc4b4fd6a649 181 {
nyatla 37:fc4b4fd6a649 182 //MUTEX LOCK
nyatla 37:fc4b4fd6a649 183 lockResource(i_inst);
nyatla 37:fc4b4fd6a649 184 rlen=NyLPC_cFifoBuffer_getLength(&(i_inst->rxbuf));
nyatla 37:fc4b4fd6a649 185 //MUTEX UNLOCK
nyatla 37:fc4b4fd6a649 186 unlockResource(i_inst);
nyatla 37:fc4b4fd6a649 187 if(rlen>0){
nyatla 37:fc4b4fd6a649 188 //受信キューにデータがあれば返す。
nyatla 37:fc4b4fd6a649 189 b=(char*)NyLPC_cFifoBuffer_getPtr(&(i_inst->rxbuf));
nyatla 37:fc4b4fd6a649 190 rh=(const struct NyLPC_TIPv4RxInfo*)b;
nyatla 37:fc4b4fd6a649 191 *o_buf_ptr=b+sizeof(struct NyLPC_TIPv4RxInfo);
nyatla 37:fc4b4fd6a649 192 if(o_info!=NULL){
nyatla 37:fc4b4fd6a649 193 *o_info=rh;
nyatla 37:fc4b4fd6a649 194 }
nyatla 37:fc4b4fd6a649 195 return rh->size;
nyatla 37:fc4b4fd6a649 196 }
nyatla 37:fc4b4fd6a649 197 //タスクスイッチ
nyatla 37:fc4b4fd6a649 198 NyLPC_cThread_yield();
nyatla 37:fc4b4fd6a649 199 };
nyatla 37:fc4b4fd6a649 200 NyLPC_cStopwatch_finalize(&sw);
nyatla 37:fc4b4fd6a649 201 return 0;
nyatla 37:fc4b4fd6a649 202 }
nyatla 37:fc4b4fd6a649 203 /**
nyatla 37:fc4b4fd6a649 204 * See header file
nyatla 37:fc4b4fd6a649 205 */
nyatla 37:fc4b4fd6a649 206 void NyLPC_cUdpSocket_pseek(NyLPC_TcUdpSocket_t* i_inst)
nyatla 37:fc4b4fd6a649 207 {
nyatla 37:fc4b4fd6a649 208 NyLPC_TUInt16 s;
nyatla 37:fc4b4fd6a649 209 const struct NyLPC_TIPv4RxInfo* rh;
nyatla 37:fc4b4fd6a649 210 //シークサイズを決定
nyatla 37:fc4b4fd6a649 211 s=NyLPC_cFifoBuffer_getLength(&(i_inst->rxbuf));
nyatla 37:fc4b4fd6a649 212 if(s==0){
nyatla 37:fc4b4fd6a649 213 return;
nyatla 37:fc4b4fd6a649 214 }
nyatla 37:fc4b4fd6a649 215 rh=(const struct NyLPC_TIPv4RxInfo*)NyLPC_cFifoBuffer_getPtr(&(i_inst->rxbuf));
nyatla 37:fc4b4fd6a649 216 NyLPC_cFifoBuffer_pop(&(i_inst->rxbuf),rh->size+sizeof(struct NyLPC_TIPv4RxInfo));
nyatla 37:fc4b4fd6a649 217 }
nyatla 37:fc4b4fd6a649 218
nyatla 37:fc4b4fd6a649 219 /**
nyatla 37:fc4b4fd6a649 220 * See header file.
nyatla 37:fc4b4fd6a649 221 */
nyatla 37:fc4b4fd6a649 222 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 223 {
nyatla 37:fc4b4fd6a649 224 NyLPC_TUInt16 s;
nyatla 37:fc4b4fd6a649 225 void* buf;
nyatla 37:fc4b4fd6a649 226 NyLPC_TcStopwatch_t sw;
nyatla 37:fc4b4fd6a649 227
nyatla 37:fc4b4fd6a649 228 NyLPC_cStopwatch_initialize(&sw);
nyatla 37:fc4b4fd6a649 229 NyLPC_cStopwatch_startExpire(&sw,i_wait_in_msec);
nyatla 37:fc4b4fd6a649 230
nyatla 37:fc4b4fd6a649 231 //送信バッファを取得
nyatla 37:fc4b4fd6a649 232 //@bug バッファが取れるまで通信がブロックするの。ここはなんとかしないと。
nyatla 37:fc4b4fd6a649 233 for(;;){
nyatla 37:fc4b4fd6a649 234 buf=NyLPC_cUipService_allocTxBuf(i_hint+(SIZE_OF_IPv4_UDPIP_HEADER),&s);
nyatla 37:fc4b4fd6a649 235 if(buf!=NULL){
nyatla 37:fc4b4fd6a649 236 break;
nyatla 37:fc4b4fd6a649 237 }
nyatla 37:fc4b4fd6a649 238 //タイムアウト確認
nyatla 37:fc4b4fd6a649 239 if(NyLPC_cStopwatch_isExpired(&sw)){
nyatla 37:fc4b4fd6a649 240 return NULL;
nyatla 37:fc4b4fd6a649 241 }
nyatla 37:fc4b4fd6a649 242 }
nyatla 37:fc4b4fd6a649 243 //バッファサイズ確定。
nyatla 37:fc4b4fd6a649 244 *o_buf_size=s;
nyatla 37:fc4b4fd6a649 245 NyLPC_cStopwatch_finalize(&sw);
nyatla 37:fc4b4fd6a649 246 return (NyLPC_TUInt8*)buf+SIZE_OF_IPv4_UDPIP_HEADER;
nyatla 37:fc4b4fd6a649 247 }
nyatla 37:fc4b4fd6a649 248 /**
nyatla 37:fc4b4fd6a649 249 * See Header file.
nyatla 37:fc4b4fd6a649 250 */
nyatla 37:fc4b4fd6a649 251 void NyLPC_cUdpSocket_releaseSendBuf(NyLPC_TcUdpSocket_t* i_inst,void* i_buf_ptr)
nyatla 37:fc4b4fd6a649 252 {
nyatla 37:fc4b4fd6a649 253 NyLPC_cUipService_releaseTxBuf((NyLPC_TUInt8*)i_buf_ptr-SIZE_OF_IPv4_UDPIP_HEADER);
nyatla 37:fc4b4fd6a649 254 }
nyatla 37:fc4b4fd6a649 255
nyatla 37:fc4b4fd6a649 256 /**
nyatla 37:fc4b4fd6a649 257 * See header file
nyatla 37:fc4b4fd6a649 258 */
nyatla 37:fc4b4fd6a649 259 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 260 {
nyatla 37:fc4b4fd6a649 261 void* buf;
nyatla 37:fc4b4fd6a649 262 NyLPC_TcIPv4Payload_t ipp;
nyatla 37:fc4b4fd6a649 263 //ブロードキャストの場合、フラグを確認
nyatla 37:fc4b4fd6a649 264 if(NyLPC_TIPv4Addr_isEqual(i_addr,&NyLPC_TIPv4Addr_BROADCAST)){
nyatla 37:fc4b4fd6a649 265 if(!NyLPC_TUInt8_isBitOn(i_inst->uip_udp_conn.flags,NyLPC_cUdpSocket_FLAG_BROADCAST)){
nyatla 37:fc4b4fd6a649 266 return NyLPC_TBool_FALSE;
nyatla 37:fc4b4fd6a649 267 }
nyatla 37:fc4b4fd6a649 268 }
nyatla 37:fc4b4fd6a649 269
nyatla 37:fc4b4fd6a649 270 //先頭ポインタは、i_buf-sizeof(SIZE_OF_IPv4_TCPIP_HEADER)固定
nyatla 37:fc4b4fd6a649 271 buf=(NyLPC_TUInt8*)i_buf_ptr-SIZE_OF_IPv4_UDPIP_HEADER;
nyatla 37:fc4b4fd6a649 272 NyLPC_cIPv4Payload_initialize(&ipp);
nyatla 37:fc4b4fd6a649 273
nyatla 37:fc4b4fd6a649 274 lockResource(i_inst);
nyatla 37:fc4b4fd6a649 275 //IPv4ペイロードの書き込み
nyatla 37:fc4b4fd6a649 276 NyLPC_cIPv4Payload_setTxBuf(&ipp,buf);
nyatla 37:fc4b4fd6a649 277 //パケットヘッダの生成
nyatla 37:fc4b4fd6a649 278 NyLPC_cIPv4Payload_initUdpTx(&ipp,0x05,i_len);
nyatla 37:fc4b4fd6a649 279 NyLPC_cIPv4Payload_setUdpTxHeaderByConnection(&ipp,&(i_inst->uip_udp_conn),i_addr,i_port);
nyatla 37:fc4b4fd6a649 280 NyLPC_cIPv4Payload_closeUdpTxPacket(&ipp);
nyatla 37:fc4b4fd6a649 281 unlockResource(i_inst);
nyatla 37:fc4b4fd6a649 282 // !(BroadCast || Multicast)の場合は送信前にARPテーブルをチェックする。
nyatla 37:fc4b4fd6a649 283 if(!(NyLPC_TIPv4Addr_isEqual(i_addr,&NyLPC_TIPv4Addr_BROADCAST) || NyLPC_TIPv4Addr_isEqualWithMask(i_addr,&NyLPC_TIPv4Addr_MULTICAST,&NyLPC_TIPv4Addr_MULTICAST_MASK))){
nyatla 37:fc4b4fd6a649 284 if(!NyLPC_cUipService_hasArpInfo(i_addr)){
nyatla 37:fc4b4fd6a649 285 NyLPC_cUipService_sendArpRequest(i_addr);
nyatla 37:fc4b4fd6a649 286 NyLPC_cThread_sleep(30);
nyatla 37:fc4b4fd6a649 287 }
nyatla 37:fc4b4fd6a649 288 }
nyatla 37:fc4b4fd6a649 289 NyLPC_cUipService_sendIPv4Tx(buf);
nyatla 37:fc4b4fd6a649 290 NyLPC_cUipService_releaseTxBuf(buf);
nyatla 37:fc4b4fd6a649 291 NyLPC_cIPv4Payload_finalize(&ipp);
nyatla 37:fc4b4fd6a649 292 return NyLPC_TBool_TRUE;
nyatla 37:fc4b4fd6a649 293 }
nyatla 37:fc4b4fd6a649 294
nyatla 37:fc4b4fd6a649 295 /**
nyatla 37:fc4b4fd6a649 296 * See header file.
nyatla 37:fc4b4fd6a649 297 */
nyatla 37:fc4b4fd6a649 298 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 299 {
nyatla 37:fc4b4fd6a649 300 NyLPC_TUInt16 s;
nyatla 37:fc4b4fd6a649 301 int i;
nyatla 37:fc4b4fd6a649 302 void* buf;
nyatla 37:fc4b4fd6a649 303 if(i_len<1 || i_len>1200){
nyatla 37:fc4b4fd6a649 304 return 0;
nyatla 37:fc4b4fd6a649 305 }
nyatla 37:fc4b4fd6a649 306 //バッファの取得確率を上げるために2倍のサイズを要求
nyatla 37:fc4b4fd6a649 307 for(i=0;i<3;i++){
nyatla 37:fc4b4fd6a649 308 buf=NyLPC_cUdpSocket_allocSendBuf(i_inst,i_len*2,&s,i_wait_in_msec);
nyatla 37:fc4b4fd6a649 309 if(buf==NULL || s<i_len){
nyatla 37:fc4b4fd6a649 310 continue;
nyatla 37:fc4b4fd6a649 311 }
nyatla 37:fc4b4fd6a649 312 break;
nyatla 37:fc4b4fd6a649 313 }
nyatla 37:fc4b4fd6a649 314 if(buf==NULL){
nyatla 37:fc4b4fd6a649 315 return -1;
nyatla 37:fc4b4fd6a649 316 }
nyatla 37:fc4b4fd6a649 317 //送信サイズの計算
nyatla 37:fc4b4fd6a649 318 memcpy(buf,i_buf_ptr,i_len);
nyatla 37:fc4b4fd6a649 319 if(!NyLPC_cUdpSocket_psend(i_inst,i_addr,i_port,buf,i_len)){
nyatla 37:fc4b4fd6a649 320 NyLPC_cUdpSocket_releaseSendBuf(i_inst,buf);
nyatla 37:fc4b4fd6a649 321 return -1;
nyatla 37:fc4b4fd6a649 322 }
nyatla 37:fc4b4fd6a649 323 return i_len;
nyatla 37:fc4b4fd6a649 324 }
nyatla 37:fc4b4fd6a649 325
nyatla 37:fc4b4fd6a649 326 void NyLPC_cUdpSocket_startService(NyLPC_TcUdpSocket_t* i_inst,const NyLPC_TcIPv4Config_t* i_config)
nyatla 37:fc4b4fd6a649 327 {
nyatla 37:fc4b4fd6a649 328 i_inst->uip_udp_conn.lipaddr=i_config->ip_addr;
nyatla 37:fc4b4fd6a649 329 //受信バッファのクリア
nyatla 37:fc4b4fd6a649 330 NyLPC_cFifoBuffer_clear(&(i_inst->rxbuf));
nyatla 37:fc4b4fd6a649 331 return;
nyatla 37:fc4b4fd6a649 332 }
nyatla 37:fc4b4fd6a649 333
nyatla 37:fc4b4fd6a649 334
nyatla 37:fc4b4fd6a649 335 void NyLPC_cUdpSocket_stopService(NyLPC_TcUdpSocket_t* i_inst)
nyatla 37:fc4b4fd6a649 336 {
nyatla 37:fc4b4fd6a649 337 //停止処理?
nyatla 37:fc4b4fd6a649 338 }
nyatla 37:fc4b4fd6a649 339