Daiki Kato / DhcpServer_mbed-os

Fork of DhcpServer by Daiki Kato

Files at this revision

API Documentation at this revision

Comitter:
dkato
Date:
Tue Jun 20 10:32:37 2017 +0000
Parent:
1:14ee6da645a9
Child:
3:bdea854c0ec8
Commit message:
Supports mbed os 5

Changed in this revision

DhcpServer.cpp Show annotated file Show diff for this revision Revisions of this file
DhcpServer.h Show annotated file Show diff for this revision Revisions of this file
--- a/DhcpServer.cpp	Wed Apr 06 03:11:10 2016 +0000
+++ b/DhcpServer.cpp	Tue Jun 20 10:32:37 2017 +0000
@@ -1,14 +1,14 @@
 #include "mbed.h"
 #include "rtos.h"
 #include "lwip/netif.h"
-#include "TCPSocketConnection.h"
-#include "TCPSocketServer.h"
-#include "Endpoint.h"
+#include "TCPSocket.h"
+#include "TCPServer.h"
+#include "SocketAddress.h"
 #include "UDPSocket.h"
-#include "dhcp.h"
+#include "prot/ip4.h"
+#include "prot/dhcp.h"
 #include "DhcpServer.h"
 
-#define CONNECT_NUM    (5)
 #define IP_ADDER_START (10)
 
 #define OFS_XID        (4)
@@ -19,26 +19,28 @@
 #define OFS_COOKIE     (236)
 #define OFS_OPTIONS    (240)
 
-static char receivebuff[500];
-static char sendbuff[300] = {0};
-static UDPSocket dhcp_server;
-static Thread * dhcpThread = NULL;
-static char chaddr_tbl[CONNECT_NUM][6] = {0};
-static const char mac_no_use[6] = {0, 0, 0, 0, 0, 0};
-
-
-static void dhcp_task_static(void const *argument) {
-    Endpoint client;
+void DhcpServer::dhcp_process(void) {
+    SocketAddress client;
     int cnt;
     int tbl_index;
+    int option;
+    nsapi_addr_t addr;
+    const char mac_no_use[6] = {0, 0, 0, 0, 0, 0};
 
     dhcp_server.bind(67);
-    dhcp_server.set_broadcasting(true);
+    option = 1;
+    dhcp_server.setsockopt(0xfff, 0x0020, &option, sizeof(option));
 
     while (true) {
-        int n = dhcp_server.receiveFrom(client, receivebuff, sizeof(receivebuff));
+        int n = dhcp_server.recvfrom(&client, receivebuff, 500);
         if (n > 0) {
-            client.set_address("255.255.255.255", 68);
+            addr.version = NSAPI_IPv4;
+            addr.bytes[0] = 255;
+            addr.bytes[1] = 255;
+            addr.bytes[2] = 255;
+            addr.bytes[3] = 255;
+            client.set_addr(addr);
+            client.set_port(68);
 
             sendbuff[OFS_XID + 0]    = receivebuff[OFS_XID + 0];
             sendbuff[OFS_XID + 1]    = receivebuff[OFS_XID + 1];
@@ -93,14 +95,14 @@
                     sendbuff[OFS_YIADDR + 2] = sendbuff[OFS_SIADDR + 2];
                     sendbuff[OFS_YIADDR + 3] = IP_ADDER_START + tbl_index;
                 }
-                dhcp_server.sendTo(client, sendbuff, 300);
+                dhcp_server.sendto(client, sendbuff, 300);
             } else if (receivebuff[OFS_OPTIONS + 2] == DHCP_REQUEST) {
                 if (tbl_index != -1) {
                     sendbuff[OFS_OPTIONS + 2]  = DHCP_ACK;
                 } else {
                     sendbuff[OFS_OPTIONS + 2]  = DHCP_NAK;
                 }
-                dhcp_server.sendTo(client, sendbuff, 300);
+                dhcp_server.sendto(client, sendbuff, 300);
             } else if (receivebuff[OFS_OPTIONS + 2] == DHCP_RELEASE) {
                 if (tbl_index != -1) {
                     memset(chaddr_tbl[tbl_index], 0, 6);
@@ -112,12 +114,18 @@
     }
 }
 
-DhcpServer::DhcpServer(char * name, char * ipadder) {
-    int i;
-    int len;
-    int ofs;
+DhcpServer::DhcpServer(NetworkInterface *net, char * name) :
+ dhcp_server(net), dhcpThread(osPriorityNormal, (1024 * 8)) {
+    uint32_t i;
+    uint32_t len;
+    uint32_t ofs;
 
-    sscanf(ipadder, "%d.%d.%d.%d", (int *)&sendbuff[OFS_SIADDR + 0], (int *)&sendbuff[OFS_SIADDR + 1],
+    receivebuff = new char[500];
+    sendbuff = new char[300];
+    memset(sendbuff, 0, 300);
+    memset(chaddr_tbl, 0, sizeof(chaddr_tbl));
+
+    sscanf(net->get_ip_address(), "%d.%d.%d.%d", (int *)&sendbuff[OFS_SIADDR + 0], (int *)&sendbuff[OFS_SIADDR + 1],
      (int *)&sendbuff[OFS_SIADDR + 2], (int *)&sendbuff[OFS_SIADDR + 3]);
 
     len = strlen(name);
@@ -177,13 +185,10 @@
 
     sendbuff[ofs++] = 0xff;
 
-    if (dhcpThread == NULL) {
-        dhcpThread = new Thread(&dhcp_task_static, NULL, osPriorityNormal, (1024 * 8));
-    }
+    dhcpThread.start(callback(this, &DhcpServer::dhcp_process));
 }
 
 DhcpServer::~DhcpServer() {
-    if (dhcpThread != NULL) {
-        delete dhcpThread;
-    }
+    delete [] receivebuff;
+    delete [] sendbuff;
 }
--- a/DhcpServer.h	Wed Apr 06 03:11:10 2016 +0000
+++ b/DhcpServer.h	Tue Jun 20 10:32:37 2017 +0000
@@ -14,14 +14,25 @@
 public:
     /** Constructor: Initializes DhcpServer.
      *
+     * @param net Common interface that is shared between network devices
      * @param name a pointer to a string containing the server name. (e.g."ServerName")
-     * @param ipadder a pointer to a string containing the server IP address. (e.g."192.168.0.1")
      */
-    DhcpServer(char * name, char * ipadder);
+    DhcpServer(NetworkInterface *net, char * name);
 
     /** Destructor
      *
      */
     virtual ~DhcpServer();
+
+private:
+    #define CONNECT_NUM    (5)
+
+    UDPSocket dhcp_server;
+    Thread dhcpThread;
+    char * receivebuff;
+    char * sendbuff;
+    char chaddr_tbl[CONNECT_NUM][6];
+
+    void dhcp_process(void);
 };
 #endif /* DHCP_SERVER_H_ */