Skip to content

Commit 02f70f3

Browse files
committed
added initial esp8266 implementation
1 parent 46fa5d3 commit 02f70f3

9 files changed

+304
-16
lines changed

src/common/AWSClient2.cpp

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
static const char* CANONICAL_FORM_POST_LINE = "POST\n/\n\n";
1919
static const int CANONICAL_FORM_POST_LINE_LEN = 8;
2020
static const char* HTTPS_REQUEST_POST_LINE =
21-
"POST https://%s.%s.%s/ HTTP/1.1\n";
21+
"POST https://%s/ HTTP/1.1\n";
2222
static const int HTTPS_REQUEST_POST_LINE_LEN = 28;
23-
static const char* HTTP_REQUEST_POST_LINE = "POST http://%s.%s.%s/ HTTP/1.1\n";
23+
static const char* HTTP_REQUEST_POST_LINE = "POST http://%s/ HTTP/1.1\n";
2424
static const int HTTP_REQUEST_POST_LINE_LEN = 27;
2525
static const char* TO_SIGN_TEMPLATE =
2626
"AWS4-HMAC-SHA256\n%sT%sZ\n%s/%s/%s/aws4_request\n%s";
@@ -62,6 +62,11 @@ void AWSClient2::setAWSEndpoint(const char * awsEndpoint) {
6262
this->awsEndpoint = new char[len]();
6363
strcpy(this->awsEndpoint, awsEndpoint);
6464
}
65+
void AWSClient2::setAWSDomain(const char * awsDomain) {
66+
int len = strlen(awsDomain) + 1;
67+
this->awsDomain = new char[len]();
68+
strcpy(this->awsDomain, awsDomain);
69+
}
6570
void AWSClient2::setAWSSecretKey(const char * awsSecKey) {
6671
int len = strlen(awsSecKey) + 1;
6772
this->awsSecKey = new char[len]();
@@ -120,6 +125,16 @@ void AWSClient2::initSignedHeaders() {
120125
headerLens[headersCreated++] = len;
121126
}
122127

128+
char* AWSClient2::createHostString(void) {
129+
if(awsDomain[0] != '\0') {
130+
return awsDomain;
131+
} else {
132+
char* host = new char[200]();
133+
sprintf(host, "%s.%s.%s", awsService, awsRegion, awsEndpoint);
134+
return host;
135+
}
136+
}
137+
123138
char* AWSClient2::createStringToSign(void) {
124139
SHA256* sha256 = new SHA256();
125140
char* hashed;
@@ -251,8 +266,8 @@ char* AWSClient2::headersToRequest() {
251266
httpS ? HTTPS_REQUEST_POST_LINE : HTTP_REQUEST_POST_LINE;
252267

253268
/* Calculate length of httpRequest string. */
254-
int httpRequestLen = postLineLen + strlen(awsService) + strlen(awsRegion)
255-
+ strlen(awsEndpoint);
269+
char* host = createHostString();
270+
int httpRequestLen = postLineLen + strlen(host);
256271
for (int i = 0; i < headersCreated; i++) {
257272
/* +1 for newline. */
258273
httpRequestLen += *(headerLens + i) + 1;
@@ -263,8 +278,7 @@ char* AWSClient2::headersToRequest() {
263278
/* Create and write to the httpRequest string. */
264279
char* httpRequest = new char[httpRequestLen + 1]();
265280
int httpRequestWritten = 0;
266-
httpRequestWritten += sprintf(httpRequest + httpRequestWritten, postLine,
267-
awsService, awsRegion, awsEndpoint);
281+
httpRequestWritten += sprintf(httpRequest + httpRequestWritten, postLine, host);
268282
for (int i = 0; i < headersCreated; i++) {
269283
httpRequestWritten += sprintf(httpRequest + httpRequestWritten, "%s\n",
270284
*(headers + i));
@@ -280,7 +294,7 @@ char* AWSClient2::createRequest(MinimalString &reqPayload) {
280294
if (awsRegion == 0 || awsEndpoint == 0 || awsSecKey == 0 || awsKeyID == 0
281295
|| httpClient == 0 || dateTimeProvider == 0)
282296
return 0;
283-
297+
284298
createRequestInit(reqPayload);
285299
char* request = headersToRequest();
286300
createRequestCleanup();

src/common/AWSClient2.h

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@
1212
#include "AWSFoundationalTypes.h"
1313

1414
/* Total number of headers. */
15-
static const int HEADER_COUNT = 7;
15+
static const int HEADER_COUNT2 = 7;
1616
/* Size of the awsDate string. */
17-
static const int AWS_DATE_LEN = 8;
17+
static const int AWS_DATE_LEN2 = 8;
1818
/* Size of the awsTime string. */
19-
static const int AWS_TIME_LEN = 6;
19+
static const int AWS_TIME_LEN2 = 6;
2020
/* Size of sha hashes and signatures in hexidecimal. */
21-
static const int HASH_HEX_LEN = 64;
21+
static const int HASH_HEX_LEN2 = 64;
2222

2323
/* Base class for an AWS Service Client. Creates http and https request in raw
2424
* http format or as a curl command. */
@@ -27,20 +27,22 @@ class AWSClient2 {
2727
char* awsRegion;
2828
/* Endpoint, eg. "amazonaws.com" in "kinesis.us-east-1.amazonaws.com". */
2929
char* awsEndpoint;
30+
/* Subdomain, eg. "A2MBBEONHC7LUH" in "A2MBBEONHC9LUG.iot.us-east-1.amazonaws.com". */
31+
char* awsDomain;
3032
/* The user's AWS Secret Key for accessing the AWS Resource. */
3133
char* awsSecKey;
3234
/* The user's AWS Access Key ID for accessing the AWS Resource. */
3335
char* awsKeyID;
3436
/* GMT date in yyyyMMdd format. */
35-
char awsDate[AWS_DATE_LEN + 1];
37+
char awsDate[AWS_DATE_LEN2 + 1];
3638
/* GMT time in HHmmss format. */
37-
char awsTime[AWS_TIME_LEN + 1];
39+
char awsTime[AWS_TIME_LEN2 + 1];
3840
/* Number of headers created. */
3941
int headersCreated;
4042
/* Array of the created http headers. */
41-
char* headers[HEADER_COUNT];
43+
char* headers[HEADER_COUNT2];
4244
/* Array of string lengths of the headers in the "headers" array. */
43-
int headerLens[HEADER_COUNT];
45+
int headerLens[HEADER_COUNT2];
4446
/* The payload of the httprequest to be created */
4547
MinimalString payload;
4648

@@ -76,6 +78,8 @@ class AWSClient2 {
7678
const char* awsService;
7779
/* Content type of payload, eg. "application/x-amz-json-1.1". */
7880
const char* contentType;
81+
// /* Generates the host based on subdomain, service, etc */
82+
// char* createHostString(void);
7983
/* Creates a raw http request, given the payload and current GMT date in
8084
* yyyyMMddHHmmss format. Should be exposed to user by extending class.
8185
* Returns 0 if client is unititialized. */
@@ -88,8 +92,11 @@ class AWSClient2 {
8892
public:
8993
/* Setters for values used by createRequest and createCurlRequest. Must
9094
* be set or create[Curl]Request will return null. */
95+
/* Generates the host based on subdomain, service, etc */
96+
char* createHostString(void);
9197
void setAWSRegion(const char * awsRegion);
9298
void setAWSEndpoint(const char * awsEndpoint);
99+
void setAWSDomain(const char * awsDomain);
93100
void setAWSSecretKey(const char * awsSecKey);
94101
void setAWSKeyID(const char * awsKeyID);
95102
void setHttpClient(IHttpClient* httpClient);

src/common/AmazonIOTClient.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#include "AmazonIOTClient.h"
2+
#include "AWSFoundationalTypes.h"
3+
#include <stdlib.h>
4+
#include "Utils.h"
5+
6+
static const char* SERVICE = "iotdata";
7+
static const char* FORM_TYPE = "application/json";
8+
static const char* PAYLOAD_TEMPLATE = "%s";
9+
// int PAYLOAD_TEMPLATE_LENGTH = 2;
10+
int IOT_EXTRACTED_TIMESTAMP_BUFFER_LENGTH = 17;
11+
int IOT_FORMATTED_TIMESTAMP_BUFFER_LENGTH = 15;
12+
13+
AmazonIOTClient::AmazonIOTClient() : AWSClient2() {
14+
awsService = SERVICE;
15+
httpS = true;
16+
}
17+
18+
char* AmazonIOTClient::update_shadow(MinimalString url, MinimalString shadow, ActionError& actionError) {
19+
20+
actionError = NONE_ACTIONERROR;
21+
contentType = FORM_TYPE;
22+
23+
// char* request = createRequest(shadow, url);
24+
// publishOutput.setResponse(request);
25+
26+
// char* request = createRequest(url, shadow);
27+
char* request = createRequest(shadow);
28+
return request;
29+
30+
// char* response = sendData(request);
31+
// delete[] request;
32+
//
33+
// if (response == NULL) {
34+
// actionError = CONNECTION_ACTIONERROR;
35+
// return 999;
36+
// }
37+
//
38+
// int httpStatusCode = findHttpStatusCode(response);
39+
//
40+
// if (httpStatusCode == 200) {
41+
// return 1;
42+
// }
43+
//
44+
// if (httpStatusCode == 403) {
45+
// char* ts = strstr(response, "earlier than ");
46+
// int pos = ts - response;
47+
//
48+
// char* newts = new char[IOT_EXTRACTED_TIMESTAMP_BUFFER_LENGTH]();
49+
// strncpy(newts, response + pos + 31, IOT_EXTRACTED_TIMESTAMP_BUFFER_LENGTH - 1);
50+
// newts[16] = '\0';
51+
//
52+
// char* time = new char[IOT_FORMATTED_TIMESTAMP_BUFFER_LENGTH]();
53+
// sprintf(time, "%.8s%.6s", newts, newts + 9);
54+
// dateTimeProvider->sync(time);
55+
// return 0;
56+
// }
57+
//
58+
// return httpStatusCode;
59+
}

src/common/AmazonIOTClient.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#ifndef AMAZONIOTCLIENT_H_
2+
#define AMAZONIOTCLIENT_H_
3+
#include "AWSClient2.h"
4+
5+
6+
// class Shadow {
7+
// MinimalString shadow;
8+
// void reset();
9+
// public:
10+
// void setShadow(shadow) const;
11+
// };
12+
13+
14+
15+
class AmazonIOTClient : public AWSClient2 {
16+
public:
17+
AmazonIOTClient();
18+
19+
char* update_shadow(MinimalString url, MinimalString shadow, ActionError& actionError);
20+
};
21+
22+
#endif /* AMAZONSNSCLIENT_H_ */

src/common/AmazonKinesisClient.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2354,4 +2354,3 @@ KinesisErrorCheckingOnlyOutput AmazonKinesisClient::splitShard(SplitShardInput s
23542354
}
23552355
return kinesisErrorCheckingOnlyOutput;
23562356
}
2357-
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/Users/svdgraaf/Documents/Arduino/Libraries/aws-sdk-arduino/src/esp8266/ESP8266AWSImplementations.h
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/Users/svdgraaf/Documents/Arduino/Libraries/aws-sdk-arduino/src/esp8266/ESP8266AWSImplentations.cpp
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#ifndef AWSESP2866IMPLEMENTATIONS_H_
2+
#define AWSESP2866IMPLEMENTATIONS_H_
3+
#include "DeviceIndependentInterfaces.h"
4+
/* application.h is Esp8266's standard library. Define TCPClient. */
5+
//#include <application.h>
6+
#include <ESP8266WiFi.h>
7+
8+
/* HttpClient implementation to be used on the Esp8266 Core device. */
9+
class Esp8266HttpClient: public IHttpClient {
10+
WiFiClientSecure client;
11+
//TCPClient client;
12+
public:
13+
Esp8266HttpClient();
14+
/* Send http request and return the response. */
15+
char* send(const char *request, const char* serverUrl, int port);
16+
/* Returns false. Client uses raw http/https. */
17+
bool usesCurl(void);
18+
};
19+
20+
class Esp8266DateTimeProvider: public IDateTimeProvider {
21+
/* The time as a cstring in yyyyMMddHHmmss format. Is written to within and
22+
* returned by getDateTime(). */
23+
WiFiClient client2;
24+
//char dateTime[15];
25+
public:
26+
char dateTime[15];
27+
Esp8266DateTimeProvider();
28+
/* Retrieve the current GMT date and time in yyyyMMddHHmmss format. */
29+
const char* getDateTime(void);
30+
/* Returns false because Esp8266 has it's own mechanism for syncing that does
31+
* not require an argument. */
32+
bool syncTakesArg(void);
33+
/* Synchronizes Esp8266's date and time with Esp8266's servers. The dateTime
34+
* argument is ignored. */
35+
void sync(const char* dateTime);
36+
};
37+
38+
#endif /* AWSESP2866IMPLEMENTATIONS_H_ */

0 commit comments

Comments
 (0)