Lwip Client Echo Application [SOLVED]

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Lwip Client Echo Application [SOLVED]

2,863 Views
ramazan_kurban3
Contributor III

Hi. I use lpc1769 custom board and MCUxpresso.

I need to convert Lwip_tcpecho_sa server application to client application or  get new lwip client echo application. 

Is there any lwip client application. 

How can ı do ?

0 Kudos
4 Replies

2,514 Views
ramazan_kurban3
Contributor III

ADD FOR CLIENT THIS CODES TO ECHO.C IN LPCOPEN LWIP_TCPECHO_SA EXAMPLE

void tcp_echoclient_connect(void)
{
struct ip_addr DestIPaddr;

if (echo_pcb != NULL)
{
IP4_ADDR( &DestIPaddr, 192, 168, 1, 43 );

/* connect to destination address/port */
tcp_connect(echo_pcb,&DestIPaddr,7,echo_accept);
}
else
{
/* deallocate the pcb */
memp_free(MEMP_TCP_PCB, echo_pcb);
// DEBUGOUT("\n\r can not create tcp pcb");

}
}


void
echo_init(void)
{
echo_pcb = tcp_new();
if (echo_pcb != NULL)
{
err_t err;

err = tcp_bind(echo_pcb, IP_ADDR_ANY, 7);
if (err == ERR_OK)
{
// FOR SERVER
// echo_pcb = tcp_listen(echo_pcb);
// tcp_accept(echo_pcb, echo_accept);

// FOR CLİENT
tcp_echoclient_connect();
}
else
{
/* abort? output diagnostic? */
}
}
else
{
/* abort? output diagnostic? */
}
}

0 Kudos

2,514 Views
ramazan_kurban3
Contributor III

I changed echo_init(); function with  tcp_echoclient_connect();

now ı can send data to server but cant get data from server. When ı send data from hercules (server application), hercules shutdown.

When client connect to hercules(server),client count i set -1 and if ı reset my board, this number increse to -2 -3 -4 ....

How can ı get data from server ? What is other problems ?

void tcp_echoclient_connect(void)
{
struct ip_addr DestIPaddr;

/* create new tcp pcb */
echoclient_pcb = tcp_new();

if (echoclient_pcb != NULL)
{
IP4_ADDR( &DestIPaddr, DEST_IP_ADDR0, DEST_IP_ADDR1, DEST_IP_ADDR2, DEST_IP_ADDR3 );

/* connect to destination address/port */
tcp_connect(echoclient_pcb,&DestIPaddr,DEST_PORT,tcp_echoclient_connected);
}
else
{
/* deallocate the pcb */
memp_free(MEMP_TCP_PCB, echoclient_pcb);
#ifdef SERIAL_DEBUG
printf("\n\r can not create tcp pcb");
#endif
}
}

0 Kudos

2,514 Views
Alexis_A
NXP TechSupport
NXP TechSupport

Hi Ramazan,

At code level there's not much differences between a client and server application, usually the first one that make the connection is considered a client.

So you could base your application in the lwip_tcpecho_sa/freertos.

Best Regards,

Alexis Andlaon

0 Kudos

1,308 Views
Roua
Contributor I

Hello Alexis,

hope you doing well 

I am working on MK20DX256xxx7 I have Problem when I need to connect my code to server this the configration of lwip and the configuration of my project I don't Know exactly where the problem configuration seem good 

/**
* @file
* @defgroup altcp Application layered TCP Functions
* @ingroup altcp_api
*
* This file contains the common functions for altcp to work.
* For more details see @ref altcp_api.
*/

/**
* @defgroup altcp_api Application layered TCP Introduction
* @ingroup callbackstyle_api
*
* Overview
* --------
* altcp (application layered TCP connection API; to be used from TCPIP thread)
* is an abstraction layer that prevents applications linking hard against the
* @ref tcp.h functions while providing the same functionality. It is used to
* e.g. add SSL/TLS (see LWIP_ALTCP_TLS) or proxy-connect support to an application
* written for the tcp callback API without that application knowing the
* protocol details.
*
* * This interface mimics the tcp callback API to the application while preventing
* direct linking (much like virtual functions).
* * This way, an application can make use of other application layer protocols
* on top of TCP without knowing the details (e.g. TLS, proxy connection).
* * This is achieved by simply including "lwip/altcp.h" instead of "lwip/tcp.h",
* replacing "struct tcp_pcb" with "struct altcp_pcb" and prefixing all functions
* with "altcp_" instead of "tcp_".
*
* With altcp support disabled (LWIP_ALTCP==0), applications written against the
* altcp API can still be compiled but are directly linked against the tcp.h
* callback API and then cannot use layered protocols. To minimize code changes
* in this case, the use of altcp_allocators is strongly suggested.
*
* Usage
* -----
* To make use of this API from an existing tcp raw API application:
* * Include "lwip/altcp.h" instead of "lwip/tcp.h"
* * Replace "struct tcp_pcb" with "struct altcp_pcb"
* * Prefix all called tcp API functions with "altcp_" instead of "tcp_" to link
* against the altcp functions
* * @ref altcp_new (and @ref altcp_new_ip_type/@ref altcp_new_ip6) take
* an @ref altcp_allocator_t as an argument, whereas the original tcp API
* functions take no arguments.
* * An @ref altcp_allocator_t allocator is an object that holds a pointer to an
* allocator object and a corresponding state (e.g. for TLS, the corresponding
* state may hold certificates or keys). This way, the application does not
* even need to know if it uses TLS or pure TCP, this is handled at runtime
* by passing a specific allocator.
* * An application can alternatively bind hard to the altcp_tls API by calling
* @ref altcp_tls_new or @ref altcp_tls_wrap.
* * The TLS layer is not directly implemented by lwIP, but a port to mbedTLS is
* provided.
* * Another altcp layer is proxy-connect to use TLS behind a HTTP proxy (see
* @ref altcp_proxyconnect.h)
*
* altcp_allocator_t
* -----------------
* An altcp allocator is created by the application by combining an allocator
* callback function and a corresponding state, e.g.:\code{.c}
* static const unsigned char cert[] = {0x2D, ... (see mbedTLS doc for how to create this)};
* struct altcp_tls_config * conf = altcp_tls_create_config_client(cert, sizeof(cert));
* altcp_allocator_t tls_allocator = {
* altcp_tls_alloc, conf
* };
* \endcode
*
*
* struct altcp_tls_config
* -----------------------
* The struct altcp_tls_config holds state that is needed to create new TLS client
* or server connections (e.g. certificates and private keys).
*
* It is not defined by lwIP itself but by the TLS port (e.g. altcp_tls to mbedTLS
* adaption). However, the parameters used to create it are defined in @ref
* altcp_tls.h (see @ref altcp_tls_create_config_server_privkey_cert for servers
* and @ref altcp_tls_create_config_client/@ref altcp_tls_create_config_client_2wayauth
* for clients).
*
* For mbedTLS, ensure that certificates can be parsed by 'mbedtls_x509_crt_parse()' and
* private keys can be parsed by 'mbedtls_pk_parse_key()'.
*/

/*
* Copyright (c) 2017 Simon Goldschmidt
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE.
*
* This file is part of the lwIP TCP/IP stack.
*
* Author: Simon Goldschmidt <goldsimon@gmx.de>
*
*/

#include "lwip/opt.h"

#if LWIP_ALTCP /* don't build if not configured for use in lwipopts.h */

#include "lwip/altcp.h"
#include "lwip/priv/altcp_priv.h"
#include "lwip/altcp_tcp.h"
#include "lwip/tcp.h"
#include "lwip/mem.h"

#include <string.h>

extern const struct altcp_functions altcp_tcp_functions;

/**
* For altcp layer implementations only: allocate a new struct altcp_pcb from the pool
* and zero the memory
*/
struct altcp_pcb *
altcp_alloc(void)
{
struct altcp_pcb *ret = (struct altcp_pcb *)memp_malloc(MEMP_ALTCP_PCB);
if (ret != NULL) {
memset(ret, 0, sizeof(struct altcp_pcb));
}
return ret;
}

/**
* For altcp layer implementations only: return a struct altcp_pcb to the pool
*/
void
altcp_free(struct altcp_pcb *conn)
{
if (conn) {
if (conn->fns && conn->fns->dealloc) {
conn->fns->dealloc(conn);
}
memp_free(MEMP_ALTCP_PCB, conn);
}
}

/**
* @ingroup altcp
* altcp_new_ip6: @ref altcp_new for IPv6
*/
struct altcp_pcb *
altcp_new_ip6(altcp_allocator_t *allocator)
{
return altcp_new_ip_type(allocator, IPADDR_TYPE_V6);
}

/**
* @ingroup altcp
* altcp_new: @ref altcp_new for IPv4
*/
struct altcp_pcb *
altcp_new(altcp_allocator_t *allocator)
{
return altcp_new_ip_type(allocator, IPADDR_TYPE_V4);
}

/**
* @ingroup altcp
* altcp_new_ip_type: called by applications to allocate a new pcb with the help of an
* allocator function.
*
* @Param allocator allocator function and argument
* @Param ip_type IP version of the pcb (@ref lwip_ip_addr_type)
* @return a new altcp_pcb or NULL on error
*/
struct altcp_pcb *
altcp_new_ip_type(altcp_allocator_t *allocator, u8_t ip_type)
{
struct altcp_pcb *conn;
if (allocator == NULL) {
/* no allocator given, create a simple TCP connection */
return altcp_tcp_new_ip_type(ip_type);
}
if (allocator->alloc == NULL) {
/* illegal allocator */
return NULL;
}
conn = allocator->alloc(allocator->arg, ip_type);
if (conn == NULL) {
/* allocation failed */
return NULL;
}
return conn;
}

/**
* @ingroup altcp
* @see tcp_arg()
*/
void
altcp_arg(struct altcp_pcb *conn, void *arg)
{
if (conn) {
conn->arg = arg;
}
}

/**
* @ingroup altcp
* @see tcp_accept()
*/
void
altcp_accept(struct altcp_pcb *conn, altcp_accept_fn accept)
{
if (conn != NULL) {
conn->accept = accept;
}
}

/**
* @ingroup altcp
* @see tcp_recv()
*/
void
altcp_recv(struct altcp_pcb *conn, altcp_recv_fn recv)
{
if (conn) {
conn->recv = recv;
}
}

/**
* @ingroup altcp
* @see tcp_sent()
*/
void
altcp_sent(struct altcp_pcb *conn, altcp_sent_fn sent)
{
if (conn) {
conn->sent = sent;
}
}

/**
* @ingroup altcp
* @see tcp_poll()
*/
void
altcp_poll(struct altcp_pcb *conn, altcp_poll_fn poll, u8_t interval)
{
if (conn) {
conn->poll = poll;
conn->pollinterval = interval;
if (conn->fns && conn->fns->set_poll) {
conn->fns->set_poll(conn, interval);
}
}
}

/**
* @ingroup altcp
* @see tcp_err()
*/
void
altcp_err(struct altcp_pcb *conn, altcp_err_fn err)
{
if (conn) {
conn->err = err;
}
}

/* Generic functions calling the "virtual" ones */

/**
* @ingroup altcp
* @see tcp_recved()
*/
void
altcp_recved(struct altcp_pcb *conn, u16_t len)
{
if (conn && conn->fns && conn->fns->recved) {
conn->fns->recved(conn, len);
}
}

/**
* @ingroup altcp
* @see tcp_bind()
*/
err_t
altcp_bind(struct altcp_pcb *conn, const ip_addr_t *ipaddr, u16_t port)
{
if (conn && conn->fns && conn->fns->bind) {
return conn->fns->bind(conn, ipaddr, port);
}
return ERR_LWIP_VAL;
}

/**
* @ingroup altcp
* @see tcp_connect()
*/
err_t
altcp_connect(struct altcp_pcb *conn, const ip_addr_t *ipaddr, u16_t port, altcp_connected_fn connected)
{
if (conn && conn->fns && conn->fns->connect) {
return conn->fns->connect(conn, ipaddr, port, connected);
}
return ERR_LWIP_VAL;
}

/**
* @ingroup altcp
* @see tcp_listen_with_backlog_and_err()
*/
struct altcp_pcb *
altcp_listen_with_backlog_and_err(struct altcp_pcb *conn, u8_t backlog, err_t *err)
{
if (conn && conn->fns && conn->fns->listen) {
return conn->fns->listen(conn, backlog, err);
}
return NULL;
}

/**
* @ingroup altcp
* @see tcp_abort()
*/
void
altcp_abort(struct altcp_pcb *conn)
{
if (conn && conn->fns && conn->fns->abort) {
conn->fns->abort(conn);
}
}

/**
* @ingroup altcp
* @see tcp_close()
*/
err_t
altcp_close(struct altcp_pcb *conn)
{
if (conn && conn->fns && conn->fns->close) {
return conn->fns->close(conn);
}
return ERR_LWIP_VAL;
}

/**
* @ingroup altcp
* @see tcp_shutdown()
*/
err_t
altcp_shutdown(struct altcp_pcb *conn, int shut_rx, int shut_tx)
{
if (conn && conn->fns && conn->fns->shutdown) {
return conn->fns->shutdown(conn, shut_rx, shut_tx);
}
return ERR_LWIP_VAL;
}

/**
* @ingroup altcp
* @see tcp_write()
*/
err_t
altcp_write(struct altcp_pcb *conn, const void *dataptr, u16_t len, u8_t apiflags)
{
if (conn && conn->fns && conn->fns->write) {
return conn->fns->write(conn, dataptr, len, apiflags);
}
return ERR_LWIP_VAL;
}

/**
* @ingroup altcp
* @see tcp_output()
*/
err_t
altcp_output(struct altcp_pcb *conn)
{
if (conn && conn->fns && conn->fns->output) {
return conn->fns->output(conn);
}
return ERR_LWIP_VAL;
}

/**
* @ingroup altcp
* @see tcp_mss()
*/
u16_t
altcp_mss(struct altcp_pcb *conn)
{
if (conn && conn->fns && conn->fns->mss) {
return conn->fns->mss(conn);
}
return 0;
}

/**
* @ingroup altcp
* @see tcp_sndbuf()
*/
u16_t
altcp_sndbuf(struct altcp_pcb *conn)
{
if (conn && conn->fns && conn->fns->sndbuf) {
return conn->fns->sndbuf(conn);
}
return 0;
}

/**
* @ingroup altcp
* @see tcp_sndqueuelen()
*/
u16_t
altcp_sndqueuelen(struct altcp_pcb *conn)
{
if (conn && conn->fns && conn->fns->sndqueuelen) {
return conn->fns->sndqueuelen(conn);
}
return 0;
}

void
altcp_nagle_disable(struct altcp_pcb *conn)
{
if (conn && conn->fns && conn->fns->nagle_disable) {
conn->fns->nagle_disable(conn);
}
}

void
altcp_nagle_enable(struct altcp_pcb *conn)
{
if (conn && conn->fns && conn->fns->nagle_enable) {
conn->fns->nagle_enable(conn);
}
}

int
altcp_nagle_disabled(struct altcp_pcb *conn)
{
if (conn && conn->fns && conn->fns->nagle_disabled) {
return conn->fns->nagle_disabled(conn);
}
return 0;
}

/**
* @ingroup altcp
* @see tcp_setprio()
*/
void
altcp_setprio(struct altcp_pcb *conn, u8_t prio)
{
if (conn && conn->fns && conn->fns->setprio) {
conn->fns->setprio(conn, prio);
}
}

err_t
altcp_get_tcp_addrinfo(struct altcp_pcb *conn, int local, ip_addr_t *addr, u16_t *port)
{
if (conn && conn->fns && conn->fns->addrinfo) {
return conn->fns->addrinfo(conn, local, addr, port);
}
return ERR_LWIP_VAL;
}

ip_addr_t *
altcp_get_ip(struct altcp_pcb *conn, int local)
{
if (conn && conn->fns && conn->fns->getip) {
return conn->fns->getip(conn, local);
}
return NULL;
}

u16_t
altcp_get_port(struct altcp_pcb *conn, int local)
{
if (conn && conn->fns && conn->fns->getport) {
return conn->fns->getport(conn, local);
}
return 0;
}

#ifdef LWIP_DEBUG
enum tcp_state
altcp_dbg_get_tcp_state(struct altcp_pcb *conn)
{
if (conn && conn->fns && conn->fns->dbg_get_tcp_state) {
return conn->fns->dbg_get_tcp_state(conn);
}
return CLOSED;
}
#endif

/* Default implementations for the "virtual" functions */

void
altcp_default_set_poll(struct altcp_pcb *conn, u8_t interval)
{
if (conn && conn->inner_conn) {
altcp_poll(conn->inner_conn, conn->poll, interval);
}
}

void
altcp_default_recved(struct altcp_pcb *conn, u16_t len)
{
if (conn && conn->inner_conn) {
altcp_recved(conn->inner_conn, len);
}
}

err_t
altcp_default_bind(struct altcp_pcb *conn, const ip_addr_t *ipaddr, u16_t port)
{
if (conn && conn->inner_conn) {
return altcp_bind(conn->inner_conn, ipaddr, port);
}
return ERR_LWIP_VAL;
}

err_t
altcp_default_shutdown(struct altcp_pcb *conn, int shut_rx, int shut_tx)
{
if (conn) {
if (shut_rx && shut_tx && conn->fns && conn->fns->close) {
/* default shutdown for both sides is close */
return conn->fns->close(conn);
}
if (conn->inner_conn) {
return altcp_shutdown(conn->inner_conn, shut_rx, shut_tx);
}
}
return ERR_LWIP_VAL;
}

err_t
altcp_default_write(struct altcp_pcb *conn, const void *dataptr, u16_t len, u8_t apiflags)
{
if (conn && conn->inner_conn) {
return altcp_write(conn->inner_conn, dataptr, len, apiflags);
}
return ERR_LWIP_VAL;
}

err_t
altcp_default_output(struct altcp_pcb *conn)
{
if (conn && conn->inner_conn) {
return altcp_output(conn->inner_conn);
}
return ERR_LWIP_VAL;
}

u16_t
altcp_default_mss(struct altcp_pcb *conn)
{
if (conn && conn->inner_conn) {
return altcp_mss(conn->inner_conn);
}
return 0;
}

u16_t
altcp_default_sndbuf(struct altcp_pcb *conn)
{
if (conn && conn->inner_conn) {
return altcp_sndbuf(conn->inner_conn);
}
return 0;
}

u16_t
altcp_default_sndqueuelen(struct altcp_pcb *conn)
{
if (conn && conn->inner_conn) {
return altcp_sndqueuelen(conn->inner_conn);
}
return 0;
}

void
altcp_default_nagle_disable(struct altcp_pcb *conn)
{
if (conn && conn->inner_conn) {
altcp_nagle_disable(conn->inner_conn);
}
}

void
altcp_default_nagle_enable(struct altcp_pcb *conn)
{
if (conn && conn->inner_conn) {
altcp_nagle_enable(conn->inner_conn);
}
}

int
altcp_default_nagle_disabled(struct altcp_pcb *conn)
{
if (conn && conn->inner_conn) {
return altcp_nagle_disabled(conn->inner_conn);
}
return 0;
}

void
altcp_default_setprio(struct altcp_pcb *conn, u8_t prio)
{
if (conn && conn->inner_conn) {
altcp_setprio(conn->inner_conn, prio);
}
}

void
altcp_default_dealloc(struct altcp_pcb *conn)
{
LWIP_UNUSED_ARG(conn);
/* nothing to do */
}

err_t
altcp_default_get_tcp_addrinfo(struct altcp_pcb *conn, int local, ip_addr_t *addr, u16_t *port)
{
if (conn && conn->inner_conn) {
return altcp_get_tcp_addrinfo(conn->inner_conn, local, addr, port);
}
return ERR_LWIP_VAL;
}

ip_addr_t *
altcp_default_get_ip(struct altcp_pcb *conn, int local)
{
if (conn && conn->inner_conn) {
return altcp_get_ip(conn->inner_conn, local);
}
return NULL;
}

u16_t
altcp_default_get_port(struct altcp_pcb *conn, int local)
{
if (conn && conn->inner_conn) {
return altcp_get_port(conn->inner_conn, local);
}
return 0;
}

#ifdef LWIP_DEBUG
enum tcp_state
altcp_default_dbg_get_tcp_state(struct altcp_pcb *conn)
{
if (conn && conn->inner_conn) {
return altcp_dbg_get_tcp_state(conn->inner_conn);
}
return CLOSED;
}
#endif


#endif /* LWIP_ALTCP */

/* ###################################################################
** Filename : qoconfig.c
** Project : Qometer
** Processor : MK20DX256VLH7
** Component :
** Version : Driver 01.00
** Compiler : GNU C Compiler
** Date/Time : 2020-03-20, 12:16, # CodeGen: 0
** Abstract :
** Interface to update the config from the server
** Contents :
**
** ###################################################################*/
/*!
** @file qoconfig.c
** @version 01.00
** @brief
** Interface to update the config from the server
*/
/*!
** @{
*/
/* MODULE qoconfig */
/*====================================================================*
* system header files;
*--------------------------------------------------------------------*/
#include <stddef.h>
#include <stdint.h>
#include <string.h>
/*====================================================================*
* qometer header files;
*--------------------------------------------------------------------*/
#include "qometer.h"
#include "qonetif.h"
#include "qoconfig.h"
/*====================================================================*
* lwip header files;
*--------------------------------------------------------------------*/
#include "lwip/opt.h"
#include "lwip/netif.h"
#include "lwip/def.h"
#include "lwip/ip_addr.h"
#include "lwip/err.h"
#include "lwip/apps/http_client.h"
/*====================================================================*
* constante;
*--------------------------------------------------------------------*/

#define URL "qometer-1a797-default-rtdb.firebaseio.com"

#define PING "/ping/"

#define MAX_SIZE_MSG 300
#define TOTAL_POWER "totalPower"
#define FIRMWARE_URL "firmwareUrl"
#define FIRMWARE_VERSION "firmwareVersion"
#define FIRMWARE_CRC "firmwareCrc"
#define MODE_POWER_LIMIT "mode"
#define POWER_LIMIT_SHEDDER "availablePower"
#define THRESHOLD "threshold"
#define LOAD_SHEDDER "LOAD_SHEDDER"
#define REGULATOR "REGULATOR"
/*====================================================================*
* static variable;
*--------------------------------------------------------------------*/
static struct ip4_addr ip_server;
static struct qoconfigResult result;
static httpc_connection_t connSettingsConfig;
/*====================================================================*
* static function;
*--------------------------------------------------------------------*/
static uint8_t qoconfigGetUrl(uint8_t *url, uint32_t sizeUrl);

static err_t RecvHttpHeaderCallbackConfig (httpc_state_t *connection,
void *arg,
struct pbuf *hdr,
uint16_t hdr_len,
uint32_t content_len);

static err_t HttpClientResultCallbackConfig (void *arg,
struct tcp_pcb *tpcb,
struct pbuf *p,
err_t err) ;

/*====================================================================*
* qoupdate functions;
*--------------------------------------------------------------------*/

/**
*
* @brief Function to get thr url for the get config
* @Param url to store thr url
* @Param sizeUrl the size max of the url
* @retval FALSE on success
* @retval TRUE when an error occurs or the space is not enough
*
*/
static uint8_t qoconfigGetUrl(uint8_t *url, uint32_t sizeUrl) {
if(url == NULL)
return TRUE;

uint8_t macAddress[6] = {0};
uint8_t macAddresStr[13] = {0};

/* the ping url format is : /ping/macAddress?crc=0x0000&status=AVAILABLE
mac address : the qometer's mac address
crc : the crc32 of the last answer of the ping
status : status of the qometer*/

/* check if the space is enough */
if(strlen(url) + strlen(PING)>sizeUrl)
return TRUE;

strcat(url,PING);

/* get the mac address */
if(qoflashGetValShared(MAC_ADDRESS,(uint32_t*) macAddress))
return TRUE;
//macAddress[0] = 0x00;
//macAddress[1] = 0x01;
//macAddress[2] = 0x87;
//macAddress[3] = 0x10;
//macAddress[4] = 0x88;
// macAddress[5] = 0xAA;
/*convert the mac in str */
for(uint32_t i=0; i<6; i++){
if(!sprintf((macAddresStr+i*2), "%02x", macAddress[i]))
return TRUE;
}

/* check if the space is enough */
if(strlen(url) + strlen(macAddresStr) + 1 >sizeUrl)
return TRUE;

strcat(url,macAddresStr);

/* check if the space is enough */
if(strlen(url) + strlen("?crc=0x0000&status=AVAILABLE")>sizeUrl)
return TRUE;

strcat(url,"?crc=0x0000&status=AVAILABLE");

return FALSE;
}

 

/**
*
* @brief Send the request HTTP to get the config
* @Param serverName the DNS server name
* @Param port port TCP
* @Param uri uri to get from the server, remember leading "/"!
* @retval QOCONFIG_OK on success
* @retval QOCONFIG_FAILED when failed to send the request HTTP
*
*
*/
enum qoconfigErr qoconfigGetConfigDns(struct qoconfigResult* result) {
int16_t err;
httpc_state_t *connection;
uint8_t url[MAX_SIZE_MSG] = {0};
connSettingsConfig.use_proxy = 0;
connSettingsConfig.result_fn = NULL;
connSettingsConfig.headers_done_fn = RecvHttpHeaderCallbackConfig;

/* Get the URL */
qoconfigGetUrl(url,MAX_SIZE_MSG);

if(!httpc_get_file_dns(QOVOLTIS_SERVER_NAME,
PORT_HTTP,
url,
&connSettingsConfig,
HttpClientResultCallbackConfig,
(void*) result,
&connection)){
result->state = START_GET_CONFIG;
return QOCONFIG_OK;
}
return QOCONFIG_FAILED;
}

/**
* @brief callback : called when the headers are received
* @Param input are definies by lwip
*/
static err_t RecvHttpHeaderCallbackConfig (httpc_state_t *connection,
void *arg,
struct pbuf *hdr,
uint16_t hdr_len,
uint32_t content_len) {
if((hdr == NULL) || (connection == NULL) || (arg == NULL))
return ERR_LWIP_ARG;
struct qoconfigResult* result = (struct qoconfigResult*) arg;
/* Check the answer is OK from server */
if(strstr(hdr->payload,HTTP_OK)) {
result->state = HEADER_OK_GET_CONFIG;
return ERR_LWIP_OK;
}

return ERR_LWIP_ABRT;
}

/**
* @brief callback : called when the data are received
* @Param input are definies by lwip
*/
static err_t HttpClientResultCallbackConfig (void *arg,
struct tcp_pcb *tpcb,
struct pbuf *p,
err_t err) {
struct pbuf *data = (struct pbuf*) p;
char* positionInData = NULL;
struct qoconfigResult* result = (struct qoconfigResult*) arg;
if((data == NULL) || (arg == NULL))
return;

if(result->state != HEADER_OK_GET_CONFIG)
return;
/* parse the parameter : total power */
if(positionInData = strstr((char*)data->payload,TOTAL_POWER)) {
while((*positionInData != '=') && (positionInData != (data->payload + data->tot_len)))
positionInData++;
positionInData++;
result->totalPower = strtol(positionInData,&positionInData,BASE10);
}
else
result->totalPower = 0;

/* Set the new value in the FALSH */
qoflashSetValShared(POWER_LIMIT_CONFIG,result->totalPower);

/* parse the url for firmware update */
if(positionInData = strstr((char*)data->payload,FIRMWARE_URL)) {
while((*positionInData != '/') &&
(positionInData != (data->payload + data->tot_len)))
positionInData ++;

/* parse the server name */
positionInData = positionInData + 2;
uint16_t lenght = 0;
uint32_t index = 0;
while((index<LENGHT_MAX) && ((uint32_t*) positionInData <
(uint32_t*) (data->payload + data->tot_len))
&& (*positionInData != '/')
&& (*positionInData != ':')) {
result->serverName[index++] = *positionInData;
lenght++;
positionInData++;
}
result->serverName[index + 1] = '\0';
result->lenghtServerName = lenght;
/* drop the port */
if(*positionInData == ':'){
while(*positionInData != '/')
positionInData ++;
}

/* parse the url */
lenght = 0;
index = 0;
while((index<LENGHT_MAX) && (*positionInData != '\0') && (*positionInData != '|')
&& ((uint32_t*) positionInData < (uint32_t*) (data->payload + data->tot_len))){
result->uri[index++] = *positionInData;
lenght++;
positionInData ++;
}
result->uri[index + 1] = '\0';
result->lenghtUri = lenght;
}
else
result->lenghtServerName = 0;
/* parse the firmware version */
if(positionInData = strstr((char*)data->payload,FIRMWARE_VERSION)) {
while((*positionInData != '=') && (positionInData != (data->payload + data->tot_len)))
positionInData++;
positionInData++;
uint32_t index = 0;
while((index<LENGHT_MAX) && (*positionInData != '\0') && (*positionInData != '|')
&& ((uint32_t*) positionInData < (uint32_t*) (data->payload + data->tot_len))){
result->firmwareVersion[index++] = *positionInData;
positionInData ++;
}
result->firmwareVersion[index + 1] = '\0';
}
else {
result->firmwareVersion[0] = "-";
result->firmwareVersion[1] = "1";
}
/* parse the CRC firmware */
if(positionInData = strstr((char*)data->payload,FIRMWARE_CRC)) {
while((*positionInData != '=') && (positionInData != (data->payload + data->tot_len)))
positionInData++;
positionInData++;
result->firmwareCrc = strtoul(positionInData,&positionInData,0);
}
/* parse the power mode */
if(positionInData = strstr((char*)data->payload,MODE_POWER_LIMIT)) {
while((*positionInData != '=') && (positionInData != (data->payload + data->tot_len)))
positionInData++;
positionInData++;
// if(strstr((char*)data->payload,LOAD_SHEDDER))
// result->powerMode = POWER_MODE_LOAD_SHEDDER;
//else
result->powerMode = POWER_MODE_REGULATOR;
}
/* parse the power limit load shedder */
/*if(positionInData = strstr((char*)data->payload,POWER_LIMIT_SHEDDER)) {
while((*positionInData != '=') && (positionInData != (data->payload + data->tot_len)))
positionInData++;
positionInData++;
result->powerLimitLoadShedder = strtoul(positionInData,&positionInData,0);
}*/
/* parse threshold */
if(positionInData = strstr((char*)data->payload,THRESHOLD)) {
while((*positionInData != '=') && (positionInData != (data->payload + data->tot_len)))
positionInData++;
positionInData++;
result->threshold = strtoul(positionInData,&positionInData,0);
}

result->state = DONE_GET_CONFIG;
return FALSE;
}

0 Kudos