123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768 |
- /*Includes ----------------------------------------------*/
- #include "tool.h"
- #include "nrf_delay.h"
- #include "system.h"
- #include "ble_comm.h"
- #include "bsp_time.h"
- #include "hal_led.h"
- #include "hal_charge.h"
- #include "hal_ble_common.h"
- #include "hal_ble_client.h"
- #include "hal_ble_host.h"
- #include "app_flash.h"
- #include "app_wireless_pair.h"
- #include "app_detectIsHost.h"
- /*Private macro ------------------------------------------------------------------------------------------------------------------------------------*/
- #define APP_WIRELESS_PAIR_FSMTABLE_LEN 8 //状态表长度
- #define APP_WIRELESS_PAIR_RSSI_THRESHOLD -18 //rssi阈值
- #define APP_WIRELESS_PAIR_RSSI_BUFF_LEN 15 //rssi缓冲区长度
- /*Struction ------------------------------------------------------------------------------------------------------------------------------------*/
- //定义状态表的数据类型
- typedef struct _app_wireless_pair_fsmtable_s
- {
- App_Wireless_Pair_Event_e event; //事件
- App_Wireless_Pair_State_e CurState; //当前状态
- int (*eventActFun)(); //函数指针
- App_Wireless_Pair_State_e NextState; //下一个状态
- }App_Wireless_Pair_fsmtable_s;
- //定义状态机类型
- typedef struct _app_wireless_pair_fsm_s
- {
- App_Wireless_Pair_State_e curState; //当前状态
- App_Wireless_Pair_fsmtable_s FsmTable[APP_WIRELESS_PAIR_FSMTABLE_LEN]; //状态表
- App_Wireless_Pair_Set_Scan_Cb set_scan_cb; //设置扫描回调
- App_Wireless_Pair_Set_Adv_Cb set_adv_cb; //设置广播回调
- uint8_t peer_addr[6]; //对端MAC地址
- bool is_busy; //是否忙碌
- uint8_t led_display_count; //亮灯次数
- uint8_t led_switch; //灯的开关
- char already_scan_or_adv_mac_buf[16]; //已经在扫描或广播的mac地址
- int rssi_buf[APP_WIRELESS_PAIR_RSSI_BUFF_LEN]; //rssi缓冲区
- }App_Wireless_Pair_Fsm_s;
- /*Local Variable ------------------------------------------------------------------------------------------------------------------------------------*/
- static App_Wireless_Pair_Fsm_s ob_app_wireless_pair_fsm;
- /*Local Functions ------------------------------------------------------------------------------------------------------------------------------------*/
- static void app_wireless_pair_slow_clear_rssi_buff_process(void);
- static int app_wireless_pair_set_scan_adv_on(void)
- {
- int ret = 0;
- int adv_stop = -1;
-
- //若是右鞋则进入主动扫描状态
- //若是左鞋则设置扫描回应为MAC地址状态
- if(mFlash.isHost)
- {
- if(slave_isconnect() == 0)
- {
- //已连接状态下,不能调用停止广播
- adv_stop = wireless_pair_advertising_stop();
- if(ob_app_wireless_pair_fsm.set_adv_cb != NULL && adv_stop == 0 && slave_isconnect() == 0)ret += ob_app_wireless_pair_fsm.set_adv_cb(0x01);
- else ret = -1;
- }
- nrf_ble_scan_stop();
- host_disconnect();
- //已连接状态下,不能调用停止扫描
- if(host_isconnect() == 0)
- {
- if(ob_app_wireless_pair_fsm.set_scan_cb != NULL)ret += ob_app_wireless_pair_fsm.set_scan_cb(0x00);
- else ret = -1;
- }
- else
- {
- ret = -1;
- }
- }
- else
- {
- if(host_isconnect() == 0)
- {
- //已连接状态下,不能调用停止扫描
- nrf_ble_scan_stop();
- if(ob_app_wireless_pair_fsm.set_scan_cb != NULL)ret += ob_app_wireless_pair_fsm.set_scan_cb(0x01);
- else ret = -1;
- }
- slave_disconnect();
- //已连接状态下,不能调用停止广播
- if(slave_isconnect() == 0)adv_stop = wireless_pair_advertising_stop();
- if(adv_stop == 0 && slave_isconnect() == 0)
- {
- if(ob_app_wireless_pair_fsm.set_adv_cb != NULL)ret += ob_app_wireless_pair_fsm.set_adv_cb(0x00);
- else ret = -1;
- }
- else
- {
- ret = -1;
- }
- }
-
- if(ret == 0)Process_Start(100,"app_wireless_pair_slow_clear_rssi_buff_process",app_wireless_pair_slow_clear_rssi_buff_process);
-
- return ret;
- }
- static int app_wireless_pair_set_scan_adv_off(void)
- {
- int ret = 0;
- char buf[16];
- int adv_stop = -1;
-
- //若是右鞋则进入被动扫描状态
- //若是左鞋则设置扫描回应为空数据状态
- if(mFlash.isHost)
- {
- if(slave_isconnect() == 0)
- {
- //已连接状态下,不能调用停止广播
- adv_stop = wireless_pair_advertising_stop();
- if(ob_app_wireless_pair_fsm.set_adv_cb != NULL && adv_stop == 0 && slave_isconnect() == 0)ret += ob_app_wireless_pair_fsm.set_adv_cb(0x00);
- else ret = -1;
- }
- nrf_ble_scan_stop();
- host_disconnect();
- //已连接状态下,不能调用停止扫描
- if(host_isconnect() == 0)
- {
- if(ob_app_wireless_pair_fsm.set_scan_cb != NULL)
- {
- sprintf(buf,"%02X%02X%02X%02X%02X%02X",mFlash.macHost[0],mFlash.macHost[1],mFlash.macHost[2],mFlash.mClient.macAddr[3],mFlash.mClient.macAddr[4],mFlash.mClient.macAddr[5]);
- host_set_scan_name(buf,strlen(buf));
- ret += ob_app_wireless_pair_fsm.set_scan_cb(0x00);
- }
- else ret = -1;
- }
- else
- {
- ret = -1;
- }
- }
- else
- {
- if(host_isconnect() == 0)
- {
- //已连接状态下,不能调用停止扫描
- //调用停止扫描,因为右鞋不需要,反而会干扰传输速度
- nrf_ble_scan_stop();
- }
- slave_disconnect();
- //已连接状态下,不能调用停止广播
- if(slave_isconnect() == 0)adv_stop = wireless_pair_advertising_stop();
- if(adv_stop == 0 && slave_isconnect() == 0)
- {
- if(ob_app_wireless_pair_fsm.set_adv_cb != NULL)
- {
- if(mFlash.mClient.isConfig == 'C')
- {
- sprintf(buf,"%02X%02X%02X%02X%02X%02X",mFlash.mClient.macAddr[0],mFlash.mClient.macAddr[1],mFlash.mClient.macAddr[2],mFlash.macHost[3],mFlash.macHost[4],mFlash.macHost[5]);
- }
- else
- {
- ble_gap_addr_t m_addr;
- uint32_t err_code;
- //获取自身mac地址
- err_code = sd_ble_gap_addr_get(&m_addr);
- APP_ERROR_CHECK(err_code);
- for(int i=0;i<6;i++)mFlash.macHost[i] = m_addr.addr[5-i];
- sprintf(buf,"%02X%02X%02X%02X%02X%02X",mFlash.macHost[0],mFlash.macHost[1],mFlash.macHost[2],mFlash.macHost[3],mFlash.macHost[4],mFlash.macHost[5]);
- }
- slave_set_adv_name(buf,strlen(buf));
- slave_adv_init();
- ret += ob_app_wireless_pair_fsm.set_adv_cb(0x00);
- }
- else ret = -1;
- }
- else
- {
- ret = -1;
- }
- }
-
- if(ret == 0)
- {
- ob_app_wireless_pair_fsm.led_switch = 1;
- memset(&ob_app_wireless_pair_fsm.peer_addr,0xFF,sizeof(ob_app_wireless_pair_fsm.peer_addr));
- for(int i=0;i<APP_WIRELESS_PAIR_RSSI_BUFF_LEN;i++)ob_app_wireless_pair_fsm.rssi_buf[i] = -500;
- Process_Stop(app_wireless_pair_slow_clear_rssi_buff_process);
- }
- return ret;
- }
- static int app_wireless_pair_unconditional_entry(void)
- {
- return 0; //不用做任何操作,直接进入下一个状态
- }
- static int app_wireless_pair_enter_in_process(int rssi, uint8_t *addr)
- {
- static uint8_t index = 0;
- int ret = 0;
- int rssi_max = -500;
- uint8_t null_addr[6] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
- if(ob_app_wireless_pair_fsm.is_busy == false)
- {
- ob_app_wireless_pair_fsm.rssi_buf[index++] = rssi;
- if(index == APP_WIRELESS_PAIR_RSSI_BUFF_LEN)index = 0;
-
- for(int i=0;i<APP_WIRELESS_PAIR_RSSI_BUFF_LEN;i++)rssi_max = (rssi_max > ob_app_wireless_pair_fsm.rssi_buf[i])?rssi_max:ob_app_wireless_pair_fsm.rssi_buf[i];
-
-
-
-
-
- // SEGGER_RTT_printf(0,"rssi:%d,%d,st:%d,curState:%d,active:%d,host:%d,slave:%d,mFlash.isHost:%d,led:%d,mac: ",rssi_max,rssi,NRF_RTC0->COUNTER, \
- // ob_app_wireless_pair_fsm.curState,wireless_pair_scan_get_active(),\
- // host_isconnect(),slave_isconnect(),mFlash.isHost,ob_app_wireless_pair_fsm.led_switch);
-
- if(mFlash.isHost)
- {
- extern char Target_scan[TARFET_LEN_MAX];
- // for(int i=0; i<6; i++)SEGGER_RTT_printf(0,"0x%x ",Target_scan[5-i]);
- }
- else
- {
- int len;
- char name[16];
- slave_get_advname_len(&len);
- slave_get_advname(name,len);
- // for(int i=0; i<6; i++)SEGGER_RTT_printf(0,"0x%x ",name[5-i]);
- }
-
- // if(rssi >= APP_WIRELESS_PAIR_RSSI_THRESHOLD)
- // {
- // SEGGER_RTT_printf(0," rssi_mac : ");for(int i=0; i<6; i++)SEGGER_RTT_printf(0,"0x%x ",addr[5-i]);SEGGER_RTT_printf(0,"\r\n");
- // }
- // else
- // SEGGER_RTT_printf(0,"\r\n");
-
-
- if(memcmp(addr,null_addr,6) != 0 && rssi >=APP_WIRELESS_PAIR_RSSI_THRESHOLD)
- {
- for(int i=0; i<6; i++)ob_app_wireless_pair_fsm.peer_addr[i] = addr[5-i];//这里是确保该rssi对应的mac
- ret = app_wireless_pair_EventHandle(APP_WIRELESS_PAIR_EVENT_RSSI_MEETS_REQUIREMENTS);
- }
- else if(rssi_max < APP_WIRELESS_PAIR_RSSI_THRESHOLD)
- {
- if(mFlash.isHost && host_isconnect() == 1)
- {
- ret = app_wireless_pair_EventHandle(APP_WIRELESS_PAIR_EVENT_RSSI_MEETS_REQUIREMENTS);
- }
- else if(!mFlash.isHost && slave_isconnect() == 1)
- {
- ret = app_wireless_pair_EventHandle(APP_WIRELESS_PAIR_EVENT_RSSI_MEETS_REQUIREMENTS);
- }
- else ret = app_wireless_pair_EventHandle(APP_WIRELESS_PAIR_EVENT_RSSI_DOES_NOT_MEETS_REQUIREMENTS);
- }
- else
- {
- if(memcmp(addr,null_addr,6) != 0)
- {
- ret = app_wireless_pair_EventHandle(APP_WIRELESS_PAIR_EVENT_RSSI_MEETS_REQUIREMENTS);
- }
- else
- {
- if(mFlash.isHost && host_isconnect() == 1)
- {
- ret = app_wireless_pair_EventHandle(APP_WIRELESS_PAIR_EVENT_RSSI_MEETS_REQUIREMENTS);
- }
- else if(!mFlash.isHost && slave_isconnect() == 1)
- {
- ret = app_wireless_pair_EventHandle(APP_WIRELESS_PAIR_EVENT_RSSI_MEETS_REQUIREMENTS);
- }
- else ret = app_wireless_pair_EventHandle(APP_WIRELESS_PAIR_EVENT_RSSI_DOES_NOT_MEETS_REQUIREMENTS);
- }
- }
-
-
-
-
- // if(rssi_max >= APP_WIRELESS_PAIR_RSSI_THRESHOLD)
- // {
- // if(memcmp(addr,null_addr,6) != 0)
- // {
- // ret = app_wireless_pair_EventHandle(APP_WIRELESS_PAIR_EVENT_RSSI_MEETS_REQUIREMENTS);
- // }
- // else
- // {
- // if(!mFlash.isHost && slave_isconnect() == 1)
- // {
- // ret = app_wireless_pair_EventHandle(APP_WIRELESS_PAIR_EVENT_RSSI_MEETS_REQUIREMENTS);
- // }
- // else ret = app_wireless_pair_EventHandle(APP_WIRELESS_PAIR_EVENT_RSSI_DOES_NOT_MEETS_REQUIREMENTS);
- // }
- // }
- // else
- // {
- // if(mFlash.isHost && host_isconnect() == 1)
- // {
- // ret = app_wireless_pair_EventHandle(APP_WIRELESS_PAIR_EVENT_RSSI_MEETS_REQUIREMENTS);
- // }
- // else if(!mFlash.isHost && slave_isconnect() == 1)
- // {
- // ret = app_wireless_pair_EventHandle(APP_WIRELESS_PAIR_EVENT_RSSI_MEETS_REQUIREMENTS);
- // }
- // else ret = app_wireless_pair_EventHandle(APP_WIRELESS_PAIR_EVENT_RSSI_DOES_NOT_MEETS_REQUIREMENTS);
- // }
- }
-
- return ret;
- }
- static void app_wireless_pair_slow_clear_rssi_buff_process(void)
- {
- uint8_t addr[6];
- memset(addr,0x00,6);
- app_wireless_pair_enter_in_process(APP_WIRELESS_PAIR_RSSI_THRESHOLD*2,addr);
- }
- static void display_led_process(void)
- {
- ob_app_wireless_pair_fsm.led_display_count++;
- if (ob_app_wireless_pair_fsm.led_display_count % 2 == 0)
- {
- LED_Start(LED_PAIR, COLOR_BLACK);
- }
- else
- {
- LED_Start(LED_PAIR, COLOR_BLUE);
- }
- if (ob_app_wireless_pair_fsm.led_display_count > 20)
- {
- ob_app_wireless_pair_fsm.led_display_count = 0;
- LED_Stop(LED_PAIR);
- Process_SetHoldOn(display_led_process, 0);
- Process_Stop(display_led_process);
- }
- }
- static void app_wireless_pair_start_display_led(void)
- {
- ob_app_wireless_pair_fsm.led_display_count = 0;
- Process_Start(500, "display_led_process", display_led_process);
- Process_SetHoldOn(display_led_process, 1);
- }
- static void app_wireless_pair_stop_display_led(void)
- {
- ob_app_wireless_pair_fsm.led_display_count = 0;
- LED_Stop(LED_PAIR);
- Process_SetHoldOn(display_led_process, 0);
- Process_Stop(display_led_process);
- }
- static void app_wireless_pair_in_process(void)
- {
- int ret = 0;
- uint8_t *connect_addr;
- uint8_t peer_addr[6]; //对端mac地址
- ble_gap_addr_t m_addr; //自身mac地址
- char buf[16];
- uint32_t err_code;
- int adv_stop = -1;
- uint8_t null_addr[6] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
- if(!get_LR_readly())
- {
- return;
- }
-
- if(ob_app_wireless_pair_fsm.curState == APP_WIRELESS_PAIR_STATE_PAIRING_IN_PROGRESS)
- {
- ob_app_wireless_pair_fsm.is_busy = true;
-
- //获取已连接的对端mac地址
- connect_addr = wireless_pair_get_connect_macaddr();
- //获取自身mac地址
- err_code = sd_ble_gap_addr_get(&m_addr);
- APP_ERROR_CHECK(err_code);
- //获取对端的mac地址
- for(int i=0;i<6;i++)peer_addr[i]=ob_app_wireless_pair_fsm.peer_addr[i];
-
-
- if(mFlash.isHost) //左鞋处理逻辑
- {
- //判断是否处于连接且连接的mac地址为配对地址
- if(host_isconnect() == 1 && memcmp(peer_addr,connect_addr,6) == 0)
- {
- //判断是否配对过,若已配对过,直接进入配对结束阶段。若无配对过,则记录MAC地址,清除步数,进入配对结束。
- if(memcmp(peer_addr,mFlash.mClient.macAddr,6) != 0)
- {
- //配置左鞋广播名字,已连接状态下,不能调用停止广播
- if(slave_isconnect() == 0)adv_stop = wireless_pair_advertising_stop();
- if(adv_stop == 0 && slave_isconnect() == 0)
- {
- sprintf(buf,"SH_%02X%02X",m_addr.addr[4], m_addr.addr[5]);
- slave_set_adv_name(buf,strlen(buf));
- slave_adv_init();
- ret = ob_app_wireless_pair_fsm.set_adv_cb(0x01);
- if(ret == 0)
- {
- for(int i = 0; i < 6; i++)
- {
- mFlash.mClient.macAddr[i] = peer_addr[i];
- mFlash.macHost[i] = m_addr.addr[5-i];
- mBackup.macAddr_L[i] = mFlash.macHost[i]; //主机地址
- mBackup.macAddr_R[i] = mFlash.mClient.macAddr[i]; //从机地址
- }
- mFlash.mClient.isConfig ='C';
- mBackup.hardVersion = mFlash.mClient.hardVersion;
- mBackup.sotfVersion = mFlash.mClient.sotfVersion;
- mBackup.isConfig = mFlash.mClient.isConfig;
- if(Flash_SaveBackup() != ZONE_OP_SUCCESS){Except_TxError(EXCEPT_Pair, "pair save backup fail");}
- if(Flash_DeleteAllStep() != ZONE_OP_SUCCESS){Except_TxError(EXCEPT_Pair, "pair clear step fail");}
- memset(&mFlash.mStep, 0, sizeof(FlashStep_t));
- if(Flash_SaveInfomation() != ZONE_OP_SUCCESS){Except_TxError(EXCEPT_Pair, "Flash_SaveInfomation fail");}
- app_wireless_pair_start_led();//如果连接上没靠近,要在这里加才会开线程。
- app_wireless_pair_EventHandle(APP_WIRELESS_PAIR_EVENT_PAIR_DONE);
- }
- }
- }
- else
- {
- app_wireless_pair_start_led();//如果连接上没靠近,要在这里加才会开线程。
- app_wireless_pair_EventHandle(APP_WIRELESS_PAIR_EVENT_PAIR_DONE);
- }
-
- if(ob_app_wireless_pair_fsm.curState == APP_WIRELESS_PAIR_STATE_END_OF_PAIRING && host_get_rssi() >= APP_WIRELESS_PAIR_RSSI_THRESHOLD)
- {
- if(slave_isconnect() == 0)adv_stop = wireless_pair_advertising_stop();
- if(adv_stop == 0 && slave_isconnect() == 0)
- {
- ob_app_wireless_pair_fsm.set_adv_cb(0x00);
- }
- }
- }
- else
- {
- //若不是,断开连接,设置扫描为新的配对地址
- sprintf(buf,"%02X%02X%02X%02X%02X%02X",m_addr.addr[5],m_addr.addr[4],m_addr.addr[3],peer_addr[3],peer_addr[4],peer_addr[5]);
- if(memcmp(ob_app_wireless_pair_fsm.already_scan_or_adv_mac_buf,buf,strlen(buf)) != 0 && memcmp(peer_addr,null_addr,6) != 0)
- {
- // SEGGER_RTT_printf(0,"scan_or_adv_mac_buf: %s\r\n",ob_app_wireless_pair_fsm.already_scan_or_adv_mac_buf);
- // SEGGER_RTT_printf(0,"buf: %s\r\n",buf);
- if(slave_isconnect() == 0)
- {
- //已连接状态下,不能调用停止广播
- adv_stop = wireless_pair_advertising_stop();
- if(adv_stop == 0 && slave_isconnect() == 0)ret += ob_app_wireless_pair_fsm.set_adv_cb(0x01);
- }
- nrf_ble_scan_stop();
- host_disconnect();
- //已连接状态下,不能调用停止扫描
- if(host_isconnect() == 0)
- {
- host_set_scan_name(buf,strlen(buf));
- ret += ob_app_wireless_pair_fsm.set_scan_cb(0x02);
- if(ret == 0){memcpy(ob_app_wireless_pair_fsm.already_scan_or_adv_mac_buf,buf,strlen(buf));}
- }
- }
- }
- }
- else //右鞋处理逻辑
- {
- //判断是否处于连接且连接的mac地址为配对地址
- if(slave_isconnect() == 1 && memcmp(peer_addr,connect_addr,6) == 0)
- {
- //判断是否配对过,若已配对过,直接进入配对结束阶段。若无配对过,则记录MAC地址,清除步数,进入配对结束。
- if(memcmp(peer_addr,mFlash.mClient.macAddr,6) != 0)
- {
- for(int i = 0; i < 6; i++)
- {
- mFlash.mClient.macAddr[i] = peer_addr[i];
- mFlash.macHost[i] = m_addr.addr[5-i];
- mBackup.macAddr_L[i] = mFlash.macHost[i]; //主机地址
- mBackup.macAddr_R[i] = mFlash.mClient.macAddr[i]; //从机地址
- }
- mFlash.mClient.isConfig ='C';
- mBackup.hardVersion = mFlash.mClient.hardVersion;
- mBackup.sotfVersion = mFlash.mClient.sotfVersion;
- mBackup.isConfig = mFlash.mClient.isConfig;
- if(Flash_SaveBackup() != ZONE_OP_SUCCESS){Except_TxError(EXCEPT_Pair, "pair save backup fail");}
- if(Flash_DeleteAllStep() != ZONE_OP_SUCCESS){Except_TxError(EXCEPT_Pair, "pair clear step fail");}
- memset(&mFlash.mStep, 0, sizeof(FlashStep_t));
- if(Flash_SaveInfomation() != ZONE_OP_SUCCESS){Except_TxError(EXCEPT_Pair, "Flash_SaveInfomation fail");}
- }
- app_wireless_pair_start_led();//如果连接上没靠近,要在这里加才会开线程。
- app_wireless_pair_EventHandle(APP_WIRELESS_PAIR_EVENT_PAIR_DONE);
- if(ob_app_wireless_pair_fsm.curState == APP_WIRELESS_PAIR_STATE_END_OF_PAIRING && slave_get_rssi() >= APP_WIRELESS_PAIR_RSSI_THRESHOLD)
- {
- if(host_isconnect() == 0)nrf_ble_scan_stop();
- }
- }
- else
- {
- //若不是,断开连接,设置广播为新的配对地址
- sprintf(buf,"%02X%02X%02X%02X%02X%02X",peer_addr[0],peer_addr[1],peer_addr[2],m_addr.addr[2],m_addr.addr[1],m_addr.addr[0]);
- if(memcmp(ob_app_wireless_pair_fsm.already_scan_or_adv_mac_buf,buf,strlen(buf)) != 0 && memcmp(peer_addr,null_addr,6) != 0)
- {
- if(host_isconnect() == 0)
- {
- //已连接状态下,不能调用停止扫描
- nrf_ble_scan_stop();
- ret += ob_app_wireless_pair_fsm.set_scan_cb(0x01);
- }
- slave_disconnect();
- //已连接状态下,不能调用停止广播
- if(slave_isconnect() == 0)adv_stop = wireless_pair_advertising_stop();
- if(adv_stop == 0 && slave_isconnect() == 0)
- {
- DEBUG_LOG("pair : advName(%d):%s\n",strlen(buf),buf);
- slave_set_adv_name(buf,strlen(buf));
- slave_adv_init();
- ret += ob_app_wireless_pair_fsm.set_adv_cb(0x02);
- if(ret == 0){memcpy(ob_app_wireless_pair_fsm.already_scan_or_adv_mac_buf,buf,strlen(buf));}
- }
- }
- }
- }
- ob_app_wireless_pair_fsm.is_busy = false;
- }
- }
- /*API ------------------------------------------------------------------------------------------------------------------------------------*/
- /**
- @brief 初始化无线配对应用
- @param 无
- @return 错误代码 - [out] -1失败,0成功
- */
- int app_wireless_pair_Init(void)
- {
- //初始化结构体
- // memset(&ob_app_wireless_pair_fsm,0,sizeof(App_Wireless_Pair_Fsm_s)); //不能meset初始化,因为回调函数会被清除。
-
- ob_app_wireless_pair_fsm.curState = APP_WIRELESS_PAIR_STATE_PAIRING_STANDBY;
-
- ob_app_wireless_pair_fsm.is_busy = false;
-
- ob_app_wireless_pair_fsm.led_switch = 1;
-
- memset(&ob_app_wireless_pair_fsm.peer_addr,0xFF,sizeof(ob_app_wireless_pair_fsm.peer_addr));
- for(int i=0;i<APP_WIRELESS_PAIR_RSSI_BUFF_LEN;i++)ob_app_wireless_pair_fsm.rssi_buf[i] = -500;
-
- //{到来的事件,当前的状态,将要要执行的函数,下一个状态}
- ob_app_wireless_pair_fsm.FsmTable[0].event = APP_WIRELESS_PAIR_EVENT_CHARGING_INSERTING; //充电插入
- ob_app_wireless_pair_fsm.FsmTable[0].CurState = APP_WIRELESS_PAIR_STATE_PAIRING_STANDBY; //配对待机状态
- ob_app_wireless_pair_fsm.FsmTable[0].eventActFun = app_wireless_pair_set_scan_adv_on; //设置扫描和广播开
- ob_app_wireless_pair_fsm.FsmTable[0].NextState = APP_WIRELESS_PAIR_STATE_PAIRING_PREPARATION; //配对准备中状态
-
- ob_app_wireless_pair_fsm.FsmTable[1].event = APP_WIRELESS_PAIR_EVENT_CHARGING_UNPLUG; //充电拔出
- ob_app_wireless_pair_fsm.FsmTable[1].CurState = APP_WIRELESS_PAIR_STATE_PAIRING_PREPARATION; //配对准备中状态
- ob_app_wireless_pair_fsm.FsmTable[1].eventActFun = app_wireless_pair_set_scan_adv_off; //设置扫描和广播关
- ob_app_wireless_pair_fsm.FsmTable[1].NextState = APP_WIRELESS_PAIR_STATE_PAIRING_STANDBY; //配对待机状态
-
- ob_app_wireless_pair_fsm.FsmTable[2].event = APP_WIRELESS_PAIR_EVENT_RSSI_MEETS_REQUIREMENTS; //rssi符合要求
- ob_app_wireless_pair_fsm.FsmTable[2].CurState = APP_WIRELESS_PAIR_STATE_PAIRING_PREPARATION; //配对准备中状态
- ob_app_wireless_pair_fsm.FsmTable[2].eventActFun = app_wireless_pair_unconditional_entry; //不操作,直接进入下一个状态
- ob_app_wireless_pair_fsm.FsmTable[2].NextState = APP_WIRELESS_PAIR_STATE_PAIRING_IN_PROGRESS; //配对进行中状态
-
- ob_app_wireless_pair_fsm.FsmTable[3].event = APP_WIRELESS_PAIR_EVENT_CHARGING_UNPLUG; //充电拔出
- ob_app_wireless_pair_fsm.FsmTable[3].CurState = APP_WIRELESS_PAIR_STATE_PAIRING_IN_PROGRESS; //配对进行中状态
- ob_app_wireless_pair_fsm.FsmTable[3].eventActFun = app_wireless_pair_set_scan_adv_off; //设置扫描和广播关
- ob_app_wireless_pair_fsm.FsmTable[3].NextState = APP_WIRELESS_PAIR_STATE_PAIRING_STANDBY; //配对待机状态
-
- ob_app_wireless_pair_fsm.FsmTable[4].event = APP_WIRELESS_PAIR_EVENT_RSSI_DOES_NOT_MEETS_REQUIREMENTS; //rssi不符合要求
- ob_app_wireless_pair_fsm.FsmTable[4].CurState = APP_WIRELESS_PAIR_STATE_PAIRING_IN_PROGRESS; //配对进行中状态
- ob_app_wireless_pair_fsm.FsmTable[4].eventActFun = app_wireless_pair_unconditional_entry; //不操作,直接进入下一个状态
- ob_app_wireless_pair_fsm.FsmTable[4].NextState = APP_WIRELESS_PAIR_STATE_PAIRING_PREPARATION; //配对准备中状态
-
- ob_app_wireless_pair_fsm.FsmTable[5].event = APP_WIRELESS_PAIR_EVENT_PAIR_DONE; //配对完成
- ob_app_wireless_pair_fsm.FsmTable[5].CurState = APP_WIRELESS_PAIR_STATE_PAIRING_IN_PROGRESS; //配对进行中状态
- ob_app_wireless_pair_fsm.FsmTable[5].eventActFun = app_wireless_pair_unconditional_entry; //不操作,直接进入下一个状态
- ob_app_wireless_pair_fsm.FsmTable[5].NextState = APP_WIRELESS_PAIR_STATE_END_OF_PAIRING; //配对结束状态
-
- ob_app_wireless_pair_fsm.FsmTable[6].event = APP_WIRELESS_PAIR_EVENT_CHARGING_UNPLUG; //充电拔出
- ob_app_wireless_pair_fsm.FsmTable[6].CurState = APP_WIRELESS_PAIR_STATE_END_OF_PAIRING; //配对结束状态
- ob_app_wireless_pair_fsm.FsmTable[6].eventActFun = app_wireless_pair_set_scan_adv_off; //设置扫描和广播关
- ob_app_wireless_pair_fsm.FsmTable[6].NextState = APP_WIRELESS_PAIR_STATE_PAIRING_STANDBY; //配对待机状态
-
- ob_app_wireless_pair_fsm.FsmTable[7].event = APP_WIRELESS_PAIR_EVENT_RSSI_DOES_NOT_MEETS_REQUIREMENTS; //rssi不符合要求
- ob_app_wireless_pair_fsm.FsmTable[7].CurState = APP_WIRELESS_PAIR_STATE_END_OF_PAIRING; //配对结束状态
- ob_app_wireless_pair_fsm.FsmTable[7].eventActFun = app_wireless_pair_unconditional_entry; //不操作,直接进入下一个状态
- ob_app_wireless_pair_fsm.FsmTable[7].NextState = APP_WIRELESS_PAIR_STATE_PAIRING_PREPARATION; //配对准备中状态
-
- Process_Start(0,"app_wireless_pair_in_process",app_wireless_pair_in_process);
-
- return 0;
- }
- /**
- @brief 无线配对注册扫描设置
- @param cb - [in] 注册的回调
- @return 错误代码 - [out] -1失败,0成功
- */
- int app_wireless_pair_Regist_SetScan(App_Wireless_Pair_Set_Scan_Cb cb)
- {
- if(cb != NULL)
- {
- ob_app_wireless_pair_fsm.set_scan_cb = cb;
- return 0;
- }
- return -1;
- }
- /**
- @brief 无线配对注册广播设置
- @param cb - [in] 注册的回调
- @return 错误代码 - [out] -1失败,0成功
- */
- int app_wireless_pair_Regist_SetAdv(App_Wireless_Pair_Set_Adv_Cb cb)
- {
- if(cb != NULL)
- {
- ob_app_wireless_pair_fsm.set_adv_cb = cb;
- return 0;
- }
- return -1;
- }
- /**
- @brief 无线配对扫描请求
- @param rssi - [in] 天线信号强度
- @param addr - [in] MAC地址缓冲区指针
- @return 错误代码 - [out] -1失败,0成功
- */
- int app_wireless_pair_Scan_Req(int rssi, uint8_t *addr)
- {
- int ret = 0;
-
- if(mFlash.isHost)
- {
- app_wireless_pair_enter_in_process(rssi,addr);
- }
- else
- {
- ret = -1; //只有左鞋收到右鞋的扫描请求才对
- }
-
- return ret;
- }
- /**
- @brief 无线配对扫描回复
- @param rssi - [in] 天线信号强度
- @param addr - [in] MAC地址缓冲区指针
- @return 错误代码 - [out] -1失败,0成功
- */
- int app_wireless_pair_Scan_Rsp(int rssi, uint8_t *addr)
- {
- int ret = 0;
-
- if(!mFlash.isHost)
- {
- app_wireless_pair_enter_in_process(rssi,addr);
- }
- else
- {
- ret = -1; //只有右鞋收到左鞋的扫描回应才对
- }
-
- return ret;
- }
- /**
- @brief 无线配对亮灯
- @param 无
- @return 错误代码 - [out] -1失败,0成功
- */
- int app_wireless_pair_start_led(void)
- {
- if(ob_app_wireless_pair_fsm.led_switch != 0 && (ob_app_wireless_pair_fsm.curState == APP_WIRELESS_PAIR_STATE_PAIRING_IN_PROGRESS))
- {
- ob_app_wireless_pair_fsm.led_switch--;
- app_wireless_pair_start_display_led();
- // SEGGER_RTT_printf(0,"app_wireless_pair_start_display_led\r\n");
- }
- return 0;
- }
- /**
- @brief 无线配对灭灯
- @param 无
- @return 错误代码 - [out] -1失败,0成功
- */
- int app_wireless_pair_stop_led(void)
- {
- app_wireless_pair_stop_display_led();
- app_wireless_pair_EventHandle(APP_WIRELESS_PAIR_EVENT_RSSI_DOES_NOT_MEETS_REQUIREMENTS);
- // SEGGER_RTT_printf(0,"app_wireless_pair_stop_led\r\n");
- return 0;
- }
- /**
- @brief 无线配对事件处理
- @param event - [in] 事件
- @return 错误代码 - [out] -1失败,0成功
- */
- int app_wireless_pair_EventHandle(App_Wireless_Pair_Event_e event)
- {
- int (*eventActFun)() = NULL; //函数指针初始化为空
- App_Wireless_Pair_State_e NextState;
- int flag = 0; //标识是否满足条件
- int i;
- int ret = 0;
-
-
- /*获取当前动作函数*/
- for (i = 0; i<APP_WIRELESS_PAIR_FSMTABLE_LEN; i++)
- {
- //当且仅当当前状态下来个指定的事件,我才执行它
- if (event == ob_app_wireless_pair_fsm.FsmTable[i].event && ob_app_wireless_pair_fsm.curState == ob_app_wireless_pair_fsm.FsmTable[i].CurState)
- {
- flag = 1;
- eventActFun = ob_app_wireless_pair_fsm.FsmTable[i].eventActFun;
- NextState = ob_app_wireless_pair_fsm.FsmTable[i].NextState;
- break;
- }
- }
-
- if (flag) //如果满足条件了
- {
- /*动作执行*/
- if(eventActFun)
- {
- ret = eventActFun();
- }
- //跳转到下一个状态
- if(ret == 0)
- {
- ob_app_wireless_pair_fsm.curState = NextState;
- // SEGGER_RTT_printf(0,"success state:%d, event:%d\r\n",ob_app_wireless_pair_fsm.curState,event);
- }
- else
- {
- // SEGGER_RTT_printf(0,"*****************************************fail state:%d, event:%d\r\n",ob_app_wireless_pair_fsm.curState,event);
- }
- }
- else
- {
- //there is no match
- ret = -1;
- }
-
- return ret;
- }
- App_Wireless_Pair_State_e app_wireless_pair_get_curstate(void)
- {
- return ob_app_wireless_pair_fsm.curState;
- }
|