SerialPortReader::SerialPortReader(QSerialPort* serial, QObject* parent) : QObject(parent), _serial(serial) { // The magic call that makes QSerialPort outputs from a serial connected with Arduino _serial->setDataTerminalReady(true); QObject::connect(_serial, SIGNAL(readyRead()), SLOT(_read_callback())); QObject::connect(_serial, SIGNAL(error(QSerialPort::SerialPortError)), SLOT(_error_callback(QSerialPort::SerialPortError))); }
void sge::opencl::context::object::error_callback( char const *const _error_info_string, void const *const _bindata, size_t const _size_of_bindata, void *const _user_data ) { fcppt::optional::maybe_void( fcppt::cast::from_void_ptr< sge::opencl::context::object * >( _user_data )->error_callback_, [ _error_info_string, _bindata, _size_of_bindata ]( sge::opencl::context::error_callback const &_error_callback ) { _error_callback( sge::opencl::error_information_string( _error_info_string ), sge::opencl::binary_error_data( fcppt::cast::from_void_ptr< unsigned char const * >( _bindata ), fcppt::cast::from_void_ptr< unsigned char const * >( _bindata ) + _size_of_bindata ) ); } ); }
void ArduinoOTAClass::_runUpdate() { if (!Update.begin(_size, _cmd)) { #ifdef OTA_DEBUG OTA_DEBUG.println("Update Begin Error"); #endif if (_error_callback) { _error_callback(OTA_BEGIN_ERROR); } _udp_ota->listen(*IP_ADDR_ANY, _port); _state = OTA_IDLE; return; } Update.setMD5(_md5.c_str()); WiFiUDP::stopAll(); WiFiClient::stopAll(); if (_start_callback) { _start_callback(); } if (_progress_callback) { _progress_callback(0, _size); } WiFiClient client; if (!client.connect(_ota_ip, _ota_port)) { #ifdef OTA_DEBUG OTA_DEBUG.printf("Connect Failed\n"); #endif _udp_ota->listen(*IP_ADDR_ANY, _port); if (_error_callback) { _error_callback(OTA_CONNECT_ERROR); } _state = OTA_IDLE; } uint32_t written, total = 0; while (!Update.isFinished() && client.connected()) { int waited = 1000; while (!client.available() && waited--) delay(1); if (!waited){ #ifdef OTA_DEBUG OTA_DEBUG.printf("Receive Failed\n"); #endif _udp_ota->listen(*IP_ADDR_ANY, _port); if (_error_callback) { _error_callback(OTA_RECEIVE_ERROR); } _state = OTA_IDLE; } written = Update.write(client); if (written > 0) { client.print(written, DEC); total += written; if(_progress_callback) { _progress_callback(total, _size); } } } if (Update.end()) { client.print("OK"); client.stop(); delay(10); #ifdef OTA_DEBUG OTA_DEBUG.printf("Update Success\nRebooting...\n"); #endif if (_end_callback) { _end_callback(); } ESP.restart(); } else { _udp_ota->listen(*IP_ADDR_ANY, _port); if (_error_callback) { _error_callback(OTA_END_ERROR); } Update.printError(client); #ifdef OTA_DEBUG Update.printError(OTA_DEBUG); #endif _state = OTA_IDLE; } }
void ArduinoOTAClass::_onRx(){ if(!_udp_ota->next()) return; ip_addr_t ota_ip; if (_state == OTA_IDLE) { int cmd = parseInt(); if (cmd != U_FLASH && cmd != U_SPIFFS) return; _ota_ip = _udp_ota->getRemoteAddress(); _cmd = cmd; _ota_port = parseInt(); _size = parseInt(); _udp_ota->read(); _md5 = readStringUntil('\n'); _md5.trim(); if(_md5.length() != 32) return; ota_ip.addr = (uint32_t)_ota_ip; if (_password.length()){ MD5Builder nonce_md5; nonce_md5.begin(); nonce_md5.add(String(micros())); nonce_md5.calculate(); _nonce = nonce_md5.toString(); char auth_req[38]; sprintf(auth_req, "AUTH %s", _nonce.c_str()); _udp_ota->append((const char *)auth_req, strlen(auth_req)); _udp_ota->send(&ota_ip, _udp_ota->getRemotePort()); _state = OTA_WAITAUTH; return; } else { _udp_ota->append("OK", 2); _udp_ota->send(&ota_ip, _udp_ota->getRemotePort()); _state = OTA_RUNUPDATE; } } else if (_state == OTA_WAITAUTH) { int cmd = parseInt(); if (cmd != U_AUTH) { _state = OTA_IDLE; return; } _udp_ota->read(); String cnonce = readStringUntil(' '); String response = readStringUntil('\n'); if (cnonce.length() != 32 || response.length() != 32) { _state = OTA_IDLE; return; } MD5Builder _passmd5; _passmd5.begin(); _passmd5.add(_password); _passmd5.calculate(); String passmd5 = _passmd5.toString(); String challenge = passmd5 + ":" + String(_nonce) + ":" + cnonce; MD5Builder _challengemd5; _challengemd5.begin(); _challengemd5.add(challenge); _challengemd5.calculate(); String result = _challengemd5.toString(); ota_ip.addr = (uint32_t)_ota_ip; if(result.equals(response)){ _udp_ota->append("OK", 2); _udp_ota->send(&ota_ip, _udp_ota->getRemotePort()); _state = OTA_RUNUPDATE; } else { _udp_ota->append("Authentication Failed", 21); _udp_ota->send(&ota_ip, _udp_ota->getRemotePort()); if (_error_callback) _error_callback(OTA_AUTH_ERROR); _state = OTA_IDLE; } } while(_udp_ota->next()) _udp_ota->flush(); }