Riot-OS: tutorial to use command handlers and multi threading

This content has 5 years. Please, read this page keeping its age in your mind.

This another tutorial of how using command handlers and multithreading. In short, we have a thread that listens for messages and prints the pid and the content value. To send the message we use a shell script by using the command handle mechanism. The code looks like:

#include <stdio.h>
#include <string.h>
#include <thread.h>

#include "shell.h"

char rcv_thread_stack[THREAD_STACKSIZE_MAIN];
kernel_pid_t pid;

int msgSend(int argc, char **argv)
{
  msg_t m;
  m.content.value = 1;
  msg_send(&m, pid);

  return 0;
}

const shell_command_t commands[] = {
  {"msg", "send message to the secondary thread", msgSend},
  {NULL, NULL, NULL}
};

void *rcv_thread(void *arg)
{
  (void)arg;
  msg_t m;

  while (1) {
    msg_receive(&m);
    printf("Got msg from %" PRIkernel_pid " with data %d\n", m.sender_pid, m.content.value);
  }

  printf("Hello, I'm a thread!\n");

  return NULL;
}

int main(void)
{
  printf("This is task 03\n");

  pid = thread_create(rcv_thread_stack, sizeof(rcv_thread_stack),
      THREAD_PRIORITY_MAIN - 1,
      THREAD_CREATE_STACKTEST,
      rcv_thread,
      NULL, "thread");

  char line_buf[SHELL_DEFAULT_BUFSIZE];
  shell_run( commands, line_buf, SHELL_DEFAULT_BUFSIZE);

  return 0;
}

In order to compile the code, you should add in the makefile the following condition to avoid consider warnings as errors.

WERROR ?=0