2017-10-08 16:46:14 +02:00
|
|
|
/*******************************************************************************
|
|
|
|
|
*
|
|
|
|
|
* Copyright (c) 2013, 2014 Intel Corporation and others.
|
|
|
|
|
* All rights reserved. This program and the accompanying materials
|
|
|
|
|
* are made available under the terms of the Eclipse Public License v1.0
|
|
|
|
|
* and Eclipse Distribution License v1.0 which accompany this distribution.
|
|
|
|
|
*
|
|
|
|
|
* The Eclipse Public License is available at
|
|
|
|
|
* http://www.eclipse.org/legal/epl-v10.html
|
|
|
|
|
* The Eclipse Distribution License is available at
|
|
|
|
|
* http://www.eclipse.org/org/documents/edl-v10.php.
|
|
|
|
|
*
|
|
|
|
|
* Contributors:
|
|
|
|
|
* David Navarro, Intel Corporation - initial API and implementation
|
|
|
|
|
* Pascal Rieux - Please refer to git log
|
|
|
|
|
*
|
|
|
|
|
*******************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <Dns.h>
|
|
|
|
|
#include <Ethernet.h>
|
|
|
|
|
#include <IPAddress.h>
|
|
|
|
|
#include "connection.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
connection_t * connection_find(connection_t * connList,
|
|
|
|
|
EthernetUDP * udpConnection)
|
|
|
|
|
{
|
|
|
|
|
connection_t * connP;
|
|
|
|
|
|
|
|
|
|
connP = connList;
|
|
|
|
|
while (connP != nullptr)
|
|
|
|
|
{
|
|
|
|
|
if (connP->udpConnection == udpConnection)
|
|
|
|
|
{
|
|
|
|
|
return connP;
|
|
|
|
|
}
|
|
|
|
|
connP = connP->next;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return connP;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
connection_t * connection_new_incoming(connection_t * connList,
|
|
|
|
|
EthernetUDP * udpConnection,
|
|
|
|
|
IPAddress * remoteIp,
|
|
|
|
|
uint16_t port)
|
|
|
|
|
{
|
|
|
|
|
connection_t * connP;
|
|
|
|
|
|
|
|
|
|
connP = (connection_t *)malloc(sizeof(connection_t));
|
|
|
|
|
if (connP != nullptr)
|
|
|
|
|
{
|
|
|
|
|
connP->udpConnection = udpConnection;
|
|
|
|
|
connP->remoteIp = remoteIp;
|
|
|
|
|
connP->port = port;
|
|
|
|
|
connP->next = connList;
|
|
|
|
|
SerialUSB.println((uint32_t)udpConnection);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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,
|
|
|
|
|
EthernetUDP * udpConnection,
|
|
|
|
|
IPAddress * remoteIp,
|
|
|
|
|
uint16_t port)
|
|
|
|
|
{
|
|
|
|
|
connection_t * connP = nullptr;
|
|
|
|
|
|
|
|
|
|
if(*remoteIp != 0) {
|
|
|
|
|
connP = connection_new_incoming(connList, udpConnection, remoteIp, port);
|
|
|
|
|
}
|
|
|
|
|
return connP;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void connection_free(connection_t * connList)
|
|
|
|
|
{
|
|
|
|
|
while (connList != nullptr)
|
|
|
|
|
{
|
|
|
|
|
connection_t * nextP;
|
|
|
|
|
|
|
|
|
|
nextP = connList->next;
|
|
|
|
|
free(connList);
|
|
|
|
|
|
|
|
|
|
connList = nextP;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int connection_send(connection_t *connP,
|
|
|
|
|
uint8_t * buffer,
|
|
|
|
|
size_t length)
|
|
|
|
|
{
|
2017-10-10 08:33:02 +02:00
|
|
|
SerialUSB.println(F("connection_send() start"));
|
|
|
|
|
connP->udpConnection->beginPacket(*(connP->remoteIp), connP->port);
|
2017-10-08 16:46:14 +02:00
|
|
|
connP->udpConnection->write(buffer, length);
|
|
|
|
|
connP->udpConnection->endPacket();
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
uint8_t lwm2m_buffer_send(void * sessionH,
|
|
|
|
|
uint8_t * buffer,
|
|
|
|
|
size_t length,
|
|
|
|
|
void * userdata)
|
|
|
|
|
{
|
|
|
|
|
connection_t * connP = (connection_t*) 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 ;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return COAP_NO_ERROR;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool lwm2m_session_is_equal(void * session1,
|
|
|
|
|
void * session2,
|
|
|
|
|
void * userData)
|
|
|
|
|
{
|
|
|
|
|
return (session1 == session2);
|
|
|
|
|
}
|