Skip to content

Commit 067d363

Browse files
authored
Merge pull request #7 from PlantFactory/6-fix-content-length-count
content length計算修正 close #6
2 parents 087fb78 + 3b10353 commit 067d363

File tree

2 files changed

+30
-19
lines changed

2 files changed

+30
-19
lines changed

PFFIAPUploadAgent.cpp

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ int FIAPUploadAgent::post(struct fiap_element* v, byte esize){
7575
int clen = 0; // content length
7676
struct fiap_element *v0;
7777
char count;
78+
unsigned int receive_loop_count;
7879
unsigned char c;
7980

8081
EthernetClient client;
@@ -88,12 +89,12 @@ int FIAPUploadAgent::post(struct fiap_element* v, byte esize){
8889
// TCP接続成功
8990
// コンテンツサイズ計算
9091
v0 = v;
91-
clen = 320; // sum of literal strings
92+
clen = 294; // sum of literal strings
9293
for (count = 0; count < esize; count++) {
9394
clen += strlen(fiap_id_prefix.c_str());
9495
clen += strlen(v0->cid);
95-
clen += strlen(v0->value); // + 3;
96-
clen += 75;
96+
clen += strlen(v0->value);
97+
clen += 69;
9798
v0++;
9899
} // Serial.print("len="); Serial.println(clen);
99100

@@ -122,19 +123,19 @@ int FIAPUploadAgent::post(struct fiap_element* v, byte esize){
122123

123124
// send HTTP body
124125
strcpy_P(sbuf,FIAPUploadAgent_Post_HTTPBODY01);
125-
client.println(sbuf); // "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
126+
client.print(sbuf); // "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
126127
strcpy_P(sbuf,FIAPUploadAgent_Post_HTTPBODY02);
127128
client.print(sbuf); // "<soapenv:Envelope xmlns:soapenv="
128129
strcpy_P(sbuf,FIAPUploadAgent_Post_HTTPBODY03);
129-
client.println(sbuf); // "\"http://schemas.xmlsoap.org/soap/envelope/\">"
130+
client.print(sbuf); // "\"http://schemas.xmlsoap.org/soap/envelope/\">"
130131
strcpy_P(sbuf,FIAPUploadAgent_Post_HTTPBODY04);
131-
client.println(sbuf); // "<soapenv:Body>"
132+
client.print(sbuf); // "<soapenv:Body>"
132133
strcpy_P(sbuf,FIAPUploadAgent_Post_HTTPBODY05);
133-
client.println(sbuf); // "<ns2:dataRQ xmlns:ns2=\"http://soap.fiap.org/\">"
134+
client.print(sbuf); // "<ns2:dataRQ xmlns:ns2=\"http://soap.fiap.org/\">"
134135
strcpy_P(sbuf,FIAPUploadAgent_Post_HTTPBODY06);
135-
client.println(sbuf); // "<transport xmlns=\"http://gutp.jp/fiap/2009/11/\">"
136+
client.print(sbuf); // "<transport xmlns=\"http://gutp.jp/fiap/2009/11/\">"
136137
strcpy_P(sbuf,FIAPUploadAgent_Post_HTTPBODY07);
137-
client.println(sbuf); // "<body>"
138+
client.print(sbuf); // "<body>"
138139

139140

140141
v0=v;
@@ -143,7 +144,7 @@ int FIAPUploadAgent::post(struct fiap_element* v, byte esize){
143144
client.print(sbuf); // "<point id=\""
144145
client.print(fiap_id_prefix.c_str()); client.print(v0->cid);
145146
strcpy_P(sbuf,FIAPUploadAgent_Post_HTTPBODY11);
146-
client.println(sbuf); // "\">"
147+
client.print(sbuf); // "\">"
147148

148149
strcpy_P(sbuf,FIAPUploadAgent_Post_HTTPBODY12);
149150
client.print(sbuf); // "<value time=\""
@@ -153,31 +154,32 @@ int FIAPUploadAgent::post(struct fiap_element* v, byte esize){
153154
client.print(v0->value);
154155

155156
strcpy_P(sbuf,FIAPUploadAgent_Post_HTTPBODY14);
156-
client.println(sbuf); // "</value>"
157+
client.print(sbuf); // "</value>"
157158

158159
strcpy_P(sbuf,FIAPUploadAgent_Post_HTTPBODY15);
159-
client.println(sbuf); // "</point>"
160+
client.print(sbuf); // "</point>"
160161
v0++;
161162
}
162163
strcpy_P(sbuf,FIAPUploadAgent_Post_HTTPBODY17);
163-
client.println(sbuf); // "</body>"
164+
client.print(sbuf); // "</body>"
164165
strcpy_P(sbuf,FIAPUploadAgent_Post_HTTPBODY18);
165-
client.println(sbuf); // "</transport>"
166+
client.print(sbuf); // "</transport>"
166167
strcpy_P(sbuf,FIAPUploadAgent_Post_HTTPBODY19);
167-
client.println(sbuf); // "</ns2:dataRQ>"
168+
client.print(sbuf); // "</ns2:dataRQ>"
168169
strcpy_P(sbuf,FIAPUploadAgent_Post_HTTPBODY20);
169-
client.println(sbuf); // "</soapenv:Body>"
170+
client.print(sbuf); // "</soapenv:Body>"
170171
strcpy_P(sbuf,FIAPUploadAgent_Post_HTTPBODY21);
171-
client.println(sbuf); // "</soapenv:Envelope>"
172-
client.println();
172+
client.print(sbuf); // "</soapenv:Envelope>"
173+
// client.println();
173174

174175
// parse HTTP response
175176
count = 0;
177+
receive_loop_count = 0;
176178
while (client.connected()) {
177179
// Serial.print("C");
178180
if (client.available()) {
179181
c = client.read(); // Serial.print(c);
180-
if (count == 1 && (c >= '0' && c <= '9')) { // parse HTTP response code
182+
if (count == 1 && isDigit(c)) { // parse HTTP response code
181183
rescode = rescode * 10 + (c - '0');
182184
continue;
183185
}
@@ -188,6 +190,12 @@ int FIAPUploadAgent::post(struct fiap_element* v, byte esize){
188190
break; // 応答ヘッダの2行目以降は見ない
189191
}
190192
}
193+
receive_loop_count++;
194+
if(receive_loop_count >= FIAP_RESPONSE_TIMEOUT) {
195+
client.stop();
196+
return(FIAP_UPLOAD_HTTPTOERR);
197+
}
198+
delayMicroseconds(1);
191199
}
192200
if (!client.connected()) { // unexpected disconnect
193201
client.stop();

PFFIAPUploadAgent.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@
66
#ifndef __FIAPUploadAgent__
77
#define __FIAPUploadAgent__
88

9+
#define FIAP_RESPONSE_TIMEOUT 500000 // 500ms
10+
911
// return code of post method
1012
#define FIAP_UPLOAD_OK 0 // Succeeded
1113
#define FIAP_UPLOAD_CONNFAIL 1 // Connection faild (Socket I/O error)
1214
#define FIAP_UPLOAD_DNSERR 2 // DNS error
1315
#define FIAP_UPLOAD_HTTPERR 3 // HTTP Server error (The response was not "200 OK")
1416
#define FIAP_UPLOAD_FIAPERR 4 // FIAP Server error
17+
#define FIAP_UPLOAD_HTTPTOERR 5 // HTTP Timeout error
1518

1619
#include <Arduino.h>
1720
#include "TimeLib.h"

0 commit comments

Comments
 (0)