WIZwikiREST-io exercise code for WIZnet Academy
Dependencies: MbedJSONValue_v102 WIZnetInterface mbed
Fork of WIZwiki-REST-io_v103 by
RequestHandler.cpp@1:728e5b5c8dae, 2016-04-05 (annotated)
- Committer:
- MidnightCow
- Date:
- Tue Apr 05 11:40:33 2016 +0000
- Revision:
- 1:728e5b5c8dae
- Parent:
- 0:5886f525a4ad
- Child:
- 2:ca36ecca24c2
1st Released
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
joon874 | 0:5886f525a4ad | 1 | #include <stdio.h> |
joon874 | 0:5886f525a4ad | 2 | #include <stdlib.h> |
joon874 | 0:5886f525a4ad | 3 | #include <string.h> |
joon874 | 0:5886f525a4ad | 4 | #include "RequestHandler.h" |
joon874 | 0:5886f525a4ad | 5 | #include "MbedJSONValue.h" |
joon874 | 0:5886f525a4ad | 6 | |
joon874 | 0:5886f525a4ad | 7 | extern MbedJSONValue WIZwikiREST; |
joon874 | 0:5886f525a4ad | 8 | |
joon874 | 0:5886f525a4ad | 9 | void GetRequestHandler::handle(char* rest_uri, char *reply) |
joon874 | 0:5886f525a4ad | 10 | { |
joon874 | 0:5886f525a4ad | 11 | MbedJSONValue* tmpJson; |
joon874 | 0:5886f525a4ad | 12 | |
joon874 | 0:5886f525a4ad | 13 | char* tok; |
joon874 | 0:5886f525a4ad | 14 | char* last; |
joon874 | 0:5886f525a4ad | 15 | |
joon874 | 0:5886f525a4ad | 16 | printf("GetRequestHandler():%s\r\n",rest_uri); |
joon874 | 0:5886f525a4ad | 17 | |
joon874 | 0:5886f525a4ad | 18 | if(!strcmp(rest_uri, "/")) |
joon874 | 0:5886f525a4ad | 19 | { |
joon874 | 0:5886f525a4ad | 20 | strcpy(reply, WIZwikiREST.serialize().c_str()); |
joon874 | 0:5886f525a4ad | 21 | return; |
joon874 | 0:5886f525a4ad | 22 | } |
joon874 | 0:5886f525a4ad | 23 | tok = strtok_r(rest_uri+1, "/", &last); // 20160226 |
joon874 | 0:5886f525a4ad | 24 | tmpJson = &WIZwikiREST; |
joon874 | 0:5886f525a4ad | 25 | |
MidnightCow | 1:728e5b5c8dae | 26 | char depth = 0; |
MidnightCow | 1:728e5b5c8dae | 27 | char* name = 0; |
joon874 | 0:5886f525a4ad | 28 | while(tok) |
joon874 | 0:5886f525a4ad | 29 | { |
MidnightCow | 1:728e5b5c8dae | 30 | printf("tok = %s \r\n", tok); // Name |
MidnightCow | 1:728e5b5c8dae | 31 | if(tmpJson->hasMember(tok)) |
joon874 | 0:5886f525a4ad | 32 | { |
MidnightCow | 1:728e5b5c8dae | 33 | tmpJson = &((*tmpJson)[tok]); |
MidnightCow | 1:728e5b5c8dae | 34 | name = tok; |
MidnightCow | 1:728e5b5c8dae | 35 | tok = strtok_r(0, "/", &last); |
MidnightCow | 1:728e5b5c8dae | 36 | depth++; |
MidnightCow | 1:728e5b5c8dae | 37 | } |
joon874 | 0:5886f525a4ad | 38 | else |
joon874 | 0:5886f525a4ad | 39 | { |
MidnightCow | 1:728e5b5c8dae | 40 | printf("No Member\r\n"); |
MidnightCow | 1:728e5b5c8dae | 41 | break; |
joon874 | 0:5886f525a4ad | 42 | } |
MidnightCow | 1:728e5b5c8dae | 43 | } |
MidnightCow | 1:728e5b5c8dae | 44 | /* |
MidnightCow | 1:728e5b5c8dae | 45 | if(tmpJson && tmpJson->size() > 0) |
MidnightCow | 1:728e5b5c8dae | 46 | { |
MidnightCow | 1:728e5b5c8dae | 47 | strcpy(reply, (*tmpJson).serialize().c_str()); |
MidnightCow | 1:728e5b5c8dae | 48 | } |
MidnightCow | 1:728e5b5c8dae | 49 | else |
MidnightCow | 1:728e5b5c8dae | 50 | { |
MidnightCow | 1:728e5b5c8dae | 51 | strcpy(reply, "{\"Result\" : \"No defined Resource\"}"); |
MidnightCow | 1:728e5b5c8dae | 52 | } |
MidnightCow | 1:728e5b5c8dae | 53 | */ |
MidnightCow | 1:728e5b5c8dae | 54 | /* |
MidnightCow | 1:728e5b5c8dae | 55 | if(name) |
MidnightCow | 1:728e5b5c8dae | 56 | { |
MidnightCow | 1:728e5b5c8dae | 57 | printf("name=%s, tok=%s\r\n",name,tok); |
MidnightCow | 1:728e5b5c8dae | 58 | if(tok) |
MidnightCow | 1:728e5b5c8dae | 59 | { |
MidnightCow | 1:728e5b5c8dae | 60 | if(depth == 2) |
MidnightCow | 1:728e5b5c8dae | 61 | { |
MidnightCow | 1:728e5b5c8dae | 62 | if(!strcmp(name, "MAC")) |
MidnightCow | 1:728e5b5c8dae | 63 | strcpy(reply, "{\"Result\" : \"No Accessible\"}"); |
MidnightCow | 1:728e5b5c8dae | 64 | else |
MidnightCow | 1:728e5b5c8dae | 65 | { |
MidnightCow | 1:728e5b5c8dae | 66 | if(tmpJson->size() > 0) *tmpJson = std::string(tok); |
MidnightCow | 1:728e5b5c8dae | 67 | else *tmpJson = atoi(tok); |
MidnightCow | 1:728e5b5c8dae | 68 | strcpy(reply, (*tmpJson).serialize().c_str()); |
MidnightCow | 1:728e5b5c8dae | 69 | } |
MidnightCow | 1:728e5b5c8dae | 70 | } |
MidnightCow | 1:728e5b5c8dae | 71 | else if(depth == 1) strcpy(reply, "{\"Result : No Accessible\"}"); |
MidnightCow | 1:728e5b5c8dae | 72 | } |
MidnightCow | 1:728e5b5c8dae | 73 | else strcpy(reply, (*tmpJson).serialize().c_str()); |
joon874 | 0:5886f525a4ad | 74 | return; |
MidnightCow | 1:728e5b5c8dae | 75 | } |
MidnightCow | 1:728e5b5c8dae | 76 | strcpy(reply, "{\"Result\" : \"No defined Resource\"}"); |
MidnightCow | 1:728e5b5c8dae | 77 | */ |
MidnightCow | 1:728e5b5c8dae | 78 | if(name) |
MidnightCow | 1:728e5b5c8dae | 79 | { |
MidnightCow | 1:728e5b5c8dae | 80 | if(tok) |
MidnightCow | 1:728e5b5c8dae | 81 | { |
MidnightCow | 1:728e5b5c8dae | 82 | if(tmpJson->accessible) |
MidnightCow | 1:728e5b5c8dae | 83 | { |
MidnightCow | 1:728e5b5c8dae | 84 | printf("accessible : tmpJson->size()=%d\r\n",tmpJson->size()); |
MidnightCow | 1:728e5b5c8dae | 85 | if(tmpJson->size() > 0) {*tmpJson = std::string(tok); tmpJson->cb_action((void*)tok); printf("set string:%s\r\n",tok);} |
MidnightCow | 1:728e5b5c8dae | 86 | else {*tmpJson = atoi(tok); tmpJson->cb_action(&tmpJson->_value); printf("set int:%d\r\n",atoi(tok));} |
MidnightCow | 1:728e5b5c8dae | 87 | strcpy(reply, (*tmpJson).serialize().c_str()); |
MidnightCow | 1:728e5b5c8dae | 88 | } |
MidnightCow | 1:728e5b5c8dae | 89 | else strcpy(reply, "{\"Result : No Accessible\"}"); |
MidnightCow | 1:728e5b5c8dae | 90 | } |
MidnightCow | 1:728e5b5c8dae | 91 | else strcpy(reply, (*tmpJson).serialize().c_str()); |
MidnightCow | 1:728e5b5c8dae | 92 | } |
MidnightCow | 1:728e5b5c8dae | 93 | else strcpy(reply, "{\"Result\" : \"No defined Resource\"}"); |
MidnightCow | 1:728e5b5c8dae | 94 | return; |
joon874 | 0:5886f525a4ad | 95 | } |
joon874 | 0:5886f525a4ad | 96 | |
joon874 | 0:5886f525a4ad | 97 | void PutRequestHandler::handle(char* rest_uri, char *reply) |
joon874 | 0:5886f525a4ad | 98 | { |
joon874 | 0:5886f525a4ad | 99 | } |
joon874 | 0:5886f525a4ad | 100 | |
joon874 | 0:5886f525a4ad | 101 | void DeleteRequestHandler::handle(char* rest_uri, char *reply) |
joon874 | 0:5886f525a4ad | 102 | { |
joon874 | 0:5886f525a4ad | 103 | } |
joon874 | 0:5886f525a4ad | 104 | |
joon874 | 0:5886f525a4ad | 105 |