-
Notifications
You must be signed in to change notification settings - Fork 2
/
ocf_mylight_notify.c
195 lines (147 loc) · 3.64 KB
/
ocf_mylight_notify.c
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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <errno.h>
#include "ocf_mylight.h"
#define MAX_SLOT 8
struct notify_slot {
bool active;
OCObservationId id;
};
static struct notify_slot slots[2][MAX_SLOT];
static pthread_t _notify_tid;
static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t lock_loop = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
static unsigned int changed_light = 0;
static int find_empty_slot(unsigned int light_id)
{
int i;
for (i = 0; i < MAX_SLOT; i++) {
if (slots[light_id][i].active == false)
return i;
}
return -1;
}
static int find_slot(unsigned int light_id, OCObservationId id)
{
int i;
for (i = 0; i < MAX_SLOT; i++) {
if (slots[light_id][i].id == id)
return i;
}
return -1;
}
static void notify(int light_id)
{
OCRepPayload *payload = NULL;
OCObservationId list[MAX_SLOT];
OCResourceHandle handle;
int i;
int list_cnt = 0;
bool value;
pthread_mutex_lock(&lock);
for (i = 0; i < MAX_SLOT; i++) {
if (slots[light_id][i].active) {
list[list_cnt] = slots[light_id][i].id;
list_cnt++;
}
}
DBG("list_cnt = %d", list_cnt);
pthread_mutex_unlock(&lock);
payload = OCRepPayloadCreate();
OCRepPayloadAddResourceType(payload, "oic.r.switch.binary");
OCRepPayloadAddInterface(payload, "oic.if.baseline");
OCRepPayloadAddInterface(payload, "oic.if.a");
OCRepPayloadSetUri(payload, ocf_mylight_light_peek_uri(light_id));
ocf_mylight_light_get_status(light_id, &value);
OCRepPayloadSetPropBool(payload, "value", value);
ocf_mylight_light_get_handle(light_id, &handle);
OCNotifyListOfObservers(handle, list, list_cnt, payload, OC_NA_QOS);
OCRepPayloadDestroy(payload);
}
static void *loop_notify(void *user_data _UNUSED_)
{
MSG("Notify Thread ready...");
while (1) {
pthread_mutex_lock(&lock_loop);
pthread_cond_wait(&cond, &lock_loop);
MSG("Do notify! (light=%d)", changed_light);
notify(changed_light);
pthread_mutex_unlock(&lock_loop);
}
return NULL;
}
int ocf_mylight_notify_add(unsigned int light_id, OCObservationId obs_id)
{
int i;
if (light_id > 1) {
DBG("invalid light_id(%u)", light_id);
return -1;
}
DBG("Light%u obs_id = %d", light_id, obs_id);
pthread_mutex_lock(&lock);
i = find_empty_slot(light_id);
if (i == -1) {
DBG("notify slot is full");
pthread_mutex_unlock(&lock);
return -1;
}
slots[light_id][i].active = true;
slots[light_id][i].id = obs_id;
pthread_mutex_unlock(&lock);
return 0;
}
int ocf_mylight_notify_del(unsigned int light_id, OCObservationId obs_id)
{
int i;
if (light_id > 1) {
DBG("invalid light_id(%u)", light_id);
return -1;
}
DBG("Light%u obs_id = %d", light_id, obs_id);
pthread_mutex_lock(&lock);
i = find_slot(light_id, obs_id);
if (i == -1) {
DBG("invalid obs_id(%d)", obs_id);
pthread_mutex_unlock(&lock);
return -1;
}
slots[light_id][i].active = false;
slots[light_id][i].id = 0;
pthread_mutex_unlock(&lock);
return 0;
}
int ocf_mylight_notify_emit(unsigned int light_id)
{
if (light_id > 1) {
DBG("invalid light_id(%u)", light_id);
return -1;
}
pthread_mutex_lock(&lock_loop);
MSG("request (light=%d)", light_id);
changed_light = light_id;
pthread_cond_signal(&cond);
pthread_mutex_unlock(&lock_loop);
return 0;
}
int ocf_mylight_notify_init()
{
int i;
for (i = 0; i < MAX_SLOT; i++) {
slots[0][i].active = false;
slots[0][i].id = 0;
slots[1][i].active = false;
slots[1][i].id = 0;
}
if (pthread_create(&_notify_tid, NULL, loop_notify, NULL) != 0) {
DBG("pthread_create failed. errno=%d", errno);
return -1;
}
return 0;
}
void ocf_mylight_notify_exit()
{
}