Webserver for peach-board

Committer:
thudt90
Date:
Thu Mar 12 08:26:27 2015 +0000
Revision:
1:f1f734dd23ee
Parent:
0:6ec14f880a00
Just test

Who changed what in which revision?

UserRevisionLine numberNew contents of line
thudt90 0:6ec14f880a00 1 #include "RPCCommand.h"
thudt90 0:6ec14f880a00 2 #include "mbed.h"
thudt90 0:6ec14f880a00 3 #include "RPCType.h"
thudt90 0:6ec14f880a00 4
thudt90 0:6ec14f880a00 5
thudt90 0:6ec14f880a00 6 RPCCommand::RPCCommand():
thudt90 1:f1f734dd23ee 7 cmd(),
thudt90 1:f1f734dd23ee 8 obj_name(NULL),
thudt90 1:f1f734dd23ee 9 func_name(NULL)
thudt90 0:6ec14f880a00 10 {
thudt90 0:6ec14f880a00 11
thudt90 0:6ec14f880a00 12 }
thudt90 0:6ec14f880a00 13
thudt90 0:6ec14f880a00 14 bool RPCCommand::decode(char *buffer)
thudt90 0:6ec14f880a00 15 {
thudt90 1:f1f734dd23ee 16 // buffer = /DigitalOut/new?arg=ytgf&name=thu
thudt90 0:6ec14f880a00 17 if(buffer == NULL)
thudt90 0:6ec14f880a00 18 return false;
thudt90 0:6ec14f880a00 19 if(buffer[0] != '/')
thudt90 0:6ec14f880a00 20 return false;
thudt90 1:f1f734dd23ee 21
thudt90 0:6ec14f880a00 22 ++buffer;
thudt90 1:f1f734dd23ee 23 // buffer = DigitalOut/new?arg=ytgf&name=thu
thudt90 0:6ec14f880a00 24 char *tmp = strchr(buffer ,'/');
thudt90 1:f1f734dd23ee 25 // tmp = /new?arg=ytgf&name=thu
thudt90 0:6ec14f880a00 26
thudt90 0:6ec14f880a00 27 if(tmp == NULL)
thudt90 0:6ec14f880a00 28 return false;
thudt90 0:6ec14f880a00 29 if(tmp == buffer)
thudt90 0:6ec14f880a00 30 return false;
thudt90 1:f1f734dd23ee 31
thudt90 0:6ec14f880a00 32 tmp[0] = '\0';
thudt90 1:f1f734dd23ee 33 // tmp = new?arg=ytgf&name=thu
thudt90 0:6ec14f880a00 34 obj_name = buffer;
thudt90 1:f1f734dd23ee 35 // obj_name = DigitalOut/new?arg=ytgf&name=thu
thudt90 1:f1f734dd23ee 36
thudt90 0:6ec14f880a00 37 buffer = tmp+1;
thudt90 1:f1f734dd23ee 38 //buffer = new?arg=ytgf&name=thu
thudt90 1:f1f734dd23ee 39
thudt90 0:6ec14f880a00 40 if(buffer[0] == '\0' || buffer[0] == '?')
thudt90 0:6ec14f880a00 41 return false;
thudt90 1:f1f734dd23ee 42
thudt90 0:6ec14f880a00 43 func_name = buffer;
thudt90 1:f1f734dd23ee 44 // func_name = new?arg=ytgf&name=thu
thudt90 1:f1f734dd23ee 45
thudt90 0:6ec14f880a00 46 tmp = strchr(buffer, '?');
thudt90 1:f1f734dd23ee 47 //tmp = ?arg=ytgf&name=thu
thudt90 1:f1f734dd23ee 48 if(tmp != NULL) {
thudt90 0:6ec14f880a00 49 if(tmp[1] == '\0')
thudt90 0:6ec14f880a00 50 return false;
thudt90 0:6ec14f880a00 51 tmp[0] = '\0';
thudt90 0:6ec14f880a00 52 }
thudt90 1:f1f734dd23ee 53
thudt90 0:6ec14f880a00 54 cmd[0] = '\0';
thudt90 1:f1f734dd23ee 55 //cmd[0] = null
thudt90 0:6ec14f880a00 56 strcat(cmd, "/");
thudt90 0:6ec14f880a00 57 strcat(cmd, obj_name);
thudt90 0:6ec14f880a00 58 strcat(cmd, "/");
thudt90 0:6ec14f880a00 59 strcat(cmd, func_name);
thudt90 1:f1f734dd23ee 60 //cmd = '\0'/DigitalOut/new?arg=ytgf&name=thu/new?arg=ytgf&name=thu
thudt90 0:6ec14f880a00 61
thudt90 0:6ec14f880a00 62 if(tmp == NULL)
thudt90 0:6ec14f880a00 63 return true;
thudt90 1:f1f734dd23ee 64
thudt90 0:6ec14f880a00 65 buffer = tmp+1;
thudt90 1:f1f734dd23ee 66 //buffer = arg=ytgf&name=thu
thudt90 1:f1f734dd23ee 67 do {
thudt90 0:6ec14f880a00 68 tmp = strchr(buffer, '&');
thudt90 1:f1f734dd23ee 69 //tmp = &name=thu
thudt90 1:f1f734dd23ee 70 // lan 2: tmp = null
thudt90 1:f1f734dd23ee 71
thudt90 1:f1f734dd23ee 72 if(tmp != NULL) {
thudt90 0:6ec14f880a00 73 if(tmp[1] == '\0' || buffer == tmp)
thudt90 0:6ec14f880a00 74 return false;
thudt90 0:6ec14f880a00 75 tmp[0] = '\0';
thudt90 1:f1f734dd23ee 76 // tmp = '\0'name=thu
thudt90 0:6ec14f880a00 77 }
thudt90 0:6ec14f880a00 78
thudt90 0:6ec14f880a00 79 char *sep = strchr(buffer, '=');
thudt90 1:f1f734dd23ee 80 //sep = =ytgf&name=thu
thudt90 1:f1f734dd23ee 81 // lan 2: sep = =thu
thudt90 0:6ec14f880a00 82 if(sep == NULL)
thudt90 0:6ec14f880a00 83 return false;
thudt90 0:6ec14f880a00 84 if(sep == buffer)
thudt90 0:6ec14f880a00 85 return false;
thudt90 0:6ec14f880a00 86 if(sep[1] == '\0' || sep[1] == '&')
thudt90 0:6ec14f880a00 87 return false;
thudt90 1:f1f734dd23ee 88
thudt90 0:6ec14f880a00 89 strcat(cmd, " ");
thudt90 1:f1f734dd23ee 90
thudt90 0:6ec14f880a00 91 strcat(cmd, sep+1);
thudt90 1:f1f734dd23ee 92 //cmd = '\0'/DigitalOut/new?arg=ytgf&name=thu/new?arg=ytgf&name=thu ytgf&name=thu
thudt90 1:f1f734dd23ee 93 // lan 2: cmd = '\0'/DigitalOut/new?arg=ytgf&name=thu/new?arg=ytgf&name=thu ytgf&name=thu thu
thudt90 1:f1f734dd23ee 94
thudt90 0:6ec14f880a00 95 if(tmp != NULL)
thudt90 0:6ec14f880a00 96 buffer = tmp+1;
thudt90 1:f1f734dd23ee 97 // buffer = name=thu
thudt90 0:6ec14f880a00 98 else
thudt90 0:6ec14f880a00 99 buffer = NULL;
thudt90 1:f1f734dd23ee 100 } while(buffer);
thudt90 1:f1f734dd23ee 101
thudt90 0:6ec14f880a00 102 return true;
thudt90 0:6ec14f880a00 103 }
thudt90 0:6ec14f880a00 104
thudt90 0:6ec14f880a00 105
thudt90 0:6ec14f880a00 106
thudt90 0:6ec14f880a00 107 char* RPCCommand::get_cmd() const
thudt90 0:6ec14f880a00 108 {
thudt90 0:6ec14f880a00 109 return (char*)cmd;
thudt90 0:6ec14f880a00 110 }
thudt90 0:6ec14f880a00 111
thudt90 0:6ec14f880a00 112 RPC_COMMAND_TYPE RPCCommand::get_type() const
thudt90 0:6ec14f880a00 113 {
thudt90 0:6ec14f880a00 114 if(!strcmp(func_name, "new") && RPCType::instance().is_supported_type(obj_name))
thudt90 0:6ec14f880a00 115 return CREATE;
thudt90 1:f1f734dd23ee 116
thudt90 0:6ec14f880a00 117 RPC* r = RPC::lookup(obj_name);
thudt90 0:6ec14f880a00 118 if(r == NULL)
thudt90 0:6ec14f880a00 119 return INVALID;
thudt90 1:f1f734dd23ee 120
thudt90 0:6ec14f880a00 121 if(!strcmp(func_name, "delete"))
thudt90 0:6ec14f880a00 122 return DELETE;
thudt90 1:f1f734dd23ee 123
thudt90 0:6ec14f880a00 124 const struct rpc_method *methods = r->get_rpc_methods();
thudt90 0:6ec14f880a00 125 int i = 0;
thudt90 1:f1f734dd23ee 126 while(methods[i].name != NULL) {
thudt90 1:f1f734dd23ee 127 if(!strcmp(func_name, methods[i].name)) {
thudt90 0:6ec14f880a00 128 return FUNCTION_CALL;
thudt90 0:6ec14f880a00 129 }
thudt90 0:6ec14f880a00 130 ++i;
thudt90 0:6ec14f880a00 131 }
thudt90 1:f1f734dd23ee 132
thudt90 0:6ec14f880a00 133 return INVALID;
thudt90 0:6ec14f880a00 134 }
thudt90 0:6ec14f880a00 135
thudt90 0:6ec14f880a00 136 char* RPCCommand::get_obj_name() const
thudt90 0:6ec14f880a00 137 {
thudt90 0:6ec14f880a00 138 return obj_name;
thudt90 0:6ec14f880a00 139 }
thudt90 0:6ec14f880a00 140
thudt90 0:6ec14f880a00 141 char* RPCCommand::get_func_name() const
thudt90 0:6ec14f880a00 142 {
thudt90 0:6ec14f880a00 143 return func_name;
thudt90 0:6ec14f880a00 144 }
thudt90 0:6ec14f880a00 145