|
@@ -10,15 +10,22 @@
|
|
|
#include <aem_core_if.h>
|
|
|
#include <logging/log.h>
|
|
|
//#include <gps/gps.h>
|
|
|
+#include "aem_device.h"
|
|
|
+#include "aem_adapter_dev.h"
|
|
|
+#include <drivers/hrtimer.h>
|
|
|
|
|
|
LOG_MODULE_REGISTER(uart2_rec, LOG_LEVEL_INF);
|
|
|
#define CONFIG_UART2_REC_DEV_NAME "uart2_rec"
|
|
|
#define UART2_REC_DEV_NAME CONFIG_UART_2_NAME
|
|
|
|
|
|
-#define UART_FIFO_MAX 256
|
|
|
-#define REC_DATA_BUFFER_SIZE 256
|
|
|
-#define PRODUCT_INFO_SIZE 64
|
|
|
+// 定义数据区大小
|
|
|
+#define UART_FIFO_MAX 64
|
|
|
+#define REC_DATA_BUFFER_SIZE 192
|
|
|
+#define DEVICE_ID_SIZE 8
|
|
|
+#define PROJECT_ID_SIZE 4
|
|
|
#define WRITE_KEY_SIZE 160
|
|
|
+#define DEVICE_SN_SIZE 6
|
|
|
+#define FW_VERSION_SIZE 3
|
|
|
|
|
|
// 定义命令
|
|
|
#define DEVICE_ID_CMD 0xB1
|
|
@@ -26,6 +33,8 @@ LOG_MODULE_REGISTER(uart2_rec, LOG_LEVEL_INF);
|
|
|
#define WRITE_KEY_CMD 0xB3
|
|
|
#define IS_KEY_INSIDE_CMD 0xB4
|
|
|
#define SET_TIME_CMD 0xC1
|
|
|
+#define GET_DEVICE_SN_CMD 0xD1
|
|
|
+#define GET_FW_VERSION_CMD 0xD3
|
|
|
// 定义帧头和帧尾
|
|
|
#define FRAME_HEADER1 0xAA
|
|
|
#define FRAME_HEADER2 0x55
|
|
@@ -39,6 +48,7 @@ struct uart2_rec_data {
|
|
|
};
|
|
|
|
|
|
static struct uart2_rec_data uart2_rec_data;
|
|
|
+static struct hrtimer g_rtc_uart_proc;
|
|
|
uint8_t bySetTimeStep = 0;
|
|
|
|
|
|
extern uint8_t bySetHour;
|
|
@@ -50,6 +60,7 @@ extern uint8_t bySetDay;
|
|
|
|
|
|
extern void uart2_poll_out_ch(int c); // UART2发送一个字节
|
|
|
extern bool aem_read_aem_key_record(aem_key_w_record_t *record); // 读取AEM KEY记录
|
|
|
+extern bool aem_write_aem_key_record(uint8_t mothod, uint8_t is_pass); // 写入AEM KEY记录
|
|
|
extern bool aem_defender_factory_write_key(uint8_t * data, uint16_t len); // 写入KEY
|
|
|
extern uint16_t aem_factory_get_product_info(uint8_t *data, uint16_t in_len); // 获取产品信息
|
|
|
|
|
@@ -116,22 +127,73 @@ bool check_receive_frame(uint8_t *data)
|
|
|
return true; // 接收成功
|
|
|
}
|
|
|
|
|
|
-static uint8_t byAACnt = 2;
|
|
|
+static void uart_proc_timer_acts_handler(struct k_work *work)
|
|
|
+{
|
|
|
+ // 定义发送缓冲区
|
|
|
+ uint8_t tx_buff[2];
|
|
|
+ // 定义AEM密钥记录结构体
|
|
|
+ aem_key_w_record_t key_record;
|
|
|
+
|
|
|
+ // 根据接收到的命令,执行不同的操作
|
|
|
+ switch (uart2_rec_data.rec_data[2])
|
|
|
+ {
|
|
|
+ case WRITE_KEY_CMD:
|
|
|
+ // 发送缓冲区初始化
|
|
|
+ tx_buff[0] = 0;
|
|
|
+ // 调用写入密钥函数,如果写入成功
|
|
|
+ if (aem_defender_factory_write_key(&uart2_rec_data.rec_data[4], WRITE_KEY_SIZE))
|
|
|
+ {
|
|
|
+ aem_write_aem_key_record(0, 1); // 写入成功,写入NVRAM记录
|
|
|
+ tx_buff[0] = 0xAA;
|
|
|
+ }
|
|
|
+
|
|
|
+ send_frame(WRITE_KEY_CMD, tx_buff, 1);
|
|
|
+ break;
|
|
|
+
|
|
|
+ case IS_KEY_INSIDE_CMD:
|
|
|
+ tx_buff[0] = 0;
|
|
|
+ if (aem_read_aem_key_record(&key_record)) // 读取NVRAM记录
|
|
|
+ {
|
|
|
+ //if (&key_record != NULL)
|
|
|
+ {
|
|
|
+ if (key_record.is_pass)
|
|
|
+ tx_buff[0] = 0xAA;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ send_frame(IS_KEY_INSIDE_CMD, tx_buff, 1);
|
|
|
+ break;
|
|
|
+
|
|
|
+ default:
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|
|
|
+K_WORK_DEFINE(uart_proc_timer_acts, uart_proc_timer_acts_handler);
|
|
|
+static void htimer_uart_proc(struct hrtimer *ttimer, void *expiry_fn_arg)
|
|
|
+{
|
|
|
+ k_work_submit(&uart_proc_timer_acts); //向系统工作队列提交一个工作项,让工作队列的线程将执行该工作
|
|
|
+}
|
|
|
+
|
|
|
+static uint8_t byAACnt = 0;
|
|
|
+static uint8_t by55Cnt = 0;
|
|
|
static bool bReceiveFrameDone = false;
|
|
|
static void uart_fifo_callback(const struct device *dev, void *user_data)
|
|
|
{
|
|
|
uint8_t rx_buff[UART_FIFO_MAX];
|
|
|
uint8_t tx_buff[UART_FIFO_MAX];
|
|
|
+ char mac_str[7] = { 0 }; // MAC地址字符串
|
|
|
int read_size;
|
|
|
static uint8_t cnt;
|
|
|
- aem_key_w_record_t *key_record = NULL;
|
|
|
+ aem_dev_info_t info; // 定义AEM设备信息结构体
|
|
|
|
|
|
- printk("uart_fifo_callback start\n");
|
|
|
+ //printk("uart_fifo_callback start\n");
|
|
|
|
|
|
uart_irq_update(dev);
|
|
|
if (uart_irq_rx_ready(dev)) {
|
|
|
read_size = uart_fifo_read(dev, rx_buff, UART_FIFO_MAX);
|
|
|
- printk("read_size: %d\n", read_size);
|
|
|
+ //printk("read_size: %d\n", read_size);
|
|
|
if (read_size == UART_FIFO_MAX)
|
|
|
printk("uart fifo buffer overflow\n");
|
|
|
|
|
@@ -139,20 +201,35 @@ static void uart_fifo_callback(const struct device *dev, void *user_data)
|
|
|
{
|
|
|
switch (rx_buff[i]) {
|
|
|
case FRAME_HEADER1:
|
|
|
- byAACnt++;
|
|
|
- if (byAACnt > 2)
|
|
|
+ if (byAACnt == 0)
|
|
|
+ byAACnt++;
|
|
|
+ else if (byAACnt == 1 && by55Cnt == 2)
|
|
|
+ byAACnt++;
|
|
|
+
|
|
|
+ if (by55Cnt == 0 && byAACnt == 1)
|
|
|
{
|
|
|
- byAACnt = 1;
|
|
|
cnt = 0;
|
|
|
bReceiveFrameDone = false;
|
|
|
}
|
|
|
- else if (byAACnt == 2)
|
|
|
+ else if (by55Cnt == 2 && byAACnt == 2)
|
|
|
{
|
|
|
bReceiveFrameDone = true;
|
|
|
+ by55Cnt = 0;
|
|
|
+ byAACnt = 0;
|
|
|
}
|
|
|
uart2_rec_data.rec_data[cnt++] = rx_buff[i];
|
|
|
break;
|
|
|
-
|
|
|
+
|
|
|
+ case FRAME_HEADER2:
|
|
|
+ if (by55Cnt == 0 && cnt == 1)
|
|
|
+ by55Cnt++;
|
|
|
+
|
|
|
+ if (cnt > 4 && (cnt - 5 == uart2_rec_data.rec_data[3])) //长度大于4且与接收数据的长度相等
|
|
|
+ by55Cnt++;
|
|
|
+
|
|
|
+ uart2_rec_data.rec_data[cnt++] = rx_buff[i];
|
|
|
+ break;
|
|
|
+
|
|
|
default:
|
|
|
uart2_rec_data.rec_data[cnt++] = rx_buff[i];
|
|
|
|
|
@@ -170,8 +247,8 @@ static void uart_fifo_callback(const struct device *dev, void *user_data)
|
|
|
switch (uart2_rec_data.rec_data[2])
|
|
|
{
|
|
|
case DEVICE_ID_CMD:
|
|
|
- aem_factory_get_product_info(tx_buff, PRODUCT_INFO_SIZE);
|
|
|
- send_frame(PROJECT_ID_CMD, tx_buff, PRODUCT_INFO_SIZE);
|
|
|
+ aem_factory_get_product_info(tx_buff, 64);
|
|
|
+ send_frame(DEVICE_ID_CMD, tx_buff, DEVICE_ID_SIZE);
|
|
|
break;
|
|
|
|
|
|
case PROJECT_ID_CMD:
|
|
@@ -179,44 +256,48 @@ static void uart_fifo_callback(const struct device *dev, void *user_data)
|
|
|
tx_buff[1] = 0x6B;
|
|
|
tx_buff[2] = 0x3B;
|
|
|
tx_buff[3] = 0xD8;
|
|
|
- send_frame(PROJECT_ID_CMD, tx_buff, 4);
|
|
|
+ send_frame(PROJECT_ID_CMD, tx_buff, PROJECT_ID_SIZE);
|
|
|
break;
|
|
|
|
|
|
case WRITE_KEY_CMD:
|
|
|
- //uart_fifo_fill(dev,rx_buff,read_size);
|
|
|
+ case IS_KEY_INSIDE_CMD:
|
|
|
|
|
|
- tx_buff[0] = 0;
|
|
|
- if (aem_defender_factory_write_key(uart2_rec_data.rec_data, WRITE_KEY_SIZE))
|
|
|
+ hrtimer_init(&g_rtc_uart_proc, htimer_uart_proc, NULL);
|
|
|
+ hrtimer_start(&g_rtc_uart_proc, 1000*10, 0); //10ms 后执行回调函数
|
|
|
+ break;
|
|
|
+
|
|
|
+ case SET_TIME_CMD:
|
|
|
+ if (uart2_rec_data.rec_data[3] == 6) //时间数据长度为6
|
|
|
{
|
|
|
- tx_buff[0] = 0xAA;
|
|
|
+ bySetMinute = uart2_rec_data.rec_data[4];
|
|
|
+ bySetHour = uart2_rec_data.rec_data[5];
|
|
|
+ bySetDay = uart2_rec_data.rec_data[6];
|
|
|
+ bySetWeekday = uart2_rec_data.rec_data[7];
|
|
|
+ bySetMonth = uart2_rec_data.rec_data[8];
|
|
|
+ wSetYear = uart2_rec_data.rec_data[9];
|
|
|
+ bySetTimeStep = 1;
|
|
|
}
|
|
|
-
|
|
|
- send_frame(WRITE_KEY_CMD, tx_buff, 1);
|
|
|
+
|
|
|
+ send_frame(SET_TIME_CMD, tx_buff, 0);
|
|
|
break;
|
|
|
|
|
|
- case IS_KEY_INSIDE_CMD:
|
|
|
- tx_buff[0] = 0;
|
|
|
- aem_read_aem_key_record(key_record);
|
|
|
- if (key_record != NULL)
|
|
|
+ case GET_DEVICE_SN_CMD:
|
|
|
+ aem_get_ble_mac_addr(mac_str, 6);
|
|
|
+ for (int i = 0; i < DEVICE_SN_SIZE; i++)
|
|
|
{
|
|
|
- if (key_record->is_pass)
|
|
|
- tx_buff[0] = 0xAA;
|
|
|
+ tx_buff[i] = 0xFF - mac_str[i];
|
|
|
}
|
|
|
-
|
|
|
- send_frame(IS_KEY_INSIDE_CMD, tx_buff, 1);
|
|
|
+ send_frame(GET_DEVICE_SN_CMD, tx_buff, DEVICE_SN_SIZE);
|
|
|
break;
|
|
|
|
|
|
- case SET_TIME_CMD:
|
|
|
- bySetMinute = uart2_rec_data.rec_data[4];
|
|
|
- bySetHour = uart2_rec_data.rec_data[5];
|
|
|
- bySetDay = uart2_rec_data.rec_data[6];
|
|
|
- bySetWeekday = uart2_rec_data.rec_data[7];
|
|
|
- bySetMonth = uart2_rec_data.rec_data[8];
|
|
|
- wSetYear = uart2_rec_data.rec_data[9];
|
|
|
- bySetTimeStep = 1;
|
|
|
- send_frame(SET_TIME_CMD, tx_buff, 0);
|
|
|
+ case GET_FW_VERSION_CMD:
|
|
|
+ info = get_device_info();
|
|
|
+ tx_buff[0] = info.major_v; //主版本号
|
|
|
+ tx_buff[1] = info.minor_v; //次版本号
|
|
|
+ tx_buff[2] = info.micro_v; //补丁版本号
|
|
|
+ send_frame(GET_FW_VERSION_CMD, tx_buff, FW_VERSION_SIZE);
|
|
|
break;
|
|
|
-
|
|
|
+
|
|
|
default:
|
|
|
break;
|
|
|
}
|