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

Refactor, add radio transmission code

parent edf957ac
No related branches found
No related tags found
No related merge requests found
......@@ -48,7 +48,9 @@
#include <fcntl.h>
#include <unistd.h>
#include <hn70ap/system.h>
#include <hn70ap/eeprom.h>
#include <hn70ap/radio.h>
/****************************************************************************
* Public Functions
......@@ -72,10 +74,8 @@ int main(int argc, FAR char *argv[])
int beacon_main(int argc, char *argv[])
#endif
{
char *devname = "/dev/raux";
char *data;
int ret = OK;
int fd;
int buflen = 1024;
uint32_t seqnum = 0;
char call[9];
......@@ -83,22 +83,20 @@ int beacon_main(int argc, char *argv[])
uint8_t *buf;
if(argc == 3)
{
devname = argv[1];
data = argv[2];
}
else if(argc == 2)
if(argc == 2)
{
data = argv[1];
}
else if(argc == 1)
else
{
data = "HELLO 73";
}
else
ret = hn70ap_system_init();
if(ret != 0)
{
return beacon_usage();
fprintf(stderr, "hn70ap init failed!\n");
return ERROR;
}
buf = malloc(buflen);
......@@ -110,15 +108,7 @@ int beacon_main(int argc, char *argv[])
}
fd = open(devname, O_RDWR);
if(fd<0)
{
fprintf(stderr, "open failed!\n");
ret = ERROR;
goto retfree;
}
printf("\nTX beacon using %s\n", devname);
printf("\nTX beacon using aux radio\n");
/* Define payload */
hn70ap_eeconfig_getcall("call", call);
......@@ -129,14 +119,14 @@ int beacon_main(int argc, char *argv[])
{
fprintf(stderr, "call sign not defined, see config app\n");
ret = ERROR;
goto retclose;
goto retfree;
}
while(1)
{
sprintf(buf, "DE %s/%d HN70AP TEST BEACON SEQ %u: %s\n", call, ssid, seqnum, data);
printf("%s", buf);
ret = write(fd, buf, strlen(buf));
sprintf((char*)buf, "DE %s/%d HN70AP TEST BEACON SEQ %u: %s\n", call, ssid, seqnum, data);
printf("%s", (char*)buf);
ret = hn70ap_radio_transmit(HN70AP_RADIO_AUX, buf, strlen((char*)buf));
if(ret < 0)
{
printf("write failed, errno=%d\n", errno);
......@@ -146,8 +136,6 @@ int beacon_main(int argc, char *argv[])
seqnum += 1;
}
retclose:
close(fd);
retfree:
free(buf);
done:
......
......@@ -47,7 +47,7 @@
#define LED_HEARTBEAT 5
#define LED_MACLINK 6
int leds_init(void);
int leds_state(int led, int state);
int hn70ap_leds_init(void);
int hn70ap_leds_state(int led, int state);
#endif //HN70AP_TIMER
/****************************************************************************
* hn70ap/apps/export/radio.h
*
* Copyright (C) 2018 Sebastien Lorquet. All rights reserved.
* Author: Sebastien Lorquet <sebastien@lorquet.fr>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
#ifndef HN70AP_RADIO_H
#define HN70AP_RADIO_H
#include <stdint.h>
#include <stdbool.h>
#define HN70AP_RADIO_MAIN 1
#define HN70AP_RADIO_AUX 2
int hn70ap_radio_init(void);
int hn70ap_radio_transmit(uint8_t device, uint8_t *buf, size_t len);
#endif /* HN70AP_SYSTEM_H */
......@@ -39,7 +39,7 @@
#include <stdint.h>
#include <stdbool.h>
int hn70ap_systeminit(void);
int hn70ap_system_init(void);
#endif /* HN70AP_SYSTEM_H */
......@@ -36,6 +36,6 @@
#ifndef HN70AP_TIMER
#define HN70AP_TIMER
int timer_init(void);
int hn70ap_timer_init(void);
#endif //HN70AP_TIMER
......@@ -36,7 +36,7 @@
-include $(TOPDIR)/Make.defs
ASRCS =
CSRCS = eeprom.c leds.c timer.c
CSRCS = eeprom.c leds.c timer.c system.c radio.c
CSRCS += tlv.c crc.c sha256.c hdlc.c
CSRCS += update.c
......
......@@ -48,13 +48,13 @@
#include <hn70ap/timer.h>
#include <hn70ap/leds.h>
int leds_init(void)
int hn70ap_leds_init(void)
{
/* TODO open driver once and for all to speed up led update? */
return 0;
}
int leds_state(int lednum, int state)
int hn70ap_leds_state(int lednum, int state)
{
struct userled_s led;
int fd;
......
/****************************************************************************
* hn70ap/apps/libhn70ap/radio.c
*
* Copyright (C) 2018 Sebastien Lorquet. All rights reserved.
* Author: Sebastien Lorquet <sebastien@lorquet.fr>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
#include <nuttx/config.h>
#include <stdint.h>
#include <stdbool.h>
#include <fcntl.h>
#include <syslog.h>
#include <hn70ap/radio.h>
#include <hn70ap/leds.h>
static int fd_main;
static int fd_aux;
int hn70ap_radio_init(void)
{
int ret = 0;
#ifdef CONFIG_HN70AP_MAINRADIO
syslog(LOG_INFO, "Checking main radio\n");
fd_main = open("/dev/rmain", O_RDWR);
if(fd_main<0)
{
syslog(LOG_ERR, "Failed to access main radio!\n");
}
#endif
#ifdef CONFIG_HN70AP_AUXRADIO
syslog(LOG_INFO, "Checking aux radio\n");
fd_aux = open("/dev/raux", O_RDWR);
if(fd_aux<0)
{
syslog(LOG_ERR, "Failed to access aux radio!\n");
}
#endif
return ret;
}
int hn70ap_radio_transmit(uint8_t device, uint8_t *buf, size_t len)
{
int fd;
int ret;
if(device == HN70AP_RADIO_MAIN)
{
fd = fd_main;
}
else if(device == HN70AP_RADIO_AUX)
{
fd = fd_aux;
}
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);
/* Transmit */
ret = write(fd, buf, len);
/* Turn off radio LED */
hn70ap_leds_state(LED_1A, LED_STATE_OFF);
hn70ap_leds_state(LED_1B, LED_STATE_OFF);
return ret;
}
......@@ -38,50 +38,67 @@
#include <stdint.h>
#include <stdbool.h>
int hn70ap_systeminit(void)
#include <syslog.h>
#include <hn70ap/eeprom.h>
#include <hn70ap/timer.h>
#include <hn70ap/leds.h>
#include <hn70ap/radio.h>
static bool hn70ap_system_initialized = false;
int hn70ap_system_init(void)
{
int ret;
bool defaults;
if(hn70ap_system_initialized)
{
syslog(LOG_WARNING, "System already initialized\n");
return 0;
}
/* Initialize timer thread to help us schedule delays */
ret = timer_init();
ret = hn70ap_timer_init();
if(ret != 0)
{
printf("FATAL: Failed to initialize timers\n");
goto lfail;
syslog(LOG_ERR, "FATAL: Failed to initialize timers\n");
return ERROR;
}
/* Initialize the leds */
/* Requires timer for blinking */
ret = leds_init();
ret = hn70ap_leds_init();
if(ret != 0)
{
printf("FATAL: Failed to initialize Leds\n");
goto lfail;
syslog(LOG_ERR, "FATAL: Failed to initialize Leds\n");
return ERROR;
}
/* Initialize the EEPROM */
hn70ap_eeconfig_init(&defaults);
ret = hn70ap_eeconfig_init(&defaults);
if(ret != 0)
{
syslog(LOG_ERR, "FATAL: Failed to initialize EEPROM\n");
return ERROR;
}
if(defaults)
{
printf("WARNING: Default config values loaded in EEPROM\n");
syslog(LOG_ERR, "WARNING: Default config values loaded in EEPROM\n");
}
else
ret = hn70ap_radio_init();
if(ret != 0)
{
hn70ap_eeconfig_getcall("call", call);
if(call[0] == 0)
{
printf("Callsign not defined yet, please use the config tool\n");
}
else
{
call[8] = 0;
printf("Hello %s, best 73's\n", call);
}
syslog(LOG_ERR, "FATAL: Failed to initialize Radios\n");
return ERROR;
}
return ret;
hn70ap_system_initialized = true;
return OK;
}
......@@ -62,7 +62,7 @@ struct timer_s *g_timers_tail;
static pthread_t g_timerthreadid;
static int heartbeat;
void *timer_thread(void *arg)
static void *timer_thread(void *arg)
{
sem_t sem;
struct timespec tout;
......@@ -84,7 +84,7 @@ void *timer_thread(void *arg)
sem_timedwait(&sem, &tout);
/* Toggle the heartbeat LED */
leds_state(LED_HEARTBEAT, (heartbeat<9)?LED_STATE_OFF:LED_STATE_ON);
hn70ap_leds_state(LED_HEARTBEAT, (heartbeat<9)?LED_STATE_OFF:LED_STATE_ON);
heartbeat += 1;
if(heartbeat == 10) heartbeat = 0;
......@@ -93,7 +93,7 @@ void *timer_thread(void *arg)
return NULL;
}
int timer_init(void)
int hn70ap_timer_init(void)
{
int ret;
......@@ -106,7 +106,7 @@ int timer_init(void)
return ret;
}
int timer_add(void (*callback)(void*arg), void *arg, uint32_t delay, bool repeat)
int hn70ap_timer_add(void (*callback)(void*arg), void *arg, uint32_t delay, bool repeat)
{
return -1;
}
......
......@@ -44,12 +44,14 @@
#include <sys/mount.h>
#include <hn70ap/eeprom.h>
#include <hn70ap/timer.h>
#include <hn70ap/system.h>
#include <hn70ap/leds.h>
#include <hn70ap/eeprom.h>
#include "sysdaemon_internal.h"
int nsh_main(int argc, char **argv);
/****************************************************************************
* Public Functions
****************************************************************************/
......@@ -83,22 +85,30 @@ int sysdaemon_main(int argc, char *argv[])
#endif
{
int ret;
bool defaults;
char call[9];
printf("\nhn70ap system daemon starting\n");
ret = hn70ap_systeminit();
ret = hn70ap_system_init();
hn70ap_mount_storage();
hn70ap_netmonitor_init();
leds_state(LED_GREEN, LED_STATE_ON);
if(ret == 0)
hn70ap_leds_state(LED_GREEN, LED_STATE_ON);
if(ret == OK)
{
leds_state(LED_RED, LED_STATE_ON);
hn70ap_leds_state(LED_RED, LED_STATE_ON);
}
hn70ap_mount_storage();
hn70ap_netmonitor_init();
hn70ap_eeconfig_getcall("call", call);
if(call[0] == 0)
{
printf("Callsign not defined yet, please use the config tool\n");
}
else
{
call[8] = 0;
printf("Hello %s, best 73's\n", call);
}
printf("TODO start screen management\n");
......@@ -110,13 +120,8 @@ int sysdaemon_main(int argc, char *argv[])
#endif
printf("Back from nsh, now sleeping forever.\n");
hn70ap_leds_state(LED_GREEN, LED_STATE_OFF);
hn70ap_leds_state(LED_RED , LED_STATE_ON);
return 0;
lfail:
/* Panic... something could not be initialized
* Try to switch on the red LED
*/
leds_state(LED_RED, LED_STATE_ON);
return ERROR;
}
......@@ -146,7 +146,7 @@ static void netmonitor_ifup(void* arg)
syslog(LOG_INFO, "Interface is going UP\n");
/* Enable the link LED */
leds_state(LED_MACLINK, LED_STATE_ON);
hn70ap_leds_state(LED_MACLINK, LED_STATE_ON);
dhcp_negociate();
}
......@@ -157,7 +157,7 @@ static void netmonitor_ifdown(void)
syslog(LOG_INFO, "Interface is going DOWN\n");
/* Disable the link LED */
leds_state(LED_MACLINK, LED_STATE_OFF);
hn70ap_leds_state(LED_MACLINK, LED_STATE_OFF);
}
/*----------------------------------------------------------------------------*/
......
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