int measure_updateRot(uint8_t *data, size_t len) { // Save the rot of current encoded data. uint16_t rot; rotating_init(&rot); rotating_update(data, len, &rot); LOG_INFO("Update rot in bkp[%d]\n", rot); bkp_write(&rot, sizeof(uint16_t)); return 0; }
int measure_isDataChage(uint8_t *data, size_t len) { uint16_t rot; uint16_t prev_rot; bkp_read(&prev_rot, sizeof(uint16_t)); rotating_init(&rot); rotating_update(data, len, &rot); LOG_INFO("Prev[%d] Now[%d]\n", prev_rot, rot); if (rot == prev_rot) return 0; bkp_write(&rot, sizeof(uint16_t)); return 1; }
/** * Try to read a packet from the pocketBus. * \return true if a packet is received, false otherwise. */ bool pocketbus_recv(struct PocketBusCtx *ctx, struct PocketMsg *msg) { int c; /* Process incoming characters until buffer is not empty */ while ((c = kfile_getc(ctx->fd)) != EOF) { /* Look for STX char */ if (c == POCKETBUS_STX && !ctx->escape) { /* When an STX is found, inconditionally start a new packet */ if (ctx->sync) kprintf("pocketBus double sync!\n"); ctx->sync = true; ctx->len = 0; rotating_init(&ctx->in_cks); continue; } if (ctx->sync) { /* Handle escape mode */ if (c == POCKETBUS_ESC && !ctx->escape) { ctx->escape = true; continue; } /* Handle message end */ if (c == POCKETBUS_ETX && !ctx->escape) { ctx->sync = false; /* Check minimum size */ if (ctx->len < sizeof(PocketBusHdr) + sizeof(rotating_t)) { kprintf("pocketBus short pkt!\n"); continue; } /* Remove checksum bytes from packet len */ ctx->len -= sizeof(rotating_t); /* Compute checksum */ rotating_update(ctx->buf, ctx->len, &ctx->in_cks); uint8_t cks_h = *(ctx->buf + ctx->len); uint8_t cks_l = *(ctx->buf + ctx->len + 1); rotating_t recv_cks = (cks_h << 8) | cks_l; /* Checksum check */ if (recv_cks == ctx->in_cks) { PocketBusHdr *hdr = (PocketBusHdr *)ctx; /* Check packet version */ if (hdr->ver == POCKETBUS_VER) { /* Packet received, set msg fields */ msg->payload = ctx->buf + sizeof(PocketBusHdr); msg->addr = be16_to_cpu(hdr->addr); msg->len = ctx->len - sizeof(PocketBusHdr); msg->ctx = ctx; return true; } else { kprintf("pocketBus version mismatch, here[%d], there[%d]\n", POCKETBUS_VER, hdr->ver); continue; } } else { kprintf("pocketBus cks error, here[%04X], there[%04X]\n", ctx->in_cks, recv_cks); continue; } } ctx->escape = false; /* Check buffer overflow: simply ignore received data and go to unsynced state. */ if (ctx->len >= CONFIG_POCKETBUS_BUFLEN) { kprintf("pocketBus buffer overflow\n"); ctx->sync = false; continue; } /* Put received data in the buffer */ ctx->buf[ctx->len] = c; ctx->len++; } } /* * Check stream status. */ if (kfile_error(ctx->fd)) { LOG_ERR("fd status[%04X]\n", kfile_error(ctx->fd)); kfile_clearerr(ctx->fd); } return false; }