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: DebugLib EthernetInterface cantcoap mbed-rtos
Fork of yeswecancoap by
Diff: server.cpp
- Revision:
- 7:1bed29e1b0a4
- Parent:
- 6:0c477f5b79ff
- Child:
- 11:642eaabf1c2b
--- a/server.cpp Wed Oct 21 08:51:38 2015 +0000
+++ b/server.cpp Wed Oct 21 10:23:21 2015 +0000
@@ -26,38 +26,54 @@
printf("\r\nWaiting for UDP packet...\r\n");
int size = server.receiveFrom(client, buffer, sizeof(buffer));
buffer[size] = '\0';
-
- char uriBuffer[URI_BUFFER_SIZE];
- int uriLength;
- uint8_t token[TOKEN_BUFFER_SIZE];
- uint8_t tokenLength;
CoapPDU *req = new CoapPDU((uint8_t*)buffer,256, size);
if(req->validate()) {
+
+ char uriBuffer[URI_BUFFER_SIZE];
+ int uriLength;
+ uint8_t token[TOKEN_BUFFER_SIZE];
+ uint8_t tokenLength;
+
req->printHuman();
req->getURI(uriBuffer,URI_BUFFER_SIZE,&uriLength);
tokenLength = req->getTokenLength();
memcpy(token, req->getTokenPointer(), tokenLength);
- }
+
+ int resourceIndex = findResource(uriBuffer, req->getCode());
-
+
+ CoapPDU *res = new CoapPDU();
+ res->setMessageID(req->getMessageID());
+ res->setToken(token,4);
+
+ switch(req->getType()){
+ case Response::COAP_NON_CONFIRMABLE:
+ res->setType(Response::COAP_NON_CONFIRMABLE);
+ break;
+ case Response::COAP_CONFIRMABLE:
+ res->setType(Response::COAP_ACKNOWLEDGEMENT);
+ }
+
+ res->setCode(Response::COAP_NOT_FOUND);
+
+ if(resourceIndex != -1){
+ res->setCode(Response::COAP_NOT_IMPLEMENTED);
+ resources[resourceIndex].function((Request*)req, (Response*)res);
+ }
+
+ server.sendTo(client, (char*) res->getPDUPointer(),res->getPDULength());
+
+ delete res;
+ delete req;
+ }
+}
+
+int Server::findResource(char* uri, CoapPDU::Code code)
+{
for(int i = 0; i < resources.size(); i++){
- if(strcmp(uriBuffer, resources[i].uri) == 0){
-
- CoapPDU *res = new CoapPDU();
- res->setType(Response::COAP_ACKNOWLEDGEMENT);
- res->setCode(Response::COAP_CONTENT);
- res->setMessageID(req->getMessageID());
- res->setToken(token,4);
-
- resources[i].function((Request*)req, (Response*)res);
-
- server.sendTo(client, (char*) res->getPDUPointer(),res->getPDULength());
- delete res;
-
- }
+ if(strcmp(uri, resources[i].uri) == 0) return i;
}
-
- delete req;
+ return -1;
}
\ No newline at end of file
