0

I'm having some problems with pointers in a school task, since I haven't programmed in C in a long time. I'm trying to store a value called rssi on a device running Contiki-NG which is like an OS for IOT devices.

I initialized the variable rssi in the beginning of the code:

static int8_t *rssi;

Then, in a function called on_reception, I'm updating the variable:

void on_reception(const void *data,
    uint16_t len,
    const linkaddr_t *src,
    const linkaddr_t *dest)
{
   ....
   // Update variable
   rssi = &packetbuf_attr(PACKETBUF_ATTR_RSSI);
}

And then in the main loop I'm trying to store this value using a provided cfs_write function:

PROCESS_THREAD(receiver_process, ev, data)
{
   PROCESS_BEGIN();
   ....

   while(1) {
    PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&periodic_timer));
    etimer_reset(&periodic_timer);
    entry = akes_nbr_head();
    if(entry && entry->permanent) {
      akes_trickle_stop();
      break;
    }
    
    // Store rssi value
    r = cfs_write(fd, &rssi, sizeof(rssi));
  }
}

However I'm getting problems like:

"lvalue required as unary '&' operand"

I've tried using '*' instead of '&' and what not but I can't seem to get it to work. Could anyone point me in the right direction?

EDIT: PACKETBUF_ATTR is a struct that contains a value according to the header file:

typedef uint16_t packetbuf_attr_t;

struct packetbuf_attr {
  packetbuf_attr_t val;
};

They gave me an example of printing the values:

  memcpy(&counter, data, sizeof(counter));
  printf("B,PING,%lu,%u,%lu,%i,0\n",
      clock_time(),
      (uint8_t)packetbuf_attr(PACKETBUF_ATTR_CHANNEL),
      counter,
      (int8_t)packetbuf_attr(PACKETBUF_ATTR_RSSI));
5
  • Which is the line where you get the error? Commented May 2, 2022 at 9:57
  • @SupportUkraine Yeah, that's true. I tried using calloc() instead, but then it just complains that the "initializer element is not constant": static int8_t *rssi = calloc(1, sizeof(int8_t)); Commented May 2, 2022 at 10:02
  • "I'm not sure what PACKETBUF_ATTR is returning": Well, since your source uses this symbol, you should be able to track it into some header file to reveal its nature. -- What is packetbuf_attr? -- "I updated the answer." Do you mean the question? Commented May 2, 2022 at 10:15
  • I initialized the variable rssi No, the variable is not initialized besides automatic initialization with 0. Commented May 2, 2022 at 10:19
  • @SupportUkraine Yeah, that was kind of what the initial problem was. I'm not sure if it should be a pointer or not, and I've tried not using pointers but haven't gotten it to work. Commented May 2, 2022 at 10:40

1 Answer 1

1

The function packetbuf_attr() returns value of type packetbuf_attr_t, which is just another name for uint16_t (unsigned 16 bit integer):

typedef uint16_t packetbuf_attr_t;

You don't need to use pointers. The important issue is that RSSI is a signed integer, usually below zero, and always fits in 8 bits. Taking this into account, you need to cast the return value of packetbuf_attr() to the correct type:

static int8_t rssi;
...
rssi = (int8_t)packetbuf_attr(PACKETBUF_ATTR_RSSI);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.