temp_sensor_client_linux.c 4.04 KB
Newer Older
Kishen Maloor's avatar
Kishen Maloor committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
// Copyright (c) 2016 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
*/

#include "oc_api.h"

19
static int
Kishen Maloor's avatar
Kishen Maloor committed
20 21
app_init(void)
{
22
  int ret = oc_init_platform("GE", NULL, NULL);
23 24
  ret |= oc_add_device("/oic/d", "oic.d.smarthub", "Smart home hub",
                       "ocf.1.0.0", "ocf.res.1.0.0", NULL, NULL);
25
  return ret;
Kishen Maloor's avatar
Kishen Maloor committed
26 27 28 29
}

#define MAX_URI_LENGTH (30)
static char temp_1[MAX_URI_LENGTH];
30
static oc_endpoint_t *temp_sensor;
Kishen Maloor's avatar
Kishen Maloor committed
31 32
static int temperature;

Flavio Ceolin's avatar
Flavio Ceolin committed
33 34
static oc_event_callback_retval_t
stop_observe(void *data)
Kishen Maloor's avatar
Kishen Maloor committed
35
{
Kishen Maloor's avatar
Kishen Maloor committed
36
  (void)data;
Kishen Maloor's avatar
Kishen Maloor committed
37
  PRINT("Stopping OBSERVE\n");
38
  oc_stop_observe(temp_1, temp_sensor);
39
  return OC_EVENT_DONE;
Kishen Maloor's avatar
Kishen Maloor committed
40 41
}

Flavio Ceolin's avatar
Flavio Ceolin committed
42
static void
Kishen Maloor's avatar
Kishen Maloor committed
43 44 45 46 47 48
get_temp(oc_client_response_t *data)
{
  oc_rep_t *rep = data->payload;
  while (rep != NULL) {
    PRINT("key %s, value ", oc_string(rep->name));
    switch (rep->type) {
49
    case OC_REP_INT:
George Nash's avatar
George Nash committed
50
      PRINT("%lld\n", rep->value.integer);
Kishen Maloor's avatar
Kishen Maloor committed
51
      temperature = (int)rep->value.integer;
Kishen Maloor's avatar
Kishen Maloor committed
52 53 54 55 56 57 58 59
      break;
    default:
      break;
    }
    rep = rep->next;
  }
}

Flavio Ceolin's avatar
Flavio Ceolin committed
60
static oc_discovery_flags_t
61
discovery(const char *anchor, const char *uri, oc_string_array_t types,
George Nash's avatar
George Nash committed
62
          oc_interface_mask_t iface_mask, oc_endpoint_t *endpoint,
63
          oc_resource_properties_t bm, void *user_data)
Kishen Maloor's avatar
Kishen Maloor committed
64
{
65
  (void)anchor;
George Nash's avatar
George Nash committed
66
  (void)iface_mask;
Kishen Maloor's avatar
Kishen Maloor committed
67
  (void)user_data;
68
  (void)bm;
Kishen Maloor's avatar
Kishen Maloor committed
69 70
  int i;
  int uri_len = strlen(uri);
Kishen Maloor's avatar
Kishen Maloor committed
71
  uri_len = (uri_len >= MAX_URI_LENGTH) ? MAX_URI_LENGTH - 1 : uri_len;
Kishen Maloor's avatar
Kishen Maloor committed
72

Kishen Maloor's avatar
Kishen Maloor committed
73
  for (i = 0; i < (int)oc_string_array_get_allocated_size(types); i++) {
Kishen Maloor's avatar
Kishen Maloor committed
74 75
    char *t = oc_string_array_get_item(types, i);
    if (strlen(t) == 16 && strncmp(t, "oic.r.tempsensor", 16) == 0) {
76
      oc_endpoint_list_copy(&temp_sensor, endpoint);
Kishen Maloor's avatar
Kishen Maloor committed
77 78 79
      strncpy(temp_1, uri, uri_len);
      temp_1[uri_len] = '\0';

80 81 82 83 84 85 86 87 88
      PRINT("Resource %s hosted at endpoints:\n", temp_1);
      oc_endpoint_t *ep = endpoint;
      while (ep != NULL) {
        PRINTipaddr(*ep);
        PRINT("\n");
        ep = ep->next;
      }

      oc_do_observe(temp_1, temp_sensor, NULL, &get_temp, HIGH_QOS, NULL);
Kishen Maloor's avatar
Kishen Maloor committed
89 90 91 92 93 94 95 96
      oc_set_delayed_callback(NULL, &stop_observe, 30);

      return OC_STOP_DISCOVERY;
    }
  }
  return OC_CONTINUE_DISCOVERY;
}

Flavio Ceolin's avatar
Flavio Ceolin committed
97
static void
Kishen Maloor's avatar
Kishen Maloor committed
98 99
issue_requests(void)
{
100
  oc_do_ip_discovery("oic.r.tempsensor", &discovery, NULL);
Kishen Maloor's avatar
Kishen Maloor committed
101 102 103
}

#include "port/oc_clock.h"
Kishen Maloor's avatar
Kishen Maloor committed
104 105 106
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
Kishen Maloor's avatar
Kishen Maloor committed
107

Flavio Ceolin's avatar
Flavio Ceolin committed
108 109 110 111
static pthread_mutex_t mutex;
static pthread_cond_t cv;
static struct timespec ts;
static int quit = 0;
Kishen Maloor's avatar
Kishen Maloor committed
112

113 114
static void
signal_event_loop(void)
Kishen Maloor's avatar
Kishen Maloor committed
115
{
116
  pthread_mutex_lock(&mutex);
Kishen Maloor's avatar
Kishen Maloor committed
117
  pthread_cond_signal(&cv);
118
  pthread_mutex_unlock(&mutex);
Kishen Maloor's avatar
Kishen Maloor committed
119 120
}

Flavio Ceolin's avatar
Flavio Ceolin committed
121
static void
Kishen Maloor's avatar
Kishen Maloor committed
122 123
handle_signal(int signal)
{
Kishen Maloor's avatar
Kishen Maloor committed
124
  (void)signal;
125
  signal_event_loop();
Kishen Maloor's avatar
Kishen Maloor committed
126 127 128 129 130 131 132 133 134 135 136 137
  quit = 1;
}

int
main(void)
{
  struct sigaction sa;
  sigfillset(&sa.sa_mask);
  sa.sa_flags = 0;
  sa.sa_handler = handle_signal;
  sigaction(SIGINT, &sa, NULL);

138 139 140
  static const oc_handler_t handler = { .init = app_init,
                                        .signal_event_loop = signal_event_loop,
                                        .requests_entry = issue_requests };
Kishen Maloor's avatar
Kishen Maloor committed
141 142 143

  oc_clock_time_t next_event;

144
#ifdef OC_STORAGE
145
  oc_storage_config("./temp_sensor_creds");
146
#endif /* OC_STORAGE */
147

Kishen Maloor's avatar
Kishen Maloor committed
148 149 150 151 152 153 154 155 156 157
  int init = oc_main_init(&handler);

  if (init < 0)
    return init;

  while (quit != 1) {
    next_event = oc_main_poll();
    pthread_mutex_lock(&mutex);
    if (next_event == 0) {
      pthread_cond_wait(&cv, &mutex);
Kishen Maloor's avatar
Kishen Maloor committed
158
    } else {
Kishen Maloor's avatar
Kishen Maloor committed
159 160 161 162 163 164 165 166 167 168 169
      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);
    }
    pthread_mutex_unlock(&mutex);
  }

  oc_main_shutdown();

  return 0;
}