Compare commits

..

5 Commits

Author SHA1 Message Date
Danyi Dávid
10922666b3 * receiving 0 byte pending packages aswell, some may come from appiot 2018-03-02 22:40:00 +01:00
Dávid Danyi
ab58b96f08 * new constructor added to change the client name
* read bytes should be pending bytes, not buffer size
2018-03-01 16:59:26 +01:00
Dávid Danyi
9673e8679c * buffer size switched to fix 512 2018-03-01 11:13:13 +01:00
Dávid Danyi
953bb4e1d1 * cmakelist fixed
* device id changed to appiot registered name
2018-02-28 13:38:01 +01:00
Dávid Danyi
1fbaed4674 * updated to nbiot driver 2018-02-27 15:04:17 +01:00
2 changed files with 53 additions and 46 deletions

View File

@ -1,6 +1,5 @@
#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>
@ -15,47 +14,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() { void ArduinoClient::init(Sodaq_nbIOT * nbiot) {
SerialOut.println(F("init():start")); SerialUSB.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.udp = new EthernetUDP(); data.nbIOT = nbiot;
data.udp->begin(localPort); data.sock = data.nbIOT->createSocket(localPort);
// init objects // init objects
SerialOut.println(F("*object:security")); SerialUSB.println(F("*object:security"));
objArray[0] = get_security_object(uri); objArray[0] = get_security_object(uri);
if (nullptr == objArray[0]) if (nullptr == objArray[0])
{ {
SerialOut.println(objFail); SerialUSB.println(objFail);
exit(0); exit(0);
} }
data.securityObjP = objArray[0]; data.securityObjP = objArray[0];
SerialOut.println(F("*object:server")); SerialUSB.println(F("*object:server"));
objArray[1] = get_server_object(); objArray[1] = get_server_object();
if (nullptr == objArray[1]) if (nullptr == objArray[1])
{ {
SerialOut.println(objFail); SerialUSB.println(objFail);
exit(0); exit(0);
} }
SerialOut.println(F("*object:device")); SerialUSB.println(F("*object:device"));
objArray[2] = get_object_device(); objArray[2] = get_object_device();
if (nullptr == objArray[2]) if (nullptr == objArray[2])
{ {
SerialOut.println(objFail); SerialUSB.println(objFail);
exit(0); exit(0);
} }
SerialOut.println(F("*object:test")); SerialUSB.println(F("*object:test"));
objArray[3] = get_test_object(); objArray[3] = get_test_object();
if (nullptr == objArray[3]) if (nullptr == objArray[3])
{ {
SerialOut.println(objFail); SerialUSB.println(objFail);
exit(0); exit(0);
} }
@ -63,11 +62,11 @@ void ArduinoClient::init() {
* 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
*/ */
SerialOut.println(F("*lwm2m_init()")); SerialUSB.println(F("*lwm2m_init()"));
lwm2mH = lwm2m_init(&data); lwm2mH = lwm2m_init(&data);
if (NULL == lwm2mH) if (NULL == lwm2mH)
{ {
SerialOut.println(F("lwm2m_init() failed")); SerialUSB.println(F("lwm2m_init() failed"));
exit(0); exit(0);
} }
@ -75,19 +74,19 @@ void ArduinoClient::init() {
* 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
*/ */
SerialOut.println(F("*lwm2m_configure()")); SerialUSB.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)
{ {
SerialOut.println(F("lwm2m_configure() failed")); SerialUSB.println(F("lwm2m_configure() failed"));
exit(0); exit(0);
} }
SerialOut.println(F("init():done")); SerialUSB.println(F("init():done"));
} }
void ArduinoClient::doWorkStep() { void ArduinoClient::doWorkStep() {
SerialOut.println(F("doWorkStep():start")); SerialUSB.println(F("doWorkStep():start"));
/* /*
* This function does two things: * This function does two things:
@ -95,23 +94,26 @@ 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
*/ */
SerialOut.println(F("lwm2m_step()")); SerialUSB.println(F("lwm2m_step()"));
result = lwm2m_step(lwm2mH, &step_delay); result = lwm2m_step(lwm2mH, &step_delay);
if (result != 0) if (result != 0)
{ {
SerialOut.print(F("lwm2m_step() failed")); SerialUSB.print(F("lwm2m_step(): failed with error code "));
SerialOut.println(result); SerialUSB.println(result);
return; return;
} }
// wait for socket event // wait for socket event
SerialOut.println(F("parsePacket()")); SerialUSB.println(F("parsePacket()"));
int packetSize = data.udp->parsePacket(); size_t pending = data.nbIOT->socketBytesPending(data.sock);
if (packetSize) { if (pending >= 0) {
int numBytes = data.udp->read(packetBuffer, 512); size_t numBytes = data.nbIOT->socketReceive(data.sock, packetBuffer, pending);
if (pending == 0) {
return;
}
connection_t * connP; connection_t * connP;
connP = connection_find(data.connList, data.udp); connP = connection_find(data.connList, data.sock);
if (connP != nullptr) if (connP != nullptr)
{ {
/* /*
@ -120,7 +122,7 @@ void ArduinoClient::doWorkStep() {
lwm2m_handle_packet(lwm2mH, (uint8_t*)packetBuffer, numBytes, connP); lwm2m_handle_packet(lwm2mH, (uint8_t*)packetBuffer, numBytes, connP);
} }
} }
SerialOut.println(F("doWorkStep():done")); SerialUSB.println(F("doWorkStep():done"));
} }
/** /**
@ -128,7 +130,7 @@ void ArduinoClient::doWorkStep() {
*/ */
ArduinoClient::~ArduinoClient() { ArduinoClient::~ArduinoClient() {
lwm2m_close(lwm2mH); lwm2m_close(lwm2mH);
data.udp->stop(); data.nbIOT->closeSocket(data.sock);
connection_free(data.connList); connection_free(data.connList);
free_security_object(objArray[0]); free_security_object(objArray[0]);
@ -157,8 +159,8 @@ void * lwm2m_connect_server(uint16_t secObjInstID, void *userData) {
if (uri == nullptr) return nullptr; if (uri == nullptr) return nullptr;
SerialOut.print(F("Connecting to ")); SerialUSB.print(F("Connecting to "));
SerialOut.println(uri); SerialUSB.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://")))
@ -196,23 +198,26 @@ void * lwm2m_connect_server(uint16_t secObjInstID, void *userData) {
*port = 0; *port = 0;
port++; port++;
auto * remoteIp = new IPAddress(); // IPAddress * 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->udp, remoteIp, portStr.toInt()); newConnP = connection_create(dataP->connList, dataP->sock, dataP->nbIOT, host, portStr.toInt());
if (newConnP == nullptr) { if (newConnP == nullptr) {
SerialOut.println(F("Connection creation failed")); SerialUSB.println(F("Connection creation failed"));
} }
else { else {
dataP->connList = newConnP; dataP->connList = newConnP;
} }
SerialOut.println(F("Connection created")); SerialUSB.println(F("Connection created"));
lwm2m_free(uri); lwm2m_free(uri);
return (void *)newConnP; return (void *)newConnP;
@ -255,5 +260,5 @@ void lwm2m_close_connection(void *sessionH, void *userData) {
void lwm2m_printf(const char * format, ...) void lwm2m_printf(const char * format, ...)
{ {
SerialOut.println(format); SerialUSB.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 <connection.h> #include <Sodaq_nbIOT.h>
#include <serialout.h> #include "connection.h"
#define OBJ_COUNT 4 #define OBJ_COUNT 4
@ -24,7 +24,8 @@ typedef struct
{ {
lwm2m_object_t * securityObjP; lwm2m_object_t * securityObjP;
connection_t * connList; connection_t * connList;
EthernetUDP * udp; Sodaq_nbIOT * nbIOT;
int sock;
} client_data_t; } client_data_t;
@ -32,7 +33,8 @@ class ArduinoClient {
public: public:
ArduinoClient(const char *uri) : uri(uri) {}; ArduinoClient(const char *uri) : uri(uri) {};
void init(); ArduinoClient(const char *name, const char *uri) : name(name),uri(uri) {};
void init(Sodaq_nbIOT *nbiot);
~ArduinoClient(); ~ArduinoClient();
void doWorkStep(); void doWorkStep();
private: private: