Blame view

RIOT/examples/iotivity_examples/server/resources/res-light.c 2.87 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
  /*
   * 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        res-light.c
   * @brief       Resource light definition. Handler are defined here.
   *
   * @author      Mattia Antonini <mattia.antonini1@studenti.unipr.it>
   *                              <m.antonini@create-net.org>
   *
   * @}
   */
  
  #include "oc_api.h"
  #include "board.h"
  
  
  static oc_resource_t *res_light = NULL;
  static bool res_light_state = false;
  
  static void
  res_light_set_led_state(bool light_state)
  {
      if (light_state) {
          PRINT("server_oic: LED0 is ON\n");
          LED0_ON;
      }
      else {
          PRINT("server_oic: LED0 is OFF\n");
          LED0_OFF;
      }
  }
  
  
  static void
  res_light_get_handler(oc_request_t *request, oc_interface_mask_t interface, void *user_data)
  {
      PRINT("server_oic: GET request\n");
      oc_rep_start_root_object();
      switch (interface) {
          case OC_IF_BASELINE:
              oc_process_baseline_interface(request->resource);
          case OC_IF_RW:
              oc_rep_set_boolean(root, state, res_light_state);
              break;
          default:
              break;
      }
      oc_rep_end_root_object();
      oc_send_response(request, OC_STATUS_OK);
      PRINT("server_oic: Light state %d\n", res_light_state);
  }
  
  static void
  res_light_put_handler(oc_request_t *request, oc_interface_mask_t interface, void *user_data)
  {
      PRINT("server_oic: PUT request\n");
      bool state = false;
      oc_rep_t *rep = request->request_payload;
  
      while (rep != NULL) {
          PRINT("server_oic: key: %s ", oc_string(rep->name));
          switch (rep->type) {
              case BOOL:
                  state = rep->value_boolean;
                  PRINT("value: %d\n", state);
                  break;
              default:
                  oc_send_response(request, OC_STATUS_BAD_REQUEST);
                  return;
                  break;
          }
          rep = rep->next;
      }
      oc_send_response(request, OC_STATUS_CHANGED);
      res_light_state = state;
      res_light_set_led_state(res_light_state);
  }
  
  void
  res_light_register(void)
  {
      res_light = oc_new_resource("/light/1", 1, 0);
      oc_resource_bind_resource_type(res_light, "oic.r.light");
      oc_resource_bind_resource_interface(res_light, OC_IF_RW);
      oc_resource_set_default_interface(res_light, OC_IF_RW);
  
  //TODO: Security is not supported by the pkg
  #ifdef OC_SECURITY
      oc_resource_make_secure(res_light);
  #endif
  
      oc_resource_set_discoverable(res_light, true);
      oc_resource_set_observable(res_light, true);
      oc_resource_set_request_handler(res_light, OC_GET, res_light_get_handler, NULL);
      oc_resource_set_request_handler(res_light, OC_PUT, res_light_put_handler, NULL);
      oc_add_resource(res_light);
  
      res_light_set_led_state(false); //RIMANE TRUE... PERCHe'
  }