[136] | 1 | /*
|
---|
| 2 | ###############################################################################
|
---|
| 3 | #
|
---|
| 4 | # Temboo Arduino library
|
---|
| 5 | #
|
---|
| 6 | # Copyright 2014, Temboo Inc.
|
---|
| 7 | #
|
---|
| 8 | # Licensed under the Apache License, Version 2.0 (the "License");
|
---|
| 9 | # you may not use this file except in compliance with the License.
|
---|
| 10 | # You may obtain a copy of the License at
|
---|
| 11 | #
|
---|
| 12 | # http://www.apache.org/licenses/LICENSE-2.0
|
---|
| 13 | #
|
---|
| 14 | # Unless required by applicable law or agreed to in writing,
|
---|
| 15 | # software distributed under the License is distributed on an
|
---|
| 16 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
---|
| 17 | # either express or implied. See the License for the specific
|
---|
| 18 | # language governing permissions and limitations under the License.
|
---|
| 19 | #
|
---|
| 20 | ###############################################################################
|
---|
| 21 | */
|
---|
| 22 |
|
---|
| 23 | #ifndef TEMBOOSESSIONCLASS_H_
|
---|
| 24 | #define TEMBOOSESSIONCLASS_H_
|
---|
| 25 |
|
---|
| 26 | #include <stdint.h>
|
---|
| 27 | #include <Arduino.h>
|
---|
| 28 | #include <IPAddress.h>
|
---|
| 29 | #include <Client.h>
|
---|
| 30 | #include "TembooGlobal.h"
|
---|
| 31 |
|
---|
| 32 | #ifndef TEMBOO_SEND_QUEUE_SIZE
|
---|
| 33 |
|
---|
| 34 | // Some network interfaces (i.e. ethernet or WiFi shields) can only accept
|
---|
| 35 | // a limited amount of data. If you try to send more than the limit, the excess
|
---|
| 36 | // is just lost. However, sending one character at a time is very inefficient.
|
---|
| 37 | // To deal with this situation, we queue up TEMBOO_SEND_QUEUE_SIZE bytes to send
|
---|
| 38 | // to the network device at one time. This is a compromise between RAM usage
|
---|
| 39 | // and performance.
|
---|
| 40 | #define TEMBOO_SEND_QUEUE_SIZE (32)
|
---|
| 41 | #endif
|
---|
| 42 |
|
---|
| 43 | class ChoreoInputSet;
|
---|
| 44 | class ChoreoOutputSet;
|
---|
| 45 | class ChoreoPreset;
|
---|
| 46 | class DataFormatter;
|
---|
| 47 |
|
---|
| 48 | class TembooSession {
|
---|
| 49 | public:
|
---|
| 50 |
|
---|
| 51 | //TembooSession constructor
|
---|
| 52 | //client: REQUIRED TCP/IP client object. Usually either an EthernetClient or a WiFiClient
|
---|
| 53 | //IPAddress: OPTIONAL IP address of the server to connect to. Usually only used for testing.
|
---|
| 54 | //port: OPTIONAL port number to use with the IPAddress. Usually only used for testing.
|
---|
| 55 | TembooSession(Client& client, IPAddress serverAddr=INADDR_NONE, uint16_t port=80);
|
---|
| 56 |
|
---|
| 57 | //executeChoreo sends a choreo execution request to the Temboo system.
|
---|
| 58 | // Does not wait for a response (that's a job for whoever owns the Client.)
|
---|
| 59 | //accountName: the name of the user's account at Temboo.
|
---|
| 60 | //appKeyName: the name of an application key in the user's account to use
|
---|
| 61 | // for this execution (analogous to a user name).
|
---|
| 62 | //appKeyValue: the value of the application key named in appKeyName.
|
---|
| 63 | // Used to authenticate the user (analogous to a password)
|
---|
| 64 | //path: The full path to the choreo to be executed (relative to the root of the
|
---|
| 65 | // user's account.)
|
---|
| 66 | //inputSet: the set of inputs needed by the choreo.
|
---|
| 67 | // May be an empty ChoreoInputSet.
|
---|
| 68 | //outputSet: the set of output filters to be applied to the choreo results.
|
---|
| 69 | // May be an empty ChoreoOutputSet
|
---|
| 70 | //preset: the ChoreoPreset to be used with the choreo execution.
|
---|
| 71 | // May be an empty ChoreoPreset.
|
---|
| 72 | int executeChoreo(const char* accountName,
|
---|
| 73 | const char* appKeyName,
|
---|
| 74 | const char* appKeyValue,
|
---|
| 75 | const char* path,
|
---|
| 76 | const ChoreoInputSet& inputSet,
|
---|
| 77 | const ChoreoOutputSet& outputSet,
|
---|
| 78 | const ChoreoPreset& preset);
|
---|
| 79 |
|
---|
| 80 | // setTime sets the current time in Unix timestamp format. Needed for execution request authentication.
|
---|
| 81 | // NOTE: This method is usually called by TembooChoreo.run() with the current time returned by
|
---|
| 82 | // an error response from the Temboo system, thus automatically setting the time. However, it
|
---|
| 83 | // MAY be called from user code if the particular board has a way of determining the current
|
---|
| 84 | // time in the proper format.
|
---|
| 85 | // currentTime: the number of seconds since 1970-01-01 00:00:00 UTC.
|
---|
| 86 | static void setTime(unsigned long currentTime);
|
---|
| 87 |
|
---|
| 88 | //getTime returns the current time in Unix timestamp format (seconds since 1970-01-01 00:00:00 UTC).
|
---|
| 89 | // Only valid after setTime has been called.
|
---|
| 90 | static unsigned long getTime();
|
---|
| 91 |
|
---|
| 92 | private:
|
---|
| 93 | static unsigned long s_timeOffset;
|
---|
| 94 |
|
---|
| 95 | IPAddress m_addr;
|
---|
| 96 | uint16_t m_port;
|
---|
| 97 |
|
---|
| 98 | Client& m_client;
|
---|
| 99 | char m_sendQueue[TEMBOO_SEND_QUEUE_SIZE];
|
---|
| 100 | size_t m_sendQueueDepth;
|
---|
| 101 |
|
---|
| 102 | // calculate the authentication code value of the formatted request body
|
---|
| 103 | // using the salted application key value as the key.
|
---|
| 104 | // Returns the number of characters processed (i.e. the length of the request body)
|
---|
| 105 | uint16_t getAuth(DataFormatter& fmt, const char* appKeyValue, const char* salt, char* hexAuth) const;
|
---|
| 106 |
|
---|
| 107 |
|
---|
| 108 | // queue an entire nul-terminated char array
|
---|
| 109 | // from RAM followed by a newline.
|
---|
| 110 | void qsendln(const char* str);
|
---|
| 111 |
|
---|
| 112 | // queue an entire nul-terminated char array
|
---|
| 113 | // from flash memory (PROGMEM) one byte at a time,
|
---|
| 114 | // followed by a newline.
|
---|
| 115 | void qsendlnProgmem(const char* str);
|
---|
| 116 |
|
---|
| 117 | // queue an entire nul-terminated char array
|
---|
| 118 | // from RAM one byte at a time.
|
---|
| 119 | void qsend(const char*);
|
---|
| 120 |
|
---|
| 121 | // queue an entire nul-terminated char array
|
---|
| 122 | // from flash memory (PROGMEM) one byte at a time.
|
---|
| 123 | void qsendProgmem(const char*);
|
---|
| 124 |
|
---|
| 125 | // queue a single character to be sent when the queue is full.
|
---|
| 126 | void qsend(char);
|
---|
| 127 |
|
---|
| 128 | // send the current contents of the send queue to the client.
|
---|
| 129 | void qflush();
|
---|
| 130 |
|
---|
| 131 | };
|
---|
| 132 |
|
---|
| 133 | #endif
|
---|
| 134 |
|
---|