Blame view

RIOT/examples/iotivity_examples/server/server.c 2.7 KB
fb11e647   vrobic   reseau statique a...
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
  /*
   * Copyright (C) 2016 CREATE-NET
   *
   * 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     Examples
   * @{
   *
   * @file        server.c
   * @brief       This is an IoTivity Server.
   *
   * @author      Mattia Antonini <mattia.antonini1@studenti.unipr.it>
   *                              <m.antonini@create-net.org>
   *
   * @}
   */
  
  #include "oc_api.h"
  #include "xtimer.h"
  #include "pthread_cond.h"
  #include "thread.h"
  
  extern int _netif_config(int argc, char **argv);
  
  static int quit;
  static mutex_t mutex;
  static pthread_cond_t cv;
  static struct timespec ts;
  static char _oc_main_stack[THREAD_STACKSIZE_MAIN];
  
  #define OC_QUEUE_SIZE     (8)
  static msg_t _oc_msg_queue[OC_QUEUE_SIZE];
  
  //Resources registration functions
  extern void res_light_register(void);
  
  static void
  app_init(void)
  {
      oc_init_platform("RIOT-OS", NULL, NULL);
  
      oc_add_device("/oic/d", "oic.d.light", "RIOT's light", "1.0", "1.0", NULL,
                    NULL);
  }
  
  
  static void
  register_resources_func(void)
  {
      res_light_register();
  }
  
  static void
  signal_event_loop_func(void)
  {
      mutex_lock(&mutex);
      pthread_cond_signal(&cv);
      mutex_unlock(&mutex);
  }
  
  void *
  oc_main_thread(void *arg)
  {
      (void)arg;
  
      pthread_cond_init(&cv, NULL);
  
      static const oc_handler_t handler = { .init = app_init,
                                            .signal_event_loop = signal_event_loop_func,
                                            .register_resources =
                                                register_resources_func };
  
      msg_init_queue(_oc_msg_queue, OC_QUEUE_SIZE);
  
      if (oc_main_init(&handler) < 0) {
          PRINT("server_oic: failed to initialize stack\n");
          return NULL;
      }
  
      /* print network addresses */
      puts("server_oic: Configured network interfaces:");
      _netif_config(0, NULL);
  
  
  
      oc_clock_time_t next_event;
      while (!quit) {
          next_event = oc_main_poll();
          mutex_lock(&mutex);
          if (next_event == 0) {
              pthread_cond_wait(&cv, &mutex);
          }
          else {
              ts.tv_sec = (next_event / OC_CLOCK_SECOND);
              ts.tv_nsec = (next_event % OC_CLOCK_SECOND) * 1.e09 / OC_CLOCK_SECOND;
              pthread_cond_timedwait(&cv, &mutex, &ts);
          }
          mutex_unlock(&mutex);
      }
  
      oc_main_shutdown();
  
      return NULL;
  }
  
  int
  main(void)
  {
      PRINT("server_oic: Waiting for address autoconfiguration...");
      xtimer_sleep(10);
  
      thread_create(_oc_main_stack, sizeof(_oc_main_stack), 6, 0, oc_main_thread,
                    NULL, "OCF event thread");
  
      fgetc(stdin);
  
      quit = 1;
      signal_event_loop_func();
  
      return 0;
  }