This is WIZwiki-REST-io upper version. This version possible to handle PWM, I2C, GPIO.
Dependencies: Adafruit_GFX MbedJSONValue_copy WIZnetInterface mbed
Fork of WIZwiki-REST-io by
RequestHandler.cpp
00001 #include <stdio.h> 00002 #include <stdlib.h> 00003 #include <string.h> 00004 #include "RequestHandler.h" 00005 #include "MbedJSONValue.h" 00006 00007 extern MbedJSONValue WIZwikiREST; 00008 00009 MbedJSONValue* pDataJson = 0; 00010 00011 void GetRequestHandler::handle(char* rest_uri, char* request_data, char *reply) 00012 { 00013 MbedJSONValue* tmpJson; 00014 00015 char* tok; 00016 char* last; 00017 const char * pchar = 0; 00018 #ifdef DEBUG_HTTPREQ 00019 printf("GetRequestHandler():%s\r\n",rest_uri); 00020 #endif 00021 if(!strcmp(rest_uri, "/")){ 00022 tmpJson = &WIZwikiREST; 00023 } 00024 else{ 00025 tok = strtok_r(rest_uri+1, "/", &last); 00026 tmpJson = &WIZwikiREST; 00027 00028 char* name = 0; 00029 while(tok) 00030 { 00031 #ifdef DEBUG_HTTPREQ 00032 printf("tok = %s \r\n", tok); 00033 #endif 00034 if(tmpJson->hasMember(tok)){ 00035 tmpJson = &((*tmpJson)[tok]); 00036 name = tok; 00037 tok = strtok_r(0, "/", &last); 00038 } 00039 else{ 00040 #ifdef DEBUG_HTTPREQ 00041 printf("No Member\r\n"); 00042 #endif 00043 break; 00044 } 00045 } 00046 if(name){ 00047 if(tok){ 00048 if(tmpJson->accessible){ 00049 #ifdef DEBUG_HTTPREQ 00050 printf("accessible : tmpJson->size()=%d\r\n",tmpJson->size()); 00051 #endif 00052 00053 if(tmpJson->size() > 0){ 00054 *tmpJson = std::string(tok); 00055 tmpJson->cb_action((void*)tok); 00056 #ifdef DEBUG_HTTPREQ 00057 printf("set string:%s\r\n",tok); 00058 #endif 00059 } 00060 else{ 00061 *tmpJson = atoi(tok); 00062 tmpJson->cb_action(&tmpJson->_value); 00063 #ifdef DEBUG_HTTPREQ 00064 printf("set int:%d\r\n",atoi(tok)); 00065 #endif 00066 } 00067 } 00068 else{ 00069 strcpy (reply, "HTTP/1.1 403 OK\r\n"); 00070 strcat (reply, "Sever: WIZwiki-REST\r\n"); 00071 strcat (reply, "content-Type: text/json\r\n"); 00072 sprintf(reply + strlen(reply), "Content-Length: %d\r\n\r\n", 26+4); 00073 strcat(reply, "{\"Result : No Accessible\"}"); 00074 strcat (reply, "\r\n\r\n"); 00075 return; 00076 } 00077 } 00078 } 00079 else{ 00080 strcpy (reply, "HTTP/1.1 404 OK\r\n"); 00081 strcat (reply, "Sever: WIZwiki-REST\r\n"); 00082 strcat (reply, "content-Type: text/json\r\n"); 00083 sprintf(reply + strlen(reply), "Content-Length: %d\r\n\r\n", 33+4); 00084 strcat(reply, "{\"Result\" : \"No defined Resource\"}"); 00085 strcat (reply, "\r\n\r\n"); 00086 return; 00087 } 00088 } 00089 pchar = (*tmpJson).serialize().c_str(); 00090 strcpy (reply, "HTTP/1.1 200 OK\r\n"); 00091 strcat (reply, "Sever: WIZwiki-REST\r\n"); 00092 strcat (reply, "content-Type: text/json\r\n"); 00093 sprintf(reply + strlen(reply), "Content-Length: %d\r\n\r\n", strlen(pchar)+4); 00094 strcat (reply, pchar); 00095 strcat (reply, "\r\n\r\n"); 00096 00097 return; 00098 } 00099 00100 void PostRequestHandler::handle(char* rest_uri, char* request_data, char *reply) 00101 { 00102 MbedJSONValue* tmpJson; 00103 char* tok; 00104 char* last; 00105 int errnum = 200; 00106 00107 #ifdef DEBUG_HTTPREQ 00108 printf("PostRequestHandler():%s\r\n",request_data+4); 00109 #endif 00110 if(!pDataJson){ 00111 pDataJson = new MbedJSONValue(); 00112 } 00113 #ifdef DEBUG_HTTPREQ 00114 printf("Before Parse\r\n"); 00115 debug_info(); 00116 #endif 00117 parse(*pDataJson,(const char*)request_data); 00118 #ifdef DEBUG_HTTPREQ 00119 printf("After Parse\r\n"); 00120 debug_info(); 00121 printf("DataJson._type=%d\r\n",pDataJson->_type); 00122 printf("DataJson=%s\r\n",pDataJson->serialize().c_str()); 00123 printf("DataJson.size()=%d\r\n",pDataJson->size()); 00124 printf("DataJason.type=%d\r\n",pDataJson->_type); 00125 #endif 00126 00127 if(!strcmp(rest_uri, "/")){ 00128 tmpJson = &WIZwikiREST; 00129 } 00130 else{ 00131 tok = strtok_r(rest_uri+1, "/", &last); 00132 tmpJson = &WIZwikiREST; 00133 00134 char* name = 0; 00135 while(tok) 00136 { 00137 #ifdef DEBUG_HTTPREQ 00138 printf("tok = %s \r\n", tok); 00139 #endif 00140 if(tmpJson->hasMember(tok)){ 00141 tmpJson = &((*tmpJson)[tok]); 00142 name = tok; 00143 tok = strtok_r(0, "/", &last); 00144 } 00145 else{ 00146 #ifdef DEBUG_HTTPREQ 00147 printf("No Member\r\n"); 00148 #endif 00149 break; 00150 } 00151 } 00152 if(name){ 00153 #ifdef DEBUG_HTTPREQ 00154 printf("Token_name=%s\r\n",name); 00155 #endif 00156 if(tok){ 00157 #ifdef DEBUG_HTTPREQ 00158 printf("It should be no parameters : tok=%s\r\n",tok); 00159 #endif 00160 errnum = 403; 00161 } 00162 } 00163 else{ 00164 errnum = 404; 00165 } 00166 } 00167 if(errnum != 0){ 00168 switch(pDataJson->_type) 00169 { 00170 case MbedJSONValue::TypeInt: 00171 if(tmpJson->accessible){ 00172 *tmpJson = pDataJson->_value.asInt; 00173 tmpJson->cb_action(&tmpJson->_value); 00174 #ifdef DEBUG_HTTPREQ 00175 printf("set int:%d\r\n",atoi(tok)); 00176 #endif 00177 } 00178 else{ 00179 errnum = 403; 00180 } 00181 break; 00182 00183 case MbedJSONValue::TypeString: 00184 if(tmpJson->accessible){ 00185 *tmpJson = pDataJson->_value.asString; 00186 tmpJson->cb_action((void*)tmpJson->_value.asString->c_str()); 00187 } 00188 else{ 00189 errnum = 403; 00190 } 00191 break; 00192 00193 case MbedJSONValue::TypeObject: 00194 for(int i = 0; i < pDataJson->index_token; i++) 00195 { 00196 tok = (char*)pDataJson->token_name[i]->c_str(); 00197 #ifdef DEBUG_HTTPREQ 00198 printf("pDataJson.token_name[%d]->c_str()=%s\r\n",i,tok); 00199 #endif 00200 if(tmpJson->hasMember(tok)){ 00201 if((*tmpJson)[tok].accessible){ 00202 errnum = 200; 00203 if((*pDataJson)[i].size() > 0){ 00204 //(*tmpJson)[tok] = (*pDataJson)[i]._value.asString; 00205 (*tmpJson)[tok] = (*pDataJson)[i]._value.asString->c_str(); 00206 00207 //(*tmpJson)[tok].cb_action((void*)&((*pDataJson)[i]._value.asInt)); 00208 (*tmpJson)[tok].cb_action((void*)((*pDataJson)[i]._value.asString->c_str())); 00209 #ifdef DEBUG_HTTPREQ 00210 printf("String Updated: %s : %s\r\n", tok,(*tmpJson)[tok].get<std::string>().c_str()); 00211 #endif 00212 } 00213 else{ 00214 (*tmpJson)[tok] = (*pDataJson)[i]._value.asInt; 00215 (*tmpJson)[tok].cb_action((void*)&((*pDataJson)[i]._value.asInt)); 00216 #ifdef DEBUG_HTTPREQ 00217 printf("Int Updated: %s : %d\r\n", tok,(*tmpJson)[tok].get<int>()); 00218 #endif 00219 } 00220 } 00221 else{ 00222 errnum = 403; break; 00223 } 00224 } 00225 else{ 00226 errnum = 404; break; 00227 } 00228 } 00229 break; 00230 00231 default: 00232 errnum = 403; break; 00233 } 00234 } 00235 00236 switch(errnum) 00237 { 00238 case 403: 00239 strcpy (reply, "HTTP/1.1 403 OK\r\n"); 00240 strcat (reply, "Sever: WIZwiki-REST\r\n"); 00241 strcat (reply, "content-Type: text/json\r\n"); 00242 sprintf(reply + strlen(reply), "Content-Length: %d\r\n\r\n", 26+4); 00243 strcat(reply, "{\"Result : No Accessible\"}"); 00244 strcat (reply, "\r\n\r\n"); 00245 break; 00246 00247 case 404: 00248 strcpy (reply, "HTTP/1.1 404 OK\r\n"); 00249 strcat (reply, "Sever: WIZwiki-REST\r\n"); 00250 strcat (reply, "content-Type: text/json\r\n"); 00251 sprintf(reply + strlen(reply), "Content-Length: %d\r\n\r\n", 33+4); 00252 strcat(reply, "{\"Result\" : \"No defined Resource\"}"); 00253 strcat (reply, "\r\n\r\n"); 00254 break; 00255 00256 case 200: 00257 strcpy (reply, "HTTP/1.1 200 OK\r\n"); 00258 strcat (reply, "Sever: WIZwiki-REST\r\n"); 00259 strcat (reply, "content-Type: text/json\r\n"); 00260 sprintf(reply + strlen(reply), "Content-Length: %d\r\n\r\n", 15+4); 00261 strcat (reply, "{\"Result\":\"OK\"}"); 00262 strcat (reply, "\r\n\r\n"); 00263 break; 00264 } 00265 00266 #ifdef DEBUG_HTTPREQ 00267 printf("Before Delete\r\n"); 00268 debug_info(); 00269 #endif 00270 if(pDataJson){ 00271 #ifdef DEBUG_HTTPREQ 00272 printf("type;%d, pDataJson->index_token=%d\r\n",pDataJson->_type,pDataJson->index_token); 00273 #endif 00274 delete pDataJson; 00275 pDataJson = 0; 00276 } 00277 #ifdef DEBUG_HTTPREQ 00278 printf("After Delete\r\n"); 00279 debug_info(); 00280 #endif 00281 } 00282 00283 /* 00284 void PutRequestHandler::handle(char* rest_uri, char* request_data, char *reply) 00285 { 00286 } 00287 00288 void DeleteRequestHandler::handle(char* rest_uri, char* request_data, char *reply) 00289 { 00290 } 00291 */
Generated on Thu Jul 14 2022 05:21:39 by 1.7.2