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

WIP tunnels

parent cb339590
No related branches found
No related tags found
No related merge requests found
......@@ -42,9 +42,11 @@
#define HN70AP_RADIO_MAIN 1
#define HN70AP_RADIO_AUX 2
typedef int (*radiorxfunction_f)(uint8_t device, FAR void *arg, FAR uint8_t *data, int length);
int hn70ap_radio_init(void);
int hn70ap_radio_transmit(uint8_t device, uint8_t *buf, size_t len);
int hn70ap_radio_receive (uint8_t device, uint8_t *buf, size_t len);
int hn70ap_radio_rxfunction(uint8_t device, radiorxfunction_f rx, FAR void *arg, FAR uint8_t *userbuf, int userbuflen);
int hn70ap_radio_transmit(uint8_t device, FAR uint8_t *buf, size_t len);
#endif /* HN70AP_SYSTEM_H */
......@@ -39,9 +39,16 @@
#include <stdint.h>
#include <stdbool.h>
#include <netinet/in.h>
#include <net/if.h>
int hn70ap_tun_init(char ifname[IFNAMSIZ]);
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_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);
#endif /* HN70AP_SYSTEM_H */
......@@ -41,6 +41,7 @@
#include <fcntl.h>
#include <syslog.h>
#include <unistd.h>
#include <errno.h>
#include <pthread.h>
#include <hn70ap/radio.h>
......@@ -52,39 +53,31 @@ struct radio_s
int devfd;
bool alive;
pthread_t rxthread;
/*data reception and calling back to the user*/
FAR uint8_t * userbuf;
int userbuflen;
radiorxfunction_f callback;
FAR void * arg;
};
static struct radio_s g_hn70ap_mainradio;
static struct radio_s g_hn70ap_auxradio;
/****************************************************************************
* hn70ap_radio_transmit
* hn70ap_radio_receive
****************************************************************************/
int hn70ap_radio_transmit(uint8_t device, uint8_t *buf, size_t len)
static int hn70ap_radio_receive(struct radio_s *radio, uint8_t *buf, size_t len)
{
struct radio_s *radio;
int ret;
if(device == HN70AP_RADIO_MAIN)
{
radio = &g_hn70ap_mainradio;
}
else if(device == HN70AP_RADIO_AUX)
{
radio = &g_hn70ap_auxradio;
}
else
{
return ERROR;
}
/* Turn on radio LED in transmit mode */
hn70ap_leds_state(LED_1A, LED_STATE_ON);
hn70ap_leds_state(LED_1B, LED_STATE_OFF);
/* Turn on radio LED in receive mode */
hn70ap_leds_state(LED_1A, LED_STATE_OFF);
hn70ap_leds_state(LED_1B, LED_STATE_ON);
/* Transmit */
ret = write(radio->devfd, buf, len);
/* Receive */
ret = read(radio->devfd, buf, len);
/* Turn off radio LED */
hn70ap_leds_state(LED_1A, LED_STATE_OFF);
......@@ -94,10 +87,10 @@ int hn70ap_radio_transmit(uint8_t device, uint8_t *buf, size_t len)
}
/****************************************************************************
* hn70ap_radio_receive
* hn70ap_radio_transmit
****************************************************************************/
int hn70ap_radio_receive(uint8_t device, uint8_t *buf, size_t len)
int hn70ap_radio_transmit(uint8_t device, uint8_t *buf, size_t len)
{
struct radio_s *radio;
int ret;
......@@ -115,12 +108,12 @@ int hn70ap_radio_receive(uint8_t device, uint8_t *buf, size_t len)
return ERROR;
}
/* Turn on radio LED in receive mode */
hn70ap_leds_state(LED_1A, LED_STATE_OFF);
hn70ap_leds_state(LED_1B, LED_STATE_ON);
/* Turn on radio LED in transmit mode */
hn70ap_leds_state(LED_1A, LED_STATE_ON);
hn70ap_leds_state(LED_1B, LED_STATE_OFF);
/* Receive */
ret = read(radio->devfd, buf, len);
/* Transmit */
ret = write(radio->devfd, buf, len);
/* Turn off radio LED */
hn70ap_leds_state(LED_1A, LED_STATE_OFF);
......@@ -138,12 +131,27 @@ void *hn70ap_radio_rxthread(void *arg)
{
struct radio_s *radio = (struct radio_s*)arg;
syslog(LOG_INFO, "Started radio RX thread\n");
int ret;
while(radio->alive)
{
//Wait for messages on the air
//Dispatch them to the callback
}
if(radio->callback)
{
//Wait for messages on the air
ret = hn70ap_radio_receive(radio, radio->userbuf, radio->userbuflen);
if(ret > 0)
{
//Dispatch them to the callback
radio->callback((radio==&g_hn70ap_mainradio)?HN70AP_RADIO_MAIN:HN70AP_RADIO_AUX, radio->arg, radio->userbuf, ret);
}
else
{
syslog(LOG_ERR, "radio rx failed -> errno=%d\n", errno);
}
} //callback defined
} //radio alive
syslog(LOG_INFO, "Stopped radio RX thread\n");
return NULL;
}
/****************************************************************************
......@@ -172,13 +180,40 @@ lret:
return ret;
}
/****************************************************************************
* hn70ap_radio_rxfunction
****************************************************************************/
int hn70ap_radio_rxfunction(uint8_t device, radiorxfunction_f rx, FAR void *arg, FAR uint8_t *userbuf, int userbuflen)
{
struct radio_s *radio;
if(device == HN70AP_RADIO_MAIN)
{
radio = &g_hn70ap_mainradio;
}
else if(device == HN70AP_RADIO_AUX)
{
radio = &g_hn70ap_auxradio;
}
else
{
return ERROR;
}
radio->userbuf = userbuf;
radio->userbuflen = userbuflen;
radio->arg = arg;
radio->callback = rx;
return OK;
}
/****************************************************************************
* hn70ap_radio_init
****************************************************************************/
int hn70ap_radio_init(void)
{
int ret = 0;
#ifdef CONFIG_HN70AP_MAINRADIO
syslog(LOG_INFO, "Checking main radio\n");
......@@ -190,5 +225,6 @@ int hn70ap_radio_init(void)
hn70ap_radio_devinit(&g_hn70ap_auxradio, "/dev/raux");
#endif
return OK;
}
......@@ -99,19 +99,27 @@ int hn70ap_system_init(void)
syslog(LOG_ERR, "WARNING: Failed to initialize Screen\n");
}
ret = hn70ap_radio_init();
ret = hn70ap_tun_init();
if(ret != 0)
{
syslog(LOG_ERR, "WARNING: Failed to initialize Radios\n");
syslog(LOG_ERR, "WARNING: Failed to initialize tunnels\n");
}
strcpy(tunname, "uhf%d");
ret = hn70ap_tun_init(tunname);
ret = hn70ap_tun_initdevice(tunname);
if(ret != 0)
{
syslog(LOG_ERR, "WARNING: Failed to initialize TUN interface\n");
}
ret = hn70ap_radio_init();
if(ret != 0)
{
syslog(LOG_ERR, "WARNING: Failed to initialize Radios\n");
}
// bind the tunnel and the aux radio
hn70ap_system_initialized = true;
return OK;
......
......@@ -39,6 +39,7 @@
#include <fcntl.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/ioctl.h>
......@@ -46,7 +47,58 @@
#include <hn70ap/tun.h>
int hn70ap_tun_init(char ifname[IFNAMSIZ])
struct iptunnel_s
{
int fd;
pthread_t rxthread;
/*data reception and calling back to the user*/
FAR uint8_t * userbuf;
int userbuflen;
tunrxfunction_f callback;
FAR void * arg;
};
struct iptunnel_s tunnels[2];
/****************************************************************************
* hn70ap_tun_transmit
****************************************************************************/
int hn70ap_tun_transmit(int tunnel, FAR uint8_t *buf, size_t len)
{
if(tunnel != 0 && tunnel != 1)
{
return -1;
}
return write(tunnels[tunnel].fd, buf, len);
}
/****************************************************************************
* hn70ap_tun_rxfunction
****************************************************************************/
int hn70ap_tun_rxfunction(int tunnel, tunrxfunction_f rx, FAR void *arg, FAR uint8_t *userbuf, int userbuflen)
{
if(tunnel != 0 && tunnel != 1)
{
return -1;
}
tunnels[tunnel].userbuf = userbuf;
tunnels[tunnel].userbuflen = userbuflen;
tunnels[tunnel].arg = arg;
tunnels[tunnel].callback = rx;
return OK;
}
/****************************************************************************
* hn70ap_tun_init
****************************************************************************/
int hn70ap_tun_initdevice(char ifname[IFNAMSIZ])
{
struct ifreq ifr;
int errcode;
......@@ -70,6 +122,24 @@ int hn70ap_tun_init(char ifname[IFNAMSIZ])
return errcode;
}
//Start RX thread
return fd;
}
/****************************************************************************
* hn70ap_tun_addroute
****************************************************************************/
int hn70ap_tun_addroute(int fd, in_addr_t destination, int maskbits)
{
}
int hn70ap_tun_init(void)
{
tunnels[0].fd = 0;
tunnels[1].fd = 0;
return 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