Blame view

RIOT/tests/msg_send_receive/main.c 2.42 KB
a752c7ab   elopes   add first test an...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
  /*
   * Copyright (C) 2014 Martine Lenders <mlenders@inf.fu-berlin.de>
   *
   * This file is subject to the terms and conditions of the GNU Lesser
   * General Public License v2.1. See the file LICENSE in the top level
   * directory for more details.
   */
  
  /**
   * @ingroup     tests
   * @{
   *
   * @file
   * @brief       Test msg_send_receive().
   *
   * @author      Martine Lenders <mlenders@inf.fu-berlin.de>
   * @author      René Kijewski <rene.kijewski@fu-berlin.de>
   * @author      Oliver Hahm <oliver.hahm@inria.fr>
   *
   * @}
   */
  
  #include <stdio.h>
  
  #include "cpu_conf.h"
  #include "thread.h"
  
  #define THREAD1_STACKSIZE   (THREAD_STACKSIZE_MAIN)
  #define THREAD2_STACKSIZE   (THREAD_STACKSIZE_MAIN)
  
  #ifndef TEST_EXECUTION_NUM
  #define TEST_EXECUTION_NUM  (10)
  #endif
  
  static char thread1_stack[THREAD1_STACKSIZE];
  static char thread2_stack[THREAD2_STACKSIZE];
  
  static kernel_pid_t thread1_pid, thread2_pid;
  
  static int counter1 = 0;
  static int counter2 = 0;
  
  static void *thread1(void *args)
  {
      (void)args;
  
      msg_t msg_req, msg_resp;
      int success = 1;
  
      msg_resp.content.ptr = NULL;
      msg_req.content.ptr = &counter1;
  
      for (int i = 0; i < TEST_EXECUTION_NUM; i++) {
          msg_send_receive(&msg_req, &msg_resp, thread2_pid);
  
          if ((NULL == msg_resp.content.ptr) ||
              (&counter1 != ((int *) msg_req.content.ptr)) ||
              (counter1 != (*(int *) msg_resp.content.ptr)) ||
              (counter1 != (*(int *) msg_req.content.ptr))) {
              success = 0;
              break;
          }
      }
  
      if (success) {
          puts("Test successful.");
      }
      else {
          puts("Test failed.");
      }
  
      return NULL;
  }
  
  static void *thread2(void *args)
  {
      (void)args;
  
      msg_t msg_req, msg_resp;
  
      msg_resp.content.ptr = &counter2;
  
      for (int i = 0; i < TEST_EXECUTION_NUM; i++) {
          msg_receive(&msg_req);
  
          ++*(int *) msg_req.content.ptr;
          ++*(int *) msg_resp.content.ptr;
          printf("Incremented counters to %d and %d\n",
                 *(int *) msg_req.content.ptr, *(int *) msg_resp.content.ptr);
  
          msg_reply(&msg_req, &msg_resp);
      }
  
      return NULL;
  }
  
  int main(void)
  {
      thread2_pid = thread_create(thread2_stack, THREAD2_STACKSIZE, THREAD_PRIORITY_MAIN - 2,
                                  0, thread2, NULL, "thread2");
      thread1_pid = thread_create(thread1_stack, THREAD1_STACKSIZE, THREAD_PRIORITY_MAIN - 1,
                                  0, thread1, NULL, "thread1");
      return 0;
  }