Skip to content

Commit f0171ba

Browse files
committedFeb 16, 2017
2016.02.05.
0 parents  commit f0171ba

25 files changed

+3787
-0
lines changed
 

‎README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
## Arduino Library for PHPoC
2+
This library is to use [PHPoC (WiFi) Shield for Arduino](http://www.phpoc.com/phpoc_shield_for_arduino.php).
3+
4+
Available Examples
5+
----------------------------
6+
* ChatServer
7+
* DateTime
8+
* EmailClient
9+
* SSHServer
10+
* SSLServer
11+
* TelnetServer
12+
* WebClient
13+
* WebRemotePush
14+
* WebRemoteSlide
15+
* WebSSLClient
16+
17+
References
18+
----------------------------
19+
* [PHPoC (WiFi) Shield for Arduino Library Reference](http://www.phpoc.com/support/manual/phpoc_shield_for_arduino_library_reference/)
20+
21+
Arduino Library 05

‎examples/ChatServer/ChatServer.ino

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include <SPI.h>
2+
#include <Phpoc.h>
3+
4+
PhpocServer server(23);
5+
boolean alreadyConnected = false; // whether or not the client was connected previously
6+
7+
void setup() {
8+
Serial.begin(9600);
9+
while(!Serial)
10+
;
11+
12+
Phpoc.begin(PF_LOG_SPI | PF_LOG_NET);
13+
//Phpoc.begin();
14+
15+
server.begin();
16+
17+
Serial.print("Chat server address : ");
18+
Serial.println(Phpoc.localIP());
19+
}
20+
21+
void loop() {
22+
// wait for a new client:
23+
PhpocClient client = server.available();
24+
25+
// when the client sends the first byte, say hello:
26+
if (client) {
27+
if (!alreadyConnected) {
28+
// clear out the transmission buffer:
29+
client.flush();
30+
Serial.println("We have a new client");
31+
client.println("Hello, client!");
32+
alreadyConnected = true;
33+
}
34+
35+
if (client.available() > 0) {
36+
// read the bytes incoming from the client:
37+
char thisChar = client.read();
38+
// echo the bytes back to the client:
39+
server.write(thisChar);
40+
// echo the bytes to the server as well:
41+
Serial.write(thisChar);
42+
}
43+
}
44+
}

‎examples/DateTime/DateTime.ino

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include <SPI.h>
2+
#include <Phpoc.h>
3+
4+
PhpocDateTime datetime;
5+
6+
void setup() {
7+
Serial.begin(9600);
8+
while(!Serial)
9+
;
10+
11+
Phpoc.begin();
12+
13+
Serial.println("Phpoc Time test");
14+
15+
Serial.print(datetime.year());
16+
Serial.print('-');
17+
Serial.print(datetime.month());
18+
Serial.print('-');
19+
Serial.print(datetime.day());
20+
Serial.print(' ');
21+
Serial.print(datetime.dayofWeek());
22+
Serial.print(':');
23+
Serial.print(datetime.hour());
24+
Serial.print(':');
25+
Serial.print(datetime.minute());
26+
Serial.print(':');
27+
Serial.print(datetime.second());
28+
Serial.println();
29+
30+
datetime.date("Y-m-d H:i:s");
31+
}
32+
33+
void loop() {
34+
Serial.println(datetime.date());
35+
delay(1000);
36+
}

‎examples/EmailClient/EmailClient.ino

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include "SPI.h"
2+
#include "Phpoc.h"
3+
4+
PhpocEmail email;
5+
6+
void setup() {
7+
Serial.begin(9600);
8+
while(!Serial)
9+
;
10+
11+
Phpoc.begin(PF_LOG_SPI | PF_LOG_NET | PF_LOG_APP);
12+
//Phpoc.begin();
13+
14+
Serial.println("Email Client Test");
15+
16+
// setup From/To/Subject
17+
email.setFrom("from_email_address", "from_user_name");
18+
email.setTo("to_email_address", "to_user_name");
19+
email.setSubject("Mail from PHPoC Shield for Arduino");
20+
21+
// write email message
22+
email.beginMessage();
23+
email.println("Hello, world!");
24+
email.println("I am PHPoC Shield for Arduino");
25+
email.println("Good bye");
26+
email.endMessage();
27+
28+
// send email
29+
if(email.send() > 0)
30+
Serial.println("Email send ok");
31+
else
32+
Serial.println("Email send failed");
33+
}
34+
35+
void loop() {
36+
}

‎examples/GmailClient/GmailClient.ino

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include "SPI.h"
2+
#include "Phpoc.h"
3+
4+
PhpocEmail email;
5+
6+
void setup() {
7+
Serial.begin(9600);
8+
while(!Serial)
9+
;
10+
11+
Phpoc.begin(PF_LOG_SPI | PF_LOG_NET | PF_LOG_APP);
12+
//Phpoc.begin();
13+
14+
Serial.println("Email Client Test using outgoing relay server");
15+
16+
// [login using your private password]
17+
// Google may block sign-in attempts from some apps or devices that do not use modern security standards.
18+
// Change your settings to allow less secure apps to access your account.
19+
// https://www.google.com/settings/security/lesssecureapps
20+
21+
// [login using app password]
22+
// 1. turn on 2-step verification
23+
// 2. create app password
24+
// 3. apply app password as your login password
25+
26+
// setup outgoing relay server - gmail.com
27+
email.setOutgoingServer("smtp.gmail.com", 587);
28+
email.setOutgoingLogin("your_login_id", "your_login_password or app_password");
29+
30+
// setup From/To/Subject
31+
email.setFrom("from_email_address", "from_user_name");
32+
email.setTo("to_email_address", "to_user_name");
33+
email.setSubject("Mail from PHPoC Shield for Arduino");
34+
35+
// write email message
36+
email.beginMessage();
37+
email.println("Hello, world!");
38+
email.println("I am PHPoC Shield for Arduino");
39+
email.println("Good bye");
40+
email.endMessage();
41+
42+
// send email
43+
if(email.send() > 0)
44+
Serial.println("Email send ok");
45+
else
46+
Serial.println("Email send failed");
47+
}
48+
49+
void loop() {
50+
}

‎examples/SSHServer/SSHServer.ino

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include "SPI.h"
2+
#include "Phpoc.h"
3+
4+
PhpocServer server(22);
5+
boolean alreadyConnected = false; // whether or not the client was connected previously
6+
7+
void setup() {
8+
Serial.begin(9600);
9+
while(!Serial)
10+
;
11+
12+
Phpoc.begin(PF_LOG_SPI | PF_LOG_NET);
13+
//Phpoc.begin();
14+
15+
server.beginSSH("root", "1234");
16+
//server.beginSSH("", "");
17+
//server.beginSSH();
18+
19+
Serial.print("SSH server address : ");
20+
Serial.println(Phpoc.localIP());
21+
}
22+
23+
void loop() {
24+
// wait for a new client:
25+
PhpocClient client = server.available();
26+
27+
// when the client sends the first byte, say hello:
28+
if (client) {
29+
if (!alreadyConnected) {
30+
// clear out the transmission buffer:
31+
client.flush();
32+
Serial.println("We have a new client");
33+
client.println("Hello, client!");
34+
alreadyConnected = true;
35+
}
36+
37+
if (client.available() > 0) {
38+
// read the bytes incoming from the client:
39+
char thisChar = client.read();
40+
// echo the bytes back to the client:
41+
server.write(thisChar);
42+
// echo the bytes to the server as well:
43+
Serial.write(thisChar);
44+
}
45+
}
46+
}

‎examples/SSLServer/SSLServer.ino

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include "SPI.h"
2+
#include "Phpoc.h"
3+
4+
PhpocServer server(443);
5+
boolean alreadyConnected = false; // whether or not the client was connected previously
6+
7+
void setup() {
8+
Serial.begin(9600);
9+
while(!Serial)
10+
;
11+
12+
Phpoc.begin(PF_LOG_SPI | PF_LOG_NET);
13+
//Phpoc.begin();
14+
15+
server.beginSSL();
16+
17+
Serial.print("SSL server address : ");
18+
Serial.println(Phpoc.localIP());
19+
}
20+
21+
void loop() {
22+
// wait for a new client:
23+
PhpocClient client = server.available();
24+
25+
// when the client sends the first byte, say hello:
26+
if (client) {
27+
if (!alreadyConnected) {
28+
// clear out the transmission buffer:
29+
client.flush();
30+
Serial.println("We have a new client");
31+
client.println("Hello, client!");
32+
alreadyConnected = true;
33+
}
34+
35+
if (client.available() > 0) {
36+
// read the bytes incoming from the client:
37+
char thisChar = client.read();
38+
// echo the bytes back to the client:
39+
server.write(thisChar);
40+
// echo the bytes to the server as well:
41+
Serial.write(thisChar);
42+
}
43+
}
44+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include "SPI.h"
2+
#include "Phpoc.h"
3+
4+
PhpocServer server(23);
5+
boolean alreadyConnected = false; // whether or not the client was connected previously
6+
7+
void setup() {
8+
Serial.begin(9600);
9+
while(!Serial)
10+
;
11+
12+
Phpoc.begin(PF_LOG_SPI | PF_LOG_NET);
13+
//Phpoc.begin();
14+
15+
// beginTelnet() enables telnet option negotiation & "character at a time".
16+
// In "character at a time" mode, text typed is immediately sent to server.
17+
server.beginTelnet();
18+
19+
Serial.print("Telnet server address : ");
20+
Serial.println(Phpoc.localIP());
21+
}
22+
23+
void loop() {
24+
// wait for a new client:
25+
PhpocClient client = server.available();
26+
27+
// when the client sends the first byte, say hello:
28+
if (client) {
29+
if (!alreadyConnected) {
30+
// clear out the transmission buffer:
31+
client.flush();
32+
Serial.println("We have a new client");
33+
client.println("Hello, client!");
34+
alreadyConnected = true;
35+
}
36+
37+
if (client.available() > 0) {
38+
// read the bytes incoming from the client:
39+
char thisChar = client.read();
40+
// echo the bytes back to the client:
41+
server.write(thisChar);
42+
// echo the bytes to the server as well:
43+
Serial.write(thisChar);
44+
}
45+
}
46+
}

‎examples/WebClient/WebClient.ino

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include <SPI.h>
2+
#include <Phpoc.h>
3+
4+
//char server_name[] = "www.google.com";
5+
//char server_name[] = "216.58.221.36";
6+
char server_name[] = "www.arduino.cc";
7+
PhpocClient client;
8+
9+
void setup() {
10+
Serial.begin(9600);
11+
while(!Serial)
12+
;
13+
14+
Serial.println("PHPoC TCP Client test");
15+
16+
Phpoc.begin(PF_LOG_SPI | PF_LOG_NET);
17+
//Phpoc.begin();
18+
19+
if(client.connect(server_name, 80))
20+
{
21+
Serial.println("connected");
22+
client.println("GET / HTTP/1.0");
23+
client.println();
24+
}
25+
else
26+
Serial.println("connection failed");
27+
}
28+
29+
void loop() {
30+
if(client.available())
31+
{
32+
char c = client.read();
33+
Serial.print(c);
34+
}
35+
36+
if(!client.connected())
37+
{
38+
Serial.println("disconnected");
39+
client.stop();
40+
while(1)
41+
;
42+
}
43+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include "SPI.h"
2+
#include "Phpoc.h"
3+
4+
PhpocServer server(80);
5+
6+
void setup() {
7+
Serial.begin(9600);
8+
while(!Serial)
9+
;
10+
11+
Phpoc.begin(PF_LOG_SPI | PF_LOG_NET);
12+
//Phpoc.begin();
13+
14+
server.beginWebSocket("remote_push");
15+
16+
Serial.print("WebSocket server address : ");
17+
Serial.println(Phpoc.localIP());
18+
}
19+
20+
void loop() {
21+
// wait for a new client:
22+
PhpocClient client = server.available();
23+
24+
if (client) {
25+
if (client.available() > 0) {
26+
// read the bytes incoming from the client:
27+
char thisChar = client.read();
28+
29+
if(thisChar == 'A')
30+
Serial.println("button A press");
31+
if(thisChar == 'a')
32+
Serial.println("button A release");
33+
if(thisChar == 'B')
34+
Serial.println("button B press");
35+
if(thisChar == 'b')
36+
Serial.println("button B release");
37+
if(thisChar == 'C')
38+
Serial.println("button C press");
39+
if(thisChar == 'c')
40+
Serial.println("button C release");
41+
}
42+
}
43+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include "SPI.h"
2+
#include "Phpoc.h"
3+
4+
PhpocServer server(80);
5+
6+
char slideName;
7+
int slideValue;
8+
9+
void setup() {
10+
Serial.begin(9600);
11+
while(!Serial)
12+
;
13+
14+
Phpoc.begin(PF_LOG_SPI | PF_LOG_NET);
15+
//Phpoc.begin();
16+
17+
server.beginWebSocket("remote_slide");
18+
19+
Serial.print("WebSocket server address : ");
20+
Serial.println(Phpoc.localIP());
21+
}
22+
23+
void loop() {
24+
// wait for a new client:
25+
PhpocClient client = server.available();
26+
27+
if (client) {
28+
String slideStr = client.readLine();
29+
30+
if(slideStr)
31+
{
32+
slideName = slideStr.charAt(0);
33+
slideValue = slideStr.substring(1).toInt();
34+
35+
Serial.print(slideName);
36+
Serial.print('/');
37+
Serial.println(slideValue);
38+
}
39+
}
40+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include "SPI.h"
2+
#include "Phpoc.h"
3+
4+
char server[] = "www.arduino.cc";
5+
6+
PhpocClient client;
7+
8+
void setup() {
9+
Serial.begin(9600);
10+
while(!Serial)
11+
;
12+
13+
Serial.println("PHPoC SSL Client test");
14+
15+
Phpoc.begin(PF_LOG_SPI | PF_LOG_NET);
16+
//Phpoc.begin();
17+
18+
if(client.connectSSL(server, 443)) {
19+
Serial.println("Connected to server");
20+
// Make a HTTP request:
21+
client.println("GET /asciilogo.txt HTTP/1.1");
22+
client.println("Host: www.arduino.cc");
23+
client.println("Connection: close");
24+
client.println();
25+
Serial.println("Request sent");
26+
}
27+
}
28+
29+
void loop() {
30+
// if there are incoming bytes available
31+
// from the server, read them and print them:
32+
while (client.available()) {
33+
char c = client.read();
34+
Serial.write(c);
35+
}
36+
37+
// if the server's disconnected, stop the client:
38+
if (!client.connected()) {
39+
Serial.println();
40+
Serial.println("disconnecting from server.");
41+
client.stop();
42+
43+
// do nothing forevermore:
44+
while (true);
45+
}
46+
47+
}

‎keywords.txt

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#######################################
2+
# Syntax Coloring Map For PHPoC
3+
#######################################
4+
5+
#######################################
6+
# Datatypes (KEYWORD1)
7+
#######################################
8+
9+
Phpoc KEYWORD1
10+
PhpocClient KEYWORD1
11+
PhpocServer KEYWORD1
12+
PhpocEmail KEYWORD1
13+
PhpocDateTime KEYWORD1
14+
15+
#######################################
16+
# Methods and Functions (KEYWORD2)
17+
#######################################
18+
19+
begin KEYWORD2
20+
beginTelnet KEYWORD2
21+
beginWebSocket KEYWORD2
22+
beginSSL KEYWORD2
23+
beginSSH KEYWORD2
24+
connect KEYWORD2
25+
connectSSL KEYWORD2
26+
connected KEYWORD2
27+
available KEYWORD2
28+
read KEYWORD2
29+
readLine KEYWORD2
30+
peek KEYWORD2
31+
write KEYWORD2
32+
flush KEYWORD2
33+
stop KEYWORD2
34+
command KEYWORD2
35+
setOutgoingServer KEYWORD2
36+
setOutgoingLogin KEYWORD2
37+
setFrom KEYWORD2
38+
setTo KEYWORD2
39+
setSubject KEYWORD2
40+
beginMessage KEYWORD2
41+
endMessage KEYWORD2
42+
send KEYWORD2
43+
date KEYWORD2
44+
hour KEYWORD2
45+
minute KEYWORD2
46+
second KEYWORD2
47+
day KEYWORD2
48+
dayofWeek KEYWORD2
49+
month KEYWORD2
50+
year KEYWORD2
51+
52+
#######################################
53+
# Constants (LITERAL1)
54+
#######################################
55+
56+
PF_LOG_SPI LITERAL1
57+
PF_LOG_NET LITERAL1
58+
PF_LOG_APP LITERAL1

‎library.properties

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=PHPoC
2+
version=0.8.0
3+
author=Sollae Systems
4+
maintainer=Sollae Systems (phpoc@phpoc.com)
5+
sentence=PHPoC Ethernet/Wifi Shield for Arduino
6+
paragraph=TCP/EMAIL/SSL/SSH/Web communication helper based on PHPoC
7+
category=Communication
8+
url=http://www.phpoc.com
9+
architectures=avr

‎src/Phpoc.cpp

Lines changed: 793 additions & 0 deletions
Large diffs are not rendered by default.

‎src/Phpoc.h

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
* Copyright (c) 2016, Sollae Systems. All rights reserved.
3+
*
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions
6+
* are met:
7+
*
8+
* - Redistributions of source code must retain the above copyright
9+
* notice, this list of conditions and the following disclaimer.
10+
*
11+
* - Redistributions in binary form must reproduce the above copyright
12+
* notice, this list of conditions and the following disclaimer in the
13+
* documentation and/or other materials provided with the distribution.
14+
*
15+
* - Neither the name of the Sollae Systems nor the names of its
16+
* contributors may be used to endorse or promote products derived from
17+
* this software without specific prior written permission.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY SOLLAE SYSTEMS "AS IS" AND ANY EXPRESS OR
20+
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22+
* DISCLAIMED. IN NO EVENT SHALL SOLLAE SYSTEMS BE LIABLE FOR ANY DIRECT,
23+
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27+
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
28+
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29+
* POSSIBILITY OF SUCH DAMAGE.
30+
*/
31+
32+
#ifndef Phpoc_h
33+
#define Phpoc_h
34+
35+
#include <IPAddress.h>
36+
#include <PhpocClient.h>
37+
#include <PhpocServer.h>
38+
#include <PhpocEmail.h>
39+
#include <PhpocDateTime.h>
40+
41+
/* PHPoC flags */
42+
#define PF_SHIELD 0x01 // PHPoC shield installed
43+
#define PF_LOG_SPI 0x02
44+
#define PF_LOG_NET 0x04
45+
#define PF_LOG_APP 0x08
46+
47+
/* PHPoC shield runtime error */
48+
#define PE_NO_SHIELD 1
49+
#define PE_TIMEOUT 2
50+
#define PE_PROTOCOL 3
51+
52+
/* PHPoC shield state */
53+
#define PS_NO_SHIELD 0 // PHPoC Shield not found
54+
#define PS_NET_STOP 1 // ethernet enabled, but unplugged
55+
#define PS_NET_SCAN 2 // wifi enabled, but doesn't associated
56+
#define PS_NET_ADDRESS 3 // acquiring network address (ip address)
57+
#define PS_NET_CONNECTED 4 // ethernet or wifi ready
58+
59+
#define VSP_COUNT_LIMIT 64 // string + NULL(0x00)
60+
61+
class PhpocClass
62+
{
63+
private:
64+
/* SPI priviate member functions */
65+
uint16_t spi_request(int req);
66+
uint16_t spi_resync();
67+
int spi_wait(int len, int ms);
68+
int spi_cmd_status();
69+
int spi_cmd_txlen();
70+
int spi_cmd_rxbuf();
71+
int spi_cmd_sync();
72+
int spi_cmd_read(uint8_t *rbuf, size_t rlen);
73+
int spi_cmd_write(const uint8_t *wbuf, size_t wlen, boolean pgm);
74+
int api_write_command(const char *wbuf, int wlen);
75+
int api_write_data(const uint8_t *wbuf, int wlen, boolean pgm);
76+
77+
protected:
78+
int vsprintf(char *str, const __FlashStringHelper *format, va_list args);
79+
int vsprintf(char *str, const char *format, va_list args);
80+
IPAddress inet_aton(const char *str);
81+
82+
public:
83+
uint8_t flags;
84+
uint16_t spi_wait_ms;
85+
/* PHPoC specific public member functions */
86+
int command(const __FlashStringHelper *format, ...);
87+
int command(const char *format, ...);
88+
int write(const __FlashStringHelper *wstr);
89+
int write(const char *wstr);
90+
int write(const uint8_t *wbuf, size_t wlen);
91+
int read(uint8_t *rbuf, size_t rlen);
92+
IPAddress parseIP(); /* read & parse IP address */
93+
uint16_t parseInt(); /* read & parse integer */
94+
IPAddress getHostByName(const char *host, int wait_ms = 2000);
95+
void logFlush(uint8_t id);
96+
void logPrint(uint8_t id);
97+
98+
public:
99+
/* Arduino Ethernet compatible public member functions */
100+
int begin(uint8_t init_flags = 0x00);
101+
int maintain();
102+
IPAddress localIP();
103+
IPAddress subnetMask();
104+
IPAddress gatewayIP();
105+
IPAddress dnsServerIP();
106+
};
107+
108+
extern PhpocClass Phpoc;
109+
110+
#endif

‎src/PhpocClient.cpp

Lines changed: 804 additions & 0 deletions
Large diffs are not rendered by default.

‎src/PhpocClient.h

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/*
2+
* Copyright (c) 2016, Sollae Systems. All rights reserved.
3+
*
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions
6+
* are met:
7+
*
8+
* - Redistributions of source code must retain the above copyright
9+
* notice, this list of conditions and the following disclaimer.
10+
*
11+
* - Redistributions in binary form must reproduce the above copyright
12+
* notice, this list of conditions and the following disclaimer in the
13+
* documentation and/or other materials provided with the distribution.
14+
*
15+
* - Neither the name of the Sollae Systems nor the names of its
16+
* contributors may be used to endorse or promote products derived from
17+
* this software without specific prior written permission.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY SOLLAE SYSTEMS "AS IS" AND ANY EXPRESS OR
20+
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22+
* DISCLAIMED. IN NO EVENT SHALL SOLLAE SYSTEMS BE LIABLE FOR ANY DIRECT,
23+
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27+
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
28+
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29+
* POSSIBILITY OF SUCH DAMAGE.
30+
*/
31+
32+
#ifndef PhpocClient_h
33+
#define PhpocClient_h
34+
35+
#include <Arduino.h>
36+
#include <Print.h>
37+
#include <Client.h>
38+
#include <IPAddress.h>
39+
40+
#define INCLUDE_PHPOC_CACHE
41+
42+
#ifdef INCLUDE_PHPOC_CACHE
43+
#define READ_CACHE_SIZE 18 /* 3x 6bytes websocket data */
44+
#define WRITE_CACHE_SIZE 16
45+
#endif
46+
47+
#ifdef READ_CACHE_SIZE
48+
#define LINE_BUF_SIZE READ_CACHE_SIZE
49+
#else
50+
#define LINE_BUF_SIZE 32
51+
#endif
52+
53+
#define MAX_SOCK_TCP 6 /* 1 SSL + 1 SSH + 4 TCP */
54+
#define MAX_SOCK_UDP 4
55+
56+
/* socket ID */
57+
#define SOCK_ID_SSL 0 /* tcp0 */
58+
#define SOCK_ID_SSH 1 /* tcp1 */
59+
#define SOCK_ID_TCP 2 /* tcp2/3/4/5 */
60+
61+
/* tcp/ssl/ssh state */
62+
#define TCP_CLOSED 0
63+
#define TCP_LISTEN 1
64+
#define TCP_CONNECTED 4
65+
#define SSL_STOP 11
66+
#define SSL_CONNECTED 19
67+
#define SSH_STOP 11
68+
#define SSH_AUTH 17
69+
#define SSH_CONNECTED 19
70+
71+
#define SSH_AUTH_SIZE 16 /* buffer size for ssh username/password */
72+
73+
class PhpocClient : public Client
74+
{
75+
private:
76+
#ifdef INCLUDE_PHPOC_CACHE
77+
static uint32_t tick_32ms[MAX_SOCK_TCP];
78+
static uint8_t state_32ms[MAX_SOCK_TCP];
79+
static uint16_t rxlen_32ms[MAX_SOCK_TCP];
80+
static uint8_t read_cache_len[MAX_SOCK_TCP];
81+
static uint8_t write_cache_len[MAX_SOCK_TCP];
82+
static uint8_t read_cache_buf[MAX_SOCK_TCP][READ_CACHE_SIZE];
83+
static uint8_t write_cache_buf[MAX_SOCK_TCP][WRITE_CACHE_SIZE];
84+
static void update_cache(uint8_t id);
85+
#endif
86+
static char read_line_buf[LINE_BUF_SIZE + 2];
87+
88+
private:
89+
uint8_t sock_id;
90+
int read_line_from_cache(uint8_t *buf, size_t size);
91+
92+
public:
93+
/* PHPoC specific public member functions */
94+
int connectSSL(IPAddress ip, uint16_t port);
95+
int connectSSL(const char *host, uint16_t port);
96+
char *readLine();
97+
int readLine(uint8_t *buf, size_t size);
98+
99+
public:
100+
/* Arduino EthernetClient compatible public member functions */
101+
PhpocClient();
102+
PhpocClient(uint8_t id);
103+
virtual int connect(IPAddress ip, uint16_t port);
104+
virtual int connect(const char *host, uint16_t port);
105+
virtual size_t write(uint8_t byte);
106+
virtual size_t write(const uint8_t *buf, size_t size);
107+
virtual int available();
108+
virtual int read();
109+
virtual int read(uint8_t *buf, size_t size);
110+
virtual int peek();
111+
virtual void flush();
112+
virtual void stop();
113+
virtual uint8_t connected();
114+
virtual operator bool();
115+
virtual bool operator==(const bool value) { return bool() == value; }
116+
virtual bool operator!=(const bool value) { return bool() != value; }
117+
virtual bool operator==(const PhpocClient&);
118+
virtual bool operator!=(const PhpocClient& rhs) { return !this->operator==(rhs); };
119+
120+
friend class PhpocServer;
121+
122+
using Print::write;
123+
};
124+
125+
#endif

‎src/PhpocDateTime.cpp

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/*
2+
* Copyright (c) 2016, Sollae Systems. All rights reserved.
3+
*
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions
6+
* are met:
7+
*
8+
* - Redistributions of source code must retain the above copyright
9+
* notice, this list of conditions and the following disclaimer.
10+
*
11+
* - Redistributions in binary form must reproduce the above copyright
12+
* notice, this list of conditions and the following disclaimer in the
13+
* documentation and/or other materials provided with the distribution.
14+
*
15+
* - Neither the name of the Sollae Systems nor the names of its
16+
* contributors may be used to endorse or promote products derived from
17+
* this software without specific prior written permission.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY SOLLAE SYSTEMS "AS IS" AND ANY EXPRESS OR
20+
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22+
* DISCLAIMED. IN NO EVENT SHALL SOLLAE SYSTEMS BE LIABLE FOR ANY DIRECT,
23+
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27+
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
28+
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29+
* POSSIBILITY OF SUCH DAMAGE.
30+
*/
31+
32+
#include <Phpoc.h>
33+
34+
char *PhpocDateTime::date(const char *format)
35+
{
36+
int len;
37+
38+
if(format && format[0])
39+
{
40+
if(Phpoc.command(F("sys date format")) >= 0)
41+
Phpoc.write(format);
42+
}
43+
44+
if(Phpoc.command(F("sys date")) > 0)
45+
{
46+
if((len = Phpoc.read((uint8_t *)date_buf, DATE_BUF_SIZE - 1)) >= 0)
47+
date_buf[len] = 0x00;
48+
else
49+
date_buf[0] = 0x00;
50+
}
51+
else
52+
date_buf[0] = 0x00;
53+
54+
return date_buf;
55+
}
56+
57+
char *PhpocDateTime::date(const __FlashStringHelper *format)
58+
{
59+
if(format && pgm_read_byte(0))
60+
{
61+
if(Phpoc.command(F("sys date format")) >= 0)
62+
Phpoc.write(format);
63+
}
64+
65+
return date();
66+
}
67+
68+
uint8_t PhpocDateTime::hour()
69+
{
70+
if(Phpoc.command(F("sys rtc get hour")) > 0)
71+
return Phpoc.parseInt();
72+
else
73+
return 0;
74+
}
75+
76+
uint8_t PhpocDateTime::minute()
77+
{
78+
if(Phpoc.command(F("sys rtc get minute")) > 0)
79+
return Phpoc.parseInt();
80+
else
81+
return 0;
82+
}
83+
84+
uint8_t PhpocDateTime::second()
85+
{
86+
if(Phpoc.command(F("sys rtc get second")) > 0)
87+
return Phpoc.parseInt();
88+
else
89+
return 0;
90+
}
91+
92+
uint8_t PhpocDateTime::day()
93+
{
94+
if(Phpoc.command(F("sys rtc get day")) > 0)
95+
return Phpoc.parseInt();
96+
else
97+
return 0;
98+
}
99+
100+
uint8_t PhpocDateTime::dayofWeek()
101+
{
102+
if(Phpoc.command(F("sys rtc get wday")) > 0)
103+
return Phpoc.parseInt();
104+
else
105+
return 0;
106+
}
107+
108+
uint8_t PhpocDateTime::month()
109+
{
110+
if(Phpoc.command(F("sys rtc get month")) > 0)
111+
return Phpoc.parseInt();
112+
else
113+
return 0;
114+
}
115+
116+
uint16_t PhpocDateTime::year()
117+
{
118+
if(Phpoc.command(F("sys rtc get year")) > 0)
119+
return Phpoc.parseInt();
120+
else
121+
return 0;
122+
}
123+

‎src/PhpocDateTime.h

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright (c) 2016, Sollae Systems. All rights reserved.
3+
*
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions
6+
* are met:
7+
*
8+
* - Redistributions of source code must retain the above copyright
9+
* notice, this list of conditions and the following disclaimer.
10+
*
11+
* - Redistributions in binary form must reproduce the above copyright
12+
* notice, this list of conditions and the following disclaimer in the
13+
* documentation and/or other materials provided with the distribution.
14+
*
15+
* - Neither the name of the Sollae Systems nor the names of its
16+
* contributors may be used to endorse or promote products derived from
17+
* this software without specific prior written permission.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY SOLLAE SYSTEMS "AS IS" AND ANY EXPRESS OR
20+
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22+
* DISCLAIMED. IN NO EVENT SHALL SOLLAE SYSTEMS BE LIABLE FOR ANY DIRECT,
23+
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27+
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
28+
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29+
* POSSIBILITY OF SUCH DAMAGE.
30+
*/
31+
32+
#ifndef PhpocDateTime_h
33+
#define PhpocDateTime_h
34+
35+
#define DATE_BUF_SIZE 32
36+
37+
class PhpocDateTime
38+
{
39+
private:
40+
char date_buf[DATE_BUF_SIZE];
41+
42+
public:
43+
char *date(const char *format = NULL);
44+
char *date(const __FlashStringHelper *format);
45+
uint8_t hour(); // 0 ~ 23
46+
uint8_t minute(); // 0 ~ 59
47+
uint8_t second(); // 0 ~ 59
48+
/* day, week, month, year number is ISO-8601 compliant */
49+
uint8_t day(); // 1 ~ 31
50+
uint8_t dayofWeek(); // 1(Monday) ~ 7(Sunday)
51+
uint8_t month(); // 1(January) ~ 12
52+
uint16_t year(); // 2000 ~ 2099
53+
};
54+
55+
#endif

‎src/PhpocEmail.cpp

Lines changed: 230 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
1+
/*
2+
* Copyright (c) 2016, Sollae Systems. All rights reserved.
3+
*
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions
6+
* are met:
7+
*
8+
* - Redistributions of source code must retain the above copyright
9+
* notice, this list of conditions and the following disclaimer.
10+
*
11+
* - Redistributions in binary form must reproduce the above copyright
12+
* notice, this list of conditions and the following disclaimer in the
13+
* documentation and/or other materials provided with the distribution.
14+
*
15+
* - Neither the name of the Sollae Systems nor the names of its
16+
* contributors may be used to endorse or promote products derived from
17+
* this software without specific prior written permission.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY SOLLAE SYSTEMS "AS IS" AND ANY EXPRESS OR
20+
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22+
* DISCLAIMED. IN NO EVENT SHALL SOLLAE SYSTEMS BE LIABLE FOR ANY DIRECT,
23+
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27+
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
28+
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29+
* POSSIBILITY OF SUCH DAMAGE.
30+
*/
31+
32+
#include <Phpoc.h>
33+
34+
void PhpocEmail::setOutgoingServer(const char *host, uint16_t port)
35+
{
36+
if(host && host[0])
37+
Phpoc.command(F("smtp server %s %u"), host, port);
38+
}
39+
40+
void PhpocEmail::setOutgoingServer(const __FlashStringHelper *host, uint16_t port)
41+
{
42+
if(host && pgm_read_byte(host));
43+
Phpoc.command(F("smtp server %S %u"), host, port);
44+
}
45+
46+
void PhpocEmail::setOutgoingLogin(const char *username, const char *password)
47+
{
48+
if(username && username[0] && password && password[0])
49+
Phpoc.command(F("smtp login %s %s"), username, password);
50+
}
51+
52+
void PhpocEmail::setOutgoingLogin(const __FlashStringHelper *username, const __FlashStringHelper *password)
53+
{
54+
if(username && pgm_read_byte(username) && password && pgm_read_byte(password))
55+
Phpoc.command(F("smtp login %S %S"), username, password);
56+
}
57+
58+
void PhpocEmail::setFrom(const char *email, const char *name)
59+
{
60+
if(Phpoc.command(F("smtp from")) >= 0)
61+
{
62+
if(email && email[0])
63+
Phpoc.write(email);
64+
else
65+
return;
66+
67+
if(name && name[0])
68+
Phpoc.write(name);
69+
else
70+
Phpoc.write(email);
71+
}
72+
}
73+
74+
void PhpocEmail::setFrom(const __FlashStringHelper *email, const __FlashStringHelper *name)
75+
{
76+
if(Phpoc.command(F("smtp from")) >= 0)
77+
{
78+
if(email && pgm_read_byte(email))
79+
Phpoc.write(email);
80+
else
81+
return;
82+
83+
if(name && pgm_read_byte(name))
84+
Phpoc.write(name);
85+
else
86+
Phpoc.write(email);
87+
}
88+
}
89+
90+
void PhpocEmail::setTo(const char *email, const char *name)
91+
{
92+
if(Phpoc.command(F("smtp to")) >= 0)
93+
{
94+
if(email && email[0])
95+
Phpoc.write(email);
96+
else
97+
return;
98+
99+
if(name && name[0])
100+
Phpoc.write(name);
101+
else
102+
Phpoc.write(email);
103+
}
104+
}
105+
106+
void PhpocEmail::setTo(const __FlashStringHelper *email, const __FlashStringHelper *name)
107+
{
108+
if(Phpoc.command(F("smtp to")) >= 0)
109+
{
110+
if(email && pgm_read_byte(email))
111+
Phpoc.write(email);
112+
else
113+
return;
114+
115+
if(name && pgm_read_byte(name))
116+
Phpoc.write(name);
117+
else
118+
Phpoc.write(email);
119+
}
120+
}
121+
122+
void PhpocEmail::setSubject(const char *subject)
123+
{
124+
if(subject && subject[0])
125+
{
126+
if(Phpoc.command(F("smtp subject")) >= 0)
127+
Phpoc.write(subject);
128+
}
129+
}
130+
131+
void PhpocEmail::setSubject(const __FlashStringHelper *subject)
132+
{
133+
if(subject && pgm_read_byte(subject))
134+
{
135+
if(Phpoc.command(F("smtp subject")) >= 0)
136+
Phpoc.write(subject);
137+
}
138+
}
139+
140+
void PhpocEmail::beginMessage()
141+
{
142+
Phpoc.command(F("smtp data begin"));
143+
write_cache_len = 0;
144+
}
145+
146+
void PhpocEmail::endMessage()
147+
{
148+
if(write_cache_len)
149+
{
150+
if(Phpoc.command(F("smtp data")) >= 0)
151+
Phpoc.write(write_cache_buf, write_cache_len);
152+
write_cache_len = 0;
153+
}
154+
}
155+
156+
size_t PhpocEmail::write(uint8_t byte)
157+
{
158+
return write(&byte, 1);
159+
}
160+
161+
size_t PhpocEmail::write(const uint8_t *buf, size_t size)
162+
{
163+
if(write_cache_len + size >= WRITE_CACHE_SIZE)
164+
{
165+
if(write_cache_len)
166+
{
167+
if(Phpoc.command(F("smtp data")) >= 0)
168+
Phpoc.write(write_cache_buf, write_cache_len);
169+
write_cache_len = 0;
170+
}
171+
172+
if(size)
173+
{
174+
if(Phpoc.command(F("smtp data")) >= 0)
175+
Phpoc.write(buf, size);
176+
}
177+
178+
return size;
179+
}
180+
else
181+
{
182+
memcpy(write_cache_buf + write_cache_len, buf, size);
183+
write_cache_len += size;
184+
return size;
185+
}
186+
}
187+
188+
uint8_t PhpocEmail::send()
189+
{
190+
int len, status;
191+
192+
#ifdef PF_LOG_APP
193+
if(Phpoc.flags & PF_LOG_APP)
194+
Phpoc.logFlush(1);
195+
#endif
196+
197+
if(Phpoc.command(F("smtp send")) < 0)
198+
return 0;
199+
200+
while(1)
201+
{
202+
#ifdef PF_LOG_APP
203+
if(Phpoc.flags & PF_LOG_APP)
204+
Phpoc.logPrint(1);
205+
#endif
206+
207+
if((len = Phpoc.command(F("smtp status"))) < 0)
208+
return 0;
209+
210+
if(len)
211+
{
212+
status = Phpoc.parseInt();
213+
break;
214+
}
215+
216+
delay(10);
217+
}
218+
219+
#ifdef PF_LOG_APP
220+
delay(100);
221+
if(Phpoc.flags & PF_LOG_APP)
222+
Phpoc.logPrint(1);
223+
#endif
224+
225+
if((status / 100) == 2)
226+
return 1; /* mail server respond 2xx */
227+
else
228+
return 0;
229+
}
230+

‎src/PhpocEmail.h

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright (c) 2016, Sollae Systems. All rights reserved.
3+
*
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions
6+
* are met:
7+
*
8+
* - Redistributions of source code must retain the above copyright
9+
* notice, this list of conditions and the following disclaimer.
10+
*
11+
* - Redistributions in binary form must reproduce the above copyright
12+
* notice, this list of conditions and the following disclaimer in the
13+
* documentation and/or other materials provided with the distribution.
14+
*
15+
* - Neither the name of the Sollae Systems nor the names of its
16+
* contributors may be used to endorse or promote products derived from
17+
* this software without specific prior written permission.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY SOLLAE SYSTEMS "AS IS" AND ANY EXPRESS OR
20+
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22+
* DISCLAIMED. IN NO EVENT SHALL SOLLAE SYSTEMS BE LIABLE FOR ANY DIRECT,
23+
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27+
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
28+
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29+
* POSSIBILITY OF SUCH DAMAGE.
30+
*/
31+
32+
#ifndef PhpocEmail_h
33+
#define PhpocEmail_h
34+
35+
#ifndef WRITE_CACHE_SIZE
36+
#define WRITE_CACHE_SIZE 32
37+
#endif
38+
39+
class PhpocEmail : public Print
40+
{
41+
private:
42+
uint8_t write_cache_len;
43+
uint8_t write_cache_buf[WRITE_CACHE_SIZE];
44+
45+
public:
46+
void setOutgoingServer(const char *host, uint16_t port);
47+
void setOutgoingServer(const __FlashStringHelper *host, uint16_t port);
48+
void setOutgoingLogin(const char *username, const char *password);
49+
void setOutgoingLogin(const __FlashStringHelper *username, const __FlashStringHelper *password);
50+
void setFrom(const char *email, const char *name = NULL);
51+
void setFrom(const __FlashStringHelper *email, const __FlashStringHelper *name = NULL);
52+
void setTo(const char *email, const char *name = NULL);
53+
void setTo(const __FlashStringHelper *email, const __FlashStringHelper *name = NULL);
54+
void setSubject(const char *subject);
55+
void setSubject(const __FlashStringHelper *subject);
56+
void beginMessage();
57+
void endMessage();
58+
virtual size_t write(uint8_t byte);
59+
virtual size_t write(const uint8_t *buf, size_t size);
60+
uint8_t send();
61+
using Print::write;
62+
};
63+
64+
#endif

‎src/PhpocServer.cpp

Lines changed: 502 additions & 0 deletions
Large diffs are not rendered by default.

‎src/PhpocServer.h

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright (c) 2016, Sollae Systems. All rights reserved.
3+
*
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions
6+
* are met:
7+
*
8+
* - Redistributions of source code must retain the above copyright
9+
* notice, this list of conditions and the following disclaimer.
10+
*
11+
* - Redistributions in binary form must reproduce the above copyright
12+
* notice, this list of conditions and the following disclaimer in the
13+
* documentation and/or other materials provided with the distribution.
14+
*
15+
* - Neither the name of the Sollae Systems nor the names of its
16+
* contributors may be used to endorse or promote products derived from
17+
* this software without specific prior written permission.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY SOLLAE SYSTEMS "AS IS" AND ANY EXPRESS OR
20+
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22+
* DISCLAIMED. IN NO EVENT SHALL SOLLAE SYSTEMS BE LIABLE FOR ANY DIRECT,
23+
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27+
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
28+
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29+
* POSSIBILITY OF SUCH DAMAGE.
30+
*/
31+
32+
#ifndef PhpocServer_h
33+
#define PhpocServer_h
34+
35+
#include <Server.h>
36+
37+
#define SERVER_API_TCP 0
38+
#define SERVER_API_TELNET 1
39+
#define SERVER_API_WS 2
40+
#define SERVER_API_SSL 3
41+
#define SERVER_API_SSH 4
42+
43+
class PhpocServer : public Server
44+
{
45+
private:
46+
static uint16_t server_port[MAX_SOCK_TCP];
47+
uint16_t listen_port;
48+
uint8_t server_api;
49+
const char *ws_path;
50+
const char *ssh_username;
51+
const char *ssh_password;
52+
void session_loop_tcp();
53+
void session_loop_ssl();
54+
void session_loop_ssh();
55+
56+
public:
57+
/* PHPoC specific public member functions */
58+
void beginTelnet();
59+
void beginWebSocket(const char *path = NULL);
60+
void beginSSL();
61+
void beginSSH(const char *username = NULL, const char *password = NULL);
62+
63+
public:
64+
/* Arduino EthernetServer compatible public member functions */
65+
PhpocServer(uint16_t port);
66+
PhpocClient available();
67+
virtual void begin();
68+
virtual size_t write(uint8_t byte);
69+
virtual size_t write(const uint8_t *buf, size_t size);
70+
using Print::write;
71+
};
72+
73+
#endif

‎src/PhpocVsprintf.cpp

Lines changed: 345 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,345 @@
1+
/*
2+
* Copyright (c) 2016, Sollae Systems. All rights reserved.
3+
*
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions
6+
* are met:
7+
*
8+
* - Redistributions of source code must retain the above copyright
9+
* notice, this list of conditions and the following disclaimer.
10+
*
11+
* - Redistributions in binary form must reproduce the above copyright
12+
* notice, this list of conditions and the following disclaimer in the
13+
* documentation and/or other materials provided with the distribution.
14+
*
15+
* - Neither the name of the Sollae Systems nor the names of its
16+
* contributors may be used to endorse or promote products derived from
17+
* this software without specific prior written permission.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY SOLLAE SYSTEMS "AS IS" AND ANY EXPRESS OR
20+
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22+
* DISCLAIMED. IN NO EVENT SHALL SOLLAE SYSTEMS BE LIABLE FOR ANY DIRECT,
23+
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27+
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
28+
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29+
* POSSIBILITY OF SUCH DAMAGE.
30+
*
31+
* simplified & PROGMEM enabled vsprintf
32+
* - %s : data memory string
33+
* - %S : program memory string
34+
*/
35+
36+
#include <Phpoc.h>
37+
38+
#define STATE_NC 0 /* No Conversion */
39+
#define STATE_SC 1 /* Start Conversion */
40+
#define STATE_c 2
41+
#define STATE_d 3
42+
#define STATE_s 4
43+
#define STATE_u 5
44+
#define STATE_x 6
45+
46+
#define FLAG_UPPER 0x01 /* upper case hexa-decimal, E, F, G */
47+
#define FLAG_LEFT 0x02 /* left justified (-) */
48+
#define FLAG_SIGN 0x04 /* sign specifier (+) */
49+
#define FLAG_SIGN_MINUS 0x08 /* minus sign */
50+
#define FLAG_LONG 0x10 /* long type modifier (32bit) */
51+
#define FLAG_LONG_LONG 0x20 /* long long type modifier (64bit) */
52+
#define FLAG_PGM_STR 0x40 /* program memory string */
53+
54+
static char *vsp_ptr;
55+
static uint8_t vsp_count;
56+
57+
static void vsp_out_char(char ch)
58+
{
59+
if(vsp_ptr)
60+
{
61+
if((vsp_count + 1) < VSP_COUNT_LIMIT)
62+
{
63+
*vsp_ptr++ = ch;
64+
vsp_count++;
65+
}
66+
}
67+
/*
68+
else
69+
{
70+
vsp_write(&ch, 1);
71+
vsp_count++;
72+
}
73+
*/
74+
}
75+
76+
static void vsp_out_u16(uint16_t u16, uint8_t base, uint8_t flags)
77+
{
78+
char digit_buf[7]; /* 1(sign) + 5(digit) + 1(0x00) */
79+
int digit, div;
80+
char *ptr;
81+
82+
memset(digit_buf, '0', 6);
83+
digit_buf[6] = 0x00;
84+
ptr = digit_buf + 5;
85+
86+
for(digit = 4; digit >=0; digit--, ptr--)
87+
{
88+
if(u16)
89+
{
90+
if(digit < 4)
91+
u16 /= base;
92+
div = u16 % base;
93+
94+
if(div < 10)
95+
*ptr = '0' + div;
96+
else
97+
{
98+
if(flags & FLAG_UPPER)
99+
*ptr = ('A' - 10) + div;
100+
else
101+
*ptr = ('a' - 10) + div;
102+
}
103+
}
104+
else
105+
break;
106+
}
107+
108+
ptr = digit_buf;
109+
while(*ptr == '0')
110+
ptr++;
111+
112+
if(!*ptr)
113+
ptr--;
114+
else
115+
{
116+
if(flags & FLAG_SIGN_MINUS)
117+
{
118+
ptr--;
119+
*ptr = '-';
120+
}
121+
}
122+
123+
digit = strlen(ptr);
124+
125+
if(vsp_ptr)
126+
{
127+
if((vsp_count + digit) < VSP_COUNT_LIMIT)
128+
{
129+
strcpy(vsp_ptr, ptr);
130+
vsp_ptr += digit;
131+
vsp_count += digit;
132+
}
133+
}
134+
/*
135+
else
136+
{
137+
vsp_write(ptr, digit);
138+
vsp_count += digit;
139+
}
140+
*/
141+
}
142+
143+
static void vsp_out_str(char *str, uint8_t flags)
144+
{
145+
char ch;
146+
147+
while(1)
148+
{
149+
if(flags & FLAG_PGM_STR)
150+
ch = pgm_read_byte(str++);
151+
else
152+
ch = *str++;
153+
if(!ch)
154+
return;
155+
156+
if(vsp_ptr)
157+
{
158+
if((vsp_count + 1) >= VSP_COUNT_LIMIT)
159+
return;
160+
*vsp_ptr++ = ch;
161+
vsp_count++;
162+
}
163+
/*
164+
else
165+
{
166+
vsp_write(&ch, 1);
167+
vsp_count++;
168+
}
169+
*/
170+
}
171+
}
172+
173+
static int __vsprintf(const char *format, va_list args, boolean pgm)
174+
{
175+
uint8_t state, flags;
176+
uint16_t arg_u16;
177+
char ch;
178+
179+
state = STATE_NC;
180+
flags = 0x00;
181+
182+
while(1)
183+
{
184+
if(pgm)
185+
ch = pgm_read_byte(format++);
186+
else
187+
ch = *format++;
188+
189+
if(!ch)
190+
break;
191+
192+
if(state == STATE_NC)
193+
{
194+
if(ch == '%')
195+
state = STATE_SC;
196+
else
197+
vsp_out_char(ch);
198+
continue;
199+
}
200+
201+
if(state == STATE_SC)
202+
{
203+
switch(ch)
204+
{
205+
case '%':
206+
vsp_out_char(ch);
207+
state = STATE_NC;
208+
continue;
209+
case 'c':
210+
state = STATE_c;
211+
break;
212+
case 'd':
213+
state = STATE_d;
214+
break;
215+
case 'S':
216+
flags |= FLAG_PGM_STR;
217+
state = STATE_s;
218+
break;
219+
case 's':
220+
state = STATE_s;
221+
break;
222+
case 'u':
223+
state = STATE_u;
224+
break;
225+
case 'X':
226+
flags |= FLAG_UPPER;
227+
case 'x':
228+
state = STATE_x;
229+
break;
230+
default:
231+
vsp_out_char(ch);
232+
goto _end_of_conv;
233+
}
234+
}
235+
236+
switch(state)
237+
{
238+
case STATE_c:
239+
vsp_out_char(va_arg(args, int));
240+
break;
241+
case STATE_d:
242+
arg_u16 = va_arg(args, uint16_t);
243+
if(arg_u16 & 0x8000)
244+
{
245+
arg_u16 = ~arg_u16 + 1;
246+
flags |= FLAG_SIGN_MINUS;
247+
}
248+
vsp_out_u16(arg_u16, 10, flags);
249+
break;
250+
case STATE_s:
251+
arg_u16 = (uint16_t)va_arg(args, char *);
252+
vsp_out_str((char *)arg_u16, flags);
253+
break;
254+
case STATE_u:
255+
arg_u16 = va_arg(args, uint16_t);
256+
vsp_out_u16(arg_u16, 10, flags);
257+
break;
258+
case STATE_x:
259+
arg_u16 = va_arg(args, uint16_t);
260+
vsp_out_u16(arg_u16, 16, flags);
261+
break;
262+
}
263+
264+
_end_of_conv:
265+
state = STATE_NC;
266+
flags = 0x00;
267+
}
268+
269+
if(vsp_ptr)
270+
*vsp_ptr = 0x00;
271+
272+
return vsp_count;
273+
}
274+
275+
int PhpocClass::vsprintf(char *str, const __FlashStringHelper *format, va_list args)
276+
{
277+
vsp_ptr = str;
278+
vsp_count = 0;
279+
return ::__vsprintf((const char *)format, args, true);
280+
}
281+
282+
int PhpocClass::vsprintf(char *str, const char *format, va_list args)
283+
{
284+
vsp_ptr = str;
285+
vsp_count = 0;
286+
return ::__vsprintf(format, args, false);
287+
}
288+
289+
/*
290+
int PhpocClass::sprintf(char *str, const __FlashStringHelper *format, ...)
291+
{
292+
va_list args;
293+
int len;
294+
295+
vsp_ptr = str;
296+
vsp_count = 0;
297+
298+
va_start(args, format);
299+
len = ::__vsprintf((const char *)format, args, true);
300+
va_end(args);
301+
302+
return len;
303+
}
304+
int PhpocClass::sprintf(char *str, const char *format, ...)
305+
{
306+
va_list args;
307+
int len;
308+
309+
vsp_ptr = str;
310+
vsp_count = 0;
311+
312+
va_start(args, format);
313+
len = ::__vsprintf(format, args, false);
314+
va_end(args);
315+
316+
return len;
317+
}
318+
int PhpocClass::printf(const __FlashStringHelper *format, ...)
319+
{
320+
va_list args;
321+
int len;
322+
323+
vsp_ptr = NULL;
324+
325+
va_start(args, format);
326+
len = ::__vsprintf((const char *)format, args, true);
327+
va_end(args);
328+
329+
return len;
330+
}
331+
332+
int PhpocClass::printf(const char *format, ...)
333+
{
334+
va_list args;
335+
int len;
336+
337+
vsp_ptr = NULL;
338+
339+
va_start(args, format);
340+
len = ::__vsprintf(format, args, false);
341+
va_end(args);
342+
343+
return len;
344+
}
345+
*/

0 commit comments

Comments
 (0)
Please sign in to comment.