bool SfsEntry::setLocalCsum(const u_int8_t *cs) { bool c1, c2, c3, c4; if (cs != 0) { int result = memcmp(localCsum_, cs, ANOUBIS_CS_LEN); c1 = (result != 0); memcpy(localCsum_, cs, ANOUBIS_CS_LEN); haveLocalCsum_ = true; } else { /* Reset local checksum * Local checksum state has changed, if a checksum was assigned */ c1 = haveLocalCsum_; haveLocalCsum_ = false; } c2 = validateChecksum(SFSENTRY_CHECKSUM); c3 = validateChecksum(SFSENTRY_SIGNATURE); c4 = validateChecksum(SFSENTRY_UPGRADE); if (c1 || c2 || c3 || c4) { sendRowChangeEvent(); return (true); } else return (false); }
// M | plant | 2 byte moisture | checksum void handleMoistureReading() { int out, bytesRead; char buf[5]; buf[0] = 'M'; bytesRead = read(serial[activePort], (buf+1), 4); if (bytesRead == 4 && validateChecksum(buf, 5)) { if (awaitingMoistureResponse && buf[1] == forPlant) { char outString[2] = {buf[2],buf[3]}; out = open(FIFO_OUT, O_NONBLOCK | O_WRONLY); write(out, outString, 2); close(out); //printf("%d\n", (unsigned char)buf[2] + ((unsigned char)buf[3] << 8)); g_moisture = (unsigned char)buf[2] + ((unsigned char)buf[3] << 8); awaitingMoistureResponse = FALSE; } printf("Received moisture reading for plant %d of %d.\n", buf[1], (unsigned char)buf[2] + ((unsigned char)buf[3] << 8)); writeMoistureToFile(buf[1], (unsigned char)buf[2] + ((unsigned char)buf[3] << 8)); } else { handleError('R', 12); } }
// Validate a MAP packet. // // If the require_checksum argument is true, then the packet must contain a (valid) checksum // in the outermost encapsulation to be considered valid. // If the remove_checksums argument is true, then any checksums present will be removed // once validated. (Note that the process aborts after encountering the first packet error.) bool MAP::MAPPacket::validate(HeaderOffset_t headerOffset, bool require_checksum, bool remove_checksums){ // Make sure packet includes at least an initial header // Make sure packet includes an outermost checksum, if requested. Data_t* header = get_header(headerOffset); if( header == NULL || ( require_checksum && !get_checksumPresent(*header) ) ) return false; DEBUGprint_MAP("MPPval: cap=%d, size=%d\n", get_capacity(), get_size()); // Validate (possibly nested) MAP header structure. // This requires a pass through the entire header structure. // Some processing time could perhaps be saved by using the same // pass to feed all the headers through the checksum engine, // especially if the packet data is stored in something slower // than RAM. Data_t* data_ptr = get_data(header); if(data_ptr == NULL) return false; // Stop point Data_t *stop_ptr = back(); // Stop at actual data (no more MAP headers). while(header != NULL){ // If header indicates a checksum, validate it. if(get_checksumPresent(*header)){ DEBUGprint_MAP("MPPval: val crc\n"); // Validate the checksum if(! validateChecksum(header, stop_ptr)) return false; // Encapsulated data is now one checksum back. stop_ptr -= MAP::ChecksumLength; if(remove_checksums){ *header = MAP::set_checksumPresent(*header, false); DEBUGprint_MAP("MPPval: rem crc\n"); } } // Next header header = get_next_header(header); } // If requested, cut off all checksums. if(remove_checksums){ // Set the packet size such that the checksums (at the end) are all removed. set_size(stop_ptr - front()); // Try to eliminate excess capacity //set_capacity(stop_ptr - front()); } // Checksums (if any) were all valid. return true; }
bool SfsEntry::setChecksum(ChecksumType type, const u_int8_t *cs, size_t size) { copyChecksum(type, cs, size); if (validateChecksum(type)) { sendRowChangeEvent(); return (true); } else return (false); }
void handleConfigurationAck() { int out, bytesRead; char buf[2]; buf[0] = 'C'; bytesRead = read(serial[activePort], &(buf[1]), 1); if (bytesRead == 1 && validateChecksum(buf, 2) && awaitingConfigurationAck) { printf("Received configuration ack.\n"); out = open(FIFO_OUT, O_NONBLOCK | O_WRONLY); write(out, buf, 2); close(out); awaitingConfigurationAck = FALSE; } else { handleError('R', 14); } }
void handleDryLevelAck() { int out, bytesRead; char buf[2]; buf[0] = 'D'; bytesRead = read(serial[activePort], (buf+1), 1); if (bytesRead == 1 && validateChecksum(buf, 2) && awaitingDryLevelAck) { printf("Received dry level ack.\n"); out = open(FIFO_OUT, O_NONBLOCK | O_WRONLY); write(out, buf, 2); close(out); awaitingDryLevelAck = FALSE; } else { handleError('R', 13); } }
//need to checksum void decodeMCUInput(char buf) { char buffer[4] = {'E', 'Z', '3', '0'}; int bytesRead = 0; switch (buf) { case 'M': handleMoistureReading(); break; case 'D': handleDryLevelAck(); break; case 'C': handleConfigurationAck(); break; case 'E': bytesRead = read(serial[activePort], &(buffer[1]), 1); assert(bytesRead == 1); bytesRead = read(serial[activePort], &(buffer[2]), 1); assert(bytesRead == 1); bytesRead = read(serial[activePort], &(buffer[3]), 1); assert(bytesRead == 1); validateChecksum(buffer, 4); handleError(buffer[1], buffer[2]); break; case 'Z': printf("Working it seems.\n"); break; case 'X': printf("Not working it seems.\n"); break; default: handleError('R', 10); printf("Received '%c'.\n", buf); break; } }