1 /*---------------------------------------------------------------------------*
2   Project:  Horizon
3   File:     nnet.h
4 
5   Copyright (C)2009 Nintendo Co., Ltd.  All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law.  They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13   $Rev: 14703 $
14  *---------------------------------------------------------------------------*/
15 
16 /**
17  * NNETモジュールの外部参照用定義
18  */
19 
20 #ifndef NNET_H
21 #define NNET_H
22 
23 #include <nn/net/compatible/nos.h>            /* for NOS*** */
24 
25 #ifdef  __cplusplus
26 extern "C" {
27 #endif
28 
29 /*---------------------------------------------------------------------------*
30  * Definitions
31  *---------------------------------------------------------------------------*/
32 
33 #define NNET_ERROR_BASE                          0xfff70000
34 #define NNET_ERROR_CODE(code)                    ((s32)(NNET_ERROR_BASE|(code)))
35 
36 /**
37  * NNETモジュールのエラーコード
38  */
39 enum {
40     NNET_ERR_PARAM        = NNET_ERROR_CODE(1),     /**< パラメータエラー */
41     NNET_ERR_EXIST        = NNET_ERROR_CODE(2),     /**< 既に作成済み */
42     NNET_ERR_NOMEM        = NNET_ERROR_CODE(3),     /**< メモリ不足 */
43     NNET_ERR_RECEIVE      = NNET_ERROR_CODE(4),     /**< 受信失敗 */
44     NNET_ERR_SEND         = NNET_ERROR_CODE(5),     /**< 送信失敗 */
45     NNET_ERR_RESOURCE     = NNET_ERROR_CODE(6),     /**< OSリソース確保失敗 */
46     NNET_ERR_CANCEL       = NNET_ERROR_CODE(7),     /**< キャンセル */
47     NNET_ERR_BUSY         = NNET_ERROR_CODE(8),     /**< バッファフル */
48     NNET_ERR_STATE        = NNET_ERROR_CODE(9),     /**< ステート不正 */
49     NNET_ERR_NONE         = 0                       /**< 正常終了 */
50 };
51 
52 /*---------------------------------------------------------------------------*
53  * Types/Declarations
54  *---------------------------------------------------------------------------*/
55 #define NNET_TMO_WAITFOREVER    (-1L)   /* 無限待ち */
56 
57 #define NNET_ETH_ALEN       6       /* MACアドレス長 */
58 #define NNET_IP_ALEN        4       /* IPv4アドレス長 */
59 
60 #define NNET_MIN(a, b)   (((a) < (b)) ? (a) : (b))    /**< aがbより小さい場合はaを、そうでなければbを返します。 */
61 #define NNET_MAX(a, b)   (((a) > (b)) ? (a) : (b))    /**< aがbより大きい場合はaを、そうでなければbを返します。 */
62 
63 
64 /**
65  *  アロケータ
66  */
67 typedef struct NNETAllocator {
68     void*    (* alloc) (u32 name, u32 size, u32 align); /**< メモリ確保関数 */
69     void     (* free) (u32 name, void* ptr);            /**< メモリ解放関数 */
70 } NNETAllocator;
71 
72 /**
73  *  抽象化インスタンス
74  */
75 typedef struct NNETInstance {
76     struct NNETInstance *lower_ins; /**< 下位インスタンス。0はこのレイヤが最下位であることを示します。 */
77     s32 (* startup) (struct NNETInstance *ins, s32 timeout);    /**< startup関数 */
78     s32 (* cleanup) (struct NNETInstance *ins, s32 timeout);    /**< cleanup関数 */
79     s32 (* abort)   (struct NNETInstance *ins);                 /**< abort関数 */
80 } NNETInstance;
81 
82 /**
83  *  下位レイヤで得た設定をソケットレイヤに渡すための構造体
84  */
85 typedef struct NNETConfig {
86     u8 addr[NNET_IP_ALEN];      /**< IPアドレス */
87     u8 gateway[NNET_IP_ALEN];   /**< デフォルトゲートウエイアドレス */
88     u8 dns1[NNET_IP_ALEN];      /**< プライマリDNSサーバアドレス */
89     u8 dns2[NNET_IP_ALEN];      /**< セカンダリDNSサーバアドレス */
90     s32 mtu;                    /**< MTU */
91 } NNETConfig;
92 
93 
94 #define NNET_L2TYPE_ETHERNET    1   /**< IPパケット送信前にARPなどアドレッシング処理を行い、Ethernetヘッダで送信するタイプのレイヤです。 */
95 #define NNET_L2TYPE_PPP         2   /**< そのままIPパケットを送信するタイプのレイヤです。 */
96 
97 /**
98  *  ソケットレイヤとその下位レイヤのためのAPI
99  */
100 typedef struct NNETEthernetAPI {
101     NNETInstance *ins;
102     s32 (* eth_send) (NNETInstance *ins, u8 *dstMAC, NOSMessageBuf *mbuf);           /**< 送信関数 */
103     s32 (* eth_receive) (NNETInstance *ins, u32 receive_id, NOSMessageBuf **mbuf);   /**< 受信関数 */
104     s32 (* eth_setupper) (NNETInstance *ins, NNETInstance *upper_ins, u32 *receive_id, u16 list[]); /**< 上位インスタンスの情報を下位にセットするための関数 */
105     s32 (* eth_clearupper) (NNETInstance *ins, NNETInstance *upper_ins);             /**< 上位インスタンスの情報を下位から削除するための関数 */
106     u8  eth_type;                                                                    /**< NNET_L2TYPE_XXXX */
107     s32 (* eth_getmacaddr) (NNETInstance *ins, u8 *mac);                             /**< 自MACアドレス取得関数 */
108     s32 (* eth_setmulticast) (NNETInstance *ins, u8 *mac);                           /**< 受信マルチキャストアドレス登録 */
109     s32 (* eth_clearmulticast) (NNETInstance *ins, u8 *mac);                         /**< 受信マルチキャストアドレス削除 */
110     s32 (* eth_cancel_receive) (NNETInstance *ins, u32 receive_id);                  /**< 下位の受信停止関数 */
111     s32 (* eth_getconfig) (NNETInstance *ins, NNETConfig *config);                   /**< 下位の設定取得関数 */
112 } NNETEthernetAPI;
113 
114 
115 /**
116  *  PPPレイヤとその下位レイヤのためのAPI
117  */
118 typedef struct NNETPPPAPI {
119     NNETInstance *ins;
120     s32 (* ppp_send) (NNETInstance *ins, NOSMessageBuf *mbuf);                       /**< 送信関数 */
121     s32 (* ppp_receive) (NNETInstance *ins, NOSMessageBuf **mbuf);                   /**< 受信関数 */
122     s32 (* ppp_setupper) (NNETInstance *ins, NNETInstance *upper_ins);               /**< 上位インスタンスの情報を下位にセットするための関数 */
123     s32 (* ppp_clearupper) (NNETInstance *ins, NNETInstance *upper_ins);             /**< 上位インスタンスの情報を下位から削除するための関数 */
124     s32 (* ppp_cancel_receive) (NNETInstance *ins);                                  /**< 下位の受信停止関数 */
125 } NNETPPPAPI;
126 
127 /*---------------------------------------------------------------------------*
128  * Function Prototypes
129  *---------------------------------------------------------------------------*/
130 extern NNETInstance *NNET_GetBottomIF(const NNETInstance *upper_ins);
131 extern NNETInstance *NNET_GetUpperIFByTop(const NNETInstance *top_ins, const NNETInstance *low_ins);
132 extern NNETInstance *NNET_GetLowerIF(NNETInstance *upper_ins);
133 extern s32 NNET_StartupIF(NNETInstance *ins, s32 timeout);
134 extern s32 NNET_CleanupIF(NNETInstance *ins, s32 timeout);
135 extern s32 NNET_AbortIF(NNETInstance *ins);
136 
137 #ifdef NNET_BIG_ENDIAN
138 #define NNET_Swp2( _d )  d
139 #else
140 #define NNET_Swp2( _d )  ((u16)((((_d)>>8)&0x00FF)|(((_d)<<8)&0xFF00)))
141 #endif
142 extern u16 NNET_Get2( const void* ptr );
143 extern u16 NNET_Set2( void* ptr, u16 dat );
144 extern u16 NNET_And2( void* ptr, u16 dat );
145 extern u16 NNET_Orr2( void* ptr, u16 dat );
146 extern u16 NNET_Inc2( void* ptr );
147 extern u16 NNET_Dec2( void* ptr );
148 extern u16 NNET_Sub2( void* ptr, u16 dat );
149 
150 extern u32 NNET_Get4( const void* ptr );
151 extern u32 NNET_Get4( const void* ptr );
152 extern u32 NNET_Set4( void* ptr, u32 dat );
153 extern u32 NNET_Inc4( void* ptr );
154 extern u32 NNET_Add4( void* ptr, u32 dat );
155 
156 #include <nn/net/compatible/nnet/nnet_ring.h>
157 
158 
159 /*
160     デバッグ用の定義
161 */
162 #include <nn/net/compatible/ndebug.h>
163 
164 #ifdef NDEBUG_ENABLE
165 
166 #define NNET_DBGASSERT(cond)  NDEBUG_Assert(cond)
167 
168 #else  /* NDEBUG_ENABLE */
169 
170 #define NNET_DBGASSERT(cond)
171 
172 #endif  /* NDEBUG_ENABLE */
173 
174 
175 #ifdef NDEBUG_PRINT_ENABLE
176 
177 extern u32 ndebug_mask_nnet;
178 
179 #define NNET_DBGREPORT(level, log)         \
180     do {                                  \
181         if( level & ndebug_mask_nnet ) {   \
182             NDEBUG_Printf ("[NNET] ");    \
183             NDEBUG_Printf log;            \
184             NDEBUG_Printf ("\n");         \
185         }                                 \
186     } while (0)
187 
188 #define NNET_DBGFUNC(level, func)          \
189     do {                                  \
190         if( level & ndebug_mask_nnet ) {   \
191             func;                         \
192         }                                 \
193     } while (0)
194 
195 #else /* NDEBUG_PRINT_ENABLE */
196 
197 #define NNET_DBGREPORT(level, log)
198 #define NNET_DBGFUNC(level, func)
199 
200 #endif /* NDEBUG_PRINT_ENABLE */
201 
202 
203 #ifdef  __cplusplus
204 }
205 #endif
206 
207 /* NNET_H */
208 #endif
209