Compare commits
1 Commits
master
...
udp-driver
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
65e6ea1156 |
@ -17,19 +17,21 @@
|
||||
*******************************************************************************/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <Sodaq_nbIOT.h>
|
||||
#include <Dns.h>
|
||||
#include <Ethernet.h>
|
||||
#include <IPAddress.h>
|
||||
#include "connection.h"
|
||||
|
||||
|
||||
connection_t * connection_find(connection_t * connList,
|
||||
int sock)
|
||||
EthernetUDP * udpConnection)
|
||||
{
|
||||
connection_t * connP;
|
||||
|
||||
connP = connList;
|
||||
while (connP != nullptr)
|
||||
{
|
||||
if (connP->sock == sock)
|
||||
if (connP->udpConnection == udpConnection)
|
||||
{
|
||||
return connP;
|
||||
}
|
||||
@ -41,9 +43,8 @@ connection_t * connection_find(connection_t * connList,
|
||||
|
||||
|
||||
connection_t * connection_new_incoming(connection_t * connList,
|
||||
int sock,
|
||||
Sodaq_nbIOT * nbIOT,
|
||||
char * remoteIpStr,
|
||||
EthernetUDP * udpConnection,
|
||||
IPAddress * remoteIp,
|
||||
uint16_t port)
|
||||
{
|
||||
connection_t * connP;
|
||||
@ -51,33 +52,32 @@ connection_t * connection_new_incoming(connection_t * connList,
|
||||
connP = (connection_t *)malloc(sizeof(connection_t));
|
||||
if (connP != nullptr)
|
||||
{
|
||||
int remoteIpLen = strlen(remoteIpStr)+1;
|
||||
connP->remoteIpStr = (char*)malloc(remoteIpLen);
|
||||
// memset(connP->remoteIpStr, 0, remoteIpLen);
|
||||
strncpy(connP->remoteIpStr, remoteIpStr, remoteIpLen);
|
||||
|
||||
connP->nbIOT = nbIOT;
|
||||
connP->sock = sock;
|
||||
connP->udpConnection = udpConnection;
|
||||
connP->remoteIp = remoteIp;
|
||||
connP->port = port;
|
||||
connP->next = connList;
|
||||
|
||||
SerialUSB.print(F("connP->remoteIpStr: "));
|
||||
SerialUSB.println(connP->remoteIpStr);
|
||||
}
|
||||
|
||||
return connP;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @todo add some sort of sanity checking to see if its possible to connect
|
||||
* @param connList
|
||||
* @param udpConnection
|
||||
* @param remoteIp
|
||||
* @param port
|
||||
* @return
|
||||
*/
|
||||
connection_t * connection_create(connection_t * connList,
|
||||
int sock,
|
||||
Sodaq_nbIOT * nbIOT,
|
||||
char * remoteIpStr,
|
||||
EthernetUDP * udpConnection,
|
||||
IPAddress * remoteIp,
|
||||
uint16_t port)
|
||||
{
|
||||
connection_t * connP = nullptr;
|
||||
if(*remoteIpStr != NULL) {
|
||||
connP = connection_new_incoming(connList, sock, nbIOT, remoteIpStr, port);
|
||||
|
||||
if(*remoteIp != 0) {
|
||||
connP = connection_new_incoming(connList, udpConnection, remoteIp, port);
|
||||
}
|
||||
return connP;
|
||||
}
|
||||
@ -90,7 +90,6 @@ void connection_free(connection_t * connList)
|
||||
connection_t * nextP;
|
||||
|
||||
nextP = connList->next;
|
||||
free(connList->remoteIpStr);
|
||||
free(connList);
|
||||
|
||||
connList = nextP;
|
||||
@ -102,7 +101,10 @@ int connection_send(connection_t *connP,
|
||||
uint8_t * buffer,
|
||||
size_t length)
|
||||
{
|
||||
connP->nbIOT->sendSocket(connP->sock, connP->remoteIpStr, connP->port, buffer, length);
|
||||
SerialOut.println(F("connection_send() start"));
|
||||
connP->udpConnection->beginPacket(*(connP->remoteIp), connP->port);
|
||||
connP->udpConnection->write(buffer, length);
|
||||
connP->udpConnection->endPacket();
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -116,11 +118,13 @@ uint8_t lwm2m_buffer_send(void * sessionH,
|
||||
|
||||
if (connP == nullptr)
|
||||
{
|
||||
// fprintf(stderr, "#> failed sending %lu bytes, missing connection\r\n", length);
|
||||
return COAP_500_INTERNAL_SERVER_ERROR ;
|
||||
}
|
||||
|
||||
if (-1 == connection_send(connP, buffer, length))
|
||||
{
|
||||
// fprintf(stderr, "#> failed sending %lu bytes\r\n", length);
|
||||
return COAP_500_INTERNAL_SERVER_ERROR ;
|
||||
}
|
||||
|
||||
|
||||
@ -23,6 +23,9 @@
|
||||
#include <sys/stat.h>
|
||||
#include <liblwm2m.h>
|
||||
|
||||
#include <EthernetUdp.h>
|
||||
#include <serialout.h>
|
||||
|
||||
#define LWM2M_STANDARD_PORT_STR "5683"
|
||||
#define LWM2M_STANDARD_PORT 5683
|
||||
#define LWM2M_DTLS_PORT_STR "5684"
|
||||
@ -33,29 +36,26 @@
|
||||
typedef struct _connection_t
|
||||
{
|
||||
struct _connection_t * next;
|
||||
int sock;
|
||||
Sodaq_nbIOT * nbIOT;
|
||||
char * remoteIpStr;
|
||||
EthernetUDP * udpConnection;
|
||||
IPAddress * remoteIp;
|
||||
uint16_t port;
|
||||
} connection_t;
|
||||
|
||||
//int create_socket(const char * portStr, int ai_family);
|
||||
|
||||
connection_t * connection_find(connection_t * connList,
|
||||
int sock);
|
||||
EthernetUDP * udpConnection);
|
||||
connection_t * connection_new_incoming(connection_t * connList,
|
||||
int sock,
|
||||
Sodaq_nbIOT * nbIOT,
|
||||
char * remoteIp,
|
||||
EthernetUDP * udpConnection,
|
||||
IPAddress * remoteIp,
|
||||
uint16_t port);
|
||||
connection_t * connection_create(connection_t * connList,
|
||||
int sock,
|
||||
Sodaq_nbIOT * nbIOT,
|
||||
char * remoteIp,
|
||||
EthernetUDP * udpConnection,
|
||||
IPAddress * remoteIp,
|
||||
uint16_t port);
|
||||
|
||||
void connection_free(connection_t * connList);
|
||||
|
||||
int connection_send(connection_t *connP, char * buffer, size_t length);
|
||||
int connection_send(connection_t *connP, uint8_t * buffer, size_t length);
|
||||
|
||||
#endif
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user