source: rtos_arduino/trunk/arduino_lib/libraries/Ciao/src/lib/rest.cpp@ 175

Last change on this file since 175 was 175, checked in by ertl-honda, 8 years ago

ライブラリを Arduino IDE 1.7.9 にupdate

File size: 3.3 KB
Line 
1/**
2 * \file
3 * ESP8266 RESTful Bridge
4 * \author
5 * Tuan PM <tuanpm@live.com>
6 */
7#include "rest.h"
8REST::REST(ESP *e)
9{
10 esp = e;
11 remote_instance = 0;
12 timeout = DEFAULT_REST_TIMEOUT;
13}
14void REST::restCallback(void *resp)
15{
16 response = true;
17 res = resp;
18}
19
20boolean REST::begin(const char* host, uint16_t port, boolean security)
21{
22 uint8_t sec = 0;
23 if(security)
24 sec = 1;
25 restCb.attach(this, &REST::restCallback);
26
27 uint16_t crc = esp->request(CMD_REST_SETUP, (uint32_t)&restCb, 1, 3);
28 crc = esp->request(crc,(uint8_t*)host, strlen(host));
29 crc = esp->request(crc,(uint8_t*)&port, 2);
30 crc = esp->request(crc,(uint8_t*)&sec, 1);
31 esp->request(crc);
32
33 if(esp->waitReturn(timeout) && esp->return_value != 0){
34 remote_instance = esp->return_value;
35 return true;
36 }
37 return false;
38}
39
40boolean REST::begin(const char* host)
41{
42 return begin(host, 80, false);
43}
44void REST::request(const char* path, const char* method, const char* data, int len)
45{
46 if(remote_instance == 0)
47 return;
48 uint16_t crc;
49 if(len > 0)
50 crc = esp->request(CMD_REST_REQUEST, 0, 0, 5);
51 else
52 crc = esp->request(CMD_REST_REQUEST, 0, 0, 3);
53 crc = esp->request(crc,(uint8_t*)&remote_instance, 4);
54 crc = esp->request(crc,(uint8_t*)method, strlen(method));
55 crc = esp->request(crc,(uint8_t*)path, strlen(path));
56 if(len > 0){
57 crc = esp->request(crc,(uint8_t*)&len, 2);
58 crc = esp->request(crc,(uint8_t*)data, len);
59 }
60
61 esp->request(crc);
62}
63void REST::request(const char* path, const char* method, const char* data)
64{
65 request(path, method, data, strlen(data));
66}
67void REST::get(const char* path, const char* data)
68{
69 request(path, "GET", data);
70}
71void REST::get(const char* path)
72{
73 request(path, "GET", 0, 0);
74}
75void REST::post(const char* path, const char* data)
76{
77 request(path, "POST", data);
78}
79void REST::put(const char* path, const char* data)
80{
81 request(path, "PUT", data);
82}
83void REST::del(const char* path, const char* data)
84{
85 request(path, "DELETE", data);
86}
87
88/*
89void REST::setHeader(const char* value)
90{
91 uint8_t header_index = HEADER_GENERIC;
92 uint16_t crc = esp->request(CMD_REST_SETHEADER, 0, 0, 3);
93 crc = esp->request(crc,(uint8_t*)&remote_instance, 4);
94 crc = esp->request(crc,(uint8_t*)&header_index, 1);
95 crc = esp->request(crc,(uint8_t*)value, strlen(value));
96 esp->request(crc);
97}
98void REST::setContentType(const char* value)
99{
100 uint8_t header_index = HEADER_CONTENT_TYPE;
101 uint16_t crc = esp->request(CMD_REST_SETHEADER, 0, 0, 3);
102 crc = esp->request(crc,(uint8_t*)&remote_instance, 4);
103 crc = esp->request(crc,(uint8_t*)&header_index, 1);
104 crc = esp->request(crc,(uint8_t*)value, strlen(value));
105 esp->request(crc);
106}
107void REST::setUserAgent(const char* value)
108{
109 uint8_t header_index = HEADER_USER_AGENT;
110 uint16_t crc = esp->request(CMD_REST_SETHEADER, 0, 0, 3);
111 crc = esp->request(crc,(uint8_t*)&remote_instance, 4);
112 crc = esp->request(crc,(uint8_t*)&header_index, 1);
113 crc = esp->request(crc,(uint8_t*)value, strlen(value));
114 esp->request(crc);
115}
116void REST::setTimeout(uint32_t ms)
117{
118 timeout = ms;
119}
120*/
121
122uint16_t REST::getResponse(char* data, uint16_t maxLen)
123{
124 response = false;
125 uint32_t wait = millis();
126 while(response == false && (millis() - wait < timeout)) {
127 esp->process();
128 }
129 if(response){
130 RESPONSE resp(res);
131
132 resp.popArgs((uint8_t*)data, maxLen);
133 return esp->return_value;
134 }
135
136
137 return 0;
138}
Note: See TracBrowser for help on using the repository browser.