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 Jun 29 12:56:59 2014 +0000
Revision:
83:d46aa580722a
Parent:
75:2a18df0ef111
Child:
84:a5f5973591d8
MiMicCore-r379??;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nyatla 71:855020258513 1 #include "NyLPC_cJsonRpcParser.h"
nyatla 83:d46aa580722a 2 #include <ctype.h>
nyatla 71:855020258513 3 //
nyatla 71:855020258513 4 // NyLPC_TJsonRpcParserResult
nyatla 71:855020258513 5 //
nyatla 71:855020258513 6
nyatla 71:855020258513 7
nyatla 71:855020258513 8 NyLPC_TBool NyLPC_TJsonRpcParserResult_getUInt32(const union NyLPC_TJsonRpcParserResult* i_struct,NyLPC_TInt16 i_idx,NyLPC_TUInt32* o_val)
nyatla 71:855020258513 9 {
nyatla 75:2a18df0ef111 10 if(i_struct->method.param_index[i_idx]==0xff){
nyatla 75:2a18df0ef111 11 return NyLPC_TBool_FALSE;
nyatla 75:2a18df0ef111 12 }
nyatla 71:855020258513 13 if(i_struct->method.class_def->functions[i_struct->method.func_number].param_patt[i_idx]!=NyLPC_cJsonRpcParser_TYPE_UINT32){
nyatla 71:855020258513 14 return NyLPC_TBool_FALSE;
nyatla 71:855020258513 15 }
nyatla 71:855020258513 16 *o_val = *((NyLPC_TUInt32*)(i_struct->method.param_buf + i_struct->method.param_index[i_idx]));
nyatla 71:855020258513 17 return NyLPC_TBool_TRUE;
nyatla 71:855020258513 18 }
nyatla 71:855020258513 19 NyLPC_TBool NyLPC_TJsonRpcParserResult_getInt32(const union NyLPC_TJsonRpcParserResult* i_struct, NyLPC_TInt16 i_idx, NyLPC_TInt32* o_val)
nyatla 71:855020258513 20 {
nyatla 75:2a18df0ef111 21 if(i_struct->method.param_index[i_idx]==0xff){
nyatla 75:2a18df0ef111 22 return NyLPC_TBool_FALSE;
nyatla 75:2a18df0ef111 23 }
nyatla 71:855020258513 24 if (i_struct->method.class_def->functions[i_struct->method.func_number].param_patt[i_idx] != NyLPC_cJsonRpcParser_TYPE_INT32){
nyatla 71:855020258513 25 return NyLPC_TBool_FALSE;
nyatla 71:855020258513 26 }
nyatla 71:855020258513 27 *o_val = *((NyLPC_TInt32*)(i_struct->method.param_buf + i_struct->method.param_index[i_idx]));
nyatla 71:855020258513 28 return NyLPC_TBool_TRUE;
nyatla 71:855020258513 29 }
nyatla 83:d46aa580722a 30 NyLPC_TBool NyLPC_TJsonRpcParserResult_getStr(const union NyLPC_TJsonRpcParserResult* i_struct, NyLPC_TInt16 i_idx, const NyLPC_TChar** o_val, NyLPC_TUInt8* o_len)
nyatla 71:855020258513 31 {
nyatla 75:2a18df0ef111 32 if(i_struct->method.param_index[i_idx]==0xff){
nyatla 75:2a18df0ef111 33 return NyLPC_TBool_FALSE;
nyatla 75:2a18df0ef111 34 }
nyatla 71:855020258513 35 if (i_struct->method.class_def->functions[i_struct->method.func_number].param_patt[i_idx] != NyLPC_cJsonRpcParser_TYPE_STRING){
nyatla 71:855020258513 36 return NyLPC_TBool_FALSE;
nyatla 71:855020258513 37 }
nyatla 83:d46aa580722a 38 *o_val = ((NyLPC_TChar*)(i_struct->method.param_buf + i_struct->method.param_index[i_idx]+1));
nyatla 83:d46aa580722a 39 if (o_len){
nyatla 83:d46aa580722a 40 *o_len = *((NyLPC_TUInt8*)(*o_val-1));
nyatla 83:d46aa580722a 41 }
nyatla 71:855020258513 42 return NyLPC_TBool_TRUE;
nyatla 71:855020258513 43 }
nyatla 83:d46aa580722a 44 NyLPC_TBool NyLPC_TJsonRpcParserResult_getByteArray(const union NyLPC_TJsonRpcParserResult* i_struct, NyLPC_TInt16 i_idx, const NyLPC_TUInt8** o_val, NyLPC_TUInt8* o_len)
nyatla 83:d46aa580722a 45 {
nyatla 83:d46aa580722a 46 if (i_struct->method.param_index[i_idx] == 0xff){
nyatla 83:d46aa580722a 47 return NyLPC_TBool_FALSE;
nyatla 83:d46aa580722a 48 }
nyatla 83:d46aa580722a 49 if (i_struct->method.class_def->functions[i_struct->method.func_number].param_patt[i_idx] != NyLPC_cJsonRpcParser_TYPE_BSTRING){
nyatla 83:d46aa580722a 50 return NyLPC_TBool_FALSE;
nyatla 83:d46aa580722a 51 }
nyatla 83:d46aa580722a 52 *o_val = ((NyLPC_TUInt8*)(i_struct->method.param_buf + i_struct->method.param_index[i_idx] + 1));
nyatla 83:d46aa580722a 53 if (o_len){
nyatla 83:d46aa580722a 54 *o_len = *((NyLPC_TUInt8*)(*o_val - 1));
nyatla 83:d46aa580722a 55 }
nyatla 83:d46aa580722a 56 return NyLPC_TBool_TRUE;
nyatla 83:d46aa580722a 57 }
nyatla 83:d46aa580722a 58
nyatla 71:855020258513 59 NyLPC_TBool NyLPC_TJsonRpcParserResult_getByte(const union NyLPC_TJsonRpcParserResult* i_struct, NyLPC_TInt16 i_idx, NyLPC_TUInt8* o_val)
nyatla 71:855020258513 60 {
nyatla 75:2a18df0ef111 61 if(i_struct->method.param_index[i_idx]==0xff){
nyatla 75:2a18df0ef111 62 return NyLPC_TBool_FALSE;
nyatla 75:2a18df0ef111 63 }
nyatla 71:855020258513 64 if (i_struct->method.class_def->functions[i_struct->method.func_number].param_patt[i_idx] != NyLPC_cJsonRpcParser_TYPE_BYTE){
nyatla 71:855020258513 65 return NyLPC_TBool_FALSE;
nyatla 71:855020258513 66 }
nyatla 71:855020258513 67 *o_val = *(i_struct->method.param_buf + i_struct->method.param_index[i_idx]);
nyatla 71:855020258513 68 return NyLPC_TBool_TRUE;
nyatla 71:855020258513 69 }
nyatla 71:855020258513 70
nyatla 71:855020258513 71
nyatla 71:855020258513 72 //
nyatla 71:855020258513 73 // NyLPC_cJsonRpcParser
nyatla 71:855020258513 74 //
nyatla 71:855020258513 75
nyatla 71:855020258513 76
nyatla 71:855020258513 77
nyatla 71:855020258513 78 #define NAME_ID_UNKNOWN 0
nyatla 71:855020258513 79 #define NAME_ID_VERSION 1
nyatla 71:855020258513 80 #define NAME_ID_METHOD 2
nyatla 71:855020258513 81 #define NAME_ID_RESULT 3
nyatla 71:855020258513 82 #define NAME_ID_PARAMS 4
nyatla 71:855020258513 83 #define NAME_ID_ID 5
nyatla 71:855020258513 84
nyatla 71:855020258513 85 const struct NyLPC_TTextIdTbl method_name_tbl[]=
nyatla 71:855020258513 86 {
nyatla 71:855020258513 87 {"version",NAME_ID_VERSION},
nyatla 71:855020258513 88 {"method",NAME_ID_METHOD},
nyatla 71:855020258513 89 {"params",NAME_ID_PARAMS},
nyatla 71:855020258513 90 {"id",NAME_ID_ID},
nyatla 71:855020258513 91 {NULL,NAME_ID_UNKNOWN}
nyatla 71:855020258513 92 };
nyatla 71:855020258513 93
nyatla 71:855020258513 94
nyatla 71:855020258513 95 static const struct NyLPC_TJsonRpcClassDef* findFunction(const struct NyLPC_TJsonRpcClassDef** i_tbl, const NyLPC_TChar* i_method_path, NyLPC_TUInt8* o_function_idx);
nyatla 71:855020258513 96 static void putchar_params(NyLPC_TcJsonRpcParser_t* i_inst, char i_c);
nyatla 71:855020258513 97 static NyLPC_TUInt8 valTerminator2St(NyLPC_TChar i_c);
nyatla 71:855020258513 98
nyatla 71:855020258513 99
nyatla 71:855020258513 100 void NyLPC_cJsonRpcParser_initialize(
nyatla 71:855020258513 101 NyLPC_TcJsonRpcParser_t* i_inst,
nyatla 71:855020258513 102 const struct NyLPC_TJsonRpcClassDef** i_class_def)
nyatla 71:855020258513 103 {
nyatla 71:855020258513 104 i_inst->_class_def=i_class_def;
nyatla 71:855020258513 105 }
nyatla 71:855020258513 106 void NyLPC_cJsonRpcParser_initParser(NyLPC_TcJsonRpcParser_t* i_inst,union NyLPC_TJsonRpcParserResult* i_result)
nyatla 71:855020258513 107 {
nyatla 71:855020258513 108 i_inst->_result=i_result;
nyatla 71:855020258513 109 i_result->type=NyLPC_TJsonRpcParserResult_TYPE_UNKNOWN;
nyatla 71:855020258513 110 i_inst->_st = NyLPC_TcJsonRpcParser_ST_START;
nyatla 71:855020258513 111 i_inst->_pcounter = 0;
nyatla 71:855020258513 112 memset(i_result->raw.param_index, 0xff, NyLPC_TJsonRpcParserResult_NUMBER_OF_PARAM_INDEX);
nyatla 71:855020258513 113 }
nyatla 71:855020258513 114
nyatla 71:855020258513 115
nyatla 71:855020258513 116
nyatla 71:855020258513 117 static const struct NyLPC_TJsonRpcClassDef* findFunction(const struct NyLPC_TJsonRpcClassDef** i_tbl,const NyLPC_TChar* i_method_path,NyLPC_TUInt8* o_function_idx)
nyatla 71:855020258513 118 {
nyatla 71:855020258513 119 const NyLPC_TChar* ns;
nyatla 71:855020258513 120 const NyLPC_TChar* class_;
nyatla 71:855020258513 121 const NyLPC_TChar* method;
nyatla 71:855020258513 122 NyLPC_TUInt8 fidx;
nyatla 71:855020258513 123 int l;
nyatla 71:855020258513 124 //namespaceの抽出
nyatla 71:855020258513 125 ns=i_method_path;
nyatla 71:855020258513 126 class_=strchr(i_method_path,':');
nyatla 71:855020258513 127 if(class_==NULL){
nyatla 71:855020258513 128 return NULL;
nyatla 71:855020258513 129 }
nyatla 71:855020258513 130 class_++;
nyatla 71:855020258513 131 //functionの抽出
nyatla 71:855020258513 132 method=strchr(class_,':');
nyatla 71:855020258513 133 if(method==NULL){
nyatla 71:855020258513 134 return NULL;
nyatla 71:855020258513 135 }
nyatla 71:855020258513 136 method++;
nyatla 71:855020258513 137 while((*i_tbl)!=NULL){
nyatla 71:855020258513 138 l=class_-ns-1;
nyatla 71:855020258513 139 if(strncmp((*i_tbl)->names_pace,ns,l)!=0 || (*(ns+l))!=':'){
nyatla 71:855020258513 140 i_tbl++;
nyatla 71:855020258513 141 continue;
nyatla 71:855020258513 142 }
nyatla 71:855020258513 143 l=method-class_-1;
nyatla 71:855020258513 144 if(strncmp((*i_tbl)->class_name,class_,l)!=0 || (*(class_+l))!=':'){
nyatla 71:855020258513 145 i_tbl++;
nyatla 71:855020258513 146 continue;
nyatla 71:855020258513 147 }
nyatla 71:855020258513 148 fidx=0;
nyatla 71:855020258513 149 while(((*i_tbl)->functions+fidx)->name!=NULL){
nyatla 71:855020258513 150 if(strcmp(((*i_tbl)->functions+fidx)->name,method)!=0){
nyatla 71:855020258513 151 fidx++;
nyatla 71:855020258513 152 continue;
nyatla 71:855020258513 153 }
nyatla 71:855020258513 154 *o_function_idx=fidx;
nyatla 71:855020258513 155 return (*i_tbl);
nyatla 71:855020258513 156 }
nyatla 71:855020258513 157 break;
nyatla 71:855020258513 158 }
nyatla 71:855020258513 159 return NULL;
nyatla 71:855020258513 160 }
nyatla 83:d46aa580722a 161 /**
nyatla 83:d46aa580722a 162 * putchar_paramのステータス値
nyatla 83:d46aa580722a 163 */
nyatla 83:d46aa580722a 164 #define PARAM_ST_START 1
nyatla 83:d46aa580722a 165 #define PARAM_ST_VAL 2
nyatla 83:d46aa580722a 166 #define PARAM_ST_INT 3
nyatla 83:d46aa580722a 167 #define PARAM_ST_UINT 4
nyatla 83:d46aa580722a 168 #define PARAM_ST_STR 5
nyatla 83:d46aa580722a 169 #define PARAM_ST_STR_ESCAPE_HEX 6
nyatla 83:d46aa580722a 170 #define PARAM_ST_STR_ESCAPE 7
nyatla 83:d46aa580722a 171 #define PARAM_ST_BYTE 8
nyatla 83:d46aa580722a 172 #define PARAM_ST_BSTR 9
nyatla 83:d46aa580722a 173 #define PARAM_ST_NEXT 11
nyatla 83:d46aa580722a 174
nyatla 83:d46aa580722a 175
nyatla 83:d46aa580722a 176 /**
nyatla 83:d46aa580722a 177 * シングルエスケープ文字列をエスケープされた文字列へ変換します。
nyatla 83:d46aa580722a 178 */
nyatla 83:d46aa580722a 179 static NyLPC_TBool convertSingleCharEscape(const char i_s, char* o_out)
nyatla 83:d46aa580722a 180 {
nyatla 83:d46aa580722a 181 switch (i_s){
nyatla 83:d46aa580722a 182 case '0': *o_out = '\0'; break;
nyatla 83:d46aa580722a 183 case 'r': *o_out = '\r'; break;
nyatla 83:d46aa580722a 184 case 'n': *o_out = '\n'; break;
nyatla 83:d46aa580722a 185 case 't': *o_out = '\t'; break;
nyatla 83:d46aa580722a 186 case '\'': *o_out = '\''; break;
nyatla 83:d46aa580722a 187 case '\"': *o_out = '\"'; break;
nyatla 83:d46aa580722a 188 case '\\': *o_out = '\\'; break;
nyatla 83:d46aa580722a 189 default:
nyatla 83:d46aa580722a 190 return NyLPC_TBool_FALSE;
nyatla 83:d46aa580722a 191 }
nyatla 83:d46aa580722a 192 return NyLPC_TBool_TRUE;
nyatla 83:d46aa580722a 193 }
nyatla 83:d46aa580722a 194 /**
nyatla 83:d46aa580722a 195 * 符号付整数値を構成する文字セットであるかを返します。
nyatla 83:d46aa580722a 196 */
nyatla 83:d46aa580722a 197 static NyLPC_TBool isSignedCharSet(const char i_c)
nyatla 83:d46aa580722a 198 {
nyatla 83:d46aa580722a 199 return (strchr("-0123456789", i_c) != NULL);
nyatla 83:d46aa580722a 200 }
nyatla 83:d46aa580722a 201 /**
nyatla 83:d46aa580722a 202 * 整数を構成する文字セットであるかを返します。
nyatla 83:d46aa580722a 203 */
nyatla 83:d46aa580722a 204 #define isUnSignedCharSet(i_c) isdigit(i_c)
nyatla 71:855020258513 205
nyatla 71:855020258513 206 static void putchar_params(NyLPC_TcJsonRpcParser_t* i_inst, char i_c)
nyatla 71:855020258513 207 {
nyatla 71:855020258513 208 switch (i_inst->_pst){
nyatla 71:855020258513 209 case PARAM_ST_START:
nyatla 71:855020258513 210 if (i_c == '['){
nyatla 71:855020258513 211 //パース開始
nyatla 71:855020258513 212 i_inst->_work.str.n = 0;
nyatla 71:855020258513 213 i_inst->_pst = PARAM_ST_VAL;
nyatla 71:855020258513 214 return;
nyatla 71:855020258513 215 }
nyatla 71:855020258513 216 if (strchr(" ", i_c) != NULL){
nyatla 71:855020258513 217 //無視
nyatla 71:855020258513 218 return;
nyatla 71:855020258513 219 }
nyatla 71:855020258513 220 //エラー
nyatla 71:855020258513 221 goto ERROR;
nyatla 71:855020258513 222 case PARAM_ST_VAL:
nyatla 71:855020258513 223 if (i_inst->_pcounter >= NyLPC_TJsonRpcParserResult_NUMBER_OF_PARAM_INDEX){
nyatla 71:855020258513 224 goto ERROR;
nyatla 71:855020258513 225 }
nyatla 71:855020258513 226 if (strchr(" ", i_c) != NULL){
nyatla 71:855020258513 227 //無視
nyatla 71:855020258513 228 return;
nyatla 71:855020258513 229 }
nyatla 71:855020258513 230 if (strchr("]", i_c) != NULL){
nyatla 71:855020258513 231 i_inst->_st = NyLPC_TcJsonRpcParser_ST_NEXT;
nyatla 71:855020258513 232 i_inst->_pcounter = 0xff;
nyatla 71:855020258513 233 return;
nyatla 71:855020258513 234 }
nyatla 71:855020258513 235 switch (i_inst->_result->method.class_def->functions[i_inst->_result->method.func_number].param_patt[i_inst->_pcounter]){
nyatla 71:855020258513 236 case NyLPC_cJsonRpcParser_TYPE_INT32:
nyatla 83:d46aa580722a 237 if(!isSignedCharSet(i_c)){
nyatla 71:855020258513 238 goto ERROR;
nyatla 71:855020258513 239 }
nyatla 71:855020258513 240 i_inst->_pst = PARAM_ST_INT;
nyatla 71:855020258513 241 i_inst->_work.int32.s = i_c == '-' ? -1 : 1;
nyatla 71:855020258513 242 i_inst->_work.int32.v = i_c == '-' ? 0 : (i_c - '0');
nyatla 71:855020258513 243 i_inst->_work.str.n = ((i_inst->_work.str.n + 3) / 4) * 4;
nyatla 71:855020258513 244 break;
nyatla 71:855020258513 245 case NyLPC_cJsonRpcParser_TYPE_UINT32:
nyatla 83:d46aa580722a 246 if (!isUnSignedCharSet(i_c)){
nyatla 71:855020258513 247 goto ERROR;
nyatla 71:855020258513 248 }
nyatla 71:855020258513 249 i_inst->_pst = PARAM_ST_UINT;
nyatla 71:855020258513 250 i_inst->_work.uint32 = (i_c - '0');
nyatla 71:855020258513 251 //開始位置を4バイト境界に
nyatla 71:855020258513 252 i_inst->_work.str.n = ((i_inst->_work.str.n + 3) / 4) * 4;
nyatla 71:855020258513 253 break;
nyatla 71:855020258513 254 case NyLPC_cJsonRpcParser_TYPE_BYTE:
nyatla 83:d46aa580722a 255 if (!isUnSignedCharSet(i_c)){
nyatla 71:855020258513 256 goto ERROR;
nyatla 71:855020258513 257 }
nyatla 71:855020258513 258 i_inst->_pst = PARAM_ST_BYTE;
nyatla 71:855020258513 259 i_inst->_work.uint32 = (i_c - '0');
nyatla 71:855020258513 260 break;
nyatla 71:855020258513 261 case NyLPC_cJsonRpcParser_TYPE_STRING:
nyatla 71:855020258513 262 if (i_c != '"'){
nyatla 71:855020258513 263 goto ERROR;
nyatla 71:855020258513 264 }
nyatla 83:d46aa580722a 265 i_inst->_result->method.param_index[i_inst->_pcounter] = i_inst->_work.str.n;
nyatla 83:d46aa580722a 266 i_inst->_pcounter++;
nyatla 83:d46aa580722a 267 if (i_inst->_work.str.n >= NyLPC_TJsonRpcParserResult_PARAM_BUF){
nyatla 83:d46aa580722a 268 goto ERROR;
nyatla 83:d46aa580722a 269 }
nyatla 83:d46aa580722a 270 i_inst->_work.str.n++;//文字数の記憶領域
nyatla 71:855020258513 271 i_inst->_pst = PARAM_ST_STR;
nyatla 83:d46aa580722a 272 return;//開始時に1バイト予約するから手順が違う
nyatla 83:d46aa580722a 273 case NyLPC_cJsonRpcParser_TYPE_BSTRING:
nyatla 83:d46aa580722a 274 if (i_c != '"'){
nyatla 83:d46aa580722a 275 goto ERROR;
nyatla 83:d46aa580722a 276 }
nyatla 83:d46aa580722a 277 i_inst->_result->method.param_index[i_inst->_pcounter] = i_inst->_work.str.n;
nyatla 83:d46aa580722a 278 i_inst->_pcounter++;
nyatla 83:d46aa580722a 279 if (i_inst->_work.str.n >= NyLPC_TJsonRpcParserResult_PARAM_BUF){
nyatla 83:d46aa580722a 280 goto ERROR;
nyatla 83:d46aa580722a 281 }
nyatla 83:d46aa580722a 282 i_inst->_work.str.n++;//文字数の記憶領域
nyatla 83:d46aa580722a 283 i_inst->_pst = PARAM_ST_BSTR;
nyatla 83:d46aa580722a 284 i_inst->_work.str.st = 0;
nyatla 83:d46aa580722a 285 return;//開始時に1バイト予約するから手順が違う
nyatla 71:855020258513 286 default:
nyatla 71:855020258513 287 goto ERROR;
nyatla 71:855020258513 288 }
nyatla 71:855020258513 289 i_inst->_result->method.param_index[i_inst->_pcounter] = i_inst->_work.str.n;
nyatla 71:855020258513 290 i_inst->_pcounter++;
nyatla 71:855020258513 291 return;
nyatla 83:d46aa580722a 292 case PARAM_ST_STR_ESCAPE_HEX:
nyatla 83:d46aa580722a 293 //16進数構成文字のみ
nyatla 83:d46aa580722a 294 if (!isxdigit((int)i_c)){
nyatla 83:d46aa580722a 295 goto ERROR;
nyatla 83:d46aa580722a 296 }
nyatla 83:d46aa580722a 297 i_inst->_work.str.vt = (i_inst->_work.str.vt << 4) | NyLPC_ctox(i_c);
nyatla 83:d46aa580722a 298 i_inst->_work.str.st++;
nyatla 83:d46aa580722a 299 //2文字目で値を確定
nyatla 83:d46aa580722a 300 if (i_inst->_work.str.st>=2){
nyatla 83:d46aa580722a 301 if (i_inst->_work.str.n >= NyLPC_TJsonRpcParserResult_PARAM_BUF){
nyatla 83:d46aa580722a 302 goto ERROR;
nyatla 83:d46aa580722a 303 }
nyatla 83:d46aa580722a 304 i_inst->_result->method.param_buf[i_inst->_work.str.n] = i_inst->_work.str.vt;
nyatla 83:d46aa580722a 305 i_inst->_work.str.n++;
nyatla 83:d46aa580722a 306 i_inst->_pst = PARAM_ST_STR;
nyatla 83:d46aa580722a 307 }
nyatla 83:d46aa580722a 308 return;
nyatla 83:d46aa580722a 309 case PARAM_ST_STR_ESCAPE:
nyatla 83:d46aa580722a 310 if(i_c=='x'){
nyatla 83:d46aa580722a 311 i_inst->_pst = PARAM_ST_STR_ESCAPE_HEX;
nyatla 83:d46aa580722a 312 i_inst->_work.str.st = 0;
nyatla 83:d46aa580722a 313 i_inst->_work.str.vt = 0;
nyatla 83:d46aa580722a 314 }
nyatla 83:d46aa580722a 315 else{
nyatla 83:d46aa580722a 316 if (i_inst->_work.str.n >= NyLPC_TJsonRpcParserResult_PARAM_BUF){
nyatla 83:d46aa580722a 317 goto ERROR;
nyatla 83:d46aa580722a 318 }
nyatla 83:d46aa580722a 319 if (!convertSingleCharEscape(i_c,&i_inst->_result->method.param_buf[i_inst->_work.str.n])){
nyatla 83:d46aa580722a 320 goto ERROR;
nyatla 83:d46aa580722a 321 }
nyatla 83:d46aa580722a 322 i_inst->_work.str.n++;
nyatla 83:d46aa580722a 323 i_inst->_pst = PARAM_ST_STR;
nyatla 83:d46aa580722a 324 }
nyatla 83:d46aa580722a 325 return;
nyatla 71:855020258513 326 case PARAM_ST_STR:
nyatla 71:855020258513 327 if (i_c == '"'){
nyatla 71:855020258513 328 //完了
nyatla 71:855020258513 329 if (i_inst->_work.str.n >= NyLPC_TJsonRpcParserResult_PARAM_BUF){
nyatla 71:855020258513 330 goto ERROR;
nyatla 71:855020258513 331 }
nyatla 71:855020258513 332 i_inst->_result->method.param_buf[i_inst->_work.str.n] = '\0';
nyatla 71:855020258513 333 i_inst->_work.str.n++;
nyatla 83:d46aa580722a 334 //文字数を更新
nyatla 83:d46aa580722a 335 *((NyLPC_TUInt8*)(i_inst->_result->method.param_buf+i_inst->_result->method.param_index[i_inst->_pcounter - 1])) = i_inst->_work.str.n - i_inst->_result->method.param_index[i_inst->_pcounter - 1]-2;
nyatla 71:855020258513 336 i_inst->_pst = PARAM_ST_NEXT;
nyatla 71:855020258513 337 return;
nyatla 71:855020258513 338 }
nyatla 83:d46aa580722a 339 else if (i_c=='\\'){
nyatla 83:d46aa580722a 340 i_inst->_pst = PARAM_ST_STR_ESCAPE;
nyatla 83:d46aa580722a 341 }
nyatla 71:855020258513 342 else{
nyatla 71:855020258513 343 if (i_inst->_work.str.n >= NyLPC_TJsonRpcParserResult_PARAM_BUF){
nyatla 71:855020258513 344 goto ERROR;
nyatla 71:855020258513 345 }
nyatla 71:855020258513 346 i_inst->_result->method.param_buf[i_inst->_work.str.n] = i_c;
nyatla 71:855020258513 347 i_inst->_work.str.n++;
nyatla 71:855020258513 348 }
nyatla 71:855020258513 349 return;
nyatla 83:d46aa580722a 350 case PARAM_ST_BSTR:
nyatla 83:d46aa580722a 351 if (i_c == '"'){
nyatla 83:d46aa580722a 352 if (i_inst->_work.str.st != 0){
nyatla 83:d46aa580722a 353 goto ERROR;
nyatla 83:d46aa580722a 354 }
nyatla 83:d46aa580722a 355 //文字数を更新
nyatla 83:d46aa580722a 356 *((NyLPC_TUInt8*)(i_inst->_result->method.param_buf + i_inst->_result->method.param_index[i_inst->_pcounter - 1])) = i_inst->_work.str.n - i_inst->_result->method.param_index[i_inst->_pcounter - 1] - 1;
nyatla 83:d46aa580722a 357 i_inst->_pst = PARAM_ST_NEXT;
nyatla 83:d46aa580722a 358 return;
nyatla 83:d46aa580722a 359 }
nyatla 83:d46aa580722a 360 else{
nyatla 83:d46aa580722a 361 //16進数構成文字のみ
nyatla 83:d46aa580722a 362 if (!isxdigit((int)i_c)){
nyatla 83:d46aa580722a 363 goto ERROR;
nyatla 83:d46aa580722a 364 }
nyatla 83:d46aa580722a 365 switch(i_inst->_work.str.st){
nyatla 83:d46aa580722a 366 case 0:
nyatla 83:d46aa580722a 367 i_inst->_work.str.vt = NyLPC_ctox(i_c);
nyatla 83:d46aa580722a 368 i_inst->_work.str.st++;
nyatla 83:d46aa580722a 369 break;
nyatla 83:d46aa580722a 370 case 1:
nyatla 83:d46aa580722a 371 i_inst->_work.str.vt = (i_inst->_work.str.vt << 4) | NyLPC_ctox(i_c);
nyatla 83:d46aa580722a 372 if (i_inst->_work.str.n >= NyLPC_TJsonRpcParserResult_PARAM_BUF){
nyatla 83:d46aa580722a 373 goto ERROR;
nyatla 83:d46aa580722a 374 }
nyatla 83:d46aa580722a 375 i_inst->_result->method.param_buf[i_inst->_work.str.n] = i_inst->_work.str.vt;
nyatla 83:d46aa580722a 376 i_inst->_work.str.n++;
nyatla 83:d46aa580722a 377 i_inst->_work.str.st = 0;
nyatla 83:d46aa580722a 378 break;
nyatla 83:d46aa580722a 379 }
nyatla 83:d46aa580722a 380 }
nyatla 83:d46aa580722a 381 return;
nyatla 71:855020258513 382 case PARAM_ST_INT:
nyatla 83:d46aa580722a 383 if (isUnSignedCharSet(i_c)){
nyatla 71:855020258513 384 i_inst->_work.int32.v = i_inst->_work.int32.v * 10 + (i_c - '0');
nyatla 71:855020258513 385 return;
nyatla 71:855020258513 386 }
nyatla 71:855020258513 387 switch (i_c){
nyatla 71:855020258513 388 case ' ':
nyatla 71:855020258513 389 i_inst->_pst = PARAM_ST_NEXT; break;
nyatla 71:855020258513 390 case ',':
nyatla 71:855020258513 391 i_inst->_pst = PARAM_ST_VAL; break;
nyatla 71:855020258513 392 case ']':
nyatla 71:855020258513 393 i_inst->_st = NyLPC_TcJsonRpcParser_ST_NEXT; break;
nyatla 71:855020258513 394 default:
nyatla 71:855020258513 395 goto ERROR;
nyatla 71:855020258513 396 }
nyatla 71:855020258513 397 //4バイト境界に揃える
nyatla 71:855020258513 398 i_inst->_work.str.n += 4;
nyatla 71:855020258513 399 if (i_inst->_work.str.n > NyLPC_TJsonRpcParserResult_PARAM_BUF){
nyatla 71:855020258513 400 goto ERROR;
nyatla 71:855020258513 401 }
nyatla 71:855020258513 402 *((NyLPC_TInt32*)&(i_inst->_result->method.param_buf[i_inst->_work.str.n - 4])) = i_inst->_work.int32.v*i_inst->_work.int32.s;
nyatla 71:855020258513 403 return;
nyatla 71:855020258513 404 case PARAM_ST_UINT:
nyatla 83:d46aa580722a 405 if (isUnSignedCharSet(i_c)){
nyatla 71:855020258513 406 i_inst->_work.uint32 = i_inst->_work.uint32 * 10 + (i_c - '0');
nyatla 71:855020258513 407 return;
nyatla 71:855020258513 408 }
nyatla 71:855020258513 409 switch (i_c){
nyatla 71:855020258513 410 case ' ':
nyatla 71:855020258513 411 i_inst->_pst = PARAM_ST_NEXT; break;
nyatla 71:855020258513 412 case ',':
nyatla 71:855020258513 413 i_inst->_pst = PARAM_ST_VAL; break;
nyatla 71:855020258513 414 case ']':
nyatla 71:855020258513 415 i_inst->_st = NyLPC_TcJsonRpcParser_ST_NEXT; break;
nyatla 71:855020258513 416 default:
nyatla 71:855020258513 417 goto ERROR;
nyatla 71:855020258513 418 }
nyatla 71:855020258513 419 i_inst->_work.str.n += 4;
nyatla 71:855020258513 420 if (i_inst->_work.str.n > NyLPC_TJsonRpcParserResult_PARAM_BUF){
nyatla 71:855020258513 421 goto ERROR;
nyatla 71:855020258513 422 }
nyatla 71:855020258513 423 *((NyLPC_TUInt32*)&(i_inst->_result->method.param_buf[i_inst->_work.str.n - 4])) = i_inst->_work.uint32;
nyatla 71:855020258513 424 return;
nyatla 71:855020258513 425 case PARAM_ST_BYTE:
nyatla 83:d46aa580722a 426 if (isUnSignedCharSet(i_c)){
nyatla 71:855020258513 427 i_inst->_work.uint32 = i_inst->_work.uint32 * 10 + (i_c - '0');
nyatla 71:855020258513 428 return;
nyatla 71:855020258513 429 }
nyatla 71:855020258513 430 switch (i_c){
nyatla 71:855020258513 431 case ' ':
nyatla 71:855020258513 432 i_inst->_pst = PARAM_ST_NEXT; break;
nyatla 71:855020258513 433 case ',':
nyatla 71:855020258513 434 i_inst->_pst = PARAM_ST_VAL; break;
nyatla 71:855020258513 435 case ']':
nyatla 71:855020258513 436 i_inst->_st = NyLPC_TcJsonRpcParser_ST_NEXT; break;
nyatla 71:855020258513 437 default:
nyatla 71:855020258513 438 goto ERROR;
nyatla 71:855020258513 439 }
nyatla 71:855020258513 440 i_inst->_work.str.n ++;
nyatla 71:855020258513 441 if (i_inst->_work.str.n > NyLPC_TJsonRpcParserResult_PARAM_BUF){
nyatla 71:855020258513 442 goto ERROR;
nyatla 71:855020258513 443 }
nyatla 71:855020258513 444 *((NyLPC_TUInt8*)&(i_inst->_result->method.param_buf[i_inst->_work.str.n - 1])) = (NyLPC_TUInt8)(i_inst->_work.uint32&0xff);
nyatla 71:855020258513 445 return;
nyatla 71:855020258513 446 case PARAM_ST_NEXT:
nyatla 71:855020258513 447 switch (i_c){
nyatla 71:855020258513 448 case ' ':break;
nyatla 71:855020258513 449 case ',':
nyatla 71:855020258513 450 i_inst->_pst = PARAM_ST_VAL; break;
nyatla 71:855020258513 451 case ']':
nyatla 71:855020258513 452 i_inst->_st = NyLPC_TcJsonRpcParser_ST_NEXT; break;
nyatla 71:855020258513 453 default:
nyatla 71:855020258513 454 goto ERROR;
nyatla 71:855020258513 455 }
nyatla 71:855020258513 456 return;
nyatla 71:855020258513 457 }
nyatla 71:855020258513 458 ERROR:
nyatla 71:855020258513 459 i_inst->_st = NyLPC_TcJsonRpcParser_ST_ERROR;
nyatla 71:855020258513 460 return;
nyatla 71:855020258513 461 }
nyatla 71:855020258513 462
nyatla 71:855020258513 463
nyatla 71:855020258513 464 /**
nyatla 71:855020258513 465 * NyLPC_cJsonRpcParser_putCharのサブ関数
nyatla 71:855020258513 466 */
nyatla 71:855020258513 467 static NyLPC_TUInt8 valTerminator2St(NyLPC_TChar i_c)
nyatla 71:855020258513 468 {
nyatla 71:855020258513 469 switch (i_c){
nyatla 71:855020258513 470 case ' ':
nyatla 71:855020258513 471 return NyLPC_TcJsonRpcParser_ST_NEXT;
nyatla 71:855020258513 472 case '}':
nyatla 71:855020258513 473 return NyLPC_TcJsonRpcParser_ST_END;
nyatla 71:855020258513 474 case ',':
nyatla 71:855020258513 475 return NyLPC_TcJsonRpcParser_ST_NAME_Q;
nyatla 71:855020258513 476 default:
nyatla 71:855020258513 477 //ないはず
nyatla 71:855020258513 478 return NyLPC_TcJsonRpcParser_ST_ERROR;
nyatla 71:855020258513 479 }
nyatla 71:855020258513 480 }
nyatla 71:855020258513 481
nyatla 71:855020258513 482 /** 文字列をパーサに入力してパーサの状態を遷移させます。
nyatla 71:855020258513 483
nyatla 71:855020258513 484 */
nyatla 71:855020258513 485 void NyLPC_cJsonRpcParser_putChar(NyLPC_TcJsonRpcParser_t* i_inst,char i_c)
nyatla 71:855020258513 486 {
nyatla 71:855020258513 487 switch(i_inst->_st){
nyatla 71:855020258513 488 case NyLPC_TcJsonRpcParser_ST_PARAMS:
nyatla 71:855020258513 489 putchar_params(i_inst,i_c);
nyatla 71:855020258513 490 return;
nyatla 71:855020258513 491 case NyLPC_TcJsonRpcParser_ST_START:
nyatla 71:855020258513 492 if(i_c=='{'){
nyatla 71:855020258513 493 i_inst->_st=NyLPC_TcJsonRpcParser_ST_NAME_Q;
nyatla 71:855020258513 494 return;
nyatla 71:855020258513 495 }
nyatla 71:855020258513 496 if(strchr(" ",i_c)!=NULL){
nyatla 71:855020258513 497 //無視
nyatla 71:855020258513 498 return;
nyatla 71:855020258513 499 }
nyatla 71:855020258513 500 //エラー
nyatla 71:855020258513 501 goto ERROR;
nyatla 71:855020258513 502 case NyLPC_TcJsonRpcParser_ST_NAME_Q:
nyatla 71:855020258513 503 if(i_c=='"'){
nyatla 71:855020258513 504 i_inst->_st=NyLPC_TcJsonRpcParser_ST_NAME_STR;
nyatla 71:855020258513 505 i_inst->_work.str.n=0;
nyatla 71:855020258513 506 return;
nyatla 71:855020258513 507 }
nyatla 71:855020258513 508 if(strchr(" ",i_c)!=NULL){
nyatla 71:855020258513 509 //無視
nyatla 71:855020258513 510 return;
nyatla 71:855020258513 511 }
nyatla 71:855020258513 512 //エラー
nyatla 71:855020258513 513 goto ERROR;
nyatla 71:855020258513 514 case NyLPC_TcJsonRpcParser_ST_NAME_STR:
nyatla 71:855020258513 515 if(i_c=='"'){
nyatla 71:855020258513 516 //完了
nyatla 71:855020258513 517 i_inst->_work.str.buf[i_inst->_work.str.n]='\0';
nyatla 71:855020258513 518 //メソッドIDを記録
nyatla 71:855020258513 519 i_inst->_name_id=NyLPC_TTextIdTbl_getMatchId(i_inst->_work.str.buf,method_name_tbl);
nyatla 71:855020258513 520 i_inst->_st=NyLPC_TcJsonRpcParser_ST_NV_SEP;
nyatla 71:855020258513 521 switch(i_inst->_name_id){
nyatla 71:855020258513 522 case NAME_ID_METHOD:
nyatla 71:855020258513 523 case NAME_ID_RESULT:
nyatla 71:855020258513 524 if(i_inst->_result->type!=NyLPC_TJsonRpcParserResult_TYPE_UNKNOWN){
nyatla 71:855020258513 525 goto ERROR;
nyatla 71:855020258513 526 }
nyatla 71:855020258513 527 break;
nyatla 71:855020258513 528 case NAME_ID_PARAMS:
nyatla 71:855020258513 529 if (i_inst->_pcounter != 0){
nyatla 71:855020258513 530 goto ERROR;
nyatla 71:855020258513 531 }
nyatla 71:855020258513 532 break;
nyatla 71:855020258513 533 default:
nyatla 71:855020258513 534 break;
nyatla 71:855020258513 535 }
nyatla 71:855020258513 536 return;
nyatla 71:855020258513 537 }
nyatla 71:855020258513 538 i_inst->_work.str.n++;
nyatla 71:855020258513 539 if(i_inst->_work.str.n>=NyLPC_TcJsonRpcParser_WORK_MAX){
nyatla 71:855020258513 540 //文字列長すぎでござる。
nyatla 71:855020258513 541 goto ERROR;
nyatla 71:855020258513 542 }
nyatla 71:855020258513 543 i_inst->_work.str.buf[i_inst->_work.str.n-1]=i_c;
nyatla 71:855020258513 544 return;
nyatla 71:855020258513 545 case NyLPC_TcJsonRpcParser_ST_NV_SEP:
nyatla 71:855020258513 546 if(i_c==':'){
nyatla 71:855020258513 547 if (i_inst->_name_id == NAME_ID_PARAMS){
nyatla 71:855020258513 548 i_inst->_pst = PARAM_ST_START;
nyatla 71:855020258513 549 i_inst->_st = NyLPC_TcJsonRpcParser_ST_PARAMS;
nyatla 71:855020258513 550 }
nyatla 71:855020258513 551 else{
nyatla 71:855020258513 552 i_inst->_st = NyLPC_TcJsonRpcParser_ST_VAL;
nyatla 71:855020258513 553 i_inst->_work.str.n = 0;
nyatla 71:855020258513 554 }
nyatla 71:855020258513 555 return;
nyatla 71:855020258513 556 }
nyatla 71:855020258513 557 if(strchr(" ",i_c)!=NULL){
nyatla 71:855020258513 558 //無視
nyatla 71:855020258513 559 return;
nyatla 71:855020258513 560 }
nyatla 71:855020258513 561 //エラー
nyatla 71:855020258513 562 goto ERROR;
nyatla 71:855020258513 563 case NyLPC_TcJsonRpcParser_ST_VAL:
nyatla 71:855020258513 564 if(i_c=='"'){
nyatla 71:855020258513 565 i_inst->_st=NyLPC_TcJsonRpcParser_ST_VAL_STR;
nyatla 71:855020258513 566 i_inst->_work.str.n=0;
nyatla 71:855020258513 567 return;
nyatla 71:855020258513 568 }
nyatla 71:855020258513 569 if(strchr(" ",i_c)!=NULL){
nyatla 71:855020258513 570 //無視
nyatla 71:855020258513 571 return;
nyatla 71:855020258513 572 }
nyatla 83:d46aa580722a 573 if (isUnSignedCharSet(i_c)){
nyatla 83:d46aa580722a 574 i_inst->_work.uint32 = (i_c - '0');
nyatla 71:855020258513 575 i_inst->_st=NyLPC_TcJsonRpcParser_ST_VAL_UINT;
nyatla 71:855020258513 576 return;
nyatla 71:855020258513 577 }
nyatla 71:855020258513 578 //エラー
nyatla 71:855020258513 579 goto ERROR;
nyatla 71:855020258513 580 case NyLPC_TcJsonRpcParser_ST_VAL_UINT:
nyatla 83:d46aa580722a 581 if (isUnSignedCharSet(i_c)){
nyatla 83:d46aa580722a 582 i_inst->_work.uint32 = i_inst->_work.uint32 * 10 + (i_c - '0');
nyatla 71:855020258513 583 return;
nyatla 71:855020258513 584 }
nyatla 71:855020258513 585 if(strchr(" ,}",i_c)!=NULL){
nyatla 71:855020258513 586 //確定
nyatla 71:855020258513 587 switch(i_inst->_name_id){
nyatla 71:855020258513 588 case NAME_ID_ID:
nyatla 71:855020258513 589 i_inst->_result->method.id=i_inst->_work.uint32;
nyatla 71:855020258513 590 break;
nyatla 71:855020258513 591 case NAME_ID_UNKNOWN:
nyatla 71:855020258513 592 //知らないIDは無視
nyatla 71:855020258513 593 break;
nyatla 71:855020258513 594 default:
nyatla 71:855020258513 595 //数値を受け入れないパラメータ
nyatla 71:855020258513 596 goto ERROR;
nyatla 71:855020258513 597 }
nyatla 71:855020258513 598 i_inst->_st=valTerminator2St(i_c);
nyatla 71:855020258513 599 return;
nyatla 71:855020258513 600 }
nyatla 71:855020258513 601 goto ERROR;
nyatla 71:855020258513 602 case NyLPC_TcJsonRpcParser_ST_VAL_STR:
nyatla 71:855020258513 603 if(i_c=='"'){
nyatla 71:855020258513 604 i_inst->_work.str.buf[i_inst->_work.str.n]='\0';
nyatla 71:855020258513 605 //確定
nyatla 71:855020258513 606 switch(i_inst->_name_id){
nyatla 71:855020258513 607 case NAME_ID_VERSION:
nyatla 71:855020258513 608 if(strcmp(i_inst->_work.str.buf,"2.0")!=0){
nyatla 71:855020258513 609 goto ERROR;
nyatla 71:855020258513 610 }
nyatla 71:855020258513 611 break;
nyatla 71:855020258513 612 case NAME_ID_METHOD:
nyatla 71:855020258513 613 i_inst->_result->method._type=NyLPC_TJsonRpcParserResult_TYPE_METHOD;
nyatla 71:855020258513 614 i_inst->_result->method.class_def=findFunction(i_inst->_class_def,i_inst->_work.str.buf,&(i_inst->_result->method.func_number));
nyatla 71:855020258513 615 if(i_inst->_result->method.class_def==NULL){
nyatla 71:855020258513 616 goto ERROR;
nyatla 71:855020258513 617 }
nyatla 71:855020258513 618 break;
nyatla 71:855020258513 619 case NAME_ID_UNKNOWN:
nyatla 71:855020258513 620 //知らないIDは無視
nyatla 71:855020258513 621 break;
nyatla 71:855020258513 622 default:
nyatla 71:855020258513 623 //文字列を受け入れないパラメータ
nyatla 71:855020258513 624 goto ERROR;
nyatla 71:855020258513 625 }
nyatla 71:855020258513 626 i_inst->_st=NyLPC_TcJsonRpcParser_ST_NEXT;
nyatla 71:855020258513 627 return;
nyatla 71:855020258513 628 }
nyatla 71:855020258513 629 i_inst->_work.str.n++;
nyatla 71:855020258513 630 if(i_inst->_work.str.n>=NyLPC_TcJsonRpcParser_WORK_MAX){
nyatla 71:855020258513 631 //文字列長すぎでござる。
nyatla 71:855020258513 632 goto ERROR;
nyatla 71:855020258513 633 }
nyatla 71:855020258513 634 i_inst->_work.str.buf[i_inst->_work.str.n-1]=i_c;
nyatla 71:855020258513 635 return;
nyatla 71:855020258513 636 case NyLPC_TcJsonRpcParser_ST_NEXT:
nyatla 71:855020258513 637 if(i_c==','){
nyatla 71:855020258513 638 i_inst->_st=NyLPC_TcJsonRpcParser_ST_NAME_Q;
nyatla 71:855020258513 639 }else if(i_c=='}'){
nyatla 71:855020258513 640 i_inst->_st=NyLPC_TcJsonRpcParser_ST_END;
nyatla 71:855020258513 641 }else if(strchr(" ",i_c)!=NULL){
nyatla 71:855020258513 642 //nothing to do
nyatla 71:855020258513 643 }else{
nyatla 71:855020258513 644 goto ERROR;
nyatla 71:855020258513 645 }
nyatla 71:855020258513 646 return;
nyatla 71:855020258513 647 default:
nyatla 71:855020258513 648 goto ERROR;
nyatla 71:855020258513 649 }
nyatla 71:855020258513 650 ERROR:
nyatla 71:855020258513 651 i_inst->_st = NyLPC_TcJsonRpcParser_ST_ERROR;
nyatla 71:855020258513 652 return;
nyatla 71:855020258513 653 }
nyatla 71:855020258513 654
nyatla 71:855020258513 655 #define COMMENT_DEBUG
nyatla 71:855020258513 656 #ifndef COMMENT_DEBUG
nyatla 71:855020258513 657
nyatla 71:855020258513 658 const struct NyLPC_TJsonRpcMethodDef test_method[]=
nyatla 71:855020258513 659 {
nyatla 71:855020258513 660 { "func" , ""},
nyatla 71:855020258513 661 { "func1", "d" },
nyatla 71:855020258513 662 { "func2", "dd" },
nyatla 71:855020258513 663 { "func3", "u" },
nyatla 71:855020258513 664 { "func4", "uu" },
nyatla 71:855020258513 665 { "func5", "uss" },
nyatla 71:855020258513 666 { "func5", "uss" },
nyatla 71:855020258513 667 { "func6", "s" },
nyatla 71:855020258513 668 { "func7", "sd" },
nyatla 71:855020258513 669 { "func8", "su" },
nyatla 71:855020258513 670 { "func9", "ss" },
nyatla 71:855020258513 671 { "f10", "suu" },
nyatla 71:855020258513 672 { "f11", "bbbb" },
nyatla 83:d46aa580722a 673 { "f12", "usu" },
nyatla 83:d46aa580722a 674 { "f13", "uBu" },
nyatla 71:855020258513 675 { NULL, "" }
nyatla 71:855020258513 676 };
nyatla 71:855020258513 677
nyatla 71:855020258513 678 const struct NyLPC_TJsonRpcClassDef test_def=
nyatla 71:855020258513 679 {
nyatla 71:855020258513 680 "ns","class",
nyatla 71:855020258513 681 test_method
nyatla 71:855020258513 682 };
nyatla 71:855020258513 683
nyatla 71:855020258513 684 /**
nyatla 71:855020258513 685 * テスト用のアレイ
nyatla 71:855020258513 686 */
nyatla 71:855020258513 687 const struct NyLPC_TJsonRpcClassDef* test_def_array[]=
nyatla 71:855020258513 688 {
nyatla 71:855020258513 689 &test_def,
nyatla 71:855020258513 690 NULL
nyatla 71:855020258513 691 };
nyatla 71:855020258513 692
nyatla 71:855020258513 693 void NyLPC_cJsonRpcParser_putText(NyLPC_TcJsonRpcParser_t* i_inst, const NyLPC_TChar* i_text,NyLPC_TUInt16 i_size)
nyatla 71:855020258513 694 {
nyatla 71:855020258513 695 NyLPC_TUInt16 i;
nyatla 71:855020258513 696 NyLPC_TUInt32 u32;
nyatla 71:855020258513 697 NyLPC_TInt32 i32;
nyatla 71:855020258513 698 NyLPC_TChar* c;
nyatla 83:d46aa580722a 699 NyLPC_TUInt8 u8,l8;
nyatla 71:855020258513 700
nyatla 71:855020258513 701
nyatla 71:855020258513 702 for (i = 0; i < i_size; i++){
nyatla 71:855020258513 703 NyLPC_cJsonRpcParser_putChar(i_inst,i_text[i]);
nyatla 71:855020258513 704 if (i_inst->_st == NyLPC_TcJsonRpcParser_ST_ERROR){
nyatla 71:855020258513 705 break;
nyatla 71:855020258513 706 }
nyatla 71:855020258513 707 else if (i_inst->_st == NyLPC_TcJsonRpcParser_ST_END){
nyatla 71:855020258513 708 break;
nyatla 71:855020258513 709 }
nyatla 71:855020258513 710 }
nyatla 71:855020258513 711 if (i_inst->_st == NyLPC_TcJsonRpcParser_ST_ERROR){
nyatla 71:855020258513 712 printf("ERROR!\n");
nyatla 71:855020258513 713 }
nyatla 71:855020258513 714 else if (i_inst->_st == NyLPC_TcJsonRpcParser_ST_END){
nyatla 71:855020258513 715 printf("OK!\n");
nyatla 71:855020258513 716 for (int i = 0; i_inst->_result->method.param_index[i] != 0xff && i<i_inst->_pcounter; i++){
nyatla 71:855020258513 717 switch (i_inst->_result->method.class_def->functions[i_inst->_result->method.func_number].param_patt[i]){
nyatla 71:855020258513 718 case 'u':NyLPC_TJsonRpcParserResult_getUInt32(i_inst->_result,i, &u32); printf("%u,", u32); break;
nyatla 71:855020258513 719 case 'd':NyLPC_TJsonRpcParserResult_getInt32(i_inst->_result, i, &i32); printf("%d,", i32); break;
nyatla 83:d46aa580722a 720 case 's':NyLPC_TJsonRpcParserResult_getStr(i_inst->_result, i, &c, &l8); printf("%s:%d,", c, l8); break;
nyatla 83:d46aa580722a 721 case 'B':NyLPC_TJsonRpcParserResult_getByteArray(i_inst->_result, i, &c, &l8); printf("--:%d,", l8); break;
nyatla 71:855020258513 722 case 'b':NyLPC_TJsonRpcParserResult_getByte(i_inst->_result, i, &u8); printf("%u,", u8); break;
nyatla 71:855020258513 723 }
nyatla 71:855020258513 724 }
nyatla 71:855020258513 725 printf("\n");
nyatla 71:855020258513 726 }
nyatla 71:855020258513 727 else{
nyatla 71:855020258513 728 printf("CONTINUE...\n");
nyatla 71:855020258513 729 }
nyatla 71:855020258513 730 }
nyatla 71:855020258513 731
nyatla 71:855020258513 732 void main(void)
nyatla 71:855020258513 733 {
nyatla 71:855020258513 734 NyLPC_TcJsonRpcParser_t inst;
nyatla 71:855020258513 735 union NyLPC_TJsonRpcParserResult ret;
nyatla 71:855020258513 736
nyatla 71:855020258513 737 //JSONCORE
nyatla 71:855020258513 738 //#define JSONCORE 1
nyatla 71:855020258513 739 #ifdef JSONCORE
nyatla 71:855020258513 740 const char* t[] = {
nyatla 71:855020258513 741 "{\"0123\"\"2.0\"}",//NO
nyatla 71:855020258513 742 "{\"01234567890123456789\":\"2.0\"}",//NO
nyatla 71:855020258513 743 "{\"0123\":\"01234567890123456789\"}",//NO
nyatla 71:855020258513 744 "{\"version\":\"2.0\"}",//YES
nyatla 71:855020258513 745 "{\"version\":\"2.1\"}",//NO
nyatla 71:855020258513 746 " { \"version\" : \"2.0\" }",//YES
nyatla 71:855020258513 747 "{\"version\":\"2.0\",}",//NO
nyatla 71:855020258513 748 "{\"version\":\"2.0\" \"test\":\"t\"}",//NO
nyatla 71:855020258513 749 "{\"version\":\"2.0\",\"method\":}",//NO
nyatla 71:855020258513 750 "{\"version\":\"2.0\",\"method\":\"ns:class:func\"}",//YES
nyatla 71:855020258513 751 "{\"version\":\"2.0\",\"method\":\"ns2:class:func\"}",//NO
nyatla 71:855020258513 752 "{\"version\":\"2.0\",\"method\":\"ns:class2:func\"}",//NO
nyatla 71:855020258513 753 "{\"version\":\"2.0\",\"method\":\"ns:class:func2\"}",//NO
nyatla 71:855020258513 754 "{\"version\":\"2.0\",\"method\":\"ns:class:func\",\"method\":\"\"}",//NO
nyatla 71:855020258513 755 "{\"version\":\"2.0\",\"method\":\"ns:class:func\",\"id\":0}",//YES
nyatla 71:855020258513 756 "{\"version\":\"2.0\",\"method\":\"ns:class:func\",\"id\":\"123\"}",//YES
nyatla 71:855020258513 757 "{\"version\":\"2.0\",\"method\":123}",//NO
nyatla 71:855020258513 758 "{\"version\":\"2.0\",\"test\":123}",//YES
nyatla 71:855020258513 759 "{\"version\":\"2.0\",\"test\":123, \"test\":123 }",//YES
nyatla 71:855020258513 760 "{\"version\":\"2.0\",\"test\":123 ,\"test\":123 }",//YES
nyatla 71:855020258513 761 NULL
nyatla 71:855020258513 762 };
nyatla 71:855020258513 763 #else
nyatla 71:855020258513 764 const char* t[] = {
nyatla 83:d46aa580722a 765 /* "{\"version\":\"2.0\",\"method\":\"ns:class:func\",\"params\":[]}",//YES
nyatla 71:855020258513 766 "{\"version\":\"2.0\",\"method\":\"ns:class:func\",\"params\": [] }",//YES
nyatla 71:855020258513 767 "{\"version\":\"2.0\",\"method\":\"ns:class:func1\",\"params\": [-123] }",//YES
nyatla 71:855020258513 768 "{\"version\":\"2.0\",\"method\":\"ns:class:func1\",\"params\": [\"123\"] }",//NG
nyatla 71:855020258513 769 "{\"version\":\"2.0\",\"method\":\"ns:class:func2\",\"params\": [ 1 , 456 ] }",//YES
nyatla 71:855020258513 770 "{\"version\":\"2.0\",\"method\":\"ns:class:func2\",\"params\": [ 123 , 456 a] }",//NG
nyatla 71:855020258513 771 "{\"version\":\"2.0\",\"method\":\"ns:class:func2\",\"params\": [ 123a] }",//NG
nyatla 71:855020258513 772 "{\"version\":\"2.0\",\"method\":\"ns:class:func2\",\"params\": [ 123,456] }",//OK
nyatla 71:855020258513 773 "{\"version\":\"2.0\",\"method\":\"ns:class:func2\",\"params\": [123,456] }",//YES
nyatla 71:855020258513 774 "{\"version\":\"2.0\",\"method\":\"ns:class:func3\",\"params\": [123] }",//YES
nyatla 71:855020258513 775 "{\"version\":\"2.0\",\"method\":\"ns:class:func3\",\"params\": [\"123\"] }",//NG
nyatla 71:855020258513 776 "{\"version\":\"2.0\",\"method\":\"ns:class:func4\",\"params\": [ 123 , 456 ] }",//YES
nyatla 71:855020258513 777 "{\"version\":\"2.0\",\"method\":\"ns:class:func4\",\"params\": [ 123 , 456 a] }",//NG
nyatla 71:855020258513 778 "{\"version\":\"2.0\",\"method\":\"ns:class:func4\",\"params\": [ 123a] }",//NG
nyatla 71:855020258513 779 "{\"version\":\"2.0\",\"method\":\"ns:class:func4\",\"params\": [ 123,456] }",//OK
nyatla 71:855020258513 780 "{\"version\":\"2.0\",\"method\":\"ns:class:func5\",\"params\": [123, \"abc\" , \"abc\"] }",//OK
nyatla 71:855020258513 781 "{\"version\":\"2.0\",\"method\":\"ns:class:func6\",\"params\": [\"01234567890123456789012\"] }",//OK
nyatla 71:855020258513 782 "{\"version\":\"2.0\",\"method\":\"ns:class:func6\",\"params\": [1] }",//NG
nyatla 71:855020258513 783 "{\"version\":\"2.0\",\"method\":\"ns:class:func8\",\"params\": [\"012345678901234567\",1,1] }",//OK
nyatla 71:855020258513 784 "{\"version\":\"2.0\",\"method\":\"ns:class:func8\",\"params\": [\"012345678901234567\",1] }",//OK
nyatla 71:855020258513 785 "{\"version\":\"2.0\",\"method\":\"ns:class:func7\",\"params\": [\"012345678901234567\",1] }",//OK
nyatla 71:855020258513 786 "{\"version\":\"2.0\",\"method\":\"ns:class:f10\",\"params\": [\"01234567890123\",22,33] }",//OK
nyatla 71:855020258513 787 "{\"version\":\"2.0\",\"method\":\"ns:class:f11\",\"params\": [ 0 , 1 ,2,3] }",//OK
nyatla 71:855020258513 788 "{\"version\":\"2.0\",\"method\":\"ns:class:f11\",\"params\": [ 15 , 1 ,2,3a] }",//NG
nyatla 83:d46aa580722a 789 */
nyatla 83:d46aa580722a 790 "{\"version\":\"2.0\",\"method\":\"ns:class:f13\",\"params\": [2,\"0011223344ff\",1] }",//NG
nyatla 83:d46aa580722a 791 // "{\"version\":\"2.0\",\"method\":\"ns:class:f12\",\"params\": [2,\"0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789\",1] }",//NG
nyatla 71:855020258513 792 NULL
nyatla 71:855020258513 793 };
nyatla 71:855020258513 794 #endif
nyatla 71:855020258513 795 int i;
nyatla 71:855020258513 796
nyatla 71:855020258513 797 for (i = 0;t[i]!=NULL; i++){
nyatla 71:855020258513 798 NyLPC_cJsonRpcParser_initialize(&inst, test_def_array);
nyatla 71:855020258513 799 NyLPC_cJsonRpcParser_initParser(&inst, &ret);
nyatla 71:855020258513 800 NyLPC_cJsonRpcParser_putText(&inst, t[i], strlen(t[i]));
nyatla 83:d46aa580722a 801 continue;
nyatla 71:855020258513 802 }
nyatla 71:855020258513 803 printf("end\n");
nyatla 71:855020258513 804 return;
nyatla 71:855020258513 805
nyatla 71:855020258513 806 }
nyatla 71:855020258513 807 #endif
nyatla 71:855020258513 808