Compare commits

..

2 Commits

Author SHA1 Message Date
Dávid Danyi
42c5965bf7 * missed buffer size fix in prev commit 2018-03-01 11:24:18 +01:00
Dávid Danyi
c6f68183d1 * Ethernet shield stuff updated with nbiot stability changes 2018-03-01 11:17:49 +01:00
2 changed files with 46 additions and 53 deletions

View File

@ -1,5 +1,6 @@
#include "wakaama-client.h" #include "wakaama-client.h"
#include <liblwm2m.h> #include <liblwm2m.h>
#include <USB/USBAPI.h>
#include <Dns.h> #include <Dns.h>
#include <Ethernet.h> #include <Ethernet.h>
@ -14,47 +15,47 @@ extern void lwm2m_printf(const char * format, ...);
* @param uri * @param uri
* @todo init and use uri in security object * @todo init and use uri in security object
*/ */
void ArduinoClient::init(Sodaq_nbIOT * nbiot) { void ArduinoClient::init() {
SerialUSB.println(F("init():start")); SerialOut.println(F("init():start"));
memset(&data, 0, sizeof(client_data_t)); memset(&data, 0, sizeof(client_data_t));
const __FlashStringHelper * objFail = F("Failed to create object"); const __FlashStringHelper * objFail = F("Failed to create object");
// create udp listener // create udp listener
data.nbIOT = nbiot; data.udp = new EthernetUDP();
data.sock = data.nbIOT->createSocket(localPort); data.udp->begin(localPort);
// init objects // init objects
SerialUSB.println(F("*object:security")); SerialOut.println(F("*object:security"));
objArray[0] = get_security_object(uri); objArray[0] = get_security_object(uri);
if (nullptr == objArray[0]) if (nullptr == objArray[0])
{ {
SerialUSB.println(objFail); SerialOut.println(objFail);
exit(0); exit(0);
} }
data.securityObjP = objArray[0]; data.securityObjP = objArray[0];
SerialUSB.println(F("*object:server")); SerialOut.println(F("*object:server"));
objArray[1] = get_server_object(); objArray[1] = get_server_object();
if (nullptr == objArray[1]) if (nullptr == objArray[1])
{ {
SerialUSB.println(objFail); SerialOut.println(objFail);
exit(0); exit(0);
} }
SerialUSB.println(F("*object:device")); SerialOut.println(F("*object:device"));
objArray[2] = get_object_device(); objArray[2] = get_object_device();
if (nullptr == objArray[2]) if (nullptr == objArray[2])
{ {
SerialUSB.println(objFail); SerialOut.println(objFail);
exit(0); exit(0);
} }
SerialUSB.println(F("*object:test")); SerialOut.println(F("*object:test"));
objArray[3] = get_test_object(); objArray[3] = get_test_object();
if (nullptr == objArray[3]) if (nullptr == objArray[3])
{ {
SerialUSB.println(objFail); SerialOut.println(objFail);
exit(0); exit(0);
} }
@ -62,11 +63,11 @@ void ArduinoClient::init(Sodaq_nbIOT * nbiot) {
* The liblwm2m library is now initialized with the functions that will be in * The liblwm2m library is now initialized with the functions that will be in
* charge of communication * charge of communication
*/ */
SerialUSB.println(F("*lwm2m_init()")); SerialOut.println(F("*lwm2m_init()"));
lwm2mH = lwm2m_init(&data); lwm2mH = lwm2m_init(&data);
if (NULL == lwm2mH) if (NULL == lwm2mH)
{ {
SerialUSB.println(F("lwm2m_init() failed")); SerialOut.println(F("lwm2m_init() failed"));
exit(0); exit(0);
} }
@ -74,19 +75,19 @@ void ArduinoClient::init(Sodaq_nbIOT * nbiot) {
* We configure the liblwm2m library with the name of the client - which shall be unique for each client - * We configure the liblwm2m library with the name of the client - which shall be unique for each client -
* the number of objects we will be passing through and the objects array * the number of objects we will be passing through and the objects array
*/ */
SerialUSB.println(F("*lwm2m_configure()")); SerialOut.println(F("*lwm2m_configure()"));
result = lwm2m_configure(lwm2mH, name, NULL, NULL, OBJ_COUNT, objArray); result = lwm2m_configure(lwm2mH, name, NULL, NULL, OBJ_COUNT, objArray);
if (result != 0) if (result != 0)
{ {
SerialUSB.println(F("lwm2m_configure() failed")); SerialOut.println(F("lwm2m_configure() failed"));
exit(0); exit(0);
} }
SerialUSB.println(F("init():done")); SerialOut.println(F("init():done"));
} }
void ArduinoClient::doWorkStep() { void ArduinoClient::doWorkStep() {
SerialUSB.println(F("doWorkStep():start")); SerialOut.println(F("doWorkStep():start"));
/* /*
* This function does two things: * This function does two things:
@ -94,26 +95,23 @@ void ArduinoClient::doWorkStep() {
* - Secondly it adjusts the timeout value (default 60s) depending on the state of the transaction * - Secondly it adjusts the timeout value (default 60s) depending on the state of the transaction
* (eg. retransmission) and the time before the next operation * (eg. retransmission) and the time before the next operation
*/ */
SerialUSB.println(F("lwm2m_step()")); SerialOut.println(F("lwm2m_step()"));
result = lwm2m_step(lwm2mH, &step_delay); result = lwm2m_step(lwm2mH, &step_delay);
if (result != 0) if (result != 0)
{ {
SerialUSB.print(F("lwm2m_step(): failed with error code ")); SerialOut.print(F("lwm2m_step() failed"));
SerialUSB.println(result); SerialOut.println(result);
return; return;
} }
// wait for socket event // wait for socket event
SerialUSB.println(F("parsePacket()")); SerialOut.println(F("parsePacket()"));
size_t pending = data.nbIOT->socketBytesPending(data.sock); int packetSize = data.udp->parsePacket();
if (pending >= 0) { if (packetSize) {
size_t numBytes = data.nbIOT->socketReceive(data.sock, packetBuffer, pending); int numBytes = data.udp->read(packetBuffer, 512);
if (pending == 0) {
return;
}
connection_t * connP; connection_t * connP;
connP = connection_find(data.connList, data.sock); connP = connection_find(data.connList, data.udp);
if (connP != nullptr) if (connP != nullptr)
{ {
/* /*
@ -122,7 +120,7 @@ void ArduinoClient::doWorkStep() {
lwm2m_handle_packet(lwm2mH, (uint8_t*)packetBuffer, numBytes, connP); lwm2m_handle_packet(lwm2mH, (uint8_t*)packetBuffer, numBytes, connP);
} }
} }
SerialUSB.println(F("doWorkStep():done")); SerialOut.println(F("doWorkStep():done"));
} }
/** /**
@ -130,7 +128,7 @@ void ArduinoClient::doWorkStep() {
*/ */
ArduinoClient::~ArduinoClient() { ArduinoClient::~ArduinoClient() {
lwm2m_close(lwm2mH); lwm2m_close(lwm2mH);
data.nbIOT->closeSocket(data.sock); data.udp->stop();
connection_free(data.connList); connection_free(data.connList);
free_security_object(objArray[0]); free_security_object(objArray[0]);
@ -159,8 +157,8 @@ void * lwm2m_connect_server(uint16_t secObjInstID, void *userData) {
if (uri == nullptr) return nullptr; if (uri == nullptr) return nullptr;
SerialUSB.print(F("Connecting to ")); SerialOut.print(F("Connecting to "));
SerialUSB.println(uri); SerialOut.println(uri);
// parse uri in the form "coaps://[host]:[port]" // parse uri in the form "coaps://[host]:[port]"
if (0 == strncmp(uri, "coaps://", strlen("coaps://"))) if (0 == strncmp(uri, "coaps://", strlen("coaps://")))
@ -198,26 +196,23 @@ void * lwm2m_connect_server(uint16_t secObjInstID, void *userData) {
*port = 0; *port = 0;
port++; port++;
// IPAddress * remoteIp = new IPAddress(); auto * remoteIp = new IPAddress();
// if(!remoteIp->fromString(host)) { if(!remoteIp->fromString(host)) {
// DNSClient dns; DNSClient dns;
// dns.begin(Ethernet.dnsServerIP()); dns.begin(Ethernet.dnsServerIP());
// dns.getHostByName(host, *remoteIp); dns.getHostByName(host, *remoteIp);
// } }
SerialUSB.print(F("Host is "));
SerialUSB.println(host);
String portStr = port; String portStr = port;
newConnP = connection_create(dataP->connList, dataP->sock, dataP->nbIOT, host, portStr.toInt()); newConnP = connection_create(dataP->connList, dataP->udp, remoteIp, portStr.toInt());
if (newConnP == nullptr) { if (newConnP == nullptr) {
SerialUSB.println(F("Connection creation failed")); SerialOut.println(F("Connection creation failed"));
} }
else { else {
dataP->connList = newConnP; dataP->connList = newConnP;
} }
SerialUSB.println(F("Connection created")); SerialOut.println(F("Connection created"));
lwm2m_free(uri); lwm2m_free(uri);
return (void *)newConnP; return (void *)newConnP;
@ -260,5 +255,5 @@ void lwm2m_close_connection(void *sessionH, void *userData) {
void lwm2m_printf(const char * format, ...) void lwm2m_printf(const char * format, ...)
{ {
SerialUSB.println(format); SerialOut.println(format);
} }

View File

@ -1,10 +1,10 @@
#ifndef WAKAAMA_CLIENT_H_ #ifndef WAKAAMA_CLIENT_H_
#define WAKAAMA_CLIENT_H_ #define WAKAAMA_CLIENT_H_
#include <Udp.h>
#include <EthernetUdp.h> #include <EthernetUdp.h>
#include <Sodaq_nbIOT.h> #include <connection.h>
#include "connection.h" #include <serialout.h>
#define OBJ_COUNT 4 #define OBJ_COUNT 4
@ -24,8 +24,7 @@ typedef struct
{ {
lwm2m_object_t * securityObjP; lwm2m_object_t * securityObjP;
connection_t * connList; connection_t * connList;
Sodaq_nbIOT * nbIOT; EthernetUDP * udp;
int sock;
} client_data_t; } client_data_t;
@ -33,8 +32,7 @@ class ArduinoClient {
public: public:
ArduinoClient(const char *uri) : uri(uri) {}; ArduinoClient(const char *uri) : uri(uri) {};
ArduinoClient(const char *name, const char *uri) : name(name),uri(uri) {}; void init();
void init(Sodaq_nbIOT *nbiot);
~ArduinoClient(); ~ArduinoClient();
void doWorkStep(); void doWorkStep();
private: private: