bool Esp8266::reset() { unsigned long timeout = 7000; unsigned long t_start = 0; int buf[10]; char index=0; clearBuf(); write("AT+RST"); t_start = millis(); while ((millis()-t_start) < timeout) { while (available()>0) { buf[index] = read(); if (buf[index]=='y' && buf[(index+9)%10]=='d' && buf[(index+8)%10]=='a' && buf[(index+7)%10]=='e' && buf[(index+6)%10]=='r') { return true; } index++; if (index==10) index = 0; } } if (this->isDebug) { debugPrintln("rest esp8266 timeout"); } return false; }
unsigned char SONAR::recvDat(unsigned char desiredSize) { debug(); clearBuf(); init(); // 201209 setRX(); unsigned char datSize=0; #if defined(BOARD_maple) || defined(BOARD_maple_native) || defined(BOARD_maple_mini) for(int j=0;datSize<desiredSize && j<15;++j) { delay(1); unsigned char ibyte=Serial.read(); if(datSize==0 && ibyte!=0x55) continue; if(datSize==1 && ibyte!=0xaa) continue; _recvBuf[datSize++]=ibyte; } #else//for arduino for(int j=0;datSize<desiredSize && j<5000;++j) { unsigned char ibyte=Serial.read(); if(ibyte!=0xff) { _recvBuf[datSize++]=ibyte; } } #endif //SonarPrint.println(Serial.read(), HEX); setTX(); return datSize; }
ServerOutputStream& ServerOutputStream::setPrefix(const QString& prefix) { flushBuf(); m_prefix = prefix; m_prefixBytes = prefix.toUtf8(); clearBuf(); return *this; }
void ServerOutputStream::flushBuf() { if (m_bufLen == m_prefixBytes.size()) return; DefaultOutputStream::writeData(m_buf.data(), m_bufLen); // DefaultOutputStream::flush(); clearBuf(); }
bool Esp8266::setMux(int flag) { String str; clearBuf(); write("AT+CIPMUX="+String(flag)); delay(100); str = readData(); if (str.indexOf("OK")>0 || str.indexOf("link is builded")>0) return true; else return false; }
bool Esp8266::check() { bool isOK=false; clearBuf(); write("AT"); delay(200); isOK = this->serial->findUntil("AT", "\r\n"); if (true == isOK) { return true; } else { return false; } }
int main(void) { int inBuf[MAX_LEN]; int outBuf[MAX_LEN]; int len; clearBuf(inBuf, MAX_LEN); clearBuf(outBuf, MAX_LEN); while ((len = getLine(inBuf, MAX_LEN)) != 0) { printf("entered: %d char[s]\n", len); reverse(inBuf, outBuf, len); printf("reverse: "); print(outBuf, len); printf("\n"); clearBuf(inBuf, MAX_LEN); clearBuf(outBuf, MAX_LEN); } return 0; }
bool Esp8266::connect(String serverIP, String serverPort, bool udp) { unsigned long timeout = 5000; unsigned long t_start = 0; unsigned char buf[10]; unsigned char index=0; String proto = udp ? "UDP" : "TCP"; clearBuf(); if (!this->multiFlag) { write("AT+CIPSTART=\"" + proto + "\",\"" + serverIP + "\"," + serverPort); t_start = millis(); while((millis())-t_start < timeout) { while(available()) { buf[index] = read(); if (buf[index]=='T' && buf[(index+9)%10]=='C' && buf[(index+8)%10]=='E' && buf[(index+7)%10]=='N' && buf[(index+6)%10]=='N' && buf[(index+5)%10]=='O' && buf[(index+4)%10]=='C') { return true; } index++; if (index==10) index = 0; } } if (this->isDebug) { debugPrintln("connectTCPServer timeout"); } return false; } else { write("AT+CIPSTART="+ String(this->connectID) + ",\"TCP\",\"" + serverIP + "\"," + serverPort); t_start = millis(); while((millis())-t_start < timeout) { while(available()) { buf[index] = read(); if (buf[index]=='T' && buf[(index+9)%10]=='C' && buf[(index+8)%10]=='E' && buf[(index+7)%10]=='N' && buf[(index+6)%10]=='N' && buf[(index+5)%10]=='O' && buf[(index+4)%10]=='C') { this->connectID++; return true; } index++; if (index==10) index = 0; } } if (this->isDebug) { debugPrintln("connectTCPServer timeout"); } return false; } }
char Esp8266::checkMode() { clearBuf(); write("AT+CWMODE?"); delay(200); String str = readData(); // Serial.println(str); if (str.indexOf('1') > 0 ) return '1'; else if (str.indexOf('2') > 0) return '2'; else if (str.indexOf('3') > 0) return '3'; else return '0'; }
static void encodePrivateKeyHeader(const CssmData &inBlob, CFDataRef certificate, FVPrivateKeyHeader &outHeader) { CssmClient::CL cl(gGuidAppleX509CL); const CssmData cert(const_cast<UInt8 *>(CFDataGetBytePtr(certificate)), CFDataGetLength(certificate)); CSSM_KEY_PTR key; if (CSSM_RETURN rv = CSSM_CL_CertGetKeyInfo(cl->handle(), &cert, &key)) CssmError::throwMe(rv); Security::CssmClient::CSP fCSP(gGuidAppleCSP); // Set it up so the cl is used to free key and key->KeyData // CssmAutoData _keyData(cl.allocator()); // _keyData.set(CssmData::overlay(key->KeyData)); CssmAutoData _key(cl.allocator()); _key.set(reinterpret_cast<uint8 *>(key), sizeof(*key)); CssmClient::Key cKey(fCSP, *key); /* Given a CSSM_KEY_PTR in any format, obtain the SHA-1 hash of the * associated key blob. * Key is specified in CSSM_CSP_CreatePassThroughContext. * Hash is allocated by the CSP, in the App's memory, and returned * in *outData. */ CssmClient::PassThrough passThrough(fCSP); passThrough.key(key); void *outData; passThrough(CSSM_APPLECSP_KEYDIGEST, NULL, &outData); CssmData *cssmData = reinterpret_cast<CssmData *>(outData); assert(cssmData->Length <= sizeof(outHeader.publicKeyHash)); outHeader.publicKeyHashSize = (uint32_t)cssmData->Length; memcpy(outHeader.publicKeyHash, cssmData->Data, cssmData->Length); fCSP.allocator().free(cssmData->Data); fCSP.allocator().free(cssmData); /* Now encrypt the blob with the public key. */ CssmClient::Encrypt encrypt(fCSP, key->KeyHeader.AlgorithmId); encrypt.key(cKey); CssmData clearBuf(outHeader.encryptedBlob, sizeof(outHeader.encryptedBlob)); CssmAutoData remData(fCSP.allocator()); encrypt.padding(CSSM_PADDING_PKCS1); outHeader.encryptedBlobSize = (uint32_t)encrypt.encrypt(inBlob, clearBuf, remData.get()); if (outHeader.encryptedBlobSize > sizeof(outHeader.encryptedBlob)) secinfo("FDERecovery", "encodePrivateKeyHeader: encrypted blob too big: %d", outHeader.encryptedBlobSize); }
bool Esp8266::setMode(char mode) { clearBuf(); write("AT+CWMODE="+String(mode)); delay(200); String str = readData(); if (str.indexOf("no change") > 0) return true; else { if (reset()) { this->wifiMode = mode; return true; } else { return false; } } }
bool Esp8266::connectAP(String ssid, String password) { unsigned long timeout = 20000; unsigned long t_start = 0; int buf[10]; char index=0; if (checkMode()!=WIFI_MODE_AP){ this->wifiMode = WIFI_MODE_STATION; } else { if (setMode(WIFI_MODE_STATION)) this->wifiMode = WIFI_MODE_STATION; else { if (this->isDebug) { debugPrintln("set mode to station false!"); } return false; } } clearBuf(); this->serial->println("AT+CWJAP=\""+ssid+"\",\""+password+"\""); t_start = millis(); while ((millis()-t_start) < timeout) { while (available()>0) { buf[index] = read(); if (buf[index]=='K' && buf[(index+9)%10]=='O') { return true; } if (buf[index]=='L' && buf[(index+9)%10]=='I' && buf[(index+8)%10]=='A' && buf[(index+7)%10]=='F') { return false; } index++; if (index==10) index = 0; } } if (this->isDebug) { debugPrintln("connect AP timeout"); } return false; }
CFDataRef decodePrivateKeyHeader(SecKeychainRef keychain, const FVPrivateKeyHeader &inHeader) { // kSecKeyLabel is defined in libsecurity_keychain/lib/SecKey.h SecKeychainAttribute attrs[] = { { 6 /* kSecKeyLabel */, inHeader.publicKeyHashSize, const_cast<uint8 *>(inHeader.publicKeyHash) } }; SecKeychainAttributeList attrList = { sizeof(attrs) / sizeof(SecKeychainAttribute), attrs }; CSSM_CSP_HANDLE cspHandle = 0; const CSSM_KEY *cssmKey = NULL; const CSSM_ACCESS_CREDENTIALS *accessCred = NULL; CSSM_CC_HANDLE cc = 0; SecKeychainSearchRef _searchRef; throwIfError(SecKeychainSearchCreateFromAttributes(keychain, (SecItemClass) CSSM_DL_DB_RECORD_PRIVATE_KEY, &attrList, &_searchRef)); CFRef<SecKeychainSearchRef> searchRef(_searchRef); SecKeychainItemRef _item; if (SecKeychainSearchCopyNext(searchRef, &_item) != 0) { return NULL; // XXX possibly should throw here? } CFRef<SecKeyRef> keyItem(reinterpret_cast<SecKeyRef>(_item)); throwIfError(SecKeyGetCSPHandle(keyItem, &cspHandle)); throwIfError(SecKeyGetCSSMKey(keyItem, &cssmKey)); throwIfError(SecKeyGetCredentials(keyItem, CSSM_ACL_AUTHORIZATION_DECRYPT, kSecCredentialTypeDefault, &accessCred)); throwIfError(CSSM_CSP_CreateAsymmetricContext(cspHandle, cssmKey->KeyHeader.AlgorithmId, accessCred, cssmKey, CSSM_PADDING_PKCS1, &cc)); CFDataRef result; try { CssmMemoryFunctions memFuncs; throwIfError(CSSM_GetAPIMemoryFunctions(cspHandle, &memFuncs)); CssmMemoryFunctionsAllocator allocator(memFuncs); const CssmData cipherBuf(const_cast<uint8 *>(inHeader.encryptedBlob), inHeader.encryptedBlobSize); CssmAutoData clearBuf(allocator); CssmAutoData remData(allocator); size_t bytesDecrypted; CSSM_RETURN crx = CSSM_DecryptData(cc, &cipherBuf, 1, &clearBuf.get(), 1, &bytesDecrypted, &remData.get()); secinfo("FDERecovery", "decodePrivateKeyHeader: CSSM_DecryptData result: %d", crx); throwIfError(crx); // throwIfError(CSSM_DecryptData(cc, &cipherBuf, 1, &clearBuf.get(), 1, &bytesDecrypted, &remData.get())); clearBuf.length(bytesDecrypted); // rawKey.copy(clearBuf.get()); result = CFDataCreate(kCFAllocatorDefault, (const UInt8 *)clearBuf.get().data(), clearBuf.get().length()); // result = parseKeyBlob(clearBuf.get()); } catch(...) { CSSM_DeleteContext(cc); throw; } throwIfError(CSSM_DeleteContext(cc)); return result; }