static int zm_dataevent(FAR struct zm_state_s *pzm)
{
    zmdbg("Received type: %d length: %d\n", pzm->pkttype, pzm->pktlen);
    zmdbg("PSTATE %d:%d->%d:%d\n",
          pzm->pstate, pzm->psubstate, PSTATE_IDLE, PIDLE_ZPAD);

    /* Revert to the IDLE state */

    pzm->pstate    = PSTATE_IDLE;
    pzm->psubstate = PIDLE_ZPAD;

    /* Verify the checksum. 16- or 32-bit? */

    if (pzm->hdrfmt == ZBIN32)
    {
        uint32_t crc;

        crc = crc32part(pzm->pktbuf, pzm->pktlen, 0xffffffff);
        if (crc != 0xdebb20e3)
        {
            zmdbg("ERROR: ZBIN32 CRC32 failure: %08x vs debb20e3\n", crc);
            pzm->flags &= ~ZM_FLAG_CRKOK;
        }
        else
        {
            pzm->flags |= ZM_FLAG_CRKOK;
        }

        /* Adjust the back length to exclude the packet type length of the 4-
         * byte checksum.
         */

        pzm->pktlen -= 5;
    }
    else
    {
        uint16_t crc;

        crc = crc16part(pzm->pktbuf, pzm->pktlen, 0);
        if (crc != 0)
        {
            zmdbg("ERROR: ZBIN/ZHEX CRC16 failure: %04x vs 0000\n", crc);
            pzm->flags &= ~ZM_FLAG_CRKOK;
        }
        else
        {
            pzm->flags |= ZM_FLAG_CRKOK;
        }

        /* Adjust the back length to exclude the packet type length of the 2-
         * byte checksum.
         */

        pzm->pktlen -= 3;
    }

    /* Then handle the data received event */

    return zm_event(pzm, ZME_DATARCVD);
}
예제 #2
0
/**
 * @fn                  :tc_libc_misc_crc16part
 * @brief               :Continue CRC calculation on a part of the buffer.
 * @scenario            :Continue CRC calculation on a part of the buffer.
 * @API's covered       :crc16part
 * @Preconditions       :None
 * @Postconditions      :None
 * @Return              :void
 */
static void tc_libc_misc_crc16part(void)
{
	uint16_t ret_chk;
	uint8_t src_arr[1] = { VAL_100 };
	uint16_t crc_16val = 0;
	size_t length = 1;

	/* Return value should be 100 as calculated by crc16part */

	ret_chk = crc16part(src_arr, length, crc_16val);
	TC_ASSERT_EQ("crc16part", ret_chk, VAL_100);

	/* Return value should be 65380 as calculated by crc16part */

	crc_16val = VAL_255;
	ret_chk = crc16part(src_arr, length, crc_16val);
	TC_ASSERT_EQ("crc16part", ret_chk, VAL_65380);

	TC_SUCCESS_RESULT();
}
static int zm_hdrevent(FAR struct zm_state_s *pzm)
{
    zmdbg("Received type: %d data: %02x %02x %02x %02x\n",
          pzm->hdrdata[0],
          pzm->hdrdata[1], pzm->hdrdata[2], pzm->hdrdata[3], pzm->hdrdata[4]);
    zmdbg("PSTATE %d:%d->%d:%d\n",
          pzm->pstate, pzm->psubstate, PSTATE_IDLE, PIDLE_ZPAD);

    /* Revert to the IDLE state */

    pzm->pstate    = PSTATE_IDLE;
    pzm->psubstate = PIDLE_ZPAD;

    /* Verify the checksum.  16- or 32-bit? */

    if (pzm->hdrfmt == ZBIN32)
    {
        uint32_t crc;

        /* Checksum is over 9 bytes:  The header type, 4 data bytes, plus 4 CRC bytes */

        crc = crc32part(pzm->hdrdata, 9, 0xffffffff);
        if (crc != 0xdebb20e3)
        {
            zmdbg("ERROR: ZBIN32 CRC32 failure: %08x vs debb20e3\n", crc);
            return zm_nakhdr(pzm);
        }
    }
    else
    {
        uint16_t crc;

        /* Checksum is over 7 bytes:  The header type, 4 data bytes, plus 2 CRC bytes */

        crc = crc16part(pzm->hdrdata, 7, 0);
        if (crc != 0)
        {
            zmdbg("ERROR: ZBIN/ZHEX CRC16 failure: %04x vs 0000\n", crc);
            return zm_nakhdr(pzm);
        }
    }

    return zm_event(pzm, pzm->hdrdata[0]);
}
예제 #4
0
파일: lib_crc16.c 프로젝트: davepmo/miniecu
uint16_t crc16(const uint8_t *src, size_t len)
{
  return crc16part(src, len, 0);
}