├── LICENSE
├── README.markdown
├── XMPPClient.cpp
├── XMPPClient.h
├── examples
└── message_and_close
│ └── message_and_close.pde
└── keywords.txt
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2013 Adam Rudd
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy of
6 | this software and associated documentation files (the "Software"), to deal in
7 | the Software without restriction, including without limitation the rights to
8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9 | the Software, and to permit persons to whom the Software is furnished to do so,
10 | subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 |
--------------------------------------------------------------------------------
/README.markdown:
--------------------------------------------------------------------------------
1 | Introduction
2 | ------------
3 |
4 | Arduino-XMPP is a simple write only XMPP client for the Arduino platform.
5 |
6 | Dependencies
7 | ------------
8 |
9 | Arduino-XMPP depends on Arduino-Base64 (https://github.com/adamvr/arduino-base64). This should be included before XMPPClient.h or added to your main library directory.
10 |
11 | Operation
12 | ---------
13 |
14 | Basic operation is as follows:
15 |
16 | 1. Create an XMPPClient instance specifying your XMPP server IP address and port.
17 | 2. Call connect on the XMPPClient interface, specifying your username, the server's host name, the resource name you wish to bind to and the your password.
18 | 3. If connect returns 1, you're good to go on.
19 | 4. Register your presence as available by calling sendPresence.
20 | 5. Send messages using sendMessage.
21 | 6. Close the connection and XMPP stream using close.
22 |
23 | Limitations
24 | -----------
25 |
26 | This client library is somewhat restricted in what in can do. The major restrictions are as follows:
27 |
28 | 1. SASL-PLAIN authentication only
29 | 2. No SSL/TLS encryption on the stream
30 | 3. No ability to receive and interpret incoming messages
31 |
32 | While the first one is on the two do list in some capacity, the first two are likely to never be rectified, due primarily to the SRAM restrictions of the Arduino
33 |
34 | TODO:
35 | -----
36 |
37 | * Add some semblance of being able to receive messages
38 | * Simplify the dependency on Base64, put base64 directly in the Arduino-XMPP directory, perhaps.
39 | * Add the ability to connect using a complete jid, rather than the parts of one
40 | * Add the ability to send presence messages that aren't just 'I'm here'
41 |
--------------------------------------------------------------------------------
/XMPPClient.cpp:
--------------------------------------------------------------------------------
1 | #include "XMPPClient.h"
2 |
3 | /****************/
4 | /* XMPP STANZAS */
5 | /***************/
6 | static const prog_char PROGMEM open_stream_template[] = "";
11 |
12 | static const prog_char PROGMEM plain_auth_template[] = ""
15 | "%s"
16 | "";
17 |
18 | static const prog_char PROGMEM bind_template[] = ""
21 | ""
23 | "%s"
24 | ""
25 | "";
26 |
27 | static const prog_char PROGMEM session_request_template[] = ""
31 | ""
33 | "";
34 |
35 | static const prog_char PROGMEM presence_template[] = ""
36 | ""
37 | "";
38 |
39 | static const prog_char PROGMEM message_template[] = ""
45 | "%s"
46 | "";
47 | static const prog_char PROGMEM close_template[] = ""
48 | "";
49 |
50 | /********************/
51 | /* TRANSITION TABLE */
52 | /********************/
53 | struct XMPPTransitionTableEntry {
54 | XMPPState currentState;
55 | XMPPState nextState;
56 | char *keyword;
57 | };
58 |
59 | int connTableSize = 6;
60 | XMPPTransitionTableEntry connTable[] = {{INIT, AUTH, "PLAIN"},
61 | {AUTH, AUTH_STREAM, "success"},
62 | {AUTH_STREAM, BIND, "bind"},
63 | {BIND, SESS, "jid"},
64 | {SESS, READY, "session"},
65 | {READY, WAIT, ""},
66 | {WAIT, WAIT, ""}};
67 |
68 | XMPPClient::XMPPClient() : client(0) {
69 | ;
70 | }
71 |
72 | XMPPClient::XMPPClient(uint8_t *ip, uint16_t port) : client(ip, port) {
73 | ;
74 | }
75 |
76 | int XMPPClient::connect(char *username, char *server, char *resource, char *password) {
77 | boolean connected = false, error = false;
78 | this->username = username;
79 | this->server = server;
80 | this->resource = resource;
81 | this->password = password;
82 |
83 | while(!client.connect()) {
84 | /* Retry connection every 1s and block until we're successful */
85 | delay(1000);
86 | }
87 |
88 | while(!connected && !error) {
89 | /* Connect dat shit^H^H^H^Hcuss */
90 | if(!client.connected()) {
91 | error = true;
92 | continue;
93 | }
94 |
95 | int ret;
96 | ret = stateAction();
97 |
98 | if(ret == -1) {
99 | error = true;
100 | }
101 |
102 | if(ret == 1) {
103 | connected = true;
104 | }
105 |
106 | if(ret) {
107 | continue;
108 | }
109 |
110 | processInput();
111 | }
112 |
113 | if(error || !connected) {
114 | return 0;
115 | } else {
116 | return 1;
117 | }
118 | }
119 |
120 | int XMPPClient::connect(char *jid, char *password) {
121 | /* Split the JID */
122 | /* Call connect(char*,char*,char*,char*) */
123 | }
124 |
125 | int XMPPClient::openStream(char *server) {
126 | sendTemplate(open_stream_template, strlen(server), server);
127 | }
128 |
129 | int XMPPClient::authenticate(char *username, char *password) {
130 | int plainStringLen = strlen(username) + strlen(password) + 2;
131 | int encStringLen = base64_enc_len(plainStringLen);
132 | char plainString[plainStringLen];
133 | char encString[encStringLen];
134 |
135 | /* Set up our plain auth string. It's in the form:
136 | * "\0username\0password"
137 | * where \0 is the null character
138 | */
139 | memset(plainString, '\0', plainStringLen);
140 | memcpy(plainString + 1, username, strlen(username));
141 | memcpy(plainString + 2 + strlen(username), password, strlen(password));
142 |
143 | /* Encode to base64 */
144 | base64_encode(encString, plainString, plainStringLen);
145 | sendTemplate(plain_auth_template, encStringLen, encString);
146 | }
147 |
148 | int XMPPClient::bindResource(char *resource) {
149 | sendTemplate(bind_template, strlen(resource), resource);
150 | }
151 |
152 | int XMPPClient::openSession(char *server) {
153 | sendTemplate(session_request_template, strlen(server), server);
154 | }
155 |
156 | int XMPPClient::sendMessage(char *recipientJid, char *message) {
157 | sendTemplate(message_template, strlen(recipientJid) + strlen(message), recipientJid, message);
158 | }
159 |
160 | int XMPPClient::sendPresence() {
161 | sendTemplate(presence_template, 0);
162 | }
163 |
164 | int XMPPClient::close() {
165 | sendTemplate(close_template, 0);
166 | }
167 |
168 | int XMPPClient::sendTemplate(const prog_char *temp_P, int fillLen, ...) {
169 | int tempLen = strlen_P(temp_P);
170 | char temp[tempLen];
171 | char buffer[tempLen + fillLen];
172 | va_list args;
173 |
174 | strcpy_P(temp, temp_P);
175 |
176 | va_start(args, fillLen);
177 | vsprintf(buffer, temp, args);
178 | client.write(buffer);
179 |
180 | return 1;
181 | }
182 |
183 | int XMPPClient::stateAction() {
184 | /*
185 | Serial.print("State = ");
186 | Serial.println(state);
187 | */
188 | switch(state) {
189 | case INIT:
190 | openStream(server);
191 | break;
192 | case AUTH:
193 | authenticate(username, password);
194 | break;
195 | case AUTH_STREAM:
196 | openStream(server);
197 | break;
198 | case BIND:
199 | bindResource(resource);
200 | break;
201 | case SESS:
202 | openSession(server);
203 | break;
204 | case READY:
205 | return 1;
206 | break;
207 | case WAIT:
208 | break;
209 | default:
210 | return -1;
211 | break;
212 | }
213 | return 0;
214 | }
215 |
216 | void XMPPClient::processInput() {
217 | int bufLen = 8;
218 | char buffer[bufLen];
219 | int i = 0;
220 | memset(buffer, '\0', bufLen);
221 | boolean stateChanged = false;
222 |
223 | if(!client.connected()) {
224 | state = WAIT;
225 | return;
226 | }
227 |
228 | // TODO: This process is pretty inefficient and naively implemented
229 | // It might be an idea to rewrite it cleverer
230 | while(!stateChanged) {
231 | if(client.available()) {
232 | /* Push a character from the ethernet interface into the buffer */
233 | for(i = 0 ; i < bufLen; i++) {
234 | buffer[i] = buffer[i+1];
235 | }
236 | buffer[i] = client.read();
237 |
238 |
239 | /* Ignore what we've read if it's an empty string */
240 | if(!strlen(buffer)) {
241 | continue;
242 | } else {
243 | //Serial.println(buffer);
244 | }
245 |
246 | for(int i = 0; i < connTableSize; i++) {
247 | if(state == connTable[i].currentState && strstr(buffer,connTable[i].keyword)) {
248 |
249 | /*
250 | Serial.println(buffer);
251 | Serial.println(connTable[i].keyword);
252 | Serial.println((int)strstr(buffer, connTable[i].keyword));
253 |
254 | Serial.print(connTable[i].keyword);
255 | Serial.println(" seen, transitioning");
256 | */
257 |
258 | state = connTable[i].nextState;
259 | client.flush();
260 | stateChanged = true;
261 | break;
262 | }
263 | }
264 | } else {
265 | delay(10);
266 | }
267 | }
268 | }
269 |
--------------------------------------------------------------------------------
/XMPPClient.h:
--------------------------------------------------------------------------------
1 | #ifndef _H_XMPP_CLIENT
2 | #define _H_XMPP_CLIENT
3 |
4 | #include
5 | #include
6 | #include
7 | #include
8 | #include
9 |
10 | enum XMPPState {
11 | INIT,
12 | AUTH,
13 | AUTH_STREAM,
14 | BIND,
15 | SESS,
16 | READY,
17 | WAIT
18 | };
19 |
20 | class XMPPClient {
21 | private:
22 | Client client;
23 | char *username;
24 | char *server;
25 | char *password;
26 | char *resource;
27 | XMPPState state;
28 |
29 | int sendTemplate(const prog_char *strTemplate, int fillLen, ...);
30 |
31 | int openStream(char *server);
32 | int authenticate(char *username, char *password);
33 | int bindResource(char *resource);
34 | int openSession(char *server);
35 |
36 | void processInput();
37 | int stateAction();
38 |
39 |
40 | public:
41 | XMPPClient();
42 | XMPPClient(uint8_t *ip, uint16_t port);
43 |
44 | int connect(char *username, char *server, char *resource, char *password);
45 | int connect(char *jid, char *password);
46 |
47 | int sendMessage(char *recipientJid, char *message);
48 | int sendPresence();
49 |
50 | int close();
51 |
52 | };
53 |
54 | #endif /* _H_XMPP_CLIENT */
55 |
--------------------------------------------------------------------------------
/examples/message_and_close/message_and_close.pde:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 | #include
5 | #include
6 | #include
7 |
8 | // Update these with values suitable for your network.
9 | byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
10 | byte server[] = { 192,168,2,1 };
11 | byte ip[] = { 192,168,2,10 };
12 |
13 | XMPPClient client(server, 5222);
14 |
15 | void setup()
16 | {
17 | Serial.begin(9600);
18 | Ethernet.begin(mac, ip);
19 | /* Connect to the XMPP server */
20 | if (client.connect("adam", "test.awg", "arduinus", "avrud0")) {
21 | /* Connected, let everyone know that we're online */
22 | client.sendPresence();
23 | }
24 | }
25 |
26 | int count = 0;
27 | void loop() {
28 | /* Say hello! */
29 | client.sendMessage("admin@test.awg", "hello");
30 | delay(5000);
31 |
32 | /* A couple of times... */
33 | if(count++ > 0) {
34 | /* Say goodbye! */
35 | client.sendMessage("admin@test.awg", "bye!");
36 | /* Close the connection */
37 | client.close();
38 | /* Spin forever */
39 | while(1) {;}
40 | }
41 | }
42 |
43 |
44 |
45 |
46 |
--------------------------------------------------------------------------------
/keywords.txt:
--------------------------------------------------------------------------------
1 | #######################################
2 | # Syntax Coloring Map For Ultrasound
3 | #######################################
4 |
5 | #######################################
6 | # Datatypes (KEYWORD1)
7 | #######################################
8 |
9 | XMPPClient KEYWORD1
10 |
11 | #######################################
12 | # Methods and Functions (KEYWORD2)
13 | #######################################
14 |
15 | connect KEYWORD2
16 | close KEYWORD2
17 | sendPresence KEYWORD2
18 | sendMessage KEYWORD2
19 |
20 | #######################################
21 | # Constants (LITERAL1)
22 | #######################################
23 |
24 |
--------------------------------------------------------------------------------