Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8677605
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:30:10+00:00 2026-06-12T20:30:10+00:00

I found this in unicast example of contiki written using C. #define MSG_LEN 20

  • 0

I found this in unicast example of contiki written using C.

#define MSG_LEN 20
msg_header_t * header;
uint8_t *data;

packetbuf_clear();
header = (msg_header_t *)(packetbuf_dataptr());
data = (uint8_t *)(header + 1);
random_data(data, MSG_LEN);
hton_uint16(&header->data_len, MSG_LEN);

packetbuf_set_datalen(sizeof(msg_header_t) + MSG_LEN);
rimeaddr_t addr;
addr.u8[0] = 2;
addr.u8[1] = 0;
if(!rimeaddr_cmp(&addr, &rimeaddr_node_addr)) {
    unicast_send(&uc, &addr);
}

Here are some regarding details

typedef struct {
    unsigned char data[2]; 
} nw_uint16_t;

typedef struct msg_header 
{
    NN_DIGIT r[NUMWORDS]; //NN_DIGIT = uint32_t, NUMWORDS = 6
    NN_DIGIT s[NUMWORDS];
    nw_uint16_t data_len; 
} msg_header_t;

inline uint16_t hton_uint16(void * target, uint16_t value);

inline uint16_t ntoh_uint16(void * source);

methods

inline uint16_t  hton_uint16(void * target, uint16_t value) 
{
    uint8_t *base = target;
    base[1] = value;
    base[0] = value >> 8;
    return value;
}
/*---------------------------------------------------------------------------*/
inline uint16_t ntoh_uint16(void * source)
{
    uint8_t *base = source;
    return (uint16_t)(base[0] << 8 | base[1]);
}
/*---------------------------------------------------------------------------*/
static void random_data(void *ptr, uint16_t len)
{
    uint16_t i;
    for(i=0; i<len; i++) {
        srand(100);
        ((uint8_t *)(ptr))[i] = 2; 
    }
}

packetbuf methods
http://dak664.github.com/contiki-doxygen/a01563.html#_details

Can anyone explain me these packetbuf_clear(); header = (msg_header_t *)(packetbuf_dataptr()); data = (uint8_t *)(header + 1); random_data(data, MSG_LEN);

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T20:30:11+00:00Added an answer on June 12, 2026 at 8:30 pm

    From what I can understand:

    packetbuf_dataptr() allocates enough memory to hold a packet header, data length and it’s payload.

    header = (msg_header_t *)(packetbuf_dataptr()); sets the pointer to the header to the beginning of that memory array.

    data = (uint8_t *)(header + 1); sets the beginning of the data “payload” to the first memory address after the header header + 1 which means as much as <address of header> + sizeof(msg_header_t)

    random_data(data, MSG_LEN);puts some random data of MSG_LEN bytes into the payload.

    hton_uint16(&header->data_len, MSG_LEN); writes the length of the data segment in network byte order into the packet header data_len field.

    packetbuf_set_datalen(sizeof(msg_header_t) + MSG_LEN); sends the header off together with it’s payload.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I found this example using php, Facebook user deauthorizes the app but unfortunately, I
Found this is some example CSS I was reading and I'm having a hard
I found this example where they used PowerMock and EasyMock to stub/mock the Menu
Found this answer In Jquery mobile, Header and footers are always hiding, when clicking
I found this code for a replacement gotoxy() function using C standard library only.
Found this great article on using jquery for image swapping: http://jquery-howto.blogspot.com/2009/05/replacing-images-at-time-intervals.html How do you
I found this guide for using the flash parameters, thought it might be useful
I found this very useful program that was written for a Console application: http://www.codeproject.com/Articles/1415/Introduction-to-TCP-client-server-in-C
Found this interesting interview question: You need to display the sales data for your
Found this technique of using ajax to preload things at: http://perishablepress.com/3-ways-preload-images-css-javascript-ajax/ window.onload = function()

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.