barback32/main/lorcomm.c

48 lines
1.3 KiB
C

#include <string.h>
#include "esp_log.h"
#include "esp32-lora.h"
#include "lorcomm.h"
#define TAG "lorcomm"
uint8_t mac[6] = {0};
void send_message(lora32_cfg_t *lora, char *text) {
uint8_t len = strlen(text) + 1;
lora_packet packet = { 0 };
lora_msg msg = { 0 };
memcpy(packet.src, &mac[3], 3);
memset(packet.dst, 0xFF, 3);
packet.type = TEXT;
packet.payload = malloc(LORA_PACKET_HEADER_SIZE + len);
msg.length = len;
msg.msg = malloc(LORA_MSG_HEADER_SIZE + len);
memcpy(msg.msg, text, len);
uint8_t buf_len = LORA_PACKET_HEADER_SIZE + LORA_MSG_HEADER_SIZE + len;
uint8_t *send_buf = malloc(buf_len);
ESP_LOGI(TAG, "packet length: %d", buf_len);
// set src, dst, length
memcpy(send_buf, (uint8_t*)&packet, LORA_PACKET_HEADER_SIZE);
memcpy((uint8_t*)(send_buf + LORA_PACKET_HEADER_SIZE), (void*)&msg, LORA_MSG_HEADER_SIZE);
memcpy((uint8_t*)(send_buf + LORA_PACKET_HEADER_SIZE + LORA_MSG_HEADER_SIZE), (void*)msg.msg, len);
ESP_LOGI(TAG, "src: [%02X:%02X:%02X]", packet.src[0], packet.src[1], packet.src[2]);
ESP_LOGI(TAG, "dst: [%02X:%02X:%02X]", packet.dst[0], packet.dst[1], packet.dst[2]);
lora32_send(lora, send_buf, buf_len);
free(packet.payload);
packet.payload = NULL;
free(msg.msg);
msg.msg = NULL;
free(send_buf);
}