OSDN Git Service

jsonrpcparserにBSTR型を追加
[mimic/MiMicSDK.git] / lib / src / http / json / NyLPC_cJsonRpcParser.h
1 #ifndef NYLPC_CJSONRPCPARSER_H_\r
2 #define NYLPC_CJSONRPCPARSER_H_\r
3 #ifdef __cplusplus\r
4 extern "C" {\r
5 #endif\r
6 \r
7 #include "NyLPC_stdlib.h"\r
8 \r
9 \r
10 #define NyLPC_TJsonRpcErrorCode_PARSE_ERROR                     (-32700)\r
11 #define NyLPC_TJsonRpcErrorCode_INVALID_REQUEST         (-32600)\r
12 #define NyLPC_TJsonRpcErrorCode_METHOD_NOT_FOUND        (-32601)\r
13 #define NyLPC_TJsonRpcErrorCode_INVALID_PARAMS          (-32602)\r
14 #define NyLPC_TJsonRpcErrorCode_INTERNAL_ERROR          (-32603)\r
15 #define NyLPC_TJsonRpcErrorCode_SERVER_ERROR_BASE       (-32000)\r
16 \r
17 \r
18 /** 型定義*/\r
19 union NyLPC_TJsonRpcParserResult;\r
20 \r
21 /**\r
22  * RPC関数ハンドラ型です。\r
23  * @param\r
24  */\r
25 typedef NyLPC_TBool (*NyLPC_TJsonRpcHandler)(const union NyLPC_TJsonRpcParserResult* i_rpc,void* i_param);\r
26 \r
27 /**\r
28  * JSON RPC定義テーブルの一要素。\r
29  * メソッド名とパラメータパターンのセットを定義します。\r
30  * この構造体は配列としてNyLPC_TJsonRpcFunctionTableから参照されます。\r
31  */\r
32 struct NyLPC_TJsonRpcMethodDef\r
33 {\r
34         /**\r
35          * 関数名\r
36          */\r
37         const char* name;\r
38         /**\r
39          * パラメータパターン\r
40          * <ul>\r
41          * <li>s - 文字列</li>\r
42          * <li>i - signed int32</li>\r
43          * <li>u - unsigned int32</li>\r
44          * <li>b - unsigned int8</li>\r
45          * <li>B - BSTR (string ByteArray)</li>\r
46          * </ul>\r
47          */\r
48         const char* param_patt;\r
49         /**\r
50          * 外部のDispatch関数で使われるRPC関数ハンドラ。\r
51          */\r
52         NyLPC_TJsonRpcHandler handler;\r
53 };\r
54 \r
55 \r
56 /**\r
57  * JsonRPCクラスの定義テーブル。\r
58  * JsonRPCは、 [namespace]::[]\r
59  */\r
60 struct NyLPC_TJsonRpcClassDef\r
61 {\r
62         const char* names_pace;\r
63         const char* class_name;\r
64         const struct NyLPC_TJsonRpcMethodDef* functions;\r
65 };\r
66 \r
67 \r
68 \r
69 \r
70 \r
71 #define NyLPC_TJsonRpcParserResult_TYPE_UNKNOWN 0\r
72 #define NyLPC_TJsonRpcParserResult_TYPE_RESULT  1\r
73 #define NyLPC_TJsonRpcParserResult_TYPE_METHOD  2\r
74 \r
75 /** NyLPC_TJsonRpcParserResultが格納できる引数の最大数。パラメータの最大数に等しくなります。 */\r
76 #define NyLPC_TJsonRpcParserResult_NUMBER_OF_PARAM_INDEX 32\r
77 /**\r
78  * NyLPC_TJsonRpcParserResultが格納できるパラメータの総バイト数。\r
79  * (文字数+1)+(uint32|int32)*4+uint8の合計値です。\r
80  */\r
81 #define NyLPC_TJsonRpcParserResult_PARAM_BUF 256\r
82 \r
83 /**\r
84  * JSONRPC構文のパース結果を格納します。\r
85  * 開発メモ\r
86  * 更新する場合は、param_indexまでの構造体のレイアウトを破壊しないようにしてください。\r
87  */\r
88 union NyLPC_TJsonRpcParserResult\r
89 {\r
90         NyLPC_TUInt8    type;                           //タイプ\r
91         /** この構造体は type==NyLPC_TJsonRpcParserResult_TYPE_METHODのときに有効です。 */\r
92         struct{\r
93                 NyLPC_TUInt8    _type;                  //タイプ\r
94                 NyLPC_TUInt8    func_number;    //確定した関数番号。\r
95                 NyLPC_TUInt8    _padding[2];    //パディング\r
96                 NyLPC_TInt32    id;                             //idパラメータの値\r
97                 NyLPC_TChar             param_buf[NyLPC_TJsonRpcParserResult_PARAM_BUF];\r
98                 NyLPC_TUInt8    param_index[NyLPC_TJsonRpcParserResult_NUMBER_OF_PARAM_INDEX];\r
99                 /** 関数の含まれるクラスへのポインタ。 */\r
100                 const struct NyLPC_TJsonRpcClassDef* class_def;\r
101         }method;\r
102         struct{\r
103                 NyLPC_TUInt8    _type;                  //タイプ\r
104                 NyLPC_TUInt8    _padding[3];    //パディング\r
105                 NyLPC_TInt32    id;                             //idパラメータの値\r
106                 NyLPC_TChar             param_buf[NyLPC_TJsonRpcParserResult_PARAM_BUF];\r
107                 NyLPC_TUInt8    param_index[NyLPC_TJsonRpcParserResult_NUMBER_OF_PARAM_INDEX];\r
108         }result;\r
109         struct{\r
110                 NyLPC_TUInt8    _type;          //タイプ\r
111                 NyLPC_TInt8     _padding[3];//\r
112                 NyLPC_TInt32    _id;            //idパラメータの値\r
113                 NyLPC_TChar             param_buf[NyLPC_TJsonRpcParserResult_PARAM_BUF];\r
114                 NyLPC_TUInt8    param_index[NyLPC_TJsonRpcParserResult_NUMBER_OF_PARAM_INDEX];\r
115         }raw;\r
116 };\r
117 /**\r
118  * Resultに格納される結果がハンドラを持っているかを返します。\r
119  */\r
120 #define NyLPC_TJsonRpcParserResult_hasMethodHandler(i_struct) ((i_struct)->method.class_def->functions[(i_struct)->method.func_number].handler!=NULL)\r
121 /**\r
122  * Resultに含まれるハンドラを呼び出します。\r
123  * @param i_struct\r
124  * Result構造体\r
125  * @param i_param\r
126  * ハンドラに引き渡すパラメータ\r
127  * @return\r
128  * FALSEの場合、ループを終了してください。\r
129  */\r
130 #define NyLPC_TJsonRpcParserResult_callMethodHandler(i_struct,i_param) (i_struct)->method.class_def->functions[(i_struct)->method.func_number].handler((i_struct),(i_param))\r
131 \r
132 /**\r
133  * i_idx番目のパラメータをuint32としてo_valへ取り出します。\r
134  */\r
135 NyLPC_TBool NyLPC_TJsonRpcParserResult_getUInt32(const union NyLPC_TJsonRpcParserResult* i_struct, NyLPC_TInt16 i_idx, NyLPC_TUInt32* o_val);\r
136 /**\r
137  * i_idx番目のパラメータをint32としてo_valへ取り出します。\r
138  */\r
139 NyLPC_TBool NyLPC_TJsonRpcParserResult_getInt32(const union NyLPC_TJsonRpcParserResult* i_struct, NyLPC_TInt16 i_idx, NyLPC_TInt32* o_val);\r
140 /**\r
141  * i_idx番目のパラメータをchar[]としてo_valへ取り出します。\r
142  */\r
143 NyLPC_TBool NyLPC_TJsonRpcParserResult_getStr(const union NyLPC_TJsonRpcParserResult* i_struct, NyLPC_TInt16 i_idx, const NyLPC_TChar** o_val, NyLPC_TUInt8* o_len);\r
144 /**\r
145 * i_idx番目のパラメータをuchar[]としてo_valへ取り出します。\r
146 */\r
147 NyLPC_TBool NyLPC_TJsonRpcParserResult_getByteArray(const union NyLPC_TJsonRpcParserResult* i_struct, NyLPC_TInt16 i_idx, const NyLPC_TUInt8** o_val, NyLPC_TUInt8* o_len);\r
148 \r
149 /**\r
150  * i_idx番目のパラメータをuint8としてo_valへ取り出します。\r
151  */\r
152 NyLPC_TBool NyLPC_TJsonRpcParserResult_getByte(const union NyLPC_TJsonRpcParserResult* i_struct, NyLPC_TInt16 i_idx, NyLPC_TUInt8* o_val);\r
153 \r
154 /********************************************************************************\r
155  *\r
156  * NyLPC_TcJsonRpcParser\r
157  *\r
158  ********************************************************************************/\r
159 /**\r
160  * JSONRPCの型定義定数です。\r
161  */\r
162 #define NyLPC_cJsonRpcParser_TYPE_INT32         'd'\r
163 #define NyLPC_cJsonRpcParser_TYPE_UINT32        'u'\r
164 #define NyLPC_cJsonRpcParser_TYPE_STRING        's'\r
165 #define NyLPC_cJsonRpcParser_TYPE_BYTE          'b'\r
166 #define NyLPC_cJsonRpcParser_TYPE_BSTRING       'B'\r
167 \r
168 /**\r
169 * JsonRPCメッセージをパースします。パース出来るメッセージは以下の通りです。\r
170 * クラスは、RPC関数定義テーブルに従ってメッセージを分析し、テーブルに存在する関数のみを返却することができます。\r
171 * <p>\r
172 * メッセージ形式\r
173 * <pre>\r
174 * METHOD:\r
175 * {"method":METHOD,"version":VERSION,"params":PARAMS,"id":ID}\r
176 * METHOD VERSION as string\r
177 * ID as uint32\r
178 * PARAMS as Array of (string|uint32|int32)\r
179 * </pre>\r
180 * </p>\r
181 */\r
182 typedef struct NyLPC_TcJsonRpcParser NyLPC_TcJsonRpcParser_t;\r
183 \r
184 \r
185 \r
186 \r
187 /** パーサの状態値*/\r
188 typedef NyLPC_TUInt8 NyLPC_TcJsonRpcParser_TStatus;\r
189 \r
190 #define NyLPC_TcJsonRpcParser_ST_START                  0x01    //開始ブランケット受信待ち\r
191 #define NyLPC_TcJsonRpcParser_ST_END                    0x02    //終了受信済\r
192 #define NyLPC_TcJsonRpcParser_ST_ERROR                  0x03    //エラー発生\r
193 #define NyLPC_TcJsonRpcParser_ST_NAME_Q                 0x04    //名前クオート受信待ち\r
194 #define NyLPC_TcJsonRpcParser_ST_NAME_STR               0x05    //名前受信中\r
195 #define NyLPC_TcJsonRpcParser_ST_NV_SEP                 0x06    //名前と値のセパレータ待ち\r
196 #define NyLPC_TcJsonRpcParser_ST_VAL                    0x07    //値開始待ち\r
197 #define NyLPC_TcJsonRpcParser_ST_VAL_STR                0x08    //文字列受信\r
198 #define NyLPC_TcJsonRpcParser_ST_VAL_UINT               0x09    //UINT受信中\r
199 #define NyLPC_TcJsonRpcParser_ST_VAL_INT                0x10    //INT受信中\r
200 #define NyLPC_TcJsonRpcParser_ST_NEXT                   0x11    //次のNAMEもしくは終了ブランケット\r
201 #define NyLPC_TcJsonRpcParser_ST_PARAMS                 0x12    //PARAM要素パース中\r
202 \r
203 \r
204 \r
205 /** NyLPC_TcJsonRpcParserの定数値です。 字句解析ワークメモリの長さ。256未満8*n-4の数を指定してください。 */\r
206 #define NyLPC_TcJsonRpcParser_WORK_MAX (48-4)\r
207 \r
208 /**\r
209  * クラス構造体です。\r
210  */\r
211 struct NyLPC_TcJsonRpcParser\r
212 {\r
213         const struct NyLPC_TJsonRpcClassDef** _class_def;\r
214         union{\r
215                 struct{\r
216                         NyLPC_TChar     buf[NyLPC_TcJsonRpcParser_WORK_MAX];    //文字解析メモリ\r
217                         NyLPC_TUInt8    n;                                                                              //字句解析の文字数\r
218                         NyLPC_TUInt8    st;                                                                             //字句解析のサブステータス\r
219                         NyLPC_TUInt8    vt;                                                                             //数字エスケープ解釈のテンポラリ\r
220                         NyLPC_TUInt8    _padding;                                                               //\r
221                 }str;\r
222                 struct{\r
223                         NyLPC_TInt32 v;\r
224                         NyLPC_TInt8     s;\r
225                 }int32;\r
226                 NyLPC_TUInt32 uint32;\r
227         }_work;\r
228         NyLPC_TcJsonRpcParser_TStatus   _st;            //パーサステータス\r
229         NyLPC_TUInt8    _pst;           //PARAMS解析ステータス\r
230         NyLPC_TUInt8    _name_id;       //解析中のNAME_ID\r
231         NyLPC_TUInt8    _pcounter;      //パラメタ解析に使うワークカウンタ\r
232         union NyLPC_TJsonRpcParserResult* _result;//出力格納先\r
233 };\r
234 \r
235 \r
236 \r
237 \r
238 \r
239 \r
240 /**\r
241  * インスタンスを初期化します。\r
242  * @param i_class_def\r
243  * クラステーブルの配列です。NULLで終端します。\r
244  */\r
245 void NyLPC_cJsonRpcParser_initialize(\r
246         NyLPC_TcJsonRpcParser_t* i_inst,\r
247         const struct NyLPC_TJsonRpcClassDef** i_class_def);\r
248 \r
249 #define NyLPC_cJsonRpcParser_finalize(i)\r
250 /**\r
251  * パーサの状態を初期化します。\r
252  * @param i_result\r
253  * パース結果の出力先構造体のアドレスです。\r
254  */\r
255 void NyLPC_cJsonRpcParser_initParser(NyLPC_TcJsonRpcParser_t* i_inst, union NyLPC_TJsonRpcParserResult* i_result);\r
256 /**\r
257  * パーサに文字列を入力します。入力後は、NyLPC_cJsonRpcParser_getStatusでパーサの状態をチェックしてください。\r
258  */\r
259 void NyLPC_cJsonRpcParser_putChar(NyLPC_TcJsonRpcParser_t* i_inst, char i_c);\r
260 \r
261 #define NyLPC_cJsonRpcParser_getStatus(i) ((i)->_st)\r
262 /** クラス定義テーブルを返します。*/\r
263 #define NyLPC_cJsonRpcParser_getClassDef(i) ((i)->_class_def)\r
264 \r
265 \r
266 #ifdef __cplusplus\r
267 }\r
268 #endif /* __cplusplus */\r
269 \r
270 #endif /* NYLPC_CJSONRPCPARSER4_H_ */\r