-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathweb_thing_adapter.c
288 lines (250 loc) · 7.83 KB
/
web_thing_adapter.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
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
Copyright (c) 2019 Akshay Vernekar
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "web_thing_adapter.h"
#include <esp_wifi.h>
#include <esp_event_loop.h>
#include <esp_log.h>
#include <esp_system.h>
#include <nvs_flash.h>
#include <sys/param.h>
#include <mdns.h>
#include <esp_http_server.h>
static Thing* gThing=NULL;
static const char* MDNS_INSTANCE_NAME = "webthing";
static const char* REST_TAG ="web_thing_adapter";
static void initialise_mdns(char* instance_name)
{
if(gThing == NULL)
return;
mdns_init();
mdns_hostname_set(MDNS_INSTANCE_NAME);
mdns_instance_name_set(instance_name);
mdns_txt_item_t serviceTxtData[] = {
{"path", "/"}
};
ESP_ERROR_CHECK(mdns_service_add("webthing", "_webthing", "_tcp", CONFIG_WEB_THING_PORT, serviceTxtData,
sizeof(serviceTxtData) / sizeof(serviceTxtData[0])));
}
esp_err_t handleGetThing(httpd_req_t *req)
{
Thing* device = NULL;
cJSON* responseJson = NULL;
if(req->user_ctx != NULL)
{
device = (Thing*)req->user_ctx;
}
else
{
ESP_LOGI(REST_TAG,"user_ctx is null");
}
if(device)
{
cJSON* responseJson = cJSON_CreateObject();
serializeDevice(device,responseJson);
const char* strRes = cJSON_Print(responseJson);
httpd_resp_set_type(req, "application/json");
httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
httpd_resp_send(req, strRes, strlen(strRes));
//cleanup
free(strRes);
cJSON_Delete(responseJson);
}
return ESP_OK;
}
esp_err_t handleThingGetItem(httpd_req_t *req)
{
ThingProperty* property = NULL;
cJSON* responseJson = NULL;
if(req->user_ctx != NULL)
{
property = (ThingProperty*)req->user_ctx;
}
if(property)
{
responseJson = cJSON_CreateObject();
serialise_property_item(property,responseJson);
const char* strRes = cJSON_Print(responseJson);
httpd_resp_set_type(req, "application/json");
httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
httpd_resp_send(req, strRes, strlen(strRes));
//cleanup
free(strRes);
cJSON_Delete(responseJson);
}
return ESP_OK;
}
esp_err_t handleThingPutItem(httpd_req_t *req)
{
ThingProperty* property = NULL;
cJSON* responseJson = NULL;
esp_err_t resCode = ESP_OK;
if(req->user_ctx != NULL)
{
property = (ThingProperty*)req->user_ctx;
}
if(property)
{
char* content = malloc(sizeof(char)*(req->content_len+1));
cJSON *newvalue = NULL;
int ret = httpd_req_recv(req, content, req->content_len);
content[req->content_len] = '\0';
if (ret <= 0)
{ /* 0 return value indicates connection closed */
/* Check if timeout occurred */
if (ret == HTTPD_SOCK_ERR_TIMEOUT) {
/* In case of timeout one can choose to retry calling
* httpd_req_recv(), but to keep it simple, here we
* respond with an HTTP 408 (Request Timeout) error */
httpd_resp_send_408(req);
}
/* In case of error, returning ESP_FAIL will
* ensure that the underlying socket is closed */
resCode = ESP_FAIL;
goto cleanup;
}
newvalue = cJSON_Parse((char *)content);
if(newvalue == NULL)
{
ESP_LOGI(REST_TAG,"handle_directives::Parsing failed");
resCode = ESP_FAIL;
goto cleanup;
}
if(update_thing_property(property,newvalue))
{
httpd_resp_set_type(req, "application/json");
httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
httpd_resp_send(req, content, strlen(content));
}
cleanup:
if(newvalue)
cJSON_Delete(newvalue);
if(content)
free(content);
}
return resCode;
}
esp_err_t handleThingGetAllProperties(httpd_req_t *req)
{
ESP_LOGI(REST_TAG,"handleThingGetAllProperties hit");
Thing* thing = NULL;
cJSON* responseJson = NULL;
if(req->user_ctx != NULL)
{
thing = (Thing*)req->user_ctx;
}
else
{
return ESP_FAIL;
}
ThingProperty* property = thing->property;
responseJson = cJSON_CreateObject();
while (property != NULL)
{
serialise_property_item(property,responseJson);
property = (ThingProperty*)property->next;
}
const char* strRes = cJSON_Print(responseJson);
httpd_resp_set_type(req, "application/json");
httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
httpd_resp_send(req, strRes, strlen(strRes));
//cleanup
free(strRes);
cJSON_Delete(responseJson);
return ESP_OK;
}
void startRestAPIServer(Thing* thing)
{
httpd_handle_t server = NULL;
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
/*
3 handles for "/","/things" and "/things/properties"
2 handlers for each property one GET and one PUT
We are considering max 3 properties , so MAX_URI_HANDLERS = 3+ 2*<MAX_NUMBER_OF_PROPERTIES>
you can increase this depending upon your usecase
*/
config.max_uri_handlers = CONFIG_MAX_PROPERTY*2 + 3;
config.server_port = CONFIG_WEB_THING_PORT;
ESP_LOGI(REST_TAG, "Starting webthing Server");
if(httpd_start(&server, &config) != ESP_OK)
{
ESP_LOGI(REST_TAG,"server start failed!");
return;
}
/* URI handler for fetching system info */
httpd_uri_t system_info_get_uri = {
.uri = "/",
.method = HTTP_GET,
.handler = handleGetThing,
.user_ctx = thing
};
httpd_register_uri_handler(server, &system_info_get_uri);
char* thingDescriptionUrl = getThingDescriptionUrl(thing);
system_info_get_uri.uri = thingDescriptionUrl;
system_info_get_uri.method = HTTP_GET;
system_info_get_uri.handler = handleGetThing;
system_info_get_uri.user_ctx = thing;
httpd_register_uri_handler(server, &system_info_get_uri);
ThingProperty* property = thing->property;
httpd_uri_t request_handle;
while (property != NULL)
{
char* propUri = getPropertyEndpointUrl(thing,property);
ESP_LOGI(REST_TAG,"url:%s",propUri);
request_handle.uri = propUri;
request_handle.method = HTTP_GET;
request_handle.handler = handleThingGetItem;
request_handle.user_ctx = property;
httpd_register_uri_handler(server, &request_handle);
if(!property->info.readOnly)
{
request_handle.uri = propUri;
request_handle.method = HTTP_PUT;
request_handle.handler = handleThingPutItem;
request_handle.user_ctx = property;
httpd_register_uri_handler(server, &request_handle);
}
if(propUri)
{
free(propUri);
propUri = NULL;
}
property = (ThingProperty*)property->next;
}
// request_handle.uri ="/things/properties";
char* thingPropertiesUrl = (char*) malloc(sizeof(char)*(strlen(thingDescriptionUrl)+strlen("/properties")+1));
sprintf(thingPropertiesUrl,"%s/properties",thingDescriptionUrl);
request_handle.uri = thingPropertiesUrl;
request_handle.method = HTTP_GET;
request_handle.handler = handleThingGetAllProperties;
request_handle.user_ctx = thing;
httpd_register_uri_handler(server, &request_handle);
if(thingPropertiesUrl)
free(thingPropertiesUrl);
if(thingDescriptionUrl)
free(thingDescriptionUrl);
}
void initAdapter(Thing* thing)
{
gThing = thing;
initialise_mdns(gThing->title);
}
void startAdapter()
{
startRestAPIServer(gThing);
}