* updated to nbiot driver
This commit is contained in:
parent
b67069fc45
commit
1fbaed4674
@ -7,17 +7,24 @@ add_definitions(-DSerialOut=SerialUSB)
|
|||||||
if(DTLS)
|
if(DTLS)
|
||||||
message(FATAL_ERROR "DTLS option is not supported." )
|
message(FATAL_ERROR "DTLS option is not supported." )
|
||||||
endif()
|
endif()
|
||||||
|
set(ARDUINO_USER_LIBDIR $ENV{HOME}/Arduino/libraries)
|
||||||
|
|
||||||
include(${CMAKE_CURRENT_LIST_DIR}/../wakaama-core/src/wakaama.cmake)
|
include(${CMAKE_CURRENT_LIST_DIR}/../wakaama-arduino-core/src/wakaama.cmake)
|
||||||
include(${CMAKE_CURRENT_LIST_DIR}/../wakaama-shared/src/arduino-base.cmake)
|
include(${CMAKE_CURRENT_LIST_DIR}/../wakaama-arduino-connection/src/arduino-base.cmake)
|
||||||
include(${CMAKE_CURRENT_LIST_DIR}/../wakaama-shared/src/shared.cmake)
|
include(${CMAKE_CURRENT_LIST_DIR}/../wakaama-arduino-connection/src/shared.cmake)
|
||||||
|
|
||||||
add_definitions(-DLWM2M_CLIENT_MODE)
|
add_definitions(-DLWM2M_CLIENT_MODE)
|
||||||
add_definitions(${SHARED_DEFINITIONS} ${WAKAAMA_DEFINITIONS})
|
add_definitions(${SHARED_DEFINITIONS} ${WAKAAMA_DEFINITIONS})
|
||||||
|
|
||||||
include_directories (${WAKAAMA_SOURCES_DIR} ${SHARED_INCLUDE_DIRS} ${ARDUINO_INCLUDE_DIRS})
|
include_directories(
|
||||||
|
${WAKAAMA_SOURCES_DIR}
|
||||||
|
${SHARED_INCLUDE_DIRS}
|
||||||
|
${ARDUINO_INCLUDE_DIRS}
|
||||||
|
${ARDUINO_USER_LIBDIR}/Sodaq_wdt/src
|
||||||
|
${ARDUINO_USER_LIBDIR}/Sodaq_nbIOT/src
|
||||||
|
)
|
||||||
|
|
||||||
SET(SOURCES
|
set(SOURCES
|
||||||
${CMAKE_CURRENT_LIST_DIR}/wakaama-client.cpp
|
${CMAKE_CURRENT_LIST_DIR}/wakaama-client.cpp
|
||||||
${CMAKE_CURRENT_LIST_DIR}/object_security.c
|
${CMAKE_CURRENT_LIST_DIR}/object_security.c
|
||||||
${CMAKE_CURRENT_LIST_DIR}/object_server.c
|
${CMAKE_CURRENT_LIST_DIR}/object_server.c
|
||||||
|
|||||||
@ -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,22 +94,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
|
||||||
*/
|
*/
|
||||||
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 "));
|
||||||
exit(0);
|
SerialUSB.println(result);
|
||||||
|
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, UDP_TX_PACKET_MAX_SIZE);
|
size_t numBytes = data.nbIOT->socketReceive(data.sock, packetBuffer, UDP_TX_PACKET_MAX_SIZE);
|
||||||
|
|
||||||
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)
|
||||||
{
|
{
|
||||||
/*
|
/*
|
||||||
@ -119,7 +119,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"));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -127,7 +127,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]);
|
||||||
@ -156,8 +156,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://")))
|
||||||
@ -195,23 +195,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;
|
||||||
@ -254,5 +257,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);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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,13 +33,13 @@ class ArduinoClient {
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
ArduinoClient(const char *uri) : uri(uri) {};
|
ArduinoClient(const char *uri) : uri(uri) {};
|
||||||
void init();
|
void init(Sodaq_nbIOT *nbiot);
|
||||||
~ArduinoClient();
|
~ArduinoClient();
|
||||||
void doWorkStep();
|
void doWorkStep();
|
||||||
private:
|
private:
|
||||||
const char * uri;
|
const char * uri;
|
||||||
const uint16_t localPort = 56830;
|
const uint16_t localPort = 56830;
|
||||||
const char * name = "wakaamArduino";
|
const char * name = "arduino_wakaama_client";
|
||||||
time_t step_delay = 60;
|
time_t step_delay = 60;
|
||||||
int result;
|
int result;
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user