<commit_before>/*
 *  File: rn2483.c
 *  Description: Model for LoRaWAN tranceiver "Microchip RN2483"
 *  Author: Sam
 */

#include "../../../Board.h"
#include "rn2483.h"
#include "../comm.h"
#include "uart_helper.h"
#include <serial.h>
#include "../../lib/printf.h"
#include "../../lib/mavlink/mavlink_helpers.h"

#define RN2483_RXBUFFER_SIZE 20
#define RN2483_READ_TIMEOUT 1000
#define RN2483_BAUD_RATE 57600

static UART_Handle rn2483_uart;
SerialDevice *lora_serialdev;
static UART_SerialDevice lora_uart_dev;
static int rn2483_initialised = 0;

#ifdef CONFIG_MODE
//responses from the modem are "encapsulated" in "\r\n"
#define RN2483_CONFIG_LENGTH 50 //maximum length of the below 4 strings
	#ifdef CONFIG_SWISSCOM
		static const char rn_set_nwkskey[] ="mac set nwkskey A4F613D0D65CC3DA3D874D82E12265B8\r\n";
		static const char rn_set_appskey[] ="mac set appskey 32BC3ABB9E1ADC0594811AEB1CCA704F\r\n";
		static const char rn_set_devaddr[] ="mac set devaddr 08050046\r\n";
		static const char rn_set_deveui[] ="mac set deveui F03D291000000046\r\n";
		static const char rn_save[] ="mac save\r\n";
	#else //thethingsnetwork login:
//		static const char rn_set_nwkskey[] ="mac set nwkskey 2B7E151628AED2A6ABF7158809CF4F3C\r\n"; //(old config values)
//		static const char rn_set_appskey[] ="mac set appskey <KEY>";
//		static const char rn_set_devaddr[] ="mac set devaddr 08050046\r\n";
		static const char rn_set_nwkskey[] ="mac set nwkskey <KEY>";
		static const char rn_set_appskey[] ="mac set appkey 01020304050607080910111213141516\r\n";
		static const char rn_set_devaddr[] ="mac set devaddr 020312A1\r\n";
		static const char rn_set_deveui[] =	"mac set deveui F03D291000000046\r\n";
		static const char rn_set_appeui[] =	"mac set appeui 70b3d57ed0000172\r\n"; // Octanis 1 App on the Field Station
		static const char rn_save[] ="mac save\r\n";
	#endif
#endif


static const char rn_get_devaddr[] = "mac get devaddr\r\n";
static const char rn_get_deveui[] = "mac get deveui\r\n";
static const char rn_get_nwkskey[] = "mac get nwkskey\r\n";
static const char rn_get_appskey[] = "mac get appskey\r\n";


static const char rn_txbeacon[] = "mac tx uncnf 1 deadbeef\r\n";
static const char rn_txstart[] = "mac tx uncnf 1 ";
static const char rn_txstop[] = "\r\n";

static const char rn_join_abp[] = "mac join abp\r\n";
static const char rn_join_otaa[] = "mac join otaa\r\n";
static const char rn_reset[] = "sys reset\r\n";

static void rn2483_uart_open(UART_SerialDevice *dev) {
	static UART_Params uartParams;

	UART_Params_init(&uartParams);
	uartParams.writeDataMode = UART_DATA_BINARY;
	uartParams.readDataMode = UART_DATA_BINARY;
	uartParams.readReturnMode = UART_RETURN_FULL;
	uartParams.readTimeout = RN2483_READ_TIMEOUT;
    uartParams.writeMode = UART_MODE_BLOCKING;
	uartParams.readEcho = UART_ECHO_OFF;
	uartParams.baudRate = 57600;
	rn2483_uart = UART_open(Board_UART3_LORACOMM, &uartParams);

	 if (rn2483_uart == NULL) {
		System_abort("Error opening the UART");
	}

	dev->fntab = &UART_SerialDevice_fntab;
	dev->uart = rn2483_uart;
}

//must be called from within a task - this function will block!
int rn2483_begin(){

	//	reset the rn2483
    GPIO_write(Board_LORA_RESET_N, 0);

	Task_sleep(500);

#ifndef CONFIG_MODE
	rn2483_uart_open(&lora_uart_dev);
	lora_serialdev = (SerialDevice *)&lora_uart_dev;
#endif

	GPIO_write(Board_LORA_RESET_N, 1);
	char rn2483_rxBuffer[RN2483_RXBUFFER_SIZE];

//	UART_write(rn2483_uart, rn_reset, sizeof(rn_reset));
//	UART_read(rn2483_uart, rn2483_rxBuffer, sizeof(rn2483_rxBuffer));

	Task_sleep(500);
	UART_read(rn2483_uart, rn2483_rxBuffer, sizeof(rn2483_rxBuffer)); //clean the reset message
	UART_read(rn2483_uart, rn2483_rxBuffer, sizeof(rn2483_rxBuffer));
	memset(&rn2483_rxBuffer, 0, sizeof(rn2483_rxBuffer));


//	/*check parameters*/
//	Task_sleep(500);
//	UART_write(rn2483_uart, rn_get_devaddr, sizeof(rn_get_devaddr));
//	UART_read(rn2483_uart, rn2483_rxBuffer, sizeof(rn2483_rxBuffer));
//	Task_sleep(500);

	UART_write(rn2483_uart, rn_get_deveui, sizeof(rn_get_deveui));
	UART_read(rn2483_uart, rn2483_rxBuffer, sizeof(rn2483_rxBuffer));
	Task_sleep(500);

	serial_printf(cli_stdout, "rn2483 deveui: %s.\r\n", rn2483_rxBuffer);

	UART_write(rn2483_uart, rn_join_otaa, sizeof(rn_join_otaa));
	Task_sleep(10000); // Time needed to complete OTAA.
	memset(&rn2483_rxBuffer, 0, sizeof(rn2483_rxBuffer));
	UART_read(rn2483_uart, rn2483_rxBuffer, sizeof(rn2483_rxBuffer));

	if(!strcmp("ok\r\naccepted\r\n", rn2483_rxBuffer)){
		serial_printf(cli_stdout, "rn2483 OTAA success: %s\r\n", rn2483_rxBuffer);
		rn2483_initialised = 1;
		return 1; //modem can now communicate with us
	}else{
		serial_printf(cli_stdout, "rn2483 error: OTAA failed. Message: %s\r\n", rn2483_rxBuffer);
		return 0;
	}

}


void rn2483_end(){

	rn2483_initialised = 0;
	UART_close(rn2483_uart);
	rn2483_uart = NULL;

	//put module to sleep
	//GPIO_write(Board_ROCKBLOCK_SLEEP, ROCKBLOCK_SLEEP);
}

#ifdef CONFIG_MODE
/*Used to configure the module with network data etc. */
int rn2483_config()
{
	int comm_result = 0;

	//	reset the rn2483
    GPIO_write(Board_LORA_RESET_N, 0);
    GPIO_write(Board_LORA_RESET_N, 1);
    	char rn2483_rxBuffer[RN2483_RXBUFFER_SIZE];
<commit_msg>red led when OTAA success<commit_after>/*
 *  File: rn2483.c
 *  Description: Model for LoRaWAN tranceiver "Microchip RN2483"
 *  Author: Sam
 */

#include "../../../Board.h"
#include "rn2483.h"
#include "../comm.h"
#include "uart_helper.h"
#include <serial.h>
#include "../../lib/printf.h"
#include "../../lib/mavlink/mavlink_helpers.h"

#define RN2483_RXBUFFER_SIZE 20
#define RN2483_READ_TIMEOUT 1000
#define RN2483_BAUD_RATE 57600

static UART_Handle rn2483_uart;
SerialDevice *lora_serialdev;
static UART_SerialDevice lora_uart_dev;
static int rn2483_initialised = 0;

#ifdef CONFIG_MODE
//responses from the modem are "encapsulated" in "\r\n"
#define RN2483_CONFIG_LENGTH 50 //maximum length of the below 4 strings
	#ifdef CONFIG_SWISSCOM
		static const char rn_set_nwkskey[] ="mac set nwkskey A4F613D0D65CC3DA3D874D82E12265B8\r\n";
		static const char rn_set_appskey[] ="mac set appskey 32BC3ABB9E1ADC0594811AEB1CCA704F\r\n";
		static const char rn_set_devaddr[] ="mac set devaddr 08050046\r\n";
		static const char rn_set_deveui[] ="mac set deveui F03D291000000046\r\n";
		static const char rn_save[] ="mac save\r\n";
	#else //thethingsnetwork login:
//		static const char rn_set_nwkskey[] ="mac set nwkskey <KEY>"; //(old config values)
//		static const char rn_set_appskey[] ="mac set appskey <KEY>";
//		static const char rn_set_devaddr[] ="mac set devaddr 08050046\r\n";
		static const char rn_set_nwkskey[] ="mac set nwkskey <KEY>";
		static const char rn_set_appskey[] ="mac set appkey 01020304050607080910111213141516\r\n";
		static const char rn_set_devaddr[] ="mac set devaddr 020312A1\r\n";
		static const char rn_set_deveui[] =	"mac set deveui F03D291000000046\r\n";
		static const char rn_set_appeui[] =	"mac set appeui 70b3d57ed0000172\r\n"; // Octanis 1 App on the Field Station
		static const char rn_save[] ="mac save\r\n";
	#endif
#endif


static const char rn_get_devaddr[] = "mac get devaddr\r\n";
static const char rn_get_deveui[] = "mac get deveui\r\n";
static const char rn_get_nwkskey[] = "mac get nwkskey\r\n";
static const char rn_get_appskey[] = "mac get appskey\r\n";


static const char rn_txbeacon[] = "mac tx uncnf 1 deadbeef\r\n";
static const char rn_txstart[] = "mac tx uncnf 1 ";
static const char rn_txstop[] = "\r\n";

static const char rn_join_abp[] = "mac join abp\r\n";
static const char rn_join_otaa[] = "mac join otaa\r\n";
static const char rn_reset[] = "sys reset\r\n";

static void rn2483_uart_open(UART_SerialDevice *dev) {
	static UART_Params uartParams;

	UART_Params_init(&uartParams);
	uartParams.writeDataMode = UART_DATA_BINARY;
	uartParams.readDataMode = UART_DATA_BINARY;
	uartParams.readReturnMode = UART_RETURN_FULL;
	uartParams.readTimeout = RN2483_READ_TIMEOUT;
    uartParams.writeMode = UART_MODE_BLOCKING;
	uartParams.readEcho = UART_ECHO_OFF;
	uartParams.baudRate = 57600;
	rn2483_uart = UART_open(Board_UART3_LORACOMM, &uartParams);

	 if (rn2483_uart == NULL) {
		System_abort("Error opening the UART");
	}

	dev->fntab = &UART_SerialDevice_fntab;
	dev->uart = rn2483_uart;
}

//must be called from within a task - this function will block!
int rn2483_begin(){

	//	reset the rn2483
    GPIO_write(Board_LORA_RESET_N, 0);

	Task_sleep(500);

#ifndef CONFIG_MODE
	rn2483_uart_open(&lora_uart_dev);
	lora_serialdev = (SerialDevice *)&lora_uart_dev;
#endif

	GPIO_write(Board_LORA_RESET_N, 1);
	char rn2483_rxBuffer[RN2483_RXBUFFER_SIZE];

//	UART_write(rn2483_uart, rn_reset, sizeof(rn_reset));
//	UART_read(rn2483_uart, rn2483_rxBuffer, sizeof(rn2483_rxBuffer));

	Task_sleep(500);
	UART_read(rn2483_uart, rn2483_rxBuffer, sizeof(rn2483_rxBuffer)); //clean the reset message
	UART_read(rn2483_uart, rn2483_rxBuffer, sizeof(rn2483_rxBuffer));
	memset(&rn2483_rxBuffer, 0, sizeof(rn2483_rxBuffer));


//	/*check parameters*/
//	Task_sleep(500);
//	UART_write(rn2483_uart, rn_get_devaddr, sizeof(rn_get_devaddr));
//	UART_read(rn2483_uart, rn2483_rxBuffer, sizeof(rn2483_rxBuffer));
//	Task_sleep(500);

	UART_write(rn2483_uart, rn_get_deveui, sizeof(rn_get_deveui));
	UART_read(rn2483_uart, rn2483_rxBuffer, sizeof(rn2483_rxBuffer));
	Task_sleep(500);

	serial_printf(cli_stdout, "rn2483 deveui: %s.\r\n", rn2483_rxBuffer);

	UART_write(rn2483_uart, rn_join_otaa, sizeof(rn_join_otaa));
	Task_sleep(10000); // Time needed to complete OTAA.
	memset(&rn2483_rxBuffer, 0, sizeof(rn2483_rxBuffer));
	UART_read(rn2483_uart, rn2483_rxBuffer, sizeof(rn2483_rxBuffer));

	if(!strcmp("ok\r\naccepted\r\n", rn2483_rxBuffer)){
		serial_printf(cli_stdout, "rn2483 OTAA success: %s\r\n", rn2483_rxBuffer);
		rn2483_initialised = 1;
		GPIO_write(Board_LED_RED,1);
		return 1; //modem can now communicate with us
	}else{
		serial_printf(cli_stdout, "rn2483 error: OTAA failed. Message: %s\r\n", rn2483_rxBuffer);
		return 0;
	}

}


void rn2483_end(){

	rn2483_initialised = 0;
	UART_close(rn2483_uart);
	rn2483_uart = NULL;

	//put module to sleep
	//GPIO_write(Board_ROCKBLOCK_SLEEP, ROCKBLOCK_SLEEP);
}

#ifdef CONFIG_MODE
/*Used to configure the module with network data etc. */
int rn2483_config()
{
	int comm_result = 0;

	//	reset the rn2483
    GPIO_write(Board_LORA_RESET_N, 0);
    GPIO_write(Board_LORA_RESET_N, 1);
    	char rn2483_rxBuffer[RN2483_RXBUFFER_SIZE];

{'fraction_non_alphanumeric': 0.11889718552556003, 'fraction_numerical': 0.0842427723530538, 'mean_word_length': 5.440813810110974, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 4, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6858135', 'n_tokens_mistral': 4878, 'n_tokens_neox': 4241, 'n_words': 909}