8000 Wed Mar 1 09:47:36 CST 2017 · mwerschy/esp32-snippets@6d25120 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6d25120

Browse files
author
kolban
committed
Wed Mar 1 09:47:36 CST 2017
1 parent 66ddc54 commit 6d25120

File tree

255 files changed

+215
-13916
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

255 files changed

+215
-13916
lines changed

cpp_utils/GPIO.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* GPIO.cpp
3+
*
4+
* Created on: Feb 28, 2017
5+
* Author: kolban
6+
*/
7+
8+
#include "GPIO.h"
9+
10+
GPIO::GPIO() {
11+
// TODO Auto-generated constructor stub
12+
13+
}
14+
15+
/**
16+
* Determine if the pin is a valid pin for an ESP32 (i.e. is it in range).
17+
* @param [in] pin The pin number to validate.
18+
* @return The value of true if the pin is valid and false otherwise.
19+
*/
20+
bool GPIO::inRange(int pin) {
21+
if (pin>=0 && pin<=39) {
22+
return true;
23+
}
24+
return false;
25+
}

cpp_utils/GPIO.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* GPIO.h
3+
*
4+
* Created on: Feb 28, 2017
5+
* Author: kolban
6+
*/
7+
8+
#ifndef COMPONENTS_CPP_UTILS_GPIO_H_
9+
#define COMPONENTS_CPP_UTILS_GPIO_H_
10+
11+
class GPIO {
12+
public:
13+
GPIO();
14+
static bool inRange(int pin);
15+
};
16+
17+
#endif /* COMPONENTS_CPP_UTILS_GPIO_H_ */

cpp_utils/SockServ.cpp

Lines changed: 62 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,34 @@
55
* Author: kolban
66
*/
77

8+
#include <errno.h>
89
#include <esp_log.h>
910
#include <lwip/sockets.h>
1011
#include <stdint.h>
12+
#include <string.h>
1113
#include <string>
12-
//#include "/opt/xtensa-esp32-elf/xtensa-esp32-elf/include/c++/5.2.0/string"
1314

1415
#include "FreeRTOS.h"
15-
#include "SockServ.h"
1616
#include "sdkconfig.h"
17+
#include "SockServ.h"
1718

1819
static char tag[] = "SockServ";
1920

21+
2022
/**
21-
*
23+
* Create an instance of the class.
24+
* We won't actually start listening for clients until after the start() method has been called.
25+
* @param [in] port The TCP/IP port number on which we will listen for incoming connection requests.
26+
*/
27+
SockServ::SockServ(uint16_t port) {
28+
this->port = port;
29+
clientSock = -1;
30+
} // SockServ
31+
32+
33+
/**
34+
* Accept an incoming connection.
35+
* Block waiting for an incoming connection and accept it when it arrives.
2236
*/
2337
void SockServ::acceptTask(void *data) {
2438

@@ -28,54 +42,71 @@ void SockServ::acceptTask(void *data) {
2842
while(1) {
2943
socklen_t clientAddressLength = sizeof(clientAddress);
3044
int tempSock = ::accept(pSockServ->sock, (struct sockaddr *)&clientAddress, &clientAddressLength);
45+
if (tempSock == -1) {
46+
ESP_LOGE(tag, "close(): %s", strerror(errno));
47+
}
3148
ESP_LOGD(tag, "accept() - New socket");
3249
if (pSockServ->clientSock != -1) {
33-
::close(pSockServ->clientSock);
50+
int rc = ::close(pSockServ->clientSock);
51+
if (rc == -1) {
52+
ESP_LOGE(tag, "close(): %s", strerror(errno));
53+
}
3454
}
3555
pSockServ->clientSock = tempSock;
3656
}
37-
}
38-
39-
40-
SockServ::SockServ(uint16_t port) {
41-
this->port = port;
42-
clientSock = -1;
43-
}
57+
} // acceptTask
4458

4559

4660
/**
4761
* Start listening for new partner connections.
4862
*/
4963
void SockServ::start() {
5064
sock = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
65+
if (sock == -1) {
66+
ESP_LOGE(tag, "socket(): %s", strerror(errno));
67+
}
5168
struct sockaddr_in serverAddress;
5269
serverAddress.sin_family = AF_INET;
5370
serverAddress.sin_addr.s_addr = htonl(INADDR_ANY);
5471
serverAddress.sin_port = htons(port);
55-
::bind(sock, (struct sockaddr *)&serverAddress, sizeof(serverAddress));
56-
::listen(sock, 5);
72+
int rc = ::bind(sock, (struct sockaddr *)&serverAddress, sizeof(s B41A erverAddress));
73+
if (rc == -1) {
74+
ESP_LOGE(tag, "bind(): %s", strerror(errno));
75+
}
76+
rc = ::listen(sock, 5);
77+
if (rc == -1) {
78+
ESP_LOGE(tag, "listen(): %s", strerror(errno));
79+
}
5780
ESP_LOGD(tag, "Now listening on port %d", port);
5881
FreeRTOS::startTask(acceptTask, "acceptTask", this);
59-
}
82+
} // start
83+
6084

6185
/**
6286
* Stop listening for new partner connections.
6387
*/
6488
void SockServ::stop() {
65-
::close(sock);
66-
}
89+
int rc = ::close(sock);
90+
if (rc == -1) {
91+
ESP_LOGE(tag, "close(): %s", strerror(errno));
92+
}
93+
} // stop
94+
6795

6896
/**
6997
* Send data to any connected partners.
7098
* @param[in] data A sequence of bytes to send to the partner.
7199
* @param[in] length The length of the sequence of bytes to send to the partner.
72100
*/
73101
void SockServ::sendData(uint8_t *data, size_t length) {
74-
if (clientSock == -1) {
102+
if (connectedCount() == 0) {
75103
return;
76104
}
77-
::send(clientSock, data, length, 0);
78-
}
105+
int rc = ::send(clientSock, data, length, 0);
106+
if (rc == -1) {
107+
ESP_LOGE(tag, "send(): %s", strerror(errno));
108+
}
109+
} // sendData
79110

80111

81112
/**
@@ -84,4 +115,16 @@ void SockServ::sendData(uint8_t *data, size_t length) {
84115
*/
85116
void SockServ::sendData(std::string str) {
86117
sendData((uint8_t *)str.data(), str.size());
118+
} // sendData
119+
120+
121+
/**
122+
* Determine the number of connected partners.
123+
* @return The number of connected partners.
124+
*/
125+
int SockServ::connectedCount() {
126+
if (clientSock == -1) {
127+
return 0;
128+
}
129+
return 1;
87130
}

cpp_utils/SockServ.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class SockServ {
2424
void stop();
2525
void sendData(uint8_t *data, size_t length);
2626
void sendData(std::string str);
27+
int connectedCount();
2728
};
2829

2930
#endif /* MAIN_SOCKSERV_H_ */

0 commit comments

Comments
 (0)
0