QByteArray MessageManager::dispatchHandler(QString incomingMessage, QTcpSocket *socket)
{
    QStringList message = incomingMessage.split('|');

    if(message.size() > 0){

        QString messageType = QString::fromLocal8Bit(message.at(0).toLocal8Bit()); //Extract the type

        //Call Handler for each type of message
        if(messageType == LOGIN_REQUEST){
            QString username = QString::fromLocal8Bit(message.at(1).toLocal8Bit());

            //Search for the username in the database of users
            QByteArray result = handleLogin(username).toLocal8Bit();
            return result;
        }
        else if(messageType == PATIENT_DATA_REQUEST){
            QByteArray result = handleDataRequest(socket).toLocal8Bit();
            return result;
        }
        else if(messageType == ADD_PATIENT_REQUEST){

        }
        else if(messageType == EDIT_PATIENT_REQUEST){

        }
        else if(messageType == DELETE_PATIENT_REQUEST){

        }
        else if(messageType == ADD_CONSULTATION_REQUEST){
            QByteArray result = handleAddConsultation(incomingMessage, socket).toLocal8Bit();
            return result;
        }
        else if(messageType == EDIT_CONSULTATION_REQUEST){
            QByteArray result = handleEditConsultation(incomingMessage).toLocal8Bit();
            return result;
        }
        else if(messageType == ADD_FOLLOWUP_REQUEST){
            QByteArray result = handleAddFollowUp(incomingMessage, socket).toLocal8Bit();
            return result;
        }
        else if(messageType == EDIT_FOLLOWUP_REQUEST){
            QByteArray result = handleEditFollowUp(incomingMessage, socket).toLocal8Bit();
            return result;
        }
    }
    return INVALID_MESSAGE_TYPE.toLocal8Bit();
}
void UDPServer::processDatagram(const QByteArray &datagram)
{
    QString datastring(datagram);
    switch (datastring.section(';',0,0).toInt())
    {
    case 1:
        // set the correct IP to send to
        otherNode->setAddress(datastring.section(';',1,1));
        qDebug() << "otherNode adress set to: " << datastring.section(';',1,1);

        // reply to SYN msg
        sendSYNACK();

        qDebug() << "processed Datagram case 1";
        break;
    case 3:
        // reply to ACK msg
        stopTimeOutTimer();
        sendAME();

        qDebug() << "processed Datagram case 3: Handshake complete";
        break;
    case 5:
        // intern reply to the 'alive' msg of the client
        if (!connected)
            setConnected(true);

        resetAliveTimer();

        qDebug() << "processed Datagram case 5";
        break;
    case 11:
        // incomming request for data from server
        handleDataRequest(datagram);

        qDebug() << "processed Datagram case 11";
        break;
    default:
        // Just break on everything else
        break;
    }
}
Esempio n. 3
0
int main(void) {
	SYS_Init(); // Init Atmel Lightweight Mesh stack

	SYS_TaskHandler(); // Call the system task handler once before we configure the radio
	NWK_SetAddr(eeprom_read_word((uint16_t*)0));
	NWK_SetPanId(0); // Default PAN ID will be 0, can be changed using the set PAN command
	PHY_SetChannel(APP_CHANNEL);
	//NWK_SetSecurityKey(APP_SECURITY_KEY);
	PHY_SetRxState(true);
	NWK_OpenEndpoint(APP_ENDPOINT, rfReceivePacket);
	PHY_SetTxPower(0);

	// Seed the pseudorandom number generator
	srand(eeprom_read_word((uint16_t*)0));

	// Read eeprom data
	eeprom_read_block(&deviceCalibration, (void*)8, sizeof(deviceCalibration));
	if (eeprom_read_byte((uint8_t*)26)) { // There is valid data in the network information struct
		eeprom_read_block(baseStationList, (void*)27, sizeof(struct baseStation));
		uint8_t ch = 17;
		while ((baseStationList[0].name[ch] == ' ') || (baseStationList[0].name[ch] == '\0') || (baseStationList[0].name[ch] == 0xFF)) {
			baseStationList[0].name[ch] = ' ';
			ch -= 1;
		}
		baseStationList[0].nameLen = ch+1;
		baseStationListLength += 1;

		for (int cnt = 0; cnt < BASESTATION_LIST_SIZE; cnt++) {
			baseStationList[cnt].name[16] = ' ';
			baseStationList[cnt].name[17] = ' ';
		}
		sendConnectionRequest(0, &deviceCalibration);
	}	

	initDataAck();
	initLCD(display_cmd_buffer, 64);
	initUI();

	TCCR0B |= (1<<CS01);
	TCCR3B |= (1<<CS32) | (1<<CS30);

	sei();
	startDataAck();

	while (1) {
		SYS_TaskHandler();

		if (receivedDataRequest) {
			handleDataRequest();
			receivedDataRequest = false;
		}

		if (receivedColdStart) {
			if (connectedBaseStation == -1)
				sendConnectionRequest(0, &deviceCalibration);
			else
				sendConnectionRequest(connectedBaseStation, &deviceCalibration);
			ui_baseStationDisconnected();
			receivedColdStart = false;
		}

		updateUI();

		if (sampleCount > 40000) {
			if (dataPacket.sampleCount != 0)
				removeSamples(&dataPacket); // If the last transmitted data has not been acked then first remove the old data.
			getData(&dataPacket); // Sample new data
			ui_updatePowerValues(&dataPacket); // Update display
			removeSamples(&dataPacket); // Get rid of these samples now
		}
		
		if (TCNT0 > 80) {
			serviceLCD();
			TCNT0 = 0;
		}

		TCCR3B &= ~((1<<CS32) | (1<<CS30));
		if (TCNT3 != 0) {
			for (uint8_t cnt = 0; cnt < DATA_REQ_BUFFER_CNT; cnt++) {
				if (dataReqBusy[cnt] && (retransmit_time[cnt] != 0)) {
					if (retransmit_time[cnt] <= TCNT3) {
						NWK_DataReq(&(nwkPacket[cnt]));
						retransmit_time[cnt] = 0;
					}
					else
						retransmit_time[cnt] -= TCNT3;
				}
			}
		}
		TCNT3 = 0;
		TCCR3B |= (1<<CS32) | (1<<CS30);
	}
}