From f7ff571c5f219045425fae46ddbb799abf142c5d Mon Sep 17 00:00:00 2001 From: Sergey Lyubka Date: Wed, 5 Jan 2022 13:22:00 +0000 Subject: [PATCH] Added SSDP search example --- examples/udp-ssdp-search/Makefile | 10 +++++++++ examples/udp-ssdp-search/main.c | 36 +++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 examples/udp-ssdp-search/Makefile create mode 100644 examples/udp-ssdp-search/main.c diff --git a/examples/udp-ssdp-search/Makefile b/examples/udp-ssdp-search/Makefile new file mode 100644 index 00000000..e4aed5d8 --- /dev/null +++ b/examples/udp-ssdp-search/Makefile @@ -0,0 +1,10 @@ +PROG ?= example + +all: $(PROG) + $(RUN) ./$(PROG) $(ARGS) + +$(PROG): main.c + $(CC) ../../mongoose.c -I../.. -W -Wall $(CFLAGS) -o $(PROG) main.c + +clean: + rm -rf $(PROG) *.o *.dSYM *.gcov *.gcno *.gcda *.obj *.exe *.ilk *.pdb diff --git a/examples/udp-ssdp-search/main.c b/examples/udp-ssdp-search/main.c new file mode 100644 index 00000000..9171765e --- /dev/null +++ b/examples/udp-ssdp-search/main.c @@ -0,0 +1,36 @@ +// Copyright (c) 2022 Cesanta Software Limited +// All rights reserved + +#include "mongoose.h" + +static const char *s_ssdp_url = "udp://239.255.255.250:1900"; + +static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { + if (ev == MG_EV_OPEN) c->is_hexdumping = 1; + LOG(LL_DEBUG, ("%p got event: %d %p %p", c, ev, ev_data, fn_data)); +} + +static void tfn(void *param) { + struct mg_connection *c = param; + if (c == NULL) return; + LOG(LL_INFO, ("Sending M-SEARCH")); + mg_printf(c, "%s", + "M-SEARCH * HTTP/1.1\r\n" + "HOST: 239.255.255.250:1900\r\n" + "MAN: \"ssdp:discover\"\r\n" + "ST: urn:schemas-upnp-org:device:MediaRenderer:1\r\n" + "MX: 5\r\n" + "\r\n"); +} + +int main(void) { + struct mg_mgr mgr; + struct mg_timer timer; + static struct mg_connection *c; + mg_mgr_init(&mgr); + c = mg_connect(&mgr, s_ssdp_url, fn, NULL); + mg_timer_init(&timer, 2000, MG_TIMER_REPEAT | MG_TIMER_RUN_NOW, tfn, c); + while (true) mg_mgr_poll(&mgr, 200); + mg_mgr_free(&mgr); + return 0; +}