Exemple #1
0
void proto_packet_seal(struct proto_packet *packet)
{
	packet->sof = PROTO_SOF;
	packet->crc = crc8_start();
	packet->crc = crc8_calc(packet->crc, packet->addr);
	packet->crc = crc8_calc(packet->crc, packet->cmd);
	packet->crc = crc8_calc(packet->crc, packet->val);
	packet->crc = crc8_end(packet->crc);
	packet->eof = PROTO_EOF;
}
/*
 * Given offset in flash area, fill in rest of the fcb_entry, and crc8 over
 * the data.
 */
int
fcb_elem_crc8(struct fcb *fcb, struct fcb_entry *loc, uint8_t *c8p)
{
    uint8_t tmp_str[FCB_TMP_BUF_SZ];
    int cnt;
    int blk_sz;
    uint8_t crc8;
    uint16_t len;
    uint32_t off;
    uint32_t end;
    int rc;

    if (loc->fe_elem_off + 2 > loc->fe_area->fa_size) {
        return FCB_ERR_NOVAR;
    }
    rc = flash_area_read(loc->fe_area, loc->fe_elem_off, tmp_str, 2);
    if (rc) {
        return FCB_ERR_FLASH;
    }

    cnt = fcb_get_len(tmp_str, &len);
    if (cnt < 0) {
        return cnt;
    }
    loc->fe_data_off = loc->fe_elem_off + fcb_len_in_flash(fcb, cnt);
    loc->fe_data_len = len;

    crc8 = crc8_init();
    crc8 = crc8_calc(crc8, tmp_str, cnt);

    off = loc->fe_data_off;
    end = loc->fe_data_off + len;
    for (; off < end; off += blk_sz) {
        blk_sz = end - off;
        if (blk_sz > sizeof(tmp_str)) {
            blk_sz = sizeof(tmp_str);
        }

        rc = flash_area_read(loc->fe_area, off, tmp_str, blk_sz);
        if (rc) {
            return FCB_ERR_FLASH;
        }
        crc8 = crc8_calc(crc8, tmp_str, blk_sz);
    }
    *c8p = crc8;

    return 0;
}
Exemple #3
0
/**
 * @param credits - only used for RFCOMM flow control in UIH wiht P/F = 1
 */
void rfcomm_send_packet(uint16_t source_cid, uint8_t address, uint8_t control, uint8_t credits, uint8_t *data, uint16_t len){
	
	uint16_t pos = 0;
	uint8_t crc_fields = 3;
	
	rfcomm_out_buffer[pos++] = address;
	rfcomm_out_buffer[pos++] = control;
	
	// length field can be 1 or 2 octets
	if (len < 128){
		rfcomm_out_buffer[pos++] = (len << 1)| 1;     // bits 0-6
	} else {
		rfcomm_out_buffer[pos++] = (len & 0x7f) << 1; // bits 0-6
		rfcomm_out_buffer[pos++] = len >> 7;          // bits 7-14
		crc_fields++;
	}
	
	// add credits for UIH frames when PF bit is set
	if (control == BT_RFCOMM_UIH_PF){
		rfcomm_out_buffer[pos++] = credits;
	}
	
	// copy actual data
	memcpy(&rfcomm_out_buffer[pos], data, len);
	pos += len;
	
	// UIH frames only calc FCS over address + control (5.1.1)
	if ((control & 0xef) == BT_RFCOMM_UIH){
		crc_fields = 2;
	}
	rfcomm_out_buffer[pos++] =  crc8_calc(rfcomm_out_buffer, crc_fields); // calc fcs
    bt_send_l2cap( source_cid, rfcomm_out_buffer, pos);
}