/****************************************************************************** * * @brief On packet reception, process the data * BSL-based protocol expects: * HEADER = 0x80 * Lenght = lenght of CMD + [ADDR] + [DATA] * CMD = 1 byte with the corresponding command * ADDR = optional address depending on command * DATA = optional data depending on command * CHKSUM = 2 bytes (L:H) with CRC checksum of CMD + [ADDR] + [DATA] * * @return RET_OK: Communication protocol in progress * RET_JUMP_TO_APP: Last byte received, request jump to application *****************************************************************************/ uint8_t TI_MSPBoot_CI_Process(void) { uint8_t ret = RET_NO_DATA; uint8_t sendAck; if (readPacket()) // On complete packet reception { if ((gPacket.length > PACKET_DESTINATION) && (gPacket.data[PACKET_PROTOCOL] == MSP430_BSL_PROTOCOL) && (gPacket.data[PACKET_DESTINATION] == gPacket.myAddr)) { sendAck = verifyPacket(); ackPacket(sendAck); if (sendAck == ACK) { /* CI_CMD_Intepreter will set up the packet response */ ret = CI_CMD_Intepreter(); updatePacket(); writePacket(); } } packetReset(); } return ret; }
void QueueController::sendAck(PIA &packet) { PIA ackPacket(settings->getLocalIP(), //IPaddr packet.getSourceAddress(), //IPaddr 0, //sequencenr packet.getSequenceNumber() + 1, //acknr true, //ACK flag false, //NTA flag "ack!" //payload ); sendQueue->push_back(ackPacket, true); }
bool Nordic::sendCommand(byte target, byte direction, byte time){ byte data[2] = {direction, time}; //Packet packet(seq_num, target, ID, COMMAND, data); Packet packet(conversation->getNextSeqNum(target), target, ID, COMMAND, data); conversation->update(packet); byte taddr[5] = {0, 0, 0, 0, target}; //Mirf.setTADDR(taddr); bool gotACK = false; for (int t=TIMEOUT; t>=0 && !gotACK; t--){ Mirf.send(packet.pack()); while (Mirf.isSending()); Serial.println("Sent packet"); //Serial.print("Of type: "); //Serial.println(packet.getType()); packet.print(DEC); byte ack[sizeof(Packet)]; for (int i = 5; i >= 0; i--){ if (Mirf.dataReady()){ break; } delay(10); } if (Mirf.dataReady()){ Mirf.getData(ack); Packet ackPacket(ack); if (ackPacket.getSequenceNumber() == packet.getSequenceNumber() && ackPacket.getSourceId() == packet.getTargetId() && ackPacket.getTargetId() == packet.getSourceId() && ackPacket.getType() == ACK){ Serial.println("Got ACK"); gotACK = true; } } } return gotACK; }
bool Nordic::passToken(byte target){ bool gotACK = false; if (hasToken()){ byte data[2] = {0,0}; Packet packet(conversation->getNextSeqNum(target), target, ID, TOKEN, data); conversation->update(packet); byte taddr[5] = {0,0,0,0,target}; for (int t=TIMEOUT; t >= 0 && !gotACK; t--){ Mirf.send(packet.pack()); while (Mirf.isSending()); Serial.println("Sent token"); packet.print(DEC); byte ack[sizeof(Packet)]; for (int i = 5; i >= 0; i--){ if (Mirf.dataReady()){ break; } delay(10); } if (Mirf.dataReady()){ Mirf.getData(ack); Packet ackPacket(ack); if (ackPacket.getSequenceNumber() == packet.getSequenceNumber() && ackPacket.getSourceId() == packet.getTargetId() && ackPacket.getTargetId() == packet.getSourceId() && ackPacket.getType() == ACK){ Serial.println("Got ACK"); gotACK = true; token = false; } } } } return gotACK; }
TcpConnection::TcpConnection(char* ip, int port) { struct sockaddr_in myaddr, remaddr; int fd, slen = sizeof(remaddr); char buf[BUFLEN]; /* message buffer */ int recvlen; /* # bytes in acknowledgement message */ srand((int) time(NULL)); bool flags[] = { false, false, false, false, false, false, false, true, false }; seqNo = rand() % 10000; TcpPacket synPacket(seqNo, rand(), flags, 1u, time(0), PACKET_SIZE, nullptr); /* Initialize Network */ Network network = Network(); /* create a socket */ if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) printf("socket created\n"); /* bind it to all local addresses and pick any port number */ memset((char *) &myaddr, 0, sizeof(myaddr)); myaddr.sin_family = AF_INET; myaddr.sin_addr.s_addr = htonl(INADDR_ANY); myaddr.sin_port = htons(0); if (bind(fd, (struct sockaddr *)&myaddr, sizeof(myaddr)) < 0) { perror("bind failed"); return; } /* now define remaddr, the address to whom we want to send messages */ /* For convenience, the host address is expressed as a numeric IP address */ /* that we will convert to a binary format via inet_aton */ memset((char *) &remaddr, 0, sizeof(remaddr)); remaddr.sin_family = AF_INET; remaddr.sin_port = htons(SERVICE_PORT); if (inet_pton(remaddr.sin_family, SERVER, &remaddr.sin_addr) == 0) { fprintf(stderr, "inet_pton() failed\n"); exit(1); } /* Set socket timeout */ struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 100000; if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (char *) &tv, sizeof(tv)) < 0) { perror("socket timeout set failed"); exit(1); } printf("Sending packet to %s port %d\n", SERVER, SERVICE_PORT); if (sendto(fd, synPacket.buf, PACKET_SIZE, 0, (struct sockaddr *)&remaddr, slen) == -1) { perror("sendto"); exit(1); } printf("syn packet sent\n"); /* now receive an acknowledgement from the server */ recvlen = recvfrom(fd, buf, BUFLEN, 0, (struct sockaddr *)&remaddr, &slen); if (recvlen >= 0) { /* check if ACK & SYN flag set in response*/ bool* Aflags = TcpPacket::getFlags(buf); if (*(Aflags + ACKBIT) && *(Aflags + SYNBIT)) { printf("Ack received\n"); char* seqno = TcpPacket::getBytes(buf, 0, SEQUENCE_SIZE); int AseqNo = atoi(seqno); char* ackno = TcpPacket::getBytes(buf, SEQUENCE_SIZE, ACK_SIZE); int ano = atoi(ackno); if (ano != seqNo + 1) { perror("seq error mismtach"); exit(1); } /* construct ack for the packet just received*/ bool flags[] = { false, false, false, false, true, false, false, false, false }; seqNo++; TcpPacket ackPacket(seqNo, AseqNo + 1, flags, 1u, time(0), PACKET_SIZE, nullptr); if (sendto(fd, ackPacket.buf, PACKET_SIZE, 0, (struct sockaddr *)&remaddr, slen) == -1) { perror("ackPacket"); exit(1); } printf("Three way handshake complete\n"); /* Free memory */ free(seqno); free(ackno); } else { perror("Ack not received"); exit(1); } free(Aflags); } closesocket(fd); }