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
Diff: master.cpp
- Revision:
- 1:b5c534165dfe
- Parent:
- 0:88d3b9015f7c
- Child:
- 2:6bfe732ba6bc
diff -r 88d3b9015f7c -r b5c534165dfe master.cpp --- a/master.cpp Mon Feb 26 11:25:59 2018 +0000 +++ b/master.cpp Wed Mar 14 07:59:21 2018 +0000 @@ -1,92 +1,138 @@ #include "master.h" #include "command.h" +#include "EthernetInterface.h" #include <string.h> #include <stdio.h> +#define MAX_PACKET_SIZE 512 + namespace ProjectOne{ Serial pc(USBTX, USBRX); + + const char* Master::MASK = "255.255.255.0"; + const char* Master::GATEWAY = "192.168.0.1"; + + EthernetInterface eth_master; + UDPSocket sock_master; + Endpoint client; - Master::Master(void){ + Master::Master(int master_id){ + masterId = master_id; CurrentMasterState = STATE_INIT; - inputCommandArrayPtr = inputCommandArray; } void Master::handlePcData(void){ - sendMessageToPc("Listening for commands.\r\n"); + char in_buffer[MAX_PACKET_SIZE]; + char client_ip[] = {'1','9','2','.','1','6','8','.','0','.','x','x','x'}; + char ip_address[] = {'1','9','2','.','1','6','8','.','0','.','x','x','x'}; switch(CurrentMasterState){ case STATE_INIT: { - //TO DO UDP init + //serial init pc.baud(9600); - sendMessageToPc("--------------------------------"); - sendMessageToPc("Initiating serial Communication."); - sendMessageToPc("--------------------------------"); - CurrentMasterState = STATE_RECEIVING; + sendMessageToPc("-----------------"); + sendMessageToPc("Initiating master"); + sendMessageToPc("-----------------"); + + int temp[3]; + int i = 0; + do{ + temp[i] = masterId % 10; + masterId /= 10; + i++; + }while(masterId !=0); + + char tempi[4]; + sprintf(tempi, "%ld", temp[2]); + ip_address[10] = *tempi; + sprintf(tempi, "%ld", temp[1]); + ip_address[11] = *tempi; + sprintf(tempi, "%ld", temp[0]); + ip_address[12] = *tempi; + + eth_master.init(ip_address, MASK, GATEWAY); + eth_master.connect(); + + CurrentMasterState = STATE_RECEIVING_FROM_PC; break; } - case STATE_RECEIVING: + case STATE_RECEIVING_FROM_PC: { - sendMessageToPc("In state receiving.\r\n"); - char temp; + printf("Enter your command.\r\n"); + char temp1; - sendMessageToPc("In state receiving in if loop.\r\n"); - char_counter = 0; - for(int i = 0; i <= 19; i ++){ - temp = pc.getc(); - if((int)temp == 13){ - sendMessageToPc("Enter detected.\r\n"); - CurrentMasterState = STATE_HANDLE_STRING; + char_counter_command = 0; + for(int i = 0; i <= MAX_PACKET_SIZE; i ++){ + temp1 = pc.getc(); + if((int)temp1 == 13){ + CurrentMasterState = STATE_HANDLE_ID; break; } - //appendChar(inputCommandArray, temp, char_counter); - inputCommandArray[char_counter] = temp; - sendMessageToPc("Appending character to array.\r\n"); - char_counter++; + inputCommandArray[char_counter_command] = temp1; + char_counter_command++; } - CurrentMasterState = STATE_HANDLE_STRING; + printf("The command you entered is: '%s'\r\n", inputCommandArray); + CurrentMasterState = STATE_HANDLE_ID; break; } - case STATE_HANDLE_STRING: + case STATE_HANDLE_ID: + { + sendMessageToPc("Enter client ID:"); + char_counter_id = 0; + char temp2; + for(int i = 0; i <= 3; i ++){ + temp2 = pc.getc(); + if((int)temp2 == 13){ + CurrentMasterState = STATE_SEND_UDP_PACKET; + break; + } + client_id[char_counter_id] = temp2; + char_counter_id++; + } + client_ip[10] = client_id[0]; + client_ip[11] = client_id[1]; + client_ip[12] = client_id[2]; + client.set_address((const char *)client_ip,4000); + + CurrentMasterState = STATE_SEND_UDP_PACKET; + break; + } + case STATE_SEND_UDP_PACKET: { - sendMessageToPc("In state handle string.\r\n"); - char_counter = 0; - - sendMessageToPc(inputCommandArray); - - char command_type[3]; - char command[17]; - - strncpy(command_type, inputCommandArray, 3); - strncpy(command, inputCommandArray + 4, 17); - - sendMessageToPc(command_type); //type command PUT of GET - sendMessageToPc(command); //Welke command - - //Van hieruit frame maken en verzenden naar andere mbed met commando - - memset(&inputCommandArray[0], 0, sizeof(inputCommandArray)); - CurrentMasterState = STATE_RECEIVING; + sock_master.init(); + sock_master.sendTo(client, inputCommandArray, sizeof(inputCommandArray)); + memset(&inputCommandArray[0], 0, sizeof(inputCommandArray)); //clear input array + CurrentMasterState = STATE_HANDLE_RESPONSE; + break; + } + case STATE_HANDLE_RESPONSE: + { + memset(&in_buffer[0], 0, sizeof(in_buffer)); //buffer clear voor ontvangst + //start timer + int n = sock_master.receiveFrom(client, in_buffer, sizeof(in_buffer)); + sock_master.close(); + //stop timer, niet voltooid -> error state + printf("Received response: '%s'\r\n", in_buffer); + memset(&in_buffer[0], 0, sizeof(in_buffer)); //buffer clear na ontvangst + CurrentMasterState = STATE_RECEIVING_FROM_PC; + break; + } + case STATE_ERROR_STATE: + { + printf("Error: did not receive a response from the client in time"); + memset(&in_buffer[0], 0, sizeof(in_buffer)); //buffer clear + CurrentMasterState = STATE_RECEIVING_FROM_PC; break; } } } - //ACK 2.05 = 1, ACK 2.04 = 2, ACK 4.00 = 3 - void Master::sendAcknowledgement(int ack_type, int value_to_include){ //dit moet in UDP klasse - - } - void Master::sendMessageToPc(char *message_to_pc){ printf(message_to_pc); printf("\r\n"); } - - void Master::appendChar(char array[], char charToAppend, int position){ - - } - }