12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415 |
- /*******************************
- *
- * 骑享程序测试
- *
- * ****************************/
- #include "bsp.h"
- #include "bsp_custom.h"
- #include "osasys.h"
- #include "ostask.h"
- #include "queue.h"
- #include "ps_event_callback.h"
- #include "app.h"
- #include "cmisim.h"
- #include "cmimm.h"
- #include "cmips.h"
- #include "sockets.h"
- #include "psifevent.h"
- #include "ps_lib_api.h"
- #include "lwip/netdb.h"
- #include <cis_def.h>
- #include "debug_log.h"
- #include "slpman_ec616.h"
- #include "plat_config.h"
- //空间及变量定义
- #define PROC_MAIN_TASK_STACK_SIZE (1024)
- #define PROC_UART_TASK_STACK_SIZE (1024)
- #define PROC_CAN_TASK_STACK_SIZE (1024)
- #define PROC_TCP_TASK_STACK_SIZE (1024)
- #define APP_EVENT_QUEUE_SIZE (10)
- uint8_t deepslpTimerID = DEEPSLP_TIMER_ID7;
- uint8_t MainSlpHandler = 0xff;
- uint8_t UartSlpHandler = 0xfe;
- uint8_t CanSlpHandler = 0xfd;
- uint8_t TcpSlpHandler = 0xfd;
- #define Uart_Send_LEN (8)
- #define Uart_Rece_LEN (40)
- #define battbuffer_len (110)
- #define RTE_UART_RX_IO_MODE RTE_UART1_RX_IO_MODE
- extern ARM_DRIVER_USART Driver_USART1;
- static ARM_DRIVER_USART *USARTdrv = &Driver_USART1;
- volatile bool isRecvTimeout = false;
- volatile bool isRecvComplete = false;
- QueueHandle_t gpsMsgQueue = NULL;
- GPS_INFO Gps_Data;
- static volatile UINT32 Event;
- static QueueHandle_t psEventQueueHandle;
- static UINT8 gImsi[16] = {0};
- static INT32 sockfd = -1;
- static UINT32 gCellID = 0;
- int TcpconnectID = -1;
- volatile bool tcp_ready = false;
- uint8_t Batt_Cell_Num = 17;//默认数值14、17
- uint8_t Batt_Cell_Num_2 ;//默认数值
- uint8_t Batt_Temp_Num = 7;//默认数值5、7
- int16_t Uart_Rece_BattI=0x0000;
- uint8_t Can_Rece_buffer[8];
- volatile bool Can_Enable = false;
- uint16_t data_index = 0x0000;
- volatile bool Sleep_Flag = false;
- volatile bool Uart_Sleep_Flag = false;
- volatile bool Can_Sleep_Flag = false;
- volatile bool Tcp_Sleep_Flag = false;
- uint8_t battbuffer[battbuffer_len];//电池数据都存在此数组中————电压14,温度5
- /**
- * 存放规则如下:
- * 位置: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
- * 数据: 年 月 日 时 分 秒 信息体标志 年 月 日 时 分 秒 网络信号 故障等级 故障代码高 故障代码低
- *
- * 17 18 19 20 21 22 23 24 25 26 27 28 29 30 30+1 .... 30+X*2 31+X*2 31+1...31+X*2+N
- * 电流H 电流L Link电压H Link电压L Pack电压H Pack电压L 开关状态 SOC SOH 均衡状态 单体个数X 单体v1...单体vX 温度个数N 温度1..温度N
- * 32+X*2+N 33+X*2+N 34+x*2+N 35 +X*2+N 36+X*2 +N 37+X*2+N
- * 电池状态 是否加热 最高单体H 最高单体L 最低单体H 最低单体L
- * */
- //GPS信息
- uint8_t gpsbuffer[30];//定位都存在此数组中
- /**
- * 存放规则如下:
- * 位置: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
- * 数据: 年 月 日 时 分 秒 信息体标志 年 月 日 时 分 秒 定位标志 卫星数目 航向H 航向L
- *
- * 17 18 19 20 21 22 23 24 25 26 27 28
- * 速度H 速度L 海拔H 海拔L 纬度HH 纬度HL 纬度LH 纬度LL 经度HH 经度HL 经度LH 经度LL
- *
- * */
- //GPS信息
- //状态机定义
- typedef enum
- {
- PROCESS_STATE_IDLE = 0,
- PROCESS_STATE_WORK,
- PROCESS_STATE_SLEEP
- }process_Main;
- static process_Main gProcess_Main_Task = PROCESS_STATE_IDLE;
- #define PROC_MAIN_STATE_SWITCH(a) (gProcess_Main_Task = a)
- typedef enum
- {
- PROCESS_UART_STATE_IDLE = 0,
- PROCESS_UART_STATE_CHECK,
- PROCESS_UART_STATE_WORK,
- PROCESS_UART_STATE_SLEEP
- }process_Uart;
- static process_Uart gProcess_Uart_Task = PROCESS_UART_STATE_IDLE;
- #define PROC_UART_STATE_SWITCH(a) (gProcess_Uart_Task = a)
- typedef enum
- {
- PROCESS_CAN_STATE_IDLE = 0,
- PROCESS_CAN_STATE_RECV,
- PROCESS_CAN_STATE_SEND,
- PROCESS_CAN_STATE_SLEEP
- }process_CAN;
- static process_CAN gProcess_Can_Task = PROCESS_CAN_STATE_IDLE;
- #define PROC_CAN_STATE_SWITCH(a) (gProcess_Can_Task = a)
- typedef enum
- {
- PROCESS_TCP_STATE_IDLE = 0,
- PROCESS_TCP_STATE_LINK,
- PROCESS_TCP_STATE_WORK,
- PROCESS_TCP_STATE_SLEEP
- }process_TCP;
- static process_TCP gProcess_Tcp_Task = PROCESS_TCP_STATE_IDLE;
- #define PROC_TCP_STATE_SWITCH(a) (gProcess_Tcp_Task = a)
- //堆栈申请
- static StaticTask_t gProcess_Main_Task_t;
- static UINT8 gProcess_Main_TaskStack[PROC_UART_TASK_STACK_SIZE];
- static StaticTask_t gProcess_Uart_Task_t;
- static UINT8 gProcess_Uart_TaskStack[PROC_UART_TASK_STACK_SIZE];
- static StaticTask_t gProcess_Can_Task_t;
- static UINT8 gProcess_Can_TaskStack[PROC_CAN_TASK_STACK_SIZE];
- static StaticTask_t gProcess_Tcp_Task_t;
- static UINT8 gProcess_Tcp_TaskStack[PROC_TCP_TASK_STACK_SIZE];
- //睡眠进出函数
- static void appBeforeHib(void *pdata, slpManLpState state)
- {
- uint32_t *p_param = (uint32_t *)pdata;
- #ifdef USING_PRINTF
- printf("Before Hibernate:%d \n",state);
- #endif
- slpManAONIOLatchEn(AonIOLatch_Enable);
- }
- static void appAfterHib(void *pdata, slpManLpState state)
- {
- #ifdef USING_PRINTF
- printf("Try Hibernate Failed:%d \n",state);
- #endif
- }
- static void appBeforeSlp1(void *pdata, slpManLpState state)
- {
- #ifdef USING_PRINTF
- printf("Before Sleep1:%d \n",state);
- #endif
- slpManAONIOLatchEn(AonIOLatch_Enable);
- }
- static void appAfterSlp1(void *pdata, slpManLpState state)
- {
- #ifdef USING_PRINTF
- printf("After Sleep1:%d \n",state);
- #endif
- }
- static void appBeforeSlp2(void *pdata, slpManLpState state)
- {
- #ifdef USING_PRINTF
- printf("before sleep2:%d \n",state);
- #endif
- slpManAONIOLatchEn(AonIOLatch_Enable);
- }
- static void appAfterSlp2(void *pdata, slpManLpState state)
- {
- #ifdef USING_PRINTF
- printf("sleep2 failed:%d \n",state);
- #endif
- }
- // 主任务线程
- static void Main_Task(void* arg)
- {
- UINT16 Can_index = 0;
- UINT16 Uart_index = 0;
- UINT16 Tcp_index = 0;
- uint32_t sleep_index = 0;
- uint32_t sleep_time_flag;
- int32_t inParam = 0xAABBCCDD;
- uint32_t Chrgend_Work_time = 180000;
- uint32_t Wakeup_Work_time = 30000;
- uint32_t Sleep_Time = 1800000;
- //上述参数应写入文件里
- slpManWakeSrc_e Wakeup_source;
- PROC_MAIN_STATE_SWITCH(PROCESS_STATE_IDLE);
- NetSocDisplay(LED_SOC_0,LED_TURN_OFF);
- NetSocDisplay(LED_SOC_1,LED_TURN_OFF);
- NetSocDisplay(LED_SOC_2,LED_TURN_OFF);
- NetSocDisplay(LED_SOC_3,LED_TURN_OFF);
- FaultDisplay(LED_TURN_OFF);
- NVIC_EnableIRQ(PadWakeup1_IRQn);
- slpManSetPmuSleepMode(true,SLP_HIB_STATE,false);
- slpManApplyPlatVoteHandle("MainSlp",&MainSlpHandler);
- slpManPlatVoteDisableSleep(MainSlpHandler, SLP_SLP2_STATE);
- slpManRegisterUsrdefinedBackupCb(appBeforeHib,&inParam,SLPMAN_HIBERNATE_STATE);
- slpManRegisterUsrdefinedRestoreCb(appAfterHib,NULL,SLPMAN_HIBERNATE_STATE);
- slpManRegisterUsrdefinedBackupCb(appBeforeSlp1,NULL,SLPMAN_SLEEP1_STATE);
- slpManRegisterUsrdefinedRestoreCb(appAfterSlp1,NULL,SLPMAN_SLEEP1_STATE);
- slpManRegisterUsrdefinedBackupCb(appBeforeSlp2,NULL,SLPMAN_SLEEP2_STATE);
- slpManRegisterUsrdefinedRestoreCb(appAfterSlp2,NULL,SLPMAN_SLEEP2_STATE);
- slpManSlpState_t slpstate = slpManGetLastSlpState();
- #ifdef USING_PRINTF
- printf("slpstate:%d \n",slpstate);
- #endif
- Wakeup_source = slpManGetWakeupSrc();//获取唤醒源
- #ifdef USING_PRINTF
- printf("Wakeup_source:%d \n",Wakeup_source);
- #endif
- if (Wakeup_source==0)
- {
- sleep_time_flag = Wakeup_Work_time;
- }
- else if (Wakeup_source==1)
- {
- sleep_time_flag = Wakeup_Work_time;
- }
- else
- {
- sleep_time_flag = Chrgend_Work_time;
- }
-
- if((slpstate == SLP_SLP2_STATE) || (slpstate == SLP_HIB_STATE))
- {
- PROC_MAIN_STATE_SWITCH(PROCESS_STATE_WORK);
- }
- else
- {
- PROC_MAIN_STATE_SWITCH(PROCESS_STATE_WORK);
- }
- //线程初始化完成
- while (true)
- {
- switch(gProcess_Main_Task)
- {
- case PROCESS_STATE_IDLE:
- {
- // 暂时不区分唤醒
- PROC_MAIN_STATE_SWITCH(PROCESS_STATE_WORK);
- break;
- }
- case PROCESS_STATE_WORK:
- {
- NetSocDisplay(LED_SOC_0,LED_TURN_ON);
- osDelay(10/portTICK_PERIOD_MS);
- Can_index++;
- Uart_index++;
- Tcp_index++;
- if (Uart_index >100)//Uart 1s 调用一次
- {
- PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_WORK);
- Uart_index = 0;
- }
- if (Can_index >=100)//Can 1s 调用一次
- {
- PROC_CAN_STATE_SWITCH(PROCESS_CAN_STATE_RECV);
- Can_index = 0;
- }
- if (Tcp_index >=100)//Tcp 1s 调用一次
- {
- PROC_TCP_STATE_SWITCH(PROCESS_TCP_STATE_LINK);
- Tcp_index = 0;
- }
- if((!Can_Enable)&&((Uart_Rece_BattI==0x0000)||(Uart_Rece_BattI==0xffff)))
- {
- sleep_index++;
- }
- else
- {
- sleep_index = 0;
- }
- if (sleep_index >=sleep_time_flag)
- {
- PROC_MAIN_STATE_SWITCH(PROCESS_STATE_SLEEP);
- sleep_index = 0;
- }
- break;
- }
- case PROCESS_STATE_SLEEP:
- {
- while(true)
- {
- PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_SLEEP);
- PROC_CAN_STATE_SWITCH(PROCESS_CAN_STATE_SLEEP);
- PROC_TCP_STATE_SWITCH(PROCESS_TCP_STATE_SLEEP);
- if((Uart_Sleep_Flag)&&(Can_Sleep_Flag)&&(Tcp_Sleep_Flag))
- break;
- }
- Sleep_Flag = TRUE;
- osDelay(2000);
- #ifdef USING_PRINTF
- printf("Ms_sleep:uart_%d,can_%d,tcp_%d\r\n",gProcess_Uart_Task,gProcess_Can_Task,gProcess_Tcp_Task);
- #endif
- slpManSlpState_t State;
- uint8_t cnt;
- if(slpManCheckVoteState(MainSlpHandler, &State, &cnt)==RET_TRUE)
- {
- #ifdef USING_PRINTF
- printf("[%d]We Can Check Vote Main State, state=%d, cnt=%d\r\n",__LINE__,State,cnt);
- #endif
- }
- slpManPlatVoteForceEnableSleep(MainSlpHandler, SLP_SLP2_STATE); //增加强制投票
- if(slpManCheckVoteState(MainSlpHandler, &State, &cnt)==RET_TRUE)
- {
- #ifdef USING_PRINTF
- printf("[%d]We Can Check Vote Main State Again, state=%d, cnt=%d\r\n",__LINE__,State,cnt);
- #endif
- }
- #ifdef USING_PRINTF
- printf("[%d]Waiting sleep\r\n",__LINE__);
- #endif
- FaultDisplay(LED_TURN_ON);
- slpManSlpState_t slpstate = slpManPlatGetSlpState();
- #ifdef USING_PRINTF
- printf("which slpstate can go now :%d \n",slpstate);
- #endif
- slpManDeepSlpTimerStart(deepslpTimerID, Sleep_Time);
- while(1)
- {
- osDelay(60000/portTICK_PERIOD_MS);
- }
- break;
- }
- }
- }
-
- }
- //Uart校验程序
- unsigned int crc_chk(uint8_t* data, uint8_t length)
- {
- int j;
- uint16_t reg_crc=0xFFFF;
- while(length--)
- {
- reg_crc ^= *data++;
- for(j=0;j<8;j++)
- {
- if(reg_crc & 0x01)
- {
- reg_crc=(reg_crc>>1) ^ 0xA001;
- }
- else
- {
- reg_crc=reg_crc >>1;
- }
- }
- }
- return reg_crc;
- }
- //Uart回调程序
- void USART_callback(uint32_t event)
- {
- if(event & ARM_USART_EVENT_RX_TIMEOUT)
- {
- isRecvTimeout = true;
- }
- if(event & ARM_USART_EVENT_RECEIVE_COMPLETE)
- {
- isRecvComplete = true;
- }
- }
- //Uart发送接收函数
- uint8_t* Uart_Receive_func(Uart_Receive_Type Uart_Receive_Msg,uint8_t* Uart_Rece_buffer,uint8_t Data_Len)
- {
- uint16_t CRC_Rece_buffer;
- uint16_t CRC_chk_buffer;
- uint8_t Uart_Send_buffer[8];
- Uart_Send_buffer[0] = Uart_Receive_Msg.Bms_Address;
- Uart_Send_buffer[1] = Uart_Receive_Msg.Bms_Read_Funcode;
- Uart_Send_buffer[2] = Uart_Receive_Msg.Reg_Begin_H;
- Uart_Send_buffer[3] = Uart_Receive_Msg.Reg_Begin_L;
- Uart_Send_buffer[4] = Uart_Receive_Msg.Reg_Num_H;
- Uart_Send_buffer[5] = Uart_Receive_Msg.Reg_Num_L;
- CRC_chk_buffer = crc_chk(Uart_Send_buffer,6);
- Uart_Send_buffer[6] = CRC_chk_buffer;
- Uart_Send_buffer[7] = CRC_chk_buffer>>8;
- uint32_t timeout = 0x00000000;
- USARTdrv->Send(Uart_Send_buffer,8);
- // #ifdef USING_PRINTF
- // printf("Uart_Send_buffer: ");
- // for(int i=0;i<8;i++)
- // {
- // printf("%x ",Uart_Send_buffer[i]);
- // }
- // printf("\n");
- // #endif
- USARTdrv->Receive(Uart_Rece_buffer,Data_Len);
- while((isRecvTimeout == false) && (isRecvComplete == false))
- {
- timeout++;
- osDelay(100);
- if (timeout>=10)
- {
- timeout =0;
- isRecvTimeout = true;
- break;
- }
- }
- // #ifdef USING_PRINTF
- // printf("Uart_Rece_buffer1: ");
- // for(int i=0;i<Data_Len;i++)
- // {
- // printf("%x ",*(Uart_Rece_buffer+i));
- // }
- // #endif
- if (isRecvComplete == true)
- {
- isRecvComplete = false;
- CRC_Rece_buffer =*(Uart_Rece_buffer+Data_Len-1)<<8|*(Uart_Rece_buffer+Data_Len-2);
- CRC_chk_buffer = crc_chk(Uart_Rece_buffer,Data_Len-2);
- // #ifdef USING_PRINTF
- // printf("Uart_Rece_buffer2: ");
- // for(int i=0;i<Data_Len;i++)
- // {
- // printf("%x ",*(Uart_Rece_buffer+i));
- // }
- // printf("\ncrcchk:%x,%x\n ",CRC_chk_buffer,CRC_Rece_buffer);
- // #endif
- if (CRC_Rece_buffer == CRC_chk_buffer)//满足校验
- {
- return Uart_Rece_buffer;//此处指针移位出现重启问题
- }
- else //接收数据的校验不过屏蔽
- {
- memset(Uart_Rece_buffer,0xff,Data_Len);
- return Uart_Rece_buffer;
- }
- }
- else
- {
- memset(Uart_Rece_buffer,0x00,Data_Len);
- isRecvTimeout = false;
- return Uart_Rece_buffer;
- }
- }
- void GGACallBack(UINT8 res, UINT8 * params)
- {
- #ifdef USING_PRINTF
- printf("GPS:len=%d,data=%s\r\n",res,params);
- #else
- ECOMM_STRING(UNILOG_PLA_STRING, GPS_DATA, P_INFO, "gps:%s", params);
- #endif
- }
- //Uart线程
- static void Uart_Task(void* arg)
- {
- USARTdrv->Initialize(USART_callback);
- USARTdrv->PowerControl(ARM_POWER_FULL);
- USARTdrv->Control(ARM_USART_MODE_ASYNCHRONOUS |
- ARM_USART_DATA_BITS_8 |
- ARM_USART_PARITY_NONE |
- ARM_USART_STOP_BITS_1 |
- ARM_USART_FLOW_CONTROL_NONE, 9600);
- int Rece_index = 0;
- volatile uint8_t Data_Len;
- Uart_Receive_Type Uart_Receive_Msg;
- memset(&battbuffer[0],0x00,battbuffer_len);
- PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_IDLE);
- slpManApplyPlatVoteHandle("UARTSLP",&UartSlpHandler);
- slpManPlatVoteDisableSleep(UartSlpHandler, SLP_SLP2_STATE);
- Uart_Receive_Msg.Bms_Address = 0x01;
- Uart_Receive_Msg.Bms_Read_Funcode = 0x03;
- uint8_t *Uart_Rece_buffer = NULL;
- volatile bool Uart_task = false;
- Batt_Cell_Num_2 = Batt_Cell_Num<<1;
- while (true)
- {
- switch (gProcess_Uart_Task)
- {
- case PROCESS_UART_STATE_IDLE:
- {
- Rece_index = 0;
- break;
- }
- case PROCESS_UART_STATE_CHECK:
- {
- Uart_Rece_buffer = (uint8_t *)malloc(Uart_Rece_LEN);
- Uart_Receive_Msg.Reg_Begin_H = 0x00;
- Uart_Receive_Msg.Reg_Begin_L= 0x02+Batt_Cell_Num;
- Uart_Receive_Msg.Reg_Num_H = 0x00;
- Uart_Receive_Msg.Reg_Num_L = 0x01;
- Data_Len = Uart_Receive_Msg.Reg_Num_L*2+5;
- memset(Uart_Rece_buffer,0x00,Data_Len);
- Uart_Rece_buffer = Uart_Receive_func(Uart_Receive_Msg,Uart_Rece_buffer,Data_Len);
- Uart_Rece_BattI = *(Uart_Rece_buffer+3+0)<<8 |*(Uart_Rece_buffer+3+1);
- #ifdef USING_PRINTF
- printf("Check_Current!\n");
- #endif
- PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_IDLE);
- free(Uart_Rece_buffer);
- break;
- }
- case PROCESS_UART_STATE_WORK:
- {
- #ifdef USING_PRINTF
- printf("UART_STATE_WORK!\n");
- #endif
- NetSocDisplay(LED_SOC_1,LED_TURN_ON);
- Uart_task = false;
- Uart_Rece_buffer = (uint8_t *)malloc(Uart_Rece_LEN);
- while(!Uart_task)
- {
- switch (Rece_index)
- {
- case 0://current
- {
- Uart_Receive_Msg.Reg_Begin_H = 0x00;
- Uart_Receive_Msg.Reg_Begin_L= 0x02+Batt_Cell_Num;
- Uart_Receive_Msg.Reg_Num_H = 0x00;
- Uart_Receive_Msg.Reg_Num_L = 0x01;
- Data_Len = Uart_Receive_Msg.Reg_Num_L*2+5;
- memset(Uart_Rece_buffer,0x00,Data_Len);
- Uart_Rece_buffer = Uart_Receive_func(Uart_Receive_Msg,Uart_Rece_buffer,Data_Len);
- Uart_Rece_BattI = *(Uart_Rece_buffer+3+0)<<8 |*(Uart_Rece_buffer+3+1);
- break;
- }
- case 1://cell votage
- {
- Uart_Receive_Msg.Reg_Begin_H = 0x00;
- Uart_Receive_Msg.Reg_Begin_L = 0x02;
- Uart_Receive_Msg.Reg_Num_H = Batt_Cell_Num>>8;
- Uart_Receive_Msg.Reg_Num_L = Batt_Cell_Num;
- Data_Len = Uart_Receive_Msg.Reg_Num_L*2+5;
- memset(Uart_Rece_buffer,0x00,Data_Len);
- Uart_Rece_buffer = Uart_Receive_func(Uart_Receive_Msg,Uart_Rece_buffer,Data_Len);
- battbuffer[30] = Batt_Cell_Num;
- memcpy(&battbuffer[31],Uart_Rece_buffer+3,Batt_Cell_Num_2);
- break;
- }
- case 2://temprature
- {
- Uart_Receive_Msg.Reg_Begin_H = 0x00;
- Uart_Receive_Msg.Reg_Begin_L = 0x06+Batt_Cell_Num;
- Uart_Receive_Msg.Reg_Num_H = Batt_Temp_Num>>8;
- Uart_Receive_Msg.Reg_Num_L = Batt_Temp_Num;
- Data_Len = Uart_Receive_Msg.Reg_Num_L*2+5;
- memset(Uart_Rece_buffer,0x00,Data_Len);
- Uart_Rece_buffer = Uart_Receive_func(Uart_Receive_Msg,Uart_Rece_buffer,Data_Len);
- battbuffer[31+Batt_Cell_Num_2] = Batt_Temp_Num;
- for (int i = 0; i < Batt_Temp_Num; i++)
- {
- battbuffer[32+Batt_Cell_Num_2+i] = *(Uart_Rece_buffer+3+2*i+1);
- }
- break;
- }
- case 3://batt votage
- {
- Uart_Receive_Msg.Reg_Begin_H = 0x00;
- Uart_Receive_Msg.Reg_Begin_L = 0x18+Batt_Cell_Num+Batt_Temp_Num;
- Uart_Receive_Msg.Reg_Num_H = 0x00;
- Uart_Receive_Msg.Reg_Num_L = 0x01;
- Data_Len = Uart_Receive_Msg.Reg_Num_L*2+5;
- memset(Uart_Rece_buffer,0x00,Data_Len);
- Uart_Rece_buffer = Uart_Receive_func(Uart_Receive_Msg,Uart_Rece_buffer,Data_Len);
- battbuffer[19] = *(Uart_Rece_buffer+3+0);//Link U
- battbuffer[20] = *(Uart_Rece_buffer+3+1);
- battbuffer[21] = *(Uart_Rece_buffer+3+0);//Pack U
- battbuffer[22] = *(Uart_Rece_buffer+3+1);
- break;
- }
- case 4://soc,soh
- {
- Uart_Receive_Msg.Reg_Begin_H = 0x00;
- Uart_Receive_Msg.Reg_Begin_L = 0x09+Batt_Cell_Num+Batt_Temp_Num;
- Uart_Receive_Msg.Reg_Num_H = 0x00;
- Uart_Receive_Msg.Reg_Num_L = 0x04;
- Data_Len = Uart_Receive_Msg.Reg_Num_L*2+5;
- memset(Uart_Rece_buffer,0x00,Data_Len);
- Uart_Rece_buffer = Uart_Receive_func(Uart_Receive_Msg,Uart_Rece_buffer,Data_Len);
- battbuffer[23] = *(Uart_Rece_buffer+3+0)>>1;//mos状态
- battbuffer[24] = *(Uart_Rece_buffer+3+5);//SOC
- battbuffer[25] = *(Uart_Rece_buffer+3+7);//SOH
- break;
- }
- case 5://均衡
- {
- Uart_Receive_Msg.Reg_Begin_H = 0x00;
- Uart_Receive_Msg.Reg_Begin_L = 0x06+Batt_Cell_Num+Batt_Temp_Num;
- Uart_Receive_Msg.Reg_Num_H = 0x00;
- Uart_Receive_Msg.Reg_Num_L = 0x02;
- Data_Len = Uart_Receive_Msg.Reg_Num_L*2+5;
- memset(Uart_Rece_buffer,0x00,Data_Len);
- Uart_Rece_buffer = Uart_Receive_func(Uart_Receive_Msg,Uart_Rece_buffer,Data_Len);
- memcpy(&battbuffer[26],Uart_Rece_buffer+3,4);
- break;
- }
- case 6:
- {
- Uart_Receive_Msg.Reg_Begin_H = 0x00;
- Uart_Receive_Msg.Reg_Begin_L = 0x19+Batt_Cell_Num+Batt_Temp_Num;
- Uart_Receive_Msg.Reg_Num_H = 0x00;
- Uart_Receive_Msg.Reg_Num_L = 0x02;
- Data_Len = Uart_Receive_Msg.Reg_Num_L*2+5;
- memset(Uart_Rece_buffer,0x00,Data_Len);
- Uart_Rece_buffer = Uart_Receive_func(Uart_Receive_Msg,Uart_Rece_buffer,Data_Len);
- memcpy(&battbuffer[34+Batt_Cell_Num_2+Batt_Temp_Num],Uart_Rece_buffer+3,4);
- break;
- }
- default:
- {
- PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_IDLE);
- Uart_task = true;
- break;
- }
- }
- Rece_index++;
- }
- free(Uart_Rece_buffer);
- break;
- }
- case PROCESS_UART_STATE_SLEEP:
- {
- slpManPlatVoteEnableSleep(UartSlpHandler, SLP_SLP2_STATE);
- #ifdef USING_PRINTF
- printf("UART_STATE_SLEEP\n");
- #endif
- Uart_Sleep_Flag = true;
- while(1)
- {
- osDelay(60000/portTICK_PERIOD_MS);
- }
- }
- }
- }
- }
- //Can-线程任务
- static void Can_Task(void* arg)
- {
- uint32_t Can_ID;
- Can_InitType param;
- Can_TxMsgType Can_TxMsg;
- param.baudrate = CAN_500Kbps;
- param.mode = REQOP_NORMAL;
- //过滤ID配置
- param.TxStdIDH = 0x00;
- param.TxStdIDL = 0x00;
- param.RxStdIDH[0] = 0x00;
- param.RxStdIDL[0] = 0x00;
- /*stdid 0000 0000 001x*/
- param.RxStdIDH[1] = 0x00;
- param.RxStdIDL[1] = 0x20;
- /*stdid 0000 0000 010x */
- param.RxStdIDH[2] = 0x00;
- param.RxStdIDL[2] = 0x40;
- /*stdid 0000 0000 011x*/
- param.RxStdIDH[3] = 0x00;
- param.RxStdIDL[3] =0x60;
- /*stdid 0000 0000 100x */
- param.RxStdIDH[4] = 0x00;
- param.RxStdIDL[4] = 0x80;
- /*stdid 0000 0000 101x*/
- param.RxStdIDH[5] = 0x00;
- param.RxStdIDL[5] =0xa0;
- //配置完毕
- param.packType = STD_PACK;
- HAL_Can_Init(param);
- int send_index = 0;
- uint16_t Batt_Cell_addU = 0x0000;
- slpManApplyPlatVoteHandle("CanSlp",&CanSlpHandler);
- slpManPlatVoteDisableSleep(CanSlpHandler, SLP_SLP2_STATE);
- volatile bool Can_Flag=false;
- PROC_CAN_STATE_SWITCH(PROCESS_CAN_STATE_IDLE);
- posGGAServiceStart(GGACallBack);
- while(1)
- {
- switch(gProcess_Can_Task)
- {
- case PROCESS_CAN_STATE_IDLE:
- {
- Can_Rece_buffer[0]=0xff;
- send_index = 0;
- break;
- }
- case PROCESS_CAN_STATE_RECV:
- {
- //加入GPS启动程序
- if(gpsMsgQueue == NULL)
- {
- gpsMsgQueue = osMessageQueueNew(1,sizeof(GPS_INFO), NULL);
- }
- osMessageQueueGet(gpsMsgQueue, &Gps_Data, 0, 1000);
- #ifdef USING_PRINTF
- printf("Gpsmsgqueue: %d,%d,%d,%d\r\n\n",Gps_Data.latitude,Gps_Data.longitude,Gps_Data.speed,Gps_Data.direction);
- #endif
-
- //can采用先接收后发送的策略
- HAL_Can_Receive(Can_Rece_buffer);
- if (Can_Rece_buffer[0]!=0xff)//满足can发送使能
- {
- PROC_CAN_STATE_SWITCH(PROCESS_CAN_STATE_SEND);
- Can_Enable = true;
- }
- else
- {
- PROC_CAN_STATE_SWITCH(PROCESS_CAN_STATE_IDLE);
- Can_Enable = false;
- }
- break;
- }
- case PROCESS_CAN_STATE_SEND:
- {
- #ifdef USING_PRINTF
- printf("CAN_STATE_SEND!\n");
- #endif
- Can_Flag=false;
- while(!Can_Flag)
- {
- switch(send_index)
- {
- case 0:
- {
- Can_ID = 0x001;
- for (int i = 0; i < 8; i++)
- {
- Can_TxMsg.Data[i] = battbuffer[i+31+send_index*8];
- }
- Can_TxMsg.stdIDH = Can_ID>>3;
- Can_TxMsg.stdIDL = Can_ID<<5;
- Can_TxMsg.DLC = 8;
- HAL_Can_Transmit(Can_TxMsg);
- break;
- }
- case 1:
- {
- Can_ID = 0x011;
- for (int i = 0; i < 8; i++)
- {
- Can_TxMsg.Data[i] = battbuffer[i+31+send_index*8];
- }
- Can_TxMsg.stdIDH = Can_ID>>3;
- Can_TxMsg.stdIDL = Can_ID<<5;
- Can_TxMsg.DLC = 8;
- HAL_Can_Transmit(Can_TxMsg);
- break;
- }
- case 2:
- {
- Can_ID = 0x020;
- for (int i = 0; i < 8; i++)
- {
- Can_TxMsg.Data[i] = battbuffer[i+31+send_index*8];
- }
- Can_TxMsg.stdIDH = Can_ID>>3;
- Can_TxMsg.stdIDL = Can_ID<<5;
- Can_TxMsg.DLC = 8;
- HAL_Can_Transmit(Can_TxMsg);
- break;
- }
- case 3:
- {
- Can_ID = 0x031;
- for (int i = 0; i < 8; i++)
- {
- Can_TxMsg.Data[i] = battbuffer[i+31+send_index*8];
- }
- Can_TxMsg.stdIDH = Can_ID>>3;
- Can_TxMsg.stdIDL = Can_ID<<5;
- Can_TxMsg.DLC = 8;
- HAL_Can_Transmit(Can_TxMsg);
- break;
- }
- case 8:
- {
- Can_ID = 0x041;
- for (int i = 0; i < 2; i++)
- {
- Can_TxMsg.Data[i] = battbuffer[i+31+4*8];
- }
- for (int i = 0; i < 6; i++)
- {
- Can_TxMsg.Data[i+2] = 0x00;
- }
- Can_TxMsg.stdIDH = Can_ID>>3;
- Can_TxMsg.stdIDL = Can_ID<<5;
- Can_TxMsg.DLC = 8;
- HAL_Can_Transmit(Can_TxMsg);
- break;
- }
- case 4:
- {
- Can_ID = 0x101;
- for (int i = 0; i < Batt_Temp_Num; i++)
- {
- Can_TxMsg.Data[i] = battbuffer[i+32+Batt_Cell_Num_2];
- }
- Can_TxMsg.stdIDH = Can_ID>>3;
- Can_TxMsg.stdIDL = Can_ID<<5;
- Can_TxMsg.DLC = 8;
- HAL_Can_Transmit(Can_TxMsg);
- break;
- }
- case 5:
- {
- Batt_Cell_addU = 0x0000;
- for (int i = 0; i < Batt_Cell_Num; i++)
- {
- Batt_Cell_addU = Batt_Cell_addU + (battbuffer[31+i*2]<<8|battbuffer[31+i*2+1]);
- }
- Can_ID = 0x201;
- Can_TxMsg.Data[0] = battbuffer[19];
- Can_TxMsg.Data[1] = battbuffer[20];
- Can_TxMsg.Data[2] = battbuffer[21];
- Can_TxMsg.Data[3] = battbuffer[22];//外电压
- Can_TxMsg.Data[4] = Batt_Cell_addU>>8;
- Can_TxMsg.Data[5] = Batt_Cell_addU;//累加电压
- Can_TxMsg.Data[6] = Uart_Rece_BattI>>8;
- Can_TxMsg.Data[7] = Uart_Rece_BattI;
- Can_TxMsg.stdIDH = Can_ID>>3;
- Can_TxMsg.stdIDL = Can_ID<<5;
- Can_TxMsg.DLC = 8;
- HAL_Can_Transmit(Can_TxMsg);
- break;
- }
- case 6:
- {
- Can_ID = 0x211;
- Can_TxMsg.Data[0] = battbuffer[34+Batt_Cell_Num_2+Batt_Temp_Num];
- Can_TxMsg.Data[1] = battbuffer[35+Batt_Cell_Num_2+Batt_Temp_Num];
- Can_TxMsg.Data[2] = battbuffer[36+Batt_Cell_Num_2+Batt_Temp_Num];
- Can_TxMsg.Data[3] = battbuffer[37+Batt_Cell_Num_2+Batt_Temp_Num];
- Can_TxMsg.Data[4] = 0x00;
- Can_TxMsg.Data[5] = 0x00;
- Can_TxMsg.Data[6] = battbuffer[24];
- Can_TxMsg.Data[7] = Batt_Temp_Num<<4|0;
- Can_TxMsg.stdIDH = Can_ID>>3;
- Can_TxMsg.stdIDL = Can_ID<<5;
- Can_TxMsg.DLC = 8;
- HAL_Can_Transmit(Can_TxMsg);
- break;
- }
- case 7:
- {
- Can_ID = 0x221;
- Can_TxMsg.Data[0] = battbuffer[25];//SOH
- Can_TxMsg.Data[1] = 0x00;
- Can_TxMsg.Data[2] = 0x00;
- Can_TxMsg.Data[3] = 0x00;
- Can_TxMsg.Data[4] = 0x00;
- Can_TxMsg.Data[5] = 0x00;
- Can_TxMsg.Data[6] = 0x00;
- Can_TxMsg.Data[7] = 0x00;
- Can_TxMsg.stdIDH = Can_ID>>3;
- Can_TxMsg.stdIDL = Can_ID<<5;
- Can_TxMsg.DLC = 8;
- HAL_Can_Transmit(Can_TxMsg);
- break;
- }
- default:
- {
- Can_Flag=true;
- PROC_CAN_STATE_SWITCH(PROCESS_CAN_STATE_IDLE);
- break;
- }
- }
- send_index ++;
- }
- break;
- }
- case PROCESS_CAN_STATE_SLEEP:
- {
- posGGAServiceStop();
- HAL_Can_Sleep();
- slpManSlpState_t State;
- uint8_t cnt;
- if(slpManCheckVoteState(CanSlpHandler, &State, &cnt)==RET_TRUE)
- {
- #ifdef USING_PRINTF
- printf("[%d]We Can Check Vote Can State, state=%d, cnt=%d\r\n",__LINE__,State,cnt);
- #endif
- }
- slpManPlatVoteEnableSleep(CanSlpHandler, SLP_SLP2_STATE);
- if(slpManCheckVoteState(CanSlpHandler, &State, &cnt)==RET_TRUE)
- {
- #ifdef USING_PRINTF
- printf("[%d]We Can Check Vote can State Again, state=%d, cnt=%d\r\n",__LINE__,State,cnt);
- #endif
- }
- Can_Sleep_Flag = true;
- while(true)
- {
- osDelay(60000/portTICK_PERIOD_MS);
- }
- }
- }
- }
- }
- static void sendQueueMsg(UINT32 msgId, UINT32 xTickstoWait)
- {
- eventCallbackMessage_t *queueMsg = NULL;
- queueMsg = malloc(sizeof(eventCallbackMessage_t));
- queueMsg->messageId = msgId;
- if (psEventQueueHandle)
- {
- if (pdTRUE != xQueueSend(psEventQueueHandle, &queueMsg, xTickstoWait))
- {
- ECOMM_TRACE(UNILOG_PLA_APP, sendQueueMsg_1, P_ERROR, 0, "xQueueSend error");
- }
- }
- }
- static INT32 socketRegisterPSUrcCallback(urcID_t eventID, void *param, UINT32 paramLen)
- {
- CmiSimImsiStr *imsi = NULL;
- CmiPsCeregInd *cereg = NULL;
- UINT8 rssi = 0, index = 0;
- NmAtiNetifInfo *netif = NULL;
- switch(eventID)
- {
- case NB_URC_ID_SIM_READY:
- {
- imsi = (CmiSimImsiStr *)param;
- memcpy(gImsi, imsi->contents, imsi->length);
- #ifdef USING_PRINTF
- printf("SIM ready(imsi=%s)\n",(UINT8 *)imsi->contents);
- #endif
- break;
- }
- case NB_URC_ID_MM_SIGQ:
- {
- rssi = *(UINT8 *)param;
- #ifdef USING_PRINTF
- printf("RSSI signal=%d\n",rssi);
- #endif
- ECOMM_TRACE(UNILOG_PLA_APP, socketRegisterPSUrcCallback_1, P_INFO, 1, "RSSI signal=%d", rssi);
- break;
- }
- case NB_URC_ID_PS_BEARER_ACTED:
- {
- #ifdef USING_PRINTF
- printf("Default bearer activated\n");
- #endif
- break;
- }
- case NB_URC_ID_PS_BEARER_DEACTED:
- {
- #ifdef USING_PRINTF
- printf("Default bearer Deactivated\n");
- #endif
- break;
- }
- case NB_URC_ID_PS_CEREG_CHANGED:
- {
- cereg = (CmiPsCeregInd *)param;
- gCellID = cereg->celId;
- #ifdef USING_PRINTF
- printf("URCCallBack:CEREG changed act:%d celId:%d locPresent:%d tac:%d\n", cereg->act, cereg->celId, cereg->locPresent, cereg->tac);
- #endif
- break;
- }
- case NB_URC_ID_PS_NETINFO:
- {
- netif = (NmAtiNetifInfo *)param;
- if (netif->netStatus == NM_NETIF_ACTIVATED)
- sendQueueMsg(QMSG_ID_NW_IP_READY, 0);
- tcp_ready = true;
- break;
- }
- }
- return 0;
- }
- uint8_t bcc_chk(uint8_t* data, uint8_t length)//发送bcc校验函数
- {
- uint8_t bcc_chk_return = 0x00;
- uint8_t count = 0;
- while (count<length)
- {
- bcc_chk_return^=data[count];
- count++;
- }
- return bcc_chk_return;
- }
- //电池数据组装函数
- void Tcp_Batt_Data_Assemble(void)
- {
- int16_t Batt_current;
- uint16_t BattU = 0x00;
- uint8_t csq=0;
- int8_t snr=0;
- int8_t rsnr=0;
- Batt_current = Uart_Rece_BattI;
- OsaUtcTimeTValue timestracture;
- appGetSystemTimeUtcSync(×tracture);
- battbuffer[0] = timestracture.UTCtimer1>>16;
- battbuffer[0] = battbuffer[0] - 0x07D0;
- battbuffer[1] = timestracture.UTCtimer1>>8;
- battbuffer[2] = timestracture.UTCtimer1;
- battbuffer[3] = timestracture.UTCtimer2>>24;
- battbuffer[4] = timestracture.UTCtimer2>>16;
- battbuffer[5] = timestracture.UTCtimer2>>8;
- battbuffer[6] = 0x80;//信息体标志,此处为电池信息
- battbuffer[7] = battbuffer[0];//年
- battbuffer[8] = battbuffer[1];//月
- battbuffer[9] = battbuffer[2];//日
- battbuffer[10] = battbuffer[3];//时 0时区时间
- battbuffer[11] = battbuffer[4];//分
- battbuffer[12] = battbuffer[5];//秒
- appGetSignalInfoSync(&csq,&snr,&rsnr);//获取信号质量
- battbuffer[13] = csq;// 网络信号
- battbuffer[14] = 0x00;//故障等级
- battbuffer[15] = 0x00;//故障代码高
- battbuffer[16] = 0x00;//故障代码低
- //电流适应性更改,从int转换到uint,加10000的偏移量,100mA的单位
- if (Batt_current>0x8000)
- {
- Batt_current = Batt_current|0x7fff;
- Batt_current = 0x2710 - Batt_current;
- Batt_current = Batt_current;
- }
- else
- {
- Batt_current = Batt_current+0x2710;
- Batt_current = Batt_current;
- }
- battbuffer[17] = Batt_current>>8;
- battbuffer[18] = Batt_current;
- BattU = battbuffer[19]<<8|battbuffer[20];
- BattU = BattU/10;
- battbuffer[19] = BattU>>8;
- battbuffer[20] = BattU;
- battbuffer[21] = BattU>>8;
- battbuffer[22] = BattU;//外电压
- data_index = 32+Batt_Cell_Num_2+Batt_Temp_Num;
- battbuffer[data_index] = 0x00;//电池状态
- data_index++;
- battbuffer[data_index] = 0x00;//是否加热
- data_index++;
- }
- //Gps 数据组装函数
- void Tcp_Gps_Data_Assemble(void)
- {
- OsaUtcTimeTValue timestracture;
- appGetSystemTimeUtcSync(×tracture);
- gpsbuffer[0] = timestracture.UTCtimer1>>16;
- gpsbuffer[0] = gpsbuffer[0] - 0x07D0;
- gpsbuffer[1] = timestracture.UTCtimer1>>8;
- gpsbuffer[2] = timestracture.UTCtimer1;
- gpsbuffer[3] = timestracture.UTCtimer2>>24;
- gpsbuffer[4] = timestracture.UTCtimer2>>16;
- gpsbuffer[5] = timestracture.UTCtimer2>>8;
- gpsbuffer[6] = 0x82;//信息体标志,此处为GPS信息
- gpsbuffer[7] = gpsbuffer[0];//年
- gpsbuffer[8] = gpsbuffer[1];//月
- gpsbuffer[9] = gpsbuffer[2];//日
- gpsbuffer[10] = gpsbuffer[3];//时 0时区时间
- gpsbuffer[11] = gpsbuffer[4];//分
- gpsbuffer[12] = gpsbuffer[5];//秒
- gpsbuffer[13] = Gps_Data.status;
- gpsbuffer[14] = Gps_Data.satellite_num;
- gpsbuffer[15] = Gps_Data.direction>>8;
- gpsbuffer[16] = Gps_Data.direction;
- gpsbuffer[17] = Gps_Data.speed>>8;
- gpsbuffer[18] = Gps_Data.speed;
- gpsbuffer[19] = Gps_Data.altitude>>8;
- gpsbuffer[20] = Gps_Data.altitude;
- gpsbuffer[21] = Gps_Data.latitude>>24;
- gpsbuffer[22] = Gps_Data.latitude>>16;
- gpsbuffer[23] = Gps_Data.latitude>>8;
- gpsbuffer[24] = Gps_Data.latitude;
- gpsbuffer[25] = Gps_Data.longitude>>24;
- gpsbuffer[26] = Gps_Data.longitude>>16;
- gpsbuffer[27] = Gps_Data.longitude>>8;
- gpsbuffer[28] = Gps_Data.longitude;
- }
- //Tcp线程
- static void Tcp_Task(void* arg)
- {
- CHAR SN[] = "GYTEST00000000003";//SN应写到osfile里面
- CHAR serverip[] = "47.97.127.222";
- CHAR serverport[] = "8712";
- struct addrinfo hints, *server_res;
- int TcpsendID = -1;
- memset( &hints, 0, sizeof( hints ) );
- hints.ai_socktype = SOCK_STREAM;
- hints.ai_protocol = IPPROTO_TCP;
- psEventQueueHandle = xQueueCreate(APP_EVENT_QUEUE_SIZE, sizeof(eventCallbackMessage_t*));
- slpManApplyPlatVoteHandle("TcpSlp",&TcpSlpHandler);
- slpManPlatVoteDisableSleep(TcpSlpHandler, SLP_SLP2_STATE);
- PROC_TCP_STATE_SWITCH(PROCESS_TCP_STATE_IDLE);
- uint8_t Tcp_Index = 0;
- uint8_t* TcpSendBuffer=NULL;
- volatile bool Tcp_Flag=false;
- eventCallbackMessage_t *queueItem = NULL;
- uint8_t pmode;
- uint32_t tau;
- uint32_t act;
- uint8_t NB_send_len;
- appSetEDRXSettingSync(0,5,0);
- appSetPSMSettingSync(1,3*60*60,10);
- #ifdef USING_PRINTF
- printf("psm:pmode-%d,tau-%d,act-%d!\n",pmode,tau,act);
- #endif
- while(true)
- {
- switch(gProcess_Tcp_Task)
- {
- case PROCESS_TCP_STATE_IDLE:
- {
- osDelay(100);
- break;
- }
- case PROCESS_TCP_STATE_LINK:
- {
- if(!tcp_ready)
- {
- #ifdef USING_PRINTF
- printf("tcp not ready!\n");
- #endif
- PROC_TCP_STATE_SWITCH(PROCESS_TCP_STATE_IDLE);
- }
- else
- {
- if(TcpconnectID<0)
- {
- #ifdef USING_PRINTF
- printf("Connecting...!%d\n",TcpconnectID);
- #endif
- if (getaddrinfo( serverip, serverport , &hints, &server_res ) != 0 )
- {
- #ifdef USING_PRINTF
- printf("TCP connect unresolved dns\n");
- #endif
- }
- sockfd = socket(AF_INET, SOCK_STREAM, 0);
- if(sockfd<0)
- {
- #ifdef USING_PRINTF
- printf("socket create error\n");
- #endif
- }
- TcpconnectID = connect(sockfd, (struct sockaddr *) server_res->ai_addr, server_res->ai_addrlen);
- if(TcpconnectID<0 && errno != EINPROGRESS)
- {
- #ifdef USING_PRINTF
- printf("Connect failed!%d\n",TcpconnectID);
- #endif
- closesocket(sockfd);
- PROC_TCP_STATE_SWITCH(PROCESS_TCP_STATE_IDLE);
- break;
- }
- else
- {
- PROC_TCP_STATE_SWITCH(PROCESS_TCP_STATE_WORK);
- break;
- }
- }
- else
- {
- PROC_TCP_STATE_SWITCH(PROCESS_TCP_STATE_WORK);
- }
- }
- #ifdef USING_PRINTF
- printf("tcp_statuts,%d,%d!\n",tcp_ready,TcpconnectID);
- #endif
- break;
- }
- case PROCESS_TCP_STATE_WORK:
- {
- #ifdef USING_PRINTF
- printf("TCP_STATE_WORK!\n");
- #endif
- appGetPSMSettingSync(&pmode,&tau,&act);
- #ifdef USING_PRINTF
- printf("psm:pmode-%d,tau-%d,act-%d!\n",pmode,tau,act);
- #endif
- Tcp_Flag = false;
- while (!Tcp_Flag)
- {
- switch(Tcp_Index)
- {
- case 0://发送登录信息
- {
- Tcp_Index=1;
- break;
- }
- case 1://发送电池数据
- {
- NB_send_len=59+Batt_Cell_Num_2+Batt_Temp_Num;//电池数据长度
- TcpSendBuffer = (uint8_t *)malloc(NB_send_len);
- data_index = 0;
- memset(TcpSendBuffer,0x00,NB_send_len);
- *(TcpSendBuffer+0) = 0x23;
- *(TcpSendBuffer+1) = 0x23;
- *(TcpSendBuffer+2) = 0x02;
- *(TcpSendBuffer+3) = 0xfe;
- memcpy(TcpSendBuffer+4,SN,17);
- *(TcpSendBuffer+21) = 0x01;//01表示不加密
- Tcp_Batt_Data_Assemble();//数据组装函数
- *(TcpSendBuffer+22) = data_index>>8;//数据长度
- *(TcpSendBuffer+23) = data_index;//数据长度
- memcpy(TcpSendBuffer+24,battbuffer,data_index);
- *(TcpSendBuffer+NB_send_len-1) = bcc_chk(TcpSendBuffer,NB_send_len-1);
- #ifdef USING_PRINTF
- printf("battbuffer:");
- for (int i = 0; i < data_index; i++)
- {
- printf("%x ",battbuffer[i]);
- }
- printf("\n");
- #endif
- TcpsendID = send(sockfd, TcpSendBuffer, NB_send_len, 0 );
- //发送失败暂时不写
- #ifdef USING_PRINTF
- printf("ConnectID:%d,TcpSend:%d,data length:%d,Data: ",sockfd,TcpsendID,NB_send_len);
- for (int i = 0; i < NB_send_len; i++)
- {
- printf("%x ",*(TcpSendBuffer+i));
- }
- printf("\n");
- #endif
- free(TcpSendBuffer);
- if (TcpsendID<0)
- {
- closesocket(sockfd);
- TcpconnectID = -1;
- Tcp_Index=-1;
- }
- else
- {
- Tcp_Index=2;
- }
- break;
- }
- case 2:
- {
- data_index = 29;
- NB_send_len = 25+29;//数据总长度:25个固定+29个GPS长度
- TcpSendBuffer = (uint8_t *)malloc(NB_send_len);
- memset(TcpSendBuffer,0x00,NB_send_len);
- *(TcpSendBuffer+0) = 0x23;
- *(TcpSendBuffer+1) = 0x23;
- *(TcpSendBuffer+2) = 0x02;
- *(TcpSendBuffer+3) = 0xfe;
- memcpy(TcpSendBuffer+4,SN,17);
- *(TcpSendBuffer+21) = 0x01;//01表示不加密
- Tcp_Gps_Data_Assemble();//Gps数据组装
- *(TcpSendBuffer+22) = data_index>>8;//数据长度
- *(TcpSendBuffer+23) = data_index;//数据长度
- memcpy(TcpSendBuffer+24,gpsbuffer,data_index);
- *(TcpSendBuffer+NB_send_len-1) = bcc_chk(TcpSendBuffer,NB_send_len-1);
- #ifdef USING_PRINTF
- printf("ConnectID:%d,TcpSend:%d,data length:%d,Data: ",sockfd,TcpsendID,NB_send_len);
- for (int i = 0; i < NB_send_len; i++)
- {
- printf("%x ",*(TcpSendBuffer+i));
- }
- printf("\n");
- #endif
- TcpsendID = send(sockfd, TcpSendBuffer, NB_send_len, 0 );
- free(TcpSendBuffer);
- if (TcpsendID<0)
- {
- closesocket(sockfd);
- TcpconnectID = -1;
- Tcp_Index=-1;
- }
- else
- {
- Tcp_Index=3;
- }
- break;
- }
- default:
- {
- PROC_TCP_STATE_SWITCH(PROCESS_TCP_STATE_IDLE);
- Tcp_Index = 0;
- Tcp_Flag = true;
- }
- }
- }
- break;
- }
- case PROCESS_TCP_STATE_SLEEP:
- {
- slpManPlatVoteEnableSleep(TcpSlpHandler, SLP_SLP2_STATE);
- closesocket(sockfd);
- tcp_ready = false;
- deregisterPSEventCallback(socketRegisterPSUrcCallback);//注销NB网络事件函数
- #ifdef USING_PRINTF
- printf("TCP_STATE_SLEEP\n");
- #endif
- Tcp_Sleep_Flag = TRUE;
- while(1)
- {
- osDelay(30000/portTICK_PERIOD_MS);
- }
- }
- }
- }
- }
- //主任务线程初始化
- void Main_Task_Init()
- {
- #ifndef USING_PRINTF
- if(BSP_GetPlatConfigItemValue(PLAT_CONFIG_ITEM_LOG_CONTROL) != 0)
- {
- HAL_UART_RecvFlowControl(false);
- }
- #endif
- osThreadAttr_t task_attr;
- memset(&task_attr,0,sizeof(task_attr));
- memset(gProcess_Main_TaskStack, 0xA5, PROC_MAIN_TASK_STACK_SIZE);
- task_attr.name = "Main_Task";
- task_attr.stack_mem = gProcess_Main_TaskStack;
- task_attr.stack_size = PROC_MAIN_TASK_STACK_SIZE;
- task_attr.priority = osPriorityNormal;
- task_attr.cb_mem = &gProcess_Main_Task_t;
- task_attr.cb_size = sizeof(StaticTask_t);
- osThreadNew(Main_Task, NULL, &task_attr);
- }
- //Uart读取线程初始化
- void Uart_Task_Init()
- {
- osThreadAttr_t task_attr;
- memset(&task_attr,0,sizeof(task_attr));
- memset(gProcess_Uart_TaskStack, 0xA5, PROC_UART_TASK_STACK_SIZE);
- task_attr.name = "Uart_Task";
- task_attr.stack_mem = gProcess_Uart_TaskStack;
- task_attr.stack_size = PROC_UART_TASK_STACK_SIZE;
- task_attr.priority = osPriorityNormal;
- task_attr.cb_mem = &gProcess_Uart_Task_t;
- task_attr.cb_size = sizeof(StaticTask_t);
- osThreadNew(Uart_Task, NULL, &task_attr);
- }
- //Can线程初始化
- void Can_Task_Init()
- {
- osThreadAttr_t task_attr;
- memset(&task_attr,0,sizeof(task_attr));
- memset(gProcess_Can_TaskStack, 0xA5, PROC_CAN_TASK_STACK_SIZE);
- task_attr.name = "Can_Task";
- task_attr.stack_mem = gProcess_Can_TaskStack;
- task_attr.stack_size = PROC_CAN_TASK_STACK_SIZE;
- task_attr.priority = osPriorityNormal;
- task_attr.cb_mem = &gProcess_Can_Task_t;
- task_attr.cb_size = sizeof(StaticTask_t);
- osThreadNew(Can_Task, NULL, &task_attr);
- }
- //Tcp线程初始化
- void Tcp_Task_Init()
- {
- osThreadAttr_t task_attr;
- registerPSEventCallback(NB_GROUP_ALL_MASK, socketRegisterPSUrcCallback);
- memset(&task_attr,0,sizeof(task_attr));
- memset(gProcess_Tcp_TaskStack, 0xA5, PROC_TCP_TASK_STACK_SIZE);
- task_attr.name = "Tcp_Task";
- task_attr.stack_mem = gProcess_Tcp_TaskStack;
- task_attr.stack_size = PROC_TCP_TASK_STACK_SIZE;
- task_attr.priority = osPriorityNormal;
- task_attr.cb_mem = &gProcess_Tcp_Task_t;
- task_attr.cb_size = sizeof(StaticTask_t);
- osThreadNew(Tcp_Task, NULL, &task_attr);
- }
- void appInit(void *arg)
- {
- Main_Task_Init();
- Uart_Task_Init();
- Can_Task_Init();
- GpsTaskInit();
- Tcp_Task_Init();
- }
- //主函数入口
- void main_entry(void) {
- BSP_CommonInit();
- osKernelInitialize();
- registerAppEntry(appInit, NULL);
- if (osKernelGetState() == osKernelReady)
- {
- osKernelStart();
- }
- while(1);
- }
|