Skip to content

Commit c9bcc7b

Browse files
James Fosterianfixes
James Foster
authored andcommitted
Add files needed to compile Ethernet library (squashed)
1 parent 7978af6 commit c9bcc7b

21 files changed

+295
-12
lines changed

.travis.yml

+6
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,9 @@ script:
2727
- cd SampleProjects/TestSomething
2828
- bundle install
2929
- bundle exec arduino_ci.rb
30+
- cd ../NetworkLib
31+
- bundle install
32+
- cd scripts
33+
- bash -x ./install.sh
34+
- cd ..
35+
- bundle exec arduino_ci.rb

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1616
- `CppLibrary.print_stack_dump` prints stack trace dumps (on Windows specifically) to the console if encountered
1717
- Definitions for Arduino zero
1818
- Support for mock EEPROM (but only if board supports it)
19+
- Add stubs for `Client.h`, `IPAddress.h`, `Printable.h`, `Server.h`, and `Udp.h`
1920

2021
### Changed
2122
- Move repository from https://github.com/ianfixes/arduino_ci to https://github.com/Arduino-CI/arduino_ci
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
unittest:
2+
platforms:
3+
- mega2560
4+
libraries:
5+
- "Ethernet"
6+
7+
compile:
8+
platforms:
9+
- mega2560
10+
libraries:
11+
- "Ethernet"

SampleProjects/NetworkLib/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.bundle

SampleProjects/NetworkLib/Gemfile

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
source 'https://rubygems.org'
2+
gem 'arduino_ci', path: '../../'

SampleProjects/NetworkLib/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# NetworkLib
2+
3+
This is an example of a library that depends on Ethernet.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#include <NetworkLib.h>
2+
// if it seems bare, that's because it's only meant to
3+
// demonstrate compilation -- that references work
4+
void setup() {}
5+
6+
void loop() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
name=Ethernet
2+
version=0.1.0
3+
author=James Foster <[email protected]>
4+
maintainer=James Foster <[email protected]>
5+
sentence=Sample Ethernet library to validate Client/Server mocks
6+
paragraph=Sample Ethernet library to validate Client/Server mocks
7+
category=Other
8+
url=https://github.com/Arduino-CI/arduino_ci/SampleProjects/Ethernet
9+
architectures=avr,esp8266
10+
includes=NetworkLib.h
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/bash
2+
3+
# if we don't have an Ethernet library already (say, in new install or for an automated test),
4+
# then get the custom one we want to use for testing
5+
cd $(bundle exec arduino_library_location.rb)
6+
if [ ! -d ./Ethernet ] ; then
7+
git clone https://github.com/arduino-libraries/Ethernet.git
8+
fi
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include "Ethernet.h"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#pragma once
2+
3+
#include <Arduino.h>
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
cd SampleProjects/NetworkLib
3+
bundle config --local path vendor/bundle
4+
bundle install
5+
bundle exec arduino_ci_remote.rb --skip-compilation
6+
# bundle exec arduino_ci_remote.rb --skip-examples-compilation
7+
*/
8+
9+
#include <Arduino.h>
10+
#include <ArduinoUnitTests.h>
11+
#include <Ethernet.h>
12+
13+
unittest(test) { assertEqual(EthernetNoHardware, Ethernet.hardwareStatus()); }
14+
15+
unittest_main()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#include <Arduino.h>
2+
#include <ArduinoUnitTests.h>
3+
#include <Client.h>
4+
#include <IPAddress.h>
5+
#include <Printable.h>
6+
#include <Server.h>
7+
#include <Udp.h>
8+
9+
// Provide some rudamentary tests for these classes
10+
// They get more thoroughly tested in SampleProjects/NetworkLib
11+
12+
unittest(Client) {
13+
Client client;
14+
assertEqual(0, client.available()); // subclass of Stream
15+
assertEqual(0, client.availableForWrite()); // subclass of Print
16+
String outData = "Hello, world!";
17+
client.println(outData);
18+
String inData = client.readString();
19+
assertEqual(outData + "\r\n", inData);
20+
}
21+
22+
unittest(IPAddress) {
23+
IPAddress ipAddress0;
24+
assertEqual(0, ipAddress0.asWord());
25+
uint32_t one = 0x01020304;
26+
IPAddress ipAddress1(one);
27+
assertEqual(one, ipAddress1.asWord());
28+
IPAddress ipAddress2(2, 3, 4, 5);
29+
assertEqual(0x05040302, ipAddress2.asWord());
30+
uint8_t bytes[] = {3, 4, 5, 6};
31+
IPAddress ipAddress3(bytes);
32+
assertEqual(0x06050403, ipAddress3.asWord());
33+
uint8_t *pBytes = ipAddress1.raw_address();
34+
assertEqual(*(pBytes + 0), 4);
35+
assertEqual(*(pBytes + 1), 3);
36+
assertEqual(*(pBytes + 2), 2);
37+
assertEqual(*(pBytes + 3), 1);
38+
IPAddress ipAddress1a(one);
39+
assertTrue(ipAddress1 == ipAddress1a);
40+
assertTrue(ipAddress1 != ipAddress2);
41+
assertEqual(1, ipAddress1[3]);
42+
ipAddress1[1] = 11;
43+
assertEqual(11, ipAddress1[1]);
44+
assertEqual(1, ipAddress0 + 1);
45+
}
46+
47+
class TestPrintable : public Printable {
48+
public:
49+
virtual size_t printTo(Print &p) const {
50+
p.print("TestPrintable");
51+
return 13;
52+
}
53+
};
54+
55+
unittest(Printable) {
56+
TestPrintable printable;
57+
Client client;
58+
client.print(printable);
59+
assertEqual("TestPrintable", client.readString());
60+
}
61+
62+
class TestServer : public Server {
63+
public:
64+
uint8_t data;
65+
virtual size_t write(uint8_t value) {
66+
data = value;
67+
return 1;
68+
};
69+
};
70+
71+
unittest(Server) {
72+
TestServer server;
73+
server.write(67);
74+
assertEqual(67, server.data);
75+
}
76+
77+
unittest(Udp) {
78+
UDP udp;
79+
assertEqual(0, udp.available()); // subclass of Stream
80+
assertEqual(0, udp.availableForWrite()); // subclass of Print
81+
String outData = "Hello, world!";
82+
udp.println(outData);
83+
String inData = udp.readString();
84+
assertEqual(outData + "\r\n", inData);
85+
}
86+
87+
unittest_main()

appveyor.yml

+6
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,9 @@ test_script:
2525
- cd SampleProjects\TestSomething
2626
- bundle install
2727
- bundle exec arduino_ci.rb
28+
- cd ../NetworkLib
29+
- bundle install
30+
- cd scripts
31+
- install.sh
32+
- cd ..
33+
- bundle exec arduino_ci.rb

cpp/arduino/Arduino.h

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Where possible, variable names from the Arduino library are used to avoid confli
99

1010
#include "ArduinoDefines.h"
1111

12+
#include "IPAddress.h"
1213
#include "WCharacter.h"
1314
#include "WString.h"
1415
#include "Print.h"

cpp/arduino/Client.h

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#pragma once
2+
3+
#include <Stream.h>
4+
5+
class Client : public Stream {
6+
public:
7+
Client() {
8+
// The Stream mock defines a String buffer but never puts anyting in it!
9+
if (!mGodmodeDataIn) {
10+
mGodmodeDataIn = new String;
11+
}
12+
}
13+
~Client() {
14+
if (mGodmodeDataIn) {
15+
delete mGodmodeDataIn;
16+
mGodmodeDataIn = nullptr;
17+
}
18+
}
19+
virtual size_t write(uint8_t value) {
20+
mGodmodeDataIn->concat(value);
21+
return 1;
22+
}
23+
24+
protected:
25+
uint8_t *rawIPAddress(IPAddress &addr) { return addr.raw_address(); }
26+
};

cpp/arduino/IPAddress.h

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#pragma once
2+
3+
#include <stdint.h>
4+
5+
class IPAddress {
6+
private:
7+
union {
8+
uint8_t bytes[4];
9+
uint32_t dword;
10+
operator uint8_t *() const { return (uint8_t *)bytes; }
11+
} _address;
12+
13+
public:
14+
// Constructors
15+
IPAddress() : IPAddress(0, 0, 0, 0) {}
16+
IPAddress(uint8_t octet1, uint8_t octet2, uint8_t octet3, uint8_t octet4) {
17+
_address.bytes[0] = octet1;
18+
_address.bytes[1] = octet2;
19+
_address.bytes[2] = octet3;
20+
_address.bytes[3] = octet4;
21+
}
22+
IPAddress(uint32_t dword) { _address.dword = dword; }
23+
IPAddress(const uint8_t bytes[]) {
24+
_address.bytes[0] = bytes[0];
25+
_address.bytes[1] = bytes[1];
26+
_address.bytes[2] = bytes[2];
27+
_address.bytes[3] = bytes[3];
28+
}
29+
IPAddress(unsigned long dword) { _address.dword = (uint32_t)dword; }
30+
31+
// Accessors
32+
uint32_t asWord() const { return _address.dword; }
33+
uint8_t *raw_address() { return _address.bytes; }
34+
35+
// Comparisons
36+
bool operator==(const IPAddress &rhs) const {
37+
return _address.dword == rhs.asWord();
38+
}
39+
40+
bool operator!=(const IPAddress &rhs) const {
41+
return _address.dword != rhs.asWord();
42+
}
43+
44+
// Indexing
45+
uint8_t operator[](int index) const { return _address.bytes[index]; }
46+
uint8_t &operator[](int index) { return _address.bytes[index]; }
47+
48+
// Conversions
49+
operator uint32_t() const { return _address.dword; };
50+
51+
friend class EthernetClass;
52+
friend class UDP;
53+
friend class Client;
54+
friend class Server;
55+
friend class DhcpClass;
56+
friend class DNSClient;
57+
};
58+
59+
const IPAddress INADDR_NONE(0, 0, 0, 0);

cpp/arduino/Print.h

+9-12
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
#include <stdio.h>
44
#include <avr/pgmspace.h>
5+
6+
#include "Printable.h"
57
#include "WString.h"
68

79
#define DEC 10
@@ -12,22 +14,17 @@
1214
#endif
1315
#define BIN 2
1416

15-
class Print;
16-
17-
class Printable
18-
{
19-
public:
20-
virtual size_t printTo(Print& p) const = 0;
21-
};
22-
2317
class Print
2418
{
19+
private:
20+
int write_error;
21+
protected:
22+
void setWriteError(int err = 1) { write_error = err; }
2523
public:
26-
Print() {}
24+
Print() : write_error(0) {}
2725

28-
// Arduino's version of this is richer but until I see an actual error case I'm not sure how to mock
29-
int getWriteError() { return 0; }
30-
void clearWriteError() { }
26+
int getWriteError() { return write_error; }
27+
void clearWriteError() { setWriteError(0); }
3128
virtual int availableForWrite() { return 0; }
3229

3330
virtual size_t write(uint8_t) = 0;

cpp/arduino/Printable.h

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#pragma once
2+
3+
class Print;
4+
5+
class Printable {
6+
public:
7+
virtual size_t printTo(Print &p) const = 0;
8+
};

cpp/arduino/Server.h

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#pragma once
2+
3+
#include <Stream.h>
4+
5+
class Server : public Print {};

cpp/arduino/Udp.h

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#pragma once
2+
3+
#include <IPAddress.h>
4+
#include <Stream.h>
5+
6+
class UDP : public Stream {
7+
protected:
8+
uint8_t *rawIPAddress(IPAddress &addr) { return addr.raw_address(); };
9+
10+
public:
11+
UDP() {
12+
// The Stream mock defines a String buffer but never puts anyting in it!
13+
if (!mGodmodeDataIn) {
14+
mGodmodeDataIn = new String;
15+
}
16+
}
17+
~UDP() {
18+
if (mGodmodeDataIn) {
19+
delete mGodmodeDataIn;
20+
mGodmodeDataIn = nullptr;
21+
}
22+
}
23+
virtual size_t write(uint8_t value) {
24+
mGodmodeDataIn->concat(value);
25+
return 1;
26+
}
27+
};

0 commit comments

Comments
 (0)