int sendWorkerDetails(int sock, struct workerNode *ptr){
	//Send Worker Found
	if(sendInt(sock , 1) < 0){
		puts("Send Query result Failed");
		return -1;
	}

	puts("Worker present status sent");

	//Send the IP and Port
	if(send(sock, ptr->ip, sizeof(int)*4, 0) < 0){
		puts("Send IP Failed");
		return -1;
	}

	puts("IP sent");

	//Wait for ACK
	if(waitForAck(sock) < 0){
		puts("ACK not received");
		return -1;
	}

	//Send Port number
	if(sendInt(sock, ptr->port) < 0){
		puts("Send Port Failed");
		return -1;
	}

	puts("Port Sent");

	return 1;
}
Exemple #2
0
void QSocketSession::findByteOrder()
{
	float i = 3.1415;
	if (theIsMaster)
	{	sendChunk((uchar *)&i, 4);
		if (!waitForAck(1000, &theSameByteOrder) || !isOpen())
		{	qWarning("*** ERROR: Handshake failed: Didn't receive ack. Closing link.");
			close();
			return;
		}
	}
	else
	{	float remotei;
		if (!receiveChunk((uchar *)&remotei, 4, 1000) || !isOpen())
		{	qWarning("*** ERROR: Handshake failed: Didn't receive remote bit pattern. Closing link.");
			close();
			return;
		}
		float rremotei;
		uint &t = *((uint *)&rremotei);
		t = bswap_32(*((uint *)&remotei));

		if (remotei == i)
			theSameByteOrder = true;
		else if (rremotei == i)
			theSameByteOrder = false;
		else
			qWarning("*** CRITICAL: Two hosts have neither same nor opposite byte order. (%f or %f should be %f)", remotei, rremotei, i);
		ack(theSameByteOrder);
	}
}
Exemple #3
0
//--------------------------------------------------------------------------------------------------
// Send payload data via RF
//-------------------------------------------------------------------------------------------------
void tools_rfwrite (byte myNodeID, const void* ptr, uint8_t len) {
   if(tools_ack()){
      for (byte i = 0; i <= RETRY_LIMIT; ++i) {  // tx and wait for ack up to RETRY_LIMIT times
        rf12_sleep(-1);              // Wake up RF module
         while (!rf12_canSend())
         rf12_recvDone();
         rf12_sendStart(RF12_HDR_ACK, ptr, len); 
         rf12_sendWait(2);           // Wait for RF to finish sending while in standby mode
         byte acked = waitForAck(myNodeID);  // Wait for ACK
         rf12_sleep(0);              // Put RF module to sleep
         if (acked) { return; }      // Return if ACK received
     
         Sleepy::loseSomeTime(RETRY_PERIOD * 1000);     // If no ack received wait and try again
      }
   }
   else {
      rf12_sleep(-1);              // Wake up RF module
      while (!rf12_canSend())
         rf12_recvDone();
      rf12_sendStart(0, ptr, len); 
      rf12_sendWait(2);           // Wait for RF to finish sending while in standby mode
      rf12_sleep(0);              // Put RF module to sleep
      return;
   }
}
Exemple #4
0
void QSocketSession::handshake()
{
	if (MESSAGES) qDebug("Handshaking... (isOpen()=%d)", isOpen());
	ack();
	if (!waitForAck(1000) || !isOpen())
	{	qWarning("*** ERROR: Handshake failed: Didn't receive ack. Closing link.");
		close();
		return;
	}
	if (MESSAGES) qDebug("Got ack!");
	for (bool know = false; !know;)
	{	uchar mi = rand() & 255;
		sendByte(mi);
		uchar i;
		if (!receiveChunk((uchar *)&i, 1, 1000) || !isOpen())
		{	qWarning("*** ERROR: Handshake failed: Didn't receive random. Closing link.");
			close();
			return;
		}
		if (mi != i)
		{	theIsMaster = mi > i;
			know = true;
		}
		else
		{	if (MESSAGES) qDebug("Doh! Number clash (%d = %d)", mi, i);
		}
	}
	findByteOrder();
}
Exemple #5
0
/******************************************************************************
* @fn         mrfiLinkSend
*
* @brief      Send data on the RX link.
*
* @param      pBuf - buffer to be transmitted
*
* @param      len -  number of bytes to be transmitted
*
* @return     Return code indicates success or failure of transmit:
*                  MRFI_TX_RESULT_SUCCESS - transmit succeeded
*                  MRFI_TX_RESULT_FAILED  - transmit failed because CCA or ACK failed
*/
uint8 mrfiLinkSend(uint8 *pBuf, uint8 len, uint8 nRetrans)
{
    uint8 v,i,status;

    v= halIntLock();

    MRFI_SET_PAYLOAD_LEN(&pkt, len+2);
    memcpy(MRFI_P_DST_ADDR(&pkt), dest_addr, 4);
    memcpy(MRFI_P_SRC_ADDR(&pkt), src_addr, 4);

    MRFI_P_PAYLOAD(&pkt)[0]= seqSend;
    MRFI_P_PAYLOAD(&pkt)[1]= MRFI_LINK_DATA;

    memcpy(MRFI_P_PAYLOAD(&pkt)+2, pBuf, len);
    halIntUnlock(v);

    for (i=0;i<nRetrans;i++) {
        status= MRFI_Transmit(&pkt, MRFI_TX_TYPE_CCA);
        if (status==MRFI_TX_RESULT_SUCCESS) {
            if (waitForAck(20)) {
                seqSend++;
                break;
            } else {
                status= MRFI_TX_RESULT_FAILED;
                // wait random time if sending is not successful
                // (20-40 milliseconds)
                halMcuWaitUs( (20000/255*MRFI_RandomByte()) + 20000 );
            }
        }
    }

    return status;

}
Exemple #6
0
void setFieldColor(uint8 color)
{
    PORTC = 0x0B;
    PORTB = color;
    PORTA = 0x01;
    PORTA = 0x00;

    waitForAck();
}
void GPS::sendCommand(const char* str, const bool wait, const uint32_t timeout) {
#ifdef SIMULATE
    // Do nothing if in simulation mode
#else
  //Serial.print("GPS(");
  //Serial.print(str);
  //Serial.println(")");
    gpsSwSerial->println(str);
    if (wait) waitForAck(timeout);
#endif
}
Exemple #8
0
//fcn to initialize the frontend display
void startGame(void) {
    //temp variables
    char buffer[BUFFSIZE];
    
    //construct message
    sprintf(buffer, "start");
    
    //send message
    serDevice.sendData(buffer);
    
    //wait for acknowledgement
    waitForAck();
}
Exemple #9
0
void setDoubleBuffer(bool doubleBuffer)
{
    PORTC = 0x09;
    if (doubleBuffer)
        PORTB = 0x01;
    else
        PORTB = 0x00;

    PORTA = 0x01;
    PORTA = 0x00;

    waitForAck();
}
Exemple #10
0
void QSocketSession::handshake(bool opposite)
{

	if (MESSAGES) qDebug("Handshaking... (isOpen()=%d)", isOpen());
	ack();
	if (!waitForAck(1000) || !isOpen())
	{	qWarning("*** ERROR: Handshake failed: Didn't receive ack. Closing link.");
		close();
		return;
	}
	if (MESSAGES) qDebug("Got ack!");
	theIsMaster = opposite;
	findByteOrder();
}
void SegmentDisplayProtocol::writeByte(uint8_t data)
{
    for (int bit = 0; bit < 8; bit++)
    {
        _pinClock->setLow();
        _pinData->set(data & 0x01);
        _pinClock->setHigh();
        bitDelay();

        for (int cnt = 0; !_pinClock->get() && cnt < 20; cnt++)
            bitDelay();

        data >>= 1;
    }

    waitForAck();
}
Exemple #12
0
void flipBuffer(uint8* buf)
{
    PORTC = 0x0A;
    if (*buf)
    {
        *buf = 0;
        PORTB = 0x01;
    }
    else
    {
        *buf = 1;
        PORTB = 0x00;
    }
    PORTA = 0x01;
    PORTA = 0x00;

    waitForAck();
}
bool RControlStationComm::setAutopilotActive(int car, bool active, int timeoutMs)
{
    if (!isTcpConnected()) {
        qWarning() << "libRControlStation: not connected";
        return false;
    }

    QString str;
    QXmlStreamWriter stream(&str);
    stream.setAutoFormatting(true);

    QString cmd = "setAutopilotActive";
    stream.writeStartDocument();
    stream.writeStartElement("message");
    stream.writeStartElement(cmd);
    stream.writeTextElement("id", QString::number(car));
    stream.writeTextElement("enabled", QString::number(active));
    stream.writeEndDocument();

    sendData(str.toLocal8Bit());
    return waitForAck(cmd, timeoutMs);
}
bool RControlStationComm::addRoutePoints(int car, ROUTE_POINT *route, int len, bool replace,
                                         bool mapOnly, int mapRoute, int timeoutMs)
{
    if (!isTcpConnected()) {
        qWarning() << "libRControlStation: not connected";
        return false;
    }

    QString str;
    QXmlStreamWriter stream(&str);
    stream.setAutoFormatting(true);

    QString cmd = "addRoutePoint";
    if (replace) {
        cmd = "replaceRoute";
    }

    stream.writeStartDocument();
    stream.writeStartElement("message");
    stream.writeStartElement(cmd);
    stream.writeTextElement("id", QString::number(car));
    stream.writeTextElement("mapOnly", QString::number(mapOnly));
    stream.writeTextElement("mapRoute", QString::number(mapRoute));

    for (int i = 0;i < len;i++) {
        stream.writeStartElement("point");
        stream.writeTextElement("px", QString::number(route[i].px));
        stream.writeTextElement("py", QString::number(route[i].py));
        stream.writeTextElement("speed", QString::number(route[i].speed));
        stream.writeTextElement("time", QString::number(route[i].time));
        stream.writeEndElement();
    }

    stream.writeEndDocument();

    sendData(str.toLocal8Bit());
    return waitForAck(cmd, timeoutMs);
}
Exemple #15
0
//fcn to print to console
void print(const char *format, ...) {
    //temp variables
    char buffer[BUFFSIZE];
    char temp[BUFFSIZE-6];
    
    //construct message part 1
    sprintf(buffer, "print-");
    
    //construct message part 2
    va_list arguments;
    va_start(arguments, format);
    vsnprintf(temp, BUFFSIZE-7, format, arguments);
    va_end(arguments);
    
    //concatenate parts
    strcat(buffer, temp);
    
    //send message
    serDevice.sendData(buffer);
    
    //wait for acknowledgement
    waitForAck();  
}
bool RControlStationComm::setEnuRef(int car, double *llh, int timeoutMs)
{
    if (!isTcpConnected()) {
        qWarning() << "libRControlStation: not connected";
        return false;
    }

    QString str;
    QXmlStreamWriter stream(&str);
    stream.setAutoFormatting(true);

    stream.writeStartDocument();
    stream.writeStartElement("message");
    stream.writeStartElement("setEnuRef");
    stream.writeTextElement("id", QString::number(car));
    stream.writeTextElement("lat", QString::number(llh[0], 'g', 10));
    stream.writeTextElement("lon", QString::number(llh[1], 'g', 10));
    stream.writeTextElement("height", QString::number(llh[2]));
    stream.writeEndDocument();

    sendData(str.toLocal8Bit());
    return waitForAck("setEnuRef", timeoutMs);
}
Exemple #17
0
//-----------------------------------------------------------------------------
// See cul.h for a description of this function.
//-----------------------------------------------------------------------------
BYTE sppSend(SPP_STRUCT* pPacketPointer){
   BYTE res = TRUE;

   // Checking that length is not too long
   if (pPacketPointer->payloadLength > SPP_MAX_PAYLOAD_LENGTH)
   {
      res = TOO_LONG;
      sppTxStatus = TX_IDLE;
   }

   // Flipping the sequence bit, writing  total packet length and address if the transfer is not a retransmission.
   // If it is a retransmission, the fields are correct
   if(!(pPacketPointer->flags & RETRANSMISSION))
   {
      pPacketPointer->flags ^= SEQUENCE_BIT;
      pPacketPointer->payloadLength += SPP_HEADER_AND_FOOTER_LENGTH;
      pPacketPointer->srcAddress = myAddress;
   }


   // Setting up the DMA
   DMA_ABORT_CHANNEL(dmaNumberTx);
   SET_DMA_SOURCE(dmaTx,pPacketPointer);


   // Proceed if the packet length is OK.
   if (res == TRUE)
   {
      // Clearing RF interrupt flags and enabling RF interrupts.
      RFIF &= ~IRQ_DONE;
      RFIM &= ~IRQ_SFD;
      INT_SETFLAG(INUM_RF, INT_CLR);
#ifdef CCA_ENABLE
      if(!CCA)
      {
         SRX();
         // Turning on Rx and waiting to make the RSSI value become valid.
         halWait(1);
      }
      if(CCA)
#endif
      {  // Setting up radio
         DMA_ABORT_CHANNEL(dmaNumberRx);
         SIDLE();
         RFTXRXIF = 0;
         INT_GLOBAL_ENABLE(FALSE);
         DMA_ARM_CHANNEL(dmaNumberTx);
         STX();
         INT_GLOBAL_ENABLE(TRUE);
         sppTxStatus = TX_IN_PROGRESS;

         if(pPacketPointer->flags & DO_ACK)
         {
            pAckData = pPacketPointer;
            waitForAck();
         }
         else
         {
            pAckData = NULL;
         }
         RFIM |= IRQ_DONE;
      }
#ifdef CCA_ENABLE
      // The "air" is busy
      else
      {
         res = CHANNEL_BUSY;
         RFIM &= ~IRQ_DONE;
         // De-flipping the sequence bit.
         if(!(pPacketPointer->flags & RETRANSMISSION))
         {
            pPacketPointer->flags ^= SEQUENCE_BIT;
         }
      }
#endif
   }
   return res;
} // ends sppSend
Exemple #18
0
void drawData(void)
{
    PORTA = 0b00000010;           //initialize draw
    PORTA = 0b00000000;           //draw command received, so turn off draw bit
    waitForAck();
}
Exemple #19
0
int sendFileToServer(int sock, char path[100], char fileName[100]){

	// Entire file path
	char wholePath[200];

	strcpy(wholePath, path);
	strcat(wholePath, "/");
	strcat(wholePath, fileName);

	printf("Whole File Path = %s\n",wholePath);

	//Sending 1 KB at a time
	char fileContents[1024];

	// Find out file size
	int size;
	struct stat s;

	int status = stat(wholePath, &s);

	if(status < 0){
		// stat failed
		puts("No Such File or Directory");

		// Send incorrect file details stopper as status -1
		if(sendInt(sock, -1) < 0){
			puts("Incorrect file details sent as status -1 :  Failed to send");
		}
		return -1;
	}else{
		if(sendInt(sock, 1) < 0){
			puts("Incorrect file details sent as status 1 :  Failed to send");
		}
	}

	size = s.st_size;

	printf("File Size = %d\n",size);

	// Open file
	FILE *fp;

	fp = fopen(wholePath, "r");

	if(fp == NULL){
		printf("%s : Error opening file\n",fileName);
		return -1;
	}

	//Send FileName
	if(send(sock , &fileName[0] , sizeof(char)*100 , 0) < 0){
		puts("Send File Name Failed");
		return -1;
	}

	printf("File Name Sent = %s\n",fileName);

	if(waitForAck(sock) < 0){
		return -1;
	}

	// Wait for server response for file already exists
	int fileExists;

	if(readInt(sock, &fileExists) < 0){
		puts("Failed to read file exists from server");
		return -1;
	}

	if(fileExists < 0){
		// File already exists, do not send, but do consider this to be an error
		puts("File already Indexed | Will not be indexed again");
		return 1;
	}

	//Send File size
	if(sendInt(sock, size) < 0){
		puts("Send File Size Failed");
		return -1;
	}

	puts("File Size Sent");

	// Read file
	while(fgets(fileContents, 1024, (FILE*)fp) != NULL){
		//Sending maximum 1 KB of the file (Can be less depending of bytes in the line)
		if(send(sock , &fileContents , sizeof(char)*1024 , 0) < 0){
			puts("Send Failed");
			return -1;
		}

		// Wait for Ack
		if(waitForAck(sock) < 0){
			puts("Failed receiving Ack when sending the file");
			return -1;
		}
	}

	puts("File Sent");

	if(waitForAck(sock) < 0){
		return -1;
	}

	// Close file
	fclose(fp);

	return 1;
}