-
Notifications
You must be signed in to change notification settings - Fork 2
/
ocf_mylight_main.c
170 lines (136 loc) · 3.83 KB
/
ocf_mylight_main.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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include "ocf_mylight.h"
static int gQuitFlag = 0;
static int server_cb(int argc _UNUSED_, char *argv[] _UNUSED_)
{
MSG("IoTivity Demo - IOTIVITY version is %s", IOTIVITY_VERSION);
MSG("OCF My Light Server is starting...");
#ifndef CONFIG_ARCH_BOARD_ARTIK053
ocf_mylight_security_init();
#endif
if (OCInit(NULL, 0, OC_SERVER) != OC_STACK_OK) {
DBG("OCStack init error");
return 0;
}
MSG("Supported Endpoint Transport Protocol Suites: 0x%X",
OCGetSupportedEndpointTpsFlags());
ocf_mylight_playform_init();
ocf_mylight_device_init();
ocf_mylight_configuration_init();
ocf_mylight_maintenance_init();
ocf_mylight_light_init();
ocf_mylight_notify_init();
#ifndef CONFIG_ARCH_BOARD_ARTIK053
ocf_mylight_userinput_init();
#endif
MSG("Entering ocserver main loop...");
while (!gQuitFlag) {
usleep(10000);
if (OCProcess() != OC_STACK_OK) {
DBG("OCStack process error");
return 0;
}
}
ocf_mylight_light_exit();
MSG("Exiting ocserver main loop...");
if (OCStop() != OC_STACK_OK)
DBG("OCStack process error");
return 0;
}
static void _timestamp()
{
struct tm stamp;
struct timespec tspec;
char timebuf[32];
clock_gettime(CLOCK_REALTIME, &tspec);
localtime_r(&tspec.tv_sec, &stamp);
strftime(timebuf, sizeof(timebuf), "%m-%d %H:%M:%S", &stamp);
printf("%s.%03ld", timebuf, tspec.tv_nsec / 1000000);
}
OCEntityHandlerResult ocf_mylight_handler(OCEntityHandlerFlag flag,
OCEntityHandlerRequest *req, void *user_data)
{
struct ocf_ops *ops = user_data;
OCEntityHandlerResult ret = OC_EH_METHOD_NOT_ALLOWED;
printf("\n");
_timestamp();
MSG("\n<New request>");
ocf_mylight_verbose_request(flag, req);
if (!ops)
return ret;
if (flag & OC_REQUEST_FLAG) {
if (OC_REST_GET == req->method && ops->get)
ret = ops->get(flag, req, user_data);
else if (OC_REST_PUT == req->method && ops->put)
ret = ops->put(flag, req, user_data);
else if (OC_REST_POST == req->method && ops->post)
ret = ops->post(flag, req, user_data);
else if (OC_REST_DELETE == req->method && ops->del)
ret = ops->del(flag, req, user_data);
}
if (flag & OC_OBSERVE_FLAG) {
if (OC_OBSERVE_REGISTER == req->obsInfo.action
&& ops->register_observe)
ret = ops->register_observe(flag, req, user_data);
else if (OC_OBSERVE_DEREGISTER == req->obsInfo.action
&& ops->deregister_observe)
ret = ops->deregister_observe(flag, req, user_data);
}
MSG(" return code: %d", ret);
MSG("</New request>\n");
return ret;
}
OCEntityHandlerResult ocf_mylight_dev_handler(OCEntityHandlerFlag flag,
OCEntityHandlerRequest *req, char *uri, void *user_data)
{
struct ocf_dev_ops *ops = user_data;
OCEntityHandlerResult ret = OC_EH_METHOD_NOT_ALLOWED;
printf("\n");
_timestamp();
MSG("\n<New request for device>");
ocf_mylight_verbose_request(flag, req);
MSG(" - uri: %s", uri);
if (!ops)
return ret;
if (flag & OC_REQUEST_FLAG) {
if (OC_REST_GET == req->method && ops->get)
ret = ops->get(flag, req, uri, user_data);
else if (OC_REST_PUT == req->method && ops->put)
ret = ops->put(flag, req, uri, user_data);
else if (OC_REST_DELETE == req->method && ops->del)
ret = ops->del(flag, req, uri, user_data);
}
MSG(" return code: %d", ret);
MSG("</New request for device>\n");
return ret;
}
int ocf_mylight_get_quit_flag()
{
return gQuitFlag;
}
#ifndef CONFIG_ARCH_BOARD_ARTIK053
int main(int argc, char *argv[])
{
server_cb(argc, argv);
return 0;
}
#else
#define IOTIVITY_TEST_STACKSIZE 4096
#define IOTIVITY_TEST_PRI 100
#define IOTIVITY_TEST_SCHED_POLICIY SCHED_RR
#ifdef CONFIG_BUILD_KERNEL
int main(int argc, char *argv[])
#else
int ocf_mylight_main(int argc, char *argv[])
#endif
{
task_create("ocf_mylight_server", IOTIVITY_TEST_PRI,
IOTIVITY_TEST_STACKSIZE, server_cb,
(FAR char * const *)NULL);
return 0;
}
#endif