diff --git a/src/entrypoints/dsgamma-test.cpp b/src/entrypoints/dsgamma-test.cpp index 79ca7f1af8b6f91a0a2a8b0ccdd83d5a4dea55c7..3d1f05a3d4d5f53ba5861e047429e700642f3ab4 100644 --- a/src/entrypoints/dsgamma-test.cpp +++ b/src/entrypoints/dsgamma-test.cpp @@ -20,24 +20,23 @@ * THE SOFTWARE. */ -#include <cstdio> #include "miosix.h" #include <drivers/gamma868/Gamma868.h> -#include <string.h> -#include <sys/types.h> -#include <sys/stat.h> -#include <fcntl.h> using namespace std; using namespace miosix; -#define PKT_LEN 24 + +/* + * STRANGE THING #1 (AKA BUG): + * Stack overflow if PKT_LEN in greater than 24 !!!!! + */ +#define PKT_LEN 14 typedef Gpio<GPIOG_BASE,13> greenLed; typedef Gpio<GPIOG_BASE,14> redLed; typedef Gpio<GPIOA_BASE,0> button; -int ping = 0; Gamma868 gamma("/dev/auxtty"); long long sendTime = 0; @@ -58,28 +57,34 @@ int main() { Thread::create(sender, STACK_MIN); - receiver(); + receiver(); //Runs in a loop return 0; } void sender(void *arg){ + + char msg[PKT_LEN]; + while(1){ + //Wait for button while(1){ - if(button::value()==1) break; //Wait for button + if(button::value()==1) break; } + //Prepare a packet of PKT_LEN chars with all # printf("Writing... \n"); - - char msg[PKT_LEN]; for(int i = 0; i < PKT_LEN; i++){ msg[i] = '#'; } + //Save current time sendTime = miosix::getTick(); nTentativi++; + + //Send gamma.send(msg); printf("Ok \n" ); @@ -88,31 +93,36 @@ void sender(void *arg){ } void receiver(){ - while(1){ - //read - // int len = 1; // SENDER ONLY - int len = PKT_LEN; - char inputBuf[len]; + + int len = PKT_LEN; + char inputBuf[len]; + long long arrivalTime; + + while(1){ + + //Read PKT_LEN bytes printf("Reading: \n"); gamma.receive(len, inputBuf); + //Print received chars for(int i = 0; i < len ; i++){ printf("Received: %c\n", inputBuf[i]); } - long long arrivalTime = miosix::getTick(); + //Calculate Round Trip Time + arrivalTime = miosix::getTick(); int rtt = arrivalTime - sendTime; printf("RTT: %d\n", rtt); printf("--------------RESULT------------"); tot += rtt; + + //Print Delay calculation + if(nTentativi > 0) printf("Tentativi: %d Media: %d \n", nTentativi, tot/(2*nTentativi)); - // RECEIVER ONLY - Thread::sleep(200); + //------------------ RECEIVER ONLY ----------------- gamma.send(inputBuf); - - - //Thread::sleep(1000); + //-------------------------------------------------- } } \ No newline at end of file diff --git a/src/shared/drivers/gamma868/Gamma868.cpp b/src/shared/drivers/gamma868/Gamma868.cpp index 86bba022693e65132e9f050c51f0f2cb158c3b73..08760408c8dd221c80fc724281f2cf43ab1ebfdb 100644 --- a/src/shared/drivers/gamma868/Gamma868.cpp +++ b/src/shared/drivers/gamma868/Gamma868.cpp @@ -21,11 +21,6 @@ */ #include "Gamma868.h" -#include <stdio.h> -#include <sys/types.h> -#include <sys/stat.h> -#include <fcntl.h> -#include <string.h> using namespace std; @@ -53,12 +48,13 @@ bool Gamma868::send(const char *msg) bool Gamma868::receive(int bufLen, char *buf) { - //TODO synchronize + pthread_mutex_lock(&readingMutex); char received[bufLen+1]; read(fd, &received, bufLen+1); for(int i = 0; i < bufLen; i++){ buf[i] = received[i]; } + pthread_mutex_unlock(&readingMutex); return true; } \ No newline at end of file diff --git a/src/shared/drivers/gamma868/Gamma868.h b/src/shared/drivers/gamma868/Gamma868.h index ebabe6fccaf6db90418730c6a6b66fe22c8e1a84..36169b74d0066bdebbce765768256208af8aace8 100644 --- a/src/shared/drivers/gamma868/Gamma868.h +++ b/src/shared/drivers/gamma868/Gamma868.h @@ -24,6 +24,11 @@ #define GAMMA868_H #include <pthread.h> +#include <stdio.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <string.h> struct Configuration{ int local_addr[3] = {127, 127, 127}; @@ -53,9 +58,8 @@ class Gamma868 { private: int fd; - //pthread_t thread; + pthread_t writeThread; pthread_mutex_t readingMutex; - pthread_cond_t readingCond; pthread_mutex_t writingMutex; pthread_cond_t writingCond; };