Part One of my Project Course. Implementation of simple I/O and a custom defined protocol over UDP/IP.

Dependencies:   C12832 LM75B mbed EthernetInterface mbed-rtos

Files at this revision

API Documentation at this revision

Comitter:
bertgereels
Date:
Fri Mar 23 13:42:13 2018 +0000
Parent:
2:6bfe732ba6bc
Commit message:
Cleared response array before constructing ack.

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
master.cpp Show annotated file Show diff for this revision Revisions of this file
master.h Show annotated file Show diff for this revision Revisions of this file
slave.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Mon Mar 19 11:32:54 2018 +0000
+++ b/main.cpp	Fri Mar 23 13:42:13 2018 +0000
@@ -9,8 +9,6 @@
 #include "selection.h"
    
 using namespace ProjectOne;
-    
-BusIn joy(p14); //enkel verticaal
 
 int main() {
     BuzzerMusic buzzerMusic;
--- a/master.cpp	Mon Mar 19 11:32:54 2018 +0000
+++ b/master.cpp	Fri Mar 23 13:42:13 2018 +0000
@@ -94,6 +94,9 @@
                 client_ip[10] = client_id[0];
                 client_ip[11] = client_id[1];
                 client_ip[12] = client_id[2];
+                
+                client_id[3] = '\0';
+                
                 client.set_address((const char *)client_ip,4000);
 
                 CurrentMasterState = STATE_SEND_UDP_PACKET;
--- a/master.h	Mon Mar 19 11:32:54 2018 +0000
+++ b/master.h	Fri Mar 23 13:42:13 2018 +0000
@@ -50,7 +50,7 @@
             masterStates CurrentMasterState;
 
             char inputCommandArray[512];
-            char client_id[3];
+            char client_id[4];
             int char_counter_command, char_counter_id;
             
             int masterId;
--- a/slave.cpp	Mon Mar 19 11:32:54 2018 +0000
+++ b/slave.cpp	Fri Mar 23 13:42:13 2018 +0000
@@ -27,9 +27,8 @@
     
     void Slave::handleIncomingFrame(void){
         char in_buffer[MAX_PACKET_SIZE];
-        //char *command_info[3];
-        //char ip_address[] = {'1','9','2','.','1','6','8','.','0','.','x','x','x'};
-        char ack_id[3];
+        char ack_id[4];
+        char response_array[512];
         switch(CurrentSlaveState){
             case STATE_INIT:
             {
@@ -92,18 +91,15 @@
                 
                 char *p = strtok(in_buffer, " ");
                 command_info[0] = p;
-                printf("Eerste strtok: '%s'\r\n", command_info[0]);
                 char *q = strtok(NULL, " ");
                 command_info[1] = q;
-                printf("Tweede strtok: '%s'\r\n", command_info[1]);
                 char *s = strtok(NULL, '\0');
-                command_info[2] = s;
-                printf("Derde strtok: '%s'\r\n", command_info[2]);
-                
+                command_info[2] = s;         
                 
                 ack_id[0] = ip_address[10];
                 ack_id[1] = ip_address[11];
                 ack_id[2] = ip_address[12];
+                ack_id[3] = '\0';
                 
                 CurrentSlaveState = STATE_HANDLE_REQUEST;
                 break;
@@ -115,7 +111,8 @@
                 temperatureValue = 0.0;
                 potentiometerValue = 0.0;
                 
-                response = "";
+                response.clear();
+                memset(&response_array[0], 0, sizeof(response_array));
                 
                 if(strcmp(command_info[0], "PUT") == 0){
                     if(strcmp(command_info[1], "/LCD") == 0){
@@ -134,7 +131,7 @@
                     }
                     else
                     {
-                    response.append("ACK").append(" ").append("4.0").append("").append(ack_id);
+                    response.append("ACK").append(" ").append("4.0").append(" ").append(ack_id);
                     }
 
                 }
@@ -163,9 +160,11 @@
             }
             case STATE_SEND_ACKNOWLEDGEMENT:
             {
-                sock_slave.sendTo(master,(char *)response.c_str(), 512);
+                strcpy(response_array, response.c_str());
+                sock_slave.sendTo(master,(char *)response_array, 512);
  
-                memset(&response[0], 0, sizeof(response));
+                response.clear();
+
                 CurrentSlaveState = STATE_WAIT_FOR_FRAME;
                 break;
             }