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
server.h
00001 #pragma once 00002 00003 00004 #include "coap.h" 00005 #include "cantcoap.h" 00006 #include "response.h" 00007 #include "request.h" 00008 00009 #include <vector> 00010 00011 enum CoapCode; 00012 00013 /** Coap Server class 00014 * Used to process and respond to coap requests 00015 * 00016 * @code 00017 * #include "mbed.h" 00018 * #include "coap.h" 00019 * 00020 * void get_demo(Request* req, Response* res) 00021 * { 00022 * // do your stuff here 00023 * } 00024 * 00025 * int main() { 00026 * Server server; 00027 * 00028 * server.add("/demo", &get_led, GET); 00029 * 00030 * while(1) 00031 * { 00032 * server.waitForRequest(); 00033 * }; 00034 * } 00035 * @endcode 00036 */ 00037 class Server{ 00038 00039 public: 00040 /** Create a new Server instance */ 00041 Server(); 00042 00043 /** Add a coap resource 00044 * 00045 * @param uri The resource URI to respond on 00046 * @param function The corresponding function to call when a request for this resource is received 00047 * @param method The method to respond to (GET, POST, PUT or DELETE) 00048 * 00049 */ 00050 void add(char* uri, void (*function)(Request*, Response*), CoapCode method); 00051 00052 /** Wait for a request and process that request 00053 * @note This is usually placed inside a loop 00054 */ 00055 void waitForRequest(); 00056 00057 /** Enable broadcast functionality 00058 * @param broadcast Boolean value to enable or disable broadcasting functionality 00059 */ 00060 void enableBroadcast(int broadcast = true); 00061 00062 char* getIpAddress(); 00063 00064 00065 private: 00066 EthernetInterface eth; 00067 UDPSocket server; 00068 Endpoint client; 00069 00070 struct Resource{ 00071 char* uri; 00072 void (*function)(Request*, Response*); 00073 int method; 00074 }; 00075 00076 std::vector<Resource> resources; 00077 00078 int findResource(char* uri, CoapPDU::Code method); 00079 00080 static const int UDP_BUFFER_SIZE = 256; 00081 static const int TOKEN_BUFFER_SIZE = 8; 00082 static const int URI_BUFFER_SIZE = 64; 00083 00084 };
Generated on Wed Jul 13 2022 18:09:57 by
1.7.2
