#include "at.h" #include "board.h" #include "drv_gpio.h" #include "rtatomic.h" #include "rtthread.h" #include #include #include #define LOG_TAG "sim" #define DBG_LVL DBG_LOG // #define DBG_LVL DBG_INFO #define USE_LOG1 #define USE_LOG2 #define USE_LOG3 // #define USE_LOG4 #define USE_LOG5 // #define USE_LOG6 // #define USE_LOG_D #include "logn.h" #define BT_UART_NAME "uart2" #define BT_RST_PIN GET_PIN(C, 4) #define BT_LINK_STATUS_PIN GET_PIN(B, 6) #define BT_PIN_LINKED PIN_HIGH #define BT_PIN_UNLINKED PIN_LOW #define BT_AT_LINKED ((rt_atomic_t)1) #define BT_AT_UNLINKED ((rt_atomic_t)0) #define BT_AT_RETRY_TIMES 3 #define BT_LINK_TIMEOUT_MS 5000 #define BT24_SLEEP 0 #define BT24_POWER_ON 1 #define BT24_AT_READY 2 #define BT24_UNLINK 3 #define BT24_LINKED 4 #define BT24_TRANSPORT 5 static at_client_t at_client; #define AT_SEND_CMD(cmd, resp) \ do \ { \ rc = RT_EOK; \ if (at_obj_exec_cmd((at_client), (resp), (cmd)) < 0) \ { \ rc = -RT_ERROR; \ goto __exit; \ } \ } while (0) rt_err_t sim_call(char *phonenum) { rt_err_t rc = RT_EOK; at_response_t resp = NULL; resp = at_create_resp(256, 1, 5 * RT_TICK_PER_SECOND); if (!resp) { LOG_E("No memory for AT response structure!"); return -RT_ENOMEM; } char at_str[32]; rt_snprintf(at_str, sizeof(at_str), "ATD%s;", phonenum); AT_SEND_CMD(at_str, resp); at_resp_parse_line_args(resp, 1, "%s", at_str); if(!rt_strcasecmp(at_str, "error")) { LOG_E("Call failed!"); } __exit: if (resp) { at_delete_resp(resp); } return rc; } int cmd_at_send(int argc, char**argv) { rt_err_t rc = RT_EOK; at_response_t resp = NULL; resp = at_create_resp(256, 1, 5 * RT_TICK_PER_SECOND); if (!resp) { LOG_E("No memory for AT response structure!"); return -RT_ENOMEM; } char at_str[128]; AT_SEND_CMD(argv[1], resp); LOG5("%s",at_resp_get_line(resp, 1)); LOG5("%s",at_resp_get_line(resp, 2)); __exit: if (resp) { at_delete_resp(resp); } return rc; } MSH_CMD_EXPORT_ALIAS(cmd_at_send, AT_SEND, send AT command); static void urc_hang_up_handler(struct at_client *client, const char *buf, size_t len) { (void)client; LOG3("The call was hung up."); } static void urc_ok_handler(struct at_client *client, const char *buf, size_t len) { (void)client; LOG3("at receive OK."); } static void urc_error_handler(struct at_client *client, const char *buf, size_t len) { (void)client; LOG3("at receive ERROR."); } static struct at_urc urc_table[] = { {"NO CARRIER", "\r\n", urc_hang_up_handler}, {"OK", "\r\n", urc_ok_handler}, {"ERROR", "\r\n", urc_error_handler}, }; rt_err_t sim_dev_init() { // cmd_mq = ((struct bt_global *)parameter)->cmd_mq; // init pin // rt_pin_mode(BT_RST_PIN, PIN_MODE_OUTPUT_OD); // rt_pin_mode(BT_LINK_STATUS_PIN, PIN_MODE_INPUT); // init cmd lock // rt_mutex_init(&cmd_lock, "cmd_lock", RT_IPC_FLAG_FIFO); // init at client at_client_init(BT_UART_NAME, 128, 128); at_client = at_client_get_first(); if (at_client) { /* step2:修改串口配置参数 */ const struct serial_configure config = { .baud_rate = BAUD_RATE_115200, // 修改波特率为 115200 .data_bits = DATA_BITS_8, // 数据位 8 .stop_bits = STOP_BITS_1, // 停止位 1 .parity = PARITY_NONE, // 无奇偶校验位 .bit_order = BIT_ORDER_LSB, // 小端模式 .flowcontrol = RT_SERIAL_FLOWCONTROL_NONE, // 无流控 .invert = NRZ_NORMAL, // NRZ 正常模式 #if defined(RT_USING_SERIAL_V1) .bufsz = RT_SERIAL_RB_BUFSZ, #elif defined(RT_USING_SERIAL_V2) .rx_bufsz = BSP_UART2_RX_BUFSIZE, // 修改缓冲区 rx buff size 为 128 .tx_bufsz = BSP_UART2_TX_BUFSIZE, // 修改缓冲区 tx buff size 为 128 #endif // RT_USING_SERIAL_Vx }; /* step3:控制串口设备。通过控制接口传入命令控制字,与控制参数 */ rt_device_control(at_client->device, RT_DEVICE_CTRL_CONFIG, (void *)&config); /* 添加多种 URC 数据至 URC 列表中,当接收到同时匹配 URC 前缀和后缀的数据,执行 * URC 函数 */ at_set_urc_table(urc_table, sizeof(urc_table) / sizeof(urc_table[0])); LOG_I("sim initialization success"); return RT_EOK; } else { LOG_E("sim initialization failed"); return -RT_ERROR; } }