Example #1
0
int sixlowpan_mac_prepare_ieee802144_frame(
    ieee802154_frame_t *frame, int if_id, uint16_t dest_pan, const void *dest,
    uint8_t dest_len, const void *payload, uint8_t length, uint8_t mcast)
{
    uint8_t src_mode = net_if_get_src_address_mode(if_id);
    uint8_t dest_mode;
    uint16_t *fcs;
    set_ieee802154_frame_values(if_id, dest_pan, frame);

    if (dest_len == 8) {
        dest_mode = IEEE_802154_LONG_ADDR_M;
    }
    else if (dest_len == 2) {
        dest_mode = IEEE_802154_SHORT_ADDR_M;
    }
    else {
        DEBUG("Illegal IEEE 802.15.4 address for address length %d\n", dest_len);
        return -1;
    }

    set_ieee802154_fcf_values(frame, dest_mode, src_mode);


    if (src_mode == IEEE_802154_LONG_ADDR_M) {
        net_if_get_eui64((net_if_eui64_t *)&frame->src_addr[0], if_id, 0);
    }
    else if (src_mode == IEEE_802154_SHORT_ADDR_M) {
        uint16_t src = HTONS(net_if_get_hardware_address(if_id));
        memcpy(&frame->src_addr[0], &src, 2);
    }
    else {
        DEBUG("Illegal IEEE 802.15.4 address mode: %d\n", src_mode);
        return -1;
    }

    if (mcast) {
        memset(&frame->dest_addr[0], 0xff, dest_len);
    }
    else {
        memcpy(&frame->dest_addr[0], dest, dest_len);
    }

    frame->payload = (uint8_t *)payload; // payload won't be changed so cast is legal.
    frame->payload_len = length;
    uint8_t hdrlen = ieee802154_frame_get_hdr_len(frame);

    memset(&lowpan_mac_buf, 0, PAYLOAD_SIZE);
    ieee802154_frame_init(frame, (uint8_t *)&lowpan_mac_buf);
    memcpy(&lowpan_mac_buf[hdrlen], frame->payload, frame->payload_len);
    /* set FCS */
    fcs = (uint16_t *)&lowpan_mac_buf[frame->payload_len + hdrlen];
    *fcs = ieee802154_frame_get_fcs(lowpan_mac_buf, frame->payload_len + hdrlen);
    DEBUG("IEEE802.15.4 frame - FCF: %02X %02X DPID: %02X SPID: %02X DSN: %02X\n",
          lowpan_mac_buf[0], lowpan_mac_buf[1], frame->dest_pan_id,
          frame->src_pan_id, frame->seq_nr);

    return hdrlen;
}
Example #2
0
File: mac.c Project: fjrk/RIOT
void sixlowpan_mac_send_ieee802154_frame(const ieee_802154_long_t *addr,
        const uint8_t *payload,
        uint8_t length, uint8_t mcast)
{
    uint16_t daddr;
    /* TODO: check if dedicated response struct is necessary */
    msg_t transceiver_rsp;
    r_src_addr = local_address;
    mesg.type = SND_PKT;
    mesg.content.ptr = (char *) &tcmd;

    tcmd.transceivers = transceiver_type;
    tcmd.data = &p;

    ieee802154_frame_t frame;

    memset(&frame, 0, sizeof(frame));
    set_ieee802154_fcf_values(&frame, IEEE_802154_LONG_ADDR_M,
                              IEEE_802154_LONG_ADDR_M);
    set_ieee802154_frame_values(&frame);

    memcpy(&(frame.dest_addr[0]), &(addr->uint8[0]), 8);
    memcpy(&(frame.src_addr[0]), &(iface.laddr.uint8[0]), 8);

    daddr = HTONS(addr->uint16[3]);
    frame.payload = (uint8_t *)payload; // payload won't be changed so cast is legal.
    frame.payload_len = length;
    uint8_t hdrlen = get_802154_hdr_len(&frame);

    memset(&buf, 0, PAYLOAD_SIZE);
    init_802154_frame(&frame, (uint8_t *)&buf);
    memcpy(&buf[hdrlen], frame.payload, frame.payload_len);
    /* set FCS */
    /* RSSI = 0 */
    buf[frame.payload_len+hdrlen] = 0;
    /* FCS Valid = 1 / LQI Correlation Value = 0 */
    buf[frame.payload_len+hdrlen+1] = 0x80;
    DEBUG("IEEE802.15.4 frame - FCF: %02X %02X DPID: %02X SPID: %02X DSN: %02X\n", buf[0], buf[1], frame->dest_pan_id, frame->src_pan_id, frame->seq_nr);

    p.length = hdrlen + frame.payload_len + IEEE_802154_FCS_LEN;

    if (mcast == 0) {
        p.dst = daddr;
    }
    else {
        p.dst = 0;
    }

    p.data = buf;
    msg_send_receive(&mesg, &transceiver_rsp, transceiver_pid);

    hwtimer_wait(5000);
}