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 <liblwm2m.h>
#include <USB/USBAPI.h>
#include <Dns.h>
#include <Ethernet.h>
@ -14,47 +15,47 @@ extern void lwm2m_printf(const char * format, ...);
* @param uri
* @todo init and use uri in security object
*/
void ArduinoClient::init(Sodaq_nbIOT * nbiot) {
SerialUSB.println(F("init():start"));
void ArduinoClient::init() {
SerialOut.println(F("init():start"));
memset(&data, 0, sizeof(client_data_t));
const __FlashStringHelper * objFail = F("Failed to create object");
// create udp listener
data.nbIOT = nbiot;
data.sock = data.nbIOT->createSocket(localPort);
data.udp = new EthernetUDP();
data.udp->begin(localPort);
// init objects
SerialUSB.println(F("*object:security"));
SerialOut.println(F("*object:security"));
objArray[0] = get_security_object(uri);
if (nullptr == objArray[0])
{
SerialUSB.println(objFail);
SerialOut.println(objFail);
exit(0);
}
data.securityObjP = objArray[0];
SerialUSB.println(F("*object:server"));
SerialOut.println(F("*object:server"));
objArray[1] = get_server_object();
if (nullptr == objArray[1])
{
SerialUSB.println(objFail);
SerialOut.println(objFail);
exit(0);
}
SerialUSB.println(F("*object:device"));
SerialOut.println(F("*object:device"));
objArray[2] = get_object_device();
if (nullptr == objArray[2])
{
SerialUSB.println(objFail);
SerialOut.println(objFail);
exit(0);
}
SerialUSB.println(F("*object:test"));
SerialOut.println(F("*object:test"));
objArray[3] = get_test_object();
if (nullptr == objArray[3])
{
SerialUSB.println(objFail);
SerialOut.println(objFail);
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
* charge of communication
*/
SerialUSB.println(F("*lwm2m_init()"));
SerialOut.println(F("*lwm2m_init()"));
lwm2mH = lwm2m_init(&data);
if (NULL == lwm2mH)
{
SerialUSB.println(F("lwm2m_init() failed"));
SerialOut.println(F("lwm2m_init() failed"));
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 -
* 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);
if (result != 0)
{
SerialUSB.println(F("lwm2m_configure() failed"));
SerialOut.println(F("lwm2m_configure() failed"));
exit(0);
}
SerialUSB.println(F("init():done"));
SerialOut.println(F("init():done"));
}
void ArduinoClient::doWorkStep() {
SerialUSB.println(F("doWorkStep():start"));
SerialOut.println(F("doWorkStep():start"));
/*
* 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
* (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);
if (result != 0)
{
SerialUSB.print(F("lwm2m_step(): failed with error code "));
SerialUSB.println(result);
SerialOut.print(F("lwm2m_step() failed"));
SerialOut.println(result);
return;
}
// wait for socket event
SerialUSB.println(F("parsePacket()"));
size_t pending = data.nbIOT->socketBytesPending(data.sock);
if (pending >= 0) {
size_t numBytes = data.nbIOT->socketReceive(data.sock, packetBuffer, pending);
if (pending == 0) {
return;
}
SerialOut.println(F("parsePacket()"));
int packetSize = data.udp->parsePacket();
if (packetSize) {
int numBytes = data.udp->read(packetBuffer, 512);
connection_t * connP;
connP = connection_find(data.connList, data.sock);
connP = connection_find(data.connList, data.udp);
if (connP != nullptr)
{
/*
@ -122,7 +120,7 @@ void ArduinoClient::doWorkStep() {
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() {
lwm2m_close(lwm2mH);
data.nbIOT->closeSocket(data.sock);
data.udp->stop();
connection_free(data.connList);
free_security_object(objArray[0]);
@ -159,8 +157,8 @@ void * lwm2m_connect_server(uint16_t secObjInstID, void *userData) {
if (uri == nullptr) return nullptr;
SerialUSB.print(F("Connecting to "));
SerialUSB.println(uri);
SerialOut.print(F("Connecting to "));
SerialOut.println(uri);
// parse uri in the form "coaps://[host]:[port]"
if (0 == strncmp(uri, "coaps://", strlen("coaps://")))
@ -198,26 +196,23 @@ void * lwm2m_connect_server(uint16_t secObjInstID, void *userData) {
*port = 0;
port++;
// IPAddress * remoteIp = new IPAddress();
// if(!remoteIp->fromString(host)) {
// DNSClient dns;
// dns.begin(Ethernet.dnsServerIP());
// dns.getHostByName(host, *remoteIp);
// }
SerialUSB.print(F("Host is "));
SerialUSB.println(host);
auto * remoteIp = new IPAddress();
if(!remoteIp->fromString(host)) {
DNSClient dns;
dns.begin(Ethernet.dnsServerIP());
dns.getHostByName(host, *remoteIp);
}
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) {
SerialUSB.println(F("Connection creation failed"));
SerialOut.println(F("Connection creation failed"));
}
else {
dataP->connList = newConnP;
}
SerialUSB.println(F("Connection created"));
SerialOut.println(F("Connection created"));
lwm2m_free(uri);
return (void *)newConnP;
@ -260,5 +255,5 @@ void lwm2m_close_connection(void *sessionH, void *userData) {
void lwm2m_printf(const char * format, ...)
{
SerialUSB.println(format);
SerialOut.println(format);
}

View File

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