void ModbusIP::task() {
    word len = ether.packetReceive();
    word pos = ether.packetLoop(len);

    if (pos) {
        int i = 0;
        while (i < 7) {
            _MBAP[i] = Ethernet::buffer[pos+i];
             i++;
        }

        _len = _MBAP[4] << 8 | _MBAP[5];
        _len--; // Do not count with last byte from MBAP
        if (_MBAP[2] !=0 | _MBAP[3] !=0) return;   //Not a MODBUSIP packet
        if (_len > MODBUSIP_MAXFRAME) return;      //Length is over MODBUSIP_MAXFRAME

        _frame = (byte*) malloc(_len);
        i = 0;
        while (i < _len){
            _frame[i] = Ethernet::buffer[pos+7+i];  //Forget MBAP and take just modbus pdu
            i++;
        }

        this->receivePDU(_frame);

        if (_reply != MB_REPLY_OFF) {
            //MBAP
            _MBAP[4] = _len >> 8;
            _MBAP[5] = _len | 0x00FF;

            BufferFiller bfill = ether.tcpOffset();
            bfill.emit_raw((const char *)_MBAP, 7);
            bfill.emit_raw((const char *)_frame, _len);
            ether.httpServerReply(bfill.position());
        }

        free(_frame);
        _len = 0;
    }
Ejemplo n.º 2
0
static word serverRequestTemperature(byte fd) {
	// filling state
	uint8_t data[16] = {0}; // 16 bytes block for encryption, which have to be send in hex
	data[0] = uint8_t(uint32_t(sharedState->currentTemperature)); // whole part 
	data[1] = uint8_t(uint32_t(sharedState->currentTemperature * 100) % 100); // fraction part
	*(uint32_t*)(data + (16 - sizeof(uint32_t))) = sharedState->lastNonce;

	sharedState->lastNonce += 1;
	
	// encrypting the state
	aes128_enc_single(encKey, data);

	printlnDebug("Sending encrypted state");
	BufferFiller bfill = ether.tcpOffset();
	bfill.emit_p(sendTemperatureHeader);
	bfill.emit_raw((char*)data, sizeof(data)); 
	temperatureSend = 1;
	sendEaseTime = millis() + SENDINGEASE;
	return bfill.position();
}