MAX_CARLINK_A270S/MXC_A27-PCB4.5-270S/app/moto/protocol/conversation_protocol.c

422 lines
13 KiB
C

#include "awtk.h"
#include "conversation_protocol.h"
#include "chip.h"
#include "board.h"
#include "serial.h"
#include "sysinfo.h"
#include "user_data.h"
#include "data_port.h"
#include "universal.h"
#define CALL_NUM_SIZE 20
extern uint8_t cmdstr[64];
extern uint8_t cmdlen;
// uint8_t cmdstr[64];
// uint8_t cmdlen = 0;
uint8_t bt2_call_answer = 0;//接听标志
uint8_t bt2_call_hangup = 0;//挂断标志
uint8_t bt2_recall_answer = 0;//接听2标志
uint8_t bt2_recall_hangup = 0;//挂断2标志
uint8_t bt2_switch = 0;//发送蓝牙开关标志
uint8_t bt2_switch_state = 0;//蓝牙状态标志
uint8_t bt2_switch_time = 0;//开关蓝牙时间限制
uint8_t call_time = 0;//命令延迟限制
uint8_t complex_call = 0;//多重来电标志
static uint8_t error_answer_state =0;//接电话失败,切换命令再次接听电话
static uint8_t error_hang_up_state =0;//挂断电话失败,切换命令再次挂断电话标志
static uint8_t return_call_state = 0;//返回的call标志
static uint8_t call_state_time = 0;
static uint8_t test_call_state = 0;//暂停通话标志
static uint8_t bt2_call_state = 0;//查询当前蓝牙状态信息
uint8_t bt2_state = 0;
uint8_t bt2_state_now_flag = 0;
void uart_test_tx_demo_thread(void *param)
{
UartPort_t *uap = param;
uint8_t uarttx_CH[32] = "AT#CH\r\n";//接听 挂断
uint8_t uarttx_CJ1[32] = "AT#CJ=01\r\n";//挂断
uint8_t uarttx_CJ2[32] = "AT#CJ=02\r\n";//挂断1接听2
uint8_t uarttx_CJ3[32] = "AT#CJ=03\r\n";//挂断2
uint8_t uarttx_CJ4[32] = "AT#CJ=04\r\n";//挂断2
uint8_t uarttx_CJ5[32] = "AT#CJ=05\r\n";//挂断2
uint8_t uarttx_CO[32] = "AT#CO\r\n";//开蓝牙
uint8_t uarttx_CP[32] = "AT#CP\r\n";//关蓝牙
uint8_t uarttx_CT[32] = "AT#CT\r\n";//查询当前状态
for (;;) {
if(bt2_switch_time){
bt2_switch_time--;
}
if(call_time){
call_time--;
}
if(call_state_time){
call_state_time--;
}
if(test_call_state){//暂停通话改为正常通话
test_call_state = 0;
DEBUG_PRINT("tx >> ------------CJ4\r\n");
iUartWrite(uap, uarttx_CJ4, strlen((char*)uarttx_CJ4), pdMS_TO_TICKS(100));
}
if(bt2_call_state || bt2_state){
DEBUG_PRINT("#####AT#CT \r\n");
iUartWrite(uap, uarttx_CT, strlen((char*)uarttx_CT), pdMS_TO_TICKS(100));
bt2_call_state = 0;
bt2_state = 0;
bt2_state_now_flag = 1;
}
if(Get_sys_call_key_state()==1 && !call_state_time){//接听电话1 //挂断电话1接听电话2
DEBUG_PRINT("tx >> AT#CT \r\n");
iUartWrite(uap, uarttx_CT, strlen((char*)uarttx_CT), pdMS_TO_TICKS(100));
call_state_time = 5;
}else if(Get_sys_call_key_state()==2 && !call_state_time){//挂断电话 //挂断电话2
DEBUG_PRINT("tx >> AT#CT \r\n");
iUartWrite(uap, uarttx_CT, strlen((char*)uarttx_CT), pdMS_TO_TICKS(100));
call_state_time = 5;
}
switch(return_call_state){
case 1://接听1
DEBUG_PRINT(">>>>>> AT#CH \r\n");
iUartWrite(uap, uarttx_CH, strlen((char*)uarttx_CH), pdMS_TO_TICKS(100));
bt2_call_answer = 1;
return_call_state = 0;
break;
case 2://挂1接2
DEBUG_PRINT(">>>>>> AT#CJ2 \r\n");
iUartWrite(uap, uarttx_CJ2, strlen((char*)uarttx_CJ2), pdMS_TO_TICKS(100));
bt2_recall_answer = 1;
return_call_state = 0;
break;
case 3://挂1
DEBUG_PRINT(">>>>>> AT#CJ1 \r\n");
iUartWrite(uap, uarttx_CJ1, strlen((char*)uarttx_CJ1), pdMS_TO_TICKS(100));
bt2_call_hangup = 1;
return_call_state = 0;
break;
case 4://拒接2
DEBUG_PRINT(">>>>>> AT#CJ3 \r\n");
iUartWrite(uap, uarttx_CJ3, strlen((char*)uarttx_CJ3), pdMS_TO_TICKS(100));
bt2_recall_hangup = 1;
return_call_state = 0;
break;
default://常规
break;
}
if(bt2_switch == 1 && bt2_switch_time == 0){//关蓝牙
DEBUG_PRINT("\ntx >> AT#CP \r\n");
iUartWrite(uap, uarttx_CP, strlen((char*)uarttx_CP), pdMS_TO_TICKS(100));
bt2_switch_time = 100;
}else if (bt2_switch == 2 && bt2_switch_time == 0){//开蓝牙
DEBUG_PRINT("\ntx >> AT#CO \r\n");
iUartWrite(uap, uarttx_CO, strlen((char*)uarttx_CO), pdMS_TO_TICKS(100));
bt2_switch_time = 100;
}
if(error_hang_up_state == 1){//挂2失败 尝试一次挂1
DEBUG_PRINT("\nerror_hang_up_state == 1 tx >> AT#CJ=1 \r\n");
iUartWrite(uap, uarttx_CJ1, strlen((char*)uarttx_CJ1), pdMS_TO_TICKS(100));
error_hang_up_state = 0;
}else if(error_hang_up_state == 2){//挂1失败 尝试一次挂2
DEBUG_PRINT("\nerror_hang_up_state == 2 tx >> AT#CJ=3 \r\n");
iUartWrite(uap, uarttx_CJ3, strlen((char*)uarttx_CJ3), pdMS_TO_TICKS(100));
error_hang_up_state = 0;
}
if(error_answer_state == 1){//接1失败 尝试挂2接1
DEBUG_PRINT("\nerror_answer_state = 1 tx >> AT#CJ=2 \r\n");
iUartWrite(uap, uarttx_CJ2, strlen((char*)uarttx_CJ2), pdMS_TO_TICKS(100));
error_answer_state = 0;
}else if(error_answer_state == 2){//挂2接1失败 尝试接1
DEBUG_PRINT("\nerror_answer_state = 2 tx >> AT#CH \r\n");
iUartWrite(uap, uarttx_CH, strlen((char*)uarttx_CH), pdMS_TO_TICKS(100));
error_answer_state = 0;
}
vTaskDelay(50);
}
}
void BT2_init_close(void){
DEBUG_PRINT("BT2_init_close >>>>>>> bt2_switch_time = %d .\r\n",bt2_switch_time);
if(bt2_switch_time)
bt2_switch_time = 0;
bt2_switch = 1;
}
void BT2_init_open(void){
DEBUG_PRINT("BT2_open >>>>>>> bt2_switch_time = %d .\r\n",bt2_switch_time);
if(bt2_switch_time)
bt2_switch_time = 0;
bt2_switch = 2;
}
void BT2_Switch(int type){
if(bt2_switch_time == 0){
switch(type){
case 0://关闭蓝牙
// if(gpio_get_value(47)){
// gpio_direction_output(47, 0);
// }
bt2_switch = 1;
break;
case 1://开启蓝牙
// if(gpio_get_value(47)){
// gpio_direction_output(47, 0);
// }
bt2_switch = 2;
break;
default:
break;
}
}else{
DEBUG_PRINT("BT2_Switch error ! bt2_switch_time =%d .\r\n",bt2_switch_time);
}
}
// 解析接收到的 AT 指令
// void parseATCommand(uint8_t cmdstr[],uint8_t cmdlen) {
void parseATCommand(void) {
uint8_t str[64];
static uint8_t flag = 0;
uint8_t j=0;
for(uint8_t i=0;i<cmdlen;i++)
{
if(cmdstr[i] == 0x00) continue;
str[j] = cmdstr[i];
j++;
if(cmdstr[i]=='\n'){
if (strncmp(str, "+MC", 3) == 0) {//1路通话1路来电
DEBUG_PRINT("+MC --\n");
if(Get_sys_call_key_state()==1){//接听1
Set_sys_call_key_state(0);
DEBUG_PRINT("return_call_state = 1 --\n");
return_call_state = 1;
}else if(Get_sys_call_key_state()==2){//挂断1
Set_sys_call_key_state(0);
DEBUG_PRINT("return_call_state = 3 --\n");
return_call_state = 3;
}
} else if (strncmp(str, "+MI", 3) == 0) {//通话中
// 处理 +AT 指令
DEBUG_PRINT("+MI --\n");
if(Get_sys_call_state() == 1){
Set_sys_call_state(2);
}else if(Get_sys_call_state() == 3){
Set_sys_call_state(2);
}
if(Get_sys_call_key_state()==1){//接听1
return_call_state = 1;
}else if(Get_sys_call_key_state()==2){//挂断1
return_call_state = 3;
}
} else if (strncmp(str, "+OK", 3) == 0) {//回复OK
DEBUG_PRINT("+OK --\n");
if(bt2_switch == 1){//关蓝牙
bt2_switch = 0;
Set_sys_bt_on_off(0);
DEBUG_PRINT("close bt ok .\r\n");
}else if(bt2_switch == 2){//开蓝牙
bt2_switch = 0;
Set_sys_bt_on_off(1);
DEBUG_PRINT("open bt ok .\r\n");
}else if(bt2_call_answer == 1){//接电话1
bt2_call_answer = 0;
Set_sys_call_key_state(0);
DEBUG_PRINT("call answer ok .\r\n");
Set_sys_call_state(2);//接听
Set_sys_call_time(0);//时间归0
call_time=20;
}else if(bt2_call_hangup == 1){//挂电话1
bt2_call_hangup = 0;
Set_sys_call_key_state(0);
DEBUG_PRINT("call hang up ok .\r\n");
if(Get_sys_call_state()==4)
Set_sys_call_state(2);//状态为2
else if(Get_sys_call_state()==2)
Set_sys_call_state(0);//状态为0
call_time=20;
}else if(bt2_recall_answer == 1){//挂电话1接电话2
bt2_recall_answer = 0;
Set_sys_call_key_state(0);
Set_sys_call_state(4);
Set_sys_call_time(0);//时间归0
call_time=20;
}else if(bt2_recall_hangup == 1){//挂电话2
bt2_recall_hangup = 0;
Set_sys_call_key_state(0);
Set_sys_call_state(2);
call_time=20;
}
} else if (strncmp(cmdstr, "+CP", 3) == 0) {//蓝牙进入配对模式
DEBUG_PRINT("+CP --\n");
Set_sys_bt_connect_state(0);
Set_sys_call_state(0);//挂断蓝牙 call状态回归
Set_sys_call_key_state(0);
} else if (strncmp(cmdstr, "+CC", 3) == 0) {//蓝牙进入配对模式
DEBUG_PRINT("+CC --\n");
Set_sys_bt_connect_state(0);
Set_sys_call_state(0);//挂断蓝牙 call状态回归
Set_sys_call_key_state(0);
} else if (strncmp(cmdstr, "+CI", 3) == 0) {//蓝牙进入配对模式
DEBUG_PRINT("+CI --\n");
Set_sys_bt_connect_state(0);
Set_sys_call_state(0);//挂断蓝牙 call状态回归
Set_sys_call_key_state(0);
} else if (strncmp(str, "+M2I", 4) == 0) {//多路通话 即 有电话是保持状态
DEBUG_PRINT("+M2I --\n");
if(Get_sys_call_key_state()==1){//切换通话线路
Set_sys_call_key_state(0);
test_call_state = 1;
}else if(Get_sys_call_key_state()==2){//切换通话线路并挂断1 若两个通话同时存在则切换线路挂断通话 应该直接挂断通话
Set_sys_call_key_state(0);
if(Get_sys_call_state()==4){
// DEBUG_PRINT("4---------------------------------close call");
return_call_state = 3;
}else{
test_call_state = 1;
vTaskDelay(1500);
return_call_state = 3;
}
}else if(Get_sys_call_state()==3){
Set_sys_call_state(4);
}else
Set_sys_call_state(2);
} else if (strncmp(str, "+M2C", 4) == 0) {//1路通话1路来电
DEBUG_PRINT("+M2C --\n");
//按键
if(Get_sys_call_key_state()==1){//挂1接2
Set_sys_call_key_state(0);
return_call_state = 2;
}else if(Get_sys_call_key_state()==2){//拒2
Set_sys_call_key_state(0);
return_call_state = 4;
}
} else if (strncmp(str, "+ERROR", 6) == 0) {//回复ERROR
DEBUG_PRINT("+ERROR --\n");
if(bt2_switch == 1){//关蓝牙失败
bt2_switch = 0;
DEBUG_PRINT("close bt error .\r\n");
}else if(bt2_switch == 2){//开蓝牙失败
bt2_switch = 0;
DEBUG_PRINT("open bt error .\r\n");
}else if(bt2_call_answer == 1){//接电话
bt2_call_answer = 0;
DEBUG_PRINT("call1 answer error .\r\n");
Set_sys_call_key_state(0);
error_answer_state = 1;
}else if(bt2_call_hangup == 1){//挂电话
bt2_call_hangup = 0;
DEBUG_PRINT("call1 hang up error .\r\n");
Set_sys_call_key_state(0);
error_hang_up_state = 2;//挂1失败尝试一次挂2
}else if(bt2_recall_answer == 1){//接电话
bt2_recall_answer = 0;
DEBUG_PRINT("call2 answer error .\r\n");
Set_sys_call_key_state(0);
error_answer_state = 2;
}else if(bt2_recall_hangup == 1){//挂电话 回到状态2
bt2_recall_hangup = 0;
DEBUG_PRINT("call2 hang up error .\r\n");
Set_sys_call_key_state(0);
error_hang_up_state = 1;//挂2失败尝试挂1
}
} else if (strncmp(str, "+CALL_NUM:", 10) == 0) {//来电
// DEBUG_PRINT("+CALL_NUM-------------------------------\n");
//char str[20]={0};
char call_num[CALL_NUM_SIZE];
// 处理 +CALL_NUM 指令
char *start = str + 10;
char *end = strchr(start, '\r');
if (end) {
*end = '\0';
strncpy(call_num, start, CALL_NUM_SIZE - 1);
call_num[CALL_NUM_SIZE - 1] = '\0'; // 确保字符串以null结尾
if(call_time == 0){
// DEBUG_PRINT("call_state ========== %d .\r\n",Get_sys_call_state());
Set_sys_call_number(call_num);
Set_sys_call_renumber(call_num);
if(Get_sys_call_state()==0){
// DEBUG_PRINT("call num1-----------------\r\n");
Set_sys_call_state(1);
}else if(Get_sys_call_state()==2 || Get_sys_call_state()==4){
if(flag==2){
//电话2来电
// DEBUG_PRINT("call num2-----------------\r\n");
Set_sys_call_state(3);
flag = 0;
}
flag++;
}
call_time=20;
}
}
} else if (strncmp(str, "+PHONE_NUM:", 11) == 0) {//当前通话号码
DEBUG_PRINT("+PHONE_NUM --\n");
char call_num[CALL_NUM_SIZE];
// 处理 +PHONE_NUM 指令
char *start = str + 11;
char *end = strchr(start, '\r');
if (end) {
*end = '\0';
strncpy(call_num, start, CALL_NUM_SIZE - 1);
call_num[CALL_NUM_SIZE - 1] = '\0'; // 确保字符串以null结尾
//DEBUG_PRINT("PHONE_NUM: %s\n", call_num);
}
Set_sys_call_state(2);
memset(call_num, 0, sizeof(call_num));
} else if (strncmp(str, "+MA", 3) == 0) {//当前通话号码
DEBUG_PRINT("+MA --\n");
Set_sys_call_state(0);
Set_sys_bt_connect_state(1);
} else{
for(uint8_t t=0;t<j;t++){
DEBUG_PRINT("%c",str[t]);
}
DEBUG_PRINT("############################\r\n");
}
j=0;
bt2_state_now_flag = 0;
}
}
}