string friend, made client optional

This commit is contained in:
ladyada 2015-05-31 23:56:27 -04:00
parent 495f2cddd5
commit fa63c335a5
4 changed files with 33 additions and 32 deletions

View File

@ -6,16 +6,31 @@ Adafruit_MQTT::Adafruit_MQTT(char *server, uint16_t port, char *user, char *key,
servername[SERVERNAME_SIZE-1] = 0;
portnum = port;
serverip = 0;
strncpy(username, user, USERNAME_SIZE);
username[USERNAME_SIZE-1] = 0;
strncpy(userkey, key, KEY_SIZE);
userkey[KEY_SIZE-1] = 0;
strncpy(clientid, cid, CLIENTID_SIZE);
userkey[CLIENTID_SIZE-1] = 0;
clientid[CLIENTID_SIZE-1] = 0;
errno = 0;
}
boolean Adafruit_MQTT::ping(void) {
}
static uint8_t *stringprint(uint8_t *p, char *s) {
uint16_t len = strlen(s);
p[0] = len >> 8; p++;
p[0] = len & 0xFF; p++;
memcpy(p, s, len);
return p+len;
}
// http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc398718028
uint8_t Adafruit_MQTT::connectPacket(uint8_t *packet) {
uint8_t *p = packet;
@ -25,11 +40,7 @@ uint8_t Adafruit_MQTT::connectPacket(uint8_t *packet) {
p+=2;
// fill in packet[1] last
p[0] = 0;
p[1] = 6; // (strlen(MQIsdp)
p+=2;
memcpy(p,"MQIsdp", 6);
p+=6;
p = stringprint(p, "MQIsdp");
p[0] = MQTT_PROTOCOL_LEVEL;
p++;
@ -47,25 +58,16 @@ uint8_t Adafruit_MQTT::connectPacket(uint8_t *packet) {
p[0] = MQTT_CONN_KEEPALIVE & 0xFF;
p++;
uint16_t len = strlen(clientid);
p[0] = len >> 8; p++;
p[0] = len & 0xFF; p++;
memcpy(p, clientid, len);
p+=len;
uint16_t len;
if ((clientid[0] != 0) && (strlen(clientid) > 0)) {
p = stringprint(p, clientid);
}
if (username[0] != 0) {
len = strlen(username);
p[0] = len >> 8; p++;
p[0] = len & 0xFF; p++;
memcpy(p, username, len);
p+=len;
p = stringprint(p, username);
}
if (userkey[0] != 0) {
len = strlen(userkey);
p[0] = len >> 8; p++;
p[0] = len & 0xFF; p++;
memcpy(p, userkey, len);
p+=len;
p = stringprint(p, userkey);
}
uint8_t totallen = p - packet;
@ -82,17 +84,14 @@ uint8_t Adafruit_MQTT::connectPacket(uint8_t *packet) {
uint8_t Adafruit_MQTT::publishPacket(uint8_t *packet, char *topic, char *data, uint8_t qos) {
uint8_t *p = packet;
uint16_t len = strlen(topic);
uint16_t len;
p[0] = MQTT_CTRL_PUBLISH << 4 | qos << 1;
// fill in packet[1] last
p+=2;
p[0] = len >> 8;
p++;
p[0] = len & 0xFF;
p++;
memcpy(p, topic, len);
p+=len;
p = stringprint(p, topic);
memcpy(p, data, strlen(data));
p+=strlen(data);
len = p - packet;

View File

@ -19,7 +19,7 @@
#define SERVERNAME_SIZE 25
#define USERNAME_SIZE 25
#define KEY_SIZE 41
#define FEEDNAME_SIZE 25
#define FEEDNAME_SIZE 45
#define CLIENTID_SIZE 23
#define CONNECT_TIMEOUT_MS 3000
@ -37,12 +37,14 @@
class Adafruit_MQTT {
public:
Adafruit_MQTT(char *server, uint16_t port, char *user, char *key);
Adafruit_MQTT(char *server, uint16_t port, char *user, char *key, char *cid);
uint8_t connectPacket(uint8_t *packet);
virtual boolean publish(char *topic, char *payload, uint8_t qos) {}
uint8_t publishPacket(uint8_t *packet, char *topic, char *payload, uint8_t qos);
boolean ping(void);
protected:
int8_t errno;
char servername[SERVERNAME_SIZE];

View File

@ -2,7 +2,7 @@
#include "Adafruit_MQTT_CC3000.h"
#include <Adafruit_Watchdog.h>
Adafruit_MQTT_CC3000::Adafruit_MQTT_CC3000(Adafruit_CC3000 *cc3k, char *server, uint16_t port, char *user, char *key) : Adafruit_MQTT(server, port, user, key), cc3000(cc3k)
Adafruit_MQTT_CC3000::Adafruit_MQTT_CC3000(Adafruit_CC3000 *cc3k, char *server, uint16_t port, char *user, char *key, char *cid) : Adafruit_MQTT(server, port, user, key, cid), cc3000(cc3k)
{
// nothin doin
}

View File

@ -8,7 +8,7 @@
class Adafruit_MQTT_CC3000 : public Adafruit_MQTT {
public:
Adafruit_MQTT_CC3000(Adafruit_CC3000 *cc3k, char *server, uint16_t port, char *user, char *key);
Adafruit_MQTT_CC3000(Adafruit_CC3000 *cc3k, char *server, uint16_t port, char *user, char *key, char *cid);
int8_t connect(void);
uint16_t readPacket(uint8_t *buffer, uint8_t maxlen, uint16_t timeout);
int32_t close(void);