Skip to content

Commit

Permalink
Changes for nkolban#42 - 2017-08-28
Browse files Browse the repository at this point in the history
  • Loading branch information
nkolban committed Aug 28, 2017
1 parent 09ea2c2 commit ed4628e
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 101 deletions.
161 changes: 97 additions & 64 deletions cpp_utils/SockServ.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@

#include <errno.h>
#include <esp_log.h>
#include <FreeRTOS.h>
#include <lwip/sockets.h>

#include <stdint.h>
#include <string.h>
#include <string>

#include "FreeRTOS.h"
#include "sdkconfig.h"
#include "SockServ.h"

static char tag[] = "SockServ";
static const char* LOG_TAG = "SockServ";


/**
Expand All @@ -26,9 +27,10 @@ static char tag[] = "SockServ";
* @param [in] port The TCP/IP port number on which we will listen for incoming connection requests.
*/
SockServ::SockServ(uint16_t port) {
this->port = port;
clientSock = -1;
sock = -1;
this->m_port = port;
m_clientSock = -1;
m_sock = -1;
m_clientSemaphore.take("SockServ");
} // SockServ


Expand All @@ -39,63 +41,83 @@ SockServ::SockServ(uint16_t port) {
*/
void SockServ::acceptTask(void *data) {

SockServ *pSockServ = (SockServ *)data;
SockServ* pSockServ = (SockServ*)data;
struct sockaddr_in clientAddress;

while(1) {
socklen_t clientAddressLength = sizeof(clientAddress);
int tempSock = ::accept(pSockServ->sock, (struct sockaddr *)&clientAddress, &clientAddressLength);
int tempSock = ::accept(pSockServ->m_sock, (struct sockaddr *)&clientAddress, &clientAddressLength);
if (tempSock == -1) {
ESP_LOGE(tag, "close(): %s", strerror(errno));
ESP_LOGE(LOG_TAG, "close(): %s", strerror(errno));
}
ESP_LOGD(tag, "accept() - New socket");
if (pSockServ->clientSock != -1) {
int rc = ::close(pSockServ->clientSock);
ESP_LOGD(LOG_TAG, "accept() - New socket");
if (pSockServ->m_clientSock != -1) {
int rc = ::close(pSockServ->m_clientSock);
if (rc == -1) {
ESP_LOGE(tag, "close(): %s", strerror(errno));
ESP_LOGE(LOG_TAG, "close(): %s", strerror(errno));
}
}
pSockServ->clientSock = tempSock;
pSockServ->m_clientSock = tempSock;
pSockServ->m_clientSemaphore.give();
}
} // acceptTask


/**
* @brief Start listening for new partner connections.
* @brief Determine the number of connected partners.
*
* The port number on which we will listen is the one defined when the class was created.
* @return The number of connected partners.
*/
void SockServ::start() {
sock = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sock == -1) {
ESP_LOGE(tag, "socket(): %s", strerror(errno));
}
struct sockaddr_in serverAddress;
serverAddress.sin_family = AF_INET;
serverAddress.sin_addr.s_addr = htonl(INADDR_ANY);
serverAddress.sin_port = htons(port);
int rc = ::bind(sock, (const struct sockaddr *)&serverAddress, sizeof(serverAddress));
if (rc == -1) {
ESP_LOGE(tag, "bind(): %s", strerror(errno));
int SockServ::connectedCount() {
if (m_clientSock == -1) {
return 0;
}
rc = ::listen(sock, 5);
if (rc == -1) {
ESP_LOGE(tag, "listen(): %s", strerror(errno));
return 1;
} // connectedCount


/**
* @brief Disconnect any connected partners.
*/
void SockServ::disconnect() {
if (m_clientSock != -1) {
int rc = ::close(m_clientSock);
if (rc == -1) {
ESP_LOGE(LOG_TAG, "close(): %s", strerror(errno));
}
m_clientSock = -1;
m_clientSemaphore.take("disconnect");
}
ESP_LOGD(tag, "Now listening on port %d", port);
FreeRTOS::startTask(acceptTask, "acceptTask", this);
} // start
} // disconnect


/**
* @brief Stop listening for new partner connections.
* @brief Wait for data
* @param [in] pData Pointer to buffer to hold the data.
* @param [in] maxData Maximum size of the data to receive.
* @return The amount of data returned or 0 if there was an error.
*/
void SockServ::stop() {
int rc = ::close(sock);
size_t SockServ::receiveData(void* pData, size_t maxData) {
if (m_clientSock == -1) {
return 0;
}
int rc = ::recv(m_clientSock, pData, maxData, 0);
if (rc == -1) {
ESP_LOGE(tag, "close(): %s", strerror(errno));
ESP_LOGE(LOG_TAG, "recv(): %s", strerror(errno));
return 0;
}
} // stop
return rc;
} // receiveData


/**
* @brief Send data from a string to any connected partners.
*
* @param[in] str A string from which sequence of bytes will be used to send to the partner.
*/
void SockServ::sendData(std::string str) {
sendData((uint8_t *)str.data(), str.size());
} // sendData


/**
Expand All @@ -104,49 +126,60 @@ void SockServ::stop() {
* @param[in] data A sequence of bytes to send to the partner.
* @param[in] length The length of the sequence of bytes to send to the partner.
*/
void SockServ::sendData(uint8_t *data, size_t length) {
void SockServ::sendData(uint8_t* data, size_t length) {
if (connectedCount() == 0) {
return;
}
int rc = ::send(clientSock, data, length, 0);
int rc = ::send(m_clientSock, data, length, 0);
if (rc == -1) {
ESP_LOGE(tag, "send(): %s", strerror(errno));
ESP_LOGE(LOG_TAG, "send(): %s", strerror(errno));
}
} // sendData


/**
* @brief Send data from a string to any connected partners.
* @brief Start listening for new partner connections.
*
* @param[in] str A string from which sequence of bytes will be used to send to the partner.
* The port number on which we will listen is the one defined when the class was created.
*/
void SockServ::sendData(std::string str) {
sendData((uint8_t *)str.data(), str.size());
} // sendData
void SockServ::start() {
m_sock = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (m_sock == -1) {
ESP_LOGE(LOG_TAG, "socket(): %s", strerror(errno));
}
struct sockaddr_in serverAddress;
serverAddress.sin_family = AF_INET;
serverAddress.sin_addr.s_addr = htonl(INADDR_ANY);
serverAddress.sin_port = htons(m_port);
int rc = ::bind(m_sock, (const struct sockaddr *)&serverAddress, sizeof(serverAddress));
if (rc == -1) {
ESP_LOGE(LOG_TAG, "bind(): %s", strerror(errno));
}
rc = ::listen(m_sock, 5);
if (rc == -1) {
ESP_LOGE(LOG_TAG, "listen(): %s", strerror(errno));
}
ESP_LOGD(LOG_TAG, "Now listening on port %d", m_port);
FreeRTOS::startTask(acceptTask, "acceptTask", this);
} // start


/**
* @brief Determine the number of connected partners.
*
* @return The number of connected partners.
* @brief Stop listening for new partner connections.
*/
int SockServ::connectedCount() {
if (clientSock == -1) {
return 0;
void SockServ::stop() {
int rc = ::close(m_sock);
if (rc == -1) {
ESP_LOGE(LOG_TAG, "close(): %s", strerror(errno));
}
return 1;
} // connectedCount
} // stop


/**
* @brief Disconnect any connected partners.
* @brief Wait for a client connection to be present.
* Returns when a client connection is present. This can block until a client connects
* or can return immediately is there is already a client connection in existence.
*/
void SockServ::disconnect() {
if (clientSock == -1) {
int rc = ::close(clientSock);
if (rc == -1) {
ESP_LOGE(tag, "close(): %s", strerror(errno));
}
clientSock = -1;
}
} // disconnect
void SockServ::waitForClient() {
m_clientSemaphore.wait("waitForClient");
} // waitForClient
26 changes: 16 additions & 10 deletions cpp_utils/SockServ.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#define MAIN_SOCKSERV_H_
#include <stdint.h>
#include <string>
#include "FreeRTOS.h"


/**
* @brief Provide a socket listener and the ability to send data to connected partners.
Expand All @@ -23,20 +25,24 @@
* @endcode
*
*/

class SockServ {
private:
uint16_t port;
int sock;
int clientSock;
static void acceptTask(void *data);
static void acceptTask(void*);
uint16_t m_port;
int m_sock;
int m_clientSock;
FreeRTOS::Semaphore m_clientSemaphore;
public:
SockServ(uint16_t port);
int connectedCount();
void disconnect();
void sendData(uint8_t *data, size_t length);
void sendData(std::string str);
void start();
void stop();
int connectedCount();
void disconnect();
size_t receiveData(void* pData, size_t maxData);
void sendData(uint8_t *data, size_t length);
void sendData(std::string str);
void start();
void stop();
void waitForClient();
};

#endif /* MAIN_SOCKSERV_H_ */
26 changes: 1 addition & 25 deletions curl/build_files/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,6 @@ We now need to copy some ESP32 specific configuration files. These are:
* `lib/curl_config.h`
* `component.mk`

Finally, we need to make a small edit. Find the file called:

`curl/include/curl/system.h`

and open it in your favorite editor. Find the section that reads:

```
#elif defined(__GNUC__)
# if !defined(__LP64__) && (defined(__ILP32__) || \
defined(__i386__) || defined(__ppc__) || defined(__arm__) || \
defined(__sparc__) || defined(__mips__) || defined(__sh__))
```

and change to:

```
#elif defined(__GNUC__)
# if !defined(__LP64__) && (defined(__ILP32__) || \
defined(__i386__) || defined(__ppc__) || defined(__arm__) || \
defined(__sparc__) || defined(__mips__) || defined(__sh__) || defined(__XTENSA__))
```

A request has been made to the owners of libcurl to make this change in their own source. See [issue 1598](https://github.com/curl/curl/issues/1598).

Once the above steps have been completed, we should be able to build our ESP-IDF project as normal and that will include the construction
of the curl library ready for use. Now you can code directly to the curl APIs or you can code to the
C++ REST classes that leverage curl.
Once the above steps have been completed, we should be able to build our ESP-IDF project as normal and that will include the construction of the curl library ready for use. Now you can code directly to the curl APIs or you can code to the C++ REST classes that leverage curl.
6 changes: 4 additions & 2 deletions curl/build_files/lib/curl_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -904,6 +904,8 @@
/* The size of `void*', as computed by sizeof. */
#define SIZEOF_VOIDP 4

#define SIZEOF_CURL_OFF_T 4

/* Define to 1 if you have the ANSI C header files. */
#define STDC_HEADERS 1

Expand Down Expand Up @@ -944,8 +946,8 @@
#define USE_MANUAL 1

/* if mbedTLS is enabled */
/* #undef USE_MBEDTLS */
#define USE_MBEDTLS 1
#undef USE_MBEDTLS
//#define USE_MBEDTLS 1

/* Define to enable metalink support */
/* #undef USE_METALINK */
Expand Down
2 changes: 2 additions & 0 deletions filesystems/sendZip/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/package-lock.json
/node_modules/

0 comments on commit ed4628e

Please sign in to comment.