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:
Sun Mar 15 09:33:38 2015 +0000
Revision:
110:257739f9b31e
Parent:
109:18f12ac01097
51d1c88c8a56d6295311cfd20aa197a9cd650b2d; ????; NetworkInterface???????

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-2013 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 #include "NyLPC_cDhcpClient.h"
nyatla 110:257739f9b31e 27 #include "../NyLPC_cNet.h"
nyatla 37:fc4b4fd6a649 28 #include <stdio.h>
nyatla 37:fc4b4fd6a649 29 #include <string.h>
nyatla 37:fc4b4fd6a649 30
nyatla 37:fc4b4fd6a649 31 struct NyLPC_TDhcpHeader
nyatla 37:fc4b4fd6a649 32 {
nyatla 37:fc4b4fd6a649 33 NyLPC_TUInt8 op;
nyatla 37:fc4b4fd6a649 34 NyLPC_TUInt8 htype;
nyatla 37:fc4b4fd6a649 35 NyLPC_TUInt8 hlen;
nyatla 37:fc4b4fd6a649 36 NyLPC_TUInt8 hops;
nyatla 37:fc4b4fd6a649 37 NyLPC_TUInt32 xid;
nyatla 37:fc4b4fd6a649 38 NyLPC_TUInt16 secs;
nyatla 37:fc4b4fd6a649 39 NyLPC_TUInt16 flags;
nyatla 37:fc4b4fd6a649 40 NyLPC_TUInt32 ciaddr;
nyatla 37:fc4b4fd6a649 41 NyLPC_TUInt32 yiaddr;
nyatla 37:fc4b4fd6a649 42 NyLPC_TUInt32 siaddr;
nyatla 37:fc4b4fd6a649 43 NyLPC_TUInt32 giaddr;
nyatla 37:fc4b4fd6a649 44 struct{
nyatla 37:fc4b4fd6a649 45 struct NyLPC_TEthAddr emac;
nyatla 37:fc4b4fd6a649 46 NyLPC_TChar padding[10];
nyatla 37:fc4b4fd6a649 47 }chaddr;
nyatla 37:fc4b4fd6a649 48 NyLPC_TChar sname[64];
nyatla 37:fc4b4fd6a649 49 NyLPC_TChar file[128];
nyatla 37:fc4b4fd6a649 50 }PACK_STRUCT_END;
nyatla 37:fc4b4fd6a649 51
nyatla 37:fc4b4fd6a649 52 #define NyLPC_TDhcpHeader_BOOTREQUEST 1
nyatla 37:fc4b4fd6a649 53 #define NyLPC_TDhcpHeader_BOOTREPLY 2
nyatla 37:fc4b4fd6a649 54
nyatla 37:fc4b4fd6a649 55 #define DHCP_OPT_ID_ROUTER 3
nyatla 37:fc4b4fd6a649 56 #define DHCP_OPT_ID_SERVER_ID 54
nyatla 37:fc4b4fd6a649 57 #define DHCP_OPT_ID_NETMASK 1
nyatla 37:fc4b4fd6a649 58 #define DHCP_OPT_ID_MESSAGETYPE 53
nyatla 37:fc4b4fd6a649 59
nyatla 37:fc4b4fd6a649 60
nyatla 37:fc4b4fd6a649 61
nyatla 37:fc4b4fd6a649 62 /**
nyatla 37:fc4b4fd6a649 63 * DHCPパケットから32bit値を読み出す。
nyatla 37:fc4b4fd6a649 64 * @return
nyatla 37:fc4b4fd6a649 65 * ネットワークオーダー
nyatla 37:fc4b4fd6a649 66 */
nyatla 37:fc4b4fd6a649 67 static NyLPC_TBool getUInt32Option(const NyLPC_TUInt8* i_buf,NyLPC_TUInt16 len,NyLPC_TUInt8 i_id,NyLPC_TUInt32* o_v)
nyatla 37:fc4b4fd6a649 68 {
nyatla 37:fc4b4fd6a649 69 const NyLPC_TUInt8* p=i_buf+sizeof(struct NyLPC_TDhcpHeader)+4;
nyatla 37:fc4b4fd6a649 70 while(*p!=0x00 && p<(i_buf+len-5)){
nyatla 37:fc4b4fd6a649 71 if(*p==i_id){
nyatla 37:fc4b4fd6a649 72 if(*(p+1)==4){
nyatla 37:fc4b4fd6a649 73 *o_v=*((NyLPC_TUInt32*)(p+2));
nyatla 37:fc4b4fd6a649 74 return NyLPC_TBool_TRUE;
nyatla 37:fc4b4fd6a649 75 }
nyatla 37:fc4b4fd6a649 76 }else{
nyatla 37:fc4b4fd6a649 77 p+=(*(p+1))+2;
nyatla 37:fc4b4fd6a649 78 }
nyatla 37:fc4b4fd6a649 79 }
nyatla 37:fc4b4fd6a649 80 return NyLPC_TBool_FALSE;
nyatla 37:fc4b4fd6a649 81 }
nyatla 37:fc4b4fd6a649 82 static NyLPC_TBool getUInt8Option(const NyLPC_TUInt8* i_buf,NyLPC_TUInt16 len,NyLPC_TUInt8 i_id,NyLPC_TUInt8* o_v)
nyatla 37:fc4b4fd6a649 83 {
nyatla 37:fc4b4fd6a649 84 const NyLPC_TUInt8* p=i_buf+sizeof(struct NyLPC_TDhcpHeader)+4;
nyatla 37:fc4b4fd6a649 85 while(*p!=0x00 && p<(i_buf+len-5)){
nyatla 37:fc4b4fd6a649 86 if(*p==i_id){
nyatla 37:fc4b4fd6a649 87 if(*(p+1)==1){
nyatla 37:fc4b4fd6a649 88 *o_v=*(p+2);
nyatla 37:fc4b4fd6a649 89 return NyLPC_TBool_TRUE;
nyatla 37:fc4b4fd6a649 90 }
nyatla 37:fc4b4fd6a649 91 }else{
nyatla 37:fc4b4fd6a649 92 p+=(*(p+1))+2;
nyatla 37:fc4b4fd6a649 93 }
nyatla 37:fc4b4fd6a649 94 }
nyatla 37:fc4b4fd6a649 95 return NyLPC_TBool_FALSE;
nyatla 37:fc4b4fd6a649 96 }
nyatla 56:d38b6ce8c63b 97 static NyLPC_TBool NyLPC_TDhcpHeader_parseDHCPOFFER(const NyLPC_TUInt8* i_buf,NyLPC_TUInt16 i_len,NyLPC_TUInt32 i_xid,NyLPC_TcDhcpClient_t* i_inst)
nyatla 37:fc4b4fd6a649 98 {
nyatla 37:fc4b4fd6a649 99 struct NyLPC_TDhcpHeader* p=(struct NyLPC_TDhcpHeader*)i_buf;
nyatla 37:fc4b4fd6a649 100 //XIDのチェック
nyatla 37:fc4b4fd6a649 101 if(p->xid!=NyLPC_HTONL(i_xid)){
nyatla 37:fc4b4fd6a649 102 return NyLPC_TBool_FALSE;
nyatla 37:fc4b4fd6a649 103 }
nyatla 56:d38b6ce8c63b 104 //OFFERのclient IPアドレスをresultへ保存情報の保存
nyatla 56:d38b6ce8c63b 105 i_inst->_result->ip_addr.v=p->yiaddr;
nyatla 56:d38b6ce8c63b 106 //SERVER IDを保存
nyatla 56:d38b6ce8c63b 107 if(!getUInt32Option(i_buf,i_len,DHCP_OPT_ID_SERVER_ID,&i_inst->_offerserver.v)){
nyatla 37:fc4b4fd6a649 108 return NyLPC_TBool_FALSE;
nyatla 37:fc4b4fd6a649 109 }
nyatla 37:fc4b4fd6a649 110 return NyLPC_TBool_TRUE;
nyatla 37:fc4b4fd6a649 111 }
nyatla 37:fc4b4fd6a649 112
nyatla 37:fc4b4fd6a649 113 static NyLPC_TBool NyLPC_TDhcpHeader_parseDHCPACK(const NyLPC_TUInt8* i_buf,NyLPC_TUInt16 i_len,NyLPC_TUInt32 i_xid,NyLPC_TcIPv4Config_t* result)
nyatla 37:fc4b4fd6a649 114 {
nyatla 37:fc4b4fd6a649 115 struct NyLPC_TDhcpHeader* p=(struct NyLPC_TDhcpHeader*)i_buf;
nyatla 37:fc4b4fd6a649 116 //XIDのチェック
nyatla 37:fc4b4fd6a649 117 if(p->xid!=NyLPC_HTONL(i_xid)){
nyatla 37:fc4b4fd6a649 118 return NyLPC_TBool_FALSE;
nyatla 37:fc4b4fd6a649 119 }
nyatla 37:fc4b4fd6a649 120 if(!getUInt32Option(i_buf,i_len,DHCP_OPT_ID_ROUTER,&result->dr_addr.v)){
nyatla 37:fc4b4fd6a649 121 result->dr_addr=NyLPC_TIPv4Addr_ZERO;
nyatla 37:fc4b4fd6a649 122 }
nyatla 37:fc4b4fd6a649 123 if(!getUInt32Option(i_buf,i_len,DHCP_OPT_ID_NETMASK,&result->netmask.v)){
nyatla 37:fc4b4fd6a649 124 result->netmask=NyLPC_TIPv4Addr_ZERO;
nyatla 37:fc4b4fd6a649 125 }
nyatla 37:fc4b4fd6a649 126 result->ip_addr.v=p->yiaddr;
nyatla 37:fc4b4fd6a649 127 return NyLPC_TBool_TRUE;
nyatla 37:fc4b4fd6a649 128 }
nyatla 37:fc4b4fd6a649 129
nyatla 37:fc4b4fd6a649 130 static void NyLPC_TDhcpHeader_setDHCPDISCOVER(char* i_buf,NyLPC_TUInt32 i_xid,const struct NyLPC_TEthAddr* emac,NyLPC_TUInt16* o_len)
nyatla 37:fc4b4fd6a649 131 {
nyatla 37:fc4b4fd6a649 132 struct NyLPC_TDhcpHeader* p=(struct NyLPC_TDhcpHeader*)i_buf;
nyatla 37:fc4b4fd6a649 133 memset(i_buf,0,sizeof(struct NyLPC_TDhcpHeader));
nyatla 37:fc4b4fd6a649 134 p->op=NyLPC_TDhcpHeader_BOOTREQUEST;
nyatla 37:fc4b4fd6a649 135 p->htype=1;
nyatla 37:fc4b4fd6a649 136 p->hlen=6;
nyatla 37:fc4b4fd6a649 137 p->xid=NyLPC_HTONL(i_xid);
nyatla 37:fc4b4fd6a649 138 p->chaddr.emac=*emac;
nyatla 56:d38b6ce8c63b 139 p->flags=NyLPC_HTONS(0x8000);
nyatla 37:fc4b4fd6a649 140 memcpy(i_buf+sizeof(struct NyLPC_TDhcpHeader),
nyatla 37:fc4b4fd6a649 141 "\x63\x82\x53\x63" //4
nyatla 37:fc4b4fd6a649 142 "\x35\x01\x01" //3 MESSAGE TYPE
nyatla 37:fc4b4fd6a649 143 "\x37\x03\x01\x03\x06" //5 REQUEST LIST(1,3,6)
nyatla 56:d38b6ce8c63b 144 "\x3d\x07\x01\x00\x00\x00\x00\x00\x00" //9 CLIENT INDIFIRE
nyatla 37:fc4b4fd6a649 145 "\xff",4+3+5+9+1);
nyatla 37:fc4b4fd6a649 146 //emacの上書き
nyatla 56:d38b6ce8c63b 147 memcpy((i_buf+sizeof(struct NyLPC_TDhcpHeader)+4+3+5+3),emac->addr,6);
nyatla 37:fc4b4fd6a649 148 //送信するパケットの長さ
nyatla 37:fc4b4fd6a649 149 *o_len=sizeof(struct NyLPC_TDhcpHeader)+4+3+5+9+1;
nyatla 37:fc4b4fd6a649 150 return;
nyatla 37:fc4b4fd6a649 151 }
nyatla 56:d38b6ce8c63b 152 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)
nyatla 37:fc4b4fd6a649 153 {
nyatla 37:fc4b4fd6a649 154 struct NyLPC_TDhcpHeader* p=(struct NyLPC_TDhcpHeader*)i_buf;
nyatla 37:fc4b4fd6a649 155 memset(i_buf,0,sizeof(struct NyLPC_TDhcpHeader));
nyatla 37:fc4b4fd6a649 156 p->op=NyLPC_TDhcpHeader_BOOTREQUEST;
nyatla 37:fc4b4fd6a649 157 p->htype=1;
nyatla 37:fc4b4fd6a649 158 p->hlen=6;
nyatla 37:fc4b4fd6a649 159 p->xid=NyLPC_HTONL(i_xid);
nyatla 37:fc4b4fd6a649 160 p->chaddr.emac=*emac;
nyatla 56:d38b6ce8c63b 161 p->flags=NyLPC_HTONS(0x8000);
nyatla 37:fc4b4fd6a649 162 memcpy(i_buf+sizeof(struct NyLPC_TDhcpHeader),
nyatla 37:fc4b4fd6a649 163 "\x63\x82\x53\x63" //4
nyatla 37:fc4b4fd6a649 164 "\x35\x01\x03" //3 MESSAGE TYPE
nyatla 37:fc4b4fd6a649 165 "\x37\x03\x01\x03\x06" //5 REQUEST LIST(1,3,6)
nyatla 56:d38b6ce8c63b 166 "\x3d\x07\x01\x00\x00\x00\x00\x00\x00" //9 CLIENT INDIFIRE
nyatla 37:fc4b4fd6a649 167 "\x36\x04\x00\x00\x00\x00" // 6 SERVER ID
nyatla 56:d38b6ce8c63b 168 "\x32\x04\x00\x00\x00\x00" // 6 Reqested IP
nyatla 56:d38b6ce8c63b 169 "\xff",4+3+5+9+6+6+1);
nyatla 37:fc4b4fd6a649 170 //emacの上書き
nyatla 56:d38b6ce8c63b 171 memcpy((i_buf+sizeof(struct NyLPC_TDhcpHeader)+4+3+5+3),emac->addr,6);
nyatla 37:fc4b4fd6a649 172 //sidの上書き
nyatla 37:fc4b4fd6a649 173 memcpy((i_buf+sizeof(struct NyLPC_TDhcpHeader)+4+3+5+9+2),i_sid,4);
nyatla 56:d38b6ce8c63b 174 //reqidの上書き
nyatla 56:d38b6ce8c63b 175 memcpy((i_buf+sizeof(struct NyLPC_TDhcpHeader)+4+3+5+9+6+2),i_reqid,4);
nyatla 37:fc4b4fd6a649 176 //送信するパケットの長さ
nyatla 56:d38b6ce8c63b 177 *o_len=sizeof(struct NyLPC_TDhcpHeader)+4+3+5+9+6+6+1;
nyatla 37:fc4b4fd6a649 178 return;
nyatla 37:fc4b4fd6a649 179 }
nyatla 37:fc4b4fd6a649 180
nyatla 37:fc4b4fd6a649 181
nyatla 37:fc4b4fd6a649 182
nyatla 37:fc4b4fd6a649 183 #define TcDhcpSock_ST_WAIT_OFFER 1
nyatla 37:fc4b4fd6a649 184 #define TcDhcpSock_ST_WAIT_OFFER_OK 2
nyatla 37:fc4b4fd6a649 185 #define TcDhcpSock_ST_WAIT_ACK 3
nyatla 37:fc4b4fd6a649 186 #define TcDhcpSock_ST_WAIT_ACK_OK 4
nyatla 37:fc4b4fd6a649 187 #define TcDhcpSock_ST_DONE_NG 3
nyatla 37:fc4b4fd6a649 188 #define TcDhcpSock_ST_DONE_OK 4
nyatla 37:fc4b4fd6a649 189
nyatla 37:fc4b4fd6a649 190
nyatla 37:fc4b4fd6a649 191
nyatla 37:fc4b4fd6a649 192
nyatla 37:fc4b4fd6a649 193 #define DHCP_OPT_ID_MESSAGETYPE_ACK 5
nyatla 37:fc4b4fd6a649 194 #define DHCP_OPT_ID_MESSAGETYPE_OFFER 2
nyatla 37:fc4b4fd6a649 195
nyatla 109:18f12ac01097 196 static NyLPC_TBool onPacket(NyLPC_TiUdpSocket_t* i_inst,const void* i_buf,const struct NyLPC_TIPv4RxInfo* i_info);
nyatla 37:fc4b4fd6a649 197
nyatla 37:fc4b4fd6a649 198 /**
nyatla 37:fc4b4fd6a649 199 * DHCPソケットを作成します。
nyatla 37:fc4b4fd6a649 200 */
nyatla 37:fc4b4fd6a649 201 NyLPC_TBool NyLPC_cDhcpClient_initialize(NyLPC_TcDhcpClient_t* i_inst)
nyatla 37:fc4b4fd6a649 202 {
nyatla 110:257739f9b31e 203 i_inst->_socket=NyLPC_cNet_createUdpSocketEx(68,NyLPC_TSocketType_UDP_NOBUF);
nyatla 109:18f12ac01097 204 if(i_inst->_socket==NULL){
nyatla 37:fc4b4fd6a649 205 return NyLPC_TBool_FALSE;
nyatla 37:fc4b4fd6a649 206 }
nyatla 109:18f12ac01097 207 i_inst->_socket->_tag=i_inst;
nyatla 109:18f12ac01097 208 NyLPC_iUdpSocket_setBroadcast(i_inst->_socket);
nyatla 109:18f12ac01097 209 NyLPC_iUdpSocket_setOnRxHandler(i_inst->_socket,onPacket);
nyatla 37:fc4b4fd6a649 210 return NyLPC_TBool_TRUE;
nyatla 37:fc4b4fd6a649 211 }
nyatla 37:fc4b4fd6a649 212 void NyLPC_cDhcpClient_finalize(NyLPC_TcDhcpClient_t* i_inst)
nyatla 37:fc4b4fd6a649 213 {
nyatla 109:18f12ac01097 214 NyLPC_iUdpSocket_finalize(i_inst->_socket);
nyatla 37:fc4b4fd6a649 215 }
nyatla 37:fc4b4fd6a649 216 #define TIMEOUT_SOCKAPI_MS 1000
nyatla 37:fc4b4fd6a649 217 #define TIMEOUT_RECVMSG_MS 3000
nyatla 37:fc4b4fd6a649 218
nyatla 37:fc4b4fd6a649 219 /**
nyatla 37:fc4b4fd6a649 220 * ネットワークを更新します。
nyatla 37:fc4b4fd6a649 221 * emac/default_mssを設定したネットワークが必要です。
nyatla 37:fc4b4fd6a649 222 */
nyatla 109:18f12ac01097 223 static NyLPC_TBool NyLPC_cDhcpClient_dhcpRequest(NyLPC_TcDhcpClient_t* i_inst,NyLPC_TcIPv4Config_t* i_result)
nyatla 37:fc4b4fd6a649 224 {
nyatla 37:fc4b4fd6a649 225 char* buf;
nyatla 37:fc4b4fd6a649 226 NyLPC_TcStopwatch_t sw;
nyatla 37:fc4b4fd6a649 227 NyLPC_TUInt16 s;
nyatla 37:fc4b4fd6a649 228 NyLPC_TInt16 hint=sizeof(struct NyLPC_TDhcpHeader)+128;
nyatla 109:18f12ac01097 229 i_inst->txid+=(*(NyLPC_TUInt16*)(&(i_result->eth_mac.addr[2])))+(*(NyLPC_TUInt16*)(&(i_result->eth_mac.addr[4])));
nyatla 109:18f12ac01097 230 i_inst->_result=i_result;
nyatla 109:18f12ac01097 231 buf=NyLPC_iUdpSocket_allocSendBuf(i_inst->_socket,hint,&s,TIMEOUT_SOCKAPI_MS);
nyatla 37:fc4b4fd6a649 232 if(buf==NULL || s<hint){
nyatla 37:fc4b4fd6a649 233 return NyLPC_TBool_FALSE;
nyatla 37:fc4b4fd6a649 234 }
nyatla 109:18f12ac01097 235 NyLPC_TDhcpHeader_setDHCPDISCOVER(buf,i_inst->txid,&i_inst->_result->eth_mac,&s);
nyatla 109:18f12ac01097 236 i_inst->_status=TcDhcpSock_ST_WAIT_OFFER;
nyatla 109:18f12ac01097 237 if(!NyLPC_iUdpSocket_psend(i_inst->_socket,&NyLPC_TIPv4Addr_BROADCAST,67,buf,s)){
nyatla 109:18f12ac01097 238 NyLPC_iUdpSocket_releaseSendBuf(i_inst->_socket,buf);
nyatla 37:fc4b4fd6a649 239 return NyLPC_TBool_FALSE;
nyatla 37:fc4b4fd6a649 240 }
nyatla 37:fc4b4fd6a649 241 NyLPC_cStopwatch_initialize(&sw);
nyatla 37:fc4b4fd6a649 242 NyLPC_cStopwatch_startExpire(&sw,TIMEOUT_RECVMSG_MS);
nyatla 109:18f12ac01097 243 while(i_inst->_status==TcDhcpSock_ST_WAIT_OFFER){
nyatla 37:fc4b4fd6a649 244 if(NyLPC_cStopwatch_isExpired(&sw)){
nyatla 37:fc4b4fd6a649 245 return NyLPC_TBool_FALSE;
nyatla 37:fc4b4fd6a649 246 }
nyatla 37:fc4b4fd6a649 247 }
nyatla 37:fc4b4fd6a649 248 //レスポンスのチェック
nyatla 109:18f12ac01097 249 if(i_inst->_status!=TcDhcpSock_ST_WAIT_OFFER_OK)
nyatla 37:fc4b4fd6a649 250 {
nyatla 37:fc4b4fd6a649 251 return NyLPC_TBool_FALSE;
nyatla 37:fc4b4fd6a649 252 }
nyatla 109:18f12ac01097 253 buf=NyLPC_iUdpSocket_allocSendBuf(i_inst->_socket,hint,&s,TIMEOUT_SOCKAPI_MS);
nyatla 37:fc4b4fd6a649 254 if(buf==NULL || s<hint){
nyatla 37:fc4b4fd6a649 255 return NyLPC_TBool_FALSE;
nyatla 37:fc4b4fd6a649 256 }
nyatla 109:18f12ac01097 257 NyLPC_TDhcpHeader_setDHCPREQUEST(buf,i_inst->txid,&(i_inst->_offerserver),&(i_inst->_result->ip_addr),&i_inst->_result->eth_mac,&s);
nyatla 109:18f12ac01097 258 i_inst->_status=TcDhcpSock_ST_WAIT_ACK;
nyatla 109:18f12ac01097 259 if(!NyLPC_iUdpSocket_psend(i_inst->_socket,&NyLPC_TIPv4Addr_BROADCAST,67,buf,s)){
nyatla 109:18f12ac01097 260 NyLPC_iUdpSocket_releaseSendBuf(i_inst->_socket,buf);
nyatla 37:fc4b4fd6a649 261 return NyLPC_TBool_FALSE;
nyatla 37:fc4b4fd6a649 262 }
nyatla 37:fc4b4fd6a649 263 NyLPC_cStopwatch_startExpire(&sw,TIMEOUT_RECVMSG_MS);
nyatla 109:18f12ac01097 264 while(i_inst->_status==TcDhcpSock_ST_WAIT_ACK){
nyatla 37:fc4b4fd6a649 265 if(NyLPC_cStopwatch_isExpired(&sw)){
nyatla 37:fc4b4fd6a649 266 return NyLPC_TBool_FALSE;
nyatla 37:fc4b4fd6a649 267 }
nyatla 37:fc4b4fd6a649 268 }
nyatla 37:fc4b4fd6a649 269 //レスポンスのチェック
nyatla 109:18f12ac01097 270 if(i_inst->_status!=TcDhcpSock_ST_WAIT_ACK_OK)
nyatla 37:fc4b4fd6a649 271 {
nyatla 37:fc4b4fd6a649 272 return NyLPC_TBool_FALSE;
nyatla 37:fc4b4fd6a649 273 }
nyatla 37:fc4b4fd6a649 274 return NyLPC_TBool_TRUE;
nyatla 37:fc4b4fd6a649 275 }
nyatla 37:fc4b4fd6a649 276
nyatla 37:fc4b4fd6a649 277 /**
nyatla 37:fc4b4fd6a649 278 * NyLPC_TcIPv4Config_tをDHCPで更新します。
nyatla 37:fc4b4fd6a649 279 * この関数をコールする時は、サービスは停止中でなければなりません。
nyatla 37:fc4b4fd6a649 280 * @param i_cfg
nyatla 37:fc4b4fd6a649 281 * 更新するi_cfg構造体。
nyatla 37:fc4b4fd6a649 282 * emac,default_mssは設定済である必要があります。他のフィールド値は不定で構いません。
nyatla 37:fc4b4fd6a649 283 * 更新されるフィールドは、ip,netmast,default_rootの3つです。
nyatla 37:fc4b4fd6a649 284 * @return
nyatla 37:fc4b4fd6a649 285 * 更新に成功した場合TRUE
nyatla 37:fc4b4fd6a649 286 */
nyatla 37:fc4b4fd6a649 287 NyLPC_TBool NyLPC_cDhcpClient_requestAddr(NyLPC_TcDhcpClient_t* i_inst,NyLPC_TcIPv4Config_t* i_cfg,NyLPC_TInt16 i_repeat)
nyatla 37:fc4b4fd6a649 288 {
nyatla 37:fc4b4fd6a649 289 NyLPC_TInt16 i;
nyatla 37:fc4b4fd6a649 290 NyLPC_TBool ret=NyLPC_TBool_FALSE;
nyatla 37:fc4b4fd6a649 291 NyLPC_TcIPv4Config_t c2;
nyatla 37:fc4b4fd6a649 292 //工場出荷時設定でリセットしてIPを0に
nyatla 37:fc4b4fd6a649 293 NyLPC_cIPv4Config_initialzeCopy(&c2,i_cfg);
nyatla 37:fc4b4fd6a649 294 NyLPC_cIPv4Config_setIp(&c2,&NyLPC_TIPv4Addr_ZERO,&NyLPC_TIPv4Addr_ZERO);
nyatla 37:fc4b4fd6a649 295 NyLPC_cIPv4Config_setDefaultRoute(&c2,&NyLPC_TIPv4Addr_ZERO);
nyatla 37:fc4b4fd6a649 296 //netを開始
nyatla 110:257739f9b31e 297 NyLPC_cNet_start(&c2);
nyatla 37:fc4b4fd6a649 298 for(i=i_repeat-1;i>=0;i--){
nyatla 37:fc4b4fd6a649 299 ret=NyLPC_cDhcpClient_dhcpRequest(i_inst,i_cfg);
nyatla 37:fc4b4fd6a649 300 if(ret){
nyatla 37:fc4b4fd6a649 301 break;
nyatla 37:fc4b4fd6a649 302 }
nyatla 37:fc4b4fd6a649 303 }
nyatla 110:257739f9b31e 304 NyLPC_cNet_stop();
nyatla 37:fc4b4fd6a649 305 NyLPC_cIPv4Config_finalize(&c2);
nyatla 37:fc4b4fd6a649 306 return ret;
nyatla 37:fc4b4fd6a649 307 }
nyatla 37:fc4b4fd6a649 308
nyatla 37:fc4b4fd6a649 309
nyatla 37:fc4b4fd6a649 310
nyatla 109:18f12ac01097 311 static NyLPC_TBool onPacket(NyLPC_TiUdpSocket_t* i_inst,const void* i_buf,const struct NyLPC_TIPv4RxInfo* i_info)
nyatla 37:fc4b4fd6a649 312 {
nyatla 37:fc4b4fd6a649 313 NyLPC_TUInt8 mt;//message type
nyatla 109:18f12ac01097 314 NyLPC_TcDhcpClient_t* inst=(NyLPC_TcDhcpClient_t*)i_inst->_tag;
nyatla 37:fc4b4fd6a649 315 struct NyLPC_TDhcpHeader* dnsh=(struct NyLPC_TDhcpHeader*)i_buf;
nyatla 37:fc4b4fd6a649 316 if(i_info->size<sizeof(struct NyLPC_TDhcpHeader)+1){
nyatla 37:fc4b4fd6a649 317 return NyLPC_TBool_FALSE;//DROP
nyatla 37:fc4b4fd6a649 318 }
nyatla 37:fc4b4fd6a649 319 switch(inst->_status)
nyatla 37:fc4b4fd6a649 320 {
nyatla 37:fc4b4fd6a649 321 case TcDhcpSock_ST_WAIT_ACK:
nyatla 37:fc4b4fd6a649 322 if(dnsh->op!=NyLPC_TDhcpHeader_BOOTREPLY){
nyatla 37:fc4b4fd6a649 323 return NyLPC_TBool_FALSE;
nyatla 37:fc4b4fd6a649 324 }
nyatla 37:fc4b4fd6a649 325 if(!getUInt8Option(i_buf,i_info->size,DHCP_OPT_ID_MESSAGETYPE,&mt)){
nyatla 37:fc4b4fd6a649 326 return NyLPC_TBool_FALSE;
nyatla 37:fc4b4fd6a649 327 }
nyatla 37:fc4b4fd6a649 328 if(mt!=DHCP_OPT_ID_MESSAGETYPE_ACK){
nyatla 37:fc4b4fd6a649 329 return NyLPC_TBool_FALSE;
nyatla 37:fc4b4fd6a649 330 }
nyatla 37:fc4b4fd6a649 331 if(!NyLPC_TDhcpHeader_parseDHCPACK(i_buf,i_info->size,inst->txid,inst->_result)){
nyatla 37:fc4b4fd6a649 332 return NyLPC_TBool_FALSE;
nyatla 37:fc4b4fd6a649 333 }
nyatla 37:fc4b4fd6a649 334 inst->_status=TcDhcpSock_ST_WAIT_ACK_OK;
nyatla 37:fc4b4fd6a649 335 break;
nyatla 37:fc4b4fd6a649 336 case TcDhcpSock_ST_WAIT_OFFER:
nyatla 37:fc4b4fd6a649 337 if(dnsh->op!=NyLPC_TDhcpHeader_BOOTREPLY){
nyatla 37:fc4b4fd6a649 338 return NyLPC_TBool_FALSE;
nyatla 37:fc4b4fd6a649 339 }
nyatla 37:fc4b4fd6a649 340 if(!getUInt8Option(i_buf,i_info->size,DHCP_OPT_ID_MESSAGETYPE,&mt)){
nyatla 37:fc4b4fd6a649 341 return NyLPC_TBool_FALSE;
nyatla 37:fc4b4fd6a649 342 }
nyatla 37:fc4b4fd6a649 343 if(mt!=DHCP_OPT_ID_MESSAGETYPE_OFFER){
nyatla 37:fc4b4fd6a649 344 return NyLPC_TBool_FALSE;
nyatla 37:fc4b4fd6a649 345 }
nyatla 56:d38b6ce8c63b 346 if(!NyLPC_TDhcpHeader_parseDHCPOFFER(i_buf,i_info->size,inst->txid,inst)){
nyatla 37:fc4b4fd6a649 347 return NyLPC_TBool_FALSE;
nyatla 37:fc4b4fd6a649 348 }
nyatla 37:fc4b4fd6a649 349 inst->_status=TcDhcpSock_ST_WAIT_OFFER_OK;
nyatla 37:fc4b4fd6a649 350 break;
nyatla 37:fc4b4fd6a649 351 }
nyatla 37:fc4b4fd6a649 352 return NyLPC_TBool_FALSE;
nyatla 37:fc4b4fd6a649 353
nyatla 37:fc4b4fd6a649 354 }