各ピンへのread/writeを提供するサーバサンプル
Dependencies: NySNICInterface mbed-rtos mbed
Fork of RESTServerSample2 by
RPCObject.cpp@1:e5d3bd4af9da, 2015-03-12 (annotated)
- Committer:
- komoritan
- Date:
- Thu Mar 12 12:40:48 2015 +0000
- Revision:
- 1:e5d3bd4af9da
- Parent:
- 0:998e2e00df0c
Bug fix - handle_request
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
komoritan | 0:998e2e00df0c | 1 | #include "RPCObject.h" |
komoritan | 0:998e2e00df0c | 2 | #include "parse_pins.h" |
komoritan | 0:998e2e00df0c | 3 | #include "mbed.h" |
komoritan | 0:998e2e00df0c | 4 | #include "HTTPServer.h" |
komoritan | 0:998e2e00df0c | 5 | |
komoritan | 0:998e2e00df0c | 6 | |
komoritan | 0:998e2e00df0c | 7 | RPCObject::RPCObject() |
komoritan | 0:998e2e00df0c | 8 | { |
komoritan | 0:998e2e00df0c | 9 | } |
komoritan | 0:998e2e00df0c | 10 | |
komoritan | 0:998e2e00df0c | 11 | |
komoritan | 0:998e2e00df0c | 12 | int RPCObject::decode(char* request, char* reply) |
komoritan | 0:998e2e00df0c | 13 | { |
komoritan | 0:998e2e00df0c | 14 | char* clz = strtok(request+1,"/"); |
komoritan | 0:998e2e00df0c | 15 | char* pin = strtok(NULL, "/"); |
komoritan | 0:998e2e00df0c | 16 | char* val = strtok(NULL, "/"); |
komoritan | 0:998e2e00df0c | 17 | |
komoritan | 0:998e2e00df0c | 18 | if(!strcmp(clz, "DigitalIn")){ |
komoritan | 0:998e2e00df0c | 19 | type = RPC_PIN_DIGITAL_IN; |
komoritan | 0:998e2e00df0c | 20 | printf(" type is DigitalIn. \r\n"); |
komoritan | 0:998e2e00df0c | 21 | } else if(!strcmp(clz, "DigitalOut")){ |
komoritan | 0:998e2e00df0c | 22 | type = RPC_PIN_DIGITAL_OUT; |
komoritan | 0:998e2e00df0c | 23 | printf(" type is DigitalOut. \r\n"); |
komoritan | 0:998e2e00df0c | 24 | } else if(!strcmp(clz, "DigitalInOut")){ |
komoritan | 0:998e2e00df0c | 25 | type = RPC_PIN_DIGITAL_INOUT; |
komoritan | 0:998e2e00df0c | 26 | printf(" type is DigitalInOut. \r\n"); |
komoritan | 0:998e2e00df0c | 27 | } else { |
komoritan | 0:998e2e00df0c | 28 | type = RPC_PIN_UNKNOWN; |
komoritan | 0:998e2e00df0c | 29 | printf("Unsupported type name: %s. \r\n", clz); |
komoritan | 0:998e2e00df0c | 30 | sprintf(reply, "Unsupported type name: %s. \r\n", clz); |
komoritan | 0:998e2e00df0c | 31 | return HTTP_400_BADREQUEST; |
komoritan | 0:998e2e00df0c | 32 | } |
komoritan | 0:998e2e00df0c | 33 | |
komoritan | 0:998e2e00df0c | 34 | pin_name = parse_pins(pin); |
komoritan | 0:998e2e00df0c | 35 | if(pin_name == NC){ |
komoritan | 0:998e2e00df0c | 36 | printf("Unsupported pin name: %s. \n", pin); |
komoritan | 0:998e2e00df0c | 37 | sprintf(reply, "Unsupported pin name: %s. \r\n", pin); |
komoritan | 0:998e2e00df0c | 38 | return HTTP_400_BADREQUEST; |
komoritan | 0:998e2e00df0c | 39 | } |
komoritan | 0:998e2e00df0c | 40 | |
komoritan | 0:998e2e00df0c | 41 | if(!val || val[0] == '\0'){ |
komoritan | 0:998e2e00df0c | 42 | value = -1; |
komoritan | 0:998e2e00df0c | 43 | } |
komoritan | 0:998e2e00df0c | 44 | else if(!strcmp(val, "delete")){ |
komoritan | 0:998e2e00df0c | 45 | value = -2; |
komoritan | 0:998e2e00df0c | 46 | } |
komoritan | 0:998e2e00df0c | 47 | else { |
komoritan | 0:998e2e00df0c | 48 | value = (val[0] - '0') ? 1 : 0; |
komoritan | 0:998e2e00df0c | 49 | } |
komoritan | 0:998e2e00df0c | 50 | |
komoritan | 0:998e2e00df0c | 51 | return 0; |
komoritan | 0:998e2e00df0c | 52 | } |
komoritan | 0:998e2e00df0c | 53 | |
komoritan | 0:998e2e00df0c | 54 | |
komoritan | 0:998e2e00df0c | 55 | bool RPCObject::create_pin_object(char* reply) |
komoritan | 0:998e2e00df0c | 56 | { |
komoritan | 0:998e2e00df0c | 57 | RPCClass* pinobj; |
komoritan | 0:998e2e00df0c | 58 | |
komoritan | 0:998e2e00df0c | 59 | if(pinObjects.find(pin_name) != pinObjects.end()){ |
komoritan | 0:998e2e00df0c | 60 | printf("The pin already exists.\r\n"); |
komoritan | 0:998e2e00df0c | 61 | strcat(reply, "The pin already exists. "); |
komoritan | 0:998e2e00df0c | 62 | return false; |
komoritan | 0:998e2e00df0c | 63 | } |
komoritan | 0:998e2e00df0c | 64 | |
komoritan | 0:998e2e00df0c | 65 | switch(type){ |
komoritan | 0:998e2e00df0c | 66 | case RPC_PIN_DIGITAL_IN: |
komoritan | 0:998e2e00df0c | 67 | printf("DigitalIn.\r\n"); |
komoritan | 0:998e2e00df0c | 68 | pinobj = new RPCDigitalIn(pin_name); |
komoritan | 0:998e2e00df0c | 69 | break; |
komoritan | 0:998e2e00df0c | 70 | case RPC_PIN_DIGITAL_OUT: |
komoritan | 0:998e2e00df0c | 71 | printf("DigitalOut.\r\n"); |
komoritan | 0:998e2e00df0c | 72 | pinobj = new RPCDigitalOut(pin_name); |
komoritan | 0:998e2e00df0c | 73 | break; |
komoritan | 0:998e2e00df0c | 74 | case RPC_PIN_DIGITAL_INOUT: |
komoritan | 0:998e2e00df0c | 75 | printf("DigitalInOut.\r\n"); |
komoritan | 0:998e2e00df0c | 76 | pinobj = new RPCDigitalInOut(pin_name); |
komoritan | 0:998e2e00df0c | 77 | break; |
komoritan | 0:998e2e00df0c | 78 | default: |
komoritan | 0:998e2e00df0c | 79 | printf(" Unsupported type.\r\n"); |
komoritan | 0:998e2e00df0c | 80 | strcat(reply, "Unsupported type. "); |
komoritan | 0:998e2e00df0c | 81 | return false; |
komoritan | 0:998e2e00df0c | 82 | } |
komoritan | 0:998e2e00df0c | 83 | |
komoritan | 0:998e2e00df0c | 84 | pinObjects[pin_name] = pinobj; |
komoritan | 0:998e2e00df0c | 85 | |
komoritan | 0:998e2e00df0c | 86 | return true; |
komoritan | 0:998e2e00df0c | 87 | } |