Free (GPLv2) TCP/IP stack developed by TASS Belgium

Fork of PicoTCP by Daniele Lacamera

Files at this revision

API Documentation at this revision

Comitter:
daniele
Date:
Sun Jun 16 02:31:38 2013 +0000
Parent:
26:dc3e7f96338f
Child:
28:394d88a116b2
Commit message:
added dsr option for devices during async event

Changed in this revision

Socket/bsd/proxy_endpoint.h Show annotated file Show diff for this revision Revisions of this file
Socket/bsd/stack_endpoint.cpp Show annotated file Show diff for this revision Revisions of this file
include/pico_queue.h Show annotated file Show diff for this revision Revisions of this file
--- a/Socket/bsd/proxy_endpoint.h	Sat Jun 15 18:38:26 2013 +0000
+++ b/Socket/bsd/proxy_endpoint.h	Sun Jun 16 02:31:38 2013 +0000
@@ -107,7 +107,7 @@
 int picotcp_setblocking(struct stack_endpoint *,int blocking);
 int picotcp_join_multicast(struct stack_endpoint *,const char* address,const char* local);
 
-int picotcp_async_interrupt(void);
+int picotcp_async_interrupt(void *);
 
 
 #endif
--- a/Socket/bsd/stack_endpoint.cpp	Sat Jun 15 18:38:26 2013 +0000
+++ b/Socket/bsd/stack_endpoint.cpp	Sun Jun 16 02:31:38 2013 +0000
@@ -19,7 +19,7 @@
 Mutex *PicoTcpLock;
 Queue<void,10> *PicoTcpEvents;
 
-
+static struct stack_endpoint *ep_accepting;
 static Thread * serverThread = NULL;
 
 #define pt_proxy_dbg(...) 
@@ -57,8 +57,13 @@
 {
   struct stack_endpoint *ep = (struct stack_endpoint *)s->priv;
   if (!ep) {
-    printf("WAKEUP: no socket :(\n");
-    return;
+    if (ep_accepting != NULL) {
+        printf("Delivering %02x to accepting socket...\n", ev);
+        ep = ep_accepting;
+    } else {
+        printf("WAKEUP: socket not found! ev=%04x\n", ev);
+        return;
+    }
   }
   ep->revents |= ev;
 
@@ -86,6 +91,7 @@
     printf("Error opening socket!\n");
   } else {
     ep->s->priv = ep;
+    printf("Added socket (open)\n");
     ep->queue = new Queue<void,1>();
   }
   PicoTcpLock->unlock();
@@ -122,6 +128,10 @@
   int ret;
   PicoTcpLock->lock();
   ret = pico_socket_listen(ep->s, queue);
+  ep_accepting = (struct stack_endpoint *) pico_zalloc(sizeof(struct stack_endpoint));
+  ep_accepting->queue = new Queue<void,1>();
+  if (!ep_accepting)
+    ret = -1;
   PicoTcpLock->unlock();
   return ret;
 }
@@ -150,19 +160,28 @@
 struct stack_endpoint *picotcp_accept(struct stack_endpoint *ep, struct sockaddr *_cli_addr, socklen_t *len)
 {
   int retval;
-  struct stack_endpoint *aep = (struct stack_endpoint *) pico_zalloc(sizeof(struct stack_endpoint));
+  struct stack_endpoint *aep = ep_accepting;
   struct sockaddr_in *cli_addr = (struct sockaddr_in *)_cli_addr;
+  ep_accepting = (struct stack_endpoint *) pico_zalloc(sizeof(struct stack_endpoint));
+  if (ep_accepting)
+    ep_accepting->queue = new Queue<void,1>();
+  
+  
   if (!aep)
     return aep;
+  
   PicoTcpLock->lock();
   ep->events = PICO_SOCK_EV_CONN | PICO_SOCK_EV_ERR;
   __critical_select(ep, osWaitForever);
   if (ep->revents & PICO_SOCK_EV_CONN) {
+    printf("Calling Accept\n");
     aep->s = pico_socket_accept(ep->s, (struct pico_ip4 *)(&cli_addr->sin_addr.s_addr), &cli_addr->sin_port);
+    printf("Accept returned\n");
+    aep->s->priv = aep;
     ep->revents &= (~PICO_SOCK_EV_CONN);
     aep->revents |= PICO_SOCK_EV_WR;
-    aep->s->priv = aep;
-    aep->queue = new Queue<void,1>();
+    printf("Added socket (accept)\n");
+    
     *len = sizeof(struct sockaddr_in);
     ptsock_dbg("Established. sock state: %x\n", aep->s->state);
   } else {
@@ -223,10 +242,15 @@
   PicoTcpLock->lock();
   while (tot_len < len) {
     retval = pico_socket_read(ep->s, ((uint8_t *)buf) + tot_len ,  len - tot_len);
-    if (retval <= 0) {
-        ep->revents &= ~PICO_SOCK_EV_RD;
+    if (retval == 0) {
+        if (tot_len < len)
+            ep->revents &= ~PICO_SOCK_EV_RD;
         break;
     }
+    if (retval < 0) {
+       tot_len = -1;
+       break;
+    }
     tot_len += retval;
   }
   PicoTcpLock->unlock();
@@ -242,14 +266,21 @@
   PicoTcpLock->lock();
   while (tot_len < len) {
     retval = pico_socket_write(ep->s, ((uint8_t *)buf) + tot_len ,  len - tot_len);
-    if (retval <= 0) {
-        ep->revents &= ~PICO_SOCK_EV_WR;
+    retval = pico_socket_read(ep->s, ((uint8_t *)buf) + tot_len ,  len - tot_len);
+    if (retval == 0) {
+        if (tot_len < len)
+            ep->revents &= ~PICO_SOCK_EV_RD;
         break;
     }
+    if (retval < 0) {
+       tot_len = -1;
+       break;
+    }
     tot_len += retval;
-    picotcp_async_interrupt();
+    
   }
   PicoTcpLock->unlock();
+  picotcp_async_interrupt(NULL);
   return tot_len; 
 }
 
@@ -289,11 +320,27 @@
 {
   (void)arg;
   int ret = 0;
+  struct pico_device *dev;
   while(1) {
-    PicoTcpEvents->get(2);
-    if (PicoTcpLock->trylock()) {
+    
+    osEvent evt = PicoTcpEvents->get(2);
+    
+    if (evt.status == osEventMessage) {
+        dev = (struct pico_device *)evt.value.p;
+    } else {
+        dev = NULL;
+    }
+    
+    if (dev && dev->dsr){ 
+      PicoTcpLock->lock();
+      dev->dsr(dev, 200);
       pico_stack_tick();
       PicoTcpLock->unlock();
+    } else {
+      if (PicoTcpLock->trylock()) {
+        pico_stack_tick();
+        PicoTcpLock->unlock();
+      }
     }
   }
 }
@@ -314,7 +361,7 @@
   picotcp_start(); 
 }
 
-int picotcp_async_interrupt(void)
+int picotcp_async_interrupt(void *arg)
 {
-    PicoTcpEvents->put((void *)0);
-}
\ No newline at end of file
+    PicoTcpEvents->put(arg);
+}
--- a/include/pico_queue.h	Sat Jun 15 18:38:26 2013 +0000
+++ b/include/pico_queue.h	Sun Jun 16 02:31:38 2013 +0000
@@ -9,7 +9,7 @@
 #include "pico_config.h"
 #include "pico_frame.h"
 
-#define Q_LIMIT 2000
+#define Q_LIMIT 0
 
 #ifndef NULL
 #define NULL ((void *)0)