OSDN Git Service

update libMiMic
[mimic/MiMicSDK.git] / lib / src / netif / NyLPC_iUdpSocket.h
1 /*********************************************************************************
2  * PROJECT: MiMic
3  * --------------------------------------------------------------------------------
4  *
5  * This file is part of MiMic
6  * Copyright (C)2011 Ryo Iizuka
7  *
8  * MiMic is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License as published
10  * by the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  *
21  * For further information please contact.
22  *  http://nyatla.jp/
23  *  <airmail(at)ebony.plala.or.jp> or <nyatla(at)nyatla.jp>
24  *
25  *********************************************************************************/
26 #ifndef NYLPC_IIUDPSOCKET_H_
27 #define NYLPC_IIUDPSOCKET_H_
28
29
30 #include "NyLPC_stdlib.h"
31 #include "NyLPC_os.h"
32 #include "NyLPC_NetIf_ip_types.h"
33 #include "NyLPC_iTcpSocket.h"
34 #ifdef __cplusplus
35 extern "C" {
36 #endif /* __cplusplus */
37
38 typedef struct NyLPC_TiUdpSocket NyLPC_TiUdpSocket_t;
39 /**********************************************************************
40  * Struct
41  **********************************************************************/
42
43 /**
44  * 受信情報を格納する構造体
45  */
46 struct NyLPC_TIPv4RxInfo
47 {
48     NyLPC_TUInt16 size;//パケットサイズ
49     NyLPC_TUInt16 port;//受信ポート
50     NyLPC_TUInt16 peer_port;//PEERポート
51     struct NyLPC_TIPv4Addr ip;//受信IP
52     struct NyLPC_TIPv4Addr peer_ip;//PEERIP
53 };
54
55
56 /**********************************************************************
57  * Event
58  **********************************************************************/
59
60 /**
61  * 受信時に非同期にコールされるハンドラ
62  * UIPサービスタスクが実行する。
63  * @return
64  * TRUEならパケットを受信キューへ追加する。FALSEならパケットを受信キューへ追加しない。
65  */
66 typedef NyLPC_TBool (*NyLPC_TiUdpSocket_onRxHandler)(NyLPC_TiUdpSocket_t* i_inst,const void* i_buf,const struct NyLPC_TIPv4RxInfo* i_info);
67
68 /**
69  * 非同期にコールされるハンドラ。
70  * UIPサービスタスクが実行する。
71  */
72 typedef void (*NyLPC_TiUdpSocket_onPeriodicHandler)(NyLPC_TiUdpSocket_t* i_inst);
73
74 /**********************************************************************
75  * Function
76  **********************************************************************/
77
78
79
80 /**
81  * マルチキャストアドレスに参加する。
82  * @param i_addr
83  * 参加するマルチキャストグループを指定する。
84  * 同じマルチキャスとグループに参加できるのは、システムの中で1つに限られます。
85  * 0を指定した場合、マルチキャスとグループから離脱します。
86  */
87 typedef void (*NyLPC_TiUdpSocket_joinMulticast)(NyLPC_TiUdpSocket_t* i_inst,const struct NyLPC_TIPv4Addr* i_addr);
88
89 /**
90  * ブロードキャストに参加する。
91  */
92 typedef void (*NyLPC_TiUdpSocket_setBroadcast)(NyLPC_TiUdpSocket_t* i_inst);
93
94
95 /**
96  * この関数は、ソケットの受信バッファの読み取り位置と、読み出せるデータサイズを返却します。
97  * 関数はポインターを返却するだけで、バッファの読み取り位置をシークしません。
98  * シークするにはNyLPC_cTcpSocket_pseekを使います。
99  */
100 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);
101 /**
102  * 受信バッファを次のバッファまでシークします。
103  */
104 typedef void (*NyLPC_TiUdpSocket_pseek)(NyLPC_TiUdpSocket_t* i_inst);
105
106 /**
107  * 送信バッファを割り当てて返します。
108  * 割り当てたメモリは、releaseSendBuf関数か、psend関数を成功させて開放する必要があります。
109  * @param i_hint
110  * 取得したいメモリサイズをセットします。
111  * 関数は要求サイズより小さいメモリを返すことがあります。
112  */
113 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);
114
115 typedef void (*NyLPC_TiUdpSocket_releaseSendBuf)(NyLPC_TiUdpSocket_t* i_inst,void* i_buf_ptr);
116
117 /**
118  * 事前にAllocしたTxパケットを送信します。
119  * このAPIはゼロコピー送信をサポートするためのものです。
120  * @param i_buf_ptr
121  * allocSendBufで取得したメモリを指定します。
122  * @return
123  * 関数が失敗した場合、i_buf_ptrは「開放されません。」
124  */
125 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);
126
127 /**
128  * 最大送信サイズは1200バイトです。
129  */
130 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);
131
132 /**
133  * 非同期パケットハンドラを設定する。
134  */
135 typedef void (*NyLPC_TiUdpSocket_setOnRxHandler)(NyLPC_TiUdpSocket_t* i_inst,NyLPC_TiUdpSocket_onRxHandler i_handler);
136
137 /**
138  * 非同期タイマ呼び出しハンドラを設定する。
139  */
140 typedef void (*NyLPC_TiUdpSocket_setOnPeriodicHandler)(NyLPC_TiUdpSocket_t* i_inst,NyLPC_TiUdpSocket_onPeriodicHandler i_handler);
141
142
143 /**
144  * ソケットのローカルIPのアドレスを返す。
145  * 値はuipが動作中のみ有効。
146  */
147 typedef const struct NyLPC_TIPv4Addr* (*NyLPC_TiUdpSocket_getSockIP)(const NyLPC_TiUdpSocket_t* i_inst);
148
149 typedef void (*NyLPC_TiUdpSocket_finalize)(NyLPC_TiUdpSocket_t* i_inst);
150
151
152 /**********************************************************************
153  * Interface
154  **********************************************************************/
155
156 /**
157  */
158 struct NyLPC_TiUdpSocket_Interface
159 {
160         NyLPC_TiUdpSocket_joinMulticast joinMulticast;
161         NyLPC_TiUdpSocket_setBroadcast setBroadcast;
162         NyLPC_TiUdpSocket_precv precv;
163         NyLPC_TiUdpSocket_pseek pseek;
164         NyLPC_TiUdpSocket_allocSendBuf allocSendBuf;
165         NyLPC_TiUdpSocket_releaseSendBuf releaseSendBuf;
166         NyLPC_TiUdpSocket_psend psend;
167         NyLPC_TiUdpSocket_send send;
168         NyLPC_TiUdpSocket_setOnRxHandler setOnRxHandler;
169         NyLPC_TiUdpSocket_setOnPeriodicHandler setOnPeriodicHandler;
170         NyLPC_TiUdpSocket_getSockIP getSockIP;
171         NyLPC_TiUdpSocket_finalize finalize;
172 };
173
174 struct NyLPC_TiUdpSocket
175 {
176         const struct NyLPC_TiUdpSocket_Interface* _interface;
177         void* _tag;
178 };
179
180
181 #define NyLPC_iUdpSocket_joinMulticast(i_inst,i_addr)                                                           ((i_inst)->_interface->joinMulticast((i_inst),(i_addr)))
182 #define NyLPC_iUdpSocket_setBroadcast(i_inst)                                                                           ((i_inst)->_interface->setBroadcast((i_inst)))
183 #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)))
184 #define NyLPC_iUdpSocket_pseek(i_inst)                                                                                          ((i_inst)->_interface->pseek((i_inst)))
185 #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)))
186 #define NyLPC_iUdpSocket_releaseSendBuf(i_inst,i_buf_ptr)                                                       ((i_inst)->_interface->releaseSendBuf((i_inst),(i_buf_ptr)))
187 #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)))
188 #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)))
189 #define NyLPC_iUdpSocket_setOnRxHandler(i_inst,i_handler)                                                       ((i_inst)->_interface->setOnRxHandler((i_inst),(i_handler)))
190 #define NyLPC_iUdpSocket_setOnPeriodicHandler(i_inst,i_handler)                                         ((i_inst)->_interface->setOnPeriodicHandler((i_inst),(i_handler)))
191 #define NyLPC_iUdpSocket_getSockIP(i_inst)                                                                                      ((i_inst)->_interface->getSockIP((i_inst)))
192 #define NyLPC_iUdpSocket_finalize(i_inst)                                                                                       ((i_inst)->_interface->finalize((i_inst)))
193
194
195 #ifdef __cplusplus
196 }
197 #endif /* __cplusplus */
198
199 #endif /* NYLPC_IIUDPSOCKET_H_ */