| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448 |
- #include "CONFIG.h"
- #if BC260Y
- #include "stm32f10x.h"
- #include "Regist.h"
- #include "FlashSetup.h"
- #include <stdio.h>
- #include <stdarg.h>
- #include <string.h>
- #include <stdlib.h>
- #include "cJSON.h"
- #include "mbedtls_util.h"
- #include "Log_Module.h"
- #include "At_Module.h"
- #include "Common_Util.h"
- #include "Tuoreniot.h"
- #include "bc260y.h"
- // socket ID
- static uint8_t connectID = 2;
- // 注册参数
- static struct TUORENIOT_RegistRequestStruct regist_request = {
- .version = 1,
- .networkType = 1,
- .networkProtocol = 1,
- .productId = "1dbfd476b7nm2",
- .deviceId = "3431228A1936013C",
- .userId = "1226",
- .sim = "",
- };
- // dns
- static char pridnsaddr[20];
- // 函数声明
- static void REGIST_Process(void); // 注册流程
- static void REGIST_SetStatus(enum REGIST_StatusEnum status); // 设置注册状态
- static void REGIST_SetResult(enum REGIST_ResultEnum result); // 设置注册结果
- static void goto_start(void); // 开始
- static void goto_success(void); // 成功
- static void goto_failure(char * _info); // 失败
- static void goto_finish(void); // 结束
- // 流程
- enum Step{
- STEP_FINISH, // 结束
- STEP_START, // 开始
- STEP_EXIT_SLEEP, // 退出睡眠
- STEP_WAIT, // 等待
-
- STEP_STATE, // 查询状态
- STEP_OPEN, // 打开
- STEP_SET_SLEEP, // 设置休眠
-
- STEP_SET_QISDE_0, // 关闭发送回显
- STEP_SET_QISDE_1, // 打开发送回显
- STEP_SEND, // 发送
- STEP_RECV, // 等待发送结果
-
- STEP_QUERY_ID, //查询SIM卡号
- STEP_QUERY_CFUN, // 查询功能模式
- STEP_SET_CFUN_0, // 设置最小功能模式
- STEP_SET_CFUN_1, // 设置全功能模式
-
- STEP_SET_CEREG_2, // 设置ps域允许上报网络注册和位置信息
- STEP_QUERY_CEREG, // 查询网络注册状态
- STEP_DATAFORMAT, // 配置发送格式
-
- STEP_CLOSE, // 关闭
-
- STEP_SUCCESS, // 成功
- STEP_FAILURE, // 失败
- STEP_QUERY_DNS, // 查询DNS
- STEP_SET_DNS, // 设置DNS
- };
- // 步骤跳转时的监听
- static uint8_t STEP_SET_CFUN_1_times = 0; // 全功能模式的次数
- static uint16_t goto_step_event(uint16_t ns)
- {
- // 重置bc260y状态
- bc260y.reset();
-
- // 流程控制
- uint16_t step = ns;
- if(ns == STEP_SET_CFUN_1 && STEP_SET_CFUN_1_times++ >= 1)
- {
- goto_failure("重新联网次数过多");
- step = STEP_FAILURE;
- }
- return step;
- }
- // 流程控制
- static struct PCTRL_Struct pctrl = {
- .current_step = STEP_FINISH,
- .next_step = STEP_FINISH,
- .step_wait = STEP_WAIT,
- .goto_step_listener = goto_step_event,
- };
- // 备注
- static char info[40];
- // 开始
- static void goto_start(void)
- {
- PCTRL_GotoStep(&pctrl, STEP_START, "开始");
- }
- // 成功
- static void goto_success(void)
- {
- REGIST_SetResult(REGIST_Result_Success);
- PCTRL_GotoStep(&pctrl, STEP_SUCCESS, "成功");
- }
- // 失败
- static void goto_failure(char * _info)
- {
- REGIST_SetResult(REGIST_Result_Failure);
- memset(info, 0, sizeof(info));
- strcpy(info, _info);
- PCTRL_GotoStep(&pctrl, STEP_FAILURE, "失败");
- Log_Printf_Debug("info:%s!\r\n", info);
- }
- // 结束
- static void goto_finish(void)
- {
- REGIST_SetStatus(REGIST_Status_Done);
- PCTRL_GotoStep(&pctrl, STEP_FINISH, "结束");
- }
- // 注册状态
- static enum REGIST_StatusEnum regist_status = REGIST_Status_None;
- // 注册结果
- static enum REGIST_ResultEnum regist_result = REGIST_Result_None;
- // 设置注册状态
- static void REGIST_SetStatus(enum REGIST_StatusEnum status)
- {
- regist_status = status;
- }
- // 获取注册状态
- enum REGIST_StatusEnum REGIST_GetStatus(void)
- {
- return regist_status;
- }
- // 设置注册结果
- static void REGIST_SetResult(enum REGIST_ResultEnum result)
- {
- regist_result = result;
- }
- // 获取注册结果
- enum REGIST_ResultEnum REGIST_GetResult(void)
- {
- return regist_result;
- }
- // 重置注册
- enum EXECUTE_ResultEnum REGIST_Reset(void)
- {
- if(REGIST_GetStatus() == REGIST_Status_Being)
- {
- Log_Printf_Debug("注册流程重置错误!\r\n");
- return EXECUTE_Result_Failure;
- }
- REGIST_SetStatus(REGIST_Status_None);
- REGIST_SetResult(REGIST_Result_None);
- return EXECUTE_Result_Success;
- }
- // 开始注册
- enum EXECUTE_ResultEnum REGIST_Start(void)
- {
- REGIST_Reset();
- if(REGIST_GetStatus() != REGIST_Status_None)
- {
- Log_Printf_Debug("注册流程启动错误!\r\n");
- return EXECUTE_Result_Failure;
- }
- goto_start();
- REGIST_SetStatus(REGIST_Status_Being);
- return EXECUTE_Result_Success;
- }
- // 注册流程处理,放到大循环里
- void REGIST_ProcessHandle(void)
- {
- if(REGIST_GetStatus() == REGIST_Status_Being)
- {
- REGIST_Process();
- }
- }
- // 注册流程
- static enum Result result = Result_None;
- static uint8_t cfun_mode, mode, stat = 0;
- static uint16_t lac, err = 0;
- static uint32_t ci = 0;
- static void REGIST_Process()
- {
- switch(pctrl.current_step)
- {
- case STEP_START: // 开始
- PCTRL_GotoStep(&pctrl, STEP_EXIT_SLEEP, "退出休眠");
- break;
- case STEP_EXIT_SLEEP:
- result = bc260y.exit_sleep_2();//退出休眠
- if(result == Result_Success)
- {
- PCTRL_GotoStep(&pctrl, STEP_SET_QISDE_1, "开启回显");
- }
- else if(result == Result_Failed)
- {
- goto_failure("退出休眠失败");
- }
- break;
- case STEP_SET_QISDE_1:
- result = bc260y.set_qisde(1);//开启回显
- if(result == Result_Success)
- {
- PCTRL_GotoStep(&pctrl, STEP_SET_SLEEP, "设置休眠模式");
- }
- else if(result == Result_Failed)
- {
- goto_failure("开启回显失败");
- }
- break;
- case STEP_SET_SLEEP:
- result=bc260y.set_sleep(2);//设置休眠模式
- if(result == Result_Success)
- {
- PCTRL_GotoStep(&pctrl, STEP_DATAFORMAT, "设置数据格式");
- }
- else if(result == Result_Failed)
- {
- goto_failure("设置休眠模式失败");
- }
- break;
- case STEP_DATAFORMAT:
- result = bc260y.dataformat(1); // 设置数据格式, 十六进制
- if(result == Result_Success)
- {
- PCTRL_GotoStep(&pctrl, STEP_QUERY_CFUN, "查询功能模式");
- }
- else if(result == Result_Failed)
- {
- goto_failure("设置数据格式失败");
- }
- break;
- case STEP_QUERY_CFUN:
- result = bc260y.query_cfun(&cfun_mode); // 查询功能模式
- if(result == Result_Success)
- {
- if(cfun_mode == 1)
- {
- PCTRL_GotoStep(&pctrl, STEP_SET_CFUN_0, "设置最小功能模式");
- }
- else
- {
- PCTRL_GotoStep(&pctrl, STEP_SET_CFUN_1, "设置全功能模式");
- }
- }
- else if(result == Result_Failed)
- {
- goto_failure("查询功能模式失败");
- }
- break;
- case STEP_SET_CFUN_0:
- result=bc260y.set_cfun(0);//设置最小功能模式
- if(result == Result_Success)
- {
- if(REGIST_GetResult() != REGIST_Result_None)
- {
- goto_finish();
- }
- else
- {
- PCTRL_GotoStep(&pctrl, STEP_SET_CFUN_1, "设置全功能模式");
- }
- }
- else if(result == Result_Failed)
- {
- goto_failure("设置最小功能模式失败");
- goto_finish();
- }
- break;
- case STEP_SET_CFUN_1:
- result=bc260y.set_cfun(1);//设置全功能模式
- if(result == Result_Success)
- {
- PCTRL_GotoStep(&pctrl, STEP_QUERY_DNS, "查询DNS");
- memset(pridnsaddr, 0, sizeof(pridnsaddr));
- }
- else if(result == Result_Failed)
- {
- goto_failure("设置全功能模式失败");
- }
- break;
- case STEP_QUERY_DNS:
- result = bc260y.query_dns(pridnsaddr); // 查询DNS
- if(result == Result_Success)
- {
- Log_Printf_Debug("DNS: %s, %d\r\n", pridnsaddr, strlen(pridnsaddr));
- if(strlen(pridnsaddr) <= 1)
- {
- PCTRL_GotoStep(&pctrl, STEP_SET_DNS, "设置DNS");
- }
- else
- {
- PCTRL_GotoStep(&pctrl, STEP_SET_CEREG_2, "设置网络状态");
- }
- }
- else if(result == Result_Failed) goto_failure("查询DNS失败");
- break;
- case STEP_SET_DNS:
- result = bc260y.set_dns("114.114.114.114", "8.8.8.8"); // 设置DNS
- if(result == Result_Success)
- {
- PCTRL_GotoStep(&pctrl, STEP_SET_CFUN_0, "设置最小功能模式");
- }
- else if(result == Result_Failed)
- {
- goto_failure("设置DNS失败");
- }
- break;
- case STEP_SET_CEREG_2:
- result=bc260y.set_cereg(2); // 设置网络状态
- if(result == Result_Success)
- {
- PCTRL_GotoStep(&pctrl, STEP_QUERY_CEREG, "查询网络状态");
- }
- else if(result == Result_Failed)
- {
- goto_failure("设置网络状态失败");
- }
- break;
- case STEP_QUERY_CEREG:
- result=bc260y.query_cereg(&mode, &stat, &lac, &ci);// 查询网络状态
- if(result == Result_Success)
- {
- PCTRL_GotoStep(&pctrl, STEP_QUERY_ID, "查询SIM卡号");
- }
- else if(result == Result_Failed)
- {
- goto_failure("查询网络状态失败");
- }
- break;
- case STEP_QUERY_ID: //查询SIM卡号
- result = bc260y.query_qccid(regist_request.sim);
- if(result == Result_Success)
- {
- PCTRL_GotoStep(&pctrl, STEP_CLOSE, "关闭连接");
- }
- else if(result == Result_Failed)
- {
- goto_failure("查询SIM卡号失败");
- }
- break;
- case STEP_CLOSE:
- result=bc260y.close_socket(connectID);//防止以前链接没断开,断开tcp链接
- if(result == Result_Success)
- {
- if(REGIST_GetResult() != REGIST_Result_None)
- {
- PCTRL_GotoStep(&pctrl, STEP_SET_CFUN_0, "设置最小功能模式");
- }
- else
- {
- PCTRL_GotoStep(&pctrl, STEP_OPEN, "连接tcp");
- }
- }
- else if(result == Result_Failed)
- {
- goto_failure("断开tcp连接失败");
- }
- break;
- case STEP_OPEN:
- result=bc260y.open_socket(connectID, "TCP", REGIST_SERVER, REGIST_PORT, 1, &err); // 连接tcp
- if(result == Result_Success)
- {
- PCTRL_GotoStep(&pctrl, STEP_SEND, "发送数据");
- // 打包注册请求
- memset(regist_data, 0, sizeof(regist_data));
- TUORENIOT_PackRegistRequest(regist_data, ®ist_data_length, ®ist_request);
- TUORENIOT_PrintfRegistRequestStruct(®ist_request);
- }
- else if(result == Result_Failed)
- {
- goto_failure("连接tcp失败");
- }
- break;
- case STEP_SEND: // 发送数据
- result =bc260y.send_rai(connectID, regist_data, regist_data_length, 0);//数据发送
- if(result == Result_Success)
- {
- PCTRL_GotoStep(&pctrl, STEP_RECV, "等待结果");
- memset(regist_data, 0, sizeof(regist_data));
- }
- else if(result == Result_Failed)
- {
- goto_failure("发送数据失败");
- }
- break;
- case STEP_RECV: // 等待结果
- result =bc260y.recv_with_time(connectID, regist_data, ®ist_data_length, 10000);
- if(result == Result_Success)
- {
- uint8_t res = TUORENIOT_AnalysisRegistData(regist_data, regist_data_length);
- if(res == 1) // 成功
- {
- goto_success();
- }
- else // 失败
- {
- goto_failure("解析失败");
- }
- PCTRL_GotoStep(&pctrl, STEP_CLOSE, "断开tcp连接");
- }
- else if(result == Result_Failed)
- {
- goto_failure("等待结果失败");
- }
- break;
- case STEP_WAIT: // 等待
- PCTRL_Wait(&pctrl);
- break;
- case STEP_SUCCESS: // 成功
- goto_finish();
- break;
- case STEP_FAILURE: // 失败
- PCTRL_GotoStep(&pctrl, STEP_SET_CFUN_0, "失败,设置最小功能模式");
- break;
- case STEP_FINISH: // 结束
- break;
- default: // 默认
- goto_failure("步骤不存在");
- break;
- }
- }
- #endif
|