sds

simple/sensor data server
git clone git://git.hanetzok.net/sds
Log | Files | Refs | README | LICENSE

commit c81c3db79e57f4037436af90da68564807ab50d6
parent ff5c4205827063dd6b774d34bb2809aa68bfdbd0
Author: Markus Hanetzok <markus@hanetzok.net>
Date:   Tue, 18 Nov 2025 22:37:16 +0100

add testserver

Diffstat:
M.gitignore | 1+
MMakefile | 5+++--
Msensors.def.h | 12++++++------
Atestserver.c | 42++++++++++++++++++++++++++++++++++++++++++
4 files changed, 52 insertions(+), 8 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -1,2 +1,3 @@ sds +testserver sensors.h diff --git a/Makefile b/Makefile @@ -1,8 +1,9 @@ default: build -build: clean +build: @if [ ! -f sensors.h ]; then cp sensors.def.h sensors.h; fi gcc -Wall -o sds sds.c -l curl + gcc -Wall -o testserver testserver.c install: build cp -f sds /usr/bin/sds @@ -12,4 +13,4 @@ uninstall: rm -f /usr/bin/sds clean: - rm -rf sds + rm -rf sds testserver diff --git a/sensors.def.h b/sensors.def.h @@ -6,18 +6,18 @@ Foundation, either version 3 of the License, or (at your option) any later version. - sds is distributed in the hope that it will be useful, but WITHOUT ANY - WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + sds is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. - You should have received a copy of the GNU General Public License along with - sds. If not, see <https://www.gnu.org/licenses/>. + You should have received a copy of the GNU General Public License along with + sds. If not, see <https://www.gnu.org/licenses/>. */ #include <stdio.h> static const Sensor sensors[] = { // name // address // unit - { "Sensor description", "192.168.178.1/temp", "C"}, - { "Another sensor description", "192.168.178.1/hum", "%"}, + { "Testserver", "localhost:8080", "C"}, }; + diff --git a/testserver.c b/testserver.c @@ -0,0 +1,42 @@ +/* very simple webserver to mock a sensor for testing */ +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <unistd.h> +#include <arpa/inet.h> + +int main() { + int server_fd, client_fd; + struct sockaddr_in address; + int opt = 1; + int addrlen = sizeof(address); + char response[] = + "HTTP/1.1 200 OK\r\n" + "Content-Type: text/plain\r\n" + "Content-Length: 4\r\n" + "\r\n" + "3.14"; + + server_fd = socket(AF_INET, SOCK_STREAM, 0); + + setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); + + address.sin_family = AF_INET; + address.sin_addr.s_addr = INADDR_ANY; + address.sin_port = htons(8080); + + bind(server_fd, (struct sockaddr *)&address, sizeof(address)); + + listen(server_fd, 3); + + printf("Server running on port 8080...\n"); + + while (1) { + client_fd = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen); + write(client_fd, response, strlen(response)); + close(client_fd); + } + + return 0; +} +