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.
Dependencies: NySNICInterface mbed-rtos mbed
Fork of RESTServerSample by
RequestHandler.cpp
- Committer:
- oks486
- Date:
- 2015-02-15
- Revision:
- 4:99a67256b765
- Parent:
- 0:998e2e00df0c
File content as of revision 4:99a67256b765:
#include "mbed.h"
#include "RequestHandler.h"
#include "RPCObject.h"
#include "HTTPServer.h"
int GetRequestHandler::handle(RPCObject& cmd, char* reply)
{
int value;
std::map<PinName, RPCClass*>::iterator itor;
printf("handling GET request.\r\n");
itor = cmd.pinObjects.find(cmd.get_pin_name());
if(itor == cmd.pinObjects.end()){
printf("The pin is not created.\r\n");
return HTTP_404_NOTFOUND;
}
value = itor->second->read();
reply[0] = '0' + value;
reply[1] = '\0';
return HTTP_200_OK;
}
int PostRequestHandler::handle(RPCObject& cmd, char* reply)
{
int value = cmd.get_value();
std::map<PinName, RPCClass*>::iterator itor;
printf("handling POST request.\r\n");
switch(value){
case 0:
case 1:
//update
printf("now updating the object to %d.\r\n", value);
itor = cmd.pinObjects.find(cmd.get_pin_name());
if(itor == cmd.pinObjects.end()){
printf("The pin is not created.\r\n");
return HTTP_404_NOTFOUND;
}
itor->second->write(value);
break;
case -1:
//create
printf("now createing the object.\r\n");
if(!cmd.create_pin_object(reply)){
return -1;
}
break;
case -2:
// delete
itor = cmd.pinObjects.find(cmd.get_pin_name());
if(itor == cmd.pinObjects.end()){
printf("The pin is not created.\r\n");
return HTTP_404_NOTFOUND;
}
delete itor->second;
cmd.pinObjects.erase(cmd.pinObjects.find(cmd.get_pin_name()));
break;
default:
return -1;
}
return HTTP_200_OK;
}
int DeleteRequestHandler::handle(RPCObject& cmd, char* reply)
{
std::map<PinName, RPCClass*>::iterator itor;
printf("handling DELETE request.\r\n");
itor = cmd.pinObjects.find(cmd.get_pin_name());
if(itor == cmd.pinObjects.end()){
printf("The pin is not created.\r\n");
return HTTP_404_NOTFOUND;
}
delete itor->second;
cmd.pinObjects.erase(cmd.pinObjects.find(cmd.get_pin_name()));
return HTTP_200_OK;
}

