Skip to content

Commit

Permalink
Starting to work on ONVIF support
Browse files Browse the repository at this point in the history
  • Loading branch information
wberube committed Nov 11, 2024
1 parent 3ab8dd8 commit fd332d0
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
58 changes: 58 additions & 0 deletions src/network.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "network.h"

pthread_t onvifPid = 0;

struct mdnsd *mdns = {0};

int start_mdns() {
Expand Down Expand Up @@ -46,4 +48,60 @@ void stop_mdns() {
if (mdns)
mdnsd_stop(mdns);
mdns = NULL;
}

void *onvif_thread(void) {
char msgbuf[1024];
int servfd, msglen;
struct sockaddr_in servaddr, clntaddr;
socklen_t clntsz;

if ((servfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
HAL_ERROR("onvif", "Failed to create socket!\n");

memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = inet_addr("239.255.255.250");
servaddr.sin_port = htons(3702);

if (bind(servfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) == -1)
HAL_ERROR("onvif", "Failed to bind socket!\n");

while (keepRunning) {
clntsz = sizeof(clntaddr);
if (msglen = recvfrom(servfd, msgbuf, sizeof(msgbuf), 0, (struct sockaddr *)&clntaddr, &clntsz) < 0)
continue;

msgbuf[msglen] = '\0';
HAL_INFO("onvif", "Received message: %s\n", msgbuf);

if (!strstr(msgbuf, "http://schemas.xmlsoap.org/ws/2005/04/discovery/Probe"))
continue;

char *response = "";
sendto(servfd, response, strlen(response), 0, (struct sockaddr *)&clntaddr, clntsz);
}

close(servfd);
return EXIT_SUCCESS;
}

int start_onvif_server() {
pthread_attr_t thread_attr;
pthread_attr_init(&thread_attr);
size_t stacksize;
pthread_attr_getstacksize(&thread_attr, &stacksize);
size_t new_stacksize = 16 * 1024;
if (pthread_attr_setstacksize(&thread_attr, new_stacksize)) {
HAL_DANGER("onvif", "Error: Can't set stack size %zu\n", new_stacksize);
}
pthread_create(&onvifPid, &thread_attr, (void *(*)(void *))onvif_thread, NULL);
if (pthread_attr_setstacksize(&thread_attr, stacksize)) {
HAL_DANGER("onvif", "Error: Can't set stack size %zu\n", stacksize);
}
pthread_attr_destroy(&thread_attr);
}

void stop_onvif_server() {
pthread_join(onvifPid, NULL);
}
8 changes: 7 additions & 1 deletion src/network.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <ifaddrs.h>
#include <net/if.h>
#include <pthread.h>
#include <stdio.h>
#include <sys/utsname.h>

Expand All @@ -13,4 +14,9 @@ extern int asprintf(char **restrict strp, const char *restrict fmt, ...);
#include "lib/tinysvcmdns.h"

int start_mdns();
void stop_mdns();
void stop_mdns();

void *onvif_thread();

int start_onvif_server();
void stop_onvif_server();

0 comments on commit fd332d0

Please sign in to comment.