Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of libMiMic by
NyLPC_cModJsonRpc.c
00001 #include "NyLPC_cModJsonRpc.h" 00002 00003 00004 00005 00006 void NyLPC_cModJsonRpc_initialize(NyLPC_TcModJsonRpc_t* i_inst,const NyLPC_TChar* i_ref_root_path,const struct NyLPC_TJsonRpcClassDef** i_class_tbl) 00007 { 00008 NyLPC_cModWebSocket_initialize(&i_inst->super,i_ref_root_path); 00009 NyLPC_cJsonRpcParser_initialize(&i_inst->_rpc_parser,i_class_tbl); 00010 } 00011 void NyLPC_cModJsonRpc_finalize(NyLPC_TcModJsonRpc_t* i_inst) 00012 { 00013 NyLPC_cJsonRpcParser_finalize(&i_inst->_rpc_parser); 00014 NyLPC_cModWebSocket_finalize(&i_inst->super); 00015 } 00016 #define NyLPC_cModJsonRpc_canHandle(i,c) NyLPC_cModWebSocket_canHandle(&((i)->super),(c)) 00017 00018 #define NyLPC_cModJsonRpc_close(i,t) NyLPC_cModWebSocket_close(&((i)->super),(t)) 00019 00020 00021 NyLPC_TBool NyLPC_cModJsonRpc_execute(NyLPC_TcModJsonRpc_t* i_inst,NyLPC_TcHttpdConnection_t* i_connection) 00022 { 00023 //executeで強制初期化 00024 NyLPC_cJsonRpcParser_initParser(&i_inst->_rpc_parser,&i_inst->_result); 00025 return NyLPC_cModWebSocket_execute(&((i_inst)->super),i_connection); 00026 } 00027 00028 static NyLPC_TInt32 readHandler(void* i_param,NyLPC_TChar i_c) 00029 { 00030 NyLPC_TcModJsonRpc_t* inst=(NyLPC_TcModJsonRpc_t*)i_param; 00031 NyLPC_cJsonRpcParser_putChar(&inst->_rpc_parser,i_c); 00032 switch(NyLPC_cJsonRpcParser_getStatus(&(inst->_rpc_parser))){ 00033 case NyLPC_TcJsonRpcParser_ST_END: 00034 return 0;//終端到達。中断 00035 case NyLPC_TcJsonRpcParser_ST_ERROR: 00036 return -1;//エラー 00037 default: 00038 return 1;//次の文字をリクエスト 00039 } 00040 } 00041 00042 00043 NyLPC_TBool NyLPC_cModJsonRpc_processRpcMessage(NyLPC_TcModJsonRpc_t* i_inst) 00044 { 00045 //終了した場合のみパーサステータスを初期化。 00046 switch(NyLPC_cJsonRpcParser_getStatus(&i_inst->_rpc_parser)){ 00047 case NyLPC_TcJsonRpcParser_ST_END: 00048 NyLPC_cJsonRpcParser_initParser(&i_inst->_rpc_parser,&i_inst->_result); 00049 break; 00050 case NyLPC_TcJsonRpcParser_ST_ERROR: 00051 //パーサがエラーを検出してたら何もしない。 00052 return NyLPC_TBool_FALSE; 00053 } 00054 //コールバックハンドラで受信 00055 if(NyLPC_cModWebSocket_readCB(&(i_inst->super),readHandler,i_inst)<0){ 00056 return NyLPC_TBool_FALSE; 00057 } 00058 return NyLPC_TBool_TRUE; 00059 } 00060 00061 const union NyLPC_TJsonRpcParserResult* NyLPC_cModJsonRpc_getMessage(NyLPC_TcModJsonRpc_t* i_inst) 00062 { 00063 return (NyLPC_cJsonRpcParser_getStatus(&i_inst->_rpc_parser)!=NyLPC_TcJsonRpcParser_ST_END)?NULL:(&i_inst->_result); 00064 } 00065 00066 NyLPC_TBool NyLPC_cModJsonRpc_putRawMessageV(NyLPC_TcModJsonRpc_t* i_inst,const NyLPC_TChar* i_fmt,va_list i_args) 00067 { 00068 return NyLPC_cModWebSocket_writeFormatV(&i_inst->super,i_fmt,i_args); 00069 } 00070 NyLPC_TBool NyLPC_cModJsonRpc_putRawMessage(NyLPC_TcModJsonRpc_t* i_inst,const NyLPC_TChar* i_fmt,...) 00071 { 00072 NyLPC_TBool r; 00073 va_list a; 00074 va_start(a,i_fmt); 00075 r=NyLPC_cModWebSocket_writeFormatV(&i_inst->super,i_fmt,a); 00076 va_end(a); 00077 return r; 00078 } 00079 00080 00081 00082 /** 00083 * @param i_params_fmt 00084 * パラメータ部のフォーマット文字列です。NyLPC_cFormatWriterと同等の構文を使用できます。 00085 * ここで指定した書式文字列は、"そのまま"パラメータ部に表示されます。文字列を返す場合は、次のように\"でエスケープしてください。 00086 * '0,1,2,-1,\"result\",3' 00087 */ 00088 NyLPC_TBool NyLPC_cModJsonRpc_putResult(NyLPC_TcModJsonRpc_t* i_inst,NyLPC_TUInt32 i_id,const NyLPC_TChar* i_params_fmt,...) 00089 { 00090 NyLPC_TBool r; 00091 va_list a; 00092 //書き込み文字数の事前計算 00093 va_start(a,i_params_fmt); 00094 r=NyLPC_cModJsonRpc_putResultV(i_inst,i_id,i_params_fmt,a); 00095 va_end(a); 00096 return r; 00097 } 00098 00099 /** 00100 * @param i_params_fmt 00101 * パラメータ部のフォーマット文字列です。NyLPC_cFormatWriterと同等の構文を使用できます。 00102 * ここで指定した書式文字列は、"そのまま"パラメータ部に表示されます。文字列を返す場合は、次のように\"でエスケープしてください。 00103 * '0,1,2,-1,\"result\",3' 00104 */ 00105 NyLPC_TBool NyLPC_cModJsonRpc_putResultV(NyLPC_TcModJsonRpc_t* i_inst,NyLPC_TUInt32 i_id,const NyLPC_TChar* i_params_fmt,va_list i_a) 00106 { 00107 NyLPC_TBool r; 00108 va_list a; 00109 NyLPC_TInt16 l; 00110 00111 //書き込み文字数の事前計算 00112 NyLPC_va_copy(a,i_a); 00113 l=NyLPC_cModWebSocket_testFormat(&i_inst->super,"{\"jsonrpc\":\"2.0\",\"result\":["); 00114 l+=NyLPC_cModWebSocket_testFormatV(&i_inst->super,i_params_fmt,a); 00115 l+=NyLPC_cModWebSocket_testFormat(&i_inst->super,"],\"id\":%d}",i_id); 00116 va_end(a); 00117 00118 //バルク書き込み 00119 NyLPC_cModWebSocket_startBulkWrite(&i_inst->super,l); 00120 r=NyLPC_cModWebSocket_writeBulkFormat(&i_inst->super,"{\"jsonrpc\":\"2.0\",\"result\":["); 00121 r=r&&NyLPC_cModWebSocket_writeBulkFormatV(&i_inst->super,i_params_fmt,i_a); 00122 r=r&&NyLPC_cModWebSocket_writeBulkFormat(&i_inst->super,"],\"id\":%d}",i_id); 00123 r=r&&NyLPC_cModWebSocket_endBulkWrite(&i_inst->super); 00124 return r; 00125 } 00126 00127 struct CodeMsgTbl{ 00128 const char* n; 00129 NyLPC_TInt32 id; 00130 }; 00131 00132 const static struct CodeMsgTbl _table[]= 00133 { 00134 {"Parse error",NyLPC_TJsonRpcErrorCode_PARSE_ERROR}, 00135 {"Invalid Request",NyLPC_TJsonRpcErrorCode_INVALID_REQUEST}, 00136 {"Method not found",NyLPC_TJsonRpcErrorCode_METHOD_NOT_FOUND}, 00137 {"Invalid params",NyLPC_TJsonRpcErrorCode_INVALID_PARAMS}, 00138 {"Internal error",NyLPC_TJsonRpcErrorCode_INTERNAL_ERROR}, 00139 {"Server error",NyLPC_TJsonRpcErrorCode_SERVER_ERROR_BASE} 00140 }; 00141 static const NyLPC_TChar* code2msg(NyLPC_TInt32 i_id) 00142 { 00143 int i; 00144 for(i=0;i<6;i++){ 00145 if(i_id==_table[i].id){ 00146 return _table[i].n; 00147 } 00148 } 00149 return _table[5].n; 00150 } 00151 00152 NyLPC_TBool NyLPC_cModJsonRpc_putError(NyLPC_TcModJsonRpc_t* i_inst,NyLPC_TUInt32 i_id,NyLPC_TInt32 i_code) 00153 { 00154 return NyLPC_cModWebSocket_writeFormat(&i_inst->super,"{\"jsonrpc\":\"2.0\",\"error\":{\"code\": %d, \"message\": \"%s\"},\"id\":%d}",i_code,code2msg(i_code),i_id); 00155 }
Generated on Tue Jul 12 2022 16:22:58 by
