client.c
6.02 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*
* 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 client.c
* @brief This is a simple IoTivity Client
*
* @author Mattia Antonini <mattia.antonini1@studenti.unipr.it>
* <m.antonini@create-net.org>
*
* @}
*/
#include "oc_api.h"
#include "pthread_cond.h"
#include "thread.h"
#include "xtimer.h"
#define MAX_URI_LENGTH (30)
static void observe_light(oc_client_response_t *data);
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 bool got_discovery_response = false;
static bool observe_stopped = false;
static char light_1[MAX_URI_LENGTH];
static oc_server_handle_t light_server;
static bool light_state = false;
static char _oc_main_stack[THREAD_STACKSIZE_MAIN];
#define OC_QUEUE_SIZE (8)
static msg_t _oc_msg_queue[OC_QUEUE_SIZE];
static void
set_device_custom_property(void *data)
{
oc_set_custom_device_property(purpose, "operate lamp");
}
static void
set_led_state(bool led_state)
{
if (led_state) {
LED0_ON;
}
else {
LED0_OFF;
}
}
static void
app_init(void)
{
oc_init_platform("RIOT-OS", NULL, NULL);
oc_add_device("/oic/d", "oic.d.switch", "RIOT switch", "1.0", "1.0",
set_device_custom_property, NULL);
}
static oc_event_callback_retval_t
stop_observe(void *data)
{
PRINT("client_oic: Stopping OBSERVE\n");
oc_stop_observe(light_1, &light_server);
observe_stopped = true;
return DONE;
}
static void
put_light(oc_client_response_t *data)
{
PRINT("client_oic: PUT_light:\n");
if (data->code == OC_STATUS_CHANGED) {
PRINT("client_oic: PUT response OK\n");
}
else {
PRINT("client_oic: PUT response code %d\n", data->code);
}
}
static void
observe_light(oc_client_response_t *data)
{
PRINT("client_oic: OBSERVE_light: ");
oc_rep_t *rep = data->payload;
while (rep != NULL) {
PRINT("key %s, value ", oc_string(rep->name));
switch (rep->type) {
case BOOL:
PRINT("%d\n", rep->value_boolean);
light_state = rep->value_boolean;
break;
default:
break;
}
rep = rep->next;
}
}
static oc_discovery_flags_t
discovery(const char *di, const char *uri, oc_string_array_t types,
oc_interface_mask_t interfaces, oc_server_handle_t *server,
void *user_data)
{
int i;
int uri_len = strlen(uri);
uri_len = (uri_len >= MAX_URI_LENGTH) ? MAX_URI_LENGTH - 1 : uri_len;
for (i = 0; i < oc_string_array_get_allocated_size(types); i++) {
char *t = oc_string_array_get_item(types, i);
if (strlen(t) == 11 && strncmp(t, "oic.r.light", 11) == 0) {
memcpy(&light_server, server, sizeof(oc_server_handle_t));
strncpy(light_1, uri, uri_len);
light_1[uri_len] = '\0';
got_discovery_response = true;
return OC_STOP_DISCOVERY;
}
}
return OC_CONTINUE_DISCOVERY;
}
static oc_event_callback_retval_t
periodic_put(void *data)
{
if (observe_stopped) {
PRINT("client_oic: Stop periodic PUT\n");
return DONE;
}
if (oc_init_put(light_1, &light_server, NULL, &put_light, HIGH_QOS, NULL)) {
oc_rep_start_root_object();
oc_rep_set_boolean(root, state, !light_state);
oc_rep_end_root_object();
if (oc_do_put()) {
PRINT("client_oic: Sent PUT request\n");
}
else {
PRINT("client_oic: Could not send PUT\n");
}
}
else {
PRINT("client_oic: Could not init PUT\n");
}
return CONTINUE;
}
static oc_event_callback_retval_t
do_discovery(void *data)
{
if (got_discovery_response) {
PRINT("client_oic: Discovery done\n");
PRINT("client_oic: Ready...\n");
oc_do_observe(light_1, &light_server, NULL, &observe_light, LOW_QOS, NULL);
oc_set_delayed_callback(NULL, &periodic_put, 1);
oc_set_delayed_callback(NULL, &stop_observe, 100);
set_led_state(true);
return DONE;
}
oc_do_ip_discovery("oic.r.light", &discovery, NULL);
PRINT("client_oic: continue discovery\n");
return CONTINUE;
}
static void
issue_requests(void)
{
oc_set_delayed_callback(NULL, &do_discovery, 1);
}
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,
.requests_entry = issue_requests };
msg_init_queue(_oc_msg_queue, OC_QUEUE_SIZE);
if (oc_main_init(&handler) < 0) {
PRINT("client_oic: failed to initialize stack\n");
return NULL;
}
PRINT("client_oic: Configured network interfaces:");
_netif_config(0, NULL);
oc_clock_time_t next_event;
while (quit == 0) {
next_event = oc_main_poll();
mutex_lock(&mutex);
if (next_event == 0) {
pthread_cond_wait(&cv, &mutex);
}
else if (oc_clock_time() < next_event) {
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)
{
set_led_state(false);
PRINT("client_oic: Waiting for address autoconfiguration...");
xtimer_sleep(10);
thread_create(_oc_main_stack, sizeof(_oc_main_stack), 2, 0, oc_main_thread,
NULL, "OCF event thread");
fgetc(stdin);
quit = 1;
signal_event_loop_func();
return 0;
}