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