Skip to content
Snippets Groups Projects
Commit 1d8b0320 authored by f4grx's avatar f4grx
Browse files

Merge branch 'master' of https://github.com/f4grx/hn70ap

parents 92569dcc b12538e6
No related branches found
No related tags found
No related merge requests found
......@@ -45,7 +45,7 @@
typedef int (*tunrxfunction_f)(uint8_t tun, FAR void *arg, FAR uint8_t *data, int length);
int hn70ap_tun_init(void);
int hn70ap_tun_initdevice(char ifname[IFNAMSIZ]);
int hn70ap_tun_devinit(char ifname[IFNAMSIZ]);
int hn70ap_tun_addroute(int tunnel, in_addr_t destination, int maskbits);
int hn70ap_tun_rxfunction(int tunnel, tunrxfunction_f rx, FAR void *arg, FAR uint8_t *userbuf, int userbuflen);
int hn70ap_tun_transmit(int tunnel, FAR uint8_t *buf, size_t len);
......
......@@ -170,6 +170,7 @@ int hn70ap_radio_devinit(struct radio_s *radio, const char *dev)
goto lret;
}
radio->alive = true;
ret = pthread_create(&radio->rxthread, NULL, hn70ap_radio_rxthread, NULL);
if(ret < 0)
{
......
......@@ -37,9 +37,12 @@
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <syslog.h>
#include <net/if.h>
#include <hn70ap/eeprom.h>
#include <hn70ap/timer.h>
#include <hn70ap/leds.h>
......@@ -52,6 +55,7 @@ static bool hn70ap_system_initialized = false;
int hn70ap_system_init(void)
{
int ret;
int tunid;
bool defaults;
char tunname[IFNAMSIZ];
......@@ -105,12 +109,13 @@ int hn70ap_system_init(void)
syslog(LOG_ERR, "WARNING: Failed to initialize tunnels\n");
}
strcpy(tunname, "uhf%d");
ret = hn70ap_tun_initdevice(tunname);
strncpy(tunname, "uhf%d", IFNAMSIZ);
ret = hn70ap_tun_devinit(tunname);
if(ret != 0)
{
syslog(LOG_ERR, "WARNING: Failed to initialize TUN interface\n");
}
tunid = ret;
ret = hn70ap_radio_init();
if(ret != 0)
......
......@@ -39,7 +39,9 @@
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <pthread.h>
#include <syslog.h>
#include <sys/ioctl.h>
......@@ -49,7 +51,9 @@
struct iptunnel_s
{
int fd;
int fd;
char ifname[IFNAMSIZ];
bool alive;
pthread_t rxthread;
/*data reception and calling back to the user*/
......@@ -61,6 +65,38 @@ struct iptunnel_s
struct iptunnel_s tunnels[2];
/****************************************************************************
* hn70ap_tun_rxthread
* This thread waits for packets from the tun interface, and sends them to processes.
****************************************************************************/
void *hn70ap_tun_rxthread(void *arg)
{
struct iptunnel_s *tunnel = (struct iptunnel_s*)arg;
syslog(LOG_INFO, "Started tun RX thread\n");
int ret;
while(tunnel->alive)
{
if(tunnel->callback)
{
//Wait for messages on the air
ret = read(tunnel->fd, tunnel->userbuf, tunnel->userbuflen);
if(ret > 0)
{
//Dispatch them to the callback
tunnel->callback((tunnel==tunnels)?0:1, tunnel->arg, tunnel->userbuf, ret);
}
else
{
syslog(LOG_ERR, "tunnel rx failed -> errno=%d\n", errno);
}
} //callback defined
} //tunnel alive
syslog(LOG_INFO, "Stopped tunnel RX thread\n");
return NULL;
}
/****************************************************************************
* hn70ap_tun_transmit
****************************************************************************/
......@@ -95,14 +131,27 @@ int hn70ap_tun_rxfunction(int tunnel, tunrxfunction_f rx, FAR void *arg, FAR uin
}
/****************************************************************************
* hn70ap_tun_init
* hn70ap_tun_devinit
****************************************************************************/
int hn70ap_tun_initdevice(char ifname[IFNAMSIZ])
int hn70ap_tun_devinit(char name[IFNAMSIZ])
{
struct ifreq ifr;
int errcode;
int fd;
struct ifreq ifr;
int errcode;
int fd;
int ret;
struct iptunnel_s *tunnel = &tunnels[0];
if(tunnel->fd != 0)
{
tunnel = &tunnels[1];
}
if(tunnel->fd != 0)
{
syslog(LOG_ERR, "No tunnel available");
return -1;
}
if ((fd = open("/dev/tun", O_RDWR)) < 0)
{
......@@ -111,9 +160,9 @@ int hn70ap_tun_initdevice(char ifname[IFNAMSIZ])
memset(&ifr, 0, sizeof(ifr));
ifr.ifr_flags = IFF_TUN;
if (ifname[0])
if (name[0])
{
strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
strncpy(ifr.ifr_name, name, IFNAMSIZ);
}
if ((errcode = ioctl(fd, TUNSETIFF, (unsigned long)&ifr)) < 0)
......@@ -121,8 +170,16 @@ int hn70ap_tun_initdevice(char ifname[IFNAMSIZ])
close(fd);
return errcode;
}
tunnel->fd = fd;
strncpy(tunnel->ifname, ifr.ifr_name, IFNAMSIZ);
//Start RX thread
tunnel->alive = true;
ret = pthread_create(&tunnel->rxthread, NULL, hn70ap_tun_rxthread, NULL);
if(ret < 0)
{
syslog(LOG_ERR, "Failed to start the receive thread\n");
}
return fd;
}
......@@ -131,10 +188,15 @@ int hn70ap_tun_initdevice(char ifname[IFNAMSIZ])
* hn70ap_tun_addroute
****************************************************************************/
int hn70ap_tun_addroute(int fd, in_addr_t destination, int maskbits)
int hn70ap_tun_addroute(int tunnel, in_addr_t destination, int maskbits)
{
return ERROR;
}
/****************************************************************************
* hn70ap_tun_init
****************************************************************************/
int hn70ap_tun_init(void)
{
tunnels[0].fd = 0;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment