2022-02-20 11:09:49 +00:00
|
|
|
/*
|
|
|
|
* aprsis.c
|
|
|
|
*
|
|
|
|
* Created on: Feb 20, 2022
|
|
|
|
* Author: mateusz
|
|
|
|
*/
|
|
|
|
|
2022-02-23 22:05:39 +00:00
|
|
|
#include "aprsis.h"
|
|
|
|
#include "main.h"
|
2022-02-20 11:09:49 +00:00
|
|
|
|
2022-02-23 22:05:39 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2022-02-20 11:09:49 +00:00
|
|
|
|
2022-02-23 22:05:39 +00:00
|
|
|
srl_context_t * aprsis_serial_port;
|
2022-02-20 11:09:49 +00:00
|
|
|
|
2022-02-23 22:05:39 +00:00
|
|
|
gsm_sim800_state_t * aprsis_gsm_modem_state;
|
|
|
|
|
|
|
|
char aprsis_callsign_with_ssid[10];
|
|
|
|
|
|
|
|
int32_t aprsis_passcode;
|
|
|
|
|
|
|
|
char aprsis_login_string[64];
|
|
|
|
|
|
|
|
uint8_t aprsis_connected;
|
|
|
|
|
|
|
|
const char * aprsis_sucessfull_login = "# logresp\0";
|
|
|
|
|
2022-02-26 13:24:18 +00:00
|
|
|
uint32_t aprsis_last_keepalive_ts = 0;
|
|
|
|
|
2022-02-23 22:05:39 +00:00
|
|
|
void aprsis_init(srl_context_t * context, gsm_sim800_state_t * gsm_modem_state, char * callsign, uint8_t ssid, uint32_t passcode) {
|
|
|
|
aprsis_serial_port = context;
|
|
|
|
|
|
|
|
aprsis_gsm_modem_state = gsm_modem_state;
|
|
|
|
|
|
|
|
aprsis_passcode = (int32_t)passcode;
|
|
|
|
|
|
|
|
sprintf(aprsis_callsign_with_ssid, "%s-%d", callsign, ssid);
|
|
|
|
|
|
|
|
memset(aprsis_login_string, 0x00, 0x40);
|
|
|
|
|
|
|
|
sprintf(aprsis_login_string, "user %s pass %ld vers ParaMETEO %s \r\n", aprsis_callsign_with_ssid, aprsis_passcode, SW_VER);
|
|
|
|
|
|
|
|
aprsis_connected = 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void aprsis_connect_and_login(char * address, uint8_t address_ln, uint16_t port) {
|
|
|
|
|
|
|
|
if (aprsis_serial_port == 0 || aprsis_gsm_modem_state == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
char port_str[6];
|
|
|
|
|
|
|
|
uint8_t * receive_buff;
|
|
|
|
|
|
|
|
int8_t retval = 0xFF;
|
|
|
|
|
2022-02-26 13:24:18 +00:00
|
|
|
uint8_t offset = 0;
|
|
|
|
|
2022-02-23 22:05:39 +00:00
|
|
|
memset(port_str, 0x00, 0x6);
|
|
|
|
|
|
|
|
snprintf(port_str, 6, "%d", port);
|
|
|
|
|
|
|
|
// connecting has blocking I/O
|
|
|
|
retval = gsm_sim800_tcpip_connect(address, address_ln, port_str, 0x6, aprsis_serial_port, aprsis_gsm_modem_state);
|
|
|
|
|
|
|
|
// if connection was successful
|
|
|
|
if (retval == 0) {
|
|
|
|
// wait for hello message '# aprsc 2.1.10-gd72a17c'
|
|
|
|
retval = gsm_sim800_tcpip_receive(0, 0, aprsis_serial_port, aprsis_gsm_modem_state, 0, 2000);
|
|
|
|
|
|
|
|
if (retval == 0) {
|
|
|
|
receive_buff = srl_get_rx_buffer(aprsis_serial_port);
|
|
|
|
|
|
|
|
if (*receive_buff == '#' && *(receive_buff + 1) == ' ') {
|
|
|
|
gsm_sim800_tcpip_write((uint8_t *)aprsis_login_string, strlen(aprsis_login_string), aprsis_serial_port, aprsis_gsm_modem_state);
|
|
|
|
|
|
|
|
retval = gsm_sim800_tcpip_receive(0, 0, aprsis_serial_port, aprsis_gsm_modem_state, 0, 2000);
|
|
|
|
|
|
|
|
if (retval == 0) {
|
|
|
|
receive_buff = srl_get_rx_buffer(aprsis_serial_port);
|
|
|
|
|
2022-02-26 13:24:18 +00:00
|
|
|
// fast forward to beginning of response
|
|
|
|
for (offset = 0; offset < 8; offset++) {
|
|
|
|
if (*(receive_buff + offset) == '#') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
retval = strncmp(aprsis_sucessfull_login, (const char * )(receive_buff + offset), (size_t)9);
|
2022-02-23 22:05:39 +00:00
|
|
|
if (retval == 0) {
|
|
|
|
aprsis_connected = 1;
|
|
|
|
|
2022-02-26 13:24:18 +00:00
|
|
|
// wait for consecutive data
|
|
|
|
gsm_sim800_tcpip_async_receive(aprsis_serial_port, aprsis_gsm_modem_state, 0, 61000, aprsis_receive_callback);
|
|
|
|
|
2022-02-23 22:05:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2022-02-26 13:24:18 +00:00
|
|
|
|
|
|
|
void aprsis_receive_callback(srl_context_t* srl_context) {
|
|
|
|
|
|
|
|
// if something was actually received
|
|
|
|
if (srl_context->srl_rx_state == SRL_RX_DONE) {
|
|
|
|
// check if this is keepalive message
|
|
|
|
if (*(srl_get_rx_buffer(srl_context)) == '#') {
|
|
|
|
aprsis_last_keepalive_ts = main_get_master_time();
|
|
|
|
|
|
|
|
gsm_sim800_tcpip_async_receive(aprsis_serial_port, aprsis_gsm_modem_state, 0, 61000, aprsis_receive_callback);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|